Mercurial > libavcodec.hg
comparison h263.c @ 544:5264fb104700 libavcodec
optimization (get_vlc -> get_vlc2)
author | michaelni |
---|---|
date | Sat, 13 Jul 2002 15:45:52 +0000 |
parents | d55978a3c369 |
children | 218eb765c987 |
comparison
equal
deleted
inserted
replaced
543:8f8f4885d874 | 544:5264fb104700 |
---|---|
32 //rounded divison & shift | 32 //rounded divison & shift |
33 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b)) | 33 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b)) |
34 | 34 |
35 #define PRINT_MB_TYPE(a) {} | 35 #define PRINT_MB_TYPE(a) {} |
36 //#define PRINT_MB_TYPE(a) printf(a) | 36 //#define PRINT_MB_TYPE(a) printf(a) |
37 | |
38 #define INTRA_MCBPC_VLC_BITS 6 | |
39 #define INTER_MCBPC_VLC_BITS 6 | |
40 #define CBPY_VLC_BITS 6 | |
41 #define MV_VLC_BITS 9 | |
42 #define DC_VLC_BITS 9 | |
43 #define SPRITE_TRAJ_VLC_BITS 6 | |
44 #define MB_TYPE_B_VLC_BITS 4 | |
45 #define TEX_VLC_BITS 9 | |
37 | 46 |
38 static void h263_encode_block(MpegEncContext * s, DCTELEM * block, | 47 static void h263_encode_block(MpegEncContext * s, DCTELEM * block, |
39 int n); | 48 int n); |
40 static void h263_encode_motion(MpegEncContext * s, int val, int fcode); | 49 static void h263_encode_motion(MpegEncContext * s, int val, int fcode); |
41 static void h263p_encode_umotion(MpegEncContext * s, int val); | 50 static void h263p_encode_umotion(MpegEncContext * s, int val); |
1675 static int done = 0; | 1684 static int done = 0; |
1676 | 1685 |
1677 if (!done) { | 1686 if (!done) { |
1678 done = 1; | 1687 done = 1; |
1679 | 1688 |
1680 init_vlc(&intra_MCBPC_vlc, 6, 8, | 1689 init_vlc(&intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 8, |
1681 intra_MCBPC_bits, 1, 1, | 1690 intra_MCBPC_bits, 1, 1, |
1682 intra_MCBPC_code, 1, 1); | 1691 intra_MCBPC_code, 1, 1); |
1683 init_vlc(&inter_MCBPC_vlc, 9, 25, | 1692 init_vlc(&inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 25, |
1684 inter_MCBPC_bits, 1, 1, | 1693 inter_MCBPC_bits, 1, 1, |
1685 inter_MCBPC_code, 1, 1); | 1694 inter_MCBPC_code, 1, 1); |
1686 init_vlc(&cbpy_vlc, 6, 16, | 1695 init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16, |
1687 &cbpy_tab[0][1], 2, 1, | 1696 &cbpy_tab[0][1], 2, 1, |
1688 &cbpy_tab[0][0], 2, 1); | 1697 &cbpy_tab[0][0], 2, 1); |
1689 init_vlc(&mv_vlc, 9, 33, | 1698 init_vlc(&mv_vlc, MV_VLC_BITS, 33, |
1690 &mvtab[0][1], 2, 1, | 1699 &mvtab[0][1], 2, 1, |
1691 &mvtab[0][0], 2, 1); | 1700 &mvtab[0][0], 2, 1); |
1692 init_rl(&rl_inter); | 1701 init_rl(&rl_inter); |
1693 init_rl(&rl_intra); | 1702 init_rl(&rl_intra); |
1694 init_rl(&rl_intra_aic); | 1703 init_rl(&rl_intra_aic); |
1695 init_vlc_rl(&rl_inter); | 1704 init_vlc_rl(&rl_inter); |
1696 init_vlc_rl(&rl_intra); | 1705 init_vlc_rl(&rl_intra); |
1697 init_vlc_rl(&rl_intra_aic); | 1706 init_vlc_rl(&rl_intra_aic); |
1698 init_vlc(&dc_lum, 9, 13, | 1707 init_vlc(&dc_lum, DC_VLC_BITS, 9 /* 13 */, |
1699 &DCtab_lum[0][1], 2, 1, | 1708 &DCtab_lum[0][1], 2, 1, |
1700 &DCtab_lum[0][0], 2, 1); | 1709 &DCtab_lum[0][0], 2, 1); |
1701 init_vlc(&dc_chrom, 9, 13, | 1710 init_vlc(&dc_chrom, DC_VLC_BITS, 9 /* 13 */, |
1702 &DCtab_chrom[0][1], 2, 1, | 1711 &DCtab_chrom[0][1], 2, 1, |
1703 &DCtab_chrom[0][0], 2, 1); | 1712 &DCtab_chrom[0][0], 2, 1); |
1704 init_vlc(&sprite_trajectory, 9, 15, | 1713 init_vlc(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15, |
1705 &sprite_trajectory_tab[0][1], 4, 2, | 1714 &sprite_trajectory_tab[0][1], 4, 2, |
1706 &sprite_trajectory_tab[0][0], 4, 2); | 1715 &sprite_trajectory_tab[0][0], 4, 2); |
1707 init_vlc(&mb_type_b_vlc, 4, 4, | 1716 init_vlc(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4, |
1708 &mb_type_b_tab[0][1], 2, 1, | 1717 &mb_type_b_tab[0][1], 2, 1, |
1709 &mb_type_b_tab[0][0], 2, 1); | 1718 &mb_type_b_tab[0][0], 2, 1); |
1710 } | 1719 } |
1711 } | 1720 } |
1712 | 1721 |
2022 | 2031 |
2023 if(s->pict_type==I_TYPE){ | 2032 if(s->pict_type==I_TYPE){ |
2024 int i; | 2033 int i; |
2025 | 2034 |
2026 PRINT_MB_TYPE("I"); | 2035 PRINT_MB_TYPE("I"); |
2027 cbpc = get_vlc(&s->gb, &intra_MCBPC_vlc); | 2036 cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 1); |
2028 if (cbpc < 0){ | 2037 if (cbpc < 0){ |
2029 fprintf(stderr, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y); | 2038 fprintf(stderr, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y); |
2030 return DECODING_DESYNC; | 2039 return DECODING_DESYNC; |
2031 } | 2040 } |
2032 s->cbp_table[xy]= cbpc & 3; | 2041 s->cbp_table[xy]= cbpc & 3; |
2087 if(s->mbintra_table[xy]) | 2096 if(s->mbintra_table[xy]) |
2088 ff_clean_intra_table_entries(s); | 2097 ff_clean_intra_table_entries(s); |
2089 | 2098 |
2090 continue; | 2099 continue; |
2091 } | 2100 } |
2092 cbpc = get_vlc(&s->gb, &inter_MCBPC_vlc); | 2101 cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2); |
2093 if (cbpc < 0){ | 2102 if (cbpc < 0){ |
2094 fprintf(stderr, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y); | 2103 fprintf(stderr, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y); |
2095 return DECODING_DESYNC; | 2104 return DECODING_DESYNC; |
2096 } | 2105 } |
2097 if (cbpc > 20) | 2106 if (cbpc > 20) |
2194 mb_num++; | 2203 mb_num++; |
2195 update_block_index(s); | 2204 update_block_index(s); |
2196 | 2205 |
2197 if(s->pict_type==I_TYPE){ | 2206 if(s->pict_type==I_TYPE){ |
2198 int ac_pred= get_bits1(&s->gb); | 2207 int ac_pred= get_bits1(&s->gb); |
2199 int cbpy = get_vlc(&s->gb, &cbpy_vlc); | 2208 int cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1); |
2200 if(cbpy<0){ | 2209 if(cbpy<0){ |
2201 fprintf(stderr, "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y); | 2210 fprintf(stderr, "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y); |
2202 return DECODING_AC_LOST; | 2211 return DECODING_AC_LOST; |
2203 } | 2212 } |
2204 | 2213 |
2206 s->pred_dir_table[xy]|= ac_pred<<7; | 2215 s->pred_dir_table[xy]|= ac_pred<<7; |
2207 }else{ /* P || S_TYPE */ | 2216 }else{ /* P || S_TYPE */ |
2208 if(s->mb_type[xy]&MB_TYPE_INTRA){ | 2217 if(s->mb_type[xy]&MB_TYPE_INTRA){ |
2209 int dir=0,i; | 2218 int dir=0,i; |
2210 int ac_pred = get_bits1(&s->gb); | 2219 int ac_pred = get_bits1(&s->gb); |
2211 int cbpy = get_vlc(&s->gb, &cbpy_vlc); | 2220 int cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1); |
2212 | 2221 |
2213 if(cbpy<0){ | 2222 if(cbpy<0){ |
2214 fprintf(stderr, "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y); | 2223 fprintf(stderr, "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y); |
2215 return DECODING_ACDC_LOST; | 2224 return DECODING_ACDC_LOST; |
2216 } | 2225 } |
2240 s->pred_dir_table[xy]= dir | (ac_pred<<7); | 2249 s->pred_dir_table[xy]= dir | (ac_pred<<7); |
2241 }else if(s->mb_type[xy]&MB_TYPE_SKIPED){ | 2250 }else if(s->mb_type[xy]&MB_TYPE_SKIPED){ |
2242 s->qscale_table[xy]= s->qscale; | 2251 s->qscale_table[xy]= s->qscale; |
2243 s->cbp_table[xy]= 0; | 2252 s->cbp_table[xy]= 0; |
2244 }else{ | 2253 }else{ |
2245 int cbpy = get_vlc(&s->gb, &cbpy_vlc); | 2254 int cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1); |
2246 | 2255 |
2247 if(cbpy<0){ | 2256 if(cbpy<0){ |
2248 fprintf(stderr, "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y); | 2257 fprintf(stderr, "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y); |
2249 return DECODING_ACDC_LOST; | 2258 return DECODING_ACDC_LOST; |
2250 } | 2259 } |
2441 s->mv[0][0][1] = 0; | 2450 s->mv[0][0][1] = 0; |
2442 s->mb_skiped = 1; | 2451 s->mb_skiped = 1; |
2443 } | 2452 } |
2444 return 0; | 2453 return 0; |
2445 } | 2454 } |
2446 cbpc = get_vlc(&s->gb, &inter_MCBPC_vlc); | 2455 cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2); |
2447 //fprintf(stderr, "\tCBPC: %d", cbpc); | 2456 //fprintf(stderr, "\tCBPC: %d", cbpc); |
2448 if (cbpc < 0) | 2457 if (cbpc < 0) |
2449 return -1; | 2458 return -1; |
2450 if (cbpc > 20) | 2459 if (cbpc > 20) |
2451 cbpc+=3; | 2460 cbpc+=3; |
2457 if (s->mb_intra) goto intra; | 2466 if (s->mb_intra) goto intra; |
2458 | 2467 |
2459 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0) | 2468 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0) |
2460 s->mcsel= get_bits1(&s->gb); | 2469 s->mcsel= get_bits1(&s->gb); |
2461 else s->mcsel= 0; | 2470 else s->mcsel= 0; |
2462 cbpy = get_vlc(&s->gb, &cbpy_vlc); | 2471 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1); |
2463 cbp = (cbpc & 3) | ((cbpy ^ 0xf) << 2); | 2472 cbp = (cbpc & 3) | ((cbpy ^ 0xf) << 2); |
2464 if (dquant) { | 2473 if (dquant) { |
2465 s->qscale += quant_tab[get_bits(&s->gb, 2)]; | 2474 s->qscale += quant_tab[get_bits(&s->gb, 2)]; |
2466 if (s->qscale < 1) | 2475 if (s->qscale < 1) |
2467 s->qscale = 1; | 2476 s->qscale = 1; |
2584 } | 2593 } |
2585 | 2594 |
2586 modb1= get_bits1(&s->gb); | 2595 modb1= get_bits1(&s->gb); |
2587 if(modb1==0){ | 2596 if(modb1==0){ |
2588 modb2= get_bits1(&s->gb); | 2597 modb2= get_bits1(&s->gb); |
2589 mb_type= get_vlc(&s->gb, &mb_type_b_vlc); | 2598 mb_type= get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1); |
2590 if(modb2==0) cbp= get_bits(&s->gb, 6); | 2599 if(modb2==0) cbp= get_bits(&s->gb, 6); |
2591 else cbp=0; | 2600 else cbp=0; |
2592 if (mb_type && cbp) { | 2601 if (mb_type && cbp) { |
2593 if(get_bits1(&s->gb)){ | 2602 if(get_bits1(&s->gb)){ |
2594 s->qscale +=get_bits1(&s->gb)*4 - 2; | 2603 s->qscale +=get_bits1(&s->gb)*4 - 2; |
2662 default: | 2671 default: |
2663 printf("illegal MB_type\n"); | 2672 printf("illegal MB_type\n"); |
2664 return -1; | 2673 return -1; |
2665 } | 2674 } |
2666 } else { /* I-Frame */ | 2675 } else { /* I-Frame */ |
2667 cbpc = get_vlc(&s->gb, &intra_MCBPC_vlc); | 2676 cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 1); |
2668 if (cbpc < 0) | 2677 if (cbpc < 0) |
2669 return -1; | 2678 return -1; |
2670 dquant = cbpc & 4; | 2679 dquant = cbpc & 4; |
2671 s->mb_intra = 1; | 2680 s->mb_intra = 1; |
2672 intra: | 2681 intra: |
2675 if (s->h263_pred || s->h263_aic) { | 2684 if (s->h263_pred || s->h263_aic) { |
2676 s->ac_pred = get_bits1(&s->gb); | 2685 s->ac_pred = get_bits1(&s->gb); |
2677 if (s->ac_pred && s->h263_aic) | 2686 if (s->ac_pred && s->h263_aic) |
2678 s->h263_aic_dir = get_bits1(&s->gb); | 2687 s->h263_aic_dir = get_bits1(&s->gb); |
2679 } | 2688 } |
2680 cbpy = get_vlc(&s->gb, &cbpy_vlc); | 2689 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1); |
2681 if(cbpy<0) return -1; | 2690 if(cbpy<0) return -1; |
2682 cbp = (cbpc & 3) | (cbpy << 2); | 2691 cbp = (cbpc & 3) | (cbpy << 2); |
2683 if (dquant) { | 2692 if (dquant) { |
2684 s->qscale += quant_tab[get_bits(&s->gb, 2)]; | 2693 s->qscale += quant_tab[get_bits(&s->gb, 2)]; |
2685 if (s->qscale < 1) | 2694 if (s->qscale < 1) |
2707 | 2716 |
2708 static int h263_decode_motion(MpegEncContext * s, int pred, int f_code) | 2717 static int h263_decode_motion(MpegEncContext * s, int pred, int f_code) |
2709 { | 2718 { |
2710 int code, val, sign, shift, l, m; | 2719 int code, val, sign, shift, l, m; |
2711 | 2720 |
2712 code = get_vlc(&s->gb, &mv_vlc); | 2721 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2); |
2713 if (code < 0) | 2722 if (code < 0) |
2714 return 0xffff; | 2723 return 0xffff; |
2715 | 2724 |
2716 if (code == 0) | 2725 if (code == 0) |
2717 return pred; | 2726 return pred; |
2820 s->block_last_index[n] = i - 1; | 2829 s->block_last_index[n] = i - 1; |
2821 return 0; | 2830 return 0; |
2822 } | 2831 } |
2823 | 2832 |
2824 for(;;) { | 2833 for(;;) { |
2825 code = get_vlc(&s->gb, &rl->vlc); | 2834 code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2); |
2826 if (code < 0) | 2835 if (code < 0) |
2827 return -1; | 2836 return -1; |
2828 if (code == rl->n) { | 2837 if (code == rl->n) { |
2829 /* escape */ | 2838 /* escape */ |
2830 last = get_bits1(&s->gb); | 2839 last = get_bits1(&s->gb); |
2864 { | 2873 { |
2865 int level, pred, code; | 2874 int level, pred, code; |
2866 UINT16 *dc_val; | 2875 UINT16 *dc_val; |
2867 | 2876 |
2868 if (n < 4) | 2877 if (n < 4) |
2869 code = get_vlc(&s->gb, &dc_lum); | 2878 code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1); |
2870 else | 2879 else |
2871 code = get_vlc(&s->gb, &dc_chrom); | 2880 code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1); |
2872 if (code < 0 || code > 9 /* && s->nbit<9 */){ | 2881 if (code < 0 || code > 9 /* && s->nbit<9 */){ |
2873 fprintf(stderr, "illegal dc vlc\n"); | 2882 fprintf(stderr, "illegal dc vlc\n"); |
2874 return -1; | 2883 return -1; |
2875 } | 2884 } |
2876 if (code == 0) { | 2885 if (code == 0) { |
2947 rl = &rl_inter; | 2956 rl = &rl_inter; |
2948 scan_table = zigzag_direct; | 2957 scan_table = zigzag_direct; |
2949 } | 2958 } |
2950 | 2959 |
2951 for(;;) { | 2960 for(;;) { |
2952 code = get_vlc(&s->gb, &rl->vlc); | 2961 code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2); |
2953 if (code < 0) | 2962 if (code < 0) |
2954 return DECODING_AC_LOST; | 2963 return DECODING_AC_LOST; |
2955 if (code == rl->n) { | 2964 if (code == rl->n) { |
2956 /* escape */ | 2965 /* escape */ |
2957 if (get_bits1(&s->gb) != 0) { | 2966 if (get_bits1(&s->gb) != 0) { |
2993 } | 3002 } |
2994 } | 3003 } |
2995 #endif | 3004 #endif |
2996 } else { | 3005 } else { |
2997 /* second escape */ | 3006 /* second escape */ |
2998 code = get_vlc(&s->gb, &rl->vlc); | 3007 code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2); |
2999 if (code < 0 || code >= rl->n) | 3008 if (code < 0 || code >= rl->n) |
3000 return DECODING_AC_LOST; | 3009 return DECODING_AC_LOST; |
3001 run = rl->table_run[code]; | 3010 run = rl->table_run[code]; |
3002 level = rl->table_level[code]; | 3011 level = rl->table_level[code]; |
3003 last = code >= rl->last; | 3012 last = code >= rl->last; |
3005 if (get_bits1(&s->gb)) | 3014 if (get_bits1(&s->gb)) |
3006 level = -level; | 3015 level = -level; |
3007 } | 3016 } |
3008 } else { | 3017 } else { |
3009 /* first escape */ | 3018 /* first escape */ |
3010 code = get_vlc(&s->gb, &rl->vlc); | 3019 code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2); |
3011 if (code < 0 || code >= rl->n) | 3020 if (code < 0 || code >= rl->n) |
3012 return DECODING_AC_LOST; | 3021 return DECODING_AC_LOST; |
3013 run = rl->table_run[code]; | 3022 run = rl->table_run[code]; |
3014 level = rl->table_level[code]; | 3023 level = rl->table_level[code]; |
3015 last = code >= rl->last; | 3024 last = code >= rl->last; |