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