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"
40 #define SEQ_END_CODE 0x000001b7
41 #define SEQ_START_CODE 0x000001b3
42 #define GOP_START_CODE 0x000001b8
43 #define PICTURE_START_CODE 0x00000100
44 #define SLICE_MIN_START_CODE 0x00000101
45 #define SLICE_MAX_START_CODE 0x000001af
46 #define EXT_START_CODE 0x000001b5
47 #define USER_START_CODE 0x000001b2
51 #define MBINCR_VLC_BITS 9
52 #define MB_PAT_VLC_BITS 9
53 #define MB_PTYPE_VLC_BITS 6
54 #define MB_BTYPE_VLC_BITS 6
55 #define TEX_VLC_BITS 9
57 #ifdef CONFIG_ENCODERS
58 static void mpeg1_encode_block(MpegEncContext *s,
61 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code); // RAL: f_code parameter added
62 #endif //CONFIG_ENCODERS
63 static inline int mpeg1_decode_block_inter(MpegEncContext *s,
66 static inline int mpeg1_decode_block_intra(MpegEncContext *s,
69 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n);
70 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
73 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
76 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n);
77 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n);
78 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
79 static void exchange_uv(MpegEncContext *s);
82 extern int XVMC_field_start(MpegEncContext *s, AVCodecContext *avctx);
83 extern int XVMC_field_end(MpegEncContext *s);
84 extern void XVMC_pack_pblocks(MpegEncContext *s,int cbp);
85 extern void XVMC_init_block(MpegEncContext *s);//set s->block
88 const enum PixelFormat pixfmt_yuv_420[]= {PIX_FMT_YUV420P,-1};
89 const enum PixelFormat pixfmt_yuv_422[]= {PIX_FMT_YUV422P,-1};
90 const enum PixelFormat pixfmt_yuv_444[]= {PIX_FMT_YUV444P,-1};
91 const enum PixelFormat pixfmt_xvmc_mpg2_420[] = {
92 PIX_FMT_XVMC_MPEG2_IDCT,
93 PIX_FMT_XVMC_MPEG2_MC,
95 #ifdef CONFIG_ENCODERS
96 static uint8_t (*mv_penalty)[MAX_MV*2+1]= NULL;
97 static uint8_t fcode_tab[MAX_MV*2+1];
99 static uint8_t uni_mpeg1_ac_vlc_len [64*64*2];
100 static uint8_t uni_mpeg2_ac_vlc_len [64*64*2];
102 /* simple include everything table for dc, first byte is bits number next 3 are code*/
103 static uint32_t mpeg1_lum_dc_uni[512];
104 static uint32_t mpeg1_chr_dc_uni[512];
106 static uint8_t mpeg1_index_run[2][64];
107 static int8_t mpeg1_max_level[2][64];
108 #endif //CONFIG_ENCODERS
110 static void init_2d_vlc_rl(RLTable *rl, int use_static)
114 init_vlc(&rl->vlc, TEX_VLC_BITS, rl->n + 2,
115 &rl->table_vlc[0][1], 4, 2,
116 &rl->table_vlc[0][0], 4, 2, use_static);
119 rl->rl_vlc[0]= av_mallocz_static(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
121 rl->rl_vlc[0]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
123 for(i=0; i<rl->vlc.table_size; i++){
124 int code= rl->vlc.table[i][0];
125 int len = rl->vlc.table[i][1];
128 if(len==0){ // illegal code
131 }else if(len<0){ //more bits needed
135 if(code==rl->n){ //esc
138 }else if(code==rl->n+1){ //eob
142 run= rl->table_run [code] + 1;
143 level= rl->table_level[code];
146 rl->rl_vlc[0][i].len= len;
147 rl->rl_vlc[0][i].level= level;
148 rl->rl_vlc[0][i].run= run;
152 #ifdef CONFIG_ENCODERS
153 static void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len){
156 for(i=0; i<128; i++){
159 for(run=0; run<64; run++){
162 int alevel= FFABS(level);
163 int sign= (level>>31)&1;
165 if (alevel > rl->max_level[0][run])
168 code= rl->index_run[0][run] + alevel - 1;
170 if (code < 111 /* rl->n */) {
171 /* store the vlc & sign at once */
172 len= rl->table_vlc[code][1]+1;
173 bits= (rl->table_vlc[code][0]<<1) + sign;
175 len= rl->table_vlc[111/*rl->n*/][1]+6;
176 bits= rl->table_vlc[111/*rl->n*/][0]<<6;
186 bits|= 0x8001 + level + 255;
188 bits|= level & 0xffff;
193 uni_ac_vlc_len [UNI_AC_ENC_INDEX(run, i)]= len;
199 static int find_frame_rate_index(MpegEncContext *s){
201 int64_t dmin= INT64_MAX;
205 int64_t n0= 1001LL/ff_frame_rate_tab[i].den*ff_frame_rate_tab[i].num*s->avctx->time_base.num;
206 int64_t n1= 1001LL*s->avctx->time_base.den;
207 if(s->avctx->strict_std_compliance > FF_COMPLIANCE_INOFFICIAL && i>=9) break;
212 s->frame_rate_index= i;
221 static int encode_init(AVCodecContext *avctx)
223 MpegEncContext *s = avctx->priv_data;
225 if(MPV_encode_init(avctx) < 0)
228 if(find_frame_rate_index(s) < 0){
229 if(s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
230 av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n", avctx->time_base.den, avctx->time_base.num);
233 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);
237 if(avctx->profile == FF_PROFILE_UNKNOWN)
238 avctx->profile = s->chroma_format == CHROMA_420 ? 4 : 0;
240 if(avctx->level == FF_LEVEL_UNKNOWN)
241 avctx->level = s->chroma_format == CHROMA_420 ? 8 : 5;
246 static void put_header(MpegEncContext *s, int header)
248 align_put_bits(&s->pb);
249 put_bits(&s->pb, 16, header>>16);
250 put_bits(&s->pb, 16, header&0xFFFF);
253 /* put sequence header if needed */
254 static void mpeg1_encode_sequence_header(MpegEncContext *s)
256 unsigned int vbv_buffer_size;
260 float best_aspect_error= 1E10;
261 float aspect_ratio= av_q2d(s->avctx->sample_aspect_ratio);
262 int constraint_parameter_flag;
264 if(aspect_ratio==0.0) aspect_ratio= 1.0; //pixel aspect 1:1 (VGA)
266 if (s->current_picture.key_frame) {
267 AVRational framerate= ff_frame_rate_tab[s->frame_rate_index];
269 /* mpeg1 header repeated every gop */
270 put_header(s, SEQ_START_CODE);
272 put_bits(&s->pb, 12, s->width);
273 put_bits(&s->pb, 12, s->height);
276 float error= aspect_ratio;
277 if(s->codec_id == CODEC_ID_MPEG1VIDEO || i <=1)
278 error-= 1.0/mpeg1_aspect[i];
280 error-= av_q2d(mpeg2_aspect[i])*s->height/s->width;
284 if(error < best_aspect_error){
285 best_aspect_error= error;
286 s->aspect_ratio_info= i;
290 put_bits(&s->pb, 4, s->aspect_ratio_info);
291 put_bits(&s->pb, 4, s->frame_rate_index);
293 if(s->avctx->rc_max_rate){
294 v = (s->avctx->rc_max_rate + 399) / 400;
295 if (v > 0x3ffff && s->codec_id == CODEC_ID_MPEG1VIDEO)
301 if(s->avctx->rc_buffer_size)
302 vbv_buffer_size = s->avctx->rc_buffer_size;
304 /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */
305 vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
306 vbv_buffer_size= (vbv_buffer_size + 16383) / 16384;
308 put_bits(&s->pb, 18, v & 0x3FFFF);
309 put_bits(&s->pb, 1, 1); /* marker */
310 put_bits(&s->pb, 10, vbv_buffer_size & 0x3FF);
312 constraint_parameter_flag=
313 s->width <= 768 && s->height <= 576 &&
314 s->mb_width * s->mb_height <= 396 &&
315 s->mb_width * s->mb_height * framerate.num <= framerate.den*396*25 &&
316 framerate.num <= framerate.den*30 &&
317 s->avctx->me_range && s->avctx->me_range < 128 &&
318 vbv_buffer_size <= 20 &&
320 s->codec_id == CODEC_ID_MPEG1VIDEO;
322 put_bits(&s->pb, 1, constraint_parameter_flag);
324 ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
325 ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
327 if(s->codec_id == CODEC_ID_MPEG2VIDEO){
328 put_header(s, EXT_START_CODE);
329 put_bits(&s->pb, 4, 1); //seq ext
331 put_bits(&s->pb, 1, s->chroma_format == CHROMA_422); //escx
333 put_bits(&s->pb, 3, s->avctx->profile); //profile
334 put_bits(&s->pb, 4, s->avctx->level); //level
336 put_bits(&s->pb, 1, s->progressive_sequence);
337 put_bits(&s->pb, 2, s->chroma_format);
338 put_bits(&s->pb, 2, 0); //horizontal size ext
339 put_bits(&s->pb, 2, 0); //vertical size ext
340 put_bits(&s->pb, 12, v>>18); //bitrate ext
341 put_bits(&s->pb, 1, 1); //marker
342 put_bits(&s->pb, 8, vbv_buffer_size >>10); //vbv buffer ext
343 put_bits(&s->pb, 1, s->low_delay);
344 put_bits(&s->pb, 2, 0); // frame_rate_ext_n
345 put_bits(&s->pb, 5, 0); // frame_rate_ext_d
348 put_header(s, GOP_START_CODE);
349 put_bits(&s->pb, 1, 0); /* do drop frame */
350 /* time code : we must convert from the real frame rate to a
351 fake mpeg frame rate in case of low frame rate */
352 fps = (framerate.num + framerate.den/2)/ framerate.den;
353 time_code = s->current_picture_ptr->coded_picture_number;
355 s->gop_picture_number = time_code;
356 put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
357 put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
358 put_bits(&s->pb, 1, 1);
359 put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
360 put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
361 put_bits(&s->pb, 1, !!(s->flags & CODEC_FLAG_CLOSED_GOP));
362 put_bits(&s->pb, 1, 0); /* broken link */
366 static inline void encode_mb_skip_run(MpegEncContext *s, int run){
368 put_bits(&s->pb, 11, 0x008);
371 put_bits(&s->pb, mbAddrIncrTable[run][1],
372 mbAddrIncrTable[run][0]);
374 #endif //CONFIG_ENCODERS
376 static void common_init(MpegEncContext *s)
380 s->c_dc_scale_table= mpeg2_dc_scale_table[s->intra_dc_precision];
384 void ff_mpeg1_clean_buffers(MpegEncContext *s){
385 s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
386 s->last_dc[1] = s->last_dc[0];
387 s->last_dc[2] = s->last_dc[0];
388 memset(s->last_mv, 0, sizeof(s->last_mv));
391 #ifdef CONFIG_ENCODERS
393 void ff_mpeg1_encode_slice_header(MpegEncContext *s){
394 put_header(s, SLICE_MIN_START_CODE + s->mb_y);
395 put_bits(&s->pb, 5, s->qscale); /* quantizer scale */
396 put_bits(&s->pb, 1, 0); /* slice extra information */
399 void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
401 mpeg1_encode_sequence_header(s);
403 /* mpeg1 picture header */
404 put_header(s, PICTURE_START_CODE);
405 /* temporal reference */
407 // RAL: s->picture_number instead of s->fake_picture_number
408 put_bits(&s->pb, 10, (s->picture_number -
409 s->gop_picture_number) & 0x3ff);
410 put_bits(&s->pb, 3, s->pict_type);
412 s->vbv_delay_ptr= s->pb.buf + put_bits_count(&s->pb)/8;
413 put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
415 // RAL: Forward f_code also needed for B frames
416 if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
417 put_bits(&s->pb, 1, 0); /* half pel coordinates */
418 if(s->codec_id == CODEC_ID_MPEG1VIDEO)
419 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
421 put_bits(&s->pb, 3, 7); /* forward_f_code */
424 // RAL: Backward f_code necessary for B frames
425 if (s->pict_type == B_TYPE) {
426 put_bits(&s->pb, 1, 0); /* half pel coordinates */
427 if(s->codec_id == CODEC_ID_MPEG1VIDEO)
428 put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
430 put_bits(&s->pb, 3, 7); /* backward_f_code */
433 put_bits(&s->pb, 1, 0); /* extra bit picture */
435 s->frame_pred_frame_dct = 1;
436 if(s->codec_id == CODEC_ID_MPEG2VIDEO){
437 put_header(s, EXT_START_CODE);
438 put_bits(&s->pb, 4, 8); //pic ext
439 if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
440 put_bits(&s->pb, 4, s->f_code);
441 put_bits(&s->pb, 4, s->f_code);
443 put_bits(&s->pb, 8, 255);
445 if (s->pict_type == B_TYPE) {
446 put_bits(&s->pb, 4, s->b_code);
447 put_bits(&s->pb, 4, s->b_code);
449 put_bits(&s->pb, 8, 255);
451 put_bits(&s->pb, 2, s->intra_dc_precision);
453 assert(s->picture_structure == PICT_FRAME);
454 put_bits(&s->pb, 2, s->picture_structure);
455 if (s->progressive_sequence) {
456 put_bits(&s->pb, 1, 0); /* no repeat */
458 put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
460 /* XXX: optimize the generation of this flag with entropy
462 s->frame_pred_frame_dct = s->progressive_sequence;
464 put_bits(&s->pb, 1, s->frame_pred_frame_dct);
465 put_bits(&s->pb, 1, s->concealment_motion_vectors);
466 put_bits(&s->pb, 1, s->q_scale_type);
467 put_bits(&s->pb, 1, s->intra_vlc_format);
468 put_bits(&s->pb, 1, s->alternate_scan);
469 put_bits(&s->pb, 1, s->repeat_first_field);
470 s->progressive_frame = s->progressive_sequence;
471 put_bits(&s->pb, 1, s->chroma_format == CHROMA_420 ? s->progressive_frame : 0); /* chroma_420_type */
472 put_bits(&s->pb, 1, s->progressive_frame);
473 put_bits(&s->pb, 1, 0); //composite_display_flag
475 if(s->flags & CODEC_FLAG_SVCD_SCAN_OFFSET){
478 put_header(s, USER_START_CODE);
479 for(i=0; i<sizeof(svcd_scan_offset_placeholder); i++){
480 put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
485 ff_mpeg1_encode_slice_header(s);
488 static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
489 int has_mv, int field_motion)
491 put_bits(&s->pb, n, bits);
492 if (!s->frame_pred_frame_dct) {
494 put_bits(&s->pb, 2, 2 - field_motion); /* motion_type: frame/field */
495 put_bits(&s->pb, 1, s->interlaced_dct);
499 static always_inline void mpeg1_encode_mb_internal(MpegEncContext *s,
500 DCTELEM block[6][64],
501 int motion_x, int motion_y,
505 const int mb_x = s->mb_x;
506 const int mb_y = s->mb_y;
507 const int first_mb= mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
511 for(i=0;i<mb_block_count;i++) {
512 if (s->block_last_index[i] >= 0)
513 cbp |= 1 << (mb_block_count - 1 - i);
516 if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
517 (mb_x != s->mb_width - 1 || (mb_y != s->mb_height - 1 && s->codec_id == CODEC_ID_MPEG1VIDEO)) &&
518 ((s->pict_type == P_TYPE && (motion_x | motion_y) == 0) ||
519 (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) |
520 ((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))) {
522 s->qscale -= s->dquant;
526 if(s->pict_type == P_TYPE){
527 s->last_mv[0][1][0]= s->last_mv[0][0][0]=
528 s->last_mv[0][1][1]= s->last_mv[0][0][1]= 0;
532 assert(s->mb_skip_run == 0);
533 encode_mb_skip_run(s, s->mb_x);
535 encode_mb_skip_run(s, s->mb_skip_run);
538 if (s->pict_type == I_TYPE) {
539 if(s->dquant && cbp){
540 put_mb_modes(s, 2, 1, 0, 0); /* macroblock_type : macroblock_quant = 1 */
541 put_bits(&s->pb, 5, s->qscale);
543 put_mb_modes(s, 1, 1, 0, 0); /* macroblock_type : macroblock_quant = 0 */
544 s->qscale -= s->dquant;
546 s->misc_bits+= get_bits_diff(s);
548 } else if (s->mb_intra) {
549 if(s->dquant && cbp){
550 put_mb_modes(s, 6, 0x01, 0, 0);
551 put_bits(&s->pb, 5, s->qscale);
553 put_mb_modes(s, 5, 0x03, 0, 0);
554 s->qscale -= s->dquant;
556 s->misc_bits+= get_bits_diff(s);
558 memset(s->last_mv, 0, sizeof(s->last_mv));
559 } else if (s->pict_type == P_TYPE) {
560 if(s->mv_type == MV_TYPE_16X16){
562 if ((motion_x|motion_y) == 0) {
564 put_mb_modes(s, 5, 1, 0, 0); /* macroblock_pattern & quant */
565 put_bits(&s->pb, 5, s->qscale);
567 put_mb_modes(s, 2, 1, 0, 0); /* macroblock_pattern only */
569 s->misc_bits+= get_bits_diff(s);
572 put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
573 put_bits(&s->pb, 5, s->qscale);
575 put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
577 s->misc_bits+= get_bits_diff(s);
578 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
579 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
580 s->mv_bits+= get_bits_diff(s);
583 put_bits(&s->pb, 3, 1); /* motion only */
584 if (!s->frame_pred_frame_dct)
585 put_bits(&s->pb, 2, 2); /* motion_type: frame */
586 s->misc_bits+= get_bits_diff(s);
587 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
588 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
589 s->qscale -= s->dquant;
590 s->mv_bits+= get_bits_diff(s);
592 s->last_mv[0][1][0]= s->last_mv[0][0][0]= motion_x;
593 s->last_mv[0][1][1]= s->last_mv[0][0][1]= motion_y;
595 assert(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);
599 put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
600 put_bits(&s->pb, 5, s->qscale);
602 put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
605 put_bits(&s->pb, 3, 1); /* motion only */
606 put_bits(&s->pb, 2, 1); /* motion_type: field */
607 s->qscale -= s->dquant;
609 s->misc_bits+= get_bits_diff(s);
611 put_bits(&s->pb, 1, s->field_select[0][i]);
612 mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
613 mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
614 s->last_mv[0][i][0]= s->mv[0][i][0];
615 s->last_mv[0][i][1]= 2*s->mv[0][i][1];
617 s->mv_bits+= get_bits_diff(s);
620 if (s->chroma_y_shift) {
621 put_bits(&s->pb, mbPatTable[cbp][1], mbPatTable[cbp][0]);
623 put_bits(&s->pb, mbPatTable[cbp>>2][1], mbPatTable[cbp>>2][0]);
624 put_bits(&s->pb, 2, cbp & 3);
629 static const int mb_type_len[4]={0,3,4,2}; //bak,for,bi
631 if(s->mv_type == MV_TYPE_16X16){
632 if (cbp){ // With coded bloc pattern
634 if(s->mv_dir == MV_DIR_FORWARD)
635 put_mb_modes(s, 6, 3, 1, 0);
637 put_mb_modes(s, mb_type_len[s->mv_dir]+3, 2, 1, 0);
638 put_bits(&s->pb, 5, s->qscale);
640 put_mb_modes(s, mb_type_len[s->mv_dir], 3, 1, 0);
642 }else{ // No coded bloc pattern
643 put_bits(&s->pb, mb_type_len[s->mv_dir], 2);
644 if (!s->frame_pred_frame_dct)
645 put_bits(&s->pb, 2, 2); /* motion_type: frame */
646 s->qscale -= s->dquant;
648 s->misc_bits += get_bits_diff(s);
649 if (s->mv_dir&MV_DIR_FORWARD){
650 mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
651 mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
652 s->last_mv[0][0][0]=s->last_mv[0][1][0]= s->mv[0][0][0];
653 s->last_mv[0][0][1]=s->last_mv[0][1][1]= s->mv[0][0][1];
656 if (s->mv_dir&MV_DIR_BACKWARD){
657 mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
658 mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
659 s->last_mv[1][0][0]=s->last_mv[1][1][0]= s->mv[1][0][0];
660 s->last_mv[1][0][1]=s->last_mv[1][1][1]= s->mv[1][0][1];
664 assert(s->mv_type == MV_TYPE_FIELD);
665 assert(!s->frame_pred_frame_dct);
666 if (cbp){ // With coded bloc pattern
668 if(s->mv_dir == MV_DIR_FORWARD)
669 put_mb_modes(s, 6, 3, 1, 1);
671 put_mb_modes(s, mb_type_len[s->mv_dir]+3, 2, 1, 1);
672 put_bits(&s->pb, 5, s->qscale);
674 put_mb_modes(s, mb_type_len[s->mv_dir], 3, 1, 1);
676 }else{ // No coded bloc pattern
677 put_bits(&s->pb, mb_type_len[s->mv_dir], 2);
678 put_bits(&s->pb, 2, 1); /* motion_type: field */
679 s->qscale -= s->dquant;
681 s->misc_bits += get_bits_diff(s);
682 if (s->mv_dir&MV_DIR_FORWARD){
684 put_bits(&s->pb, 1, s->field_select[0][i]);
685 mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
686 mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
687 s->last_mv[0][i][0]= s->mv[0][i][0];
688 s->last_mv[0][i][1]= 2*s->mv[0][i][1];
692 if (s->mv_dir&MV_DIR_BACKWARD){
694 put_bits(&s->pb, 1, s->field_select[1][i]);
695 mpeg1_encode_motion(s, s->mv[1][i][0] - s->last_mv[1][i][0] , s->b_code);
696 mpeg1_encode_motion(s, s->mv[1][i][1] - (s->last_mv[1][i][1]>>1), s->b_code);
697 s->last_mv[1][i][0]= s->mv[1][i][0];
698 s->last_mv[1][i][1]= 2*s->mv[1][i][1];
703 s->mv_bits += get_bits_diff(s);
705 if (s->chroma_y_shift) {
706 put_bits(&s->pb, mbPatTable[cbp][1], mbPatTable[cbp][0]);
708 put_bits(&s->pb, mbPatTable[cbp>>2][1], mbPatTable[cbp>>2][0]);
709 put_bits(&s->pb, 2, cbp & 3);
713 for(i=0;i<mb_block_count;i++) {
714 if (cbp & (1 << (mb_block_count - 1 - i))) {
715 mpeg1_encode_block(s, block[i], i);
720 s->i_tex_bits+= get_bits_diff(s);
722 s->p_tex_bits+= get_bits_diff(s);
726 void mpeg1_encode_mb(MpegEncContext *s, DCTELEM block[6][64], int motion_x, int motion_y)
728 if (s->chroma_format == CHROMA_420) mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);
729 else mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
732 // RAL: Parameter added: f_or_b_code
733 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
735 int code, bit_size, l, bits, range, sign;
741 mbMotionVectorTable[0][1],
742 mbMotionVectorTable[0][0]);
744 bit_size = f_or_b_code - 1;
745 range = 1 << bit_size;
746 /* modulo encoding */
747 l= INT_BIT - 5 - bit_size;
752 code = (val >> bit_size) + 1;
753 bits = val & (range - 1);
758 code = (val >> bit_size) + 1;
759 bits = val & (range - 1);
763 assert(code > 0 && code <= 16);
766 mbMotionVectorTable[code][1],
767 mbMotionVectorTable[code][0]);
769 put_bits(&s->pb, 1, sign);
771 put_bits(&s->pb, bit_size, bits);
776 void ff_mpeg1_encode_init(MpegEncContext *s)
788 init_rl(&rl_mpeg1, 1);
789 if(s->intra_vlc_format)
790 init_rl(&rl_mpeg2, 1);
794 mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i];
795 mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i];
798 init_uni_ac_vlc(&rl_mpeg1, uni_mpeg1_ac_vlc_len);
799 if(s->intra_vlc_format)
800 init_uni_ac_vlc(&rl_mpeg2, uni_mpeg2_ac_vlc_len);
802 /* build unified dc encoding tables */
803 for(i=-255; i<256; i++)
811 index = av_log2(2*adiff);
813 bits= vlc_dc_lum_bits[index] + index;
814 code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
815 mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
817 bits= vlc_dc_chroma_bits[index] + index;
818 code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
819 mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
822 mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
824 for(f_code=1; f_code<=MAX_FCODE; f_code++){
825 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
828 if(mv==0) len= mbMotionVectorTable[0][1];
830 int val, bit_size, range, code;
832 bit_size = f_code - 1;
833 range = 1 << bit_size;
839 code = (val >> bit_size) + 1;
841 len= mbMotionVectorTable[code][1] + 1 + bit_size;
843 len= mbMotionVectorTable[16][1] + 2 + bit_size;
847 mv_penalty[f_code][mv+MAX_MV]= len;
852 for(f_code=MAX_FCODE; f_code>0; f_code--){
853 for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
854 fcode_tab[mv+MAX_MV]= f_code;
858 s->me.mv_penalty= mv_penalty;
859 s->fcode_tab= fcode_tab;
860 if(s->codec_id == CODEC_ID_MPEG1VIDEO){
867 if (s->intra_vlc_format) {
868 s->intra_ac_vlc_length=
869 s->intra_ac_vlc_last_length= uni_mpeg2_ac_vlc_len;
871 s->intra_ac_vlc_length=
872 s->intra_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
874 s->inter_ac_vlc_length=
875 s->inter_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
878 static inline void encode_dc(MpegEncContext *s, int diff, int component)
880 if(((unsigned) (diff+255)) >= 511){
884 index= av_log2_16bit(-2*diff);
887 index= av_log2_16bit(2*diff);
889 if (component == 0) {
892 vlc_dc_lum_bits[index] + index,
893 (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1)));
897 vlc_dc_chroma_bits[index] + index,
898 (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1)));
901 if (component == 0) {
904 mpeg1_lum_dc_uni[diff+255]&0xFF,
905 mpeg1_lum_dc_uni[diff+255]>>8);
909 mpeg1_chr_dc_uni[diff+255]&0xFF,
910 mpeg1_chr_dc_uni[diff+255]>>8);
915 static void mpeg1_encode_block(MpegEncContext *s,
919 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
921 const uint16_t (*table_vlc)[2] = rl_mpeg1.table_vlc;
923 last_index = s->block_last_index[n];
927 component = (n <= 3 ? 0 : (n&1) + 1);
928 dc = block[0]; /* overflow is impossible */
929 diff = dc - s->last_dc[component];
930 encode_dc(s, diff, component);
931 s->last_dc[component] = dc;
933 if (s->intra_vlc_format)
934 table_vlc = rl_mpeg2.table_vlc;
936 /* encode the first coefficient : needs to be done here because
937 it is handled slightly differently */
939 if (abs(level) == 1) {
940 code = ((uint32_t)level >> 31); /* the sign bit */
941 put_bits(&s->pb, 2, code | 0x02);
950 /* now quantify & encode AC coefs */
951 last_non_zero = i - 1;
953 for(;i<=last_index;i++) {
954 j = s->intra_scantable.permutated[i];
959 dprintf("level[%d]=%d\n", i, level);
961 /* encode using VLC */
963 run = i - last_non_zero - 1;
966 MASK_ABS(sign, alevel)
969 if (alevel <= mpeg1_max_level[0][run]){
970 code= mpeg1_index_run[0][run] + alevel - 1;
971 /* store the vlc & sign at once */
972 put_bits(&s->pb, table_vlc[code][1]+1, (table_vlc[code][0]<<1) + sign);
974 /* escape seems to be pretty rare <5% so i dont optimize it */
975 put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
976 /* escape: only clip in this case */
977 put_bits(&s->pb, 6, run);
978 if(s->codec_id == CODEC_ID_MPEG1VIDEO){
980 put_bits(&s->pb, 8, level & 0xff);
983 put_bits(&s->pb, 16, 0x8001 + level + 255);
985 put_bits(&s->pb, 16, level & 0xffff);
989 put_bits(&s->pb, 12, level & 0xfff);
996 put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
998 #endif //CONFIG_ENCODERS
1000 /******************************************/
1003 static VLC dc_lum_vlc;
1004 static VLC dc_chroma_vlc;
1006 static VLC mbincr_vlc;
1007 static VLC mb_ptype_vlc;
1008 static VLC mb_btype_vlc;
1009 static VLC mb_pat_vlc;
1011 static void init_vlcs(void)
1013 static int done = 0;
1018 init_vlc(&dc_lum_vlc, DC_VLC_BITS, 12,
1019 vlc_dc_lum_bits, 1, 1,
1020 vlc_dc_lum_code, 2, 2, 1);
1021 init_vlc(&dc_chroma_vlc, DC_VLC_BITS, 12,
1022 vlc_dc_chroma_bits, 1, 1,
1023 vlc_dc_chroma_code, 2, 2, 1);
1024 init_vlc(&mv_vlc, MV_VLC_BITS, 17,
1025 &mbMotionVectorTable[0][1], 2, 1,
1026 &mbMotionVectorTable[0][0], 2, 1, 1);
1027 init_vlc(&mbincr_vlc, MBINCR_VLC_BITS, 36,
1028 &mbAddrIncrTable[0][1], 2, 1,
1029 &mbAddrIncrTable[0][0], 2, 1, 1);
1030 init_vlc(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
1031 &mbPatTable[0][1], 2, 1,
1032 &mbPatTable[0][0], 2, 1, 1);
1034 init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
1035 &table_mb_ptype[0][1], 2, 1,
1036 &table_mb_ptype[0][0], 2, 1, 1);
1037 init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
1038 &table_mb_btype[0][1], 2, 1,
1039 &table_mb_btype[0][0], 2, 1, 1);
1040 init_rl(&rl_mpeg1, 1);
1041 init_rl(&rl_mpeg2, 1);
1043 init_2d_vlc_rl(&rl_mpeg1, 1);
1044 init_2d_vlc_rl(&rl_mpeg2, 1);
1048 static inline int get_dmv(MpegEncContext *s)
1050 if(get_bits1(&s->gb))
1051 return 1 - (get_bits1(&s->gb) << 1);
1056 static inline int get_qscale(MpegEncContext *s)
1058 int qscale = get_bits(&s->gb, 5);
1059 if (s->q_scale_type) {
1060 return non_linear_qscale[qscale];
1066 /* motion type (for mpeg2) */
1072 static int mpeg_decode_mb(MpegEncContext *s,
1073 DCTELEM block[12][64])
1075 int i, j, k, cbp, val, mb_type, motion_type;
1076 const int mb_block_count = 4 + (1<< s->chroma_format);
1078 dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
1080 assert(s->mb_skipped==0);
1082 if (s->mb_skip_run-- != 0) {
1083 if(s->pict_type == I_TYPE){
1084 av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1091 s->block_last_index[i] = -1;
1092 if(s->picture_structure == PICT_FRAME)
1093 s->mv_type = MV_TYPE_16X16;
1095 s->mv_type = MV_TYPE_FIELD;
1096 if (s->pict_type == P_TYPE) {
1097 /* if P type, zero motion vector is implied */
1098 s->mv_dir = MV_DIR_FORWARD;
1099 s->mv[0][0][0] = s->mv[0][0][1] = 0;
1100 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1101 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1102 s->field_select[0][0]= s->picture_structure - 1;
1104 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
1109 mb_type= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1];
1111 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,
1112 if(IS_INTRA(mb_type))
1115 /* if B type, reuse previous vectors and directions */
1116 s->mv[0][0][0] = s->last_mv[0][0][0];
1117 s->mv[0][0][1] = s->last_mv[0][0][1];
1118 s->mv[1][0][0] = s->last_mv[1][0][0];
1119 s->mv[1][0][1] = s->last_mv[1][0][1];
1121 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]=
1122 mb_type | MB_TYPE_SKIP;
1123 // assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
1125 if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0)
1132 switch(s->pict_type) {
1135 if (get_bits1(&s->gb) == 0) {
1136 if (get_bits1(&s->gb) == 0){
1137 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
1140 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
1142 mb_type = MB_TYPE_INTRA;
1146 mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
1148 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
1151 mb_type = ptype2mb_type[ mb_type ];
1154 mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
1156 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
1159 mb_type = btype2mb_type[ mb_type ];
1162 dprintf("mb_type=%x\n", mb_type);
1163 // motion_type = 0; /* avoid warning */
1164 if (IS_INTRA(mb_type)) {
1165 s->dsp.clear_blocks(s->block[0]);
1167 if(!s->chroma_y_shift){
1168 s->dsp.clear_blocks(s->block[6]);
1171 /* compute dct type */
1172 if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
1173 !s->frame_pred_frame_dct) {
1174 s->interlaced_dct = get_bits1(&s->gb);
1177 if (IS_QUANT(mb_type))
1178 s->qscale = get_qscale(s);
1180 if (s->concealment_motion_vectors) {
1181 /* just parse them */
1182 if (s->picture_structure != PICT_FRAME)
1183 skip_bits1(&s->gb); /* field select */
1185 s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
1186 mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
1187 s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
1188 mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
1190 skip_bits1(&s->gb); /* marker */
1192 memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
1195 //one 1 we memcpy blocks in xvmcvideo
1196 if(s->avctx->xvmc_acceleration > 1){
1197 XVMC_pack_pblocks(s,-1);//inter are always full blocks
1204 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
1205 if(s->flags2 & CODEC_FLAG2_FAST){
1207 mpeg2_fast_decode_block_intra(s, s->pblocks[i], i);
1210 for(i=0;i<mb_block_count;i++) {
1211 if (mpeg2_decode_block_intra(s, s->pblocks[i], i) < 0)
1217 if (mpeg1_decode_block_intra(s, s->pblocks[i], i) < 0)
1222 if (mb_type & MB_TYPE_ZERO_MV){
1223 assert(mb_type & MB_TYPE_CBP);
1225 /* compute dct type */
1226 if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
1227 !s->frame_pred_frame_dct) {
1228 s->interlaced_dct = get_bits1(&s->gb);
1231 if (IS_QUANT(mb_type))
1232 s->qscale = get_qscale(s);
1234 s->mv_dir = MV_DIR_FORWARD;
1235 if(s->picture_structure == PICT_FRAME)
1236 s->mv_type = MV_TYPE_16X16;
1238 s->mv_type = MV_TYPE_FIELD;
1239 mb_type |= MB_TYPE_INTERLACED;
1240 s->field_select[0][0]= s->picture_structure - 1;
1242 s->last_mv[0][0][0] = 0;
1243 s->last_mv[0][0][1] = 0;
1244 s->last_mv[0][1][0] = 0;
1245 s->last_mv[0][1][1] = 0;
1249 assert(mb_type & MB_TYPE_L0L1);
1250 //FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
1251 /* get additionnal motion vector type */
1252 if (s->frame_pred_frame_dct)
1253 motion_type = MT_FRAME;
1255 motion_type = get_bits(&s->gb, 2);
1258 /* compute dct type */
1259 if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
1260 !s->frame_pred_frame_dct && HAS_CBP(mb_type)) {
1261 s->interlaced_dct = get_bits1(&s->gb);
1264 if (IS_QUANT(mb_type))
1265 s->qscale = get_qscale(s);
1267 /* motion vectors */
1270 if (USES_LIST(mb_type, i)) {
1271 s->mv_dir |= (MV_DIR_FORWARD >> i);
1272 dprintf("motion_type=%d\n", motion_type);
1273 switch(motion_type) {
1274 case MT_FRAME: /* or MT_16X8 */
1275 if (s->picture_structure == PICT_FRAME) {
1277 mb_type |= MB_TYPE_16x16;
1278 s->mv_type = MV_TYPE_16X16;
1279 s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
1280 mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
1281 s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
1282 mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
1283 /* full_pel: only for mpeg1 */
1284 if (s->full_pel[i]){
1285 s->mv[i][0][0] <<= 1;
1286 s->mv[i][0][1] <<= 1;
1290 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1291 s->mv_type = MV_TYPE_16X8;
1293 s->field_select[i][j] = get_bits1(&s->gb);
1295 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
1296 s->last_mv[i][j][k]);
1297 s->last_mv[i][j][k] = val;
1298 s->mv[i][j][k] = val;
1304 s->mv_type = MV_TYPE_FIELD;
1305 if (s->picture_structure == PICT_FRAME) {
1306 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1308 s->field_select[i][j] = get_bits1(&s->gb);
1309 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
1310 s->last_mv[i][j][0]);
1311 s->last_mv[i][j][0] = val;
1312 s->mv[i][j][0] = val;
1313 dprintf("fmx=%d\n", val);
1314 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
1315 s->last_mv[i][j][1] >> 1);
1316 s->last_mv[i][j][1] = val << 1;
1317 s->mv[i][j][1] = val;
1318 dprintf("fmy=%d\n", val);
1321 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
1322 s->field_select[i][0] = get_bits1(&s->gb);
1324 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
1325 s->last_mv[i][0][k]);
1326 s->last_mv[i][0][k] = val;
1327 s->last_mv[i][1][k] = val;
1328 s->mv[i][0][k] = val;
1334 int dmx, dmy, mx, my, m;
1336 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
1337 s->last_mv[i][0][0]);
1338 s->last_mv[i][0][0] = mx;
1339 s->last_mv[i][1][0] = mx;
1341 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
1342 s->last_mv[i][0][1] >> 1);
1344 s->mv_type = MV_TYPE_DMV;
1347 s->last_mv[i][0][1] = my<<1;
1348 s->last_mv[i][1][1] = my<<1;
1350 s->mv[i][0][0] = mx;
1351 s->mv[i][0][1] = my;
1352 s->mv[i][1][0] = mx;//not used
1353 s->mv[i][1][1] = my;//not used
1355 if (s->picture_structure == PICT_FRAME) {
1356 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
1358 //m = 1 + 2 * s->top_field_first;
1359 m = s->top_field_first ? 1 : 3;
1361 /* top -> top pred */
1362 s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1363 s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
1365 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1366 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
1368 mb_type |= MB_TYPE_16x16;
1370 s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
1371 s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
1372 if(s->picture_structure == PICT_TOP_FIELD)
1380 av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
1388 if (HAS_CBP(mb_type)) {
1389 s->dsp.clear_blocks(s->block[0]);
1391 if(!s->chroma_y_shift){
1392 s->dsp.clear_blocks(s->block[6]);
1395 cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
1396 if (cbp < 0 || ((cbp == 0) && (s->chroma_format < 2)) ){
1397 av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
1400 if(mb_block_count > 6){
1401 cbp<<= mb_block_count-6;
1402 cbp |= get_bits(&s->gb, mb_block_count-6);
1406 //on 1 we memcpy blocks in xvmcvideo
1407 if(s->avctx->xvmc_acceleration > 1){
1408 XVMC_pack_pblocks(s,cbp);
1415 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
1416 if(s->flags2 & CODEC_FLAG2_FAST){
1419 mpeg2_fast_decode_block_non_intra(s, s->pblocks[i], i);
1421 s->block_last_index[i] = -1;
1426 cbp<<= 12-mb_block_count;
1428 for(i=0;i<mb_block_count;i++) {
1429 if ( cbp & (1<<11) ) {
1430 if (mpeg2_decode_block_non_intra(s, s->pblocks[i], i) < 0)
1433 s->block_last_index[i] = -1;
1439 if(s->flags2 & CODEC_FLAG2_FAST){
1442 mpeg1_fast_decode_block_inter(s, s->pblocks[i], i);
1444 s->block_last_index[i] = -1;
1451 if (mpeg1_decode_block_inter(s, s->pblocks[i], i) < 0)
1454 s->block_last_index[i] = -1;
1462 s->block_last_index[i] = -1;
1466 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
1471 /* as h263, but only 17 codes */
1472 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
1474 int code, sign, val, l, shift;
1476 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
1484 sign = get_bits1(&s->gb);
1488 val = (val - 1) << shift;
1489 val |= get_bits(&s->gb, shift);
1496 /* modulo decoding */
1497 l= INT_BIT - 5 - shift;
1502 static inline int decode_dc(GetBitContext *gb, int component)
1506 if (component == 0) {
1507 code = get_vlc2(gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
1509 code = get_vlc2(gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
1512 av_log(NULL, AV_LOG_ERROR, "invalid dc code at\n");
1518 diff = get_xbits(gb, code);
1523 static inline int mpeg1_decode_block_intra(MpegEncContext *s,
1527 int level, dc, diff, i, j, run;
1529 RLTable *rl = &rl_mpeg1;
1530 uint8_t * const scantable= s->intra_scantable.permutated;
1531 const uint16_t *quant_matrix= s->intra_matrix;
1532 const int qscale= s->qscale;
1535 component = (n <= 3 ? 0 : n - 4 + 1);
1536 diff = decode_dc(&s->gb, component);
1539 dc = s->last_dc[component];
1541 s->last_dc[component] = dc;
1543 dprintf("dc=%d diff=%d\n", dc, diff);
1546 OPEN_READER(re, &s->gb);
1547 /* now quantify & encode AC coefs */
1549 UPDATE_CACHE(re, &s->gb);
1550 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1554 } else if(level != 0) {
1557 level= (level*qscale*quant_matrix[j])>>4;
1559 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1560 LAST_SKIP_BITS(re, &s->gb, 1);
1563 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1564 UPDATE_CACHE(re, &s->gb);
1565 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1566 if (level == -128) {
1567 level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
1568 } else if (level == 0) {
1569 level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
1575 level= (level*qscale*quant_matrix[j])>>4;
1579 level= (level*qscale*quant_matrix[j])>>4;
1584 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1590 CLOSE_READER(re, &s->gb);
1592 s->block_last_index[n] = i;
1596 static inline int mpeg1_decode_block_inter(MpegEncContext *s,
1600 int level, i, j, run;
1601 RLTable *rl = &rl_mpeg1;
1602 uint8_t * const scantable= s->intra_scantable.permutated;
1603 const uint16_t *quant_matrix= s->inter_matrix;
1604 const int qscale= s->qscale;
1607 OPEN_READER(re, &s->gb);
1609 /* special case for the first coef. no need to add a second vlc table */
1610 UPDATE_CACHE(re, &s->gb);
1611 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
1612 level= (3*qscale*quant_matrix[0])>>5;
1614 if(GET_CACHE(re, &s->gb)&0x40000000)
1618 SKIP_BITS(re, &s->gb, 2);
1619 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1623 /* now quantify & encode AC coefs */
1625 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1630 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1632 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1633 SKIP_BITS(re, &s->gb, 1);
1636 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1637 UPDATE_CACHE(re, &s->gb);
1638 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1639 if (level == -128) {
1640 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
1641 } else if (level == 0) {
1642 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
1648 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1652 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1657 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1662 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1664 UPDATE_CACHE(re, &s->gb);
1667 LAST_SKIP_BITS(re, &s->gb, 2);
1668 CLOSE_READER(re, &s->gb);
1670 s->block_last_index[n] = i;
1674 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
1676 int level, i, j, run;
1677 RLTable *rl = &rl_mpeg1;
1678 uint8_t * const scantable= s->intra_scantable.permutated;
1679 const int qscale= s->qscale;
1682 OPEN_READER(re, &s->gb);
1684 /* special case for the first coef. no need to add a second vlc table */
1685 UPDATE_CACHE(re, &s->gb);
1686 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
1687 level= (3*qscale)>>1;
1689 if(GET_CACHE(re, &s->gb)&0x40000000)
1693 SKIP_BITS(re, &s->gb, 2);
1694 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1698 /* now quantify & encode AC coefs */
1700 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1705 level= ((level*2+1)*qscale)>>1;
1707 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1708 SKIP_BITS(re, &s->gb, 1);
1711 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1712 UPDATE_CACHE(re, &s->gb);
1713 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1714 if (level == -128) {
1715 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
1716 } else if (level == 0) {
1717 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
1723 level= ((level*2+1)*qscale)>>1;
1727 level= ((level*2+1)*qscale)>>1;
1733 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1735 UPDATE_CACHE(re, &s->gb);
1738 LAST_SKIP_BITS(re, &s->gb, 2);
1739 CLOSE_READER(re, &s->gb);
1741 s->block_last_index[n] = i;
1746 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
1750 int level, i, j, run;
1751 RLTable *rl = &rl_mpeg1;
1752 uint8_t * const scantable= s->intra_scantable.permutated;
1753 const uint16_t *quant_matrix;
1754 const int qscale= s->qscale;
1760 OPEN_READER(re, &s->gb);
1763 quant_matrix = s->inter_matrix;
1765 quant_matrix = s->chroma_inter_matrix;
1767 /* special case for the first coef. no need to add a second vlc table */
1768 UPDATE_CACHE(re, &s->gb);
1769 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
1770 level= (3*qscale*quant_matrix[0])>>5;
1771 if(GET_CACHE(re, &s->gb)&0x40000000)
1776 SKIP_BITS(re, &s->gb, 2);
1777 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1781 /* now quantify & encode AC coefs */
1783 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1788 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1789 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1790 SKIP_BITS(re, &s->gb, 1);
1793 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1794 UPDATE_CACHE(re, &s->gb);
1795 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1800 level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
1803 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1807 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1813 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1815 UPDATE_CACHE(re, &s->gb);
1818 LAST_SKIP_BITS(re, &s->gb, 2);
1819 CLOSE_READER(re, &s->gb);
1821 block[63] ^= (mismatch & 1);
1823 s->block_last_index[n] = i;
1827 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
1831 int level, i, j, run;
1832 RLTable *rl = &rl_mpeg1;
1833 uint8_t * const scantable= s->intra_scantable.permutated;
1834 const int qscale= s->qscale;
1835 OPEN_READER(re, &s->gb);
1838 /* special case for the first coef. no need to add a second vlc table */
1839 UPDATE_CACHE(re, &s->gb);
1840 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
1841 level= (3*qscale)>>1;
1842 if(GET_CACHE(re, &s->gb)&0x40000000)
1846 SKIP_BITS(re, &s->gb, 2);
1847 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1851 /* now quantify & encode AC coefs */
1853 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1858 level= ((level*2+1)*qscale)>>1;
1859 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1860 SKIP_BITS(re, &s->gb, 1);
1863 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1864 UPDATE_CACHE(re, &s->gb);
1865 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1870 level= ((-level*2+1)*qscale)>>1;
1873 level= ((level*2+1)*qscale)>>1;
1878 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1880 UPDATE_CACHE(re, &s->gb);
1883 LAST_SKIP_BITS(re, &s->gb, 2);
1884 CLOSE_READER(re, &s->gb);
1885 s->block_last_index[n] = i;
1890 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
1894 int level, dc, diff, i, j, run;
1897 uint8_t * const scantable= s->intra_scantable.permutated;
1898 const uint16_t *quant_matrix;
1899 const int qscale= s->qscale;
1904 quant_matrix = s->intra_matrix;
1907 quant_matrix = s->chroma_intra_matrix;
1908 component = (n&1) + 1;
1910 diff = decode_dc(&s->gb, component);
1913 dc = s->last_dc[component];
1915 s->last_dc[component] = dc;
1916 block[0] = dc << (3 - s->intra_dc_precision);
1917 dprintf("dc=%d\n", block[0]);
1918 mismatch = block[0] ^ 1;
1920 if (s->intra_vlc_format)
1926 OPEN_READER(re, &s->gb);
1927 /* now quantify & encode AC coefs */
1929 UPDATE_CACHE(re, &s->gb);
1930 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1934 } else if(level != 0) {
1937 level= (level*qscale*quant_matrix[j])>>4;
1938 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1939 LAST_SKIP_BITS(re, &s->gb, 1);
1942 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1943 UPDATE_CACHE(re, &s->gb);
1944 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1948 level= (-level*qscale*quant_matrix[j])>>4;
1951 level= (level*qscale*quant_matrix[j])>>4;
1955 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1962 CLOSE_READER(re, &s->gb);
1964 block[63]^= mismatch&1;
1966 s->block_last_index[n] = i;
1970 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
1974 int level, dc, diff, j, run;
1977 uint8_t * scantable= s->intra_scantable.permutated;
1978 const uint16_t *quant_matrix;
1979 const int qscale= s->qscale;
1983 quant_matrix = s->intra_matrix;
1986 quant_matrix = s->chroma_intra_matrix;
1987 component = (n&1) + 1;
1989 diff = decode_dc(&s->gb, component);
1992 dc = s->last_dc[component];
1994 s->last_dc[component] = dc;
1995 block[0] = dc << (3 - s->intra_dc_precision);
1996 if (s->intra_vlc_format)
2002 OPEN_READER(re, &s->gb);
2003 /* now quantify & encode AC coefs */
2005 UPDATE_CACHE(re, &s->gb);
2006 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
2010 } else if(level != 0) {
2013 level= (level*qscale*quant_matrix[j])>>4;
2014 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
2015 LAST_SKIP_BITS(re, &s->gb, 1);
2018 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
2019 UPDATE_CACHE(re, &s->gb);
2020 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
2024 level= (-level*qscale*quant_matrix[j])>>4;
2027 level= (level*qscale*quant_matrix[j])>>4;
2033 CLOSE_READER(re, &s->gb);
2036 s->block_last_index[n] = scantable - s->intra_scantable.permutated;
2040 typedef struct Mpeg1Context {
2041 MpegEncContext mpeg_enc_ctx;
2042 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
2043 int repeat_field; /* true if we must repeat the field */
2044 AVPanScan pan_scan; /** some temporary storage for the panscan */
2046 int swap_uv;//indicate VCR2
2047 int save_aspect_info;
2048 AVRational frame_rate_ext; ///< MPEG-2 specific framerate modificator
2052 static int mpeg_decode_init(AVCodecContext *avctx)
2054 Mpeg1Context *s = avctx->priv_data;
2055 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2058 //we need some parmutation to store
2059 //matrixes, until MPV_common_init()
2060 //set the real permutatuon
2062 s2->dsp.idct_permutation[i]=i;
2064 MPV_decode_defaults(s2);
2066 s->mpeg_enc_ctx.avctx= avctx;
2067 s->mpeg_enc_ctx.flags= avctx->flags;
2068 s->mpeg_enc_ctx.flags2= avctx->flags2;
2069 common_init(&s->mpeg_enc_ctx);
2072 s->mpeg_enc_ctx_allocated = 0;
2073 s->mpeg_enc_ctx.picture_number = 0;
2074 s->repeat_field = 0;
2075 s->mpeg_enc_ctx.codec_id= avctx->codec->id;
2079 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
2080 const uint8_t *new_perm){
2081 uint16_t temp_matrix[64];
2084 memcpy(temp_matrix,matrix,64*sizeof(uint16_t));
2087 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
2091 //Call this function when we know all parameters
2092 //it may be called in different places for mpeg1 and mpeg2
2093 static int mpeg_decode_postinit(AVCodecContext *avctx){
2094 Mpeg1Context *s1 = avctx->priv_data;
2095 MpegEncContext *s = &s1->mpeg_enc_ctx;
2096 uint8_t old_permutation[64];
2099 (s1->mpeg_enc_ctx_allocated == 0)||
2100 avctx->coded_width != s->width ||
2101 avctx->coded_height != s->height||
2102 s1->save_aspect_info != s->aspect_ratio_info||
2106 if (s1->mpeg_enc_ctx_allocated) {
2107 ParseContext pc= s->parse_context;
2108 s->parse_context.buffer=0;
2110 s->parse_context= pc;
2113 if( (s->width == 0 )||(s->height == 0))
2116 avcodec_set_dimensions(avctx, s->width, s->height);
2117 avctx->bit_rate = s->bit_rate;
2118 s1->save_aspect_info = s->aspect_ratio_info;
2120 //low_delay may be forced, in this case we will have B frames
2121 //that behave like P frames
2122 avctx->has_b_frames = !(s->low_delay);
2124 if(avctx->sub_id==1){//s->codec_id==avctx->codec_id==CODEC_ID
2126 avctx->time_base.den= ff_frame_rate_tab[s->frame_rate_index].num;
2127 avctx->time_base.num= ff_frame_rate_tab[s->frame_rate_index].den;
2129 avctx->sample_aspect_ratio= av_d2q(
2130 1.0/mpeg1_aspect[s->aspect_ratio_info], 255);
2135 &s->avctx->time_base.den,
2136 &s->avctx->time_base.num,
2137 ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num,
2138 ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
2141 if(s->aspect_ratio_info > 1){
2142 if( (s1->pan_scan.width == 0 )||(s1->pan_scan.height == 0) ){
2143 s->avctx->sample_aspect_ratio=
2145 mpeg2_aspect[s->aspect_ratio_info],
2146 (AVRational){s->width, s->height}
2149 s->avctx->sample_aspect_ratio=
2151 mpeg2_aspect[s->aspect_ratio_info],
2152 (AVRational){s1->pan_scan.width, s1->pan_scan.height}
2156 s->avctx->sample_aspect_ratio=
2157 mpeg2_aspect[s->aspect_ratio_info];
2161 if(avctx->xvmc_acceleration){
2162 avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
2164 if(s->chroma_format < 2){
2165 avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_420);
2167 if(s->chroma_format == 2){
2168 avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_422);
2170 if(s->chroma_format > 2){
2171 avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_444);
2174 //until then pix_fmt may be changed right after codec init
2175 if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
2176 if( avctx->idct_algo == FF_IDCT_AUTO )
2177 avctx->idct_algo = FF_IDCT_SIMPLE;
2179 //quantization matrixes may need reordering
2180 //if dct permutation is changed
2181 memcpy(old_permutation,s->dsp.idct_permutation,64*sizeof(uint8_t));
2183 if (MPV_common_init(s) < 0)
2186 quant_matrix_rebuild(s->intra_matrix, old_permutation,s->dsp.idct_permutation);
2187 quant_matrix_rebuild(s->inter_matrix, old_permutation,s->dsp.idct_permutation);
2188 quant_matrix_rebuild(s->chroma_intra_matrix,old_permutation,s->dsp.idct_permutation);
2189 quant_matrix_rebuild(s->chroma_inter_matrix,old_permutation,s->dsp.idct_permutation);
2191 s1->mpeg_enc_ctx_allocated = 1;
2196 static int mpeg1_decode_picture(AVCodecContext *avctx,
2197 const uint8_t *buf, int buf_size)
2199 Mpeg1Context *s1 = avctx->priv_data;
2200 MpegEncContext *s = &s1->mpeg_enc_ctx;
2201 int ref, f_code, vbv_delay;
2203 if(mpeg_decode_postinit(s->avctx) < 0)
2206 init_get_bits(&s->gb, buf, buf_size*8);
2208 ref = get_bits(&s->gb, 10); /* temporal ref */
2209 s->pict_type = get_bits(&s->gb, 3);
2210 if(s->pict_type == 0 || s->pict_type > 3)
2213 vbv_delay= get_bits(&s->gb, 16);
2214 if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
2215 s->full_pel[0] = get_bits1(&s->gb);
2216 f_code = get_bits(&s->gb, 3);
2217 if (f_code == 0 && avctx->error_resilience >= FF_ER_COMPLIANT)
2219 s->mpeg_f_code[0][0] = f_code;
2220 s->mpeg_f_code[0][1] = f_code;
2222 if (s->pict_type == B_TYPE) {
2223 s->full_pel[1] = get_bits1(&s->gb);
2224 f_code = get_bits(&s->gb, 3);
2225 if (f_code == 0 && avctx->error_resilience >= FF_ER_COMPLIANT)
2227 s->mpeg_f_code[1][0] = f_code;
2228 s->mpeg_f_code[1][1] = f_code;
2230 s->current_picture.pict_type= s->pict_type;
2231 s->current_picture.key_frame= s->pict_type == I_TYPE;
2233 if(avctx->debug & FF_DEBUG_PICT_INFO)
2234 av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
2242 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
2244 MpegEncContext *s= &s1->mpeg_enc_ctx;
2245 int horiz_size_ext, vert_size_ext;
2248 skip_bits(&s->gb, 1); /* profil and level esc*/
2249 s->avctx->profile= get_bits(&s->gb, 3);
2250 s->avctx->level= get_bits(&s->gb, 4);
2251 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
2252 s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
2253 horiz_size_ext = get_bits(&s->gb, 2);
2254 vert_size_ext = get_bits(&s->gb, 2);
2255 s->width |= (horiz_size_ext << 12);
2256 s->height |= (vert_size_ext << 12);
2257 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
2258 s->bit_rate += (bit_rate_ext << 18) * 400;
2259 skip_bits1(&s->gb); /* marker */
2260 s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10;
2262 s->low_delay = get_bits1(&s->gb);
2263 if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
2265 s1->frame_rate_ext.num = get_bits(&s->gb, 2)+1;
2266 s1->frame_rate_ext.den = get_bits(&s->gb, 5)+1;
2268 dprintf("sequence extension\n");
2269 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
2270 s->avctx->sub_id = 2; /* indicates mpeg2 found */
2272 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
2273 av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
2274 s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
2278 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
2280 MpegEncContext *s= &s1->mpeg_enc_ctx;
2281 int color_description, w, h;
2283 skip_bits(&s->gb, 3); /* video format */
2284 color_description= get_bits1(&s->gb);
2285 if(color_description){
2286 skip_bits(&s->gb, 8); /* color primaries */
2287 skip_bits(&s->gb, 8); /* transfer_characteristics */
2288 skip_bits(&s->gb, 8); /* matrix_coefficients */
2290 w= get_bits(&s->gb, 14);
2291 skip_bits(&s->gb, 1); //marker
2292 h= get_bits(&s->gb, 14);
2293 skip_bits(&s->gb, 1); //marker
2295 s1->pan_scan.width= 16*w;
2296 s1->pan_scan.height=16*h;
2298 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
2299 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
2302 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
2304 MpegEncContext *s= &s1->mpeg_enc_ctx;
2308 if(s->progressive_sequence){
2309 if(s->repeat_first_field){
2311 if(s->top_field_first)
2315 if(s->picture_structure == PICT_FRAME){
2317 if(s->repeat_first_field)
2321 for(i=0; i<nofco; i++){
2322 s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
2323 skip_bits(&s->gb, 1); //marker
2324 s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
2325 skip_bits(&s->gb, 1); //marker
2328 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
2329 av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
2330 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
2331 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
2332 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
2336 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
2340 dprintf("matrix extension\n");
2342 if (get_bits1(&s->gb)) {
2344 v = get_bits(&s->gb, 8);
2345 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
2346 s->intra_matrix[j] = v;
2347 s->chroma_intra_matrix[j] = v;
2350 if (get_bits1(&s->gb)) {
2352 v = get_bits(&s->gb, 8);
2353 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
2354 s->inter_matrix[j] = v;
2355 s->chroma_inter_matrix[j] = v;
2358 if (get_bits1(&s->gb)) {
2360 v = get_bits(&s->gb, 8);
2361 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
2362 s->chroma_intra_matrix[j] = v;
2365 if (get_bits1(&s->gb)) {
2367 v = get_bits(&s->gb, 8);
2368 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
2369 s->chroma_inter_matrix[j] = v;
2374 static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
2376 s->full_pel[0] = s->full_pel[1] = 0;
2377 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
2378 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
2379 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
2380 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
2381 s->intra_dc_precision = get_bits(&s->gb, 2);
2382 s->picture_structure = get_bits(&s->gb, 2);
2383 s->top_field_first = get_bits1(&s->gb);
2384 s->frame_pred_frame_dct = get_bits1(&s->gb);
2385 s->concealment_motion_vectors = get_bits1(&s->gb);
2386 s->q_scale_type = get_bits1(&s->gb);
2387 s->intra_vlc_format = get_bits1(&s->gb);
2388 s->alternate_scan = get_bits1(&s->gb);
2389 s->repeat_first_field = get_bits1(&s->gb);
2390 s->chroma_420_type = get_bits1(&s->gb);
2391 s->progressive_frame = get_bits1(&s->gb);
2393 if(s->picture_structure == PICT_FRAME)
2396 s->first_field ^= 1;
2397 memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
2400 if(s->alternate_scan){
2401 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
2402 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
2404 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
2405 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
2408 /* composite display not parsed */
2409 dprintf("intra_dc_precision=%d\n", s->intra_dc_precision);
2410 dprintf("picture_structure=%d\n", s->picture_structure);
2411 dprintf("top field first=%d\n", s->top_field_first);
2412 dprintf("repeat first field=%d\n", s->repeat_first_field);
2413 dprintf("conceal=%d\n", s->concealment_motion_vectors);
2414 dprintf("intra_vlc_format=%d\n", s->intra_vlc_format);
2415 dprintf("alternate_scan=%d\n", s->alternate_scan);
2416 dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
2417 dprintf("progressive_frame=%d\n", s->progressive_frame);
2420 static void mpeg_decode_extension(AVCodecContext *avctx,
2421 const uint8_t *buf, int buf_size)
2423 Mpeg1Context *s1 = avctx->priv_data;
2424 MpegEncContext *s = &s1->mpeg_enc_ctx;
2427 init_get_bits(&s->gb, buf, buf_size*8);
2429 ext_type = get_bits(&s->gb, 4);
2432 mpeg_decode_sequence_extension(s1);
2435 mpeg_decode_sequence_display_extension(s1);
2438 mpeg_decode_quant_matrix_extension(s);
2441 mpeg_decode_picture_display_extension(s1);
2444 mpeg_decode_picture_coding_extension(s);
2449 static void exchange_uv(MpegEncContext *s){
2450 short * tmp = s->pblocks[4];
2451 s->pblocks[4] = s->pblocks[5];
2452 s->pblocks[5] = tmp;
2455 static int mpeg_field_start(MpegEncContext *s){
2456 AVCodecContext *avctx= s->avctx;
2457 Mpeg1Context *s1 = (Mpeg1Context*)s;
2459 /* start frame decoding */
2460 if(s->first_field || s->picture_structure==PICT_FRAME){
2461 if(MPV_frame_start(s, avctx) < 0)
2464 ff_er_frame_start(s);
2466 /* first check if we must repeat the frame */
2467 s->current_picture_ptr->repeat_pict = 0;
2468 if (s->repeat_first_field) {
2469 if (s->progressive_sequence) {
2470 if (s->top_field_first)
2471 s->current_picture_ptr->repeat_pict = 4;
2473 s->current_picture_ptr->repeat_pict = 2;
2474 } else if (s->progressive_frame) {
2475 s->current_picture_ptr->repeat_pict = 1;
2479 *s->current_picture_ptr->pan_scan= s1->pan_scan;
2480 }else{ //second field
2483 if(!s->current_picture_ptr){
2484 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
2489 s->current_picture.data[i] = s->current_picture_ptr->data[i];
2490 if(s->picture_structure == PICT_BOTTOM_FIELD){
2491 s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
2496 // MPV_frame_start will call this function too,
2497 // but we need to call it on every field
2498 if(s->avctx->xvmc_acceleration)
2499 XVMC_field_start(s,avctx);
2505 #define DECODE_SLICE_ERROR -1
2506 #define DECODE_SLICE_OK 0
2509 * decodes a slice. MpegEncContext.mb_y must be set to the MB row from the startcode
2510 * @return DECODE_SLICE_ERROR if the slice is damaged<br>
2511 * DECODE_SLICE_OK if this slice is ok<br>
2513 static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
2514 const uint8_t **buf, int buf_size)
2516 MpegEncContext *s = &s1->mpeg_enc_ctx;
2517 AVCodecContext *avctx= s->avctx;
2519 const int field_pic= s->picture_structure != PICT_FRAME;
2520 const int lowres= s->avctx->lowres;
2525 if (mb_y<<field_pic >= s->mb_height){
2526 av_log(s->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s->mb_height);
2530 init_get_bits(&s->gb, *buf, buf_size*8);
2532 ff_mpeg1_clean_buffers(s);
2533 s->interlaced_dct = 0;
2535 s->qscale = get_qscale(s);
2538 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
2542 /* extra slice info */
2543 while (get_bits1(&s->gb) != 0) {
2544 skip_bits(&s->gb, 8);
2550 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
2552 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
2559 /* otherwise, stuffing, nothing to do */
2566 s->resync_mb_x= s->mb_x;
2567 s->resync_mb_y= s->mb_y= mb_y;
2569 ff_init_block_index(s);
2571 if (s->mb_y==0 && s->mb_x==0 && (s->first_field || s->picture_structure==PICT_FRAME)) {
2572 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
2573 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",
2574 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],
2575 s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")),
2576 s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
2577 s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
2578 s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
2584 //one 1 we memcpy blocks in xvmcvideo
2585 if(s->avctx->xvmc_acceleration > 1)
2586 XVMC_init_block(s);//set s->block
2589 ret = mpeg_decode_mb(s, s->block);
2590 s->chroma_qscale= s->qscale;
2592 dprintf("ret=%d\n", ret);
2596 if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
2597 const int wrap = field_pic ? 2*s->b8_stride : s->b8_stride;
2598 int xy = s->mb_x*2 + s->mb_y*2*wrap;
2599 int motion_x, motion_y, dir, i;
2600 if(field_pic && !s->first_field)
2604 for(dir=0; dir<2; dir++){
2605 if (s->mb_intra || (dir==1 && s->pict_type != B_TYPE)) {
2606 motion_x = motion_y = 0;
2607 }else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)){
2608 motion_x = s->mv[dir][0][0];
2609 motion_y = s->mv[dir][0][1];
2610 } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
2611 motion_x = s->mv[dir][i][0];
2612 motion_y = s->mv[dir][i][1];
2615 s->current_picture.motion_val[dir][xy ][0] = motion_x;
2616 s->current_picture.motion_val[dir][xy ][1] = motion_y;
2617 s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
2618 s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
2619 s->current_picture.ref_index [dir][xy ]=
2620 s->current_picture.ref_index [dir][xy + 1]= s->field_select[dir][i];
2621 assert(s->field_select[dir][i]==0 || s->field_select[dir][i]==1);
2627 s->dest[0] += 16 >> lowres;
2628 s->dest[1] += 16 >> (s->chroma_x_shift + lowres);
2629 s->dest[2] += 16 >> (s->chroma_x_shift + lowres);
2631 MPV_decode_mb(s, s->block);
2633 if (++s->mb_x >= s->mb_width) {
2634 const int mb_size= 16>>s->avctx->lowres;
2636 ff_draw_horiz_band(s, mb_size*s->mb_y, mb_size);
2641 if(s->mb_y<<field_pic >= s->mb_height){
2642 int left= s->gb.size_in_bits - get_bits_count(&s->gb);
2643 int is_d10= s->chroma_format==2 && s->pict_type==I_TYPE && avctx->profile==0 && avctx->level==5
2644 && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
2645 && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
2647 if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
2648 || (avctx->error_resilience >= FF_ER_AGGRESSIVE && left>8)){
2649 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
2655 ff_init_block_index(s);
2658 /* skip mb handling */
2659 if (s->mb_skip_run == -1) {
2660 /* read again increment */
2663 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
2665 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
2670 s->mb_skip_run += 33;
2671 }else if(code == 35){
2672 if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
2673 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
2676 goto eos; /* end of slice */
2678 /* otherwise, stuffing, nothing to do */
2680 s->mb_skip_run += code;
2686 eos: // end of slice
2687 *buf += get_bits_count(&s->gb)/8 - 1;
2688 //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
2692 static int slice_decode_thread(AVCodecContext *c, void *arg){
2693 MpegEncContext *s= arg;
2694 const uint8_t *buf= s->gb.buffer;
2695 int mb_y= s->start_mb_y;
2697 s->error_count= 3*(s->end_mb_y - s->start_mb_y)*s->mb_width;
2700 uint32_t start_code;
2703 ret= mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
2705 //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
2706 //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);
2708 if(s->resync_mb_x>=0 && s->resync_mb_y>=0)
2709 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);
2711 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);
2714 if(s->mb_y == s->end_mb_y)
2718 buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);
2719 mb_y= start_code - SLICE_MIN_START_CODE;
2720 if(mb_y < 0 || mb_y >= s->end_mb_y)
2724 return 0; //not reached
2728 * handles slice ends.
2729 * @return 1 if it seems to be the last slice of
2731 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
2733 Mpeg1Context *s1 = avctx->priv_data;
2734 MpegEncContext *s = &s1->mpeg_enc_ctx;
2736 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
2740 if(s->avctx->xvmc_acceleration)
2743 /* end of slice reached */
2744 if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
2747 s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
2753 if (s->pict_type == B_TYPE || s->low_delay) {
2754 *pict= *(AVFrame*)s->current_picture_ptr;
2755 ff_print_debug_info(s, pict);
2757 s->picture_number++;
2758 /* latency of 1 frame for I and P frames */
2759 /* XXX: use another variable than picture_number */
2760 if (s->last_picture_ptr != NULL) {
2761 *pict= *(AVFrame*)s->last_picture_ptr;
2762 ff_print_debug_info(s, pict);
2772 static int mpeg1_decode_sequence(AVCodecContext *avctx,
2773 const uint8_t *buf, int buf_size)
2775 Mpeg1Context *s1 = avctx->priv_data;
2776 MpegEncContext *s = &s1->mpeg_enc_ctx;
2780 init_get_bits(&s->gb, buf, buf_size*8);
2782 width = get_bits(&s->gb, 12);
2783 height = get_bits(&s->gb, 12);
2784 if (width <= 0 || height <= 0 ||
2785 (width % 2) != 0 || (height % 2) != 0)
2787 s->aspect_ratio_info= get_bits(&s->gb, 4);
2788 if (s->aspect_ratio_info == 0)
2790 s->frame_rate_index = get_bits(&s->gb, 4);
2791 if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
2793 s->bit_rate = get_bits(&s->gb, 18) * 400;
2794 if (get_bits1(&s->gb) == 0) /* marker */
2799 s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
2800 skip_bits(&s->gb, 1);
2803 if (get_bits1(&s->gb)) {
2805 v = get_bits(&s->gb, 8);
2807 av_log(s->avctx, AV_LOG_ERROR, "intra matrix damaged\n");
2810 j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
2811 s->intra_matrix[j] = v;
2812 s->chroma_intra_matrix[j] = v;
2815 dprintf("intra matrix present\n");
2817 dprintf(" %d", s->intra_matrix[s->dsp.idct_permutation[i]]);
2822 j = s->dsp.idct_permutation[i];
2823 v = ff_mpeg1_default_intra_matrix[i];
2824 s->intra_matrix[j] = v;
2825 s->chroma_intra_matrix[j] = v;
2828 if (get_bits1(&s->gb)) {
2830 v = get_bits(&s->gb, 8);
2832 av_log(s->avctx, AV_LOG_ERROR, "inter matrix damaged\n");
2835 j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
2836 s->inter_matrix[j] = v;
2837 s->chroma_inter_matrix[j] = v;
2840 dprintf("non intra matrix present\n");
2842 dprintf(" %d", s->inter_matrix[s->dsp.idct_permutation[i]]);
2847 int j= s->dsp.idct_permutation[i];
2848 v = ff_mpeg1_default_non_intra_matrix[i];
2849 s->inter_matrix[j] = v;
2850 s->chroma_inter_matrix[j] = v;
2854 if(show_bits(&s->gb, 23) != 0){
2855 av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
2859 /* we set mpeg2 parameters so that it emulates mpeg1 */
2860 s->progressive_sequence = 1;
2861 s->progressive_frame = 1;
2862 s->picture_structure = PICT_FRAME;
2863 s->frame_pred_frame_dct = 1;
2864 s->chroma_format = 1;
2865 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
2866 avctx->sub_id = 1; /* indicates mpeg1 */
2867 s->out_format = FMT_MPEG1;
2868 s->swap_uv = 0;//AFAIK VCR2 don't have SEQ_HEADER
2869 if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
2871 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
2872 av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
2873 s->avctx->rc_buffer_size, s->bit_rate);
2878 static int vcr2_init_sequence(AVCodecContext *avctx)
2880 Mpeg1Context *s1 = avctx->priv_data;
2881 MpegEncContext *s = &s1->mpeg_enc_ctx;
2884 /* start new mpeg1 context decoding */
2885 s->out_format = FMT_MPEG1;
2886 if (s1->mpeg_enc_ctx_allocated) {
2889 s->width = avctx->coded_width;
2890 s->height = avctx->coded_height;
2891 avctx->has_b_frames= 0; //true?
2894 if(avctx->xvmc_acceleration){
2895 avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
2897 avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_420);
2900 if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
2901 if( avctx->idct_algo == FF_IDCT_AUTO )
2902 avctx->idct_algo = FF_IDCT_SIMPLE;
2904 if (MPV_common_init(s) < 0)
2906 exchange_uv(s);//common init reset pblocks, so we swap them here
2907 s->swap_uv = 1;// in case of xvmc we need to swap uv for each MB
2908 s1->mpeg_enc_ctx_allocated = 1;
2911 int j= s->dsp.idct_permutation[i];
2912 v = ff_mpeg1_default_intra_matrix[i];
2913 s->intra_matrix[j] = v;
2914 s->chroma_intra_matrix[j] = v;
2916 v = ff_mpeg1_default_non_intra_matrix[i];
2917 s->inter_matrix[j] = v;
2918 s->chroma_inter_matrix[j] = v;
2921 s->progressive_sequence = 1;
2922 s->progressive_frame = 1;
2923 s->picture_structure = PICT_FRAME;
2924 s->frame_pred_frame_dct = 1;
2925 s->chroma_format = 1;
2926 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
2927 avctx->sub_id = 2; /* indicates mpeg2 */
2932 static void mpeg_decode_user_data(AVCodecContext *avctx,
2933 const uint8_t *buf, int buf_size)
2940 /* we parse the DTG active format information */
2942 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2956 avctx->dtg_active_format = p[0] & 0x0f;
2961 static void mpeg_decode_gop(AVCodecContext *avctx,
2962 const uint8_t *buf, int buf_size){
2963 Mpeg1Context *s1 = avctx->priv_data;
2964 MpegEncContext *s = &s1->mpeg_enc_ctx;
2966 int drop_frame_flag;
2967 int time_code_hours, time_code_minutes;
2968 int time_code_seconds, time_code_pictures;
2971 init_get_bits(&s->gb, buf, buf_size*8);
2973 drop_frame_flag = get_bits1(&s->gb);
2975 time_code_hours=get_bits(&s->gb,5);
2976 time_code_minutes = get_bits(&s->gb,6);
2977 skip_bits1(&s->gb);//marker bit
2978 time_code_seconds = get_bits(&s->gb,6);
2979 time_code_pictures = get_bits(&s->gb,6);
2981 /*broken_link indicate that after editing the
2982 reference frames of the first B-Frames after GOP I-Frame
2983 are missing (open gop)*/
2984 broken_link = get_bits1(&s->gb);
2986 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
2987 av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) broken_link=%d\n",
2988 time_code_hours, time_code_minutes, time_code_seconds,
2989 time_code_pictures, broken_link);
2992 * finds the end of the current frame in the bitstream.
2993 * @return the position of the first byte of the next frame, or -1
2995 int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
2998 uint32_t state= pc->state;
3001 if(!pc->frame_start_found){
3002 for(i=0; i<buf_size; i++){
3003 i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
3004 if(state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
3006 pc->frame_start_found=1;
3012 if(pc->frame_start_found){
3013 /* EOF considered as end of frame */
3016 for(; i<buf_size; i++){
3017 i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
3018 if((state&0xFFFFFF00) == 0x100){
3019 if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
3020 pc->frame_start_found=0;
3028 return END_NOT_FOUND;
3031 /* handle buffering and image synchronisation */
3032 static int mpeg_decode_frame(AVCodecContext *avctx,
3033 void *data, int *data_size,
3034 uint8_t *buf, int buf_size)
3036 Mpeg1Context *s = avctx->priv_data;
3037 const uint8_t *buf_end;
3038 const uint8_t *buf_ptr;
3039 uint32_t start_code;
3040 int ret, input_size;
3041 AVFrame *picture = data;
3042 MpegEncContext *s2 = &s->mpeg_enc_ctx;
3043 dprintf("fill_buffer\n");
3045 if (buf_size == 0) {
3046 /* special case for last picture */
3047 if (s2->low_delay==0 && s2->next_picture_ptr) {
3048 *picture= *(AVFrame*)s2->next_picture_ptr;
3049 s2->next_picture_ptr= NULL;
3051 *data_size = sizeof(AVFrame);
3056 if(s2->flags&CODEC_FLAG_TRUNCATED){
3057 int next= ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size);
3059 if( ff_combine_frame(&s2->parse_context, next, &buf, &buf_size) < 0 )
3064 buf_end = buf + buf_size;
3067 if (s->repeat_field % 2 == 1) {
3069 //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
3070 // s2->picture_number, s->repeat_field);
3071 if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
3072 *data_size = sizeof(AVPicture);
3078 if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == ff_get_fourcc("VCR2"))
3079 vcr2_init_sequence(avctx);
3084 /* find start next code */
3086 buf_ptr = ff_find_start_code(buf_ptr,buf_end, &start_code);
3087 if (start_code > 0x1ff){
3088 if(s2->pict_type != B_TYPE || avctx->skip_frame <= AVDISCARD_DEFAULT){
3089 if(avctx->thread_count > 1){
3092 avctx->execute(avctx, slice_decode_thread, (void**)&(s2->thread_context[0]), NULL, s->slice_count);
3093 for(i=0; i<s->slice_count; i++)
3094 s2->error_count += s2->thread_context[i]->error_count;
3096 if (slice_end(avctx, picture)) {
3097 if(s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
3098 *data_size = sizeof(AVPicture);
3101 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
3104 input_size = buf_end - buf_ptr;
3106 if(avctx->debug & FF_DEBUG_STARTCODE){
3107 av_log(avctx, AV_LOG_DEBUG, "%3X at %zd left %d\n", start_code, buf_ptr-buf, input_size);
3110 /* prepare data for next start code */
3111 switch(start_code) {
3112 case SEQ_START_CODE:
3113 mpeg1_decode_sequence(avctx, buf_ptr,
3117 case PICTURE_START_CODE:
3118 /* we have a complete image : we try to decompress it */
3119 mpeg1_decode_picture(avctx,
3120 buf_ptr, input_size);
3122 case EXT_START_CODE:
3123 mpeg_decode_extension(avctx,
3124 buf_ptr, input_size);
3126 case USER_START_CODE:
3127 mpeg_decode_user_data(avctx,
3128 buf_ptr, input_size);
3130 case GOP_START_CODE:
3132 mpeg_decode_gop(avctx,
3133 buf_ptr, input_size);
3136 if (start_code >= SLICE_MIN_START_CODE &&
3137 start_code <= SLICE_MAX_START_CODE) {
3138 int mb_y= start_code - SLICE_MIN_START_CODE;
3140 if(s2->last_picture_ptr==NULL){
3141 /* skip b frames if we dont have reference frames */
3142 if(s2->pict_type==B_TYPE) break;
3143 /* skip P frames if we dont have reference frame no valid header */
3144 // if(s2->pict_type==P_TYPE && s2->first_field && !s2->first_slice) break;
3146 /* skip b frames if we are in a hurry */
3147 if(avctx->hurry_up && s2->pict_type==B_TYPE) break;
3148 if( (avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type==B_TYPE)
3149 ||(avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type!=I_TYPE)
3150 || avctx->skip_frame >= AVDISCARD_ALL)
3152 /* skip everything if we are in a hurry>=5 */
3153 if(avctx->hurry_up>=5) break;
3155 if (!s->mpeg_enc_ctx_allocated) break;
3157 if(s2->codec_id == CODEC_ID_MPEG2VIDEO){
3158 if(mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
3162 if(s2->first_slice){
3164 if(mpeg_field_start(s2) < 0)
3168 if(avctx->thread_count > 1){
3169 int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count;
3170 if(threshold <= mb_y){
3171 MpegEncContext *thread_context= s2->thread_context[s->slice_count];
3173 thread_context->start_mb_y= mb_y;
3174 thread_context->end_mb_y = s2->mb_height;
3176 s2->thread_context[s->slice_count-1]->end_mb_y= mb_y;
3177 ff_update_duplicate_context(thread_context, s2);
3179 init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
3182 buf_ptr += 2; //FIXME add minimum num of bytes per slice
3184 ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
3188 if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
3189 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);
3191 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);
3200 static int mpeg_decode_end(AVCodecContext *avctx)
3202 Mpeg1Context *s = avctx->priv_data;
3204 if (s->mpeg_enc_ctx_allocated)
3205 MPV_common_end(&s->mpeg_enc_ctx);
3209 AVCodec mpeg1video_decoder = {
3212 CODEC_ID_MPEG1VIDEO,
3213 sizeof(Mpeg1Context),
3218 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
3219 .flush= ff_mpeg_flush,
3222 AVCodec mpeg2video_decoder = {
3225 CODEC_ID_MPEG2VIDEO,
3226 sizeof(Mpeg1Context),
3231 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
3232 .flush= ff_mpeg_flush,
3236 AVCodec mpegvideo_decoder = {
3239 CODEC_ID_MPEG2VIDEO,
3240 sizeof(Mpeg1Context),
3245 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
3246 .flush= ff_mpeg_flush,
3249 #ifdef CONFIG_ENCODERS
3251 AVCodec mpeg1video_encoder = {
3254 CODEC_ID_MPEG1VIDEO,
3255 sizeof(MpegEncContext),
3259 .supported_framerates= ff_frame_rate_tab+1,
3260 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
3261 .capabilities= CODEC_CAP_DELAY,
3264 AVCodec mpeg2video_encoder = {
3267 CODEC_ID_MPEG2VIDEO,
3268 sizeof(MpegEncContext),
3272 .supported_framerates= ff_frame_rate_tab+1,
3273 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, -1},
3274 .capabilities= CODEC_CAP_DELAY,
3279 static int mpeg_mc_decode_init(AVCodecContext *avctx){
3282 if( avctx->thread_count > 1)
3284 if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
3286 if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
3287 dprintf("mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
3289 mpeg_decode_init(avctx);
3290 s = avctx->priv_data;
3292 avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
3293 avctx->xvmc_acceleration = 2;//2 - the blocks are packed!
3298 AVCodec mpeg_xvmc_decoder = {
3301 CODEC_ID_MPEG2VIDEO_XVMC,
3302 sizeof(Mpeg1Context),
3303 mpeg_mc_decode_init,
3307 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
3308 .flush= ff_mpeg_flush,
3313 /* this is ugly i know, but the alternative is too make
3314 hundreds of vars global and prefix them with ff_mpeg1_
3315 which is far uglier. */