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/imgutils.h"
35 #include "mpegvideo.h"
38 #include "xvmc_internal.h"
45 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
46 int16_t *block, int n, int qscale);
47 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
48 int16_t *block, int n, int qscale);
49 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
50 int16_t *block, int n, int qscale);
51 static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
52 int16_t *block, int n, int qscale);
53 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
54 int16_t *block, int n, int qscale);
55 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
56 int16_t *block, int n, int qscale);
57 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
58 int16_t *block, int n, int qscale);
64 static const uint8_t ff_default_chroma_qscale_table[32] = {
65 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
66 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
67 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
70 const uint8_t ff_mpeg1_dc_scale_table[128] = {
71 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
72 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
73 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
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,
82 static const uint8_t mpeg2_dc_scale_table1[128] = {
83 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
84 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
85 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
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,
94 static const uint8_t mpeg2_dc_scale_table2[128] = {
95 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
96 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
97 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
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,
106 static const uint8_t mpeg2_dc_scale_table3[128] = {
107 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
108 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
109 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
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,
118 const uint8_t *const ff_mpeg2_dc_scale_table[4] = {
119 ff_mpeg1_dc_scale_table,
120 mpeg2_dc_scale_table1,
121 mpeg2_dc_scale_table2,
122 mpeg2_dc_scale_table3,
125 const enum AVPixelFormat ff_pixfmt_list_420[] = {
130 const enum AVPixelFormat ff_hwaccel_pixfmt_list_420[] = {
132 AV_PIX_FMT_DXVA2_VLD,
135 AV_PIX_FMT_VAAPI_VLD,
147 const uint8_t *avpriv_mpv_find_start_code(const uint8_t *av_restrict p,
149 uint32_t *av_restrict state)
157 for (i = 0; i < 3; i++) {
158 uint32_t tmp = *state << 8;
159 *state = tmp + *(p++);
160 if (tmp == 0x100 || p == end)
165 if (p[-1] > 1 ) p += 3;
166 else if (p[-2] ) p += 2;
167 else if (p[-3]|(p[-1]-1)) p++;
174 p = FFMIN(p, end) - 4;
180 /* init common dct for both encoder and decoder */
181 av_cold int ff_dct_common_init(MpegEncContext *s)
183 ff_dsputil_init(&s->dsp, s->avctx);
184 ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
186 s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
187 s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
188 s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
189 s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
190 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
191 if (s->flags & CODEC_FLAG_BITEXACT)
192 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
193 s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
196 ff_MPV_common_init_x86(s);
198 ff_MPV_common_init_axp(s);
200 ff_MPV_common_init_arm(s);
202 ff_MPV_common_init_altivec(s);
204 ff_MPV_common_init_bfin(s);
207 /* load & permutate scantables
208 * note: only wmv uses different ones
210 if (s->alternate_scan) {
211 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
212 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
214 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
215 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
217 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
218 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
223 void ff_copy_picture(Picture *dst, Picture *src)
226 dst->f.type = FF_BUFFER_TYPE_COPY;
230 * Release a frame buffer
232 static void free_frame_buffer(MpegEncContext *s, Picture *pic)
234 pic->period_since_free = 0;
235 /* WM Image / Screen codecs allocate internal buffers with different
236 * dimensions / colorspaces; ignore user-defined callbacks for these. */
237 if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
238 s->codec_id != AV_CODEC_ID_VC1IMAGE &&
239 s->codec_id != AV_CODEC_ID_MSS2)
240 ff_thread_release_buffer(s->avctx, &pic->f);
242 avcodec_default_release_buffer(s->avctx, &pic->f);
243 av_freep(&pic->f.hwaccel_picture_private);
246 int ff_mpv_frame_size_alloc(MpegEncContext *s, int linesize)
248 int alloc_size = FFALIGN(FFABS(linesize) + 64, 32);
250 // edge emu needs blocksize + filter length - 1
251 // (= 17x17 for halfpel / 21x21 for h264)
252 // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9
253 // at uvlinesize. It supports only YUV420 so 24x24 is enough
254 // linesize * interlaced * MBsize
255 FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, alloc_size * 4 * 24,
258 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, alloc_size * 4 * 16 * 2,
260 s->me.temp = s->me.scratchpad;
261 s->rd_scratchpad = s->me.scratchpad;
262 s->b_scratchpad = s->me.scratchpad;
263 s->obmc_scratchpad = s->me.scratchpad + 16;
267 av_freep(&s->edge_emu_buffer);
268 return AVERROR(ENOMEM);
272 * Allocate a frame buffer
274 static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
278 if (s->avctx->hwaccel) {
279 assert(!pic->f.hwaccel_picture_private);
280 if (s->avctx->hwaccel->priv_data_size) {
281 pic->f.hwaccel_picture_private = av_mallocz(s->avctx->hwaccel->priv_data_size);
282 if (!pic->f.hwaccel_picture_private) {
283 av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
289 if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
290 s->codec_id != AV_CODEC_ID_VC1IMAGE &&
291 s->codec_id != AV_CODEC_ID_MSS2)
292 r = ff_thread_get_buffer(s->avctx, &pic->f);
294 r = avcodec_default_get_buffer(s->avctx, &pic->f);
296 if (r < 0 || !pic->f.type || !pic->f.data[0]) {
297 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %p)\n",
298 r, pic->f.type, pic->f.data[0]);
299 av_freep(&pic->f.hwaccel_picture_private);
303 if (s->linesize && (s->linesize != pic->f.linesize[0] ||
304 s->uvlinesize != pic->f.linesize[1])) {
305 av_log(s->avctx, AV_LOG_ERROR,
306 "get_buffer() failed (stride changed)\n");
307 free_frame_buffer(s, pic);
311 if (pic->f.linesize[1] != pic->f.linesize[2]) {
312 av_log(s->avctx, AV_LOG_ERROR,
313 "get_buffer() failed (uv stride mismatch)\n");
314 free_frame_buffer(s, pic);
318 if (!s->edge_emu_buffer &&
319 (ret = ff_mpv_frame_size_alloc(s, pic->f.linesize[0])) < 0) {
320 av_log(s->avctx, AV_LOG_ERROR,
321 "get_buffer() failed to allocate context scratch buffers.\n");
322 free_frame_buffer(s, pic);
330 * Allocate a Picture.
331 * The pixels are allocated/set by calling get_buffer() if shared = 0
333 int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
335 const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;
337 // the + 1 is needed so memset(,,stride*height) does not sig11
339 const int mb_array_size = s->mb_stride * s->mb_height;
340 const int b8_array_size = s->b8_stride * s->mb_height * 2;
341 const int b4_array_size = s->b4_stride * s->mb_height * 4;
346 assert(pic->f.data[0]);
347 assert(pic->f.type == 0 || pic->f.type == FF_BUFFER_TYPE_SHARED);
348 pic->f.type = FF_BUFFER_TYPE_SHARED;
350 assert(!pic->f.data[0]);
352 if (alloc_frame_buffer(s, pic) < 0)
355 s->linesize = pic->f.linesize[0];
356 s->uvlinesize = pic->f.linesize[1];
359 if (pic->f.qscale_table == NULL) {
361 FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_var,
362 mb_array_size * sizeof(int16_t), fail)
363 FF_ALLOCZ_OR_GOTO(s->avctx, pic->mc_mb_var,
364 mb_array_size * sizeof(int16_t), fail)
365 FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_mean,
366 mb_array_size * sizeof(int8_t ), fail)
369 FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.mbskip_table,
370 mb_array_size * sizeof(uint8_t) + 2, fail)// the + 2 is for the slice end check
371 FF_ALLOCZ_OR_GOTO(s->avctx, pic->qscale_table_base,
372 (big_mb_num + s->mb_stride) * sizeof(uint8_t),
374 FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_type_base,
375 (big_mb_num + s->mb_stride) * sizeof(uint32_t),
377 pic->f.mb_type = pic->mb_type_base + 2 * s->mb_stride + 1;
378 pic->f.qscale_table = pic->qscale_table_base + 2 * s->mb_stride + 1;
379 if (s->out_format == FMT_H264) {
380 for (i = 0; i < 2; i++) {
381 FF_ALLOCZ_OR_GOTO(s->avctx, pic->motion_val_base[i],
382 2 * (b4_array_size + 4) * sizeof(int16_t),
384 pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
385 FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.ref_index[i],
386 4 * mb_array_size * sizeof(uint8_t), fail)
388 pic->f.motion_subsample_log2 = 2;
389 } else if (s->out_format == FMT_H263 || s->encoding ||
390 (s->avctx->debug & FF_DEBUG_MV) || s->avctx->debug_mv) {
391 for (i = 0; i < 2; i++) {
392 FF_ALLOCZ_OR_GOTO(s->avctx, pic->motion_val_base[i],
393 2 * (b8_array_size + 4) * sizeof(int16_t),
395 pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
396 FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.ref_index[i],
397 4 * mb_array_size * sizeof(uint8_t), fail)
399 pic->f.motion_subsample_log2 = 3;
401 if (s->avctx->debug&FF_DEBUG_DCT_COEFF) {
402 FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.dct_coeff,
403 64 * mb_array_size * sizeof(int16_t) * 6, fail)
405 pic->f.qstride = s->mb_stride;
406 FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.pan_scan,
407 1 * sizeof(AVPanScan), fail)
413 fail: // for the FF_ALLOCZ_OR_GOTO macro
415 free_frame_buffer(s, pic);
420 * Deallocate a picture.
422 static void free_picture(MpegEncContext *s, Picture *pic)
426 if (pic->f.data[0] && pic->f.type != FF_BUFFER_TYPE_SHARED) {
427 free_frame_buffer(s, pic);
430 av_freep(&pic->mb_var);
431 av_freep(&pic->mc_mb_var);
432 av_freep(&pic->mb_mean);
433 av_freep(&pic->f.mbskip_table);
434 av_freep(&pic->qscale_table_base);
435 pic->f.qscale_table = NULL;
436 av_freep(&pic->mb_type_base);
437 pic->f.mb_type = NULL;
438 av_freep(&pic->f.dct_coeff);
439 av_freep(&pic->f.pan_scan);
440 pic->f.mb_type = NULL;
441 for (i = 0; i < 2; i++) {
442 av_freep(&pic->motion_val_base[i]);
443 av_freep(&pic->f.ref_index[i]);
444 pic->f.motion_val[i] = NULL;
447 if (pic->f.type == FF_BUFFER_TYPE_SHARED) {
448 for (i = 0; i < 4; i++) {
450 pic->f.data[i] = NULL;
456 static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base)
458 int y_size = s->b8_stride * (2 * s->mb_height + 1);
459 int c_size = s->mb_stride * (s->mb_height + 1);
460 int yc_size = y_size + 2 * c_size;
468 s->obmc_scratchpad = NULL;
471 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
472 ME_MAP_SIZE * sizeof(uint32_t), fail)
473 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
474 ME_MAP_SIZE * sizeof(uint32_t), fail)
475 if (s->avctx->noise_reduction) {
476 FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
477 2 * 64 * sizeof(int), fail)
480 FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
481 s->block = s->blocks[0];
483 for (i = 0; i < 12; i++) {
484 s->pblocks[i] = &s->block[i];
487 if (s->out_format == FMT_H263) {
489 FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
490 yc_size * sizeof(int16_t) * 16, fail);
491 s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
492 s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
493 s->ac_val[2] = s->ac_val[1] + c_size;
498 return -1; // free() through ff_MPV_common_end()
501 static void free_duplicate_context(MpegEncContext *s)
506 av_freep(&s->edge_emu_buffer);
507 av_freep(&s->me.scratchpad);
511 s->obmc_scratchpad = NULL;
513 av_freep(&s->dct_error_sum);
514 av_freep(&s->me.map);
515 av_freep(&s->me.score_map);
516 av_freep(&s->blocks);
517 av_freep(&s->ac_val_base);
521 static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
523 #define COPY(a) bak->a = src->a
524 COPY(edge_emu_buffer);
529 COPY(obmc_scratchpad);
536 COPY(me.map_generation);
548 int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
552 // FIXME copy only needed parts
554 backup_duplicate_context(&bak, dst);
555 memcpy(dst, src, sizeof(MpegEncContext));
556 backup_duplicate_context(dst, &bak);
557 for (i = 0; i < 12; i++) {
558 dst->pblocks[i] = &dst->block[i];
560 if (!dst->edge_emu_buffer &&
561 (ret = ff_mpv_frame_size_alloc(dst, dst->linesize)) < 0) {
562 av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
563 "scratch buffers.\n");
566 // STOP_TIMER("update_duplicate_context")
567 // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
571 int ff_mpeg_update_thread_context(AVCodecContext *dst,
572 const AVCodecContext *src)
576 MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
583 // FIXME can parameters change on I-frames?
584 // in that case dst may need a reinit
585 if (!s->context_initialized) {
586 memcpy(s, s1, sizeof(MpegEncContext));
589 s->bitstream_buffer = NULL;
590 s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
592 if (s1->context_initialized){
593 s->picture_range_start += MAX_PICTURE_COUNT;
594 s->picture_range_end += MAX_PICTURE_COUNT;
595 if((err = ff_MPV_common_init(s)) < 0){
596 memset(s, 0, sizeof(MpegEncContext));
603 if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
604 s->context_reinit = 0;
605 s->height = s1->height;
606 s->width = s1->width;
607 if ((err = ff_MPV_common_frame_size_change(s)) < 0)
611 s->avctx->coded_height = s1->avctx->coded_height;
612 s->avctx->coded_width = s1->avctx->coded_width;
613 s->avctx->width = s1->avctx->width;
614 s->avctx->height = s1->avctx->height;
616 s->coded_picture_number = s1->coded_picture_number;
617 s->picture_number = s1->picture_number;
618 s->input_picture_number = s1->input_picture_number;
620 av_assert0(!s->picture || s->picture != s1->picture);
621 memcpy(s->picture, s1->picture, s1->picture_count * sizeof(Picture));
622 memcpy(&s->last_picture, &s1->last_picture,
623 (char *) &s1->last_picture_ptr - (char *) &s1->last_picture);
625 // reset s->picture[].f.extended_data to s->picture[].f.data
626 for (i = 0; i < s->picture_count; i++) {
627 s->picture[i].f.extended_data = s->picture[i].f.data;
628 s->picture[i].period_since_free ++;
631 s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
632 s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
633 s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
635 // Error/bug resilience
636 s->next_p_frame_damaged = s1->next_p_frame_damaged;
637 s->workaround_bugs = s1->workaround_bugs;
638 s->padding_bug_score = s1->padding_bug_score;
641 memcpy(&s->time_increment_bits, &s1->time_increment_bits,
642 (char *) &s1->shape - (char *) &s1->time_increment_bits);
645 s->max_b_frames = s1->max_b_frames;
646 s->low_delay = s1->low_delay;
647 s->droppable = s1->droppable;
649 // DivX handling (doesn't work)
650 s->divx_packed = s1->divx_packed;
652 if (s1->bitstream_buffer) {
653 if (s1->bitstream_buffer_size +
654 FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
655 av_fast_malloc(&s->bitstream_buffer,
656 &s->allocated_bitstream_buffer_size,
657 s1->allocated_bitstream_buffer_size);
658 s->bitstream_buffer_size = s1->bitstream_buffer_size;
659 memcpy(s->bitstream_buffer, s1->bitstream_buffer,
660 s1->bitstream_buffer_size);
661 memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
662 FF_INPUT_BUFFER_PADDING_SIZE);
665 // linesize dependend scratch buffer allocation
666 if (!s->edge_emu_buffer)
668 if (ff_mpv_frame_size_alloc(s, s1->linesize) < 0) {
669 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
670 "scratch buffers.\n");
671 return AVERROR(ENOMEM);
674 av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
675 "be allocated due to unknown size.\n");
678 // MPEG2/interlacing info
679 memcpy(&s->progressive_sequence, &s1->progressive_sequence,
680 (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
682 if (!s1->first_field) {
683 s->last_pict_type = s1->pict_type;
684 if (s1->current_picture_ptr)
685 s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f.quality;
687 if (s1->pict_type != AV_PICTURE_TYPE_B) {
688 s->last_non_b_pict_type = s1->pict_type;
696 * Set the given MpegEncContext to common defaults
697 * (same for encoding and decoding).
698 * The changed fields will not depend upon the
699 * prior state of the MpegEncContext.
701 void ff_MPV_common_defaults(MpegEncContext *s)
703 s->y_dc_scale_table =
704 s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
705 s->chroma_qscale_table = ff_default_chroma_qscale_table;
706 s->progressive_frame = 1;
707 s->progressive_sequence = 1;
708 s->picture_structure = PICT_FRAME;
710 s->coded_picture_number = 0;
711 s->picture_number = 0;
712 s->input_picture_number = 0;
714 s->picture_in_gop_number = 0;
719 s->picture_range_start = 0;
720 s->picture_range_end = MAX_PICTURE_COUNT;
722 s->slice_context_count = 1;
726 * Set the given MpegEncContext to defaults for decoding.
727 * the changed fields will not depend upon
728 * the prior state of the MpegEncContext.
730 void ff_MPV_decode_defaults(MpegEncContext *s)
732 ff_MPV_common_defaults(s);
736 * Initialize and allocates MpegEncContext fields dependent on the resolution.
738 static int init_context_frame(MpegEncContext *s)
740 int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
742 s->mb_width = (s->width + 15) / 16;
743 s->mb_stride = s->mb_width + 1;
744 s->b8_stride = s->mb_width * 2 + 1;
745 s->b4_stride = s->mb_width * 4 + 1;
746 mb_array_size = s->mb_height * s->mb_stride;
747 mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
749 /* set default edge pos, will be overriden
750 * in decode_header if needed */
751 s->h_edge_pos = s->mb_width * 16;
752 s->v_edge_pos = s->mb_height * 16;
754 s->mb_num = s->mb_width * s->mb_height;
759 s->block_wrap[3] = s->b8_stride;
761 s->block_wrap[5] = s->mb_stride;
763 y_size = s->b8_stride * (2 * s->mb_height + 1);
764 c_size = s->mb_stride * (s->mb_height + 1);
765 yc_size = y_size + 2 * c_size;
767 FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int), fail); // error ressilience code looks cleaner with this
768 for (y = 0; y < s->mb_height; y++)
769 for (x = 0; x < s->mb_width; x++)
770 s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
772 s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
775 /* Allocate MV tables */
776 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
777 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
778 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
779 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
780 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
781 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
782 s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
783 s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
784 s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
785 s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
786 s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + s->mb_stride + 1;
787 s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
789 /* Allocate MB type table */
790 FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size * sizeof(uint16_t), fail) // needed for encoding
792 FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail)
794 FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
795 mb_array_size * sizeof(float), fail);
796 FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
797 mb_array_size * sizeof(float), fail);
801 FF_ALLOC_OR_GOTO(s->avctx, s->er_temp_buffer,
802 mb_array_size * sizeof(uint8_t), fail);
803 FF_ALLOCZ_OR_GOTO(s->avctx, s->error_status_table,
804 mb_array_size * sizeof(uint8_t), fail);
806 if (s->codec_id == AV_CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)) {
807 /* interlaced direct mode decoding tables */
808 for (i = 0; i < 2; i++) {
810 for (j = 0; j < 2; j++) {
811 for (k = 0; k < 2; k++) {
812 FF_ALLOCZ_OR_GOTO(s->avctx,
813 s->b_field_mv_table_base[i][j][k],
814 mv_table_size * 2 * sizeof(int16_t),
816 s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
819 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
820 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
821 s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
823 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
826 if (s->out_format == FMT_H263) {
828 FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
829 s->coded_block = s->coded_block_base + s->b8_stride + 1;
831 /* cbp, ac_pred, pred_dir */
832 FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table , mb_array_size * sizeof(uint8_t), fail);
833 FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail);
836 if (s->h263_pred || s->h263_plus || !s->encoding) {
838 // MN: we need these for error resilience of intra-frames
839 FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
840 s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
841 s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
842 s->dc_val[2] = s->dc_val[1] + c_size;
843 for (i = 0; i < yc_size; i++)
844 s->dc_val_base[i] = 1024;
847 /* which mb is a intra block */
848 FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
849 memset(s->mbintra_table, 1, mb_array_size);
851 /* init macroblock skip table */
852 FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
853 // Note the + 1 is for a quicker mpeg4 slice_end detection
857 return AVERROR(ENOMEM);
861 * init common structure for both encoder and decoder.
862 * this assumes that some variables like width/height are already set
864 av_cold int ff_MPV_common_init(MpegEncContext *s)
867 int nb_slices = (HAVE_THREADS &&
868 s->avctx->active_thread_type & FF_THREAD_SLICE) ?
869 s->avctx->thread_count : 1;
871 if (s->encoding && s->avctx->slices)
872 nb_slices = s->avctx->slices;
874 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
875 s->mb_height = (s->height + 31) / 32 * 2;
876 else if (s->codec_id != AV_CODEC_ID_H264)
877 s->mb_height = (s->height + 15) / 16;
879 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
880 av_log(s->avctx, AV_LOG_ERROR,
881 "decoding to AV_PIX_FMT_NONE is not supported.\n");
885 if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
888 max_slices = FFMIN(MAX_THREADS, s->mb_height);
890 max_slices = MAX_THREADS;
891 av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
892 " reducing to %d\n", nb_slices, max_slices);
893 nb_slices = max_slices;
896 if ((s->width || s->height) &&
897 av_image_check_size(s->width, s->height, 0, s->avctx))
900 ff_dct_common_init(s);
902 s->flags = s->avctx->flags;
903 s->flags2 = s->avctx->flags2;
905 /* set chroma shifts */
906 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
908 /* convert fourcc to upper case */
909 s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
910 s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag);
912 s->avctx->coded_frame = &s->current_picture.f;
915 if (s->msmpeg4_version) {
916 FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
917 2 * 2 * (MAX_LEVEL + 1) *
918 (MAX_RUN + 1) * 2 * sizeof(int), fail);
920 FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
922 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix, 64 * 32 * sizeof(int), fail)
923 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix, 64 * 32 * sizeof(int), fail)
924 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix, 64 * 32 * sizeof(int), fail)
925 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail)
926 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail)
927 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail)
928 FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture *), fail)
929 FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture *), fail)
931 if (s->avctx->noise_reduction) {
932 FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset, 2 * 64 * sizeof(uint16_t), fail);
936 s->picture_count = MAX_PICTURE_COUNT * FFMAX(1, s->avctx->thread_count);
937 FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
938 s->picture_count * sizeof(Picture), fail);
939 for (i = 0; i < s->picture_count; i++) {
940 avcodec_get_frame_defaults(&s->picture[i].f);
943 if (init_context_frame(s))
946 s->parse_context.state = -1;
948 s->context_initialized = 1;
949 s->thread_context[0] = s;
951 // if (s->width && s->height) {
953 for (i = 1; i < nb_slices; i++) {
954 s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
955 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
958 for (i = 0; i < nb_slices; i++) {
959 if (init_duplicate_context(s->thread_context[i], s) < 0)
961 s->thread_context[i]->start_mb_y =
962 (s->mb_height * (i) + nb_slices / 2) / nb_slices;
963 s->thread_context[i]->end_mb_y =
964 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
967 if (init_duplicate_context(s, s) < 0)
970 s->end_mb_y = s->mb_height;
972 s->slice_context_count = nb_slices;
977 ff_MPV_common_end(s);
982 * Frees and resets MpegEncContext fields depending on the resolution.
983 * Is used during resolution changes to avoid a full reinitialization of the
986 static int free_context_frame(MpegEncContext *s)
990 av_freep(&s->mb_type);
991 av_freep(&s->p_mv_table_base);
992 av_freep(&s->b_forw_mv_table_base);
993 av_freep(&s->b_back_mv_table_base);
994 av_freep(&s->b_bidir_forw_mv_table_base);
995 av_freep(&s->b_bidir_back_mv_table_base);
996 av_freep(&s->b_direct_mv_table_base);
997 s->p_mv_table = NULL;
998 s->b_forw_mv_table = NULL;
999 s->b_back_mv_table = NULL;
1000 s->b_bidir_forw_mv_table = NULL;
1001 s->b_bidir_back_mv_table = NULL;
1002 s->b_direct_mv_table = NULL;
1003 for (i = 0; i < 2; i++) {
1004 for (j = 0; j < 2; j++) {
1005 for (k = 0; k < 2; k++) {
1006 av_freep(&s->b_field_mv_table_base[i][j][k]);
1007 s->b_field_mv_table[i][j][k] = NULL;
1009 av_freep(&s->b_field_select_table[i][j]);
1010 av_freep(&s->p_field_mv_table_base[i][j]);
1011 s->p_field_mv_table[i][j] = NULL;
1013 av_freep(&s->p_field_select_table[i]);
1016 av_freep(&s->dc_val_base);
1017 av_freep(&s->coded_block_base);
1018 av_freep(&s->mbintra_table);
1019 av_freep(&s->cbp_table);
1020 av_freep(&s->pred_dir_table);
1022 av_freep(&s->mbskip_table);
1024 av_freep(&s->error_status_table);
1025 av_freep(&s->er_temp_buffer);
1026 av_freep(&s->mb_index2xy);
1027 av_freep(&s->lambda_table);
1029 av_freep(&s->cplx_tab);
1030 av_freep(&s->bits_tab);
1032 s->linesize = s->uvlinesize = 0;
1034 for (i = 0; i < 3; i++)
1035 av_freep(&s->visualization_buffer[i]);
1040 int ff_MPV_common_frame_size_change(MpegEncContext *s)
1044 if (s->slice_context_count > 1) {
1045 for (i = 0; i < s->slice_context_count; i++) {
1046 free_duplicate_context(s->thread_context[i]);
1048 for (i = 1; i < s->slice_context_count; i++) {
1049 av_freep(&s->thread_context[i]);
1052 free_duplicate_context(s);
1054 free_context_frame(s);
1057 for (i = 0; i < s->picture_count; i++) {
1058 s->picture[i].needs_realloc = 1;
1061 s->last_picture_ptr =
1062 s->next_picture_ptr =
1063 s->current_picture_ptr = NULL;
1066 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
1067 s->mb_height = (s->height + 31) / 32 * 2;
1068 else if (s->codec_id != AV_CODEC_ID_H264)
1069 s->mb_height = (s->height + 15) / 16;
1071 if ((s->width || s->height) &&
1072 av_image_check_size(s->width, s->height, 0, s->avctx))
1073 return AVERROR_INVALIDDATA;
1075 if ((err = init_context_frame(s)))
1078 s->thread_context[0] = s;
1080 if (s->width && s->height) {
1081 int nb_slices = s->slice_context_count;
1082 if (nb_slices > 1) {
1083 for (i = 1; i < nb_slices; i++) {
1084 s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
1085 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
1088 for (i = 0; i < nb_slices; i++) {
1089 if (init_duplicate_context(s->thread_context[i], s) < 0)
1091 s->thread_context[i]->start_mb_y =
1092 (s->mb_height * (i) + nb_slices / 2) / nb_slices;
1093 s->thread_context[i]->end_mb_y =
1094 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
1097 if (init_duplicate_context(s, s) < 0)
1100 s->end_mb_y = s->mb_height;
1102 s->slice_context_count = nb_slices;
1107 ff_MPV_common_end(s);
1111 /* init common structure for both encoder and decoder */
1112 void ff_MPV_common_end(MpegEncContext *s)
1116 if (s->slice_context_count > 1) {
1117 for (i = 0; i < s->slice_context_count; i++) {
1118 free_duplicate_context(s->thread_context[i]);
1120 for (i = 1; i < s->slice_context_count; i++) {
1121 av_freep(&s->thread_context[i]);
1123 s->slice_context_count = 1;
1124 } else free_duplicate_context(s);
1126 av_freep(&s->parse_context.buffer);
1127 s->parse_context.buffer_size = 0;
1129 av_freep(&s->bitstream_buffer);
1130 s->allocated_bitstream_buffer_size = 0;
1132 av_freep(&s->avctx->stats_out);
1133 av_freep(&s->ac_stats);
1135 if(s->q_chroma_intra_matrix != s->q_intra_matrix ) av_freep(&s->q_chroma_intra_matrix);
1136 if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
1137 s->q_chroma_intra_matrix= NULL;
1138 s->q_chroma_intra_matrix16= NULL;
1139 av_freep(&s->q_intra_matrix);
1140 av_freep(&s->q_inter_matrix);
1141 av_freep(&s->q_intra_matrix16);
1142 av_freep(&s->q_inter_matrix16);
1143 av_freep(&s->input_picture);
1144 av_freep(&s->reordered_input_picture);
1145 av_freep(&s->dct_offset);
1147 if (s->picture && !s->avctx->internal->is_copy) {
1148 for (i = 0; i < s->picture_count; i++) {
1149 free_picture(s, &s->picture[i]);
1152 av_freep(&s->picture);
1154 free_context_frame(s);
1156 if (!(s->avctx->active_thread_type & FF_THREAD_FRAME))
1157 avcodec_default_free_buffers(s->avctx);
1159 s->context_initialized = 0;
1160 s->last_picture_ptr =
1161 s->next_picture_ptr =
1162 s->current_picture_ptr = NULL;
1163 s->linesize = s->uvlinesize = 0;
1166 void ff_init_rl(RLTable *rl,
1167 uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
1169 int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
1170 uint8_t index_run[MAX_RUN + 1];
1171 int last, run, level, start, end, i;
1173 /* If table is static, we can quit if rl->max_level[0] is not NULL */
1174 if (static_store && rl->max_level[0])
1177 /* compute max_level[], max_run[] and index_run[] */
1178 for (last = 0; last < 2; last++) {
1187 memset(max_level, 0, MAX_RUN + 1);
1188 memset(max_run, 0, MAX_LEVEL + 1);
1189 memset(index_run, rl->n, MAX_RUN + 1);
1190 for (i = start; i < end; i++) {
1191 run = rl->table_run[i];
1192 level = rl->table_level[i];
1193 if (index_run[run] == rl->n)
1195 if (level > max_level[run])
1196 max_level[run] = level;
1197 if (run > max_run[level])
1198 max_run[level] = run;
1201 rl->max_level[last] = static_store[last];
1203 rl->max_level[last] = av_malloc(MAX_RUN + 1);
1204 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
1206 rl->max_run[last] = static_store[last] + MAX_RUN + 1;
1208 rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
1209 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
1211 rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
1213 rl->index_run[last] = av_malloc(MAX_RUN + 1);
1214 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
1218 void ff_init_vlc_rl(RLTable *rl)
1222 for (q = 0; q < 32; q++) {
1224 int qadd = (q - 1) | 1;
1230 for (i = 0; i < rl->vlc.table_size; i++) {
1231 int code = rl->vlc.table[i][0];
1232 int len = rl->vlc.table[i][1];
1235 if (len == 0) { // illegal code
1238 } else if (len < 0) { // more bits needed
1242 if (code == rl->n) { // esc
1246 run = rl->table_run[code] + 1;
1247 level = rl->table_level[code] * qmul + qadd;
1248 if (code >= rl->last) run += 192;
1251 rl->rl_vlc[q][i].len = len;
1252 rl->rl_vlc[q][i].level = level;
1253 rl->rl_vlc[q][i].run = run;
1258 void ff_release_unused_pictures(MpegEncContext*s, int remove_current)
1262 /* release non reference frames */
1263 for (i = 0; i < s->picture_count; i++) {
1264 if (s->picture[i].f.data[0] && !s->picture[i].f.reference &&
1265 (!s->picture[i].owner2 || s->picture[i].owner2 == s) &&
1266 (remove_current || &s->picture[i] != s->current_picture_ptr)
1267 /* && s->picture[i].type!= FF_BUFFER_TYPE_SHARED */) {
1268 free_frame_buffer(s, &s->picture[i]);
1273 static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
1275 if ( (s->avctx->active_thread_type & FF_THREAD_FRAME)
1276 && pic->f.qscale_table //check if the frame has anything allocated
1277 && pic->period_since_free < s->avctx->thread_count)
1279 if (pic->f.data[0] == NULL)
1281 if (pic->needs_realloc && !(pic->f.reference & DELAYED_PIC_REF))
1282 if (!pic->owner2 || pic->owner2 == s)
1287 static int find_unused_picture(MpegEncContext *s, int shared)
1292 for (i = s->picture_range_start; i < s->picture_range_end; i++) {
1293 if (s->picture[i].f.data[0] == NULL && s->picture[i].f.type == 0)
1297 for (i = s->picture_range_start; i < s->picture_range_end; i++) {
1298 if (pic_is_unused(s, &s->picture[i]) && s->picture[i].f.type != 0)
1301 for (i = s->picture_range_start; i < s->picture_range_end; i++) {
1302 if (pic_is_unused(s, &s->picture[i]))
1307 av_log(s->avctx, AV_LOG_FATAL,
1308 "Internal error, picture buffer overflow\n");
1309 /* We could return -1, but the codec would crash trying to draw into a
1310 * non-existing frame anyway. This is safer than waiting for a random crash.
1311 * Also the return of this is never useful, an encoder must only allocate
1312 * as much as allowed in the specification. This has no relationship to how
1313 * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
1314 * enough for such valid streams).
1315 * Plus, a decoder has to check stream validity and remove frames if too
1316 * many reference frames are around. Waiting for "OOM" is not correct at
1317 * all. Similarly, missing reference frames have to be replaced by
1318 * interpolated/MC frames, anything else is a bug in the codec ...
1324 int ff_find_unused_picture(MpegEncContext *s, int shared)
1326 int ret = find_unused_picture(s, shared);
1328 if (ret >= 0 && ret < s->picture_range_end) {
1329 if (s->picture[ret].needs_realloc) {
1330 s->picture[ret].needs_realloc = 0;
1331 free_picture(s, &s->picture[ret]);
1332 avcodec_get_frame_defaults(&s->picture[ret].f);
1338 static void update_noise_reduction(MpegEncContext *s)
1342 for (intra = 0; intra < 2; intra++) {
1343 if (s->dct_count[intra] > (1 << 16)) {
1344 for (i = 0; i < 64; i++) {
1345 s->dct_error_sum[intra][i] >>= 1;
1347 s->dct_count[intra] >>= 1;
1350 for (i = 0; i < 64; i++) {
1351 s->dct_offset[intra][i] = (s->avctx->noise_reduction *
1352 s->dct_count[intra] +
1353 s->dct_error_sum[intra][i] / 2) /
1354 (s->dct_error_sum[intra][i] + 1);
1360 * generic function for encode/decode called after coding/decoding
1361 * the header and before a frame is coded/decoded.
1363 int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
1369 if (!ff_thread_can_start_frame(avctx)) {
1370 av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
1374 /* mark & release old frames */
1375 if (s->out_format != FMT_H264 || s->codec_id == AV_CODEC_ID_SVQ3) {
1376 if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1377 s->last_picture_ptr != s->next_picture_ptr &&
1378 s->last_picture_ptr->f.data[0]) {
1379 if (s->last_picture_ptr->owner2 == s)
1380 free_frame_buffer(s, s->last_picture_ptr);
1383 /* release forgotten pictures */
1384 /* if (mpeg124/h263) */
1386 for (i = 0; i < s->picture_count; i++) {
1387 if (s->picture[i].owner2 == s && s->picture[i].f.data[0] &&
1388 &s->picture[i] != s->last_picture_ptr &&
1389 &s->picture[i] != s->next_picture_ptr &&
1390 s->picture[i].f.reference && !s->picture[i].needs_realloc) {
1391 if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1392 av_log(avctx, AV_LOG_ERROR,
1393 "releasing zombie picture\n");
1394 free_frame_buffer(s, &s->picture[i]);
1401 ff_release_unused_pictures(s, 1);
1403 if (s->current_picture_ptr &&
1404 s->current_picture_ptr->f.data[0] == NULL) {
1405 // we already have a unused image
1406 // (maybe it was set before reading the header)
1407 pic = s->current_picture_ptr;
1409 i = ff_find_unused_picture(s, 0);
1411 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1414 pic = &s->picture[i];
1417 pic->f.reference = 0;
1418 if (!s->droppable) {
1419 if (s->codec_id == AV_CODEC_ID_H264)
1420 pic->f.reference = s->picture_structure;
1421 else if (s->pict_type != AV_PICTURE_TYPE_B)
1422 pic->f.reference = 3;
1425 pic->f.coded_picture_number = s->coded_picture_number++;
1427 if (ff_alloc_picture(s, pic, 0) < 0)
1430 s->current_picture_ptr = pic;
1431 // FIXME use only the vars from current_pic
1432 s->current_picture_ptr->f.top_field_first = s->top_field_first;
1433 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
1434 s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1435 if (s->picture_structure != PICT_FRAME)
1436 s->current_picture_ptr->f.top_field_first =
1437 (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
1439 s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame &&
1440 !s->progressive_sequence;
1441 s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
1444 s->current_picture_ptr->f.pict_type = s->pict_type;
1445 // if (s->flags && CODEC_FLAG_QSCALE)
1446 // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
1447 s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1449 ff_copy_picture(&s->current_picture, s->current_picture_ptr);
1451 if (s->pict_type != AV_PICTURE_TYPE_B) {
1452 s->last_picture_ptr = s->next_picture_ptr;
1454 s->next_picture_ptr = s->current_picture_ptr;
1456 av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
1457 s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
1458 s->last_picture_ptr ? s->last_picture_ptr->f.data[0] : NULL,
1459 s->next_picture_ptr ? s->next_picture_ptr->f.data[0] : NULL,
1460 s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
1461 s->pict_type, s->droppable);
1463 if (s->codec_id != AV_CODEC_ID_H264) {
1464 if ((s->last_picture_ptr == NULL ||
1465 s->last_picture_ptr->f.data[0] == NULL) &&
1466 (s->pict_type != AV_PICTURE_TYPE_I ||
1467 s->picture_structure != PICT_FRAME)) {
1468 if (s->pict_type != AV_PICTURE_TYPE_I)
1469 av_log(avctx, AV_LOG_ERROR,
1470 "warning: first frame is no keyframe\n");
1471 else if (s->picture_structure != PICT_FRAME)
1472 av_log(avctx, AV_LOG_INFO,
1473 "allocate dummy last picture for field based first keyframe\n");
1475 /* Allocate a dummy frame */
1476 i = ff_find_unused_picture(s, 0);
1478 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1481 s->last_picture_ptr = &s->picture[i];
1482 s->last_picture_ptr->f.key_frame = 0;
1483 if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
1484 s->last_picture_ptr = NULL;
1488 if(s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263){
1489 for(i=0; i<avctx->height; i++)
1490 memset(s->last_picture_ptr->f.data[0] + s->last_picture_ptr->f.linesize[0]*i, 16, avctx->width);
1493 ff_thread_report_progress(&s->last_picture_ptr->f, INT_MAX, 0);
1494 ff_thread_report_progress(&s->last_picture_ptr->f, INT_MAX, 1);
1495 s->last_picture_ptr->f.reference = 3;
1497 if ((s->next_picture_ptr == NULL ||
1498 s->next_picture_ptr->f.data[0] == NULL) &&
1499 s->pict_type == AV_PICTURE_TYPE_B) {
1500 /* Allocate a dummy frame */
1501 i = ff_find_unused_picture(s, 0);
1503 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1506 s->next_picture_ptr = &s->picture[i];
1507 s->next_picture_ptr->f.key_frame = 0;
1508 if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
1509 s->next_picture_ptr = NULL;
1512 ff_thread_report_progress(&s->next_picture_ptr->f, INT_MAX, 0);
1513 ff_thread_report_progress(&s->next_picture_ptr->f, INT_MAX, 1);
1514 s->next_picture_ptr->f.reference = 3;
1518 memset(s->last_picture.f.data, 0, sizeof(s->last_picture.f.data));
1519 memset(s->next_picture.f.data, 0, sizeof(s->next_picture.f.data));
1520 if (s->last_picture_ptr)
1521 ff_copy_picture(&s->last_picture, s->last_picture_ptr);
1522 if (s->next_picture_ptr)
1523 ff_copy_picture(&s->next_picture, s->next_picture_ptr);
1525 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME)) {
1526 if (s->next_picture_ptr)
1527 s->next_picture_ptr->owner2 = s;
1528 if (s->last_picture_ptr)
1529 s->last_picture_ptr->owner2 = s;
1532 assert(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
1533 s->last_picture_ptr->f.data[0]));
1535 if (s->picture_structure!= PICT_FRAME && s->out_format != FMT_H264) {
1537 for (i = 0; i < 4; i++) {
1538 if (s->picture_structure == PICT_BOTTOM_FIELD) {
1539 s->current_picture.f.data[i] +=
1540 s->current_picture.f.linesize[i];
1542 s->current_picture.f.linesize[i] *= 2;
1543 s->last_picture.f.linesize[i] *= 2;
1544 s->next_picture.f.linesize[i] *= 2;
1548 s->err_recognition = avctx->err_recognition;
1550 /* set dequantizer, we can't do it during init as
1551 * it might change for mpeg4 and we can't do it in the header
1552 * decode as init is not called for mpeg4 there yet */
1553 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1554 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
1555 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
1556 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
1557 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
1558 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
1560 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
1561 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
1564 if (s->dct_error_sum) {
1565 assert(s->avctx->noise_reduction && s->encoding);
1566 update_noise_reduction(s);
1569 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1570 return ff_xvmc_field_start(s, avctx);
1575 /* generic function for encode/decode called after a
1576 * frame has been coded/decoded. */
1577 void ff_MPV_frame_end(MpegEncContext *s)
1580 /* redraw edges for the frame if decoding didn't complete */
1581 // just to make sure that all data is rendered.
1582 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {
1583 ff_xvmc_field_end(s);
1584 } else if((s->error_count || s->encoding || !(s->avctx->codec->capabilities&CODEC_CAP_DRAW_HORIZ_BAND)) &&
1585 !s->avctx->hwaccel &&
1586 !(s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) &&
1587 s->unrestricted_mv &&
1588 s->current_picture.f.reference &&
1590 !(s->flags & CODEC_FLAG_EMU_EDGE) &&
1593 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
1594 int hshift = desc->log2_chroma_w;
1595 int vshift = desc->log2_chroma_h;
1596 s->dsp.draw_edges(s->current_picture.f.data[0], s->current_picture.f.linesize[0],
1597 s->h_edge_pos, s->v_edge_pos,
1598 EDGE_WIDTH, EDGE_WIDTH,
1599 EDGE_TOP | EDGE_BOTTOM);
1600 s->dsp.draw_edges(s->current_picture.f.data[1], s->current_picture.f.linesize[1],
1601 s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
1602 EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
1603 EDGE_TOP | EDGE_BOTTOM);
1604 s->dsp.draw_edges(s->current_picture.f.data[2], s->current_picture.f.linesize[2],
1605 s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
1606 EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
1607 EDGE_TOP | EDGE_BOTTOM);
1612 s->last_pict_type = s->pict_type;
1613 s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f.quality;
1614 if (s->pict_type!= AV_PICTURE_TYPE_B) {
1615 s->last_non_b_pict_type = s->pict_type;
1618 /* copy back current_picture variables */
1619 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1620 if (s->picture[i].f.data[0] == s->current_picture.f.data[0]) {
1621 s->picture[i] = s->current_picture;
1625 assert(i < MAX_PICTURE_COUNT);
1629 /* release non-reference frames */
1630 for (i = 0; i < s->picture_count; i++) {
1631 if (s->picture[i].f.data[0] && !s->picture[i].f.reference
1632 /* && s->picture[i].type != FF_BUFFER_TYPE_SHARED */) {
1633 free_frame_buffer(s, &s->picture[i]);
1637 // clear copies, to avoid confusion
1639 memset(&s->last_picture, 0, sizeof(Picture));
1640 memset(&s->next_picture, 0, sizeof(Picture));
1641 memset(&s->current_picture, 0, sizeof(Picture));
1643 s->avctx->coded_frame = &s->current_picture_ptr->f;
1645 if (s->codec_id != AV_CODEC_ID_H264 && s->current_picture.f.reference) {
1646 ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX, 0);
1651 * Draw a line from (ex, ey) -> (sx, sy).
1652 * @param w width of the image
1653 * @param h height of the image
1654 * @param stride stride/linesize of the image
1655 * @param color color of the arrow
1657 static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
1658 int w, int h, int stride, int color)
1662 sx = av_clip(sx, 0, w - 1);
1663 sy = av_clip(sy, 0, h - 1);
1664 ex = av_clip(ex, 0, w - 1);
1665 ey = av_clip(ey, 0, h - 1);
1667 buf[sy * stride + sx] += color;
1669 if (FFABS(ex - sx) > FFABS(ey - sy)) {
1671 FFSWAP(int, sx, ex);
1672 FFSWAP(int, sy, ey);
1674 buf += sx + sy * stride;
1676 f = ((ey - sy) << 16) / ex;
1677 for (x = 0; x <= ex; x++) {
1679 fr = (x * f) & 0xFFFF;
1680 buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
1681 if(fr) buf[(y + 1) * stride + x] += (color * fr ) >> 16;
1685 FFSWAP(int, sx, ex);
1686 FFSWAP(int, sy, ey);
1688 buf += sx + sy * stride;
1691 f = ((ex - sx) << 16) / ey;
1694 for(y= 0; y <= ey; y++){
1696 fr = (y*f) & 0xFFFF;
1697 buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
1698 if(fr) buf[y * stride + x + 1] += (color * fr ) >> 16;
1704 * Draw an arrow from (ex, ey) -> (sx, sy).
1705 * @param w width of the image
1706 * @param h height of the image
1707 * @param stride stride/linesize of the image
1708 * @param color color of the arrow
1710 static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
1711 int ey, int w, int h, int stride, int color)
1715 sx = av_clip(sx, -100, w + 100);
1716 sy = av_clip(sy, -100, h + 100);
1717 ex = av_clip(ex, -100, w + 100);
1718 ey = av_clip(ey, -100, h + 100);
1723 if (dx * dx + dy * dy > 3 * 3) {
1726 int length = ff_sqrt((rx * rx + ry * ry) << 8);
1728 // FIXME subpixel accuracy
1729 rx = ROUNDED_DIV(rx * 3 << 4, length);
1730 ry = ROUNDED_DIV(ry * 3 << 4, length);
1732 draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
1733 draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
1735 draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
1739 * Print debugging info for the given picture.
1741 void ff_print_debug_info(MpegEncContext *s, AVFrame *pict)
1743 if ( s->avctx->hwaccel || !pict || !pict->mb_type
1744 || (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU))
1748 if (s->avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
1751 av_log(s->avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
1752 av_get_picture_type_char(pict->pict_type));
1753 for (y = 0; y < s->mb_height; y++) {
1754 for (x = 0; x < s->mb_width; x++) {
1755 if (s->avctx->debug & FF_DEBUG_SKIP) {
1756 int count = s->mbskip_table[x + y * s->mb_stride];
1759 av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
1761 if (s->avctx->debug & FF_DEBUG_QP) {
1762 av_log(s->avctx, AV_LOG_DEBUG, "%2d",
1763 pict->qscale_table[x + y * s->mb_stride]);
1765 if (s->avctx->debug & FF_DEBUG_MB_TYPE) {
1766 int mb_type = pict->mb_type[x + y * s->mb_stride];
1767 // Type & MV direction
1768 if (IS_PCM(mb_type))
1769 av_log(s->avctx, AV_LOG_DEBUG, "P");
1770 else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
1771 av_log(s->avctx, AV_LOG_DEBUG, "A");
1772 else if (IS_INTRA4x4(mb_type))
1773 av_log(s->avctx, AV_LOG_DEBUG, "i");
1774 else if (IS_INTRA16x16(mb_type))
1775 av_log(s->avctx, AV_LOG_DEBUG, "I");
1776 else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
1777 av_log(s->avctx, AV_LOG_DEBUG, "d");
1778 else if (IS_DIRECT(mb_type))
1779 av_log(s->avctx, AV_LOG_DEBUG, "D");
1780 else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
1781 av_log(s->avctx, AV_LOG_DEBUG, "g");
1782 else if (IS_GMC(mb_type))
1783 av_log(s->avctx, AV_LOG_DEBUG, "G");
1784 else if (IS_SKIP(mb_type))
1785 av_log(s->avctx, AV_LOG_DEBUG, "S");
1786 else if (!USES_LIST(mb_type, 1))
1787 av_log(s->avctx, AV_LOG_DEBUG, ">");
1788 else if (!USES_LIST(mb_type, 0))
1789 av_log(s->avctx, AV_LOG_DEBUG, "<");
1791 av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
1792 av_log(s->avctx, AV_LOG_DEBUG, "X");
1796 if (IS_8X8(mb_type))
1797 av_log(s->avctx, AV_LOG_DEBUG, "+");
1798 else if (IS_16X8(mb_type))
1799 av_log(s->avctx, AV_LOG_DEBUG, "-");
1800 else if (IS_8X16(mb_type))
1801 av_log(s->avctx, AV_LOG_DEBUG, "|");
1802 else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
1803 av_log(s->avctx, AV_LOG_DEBUG, " ");
1805 av_log(s->avctx, AV_LOG_DEBUG, "?");
1808 if (IS_INTERLACED(mb_type))
1809 av_log(s->avctx, AV_LOG_DEBUG, "=");
1811 av_log(s->avctx, AV_LOG_DEBUG, " ");
1814 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1818 if ((s->avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
1819 (s->avctx->debug_mv)) {
1820 const int shift = 1 + s->quarter_sample;
1824 int h_chroma_shift, v_chroma_shift, block_height;
1825 const int width = s->avctx->width;
1826 const int height = s->avctx->height;
1827 const int mv_sample_log2 = 4 - pict->motion_subsample_log2;
1828 const int mv_stride = (s->mb_width << mv_sample_log2) +
1829 (s->codec_id == AV_CODEC_ID_H264 ? 0 : 1);
1830 s->low_delay = 0; // needed to see the vectors without trashing the buffers
1832 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
1834 for (i = 0; i < 3; i++) {
1835 size_t size= (i == 0) ? pict->linesize[i] * FFALIGN(height, 16):
1836 pict->linesize[i] * FFALIGN(height, 16) >> v_chroma_shift;
1837 s->visualization_buffer[i]= av_realloc(s->visualization_buffer[i], size);
1838 memcpy(s->visualization_buffer[i], pict->data[i], size);
1839 pict->data[i] = s->visualization_buffer[i];
1841 pict->type = FF_BUFFER_TYPE_COPY;
1843 ptr = pict->data[0];
1844 block_height = 16 >> v_chroma_shift;
1846 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1848 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1849 const int mb_index = mb_x + mb_y * s->mb_stride;
1850 if ((s->avctx->debug_mv) && pict->motion_val[0]) {
1852 for (type = 0; type < 3; type++) {
1856 if ((!(s->avctx->debug_mv & FF_DEBUG_VIS_MV_P_FOR)) ||
1857 (pict->pict_type!= AV_PICTURE_TYPE_P))
1862 if ((!(s->avctx->debug_mv & FF_DEBUG_VIS_MV_B_FOR)) ||
1863 (pict->pict_type!= AV_PICTURE_TYPE_B))
1868 if ((!(s->avctx->debug_mv & FF_DEBUG_VIS_MV_B_BACK)) ||
1869 (pict->pict_type!= AV_PICTURE_TYPE_B))
1874 if (!USES_LIST(pict->mb_type[mb_index], direction))
1877 if (IS_8X8(pict->mb_type[mb_index])) {
1879 for (i = 0; i < 4; i++) {
1880 int sx = mb_x * 16 + 4 + 8 * (i & 1);
1881 int sy = mb_y * 16 + 4 + 8 * (i >> 1);
1882 int xy = (mb_x * 2 + (i & 1) +
1883 (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
1884 int mx = (pict->motion_val[direction][xy][0] >> shift) + sx;
1885 int my = (pict->motion_val[direction][xy][1] >> shift) + sy;
1886 draw_arrow(ptr, sx, sy, mx, my, width,
1887 height, s->linesize, 100);
1889 } else if (IS_16X8(pict->mb_type[mb_index])) {
1891 for (i = 0; i < 2; i++) {
1892 int sx = mb_x * 16 + 8;
1893 int sy = mb_y * 16 + 4 + 8 * i;
1894 int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
1895 int mx = (pict->motion_val[direction][xy][0] >> shift);
1896 int my = (pict->motion_val[direction][xy][1] >> shift);
1898 if (IS_INTERLACED(pict->mb_type[mb_index]))
1901 draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
1902 height, s->linesize, 100);
1904 } else if (IS_8X16(pict->mb_type[mb_index])) {
1906 for (i = 0; i < 2; i++) {
1907 int sx = mb_x * 16 + 4 + 8 * i;
1908 int sy = mb_y * 16 + 8;
1909 int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
1910 int mx = pict->motion_val[direction][xy][0] >> shift;
1911 int my = pict->motion_val[direction][xy][1] >> shift;
1913 if (IS_INTERLACED(pict->mb_type[mb_index]))
1916 draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
1917 height, s->linesize, 100);
1920 int sx= mb_x * 16 + 8;
1921 int sy= mb_y * 16 + 8;
1922 int xy= (mb_x + mb_y * mv_stride) << mv_sample_log2;
1923 int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
1924 int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
1925 draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
1929 if ((s->avctx->debug & FF_DEBUG_VIS_QP)) {
1930 uint64_t c = (pict->qscale_table[mb_index] * 128 / 31) *
1931 0x0101010101010101ULL;
1933 for (y = 0; y < block_height; y++) {
1934 *(uint64_t *)(pict->data[1] + 8 * mb_x +
1935 (block_height * mb_y + y) *
1936 pict->linesize[1]) = c;
1937 *(uint64_t *)(pict->data[2] + 8 * mb_x +
1938 (block_height * mb_y + y) *
1939 pict->linesize[2]) = c;
1942 if ((s->avctx->debug & FF_DEBUG_VIS_MB_TYPE) &&
1943 pict->motion_val[0]) {
1944 int mb_type = pict->mb_type[mb_index];
1947 #define COLOR(theta, r) \
1948 u = (int)(128 + r * cos(theta * 3.141592 / 180)); \
1949 v = (int)(128 + r * sin(theta * 3.141592 / 180));
1953 if (IS_PCM(mb_type)) {
1955 } else if ((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) ||
1956 IS_INTRA16x16(mb_type)) {
1958 } else if (IS_INTRA4x4(mb_type)) {
1960 } else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) {
1962 } else if (IS_DIRECT(mb_type)) {
1964 } else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) {
1966 } else if (IS_GMC(mb_type)) {
1968 } else if (IS_SKIP(mb_type)) {
1970 } else if (!USES_LIST(mb_type, 1)) {
1972 } else if (!USES_LIST(mb_type, 0)) {
1975 av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
1979 u *= 0x0101010101010101ULL;
1980 v *= 0x0101010101010101ULL;
1981 for (y = 0; y < block_height; y++) {
1982 *(uint64_t *)(pict->data[1] + 8 * mb_x +
1983 (block_height * mb_y + y) * pict->linesize[1]) = u;
1984 *(uint64_t *)(pict->data[2] + 8 * mb_x +
1985 (block_height * mb_y + y) * pict->linesize[2]) = v;
1989 if (IS_8X8(mb_type) || IS_16X8(mb_type)) {
1990 *(uint64_t *)(pict->data[0] + 16 * mb_x + 0 +
1991 (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
1992 *(uint64_t *)(pict->data[0] + 16 * mb_x + 8 +
1993 (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
1995 if (IS_8X8(mb_type) || IS_8X16(mb_type)) {
1996 for (y = 0; y < 16; y++)
1997 pict->data[0][16 * mb_x + 8 + (16 * mb_y + y) *
1998 pict->linesize[0]] ^= 0x80;
2000 if (IS_8X8(mb_type) && mv_sample_log2 >= 2) {
2001 int dm = 1 << (mv_sample_log2 - 2);
2002 for (i = 0; i < 4; i++) {
2003 int sx = mb_x * 16 + 8 * (i & 1);
2004 int sy = mb_y * 16 + 8 * (i >> 1);
2005 int xy = (mb_x * 2 + (i & 1) +
2006 (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
2008 int32_t *mv = (int32_t *) &pict->motion_val[0][xy];
2009 if (mv[0] != mv[dm] ||
2010 mv[dm * mv_stride] != mv[dm * (mv_stride + 1)])
2011 for (y = 0; y < 8; y++)
2012 pict->data[0][sx + 4 + (sy + y) * pict->linesize[0]] ^= 0x80;
2013 if (mv[0] != mv[dm * mv_stride] || mv[dm] != mv[dm * (mv_stride + 1)])
2014 *(uint64_t *)(pict->data[0] + sx + (sy + 4) *
2015 pict->linesize[0]) ^= 0x8080808080808080ULL;
2019 if (IS_INTERLACED(mb_type) &&
2020 s->codec_id == AV_CODEC_ID_H264) {
2024 s->mbskip_table[mb_index] = 0;
2030 static inline int hpel_motion_lowres(MpegEncContext *s,
2031 uint8_t *dest, uint8_t *src,
2032 int field_based, int field_select,
2033 int src_x, int src_y,
2034 int width, int height, int stride,
2035 int h_edge_pos, int v_edge_pos,
2036 int w, int h, h264_chroma_mc_func *pix_op,
2037 int motion_x, int motion_y)
2039 const int lowres = s->avctx->lowres;
2040 const int op_index = FFMIN(lowres, 2);
2041 const int s_mask = (2 << lowres) - 1;
2045 if (s->quarter_sample) {
2050 sx = motion_x & s_mask;
2051 sy = motion_y & s_mask;
2052 src_x += motion_x >> lowres + 1;
2053 src_y += motion_y >> lowres + 1;
2055 src += src_y * stride + src_x;
2057 if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
2058 (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
2059 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w + 1,
2060 (h + 1) << field_based, src_x,
2061 src_y << field_based,
2064 src = s->edge_emu_buffer;
2068 sx = (sx << 2) >> lowres;
2069 sy = (sy << 2) >> lowres;
2072 pix_op[op_index](dest, src, stride, h, sx, sy);
2076 /* apply one mpeg motion vector to the three components */
2077 static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
2084 uint8_t **ref_picture,
2085 h264_chroma_mc_func *pix_op,
2086 int motion_x, int motion_y,
2089 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2090 int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy,
2092 const int lowres = s->avctx->lowres;
2093 const int op_index = FFMIN(lowres-1+s->chroma_x_shift, 2);
2094 const int block_s = 8>>lowres;
2095 const int s_mask = (2 << lowres) - 1;
2096 const int h_edge_pos = s->h_edge_pos >> lowres;
2097 const int v_edge_pos = s->v_edge_pos >> lowres;
2098 linesize = s->current_picture.f.linesize[0] << field_based;
2099 uvlinesize = s->current_picture.f.linesize[1] << field_based;
2101 // FIXME obviously not perfect but qpel will not work in lowres anyway
2102 if (s->quarter_sample) {
2108 motion_y += (bottom_field - field_select)*((1 << lowres)-1);
2111 sx = motion_x & s_mask;
2112 sy = motion_y & s_mask;
2113 src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
2114 src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
2116 if (s->out_format == FMT_H263) {
2117 uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
2118 uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
2119 uvsrc_x = src_x >> 1;
2120 uvsrc_y = src_y >> 1;
2121 } else if (s->out_format == FMT_H261) {
2122 // even chroma mv's are full pel in H261
2125 uvsx = (2 * mx) & s_mask;
2126 uvsy = (2 * my) & s_mask;
2127 uvsrc_x = s->mb_x * block_s + (mx >> lowres);
2128 uvsrc_y = mb_y * block_s + (my >> lowres);
2130 if(s->chroma_y_shift){
2135 uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
2136 uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
2138 if(s->chroma_x_shift){
2142 uvsy = motion_y & s_mask;
2144 uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
2147 uvsx = motion_x & s_mask;
2148 uvsy = motion_y & s_mask;
2155 ptr_y = ref_picture[0] + src_y * linesize + src_x;
2156 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
2157 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
2159 if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) ||
2160 (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
2161 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
2162 linesize >> field_based, 17, 17 + field_based,
2163 src_x, src_y << field_based, h_edge_pos,
2165 ptr_y = s->edge_emu_buffer;
2166 if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
2167 uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;
2168 s->vdsp.emulated_edge_mc(uvbuf , ptr_cb, uvlinesize >> field_based, 9,
2170 uvsrc_x, uvsrc_y << field_based,
2171 h_edge_pos >> 1, v_edge_pos >> 1);
2172 s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr, uvlinesize >> field_based, 9,
2174 uvsrc_x, uvsrc_y << field_based,
2175 h_edge_pos >> 1, v_edge_pos >> 1);
2177 ptr_cr = uvbuf + 16;
2181 // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f.data
2183 dest_y += s->linesize;
2184 dest_cb += s->uvlinesize;
2185 dest_cr += s->uvlinesize;
2189 ptr_y += s->linesize;
2190 ptr_cb += s->uvlinesize;
2191 ptr_cr += s->uvlinesize;
2194 sx = (sx << 2) >> lowres;
2195 sy = (sy << 2) >> lowres;
2196 pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
2198 if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
2199 uvsx = (uvsx << 2) >> lowres;
2200 uvsy = (uvsy << 2) >> lowres;
2201 if (h >> s->chroma_y_shift) {
2202 pix_op[op_index](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
2203 pix_op[op_index](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
2206 // FIXME h261 lowres loop filter
2209 static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
2210 uint8_t *dest_cb, uint8_t *dest_cr,
2211 uint8_t **ref_picture,
2212 h264_chroma_mc_func * pix_op,
2215 const int lowres = s->avctx->lowres;
2216 const int op_index = FFMIN(lowres, 2);
2217 const int block_s = 8 >> lowres;
2218 const int s_mask = (2 << lowres) - 1;
2219 const int h_edge_pos = s->h_edge_pos >> lowres + 1;
2220 const int v_edge_pos = s->v_edge_pos >> lowres + 1;
2221 int emu = 0, src_x, src_y, offset, sx, sy;
2224 if (s->quarter_sample) {
2229 /* In case of 8X8, we construct a single chroma motion vector
2230 with a special rounding */
2231 mx = ff_h263_round_chroma(mx);
2232 my = ff_h263_round_chroma(my);
2236 src_x = s->mb_x * block_s + (mx >> lowres + 1);
2237 src_y = s->mb_y * block_s + (my >> lowres + 1);
2239 offset = src_y * s->uvlinesize + src_x;
2240 ptr = ref_picture[1] + offset;
2241 if (s->flags & CODEC_FLAG_EMU_EDGE) {
2242 if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
2243 (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
2244 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
2245 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
2246 ptr = s->edge_emu_buffer;
2250 sx = (sx << 2) >> lowres;
2251 sy = (sy << 2) >> lowres;
2252 pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
2254 ptr = ref_picture[2] + offset;
2256 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9,
2257 src_x, src_y, h_edge_pos, v_edge_pos);
2258 ptr = s->edge_emu_buffer;
2260 pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
2264 * motion compensation of a single macroblock
2266 * @param dest_y luma destination pointer
2267 * @param dest_cb chroma cb/u destination pointer
2268 * @param dest_cr chroma cr/v destination pointer
2269 * @param dir direction (0->forward, 1->backward)
2270 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
2271 * @param pix_op halfpel motion compensation function (average or put normally)
2272 * the motion vectors are taken from s->mv and the MV type from s->mv_type
2274 static inline void MPV_motion_lowres(MpegEncContext *s,
2275 uint8_t *dest_y, uint8_t *dest_cb,
2277 int dir, uint8_t **ref_picture,
2278 h264_chroma_mc_func *pix_op)
2282 const int lowres = s->avctx->lowres;
2283 const int block_s = 8 >>lowres;
2288 switch (s->mv_type) {
2290 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2292 ref_picture, pix_op,
2293 s->mv[dir][0][0], s->mv[dir][0][1],
2299 for (i = 0; i < 4; i++) {
2300 hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
2301 s->linesize) * block_s,
2302 ref_picture[0], 0, 0,
2303 (2 * mb_x + (i & 1)) * block_s,
2304 (2 * mb_y + (i >> 1)) * block_s,
2305 s->width, s->height, s->linesize,
2306 s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
2307 block_s, block_s, pix_op,
2308 s->mv[dir][i][0], s->mv[dir][i][1]);
2310 mx += s->mv[dir][i][0];
2311 my += s->mv[dir][i][1];
2314 if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY))
2315 chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
2319 if (s->picture_structure == PICT_FRAME) {
2321 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2322 1, 0, s->field_select[dir][0],
2323 ref_picture, pix_op,
2324 s->mv[dir][0][0], s->mv[dir][0][1],
2327 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2328 1, 1, s->field_select[dir][1],
2329 ref_picture, pix_op,
2330 s->mv[dir][1][0], s->mv[dir][1][1],
2333 if (s->picture_structure != s->field_select[dir][0] + 1 &&
2334 s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
2335 ref_picture = s->current_picture_ptr->f.data;
2338 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2339 0, 0, s->field_select[dir][0],
2340 ref_picture, pix_op,
2342 s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
2346 for (i = 0; i < 2; i++) {
2347 uint8_t **ref2picture;
2349 if (s->picture_structure == s->field_select[dir][i] + 1 ||
2350 s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
2351 ref2picture = ref_picture;
2353 ref2picture = s->current_picture_ptr->f.data;
2356 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2357 0, 0, s->field_select[dir][i],
2358 ref2picture, pix_op,
2359 s->mv[dir][i][0], s->mv[dir][i][1] +
2360 2 * block_s * i, block_s, mb_y >> 1);
2362 dest_y += 2 * block_s * s->linesize;
2363 dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
2364 dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
2368 if (s->picture_structure == PICT_FRAME) {
2369 for (i = 0; i < 2; i++) {
2371 for (j = 0; j < 2; j++) {
2372 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2374 ref_picture, pix_op,
2375 s->mv[dir][2 * i + j][0],
2376 s->mv[dir][2 * i + j][1],
2379 pix_op = s->dsp.avg_h264_chroma_pixels_tab;
2382 for (i = 0; i < 2; i++) {
2383 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2384 0, 0, s->picture_structure != i + 1,
2385 ref_picture, pix_op,
2386 s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
2387 2 * block_s, mb_y >> 1);
2389 // after put we make avg of the same block
2390 pix_op = s->dsp.avg_h264_chroma_pixels_tab;
2392 // opposite parity is always in the same
2393 // frame if this is second field
2394 if (!s->first_field) {
2395 ref_picture = s->current_picture_ptr->f.data;
2406 * find the lowest MB row referenced in the MVs
2408 int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
2410 int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
2411 int my, off, i, mvs;
2413 if (s->picture_structure != PICT_FRAME || s->mcsel)
2416 switch (s->mv_type) {
2430 for (i = 0; i < mvs; i++) {
2431 my = s->mv[dir][i][1]<<qpel_shift;
2432 my_max = FFMAX(my_max, my);
2433 my_min = FFMIN(my_min, my);
2436 off = (FFMAX(-my_min, my_max) + 63) >> 6;
2438 return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
2440 return s->mb_height-1;
2443 /* put block[] to dest[] */
2444 static inline void put_dct(MpegEncContext *s,
2445 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
2447 s->dct_unquantize_intra(s, block, i, qscale);
2448 s->dsp.idct_put (dest, line_size, block);
2451 /* add block[] to dest[] */
2452 static inline void add_dct(MpegEncContext *s,
2453 int16_t *block, int i, uint8_t *dest, int line_size)
2455 if (s->block_last_index[i] >= 0) {
2456 s->dsp.idct_add (dest, line_size, block);
2460 static inline void add_dequant_dct(MpegEncContext *s,
2461 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
2463 if (s->block_last_index[i] >= 0) {
2464 s->dct_unquantize_inter(s, block, i, qscale);
2466 s->dsp.idct_add (dest, line_size, block);
2471 * Clean dc, ac, coded_block for the current non-intra MB.
2473 void ff_clean_intra_table_entries(MpegEncContext *s)
2475 int wrap = s->b8_stride;
2476 int xy = s->block_index[0];
2479 s->dc_val[0][xy + 1 ] =
2480 s->dc_val[0][xy + wrap] =
2481 s->dc_val[0][xy + 1 + wrap] = 1024;
2483 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
2484 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
2485 if (s->msmpeg4_version>=3) {
2486 s->coded_block[xy ] =
2487 s->coded_block[xy + 1 ] =
2488 s->coded_block[xy + wrap] =
2489 s->coded_block[xy + 1 + wrap] = 0;
2492 wrap = s->mb_stride;
2493 xy = s->mb_x + s->mb_y * wrap;
2495 s->dc_val[2][xy] = 1024;
2497 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
2498 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
2500 s->mbintra_table[xy]= 0;
2503 /* generic function called after a macroblock has been parsed by the
2504 decoder or after it has been encoded by the encoder.
2506 Important variables used:
2507 s->mb_intra : true if intra macroblock
2508 s->mv_dir : motion vector direction
2509 s->mv_type : motion vector type
2510 s->mv : motion vector
2511 s->interlaced_dct : true if interlaced dct used (mpeg2)
2513 static av_always_inline
2514 void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
2515 int lowres_flag, int is_mpeg12)
2517 const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
2518 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
2519 ff_xvmc_decode_mb(s);//xvmc uses pblocks
2523 if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
2524 /* save DCT coefficients */
2526 int16_t *dct = &s->current_picture.f.dct_coeff[mb_xy * 64 * 6];
2527 av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
2529 for(j=0; j<64; j++){
2530 *dct++ = block[i][s->dsp.idct_permutation[j]];
2531 av_log(s->avctx, AV_LOG_DEBUG, "%5d", dct[-1]);
2533 av_log(s->avctx, AV_LOG_DEBUG, "\n");
2537 s->current_picture.f.qscale_table[mb_xy] = s->qscale;
2539 /* update DC predictors for P macroblocks */
2541 if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
2542 if(s->mbintra_table[mb_xy])
2543 ff_clean_intra_table_entries(s);
2547 s->last_dc[2] = 128 << s->intra_dc_precision;
2550 else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
2551 s->mbintra_table[mb_xy]=1;
2553 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
2554 uint8_t *dest_y, *dest_cb, *dest_cr;
2555 int dct_linesize, dct_offset;
2556 op_pixels_func (*op_pix)[4];
2557 qpel_mc_func (*op_qpix)[16];
2558 const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
2559 const int uvlinesize = s->current_picture.f.linesize[1];
2560 const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
2561 const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
2563 /* avoid copy if macroblock skipped in last frame too */
2564 /* skip only during decoding as we might trash the buffers during encoding a bit */
2566 uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
2568 if (s->mb_skipped) {
2570 av_assert2(s->pict_type!=AV_PICTURE_TYPE_I);
2572 } else if(!s->current_picture.f.reference) {
2575 *mbskip_ptr = 0; /* not skipped */
2579 dct_linesize = linesize << s->interlaced_dct;
2580 dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
2584 dest_cb= s->dest[1];
2585 dest_cr= s->dest[2];
2587 dest_y = s->b_scratchpad;
2588 dest_cb= s->b_scratchpad+16*linesize;
2589 dest_cr= s->b_scratchpad+32*linesize;
2593 /* motion handling */
2594 /* decoding or more than one mb_type (MC was already done otherwise) */
2597 if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
2598 if (s->mv_dir & MV_DIR_FORWARD) {
2599 ff_thread_await_progress(&s->last_picture_ptr->f,
2600 ff_MPV_lowest_referenced_row(s, 0),
2603 if (s->mv_dir & MV_DIR_BACKWARD) {
2604 ff_thread_await_progress(&s->next_picture_ptr->f,
2605 ff_MPV_lowest_referenced_row(s, 1),
2611 h264_chroma_mc_func *op_pix = s->dsp.put_h264_chroma_pixels_tab;
2613 if (s->mv_dir & MV_DIR_FORWARD) {
2614 MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix);
2615 op_pix = s->dsp.avg_h264_chroma_pixels_tab;
2617 if (s->mv_dir & MV_DIR_BACKWARD) {
2618 MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix);
2621 op_qpix= s->me.qpel_put;
2622 if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
2623 op_pix = s->dsp.put_pixels_tab;
2625 op_pix = s->dsp.put_no_rnd_pixels_tab;
2627 if (s->mv_dir & MV_DIR_FORWARD) {
2628 ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
2629 op_pix = s->dsp.avg_pixels_tab;
2630 op_qpix= s->me.qpel_avg;
2632 if (s->mv_dir & MV_DIR_BACKWARD) {
2633 ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
2638 /* skip dequant / idct if we are really late ;) */
2639 if(s->avctx->skip_idct){
2640 if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
2641 ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
2642 || s->avctx->skip_idct >= AVDISCARD_ALL)
2646 /* add dct residue */
2647 if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
2648 || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
2649 add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
2650 add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
2651 add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
2652 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
2654 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2655 if (s->chroma_y_shift){
2656 add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
2657 add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
2661 add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
2662 add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
2663 add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
2664 add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
2667 } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
2668 add_dct(s, block[0], 0, dest_y , dct_linesize);
2669 add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
2670 add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
2671 add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
2673 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2674 if(s->chroma_y_shift){//Chroma420
2675 add_dct(s, block[4], 4, dest_cb, uvlinesize);
2676 add_dct(s, block[5], 5, dest_cr, uvlinesize);
2679 dct_linesize = uvlinesize << s->interlaced_dct;
2680 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
2682 add_dct(s, block[4], 4, dest_cb, dct_linesize);
2683 add_dct(s, block[5], 5, dest_cr, dct_linesize);
2684 add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
2685 add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
2686 if(!s->chroma_x_shift){//Chroma444
2687 add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
2688 add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
2689 add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
2690 add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
2695 else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
2696 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
2699 /* dct only in intra block */
2700 if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
2701 put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
2702 put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
2703 put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
2704 put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
2706 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2707 if(s->chroma_y_shift){
2708 put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
2709 put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
2713 put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
2714 put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
2715 put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
2716 put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
2720 s->dsp.idct_put(dest_y , dct_linesize, block[0]);
2721 s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
2722 s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
2723 s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
2725 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2726 if(s->chroma_y_shift){
2727 s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
2728 s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
2731 dct_linesize = uvlinesize << s->interlaced_dct;
2732 dct_offset = s->interlaced_dct? uvlinesize : uvlinesize*block_size;
2734 s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
2735 s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
2736 s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
2737 s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
2738 if(!s->chroma_x_shift){//Chroma444
2739 s->dsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
2740 s->dsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
2741 s->dsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
2742 s->dsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
2750 s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
2751 s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
2752 s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
2757 void ff_MPV_decode_mb(MpegEncContext *s, int16_t block[12][64]){
2759 if(s->out_format == FMT_MPEG1) {
2760 if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
2761 else MPV_decode_mb_internal(s, block, 0, 1);
2764 if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
2765 else MPV_decode_mb_internal(s, block, 0, 0);
2769 * @param h is the normal height, this will be reduced automatically if needed for the last row
2771 void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
2772 const int field_pic= s->picture_structure != PICT_FRAME;
2778 if (!s->avctx->hwaccel
2779 && !(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
2780 && s->unrestricted_mv
2781 && s->current_picture.f.reference
2783 && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
2784 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
2785 int sides = 0, edge_h;
2786 int hshift = desc->log2_chroma_w;
2787 int vshift = desc->log2_chroma_h;
2788 if (y==0) sides |= EDGE_TOP;
2789 if (y + h >= s->v_edge_pos) sides |= EDGE_BOTTOM;
2791 edge_h= FFMIN(h, s->v_edge_pos - y);
2793 s->dsp.draw_edges(s->current_picture_ptr->f.data[0] + y *s->linesize,
2794 s->linesize, s->h_edge_pos, edge_h,
2795 EDGE_WIDTH, EDGE_WIDTH, sides);
2796 s->dsp.draw_edges(s->current_picture_ptr->f.data[1] + (y>>vshift)*s->uvlinesize,
2797 s->uvlinesize, s->h_edge_pos>>hshift, edge_h>>vshift,
2798 EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, sides);
2799 s->dsp.draw_edges(s->current_picture_ptr->f.data[2] + (y>>vshift)*s->uvlinesize,
2800 s->uvlinesize, s->h_edge_pos>>hshift, edge_h>>vshift,
2801 EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, sides);
2804 h= FFMIN(h, s->avctx->height - y);
2806 if(field_pic && s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
2808 if (s->avctx->draw_horiz_band) {
2810 int offset[AV_NUM_DATA_POINTERS];
2813 if(s->pict_type==AV_PICTURE_TYPE_B || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
2814 src = &s->current_picture_ptr->f;
2815 else if(s->last_picture_ptr)
2816 src = &s->last_picture_ptr->f;
2820 if(s->pict_type==AV_PICTURE_TYPE_B && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
2821 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
2824 offset[0]= y * s->linesize;
2826 offset[2]= (y >> s->chroma_y_shift) * s->uvlinesize;
2827 for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
2833 s->avctx->draw_horiz_band(s->avctx, src, offset,
2834 y, s->picture_structure, h);
2838 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
2839 const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
2840 const int uvlinesize = s->current_picture.f.linesize[1];
2841 const int mb_size= 4 - s->avctx->lowres;
2843 s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
2844 s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
2845 s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
2846 s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
2847 s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
2848 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;
2849 //block_index is not used by mpeg2, so it is not affected by chroma_format
2851 s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) << mb_size);
2852 s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
2853 s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
2855 if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
2857 if(s->picture_structure==PICT_FRAME){
2858 s->dest[0] += s->mb_y * linesize << mb_size;
2859 s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
2860 s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
2862 s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
2863 s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
2864 s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
2865 av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
2870 void ff_mpeg_flush(AVCodecContext *avctx){
2872 MpegEncContext *s = avctx->priv_data;
2874 if(s==NULL || s->picture==NULL)
2877 for(i=0; i<s->picture_count; i++){
2878 if (s->picture[i].f.data[0] &&
2879 (s->picture[i].f.type == FF_BUFFER_TYPE_INTERNAL ||
2880 s->picture[i].f.type == FF_BUFFER_TYPE_USER))
2881 free_frame_buffer(s, &s->picture[i]);
2883 s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
2885 s->mb_x= s->mb_y= 0;
2888 s->parse_context.state= -1;
2889 s->parse_context.frame_start_found= 0;
2890 s->parse_context.overread= 0;
2891 s->parse_context.overread_index= 0;
2892 s->parse_context.index= 0;
2893 s->parse_context.last_index= 0;
2894 s->bitstream_buffer_size=0;
2898 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
2899 int16_t *block, int n, int qscale)
2901 int i, level, nCoeffs;
2902 const uint16_t *quant_matrix;
2904 nCoeffs= s->block_last_index[n];
2906 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
2907 /* XXX: only mpeg1 */
2908 quant_matrix = s->intra_matrix;
2909 for(i=1;i<=nCoeffs;i++) {
2910 int j= s->intra_scantable.permutated[i];
2915 level = (int)(level * qscale * quant_matrix[j]) >> 3;
2916 level = (level - 1) | 1;
2919 level = (int)(level * qscale * quant_matrix[j]) >> 3;
2920 level = (level - 1) | 1;
2927 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
2928 int16_t *block, int n, int qscale)
2930 int i, level, nCoeffs;
2931 const uint16_t *quant_matrix;
2933 nCoeffs= s->block_last_index[n];
2935 quant_matrix = s->inter_matrix;
2936 for(i=0; i<=nCoeffs; i++) {
2937 int j= s->intra_scantable.permutated[i];
2942 level = (((level << 1) + 1) * qscale *
2943 ((int) (quant_matrix[j]))) >> 4;
2944 level = (level - 1) | 1;
2947 level = (((level << 1) + 1) * qscale *
2948 ((int) (quant_matrix[j]))) >> 4;
2949 level = (level - 1) | 1;
2956 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
2957 int16_t *block, int n, int qscale)
2959 int i, level, nCoeffs;
2960 const uint16_t *quant_matrix;
2962 if(s->alternate_scan) nCoeffs= 63;
2963 else nCoeffs= s->block_last_index[n];
2965 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
2966 quant_matrix = s->intra_matrix;
2967 for(i=1;i<=nCoeffs;i++) {
2968 int j= s->intra_scantable.permutated[i];
2973 level = (int)(level * qscale * quant_matrix[j]) >> 3;
2976 level = (int)(level * qscale * quant_matrix[j]) >> 3;
2983 static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
2984 int16_t *block, int n, int qscale)
2986 int i, level, nCoeffs;
2987 const uint16_t *quant_matrix;
2990 if(s->alternate_scan) nCoeffs= 63;
2991 else nCoeffs= s->block_last_index[n];
2993 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
2995 quant_matrix = s->intra_matrix;
2996 for(i=1;i<=nCoeffs;i++) {
2997 int j= s->intra_scantable.permutated[i];
3002 level = (int)(level * qscale * quant_matrix[j]) >> 3;
3005 level = (int)(level * qscale * quant_matrix[j]) >> 3;
3014 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
3015 int16_t *block, int n, int qscale)
3017 int i, level, nCoeffs;
3018 const uint16_t *quant_matrix;
3021 if(s->alternate_scan) nCoeffs= 63;
3022 else nCoeffs= s->block_last_index[n];
3024 quant_matrix = s->inter_matrix;
3025 for(i=0; i<=nCoeffs; i++) {
3026 int j= s->intra_scantable.permutated[i];
3031 level = (((level << 1) + 1) * qscale *
3032 ((int) (quant_matrix[j]))) >> 4;
3035 level = (((level << 1) + 1) * qscale *
3036 ((int) (quant_matrix[j]))) >> 4;
3045 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
3046 int16_t *block, int n, int qscale)
3048 int i, level, qmul, qadd;
3051 assert(s->block_last_index[n]>=0);
3056 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
3057 qadd = (qscale - 1) | 1;
3064 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
3066 for(i=1; i<=nCoeffs; i++) {
3070 level = level * qmul - qadd;
3072 level = level * qmul + qadd;
3079 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
3080 int16_t *block, int n, int qscale)
3082 int i, level, qmul, qadd;
3085 assert(s->block_last_index[n]>=0);
3087 qadd = (qscale - 1) | 1;
3090 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
3092 for(i=0; i<=nCoeffs; i++) {
3096 level = level * qmul - qadd;
3098 level = level * qmul + qadd;
3106 * set qscale and update qscale dependent variables.
3108 void ff_set_qscale(MpegEncContext * s, int qscale)
3112 else if (qscale > 31)
3116 s->chroma_qscale= s->chroma_qscale_table[qscale];
3118 s->y_dc_scale= s->y_dc_scale_table[ qscale ];
3119 s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
3122 void ff_MPV_report_decode_progress(MpegEncContext *s)
3124 if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->error_occurred)
3125 ff_thread_report_progress(&s->current_picture_ptr->f, s->mb_y, 0);