]> git.sesse.net Git - ffmpeg/blob - libavfilter/framepool.c
avutil/pixdesc: Remove deprecated AV_PIX_FMT_FLAG_PSEUDOPAL
[ffmpeg] / libavfilter / framepool.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * Copyright (c) 2015 Matthieu Bouron <matthieu.bouron stupeflix.com>
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "framepool.h"
22 #include "libavutil/avassert.h"
23 #include "libavutil/avutil.h"
24 #include "libavutil/buffer.h"
25 #include "libavutil/frame.h"
26 #include "libavutil/imgutils.h"
27 #include "libavutil/mem.h"
28 #include "libavutil/pixfmt.h"
29
30 struct FFFramePool {
31
32     enum AVMediaType type;
33
34     /* video */
35     int width;
36     int height;
37
38     /* audio */
39     int planes;
40     int channels;
41     int nb_samples;
42
43     /* common */
44     int format;
45     int align;
46     int linesize[4];
47     AVBufferPool *pools[4];
48
49 };
50
51 FFFramePool *ff_frame_pool_video_init(AVBufferRef* (*alloc)(buffer_size_t size),
52                                       int width,
53                                       int height,
54                                       enum AVPixelFormat format,
55                                       int align)
56 {
57     int i, ret;
58     FFFramePool *pool;
59     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
60
61     if (!desc)
62         return NULL;
63
64     pool = av_mallocz(sizeof(FFFramePool));
65     if (!pool)
66         return NULL;
67
68     pool->type = AVMEDIA_TYPE_VIDEO;
69     pool->width = width;
70     pool->height = height;
71     pool->format = format;
72     pool->align = align;
73
74     if ((ret = av_image_check_size2(width, height, INT64_MAX, format, 0, NULL)) < 0) {
75         goto fail;
76     }
77
78     if (!pool->linesize[0]) {
79         for(i = 1; i <= align; i += i) {
80             ret = av_image_fill_linesizes(pool->linesize, pool->format,
81                                           FFALIGN(pool->width, i));
82             if (ret < 0) {
83                 goto fail;
84             }
85             if (!(pool->linesize[0] & (pool->align - 1)))
86                 break;
87         }
88
89         for (i = 0; i < 4 && pool->linesize[i]; i++) {
90             pool->linesize[i] = FFALIGN(pool->linesize[i], pool->align);
91         }
92     }
93
94     for (i = 0; i < 4 && pool->linesize[i]; i++) {
95         int h = FFALIGN(pool->height, 32);
96         if (i == 1 || i == 2)
97             h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
98
99         pool->pools[i] = av_buffer_pool_init(pool->linesize[i] * h + 16 + 16 - 1,
100                                              alloc);
101         if (!pool->pools[i])
102             goto fail;
103     }
104
105     if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
106         pool->pools[1] = av_buffer_pool_init(AVPALETTE_SIZE, alloc);
107         if (!pool->pools[1])
108             goto fail;
109     }
110
111     return pool;
112
113 fail:
114     ff_frame_pool_uninit(&pool);
115     return NULL;
116 }
117
118 FFFramePool *ff_frame_pool_audio_init(AVBufferRef* (*alloc)(buffer_size_t size),
119                                       int channels,
120                                       int nb_samples,
121                                       enum AVSampleFormat format,
122                                       int align)
123 {
124     int ret, planar;
125     FFFramePool *pool;
126
127     pool = av_mallocz(sizeof(FFFramePool));
128     if (!pool)
129         return NULL;
130
131     planar = av_sample_fmt_is_planar(format);
132
133     pool->type = AVMEDIA_TYPE_AUDIO;
134     pool->planes = planar ? channels : 1;
135     pool->channels = channels;
136     pool->nb_samples = nb_samples;
137     pool->format = format;
138     pool->align = align;
139
140     ret = av_samples_get_buffer_size(&pool->linesize[0], channels,
141                                      nb_samples, format, 0);
142     if (ret < 0)
143         goto fail;
144
145     pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
146     if (!pool->pools[0])
147         goto fail;
148
149     return pool;
150
151 fail:
152     ff_frame_pool_uninit(&pool);
153     return NULL;
154 }
155
156 int ff_frame_pool_get_video_config(FFFramePool *pool,
157                                    int *width,
158                                    int *height,
159                                    enum AVPixelFormat *format,
160                                    int *align)
161 {
162     if (!pool)
163         return AVERROR(EINVAL);
164
165     av_assert0(pool->type == AVMEDIA_TYPE_VIDEO);
166
167     *width = pool->width;
168     *height = pool->height;
169     *format = pool->format;
170     *align = pool->align;
171
172     return 0;
173 }
174
175 int ff_frame_pool_get_audio_config(FFFramePool *pool,
176                                    int *channels,
177                                    int *nb_samples,
178                                    enum AVSampleFormat *format,
179                                    int *align)
180 {
181     if (!pool)
182         return AVERROR(EINVAL);
183
184     av_assert0(pool->type == AVMEDIA_TYPE_AUDIO);
185
186     *channels = pool->channels;
187     *nb_samples = pool->nb_samples;
188     *format = pool->format;
189     *align = pool->align;
190
191     return 0;
192 }
193
194 AVFrame *ff_frame_pool_get(FFFramePool *pool)
195 {
196     int i;
197     AVFrame *frame;
198     const AVPixFmtDescriptor *desc;
199
200     frame = av_frame_alloc();
201     if (!frame) {
202         return NULL;
203     }
204
205     switch(pool->type) {
206     case AVMEDIA_TYPE_VIDEO:
207         desc = av_pix_fmt_desc_get(pool->format);
208         if (!desc) {
209             goto fail;
210         }
211
212         frame->width = pool->width;
213         frame->height = pool->height;
214         frame->format = pool->format;
215
216         for (i = 0; i < 4; i++) {
217             frame->linesize[i] = pool->linesize[i];
218             if (!pool->pools[i])
219                 break;
220
221             frame->buf[i] = av_buffer_pool_get(pool->pools[i]);
222             if (!frame->buf[i])
223                 goto fail;
224
225             frame->data[i] = frame->buf[i]->data;
226         }
227
228         if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
229             enum AVPixelFormat format =
230                 pool->format == AV_PIX_FMT_PAL8 ? AV_PIX_FMT_BGR8 : pool->format;
231
232             av_assert0(frame->data[1] != NULL);
233             if (avpriv_set_systematic_pal2((uint32_t *)frame->data[1], format) < 0)
234                 goto fail;
235         }
236
237         frame->extended_data = frame->data;
238         break;
239     case AVMEDIA_TYPE_AUDIO:
240         frame->nb_samples = pool->nb_samples;
241         frame->channels = pool->channels;
242         frame->format = pool->format;
243         frame->linesize[0] = pool->linesize[0];
244
245         if (pool->planes > AV_NUM_DATA_POINTERS) {
246             frame->extended_data = av_mallocz_array(pool->planes,
247                                                     sizeof(*frame->extended_data));
248             frame->nb_extended_buf = pool->planes - AV_NUM_DATA_POINTERS;
249             frame->extended_buf = av_mallocz_array(frame->nb_extended_buf,
250                                                    sizeof(*frame->extended_buf));
251             if (!frame->extended_data || !frame->extended_buf)
252                 goto fail;
253         } else {
254             frame->extended_data = frame->data;
255             av_assert0(frame->nb_extended_buf == 0);
256         }
257
258         for (i = 0; i < FFMIN(pool->planes, AV_NUM_DATA_POINTERS); i++) {
259             frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
260             if (!frame->buf[i])
261                 goto fail;
262             frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
263         }
264         for (i = 0; i < frame->nb_extended_buf; i++) {
265             frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
266             if (!frame->extended_buf[i])
267                 goto fail;
268             frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
269         }
270
271         break;
272     default:
273         av_assert0(0);
274     }
275
276     return frame;
277 fail:
278     av_frame_free(&frame);
279     return NULL;
280 }
281
282 void ff_frame_pool_uninit(FFFramePool **pool)
283 {
284     int i;
285
286     if (!pool || !*pool)
287         return;
288
289     for (i = 0; i < 4; i++) {
290         av_buffer_pool_uninit(&(*pool)->pools[i]);
291     }
292
293     av_freep(pool);
294 }