int ff_h264_decode_slice_header(H264Context *h, H264Context *h0)

{

    unsigned int first_mb_in_slice;

    unsigned int pps_id;

    int ret;

    unsigned int slice_type, tmp, i, j;

    int last_pic_structure, last_pic_droppable;

    int must_reinit;

    int needs_reinit = 0;

    int field_pic_flag, bottom_field_flag;



    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_long(&h->gb);



    if (first_mb_in_slice == 0) { // FIXME better field boundary detection

        if (h0->current_slice && h->cur_pic_ptr && FIELD_PICTURE(h)) {

            ff_h264_field_end(h, 1);

        }



        h0->current_slice = 0;

        if (!h0->first_field) {

            if (h->cur_pic_ptr && !h->droppable) {

                ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,

                                          h->picture_structure == PICT_BOTTOM_FIELD);

            }

            h->cur_pic_ptr = NULL;

        }

    }



    slice_type = get_ue_golomb_31(&h->gb);

    if (slice_type > 9) {

        av_log(h->avctx, AV_LOG_ERROR,

               "slice type %d too large at %d %d\n",

               slice_type, h->mb_x, h->mb_y);

        return AVERROR_INVALIDDATA;

    }

    if (slice_type > 4) {

        slice_type -= 5;

        h->slice_type_fixed = 1;

    } else

        h->slice_type_fixed = 0;



    slice_type = golomb_to_pict_type[slice_type];

    h->slice_type     = slice_type;

    h->slice_type_nos = slice_type & 3;



    if (h->nal_unit_type  == NAL_IDR_SLICE &&

        h->slice_type_nos != AV_PICTURE_TYPE_I) {

        av_log(h->avctx, AV_LOG_ERROR, "A non-intra slice in an IDR NAL unit.\n");

        return AVERROR_INVALIDDATA;

    }



    if (

        (h->avctx->skip_frame >= AVDISCARD_NONREF && !h->nal_ref_idc) ||

        (h->avctx->skip_frame >= AVDISCARD_BIDIR  && h->slice_type_nos == AV_PICTURE_TYPE_B) ||

        (h->avctx->skip_frame >= AVDISCARD_NONINTRA && h->slice_type_nos != AV_PICTURE_TYPE_I) ||

        (h->avctx->skip_frame >= AVDISCARD_NONKEY && h->nal_unit_type != NAL_IDR_SLICE) ||

         h->avctx->skip_frame >= AVDISCARD_ALL) {

         return SLICE_SKIPED;

     }



    // to make a few old functions happy, it's wrong though

    h->pict_type = h->slice_type;



    pps_id = get_ue_golomb(&h->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;

    }

    if (!h0->pps_buffers[pps_id]) {

        av_log(h->avctx, AV_LOG_ERROR,

               "non-existing PPS %u referenced\n",

               pps_id);

        return AVERROR_INVALIDDATA;

    }

    if (h0->au_pps_id >= 0 && pps_id != h0->au_pps_id) {

        av_log(h->avctx, AV_LOG_ERROR,

               "PPS change from %d to %d forbidden\n",

               h0->au_pps_id, pps_id);

        return AVERROR_INVALIDDATA;

    }

    h->pps = *h0->pps_buffers[pps_id];



    if (!h0->sps_buffers[h->pps.sps_id]) {

        av_log(h->avctx, AV_LOG_ERROR,

               "non-existing SPS %u referenced\n",

               h->pps.sps_id);

        return AVERROR_INVALIDDATA;

    }



    if (h->pps.sps_id != h->sps.sps_id ||

        h->pps.sps_id != h->current_sps_id ||

        h0->sps_buffers[h->pps.sps_id]->new) {



        h->sps = *h0->sps_buffers[h->pps.sps_id];



        if (h->mb_width  != h->sps.mb_width ||

            h->mb_height != h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag) ||

            h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||

            h->cur_chroma_format_idc != h->sps.chroma_format_idc

        )

            needs_reinit = 1;



        if (h->bit_depth_luma    != h->sps.bit_depth_luma ||

            h->chroma_format_idc != h->sps.chroma_format_idc) {

            h->bit_depth_luma    = h->sps.bit_depth_luma;

            h->chroma_format_idc = h->sps.chroma_format_idc;

            needs_reinit         = 1;

        }

        if ((ret = ff_h264_set_parameter_from_sps(h)) < 0)

            return ret;

    }



    h->avctx->profile = ff_h264_get_profile(&h->sps);

    h->avctx->level   = h->sps.level_idc;

    h->avctx->refs    = h->sps.ref_frame_count;



    must_reinit = (h->context_initialized &&

                    (   16*h->sps.mb_width != h->avctx->coded_width

                     || 16*h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag) != h->avctx->coded_height

                     || h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma

                     || h->cur_chroma_format_idc != h->sps.chroma_format_idc

                     || av_cmp_q(h->sps.sar, h->avctx->sample_aspect_ratio)

                     || h->mb_width  != h->sps.mb_width

                     || h->mb_height != h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag)

                    ));

    if (non_j_pixfmt(h0->avctx->pix_fmt) != non_j_pixfmt(get_pixel_format(h0, 0)))

        must_reinit = 1;



    h->mb_width  = h->sps.mb_width;

    h->mb_height = h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);

    h->mb_num    = h->mb_width * h->mb_height;

    h->mb_stride = h->mb_width + 1;



    h->b_stride = h->mb_width * 4;



    h->chroma_y_shift = h->sps.chroma_format_idc <= 1; // 400 uses yuv420p



    h->width  = 16 * h->mb_width;

    h->height = 16 * h->mb_height;



    ret = init_dimensions(h);

    if (ret < 0)

        return ret;



    if (h->sps.video_signal_type_present_flag) {

        h->avctx->color_range = h->sps.full_range>0 ? AVCOL_RANGE_JPEG

                                                    : AVCOL_RANGE_MPEG;

        if (h->sps.colour_description_present_flag) {

            if (h->avctx->colorspace != h->sps.colorspace)

                needs_reinit = 1;

            h->avctx->color_primaries = h->sps.color_primaries;

            h->avctx->color_trc       = h->sps.color_trc;

            h->avctx->colorspace      = h->sps.colorspace;

        }

    }



    if (h->context_initialized &&

        (must_reinit || needs_reinit)) {

        if (h != h0) {

            av_log(h->avctx, AV_LOG_ERROR,

                   "changing width %d -> %d / height %d -> %d on "

                   "slice %d\n",

                   h->width, h->avctx->coded_width,

                   h->height, h->avctx->coded_height,

                   h0->current_slice + 1);

            return AVERROR_INVALIDDATA;

        }



        ff_h264_flush_change(h);



        if ((ret = get_pixel_format(h, 1)) < 0)

            return ret;

        h->avctx->pix_fmt = ret;



        av_log(h->avctx, AV_LOG_INFO, "Reinit context to %dx%d, "

               "pix_fmt: %s\n", h->width, h->height, av_get_pix_fmt_name(h->avctx->pix_fmt));



        if ((ret = h264_slice_header_init(h, 1)) < 0) {

            av_log(h->avctx, AV_LOG_ERROR,

                   "h264_slice_header_init() failed\n");

            return ret;

        }

    }

    if (!h->context_initialized) {

        if (h != h0) {

            av_log(h->avctx, AV_LOG_ERROR,

                   "Cannot (re-)initialize context during parallel decoding.\n");

            return AVERROR_PATCHWELCOME;

        }



        if ((ret = get_pixel_format(h, 1)) < 0)

            return ret;

        h->avctx->pix_fmt = ret;



        if ((ret = h264_slice_header_init(h, 0)) < 0) {

            av_log(h->avctx, AV_LOG_ERROR,

                   "h264_slice_header_init() failed\n");

            return ret;

        }

    }



    if (h == h0 && h->dequant_coeff_pps != pps_id) {

        h->dequant_coeff_pps = pps_id;

        h264_init_dequant_tables(h);

    }



    h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);



    h->mb_mbaff        = 0;

    h->mb_aff_frame    = 0;

    last_pic_structure = h0->picture_structure;

    last_pic_droppable = h0->droppable;

    h->droppable       = h->nal_ref_idc == 0;

    if (h->sps.frame_mbs_only_flag) {

        h->picture_structure = PICT_FRAME;

    } else {

        if (!h->sps.direct_8x8_inference_flag && slice_type == AV_PICTURE_TYPE_B) {

            av_log(h->avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");

            return -1;

        }

        field_pic_flag = get_bits1(&h->gb);

        if (field_pic_flag) {

            bottom_field_flag = get_bits1(&h->gb);

            h->picture_structure = PICT_TOP_FIELD + bottom_field_flag;

        } else {

            h->picture_structure = PICT_FRAME;

            h->mb_aff_frame      = h->sps.mb_aff;

        }

    }

    h->mb_field_decoding_flag = h->picture_structure != PICT_FRAME;



    if (h0->current_slice != 0) {

        if (last_pic_structure != h->picture_structure ||

            last_pic_droppable != h->droppable) {

            av_log(h->avctx, AV_LOG_ERROR,

                   "Changing field mode (%d -> %d) between slices is not allowed\n",

                   last_pic_structure, h->picture_structure);

            h->picture_structure = last_pic_structure;

            h->droppable         = last_pic_droppable;

            return AVERROR_INVALIDDATA;

        } else if (!h0->cur_pic_ptr) {

            av_log(h->avctx, AV_LOG_ERROR,

                   "unset cur_pic_ptr on slice %d\n",

                   h0->current_slice + 1);

            return AVERROR_INVALIDDATA;

        }

    } else {

        /* Shorten frame num gaps so we don't have to allocate reference

         * frames just to throw them away */

        if (h->frame_num != h->prev_frame_num) {

            int unwrap_prev_frame_num = h->prev_frame_num;

            int max_frame_num         = 1 << h->sps.log2_max_frame_num;



            if (unwrap_prev_frame_num > h->frame_num)

                unwrap_prev_frame_num -= max_frame_num;



            if ((h->frame_num - unwrap_prev_frame_num) > h->sps.ref_frame_count) {

                unwrap_prev_frame_num = (h->frame_num - h->sps.ref_frame_count) - 1;

                if (unwrap_prev_frame_num < 0)

                    unwrap_prev_frame_num += max_frame_num;



                h->prev_frame_num = unwrap_prev_frame_num;

            }

        }



        /* See if we have a decoded first field looking for a pair...

         * Here, we're using that to see if we should mark previously

         * decode frames as "finished".

         * We have to do that before the "dummy" in-between frame allocation,

         * since that can modify h->cur_pic_ptr. */

        if (h0->first_field) {

            assert(h0->cur_pic_ptr);

            assert(h0->cur_pic_ptr->f.buf[0]);

            assert(h0->cur_pic_ptr->reference != DELAYED_PIC_REF);



            /* Mark old field/frame as completed */

            if (h0->cur_pic_ptr->tf.owner == h0->avctx) {

                ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,

                                          last_pic_structure == PICT_BOTTOM_FIELD);

            }



            /* figure out if we have a complementary field pair */

            if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {

                /* Previous field is unmatched. Don't display it, but let it

                 * remain for reference if marked as such. */

                if (last_pic_structure != PICT_FRAME) {

                    ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,

                                              last_pic_structure == PICT_TOP_FIELD);

                }

            } else {

                if (h0->cur_pic_ptr->frame_num != h->frame_num) {

                    /* This and previous field were reference, but had

                     * different frame_nums. Consider this field first in

                     * pair. Throw away previous field except for reference

                     * purposes. */

                    if (last_pic_structure != PICT_FRAME) {

                        ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,

                                                  last_pic_structure == PICT_TOP_FIELD);

                    }

                } else {

                    /* Second field in complementary pair */

                    if (!((last_pic_structure   == PICT_TOP_FIELD &&

                           h->picture_structure == PICT_BOTTOM_FIELD) ||

                          (last_pic_structure   == PICT_BOTTOM_FIELD &&

                           h->picture_structure == PICT_TOP_FIELD))) {

                        av_log(h->avctx, AV_LOG_ERROR,

                               "Invalid field mode combination %d/%d\n",

                               last_pic_structure, h->picture_structure);

                        h->picture_structure = last_pic_structure;

                        h->droppable         = last_pic_droppable;

                        return AVERROR_INVALIDDATA;

                    } else if (last_pic_droppable != h->droppable) {

                        avpriv_request_sample(h->avctx,

                                              "Found reference and non-reference fields in the same frame, which");

                        h->picture_structure = last_pic_structure;

                        h->droppable         = last_pic_droppable;

                        return AVERROR_PATCHWELCOME;

                    }

                }

            }

        }



        while (h->frame_num != h->prev_frame_num && !h0->first_field &&

               h->frame_num != (h->prev_frame_num + 1) % (1 << h->sps.log2_max_frame_num)) {

            H264Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;

            av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",

                   h->frame_num, h->prev_frame_num);

            if (!h->sps.gaps_in_frame_num_allowed_flag)

                for(i=0; i<FF_ARRAY_ELEMS(h->last_pocs); i++)

                    h->last_pocs[i] = INT_MIN;

            ret = h264_frame_start(h);

            if (ret < 0) {

                h0->first_field = 0;

                return ret;

            }



            h->prev_frame_num++;

            h->prev_frame_num        %= 1 << h->sps.log2_max_frame_num;

            h->cur_pic_ptr->frame_num = h->prev_frame_num;

            h->cur_pic_ptr->invalid_gap = !h->sps.gaps_in_frame_num_allowed_flag;

            ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);

            ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);

            ret = ff_generate_sliding_window_mmcos(h, 1);

            if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))

                return ret;

            ret = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);

            if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))

                return ret;

            /* Error concealment: If a ref is missing, copy the previous ref

             * in its place.

             * FIXME: Avoiding a memcpy would be nice, but ref handling makes

             * many assumptions about there being no actual duplicates.

             * FIXME: This does not copy padding for out-of-frame motion

             * vectors.  Given we are concealing a lost frame, this probably

             * is not noticeable by comparison, but it should be fixed. */

            if (h->short_ref_count) {

                if (prev) {

                    av_image_copy(h->short_ref[0]->f.data,

                                  h->short_ref[0]->f.linesize,

                                  (const uint8_t **)prev->f.data,

                                  prev->f.linesize,

                                  h->avctx->pix_fmt,

                                  h->mb_width  * 16,

                                  h->mb_height * 16);

                    h->short_ref[0]->poc = prev->poc + 2;

                }

                h->short_ref[0]->frame_num = h->prev_frame_num;

            }

        }



        /* See if we have a decoded first field looking for a pair...

         * We're using that to see whether to continue decoding in that

         * frame, or to allocate a new one. */

        if (h0->first_field) {

            assert(h0->cur_pic_ptr);

            assert(h0->cur_pic_ptr->f.buf[0]);

            assert(h0->cur_pic_ptr->reference != DELAYED_PIC_REF);



            /* figure out if we have a complementary field pair */

            if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {

                /* Previous field is unmatched. Don't display it, but let it

                 * remain for reference if marked as such. */

                h0->cur_pic_ptr = NULL;

                h0->first_field = FIELD_PICTURE(h);

            } else {

                if (h0->cur_pic_ptr->frame_num != h->frame_num) {

                    ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,

                                              h0->picture_structure==PICT_BOTTOM_FIELD);

                    /* This and the previous field had different frame_nums.

                     * Consider this field first in pair. Throw away previous

                     * one except for reference purposes. */

                    h0->first_field = 1;

                    h0->cur_pic_ptr = NULL;

                } else {

                    /* Second field in complementary pair */

                    h0->first_field = 0;

                }

            }

        } else {

            /* Frame or first field in a potentially complementary pair */

            h0->first_field = FIELD_PICTURE(h);

        }



        if (!FIELD_PICTURE(h) || h0->first_field) {

            if (h264_frame_start(h) < 0) {

                h0->first_field = 0;

                return AVERROR_INVALIDDATA;

            }

        } else {

            release_unused_pictures(h, 0);

        }

        /* Some macroblocks can be accessed before they're available in case

        * of lost slices, MBAFF or threading. */

        if (FIELD_PICTURE(h)) {

            for(i = (h->picture_structure == PICT_BOTTOM_FIELD); i<h->mb_height; i++)

                memset(h->slice_table + i*h->mb_stride, -1, (h->mb_stride - (i+1==h->mb_height)) * sizeof(*h->slice_table));

        } else {

            memset(h->slice_table, -1,

                (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));

        }

        h0->last_slice_type = -1;

    }

    if (h != h0 && (ret = clone_slice(h, h0)) < 0)

        return ret;



    /* can't be in alloc_tables because linesize isn't known there.

     * FIXME: redo bipred weight to not require extra buffer? */

    for (i = 0; i < h->slice_context_count; i++)

        if (h->thread_context[i]) {

            ret = alloc_scratch_buffers(h->thread_context[i], h->linesize);

            if (ret < 0)

                return ret;

        }



    h->cur_pic_ptr->frame_num = h->frame_num; // FIXME frame_num cleanup



    av_assert1(h->mb_num == h->mb_width * h->mb_height);

    if (first_mb_in_slice << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num ||

        first_mb_in_slice >= h->mb_num) {

        av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");

        return AVERROR_INVALIDDATA;

    }

    h->resync_mb_x = h->mb_x =  first_mb_in_slice % h->mb_width;

    h->resync_mb_y = h->mb_y = (first_mb_in_slice / h->mb_width) <<

                               FIELD_OR_MBAFF_PICTURE(h);

    if (h->picture_structure == PICT_BOTTOM_FIELD)

        h->resync_mb_y = h->mb_y = h->mb_y + 1;

    av_assert1(h->mb_y < h->mb_height);



    if (h->picture_structure == PICT_FRAME) {

        h->curr_pic_num = h->frame_num;

        h->max_pic_num  = 1 << h->sps.log2_max_frame_num;

    } else {

        h->curr_pic_num = 2 * h->frame_num + 1;

        h->max_pic_num  = 1 << (h->sps.log2_max_frame_num + 1);

    }



    if (h->nal_unit_type == NAL_IDR_SLICE)

        get_ue_golomb(&h->gb); /* idr_pic_id */



    if (h->sps.poc_type == 0) {

        h->poc_lsb = get_bits(&h->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);

    }



    if (h->sps.poc_type == 1 && !h->sps.delta_pic_order_always_zero_flag) {

        h->delta_poc[0] = get_se_golomb(&h->gb);



        if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)

            h->delta_poc[1] = get_se_golomb(&h->gb);

    }



    ff_init_poc(h, h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc);



    if (h->pps.redundant_pic_cnt_present)

        h->redundant_pic_count = get_ue_golomb(&h->gb);



    ret = ff_set_ref_count(h);

    if (ret < 0)

        return ret;



    if (slice_type != AV_PICTURE_TYPE_I &&

        (h0->current_slice == 0 ||

         slice_type != h0->last_slice_type ||

         memcmp(h0->last_ref_count, h0->ref_count, sizeof(h0->ref_count)))) {



        ff_h264_fill_default_ref_list(h);

    }



    if (h->slice_type_nos != AV_PICTURE_TYPE_I) {

       ret = ff_h264_decode_ref_pic_list_reordering(h);

       if (ret < 0) {

           h->ref_count[1] = h->ref_count[0] = 0;

           return ret;

       }

    }



    if ((h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P) ||

        (h->pps.weighted_bipred_idc == 1 &&

         h->slice_type_nos == AV_PICTURE_TYPE_B))

        ff_pred_weight_table(h);

    else if (h->pps.weighted_bipred_idc == 2 &&

             h->slice_type_nos == AV_PICTURE_TYPE_B) {

        implicit_weight_table(h, -1);

    } else {

        h->use_weight = 0;

        for (i = 0; i < 2; i++) {

            h->luma_weight_flag[i]   = 0;

            h->chroma_weight_flag[i] = 0;

        }

    }



    // If frame-mt is enabled, only update mmco tables for the first slice

    // in a field. Subsequent slices can temporarily clobber h->mmco_index

    // or h->mmco, which will cause ref list mix-ups and decoding errors

    // 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,

                                             !(h->avctx->active_thread_type & FF_THREAD_FRAME) ||

                                             h0->current_slice == 0);

        if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))

            return AVERROR_INVALIDDATA;

    }



    if (FRAME_MBAFF(h)) {

        ff_h264_fill_mbaff_ref_list(h);



        if (h->pps.weighted_bipred_idc == 2 && h->slice_type_nos == AV_PICTURE_TYPE_B) {

            implicit_weight_table(h, 0);

            implicit_weight_table(h, 1);

        }

    }



    if (h->slice_type_nos == AV_PICTURE_TYPE_B && !h->direct_spatial_mv_pred)

        ff_h264_direct_dist_scale_factor(h);

    ff_h264_direct_ref_list_init(h);



    if (h->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac) {

        tmp = get_ue_golomb_31(&h->gb);

        if (tmp > 2) {

            av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\n", tmp);

            return AVERROR_INVALIDDATA;

        }

        h->cabac_init_idc = tmp;

    }



    h->last_qscale_diff = 0;

    tmp = h->pps.init_qp + get_se_golomb(&h->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;

    }

    h->qscale       = tmp;

    h->chroma_qp[0] = get_chroma_qp(h, 0, h->qscale);

    h->chroma_qp[1] = get_chroma_qp(h, 1, h->qscale);

    // FIXME qscale / qp ... stuff

    if (h->slice_type == AV_PICTURE_TYPE_SP)

        get_bits1(&h->gb); /* sp_for_switch_flag */

    if (h->slice_type == AV_PICTURE_TYPE_SP ||

        h->slice_type == AV_PICTURE_TYPE_SI)

        get_se_golomb(&h->gb); /* slice_qs_delta */



    h->deblocking_filter     = 1;

    h->slice_alpha_c0_offset = 0;

    h->slice_beta_offset     = 0;

    if (h->pps.deblocking_filter_parameters_present) {

        tmp = get_ue_golomb_31(&h->gb);

        if (tmp > 2) {

            av_log(h->avctx, AV_LOG_ERROR,

                   "deblocking_filter_idc %u out of range\n", tmp);

            return AVERROR_INVALIDDATA;

        }

        h->deblocking_filter = tmp;

        if (h->deblocking_filter < 2)

            h->deblocking_filter ^= 1;  // 1<->0



        if (h->deblocking_filter) {

            h->slice_alpha_c0_offset = get_se_golomb(&h->gb) * 2;

            h->slice_beta_offset     = get_se_golomb(&h->gb) * 2;

            if (h->slice_alpha_c0_offset >  12 ||

                h->slice_alpha_c0_offset < -12 ||

                h->slice_beta_offset >  12     ||

                h->slice_beta_offset < -12) {

                av_log(h->avctx, AV_LOG_ERROR,

                       "deblocking filter parameters %d %d out of range\n",

                       h->slice_alpha_c0_offset, h->slice_beta_offset);

                return AVERROR_INVALIDDATA;

            }

        }

    }



    if (h->avctx->skip_loop_filter >= AVDISCARD_ALL ||

        (h->avctx->skip_loop_filter >= AVDISCARD_NONKEY &&

         h->nal_unit_type != NAL_IDR_SLICE) ||

        (h->avctx->skip_loop_filter >= AVDISCARD_NONINTRA &&

         h->slice_type_nos != AV_PICTURE_TYPE_I) ||

        (h->avctx->skip_loop_filter >= AVDISCARD_BIDIR  &&

         h->slice_type_nos == AV_PICTURE_TYPE_B) ||

        (h->avctx->skip_loop_filter >= AVDISCARD_NONREF &&

         h->nal_ref_idc == 0))

        h->deblocking_filter = 0;



    if (h->deblocking_filter == 1 && h0->max_contexts > 1) {

        if (h->avctx->flags2 & CODEC_FLAG2_FAST) {

            /* Cheat slightly for speed:

             * Do not bother to deblock across slices. */

            h->deblocking_filter = 2;

        } else {

            h0->max_contexts = 1;

            if (!h0->single_decode_warning) {

                av_log(h->avctx, AV_LOG_INFO,

                       "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");

                h0->single_decode_warning = 1;

            }

            if (h != h0) {

                av_log(h->avctx, AV_LOG_ERROR,

                       "Deblocking switched inside frame.\n");

                return SLICE_SINGLETHREAD;

            }

        }

    }

    h->qp_thresh = 15 -

                   FFMIN(h->slice_alpha_c0_offset, h->slice_beta_offset) -

                   FFMAX3(0,

                          h->pps.chroma_qp_index_offset[0],

                          h->pps.chroma_qp_index_offset[1]) +

                   6 * (h->sps.bit_depth_luma - 8);



    h0->last_slice_type = slice_type;

    memcpy(h0->last_ref_count, h0->ref_count, sizeof(h0->last_ref_count));

    h->slice_num        = ++h0->current_slice;



    if (h->slice_num)

        h0->slice_row[(h->slice_num-1)&(MAX_SLICES-1)]= h->resync_mb_y;

    if (   h0->slice_row[h->slice_num&(MAX_SLICES-1)] + 3 >= h->resync_mb_y

        && h0->slice_row[h->slice_num&(MAX_SLICES-1)] <= h->resync_mb_y

        && h->slice_num >= MAX_SLICES) {

        //in case of ASO this check needs to be updated depending on how we decide to assign slice numbers in this case

        av_log(h->avctx, AV_LOG_WARNING, "Possibly too many slices (%d >= %d), increase MAX_SLICES and recompile if there are artifacts\n", h->slice_num, MAX_SLICES);

    }



    for (j = 0; j < 2; j++) {

        int id_list[16];

        int *ref2frm = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][j];

        for (i = 0; i < 16; i++) {

            id_list[i] = 60;

            if (j < h->list_count && i < h->ref_count[j] &&

                h->ref_list[j][i].f.buf[0]) {

                int k;

                AVBuffer *buf = h->ref_list[j][i].f.buf[0]->buffer;

                for (k = 0; k < h->short_ref_count; k++)

                    if (h->short_ref[k]->f.buf[0]->buffer == buf) {

                        id_list[i] = k;

                        break;

                    }

                for (k = 0; k < h->long_ref_count; k++)

                    if (h->long_ref[k] && h->long_ref[k]->f.buf[0]->buffer == buf) {

                        id_list[i] = h->short_ref_count + k;

                        break;

                    }

            }

        }



        ref2frm[0] =

        ref2frm[1] = -1;

        for (i = 0; i < 16; i++)

            ref2frm[i + 2] = 4 * id_list[i] + (h->ref_list[j][i].reference & 3);

        ref2frm[18 + 0] =

        ref2frm[18 + 1] = -1;

        for (i = 16; i < 48; i++)

            ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +

                             (h->ref_list[j][i].reference & 3);

    }



    if (h->ref_count[0]) ff_h264_set_erpic(&h->er.last_pic, &h->ref_list[0][0]);

    if (h->ref_count[1]) ff_h264_set_erpic(&h->er.next_pic, &h->ref_list[1][0]);



    h->er.ref_count = h->ref_count[0];

    h0->au_pps_id = pps_id;

    h->sps.new =

    h0->sps_buffers[h->pps.sps_id]->new = 0;

    h->current_sps_id = h->pps.sps_id;



    if (h->avctx->debug & FF_DEBUG_PICT_INFO) {

        av_log(h->avctx, AV_LOG_DEBUG,

               "slice:%d %s mb:%d %c%s%s pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s %s\n",

               h->slice_num,

               (h->picture_structure == PICT_FRAME ? "F" : h->picture_structure == PICT_TOP_FIELD ? "T" : "B"),

               first_mb_in_slice,

               av_get_picture_type_char(h->slice_type),

               h->slice_type_fixed ? " fix" : "",

               h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",

               pps_id, h->frame_num,

               h->cur_pic_ptr->field_poc[0],

               h->cur_pic_ptr->field_poc[1],

               h->ref_count[0], h->ref_count[1],

               h->qscale,

               h->deblocking_filter,

               h->slice_alpha_c0_offset, h->slice_beta_offset,

               h->use_weight,

               h->use_weight == 1 && h->use_weight_chroma ? "c" : "",

               h->slice_type == AV_PICTURE_TYPE_B ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");

    }



    return 0;

}

int ff_h264_decode_slice_header(H264Context *h, H264Context *h0)

{

    unsigned int first_mb_in_slice;

    unsigned int pps_id;

    int ret;

    unsigned int slice_type, tmp, i, j;

    int last_pic_structure, last_pic_droppable;

    int must_reinit;

    int needs_reinit = 0;

    int field_pic_flag, bottom_field_flag;



    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_long(&h->gb);



    if (first_mb_in_slice == 0) { 

        if (h0->current_slice && h->cur_pic_ptr && FIELD_PICTURE(h)) {

            ff_h264_field_end(h, 1);

        }



        h0->current_slice = 0;

        if (!h0->first_field) {

            if (h->cur_pic_ptr && !h->droppable) {

                ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,

                                          h->picture_structure == PICT_BOTTOM_FIELD);

            }

            h->cur_pic_ptr = NULL;

        }

    }



    slice_type = get_ue_golomb_31(&h->gb);

    if (slice_type > 9) {

        av_log(h->avctx, AV_LOG_ERROR,

               "slice type %d too large at %d %d\n",

               slice_type, h->mb_x, h->mb_y);

        return AVERROR_INVALIDDATA;

    }

    if (slice_type > 4) {

        slice_type -= 5;

        h->slice_type_fixed = 1;

    } else

        h->slice_type_fixed = 0;



    slice_type = golomb_to_pict_type[slice_type];

    h->slice_type     = slice_type;

    h->slice_type_nos = slice_type & 3;



    if (h->nal_unit_type  == NAL_IDR_SLICE &&

        h->slice_type_nos != AV_PICTURE_TYPE_I) {

        av_log(h->avctx, AV_LOG_ERROR, "A non-intra slice in an IDR NAL unit.\n");

        return AVERROR_INVALIDDATA;

    }



    if (

        (h->avctx->skip_frame >= AVDISCARD_NONREF && !h->nal_ref_idc) ||

        (h->avctx->skip_frame >= AVDISCARD_BIDIR  && h->slice_type_nos == AV_PICTURE_TYPE_B) ||

        (h->avctx->skip_frame >= AVDISCARD_NONINTRA && h->slice_type_nos != AV_PICTURE_TYPE_I) ||

        (h->avctx->skip_frame >= AVDISCARD_NONKEY && h->nal_unit_type != NAL_IDR_SLICE) ||

         h->avctx->skip_frame >= AVDISCARD_ALL) {

         return SLICE_SKIPED;

     }



    

    h->pict_type = h->slice_type;



    pps_id = get_ue_golomb(&h->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;

    }

    if (!h0->pps_buffers[pps_id]) {

        av_log(h->avctx, AV_LOG_ERROR,

               "non-existing PPS %u referenced\n",

               pps_id);

        return AVERROR_INVALIDDATA;

    }

    if (h0->au_pps_id >= 0 && pps_id != h0->au_pps_id) {

        av_log(h->avctx, AV_LOG_ERROR,

               "PPS change from %d to %d forbidden\n",

               h0->au_pps_id, pps_id);

        return AVERROR_INVALIDDATA;

    }

    h->pps = *h0->pps_buffers[pps_id];



    if (!h0->sps_buffers[h->pps.sps_id]) {

        av_log(h->avctx, AV_LOG_ERROR,

               "non-existing SPS %u referenced\n",

               h->pps.sps_id);

        return AVERROR_INVALIDDATA;

    }



    if (h->pps.sps_id != h->sps.sps_id ||

        h->pps.sps_id != h->current_sps_id ||

        h0->sps_buffers[h->pps.sps_id]->new) {



        h->sps = *h0->sps_buffers[h->pps.sps_id];



        if (h->mb_width  != h->sps.mb_width ||

            h->mb_height != h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag) ||

            h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||

            h->cur_chroma_format_idc != h->sps.chroma_format_idc

        )

            needs_reinit = 1;



        if (h->bit_depth_luma    != h->sps.bit_depth_luma ||

            h->chroma_format_idc != h->sps.chroma_format_idc) {

            h->bit_depth_luma    = h->sps.bit_depth_luma;

            h->chroma_format_idc = h->sps.chroma_format_idc;

            needs_reinit         = 1;

        }

        if ((ret = ff_h264_set_parameter_from_sps(h)) < 0)

            return ret;

    }



    h->avctx->profile = ff_h264_get_profile(&h->sps);

    h->avctx->level   = h->sps.level_idc;

    h->avctx->refs    = h->sps.ref_frame_count;



    must_reinit = (h->context_initialized &&

                    (   16*h->sps.mb_width != h->avctx->coded_width

                     || 16*h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag) != h->avctx->coded_height

                     || h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma

                     || h->cur_chroma_format_idc != h->sps.chroma_format_idc

                     || av_cmp_q(h->sps.sar, h->avctx->sample_aspect_ratio)

                     || h->mb_width  != h->sps.mb_width

                     || h->mb_height != h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag)

                    ));

    if (non_j_pixfmt(h0->avctx->pix_fmt) != non_j_pixfmt(get_pixel_format(h0, 0)))

        must_reinit = 1;



    h->mb_width  = h->sps.mb_width;

    h->mb_height = h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);

    h->mb_num    = h->mb_width * h->mb_height;

    h->mb_stride = h->mb_width + 1;



    h->b_stride = h->mb_width * 4;



    h->chroma_y_shift = h->sps.chroma_format_idc <= 1; 



    h->width  = 16 * h->mb_width;

    h->height = 16 * h->mb_height;



    ret = init_dimensions(h);

    if (ret < 0)

        return ret;



    if (h->sps.video_signal_type_present_flag) {

        h->avctx->color_range = h->sps.full_range>0 ? AVCOL_RANGE_JPEG

                                                    : AVCOL_RANGE_MPEG;

        if (h->sps.colour_description_present_flag) {

            if (h->avctx->colorspace != h->sps.colorspace)

                needs_reinit = 1;

            h->avctx->color_primaries = h->sps.color_primaries;

            h->avctx->color_trc       = h->sps.color_trc;

            h->avctx->colorspace      = h->sps.colorspace;

        }

    }



    if (h->context_initialized &&

        (must_reinit || needs_reinit)) {

        if (h != h0) {

            av_log(h->avctx, AV_LOG_ERROR,

                   "changing width %d -> %d / height %d -> %d on "

                   "slice %d\n",

                   h->width, h->avctx->coded_width,

                   h->height, h->avctx->coded_height,

                   h0->current_slice + 1);

            return AVERROR_INVALIDDATA;

        }



        ff_h264_flush_change(h);



        if ((ret = get_pixel_format(h, 1)) < 0)

            return ret;

        h->avctx->pix_fmt = ret;



        av_log(h->avctx, AV_LOG_INFO, "Reinit context to %dx%d, "

               "pix_fmt: %s\n", h->width, h->height, av_get_pix_fmt_name(h->avctx->pix_fmt));



        if ((ret = h264_slice_header_init(h, 1)) < 0) {

            av_log(h->avctx, AV_LOG_ERROR,

                   "h264_slice_header_init() failed\n");

            return ret;

        }

    }

    if (!h->context_initialized) {

        if (h != h0) {

            av_log(h->avctx, AV_LOG_ERROR,

                   "Cannot (re-)initialize context during parallel decoding.\n");

            return AVERROR_PATCHWELCOME;

        }



        if ((ret = get_pixel_format(h, 1)) < 0)

            return ret;

        h->avctx->pix_fmt = ret;



        if ((ret = h264_slice_header_init(h, 0)) < 0) {

            av_log(h->avctx, AV_LOG_ERROR,

                   "h264_slice_header_init() failed\n");

            return ret;

        }

    }



    if (h == h0 && h->dequant_coeff_pps != pps_id) {

        h->dequant_coeff_pps = pps_id;

        h264_init_dequant_tables(h);

    }



    h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);



    h->mb_mbaff        = 0;

    h->mb_aff_frame    = 0;

    last_pic_structure = h0->picture_structure;

    last_pic_droppable = h0->droppable;

    h->droppable       = h->nal_ref_idc == 0;

    if (h->sps.frame_mbs_only_flag) {

        h->picture_structure = PICT_FRAME;

    } else {

        if (!h->sps.direct_8x8_inference_flag && slice_type == AV_PICTURE_TYPE_B) {

            av_log(h->avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");

            return -1;

        }

        field_pic_flag = get_bits1(&h->gb);

        if (field_pic_flag) {

            bottom_field_flag = get_bits1(&h->gb);

            h->picture_structure = PICT_TOP_FIELD + bottom_field_flag;

        } else {

            h->picture_structure = PICT_FRAME;

            h->mb_aff_frame      = h->sps.mb_aff;

        }

    }

    h->mb_field_decoding_flag = h->picture_structure != PICT_FRAME;



    if (h0->current_slice != 0) {

        if (last_pic_structure != h->picture_structure ||

            last_pic_droppable != h->droppable) {

            av_log(h->avctx, AV_LOG_ERROR,

                   "Changing field mode (%d -> %d) between slices is not allowed\n",

                   last_pic_structure, h->picture_structure);

            h->picture_structure = last_pic_structure;

            h->droppable         = last_pic_droppable;

            return AVERROR_INVALIDDATA;

        } else if (!h0->cur_pic_ptr) {

            av_log(h->avctx, AV_LOG_ERROR,

                   "unset cur_pic_ptr on slice %d\n",

                   h0->current_slice + 1);

            return AVERROR_INVALIDDATA;

        }

    } else {

        



        if (h->frame_num != h->prev_frame_num) {

            int unwrap_prev_frame_num = h->prev_frame_num;

            int max_frame_num         = 1 << h->sps.log2_max_frame_num;



            if (unwrap_prev_frame_num > h->frame_num)

                unwrap_prev_frame_num -= max_frame_num;



            if ((h->frame_num - unwrap_prev_frame_num) > h->sps.ref_frame_count) {

                unwrap_prev_frame_num = (h->frame_num - h->sps.ref_frame_count) - 1;

                if (unwrap_prev_frame_num < 0)

                    unwrap_prev_frame_num += max_frame_num;



                h->prev_frame_num = unwrap_prev_frame_num;

            }

        }



        









        if (h0->first_field) {

            assert(h0->cur_pic_ptr);

            assert(h0->cur_pic_ptr->f.buf[0]);

            assert(h0->cur_pic_ptr->reference != DELAYED_PIC_REF);



            

            if (h0->cur_pic_ptr->tf.owner == h0->avctx) {

                ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,

                                          last_pic_structure == PICT_BOTTOM_FIELD);

            }



            

            if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {

                



                if (last_pic_structure != PICT_FRAME) {

                    ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,

                                              last_pic_structure == PICT_TOP_FIELD);

                }

            } else {

                if (h0->cur_pic_ptr->frame_num != h->frame_num) {

                    







                    if (last_pic_structure != PICT_FRAME) {

                        ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,

                                                  last_pic_structure == PICT_TOP_FIELD);

                    }

                } else {

                    

                    if (!((last_pic_structure   == PICT_TOP_FIELD &&

                           h->picture_structure == PICT_BOTTOM_FIELD) ||

                          (last_pic_structure   == PICT_BOTTOM_FIELD &&

                           h->picture_structure == PICT_TOP_FIELD))) {

                        av_log(h->avctx, AV_LOG_ERROR,

                               "Invalid field mode combination %d/%d\n",

                               last_pic_structure, h->picture_structure);

                        h->picture_structure = last_pic_structure;

                        h->droppable         = last_pic_droppable;

                        return AVERROR_INVALIDDATA;

                    } else if (last_pic_droppable != h->droppable) {

                        avpriv_request_sample(h->avctx,

                                              "Found reference and non-reference fields in the same frame, which");

                        h->picture_structure = last_pic_structure;

                        h->droppable         = last_pic_droppable;

                        return AVERROR_PATCHWELCOME;

                    }

                }

            }

        }



        while (h->frame_num != h->prev_frame_num && !h0->first_field &&

               h->frame_num != (h->prev_frame_num + 1) % (1 << h->sps.log2_max_frame_num)) {

            H264Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;

            av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",

                   h->frame_num, h->prev_frame_num);

            if (!h->sps.gaps_in_frame_num_allowed_flag)

                for(i=0; i<FF_ARRAY_ELEMS(h->last_pocs); i++)

                    h->last_pocs[i] = INT_MIN;

            ret = h264_frame_start(h);

            if (ret < 0) {

                h0->first_field = 0;

                return ret;

            }



            h->prev_frame_num++;

            h->prev_frame_num        %= 1 << h->sps.log2_max_frame_num;

            h->cur_pic_ptr->frame_num = h->prev_frame_num;

            h->cur_pic_ptr->invalid_gap = !h->sps.gaps_in_frame_num_allowed_flag;

            ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);

            ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);

            ret = ff_generate_sliding_window_mmcos(h, 1);

            if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))

                return ret;

            ret = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);

            if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))

                return ret;

            













            if (h->short_ref_count) {

                if (prev) {

                    av_image_copy(h->short_ref[0]->f.data,

                                  h->short_ref[0]->f.linesize,

                                  (const uint8_t **)prev->f.data,

                                  prev->f.linesize,

                                  h->avctx->pix_fmt,

                                  h->mb_width  * 16,

                                  h->mb_height * 16);

                    h->short_ref[0]->poc = prev->poc + 2;

                }

                h->short_ref[0]->frame_num = h->prev_frame_num;

            }

        }



        





        if (h0->first_field) {

            assert(h0->cur_pic_ptr);

            assert(h0->cur_pic_ptr->f.buf[0]);

            assert(h0->cur_pic_ptr->reference != DELAYED_PIC_REF);



            

            if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {

                



                h0->cur_pic_ptr = NULL;

                h0->first_field = FIELD_PICTURE(h);

            } else {

                if (h0->cur_pic_ptr->frame_num != h->frame_num) {

                    ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,

                                              h0->picture_structure==PICT_BOTTOM_FIELD);

                    





                    h0->first_field = 1;

                    h0->cur_pic_ptr = NULL;

                } else {

                    

                    h0->first_field = 0;

                }

            }

        } else {

            

            h0->first_field = FIELD_PICTURE(h);

        }



        if (!FIELD_PICTURE(h) || h0->first_field) {

            if (h264_frame_start(h) < 0) {

                h0->first_field = 0;

                return AVERROR_INVALIDDATA;

            }

        } else {

            release_unused_pictures(h, 0);

        }

        



        if (FIELD_PICTURE(h)) {

            for(i = (h->picture_structure == PICT_BOTTOM_FIELD); i<h->mb_height; i++)

                memset(h->slice_table + i*h->mb_stride, -1, (h->mb_stride - (i+1==h->mb_height)) * sizeof(*h->slice_table));

        } else {

            memset(h->slice_table, -1,

                (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));

        }

        h0->last_slice_type = -1;

    }

    if (h != h0 && (ret = clone_slice(h, h0)) < 0)

        return ret;



    



    for (i = 0; i < h->slice_context_count; i++)

        if (h->thread_context[i]) {

            ret = alloc_scratch_buffers(h->thread_context[i], h->linesize);

            if (ret < 0)

                return ret;

        }



    h->cur_pic_ptr->frame_num = h->frame_num; 



    av_assert1(h->mb_num == h->mb_width * h->mb_height);

    if (first_mb_in_slice << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num ||

        first_mb_in_slice >= h->mb_num) {

        av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");

        return AVERROR_INVALIDDATA;

    }

    h->resync_mb_x = h->mb_x =  first_mb_in_slice % h->mb_width;

    h->resync_mb_y = h->mb_y = (first_mb_in_slice / h->mb_width) <<

                               FIELD_OR_MBAFF_PICTURE(h);

    if (h->picture_structure == PICT_BOTTOM_FIELD)

        h->resync_mb_y = h->mb_y = h->mb_y + 1;

    av_assert1(h->mb_y < h->mb_height);



    if (h->picture_structure == PICT_FRAME) {

        h->curr_pic_num = h->frame_num;

        h->max_pic_num  = 1 << h->sps.log2_max_frame_num;

    } else {

        h->curr_pic_num = 2 * h->frame_num + 1;

        h->max_pic_num  = 1 << (h->sps.log2_max_frame_num + 1);

    }



    if (h->nal_unit_type == NAL_IDR_SLICE)

        get_ue_golomb(&h->gb); 



    if (h->sps.poc_type == 0) {

        h->poc_lsb = get_bits(&h->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);

    }



    if (h->sps.poc_type == 1 && !h->sps.delta_pic_order_always_zero_flag) {

        h->delta_poc[0] = get_se_golomb(&h->gb);



        if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)

            h->delta_poc[1] = get_se_golomb(&h->gb);

    }



    ff_init_poc(h, h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc);



    if (h->pps.redundant_pic_cnt_present)

        h->redundant_pic_count = get_ue_golomb(&h->gb);



    ret = ff_set_ref_count(h);

    if (ret < 0)

        return ret;



    if (slice_type != AV_PICTURE_TYPE_I &&

        (h0->current_slice == 0 ||

         slice_type != h0->last_slice_type ||

         memcmp(h0->last_ref_count, h0->ref_count, sizeof(h0->ref_count)))) {



        ff_h264_fill_default_ref_list(h);

    }



    if (h->slice_type_nos != AV_PICTURE_TYPE_I) {

       ret = ff_h264_decode_ref_pic_list_reordering(h);

       if (ret < 0) {

           h->ref_count[1] = h->ref_count[0] = 0;

           return ret;

       }

    }



    if ((h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P) ||

        (h->pps.weighted_bipred_idc == 1 &&

         h->slice_type_nos == AV_PICTURE_TYPE_B))

        ff_pred_weight_table(h);

    else if (h->pps.weighted_bipred_idc == 2 &&

             h->slice_type_nos == AV_PICTURE_TYPE_B) {

        implicit_weight_table(h, -1);

    } else {

        h->use_weight = 0;

        for (i = 0; i < 2; i++) {

            h->luma_weight_flag[i]   = 0;

            h->chroma_weight_flag[i] = 0;

        }

    }



    

    

    

    

    

    if (h->nal_ref_idc) {

        ret = ff_h264_decode_ref_pic_marking(h0, &h->gb,

                                             !(h->avctx->active_thread_type & FF_THREAD_FRAME) ||

                                             h0->current_slice == 0);

        if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))

            return AVERROR_INVALIDDATA;

    }



    if (FRAME_MBAFF(h)) {

        ff_h264_fill_mbaff_ref_list(h);



        if (h->pps.weighted_bipred_idc == 2 && h->slice_type_nos == AV_PICTURE_TYPE_B) {

            implicit_weight_table(h, 0);

            implicit_weight_table(h, 1);

        }

    }



    if (h->slice_type_nos == AV_PICTURE_TYPE_B && !h->direct_spatial_mv_pred)

        ff_h264_direct_dist_scale_factor(h);

    ff_h264_direct_ref_list_init(h);



    if (h->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac) {

        tmp = get_ue_golomb_31(&h->gb);

        if (tmp > 2) {

            av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\n", tmp);

            return AVERROR_INVALIDDATA;

        }

        h->cabac_init_idc = tmp;

    }



    h->last_qscale_diff = 0;

    tmp = h->pps.init_qp + get_se_golomb(&h->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;

    }

    h->qscale       = tmp;

    h->chroma_qp[0] = get_chroma_qp(h, 0, h->qscale);

    h->chroma_qp[1] = get_chroma_qp(h, 1, h->qscale);

    

    if (h->slice_type == AV_PICTURE_TYPE_SP)

        get_bits1(&h->gb); 

    if (h->slice_type == AV_PICTURE_TYPE_SP ||

        h->slice_type == AV_PICTURE_TYPE_SI)

        get_se_golomb(&h->gb); 



    h->deblocking_filter     = 1;

    h->slice_alpha_c0_offset = 0;

    h->slice_beta_offset     = 0;

    if (h->pps.deblocking_filter_parameters_present) {

        tmp = get_ue_golomb_31(&h->gb);

        if (tmp > 2) {

            av_log(h->avctx, AV_LOG_ERROR,

                   "deblocking_filter_idc %u out of range\n", tmp);

            return AVERROR_INVALIDDATA;

        }

        h->deblocking_filter = tmp;

        if (h->deblocking_filter < 2)

            h->deblocking_filter ^= 1;  



        if (h->deblocking_filter) {

            h->slice_alpha_c0_offset = get_se_golomb(&h->gb) * 2;

            h->slice_beta_offset     = get_se_golomb(&h->gb) * 2;

            if (h->slice_alpha_c0_offset >  12 ||

                h->slice_alpha_c0_offset < -12 ||

                h->slice_beta_offset >  12     ||

                h->slice_beta_offset < -12) {

                av_log(h->avctx, AV_LOG_ERROR,

                       "deblocking filter parameters %d %d out of range\n",

                       h->slice_alpha_c0_offset, h->slice_beta_offset);

                return AVERROR_INVALIDDATA;

            }

        }

    }



    if (h->avctx->skip_loop_filter >= AVDISCARD_ALL ||

        (h->avctx->skip_loop_filter >= AVDISCARD_NONKEY &&

         h->nal_unit_type != NAL_IDR_SLICE) ||

        (h->avctx->skip_loop_filter >= AVDISCARD_NONINTRA &&

         h->slice_type_nos != AV_PICTURE_TYPE_I) ||

        (h->avctx->skip_loop_filter >= AVDISCARD_BIDIR  &&

         h->slice_type_nos == AV_PICTURE_TYPE_B) ||

        (h->avctx->skip_loop_filter >= AVDISCARD_NONREF &&

         h->nal_ref_idc == 0))

        h->deblocking_filter = 0;



    if (h->deblocking_filter == 1 && h0->max_contexts > 1) {

        if (h->avctx->flags2 & CODEC_FLAG2_FAST) {

            



            h->deblocking_filter = 2;

        } else {

            h0->max_contexts = 1;

            if (!h0->single_decode_warning) {

                av_log(h->avctx, AV_LOG_INFO,

                       "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");

                h0->single_decode_warning = 1;

            }

            if (h != h0) {

                av_log(h->avctx, AV_LOG_ERROR,

                       "Deblocking switched inside frame.\n");

                return SLICE_SINGLETHREAD;

            }

        }

    }

    h->qp_thresh = 15 -

                   FFMIN(h->slice_alpha_c0_offset, h->slice_beta_offset) -

                   FFMAX3(0,

                          h->pps.chroma_qp_index_offset[0],

                          h->pps.chroma_qp_index_offset[1]) +

                   6 * (h->sps.bit_depth_luma - 8);



    h0->last_slice_type = slice_type;

    memcpy(h0->last_ref_count, h0->ref_count, sizeof(h0->last_ref_count));

    h->slice_num        = ++h0->current_slice;



    if (h->slice_num)

        h0->slice_row[(h->slice_num-1)&(MAX_SLICES-1)]= h->resync_mb_y;

    if (   h0->slice_row[h->slice_num&(MAX_SLICES-1)] + 3 >= h->resync_mb_y

        && h0->slice_row[h->slice_num&(MAX_SLICES-1)] <= h->resync_mb_y

        && h->slice_num >= MAX_SLICES) {

        

        av_log(h->avctx, AV_LOG_WARNING, "Possibly too many slices (%d >= %d), increase MAX_SLICES and recompile if there are artifacts\n", h->slice_num, MAX_SLICES);

    }



    for (j = 0; j < 2; j++) {

        int id_list[16];

        int *ref2frm = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][j];

        for (i = 0; i < 16; i++) {

            id_list[i] = 60;

            if (j < h->list_count && i < h->ref_count[j] &&

                h->ref_list[j][i].f.buf[0]) {

                int k;

                AVBuffer *buf = h->ref_list[j][i].f.buf[0]->buffer;

                for (k = 0; k < h->short_ref_count; k++)

                    if (h->short_ref[k]->f.buf[0]->buffer == buf) {

                        id_list[i] = k;

                        break;

                    }

                for (k = 0; k < h->long_ref_count; k++)

                    if (h->long_ref[k] && h->long_ref[k]->f.buf[0]->buffer == buf) {

                        id_list[i] = h->short_ref_count + k;

                        break;

                    }

            }

        }



        ref2frm[0] =

        ref2frm[1] = -1;

        for (i = 0; i < 16; i++)

            ref2frm[i + 2] = 4 * id_list[i] + (h->ref_list[j][i].reference & 3);

        ref2frm[18 + 0] =

        ref2frm[18 + 1] = -1;

        for (i = 16; i < 48; i++)

            ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +

                             (h->ref_list[j][i].reference & 3);

    }



    if (h->ref_count[0]) ff_h264_set_erpic(&h->er.last_pic, &h->ref_list[0][0]);

    if (h->ref_count[1]) ff_h264_set_erpic(&h->er.next_pic, &h->ref_list[1][0]);



    h->er.ref_count = h->ref_count[0];

    h0->au_pps_id = pps_id;

    h->sps.new =

    h0->sps_buffers[h->pps.sps_id]->new = 0;

    h->current_sps_id = h->pps.sps_id;



    if (h->avctx->debug & FF_DEBUG_PICT_INFO) {

        av_log(h->avctx, AV_LOG_DEBUG,

               "slice:%d %s mb:%d %c%s%s pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s %s\n",

               h->slice_num,

               (h->picture_structure == PICT_FRAME ? "F" : h->picture_structure == PICT_TOP_FIELD ? "T" : "B"),

               first_mb_in_slice,

               av_get_picture_type_char(h->slice_type),

               h->slice_type_fixed ? " fix" : "",

               h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",

               pps_id, h->frame_num,

               h->cur_pic_ptr->field_poc[0],

               h->cur_pic_ptr->field_poc[1],

               h->ref_count[0], h->ref_count[1],

               h->qscale,

               h->deblocking_filter,

               h->slice_alpha_c0_offset, h->slice_beta_offset,

               h->use_weight,

               h->use_weight == 1 && h->use_weight_chroma ? "c" : "",

               h->slice_type == AV_PICTURE_TYPE_B ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");

    }



    return 0;

}

int FUNC_0(H264Context *VAR_0, H264Context *VAR_1)

{

    unsigned int VAR_2;

    unsigned int VAR_3;

    int VAR_4;

    unsigned int VAR_5, VAR_6, VAR_7, VAR_8;

    int VAR_9, VAR_10;

    int VAR_11;

    int VAR_12 = 0;

    int VAR_13, VAR_14;



    VAR_0->qpel_put = VAR_0->h264qpel.put_h264_qpel_pixels_tab;

    VAR_0->qpel_avg = VAR_0->h264qpel.avg_h264_qpel_pixels_tab;



    VAR_2 = get_ue_golomb_long(&VAR_0->gb);



    if (VAR_2 == 0) { 

        if (VAR_1->current_slice && VAR_0->cur_pic_ptr && FIELD_PICTURE(VAR_0)) {

            ff_h264_field_end(VAR_0, 1);

        }



        VAR_1->current_slice = 0;

        if (!VAR_1->first_field) {

            if (VAR_0->cur_pic_ptr && !VAR_0->droppable) {

                ff_thread_report_progress(&VAR_0->cur_pic_ptr->tf, INT_MAX,

                                          VAR_0->picture_structure == PICT_BOTTOM_FIELD);

            }

            VAR_0->cur_pic_ptr = NULL;

        }

    }



    VAR_5 = get_ue_golomb_31(&VAR_0->gb);

    if (VAR_5 > 9) {

        av_log(VAR_0->avctx, AV_LOG_ERROR,

               "slice type %d too large at %d %d\n",

               VAR_5, VAR_0->mb_x, VAR_0->mb_y);

        return AVERROR_INVALIDDATA;

    }

    if (VAR_5 > 4) {

        VAR_5 -= 5;

        VAR_0->slice_type_fixed = 1;

    } else

        VAR_0->slice_type_fixed = 0;



    VAR_5 = golomb_to_pict_type[VAR_5];

    VAR_0->VAR_5     = VAR_5;

    VAR_0->slice_type_nos = VAR_5 & 3;



    if (VAR_0->nal_unit_type  == NAL_IDR_SLICE &&

        VAR_0->slice_type_nos != AV_PICTURE_TYPE_I) {

        av_log(VAR_0->avctx, AV_LOG_ERROR, "A non-intra slice in an IDR NAL unit.\n");

        return AVERROR_INVALIDDATA;

    }



    if (

        (VAR_0->avctx->skip_frame >= AVDISCARD_NONREF && !VAR_0->nal_ref_idc) ||

        (VAR_0->avctx->skip_frame >= AVDISCARD_BIDIR  && VAR_0->slice_type_nos == AV_PICTURE_TYPE_B) ||

        (VAR_0->avctx->skip_frame >= AVDISCARD_NONINTRA && VAR_0->slice_type_nos != AV_PICTURE_TYPE_I) ||

        (VAR_0->avctx->skip_frame >= AVDISCARD_NONKEY && VAR_0->nal_unit_type != NAL_IDR_SLICE) ||

         VAR_0->avctx->skip_frame >= AVDISCARD_ALL) {

         return SLICE_SKIPED;

     }



    

    VAR_0->pict_type = VAR_0->VAR_5;



    VAR_3 = get_ue_golomb(&VAR_0->gb);

    if (VAR_3 >= MAX_PPS_COUNT) {

        av_log(VAR_0->avctx, AV_LOG_ERROR, "VAR_3 %u out of range\n", VAR_3);

        return AVERROR_INVALIDDATA;

    }

    if (!VAR_1->pps_buffers[VAR_3]) {

        av_log(VAR_0->avctx, AV_LOG_ERROR,

               "non-existing PPS %u referenced\n",

               VAR_3);

        return AVERROR_INVALIDDATA;

    }

    if (VAR_1->au_pps_id >= 0 && VAR_3 != VAR_1->au_pps_id) {

        av_log(VAR_0->avctx, AV_LOG_ERROR,

               "PPS change from %d to %d forbidden\n",

               VAR_1->au_pps_id, VAR_3);

        return AVERROR_INVALIDDATA;

    }

    VAR_0->pps = *VAR_1->pps_buffers[VAR_3];



    if (!VAR_1->sps_buffers[VAR_0->pps.sps_id]) {

        av_log(VAR_0->avctx, AV_LOG_ERROR,

               "non-existing SPS %u referenced\n",

               VAR_0->pps.sps_id);

        return AVERROR_INVALIDDATA;

    }



    if (VAR_0->pps.sps_id != VAR_0->sps.sps_id ||

        VAR_0->pps.sps_id != VAR_0->current_sps_id ||

        VAR_1->sps_buffers[VAR_0->pps.sps_id]->new) {



        VAR_0->sps = *VAR_1->sps_buffers[VAR_0->pps.sps_id];



        if (VAR_0->mb_width  != VAR_0->sps.mb_width ||

            VAR_0->mb_height != VAR_0->sps.mb_height * (2 - VAR_0->sps.frame_mbs_only_flag) ||

            VAR_0->avctx->bits_per_raw_sample != VAR_0->sps.bit_depth_luma ||

            VAR_0->cur_chroma_format_idc != VAR_0->sps.chroma_format_idc

        )

            VAR_12 = 1;



        if (VAR_0->bit_depth_luma    != VAR_0->sps.bit_depth_luma ||

            VAR_0->chroma_format_idc != VAR_0->sps.chroma_format_idc) {

            VAR_0->bit_depth_luma    = VAR_0->sps.bit_depth_luma;

            VAR_0->chroma_format_idc = VAR_0->sps.chroma_format_idc;

            VAR_12         = 1;

        }

        if ((VAR_4 = ff_h264_set_parameter_from_sps(VAR_0)) < 0)

            return VAR_4;

    }



    VAR_0->avctx->profile = ff_h264_get_profile(&VAR_0->sps);

    VAR_0->avctx->level   = VAR_0->sps.level_idc;

    VAR_0->avctx->refs    = VAR_0->sps.ref_frame_count;



    VAR_11 = (VAR_0->context_initialized &&

                    (   16*VAR_0->sps.mb_width != VAR_0->avctx->coded_width

                     || 16*VAR_0->sps.mb_height * (2 - VAR_0->sps.frame_mbs_only_flag) != VAR_0->avctx->coded_height

                     || VAR_0->avctx->bits_per_raw_sample != VAR_0->sps.bit_depth_luma

                     || VAR_0->cur_chroma_format_idc != VAR_0->sps.chroma_format_idc

                     || av_cmp_q(VAR_0->sps.sar, VAR_0->avctx->sample_aspect_ratio)

                     || VAR_0->mb_width  != VAR_0->sps.mb_width

                     || VAR_0->mb_height != VAR_0->sps.mb_height * (2 - VAR_0->sps.frame_mbs_only_flag)

                    ));

    if (non_j_pixfmt(VAR_1->avctx->pix_fmt) != non_j_pixfmt(get_pixel_format(VAR_1, 0)))

        VAR_11 = 1;



    VAR_0->mb_width  = VAR_0->sps.mb_width;

    VAR_0->mb_height = VAR_0->sps.mb_height * (2 - VAR_0->sps.frame_mbs_only_flag);

    VAR_0->mb_num    = VAR_0->mb_width * VAR_0->mb_height;

    VAR_0->mb_stride = VAR_0->mb_width + 1;



    VAR_0->b_stride = VAR_0->mb_width * 4;



    VAR_0->chroma_y_shift = VAR_0->sps.chroma_format_idc <= 1; 



    VAR_0->width  = 16 * VAR_0->mb_width;

    VAR_0->height = 16 * VAR_0->mb_height;



    VAR_4 = init_dimensions(VAR_0);

    if (VAR_4 < 0)

        return VAR_4;



    if (VAR_0->sps.video_signal_type_present_flag) {

        VAR_0->avctx->color_range = VAR_0->sps.full_range>0 ? AVCOL_RANGE_JPEG

                                                    : AVCOL_RANGE_MPEG;

        if (VAR_0->sps.colour_description_present_flag) {

            if (VAR_0->avctx->colorspace != VAR_0->sps.colorspace)

                VAR_12 = 1;

            VAR_0->avctx->color_primaries = VAR_0->sps.color_primaries;

            VAR_0->avctx->color_trc       = VAR_0->sps.color_trc;

            VAR_0->avctx->colorspace      = VAR_0->sps.colorspace;

        }

    }



    if (VAR_0->context_initialized &&

        (VAR_11 || VAR_12)) {

        if (VAR_0 != VAR_1) {

            av_log(VAR_0->avctx, AV_LOG_ERROR,

                   "changing width %d -> %d / height %d -> %d on "

                   "slice %d\n",

                   VAR_0->width, VAR_0->avctx->coded_width,

                   VAR_0->height, VAR_0->avctx->coded_height,

                   VAR_1->current_slice + 1);

            return AVERROR_INVALIDDATA;

        }



        ff_h264_flush_change(VAR_0);



        if ((VAR_4 = get_pixel_format(VAR_0, 1)) < 0)

            return VAR_4;

        VAR_0->avctx->pix_fmt = VAR_4;



        av_log(VAR_0->avctx, AV_LOG_INFO, "Reinit context to %dx%d, "

               "pix_fmt: %s\n", VAR_0->width, VAR_0->height, av_get_pix_fmt_name(VAR_0->avctx->pix_fmt));



        if ((VAR_4 = h264_slice_header_init(VAR_0, 1)) < 0) {

            av_log(VAR_0->avctx, AV_LOG_ERROR,

                   "h264_slice_header_init() failed\n");

            return VAR_4;

        }

    }

    if (!VAR_0->context_initialized) {

        if (VAR_0 != VAR_1) {

            av_log(VAR_0->avctx, AV_LOG_ERROR,

                   "Cannot (re-)initialize context during parallel decoding.\n");

            return AVERROR_PATCHWELCOME;

        }



        if ((VAR_4 = get_pixel_format(VAR_0, 1)) < 0)

            return VAR_4;

        VAR_0->avctx->pix_fmt = VAR_4;



        if ((VAR_4 = h264_slice_header_init(VAR_0, 0)) < 0) {

            av_log(VAR_0->avctx, AV_LOG_ERROR,

                   "h264_slice_header_init() failed\n");

            return VAR_4;

        }

    }



    if (VAR_0 == VAR_1 && VAR_0->dequant_coeff_pps != VAR_3) {

        VAR_0->dequant_coeff_pps = VAR_3;

        h264_init_dequant_tables(VAR_0);

    }



    VAR_0->frame_num = get_bits(&VAR_0->gb, VAR_0->sps.log2_max_frame_num);



    VAR_0->mb_mbaff        = 0;

    VAR_0->mb_aff_frame    = 0;

    VAR_9 = VAR_1->picture_structure;

    VAR_10 = VAR_1->droppable;

    VAR_0->droppable       = VAR_0->nal_ref_idc == 0;

    if (VAR_0->sps.frame_mbs_only_flag) {

        VAR_0->picture_structure = PICT_FRAME;

    } else {

        if (!VAR_0->sps.direct_8x8_inference_flag && VAR_5 == AV_PICTURE_TYPE_B) {

            av_log(VAR_0->avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");

            return -1;

        }

        VAR_13 = get_bits1(&VAR_0->gb);

        if (VAR_13) {

            VAR_14 = get_bits1(&VAR_0->gb);

            VAR_0->picture_structure = PICT_TOP_FIELD + VAR_14;

        } else {

            VAR_0->picture_structure = PICT_FRAME;

            VAR_0->mb_aff_frame      = VAR_0->sps.mb_aff;

        }

    }

    VAR_0->mb_field_decoding_flag = VAR_0->picture_structure != PICT_FRAME;



    if (VAR_1->current_slice != 0) {

        if (VAR_9 != VAR_0->picture_structure ||

            VAR_10 != VAR_0->droppable) {

            av_log(VAR_0->avctx, AV_LOG_ERROR,

                   "Changing field mode (%d -> %d) between slices is not allowed\n",

                   VAR_9, VAR_0->picture_structure);

            VAR_0->picture_structure = VAR_9;

            VAR_0->droppable         = VAR_10;

            return AVERROR_INVALIDDATA;

        } else if (!VAR_1->cur_pic_ptr) {

            av_log(VAR_0->avctx, AV_LOG_ERROR,

                   "unset cur_pic_ptr on slice %d\n",

                   VAR_1->current_slice + 1);

            return AVERROR_INVALIDDATA;

        }

    } else {

        



        if (VAR_0->frame_num != VAR_0->prev_frame_num) {

            int VAR_15 = VAR_0->prev_frame_num;

            int VAR_16         = 1 << VAR_0->sps.log2_max_frame_num;



            if (VAR_15 > VAR_0->frame_num)

                VAR_15 -= VAR_16;



            if ((VAR_0->frame_num - VAR_15) > VAR_0->sps.ref_frame_count) {

                VAR_15 = (VAR_0->frame_num - VAR_0->sps.ref_frame_count) - 1;

                if (VAR_15 < 0)

                    VAR_15 += VAR_16;



                VAR_0->prev_frame_num = VAR_15;

            }

        }



        









        if (VAR_1->first_field) {

            assert(VAR_1->cur_pic_ptr);

            assert(VAR_1->cur_pic_ptr->f.buf[0]);

            assert(VAR_1->cur_pic_ptr->reference != DELAYED_PIC_REF);



            

            if (VAR_1->cur_pic_ptr->tf.owner == VAR_1->avctx) {

                ff_thread_report_progress(&VAR_1->cur_pic_ptr->tf, INT_MAX,

                                          VAR_9 == PICT_BOTTOM_FIELD);

            }



            

            if (!FIELD_PICTURE(VAR_0) || VAR_0->picture_structure == VAR_9) {

                



                if (VAR_9 != PICT_FRAME) {

                    ff_thread_report_progress(&VAR_1->cur_pic_ptr->tf, INT_MAX,

                                              VAR_9 == PICT_TOP_FIELD);

                }

            } else {

                if (VAR_1->cur_pic_ptr->frame_num != VAR_0->frame_num) {

                    







                    if (VAR_9 != PICT_FRAME) {

                        ff_thread_report_progress(&VAR_1->cur_pic_ptr->tf, INT_MAX,

                                                  VAR_9 == PICT_TOP_FIELD);

                    }

                } else {

                    

                    if (!((VAR_9   == PICT_TOP_FIELD &&

                           VAR_0->picture_structure == PICT_BOTTOM_FIELD) ||

                          (VAR_9   == PICT_BOTTOM_FIELD &&

                           VAR_0->picture_structure == PICT_TOP_FIELD))) {

                        av_log(VAR_0->avctx, AV_LOG_ERROR,

                               "Invalid field mode combination %d/%d\n",

                               VAR_9, VAR_0->picture_structure);

                        VAR_0->picture_structure = VAR_9;

                        VAR_0->droppable         = VAR_10;

                        return AVERROR_INVALIDDATA;

                    } else if (VAR_10 != VAR_0->droppable) {

                        avpriv_request_sample(VAR_0->avctx,

                                              "Found reference and non-reference fields in the same frame, which");

                        VAR_0->picture_structure = VAR_9;

                        VAR_0->droppable         = VAR_10;

                        return AVERROR_PATCHWELCOME;

                    }

                }

            }

        }



        while (VAR_0->frame_num != VAR_0->prev_frame_num && !VAR_1->first_field &&

               VAR_0->frame_num != (VAR_0->prev_frame_num + 1) % (1 << VAR_0->sps.log2_max_frame_num)) {

            H264Picture *prev = VAR_0->short_ref_count ? VAR_0->short_ref[0] : NULL;

            av_log(VAR_0->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",

                   VAR_0->frame_num, VAR_0->prev_frame_num);

            if (!VAR_0->sps.gaps_in_frame_num_allowed_flag)

                for(VAR_7=0; VAR_7<FF_ARRAY_ELEMS(VAR_0->last_pocs); VAR_7++)

                    VAR_0->last_pocs[VAR_7] = INT_MIN;

            VAR_4 = h264_frame_start(VAR_0);

            if (VAR_4 < 0) {

                VAR_1->first_field = 0;

                return VAR_4;

            }



            VAR_0->prev_frame_num++;

            VAR_0->prev_frame_num        %= 1 << VAR_0->sps.log2_max_frame_num;

            VAR_0->cur_pic_ptr->frame_num = VAR_0->prev_frame_num;

            VAR_0->cur_pic_ptr->invalid_gap = !VAR_0->sps.gaps_in_frame_num_allowed_flag;

            ff_thread_report_progress(&VAR_0->cur_pic_ptr->tf, INT_MAX, 0);

            ff_thread_report_progress(&VAR_0->cur_pic_ptr->tf, INT_MAX, 1);

            VAR_4 = ff_generate_sliding_window_mmcos(VAR_0, 1);

            if (VAR_4 < 0 && (VAR_0->avctx->err_recognition & AV_EF_EXPLODE))

                return VAR_4;

            VAR_4 = ff_h264_execute_ref_pic_marking(VAR_0, VAR_0->mmco, VAR_0->mmco_index);

            if (VAR_4 < 0 && (VAR_0->avctx->err_recognition & AV_EF_EXPLODE))

                return VAR_4;

            













            if (VAR_0->short_ref_count) {

                if (prev) {

                    av_image_copy(VAR_0->short_ref[0]->f.data,

                                  VAR_0->short_ref[0]->f.linesize,

                                  (const uint8_t **)prev->f.data,

                                  prev->f.linesize,

                                  VAR_0->avctx->pix_fmt,

                                  VAR_0->mb_width  * 16,

                                  VAR_0->mb_height * 16);

                    VAR_0->short_ref[0]->poc = prev->poc + 2;

                }

                VAR_0->short_ref[0]->frame_num = VAR_0->prev_frame_num;

            }

        }



        





        if (VAR_1->first_field) {

            assert(VAR_1->cur_pic_ptr);

            assert(VAR_1->cur_pic_ptr->f.buf[0]);

            assert(VAR_1->cur_pic_ptr->reference != DELAYED_PIC_REF);



            

            if (!FIELD_PICTURE(VAR_0) || VAR_0->picture_structure == VAR_9) {

                



                VAR_1->cur_pic_ptr = NULL;

                VAR_1->first_field = FIELD_PICTURE(VAR_0);

            } else {

                if (VAR_1->cur_pic_ptr->frame_num != VAR_0->frame_num) {

                    ff_thread_report_progress(&VAR_1->cur_pic_ptr->tf, INT_MAX,

                                              VAR_1->picture_structure==PICT_BOTTOM_FIELD);

                    





                    VAR_1->first_field = 1;

                    VAR_1->cur_pic_ptr = NULL;

                } else {

                    

                    VAR_1->first_field = 0;

                }

            }

        } else {

            

            VAR_1->first_field = FIELD_PICTURE(VAR_0);

        }



        if (!FIELD_PICTURE(VAR_0) || VAR_1->first_field) {

            if (h264_frame_start(VAR_0) < 0) {

                VAR_1->first_field = 0;

                return AVERROR_INVALIDDATA;

            }

        } else {

            release_unused_pictures(VAR_0, 0);

        }

        



        if (FIELD_PICTURE(VAR_0)) {

            for(VAR_7 = (VAR_0->picture_structure == PICT_BOTTOM_FIELD); VAR_7<VAR_0->mb_height; VAR_7++)

                memset(VAR_0->slice_table + VAR_7*VAR_0->mb_stride, -1, (VAR_0->mb_stride - (VAR_7+1==VAR_0->mb_height)) * sizeof(*VAR_0->slice_table));

        } else {

            memset(VAR_0->slice_table, -1,

                (VAR_0->mb_height * VAR_0->mb_stride - 1) * sizeof(*VAR_0->slice_table));

        }

        VAR_1->last_slice_type = -1;

    }

    if (VAR_0 != VAR_1 && (VAR_4 = clone_slice(VAR_0, VAR_1)) < 0)

        return VAR_4;



    



    for (VAR_7 = 0; VAR_7 < VAR_0->slice_context_count; VAR_7++)

        if (VAR_0->thread_context[VAR_7]) {

            VAR_4 = alloc_scratch_buffers(VAR_0->thread_context[VAR_7], VAR_0->linesize);

            if (VAR_4 < 0)

                return VAR_4;

        }



    VAR_0->cur_pic_ptr->frame_num = VAR_0->frame_num; 



    av_assert1(VAR_0->mb_num == VAR_0->mb_width * VAR_0->mb_height);

    if (VAR_2 << FIELD_OR_MBAFF_PICTURE(VAR_0) >= VAR_0->mb_num ||

        VAR_2 >= VAR_0->mb_num) {

        av_log(VAR_0->avctx, AV_LOG_ERROR, "VAR_2 overflow\n");

        return AVERROR_INVALIDDATA;

    }

    VAR_0->resync_mb_x = VAR_0->mb_x =  VAR_2 % VAR_0->mb_width;

    VAR_0->resync_mb_y = VAR_0->mb_y = (VAR_2 / VAR_0->mb_width) <<

                               FIELD_OR_MBAFF_PICTURE(VAR_0);

    if (VAR_0->picture_structure == PICT_BOTTOM_FIELD)

        VAR_0->resync_mb_y = VAR_0->mb_y = VAR_0->mb_y + 1;

    av_assert1(VAR_0->mb_y < VAR_0->mb_height);



    if (VAR_0->picture_structure == PICT_FRAME) {

        VAR_0->curr_pic_num = VAR_0->frame_num;

        VAR_0->max_pic_num  = 1 << VAR_0->sps.log2_max_frame_num;

    } else {

        VAR_0->curr_pic_num = 2 * VAR_0->frame_num + 1;

        VAR_0->max_pic_num  = 1 << (VAR_0->sps.log2_max_frame_num + 1);

    }



    if (VAR_0->nal_unit_type == NAL_IDR_SLICE)

        get_ue_golomb(&VAR_0->gb); 



    if (VAR_0->sps.poc_type == 0) {

        VAR_0->poc_lsb = get_bits(&VAR_0->gb, VAR_0->sps.log2_max_poc_lsb);



        if (VAR_0->pps.pic_order_present == 1 && VAR_0->picture_structure == PICT_FRAME)

            VAR_0->delta_poc_bottom = get_se_golomb(&VAR_0->gb);

    }



    if (VAR_0->sps.poc_type == 1 && !VAR_0->sps.delta_pic_order_always_zero_flag) {

        VAR_0->delta_poc[0] = get_se_golomb(&VAR_0->gb);



        if (VAR_0->pps.pic_order_present == 1 && VAR_0->picture_structure == PICT_FRAME)

            VAR_0->delta_poc[1] = get_se_golomb(&VAR_0->gb);

    }



    ff_init_poc(VAR_0, VAR_0->cur_pic_ptr->field_poc, &VAR_0->cur_pic_ptr->poc);



    if (VAR_0->pps.redundant_pic_cnt_present)

        VAR_0->redundant_pic_count = get_ue_golomb(&VAR_0->gb);



    VAR_4 = ff_set_ref_count(VAR_0);

    if (VAR_4 < 0)

        return VAR_4;



    if (VAR_5 != AV_PICTURE_TYPE_I &&

        (VAR_1->current_slice == 0 ||

         VAR_5 != VAR_1->last_slice_type ||

         memcmp(VAR_1->last_ref_count, VAR_1->ref_count, sizeof(VAR_1->ref_count)))) {



        ff_h264_fill_default_ref_list(VAR_0);

    }



    if (VAR_0->slice_type_nos != AV_PICTURE_TYPE_I) {

       VAR_4 = ff_h264_decode_ref_pic_list_reordering(VAR_0);

       if (VAR_4 < 0) {

           VAR_0->ref_count[1] = VAR_0->ref_count[0] = 0;

           return VAR_4;

       }

    }



    if ((VAR_0->pps.weighted_pred && VAR_0->slice_type_nos == AV_PICTURE_TYPE_P) ||

        (VAR_0->pps.weighted_bipred_idc == 1 &&

         VAR_0->slice_type_nos == AV_PICTURE_TYPE_B))

        ff_pred_weight_table(VAR_0);

    else if (VAR_0->pps.weighted_bipred_idc == 2 &&

             VAR_0->slice_type_nos == AV_PICTURE_TYPE_B) {

        implicit_weight_table(VAR_0, -1);

    } else {

        VAR_0->use_weight = 0;

        for (VAR_7 = 0; VAR_7 < 2; VAR_7++) {

            VAR_0->luma_weight_flag[VAR_7]   = 0;

            VAR_0->chroma_weight_flag[VAR_7] = 0;

        }

    }



    

    

    

    

    

    if (VAR_0->nal_ref_idc) {

        VAR_4 = ff_h264_decode_ref_pic_marking(VAR_1, &VAR_0->gb,

                                             !(VAR_0->avctx->active_thread_type & FF_THREAD_FRAME) ||

                                             VAR_1->current_slice == 0);

        if (VAR_4 < 0 && (VAR_0->avctx->err_recognition & AV_EF_EXPLODE))

            return AVERROR_INVALIDDATA;

    }



    if (FRAME_MBAFF(VAR_0)) {

        ff_h264_fill_mbaff_ref_list(VAR_0);



        if (VAR_0->pps.weighted_bipred_idc == 2 && VAR_0->slice_type_nos == AV_PICTURE_TYPE_B) {

            implicit_weight_table(VAR_0, 0);

            implicit_weight_table(VAR_0, 1);

        }

    }



    if (VAR_0->slice_type_nos == AV_PICTURE_TYPE_B && !VAR_0->direct_spatial_mv_pred)

        ff_h264_direct_dist_scale_factor(VAR_0);

    ff_h264_direct_ref_list_init(VAR_0);



    if (VAR_0->slice_type_nos != AV_PICTURE_TYPE_I && VAR_0->pps.cabac) {

        VAR_6 = get_ue_golomb_31(&VAR_0->gb);

        if (VAR_6 > 2) {

            av_log(VAR_0->avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\n", VAR_6);

            return AVERROR_INVALIDDATA;

        }

        VAR_0->cabac_init_idc = VAR_6;

    }



    VAR_0->last_qscale_diff = 0;

    VAR_6 = VAR_0->pps.init_qp + get_se_golomb(&VAR_0->gb);

    if (VAR_6 > 51 + 6 * (VAR_0->sps.bit_depth_luma - 8)) {

        av_log(VAR_0->avctx, AV_LOG_ERROR, "QP %u out of range\n", VAR_6);

        return AVERROR_INVALIDDATA;

    }

    VAR_0->qscale       = VAR_6;

    VAR_0->chroma_qp[0] = get_chroma_qp(VAR_0, 0, VAR_0->qscale);

    VAR_0->chroma_qp[1] = get_chroma_qp(VAR_0, 1, VAR_0->qscale);

    

    if (VAR_0->VAR_5 == AV_PICTURE_TYPE_SP)

        get_bits1(&VAR_0->gb); 

    if (VAR_0->VAR_5 == AV_PICTURE_TYPE_SP ||

        VAR_0->VAR_5 == AV_PICTURE_TYPE_SI)

        get_se_golomb(&VAR_0->gb); 



    VAR_0->deblocking_filter     = 1;

    VAR_0->slice_alpha_c0_offset = 0;

    VAR_0->slice_beta_offset     = 0;

    if (VAR_0->pps.deblocking_filter_parameters_present) {

        VAR_6 = get_ue_golomb_31(&VAR_0->gb);

        if (VAR_6 > 2) {

            av_log(VAR_0->avctx, AV_LOG_ERROR,

                   "deblocking_filter_idc %u out of range\n", VAR_6);

            return AVERROR_INVALIDDATA;

        }

        VAR_0->deblocking_filter = VAR_6;

        if (VAR_0->deblocking_filter < 2)

            VAR_0->deblocking_filter ^= 1;  



        if (VAR_0->deblocking_filter) {

            VAR_0->slice_alpha_c0_offset = get_se_golomb(&VAR_0->gb) * 2;

            VAR_0->slice_beta_offset     = get_se_golomb(&VAR_0->gb) * 2;

            if (VAR_0->slice_alpha_c0_offset >  12 ||

                VAR_0->slice_alpha_c0_offset < -12 ||

                VAR_0->slice_beta_offset >  12     ||

                VAR_0->slice_beta_offset < -12) {

                av_log(VAR_0->avctx, AV_LOG_ERROR,

                       "deblocking filter parameters %d %d out of range\n",

                       VAR_0->slice_alpha_c0_offset, VAR_0->slice_beta_offset);

                return AVERROR_INVALIDDATA;

            }

        }

    }



    if (VAR_0->avctx->skip_loop_filter >= AVDISCARD_ALL ||

        (VAR_0->avctx->skip_loop_filter >= AVDISCARD_NONKEY &&

         VAR_0->nal_unit_type != NAL_IDR_SLICE) ||

        (VAR_0->avctx->skip_loop_filter >= AVDISCARD_NONINTRA &&

         VAR_0->slice_type_nos != AV_PICTURE_TYPE_I) ||

        (VAR_0->avctx->skip_loop_filter >= AVDISCARD_BIDIR  &&

         VAR_0->slice_type_nos == AV_PICTURE_TYPE_B) ||

        (VAR_0->avctx->skip_loop_filter >= AVDISCARD_NONREF &&

         VAR_0->nal_ref_idc == 0))

        VAR_0->deblocking_filter = 0;



    if (VAR_0->deblocking_filter == 1 && VAR_1->max_contexts > 1) {

        if (VAR_0->avctx->flags2 & CODEC_FLAG2_FAST) {

            



            VAR_0->deblocking_filter = 2;

        } else {

            VAR_1->max_contexts = 1;

            if (!VAR_1->single_decode_warning) {

                av_log(VAR_0->avctx, AV_LOG_INFO,

                       "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");

                VAR_1->single_decode_warning = 1;

            }

            if (VAR_0 != VAR_1) {

                av_log(VAR_0->avctx, AV_LOG_ERROR,

                       "Deblocking switched inside frame.\n");

                return SLICE_SINGLETHREAD;

            }

        }

    }

    VAR_0->qp_thresh = 15 -

                   FFMIN(VAR_0->slice_alpha_c0_offset, VAR_0->slice_beta_offset) -

                   FFMAX3(0,

                          VAR_0->pps.chroma_qp_index_offset[0],

                          VAR_0->pps.chroma_qp_index_offset[1]) +

                   6 * (VAR_0->sps.bit_depth_luma - 8);



    VAR_1->last_slice_type = VAR_5;

    memcpy(VAR_1->last_ref_count, VAR_1->ref_count, sizeof(VAR_1->last_ref_count));

    VAR_0->slice_num        = ++VAR_1->current_slice;



    if (VAR_0->slice_num)

        VAR_1->slice_row[(VAR_0->slice_num-1)&(MAX_SLICES-1)]= VAR_0->resync_mb_y;

    if (   VAR_1->slice_row[VAR_0->slice_num&(MAX_SLICES-1)] + 3 >= VAR_0->resync_mb_y

        && VAR_1->slice_row[VAR_0->slice_num&(MAX_SLICES-1)] <= VAR_0->resync_mb_y

        && VAR_0->slice_num >= MAX_SLICES) {

        

        av_log(VAR_0->avctx, AV_LOG_WARNING, "Possibly too many slices (%d >= %d), increase MAX_SLICES and recompile if there are artifacts\n", VAR_0->slice_num, MAX_SLICES);

    }



    for (VAR_8 = 0; VAR_8 < 2; VAR_8++) {

        int VAR_17[16];

        int *VAR_18 = VAR_0->VAR_18[VAR_0->slice_num & (MAX_SLICES - 1)][VAR_8];

        for (VAR_7 = 0; VAR_7 < 16; VAR_7++) {

            VAR_17[VAR_7] = 60;

            if (VAR_8 < VAR_0->list_count && VAR_7 < VAR_0->ref_count[VAR_8] &&

                VAR_0->ref_list[VAR_8][VAR_7].f.buf[0]) {

                int VAR_19;

                AVBuffer *buf = VAR_0->ref_list[VAR_8][VAR_7].f.buf[0]->buffer;

                for (VAR_19 = 0; VAR_19 < VAR_0->short_ref_count; VAR_19++)

                    if (VAR_0->short_ref[VAR_19]->f.buf[0]->buffer == buf) {

                        VAR_17[VAR_7] = VAR_19;

                        break;

                    }

                for (VAR_19 = 0; VAR_19 < VAR_0->long_ref_count; VAR_19++)

                    if (VAR_0->long_ref[VAR_19] && VAR_0->long_ref[VAR_19]->f.buf[0]->buffer == buf) {

                        VAR_17[VAR_7] = VAR_0->short_ref_count + VAR_19;

                        break;

                    }

            }

        }



        VAR_18[0] =

        VAR_18[1] = -1;

        for (VAR_7 = 0; VAR_7 < 16; VAR_7++)

            VAR_18[VAR_7 + 2] = 4 * VAR_17[VAR_7] + (VAR_0->ref_list[VAR_8][VAR_7].reference & 3);

        VAR_18[18 + 0] =

        VAR_18[18 + 1] = -1;

        for (VAR_7 = 16; VAR_7 < 48; VAR_7++)

            VAR_18[VAR_7 + 4] = 4 * VAR_17[(VAR_7 - 16) >> 1] +

                             (VAR_0->ref_list[VAR_8][VAR_7].reference & 3);

    }



    if (VAR_0->ref_count[0]) ff_h264_set_erpic(&VAR_0->er.last_pic, &VAR_0->ref_list[0][0]);

    if (VAR_0->ref_count[1]) ff_h264_set_erpic(&VAR_0->er.next_pic, &VAR_0->ref_list[1][0]);



    VAR_0->er.ref_count = VAR_0->ref_count[0];

    VAR_1->au_pps_id = VAR_3;

    VAR_0->sps.new =

    VAR_1->sps_buffers[VAR_0->pps.sps_id]->new = 0;

    VAR_0->current_sps_id = VAR_0->pps.sps_id;



    if (VAR_0->avctx->debug & FF_DEBUG_PICT_INFO) {

        av_log(VAR_0->avctx, AV_LOG_DEBUG,

               "slice:%d %s mb:%d %c%s%s pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s %s\n",

               VAR_0->slice_num,

               (VAR_0->picture_structure == PICT_FRAME ? "F" : VAR_0->picture_structure == PICT_TOP_FIELD ? "T" : "B"),

               VAR_2,

               av_get_picture_type_char(VAR_0->VAR_5),

               VAR_0->slice_type_fixed ? " fix" : "",

               VAR_0->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",

               VAR_3, VAR_0->frame_num,

               VAR_0->cur_pic_ptr->field_poc[0],

               VAR_0->cur_pic_ptr->field_poc[1],

               VAR_0->ref_count[0], VAR_0->ref_count[1],

               VAR_0->qscale,

               VAR_0->deblocking_filter,

               VAR_0->slice_alpha_c0_offset, VAR_0->slice_beta_offset,

               VAR_0->use_weight,

               VAR_0->use_weight == 1 && VAR_0->use_weight_chroma ? "c" : "",

               VAR_0->VAR_5 == AV_PICTURE_TYPE_B ? (VAR_0->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");

    }



    return 0;

}

int FUNC_0(H264Context *VAR_0, H264Context *VAR_1)
{ unsigned int VAR_2; unsigned int VAR_3; int VAR_4; unsigned int VAR_5, VAR_6, VAR_7, VAR_8; int VAR_9, VAR_10; int VAR_11; int VAR_12 = 0; int VAR_13, VAR_14; VAR_0->qpel_put = VAR_0->h264qpel.put_h264_qpel_pixels_tab; VAR_0->qpel_avg = VAR_0->h264qpel.avg_h264_qpel_pixels_tab; VAR_2 = get_ue_golomb_long(&VAR_0->gb); if (VAR_2 == 0) { if (VAR_1->current_slice && VAR_0->cur_pic_ptr && FIELD_PICTURE(VAR_0)) { ff_h264_field_end(VAR_0, 1); } VAR_1->current_slice = 0; if (!VAR_1->first_field) { if (VAR_0->cur_pic_ptr && !VAR_0->droppable) { ff_thread_report_progress(&VAR_0->cur_pic_ptr->tf, INT_MAX,
VAR_0->picture_structure == PICT_BOTTOM_FIELD); } VAR_0->cur_pic_ptr = NULL; } } VAR_5 = get_ue_golomb_31(&VAR_0->gb); if (VAR_5 > 9) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"slice type %d too large at %d %d\n",
VAR_5, VAR_0->mb_x, VAR_0->mb_y); return AVERROR_INVALIDDATA; } if (VAR_5 > 4) { VAR_5 -= 5; VAR_0->slice_type_fixed = 1; } else VAR_0->slice_type_fixed = 0; VAR_5 = golomb_to_pict_type[VAR_5]; VAR_0->VAR_5     = VAR_5; VAR_0->slice_type_nos = VAR_5 & 3; if (VAR_0->nal_unit_type  == NAL_IDR_SLICE &&
VAR_0->slice_type_nos != AV_PICTURE_TYPE_I) { av_log(VAR_0->avctx, AV_LOG_ERROR, "A non-intra slice in an IDR NAL unit.\n"); return AVERROR_INVALIDDATA; } if (
(VAR_0->avctx->skip_frame >= AVDISCARD_NONREF && !VAR_0->nal_ref_idc) ||
(VAR_0->avctx->skip_frame >= AVDISCARD_BIDIR  && VAR_0->slice_type_nos == AV_PICTURE_TYPE_B) ||
(VAR_0->avctx->skip_frame >= AVDISCARD_NONINTRA && VAR_0->slice_type_nos != AV_PICTURE_TYPE_I) ||
(VAR_0->avctx->skip_frame >= AVDISCARD_NONKEY && VAR_0->nal_unit_type != NAL_IDR_SLICE) ||
VAR_0->avctx->skip_frame >= AVDISCARD_ALL) { return SLICE_SKIPED; } VAR_0->pict_type = VAR_0->VAR_5; VAR_3 = get_ue_golomb(&VAR_0->gb); if (VAR_3 >= MAX_PPS_COUNT) { av_log(VAR_0->avctx, AV_LOG_ERROR, "VAR_3 %u out of range\n", VAR_3); return AVERROR_INVALIDDATA; } if (!VAR_1->pps_buffers[VAR_3]) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"non-existing PPS %u referenced\n",
VAR_3); return AVERROR_INVALIDDATA; } if (VAR_1->au_pps_id >= 0 && VAR_3 != VAR_1->au_pps_id) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"PPS change from %d to %d forbidden\n",
VAR_1->au_pps_id, VAR_3); return AVERROR_INVALIDDATA; } VAR_0->pps = *VAR_1->pps_buffers[VAR_3]; if (!VAR_1->sps_buffers[VAR_0->pps.sps_id]) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"non-existing SPS %u referenced\n",
VAR_0->pps.sps_id); return AVERROR_INVALIDDATA; } if (VAR_0->pps.sps_id != VAR_0->sps.sps_id ||
VAR_0->pps.sps_id != VAR_0->current_sps_id ||
VAR_1->sps_buffers[VAR_0->pps.sps_id]->new) { VAR_0->sps = *VAR_1->sps_buffers[VAR_0->pps.sps_id]; if (VAR_0->mb_width  != VAR_0->sps.mb_width ||
VAR_0->mb_height != VAR_0->sps.mb_height * (2 - VAR_0->sps.frame_mbs_only_flag) ||
VAR_0->avctx->bits_per_raw_sample != VAR_0->sps.bit_depth_luma ||
VAR_0->cur_chroma_format_idc != VAR_0->sps.chroma_format_idc
)
VAR_12 = 1; if (VAR_0->bit_depth_luma    != VAR_0->sps.bit_depth_luma ||
VAR_0->chroma_format_idc != VAR_0->sps.chroma_format_idc) { VAR_0->bit_depth_luma    = VAR_0->sps.bit_depth_luma; VAR_0->chroma_format_idc = VAR_0->sps.chroma_format_idc; VAR_12         = 1; } if ((VAR_4 = ff_h264_set_parameter_from_sps(VAR_0)) < 0)
return VAR_4; } VAR_0->avctx->profile = ff_h264_get_profile(&VAR_0->sps); VAR_0->avctx->level   = VAR_0->sps.level_idc; VAR_0->avctx->refs    = VAR_0->sps.ref_frame_count; VAR_11 = (VAR_0->context_initialized &&
(   16*VAR_0->sps.mb_width != VAR_0->avctx->coded_width
|| 16*VAR_0->sps.mb_height * (2 - VAR_0->sps.frame_mbs_only_flag) != VAR_0->avctx->coded_height
|| VAR_0->avctx->bits_per_raw_sample != VAR_0->sps.bit_depth_luma
|| VAR_0->cur_chroma_format_idc != VAR_0->sps.chroma_format_idc
|| av_cmp_q(VAR_0->sps.sar, VAR_0->avctx->sample_aspect_ratio)
|| VAR_0->mb_width  != VAR_0->sps.mb_width
|| VAR_0->mb_height != VAR_0->sps.mb_height * (2 - VAR_0->sps.frame_mbs_only_flag)
)); if (non_j_pixfmt(VAR_1->avctx->pix_fmt) != non_j_pixfmt(get_pixel_format(VAR_1, 0)))
VAR_11 = 1; VAR_0->mb_width  = VAR_0->sps.mb_width; VAR_0->mb_height = VAR_0->sps.mb_height * (2 - VAR_0->sps.frame_mbs_only_flag); VAR_0->mb_num    = VAR_0->mb_width * VAR_0->mb_height; VAR_0->mb_stride = VAR_0->mb_width + 1; VAR_0->b_stride = VAR_0->mb_width * 4; VAR_0->chroma_y_shift = VAR_0->sps.chroma_format_idc <= 1; VAR_0->width  = 16 * VAR_0->mb_width; VAR_0->height = 16 * VAR_0->mb_height; VAR_4 = init_dimensions(VAR_0); if (VAR_4 < 0)
return VAR_4; if (VAR_0->sps.video_signal_type_present_flag) { VAR_0->avctx->color_range = VAR_0->sps.full_range>0 ? AVCOL_RANGE_JPEG
: AVCOL_RANGE_MPEG; if (VAR_0->sps.colour_description_present_flag) { if (VAR_0->avctx->colorspace != VAR_0->sps.colorspace)
VAR_12 = 1; VAR_0->avctx->color_primaries = VAR_0->sps.color_primaries; VAR_0->avctx->color_trc       = VAR_0->sps.color_trc; VAR_0->avctx->colorspace      = VAR_0->sps.colorspace; } } if (VAR_0->context_initialized &&
(VAR_11 || VAR_12)) { if (VAR_0 != VAR_1) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"changing width %d -> %d / height %d -> %d on "
"slice %d\n",
VAR_0->width, VAR_0->avctx->coded_width,
VAR_0->height, VAR_0->avctx->coded_height,
VAR_1->current_slice + 1); return AVERROR_INVALIDDATA; } ff_h264_flush_change(VAR_0); if ((VAR_4 = get_pixel_format(VAR_0, 1)) < 0)
return VAR_4; VAR_0->avctx->pix_fmt = VAR_4; av_log(VAR_0->avctx, AV_LOG_INFO, "Reinit context to %dx%d, "
"pix_fmt: %s\n", VAR_0->width, VAR_0->height, av_get_pix_fmt_name(VAR_0->avctx->pix_fmt)); if ((VAR_4 = h264_slice_header_init(VAR_0, 1)) < 0) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"h264_slice_header_init() failed\n"); return VAR_4; } } if (!VAR_0->context_initialized) { if (VAR_0 != VAR_1) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"Cannot (re-)initialize context during parallel decoding.\n"); return AVERROR_PATCHWELCOME; } if ((VAR_4 = get_pixel_format(VAR_0, 1)) < 0)
return VAR_4; VAR_0->avctx->pix_fmt = VAR_4; if ((VAR_4 = h264_slice_header_init(VAR_0, 0)) < 0) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"h264_slice_header_init() failed\n"); return VAR_4; } } if (VAR_0 == VAR_1 && VAR_0->dequant_coeff_pps != VAR_3) { VAR_0->dequant_coeff_pps = VAR_3; h264_init_dequant_tables(VAR_0); } VAR_0->frame_num = get_bits(&VAR_0->gb, VAR_0->sps.log2_max_frame_num); VAR_0->mb_mbaff        = 0; VAR_0->mb_aff_frame    = 0; VAR_9 = VAR_1->picture_structure; VAR_10 = VAR_1->droppable; VAR_0->droppable       = VAR_0->nal_ref_idc == 0; if (VAR_0->sps.frame_mbs_only_flag) { VAR_0->picture_structure = PICT_FRAME; } else { if (!VAR_0->sps.direct_8x8_inference_flag && VAR_5 == AV_PICTURE_TYPE_B) { av_log(VAR_0->avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n"); return -1; } VAR_13 = get_bits1(&VAR_0->gb); if (VAR_13) { VAR_14 = get_bits1(&VAR_0->gb); VAR_0->picture_structure = PICT_TOP_FIELD + VAR_14; } else { VAR_0->picture_structure = PICT_FRAME; VAR_0->mb_aff_frame      = VAR_0->sps.mb_aff; } } VAR_0->mb_field_decoding_flag = VAR_0->picture_structure != PICT_FRAME; if (VAR_1->current_slice != 0) { if (VAR_9 != VAR_0->picture_structure ||
VAR_10 != VAR_0->droppable) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"Changing field mode (%d -> %d) between slices is not allowed\n",
VAR_9, VAR_0->picture_structure); VAR_0->picture_structure = VAR_9; VAR_0->droppable         = VAR_10; return AVERROR_INVALIDDATA; } else if (!VAR_1->cur_pic_ptr) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"unset cur_pic_ptr on slice %d\n",
VAR_1->current_slice + 1); return AVERROR_INVALIDDATA; } } else { if (VAR_0->frame_num != VAR_0->prev_frame_num) { int VAR_15 = VAR_0->prev_frame_num; int VAR_16         = 1 << VAR_0->sps.log2_max_frame_num; if (VAR_15 > VAR_0->frame_num)
VAR_15 -= VAR_16; if ((VAR_0->frame_num - VAR_15) > VAR_0->sps.ref_frame_count) { VAR_15 = (VAR_0->frame_num - VAR_0->sps.ref_frame_count) - 1; if (VAR_15 < 0)
VAR_15 += VAR_16; VAR_0->prev_frame_num = VAR_15; } } if (VAR_1->first_field) { assert(VAR_1->cur_pic_ptr); assert(VAR_1->cur_pic_ptr->f.buf[0]); assert(VAR_1->cur_pic_ptr->reference != DELAYED_PIC_REF); if (VAR_1->cur_pic_ptr->tf.owner == VAR_1->avctx) { ff_thread_report_progress(&VAR_1->cur_pic_ptr->tf, INT_MAX,
VAR_9 == PICT_BOTTOM_FIELD); } if (!FIELD_PICTURE(VAR_0) || VAR_0->picture_structure == VAR_9) { if (VAR_9 != PICT_FRAME) { ff_thread_report_progress(&VAR_1->cur_pic_ptr->tf, INT_MAX,
VAR_9 == PICT_TOP_FIELD); } } else { if (VAR_1->cur_pic_ptr->frame_num != VAR_0->frame_num) { if (VAR_9 != PICT_FRAME) { ff_thread_report_progress(&VAR_1->cur_pic_ptr->tf, INT_MAX,
VAR_9 == PICT_TOP_FIELD); } } else { if (!((VAR_9   == PICT_TOP_FIELD &&
VAR_0->picture_structure == PICT_BOTTOM_FIELD) ||
(VAR_9   == PICT_BOTTOM_FIELD &&
VAR_0->picture_structure == PICT_TOP_FIELD))) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"Invalid field mode combination %d/%d\n",
VAR_9, VAR_0->picture_structure); VAR_0->picture_structure = VAR_9; VAR_0->droppable         = VAR_10; return AVERROR_INVALIDDATA; } else if (VAR_10 != VAR_0->droppable) { avpriv_request_sample(VAR_0->avctx,
"Found reference and non-reference fields in the same frame, which"); VAR_0->picture_structure = VAR_9; VAR_0->droppable         = VAR_10; return AVERROR_PATCHWELCOME; } } } } while (VAR_0->frame_num != VAR_0->prev_frame_num && !VAR_1->first_field &&
VAR_0->frame_num != (VAR_0->prev_frame_num + 1) % (1 << VAR_0->sps.log2_max_frame_num)) { H264Picture *prev = VAR_0->short_ref_count ? VAR_0->short_ref[0] : NULL; av_log(VAR_0->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
VAR_0->frame_num, VAR_0->prev_frame_num); if (!VAR_0->sps.gaps_in_frame_num_allowed_flag)
for(VAR_7=0; VAR_7<FF_ARRAY_ELEMS(VAR_0->last_pocs); VAR_7++) VAR_0->last_pocs[VAR_7] = INT_MIN; VAR_4 = h264_frame_start(VAR_0); if (VAR_4 < 0) { VAR_1->first_field = 0; return VAR_4; } VAR_0->prev_frame_num++; VAR_0->prev_frame_num        %= 1 << VAR_0->sps.log2_max_frame_num; VAR_0->cur_pic_ptr->frame_num = VAR_0->prev_frame_num; VAR_0->cur_pic_ptr->invalid_gap = !VAR_0->sps.gaps_in_frame_num_allowed_flag; ff_thread_report_progress(&VAR_0->cur_pic_ptr->tf, INT_MAX, 0); ff_thread_report_progress(&VAR_0->cur_pic_ptr->tf, INT_MAX, 1); VAR_4 = ff_generate_sliding_window_mmcos(VAR_0, 1); if (VAR_4 < 0 && (VAR_0->avctx->err_recognition & AV_EF_EXPLODE))
return VAR_4; VAR_4 = ff_h264_execute_ref_pic_marking(VAR_0, VAR_0->mmco, VAR_0->mmco_index); if (VAR_4 < 0 && (VAR_0->avctx->err_recognition & AV_EF_EXPLODE))
return VAR_4; if (VAR_0->short_ref_count) { if (prev) { av_image_copy(VAR_0->short_ref[0]->f.data,
VAR_0->short_ref[0]->f.linesize,
(const uint8_t **)prev->f.data,
prev->f.linesize,
VAR_0->avctx->pix_fmt,
VAR_0->mb_width  * 16,
VAR_0->mb_height * 16); VAR_0->short_ref[0]->poc = prev->poc + 2; } VAR_0->short_ref[0]->frame_num = VAR_0->prev_frame_num; } } if (VAR_1->first_field) { assert(VAR_1->cur_pic_ptr); assert(VAR_1->cur_pic_ptr->f.buf[0]); assert(VAR_1->cur_pic_ptr->reference != DELAYED_PIC_REF); if (!FIELD_PICTURE(VAR_0) || VAR_0->picture_structure == VAR_9) { VAR_1->cur_pic_ptr = NULL; VAR_1->first_field = FIELD_PICTURE(VAR_0); } else { if (VAR_1->cur_pic_ptr->frame_num != VAR_0->frame_num) { ff_thread_report_progress(&VAR_1->cur_pic_ptr->tf, INT_MAX,
VAR_1->picture_structure==PICT_BOTTOM_FIELD); VAR_1->first_field = 1; VAR_1->cur_pic_ptr = NULL; } else { VAR_1->first_field = 0; } } } else { VAR_1->first_field = FIELD_PICTURE(VAR_0); } if (!FIELD_PICTURE(VAR_0) || VAR_1->first_field) { if (h264_frame_start(VAR_0) < 0) { VAR_1->first_field = 0; return AVERROR_INVALIDDATA; } } else { release_unused_pictures(VAR_0, 0); } if (FIELD_PICTURE(VAR_0)) { for(VAR_7 = (VAR_0->picture_structure == PICT_BOTTOM_FIELD); VAR_7<VAR_0->mb_height; VAR_7++) memset(VAR_0->slice_table + VAR_7*VAR_0->mb_stride, -1, (VAR_0->mb_stride - (VAR_7+1==VAR_0->mb_height)) * sizeof(*VAR_0->slice_table)); } else { memset(VAR_0->slice_table, -1,
(VAR_0->mb_height * VAR_0->mb_stride - 1) * sizeof(*VAR_0->slice_table)); } VAR_1->last_slice_type = -1; } if (VAR_0 != VAR_1 && (VAR_4 = clone_slice(VAR_0, VAR_1)) < 0)
return VAR_4; for (VAR_7 = 0; VAR_7 < VAR_0->slice_context_count; VAR_7++) if (VAR_0->thread_context[VAR_7]) { VAR_4 = alloc_scratch_buffers(VAR_0->thread_context[VAR_7], VAR_0->linesize); if (VAR_4 < 0)
return VAR_4; } VAR_0->cur_pic_ptr->frame_num = VAR_0->frame_num; av_assert1(VAR_0->mb_num == VAR_0->mb_width * VAR_0->mb_height); if (VAR_2 << FIELD_OR_MBAFF_PICTURE(VAR_0) >= VAR_0->mb_num ||
VAR_2 >= VAR_0->mb_num) { av_log(VAR_0->avctx, AV_LOG_ERROR, "VAR_2 overflow\n"); return AVERROR_INVALIDDATA; } VAR_0->resync_mb_x = VAR_0->mb_x =  VAR_2 % VAR_0->mb_width; VAR_0->resync_mb_y = VAR_0->mb_y = (VAR_2 / VAR_0->mb_width) <<
FIELD_OR_MBAFF_PICTURE(VAR_0); if (VAR_0->picture_structure == PICT_BOTTOM_FIELD)
VAR_0->resync_mb_y = VAR_0->mb_y = VAR_0->mb_y + 1; av_assert1(VAR_0->mb_y < VAR_0->mb_height); if (VAR_0->picture_structure == PICT_FRAME) { VAR_0->curr_pic_num = VAR_0->frame_num; VAR_0->max_pic_num  = 1 << VAR_0->sps.log2_max_frame_num; } else { VAR_0->curr_pic_num = 2 * VAR_0->frame_num + 1; VAR_0->max_pic_num  = 1 << (VAR_0->sps.log2_max_frame_num + 1); } if (VAR_0->nal_unit_type == NAL_IDR_SLICE)
get_ue_golomb(&VAR_0->gb); if (VAR_0->sps.poc_type == 0) { VAR_0->poc_lsb = get_bits(&VAR_0->gb, VAR_0->sps.log2_max_poc_lsb); if (VAR_0->pps.pic_order_present == 1 && VAR_0->picture_structure == PICT_FRAME)
VAR_0->delta_poc_bottom = get_se_golomb(&VAR_0->gb); } if (VAR_0->sps.poc_type == 1 && !VAR_0->sps.delta_pic_order_always_zero_flag) { VAR_0->delta_poc[0] = get_se_golomb(&VAR_0->gb); if (VAR_0->pps.pic_order_present == 1 && VAR_0->picture_structure == PICT_FRAME)
VAR_0->delta_poc[1] = get_se_golomb(&VAR_0->gb); } ff_init_poc(VAR_0, VAR_0->cur_pic_ptr->field_poc, &VAR_0->cur_pic_ptr->poc); if (VAR_0->pps.redundant_pic_cnt_present)
VAR_0->redundant_pic_count = get_ue_golomb(&VAR_0->gb); VAR_4 = ff_set_ref_count(VAR_0); if (VAR_4 < 0)
return VAR_4; if (VAR_5 != AV_PICTURE_TYPE_I &&
(VAR_1->current_slice == 0 ||
VAR_5 != VAR_1->last_slice_type ||
memcmp(VAR_1->last_ref_count, VAR_1->ref_count, sizeof(VAR_1->ref_count)))) { ff_h264_fill_default_ref_list(VAR_0); } if (VAR_0->slice_type_nos != AV_PICTURE_TYPE_I) { VAR_4 = ff_h264_decode_ref_pic_list_reordering(VAR_0); if (VAR_4 < 0) { VAR_0->ref_count[1] = VAR_0->ref_count[0] = 0; return VAR_4; } } if ((VAR_0->pps.weighted_pred && VAR_0->slice_type_nos == AV_PICTURE_TYPE_P) ||
(VAR_0->pps.weighted_bipred_idc == 1 &&
VAR_0->slice_type_nos == AV_PICTURE_TYPE_B))
ff_pred_weight_table(VAR_0); else if (VAR_0->pps.weighted_bipred_idc == 2 &&
VAR_0->slice_type_nos == AV_PICTURE_TYPE_B) { implicit_weight_table(VAR_0, -1); } else { VAR_0->use_weight = 0; for (VAR_7 = 0; VAR_7 < 2; VAR_7++) { VAR_0->luma_weight_flag[VAR_7]   = 0; VAR_0->chroma_weight_flag[VAR_7] = 0; } } if (VAR_0->nal_ref_idc) { VAR_4 = ff_h264_decode_ref_pic_marking(VAR_1, &VAR_0->gb,
!(VAR_0->avctx->active_thread_type & FF_THREAD_FRAME) ||
VAR_1->current_slice == 0); if (VAR_4 < 0 && (VAR_0->avctx->err_recognition & AV_EF_EXPLODE))
return AVERROR_INVALIDDATA; } if (FRAME_MBAFF(VAR_0)) { ff_h264_fill_mbaff_ref_list(VAR_0); if (VAR_0->pps.weighted_bipred_idc == 2 && VAR_0->slice_type_nos == AV_PICTURE_TYPE_B) { implicit_weight_table(VAR_0, 0); implicit_weight_table(VAR_0, 1); } } if (VAR_0->slice_type_nos == AV_PICTURE_TYPE_B && !VAR_0->direct_spatial_mv_pred)
ff_h264_direct_dist_scale_factor(VAR_0); ff_h264_direct_ref_list_init(VAR_0); if (VAR_0->slice_type_nos != AV_PICTURE_TYPE_I && VAR_0->pps.cabac) { VAR_6 = get_ue_golomb_31(&VAR_0->gb); if (VAR_6 > 2) { av_log(VAR_0->avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\n", VAR_6); return AVERROR_INVALIDDATA; } VAR_0->cabac_init_idc = VAR_6; } VAR_0->last_qscale_diff = 0; VAR_6 = VAR_0->pps.init_qp + get_se_golomb(&VAR_0->gb); if (VAR_6 > 51 + 6 * (VAR_0->sps.bit_depth_luma - 8)) { av_log(VAR_0->avctx, AV_LOG_ERROR, "QP %u out of range\n", VAR_6); return AVERROR_INVALIDDATA; } VAR_0->qscale       = VAR_6; VAR_0->chroma_qp[0] = get_chroma_qp(VAR_0, 0, VAR_0->qscale); VAR_0->chroma_qp[1] = get_chroma_qp(VAR_0, 1, VAR_0->qscale); if (VAR_0->VAR_5 == AV_PICTURE_TYPE_SP)
get_bits1(&VAR_0->gb); if (VAR_0->VAR_5 == AV_PICTURE_TYPE_SP ||
VAR_0->VAR_5 == AV_PICTURE_TYPE_SI)
get_se_golomb(&VAR_0->gb); VAR_0->deblocking_filter     = 1; VAR_0->slice_alpha_c0_offset = 0; VAR_0->slice_beta_offset     = 0; if (VAR_0->pps.deblocking_filter_parameters_present) { VAR_6 = get_ue_golomb_31(&VAR_0->gb); if (VAR_6 > 2) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"deblocking_filter_idc %u out of range\n", VAR_6); return AVERROR_INVALIDDATA; } VAR_0->deblocking_filter = VAR_6; if (VAR_0->deblocking_filter < 2)
VAR_0->deblocking_filter ^= 1; if (VAR_0->deblocking_filter) { VAR_0->slice_alpha_c0_offset = get_se_golomb(&VAR_0->gb) * 2; VAR_0->slice_beta_offset     = get_se_golomb(&VAR_0->gb) * 2; if (VAR_0->slice_alpha_c0_offset >  12 ||
VAR_0->slice_alpha_c0_offset < -12 ||
VAR_0->slice_beta_offset >  12     ||
VAR_0->slice_beta_offset < -12) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"deblocking filter parameters %d %d out of range\n",
VAR_0->slice_alpha_c0_offset, VAR_0->slice_beta_offset); return AVERROR_INVALIDDATA; } } } if (VAR_0->avctx->skip_loop_filter >= AVDISCARD_ALL ||
(VAR_0->avctx->skip_loop_filter >= AVDISCARD_NONKEY &&
VAR_0->nal_unit_type != NAL_IDR_SLICE) ||
(VAR_0->avctx->skip_loop_filter >= AVDISCARD_NONINTRA &&
VAR_0->slice_type_nos != AV_PICTURE_TYPE_I) ||
(VAR_0->avctx->skip_loop_filter >= AVDISCARD_BIDIR  &&
VAR_0->slice_type_nos == AV_PICTURE_TYPE_B) ||
(VAR_0->avctx->skip_loop_filter >= AVDISCARD_NONREF &&
VAR_0->nal_ref_idc == 0))
VAR_0->deblocking_filter = 0; if (VAR_0->deblocking_filter == 1 && VAR_1->max_contexts > 1) { if (VAR_0->avctx->flags2 & CODEC_FLAG2_FAST) { VAR_0->deblocking_filter = 2; } else { VAR_1->max_contexts = 1; if (!VAR_1->single_decode_warning) { av_log(VAR_0->avctx, AV_LOG_INFO,
"Cannot parallelize deblocking type 1, decoding such frames in sequential order\n"); VAR_1->single_decode_warning = 1; } if (VAR_0 != VAR_1) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"Deblocking switched inside frame.\n"); return SLICE_SINGLETHREAD; } } } VAR_0->qp_thresh = 15 -
FFMIN(VAR_0->slice_alpha_c0_offset, VAR_0->slice_beta_offset) -
FFMAX3(0,
VAR_0->pps.chroma_qp_index_offset[0],
VAR_0->pps.chroma_qp_index_offset[1]) +
6 * (VAR_0->sps.bit_depth_luma - 8); VAR_1->last_slice_type = VAR_5; memcpy(VAR_1->last_ref_count, VAR_1->ref_count, sizeof(VAR_1->last_ref_count)); VAR_0->slice_num        = ++VAR_1->current_slice; if (VAR_0->slice_num)
VAR_1->slice_row[(VAR_0->slice_num-1)&(MAX_SLICES-1)]= VAR_0->resync_mb_y; if (   VAR_1->slice_row[VAR_0->slice_num&(MAX_SLICES-1)] + 3 >= VAR_0->resync_mb_y
&& VAR_1->slice_row[VAR_0->slice_num&(MAX_SLICES-1)] <= VAR_0->resync_mb_y
&& VAR_0->slice_num >= MAX_SLICES) { av_log(VAR_0->avctx, AV_LOG_WARNING, "Possibly too many slices (%d >= %d), increase MAX_SLICES and recompile if there are artifacts\n", VAR_0->slice_num, MAX_SLICES); } for (VAR_8 = 0; VAR_8 < 2; VAR_8++) { int VAR_17[16]; int *VAR_18 = VAR_0->VAR_18[VAR_0->slice_num & (MAX_SLICES - 1)][VAR_8]; for (VAR_7 = 0; VAR_7 < 16; VAR_7++) { VAR_17[VAR_7] = 60; if (VAR_8 < VAR_0->list_count && VAR_7 < VAR_0->ref_count[VAR_8] &&
VAR_0->ref_list[VAR_8][VAR_7].f.buf[0]) { int VAR_19; AVBuffer *buf = VAR_0->ref_list[VAR_8][VAR_7].f.buf[0]->buffer; for (VAR_19 = 0; VAR_19 < VAR_0->short_ref_count; VAR_19++) if (VAR_0->short_ref[VAR_19]->f.buf[0]->buffer == buf) { VAR_17[VAR_7] = VAR_19; break; } for (VAR_19 = 0; VAR_19 < VAR_0->long_ref_count; VAR_19++) if (VAR_0->long_ref[VAR_19] && VAR_0->long_ref[VAR_19]->f.buf[0]->buffer == buf) { VAR_17[VAR_7] = VAR_0->short_ref_count + VAR_19; break; } } } VAR_18[0] =
VAR_18[1] = -1; for (VAR_7 = 0; VAR_7 < 16; VAR_7++) VAR_18[VAR_7 + 2] = 4 * VAR_17[VAR_7] + (VAR_0->ref_list[VAR_8][VAR_7].reference & 3); VAR_18[18 + 0] =
VAR_18[18 + 1] = -1; for (VAR_7 = 16; VAR_7 < 48; VAR_7++) VAR_18[VAR_7 + 4] = 4 * VAR_17[(VAR_7 - 16) >> 1] +
(VAR_0->ref_list[VAR_8][VAR_7].reference & 3); } if (VAR_0->ref_count[0]) ff_h264_set_erpic(&VAR_0->er.last_pic, &VAR_0->ref_list[0][0]); if (VAR_0->ref_count[1]) ff_h264_set_erpic(&VAR_0->er.next_pic, &VAR_0->ref_list[1][0]); VAR_0->er.ref_count = VAR_0->ref_count[0]; VAR_1->au_pps_id = VAR_3; VAR_0->sps.new =
VAR_1->sps_buffers[VAR_0->pps.sps_id]->new = 0; VAR_0->current_sps_id = VAR_0->pps.sps_id; if (VAR_0->avctx->debug & FF_DEBUG_PICT_INFO) { av_log(VAR_0->avctx, AV_LOG_DEBUG,
"slice:%d %s mb:%d %c%s%s pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s %s\n",
VAR_0->slice_num,
(VAR_0->picture_structure == PICT_FRAME ? "F" : VAR_0->picture_structure == PICT_TOP_FIELD ? "T" : "B"),
VAR_2,
av_get_picture_type_char(VAR_0->VAR_5),
VAR_0->slice_type_fixed ? " fix" : "",
VAR_0->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
VAR_3, VAR_0->frame_num,
VAR_0->cur_pic_ptr->field_poc[0],
VAR_0->cur_pic_ptr->field_poc[1],
VAR_0->ref_count[0], VAR_0->ref_count[1],
VAR_0->qscale,
VAR_0->deblocking_filter,
VAR_0->slice_alpha_c0_offset, VAR_0->slice_beta_offset,
VAR_0->use_weight,
VAR_0->use_weight == 1 && VAR_0->use_weight_chroma ? "c" : "",
VAR_0->VAR_5 == AV_PICTURE_TYPE_B ? (VAR_0->direct_spatial_mv_pred ? "SPAT" : "TEMP") : ""); } return 0; }