2 * Copyright Stefano Sabatini <stefasab gmail com>
3 * Copyright Anton Khirnov <anton khirnov net>
4 * Copyright Michael Niedermayer <michaelni gmx at>
6 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavutil/channel_layout.h"
24 #include "libavutil/avassert.h"
25 #include "libavutil/common.h"
26 #include "libavutil/imgutils.h"
27 #include "libavcodec/avcodec.h"
34 void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
36 if (ptr->extended_data != ptr->data)
37 av_freep(&ptr->extended_data);
38 av_free(ptr->data[0]);
42 static void copy_video_props(AVFilterBufferRefVideoProps *dst, AVFilterBufferRefVideoProps *src) {
45 int qsize = src->qp_table_size;
46 dst->qp_table = av_malloc(qsize);
47 memcpy(dst->qp_table, src->qp_table, qsize);
51 AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
53 AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
59 av_dict_copy(&ret->metadata, ref->metadata, 0);
61 if (ref->type == AVMEDIA_TYPE_VIDEO) {
62 ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
67 copy_video_props(ret->video, ref->video);
68 ret->extended_data = ret->data;
69 } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
70 ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
75 *ret->audio = *ref->audio;
77 if (ref->extended_data && ref->extended_data != ref->data) {
78 int nb_channels = av_get_channel_layout_nb_channels(ref->audio->channel_layout);
79 if (!(ret->extended_data = av_malloc(sizeof(*ret->extended_data) *
81 av_freep(&ret->audio);
85 memcpy(ret->extended_data, ref->extended_data,
86 sizeof(*ret->extended_data) * nb_channels);
88 ret->extended_data = ret->data;
91 ret->buf->refcount ++;
95 void ff_free_pool(AVFilterPool *pool)
99 av_assert0(pool->refcount > 0);
101 for (i = 0; i < POOL_SIZE; i++) {
103 AVFilterBufferRef *picref = pool->pic[i];
104 /* free buffer: picrefs stored in the pool are not
105 * supposed to contain a free callback */
106 av_assert0(!picref->buf->refcount);
107 av_freep(&picref->buf->data[0]);
108 av_freep(&picref->buf);
110 av_freep(&picref->audio);
111 av_assert0(!picref->video || !picref->video->qp_table);
112 av_freep(&picref->video);
113 av_freep(&pool->pic[i]);
119 if (!--pool->refcount) {
120 av_assert0(!pool->count);
125 static void store_in_pool(AVFilterBufferRef *ref)
128 AVFilterPool *pool= ref->buf->priv;
130 av_assert0(ref->buf->data[0]);
131 av_assert0(pool->refcount>0);
134 av_freep(&ref->video->qp_table);
136 if (pool->count == POOL_SIZE) {
137 AVFilterBufferRef *ref1 = pool->pic[0];
138 av_freep(&ref1->video);
139 av_freep(&ref1->audio);
140 av_freep(&ref1->buf->data[0]);
141 av_freep(&ref1->buf);
143 memmove(&pool->pic[0], &pool->pic[1], sizeof(void*)*(POOL_SIZE-1));
145 pool->pic[POOL_SIZE-1] = NULL;
148 for (i = 0; i < POOL_SIZE; i++) {
155 if (pool->draining) {
161 void avfilter_unref_buffer(AVFilterBufferRef *ref)
165 av_assert0(ref->buf->refcount > 0);
166 if (!(--ref->buf->refcount)) {
167 if (!ref->buf->free) {
171 ref->buf->free(ref->buf);
173 if (ref->extended_data != ref->data)
174 av_freep(&ref->extended_data);
176 av_freep(&ref->video->qp_table);
177 av_freep(&ref->video);
178 av_freep(&ref->audio);
179 av_dict_free(&ref->metadata);
183 void avfilter_unref_bufferp(AVFilterBufferRef **ref)
185 avfilter_unref_buffer(*ref);
189 void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
191 // copy common properties
196 case AVMEDIA_TYPE_VIDEO: {
197 if (dst->video->qp_table)
198 av_freep(&dst->video->qp_table);
199 copy_video_props(dst->video, src->video);
202 case AVMEDIA_TYPE_AUDIO: *dst->audio = *src->audio; break;
206 av_dict_free(&dst->metadata);
207 av_dict_copy(&dst->metadata, src->metadata, 0);
210 AVFilterBufferRef *ff_copy_buffer_ref(AVFilterLink *outlink,
211 AVFilterBufferRef *ref)
213 AVFilterBufferRef *buf;
216 switch (outlink->type) {
218 case AVMEDIA_TYPE_VIDEO:
219 buf = ff_get_video_buffer(outlink, AV_PERM_WRITE,
220 ref->video->w, ref->video->h);
223 av_image_copy(buf->data, buf->linesize,
224 (void*)ref->data, ref->linesize,
225 ref->format, ref->video->w, ref->video->h);
228 case AVMEDIA_TYPE_AUDIO:
229 buf = ff_get_audio_buffer(outlink, AV_PERM_WRITE,
230 ref->audio->nb_samples);
233 channels = ref->audio->channels;
234 av_samples_copy(buf->extended_data, ref->buf->extended_data,
235 0, 0, ref->audio->nb_samples,
243 avfilter_copy_buffer_ref_props(buf, ref);