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 "mpegutils.h"
41 #include "mpegvideo.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];
116 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
117 /* XXX: only mpeg1 */
118 quant_matrix = s->intra_matrix;
119 for(i=1;i<=nCoeffs;i++) {
120 int j= s->intra_scantable.permutated[i];
125 level = (int)(level * qscale * quant_matrix[j]) >> 3;
126 level = (level - 1) | 1;
129 level = (int)(level * qscale * quant_matrix[j]) >> 3;
130 level = (level - 1) | 1;
137 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
138 int16_t *block, int n, int qscale)
140 int i, level, nCoeffs;
141 const uint16_t *quant_matrix;
143 nCoeffs= s->block_last_index[n];
145 quant_matrix = s->inter_matrix;
146 for(i=0; i<=nCoeffs; i++) {
147 int j= s->intra_scantable.permutated[i];
152 level = (((level << 1) + 1) * qscale *
153 ((int) (quant_matrix[j]))) >> 4;
154 level = (level - 1) | 1;
157 level = (((level << 1) + 1) * qscale *
158 ((int) (quant_matrix[j]))) >> 4;
159 level = (level - 1) | 1;
166 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
167 int16_t *block, int n, int qscale)
169 int i, level, nCoeffs;
170 const uint16_t *quant_matrix;
172 if(s->alternate_scan) nCoeffs= 63;
173 else nCoeffs= s->block_last_index[n];
175 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
176 quant_matrix = s->intra_matrix;
177 for(i=1;i<=nCoeffs;i++) {
178 int j= s->intra_scantable.permutated[i];
183 level = (int)(level * qscale * quant_matrix[j]) >> 3;
186 level = (int)(level * qscale * quant_matrix[j]) >> 3;
193 static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
194 int16_t *block, int n, int qscale)
196 int i, level, nCoeffs;
197 const uint16_t *quant_matrix;
200 if(s->alternate_scan) nCoeffs= 63;
201 else nCoeffs= s->block_last_index[n];
203 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
205 quant_matrix = s->intra_matrix;
206 for(i=1;i<=nCoeffs;i++) {
207 int j= s->intra_scantable.permutated[i];
212 level = (int)(level * qscale * quant_matrix[j]) >> 3;
215 level = (int)(level * qscale * quant_matrix[j]) >> 3;
224 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
225 int16_t *block, int n, int qscale)
227 int i, level, nCoeffs;
228 const uint16_t *quant_matrix;
231 if(s->alternate_scan) nCoeffs= 63;
232 else nCoeffs= s->block_last_index[n];
234 quant_matrix = s->inter_matrix;
235 for(i=0; i<=nCoeffs; i++) {
236 int j= s->intra_scantable.permutated[i];
241 level = (((level << 1) + 1) * qscale *
242 ((int) (quant_matrix[j]))) >> 4;
245 level = (((level << 1) + 1) * qscale *
246 ((int) (quant_matrix[j]))) >> 4;
255 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
256 int16_t *block, int n, int qscale)
258 int i, level, qmul, qadd;
261 av_assert2(s->block_last_index[n]>=0 || s->h263_aic);
266 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
267 qadd = (qscale - 1) | 1;
274 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
276 for(i=1; i<=nCoeffs; i++) {
280 level = level * qmul - qadd;
282 level = level * qmul + qadd;
289 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
290 int16_t *block, int n, int qscale)
292 int i, level, qmul, qadd;
295 av_assert2(s->block_last_index[n]>=0);
297 qadd = (qscale - 1) | 1;
300 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
302 for(i=0; i<=nCoeffs; i++) {
306 level = level * qmul - qadd;
308 level = level * qmul + qadd;
315 static void mpeg_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
317 int mb_x, int mb_y, int mb_intra, int mb_skipped)
319 MpegEncContext *s = opaque;
322 s->mv_type = mv_type;
323 s->mb_intra = mb_intra;
324 s->mb_skipped = mb_skipped;
327 memcpy(s->mv, mv, sizeof(*mv));
329 ff_init_block_index(s);
330 ff_update_block_index(s);
332 s->dsp.clear_blocks(s->block[0]);
334 s->dest[0] = s->current_picture.f->data[0] + (s->mb_y * 16 * s->linesize) + s->mb_x * 16;
335 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);
336 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);
339 av_log(s->avctx, AV_LOG_DEBUG, "Interlaced error concealment is not fully implemented\n");
340 ff_MPV_decode_mb(s, s->block);
343 static void gray16(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
346 memset(dst + h*linesize, 128, 16);
349 static void gray8(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
352 memset(dst + h*linesize, 128, 8);
355 /* init common dct for both encoder and decoder */
356 av_cold int ff_dct_common_init(MpegEncContext *s)
358 ff_dsputil_init(&s->dsp, s->avctx);
359 ff_h264chroma_init(&s->h264chroma, 8); //for lowres
360 ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
361 ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
363 if (s->avctx->debug & FF_DEBUG_NOMC) {
365 for (i=0; i<4; i++) {
366 s->hdsp.avg_pixels_tab[0][i] = gray16;
367 s->hdsp.put_pixels_tab[0][i] = gray16;
368 s->hdsp.put_no_rnd_pixels_tab[0][i] = gray16;
370 s->hdsp.avg_pixels_tab[1][i] = gray8;
371 s->hdsp.put_pixels_tab[1][i] = gray8;
372 s->hdsp.put_no_rnd_pixels_tab[1][i] = gray8;
376 s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
377 s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
378 s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
379 s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
380 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
381 if (s->flags & CODEC_FLAG_BITEXACT)
382 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
383 s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
386 ff_MPV_common_init_axp(s);
388 ff_MPV_common_init_arm(s);
390 ff_MPV_common_init_ppc(s);
392 ff_MPV_common_init_x86(s);
394 /* load & permutate scantables
395 * note: only wmv uses different ones
397 if (s->alternate_scan) {
398 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
399 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
401 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
402 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
404 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
405 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
410 static int frame_size_alloc(MpegEncContext *s, int linesize)
412 int alloc_size = FFALIGN(FFABS(linesize) + 64, 32);
414 // edge emu needs blocksize + filter length - 1
415 // (= 17x17 for halfpel / 21x21 for h264)
416 // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9
417 // at uvlinesize. It supports only YUV420 so 24x24 is enough
418 // linesize * interlaced * MBsize
419 FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, alloc_size * 4 * 24,
422 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, alloc_size * 4 * 16 * 2,
424 s->me.temp = s->me.scratchpad;
425 s->rd_scratchpad = s->me.scratchpad;
426 s->b_scratchpad = s->me.scratchpad;
427 s->obmc_scratchpad = s->me.scratchpad + 16;
431 av_freep(&s->edge_emu_buffer);
432 return AVERROR(ENOMEM);
436 * Allocate a frame buffer
438 static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
440 int edges_needed = av_codec_is_encoder(s->avctx->codec);
444 if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
445 s->codec_id != AV_CODEC_ID_VC1IMAGE &&
446 s->codec_id != AV_CODEC_ID_MSS2) {
448 pic->f->width = s->avctx->width + 2 * EDGE_WIDTH;
449 pic->f->height = s->avctx->height + 2 * EDGE_WIDTH;
452 r = ff_thread_get_buffer(s->avctx, &pic->tf,
453 pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
455 pic->f->width = s->avctx->width;
456 pic->f->height = s->avctx->height;
457 pic->f->format = s->avctx->pix_fmt;
458 r = avcodec_default_get_buffer2(s->avctx, pic->f, 0);
461 if (r < 0 || !pic->f->buf[0]) {
462 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
469 for (i = 0; pic->f->data[i]; i++) {
470 int offset = (EDGE_WIDTH >> (i ? s->chroma_y_shift : 0)) *
471 pic->f->linesize[i] +
472 (EDGE_WIDTH >> (i ? s->chroma_x_shift : 0));
473 pic->f->data[i] += offset;
475 pic->f->width = s->avctx->width;
476 pic->f->height = s->avctx->height;
479 if (s->avctx->hwaccel) {
480 assert(!pic->hwaccel_picture_private);
481 if (s->avctx->hwaccel->frame_priv_data_size) {
482 pic->hwaccel_priv_buf = av_buffer_allocz(s->avctx->hwaccel->frame_priv_data_size);
483 if (!pic->hwaccel_priv_buf) {
484 av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
487 pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
491 if (s->linesize && (s->linesize != pic->f->linesize[0] ||
492 s->uvlinesize != pic->f->linesize[1])) {
493 av_log(s->avctx, AV_LOG_ERROR,
494 "get_buffer() failed (stride changed)\n");
495 ff_mpeg_unref_picture(s, pic);
499 if (pic->f->linesize[1] != pic->f->linesize[2]) {
500 av_log(s->avctx, AV_LOG_ERROR,
501 "get_buffer() failed (uv stride mismatch)\n");
502 ff_mpeg_unref_picture(s, pic);
506 if (!s->edge_emu_buffer &&
507 (ret = frame_size_alloc(s, pic->f->linesize[0])) < 0) {
508 av_log(s->avctx, AV_LOG_ERROR,
509 "get_buffer() failed to allocate context scratch buffers.\n");
510 ff_mpeg_unref_picture(s, pic);
517 void ff_free_picture_tables(Picture *pic)
521 pic->alloc_mb_width =
522 pic->alloc_mb_height = 0;
524 av_buffer_unref(&pic->mb_var_buf);
525 av_buffer_unref(&pic->mc_mb_var_buf);
526 av_buffer_unref(&pic->mb_mean_buf);
527 av_buffer_unref(&pic->mbskip_table_buf);
528 av_buffer_unref(&pic->qscale_table_buf);
529 av_buffer_unref(&pic->mb_type_buf);
531 for (i = 0; i < 2; i++) {
532 av_buffer_unref(&pic->motion_val_buf[i]);
533 av_buffer_unref(&pic->ref_index_buf[i]);
537 static int alloc_picture_tables(MpegEncContext *s, Picture *pic)
539 const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;
540 const int mb_array_size = s->mb_stride * s->mb_height;
541 const int b8_array_size = s->b8_stride * s->mb_height * 2;
545 pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
546 pic->qscale_table_buf = av_buffer_allocz(big_mb_num + s->mb_stride);
547 pic->mb_type_buf = av_buffer_allocz((big_mb_num + s->mb_stride) *
549 if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
550 return AVERROR(ENOMEM);
553 pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
554 pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
555 pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
556 if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
557 return AVERROR(ENOMEM);
560 if (s->out_format == FMT_H263 || s->encoding || s->avctx->debug_mv) {
561 int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
562 int ref_index_size = 4 * mb_array_size;
564 for (i = 0; mv_size && i < 2; i++) {
565 pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
566 pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
567 if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
568 return AVERROR(ENOMEM);
572 pic->alloc_mb_width = s->mb_width;
573 pic->alloc_mb_height = s->mb_height;
578 static int make_tables_writable(Picture *pic)
581 #define MAKE_WRITABLE(table) \
584 (ret = av_buffer_make_writable(&pic->table)) < 0)\
588 MAKE_WRITABLE(mb_var_buf);
589 MAKE_WRITABLE(mc_mb_var_buf);
590 MAKE_WRITABLE(mb_mean_buf);
591 MAKE_WRITABLE(mbskip_table_buf);
592 MAKE_WRITABLE(qscale_table_buf);
593 MAKE_WRITABLE(mb_type_buf);
595 for (i = 0; i < 2; i++) {
596 MAKE_WRITABLE(motion_val_buf[i]);
597 MAKE_WRITABLE(ref_index_buf[i]);
604 * Allocate a Picture.
605 * The pixels are allocated/set by calling get_buffer() if shared = 0
607 int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
611 if (pic->qscale_table_buf)
612 if ( pic->alloc_mb_width != s->mb_width
613 || pic->alloc_mb_height != s->mb_height)
614 ff_free_picture_tables(pic);
617 av_assert0(pic->f->data[0]);
620 av_assert0(!pic->f->buf[0]);
622 if (alloc_frame_buffer(s, pic) < 0)
625 s->linesize = pic->f->linesize[0];
626 s->uvlinesize = pic->f->linesize[1];
629 if (!pic->qscale_table_buf)
630 ret = alloc_picture_tables(s, pic);
632 ret = make_tables_writable(pic);
637 pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
638 pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
639 pic->mb_mean = pic->mb_mean_buf->data;
642 pic->mbskip_table = pic->mbskip_table_buf->data;
643 pic->qscale_table = pic->qscale_table_buf->data + 2 * s->mb_stride + 1;
644 pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;
646 if (pic->motion_val_buf[0]) {
647 for (i = 0; i < 2; i++) {
648 pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
649 pic->ref_index[i] = pic->ref_index_buf[i]->data;
655 av_log(s->avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
656 ff_mpeg_unref_picture(s, pic);
657 ff_free_picture_tables(pic);
658 return AVERROR(ENOMEM);
662 * Deallocate a picture.
664 void ff_mpeg_unref_picture(MpegEncContext *s, Picture *pic)
666 int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
669 /* WM Image / Screen codecs allocate internal buffers with different
670 * dimensions / colorspaces; ignore user-defined callbacks for these. */
671 if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
672 s->codec_id != AV_CODEC_ID_VC1IMAGE &&
673 s->codec_id != AV_CODEC_ID_MSS2)
674 ff_thread_release_buffer(s->avctx, &pic->tf);
676 av_frame_unref(pic->f);
678 av_buffer_unref(&pic->hwaccel_priv_buf);
680 if (pic->needs_realloc)
681 ff_free_picture_tables(pic);
683 memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
686 static int update_picture_tables(Picture *dst, Picture *src)
690 #define UPDATE_TABLE(table)\
693 (!dst->table || dst->table->buffer != src->table->buffer)) {\
694 av_buffer_unref(&dst->table);\
695 dst->table = av_buffer_ref(src->table);\
697 ff_free_picture_tables(dst);\
698 return AVERROR(ENOMEM);\
703 UPDATE_TABLE(mb_var_buf);
704 UPDATE_TABLE(mc_mb_var_buf);
705 UPDATE_TABLE(mb_mean_buf);
706 UPDATE_TABLE(mbskip_table_buf);
707 UPDATE_TABLE(qscale_table_buf);
708 UPDATE_TABLE(mb_type_buf);
709 for (i = 0; i < 2; i++) {
710 UPDATE_TABLE(motion_val_buf[i]);
711 UPDATE_TABLE(ref_index_buf[i]);
714 dst->mb_var = src->mb_var;
715 dst->mc_mb_var = src->mc_mb_var;
716 dst->mb_mean = src->mb_mean;
717 dst->mbskip_table = src->mbskip_table;
718 dst->qscale_table = src->qscale_table;
719 dst->mb_type = src->mb_type;
720 for (i = 0; i < 2; i++) {
721 dst->motion_val[i] = src->motion_val[i];
722 dst->ref_index[i] = src->ref_index[i];
725 dst->alloc_mb_width = src->alloc_mb_width;
726 dst->alloc_mb_height = src->alloc_mb_height;
731 int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src)
735 av_assert0(!dst->f->buf[0]);
736 av_assert0(src->f->buf[0]);
740 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
744 ret = update_picture_tables(dst, src);
748 if (src->hwaccel_picture_private) {
749 dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
750 if (!dst->hwaccel_priv_buf)
752 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
755 dst->field_picture = src->field_picture;
756 dst->mb_var_sum = src->mb_var_sum;
757 dst->mc_mb_var_sum = src->mc_mb_var_sum;
758 dst->b_frame_score = src->b_frame_score;
759 dst->needs_realloc = src->needs_realloc;
760 dst->reference = src->reference;
761 dst->shared = src->shared;
765 ff_mpeg_unref_picture(s, dst);
769 static void exchange_uv(MpegEncContext *s)
774 s->pblocks[4] = s->pblocks[5];
778 static int init_duplicate_context(MpegEncContext *s)
780 int y_size = s->b8_stride * (2 * s->mb_height + 1);
781 int c_size = s->mb_stride * (s->mb_height + 1);
782 int yc_size = y_size + 2 * c_size;
785 if (s->mb_height & 1)
786 yc_size += 2*s->b8_stride + 2*s->mb_stride;
793 s->obmc_scratchpad = NULL;
796 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
797 ME_MAP_SIZE * sizeof(uint32_t), fail)
798 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
799 ME_MAP_SIZE * sizeof(uint32_t), fail)
800 if (s->avctx->noise_reduction) {
801 FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
802 2 * 64 * sizeof(int), fail)
805 FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
806 s->block = s->blocks[0];
808 for (i = 0; i < 12; i++) {
809 s->pblocks[i] = &s->block[i];
811 if (s->avctx->codec_tag == AV_RL32("VCR2"))
814 if (s->out_format == FMT_H263) {
816 FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
817 yc_size * sizeof(int16_t) * 16, fail);
818 s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
819 s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
820 s->ac_val[2] = s->ac_val[1] + c_size;
825 return -1; // free() through ff_MPV_common_end()
828 static void free_duplicate_context(MpegEncContext *s)
833 av_freep(&s->edge_emu_buffer);
834 av_freep(&s->me.scratchpad);
838 s->obmc_scratchpad = NULL;
840 av_freep(&s->dct_error_sum);
841 av_freep(&s->me.map);
842 av_freep(&s->me.score_map);
843 av_freep(&s->blocks);
844 av_freep(&s->ac_val_base);
848 static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
850 #define COPY(a) bak->a = src->a
851 COPY(edge_emu_buffer);
856 COPY(obmc_scratchpad);
863 COPY(me.map_generation);
875 int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
879 // FIXME copy only needed parts
881 backup_duplicate_context(&bak, dst);
882 memcpy(dst, src, sizeof(MpegEncContext));
883 backup_duplicate_context(dst, &bak);
884 for (i = 0; i < 12; i++) {
885 dst->pblocks[i] = &dst->block[i];
887 if (dst->avctx->codec_tag == AV_RL32("VCR2"))
889 if (!dst->edge_emu_buffer &&
890 (ret = frame_size_alloc(dst, dst->linesize)) < 0) {
891 av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
892 "scratch buffers.\n");
895 // STOP_TIMER("update_duplicate_context")
896 // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
900 int ff_mpeg_update_thread_context(AVCodecContext *dst,
901 const AVCodecContext *src)
904 MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
911 // FIXME can parameters change on I-frames?
912 // in that case dst may need a reinit
913 if (!s->context_initialized) {
914 memcpy(s, s1, sizeof(MpegEncContext));
917 s->bitstream_buffer = NULL;
918 s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
920 if (s1->context_initialized){
921 // s->picture_range_start += MAX_PICTURE_COUNT;
922 // s->picture_range_end += MAX_PICTURE_COUNT;
923 if((ret = ff_MPV_common_init(s)) < 0){
924 memset(s, 0, sizeof(MpegEncContext));
931 if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
932 s->context_reinit = 0;
933 s->height = s1->height;
934 s->width = s1->width;
935 if ((ret = ff_MPV_common_frame_size_change(s)) < 0)
939 s->avctx->coded_height = s1->avctx->coded_height;
940 s->avctx->coded_width = s1->avctx->coded_width;
941 s->avctx->width = s1->avctx->width;
942 s->avctx->height = s1->avctx->height;
944 s->coded_picture_number = s1->coded_picture_number;
945 s->picture_number = s1->picture_number;
947 av_assert0(!s->picture || s->picture != s1->picture);
949 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
950 ff_mpeg_unref_picture(s, &s->picture[i]);
951 if (s1->picture[i].f->buf[0] &&
952 (ret = ff_mpeg_ref_picture(s, &s->picture[i], &s1->picture[i])) < 0)
956 #define UPDATE_PICTURE(pic)\
958 ff_mpeg_unref_picture(s, &s->pic);\
959 if (s1->pic.f && s1->pic.f->buf[0])\
960 ret = ff_mpeg_ref_picture(s, &s->pic, &s1->pic);\
962 ret = update_picture_tables(&s->pic, &s1->pic);\
967 UPDATE_PICTURE(current_picture);
968 UPDATE_PICTURE(last_picture);
969 UPDATE_PICTURE(next_picture);
971 s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
972 s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
973 s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
975 // Error/bug resilience
976 s->next_p_frame_damaged = s1->next_p_frame_damaged;
977 s->workaround_bugs = s1->workaround_bugs;
978 s->padding_bug_score = s1->padding_bug_score;
981 memcpy(&s->last_time_base, &s1->last_time_base,
982 (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
983 (char *) &s1->last_time_base);
986 s->max_b_frames = s1->max_b_frames;
987 s->low_delay = s1->low_delay;
988 s->droppable = s1->droppable;
990 // DivX handling (doesn't work)
991 s->divx_packed = s1->divx_packed;
993 if (s1->bitstream_buffer) {
994 if (s1->bitstream_buffer_size +
995 FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
996 av_fast_malloc(&s->bitstream_buffer,
997 &s->allocated_bitstream_buffer_size,
998 s1->allocated_bitstream_buffer_size);
999 s->bitstream_buffer_size = s1->bitstream_buffer_size;
1000 memcpy(s->bitstream_buffer, s1->bitstream_buffer,
1001 s1->bitstream_buffer_size);
1002 memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
1003 FF_INPUT_BUFFER_PADDING_SIZE);
1006 // linesize dependend scratch buffer allocation
1007 if (!s->edge_emu_buffer)
1009 if (frame_size_alloc(s, s1->linesize) < 0) {
1010 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
1011 "scratch buffers.\n");
1012 return AVERROR(ENOMEM);
1015 av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
1016 "be allocated due to unknown size.\n");
1019 // MPEG2/interlacing info
1020 memcpy(&s->progressive_sequence, &s1->progressive_sequence,
1021 (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
1023 if (!s1->first_field) {
1024 s->last_pict_type = s1->pict_type;
1025 if (s1->current_picture_ptr)
1026 s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f->quality;
1033 * Set the given MpegEncContext to common defaults
1034 * (same for encoding and decoding).
1035 * The changed fields will not depend upon the
1036 * prior state of the MpegEncContext.
1038 void ff_MPV_common_defaults(MpegEncContext *s)
1040 s->y_dc_scale_table =
1041 s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
1042 s->chroma_qscale_table = ff_default_chroma_qscale_table;
1043 s->progressive_frame = 1;
1044 s->progressive_sequence = 1;
1045 s->picture_structure = PICT_FRAME;
1047 s->coded_picture_number = 0;
1048 s->picture_number = 0;
1053 s->slice_context_count = 1;
1057 * Set the given MpegEncContext to defaults for decoding.
1058 * the changed fields will not depend upon
1059 * the prior state of the MpegEncContext.
1061 void ff_MPV_decode_defaults(MpegEncContext *s)
1063 ff_MPV_common_defaults(s);
1066 static int init_er(MpegEncContext *s)
1068 ERContext *er = &s->er;
1069 int mb_array_size = s->mb_height * s->mb_stride;
1072 er->avctx = s->avctx;
1075 er->mb_index2xy = s->mb_index2xy;
1076 er->mb_num = s->mb_num;
1077 er->mb_width = s->mb_width;
1078 er->mb_height = s->mb_height;
1079 er->mb_stride = s->mb_stride;
1080 er->b8_stride = s->b8_stride;
1082 er->er_temp_buffer = av_malloc(s->mb_height * s->mb_stride);
1083 er->error_status_table = av_mallocz(mb_array_size);
1084 if (!er->er_temp_buffer || !er->error_status_table)
1087 er->mbskip_table = s->mbskip_table;
1088 er->mbintra_table = s->mbintra_table;
1090 for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++)
1091 er->dc_val[i] = s->dc_val[i];
1093 er->decode_mb = mpeg_er_decode_mb;
1098 av_freep(&er->er_temp_buffer);
1099 av_freep(&er->error_status_table);
1100 return AVERROR(ENOMEM);
1104 * Initialize and allocates MpegEncContext fields dependent on the resolution.
1106 static int init_context_frame(MpegEncContext *s)
1108 int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
1110 s->mb_width = (s->width + 15) / 16;
1111 s->mb_stride = s->mb_width + 1;
1112 s->b8_stride = s->mb_width * 2 + 1;
1113 mb_array_size = s->mb_height * s->mb_stride;
1114 mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
1116 /* set default edge pos, will be overridden
1117 * in decode_header if needed */
1118 s->h_edge_pos = s->mb_width * 16;
1119 s->v_edge_pos = s->mb_height * 16;
1121 s->mb_num = s->mb_width * s->mb_height;
1126 s->block_wrap[3] = s->b8_stride;
1128 s->block_wrap[5] = s->mb_stride;
1130 y_size = s->b8_stride * (2 * s->mb_height + 1);
1131 c_size = s->mb_stride * (s->mb_height + 1);
1132 yc_size = y_size + 2 * c_size;
1134 if (s->mb_height & 1)
1135 yc_size += 2*s->b8_stride + 2*s->mb_stride;
1137 FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int), fail); // error ressilience code looks cleaner with this
1138 for (y = 0; y < s->mb_height; y++)
1139 for (x = 0; x < s->mb_width; x++)
1140 s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
1142 s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
1145 /* Allocate MV tables */
1146 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
1147 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
1148 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
1149 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
1150 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
1151 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
1152 s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
1153 s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
1154 s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
1155 s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
1156 s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + s->mb_stride + 1;
1157 s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
1159 /* Allocate MB type table */
1160 FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size * sizeof(uint16_t), fail) // needed for encoding
1162 FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail)
1164 FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
1165 mb_array_size * sizeof(float), fail);
1166 FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
1167 mb_array_size * sizeof(float), fail);
1171 if (s->codec_id == AV_CODEC_ID_MPEG4 ||
1172 (s->flags & CODEC_FLAG_INTERLACED_ME)) {
1173 /* interlaced direct mode decoding tables */
1174 for (i = 0; i < 2; i++) {
1176 for (j = 0; j < 2; j++) {
1177 for (k = 0; k < 2; k++) {
1178 FF_ALLOCZ_OR_GOTO(s->avctx,
1179 s->b_field_mv_table_base[i][j][k],
1180 mv_table_size * 2 * sizeof(int16_t),
1182 s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
1185 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
1186 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
1187 s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
1189 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
1192 if (s->out_format == FMT_H263) {
1194 FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size + (s->mb_height&1)*2*s->b8_stride, fail);
1195 s->coded_block = s->coded_block_base + s->b8_stride + 1;
1197 /* cbp, ac_pred, pred_dir */
1198 FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table , mb_array_size * sizeof(uint8_t), fail);
1199 FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail);
1202 if (s->h263_pred || s->h263_plus || !s->encoding) {
1204 // MN: we need these for error resilience of intra-frames
1205 FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
1206 s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
1207 s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
1208 s->dc_val[2] = s->dc_val[1] + c_size;
1209 for (i = 0; i < yc_size; i++)
1210 s->dc_val_base[i] = 1024;
1213 /* which mb is a intra block */
1214 FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
1215 memset(s->mbintra_table, 1, mb_array_size);
1217 /* init macroblock skip table */
1218 FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
1219 // Note the + 1 is for a quicker mpeg4 slice_end detection
1223 return AVERROR(ENOMEM);
1227 * init common structure for both encoder and decoder.
1228 * this assumes that some variables like width/height are already set
1230 av_cold int ff_MPV_common_init(MpegEncContext *s)
1233 int nb_slices = (HAVE_THREADS &&
1234 s->avctx->active_thread_type & FF_THREAD_SLICE) ?
1235 s->avctx->thread_count : 1;
1237 if (s->encoding && s->avctx->slices)
1238 nb_slices = s->avctx->slices;
1240 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
1241 s->mb_height = (s->height + 31) / 32 * 2;
1243 s->mb_height = (s->height + 15) / 16;
1245 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
1246 av_log(s->avctx, AV_LOG_ERROR,
1247 "decoding to AV_PIX_FMT_NONE is not supported.\n");
1251 if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
1254 max_slices = FFMIN(MAX_THREADS, s->mb_height);
1256 max_slices = MAX_THREADS;
1257 av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
1258 " reducing to %d\n", nb_slices, max_slices);
1259 nb_slices = max_slices;
1262 if ((s->width || s->height) &&
1263 av_image_check_size(s->width, s->height, 0, s->avctx))
1266 ff_dct_common_init(s);
1268 s->flags = s->avctx->flags;
1269 s->flags2 = s->avctx->flags2;
1271 /* set chroma shifts */
1272 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,
1274 &s->chroma_y_shift);
1276 /* convert fourcc to upper case */
1277 s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
1279 s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag);
1281 FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
1282 MAX_PICTURE_COUNT * sizeof(Picture), fail);
1283 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1284 s->picture[i].f = av_frame_alloc();
1285 if (!s->picture[i].f)
1288 memset(&s->next_picture, 0, sizeof(s->next_picture));
1289 memset(&s->last_picture, 0, sizeof(s->last_picture));
1290 memset(&s->current_picture, 0, sizeof(s->current_picture));
1291 memset(&s->new_picture, 0, sizeof(s->new_picture));
1292 s->next_picture.f = av_frame_alloc();
1293 if (!s->next_picture.f)
1295 s->last_picture.f = av_frame_alloc();
1296 if (!s->last_picture.f)
1298 s->current_picture.f = av_frame_alloc();
1299 if (!s->current_picture.f)
1301 s->new_picture.f = av_frame_alloc();
1302 if (!s->new_picture.f)
1305 if (init_context_frame(s))
1308 s->parse_context.state = -1;
1310 s->context_initialized = 1;
1311 s->thread_context[0] = s;
1313 // if (s->width && s->height) {
1314 if (nb_slices > 1) {
1315 for (i = 1; i < nb_slices; i++) {
1316 s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
1317 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
1320 for (i = 0; i < nb_slices; i++) {
1321 if (init_duplicate_context(s->thread_context[i]) < 0)
1323 s->thread_context[i]->start_mb_y =
1324 (s->mb_height * (i) + nb_slices / 2) / nb_slices;
1325 s->thread_context[i]->end_mb_y =
1326 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
1329 if (init_duplicate_context(s) < 0)
1332 s->end_mb_y = s->mb_height;
1334 s->slice_context_count = nb_slices;
1339 ff_MPV_common_end(s);
1344 * Frees and resets MpegEncContext fields depending on the resolution.
1345 * Is used during resolution changes to avoid a full reinitialization of the
1348 static int free_context_frame(MpegEncContext *s)
1352 av_freep(&s->mb_type);
1353 av_freep(&s->p_mv_table_base);
1354 av_freep(&s->b_forw_mv_table_base);
1355 av_freep(&s->b_back_mv_table_base);
1356 av_freep(&s->b_bidir_forw_mv_table_base);
1357 av_freep(&s->b_bidir_back_mv_table_base);
1358 av_freep(&s->b_direct_mv_table_base);
1359 s->p_mv_table = NULL;
1360 s->b_forw_mv_table = NULL;
1361 s->b_back_mv_table = NULL;
1362 s->b_bidir_forw_mv_table = NULL;
1363 s->b_bidir_back_mv_table = NULL;
1364 s->b_direct_mv_table = NULL;
1365 for (i = 0; i < 2; i++) {
1366 for (j = 0; j < 2; j++) {
1367 for (k = 0; k < 2; k++) {
1368 av_freep(&s->b_field_mv_table_base[i][j][k]);
1369 s->b_field_mv_table[i][j][k] = NULL;
1371 av_freep(&s->b_field_select_table[i][j]);
1372 av_freep(&s->p_field_mv_table_base[i][j]);
1373 s->p_field_mv_table[i][j] = NULL;
1375 av_freep(&s->p_field_select_table[i]);
1378 av_freep(&s->dc_val_base);
1379 av_freep(&s->coded_block_base);
1380 av_freep(&s->mbintra_table);
1381 av_freep(&s->cbp_table);
1382 av_freep(&s->pred_dir_table);
1384 av_freep(&s->mbskip_table);
1386 av_freep(&s->er.error_status_table);
1387 av_freep(&s->er.er_temp_buffer);
1388 av_freep(&s->mb_index2xy);
1389 av_freep(&s->lambda_table);
1391 av_freep(&s->cplx_tab);
1392 av_freep(&s->bits_tab);
1394 s->linesize = s->uvlinesize = 0;
1399 int ff_MPV_common_frame_size_change(MpegEncContext *s)
1403 if (s->slice_context_count > 1) {
1404 for (i = 0; i < s->slice_context_count; i++) {
1405 free_duplicate_context(s->thread_context[i]);
1407 for (i = 1; i < s->slice_context_count; i++) {
1408 av_freep(&s->thread_context[i]);
1411 free_duplicate_context(s);
1413 if ((err = free_context_frame(s)) < 0)
1417 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1418 s->picture[i].needs_realloc = 1;
1421 s->last_picture_ptr =
1422 s->next_picture_ptr =
1423 s->current_picture_ptr = NULL;
1426 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
1427 s->mb_height = (s->height + 31) / 32 * 2;
1429 s->mb_height = (s->height + 15) / 16;
1431 if ((s->width || s->height) &&
1432 av_image_check_size(s->width, s->height, 0, s->avctx))
1433 return AVERROR_INVALIDDATA;
1435 if ((err = init_context_frame(s)))
1438 s->thread_context[0] = s;
1440 if (s->width && s->height) {
1441 int nb_slices = s->slice_context_count;
1442 if (nb_slices > 1) {
1443 for (i = 1; i < nb_slices; i++) {
1444 s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
1445 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
1448 for (i = 0; i < nb_slices; i++) {
1449 if (init_duplicate_context(s->thread_context[i]) < 0)
1451 s->thread_context[i]->start_mb_y =
1452 (s->mb_height * (i) + nb_slices / 2) / nb_slices;
1453 s->thread_context[i]->end_mb_y =
1454 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
1457 err = init_duplicate_context(s);
1461 s->end_mb_y = s->mb_height;
1463 s->slice_context_count = nb_slices;
1468 ff_MPV_common_end(s);
1472 /* init common structure for both encoder and decoder */
1473 void ff_MPV_common_end(MpegEncContext *s)
1477 if (s->slice_context_count > 1) {
1478 for (i = 0; i < s->slice_context_count; i++) {
1479 free_duplicate_context(s->thread_context[i]);
1481 for (i = 1; i < s->slice_context_count; i++) {
1482 av_freep(&s->thread_context[i]);
1484 s->slice_context_count = 1;
1485 } else free_duplicate_context(s);
1487 av_freep(&s->parse_context.buffer);
1488 s->parse_context.buffer_size = 0;
1490 av_freep(&s->bitstream_buffer);
1491 s->allocated_bitstream_buffer_size = 0;
1494 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1495 ff_free_picture_tables(&s->picture[i]);
1496 ff_mpeg_unref_picture(s, &s->picture[i]);
1497 av_frame_free(&s->picture[i].f);
1500 av_freep(&s->picture);
1501 ff_free_picture_tables(&s->last_picture);
1502 ff_mpeg_unref_picture(s, &s->last_picture);
1503 av_frame_free(&s->last_picture.f);
1504 ff_free_picture_tables(&s->current_picture);
1505 ff_mpeg_unref_picture(s, &s->current_picture);
1506 av_frame_free(&s->current_picture.f);
1507 ff_free_picture_tables(&s->next_picture);
1508 ff_mpeg_unref_picture(s, &s->next_picture);
1509 av_frame_free(&s->next_picture.f);
1510 ff_free_picture_tables(&s->new_picture);
1511 ff_mpeg_unref_picture(s, &s->new_picture);
1512 av_frame_free(&s->new_picture.f);
1514 free_context_frame(s);
1516 s->context_initialized = 0;
1517 s->last_picture_ptr =
1518 s->next_picture_ptr =
1519 s->current_picture_ptr = NULL;
1520 s->linesize = s->uvlinesize = 0;
1523 av_cold void ff_init_rl(RLTable *rl,
1524 uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
1526 int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
1527 uint8_t index_run[MAX_RUN + 1];
1528 int last, run, level, start, end, i;
1530 /* If table is static, we can quit if rl->max_level[0] is not NULL */
1531 if (static_store && rl->max_level[0])
1534 /* compute max_level[], max_run[] and index_run[] */
1535 for (last = 0; last < 2; last++) {
1544 memset(max_level, 0, MAX_RUN + 1);
1545 memset(max_run, 0, MAX_LEVEL + 1);
1546 memset(index_run, rl->n, MAX_RUN + 1);
1547 for (i = start; i < end; i++) {
1548 run = rl->table_run[i];
1549 level = rl->table_level[i];
1550 if (index_run[run] == rl->n)
1552 if (level > max_level[run])
1553 max_level[run] = level;
1554 if (run > max_run[level])
1555 max_run[level] = run;
1558 rl->max_level[last] = static_store[last];
1560 rl->max_level[last] = av_malloc(MAX_RUN + 1);
1561 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
1563 rl->max_run[last] = static_store[last] + MAX_RUN + 1;
1565 rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
1566 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
1568 rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
1570 rl->index_run[last] = av_malloc(MAX_RUN + 1);
1571 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
1575 av_cold void ff_init_vlc_rl(RLTable *rl)
1579 for (q = 0; q < 32; q++) {
1581 int qadd = (q - 1) | 1;
1587 for (i = 0; i < rl->vlc.table_size; i++) {
1588 int code = rl->vlc.table[i][0];
1589 int len = rl->vlc.table[i][1];
1592 if (len == 0) { // illegal code
1595 } else if (len < 0) { // more bits needed
1599 if (code == rl->n) { // esc
1603 run = rl->table_run[code] + 1;
1604 level = rl->table_level[code] * qmul + qadd;
1605 if (code >= rl->last) run += 192;
1608 rl->rl_vlc[q][i].len = len;
1609 rl->rl_vlc[q][i].level = level;
1610 rl->rl_vlc[q][i].run = run;
1615 static void release_unused_pictures(MpegEncContext *s)
1619 /* release non reference frames */
1620 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1621 if (!s->picture[i].reference)
1622 ff_mpeg_unref_picture(s, &s->picture[i]);
1626 static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
1628 if (pic == s->last_picture_ptr)
1630 if (pic->f->buf[0] == NULL)
1632 if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
1637 static int find_unused_picture(MpegEncContext *s, int shared)
1642 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1643 if (s->picture[i].f->buf[0] == NULL && &s->picture[i] != s->last_picture_ptr)
1647 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1648 if (pic_is_unused(s, &s->picture[i]))
1653 av_log(s->avctx, AV_LOG_FATAL,
1654 "Internal error, picture buffer overflow\n");
1655 /* We could return -1, but the codec would crash trying to draw into a
1656 * non-existing frame anyway. This is safer than waiting for a random crash.
1657 * Also the return of this is never useful, an encoder must only allocate
1658 * as much as allowed in the specification. This has no relationship to how
1659 * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
1660 * enough for such valid streams).
1661 * Plus, a decoder has to check stream validity and remove frames if too
1662 * many reference frames are around. Waiting for "OOM" is not correct at
1663 * all. Similarly, missing reference frames have to be replaced by
1664 * interpolated/MC frames, anything else is a bug in the codec ...
1670 int ff_find_unused_picture(MpegEncContext *s, int shared)
1672 int ret = find_unused_picture(s, shared);
1674 if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
1675 if (s->picture[ret].needs_realloc) {
1676 s->picture[ret].needs_realloc = 0;
1677 ff_free_picture_tables(&s->picture[ret]);
1678 ff_mpeg_unref_picture(s, &s->picture[ret]);
1684 static void gray_frame(AVFrame *frame)
1686 int i, h_chroma_shift, v_chroma_shift;
1688 av_pix_fmt_get_chroma_sub_sample(frame->format, &h_chroma_shift, &v_chroma_shift);
1690 for(i=0; i<frame->height; i++)
1691 memset(frame->data[0] + frame->linesize[0]*i, 0x80, frame->width);
1692 for(i=0; i<FF_CEIL_RSHIFT(frame->height, v_chroma_shift); i++) {
1693 memset(frame->data[1] + frame->linesize[1]*i,
1694 0x80, FF_CEIL_RSHIFT(frame->width, h_chroma_shift));
1695 memset(frame->data[2] + frame->linesize[2]*i,
1696 0x80, FF_CEIL_RSHIFT(frame->width, h_chroma_shift));
1701 * generic function called after decoding
1702 * the header and before a frame is decoded.
1704 int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
1710 if (!ff_thread_can_start_frame(avctx)) {
1711 av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
1715 /* mark & release old frames */
1716 if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1717 s->last_picture_ptr != s->next_picture_ptr &&
1718 s->last_picture_ptr->f->buf[0]) {
1719 ff_mpeg_unref_picture(s, s->last_picture_ptr);
1722 /* release forgotten pictures */
1723 /* if (mpeg124/h263) */
1724 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1725 if (&s->picture[i] != s->last_picture_ptr &&
1726 &s->picture[i] != s->next_picture_ptr &&
1727 s->picture[i].reference && !s->picture[i].needs_realloc) {
1728 if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1729 av_log(avctx, AV_LOG_ERROR,
1730 "releasing zombie picture\n");
1731 ff_mpeg_unref_picture(s, &s->picture[i]);
1735 ff_mpeg_unref_picture(s, &s->current_picture);
1737 release_unused_pictures(s);
1739 if (s->current_picture_ptr &&
1740 s->current_picture_ptr->f->buf[0] == NULL) {
1741 // we already have a unused image
1742 // (maybe it was set before reading the header)
1743 pic = s->current_picture_ptr;
1745 i = ff_find_unused_picture(s, 0);
1747 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1750 pic = &s->picture[i];
1754 if (!s->droppable) {
1755 if (s->pict_type != AV_PICTURE_TYPE_B)
1759 pic->f->coded_picture_number = s->coded_picture_number++;
1761 if (ff_alloc_picture(s, pic, 0) < 0)
1764 s->current_picture_ptr = pic;
1765 // FIXME use only the vars from current_pic
1766 s->current_picture_ptr->f->top_field_first = s->top_field_first;
1767 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
1768 s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1769 if (s->picture_structure != PICT_FRAME)
1770 s->current_picture_ptr->f->top_field_first =
1771 (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
1773 s->current_picture_ptr->f->interlaced_frame = !s->progressive_frame &&
1774 !s->progressive_sequence;
1775 s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
1777 s->current_picture_ptr->f->pict_type = s->pict_type;
1778 // if (s->flags && CODEC_FLAG_QSCALE)
1779 // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
1780 s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1782 if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
1783 s->current_picture_ptr)) < 0)
1786 if (s->pict_type != AV_PICTURE_TYPE_B) {
1787 s->last_picture_ptr = s->next_picture_ptr;
1789 s->next_picture_ptr = s->current_picture_ptr;
1791 av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
1792 s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
1793 s->last_picture_ptr ? s->last_picture_ptr->f->data[0] : NULL,
1794 s->next_picture_ptr ? s->next_picture_ptr->f->data[0] : NULL,
1795 s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL,
1796 s->pict_type, s->droppable);
1798 if ((s->last_picture_ptr == NULL ||
1799 s->last_picture_ptr->f->buf[0] == NULL) &&
1800 (s->pict_type != AV_PICTURE_TYPE_I ||
1801 s->picture_structure != PICT_FRAME)) {
1802 int h_chroma_shift, v_chroma_shift;
1803 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
1804 &h_chroma_shift, &v_chroma_shift);
1805 if (s->pict_type == AV_PICTURE_TYPE_B && s->next_picture_ptr && s->next_picture_ptr->f->buf[0])
1806 av_log(avctx, AV_LOG_DEBUG,
1807 "allocating dummy last picture for B frame\n");
1808 else if (s->pict_type != AV_PICTURE_TYPE_I)
1809 av_log(avctx, AV_LOG_ERROR,
1810 "warning: first frame is no keyframe\n");
1811 else if (s->picture_structure != PICT_FRAME)
1812 av_log(avctx, AV_LOG_DEBUG,
1813 "allocate dummy last picture for field based first keyframe\n");
1815 /* Allocate a dummy frame */
1816 i = ff_find_unused_picture(s, 0);
1818 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1821 s->last_picture_ptr = &s->picture[i];
1823 s->last_picture_ptr->reference = 3;
1824 s->last_picture_ptr->f->key_frame = 0;
1825 s->last_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
1827 if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
1828 s->last_picture_ptr = NULL;
1832 if (!avctx->hwaccel && !(avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)) {
1833 for(i=0; i<avctx->height; i++)
1834 memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0]*i,
1835 0x80, avctx->width);
1836 for(i=0; i<FF_CEIL_RSHIFT(avctx->height, v_chroma_shift); i++) {
1837 memset(s->last_picture_ptr->f->data[1] + s->last_picture_ptr->f->linesize[1]*i,
1838 0x80, FF_CEIL_RSHIFT(avctx->width, h_chroma_shift));
1839 memset(s->last_picture_ptr->f->data[2] + s->last_picture_ptr->f->linesize[2]*i,
1840 0x80, FF_CEIL_RSHIFT(avctx->width, h_chroma_shift));
1843 if(s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263){
1844 for(i=0; i<avctx->height; i++)
1845 memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0]*i, 16, avctx->width);
1849 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
1850 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
1852 if ((s->next_picture_ptr == NULL ||
1853 s->next_picture_ptr->f->buf[0] == NULL) &&
1854 s->pict_type == AV_PICTURE_TYPE_B) {
1855 /* Allocate a dummy frame */
1856 i = ff_find_unused_picture(s, 0);
1858 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1861 s->next_picture_ptr = &s->picture[i];
1863 s->next_picture_ptr->reference = 3;
1864 s->next_picture_ptr->f->key_frame = 0;
1865 s->next_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
1867 if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
1868 s->next_picture_ptr = NULL;
1871 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
1872 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
1875 #if 0 // BUFREF-FIXME
1876 memset(s->last_picture.f->data, 0, sizeof(s->last_picture.f->data));
1877 memset(s->next_picture.f->data, 0, sizeof(s->next_picture.f->data));
1879 if (s->last_picture_ptr) {
1880 ff_mpeg_unref_picture(s, &s->last_picture);
1881 if (s->last_picture_ptr->f->buf[0] &&
1882 (ret = ff_mpeg_ref_picture(s, &s->last_picture,
1883 s->last_picture_ptr)) < 0)
1886 if (s->next_picture_ptr) {
1887 ff_mpeg_unref_picture(s, &s->next_picture);
1888 if (s->next_picture_ptr->f->buf[0] &&
1889 (ret = ff_mpeg_ref_picture(s, &s->next_picture,
1890 s->next_picture_ptr)) < 0)
1894 av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
1895 s->last_picture_ptr->f->buf[0]));
1897 if (s->picture_structure!= PICT_FRAME) {
1899 for (i = 0; i < 4; i++) {
1900 if (s->picture_structure == PICT_BOTTOM_FIELD) {
1901 s->current_picture.f->data[i] +=
1902 s->current_picture.f->linesize[i];
1904 s->current_picture.f->linesize[i] *= 2;
1905 s->last_picture.f->linesize[i] *= 2;
1906 s->next_picture.f->linesize[i] *= 2;
1910 s->err_recognition = avctx->err_recognition;
1912 /* set dequantizer, we can't do it during init as
1913 * it might change for mpeg4 and we can't do it in the header
1914 * decode as init is not called for mpeg4 there yet */
1915 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1916 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
1917 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
1918 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
1919 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
1920 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
1922 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
1923 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
1926 if (s->avctx->debug & FF_DEBUG_NOMC) {
1927 gray_frame(s->current_picture_ptr->f);
1933 /* called after a frame has been decoded. */
1934 void ff_MPV_frame_end(MpegEncContext *s)
1938 if (s->current_picture.reference)
1939 ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
1943 * Draw a line from (ex, ey) -> (sx, sy).
1944 * @param w width of the image
1945 * @param h height of the image
1946 * @param stride stride/linesize of the image
1947 * @param color color of the arrow
1949 static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
1950 int w, int h, int stride, int color)
1954 sx = av_clip(sx, 0, w - 1);
1955 sy = av_clip(sy, 0, h - 1);
1956 ex = av_clip(ex, 0, w - 1);
1957 ey = av_clip(ey, 0, h - 1);
1959 buf[sy * stride + sx] += color;
1961 if (FFABS(ex - sx) > FFABS(ey - sy)) {
1963 FFSWAP(int, sx, ex);
1964 FFSWAP(int, sy, ey);
1966 buf += sx + sy * stride;
1968 f = ((ey - sy) << 16) / ex;
1969 for (x = 0; x <= ex; x++) {
1971 fr = (x * f) & 0xFFFF;
1972 buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
1973 if(fr) buf[(y + 1) * stride + x] += (color * fr ) >> 16;
1977 FFSWAP(int, sx, ex);
1978 FFSWAP(int, sy, ey);
1980 buf += sx + sy * stride;
1983 f = ((ex - sx) << 16) / ey;
1986 for(y= 0; y <= ey; y++){
1988 fr = (y*f) & 0xFFFF;
1989 buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
1990 if(fr) buf[y * stride + x + 1] += (color * fr ) >> 16;
1996 * Draw an arrow from (ex, ey) -> (sx, sy).
1997 * @param w width of the image
1998 * @param h height of the image
1999 * @param stride stride/linesize of the image
2000 * @param color color of the arrow
2002 static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
2003 int ey, int w, int h, int stride, int color)
2007 sx = av_clip(sx, -100, w + 100);
2008 sy = av_clip(sy, -100, h + 100);
2009 ex = av_clip(ex, -100, w + 100);
2010 ey = av_clip(ey, -100, h + 100);
2015 if (dx * dx + dy * dy > 3 * 3) {
2018 int length = ff_sqrt((rx * rx + ry * ry) << 8);
2020 // FIXME subpixel accuracy
2021 rx = ROUNDED_DIV(rx * 3 << 4, length);
2022 ry = ROUNDED_DIV(ry * 3 << 4, length);
2024 draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
2025 draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
2027 draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
2031 * Print debugging info for the given picture.
2033 void ff_print_debug_info2(AVCodecContext *avctx, AVFrame *pict, uint8_t *mbskip_table,
2034 uint32_t *mbtype_table, int8_t *qscale_table, int16_t (*motion_val[2])[2],
2036 int mb_width, int mb_height, int mb_stride, int quarter_sample)
2038 if (avctx->hwaccel || !mbtype_table
2039 || (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU))
2043 if (avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
2046 av_log(avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
2047 av_get_picture_type_char(pict->pict_type));
2048 for (y = 0; y < mb_height; y++) {
2049 for (x = 0; x < mb_width; x++) {
2050 if (avctx->debug & FF_DEBUG_SKIP) {
2051 int count = mbskip_table[x + y * mb_stride];
2054 av_log(avctx, AV_LOG_DEBUG, "%1d", count);
2056 if (avctx->debug & FF_DEBUG_QP) {
2057 av_log(avctx, AV_LOG_DEBUG, "%2d",
2058 qscale_table[x + y * mb_stride]);
2060 if (avctx->debug & FF_DEBUG_MB_TYPE) {
2061 int mb_type = mbtype_table[x + y * mb_stride];
2062 // Type & MV direction
2063 if (IS_PCM(mb_type))
2064 av_log(avctx, AV_LOG_DEBUG, "P");
2065 else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
2066 av_log(avctx, AV_LOG_DEBUG, "A");
2067 else if (IS_INTRA4x4(mb_type))
2068 av_log(avctx, AV_LOG_DEBUG, "i");
2069 else if (IS_INTRA16x16(mb_type))
2070 av_log(avctx, AV_LOG_DEBUG, "I");
2071 else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
2072 av_log(avctx, AV_LOG_DEBUG, "d");
2073 else if (IS_DIRECT(mb_type))
2074 av_log(avctx, AV_LOG_DEBUG, "D");
2075 else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
2076 av_log(avctx, AV_LOG_DEBUG, "g");
2077 else if (IS_GMC(mb_type))
2078 av_log(avctx, AV_LOG_DEBUG, "G");
2079 else if (IS_SKIP(mb_type))
2080 av_log(avctx, AV_LOG_DEBUG, "S");
2081 else if (!USES_LIST(mb_type, 1))
2082 av_log(avctx, AV_LOG_DEBUG, ">");
2083 else if (!USES_LIST(mb_type, 0))
2084 av_log(avctx, AV_LOG_DEBUG, "<");
2086 av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
2087 av_log(avctx, AV_LOG_DEBUG, "X");
2091 if (IS_8X8(mb_type))
2092 av_log(avctx, AV_LOG_DEBUG, "+");
2093 else if (IS_16X8(mb_type))
2094 av_log(avctx, AV_LOG_DEBUG, "-");
2095 else if (IS_8X16(mb_type))
2096 av_log(avctx, AV_LOG_DEBUG, "|");
2097 else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
2098 av_log(avctx, AV_LOG_DEBUG, " ");
2100 av_log(avctx, AV_LOG_DEBUG, "?");
2103 if (IS_INTERLACED(mb_type))
2104 av_log(avctx, AV_LOG_DEBUG, "=");
2106 av_log(avctx, AV_LOG_DEBUG, " ");
2109 av_log(avctx, AV_LOG_DEBUG, "\n");
2113 if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
2114 (avctx->debug_mv)) {
2115 const int shift = 1 + quarter_sample;
2119 int h_chroma_shift, v_chroma_shift, block_height;
2120 const int width = avctx->width;
2121 const int height = avctx->height;
2122 const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
2123 const int mv_stride = (mb_width << mv_sample_log2) +
2124 (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
2126 *low_delay = 0; // needed to see the vectors without trashing the buffers
2128 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
2130 av_frame_make_writable(pict);
2132 pict->opaque = NULL;
2133 ptr = pict->data[0];
2134 block_height = 16 >> v_chroma_shift;
2136 for (mb_y = 0; mb_y < mb_height; mb_y++) {
2138 for (mb_x = 0; mb_x < mb_width; mb_x++) {
2139 const int mb_index = mb_x + mb_y * mb_stride;
2140 if ((avctx->debug_mv) && motion_val[0]) {
2142 for (type = 0; type < 3; type++) {
2146 if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_P_FOR)) ||
2147 (pict->pict_type!= AV_PICTURE_TYPE_P))
2152 if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_FOR)) ||
2153 (pict->pict_type!= AV_PICTURE_TYPE_B))
2158 if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_BACK)) ||
2159 (pict->pict_type!= AV_PICTURE_TYPE_B))
2164 if (!USES_LIST(mbtype_table[mb_index], direction))
2167 if (IS_8X8(mbtype_table[mb_index])) {
2169 for (i = 0; i < 4; i++) {
2170 int sx = mb_x * 16 + 4 + 8 * (i & 1);
2171 int sy = mb_y * 16 + 4 + 8 * (i >> 1);
2172 int xy = (mb_x * 2 + (i & 1) +
2173 (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
2174 int mx = (motion_val[direction][xy][0] >> shift) + sx;
2175 int my = (motion_val[direction][xy][1] >> shift) + sy;
2176 draw_arrow(ptr, sx, sy, mx, my, width,
2177 height, pict->linesize[0], 100);
2179 } else if (IS_16X8(mbtype_table[mb_index])) {
2181 for (i = 0; i < 2; i++) {
2182 int sx = mb_x * 16 + 8;
2183 int sy = mb_y * 16 + 4 + 8 * i;
2184 int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
2185 int mx = (motion_val[direction][xy][0] >> shift);
2186 int my = (motion_val[direction][xy][1] >> shift);
2188 if (IS_INTERLACED(mbtype_table[mb_index]))
2191 draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
2192 height, pict->linesize[0], 100);
2194 } else if (IS_8X16(mbtype_table[mb_index])) {
2196 for (i = 0; i < 2; i++) {
2197 int sx = mb_x * 16 + 4 + 8 * i;
2198 int sy = mb_y * 16 + 8;
2199 int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
2200 int mx = motion_val[direction][xy][0] >> shift;
2201 int my = motion_val[direction][xy][1] >> shift;
2203 if (IS_INTERLACED(mbtype_table[mb_index]))
2206 draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
2207 height, pict->linesize[0], 100);
2210 int sx= mb_x * 16 + 8;
2211 int sy= mb_y * 16 + 8;
2212 int xy= (mb_x + mb_y * mv_stride) << mv_sample_log2;
2213 int mx= (motion_val[direction][xy][0]>>shift) + sx;
2214 int my= (motion_val[direction][xy][1]>>shift) + sy;
2215 draw_arrow(ptr, sx, sy, mx, my, width, height, pict->linesize[0], 100);
2219 if ((avctx->debug & FF_DEBUG_VIS_QP)) {
2220 uint64_t c = (qscale_table[mb_index] * 128 / 31) *
2221 0x0101010101010101ULL;
2223 for (y = 0; y < block_height; y++) {
2224 *(uint64_t *)(pict->data[1] + 8 * mb_x +
2225 (block_height * mb_y + y) *
2226 pict->linesize[1]) = c;
2227 *(uint64_t *)(pict->data[2] + 8 * mb_x +
2228 (block_height * mb_y + y) *
2229 pict->linesize[2]) = c;
2232 if ((avctx->debug & FF_DEBUG_VIS_MB_TYPE) &&
2234 int mb_type = mbtype_table[mb_index];
2237 #define COLOR(theta, r) \
2238 u = (int)(128 + r * cos(theta * 3.141592 / 180)); \
2239 v = (int)(128 + r * sin(theta * 3.141592 / 180));
2243 if (IS_PCM(mb_type)) {
2245 } else if ((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) ||
2246 IS_INTRA16x16(mb_type)) {
2248 } else if (IS_INTRA4x4(mb_type)) {
2250 } else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) {
2252 } else if (IS_DIRECT(mb_type)) {
2254 } else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) {
2256 } else if (IS_GMC(mb_type)) {
2258 } else if (IS_SKIP(mb_type)) {
2260 } else if (!USES_LIST(mb_type, 1)) {
2262 } else if (!USES_LIST(mb_type, 0)) {
2265 av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
2269 u *= 0x0101010101010101ULL;
2270 v *= 0x0101010101010101ULL;
2271 for (y = 0; y < block_height; y++) {
2272 *(uint64_t *)(pict->data[1] + 8 * mb_x +
2273 (block_height * mb_y + y) * pict->linesize[1]) = u;
2274 *(uint64_t *)(pict->data[2] + 8 * mb_x +
2275 (block_height * mb_y + y) * pict->linesize[2]) = v;
2279 if (IS_8X8(mb_type) || IS_16X8(mb_type)) {
2280 *(uint64_t *)(pict->data[0] + 16 * mb_x + 0 +
2281 (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
2282 *(uint64_t *)(pict->data[0] + 16 * mb_x + 8 +
2283 (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
2285 if (IS_8X8(mb_type) || IS_8X16(mb_type)) {
2286 for (y = 0; y < 16; y++)
2287 pict->data[0][16 * mb_x + 8 + (16 * mb_y + y) *
2288 pict->linesize[0]] ^= 0x80;
2290 if (IS_8X8(mb_type) && mv_sample_log2 >= 2) {
2291 int dm = 1 << (mv_sample_log2 - 2);
2292 for (i = 0; i < 4; i++) {
2293 int sx = mb_x * 16 + 8 * (i & 1);
2294 int sy = mb_y * 16 + 8 * (i >> 1);
2295 int xy = (mb_x * 2 + (i & 1) +
2296 (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
2298 int32_t *mv = (int32_t *) &motion_val[0][xy];
2299 if (mv[0] != mv[dm] ||
2300 mv[dm * mv_stride] != mv[dm * (mv_stride + 1)])
2301 for (y = 0; y < 8; y++)
2302 pict->data[0][sx + 4 + (sy + y) * pict->linesize[0]] ^= 0x80;
2303 if (mv[0] != mv[dm * mv_stride] || mv[dm] != mv[dm * (mv_stride + 1)])
2304 *(uint64_t *)(pict->data[0] + sx + (sy + 4) *
2305 pict->linesize[0]) ^= 0x8080808080808080ULL;
2309 if (IS_INTERLACED(mb_type) &&
2310 avctx->codec->id == AV_CODEC_ID_H264) {
2314 mbskip_table[mb_index] = 0;
2320 void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
2322 ff_print_debug_info2(s->avctx, pict, s->mbskip_table, p->mb_type,
2323 p->qscale_table, p->motion_val, &s->low_delay,
2324 s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
2327 int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
2329 AVBufferRef *ref = av_buffer_ref(p->qscale_table_buf);
2330 int offset = 2*s->mb_stride + 1;
2332 return AVERROR(ENOMEM);
2333 av_assert0(ref->size >= offset + s->mb_stride * ((f->height+15)/16));
2334 ref->size -= offset;
2335 ref->data += offset;
2336 return av_frame_set_qp_table(f, ref, s->mb_stride, qp_type);
2339 static inline int hpel_motion_lowres(MpegEncContext *s,
2340 uint8_t *dest, uint8_t *src,
2341 int field_based, int field_select,
2342 int src_x, int src_y,
2343 int width, int height, ptrdiff_t stride,
2344 int h_edge_pos, int v_edge_pos,
2345 int w, int h, h264_chroma_mc_func *pix_op,
2346 int motion_x, int motion_y)
2348 const int lowres = s->avctx->lowres;
2349 const int op_index = FFMIN(lowres, 3);
2350 const int s_mask = (2 << lowres) - 1;
2354 if (s->quarter_sample) {
2359 sx = motion_x & s_mask;
2360 sy = motion_y & s_mask;
2361 src_x += motion_x >> lowres + 1;
2362 src_y += motion_y >> lowres + 1;
2364 src += src_y * stride + src_x;
2366 if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
2367 (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
2368 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
2369 s->linesize, s->linesize,
2370 w + 1, (h + 1) << field_based,
2371 src_x, src_y << field_based,
2372 h_edge_pos, v_edge_pos);
2373 src = s->edge_emu_buffer;
2377 sx = (sx << 2) >> lowres;
2378 sy = (sy << 2) >> lowres;
2381 pix_op[op_index](dest, src, stride, h, sx, sy);
2385 /* apply one mpeg motion vector to the three components */
2386 static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
2393 uint8_t **ref_picture,
2394 h264_chroma_mc_func *pix_op,
2395 int motion_x, int motion_y,
2398 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2399 int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
2400 ptrdiff_t uvlinesize, linesize;
2401 const int lowres = s->avctx->lowres;
2402 const int op_index = FFMIN(lowres-1+s->chroma_x_shift, 3);
2403 const int block_s = 8>>lowres;
2404 const int s_mask = (2 << lowres) - 1;
2405 const int h_edge_pos = s->h_edge_pos >> lowres;
2406 const int v_edge_pos = s->v_edge_pos >> lowres;
2407 linesize = s->current_picture.f->linesize[0] << field_based;
2408 uvlinesize = s->current_picture.f->linesize[1] << field_based;
2410 // FIXME obviously not perfect but qpel will not work in lowres anyway
2411 if (s->quarter_sample) {
2417 motion_y += (bottom_field - field_select)*((1 << lowres)-1);
2420 sx = motion_x & s_mask;
2421 sy = motion_y & s_mask;
2422 src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
2423 src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
2425 if (s->out_format == FMT_H263) {
2426 uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
2427 uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
2428 uvsrc_x = src_x >> 1;
2429 uvsrc_y = src_y >> 1;
2430 } else if (s->out_format == FMT_H261) {
2431 // even chroma mv's are full pel in H261
2434 uvsx = (2 * mx) & s_mask;
2435 uvsy = (2 * my) & s_mask;
2436 uvsrc_x = s->mb_x * block_s + (mx >> lowres);
2437 uvsrc_y = mb_y * block_s + (my >> lowres);
2439 if(s->chroma_y_shift){
2444 uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
2445 uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
2447 if(s->chroma_x_shift){
2451 uvsy = motion_y & s_mask;
2453 uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
2456 uvsx = motion_x & s_mask;
2457 uvsy = motion_y & s_mask;
2464 ptr_y = ref_picture[0] + src_y * linesize + src_x;
2465 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
2466 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
2468 if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) || uvsrc_y<0 ||
2469 (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
2470 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
2471 linesize >> field_based, linesize >> field_based,
2472 17, 17 + field_based,
2473 src_x, src_y << field_based, h_edge_pos,
2475 ptr_y = s->edge_emu_buffer;
2476 if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
2477 uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;
2478 s->vdsp.emulated_edge_mc(uvbuf, ptr_cb,
2479 uvlinesize >> field_based, uvlinesize >> field_based,
2481 uvsrc_x, uvsrc_y << field_based,
2482 h_edge_pos >> 1, v_edge_pos >> 1);
2483 s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr,
2484 uvlinesize >> field_based,uvlinesize >> field_based,
2486 uvsrc_x, uvsrc_y << field_based,
2487 h_edge_pos >> 1, v_edge_pos >> 1);
2489 ptr_cr = uvbuf + 16;
2493 // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f->data
2495 dest_y += s->linesize;
2496 dest_cb += s->uvlinesize;
2497 dest_cr += s->uvlinesize;
2501 ptr_y += s->linesize;
2502 ptr_cb += s->uvlinesize;
2503 ptr_cr += s->uvlinesize;
2506 sx = (sx << 2) >> lowres;
2507 sy = (sy << 2) >> lowres;
2508 pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
2510 if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
2511 int hc = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h;
2512 uvsx = (uvsx << 2) >> lowres;
2513 uvsy = (uvsy << 2) >> lowres;
2515 pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy);
2516 pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy);
2519 // FIXME h261 lowres loop filter
2522 static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
2523 uint8_t *dest_cb, uint8_t *dest_cr,
2524 uint8_t **ref_picture,
2525 h264_chroma_mc_func * pix_op,
2528 const int lowres = s->avctx->lowres;
2529 const int op_index = FFMIN(lowres, 3);
2530 const int block_s = 8 >> lowres;
2531 const int s_mask = (2 << lowres) - 1;
2532 const int h_edge_pos = s->h_edge_pos >> lowres + 1;
2533 const int v_edge_pos = s->v_edge_pos >> lowres + 1;
2534 int emu = 0, src_x, src_y, sx, sy;
2538 if (s->quarter_sample) {
2543 /* In case of 8X8, we construct a single chroma motion vector
2544 with a special rounding */
2545 mx = ff_h263_round_chroma(mx);
2546 my = ff_h263_round_chroma(my);
2550 src_x = s->mb_x * block_s + (mx >> lowres + 1);
2551 src_y = s->mb_y * block_s + (my >> lowres + 1);
2553 offset = src_y * s->uvlinesize + src_x;
2554 ptr = ref_picture[1] + offset;
2555 if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
2556 (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
2557 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
2558 s->uvlinesize, s->uvlinesize,
2560 src_x, src_y, h_edge_pos, v_edge_pos);
2561 ptr = s->edge_emu_buffer;
2564 sx = (sx << 2) >> lowres;
2565 sy = (sy << 2) >> lowres;
2566 pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
2568 ptr = ref_picture[2] + offset;
2570 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
2571 s->uvlinesize, s->uvlinesize,
2573 src_x, src_y, h_edge_pos, v_edge_pos);
2574 ptr = s->edge_emu_buffer;
2576 pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
2580 * motion compensation of a single macroblock
2582 * @param dest_y luma destination pointer
2583 * @param dest_cb chroma cb/u destination pointer
2584 * @param dest_cr chroma cr/v destination pointer
2585 * @param dir direction (0->forward, 1->backward)
2586 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
2587 * @param pix_op halfpel motion compensation function (average or put normally)
2588 * the motion vectors are taken from s->mv and the MV type from s->mv_type
2590 static inline void MPV_motion_lowres(MpegEncContext *s,
2591 uint8_t *dest_y, uint8_t *dest_cb,
2593 int dir, uint8_t **ref_picture,
2594 h264_chroma_mc_func *pix_op)
2598 const int lowres = s->avctx->lowres;
2599 const int block_s = 8 >>lowres;
2604 switch (s->mv_type) {
2606 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2608 ref_picture, pix_op,
2609 s->mv[dir][0][0], s->mv[dir][0][1],
2615 for (i = 0; i < 4; i++) {
2616 hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
2617 s->linesize) * block_s,
2618 ref_picture[0], 0, 0,
2619 (2 * mb_x + (i & 1)) * block_s,
2620 (2 * mb_y + (i >> 1)) * block_s,
2621 s->width, s->height, s->linesize,
2622 s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
2623 block_s, block_s, pix_op,
2624 s->mv[dir][i][0], s->mv[dir][i][1]);
2626 mx += s->mv[dir][i][0];
2627 my += s->mv[dir][i][1];
2630 if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY))
2631 chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
2635 if (s->picture_structure == PICT_FRAME) {
2637 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2638 1, 0, s->field_select[dir][0],
2639 ref_picture, pix_op,
2640 s->mv[dir][0][0], s->mv[dir][0][1],
2643 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2644 1, 1, s->field_select[dir][1],
2645 ref_picture, pix_op,
2646 s->mv[dir][1][0], s->mv[dir][1][1],
2649 if (s->picture_structure != s->field_select[dir][0] + 1 &&
2650 s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
2651 ref_picture = s->current_picture_ptr->f->data;
2654 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2655 0, 0, s->field_select[dir][0],
2656 ref_picture, pix_op,
2658 s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
2662 for (i = 0; i < 2; i++) {
2663 uint8_t **ref2picture;
2665 if (s->picture_structure == s->field_select[dir][i] + 1 ||
2666 s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
2667 ref2picture = ref_picture;
2669 ref2picture = s->current_picture_ptr->f->data;
2672 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2673 0, 0, s->field_select[dir][i],
2674 ref2picture, pix_op,
2675 s->mv[dir][i][0], s->mv[dir][i][1] +
2676 2 * block_s * i, block_s, mb_y >> 1);
2678 dest_y += 2 * block_s * s->linesize;
2679 dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
2680 dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
2684 if (s->picture_structure == PICT_FRAME) {
2685 for (i = 0; i < 2; i++) {
2687 for (j = 0; j < 2; j++) {
2688 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2690 ref_picture, pix_op,
2691 s->mv[dir][2 * i + j][0],
2692 s->mv[dir][2 * i + j][1],
2695 pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
2698 for (i = 0; i < 2; i++) {
2699 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2700 0, 0, s->picture_structure != i + 1,
2701 ref_picture, pix_op,
2702 s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
2703 2 * block_s, mb_y >> 1);
2705 // after put we make avg of the same block
2706 pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
2708 // opposite parity is always in the same
2709 // frame if this is second field
2710 if (!s->first_field) {
2711 ref_picture = s->current_picture_ptr->f->data;
2722 * find the lowest MB row referenced in the MVs
2724 int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
2726 int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
2727 int my, off, i, mvs;
2729 if (s->picture_structure != PICT_FRAME || s->mcsel)
2732 switch (s->mv_type) {
2746 for (i = 0; i < mvs; i++) {
2747 my = s->mv[dir][i][1]<<qpel_shift;
2748 my_max = FFMAX(my_max, my);
2749 my_min = FFMIN(my_min, my);
2752 off = (FFMAX(-my_min, my_max) + 63) >> 6;
2754 return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
2756 return s->mb_height-1;
2759 /* put block[] to dest[] */
2760 static inline void put_dct(MpegEncContext *s,
2761 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
2763 s->dct_unquantize_intra(s, block, i, qscale);
2764 s->dsp.idct_put (dest, line_size, block);
2767 /* add block[] to dest[] */
2768 static inline void add_dct(MpegEncContext *s,
2769 int16_t *block, int i, uint8_t *dest, int line_size)
2771 if (s->block_last_index[i] >= 0) {
2772 s->dsp.idct_add (dest, line_size, block);
2776 static inline void add_dequant_dct(MpegEncContext *s,
2777 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
2779 if (s->block_last_index[i] >= 0) {
2780 s->dct_unquantize_inter(s, block, i, qscale);
2782 s->dsp.idct_add (dest, line_size, block);
2787 * Clean dc, ac, coded_block for the current non-intra MB.
2789 void ff_clean_intra_table_entries(MpegEncContext *s)
2791 int wrap = s->b8_stride;
2792 int xy = s->block_index[0];
2795 s->dc_val[0][xy + 1 ] =
2796 s->dc_val[0][xy + wrap] =
2797 s->dc_val[0][xy + 1 + wrap] = 1024;
2799 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
2800 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
2801 if (s->msmpeg4_version>=3) {
2802 s->coded_block[xy ] =
2803 s->coded_block[xy + 1 ] =
2804 s->coded_block[xy + wrap] =
2805 s->coded_block[xy + 1 + wrap] = 0;
2808 wrap = s->mb_stride;
2809 xy = s->mb_x + s->mb_y * wrap;
2811 s->dc_val[2][xy] = 1024;
2813 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
2814 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
2816 s->mbintra_table[xy]= 0;
2819 /* generic function called after a macroblock has been parsed by the
2820 decoder or after it has been encoded by the encoder.
2822 Important variables used:
2823 s->mb_intra : true if intra macroblock
2824 s->mv_dir : motion vector direction
2825 s->mv_type : motion vector type
2826 s->mv : motion vector
2827 s->interlaced_dct : true if interlaced dct used (mpeg2)
2829 static av_always_inline
2830 void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
2831 int lowres_flag, int is_mpeg12)
2833 const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
2836 s->avctx->hwaccel && s->avctx->hwaccel->decode_mb) {
2837 s->avctx->hwaccel->decode_mb(s);//xvmc uses pblocks
2841 if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
2842 /* print DCT coefficients */
2844 av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
2846 for(j=0; j<64; j++){
2847 av_log(s->avctx, AV_LOG_DEBUG, "%5d", block[i][s->dsp.idct_permutation[j]]);
2849 av_log(s->avctx, AV_LOG_DEBUG, "\n");
2853 s->current_picture.qscale_table[mb_xy] = s->qscale;
2855 /* update DC predictors for P macroblocks */
2857 if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
2858 if(s->mbintra_table[mb_xy])
2859 ff_clean_intra_table_entries(s);
2863 s->last_dc[2] = 128 << s->intra_dc_precision;
2866 else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
2867 s->mbintra_table[mb_xy]=1;
2869 if ( (s->flags&CODEC_FLAG_PSNR)
2870 || s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor
2871 || !(s->encoding && (s->intra_only || s->pict_type==AV_PICTURE_TYPE_B) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc
2872 uint8_t *dest_y, *dest_cb, *dest_cr;
2873 int dct_linesize, dct_offset;
2874 op_pixels_func (*op_pix)[4];
2875 qpel_mc_func (*op_qpix)[16];
2876 const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
2877 const int uvlinesize = s->current_picture.f->linesize[1];
2878 const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
2879 const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
2881 /* avoid copy if macroblock skipped in last frame too */
2882 /* skip only during decoding as we might trash the buffers during encoding a bit */
2884 uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
2886 if (s->mb_skipped) {
2888 av_assert2(s->pict_type!=AV_PICTURE_TYPE_I);
2890 } else if(!s->current_picture.reference) {
2893 *mbskip_ptr = 0; /* not skipped */
2897 dct_linesize = linesize << s->interlaced_dct;
2898 dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
2902 dest_cb= s->dest[1];
2903 dest_cr= s->dest[2];
2905 dest_y = s->b_scratchpad;
2906 dest_cb= s->b_scratchpad+16*linesize;
2907 dest_cr= s->b_scratchpad+32*linesize;
2911 /* motion handling */
2912 /* decoding or more than one mb_type (MC was already done otherwise) */
2915 if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
2916 if (s->mv_dir & MV_DIR_FORWARD) {
2917 ff_thread_await_progress(&s->last_picture_ptr->tf,
2918 ff_MPV_lowest_referenced_row(s, 0),
2921 if (s->mv_dir & MV_DIR_BACKWARD) {
2922 ff_thread_await_progress(&s->next_picture_ptr->tf,
2923 ff_MPV_lowest_referenced_row(s, 1),
2929 h264_chroma_mc_func *op_pix = s->h264chroma.put_h264_chroma_pixels_tab;
2931 if (s->mv_dir & MV_DIR_FORWARD) {
2932 MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix);
2933 op_pix = s->h264chroma.avg_h264_chroma_pixels_tab;
2935 if (s->mv_dir & MV_DIR_BACKWARD) {
2936 MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix);
2939 op_qpix = s->me.qpel_put;
2940 if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
2941 op_pix = s->hdsp.put_pixels_tab;
2943 op_pix = s->hdsp.put_no_rnd_pixels_tab;
2945 if (s->mv_dir & MV_DIR_FORWARD) {
2946 ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix, op_qpix);
2947 op_pix = s->hdsp.avg_pixels_tab;
2948 op_qpix= s->me.qpel_avg;
2950 if (s->mv_dir & MV_DIR_BACKWARD) {
2951 ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix, op_qpix);
2956 /* skip dequant / idct if we are really late ;) */
2957 if(s->avctx->skip_idct){
2958 if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
2959 ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
2960 || s->avctx->skip_idct >= AVDISCARD_ALL)
2964 /* add dct residue */
2965 if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
2966 || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
2967 add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
2968 add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
2969 add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
2970 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
2972 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2973 if (s->chroma_y_shift){
2974 add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
2975 add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
2979 add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
2980 add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
2981 add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
2982 add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
2985 } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
2986 add_dct(s, block[0], 0, dest_y , dct_linesize);
2987 add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
2988 add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
2989 add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
2991 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2992 if(s->chroma_y_shift){//Chroma420
2993 add_dct(s, block[4], 4, dest_cb, uvlinesize);
2994 add_dct(s, block[5], 5, dest_cr, uvlinesize);
2997 dct_linesize = uvlinesize << s->interlaced_dct;
2998 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
3000 add_dct(s, block[4], 4, dest_cb, dct_linesize);
3001 add_dct(s, block[5], 5, dest_cr, dct_linesize);
3002 add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
3003 add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
3004 if(!s->chroma_x_shift){//Chroma444
3005 add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
3006 add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
3007 add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
3008 add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
3013 else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
3014 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
3017 /* dct only in intra block */
3018 if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
3019 put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
3020 put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
3021 put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
3022 put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
3024 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
3025 if(s->chroma_y_shift){
3026 put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
3027 put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
3031 put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
3032 put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
3033 put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
3034 put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
3038 s->dsp.idct_put(dest_y , dct_linesize, block[0]);
3039 s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
3040 s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
3041 s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
3043 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
3044 if(s->chroma_y_shift){
3045 s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
3046 s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
3049 dct_linesize = uvlinesize << s->interlaced_dct;
3050 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
3052 s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
3053 s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
3054 s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
3055 s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
3056 if(!s->chroma_x_shift){//Chroma444
3057 s->dsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
3058 s->dsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
3059 s->dsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
3060 s->dsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
3068 s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
3069 s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
3070 s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
3075 void ff_MPV_decode_mb(MpegEncContext *s, int16_t block[12][64]){
3077 if(s->out_format == FMT_MPEG1) {
3078 if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
3079 else MPV_decode_mb_internal(s, block, 0, 1);
3082 if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
3083 else MPV_decode_mb_internal(s, block, 0, 0);
3086 void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
3088 ff_draw_horiz_band(s->avctx, s->current_picture_ptr->f,
3089 s->last_picture_ptr ? s->last_picture_ptr->f : NULL, y, h, s->picture_structure,
3090 s->first_field, s->low_delay);
3093 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
3094 const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
3095 const int uvlinesize = s->current_picture.f->linesize[1];
3096 const int mb_size= 4 - s->avctx->lowres;
3098 s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
3099 s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
3100 s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
3101 s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
3102 s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
3103 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;
3104 //block_index is not used by mpeg2, so it is not affected by chroma_format
3106 s->dest[0] = s->current_picture.f->data[0] + ((s->mb_x - 1) << mb_size);
3107 s->dest[1] = s->current_picture.f->data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
3108 s->dest[2] = s->current_picture.f->data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
3110 if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
3112 if(s->picture_structure==PICT_FRAME){
3113 s->dest[0] += s->mb_y * linesize << mb_size;
3114 s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
3115 s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
3117 s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
3118 s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
3119 s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
3120 av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
3126 * Permute an 8x8 block.
3127 * @param block the block which will be permuted according to the given permutation vector
3128 * @param permutation the permutation vector
3129 * @param last the last non zero coefficient in scantable order, used to speed the permutation up
3130 * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
3131 * (inverse) permutated to scantable order!
3133 void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
3139 //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
3141 for(i=0; i<=last; i++){
3142 const int j= scantable[i];
3147 for(i=0; i<=last; i++){
3148 const int j= scantable[i];
3149 const int perm_j= permutation[j];
3150 block[perm_j]= temp[j];
3154 void ff_mpeg_flush(AVCodecContext *avctx){
3156 MpegEncContext *s = avctx->priv_data;
3158 if(s==NULL || s->picture==NULL)
3161 for (i = 0; i < MAX_PICTURE_COUNT; i++)
3162 ff_mpeg_unref_picture(s, &s->picture[i]);
3163 s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
3165 ff_mpeg_unref_picture(s, &s->current_picture);
3166 ff_mpeg_unref_picture(s, &s->last_picture);
3167 ff_mpeg_unref_picture(s, &s->next_picture);
3169 s->mb_x= s->mb_y= 0;
3172 s->parse_context.state= -1;
3173 s->parse_context.frame_start_found= 0;
3174 s->parse_context.overread= 0;
3175 s->parse_context.overread_index= 0;
3176 s->parse_context.index= 0;
3177 s->parse_context.last_index= 0;
3178 s->bitstream_buffer_size=0;
3183 * set qscale and update qscale dependent variables.
3185 void ff_set_qscale(MpegEncContext * s, int qscale)
3189 else if (qscale > 31)
3193 s->chroma_qscale= s->chroma_qscale_table[qscale];
3195 s->y_dc_scale= s->y_dc_scale_table[ qscale ];
3196 s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
3199 void ff_MPV_report_decode_progress(MpegEncContext *s)
3201 if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
3202 ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
3205 #if CONFIG_ERROR_RESILIENCE
3206 void ff_mpeg_set_erpic(ERPicture *dst, Picture *src)
3210 memset(dst, 0, sizeof(*dst));
3217 for (i = 0; i < 2; i++) {
3218 dst->motion_val[i] = src->motion_val[i];
3219 dst->ref_index[i] = src->ref_index[i];
3222 dst->mb_type = src->mb_type;
3223 dst->field_picture = src->field_picture;
3226 void ff_mpeg_er_frame_start(MpegEncContext *s)
3228 ERContext *er = &s->er;
3230 ff_mpeg_set_erpic(&er->cur_pic, s->current_picture_ptr);
3231 ff_mpeg_set_erpic(&er->next_pic, s->next_picture_ptr);
3232 ff_mpeg_set_erpic(&er->last_pic, s->last_picture_ptr);
3234 er->pp_time = s->pp_time;
3235 er->pb_time = s->pb_time;
3236 er->quarter_sample = s->quarter_sample;
3237 er->partitioned_frame = s->partitioned_frame;
3239 ff_er_frame_start(er);
3241 #endif /* CONFIG_ERROR_RESILIENCE */