|
Lines 2862-3073
Link Here
|
| 2862 |
static VLC h263_mbtype_b_vlc; |
2862 |
static VLC h263_mbtype_b_vlc; |
| 2863 |
static VLC cbpc_b_vlc; |
2863 |
static VLC cbpc_b_vlc; |
| 2864 |
|
2864 |
|
| 2865 |
void init_vlc_rl(RLTable *rl, int use_static) |
2865 |
/** |
|
|
2866 |
* decodes the dc value. |
| 2867 |
* @param n block index (0-3 are luma, 4-5 are chroma) |
| 2868 |
* @param dir_ptr the prediction direction will be stored here |
| 2869 |
* @return the quantized dc |
| 2870 |
*/ |
| 2871 |
static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr) |
| 2866 |
{ |
2872 |
{ |
| 2867 |
int i, q; |
2873 |
int level, code; |
| 2868 |
|
|
|
| 2869 |
/* Return if static table is already initialized */ |
| 2870 |
if(use_static && rl->rl_vlc[0]) |
| 2871 |
return; |
| 2872 |
|
| 2873 |
init_vlc(&rl->vlc, 9, rl->n + 1, |
| 2874 |
&rl->table_vlc[0][1], 4, 2, |
| 2875 |
&rl->table_vlc[0][0], 4, 2, use_static); |
| 2876 |
|
2874 |
|
| 2877 |
|
2875 |
if (n < 4) |
| 2878 |
for(q=0; q<32; q++){ |
2876 |
code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1); |
| 2879 |
int qmul= q*2; |
2877 |
else |
| 2880 |
int qadd= (q-1)|1; |
2878 |
code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1); |
| 2881 |
|
2879 |
if (code < 0 || code > 9 /* && s->nbit<9 */){ |
| 2882 |
if(q==0){ |
2880 |
av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n"); |
| 2883 |
qmul=1; |
2881 |
return -1; |
| 2884 |
qadd=0; |
2882 |
} |
|
|
2883 |
if (code == 0) { |
| 2884 |
level = 0; |
| 2885 |
} else { |
| 2886 |
if(IS_3IV1){ |
| 2887 |
if(code==1) |
| 2888 |
level= 2*get_bits1(&s->gb)-1; |
| 2889 |
else{ |
| 2890 |
if(get_bits1(&s->gb)) |
| 2891 |
level = get_bits(&s->gb, code-1) + (1<<(code-1)); |
| 2892 |
else |
| 2893 |
level = -get_bits(&s->gb, code-1) - (1<<(code-1)); |
| 2894 |
} |
| 2895 |
}else{ |
| 2896 |
level = get_xbits(&s->gb, code); |
| 2885 |
} |
2897 |
} |
| 2886 |
if(use_static) |
2898 |
|
| 2887 |
rl->rl_vlc[q]= av_mallocz_static(rl->vlc.table_size*sizeof(RL_VLC_ELEM)); |
2899 |
if (code > 8){ |
| 2888 |
else |
2900 |
if(get_bits1(&s->gb)==0){ /* marker */ |
| 2889 |
rl->rl_vlc[q]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM)); |
2901 |
if(s->error_resilience>=2){ |
| 2890 |
for(i=0; i<rl->vlc.table_size; i++){ |
2902 |
av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n"); |
| 2891 |
int code= rl->vlc.table[i][0]; |
2903 |
return -1; |
| 2892 |
int len = rl->vlc.table[i][1]; |
|
|
| 2893 |
int level, run; |
| 2894 |
|
| 2895 |
if(len==0){ // illegal code |
| 2896 |
run= 66; |
| 2897 |
level= MAX_LEVEL; |
| 2898 |
}else if(len<0){ //more bits needed |
| 2899 |
run= 0; |
| 2900 |
level= code; |
| 2901 |
}else{ |
| 2902 |
if(code==rl->n){ //esc |
| 2903 |
run= 66; |
| 2904 |
level= 0; |
| 2905 |
}else{ |
| 2906 |
run= rl->table_run [code] + 1; |
| 2907 |
level= rl->table_level[code] * qmul + qadd; |
| 2908 |
if(code >= rl->last) run+=192; |
| 2909 |
} |
2904 |
} |
| 2910 |
} |
2905 |
} |
| 2911 |
rl->rl_vlc[q][i].len= len; |
|
|
| 2912 |
rl->rl_vlc[q][i].level= level; |
| 2913 |
rl->rl_vlc[q][i].run= run; |
| 2914 |
} |
2906 |
} |
| 2915 |
} |
2907 |
} |
| 2916 |
} |
|
|
| 2917 |
|
| 2918 |
/* init vlcs */ |
| 2919 |
|
| 2920 |
/* XXX: find a better solution to handle static init */ |
| 2921 |
void h263_decode_init_vlc(MpegEncContext *s) |
| 2922 |
{ |
| 2923 |
static int done = 0; |
| 2924 |
|
| 2925 |
if (!done) { |
| 2926 |
done = 1; |
| 2927 |
|
| 2928 |
init_vlc(&intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 9, |
| 2929 |
intra_MCBPC_bits, 1, 1, |
| 2930 |
intra_MCBPC_code, 1, 1, 1); |
| 2931 |
init_vlc(&inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 28, |
| 2932 |
inter_MCBPC_bits, 1, 1, |
| 2933 |
inter_MCBPC_code, 1, 1, 1); |
| 2934 |
init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16, |
| 2935 |
&cbpy_tab[0][1], 2, 1, |
| 2936 |
&cbpy_tab[0][0], 2, 1, 1); |
| 2937 |
init_vlc(&mv_vlc, MV_VLC_BITS, 33, |
| 2938 |
&mvtab[0][1], 2, 1, |
| 2939 |
&mvtab[0][0], 2, 1, 1); |
| 2940 |
init_rl(&rl_inter, 1); |
| 2941 |
init_rl(&rl_intra, 1); |
| 2942 |
init_rl(&rvlc_rl_inter, 1); |
| 2943 |
init_rl(&rvlc_rl_intra, 1); |
| 2944 |
init_rl(&rl_intra_aic, 1); |
| 2945 |
init_vlc_rl(&rl_inter, 1); |
| 2946 |
init_vlc_rl(&rl_intra, 1); |
| 2947 |
init_vlc_rl(&rvlc_rl_inter, 1); |
| 2948 |
init_vlc_rl(&rvlc_rl_intra, 1); |
| 2949 |
init_vlc_rl(&rl_intra_aic, 1); |
| 2950 |
init_vlc(&dc_lum, DC_VLC_BITS, 10 /* 13 */, |
| 2951 |
&DCtab_lum[0][1], 2, 1, |
| 2952 |
&DCtab_lum[0][0], 2, 1, 1); |
| 2953 |
init_vlc(&dc_chrom, DC_VLC_BITS, 10 /* 13 */, |
| 2954 |
&DCtab_chrom[0][1], 2, 1, |
| 2955 |
&DCtab_chrom[0][0], 2, 1, 1); |
| 2956 |
init_vlc(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15, |
| 2957 |
&sprite_trajectory_tab[0][1], 4, 2, |
| 2958 |
&sprite_trajectory_tab[0][0], 4, 2, 1); |
| 2959 |
init_vlc(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4, |
| 2960 |
&mb_type_b_tab[0][1], 2, 1, |
| 2961 |
&mb_type_b_tab[0][0], 2, 1, 1); |
| 2962 |
init_vlc(&h263_mbtype_b_vlc, H263_MBTYPE_B_VLC_BITS, 15, |
| 2963 |
&h263_mbtype_b_tab[0][1], 2, 1, |
| 2964 |
&h263_mbtype_b_tab[0][0], 2, 1, 1); |
| 2965 |
init_vlc(&cbpc_b_vlc, CBPC_B_VLC_BITS, 4, |
| 2966 |
&cbpc_b_tab[0][1], 2, 1, |
| 2967 |
&cbpc_b_tab[0][0], 2, 1, 1); |
| 2968 |
} |
| 2969 |
} |
| 2970 |
|
| 2971 |
/** |
| 2972 |
* Get the GOB height based on picture height. |
| 2973 |
*/ |
| 2974 |
int ff_h263_get_gob_height(MpegEncContext *s){ |
| 2975 |
if (s->height <= 400) |
| 2976 |
return 1; |
| 2977 |
else if (s->height <= 800) |
| 2978 |
return 2; |
| 2979 |
else |
| 2980 |
return 4; |
| 2981 |
} |
| 2982 |
|
| 2983 |
int ff_h263_decode_mba(MpegEncContext *s) |
| 2984 |
{ |
| 2985 |
int i, mb_pos; |
| 2986 |
|
| 2987 |
for(i=0; i<6; i++){ |
| 2988 |
if(s->mb_num-1 <= ff_mba_max[i]) break; |
| 2989 |
} |
| 2990 |
mb_pos= get_bits(&s->gb, ff_mba_length[i]); |
| 2991 |
s->mb_x= mb_pos % s->mb_width; |
| 2992 |
s->mb_y= mb_pos / s->mb_width; |
| 2993 |
|
| 2994 |
return mb_pos; |
| 2995 |
} |
| 2996 |
|
2908 |
|
| 2997 |
void ff_h263_encode_mba(MpegEncContext *s) |
2909 |
return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0); |
| 2998 |
{ |
|
|
| 2999 |
int i, mb_pos; |
| 3000 |
|
| 3001 |
for(i=0; i<6; i++){ |
| 3002 |
if(s->mb_num-1 <= ff_mba_max[i]) break; |
| 3003 |
} |
| 3004 |
mb_pos= s->mb_x + s->mb_width*s->mb_y; |
| 3005 |
put_bits(&s->pb, ff_mba_length[i], mb_pos); |
| 3006 |
} |
2910 |
} |
| 3007 |
|
2911 |
|
| 3008 |
/** |
2912 |
/** |
| 3009 |
* decodes the group of blocks header or slice header. |
2913 |
* decodes a block. |
| 3010 |
* @return <0 if an error occured |
2914 |
* @return <0 if an error occured |
| 3011 |
*/ |
2915 |
*/ |
| 3012 |
static int h263_decode_gob_header(MpegEncContext *s) |
2916 |
static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block, |
|
|
2917 |
int n, int coded, int intra, int rvlc) |
| 3013 |
{ |
2918 |
{ |
| 3014 |
unsigned int val, gfid, gob_number; |
2919 |
int level, i, last, run; |
| 3015 |
int left; |
2920 |
int dc_pred_dir; |
| 3016 |
|
2921 |
RLTable * rl; |
| 3017 |
/* Check for GOB Start Code */ |
2922 |
RL_VLC_ELEM * rl_vlc; |
| 3018 |
val = show_bits(&s->gb, 16); |
2923 |
const uint8_t * scan_table; |
| 3019 |
if(val) |
2924 |
int qmul, qadd; |
| 3020 |
return -1; |
|
|
| 3021 |
|
| 3022 |
/* We have a GBSC probably with GSTUFF */ |
| 3023 |
skip_bits(&s->gb, 16); /* Drop the zeros */ |
| 3024 |
left= s->gb.size_in_bits - get_bits_count(&s->gb); |
| 3025 |
//MN: we must check the bits left or we might end in a infinite loop (or segfault) |
| 3026 |
for(;left>13; left--){ |
| 3027 |
if(get_bits1(&s->gb)) break; /* Seek the '1' bit */ |
| 3028 |
} |
| 3029 |
if(left<=13) |
| 3030 |
return -1; |
| 3031 |
|
| 3032 |
if(s->h263_slice_structured){ |
| 3033 |
if(get_bits1(&s->gb)==0) |
| 3034 |
return -1; |
| 3035 |
|
| 3036 |
ff_h263_decode_mba(s); |
| 3037 |
|
2925 |
|
| 3038 |
if(s->mb_num > 1583) |
2926 |
//Note intra & rvlc should be optimized away if this is inlined |
| 3039 |
if(get_bits1(&s->gb)==0) |
2927 |
|
|
|
2928 |
if(intra) { |
| 2929 |
if(s->qscale < s->intra_dc_threshold){ |
| 2930 |
/* DC coef */ |
| 2931 |
if(s->partitioned_frame){ |
| 2932 |
level = s->dc_val[0][ s->block_index[n] ]; |
| 2933 |
if(n<4) level= FASTDIV((level + (s->y_dc_scale>>1)), s->y_dc_scale); |
| 2934 |
else level= FASTDIV((level + (s->c_dc_scale>>1)), s->c_dc_scale); |
| 2935 |
dc_pred_dir= (s->pred_dir_table[s->mb_x + s->mb_y*s->mb_stride]<<n)&32; |
| 2936 |
}else{ |
| 2937 |
level = mpeg4_decode_dc(s, n, &dc_pred_dir); |
| 2938 |
if (level < 0) |
| 3040 |
return -1; |
2939 |
return -1; |
|
|
2940 |
} |
| 2941 |
block[0] = level; |
| 2942 |
i = 0; |
| 2943 |
}else{ |
| 2944 |
i = -1; |
| 2945 |
} |
| 2946 |
if (!coded) |
| 2947 |
goto not_coded; |
| 3041 |
|
2948 |
|
| 3042 |
s->qscale = get_bits(&s->gb, 5); /* SQUANT */ |
2949 |
if(rvlc){ |
| 3043 |
if(get_bits1(&s->gb)==0) |
2950 |
rl = &rvlc_rl_intra; |
| 3044 |
return -1; |
2951 |
rl_vlc = rvlc_rl_intra.rl_vlc[0]; |
| 3045 |
gfid = get_bits(&s->gb, 2); /* GFID */ |
2952 |
}else{ |
| 3046 |
}else{ |
2953 |
rl = &rl_intra; |
| 3047 |
gob_number = get_bits(&s->gb, 5); /* GN */ |
2954 |
rl_vlc = rl_intra.rl_vlc[0]; |
| 3048 |
s->mb_x= 0; |
2955 |
} |
| 3049 |
s->mb_y= s->gob_index* gob_number; |
2956 |
if (s->ac_pred) { |
| 3050 |
gfid = get_bits(&s->gb, 2); /* GFID */ |
2957 |
if (dc_pred_dir == 0) |
| 3051 |
s->qscale = get_bits(&s->gb, 5); /* GQUANT */ |
2958 |
scan_table = s->intra_v_scantable.permutated; /* left */ |
| 3052 |
} |
2959 |
else |
| 3053 |
|
2960 |
scan_table = s->intra_h_scantable.permutated; /* top */ |
| 3054 |
if(s->mb_y >= s->mb_height) |
2961 |
} else { |
| 3055 |
return -1; |
2962 |
scan_table = s->intra_scantable.permutated; |
| 3056 |
|
2963 |
} |
| 3057 |
if(s->qscale==0) |
2964 |
qmul=1; |
| 3058 |
return -1; |
2965 |
qadd=0; |
| 3059 |
|
2966 |
} else { |
| 3060 |
return 0; |
2967 |
i = -1; |
| 3061 |
} |
2968 |
if (!coded) { |
| 3062 |
|
2969 |
s->block_last_index[n] = i; |
| 3063 |
static inline void memsetw(short *tab, int val, int n) |
2970 |
return 0; |
| 3064 |
{ |
2971 |
} |
| 3065 |
int i; |
2972 |
if(rvlc) rl = &rvlc_rl_inter; |
| 3066 |
for(i=0;i<n;i++) |
2973 |
else rl = &rl_inter; |
| 3067 |
tab[i] = val; |
2974 |
|
| 3068 |
} |
2975 |
scan_table = s->intra_scantable.permutated; |
| 3069 |
|
2976 |
|
| 3070 |
#ifdef CONFIG_ENCODERS |
2977 |
if(s->mpeg_quant){ |
|
|
2978 |
qmul=1; |
| 2979 |
qadd=0; |
| 2980 |
if(rvlc){ |
| 2981 |
rl_vlc = rvlc_rl_inter.rl_vlc[0]; |
| 2982 |
}else{ |
| 2983 |
rl_vlc = rl_inter.rl_vlc[0]; |
| 2984 |
} |
| 2985 |
}else{ |
| 2986 |
qmul = s->qscale << 1; |
| 2987 |
qadd = (s->qscale - 1) | 1; |
| 2988 |
if(rvlc){ |
| 2989 |
rl_vlc = rvlc_rl_inter.rl_vlc[s->qscale]; |
| 2990 |
}else{ |
| 2991 |
rl_vlc = rl_inter.rl_vlc[s->qscale]; |
| 2992 |
} |
| 2993 |
} |
| 2994 |
} |
| 2995 |
{ |
| 2996 |
OPEN_READER(re, &s->gb); |
| 2997 |
for(;;) { |
| 2998 |
UPDATE_CACHE(re, &s->gb); |
| 2999 |
GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0); |
| 3000 |
if (level==0) { |
| 3001 |
/* escape */ |
| 3002 |
if(rvlc){ |
| 3003 |
if(SHOW_UBITS(re, &s->gb, 1)==0){ |
| 3004 |
av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in rvlc esc\n"); |
| 3005 |
return -1; |
| 3006 |
}; SKIP_CACHE(re, &s->gb, 1); |
| 3007 |
|
| 3008 |
last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1); |
| 3009 |
run= SHOW_UBITS(re, &s->gb, 6); LAST_SKIP_CACHE(re, &s->gb, 6); |
| 3010 |
SKIP_COUNTER(re, &s->gb, 1+1+6); |
| 3011 |
UPDATE_CACHE(re, &s->gb); |
| 3012 |
|
| 3013 |
if(SHOW_UBITS(re, &s->gb, 1)==0){ |
| 3014 |
av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in rvlc esc\n"); |
| 3015 |
return -1; |
| 3016 |
}; SKIP_CACHE(re, &s->gb, 1); |
| 3017 |
|
| 3018 |
level= SHOW_UBITS(re, &s->gb, 11); SKIP_CACHE(re, &s->gb, 11); |
| 3019 |
|
| 3020 |
if(SHOW_UBITS(re, &s->gb, 5)!=0x10){ |
| 3021 |
av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n"); |
| 3022 |
return -1; |
| 3023 |
}; SKIP_CACHE(re, &s->gb, 5); |
| 3024 |
|
| 3025 |
level= level * qmul + qadd; |
| 3026 |
level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); LAST_SKIP_CACHE(re, &s->gb, 1); |
| 3027 |
SKIP_COUNTER(re, &s->gb, 1+11+5+1); |
| 3028 |
|
| 3029 |
i+= run + 1; |
| 3030 |
if(last) i+=192; |
| 3031 |
}else{ |
| 3032 |
int cache; |
| 3033 |
cache= GET_CACHE(re, &s->gb); |
| 3034 |
|
| 3035 |
if(IS_3IV1) |
| 3036 |
cache ^= 0xC0000000; |
| 3037 |
|
| 3038 |
if (cache&0x80000000) { |
| 3039 |
if (cache&0x40000000) { |
| 3040 |
/* third escape */ |
| 3041 |
SKIP_CACHE(re, &s->gb, 2); |
| 3042 |
last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1); |
| 3043 |
run= SHOW_UBITS(re, &s->gb, 6); LAST_SKIP_CACHE(re, &s->gb, 6); |
| 3044 |
SKIP_COUNTER(re, &s->gb, 2+1+6); |
| 3045 |
UPDATE_CACHE(re, &s->gb); |
| 3046 |
|
| 3047 |
if(IS_3IV1){ |
| 3048 |
level= SHOW_SBITS(re, &s->gb, 12); LAST_SKIP_BITS(re, &s->gb, 12); |
| 3049 |
}else{ |
| 3050 |
if(SHOW_UBITS(re, &s->gb, 1)==0){ |
| 3051 |
av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in 3. esc\n"); |
| 3052 |
return -1; |
| 3053 |
}; SKIP_CACHE(re, &s->gb, 1); |
| 3054 |
|
| 3055 |
level= SHOW_SBITS(re, &s->gb, 12); SKIP_CACHE(re, &s->gb, 12); |
| 3056 |
|
| 3057 |
if(SHOW_UBITS(re, &s->gb, 1)==0){ |
| 3058 |
av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in 3. esc\n"); |
| 3059 |
return -1; |
| 3060 |
}; LAST_SKIP_CACHE(re, &s->gb, 1); |
| 3061 |
|
| 3062 |
SKIP_COUNTER(re, &s->gb, 1+12+1); |
| 3063 |
} |
| 3064 |
|
| 3065 |
#if 0 |
| 3066 |
if(s->error_resilience >= FF_ER_COMPLIANT){ |
| 3067 |
const int abs_level= ABS(level); |
| 3068 |
if(abs_level<=MAX_LEVEL && run<=MAX_RUN){ |
| 3069 |
const int run1= run - rl->max_run[last][abs_level] - 1; |
| 3070 |
if(abs_level <= rl->max_level[last][run]){ |
| 3071 |
av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n"); |
| 3072 |
return -1; |
| 3073 |
} |
| 3074 |
if(s->error_resilience > FF_ER_COMPLIANT){ |
| 3075 |
if(abs_level <= rl->max_level[last][run]*2){ |
| 3076 |
av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n"); |
| 3077 |
return -1; |
| 3078 |
} |
| 3079 |
if(run1 >= 0 && abs_level <= rl->max_level[last][run1]){ |
| 3080 |
av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n"); |
| 3081 |
return -1; |
| 3082 |
} |
| 3083 |
} |
| 3084 |
} |
| 3085 |
} |
| 3086 |
#endif |
| 3087 |
if (level>0) level= level * qmul + qadd; |
| 3088 |
else level= level * qmul - qadd; |
| 3089 |
|
| 3090 |
if((unsigned)(level + 2048) > 4095){ |
| 3091 |
if(s->error_resilience > FF_ER_COMPLIANT){ |
| 3092 |
if(level > 2560 || level<-2560){ |
| 3093 |
av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc, qp=%d\n", s->qscale); |
| 3094 |
return -1; |
| 3095 |
} |
| 3096 |
} |
| 3097 |
level= level<0 ? -2048 : 2047; |
| 3098 |
} |
| 3099 |
|
| 3100 |
i+= run + 1; |
| 3101 |
if(last) i+=192; |
| 3102 |
} else { |
| 3103 |
/* second escape */ |
| 3104 |
#if MIN_CACHE_BITS < 20 |
| 3105 |
LAST_SKIP_BITS(re, &s->gb, 2); |
| 3106 |
UPDATE_CACHE(re, &s->gb); |
| 3107 |
#else |
| 3108 |
SKIP_BITS(re, &s->gb, 2); |
| 3109 |
#endif |
| 3110 |
GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1); |
| 3111 |
i+= run + rl->max_run[run>>7][level/qmul] +1; //FIXME opt indexing |
| 3112 |
level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); |
| 3113 |
LAST_SKIP_BITS(re, &s->gb, 1); |
| 3114 |
} |
| 3115 |
} else { |
| 3116 |
/* first escape */ |
| 3117 |
#if MIN_CACHE_BITS < 19 |
| 3118 |
LAST_SKIP_BITS(re, &s->gb, 1); |
| 3119 |
UPDATE_CACHE(re, &s->gb); |
| 3120 |
#else |
| 3121 |
SKIP_BITS(re, &s->gb, 1); |
| 3122 |
#endif |
| 3123 |
GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1); |
| 3124 |
i+= run; |
| 3125 |
level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing |
| 3126 |
level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); |
| 3127 |
LAST_SKIP_BITS(re, &s->gb, 1); |
| 3128 |
} |
| 3129 |
} |
| 3130 |
} else { |
| 3131 |
i+= run; |
| 3132 |
level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); |
| 3133 |
LAST_SKIP_BITS(re, &s->gb, 1); |
| 3134 |
} |
| 3135 |
if (i > 62){ |
| 3136 |
i-= 192; |
| 3137 |
if(i&(~63)){ |
| 3138 |
av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y); |
| 3139 |
return -1; |
| 3140 |
} |
| 3141 |
|
| 3142 |
block[scan_table[i]] = level; |
| 3143 |
break; |
| 3144 |
} |
| 3145 |
|
| 3146 |
block[scan_table[i]] = level; |
| 3147 |
} |
| 3148 |
CLOSE_READER(re, &s->gb); |
| 3149 |
} |
| 3150 |
not_coded: |
| 3151 |
if (intra) { |
| 3152 |
if(s->qscale >= s->intra_dc_threshold){ |
| 3153 |
block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0); |
| 3154 |
|
| 3155 |
if(i == -1) i=0; |
| 3156 |
} |
| 3157 |
|
| 3158 |
mpeg4_pred_ac(s, block, n, dc_pred_dir); |
| 3159 |
if (s->ac_pred) { |
| 3160 |
i = 63; /* XXX: not optimal */ |
| 3161 |
} |
| 3162 |
} |
| 3163 |
s->block_last_index[n] = i; |
| 3164 |
return 0; |
| 3165 |
} |
| 3166 |
|
| 3167 |
void init_vlc_rl(RLTable *rl, int use_static) |
| 3168 |
{ |
| 3169 |
int i, q; |
| 3170 |
|
| 3171 |
/* Return if static table is already initialized */ |
| 3172 |
if(use_static && rl->rl_vlc[0]) |
| 3173 |
return; |
| 3174 |
|
| 3175 |
init_vlc(&rl->vlc, 9, rl->n + 1, |
| 3176 |
&rl->table_vlc[0][1], 4, 2, |
| 3177 |
&rl->table_vlc[0][0], 4, 2, use_static); |
| 3178 |
|
| 3179 |
|
| 3180 |
for(q=0; q<32; q++){ |
| 3181 |
int qmul= q*2; |
| 3182 |
int qadd= (q-1)|1; |
| 3183 |
|
| 3184 |
if(q==0){ |
| 3185 |
qmul=1; |
| 3186 |
qadd=0; |
| 3187 |
} |
| 3188 |
if(use_static) |
| 3189 |
rl->rl_vlc[q]= av_mallocz_static(rl->vlc.table_size*sizeof(RL_VLC_ELEM)); |
| 3190 |
else |
| 3191 |
rl->rl_vlc[q]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM)); |
| 3192 |
for(i=0; i<rl->vlc.table_size; i++){ |
| 3193 |
int code= rl->vlc.table[i][0]; |
| 3194 |
int len = rl->vlc.table[i][1]; |
| 3195 |
int level, run; |
| 3196 |
|
| 3197 |
if(len==0){ // illegal code |
| 3198 |
run= 66; |
| 3199 |
level= MAX_LEVEL; |
| 3200 |
}else if(len<0){ //more bits needed |
| 3201 |
run= 0; |
| 3202 |
level= code; |
| 3203 |
}else{ |
| 3204 |
if(code==rl->n){ //esc |
| 3205 |
run= 66; |
| 3206 |
level= 0; |
| 3207 |
}else{ |
| 3208 |
run= rl->table_run [code] + 1; |
| 3209 |
level= rl->table_level[code] * qmul + qadd; |
| 3210 |
if(code >= rl->last) run+=192; |
| 3211 |
} |
| 3212 |
} |
| 3213 |
rl->rl_vlc[q][i].len= len; |
| 3214 |
rl->rl_vlc[q][i].level= level; |
| 3215 |
rl->rl_vlc[q][i].run= run; |
| 3216 |
} |
| 3217 |
} |
| 3218 |
} |
| 3219 |
|
| 3220 |
/* init vlcs */ |
| 3221 |
|
| 3222 |
/* XXX: find a better solution to handle static init */ |
| 3223 |
void h263_decode_init_vlc(MpegEncContext *s) |
| 3224 |
{ |
| 3225 |
static int done = 0; |
| 3226 |
|
| 3227 |
if (!done) { |
| 3228 |
done = 1; |
| 3229 |
|
| 3230 |
init_vlc(&intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 9, |
| 3231 |
intra_MCBPC_bits, 1, 1, |
| 3232 |
intra_MCBPC_code, 1, 1, 1); |
| 3233 |
init_vlc(&inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 28, |
| 3234 |
inter_MCBPC_bits, 1, 1, |
| 3235 |
inter_MCBPC_code, 1, 1, 1); |
| 3236 |
init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16, |
| 3237 |
&cbpy_tab[0][1], 2, 1, |
| 3238 |
&cbpy_tab[0][0], 2, 1, 1); |
| 3239 |
init_vlc(&mv_vlc, MV_VLC_BITS, 33, |
| 3240 |
&mvtab[0][1], 2, 1, |
| 3241 |
&mvtab[0][0], 2, 1, 1); |
| 3242 |
init_rl(&rl_inter, 1); |
| 3243 |
init_rl(&rl_intra, 1); |
| 3244 |
init_rl(&rvlc_rl_inter, 1); |
| 3245 |
init_rl(&rvlc_rl_intra, 1); |
| 3246 |
init_rl(&rl_intra_aic, 1); |
| 3247 |
init_vlc_rl(&rl_inter, 1); |
| 3248 |
init_vlc_rl(&rl_intra, 1); |
| 3249 |
init_vlc_rl(&rvlc_rl_inter, 1); |
| 3250 |
init_vlc_rl(&rvlc_rl_intra, 1); |
| 3251 |
init_vlc_rl(&rl_intra_aic, 1); |
| 3252 |
init_vlc(&dc_lum, DC_VLC_BITS, 10 /* 13 */, |
| 3253 |
&DCtab_lum[0][1], 2, 1, |
| 3254 |
&DCtab_lum[0][0], 2, 1, 1); |
| 3255 |
init_vlc(&dc_chrom, DC_VLC_BITS, 10 /* 13 */, |
| 3256 |
&DCtab_chrom[0][1], 2, 1, |
| 3257 |
&DCtab_chrom[0][0], 2, 1, 1); |
| 3258 |
init_vlc(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15, |
| 3259 |
&sprite_trajectory_tab[0][1], 4, 2, |
| 3260 |
&sprite_trajectory_tab[0][0], 4, 2, 1); |
| 3261 |
init_vlc(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4, |
| 3262 |
&mb_type_b_tab[0][1], 2, 1, |
| 3263 |
&mb_type_b_tab[0][0], 2, 1, 1); |
| 3264 |
init_vlc(&h263_mbtype_b_vlc, H263_MBTYPE_B_VLC_BITS, 15, |
| 3265 |
&h263_mbtype_b_tab[0][1], 2, 1, |
| 3266 |
&h263_mbtype_b_tab[0][0], 2, 1, 1); |
| 3267 |
init_vlc(&cbpc_b_vlc, CBPC_B_VLC_BITS, 4, |
| 3268 |
&cbpc_b_tab[0][1], 2, 1, |
| 3269 |
&cbpc_b_tab[0][0], 2, 1, 1); |
| 3270 |
} |
| 3271 |
} |
| 3272 |
|
| 3273 |
/** |
| 3274 |
* Get the GOB height based on picture height. |
| 3275 |
*/ |
| 3276 |
int ff_h263_get_gob_height(MpegEncContext *s){ |
| 3277 |
if (s->height <= 400) |
| 3278 |
return 1; |
| 3279 |
else if (s->height <= 800) |
| 3280 |
return 2; |
| 3281 |
else |
| 3282 |
return 4; |
| 3283 |
} |
| 3284 |
|
| 3285 |
int ff_h263_decode_mba(MpegEncContext *s) |
| 3286 |
{ |
| 3287 |
int i, mb_pos; |
| 3288 |
|
| 3289 |
for(i=0; i<6; i++){ |
| 3290 |
if(s->mb_num-1 <= ff_mba_max[i]) break; |
| 3291 |
} |
| 3292 |
mb_pos= get_bits(&s->gb, ff_mba_length[i]); |
| 3293 |
s->mb_x= mb_pos % s->mb_width; |
| 3294 |
s->mb_y= mb_pos / s->mb_width; |
| 3295 |
|
| 3296 |
return mb_pos; |
| 3297 |
} |
| 3298 |
|
| 3299 |
void ff_h263_encode_mba(MpegEncContext *s) |
| 3300 |
{ |
| 3301 |
int i, mb_pos; |
| 3302 |
|
| 3303 |
for(i=0; i<6; i++){ |
| 3304 |
if(s->mb_num-1 <= ff_mba_max[i]) break; |
| 3305 |
} |
| 3306 |
mb_pos= s->mb_x + s->mb_width*s->mb_y; |
| 3307 |
put_bits(&s->pb, ff_mba_length[i], mb_pos); |
| 3308 |
} |
| 3309 |
|
| 3310 |
/** |
| 3311 |
* decodes the group of blocks header or slice header. |
| 3312 |
* @return <0 if an error occured |
| 3313 |
*/ |
| 3314 |
static int h263_decode_gob_header(MpegEncContext *s) |
| 3315 |
{ |
| 3316 |
unsigned int val, gfid, gob_number; |
| 3317 |
int left; |
| 3318 |
|
| 3319 |
/* Check for GOB Start Code */ |
| 3320 |
val = show_bits(&s->gb, 16); |
| 3321 |
if(val) |
| 3322 |
return -1; |
| 3323 |
|
| 3324 |
/* We have a GBSC probably with GSTUFF */ |
| 3325 |
skip_bits(&s->gb, 16); /* Drop the zeros */ |
| 3326 |
left= s->gb.size_in_bits - get_bits_count(&s->gb); |
| 3327 |
//MN: we must check the bits left or we might end in a infinite loop (or segfault) |
| 3328 |
for(;left>13; left--){ |
| 3329 |
if(get_bits1(&s->gb)) break; /* Seek the '1' bit */ |
| 3330 |
} |
| 3331 |
if(left<=13) |
| 3332 |
return -1; |
| 3333 |
|
| 3334 |
if(s->h263_slice_structured){ |
| 3335 |
if(get_bits1(&s->gb)==0) |
| 3336 |
return -1; |
| 3337 |
|
| 3338 |
ff_h263_decode_mba(s); |
| 3339 |
|
| 3340 |
if(s->mb_num > 1583) |
| 3341 |
if(get_bits1(&s->gb)==0) |
| 3342 |
return -1; |
| 3343 |
|
| 3344 |
s->qscale = get_bits(&s->gb, 5); /* SQUANT */ |
| 3345 |
if(get_bits1(&s->gb)==0) |
| 3346 |
return -1; |
| 3347 |
gfid = get_bits(&s->gb, 2); /* GFID */ |
| 3348 |
}else{ |
| 3349 |
gob_number = get_bits(&s->gb, 5); /* GN */ |
| 3350 |
s->mb_x= 0; |
| 3351 |
s->mb_y= s->gob_index* gob_number; |
| 3352 |
gfid = get_bits(&s->gb, 2); /* GFID */ |
| 3353 |
s->qscale = get_bits(&s->gb, 5); /* GQUANT */ |
| 3354 |
} |
| 3355 |
|
| 3356 |
if(s->mb_y >= s->mb_height) |
| 3357 |
return -1; |
| 3358 |
|
| 3359 |
if(s->qscale==0) |
| 3360 |
return -1; |
| 3361 |
|
| 3362 |
return 0; |
| 3363 |
} |
| 3364 |
|
| 3365 |
static inline void memsetw(short *tab, int val, int n) |
| 3366 |
{ |
| 3367 |
int i; |
| 3368 |
for(i=0;i<n;i++) |
| 3369 |
tab[i] = val; |
| 3370 |
} |
| 3371 |
|
| 3372 |
#ifdef CONFIG_ENCODERS |
| 3071 |
|
3373 |
|
| 3072 |
void ff_mpeg4_init_partitions(MpegEncContext *s) |
3374 |
void ff_mpeg4_init_partitions(MpegEncContext *s) |
| 3073 |
{ |
3375 |
{ |
|
Lines 4563-4978
Link Here
|
| 4563 |
scan_table = s->intra_v_scantable.permutated; /* left */ |
4865 |
scan_table = s->intra_v_scantable.permutated; /* left */ |
| 4564 |
else |
4866 |
else |
| 4565 |
scan_table = s->intra_h_scantable.permutated; /* top */ |
4867 |
scan_table = s->intra_h_scantable.permutated; /* top */ |
| 4566 |
} |
4868 |
} |
| 4567 |
} else if (s->mb_intra) { |
4869 |
} else if (s->mb_intra) { |
| 4568 |
/* DC coef */ |
4870 |
/* DC coef */ |
| 4569 |
if(s->codec_id == CODEC_ID_RV10){ |
4871 |
if(s->codec_id == CODEC_ID_RV10){ |
| 4570 |
#ifdef CONFIG_RV10_DECODER |
4872 |
#ifdef CONFIG_RV10_DECODER |
| 4571 |
if (s->rv10_version == 3 && s->pict_type == I_TYPE) { |
4873 |
if (s->rv10_version == 3 && s->pict_type == I_TYPE) { |
| 4572 |
int component, diff; |
4874 |
int component, diff; |
| 4573 |
component = (n <= 3 ? 0 : n - 4 + 1); |
4875 |
component = (n <= 3 ? 0 : n - 4 + 1); |
| 4574 |
level = s->last_dc[component]; |
4876 |
level = s->last_dc[component]; |
| 4575 |
if (s->rv10_first_dc_coded[component]) { |
4877 |
if (s->rv10_first_dc_coded[component]) { |
| 4576 |
diff = rv_decode_dc(s, n); |
4878 |
diff = rv_decode_dc(s, n); |
| 4577 |
if (diff == 0xffff) |
4879 |
if (diff == 0xffff) |
| 4578 |
return -1; |
4880 |
return -1; |
| 4579 |
level += diff; |
4881 |
level += diff; |
| 4580 |
level = level & 0xff; /* handle wrap round */ |
4882 |
level = level & 0xff; /* handle wrap round */ |
| 4581 |
s->last_dc[component] = level; |
4883 |
s->last_dc[component] = level; |
| 4582 |
} else { |
4884 |
} else { |
| 4583 |
s->rv10_first_dc_coded[component] = 1; |
4885 |
s->rv10_first_dc_coded[component] = 1; |
| 4584 |
} |
|
|
| 4585 |
} else { |
| 4586 |
level = get_bits(&s->gb, 8); |
| 4587 |
if (level == 255) |
| 4588 |
level = 128; |
| 4589 |
} |
| 4590 |
#endif |
| 4591 |
}else{ |
| 4592 |
level = get_bits(&s->gb, 8); |
| 4593 |
if((level&0x7F) == 0){ |
| 4594 |
av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y); |
| 4595 |
if(s->error_resilience >= FF_ER_COMPLIANT) |
| 4596 |
return -1; |
| 4597 |
} |
| 4598 |
if (level == 255) |
| 4599 |
level = 128; |
| 4600 |
} |
| 4601 |
block[0] = level; |
| 4602 |
i = 1; |
| 4603 |
} else { |
| 4604 |
i = 0; |
| 4605 |
} |
| 4606 |
if (!coded) { |
| 4607 |
if (s->mb_intra && s->h263_aic) |
| 4608 |
goto not_coded; |
| 4609 |
s->block_last_index[n] = i - 1; |
| 4610 |
return 0; |
| 4611 |
} |
| 4612 |
retry: |
| 4613 |
for(;;) { |
| 4614 |
code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2); |
| 4615 |
if (code < 0){ |
| 4616 |
av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y); |
| 4617 |
return -1; |
| 4618 |
} |
| 4619 |
if (code == rl->n) { |
| 4620 |
/* escape */ |
| 4621 |
if (s->h263_flv > 1) { |
| 4622 |
int is11 = get_bits1(&s->gb); |
| 4623 |
last = get_bits1(&s->gb); |
| 4624 |
run = get_bits(&s->gb, 6); |
| 4625 |
if(is11){ |
| 4626 |
level = get_sbits(&s->gb, 11); |
| 4627 |
} else { |
| 4628 |
level = get_sbits(&s->gb, 7); |
| 4629 |
} |
| 4630 |
} else { |
| 4631 |
last = get_bits1(&s->gb); |
| 4632 |
run = get_bits(&s->gb, 6); |
| 4633 |
level = (int8_t)get_bits(&s->gb, 8); |
| 4634 |
if(level == -128){ |
| 4635 |
if (s->codec_id == CODEC_ID_RV10) { |
| 4636 |
/* XXX: should patch encoder too */ |
| 4637 |
level = get_sbits(&s->gb, 12); |
| 4638 |
}else{ |
| 4639 |
level = get_bits(&s->gb, 5); |
| 4640 |
level |= get_sbits(&s->gb, 6)<<5; |
| 4641 |
} |
| 4642 |
} |
| 4643 |
} |
| 4644 |
} else { |
| 4645 |
run = rl->table_run[code]; |
| 4646 |
level = rl->table_level[code]; |
| 4647 |
last = code >= rl->last; |
| 4648 |
if (get_bits1(&s->gb)) |
| 4649 |
level = -level; |
| 4650 |
} |
| 4651 |
i += run; |
| 4652 |
if (i >= 64){ |
| 4653 |
if(s->alt_inter_vlc && rl == &rl_inter && !s->mb_intra){ |
| 4654 |
//looks like a hack but no, it's the way its supposed to work ... |
| 4655 |
rl = &rl_intra_aic; |
| 4656 |
i = 0; |
| 4657 |
s->gb= gb; |
| 4658 |
memset(block, 0, sizeof(DCTELEM)*64); |
| 4659 |
goto retry; |
| 4660 |
} |
| 4661 |
av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d i:%d\n", s->mb_x, s->mb_y, s->mb_intra); |
| 4662 |
return -1; |
| 4663 |
} |
| 4664 |
j = scan_table[i]; |
| 4665 |
block[j] = level; |
| 4666 |
if (last) |
| 4667 |
break; |
| 4668 |
i++; |
| 4669 |
} |
| 4670 |
not_coded: |
| 4671 |
if (s->mb_intra && s->h263_aic) { |
| 4672 |
h263_pred_acdc(s, block, n); |
| 4673 |
i = 63; |
| 4674 |
} |
| 4675 |
s->block_last_index[n] = i; |
| 4676 |
return 0; |
| 4677 |
} |
| 4678 |
|
| 4679 |
/** |
| 4680 |
* decodes the dc value. |
| 4681 |
* @param n block index (0-3 are luma, 4-5 are chroma) |
| 4682 |
* @param dir_ptr the prediction direction will be stored here |
| 4683 |
* @return the quantized dc |
| 4684 |
*/ |
| 4685 |
static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr) |
| 4686 |
{ |
| 4687 |
int level, code; |
| 4688 |
|
| 4689 |
if (n < 4) |
| 4690 |
code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1); |
| 4691 |
else |
| 4692 |
code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1); |
| 4693 |
if (code < 0 || code > 9 /* && s->nbit<9 */){ |
| 4694 |
av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n"); |
| 4695 |
return -1; |
| 4696 |
} |
| 4697 |
if (code == 0) { |
| 4698 |
level = 0; |
| 4699 |
} else { |
| 4700 |
if(IS_3IV1){ |
| 4701 |
if(code==1) |
| 4702 |
level= 2*get_bits1(&s->gb)-1; |
| 4703 |
else{ |
| 4704 |
if(get_bits1(&s->gb)) |
| 4705 |
level = get_bits(&s->gb, code-1) + (1<<(code-1)); |
| 4706 |
else |
| 4707 |
level = -get_bits(&s->gb, code-1) - (1<<(code-1)); |
| 4708 |
} |
| 4709 |
}else{ |
| 4710 |
level = get_xbits(&s->gb, code); |
| 4711 |
} |
| 4712 |
|
| 4713 |
if (code > 8){ |
| 4714 |
if(get_bits1(&s->gb)==0){ /* marker */ |
| 4715 |
if(s->error_resilience>=2){ |
| 4716 |
av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n"); |
| 4717 |
return -1; |
| 4718 |
} |
| 4719 |
} |
| 4720 |
} |
| 4721 |
} |
| 4722 |
|
| 4723 |
return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0); |
| 4724 |
} |
| 4725 |
|
| 4726 |
/** |
| 4727 |
* decodes a block. |
| 4728 |
* @return <0 if an error occured |
| 4729 |
*/ |
| 4730 |
static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block, |
| 4731 |
int n, int coded, int intra, int rvlc) |
| 4732 |
{ |
| 4733 |
int level, i, last, run; |
| 4734 |
int dc_pred_dir; |
| 4735 |
RLTable * rl; |
| 4736 |
RL_VLC_ELEM * rl_vlc; |
| 4737 |
const uint8_t * scan_table; |
| 4738 |
int qmul, qadd; |
| 4739 |
|
| 4740 |
//Note intra & rvlc should be optimized away if this is inlined |
| 4741 |
|
| 4742 |
if(intra) { |
| 4743 |
if(s->qscale < s->intra_dc_threshold){ |
| 4744 |
/* DC coef */ |
| 4745 |
if(s->partitioned_frame){ |
| 4746 |
level = s->dc_val[0][ s->block_index[n] ]; |
| 4747 |
if(n<4) level= FASTDIV((level + (s->y_dc_scale>>1)), s->y_dc_scale); |
| 4748 |
else level= FASTDIV((level + (s->c_dc_scale>>1)), s->c_dc_scale); |
| 4749 |
dc_pred_dir= (s->pred_dir_table[s->mb_x + s->mb_y*s->mb_stride]<<n)&32; |
| 4750 |
}else{ |
| 4751 |
level = mpeg4_decode_dc(s, n, &dc_pred_dir); |
| 4752 |
if (level < 0) |
| 4753 |
return -1; |
| 4754 |
} |
| 4755 |
block[0] = level; |
| 4756 |
i = 0; |
| 4757 |
}else{ |
| 4758 |
i = -1; |
| 4759 |
} |
| 4760 |
if (!coded) |
| 4761 |
goto not_coded; |
| 4762 |
|
| 4763 |
if(rvlc){ |
| 4764 |
rl = &rvlc_rl_intra; |
| 4765 |
rl_vlc = rvlc_rl_intra.rl_vlc[0]; |
| 4766 |
}else{ |
| 4767 |
rl = &rl_intra; |
| 4768 |
rl_vlc = rl_intra.rl_vlc[0]; |
| 4769 |
} |
| 4770 |
if (s->ac_pred) { |
| 4771 |
if (dc_pred_dir == 0) |
| 4772 |
scan_table = s->intra_v_scantable.permutated; /* left */ |
| 4773 |
else |
| 4774 |
scan_table = s->intra_h_scantable.permutated; /* top */ |
| 4775 |
} else { |
| 4776 |
scan_table = s->intra_scantable.permutated; |
| 4777 |
} |
| 4778 |
qmul=1; |
| 4779 |
qadd=0; |
| 4780 |
} else { |
| 4781 |
i = -1; |
| 4782 |
if (!coded) { |
| 4783 |
s->block_last_index[n] = i; |
| 4784 |
return 0; |
| 4785 |
} |
| 4786 |
if(rvlc) rl = &rvlc_rl_inter; |
| 4787 |
else rl = &rl_inter; |
| 4788 |
|
| 4789 |
scan_table = s->intra_scantable.permutated; |
| 4790 |
|
| 4791 |
if(s->mpeg_quant){ |
| 4792 |
qmul=1; |
| 4793 |
qadd=0; |
| 4794 |
if(rvlc){ |
| 4795 |
rl_vlc = rvlc_rl_inter.rl_vlc[0]; |
| 4796 |
}else{ |
| 4797 |
rl_vlc = rl_inter.rl_vlc[0]; |
| 4798 |
} |
4886 |
} |
|
|
4887 |
} else { |
| 4888 |
level = get_bits(&s->gb, 8); |
| 4889 |
if (level == 255) |
| 4890 |
level = 128; |
| 4891 |
} |
| 4892 |
#endif |
| 4799 |
}else{ |
4893 |
}else{ |
| 4800 |
qmul = s->qscale << 1; |
4894 |
level = get_bits(&s->gb, 8); |
| 4801 |
qadd = (s->qscale - 1) | 1; |
4895 |
if((level&0x7F) == 0){ |
| 4802 |
if(rvlc){ |
4896 |
av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y); |
| 4803 |
rl_vlc = rvlc_rl_inter.rl_vlc[s->qscale]; |
4897 |
if(s->error_resilience >= FF_ER_COMPLIANT) |
| 4804 |
}else{ |
4898 |
return -1; |
| 4805 |
rl_vlc = rl_inter.rl_vlc[s->qscale]; |
|
|
| 4806 |
} |
4899 |
} |
|
|
4900 |
if (level == 255) |
| 4901 |
level = 128; |
| 4807 |
} |
4902 |
} |
|
|
4903 |
block[0] = level; |
| 4904 |
i = 1; |
| 4905 |
} else { |
| 4906 |
i = 0; |
| 4808 |
} |
4907 |
} |
| 4809 |
{ |
4908 |
if (!coded) { |
| 4810 |
OPEN_READER(re, &s->gb); |
4909 |
if (s->mb_intra && s->h263_aic) |
|
|
4910 |
goto not_coded; |
| 4911 |
s->block_last_index[n] = i - 1; |
| 4912 |
return 0; |
| 4913 |
} |
| 4914 |
retry: |
| 4811 |
for(;;) { |
4915 |
for(;;) { |
| 4812 |
UPDATE_CACHE(re, &s->gb); |
4916 |
code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2); |
| 4813 |
GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0); |
4917 |
if (code < 0){ |
| 4814 |
if (level==0) { |
4918 |
av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y); |
| 4815 |
/* escape */ |
4919 |
return -1; |
| 4816 |
if(rvlc){ |
4920 |
} |
| 4817 |
if(SHOW_UBITS(re, &s->gb, 1)==0){ |
4921 |
if (code == rl->n) { |
| 4818 |
av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in rvlc esc\n"); |
4922 |
/* escape */ |
| 4819 |
return -1; |
4923 |
if (s->h263_flv > 1) { |
| 4820 |
}; SKIP_CACHE(re, &s->gb, 1); |
4924 |
int is11 = get_bits1(&s->gb); |
| 4821 |
|
4925 |
last = get_bits1(&s->gb); |
| 4822 |
last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1); |
4926 |
run = get_bits(&s->gb, 6); |
| 4823 |
run= SHOW_UBITS(re, &s->gb, 6); LAST_SKIP_CACHE(re, &s->gb, 6); |
4927 |
if(is11){ |
| 4824 |
SKIP_COUNTER(re, &s->gb, 1+1+6); |
4928 |
level = get_sbits(&s->gb, 11); |
| 4825 |
UPDATE_CACHE(re, &s->gb); |
|
|
| 4826 |
|
| 4827 |
if(SHOW_UBITS(re, &s->gb, 1)==0){ |
| 4828 |
av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in rvlc esc\n"); |
| 4829 |
return -1; |
| 4830 |
}; SKIP_CACHE(re, &s->gb, 1); |
| 4831 |
|
| 4832 |
level= SHOW_UBITS(re, &s->gb, 11); SKIP_CACHE(re, &s->gb, 11); |
| 4833 |
|
| 4834 |
if(SHOW_UBITS(re, &s->gb, 5)!=0x10){ |
| 4835 |
av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n"); |
| 4836 |
return -1; |
| 4837 |
}; SKIP_CACHE(re, &s->gb, 5); |
| 4838 |
|
| 4839 |
level= level * qmul + qadd; |
| 4840 |
level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); LAST_SKIP_CACHE(re, &s->gb, 1); |
| 4841 |
SKIP_COUNTER(re, &s->gb, 1+11+5+1); |
| 4842 |
|
| 4843 |
i+= run + 1; |
| 4844 |
if(last) i+=192; |
| 4845 |
}else{ |
| 4846 |
int cache; |
| 4847 |
cache= GET_CACHE(re, &s->gb); |
| 4848 |
|
| 4849 |
if(IS_3IV1) |
| 4850 |
cache ^= 0xC0000000; |
| 4851 |
|
| 4852 |
if (cache&0x80000000) { |
| 4853 |
if (cache&0x40000000) { |
| 4854 |
/* third escape */ |
| 4855 |
SKIP_CACHE(re, &s->gb, 2); |
| 4856 |
last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1); |
| 4857 |
run= SHOW_UBITS(re, &s->gb, 6); LAST_SKIP_CACHE(re, &s->gb, 6); |
| 4858 |
SKIP_COUNTER(re, &s->gb, 2+1+6); |
| 4859 |
UPDATE_CACHE(re, &s->gb); |
| 4860 |
|
| 4861 |
if(IS_3IV1){ |
| 4862 |
level= SHOW_SBITS(re, &s->gb, 12); LAST_SKIP_BITS(re, &s->gb, 12); |
| 4863 |
}else{ |
| 4864 |
if(SHOW_UBITS(re, &s->gb, 1)==0){ |
| 4865 |
av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in 3. esc\n"); |
| 4866 |
return -1; |
| 4867 |
}; SKIP_CACHE(re, &s->gb, 1); |
| 4868 |
|
| 4869 |
level= SHOW_SBITS(re, &s->gb, 12); SKIP_CACHE(re, &s->gb, 12); |
| 4870 |
|
| 4871 |
if(SHOW_UBITS(re, &s->gb, 1)==0){ |
| 4872 |
av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in 3. esc\n"); |
| 4873 |
return -1; |
| 4874 |
}; LAST_SKIP_CACHE(re, &s->gb, 1); |
| 4875 |
|
| 4876 |
SKIP_COUNTER(re, &s->gb, 1+12+1); |
| 4877 |
} |
| 4878 |
|
| 4879 |
#if 0 |
| 4880 |
if(s->error_resilience >= FF_ER_COMPLIANT){ |
| 4881 |
const int abs_level= ABS(level); |
| 4882 |
if(abs_level<=MAX_LEVEL && run<=MAX_RUN){ |
| 4883 |
const int run1= run - rl->max_run[last][abs_level] - 1; |
| 4884 |
if(abs_level <= rl->max_level[last][run]){ |
| 4885 |
av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n"); |
| 4886 |
return -1; |
| 4887 |
} |
| 4888 |
if(s->error_resilience > FF_ER_COMPLIANT){ |
| 4889 |
if(abs_level <= rl->max_level[last][run]*2){ |
| 4890 |
av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n"); |
| 4891 |
return -1; |
| 4892 |
} |
| 4893 |
if(run1 >= 0 && abs_level <= rl->max_level[last][run1]){ |
| 4894 |
av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n"); |
| 4895 |
return -1; |
| 4896 |
} |
| 4897 |
} |
| 4898 |
} |
| 4899 |
} |
| 4900 |
#endif |
| 4901 |
if (level>0) level= level * qmul + qadd; |
| 4902 |
else level= level * qmul - qadd; |
| 4903 |
|
| 4904 |
if((unsigned)(level + 2048) > 4095){ |
| 4905 |
if(s->error_resilience > FF_ER_COMPLIANT){ |
| 4906 |
if(level > 2560 || level<-2560){ |
| 4907 |
av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc, qp=%d\n", s->qscale); |
| 4908 |
return -1; |
| 4909 |
} |
| 4910 |
} |
| 4911 |
level= level<0 ? -2048 : 2047; |
| 4912 |
} |
| 4913 |
|
| 4914 |
i+= run + 1; |
| 4915 |
if(last) i+=192; |
| 4916 |
} else { |
4929 |
} else { |
| 4917 |
/* second escape */ |
4930 |
level = get_sbits(&s->gb, 7); |
| 4918 |
#if MIN_CACHE_BITS < 20 |
|
|
| 4919 |
LAST_SKIP_BITS(re, &s->gb, 2); |
| 4920 |
UPDATE_CACHE(re, &s->gb); |
| 4921 |
#else |
| 4922 |
SKIP_BITS(re, &s->gb, 2); |
| 4923 |
#endif |
| 4924 |
GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1); |
| 4925 |
i+= run + rl->max_run[run>>7][level/qmul] +1; //FIXME opt indexing |
| 4926 |
level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); |
| 4927 |
LAST_SKIP_BITS(re, &s->gb, 1); |
| 4928 |
} |
4931 |
} |
| 4929 |
} else { |
4932 |
} else { |
| 4930 |
/* first escape */ |
4933 |
last = get_bits1(&s->gb); |
| 4931 |
#if MIN_CACHE_BITS < 19 |
4934 |
run = get_bits(&s->gb, 6); |
| 4932 |
LAST_SKIP_BITS(re, &s->gb, 1); |
4935 |
level = (int8_t)get_bits(&s->gb, 8); |
| 4933 |
UPDATE_CACHE(re, &s->gb); |
4936 |
if(level == -128){ |
| 4934 |
#else |
4937 |
if (s->codec_id == CODEC_ID_RV10) { |
| 4935 |
SKIP_BITS(re, &s->gb, 1); |
4938 |
/* XXX: should patch encoder too */ |
| 4936 |
#endif |
4939 |
level = get_sbits(&s->gb, 12); |
| 4937 |
GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1); |
4940 |
}else{ |
| 4938 |
i+= run; |
4941 |
level = get_bits(&s->gb, 5); |
| 4939 |
level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing |
4942 |
level |= get_sbits(&s->gb, 6)<<5; |
| 4940 |
level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); |
4943 |
} |
| 4941 |
LAST_SKIP_BITS(re, &s->gb, 1); |
4944 |
} |
| 4942 |
} |
4945 |
} |
| 4943 |
} |
|
|
| 4944 |
} else { |
4946 |
} else { |
| 4945 |
i+= run; |
4947 |
run = rl->table_run[code]; |
| 4946 |
level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); |
4948 |
level = rl->table_level[code]; |
| 4947 |
LAST_SKIP_BITS(re, &s->gb, 1); |
4949 |
last = code >= rl->last; |
|
|
4950 |
if (get_bits1(&s->gb)) |
| 4951 |
level = -level; |
| 4948 |
} |
4952 |
} |
| 4949 |
if (i > 62){ |
4953 |
i += run; |
| 4950 |
i-= 192; |
4954 |
if (i >= 64){ |
| 4951 |
if(i&(~63)){ |
4955 |
if(s->alt_inter_vlc && rl == &rl_inter && !s->mb_intra){ |
| 4952 |
av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y); |
4956 |
//looks like a hack but no, it's the way its supposed to work ... |
| 4953 |
return -1; |
4957 |
rl = &rl_intra_aic; |
|
|
4958 |
i = 0; |
| 4959 |
s->gb= gb; |
| 4960 |
memset(block, 0, sizeof(DCTELEM)*64); |
| 4961 |
goto retry; |
| 4954 |
} |
4962 |
} |
| 4955 |
|
4963 |
av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d i:%d\n", s->mb_x, s->mb_y, s->mb_intra); |
| 4956 |
block[scan_table[i]] = level; |
4964 |
return -1; |
| 4957 |
break; |
|
|
| 4958 |
} |
4965 |
} |
| 4959 |
|
4966 |
j = scan_table[i]; |
| 4960 |
block[scan_table[i]] = level; |
4967 |
block[j] = level; |
|
|
4968 |
if (last) |
| 4969 |
break; |
| 4970 |
i++; |
| 4961 |
} |
4971 |
} |
| 4962 |
CLOSE_READER(re, &s->gb); |
4972 |
not_coded: |
| 4963 |
} |
4973 |
if (s->mb_intra && s->h263_aic) { |
| 4964 |
not_coded: |
4974 |
h263_pred_acdc(s, block, n); |
| 4965 |
if (intra) { |
4975 |
i = 63; |
| 4966 |
if(s->qscale >= s->intra_dc_threshold){ |
|
|
| 4967 |
block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0); |
| 4968 |
|
| 4969 |
if(i == -1) i=0; |
| 4970 |
} |
| 4971 |
|
| 4972 |
mpeg4_pred_ac(s, block, n, dc_pred_dir); |
| 4973 |
if (s->ac_pred) { |
| 4974 |
i = 63; /* XXX: not optimal */ |
| 4975 |
} |
| 4976 |
} |
4976 |
} |
| 4977 |
s->block_last_index[n] = i; |
4977 |
s->block_last_index[n] = i; |
| 4978 |
return 0; |
4978 |
return 0; |