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 Libav.
10 * Libav 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 * Libav 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 Libav; 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"
40 #include "mpegutils.h"
41 #include "mpegvideo.h"
42 #include "mpegvideodata.h"
46 #include "xvmc_internal.h"
51 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
52 int16_t *block, int n, int qscale)
54 int i, level, nCoeffs;
55 const uint16_t *quant_matrix;
57 nCoeffs= s->block_last_index[n];
60 block[0] = block[0] * s->y_dc_scale;
62 block[0] = block[0] * s->c_dc_scale;
64 quant_matrix = s->intra_matrix;
65 for(i=1;i<=nCoeffs;i++) {
66 int j= s->intra_scantable.permutated[i];
71 level = (int)(level * qscale * quant_matrix[j]) >> 3;
72 level = (level - 1) | 1;
75 level = (int)(level * qscale * quant_matrix[j]) >> 3;
76 level = (level - 1) | 1;
83 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
84 int16_t *block, int n, int qscale)
86 int i, level, nCoeffs;
87 const uint16_t *quant_matrix;
89 nCoeffs= s->block_last_index[n];
91 quant_matrix = s->inter_matrix;
92 for(i=0; i<=nCoeffs; i++) {
93 int j= s->intra_scantable.permutated[i];
98 level = (((level << 1) + 1) * qscale *
99 ((int) (quant_matrix[j]))) >> 4;
100 level = (level - 1) | 1;
103 level = (((level << 1) + 1) * qscale *
104 ((int) (quant_matrix[j]))) >> 4;
105 level = (level - 1) | 1;
112 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
113 int16_t *block, int n, int qscale)
115 int i, level, nCoeffs;
116 const uint16_t *quant_matrix;
118 if(s->alternate_scan) nCoeffs= 63;
119 else nCoeffs= s->block_last_index[n];
122 block[0] = block[0] * s->y_dc_scale;
124 block[0] = block[0] * s->c_dc_scale;
125 quant_matrix = s->intra_matrix;
126 for(i=1;i<=nCoeffs;i++) {
127 int j= s->intra_scantable.permutated[i];
132 level = (int)(level * qscale * quant_matrix[j]) >> 3;
135 level = (int)(level * qscale * quant_matrix[j]) >> 3;
142 static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
143 int16_t *block, int n, int qscale)
145 int i, level, nCoeffs;
146 const uint16_t *quant_matrix;
149 if(s->alternate_scan) nCoeffs= 63;
150 else nCoeffs= s->block_last_index[n];
153 block[0] = block[0] * s->y_dc_scale;
155 block[0] = block[0] * s->c_dc_scale;
156 quant_matrix = s->intra_matrix;
157 for(i=1;i<=nCoeffs;i++) {
158 int j= s->intra_scantable.permutated[i];
163 level = (int)(level * qscale * quant_matrix[j]) >> 3;
166 level = (int)(level * qscale * quant_matrix[j]) >> 3;
175 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
176 int16_t *block, int n, int qscale)
178 int i, level, nCoeffs;
179 const uint16_t *quant_matrix;
182 if(s->alternate_scan) nCoeffs= 63;
183 else nCoeffs= s->block_last_index[n];
185 quant_matrix = s->inter_matrix;
186 for(i=0; i<=nCoeffs; i++) {
187 int j= s->intra_scantable.permutated[i];
192 level = (((level << 1) + 1) * qscale *
193 ((int) (quant_matrix[j]))) >> 4;
196 level = (((level << 1) + 1) * qscale *
197 ((int) (quant_matrix[j]))) >> 4;
206 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
207 int16_t *block, int n, int qscale)
209 int i, level, qmul, qadd;
212 assert(s->block_last_index[n]>=0);
218 block[0] = block[0] * s->y_dc_scale;
220 block[0] = block[0] * s->c_dc_scale;
221 qadd = (qscale - 1) | 1;
228 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
230 for(i=1; i<=nCoeffs; i++) {
234 level = level * qmul - qadd;
236 level = level * qmul + qadd;
243 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
244 int16_t *block, int n, int qscale)
246 int i, level, qmul, qadd;
249 assert(s->block_last_index[n]>=0);
251 qadd = (qscale - 1) | 1;
254 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
256 for(i=0; i<=nCoeffs; i++) {
260 level = level * qmul - qadd;
262 level = level * qmul + qadd;
269 static void mpeg_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
271 int mb_x, int mb_y, int mb_intra, int mb_skipped)
273 MpegEncContext *s = opaque;
276 s->mv_type = mv_type;
277 s->mb_intra = mb_intra;
278 s->mb_skipped = mb_skipped;
281 memcpy(s->mv, mv, sizeof(*mv));
283 ff_init_block_index(s);
284 ff_update_block_index(s);
286 s->bdsp.clear_blocks(s->block[0]);
288 s->dest[0] = s->current_picture.f->data[0] + (s->mb_y * 16 * s->linesize) + s->mb_x * 16;
289 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);
290 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);
293 av_log(s->avctx, AV_LOG_DEBUG,
294 "Interlaced error concealment is not fully implemented\n");
295 ff_mpv_decode_mb(s, s->block);
298 /* init common dct for both encoder and decoder */
299 static av_cold int dct_init(MpegEncContext *s)
301 ff_blockdsp_init(&s->bdsp, s->avctx);
302 ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
303 ff_mpegvideodsp_init(&s->mdsp);
304 ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
306 s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
307 s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
308 s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
309 s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
310 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
311 if (s->avctx->flags & CODEC_FLAG_BITEXACT)
312 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
313 s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
315 if (HAVE_INTRINSICS_NEON)
316 ff_mpv_common_init_neon(s);
319 ff_mpv_common_init_arm(s);
321 ff_mpv_common_init_ppc(s);
323 ff_mpv_common_init_x86(s);
328 av_cold void ff_mpv_idct_init(MpegEncContext *s)
330 ff_idctdsp_init(&s->idsp, s->avctx);
332 /* load & permutate scantables
333 * note: only wmv uses different ones
335 if (s->alternate_scan) {
336 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
337 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
339 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
340 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
342 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
343 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
346 static int alloc_picture(MpegEncContext *s, Picture *pic, int shared)
348 return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, shared, 0,
349 s->chroma_x_shift, s->chroma_y_shift, s->out_format,
350 s->mb_stride, s->mb_height, s->b8_stride,
351 &s->linesize, &s->uvlinesize);
354 static int init_duplicate_context(MpegEncContext *s)
356 int y_size = s->b8_stride * (2 * s->mb_height + 1);
357 int c_size = s->mb_stride * (s->mb_height + 1);
358 int yc_size = y_size + 2 * c_size;
361 s->sc.edge_emu_buffer =
364 s->sc.rd_scratchpad =
366 s->sc.obmc_scratchpad = NULL;
369 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
370 ME_MAP_SIZE * sizeof(uint32_t), fail)
371 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
372 ME_MAP_SIZE * sizeof(uint32_t), fail)
373 if (s->avctx->noise_reduction) {
374 FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
375 2 * 64 * sizeof(int), fail)
378 FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
379 s->block = s->blocks[0];
381 for (i = 0; i < 12; i++) {
382 s->pblocks[i] = &s->block[i];
384 if (s->avctx->codec_tag == AV_RL32("VCR2")) {
388 s->pblocks[4] = s->pblocks[5];
392 if (s->out_format == FMT_H263) {
394 FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
395 yc_size * sizeof(int16_t) * 16, fail);
396 s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
397 s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
398 s->ac_val[2] = s->ac_val[1] + c_size;
403 return -1; // free() through ff_mpv_common_end()
406 static void free_duplicate_context(MpegEncContext *s)
411 av_freep(&s->sc.edge_emu_buffer);
412 av_freep(&s->me.scratchpad);
414 s->sc.rd_scratchpad =
416 s->sc.obmc_scratchpad = NULL;
418 av_freep(&s->dct_error_sum);
419 av_freep(&s->me.map);
420 av_freep(&s->me.score_map);
421 av_freep(&s->blocks);
422 av_freep(&s->ac_val_base);
426 static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
428 #define COPY(a) bak->a = src->a
429 COPY(sc.edge_emu_buffer);
432 COPY(sc.rd_scratchpad);
433 COPY(sc.b_scratchpad);
434 COPY(sc.obmc_scratchpad);
441 COPY(me.map_generation);
453 int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
457 // FIXME copy only needed parts
459 backup_duplicate_context(&bak, dst);
460 memcpy(dst, src, sizeof(MpegEncContext));
461 backup_duplicate_context(dst, &bak);
462 for (i = 0; i < 12; i++) {
463 dst->pblocks[i] = &dst->block[i];
465 if (dst->avctx->codec_tag == AV_RL32("VCR2")) {
468 tmp = dst->pblocks[4];
469 dst->pblocks[4] = dst->pblocks[5];
470 dst->pblocks[5] = tmp;
472 if (!dst->sc.edge_emu_buffer &&
473 (ret = ff_mpeg_framesize_alloc(dst->avctx, &dst->me,
474 &dst->sc, dst->linesize)) < 0) {
475 av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
476 "scratch buffers.\n");
479 // STOP_TIMER("update_duplicate_context")
480 // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
484 int ff_mpeg_update_thread_context(AVCodecContext *dst,
485 const AVCodecContext *src)
488 MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
490 if (dst == src || !s1->context_initialized)
493 // FIXME can parameters change on I-frames?
494 // in that case dst may need a reinit
495 if (!s->context_initialized) {
497 memcpy(s, s1, sizeof(MpegEncContext));
500 s->bitstream_buffer = NULL;
501 s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
504 if ((err = ff_mpv_common_init(s)) < 0)
508 if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
510 s->context_reinit = 0;
511 s->height = s1->height;
512 s->width = s1->width;
513 if ((err = ff_mpv_common_frame_size_change(s)) < 0)
517 s->avctx->coded_height = s1->avctx->coded_height;
518 s->avctx->coded_width = s1->avctx->coded_width;
519 s->avctx->width = s1->avctx->width;
520 s->avctx->height = s1->avctx->height;
522 s->coded_picture_number = s1->coded_picture_number;
523 s->picture_number = s1->picture_number;
525 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
526 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
527 if (s1->picture[i].f->buf[0] &&
528 (ret = ff_mpeg_ref_picture(s->avctx, &s->picture[i], &s1->picture[i])) < 0)
532 #define UPDATE_PICTURE(pic)\
534 ff_mpeg_unref_picture(s->avctx, &s->pic);\
535 if (s1->pic.f->buf[0])\
536 ret = ff_mpeg_ref_picture(s->avctx, &s->pic, &s1->pic);\
538 ret = ff_update_picture_tables(&s->pic, &s1->pic);\
543 UPDATE_PICTURE(current_picture);
544 UPDATE_PICTURE(last_picture);
545 UPDATE_PICTURE(next_picture);
547 #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
548 ((pic && pic >= old_ctx->picture && \
549 pic < old_ctx->picture + MAX_PICTURE_COUNT) ? \
550 &new_ctx->picture[pic - old_ctx->picture] : NULL)
552 s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
553 s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
554 s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
556 // Error/bug resilience
557 s->next_p_frame_damaged = s1->next_p_frame_damaged;
558 s->workaround_bugs = s1->workaround_bugs;
561 memcpy(&s->last_time_base, &s1->last_time_base,
562 (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
563 (char *) &s1->last_time_base);
566 s->max_b_frames = s1->max_b_frames;
567 s->low_delay = s1->low_delay;
568 s->droppable = s1->droppable;
570 // DivX handling (doesn't work)
571 s->divx_packed = s1->divx_packed;
573 if (s1->bitstream_buffer) {
574 if (s1->bitstream_buffer_size +
575 FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
576 av_fast_malloc(&s->bitstream_buffer,
577 &s->allocated_bitstream_buffer_size,
578 s1->allocated_bitstream_buffer_size);
579 s->bitstream_buffer_size = s1->bitstream_buffer_size;
580 memcpy(s->bitstream_buffer, s1->bitstream_buffer,
581 s1->bitstream_buffer_size);
582 memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
583 FF_INPUT_BUFFER_PADDING_SIZE);
586 // linesize dependend scratch buffer allocation
587 if (!s->sc.edge_emu_buffer)
589 if (ff_mpeg_framesize_alloc(s->avctx, &s->me,
590 &s->sc, s1->linesize) < 0) {
591 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
592 "scratch buffers.\n");
593 return AVERROR(ENOMEM);
596 av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
597 "be allocated due to unknown size.\n");
601 // MPEG2/interlacing info
602 memcpy(&s->progressive_sequence, &s1->progressive_sequence,
603 (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
605 if (!s1->first_field) {
606 s->last_pict_type = s1->pict_type;
607 if (s1->current_picture_ptr)
608 s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f->quality;
615 * Set the given MpegEncContext to common defaults
616 * (same for encoding and decoding).
617 * The changed fields will not depend upon the
618 * prior state of the MpegEncContext.
620 void ff_mpv_common_defaults(MpegEncContext *s)
622 s->y_dc_scale_table =
623 s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
624 s->chroma_qscale_table = ff_default_chroma_qscale_table;
625 s->progressive_frame = 1;
626 s->progressive_sequence = 1;
627 s->picture_structure = PICT_FRAME;
629 s->coded_picture_number = 0;
630 s->picture_number = 0;
635 s->slice_context_count = 1;
639 * Set the given MpegEncContext to defaults for decoding.
640 * the changed fields will not depend upon
641 * the prior state of the MpegEncContext.
643 void ff_mpv_decode_defaults(MpegEncContext *s)
645 ff_mpv_common_defaults(s);
648 static int init_er(MpegEncContext *s)
650 ERContext *er = &s->er;
651 int mb_array_size = s->mb_height * s->mb_stride;
654 er->avctx = s->avctx;
656 er->mb_index2xy = s->mb_index2xy;
657 er->mb_num = s->mb_num;
658 er->mb_width = s->mb_width;
659 er->mb_height = s->mb_height;
660 er->mb_stride = s->mb_stride;
661 er->b8_stride = s->b8_stride;
663 er->er_temp_buffer = av_malloc(s->mb_height * s->mb_stride);
664 er->error_status_table = av_mallocz(mb_array_size);
665 if (!er->er_temp_buffer || !er->error_status_table)
668 er->mbskip_table = s->mbskip_table;
669 er->mbintra_table = s->mbintra_table;
671 for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++)
672 er->dc_val[i] = s->dc_val[i];
674 er->decode_mb = mpeg_er_decode_mb;
679 av_freep(&er->er_temp_buffer);
680 av_freep(&er->error_status_table);
681 return AVERROR(ENOMEM);
685 * Initialize and allocates MpegEncContext fields dependent on the resolution.
687 static int init_context_frame(MpegEncContext *s)
689 int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
691 s->mb_width = (s->width + 15) / 16;
692 s->mb_stride = s->mb_width + 1;
693 s->b8_stride = s->mb_width * 2 + 1;
694 mb_array_size = s->mb_height * s->mb_stride;
695 mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
697 /* set default edge pos, will be overriden
698 * in decode_header if needed */
699 s->h_edge_pos = s->mb_width * 16;
700 s->v_edge_pos = s->mb_height * 16;
702 s->mb_num = s->mb_width * s->mb_height;
707 s->block_wrap[3] = s->b8_stride;
709 s->block_wrap[5] = s->mb_stride;
711 y_size = s->b8_stride * (2 * s->mb_height + 1);
712 c_size = s->mb_stride * (s->mb_height + 1);
713 yc_size = y_size + 2 * c_size;
715 FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
716 fail); // error ressilience code looks cleaner with this
717 for (y = 0; y < s->mb_height; y++)
718 for (x = 0; x < s->mb_width; x++)
719 s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
721 s->mb_index2xy[s->mb_height * s->mb_width] =
722 (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
725 /* Allocate MV tables */
726 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,
727 mv_table_size * 2 * sizeof(int16_t), fail);
728 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,
729 mv_table_size * 2 * sizeof(int16_t), fail);
730 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,
731 mv_table_size * 2 * sizeof(int16_t), fail);
732 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,
733 mv_table_size * 2 * sizeof(int16_t), fail);
734 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,
735 mv_table_size * 2 * sizeof(int16_t), fail);
736 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,
737 mv_table_size * 2 * sizeof(int16_t), fail);
738 s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
739 s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
740 s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
741 s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base +
743 s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base +
745 s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
747 /* Allocate MB type table */
748 FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *
749 sizeof(uint16_t), fail); // needed for encoding
751 FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *
754 FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
755 mb_array_size * sizeof(float), fail);
756 FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
757 mb_array_size * sizeof(float), fail);
761 if (s->codec_id == AV_CODEC_ID_MPEG4 ||
762 (s->avctx->flags & CODEC_FLAG_INTERLACED_ME)) {
763 /* interlaced direct mode decoding tables */
764 for (i = 0; i < 2; i++) {
766 for (j = 0; j < 2; j++) {
767 for (k = 0; k < 2; k++) {
768 FF_ALLOCZ_OR_GOTO(s->avctx,
769 s->b_field_mv_table_base[i][j][k],
770 mv_table_size * 2 * sizeof(int16_t),
772 s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
775 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j],
776 mb_array_size * 2 * sizeof(uint8_t), fail);
777 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j],
778 mv_table_size * 2 * sizeof(int16_t), fail);
779 s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]
782 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i],
783 mb_array_size * 2 * sizeof(uint8_t), fail);
786 if (s->out_format == FMT_H263) {
788 FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
789 s->coded_block = s->coded_block_base + s->b8_stride + 1;
791 /* cbp, ac_pred, pred_dir */
792 FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table,
793 mb_array_size * sizeof(uint8_t), fail);
794 FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table,
795 mb_array_size * sizeof(uint8_t), fail);
798 if (s->h263_pred || s->h263_plus || !s->encoding) {
800 // MN: we need these for error resilience of intra-frames
801 FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base,
802 yc_size * sizeof(int16_t), fail);
803 s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
804 s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
805 s->dc_val[2] = s->dc_val[1] + c_size;
806 for (i = 0; i < yc_size; i++)
807 s->dc_val_base[i] = 1024;
810 /* which mb is a intra block */
811 FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
812 memset(s->mbintra_table, 1, mb_array_size);
814 /* init macroblock skip table */
815 FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
816 // Note the + 1 is for a quicker mpeg4 slice_end detection
820 return AVERROR(ENOMEM);
824 * init common structure for both encoder and decoder.
825 * this assumes that some variables like width/height are already set
827 av_cold int ff_mpv_common_init(MpegEncContext *s)
830 int nb_slices = (HAVE_THREADS &&
831 s->avctx->active_thread_type & FF_THREAD_SLICE) ?
832 s->avctx->thread_count : 1;
834 if (s->encoding && s->avctx->slices)
835 nb_slices = s->avctx->slices;
837 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
838 s->mb_height = (s->height + 31) / 32 * 2;
840 s->mb_height = (s->height + 15) / 16;
842 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
843 av_log(s->avctx, AV_LOG_ERROR,
844 "decoding to AV_PIX_FMT_NONE is not supported.\n");
848 if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
851 max_slices = FFMIN(MAX_THREADS, s->mb_height);
853 max_slices = MAX_THREADS;
854 av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
855 " reducing to %d\n", nb_slices, max_slices);
856 nb_slices = max_slices;
859 if ((s->width || s->height) &&
860 av_image_check_size(s->width, s->height, 0, s->avctx))
865 /* set chroma shifts */
866 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
870 /* convert fourcc to upper case */
871 s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
873 FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
874 MAX_PICTURE_COUNT * sizeof(Picture), fail);
875 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
876 s->picture[i].f = av_frame_alloc();
877 if (!s->picture[i].f)
880 memset(&s->next_picture, 0, sizeof(s->next_picture));
881 memset(&s->last_picture, 0, sizeof(s->last_picture));
882 memset(&s->current_picture, 0, sizeof(s->current_picture));
883 memset(&s->new_picture, 0, sizeof(s->new_picture));
884 s->next_picture.f = av_frame_alloc();
885 if (!s->next_picture.f)
887 s->last_picture.f = av_frame_alloc();
888 if (!s->last_picture.f)
890 s->current_picture.f = av_frame_alloc();
891 if (!s->current_picture.f)
893 s->new_picture.f = av_frame_alloc();
894 if (!s->new_picture.f)
897 if (s->width && s->height) {
898 if (init_context_frame(s))
901 s->parse_context.state = -1;
904 s->context_initialized = 1;
905 s->thread_context[0] = s;
907 if (s->width && s->height) {
909 for (i = 1; i < nb_slices; i++) {
910 s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
911 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
914 for (i = 0; i < nb_slices; i++) {
915 if (init_duplicate_context(s->thread_context[i]) < 0)
917 s->thread_context[i]->start_mb_y =
918 (s->mb_height * (i) + nb_slices / 2) / nb_slices;
919 s->thread_context[i]->end_mb_y =
920 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
923 if (init_duplicate_context(s) < 0)
926 s->end_mb_y = s->mb_height;
928 s->slice_context_count = nb_slices;
933 ff_mpv_common_end(s);
938 * Frees and resets MpegEncContext fields depending on the resolution.
939 * Is used during resolution changes to avoid a full reinitialization of the
942 static void free_context_frame(MpegEncContext *s)
946 av_freep(&s->mb_type);
947 av_freep(&s->p_mv_table_base);
948 av_freep(&s->b_forw_mv_table_base);
949 av_freep(&s->b_back_mv_table_base);
950 av_freep(&s->b_bidir_forw_mv_table_base);
951 av_freep(&s->b_bidir_back_mv_table_base);
952 av_freep(&s->b_direct_mv_table_base);
953 s->p_mv_table = NULL;
954 s->b_forw_mv_table = NULL;
955 s->b_back_mv_table = NULL;
956 s->b_bidir_forw_mv_table = NULL;
957 s->b_bidir_back_mv_table = NULL;
958 s->b_direct_mv_table = NULL;
959 for (i = 0; i < 2; i++) {
960 for (j = 0; j < 2; j++) {
961 for (k = 0; k < 2; k++) {
962 av_freep(&s->b_field_mv_table_base[i][j][k]);
963 s->b_field_mv_table[i][j][k] = NULL;
965 av_freep(&s->b_field_select_table[i][j]);
966 av_freep(&s->p_field_mv_table_base[i][j]);
967 s->p_field_mv_table[i][j] = NULL;
969 av_freep(&s->p_field_select_table[i]);
972 av_freep(&s->dc_val_base);
973 av_freep(&s->coded_block_base);
974 av_freep(&s->mbintra_table);
975 av_freep(&s->cbp_table);
976 av_freep(&s->pred_dir_table);
978 av_freep(&s->mbskip_table);
980 av_freep(&s->er.error_status_table);
981 av_freep(&s->er.er_temp_buffer);
982 av_freep(&s->mb_index2xy);
983 av_freep(&s->lambda_table);
984 av_freep(&s->cplx_tab);
985 av_freep(&s->bits_tab);
987 s->linesize = s->uvlinesize = 0;
990 int ff_mpv_common_frame_size_change(MpegEncContext *s)
994 if (s->slice_context_count > 1) {
995 for (i = 0; i < s->slice_context_count; i++) {
996 free_duplicate_context(s->thread_context[i]);
998 for (i = 1; i < s->slice_context_count; i++) {
999 av_freep(&s->thread_context[i]);
1002 free_duplicate_context(s);
1004 free_context_frame(s);
1007 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1008 s->picture[i].needs_realloc = 1;
1011 s->last_picture_ptr =
1012 s->next_picture_ptr =
1013 s->current_picture_ptr = NULL;
1016 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
1017 s->mb_height = (s->height + 31) / 32 * 2;
1019 s->mb_height = (s->height + 15) / 16;
1021 if ((s->width || s->height) &&
1022 (err = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
1025 if ((err = init_context_frame(s)))
1028 s->thread_context[0] = s;
1030 if (s->width && s->height) {
1031 int nb_slices = s->slice_context_count;
1032 if (nb_slices > 1) {
1033 for (i = 1; i < nb_slices; i++) {
1034 s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
1035 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
1038 for (i = 0; i < nb_slices; i++) {
1039 if ((err = init_duplicate_context(s->thread_context[i])) < 0)
1041 s->thread_context[i]->start_mb_y =
1042 (s->mb_height * (i) + nb_slices / 2) / nb_slices;
1043 s->thread_context[i]->end_mb_y =
1044 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
1047 if (init_duplicate_context(s) < 0)
1050 s->end_mb_y = s->mb_height;
1052 s->slice_context_count = nb_slices;
1057 ff_mpv_common_end(s);
1061 /* init common structure for both encoder and decoder */
1062 void ff_mpv_common_end(MpegEncContext *s)
1066 if (s->slice_context_count > 1) {
1067 for (i = 0; i < s->slice_context_count; i++) {
1068 free_duplicate_context(s->thread_context[i]);
1070 for (i = 1; i < s->slice_context_count; i++) {
1071 av_freep(&s->thread_context[i]);
1073 s->slice_context_count = 1;
1074 } else free_duplicate_context(s);
1076 av_freep(&s->parse_context.buffer);
1077 s->parse_context.buffer_size = 0;
1079 av_freep(&s->bitstream_buffer);
1080 s->allocated_bitstream_buffer_size = 0;
1083 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1084 ff_free_picture_tables(&s->picture[i]);
1085 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
1086 av_frame_free(&s->picture[i].f);
1089 av_freep(&s->picture);
1090 ff_free_picture_tables(&s->last_picture);
1091 ff_mpeg_unref_picture(s->avctx, &s->last_picture);
1092 av_frame_free(&s->last_picture.f);
1093 ff_free_picture_tables(&s->current_picture);
1094 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
1095 av_frame_free(&s->current_picture.f);
1096 ff_free_picture_tables(&s->next_picture);
1097 ff_mpeg_unref_picture(s->avctx, &s->next_picture);
1098 av_frame_free(&s->next_picture.f);
1099 ff_free_picture_tables(&s->new_picture);
1100 ff_mpeg_unref_picture(s->avctx, &s->new_picture);
1101 av_frame_free(&s->new_picture.f);
1103 free_context_frame(s);
1105 s->context_initialized = 0;
1106 s->last_picture_ptr =
1107 s->next_picture_ptr =
1108 s->current_picture_ptr = NULL;
1109 s->linesize = s->uvlinesize = 0;
1113 * generic function called after decoding
1114 * the header and before a frame is decoded.
1116 int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
1122 /* mark & release old frames */
1123 if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1124 s->last_picture_ptr != s->next_picture_ptr &&
1125 s->last_picture_ptr->f->buf[0]) {
1126 ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
1129 /* release forgotten pictures */
1130 /* if (mpeg124/h263) */
1131 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1132 if (&s->picture[i] != s->last_picture_ptr &&
1133 &s->picture[i] != s->next_picture_ptr &&
1134 s->picture[i].reference && !s->picture[i].needs_realloc) {
1135 if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1136 av_log(avctx, AV_LOG_ERROR,
1137 "releasing zombie picture\n");
1138 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
1142 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
1144 /* release non reference frames */
1145 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1146 if (!s->picture[i].reference)
1147 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
1150 if (s->current_picture_ptr && !s->current_picture_ptr->f->buf[0]) {
1151 // we already have a unused image
1152 // (maybe it was set before reading the header)
1153 pic = s->current_picture_ptr;
1155 i = ff_find_unused_picture(s->avctx, s->picture, 0);
1157 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1160 pic = &s->picture[i];
1164 if (!s->droppable) {
1165 if (s->pict_type != AV_PICTURE_TYPE_B)
1169 pic->f->coded_picture_number = s->coded_picture_number++;
1171 if (alloc_picture(s, pic, 0) < 0)
1174 s->current_picture_ptr = pic;
1175 // FIXME use only the vars from current_pic
1176 s->current_picture_ptr->f->top_field_first = s->top_field_first;
1177 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
1178 s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1179 if (s->picture_structure != PICT_FRAME)
1180 s->current_picture_ptr->f->top_field_first =
1181 (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
1183 s->current_picture_ptr->f->interlaced_frame = !s->progressive_frame &&
1184 !s->progressive_sequence;
1185 s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
1187 s->current_picture_ptr->f->pict_type = s->pict_type;
1188 // if (s->avctx->flags && CODEC_FLAG_QSCALE)
1189 // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
1190 s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1192 if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
1193 s->current_picture_ptr)) < 0)
1196 if (s->pict_type != AV_PICTURE_TYPE_B) {
1197 s->last_picture_ptr = s->next_picture_ptr;
1199 s->next_picture_ptr = s->current_picture_ptr;
1201 ff_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
1202 s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
1203 s->last_picture_ptr ? s->last_picture_ptr->f->data[0] : NULL,
1204 s->next_picture_ptr ? s->next_picture_ptr->f->data[0] : NULL,
1205 s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL,
1206 s->pict_type, s->droppable);
1208 if ((!s->last_picture_ptr || !s->last_picture_ptr->f->buf[0]) &&
1209 (s->pict_type != AV_PICTURE_TYPE_I ||
1210 s->picture_structure != PICT_FRAME)) {
1211 int h_chroma_shift, v_chroma_shift;
1212 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
1213 &h_chroma_shift, &v_chroma_shift);
1214 if (s->pict_type != AV_PICTURE_TYPE_I)
1215 av_log(avctx, AV_LOG_ERROR,
1216 "warning: first frame is no keyframe\n");
1217 else if (s->picture_structure != PICT_FRAME)
1218 av_log(avctx, AV_LOG_INFO,
1219 "allocate dummy last picture for field based first keyframe\n");
1221 /* Allocate a dummy frame */
1222 i = ff_find_unused_picture(s->avctx, s->picture, 0);
1224 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1227 s->last_picture_ptr = &s->picture[i];
1229 s->last_picture_ptr->reference = 3;
1230 s->last_picture_ptr->f->pict_type = AV_PICTURE_TYPE_I;
1232 if (alloc_picture(s, s->last_picture_ptr, 0) < 0) {
1233 s->last_picture_ptr = NULL;
1237 memset(s->last_picture_ptr->f->data[0], 0,
1238 avctx->height * s->last_picture_ptr->f->linesize[0]);
1239 memset(s->last_picture_ptr->f->data[1], 0x80,
1240 (avctx->height >> v_chroma_shift) *
1241 s->last_picture_ptr->f->linesize[1]);
1242 memset(s->last_picture_ptr->f->data[2], 0x80,
1243 (avctx->height >> v_chroma_shift) *
1244 s->last_picture_ptr->f->linesize[2]);
1246 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
1247 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
1249 if ((!s->next_picture_ptr || !s->next_picture_ptr->f->buf[0]) &&
1250 s->pict_type == AV_PICTURE_TYPE_B) {
1251 /* Allocate a dummy frame */
1252 i = ff_find_unused_picture(s->avctx, s->picture, 0);
1254 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1257 s->next_picture_ptr = &s->picture[i];
1259 s->next_picture_ptr->reference = 3;
1260 s->next_picture_ptr->f->pict_type = AV_PICTURE_TYPE_I;
1262 if (alloc_picture(s, s->next_picture_ptr, 0) < 0) {
1263 s->next_picture_ptr = NULL;
1266 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
1267 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
1270 if (s->last_picture_ptr) {
1271 ff_mpeg_unref_picture(s->avctx, &s->last_picture);
1272 if (s->last_picture_ptr->f->buf[0] &&
1273 (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
1274 s->last_picture_ptr)) < 0)
1277 if (s->next_picture_ptr) {
1278 ff_mpeg_unref_picture(s->avctx, &s->next_picture);
1279 if (s->next_picture_ptr->f->buf[0] &&
1280 (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
1281 s->next_picture_ptr)) < 0)
1285 if (s->pict_type != AV_PICTURE_TYPE_I &&
1286 !(s->last_picture_ptr && s->last_picture_ptr->f->buf[0])) {
1287 av_log(s, AV_LOG_ERROR,
1288 "Non-reference picture received and no reference available\n");
1289 return AVERROR_INVALIDDATA;
1292 if (s->picture_structure!= PICT_FRAME) {
1294 for (i = 0; i < 4; i++) {
1295 if (s->picture_structure == PICT_BOTTOM_FIELD) {
1296 s->current_picture.f->data[i] +=
1297 s->current_picture.f->linesize[i];
1299 s->current_picture.f->linesize[i] *= 2;
1300 s->last_picture.f->linesize[i] *= 2;
1301 s->next_picture.f->linesize[i] *= 2;
1305 /* set dequantizer, we can't do it during init as
1306 * it might change for mpeg4 and we can't do it in the header
1307 * decode as init is not called for mpeg4 there yet */
1308 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1309 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
1310 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
1311 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
1312 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
1313 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
1315 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
1316 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
1320 FF_DISABLE_DEPRECATION_WARNINGS
1321 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1322 return ff_xvmc_field_start(s, avctx);
1323 FF_ENABLE_DEPRECATION_WARNINGS
1324 #endif /* FF_API_XVMC */
1329 /* called after a frame has been decoded. */
1330 void ff_mpv_frame_end(MpegEncContext *s)
1333 FF_DISABLE_DEPRECATION_WARNINGS
1334 /* redraw edges for the frame if decoding didn't complete */
1335 // just to make sure that all data is rendered.
1336 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {
1337 ff_xvmc_field_end(s);
1339 FF_ENABLE_DEPRECATION_WARNINGS
1340 #endif /* FF_API_XVMC */
1344 if (s->current_picture.reference)
1345 ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
1349 * Print debugging info for the given picture.
1351 void ff_print_debug_info(MpegEncContext *s, Picture *p)
1354 if (s->avctx->hwaccel || !p || !p->mb_type)
1358 if (s->avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
1361 av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
1362 switch (pict->pict_type) {
1363 case AV_PICTURE_TYPE_I:
1364 av_log(s->avctx,AV_LOG_DEBUG,"I\n");
1366 case AV_PICTURE_TYPE_P:
1367 av_log(s->avctx,AV_LOG_DEBUG,"P\n");
1369 case AV_PICTURE_TYPE_B:
1370 av_log(s->avctx,AV_LOG_DEBUG,"B\n");
1372 case AV_PICTURE_TYPE_S:
1373 av_log(s->avctx,AV_LOG_DEBUG,"S\n");
1375 case AV_PICTURE_TYPE_SI:
1376 av_log(s->avctx,AV_LOG_DEBUG,"SI\n");
1378 case AV_PICTURE_TYPE_SP:
1379 av_log(s->avctx,AV_LOG_DEBUG,"SP\n");
1382 for (y = 0; y < s->mb_height; y++) {
1383 for (x = 0; x < s->mb_width; x++) {
1384 if (s->avctx->debug & FF_DEBUG_SKIP) {
1385 int count = s->mbskip_table[x + y * s->mb_stride];
1388 av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
1390 if (s->avctx->debug & FF_DEBUG_QP) {
1391 av_log(s->avctx, AV_LOG_DEBUG, "%2d",
1392 p->qscale_table[x + y * s->mb_stride]);
1394 if (s->avctx->debug & FF_DEBUG_MB_TYPE) {
1395 int mb_type = p->mb_type[x + y * s->mb_stride];
1396 // Type & MV direction
1397 if (IS_PCM(mb_type))
1398 av_log(s->avctx, AV_LOG_DEBUG, "P");
1399 else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
1400 av_log(s->avctx, AV_LOG_DEBUG, "A");
1401 else if (IS_INTRA4x4(mb_type))
1402 av_log(s->avctx, AV_LOG_DEBUG, "i");
1403 else if (IS_INTRA16x16(mb_type))
1404 av_log(s->avctx, AV_LOG_DEBUG, "I");
1405 else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
1406 av_log(s->avctx, AV_LOG_DEBUG, "d");
1407 else if (IS_DIRECT(mb_type))
1408 av_log(s->avctx, AV_LOG_DEBUG, "D");
1409 else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
1410 av_log(s->avctx, AV_LOG_DEBUG, "g");
1411 else if (IS_GMC(mb_type))
1412 av_log(s->avctx, AV_LOG_DEBUG, "G");
1413 else if (IS_SKIP(mb_type))
1414 av_log(s->avctx, AV_LOG_DEBUG, "S");
1415 else if (!USES_LIST(mb_type, 1))
1416 av_log(s->avctx, AV_LOG_DEBUG, ">");
1417 else if (!USES_LIST(mb_type, 0))
1418 av_log(s->avctx, AV_LOG_DEBUG, "<");
1420 assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
1421 av_log(s->avctx, AV_LOG_DEBUG, "X");
1425 if (IS_8X8(mb_type))
1426 av_log(s->avctx, AV_LOG_DEBUG, "+");
1427 else if (IS_16X8(mb_type))
1428 av_log(s->avctx, AV_LOG_DEBUG, "-");
1429 else if (IS_8X16(mb_type))
1430 av_log(s->avctx, AV_LOG_DEBUG, "|");
1431 else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
1432 av_log(s->avctx, AV_LOG_DEBUG, " ");
1434 av_log(s->avctx, AV_LOG_DEBUG, "?");
1437 if (IS_INTERLACED(mb_type))
1438 av_log(s->avctx, AV_LOG_DEBUG, "=");
1440 av_log(s->avctx, AV_LOG_DEBUG, " ");
1443 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1449 * find the lowest MB row referenced in the MVs
1451 static int lowest_referenced_row(MpegEncContext *s, int dir)
1453 int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
1454 int my, off, i, mvs;
1456 if (s->picture_structure != PICT_FRAME || s->mcsel)
1459 switch (s->mv_type) {
1473 for (i = 0; i < mvs; i++) {
1474 my = s->mv[dir][i][1]<<qpel_shift;
1475 my_max = FFMAX(my_max, my);
1476 my_min = FFMIN(my_min, my);
1479 off = (FFMAX(-my_min, my_max) + 63) >> 6;
1481 return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
1483 return s->mb_height-1;
1486 /* put block[] to dest[] */
1487 static inline void put_dct(MpegEncContext *s,
1488 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
1490 s->dct_unquantize_intra(s, block, i, qscale);
1491 s->idsp.idct_put(dest, line_size, block);
1494 /* add block[] to dest[] */
1495 static inline void add_dct(MpegEncContext *s,
1496 int16_t *block, int i, uint8_t *dest, int line_size)
1498 if (s->block_last_index[i] >= 0) {
1499 s->idsp.idct_add(dest, line_size, block);
1503 static inline void add_dequant_dct(MpegEncContext *s,
1504 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
1506 if (s->block_last_index[i] >= 0) {
1507 s->dct_unquantize_inter(s, block, i, qscale);
1509 s->idsp.idct_add(dest, line_size, block);
1514 * Clean dc, ac, coded_block for the current non-intra MB.
1516 void ff_clean_intra_table_entries(MpegEncContext *s)
1518 int wrap = s->b8_stride;
1519 int xy = s->block_index[0];
1522 s->dc_val[0][xy + 1 ] =
1523 s->dc_val[0][xy + wrap] =
1524 s->dc_val[0][xy + 1 + wrap] = 1024;
1526 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
1527 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
1528 if (s->msmpeg4_version>=3) {
1529 s->coded_block[xy ] =
1530 s->coded_block[xy + 1 ] =
1531 s->coded_block[xy + wrap] =
1532 s->coded_block[xy + 1 + wrap] = 0;
1535 wrap = s->mb_stride;
1536 xy = s->mb_x + s->mb_y * wrap;
1538 s->dc_val[2][xy] = 1024;
1540 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
1541 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
1543 s->mbintra_table[xy]= 0;
1546 /* generic function called after a macroblock has been parsed by the
1547 decoder or after it has been encoded by the encoder.
1549 Important variables used:
1550 s->mb_intra : true if intra macroblock
1551 s->mv_dir : motion vector direction
1552 s->mv_type : motion vector type
1553 s->mv : motion vector
1554 s->interlaced_dct : true if interlaced dct used (mpeg2)
1556 static av_always_inline
1557 void mpv_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
1560 const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
1563 FF_DISABLE_DEPRECATION_WARNINGS
1564 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
1565 ff_xvmc_decode_mb(s);//xvmc uses pblocks
1568 FF_ENABLE_DEPRECATION_WARNINGS
1569 #endif /* FF_API_XVMC */
1571 if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
1572 /* print DCT coefficients */
1574 av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
1576 for(j=0; j<64; j++){
1577 av_log(s->avctx, AV_LOG_DEBUG, "%5d",
1578 block[i][s->idsp.idct_permutation[j]]);
1580 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1584 s->current_picture.qscale_table[mb_xy] = s->qscale;
1586 /* update DC predictors for P macroblocks */
1588 if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
1589 if(s->mbintra_table[mb_xy])
1590 ff_clean_intra_table_entries(s);
1594 s->last_dc[2] = 128 << s->intra_dc_precision;
1597 else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
1598 s->mbintra_table[mb_xy]=1;
1600 if ((s->avctx->flags & CODEC_FLAG_PSNR) ||
1601 !(s->encoding && (s->intra_only || s->pict_type == AV_PICTURE_TYPE_B) &&
1602 s->avctx->mb_decision != FF_MB_DECISION_RD)) { // FIXME precalc
1603 uint8_t *dest_y, *dest_cb, *dest_cr;
1604 int dct_linesize, dct_offset;
1605 op_pixels_func (*op_pix)[4];
1606 qpel_mc_func (*op_qpix)[16];
1607 const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
1608 const int uvlinesize = s->current_picture.f->linesize[1];
1609 const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band;
1610 const int block_size = 8;
1612 /* avoid copy if macroblock skipped in last frame too */
1613 /* skip only during decoding as we might trash the buffers during encoding a bit */
1615 uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
1617 if (s->mb_skipped) {
1619 assert(s->pict_type!=AV_PICTURE_TYPE_I);
1621 } else if(!s->current_picture.reference) {
1624 *mbskip_ptr = 0; /* not skipped */
1628 dct_linesize = linesize << s->interlaced_dct;
1629 dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
1633 dest_cb= s->dest[1];
1634 dest_cr= s->dest[2];
1636 dest_y = s->sc.b_scratchpad;
1637 dest_cb= s->sc.b_scratchpad+16*linesize;
1638 dest_cr= s->sc.b_scratchpad+32*linesize;
1642 /* motion handling */
1643 /* decoding or more than one mb_type (MC was already done otherwise) */
1646 if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
1647 if (s->mv_dir & MV_DIR_FORWARD) {
1648 ff_thread_await_progress(&s->last_picture_ptr->tf,
1649 lowest_referenced_row(s, 0),
1652 if (s->mv_dir & MV_DIR_BACKWARD) {
1653 ff_thread_await_progress(&s->next_picture_ptr->tf,
1654 lowest_referenced_row(s, 1),
1659 op_qpix= s->me.qpel_put;
1660 if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
1661 op_pix = s->hdsp.put_pixels_tab;
1663 op_pix = s->hdsp.put_no_rnd_pixels_tab;
1665 if (s->mv_dir & MV_DIR_FORWARD) {
1666 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix, op_qpix);
1667 op_pix = s->hdsp.avg_pixels_tab;
1668 op_qpix= s->me.qpel_avg;
1670 if (s->mv_dir & MV_DIR_BACKWARD) {
1671 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix, op_qpix);
1675 /* skip dequant / idct if we are really late ;) */
1676 if(s->avctx->skip_idct){
1677 if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
1678 ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
1679 || s->avctx->skip_idct >= AVDISCARD_ALL)
1683 /* add dct residue */
1684 if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
1685 || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
1686 add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
1687 add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
1688 add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
1689 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
1691 if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
1692 if (s->chroma_y_shift){
1693 add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
1694 add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
1698 add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
1699 add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
1700 add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
1701 add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
1704 } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
1705 add_dct(s, block[0], 0, dest_y , dct_linesize);
1706 add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
1707 add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
1708 add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
1710 if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
1711 if(s->chroma_y_shift){//Chroma420
1712 add_dct(s, block[4], 4, dest_cb, uvlinesize);
1713 add_dct(s, block[5], 5, dest_cr, uvlinesize);
1716 dct_linesize = uvlinesize << s->interlaced_dct;
1717 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
1719 add_dct(s, block[4], 4, dest_cb, dct_linesize);
1720 add_dct(s, block[5], 5, dest_cr, dct_linesize);
1721 add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
1722 add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
1723 if(!s->chroma_x_shift){//Chroma444
1724 add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
1725 add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
1726 add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
1727 add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
1732 else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
1733 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
1736 /* dct only in intra block */
1737 if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
1738 put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
1739 put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
1740 put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
1741 put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
1743 if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
1744 if(s->chroma_y_shift){
1745 put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
1746 put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
1750 put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
1751 put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
1752 put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
1753 put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
1757 s->idsp.idct_put(dest_y, dct_linesize, block[0]);
1758 s->idsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
1759 s->idsp.idct_put(dest_y + dct_offset, dct_linesize, block[2]);
1760 s->idsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
1762 if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
1763 if(s->chroma_y_shift){
1764 s->idsp.idct_put(dest_cb, uvlinesize, block[4]);
1765 s->idsp.idct_put(dest_cr, uvlinesize, block[5]);
1768 dct_linesize = uvlinesize << s->interlaced_dct;
1769 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
1771 s->idsp.idct_put(dest_cb, dct_linesize, block[4]);
1772 s->idsp.idct_put(dest_cr, dct_linesize, block[5]);
1773 s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
1774 s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
1775 if(!s->chroma_x_shift){//Chroma444
1776 s->idsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
1777 s->idsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
1778 s->idsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
1779 s->idsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
1787 s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
1788 s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
1789 s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
1794 void ff_mpv_decode_mb(MpegEncContext *s, int16_t block[12][64])
1797 if(s->out_format == FMT_MPEG1) {
1798 mpv_decode_mb_internal(s, block, 1);
1801 mpv_decode_mb_internal(s, block, 0);
1804 void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
1806 ff_draw_horiz_band(s->avctx, s->current_picture.f,
1807 s->last_picture.f, y, h, s->picture_structure,
1808 s->first_field, s->low_delay);
1811 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
1812 const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
1813 const int uvlinesize = s->current_picture.f->linesize[1];
1814 const int mb_size= 4;
1816 s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
1817 s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
1818 s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
1819 s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
1820 s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
1821 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;
1822 //block_index is not used by mpeg2, so it is not affected by chroma_format
1824 s->dest[0] = s->current_picture.f->data[0] + ((s->mb_x - 1) << mb_size);
1825 s->dest[1] = s->current_picture.f->data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
1826 s->dest[2] = s->current_picture.f->data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
1828 if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
1830 if(s->picture_structure==PICT_FRAME){
1831 s->dest[0] += s->mb_y * linesize << mb_size;
1832 s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
1833 s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
1835 s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
1836 s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
1837 s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
1838 assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
1843 void ff_mpeg_flush(AVCodecContext *avctx){
1845 MpegEncContext *s = avctx->priv_data;
1847 if (!s || !s->picture)
1850 for (i = 0; i < MAX_PICTURE_COUNT; i++)
1851 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
1852 s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
1854 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
1855 ff_mpeg_unref_picture(s->avctx, &s->last_picture);
1856 ff_mpeg_unref_picture(s->avctx, &s->next_picture);
1858 s->mb_x= s->mb_y= 0;
1860 s->parse_context.state= -1;
1861 s->parse_context.frame_start_found= 0;
1862 s->parse_context.overread= 0;
1863 s->parse_context.overread_index= 0;
1864 s->parse_context.index= 0;
1865 s->parse_context.last_index= 0;
1866 s->bitstream_buffer_size=0;
1871 * set qscale and update qscale dependent variables.
1873 void ff_set_qscale(MpegEncContext * s, int qscale)
1877 else if (qscale > 31)
1881 s->chroma_qscale= s->chroma_qscale_table[qscale];
1883 s->y_dc_scale= s->y_dc_scale_table[ qscale ];
1884 s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
1887 void ff_mpv_report_decode_progress(MpegEncContext *s)
1889 if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
1890 ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);