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