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"
41 #include "mpegutils.h"
42 #include "mpegvideo.h"
43 #include "mpegvideodata.h"
47 #include "xvmc_internal.h"
52 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
53 int16_t *block, int n, int qscale)
55 int i, level, nCoeffs;
56 const uint16_t *quant_matrix;
58 nCoeffs= s->block_last_index[n];
61 block[0] = block[0] * s->y_dc_scale;
63 block[0] = block[0] * s->c_dc_scale;
65 quant_matrix = s->intra_matrix;
66 for(i=1;i<=nCoeffs;i++) {
67 int j= s->intra_scantable.permutated[i];
72 level = (int)(level * qscale * quant_matrix[j]) >> 3;
73 level = (level - 1) | 1;
76 level = (int)(level * qscale * quant_matrix[j]) >> 3;
77 level = (level - 1) | 1;
84 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
85 int16_t *block, int n, int qscale)
87 int i, level, nCoeffs;
88 const uint16_t *quant_matrix;
90 nCoeffs= s->block_last_index[n];
92 quant_matrix = s->inter_matrix;
93 for(i=0; i<=nCoeffs; i++) {
94 int j= s->intra_scantable.permutated[i];
99 level = (((level << 1) + 1) * qscale *
100 ((int) (quant_matrix[j]))) >> 4;
101 level = (level - 1) | 1;
104 level = (((level << 1) + 1) * qscale *
105 ((int) (quant_matrix[j]))) >> 4;
106 level = (level - 1) | 1;
113 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
114 int16_t *block, int n, int qscale)
116 int i, level, nCoeffs;
117 const uint16_t *quant_matrix;
119 if(s->alternate_scan) nCoeffs= 63;
120 else nCoeffs= s->block_last_index[n];
123 block[0] = block[0] * s->y_dc_scale;
125 block[0] = block[0] * s->c_dc_scale;
126 quant_matrix = s->intra_matrix;
127 for(i=1;i<=nCoeffs;i++) {
128 int j= s->intra_scantable.permutated[i];
133 level = (int)(level * qscale * quant_matrix[j]) >> 3;
136 level = (int)(level * qscale * quant_matrix[j]) >> 3;
143 static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
144 int16_t *block, int n, int qscale)
146 int i, level, nCoeffs;
147 const uint16_t *quant_matrix;
150 if(s->alternate_scan) nCoeffs= 63;
151 else nCoeffs= s->block_last_index[n];
154 block[0] = block[0] * s->y_dc_scale;
156 block[0] = block[0] * s->c_dc_scale;
157 quant_matrix = s->intra_matrix;
158 for(i=1;i<=nCoeffs;i++) {
159 int j= s->intra_scantable.permutated[i];
164 level = (int)(level * qscale * quant_matrix[j]) >> 3;
167 level = (int)(level * qscale * quant_matrix[j]) >> 3;
176 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
177 int16_t *block, int n, int qscale)
179 int i, level, nCoeffs;
180 const uint16_t *quant_matrix;
183 if(s->alternate_scan) nCoeffs= 63;
184 else nCoeffs= s->block_last_index[n];
186 quant_matrix = s->inter_matrix;
187 for(i=0; i<=nCoeffs; i++) {
188 int j= s->intra_scantable.permutated[i];
193 level = (((level << 1) + 1) * qscale *
194 ((int) (quant_matrix[j]))) >> 4;
197 level = (((level << 1) + 1) * qscale *
198 ((int) (quant_matrix[j]))) >> 4;
207 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
208 int16_t *block, int n, int qscale)
210 int i, level, qmul, qadd;
213 assert(s->block_last_index[n]>=0);
219 block[0] = block[0] * s->y_dc_scale;
221 block[0] = block[0] * s->c_dc_scale;
222 qadd = (qscale - 1) | 1;
229 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
231 for(i=1; i<=nCoeffs; i++) {
235 level = level * qmul - qadd;
237 level = level * qmul + qadd;
244 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
245 int16_t *block, int n, int qscale)
247 int i, level, qmul, qadd;
250 assert(s->block_last_index[n]>=0);
252 qadd = (qscale - 1) | 1;
255 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
257 for(i=0; i<=nCoeffs; i++) {
261 level = level * qmul - qadd;
263 level = level * qmul + qadd;
270 /* init common dct for both encoder and decoder */
271 static av_cold int dct_init(MpegEncContext *s)
273 ff_blockdsp_init(&s->bdsp, s->avctx);
274 ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
275 ff_mpegvideodsp_init(&s->mdsp);
276 ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
278 s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
279 s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
280 s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
281 s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
282 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
283 if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
284 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
285 s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
287 if (HAVE_INTRINSICS_NEON)
288 ff_mpv_common_init_neon(s);
291 ff_mpv_common_init_arm(s);
293 ff_mpv_common_init_ppc(s);
295 ff_mpv_common_init_x86(s);
300 av_cold void ff_mpv_idct_init(MpegEncContext *s)
302 ff_idctdsp_init(&s->idsp, s->avctx);
304 /* load & permutate scantables
305 * note: only wmv uses different ones
307 if (s->alternate_scan) {
308 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
309 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
311 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
312 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
314 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
315 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
318 static int alloc_picture(MpegEncContext *s, Picture *pic, int shared)
320 return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, shared, 0,
321 s->chroma_x_shift, s->chroma_y_shift, s->out_format,
322 s->mb_stride, s->mb_height, s->b8_stride,
323 &s->linesize, &s->uvlinesize);
326 static int init_duplicate_context(MpegEncContext *s)
328 int y_size = s->b8_stride * (2 * s->mb_height + 1);
329 int c_size = s->mb_stride * (s->mb_height + 1);
330 int yc_size = y_size + 2 * c_size;
333 s->sc.edge_emu_buffer =
336 s->sc.rd_scratchpad =
338 s->sc.obmc_scratchpad = NULL;
341 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
342 ME_MAP_SIZE * sizeof(uint32_t), fail)
343 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
344 ME_MAP_SIZE * sizeof(uint32_t), fail)
345 if (s->avctx->noise_reduction) {
346 FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
347 2 * 64 * sizeof(int), fail)
350 FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
351 s->block = s->blocks[0];
353 for (i = 0; i < 12; i++) {
354 s->pblocks[i] = &s->block[i];
356 if (s->avctx->codec_tag == AV_RL32("VCR2")) {
360 s->pblocks[4] = s->pblocks[5];
364 if (s->out_format == FMT_H263) {
366 FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
367 yc_size * sizeof(int16_t) * 16, fail);
368 s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
369 s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
370 s->ac_val[2] = s->ac_val[1] + c_size;
375 return -1; // free() through ff_mpv_common_end()
378 static void free_duplicate_context(MpegEncContext *s)
383 av_freep(&s->sc.edge_emu_buffer);
384 av_freep(&s->me.scratchpad);
386 s->sc.rd_scratchpad =
388 s->sc.obmc_scratchpad = NULL;
390 av_freep(&s->dct_error_sum);
391 av_freep(&s->me.map);
392 av_freep(&s->me.score_map);
393 av_freep(&s->blocks);
394 av_freep(&s->ac_val_base);
398 static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
400 #define COPY(a) bak->a = src->a
401 COPY(sc.edge_emu_buffer);
404 COPY(sc.rd_scratchpad);
405 COPY(sc.b_scratchpad);
406 COPY(sc.obmc_scratchpad);
413 COPY(me.map_generation);
425 int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
429 // FIXME copy only needed parts
431 backup_duplicate_context(&bak, dst);
432 memcpy(dst, src, sizeof(MpegEncContext));
433 backup_duplicate_context(dst, &bak);
434 for (i = 0; i < 12; i++) {
435 dst->pblocks[i] = &dst->block[i];
437 if (dst->avctx->codec_tag == AV_RL32("VCR2")) {
440 tmp = dst->pblocks[4];
441 dst->pblocks[4] = dst->pblocks[5];
442 dst->pblocks[5] = tmp;
444 if (!dst->sc.edge_emu_buffer &&
445 (ret = ff_mpeg_framesize_alloc(dst->avctx, &dst->me,
446 &dst->sc, dst->linesize)) < 0) {
447 av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
448 "scratch buffers.\n");
451 // STOP_TIMER("update_duplicate_context")
452 // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
456 int ff_mpeg_update_thread_context(AVCodecContext *dst,
457 const AVCodecContext *src)
460 MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
462 if (dst == src || !s1->context_initialized)
465 // FIXME can parameters change on I-frames?
466 // in that case dst may need a reinit
467 if (!s->context_initialized) {
469 memcpy(s, s1, sizeof(MpegEncContext));
472 s->bitstream_buffer = NULL;
473 s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
476 if ((err = ff_mpv_common_init(s)) < 0)
480 if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
482 s->context_reinit = 0;
483 s->height = s1->height;
484 s->width = s1->width;
485 if ((err = ff_mpv_common_frame_size_change(s)) < 0)
489 s->avctx->coded_height = s1->avctx->coded_height;
490 s->avctx->coded_width = s1->avctx->coded_width;
491 s->avctx->width = s1->avctx->width;
492 s->avctx->height = s1->avctx->height;
494 s->coded_picture_number = s1->coded_picture_number;
495 s->picture_number = s1->picture_number;
497 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
498 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
499 if (s1->picture[i].f->buf[0] &&
500 (ret = ff_mpeg_ref_picture(s->avctx, &s->picture[i], &s1->picture[i])) < 0)
504 #define UPDATE_PICTURE(pic)\
506 ff_mpeg_unref_picture(s->avctx, &s->pic);\
507 if (s1->pic.f->buf[0])\
508 ret = ff_mpeg_ref_picture(s->avctx, &s->pic, &s1->pic);\
510 ret = ff_update_picture_tables(&s->pic, &s1->pic);\
515 UPDATE_PICTURE(current_picture);
516 UPDATE_PICTURE(last_picture);
517 UPDATE_PICTURE(next_picture);
519 #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
520 ((pic && pic >= old_ctx->picture && \
521 pic < old_ctx->picture + MAX_PICTURE_COUNT) ? \
522 &new_ctx->picture[pic - old_ctx->picture] : NULL)
524 s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
525 s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
526 s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
528 // Error/bug resilience
529 s->next_p_frame_damaged = s1->next_p_frame_damaged;
530 s->workaround_bugs = s1->workaround_bugs;
533 memcpy(&s->last_time_base, &s1->last_time_base,
534 (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
535 (char *) &s1->last_time_base);
538 s->max_b_frames = s1->max_b_frames;
539 s->low_delay = s1->low_delay;
540 s->droppable = s1->droppable;
542 // DivX handling (doesn't work)
543 s->divx_packed = s1->divx_packed;
545 if (s1->bitstream_buffer) {
546 if (s1->bitstream_buffer_size +
547 AV_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
548 av_fast_malloc(&s->bitstream_buffer,
549 &s->allocated_bitstream_buffer_size,
550 s1->allocated_bitstream_buffer_size);
551 s->bitstream_buffer_size = s1->bitstream_buffer_size;
552 memcpy(s->bitstream_buffer, s1->bitstream_buffer,
553 s1->bitstream_buffer_size);
554 memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
555 AV_INPUT_BUFFER_PADDING_SIZE);
558 // linesize dependend scratch buffer allocation
559 if (!s->sc.edge_emu_buffer)
561 if (ff_mpeg_framesize_alloc(s->avctx, &s->me,
562 &s->sc, s1->linesize) < 0) {
563 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
564 "scratch buffers.\n");
565 return AVERROR(ENOMEM);
568 av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
569 "be allocated due to unknown size.\n");
573 // MPEG2/interlacing info
574 memcpy(&s->progressive_sequence, &s1->progressive_sequence,
575 (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
577 if (!s1->first_field) {
578 s->last_pict_type = s1->pict_type;
579 if (s1->current_picture_ptr)
580 s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f->quality;
587 * Set the given MpegEncContext to common defaults
588 * (same for encoding and decoding).
589 * The changed fields will not depend upon the
590 * prior state of the MpegEncContext.
592 void ff_mpv_common_defaults(MpegEncContext *s)
594 s->y_dc_scale_table =
595 s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
596 s->chroma_qscale_table = ff_default_chroma_qscale_table;
597 s->progressive_frame = 1;
598 s->progressive_sequence = 1;
599 s->picture_structure = PICT_FRAME;
601 s->coded_picture_number = 0;
602 s->picture_number = 0;
607 s->slice_context_count = 1;
611 * Set the given MpegEncContext to defaults for decoding.
612 * the changed fields will not depend upon
613 * the prior state of the MpegEncContext.
615 void ff_mpv_decode_defaults(MpegEncContext *s)
617 ff_mpv_common_defaults(s);
621 * Initialize and allocates MpegEncContext fields dependent on the resolution.
623 static int init_context_frame(MpegEncContext *s)
625 int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
627 s->mb_width = (s->width + 15) / 16;
628 s->mb_stride = s->mb_width + 1;
629 s->b8_stride = s->mb_width * 2 + 1;
630 mb_array_size = s->mb_height * s->mb_stride;
631 mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
633 /* set default edge pos, will be overriden
634 * in decode_header if needed */
635 s->h_edge_pos = s->mb_width * 16;
636 s->v_edge_pos = s->mb_height * 16;
638 s->mb_num = s->mb_width * s->mb_height;
643 s->block_wrap[3] = s->b8_stride;
645 s->block_wrap[5] = s->mb_stride;
647 y_size = s->b8_stride * (2 * s->mb_height + 1);
648 c_size = s->mb_stride * (s->mb_height + 1);
649 yc_size = y_size + 2 * c_size;
651 FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
652 fail); // error ressilience code looks cleaner with this
653 for (y = 0; y < s->mb_height; y++)
654 for (x = 0; x < s->mb_width; x++)
655 s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
657 s->mb_index2xy[s->mb_height * s->mb_width] =
658 (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
661 /* Allocate MV tables */
662 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,
663 mv_table_size * 2 * sizeof(int16_t), fail);
664 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,
665 mv_table_size * 2 * sizeof(int16_t), fail);
666 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,
667 mv_table_size * 2 * sizeof(int16_t), fail);
668 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,
669 mv_table_size * 2 * sizeof(int16_t), fail);
670 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,
671 mv_table_size * 2 * sizeof(int16_t), fail);
672 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,
673 mv_table_size * 2 * sizeof(int16_t), fail);
674 s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
675 s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
676 s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
677 s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base +
679 s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base +
681 s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
683 /* Allocate MB type table */
684 FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *
685 sizeof(uint16_t), fail); // needed for encoding
687 FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *
690 FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
691 mb_array_size * sizeof(float), fail);
692 FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
693 mb_array_size * sizeof(float), fail);
697 if (s->codec_id == AV_CODEC_ID_MPEG4 ||
698 (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) {
699 /* interlaced direct mode decoding tables */
700 for (i = 0; i < 2; i++) {
702 for (j = 0; j < 2; j++) {
703 for (k = 0; k < 2; k++) {
704 FF_ALLOCZ_OR_GOTO(s->avctx,
705 s->b_field_mv_table_base[i][j][k],
706 mv_table_size * 2 * sizeof(int16_t),
708 s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
711 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j],
712 mb_array_size * 2 * sizeof(uint8_t), fail);
713 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j],
714 mv_table_size * 2 * sizeof(int16_t), fail);
715 s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]
718 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i],
719 mb_array_size * 2 * sizeof(uint8_t), fail);
722 if (s->out_format == FMT_H263) {
724 FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
725 s->coded_block = s->coded_block_base + s->b8_stride + 1;
727 /* cbp, ac_pred, pred_dir */
728 FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table,
729 mb_array_size * sizeof(uint8_t), fail);
730 FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table,
731 mb_array_size * sizeof(uint8_t), fail);
734 if (s->h263_pred || s->h263_plus || !s->encoding) {
736 // MN: we need these for error resilience of intra-frames
737 FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base,
738 yc_size * sizeof(int16_t), fail);
739 s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
740 s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
741 s->dc_val[2] = s->dc_val[1] + c_size;
742 for (i = 0; i < yc_size; i++)
743 s->dc_val_base[i] = 1024;
746 /* which mb is a intra block */
747 FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
748 memset(s->mbintra_table, 1, mb_array_size);
750 /* init macroblock skip table */
751 FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
752 // Note the + 1 is for a quicker mpeg4 slice_end detection
754 return ff_mpeg_er_init(s);
756 return AVERROR(ENOMEM);
760 * init common structure for both encoder and decoder.
761 * this assumes that some variables like width/height are already set
763 av_cold int ff_mpv_common_init(MpegEncContext *s)
766 int nb_slices = (HAVE_THREADS &&
767 s->avctx->active_thread_type & FF_THREAD_SLICE) ?
768 s->avctx->thread_count : 1;
770 if (s->encoding && s->avctx->slices)
771 nb_slices = s->avctx->slices;
773 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
774 s->mb_height = (s->height + 31) / 32 * 2;
776 s->mb_height = (s->height + 15) / 16;
778 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
779 av_log(s->avctx, AV_LOG_ERROR,
780 "decoding to AV_PIX_FMT_NONE is not supported.\n");
784 if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
787 max_slices = FFMIN(MAX_THREADS, s->mb_height);
789 max_slices = MAX_THREADS;
790 av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
791 " reducing to %d\n", nb_slices, max_slices);
792 nb_slices = max_slices;
795 if ((s->width || s->height) &&
796 av_image_check_size(s->width, s->height, 0, s->avctx))
801 /* set chroma shifts */
802 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
806 /* convert fourcc to upper case */
807 s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
809 FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
810 MAX_PICTURE_COUNT * sizeof(Picture), fail);
811 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
812 s->picture[i].f = av_frame_alloc();
813 if (!s->picture[i].f)
816 memset(&s->next_picture, 0, sizeof(s->next_picture));
817 memset(&s->last_picture, 0, sizeof(s->last_picture));
818 memset(&s->current_picture, 0, sizeof(s->current_picture));
819 memset(&s->new_picture, 0, sizeof(s->new_picture));
820 s->next_picture.f = av_frame_alloc();
821 if (!s->next_picture.f)
823 s->last_picture.f = av_frame_alloc();
824 if (!s->last_picture.f)
826 s->current_picture.f = av_frame_alloc();
827 if (!s->current_picture.f)
829 s->new_picture.f = av_frame_alloc();
830 if (!s->new_picture.f)
833 if (s->width && s->height) {
834 if (init_context_frame(s))
837 s->parse_context.state = -1;
840 s->context_initialized = 1;
841 s->thread_context[0] = s;
843 if (s->width && s->height) {
845 for (i = 1; i < nb_slices; i++) {
846 s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
847 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
850 for (i = 0; i < nb_slices; i++) {
851 if (init_duplicate_context(s->thread_context[i]) < 0)
853 s->thread_context[i]->start_mb_y =
854 (s->mb_height * (i) + nb_slices / 2) / nb_slices;
855 s->thread_context[i]->end_mb_y =
856 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
859 if (init_duplicate_context(s) < 0)
862 s->end_mb_y = s->mb_height;
864 s->slice_context_count = nb_slices;
869 ff_mpv_common_end(s);
874 * Frees and resets MpegEncContext fields depending on the resolution.
875 * Is used during resolution changes to avoid a full reinitialization of the
878 static void free_context_frame(MpegEncContext *s)
882 av_freep(&s->mb_type);
883 av_freep(&s->p_mv_table_base);
884 av_freep(&s->b_forw_mv_table_base);
885 av_freep(&s->b_back_mv_table_base);
886 av_freep(&s->b_bidir_forw_mv_table_base);
887 av_freep(&s->b_bidir_back_mv_table_base);
888 av_freep(&s->b_direct_mv_table_base);
889 s->p_mv_table = NULL;
890 s->b_forw_mv_table = NULL;
891 s->b_back_mv_table = NULL;
892 s->b_bidir_forw_mv_table = NULL;
893 s->b_bidir_back_mv_table = NULL;
894 s->b_direct_mv_table = NULL;
895 for (i = 0; i < 2; i++) {
896 for (j = 0; j < 2; j++) {
897 for (k = 0; k < 2; k++) {
898 av_freep(&s->b_field_mv_table_base[i][j][k]);
899 s->b_field_mv_table[i][j][k] = NULL;
901 av_freep(&s->b_field_select_table[i][j]);
902 av_freep(&s->p_field_mv_table_base[i][j]);
903 s->p_field_mv_table[i][j] = NULL;
905 av_freep(&s->p_field_select_table[i]);
908 av_freep(&s->dc_val_base);
909 av_freep(&s->coded_block_base);
910 av_freep(&s->mbintra_table);
911 av_freep(&s->cbp_table);
912 av_freep(&s->pred_dir_table);
914 av_freep(&s->mbskip_table);
916 av_freep(&s->er.error_status_table);
917 av_freep(&s->er.er_temp_buffer);
918 av_freep(&s->mb_index2xy);
919 av_freep(&s->lambda_table);
920 av_freep(&s->cplx_tab);
921 av_freep(&s->bits_tab);
923 s->linesize = s->uvlinesize = 0;
926 int ff_mpv_common_frame_size_change(MpegEncContext *s)
930 if (s->slice_context_count > 1) {
931 for (i = 0; i < s->slice_context_count; i++) {
932 free_duplicate_context(s->thread_context[i]);
934 for (i = 1; i < s->slice_context_count; i++) {
935 av_freep(&s->thread_context[i]);
938 free_duplicate_context(s);
940 free_context_frame(s);
943 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
944 s->picture[i].needs_realloc = 1;
947 s->last_picture_ptr =
948 s->next_picture_ptr =
949 s->current_picture_ptr = NULL;
952 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
953 s->mb_height = (s->height + 31) / 32 * 2;
955 s->mb_height = (s->height + 15) / 16;
957 if ((s->width || s->height) &&
958 (err = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
961 if ((err = init_context_frame(s)))
964 s->thread_context[0] = s;
966 if (s->width && s->height) {
967 int nb_slices = s->slice_context_count;
969 for (i = 1; i < nb_slices; i++) {
970 s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
971 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
974 for (i = 0; i < nb_slices; i++) {
975 if ((err = init_duplicate_context(s->thread_context[i])) < 0)
977 s->thread_context[i]->start_mb_y =
978 (s->mb_height * (i) + nb_slices / 2) / nb_slices;
979 s->thread_context[i]->end_mb_y =
980 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
983 if (init_duplicate_context(s) < 0)
986 s->end_mb_y = s->mb_height;
988 s->slice_context_count = nb_slices;
993 ff_mpv_common_end(s);
997 /* init common structure for both encoder and decoder */
998 void ff_mpv_common_end(MpegEncContext *s)
1002 if (s->slice_context_count > 1) {
1003 for (i = 0; i < s->slice_context_count; i++) {
1004 free_duplicate_context(s->thread_context[i]);
1006 for (i = 1; i < s->slice_context_count; i++) {
1007 av_freep(&s->thread_context[i]);
1009 s->slice_context_count = 1;
1010 } else free_duplicate_context(s);
1012 av_freep(&s->parse_context.buffer);
1013 s->parse_context.buffer_size = 0;
1015 av_freep(&s->bitstream_buffer);
1016 s->allocated_bitstream_buffer_size = 0;
1019 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1020 ff_free_picture_tables(&s->picture[i]);
1021 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
1022 av_frame_free(&s->picture[i].f);
1025 av_freep(&s->picture);
1026 ff_free_picture_tables(&s->last_picture);
1027 ff_mpeg_unref_picture(s->avctx, &s->last_picture);
1028 av_frame_free(&s->last_picture.f);
1029 ff_free_picture_tables(&s->current_picture);
1030 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
1031 av_frame_free(&s->current_picture.f);
1032 ff_free_picture_tables(&s->next_picture);
1033 ff_mpeg_unref_picture(s->avctx, &s->next_picture);
1034 av_frame_free(&s->next_picture.f);
1035 ff_free_picture_tables(&s->new_picture);
1036 ff_mpeg_unref_picture(s->avctx, &s->new_picture);
1037 av_frame_free(&s->new_picture.f);
1039 free_context_frame(s);
1041 s->context_initialized = 0;
1042 s->last_picture_ptr =
1043 s->next_picture_ptr =
1044 s->current_picture_ptr = NULL;
1045 s->linesize = s->uvlinesize = 0;
1049 * generic function called after decoding
1050 * the header and before a frame is decoded.
1052 int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
1058 /* mark & release old frames */
1059 if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1060 s->last_picture_ptr != s->next_picture_ptr &&
1061 s->last_picture_ptr->f->buf[0]) {
1062 ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
1065 /* release forgotten pictures */
1066 /* if (mpeg124/h263) */
1067 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1068 if (&s->picture[i] != s->last_picture_ptr &&
1069 &s->picture[i] != s->next_picture_ptr &&
1070 s->picture[i].reference && !s->picture[i].needs_realloc) {
1071 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
1075 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
1077 /* release non reference frames */
1078 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1079 if (!s->picture[i].reference)
1080 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
1083 if (s->current_picture_ptr && !s->current_picture_ptr->f->buf[0]) {
1084 // we already have a unused image
1085 // (maybe it was set before reading the header)
1086 pic = s->current_picture_ptr;
1088 i = ff_find_unused_picture(s->avctx, s->picture, 0);
1090 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1093 pic = &s->picture[i];
1097 if (!s->droppable) {
1098 if (s->pict_type != AV_PICTURE_TYPE_B)
1102 pic->f->coded_picture_number = s->coded_picture_number++;
1104 if (alloc_picture(s, pic, 0) < 0)
1107 s->current_picture_ptr = pic;
1108 // FIXME use only the vars from current_pic
1109 s->current_picture_ptr->f->top_field_first = s->top_field_first;
1110 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
1111 s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1112 if (s->picture_structure != PICT_FRAME)
1113 s->current_picture_ptr->f->top_field_first =
1114 (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
1116 s->current_picture_ptr->f->interlaced_frame = !s->progressive_frame &&
1117 !s->progressive_sequence;
1118 s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
1120 s->current_picture_ptr->f->pict_type = s->pict_type;
1121 // if (s->avctx->flags && AV_CODEC_FLAG_QSCALE)
1122 // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
1123 s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1125 if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
1126 s->current_picture_ptr)) < 0)
1129 if (s->pict_type != AV_PICTURE_TYPE_B) {
1130 s->last_picture_ptr = s->next_picture_ptr;
1132 s->next_picture_ptr = s->current_picture_ptr;
1134 ff_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
1135 s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
1136 s->last_picture_ptr ? s->last_picture_ptr->f->data[0] : NULL,
1137 s->next_picture_ptr ? s->next_picture_ptr->f->data[0] : NULL,
1138 s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL,
1139 s->pict_type, s->droppable);
1141 if ((!s->last_picture_ptr || !s->last_picture_ptr->f->buf[0]) &&
1142 (s->pict_type != AV_PICTURE_TYPE_I ||
1143 s->picture_structure != PICT_FRAME)) {
1144 int h_chroma_shift, v_chroma_shift;
1145 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
1146 &h_chroma_shift, &v_chroma_shift);
1147 if (s->pict_type != AV_PICTURE_TYPE_I)
1148 av_log(avctx, AV_LOG_ERROR,
1149 "warning: first frame is no keyframe\n");
1150 else if (s->picture_structure != PICT_FRAME)
1151 av_log(avctx, AV_LOG_INFO,
1152 "allocate dummy last picture for field based first keyframe\n");
1154 /* Allocate a dummy frame */
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 s->last_picture_ptr = &s->picture[i];
1162 s->last_picture_ptr->reference = 3;
1163 s->last_picture_ptr->f->pict_type = AV_PICTURE_TYPE_I;
1165 if (alloc_picture(s, s->last_picture_ptr, 0) < 0) {
1166 s->last_picture_ptr = NULL;
1170 memset(s->last_picture_ptr->f->data[0], 0,
1171 avctx->height * s->last_picture_ptr->f->linesize[0]);
1172 memset(s->last_picture_ptr->f->data[1], 0x80,
1173 (avctx->height >> v_chroma_shift) *
1174 s->last_picture_ptr->f->linesize[1]);
1175 memset(s->last_picture_ptr->f->data[2], 0x80,
1176 (avctx->height >> v_chroma_shift) *
1177 s->last_picture_ptr->f->linesize[2]);
1179 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
1180 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
1182 if ((!s->next_picture_ptr || !s->next_picture_ptr->f->buf[0]) &&
1183 s->pict_type == AV_PICTURE_TYPE_B) {
1184 /* Allocate a dummy frame */
1185 i = ff_find_unused_picture(s->avctx, s->picture, 0);
1187 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1190 s->next_picture_ptr = &s->picture[i];
1192 s->next_picture_ptr->reference = 3;
1193 s->next_picture_ptr->f->pict_type = AV_PICTURE_TYPE_I;
1195 if (alloc_picture(s, s->next_picture_ptr, 0) < 0) {
1196 s->next_picture_ptr = NULL;
1199 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
1200 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
1203 if (s->last_picture_ptr) {
1204 ff_mpeg_unref_picture(s->avctx, &s->last_picture);
1205 if (s->last_picture_ptr->f->buf[0] &&
1206 (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
1207 s->last_picture_ptr)) < 0)
1210 if (s->next_picture_ptr) {
1211 ff_mpeg_unref_picture(s->avctx, &s->next_picture);
1212 if (s->next_picture_ptr->f->buf[0] &&
1213 (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
1214 s->next_picture_ptr)) < 0)
1218 if (s->pict_type != AV_PICTURE_TYPE_I &&
1219 !(s->last_picture_ptr && s->last_picture_ptr->f->buf[0])) {
1220 av_log(s, AV_LOG_ERROR,
1221 "Non-reference picture received and no reference available\n");
1222 return AVERROR_INVALIDDATA;
1225 if (s->picture_structure!= PICT_FRAME) {
1227 for (i = 0; i < 4; i++) {
1228 if (s->picture_structure == PICT_BOTTOM_FIELD) {
1229 s->current_picture.f->data[i] +=
1230 s->current_picture.f->linesize[i];
1232 s->current_picture.f->linesize[i] *= 2;
1233 s->last_picture.f->linesize[i] *= 2;
1234 s->next_picture.f->linesize[i] *= 2;
1238 /* set dequantizer, we can't do it during init as
1239 * it might change for mpeg4 and we can't do it in the header
1240 * decode as init is not called for mpeg4 there yet */
1241 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1242 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
1243 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
1244 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
1245 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
1246 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
1248 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
1249 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
1253 FF_DISABLE_DEPRECATION_WARNINGS
1254 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1255 return ff_xvmc_field_start(s, avctx);
1256 FF_ENABLE_DEPRECATION_WARNINGS
1257 #endif /* FF_API_XVMC */
1262 /* called after a frame has been decoded. */
1263 void ff_mpv_frame_end(MpegEncContext *s)
1266 FF_DISABLE_DEPRECATION_WARNINGS
1267 /* redraw edges for the frame if decoding didn't complete */
1268 // just to make sure that all data is rendered.
1269 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {
1270 ff_xvmc_field_end(s);
1272 FF_ENABLE_DEPRECATION_WARNINGS
1273 #endif /* FF_API_XVMC */
1277 if (s->current_picture.reference)
1278 ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
1282 * Print debugging info for the given picture.
1284 void ff_print_debug_info(MpegEncContext *s, Picture *p)
1287 if (s->avctx->hwaccel || !p || !p->mb_type)
1291 if (s->avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
1294 av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
1295 switch (pict->pict_type) {
1296 case AV_PICTURE_TYPE_I:
1297 av_log(s->avctx,AV_LOG_DEBUG,"I\n");
1299 case AV_PICTURE_TYPE_P:
1300 av_log(s->avctx,AV_LOG_DEBUG,"P\n");
1302 case AV_PICTURE_TYPE_B:
1303 av_log(s->avctx,AV_LOG_DEBUG,"B\n");
1305 case AV_PICTURE_TYPE_S:
1306 av_log(s->avctx,AV_LOG_DEBUG,"S\n");
1308 case AV_PICTURE_TYPE_SI:
1309 av_log(s->avctx,AV_LOG_DEBUG,"SI\n");
1311 case AV_PICTURE_TYPE_SP:
1312 av_log(s->avctx,AV_LOG_DEBUG,"SP\n");
1315 for (y = 0; y < s->mb_height; y++) {
1316 for (x = 0; x < s->mb_width; x++) {
1317 if (s->avctx->debug & FF_DEBUG_SKIP) {
1318 int count = s->mbskip_table[x + y * s->mb_stride];
1321 av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
1323 if (s->avctx->debug & FF_DEBUG_QP) {
1324 av_log(s->avctx, AV_LOG_DEBUG, "%2d",
1325 p->qscale_table[x + y * s->mb_stride]);
1327 if (s->avctx->debug & FF_DEBUG_MB_TYPE) {
1328 int mb_type = p->mb_type[x + y * s->mb_stride];
1329 // Type & MV direction
1330 if (IS_PCM(mb_type))
1331 av_log(s->avctx, AV_LOG_DEBUG, "P");
1332 else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
1333 av_log(s->avctx, AV_LOG_DEBUG, "A");
1334 else if (IS_INTRA4x4(mb_type))
1335 av_log(s->avctx, AV_LOG_DEBUG, "i");
1336 else if (IS_INTRA16x16(mb_type))
1337 av_log(s->avctx, AV_LOG_DEBUG, "I");
1338 else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
1339 av_log(s->avctx, AV_LOG_DEBUG, "d");
1340 else if (IS_DIRECT(mb_type))
1341 av_log(s->avctx, AV_LOG_DEBUG, "D");
1342 else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
1343 av_log(s->avctx, AV_LOG_DEBUG, "g");
1344 else if (IS_GMC(mb_type))
1345 av_log(s->avctx, AV_LOG_DEBUG, "G");
1346 else if (IS_SKIP(mb_type))
1347 av_log(s->avctx, AV_LOG_DEBUG, "S");
1348 else if (!USES_LIST(mb_type, 1))
1349 av_log(s->avctx, AV_LOG_DEBUG, ">");
1350 else if (!USES_LIST(mb_type, 0))
1351 av_log(s->avctx, AV_LOG_DEBUG, "<");
1353 assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
1354 av_log(s->avctx, AV_LOG_DEBUG, "X");
1358 if (IS_8X8(mb_type))
1359 av_log(s->avctx, AV_LOG_DEBUG, "+");
1360 else if (IS_16X8(mb_type))
1361 av_log(s->avctx, AV_LOG_DEBUG, "-");
1362 else if (IS_8X16(mb_type))
1363 av_log(s->avctx, AV_LOG_DEBUG, "|");
1364 else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
1365 av_log(s->avctx, AV_LOG_DEBUG, " ");
1367 av_log(s->avctx, AV_LOG_DEBUG, "?");
1370 if (IS_INTERLACED(mb_type))
1371 av_log(s->avctx, AV_LOG_DEBUG, "=");
1373 av_log(s->avctx, AV_LOG_DEBUG, " ");
1376 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1382 * find the lowest MB row referenced in the MVs
1384 static int lowest_referenced_row(MpegEncContext *s, int dir)
1386 int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
1387 int my, off, i, mvs;
1389 if (s->picture_structure != PICT_FRAME || s->mcsel)
1392 switch (s->mv_type) {
1406 for (i = 0; i < mvs; i++) {
1407 my = s->mv[dir][i][1]<<qpel_shift;
1408 my_max = FFMAX(my_max, my);
1409 my_min = FFMIN(my_min, my);
1412 off = (FFMAX(-my_min, my_max) + 63) >> 6;
1414 return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
1416 return s->mb_height-1;
1419 /* put block[] to dest[] */
1420 static inline void put_dct(MpegEncContext *s,
1421 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
1423 s->dct_unquantize_intra(s, block, i, qscale);
1424 s->idsp.idct_put(dest, line_size, block);
1427 /* add block[] to dest[] */
1428 static inline void add_dct(MpegEncContext *s,
1429 int16_t *block, int i, uint8_t *dest, int line_size)
1431 if (s->block_last_index[i] >= 0) {
1432 s->idsp.idct_add(dest, line_size, block);
1436 static inline void add_dequant_dct(MpegEncContext *s,
1437 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
1439 if (s->block_last_index[i] >= 0) {
1440 s->dct_unquantize_inter(s, block, i, qscale);
1442 s->idsp.idct_add(dest, line_size, block);
1447 * Clean dc, ac, coded_block for the current non-intra MB.
1449 void ff_clean_intra_table_entries(MpegEncContext *s)
1451 int wrap = s->b8_stride;
1452 int xy = s->block_index[0];
1455 s->dc_val[0][xy + 1 ] =
1456 s->dc_val[0][xy + wrap] =
1457 s->dc_val[0][xy + 1 + wrap] = 1024;
1459 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
1460 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
1461 if (s->msmpeg4_version>=3) {
1462 s->coded_block[xy ] =
1463 s->coded_block[xy + 1 ] =
1464 s->coded_block[xy + wrap] =
1465 s->coded_block[xy + 1 + wrap] = 0;
1468 wrap = s->mb_stride;
1469 xy = s->mb_x + s->mb_y * wrap;
1471 s->dc_val[2][xy] = 1024;
1473 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
1474 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
1476 s->mbintra_table[xy]= 0;
1479 /* generic function called after a macroblock has been parsed by the
1480 decoder or after it has been encoded by the encoder.
1482 Important variables used:
1483 s->mb_intra : true if intra macroblock
1484 s->mv_dir : motion vector direction
1485 s->mv_type : motion vector type
1486 s->mv : motion vector
1487 s->interlaced_dct : true if interlaced dct used (mpeg2)
1489 static av_always_inline
1490 void mpv_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
1493 const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
1496 FF_DISABLE_DEPRECATION_WARNINGS
1497 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
1498 ff_xvmc_decode_mb(s);//xvmc uses pblocks
1501 FF_ENABLE_DEPRECATION_WARNINGS
1502 #endif /* FF_API_XVMC */
1504 if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
1505 /* print DCT coefficients */
1507 av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
1509 for(j=0; j<64; j++){
1510 av_log(s->avctx, AV_LOG_DEBUG, "%5d",
1511 block[i][s->idsp.idct_permutation[j]]);
1513 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1517 s->current_picture.qscale_table[mb_xy] = s->qscale;
1519 /* update DC predictors for P macroblocks */
1521 if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
1522 if(s->mbintra_table[mb_xy])
1523 ff_clean_intra_table_entries(s);
1527 s->last_dc[2] = 128 << s->intra_dc_precision;
1530 else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
1531 s->mbintra_table[mb_xy]=1;
1533 if ((s->avctx->flags & AV_CODEC_FLAG_PSNR) ||
1534 !(s->encoding && (s->intra_only || s->pict_type == AV_PICTURE_TYPE_B) &&
1535 s->avctx->mb_decision != FF_MB_DECISION_RD)) { // FIXME precalc
1536 uint8_t *dest_y, *dest_cb, *dest_cr;
1537 int dct_linesize, dct_offset;
1538 op_pixels_func (*op_pix)[4];
1539 qpel_mc_func (*op_qpix)[16];
1540 const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
1541 const int uvlinesize = s->current_picture.f->linesize[1];
1542 const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band;
1543 const int block_size = 8;
1545 /* avoid copy if macroblock skipped in last frame too */
1546 /* skip only during decoding as we might trash the buffers during encoding a bit */
1548 uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
1550 if (s->mb_skipped) {
1552 assert(s->pict_type!=AV_PICTURE_TYPE_I);
1554 } else if(!s->current_picture.reference) {
1557 *mbskip_ptr = 0; /* not skipped */
1561 dct_linesize = linesize << s->interlaced_dct;
1562 dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
1566 dest_cb= s->dest[1];
1567 dest_cr= s->dest[2];
1569 dest_y = s->sc.b_scratchpad;
1570 dest_cb= s->sc.b_scratchpad+16*linesize;
1571 dest_cr= s->sc.b_scratchpad+32*linesize;
1575 /* motion handling */
1576 /* decoding or more than one mb_type (MC was already done otherwise) */
1579 if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
1580 if (s->mv_dir & MV_DIR_FORWARD) {
1581 ff_thread_await_progress(&s->last_picture_ptr->tf,
1582 lowest_referenced_row(s, 0),
1585 if (s->mv_dir & MV_DIR_BACKWARD) {
1586 ff_thread_await_progress(&s->next_picture_ptr->tf,
1587 lowest_referenced_row(s, 1),
1592 op_qpix= s->me.qpel_put;
1593 if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
1594 op_pix = s->hdsp.put_pixels_tab;
1596 op_pix = s->hdsp.put_no_rnd_pixels_tab;
1598 if (s->mv_dir & MV_DIR_FORWARD) {
1599 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix, op_qpix);
1600 op_pix = s->hdsp.avg_pixels_tab;
1601 op_qpix= s->me.qpel_avg;
1603 if (s->mv_dir & MV_DIR_BACKWARD) {
1604 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix, op_qpix);
1608 /* skip dequant / idct if we are really late ;) */
1609 if(s->avctx->skip_idct){
1610 if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
1611 ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
1612 || s->avctx->skip_idct >= AVDISCARD_ALL)
1616 /* add dct residue */
1617 if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
1618 || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
1619 add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
1620 add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
1621 add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
1622 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
1624 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1625 if (s->chroma_y_shift){
1626 add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
1627 add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
1631 add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
1632 add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
1633 add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
1634 add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
1637 } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
1638 add_dct(s, block[0], 0, dest_y , dct_linesize);
1639 add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
1640 add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
1641 add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
1643 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1644 if(s->chroma_y_shift){//Chroma420
1645 add_dct(s, block[4], 4, dest_cb, uvlinesize);
1646 add_dct(s, block[5], 5, dest_cr, uvlinesize);
1649 dct_linesize = uvlinesize << s->interlaced_dct;
1650 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
1652 add_dct(s, block[4], 4, dest_cb, dct_linesize);
1653 add_dct(s, block[5], 5, dest_cr, dct_linesize);
1654 add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
1655 add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
1656 if(!s->chroma_x_shift){//Chroma444
1657 add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
1658 add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
1659 add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
1660 add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
1665 else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
1666 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
1669 /* dct only in intra block */
1670 if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
1671 put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
1672 put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
1673 put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
1674 put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
1676 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1677 if(s->chroma_y_shift){
1678 put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
1679 put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
1683 put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
1684 put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
1685 put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
1686 put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
1690 s->idsp.idct_put(dest_y, dct_linesize, block[0]);
1691 s->idsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
1692 s->idsp.idct_put(dest_y + dct_offset, dct_linesize, block[2]);
1693 s->idsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
1695 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1696 if(s->chroma_y_shift){
1697 s->idsp.idct_put(dest_cb, uvlinesize, block[4]);
1698 s->idsp.idct_put(dest_cr, uvlinesize, block[5]);
1701 dct_linesize = uvlinesize << s->interlaced_dct;
1702 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
1704 s->idsp.idct_put(dest_cb, dct_linesize, block[4]);
1705 s->idsp.idct_put(dest_cr, dct_linesize, block[5]);
1706 s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
1707 s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
1708 if(!s->chroma_x_shift){//Chroma444
1709 s->idsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
1710 s->idsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
1711 s->idsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
1712 s->idsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
1720 s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
1721 s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
1722 s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
1727 void ff_mpv_decode_mb(MpegEncContext *s, int16_t block[12][64])
1730 if(s->out_format == FMT_MPEG1) {
1731 mpv_decode_mb_internal(s, block, 1);
1734 mpv_decode_mb_internal(s, block, 0);
1737 void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
1739 ff_draw_horiz_band(s->avctx, s->current_picture.f,
1740 s->last_picture.f, y, h, s->picture_structure,
1741 s->first_field, s->low_delay);
1744 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
1745 const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
1746 const int uvlinesize = s->current_picture.f->linesize[1];
1747 const int mb_size= 4;
1749 s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
1750 s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
1751 s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
1752 s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
1753 s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
1754 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;
1755 //block_index is not used by mpeg2, so it is not affected by chroma_format
1757 s->dest[0] = s->current_picture.f->data[0] + ((s->mb_x - 1) << mb_size);
1758 s->dest[1] = s->current_picture.f->data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
1759 s->dest[2] = s->current_picture.f->data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
1761 if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
1763 if(s->picture_structure==PICT_FRAME){
1764 s->dest[0] += s->mb_y * linesize << mb_size;
1765 s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
1766 s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
1768 s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
1769 s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
1770 s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
1771 assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
1776 void ff_mpeg_flush(AVCodecContext *avctx){
1778 MpegEncContext *s = avctx->priv_data;
1780 if (!s || !s->picture)
1783 for (i = 0; i < MAX_PICTURE_COUNT; i++)
1784 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
1785 s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
1787 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
1788 ff_mpeg_unref_picture(s->avctx, &s->last_picture);
1789 ff_mpeg_unref_picture(s->avctx, &s->next_picture);
1791 s->mb_x= s->mb_y= 0;
1793 s->parse_context.state= -1;
1794 s->parse_context.frame_start_found= 0;
1795 s->parse_context.overread= 0;
1796 s->parse_context.overread_index= 0;
1797 s->parse_context.index= 0;
1798 s->parse_context.last_index= 0;
1799 s->bitstream_buffer_size=0;
1804 * set qscale and update qscale dependent variables.
1806 void ff_set_qscale(MpegEncContext * s, int qscale)
1810 else if (qscale > 31)
1814 s->chroma_qscale= s->chroma_qscale_table[qscale];
1816 s->y_dc_scale= s->y_dc_scale_table[ qscale ];
1817 s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
1820 void ff_mpv_report_decode_progress(MpegEncContext *s)
1822 if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
1823 ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);