2 * MPEG1 codec / MPEG2 decoder
3 * Copyright (c) 2000,2001 Fabrice Bellard.
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 #include "mpegvideo.h"
33 #include "mpeg12data.h"
34 #include "bytestream.h"
42 #define MBINCR_VLC_BITS 9
43 #define MB_PAT_VLC_BITS 9
44 #define MB_PTYPE_VLC_BITS 6
45 #define MB_BTYPE_VLC_BITS 6
46 #define TEX_VLC_BITS 9
48 #ifdef CONFIG_ENCODERS
49 static void mpeg1_encode_block(MpegEncContext *s,
52 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code); // RAL: f_code parameter added
53 #endif //CONFIG_ENCODERS
54 static inline int mpeg1_decode_block_inter(MpegEncContext *s,
57 static inline int mpeg1_decode_block_intra(MpegEncContext *s,
60 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n);
61 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
64 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
67 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n);
68 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n);
69 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
70 static void exchange_uv(MpegEncContext *s);
72 extern int XVMC_field_start(MpegEncContext *s, AVCodecContext *avctx);
73 extern int XVMC_field_end(MpegEncContext *s);
74 extern void XVMC_pack_pblocks(MpegEncContext *s,int cbp);
75 extern void XVMC_init_block(MpegEncContext *s);//set s->block
77 static const enum PixelFormat pixfmt_yuv_420[]= {PIX_FMT_YUV420P,-1};
78 static const enum PixelFormat pixfmt_yuv_422[]= {PIX_FMT_YUV422P,-1};
79 static const enum PixelFormat pixfmt_yuv_444[]= {PIX_FMT_YUV444P,-1};
80 static const enum PixelFormat pixfmt_xvmc_mpg2_420[] = {
81 PIX_FMT_XVMC_MPEG2_IDCT,
82 PIX_FMT_XVMC_MPEG2_MC,
84 #ifdef CONFIG_ENCODERS
85 static uint8_t mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
86 static uint8_t fcode_tab[MAX_MV*2+1];
88 static uint8_t uni_mpeg1_ac_vlc_len [64*64*2];
89 static uint8_t uni_mpeg2_ac_vlc_len [64*64*2];
91 /* simple include everything table for dc, first byte is bits number next 3 are code*/
92 static uint32_t mpeg1_lum_dc_uni[512];
93 static uint32_t mpeg1_chr_dc_uni[512];
95 static uint8_t mpeg1_index_run[2][64];
96 static int8_t mpeg1_max_level[2][64];
97 #endif //CONFIG_ENCODERS
99 static uint8_t static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
101 static void init_2d_vlc_rl(RLTable *rl, int use_static)
105 init_vlc(&rl->vlc, TEX_VLC_BITS, rl->n + 2,
106 &rl->table_vlc[0][1], 4, 2,
107 &rl->table_vlc[0][0], 4, 2, use_static);
110 rl->rl_vlc[0]= av_mallocz_static(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
112 rl->rl_vlc[0]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
114 for(i=0; i<rl->vlc.table_size; i++){
115 int code= rl->vlc.table[i][0];
116 int len = rl->vlc.table[i][1];
119 if(len==0){ // illegal code
122 }else if(len<0){ //more bits needed
126 if(code==rl->n){ //esc
129 }else if(code==rl->n+1){ //eob
133 run= rl->table_run [code] + 1;
134 level= rl->table_level[code];
137 rl->rl_vlc[0][i].len= len;
138 rl->rl_vlc[0][i].level= level;
139 rl->rl_vlc[0][i].run= run;
143 #ifdef CONFIG_ENCODERS
144 static void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len){
147 for(i=0; i<128; i++){
150 for(run=0; run<64; run++){
153 int alevel= FFABS(level);
154 int sign= (level>>31)&1;
156 if (alevel > rl->max_level[0][run])
159 code= rl->index_run[0][run] + alevel - 1;
161 if (code < 111 /* rl->n */) {
162 /* store the vlc & sign at once */
163 len= rl->table_vlc[code][1]+1;
164 bits= (rl->table_vlc[code][0]<<1) + sign;
166 len= rl->table_vlc[111/*rl->n*/][1]+6;
167 bits= rl->table_vlc[111/*rl->n*/][0]<<6;
177 bits|= 0x8001 + level + 255;
179 bits|= level & 0xffff;
184 uni_ac_vlc_len [UNI_AC_ENC_INDEX(run, i)]= len;
190 static int find_frame_rate_index(MpegEncContext *s){
192 int64_t dmin= INT64_MAX;
196 int64_t n0= 1001LL/ff_frame_rate_tab[i].den*ff_frame_rate_tab[i].num*s->avctx->time_base.num;
197 int64_t n1= 1001LL*s->avctx->time_base.den;
198 if(s->avctx->strict_std_compliance > FF_COMPLIANCE_INOFFICIAL && i>=9) break;
203 s->frame_rate_index= i;
212 static int encode_init(AVCodecContext *avctx)
214 MpegEncContext *s = avctx->priv_data;
216 if(MPV_encode_init(avctx) < 0)
219 if(find_frame_rate_index(s) < 0){
220 if(s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
221 av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n", avctx->time_base.den, avctx->time_base.num);
224 av_log(avctx, AV_LOG_INFO, "MPEG1/2 does not support %d/%d fps, there may be AV sync issues\n", avctx->time_base.den, avctx->time_base.num);
228 if(avctx->profile == FF_PROFILE_UNKNOWN){
229 if(avctx->level != FF_LEVEL_UNKNOWN){
230 av_log(avctx, AV_LOG_ERROR, "Set profile and level\n");
233 avctx->profile = s->chroma_format == CHROMA_420 ? 4 : 0; /* Main or 4:2:2 */
236 if(avctx->level == FF_LEVEL_UNKNOWN){
237 if(avctx->profile == 0){ /* 4:2:2 */
238 if(avctx->width <= 720 && avctx->height <= 608) avctx->level = 5; /* Main */
239 else avctx->level = 2; /* High */
241 if(avctx->profile != 1 && s->chroma_format != CHROMA_420){
242 av_log(avctx, AV_LOG_ERROR, "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n");
245 if(avctx->width <= 720 && avctx->height <= 576) avctx->level = 8; /* Main */
246 else if(avctx->width <= 1440) avctx->level = 6; /* High 1440 */
247 else avctx->level = 4; /* High */
251 if((avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE) && s->frame_rate_index != 4){
252 av_log(avctx, AV_LOG_ERROR, "Drop frame time code only allowed with 1001/30000 fps\n");
259 static void put_header(MpegEncContext *s, int header)
261 align_put_bits(&s->pb);
262 put_bits(&s->pb, 16, header>>16);
263 put_bits(&s->pb, 16, header&0xFFFF);
266 /* put sequence header if needed */
267 static void mpeg1_encode_sequence_header(MpegEncContext *s)
269 unsigned int vbv_buffer_size;
273 float best_aspect_error= 1E10;
274 float aspect_ratio= av_q2d(s->avctx->sample_aspect_ratio);
275 int constraint_parameter_flag;
277 if(aspect_ratio==0.0) aspect_ratio= 1.0; //pixel aspect 1:1 (VGA)
279 if (s->current_picture.key_frame) {
280 AVRational framerate= ff_frame_rate_tab[s->frame_rate_index];
282 /* mpeg1 header repeated every gop */
283 put_header(s, SEQ_START_CODE);
285 put_bits(&s->pb, 12, s->width);
286 put_bits(&s->pb, 12, s->height);
289 float error= aspect_ratio;
290 if(s->codec_id == CODEC_ID_MPEG1VIDEO || i <=1)
291 error-= 1.0/mpeg1_aspect[i];
293 error-= av_q2d(mpeg2_aspect[i])*s->height/s->width;
297 if(error < best_aspect_error){
298 best_aspect_error= error;
299 s->aspect_ratio_info= i;
303 put_bits(&s->pb, 4, s->aspect_ratio_info);
304 put_bits(&s->pb, 4, s->frame_rate_index);
306 if(s->avctx->rc_max_rate){
307 v = (s->avctx->rc_max_rate + 399) / 400;
308 if (v > 0x3ffff && s->codec_id == CODEC_ID_MPEG1VIDEO)
314 if(s->avctx->rc_buffer_size)
315 vbv_buffer_size = s->avctx->rc_buffer_size;
317 /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */
318 vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
319 vbv_buffer_size= (vbv_buffer_size + 16383) / 16384;
321 put_bits(&s->pb, 18, v & 0x3FFFF);
322 put_bits(&s->pb, 1, 1); /* marker */
323 put_bits(&s->pb, 10, vbv_buffer_size & 0x3FF);
325 constraint_parameter_flag=
326 s->width <= 768 && s->height <= 576 &&
327 s->mb_width * s->mb_height <= 396 &&
328 s->mb_width * s->mb_height * framerate.num <= framerate.den*396*25 &&
329 framerate.num <= framerate.den*30 &&
330 s->avctx->me_range && s->avctx->me_range < 128 &&
331 vbv_buffer_size <= 20 &&
333 s->codec_id == CODEC_ID_MPEG1VIDEO;
335 put_bits(&s->pb, 1, constraint_parameter_flag);
337 ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
338 ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
340 if(s->codec_id == CODEC_ID_MPEG2VIDEO){
341 put_header(s, EXT_START_CODE);
342 put_bits(&s->pb, 4, 1); //seq ext
344 put_bits(&s->pb, 1, s->avctx->profile == 0); //escx 1 for 4:2:2 profile */
346 put_bits(&s->pb, 3, s->avctx->profile); //profile
347 put_bits(&s->pb, 4, s->avctx->level); //level
349 put_bits(&s->pb, 1, s->progressive_sequence);
350 put_bits(&s->pb, 2, s->chroma_format);
351 put_bits(&s->pb, 2, 0); //horizontal size ext
352 put_bits(&s->pb, 2, 0); //vertical size ext
353 put_bits(&s->pb, 12, v>>18); //bitrate ext
354 put_bits(&s->pb, 1, 1); //marker
355 put_bits(&s->pb, 8, vbv_buffer_size >>10); //vbv buffer ext
356 put_bits(&s->pb, 1, s->low_delay);
357 put_bits(&s->pb, 2, 0); // frame_rate_ext_n
358 put_bits(&s->pb, 5, 0); // frame_rate_ext_d
361 put_header(s, GOP_START_CODE);
362 put_bits(&s->pb, 1, !!(s->avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE)); /* drop frame flag */
363 /* time code : we must convert from the real frame rate to a
364 fake mpeg frame rate in case of low frame rate */
365 fps = (framerate.num + framerate.den/2)/ framerate.den;
366 time_code = s->current_picture_ptr->coded_picture_number + s->avctx->timecode_frame_start;
368 s->gop_picture_number = s->current_picture_ptr->coded_picture_number;
369 if (s->avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE) {
370 /* only works for NTSC 29.97 */
371 int d = time_code / 17982;
372 int m = time_code % 17982;
373 //if (m < 2) m += 2; /* not needed since -2,-1 / 1798 in C returns 0 */
374 time_code += 18 * d + 2 * ((m - 2) / 1798);
376 put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
377 put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
378 put_bits(&s->pb, 1, 1);
379 put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
380 put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
381 put_bits(&s->pb, 1, !!(s->flags & CODEC_FLAG_CLOSED_GOP));
382 put_bits(&s->pb, 1, 0); /* broken link */
386 static inline void encode_mb_skip_run(MpegEncContext *s, int run){
388 put_bits(&s->pb, 11, 0x008);
391 put_bits(&s->pb, mbAddrIncrTable[run][1],
392 mbAddrIncrTable[run][0]);
394 #endif //CONFIG_ENCODERS
396 static void common_init(MpegEncContext *s)
400 s->c_dc_scale_table= mpeg2_dc_scale_table[s->intra_dc_precision];
404 void ff_mpeg1_clean_buffers(MpegEncContext *s){
405 s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
406 s->last_dc[1] = s->last_dc[0];
407 s->last_dc[2] = s->last_dc[0];
408 memset(s->last_mv, 0, sizeof(s->last_mv));
411 #ifdef CONFIG_ENCODERS
413 static av_always_inline void put_qscale(MpegEncContext *s)
416 assert(s->qscale>=1 && s->qscale <=12);
417 put_bits(&s->pb, 5, inv_non_linear_qscale[s->qscale]);
419 put_bits(&s->pb, 5, s->qscale);
423 void ff_mpeg1_encode_slice_header(MpegEncContext *s){
424 put_header(s, SLICE_MIN_START_CODE + s->mb_y);
426 put_bits(&s->pb, 1, 0); /* slice extra information */
429 void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
431 mpeg1_encode_sequence_header(s);
433 /* mpeg1 picture header */
434 put_header(s, PICTURE_START_CODE);
435 /* temporal reference */
437 // RAL: s->picture_number instead of s->fake_picture_number
438 put_bits(&s->pb, 10, (s->picture_number -
439 s->gop_picture_number) & 0x3ff);
440 put_bits(&s->pb, 3, s->pict_type);
442 s->vbv_delay_ptr= s->pb.buf + put_bits_count(&s->pb)/8;
443 put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
445 // RAL: Forward f_code also needed for B frames
446 if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
447 put_bits(&s->pb, 1, 0); /* half pel coordinates */
448 if(s->codec_id == CODEC_ID_MPEG1VIDEO)
449 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
451 put_bits(&s->pb, 3, 7); /* forward_f_code */
454 // RAL: Backward f_code necessary for B frames
455 if (s->pict_type == B_TYPE) {
456 put_bits(&s->pb, 1, 0); /* half pel coordinates */
457 if(s->codec_id == CODEC_ID_MPEG1VIDEO)
458 put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
460 put_bits(&s->pb, 3, 7); /* backward_f_code */
463 put_bits(&s->pb, 1, 0); /* extra bit picture */
465 s->frame_pred_frame_dct = 1;
466 if(s->codec_id == CODEC_ID_MPEG2VIDEO){
467 put_header(s, EXT_START_CODE);
468 put_bits(&s->pb, 4, 8); //pic ext
469 if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
470 put_bits(&s->pb, 4, s->f_code);
471 put_bits(&s->pb, 4, s->f_code);
473 put_bits(&s->pb, 8, 255);
475 if (s->pict_type == B_TYPE) {
476 put_bits(&s->pb, 4, s->b_code);
477 put_bits(&s->pb, 4, s->b_code);
479 put_bits(&s->pb, 8, 255);
481 put_bits(&s->pb, 2, s->intra_dc_precision);
483 assert(s->picture_structure == PICT_FRAME);
484 put_bits(&s->pb, 2, s->picture_structure);
485 if (s->progressive_sequence) {
486 put_bits(&s->pb, 1, 0); /* no repeat */
488 put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
490 /* XXX: optimize the generation of this flag with entropy
492 s->frame_pred_frame_dct = s->progressive_sequence;
494 put_bits(&s->pb, 1, s->frame_pred_frame_dct);
495 put_bits(&s->pb, 1, s->concealment_motion_vectors);
496 put_bits(&s->pb, 1, s->q_scale_type);
497 put_bits(&s->pb, 1, s->intra_vlc_format);
498 put_bits(&s->pb, 1, s->alternate_scan);
499 put_bits(&s->pb, 1, s->repeat_first_field);
500 s->progressive_frame = s->progressive_sequence;
501 put_bits(&s->pb, 1, s->chroma_format == CHROMA_420 ? s->progressive_frame : 0); /* chroma_420_type */
502 put_bits(&s->pb, 1, s->progressive_frame);
503 put_bits(&s->pb, 1, 0); //composite_display_flag
505 if(s->flags & CODEC_FLAG_SVCD_SCAN_OFFSET){
508 put_header(s, USER_START_CODE);
509 for(i=0; i<sizeof(svcd_scan_offset_placeholder); i++){
510 put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
515 ff_mpeg1_encode_slice_header(s);
518 static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
519 int has_mv, int field_motion)
521 put_bits(&s->pb, n, bits);
522 if (!s->frame_pred_frame_dct) {
524 put_bits(&s->pb, 2, 2 - field_motion); /* motion_type: frame/field */
525 put_bits(&s->pb, 1, s->interlaced_dct);
529 static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s,
530 DCTELEM block[6][64],
531 int motion_x, int motion_y,
535 const int mb_x = s->mb_x;
536 const int mb_y = s->mb_y;
537 const int first_mb= mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
541 for(i=0;i<mb_block_count;i++) {
542 if (s->block_last_index[i] >= 0)
543 cbp |= 1 << (mb_block_count - 1 - i);
546 if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
547 (mb_x != s->mb_width - 1 || (mb_y != s->mb_height - 1 && s->codec_id == CODEC_ID_MPEG1VIDEO)) &&
548 ((s->pict_type == P_TYPE && (motion_x | motion_y) == 0) ||
549 (s->pict_type == B_TYPE && s->mv_dir == s->last_mv_dir && (((s->mv_dir & MV_DIR_FORWARD) ? ((s->mv[0][0][0] - s->last_mv[0][0][0])|(s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) |
550 ((s->mv_dir & MV_DIR_BACKWARD) ? ((s->mv[1][0][0] - s->last_mv[1][0][0])|(s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) {
552 s->qscale -= s->dquant;
556 if(s->pict_type == P_TYPE){
557 s->last_mv[0][1][0]= s->last_mv[0][0][0]=
558 s->last_mv[0][1][1]= s->last_mv[0][0][1]= 0;
562 assert(s->mb_skip_run == 0);
563 encode_mb_skip_run(s, s->mb_x);
565 encode_mb_skip_run(s, s->mb_skip_run);
568 if (s->pict_type == I_TYPE) {
569 if(s->dquant && cbp){
570 put_mb_modes(s, 2, 1, 0, 0); /* macroblock_type : macroblock_quant = 1 */
573 put_mb_modes(s, 1, 1, 0, 0); /* macroblock_type : macroblock_quant = 0 */
574 s->qscale -= s->dquant;
576 s->misc_bits+= get_bits_diff(s);
578 } else if (s->mb_intra) {
579 if(s->dquant && cbp){
580 put_mb_modes(s, 6, 0x01, 0, 0);
583 put_mb_modes(s, 5, 0x03, 0, 0);
584 s->qscale -= s->dquant;
586 s->misc_bits+= get_bits_diff(s);
588 memset(s->last_mv, 0, sizeof(s->last_mv));
589 } else if (s->pict_type == P_TYPE) {
590 if(s->mv_type == MV_TYPE_16X16){
592 if ((motion_x|motion_y) == 0) {
594 put_mb_modes(s, 5, 1, 0, 0); /* macroblock_pattern & quant */
597 put_mb_modes(s, 2, 1, 0, 0); /* macroblock_pattern only */
599 s->misc_bits+= get_bits_diff(s);
602 put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
605 put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
607 s->misc_bits+= get_bits_diff(s);
608 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
609 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
610 s->mv_bits+= get_bits_diff(s);
613 put_bits(&s->pb, 3, 1); /* motion only */
614 if (!s->frame_pred_frame_dct)
615 put_bits(&s->pb, 2, 2); /* motion_type: frame */
616 s->misc_bits+= get_bits_diff(s);
617 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
618 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
619 s->qscale -= s->dquant;
620 s->mv_bits+= get_bits_diff(s);
622 s->last_mv[0][1][0]= s->last_mv[0][0][0]= motion_x;
623 s->last_mv[0][1][1]= s->last_mv[0][0][1]= motion_y;
625 assert(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);
629 put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
632 put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
635 put_bits(&s->pb, 3, 1); /* motion only */
636 put_bits(&s->pb, 2, 1); /* motion_type: field */
637 s->qscale -= s->dquant;
639 s->misc_bits+= get_bits_diff(s);
641 put_bits(&s->pb, 1, s->field_select[0][i]);
642 mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
643 mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
644 s->last_mv[0][i][0]= s->mv[0][i][0];
645 s->last_mv[0][i][1]= 2*s->mv[0][i][1];
647 s->mv_bits+= get_bits_diff(s);
650 if (s->chroma_y_shift) {
651 put_bits(&s->pb, mbPatTable[cbp][1], mbPatTable[cbp][0]);
653 put_bits(&s->pb, mbPatTable[cbp>>2][1], mbPatTable[cbp>>2][0]);
654 put_bits(&s->pb, 2, cbp & 3);
659 static const int mb_type_len[4]={0,3,4,2}; //bak,for,bi
661 if(s->mv_type == MV_TYPE_16X16){
662 if (cbp){ // With coded bloc pattern
664 if(s->mv_dir == MV_DIR_FORWARD)
665 put_mb_modes(s, 6, 3, 1, 0);
667 put_mb_modes(s, mb_type_len[s->mv_dir]+3, 2, 1, 0);
670 put_mb_modes(s, mb_type_len[s->mv_dir], 3, 1, 0);
672 }else{ // No coded bloc pattern
673 put_bits(&s->pb, mb_type_len[s->mv_dir], 2);
674 if (!s->frame_pred_frame_dct)
675 put_bits(&s->pb, 2, 2); /* motion_type: frame */
676 s->qscale -= s->dquant;
678 s->misc_bits += get_bits_diff(s);
679 if (s->mv_dir&MV_DIR_FORWARD){
680 mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
681 mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
682 s->last_mv[0][0][0]=s->last_mv[0][1][0]= s->mv[0][0][0];
683 s->last_mv[0][0][1]=s->last_mv[0][1][1]= s->mv[0][0][1];
686 if (s->mv_dir&MV_DIR_BACKWARD){
687 mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
688 mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
689 s->last_mv[1][0][0]=s->last_mv[1][1][0]= s->mv[1][0][0];
690 s->last_mv[1][0][1]=s->last_mv[1][1][1]= s->mv[1][0][1];
694 assert(s->mv_type == MV_TYPE_FIELD);
695 assert(!s->frame_pred_frame_dct);
696 if (cbp){ // With coded bloc pattern
698 if(s->mv_dir == MV_DIR_FORWARD)
699 put_mb_modes(s, 6, 3, 1, 1);
701 put_mb_modes(s, mb_type_len[s->mv_dir]+3, 2, 1, 1);
704 put_mb_modes(s, mb_type_len[s->mv_dir], 3, 1, 1);
706 }else{ // No coded bloc pattern
707 put_bits(&s->pb, mb_type_len[s->mv_dir], 2);
708 put_bits(&s->pb, 2, 1); /* motion_type: field */
709 s->qscale -= s->dquant;
711 s->misc_bits += get_bits_diff(s);
712 if (s->mv_dir&MV_DIR_FORWARD){
714 put_bits(&s->pb, 1, s->field_select[0][i]);
715 mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
716 mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
717 s->last_mv[0][i][0]= s->mv[0][i][0];
718 s->last_mv[0][i][1]= 2*s->mv[0][i][1];
722 if (s->mv_dir&MV_DIR_BACKWARD){
724 put_bits(&s->pb, 1, s->field_select[1][i]);
725 mpeg1_encode_motion(s, s->mv[1][i][0] - s->last_mv[1][i][0] , s->b_code);
726 mpeg1_encode_motion(s, s->mv[1][i][1] - (s->last_mv[1][i][1]>>1), s->b_code);
727 s->last_mv[1][i][0]= s->mv[1][i][0];
728 s->last_mv[1][i][1]= 2*s->mv[1][i][1];
733 s->mv_bits += get_bits_diff(s);
735 if (s->chroma_y_shift) {
736 put_bits(&s->pb, mbPatTable[cbp][1], mbPatTable[cbp][0]);
738 put_bits(&s->pb, mbPatTable[cbp>>2][1], mbPatTable[cbp>>2][0]);
739 put_bits(&s->pb, 2, cbp & 3);
743 for(i=0;i<mb_block_count;i++) {
744 if (cbp & (1 << (mb_block_count - 1 - i))) {
745 mpeg1_encode_block(s, block[i], i);
750 s->i_tex_bits+= get_bits_diff(s);
752 s->p_tex_bits+= get_bits_diff(s);
756 void mpeg1_encode_mb(MpegEncContext *s, DCTELEM block[6][64], int motion_x, int motion_y)
758 if (s->chroma_format == CHROMA_420) mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);
759 else mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
762 // RAL: Parameter added: f_or_b_code
763 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
765 int code, bit_size, l, bits, range, sign;
771 mbMotionVectorTable[0][1],
772 mbMotionVectorTable[0][0]);
774 bit_size = f_or_b_code - 1;
775 range = 1 << bit_size;
776 /* modulo encoding */
777 l= INT_BIT - 5 - bit_size;
782 code = (val >> bit_size) + 1;
783 bits = val & (range - 1);
788 code = (val >> bit_size) + 1;
789 bits = val & (range - 1);
793 assert(code > 0 && code <= 16);
796 mbMotionVectorTable[code][1],
797 mbMotionVectorTable[code][0]);
799 put_bits(&s->pb, 1, sign);
801 put_bits(&s->pb, bit_size, bits);
806 void ff_mpeg1_encode_init(MpegEncContext *s)
818 init_rl(&rl_mpeg1, static_rl_table_store[0]);
819 init_rl(&rl_mpeg2, static_rl_table_store[1]);
823 mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i];
824 mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i];
827 init_uni_ac_vlc(&rl_mpeg1, uni_mpeg1_ac_vlc_len);
828 if(s->intra_vlc_format)
829 init_uni_ac_vlc(&rl_mpeg2, uni_mpeg2_ac_vlc_len);
831 /* build unified dc encoding tables */
832 for(i=-255; i<256; i++)
840 index = av_log2(2*adiff);
842 bits= vlc_dc_lum_bits[index] + index;
843 code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
844 mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
846 bits= vlc_dc_chroma_bits[index] + index;
847 code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
848 mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
851 for(f_code=1; f_code<=MAX_FCODE; f_code++){
852 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
855 if(mv==0) len= mbMotionVectorTable[0][1];
857 int val, bit_size, range, code;
859 bit_size = f_code - 1;
860 range = 1 << bit_size;
866 code = (val >> bit_size) + 1;
868 len= mbMotionVectorTable[code][1] + 1 + bit_size;
870 len= mbMotionVectorTable[16][1] + 2 + bit_size;
874 mv_penalty[f_code][mv+MAX_MV]= len;
879 for(f_code=MAX_FCODE; f_code>0; f_code--){
880 for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
881 fcode_tab[mv+MAX_MV]= f_code;
885 s->me.mv_penalty= mv_penalty;
886 s->fcode_tab= fcode_tab;
887 if(s->codec_id == CODEC_ID_MPEG1VIDEO){
894 if (s->intra_vlc_format) {
895 s->intra_ac_vlc_length=
896 s->intra_ac_vlc_last_length= uni_mpeg2_ac_vlc_len;
898 s->intra_ac_vlc_length=
899 s->intra_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
901 s->inter_ac_vlc_length=
902 s->inter_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
905 static inline void encode_dc(MpegEncContext *s, int diff, int component)
907 if(((unsigned) (diff+255)) >= 511){
911 index= av_log2_16bit(-2*diff);
914 index= av_log2_16bit(2*diff);
916 if (component == 0) {
919 vlc_dc_lum_bits[index] + index,
920 (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1)));
924 vlc_dc_chroma_bits[index] + index,
925 (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1)));
928 if (component == 0) {
931 mpeg1_lum_dc_uni[diff+255]&0xFF,
932 mpeg1_lum_dc_uni[diff+255]>>8);
936 mpeg1_chr_dc_uni[diff+255]&0xFF,
937 mpeg1_chr_dc_uni[diff+255]>>8);
942 static void mpeg1_encode_block(MpegEncContext *s,
946 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
948 const uint16_t (*table_vlc)[2] = rl_mpeg1.table_vlc;
950 last_index = s->block_last_index[n];
954 component = (n <= 3 ? 0 : (n&1) + 1);
955 dc = block[0]; /* overflow is impossible */
956 diff = dc - s->last_dc[component];
957 encode_dc(s, diff, component);
958 s->last_dc[component] = dc;
960 if (s->intra_vlc_format)
961 table_vlc = rl_mpeg2.table_vlc;
963 /* encode the first coefficient : needs to be done here because
964 it is handled slightly differently */
966 if (abs(level) == 1) {
967 code = ((uint32_t)level >> 31); /* the sign bit */
968 put_bits(&s->pb, 2, code | 0x02);
977 /* now quantify & encode AC coefs */
978 last_non_zero = i - 1;
980 for(;i<=last_index;i++) {
981 j = s->intra_scantable.permutated[i];
986 dprintf(s->avctx, "level[%d]=%d\n", i, level);
988 /* encode using VLC */
990 run = i - last_non_zero - 1;
993 MASK_ABS(sign, alevel)
996 if (alevel <= mpeg1_max_level[0][run]){
997 code= mpeg1_index_run[0][run] + alevel - 1;
998 /* store the vlc & sign at once */
999 put_bits(&s->pb, table_vlc[code][1]+1, (table_vlc[code][0]<<1) + sign);
1001 /* escape seems to be pretty rare <5% so I do not optimize it */
1002 put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
1003 /* escape: only clip in this case */
1004 put_bits(&s->pb, 6, run);
1005 if(s->codec_id == CODEC_ID_MPEG1VIDEO){
1007 put_bits(&s->pb, 8, level & 0xff);
1010 put_bits(&s->pb, 16, 0x8001 + level + 255);
1012 put_bits(&s->pb, 16, level & 0xffff);
1016 put_bits(&s->pb, 12, level & 0xfff);
1023 put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
1025 #endif //CONFIG_ENCODERS
1027 /******************************************/
1030 static VLC dc_lum_vlc;
1031 static VLC dc_chroma_vlc;
1033 static VLC mbincr_vlc;
1034 static VLC mb_ptype_vlc;
1035 static VLC mb_btype_vlc;
1036 static VLC mb_pat_vlc;
1038 static void init_vlcs(void)
1040 static int done = 0;
1045 init_vlc(&dc_lum_vlc, DC_VLC_BITS, 12,
1046 vlc_dc_lum_bits, 1, 1,
1047 vlc_dc_lum_code, 2, 2, 1);
1048 init_vlc(&dc_chroma_vlc, DC_VLC_BITS, 12,
1049 vlc_dc_chroma_bits, 1, 1,
1050 vlc_dc_chroma_code, 2, 2, 1);
1051 init_vlc(&mv_vlc, MV_VLC_BITS, 17,
1052 &mbMotionVectorTable[0][1], 2, 1,
1053 &mbMotionVectorTable[0][0], 2, 1, 1);
1054 init_vlc(&mbincr_vlc, MBINCR_VLC_BITS, 36,
1055 &mbAddrIncrTable[0][1], 2, 1,
1056 &mbAddrIncrTable[0][0], 2, 1, 1);
1057 init_vlc(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
1058 &mbPatTable[0][1], 2, 1,
1059 &mbPatTable[0][0], 2, 1, 1);
1061 init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
1062 &table_mb_ptype[0][1], 2, 1,
1063 &table_mb_ptype[0][0], 2, 1, 1);
1064 init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
1065 &table_mb_btype[0][1], 2, 1,
1066 &table_mb_btype[0][0], 2, 1, 1);
1067 init_rl(&rl_mpeg1, static_rl_table_store[0]);
1068 init_rl(&rl_mpeg2, static_rl_table_store[1]);
1070 init_2d_vlc_rl(&rl_mpeg1, 1);
1071 init_2d_vlc_rl(&rl_mpeg2, 1);
1075 static inline int get_dmv(MpegEncContext *s)
1077 if(get_bits1(&s->gb))
1078 return 1 - (get_bits1(&s->gb) << 1);
1083 static inline int get_qscale(MpegEncContext *s)
1085 int qscale = get_bits(&s->gb, 5);
1086 if (s->q_scale_type) {
1087 return non_linear_qscale[qscale];
1093 /* motion type (for mpeg2) */
1099 static int mpeg_decode_mb(MpegEncContext *s,
1100 DCTELEM block[12][64])
1102 int i, j, k, cbp, val, mb_type, motion_type;
1103 const int mb_block_count = 4 + (1<< s->chroma_format);
1105 dprintf(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
1107 assert(s->mb_skipped==0);
1109 if (s->mb_skip_run-- != 0) {
1110 if(s->pict_type == I_TYPE){
1111 av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1118 s->block_last_index[i] = -1;
1119 if(s->picture_structure == PICT_FRAME)
1120 s->mv_type = MV_TYPE_16X16;
1122 s->mv_type = MV_TYPE_FIELD;
1123 if (s->pict_type == P_TYPE) {
1124 /* if P type, zero motion vector is implied */
1125 s->mv_dir = MV_DIR_FORWARD;
1126 s->mv[0][0][0] = s->mv[0][0][1] = 0;
1127 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1128 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1129 s->field_select[0][0]= s->picture_structure - 1;
1131 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
1136 mb_type= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1];
1138 mb_type= s->current_picture.mb_type[ s->mb_width + (s->mb_y-1)*s->mb_stride - 1]; // FIXME not sure if this is allowed in mpeg at all,
1139 if(IS_INTRA(mb_type))
1142 /* if B type, reuse previous vectors and directions */
1143 s->mv[0][0][0] = s->last_mv[0][0][0];
1144 s->mv[0][0][1] = s->last_mv[0][0][1];
1145 s->mv[1][0][0] = s->last_mv[1][0][0];
1146 s->mv[1][0][1] = s->last_mv[1][0][1];
1148 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]=
1149 mb_type | MB_TYPE_SKIP;
1150 // assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
1152 if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0)
1159 switch(s->pict_type) {
1162 if (get_bits1(&s->gb) == 0) {
1163 if (get_bits1(&s->gb) == 0){
1164 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
1167 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
1169 mb_type = MB_TYPE_INTRA;
1173 mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
1175 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
1178 mb_type = ptype2mb_type[ mb_type ];
1181 mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
1183 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
1186 mb_type = btype2mb_type[ mb_type ];
1189 dprintf(s->avctx, "mb_type=%x\n", mb_type);
1190 // motion_type = 0; /* avoid warning */
1191 if (IS_INTRA(mb_type)) {
1192 s->dsp.clear_blocks(s->block[0]);
1194 if(!s->chroma_y_shift){
1195 s->dsp.clear_blocks(s->block[6]);
1198 /* compute dct type */
1199 if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
1200 !s->frame_pred_frame_dct) {
1201 s->interlaced_dct = get_bits1(&s->gb);
1204 if (IS_QUANT(mb_type))
1205 s->qscale = get_qscale(s);
1207 if (s->concealment_motion_vectors) {
1208 /* just parse them */
1209 if (s->picture_structure != PICT_FRAME)
1210 skip_bits1(&s->gb); /* field select */
1212 s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
1213 mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
1214 s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
1215 mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
1217 skip_bits1(&s->gb); /* marker */
1219 memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
1222 //one 1 we memcpy blocks in xvmcvideo
1223 if(s->avctx->xvmc_acceleration > 1){
1224 XVMC_pack_pblocks(s,-1);//inter are always full blocks
1231 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
1232 if(s->flags2 & CODEC_FLAG2_FAST){
1234 mpeg2_fast_decode_block_intra(s, s->pblocks[i], i);
1237 for(i=0;i<mb_block_count;i++) {
1238 if (mpeg2_decode_block_intra(s, s->pblocks[i], i) < 0)
1244 if (mpeg1_decode_block_intra(s, s->pblocks[i], i) < 0)
1249 if (mb_type & MB_TYPE_ZERO_MV){
1250 assert(mb_type & MB_TYPE_CBP);
1252 /* compute dct type */
1253 if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
1254 !s->frame_pred_frame_dct) {
1255 s->interlaced_dct = get_bits1(&s->gb);
1258 if (IS_QUANT(mb_type))
1259 s->qscale = get_qscale(s);
1261 s->mv_dir = MV_DIR_FORWARD;
1262 if(s->picture_structure == PICT_FRAME)
1263 s->mv_type = MV_TYPE_16X16;
1265 s->mv_type = MV_TYPE_FIELD;
1266 mb_type |= MB_TYPE_INTERLACED;
1267 s->field_select[0][0]= s->picture_structure - 1;
1269 s->last_mv[0][0][0] = 0;
1270 s->last_mv[0][0][1] = 0;
1271 s->last_mv[0][1][0] = 0;
1272 s->last_mv[0][1][1] = 0;
1276 assert(mb_type & MB_TYPE_L0L1);
1277 //FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
1278 /* get additional motion vector type */
1279 if (s->frame_pred_frame_dct)
1280 motion_type = MT_FRAME;
1282 motion_type = get_bits(&s->gb, 2);
1285 /* compute dct type */
1286 if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
1287 !s->frame_pred_frame_dct && HAS_CBP(mb_type)) {
1288 s->interlaced_dct = get_bits1(&s->gb);
1291 if (IS_QUANT(mb_type))
1292 s->qscale = get_qscale(s);
1294 /* motion vectors */
1297 if (USES_LIST(mb_type, i)) {
1298 s->mv_dir |= (MV_DIR_FORWARD >> i);
1299 dprintf(s->avctx, "motion_type=%d\n", motion_type);
1300 switch(motion_type) {
1301 case MT_FRAME: /* or MT_16X8 */
1302 if (s->picture_structure == PICT_FRAME) {
1304 mb_type |= MB_TYPE_16x16;
1305 s->mv_type = MV_TYPE_16X16;
1306 s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
1307 mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
1308 s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
1309 mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
1310 /* full_pel: only for mpeg1 */
1311 if (s->full_pel[i]){
1312 s->mv[i][0][0] <<= 1;
1313 s->mv[i][0][1] <<= 1;
1317 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1318 s->mv_type = MV_TYPE_16X8;
1320 s->field_select[i][j] = get_bits1(&s->gb);
1322 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
1323 s->last_mv[i][j][k]);
1324 s->last_mv[i][j][k] = val;
1325 s->mv[i][j][k] = val;
1331 s->mv_type = MV_TYPE_FIELD;
1332 if (s->picture_structure == PICT_FRAME) {
1333 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1335 s->field_select[i][j] = get_bits1(&s->gb);
1336 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
1337 s->last_mv[i][j][0]);
1338 s->last_mv[i][j][0] = val;
1339 s->mv[i][j][0] = val;
1340 dprintf(s->avctx, "fmx=%d\n", val);
1341 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
1342 s->last_mv[i][j][1] >> 1);
1343 s->last_mv[i][j][1] = val << 1;
1344 s->mv[i][j][1] = val;
1345 dprintf(s->avctx, "fmy=%d\n", val);
1348 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
1349 s->field_select[i][0] = get_bits1(&s->gb);
1351 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
1352 s->last_mv[i][0][k]);
1353 s->last_mv[i][0][k] = val;
1354 s->last_mv[i][1][k] = val;
1355 s->mv[i][0][k] = val;
1361 int dmx, dmy, mx, my, m;
1363 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
1364 s->last_mv[i][0][0]);
1365 s->last_mv[i][0][0] = mx;
1366 s->last_mv[i][1][0] = mx;
1368 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
1369 s->last_mv[i][0][1] >> 1);
1371 s->mv_type = MV_TYPE_DMV;
1374 s->last_mv[i][0][1] = my<<1;
1375 s->last_mv[i][1][1] = my<<1;
1377 s->mv[i][0][0] = mx;
1378 s->mv[i][0][1] = my;
1379 s->mv[i][1][0] = mx;//not used
1380 s->mv[i][1][1] = my;//not used
1382 if (s->picture_structure == PICT_FRAME) {
1383 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
1385 //m = 1 + 2 * s->top_field_first;
1386 m = s->top_field_first ? 1 : 3;
1388 /* top -> top pred */
1389 s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1390 s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
1392 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1393 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
1395 mb_type |= MB_TYPE_16x16;
1397 s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
1398 s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
1399 if(s->picture_structure == PICT_TOP_FIELD)
1407 av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
1415 if (HAS_CBP(mb_type)) {
1416 s->dsp.clear_blocks(s->block[0]);
1418 if(!s->chroma_y_shift){
1419 s->dsp.clear_blocks(s->block[6]);
1422 cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
1423 if (cbp < 0 || ((cbp == 0) && (s->chroma_format < 2)) ){
1424 av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
1427 if(mb_block_count > 6){
1428 cbp<<= mb_block_count-6;
1429 cbp |= get_bits(&s->gb, mb_block_count-6);
1433 //on 1 we memcpy blocks in xvmcvideo
1434 if(s->avctx->xvmc_acceleration > 1){
1435 XVMC_pack_pblocks(s,cbp);
1442 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
1443 if(s->flags2 & CODEC_FLAG2_FAST){
1446 mpeg2_fast_decode_block_non_intra(s, s->pblocks[i], i);
1448 s->block_last_index[i] = -1;
1453 cbp<<= 12-mb_block_count;
1455 for(i=0;i<mb_block_count;i++) {
1456 if ( cbp & (1<<11) ) {
1457 if (mpeg2_decode_block_non_intra(s, s->pblocks[i], i) < 0)
1460 s->block_last_index[i] = -1;
1466 if(s->flags2 & CODEC_FLAG2_FAST){
1469 mpeg1_fast_decode_block_inter(s, s->pblocks[i], i);
1471 s->block_last_index[i] = -1;
1478 if (mpeg1_decode_block_inter(s, s->pblocks[i], i) < 0)
1481 s->block_last_index[i] = -1;
1489 s->block_last_index[i] = -1;
1493 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
1498 /* as h263, but only 17 codes */
1499 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
1501 int code, sign, val, l, shift;
1503 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
1511 sign = get_bits1(&s->gb);
1515 val = (val - 1) << shift;
1516 val |= get_bits(&s->gb, shift);
1523 /* modulo decoding */
1524 l= INT_BIT - 5 - shift;
1529 static inline int decode_dc(GetBitContext *gb, int component)
1533 if (component == 0) {
1534 code = get_vlc2(gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
1536 code = get_vlc2(gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
1539 av_log(NULL, AV_LOG_ERROR, "invalid dc code at\n");
1545 diff = get_xbits(gb, code);
1550 static inline int mpeg1_decode_block_intra(MpegEncContext *s,
1554 int level, dc, diff, i, j, run;
1556 RLTable *rl = &rl_mpeg1;
1557 uint8_t * const scantable= s->intra_scantable.permutated;
1558 const uint16_t *quant_matrix= s->intra_matrix;
1559 const int qscale= s->qscale;
1562 component = (n <= 3 ? 0 : n - 4 + 1);
1563 diff = decode_dc(&s->gb, component);
1566 dc = s->last_dc[component];
1568 s->last_dc[component] = dc;
1570 dprintf(s->avctx, "dc=%d diff=%d\n", dc, diff);
1573 OPEN_READER(re, &s->gb);
1574 /* now quantify & encode AC coefs */
1576 UPDATE_CACHE(re, &s->gb);
1577 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1581 } else if(level != 0) {
1584 level= (level*qscale*quant_matrix[j])>>4;
1586 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1587 LAST_SKIP_BITS(re, &s->gb, 1);
1590 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1591 UPDATE_CACHE(re, &s->gb);
1592 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1593 if (level == -128) {
1594 level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
1595 } else if (level == 0) {
1596 level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
1602 level= (level*qscale*quant_matrix[j])>>4;
1606 level= (level*qscale*quant_matrix[j])>>4;
1611 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1617 CLOSE_READER(re, &s->gb);
1619 s->block_last_index[n] = i;
1623 static inline int mpeg1_decode_block_inter(MpegEncContext *s,
1627 int level, i, j, run;
1628 RLTable *rl = &rl_mpeg1;
1629 uint8_t * const scantable= s->intra_scantable.permutated;
1630 const uint16_t *quant_matrix= s->inter_matrix;
1631 const int qscale= s->qscale;
1634 OPEN_READER(re, &s->gb);
1636 /* special case for the first coef. no need to add a second vlc table */
1637 UPDATE_CACHE(re, &s->gb);
1638 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
1639 level= (3*qscale*quant_matrix[0])>>5;
1641 if(GET_CACHE(re, &s->gb)&0x40000000)
1645 SKIP_BITS(re, &s->gb, 2);
1646 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1650 /* now quantify & encode AC coefs */
1652 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1657 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1659 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1660 SKIP_BITS(re, &s->gb, 1);
1663 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1664 UPDATE_CACHE(re, &s->gb);
1665 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1666 if (level == -128) {
1667 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
1668 } else if (level == 0) {
1669 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
1675 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1679 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1684 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1689 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1691 UPDATE_CACHE(re, &s->gb);
1694 LAST_SKIP_BITS(re, &s->gb, 2);
1695 CLOSE_READER(re, &s->gb);
1697 s->block_last_index[n] = i;
1701 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
1703 int level, i, j, run;
1704 RLTable *rl = &rl_mpeg1;
1705 uint8_t * const scantable= s->intra_scantable.permutated;
1706 const int qscale= s->qscale;
1709 OPEN_READER(re, &s->gb);
1711 /* special case for the first coef. no need to add a second vlc table */
1712 UPDATE_CACHE(re, &s->gb);
1713 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
1714 level= (3*qscale)>>1;
1716 if(GET_CACHE(re, &s->gb)&0x40000000)
1720 SKIP_BITS(re, &s->gb, 2);
1721 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1725 /* now quantify & encode AC coefs */
1727 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1732 level= ((level*2+1)*qscale)>>1;
1734 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1735 SKIP_BITS(re, &s->gb, 1);
1738 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1739 UPDATE_CACHE(re, &s->gb);
1740 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1741 if (level == -128) {
1742 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
1743 } else if (level == 0) {
1744 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
1750 level= ((level*2+1)*qscale)>>1;
1754 level= ((level*2+1)*qscale)>>1;
1760 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1762 UPDATE_CACHE(re, &s->gb);
1765 LAST_SKIP_BITS(re, &s->gb, 2);
1766 CLOSE_READER(re, &s->gb);
1768 s->block_last_index[n] = i;
1773 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
1777 int level, i, j, run;
1778 RLTable *rl = &rl_mpeg1;
1779 uint8_t * const scantable= s->intra_scantable.permutated;
1780 const uint16_t *quant_matrix;
1781 const int qscale= s->qscale;
1787 OPEN_READER(re, &s->gb);
1790 quant_matrix = s->inter_matrix;
1792 quant_matrix = s->chroma_inter_matrix;
1794 /* special case for the first coef. no need to add a second vlc table */
1795 UPDATE_CACHE(re, &s->gb);
1796 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
1797 level= (3*qscale*quant_matrix[0])>>5;
1798 if(GET_CACHE(re, &s->gb)&0x40000000)
1803 SKIP_BITS(re, &s->gb, 2);
1804 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1808 /* now quantify & encode AC coefs */
1810 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1815 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1816 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1817 SKIP_BITS(re, &s->gb, 1);
1820 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1821 UPDATE_CACHE(re, &s->gb);
1822 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1827 level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
1830 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1834 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1840 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1842 UPDATE_CACHE(re, &s->gb);
1845 LAST_SKIP_BITS(re, &s->gb, 2);
1846 CLOSE_READER(re, &s->gb);
1848 block[63] ^= (mismatch & 1);
1850 s->block_last_index[n] = i;
1854 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
1858 int level, i, j, run;
1859 RLTable *rl = &rl_mpeg1;
1860 uint8_t * const scantable= s->intra_scantable.permutated;
1861 const int qscale= s->qscale;
1862 OPEN_READER(re, &s->gb);
1865 /* special case for the first coef. no need to add a second vlc table */
1866 UPDATE_CACHE(re, &s->gb);
1867 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
1868 level= (3*qscale)>>1;
1869 if(GET_CACHE(re, &s->gb)&0x40000000)
1873 SKIP_BITS(re, &s->gb, 2);
1874 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1878 /* now quantify & encode AC coefs */
1880 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1885 level= ((level*2+1)*qscale)>>1;
1886 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1887 SKIP_BITS(re, &s->gb, 1);
1890 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1891 UPDATE_CACHE(re, &s->gb);
1892 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1897 level= ((-level*2+1)*qscale)>>1;
1900 level= ((level*2+1)*qscale)>>1;
1905 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1907 UPDATE_CACHE(re, &s->gb);
1910 LAST_SKIP_BITS(re, &s->gb, 2);
1911 CLOSE_READER(re, &s->gb);
1912 s->block_last_index[n] = i;
1917 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
1921 int level, dc, diff, i, j, run;
1924 uint8_t * const scantable= s->intra_scantable.permutated;
1925 const uint16_t *quant_matrix;
1926 const int qscale= s->qscale;
1931 quant_matrix = s->intra_matrix;
1934 quant_matrix = s->chroma_intra_matrix;
1935 component = (n&1) + 1;
1937 diff = decode_dc(&s->gb, component);
1940 dc = s->last_dc[component];
1942 s->last_dc[component] = dc;
1943 block[0] = dc << (3 - s->intra_dc_precision);
1944 dprintf(s->avctx, "dc=%d\n", block[0]);
1945 mismatch = block[0] ^ 1;
1947 if (s->intra_vlc_format)
1953 OPEN_READER(re, &s->gb);
1954 /* now quantify & encode AC coefs */
1956 UPDATE_CACHE(re, &s->gb);
1957 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1961 } else if(level != 0) {
1964 level= (level*qscale*quant_matrix[j])>>4;
1965 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1966 LAST_SKIP_BITS(re, &s->gb, 1);
1969 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1970 UPDATE_CACHE(re, &s->gb);
1971 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1975 level= (-level*qscale*quant_matrix[j])>>4;
1978 level= (level*qscale*quant_matrix[j])>>4;
1982 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1989 CLOSE_READER(re, &s->gb);
1991 block[63]^= mismatch&1;
1993 s->block_last_index[n] = i;
1997 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
2001 int level, dc, diff, j, run;
2004 uint8_t * scantable= s->intra_scantable.permutated;
2005 const uint16_t *quant_matrix;
2006 const int qscale= s->qscale;
2010 quant_matrix = s->intra_matrix;
2013 quant_matrix = s->chroma_intra_matrix;
2014 component = (n&1) + 1;
2016 diff = decode_dc(&s->gb, component);
2019 dc = s->last_dc[component];
2021 s->last_dc[component] = dc;
2022 block[0] = dc << (3 - s->intra_dc_precision);
2023 if (s->intra_vlc_format)
2029 OPEN_READER(re, &s->gb);
2030 /* now quantify & encode AC coefs */
2032 UPDATE_CACHE(re, &s->gb);
2033 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
2037 } else if(level != 0) {
2040 level= (level*qscale*quant_matrix[j])>>4;
2041 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
2042 LAST_SKIP_BITS(re, &s->gb, 1);
2045 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
2046 UPDATE_CACHE(re, &s->gb);
2047 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
2051 level= (-level*qscale*quant_matrix[j])>>4;
2054 level= (level*qscale*quant_matrix[j])>>4;
2060 CLOSE_READER(re, &s->gb);
2063 s->block_last_index[n] = scantable - s->intra_scantable.permutated;
2067 typedef struct Mpeg1Context {
2068 MpegEncContext mpeg_enc_ctx;
2069 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
2070 int repeat_field; /* true if we must repeat the field */
2071 AVPanScan pan_scan; /** some temporary storage for the panscan */
2073 int swap_uv;//indicate VCR2
2074 int save_aspect_info;
2075 int save_width, save_height;
2076 AVRational frame_rate_ext; ///< MPEG-2 specific framerate modificator
2080 static int mpeg_decode_init(AVCodecContext *avctx)
2082 Mpeg1Context *s = avctx->priv_data;
2083 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2086 //we need some parmutation to store
2087 //matrixes, until MPV_common_init()
2088 //set the real permutatuon
2090 s2->dsp.idct_permutation[i]=i;
2092 MPV_decode_defaults(s2);
2094 s->mpeg_enc_ctx.avctx= avctx;
2095 s->mpeg_enc_ctx.flags= avctx->flags;
2096 s->mpeg_enc_ctx.flags2= avctx->flags2;
2097 common_init(&s->mpeg_enc_ctx);
2100 s->mpeg_enc_ctx_allocated = 0;
2101 s->mpeg_enc_ctx.picture_number = 0;
2102 s->repeat_field = 0;
2103 s->mpeg_enc_ctx.codec_id= avctx->codec->id;
2107 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
2108 const uint8_t *new_perm){
2109 uint16_t temp_matrix[64];
2112 memcpy(temp_matrix,matrix,64*sizeof(uint16_t));
2115 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
2119 //Call this function when we know all parameters
2120 //it may be called in different places for mpeg1 and mpeg2
2121 static int mpeg_decode_postinit(AVCodecContext *avctx){
2122 Mpeg1Context *s1 = avctx->priv_data;
2123 MpegEncContext *s = &s1->mpeg_enc_ctx;
2124 uint8_t old_permutation[64];
2127 (s1->mpeg_enc_ctx_allocated == 0)||
2128 avctx->coded_width != s->width ||
2129 avctx->coded_height != s->height||
2130 s1->save_width != s->width ||
2131 s1->save_height != s->height ||
2132 s1->save_aspect_info != s->aspect_ratio_info||
2136 if (s1->mpeg_enc_ctx_allocated) {
2137 ParseContext pc= s->parse_context;
2138 s->parse_context.buffer=0;
2140 s->parse_context= pc;
2143 if( (s->width == 0 )||(s->height == 0))
2146 avcodec_set_dimensions(avctx, s->width, s->height);
2147 avctx->bit_rate = s->bit_rate;
2148 s1->save_aspect_info = s->aspect_ratio_info;
2149 s1->save_width = s->width;
2150 s1->save_height = s->height;
2152 //low_delay may be forced, in this case we will have B frames
2153 //that behave like P frames
2154 avctx->has_b_frames = !(s->low_delay);
2156 if(avctx->sub_id==1){//s->codec_id==avctx->codec_id==CODEC_ID
2158 avctx->time_base.den= ff_frame_rate_tab[s->frame_rate_index].num;
2159 avctx->time_base.num= ff_frame_rate_tab[s->frame_rate_index].den;
2161 avctx->sample_aspect_ratio= av_d2q(
2162 1.0/mpeg1_aspect[s->aspect_ratio_info], 255);
2167 &s->avctx->time_base.den,
2168 &s->avctx->time_base.num,
2169 ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num,
2170 ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
2173 if(s->aspect_ratio_info > 1){
2174 if( (s1->pan_scan.width == 0 )||(s1->pan_scan.height == 0) ){
2175 s->avctx->sample_aspect_ratio=
2177 mpeg2_aspect[s->aspect_ratio_info],
2178 (AVRational){s->width, s->height}
2181 s->avctx->sample_aspect_ratio=
2183 mpeg2_aspect[s->aspect_ratio_info],
2184 (AVRational){s1->pan_scan.width, s1->pan_scan.height}
2188 s->avctx->sample_aspect_ratio=
2189 mpeg2_aspect[s->aspect_ratio_info];
2193 if(avctx->xvmc_acceleration){
2194 avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
2196 if(s->chroma_format < 2){
2197 avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_420);
2199 if(s->chroma_format == 2){
2200 avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_422);
2202 if(s->chroma_format > 2){
2203 avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_444);
2206 //until then pix_fmt may be changed right after codec init
2207 if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
2208 if( avctx->idct_algo == FF_IDCT_AUTO )
2209 avctx->idct_algo = FF_IDCT_SIMPLE;
2211 //quantization matrixes may need reordering
2212 //if dct permutation is changed
2213 memcpy(old_permutation,s->dsp.idct_permutation,64*sizeof(uint8_t));
2215 if (MPV_common_init(s) < 0)
2218 quant_matrix_rebuild(s->intra_matrix, old_permutation,s->dsp.idct_permutation);
2219 quant_matrix_rebuild(s->inter_matrix, old_permutation,s->dsp.idct_permutation);
2220 quant_matrix_rebuild(s->chroma_intra_matrix,old_permutation,s->dsp.idct_permutation);
2221 quant_matrix_rebuild(s->chroma_inter_matrix,old_permutation,s->dsp.idct_permutation);
2223 s1->mpeg_enc_ctx_allocated = 1;
2228 static int mpeg1_decode_picture(AVCodecContext *avctx,
2229 const uint8_t *buf, int buf_size)
2231 Mpeg1Context *s1 = avctx->priv_data;
2232 MpegEncContext *s = &s1->mpeg_enc_ctx;
2233 int ref, f_code, vbv_delay;
2235 if(mpeg_decode_postinit(s->avctx) < 0)
2238 init_get_bits(&s->gb, buf, buf_size*8);
2240 ref = get_bits(&s->gb, 10); /* temporal ref */
2241 s->pict_type = get_bits(&s->gb, 3);
2242 if(s->pict_type == 0 || s->pict_type > 3)
2245 vbv_delay= get_bits(&s->gb, 16);
2246 if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
2247 s->full_pel[0] = get_bits1(&s->gb);
2248 f_code = get_bits(&s->gb, 3);
2249 if (f_code == 0 && avctx->error_resilience >= FF_ER_COMPLIANT)
2251 s->mpeg_f_code[0][0] = f_code;
2252 s->mpeg_f_code[0][1] = f_code;
2254 if (s->pict_type == B_TYPE) {
2255 s->full_pel[1] = get_bits1(&s->gb);
2256 f_code = get_bits(&s->gb, 3);
2257 if (f_code == 0 && avctx->error_resilience >= FF_ER_COMPLIANT)
2259 s->mpeg_f_code[1][0] = f_code;
2260 s->mpeg_f_code[1][1] = f_code;
2262 s->current_picture.pict_type= s->pict_type;
2263 s->current_picture.key_frame= s->pict_type == I_TYPE;
2265 if(avctx->debug & FF_DEBUG_PICT_INFO)
2266 av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
2274 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
2276 MpegEncContext *s= &s1->mpeg_enc_ctx;
2277 int horiz_size_ext, vert_size_ext;
2280 skip_bits(&s->gb, 1); /* profil and level esc*/
2281 s->avctx->profile= get_bits(&s->gb, 3);
2282 s->avctx->level= get_bits(&s->gb, 4);
2283 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
2284 s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
2285 horiz_size_ext = get_bits(&s->gb, 2);
2286 vert_size_ext = get_bits(&s->gb, 2);
2287 s->width |= (horiz_size_ext << 12);
2288 s->height |= (vert_size_ext << 12);
2289 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
2290 s->bit_rate += (bit_rate_ext << 18) * 400;
2291 skip_bits1(&s->gb); /* marker */
2292 s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10;
2294 s->low_delay = get_bits1(&s->gb);
2295 if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
2297 s1->frame_rate_ext.num = get_bits(&s->gb, 2)+1;
2298 s1->frame_rate_ext.den = get_bits(&s->gb, 5)+1;
2300 dprintf(s->avctx, "sequence extension\n");
2301 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
2302 s->avctx->sub_id = 2; /* indicates mpeg2 found */
2304 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
2305 av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
2306 s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
2310 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
2312 MpegEncContext *s= &s1->mpeg_enc_ctx;
2313 int color_description, w, h;
2315 skip_bits(&s->gb, 3); /* video format */
2316 color_description= get_bits1(&s->gb);
2317 if(color_description){
2318 skip_bits(&s->gb, 8); /* color primaries */
2319 skip_bits(&s->gb, 8); /* transfer_characteristics */
2320 skip_bits(&s->gb, 8); /* matrix_coefficients */
2322 w= get_bits(&s->gb, 14);
2323 skip_bits(&s->gb, 1); //marker
2324 h= get_bits(&s->gb, 14);
2325 skip_bits(&s->gb, 1); //marker
2327 s1->pan_scan.width= 16*w;
2328 s1->pan_scan.height=16*h;
2330 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
2331 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
2334 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
2336 MpegEncContext *s= &s1->mpeg_enc_ctx;
2340 if(s->progressive_sequence){
2341 if(s->repeat_first_field){
2343 if(s->top_field_first)
2347 if(s->picture_structure == PICT_FRAME){
2349 if(s->repeat_first_field)
2353 for(i=0; i<nofco; i++){
2354 s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
2355 skip_bits(&s->gb, 1); //marker
2356 s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
2357 skip_bits(&s->gb, 1); //marker
2360 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
2361 av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
2362 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
2363 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
2364 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
2368 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
2372 dprintf(s->avctx, "matrix extension\n");
2374 if (get_bits1(&s->gb)) {
2376 v = get_bits(&s->gb, 8);
2377 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
2378 s->intra_matrix[j] = v;
2379 s->chroma_intra_matrix[j] = v;
2382 if (get_bits1(&s->gb)) {
2384 v = get_bits(&s->gb, 8);
2385 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
2386 s->inter_matrix[j] = v;
2387 s->chroma_inter_matrix[j] = v;
2390 if (get_bits1(&s->gb)) {
2392 v = get_bits(&s->gb, 8);
2393 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
2394 s->chroma_intra_matrix[j] = v;
2397 if (get_bits1(&s->gb)) {
2399 v = get_bits(&s->gb, 8);
2400 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
2401 s->chroma_inter_matrix[j] = v;
2406 static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
2408 s->full_pel[0] = s->full_pel[1] = 0;
2409 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
2410 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
2411 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
2412 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
2413 s->intra_dc_precision = get_bits(&s->gb, 2);
2414 s->picture_structure = get_bits(&s->gb, 2);
2415 s->top_field_first = get_bits1(&s->gb);
2416 s->frame_pred_frame_dct = get_bits1(&s->gb);
2417 s->concealment_motion_vectors = get_bits1(&s->gb);
2418 s->q_scale_type = get_bits1(&s->gb);
2419 s->intra_vlc_format = get_bits1(&s->gb);
2420 s->alternate_scan = get_bits1(&s->gb);
2421 s->repeat_first_field = get_bits1(&s->gb);
2422 s->chroma_420_type = get_bits1(&s->gb);
2423 s->progressive_frame = get_bits1(&s->gb);
2425 if(s->picture_structure == PICT_FRAME){
2427 s->v_edge_pos= 16*s->mb_height;
2429 s->first_field ^= 1;
2430 s->v_edge_pos= 8*s->mb_height;
2431 memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
2434 if(s->alternate_scan){
2435 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
2436 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
2438 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
2439 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
2442 /* composite display not parsed */
2443 dprintf(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
2444 dprintf(s->avctx, "picture_structure=%d\n", s->picture_structure);
2445 dprintf(s->avctx, "top field first=%d\n", s->top_field_first);
2446 dprintf(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
2447 dprintf(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
2448 dprintf(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
2449 dprintf(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
2450 dprintf(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
2451 dprintf(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
2454 static void mpeg_decode_extension(AVCodecContext *avctx,
2455 const uint8_t *buf, int buf_size)
2457 Mpeg1Context *s1 = avctx->priv_data;
2458 MpegEncContext *s = &s1->mpeg_enc_ctx;
2461 init_get_bits(&s->gb, buf, buf_size*8);
2463 ext_type = get_bits(&s->gb, 4);
2466 mpeg_decode_sequence_extension(s1);
2469 mpeg_decode_sequence_display_extension(s1);
2472 mpeg_decode_quant_matrix_extension(s);
2475 mpeg_decode_picture_display_extension(s1);
2478 mpeg_decode_picture_coding_extension(s);
2483 static void exchange_uv(MpegEncContext *s){
2484 short * tmp = s->pblocks[4];
2485 s->pblocks[4] = s->pblocks[5];
2486 s->pblocks[5] = tmp;
2489 static int mpeg_field_start(MpegEncContext *s){
2490 AVCodecContext *avctx= s->avctx;
2491 Mpeg1Context *s1 = (Mpeg1Context*)s;
2493 /* start frame decoding */
2494 if(s->first_field || s->picture_structure==PICT_FRAME){
2495 if(MPV_frame_start(s, avctx) < 0)
2498 ff_er_frame_start(s);
2500 /* first check if we must repeat the frame */
2501 s->current_picture_ptr->repeat_pict = 0;
2502 if (s->repeat_first_field) {
2503 if (s->progressive_sequence) {
2504 if (s->top_field_first)
2505 s->current_picture_ptr->repeat_pict = 4;
2507 s->current_picture_ptr->repeat_pict = 2;
2508 } else if (s->progressive_frame) {
2509 s->current_picture_ptr->repeat_pict = 1;
2513 *s->current_picture_ptr->pan_scan= s1->pan_scan;
2514 }else{ //second field
2517 if(!s->current_picture_ptr){
2518 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
2523 s->current_picture.data[i] = s->current_picture_ptr->data[i];
2524 if(s->picture_structure == PICT_BOTTOM_FIELD){
2525 s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
2530 // MPV_frame_start will call this function too,
2531 // but we need to call it on every field
2532 if(s->avctx->xvmc_acceleration)
2533 XVMC_field_start(s,avctx);
2539 #define DECODE_SLICE_ERROR -1
2540 #define DECODE_SLICE_OK 0
2543 * decodes a slice. MpegEncContext.mb_y must be set to the MB row from the startcode
2544 * @return DECODE_SLICE_ERROR if the slice is damaged<br>
2545 * DECODE_SLICE_OK if this slice is ok<br>
2547 static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
2548 const uint8_t **buf, int buf_size)
2550 MpegEncContext *s = &s1->mpeg_enc_ctx;
2551 AVCodecContext *avctx= s->avctx;
2553 const int field_pic= s->picture_structure != PICT_FRAME;
2554 const int lowres= s->avctx->lowres;
2559 if (mb_y<<field_pic >= s->mb_height){
2560 av_log(s->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s->mb_height);
2564 init_get_bits(&s->gb, *buf, buf_size*8);
2566 ff_mpeg1_clean_buffers(s);
2567 s->interlaced_dct = 0;
2569 s->qscale = get_qscale(s);
2572 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
2576 /* extra slice info */
2577 while (get_bits1(&s->gb) != 0) {
2578 skip_bits(&s->gb, 8);
2584 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
2586 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
2593 /* otherwise, stuffing, nothing to do */
2599 if(s->mb_x >= (unsigned)s->mb_width){
2600 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
2604 s->resync_mb_x= s->mb_x;
2605 s->resync_mb_y= s->mb_y= mb_y;
2607 ff_init_block_index(s);
2609 if (s->mb_y==0 && s->mb_x==0 && (s->first_field || s->picture_structure==PICT_FRAME)) {
2610 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
2611 av_log(s->avctx, AV_LOG_DEBUG, "qp:%d fc:%2d%2d%2d%2d %s %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n",
2612 s->qscale, s->mpeg_f_code[0][0],s->mpeg_f_code[0][1],s->mpeg_f_code[1][0],s->mpeg_f_code[1][1],
2613 s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")),
2614 s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
2615 s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
2616 s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
2622 //one 1 we memcpy blocks in xvmcvideo
2623 if(s->avctx->xvmc_acceleration > 1)
2624 XVMC_init_block(s);//set s->block
2627 ret = mpeg_decode_mb(s, s->block);
2628 s->chroma_qscale= s->qscale;
2630 dprintf(s->avctx, "ret=%d\n", ret);
2634 if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
2635 const int wrap = field_pic ? 2*s->b8_stride : s->b8_stride;
2636 int xy = s->mb_x*2 + s->mb_y*2*wrap;
2637 int motion_x, motion_y, dir, i;
2638 if(field_pic && !s->first_field)
2642 for(dir=0; dir<2; dir++){
2643 if (s->mb_intra || (dir==1 && s->pict_type != B_TYPE)) {
2644 motion_x = motion_y = 0;
2645 }else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)){
2646 motion_x = s->mv[dir][0][0];
2647 motion_y = s->mv[dir][0][1];
2648 } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
2649 motion_x = s->mv[dir][i][0];
2650 motion_y = s->mv[dir][i][1];
2653 s->current_picture.motion_val[dir][xy ][0] = motion_x;
2654 s->current_picture.motion_val[dir][xy ][1] = motion_y;
2655 s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
2656 s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
2657 s->current_picture.ref_index [dir][xy ]=
2658 s->current_picture.ref_index [dir][xy + 1]= s->field_select[dir][i];
2659 assert(s->field_select[dir][i]==0 || s->field_select[dir][i]==1);
2665 s->dest[0] += 16 >> lowres;
2666 s->dest[1] += 16 >> (s->chroma_x_shift + lowres);
2667 s->dest[2] += 16 >> (s->chroma_x_shift + lowres);
2669 MPV_decode_mb(s, s->block);
2671 if (++s->mb_x >= s->mb_width) {
2672 const int mb_size= 16>>s->avctx->lowres;
2674 ff_draw_horiz_band(s, mb_size*s->mb_y, mb_size);
2679 if(s->mb_y<<field_pic >= s->mb_height){
2680 int left= s->gb.size_in_bits - get_bits_count(&s->gb);
2681 int is_d10= s->chroma_format==2 && s->pict_type==I_TYPE && avctx->profile==0 && avctx->level==5
2682 && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
2683 && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
2685 if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
2686 || (avctx->error_resilience >= FF_ER_AGGRESSIVE && left>8)){
2687 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
2693 ff_init_block_index(s);
2696 /* skip mb handling */
2697 if (s->mb_skip_run == -1) {
2698 /* read again increment */
2701 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
2703 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
2708 s->mb_skip_run += 33;
2709 }else if(code == 35){
2710 if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
2711 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
2714 goto eos; /* end of slice */
2716 /* otherwise, stuffing, nothing to do */
2718 s->mb_skip_run += code;
2724 eos: // end of slice
2725 *buf += get_bits_count(&s->gb)/8 - 1;
2726 //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
2730 static int slice_decode_thread(AVCodecContext *c, void *arg){
2731 MpegEncContext *s= arg;
2732 const uint8_t *buf= s->gb.buffer;
2733 int mb_y= s->start_mb_y;
2735 s->error_count= 3*(s->end_mb_y - s->start_mb_y)*s->mb_width;
2738 uint32_t start_code;
2741 ret= mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
2743 //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
2744 //ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, s->start_mb_y, s->end_mb_y, s->error_count);
2746 if(s->resync_mb_x>=0 && s->resync_mb_y>=0)
2747 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
2749 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);
2752 if(s->mb_y == s->end_mb_y)
2756 buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);
2757 mb_y= start_code - SLICE_MIN_START_CODE;
2758 if(mb_y < 0 || mb_y >= s->end_mb_y)
2762 return 0; //not reached
2766 * handles slice ends.
2767 * @return 1 if it seems to be the last slice of
2769 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
2771 Mpeg1Context *s1 = avctx->priv_data;
2772 MpegEncContext *s = &s1->mpeg_enc_ctx;
2774 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
2778 if(s->avctx->xvmc_acceleration)
2781 /* end of slice reached */
2782 if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
2785 s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
2791 if (s->pict_type == B_TYPE || s->low_delay) {
2792 *pict= *(AVFrame*)s->current_picture_ptr;
2793 ff_print_debug_info(s, pict);
2795 s->picture_number++;
2796 /* latency of 1 frame for I and P frames */
2797 /* XXX: use another variable than picture_number */
2798 if (s->last_picture_ptr != NULL) {
2799 *pict= *(AVFrame*)s->last_picture_ptr;
2800 ff_print_debug_info(s, pict);
2810 static int mpeg1_decode_sequence(AVCodecContext *avctx,
2811 const uint8_t *buf, int buf_size)
2813 Mpeg1Context *s1 = avctx->priv_data;
2814 MpegEncContext *s = &s1->mpeg_enc_ctx;
2818 init_get_bits(&s->gb, buf, buf_size*8);
2820 width = get_bits(&s->gb, 12);
2821 height = get_bits(&s->gb, 12);
2822 if (width <= 0 || height <= 0 ||
2823 (width % 2) != 0 || (height % 2) != 0)
2825 s->aspect_ratio_info= get_bits(&s->gb, 4);
2826 if (s->aspect_ratio_info == 0)
2828 s->frame_rate_index = get_bits(&s->gb, 4);
2829 if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
2831 s->bit_rate = get_bits(&s->gb, 18) * 400;
2832 if (get_bits1(&s->gb) == 0) /* marker */
2837 s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
2838 skip_bits(&s->gb, 1);
2841 if (get_bits1(&s->gb)) {
2843 v = get_bits(&s->gb, 8);
2845 av_log(s->avctx, AV_LOG_ERROR, "intra matrix damaged\n");
2848 j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
2849 s->intra_matrix[j] = v;
2850 s->chroma_intra_matrix[j] = v;
2853 dprintf(s->avctx, "intra matrix present\n");
2855 dprintf(s->avctx, " %d", s->intra_matrix[s->dsp.idct_permutation[i]]);
2856 dprintf(s->avctx, "\n");
2860 j = s->dsp.idct_permutation[i];
2861 v = ff_mpeg1_default_intra_matrix[i];
2862 s->intra_matrix[j] = v;
2863 s->chroma_intra_matrix[j] = v;
2866 if (get_bits1(&s->gb)) {
2868 v = get_bits(&s->gb, 8);
2870 av_log(s->avctx, AV_LOG_ERROR, "inter matrix damaged\n");
2873 j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
2874 s->inter_matrix[j] = v;
2875 s->chroma_inter_matrix[j] = v;
2878 dprintf(s->avctx, "non intra matrix present\n");
2880 dprintf(s->avctx, " %d", s->inter_matrix[s->dsp.idct_permutation[i]]);
2881 dprintf(s->avctx, "\n");
2885 int j= s->dsp.idct_permutation[i];
2886 v = ff_mpeg1_default_non_intra_matrix[i];
2887 s->inter_matrix[j] = v;
2888 s->chroma_inter_matrix[j] = v;
2892 if(show_bits(&s->gb, 23) != 0){
2893 av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
2897 /* we set mpeg2 parameters so that it emulates mpeg1 */
2898 s->progressive_sequence = 1;
2899 s->progressive_frame = 1;
2900 s->picture_structure = PICT_FRAME;
2901 s->frame_pred_frame_dct = 1;
2902 s->chroma_format = 1;
2903 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
2904 avctx->sub_id = 1; /* indicates mpeg1 */
2905 s->out_format = FMT_MPEG1;
2906 s->swap_uv = 0;//AFAIK VCR2 don't have SEQ_HEADER
2907 if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
2909 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
2910 av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
2911 s->avctx->rc_buffer_size, s->bit_rate);
2916 static int vcr2_init_sequence(AVCodecContext *avctx)
2918 Mpeg1Context *s1 = avctx->priv_data;
2919 MpegEncContext *s = &s1->mpeg_enc_ctx;
2922 /* start new mpeg1 context decoding */
2923 s->out_format = FMT_MPEG1;
2924 if (s1->mpeg_enc_ctx_allocated) {
2927 s->width = avctx->coded_width;
2928 s->height = avctx->coded_height;
2929 avctx->has_b_frames= 0; //true?
2932 if(avctx->xvmc_acceleration){
2933 avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
2935 avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_420);
2938 if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
2939 if( avctx->idct_algo == FF_IDCT_AUTO )
2940 avctx->idct_algo = FF_IDCT_SIMPLE;
2942 if (MPV_common_init(s) < 0)
2944 exchange_uv(s);//common init reset pblocks, so we swap them here
2945 s->swap_uv = 1;// in case of xvmc we need to swap uv for each MB
2946 s1->mpeg_enc_ctx_allocated = 1;
2949 int j= s->dsp.idct_permutation[i];
2950 v = ff_mpeg1_default_intra_matrix[i];
2951 s->intra_matrix[j] = v;
2952 s->chroma_intra_matrix[j] = v;
2954 v = ff_mpeg1_default_non_intra_matrix[i];
2955 s->inter_matrix[j] = v;
2956 s->chroma_inter_matrix[j] = v;
2959 s->progressive_sequence = 1;
2960 s->progressive_frame = 1;
2961 s->picture_structure = PICT_FRAME;
2962 s->frame_pred_frame_dct = 1;
2963 s->chroma_format = 1;
2964 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
2965 avctx->sub_id = 2; /* indicates mpeg2 */
2970 static void mpeg_decode_user_data(AVCodecContext *avctx,
2971 const uint8_t *buf, int buf_size)
2978 /* we parse the DTG active format information */
2980 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2994 avctx->dtg_active_format = p[0] & 0x0f;
2999 static void mpeg_decode_gop(AVCodecContext *avctx,
3000 const uint8_t *buf, int buf_size){
3001 Mpeg1Context *s1 = avctx->priv_data;
3002 MpegEncContext *s = &s1->mpeg_enc_ctx;
3004 int drop_frame_flag;
3005 int time_code_hours, time_code_minutes;
3006 int time_code_seconds, time_code_pictures;
3009 init_get_bits(&s->gb, buf, buf_size*8);
3011 drop_frame_flag = get_bits1(&s->gb);
3013 time_code_hours=get_bits(&s->gb,5);
3014 time_code_minutes = get_bits(&s->gb,6);
3015 skip_bits1(&s->gb);//marker bit
3016 time_code_seconds = get_bits(&s->gb,6);
3017 time_code_pictures = get_bits(&s->gb,6);
3019 /*broken_link indicate that after editing the
3020 reference frames of the first B-Frames after GOP I-Frame
3021 are missing (open gop)*/
3022 broken_link = get_bits1(&s->gb);
3024 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
3025 av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) broken_link=%d\n",
3026 time_code_hours, time_code_minutes, time_code_seconds,
3027 time_code_pictures, broken_link);
3030 * finds the end of the current frame in the bitstream.
3031 * @return the position of the first byte of the next frame, or -1
3033 int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
3036 uint32_t state= pc->state;
3039 if(!pc->frame_start_found){
3040 for(i=0; i<buf_size; i++){
3041 i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
3042 if(state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
3044 pc->frame_start_found=1;
3050 if(pc->frame_start_found){
3051 /* EOF considered as end of frame */
3054 for(; i<buf_size; i++){
3055 i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
3056 if((state&0xFFFFFF00) == 0x100){
3057 if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
3058 pc->frame_start_found=0;
3066 return END_NOT_FOUND;
3069 /* handle buffering and image synchronisation */
3070 static int mpeg_decode_frame(AVCodecContext *avctx,
3071 void *data, int *data_size,
3072 uint8_t *buf, int buf_size)
3074 Mpeg1Context *s = avctx->priv_data;
3075 const uint8_t *buf_end;
3076 const uint8_t *buf_ptr;
3077 uint32_t start_code;
3078 int ret, input_size;
3079 AVFrame *picture = data;
3080 MpegEncContext *s2 = &s->mpeg_enc_ctx;
3081 dprintf(avctx, "fill_buffer\n");
3083 if (buf_size == 0) {
3084 /* special case for last picture */
3085 if (s2->low_delay==0 && s2->next_picture_ptr) {
3086 *picture= *(AVFrame*)s2->next_picture_ptr;
3087 s2->next_picture_ptr= NULL;
3089 *data_size = sizeof(AVFrame);
3094 if(s2->flags&CODEC_FLAG_TRUNCATED){
3095 int next= ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size);
3097 if( ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
3102 buf_end = buf + buf_size;
3105 if (s->repeat_field % 2 == 1) {
3107 //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
3108 // s2->picture_number, s->repeat_field);
3109 if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
3110 *data_size = sizeof(AVPicture);
3116 if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == ff_get_fourcc("VCR2"))
3117 vcr2_init_sequence(avctx);
3122 /* find start next code */
3124 buf_ptr = ff_find_start_code(buf_ptr,buf_end, &start_code);
3125 if (start_code > 0x1ff){
3126 if(s2->pict_type != B_TYPE || avctx->skip_frame <= AVDISCARD_DEFAULT){
3127 if(avctx->thread_count > 1){
3130 avctx->execute(avctx, slice_decode_thread, (void**)&(s2->thread_context[0]), NULL, s->slice_count);
3131 for(i=0; i<s->slice_count; i++)
3132 s2->error_count += s2->thread_context[i]->error_count;
3134 if (slice_end(avctx, picture)) {
3135 if(s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
3136 *data_size = sizeof(AVPicture);
3139 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
3142 input_size = buf_end - buf_ptr;
3144 if(avctx->debug & FF_DEBUG_STARTCODE){
3145 av_log(avctx, AV_LOG_DEBUG, "%3X at %zd left %d\n", start_code, buf_ptr-buf, input_size);
3148 /* prepare data for next start code */
3149 switch(start_code) {
3150 case SEQ_START_CODE:
3151 mpeg1_decode_sequence(avctx, buf_ptr,
3155 case PICTURE_START_CODE:
3156 /* we have a complete image : we try to decompress it */
3157 mpeg1_decode_picture(avctx,
3158 buf_ptr, input_size);
3160 case EXT_START_CODE:
3161 mpeg_decode_extension(avctx,
3162 buf_ptr, input_size);
3164 case USER_START_CODE:
3165 mpeg_decode_user_data(avctx,
3166 buf_ptr, input_size);
3168 case GOP_START_CODE:
3170 mpeg_decode_gop(avctx,
3171 buf_ptr, input_size);
3174 if (start_code >= SLICE_MIN_START_CODE &&
3175 start_code <= SLICE_MAX_START_CODE) {
3176 int mb_y= start_code - SLICE_MIN_START_CODE;
3178 if(s2->last_picture_ptr==NULL){
3179 /* Skip B-frames if we do not have reference frames. */
3180 if(s2->pict_type==B_TYPE) break;
3181 /* Skip P-frames if we do not have reference frame no valid header. */
3182 // if(s2->pict_type==P_TYPE && s2->first_field && !s2->first_slice) break;
3184 /* Skip B-frames if we are in a hurry. */
3185 if(avctx->hurry_up && s2->pict_type==B_TYPE) break;
3186 if( (avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type==B_TYPE)
3187 ||(avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type!=I_TYPE)
3188 || avctx->skip_frame >= AVDISCARD_ALL)
3190 /* Skip everything if we are in a hurry>=5. */
3191 if(avctx->hurry_up>=5) break;
3193 if (!s->mpeg_enc_ctx_allocated) break;
3195 if(s2->codec_id == CODEC_ID_MPEG2VIDEO){
3196 if(mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
3200 if(s2->first_slice){
3202 if(mpeg_field_start(s2) < 0)
3205 if(!s2->current_picture_ptr){
3206 av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
3210 if(avctx->thread_count > 1){
3211 int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count;
3212 if(threshold <= mb_y){
3213 MpegEncContext *thread_context= s2->thread_context[s->slice_count];
3215 thread_context->start_mb_y= mb_y;
3216 thread_context->end_mb_y = s2->mb_height;
3218 s2->thread_context[s->slice_count-1]->end_mb_y= mb_y;
3219 ff_update_duplicate_context(thread_context, s2);
3221 init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
3224 buf_ptr += 2; //FIXME add minimum num of bytes per slice
3226 ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
3230 if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
3231 ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
3233 ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x-1, s2->mb_y, AC_END|DC_END|MV_END);
3242 static int mpeg_decode_end(AVCodecContext *avctx)
3244 Mpeg1Context *s = avctx->priv_data;
3246 if (s->mpeg_enc_ctx_allocated)
3247 MPV_common_end(&s->mpeg_enc_ctx);
3251 AVCodec mpeg1video_decoder = {
3254 CODEC_ID_MPEG1VIDEO,
3255 sizeof(Mpeg1Context),
3260 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
3261 .flush= ff_mpeg_flush,
3264 AVCodec mpeg2video_decoder = {
3267 CODEC_ID_MPEG2VIDEO,
3268 sizeof(Mpeg1Context),
3273 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
3274 .flush= ff_mpeg_flush,
3278 AVCodec mpegvideo_decoder = {
3281 CODEC_ID_MPEG2VIDEO,
3282 sizeof(Mpeg1Context),
3287 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
3288 .flush= ff_mpeg_flush,
3291 #ifdef CONFIG_ENCODERS
3293 AVCodec mpeg1video_encoder = {
3296 CODEC_ID_MPEG1VIDEO,
3297 sizeof(MpegEncContext),
3301 .supported_framerates= ff_frame_rate_tab+1,
3302 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
3303 .capabilities= CODEC_CAP_DELAY,
3306 AVCodec mpeg2video_encoder = {
3309 CODEC_ID_MPEG2VIDEO,
3310 sizeof(MpegEncContext),
3314 .supported_framerates= ff_frame_rate_tab+1,
3315 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, -1},
3316 .capabilities= CODEC_CAP_DELAY,
3321 static int mpeg_mc_decode_init(AVCodecContext *avctx){
3324 if( avctx->thread_count > 1)
3326 if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
3328 if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
3329 dprintf(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
3331 mpeg_decode_init(avctx);
3332 s = avctx->priv_data;
3334 avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
3335 avctx->xvmc_acceleration = 2;//2 - the blocks are packed!
3340 AVCodec mpeg_xvmc_decoder = {
3343 CODEC_ID_MPEG2VIDEO_XVMC,
3344 sizeof(Mpeg1Context),
3345 mpeg_mc_decode_init,
3349 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
3350 .flush= ff_mpeg_flush,
3355 /* this is ugly i know, but the alternative is too make
3356 hundreds of vars global and prefix them with ff_mpeg1_
3357 which is far uglier. */