2 * This file is part of FFmpeg.
4 * Copyright (c) 2015 Matthieu Bouron <matthieu.bouron stupeflix.com>
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.
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.
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
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"
32 enum AVMediaType type;
47 AVBufferPool *pools[4];
51 FFFramePool *ff_frame_pool_video_init(AVBufferRef* (*alloc)(size_t size),
54 enum AVPixelFormat format,
59 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
64 pool = av_mallocz(sizeof(FFFramePool));
68 pool->type = AVMEDIA_TYPE_VIDEO;
70 pool->height = height;
71 pool->format = format;
74 if ((ret = av_image_check_size2(width, height, INT64_MAX, format, 0, NULL)) < 0) {
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));
85 if (!(pool->linesize[0] & (pool->align - 1)))
89 for (i = 0; i < 4 && pool->linesize[i]; i++) {
90 pool->linesize[i] = FFALIGN(pool->linesize[i], pool->align);
94 for (i = 0; i < 4 && pool->linesize[i]; i++) {
95 int h = FFALIGN(pool->height, 32);
97 h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
99 pool->pools[i] = av_buffer_pool_init(pool->linesize[i] * h + 16 + 16 - 1,
105 if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
106 pool->pools[1] = av_buffer_pool_init(AVPALETTE_SIZE, alloc);
114 ff_frame_pool_uninit(&pool);
118 FFFramePool *ff_frame_pool_audio_init(AVBufferRef* (*alloc)(size_t size),
121 enum AVSampleFormat format,
127 pool = av_mallocz(sizeof(FFFramePool));
131 planar = av_sample_fmt_is_planar(format);
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;
140 ret = av_samples_get_buffer_size(&pool->linesize[0], channels,
141 nb_samples, format, 0);
145 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
152 ff_frame_pool_uninit(&pool);
156 int ff_frame_pool_get_video_config(FFFramePool *pool,
159 enum AVPixelFormat *format,
163 return AVERROR(EINVAL);
165 av_assert0(pool->type == AVMEDIA_TYPE_VIDEO);
167 *width = pool->width;
168 *height = pool->height;
169 *format = pool->format;
170 *align = pool->align;
175 int ff_frame_pool_get_audio_config(FFFramePool *pool,
178 enum AVSampleFormat *format,
182 return AVERROR(EINVAL);
184 av_assert0(pool->type == AVMEDIA_TYPE_AUDIO);
186 *channels = pool->channels;
187 *nb_samples = pool->nb_samples;
188 *format = pool->format;
189 *align = pool->align;
194 AVFrame *ff_frame_pool_get(FFFramePool *pool)
198 const AVPixFmtDescriptor *desc;
200 frame = av_frame_alloc();
206 case AVMEDIA_TYPE_VIDEO:
207 desc = av_pix_fmt_desc_get(pool->format);
212 frame->width = pool->width;
213 frame->height = pool->height;
214 frame->format = pool->format;
216 for (i = 0; i < 4; i++) {
217 frame->linesize[i] = pool->linesize[i];
221 frame->buf[i] = av_buffer_pool_get(pool->pools[i]);
225 frame->data[i] = frame->buf[i]->data;
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;
232 av_assert0(frame->data[1] != NULL);
233 if (avpriv_set_systematic_pal2((uint32_t *)frame->data[1], format) < 0)
237 frame->extended_data = frame->data;
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];
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)
254 frame->extended_data = frame->data;
255 av_assert0(frame->nb_extended_buf == 0);
258 for (i = 0; i < FFMIN(pool->planes, AV_NUM_DATA_POINTERS); i++) {
259 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
262 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
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])
268 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
278 av_frame_free(&frame);
282 void ff_frame_pool_uninit(FFFramePool **pool)
289 for (i = 0; i < 4; i++) {
290 av_buffer_pool_uninit(&(*pool)->pools[i]);