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 if (!FF_ALLOCZ_TYPED_ARRAY(sc->edge_emu_buffer, alloc_size * EMU_EDGE_HEIGHT) ||
82 !FF_ALLOCZ_TYPED_ARRAY(me->scratchpad, alloc_size * 4 * 16 * 2))
83 return AVERROR(ENOMEM);
84 me->temp = me->scratchpad;
85 sc->rd_scratchpad = me->scratchpad;
86 sc->b_scratchpad = me->scratchpad;
87 sc->obmc_scratchpad = me->scratchpad + 16;
93 * Allocate a frame buffer
95 static int alloc_frame_buffer(AVCodecContext *avctx, Picture *pic,
96 MotionEstContext *me, ScratchpadContext *sc,
97 int chroma_x_shift, int chroma_y_shift,
98 int linesize, int uvlinesize)
100 int edges_needed = av_codec_is_encoder(avctx->codec);
104 if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
105 avctx->codec_id != AV_CODEC_ID_VC1IMAGE &&
106 avctx->codec_id != AV_CODEC_ID_MSS2) {
108 pic->f->width = avctx->width + 2 * EDGE_WIDTH;
109 pic->f->height = avctx->height + 2 * EDGE_WIDTH;
112 r = ff_thread_get_buffer(avctx, &pic->tf,
113 pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
115 pic->f->width = avctx->width;
116 pic->f->height = avctx->height;
117 pic->f->format = avctx->pix_fmt;
118 r = avcodec_default_get_buffer2(avctx, pic->f, 0);
121 if (r < 0 || !pic->f->buf[0]) {
122 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
129 for (i = 0; pic->f->data[i]; i++) {
130 int offset = (EDGE_WIDTH >> (i ? chroma_y_shift : 0)) *
131 pic->f->linesize[i] +
132 (EDGE_WIDTH >> (i ? chroma_x_shift : 0));
133 pic->f->data[i] += offset;
135 pic->f->width = avctx->width;
136 pic->f->height = avctx->height;
139 if (avctx->hwaccel) {
140 assert(!pic->hwaccel_picture_private);
141 if (avctx->hwaccel->frame_priv_data_size) {
142 pic->hwaccel_priv_buf = av_buffer_allocz(avctx->hwaccel->frame_priv_data_size);
143 if (!pic->hwaccel_priv_buf) {
144 av_log(avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
147 pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
151 if ((linesize && linesize != pic->f->linesize[0]) ||
152 (uvlinesize && uvlinesize != pic->f->linesize[1])) {
153 av_log(avctx, AV_LOG_ERROR,
154 "get_buffer() failed (stride changed: linesize=%d/%d uvlinesize=%d/%d)\n",
155 linesize, pic->f->linesize[0],
156 uvlinesize, pic->f->linesize[1]);
157 ff_mpeg_unref_picture(avctx, pic);
161 if (av_pix_fmt_count_planes(pic->f->format) > 2 &&
162 pic->f->linesize[1] != pic->f->linesize[2]) {
163 av_log(avctx, AV_LOG_ERROR,
164 "get_buffer() failed (uv stride mismatch)\n");
165 ff_mpeg_unref_picture(avctx, pic);
169 if (!sc->edge_emu_buffer &&
170 (ret = ff_mpeg_framesize_alloc(avctx, me, sc,
171 pic->f->linesize[0])) < 0) {
172 av_log(avctx, AV_LOG_ERROR,
173 "get_buffer() failed to allocate context scratch buffers.\n");
174 ff_mpeg_unref_picture(avctx, pic);
181 static int alloc_picture_tables(AVCodecContext *avctx, Picture *pic, int encoding, int out_format,
182 int mb_stride, int mb_width, int mb_height, int b8_stride)
184 const int big_mb_num = mb_stride * (mb_height + 1) + 1;
185 const int mb_array_size = mb_stride * mb_height;
186 const int b8_array_size = b8_stride * mb_height * 2;
190 pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
191 pic->qscale_table_buf = av_buffer_allocz(big_mb_num + mb_stride);
192 pic->mb_type_buf = av_buffer_allocz((big_mb_num + mb_stride) *
194 if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
195 return AVERROR(ENOMEM);
198 pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
199 pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
200 pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
201 if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
202 return AVERROR(ENOMEM);
205 if (out_format == FMT_H263 || encoding ||
209 (avctx->export_side_data & AV_CODEC_EXPORT_DATA_MVS)) {
210 int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
211 int ref_index_size = 4 * mb_array_size;
213 for (i = 0; mv_size && i < 2; i++) {
214 pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
215 pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
216 if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
217 return AVERROR(ENOMEM);
221 pic->alloc_mb_width = mb_width;
222 pic->alloc_mb_height = mb_height;
228 * Allocate a Picture.
229 * The pixels are allocated/set by calling get_buffer() if shared = 0
231 int ff_alloc_picture(AVCodecContext *avctx, Picture *pic, MotionEstContext *me,
232 ScratchpadContext *sc, int shared, int encoding,
233 int chroma_x_shift, int chroma_y_shift, int out_format,
234 int mb_stride, int mb_width, int mb_height, int b8_stride,
235 ptrdiff_t *linesize, ptrdiff_t *uvlinesize)
239 if (pic->qscale_table_buf)
240 if ( pic->alloc_mb_width != mb_width
241 || pic->alloc_mb_height != mb_height)
242 ff_free_picture_tables(pic);
245 av_assert0(pic->f->data[0]);
248 av_assert0(!pic->f->buf[0]);
249 if (alloc_frame_buffer(avctx, pic, me, sc,
250 chroma_x_shift, chroma_y_shift,
251 *linesize, *uvlinesize) < 0)
254 *linesize = pic->f->linesize[0];
255 *uvlinesize = pic->f->linesize[1];
258 if (!pic->qscale_table_buf)
259 ret = alloc_picture_tables(avctx, pic, encoding, out_format,
260 mb_stride, mb_width, mb_height, b8_stride);
262 ret = make_tables_writable(pic);
267 pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
268 pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
269 pic->mb_mean = pic->mb_mean_buf->data;
272 pic->mbskip_table = pic->mbskip_table_buf->data;
273 pic->qscale_table = pic->qscale_table_buf->data + 2 * mb_stride + 1;
274 pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * mb_stride + 1;
276 if (pic->motion_val_buf[0]) {
277 for (i = 0; i < 2; i++) {
278 pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
279 pic->ref_index[i] = pic->ref_index_buf[i]->data;
285 av_log(avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
286 ff_mpeg_unref_picture(avctx, pic);
287 ff_free_picture_tables(pic);
288 return AVERROR(ENOMEM);
292 * Deallocate a picture.
294 void ff_mpeg_unref_picture(AVCodecContext *avctx, Picture *pic)
296 int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
299 /* WM Image / Screen codecs allocate internal buffers with different
300 * dimensions / colorspaces; ignore user-defined callbacks for these. */
301 if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
302 avctx->codec_id != AV_CODEC_ID_VC1IMAGE &&
303 avctx->codec_id != AV_CODEC_ID_MSS2)
304 ff_thread_release_buffer(avctx, &pic->tf);
306 av_frame_unref(pic->f);
308 av_buffer_unref(&pic->hwaccel_priv_buf);
310 if (pic->needs_realloc)
311 ff_free_picture_tables(pic);
313 memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
316 int ff_update_picture_tables(Picture *dst, Picture *src)
320 ret = av_buffer_replace(&dst->mb_var_buf, src->mb_var_buf);
321 ret |= av_buffer_replace(&dst->mc_mb_var_buf, src->mc_mb_var_buf);
322 ret |= av_buffer_replace(&dst->mb_mean_buf, src->mb_mean_buf);
323 ret |= av_buffer_replace(&dst->mbskip_table_buf, src->mbskip_table_buf);
324 ret |= av_buffer_replace(&dst->qscale_table_buf, src->qscale_table_buf);
325 ret |= av_buffer_replace(&dst->mb_type_buf, src->mb_type_buf);
326 for (i = 0; i < 2; i++) {
327 ret |= av_buffer_replace(&dst->motion_val_buf[i], src->motion_val_buf[i]);
328 ret |= av_buffer_replace(&dst->ref_index_buf[i], src->ref_index_buf[i]);
332 ff_free_picture_tables(dst);
336 dst->mb_var = src->mb_var;
337 dst->mc_mb_var = src->mc_mb_var;
338 dst->mb_mean = src->mb_mean;
339 dst->mbskip_table = src->mbskip_table;
340 dst->qscale_table = src->qscale_table;
341 dst->mb_type = src->mb_type;
342 for (i = 0; i < 2; i++) {
343 dst->motion_val[i] = src->motion_val[i];
344 dst->ref_index[i] = src->ref_index[i];
347 dst->alloc_mb_width = src->alloc_mb_width;
348 dst->alloc_mb_height = src->alloc_mb_height;
353 int ff_mpeg_ref_picture(AVCodecContext *avctx, Picture *dst, Picture *src)
357 av_assert0(!dst->f->buf[0]);
358 av_assert0(src->f->buf[0]);
362 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
366 ret = ff_update_picture_tables(dst, src);
370 if (src->hwaccel_picture_private) {
371 dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
372 if (!dst->hwaccel_priv_buf) {
373 ret = AVERROR(ENOMEM);
376 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
379 dst->field_picture = src->field_picture;
380 dst->mb_var_sum = src->mb_var_sum;
381 dst->mc_mb_var_sum = src->mc_mb_var_sum;
382 dst->b_frame_score = src->b_frame_score;
383 dst->needs_realloc = src->needs_realloc;
384 dst->reference = src->reference;
385 dst->shared = src->shared;
387 memcpy(dst->encoding_error, src->encoding_error,
388 sizeof(dst->encoding_error));
392 ff_mpeg_unref_picture(avctx, dst);
396 static inline int pic_is_unused(Picture *pic)
400 if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
405 static int find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
410 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
411 if (!picture[i].f->buf[0])
415 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
416 if (pic_is_unused(&picture[i]))
421 av_log(avctx, AV_LOG_FATAL,
422 "Internal error, picture buffer overflow\n");
423 /* We could return -1, but the codec would crash trying to draw into a
424 * non-existing frame anyway. This is safer than waiting for a random crash.
425 * Also the return of this is never useful, an encoder must only allocate
426 * as much as allowed in the specification. This has no relationship to how
427 * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
428 * enough for such valid streams).
429 * Plus, a decoder has to check stream validity and remove frames if too
430 * many reference frames are around. Waiting for "OOM" is not correct at
431 * all. Similarly, missing reference frames have to be replaced by
432 * interpolated/MC frames, anything else is a bug in the codec ...
438 int ff_find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
440 int ret = find_unused_picture(avctx, picture, shared);
442 if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
443 if (picture[ret].needs_realloc) {
444 picture[ret].needs_realloc = 0;
445 ff_free_picture_tables(&picture[ret]);
446 ff_mpeg_unref_picture(avctx, &picture[ret]);
452 void ff_free_picture_tables(Picture *pic)
456 pic->alloc_mb_width =
457 pic->alloc_mb_height = 0;
459 av_buffer_unref(&pic->mb_var_buf);
460 av_buffer_unref(&pic->mc_mb_var_buf);
461 av_buffer_unref(&pic->mb_mean_buf);
462 av_buffer_unref(&pic->mbskip_table_buf);
463 av_buffer_unref(&pic->qscale_table_buf);
464 av_buffer_unref(&pic->mb_type_buf);
466 for (i = 0; i < 2; i++) {
467 av_buffer_unref(&pic->motion_val_buf[i]);
468 av_buffer_unref(&pic->ref_index_buf[i]);