2 * MPEG1 encoder / MPEG2 decoder
3 * Copyright (c) 2000,2001 Gerard Lantau.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program 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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "mpegvideo.h"
24 #include "mpeg12data.h"
27 #define SEQ_END_CODE 0x000001b7
28 #define SEQ_START_CODE 0x000001b3
29 #define GOP_START_CODE 0x000001b8
30 #define PICTURE_START_CODE 0x00000100
31 #define SLICE_MIN_START_CODE 0x00000101
32 #define SLICE_MAX_START_CODE 0x000001af
33 #define EXT_START_CODE 0x000001b5
34 #define USER_START_CODE 0x000001b2
36 #define ABS(a) ((a)<0 ? -(a) : (a))
38 static void mpeg1_encode_block(MpegEncContext *s,
41 static void mpeg1_encode_motion(MpegEncContext *s, int val);
42 static void mpeg1_skip_picture(MpegEncContext *s, int pict_num);
43 static int mpeg1_decode_block(MpegEncContext *s,
46 static int mpeg2_decode_block_non_intra(MpegEncContext *s,
49 static int mpeg2_decode_block_intra(MpegEncContext *s,
52 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
54 static UINT16 mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
55 static UINT8 fcode_tab[MAX_MV*2+1];
57 static void put_header(MpegEncContext *s, int header)
59 align_put_bits(&s->pb);
60 put_bits(&s->pb, 16, header>>16);
61 put_bits(&s->pb, 16, header&0xFFFF);
64 /* put sequence header if needed */
65 static void mpeg1_encode_sequence_header(MpegEncContext *s)
67 unsigned int vbv_buffer_size;
72 if (s->picture_in_gop_number == 0) {
73 /* mpeg1 header repeated every gop */
74 put_header(s, SEQ_START_CODE);
76 /* search closest frame rate */
79 s->frame_rate_index = 0;
82 d = abs(s->frame_rate - frame_rate_tab[i]);
85 s->frame_rate_index = i;
90 put_bits(&s->pb, 12, s->width);
91 put_bits(&s->pb, 12, s->height);
92 put_bits(&s->pb, 4, 1); /* 1/1 aspect ratio */
93 put_bits(&s->pb, 4, s->frame_rate_index);
94 v = s->bit_rate / 400;
97 put_bits(&s->pb, 18, v);
98 put_bits(&s->pb, 1, 1); /* marker */
99 /* vbv buffer size: slightly greater than an I frame. We add
100 some margin just in case */
101 vbv_buffer_size = (3 * s->I_frame_bits) / (2 * 8);
102 put_bits(&s->pb, 10, (vbv_buffer_size + 16383) / 16384);
103 put_bits(&s->pb, 1, 1); /* constrained parameter flag */
104 put_bits(&s->pb, 1, 0); /* no custom intra matrix */
105 put_bits(&s->pb, 1, 0); /* no custom non intra matrix */
107 put_header(s, GOP_START_CODE);
108 put_bits(&s->pb, 1, 0); /* do drop frame */
109 /* time code : we must convert from the real frame rate to a
110 fake mpeg frame rate in case of low frame rate */
111 fps = frame_rate_tab[s->frame_rate_index];
112 time_code = (INT64)s->fake_picture_number * FRAME_RATE_BASE;
113 s->gop_picture_number = s->fake_picture_number;
114 put_bits(&s->pb, 5, (UINT32)((time_code / (fps * 3600)) % 24));
115 put_bits(&s->pb, 6, (UINT32)((time_code / (fps * 60)) % 60));
116 put_bits(&s->pb, 1, 1);
117 put_bits(&s->pb, 6, (UINT32)((time_code / fps) % 60));
118 put_bits(&s->pb, 6, (UINT32)((time_code % fps) / FRAME_RATE_BASE));
119 put_bits(&s->pb, 1, 1); /* closed gop */
120 put_bits(&s->pb, 1, 0); /* broken link */
123 if (s->frame_rate < (24 * FRAME_RATE_BASE) && s->picture_number > 0) {
124 /* insert empty P pictures to slow down to the desired
125 frame rate. Each fake pictures takes about 20 bytes */
126 fps = frame_rate_tab[s->frame_rate_index];
127 n = (((INT64)s->picture_number * fps) / s->frame_rate) - 1;
128 while (s->fake_picture_number < n) {
129 mpeg1_skip_picture(s, s->fake_picture_number -
130 s->gop_picture_number);
131 s->fake_picture_number++;
138 /* insert a fake P picture */
139 static void mpeg1_skip_picture(MpegEncContext *s, int pict_num)
141 unsigned int mb_incr;
143 /* mpeg1 picture header */
144 put_header(s, PICTURE_START_CODE);
145 /* temporal reference */
146 put_bits(&s->pb, 10, pict_num & 0x3ff);
148 put_bits(&s->pb, 3, P_TYPE);
149 put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */
151 put_bits(&s->pb, 1, 1); /* integer coordinates */
152 put_bits(&s->pb, 3, 1); /* forward_f_code */
154 put_bits(&s->pb, 1, 0); /* extra bit picture */
157 put_header(s, SLICE_MIN_START_CODE);
158 put_bits(&s->pb, 5, 1); /* quantizer scale */
159 put_bits(&s->pb, 1, 0); /* slice extra information */
162 put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1],
163 mbAddrIncrTable[mb_incr - 1][0]);
165 /* empty macroblock */
166 put_bits(&s->pb, 3, 1); /* motion only */
168 /* zero motion x & y */
169 put_bits(&s->pb, 1, 1);
170 put_bits(&s->pb, 1, 1);
172 /* output a number of empty slice */
173 mb_incr = s->mb_width * s->mb_height - 1;
174 while (mb_incr > 33) {
175 put_bits(&s->pb, 11, 0x008);
178 put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1],
179 mbAddrIncrTable[mb_incr - 1][0]);
181 /* empty macroblock */
182 put_bits(&s->pb, 3, 1); /* motion only */
184 /* zero motion x & y */
185 put_bits(&s->pb, 1, 1);
186 put_bits(&s->pb, 1, 1);
189 void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
200 mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i];
201 mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i];
204 /* build unified dc encoding tables */
205 for(i=-255; i<256; i++)
213 index = vlc_dc_table[adiff];
215 bits= vlc_dc_lum_bits[index] + index;
216 code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
217 mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
219 bits= vlc_dc_chroma_bits[index] + index;
220 code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
221 mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
224 mpeg1_encode_sequence_header(s);
226 /* mpeg1 picture header */
227 put_header(s, PICTURE_START_CODE);
228 /* temporal reference */
229 put_bits(&s->pb, 10, (s->fake_picture_number -
230 s->gop_picture_number) & 0x3ff);
231 s->fake_picture_number++;
233 put_bits(&s->pb, 3, s->pict_type);
234 put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */
236 if (s->pict_type == P_TYPE) {
237 put_bits(&s->pb, 1, 0); /* half pel coordinates */
238 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
241 put_bits(&s->pb, 1, 0); /* extra bit picture */
244 put_header(s, SLICE_MIN_START_CODE);
245 put_bits(&s->pb, 5, s->qscale); /* quantizer scale */
246 put_bits(&s->pb, 1, 0); /* slice extra information */
249 void mpeg1_encode_mb(MpegEncContext *s,
250 DCTELEM block[6][64],
251 int motion_x, int motion_y)
253 int mb_incr, i, cbp, mb_x, mb_y;
261 if (s->block_last_index[i] >= 0)
265 /* skip macroblock, except if first or last macroblock of a slice */
266 if ((cbp | motion_x | motion_y) == 0 &&
267 (!((mb_x | mb_y) == 0 ||
268 (mb_x == s->mb_width - 1 && mb_y == s->mb_height - 1)))) {
272 mb_incr = s->mb_incr;
274 while (mb_incr > 33) {
275 put_bits(&s->pb, 11, 0x008);
278 put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1],
279 mbAddrIncrTable[mb_incr - 1][0]);
281 if (s->pict_type == I_TYPE) {
282 put_bits(&s->pb, 1, 1); /* macroblock_type : macroblock_quant = 0 */
285 put_bits(&s->pb, 5, 0x03);
288 if (motion_x == 0 && motion_y == 0) {
289 put_bits(&s->pb, 2, 1); /* macroblock_pattern only */
290 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
292 put_bits(&s->pb, 1, 1); /* motion + cbp */
293 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0]);
294 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1]);
295 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
298 put_bits(&s->pb, 3, 1); /* motion only */
299 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0]);
300 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1]);
305 if (cbp & (1 << (5 - i))) {
306 mpeg1_encode_block(s, block[i], i);
311 s->last_mv[0][0][0] = motion_x;
312 s->last_mv[0][0][1] = motion_y;
315 static void mpeg1_encode_motion(MpegEncContext *s, int val)
317 int code, bit_size, l, m, bits, range, sign;
323 mbMotionVectorTable[0][1],
324 mbMotionVectorTable[0][0]);
326 bit_size = s->f_code - 1;
327 range = 1 << bit_size;
328 /* modulo encoding */
333 } else if (val >= l) {
339 code = (val >> bit_size) + 1;
340 bits = val & (range - 1);
345 code = (val >> bit_size) + 1;
346 bits = val & (range - 1);
350 mbMotionVectorTable[code][1],
351 mbMotionVectorTable[code][0]);
352 put_bits(&s->pb, 1, sign);
354 put_bits(&s->pb, bit_size, bits);
359 void mpeg1_encode_init(MpegEncContext *s)
367 for(f_code=1; f_code<=MAX_FCODE; f_code++){
368 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
371 if(mv==0) len= mbMotionVectorTable[0][1];
373 int val, bit_size, range, code;
375 bit_size = s->f_code - 1;
376 range = 1 << bit_size;
382 code = (val >> bit_size) + 1;
384 len= mbMotionVectorTable[code][1] + 1 + bit_size;
386 len= mbMotionVectorTable[16][1] + 2 + bit_size;
390 mv_penalty[f_code][mv+MAX_MV]= len;
395 for(f_code=MAX_FCODE; f_code>0; f_code--){
396 for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
397 fcode_tab[mv+MAX_MV]= f_code;
401 s->mv_penalty= mv_penalty;
403 s->fcode_tab= fcode_tab;
406 static inline void encode_dc(MpegEncContext *s, int diff, int component)
408 if (component == 0) {
411 mpeg1_lum_dc_uni[diff+255]&0xFF,
412 mpeg1_lum_dc_uni[diff+255]>>8);
416 mpeg1_chr_dc_uni[diff+255]&0xFF,
417 mpeg1_chr_dc_uni[diff+255]>>8);
421 static void mpeg1_encode_block(MpegEncContext *s,
425 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
427 // RLTable *rl = &rl_mpeg1;
429 last_index = s->block_last_index[n];
433 component = (n <= 3 ? 0 : n - 4 + 1);
434 dc = block[0]; /* overflow is impossible */
435 diff = dc - s->last_dc[component];
436 encode_dc(s, diff, component);
437 s->last_dc[component] = dc;
440 /* encode the first coefficient : needs to be done here because
441 it is handled slightly differently */
443 if (abs(level) == 1) {
444 code = ((UINT32)level >> 31); /* the sign bit */
445 put_bits(&s->pb, 2, code | 0x02);
454 /* now quantify & encode AC coefs */
455 last_non_zero = i - 1;
457 for(;i<=last_index;i++) {
458 j = zigzag_direct[i];
463 dprintf("level[%d]=%d\n", i, level);
465 /* encode using VLC */
467 run = i - last_non_zero - 1;
477 : "=&r" (alevel), "=&r" (sign)
488 // code = get_rl_index(rl, 0, run, alevel);
489 if (alevel > mpeg1_max_level[0][run])
492 code= mpeg1_index_run[0][run] + alevel - 1;
494 if (code < 111 /* rl->n */) {
495 /* store the vlc & sign at once */
496 put_bits(&s->pb, mpeg1_vlc[code][1]+1, (mpeg1_vlc[code][0]<<1) + sign);
498 /* escape seems to be pretty rare <5% so i dont optimize it */
499 put_bits(&s->pb, mpeg1_vlc[111/*rl->n*/][1], mpeg1_vlc[111/*rl->n*/][0]);
500 /* escape: only clip in this case */
501 put_bits(&s->pb, 6, run);
503 put_bits(&s->pb, 8, level & 0xff);
506 put_bits(&s->pb, 16, 0x8001 + level + 255);
508 put_bits(&s->pb, 16, level & 0xffff);
516 put_bits(&s->pb, 2, 0x2);
519 /******************************************/
522 static VLC dc_lum_vlc;
523 static VLC dc_chroma_vlc;
525 static VLC mbincr_vlc;
526 static VLC mb_ptype_vlc;
527 static VLC mb_btype_vlc;
528 static VLC mb_pat_vlc;
530 void mpeg1_init_vlc(MpegEncContext *s)
536 init_vlc(&dc_lum_vlc, 9, 12,
537 vlc_dc_lum_bits, 1, 1,
538 vlc_dc_lum_code, 2, 2);
539 init_vlc(&dc_chroma_vlc, 9, 12,
540 vlc_dc_chroma_bits, 1, 1,
541 vlc_dc_chroma_code, 2, 2);
542 init_vlc(&mv_vlc, 9, 17,
543 &mbMotionVectorTable[0][1], 2, 1,
544 &mbMotionVectorTable[0][0], 2, 1);
545 init_vlc(&mbincr_vlc, 9, 35,
546 &mbAddrIncrTable[0][1], 2, 1,
547 &mbAddrIncrTable[0][0], 2, 1);
548 init_vlc(&mb_pat_vlc, 9, 63,
549 &mbPatTable[0][1], 2, 1,
550 &mbPatTable[0][0], 2, 1);
552 init_vlc(&mb_ptype_vlc, 6, 32,
553 &table_mb_ptype[0][1], 2, 1,
554 &table_mb_ptype[0][0], 2, 1);
555 init_vlc(&mb_btype_vlc, 6, 32,
556 &table_mb_btype[0][1], 2, 1,
557 &table_mb_btype[0][0], 2, 1);
560 /* cannot use generic init because we must add the EOB code */
561 init_vlc(&rl_mpeg1.vlc, 9, rl_mpeg1.n + 2,
562 &rl_mpeg1.table_vlc[0][1], 4, 2,
563 &rl_mpeg1.table_vlc[0][0], 4, 2);
564 init_vlc(&rl_mpeg2.vlc, 9, rl_mpeg2.n + 2,
565 &rl_mpeg2.table_vlc[0][1], 4, 2,
566 &rl_mpeg2.table_vlc[0][0], 4, 2);
570 static inline int get_dmv(MpegEncContext *s)
572 if(get_bits1(&s->gb))
573 return 1 - (get_bits1(&s->gb) << 1);
578 static inline int get_qscale(MpegEncContext *s)
582 if (s->q_scale_type) {
583 qscale = non_linear_qscale[get_bits(&s->gb, 5)];
585 qscale = get_bits(&s->gb, 5) << 1;
588 /* for mpeg1, we use the generic unquant code */
589 qscale = get_bits(&s->gb, 5);
594 /* motion type (for mpeg2) */
600 static int mpeg_decode_mb(MpegEncContext *s,
601 DCTELEM block[6][64])
603 int i, j, k, cbp, val, code, mb_type, motion_type;
605 /* skip mb handling */
606 if (s->mb_incr == 0) {
607 /* read again increment */
610 code = get_vlc(&s->gb, &mbincr_vlc);
612 return 1; /* error = end of slice */
617 /* otherwise, stuffing, nothing to do */
624 if (++s->mb_x >= s->mb_width) {
626 if (s->mb_y >= (s->mb_height - 1))
630 dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
632 if (--s->mb_incr != 0) {
636 s->block_last_index[i] = -1;
637 s->mv_type = MV_TYPE_16X16;
638 if (s->pict_type == P_TYPE) {
639 /* if P type, zero motion vector is implied */
640 s->mv_dir = MV_DIR_FORWARD;
641 s->mv[0][0][0] = s->mv[0][0][1] = 0;
642 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
643 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
645 /* if B type, reuse previous vectors and directions */
646 s->mv[0][0][0] = s->last_mv[0][0][0];
647 s->mv[0][0][1] = s->last_mv[0][0][1];
648 s->mv[1][0][0] = s->last_mv[1][0][0];
649 s->mv[1][0][1] = s->last_mv[1][0][1];
655 switch(s->pict_type) {
658 if (get_bits1(&s->gb) == 0) {
659 if (get_bits1(&s->gb) == 0)
661 mb_type = MB_QUANT | MB_INTRA;
667 mb_type = get_vlc(&s->gb, &mb_ptype_vlc);
672 mb_type = get_vlc(&s->gb, &mb_btype_vlc);
677 dprintf("mb_type=%x\n", mb_type);
678 motion_type = 0; /* avoid warning */
679 if (mb_type & (MB_FOR|MB_BACK)) {
680 /* get additionnal motion vector type */
681 if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct)
682 motion_type = MT_FRAME;
684 motion_type = get_bits(&s->gb, 2);
686 /* compute dct type */
687 if (s->picture_structure == PICT_FRAME &&
688 !s->frame_pred_frame_dct &&
689 (mb_type & (MB_PAT | MB_INTRA))) {
690 s->interlaced_dct = get_bits1(&s->gb);
692 if (s->interlaced_dct)
693 printf("interlaced_dct\n");
696 s->interlaced_dct = 0; /* frame based */
699 if (mb_type & MB_QUANT) {
700 s->qscale = get_qscale(s);
702 if (mb_type & MB_INTRA) {
703 if (s->concealment_motion_vectors) {
704 /* just parse them */
705 if (s->picture_structure != PICT_FRAME)
706 skip_bits1(&s->gb); /* field select */
707 mpeg_decode_motion(s, s->mpeg_f_code[0][0], 0);
708 mpeg_decode_motion(s, s->mpeg_f_code[0][1], 0);
712 memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
717 /* special case of implicit zero motion vector */
718 if (s->pict_type == P_TYPE && !(mb_type & MB_FOR)) {
719 s->mv_dir = MV_DIR_FORWARD;
720 s->mv_type = MV_TYPE_16X16;
721 s->last_mv[0][0][0] = 0;
722 s->last_mv[0][0][1] = 0;
723 s->last_mv[0][1][0] = 0;
724 s->last_mv[0][1][1] = 0;
727 } else if (mb_type & (MB_FOR | MB_BACK)) {
731 if (mb_type & (MB_FOR >> i)) {
732 s->mv_dir |= (MV_DIR_FORWARD >> i);
733 dprintf("motion_type=%d\n", motion_type);
734 switch(motion_type) {
735 case MT_FRAME: /* or MT_16X8 */
736 if (s->picture_structure == PICT_FRAME) {
738 s->mv_type = MV_TYPE_16X16;
740 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
741 s->last_mv[i][0][k]);
742 s->last_mv[i][0][k] = val;
743 s->last_mv[i][1][k] = val;
744 /* full_pel: only for mpeg1 */
747 s->mv[i][0][k] = val;
748 dprintf("mv%d: %d\n", k, val);
752 s->mv_type = MV_TYPE_16X8;
754 s->field_select[i][j] = get_bits1(&s->gb);
756 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
757 s->last_mv[i][j][k]);
758 s->last_mv[i][j][k] = val;
759 s->mv[i][j][k] = val;
765 if (s->picture_structure == PICT_FRAME) {
766 s->mv_type = MV_TYPE_FIELD;
768 s->field_select[i][j] = get_bits1(&s->gb);
769 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
770 s->last_mv[i][j][0]);
771 s->last_mv[i][j][0] = val;
772 s->mv[i][j][0] = val;
773 dprintf("fmx=%d\n", val);
774 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
775 s->last_mv[i][j][1] >> 1);
776 s->last_mv[i][j][1] = val << 1;
777 s->mv[i][j][1] = val;
778 dprintf("fmy=%d\n", val);
781 s->mv_type = MV_TYPE_16X16;
782 s->field_select[i][0] = get_bits1(&s->gb);
784 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
785 s->last_mv[i][0][k]);
786 s->last_mv[i][0][k] = val;
787 s->last_mv[i][1][k] = val;
788 s->mv[i][0][k] = val;
794 int dmx, dmy, mx, my, m;
796 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
797 s->last_mv[i][0][0]);
798 s->last_mv[i][0][0] = mx;
799 s->last_mv[i][1][0] = mx;
801 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
802 s->last_mv[i][0][1] >> 1);
804 s->mv_type = MV_TYPE_DMV;
805 /* XXX: totally broken */
806 if (s->picture_structure == PICT_FRAME) {
807 s->last_mv[i][0][1] = my << 1;
808 s->last_mv[i][1][1] = my << 1;
810 m = s->top_field_first ? 1 : 3;
811 /* top -> top pred */
813 s->mv[i][0][1] = my << 1;
814 s->mv[i][1][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
815 s->mv[i][1][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
818 s->mv[i][2][1] = my << 1;
819 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
820 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
822 s->last_mv[i][0][1] = my;
823 s->last_mv[i][1][1] = my;
826 s->mv[i][1][0] = ((mx + (mx > 0)) >> 1) + dmx;
827 s->mv[i][1][1] = ((my + (my > 0)) >> 1) + dmy - 1
828 /* + 2 * cur_field */;
837 if ((mb_type & MB_INTRA) && s->concealment_motion_vectors) {
838 skip_bits1(&s->gb); /* marker */
841 if (mb_type & MB_PAT) {
842 cbp = get_vlc(&s->gb, &mb_pat_vlc);
847 dprintf("cbp=%x\n", cbp);
852 if (cbp & (1 << (5 - i))) {
853 if (mpeg2_decode_block_intra(s, block[i], i) < 0)
859 if (cbp & (1 << (5 - i))) {
860 if (mpeg2_decode_block_non_intra(s, block[i], i) < 0)
867 if (cbp & (1 << (5 - i))) {
868 if (mpeg1_decode_block(s, block[i], i) < 0)
876 /* as h263, but only 17 codes */
877 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
879 int code, sign, val, m, l, shift;
881 code = get_vlc(&s->gb, &mv_vlc);
888 sign = get_bits1(&s->gb);
890 val = (code - 1) << shift;
892 val |= get_bits(&s->gb, shift);
898 /* modulo decoding */
899 l = (1 << shift) * 16;
903 } else if (val >= l) {
909 static inline int decode_dc(MpegEncContext *s, int component)
913 if (component == 0) {
914 code = get_vlc(&s->gb, &dc_lum_vlc);
916 code = get_vlc(&s->gb, &dc_chroma_vlc);
923 diff = get_bits(&s->gb, code);
924 if ((diff & (1 << (code - 1))) == 0)
925 diff = (-1 << code) | (diff + 1);
930 static int mpeg1_decode_block(MpegEncContext *s,
934 int level, dc, diff, i, j, run;
936 RLTable *rl = &rl_mpeg1;
940 component = (n <= 3 ? 0 : n - 4 + 1);
941 diff = decode_dc(s, component);
944 dc = s->last_dc[component];
946 s->last_dc[component] = dc;
948 dprintf("dc=%d diff=%d\n", dc, diff);
955 /* special case for the first coef. no need to add a second vlc table */
957 SHOW_BITS(&s->gb, v, 2);
960 level = 1 - ((v & 1) << 1);
962 RESTORE_BITS(&s->gb);
965 RESTORE_BITS(&s->gb);
968 /* now quantify & encode AC coefs */
970 code = get_vlc(&s->gb, &rl->vlc);
976 } else if (code == 111) {
978 run = get_bits(&s->gb, 6);
979 level = get_bits(&s->gb, 8);
980 level = (level << 24) >> 24;
982 level = get_bits(&s->gb, 8) - 256;
983 } else if (level == 0) {
984 level = get_bits(&s->gb, 8);
987 run = rl->table_run[code];
988 level = rl->table_level[code];
989 if (get_bits1(&s->gb))
996 dprintf("%d: run=%d level=%d\n", n, run, level);
997 j = zigzag_direct[i];
1001 s->block_last_index[n] = i-1;
1005 /* Also does unquantization here, since I will never support mpeg2
1007 static int mpeg2_decode_block_non_intra(MpegEncContext *s,
1011 int level, i, j, run;
1013 RLTable *rl = &rl_mpeg1;
1014 const UINT8 *scan_table;
1015 const UINT16 *matrix;
1018 if (s->alternate_scan)
1019 scan_table = ff_alternate_vertical_scan;
1021 scan_table = zigzag_direct;
1030 matrix = s->non_intra_matrix;
1032 matrix = s->chroma_non_intra_matrix;
1034 /* special case for the first coef. no need to add a second vlc table */
1036 SHOW_BITS(&s->gb, v, 2);
1039 level = 1 - ((v & 1) << 1);
1041 RESTORE_BITS(&s->gb);
1044 RESTORE_BITS(&s->gb);
1047 /* now quantify & encode AC coefs */
1049 code = get_vlc(&s->gb, &rl->vlc);
1054 } else if (code == 111) {
1056 run = get_bits(&s->gb, 6);
1057 level = get_bits(&s->gb, 12);
1058 level = (level << 20) >> 20;
1060 run = rl->table_run[code];
1061 level = rl->table_level[code];
1062 if (get_bits1(&s->gb))
1070 dprintf("%d: run=%d level=%d\n", n, run, level);
1073 level = ((level * 2 + 1) * s->qscale * matrix[j]) >> 5;
1075 level = ((-level * 2 + 1) * s->qscale * matrix[j]) >> 5;
1078 /* XXX: is it really necessary to saturate since the encoder
1079 knows whats going on ? */
1084 block[63] ^= (mismatch & 1);
1085 s->block_last_index[n] = i;
1089 static int mpeg2_decode_block_intra(MpegEncContext *s,
1093 int level, dc, diff, i, j, run;
1094 int code, component;
1096 const UINT8 *scan_table;
1097 const UINT16 *matrix;
1100 if (s->alternate_scan)
1101 scan_table = ff_alternate_vertical_scan;
1103 scan_table = zigzag_direct;
1106 component = (n <= 3 ? 0 : n - 4 + 1);
1107 diff = decode_dc(s, component);
1110 dc = s->last_dc[component];
1112 s->last_dc[component] = dc;
1113 block[0] = dc << (3 - s->intra_dc_precision);
1114 dprintf("dc=%d\n", block[0]);
1115 mismatch = block[0] ^ 1;
1117 if (s->intra_vlc_format)
1122 matrix = s->intra_matrix;
1124 matrix = s->chroma_intra_matrix;
1126 /* now quantify & encode AC coefs */
1128 code = get_vlc(&s->gb, &rl->vlc);
1133 } else if (code == 111) {
1135 run = get_bits(&s->gb, 6);
1136 level = get_bits(&s->gb, 12);
1137 level = (level << 20) >> 20;
1139 run = rl->table_run[code];
1140 level = rl->table_level[code];
1141 if (get_bits1(&s->gb))
1148 dprintf("%d: run=%d level=%d\n", n, run, level);
1149 level = (level * s->qscale * matrix[j]) / 16;
1150 /* XXX: is it really necessary to saturate since the encoder
1151 knows whats going on ? */
1156 block[63] ^= (mismatch & 1);
1157 s->block_last_index[n] = i;
1161 /* compressed picture size */
1162 #define PICTURE_BUFFER_SIZE 100000
1164 typedef struct Mpeg1Context {
1165 MpegEncContext mpeg_enc_ctx;
1166 UINT32 header_state;
1167 int start_code; /* current start code */
1168 UINT8 buffer[PICTURE_BUFFER_SIZE];
1171 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
1172 int repeat_field; /* true if we must repeat the field */
1175 static int mpeg_decode_init(AVCodecContext *avctx)
1177 Mpeg1Context *s = avctx->priv_data;
1179 s->header_state = 0xff;
1180 s->mpeg_enc_ctx_allocated = 0;
1181 s->buffer_size = PICTURE_BUFFER_SIZE;
1183 s->buf_ptr = s->buffer;
1184 s->mpeg_enc_ctx.picture_number = 0;
1185 s->repeat_field = 0;
1189 /* return the 8 bit start code value and update the search
1190 state. Return -1 if no start code found */
1191 static int find_start_code(UINT8 **pbuf_ptr, UINT8 *buf_end,
1192 UINT32 *header_state)
1195 unsigned int state, v;
1198 state = *header_state;
1199 buf_ptr = *pbuf_ptr;
1200 while (buf_ptr < buf_end) {
1202 if (state == 0x000001) {
1203 state = ((state << 8) | v) & 0xffffff;
1207 state = ((state << 8) | v) & 0xffffff;
1211 *pbuf_ptr = buf_ptr;
1212 *header_state = state;
1216 static int mpeg1_decode_picture(AVCodecContext *avctx,
1217 UINT8 *buf, int buf_size)
1219 Mpeg1Context *s1 = avctx->priv_data;
1220 MpegEncContext *s = &s1->mpeg_enc_ctx;
1223 init_get_bits(&s->gb, buf, buf_size);
1225 ref = get_bits(&s->gb, 10); /* temporal ref */
1226 s->pict_type = get_bits(&s->gb, 3);
1227 dprintf("pict_type=%d number=%d\n", s->pict_type, s->picture_number);
1228 skip_bits(&s->gb, 16);
1229 if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
1230 s->full_pel[0] = get_bits1(&s->gb);
1231 f_code = get_bits(&s->gb, 3);
1234 s->mpeg_f_code[0][0] = f_code;
1235 s->mpeg_f_code[0][1] = f_code;
1237 if (s->pict_type == B_TYPE) {
1238 s->full_pel[1] = get_bits1(&s->gb);
1239 f_code = get_bits(&s->gb, 3);
1242 s->mpeg_f_code[1][0] = f_code;
1243 s->mpeg_f_code[1][1] = f_code;
1251 static void mpeg_decode_sequence_extension(MpegEncContext *s)
1253 int horiz_size_ext, vert_size_ext;
1254 int bit_rate_ext, vbv_buf_ext, low_delay;
1255 int frame_rate_ext_n, frame_rate_ext_d;
1257 skip_bits(&s->gb, 8); /* profil and level */
1258 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1259 skip_bits(&s->gb, 2); /* chroma_format */
1260 horiz_size_ext = get_bits(&s->gb, 2);
1261 vert_size_ext = get_bits(&s->gb, 2);
1262 s->width |= (horiz_size_ext << 12);
1263 s->height |= (vert_size_ext << 12);
1264 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1265 s->bit_rate = ((s->bit_rate / 400) | (bit_rate_ext << 12)) * 400;
1266 skip_bits1(&s->gb); /* marker */
1267 vbv_buf_ext = get_bits(&s->gb, 8);
1268 low_delay = get_bits1(&s->gb);
1269 frame_rate_ext_n = get_bits(&s->gb, 2);
1270 frame_rate_ext_d = get_bits(&s->gb, 5);
1271 if (frame_rate_ext_d >= 1)
1272 s->frame_rate = (s->frame_rate * frame_rate_ext_n) / frame_rate_ext_d;
1273 dprintf("sequence extension\n");
1277 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1281 dprintf("matrix extension\n");
1283 if (get_bits1(&s->gb)) {
1285 v = get_bits(&s->gb, 8);
1286 j = zigzag_direct[i];
1287 s->intra_matrix[j] = v;
1288 s->chroma_intra_matrix[j] = v;
1291 if (get_bits1(&s->gb)) {
1293 v = get_bits(&s->gb, 8);
1294 j = zigzag_direct[i];
1295 s->non_intra_matrix[j] = v;
1296 s->chroma_non_intra_matrix[j] = v;
1299 if (get_bits1(&s->gb)) {
1301 v = get_bits(&s->gb, 8);
1302 j = zigzag_direct[i];
1303 s->chroma_intra_matrix[j] = v;
1306 if (get_bits1(&s->gb)) {
1308 v = get_bits(&s->gb, 8);
1309 j = zigzag_direct[i];
1310 s->chroma_non_intra_matrix[j] = v;
1315 static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
1317 s->full_pel[0] = s->full_pel[1] = 0;
1318 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1319 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1320 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1321 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1322 s->intra_dc_precision = get_bits(&s->gb, 2);
1323 s->picture_structure = get_bits(&s->gb, 2);
1324 s->top_field_first = get_bits1(&s->gb);
1325 s->frame_pred_frame_dct = get_bits1(&s->gb);
1326 s->concealment_motion_vectors = get_bits1(&s->gb);
1327 s->q_scale_type = get_bits1(&s->gb);
1328 s->intra_vlc_format = get_bits1(&s->gb);
1329 s->alternate_scan = get_bits1(&s->gb);
1330 s->repeat_first_field = get_bits1(&s->gb);
1331 s->chroma_420_type = get_bits1(&s->gb);
1332 s->progressive_frame = get_bits1(&s->gb);
1333 /* composite display not parsed */
1334 dprintf("intra_dc_precision=%d\n", s->intra_dc_precision);
1335 dprintf("picture_structure=%d\n", s->picture_structure);
1336 dprintf("conceal=%d\n", s->concealment_motion_vectors);
1337 dprintf("intra_vlc_format=%d\n", s->intra_vlc_format);
1338 dprintf("alternate_scan=%d\n", s->alternate_scan);
1339 dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1340 dprintf("progressive_frame=%d\n", s->progressive_frame);
1343 static void mpeg_decode_extension(AVCodecContext *avctx,
1344 UINT8 *buf, int buf_size)
1346 Mpeg1Context *s1 = avctx->priv_data;
1347 MpegEncContext *s = &s1->mpeg_enc_ctx;
1350 init_get_bits(&s->gb, buf, buf_size);
1352 ext_type = get_bits(&s->gb, 4);
1356 mpeg_decode_sequence_extension(s);
1359 /* quant matrix extension */
1360 mpeg_decode_quant_matrix_extension(s);
1363 /* picture extension */
1364 mpeg_decode_picture_coding_extension(s);
1369 /* return 1 if end of frame */
1370 static int mpeg_decode_slice(AVCodecContext *avctx,
1373 UINT8 *buf, int buf_size)
1375 Mpeg1Context *s1 = avctx->priv_data;
1376 MpegEncContext *s = &s1->mpeg_enc_ctx;
1379 start_code = (start_code - 1) & 0xff;
1380 if (start_code >= s->mb_height)
1382 s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
1383 s->last_dc[1] = s->last_dc[0];
1384 s->last_dc[2] = s->last_dc[0];
1385 memset(s->last_mv, 0, sizeof(s->last_mv));
1387 s->mb_y = start_code;
1390 /* start frame decoding */
1391 if (s->first_slice) {
1396 init_get_bits(&s->gb, buf, buf_size);
1398 s->qscale = get_qscale(s);
1399 /* extra slice info */
1400 while (get_bits1(&s->gb) != 0) {
1401 skip_bits(&s->gb, 8);
1405 clear_blocks(s->block[0]);
1406 ret = mpeg_decode_mb(s, s->block);
1407 dprintf("ret=%d\n", ret);
1412 MPV_decode_mb(s, s->block);
1415 /* end of slice reached */
1416 if (s->mb_x == (s->mb_width - 1) &&
1417 s->mb_y == (s->mb_height - 1)) {
1423 /* XXX: incorrect reported qscale for mpeg2 */
1424 if (s->pict_type == B_TYPE) {
1425 picture = s->current_picture;
1426 avctx->quality = s->qscale;
1428 /* latency of 1 frame for I and P frames */
1429 /* XXX: use another variable than picture_number */
1430 if (s->picture_number == 0) {
1433 picture = s->last_picture;
1434 avctx->quality = s->last_qscale;
1436 s->last_qscale = s->qscale;
1437 s->picture_number++;
1440 pict->data[0] = picture[0];
1441 pict->data[1] = picture[1];
1442 pict->data[2] = picture[2];
1443 pict->linesize[0] = s->linesize;
1444 pict->linesize[1] = s->linesize / 2;
1445 pict->linesize[2] = s->linesize / 2;
1455 static int mpeg1_decode_sequence(AVCodecContext *avctx,
1456 UINT8 *buf, int buf_size)
1458 Mpeg1Context *s1 = avctx->priv_data;
1459 MpegEncContext *s = &s1->mpeg_enc_ctx;
1460 int width, height, i, v, j;
1462 init_get_bits(&s->gb, buf, buf_size);
1464 width = get_bits(&s->gb, 12);
1465 height = get_bits(&s->gb, 12);
1466 skip_bits(&s->gb, 4);
1467 s->frame_rate_index = get_bits(&s->gb, 4);
1468 if (s->frame_rate_index == 0)
1470 s->bit_rate = get_bits(&s->gb, 18) * 400;
1471 if (get_bits1(&s->gb) == 0) /* marker */
1473 if (width <= 0 || height <= 0 ||
1474 (width % 2) != 0 || (height % 2) != 0)
1476 if (width != s->width ||
1477 height != s->height) {
1478 /* start new mpeg1 context decoding */
1479 s->out_format = FMT_MPEG1;
1480 if (s1->mpeg_enc_ctx_allocated) {
1485 s->has_b_frames = 1;
1487 avctx->width = width;
1488 avctx->height = height;
1489 avctx->frame_rate = frame_rate_tab[s->frame_rate_index];
1490 s->frame_rate = avctx->frame_rate;
1491 avctx->bit_rate = s->bit_rate;
1493 if (MPV_common_init(s) < 0)
1496 s1->mpeg_enc_ctx_allocated = 1;
1499 skip_bits(&s->gb, 10); /* vbv_buffer_size */
1500 skip_bits(&s->gb, 1);
1503 if (get_bits1(&s->gb)) {
1505 v = get_bits(&s->gb, 8);
1506 j = zigzag_direct[i];
1507 s->intra_matrix[j] = v;
1508 s->chroma_intra_matrix[j] = v;
1511 dprintf("intra matrix present\n");
1513 dprintf(" %d", s->intra_matrix[zigzag_direct[i]]);
1518 v = default_intra_matrix[i];
1519 s->intra_matrix[i] = v;
1520 s->chroma_intra_matrix[i] = v;
1523 if (get_bits1(&s->gb)) {
1525 v = get_bits(&s->gb, 8);
1526 j = zigzag_direct[i];
1527 s->non_intra_matrix[j] = v;
1528 s->chroma_non_intra_matrix[j] = v;
1531 dprintf("non intra matrix present\n");
1533 dprintf(" %d", s->non_intra_matrix[zigzag_direct[i]]);
1538 v = default_non_intra_matrix[i];
1539 s->non_intra_matrix[i] = v;
1540 s->chroma_non_intra_matrix[i] = v;
1544 /* we set mpeg2 parameters so that it emulates mpeg1 */
1545 s->progressive_sequence = 1;
1546 s->progressive_frame = 1;
1547 s->picture_structure = PICT_FRAME;
1548 s->frame_pred_frame_dct = 1;
1553 /* handle buffering and image synchronisation */
1554 static int mpeg_decode_frame(AVCodecContext *avctx,
1555 void *data, int *data_size,
1556 UINT8 *buf, int buf_size)
1558 Mpeg1Context *s = avctx->priv_data;
1559 UINT8 *buf_end, *buf_ptr, *buf_start;
1560 int len, start_code_found, ret, code, start_code, input_size;
1561 AVPicture *picture = data;
1562 MpegEncContext *s2 = &s->mpeg_enc_ctx;
1564 dprintf("fill_buffer\n");
1568 /* special case for last picture */
1569 if (buf_size == 0) {
1570 if (s2->picture_number > 0) {
1571 picture->data[0] = s2->next_picture[0];
1572 picture->data[1] = s2->next_picture[1];
1573 picture->data[2] = s2->next_picture[2];
1574 picture->linesize[0] = s2->linesize;
1575 picture->linesize[1] = s2->linesize / 2;
1576 picture->linesize[2] = s2->linesize / 2;
1577 *data_size = sizeof(AVPicture);
1583 buf_end = buf + buf_size;
1585 if (s->repeat_field % 2 == 1) {
1587 //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
1588 // s2->picture_number, s->repeat_field);
1589 *data_size = sizeof(AVPicture);
1593 while (buf_ptr < buf_end) {
1594 buf_start = buf_ptr;
1595 /* find start next code */
1596 code = find_start_code(&buf_ptr, buf_end, &s->header_state);
1598 start_code_found = 1;
1600 start_code_found = 0;
1602 /* copy to buffer */
1603 len = buf_ptr - buf_start;
1604 if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
1605 /* data too big : flush */
1606 s->buf_ptr = s->buffer;
1607 if (start_code_found)
1608 s->start_code = code;
1610 memcpy(s->buf_ptr, buf_start, len);
1613 if (start_code_found) {
1614 /* prepare data for next start code */
1615 input_size = s->buf_ptr - s->buffer;
1616 start_code = s->start_code;
1617 s->buf_ptr = s->buffer;
1618 s->start_code = code;
1619 switch(start_code) {
1620 case SEQ_START_CODE:
1621 mpeg1_decode_sequence(avctx, s->buffer,
1625 case PICTURE_START_CODE:
1626 /* we have a complete image : we try to decompress it */
1627 mpeg1_decode_picture(avctx,
1628 s->buffer, input_size);
1630 case EXT_START_CODE:
1631 mpeg_decode_extension(avctx,
1632 s->buffer, input_size);
1635 if (start_code >= SLICE_MIN_START_CODE &&
1636 start_code <= SLICE_MAX_START_CODE) {
1637 ret = mpeg_decode_slice(avctx, picture,
1638 start_code, s->buffer, input_size);
1640 /* got a picture: exit */
1641 /* first check if we must repeat the frame */
1642 if (s2->progressive_frame && s2->repeat_first_field) {
1643 //fprintf(stderr,"\nRepeat this frame: %d! pict: %d",avctx->frame_number,s2->picture_number);
1644 s2->repeat_first_field = 0;
1645 s2->progressive_frame = 0;
1646 if (++s->repeat_field > 2)
1647 s->repeat_field = 0;
1649 *data_size = sizeof(AVPicture);
1659 return buf_ptr - buf;
1662 static int mpeg_decode_end(AVCodecContext *avctx)
1664 Mpeg1Context *s = avctx->priv_data;
1666 if (s->mpeg_enc_ctx_allocated)
1667 MPV_common_end(&s->mpeg_enc_ctx);
1671 AVCodec mpeg_decoder = {
1674 CODEC_ID_MPEG1VIDEO,
1675 sizeof(Mpeg1Context),