2 * The default get_buffer2() implementation
4 * This file is part of FFmpeg.
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
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"
36 typedef struct FramePool
{
38 * Pools for each data plane. For audio all the planes have the same size,
39 * so only pools[0] is used.
41 AVBufferPool
*pools
[4];
48 int stride_align
[AV_NUM_DATA_POINTERS
];
55 static void frame_pool_free(void *opaque
, uint8_t *data
)
57 FramePool
*pool
= (FramePool
*)data
;
60 for (i
= 0; i
< FF_ARRAY_ELEMS(pool
->pools
); i
++)
61 av_buffer_pool_uninit(&pool
->pools
[i
]);
66 static AVBufferRef
*frame_pool_alloc(void)
68 FramePool
*pool
= av_mallocz(sizeof(*pool
));
74 buf
= av_buffer_create((uint8_t*)pool
, sizeof(*pool
),
75 frame_pool_free
, NULL
, 0);
84 static int update_frame_pool(AVCodecContext
*avctx
, AVFrame
*frame
)
86 FramePool
*pool
= avctx
->internal
->pool
?
87 (FramePool
*)avctx
->internal
->pool
->data
: NULL
;
88 AVBufferRef
*pool_buf
;
89 int i
, ret
, ch
, planes
;
91 if (avctx
->codec_type
== AVMEDIA_TYPE_AUDIO
) {
92 int planar
= av_sample_fmt_is_planar(frame
->format
);
93 ch
= frame
->ch_layout
.nb_channels
;
94 #if FF_API_OLD_CHANNEL_LAYOUT
95 FF_DISABLE_DEPRECATION_WARNINGS
98 FF_ENABLE_DEPRECATION_WARNINGS
100 planes
= planar
? ch
: 1;
103 if (pool
&& pool
->format
== frame
->format
) {
104 if (avctx
->codec_type
== AVMEDIA_TYPE_VIDEO
&&
105 pool
->width
== frame
->width
&& pool
->height
== frame
->height
)
107 if (avctx
->codec_type
== AVMEDIA_TYPE_AUDIO
&& pool
->planes
== planes
&&
108 pool
->channels
== ch
&& frame
->nb_samples
== pool
->samples
)
112 pool_buf
= frame_pool_alloc();
114 return AVERROR(ENOMEM
);
115 pool
= (FramePool
*)pool_buf
->data
;
117 switch (avctx
->codec_type
) {
118 case AVMEDIA_TYPE_VIDEO
: {
120 int w
= frame
->width
;
121 int h
= frame
->height
;
123 ptrdiff_t linesize1
[4];
126 avcodec_align_dimensions2(avctx
, &w
, &h
, pool
->stride_align
);
129 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
130 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
131 ret
= av_image_fill_linesizes(linesize
, avctx
->pix_fmt
, w
);
134 // increase alignment of w for next try (rhs gives the lowest bit set in w)
138 for (i
= 0; i
< 4; i
++)
139 unaligned
|= linesize
[i
] % pool
->stride_align
[i
];
142 for (i
= 0; i
< 4; i
++)
143 linesize1
[i
] = linesize
[i
];
144 ret
= av_image_fill_plane_sizes(size
, avctx
->pix_fmt
, h
, linesize1
);
148 for (i
= 0; i
< 4; i
++) {
149 pool
->linesize
[i
] = linesize
[i
];
151 if (size
[i
] > INT_MAX
- (16 + STRIDE_ALIGN
- 1)) {
152 ret
= AVERROR(EINVAL
);
155 pool
->pools
[i
] = av_buffer_pool_init(size
[i
] + 16 + STRIDE_ALIGN
- 1,
156 CONFIG_MEMORY_POISONING
?
159 if (!pool
->pools
[i
]) {
160 ret
= AVERROR(ENOMEM
);
165 pool
->format
= frame
->format
;
166 pool
->width
= frame
->width
;
167 pool
->height
= frame
->height
;
171 case AVMEDIA_TYPE_AUDIO
: {
172 ret
= av_samples_get_buffer_size(&pool
->linesize
[0], ch
,
173 frame
->nb_samples
, frame
->format
, 0);
177 pool
->pools
[0] = av_buffer_pool_init(pool
->linesize
[0],
178 CONFIG_MEMORY_POISONING
?
181 if (!pool
->pools
[0]) {
182 ret
= AVERROR(ENOMEM
);
186 pool
->format
= frame
->format
;
187 pool
->planes
= planes
;
189 pool
->samples
= frame
->nb_samples
;
192 default: av_assert0(0);
195 av_buffer_unref(&avctx
->internal
->pool
);
196 avctx
->internal
->pool
= pool_buf
;
200 av_buffer_unref(&pool_buf
);
204 static int audio_get_buffer(AVCodecContext
*avctx
, AVFrame
*frame
)
206 FramePool
*pool
= (FramePool
*)avctx
->internal
->pool
->data
;
207 int planes
= pool
->planes
;
210 frame
->linesize
[0] = pool
->linesize
[0];
212 if (planes
> AV_NUM_DATA_POINTERS
) {
213 frame
->extended_data
= av_calloc(planes
, sizeof(*frame
->extended_data
));
214 frame
->nb_extended_buf
= planes
- AV_NUM_DATA_POINTERS
;
215 frame
->extended_buf
= av_calloc(frame
->nb_extended_buf
,
216 sizeof(*frame
->extended_buf
));
217 if (!frame
->extended_data
|| !frame
->extended_buf
) {
218 av_freep(&frame
->extended_data
);
219 av_freep(&frame
->extended_buf
);
220 return AVERROR(ENOMEM
);
223 frame
->extended_data
= frame
->data
;
224 av_assert0(frame
->nb_extended_buf
== 0);
227 for (i
= 0; i
< FFMIN(planes
, AV_NUM_DATA_POINTERS
); i
++) {
228 frame
->buf
[i
] = av_buffer_pool_get(pool
->pools
[0]);
231 frame
->extended_data
[i
] = frame
->data
[i
] = frame
->buf
[i
]->data
;
233 for (i
= 0; i
< frame
->nb_extended_buf
; i
++) {
234 frame
->extended_buf
[i
] = av_buffer_pool_get(pool
->pools
[0]);
235 if (!frame
->extended_buf
[i
])
237 frame
->extended_data
[i
+ AV_NUM_DATA_POINTERS
] = frame
->extended_buf
[i
]->data
;
240 if (avctx
->debug
& FF_DEBUG_BUFFERS
)
241 av_log(avctx
, AV_LOG_DEBUG
, "default_get_buffer called on frame %p", frame
);
245 av_frame_unref(frame
);
246 return AVERROR(ENOMEM
);
249 static int video_get_buffer(AVCodecContext
*s
, AVFrame
*pic
)
251 FramePool
*pool
= (FramePool
*)s
->internal
->pool
->data
;
252 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(pic
->format
);
255 if (pic
->data
[0] || pic
->data
[1] || pic
->data
[2] || pic
->data
[3]) {
256 av_log(s
, AV_LOG_ERROR
, "pic->data[*]!=NULL in avcodec_default_get_buffer\n");
261 av_log(s
, AV_LOG_ERROR
,
262 "Unable to get pixel format descriptor for format %s\n",
263 av_get_pix_fmt_name(pic
->format
));
264 return AVERROR(EINVAL
);
267 memset(pic
->data
, 0, sizeof(pic
->data
));
268 pic
->extended_data
= pic
->data
;
270 for (i
= 0; i
< 4 && pool
->pools
[i
]; i
++) {
271 pic
->linesize
[i
] = pool
->linesize
[i
];
273 pic
->buf
[i
] = av_buffer_pool_get(pool
->pools
[i
]);
277 pic
->data
[i
] = pic
->buf
[i
]->data
;
279 for (; i
< AV_NUM_DATA_POINTERS
; i
++) {
281 pic
->linesize
[i
] = 0;
284 if (s
->debug
& FF_DEBUG_BUFFERS
)
285 av_log(s
, AV_LOG_DEBUG
, "default_get_buffer called on pic %p\n", pic
);
290 return AVERROR(ENOMEM
);
293 int avcodec_default_get_buffer2(AVCodecContext
*avctx
, AVFrame
*frame
, int flags
)
297 if (avctx
->hw_frames_ctx
) {
298 ret
= av_hwframe_get_buffer(avctx
->hw_frames_ctx
, frame
, 0);
299 frame
->width
= avctx
->coded_width
;
300 frame
->height
= avctx
->coded_height
;
304 if ((ret
= update_frame_pool(avctx
, frame
)) < 0)
307 switch (avctx
->codec_type
) {
308 case AVMEDIA_TYPE_VIDEO
:
309 return video_get_buffer(avctx
, frame
);
310 case AVMEDIA_TYPE_AUDIO
:
311 return audio_get_buffer(avctx
, frame
);