2 * MPEG1 encoder / MPEG2 decoder
3 * Copyright (c) 2000,2001 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "mpegvideo.h"
24 #include "mpeg12data.h"
27 #define PRINT_QP(a, b) {}
29 #define PRINT_QP(a, b) printf(a, b)
33 #define SEQ_END_CODE 0x000001b7
34 #define SEQ_START_CODE 0x000001b3
35 #define GOP_START_CODE 0x000001b8
36 #define PICTURE_START_CODE 0x00000100
37 #define SLICE_MIN_START_CODE 0x00000101
38 #define SLICE_MAX_START_CODE 0x000001af
39 #define EXT_START_CODE 0x000001b5
40 #define USER_START_CODE 0x000001b2
44 #define MBINCR_VLC_BITS 9
45 #define MB_PAT_VLC_BITS 9
46 #define MB_PTYPE_VLC_BITS 6
47 #define MB_BTYPE_VLC_BITS 6
48 #define TEX_VLC_BITS 9
50 static void mpeg1_encode_block(MpegEncContext *s,
53 static void mpeg1_encode_motion(MpegEncContext *s, int val);
54 static void mpeg1_skip_picture(MpegEncContext *s, int pict_num);
55 static inline int mpeg1_decode_block_inter(MpegEncContext *s,
58 static inline int mpeg1_decode_block_intra(MpegEncContext *s,
61 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
64 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
67 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
69 static UINT16 mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
70 static UINT8 fcode_tab[MAX_MV*2+1];
72 static inline int get_bits_diff(MpegEncContext *s){
75 bits= get_bit_count(&s->pb);
76 ret= bits - s->last_bits;
82 static void init_2d_vlc_rl(RLTable *rl)
86 init_vlc(&rl->vlc, TEX_VLC_BITS, rl->n + 2,
87 &rl->table_vlc[0][1], 4, 2,
88 &rl->table_vlc[0][0], 4, 2);
91 rl->rl_vlc[0]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
92 for(i=0; i<rl->vlc.table_size; i++){
93 int code= rl->vlc.table[i][0];
94 int len = rl->vlc.table[i][1];
97 if(len==0){ // illegal code
100 }else if(len<0){ //more bits needed
104 if(code==rl->n){ //esc
107 }else if(code==rl->n+1){ //eob
111 run= rl->table_run [code] + 1;
112 level= rl->table_level[code];
115 rl->rl_vlc[0][i].len= len;
116 rl->rl_vlc[0][i].level= level;
117 rl->rl_vlc[0][i].run= run;
122 static void put_header(MpegEncContext *s, int header)
124 align_put_bits(&s->pb);
125 put_bits(&s->pb, 16, header>>16);
126 put_bits(&s->pb, 16, header&0xFFFF);
129 /* put sequence header if needed */
130 static void mpeg1_encode_sequence_header(MpegEncContext *s)
132 unsigned int vbv_buffer_size;
136 float best_aspect_error= 1E10;
137 float aspect_ratio= s->avctx->aspect_ratio;
139 if(aspect_ratio==0.0) aspect_ratio= s->width / (float)s->height; //pixel aspect 1:1 (VGA)
141 if (s->current_picture.key_frame) {
142 /* mpeg1 header repeated every gop */
143 put_header(s, SEQ_START_CODE);
145 /* search closest frame rate */
148 s->frame_rate_index = 0;
151 d = abs(s->frame_rate - frame_rate_tab[i]);
154 s->frame_rate_index = i;
159 put_bits(&s->pb, 12, s->width);
160 put_bits(&s->pb, 12, s->height);
163 float error= mpeg1_aspect[i] - s->width/(s->height*aspect_ratio);
166 if(error < best_aspect_error){
167 best_aspect_error= error;
168 s->aspect_ratio_info= i;
172 put_bits(&s->pb, 4, s->aspect_ratio_info);
173 put_bits(&s->pb, 4, s->frame_rate_index);
174 v = s->bit_rate / 400;
177 put_bits(&s->pb, 18, v);
178 put_bits(&s->pb, 1, 1); /* marker */
180 if(s->avctx->rc_buffer_size)
181 vbv_buffer_size = s->avctx->rc_buffer_size;
183 /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */
184 vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
185 put_bits(&s->pb, 10, (vbv_buffer_size + 16383) / 16384);
186 put_bits(&s->pb, 1, 1); /* constrained parameter flag */
187 put_bits(&s->pb, 1, 0); /* no custom intra matrix */
188 put_bits(&s->pb, 1, 0); /* no custom non intra matrix */
190 put_header(s, GOP_START_CODE);
191 put_bits(&s->pb, 1, 0); /* do drop frame */
192 /* time code : we must convert from the real frame rate to a
193 fake mpeg frame rate in case of low frame rate */
194 fps = frame_rate_tab[s->frame_rate_index];
195 time_code = (INT64)s->fake_picture_number * FRAME_RATE_BASE;
196 s->gop_picture_number = s->fake_picture_number;
197 put_bits(&s->pb, 5, (UINT32)((time_code / (fps * 3600)) % 24));
198 put_bits(&s->pb, 6, (UINT32)((time_code / (fps * 60)) % 60));
199 put_bits(&s->pb, 1, 1);
200 put_bits(&s->pb, 6, (UINT32)((time_code / fps) % 60));
201 put_bits(&s->pb, 6, (UINT32)((time_code % fps) / FRAME_RATE_BASE));
202 put_bits(&s->pb, 1, 1); /* closed gop */
203 put_bits(&s->pb, 1, 0); /* broken link */
206 if (s->frame_rate < (24 * FRAME_RATE_BASE) && s->picture_number > 0) {
207 /* insert empty P pictures to slow down to the desired
208 frame rate. Each fake pictures takes about 20 bytes */
209 fps = frame_rate_tab[s->frame_rate_index];
210 n = (((INT64)s->picture_number * fps) / s->frame_rate) - 1;
211 while (s->fake_picture_number < n) {
212 mpeg1_skip_picture(s, s->fake_picture_number -
213 s->gop_picture_number);
214 s->fake_picture_number++;
221 /* insert a fake P picture */
222 static void mpeg1_skip_picture(MpegEncContext *s, int pict_num)
224 unsigned int mb_incr;
226 /* mpeg1 picture header */
227 put_header(s, PICTURE_START_CODE);
228 /* temporal reference */
229 put_bits(&s->pb, 10, pict_num & 0x3ff);
231 put_bits(&s->pb, 3, P_TYPE);
232 put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */
234 put_bits(&s->pb, 1, 1); /* integer coordinates */
235 put_bits(&s->pb, 3, 1); /* forward_f_code */
237 put_bits(&s->pb, 1, 0); /* extra bit picture */
240 put_header(s, SLICE_MIN_START_CODE);
241 put_bits(&s->pb, 5, 1); /* quantizer scale */
242 put_bits(&s->pb, 1, 0); /* slice extra information */
245 put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1],
246 mbAddrIncrTable[mb_incr - 1][0]);
248 /* empty macroblock */
249 put_bits(&s->pb, 3, 1); /* motion only */
251 /* zero motion x & y */
252 put_bits(&s->pb, 1, 1);
253 put_bits(&s->pb, 1, 1);
255 /* output a number of empty slice */
256 mb_incr = s->mb_width * s->mb_height - 1;
257 while (mb_incr > 33) {
258 put_bits(&s->pb, 11, 0x008);
261 put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1],
262 mbAddrIncrTable[mb_incr - 1][0]);
264 /* empty macroblock */
265 put_bits(&s->pb, 3, 1); /* motion only */
267 /* zero motion x & y */
268 put_bits(&s->pb, 1, 1);
269 put_bits(&s->pb, 1, 1);
272 static void common_init(MpegEncContext *s)
275 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
278 void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
280 mpeg1_encode_sequence_header(s);
282 /* mpeg1 picture header */
283 put_header(s, PICTURE_START_CODE);
284 /* temporal reference */
285 put_bits(&s->pb, 10, (s->fake_picture_number -
286 s->gop_picture_number) & 0x3ff);
287 s->fake_picture_number++;
289 put_bits(&s->pb, 3, s->pict_type);
290 put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */
292 if (s->pict_type == P_TYPE) {
293 put_bits(&s->pb, 1, 0); /* half pel coordinates */
294 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
297 put_bits(&s->pb, 1, 0); /* extra bit picture */
300 put_header(s, SLICE_MIN_START_CODE);
301 put_bits(&s->pb, 5, s->qscale); /* quantizer scale */
302 put_bits(&s->pb, 1, 0); /* slice extra information */
305 void mpeg1_encode_mb(MpegEncContext *s,
306 DCTELEM block[6][64],
307 int motion_x, int motion_y)
309 int mb_incr, i, cbp, mb_x, mb_y;
317 if (s->block_last_index[i] >= 0)
321 /* skip macroblock, except if first or last macroblock of a slice */
322 if ((cbp | motion_x | motion_y) == 0 &&
323 (!((mb_x | mb_y) == 0 ||
324 (mb_x == s->mb_width - 1 && mb_y == s->mb_height - 1)))) {
326 s->qscale -= s->dquant;
332 mb_incr = s->mb_incr;
334 while (mb_incr > 33) {
335 put_bits(&s->pb, 11, 0x008);
338 put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1],
339 mbAddrIncrTable[mb_incr - 1][0]);
341 if (s->pict_type == I_TYPE) {
342 if(s->dquant && cbp){
343 put_bits(&s->pb, 2, 1); /* macroblock_type : macroblock_quant = 1 */
344 put_bits(&s->pb, 5, s->qscale);
346 put_bits(&s->pb, 1, 1); /* macroblock_type : macroblock_quant = 0 */
347 s->qscale -= s->dquant;
349 s->misc_bits+= get_bits_diff(s);
353 if(s->dquant && cbp){
354 put_bits(&s->pb, 6, 0x01);
355 put_bits(&s->pb, 5, s->qscale);
357 put_bits(&s->pb, 5, 0x03);
358 s->qscale -= s->dquant;
360 s->misc_bits+= get_bits_diff(s);
364 if (motion_x == 0 && motion_y == 0) {
366 put_bits(&s->pb, 5, 1); /* macroblock_pattern & quant */
367 put_bits(&s->pb, 5, s->qscale);
369 put_bits(&s->pb, 2, 1); /* macroblock_pattern only */
371 s->misc_bits+= get_bits_diff(s);
372 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
375 put_bits(&s->pb, 5, 2); /* motion + cbp */
376 put_bits(&s->pb, 5, s->qscale);
378 put_bits(&s->pb, 1, 1); /* motion + cbp */
380 s->misc_bits+= get_bits_diff(s);
381 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0]);
382 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1]);
383 s->mv_bits+= get_bits_diff(s);
384 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
387 put_bits(&s->pb, 3, 1); /* motion only */
388 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0]);
389 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1]);
390 s->qscale -= s->dquant;
391 s->mv_bits+= get_bits_diff(s);
397 if (cbp & (1 << (5 - i))) {
398 mpeg1_encode_block(s, block[i], i);
403 s->i_tex_bits+= get_bits_diff(s);
405 s->p_tex_bits+= get_bits_diff(s);
407 s->last_mv[0][0][0] = motion_x;
408 s->last_mv[0][0][1] = motion_y;
411 static void mpeg1_encode_motion(MpegEncContext *s, int val)
413 int code, bit_size, l, m, bits, range, sign;
419 mbMotionVectorTable[0][1],
420 mbMotionVectorTable[0][0]);
422 bit_size = s->f_code - 1;
423 range = 1 << bit_size;
424 /* modulo encoding */
429 } else if (val >= l) {
435 code = (val >> bit_size) + 1;
436 bits = val & (range - 1);
441 code = (val >> bit_size) + 1;
442 bits = val & (range - 1);
446 mbMotionVectorTable[code][1],
447 mbMotionVectorTable[code][0]);
448 put_bits(&s->pb, 1, sign);
450 put_bits(&s->pb, bit_size, bits);
455 void ff_mpeg1_encode_init(MpegEncContext *s)
471 mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i];
472 mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i];
475 /* build unified dc encoding tables */
476 for(i=-255; i<256; i++)
484 index = vlc_dc_table[adiff];
486 bits= vlc_dc_lum_bits[index] + index;
487 code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
488 mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
490 bits= vlc_dc_chroma_bits[index] + index;
491 code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
492 mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
495 for(f_code=1; f_code<=MAX_FCODE; f_code++){
496 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
499 if(mv==0) len= mbMotionVectorTable[0][1];
501 int val, bit_size, range, code;
503 bit_size = s->f_code - 1;
504 range = 1 << bit_size;
510 code = (val >> bit_size) + 1;
512 len= mbMotionVectorTable[code][1] + 1 + bit_size;
514 len= mbMotionVectorTable[16][1] + 2 + bit_size;
518 mv_penalty[f_code][mv+MAX_MV]= len;
523 for(f_code=MAX_FCODE; f_code>0; f_code--){
524 for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
525 fcode_tab[mv+MAX_MV]= f_code;
529 s->mv_penalty= mv_penalty;
530 s->fcode_tab= fcode_tab;
533 s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
534 s->inter_quant_bias= 0;
537 static inline void encode_dc(MpegEncContext *s, int diff, int component)
539 if (component == 0) {
542 mpeg1_lum_dc_uni[diff+255]&0xFF,
543 mpeg1_lum_dc_uni[diff+255]>>8);
547 mpeg1_chr_dc_uni[diff+255]&0xFF,
548 mpeg1_chr_dc_uni[diff+255]>>8);
552 static void mpeg1_encode_block(MpegEncContext *s,
556 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
558 // RLTable *rl = &rl_mpeg1;
560 last_index = s->block_last_index[n];
564 component = (n <= 3 ? 0 : n - 4 + 1);
565 dc = block[0]; /* overflow is impossible */
566 diff = dc - s->last_dc[component];
567 encode_dc(s, diff, component);
568 s->last_dc[component] = dc;
571 /* encode the first coefficient : needs to be done here because
572 it is handled slightly differently */
574 if (abs(level) == 1) {
575 code = ((UINT32)level >> 31); /* the sign bit */
576 put_bits(&s->pb, 2, code | 0x02);
585 /* now quantify & encode AC coefs */
586 last_non_zero = i - 1;
588 for(;i<=last_index;i++) {
589 j = s->intra_scantable.permutated[i];
594 dprintf("level[%d]=%d\n", i, level);
596 /* encode using VLC */
598 run = i - last_non_zero - 1;
601 MASK_ABS(sign, alevel)
604 // code = get_rl_index(rl, 0, run, alevel);
605 if (alevel > mpeg1_max_level[0][run])
608 code= mpeg1_index_run[0][run] + alevel - 1;
610 if (code < 111 /* rl->n */) {
611 /* store the vlc & sign at once */
612 put_bits(&s->pb, mpeg1_vlc[code][1]+1, (mpeg1_vlc[code][0]<<1) + sign);
614 /* escape seems to be pretty rare <5% so i dont optimize it */
615 put_bits(&s->pb, mpeg1_vlc[111/*rl->n*/][1], mpeg1_vlc[111/*rl->n*/][0]);
616 /* escape: only clip in this case */
617 put_bits(&s->pb, 6, run);
619 put_bits(&s->pb, 8, level & 0xff);
622 put_bits(&s->pb, 16, 0x8001 + level + 255);
624 put_bits(&s->pb, 16, level & 0xffff);
632 put_bits(&s->pb, 2, 0x2);
635 /******************************************/
638 static VLC dc_lum_vlc;
639 static VLC dc_chroma_vlc;
641 static VLC mbincr_vlc;
642 static VLC mb_ptype_vlc;
643 static VLC mb_btype_vlc;
644 static VLC mb_pat_vlc;
646 static void init_vlcs(MpegEncContext *s)
653 init_vlc(&dc_lum_vlc, DC_VLC_BITS, 12,
654 vlc_dc_lum_bits, 1, 1,
655 vlc_dc_lum_code, 2, 2);
656 init_vlc(&dc_chroma_vlc, DC_VLC_BITS, 12,
657 vlc_dc_chroma_bits, 1, 1,
658 vlc_dc_chroma_code, 2, 2);
659 init_vlc(&mv_vlc, MV_VLC_BITS, 17,
660 &mbMotionVectorTable[0][1], 2, 1,
661 &mbMotionVectorTable[0][0], 2, 1);
662 init_vlc(&mbincr_vlc, MBINCR_VLC_BITS, 35,
663 &mbAddrIncrTable[0][1], 2, 1,
664 &mbAddrIncrTable[0][0], 2, 1);
665 init_vlc(&mb_pat_vlc, MB_PAT_VLC_BITS, 63,
666 &mbPatTable[0][1], 2, 1,
667 &mbPatTable[0][0], 2, 1);
669 init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 32,
670 &table_mb_ptype[0][1], 2, 1,
671 &table_mb_ptype[0][0], 2, 1);
672 init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 32,
673 &table_mb_btype[0][1], 2, 1,
674 &table_mb_btype[0][0], 2, 1);
678 init_2d_vlc_rl(&rl_mpeg1);
679 init_2d_vlc_rl(&rl_mpeg2);
683 static inline int get_dmv(MpegEncContext *s)
685 if(get_bits1(&s->gb))
686 return 1 - (get_bits1(&s->gb) << 1);
691 static inline int get_qscale(MpegEncContext *s)
695 if (s->q_scale_type) {
696 qscale = non_linear_qscale[get_bits(&s->gb, 5)];
698 qscale = get_bits(&s->gb, 5) << 1;
701 /* for mpeg1, we use the generic unquant code */
702 qscale = get_bits(&s->gb, 5);
707 /* motion type (for mpeg2) */
713 static int mpeg_decode_mb(MpegEncContext *s,
714 DCTELEM block[6][64])
716 int i, j, k, cbp, val, mb_type, motion_type;
718 dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
720 if (--s->mb_incr != 0) {
724 s->block_last_index[i] = -1;
725 s->mv_type = MV_TYPE_16X16;
726 if (s->pict_type == P_TYPE) {
727 /* if P type, zero motion vector is implied */
728 s->mv_dir = MV_DIR_FORWARD;
729 s->mv[0][0][0] = s->mv[0][0][1] = 0;
730 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
731 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
733 /* if B type, reuse previous vectors and directions */
734 s->mv[0][0][0] = s->last_mv[0][0][0];
735 s->mv[0][0][1] = s->last_mv[0][0][1];
736 s->mv[1][0][0] = s->last_mv[1][0][0];
737 s->mv[1][0][1] = s->last_mv[1][0][1];
744 switch(s->pict_type) {
747 if (get_bits1(&s->gb) == 0) {
748 if (get_bits1(&s->gb) == 0)
750 mb_type = MB_QUANT | MB_INTRA;
756 mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
758 fprintf(stderr, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
763 mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
765 fprintf(stderr, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
770 dprintf("mb_type=%x\n", mb_type);
771 motion_type = 0; /* avoid warning */
772 if (mb_type & (MB_FOR|MB_BACK)) {
773 /* get additionnal motion vector type */
774 if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct)
775 motion_type = MT_FRAME;
777 motion_type = get_bits(&s->gb, 2);
779 /* compute dct type */
780 if (s->picture_structure == PICT_FRAME &&
781 !s->frame_pred_frame_dct &&
782 (mb_type & (MB_PAT | MB_INTRA))) {
783 s->interlaced_dct = get_bits1(&s->gb);
785 if (s->interlaced_dct)
786 printf("interlaced_dct\n");
789 s->interlaced_dct = 0; /* frame based */
792 if (mb_type & MB_QUANT) {
793 s->qscale = get_qscale(s);
795 if (mb_type & MB_INTRA) {
796 if (s->concealment_motion_vectors) {
797 /* just parse them */
798 if (s->picture_structure != PICT_FRAME)
799 skip_bits1(&s->gb); /* field select */
800 mpeg_decode_motion(s, s->mpeg_f_code[0][0], 0);
801 mpeg_decode_motion(s, s->mpeg_f_code[0][1], 0);
805 memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
810 /* special case of implicit zero motion vector */
811 if (s->pict_type == P_TYPE && !(mb_type & MB_FOR)) {
812 s->mv_dir = MV_DIR_FORWARD;
813 s->mv_type = MV_TYPE_16X16;
814 s->last_mv[0][0][0] = 0;
815 s->last_mv[0][0][1] = 0;
816 s->last_mv[0][1][0] = 0;
817 s->last_mv[0][1][1] = 0;
820 } else if (mb_type & (MB_FOR | MB_BACK)) {
824 if (mb_type & (MB_FOR >> i)) {
825 s->mv_dir |= (MV_DIR_FORWARD >> i);
826 dprintf("motion_type=%d\n", motion_type);
827 switch(motion_type) {
828 case MT_FRAME: /* or MT_16X8 */
829 if (s->picture_structure == PICT_FRAME) {
831 s->mv_type = MV_TYPE_16X16;
833 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
834 s->last_mv[i][0][k]);
835 s->last_mv[i][0][k] = val;
836 s->last_mv[i][1][k] = val;
837 /* full_pel: only for mpeg1 */
840 s->mv[i][0][k] = val;
841 dprintf("mv%d: %d\n", k, val);
845 s->mv_type = MV_TYPE_16X8;
847 s->field_select[i][j] = get_bits1(&s->gb);
849 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
850 s->last_mv[i][j][k]);
851 s->last_mv[i][j][k] = val;
852 s->mv[i][j][k] = val;
858 if (s->picture_structure == PICT_FRAME) {
859 s->mv_type = MV_TYPE_FIELD;
861 s->field_select[i][j] = get_bits1(&s->gb);
862 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
863 s->last_mv[i][j][0]);
864 s->last_mv[i][j][0] = val;
865 s->mv[i][j][0] = val;
866 dprintf("fmx=%d\n", val);
867 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
868 s->last_mv[i][j][1] >> 1);
869 s->last_mv[i][j][1] = val << 1;
870 s->mv[i][j][1] = val;
871 dprintf("fmy=%d\n", val);
874 s->mv_type = MV_TYPE_16X16;
875 s->field_select[i][0] = get_bits1(&s->gb);
877 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
878 s->last_mv[i][0][k]);
879 s->last_mv[i][0][k] = val;
880 s->last_mv[i][1][k] = val;
881 s->mv[i][0][k] = val;
887 int dmx, dmy, mx, my, m;
889 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
890 s->last_mv[i][0][0]);
891 s->last_mv[i][0][0] = mx;
892 s->last_mv[i][1][0] = mx;
894 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
895 s->last_mv[i][0][1] >> 1);
897 s->mv_type = MV_TYPE_DMV;
898 /* XXX: totally broken */
899 if (s->picture_structure == PICT_FRAME) {
900 s->last_mv[i][0][1] = my << 1;
901 s->last_mv[i][1][1] = my << 1;
903 m = s->top_field_first ? 1 : 3;
904 /* top -> top pred */
906 s->mv[i][0][1] = my << 1;
907 s->mv[i][1][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
908 s->mv[i][1][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
911 s->mv[i][2][1] = my << 1;
912 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
913 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
915 s->last_mv[i][0][1] = my;
916 s->last_mv[i][1][1] = my;
919 s->mv[i][1][0] = ((mx + (mx > 0)) >> 1) + dmx;
920 s->mv[i][1][1] = ((my + (my > 0)) >> 1) + dmy - 1
921 /* + 2 * cur_field */;
930 if ((mb_type & MB_INTRA) && s->concealment_motion_vectors) {
931 skip_bits1(&s->gb); /* marker */
934 if (mb_type & MB_PAT) {
935 cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
937 fprintf(stderr, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
942 dprintf("cbp=%x\n", cbp);
947 if (mpeg2_decode_block_intra(s, block[i], i) < 0)
953 if (mpeg2_decode_block_non_intra(s, block[i], i) < 0)
956 s->block_last_index[i] = -1;
964 if (mpeg1_decode_block_intra(s, block[i], i) < 0)
970 if (mpeg1_decode_block_inter(s, block[i], i) < 0)
973 s->block_last_index[i] = -1;
982 /* as h263, but only 17 codes */
983 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
985 int code, sign, val, m, l, shift;
987 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
994 sign = get_bits1(&s->gb);
996 val = (code - 1) << shift;
998 val |= get_bits(&s->gb, shift);
1004 /* modulo decoding */
1005 l = (1 << shift) * 16;
1009 } else if (val >= l) {
1015 static inline int decode_dc(MpegEncContext *s, int component)
1019 if (component == 0) {
1020 code = get_vlc2(&s->gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
1022 code = get_vlc2(&s->gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
1025 fprintf(stderr, "invalid dc code at %d %d\n", s->mb_x, s->mb_y);
1031 diff = get_bits(&s->gb, code);
1032 if ((diff & (1 << (code - 1))) == 0)
1033 diff = (-1 << code) | (diff + 1);
1038 static inline int mpeg1_decode_block_intra(MpegEncContext *s,
1042 int level, dc, diff, i, j, run;
1044 RLTable *rl = &rl_mpeg1;
1045 UINT8 * const scantable= s->intra_scantable.permutated;
1046 const UINT16 *quant_matrix= s->intra_matrix;
1047 const int qscale= s->qscale;
1050 component = (n <= 3 ? 0 : n - 4 + 1);
1051 diff = decode_dc(s, component);
1054 dc = s->last_dc[component];
1056 s->last_dc[component] = dc;
1058 dprintf("dc=%d diff=%d\n", dc, diff);
1061 OPEN_READER(re, &s->gb);
1062 /* now quantify & encode AC coefs */
1064 UPDATE_CACHE(re, &s->gb);
1065 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
1069 } else if(level != 0) {
1072 level= (level*qscale*quant_matrix[j])>>3;
1074 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1075 LAST_SKIP_BITS(re, &s->gb, 1);
1078 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1079 UPDATE_CACHE(re, &s->gb);
1080 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1081 if (level == -128) {
1082 level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
1083 } else if (level == 0) {
1084 level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
1090 level= (level*qscale*quant_matrix[j])>>3;
1094 level= (level*qscale*quant_matrix[j])>>3;
1099 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1105 CLOSE_READER(re, &s->gb);
1107 s->block_last_index[n] = i;
1111 static inline int mpeg1_decode_block_inter(MpegEncContext *s,
1115 int level, i, j, run;
1116 RLTable *rl = &rl_mpeg1;
1117 UINT8 * const scantable= s->intra_scantable.permutated;
1118 const UINT16 *quant_matrix= s->inter_matrix;
1119 const int qscale= s->qscale;
1123 OPEN_READER(re, &s->gb);
1125 /* special case for the first coef. no need to add a second vlc table */
1126 UPDATE_CACHE(re, &s->gb);
1127 v= SHOW_UBITS(re, &s->gb, 2);
1129 LAST_SKIP_BITS(re, &s->gb, 2);
1130 level= (3*qscale*quant_matrix[0])>>4;
1138 /* now quantify & encode AC coefs */
1140 UPDATE_CACHE(re, &s->gb);
1141 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
1145 } else if(level != 0) {
1148 level= ((level*2+1)*qscale*quant_matrix[j])>>4;
1150 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1151 LAST_SKIP_BITS(re, &s->gb, 1);
1154 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1155 UPDATE_CACHE(re, &s->gb);
1156 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1157 if (level == -128) {
1158 level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
1159 } else if (level == 0) {
1160 level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
1166 level= ((level*2+1)*qscale*quant_matrix[j])>>4;
1170 level= ((level*2+1)*qscale*quant_matrix[j])>>4;
1175 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1181 CLOSE_READER(re, &s->gb);
1183 s->block_last_index[n] = i;
1187 /* Also does unquantization here, since I will never support mpeg2
1189 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
1193 int level, i, j, run;
1194 RLTable *rl = &rl_mpeg1;
1195 UINT8 * const scantable= s->intra_scantable.permutated;
1196 const UINT16 *quant_matrix;
1197 const int qscale= s->qscale;
1204 OPEN_READER(re, &s->gb);
1207 quant_matrix = s->inter_matrix;
1209 quant_matrix = s->chroma_inter_matrix;
1211 /* special case for the first coef. no need to add a second vlc table */
1212 UPDATE_CACHE(re, &s->gb);
1213 v= SHOW_UBITS(re, &s->gb, 2);
1215 LAST_SKIP_BITS(re, &s->gb, 2);
1216 level= (3*qscale*quant_matrix[0])>>5;
1224 /* now quantify & encode AC coefs */
1226 UPDATE_CACHE(re, &s->gb);
1227 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
1231 } else if(level != 0) {
1234 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1235 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1236 LAST_SKIP_BITS(re, &s->gb, 1);
1239 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1240 UPDATE_CACHE(re, &s->gb);
1241 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1246 level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
1249 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1253 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1260 CLOSE_READER(re, &s->gb);
1262 block[63] ^= (mismatch & 1);
1264 s->block_last_index[n] = i;
1268 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
1272 int level, dc, diff, i, j, run;
1275 UINT8 * const scantable= s->intra_scantable.permutated;
1276 const UINT16 *quant_matrix;
1277 const int qscale= s->qscale;
1282 quant_matrix = s->intra_matrix;
1285 quant_matrix = s->chroma_intra_matrix;
1288 diff = decode_dc(s, component);
1291 dc = s->last_dc[component];
1293 s->last_dc[component] = dc;
1294 block[0] = dc << (3 - s->intra_dc_precision);
1295 dprintf("dc=%d\n", block[0]);
1296 mismatch = block[0] ^ 1;
1298 if (s->intra_vlc_format)
1304 OPEN_READER(re, &s->gb);
1305 /* now quantify & encode AC coefs */
1307 UPDATE_CACHE(re, &s->gb);
1308 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
1312 } else if(level != 0) {
1315 level= (level*qscale*quant_matrix[j])>>4;
1316 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1317 LAST_SKIP_BITS(re, &s->gb, 1);
1320 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1321 UPDATE_CACHE(re, &s->gb);
1322 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1326 level= (-level*qscale*quant_matrix[j])>>4;
1329 level= (level*qscale*quant_matrix[j])>>4;
1333 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1340 CLOSE_READER(re, &s->gb);
1342 block[63]^= mismatch&1;
1344 s->block_last_index[n] = i;
1348 /* compressed picture size */
1349 #define PICTURE_BUFFER_SIZE 100000
1351 typedef struct Mpeg1Context {
1352 MpegEncContext mpeg_enc_ctx;
1353 UINT32 header_state;
1354 int start_code; /* current start code */
1355 UINT8 buffer[PICTURE_BUFFER_SIZE];
1358 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
1359 int repeat_field; /* true if we must repeat the field */
1362 static int mpeg_decode_init(AVCodecContext *avctx)
1364 Mpeg1Context *s = avctx->priv_data;
1366 s->mpeg_enc_ctx.flags= avctx->flags;
1367 common_init(&s->mpeg_enc_ctx);
1368 init_vlcs(&s->mpeg_enc_ctx);
1370 s->header_state = 0xff;
1371 s->mpeg_enc_ctx_allocated = 0;
1372 s->buffer_size = PICTURE_BUFFER_SIZE;
1374 s->buf_ptr = s->buffer;
1375 s->mpeg_enc_ctx.picture_number = 0;
1376 s->repeat_field = 0;
1377 s->mpeg_enc_ctx.codec_id= avctx->codec->id;
1381 /* return the 8 bit start code value and update the search
1382 state. Return -1 if no start code found */
1383 static int find_start_code(UINT8 **pbuf_ptr, UINT8 *buf_end,
1384 UINT32 *header_state)
1387 unsigned int state, v;
1390 state = *header_state;
1391 buf_ptr = *pbuf_ptr;
1392 while (buf_ptr < buf_end) {
1394 if (state == 0x000001) {
1395 state = ((state << 8) | v) & 0xffffff;
1399 state = ((state << 8) | v) & 0xffffff;
1403 *pbuf_ptr = buf_ptr;
1404 *header_state = state;
1408 static int mpeg1_decode_picture(AVCodecContext *avctx,
1409 UINT8 *buf, int buf_size)
1411 Mpeg1Context *s1 = avctx->priv_data;
1412 MpegEncContext *s = &s1->mpeg_enc_ctx;
1415 init_get_bits(&s->gb, buf, buf_size);
1417 ref = get_bits(&s->gb, 10); /* temporal ref */
1418 s->pict_type = get_bits(&s->gb, 3);
1419 dprintf("pict_type=%d number=%d\n", s->pict_type, s->picture_number);
1421 skip_bits(&s->gb, 16);
1422 if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
1423 s->full_pel[0] = get_bits1(&s->gb);
1424 f_code = get_bits(&s->gb, 3);
1427 s->mpeg_f_code[0][0] = f_code;
1428 s->mpeg_f_code[0][1] = f_code;
1430 if (s->pict_type == B_TYPE) {
1431 s->full_pel[1] = get_bits1(&s->gb);
1432 f_code = get_bits(&s->gb, 3);
1435 s->mpeg_f_code[1][0] = f_code;
1436 s->mpeg_f_code[1][1] = f_code;
1438 s->current_picture.pict_type= s->pict_type;
1439 s->current_picture.key_frame= s->pict_type == I_TYPE;
1447 static void mpeg_decode_sequence_extension(MpegEncContext *s)
1449 int horiz_size_ext, vert_size_ext;
1450 int bit_rate_ext, vbv_buf_ext;
1451 int frame_rate_ext_n, frame_rate_ext_d;
1454 skip_bits(&s->gb, 8); /* profil and level */
1455 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1456 skip_bits(&s->gb, 2); /* chroma_format */
1457 horiz_size_ext = get_bits(&s->gb, 2);
1458 vert_size_ext = get_bits(&s->gb, 2);
1459 s->width |= (horiz_size_ext << 12);
1460 s->height |= (vert_size_ext << 12);
1461 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1462 s->bit_rate = ((s->bit_rate / 400) | (bit_rate_ext << 12)) * 400;
1463 skip_bits1(&s->gb); /* marker */
1464 vbv_buf_ext = get_bits(&s->gb, 8);
1465 s->low_delay = get_bits1(&s->gb);
1466 frame_rate_ext_n = get_bits(&s->gb, 2);
1467 frame_rate_ext_d = get_bits(&s->gb, 5);
1468 if (frame_rate_ext_d >= 1)
1469 s->frame_rate = (s->frame_rate * frame_rate_ext_n) / frame_rate_ext_d;
1470 dprintf("sequence extension\n");
1472 s->avctx->sub_id = 2; /* indicates mpeg2 found */
1474 aspect= mpeg2_aspect[s->aspect_ratio_info];
1475 if(aspect>0.0) s->avctx->aspect_ratio= s->width/(aspect*s->height);
1476 else if(aspect<0.0) s->avctx->aspect_ratio= -1.0/aspect;
1479 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1483 dprintf("matrix extension\n");
1485 if (get_bits1(&s->gb)) {
1487 v = get_bits(&s->gb, 8);
1488 j= s->idct_permutation[ ff_zigzag_direct[i] ];
1489 s->intra_matrix[j] = v;
1490 s->chroma_intra_matrix[j] = v;
1493 if (get_bits1(&s->gb)) {
1495 v = get_bits(&s->gb, 8);
1496 j= s->idct_permutation[ ff_zigzag_direct[i] ];
1497 s->inter_matrix[j] = v;
1498 s->chroma_inter_matrix[j] = v;
1501 if (get_bits1(&s->gb)) {
1503 v = get_bits(&s->gb, 8);
1504 j= s->idct_permutation[ ff_zigzag_direct[i] ];
1505 s->chroma_intra_matrix[j] = v;
1508 if (get_bits1(&s->gb)) {
1510 v = get_bits(&s->gb, 8);
1511 j= s->idct_permutation[ ff_zigzag_direct[i] ];
1512 s->chroma_inter_matrix[j] = v;
1517 static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
1519 s->full_pel[0] = s->full_pel[1] = 0;
1520 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1521 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1522 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1523 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1524 s->intra_dc_precision = get_bits(&s->gb, 2);
1525 s->picture_structure = get_bits(&s->gb, 2);
1526 s->top_field_first = get_bits1(&s->gb);
1527 s->frame_pred_frame_dct = get_bits1(&s->gb);
1528 s->concealment_motion_vectors = get_bits1(&s->gb);
1529 s->q_scale_type = get_bits1(&s->gb);
1530 s->intra_vlc_format = get_bits1(&s->gb);
1531 s->alternate_scan = get_bits1(&s->gb);
1532 s->repeat_first_field = get_bits1(&s->gb);
1533 s->chroma_420_type = get_bits1(&s->gb);
1534 s->progressive_frame = get_bits1(&s->gb);
1536 if(s->alternate_scan){
1537 ff_init_scantable(s, &s->inter_scantable , ff_alternate_vertical_scan);
1538 ff_init_scantable(s, &s->intra_scantable , ff_alternate_vertical_scan);
1539 ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_vertical_scan);
1540 ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
1542 ff_init_scantable(s, &s->inter_scantable , ff_zigzag_direct);
1543 ff_init_scantable(s, &s->intra_scantable , ff_zigzag_direct);
1544 ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_horizontal_scan);
1545 ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
1548 /* composite display not parsed */
1549 dprintf("intra_dc_precision=%d\n", s->intra_dc_precision);
1550 dprintf("picture_structure=%d\n", s->picture_structure);
1551 dprintf("top field first=%d\n", s->top_field_first);
1552 dprintf("repeat first field=%d\n", s->repeat_first_field);
1553 dprintf("conceal=%d\n", s->concealment_motion_vectors);
1554 dprintf("intra_vlc_format=%d\n", s->intra_vlc_format);
1555 dprintf("alternate_scan=%d\n", s->alternate_scan);
1556 dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1557 dprintf("progressive_frame=%d\n", s->progressive_frame);
1560 static void mpeg_decode_extension(AVCodecContext *avctx,
1561 UINT8 *buf, int buf_size)
1563 Mpeg1Context *s1 = avctx->priv_data;
1564 MpegEncContext *s = &s1->mpeg_enc_ctx;
1567 init_get_bits(&s->gb, buf, buf_size);
1569 ext_type = get_bits(&s->gb, 4);
1573 mpeg_decode_sequence_extension(s);
1576 /* quant matrix extension */
1577 mpeg_decode_quant_matrix_extension(s);
1580 /* picture extension */
1581 mpeg_decode_picture_coding_extension(s);
1586 #define DECODE_SLICE_FATAL_ERROR -2
1587 #define DECODE_SLICE_ERROR -1
1588 #define DECODE_SLICE_OK 0
1589 #define DECODE_SLICE_EOP 1
1593 * @return DECODE_SLICE_FATAL_ERROR if a non recoverable error occured<br>
1594 * DECODE_SLICE_ERROR if the slice is damaged<br>
1595 * DECODE_SLICE_OK if this slice is ok<br>
1596 * DECODE_SLICE_EOP if the end of the picture is reached
1598 static int mpeg_decode_slice(AVCodecContext *avctx,
1601 UINT8 *buf, int buf_size)
1603 Mpeg1Context *s1 = avctx->priv_data;
1604 MpegEncContext *s = &s1->mpeg_enc_ctx;
1607 start_code = (start_code - 1) & 0xff;
1608 if (start_code >= s->mb_height){
1609 fprintf(stderr, "slice below image (%d >= %d)\n", start_code, s->mb_height);
1610 return DECODE_SLICE_ERROR;
1612 s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
1613 s->last_dc[1] = s->last_dc[0];
1614 s->last_dc[2] = s->last_dc[0];
1615 memset(s->last_mv, 0, sizeof(s->last_mv));
1616 /* start frame decoding */
1617 if (s->first_slice) {
1619 if(MPV_frame_start(s, avctx) < 0)
1620 return DECODE_SLICE_FATAL_ERROR;
1622 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
1623 printf("qp:%d fc:%d%d%d%d %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n",
1624 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],
1625 s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")),
1626 s->progressive_sequence ? "pro" :"", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
1627 s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
1628 s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
1632 init_get_bits(&s->gb, buf, buf_size);
1634 s->qscale = get_qscale(s);
1635 /* extra slice info */
1636 while (get_bits1(&s->gb) != 0) {
1637 skip_bits(&s->gb, 8);
1642 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1644 return -1; /* error = end of slice, but empty slice is bad or?*/
1649 /* otherwise, stuffing, nothing to do */
1655 s->mb_y = start_code;
1659 s->dsp.clear_blocks(s->block[0]);
1661 ret = mpeg_decode_mb(s, s->block);
1662 dprintf("ret=%d\n", ret);
1666 MPV_decode_mb(s, s->block);
1668 if (++s->mb_x >= s->mb_width) {
1669 ff_draw_horiz_band(s);
1673 PRINT_QP("%s", "\n");
1675 PRINT_QP("%2d", s->qscale);
1677 /* skip mb handling */
1678 if (s->mb_incr == 0) {
1679 /* read again increment */
1682 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1684 goto eos; /* error = end of slice */
1689 /* otherwise, stuffing, nothing to do */
1696 if(s->mb_y >= s->mb_height){
1697 fprintf(stderr, "slice too long\n");
1698 return DECODE_SLICE_ERROR;
1705 /* end of slice reached */
1706 if (/*s->mb_x == 0 &&*/
1707 s->mb_y == s->mb_height) {
1715 if (s->pict_type == B_TYPE || s->low_delay) {
1716 *pict= *(AVFrame*)&s->current_picture;
1718 s->picture_number++;
1719 /* latency of 1 frame for I and P frames */
1720 /* XXX: use another variable than picture_number */
1721 if (s->last_picture.data[0] == NULL) {
1722 return DECODE_SLICE_OK;
1724 *pict= *(AVFrame*)&s->last_picture;
1727 return DECODE_SLICE_EOP;
1729 return DECODE_SLICE_OK;
1733 static int mpeg1_decode_sequence(AVCodecContext *avctx,
1734 UINT8 *buf, int buf_size)
1736 Mpeg1Context *s1 = avctx->priv_data;
1737 MpegEncContext *s = &s1->mpeg_enc_ctx;
1738 int width, height, i, v, j;
1741 init_get_bits(&s->gb, buf, buf_size);
1743 width = get_bits(&s->gb, 12);
1744 height = get_bits(&s->gb, 12);
1745 s->aspect_ratio_info= get_bits(&s->gb, 4);
1747 aspect= mpeg1_aspect[s->aspect_ratio_info];
1748 if(aspect!=0.0) avctx->aspect_ratio= width/(aspect*height);
1751 s->frame_rate_index = get_bits(&s->gb, 4);
1752 if (s->frame_rate_index == 0)
1754 s->bit_rate = get_bits(&s->gb, 18) * 400;
1755 if (get_bits1(&s->gb) == 0) /* marker */
1757 if (width <= 0 || height <= 0 ||
1758 (width % 2) != 0 || (height % 2) != 0)
1760 if (width != s->width ||
1761 height != s->height) {
1762 /* start new mpeg1 context decoding */
1763 s->out_format = FMT_MPEG1;
1764 if (s1->mpeg_enc_ctx_allocated) {
1769 avctx->has_b_frames= 1;
1771 avctx->width = width;
1772 avctx->height = height;
1773 if (s->frame_rate_index >= 9) {
1774 /* at least give a valid frame rate (some old mpeg1 have this) */
1775 avctx->frame_rate = 25 * FRAME_RATE_BASE;
1777 avctx->frame_rate = frame_rate_tab[s->frame_rate_index];
1779 s->frame_rate = avctx->frame_rate;
1780 avctx->bit_rate = s->bit_rate;
1782 if (MPV_common_init(s) < 0)
1784 s1->mpeg_enc_ctx_allocated = 1;
1787 skip_bits(&s->gb, 10); /* vbv_buffer_size */
1788 skip_bits(&s->gb, 1);
1791 if (get_bits1(&s->gb)) {
1793 v = get_bits(&s->gb, 8);
1794 j = s->intra_scantable.permutated[i];
1795 s->intra_matrix[j] = v;
1796 s->chroma_intra_matrix[j] = v;
1799 dprintf("intra matrix present\n");
1801 dprintf(" %d", s->intra_matrix[s->intra_scantable.permutated[i]]);
1806 int j= s->idct_permutation[i];
1807 v = ff_mpeg1_default_intra_matrix[i];
1808 s->intra_matrix[j] = v;
1809 s->chroma_intra_matrix[j] = v;
1812 if (get_bits1(&s->gb)) {
1814 v = get_bits(&s->gb, 8);
1815 j = s->intra_scantable.permutated[i];
1816 s->inter_matrix[j] = v;
1817 s->chroma_inter_matrix[j] = v;
1820 dprintf("non intra matrix present\n");
1822 dprintf(" %d", s->inter_matrix[s->intra_scantable.permutated[i]]);
1827 int j= s->idct_permutation[i];
1828 v = ff_mpeg1_default_non_intra_matrix[i];
1829 s->inter_matrix[j] = v;
1830 s->chroma_inter_matrix[j] = v;
1834 /* we set mpeg2 parameters so that it emulates mpeg1 */
1835 s->progressive_sequence = 1;
1836 s->progressive_frame = 1;
1837 s->picture_structure = PICT_FRAME;
1838 s->frame_pred_frame_dct = 1;
1840 avctx->sub_id = 1; /* indicates mpeg1 */
1844 /* handle buffering and image synchronisation */
1845 static int mpeg_decode_frame(AVCodecContext *avctx,
1846 void *data, int *data_size,
1847 UINT8 *buf, int buf_size)
1849 Mpeg1Context *s = avctx->priv_data;
1850 UINT8 *buf_end, *buf_ptr, *buf_start;
1851 int len, start_code_found, ret, code, start_code, input_size;
1852 AVFrame *picture = data;
1853 MpegEncContext *s2 = &s->mpeg_enc_ctx;
1855 dprintf("fill_buffer\n");
1859 /* special case for last picture */
1860 if (buf_size == 0) {
1861 if (s2->picture_number > 0) {
1862 *picture= *(AVFrame*)&s2->next_picture;
1864 *data_size = sizeof(AVFrame);
1870 buf_end = buf + buf_size;
1873 if (s->repeat_field % 2 == 1) {
1875 //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
1876 // s2->picture_number, s->repeat_field);
1877 if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
1878 *data_size = sizeof(AVPicture);
1883 while (buf_ptr < buf_end) {
1884 buf_start = buf_ptr;
1885 /* find start next code */
1886 code = find_start_code(&buf_ptr, buf_end, &s->header_state);
1888 start_code_found = 1;
1890 start_code_found = 0;
1892 /* copy to buffer */
1893 len = buf_ptr - buf_start;
1894 if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
1895 /* data too big : flush */
1896 s->buf_ptr = s->buffer;
1897 if (start_code_found)
1898 s->start_code = code;
1900 memcpy(s->buf_ptr, buf_start, len);
1902 if( (!(s2->flags&CODEC_FLAG_TRUNCATED)) && (!start_code_found)
1903 && s->buf_ptr+4<s->buffer+s->buffer_size){
1904 start_code_found= 1;
1906 s->header_state=0xFF;
1913 if (start_code_found) {
1914 /* prepare data for next start code */
1915 input_size = s->buf_ptr - s->buffer;
1916 start_code = s->start_code;
1917 s->buf_ptr = s->buffer;
1918 s->start_code = code;
1919 switch(start_code) {
1920 case SEQ_START_CODE:
1921 mpeg1_decode_sequence(avctx, s->buffer,
1925 case PICTURE_START_CODE:
1926 /* we have a complete image : we try to decompress it */
1927 mpeg1_decode_picture(avctx,
1928 s->buffer, input_size);
1930 case EXT_START_CODE:
1931 mpeg_decode_extension(avctx,
1932 s->buffer, input_size);
1935 if (start_code >= SLICE_MIN_START_CODE &&
1936 start_code <= SLICE_MAX_START_CODE) {
1938 /* skip b frames if we dont have reference frames */
1939 if(s2->last_picture.data[0]==NULL && s2->pict_type==B_TYPE) break;
1940 /* skip b frames if we are in a hurry */
1941 if(avctx->hurry_up && s2->pict_type==B_TYPE) break;
1942 /* skip everything if we are in a hurry>=5 */
1943 if(avctx->hurry_up>=5) break;
1945 ret = mpeg_decode_slice(avctx, picture,
1946 start_code, s->buffer, input_size);
1947 if (ret == DECODE_SLICE_EOP) {
1948 /* got a picture: exit */
1949 /* first check if we must repeat the frame */
1950 avctx->repeat_pict = 0;
1952 if (s2->progressive_frame && s2->repeat_first_field) {
1953 //fprintf(stderr,"\nRepeat this frame: %d! pict: %d",avctx->frame_number,s2->picture_number);
1954 //s2->repeat_first_field = 0;
1955 //s2->progressive_frame = 0;
1956 if (++s->repeat_field > 2)
1957 s->repeat_field = 0;
1958 avctx->repeat_pict = 1;
1961 if (s2->repeat_first_field) {
1962 if (s2->progressive_sequence) {
1963 if (s2->top_field_first)
1964 avctx->repeat_pict = 4;
1966 avctx->repeat_pict = 2;
1967 } else if (s2->progressive_frame) {
1968 avctx->repeat_pict = 1;
1971 *data_size = sizeof(AVPicture);
1974 fprintf(stderr,"Error while decoding slice\n");
1975 if(ret==DECODE_SLICE_FATAL_ERROR) return -1;
1984 return buf_ptr - buf;
1987 static int mpeg_decode_end(AVCodecContext *avctx)
1989 Mpeg1Context *s = avctx->priv_data;
1991 if (s->mpeg_enc_ctx_allocated)
1992 MPV_common_end(&s->mpeg_enc_ctx);
1996 AVCodec mpeg_decoder = {
1999 CODEC_ID_MPEG1VIDEO,
2000 sizeof(Mpeg1Context),
2005 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,