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