- s->rl_table_index = decode012(&s->gb);
- s->rl_chroma_table_index = s->rl_table_index;
-
- s->dc_table_index = get_bits1(&s->gb);
-
- s->mv_table_index = get_bits1(&s->gb);
- }
-/* printf(" %d %d %d %d %d \n",
- s->use_skip_mb_code,
- s->rl_table_index,
- s->rl_chroma_table_index,
- s->dc_table_index,
- s->mv_table_index);*/
- if(s->flipflop_rounding){
- s->no_rounding ^= 1;
- }else{
- s->no_rounding = 0;
- }
-// printf("%d", s->no_rounding);
-//return -1;
- }
-
-#if 0
-if(s->msmpeg4_version==2)
-{
-int i;
-for(i=0; i<s->gb.size*8; i++)
-// printf("%d", get_bits1(&s->gb));
- get_bits1(&s->gb);
-printf("END\n");
-return -1;
-}
-#endif
-
-#ifdef DEBUG
- printf("*****frame %d:\n", frame_count++);
-#endif
- return 0;
-}
-
-int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
-{
- /* the alt_bitstream reader could read over the end so we need to check it */
- if(get_bits_count(&s->gb) + 16 < buf_size*8)
- {
- int fps;
-
- fps= get_bits(&s->gb, 5);
- s->bitrate= get_bits(&s->gb, 11);
- s->flipflop_rounding= get_bits1(&s->gb);
-
-// printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bitrate, s->flipflop_rounding);
- }
- else
- {
- s->flipflop_rounding= 0;
- s->bitrate= 0;
- }
-
- return 0;
-}
-
-static inline void memsetw(short *tab, int val, int n)
-{
- int i;
- for(i=0;i<n;i++)
- tab[i] = val;
-}
-
-static void msmpeg4v2_encode_motion(MpegEncContext * s, int val)
-{
- int range, bit_size, sign, code, bits;
-
- if (val == 0) {
- /* zero vector */
- code = 0;
- put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
- } else {
- bit_size = s->f_code - 1;
- range = 1 << bit_size;
- if (val <= -64)
- val += 64;
- else if (val >= 64)
- val -= 64;
-
- if (val >= 0) {
- sign = 0;
- } else {
- val = -val;
- sign = 1;
- }
- val--;
- code = (val >> bit_size) + 1;
- bits = val & (range - 1);
-
- put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
- if (bit_size > 0) {
- put_bits(&s->pb, bit_size, bits);
- }
- }
-}
-
-/* this is identical to h263 except that its range is multiplied by 2 */
-static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
-{
- int code, val, sign, shift;
-
- code = get_vlc(&s->gb, &v2_mv_vlc);
- if (code < 0)
- return 0xffff;
-
- if (code == 0)
- return pred;
- sign = get_bits1(&s->gb);
- shift = f_code - 1;
- val = (code - 1) << shift;
- if (shift > 0)
- val |= get_bits(&s->gb, shift);
- val++;
- if (sign)
- val = -val;
- val += pred;
-
- if (val <= -64)
- val += 64;
- else if (val >= 64)
- val -= 64;
-
- return val;
-}
-
-
-int msmpeg4v2_decode_mb(MpegEncContext *s,
- DCTELEM block[6][64])
-{
- int cbp, code, i;
- if (s->pict_type == P_TYPE) {
- if (s->use_skip_mb_code) {
- if (get_bits1(&s->gb)) {
- /* skip mb */
- s->mb_intra = 0;
- for(i=0;i<6;i++)
- s->block_last_index[i] = -1;
- s->mv_dir = MV_DIR_FORWARD;
- s->mv_type = MV_TYPE_16X16;
- s->mv[0][0][0] = 0;
- s->mv[0][0][1] = 0;
- s->mb_skiped = 1;
- return 0;
- }
- }
-
- code = get_vlc(&s->gb, &v2_mb_type_vlc);
- s->mb_intra = code >>2;
-
- cbp = code & 0x3;
- } else {
- s->mb_intra = 1;
- cbp= get_vlc(&s->gb, &v2_intra_cbpc_vlc);
- }
-
- if (!s->mb_intra) {
- int mx, my;
-
- cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2;
- if((cbp&3) != 3) cbp^= 0x3C;
-
- h263_pred_motion(s, 0, &mx, &my);
- mx= msmpeg4v2_decode_motion(s, mx, 1);
- my= msmpeg4v2_decode_motion(s, my, 1);
-
- s->mv_dir = MV_DIR_FORWARD;
- s->mv_type = MV_TYPE_16X16;
- s->mv[0][0][0] = mx;
- s->mv[0][0][1] = my;
- } else {
- s->ac_pred = get_bits1(&s->gb);
- cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2;
- }
-
- for (i = 0; i < 6; i++) {
- if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
- {
- fprintf(stderr,"\nIgnoring error while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
- return -1;
- }
- }
- return 0;
-}
-
-int msmpeg4_decode_mb(MpegEncContext *s,
- DCTELEM block[6][64])
-{
- int cbp, code, i;
- UINT8 *coded_val;
-
- /* special slice handling */
- if (s->mb_x == 0) {
- if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
- int wrap;
- /* reset DC pred (set previous line to 1024) */
- wrap = 2 * s->mb_width + 2;
- memsetw(&s->dc_val[0][(1) + (2 * s->mb_y) * wrap],
- 1024, 2 * s->mb_width);
- wrap = s->mb_width + 2;
- memsetw(&s->dc_val[1][(1) + (s->mb_y) * wrap],
- 1024, s->mb_width);
- memsetw(&s->dc_val[2][(1) + (s->mb_y) * wrap],
- 1024, s->mb_width);
-
- /* reset AC pred (set previous line to 0) */
- wrap = s->mb_width * 2 + 2;
- memsetw(s->ac_val[0][0] + (1 + (2 * s->mb_y) * wrap)*16,
- 0, 2 * s->mb_width*16);
- wrap = s->mb_width + 2;
- memsetw(s->ac_val[1][0] + (1 + (s->mb_y) * wrap)*16,
- 0, s->mb_width*16);
- memsetw(s->ac_val[2][0] + (1 + (s->mb_y) * wrap)*16,
- 0, s->mb_width*16);
-
- s->first_slice_line = 1;
- } else {
- s->first_slice_line = 0;
- }
- }
-
- if(s->msmpeg4_version==2) return msmpeg4v2_decode_mb(s, block); //FIXME merge if possible
-
- if (s->pict_type == P_TYPE) {
- set_stat(ST_INTER_MB);
- if (s->use_skip_mb_code) {
- if (get_bits1(&s->gb)) {
- /* skip mb */
- s->mb_intra = 0;
- for(i=0;i<6;i++)
- s->block_last_index[i] = -1;
- s->mv_dir = MV_DIR_FORWARD;
- s->mv_type = MV_TYPE_16X16;
- s->mv[0][0][0] = 0;
- s->mv[0][0][1] = 0;
- s->mb_skiped = 1;
- return 0;
- }
- }
-
- code = get_vlc(&s->gb, &mb_non_intra_vlc);
- if (code < 0)
- return -1;
- //s->mb_intra = (code & 0x40) ? 0 : 1;
- s->mb_intra = (~code & 0x40) >> 6;
-
- cbp = code & 0x3f;
- } else {
- set_stat(ST_INTRA_MB);
- s->mb_intra = 1;
- code = get_vlc(&s->gb, &mb_intra_vlc);
- if (code < 0)
- return -1;
- /* predict coded block pattern */
- cbp = 0;
- for(i=0;i<6;i++) {
- int val = ((code >> (5 - i)) & 1);
- if (i < 4) {
- int pred = coded_block_pred(s, i, &coded_val);
- val = val ^ pred;
- *coded_val = val;
- }
- cbp |= val << (5 - i);
- }
- }
-
- if (!s->mb_intra) {
- int mx, my;
- set_stat(ST_MV);
- h263_pred_motion(s, 0, &mx, &my);
- if (msmpeg4_decode_motion(s, &mx, &my) < 0)
- return -1;
- s->mv_dir = MV_DIR_FORWARD;
- s->mv_type = MV_TYPE_16X16;
- s->mv[0][0][0] = mx;
- s->mv[0][0][1] = my;
- } else {
- set_stat(ST_INTRA_MB);
- s->ac_pred = get_bits1(&s->gb);
- }
-
- for (i = 0; i < 6; i++) {
- if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
- {
- fprintf(stderr,"\nIgnoring error while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
- // return -1;
- }
- }
- return 0;
-}
-
-static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
- int n, int coded)
-{
- int code, level, i, j, last, run, run_diff;
- int dc_pred_dir;
- RLTable *rl;
- const UINT8 *scan_table;
- int qmul, qadd;
-
- if (s->mb_intra) {
- qmul=1;
- qadd=0;
-
- /* DC coef */
- set_stat(ST_DC);
- level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
- if (level < 0)
- return -1;
- block[0] = level;
- if (n < 4) {
- rl = &rl_table[s->rl_table_index];
- } else {
- rl = &rl_table[3 + s->rl_chroma_table_index];
- }
-
- run_diff = 0;
- i = 1;
- if (!coded) {
- goto not_coded;
- }
- if (s->ac_pred) {
- if (dc_pred_dir == 0)
- scan_table = ff_alternate_vertical_scan; /* left */
- else
- scan_table = ff_alternate_horizontal_scan; /* top */
- } else {
- scan_table = zigzag_direct;
- }
- set_stat(ST_INTRA_AC);
- } else {
- qmul = s->qscale << 1;
- qadd = (s->qscale - 1) | 1;
- i = 0;
- rl = &rl_table[3 + s->rl_table_index];
-
- if(s->msmpeg4_version==2)
- run_diff = 0;
- else
- run_diff = 1;
-
- if (!coded) {
- s->block_last_index[n] = i - 1;
- return 0;
- }
- scan_table = zigzag_direct;
- set_stat(ST_INTER_AC);
- }
-
- for(;;) {
- code = get_vlc(&s->gb, &rl->vlc);
- if (code < 0)
- return -1;
- if (code == rl->n) {
- /* escape */
- if (get_bits1(&s->gb) == 0) {
- if (get_bits1(&s->gb) == 0) {
- /* third escape */
- last = get_bits1(&s->gb);
- run = get_bits(&s->gb, 6);
- level = get_bits(&s->gb, 8);
- level = (level << 24) >> 24; /* sign extend */
- //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
- if (level>0) level= level * qmul + qadd;
- else level= level * qmul - qadd;
- } else {
- /* second escape */
- code = get_vlc(&s->gb, &rl->vlc);
- if (code < 0 || code >= rl->n)
- return -1;
- run = rl->table_run[code];
- level = rl->table_level[code];
- last = code >= rl->last;
- run += rl->max_run[last][level] + run_diff;
- level= level * qmul + qadd;
- if (get_bits1(&s->gb))
- level = -level;
- }