lavfi: use a new field for automatic buffer copy.
[ffmpeg.git] / libavfilter / buffer.c
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
23 #include "libavutil/audioconvert.h"
24 #include "libavutil/avassert.h"
25 #include "libavutil/imgutils.h"
26 #include "libavcodec/avcodec.h"
27
28 #include "avfilter.h"
29 #include "internal.h"
30 #include "audio.h"
31 #include "avcodec.h"
32
33 void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
34 {
35     if (ptr->extended_data != ptr->data)
36         av_freep(&ptr->extended_data);
37     av_free(ptr->data[0]);
38     av_free(ptr);
39 }
40
41 AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
42 {
43     AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
44     if (!ret)
45         return NULL;
46     *ret = *ref;
47     if (ref->type == AVMEDIA_TYPE_VIDEO) {
48         ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
49         if (!ret->video) {
50             av_free(ret);
51             return NULL;
52         }
53         *ret->video = *ref->video;
54         ret->extended_data = ret->data;
55     } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
56         ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
57         if (!ret->audio) {
58             av_free(ret);
59             return NULL;
60         }
61         *ret->audio = *ref->audio;
62
63         if (ref->extended_data && ref->extended_data != ref->data) {
64             int nb_channels = av_get_channel_layout_nb_channels(ref->audio->channel_layout);
65             if (!(ret->extended_data = av_malloc(sizeof(*ret->extended_data) *
66                                                  nb_channels))) {
67                 av_freep(&ret->audio);
68                 av_freep(&ret);
69                 return NULL;
70             }
71             memcpy(ret->extended_data, ref->extended_data,
72                    sizeof(*ret->extended_data) * nb_channels);
73         } else
74             ret->extended_data = ret->data;
75     }
76     ret->perms &= pmask;
77     ret->buf->refcount ++;
78     return ret;
79 }
80
81 void ff_free_pool(AVFilterPool *pool)
82 {
83     int i;
84
85     av_assert0(pool->refcount > 0);
86
87     for (i = 0; i < POOL_SIZE; i++) {
88         if (pool->pic[i]) {
89             AVFilterBufferRef *picref = pool->pic[i];
90             /* free buffer: picrefs stored in the pool are not
91              * supposed to contain a free callback */
92             av_assert0(!picref->buf->refcount);
93             av_freep(&picref->buf->data[0]);
94             av_freep(&picref->buf);
95
96             av_freep(&picref->audio);
97             av_freep(&picref->video);
98             av_freep(&pool->pic[i]);
99             pool->count--;
100         }
101     }
102     pool->draining = 1;
103
104     if (!--pool->refcount) {
105         av_assert0(!pool->count);
106         av_free(pool);
107     }
108 }
109
110 static void store_in_pool(AVFilterBufferRef *ref)
111 {
112     int i;
113     AVFilterPool *pool= ref->buf->priv;
114
115     av_assert0(ref->buf->data[0]);
116     av_assert0(pool->refcount>0);
117
118     if (pool->count == POOL_SIZE) {
119         AVFilterBufferRef *ref1 = pool->pic[0];
120         av_freep(&ref1->video);
121         av_freep(&ref1->audio);
122         av_freep(&ref1->buf->data[0]);
123         av_freep(&ref1->buf);
124         av_free(ref1);
125         memmove(&pool->pic[0], &pool->pic[1], sizeof(void*)*(POOL_SIZE-1));
126         pool->count--;
127         pool->pic[POOL_SIZE-1] = NULL;
128     }
129
130     for (i = 0; i < POOL_SIZE; i++) {
131         if (!pool->pic[i]) {
132             pool->pic[i] = ref;
133             pool->count++;
134             break;
135         }
136     }
137     if (pool->draining) {
138         ff_free_pool(pool);
139     } else
140         --pool->refcount;
141 }
142
143 void avfilter_unref_buffer(AVFilterBufferRef *ref)
144 {
145     if (!ref)
146         return;
147     av_assert0(ref->buf->refcount > 0);
148     if (!(--ref->buf->refcount)) {
149         if (!ref->buf->free) {
150             store_in_pool(ref);
151             return;
152         }
153         ref->buf->free(ref->buf);
154     }
155     if (ref->extended_data != ref->data)
156         av_freep(&ref->extended_data);
157     av_freep(&ref->video);
158     av_freep(&ref->audio);
159     av_free(ref);
160 }
161
162 void avfilter_unref_bufferp(AVFilterBufferRef **ref)
163 {
164     avfilter_unref_buffer(*ref);
165     *ref = NULL;
166 }
167
168 void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
169 {
170     // copy common properties
171     dst->pts             = src->pts;
172     dst->pos             = src->pos;
173
174     switch (src->type) {
175     case AVMEDIA_TYPE_VIDEO: *dst->video = *src->video; break;
176     case AVMEDIA_TYPE_AUDIO: *dst->audio = *src->audio; break;
177     default: break;
178     }
179 }
180
181 AVFilterBufferRef *ff_copy_buffer_ref(AVFilterLink *outlink,
182                                       AVFilterBufferRef *ref)
183 {
184     AVFilterBufferRef *buf;
185     int channels;
186
187     switch (outlink->type) {
188
189     case AVMEDIA_TYPE_VIDEO:
190         buf = ff_get_video_buffer(outlink, AV_PERM_WRITE,
191                                   ref->video->w, ref->video->h);
192         if(!buf)
193             return NULL;
194         av_image_copy(buf->data, buf->linesize,
195                       (void*)ref->data, ref->linesize,
196                       ref->format, ref->video->w, ref->video->h);
197         break;
198
199     case AVMEDIA_TYPE_AUDIO:
200         buf = ff_get_audio_buffer(outlink, AV_PERM_WRITE,
201                                         ref->audio->nb_samples);
202         if(!buf)
203             return NULL;
204         channels = av_get_channel_layout_nb_channels(ref->audio->channel_layout);
205         av_samples_copy(buf->extended_data, ref->buf->extended_data,
206                         0, 0, ref->audio->nb_samples,
207                         channels,
208                         ref->format);
209         break;
210
211     default:
212         return NULL;
213     }
214     avfilter_copy_buffer_ref_props(buf, ref);
215     return buf;
216 }