]> git.sesse.net Git - ffmpeg/blob - libavfilter/buffer.c
Merge commit 'a5e8c41c28f907d98d2a739db08f7aef4cbfcf3a'
[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 void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
167 {
168     // copy common properties
169     dst->pts             = src->pts;
170     dst->pos             = src->pos;
171
172     switch (src->type) {
173     case AVMEDIA_TYPE_VIDEO: *dst->video = *src->video; break;
174     case AVMEDIA_TYPE_AUDIO: *dst->audio = *src->audio; break;
175     default: break;
176     }
177 }