comparison h264.c @ 1908:e20fd60b215c libavcodec

h264 - progressive I frame CABAC support patch by (Laurent Aimar <fenrir at via dot ecp dot fr>)
author michael
date Fri, 26 Mar 2004 19:35:53 +0000
parents 379a18a7131f
children 559e92baf533
comparison
equal deleted inserted replaced
1907:7ff22288289e 1908:e20fd60b215c
29 #include "avcodec.h" 29 #include "avcodec.h"
30 #include "mpegvideo.h" 30 #include "mpegvideo.h"
31 #include "h264data.h" 31 #include "h264data.h"
32 #include "golomb.h" 32 #include "golomb.h"
33 33
34 #include "cabac.h"
35
34 #undef NDEBUG 36 #undef NDEBUG
35 #include <assert.h> 37 #include <assert.h>
36 38
37 #define interlaced_dct interlaced_dct_is_a_bad_name 39 #define interlaced_dct interlaced_dct_is_a_bad_name
38 #define mb_intra mb_intra_isnt_initalized_see_mb_type 40 #define mb_intra mb_intra_isnt_initalized_see_mb_type
282 GetBitContext inter_gb; 284 GetBitContext inter_gb;
283 GetBitContext *intra_gb_ptr; 285 GetBitContext *intra_gb_ptr;
284 GetBitContext *inter_gb_ptr; 286 GetBitContext *inter_gb_ptr;
285 287
286 DCTELEM mb[16*24] __align8; 288 DCTELEM mb[16*24] __align8;
289
290 /**
291 * Cabac
292 */
293 CABACContext cabac;
294 uint8_t cabac_state[399];
295 int cabac_init_idc;
296
297 /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
298 uint16_t *cbp_table;
299 uint8_t *chroma_pred_mode_table;
300 int last_qscale_diff;
301
287 }H264Context; 302 }H264Context;
288 303
289 static VLC coeff_token_vlc[4]; 304 static VLC coeff_token_vlc[4];
290 static VLC chroma_dc_coeff_token_vlc; 305 static VLC chroma_dc_coeff_token_vlc;
291 306
1002 *consumed= si + 1;//+1 for the header 1017 *consumed= si + 1;//+1 for the header
1003 //FIXME store exact number of bits in the getbitcontext (its needed for decoding) 1018 //FIXME store exact number of bits in the getbitcontext (its needed for decoding)
1004 return dst; 1019 return dst;
1005 } 1020 }
1006 1021
1022 #if 0
1007 /** 1023 /**
1008 * @param src the data which should be escaped 1024 * @param src the data which should be escaped
1009 * @param dst the target buffer, dst+1 == src is allowed as a special case 1025 * @param dst the target buffer, dst+1 == src is allowed as a special case
1010 * @param length the length of the src data 1026 * @param length the length of the src data
1011 * @param dst_length the length of the dst array 1027 * @param dst_length the length of the dst array
1074 int length; 1090 int length;
1075 put_bits(pb, 1, 1); 1091 put_bits(pb, 1, 1);
1076 length= (-put_bits_count(pb))&7; 1092 length= (-put_bits_count(pb))&7;
1077 if(length) put_bits(pb, length, 0); 1093 if(length) put_bits(pb, length, 0);
1078 } 1094 }
1095 #endif
1079 1096
1080 /** 1097 /**
1081 * identifies the exact end of the bitstream 1098 * identifies the exact end of the bitstream
1082 * @return the length of the trailing, or 0 if damaged 1099 * @return the length of the trailing, or 0 if damaged
1083 */ 1100 */
1133 block[stride*8 +offset]= ((z1 - z2)*qmul + 2)>>2; 1150 block[stride*8 +offset]= ((z1 - z2)*qmul + 2)>>2;
1134 block[stride*10+offset]= ((z0 - z3)*qmul + 2)>>2; 1151 block[stride*10+offset]= ((z0 - z3)*qmul + 2)>>2;
1135 } 1152 }
1136 } 1153 }
1137 1154
1155 #if 0
1138 /** 1156 /**
1139 * dct tranforms the 16 dc values. 1157 * dct tranforms the 16 dc values.
1140 * @param qp quantization parameter ??? FIXME 1158 * @param qp quantization parameter ??? FIXME
1141 */ 1159 */
1142 static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){ 1160 static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
1170 block[stride*2 +offset]= (z1 + z2)>>1; 1188 block[stride*2 +offset]= (z1 + z2)>>1;
1171 block[stride*8 +offset]= (z1 - z2)>>1; 1189 block[stride*8 +offset]= (z1 - z2)>>1;
1172 block[stride*10+offset]= (z0 - z3)>>1; 1190 block[stride*10+offset]= (z0 - z3)>>1;
1173 } 1191 }
1174 } 1192 }
1193 #endif
1194
1175 #undef xStride 1195 #undef xStride
1176 #undef stride 1196 #undef stride
1177 1197
1178 static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp){ 1198 static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp){
1179 const int qmul= dequant_coeff[qp][0]; 1199 const int qmul= dequant_coeff[qp][0];
1195 block[stride*0 + xStride*1]= ((e+b)*qmul + 0)>>1; 1215 block[stride*0 + xStride*1]= ((e+b)*qmul + 0)>>1;
1196 block[stride*1 + xStride*0]= ((a-c)*qmul + 0)>>1; 1216 block[stride*1 + xStride*0]= ((a-c)*qmul + 0)>>1;
1197 block[stride*1 + xStride*1]= ((e-b)*qmul + 0)>>1; 1217 block[stride*1 + xStride*1]= ((e-b)*qmul + 0)>>1;
1198 } 1218 }
1199 1219
1220 #if 0
1200 static void chroma_dc_dct_c(DCTELEM *block){ 1221 static void chroma_dc_dct_c(DCTELEM *block){
1201 const int stride= 16*2; 1222 const int stride= 16*2;
1202 const int xStride= 16; 1223 const int xStride= 16;
1203 int a,b,c,d,e; 1224 int a,b,c,d,e;
1204 1225
1215 block[stride*0 + xStride*0]= (a+c); 1236 block[stride*0 + xStride*0]= (a+c);
1216 block[stride*0 + xStride*1]= (e+b); 1237 block[stride*0 + xStride*1]= (e+b);
1217 block[stride*1 + xStride*0]= (a-c); 1238 block[stride*1 + xStride*0]= (a-c);
1218 block[stride*1 + xStride*1]= (e-b); 1239 block[stride*1 + xStride*1]= (e-b);
1219 } 1240 }
1241 #endif
1220 1242
1221 /** 1243 /**
1222 * gets the chroma qp. 1244 * gets the chroma qp.
1223 */ 1245 */
1224 static inline int get_chroma_qp(H264Context *h, int qscale){ 1246 static inline int get_chroma_qp(H264Context *h, int qscale){
1284 dst[i + 3*stride]= cm[ dst[i + 3*stride] + ((z0 - z3) >> 6) ]; 1306 dst[i + 3*stride]= cm[ dst[i + 3*stride] + ((z0 - z3) >> 6) ];
1285 } 1307 }
1286 #endif 1308 #endif
1287 } 1309 }
1288 1310
1311 #if 0
1289 static void h264_diff_dct_c(DCTELEM *block, uint8_t *src1, uint8_t *src2, int stride){ 1312 static void h264_diff_dct_c(DCTELEM *block, uint8_t *src1, uint8_t *src2, int stride){
1290 int i; 1313 int i;
1291 //FIXME try int temp instead of block 1314 //FIXME try int temp instead of block
1292 1315
1293 for(i=0; i<4; i++){ 1316 for(i=0; i<4; i++){
1316 block[1*4 + i]= 2*z3 + z2; 1339 block[1*4 + i]= 2*z3 + z2;
1317 block[2*4 + i]= z0 - z1; 1340 block[2*4 + i]= z0 - z1;
1318 block[3*4 + i]= z3 - 2*z2; 1341 block[3*4 + i]= z3 - 2*z2;
1319 } 1342 }
1320 } 1343 }
1344 #endif
1321 1345
1322 //FIXME need to check that this doesnt overflow signed 32 bit for low qp, iam not sure, its very close 1346 //FIXME need to check that this doesnt overflow signed 32 bit for low qp, iam not sure, its very close
1323 //FIXME check that gcc inlines this (and optimizes intra & seperate_dc stuff away) 1347 //FIXME check that gcc inlines this (and optimizes intra & seperate_dc stuff away)
1324 static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int seperate_dc){ 1348 static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int seperate_dc){
1325 int i; 1349 int i;
2107 h->pred16x16[DC_128_PRED8x8 ]= pred16x16_128_dc_c; 2131 h->pred16x16[DC_128_PRED8x8 ]= pred16x16_128_dc_c;
2108 } 2132 }
2109 2133
2110 static void free_tables(H264Context *h){ 2134 static void free_tables(H264Context *h){
2111 av_freep(&h->intra4x4_pred_mode); 2135 av_freep(&h->intra4x4_pred_mode);
2136 av_freep(&h->chroma_pred_mode_table);
2137 av_freep(&h->cbp_table);
2112 av_freep(&h->non_zero_count); 2138 av_freep(&h->non_zero_count);
2113 av_freep(&h->slice_table_base); 2139 av_freep(&h->slice_table_base);
2114 av_freep(&h->top_border); 2140 av_freep(&h->top_border);
2115 h->slice_table= NULL; 2141 h->slice_table= NULL;
2116 2142
2117 av_freep(&h->mb2b_xy); 2143 av_freep(&h->mb2b_xy);
2118 av_freep(&h->mb2b8_xy); 2144 av_freep(&h->mb2b8_xy);
2119 } 2145 }
2120 2146
2121 /** 2147 /**
2126 MpegEncContext * const s = &h->s; 2152 MpegEncContext * const s = &h->s;
2127 const int big_mb_num= s->mb_stride * (s->mb_height+1); 2153 const int big_mb_num= s->mb_stride * (s->mb_height+1);
2128 int x,y; 2154 int x,y;
2129 2155
2130 CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t)) 2156 CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
2157
2131 CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t)) 2158 CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
2132 CHECKED_ALLOCZ(h->slice_table_base , big_mb_num * sizeof(uint8_t)) 2159 CHECKED_ALLOCZ(h->slice_table_base , big_mb_num * sizeof(uint8_t))
2133 CHECKED_ALLOCZ(h->top_border , s->mb_width * (16+8+8) * sizeof(uint8_t)) 2160 CHECKED_ALLOCZ(h->top_border , s->mb_width * (16+8+8) * sizeof(uint8_t))
2161
2162 if( h->pps.cabac ) {
2163 CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
2164 CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
2165 } else {
2166 h->chroma_pred_mode_table = NULL;
2167 h->cbp_table = NULL;
2168 }
2134 2169
2135 memset(h->slice_table_base, -1, big_mb_num * sizeof(uint8_t)); 2170 memset(h->slice_table_base, -1, big_mb_num * sizeof(uint8_t));
2136 h->slice_table= h->slice_table_base + s->mb_stride + 1; 2171 h->slice_table= h->slice_table_base + s->mb_stride + 1;
2137 2172
2138 CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint16_t)); 2173 CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint16_t));
2407 } 2442 }
2408 if(h->deblocking_filter) { 2443 if(h->deblocking_filter) {
2409 backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize); 2444 backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
2410 filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr); 2445 filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr);
2411 } 2446 }
2412 }
2413
2414 static void decode_mb_cabac(H264Context *h){
2415 // MpegEncContext * const s = &h->s;
2416 } 2447 }
2417 2448
2418 /** 2449 /**
2419 * fills the default_ref_list. 2450 * fills the default_ref_list.
2420 */ 2451 */
3055 || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) ) 3086 || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
3056 pred_weight_table(h); 3087 pred_weight_table(h);
3057 3088
3058 if(s->current_picture.reference) 3089 if(s->current_picture.reference)
3059 decode_ref_pic_marking(h); 3090 decode_ref_pic_marking(h);
3060 //FIXME CABAC stuff 3091
3061 3092 if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac )
3062 s->qscale = h->pps.init_qp + get_se_golomb(&s->gb); //slice_qp_delta 3093 h->cabac_init_idc = get_ue_golomb(&s->gb);
3094
3095 h->last_qscale_diff = 0;
3096 s->qscale = h->pps.init_qp + get_se_golomb(&s->gb);
3063 if(s->qscale<0 || s->qscale>51){ 3097 if(s->qscale<0 || s->qscale>51){
3064 av_log(s->avctx, AV_LOG_ERROR, "QP %d out of range\n", s->qscale); 3098 av_log(s->avctx, AV_LOG_ERROR, "QP %d out of range\n", s->qscale);
3065 return -1; 3099 return -1;
3066 } 3100 }
3067 //FIXME qscale / qp ... stuff 3101 //FIXME qscale / qp ... stuff
3271 3305
3272 /** 3306 /**
3273 * decodes a macroblock 3307 * decodes a macroblock
3274 * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed 3308 * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
3275 */ 3309 */
3276 static int decode_mb(H264Context *h){ 3310 static int decode_mb_cavlc(H264Context *h){
3277 MpegEncContext * const s = &h->s; 3311 MpegEncContext * const s = &h->s;
3278 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride; 3312 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
3279 int mb_type, partition_count, cbp; 3313 int mb_type, partition_count, cbp;
3280 3314
3281 s->dsp.clear_blocks(h->mb); //FIXME avoid if allready clear (move after skip handlong? 3315 s->dsp.clear_blocks(h->mb); //FIXME avoid if allready clear (move after skip handlong?
3705 s->current_picture.qscale_table[mb_xy]= s->qscale; 3739 s->current_picture.qscale_table[mb_xy]= s->qscale;
3706 write_back_non_zero_count(h); 3740 write_back_non_zero_count(h);
3707 3741
3708 return 0; 3742 return 0;
3709 } 3743 }
3744
3745 static int decode_cabac_mb_type( H264Context *h ) {
3746 MpegEncContext * const s = &h->s;
3747
3748 if( h->slice_type == I_TYPE ) {
3749 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
3750 int ctx = 0;
3751 int mb_type;
3752
3753 if( s->mb_x > 0 && !IS_INTRA4x4( s->current_picture.mb_type[mb_xy-1] ) )
3754 ctx++;
3755 if( s->mb_y > 0 && !IS_INTRA4x4( s->current_picture.mb_type[mb_xy-s->mb_stride] ) )
3756 ctx++;
3757
3758 if( get_cabac( &h->cabac, &h->cabac_state[3+ctx] ) == 0 )
3759 return 0; /* I4x4 */
3760
3761 if( get_cabac_terminate( &h->cabac ) )
3762 return 25; /* PCM */
3763
3764 mb_type = 1; /* I16x16 */
3765 if( get_cabac( &h->cabac, &h->cabac_state[3+3] ) )
3766 mb_type += 12; /* cbp_luma != 0 */
3767
3768 if( get_cabac( &h->cabac, &h->cabac_state[3+4] ) ) {
3769 if( get_cabac( &h->cabac, &h->cabac_state[3+5] ) )
3770 mb_type += 4 * 2; /* cbp_chroma == 2 */
3771 else
3772 mb_type += 4 * 1; /* cbp_chroma == 1 */
3773 }
3774 if( get_cabac( &h->cabac, &h->cabac_state[3+6] ) )
3775 mb_type += 2;
3776 if( get_cabac( &h->cabac, &h->cabac_state[3+7] ) )
3777 mb_type += 1;
3778 return mb_type;
3779
3780 } else if( h->slice_type == P_TYPE ) {
3781 if( get_cabac( &h->cabac, &h->cabac_state[14] ) == 0 ) {
3782 /* P-type */
3783 if( get_cabac( &h->cabac, &h->cabac_state[15] ) == 0 ) {
3784 if( get_cabac( &h->cabac, &h->cabac_state[16] ) == 0 )
3785 return 0; /* P_L0_D16x16; */
3786 else
3787 return 3; /* P_8x8; */
3788 } else {
3789 if( get_cabac( &h->cabac, &h->cabac_state[17] ) == 0 )
3790 return 1; /* P_L0_D16x8; */
3791 else
3792 return 2; /* P_L0_D8x16; */
3793 }
3794 } else {
3795 int mb_type;
3796 /* I-type */
3797 if( get_cabac( &h->cabac, &h->cabac_state[17] ) == 0 )
3798 return 5+0; /* I_4x4 */
3799 if( get_cabac_terminate( &h->cabac ) )
3800 return 5+25; /*I_PCM */
3801 mb_type = 5+1; /* I16x16 */
3802 if( get_cabac( &h->cabac, &h->cabac_state[17+1] ) )
3803 mb_type += 12; /* cbp_luma != 0 */
3804
3805 if( get_cabac( &h->cabac, &h->cabac_state[17+2] ) ) {
3806 if( get_cabac( &h->cabac, &h->cabac_state[17+2] ) )
3807 mb_type += 4 * 2; /* cbp_chroma == 2 */
3808 else
3809 mb_type += 4 * 1; /* cbp_chroma == 1 */
3810 }
3811 if( get_cabac( &h->cabac, &h->cabac_state[17+3] ) )
3812 mb_type += 2;
3813 if( get_cabac( &h->cabac, &h->cabac_state[17+3] ) )
3814 mb_type += 1;
3815
3816 return mb_type;
3817 }
3818 } else {
3819 /* TODO do others frames types */
3820 return -1;
3821 }
3822 }
3823
3824 static int decode_cabac_mb_skip( H264Context *h) {
3825 MpegEncContext * const s = &h->s;
3826 const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
3827 const int mba_xy = mb_xy - 1;
3828 const int mbb_xy = mb_xy - s->mb_stride;
3829 int ctx = 0;
3830
3831 if( s->mb_x > 0 && !IS_SKIP( s->current_picture.mb_type[mba_xy] ) )
3832 ctx++;
3833 if( s->mb_y > 0 && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ) )
3834 ctx++;
3835
3836 if( h->slice_type == P_TYPE || h->slice_type == SP_TYPE)
3837 return get_cabac( &h->cabac, &h->cabac_state[11+ctx] );
3838 else /* B-frame */
3839 return get_cabac( &h->cabac, &h->cabac_state[24+ctx] );
3840 }
3841
3842 static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
3843 int mode = 0;
3844
3845 if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
3846 return pred_mode;
3847
3848 if( get_cabac( &h->cabac, &h->cabac_state[69] ) )
3849 mode += 1;
3850 if( get_cabac( &h->cabac, &h->cabac_state[69] ) )
3851 mode += 2;
3852 if( get_cabac( &h->cabac, &h->cabac_state[69] ) )
3853 mode += 4;
3854 if( mode >= pred_mode )
3855 return mode + 1;
3856 else
3857 return mode;
3858 }
3859
3860 static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
3861 MpegEncContext * const s = &h->s;
3862 const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
3863 const int mba_xy = mb_xy - 1;
3864 const int mbb_xy = mb_xy - s->mb_stride;
3865
3866 int ctx = 0;
3867
3868 if( s->mb_x > 0 &&
3869 ( IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) || IS_INTRA16x16( s->current_picture.mb_type[mba_xy] ) ) &&
3870 h->chroma_pred_mode_table[mba_xy] != 0 ) {
3871 ctx++;
3872 }
3873 if( s->mb_y > 0 &&
3874 ( IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) || IS_INTRA16x16( s->current_picture.mb_type[mbb_xy] ) ) &&
3875 h->chroma_pred_mode_table[mbb_xy] != 0 ) {
3876 ctx++;
3877 }
3878
3879 if( get_cabac( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
3880 return 0;
3881
3882 if( get_cabac( &h->cabac, &h->cabac_state[64+3] ) == 0 )
3883 return 1;
3884 if( get_cabac( &h->cabac, &h->cabac_state[64+3] ) == 0 )
3885 return 2;
3886 else
3887 return 3;
3888 }
3889
3890 static const uint8_t block_idx_x[16] = {
3891 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
3892 };
3893 static const uint8_t block_idx_y[16] = {
3894 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
3895 };
3896 static const uint8_t block_idx_xy[4][4] = {
3897 { 0, 2, 8, 10},
3898 { 1, 3, 9, 11},
3899 { 4, 6, 12, 14},
3900 { 5, 7, 13, 15}
3901 };
3902
3903 static int decode_cabac_mb_cbp_luma( H264Context *h) {
3904 MpegEncContext * const s = &h->s;
3905 const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
3906
3907 int cbp = 0;
3908 int i8x8;
3909
3910 h->cbp_table[mb_xy] = 0; /* FIXME aaahahahah beurk */
3911
3912 for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
3913 int mba_xy = -1;
3914 int mbb_xy = -1;
3915 int x, y;
3916 int ctx = 0;
3917
3918 x = block_idx_x[4*i8x8];
3919 y = block_idx_y[4*i8x8];
3920
3921 if( x > 0 )
3922 mba_xy = mb_xy;
3923 else if( s->mb_x > 0 )
3924 mba_xy = mb_xy - 1;
3925
3926 if( y > 0 )
3927 mbb_xy = mb_xy;
3928 else if( s->mb_y > 0 )
3929 mbb_xy = mb_xy - s->mb_stride;
3930
3931 if( mba_xy >= 0 ) {
3932 int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
3933 if( IS_SKIP( s->current_picture.mb_type[mba_xy] ) || ((h->cbp_table[mba_xy] >> i8x8a)&0x01) == 0 )
3934 ctx++;
3935 }
3936
3937 if( mbb_xy >= 0 ) {
3938 int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
3939 if( IS_SKIP( s->current_picture.mb_type[mbb_xy] ) || ((h->cbp_table[mbb_xy] >> i8x8b)&0x01) == 0 )
3940 ctx += 2;
3941 }
3942
3943 if( get_cabac( &h->cabac, &h->cabac_state[73 + ctx] ) ) {
3944 cbp |= 1 << i8x8;
3945 h->cbp_table[mb_xy] = cbp; /* FIXME aaahahahah beurk */
3946 }
3947 }
3948 return cbp;
3949 }
3950 static int decode_cabac_mb_cbp_chroma( H264Context *h) {
3951 MpegEncContext * const s = &h->s;
3952 const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
3953 int ctx;
3954 int cbp_a, cbp_b;
3955
3956 if( s->mb_x > 0 && !IS_SKIP( s->current_picture.mb_type[mb_xy-1] ) )
3957 cbp_a = (h->cbp_table[mb_xy-1]>>4)&0x03;
3958 else
3959 cbp_a = -1;
3960
3961 if( s->mb_y > 0 && !IS_SKIP( s->current_picture.mb_type[mb_xy-s->mb_stride] ) )
3962 cbp_b = (h->cbp_table[mb_xy-s->mb_stride]>>4)&0x03;
3963 else
3964 cbp_b = -1;
3965
3966 ctx = 0;
3967 if( cbp_a > 0 ) ctx++;
3968 if( cbp_b > 0 ) ctx += 2;
3969 if( get_cabac( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
3970 return 0;
3971
3972 ctx = 4;
3973 if( cbp_a == 2 ) ctx++;
3974 if( cbp_b == 2 ) ctx += 2;
3975 if( get_cabac( &h->cabac, &h->cabac_state[77 + ctx] ) )
3976 return 2;
3977 else
3978 return 1;
3979 }
3980 static int decode_cabac_mb_dqp( H264Context *h) {
3981 MpegEncContext * const s = &h->s;
3982 int mbn_xy;
3983 int ctx = 0;
3984 int val = 0;
3985
3986 if( s->mb_x > 0 )
3987 mbn_xy = s->mb_x + s->mb_y*s->mb_stride - 1;
3988 else
3989 mbn_xy = s->mb_width - 1 + (s->mb_y-1)*s->mb_stride;
3990
3991 if( mbn_xy >= 0 && h->last_qscale_diff != 0 && ( IS_INTRA16x16(s->current_picture.mb_type[mbn_xy] ) || (h->cbp_table[mbn_xy]&0x3f) ) )
3992 ctx++;
3993
3994 while( get_cabac( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
3995 if( ctx < 2 )
3996 ctx = 2;
3997 else
3998 ctx = 3;
3999 val++;
4000 }
4001
4002 if( val&0x01 )
4003 return (val + 1)/2;
4004 else
4005 return -(val + 1)/2;
4006 }
4007
4008 static int get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) {
4009 MpegEncContext * const s = &h->s;
4010 const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
4011 int mba_xy = -1;
4012 int mbb_xy = -1;
4013
4014 int nza = -1;
4015 int nzb = -1;
4016 int ctx = 0;
4017
4018 if( cat == 0 ) {
4019 if( s->mb_x > 0 ) {
4020 mba_xy = mb_xy - 1;
4021 if( IS_INTRA16x16(s->current_picture.mb_type[mba_xy] ) )
4022 nza = h->cbp_table[mba_xy]&0x100;
4023 }
4024 if( s->mb_y > 0 ) {
4025 mbb_xy = mb_xy - s->mb_stride;
4026 if( IS_INTRA16x16(s->current_picture.mb_type[mbb_xy] ) )
4027 nzb = h->cbp_table[mbb_xy]&0x100;
4028 }
4029 } else if( cat == 1 || cat == 2 ) {
4030 int i8x8a, i8x8b;
4031 int x, y;
4032
4033 x = block_idx_x[idx];
4034 y = block_idx_y[idx];
4035
4036 if( x > 0 )
4037 mba_xy = mb_xy;
4038 else if( s->mb_x > 0 )
4039 mba_xy = mb_xy - 1;
4040
4041 if( y > 0 )
4042 mbb_xy = mb_xy;
4043 else if( s->mb_y > 0 )
4044 mbb_xy = mb_xy - s->mb_stride;
4045
4046 if( mba_xy >= 0 ) {
4047 i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
4048
4049 if( !IS_SKIP(s->current_picture.mb_type[mba_xy] ) &&
4050 !IS_INTRA_PCM(s->current_picture.mb_type[mba_xy] ) &&
4051 ((h->cbp_table[mba_xy]&0x0f)>>i8x8a))
4052 nza = h->non_zero_count_cache[scan8[idx] - 1];
4053 }
4054
4055 if( mbb_xy >= 0 ) {
4056 i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
4057
4058 if( !IS_SKIP(s->current_picture.mb_type[mbb_xy] ) &&
4059 !IS_INTRA_PCM(s->current_picture.mb_type[mbb_xy] ) &&
4060 ((h->cbp_table[mbb_xy]&0x0f)>>i8x8b))
4061 nzb = h->non_zero_count_cache[scan8[idx] - 8];
4062 }
4063 } else if( cat == 3 ) {
4064 if( s->mb_x > 0 ) {
4065 mba_xy = mb_xy - 1;
4066
4067 if( !IS_SKIP(s->current_picture.mb_type[mba_xy] ) &&
4068 !IS_INTRA_PCM(s->current_picture.mb_type[mba_xy] ) &&
4069 (h->cbp_table[mba_xy]&0x30) )
4070 nza = (h->cbp_table[mba_xy]>>(6+idx))&0x01;
4071 }
4072 if( s->mb_y > 0 ) {
4073 mbb_xy = mb_xy - s->mb_stride;
4074
4075 if( !IS_SKIP(s->current_picture.mb_type[mbb_xy] ) &&
4076 !IS_INTRA_PCM(s->current_picture.mb_type[mbb_xy] ) &&
4077 (h->cbp_table[mbb_xy]&0x30) )
4078 nzb = (h->cbp_table[mbb_xy]>>(6+idx))&0x01;
4079 }
4080 } else if( cat == 4 ) {
4081 int idxc = idx % 4 ;
4082 if( idxc == 1 || idxc == 3 )
4083 mba_xy = mb_xy;
4084 else if( s->mb_x > 0 )
4085 mba_xy = mb_xy -1;
4086
4087 if( idxc == 2 || idxc == 3 )
4088 mbb_xy = mb_xy;
4089 else if( s->mb_y > 0 )
4090 mbb_xy = mb_xy - s->mb_stride;
4091
4092 if( mba_xy >= 0 &&
4093 !IS_SKIP(s->current_picture.mb_type[mba_xy] ) &&
4094 !IS_INTRA_PCM(s->current_picture.mb_type[mba_xy] ) &&
4095 (h->cbp_table[mba_xy]&0x30) == 0x20 )
4096 nza = h->non_zero_count_cache[scan8[16+idx] - 1];
4097
4098 if( mbb_xy >= 0 &&
4099 !IS_SKIP(s->current_picture.mb_type[mbb_xy] ) &&
4100 !IS_INTRA_PCM(s->current_picture.mb_type[mbb_xy] ) &&
4101 (h->cbp_table[mbb_xy]&0x30) == 0x20 )
4102 nzb = h->non_zero_count_cache[scan8[16+idx] - 8];
4103 }
4104
4105 if( ( mba_xy < 0 && IS_INTRA( s->current_picture.mb_type[mb_xy] ) ) ||
4106 ( mba_xy >= 0 && IS_INTRA_PCM(s->current_picture.mb_type[mba_xy] ) ) ||
4107 nza > 0 )
4108 ctx++;
4109
4110 if( ( mbb_xy < 0 && IS_INTRA( s->current_picture.mb_type[mb_xy] ) ) ||
4111 ( mbb_xy >= 0 && IS_INTRA_PCM(s->current_picture.mb_type[mbb_xy] ) ) ||
4112 nzb > 0 )
4113 ctx += 2;
4114
4115 return ctx + 4 * cat;
4116 }
4117
4118 static int decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, int qp, int max_coeff) {
4119 const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
4120 const uint16_t *qmul= dequant_coeff[qp];
4121 static const int significant_coeff_flag_offset[5] = { 0, 15, 29, 44, 47 };
4122 static const int last_significant_coeff_flag_offset[5] = { 0, 15, 29, 44, 47 };
4123 static const int coeff_abs_level_m1_offset[5] = { 0, 10, 20, 30, 39 };
4124
4125 int coeff[16];
4126
4127 int last = 0;
4128 int coeff_count = 0;
4129 int nz[16] = {0};
4130 int i;
4131
4132 int abslevel1 = 0;
4133 int abslevelgt1 = 0;
4134
4135 /* cat: 0-> DC 16x16 n = 0
4136 * 1-> AC 16x16 n = luma4x4idx
4137 * 2-> Luma4x4 n = luma4x4idx
4138 * 3-> DC Chroma n = iCbCr
4139 * 4-> AC Chroma n = 4 * iCbCr + chroma4x4idx
4140 */
4141
4142 /* read coded block flag */
4143 if( get_cabac( &h->cabac, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n ) ] ) == 0 ) {
4144 if( cat == 1 || cat == 2 )
4145 h->non_zero_count_cache[scan8[n]] = 0;
4146 else if( cat == 4 )
4147 h->non_zero_count_cache[scan8[16+n]] = 0;
4148
4149 return 0;
4150 }
4151
4152 while( last < max_coeff - 1 ) {
4153 int ctx = FFMIN( last, max_coeff - 2 );
4154
4155 if( get_cabac( &h->cabac, &h->cabac_state[105+significant_coeff_flag_offset[cat]+ctx] ) == 0 ) {
4156 nz[last++] = 0;
4157 }
4158 else {
4159 nz[last++] = 1;
4160 coeff_count++;
4161 if( get_cabac( &h->cabac, &h->cabac_state[166+last_significant_coeff_flag_offset[cat]+ctx] ) ) {
4162 while( last < max_coeff ) {
4163 nz[last++] = 0;
4164 }
4165 break;
4166 }
4167 }
4168 }
4169 if( last == max_coeff -1 ) {
4170 nz[last++] = 1;
4171 coeff_count++;
4172 }
4173
4174 if( cat == 0 && coeff_count > 0 )
4175 h->cbp_table[mb_xy] |= 0x100;
4176 else if( cat == 1 || cat == 2 )
4177 h->non_zero_count_cache[scan8[n]] = coeff_count;
4178 else if( cat == 3 && coeff_count > 0 )
4179 h->cbp_table[mb_xy] |= 0x40 << n;
4180 else if( cat == 4 )
4181 h->non_zero_count_cache[scan8[16+n]] = coeff_count;
4182
4183 for( i = coeff_count - 1; i >= 0; i-- ) {
4184 int coeff_abs_m1;
4185
4186 int ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 + 1 )) + coeff_abs_level_m1_offset[cat];
4187
4188 if( get_cabac( &h->cabac, &h->cabac_state[227+ctx] ) == 0 ) {
4189 coeff_abs_m1 = 0;
4190 } else {
4191 coeff_abs_m1 = 1;
4192 ctx = 5 + FFMIN( 4, abslevelgt1 ) + coeff_abs_level_m1_offset[cat];
4193 while( coeff_abs_m1 < 14 && get_cabac( &h->cabac, &h->cabac_state[227+ctx] ) ) {
4194 coeff_abs_m1++;
4195 }
4196 }
4197
4198 if( coeff_abs_m1 >= 14 ) {
4199 int j = 0;
4200 while( get_cabac_bypass( &h->cabac ) ) {
4201 coeff_abs_m1 += 1 << j;
4202 j++;
4203 }
4204
4205 while( j-- ) {
4206 if( get_cabac_bypass( &h->cabac ) )
4207 coeff_abs_m1 += 1 << j ;
4208 }
4209 }
4210 if( get_cabac_bypass( &h->cabac ) )
4211 coeff[i] = -1 *( coeff_abs_m1 + 1 );
4212 else
4213 coeff[i] = coeff_abs_m1 + 1;
4214
4215 if( coeff_abs_m1 == 0 )
4216 abslevel1++;
4217 else
4218 abslevelgt1++;
4219 }
4220
4221 if( cat == 0 || cat == 3 ) { /* DC */
4222 int j;
4223 for( i = 0, j = 0; j < coeff_count; i++ ) {
4224 if( nz[i] ) {
4225 block[scantable[i]] = coeff[j];
4226
4227 j++;
4228 }
4229 }
4230
4231 } else { /* AC */
4232 int j;
4233 for( i = 0, j = 0; j < coeff_count; i++ ) {
4234 if( nz[i] ) {
4235 block[scantable[i]] = coeff[j] * qmul[scantable[i]];
4236
4237 j++;
4238 }
4239 }
4240 }
4241 return 0;
4242 }
4243
4244 /**
4245 * decodes a macroblock
4246 * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
4247 */
4248 static int decode_mb_cabac(H264Context *h) {
4249 MpegEncContext * const s = &h->s;
4250 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
4251 int mb_type, partition_count, cbp = 0;
4252
4253 s->dsp.clear_blocks(h->mb); //FIXME avoid if allready clear (move after skip handlong?)
4254
4255 if( h->slice_type == B_TYPE ) {
4256 av_log( h->s.avctx, AV_LOG_ERROR, "B-frame not supported with CABAC\n" );
4257 return -1;
4258 }
4259 if( h->sps.mb_aff ) {
4260 av_log( h->s.avctx, AV_LOG_ERROR, "Fields not supported with CABAC\n" );
4261 return -1;
4262 }
4263
4264 if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) {
4265 /* read skip flags */
4266 if( decode_cabac_mb_skip( h ) ) {
4267 int mx, my;
4268
4269 /* skip mb */
4270 mb_type= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
4271
4272 memset(h->non_zero_count[mb_xy], 0, 16);
4273 memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
4274 #if 0
4275 if(h->sps.mb_aff && s->mb_skip_run==0 && (s->mb_y&1)==0){
4276 h->mb_field_decoding_flag= get_bits1(&s->gb);
4277 }
4278 if(h->mb_field_decoding_flag)
4279 mb_type|= MB_TYPE_INTERLACED;
4280 #endif
4281
4282 fill_caches(h, mb_type); //FIXME check what is needed and what not ...
4283 pred_pskip_motion(h, &mx, &my);
4284 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
4285 fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
4286 write_back_motion(h, mb_type);
4287
4288 s->current_picture.mb_type[mb_xy]= mb_type; //FIXME SKIP type
4289 s->current_picture.qscale_table[mb_xy]= s->qscale;
4290 h->slice_table[ mb_xy ]= h->slice_num;
4291 h->cbp_table[mb_xy] = 0;
4292 h->last_qscale_diff = 0;
4293
4294 h->prev_mb_skiped= 1;
4295
4296 return 0;
4297
4298 }
4299 }
4300 h->prev_mb_skiped = 0;
4301
4302 if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
4303 av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
4304 return -1;
4305 }
4306 //av_log( s->avctx, AV_LOG_ERROR, "mb_type=%d\n", mb_type );
4307
4308 if( h->slice_type == P_TYPE ) {
4309 if( mb_type < 5) {
4310 partition_count= p_mb_type_info[mb_type].partition_count;
4311 mb_type= p_mb_type_info[mb_type].type;
4312 av_log( h->s.avctx, AV_LOG_ERROR, "gni P-type not yet supported\n" );
4313 return -1;
4314 } else {
4315 mb_type -= 5;
4316 goto decode_intra_mb;
4317 }
4318 } else {
4319 assert(h->slice_type == I_TYPE);
4320 decode_intra_mb:
4321 partition_count = 0;
4322 cbp= i_mb_type_info[mb_type].cbp;
4323 h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
4324 mb_type= i_mb_type_info[mb_type].type;
4325 }
4326 #if 0
4327 if(h->mb_field_decoding_flag)
4328 mb_type |= MB_TYPE_INTERLACED;
4329 #endif
4330
4331 s->current_picture.mb_type[mb_xy]= mb_type;
4332 h->slice_table[ mb_xy ]= h->slice_num;
4333
4334 if(IS_INTRA_PCM(mb_type)) {
4335 /* TODO */
4336 h->cbp_table[mb_xy] = 0xf +4*2;
4337 s->current_picture.qscale_table[mb_xy]= s->qscale;
4338 return -1;
4339 }
4340
4341 fill_caches(h, mb_type);
4342
4343 if( IS_INTRA( mb_type ) ) {
4344 if( IS_INTRA4x4( mb_type ) ) {
4345 int i;
4346 for( i = 0; i < 16; i++ ) {
4347 int pred = pred_intra_mode( h, i );
4348 h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
4349
4350 //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
4351 }
4352 write_back_intra_pred_mode(h);
4353 if( check_intra4x4_pred_mode(h) < 0 ) return -1;
4354 } else {
4355 h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );
4356 if( h->intra16x16_pred_mode < 0 ) return -1;
4357 }
4358 h->chroma_pred_mode_table[mb_xy] =
4359 h->chroma_pred_mode = decode_cabac_mb_chroma_pre_mode( h );
4360
4361 h->chroma_pred_mode= check_intra_pred_mode( h, h->chroma_pred_mode );
4362 if( h->chroma_pred_mode < 0 ) return -1;
4363 } else if( partition_count == 4 ) {
4364 /* TODO */
4365 return -1;
4366 } else if( !IS_DIRECT(mb_type) ) {
4367 /* TODO */
4368 return -1;
4369 }
4370
4371 if( IS_INTER( mb_type ) )
4372 write_back_motion( h, mb_type );
4373
4374 if( !IS_INTRA16x16( mb_type ) ) {
4375 cbp = decode_cabac_mb_cbp_luma( h );
4376 cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
4377 }
4378
4379 //av_log( NULL, AV_LOG_ERROR, "cbp=%d\n", cbp );
4380 h->cbp_table[mb_xy] = cbp;
4381
4382 if( cbp || IS_INTRA16x16( mb_type ) ) {
4383 const uint8_t *scan, *dc_scan;
4384 int dqp;
4385
4386 if(IS_INTERLACED(mb_type)){
4387 scan= field_scan;
4388 dc_scan= luma_dc_field_scan;
4389 }else{
4390 scan= zigzag_scan;
4391 dc_scan= luma_dc_zigzag_scan;
4392 }
4393
4394 h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
4395 s->qscale += dqp;
4396 if(((unsigned)s->qscale) > 51){
4397 if(s->qscale<0) s->qscale+= 52;
4398 else s->qscale-= 52;
4399 }
4400 h->chroma_qp = get_chroma_qp(h, s->qscale);
4401
4402 if( IS_INTRA16x16( mb_type ) ) {
4403 int i;
4404 //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
4405 if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, s->qscale, 16) < 0)
4406 return -1;
4407 if( cbp&15 ) {
4408 for( i = 0; i < 16; i++ ) {
4409 //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
4410 if( decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, s->qscale, 15) < 0 )
4411 return -1;
4412 }
4413 } else {
4414 fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
4415 }
4416 } else {
4417 int i8x8, i4x4;
4418 for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
4419 if( cbp & (1<<i8x8) ) {
4420 for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
4421 const int index = 4*i8x8 + i4x4;
4422 //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
4423 if( decode_cabac_residual(h, h->mb + 16*index, 2, index, scan, s->qscale, 16) < 0 )
4424 return -1;
4425 }
4426 } else {
4427 uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
4428 nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
4429 }
4430 }
4431 }
4432
4433 if( cbp&0x30 ){
4434 int c;
4435 for( c = 0; c < 2; c++ ) {
4436 //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
4437 if( decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, h->chroma_qp, 4) < 0)
4438 return -1;
4439 }
4440 }
4441
4442 if( cbp&0x20 ) {
4443 int c, i;
4444 for( c = 0; c < 2; c++ ) {
4445 for( i = 0; i < 4; i++ ) {
4446 const int index = 16 + 4 * c + i;
4447 //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
4448 if( decode_cabac_residual(h, h->mb + 16*index, 4, index - 16, scan + 1, h->chroma_qp, 15) < 0)
4449 return -1;
4450 }
4451 }
4452 } else {
4453 uint8_t * const nnz= &h->non_zero_count_cache[0];
4454 nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
4455 nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
4456 }
4457 } else {
4458 memset( &h->non_zero_count_cache[8], 0, 8*5 );
4459 }
4460
4461 s->current_picture.qscale_table[mb_xy]= s->qscale;
4462 write_back_non_zero_count(h);
4463
4464 return 0;
4465 }
4466
3710 4467
3711 static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) { 4468 static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
3712 int i, d; 4469 int i, d;
3713 const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 ); 4470 const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
3714 const int alpha = alpha_table[index_a]; 4471 const int alpha = alpha_table[index_a];
4095 static int decode_slice(H264Context *h){ 4852 static int decode_slice(H264Context *h){
4096 MpegEncContext * const s = &h->s; 4853 MpegEncContext * const s = &h->s;
4097 const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F; 4854 const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
4098 4855
4099 s->mb_skip_run= -1; 4856 s->mb_skip_run= -1;
4100 4857
4101 #if 1 4858 if( h->pps.cabac ) {
4102 for(;;){ 4859 int i;
4103 int ret= decode_mb(h); 4860
4104 4861 /* realign */
4105 hl_decode_mb(h); 4862 align_get_bits( &s->gb );
4106 4863
4107 if(ret>=0 && h->sps.mb_aff){ //FIXME optimal? or let mb_decode decode 16x32 ? 4864 /* init cabac */
4108 s->mb_y++; 4865 ff_init_cabac_states( &h->cabac, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64 );
4109 ret= decode_mb(h); 4866 ff_init_cabac_decoder( &h->cabac,
4110 4867 s->gb.buffer + get_bits_count(&s->gb)/8,
4868 ( s->gb.size_in_bits - get_bits_count(&s->gb) ) );
4869 /* calculate pre-state */
4870 for( i= 0; i < 399; i++ ) {
4871 int pre;
4872 if( h->slice_type == I_TYPE )
4873 pre = clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
4874 else
4875 pre = clip( ((cabac_context_init_PB[h->cabac_init_idc][i][0] * s->qscale) >>4 ) + cabac_context_init_PB[h->cabac_init_idc][i][1], 1, 126 );
4876
4877 if( pre <= 63 )
4878 h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
4879 else
4880 h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
4881 }
4882
4883 for(;;){
4884 int ret = decode_mb_cabac(h);
4885 int eos = get_cabac_terminate( &h->cabac ); /* End of Slice flag */
4886
4111 hl_decode_mb(h); 4887 hl_decode_mb(h);
4112 s->mb_y--; 4888
4113 } 4889 /* XXX: useless as decode_mb_cabac it doesn't support that ... */
4114 4890 if( ret >= 0 && h->sps.mb_aff ) { //FIXME optimal? or let mb_decode decode 16x32 ?
4115 if(ret<0){ 4891 s->mb_y++;
4116 av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y); 4892
4117 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask); 4893 ret = decode_mb_cabac(h);
4118 4894 eos = get_cabac_terminate( &h->cabac );
4119 return -1; 4895
4120 } 4896 hl_decode_mb(h);
4121 4897 s->mb_y--;
4122 if(++s->mb_x >= s->mb_width){ 4898 }
4123 s->mb_x=0; 4899
4124 ff_draw_horiz_band(s, 16*s->mb_y, 16); 4900 if( ret < 0 ) {
4125 if(++s->mb_y >= s->mb_height){ 4901 av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
4126 tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits); 4902 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
4127 4903 return -1;
4128 if(get_bits_count(&s->gb) == s->gb.size_in_bits){ 4904 }
4905
4906 if( ++s->mb_x >= s->mb_width ) {
4907 s->mb_x = 0;
4908 ff_draw_horiz_band(s, 16*s->mb_y, 16);
4909 if( ++s->mb_y >= s->mb_height ) {
4910 tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
4911 }
4912 }
4913
4914 if( eos || s->mb_y >= s->mb_height ) {
4915 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
4916 return 0;
4917 }
4918 #if 0
4919 /* TODO test over-reading in cabac code */
4920 else if( read too much in h->cabac ) {
4921 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
4922 return -1;
4923 }
4924 #endif
4925 }
4926
4927 } else {
4928 for(;;){
4929 int ret = decode_mb_cavlc(h);
4930
4931 hl_decode_mb(h);
4932
4933 if(ret>=0 && h->sps.mb_aff){ //FIXME optimal? or let mb_decode decode 16x32 ?
4934 s->mb_y++;
4935 ret = decode_mb_cavlc(h);
4936
4937 hl_decode_mb(h);
4938 s->mb_y--;
4939 }
4940
4941 if(ret<0){
4942 av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
4943 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
4944
4945 return -1;
4946 }
4947
4948 if(++s->mb_x >= s->mb_width){
4949 s->mb_x=0;
4950 ff_draw_horiz_band(s, 16*s->mb_y, 16);
4951 if(++s->mb_y >= s->mb_height){
4952 tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
4953
4954 if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
4955 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
4956
4957 return 0;
4958 }else{
4959 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
4960
4961 return -1;
4962 }
4963 }
4964 }
4965
4966 if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
4967 if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
4129 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask); 4968 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
4130 4969
4131 return 0; 4970 return 0;
4132 }else{ 4971 }else{
4133 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask); 4972 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
4134 4973
4135 return -1; 4974 return -1;
4136 } 4975 }
4137 } 4976 }
4138 } 4977 }
4139 4978 }
4140 if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){ 4979
4141 if(get_bits_count(&s->gb) == s->gb.size_in_bits){
4142 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
4143
4144 return 0;
4145 }else{
4146 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
4147
4148 return -1;
4149 }
4150 }
4151 }
4152 #endif
4153 #if 0 4980 #if 0
4154 for(;s->mb_y < s->mb_height; s->mb_y++){ 4981 for(;s->mb_y < s->mb_height; s->mb_y++){
4155 for(;s->mb_x < s->mb_width; s->mb_x++){ 4982 for(;s->mb_x < s->mb_width; s->mb_x++){
4156 int ret= decode_mb(h); 4983 int ret= decode_mb(h);
4157 4984