2 * The simplest mpeg encoder (well, it was the simplest!)
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
8 * This file is part of FFmpeg.
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 * The simplest mpeg encoder (well, it was the simplest!).
30 #include "libavutil/attributes.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/internal.h"
34 #include "libavutil/timer.h"
37 #include "h264chroma.h"
40 #include "mpegvideo.h"
46 static const uint8_t ff_default_chroma_qscale_table[32] = {
47 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
48 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
49 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
52 const uint8_t ff_mpeg1_dc_scale_table[128] = {
53 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
54 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
55 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
56 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
57 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
58 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
59 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
60 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
61 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
64 static const uint8_t mpeg2_dc_scale_table1[128] = {
65 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
66 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
67 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
68 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
69 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
70 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
71 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
72 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
73 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
76 static const uint8_t mpeg2_dc_scale_table2[128] = {
77 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
78 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
79 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
80 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
81 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
82 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
83 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
84 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
85 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
88 static const uint8_t mpeg2_dc_scale_table3[128] = {
89 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
90 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
91 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
92 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
93 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
94 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
95 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
96 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
97 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
100 const uint8_t *const ff_mpeg2_dc_scale_table[4] = {
101 ff_mpeg1_dc_scale_table,
102 mpeg2_dc_scale_table1,
103 mpeg2_dc_scale_table2,
104 mpeg2_dc_scale_table3,
107 const enum AVPixelFormat ff_pixfmt_list_420[] = {
112 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
113 int16_t *block, int n, int qscale)
115 int i, level, nCoeffs;
116 const uint16_t *quant_matrix;
118 nCoeffs= s->block_last_index[n];
120 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
121 /* XXX: only mpeg1 */
122 quant_matrix = s->intra_matrix;
123 for(i=1;i<=nCoeffs;i++) {
124 int j= s->intra_scantable.permutated[i];
129 level = (int)(level * qscale * quant_matrix[j]) >> 3;
130 level = (level - 1) | 1;
133 level = (int)(level * qscale * quant_matrix[j]) >> 3;
134 level = (level - 1) | 1;
141 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
142 int16_t *block, int n, int qscale)
144 int i, level, nCoeffs;
145 const uint16_t *quant_matrix;
147 nCoeffs= s->block_last_index[n];
149 quant_matrix = s->inter_matrix;
150 for(i=0; i<=nCoeffs; i++) {
151 int j= s->intra_scantable.permutated[i];
156 level = (((level << 1) + 1) * qscale *
157 ((int) (quant_matrix[j]))) >> 4;
158 level = (level - 1) | 1;
161 level = (((level << 1) + 1) * qscale *
162 ((int) (quant_matrix[j]))) >> 4;
163 level = (level - 1) | 1;
170 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
171 int16_t *block, int n, int qscale)
173 int i, level, nCoeffs;
174 const uint16_t *quant_matrix;
176 if(s->alternate_scan) nCoeffs= 63;
177 else nCoeffs= s->block_last_index[n];
179 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
180 quant_matrix = s->intra_matrix;
181 for(i=1;i<=nCoeffs;i++) {
182 int j= s->intra_scantable.permutated[i];
187 level = (int)(level * qscale * quant_matrix[j]) >> 3;
190 level = (int)(level * qscale * quant_matrix[j]) >> 3;
197 static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
198 int16_t *block, int n, int qscale)
200 int i, level, nCoeffs;
201 const uint16_t *quant_matrix;
204 if(s->alternate_scan) nCoeffs= 63;
205 else nCoeffs= s->block_last_index[n];
207 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
209 quant_matrix = s->intra_matrix;
210 for(i=1;i<=nCoeffs;i++) {
211 int j= s->intra_scantable.permutated[i];
216 level = (int)(level * qscale * quant_matrix[j]) >> 3;
219 level = (int)(level * qscale * quant_matrix[j]) >> 3;
228 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
229 int16_t *block, int n, int qscale)
231 int i, level, nCoeffs;
232 const uint16_t *quant_matrix;
235 if(s->alternate_scan) nCoeffs= 63;
236 else nCoeffs= s->block_last_index[n];
238 quant_matrix = s->inter_matrix;
239 for(i=0; i<=nCoeffs; i++) {
240 int j= s->intra_scantable.permutated[i];
245 level = (((level << 1) + 1) * qscale *
246 ((int) (quant_matrix[j]))) >> 4;
249 level = (((level << 1) + 1) * qscale *
250 ((int) (quant_matrix[j]))) >> 4;
259 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
260 int16_t *block, int n, int qscale)
262 int i, level, qmul, qadd;
265 av_assert2(s->block_last_index[n]>=0 || s->h263_aic);
270 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
271 qadd = (qscale - 1) | 1;
278 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
280 for(i=1; i<=nCoeffs; i++) {
284 level = level * qmul - qadd;
286 level = level * qmul + qadd;
293 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
294 int16_t *block, int n, int qscale)
296 int i, level, qmul, qadd;
299 av_assert2(s->block_last_index[n]>=0);
301 qadd = (qscale - 1) | 1;
304 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
306 for(i=0; i<=nCoeffs; i++) {
310 level = level * qmul - qadd;
312 level = level * qmul + qadd;
319 static void mpeg_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
321 int mb_x, int mb_y, int mb_intra, int mb_skipped)
323 MpegEncContext *s = opaque;
326 s->mv_type = mv_type;
327 s->mb_intra = mb_intra;
328 s->mb_skipped = mb_skipped;
331 memcpy(s->mv, mv, sizeof(*mv));
333 ff_init_block_index(s);
334 ff_update_block_index(s);
336 s->dsp.clear_blocks(s->block[0]);
338 s->dest[0] = s->current_picture.f.data[0] + (s->mb_y * 16 * s->linesize) + s->mb_x * 16;
339 s->dest[1] = s->current_picture.f.data[1] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);
340 s->dest[2] = s->current_picture.f.data[2] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);
343 av_log(s->avctx, AV_LOG_DEBUG, "Interlaced error concealment is not fully implemented\n");
344 ff_MPV_decode_mb(s, s->block);
347 /* init common dct for both encoder and decoder */
348 av_cold int ff_dct_common_init(MpegEncContext *s)
350 ff_dsputil_init(&s->dsp, s->avctx);
351 ff_h264chroma_init(&s->h264chroma, 8); //for lowres
352 ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
353 ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
355 s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
356 s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
357 s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
358 s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
359 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
360 if (s->flags & CODEC_FLAG_BITEXACT)
361 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
362 s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
365 ff_MPV_common_init_axp(s);
367 ff_MPV_common_init_arm(s);
369 ff_MPV_common_init_ppc(s);
371 ff_MPV_common_init_x86(s);
373 /* load & permutate scantables
374 * note: only wmv uses different ones
376 if (s->alternate_scan) {
377 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
378 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
380 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
381 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
383 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
384 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
389 static int frame_size_alloc(MpegEncContext *s, int linesize)
391 int alloc_size = FFALIGN(FFABS(linesize) + 64, 32);
393 // edge emu needs blocksize + filter length - 1
394 // (= 17x17 for halfpel / 21x21 for h264)
395 // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9
396 // at uvlinesize. It supports only YUV420 so 24x24 is enough
397 // linesize * interlaced * MBsize
398 FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, alloc_size * 4 * 24,
401 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, alloc_size * 4 * 16 * 2,
403 s->me.temp = s->me.scratchpad;
404 s->rd_scratchpad = s->me.scratchpad;
405 s->b_scratchpad = s->me.scratchpad;
406 s->obmc_scratchpad = s->me.scratchpad + 16;
410 av_freep(&s->edge_emu_buffer);
411 return AVERROR(ENOMEM);
415 * Allocate a frame buffer
417 static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
419 int edges_needed = av_codec_is_encoder(s->avctx->codec);
423 if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
424 s->codec_id != AV_CODEC_ID_VC1IMAGE &&
425 s->codec_id != AV_CODEC_ID_MSS2) {
427 pic->f.width = s->avctx->width + 2 * EDGE_WIDTH;
428 pic->f.height = s->avctx->height + 2 * EDGE_WIDTH;
431 r = ff_thread_get_buffer(s->avctx, &pic->tf,
432 pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
434 pic->f.width = s->avctx->width;
435 pic->f.height = s->avctx->height;
436 pic->f.format = s->avctx->pix_fmt;
437 r = avcodec_default_get_buffer2(s->avctx, &pic->f, 0);
440 if (r < 0 || !pic->f.buf[0]) {
441 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
448 for (i = 0; pic->f.data[i]; i++) {
449 int offset = (EDGE_WIDTH >> (i ? s->chroma_y_shift : 0)) *
451 (EDGE_WIDTH >> (i ? s->chroma_x_shift : 0));
452 pic->f.data[i] += offset;
454 pic->f.width = s->avctx->width;
455 pic->f.height = s->avctx->height;
458 if (s->avctx->hwaccel) {
459 assert(!pic->hwaccel_picture_private);
460 if (s->avctx->hwaccel->priv_data_size) {
461 pic->hwaccel_priv_buf = av_buffer_allocz(s->avctx->hwaccel->priv_data_size);
462 if (!pic->hwaccel_priv_buf) {
463 av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
466 pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
470 if (s->linesize && (s->linesize != pic->f.linesize[0] ||
471 s->uvlinesize != pic->f.linesize[1])) {
472 av_log(s->avctx, AV_LOG_ERROR,
473 "get_buffer() failed (stride changed)\n");
474 ff_mpeg_unref_picture(s, pic);
478 if (pic->f.linesize[1] != pic->f.linesize[2]) {
479 av_log(s->avctx, AV_LOG_ERROR,
480 "get_buffer() failed (uv stride mismatch)\n");
481 ff_mpeg_unref_picture(s, pic);
485 if (!s->edge_emu_buffer &&
486 (ret = frame_size_alloc(s, pic->f.linesize[0])) < 0) {
487 av_log(s->avctx, AV_LOG_ERROR,
488 "get_buffer() failed to allocate context scratch buffers.\n");
489 ff_mpeg_unref_picture(s, pic);
496 void ff_free_picture_tables(Picture *pic)
500 pic->alloc_mb_width =
501 pic->alloc_mb_height = 0;
503 av_buffer_unref(&pic->mb_var_buf);
504 av_buffer_unref(&pic->mc_mb_var_buf);
505 av_buffer_unref(&pic->mb_mean_buf);
506 av_buffer_unref(&pic->mbskip_table_buf);
507 av_buffer_unref(&pic->qscale_table_buf);
508 av_buffer_unref(&pic->mb_type_buf);
510 for (i = 0; i < 2; i++) {
511 av_buffer_unref(&pic->motion_val_buf[i]);
512 av_buffer_unref(&pic->ref_index_buf[i]);
516 static int alloc_picture_tables(MpegEncContext *s, Picture *pic)
518 const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;
519 const int mb_array_size = s->mb_stride * s->mb_height;
520 const int b8_array_size = s->b8_stride * s->mb_height * 2;
524 pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
525 pic->qscale_table_buf = av_buffer_allocz(big_mb_num + s->mb_stride);
526 pic->mb_type_buf = av_buffer_allocz((big_mb_num + s->mb_stride) *
528 if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
529 return AVERROR(ENOMEM);
532 pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
533 pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
534 pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
535 if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
536 return AVERROR(ENOMEM);
539 if (s->out_format == FMT_H263 || s->encoding || s->avctx->debug_mv) {
540 int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
541 int ref_index_size = 4 * mb_array_size;
543 for (i = 0; mv_size && i < 2; i++) {
544 pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
545 pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
546 if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
547 return AVERROR(ENOMEM);
551 pic->alloc_mb_width = s->mb_width;
552 pic->alloc_mb_height = s->mb_height;
557 static int make_tables_writable(Picture *pic)
560 #define MAKE_WRITABLE(table) \
563 (ret = av_buffer_make_writable(&pic->table)) < 0)\
567 MAKE_WRITABLE(mb_var_buf);
568 MAKE_WRITABLE(mc_mb_var_buf);
569 MAKE_WRITABLE(mb_mean_buf);
570 MAKE_WRITABLE(mbskip_table_buf);
571 MAKE_WRITABLE(qscale_table_buf);
572 MAKE_WRITABLE(mb_type_buf);
574 for (i = 0; i < 2; i++) {
575 MAKE_WRITABLE(motion_val_buf[i]);
576 MAKE_WRITABLE(ref_index_buf[i]);
583 * Allocate a Picture.
584 * The pixels are allocated/set by calling get_buffer() if shared = 0
586 int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
590 if (pic->qscale_table_buf)
591 if ( pic->alloc_mb_width != s->mb_width
592 || pic->alloc_mb_height != s->mb_height)
593 ff_free_picture_tables(pic);
596 av_assert0(pic->f.data[0]);
599 av_assert0(!pic->f.buf[0]);
601 if (alloc_frame_buffer(s, pic) < 0)
604 s->linesize = pic->f.linesize[0];
605 s->uvlinesize = pic->f.linesize[1];
608 if (!pic->qscale_table_buf)
609 ret = alloc_picture_tables(s, pic);
611 ret = make_tables_writable(pic);
616 pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
617 pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
618 pic->mb_mean = pic->mb_mean_buf->data;
621 pic->mbskip_table = pic->mbskip_table_buf->data;
622 pic->qscale_table = pic->qscale_table_buf->data + 2 * s->mb_stride + 1;
623 pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;
625 if (pic->motion_val_buf[0]) {
626 for (i = 0; i < 2; i++) {
627 pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
628 pic->ref_index[i] = pic->ref_index_buf[i]->data;
634 av_log(s->avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
635 ff_mpeg_unref_picture(s, pic);
636 ff_free_picture_tables(pic);
637 return AVERROR(ENOMEM);
641 * Deallocate a picture.
643 void ff_mpeg_unref_picture(MpegEncContext *s, Picture *pic)
645 int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
648 /* WM Image / Screen codecs allocate internal buffers with different
649 * dimensions / colorspaces; ignore user-defined callbacks for these. */
650 if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
651 s->codec_id != AV_CODEC_ID_VC1IMAGE &&
652 s->codec_id != AV_CODEC_ID_MSS2)
653 ff_thread_release_buffer(s->avctx, &pic->tf);
655 av_frame_unref(&pic->f);
657 av_buffer_unref(&pic->hwaccel_priv_buf);
659 if (pic->needs_realloc)
660 ff_free_picture_tables(pic);
662 memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
665 static int update_picture_tables(Picture *dst, Picture *src)
669 #define UPDATE_TABLE(table)\
672 (!dst->table || dst->table->buffer != src->table->buffer)) {\
673 av_buffer_unref(&dst->table);\
674 dst->table = av_buffer_ref(src->table);\
676 ff_free_picture_tables(dst);\
677 return AVERROR(ENOMEM);\
682 UPDATE_TABLE(mb_var_buf);
683 UPDATE_TABLE(mc_mb_var_buf);
684 UPDATE_TABLE(mb_mean_buf);
685 UPDATE_TABLE(mbskip_table_buf);
686 UPDATE_TABLE(qscale_table_buf);
687 UPDATE_TABLE(mb_type_buf);
688 for (i = 0; i < 2; i++) {
689 UPDATE_TABLE(motion_val_buf[i]);
690 UPDATE_TABLE(ref_index_buf[i]);
693 dst->mb_var = src->mb_var;
694 dst->mc_mb_var = src->mc_mb_var;
695 dst->mb_mean = src->mb_mean;
696 dst->mbskip_table = src->mbskip_table;
697 dst->qscale_table = src->qscale_table;
698 dst->mb_type = src->mb_type;
699 for (i = 0; i < 2; i++) {
700 dst->motion_val[i] = src->motion_val[i];
701 dst->ref_index[i] = src->ref_index[i];
704 dst->alloc_mb_width = src->alloc_mb_width;
705 dst->alloc_mb_height = src->alloc_mb_height;
710 int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src)
714 av_assert0(!dst->f.buf[0]);
715 av_assert0(src->f.buf[0]);
719 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
723 ret = update_picture_tables(dst, src);
727 if (src->hwaccel_picture_private) {
728 dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
729 if (!dst->hwaccel_priv_buf)
731 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
734 dst->field_picture = src->field_picture;
735 dst->mb_var_sum = src->mb_var_sum;
736 dst->mc_mb_var_sum = src->mc_mb_var_sum;
737 dst->b_frame_score = src->b_frame_score;
738 dst->needs_realloc = src->needs_realloc;
739 dst->reference = src->reference;
740 dst->shared = src->shared;
744 ff_mpeg_unref_picture(s, dst);
748 static void exchange_uv(MpegEncContext *s)
753 s->pblocks[4] = s->pblocks[5];
757 static int init_duplicate_context(MpegEncContext *s)
759 int y_size = s->b8_stride * (2 * s->mb_height + 1);
760 int c_size = s->mb_stride * (s->mb_height + 1);
761 int yc_size = y_size + 2 * c_size;
764 if (s->mb_height & 1)
765 yc_size += 2*s->b8_stride + 2*s->mb_stride;
772 s->obmc_scratchpad = NULL;
775 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
776 ME_MAP_SIZE * sizeof(uint32_t), fail)
777 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
778 ME_MAP_SIZE * sizeof(uint32_t), fail)
779 if (s->avctx->noise_reduction) {
780 FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
781 2 * 64 * sizeof(int), fail)
784 FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
785 s->block = s->blocks[0];
787 for (i = 0; i < 12; i++) {
788 s->pblocks[i] = &s->block[i];
790 if (s->avctx->codec_tag == AV_RL32("VCR2"))
793 if (s->out_format == FMT_H263) {
795 FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
796 yc_size * sizeof(int16_t) * 16, fail);
797 s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
798 s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
799 s->ac_val[2] = s->ac_val[1] + c_size;
804 return -1; // free() through ff_MPV_common_end()
807 static void free_duplicate_context(MpegEncContext *s)
812 av_freep(&s->edge_emu_buffer);
813 av_freep(&s->me.scratchpad);
817 s->obmc_scratchpad = NULL;
819 av_freep(&s->dct_error_sum);
820 av_freep(&s->me.map);
821 av_freep(&s->me.score_map);
822 av_freep(&s->blocks);
823 av_freep(&s->ac_val_base);
827 static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
829 #define COPY(a) bak->a = src->a
830 COPY(edge_emu_buffer);
835 COPY(obmc_scratchpad);
842 COPY(me.map_generation);
854 int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
858 // FIXME copy only needed parts
860 backup_duplicate_context(&bak, dst);
861 memcpy(dst, src, sizeof(MpegEncContext));
862 backup_duplicate_context(dst, &bak);
863 for (i = 0; i < 12; i++) {
864 dst->pblocks[i] = &dst->block[i];
866 if (dst->avctx->codec_tag == AV_RL32("VCR2"))
868 if (!dst->edge_emu_buffer &&
869 (ret = frame_size_alloc(dst, dst->linesize)) < 0) {
870 av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
871 "scratch buffers.\n");
874 // STOP_TIMER("update_duplicate_context")
875 // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
879 int ff_mpeg_update_thread_context(AVCodecContext *dst,
880 const AVCodecContext *src)
883 MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
890 // FIXME can parameters change on I-frames?
891 // in that case dst may need a reinit
892 if (!s->context_initialized) {
893 memcpy(s, s1, sizeof(MpegEncContext));
896 s->bitstream_buffer = NULL;
897 s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
899 if (s1->context_initialized){
900 // s->picture_range_start += MAX_PICTURE_COUNT;
901 // s->picture_range_end += MAX_PICTURE_COUNT;
902 if((ret = ff_MPV_common_init(s)) < 0){
903 memset(s, 0, sizeof(MpegEncContext));
910 if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
911 s->context_reinit = 0;
912 s->height = s1->height;
913 s->width = s1->width;
914 if ((ret = ff_MPV_common_frame_size_change(s)) < 0)
918 s->avctx->coded_height = s1->avctx->coded_height;
919 s->avctx->coded_width = s1->avctx->coded_width;
920 s->avctx->width = s1->avctx->width;
921 s->avctx->height = s1->avctx->height;
923 s->coded_picture_number = s1->coded_picture_number;
924 s->picture_number = s1->picture_number;
926 av_assert0(!s->picture || s->picture != s1->picture);
928 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
929 ff_mpeg_unref_picture(s, &s->picture[i]);
930 if (s1->picture[i].f.buf[0] &&
931 (ret = ff_mpeg_ref_picture(s, &s->picture[i], &s1->picture[i])) < 0)
935 #define UPDATE_PICTURE(pic)\
937 ff_mpeg_unref_picture(s, &s->pic);\
938 if (s1->pic.f.buf[0])\
939 ret = ff_mpeg_ref_picture(s, &s->pic, &s1->pic);\
941 ret = update_picture_tables(&s->pic, &s1->pic);\
946 UPDATE_PICTURE(current_picture);
947 UPDATE_PICTURE(last_picture);
948 UPDATE_PICTURE(next_picture);
950 s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
951 s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
952 s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
954 // Error/bug resilience
955 s->next_p_frame_damaged = s1->next_p_frame_damaged;
956 s->workaround_bugs = s1->workaround_bugs;
957 s->padding_bug_score = s1->padding_bug_score;
960 memcpy(&s->last_time_base, &s1->last_time_base,
961 (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
962 (char *) &s1->last_time_base);
965 s->max_b_frames = s1->max_b_frames;
966 s->low_delay = s1->low_delay;
967 s->droppable = s1->droppable;
969 // DivX handling (doesn't work)
970 s->divx_packed = s1->divx_packed;
972 if (s1->bitstream_buffer) {
973 if (s1->bitstream_buffer_size +
974 FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
975 av_fast_malloc(&s->bitstream_buffer,
976 &s->allocated_bitstream_buffer_size,
977 s1->allocated_bitstream_buffer_size);
978 s->bitstream_buffer_size = s1->bitstream_buffer_size;
979 memcpy(s->bitstream_buffer, s1->bitstream_buffer,
980 s1->bitstream_buffer_size);
981 memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
982 FF_INPUT_BUFFER_PADDING_SIZE);
985 // linesize dependend scratch buffer allocation
986 if (!s->edge_emu_buffer)
988 if (frame_size_alloc(s, s1->linesize) < 0) {
989 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
990 "scratch buffers.\n");
991 return AVERROR(ENOMEM);
994 av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
995 "be allocated due to unknown size.\n");
998 // MPEG2/interlacing info
999 memcpy(&s->progressive_sequence, &s1->progressive_sequence,
1000 (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
1002 if (!s1->first_field) {
1003 s->last_pict_type = s1->pict_type;
1004 if (s1->current_picture_ptr)
1005 s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f.quality;
1012 * Set the given MpegEncContext to common defaults
1013 * (same for encoding and decoding).
1014 * The changed fields will not depend upon the
1015 * prior state of the MpegEncContext.
1017 void ff_MPV_common_defaults(MpegEncContext *s)
1019 s->y_dc_scale_table =
1020 s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
1021 s->chroma_qscale_table = ff_default_chroma_qscale_table;
1022 s->progressive_frame = 1;
1023 s->progressive_sequence = 1;
1024 s->picture_structure = PICT_FRAME;
1026 s->coded_picture_number = 0;
1027 s->picture_number = 0;
1032 s->slice_context_count = 1;
1036 * Set the given MpegEncContext to defaults for decoding.
1037 * the changed fields will not depend upon
1038 * the prior state of the MpegEncContext.
1040 void ff_MPV_decode_defaults(MpegEncContext *s)
1042 ff_MPV_common_defaults(s);
1045 static int init_er(MpegEncContext *s)
1047 ERContext *er = &s->er;
1048 int mb_array_size = s->mb_height * s->mb_stride;
1051 er->avctx = s->avctx;
1054 er->mb_index2xy = s->mb_index2xy;
1055 er->mb_num = s->mb_num;
1056 er->mb_width = s->mb_width;
1057 er->mb_height = s->mb_height;
1058 er->mb_stride = s->mb_stride;
1059 er->b8_stride = s->b8_stride;
1061 er->er_temp_buffer = av_malloc(s->mb_height * s->mb_stride);
1062 er->error_status_table = av_mallocz(mb_array_size);
1063 if (!er->er_temp_buffer || !er->error_status_table)
1066 er->mbskip_table = s->mbskip_table;
1067 er->mbintra_table = s->mbintra_table;
1069 for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++)
1070 er->dc_val[i] = s->dc_val[i];
1072 er->decode_mb = mpeg_er_decode_mb;
1077 av_freep(&er->er_temp_buffer);
1078 av_freep(&er->error_status_table);
1079 return AVERROR(ENOMEM);
1083 * Initialize and allocates MpegEncContext fields dependent on the resolution.
1085 static int init_context_frame(MpegEncContext *s)
1087 int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
1089 s->mb_width = (s->width + 15) / 16;
1090 s->mb_stride = s->mb_width + 1;
1091 s->b8_stride = s->mb_width * 2 + 1;
1092 s->b4_stride = s->mb_width * 4 + 1;
1093 mb_array_size = s->mb_height * s->mb_stride;
1094 mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
1096 /* set default edge pos, will be overriden
1097 * in decode_header if needed */
1098 s->h_edge_pos = s->mb_width * 16;
1099 s->v_edge_pos = s->mb_height * 16;
1101 s->mb_num = s->mb_width * s->mb_height;
1106 s->block_wrap[3] = s->b8_stride;
1108 s->block_wrap[5] = s->mb_stride;
1110 y_size = s->b8_stride * (2 * s->mb_height + 1);
1111 c_size = s->mb_stride * (s->mb_height + 1);
1112 yc_size = y_size + 2 * c_size;
1114 if (s->mb_height & 1)
1115 yc_size += 2*s->b8_stride + 2*s->mb_stride;
1117 FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int), fail); // error ressilience code looks cleaner with this
1118 for (y = 0; y < s->mb_height; y++)
1119 for (x = 0; x < s->mb_width; x++)
1120 s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
1122 s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
1125 /* Allocate MV tables */
1126 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
1127 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
1128 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
1129 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
1130 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
1131 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
1132 s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
1133 s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
1134 s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
1135 s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
1136 s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + s->mb_stride + 1;
1137 s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
1139 /* Allocate MB type table */
1140 FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size * sizeof(uint16_t), fail) // needed for encoding
1142 FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail)
1144 FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
1145 mb_array_size * sizeof(float), fail);
1146 FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
1147 mb_array_size * sizeof(float), fail);
1151 if (s->codec_id == AV_CODEC_ID_MPEG4 ||
1152 (s->flags & CODEC_FLAG_INTERLACED_ME)) {
1153 /* interlaced direct mode decoding tables */
1154 for (i = 0; i < 2; i++) {
1156 for (j = 0; j < 2; j++) {
1157 for (k = 0; k < 2; k++) {
1158 FF_ALLOCZ_OR_GOTO(s->avctx,
1159 s->b_field_mv_table_base[i][j][k],
1160 mv_table_size * 2 * sizeof(int16_t),
1162 s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
1165 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
1166 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
1167 s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
1169 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
1172 if (s->out_format == FMT_H263) {
1174 FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size + (s->mb_height&1)*2*s->b8_stride, fail);
1175 s->coded_block = s->coded_block_base + s->b8_stride + 1;
1177 /* cbp, ac_pred, pred_dir */
1178 FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table , mb_array_size * sizeof(uint8_t), fail);
1179 FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail);
1182 if (s->h263_pred || s->h263_plus || !s->encoding) {
1184 // MN: we need these for error resilience of intra-frames
1185 FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
1186 s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
1187 s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
1188 s->dc_val[2] = s->dc_val[1] + c_size;
1189 for (i = 0; i < yc_size; i++)
1190 s->dc_val_base[i] = 1024;
1193 /* which mb is a intra block */
1194 FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
1195 memset(s->mbintra_table, 1, mb_array_size);
1197 /* init macroblock skip table */
1198 FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
1199 // Note the + 1 is for a quicker mpeg4 slice_end detection
1203 return AVERROR(ENOMEM);
1207 * init common structure for both encoder and decoder.
1208 * this assumes that some variables like width/height are already set
1210 av_cold int ff_MPV_common_init(MpegEncContext *s)
1213 int nb_slices = (HAVE_THREADS &&
1214 s->avctx->active_thread_type & FF_THREAD_SLICE) ?
1215 s->avctx->thread_count : 1;
1217 if (s->encoding && s->avctx->slices)
1218 nb_slices = s->avctx->slices;
1220 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
1221 s->mb_height = (s->height + 31) / 32 * 2;
1223 s->mb_height = (s->height + 15) / 16;
1225 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
1226 av_log(s->avctx, AV_LOG_ERROR,
1227 "decoding to AV_PIX_FMT_NONE is not supported.\n");
1231 if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
1234 max_slices = FFMIN(MAX_THREADS, s->mb_height);
1236 max_slices = MAX_THREADS;
1237 av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
1238 " reducing to %d\n", nb_slices, max_slices);
1239 nb_slices = max_slices;
1242 if ((s->width || s->height) &&
1243 av_image_check_size(s->width, s->height, 0, s->avctx))
1246 ff_dct_common_init(s);
1248 s->flags = s->avctx->flags;
1249 s->flags2 = s->avctx->flags2;
1251 /* set chroma shifts */
1252 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,
1254 &s->chroma_y_shift);
1256 /* convert fourcc to upper case */
1257 s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
1259 s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag);
1261 FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
1262 MAX_PICTURE_COUNT * sizeof(Picture), fail);
1263 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1264 av_frame_unref(&s->picture[i].f);
1266 memset(&s->next_picture, 0, sizeof(s->next_picture));
1267 memset(&s->last_picture, 0, sizeof(s->last_picture));
1268 memset(&s->current_picture, 0, sizeof(s->current_picture));
1269 av_frame_unref(&s->next_picture.f);
1270 av_frame_unref(&s->last_picture.f);
1271 av_frame_unref(&s->current_picture.f);
1273 if (init_context_frame(s))
1276 s->parse_context.state = -1;
1278 s->context_initialized = 1;
1279 s->thread_context[0] = s;
1281 // if (s->width && s->height) {
1282 if (nb_slices > 1) {
1283 for (i = 1; i < nb_slices; i++) {
1284 s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
1285 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
1288 for (i = 0; i < nb_slices; i++) {
1289 if (init_duplicate_context(s->thread_context[i]) < 0)
1291 s->thread_context[i]->start_mb_y =
1292 (s->mb_height * (i) + nb_slices / 2) / nb_slices;
1293 s->thread_context[i]->end_mb_y =
1294 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
1297 if (init_duplicate_context(s) < 0)
1300 s->end_mb_y = s->mb_height;
1302 s->slice_context_count = nb_slices;
1307 ff_MPV_common_end(s);
1312 * Frees and resets MpegEncContext fields depending on the resolution.
1313 * Is used during resolution changes to avoid a full reinitialization of the
1316 static int free_context_frame(MpegEncContext *s)
1320 av_freep(&s->mb_type);
1321 av_freep(&s->p_mv_table_base);
1322 av_freep(&s->b_forw_mv_table_base);
1323 av_freep(&s->b_back_mv_table_base);
1324 av_freep(&s->b_bidir_forw_mv_table_base);
1325 av_freep(&s->b_bidir_back_mv_table_base);
1326 av_freep(&s->b_direct_mv_table_base);
1327 s->p_mv_table = NULL;
1328 s->b_forw_mv_table = NULL;
1329 s->b_back_mv_table = NULL;
1330 s->b_bidir_forw_mv_table = NULL;
1331 s->b_bidir_back_mv_table = NULL;
1332 s->b_direct_mv_table = NULL;
1333 for (i = 0; i < 2; i++) {
1334 for (j = 0; j < 2; j++) {
1335 for (k = 0; k < 2; k++) {
1336 av_freep(&s->b_field_mv_table_base[i][j][k]);
1337 s->b_field_mv_table[i][j][k] = NULL;
1339 av_freep(&s->b_field_select_table[i][j]);
1340 av_freep(&s->p_field_mv_table_base[i][j]);
1341 s->p_field_mv_table[i][j] = NULL;
1343 av_freep(&s->p_field_select_table[i]);
1346 av_freep(&s->dc_val_base);
1347 av_freep(&s->coded_block_base);
1348 av_freep(&s->mbintra_table);
1349 av_freep(&s->cbp_table);
1350 av_freep(&s->pred_dir_table);
1352 av_freep(&s->mbskip_table);
1354 av_freep(&s->er.error_status_table);
1355 av_freep(&s->er.er_temp_buffer);
1356 av_freep(&s->mb_index2xy);
1357 av_freep(&s->lambda_table);
1359 av_freep(&s->cplx_tab);
1360 av_freep(&s->bits_tab);
1362 s->linesize = s->uvlinesize = 0;
1367 int ff_MPV_common_frame_size_change(MpegEncContext *s)
1371 if (s->slice_context_count > 1) {
1372 for (i = 0; i < s->slice_context_count; i++) {
1373 free_duplicate_context(s->thread_context[i]);
1375 for (i = 1; i < s->slice_context_count; i++) {
1376 av_freep(&s->thread_context[i]);
1379 free_duplicate_context(s);
1381 if ((err = free_context_frame(s)) < 0)
1385 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1386 s->picture[i].needs_realloc = 1;
1389 s->last_picture_ptr =
1390 s->next_picture_ptr =
1391 s->current_picture_ptr = NULL;
1394 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
1395 s->mb_height = (s->height + 31) / 32 * 2;
1397 s->mb_height = (s->height + 15) / 16;
1399 if ((s->width || s->height) &&
1400 av_image_check_size(s->width, s->height, 0, s->avctx))
1401 return AVERROR_INVALIDDATA;
1403 if ((err = init_context_frame(s)))
1406 s->thread_context[0] = s;
1408 if (s->width && s->height) {
1409 int nb_slices = s->slice_context_count;
1410 if (nb_slices > 1) {
1411 for (i = 1; i < nb_slices; i++) {
1412 s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
1413 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
1416 for (i = 0; i < nb_slices; i++) {
1417 if (init_duplicate_context(s->thread_context[i]) < 0)
1419 s->thread_context[i]->start_mb_y =
1420 (s->mb_height * (i) + nb_slices / 2) / nb_slices;
1421 s->thread_context[i]->end_mb_y =
1422 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
1425 err = init_duplicate_context(s);
1429 s->end_mb_y = s->mb_height;
1431 s->slice_context_count = nb_slices;
1436 ff_MPV_common_end(s);
1440 /* init common structure for both encoder and decoder */
1441 void ff_MPV_common_end(MpegEncContext *s)
1445 if (s->slice_context_count > 1) {
1446 for (i = 0; i < s->slice_context_count; i++) {
1447 free_duplicate_context(s->thread_context[i]);
1449 for (i = 1; i < s->slice_context_count; i++) {
1450 av_freep(&s->thread_context[i]);
1452 s->slice_context_count = 1;
1453 } else free_duplicate_context(s);
1455 av_freep(&s->parse_context.buffer);
1456 s->parse_context.buffer_size = 0;
1458 av_freep(&s->bitstream_buffer);
1459 s->allocated_bitstream_buffer_size = 0;
1462 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1463 ff_free_picture_tables(&s->picture[i]);
1464 ff_mpeg_unref_picture(s, &s->picture[i]);
1467 av_freep(&s->picture);
1468 ff_free_picture_tables(&s->last_picture);
1469 ff_mpeg_unref_picture(s, &s->last_picture);
1470 ff_free_picture_tables(&s->current_picture);
1471 ff_mpeg_unref_picture(s, &s->current_picture);
1472 ff_free_picture_tables(&s->next_picture);
1473 ff_mpeg_unref_picture(s, &s->next_picture);
1475 free_context_frame(s);
1477 s->context_initialized = 0;
1478 s->last_picture_ptr =
1479 s->next_picture_ptr =
1480 s->current_picture_ptr = NULL;
1481 s->linesize = s->uvlinesize = 0;
1484 av_cold void ff_init_rl(RLTable *rl,
1485 uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
1487 int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
1488 uint8_t index_run[MAX_RUN + 1];
1489 int last, run, level, start, end, i;
1491 /* If table is static, we can quit if rl->max_level[0] is not NULL */
1492 if (static_store && rl->max_level[0])
1495 /* compute max_level[], max_run[] and index_run[] */
1496 for (last = 0; last < 2; last++) {
1505 memset(max_level, 0, MAX_RUN + 1);
1506 memset(max_run, 0, MAX_LEVEL + 1);
1507 memset(index_run, rl->n, MAX_RUN + 1);
1508 for (i = start; i < end; i++) {
1509 run = rl->table_run[i];
1510 level = rl->table_level[i];
1511 if (index_run[run] == rl->n)
1513 if (level > max_level[run])
1514 max_level[run] = level;
1515 if (run > max_run[level])
1516 max_run[level] = run;
1519 rl->max_level[last] = static_store[last];
1521 rl->max_level[last] = av_malloc(MAX_RUN + 1);
1522 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
1524 rl->max_run[last] = static_store[last] + MAX_RUN + 1;
1526 rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
1527 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
1529 rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
1531 rl->index_run[last] = av_malloc(MAX_RUN + 1);
1532 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
1536 av_cold void ff_init_vlc_rl(RLTable *rl)
1540 for (q = 0; q < 32; q++) {
1542 int qadd = (q - 1) | 1;
1548 for (i = 0; i < rl->vlc.table_size; i++) {
1549 int code = rl->vlc.table[i][0];
1550 int len = rl->vlc.table[i][1];
1553 if (len == 0) { // illegal code
1556 } else if (len < 0) { // more bits needed
1560 if (code == rl->n) { // esc
1564 run = rl->table_run[code] + 1;
1565 level = rl->table_level[code] * qmul + qadd;
1566 if (code >= rl->last) run += 192;
1569 rl->rl_vlc[q][i].len = len;
1570 rl->rl_vlc[q][i].level = level;
1571 rl->rl_vlc[q][i].run = run;
1576 static void release_unused_pictures(MpegEncContext *s)
1580 /* release non reference frames */
1581 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1582 if (!s->picture[i].reference)
1583 ff_mpeg_unref_picture(s, &s->picture[i]);
1587 static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
1589 if (pic == s->last_picture_ptr)
1591 if (pic->f.buf[0] == NULL)
1593 if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
1598 static int find_unused_picture(MpegEncContext *s, int shared)
1603 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1604 if (s->picture[i].f.buf[0] == NULL && &s->picture[i] != s->last_picture_ptr)
1608 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1609 if (pic_is_unused(s, &s->picture[i]))
1614 av_log(s->avctx, AV_LOG_FATAL,
1615 "Internal error, picture buffer overflow\n");
1616 /* We could return -1, but the codec would crash trying to draw into a
1617 * non-existing frame anyway. This is safer than waiting for a random crash.
1618 * Also the return of this is never useful, an encoder must only allocate
1619 * as much as allowed in the specification. This has no relationship to how
1620 * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
1621 * enough for such valid streams).
1622 * Plus, a decoder has to check stream validity and remove frames if too
1623 * many reference frames are around. Waiting for "OOM" is not correct at
1624 * all. Similarly, missing reference frames have to be replaced by
1625 * interpolated/MC frames, anything else is a bug in the codec ...
1631 int ff_find_unused_picture(MpegEncContext *s, int shared)
1633 int ret = find_unused_picture(s, shared);
1635 if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
1636 if (s->picture[ret].needs_realloc) {
1637 s->picture[ret].needs_realloc = 0;
1638 ff_free_picture_tables(&s->picture[ret]);
1639 ff_mpeg_unref_picture(s, &s->picture[ret]);
1646 * generic function called after decoding
1647 * the header and before a frame is decoded.
1649 int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
1655 if (!ff_thread_can_start_frame(avctx)) {
1656 av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
1660 /* mark & release old frames */
1661 if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1662 s->last_picture_ptr != s->next_picture_ptr &&
1663 s->last_picture_ptr->f.buf[0]) {
1664 ff_mpeg_unref_picture(s, s->last_picture_ptr);
1667 /* release forgotten pictures */
1668 /* if (mpeg124/h263) */
1669 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1670 if (&s->picture[i] != s->last_picture_ptr &&
1671 &s->picture[i] != s->next_picture_ptr &&
1672 s->picture[i].reference && !s->picture[i].needs_realloc) {
1673 if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1674 av_log(avctx, AV_LOG_ERROR,
1675 "releasing zombie picture\n");
1676 ff_mpeg_unref_picture(s, &s->picture[i]);
1680 ff_mpeg_unref_picture(s, &s->current_picture);
1682 release_unused_pictures(s);
1684 if (s->current_picture_ptr &&
1685 s->current_picture_ptr->f.buf[0] == NULL) {
1686 // we already have a unused image
1687 // (maybe it was set before reading the header)
1688 pic = s->current_picture_ptr;
1690 i = ff_find_unused_picture(s, 0);
1692 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1695 pic = &s->picture[i];
1699 if (!s->droppable) {
1700 if (s->pict_type != AV_PICTURE_TYPE_B)
1704 pic->f.coded_picture_number = s->coded_picture_number++;
1706 if (ff_alloc_picture(s, pic, 0) < 0)
1709 s->current_picture_ptr = pic;
1710 // FIXME use only the vars from current_pic
1711 s->current_picture_ptr->f.top_field_first = s->top_field_first;
1712 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
1713 s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1714 if (s->picture_structure != PICT_FRAME)
1715 s->current_picture_ptr->f.top_field_first =
1716 (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
1718 s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame &&
1719 !s->progressive_sequence;
1720 s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
1722 s->current_picture_ptr->f.pict_type = s->pict_type;
1723 // if (s->flags && CODEC_FLAG_QSCALE)
1724 // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
1725 s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1727 if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
1728 s->current_picture_ptr)) < 0)
1731 if (s->pict_type != AV_PICTURE_TYPE_B) {
1732 s->last_picture_ptr = s->next_picture_ptr;
1734 s->next_picture_ptr = s->current_picture_ptr;
1736 av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
1737 s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
1738 s->last_picture_ptr ? s->last_picture_ptr->f.data[0] : NULL,
1739 s->next_picture_ptr ? s->next_picture_ptr->f.data[0] : NULL,
1740 s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
1741 s->pict_type, s->droppable);
1743 if ((s->last_picture_ptr == NULL ||
1744 s->last_picture_ptr->f.buf[0] == NULL) &&
1745 (s->pict_type != AV_PICTURE_TYPE_I ||
1746 s->picture_structure != PICT_FRAME)) {
1747 int h_chroma_shift, v_chroma_shift;
1748 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
1749 &h_chroma_shift, &v_chroma_shift);
1750 if (s->pict_type == AV_PICTURE_TYPE_B && s->next_picture_ptr && s->next_picture_ptr->f.buf[0])
1751 av_log(avctx, AV_LOG_DEBUG,
1752 "allocating dummy last picture for B frame\n");
1753 else if (s->pict_type != AV_PICTURE_TYPE_I)
1754 av_log(avctx, AV_LOG_ERROR,
1755 "warning: first frame is no keyframe\n");
1756 else if (s->picture_structure != PICT_FRAME)
1757 av_log(avctx, AV_LOG_DEBUG,
1758 "allocate dummy last picture for field based first keyframe\n");
1760 /* Allocate a dummy frame */
1761 i = ff_find_unused_picture(s, 0);
1763 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1766 s->last_picture_ptr = &s->picture[i];
1768 s->last_picture_ptr->reference = 3;
1769 s->last_picture_ptr->f.key_frame = 0;
1770 s->last_picture_ptr->f.pict_type = AV_PICTURE_TYPE_P;
1772 if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
1773 s->last_picture_ptr = NULL;
1777 for(i=0; i<avctx->height; i++)
1778 memset(s->last_picture_ptr->f.data[0] + s->last_picture_ptr->f.linesize[0]*i,
1779 0x80, avctx->width);
1780 for(i=0; i<FF_CEIL_RSHIFT(avctx->height, v_chroma_shift); i++) {
1781 memset(s->last_picture_ptr->f.data[1] + s->last_picture_ptr->f.linesize[1]*i,
1782 0x80, FF_CEIL_RSHIFT(avctx->width, h_chroma_shift));
1783 memset(s->last_picture_ptr->f.data[2] + s->last_picture_ptr->f.linesize[2]*i,
1784 0x80, FF_CEIL_RSHIFT(avctx->width, h_chroma_shift));
1787 if(s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263){
1788 for(i=0; i<avctx->height; i++)
1789 memset(s->last_picture_ptr->f.data[0] + s->last_picture_ptr->f.linesize[0]*i, 16, avctx->width);
1792 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
1793 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
1795 if ((s->next_picture_ptr == NULL ||
1796 s->next_picture_ptr->f.buf[0] == NULL) &&
1797 s->pict_type == AV_PICTURE_TYPE_B) {
1798 /* Allocate a dummy frame */
1799 i = ff_find_unused_picture(s, 0);
1801 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1804 s->next_picture_ptr = &s->picture[i];
1806 s->next_picture_ptr->reference = 3;
1807 s->next_picture_ptr->f.key_frame = 0;
1808 s->next_picture_ptr->f.pict_type = AV_PICTURE_TYPE_P;
1810 if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
1811 s->next_picture_ptr = NULL;
1814 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
1815 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
1818 #if 0 // BUFREF-FIXME
1819 memset(s->last_picture.f.data, 0, sizeof(s->last_picture.f.data));
1820 memset(s->next_picture.f.data, 0, sizeof(s->next_picture.f.data));
1822 if (s->last_picture_ptr) {
1823 ff_mpeg_unref_picture(s, &s->last_picture);
1824 if (s->last_picture_ptr->f.buf[0] &&
1825 (ret = ff_mpeg_ref_picture(s, &s->last_picture,
1826 s->last_picture_ptr)) < 0)
1829 if (s->next_picture_ptr) {
1830 ff_mpeg_unref_picture(s, &s->next_picture);
1831 if (s->next_picture_ptr->f.buf[0] &&
1832 (ret = ff_mpeg_ref_picture(s, &s->next_picture,
1833 s->next_picture_ptr)) < 0)
1837 av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
1838 s->last_picture_ptr->f.buf[0]));
1840 if (s->picture_structure!= PICT_FRAME) {
1842 for (i = 0; i < 4; i++) {
1843 if (s->picture_structure == PICT_BOTTOM_FIELD) {
1844 s->current_picture.f.data[i] +=
1845 s->current_picture.f.linesize[i];
1847 s->current_picture.f.linesize[i] *= 2;
1848 s->last_picture.f.linesize[i] *= 2;
1849 s->next_picture.f.linesize[i] *= 2;
1853 s->err_recognition = avctx->err_recognition;
1855 /* set dequantizer, we can't do it during init as
1856 * it might change for mpeg4 and we can't do it in the header
1857 * decode as init is not called for mpeg4 there yet */
1858 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1859 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
1860 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
1861 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
1862 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
1863 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
1865 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
1866 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
1872 /* called after a frame has been decoded. */
1873 void ff_MPV_frame_end(MpegEncContext *s)
1877 if (s->current_picture.reference)
1878 ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
1882 * Draw a line from (ex, ey) -> (sx, sy).
1883 * @param w width of the image
1884 * @param h height of the image
1885 * @param stride stride/linesize of the image
1886 * @param color color of the arrow
1888 static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
1889 int w, int h, int stride, int color)
1893 sx = av_clip(sx, 0, w - 1);
1894 sy = av_clip(sy, 0, h - 1);
1895 ex = av_clip(ex, 0, w - 1);
1896 ey = av_clip(ey, 0, h - 1);
1898 buf[sy * stride + sx] += color;
1900 if (FFABS(ex - sx) > FFABS(ey - sy)) {
1902 FFSWAP(int, sx, ex);
1903 FFSWAP(int, sy, ey);
1905 buf += sx + sy * stride;
1907 f = ((ey - sy) << 16) / ex;
1908 for (x = 0; x <= ex; x++) {
1910 fr = (x * f) & 0xFFFF;
1911 buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
1912 if(fr) buf[(y + 1) * stride + x] += (color * fr ) >> 16;
1916 FFSWAP(int, sx, ex);
1917 FFSWAP(int, sy, ey);
1919 buf += sx + sy * stride;
1922 f = ((ex - sx) << 16) / ey;
1925 for(y= 0; y <= ey; y++){
1927 fr = (y*f) & 0xFFFF;
1928 buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
1929 if(fr) buf[y * stride + x + 1] += (color * fr ) >> 16;
1935 * Draw an arrow from (ex, ey) -> (sx, sy).
1936 * @param w width of the image
1937 * @param h height of the image
1938 * @param stride stride/linesize of the image
1939 * @param color color of the arrow
1941 static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
1942 int ey, int w, int h, int stride, int color)
1946 sx = av_clip(sx, -100, w + 100);
1947 sy = av_clip(sy, -100, h + 100);
1948 ex = av_clip(ex, -100, w + 100);
1949 ey = av_clip(ey, -100, h + 100);
1954 if (dx * dx + dy * dy > 3 * 3) {
1957 int length = ff_sqrt((rx * rx + ry * ry) << 8);
1959 // FIXME subpixel accuracy
1960 rx = ROUNDED_DIV(rx * 3 << 4, length);
1961 ry = ROUNDED_DIV(ry * 3 << 4, length);
1963 draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
1964 draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
1966 draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
1970 * Print debugging info for the given picture.
1972 void ff_print_debug_info2(AVCodecContext *avctx, Picture *p, AVFrame *pict, uint8_t *mbskip_table,
1974 int mb_width, int mb_height, int mb_stride, int quarter_sample)
1976 if (avctx->hwaccel || !p || !p->mb_type
1977 || (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU))
1981 if (avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
1984 av_log(avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
1985 av_get_picture_type_char(pict->pict_type));
1986 for (y = 0; y < mb_height; y++) {
1987 for (x = 0; x < mb_width; x++) {
1988 if (avctx->debug & FF_DEBUG_SKIP) {
1989 int count = mbskip_table[x + y * mb_stride];
1992 av_log(avctx, AV_LOG_DEBUG, "%1d", count);
1994 if (avctx->debug & FF_DEBUG_QP) {
1995 av_log(avctx, AV_LOG_DEBUG, "%2d",
1996 p->qscale_table[x + y * mb_stride]);
1998 if (avctx->debug & FF_DEBUG_MB_TYPE) {
1999 int mb_type = p->mb_type[x + y * mb_stride];
2000 // Type & MV direction
2001 if (IS_PCM(mb_type))
2002 av_log(avctx, AV_LOG_DEBUG, "P");
2003 else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
2004 av_log(avctx, AV_LOG_DEBUG, "A");
2005 else if (IS_INTRA4x4(mb_type))
2006 av_log(avctx, AV_LOG_DEBUG, "i");
2007 else if (IS_INTRA16x16(mb_type))
2008 av_log(avctx, AV_LOG_DEBUG, "I");
2009 else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
2010 av_log(avctx, AV_LOG_DEBUG, "d");
2011 else if (IS_DIRECT(mb_type))
2012 av_log(avctx, AV_LOG_DEBUG, "D");
2013 else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
2014 av_log(avctx, AV_LOG_DEBUG, "g");
2015 else if (IS_GMC(mb_type))
2016 av_log(avctx, AV_LOG_DEBUG, "G");
2017 else if (IS_SKIP(mb_type))
2018 av_log(avctx, AV_LOG_DEBUG, "S");
2019 else if (!USES_LIST(mb_type, 1))
2020 av_log(avctx, AV_LOG_DEBUG, ">");
2021 else if (!USES_LIST(mb_type, 0))
2022 av_log(avctx, AV_LOG_DEBUG, "<");
2024 av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
2025 av_log(avctx, AV_LOG_DEBUG, "X");
2029 if (IS_8X8(mb_type))
2030 av_log(avctx, AV_LOG_DEBUG, "+");
2031 else if (IS_16X8(mb_type))
2032 av_log(avctx, AV_LOG_DEBUG, "-");
2033 else if (IS_8X16(mb_type))
2034 av_log(avctx, AV_LOG_DEBUG, "|");
2035 else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
2036 av_log(avctx, AV_LOG_DEBUG, " ");
2038 av_log(avctx, AV_LOG_DEBUG, "?");
2041 if (IS_INTERLACED(mb_type))
2042 av_log(avctx, AV_LOG_DEBUG, "=");
2044 av_log(avctx, AV_LOG_DEBUG, " ");
2047 av_log(avctx, AV_LOG_DEBUG, "\n");
2051 if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
2052 (avctx->debug_mv)) {
2053 const int shift = 1 + quarter_sample;
2057 int h_chroma_shift, v_chroma_shift, block_height;
2058 const int width = avctx->width;
2059 const int height = avctx->height;
2060 const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
2061 const int mv_stride = (mb_width << mv_sample_log2) +
2062 (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
2064 *low_delay = 0; // needed to see the vectors without trashing the buffers
2066 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
2068 av_frame_make_writable(pict);
2070 pict->opaque = NULL;
2071 ptr = pict->data[0];
2072 block_height = 16 >> v_chroma_shift;
2074 for (mb_y = 0; mb_y < mb_height; mb_y++) {
2076 for (mb_x = 0; mb_x < mb_width; mb_x++) {
2077 const int mb_index = mb_x + mb_y * mb_stride;
2078 if ((avctx->debug_mv) && p->motion_val[0]) {
2080 for (type = 0; type < 3; type++) {
2084 if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_P_FOR)) ||
2085 (pict->pict_type!= AV_PICTURE_TYPE_P))
2090 if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_FOR)) ||
2091 (pict->pict_type!= AV_PICTURE_TYPE_B))
2096 if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_BACK)) ||
2097 (pict->pict_type!= AV_PICTURE_TYPE_B))
2102 if (!USES_LIST(p->mb_type[mb_index], direction))
2105 if (IS_8X8(p->mb_type[mb_index])) {
2107 for (i = 0; i < 4; i++) {
2108 int sx = mb_x * 16 + 4 + 8 * (i & 1);
2109 int sy = mb_y * 16 + 4 + 8 * (i >> 1);
2110 int xy = (mb_x * 2 + (i & 1) +
2111 (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
2112 int mx = (p->motion_val[direction][xy][0] >> shift) + sx;
2113 int my = (p->motion_val[direction][xy][1] >> shift) + sy;
2114 draw_arrow(ptr, sx, sy, mx, my, width,
2115 height, pict->linesize[0], 100);
2117 } else if (IS_16X8(p->mb_type[mb_index])) {
2119 for (i = 0; i < 2; i++) {
2120 int sx = mb_x * 16 + 8;
2121 int sy = mb_y * 16 + 4 + 8 * i;
2122 int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
2123 int mx = (p->motion_val[direction][xy][0] >> shift);
2124 int my = (p->motion_val[direction][xy][1] >> shift);
2126 if (IS_INTERLACED(p->mb_type[mb_index]))
2129 draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
2130 height, pict->linesize[0], 100);
2132 } else if (IS_8X16(p->mb_type[mb_index])) {
2134 for (i = 0; i < 2; i++) {
2135 int sx = mb_x * 16 + 4 + 8 * i;
2136 int sy = mb_y * 16 + 8;
2137 int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
2138 int mx = p->motion_val[direction][xy][0] >> shift;
2139 int my = p->motion_val[direction][xy][1] >> shift;
2141 if (IS_INTERLACED(p->mb_type[mb_index]))
2144 draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
2145 height, pict->linesize[0], 100);
2148 int sx= mb_x * 16 + 8;
2149 int sy= mb_y * 16 + 8;
2150 int xy= (mb_x + mb_y * mv_stride) << mv_sample_log2;
2151 int mx= (p->motion_val[direction][xy][0]>>shift) + sx;
2152 int my= (p->motion_val[direction][xy][1]>>shift) + sy;
2153 draw_arrow(ptr, sx, sy, mx, my, width, height, pict->linesize[0], 100);
2157 if ((avctx->debug & FF_DEBUG_VIS_QP)) {
2158 uint64_t c = (p->qscale_table[mb_index] * 128 / 31) *
2159 0x0101010101010101ULL;
2161 for (y = 0; y < block_height; y++) {
2162 *(uint64_t *)(pict->data[1] + 8 * mb_x +
2163 (block_height * mb_y + y) *
2164 pict->linesize[1]) = c;
2165 *(uint64_t *)(pict->data[2] + 8 * mb_x +
2166 (block_height * mb_y + y) *
2167 pict->linesize[2]) = c;
2170 if ((avctx->debug & FF_DEBUG_VIS_MB_TYPE) &&
2172 int mb_type = p->mb_type[mb_index];
2175 #define COLOR(theta, r) \
2176 u = (int)(128 + r * cos(theta * 3.141592 / 180)); \
2177 v = (int)(128 + r * sin(theta * 3.141592 / 180));
2181 if (IS_PCM(mb_type)) {
2183 } else if ((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) ||
2184 IS_INTRA16x16(mb_type)) {
2186 } else if (IS_INTRA4x4(mb_type)) {
2188 } else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) {
2190 } else if (IS_DIRECT(mb_type)) {
2192 } else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) {
2194 } else if (IS_GMC(mb_type)) {
2196 } else if (IS_SKIP(mb_type)) {
2198 } else if (!USES_LIST(mb_type, 1)) {
2200 } else if (!USES_LIST(mb_type, 0)) {
2203 av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
2207 u *= 0x0101010101010101ULL;
2208 v *= 0x0101010101010101ULL;
2209 for (y = 0; y < block_height; y++) {
2210 *(uint64_t *)(pict->data[1] + 8 * mb_x +
2211 (block_height * mb_y + y) * pict->linesize[1]) = u;
2212 *(uint64_t *)(pict->data[2] + 8 * mb_x +
2213 (block_height * mb_y + y) * pict->linesize[2]) = v;
2217 if (IS_8X8(mb_type) || IS_16X8(mb_type)) {
2218 *(uint64_t *)(pict->data[0] + 16 * mb_x + 0 +
2219 (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
2220 *(uint64_t *)(pict->data[0] + 16 * mb_x + 8 +
2221 (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
2223 if (IS_8X8(mb_type) || IS_8X16(mb_type)) {
2224 for (y = 0; y < 16; y++)
2225 pict->data[0][16 * mb_x + 8 + (16 * mb_y + y) *
2226 pict->linesize[0]] ^= 0x80;
2228 if (IS_8X8(mb_type) && mv_sample_log2 >= 2) {
2229 int dm = 1 << (mv_sample_log2 - 2);
2230 for (i = 0; i < 4; i++) {
2231 int sx = mb_x * 16 + 8 * (i & 1);
2232 int sy = mb_y * 16 + 8 * (i >> 1);
2233 int xy = (mb_x * 2 + (i & 1) +
2234 (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
2236 int32_t *mv = (int32_t *) &p->motion_val[0][xy];
2237 if (mv[0] != mv[dm] ||
2238 mv[dm * mv_stride] != mv[dm * (mv_stride + 1)])
2239 for (y = 0; y < 8; y++)
2240 pict->data[0][sx + 4 + (sy + y) * pict->linesize[0]] ^= 0x80;
2241 if (mv[0] != mv[dm * mv_stride] || mv[dm] != mv[dm * (mv_stride + 1)])
2242 *(uint64_t *)(pict->data[0] + sx + (sy + 4) *
2243 pict->linesize[0]) ^= 0x8080808080808080ULL;
2247 if (IS_INTERLACED(mb_type) &&
2248 avctx->codec->id == AV_CODEC_ID_H264) {
2252 mbskip_table[mb_index] = 0;
2258 void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
2260 ff_print_debug_info2(s->avctx, p, pict, s->mbskip_table, &s->low_delay,
2261 s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
2264 int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
2266 AVBufferRef *ref = av_buffer_ref(p->qscale_table_buf);
2267 int offset = 2*s->mb_stride + 1;
2269 return AVERROR(ENOMEM);
2270 av_assert0(ref->size >= offset + s->mb_stride * ((f->height+15)/16));
2271 ref->size -= offset;
2272 ref->data += offset;
2273 return av_frame_set_qp_table(f, ref, s->mb_stride, qp_type);
2276 static inline int hpel_motion_lowres(MpegEncContext *s,
2277 uint8_t *dest, uint8_t *src,
2278 int field_based, int field_select,
2279 int src_x, int src_y,
2280 int width, int height, ptrdiff_t stride,
2281 int h_edge_pos, int v_edge_pos,
2282 int w, int h, h264_chroma_mc_func *pix_op,
2283 int motion_x, int motion_y)
2285 const int lowres = s->avctx->lowres;
2286 const int op_index = FFMIN(lowres, 3);
2287 const int s_mask = (2 << lowres) - 1;
2291 if (s->quarter_sample) {
2296 sx = motion_x & s_mask;
2297 sy = motion_y & s_mask;
2298 src_x += motion_x >> lowres + 1;
2299 src_y += motion_y >> lowres + 1;
2301 src += src_y * stride + src_x;
2303 if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
2304 (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
2305 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
2306 s->linesize, s->linesize,
2307 w + 1, (h + 1) << field_based,
2308 src_x, src_y << field_based,
2309 h_edge_pos, v_edge_pos);
2310 src = s->edge_emu_buffer;
2314 sx = (sx << 2) >> lowres;
2315 sy = (sy << 2) >> lowres;
2318 pix_op[op_index](dest, src, stride, h, sx, sy);
2322 /* apply one mpeg motion vector to the three components */
2323 static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
2330 uint8_t **ref_picture,
2331 h264_chroma_mc_func *pix_op,
2332 int motion_x, int motion_y,
2335 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2336 int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
2337 ptrdiff_t uvlinesize, linesize;
2338 const int lowres = s->avctx->lowres;
2339 const int op_index = FFMIN(lowres-1+s->chroma_x_shift, 3);
2340 const int block_s = 8>>lowres;
2341 const int s_mask = (2 << lowres) - 1;
2342 const int h_edge_pos = s->h_edge_pos >> lowres;
2343 const int v_edge_pos = s->v_edge_pos >> lowres;
2344 linesize = s->current_picture.f.linesize[0] << field_based;
2345 uvlinesize = s->current_picture.f.linesize[1] << field_based;
2347 // FIXME obviously not perfect but qpel will not work in lowres anyway
2348 if (s->quarter_sample) {
2354 motion_y += (bottom_field - field_select)*((1 << lowres)-1);
2357 sx = motion_x & s_mask;
2358 sy = motion_y & s_mask;
2359 src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
2360 src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
2362 if (s->out_format == FMT_H263) {
2363 uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
2364 uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
2365 uvsrc_x = src_x >> 1;
2366 uvsrc_y = src_y >> 1;
2367 } else if (s->out_format == FMT_H261) {
2368 // even chroma mv's are full pel in H261
2371 uvsx = (2 * mx) & s_mask;
2372 uvsy = (2 * my) & s_mask;
2373 uvsrc_x = s->mb_x * block_s + (mx >> lowres);
2374 uvsrc_y = mb_y * block_s + (my >> lowres);
2376 if(s->chroma_y_shift){
2381 uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
2382 uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
2384 if(s->chroma_x_shift){
2388 uvsy = motion_y & s_mask;
2390 uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
2393 uvsx = motion_x & s_mask;
2394 uvsy = motion_y & s_mask;
2401 ptr_y = ref_picture[0] + src_y * linesize + src_x;
2402 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
2403 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
2405 if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) || uvsrc_y<0 ||
2406 (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
2407 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
2408 linesize >> field_based, linesize >> field_based,
2409 17, 17 + field_based,
2410 src_x, src_y << field_based, h_edge_pos,
2412 ptr_y = s->edge_emu_buffer;
2413 if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
2414 uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;
2415 s->vdsp.emulated_edge_mc(uvbuf, ptr_cb,
2416 uvlinesize >> field_based, uvlinesize >> field_based,
2418 uvsrc_x, uvsrc_y << field_based,
2419 h_edge_pos >> 1, v_edge_pos >> 1);
2420 s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr,
2421 uvlinesize >> field_based,uvlinesize >> field_based,
2423 uvsrc_x, uvsrc_y << field_based,
2424 h_edge_pos >> 1, v_edge_pos >> 1);
2426 ptr_cr = uvbuf + 16;
2430 // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f.data
2432 dest_y += s->linesize;
2433 dest_cb += s->uvlinesize;
2434 dest_cr += s->uvlinesize;
2438 ptr_y += s->linesize;
2439 ptr_cb += s->uvlinesize;
2440 ptr_cr += s->uvlinesize;
2443 sx = (sx << 2) >> lowres;
2444 sy = (sy << 2) >> lowres;
2445 pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
2447 if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
2448 int hc = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h;
2449 uvsx = (uvsx << 2) >> lowres;
2450 uvsy = (uvsy << 2) >> lowres;
2452 pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy);
2453 pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy);
2456 // FIXME h261 lowres loop filter
2459 static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
2460 uint8_t *dest_cb, uint8_t *dest_cr,
2461 uint8_t **ref_picture,
2462 h264_chroma_mc_func * pix_op,
2465 const int lowres = s->avctx->lowres;
2466 const int op_index = FFMIN(lowres, 3);
2467 const int block_s = 8 >> lowres;
2468 const int s_mask = (2 << lowres) - 1;
2469 const int h_edge_pos = s->h_edge_pos >> lowres + 1;
2470 const int v_edge_pos = s->v_edge_pos >> lowres + 1;
2471 int emu = 0, src_x, src_y, sx, sy;
2475 if (s->quarter_sample) {
2480 /* In case of 8X8, we construct a single chroma motion vector
2481 with a special rounding */
2482 mx = ff_h263_round_chroma(mx);
2483 my = ff_h263_round_chroma(my);
2487 src_x = s->mb_x * block_s + (mx >> lowres + 1);
2488 src_y = s->mb_y * block_s + (my >> lowres + 1);
2490 offset = src_y * s->uvlinesize + src_x;
2491 ptr = ref_picture[1] + offset;
2492 if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
2493 (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
2494 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
2495 s->uvlinesize, s->uvlinesize,
2497 src_x, src_y, h_edge_pos, v_edge_pos);
2498 ptr = s->edge_emu_buffer;
2501 sx = (sx << 2) >> lowres;
2502 sy = (sy << 2) >> lowres;
2503 pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
2505 ptr = ref_picture[2] + offset;
2507 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
2508 s->uvlinesize, s->uvlinesize,
2510 src_x, src_y, h_edge_pos, v_edge_pos);
2511 ptr = s->edge_emu_buffer;
2513 pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
2517 * motion compensation of a single macroblock
2519 * @param dest_y luma destination pointer
2520 * @param dest_cb chroma cb/u destination pointer
2521 * @param dest_cr chroma cr/v destination pointer
2522 * @param dir direction (0->forward, 1->backward)
2523 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
2524 * @param pix_op halfpel motion compensation function (average or put normally)
2525 * the motion vectors are taken from s->mv and the MV type from s->mv_type
2527 static inline void MPV_motion_lowres(MpegEncContext *s,
2528 uint8_t *dest_y, uint8_t *dest_cb,
2530 int dir, uint8_t **ref_picture,
2531 h264_chroma_mc_func *pix_op)
2535 const int lowres = s->avctx->lowres;
2536 const int block_s = 8 >>lowres;
2541 switch (s->mv_type) {
2543 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2545 ref_picture, pix_op,
2546 s->mv[dir][0][0], s->mv[dir][0][1],
2552 for (i = 0; i < 4; i++) {
2553 hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
2554 s->linesize) * block_s,
2555 ref_picture[0], 0, 0,
2556 (2 * mb_x + (i & 1)) * block_s,
2557 (2 * mb_y + (i >> 1)) * block_s,
2558 s->width, s->height, s->linesize,
2559 s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
2560 block_s, block_s, pix_op,
2561 s->mv[dir][i][0], s->mv[dir][i][1]);
2563 mx += s->mv[dir][i][0];
2564 my += s->mv[dir][i][1];
2567 if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY))
2568 chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
2572 if (s->picture_structure == PICT_FRAME) {
2574 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2575 1, 0, s->field_select[dir][0],
2576 ref_picture, pix_op,
2577 s->mv[dir][0][0], s->mv[dir][0][1],
2580 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2581 1, 1, s->field_select[dir][1],
2582 ref_picture, pix_op,
2583 s->mv[dir][1][0], s->mv[dir][1][1],
2586 if (s->picture_structure != s->field_select[dir][0] + 1 &&
2587 s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
2588 ref_picture = s->current_picture_ptr->f.data;
2591 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2592 0, 0, s->field_select[dir][0],
2593 ref_picture, pix_op,
2595 s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
2599 for (i = 0; i < 2; i++) {
2600 uint8_t **ref2picture;
2602 if (s->picture_structure == s->field_select[dir][i] + 1 ||
2603 s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
2604 ref2picture = ref_picture;
2606 ref2picture = s->current_picture_ptr->f.data;
2609 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2610 0, 0, s->field_select[dir][i],
2611 ref2picture, pix_op,
2612 s->mv[dir][i][0], s->mv[dir][i][1] +
2613 2 * block_s * i, block_s, mb_y >> 1);
2615 dest_y += 2 * block_s * s->linesize;
2616 dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
2617 dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
2621 if (s->picture_structure == PICT_FRAME) {
2622 for (i = 0; i < 2; i++) {
2624 for (j = 0; j < 2; j++) {
2625 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2627 ref_picture, pix_op,
2628 s->mv[dir][2 * i + j][0],
2629 s->mv[dir][2 * i + j][1],
2632 pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
2635 for (i = 0; i < 2; i++) {
2636 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2637 0, 0, s->picture_structure != i + 1,
2638 ref_picture, pix_op,
2639 s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
2640 2 * block_s, mb_y >> 1);
2642 // after put we make avg of the same block
2643 pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
2645 // opposite parity is always in the same
2646 // frame if this is second field
2647 if (!s->first_field) {
2648 ref_picture = s->current_picture_ptr->f.data;
2659 * find the lowest MB row referenced in the MVs
2661 int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
2663 int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
2664 int my, off, i, mvs;
2666 if (s->picture_structure != PICT_FRAME || s->mcsel)
2669 switch (s->mv_type) {
2683 for (i = 0; i < mvs; i++) {
2684 my = s->mv[dir][i][1]<<qpel_shift;
2685 my_max = FFMAX(my_max, my);
2686 my_min = FFMIN(my_min, my);
2689 off = (FFMAX(-my_min, my_max) + 63) >> 6;
2691 return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
2693 return s->mb_height-1;
2696 /* put block[] to dest[] */
2697 static inline void put_dct(MpegEncContext *s,
2698 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
2700 s->dct_unquantize_intra(s, block, i, qscale);
2701 s->dsp.idct_put (dest, line_size, block);
2704 /* add block[] to dest[] */
2705 static inline void add_dct(MpegEncContext *s,
2706 int16_t *block, int i, uint8_t *dest, int line_size)
2708 if (s->block_last_index[i] >= 0) {
2709 s->dsp.idct_add (dest, line_size, block);
2713 static inline void add_dequant_dct(MpegEncContext *s,
2714 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
2716 if (s->block_last_index[i] >= 0) {
2717 s->dct_unquantize_inter(s, block, i, qscale);
2719 s->dsp.idct_add (dest, line_size, block);
2724 * Clean dc, ac, coded_block for the current non-intra MB.
2726 void ff_clean_intra_table_entries(MpegEncContext *s)
2728 int wrap = s->b8_stride;
2729 int xy = s->block_index[0];
2732 s->dc_val[0][xy + 1 ] =
2733 s->dc_val[0][xy + wrap] =
2734 s->dc_val[0][xy + 1 + wrap] = 1024;
2736 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
2737 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
2738 if (s->msmpeg4_version>=3) {
2739 s->coded_block[xy ] =
2740 s->coded_block[xy + 1 ] =
2741 s->coded_block[xy + wrap] =
2742 s->coded_block[xy + 1 + wrap] = 0;
2745 wrap = s->mb_stride;
2746 xy = s->mb_x + s->mb_y * wrap;
2748 s->dc_val[2][xy] = 1024;
2750 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
2751 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
2753 s->mbintra_table[xy]= 0;
2756 /* generic function called after a macroblock has been parsed by the
2757 decoder or after it has been encoded by the encoder.
2759 Important variables used:
2760 s->mb_intra : true if intra macroblock
2761 s->mv_dir : motion vector direction
2762 s->mv_type : motion vector type
2763 s->mv : motion vector
2764 s->interlaced_dct : true if interlaced dct used (mpeg2)
2766 static av_always_inline
2767 void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
2768 int lowres_flag, int is_mpeg12)
2770 const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
2773 s->avctx->hwaccel && s->avctx->hwaccel->decode_mb) {
2774 s->avctx->hwaccel->decode_mb(s);//xvmc uses pblocks
2778 if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
2779 /* print DCT coefficients */
2781 av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
2783 for(j=0; j<64; j++){
2784 av_log(s->avctx, AV_LOG_DEBUG, "%5d", block[i][s->dsp.idct_permutation[j]]);
2786 av_log(s->avctx, AV_LOG_DEBUG, "\n");
2790 s->current_picture.qscale_table[mb_xy] = s->qscale;
2792 /* update DC predictors for P macroblocks */
2794 if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
2795 if(s->mbintra_table[mb_xy])
2796 ff_clean_intra_table_entries(s);
2800 s->last_dc[2] = 128 << s->intra_dc_precision;
2803 else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
2804 s->mbintra_table[mb_xy]=1;
2806 if ( (s->flags&CODEC_FLAG_PSNR)
2807 || s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor
2808 || !(s->encoding && (s->intra_only || s->pict_type==AV_PICTURE_TYPE_B) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc
2809 uint8_t *dest_y, *dest_cb, *dest_cr;
2810 int dct_linesize, dct_offset;
2811 op_pixels_func (*op_pix)[4];
2812 qpel_mc_func (*op_qpix)[16];
2813 const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
2814 const int uvlinesize = s->current_picture.f.linesize[1];
2815 const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
2816 const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
2818 /* avoid copy if macroblock skipped in last frame too */
2819 /* skip only during decoding as we might trash the buffers during encoding a bit */
2821 uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
2823 if (s->mb_skipped) {
2825 av_assert2(s->pict_type!=AV_PICTURE_TYPE_I);
2827 } else if(!s->current_picture.reference) {
2830 *mbskip_ptr = 0; /* not skipped */
2834 dct_linesize = linesize << s->interlaced_dct;
2835 dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
2839 dest_cb= s->dest[1];
2840 dest_cr= s->dest[2];
2842 dest_y = s->b_scratchpad;
2843 dest_cb= s->b_scratchpad+16*linesize;
2844 dest_cr= s->b_scratchpad+32*linesize;
2848 /* motion handling */
2849 /* decoding or more than one mb_type (MC was already done otherwise) */
2852 if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
2853 if (s->mv_dir & MV_DIR_FORWARD) {
2854 ff_thread_await_progress(&s->last_picture_ptr->tf,
2855 ff_MPV_lowest_referenced_row(s, 0),
2858 if (s->mv_dir & MV_DIR_BACKWARD) {
2859 ff_thread_await_progress(&s->next_picture_ptr->tf,
2860 ff_MPV_lowest_referenced_row(s, 1),
2866 h264_chroma_mc_func *op_pix = s->h264chroma.put_h264_chroma_pixels_tab;
2868 if (s->mv_dir & MV_DIR_FORWARD) {
2869 MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix);
2870 op_pix = s->h264chroma.avg_h264_chroma_pixels_tab;
2872 if (s->mv_dir & MV_DIR_BACKWARD) {
2873 MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix);
2876 op_qpix = s->me.qpel_put;
2877 if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
2878 op_pix = s->hdsp.put_pixels_tab;
2880 op_pix = s->hdsp.put_no_rnd_pixels_tab;
2882 if (s->mv_dir & MV_DIR_FORWARD) {
2883 ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
2884 op_pix = s->hdsp.avg_pixels_tab;
2885 op_qpix= s->me.qpel_avg;
2887 if (s->mv_dir & MV_DIR_BACKWARD) {
2888 ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
2893 /* skip dequant / idct if we are really late ;) */
2894 if(s->avctx->skip_idct){
2895 if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
2896 ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
2897 || s->avctx->skip_idct >= AVDISCARD_ALL)
2901 /* add dct residue */
2902 if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
2903 || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
2904 add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
2905 add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
2906 add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
2907 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
2909 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2910 if (s->chroma_y_shift){
2911 add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
2912 add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
2916 add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
2917 add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
2918 add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
2919 add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
2922 } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
2923 add_dct(s, block[0], 0, dest_y , dct_linesize);
2924 add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
2925 add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
2926 add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
2928 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2929 if(s->chroma_y_shift){//Chroma420
2930 add_dct(s, block[4], 4, dest_cb, uvlinesize);
2931 add_dct(s, block[5], 5, dest_cr, uvlinesize);
2934 dct_linesize = uvlinesize << s->interlaced_dct;
2935 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
2937 add_dct(s, block[4], 4, dest_cb, dct_linesize);
2938 add_dct(s, block[5], 5, dest_cr, dct_linesize);
2939 add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
2940 add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
2941 if(!s->chroma_x_shift){//Chroma444
2942 add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
2943 add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
2944 add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
2945 add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
2950 else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
2951 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
2954 /* dct only in intra block */
2955 if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
2956 put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
2957 put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
2958 put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
2959 put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
2961 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2962 if(s->chroma_y_shift){
2963 put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
2964 put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
2968 put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
2969 put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
2970 put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
2971 put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
2975 s->dsp.idct_put(dest_y , dct_linesize, block[0]);
2976 s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
2977 s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
2978 s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
2980 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2981 if(s->chroma_y_shift){
2982 s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
2983 s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
2986 dct_linesize = uvlinesize << s->interlaced_dct;
2987 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
2989 s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
2990 s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
2991 s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
2992 s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
2993 if(!s->chroma_x_shift){//Chroma444
2994 s->dsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
2995 s->dsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
2996 s->dsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
2997 s->dsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
3005 s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
3006 s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
3007 s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
3012 void ff_MPV_decode_mb(MpegEncContext *s, int16_t block[12][64]){
3014 if(s->out_format == FMT_MPEG1) {
3015 if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
3016 else MPV_decode_mb_internal(s, block, 0, 1);
3019 if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
3020 else MPV_decode_mb_internal(s, block, 0, 0);
3024 * @param h is the normal height, this will be reduced automatically if needed for the last row
3026 void ff_draw_horiz_band(AVCodecContext *avctx, Picture *cur,
3027 Picture *last, int y, int h, int picture_structure,
3028 int first_field, int low_delay)
3030 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
3031 int vshift = desc->log2_chroma_h;
3032 const int field_pic = picture_structure != PICT_FRAME;
3038 h = FFMIN(h, avctx->height - y);
3040 if(field_pic && first_field && !(avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
3042 if (avctx->draw_horiz_band) {
3044 int offset[AV_NUM_DATA_POINTERS];
3047 if(cur->f.pict_type == AV_PICTURE_TYPE_B || low_delay ||
3048 (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
3055 if (cur->f.pict_type == AV_PICTURE_TYPE_B &&
3056 picture_structure == PICT_FRAME &&
3057 avctx->codec_id != AV_CODEC_ID_SVQ3) {
3058 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
3061 offset[0]= y * src->linesize[0];
3063 offset[2]= (y >> vshift) * src->linesize[1];
3064 for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
3070 avctx->draw_horiz_band(avctx, src, offset,
3071 y, picture_structure, h);
3075 void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
3077 ff_draw_horiz_band(s->avctx, s->current_picture_ptr,
3078 s->last_picture_ptr, y, h, s->picture_structure,
3079 s->first_field, s->low_delay);
3082 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
3083 const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
3084 const int uvlinesize = s->current_picture.f.linesize[1];
3085 const int mb_size= 4 - s->avctx->lowres;
3087 s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
3088 s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
3089 s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
3090 s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
3091 s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
3092 s->block_index[5]= s->mb_stride*(s->mb_y + s->mb_height + 2) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
3093 //block_index is not used by mpeg2, so it is not affected by chroma_format
3095 s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) << mb_size);
3096 s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
3097 s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
3099 if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
3101 if(s->picture_structure==PICT_FRAME){
3102 s->dest[0] += s->mb_y * linesize << mb_size;
3103 s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
3104 s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
3106 s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
3107 s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
3108 s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
3109 av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
3115 * Permute an 8x8 block.
3116 * @param block the block which will be permuted according to the given permutation vector
3117 * @param permutation the permutation vector
3118 * @param last the last non zero coefficient in scantable order, used to speed the permutation up
3119 * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
3120 * (inverse) permutated to scantable order!
3122 void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
3128 //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
3130 for(i=0; i<=last; i++){
3131 const int j= scantable[i];
3136 for(i=0; i<=last; i++){
3137 const int j= scantable[i];
3138 const int perm_j= permutation[j];
3139 block[perm_j]= temp[j];
3143 void ff_mpeg_flush(AVCodecContext *avctx){
3145 MpegEncContext *s = avctx->priv_data;
3147 if(s==NULL || s->picture==NULL)
3150 for (i = 0; i < MAX_PICTURE_COUNT; i++)
3151 ff_mpeg_unref_picture(s, &s->picture[i]);
3152 s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
3154 ff_mpeg_unref_picture(s, &s->current_picture);
3155 ff_mpeg_unref_picture(s, &s->last_picture);
3156 ff_mpeg_unref_picture(s, &s->next_picture);
3158 s->mb_x= s->mb_y= 0;
3161 s->parse_context.state= -1;
3162 s->parse_context.frame_start_found= 0;
3163 s->parse_context.overread= 0;
3164 s->parse_context.overread_index= 0;
3165 s->parse_context.index= 0;
3166 s->parse_context.last_index= 0;
3167 s->bitstream_buffer_size=0;
3172 * set qscale and update qscale dependent variables.
3174 void ff_set_qscale(MpegEncContext * s, int qscale)
3178 else if (qscale > 31)
3182 s->chroma_qscale= s->chroma_qscale_table[qscale];
3184 s->y_dc_scale= s->y_dc_scale_table[ qscale ];
3185 s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
3188 void ff_MPV_report_decode_progress(MpegEncContext *s)
3190 if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
3191 ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
3194 #if CONFIG_ERROR_RESILIENCE
3195 void ff_mpeg_er_frame_start(MpegEncContext *s)
3197 ERContext *er = &s->er;
3199 er->cur_pic = s->current_picture_ptr;
3200 er->last_pic = s->last_picture_ptr;
3201 er->next_pic = s->next_picture_ptr;
3203 er->pp_time = s->pp_time;
3204 er->pb_time = s->pb_time;
3205 er->quarter_sample = s->quarter_sample;
3206 er->partitioned_frame = s->partitioned_frame;
3208 ff_er_frame_start(er);
3210 #endif /* CONFIG_ERROR_RESILIENCE */