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"
39 #include "mpegutils.h"
40 #include "mpegvideo.h"
43 #include "xvmc_internal.h"
47 static const uint8_t ff_default_chroma_qscale_table[32] = {
48 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
49 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
50 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
53 const uint8_t ff_mpeg1_dc_scale_table[128] = {
54 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
55 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
56 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
57 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
58 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
59 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
60 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
61 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
62 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
65 static const uint8_t mpeg2_dc_scale_table1[128] = {
66 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
67 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
68 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
69 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
70 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
71 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
72 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
73 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
74 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
77 static const uint8_t mpeg2_dc_scale_table2[128] = {
78 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
79 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
80 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
81 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
82 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
83 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
84 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
85 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
86 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
89 static const uint8_t mpeg2_dc_scale_table3[128] = {
90 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
91 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
92 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
93 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
94 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
95 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
96 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
97 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
98 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
101 const uint8_t *const ff_mpeg2_dc_scale_table[4] = {
102 ff_mpeg1_dc_scale_table,
103 mpeg2_dc_scale_table1,
104 mpeg2_dc_scale_table2,
105 mpeg2_dc_scale_table3,
108 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
109 int16_t *block, int n, int qscale)
111 int i, level, nCoeffs;
112 const uint16_t *quant_matrix;
114 nCoeffs= s->block_last_index[n];
117 block[0] = block[0] * s->y_dc_scale;
119 block[0] = block[0] * s->c_dc_scale;
120 /* XXX: only mpeg1 */
121 quant_matrix = s->intra_matrix;
122 for(i=1;i<=nCoeffs;i++) {
123 int j= s->intra_scantable.permutated[i];
128 level = (int)(level * qscale * quant_matrix[j]) >> 3;
129 level = (level - 1) | 1;
132 level = (int)(level * qscale * quant_matrix[j]) >> 3;
133 level = (level - 1) | 1;
140 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
141 int16_t *block, int n, int qscale)
143 int i, level, nCoeffs;
144 const uint16_t *quant_matrix;
146 nCoeffs= s->block_last_index[n];
148 quant_matrix = s->inter_matrix;
149 for(i=0; i<=nCoeffs; i++) {
150 int j= s->intra_scantable.permutated[i];
155 level = (((level << 1) + 1) * qscale *
156 ((int) (quant_matrix[j]))) >> 4;
157 level = (level - 1) | 1;
160 level = (((level << 1) + 1) * qscale *
161 ((int) (quant_matrix[j]))) >> 4;
162 level = (level - 1) | 1;
169 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
170 int16_t *block, int n, int qscale)
172 int i, level, nCoeffs;
173 const uint16_t *quant_matrix;
175 if(s->alternate_scan) nCoeffs= 63;
176 else nCoeffs= s->block_last_index[n];
179 block[0] = block[0] * s->y_dc_scale;
181 block[0] = block[0] * s->c_dc_scale;
182 quant_matrix = s->intra_matrix;
183 for(i=1;i<=nCoeffs;i++) {
184 int j= s->intra_scantable.permutated[i];
189 level = (int)(level * qscale * quant_matrix[j]) >> 3;
192 level = (int)(level * qscale * quant_matrix[j]) >> 3;
199 static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
200 int16_t *block, int n, int qscale)
202 int i, level, nCoeffs;
203 const uint16_t *quant_matrix;
206 if(s->alternate_scan) nCoeffs= 63;
207 else nCoeffs= s->block_last_index[n];
210 block[0] = block[0] * s->y_dc_scale;
212 block[0] = block[0] * s->c_dc_scale;
213 quant_matrix = s->intra_matrix;
214 for(i=1;i<=nCoeffs;i++) {
215 int j= s->intra_scantable.permutated[i];
220 level = (int)(level * qscale * quant_matrix[j]) >> 3;
223 level = (int)(level * qscale * quant_matrix[j]) >> 3;
232 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
233 int16_t *block, int n, int qscale)
235 int i, level, nCoeffs;
236 const uint16_t *quant_matrix;
239 if(s->alternate_scan) nCoeffs= 63;
240 else nCoeffs= s->block_last_index[n];
242 quant_matrix = s->inter_matrix;
243 for(i=0; i<=nCoeffs; i++) {
244 int j= s->intra_scantable.permutated[i];
249 level = (((level << 1) + 1) * qscale *
250 ((int) (quant_matrix[j]))) >> 4;
253 level = (((level << 1) + 1) * qscale *
254 ((int) (quant_matrix[j]))) >> 4;
263 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
264 int16_t *block, int n, int qscale)
266 int i, level, qmul, qadd;
269 assert(s->block_last_index[n]>=0);
275 block[0] = block[0] * s->y_dc_scale;
277 block[0] = block[0] * s->c_dc_scale;
278 qadd = (qscale - 1) | 1;
285 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
287 for(i=1; i<=nCoeffs; i++) {
291 level = level * qmul - qadd;
293 level = level * qmul + qadd;
300 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
301 int16_t *block, int n, int qscale)
303 int i, level, qmul, qadd;
306 assert(s->block_last_index[n]>=0);
308 qadd = (qscale - 1) | 1;
311 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
313 for(i=0; i<=nCoeffs; i++) {
317 level = level * qmul - qadd;
319 level = level * qmul + qadd;
326 static void mpeg_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
328 int mb_x, int mb_y, int mb_intra, int mb_skipped)
330 MpegEncContext *s = opaque;
333 s->mv_type = mv_type;
334 s->mb_intra = mb_intra;
335 s->mb_skipped = mb_skipped;
338 memcpy(s->mv, mv, sizeof(*mv));
340 ff_init_block_index(s);
341 ff_update_block_index(s);
343 s->dsp.clear_blocks(s->block[0]);
345 s->dest[0] = s->current_picture.f.data[0] + (s->mb_y * 16 * s->linesize) + s->mb_x * 16;
346 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);
347 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);
350 ff_MPV_decode_mb(s, s->block);
353 /* init common dct for both encoder and decoder */
354 av_cold int ff_dct_common_init(MpegEncContext *s)
356 ff_dsputil_init(&s->dsp, s->avctx);
357 ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
358 ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
360 s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
361 s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
362 s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
363 s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
364 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
365 if (s->flags & CODEC_FLAG_BITEXACT)
366 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
367 s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
370 ff_MPV_common_init_arm(s);
372 ff_MPV_common_init_ppc(s);
374 ff_MPV_common_init_x86(s);
376 /* load & permutate scantables
377 * note: only wmv uses different ones
379 if (s->alternate_scan) {
380 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
381 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
383 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
384 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
386 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
387 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
392 static int frame_size_alloc(MpegEncContext *s, int linesize)
394 int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
396 // edge emu needs blocksize + filter length - 1
397 // (= 17x17 for halfpel / 21x21 for h264)
398 // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9
399 // at uvlinesize. It supports only YUV420 so 24x24 is enough
400 // linesize * interlaced * MBsize
401 FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, alloc_size * 2 * 24,
404 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, alloc_size * 2 * 16 * 3,
406 s->me.temp = s->me.scratchpad;
407 s->rd_scratchpad = s->me.scratchpad;
408 s->b_scratchpad = s->me.scratchpad;
409 s->obmc_scratchpad = s->me.scratchpad + 16;
413 av_freep(&s->edge_emu_buffer);
414 return AVERROR(ENOMEM);
418 * Allocate a frame buffer
420 static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
422 int edges_needed = av_codec_is_encoder(s->avctx->codec);
426 if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
427 s->codec_id != AV_CODEC_ID_VC1IMAGE &&
428 s->codec_id != AV_CODEC_ID_MSS2) {
430 pic->f.width = s->avctx->width + 2 * EDGE_WIDTH;
431 pic->f.height = s->avctx->height + 2 * EDGE_WIDTH;
434 r = ff_thread_get_buffer(s->avctx, &pic->tf,
435 pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
437 pic->f.width = s->avctx->width;
438 pic->f.height = s->avctx->height;
439 pic->f.format = s->avctx->pix_fmt;
440 r = avcodec_default_get_buffer2(s->avctx, &pic->f, 0);
443 if (r < 0 || !pic->f.buf[0]) {
444 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
451 for (i = 0; pic->f.data[i]; i++) {
452 int offset = (EDGE_WIDTH >> (i ? s->chroma_y_shift : 0)) *
454 (EDGE_WIDTH >> (i ? s->chroma_x_shift : 0));
455 pic->f.data[i] += offset;
457 pic->f.width = s->avctx->width;
458 pic->f.height = s->avctx->height;
461 if (s->avctx->hwaccel) {
462 assert(!pic->hwaccel_picture_private);
463 if (s->avctx->hwaccel->priv_data_size) {
464 pic->hwaccel_priv_buf = av_buffer_allocz(s->avctx->hwaccel->priv_data_size);
465 if (!pic->hwaccel_priv_buf) {
466 av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
469 pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
473 if (s->linesize && (s->linesize != pic->f.linesize[0] ||
474 s->uvlinesize != pic->f.linesize[1])) {
475 av_log(s->avctx, AV_LOG_ERROR,
476 "get_buffer() failed (stride changed)\n");
477 ff_mpeg_unref_picture(s, pic);
481 if (pic->f.linesize[1] != pic->f.linesize[2]) {
482 av_log(s->avctx, AV_LOG_ERROR,
483 "get_buffer() failed (uv stride mismatch)\n");
484 ff_mpeg_unref_picture(s, pic);
488 if (!s->edge_emu_buffer &&
489 (ret = frame_size_alloc(s, pic->f.linesize[0])) < 0) {
490 av_log(s->avctx, AV_LOG_ERROR,
491 "get_buffer() failed to allocate context scratch buffers.\n");
492 ff_mpeg_unref_picture(s, pic);
499 void ff_free_picture_tables(Picture *pic)
503 av_buffer_unref(&pic->mb_var_buf);
504 av_buffer_unref(&pic->mc_mb_var_buf);
505 av_buffer_unref(&pic->mb_mean_buf);
506 av_buffer_unref(&pic->mbskip_table_buf);
507 av_buffer_unref(&pic->qscale_table_buf);
508 av_buffer_unref(&pic->mb_type_buf);
510 for (i = 0; i < 2; i++) {
511 av_buffer_unref(&pic->motion_val_buf[i]);
512 av_buffer_unref(&pic->ref_index_buf[i]);
516 static int alloc_picture_tables(MpegEncContext *s, Picture *pic)
518 const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;
519 const int mb_array_size = s->mb_stride * s->mb_height;
520 const int b8_array_size = s->b8_stride * s->mb_height * 2;
524 pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
525 pic->qscale_table_buf = av_buffer_allocz(big_mb_num + s->mb_stride);
526 pic->mb_type_buf = av_buffer_allocz((big_mb_num + s->mb_stride) *
528 if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
529 return AVERROR(ENOMEM);
532 pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
533 pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
534 pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
535 if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
536 return AVERROR(ENOMEM);
539 if (s->out_format == FMT_H263 || s->encoding) {
540 int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
541 int ref_index_size = 4 * mb_array_size;
543 for (i = 0; mv_size && i < 2; i++) {
544 pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
545 pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
546 if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
547 return AVERROR(ENOMEM);
554 static int make_tables_writable(Picture *pic)
557 #define MAKE_WRITABLE(table) \
560 (ret = av_buffer_make_writable(&pic->table)) < 0)\
564 MAKE_WRITABLE(mb_var_buf);
565 MAKE_WRITABLE(mc_mb_var_buf);
566 MAKE_WRITABLE(mb_mean_buf);
567 MAKE_WRITABLE(mbskip_table_buf);
568 MAKE_WRITABLE(qscale_table_buf);
569 MAKE_WRITABLE(mb_type_buf);
571 for (i = 0; i < 2; i++) {
572 MAKE_WRITABLE(motion_val_buf[i]);
573 MAKE_WRITABLE(ref_index_buf[i]);
580 * Allocate a Picture.
581 * The pixels are allocated/set by calling get_buffer() if shared = 0
583 int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
588 assert(pic->f.data[0]);
591 assert(!pic->f.buf[0]);
593 if (alloc_frame_buffer(s, pic) < 0)
596 s->linesize = pic->f.linesize[0];
597 s->uvlinesize = pic->f.linesize[1];
600 if (!pic->qscale_table_buf)
601 ret = alloc_picture_tables(s, pic);
603 ret = make_tables_writable(pic);
608 pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
609 pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
610 pic->mb_mean = pic->mb_mean_buf->data;
613 pic->mbskip_table = pic->mbskip_table_buf->data;
614 pic->qscale_table = pic->qscale_table_buf->data + 2 * s->mb_stride + 1;
615 pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;
617 if (pic->motion_val_buf[0]) {
618 for (i = 0; i < 2; i++) {
619 pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
620 pic->ref_index[i] = pic->ref_index_buf[i]->data;
626 av_log(s->avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
627 ff_mpeg_unref_picture(s, pic);
628 ff_free_picture_tables(pic);
629 return AVERROR(ENOMEM);
633 * Deallocate a picture.
635 void ff_mpeg_unref_picture(MpegEncContext *s, Picture *pic)
637 int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
640 /* WM Image / Screen codecs allocate internal buffers with different
641 * dimensions / colorspaces; ignore user-defined callbacks for these. */
642 if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
643 s->codec_id != AV_CODEC_ID_VC1IMAGE &&
644 s->codec_id != AV_CODEC_ID_MSS2)
645 ff_thread_release_buffer(s->avctx, &pic->tf);
647 av_frame_unref(&pic->f);
649 av_buffer_unref(&pic->hwaccel_priv_buf);
651 if (pic->needs_realloc)
652 ff_free_picture_tables(pic);
654 memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
657 static int update_picture_tables(Picture *dst, Picture *src)
661 #define UPDATE_TABLE(table)\
664 (!dst->table || dst->table->buffer != src->table->buffer)) {\
665 av_buffer_unref(&dst->table);\
666 dst->table = av_buffer_ref(src->table);\
668 ff_free_picture_tables(dst);\
669 return AVERROR(ENOMEM);\
674 UPDATE_TABLE(mb_var_buf);
675 UPDATE_TABLE(mc_mb_var_buf);
676 UPDATE_TABLE(mb_mean_buf);
677 UPDATE_TABLE(mbskip_table_buf);
678 UPDATE_TABLE(qscale_table_buf);
679 UPDATE_TABLE(mb_type_buf);
680 for (i = 0; i < 2; i++) {
681 UPDATE_TABLE(motion_val_buf[i]);
682 UPDATE_TABLE(ref_index_buf[i]);
685 dst->mb_var = src->mb_var;
686 dst->mc_mb_var = src->mc_mb_var;
687 dst->mb_mean = src->mb_mean;
688 dst->mbskip_table = src->mbskip_table;
689 dst->qscale_table = src->qscale_table;
690 dst->mb_type = src->mb_type;
691 for (i = 0; i < 2; i++) {
692 dst->motion_val[i] = src->motion_val[i];
693 dst->ref_index[i] = src->ref_index[i];
699 int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src)
703 av_assert0(!dst->f.buf[0]);
704 av_assert0(src->f.buf[0]);
708 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
712 ret = update_picture_tables(dst, src);
716 if (src->hwaccel_picture_private) {
717 dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
718 if (!dst->hwaccel_priv_buf)
720 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
723 dst->field_picture = src->field_picture;
724 dst->mb_var_sum = src->mb_var_sum;
725 dst->mc_mb_var_sum = src->mc_mb_var_sum;
726 dst->b_frame_score = src->b_frame_score;
727 dst->needs_realloc = src->needs_realloc;
728 dst->reference = src->reference;
729 dst->shared = src->shared;
733 ff_mpeg_unref_picture(s, dst);
737 static void exchange_uv(MpegEncContext *s)
742 s->pblocks[4] = s->pblocks[5];
746 static int init_duplicate_context(MpegEncContext *s)
748 int y_size = s->b8_stride * (2 * s->mb_height + 1);
749 int c_size = s->mb_stride * (s->mb_height + 1);
750 int yc_size = y_size + 2 * c_size;
758 s->obmc_scratchpad = NULL;
761 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
762 ME_MAP_SIZE * sizeof(uint32_t), fail)
763 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
764 ME_MAP_SIZE * sizeof(uint32_t), fail)
765 if (s->avctx->noise_reduction) {
766 FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
767 2 * 64 * sizeof(int), fail)
770 FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
771 s->block = s->blocks[0];
773 for (i = 0; i < 12; i++) {
774 s->pblocks[i] = &s->block[i];
776 if (s->avctx->codec_tag == AV_RL32("VCR2"))
779 if (s->out_format == FMT_H263) {
781 FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
782 yc_size * sizeof(int16_t) * 16, fail);
783 s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
784 s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
785 s->ac_val[2] = s->ac_val[1] + c_size;
790 return -1; // free() through ff_MPV_common_end()
793 static void free_duplicate_context(MpegEncContext *s)
798 av_freep(&s->edge_emu_buffer);
799 av_freep(&s->me.scratchpad);
803 s->obmc_scratchpad = NULL;
805 av_freep(&s->dct_error_sum);
806 av_freep(&s->me.map);
807 av_freep(&s->me.score_map);
808 av_freep(&s->blocks);
809 av_freep(&s->ac_val_base);
813 static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
815 #define COPY(a) bak->a = src->a
816 COPY(edge_emu_buffer);
821 COPY(obmc_scratchpad);
828 COPY(me.map_generation);
840 int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
844 // FIXME copy only needed parts
846 backup_duplicate_context(&bak, dst);
847 memcpy(dst, src, sizeof(MpegEncContext));
848 backup_duplicate_context(dst, &bak);
849 for (i = 0; i < 12; i++) {
850 dst->pblocks[i] = &dst->block[i];
852 if (dst->avctx->codec_tag == AV_RL32("VCR2"))
854 if (!dst->edge_emu_buffer &&
855 (ret = frame_size_alloc(dst, dst->linesize)) < 0) {
856 av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
857 "scratch buffers.\n");
860 // STOP_TIMER("update_duplicate_context")
861 // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
865 int ff_mpeg_update_thread_context(AVCodecContext *dst,
866 const AVCodecContext *src)
869 MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
871 if (dst == src || !s1->context_initialized)
874 // FIXME can parameters change on I-frames?
875 // in that case dst may need a reinit
876 if (!s->context_initialized) {
877 memcpy(s, s1, sizeof(MpegEncContext));
880 s->bitstream_buffer = NULL;
881 s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
883 ff_MPV_common_init(s);
886 if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
888 s->context_reinit = 0;
889 s->height = s1->height;
890 s->width = s1->width;
891 if ((err = ff_MPV_common_frame_size_change(s)) < 0)
895 s->avctx->coded_height = s1->avctx->coded_height;
896 s->avctx->coded_width = s1->avctx->coded_width;
897 s->avctx->width = s1->avctx->width;
898 s->avctx->height = s1->avctx->height;
900 s->coded_picture_number = s1->coded_picture_number;
901 s->picture_number = s1->picture_number;
903 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
904 ff_mpeg_unref_picture(s, &s->picture[i]);
905 if (s1->picture[i].f.buf[0] &&
906 (ret = ff_mpeg_ref_picture(s, &s->picture[i], &s1->picture[i])) < 0)
910 #define UPDATE_PICTURE(pic)\
912 ff_mpeg_unref_picture(s, &s->pic);\
913 if (s1->pic.f.buf[0])\
914 ret = ff_mpeg_ref_picture(s, &s->pic, &s1->pic);\
916 ret = update_picture_tables(&s->pic, &s1->pic);\
921 UPDATE_PICTURE(current_picture);
922 UPDATE_PICTURE(last_picture);
923 UPDATE_PICTURE(next_picture);
925 s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
926 s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
927 s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
929 // Error/bug resilience
930 s->next_p_frame_damaged = s1->next_p_frame_damaged;
931 s->workaround_bugs = s1->workaround_bugs;
934 memcpy(&s->last_time_base, &s1->last_time_base,
935 (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
936 (char *) &s1->last_time_base);
939 s->max_b_frames = s1->max_b_frames;
940 s->low_delay = s1->low_delay;
941 s->droppable = s1->droppable;
943 // DivX handling (doesn't work)
944 s->divx_packed = s1->divx_packed;
946 if (s1->bitstream_buffer) {
947 if (s1->bitstream_buffer_size +
948 FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
949 av_fast_malloc(&s->bitstream_buffer,
950 &s->allocated_bitstream_buffer_size,
951 s1->allocated_bitstream_buffer_size);
952 s->bitstream_buffer_size = s1->bitstream_buffer_size;
953 memcpy(s->bitstream_buffer, s1->bitstream_buffer,
954 s1->bitstream_buffer_size);
955 memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
956 FF_INPUT_BUFFER_PADDING_SIZE);
959 // linesize dependend scratch buffer allocation
960 if (!s->edge_emu_buffer)
962 if (frame_size_alloc(s, s1->linesize) < 0) {
963 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
964 "scratch buffers.\n");
965 return AVERROR(ENOMEM);
968 av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
969 "be allocated due to unknown size.\n");
973 // MPEG2/interlacing info
974 memcpy(&s->progressive_sequence, &s1->progressive_sequence,
975 (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
977 if (!s1->first_field) {
978 s->last_pict_type = s1->pict_type;
979 if (s1->current_picture_ptr)
980 s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f.quality;
987 * Set the given MpegEncContext to common defaults
988 * (same for encoding and decoding).
989 * The changed fields will not depend upon the
990 * prior state of the MpegEncContext.
992 void ff_MPV_common_defaults(MpegEncContext *s)
994 s->y_dc_scale_table =
995 s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
996 s->chroma_qscale_table = ff_default_chroma_qscale_table;
997 s->progressive_frame = 1;
998 s->progressive_sequence = 1;
999 s->picture_structure = PICT_FRAME;
1001 s->coded_picture_number = 0;
1002 s->picture_number = 0;
1007 s->slice_context_count = 1;
1011 * Set the given MpegEncContext to defaults for decoding.
1012 * the changed fields will not depend upon
1013 * the prior state of the MpegEncContext.
1015 void ff_MPV_decode_defaults(MpegEncContext *s)
1017 ff_MPV_common_defaults(s);
1020 static int init_er(MpegEncContext *s)
1022 ERContext *er = &s->er;
1023 int mb_array_size = s->mb_height * s->mb_stride;
1026 er->avctx = s->avctx;
1029 er->mb_index2xy = s->mb_index2xy;
1030 er->mb_num = s->mb_num;
1031 er->mb_width = s->mb_width;
1032 er->mb_height = s->mb_height;
1033 er->mb_stride = s->mb_stride;
1034 er->b8_stride = s->b8_stride;
1036 er->er_temp_buffer = av_malloc(s->mb_height * s->mb_stride);
1037 er->error_status_table = av_mallocz(mb_array_size);
1038 if (!er->er_temp_buffer || !er->error_status_table)
1041 er->mbskip_table = s->mbskip_table;
1042 er->mbintra_table = s->mbintra_table;
1044 for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++)
1045 er->dc_val[i] = s->dc_val[i];
1047 er->decode_mb = mpeg_er_decode_mb;
1052 av_freep(&er->er_temp_buffer);
1053 av_freep(&er->error_status_table);
1054 return AVERROR(ENOMEM);
1058 * Initialize and allocates MpegEncContext fields dependent on the resolution.
1060 static int init_context_frame(MpegEncContext *s)
1062 int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
1064 s->mb_width = (s->width + 15) / 16;
1065 s->mb_stride = s->mb_width + 1;
1066 s->b8_stride = s->mb_width * 2 + 1;
1067 s->b4_stride = s->mb_width * 4 + 1;
1068 mb_array_size = s->mb_height * s->mb_stride;
1069 mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
1071 /* set default edge pos, will be overriden
1072 * in decode_header if needed */
1073 s->h_edge_pos = s->mb_width * 16;
1074 s->v_edge_pos = s->mb_height * 16;
1076 s->mb_num = s->mb_width * s->mb_height;
1081 s->block_wrap[3] = s->b8_stride;
1083 s->block_wrap[5] = s->mb_stride;
1085 y_size = s->b8_stride * (2 * s->mb_height + 1);
1086 c_size = s->mb_stride * (s->mb_height + 1);
1087 yc_size = y_size + 2 * c_size;
1089 FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
1090 fail); // error ressilience code looks cleaner with this
1091 for (y = 0; y < s->mb_height; y++)
1092 for (x = 0; x < s->mb_width; x++)
1093 s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
1095 s->mb_index2xy[s->mb_height * s->mb_width] =
1096 (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
1099 /* Allocate MV tables */
1100 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,
1101 mv_table_size * 2 * sizeof(int16_t), fail);
1102 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,
1103 mv_table_size * 2 * sizeof(int16_t), fail);
1104 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,
1105 mv_table_size * 2 * sizeof(int16_t), fail);
1106 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,
1107 mv_table_size * 2 * sizeof(int16_t), fail);
1108 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,
1109 mv_table_size * 2 * sizeof(int16_t), fail);
1110 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,
1111 mv_table_size * 2 * sizeof(int16_t), fail);
1112 s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
1113 s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
1114 s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
1115 s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base +
1117 s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base +
1119 s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
1121 /* Allocate MB type table */
1122 FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *
1123 sizeof(uint16_t), fail); // needed for encoding
1125 FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *
1128 FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
1129 mb_array_size * sizeof(float), fail);
1130 FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
1131 mb_array_size * sizeof(float), fail);
1135 if (s->codec_id == AV_CODEC_ID_MPEG4 ||
1136 (s->flags & CODEC_FLAG_INTERLACED_ME)) {
1137 /* interlaced direct mode decoding tables */
1138 for (i = 0; i < 2; i++) {
1140 for (j = 0; j < 2; j++) {
1141 for (k = 0; k < 2; k++) {
1142 FF_ALLOCZ_OR_GOTO(s->avctx,
1143 s->b_field_mv_table_base[i][j][k],
1144 mv_table_size * 2 * sizeof(int16_t),
1146 s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
1149 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j],
1150 mb_array_size * 2 * sizeof(uint8_t), fail);
1151 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j],
1152 mv_table_size * 2 * sizeof(int16_t), fail);
1153 s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]
1156 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i],
1157 mb_array_size * 2 * sizeof(uint8_t), fail);
1160 if (s->out_format == FMT_H263) {
1162 FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
1163 s->coded_block = s->coded_block_base + s->b8_stride + 1;
1165 /* cbp, ac_pred, pred_dir */
1166 FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table,
1167 mb_array_size * sizeof(uint8_t), fail);
1168 FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table,
1169 mb_array_size * sizeof(uint8_t), fail);
1172 if (s->h263_pred || s->h263_plus || !s->encoding) {
1174 // MN: we need these for error resilience of intra-frames
1175 FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base,
1176 yc_size * sizeof(int16_t), fail);
1177 s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
1178 s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
1179 s->dc_val[2] = s->dc_val[1] + c_size;
1180 for (i = 0; i < yc_size; i++)
1181 s->dc_val_base[i] = 1024;
1184 /* which mb is a intra block */
1185 FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
1186 memset(s->mbintra_table, 1, mb_array_size);
1188 /* init macroblock skip table */
1189 FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
1190 // Note the + 1 is for a quicker mpeg4 slice_end detection
1194 return AVERROR(ENOMEM);
1198 * init common structure for both encoder and decoder.
1199 * this assumes that some variables like width/height are already set
1201 av_cold int ff_MPV_common_init(MpegEncContext *s)
1204 int nb_slices = (HAVE_THREADS &&
1205 s->avctx->active_thread_type & FF_THREAD_SLICE) ?
1206 s->avctx->thread_count : 1;
1208 if (s->encoding && s->avctx->slices)
1209 nb_slices = s->avctx->slices;
1211 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
1212 s->mb_height = (s->height + 31) / 32 * 2;
1214 s->mb_height = (s->height + 15) / 16;
1216 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
1217 av_log(s->avctx, AV_LOG_ERROR,
1218 "decoding to AV_PIX_FMT_NONE is not supported.\n");
1222 if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
1225 max_slices = FFMIN(MAX_THREADS, s->mb_height);
1227 max_slices = MAX_THREADS;
1228 av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
1229 " reducing to %d\n", nb_slices, max_slices);
1230 nb_slices = max_slices;
1233 if ((s->width || s->height) &&
1234 av_image_check_size(s->width, s->height, 0, s->avctx))
1237 ff_dct_common_init(s);
1239 s->flags = s->avctx->flags;
1240 s->flags2 = s->avctx->flags2;
1242 /* set chroma shifts */
1243 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
1245 &s->chroma_y_shift);
1247 /* convert fourcc to upper case */
1248 s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
1250 s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag);
1252 FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
1253 MAX_PICTURE_COUNT * sizeof(Picture), fail);
1254 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1255 av_frame_unref(&s->picture[i].f);
1257 memset(&s->next_picture, 0, sizeof(s->next_picture));
1258 memset(&s->last_picture, 0, sizeof(s->last_picture));
1259 memset(&s->current_picture, 0, sizeof(s->current_picture));
1260 av_frame_unref(&s->next_picture.f);
1261 av_frame_unref(&s->last_picture.f);
1262 av_frame_unref(&s->current_picture.f);
1264 if (s->width && s->height) {
1265 if (init_context_frame(s))
1268 s->parse_context.state = -1;
1271 s->context_initialized = 1;
1272 s->thread_context[0] = s;
1274 if (s->width && s->height) {
1275 if (nb_slices > 1) {
1276 for (i = 1; i < nb_slices; i++) {
1277 s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
1278 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
1281 for (i = 0; i < nb_slices; i++) {
1282 if (init_duplicate_context(s->thread_context[i]) < 0)
1284 s->thread_context[i]->start_mb_y =
1285 (s->mb_height * (i) + nb_slices / 2) / nb_slices;
1286 s->thread_context[i]->end_mb_y =
1287 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
1290 if (init_duplicate_context(s) < 0)
1293 s->end_mb_y = s->mb_height;
1295 s->slice_context_count = nb_slices;
1300 ff_MPV_common_end(s);
1305 * Frees and resets MpegEncContext fields depending on the resolution.
1306 * Is used during resolution changes to avoid a full reinitialization of the
1309 static int free_context_frame(MpegEncContext *s)
1313 av_freep(&s->mb_type);
1314 av_freep(&s->p_mv_table_base);
1315 av_freep(&s->b_forw_mv_table_base);
1316 av_freep(&s->b_back_mv_table_base);
1317 av_freep(&s->b_bidir_forw_mv_table_base);
1318 av_freep(&s->b_bidir_back_mv_table_base);
1319 av_freep(&s->b_direct_mv_table_base);
1320 s->p_mv_table = NULL;
1321 s->b_forw_mv_table = NULL;
1322 s->b_back_mv_table = NULL;
1323 s->b_bidir_forw_mv_table = NULL;
1324 s->b_bidir_back_mv_table = NULL;
1325 s->b_direct_mv_table = NULL;
1326 for (i = 0; i < 2; i++) {
1327 for (j = 0; j < 2; j++) {
1328 for (k = 0; k < 2; k++) {
1329 av_freep(&s->b_field_mv_table_base[i][j][k]);
1330 s->b_field_mv_table[i][j][k] = NULL;
1332 av_freep(&s->b_field_select_table[i][j]);
1333 av_freep(&s->p_field_mv_table_base[i][j]);
1334 s->p_field_mv_table[i][j] = NULL;
1336 av_freep(&s->p_field_select_table[i]);
1339 av_freep(&s->dc_val_base);
1340 av_freep(&s->coded_block_base);
1341 av_freep(&s->mbintra_table);
1342 av_freep(&s->cbp_table);
1343 av_freep(&s->pred_dir_table);
1345 av_freep(&s->mbskip_table);
1347 av_freep(&s->er.error_status_table);
1348 av_freep(&s->er.er_temp_buffer);
1349 av_freep(&s->mb_index2xy);
1350 av_freep(&s->lambda_table);
1351 av_freep(&s->cplx_tab);
1352 av_freep(&s->bits_tab);
1354 s->linesize = s->uvlinesize = 0;
1359 int ff_MPV_common_frame_size_change(MpegEncContext *s)
1363 if (s->slice_context_count > 1) {
1364 for (i = 0; i < s->slice_context_count; i++) {
1365 free_duplicate_context(s->thread_context[i]);
1367 for (i = 1; i < s->slice_context_count; i++) {
1368 av_freep(&s->thread_context[i]);
1371 free_duplicate_context(s);
1373 if ((err = free_context_frame(s)) < 0)
1377 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1378 s->picture[i].needs_realloc = 1;
1381 s->last_picture_ptr =
1382 s->next_picture_ptr =
1383 s->current_picture_ptr = NULL;
1386 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
1387 s->mb_height = (s->height + 31) / 32 * 2;
1389 s->mb_height = (s->height + 15) / 16;
1391 if ((s->width || s->height) &&
1392 av_image_check_size(s->width, s->height, 0, s->avctx))
1393 return AVERROR_INVALIDDATA;
1395 if ((err = init_context_frame(s)))
1398 s->thread_context[0] = s;
1400 if (s->width && s->height) {
1401 int nb_slices = s->slice_context_count;
1402 if (nb_slices > 1) {
1403 for (i = 1; i < nb_slices; i++) {
1404 s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
1405 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
1408 for (i = 0; i < nb_slices; i++) {
1409 if (init_duplicate_context(s->thread_context[i]) < 0)
1411 s->thread_context[i]->start_mb_y =
1412 (s->mb_height * (i) + nb_slices / 2) / nb_slices;
1413 s->thread_context[i]->end_mb_y =
1414 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
1417 if (init_duplicate_context(s) < 0)
1420 s->end_mb_y = s->mb_height;
1422 s->slice_context_count = nb_slices;
1427 ff_MPV_common_end(s);
1431 /* init common structure for both encoder and decoder */
1432 void ff_MPV_common_end(MpegEncContext *s)
1436 if (s->slice_context_count > 1) {
1437 for (i = 0; i < s->slice_context_count; i++) {
1438 free_duplicate_context(s->thread_context[i]);
1440 for (i = 1; i < s->slice_context_count; i++) {
1441 av_freep(&s->thread_context[i]);
1443 s->slice_context_count = 1;
1444 } else free_duplicate_context(s);
1446 av_freep(&s->parse_context.buffer);
1447 s->parse_context.buffer_size = 0;
1449 av_freep(&s->bitstream_buffer);
1450 s->allocated_bitstream_buffer_size = 0;
1453 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1454 ff_free_picture_tables(&s->picture[i]);
1455 ff_mpeg_unref_picture(s, &s->picture[i]);
1458 av_freep(&s->picture);
1459 ff_free_picture_tables(&s->last_picture);
1460 ff_mpeg_unref_picture(s, &s->last_picture);
1461 ff_free_picture_tables(&s->current_picture);
1462 ff_mpeg_unref_picture(s, &s->current_picture);
1463 ff_free_picture_tables(&s->next_picture);
1464 ff_mpeg_unref_picture(s, &s->next_picture);
1466 free_context_frame(s);
1468 s->context_initialized = 0;
1469 s->last_picture_ptr =
1470 s->next_picture_ptr =
1471 s->current_picture_ptr = NULL;
1472 s->linesize = s->uvlinesize = 0;
1475 av_cold void ff_init_rl(RLTable *rl,
1476 uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
1478 int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
1479 uint8_t index_run[MAX_RUN + 1];
1480 int last, run, level, start, end, i;
1482 /* If table is static, we can quit if rl->max_level[0] is not NULL */
1483 if (static_store && rl->max_level[0])
1486 /* compute max_level[], max_run[] and index_run[] */
1487 for (last = 0; last < 2; last++) {
1496 memset(max_level, 0, MAX_RUN + 1);
1497 memset(max_run, 0, MAX_LEVEL + 1);
1498 memset(index_run, rl->n, MAX_RUN + 1);
1499 for (i = start; i < end; i++) {
1500 run = rl->table_run[i];
1501 level = rl->table_level[i];
1502 if (index_run[run] == rl->n)
1504 if (level > max_level[run])
1505 max_level[run] = level;
1506 if (run > max_run[level])
1507 max_run[level] = run;
1510 rl->max_level[last] = static_store[last];
1512 rl->max_level[last] = av_malloc(MAX_RUN + 1);
1513 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
1515 rl->max_run[last] = static_store[last] + MAX_RUN + 1;
1517 rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
1518 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
1520 rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
1522 rl->index_run[last] = av_malloc(MAX_RUN + 1);
1523 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
1527 av_cold void ff_init_vlc_rl(RLTable *rl)
1531 for (q = 0; q < 32; q++) {
1533 int qadd = (q - 1) | 1;
1539 for (i = 0; i < rl->vlc.table_size; i++) {
1540 int code = rl->vlc.table[i][0];
1541 int len = rl->vlc.table[i][1];
1544 if (len == 0) { // illegal code
1547 } else if (len < 0) { // more bits needed
1551 if (code == rl->n) { // esc
1555 run = rl->table_run[code] + 1;
1556 level = rl->table_level[code] * qmul + qadd;
1557 if (code >= rl->last) run += 192;
1560 rl->rl_vlc[q][i].len = len;
1561 rl->rl_vlc[q][i].level = level;
1562 rl->rl_vlc[q][i].run = run;
1567 static void release_unused_pictures(MpegEncContext *s)
1571 /* release non reference frames */
1572 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1573 if (!s->picture[i].reference)
1574 ff_mpeg_unref_picture(s, &s->picture[i]);
1578 static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
1580 if (pic->f.buf[0] == NULL)
1582 if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
1587 static int find_unused_picture(MpegEncContext *s, int shared)
1592 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1593 if (s->picture[i].f.buf[0] == NULL)
1597 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1598 if (pic_is_unused(s, &s->picture[i]))
1603 return AVERROR_INVALIDDATA;
1606 int ff_find_unused_picture(MpegEncContext *s, int shared)
1608 int ret = find_unused_picture(s, shared);
1610 if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
1611 if (s->picture[ret].needs_realloc) {
1612 s->picture[ret].needs_realloc = 0;
1613 ff_free_picture_tables(&s->picture[ret]);
1614 ff_mpeg_unref_picture(s, &s->picture[ret]);
1621 * generic function called after decoding
1622 * the header and before a frame is decoded.
1624 int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
1630 /* mark & release old frames */
1631 if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1632 s->last_picture_ptr != s->next_picture_ptr &&
1633 s->last_picture_ptr->f.buf[0]) {
1634 ff_mpeg_unref_picture(s, s->last_picture_ptr);
1637 /* release forgotten pictures */
1638 /* if (mpeg124/h263) */
1639 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1640 if (&s->picture[i] != s->last_picture_ptr &&
1641 &s->picture[i] != s->next_picture_ptr &&
1642 s->picture[i].reference && !s->picture[i].needs_realloc) {
1643 if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1644 av_log(avctx, AV_LOG_ERROR,
1645 "releasing zombie picture\n");
1646 ff_mpeg_unref_picture(s, &s->picture[i]);
1650 ff_mpeg_unref_picture(s, &s->current_picture);
1652 release_unused_pictures(s);
1654 if (s->current_picture_ptr &&
1655 s->current_picture_ptr->f.buf[0] == NULL) {
1656 // we already have a unused image
1657 // (maybe it was set before reading the header)
1658 pic = s->current_picture_ptr;
1660 i = ff_find_unused_picture(s, 0);
1662 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1665 pic = &s->picture[i];
1669 if (!s->droppable) {
1670 if (s->pict_type != AV_PICTURE_TYPE_B)
1674 pic->f.coded_picture_number = s->coded_picture_number++;
1676 if (ff_alloc_picture(s, pic, 0) < 0)
1679 s->current_picture_ptr = pic;
1680 // FIXME use only the vars from current_pic
1681 s->current_picture_ptr->f.top_field_first = s->top_field_first;
1682 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
1683 s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1684 if (s->picture_structure != PICT_FRAME)
1685 s->current_picture_ptr->f.top_field_first =
1686 (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
1688 s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame &&
1689 !s->progressive_sequence;
1690 s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
1692 s->current_picture_ptr->f.pict_type = s->pict_type;
1693 // if (s->flags && CODEC_FLAG_QSCALE)
1694 // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
1695 s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1697 if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
1698 s->current_picture_ptr)) < 0)
1701 if (s->pict_type != AV_PICTURE_TYPE_B) {
1702 s->last_picture_ptr = s->next_picture_ptr;
1704 s->next_picture_ptr = s->current_picture_ptr;
1706 av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
1707 s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
1708 s->last_picture_ptr ? s->last_picture_ptr->f.data[0] : NULL,
1709 s->next_picture_ptr ? s->next_picture_ptr->f.data[0] : NULL,
1710 s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
1711 s->pict_type, s->droppable);
1713 if ((s->last_picture_ptr == NULL ||
1714 s->last_picture_ptr->f.buf[0] == NULL) &&
1715 (s->pict_type != AV_PICTURE_TYPE_I ||
1716 s->picture_structure != PICT_FRAME)) {
1717 int h_chroma_shift, v_chroma_shift;
1718 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
1719 &h_chroma_shift, &v_chroma_shift);
1720 if (s->pict_type != AV_PICTURE_TYPE_I)
1721 av_log(avctx, AV_LOG_ERROR,
1722 "warning: first frame is no keyframe\n");
1723 else if (s->picture_structure != PICT_FRAME)
1724 av_log(avctx, AV_LOG_INFO,
1725 "allocate dummy last picture for field based first keyframe\n");
1727 /* Allocate a dummy frame */
1728 i = ff_find_unused_picture(s, 0);
1730 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1733 s->last_picture_ptr = &s->picture[i];
1735 s->last_picture_ptr->reference = 3;
1736 s->last_picture_ptr->f.pict_type = AV_PICTURE_TYPE_I;
1738 if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
1739 s->last_picture_ptr = NULL;
1743 memset(s->last_picture_ptr->f.data[0], 0,
1744 avctx->height * s->last_picture_ptr->f.linesize[0]);
1745 memset(s->last_picture_ptr->f.data[1], 0x80,
1746 (avctx->height >> v_chroma_shift) *
1747 s->last_picture_ptr->f.linesize[1]);
1748 memset(s->last_picture_ptr->f.data[2], 0x80,
1749 (avctx->height >> v_chroma_shift) *
1750 s->last_picture_ptr->f.linesize[2]);
1752 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
1753 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
1755 if ((s->next_picture_ptr == NULL ||
1756 s->next_picture_ptr->f.buf[0] == NULL) &&
1757 s->pict_type == AV_PICTURE_TYPE_B) {
1758 /* Allocate a dummy frame */
1759 i = ff_find_unused_picture(s, 0);
1761 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1764 s->next_picture_ptr = &s->picture[i];
1766 s->next_picture_ptr->reference = 3;
1767 s->next_picture_ptr->f.pict_type = AV_PICTURE_TYPE_I;
1769 if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
1770 s->next_picture_ptr = NULL;
1773 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
1774 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
1777 if (s->last_picture_ptr) {
1778 ff_mpeg_unref_picture(s, &s->last_picture);
1779 if (s->last_picture_ptr->f.buf[0] &&
1780 (ret = ff_mpeg_ref_picture(s, &s->last_picture,
1781 s->last_picture_ptr)) < 0)
1784 if (s->next_picture_ptr) {
1785 ff_mpeg_unref_picture(s, &s->next_picture);
1786 if (s->next_picture_ptr->f.buf[0] &&
1787 (ret = ff_mpeg_ref_picture(s, &s->next_picture,
1788 s->next_picture_ptr)) < 0)
1792 if (s->pict_type != AV_PICTURE_TYPE_I &&
1793 !(s->last_picture_ptr && s->last_picture_ptr->f.buf[0])) {
1794 av_log(s, AV_LOG_ERROR,
1795 "Non-reference picture received and no reference available\n");
1796 return AVERROR_INVALIDDATA;
1799 if (s->picture_structure!= PICT_FRAME) {
1801 for (i = 0; i < 4; i++) {
1802 if (s->picture_structure == PICT_BOTTOM_FIELD) {
1803 s->current_picture.f.data[i] +=
1804 s->current_picture.f.linesize[i];
1806 s->current_picture.f.linesize[i] *= 2;
1807 s->last_picture.f.linesize[i] *= 2;
1808 s->next_picture.f.linesize[i] *= 2;
1812 s->err_recognition = avctx->err_recognition;
1814 /* set dequantizer, we can't do it during init as
1815 * it might change for mpeg4 and we can't do it in the header
1816 * decode as init is not called for mpeg4 there yet */
1817 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1818 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
1819 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
1820 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
1821 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
1822 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
1824 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
1825 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
1829 FF_DISABLE_DEPRECATION_WARNINGS
1830 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1831 return ff_xvmc_field_start(s, avctx);
1832 FF_ENABLE_DEPRECATION_WARNINGS
1833 #endif /* FF_API_XVMC */
1838 /* called after a frame has been decoded. */
1839 void ff_MPV_frame_end(MpegEncContext *s)
1842 FF_DISABLE_DEPRECATION_WARNINGS
1843 /* redraw edges for the frame if decoding didn't complete */
1844 // just to make sure that all data is rendered.
1845 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {
1846 ff_xvmc_field_end(s);
1848 FF_ENABLE_DEPRECATION_WARNINGS
1849 #endif /* FF_API_XVMC */
1853 if (s->current_picture.reference)
1854 ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
1858 * Print debugging info for the given picture.
1860 void ff_print_debug_info(MpegEncContext *s, Picture *p)
1863 if (s->avctx->hwaccel || !p || !p->mb_type)
1867 if (s->avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
1870 av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
1871 switch (pict->pict_type) {
1872 case AV_PICTURE_TYPE_I:
1873 av_log(s->avctx,AV_LOG_DEBUG,"I\n");
1875 case AV_PICTURE_TYPE_P:
1876 av_log(s->avctx,AV_LOG_DEBUG,"P\n");
1878 case AV_PICTURE_TYPE_B:
1879 av_log(s->avctx,AV_LOG_DEBUG,"B\n");
1881 case AV_PICTURE_TYPE_S:
1882 av_log(s->avctx,AV_LOG_DEBUG,"S\n");
1884 case AV_PICTURE_TYPE_SI:
1885 av_log(s->avctx,AV_LOG_DEBUG,"SI\n");
1887 case AV_PICTURE_TYPE_SP:
1888 av_log(s->avctx,AV_LOG_DEBUG,"SP\n");
1891 for (y = 0; y < s->mb_height; y++) {
1892 for (x = 0; x < s->mb_width; x++) {
1893 if (s->avctx->debug & FF_DEBUG_SKIP) {
1894 int count = s->mbskip_table[x + y * s->mb_stride];
1897 av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
1899 if (s->avctx->debug & FF_DEBUG_QP) {
1900 av_log(s->avctx, AV_LOG_DEBUG, "%2d",
1901 p->qscale_table[x + y * s->mb_stride]);
1903 if (s->avctx->debug & FF_DEBUG_MB_TYPE) {
1904 int mb_type = p->mb_type[x + y * s->mb_stride];
1905 // Type & MV direction
1906 if (IS_PCM(mb_type))
1907 av_log(s->avctx, AV_LOG_DEBUG, "P");
1908 else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
1909 av_log(s->avctx, AV_LOG_DEBUG, "A");
1910 else if (IS_INTRA4x4(mb_type))
1911 av_log(s->avctx, AV_LOG_DEBUG, "i");
1912 else if (IS_INTRA16x16(mb_type))
1913 av_log(s->avctx, AV_LOG_DEBUG, "I");
1914 else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
1915 av_log(s->avctx, AV_LOG_DEBUG, "d");
1916 else if (IS_DIRECT(mb_type))
1917 av_log(s->avctx, AV_LOG_DEBUG, "D");
1918 else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
1919 av_log(s->avctx, AV_LOG_DEBUG, "g");
1920 else if (IS_GMC(mb_type))
1921 av_log(s->avctx, AV_LOG_DEBUG, "G");
1922 else if (IS_SKIP(mb_type))
1923 av_log(s->avctx, AV_LOG_DEBUG, "S");
1924 else if (!USES_LIST(mb_type, 1))
1925 av_log(s->avctx, AV_LOG_DEBUG, ">");
1926 else if (!USES_LIST(mb_type, 0))
1927 av_log(s->avctx, AV_LOG_DEBUG, "<");
1929 assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
1930 av_log(s->avctx, AV_LOG_DEBUG, "X");
1934 if (IS_8X8(mb_type))
1935 av_log(s->avctx, AV_LOG_DEBUG, "+");
1936 else if (IS_16X8(mb_type))
1937 av_log(s->avctx, AV_LOG_DEBUG, "-");
1938 else if (IS_8X16(mb_type))
1939 av_log(s->avctx, AV_LOG_DEBUG, "|");
1940 else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
1941 av_log(s->avctx, AV_LOG_DEBUG, " ");
1943 av_log(s->avctx, AV_LOG_DEBUG, "?");
1946 if (IS_INTERLACED(mb_type))
1947 av_log(s->avctx, AV_LOG_DEBUG, "=");
1949 av_log(s->avctx, AV_LOG_DEBUG, " ");
1952 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1958 * find the lowest MB row referenced in the MVs
1960 int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
1962 int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
1963 int my, off, i, mvs;
1965 if (s->picture_structure != PICT_FRAME || s->mcsel)
1968 switch (s->mv_type) {
1982 for (i = 0; i < mvs; i++) {
1983 my = s->mv[dir][i][1]<<qpel_shift;
1984 my_max = FFMAX(my_max, my);
1985 my_min = FFMIN(my_min, my);
1988 off = (FFMAX(-my_min, my_max) + 63) >> 6;
1990 return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
1992 return s->mb_height-1;
1995 /* put block[] to dest[] */
1996 static inline void put_dct(MpegEncContext *s,
1997 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
1999 s->dct_unquantize_intra(s, block, i, qscale);
2000 s->dsp.idct_put (dest, line_size, block);
2003 /* add block[] to dest[] */
2004 static inline void add_dct(MpegEncContext *s,
2005 int16_t *block, int i, uint8_t *dest, int line_size)
2007 if (s->block_last_index[i] >= 0) {
2008 s->dsp.idct_add (dest, line_size, block);
2012 static inline void add_dequant_dct(MpegEncContext *s,
2013 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
2015 if (s->block_last_index[i] >= 0) {
2016 s->dct_unquantize_inter(s, block, i, qscale);
2018 s->dsp.idct_add (dest, line_size, block);
2023 * Clean dc, ac, coded_block for the current non-intra MB.
2025 void ff_clean_intra_table_entries(MpegEncContext *s)
2027 int wrap = s->b8_stride;
2028 int xy = s->block_index[0];
2031 s->dc_val[0][xy + 1 ] =
2032 s->dc_val[0][xy + wrap] =
2033 s->dc_val[0][xy + 1 + wrap] = 1024;
2035 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
2036 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
2037 if (s->msmpeg4_version>=3) {
2038 s->coded_block[xy ] =
2039 s->coded_block[xy + 1 ] =
2040 s->coded_block[xy + wrap] =
2041 s->coded_block[xy + 1 + wrap] = 0;
2044 wrap = s->mb_stride;
2045 xy = s->mb_x + s->mb_y * wrap;
2047 s->dc_val[2][xy] = 1024;
2049 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
2050 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
2052 s->mbintra_table[xy]= 0;
2055 /* generic function called after a macroblock has been parsed by the
2056 decoder or after it has been encoded by the encoder.
2058 Important variables used:
2059 s->mb_intra : true if intra macroblock
2060 s->mv_dir : motion vector direction
2061 s->mv_type : motion vector type
2062 s->mv : motion vector
2063 s->interlaced_dct : true if interlaced dct used (mpeg2)
2065 static av_always_inline
2066 void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
2069 const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
2072 FF_DISABLE_DEPRECATION_WARNINGS
2073 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
2074 ff_xvmc_decode_mb(s);//xvmc uses pblocks
2077 FF_ENABLE_DEPRECATION_WARNINGS
2078 #endif /* FF_API_XVMC */
2080 if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
2081 /* print DCT coefficients */
2083 av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
2085 for(j=0; j<64; j++){
2086 av_log(s->avctx, AV_LOG_DEBUG, "%5d", block[i][s->dsp.idct_permutation[j]]);
2088 av_log(s->avctx, AV_LOG_DEBUG, "\n");
2092 s->current_picture.qscale_table[mb_xy] = s->qscale;
2094 /* update DC predictors for P macroblocks */
2096 if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
2097 if(s->mbintra_table[mb_xy])
2098 ff_clean_intra_table_entries(s);
2102 s->last_dc[2] = 128 << s->intra_dc_precision;
2105 else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
2106 s->mbintra_table[mb_xy]=1;
2108 if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==AV_PICTURE_TYPE_B) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc
2109 uint8_t *dest_y, *dest_cb, *dest_cr;
2110 int dct_linesize, dct_offset;
2111 op_pixels_func (*op_pix)[4];
2112 qpel_mc_func (*op_qpix)[16];
2113 const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
2114 const int uvlinesize = s->current_picture.f.linesize[1];
2115 const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band;
2116 const int block_size = 8;
2118 /* avoid copy if macroblock skipped in last frame too */
2119 /* skip only during decoding as we might trash the buffers during encoding a bit */
2121 uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
2123 if (s->mb_skipped) {
2125 assert(s->pict_type!=AV_PICTURE_TYPE_I);
2127 } else if(!s->current_picture.reference) {
2130 *mbskip_ptr = 0; /* not skipped */
2134 dct_linesize = linesize << s->interlaced_dct;
2135 dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
2139 dest_cb= s->dest[1];
2140 dest_cr= s->dest[2];
2142 dest_y = s->b_scratchpad;
2143 dest_cb= s->b_scratchpad+16*linesize;
2144 dest_cr= s->b_scratchpad+32*linesize;
2148 /* motion handling */
2149 /* decoding or more than one mb_type (MC was already done otherwise) */
2152 if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
2153 if (s->mv_dir & MV_DIR_FORWARD) {
2154 ff_thread_await_progress(&s->last_picture_ptr->tf,
2155 ff_MPV_lowest_referenced_row(s, 0),
2158 if (s->mv_dir & MV_DIR_BACKWARD) {
2159 ff_thread_await_progress(&s->next_picture_ptr->tf,
2160 ff_MPV_lowest_referenced_row(s, 1),
2165 op_qpix= s->me.qpel_put;
2166 if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
2167 op_pix = s->hdsp.put_pixels_tab;
2169 op_pix = s->hdsp.put_no_rnd_pixels_tab;
2171 if (s->mv_dir & MV_DIR_FORWARD) {
2172 ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
2173 op_pix = s->hdsp.avg_pixels_tab;
2174 op_qpix= s->me.qpel_avg;
2176 if (s->mv_dir & MV_DIR_BACKWARD) {
2177 ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
2181 /* skip dequant / idct if we are really late ;) */
2182 if(s->avctx->skip_idct){
2183 if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
2184 ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
2185 || s->avctx->skip_idct >= AVDISCARD_ALL)
2189 /* add dct residue */
2190 if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
2191 || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
2192 add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
2193 add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
2194 add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
2195 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
2197 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2198 if (s->chroma_y_shift){
2199 add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
2200 add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
2204 add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
2205 add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
2206 add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
2207 add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
2210 } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
2211 add_dct(s, block[0], 0, dest_y , dct_linesize);
2212 add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
2213 add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
2214 add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
2216 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2217 if(s->chroma_y_shift){//Chroma420
2218 add_dct(s, block[4], 4, dest_cb, uvlinesize);
2219 add_dct(s, block[5], 5, dest_cr, uvlinesize);
2222 dct_linesize = uvlinesize << s->interlaced_dct;
2223 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
2225 add_dct(s, block[4], 4, dest_cb, dct_linesize);
2226 add_dct(s, block[5], 5, dest_cr, dct_linesize);
2227 add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
2228 add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
2229 if(!s->chroma_x_shift){//Chroma444
2230 add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
2231 add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
2232 add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
2233 add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
2238 else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
2239 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
2242 /* dct only in intra block */
2243 if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
2244 put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
2245 put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
2246 put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
2247 put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
2249 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2250 if(s->chroma_y_shift){
2251 put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
2252 put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
2256 put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
2257 put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
2258 put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
2259 put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
2263 s->dsp.idct_put(dest_y , dct_linesize, block[0]);
2264 s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
2265 s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
2266 s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
2268 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2269 if(s->chroma_y_shift){
2270 s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
2271 s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
2274 dct_linesize = uvlinesize << s->interlaced_dct;
2275 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
2277 s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
2278 s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
2279 s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
2280 s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
2281 if(!s->chroma_x_shift){//Chroma444
2282 s->dsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
2283 s->dsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
2284 s->dsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
2285 s->dsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
2293 s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
2294 s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
2295 s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
2300 void ff_MPV_decode_mb(MpegEncContext *s, int16_t block[12][64]){
2302 if(s->out_format == FMT_MPEG1) {
2303 MPV_decode_mb_internal(s, block, 1);
2306 MPV_decode_mb_internal(s, block, 0);
2309 void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
2311 ff_draw_horiz_band(s->avctx, &s->current_picture.f,
2312 &s->last_picture.f, y, h, s->picture_structure,
2313 s->first_field, s->low_delay);
2316 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
2317 const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
2318 const int uvlinesize = s->current_picture.f.linesize[1];
2319 const int mb_size= 4;
2321 s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
2322 s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
2323 s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
2324 s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
2325 s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
2326 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;
2327 //block_index is not used by mpeg2, so it is not affected by chroma_format
2329 s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) << mb_size);
2330 s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
2331 s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
2333 if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
2335 if(s->picture_structure==PICT_FRAME){
2336 s->dest[0] += s->mb_y * linesize << mb_size;
2337 s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
2338 s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
2340 s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
2341 s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
2342 s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
2343 assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
2349 * Permute an 8x8 block.
2350 * @param block the block which will be permuted according to the given permutation vector
2351 * @param permutation the permutation vector
2352 * @param last the last non zero coefficient in scantable order, used to speed the permutation up
2353 * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
2354 * (inverse) permutated to scantable order!
2356 void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
2362 //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
2364 for(i=0; i<=last; i++){
2365 const int j= scantable[i];
2370 for(i=0; i<=last; i++){
2371 const int j= scantable[i];
2372 const int perm_j= permutation[j];
2373 block[perm_j]= temp[j];
2377 void ff_mpeg_flush(AVCodecContext *avctx){
2379 MpegEncContext *s = avctx->priv_data;
2381 if(s==NULL || s->picture==NULL)
2384 for (i = 0; i < MAX_PICTURE_COUNT; i++)
2385 ff_mpeg_unref_picture(s, &s->picture[i]);
2386 s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
2388 ff_mpeg_unref_picture(s, &s->current_picture);
2389 ff_mpeg_unref_picture(s, &s->last_picture);
2390 ff_mpeg_unref_picture(s, &s->next_picture);
2392 s->mb_x= s->mb_y= 0;
2394 s->parse_context.state= -1;
2395 s->parse_context.frame_start_found= 0;
2396 s->parse_context.overread= 0;
2397 s->parse_context.overread_index= 0;
2398 s->parse_context.index= 0;
2399 s->parse_context.last_index= 0;
2400 s->bitstream_buffer_size=0;
2405 * set qscale and update qscale dependent variables.
2407 void ff_set_qscale(MpegEncContext * s, int qscale)
2411 else if (qscale > 31)
2415 s->chroma_qscale= s->chroma_qscale_table[qscale];
2417 s->y_dc_scale= s->y_dc_scale_table[ qscale ];
2418 s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
2421 void ff_MPV_report_decode_progress(MpegEncContext *s)
2423 if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
2424 ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
2427 #if CONFIG_ERROR_RESILIENCE
2428 void ff_mpeg_set_erpic(ERPicture *dst, Picture *src)
2438 for (i = 0; i < 2; i++) {
2439 dst->motion_val[i] = src->motion_val[i];
2440 dst->ref_index[i] = src->ref_index[i];
2443 dst->mb_type = src->mb_type;
2444 dst->field_picture = src->field_picture;
2447 void ff_mpeg_er_frame_start(MpegEncContext *s)
2449 ERContext *er = &s->er;
2451 ff_mpeg_set_erpic(&er->cur_pic, s->current_picture_ptr);
2452 ff_mpeg_set_erpic(&er->next_pic, s->next_picture_ptr);
2453 ff_mpeg_set_erpic(&er->last_pic, s->last_picture_ptr);
2455 er->pp_time = s->pp_time;
2456 er->pb_time = s->pb_time;
2457 er->quarter_sample = s->quarter_sample;
2458 er->partitioned_frame = s->partitioned_frame;
2460 ff_er_frame_start(er);
2462 #endif /* CONFIG_ERROR_RESILIENCE */