FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
buffer.c
Go to the documentation of this file.
1 /*
2  * Copyright Stefano Sabatini <stefasab gmail com>
3  * Copyright Anton Khirnov <anton khirnov net>
4  * Copyright Michael Niedermayer <michaelni gmx at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
24 #include "libavutil/avassert.h"
25 #include "libavutil/common.h"
26 #include "libavutil/imgutils.h"
27 #include "libavcodec/avcodec.h"
28 
29 #include "avfilter.h"
30 #include "internal.h"
31 #include "audio.h"
32 #include "avcodec.h"
33 #include "version.h"
34 
35 #if FF_API_AVFILTERBUFFER
36 void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
37 {
38  if (ptr->extended_data != ptr->data)
39  av_freep(&ptr->extended_data);
40  av_free(ptr->data[0]);
41  av_free(ptr);
42 }
43 
44 static void copy_video_props(AVFilterBufferRefVideoProps *dst, AVFilterBufferRefVideoProps *src) {
45  *dst = *src;
46  if (src->qp_table) {
47  int qsize = src->qp_table_size;
48  dst->qp_table = av_malloc(qsize);
49  memcpy(dst->qp_table, src->qp_table, qsize);
50  }
51 }
52 
53 AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
54 {
55  AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
56  if (!ret)
57  return NULL;
58  *ret = *ref;
59 
60  ret->metadata = NULL;
61  av_dict_copy(&ret->metadata, ref->metadata, 0);
62 
63  if (ref->type == AVMEDIA_TYPE_VIDEO) {
64  ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
65  if (!ret->video) {
66  av_free(ret);
67  return NULL;
68  }
69  copy_video_props(ret->video, ref->video);
70  ret->extended_data = ret->data;
71  } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
72  ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
73  if (!ret->audio) {
74  av_free(ret);
75  return NULL;
76  }
77  *ret->audio = *ref->audio;
78 
79  if (ref->extended_data && ref->extended_data != ref->data) {
80  int nb_channels = av_get_channel_layout_nb_channels(ref->audio->channel_layout);
81  if (!(ret->extended_data = av_malloc(sizeof(*ret->extended_data) *
82  nb_channels))) {
83  av_freep(&ret->audio);
84  av_freep(&ret);
85  return NULL;
86  }
87  memcpy(ret->extended_data, ref->extended_data,
88  sizeof(*ret->extended_data) * nb_channels);
89  } else
90  ret->extended_data = ret->data;
91  }
92  ret->perms &= pmask;
93  ret->buf->refcount ++;
94  return ret;
95 }
96 
97 void avfilter_unref_buffer(AVFilterBufferRef *ref)
98 {
99  if (!ref)
100  return;
101  av_assert0(ref->buf->refcount > 0);
102  if (!(--ref->buf->refcount))
103  ref->buf->free(ref->buf);
104  if (ref->extended_data != ref->data)
105  av_freep(&ref->extended_data);
106  if (ref->video)
107  av_freep(&ref->video->qp_table);
108  av_freep(&ref->video);
109  av_freep(&ref->audio);
110  av_dict_free(&ref->metadata);
111  av_free(ref);
112 }
113 
114 void avfilter_unref_bufferp(AVFilterBufferRef **ref)
115 {
116  avfilter_unref_buffer(*ref);
117  *ref = NULL;
118 }
119 
120 int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
121 {
122  dst->pts = src->pts;
123  dst->pos = av_frame_get_pkt_pos(src);
124  dst->format = src->format;
125 
126  av_dict_free(&dst->metadata);
127  av_dict_copy(&dst->metadata, av_frame_get_metadata(src), 0);
128 
129  switch (dst->type) {
130  case AVMEDIA_TYPE_VIDEO:
131  dst->video->w = src->width;
132  dst->video->h = src->height;
133  dst->video->sample_aspect_ratio = src->sample_aspect_ratio;
134  dst->video->interlaced = src->interlaced_frame;
135  dst->video->top_field_first = src->top_field_first;
136  dst->video->key_frame = src->key_frame;
137  dst->video->pict_type = src->pict_type;
138  break;
139  case AVMEDIA_TYPE_AUDIO:
140  dst->audio->sample_rate = src->sample_rate;
141  dst->audio->channel_layout = src->channel_layout;
142  break;
143  default:
144  return AVERROR(EINVAL);
145  }
146 
147  return 0;
148 }
149 
150 void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
151 {
152  // copy common properties
153  dst->pts = src->pts;
154  dst->pos = src->pos;
155 
156  switch (src->type) {
157  case AVMEDIA_TYPE_VIDEO: {
158  if (dst->video->qp_table)
159  av_freep(&dst->video->qp_table);
160  copy_video_props(dst->video, src->video);
161  break;
162  }
163  case AVMEDIA_TYPE_AUDIO: *dst->audio = *src->audio; break;
164  default: break;
165  }
166 
167  av_dict_free(&dst->metadata);
168  av_dict_copy(&dst->metadata, src->metadata, 0);
169 }
170 #endif /* FF_API_AVFILTERBUFFER */