]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/h264_cabac.c
nvenc: Generate AUD NAL units for better compatiblity
[ffmpeg] / libavcodec / h264_cabac.c
index 88f50d6be36bbf12412fe3d18951cc1e68ce6cb2..5e7f3e7768fd46b2d960afb0841b398dd0e09d3d 100644 (file)
@@ -21,7 +21,7 @@
 
 /**
  * @file
- * H.264 / AVC / MPEG4 part10 cabac decoding.
+ * H.264 / AVC / MPEG-4 part10 cabac decoding.
  * @author Michael Niedermayer <michaelni@gmx.at>
  */
 
@@ -56,7 +56,7 @@ static const int8_t cabac_context_init_I[1024][2] =
     {  2,  54 }, {  3, 74 },  { -28,127 }, { -23, 104 },
     { -6,  53 }, { -1, 54 },  {  7,  51 },
 
-    /* 11 - 23 unsused for I */
+    /* 11 - 23 unused for I */
     { 0, 0 },    { 0, 0 },    { 0, 0 },      { 0, 0 },
     { 0, 0 },    { 0, 0 },    { 0, 0 },      { 0, 0 },
     { 0, 0 },    { 0, 0 },    { 0, 0 },      { 0, 0 },
@@ -1261,11 +1261,11 @@ static const int8_t cabac_context_init_PB[3][1024][2] =
     }
 };
 
-void ff_h264_init_cabac_states(H264Context *h, H264SliceContext *sl)
+void ff_h264_init_cabac_states(const H264Context *h, H264SliceContext *sl)
 {
     int i;
     const int8_t (*tab)[2];
-    const int slice_qp = av_clip(sl->qscale - 6*(h->sps.bit_depth_luma-8), 0, 51);
+    const int slice_qp = av_clip(sl->qscale - 6*(h->ps.sps->bit_depth_luma-8), 0, 51);
 
     if (sl->slice_type_nos == AV_PICTURE_TYPE_I) tab = cabac_context_init_I;
     else                                 tab = cabac_context_init_PB[sl->cabac_init_idc];
@@ -1282,19 +1282,19 @@ void ff_h264_init_cabac_states(H264Context *h, H264SliceContext *sl)
     }
 }
 
-static int decode_cabac_field_decoding_flag(H264Context *h, H264SliceContext *sl)
+static int decode_cabac_field_decoding_flag(const H264Context *h, H264SliceContext *sl)
 {
     const long mbb_xy = sl->mb_xy - 2L*h->mb_stride;
 
     unsigned long ctx = 0;
 
-    ctx += h->mb_field_decoding_flag & !!sl->mb_x; //for FMO:(s->current_picture.mb_type[mba_xy] >> 7) & (h->slice_table[mba_xy] == h->slice_num);
+    ctx += sl->mb_field_decoding_flag & !!sl->mb_x; //for FMO:(s->current_picture.mb_type[mba_xy] >> 7) & (h->slice_table[mba_xy] == h->slice_num);
     ctx += (h->cur_pic.mb_type[mbb_xy] >> 7) & (h->slice_table[mbb_xy] == sl->slice_num);
 
     return get_cabac_noinline( &sl->cabac, &(sl->cabac_state+70)[ctx] );
 }
 
-static int decode_cabac_intra_mb_type(H264Context *h, H264SliceContext *sl,
+static int decode_cabac_intra_mb_type(H264SliceContext *sl,
                                       int ctx_base, int intra_slice)
 {
     uint8_t *state= &sl->cabac_state[ctx_base];
@@ -1326,7 +1326,7 @@ static int decode_cabac_intra_mb_type(H264Context *h, H264SliceContext *sl,
     return mb_type;
 }
 
-static int decode_cabac_mb_skip(H264Context *h, H264SliceContext *sl,
+static int decode_cabac_mb_skip(const H264Context *h, H264SliceContext *sl,
                                 int mb_x, int mb_y)
 {
     int mba_xy, mbb_xy;
@@ -1337,9 +1337,9 @@ static int decode_cabac_mb_skip(H264Context *h, H264SliceContext *sl,
         mba_xy = mb_xy - 1;
         if( (mb_y&1)
             && h->slice_table[mba_xy] == sl->slice_num
-            && MB_FIELD(h) == !!IS_INTERLACED( h->cur_pic.mb_type[mba_xy] ) )
+            && MB_FIELD(sl) == !!IS_INTERLACED( h->cur_pic.mb_type[mba_xy] ) )
             mba_xy += h->mb_stride;
-        if (MB_FIELD(h)) {
+        if (MB_FIELD(sl)) {
             mbb_xy = mb_xy - h->mb_stride;
             if( !(mb_y&1)
                 && h->slice_table[mbb_xy] == sl->slice_num
@@ -1363,7 +1363,7 @@ static int decode_cabac_mb_skip(H264Context *h, H264SliceContext *sl,
     return get_cabac_noinline( &sl->cabac, &sl->cabac_state[11+ctx] );
 }
 
-static int decode_cabac_mb_intra4x4_pred_mode(H264Context *h, H264SliceContext *sl, int pred_mode)
+static int decode_cabac_mb_intra4x4_pred_mode(H264SliceContext *sl, int pred_mode)
 {
     int mode = 0;
 
@@ -1377,7 +1377,7 @@ static int decode_cabac_mb_intra4x4_pred_mode(H264Context *h, H264SliceContext *
     return mode + ( mode >= pred_mode );
 }
 
-static int decode_cabac_mb_chroma_pre_mode(H264Context *h, H264SliceContext *sl)
+static int decode_cabac_mb_chroma_pre_mode(const H264Context *h, H264SliceContext *sl)
 {
     const int mba_xy = sl->left_mb_xy[0];
     const int mbb_xy = sl->top_mb_xy;
@@ -1402,7 +1402,7 @@ static int decode_cabac_mb_chroma_pre_mode(H264Context *h, H264SliceContext *sl)
         return 3;
 }
 
-static int decode_cabac_mb_cbp_luma(H264Context *h, H264SliceContext *sl)
+static int decode_cabac_mb_cbp_luma(H264SliceContext *sl)
 {
     int cbp_b, cbp_a, ctx, cbp = 0;
 
@@ -1419,7 +1419,7 @@ static int decode_cabac_mb_cbp_luma(H264Context *h, H264SliceContext *sl)
     cbp += get_cabac_noinline(&sl->cabac, &sl->cabac_state[73 + ctx]) << 3;
     return cbp;
 }
-static int decode_cabac_mb_cbp_chroma(H264Context *h, H264SliceContext *sl)
+static int decode_cabac_mb_cbp_chroma(H264SliceContext *sl)
 {
     int ctx;
     int cbp_a, cbp_b;
@@ -1439,7 +1439,7 @@ static int decode_cabac_mb_cbp_chroma(H264Context *h, H264SliceContext *sl)
     return 1 + get_cabac_noinline( &sl->cabac, &sl->cabac_state[77 + ctx] );
 }
 
-static int decode_cabac_p_mb_sub_type(H264Context *h, H264SliceContext *sl)
+static int decode_cabac_p_mb_sub_type(H264SliceContext *sl)
 {
     if( get_cabac( &sl->cabac, &sl->cabac_state[21] ) )
         return 0;   /* 8x8 */
@@ -1449,7 +1449,7 @@ static int decode_cabac_p_mb_sub_type(H264Context *h, H264SliceContext *sl)
         return 2;   /* 4x8 */
     return 3;       /* 4x4 */
 }
-static int decode_cabac_b_mb_sub_type(H264Context *h, H264SliceContext *sl)
+static int decode_cabac_b_mb_sub_type(H264SliceContext *sl)
 {
     int type;
     if( !get_cabac( &sl->cabac, &sl->cabac_state[36] ) )
@@ -1467,7 +1467,7 @@ static int decode_cabac_b_mb_sub_type(H264Context *h, H264SliceContext *sl)
     return type;
 }
 
-static int decode_cabac_mb_ref(H264Context *h, H264SliceContext *sl, int list, int n)
+static int decode_cabac_mb_ref(H264SliceContext *sl, int list, int n)
 {
     int refa = sl->ref_cache[list][scan8[n] - 1];
     int refb = sl->ref_cache[list][scan8[n] - 8];
@@ -1496,7 +1496,7 @@ static int decode_cabac_mb_ref(H264Context *h, H264SliceContext *sl, int list, i
     return ref;
 }
 
-static int decode_cabac_mb_mvd(H264Context *h, H264SliceContext *sl, int ctxbase, int amvd, int *mvda)
+static int decode_cabac_mb_mvd(H264SliceContext *sl, int ctxbase, int amvd, int *mvda)
 {
     int mvd;
 
@@ -1520,7 +1520,7 @@ static int decode_cabac_mb_mvd(H264Context *h, H264SliceContext *sl, int ctxbase
             mvd += 1 << k;
             k++;
             if(k>24){
-                av_log(h->avctx, AV_LOG_ERROR, "overflow in decode_cabac_mb_mvd\n");
+                av_log(sl->h264->avctx, AV_LOG_ERROR, "overflow in decode_cabac_mb_mvd\n");
                 return INT_MIN;
             }
         }
@@ -1533,18 +1533,18 @@ static int decode_cabac_mb_mvd(H264Context *h, H264SliceContext *sl, int ctxbase
     return get_cabac_bypass_sign( &sl->cabac, -mvd );
 }
 
-#define DECODE_CABAC_MB_MVD( h,  list,  n )\
+#define DECODE_CABAC_MB_MVD(sl, list,  n )\
 {\
     int amvd0 = sl->mvd_cache[list][scan8[n] - 1][0] +\
                 sl->mvd_cache[list][scan8[n] - 8][0];\
     int amvd1 = sl->mvd_cache[list][scan8[n] - 1][1] +\
                 sl->mvd_cache[list][scan8[n] - 8][1];\
 \
-    mx += decode_cabac_mb_mvd(h, sl, 40, amvd0, &mpx);\
-    my += decode_cabac_mb_mvd(h, sl, 47, amvd1, &mpy);\
+    mx += decode_cabac_mb_mvd(sl, 40, amvd0, &mpx);\
+    my += decode_cabac_mb_mvd(sl, 47, amvd1, &mpy);\
 }
 
-static av_always_inline int get_cabac_cbf_ctx(H264Context *h, H264SliceContext *sl,
+static av_always_inline int get_cabac_cbf_ctx(H264SliceContext *sl,
                                               int cat, int idx, int max_coeff,
                                               int is_dc)
 {
@@ -1577,7 +1577,7 @@ static av_always_inline int get_cabac_cbf_ctx(H264Context *h, H264SliceContext *
 }
 
 static av_always_inline void
-decode_cabac_residual_internal(H264Context *h, H264SliceContext *sl,
+decode_cabac_residual_internal(const H264Context *h, H264SliceContext *sl,
                                int16_t *block,
                                int cat, int n, const uint8_t *scantable,
                                const uint32_t *qmul, int max_coeff,
@@ -1646,9 +1646,9 @@ decode_cabac_residual_internal(H264Context *h, H264SliceContext *sl,
 #endif
 
     significant_coeff_ctx_base = sl->cabac_state
-        + significant_coeff_flag_offset[MB_FIELD(h)][cat];
+        + significant_coeff_flag_offset[MB_FIELD(sl)][cat];
     last_coeff_ctx_base = sl->cabac_state
-        + last_coeff_flag_offset[MB_FIELD(h)][cat];
+        + last_coeff_flag_offset[MB_FIELD(sl)][cat];
     abs_level_m1_ctx_base = sl->cabac_state
         + coeff_abs_level_m1_offset[cat];
 
@@ -1668,7 +1668,7 @@ decode_cabac_residual_internal(H264Context *h, H264SliceContext *sl,
         if( last == max_coeff -1 ) {\
             index[coeff_count++] = last;\
         }
-        const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD(h)];
+        const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD(sl)];
 #ifdef decode_significance
         coeff_count = decode_significance_8x8(CC, significant_coeff_ctx_base, index,
                                                  last_coeff_ctx_base, sig_off);
@@ -1762,7 +1762,7 @@ decode_cabac_residual_internal(H264Context *h, H264SliceContext *sl,
 
 }
 
-static av_noinline void decode_cabac_residual_dc_internal(H264Context *h,
+static av_noinline void decode_cabac_residual_dc_internal(const H264Context *h,
                                                           H264SliceContext *sl,
                                                           int16_t *block,
                                                           int cat, int n,
@@ -1772,7 +1772,7 @@ static av_noinline void decode_cabac_residual_dc_internal(H264Context *h,
     decode_cabac_residual_internal(h, sl, block, cat, n, scantable, NULL, max_coeff, 1, 0);
 }
 
-static av_noinline void decode_cabac_residual_dc_internal_422(H264Context *h,
+static av_noinline void decode_cabac_residual_dc_internal_422(const H264Context *h,
                                                               H264SliceContext *sl,
                                                               int16_t *block,
                                                               int cat, int n,
@@ -1782,7 +1782,7 @@ static av_noinline void decode_cabac_residual_dc_internal_422(H264Context *h,
     decode_cabac_residual_internal(h, sl, block, cat, n, scantable, NULL, max_coeff, 1, 1);
 }
 
-static av_noinline void decode_cabac_residual_nondc_internal(H264Context *h,
+static av_noinline void decode_cabac_residual_nondc_internal(const H264Context *h,
                                                              H264SliceContext *sl,
                                                              int16_t *block,
                                                              int cat, int n,
@@ -1805,7 +1805,7 @@ static av_noinline void decode_cabac_residual_nondc_internal(H264Context *h,
  * because it allows improved constant propagation into get_cabac_cbf_ctx,
  * as well as because most blocks have zero CBFs. */
 
-static av_always_inline void decode_cabac_residual_dc(H264Context *h,
+static av_always_inline void decode_cabac_residual_dc(const H264Context *h,
                                                       H264SliceContext *sl,
                                                       int16_t *block,
                                                       int cat, int n,
@@ -1813,7 +1813,7 @@ static av_always_inline void decode_cabac_residual_dc(H264Context *h,
                                                       int max_coeff)
 {
     /* read coded block flag */
-    if( get_cabac( &sl->cabac, &sl->cabac_state[get_cabac_cbf_ctx(h, sl, cat, n, max_coeff, 1)]) == 0 ) {
+    if( get_cabac( &sl->cabac, &sl->cabac_state[get_cabac_cbf_ctx(sl, cat, n, max_coeff, 1)]) == 0 ) {
         sl->non_zero_count_cache[scan8[n]] = 0;
         return;
     }
@@ -1821,20 +1821,20 @@ static av_always_inline void decode_cabac_residual_dc(H264Context *h,
 }
 
 static av_always_inline void
-decode_cabac_residual_dc_422(H264Context *h, H264SliceContext *sl,
+decode_cabac_residual_dc_422(const H264Context *h, H264SliceContext *sl,
                              int16_t *block,
                              int cat, int n, const uint8_t *scantable,
                              int max_coeff)
 {
     /* read coded block flag */
-    if (get_cabac(&sl->cabac, &sl->cabac_state[get_cabac_cbf_ctx(h, sl, cat, n, max_coeff, 1)]) == 0) {
+    if (get_cabac(&sl->cabac, &sl->cabac_state[get_cabac_cbf_ctx(sl, cat, n, max_coeff, 1)]) == 0) {
         sl->non_zero_count_cache[scan8[n]] = 0;
         return;
     }
     decode_cabac_residual_dc_internal_422(h, sl, block, cat, n, scantable, max_coeff);
 }
 
-static av_always_inline void decode_cabac_residual_nondc(H264Context *h,
+static av_always_inline void decode_cabac_residual_nondc(const H264Context *h,
                                                          H264SliceContext *sl,
                                                          int16_t *block,
                                                          int cat, int n,
@@ -1843,7 +1843,7 @@ static av_always_inline void decode_cabac_residual_nondc(H264Context *h,
                                                          int max_coeff)
 {
     /* read coded block flag */
-    if( (cat != 5 || CHROMA444(h)) && get_cabac( &sl->cabac, &sl->cabac_state[get_cabac_cbf_ctx(h, sl, cat, n, max_coeff, 0)]) == 0) {
+    if( (cat != 5 || CHROMA444(h)) && get_cabac( &sl->cabac, &sl->cabac_state[get_cabac_cbf_ctx(sl, cat, n, max_coeff, 0)]) == 0) {
         if( max_coeff == 64 ) {
             fill_rectangle(&sl->non_zero_count_cache[scan8[n]], 2, 2, 8, 0, 1);
         } else {
@@ -1854,7 +1854,7 @@ static av_always_inline void decode_cabac_residual_nondc(H264Context *h,
     decode_cabac_residual_nondc_internal(h, sl, block, cat, n, scantable, qmul, max_coeff);
 }
 
-static av_always_inline void decode_cabac_luma_residual(H264Context *h, H264SliceContext *sl,
+static av_always_inline void decode_cabac_luma_residual(const H264Context *h, H264SliceContext *sl,
                                                         const uint8_t *scan, const uint8_t *scan8x8,
                                                         int pixel_shift, int mb_type, int cbp, int p)
 {
@@ -1870,7 +1870,7 @@ static av_always_inline void decode_cabac_luma_residual(H264Context *h, H264Slic
         decode_cabac_residual_dc(h, sl, sl->mb_luma_dc[p], ctx_cat[0][p], LUMA_DC_BLOCK_INDEX+p, scan, 16);
 
         if( cbp&15 ) {
-            qmul = h->dequant4_coeff[p][qscale];
+            qmul = h->ps.pps->dequant4_coeff[p][qscale];
             for( i4x4 = 0; i4x4 < 16; i4x4++ ) {
                 const int index = 16*p + i4x4;
                 decode_cabac_residual_nondc(h, sl, sl->mb + (16*index << pixel_shift), ctx_cat[1][p], index, scan + 1, qmul, 15);
@@ -1885,9 +1885,9 @@ static av_always_inline void decode_cabac_luma_residual(H264Context *h, H264Slic
                 if( IS_8x8DCT(mb_type) ) {
                     const int index = 16*p + 4*i8x8;
                     decode_cabac_residual_nondc(h, sl, sl->mb + (16*index << pixel_shift), ctx_cat[3][p], index,
-                                                scan8x8, h->dequant8_coeff[cqm][qscale], 64);
+                                                scan8x8, h->ps.pps->dequant8_coeff[cqm][qscale], 64);
                 } else {
-                    qmul = h->dequant4_coeff[cqm][qscale];
+                    qmul = h->ps.pps->dequant4_coeff[cqm][qscale];
                     for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
                         const int index = 16*p + 4*i8x8 + i4x4;
 //START_TIMER
@@ -1906,17 +1906,17 @@ static av_always_inline void decode_cabac_luma_residual(H264Context *h, H264Slic
  * Decode a macroblock.
  * @return 0 if OK, ER_AC_ERROR / ER_DC_ERROR / ER_MV_ERROR if an error is noticed
  */
-int ff_h264_decode_mb_cabac(H264Context *h, H264SliceContext *sl)
+int ff_h264_decode_mb_cabac(const H264Context *h, H264SliceContext *sl)
 {
+    const SPS *sps = h->ps.sps;
     int mb_xy;
     int mb_type, partition_count, cbp = 0;
-    int dct8x8_allowed= h->pps.transform_8x8_mode;
-    int decode_chroma = h->sps.chroma_format_idc == 1 || h->sps.chroma_format_idc == 2;
+    int dct8x8_allowed= h->ps.pps->transform_8x8_mode;
+    int decode_chroma = sps->chroma_format_idc == 1 || sps->chroma_format_idc == 2;
     const int pixel_shift = h->pixel_shift;
 
     mb_xy = sl->mb_xy = sl->mb_x + sl->mb_y*h->mb_stride;
 
-    tprintf(h->avctx, "pic:%d mb:%d/%d\n", h->frame_num, sl->mb_x, sl->mb_y);
     if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
         int skip;
         /* a skipped mb needs the aff flag from the following mb */
@@ -1930,7 +1930,7 @@ int ff_h264_decode_mb_cabac(H264Context *h, H264SliceContext *sl)
                 h->cur_pic.mb_type[mb_xy] = MB_TYPE_SKIP;
                 sl->next_mb_skipped = decode_cabac_mb_skip(h, sl, sl->mb_x, sl->mb_y+1 );
                 if(!sl->next_mb_skipped)
-                    h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h, sl);
+                    sl->mb_mbaff = sl->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h, sl);
             }
 
             decode_mb_skip(h, sl);
@@ -1945,13 +1945,13 @@ int ff_h264_decode_mb_cabac(H264Context *h, H264SliceContext *sl)
     }
     if (FRAME_MBAFF(h)) {
         if ((sl->mb_y & 1) == 0)
-            h->mb_mbaff =
-            h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h, sl);
+            sl->mb_mbaff =
+            sl->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h, sl);
     }
 
     sl->prev_mb_skipped = 0;
 
-    fill_decode_neighbors(h, sl, -(MB_FIELD(h)));
+    fill_decode_neighbors(h, sl, -(MB_FIELD(sl)));
 
     if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
         int ctx = 0;
@@ -1975,7 +1975,7 @@ int ff_h264_decode_mb_cabac(H264Context *h, H264SliceContext *sl)
             if( bits < 8 ){
                 mb_type= bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
             }else if( bits == 13 ){
-                mb_type = decode_cabac_intra_mb_type(h, sl, 32, 0);
+                mb_type = decode_cabac_intra_mb_type(sl, 32, 0);
                 goto decode_intra_mb;
             }else if( bits == 14 ){
                 mb_type= 11; /* B_L1_L0_8x16 */
@@ -1986,8 +1986,8 @@ int ff_h264_decode_mb_cabac(H264Context *h, H264SliceContext *sl)
                 mb_type= bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
             }
         }
-            partition_countb_mb_type_info[mb_type].partition_count;
-            mb_type=         b_mb_type_info[mb_type].type;
+            partition_count = ff_h264_b_mb_type_info[mb_type].partition_count;
+            mb_type         = ff_h264_b_mb_type_info[mb_type].type;
     } else if (sl->slice_type_nos == AV_PICTURE_TYPE_P) {
         if( get_cabac_noinline( &sl->cabac, &sl->cabac_state[14] ) == 0 ) {
             /* P-type */
@@ -1998,31 +1998,31 @@ int ff_h264_decode_mb_cabac(H264Context *h, H264SliceContext *sl)
                 /* P_L0_D8x16, P_L0_D16x8 */
                 mb_type= 2 - get_cabac_noinline( &sl->cabac, &sl->cabac_state[17] );
             }
-            partition_countp_mb_type_info[mb_type].partition_count;
-            mb_type=         p_mb_type_info[mb_type].type;
+            partition_count = ff_h264_p_mb_type_info[mb_type].partition_count;
+            mb_type         = ff_h264_p_mb_type_info[mb_type].type;
         } else {
-            mb_type = decode_cabac_intra_mb_type(h, sl, 17, 0);
+            mb_type = decode_cabac_intra_mb_type(sl, 17, 0);
             goto decode_intra_mb;
         }
     } else {
-        mb_type = decode_cabac_intra_mb_type(h, sl, 3, 1);
+        mb_type = decode_cabac_intra_mb_type(sl, 3, 1);
         if (sl->slice_type == AV_PICTURE_TYPE_SI && mb_type)
             mb_type--;
         assert(sl->slice_type_nos == AV_PICTURE_TYPE_I);
 decode_intra_mb:
         partition_count = 0;
-        cbpi_mb_type_info[mb_type].cbp;
-        sl->intra16x16_pred_mode = i_mb_type_info[mb_type].pred_mode;
-        mb_typei_mb_type_info[mb_type].type;
+        cbp                      = ff_h264_i_mb_type_info[mb_type].cbp;
+        sl->intra16x16_pred_mode = ff_h264_i_mb_type_info[mb_type].pred_mode;
+        mb_type                  = ff_h264_i_mb_type_info[mb_type].type;
     }
-    if(MB_FIELD(h))
+    if (MB_FIELD(sl))
         mb_type |= MB_TYPE_INTERLACED;
 
     h->slice_table[mb_xy] = sl->slice_num;
 
     if(IS_INTRA_PCM(mb_type)) {
-        const int mb_size = ff_h264_mb_sizes[h->sps.chroma_format_idc] *
-                            h->sps.bit_depth_luma >> 3;
+        const int mb_size = ff_h264_mb_sizes[sps->chroma_format_idc] *
+                            sps->bit_depth_luma >> 3;
         const uint8_t *ptr;
 
         // We assume these blocks are very rare so we do not optimize it.
@@ -2063,29 +2063,33 @@ decode_intra_mb:
                 mb_type |= MB_TYPE_8x8DCT;
                 for( i = 0; i < 16; i+=4 ) {
                     int pred = pred_intra_mode(h, sl, i);
-                    int mode = decode_cabac_mb_intra4x4_pred_mode(h, sl, pred);
+                    int mode = decode_cabac_mb_intra4x4_pred_mode(sl, pred);
                     fill_rectangle(&sl->intra4x4_pred_mode_cache[scan8[i]], 2, 2, 8, mode, 1);
                 }
             } else {
                 for( i = 0; i < 16; i++ ) {
                     int pred = pred_intra_mode(h, sl, i);
-                    sl->intra4x4_pred_mode_cache[scan8[i]] = decode_cabac_mb_intra4x4_pred_mode(h, sl, pred);
+                    sl->intra4x4_pred_mode_cache[scan8[i]] = decode_cabac_mb_intra4x4_pred_mode(sl, pred);
 
-                    av_dlog(h->avctx, "i4x4 pred=%d mode=%d\n", pred,
-                            h->intra4x4_pred_mode_cache[scan8[i]]);
+                    ff_dlog(h->avctx, "i4x4 pred=%d mode=%d\n", pred,
+                            sl->intra4x4_pred_mode_cache[scan8[i]]);
                 }
             }
             write_back_intra_pred_mode(h, sl);
-            if (ff_h264_check_intra4x4_pred_mode(h, sl) < 0 ) return -1;
+            if (ff_h264_check_intra4x4_pred_mode(sl->intra4x4_pred_mode_cache, h->avctx,
+                                                 sl->top_samples_available, sl->left_samples_available) < 0 )
+                return -1;
         } else {
-            sl->intra16x16_pred_mode = ff_h264_check_intra_pred_mode(h, sl, sl->intra16x16_pred_mode, 0);
+            sl->intra16x16_pred_mode = ff_h264_check_intra_pred_mode(h->avctx, sl->top_samples_available,
+                                                                     sl->left_samples_available, sl->intra16x16_pred_mode, 0);
             if (sl->intra16x16_pred_mode < 0) return -1;
         }
         if(decode_chroma){
             h->chroma_pred_mode_table[mb_xy] =
             pred_mode                        = decode_cabac_mb_chroma_pre_mode(h, sl);
 
-            pred_mode= ff_h264_check_intra_pred_mode(h, sl, pred_mode, 1 );
+            pred_mode= ff_h264_check_intra_pred_mode(h->avctx, sl->top_samples_available,
+                                                     sl->left_samples_available, pred_mode, 1 );
             if( pred_mode < 0 ) return -1;
             sl->chroma_pred_mode = pred_mode;
         } else {
@@ -2096,9 +2100,9 @@ decode_intra_mb:
 
         if (sl->slice_type_nos == AV_PICTURE_TYPE_B ) {
             for( i = 0; i < 4; i++ ) {
-                sl->sub_mb_type[i] = decode_cabac_b_mb_sub_type(h, sl);
-                sub_partition_count[i] = b_sub_mb_type_info[sl->sub_mb_type[i]].partition_count;
-                sl->sub_mb_type[i]     = b_sub_mb_type_info[sl->sub_mb_type[i]].type;
+                sl->sub_mb_type[i] = decode_cabac_b_mb_sub_type(sl);
+                sub_partition_count[i] = ff_h264_b_sub_mb_type_info[sl->sub_mb_type[i]].partition_count;
+                sl->sub_mb_type[i]     = ff_h264_b_sub_mb_type_info[sl->sub_mb_type[i]].type;
             }
             if (IS_DIRECT(sl->sub_mb_type[0] | sl->sub_mb_type[1] |
                           sl->sub_mb_type[2] | sl->sub_mb_type[3])) {
@@ -2112,9 +2116,9 @@ decode_intra_mb:
             }
         } else {
             for( i = 0; i < 4; i++ ) {
-                sl->sub_mb_type[i] = decode_cabac_p_mb_sub_type(h, sl);
-                sub_partition_count[i] = p_sub_mb_type_info[sl->sub_mb_type[i]].partition_count;
-                sl->sub_mb_type[i]     = p_sub_mb_type_info[sl->sub_mb_type[i]].type;
+                sl->sub_mb_type[i] = decode_cabac_p_mb_sub_type(sl);
+                sub_partition_count[i] = ff_h264_p_sub_mb_type_info[sl->sub_mb_type[i]].partition_count;
+                sl->sub_mb_type[i]     = ff_h264_p_sub_mb_type_info[sl->sub_mb_type[i]].type;
             }
         }
 
@@ -2122,9 +2126,9 @@ decode_intra_mb:
                 for( i = 0; i < 4; i++ ) {
                     if(IS_DIRECT(sl->sub_mb_type[i])) continue;
                     if(IS_DIR(sl->sub_mb_type[i], 0, list)){
-                        int rc = sl->ref_count[list] << MB_MBAFF(h);
+                        int rc = sl->ref_count[list] << MB_MBAFF(sl);
                         if (rc > 1) {
-                            ref[list][i] = decode_cabac_mb_ref(h, sl, list, 4 * i);
+                            ref[list][i] = decode_cabac_mb_ref(sl, list, 4 * i);
                             if (ref[list][i] >= (unsigned) rc) {
                                 av_log(h->avctx, AV_LOG_ERROR, "Reference %d >= %d\n", ref[list][i], rc);
                                 return -1;
@@ -2160,8 +2164,8 @@ decode_intra_mb:
                         int16_t (* mv_cache)[2] = &sl->mv_cache[list][ scan8[index] ];
                         uint8_t (* mvd_cache)[2]= &sl->mvd_cache[list][ scan8[index] ];
                         pred_motion(h, sl, index, block_width, list, sl->ref_cache[list][ scan8[index] ], &mx, &my);
-                        DECODE_CABAC_MB_MVD( h, list, index)
-                        tprintf(h->avctx, "final mv:%d %d\n", mx, my);
+                        DECODE_CABAC_MB_MVD(sl, list, index)
+                        ff_tlog(h->avctx, "final mv:%d %d\n", mx, my);
 
                         if(IS_SUB_8X8(sub_mb_type)){
                             mv_cache[ 1 ][0]=
@@ -2202,15 +2206,15 @@ decode_intra_mb:
         ff_h264_pred_direct_motion(h, sl, &mb_type);
         fill_rectangle(sl->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 2);
         fill_rectangle(sl->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 2);
-        dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
+        dct8x8_allowed &= sps->direct_8x8_inference_flag;
     } else {
         int list, i;
         if(IS_16X16(mb_type)){
             for (list = 0; list < sl->list_count; list++) {
                 if(IS_DIR(mb_type, 0, list)){
-                    int ref, rc = sl->ref_count[list] << MB_MBAFF(h);
+                    int ref, rc = sl->ref_count[list] << MB_MBAFF(sl);
                     if (rc > 1) {
-                        ref= decode_cabac_mb_ref(h, sl, list, 0);
+                        ref= decode_cabac_mb_ref(sl, list, 0);
                         if (ref >= (unsigned) rc) {
                             av_log(h->avctx, AV_LOG_ERROR, "Reference %d >= %d\n", ref, rc);
                             return -1;
@@ -2224,8 +2228,8 @@ decode_intra_mb:
                 if(IS_DIR(mb_type, 0, list)){
                     int mx,my,mpx,mpy;
                     pred_motion(h, sl, 0, 4, list, sl->ref_cache[list][ scan8[0] ], &mx, &my);
-                    DECODE_CABAC_MB_MVD( h, list, 0)
-                    tprintf(h->avctx, "final mv:%d %d\n", mx, my);
+                    DECODE_CABAC_MB_MVD(sl, list, 0)
+                    ff_tlog(h->avctx, "final mv:%d %d\n", mx, my);
 
                     fill_rectangle(sl->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack8to16(mpx,mpy), 2);
                     fill_rectangle(sl->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
@@ -2236,9 +2240,9 @@ decode_intra_mb:
             for (list = 0; list < sl->list_count; list++) {
                     for(i=0; i<2; i++){
                         if(IS_DIR(mb_type, i, list)){
-                            int ref, rc = sl->ref_count[list] << MB_MBAFF(h);
+                            int ref, rc = sl->ref_count[list] << MB_MBAFF(sl);
                             if (rc > 1) {
-                                ref= decode_cabac_mb_ref(h, sl, list, 8 * i);
+                                ref= decode_cabac_mb_ref(sl, list, 8 * i);
                                 if (ref >= (unsigned) rc) {
                                     av_log(h->avctx, AV_LOG_ERROR, "Reference %d >= %d\n", ref, rc);
                                     return -1;
@@ -2255,8 +2259,8 @@ decode_intra_mb:
                     if(IS_DIR(mb_type, i, list)){
                         int mx,my,mpx,mpy;
                         pred_16x8_motion(h, sl, 8*i, list, sl->ref_cache[list][scan8[0] + 16*i], &mx, &my);
-                        DECODE_CABAC_MB_MVD( h, list, 8*i)
-                        tprintf(h->avctx, "final mv:%d %d\n", mx, my);
+                        DECODE_CABAC_MB_MVD(sl, list, 8*i)
+                        ff_tlog(h->avctx, "final mv:%d %d\n", mx, my);
 
                         fill_rectangle(sl->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack8to16(mpx,mpy), 2);
                         fill_rectangle(sl->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
@@ -2271,9 +2275,9 @@ decode_intra_mb:
             for (list = 0; list < sl->list_count; list++) {
                     for(i=0; i<2; i++){
                         if(IS_DIR(mb_type, i, list)){ //FIXME optimize
-                            int ref, rc = sl->ref_count[list] << MB_MBAFF(h);
+                            int ref, rc = sl->ref_count[list] << MB_MBAFF(sl);
                             if (rc > 1) {
-                                ref= decode_cabac_mb_ref(h, sl, list, 4 * i);
+                                ref = decode_cabac_mb_ref(sl, list, 4 * i);
                                 if (ref >= (unsigned) rc) {
                                     av_log(h->avctx, AV_LOG_ERROR, "Reference %d >= %d\n", ref, rc);
                                     return -1;
@@ -2290,9 +2294,9 @@ decode_intra_mb:
                     if(IS_DIR(mb_type, i, list)){
                         int mx,my,mpx,mpy;
                         pred_8x16_motion(h, sl, i*4, list, sl->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
-                        DECODE_CABAC_MB_MVD( h, list, 4*i)
+                        DECODE_CABAC_MB_MVD(sl, list, 4*i)
 
-                        tprintf(h->avctx, "final mv:%d %d\n", mx, my);
+                        ff_tlog(h->avctx, "final mv:%d %d\n", mx, my);
                         fill_rectangle(sl->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack8to16(mpx,mpy), 2);
                         fill_rectangle(sl->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
                     }else{
@@ -2310,9 +2314,9 @@ decode_intra_mb:
    }
 
     if( !IS_INTRA16x16( mb_type ) ) {
-        cbp  = decode_cabac_mb_cbp_luma(h, sl);
+        cbp  = decode_cabac_mb_cbp_luma(sl);
         if(decode_chroma)
-            cbp |= decode_cabac_mb_cbp_chroma(h, sl) << 4;
+            cbp |= decode_cabac_mb_cbp_chroma(sl) << 4;
     }
 
     h->cbp_table[mb_xy] = sl->cbp = cbp;
@@ -2361,7 +2365,7 @@ decode_intra_mb:
         if(get_cabac_noinline( &sl->cabac, &sl->cabac_state[60 + (sl->last_qscale_diff != 0)])){
             int val = 1;
             int ctx= 2;
-            const int max_qp = 51 + 6*(h->sps.bit_depth_luma-8);
+            const int max_qp = 51 + 6*(sps->bit_depth_luma-8);
 
             while( get_cabac_noinline( &sl->cabac, &sl->cabac_state[60 + ctx] ) ) {
                 ctx= 3;
@@ -2397,14 +2401,14 @@ decode_intra_mb:
                 for (c = 0; c < 2; c++)
                     decode_cabac_residual_dc_422(h, sl, sl->mb + ((256 + 16*16*c) << pixel_shift), 3,
                                                  CHROMA_DC_BLOCK_INDEX + c,
-                                                 chroma422_dc_scan, 8);
+                                                 ff_h264_chroma422_dc_scan, 8);
             }
 
             if( cbp&0x20 ) {
                 int c, i, i8x8;
                 for( c = 0; c < 2; c++ ) {
                     int16_t *mb = sl->mb + (16*(16 + 16*c) << pixel_shift);
-                    qmul = h->dequant4_coeff[c+1+(IS_INTRA( mb_type ) ? 0:3)][sl->chroma_qp[c]];
+                    qmul = h->ps.pps->dequant4_coeff[c+1+(IS_INTRA( mb_type ) ? 0:3)][sl->chroma_qp[c]];
                     for (i8x8 = 0; i8x8 < 2; i8x8++) {
                         for (i = 0; i < 4; i++) {
                             const int index = 16 + 16 * c + 8*i8x8 + i;
@@ -2421,13 +2425,14 @@ decode_intra_mb:
             if( cbp&0x30 ){
                 int c;
                 for (c = 0; c < 2; c++)
-                    decode_cabac_residual_dc(h, sl, sl->mb + ((256 + 16*16*c) << pixel_shift), 3, CHROMA_DC_BLOCK_INDEX+c, chroma_dc_scan, 4);
+                    decode_cabac_residual_dc(h, sl, sl->mb + ((256 + 16 * 16 * c) << pixel_shift),
+                                             3, CHROMA_DC_BLOCK_INDEX + c, ff_h264_chroma_dc_scan, 4);
             }
 
             if( cbp&0x20 ) {
                 int c, i;
                 for( c = 0; c < 2; c++ ) {
-                    qmul = h->dequant4_coeff[c+1+(IS_INTRA( mb_type ) ? 0:3)][sl->chroma_qp[c]];
+                    qmul = h->ps.pps->dequant4_coeff[c+1+(IS_INTRA( mb_type ) ? 0:3)][sl->chroma_qp[c]];
                     for( i = 0; i < 4; i++ ) {
                         const int index = 16 + 16 * c + i;
                         decode_cabac_residual_nondc(h, sl, sl->mb + (16*index << pixel_shift), 4, index, scan + 1, qmul, 15);