2 * Mpeg video formats-related picture management functions
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/common.h"
25 #include "libavutil/pixdesc.h"
26 #include "libavutil/imgutils.h"
29 #include "motion_est.h"
30 #include "mpegpicture.h"
31 #include "mpegutils.h"
33 static int make_tables_writable(Picture *pic)
36 #define MAKE_WRITABLE(table) \
39 (ret = av_buffer_make_writable(&pic->table)) < 0)\
43 MAKE_WRITABLE(mb_var_buf);
44 MAKE_WRITABLE(mc_mb_var_buf);
45 MAKE_WRITABLE(mb_mean_buf);
46 MAKE_WRITABLE(mbskip_table_buf);
47 MAKE_WRITABLE(qscale_table_buf);
48 MAKE_WRITABLE(mb_type_buf);
50 for (i = 0; i < 2; i++) {
51 MAKE_WRITABLE(motion_val_buf[i]);
52 MAKE_WRITABLE(ref_index_buf[i]);
58 int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me,
59 ScratchpadContext *sc, int linesize)
61 # define EMU_EDGE_HEIGHT (4 * 70)
62 int alloc_size = FFALIGN(FFABS(linesize) + 64, 32);
68 av_log(avctx, AV_LOG_ERROR, "Image too small, temporary buffers cannot function\n");
69 return AVERROR_PATCHWELCOME;
72 if (av_image_check_size2(alloc_size, EMU_EDGE_HEIGHT, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)
73 return AVERROR(ENOMEM);
75 // edge emu needs blocksize + filter length - 1
76 // (= 17x17 for halfpel / 21x21 for H.264)
77 // VC-1 computes luma and chroma simultaneously and needs 19X19 + 9x9
78 // at uvlinesize. It supports only YUV420 so 24x24 is enough
79 // linesize * interlaced * MBsize
80 // we also use this buffer for encoding in encode_mb_internal() needig an additional 32 lines
81 FF_ALLOCZ_ARRAY_OR_GOTO(avctx, sc->edge_emu_buffer, alloc_size, EMU_EDGE_HEIGHT,
84 FF_ALLOCZ_ARRAY_OR_GOTO(avctx, me->scratchpad, alloc_size, 4 * 16 * 2,
86 me->temp = me->scratchpad;
87 sc->rd_scratchpad = me->scratchpad;
88 sc->b_scratchpad = me->scratchpad;
89 sc->obmc_scratchpad = me->scratchpad + 16;
93 av_freep(&sc->edge_emu_buffer);
94 return AVERROR(ENOMEM);
98 * Allocate a frame buffer
100 static int alloc_frame_buffer(AVCodecContext *avctx, Picture *pic,
101 MotionEstContext *me, ScratchpadContext *sc,
102 int chroma_x_shift, int chroma_y_shift,
103 int linesize, int uvlinesize)
105 int edges_needed = av_codec_is_encoder(avctx->codec);
109 if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
110 avctx->codec_id != AV_CODEC_ID_VC1IMAGE &&
111 avctx->codec_id != AV_CODEC_ID_MSS2) {
113 pic->f->width = avctx->width + 2 * EDGE_WIDTH;
114 pic->f->height = avctx->height + 2 * EDGE_WIDTH;
117 r = ff_thread_get_buffer(avctx, &pic->tf,
118 pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
120 pic->f->width = avctx->width;
121 pic->f->height = avctx->height;
122 pic->f->format = avctx->pix_fmt;
123 r = avcodec_default_get_buffer2(avctx, pic->f, 0);
126 if (r < 0 || !pic->f->buf[0]) {
127 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
134 for (i = 0; pic->f->data[i]; i++) {
135 int offset = (EDGE_WIDTH >> (i ? chroma_y_shift : 0)) *
136 pic->f->linesize[i] +
137 (EDGE_WIDTH >> (i ? chroma_x_shift : 0));
138 pic->f->data[i] += offset;
140 pic->f->width = avctx->width;
141 pic->f->height = avctx->height;
144 if (avctx->hwaccel) {
145 assert(!pic->hwaccel_picture_private);
146 if (avctx->hwaccel->frame_priv_data_size) {
147 pic->hwaccel_priv_buf = av_buffer_allocz(avctx->hwaccel->frame_priv_data_size);
148 if (!pic->hwaccel_priv_buf) {
149 av_log(avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
152 pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
156 if ((linesize && linesize != pic->f->linesize[0]) ||
157 (uvlinesize && uvlinesize != pic->f->linesize[1])) {
158 av_log(avctx, AV_LOG_ERROR,
159 "get_buffer() failed (stride changed: linesize=%d/%d uvlinesize=%d/%d)\n",
160 linesize, pic->f->linesize[0],
161 uvlinesize, pic->f->linesize[1]);
162 ff_mpeg_unref_picture(avctx, pic);
166 if (av_pix_fmt_count_planes(pic->f->format) > 2 &&
167 pic->f->linesize[1] != pic->f->linesize[2]) {
168 av_log(avctx, AV_LOG_ERROR,
169 "get_buffer() failed (uv stride mismatch)\n");
170 ff_mpeg_unref_picture(avctx, pic);
174 if (!sc->edge_emu_buffer &&
175 (ret = ff_mpeg_framesize_alloc(avctx, me, sc,
176 pic->f->linesize[0])) < 0) {
177 av_log(avctx, AV_LOG_ERROR,
178 "get_buffer() failed to allocate context scratch buffers.\n");
179 ff_mpeg_unref_picture(avctx, pic);
186 static int alloc_picture_tables(AVCodecContext *avctx, Picture *pic, int encoding, int out_format,
187 int mb_stride, int mb_width, int mb_height, int b8_stride)
189 const int big_mb_num = mb_stride * (mb_height + 1) + 1;
190 const int mb_array_size = mb_stride * mb_height;
191 const int b8_array_size = b8_stride * mb_height * 2;
195 pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
196 pic->qscale_table_buf = av_buffer_allocz(big_mb_num + mb_stride);
197 pic->mb_type_buf = av_buffer_allocz((big_mb_num + mb_stride) *
199 if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
200 return AVERROR(ENOMEM);
203 pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
204 pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
205 pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
206 if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
207 return AVERROR(ENOMEM);
210 if (out_format == FMT_H263 || encoding ||
214 (avctx->export_side_data & AV_CODEC_EXPORT_DATA_MVS)) {
215 int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
216 int ref_index_size = 4 * mb_array_size;
218 for (i = 0; mv_size && i < 2; i++) {
219 pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
220 pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
221 if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
222 return AVERROR(ENOMEM);
226 pic->alloc_mb_width = mb_width;
227 pic->alloc_mb_height = mb_height;
233 * Allocate a Picture.
234 * The pixels are allocated/set by calling get_buffer() if shared = 0
236 int ff_alloc_picture(AVCodecContext *avctx, Picture *pic, MotionEstContext *me,
237 ScratchpadContext *sc, int shared, int encoding,
238 int chroma_x_shift, int chroma_y_shift, int out_format,
239 int mb_stride, int mb_width, int mb_height, int b8_stride,
240 ptrdiff_t *linesize, ptrdiff_t *uvlinesize)
244 if (pic->qscale_table_buf)
245 if ( pic->alloc_mb_width != mb_width
246 || pic->alloc_mb_height != mb_height)
247 ff_free_picture_tables(pic);
250 av_assert0(pic->f->data[0]);
253 av_assert0(!pic->f->buf[0]);
254 if (alloc_frame_buffer(avctx, pic, me, sc,
255 chroma_x_shift, chroma_y_shift,
256 *linesize, *uvlinesize) < 0)
259 *linesize = pic->f->linesize[0];
260 *uvlinesize = pic->f->linesize[1];
263 if (!pic->qscale_table_buf)
264 ret = alloc_picture_tables(avctx, pic, encoding, out_format,
265 mb_stride, mb_width, mb_height, b8_stride);
267 ret = make_tables_writable(pic);
272 pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
273 pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
274 pic->mb_mean = pic->mb_mean_buf->data;
277 pic->mbskip_table = pic->mbskip_table_buf->data;
278 pic->qscale_table = pic->qscale_table_buf->data + 2 * mb_stride + 1;
279 pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * mb_stride + 1;
281 if (pic->motion_val_buf[0]) {
282 for (i = 0; i < 2; i++) {
283 pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
284 pic->ref_index[i] = pic->ref_index_buf[i]->data;
290 av_log(avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
291 ff_mpeg_unref_picture(avctx, pic);
292 ff_free_picture_tables(pic);
293 return AVERROR(ENOMEM);
297 * Deallocate a picture.
299 void ff_mpeg_unref_picture(AVCodecContext *avctx, Picture *pic)
301 int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
304 /* WM Image / Screen codecs allocate internal buffers with different
305 * dimensions / colorspaces; ignore user-defined callbacks for these. */
306 if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
307 avctx->codec_id != AV_CODEC_ID_VC1IMAGE &&
308 avctx->codec_id != AV_CODEC_ID_MSS2)
309 ff_thread_release_buffer(avctx, &pic->tf);
311 av_frame_unref(pic->f);
313 av_buffer_unref(&pic->hwaccel_priv_buf);
315 if (pic->needs_realloc)
316 ff_free_picture_tables(pic);
318 memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
321 int ff_update_picture_tables(Picture *dst, Picture *src)
325 #define UPDATE_TABLE(table) \
328 (!dst->table || dst->table->buffer != src->table->buffer)) { \
329 av_buffer_unref(&dst->table); \
330 dst->table = av_buffer_ref(src->table); \
332 ff_free_picture_tables(dst); \
333 return AVERROR(ENOMEM); \
338 UPDATE_TABLE(mb_var_buf);
339 UPDATE_TABLE(mc_mb_var_buf);
340 UPDATE_TABLE(mb_mean_buf);
341 UPDATE_TABLE(mbskip_table_buf);
342 UPDATE_TABLE(qscale_table_buf);
343 UPDATE_TABLE(mb_type_buf);
344 for (i = 0; i < 2; i++) {
345 UPDATE_TABLE(motion_val_buf[i]);
346 UPDATE_TABLE(ref_index_buf[i]);
349 dst->mb_var = src->mb_var;
350 dst->mc_mb_var = src->mc_mb_var;
351 dst->mb_mean = src->mb_mean;
352 dst->mbskip_table = src->mbskip_table;
353 dst->qscale_table = src->qscale_table;
354 dst->mb_type = src->mb_type;
355 for (i = 0; i < 2; i++) {
356 dst->motion_val[i] = src->motion_val[i];
357 dst->ref_index[i] = src->ref_index[i];
360 dst->alloc_mb_width = src->alloc_mb_width;
361 dst->alloc_mb_height = src->alloc_mb_height;
366 int ff_mpeg_ref_picture(AVCodecContext *avctx, Picture *dst, Picture *src)
370 av_assert0(!dst->f->buf[0]);
371 av_assert0(src->f->buf[0]);
375 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
379 ret = ff_update_picture_tables(dst, src);
383 if (src->hwaccel_picture_private) {
384 dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
385 if (!dst->hwaccel_priv_buf) {
386 ret = AVERROR(ENOMEM);
389 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
392 dst->field_picture = src->field_picture;
393 dst->mb_var_sum = src->mb_var_sum;
394 dst->mc_mb_var_sum = src->mc_mb_var_sum;
395 dst->b_frame_score = src->b_frame_score;
396 dst->needs_realloc = src->needs_realloc;
397 dst->reference = src->reference;
398 dst->shared = src->shared;
400 memcpy(dst->encoding_error, src->encoding_error,
401 sizeof(dst->encoding_error));
405 ff_mpeg_unref_picture(avctx, dst);
409 static inline int pic_is_unused(Picture *pic)
413 if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
418 static int find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
423 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
424 if (!picture[i].f->buf[0])
428 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
429 if (pic_is_unused(&picture[i]))
434 av_log(avctx, AV_LOG_FATAL,
435 "Internal error, picture buffer overflow\n");
436 /* We could return -1, but the codec would crash trying to draw into a
437 * non-existing frame anyway. This is safer than waiting for a random crash.
438 * Also the return of this is never useful, an encoder must only allocate
439 * as much as allowed in the specification. This has no relationship to how
440 * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
441 * enough for such valid streams).
442 * Plus, a decoder has to check stream validity and remove frames if too
443 * many reference frames are around. Waiting for "OOM" is not correct at
444 * all. Similarly, missing reference frames have to be replaced by
445 * interpolated/MC frames, anything else is a bug in the codec ...
451 int ff_find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
453 int ret = find_unused_picture(avctx, picture, shared);
455 if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
456 if (picture[ret].needs_realloc) {
457 picture[ret].needs_realloc = 0;
458 ff_free_picture_tables(&picture[ret]);
459 ff_mpeg_unref_picture(avctx, &picture[ret]);
465 void ff_free_picture_tables(Picture *pic)
469 pic->alloc_mb_width =
470 pic->alloc_mb_height = 0;
472 av_buffer_unref(&pic->mb_var_buf);
473 av_buffer_unref(&pic->mc_mb_var_buf);
474 av_buffer_unref(&pic->mb_mean_buf);
475 av_buffer_unref(&pic->mbskip_table_buf);
476 av_buffer_unref(&pic->qscale_table_buf);
477 av_buffer_unref(&pic->mb_type_buf);
479 for (i = 0; i < 2; i++) {
480 av_buffer_unref(&pic->motion_val_buf[i]);
481 av_buffer_unref(&pic->ref_index_buf[i]);