h264: use a separate GetBitContext for slice data
authorAnton Khirnov <anton@khirnov.net>
Tue, 27 Jan 2015 13:39:06 +0000 (14:39 +0100)
committerAnton Khirnov <anton@khirnov.net>
Sat, 21 Mar 2015 10:27:14 +0000 (11:27 +0100)
libavcodec/h264.c
libavcodec/h264.h
libavcodec/h264_cavlc.c
libavcodec/h264_parser.c
libavcodec/h264_refs.c
libavcodec/h264_slice.c

index 6b72807..59c822c 100644 (file)
@@ -1007,9 +1007,9 @@ int ff_pred_weight_table(H264Context *h, H264SliceContext *sl)
 
     sl->use_weight             = 0;
     sl->use_weight_chroma      = 0;
-    sl->luma_log2_weight_denom = get_ue_golomb(&h->gb);
+    sl->luma_log2_weight_denom = get_ue_golomb(&sl->gb);
     if (h->sps.chroma_format_idc)
-        sl->chroma_log2_weight_denom = get_ue_golomb(&h->gb);
+        sl->chroma_log2_weight_denom = get_ue_golomb(&sl->gb);
     luma_def   = 1 << sl->luma_log2_weight_denom;
     chroma_def = 1 << sl->chroma_log2_weight_denom;
 
@@ -1019,10 +1019,10 @@ int ff_pred_weight_table(H264Context *h, H264SliceContext *sl)
         for (i = 0; i < sl->ref_count[list]; i++) {
             int luma_weight_flag, chroma_weight_flag;
 
-            luma_weight_flag = get_bits1(&h->gb);
+            luma_weight_flag = get_bits1(&sl->gb);
             if (luma_weight_flag) {
-                sl->luma_weight[i][list][0] = get_se_golomb(&h->gb);
-                sl->luma_weight[i][list][1] = get_se_golomb(&h->gb);
+                sl->luma_weight[i][list][0] = get_se_golomb(&sl->gb);
+                sl->luma_weight[i][list][1] = get_se_golomb(&sl->gb);
                 if (sl->luma_weight[i][list][0] != luma_def ||
                     sl->luma_weight[i][list][1] != 0) {
                     sl->use_weight             = 1;
@@ -1034,12 +1034,12 @@ int ff_pred_weight_table(H264Context *h, H264SliceContext *sl)
             }
 
             if (h->sps.chroma_format_idc) {
-                chroma_weight_flag = get_bits1(&h->gb);
+                chroma_weight_flag = get_bits1(&sl->gb);
                 if (chroma_weight_flag) {
                     int j;
                     for (j = 0; j < 2; j++) {
-                        sl->chroma_weight[i][list][j][0] = get_se_golomb(&h->gb);
-                        sl->chroma_weight[i][list][j][1] = get_se_golomb(&h->gb);
+                        sl->chroma_weight[i][list][j][0] = get_se_golomb(&sl->gb);
+                        sl->chroma_weight[i][list][j][1] = get_se_golomb(&sl->gb);
                         if (sl->chroma_weight[i][list][j][0] != chroma_def ||
                             sl->chroma_weight[i][list][j][1] != 0) {
                             sl->use_weight_chroma        = 1;
@@ -1267,15 +1267,15 @@ int ff_set_ref_count(H264Context *h, H264SliceContext *sl)
 
     if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
         if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
-            sl->direct_spatial_mv_pred = get_bits1(&h->gb);
-        num_ref_idx_active_override_flag = get_bits1(&h->gb);
+            sl->direct_spatial_mv_pred = get_bits1(&sl->gb);
+        num_ref_idx_active_override_flag = get_bits1(&sl->gb);
 
         if (num_ref_idx_active_override_flag) {
-            ref_count[0] = get_ue_golomb(&h->gb) + 1;
+            ref_count[0] = get_ue_golomb(&sl->gb) + 1;
             if (ref_count[0] < 1)
                 return AVERROR_INVALIDDATA;
             if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
-                ref_count[1] = get_ue_golomb(&h->gb) + 1;
+                ref_count[1] = get_ue_golomb(&sl->gb) + 1;
                 if (ref_count[1] < 1)
                     return AVERROR_INVALIDDATA;
             }
@@ -1528,7 +1528,7 @@ again:
                 }
                 idr(h); // FIXME ensure we don't lose some frames if there is reordering
             case NAL_SLICE:
-                init_get_bits(&hx->gb, ptr, bit_length);
+                init_get_bits(&sl->gb, ptr, bit_length);
 
                 if ((err = ff_h264_decode_slice_header(hx, sl, h)))
                     break;
index 06a4ab0..8483c15 100644 (file)
@@ -298,6 +298,7 @@ typedef struct H264Picture {
 
 typedef struct H264SliceContext {
     struct H264Context *h264;
+    GetBitContext gb;
 
     int slice_num;
     int slice_type;
index 6b0c7b7..265927b 100644 (file)
@@ -710,12 +710,12 @@ int ff_h264_decode_mb_cavlc(H264Context *h, H264SliceContext *sl)
                 down the code */
     if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
         if (sl->mb_skip_run == -1)
-            sl->mb_skip_run = get_ue_golomb(&h->gb);
+            sl->mb_skip_run = get_ue_golomb(&sl->gb);
 
         if (sl->mb_skip_run--) {
             if (FRAME_MBAFF(h) && (sl->mb_y & 1) == 0) {
                 if (sl->mb_skip_run == 0)
-                    h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&h->gb);
+                    h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&sl->gb);
             }
             decode_mb_skip(h, sl);
             return 0;
@@ -723,12 +723,12 @@ int ff_h264_decode_mb_cavlc(H264Context *h, H264SliceContext *sl)
     }
     if (FRAME_MBAFF(h)) {
         if ((sl->mb_y & 1) == 0)
-            h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&h->gb);
+            h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&sl->gb);
     }
 
     sl->prev_mb_skipped = 0;
 
-    mb_type= get_ue_golomb(&h->gb);
+    mb_type= get_ue_golomb(&sl->gb);
     if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
         if(mb_type < 23){
             partition_count= b_mb_type_info[mb_type].partition_count;
@@ -770,12 +770,12 @@ decode_intra_mb:
                             h->sps.bit_depth_luma;
 
         // We assume these blocks are very rare so we do not optimize it.
-        sl->intra_pcm_ptr = align_get_bits(&h->gb);
-        if (get_bits_left(&h->gb) < mb_size) {
+        sl->intra_pcm_ptr = align_get_bits(&sl->gb);
+        if (get_bits_left(&sl->gb) < mb_size) {
             av_log(h->avctx, AV_LOG_ERROR, "Not enough data for an intra PCM block.\n");
             return AVERROR_INVALIDDATA;
         }
-        skip_bits_long(&h->gb, mb_size);
+        skip_bits_long(&sl->gb, mb_size);
 
         // In deblocking, the quantizer is 0
         h->cur_pic.qscale_table[mb_xy] = 0;
@@ -796,7 +796,7 @@ decode_intra_mb:
         if(IS_INTRA4x4(mb_type)){
             int i;
             int di = 1;
-            if(dct8x8_allowed && get_bits1(&h->gb)){
+            if(dct8x8_allowed && get_bits1(&sl->gb)){
                 mb_type |= MB_TYPE_8x8DCT;
                 di = 4;
             }
@@ -805,8 +805,8 @@ decode_intra_mb:
             for(i=0; i<16; i+=di){
                 int mode = pred_intra_mode(h, sl, i);
 
-                if(!get_bits1(&h->gb)){
-                    const int rem_mode= get_bits(&h->gb, 3);
+                if(!get_bits1(&sl->gb)){
+                    const int rem_mode= get_bits(&sl->gb, 3);
                     mode = rem_mode + (rem_mode >= mode);
                 }
 
@@ -824,7 +824,7 @@ decode_intra_mb:
                 return -1;
         }
         if(decode_chroma){
-            pred_mode= ff_h264_check_intra_pred_mode(h, sl, get_ue_golomb_31(&h->gb), 1);
+            pred_mode= ff_h264_check_intra_pred_mode(h, sl, get_ue_golomb_31(&sl->gb), 1);
             if(pred_mode < 0)
                 return -1;
             sl->chroma_pred_mode = pred_mode;
@@ -836,7 +836,7 @@ decode_intra_mb:
 
         if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
             for(i=0; i<4; i++){
-                sl->sub_mb_type[i]= get_ue_golomb_31(&h->gb);
+                sl->sub_mb_type[i]= get_ue_golomb_31(&sl->gb);
                 if(sl->sub_mb_type[i] >=13){
                     av_log(h->avctx, AV_LOG_ERROR, "B sub_mb_type %u out of range at %d %d\n", sl->sub_mb_type[i], sl->mb_x, sl->mb_y);
                     return -1;
@@ -854,7 +854,7 @@ decode_intra_mb:
         }else{
             assert(sl->slice_type_nos == AV_PICTURE_TYPE_P); //FIXME SP correct ?
             for(i=0; i<4; i++){
-                sl->sub_mb_type[i]= get_ue_golomb_31(&h->gb);
+                sl->sub_mb_type[i]= get_ue_golomb_31(&sl->gb);
                 if(sl->sub_mb_type[i] >=4){
                     av_log(h->avctx, AV_LOG_ERROR, "P sub_mb_type %u out of range at %d %d\n", sl->sub_mb_type[i], sl->mb_x, sl->mb_y);
                     return -1;
@@ -873,9 +873,9 @@ decode_intra_mb:
                     if(ref_count == 1){
                         tmp= 0;
                     }else if(ref_count == 2){
-                        tmp= get_bits1(&h->gb)^1;
+                        tmp= get_bits1(&sl->gb)^1;
                     }else{
-                        tmp= get_ue_golomb_31(&h->gb);
+                        tmp= get_ue_golomb_31(&sl->gb);
                         if(tmp>=ref_count){
                             av_log(h->avctx, AV_LOG_ERROR, "ref %u overflow\n", tmp);
                             return -1;
@@ -909,8 +909,8 @@ decode_intra_mb:
                         const int index= 4*i + block_width*j;
                         int16_t (* mv_cache)[2]= &sl->mv_cache[list][ scan8[index] ];
                         pred_motion(h, sl, index, block_width, list, sl->ref_cache[list][ scan8[index] ], &mx, &my);
-                        mx += get_se_golomb(&h->gb);
-                        my += get_se_golomb(&h->gb);
+                        mx += get_se_golomb(&sl->gb);
+                        my += get_se_golomb(&sl->gb);
                         tprintf(h->avctx, "final mv:%d %d\n", mx, my);
 
                         if(IS_SUB_8X8(sub_mb_type)){
@@ -949,9 +949,9 @@ decode_intra_mb:
                         if (rc == 1) {
                             val= 0;
                         } else if (rc == 2) {
-                            val= get_bits1(&h->gb)^1;
+                            val= get_bits1(&sl->gb)^1;
                         }else{
-                            val= get_ue_golomb_31(&h->gb);
+                            val= get_ue_golomb_31(&sl->gb);
                             if (val >= rc) {
                                 av_log(h->avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
                                 return -1;
@@ -963,8 +963,8 @@ decode_intra_mb:
             for (list = 0; list < sl->list_count; list++) {
                 if(IS_DIR(mb_type, 0, list)){
                     pred_motion(h, sl, 0, 4, list, sl->ref_cache[list][ scan8[0] ], &mx, &my);
-                    mx += get_se_golomb(&h->gb);
-                    my += get_se_golomb(&h->gb);
+                    mx += get_se_golomb(&sl->gb);
+                    my += get_se_golomb(&sl->gb);
                     tprintf(h->avctx, "final mv:%d %d\n", mx, my);
 
                     fill_rectangle(sl->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
@@ -980,9 +980,9 @@ decode_intra_mb:
                             if (rc == 1) {
                                 val= 0;
                             } else if (rc == 2) {
-                                val= get_bits1(&h->gb)^1;
+                                val= get_bits1(&sl->gb)^1;
                             }else{
-                                val= get_ue_golomb_31(&h->gb);
+                                val= get_ue_golomb_31(&sl->gb);
                                 if (val >= rc) {
                                     av_log(h->avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
                                     return -1;
@@ -998,8 +998,8 @@ decode_intra_mb:
                     unsigned int val;
                     if(IS_DIR(mb_type, i, list)){
                         pred_16x8_motion(h, sl, 8*i, list, sl->ref_cache[list][scan8[0] + 16*i], &mx, &my);
-                        mx += get_se_golomb(&h->gb);
-                        my += get_se_golomb(&h->gb);
+                        mx += get_se_golomb(&sl->gb);
+                        my += get_se_golomb(&sl->gb);
                         tprintf(h->avctx, "final mv:%d %d\n", mx, my);
 
                         val= pack16to32(mx,my);
@@ -1018,9 +1018,9 @@ decode_intra_mb:
                             if (rc == 1) {
                                 val= 0;
                             } else if (rc == 2) {
-                                val= get_bits1(&h->gb)^1;
+                                val= get_bits1(&sl->gb)^1;
                             }else{
-                                val= get_ue_golomb_31(&h->gb);
+                                val= get_ue_golomb_31(&sl->gb);
                                 if (val >= rc) {
                                     av_log(h->avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
                                     return -1;
@@ -1036,8 +1036,8 @@ decode_intra_mb:
                     unsigned int val;
                     if(IS_DIR(mb_type, i, list)){
                         pred_8x16_motion(h, sl, i*4, list, sl->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
-                        mx += get_se_golomb(&h->gb);
-                        my += get_se_golomb(&h->gb);
+                        mx += get_se_golomb(&sl->gb);
+                        my += get_se_golomb(&sl->gb);
                         tprintf(h->avctx, "final mv:%d %d\n", mx, my);
 
                         val= pack16to32(mx,my);
@@ -1053,7 +1053,7 @@ decode_intra_mb:
         write_back_motion(h, sl, mb_type);
 
     if(!IS_INTRA16x16(mb_type)){
-        cbp= get_ue_golomb(&h->gb);
+        cbp= get_ue_golomb(&sl->gb);
 
         if(decode_chroma){
             if(cbp > 47){
@@ -1073,7 +1073,7 @@ decode_intra_mb:
     }
 
     if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
-        mb_type |= MB_TYPE_8x8DCT*get_bits1(&h->gb);
+        mb_type |= MB_TYPE_8x8DCT*get_bits1(&sl->gb);
     }
     sl->cbp=
     h->cbp_table[mb_xy]= cbp;
@@ -1083,7 +1083,7 @@ decode_intra_mb:
         int i4x4, i8x8, chroma_idx;
         int dquant;
         int ret;
-        GetBitContext *gb = &h->gb;
+        GetBitContext *gb = &sl->gb;
         const uint8_t *scan, *scan8x8;
         const int max_qp = 51 + 6*(h->sps.bit_depth_luma-8);
 
@@ -1095,7 +1095,7 @@ decode_intra_mb:
             scan    = sl->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
         }
 
-        dquant= get_se_golomb(&h->gb);
+        dquant= get_se_golomb(&sl->gb);
 
         sl->qscale += dquant;
 
index 0a38260..e652806 100644 (file)
@@ -107,7 +107,7 @@ static int scan_mmco_reset(AVCodecParserContext *s)
     sl->slice_type_nos = s->pict_type & 3;
 
     if (h->pps.redundant_pic_cnt_present)
-        get_ue_golomb(&h->gb); // redundant_pic_count
+        get_ue_golomb(&sl->gb); // redundant_pic_count
 
     if (ff_set_ref_count(h, sl) < 0)
         return AVERROR_INVALIDDATA;
@@ -115,13 +115,13 @@ static int scan_mmco_reset(AVCodecParserContext *s)
     if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
         int list;
         for (list = 0; list < sl->list_count; list++) {
-            if (get_bits1(&h->gb)) {
+            if (get_bits1(&sl->gb)) {
                 int index;
                 for (index = 0; ; index++) {
-                    unsigned int reordering_of_pic_nums_idc = get_ue_golomb_31(&h->gb);
+                    unsigned int reordering_of_pic_nums_idc = get_ue_golomb_31(&sl->gb);
 
                     if (reordering_of_pic_nums_idc < 3)
-                        get_ue_golomb(&h->gb);
+                        get_ue_golomb(&sl->gb);
                     else if (reordering_of_pic_nums_idc > 3) {
                         av_log(h->avctx, AV_LOG_ERROR,
                                "illegal reordering_of_pic_nums_idc %d\n",
@@ -144,10 +144,10 @@ static int scan_mmco_reset(AVCodecParserContext *s)
         (h->pps.weighted_bipred_idc == 1 && sl->slice_type_nos == AV_PICTURE_TYPE_B))
         ff_pred_weight_table(h, sl);
 
-    if (get_bits1(&h->gb)) { // adaptive_ref_pic_marking_mode_flag
+    if (get_bits1(&sl->gb)) { // adaptive_ref_pic_marking_mode_flag
         int i;
         for (i = 0; i < MAX_MMCO_COUNT; i++) {
-            MMCOOpcode opcode = get_ue_golomb_31(&h->gb);
+            MMCOOpcode opcode = get_ue_golomb_31(&sl->gb);
             if (opcode > (unsigned) MMCO_LONG) {
                 av_log(h->avctx, AV_LOG_ERROR,
                        "illegal memory management control operation %d\n",
@@ -160,10 +160,10 @@ static int scan_mmco_reset(AVCodecParserContext *s)
                 return 1;
 
             if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG)
-                get_ue_golomb(&h->gb);
+                get_ue_golomb(&sl->gb);
             if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED ||
                 opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG)
-                get_ue_golomb_31(&h->gb);
+                get_ue_golomb_31(&sl->gb);
         }
     }
 
@@ -184,6 +184,7 @@ static inline int parse_nal_units(AVCodecParserContext *s,
 {
     H264ParseContext *p = s->priv_data;
     H264Context      *h = &p->h;
+    H264SliceContext *sl = &h->slice_ctx[0];
     const uint8_t *buf_end = buf + buf_size;
     unsigned int pps_id;
     unsigned int slice_type;
@@ -249,14 +250,15 @@ static inline int parse_nal_units(AVCodecParserContext *s,
             h->prev_poc_lsb          = 0;
         /* fall through */
         case NAL_SLICE:
-            get_ue_golomb(&h->gb);  // skip first_mb_in_slice
-            slice_type   = get_ue_golomb_31(&h->gb);
+            init_get_bits(&sl->gb, ptr, 8 * dst_length);
+            get_ue_golomb(&sl->gb);  // skip first_mb_in_slice
+            slice_type   = get_ue_golomb_31(&sl->gb);
             s->pict_type = golomb_to_pict_type[slice_type % 5];
             if (h->sei_recovery_frame_cnt >= 0) {
                 /* key frame, since recovery_frame_cnt is set */
                 s->key_frame = 1;
             }
-            pps_id = get_ue_golomb(&h->gb);
+            pps_id = get_ue_golomb(&sl->gb);
             if (pps_id >= MAX_PPS_COUNT) {
                 av_log(h->avctx, AV_LOG_ERROR,
                        "pps_id %u out of range\n", pps_id);
@@ -274,7 +276,7 @@ static inline int parse_nal_units(AVCodecParserContext *s,
                 return -1;
             }
             h->sps       = *h->sps_buffers[h->pps.sps_id];
-            h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
+            h->frame_num = get_bits(&sl->gb, h->sps.log2_max_frame_num);
 
             s->coded_width  = 16 * h->sps.mb_width;
             s->coded_height = 16 * h->sps.mb_height;
@@ -311,30 +313,30 @@ static inline int parse_nal_units(AVCodecParserContext *s,
             if (h->sps.frame_mbs_only_flag) {
                 h->picture_structure = PICT_FRAME;
             } else {
-                if (get_bits1(&h->gb)) { // field_pic_flag
-                    h->picture_structure = PICT_TOP_FIELD + get_bits1(&h->gb); // bottom_field_flag
+                if (get_bits1(&sl->gb)) { // field_pic_flag
+                    h->picture_structure = PICT_TOP_FIELD + get_bits1(&sl->gb); // bottom_field_flag
                 } else {
                     h->picture_structure = PICT_FRAME;
                 }
             }
 
             if (h->nal_unit_type == NAL_IDR_SLICE)
-                get_ue_golomb(&h->gb); /* idr_pic_id */
+                get_ue_golomb(&sl->gb); /* idr_pic_id */
             if (h->sps.poc_type == 0) {
-                h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
+                h->poc_lsb = get_bits(&sl->gb, h->sps.log2_max_poc_lsb);
 
                 if (h->pps.pic_order_present == 1 &&
                     h->picture_structure == PICT_FRAME)
-                    h->delta_poc_bottom = get_se_golomb(&h->gb);
+                    h->delta_poc_bottom = get_se_golomb(&sl->gb);
             }
 
             if (h->sps.poc_type == 1 &&
                 !h->sps.delta_pic_order_always_zero_flag) {
-                h->delta_poc[0] = get_se_golomb(&h->gb);
+                h->delta_poc[0] = get_se_golomb(&sl->gb);
 
                 if (h->pps.pic_order_present == 1 &&
                     h->picture_structure == PICT_FRAME)
-                    h->delta_poc[1] = get_se_golomb(&h->gb);
+                    h->delta_poc[1] = get_se_golomb(&sl->gb);
             }
 
             /* Decode POC of this picture.
index 271264c..04cfa9e 100644 (file)
@@ -223,11 +223,11 @@ int ff_h264_decode_ref_pic_list_reordering(H264Context *h, H264SliceContext *sl)
         for (i = 0; i < sl->ref_count[list]; i++)
             COPY_PICTURE(&sl->ref_list[list][i], &h->default_ref_list[list][i]);
 
-        if (get_bits1(&h->gb)) {    // ref_pic_list_modification_flag_l[01]
+        if (get_bits1(&sl->gb)) {    // ref_pic_list_modification_flag_l[01]
             int pred = h->curr_pic_num;
 
             for (index = 0; ; index++) {
-                unsigned int modification_of_pic_nums_idc = get_ue_golomb_31(&h->gb);
+                unsigned int modification_of_pic_nums_idc = get_ue_golomb_31(&sl->gb);
                 unsigned int pic_id;
                 int i;
                 H264Picture *ref = NULL;
@@ -243,7 +243,7 @@ int ff_h264_decode_ref_pic_list_reordering(H264Context *h, H264SliceContext *sl)
                 switch (modification_of_pic_nums_idc) {
                 case 0:
                 case 1: {
-                    const unsigned int abs_diff_pic_num = get_ue_golomb(&h->gb) + 1;
+                    const unsigned int abs_diff_pic_num = get_ue_golomb(&sl->gb) + 1;
                     int frame_num;
 
                     if (abs_diff_pic_num > h->max_pic_num) {
@@ -274,7 +274,7 @@ int ff_h264_decode_ref_pic_list_reordering(H264Context *h, H264SliceContext *sl)
                 }
                 case 2: {
                     int long_idx;
-                    pic_id = get_ue_golomb(&h->gb); // long_term_pic_idx
+                    pic_id = get_ue_golomb(&sl->gb); // long_term_pic_idx
 
                     long_idx = pic_num_extract(h, pic_id, &pic_structure);
 
index e409847..7ad25b9 100644 (file)
@@ -1186,7 +1186,7 @@ int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl, H264Contex
     h->qpel_put = h->h264qpel.put_h264_qpel_pixels_tab;
     h->qpel_avg = h->h264qpel.avg_h264_qpel_pixels_tab;
 
-    first_mb_in_slice = get_ue_golomb(&h->gb);
+    first_mb_in_slice = get_ue_golomb(&sl->gb);
 
     if (first_mb_in_slice == 0) { // FIXME better field boundary detection
         if (h0->current_slice && h->cur_pic_ptr && FIELD_PICTURE(h)) {
@@ -1203,7 +1203,7 @@ int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl, H264Contex
         }
     }
 
-    slice_type = get_ue_golomb_31(&h->gb);
+    slice_type = get_ue_golomb_31(&sl->gb);
     if (slice_type > 9) {
         av_log(h->avctx, AV_LOG_ERROR,
                "slice type %d too large at %d\n",
@@ -1233,7 +1233,7 @@ int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl, H264Contex
     // to make a few old functions happy, it's wrong though
     h->pict_type = sl->slice_type;
 
-    pps_id = get_ue_golomb(&h->gb);
+    pps_id = get_ue_golomb(&sl->gb);
     if (pps_id >= MAX_PPS_COUNT) {
         av_log(h->avctx, AV_LOG_ERROR, "pps_id %u out of range\n", pps_id);
         return AVERROR_INVALIDDATA;
@@ -1354,7 +1354,7 @@ int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl, H264Contex
         h264_init_dequant_tables(h);
     }
 
-    h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
+    h->frame_num = get_bits(&sl->gb, h->sps.log2_max_frame_num);
 
     h->mb_mbaff        = 0;
     h->mb_aff_frame    = 0;
@@ -1364,9 +1364,9 @@ int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl, H264Contex
     if (h->sps.frame_mbs_only_flag) {
         h->picture_structure = PICT_FRAME;
     } else {
-        field_pic_flag = get_bits1(&h->gb);
+        field_pic_flag = get_bits1(&sl->gb);
         if (field_pic_flag) {
-            bottom_field_flag = get_bits1(&h->gb);
+            bottom_field_flag = get_bits1(&sl->gb);
             h->picture_structure = PICT_TOP_FIELD + bottom_field_flag;
         } else {
             h->picture_structure = PICT_FRAME;
@@ -1571,26 +1571,26 @@ int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl, H264Contex
     }
 
     if (h->nal_unit_type == NAL_IDR_SLICE)
-        get_ue_golomb(&h->gb); /* idr_pic_id */
+        get_ue_golomb(&sl->gb); /* idr_pic_id */
 
     if (h->sps.poc_type == 0) {
-        h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
+        h->poc_lsb = get_bits(&sl->gb, h->sps.log2_max_poc_lsb);
 
         if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)
-            h->delta_poc_bottom = get_se_golomb(&h->gb);
+            h->delta_poc_bottom = get_se_golomb(&sl->gb);
     }
 
     if (h->sps.poc_type == 1 && !h->sps.delta_pic_order_always_zero_flag) {
-        h->delta_poc[0] = get_se_golomb(&h->gb);
+        h->delta_poc[0] = get_se_golomb(&sl->gb);
 
         if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)
-            h->delta_poc[1] = get_se_golomb(&h->gb);
+            h->delta_poc[1] = get_se_golomb(&sl->gb);
     }
 
     ff_init_poc(h, h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc);
 
     if (h->pps.redundant_pic_cnt_present)
-        sl->redundant_pic_count = get_ue_golomb(&h->gb);
+        sl->redundant_pic_count = get_ue_golomb(&sl->gb);
 
     ret = ff_set_ref_count(h, sl);
     if (ret < 0)
@@ -1630,7 +1630,7 @@ int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl, H264Contex
     // further down the line. This may break decoding if the first slice is
     // corrupt, thus we only do this if frame-mt is enabled.
     if (h->nal_ref_idc) {
-        ret = ff_h264_decode_ref_pic_marking(h0, &h->gb,
+        ret = ff_h264_decode_ref_pic_marking(h0, &sl->gb,
                                              !(h->avctx->active_thread_type & FF_THREAD_FRAME) ||
                                              h0->current_slice == 0);
         if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
@@ -1651,7 +1651,7 @@ int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl, H264Contex
     ff_h264_direct_ref_list_init(h, sl);
 
     if (sl->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac) {
-        tmp = get_ue_golomb_31(&h->gb);
+        tmp = get_ue_golomb_31(&sl->gb);
         if (tmp > 2) {
             av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\n", tmp);
             return AVERROR_INVALIDDATA;
@@ -1660,7 +1660,7 @@ int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl, H264Contex
     }
 
     sl->last_qscale_diff = 0;
-    tmp = h->pps.init_qp + get_se_golomb(&h->gb);
+    tmp = h->pps.init_qp + get_se_golomb(&sl->gb);
     if (tmp > 51 + 6 * (h->sps.bit_depth_luma - 8)) {
         av_log(h->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
         return AVERROR_INVALIDDATA;
@@ -1670,16 +1670,16 @@ int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl, H264Contex
     sl->chroma_qp[1] = get_chroma_qp(h, 1, sl->qscale);
     // FIXME qscale / qp ... stuff
     if (sl->slice_type == AV_PICTURE_TYPE_SP)
-        get_bits1(&h->gb); /* sp_for_switch_flag */
+        get_bits1(&sl->gb); /* sp_for_switch_flag */
     if (sl->slice_type == AV_PICTURE_TYPE_SP ||
         sl->slice_type == AV_PICTURE_TYPE_SI)
-        get_se_golomb(&h->gb); /* slice_qs_delta */
+        get_se_golomb(&sl->gb); /* slice_qs_delta */
 
     sl->deblocking_filter     = 1;
     sl->slice_alpha_c0_offset = 0;
     sl->slice_beta_offset     = 0;
     if (h->pps.deblocking_filter_parameters_present) {
-        tmp = get_ue_golomb_31(&h->gb);
+        tmp = get_ue_golomb_31(&sl->gb);
         if (tmp > 2) {
             av_log(h->avctx, AV_LOG_ERROR,
                    "deblocking_filter_idc %u out of range\n", tmp);
@@ -1690,8 +1690,8 @@ int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl, H264Contex
             sl->deblocking_filter ^= 1;  // 1<->0
 
         if (sl->deblocking_filter) {
-            sl->slice_alpha_c0_offset = get_se_golomb(&h->gb) * 2;
-            sl->slice_beta_offset     = get_se_golomb(&h->gb) * 2;
+            sl->slice_alpha_c0_offset = get_se_golomb(&sl->gb) * 2;
+            sl->slice_beta_offset     = get_se_golomb(&sl->gb) * 2;
             if (sl->slice_alpha_c0_offset >  12 ||
                 sl->slice_alpha_c0_offset < -12 ||
                 sl->slice_beta_offset >  12     ||
@@ -2190,12 +2190,12 @@ static int decode_slice(struct AVCodecContext *avctx, void *arg)
 
     if (h->pps.cabac) {
         /* realign */
-        align_get_bits(&h->gb);
+        align_get_bits(&sl->gb);
 
         /* init cabac */
         ff_init_cabac_decoder(&sl->cabac,
-                              h->gb.buffer + get_bits_count(&h->gb) / 8,
-                              (get_bits_left(&h->gb) + 7) / 8);
+                              sl->gb.buffer + get_bits_count(&sl->gb) / 8,
+                              (get_bits_left(&sl->gb) + 7) / 8);
 
         ff_h264_init_cabac_states(h, sl);
 
@@ -2252,7 +2252,7 @@ static int decode_slice(struct AVCodecContext *avctx, void *arg)
 
             if (eos || sl->mb_y >= h->mb_height) {
                 tprintf(h->avctx, "slice end %d %d\n",
-                        get_bits_count(&h->gb), h->gb.size_in_bits);
+                        get_bits_count(&sl->gb), sl->gb.size_in_bits);
                 er_add_slice(h, sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1,
                              sl->mb_y, ER_MB_END);
                 if (sl->mb_x > lf_x_start)
@@ -2297,9 +2297,9 @@ static int decode_slice(struct AVCodecContext *avctx, void *arg)
                 }
                 if (sl->mb_y >= h->mb_height) {
                     tprintf(h->avctx, "slice end %d %d\n",
-                            get_bits_count(&h->gb), h->gb.size_in_bits);
+                            get_bits_count(&sl->gb), sl->gb.size_in_bits);
 
-                    if (get_bits_left(&h->gb) == 0) {
+                    if (get_bits_left(&sl->gb) == 0) {
                         er_add_slice(h, sl, sl->resync_mb_x, sl->resync_mb_y,
                                      sl->mb_x - 1, sl->mb_y, ER_MB_END);
 
@@ -2313,11 +2313,11 @@ static int decode_slice(struct AVCodecContext *avctx, void *arg)
                 }
             }
 
-            if (get_bits_left(&h->gb) <= 0 && sl->mb_skip_run <= 0) {
+            if (get_bits_left(&sl->gb) <= 0 && sl->mb_skip_run <= 0) {
                 tprintf(h->avctx, "slice end %d %d\n",
-                        get_bits_count(&h->gb), h->gb.size_in_bits);
+                        get_bits_count(&sl->gb), sl->gb.size_in_bits);
 
-                if (get_bits_left(&h->gb) == 0) {
+                if (get_bits_left(&sl->gb) == 0) {
                     er_add_slice(h, sl, sl->resync_mb_x, sl->resync_mb_y,
                                  sl->mb_x - 1, sl->mb_y, ER_MB_END);
                     if (sl->mb_x > lf_x_start)