2 * MSMPEG4 backend for ffmpeg encoder and decoder
3 * Copyright (c) 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.
23 #include "mpegvideo.h"
26 * You can also call this codec : MPEG4 with a twist !
29 * - (encoding) select best mv table (two choices)
30 * - (encoding) select best vlc/dc table
31 * - (decoding) handle slice indication
35 /* motion vector table */
36 typedef struct MVTable {
38 const UINT16 *table_mv_code;
39 const UINT8 *table_mv_bits;
40 const UINT8 *table_mvx;
41 const UINT8 *table_mvy;
42 UINT16 *table_mv_index; /* encoding: convert mv to index in table_mv */
43 VLC vlc; /* decoding: vlc */
46 static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n);
47 static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
49 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
50 static int msmpeg4_decode_motion(MpegEncContext * s,
51 int *mx_ptr, int *my_ptr);
57 /* XXX: move it to mpegvideo.h */
59 static int init_done = 0;
61 #include "msmpeg4data.h"
65 const char *st_names[ST_NB] = {
75 int st_current_index = 0;
76 unsigned int st_bit_counts[ST_NB];
77 unsigned int st_out_bit_counts[ST_NB];
79 #define set_stat(var) st_current_index = var;
81 void print_stats(void)
89 total += st_bit_counts[i];
92 for(i=0;i<ST_NB;i++) {
93 printf("%-10s : %10.1f %5.1f%%\n",
95 (double)st_bit_counts[i] / 8.0,
96 (double)st_bit_counts[i] * 100.0 / total);
98 printf("%-10s : %10.1f %5.1f%%\n",
106 total += st_out_bit_counts[i];
109 for(i=0;i<ST_NB;i++) {
110 printf("%-10s : %10.1f %5.1f%%\n",
112 (double)st_out_bit_counts[i] / 8.0,
113 (double)st_out_bit_counts[i] * 100.0 / total);
115 printf("%-10s : %10.1f %5.1f%%\n",
123 #define set_stat(var)
127 /* build the table which associate a (x,y) motion vector to a vlc */
128 static void init_mv_table(MVTable *tab)
132 tab->table_mv_index = malloc(sizeof(UINT16) * 4096);
133 /* mark all entries as not used */
135 tab->table_mv_index[i] = tab->n;
137 for(i=0;i<tab->n;i++) {
138 x = tab->table_mvx[i];
139 y = tab->table_mvy[i];
140 tab->table_mv_index[(x << 6) | y] = i;
144 static void code012(PutBitContext *pb, int n)
150 put_bits(pb, 1, (n >= 2));
154 /* write MSMPEG4 V3 compatible frame header */
155 void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
159 align_put_bits(&s->pb);
161 put_bits(&s->pb, 2, s->pict_type - 1);
163 put_bits(&s->pb, 5, s->qscale);
165 s->rl_table_index = 2;
166 s->rl_chroma_table_index = 1; /* only for I frame */
167 s->dc_table_index = 1;
168 s->mv_table_index = 1; /* only if P frame */
169 s->use_skip_mb_code = 1; /* only if P frame */
171 if (s->pict_type == I_TYPE) {
172 put_bits(&s->pb, 5, 0x17); /* indicate only one "slice" */
174 code012(&s->pb, s->rl_chroma_table_index);
175 code012(&s->pb, s->rl_table_index);
177 put_bits(&s->pb, 1, s->dc_table_index);
180 put_bits(&s->pb, 1, s->use_skip_mb_code);
182 s->rl_chroma_table_index = s->rl_table_index;
183 code012(&s->pb, s->rl_table_index);
185 put_bits(&s->pb, 1, s->dc_table_index);
187 put_bits(&s->pb, 1, s->mv_table_index);
192 /* init various encoding tables */
194 init_mv_table(&mv_tables[0]);
195 init_mv_table(&mv_tables[1]);
196 for(i=0;i<NB_RL_TABLES;i++)
197 init_rl(&rl_table[i]);
202 printf("*****frame %d:\n", frame_count++);
206 /* predict coded block */
207 static inline int coded_block_pred(MpegEncContext * s, int n, UINT8 **coded_block_ptr)
209 int x, y, wrap, pred, a, b, c;
211 x = 2 * s->mb_x + 1 + (n & 1);
212 y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
213 wrap = s->mb_width * 2 + 2;
218 a = s->coded_block[(x - 1) + (y) * wrap];
219 b = s->coded_block[(x - 1) + (y - 1) * wrap];
220 c = s->coded_block[(x) + (y - 1) * wrap];
229 *coded_block_ptr = &s->coded_block[(x) + (y) * wrap];
234 static void msmpeg4_encode_motion(MpegEncContext * s,
240 /* modulo encoding */
241 /* WARNING : you cannot reach all the MVs even with the modulo
242 encoding. This is a somewhat strange compromise they took !!! */
255 if ((unsigned)mx >= 64 ||
257 fprintf(stderr, "error mx=%d my=%d\n", mx, my);
259 mv = &mv_tables[s->mv_table_index];
261 code = mv->table_mv_index[(mx << 6) | my];
264 mv->table_mv_bits[code],
265 mv->table_mv_code[code]);
267 /* escape : code litterally */
268 put_bits(&s->pb, 6, mx);
269 put_bits(&s->pb, 6, my);
273 void msmpeg4_encode_mb(MpegEncContext * s,
274 DCTELEM block[6][64],
275 int motion_x, int motion_y)
277 int cbp, coded_cbp, i;
283 set_stat(ST_INTER_MB);
285 for (i = 0; i < 6; i++) {
286 if (s->block_last_index[i] >= 0)
289 if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
290 /* skip macroblock */
291 put_bits(&s->pb, 1, 1);
294 if (s->use_skip_mb_code)
295 put_bits(&s->pb, 1, 0); /* mb coded */
298 table_mb_non_intra[cbp + 64][1],
299 table_mb_non_intra[cbp + 64][0]);
302 h263_pred_motion(s, 0, &pred_x, &pred_y);
303 msmpeg4_encode_motion(s, motion_x - pred_x,
309 for (i = 0; i < 6; i++) {
311 val = (s->block_last_index[i] >= 1);
312 cbp |= val << (5 - i);
314 /* predict value for close blocks only for luma */
315 pred = coded_block_pred(s, i, &coded_block);
319 coded_cbp |= val << (5 - i);
323 printf("cbp=%x %x\n", cbp, coded_cbp);
326 if (s->pict_type == I_TYPE) {
327 set_stat(ST_INTRA_MB);
329 table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]);
331 if (s->use_skip_mb_code)
332 put_bits(&s->pb, 1, 0); /* mb coded */
334 table_mb_non_intra[cbp][1],
335 table_mb_non_intra[cbp][0]);
337 set_stat(ST_INTRA_MB);
338 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
341 for (i = 0; i < 6; i++) {
342 msmpeg4_encode_block(s, block[i], i);
347 /* strongly inspirated from MPEG4, but not exactly the same ! */
348 void msmpeg4_dc_scale(MpegEncContext * s)
354 else if (s->qscale < 9)
355 scale = 2 * s->qscale;
357 scale = s->qscale + 8;
358 s->y_dc_scale = scale;
359 s->c_dc_scale = (s->qscale + 13) / 2;
362 /* dir = 0: left, dir = 1: top prediction */
363 static int msmpeg4_pred_dc(MpegEncContext * s, int n,
364 UINT16 **dc_val_ptr, int *dir_ptr)
366 int a, b, c, x, y, wrap, pred, scale;
369 /* find prediction */
371 x = 2 * s->mb_x + 1 + (n & 1);
372 y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
373 wrap = s->mb_width * 2 + 2;
374 dc_val = s->dc_val[0];
375 scale = s->y_dc_scale;
379 wrap = s->mb_width + 2;
380 dc_val = s->dc_val[n - 4 + 1];
381 scale = s->c_dc_scale;
387 a = dc_val[(x - 1) + (y) * wrap];
388 b = dc_val[(x - 1) + (y - 1) * wrap];
389 c = dc_val[(x) + (y - 1) * wrap];
391 /* XXX: the following solution consumes divisions, but it does not
392 necessitate to modify mpegvideo.c. The problem comes from the
393 fact they decided to store the quantized DC (which would lead
394 to problems if Q could vary !) */
395 a = (a + (scale >> 1)) / scale;
396 b = (b + (scale >> 1)) / scale;
397 c = (c + (scale >> 1)) / scale;
399 /* XXX: WARNING: they did not choose the same test as MPEG4. This
400 is very important ! */
401 if (abs(a - b) <= abs(b - c)) {
409 /* update predictor */
410 *dc_val_ptr = &dc_val[(x) + (y) * wrap];
416 static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
422 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
424 /* update predictor */
426 *dc_val = level * s->y_dc_scale;
428 *dc_val = level * s->c_dc_scale;
431 /* do the prediction */
444 if (s->dc_table_index == 0) {
446 put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]);
448 put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]);
452 put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]);
454 put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]);
459 put_bits(&s->pb, 8, level);
462 put_bits(&s->pb, 1, sign);
466 /* Encoding of a block. Very similar to MPEG4 except for a different
467 escape coding (same as H263) and more vlc tables.
469 static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
471 int level, run, last, i, j, last_index;
472 int last_non_zero, sign, slevel;
473 int code, run_diff, dc_pred_dir;
478 msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
481 rl = &rl_table[s->rl_table_index];
483 rl = &rl_table[3 + s->rl_chroma_table_index];
486 set_stat(ST_INTRA_AC);
489 rl = &rl_table[3 + s->rl_table_index];
491 set_stat(ST_INTER_AC);
495 last_index = s->block_last_index[n];
496 last_non_zero = i - 1;
497 for (; i <= last_index; i++) {
498 j = zigzag_direct[i];
501 run = i - last_non_zero - 1;
502 last = (i == last_index);
509 code = get_rl_index(rl, last, run, level);
510 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
514 level1 = level - rl->max_level[last][run];
517 code = get_rl_index(rl, last, run, level1);
520 put_bits(&s->pb, 1, 0);
521 if (level > MAX_LEVEL)
523 run1 = run - rl->max_run[last][level] - run_diff;
526 code = get_rl_index(rl, last, run1, level);
530 put_bits(&s->pb, 1, 0);
531 put_bits(&s->pb, 1, last);
532 put_bits(&s->pb, 6, run);
533 put_bits(&s->pb, 8, slevel & 0xff);
536 put_bits(&s->pb, 1, 1);
537 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
538 put_bits(&s->pb, 1, sign);
542 put_bits(&s->pb, 1, 1);
543 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
544 put_bits(&s->pb, 1, sign);
547 put_bits(&s->pb, 1, sign);
554 /****************************************/
557 static VLC mb_non_intra_vlc;
558 static VLC mb_intra_vlc;
559 static VLC dc_lum_vlc[2];
560 static VLC dc_chroma_vlc[2];
562 /* init all vlc decoding tables */
563 int msmpeg4_decode_init_vlc(MpegEncContext *s)
568 for(i=0;i<NB_RL_TABLES;i++) {
569 init_rl(&rl_table[i]);
570 init_vlc_rl(&rl_table[i]);
574 init_vlc(&mv->vlc, 9, mv->n + 1,
575 mv->table_mv_bits, 1, 1,
576 mv->table_mv_code, 2, 2);
579 init_vlc(&dc_lum_vlc[0], 9, 120,
580 &table0_dc_lum[0][1], 8, 4,
581 &table0_dc_lum[0][0], 8, 4);
582 init_vlc(&dc_chroma_vlc[0], 9, 120,
583 &table0_dc_chroma[0][1], 8, 4,
584 &table0_dc_chroma[0][0], 8, 4);
585 init_vlc(&dc_lum_vlc[1], 9, 120,
586 &table1_dc_lum[0][1], 8, 4,
587 &table1_dc_lum[0][0], 8, 4);
588 init_vlc(&dc_chroma_vlc[1], 9, 120,
589 &table1_dc_chroma[0][1], 8, 4,
590 &table1_dc_chroma[0][0], 8, 4);
592 init_vlc(&mb_non_intra_vlc, 9, 128,
593 &table_mb_non_intra[0][1], 8, 4,
594 &table_mb_non_intra[0][0], 8, 4);
595 init_vlc(&mb_intra_vlc, 9, 128,
596 &table_mb_intra[0][1], 4, 2,
597 &table_mb_intra[0][0], 4, 2);
601 static int decode012(GetBitContext *gb)
608 return get_bits1(gb) + 1;
611 int msmpeg4_decode_picture_header(MpegEncContext * s)
615 s->pict_type = get_bits(&s->gb, 2) + 1;
616 if (s->pict_type != I_TYPE &&
617 s->pict_type != P_TYPE)
620 s->qscale = get_bits(&s->gb, 5);
622 if (s->pict_type == I_TYPE) {
623 code = get_bits(&s->gb, 5);
624 /* 0x17: one slice, 0x18: three slices */
625 /* XXX: implement it */
628 s->slice_height = s->mb_height / (code - 0x16);
629 s->rl_chroma_table_index = decode012(&s->gb);
630 s->rl_table_index = decode012(&s->gb);
632 s->dc_table_index = get_bits1(&s->gb);
635 s->use_skip_mb_code = get_bits1(&s->gb);
637 s->rl_table_index = decode012(&s->gb);
638 s->rl_chroma_table_index = s->rl_table_index;
640 s->dc_table_index = get_bits1(&s->gb);
642 s->mv_table_index = get_bits1(&s->gb);
646 printf("*****frame %d:\n", frame_count++);
651 void memsetw(short *tab, int val, int n)
658 int msmpeg4_decode_mb(MpegEncContext *s,
659 DCTELEM block[6][64])
665 /* special slice handling */
667 if ((s->mb_y % s->slice_height) == 0) {
669 /* reset DC pred (set previous line to 1024) */
670 wrap = 2 * s->mb_width + 2;
671 memsetw(&s->dc_val[0][(1) + (2 * s->mb_y) * wrap],
672 1024, 2 * s->mb_width);
673 wrap = s->mb_width + 2;
674 memsetw(&s->dc_val[1][(1) + (s->mb_y) * wrap],
676 memsetw(&s->dc_val[2][(1) + (s->mb_y) * wrap],
679 s->first_slice_line = 1;
681 s->first_slice_line = 0;
685 if (s->pict_type == P_TYPE) {
686 set_stat(ST_INTER_MB);
687 if (s->use_skip_mb_code) {
688 if (get_bits1(&s->gb)) {
692 s->block_last_index[i] = -1;
693 s->mv_dir = MV_DIR_FORWARD;
694 s->mv_type = MV_TYPE_16X16;
702 code = get_vlc(&s->gb, &mb_non_intra_vlc);
712 set_stat(ST_INTRA_MB);
714 code = get_vlc(&s->gb, &mb_intra_vlc);
717 /* predict coded block pattern */
720 val = ((code >> (5 - i)) & 1);
722 pred = coded_block_pred(s, i, &coded_val);
726 cbp |= val << (5 - i);
733 h263_pred_motion(s, 0, &mx, &my);
734 if (msmpeg4_decode_motion(s, &mx, &my) < 0)
736 s->mv_dir = MV_DIR_FORWARD;
737 s->mv_type = MV_TYPE_16X16;
741 set_stat(ST_INTRA_MB);
742 s->ac_pred = get_bits1(&s->gb);
745 for (i = 0; i < 6; i++) {
746 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
752 static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
755 int code, level, i, j, last, run, run_diff;
758 const UINT8 *scan_table;
763 level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
768 rl = &rl_table[s->rl_table_index];
770 rl = &rl_table[3 + s->rl_chroma_table_index];
778 if (dc_pred_dir == 0)
779 scan_table = ff_alternate_vertical_scan; /* left */
781 scan_table = ff_alternate_horizontal_scan; /* top */
783 scan_table = zigzag_direct;
785 set_stat(ST_INTRA_AC);
788 rl = &rl_table[3 + s->rl_table_index];
791 s->block_last_index[n] = i - 1;
794 scan_table = zigzag_direct;
795 set_stat(ST_INTER_AC);
799 code = get_vlc(&s->gb, &rl->vlc);
804 if (get_bits1(&s->gb) == 0) {
805 if (get_bits1(&s->gb) == 0) {
807 last = get_bits1(&s->gb);
808 run = get_bits(&s->gb, 6);
809 level = get_bits(&s->gb, 8);
810 level = (level << 24) >> 24; /* sign extend */
813 code = get_vlc(&s->gb, &rl->vlc);
814 if (code < 0 || code >= rl->n)
816 run = rl->table_run[code];
817 level = rl->table_level[code];
818 last = code >= rl->last;
819 run += rl->max_run[last][level] + run_diff;
820 if (get_bits1(&s->gb))
825 code = get_vlc(&s->gb, &rl->vlc);
826 if (code < 0 || code >= rl->n)
828 run = rl->table_run[code];
829 level = rl->table_level[code];
830 last = code >= rl->last;
831 level += rl->max_level[last][run];
832 if (get_bits1(&s->gb))
836 run = rl->table_run[code];
837 level = rl->table_level[code];
838 last = code >= rl->last;
839 if (get_bits1(&s->gb))
853 mpeg4_pred_ac(s, block, n, dc_pred_dir);
855 i = 64; /* XXX: not optimal */
858 s->block_last_index[n] = i - 1;
863 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
869 level = get_vlc(&s->gb, &dc_lum_vlc[s->dc_table_index]);
871 level = get_vlc(&s->gb, &dc_chroma_vlc[s->dc_table_index]);
876 if (level == DC_MAX) {
877 level = get_bits(&s->gb, 8);
878 if (get_bits1(&s->gb))
880 } else if (level != 0) {
881 if (get_bits1(&s->gb))
885 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
888 /* update predictor */
890 *dc_val = level * s->y_dc_scale;
892 *dc_val = level * s->c_dc_scale;
898 static int msmpeg4_decode_motion(MpegEncContext * s,
899 int *mx_ptr, int *my_ptr)
904 mv = &mv_tables[s->mv_table_index];
906 code = get_vlc(&s->gb, &mv->vlc);
910 mx = get_bits(&s->gb, 6);
911 my = get_bits(&s->gb, 6);
913 mx = mv->table_mvx[code];
914 my = mv->table_mvy[code];
919 /* WARNING : they do not do exactly modulo encoding */