]> git.sesse.net Git - ffmpeg/blob - libavfilter/buffer.c
Merge remote-tracking branch 'qatar/master'
[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_buf_props(AVFrame *dst, const AVFilterBufferRef *src)
167 {
168     int planes, nb_channels;
169
170     memcpy(dst->data, src->data, sizeof(dst->data));
171     memcpy(dst->linesize, src->linesize, sizeof(dst->linesize));
172
173     dst->pts     = src->pts;
174     dst->format  = src->format;
175
176     switch (src->type) {
177     case AVMEDIA_TYPE_VIDEO:
178         dst->width               = src->video->w;
179         dst->height              = src->video->h;
180         dst->sample_aspect_ratio = src->video->sample_aspect_ratio;
181         dst->interlaced_frame    = src->video->interlaced;
182         dst->top_field_first     = src->video->top_field_first;
183         dst->key_frame           = src->video->key_frame;
184         dst->pict_type           = src->video->pict_type;
185         break;
186     case AVMEDIA_TYPE_AUDIO:
187         nb_channels = av_get_channel_layout_nb_channels(src->audio->channel_layout);
188         planes      = av_sample_fmt_is_planar(src->format) ? nb_channels : 1;
189
190         if (planes > FF_ARRAY_ELEMS(dst->data)) {
191             dst->extended_data = av_mallocz(planes * sizeof(*dst->extended_data));
192             if (!dst->extended_data)
193                 return AVERROR(ENOMEM);
194             memcpy(dst->extended_data, src->extended_data,
195                    planes * sizeof(dst->extended_data));
196         } else
197             dst->extended_data = dst->data;
198
199         dst->sample_rate         = src->audio->sample_rate;
200         dst->channel_layout      = src->audio->channel_layout;
201         dst->nb_samples          = src->audio->nb_samples;
202         break;
203     default:
204         return AVERROR(EINVAL);
205     }
206
207     return 0;
208 }
209
210 void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
211 {
212     // copy common properties
213     dst->pts             = src->pts;
214     dst->pos             = src->pos;
215
216     switch (src->type) {
217     case AVMEDIA_TYPE_VIDEO: *dst->video = *src->video; break;
218     case AVMEDIA_TYPE_AUDIO: *dst->audio = *src->audio; break;
219     default: break;
220     }
221 }