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/avassert.h"
31 #include "libavutil/imgutils.h"
34 #include "h264chroma.h"
37 #include "mpegvideo.h"
40 #include "xvmc_internal.h"
47 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
48 int16_t *block, int n, int qscale);
49 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
50 int16_t *block, int n, int qscale);
51 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
52 int16_t *block, int n, int qscale);
53 static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
54 int16_t *block, int n, int qscale);
55 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
56 int16_t *block, int n, int qscale);
57 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
58 int16_t *block, int n, int qscale);
59 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
60 int16_t *block, int n, int qscale);
66 static const uint8_t ff_default_chroma_qscale_table[32] = {
67 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
68 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
69 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
72 const uint8_t ff_mpeg1_dc_scale_table[128] = {
73 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
74 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
75 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
76 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
77 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
78 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
79 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
80 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
81 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
84 static const uint8_t mpeg2_dc_scale_table1[128] = {
85 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
86 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
87 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
88 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
89 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
90 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
91 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
92 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
93 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
96 static const uint8_t mpeg2_dc_scale_table2[128] = {
97 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
98 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
99 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
100 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
101 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
102 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
103 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
104 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
105 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
108 static const uint8_t mpeg2_dc_scale_table3[128] = {
109 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
110 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
111 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
112 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
113 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
114 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
115 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
116 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
117 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
120 const uint8_t *const ff_mpeg2_dc_scale_table[4] = {
121 ff_mpeg1_dc_scale_table,
122 mpeg2_dc_scale_table1,
123 mpeg2_dc_scale_table2,
124 mpeg2_dc_scale_table3,
127 const enum AVPixelFormat ff_pixfmt_list_420[] = {
132 static void mpeg_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
134 int mb_x, int mb_y, int mb_intra, int mb_skipped)
136 MpegEncContext *s = opaque;
139 s->mv_type = mv_type;
140 s->mb_intra = mb_intra;
141 s->mb_skipped = mb_skipped;
144 memcpy(s->mv, mv, sizeof(*mv));
146 ff_init_block_index(s);
147 ff_update_block_index(s);
149 s->dsp.clear_blocks(s->block[0]);
151 s->dest[0] = s->current_picture.f.data[0] + (s->mb_y * 16 * s->linesize) + s->mb_x * 16;
152 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);
153 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);
156 ff_MPV_decode_mb(s, s->block);
159 const uint8_t *avpriv_mpv_find_start_code(const uint8_t *av_restrict p,
161 uint32_t *av_restrict state)
169 for (i = 0; i < 3; i++) {
170 uint32_t tmp = *state << 8;
171 *state = tmp + *(p++);
172 if (tmp == 0x100 || p == end)
177 if (p[-1] > 1 ) p += 3;
178 else if (p[-2] ) p += 2;
179 else if (p[-3]|(p[-1]-1)) p++;
186 p = FFMIN(p, end) - 4;
192 /* init common dct for both encoder and decoder */
193 av_cold int ff_dct_common_init(MpegEncContext *s)
195 ff_dsputil_init(&s->dsp, s->avctx);
196 ff_h264chroma_init(&s->h264chroma, 8); //for lowres
197 ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
198 ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
200 s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
201 s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
202 s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
203 s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
204 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
205 if (s->flags & CODEC_FLAG_BITEXACT)
206 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
207 s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
210 ff_MPV_common_init_x86(s);
212 ff_MPV_common_init_axp(s);
214 ff_MPV_common_init_arm(s);
216 ff_MPV_common_init_altivec(s);
218 ff_MPV_common_init_bfin(s);
221 /* load & permutate scantables
222 * note: only wmv uses different ones
224 if (s->alternate_scan) {
225 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
226 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
228 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
229 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
231 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
232 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
237 int ff_mpv_frame_size_alloc(MpegEncContext *s, int linesize)
239 int alloc_size = FFALIGN(FFABS(linesize) + 64, 32);
241 // edge emu needs blocksize + filter length - 1
242 // (= 17x17 for halfpel / 21x21 for h264)
243 // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9
244 // at uvlinesize. It supports only YUV420 so 24x24 is enough
245 // linesize * interlaced * MBsize
246 FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, alloc_size * 4 * 24,
249 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, alloc_size * 4 * 16 * 2,
251 s->me.temp = s->me.scratchpad;
252 s->rd_scratchpad = s->me.scratchpad;
253 s->b_scratchpad = s->me.scratchpad;
254 s->obmc_scratchpad = s->me.scratchpad + 16;
258 av_freep(&s->edge_emu_buffer);
259 return AVERROR(ENOMEM);
263 * Allocate a frame buffer
265 static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
269 if (s->avctx->hwaccel) {
270 assert(!pic->hwaccel_picture_private);
271 if (s->avctx->hwaccel->priv_data_size) {
272 pic->hwaccel_picture_private = av_mallocz(s->avctx->hwaccel->priv_data_size);
273 if (!pic->hwaccel_picture_private) {
274 av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
281 if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
282 s->codec_id != AV_CODEC_ID_VC1IMAGE &&
283 s->codec_id != AV_CODEC_ID_MSS2)
284 r = ff_thread_get_buffer(s->avctx, &pic->tf,
285 pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
287 pic->f.width = s->avctx->width;
288 pic->f.height = s->avctx->height;
289 pic->f.format = s->avctx->pix_fmt;
290 r = avcodec_default_get_buffer2(s->avctx, &pic->f, 0);
293 if (r < 0 || !pic->f.data[0]) {
294 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
296 av_freep(&pic->hwaccel_picture_private);
300 if (s->linesize && (s->linesize != pic->f.linesize[0] ||
301 s->uvlinesize != pic->f.linesize[1])) {
302 av_log(s->avctx, AV_LOG_ERROR,
303 "get_buffer() failed (stride changed)\n");
304 ff_mpeg_unref_picture(s, pic);
308 if (pic->f.linesize[1] != pic->f.linesize[2]) {
309 av_log(s->avctx, AV_LOG_ERROR,
310 "get_buffer() failed (uv stride mismatch)\n");
311 ff_mpeg_unref_picture(s, pic);
315 if (!s->edge_emu_buffer &&
316 (ret = ff_mpv_frame_size_alloc(s, pic->f.linesize[0])) < 0) {
317 av_log(s->avctx, AV_LOG_ERROR,
318 "get_buffer() failed to allocate context scratch buffers.\n");
319 ff_mpeg_unref_picture(s, pic);
326 static void free_picture_tables(Picture *pic)
330 av_buffer_unref(&pic->mb_var_buf);
331 av_buffer_unref(&pic->mc_mb_var_buf);
332 av_buffer_unref(&pic->mb_mean_buf);
333 av_buffer_unref(&pic->mbskip_table_buf);
334 av_buffer_unref(&pic->qscale_table_buf);
335 av_buffer_unref(&pic->mb_type_buf);
337 for (i = 0; i < 2; i++) {
338 av_buffer_unref(&pic->motion_val_buf[i]);
339 av_buffer_unref(&pic->ref_index_buf[i]);
343 static int alloc_picture_tables(MpegEncContext *s, Picture *pic)
345 const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;
346 const int mb_array_size = s->mb_stride * s->mb_height;
347 const int b8_array_size = s->b8_stride * s->mb_height * 2;
351 pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
352 pic->qscale_table_buf = av_buffer_allocz(big_mb_num + s->mb_stride);
353 pic->mb_type_buf = av_buffer_allocz((big_mb_num + s->mb_stride) *
355 if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
356 return AVERROR(ENOMEM);
359 pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
360 pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
361 pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
362 if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
363 return AVERROR(ENOMEM);
366 if (s->out_format == FMT_H263 || s->encoding ||
367 (s->avctx->debug & FF_DEBUG_MV) || s->avctx->debug_mv) {
368 int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
369 int ref_index_size = 4 * mb_array_size;
371 for (i = 0; mv_size && i < 2; i++) {
372 pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
373 pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
374 if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
375 return AVERROR(ENOMEM);
382 static int make_tables_writable(Picture *pic)
385 #define MAKE_WRITABLE(table) \
388 (ret = av_buffer_make_writable(&pic->table)) < 0)\
392 MAKE_WRITABLE(mb_var_buf);
393 MAKE_WRITABLE(mc_mb_var_buf);
394 MAKE_WRITABLE(mb_mean_buf);
395 MAKE_WRITABLE(mbskip_table_buf);
396 MAKE_WRITABLE(qscale_table_buf);
397 MAKE_WRITABLE(mb_type_buf);
399 for (i = 0; i < 2; i++) {
400 MAKE_WRITABLE(motion_val_buf[i]);
401 MAKE_WRITABLE(ref_index_buf[i]);
408 * Allocate a Picture.
409 * The pixels are allocated/set by calling get_buffer() if shared = 0
411 int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
415 if (pic->qscale_table_buf)
416 if (pic->mbskip_table_buf->size < s->mb_stride * s->mb_height + 2 ||
417 pic->qscale_table_buf->size < s->mb_stride * (s->mb_height + 1) + 1 + s->mb_stride)
418 free_picture_tables(pic);
421 assert(pic->f.data[0]);
424 assert(!pic->f.data[0]);
426 if (alloc_frame_buffer(s, pic) < 0)
429 s->linesize = pic->f.linesize[0];
430 s->uvlinesize = pic->f.linesize[1];
433 if (!pic->qscale_table_buf)
434 ret = alloc_picture_tables(s, pic);
436 ret = make_tables_writable(pic);
441 pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
442 pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
443 pic->mb_mean = pic->mb_mean_buf->data;
446 pic->mbskip_table = pic->mbskip_table_buf->data;
447 pic->qscale_table = pic->qscale_table_buf->data + 2 * s->mb_stride + 1;
448 pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;
450 if (pic->motion_val_buf[0]) {
451 for (i = 0; i < 2; i++) {
452 pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
453 pic->ref_index[i] = pic->ref_index_buf[i]->data;
459 av_log(s->avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
460 ff_mpeg_unref_picture(s, pic);
461 free_picture_tables(pic);
462 return AVERROR(ENOMEM);
466 * Deallocate a picture.
468 void ff_mpeg_unref_picture(MpegEncContext *s, Picture *pic)
470 int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
471 pic->period_since_free = 0;
474 /* WM Image / Screen codecs allocate internal buffers with different
475 * dimensions / colorspaces; ignore user-defined callbacks for these. */
476 if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
477 s->codec_id != AV_CODEC_ID_VC1IMAGE &&
478 s->codec_id != AV_CODEC_ID_MSS2)
479 ff_thread_release_buffer(s->avctx, &pic->tf);
481 av_frame_unref(&pic->f);
483 av_buffer_unref(&pic->hwaccel_priv_buf);
485 memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
488 static int update_picture_tables(Picture *dst, Picture *src)
492 #define UPDATE_TABLE(table)\
495 (!dst->table || dst->table->buffer != src->table->buffer)) {\
496 av_buffer_unref(&dst->table);\
497 dst->table = av_buffer_ref(src->table);\
499 free_picture_tables(dst);\
500 return AVERROR(ENOMEM);\
505 UPDATE_TABLE(mb_var_buf);
506 UPDATE_TABLE(mc_mb_var_buf);
507 UPDATE_TABLE(mb_mean_buf);
508 UPDATE_TABLE(mbskip_table_buf);
509 UPDATE_TABLE(qscale_table_buf);
510 UPDATE_TABLE(mb_type_buf);
511 for (i = 0; i < 2; i++) {
512 UPDATE_TABLE(motion_val_buf[i]);
513 UPDATE_TABLE(ref_index_buf[i]);
516 dst->mb_var = src->mb_var;
517 dst->mc_mb_var = src->mc_mb_var;
518 dst->mb_mean = src->mb_mean;
519 dst->mbskip_table = src->mbskip_table;
520 dst->qscale_table = src->qscale_table;
521 dst->mb_type = src->mb_type;
522 for (i = 0; i < 2; i++) {
523 dst->motion_val[i] = src->motion_val[i];
524 dst->ref_index[i] = src->ref_index[i];
530 int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src)
534 av_assert0(!dst->f.buf[0]);
535 av_assert0(src->f.buf[0]);
539 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
543 ret = update_picture_tables(dst, src);
547 if (src->hwaccel_picture_private) {
548 dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
549 if (!dst->hwaccel_priv_buf)
551 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
554 dst->field_picture = src->field_picture;
555 dst->mb_var_sum = src->mb_var_sum;
556 dst->mc_mb_var_sum = src->mc_mb_var_sum;
557 dst->b_frame_score = src->b_frame_score;
558 dst->needs_realloc = src->needs_realloc;
559 dst->reference = src->reference;
560 dst->shared = src->shared;
564 ff_mpeg_unref_picture(s, dst);
568 static int init_duplicate_context(MpegEncContext *s)
570 int y_size = s->b8_stride * (2 * s->mb_height + 1);
571 int c_size = s->mb_stride * (s->mb_height + 1);
572 int yc_size = y_size + 2 * c_size;
580 s->obmc_scratchpad = NULL;
583 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
584 ME_MAP_SIZE * sizeof(uint32_t), fail)
585 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
586 ME_MAP_SIZE * sizeof(uint32_t), fail)
587 if (s->avctx->noise_reduction) {
588 FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
589 2 * 64 * sizeof(int), fail)
592 FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
593 s->block = s->blocks[0];
595 for (i = 0; i < 12; i++) {
596 s->pblocks[i] = &s->block[i];
599 if (s->out_format == FMT_H263) {
601 FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
602 yc_size * sizeof(int16_t) * 16, fail);
603 s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
604 s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
605 s->ac_val[2] = s->ac_val[1] + c_size;
610 return -1; // free() through ff_MPV_common_end()
613 static void free_duplicate_context(MpegEncContext *s)
618 av_freep(&s->edge_emu_buffer);
619 av_freep(&s->me.scratchpad);
623 s->obmc_scratchpad = NULL;
625 av_freep(&s->dct_error_sum);
626 av_freep(&s->me.map);
627 av_freep(&s->me.score_map);
628 av_freep(&s->blocks);
629 av_freep(&s->ac_val_base);
633 static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
635 #define COPY(a) bak->a = src->a
636 COPY(edge_emu_buffer);
641 COPY(obmc_scratchpad);
648 COPY(me.map_generation);
660 int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
664 // FIXME copy only needed parts
666 backup_duplicate_context(&bak, dst);
667 memcpy(dst, src, sizeof(MpegEncContext));
668 backup_duplicate_context(dst, &bak);
669 for (i = 0; i < 12; i++) {
670 dst->pblocks[i] = &dst->block[i];
672 if (!dst->edge_emu_buffer &&
673 (ret = ff_mpv_frame_size_alloc(dst, dst->linesize)) < 0) {
674 av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
675 "scratch buffers.\n");
678 // STOP_TIMER("update_duplicate_context")
679 // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
683 int ff_mpeg_update_thread_context(AVCodecContext *dst,
684 const AVCodecContext *src)
687 MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
694 // FIXME can parameters change on I-frames?
695 // in that case dst may need a reinit
696 if (!s->context_initialized) {
697 memcpy(s, s1, sizeof(MpegEncContext));
700 s->bitstream_buffer = NULL;
701 s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
703 if (s1->context_initialized){
704 // s->picture_range_start += MAX_PICTURE_COUNT;
705 // s->picture_range_end += MAX_PICTURE_COUNT;
706 if((ret = ff_MPV_common_init(s)) < 0){
707 memset(s, 0, sizeof(MpegEncContext));
714 if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
715 s->context_reinit = 0;
716 s->height = s1->height;
717 s->width = s1->width;
718 if ((ret = ff_MPV_common_frame_size_change(s)) < 0)
722 s->avctx->coded_height = s1->avctx->coded_height;
723 s->avctx->coded_width = s1->avctx->coded_width;
724 s->avctx->width = s1->avctx->width;
725 s->avctx->height = s1->avctx->height;
727 s->coded_picture_number = s1->coded_picture_number;
728 s->picture_number = s1->picture_number;
729 s->input_picture_number = s1->input_picture_number;
731 av_assert0(!s->picture || s->picture != s1->picture);
732 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
733 ff_mpeg_unref_picture(s, &s->picture[i]);
734 if (s1->picture[i].f.data[0] &&
735 (ret = ff_mpeg_ref_picture(s, &s->picture[i], &s1->picture[i])) < 0)
737 s->picture[i].period_since_free ++;
740 #define UPDATE_PICTURE(pic)\
742 ff_mpeg_unref_picture(s, &s->pic);\
743 if (s1->pic.f.data[0])\
744 ret = ff_mpeg_ref_picture(s, &s->pic, &s1->pic);\
746 ret = update_picture_tables(&s->pic, &s1->pic);\
751 UPDATE_PICTURE(current_picture);
752 UPDATE_PICTURE(last_picture);
753 UPDATE_PICTURE(next_picture);
755 s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
756 s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
757 s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
759 // Error/bug resilience
760 s->next_p_frame_damaged = s1->next_p_frame_damaged;
761 s->workaround_bugs = s1->workaround_bugs;
762 s->padding_bug_score = s1->padding_bug_score;
765 memcpy(&s->time_increment_bits, &s1->time_increment_bits,
766 (char *) &s1->shape - (char *) &s1->time_increment_bits);
769 s->max_b_frames = s1->max_b_frames;
770 s->low_delay = s1->low_delay;
771 s->droppable = s1->droppable;
773 // DivX handling (doesn't work)
774 s->divx_packed = s1->divx_packed;
776 if (s1->bitstream_buffer) {
777 if (s1->bitstream_buffer_size +
778 FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
779 av_fast_malloc(&s->bitstream_buffer,
780 &s->allocated_bitstream_buffer_size,
781 s1->allocated_bitstream_buffer_size);
782 s->bitstream_buffer_size = s1->bitstream_buffer_size;
783 memcpy(s->bitstream_buffer, s1->bitstream_buffer,
784 s1->bitstream_buffer_size);
785 memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
786 FF_INPUT_BUFFER_PADDING_SIZE);
789 // linesize dependend scratch buffer allocation
790 if (!s->edge_emu_buffer)
792 if (ff_mpv_frame_size_alloc(s, s1->linesize) < 0) {
793 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
794 "scratch buffers.\n");
795 return AVERROR(ENOMEM);
798 av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
799 "be allocated due to unknown size.\n");
802 // MPEG2/interlacing info
803 memcpy(&s->progressive_sequence, &s1->progressive_sequence,
804 (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
806 if (!s1->first_field) {
807 s->last_pict_type = s1->pict_type;
808 if (s1->current_picture_ptr)
809 s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f.quality;
811 if (s1->pict_type != AV_PICTURE_TYPE_B) {
812 s->last_non_b_pict_type = s1->pict_type;
820 * Set the given MpegEncContext to common defaults
821 * (same for encoding and decoding).
822 * The changed fields will not depend upon the
823 * prior state of the MpegEncContext.
825 void ff_MPV_common_defaults(MpegEncContext *s)
827 s->y_dc_scale_table =
828 s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
829 s->chroma_qscale_table = ff_default_chroma_qscale_table;
830 s->progressive_frame = 1;
831 s->progressive_sequence = 1;
832 s->picture_structure = PICT_FRAME;
834 s->coded_picture_number = 0;
835 s->picture_number = 0;
836 s->input_picture_number = 0;
838 s->picture_in_gop_number = 0;
843 s->slice_context_count = 1;
847 * Set the given MpegEncContext to defaults for decoding.
848 * the changed fields will not depend upon
849 * the prior state of the MpegEncContext.
851 void ff_MPV_decode_defaults(MpegEncContext *s)
853 ff_MPV_common_defaults(s);
856 static int init_er(MpegEncContext *s)
858 ERContext *er = &s->er;
859 int mb_array_size = s->mb_height * s->mb_stride;
862 er->avctx = s->avctx;
865 er->mb_index2xy = s->mb_index2xy;
866 er->mb_num = s->mb_num;
867 er->mb_width = s->mb_width;
868 er->mb_height = s->mb_height;
869 er->mb_stride = s->mb_stride;
870 er->b8_stride = s->b8_stride;
872 er->er_temp_buffer = av_malloc(s->mb_height * s->mb_stride);
873 er->error_status_table = av_mallocz(mb_array_size);
874 if (!er->er_temp_buffer || !er->error_status_table)
877 er->mbskip_table = s->mbskip_table;
878 er->mbintra_table = s->mbintra_table;
880 for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++)
881 er->dc_val[i] = s->dc_val[i];
883 er->decode_mb = mpeg_er_decode_mb;
888 av_freep(&er->er_temp_buffer);
889 av_freep(&er->error_status_table);
890 return AVERROR(ENOMEM);
894 * Initialize and allocates MpegEncContext fields dependent on the resolution.
896 static int init_context_frame(MpegEncContext *s)
898 int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
900 s->mb_width = (s->width + 15) / 16;
901 s->mb_stride = s->mb_width + 1;
902 s->b8_stride = s->mb_width * 2 + 1;
903 s->b4_stride = s->mb_width * 4 + 1;
904 mb_array_size = s->mb_height * s->mb_stride;
905 mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
907 /* set default edge pos, will be overriden
908 * in decode_header if needed */
909 s->h_edge_pos = s->mb_width * 16;
910 s->v_edge_pos = s->mb_height * 16;
912 s->mb_num = s->mb_width * s->mb_height;
917 s->block_wrap[3] = s->b8_stride;
919 s->block_wrap[5] = s->mb_stride;
921 y_size = s->b8_stride * (2 * s->mb_height + 1);
922 c_size = s->mb_stride * (s->mb_height + 1);
923 yc_size = y_size + 2 * c_size;
925 FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int), fail); // error ressilience code looks cleaner with this
926 for (y = 0; y < s->mb_height; y++)
927 for (x = 0; x < s->mb_width; x++)
928 s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
930 s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
933 /* Allocate MV tables */
934 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
935 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
936 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
937 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
938 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
939 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
940 s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
941 s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
942 s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
943 s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
944 s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + s->mb_stride + 1;
945 s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
947 /* Allocate MB type table */
948 FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size * sizeof(uint16_t), fail) // needed for encoding
950 FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail)
952 FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
953 mb_array_size * sizeof(float), fail);
954 FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
955 mb_array_size * sizeof(float), fail);
959 if (s->codec_id == AV_CODEC_ID_MPEG4 ||
960 (s->flags & CODEC_FLAG_INTERLACED_ME)) {
961 /* interlaced direct mode decoding tables */
962 for (i = 0; i < 2; i++) {
964 for (j = 0; j < 2; j++) {
965 for (k = 0; k < 2; k++) {
966 FF_ALLOCZ_OR_GOTO(s->avctx,
967 s->b_field_mv_table_base[i][j][k],
968 mv_table_size * 2 * sizeof(int16_t),
970 s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
973 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
974 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
975 s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
977 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
980 if (s->out_format == FMT_H263) {
982 FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
983 s->coded_block = s->coded_block_base + s->b8_stride + 1;
985 /* cbp, ac_pred, pred_dir */
986 FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table , mb_array_size * sizeof(uint8_t), fail);
987 FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail);
990 if (s->h263_pred || s->h263_plus || !s->encoding) {
992 // MN: we need these for error resilience of intra-frames
993 FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
994 s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
995 s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
996 s->dc_val[2] = s->dc_val[1] + c_size;
997 for (i = 0; i < yc_size; i++)
998 s->dc_val_base[i] = 1024;
1001 /* which mb is a intra block */
1002 FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
1003 memset(s->mbintra_table, 1, mb_array_size);
1005 /* init macroblock skip table */
1006 FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
1007 // Note the + 1 is for a quicker mpeg4 slice_end detection
1011 return AVERROR(ENOMEM);
1015 * init common structure for both encoder and decoder.
1016 * this assumes that some variables like width/height are already set
1018 av_cold int ff_MPV_common_init(MpegEncContext *s)
1021 int nb_slices = (HAVE_THREADS &&
1022 s->avctx->active_thread_type & FF_THREAD_SLICE) ?
1023 s->avctx->thread_count : 1;
1025 if (s->encoding && s->avctx->slices)
1026 nb_slices = s->avctx->slices;
1028 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
1029 s->mb_height = (s->height + 31) / 32 * 2;
1030 else if (s->codec_id != AV_CODEC_ID_H264)
1031 s->mb_height = (s->height + 15) / 16;
1033 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
1034 av_log(s->avctx, AV_LOG_ERROR,
1035 "decoding to AV_PIX_FMT_NONE is not supported.\n");
1039 if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
1042 max_slices = FFMIN(MAX_THREADS, s->mb_height);
1044 max_slices = MAX_THREADS;
1045 av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
1046 " reducing to %d\n", nb_slices, max_slices);
1047 nb_slices = max_slices;
1050 if ((s->width || s->height) &&
1051 av_image_check_size(s->width, s->height, 0, s->avctx))
1054 ff_dct_common_init(s);
1056 s->flags = s->avctx->flags;
1057 s->flags2 = s->avctx->flags2;
1059 /* set chroma shifts */
1060 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
1062 /* convert fourcc to upper case */
1063 s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
1064 s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag);
1066 s->avctx->coded_frame = &s->current_picture.f;
1069 if (s->msmpeg4_version) {
1070 FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
1071 2 * 2 * (MAX_LEVEL + 1) *
1072 (MAX_RUN + 1) * 2 * sizeof(int), fail);
1074 FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
1076 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix, 64 * 32 * sizeof(int), fail)
1077 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix, 64 * 32 * sizeof(int), fail)
1078 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix, 64 * 32 * sizeof(int), fail)
1079 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail)
1080 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail)
1081 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail)
1082 FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture *), fail)
1083 FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture *), fail)
1085 if (s->avctx->noise_reduction) {
1086 FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset, 2 * 64 * sizeof(uint16_t), fail);
1090 FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
1091 MAX_PICTURE_COUNT * sizeof(Picture), fail);
1092 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1093 avcodec_get_frame_defaults(&s->picture[i].f);
1095 memset(&s->next_picture, 0, sizeof(s->next_picture));
1096 memset(&s->last_picture, 0, sizeof(s->last_picture));
1097 memset(&s->current_picture, 0, sizeof(s->current_picture));
1098 avcodec_get_frame_defaults(&s->next_picture.f);
1099 avcodec_get_frame_defaults(&s->last_picture.f);
1100 avcodec_get_frame_defaults(&s->current_picture.f);
1102 if (init_context_frame(s))
1105 s->parse_context.state = -1;
1107 s->context_initialized = 1;
1108 s->thread_context[0] = s;
1110 // if (s->width && s->height) {
1111 if (nb_slices > 1) {
1112 for (i = 1; i < nb_slices; i++) {
1113 s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
1114 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
1117 for (i = 0; i < nb_slices; i++) {
1118 if (init_duplicate_context(s->thread_context[i]) < 0)
1120 s->thread_context[i]->start_mb_y =
1121 (s->mb_height * (i) + nb_slices / 2) / nb_slices;
1122 s->thread_context[i]->end_mb_y =
1123 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
1126 if (init_duplicate_context(s) < 0)
1129 s->end_mb_y = s->mb_height;
1131 s->slice_context_count = nb_slices;
1136 ff_MPV_common_end(s);
1141 * Frees and resets MpegEncContext fields depending on the resolution.
1142 * Is used during resolution changes to avoid a full reinitialization of the
1145 static int free_context_frame(MpegEncContext *s)
1149 av_freep(&s->mb_type);
1150 av_freep(&s->p_mv_table_base);
1151 av_freep(&s->b_forw_mv_table_base);
1152 av_freep(&s->b_back_mv_table_base);
1153 av_freep(&s->b_bidir_forw_mv_table_base);
1154 av_freep(&s->b_bidir_back_mv_table_base);
1155 av_freep(&s->b_direct_mv_table_base);
1156 s->p_mv_table = NULL;
1157 s->b_forw_mv_table = NULL;
1158 s->b_back_mv_table = NULL;
1159 s->b_bidir_forw_mv_table = NULL;
1160 s->b_bidir_back_mv_table = NULL;
1161 s->b_direct_mv_table = NULL;
1162 for (i = 0; i < 2; i++) {
1163 for (j = 0; j < 2; j++) {
1164 for (k = 0; k < 2; k++) {
1165 av_freep(&s->b_field_mv_table_base[i][j][k]);
1166 s->b_field_mv_table[i][j][k] = NULL;
1168 av_freep(&s->b_field_select_table[i][j]);
1169 av_freep(&s->p_field_mv_table_base[i][j]);
1170 s->p_field_mv_table[i][j] = NULL;
1172 av_freep(&s->p_field_select_table[i]);
1175 av_freep(&s->dc_val_base);
1176 av_freep(&s->coded_block_base);
1177 av_freep(&s->mbintra_table);
1178 av_freep(&s->cbp_table);
1179 av_freep(&s->pred_dir_table);
1181 av_freep(&s->mbskip_table);
1183 av_freep(&s->er.error_status_table);
1184 av_freep(&s->er.er_temp_buffer);
1185 av_freep(&s->mb_index2xy);
1186 av_freep(&s->lambda_table);
1188 av_freep(&s->cplx_tab);
1189 av_freep(&s->bits_tab);
1191 s->linesize = s->uvlinesize = 0;
1196 int ff_MPV_common_frame_size_change(MpegEncContext *s)
1200 if (s->slice_context_count > 1) {
1201 for (i = 0; i < s->slice_context_count; i++) {
1202 free_duplicate_context(s->thread_context[i]);
1204 for (i = 1; i < s->slice_context_count; i++) {
1205 av_freep(&s->thread_context[i]);
1208 free_duplicate_context(s);
1210 if ((err = free_context_frame(s)) < 0)
1214 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1215 s->picture[i].needs_realloc = 1;
1218 s->last_picture_ptr =
1219 s->next_picture_ptr =
1220 s->current_picture_ptr = NULL;
1223 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
1224 s->mb_height = (s->height + 31) / 32 * 2;
1225 else if (s->codec_id != AV_CODEC_ID_H264)
1226 s->mb_height = (s->height + 15) / 16;
1228 if ((s->width || s->height) &&
1229 av_image_check_size(s->width, s->height, 0, s->avctx))
1230 return AVERROR_INVALIDDATA;
1232 if ((err = init_context_frame(s)))
1235 s->thread_context[0] = s;
1237 if (s->width && s->height) {
1238 int nb_slices = s->slice_context_count;
1239 if (nb_slices > 1) {
1240 for (i = 1; i < nb_slices; i++) {
1241 s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
1242 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
1245 for (i = 0; i < nb_slices; i++) {
1246 if (init_duplicate_context(s->thread_context[i]) < 0)
1248 s->thread_context[i]->start_mb_y =
1249 (s->mb_height * (i) + nb_slices / 2) / nb_slices;
1250 s->thread_context[i]->end_mb_y =
1251 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
1254 if (init_duplicate_context(s) < 0)
1257 s->end_mb_y = s->mb_height;
1259 s->slice_context_count = nb_slices;
1264 ff_MPV_common_end(s);
1268 /* init common structure for both encoder and decoder */
1269 void ff_MPV_common_end(MpegEncContext *s)
1273 if (s->slice_context_count > 1) {
1274 for (i = 0; i < s->slice_context_count; i++) {
1275 free_duplicate_context(s->thread_context[i]);
1277 for (i = 1; i < s->slice_context_count; i++) {
1278 av_freep(&s->thread_context[i]);
1280 s->slice_context_count = 1;
1281 } else free_duplicate_context(s);
1283 av_freep(&s->parse_context.buffer);
1284 s->parse_context.buffer_size = 0;
1286 av_freep(&s->bitstream_buffer);
1287 s->allocated_bitstream_buffer_size = 0;
1289 av_freep(&s->avctx->stats_out);
1290 av_freep(&s->ac_stats);
1292 if(s->q_chroma_intra_matrix != s->q_intra_matrix ) av_freep(&s->q_chroma_intra_matrix);
1293 if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
1294 s->q_chroma_intra_matrix= NULL;
1295 s->q_chroma_intra_matrix16= NULL;
1296 av_freep(&s->q_intra_matrix);
1297 av_freep(&s->q_inter_matrix);
1298 av_freep(&s->q_intra_matrix16);
1299 av_freep(&s->q_inter_matrix16);
1300 av_freep(&s->input_picture);
1301 av_freep(&s->reordered_input_picture);
1302 av_freep(&s->dct_offset);
1305 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1306 free_picture_tables(&s->picture[i]);
1307 ff_mpeg_unref_picture(s, &s->picture[i]);
1310 av_freep(&s->picture);
1311 free_picture_tables(&s->last_picture);
1312 ff_mpeg_unref_picture(s, &s->last_picture);
1313 free_picture_tables(&s->current_picture);
1314 ff_mpeg_unref_picture(s, &s->current_picture);
1315 free_picture_tables(&s->next_picture);
1316 ff_mpeg_unref_picture(s, &s->next_picture);
1317 free_picture_tables(&s->new_picture);
1318 ff_mpeg_unref_picture(s, &s->new_picture);
1320 free_context_frame(s);
1322 s->context_initialized = 0;
1323 s->last_picture_ptr =
1324 s->next_picture_ptr =
1325 s->current_picture_ptr = NULL;
1326 s->linesize = s->uvlinesize = 0;
1329 void ff_init_rl(RLTable *rl,
1330 uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
1332 int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
1333 uint8_t index_run[MAX_RUN + 1];
1334 int last, run, level, start, end, i;
1336 /* If table is static, we can quit if rl->max_level[0] is not NULL */
1337 if (static_store && rl->max_level[0])
1340 /* compute max_level[], max_run[] and index_run[] */
1341 for (last = 0; last < 2; last++) {
1350 memset(max_level, 0, MAX_RUN + 1);
1351 memset(max_run, 0, MAX_LEVEL + 1);
1352 memset(index_run, rl->n, MAX_RUN + 1);
1353 for (i = start; i < end; i++) {
1354 run = rl->table_run[i];
1355 level = rl->table_level[i];
1356 if (index_run[run] == rl->n)
1358 if (level > max_level[run])
1359 max_level[run] = level;
1360 if (run > max_run[level])
1361 max_run[level] = run;
1364 rl->max_level[last] = static_store[last];
1366 rl->max_level[last] = av_malloc(MAX_RUN + 1);
1367 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
1369 rl->max_run[last] = static_store[last] + MAX_RUN + 1;
1371 rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
1372 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
1374 rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
1376 rl->index_run[last] = av_malloc(MAX_RUN + 1);
1377 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
1381 void ff_init_vlc_rl(RLTable *rl)
1385 for (q = 0; q < 32; q++) {
1387 int qadd = (q - 1) | 1;
1393 for (i = 0; i < rl->vlc.table_size; i++) {
1394 int code = rl->vlc.table[i][0];
1395 int len = rl->vlc.table[i][1];
1398 if (len == 0) { // illegal code
1401 } else if (len < 0) { // more bits needed
1405 if (code == rl->n) { // esc
1409 run = rl->table_run[code] + 1;
1410 level = rl->table_level[code] * qmul + qadd;
1411 if (code >= rl->last) run += 192;
1414 rl->rl_vlc[q][i].len = len;
1415 rl->rl_vlc[q][i].level = level;
1416 rl->rl_vlc[q][i].run = run;
1421 void ff_release_unused_pictures(MpegEncContext*s, int remove_current)
1425 /* release non reference frames */
1426 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1427 if (!s->picture[i].reference &&
1428 (remove_current || &s->picture[i] != s->current_picture_ptr)) {
1429 ff_mpeg_unref_picture(s, &s->picture[i]);
1434 static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
1436 if ( (s->avctx->active_thread_type & FF_THREAD_FRAME)
1437 && pic->f.qscale_table //check if the frame has anything allocated
1438 && pic->period_since_free < s->avctx->thread_count)
1440 if (pic == s->last_picture_ptr)
1442 if (pic->f.data[0] == NULL)
1444 if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
1449 static int find_unused_picture(MpegEncContext *s, int shared)
1454 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1455 if (s->picture[i].f.data[0] == NULL && &s->picture[i] != s->last_picture_ptr)
1459 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1460 if (pic_is_unused(s, &s->picture[i]))
1465 av_log(s->avctx, AV_LOG_FATAL,
1466 "Internal error, picture buffer overflow\n");
1467 /* We could return -1, but the codec would crash trying to draw into a
1468 * non-existing frame anyway. This is safer than waiting for a random crash.
1469 * Also the return of this is never useful, an encoder must only allocate
1470 * as much as allowed in the specification. This has no relationship to how
1471 * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
1472 * enough for such valid streams).
1473 * Plus, a decoder has to check stream validity and remove frames if too
1474 * many reference frames are around. Waiting for "OOM" is not correct at
1475 * all. Similarly, missing reference frames have to be replaced by
1476 * interpolated/MC frames, anything else is a bug in the codec ...
1482 int ff_find_unused_picture(MpegEncContext *s, int shared)
1484 int ret = find_unused_picture(s, shared);
1486 if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
1487 if (s->picture[ret].needs_realloc) {
1488 s->picture[ret].needs_realloc = 0;
1489 free_picture_tables(&s->picture[ret]);
1490 ff_mpeg_unref_picture(s, &s->picture[ret]);
1491 avcodec_get_frame_defaults(&s->picture[ret].f);
1497 static void update_noise_reduction(MpegEncContext *s)
1501 for (intra = 0; intra < 2; intra++) {
1502 if (s->dct_count[intra] > (1 << 16)) {
1503 for (i = 0; i < 64; i++) {
1504 s->dct_error_sum[intra][i] >>= 1;
1506 s->dct_count[intra] >>= 1;
1509 for (i = 0; i < 64; i++) {
1510 s->dct_offset[intra][i] = (s->avctx->noise_reduction *
1511 s->dct_count[intra] +
1512 s->dct_error_sum[intra][i] / 2) /
1513 (s->dct_error_sum[intra][i] + 1);
1519 * generic function for encode/decode called after coding/decoding
1520 * the header and before a frame is coded/decoded.
1522 int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
1528 if (!ff_thread_can_start_frame(avctx)) {
1529 av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
1533 /* mark & release old frames */
1534 if (s->out_format != FMT_H264 || s->codec_id == AV_CODEC_ID_SVQ3) {
1535 if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1536 s->last_picture_ptr != s->next_picture_ptr &&
1537 s->last_picture_ptr->f.data[0]) {
1538 ff_mpeg_unref_picture(s, s->last_picture_ptr);
1541 /* release forgotten pictures */
1542 /* if (mpeg124/h263) */
1544 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1545 if (&s->picture[i] != s->last_picture_ptr &&
1546 &s->picture[i] != s->next_picture_ptr &&
1547 s->picture[i].reference && !s->picture[i].needs_realloc) {
1548 if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1549 av_log(avctx, AV_LOG_ERROR,
1550 "releasing zombie picture\n");
1551 ff_mpeg_unref_picture(s, &s->picture[i]);
1558 ff_release_unused_pictures(s, 1);
1560 if (s->current_picture_ptr &&
1561 s->current_picture_ptr->f.data[0] == NULL) {
1562 // we already have a unused image
1563 // (maybe it was set before reading the header)
1564 pic = s->current_picture_ptr;
1566 i = ff_find_unused_picture(s, 0);
1568 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1571 pic = &s->picture[i];
1575 if (!s->droppable) {
1576 if (s->codec_id == AV_CODEC_ID_H264)
1577 pic->reference = s->picture_structure;
1578 else if (s->pict_type != AV_PICTURE_TYPE_B)
1582 pic->f.coded_picture_number = s->coded_picture_number++;
1584 if (ff_alloc_picture(s, pic, 0) < 0)
1587 s->current_picture_ptr = pic;
1588 // FIXME use only the vars from current_pic
1589 s->current_picture_ptr->f.top_field_first = s->top_field_first;
1590 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
1591 s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1592 if (s->picture_structure != PICT_FRAME)
1593 s->current_picture_ptr->f.top_field_first =
1594 (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
1596 s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame &&
1597 !s->progressive_sequence;
1598 s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
1601 s->current_picture_ptr->f.pict_type = s->pict_type;
1602 // if (s->flags && CODEC_FLAG_QSCALE)
1603 // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
1604 s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1606 ff_mpeg_unref_picture(s, &s->current_picture);
1607 if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
1608 s->current_picture_ptr)) < 0)
1611 if (s->codec_id != AV_CODEC_ID_H264 && s->pict_type != AV_PICTURE_TYPE_B) {
1612 s->last_picture_ptr = s->next_picture_ptr;
1614 s->next_picture_ptr = s->current_picture_ptr;
1616 av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
1617 s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
1618 s->last_picture_ptr ? s->last_picture_ptr->f.data[0] : NULL,
1619 s->next_picture_ptr ? s->next_picture_ptr->f.data[0] : NULL,
1620 s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
1621 s->pict_type, s->droppable);
1623 if (s->codec_id != AV_CODEC_ID_H264) {
1624 if ((s->last_picture_ptr == NULL ||
1625 s->last_picture_ptr->f.data[0] == NULL) &&
1626 (s->pict_type != AV_PICTURE_TYPE_I ||
1627 s->picture_structure != PICT_FRAME)) {
1628 int h_chroma_shift, v_chroma_shift;
1629 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
1630 &h_chroma_shift, &v_chroma_shift);
1631 if (s->pict_type != AV_PICTURE_TYPE_I)
1632 av_log(avctx, AV_LOG_ERROR,
1633 "warning: first frame is no keyframe\n");
1634 else if (s->picture_structure != PICT_FRAME)
1635 av_log(avctx, AV_LOG_INFO,
1636 "allocate dummy last picture for field based first keyframe\n");
1638 /* Allocate a dummy frame */
1639 i = ff_find_unused_picture(s, 0);
1641 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1644 s->last_picture_ptr = &s->picture[i];
1645 s->last_picture_ptr->f.key_frame = 0;
1646 if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
1647 s->last_picture_ptr = NULL;
1651 memset(s->last_picture_ptr->f.data[0], 0x80,
1652 avctx->height * s->last_picture_ptr->f.linesize[0]);
1653 memset(s->last_picture_ptr->f.data[1], 0x80,
1654 (avctx->height >> v_chroma_shift) *
1655 s->last_picture_ptr->f.linesize[1]);
1656 memset(s->last_picture_ptr->f.data[2], 0x80,
1657 (avctx->height >> v_chroma_shift) *
1658 s->last_picture_ptr->f.linesize[2]);
1660 if(s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263){
1661 for(i=0; i<avctx->height; i++)
1662 memset(s->last_picture_ptr->f.data[0] + s->last_picture_ptr->f.linesize[0]*i, 16, avctx->width);
1664 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
1665 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
1667 if ((s->next_picture_ptr == NULL ||
1668 s->next_picture_ptr->f.data[0] == NULL) &&
1669 s->pict_type == AV_PICTURE_TYPE_B) {
1670 /* Allocate a dummy frame */
1671 i = ff_find_unused_picture(s, 0);
1673 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1676 s->next_picture_ptr = &s->picture[i];
1677 s->next_picture_ptr->f.key_frame = 0;
1678 if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
1679 s->next_picture_ptr = NULL;
1682 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
1683 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
1687 #if 0 // BUFREF-FIXME
1688 memset(s->last_picture.f.data, 0, sizeof(s->last_picture.f.data));
1689 memset(s->next_picture.f.data, 0, sizeof(s->next_picture.f.data));
1691 if (s->codec_id != AV_CODEC_ID_H264) {
1692 if (s->last_picture_ptr) {
1693 ff_mpeg_unref_picture(s, &s->last_picture);
1694 if (s->last_picture_ptr->f.data[0] &&
1695 (ret = ff_mpeg_ref_picture(s, &s->last_picture,
1696 s->last_picture_ptr)) < 0)
1699 if (s->next_picture_ptr) {
1700 ff_mpeg_unref_picture(s, &s->next_picture);
1701 if (s->next_picture_ptr->f.data[0] &&
1702 (ret = ff_mpeg_ref_picture(s, &s->next_picture,
1703 s->next_picture_ptr)) < 0)
1707 assert(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
1708 s->last_picture_ptr->f.data[0]));
1711 if (s->picture_structure!= PICT_FRAME && s->out_format != FMT_H264) {
1713 for (i = 0; i < 4; i++) {
1714 if (s->picture_structure == PICT_BOTTOM_FIELD) {
1715 s->current_picture.f.data[i] +=
1716 s->current_picture.f.linesize[i];
1718 s->current_picture.f.linesize[i] *= 2;
1719 s->last_picture.f.linesize[i] *= 2;
1720 s->next_picture.f.linesize[i] *= 2;
1724 s->err_recognition = avctx->err_recognition;
1726 /* set dequantizer, we can't do it during init as
1727 * it might change for mpeg4 and we can't do it in the header
1728 * decode as init is not called for mpeg4 there yet */
1729 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1730 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
1731 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
1732 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
1733 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
1734 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
1736 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
1737 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
1740 if (s->dct_error_sum) {
1741 assert(s->avctx->noise_reduction && s->encoding);
1742 update_noise_reduction(s);
1745 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1746 return ff_xvmc_field_start(s, avctx);
1751 /* generic function for encode/decode called after a
1752 * frame has been coded/decoded. */
1753 void ff_MPV_frame_end(MpegEncContext *s)
1756 /* redraw edges for the frame if decoding didn't complete */
1757 // just to make sure that all data is rendered.
1758 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {
1759 ff_xvmc_field_end(s);
1760 } else if ((s->er.error_count || s->encoding || !(s->avctx->codec->capabilities&CODEC_CAP_DRAW_HORIZ_BAND)) &&
1761 !s->avctx->hwaccel &&
1762 !(s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) &&
1763 s->unrestricted_mv &&
1764 s->current_picture.reference &&
1766 !(s->flags & CODEC_FLAG_EMU_EDGE) &&
1769 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
1770 int hshift = desc->log2_chroma_w;
1771 int vshift = desc->log2_chroma_h;
1772 s->dsp.draw_edges(s->current_picture.f.data[0], s->current_picture.f.linesize[0],
1773 s->h_edge_pos, s->v_edge_pos,
1774 EDGE_WIDTH, EDGE_WIDTH,
1775 EDGE_TOP | EDGE_BOTTOM);
1776 s->dsp.draw_edges(s->current_picture.f.data[1], s->current_picture.f.linesize[1],
1777 s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
1778 EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
1779 EDGE_TOP | EDGE_BOTTOM);
1780 s->dsp.draw_edges(s->current_picture.f.data[2], s->current_picture.f.linesize[2],
1781 s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
1782 EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
1783 EDGE_TOP | EDGE_BOTTOM);
1788 s->last_pict_type = s->pict_type;
1789 s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f.quality;
1790 if (s->pict_type!= AV_PICTURE_TYPE_B) {
1791 s->last_non_b_pict_type = s->pict_type;
1794 /* copy back current_picture variables */
1795 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1796 if (s->picture[i].f.data[0] == s->current_picture.f.data[0]) {
1797 s->picture[i] = s->current_picture;
1801 assert(i < MAX_PICTURE_COUNT);
1805 /* release non-reference frames */
1806 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1807 if (!s->picture[i].reference)
1808 ff_mpeg_unref_picture(s, &s->picture[i]);
1811 // clear copies, to avoid confusion
1813 memset(&s->last_picture, 0, sizeof(Picture));
1814 memset(&s->next_picture, 0, sizeof(Picture));
1815 memset(&s->current_picture, 0, sizeof(Picture));
1817 s->avctx->coded_frame = &s->current_picture_ptr->f;
1819 if (s->codec_id != AV_CODEC_ID_H264 && s->current_picture.reference)
1820 ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
1824 * Draw a line from (ex, ey) -> (sx, sy).
1825 * @param w width of the image
1826 * @param h height of the image
1827 * @param stride stride/linesize of the image
1828 * @param color color of the arrow
1830 static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
1831 int w, int h, int stride, int color)
1835 sx = av_clip(sx, 0, w - 1);
1836 sy = av_clip(sy, 0, h - 1);
1837 ex = av_clip(ex, 0, w - 1);
1838 ey = av_clip(ey, 0, h - 1);
1840 buf[sy * stride + sx] += color;
1842 if (FFABS(ex - sx) > FFABS(ey - sy)) {
1844 FFSWAP(int, sx, ex);
1845 FFSWAP(int, sy, ey);
1847 buf += sx + sy * stride;
1849 f = ((ey - sy) << 16) / ex;
1850 for (x = 0; x <= ex; x++) {
1852 fr = (x * f) & 0xFFFF;
1853 buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
1854 if(fr) buf[(y + 1) * stride + x] += (color * fr ) >> 16;
1858 FFSWAP(int, sx, ex);
1859 FFSWAP(int, sy, ey);
1861 buf += sx + sy * stride;
1864 f = ((ex - sx) << 16) / ey;
1867 for(y= 0; y <= ey; y++){
1869 fr = (y*f) & 0xFFFF;
1870 buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
1871 if(fr) buf[y * stride + x + 1] += (color * fr ) >> 16;
1877 * Draw an arrow from (ex, ey) -> (sx, sy).
1878 * @param w width of the image
1879 * @param h height of the image
1880 * @param stride stride/linesize of the image
1881 * @param color color of the arrow
1883 static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
1884 int ey, int w, int h, int stride, int color)
1888 sx = av_clip(sx, -100, w + 100);
1889 sy = av_clip(sy, -100, h + 100);
1890 ex = av_clip(ex, -100, w + 100);
1891 ey = av_clip(ey, -100, h + 100);
1896 if (dx * dx + dy * dy > 3 * 3) {
1899 int length = ff_sqrt((rx * rx + ry * ry) << 8);
1901 // FIXME subpixel accuracy
1902 rx = ROUNDED_DIV(rx * 3 << 4, length);
1903 ry = ROUNDED_DIV(ry * 3 << 4, length);
1905 draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
1906 draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
1908 draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
1912 * Print debugging info for the given picture.
1914 void ff_print_debug_info2(AVCodecContext *avctx, Picture *p, AVFrame *pict, uint8_t *mbskip_table,
1916 int mb_width, int mb_height, int mb_stride, int quarter_sample)
1918 if (avctx->hwaccel || !p || !p->mb_type
1919 || (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU))
1923 if (avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
1926 av_log(avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
1927 av_get_picture_type_char(pict->pict_type));
1928 for (y = 0; y < mb_height; y++) {
1929 for (x = 0; x < mb_width; x++) {
1930 if (avctx->debug & FF_DEBUG_SKIP) {
1931 int count = mbskip_table[x + y * mb_stride];
1934 av_log(avctx, AV_LOG_DEBUG, "%1d", count);
1936 if (avctx->debug & FF_DEBUG_QP) {
1937 av_log(avctx, AV_LOG_DEBUG, "%2d",
1938 p->qscale_table[x + y * mb_stride]);
1940 if (avctx->debug & FF_DEBUG_MB_TYPE) {
1941 int mb_type = p->mb_type[x + y * mb_stride];
1942 // Type & MV direction
1943 if (IS_PCM(mb_type))
1944 av_log(avctx, AV_LOG_DEBUG, "P");
1945 else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
1946 av_log(avctx, AV_LOG_DEBUG, "A");
1947 else if (IS_INTRA4x4(mb_type))
1948 av_log(avctx, AV_LOG_DEBUG, "i");
1949 else if (IS_INTRA16x16(mb_type))
1950 av_log(avctx, AV_LOG_DEBUG, "I");
1951 else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
1952 av_log(avctx, AV_LOG_DEBUG, "d");
1953 else if (IS_DIRECT(mb_type))
1954 av_log(avctx, AV_LOG_DEBUG, "D");
1955 else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
1956 av_log(avctx, AV_LOG_DEBUG, "g");
1957 else if (IS_GMC(mb_type))
1958 av_log(avctx, AV_LOG_DEBUG, "G");
1959 else if (IS_SKIP(mb_type))
1960 av_log(avctx, AV_LOG_DEBUG, "S");
1961 else if (!USES_LIST(mb_type, 1))
1962 av_log(avctx, AV_LOG_DEBUG, ">");
1963 else if (!USES_LIST(mb_type, 0))
1964 av_log(avctx, AV_LOG_DEBUG, "<");
1966 av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
1967 av_log(avctx, AV_LOG_DEBUG, "X");
1971 if (IS_8X8(mb_type))
1972 av_log(avctx, AV_LOG_DEBUG, "+");
1973 else if (IS_16X8(mb_type))
1974 av_log(avctx, AV_LOG_DEBUG, "-");
1975 else if (IS_8X16(mb_type))
1976 av_log(avctx, AV_LOG_DEBUG, "|");
1977 else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
1978 av_log(avctx, AV_LOG_DEBUG, " ");
1980 av_log(avctx, AV_LOG_DEBUG, "?");
1983 if (IS_INTERLACED(mb_type))
1984 av_log(avctx, AV_LOG_DEBUG, "=");
1986 av_log(avctx, AV_LOG_DEBUG, " ");
1989 av_log(avctx, AV_LOG_DEBUG, "\n");
1993 if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
1994 (avctx->debug_mv)) {
1995 const int shift = 1 + quarter_sample;
1999 int h_chroma_shift, v_chroma_shift, block_height;
2000 const int width = avctx->width;
2001 const int height = avctx->height;
2002 const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
2003 const int mv_stride = (mb_width << mv_sample_log2) +
2004 (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
2006 *low_delay = 0; // needed to see the vectors without trashing the buffers
2008 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
2010 av_frame_make_writable(pict);
2012 pict->opaque = NULL;
2013 ptr = pict->data[0];
2014 block_height = 16 >> v_chroma_shift;
2016 for (mb_y = 0; mb_y < mb_height; mb_y++) {
2018 for (mb_x = 0; mb_x < mb_width; mb_x++) {
2019 const int mb_index = mb_x + mb_y * mb_stride;
2020 if ((avctx->debug_mv) && p->motion_val[0]) {
2022 for (type = 0; type < 3; type++) {
2026 if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_P_FOR)) ||
2027 (pict->pict_type!= AV_PICTURE_TYPE_P))
2032 if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_FOR)) ||
2033 (pict->pict_type!= AV_PICTURE_TYPE_B))
2038 if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_BACK)) ||
2039 (pict->pict_type!= AV_PICTURE_TYPE_B))
2044 if (!USES_LIST(p->mb_type[mb_index], direction))
2047 if (IS_8X8(p->mb_type[mb_index])) {
2049 for (i = 0; i < 4; i++) {
2050 int sx = mb_x * 16 + 4 + 8 * (i & 1);
2051 int sy = mb_y * 16 + 4 + 8 * (i >> 1);
2052 int xy = (mb_x * 2 + (i & 1) +
2053 (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
2054 int mx = (p->motion_val[direction][xy][0] >> shift) + sx;
2055 int my = (p->motion_val[direction][xy][1] >> shift) + sy;
2056 draw_arrow(ptr, sx, sy, mx, my, width,
2057 height, pict->linesize[0], 100);
2059 } else if (IS_16X8(p->mb_type[mb_index])) {
2061 for (i = 0; i < 2; i++) {
2062 int sx = mb_x * 16 + 8;
2063 int sy = mb_y * 16 + 4 + 8 * i;
2064 int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
2065 int mx = (p->motion_val[direction][xy][0] >> shift);
2066 int my = (p->motion_val[direction][xy][1] >> shift);
2068 if (IS_INTERLACED(p->mb_type[mb_index]))
2071 draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
2072 height, pict->linesize[0], 100);
2074 } else if (IS_8X16(p->mb_type[mb_index])) {
2076 for (i = 0; i < 2; i++) {
2077 int sx = mb_x * 16 + 4 + 8 * i;
2078 int sy = mb_y * 16 + 8;
2079 int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
2080 int mx = p->motion_val[direction][xy][0] >> shift;
2081 int my = p->motion_val[direction][xy][1] >> shift;
2083 if (IS_INTERLACED(p->mb_type[mb_index]))
2086 draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
2087 height, pict->linesize[0], 100);
2090 int sx= mb_x * 16 + 8;
2091 int sy= mb_y * 16 + 8;
2092 int xy= (mb_x + mb_y * mv_stride) << mv_sample_log2;
2093 int mx= (p->motion_val[direction][xy][0]>>shift) + sx;
2094 int my= (p->motion_val[direction][xy][1]>>shift) + sy;
2095 draw_arrow(ptr, sx, sy, mx, my, width, height, pict->linesize[0], 100);
2099 if ((avctx->debug & FF_DEBUG_VIS_QP)) {
2100 uint64_t c = (p->qscale_table[mb_index] * 128 / 31) *
2101 0x0101010101010101ULL;
2103 for (y = 0; y < block_height; y++) {
2104 *(uint64_t *)(pict->data[1] + 8 * mb_x +
2105 (block_height * mb_y + y) *
2106 pict->linesize[1]) = c;
2107 *(uint64_t *)(pict->data[2] + 8 * mb_x +
2108 (block_height * mb_y + y) *
2109 pict->linesize[2]) = c;
2112 if ((avctx->debug & FF_DEBUG_VIS_MB_TYPE) &&
2114 int mb_type = p->mb_type[mb_index];
2117 #define COLOR(theta, r) \
2118 u = (int)(128 + r * cos(theta * 3.141592 / 180)); \
2119 v = (int)(128 + r * sin(theta * 3.141592 / 180));
2123 if (IS_PCM(mb_type)) {
2125 } else if ((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) ||
2126 IS_INTRA16x16(mb_type)) {
2128 } else if (IS_INTRA4x4(mb_type)) {
2130 } else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) {
2132 } else if (IS_DIRECT(mb_type)) {
2134 } else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) {
2136 } else if (IS_GMC(mb_type)) {
2138 } else if (IS_SKIP(mb_type)) {
2140 } else if (!USES_LIST(mb_type, 1)) {
2142 } else if (!USES_LIST(mb_type, 0)) {
2145 av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
2149 u *= 0x0101010101010101ULL;
2150 v *= 0x0101010101010101ULL;
2151 for (y = 0; y < block_height; y++) {
2152 *(uint64_t *)(pict->data[1] + 8 * mb_x +
2153 (block_height * mb_y + y) * pict->linesize[1]) = u;
2154 *(uint64_t *)(pict->data[2] + 8 * mb_x +
2155 (block_height * mb_y + y) * pict->linesize[2]) = v;
2159 if (IS_8X8(mb_type) || IS_16X8(mb_type)) {
2160 *(uint64_t *)(pict->data[0] + 16 * mb_x + 0 +
2161 (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
2162 *(uint64_t *)(pict->data[0] + 16 * mb_x + 8 +
2163 (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
2165 if (IS_8X8(mb_type) || IS_8X16(mb_type)) {
2166 for (y = 0; y < 16; y++)
2167 pict->data[0][16 * mb_x + 8 + (16 * mb_y + y) *
2168 pict->linesize[0]] ^= 0x80;
2170 if (IS_8X8(mb_type) && mv_sample_log2 >= 2) {
2171 int dm = 1 << (mv_sample_log2 - 2);
2172 for (i = 0; i < 4; i++) {
2173 int sx = mb_x * 16 + 8 * (i & 1);
2174 int sy = mb_y * 16 + 8 * (i >> 1);
2175 int xy = (mb_x * 2 + (i & 1) +
2176 (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
2178 int32_t *mv = (int32_t *) &p->motion_val[0][xy];
2179 if (mv[0] != mv[dm] ||
2180 mv[dm * mv_stride] != mv[dm * (mv_stride + 1)])
2181 for (y = 0; y < 8; y++)
2182 pict->data[0][sx + 4 + (sy + y) * pict->linesize[0]] ^= 0x80;
2183 if (mv[0] != mv[dm * mv_stride] || mv[dm] != mv[dm * (mv_stride + 1)])
2184 *(uint64_t *)(pict->data[0] + sx + (sy + 4) *
2185 pict->linesize[0]) ^= 0x8080808080808080ULL;
2189 if (IS_INTERLACED(mb_type) &&
2190 avctx->codec->id == AV_CODEC_ID_H264) {
2194 mbskip_table[mb_index] = 0;
2200 void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
2202 ff_print_debug_info2(s->avctx, p, pict, s->mbskip_table, &s->low_delay,
2203 s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
2206 static inline int hpel_motion_lowres(MpegEncContext *s,
2207 uint8_t *dest, uint8_t *src,
2208 int field_based, int field_select,
2209 int src_x, int src_y,
2210 int width, int height, int stride,
2211 int h_edge_pos, int v_edge_pos,
2212 int w, int h, h264_chroma_mc_func *pix_op,
2213 int motion_x, int motion_y)
2215 const int lowres = s->avctx->lowres;
2216 const int op_index = FFMIN(lowres, 2);
2217 const int s_mask = (2 << lowres) - 1;
2221 if (s->quarter_sample) {
2226 sx = motion_x & s_mask;
2227 sy = motion_y & s_mask;
2228 src_x += motion_x >> lowres + 1;
2229 src_y += motion_y >> lowres + 1;
2231 src += src_y * stride + src_x;
2233 if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
2234 (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
2235 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w + 1,
2236 (h + 1) << field_based, src_x,
2237 src_y << field_based,
2240 src = s->edge_emu_buffer;
2244 sx = (sx << 2) >> lowres;
2245 sy = (sy << 2) >> lowres;
2248 pix_op[op_index](dest, src, stride, h, sx, sy);
2252 /* apply one mpeg motion vector to the three components */
2253 static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
2260 uint8_t **ref_picture,
2261 h264_chroma_mc_func *pix_op,
2262 int motion_x, int motion_y,
2265 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2266 int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy,
2268 const int lowres = s->avctx->lowres;
2269 const int op_index = FFMIN(lowres-1+s->chroma_x_shift, 2);
2270 const int block_s = 8>>lowres;
2271 const int s_mask = (2 << lowres) - 1;
2272 const int h_edge_pos = s->h_edge_pos >> lowres;
2273 const int v_edge_pos = s->v_edge_pos >> lowres;
2274 linesize = s->current_picture.f.linesize[0] << field_based;
2275 uvlinesize = s->current_picture.f.linesize[1] << field_based;
2277 // FIXME obviously not perfect but qpel will not work in lowres anyway
2278 if (s->quarter_sample) {
2284 motion_y += (bottom_field - field_select)*((1 << lowres)-1);
2287 sx = motion_x & s_mask;
2288 sy = motion_y & s_mask;
2289 src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
2290 src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
2292 if (s->out_format == FMT_H263) {
2293 uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
2294 uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
2295 uvsrc_x = src_x >> 1;
2296 uvsrc_y = src_y >> 1;
2297 } else if (s->out_format == FMT_H261) {
2298 // even chroma mv's are full pel in H261
2301 uvsx = (2 * mx) & s_mask;
2302 uvsy = (2 * my) & s_mask;
2303 uvsrc_x = s->mb_x * block_s + (mx >> lowres);
2304 uvsrc_y = mb_y * block_s + (my >> lowres);
2306 if(s->chroma_y_shift){
2311 uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
2312 uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
2314 if(s->chroma_x_shift){
2318 uvsy = motion_y & s_mask;
2320 uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
2323 uvsx = motion_x & s_mask;
2324 uvsy = motion_y & s_mask;
2331 ptr_y = ref_picture[0] + src_y * linesize + src_x;
2332 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
2333 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
2335 if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) ||
2336 (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
2337 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
2338 linesize >> field_based, 17, 17 + field_based,
2339 src_x, src_y << field_based, h_edge_pos,
2341 ptr_y = s->edge_emu_buffer;
2342 if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
2343 uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;
2344 s->vdsp.emulated_edge_mc(uvbuf , ptr_cb, uvlinesize >> field_based, 9,
2346 uvsrc_x, uvsrc_y << field_based,
2347 h_edge_pos >> 1, v_edge_pos >> 1);
2348 s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr, uvlinesize >> field_based, 9,
2350 uvsrc_x, uvsrc_y << field_based,
2351 h_edge_pos >> 1, v_edge_pos >> 1);
2353 ptr_cr = uvbuf + 16;
2357 // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f.data
2359 dest_y += s->linesize;
2360 dest_cb += s->uvlinesize;
2361 dest_cr += s->uvlinesize;
2365 ptr_y += s->linesize;
2366 ptr_cb += s->uvlinesize;
2367 ptr_cr += s->uvlinesize;
2370 sx = (sx << 2) >> lowres;
2371 sy = (sy << 2) >> lowres;
2372 pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
2374 if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
2375 uvsx = (uvsx << 2) >> lowres;
2376 uvsy = (uvsy << 2) >> lowres;
2377 if (h >> s->chroma_y_shift) {
2378 pix_op[op_index](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
2379 pix_op[op_index](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
2382 // FIXME h261 lowres loop filter
2385 static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
2386 uint8_t *dest_cb, uint8_t *dest_cr,
2387 uint8_t **ref_picture,
2388 h264_chroma_mc_func * pix_op,
2391 const int lowres = s->avctx->lowres;
2392 const int op_index = FFMIN(lowres, 2);
2393 const int block_s = 8 >> lowres;
2394 const int s_mask = (2 << lowres) - 1;
2395 const int h_edge_pos = s->h_edge_pos >> lowres + 1;
2396 const int v_edge_pos = s->v_edge_pos >> lowres + 1;
2397 int emu = 0, src_x, src_y, offset, sx, sy;
2400 if (s->quarter_sample) {
2405 /* In case of 8X8, we construct a single chroma motion vector
2406 with a special rounding */
2407 mx = ff_h263_round_chroma(mx);
2408 my = ff_h263_round_chroma(my);
2412 src_x = s->mb_x * block_s + (mx >> lowres + 1);
2413 src_y = s->mb_y * block_s + (my >> lowres + 1);
2415 offset = src_y * s->uvlinesize + src_x;
2416 ptr = ref_picture[1] + offset;
2417 if (s->flags & CODEC_FLAG_EMU_EDGE) {
2418 if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
2419 (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
2420 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
2421 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
2422 ptr = s->edge_emu_buffer;
2426 sx = (sx << 2) >> lowres;
2427 sy = (sy << 2) >> lowres;
2428 pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
2430 ptr = ref_picture[2] + offset;
2432 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9,
2433 src_x, src_y, h_edge_pos, v_edge_pos);
2434 ptr = s->edge_emu_buffer;
2436 pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
2440 * motion compensation of a single macroblock
2442 * @param dest_y luma destination pointer
2443 * @param dest_cb chroma cb/u destination pointer
2444 * @param dest_cr chroma cr/v destination pointer
2445 * @param dir direction (0->forward, 1->backward)
2446 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
2447 * @param pix_op halfpel motion compensation function (average or put normally)
2448 * the motion vectors are taken from s->mv and the MV type from s->mv_type
2450 static inline void MPV_motion_lowres(MpegEncContext *s,
2451 uint8_t *dest_y, uint8_t *dest_cb,
2453 int dir, uint8_t **ref_picture,
2454 h264_chroma_mc_func *pix_op)
2458 const int lowres = s->avctx->lowres;
2459 const int block_s = 8 >>lowres;
2464 switch (s->mv_type) {
2466 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2468 ref_picture, pix_op,
2469 s->mv[dir][0][0], s->mv[dir][0][1],
2475 for (i = 0; i < 4; i++) {
2476 hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
2477 s->linesize) * block_s,
2478 ref_picture[0], 0, 0,
2479 (2 * mb_x + (i & 1)) * block_s,
2480 (2 * mb_y + (i >> 1)) * block_s,
2481 s->width, s->height, s->linesize,
2482 s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
2483 block_s, block_s, pix_op,
2484 s->mv[dir][i][0], s->mv[dir][i][1]);
2486 mx += s->mv[dir][i][0];
2487 my += s->mv[dir][i][1];
2490 if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY))
2491 chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
2495 if (s->picture_structure == PICT_FRAME) {
2497 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2498 1, 0, s->field_select[dir][0],
2499 ref_picture, pix_op,
2500 s->mv[dir][0][0], s->mv[dir][0][1],
2503 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2504 1, 1, s->field_select[dir][1],
2505 ref_picture, pix_op,
2506 s->mv[dir][1][0], s->mv[dir][1][1],
2509 if (s->picture_structure != s->field_select[dir][0] + 1 &&
2510 s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
2511 ref_picture = s->current_picture_ptr->f.data;
2514 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2515 0, 0, s->field_select[dir][0],
2516 ref_picture, pix_op,
2518 s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
2522 for (i = 0; i < 2; i++) {
2523 uint8_t **ref2picture;
2525 if (s->picture_structure == s->field_select[dir][i] + 1 ||
2526 s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
2527 ref2picture = ref_picture;
2529 ref2picture = s->current_picture_ptr->f.data;
2532 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2533 0, 0, s->field_select[dir][i],
2534 ref2picture, pix_op,
2535 s->mv[dir][i][0], s->mv[dir][i][1] +
2536 2 * block_s * i, block_s, mb_y >> 1);
2538 dest_y += 2 * block_s * s->linesize;
2539 dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
2540 dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
2544 if (s->picture_structure == PICT_FRAME) {
2545 for (i = 0; i < 2; i++) {
2547 for (j = 0; j < 2; j++) {
2548 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2550 ref_picture, pix_op,
2551 s->mv[dir][2 * i + j][0],
2552 s->mv[dir][2 * i + j][1],
2555 pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
2558 for (i = 0; i < 2; i++) {
2559 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2560 0, 0, s->picture_structure != i + 1,
2561 ref_picture, pix_op,
2562 s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
2563 2 * block_s, mb_y >> 1);
2565 // after put we make avg of the same block
2566 pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
2568 // opposite parity is always in the same
2569 // frame if this is second field
2570 if (!s->first_field) {
2571 ref_picture = s->current_picture_ptr->f.data;
2582 * find the lowest MB row referenced in the MVs
2584 int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
2586 int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
2587 int my, off, i, mvs;
2589 if (s->picture_structure != PICT_FRAME || s->mcsel)
2592 switch (s->mv_type) {
2606 for (i = 0; i < mvs; i++) {
2607 my = s->mv[dir][i][1]<<qpel_shift;
2608 my_max = FFMAX(my_max, my);
2609 my_min = FFMIN(my_min, my);
2612 off = (FFMAX(-my_min, my_max) + 63) >> 6;
2614 return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
2616 return s->mb_height-1;
2619 /* put block[] to dest[] */
2620 static inline void put_dct(MpegEncContext *s,
2621 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
2623 s->dct_unquantize_intra(s, block, i, qscale);
2624 s->dsp.idct_put (dest, line_size, block);
2627 /* add block[] to dest[] */
2628 static inline void add_dct(MpegEncContext *s,
2629 int16_t *block, int i, uint8_t *dest, int line_size)
2631 if (s->block_last_index[i] >= 0) {
2632 s->dsp.idct_add (dest, line_size, block);
2636 static inline void add_dequant_dct(MpegEncContext *s,
2637 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
2639 if (s->block_last_index[i] >= 0) {
2640 s->dct_unquantize_inter(s, block, i, qscale);
2642 s->dsp.idct_add (dest, line_size, block);
2647 * Clean dc, ac, coded_block for the current non-intra MB.
2649 void ff_clean_intra_table_entries(MpegEncContext *s)
2651 int wrap = s->b8_stride;
2652 int xy = s->block_index[0];
2655 s->dc_val[0][xy + 1 ] =
2656 s->dc_val[0][xy + wrap] =
2657 s->dc_val[0][xy + 1 + wrap] = 1024;
2659 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
2660 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
2661 if (s->msmpeg4_version>=3) {
2662 s->coded_block[xy ] =
2663 s->coded_block[xy + 1 ] =
2664 s->coded_block[xy + wrap] =
2665 s->coded_block[xy + 1 + wrap] = 0;
2668 wrap = s->mb_stride;
2669 xy = s->mb_x + s->mb_y * wrap;
2671 s->dc_val[2][xy] = 1024;
2673 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
2674 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
2676 s->mbintra_table[xy]= 0;
2679 /* generic function called after a macroblock has been parsed by the
2680 decoder or after it has been encoded by the encoder.
2682 Important variables used:
2683 s->mb_intra : true if intra macroblock
2684 s->mv_dir : motion vector direction
2685 s->mv_type : motion vector type
2686 s->mv : motion vector
2687 s->interlaced_dct : true if interlaced dct used (mpeg2)
2689 static av_always_inline
2690 void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
2691 int lowres_flag, int is_mpeg12)
2693 const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
2694 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
2695 ff_xvmc_decode_mb(s);//xvmc uses pblocks
2699 if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
2700 /* print DCT coefficients */
2702 av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
2704 for(j=0; j<64; j++){
2705 av_log(s->avctx, AV_LOG_DEBUG, "%5d", block[i][s->dsp.idct_permutation[j]]);
2707 av_log(s->avctx, AV_LOG_DEBUG, "\n");
2711 s->current_picture.qscale_table[mb_xy] = s->qscale;
2713 /* update DC predictors for P macroblocks */
2715 if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
2716 if(s->mbintra_table[mb_xy])
2717 ff_clean_intra_table_entries(s);
2721 s->last_dc[2] = 128 << s->intra_dc_precision;
2724 else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
2725 s->mbintra_table[mb_xy]=1;
2727 if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==AV_PICTURE_TYPE_B) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc
2728 uint8_t *dest_y, *dest_cb, *dest_cr;
2729 int dct_linesize, dct_offset;
2730 op_pixels_func (*op_pix)[4];
2731 qpel_mc_func (*op_qpix)[16];
2732 const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
2733 const int uvlinesize = s->current_picture.f.linesize[1];
2734 const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
2735 const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
2737 /* avoid copy if macroblock skipped in last frame too */
2738 /* skip only during decoding as we might trash the buffers during encoding a bit */
2740 uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
2742 if (s->mb_skipped) {
2744 av_assert2(s->pict_type!=AV_PICTURE_TYPE_I);
2746 } else if(!s->current_picture.reference) {
2749 *mbskip_ptr = 0; /* not skipped */
2753 dct_linesize = linesize << s->interlaced_dct;
2754 dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
2758 dest_cb= s->dest[1];
2759 dest_cr= s->dest[2];
2761 dest_y = s->b_scratchpad;
2762 dest_cb= s->b_scratchpad+16*linesize;
2763 dest_cr= s->b_scratchpad+32*linesize;
2767 /* motion handling */
2768 /* decoding or more than one mb_type (MC was already done otherwise) */
2771 if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
2772 if (s->mv_dir & MV_DIR_FORWARD) {
2773 ff_thread_await_progress(&s->last_picture_ptr->tf,
2774 ff_MPV_lowest_referenced_row(s, 0),
2777 if (s->mv_dir & MV_DIR_BACKWARD) {
2778 ff_thread_await_progress(&s->next_picture_ptr->tf,
2779 ff_MPV_lowest_referenced_row(s, 1),
2785 h264_chroma_mc_func *op_pix = s->h264chroma.put_h264_chroma_pixels_tab;
2787 if (s->mv_dir & MV_DIR_FORWARD) {
2788 MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix);
2789 op_pix = s->h264chroma.avg_h264_chroma_pixels_tab;
2791 if (s->mv_dir & MV_DIR_BACKWARD) {
2792 MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix);
2795 op_qpix= s->me.qpel_put;
2796 if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
2797 op_pix = s->hdsp.put_pixels_tab;
2799 op_pix = s->hdsp.put_no_rnd_pixels_tab;
2801 if (s->mv_dir & MV_DIR_FORWARD) {
2802 ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
2803 op_pix = s->hdsp.avg_pixels_tab;
2804 op_qpix= s->me.qpel_avg;
2806 if (s->mv_dir & MV_DIR_BACKWARD) {
2807 ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
2812 /* skip dequant / idct if we are really late ;) */
2813 if(s->avctx->skip_idct){
2814 if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
2815 ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
2816 || s->avctx->skip_idct >= AVDISCARD_ALL)
2820 /* add dct residue */
2821 if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
2822 || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
2823 add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
2824 add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
2825 add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
2826 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
2828 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2829 if (s->chroma_y_shift){
2830 add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
2831 add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
2835 add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
2836 add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
2837 add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
2838 add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
2841 } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
2842 add_dct(s, block[0], 0, dest_y , dct_linesize);
2843 add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
2844 add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
2845 add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
2847 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2848 if(s->chroma_y_shift){//Chroma420
2849 add_dct(s, block[4], 4, dest_cb, uvlinesize);
2850 add_dct(s, block[5], 5, dest_cr, uvlinesize);
2853 dct_linesize = uvlinesize << s->interlaced_dct;
2854 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
2856 add_dct(s, block[4], 4, dest_cb, dct_linesize);
2857 add_dct(s, block[5], 5, dest_cr, dct_linesize);
2858 add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
2859 add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
2860 if(!s->chroma_x_shift){//Chroma444
2861 add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
2862 add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
2863 add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
2864 add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
2869 else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
2870 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
2873 /* dct only in intra block */
2874 if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
2875 put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
2876 put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
2877 put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
2878 put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
2880 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2881 if(s->chroma_y_shift){
2882 put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
2883 put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
2887 put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
2888 put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
2889 put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
2890 put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
2894 s->dsp.idct_put(dest_y , dct_linesize, block[0]);
2895 s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
2896 s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
2897 s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
2899 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2900 if(s->chroma_y_shift){
2901 s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
2902 s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
2905 dct_linesize = uvlinesize << s->interlaced_dct;
2906 dct_offset = s->interlaced_dct? uvlinesize : uvlinesize*block_size;
2908 s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
2909 s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
2910 s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
2911 s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
2912 if(!s->chroma_x_shift){//Chroma444
2913 s->dsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
2914 s->dsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
2915 s->dsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
2916 s->dsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
2924 s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
2925 s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
2926 s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
2931 void ff_MPV_decode_mb(MpegEncContext *s, int16_t block[12][64]){
2933 if(s->out_format == FMT_MPEG1) {
2934 if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
2935 else MPV_decode_mb_internal(s, block, 0, 1);
2938 if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
2939 else MPV_decode_mb_internal(s, block, 0, 0);
2943 * @param h is the normal height, this will be reduced automatically if needed for the last row
2945 void ff_draw_horiz_band(AVCodecContext *avctx, DSPContext *dsp, Picture *cur,
2946 Picture *last, int y, int h, int picture_structure,
2947 int first_field, int draw_edges, int low_delay,
2948 int v_edge_pos, int h_edge_pos)
2950 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
2951 int hshift = desc->log2_chroma_w;
2952 int vshift = desc->log2_chroma_h;
2953 const int field_pic = picture_structure != PICT_FRAME;
2959 if (!avctx->hwaccel &&
2960 !(avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) &&
2963 !(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
2964 int *linesize = cur->f.linesize;
2965 int sides = 0, edge_h;
2966 if (y==0) sides |= EDGE_TOP;
2967 if (y + h >= v_edge_pos)
2968 sides |= EDGE_BOTTOM;
2970 edge_h= FFMIN(h, v_edge_pos - y);
2972 dsp->draw_edges(cur->f.data[0] + y * linesize[0],
2973 linesize[0], h_edge_pos, edge_h,
2974 EDGE_WIDTH, EDGE_WIDTH, sides);
2975 dsp->draw_edges(cur->f.data[1] + (y >> vshift) * linesize[1],
2976 linesize[1], h_edge_pos >> hshift, edge_h >> vshift,
2977 EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, sides);
2978 dsp->draw_edges(cur->f.data[2] + (y >> vshift) * linesize[2],
2979 linesize[2], h_edge_pos >> hshift, edge_h >> vshift,
2980 EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, sides);
2983 h = FFMIN(h, avctx->height - y);
2985 if(field_pic && first_field && !(avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
2987 if (avctx->draw_horiz_band) {
2989 int offset[AV_NUM_DATA_POINTERS];
2992 if(cur->f.pict_type == AV_PICTURE_TYPE_B || low_delay ||
2993 (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
3000 if (cur->f.pict_type == AV_PICTURE_TYPE_B &&
3001 picture_structure == PICT_FRAME &&
3002 avctx->codec_id != AV_CODEC_ID_H264 &&
3003 avctx->codec_id != AV_CODEC_ID_SVQ3) {
3004 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
3007 offset[0]= y * src->linesize[0];
3009 offset[2]= (y >> vshift) * src->linesize[1];
3010 for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
3016 avctx->draw_horiz_band(avctx, src, offset,
3017 y, picture_structure, h);
3021 void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
3023 int draw_edges = s->unrestricted_mv && !s->intra_only;
3024 ff_draw_horiz_band(s->avctx, &s->dsp, &s->current_picture,
3025 &s->last_picture, y, h, s->picture_structure,
3026 s->first_field, draw_edges, s->low_delay,
3027 s->v_edge_pos, s->h_edge_pos);
3030 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
3031 const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
3032 const int uvlinesize = s->current_picture.f.linesize[1];
3033 const int mb_size= 4 - s->avctx->lowres;
3035 s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
3036 s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
3037 s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
3038 s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
3039 s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
3040 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;
3041 //block_index is not used by mpeg2, so it is not affected by chroma_format
3043 s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) << mb_size);
3044 s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
3045 s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
3047 if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
3049 if(s->picture_structure==PICT_FRAME){
3050 s->dest[0] += s->mb_y * linesize << mb_size;
3051 s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
3052 s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
3054 s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
3055 s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
3056 s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
3057 av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
3063 * Permute an 8x8 block.
3064 * @param block the block which will be permuted according to the given permutation vector
3065 * @param permutation the permutation vector
3066 * @param last the last non zero coefficient in scantable order, used to speed the permutation up
3067 * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
3068 * (inverse) permutated to scantable order!
3070 void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
3076 //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
3078 for(i=0; i<=last; i++){
3079 const int j= scantable[i];
3084 for(i=0; i<=last; i++){
3085 const int j= scantable[i];
3086 const int perm_j= permutation[j];
3087 block[perm_j]= temp[j];
3091 void ff_mpeg_flush(AVCodecContext *avctx){
3093 MpegEncContext *s = avctx->priv_data;
3095 if(s==NULL || s->picture==NULL)
3098 for (i = 0; i < MAX_PICTURE_COUNT; i++)
3099 ff_mpeg_unref_picture(s, &s->picture[i]);
3100 s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
3102 s->mb_x= s->mb_y= 0;
3105 s->parse_context.state= -1;
3106 s->parse_context.frame_start_found= 0;
3107 s->parse_context.overread= 0;
3108 s->parse_context.overread_index= 0;
3109 s->parse_context.index= 0;
3110 s->parse_context.last_index= 0;
3111 s->bitstream_buffer_size=0;
3115 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
3116 int16_t *block, int n, int qscale)
3118 int i, level, nCoeffs;
3119 const uint16_t *quant_matrix;
3121 nCoeffs= s->block_last_index[n];
3123 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
3124 /* XXX: only mpeg1 */
3125 quant_matrix = s->intra_matrix;
3126 for(i=1;i<=nCoeffs;i++) {
3127 int j= s->intra_scantable.permutated[i];
3132 level = (int)(level * qscale * quant_matrix[j]) >> 3;
3133 level = (level - 1) | 1;
3136 level = (int)(level * qscale * quant_matrix[j]) >> 3;
3137 level = (level - 1) | 1;
3144 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
3145 int16_t *block, int n, int qscale)
3147 int i, level, nCoeffs;
3148 const uint16_t *quant_matrix;
3150 nCoeffs= s->block_last_index[n];
3152 quant_matrix = s->inter_matrix;
3153 for(i=0; i<=nCoeffs; i++) {
3154 int j= s->intra_scantable.permutated[i];
3159 level = (((level << 1) + 1) * qscale *
3160 ((int) (quant_matrix[j]))) >> 4;
3161 level = (level - 1) | 1;
3164 level = (((level << 1) + 1) * qscale *
3165 ((int) (quant_matrix[j]))) >> 4;
3166 level = (level - 1) | 1;
3173 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
3174 int16_t *block, int n, int qscale)
3176 int i, level, nCoeffs;
3177 const uint16_t *quant_matrix;
3179 if(s->alternate_scan) nCoeffs= 63;
3180 else nCoeffs= s->block_last_index[n];
3182 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
3183 quant_matrix = s->intra_matrix;
3184 for(i=1;i<=nCoeffs;i++) {
3185 int j= s->intra_scantable.permutated[i];
3190 level = (int)(level * qscale * quant_matrix[j]) >> 3;
3193 level = (int)(level * qscale * quant_matrix[j]) >> 3;
3200 static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
3201 int16_t *block, int n, int qscale)
3203 int i, level, nCoeffs;
3204 const uint16_t *quant_matrix;
3207 if(s->alternate_scan) nCoeffs= 63;
3208 else nCoeffs= s->block_last_index[n];
3210 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
3212 quant_matrix = s->intra_matrix;
3213 for(i=1;i<=nCoeffs;i++) {
3214 int j= s->intra_scantable.permutated[i];
3219 level = (int)(level * qscale * quant_matrix[j]) >> 3;
3222 level = (int)(level * qscale * quant_matrix[j]) >> 3;
3231 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
3232 int16_t *block, int n, int qscale)
3234 int i, level, nCoeffs;
3235 const uint16_t *quant_matrix;
3238 if(s->alternate_scan) nCoeffs= 63;
3239 else nCoeffs= s->block_last_index[n];
3241 quant_matrix = s->inter_matrix;
3242 for(i=0; i<=nCoeffs; i++) {
3243 int j= s->intra_scantable.permutated[i];
3248 level = (((level << 1) + 1) * qscale *
3249 ((int) (quant_matrix[j]))) >> 4;
3252 level = (((level << 1) + 1) * qscale *
3253 ((int) (quant_matrix[j]))) >> 4;
3262 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
3263 int16_t *block, int n, int qscale)
3265 int i, level, qmul, qadd;
3268 assert(s->block_last_index[n]>=0);
3273 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
3274 qadd = (qscale - 1) | 1;
3281 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
3283 for(i=1; i<=nCoeffs; i++) {
3287 level = level * qmul - qadd;
3289 level = level * qmul + qadd;
3296 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
3297 int16_t *block, int n, int qscale)
3299 int i, level, qmul, qadd;
3302 assert(s->block_last_index[n]>=0);
3304 qadd = (qscale - 1) | 1;
3307 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
3309 for(i=0; i<=nCoeffs; i++) {
3313 level = level * qmul - qadd;
3315 level = level * qmul + qadd;
3323 * set qscale and update qscale dependent variables.
3325 void ff_set_qscale(MpegEncContext * s, int qscale)
3329 else if (qscale > 31)
3333 s->chroma_qscale= s->chroma_qscale_table[qscale];
3335 s->y_dc_scale= s->y_dc_scale_table[ qscale ];
3336 s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
3339 void ff_MPV_report_decode_progress(MpegEncContext *s)
3341 if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
3342 ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
3345 #if CONFIG_ERROR_RESILIENCE
3346 void ff_mpeg_er_frame_start(MpegEncContext *s)
3348 ERContext *er = &s->er;
3350 er->cur_pic = s->current_picture_ptr;
3351 er->last_pic = s->last_picture_ptr;
3352 er->next_pic = s->next_picture_ptr;
3354 er->pp_time = s->pp_time;
3355 er->pb_time = s->pb_time;
3356 er->quarter_sample = s->quarter_sample;
3357 er->partitioned_frame = s->partitioned_frame;
3359 ff_er_frame_start(er);
3361 #endif /* CONFIG_ERROR_RESILIENCE */