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