FFmpeg
get_buffer.c
Go to the documentation of this file.
1 /*
2  * The default get_buffer2() implementation
3  *
4  * This file is part of FFmpeg.
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 <stdint.h>
22 
23 #include "libavutil/avassert.h"
24 #include "libavutil/avutil.h"
25 #include "libavutil/buffer.h"
26 #include "libavutil/frame.h"
27 #include "libavutil/hwcontext.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/samplefmt.h"
31 #include "libavutil/version.h"
32 
33 #include "avcodec.h"
34 #include "internal.h"
35 #include "refstruct.h"
36 
37 typedef struct FramePool {
38  /**
39  * Pools for each data plane. For audio all the planes have the same size,
40  * so only pools[0] is used.
41  */
43 
44  /*
45  * Pool parameters
46  */
47  int format;
48  int width, height;
50  int linesize[4];
51  int planes;
52  int channels;
53  int samples;
54 } FramePool;
55 
56 static void frame_pool_free(FFRefStructOpaque unused, void *obj)
57 {
58  FramePool *pool = obj;
59  int i;
60 
61  for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
62  av_buffer_pool_uninit(&pool->pools[i]);
63 }
64 
66 {
67  FramePool *pool = avctx->internal->pool;
68  int i, ret;
69 
70  if (pool && pool->format == frame->format) {
71  if (avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
72  pool->width == frame->width && pool->height == frame->height)
73  return 0;
74  if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
75  pool->channels == frame->ch_layout.nb_channels &&
76  frame->nb_samples == pool->samples)
77  return 0;
78  }
79 
80  pool = ff_refstruct_alloc_ext(sizeof(*pool), 0, NULL, frame_pool_free);
81  if (!pool)
82  return AVERROR(ENOMEM);
83 
84  switch (avctx->codec_type) {
85  case AVMEDIA_TYPE_VIDEO: {
86  int linesize[4];
87  int w = frame->width;
88  int h = frame->height;
89  int unaligned;
90  ptrdiff_t linesize1[4];
91  size_t size[4];
92 
93  avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
94 
95  do {
96  // NOTE: do not align linesizes individually, this breaks e.g. assumptions
97  // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
98  ret = av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
99  if (ret < 0)
100  goto fail;
101  // increase alignment of w for next try (rhs gives the lowest bit set in w)
102  w += w & ~(w - 1);
103 
104  unaligned = 0;
105  for (i = 0; i < 4; i++)
106  unaligned |= linesize[i] % pool->stride_align[i];
107  } while (unaligned);
108 
109  for (i = 0; i < 4; i++)
110  linesize1[i] = linesize[i];
111  ret = av_image_fill_plane_sizes(size, avctx->pix_fmt, h, linesize1);
112  if (ret < 0)
113  goto fail;
114 
115  for (i = 0; i < 4; i++) {
116  pool->linesize[i] = linesize[i];
117  if (size[i]) {
118  if (size[i] > INT_MAX - (16 + STRIDE_ALIGN - 1)) {
119  ret = AVERROR(EINVAL);
120  goto fail;
121  }
122  pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
123  CONFIG_MEMORY_POISONING ?
124  NULL :
126  if (!pool->pools[i]) {
127  ret = AVERROR(ENOMEM);
128  goto fail;
129  }
130  }
131  }
132  pool->format = frame->format;
133  pool->width = frame->width;
134  pool->height = frame->height;
135 
136  break;
137  }
138  case AVMEDIA_TYPE_AUDIO: {
140  frame->ch_layout.nb_channels,
141  frame->nb_samples, frame->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  ret = AVERROR(ENOMEM);
148  goto fail;
149  }
150 
151  pool->format = frame->format;
152  pool->channels = frame->ch_layout.nb_channels;
153  pool->samples = frame->nb_samples;
154  pool->planes = av_sample_fmt_is_planar(pool->format) ? pool->channels : 1;
155  break;
156  }
157  default: av_assert0(0);
158  }
159 
160  ff_refstruct_unref(&avctx->internal->pool);
161  avctx->internal->pool = pool;
162 
163  return 0;
164 fail:
165  ff_refstruct_unref(&pool);
166  return ret;
167 }
168 
170 {
171  FramePool *pool = avctx->internal->pool;
172  int planes = pool->planes;
173  int i;
174 
175  frame->linesize[0] = pool->linesize[0];
176 
178  frame->extended_data = av_calloc(planes, sizeof(*frame->extended_data));
179  frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
180  frame->extended_buf = av_calloc(frame->nb_extended_buf,
181  sizeof(*frame->extended_buf));
182  if (!frame->extended_data || !frame->extended_buf) {
183  av_freep(&frame->extended_data);
184  av_freep(&frame->extended_buf);
185  return AVERROR(ENOMEM);
186  }
187  } else {
188  frame->extended_data = frame->data;
189  av_assert0(frame->nb_extended_buf == 0);
190  }
191 
192  for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
193  frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
194  if (!frame->buf[i])
195  goto fail;
196  frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
197  }
198  for (i = 0; i < frame->nb_extended_buf; i++) {
199  frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
200  if (!frame->extended_buf[i])
201  goto fail;
202  frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
203  }
204 
205  if (avctx->debug & FF_DEBUG_BUFFERS)
206  av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
207 
208  return 0;
209 fail:
211  return AVERROR(ENOMEM);
212 }
213 
215 {
216  FramePool *pool = s->internal->pool;
217  int i;
218 
219  if (pic->data[0] || pic->data[1] || pic->data[2] || pic->data[3]) {
220  av_log(s, AV_LOG_ERROR, "pic->data[*]!=NULL in avcodec_default_get_buffer\n");
221  return -1;
222  }
223 
224  memset(pic->data, 0, sizeof(pic->data));
225  pic->extended_data = pic->data;
226 
227  for (i = 0; i < 4 && pool->pools[i]; i++) {
228  pic->linesize[i] = pool->linesize[i];
229 
230  pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
231  if (!pic->buf[i])
232  goto fail;
233 
234  pic->data[i] = pic->buf[i]->data;
235  }
236  for (; i < AV_NUM_DATA_POINTERS; i++) {
237  pic->data[i] = NULL;
238  pic->linesize[i] = 0;
239  }
240 
241  if (s->debug & FF_DEBUG_BUFFERS)
242  av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
243 
244  return 0;
245 fail:
246  av_frame_unref(pic);
247  return AVERROR(ENOMEM);
248 }
249 
251 {
252  int ret;
253 
254  if (avctx->hw_frames_ctx) {
256  if (ret == AVERROR(ENOMEM)) {
257  AVHWFramesContext *frames_ctx =
259  if (frames_ctx->initial_pool_size > 0 &&
261  av_log(avctx, AV_LOG_WARNING, "Failed to allocate a %s/%s "
262  "frame from a fixed pool of hardware frames.\n",
263  av_get_pix_fmt_name(frames_ctx->format),
264  av_get_pix_fmt_name(frames_ctx->sw_format));
265  av_log(avctx, AV_LOG_WARNING, "Consider setting "
266  "extra_hw_frames to a larger value "
267  "(currently set to %d, giving a pool size of %d).\n",
268  avctx->extra_hw_frames, frames_ctx->initial_pool_size);
270  }
271  }
272  frame->width = avctx->coded_width;
273  frame->height = avctx->coded_height;
274  return ret;
275  }
276 
277  if ((ret = update_frame_pool(avctx, frame)) < 0)
278  return ret;
279 
280  switch (avctx->codec_type) {
281  case AVMEDIA_TYPE_VIDEO:
282  return video_get_buffer(avctx, frame);
283  case AVMEDIA_TYPE_AUDIO:
284  return audio_get_buffer(avctx, frame);
285  default:
286  return -1;
287  }
288 }
av_buffer_pool_init
AVBufferPool * av_buffer_pool_init(size_t size, AVBufferRef *(*alloc)(size_t size))
Allocate and initialize a buffer pool.
Definition: buffer.c:283
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
frame_pool_free
static void frame_pool_free(FFRefStructOpaque unused, void *obj)
Definition: get_buffer.c:56
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVBufferPool
The buffer pool.
Definition: buffer_internal.h:88
FramePool::planes
int planes
Definition: get_buffer.c:51
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
AVHWFramesContext::format
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:197
ff_refstruct_alloc_ext
static void * ff_refstruct_alloc_ext(size_t size, unsigned flags, void *opaque, void(*free_cb)(FFRefStructOpaque opaque, void *obj))
A wrapper around ff_refstruct_alloc_ext_c() for the common case of a non-const qualified opaque.
Definition: refstruct.h:94
video_get_buffer
static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
Definition: get_buffer.c:214
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
FramePool::pools
AVBufferPool * pools[4]
Pools for each data plane.
Definition: get_buffer.c:42
w
uint8_t w
Definition: llviddspenc.c:38
internal.h
planes
static const struct @467 planes[]
FramePool::channels
int channels
Definition: get_buffer.c:52
FramePool::height
int height
Definition: get_buffer.c:48
FFRefStructOpaque
RefStruct is an API for creating reference-counted objects with minimal overhead.
Definition: refstruct.h:58
FramePool::stride_align
int stride_align[AV_NUM_DATA_POINTERS]
Definition: get_buffer.c:49
AVFrame::buf
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:602
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
STRIDE_ALIGN
#define STRIDE_ALIGN
Definition: internal.h:46
fail
#define fail()
Definition: checkasm.h:200
samplefmt.h
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:639
refstruct.h
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_buffer_pool_get
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
Definition: buffer.c:390
av_image_fill_linesizes
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:89
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVCodecInternal::pool
struct FramePool * pool
Definition: internal.h:69
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
av_sample_fmt_is_planar
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:114
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:230
avcodec_align_dimensions2
void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[AV_NUM_DATA_POINTERS])
Modify width and height values so that they will result in a memory buffer that is acceptable for the...
Definition: utils.c:144
FramePool::format
int format
Definition: get_buffer.c:47
if
if(ret)
Definition: filter_design.txt:179
FramePool::width
int width
Definition: get_buffer.c:48
NULL
#define NULL
Definition: coverity.c:32
AVHWFramesContext::sw_format
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:210
av_image_fill_plane_sizes
int av_image_fill_plane_sizes(size_t sizes[4], enum AVPixelFormat pix_fmt, int height, const ptrdiff_t linesizes[4])
Fill plane sizes for an image with pixel format pix_fmt and height height.
Definition: imgutils.c:111
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:486
av_buffer_pool_uninit
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
Definition: buffer.c:328
FramePool::linesize
int linesize[4]
Definition: get_buffer.c:50
FF_DEBUG_BUFFERS
#define FF_DEBUG_BUFFERS
Definition: avcodec.h:1418
audio_get_buffer
static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Definition: get_buffer.c:169
AVCodecContext::extra_hw_frames
int extra_hw_frames
Video decoding only.
Definition: avcodec.h:1530
size
int size
Definition: twinvq_data.h:10344
AV_NUM_DATA_POINTERS
#define AV_NUM_DATA_POINTERS
Definition: frame.h:390
frame.h
buffer.h
avcodec_default_get_buffer2
int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
The default callback for AVCodecContext.get_buffer2().
Definition: get_buffer.c:250
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:450
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:622
av_samples_get_buffer_size
int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Get the required buffer size for the given audio parameters.
Definition: samplefmt.c:121
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:663
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
AVCodecContext::hw_frames_ctx
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
Definition: avcodec.h:1485
avcodec.h
version.h
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:115
av_buffer_allocz
AVBufferRef * av_buffer_allocz(size_t size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
Definition: buffer.c:93
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
FramePool
Definition: get_buffer.c:37
AVCodecContext
main external API structure.
Definition: avcodec.h:451
update_frame_pool
static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
Definition: get_buffer.c:65
AVCodecContext::debug
int debug
debug
Definition: avcodec.h:1406
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:639
AVHWFramesContext::initial_pool_size
int initial_pool_size
Initial size of the frame pool.
Definition: hwcontext.h:187
AVCodecContext::codec_type
enum AVMediaType codec_type
Definition: avcodec.h:459
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
avutil.h
mem.h
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
imgutils.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
hwcontext.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:434
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
h
h
Definition: vp9dsp_template.c:2070
AVCodecInternal::warned_on_failed_allocation_from_fixed_pool
int warned_on_failed_allocation_from_fixed_pool
Set when the user has been warned about a failed allocation from a fixed frame pool.
Definition: internal.h:165
av_hwframe_get_buffer
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
Definition: hwcontext.c:491
FramePool::samples
int samples
Definition: get_buffer.c:53
ff_refstruct_unref
void ff_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition: refstruct.c:120
av_get_pix_fmt_name
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:3090