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/buffer.h"
24 #include "libavutil/frame.h"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/mem.h"
27 #include "libavutil/pixfmt.h"
29 struct FFVideoFramePool {
36 AVBufferPool *pools[4];
40 FFVideoFramePool *ff_video_frame_pool_init(AVBufferRef* (*alloc)(int size),
43 enum AVPixelFormat format,
47 FFVideoFramePool *pool;
48 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
53 pool = av_mallocz(sizeof(FFVideoFramePool));
58 pool->height = height;
59 pool->format = format;
62 if ((ret = av_image_check_size(width, height, 0, NULL)) < 0) {
66 if (!pool->linesize[0]) {
67 for(i = 1; i <= align; i += i) {
68 ret = av_image_fill_linesizes(pool->linesize, pool->format,
69 FFALIGN(pool->width, i));
73 if (!(pool->linesize[0] & (pool->align - 1)))
77 for (i = 0; i < 4 && pool->linesize[i]; i++) {
78 pool->linesize[i] = FFALIGN(pool->linesize[i], pool->align);
82 for (i = 0; i < 4 && pool->linesize[i]; i++) {
83 int h = FFALIGN(pool->height, 32);
85 h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
87 pool->pools[i] = av_buffer_pool_init(pool->linesize[i] * h + 16 + 16 - 1,
93 if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
94 desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
95 pool->pools[1] = av_buffer_pool_init(AVPALETTE_SIZE, alloc);
103 ff_video_frame_pool_uninit(&pool);
107 int ff_video_frame_pool_get_config(FFVideoFramePool *pool,
110 enum AVPixelFormat *format,
114 return AVERROR(EINVAL);
116 *width = pool->width;
117 *height = pool->height;
118 *format = pool->format;
119 *align = pool->align;
125 AVFrame *ff_video_frame_pool_get(FFVideoFramePool *pool)
129 const AVPixFmtDescriptor *desc;
131 frame = av_frame_alloc();
136 desc = av_pix_fmt_desc_get(pool->format);
141 frame->width = pool->width;
142 frame->height = pool->height;
143 frame->format = pool->format;
145 for (i = 0; i < 4; i++) {
146 frame->linesize[i] = pool->linesize[i];
150 frame->buf[i] = av_buffer_pool_get(pool->pools[i]);
151 if (!frame->buf[i]) {
155 frame->data[i] = frame->buf[i]->data;
158 if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
159 desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
160 enum AVPixelFormat format =
161 pool->format == AV_PIX_FMT_PAL8 ? AV_PIX_FMT_BGR8 : pool->format;
163 av_assert0(frame->data[1] != NULL);
164 if (avpriv_set_systematic_pal2((uint32_t *)frame->data[1], format) < 0) {
169 frame->extended_data = frame->data;
173 av_frame_free(&frame);
177 void ff_video_frame_pool_uninit(FFVideoFramePool **pool)
184 for (i = 0; i < 4; i++) {
185 av_buffer_pool_uninit(&(*pool)->pools[i]);