]> git.sesse.net Git - ffmpeg/blob - libavfilter/framepool.c
lavfi/extractplanes: Fix in_pixfmts.
[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/buffer.h"
24 #include "libavutil/frame.h"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/mem.h"
27 #include "libavutil/pixfmt.h"
28
29 struct FFVideoFramePool {
30
31     int width;
32     int height;
33     int format;
34     int align;
35     int linesize[4];
36     AVBufferPool *pools[4];
37
38 };
39
40 FFVideoFramePool *ff_video_frame_pool_init(AVBufferRef* (*alloc)(int size),
41                                            int width,
42                                            int height,
43                                            enum AVPixelFormat format,
44                                            int align)
45 {
46     int i, ret;
47     FFVideoFramePool *pool;
48     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
49
50     if (!desc)
51         return NULL;
52
53     pool = av_mallocz(sizeof(FFVideoFramePool));
54     if (!pool)
55         return NULL;
56
57     pool->width = width;
58     pool->height = height;
59     pool->format = format;
60     pool->align = align;
61
62     if ((ret = av_image_check_size(width, height, 0, NULL)) < 0) {
63         goto fail;
64     }
65
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));
70             if (ret < 0) {
71                 goto fail;
72             }
73             if (!(pool->linesize[0] & (pool->align - 1)))
74                 break;
75         }
76
77         for (i = 0; i < 4 && pool->linesize[i]; i++) {
78             pool->linesize[i] = FFALIGN(pool->linesize[i], pool->align);
79         }
80     }
81
82     for (i = 0; i < 4 && pool->linesize[i]; i++) {
83         int h = FFALIGN(pool->height, 32);
84         if (i == 1 || i == 2)
85             h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
86
87         pool->pools[i] = av_buffer_pool_init(pool->linesize[i] * h + 16 + 16 - 1,
88                                              alloc);
89         if (!pool->pools[i])
90             goto fail;
91     }
92
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);
96         if (!pool->pools[1])
97             goto fail;
98     }
99
100     return pool;
101
102 fail:
103     ff_video_frame_pool_uninit(&pool);
104     return NULL;
105 }
106
107 int ff_video_frame_pool_get_config(FFVideoFramePool *pool,
108                                    int *width,
109                                    int *height,
110                                    enum AVPixelFormat *format,
111                                    int *align)
112 {
113     if (!pool)
114         return AVERROR(EINVAL);
115
116     *width = pool->width;
117     *height = pool->height;
118     *format = pool->format;
119     *align = pool->align;
120
121     return 0;
122 }
123
124
125 AVFrame *ff_video_frame_pool_get(FFVideoFramePool *pool)
126 {
127     int i;
128     AVFrame *frame;
129     const AVPixFmtDescriptor *desc;
130
131     frame = av_frame_alloc();
132     if (!frame) {
133         return NULL;
134     }
135
136     desc = av_pix_fmt_desc_get(pool->format);
137     if (!desc) {
138         goto fail;
139     }
140
141     frame->width = pool->width;
142     frame->height = pool->height;
143     frame->format = pool->format;
144
145     for (i = 0; i < 4; i++) {
146         frame->linesize[i] = pool->linesize[i];
147         if (!pool->pools[i])
148             break;
149
150         frame->buf[i] = av_buffer_pool_get(pool->pools[i]);
151         if (!frame->buf[i]) {
152             goto fail;
153         }
154
155         frame->data[i] = frame->buf[i]->data;
156     }
157
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;
162
163         av_assert0(frame->data[1] != NULL);
164         if (avpriv_set_systematic_pal2((uint32_t *)frame->data[1], format) < 0) {
165             goto fail;
166         }
167     }
168
169     frame->extended_data = frame->data;
170
171     return frame;
172 fail:
173     av_frame_free(&frame);
174     return NULL;
175 }
176
177 void ff_video_frame_pool_uninit(FFVideoFramePool **pool)
178 {
179     int i;
180
181     if (!pool || !*pool)
182         return;
183
184     for (i = 0; i < 4; i++) {
185         av_buffer_pool_uninit(&(*pool)->pools[i]);
186     }
187
188     av_freep(pool);
189 }