FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
framepool.c
Go to the documentation of this file.
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 
30 
31  int width;
32  int height;
33  int format;
34  int align;
35  int linesize[4];
37 
38 };
39 
41  int width,
42  int height,
43  enum AVPixelFormat format,
44  int align)
45 {
46  int i, ret;
47  FFVideoFramePool *pool;
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 ||
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:
104  return NULL;
105 }
106 
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 
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 ||
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 
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 }
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:132
#define NULL
Definition: coverity.c:32
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2222
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
misc image utilities
void ff_video_frame_pool_uninit(FFVideoFramePool **pool)
Deallocate the video frame pool.
Definition: framepool.c:177
memory handling functions
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:363
const char * desc
Definition: nvenc.c:89
FFVideoFramePool * ff_video_frame_pool_init(AVBufferRef *(*alloc)(int size), int width, int height, enum AVPixelFormat format, int align)
Allocate and initialize a video frame pool.
Definition: framepool.c:40
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:140
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:73
int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
Definition: imgutils.c:151
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
Video frame pool.
Definition: framepool.c:29
int ff_video_frame_pool_get_config(FFVideoFramePool *pool, int *width, int *height, enum AVPixelFormat *format, int *align)
Get the video frame pool configuration.
Definition: framepool.c:107
static AVFrame * frame
#define height
ptrdiff_t size
Definition: opengl_enc.c:101
#define FFALIGN(x, a)
Definition: macros.h:48
The buffer pool.
int width
width and height of the video frame
Definition: frame.h:236
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
AVFrame * ff_video_frame_pool_get(FFVideoFramePool *pool)
Allocate a new AVFrame, reussing old buffers from the pool when available.
Definition: framepool.c:125
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:153
simple assert() macros that are a bit more flexible than ISO C assert().
#define fail()
Definition: checkasm.h:81
reference-counted frame API
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:106
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:251
#define width
int linesize[4]
Definition: framepool.c:35
#define AV_PIX_FMT_FLAG_PSEUDOPAL
The pixel format is "pseudo-paletted".
Definition: pixdesc.h:158
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:248
const AVS_VideoInfo int align
Definition: avisynth_c.h:658
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
Definition: pixfmt.h:84
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
uint8_t * data
The data buffer.
Definition: buffer.h:89
static const char * format
Definition: movenc.c:47
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
Fill plane linesizes for an image with pixel format pix_fmt and width width.
Definition: imgutils.c:88
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
Definition: buffer.c:274
refcounted data buffer API
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
A reference to a data buffer.
Definition: buffer.h:81
AVBufferPool * av_buffer_pool_init(int size, AVBufferRef *(*alloc)(int size))
Allocate and initialize a buffer pool.
Definition: buffer.c:237
pixel format definitions
AVBufferPool * pools[4]
Definition: framepool.c:36
int height
Definition: frame.h:236
#define av_freep(p)
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
Definition: buffer.c:380
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:231
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58