comparison h263.c @ 549:218eb765c987 libavcodec

rl decoding optimization
author michaelni
date Sat, 13 Jul 2002 17:30:43 +0000
parents 5264fb104700
children 61442627f857
comparison
equal deleted inserted replaced
548:3f05be811b5a 549:218eb765c987
1702 init_rl(&rl_intra); 1702 init_rl(&rl_intra);
1703 init_rl(&rl_intra_aic); 1703 init_rl(&rl_intra_aic);
1704 init_vlc_rl(&rl_inter); 1704 init_vlc_rl(&rl_inter);
1705 init_vlc_rl(&rl_intra); 1705 init_vlc_rl(&rl_intra);
1706 init_vlc_rl(&rl_intra_aic); 1706 init_vlc_rl(&rl_intra_aic);
1707 init_vlc(&dc_lum, DC_VLC_BITS, 9 /* 13 */, 1707 init_vlc(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
1708 &DCtab_lum[0][1], 2, 1, 1708 &DCtab_lum[0][1], 2, 1,
1709 &DCtab_lum[0][0], 2, 1); 1709 &DCtab_lum[0][0], 2, 1);
1710 init_vlc(&dc_chrom, DC_VLC_BITS, 9 /* 13 */, 1710 init_vlc(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
1711 &DCtab_chrom[0][1], 2, 1, 1711 &DCtab_chrom[0][1], 2, 1,
1712 &DCtab_chrom[0][0], 2, 1); 1712 &DCtab_chrom[0][0], 2, 1);
1713 init_vlc(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15, 1713 init_vlc(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
1714 &sprite_trajectory_tab[0][1], 4, 2, 1714 &sprite_trajectory_tab[0][1], 4, 2,
1715 &sprite_trajectory_tab[0][0], 4, 2); 1715 &sprite_trajectory_tab[0][0], 4, 2);
2915 * returns DECODING_ACDC_LOST if an error was detected during DC decoding 2915 * returns DECODING_ACDC_LOST if an error was detected during DC decoding
2916 */ 2916 */
2917 static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block, 2917 static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
2918 int n, int coded) 2918 int n, int coded)
2919 { 2919 {
2920 int code, level, i, j, last, run; 2920 int level, i, last, run;
2921 int dc_pred_dir; 2921 int dc_pred_dir;
2922 RLTable *rl; 2922 RLTable *rl;
2923 RL_VLC_ELEM *rl_vlc;
2923 const UINT8 *scan_table; 2924 const UINT8 *scan_table;
2925 int qmul, qadd;
2924 2926
2925 if (s->mb_intra) { 2927 if (s->mb_intra) {
2926 /* DC coef */ 2928 /* DC coef */
2927 if(s->data_partitioning && s->pict_type!=B_TYPE){ 2929 if(s->data_partitioning && s->pict_type!=B_TYPE){
2928 level = s->dc_val[0][ s->block_index[n] ]; 2930 level = s->dc_val[0][ s->block_index[n] ];
2933 level = mpeg4_decode_dc(s, n, &dc_pred_dir); 2935 level = mpeg4_decode_dc(s, n, &dc_pred_dir);
2934 if (level < 0) 2936 if (level < 0)
2935 return DECODING_ACDC_LOST; 2937 return DECODING_ACDC_LOST;
2936 } 2938 }
2937 block[0] = level; 2939 block[0] = level;
2938 i = 1; 2940 i = 0;
2939 if (!coded) 2941 if (!coded)
2940 goto not_coded; 2942 goto not_coded;
2941 rl = &rl_intra; 2943 rl = &rl_intra;
2944 rl_vlc = rl_intra.rl_vlc[0];
2942 if (s->ac_pred) { 2945 if (s->ac_pred) {
2943 if (dc_pred_dir == 0) 2946 if (dc_pred_dir == 0)
2944 scan_table = ff_alternate_vertical_scan; /* left */ 2947 scan_table = ff_alternate_vertical_scan; /* left */
2945 else 2948 else
2946 scan_table = ff_alternate_horizontal_scan; /* top */ 2949 scan_table = ff_alternate_horizontal_scan; /* top */
2947 } else { 2950 } else {
2948 scan_table = zigzag_direct; 2951 scan_table = zigzag_direct;
2949 } 2952 }
2953 qmul=1;
2954 qadd=0;
2950 } else { 2955 } else {
2951 i = 0; 2956 i = -1;
2952 if (!coded) { 2957 if (!coded) {
2953 s->block_last_index[n] = i - 1; 2958 s->block_last_index[n] = i;
2954 return 0; 2959 return 0;
2955 } 2960 }
2956 rl = &rl_inter; 2961 rl = &rl_inter;
2962 rl_vlc = rl_inter.rl_vlc[s->qscale];
2957 scan_table = zigzag_direct; 2963 scan_table = zigzag_direct;
2958 } 2964 qmul = s->qscale << 1;
2959 2965 qadd = (s->qscale - 1) | 1;
2966 }
2967 {
2968 OPEN_READER(re, &s->gb);
2960 for(;;) { 2969 for(;;) {
2961 code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2); 2970 UPDATE_CACHE(re, &s->gb);
2962 if (code < 0) 2971 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
2963 return DECODING_AC_LOST; 2972 if (level==0) {
2964 if (code == rl->n) { 2973 int cache;
2974 cache= GET_CACHE(re, &s->gb);
2965 /* escape */ 2975 /* escape */
2966 if (get_bits1(&s->gb) != 0) { 2976 if (cache&0x80000000) {
2967 if (get_bits1(&s->gb) != 0) { 2977 if (cache&0x40000000) {
2968 /* third escape */ 2978 /* third escape */
2969 last = get_bits1(&s->gb); 2979 SKIP_CACHE(re, &s->gb, 2);
2970 run = get_bits(&s->gb, 6); 2980 last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
2971 if(get_bits1(&s->gb)==0){ 2981 run= SHOW_UBITS(re, &s->gb, 6); LAST_SKIP_CACHE(re, &s->gb, 6);
2982 SKIP_COUNTER(re, &s->gb, 2+1+6);
2983 UPDATE_CACHE(re, &s->gb);
2984
2985 if(SHOW_UBITS(re, &s->gb, 1)==0){
2972 fprintf(stderr, "1. marker bit missing in 3. esc\n"); 2986 fprintf(stderr, "1. marker bit missing in 3. esc\n");
2973 return DECODING_AC_LOST; 2987 return DECODING_AC_LOST;
2974 } 2988 }; SKIP_CACHE(re, &s->gb, 1);
2975 level = get_bits(&s->gb, 12); 2989
2976 level = (level << 20) >> 20; /* sign extend */ 2990 level= SHOW_SBITS(re, &s->gb, 12); SKIP_CACHE(re, &s->gb, 12);
2977 if(get_bits1(&s->gb)==0){ 2991
2992 if(SHOW_UBITS(re, &s->gb, 1)==0){
2978 fprintf(stderr, "2. marker bit missing in 3. esc\n"); 2993 fprintf(stderr, "2. marker bit missing in 3. esc\n");
2979 return DECODING_AC_LOST; 2994 return DECODING_AC_LOST;
2980 } 2995 }; LAST_SKIP_CACHE(re, &s->gb, 1);
2996
2997 SKIP_COUNTER(re, &s->gb, 1+12+1);
2998
2981 if(level>512 || level<-512){ //FIXME check that QP=1 is ok with this too 2999 if(level>512 || level<-512){ //FIXME check that QP=1 is ok with this too
2982 fprintf(stderr, "|level| overflow in 3. esc\n"); 3000 fprintf(stderr, "|level| overflow in 3. esc\n");
2983 return DECODING_AC_LOST; 3001 return DECODING_AC_LOST;
2984 } 3002 }
2985 #if 1 3003 #if 1
3000 return DECODING_AC_LOST; 3018 return DECODING_AC_LOST;
3001 } 3019 }
3002 } 3020 }
3003 } 3021 }
3004 #endif 3022 #endif
3023 if (level>0) level= level * qmul + qadd;
3024 else level= level * qmul - qadd;
3025
3026 i+= run + 1;
3027 if(last) i+=192;
3005 } else { 3028 } else {
3006 /* second escape */ 3029 /* second escape */
3007 code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2); 3030 #if MIN_CACHE_BITS < 20
3008 if (code < 0 || code >= rl->n) 3031 LAST_SKIP_BITS(re, &s->gb, 2);
3009 return DECODING_AC_LOST; 3032 UPDATE_CACHE(re, &s->gb);
3010 run = rl->table_run[code]; 3033 #else
3011 level = rl->table_level[code]; 3034 SKIP_BITS(re, &s->gb, 2);
3012 last = code >= rl->last; 3035 #endif
3013 run += rl->max_run[last][level] + 1; 3036 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
3014 if (get_bits1(&s->gb)) 3037 i+= run + rl->max_run[run>>7][level/qmul] +1; //FIXME opt indexing
3015 level = -level; 3038 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
3039 LAST_SKIP_BITS(re, &s->gb, 1);
3016 } 3040 }
3017 } else { 3041 } else {
3018 /* first escape */ 3042 /* first escape */
3019 code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2); 3043 #if MIN_CACHE_BITS < 19
3020 if (code < 0 || code >= rl->n) 3044 LAST_SKIP_BITS(re, &s->gb, 1);
3021 return DECODING_AC_LOST; 3045 UPDATE_CACHE(re, &s->gb);
3022 run = rl->table_run[code]; 3046 #else
3023 level = rl->table_level[code]; 3047 SKIP_BITS(re, &s->gb, 1);
3024 last = code >= rl->last; 3048 #endif
3025 level += rl->max_level[last][run]; 3049 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
3026 if (get_bits1(&s->gb)) 3050 i+= run;
3027 level = -level; 3051 level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
3052 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
3053 LAST_SKIP_BITS(re, &s->gb, 1);
3028 } 3054 }
3029 } else { 3055 } else {
3030 run = rl->table_run[code]; 3056 i+= run;
3031 level = rl->table_level[code]; 3057 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
3032 last = code >= rl->last; 3058 LAST_SKIP_BITS(re, &s->gb, 1);
3033 if (get_bits1(&s->gb)) 3059 }
3034 level = -level; 3060 if (i > 62){
3035 } 3061 i-= 192;
3036 i += run; 3062 if(i&(~63)){
3037 if (i >= 64) 3063 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
3038 return DECODING_AC_LOST; 3064 return DECODING_AC_LOST;
3039 j = scan_table[i]; 3065 }
3040 block[j] = level; 3066
3041 i++; 3067 block[scan_table[i]] = level;
3042 if (last)
3043 break; 3068 break;
3044 } 3069 }
3070
3071 block[scan_table[i]] = level;
3072 }
3073 CLOSE_READER(re, &s->gb);
3074 }
3045 not_coded: 3075 not_coded:
3046 if (s->mb_intra) { 3076 if (s->mb_intra) {
3047 mpeg4_pred_ac(s, block, n, dc_pred_dir); 3077 mpeg4_pred_ac(s, block, n, dc_pred_dir);
3048 if (s->ac_pred) { 3078 if (s->ac_pred) {
3049 i = 64; /* XXX: not optimal */ 3079 i = 63; /* XXX: not optimal */
3050 } 3080 }
3051 } 3081 }
3052 s->block_last_index[n] = i - 1; 3082 s->block_last_index[n] = i;
3053 return 0; 3083 return 0;
3054 } 3084 }
3055 3085
3056 /* most is hardcoded. should extend to handle all h263 streams */ 3086 /* most is hardcoded. should extend to handle all h263 streams */
3057 int h263_decode_picture_header(MpegEncContext *s) 3087 int h263_decode_picture_header(MpegEncContext *s)