int ff_hevc_decode_short_term_rps(GetBitContext *gb, AVCodecContext *avctx,
                                  ShortTermRPS *rps, const HEVCSPS *sps, int is_slice_header)
{
    uint8_t rps_predict = 0;
    int delta_poc;
    int k0 = 0;
    int k1 = 0;
    int k  = 0;
    int i;
    if (rps != sps->st_rps && sps->nb_st_rps)
        rps_predict = get_bits1(gb);
    if (rps_predict) {
        const ShortTermRPS *rps_ridx;
        int delta_rps, abs_delta_rps;
        uint8_t use_delta_flag = 0;
        uint8_t delta_rps_sign;
        if (is_slice_header) {
            unsigned int delta_idx = get_ue_golomb_long(gb) + 1;
            if (delta_idx > sps->nb_st_rps) {
                av_log(avctx, AV_LOG_ERROR,
                       "Invalid value of delta_idx in slice header RPS: %d > %d.\n",
                       delta_idx, sps->nb_st_rps);
                return AVERROR_INVALIDDATA;
            }
            rps_ridx = &sps->st_rps[sps->nb_st_rps - delta_idx];
            rps->rps_idx_num_delta_pocs = rps_ridx->num_delta_pocs;
        } else
            rps_ridx = &sps->st_rps[rps - sps->st_rps - 1];
        delta_rps_sign = get_bits1(gb);
        abs_delta_rps  = get_ue_golomb_long(gb) + 1;
        delta_rps      = (1 - (delta_rps_sign << 1)) * abs_delta_rps;
        for (i = 0; i <= rps_ridx->num_delta_pocs; i++) {
            int used = rps->used[k] = get_bits1(gb);
            if (!used)
                use_delta_flag = get_bits1(gb);
            if (used || use_delta_flag) {
                if (i < rps_ridx->num_delta_pocs)
                    delta_poc = delta_rps + rps_ridx->delta_poc[i];
                else
                    delta_poc = delta_rps;
                rps->delta_poc[k] = delta_poc;
                if (delta_poc < 0)
                    k0++;
                else
                    k1++;
                k++;
            }
        }
        rps->num_delta_pocs    = k;
        rps->num_negative_pics = k0;
        if (rps->num_delta_pocs != 0) {
            int used, tmp;
            for (i = 1; i < rps->num_delta_pocs; i++) {
                delta_poc = rps->delta_poc[i];
                used      = rps->used[i];
                for (k = i - 1; k >= 0; k--) {
                    tmp = rps->delta_poc[k];
                    if (delta_poc < tmp) {
                        rps->delta_poc[k + 1] = tmp;
                        rps->used[k + 1]      = rps->used[k];
                        rps->delta_poc[k]     = delta_poc;
                        rps->used[k]          = used;
                    }
                }
            }
        }
        if ((rps->num_negative_pics >> 1) != 0) {
            int used;
            k = rps->num_negative_pics - 1;
            for (i = 0; i < rps->num_negative_pics >> 1; i++) {
                delta_poc         = rps->delta_poc[i];
                used              = rps->used[i];
                rps->delta_poc[i] = rps->delta_poc[k];
                rps->used[i]      = rps->used[k];
                rps->delta_poc[k] = delta_poc;
                rps->used[k]      = used;
                k--;
            }
        }
    } else {
        unsigned int prev, nb_positive_pics;
        rps->num_negative_pics = get_ue_golomb_long(gb);
        nb_positive_pics       = get_ue_golomb_long(gb);
        if (rps->num_negative_pics >= MAX_REFS ||
            nb_positive_pics >= MAX_REFS) {
            av_log(avctx, AV_LOG_ERROR, "Too many refs in a short term RPS.\n");
            return AVERROR_INVALIDDATA;
        }
        rps->num_delta_pocs = rps->num_negative_pics + nb_positive_pics;
        if (rps->num_delta_pocs) {
            prev = 0;
            for (i = 0; i < rps->num_negative_pics; i++) {
                delta_poc = get_ue_golomb_long(gb) + 1;
                prev -= delta_poc;
                rps->delta_poc[i] = prev;
                rps->used[i]      = get_bits1(gb);
            }
            prev = 0;
            for (i = 0; i < nb_positive_pics; i++) {
                delta_poc = get_ue_golomb_long(gb) + 1;
                prev += delta_poc;
                rps->delta_poc[rps->num_negative_pics + i] = prev;
                rps->used[rps->num_negative_pics + i]      = get_bits1(gb);
            }
        }
    }
    return 0;
}
['int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, unsigned int *sps_id,\n                      int apply_defdispwin, AVBufferRef **vps_list, AVCodecContext *avctx)\n{\n    int ret = 0;\n    int log2_diff_max_min_transform_block_size;\n    int bit_depth_chroma, start, vui_present, sublayer_ordering_info;\n    int i;\n    sps->vps_id = get_bits(gb, 4);\n    if (sps->vps_id >= MAX_VPS_COUNT) {\n        av_log(avctx, AV_LOG_ERROR, "VPS id out of range: %d\\n", sps->vps_id);\n        ret = AVERROR_INVALIDDATA;\n        goto err;\n    }\n    if (vps_list && !vps_list[sps->vps_id]) {\n        av_log(avctx, AV_LOG_ERROR, "VPS %d does not exist\\n",\n               sps->vps_id);\n        ret = AVERROR_INVALIDDATA;\n        goto err;\n    }\n    sps->max_sub_layers = get_bits(gb, 3) + 1;\n    if (sps->max_sub_layers > MAX_SUB_LAYERS) {\n        av_log(avctx, AV_LOG_ERROR, "sps_max_sub_layers out of range: %d\\n",\n               sps->max_sub_layers);\n        ret = AVERROR_INVALIDDATA;\n        goto err;\n    }\n    skip_bits1(gb);\n    parse_ptl(gb, avctx, &sps->ptl, sps->max_sub_layers);\n    *sps_id = get_ue_golomb_long(gb);\n    if (*sps_id >= MAX_SPS_COUNT) {\n        av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\\n", *sps_id);\n        ret = AVERROR_INVALIDDATA;\n        goto err;\n    }\n    sps->chroma_format_idc = get_ue_golomb_long(gb);\n    if (sps->chroma_format_idc != 1) {\n        avpriv_report_missing_feature(avctx, "chroma_format_idc %d",\n                                      sps->chroma_format_idc);\n        ret = AVERROR_PATCHWELCOME;\n        goto err;\n    }\n    if (sps->chroma_format_idc == 3)\n        sps->separate_colour_plane_flag = get_bits1(gb);\n    sps->width  = get_ue_golomb_long(gb);\n    sps->height = get_ue_golomb_long(gb);\n    if ((ret = av_image_check_size(sps->width,\n                                   sps->height, 0, avctx)) < 0)\n        goto err;\n    if (get_bits1(gb)) {\n        sps->pic_conf_win.left_offset   = get_ue_golomb_long(gb) * 2;\n        sps->pic_conf_win.right_offset  = get_ue_golomb_long(gb) * 2;\n        sps->pic_conf_win.top_offset    = get_ue_golomb_long(gb) * 2;\n        sps->pic_conf_win.bottom_offset = get_ue_golomb_long(gb) * 2;\n        if (avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP) {\n            av_log(avctx, AV_LOG_DEBUG,\n                   "discarding sps conformance window, "\n                   "original values are l:%u r:%u t:%u b:%u\\n",\n                   sps->pic_conf_win.left_offset,\n                   sps->pic_conf_win.right_offset,\n                   sps->pic_conf_win.top_offset,\n                   sps->pic_conf_win.bottom_offset);\n            sps->pic_conf_win.left_offset   =\n            sps->pic_conf_win.right_offset  =\n            sps->pic_conf_win.top_offset    =\n            sps->pic_conf_win.bottom_offset = 0;\n        }\n        sps->output_window = sps->pic_conf_win;\n    }\n    sps->bit_depth   = get_ue_golomb_long(gb) + 8;\n    bit_depth_chroma = get_ue_golomb_long(gb) + 8;\n    if (bit_depth_chroma != sps->bit_depth) {\n        av_log(avctx, AV_LOG_ERROR,\n               "Luma bit depth (%d) is different from chroma bit depth (%d), "\n               "this is unsupported.\\n",\n               sps->bit_depth, bit_depth_chroma);\n        ret = AVERROR_INVALIDDATA;\n        goto err;\n    }\n    ret = map_pixel_format(avctx, sps);\n    if (ret < 0)\n        goto err;\n    sps->log2_max_poc_lsb = get_ue_golomb_long(gb) + 4;\n    if (sps->log2_max_poc_lsb > 16) {\n        av_log(avctx, AV_LOG_ERROR, "log2_max_pic_order_cnt_lsb_minus4 out range: %d\\n",\n               sps->log2_max_poc_lsb - 4);\n        ret = AVERROR_INVALIDDATA;\n        goto err;\n    }\n    sublayer_ordering_info = get_bits1(gb);\n    start = sublayer_ordering_info ? 0 : sps->max_sub_layers - 1;\n    for (i = start; i < sps->max_sub_layers; i++) {\n        sps->temporal_layer[i].max_dec_pic_buffering = get_ue_golomb_long(gb) + 1;\n        sps->temporal_layer[i].num_reorder_pics      = get_ue_golomb_long(gb);\n        sps->temporal_layer[i].max_latency_increase  = get_ue_golomb_long(gb) - 1;\n        if (sps->temporal_layer[i].max_dec_pic_buffering > MAX_DPB_SIZE) {\n            av_log(avctx, AV_LOG_ERROR, "sps_max_dec_pic_buffering_minus1 out of range: %d\\n",\n                   sps->temporal_layer[i].max_dec_pic_buffering - 1);\n            ret = AVERROR_INVALIDDATA;\n            goto err;\n        }\n        if (sps->temporal_layer[i].num_reorder_pics > sps->temporal_layer[i].max_dec_pic_buffering - 1) {\n            av_log(avctx, AV_LOG_WARNING, "sps_max_num_reorder_pics out of range: %d\\n",\n                   sps->temporal_layer[i].num_reorder_pics);\n            if (avctx->err_recognition & AV_EF_EXPLODE ||\n                sps->temporal_layer[i].num_reorder_pics > MAX_DPB_SIZE - 1) {\n                ret = AVERROR_INVALIDDATA;\n                goto err;\n            }\n            sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[i].num_reorder_pics + 1;\n        }\n    }\n    if (!sublayer_ordering_info) {\n        for (i = 0; i < start; i++) {\n            sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[start].max_dec_pic_buffering;\n            sps->temporal_layer[i].num_reorder_pics      = sps->temporal_layer[start].num_reorder_pics;\n            sps->temporal_layer[i].max_latency_increase  = sps->temporal_layer[start].max_latency_increase;\n        }\n    }\n    sps->log2_min_cb_size                    = get_ue_golomb_long(gb) + 3;\n    sps->log2_diff_max_min_coding_block_size = get_ue_golomb_long(gb);\n    sps->log2_min_tb_size                    = get_ue_golomb_long(gb) + 2;\n    log2_diff_max_min_transform_block_size   = get_ue_golomb_long(gb);\n    sps->log2_max_trafo_size                 = log2_diff_max_min_transform_block_size +\n                                               sps->log2_min_tb_size;\n    if (sps->log2_min_tb_size >= sps->log2_min_cb_size) {\n        av_log(avctx, AV_LOG_ERROR, "Invalid value for log2_min_tb_size");\n        ret = AVERROR_INVALIDDATA;\n        goto err;\n    }\n    sps->max_transform_hierarchy_depth_inter = get_ue_golomb_long(gb);\n    sps->max_transform_hierarchy_depth_intra = get_ue_golomb_long(gb);\n    sps->scaling_list_enable_flag = get_bits1(gb);\n    if (sps->scaling_list_enable_flag) {\n        set_default_scaling_list_data(&sps->scaling_list);\n        if (get_bits1(gb)) {\n            ret = scaling_list_data(gb, avctx, &sps->scaling_list);\n            if (ret < 0)\n                goto err;\n        }\n    }\n    sps->amp_enabled_flag = get_bits1(gb);\n    sps->sao_enabled      = get_bits1(gb);\n    sps->pcm_enabled_flag = get_bits1(gb);\n    if (sps->pcm_enabled_flag) {\n        sps->pcm.bit_depth   = get_bits(gb, 4) + 1;\n        sps->pcm.bit_depth_chroma = get_bits(gb, 4) + 1;\n        sps->pcm.log2_min_pcm_cb_size = get_ue_golomb_long(gb) + 3;\n        sps->pcm.log2_max_pcm_cb_size = sps->pcm.log2_min_pcm_cb_size +\n                                        get_ue_golomb_long(gb);\n        if (sps->pcm.bit_depth > sps->bit_depth) {\n            av_log(avctx, AV_LOG_ERROR,\n                   "PCM bit depth (%d) is greater than normal bit depth (%d)\\n",\n                   sps->pcm.bit_depth, sps->bit_depth);\n            ret = AVERROR_INVALIDDATA;\n            goto err;\n        }\n        sps->pcm.loop_filter_disable_flag = get_bits1(gb);\n    }\n    sps->nb_st_rps = get_ue_golomb_long(gb);\n    if (sps->nb_st_rps > MAX_SHORT_TERM_RPS_COUNT) {\n        av_log(avctx, AV_LOG_ERROR, "Too many short term RPS: %d.\\n",\n               sps->nb_st_rps);\n        ret = AVERROR_INVALIDDATA;\n        goto err;\n    }\n    for (i = 0; i < sps->nb_st_rps; i++) {\n        if ((ret = ff_hevc_decode_short_term_rps(gb, avctx, &sps->st_rps[i],\n                                                 sps, 0)) < 0)\n            goto err;\n    }\n    sps->long_term_ref_pics_present_flag = get_bits1(gb);\n    if (sps->long_term_ref_pics_present_flag) {\n        sps->num_long_term_ref_pics_sps = get_ue_golomb_long(gb);\n        for (i = 0; i < sps->num_long_term_ref_pics_sps; i++) {\n            sps->lt_ref_pic_poc_lsb_sps[i]       = get_bits(gb, sps->log2_max_poc_lsb);\n            sps->used_by_curr_pic_lt_sps_flag[i] = get_bits1(gb);\n        }\n    }\n    sps->sps_temporal_mvp_enabled_flag          = get_bits1(gb);\n    sps->sps_strong_intra_smoothing_enable_flag = get_bits1(gb);\n    sps->vui.sar = (AVRational){0, 1};\n    vui_present = get_bits1(gb);\n    if (vui_present)\n        decode_vui(gb, avctx, apply_defdispwin, sps);\n    skip_bits1(gb);\n    if (apply_defdispwin) {\n        sps->output_window.left_offset   += sps->vui.def_disp_win.left_offset;\n        sps->output_window.right_offset  += sps->vui.def_disp_win.right_offset;\n        sps->output_window.top_offset    += sps->vui.def_disp_win.top_offset;\n        sps->output_window.bottom_offset += sps->vui.def_disp_win.bottom_offset;\n    }\n    if (sps->output_window.left_offset & (0x1F >> (sps->pixel_shift)) &&\n        !(avctx->flags & AV_CODEC_FLAG_UNALIGNED)) {\n        sps->output_window.left_offset &= ~(0x1F >> (sps->pixel_shift));\n        av_log(avctx, AV_LOG_WARNING, "Reducing left output window to %d "\n               "chroma samples to preserve alignment.\\n",\n               sps->output_window.left_offset);\n    }\n    sps->output_width  = sps->width -\n                         (sps->output_window.left_offset + sps->output_window.right_offset);\n    sps->output_height = sps->height -\n                         (sps->output_window.top_offset + sps->output_window.bottom_offset);\n    if (sps->output_width <= 0 || sps->output_height <= 0) {\n        av_log(avctx, AV_LOG_WARNING, "Invalid visible frame dimensions: %dx%d.\\n",\n               sps->output_width, sps->output_height);\n        if (avctx->err_recognition & AV_EF_EXPLODE) {\n            ret = AVERROR_INVALIDDATA;\n            goto err;\n        }\n        av_log(avctx, AV_LOG_WARNING,\n               "Displaying the whole video surface.\\n");\n        sps->output_window.left_offset   =\n        sps->output_window.right_offset  =\n        sps->output_window.top_offset    =\n        sps->output_window.bottom_offset = 0;\n        sps->output_width               = sps->width;\n        sps->output_height              = sps->height;\n    }\n    sps->log2_ctb_size = sps->log2_min_cb_size +\n                         sps->log2_diff_max_min_coding_block_size;\n    sps->log2_min_pu_size = sps->log2_min_cb_size - 1;\n    sps->ctb_width  = (sps->width  + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;\n    sps->ctb_height = (sps->height + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;\n    sps->ctb_size   = sps->ctb_width * sps->ctb_height;\n    sps->min_cb_width  = sps->width  >> sps->log2_min_cb_size;\n    sps->min_cb_height = sps->height >> sps->log2_min_cb_size;\n    sps->min_tb_width  = sps->width  >> sps->log2_min_tb_size;\n    sps->min_tb_height = sps->height >> sps->log2_min_tb_size;\n    sps->min_pu_width  = sps->width  >> sps->log2_min_pu_size;\n    sps->min_pu_height = sps->height >> sps->log2_min_pu_size;\n    sps->qp_bd_offset = 6 * (sps->bit_depth - 8);\n    if (sps->width  & ((1 << sps->log2_min_cb_size) - 1) ||\n        sps->height & ((1 << sps->log2_min_cb_size) - 1)) {\n        av_log(avctx, AV_LOG_ERROR, "Invalid coded frame dimensions.\\n");\n        goto err;\n    }\n    if (sps->log2_ctb_size > MAX_LOG2_CTB_SIZE) {\n        av_log(avctx, AV_LOG_ERROR, "CTB size out of range: 2^%d\\n", sps->log2_ctb_size);\n        goto err;\n    }\n    if (sps->max_transform_hierarchy_depth_inter > sps->log2_ctb_size - sps->log2_min_tb_size) {\n        av_log(avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_inter out of range: %d\\n",\n               sps->max_transform_hierarchy_depth_inter);\n        goto err;\n    }\n    if (sps->max_transform_hierarchy_depth_intra > sps->log2_ctb_size - sps->log2_min_tb_size) {\n        av_log(avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_intra out of range: %d\\n",\n               sps->max_transform_hierarchy_depth_intra);\n        goto err;\n    }\n    if (sps->log2_max_trafo_size > FFMIN(sps->log2_ctb_size, 5)) {\n        av_log(avctx, AV_LOG_ERROR,\n               "max transform block size out of range: %d\\n",\n               sps->log2_max_trafo_size);\n        goto err;\n    }\n    return 0;\nerr:\n    return ret < 0 ? ret : AVERROR_INVALIDDATA;\n}', 'int ff_hevc_decode_short_term_rps(GetBitContext *gb, AVCodecContext *avctx,\n                                  ShortTermRPS *rps, const HEVCSPS *sps, int is_slice_header)\n{\n    uint8_t rps_predict = 0;\n    int delta_poc;\n    int k0 = 0;\n    int k1 = 0;\n    int k  = 0;\n    int i;\n    if (rps != sps->st_rps && sps->nb_st_rps)\n        rps_predict = get_bits1(gb);\n    if (rps_predict) {\n        const ShortTermRPS *rps_ridx;\n        int delta_rps, abs_delta_rps;\n        uint8_t use_delta_flag = 0;\n        uint8_t delta_rps_sign;\n        if (is_slice_header) {\n            unsigned int delta_idx = get_ue_golomb_long(gb) + 1;\n            if (delta_idx > sps->nb_st_rps) {\n                av_log(avctx, AV_LOG_ERROR,\n                       "Invalid value of delta_idx in slice header RPS: %d > %d.\\n",\n                       delta_idx, sps->nb_st_rps);\n                return AVERROR_INVALIDDATA;\n            }\n            rps_ridx = &sps->st_rps[sps->nb_st_rps - delta_idx];\n            rps->rps_idx_num_delta_pocs = rps_ridx->num_delta_pocs;\n        } else\n            rps_ridx = &sps->st_rps[rps - sps->st_rps - 1];\n        delta_rps_sign = get_bits1(gb);\n        abs_delta_rps  = get_ue_golomb_long(gb) + 1;\n        delta_rps      = (1 - (delta_rps_sign << 1)) * abs_delta_rps;\n        for (i = 0; i <= rps_ridx->num_delta_pocs; i++) {\n            int used = rps->used[k] = get_bits1(gb);\n            if (!used)\n                use_delta_flag = get_bits1(gb);\n            if (used || use_delta_flag) {\n                if (i < rps_ridx->num_delta_pocs)\n                    delta_poc = delta_rps + rps_ridx->delta_poc[i];\n                else\n                    delta_poc = delta_rps;\n                rps->delta_poc[k] = delta_poc;\n                if (delta_poc < 0)\n                    k0++;\n                else\n                    k1++;\n                k++;\n            }\n        }\n        rps->num_delta_pocs    = k;\n        rps->num_negative_pics = k0;\n        if (rps->num_delta_pocs != 0) {\n            int used, tmp;\n            for (i = 1; i < rps->num_delta_pocs; i++) {\n                delta_poc = rps->delta_poc[i];\n                used      = rps->used[i];\n                for (k = i - 1; k >= 0; k--) {\n                    tmp = rps->delta_poc[k];\n                    if (delta_poc < tmp) {\n                        rps->delta_poc[k + 1] = tmp;\n                        rps->used[k + 1]      = rps->used[k];\n                        rps->delta_poc[k]     = delta_poc;\n                        rps->used[k]          = used;\n                    }\n                }\n            }\n        }\n        if ((rps->num_negative_pics >> 1) != 0) {\n            int used;\n            k = rps->num_negative_pics - 1;\n            for (i = 0; i < rps->num_negative_pics >> 1; i++) {\n                delta_poc         = rps->delta_poc[i];\n                used              = rps->used[i];\n                rps->delta_poc[i] = rps->delta_poc[k];\n                rps->used[i]      = rps->used[k];\n                rps->delta_poc[k] = delta_poc;\n                rps->used[k]      = used;\n                k--;\n            }\n        }\n    } else {\n        unsigned int prev, nb_positive_pics;\n        rps->num_negative_pics = get_ue_golomb_long(gb);\n        nb_positive_pics       = get_ue_golomb_long(gb);\n        if (rps->num_negative_pics >= MAX_REFS ||\n            nb_positive_pics >= MAX_REFS) {\n            av_log(avctx, AV_LOG_ERROR, "Too many refs in a short term RPS.\\n");\n            return AVERROR_INVALIDDATA;\n        }\n        rps->num_delta_pocs = rps->num_negative_pics + nb_positive_pics;\n        if (rps->num_delta_pocs) {\n            prev = 0;\n            for (i = 0; i < rps->num_negative_pics; i++) {\n                delta_poc = get_ue_golomb_long(gb) + 1;\n                prev -= delta_poc;\n                rps->delta_poc[i] = prev;\n                rps->used[i]      = get_bits1(gb);\n            }\n            prev = 0;\n            for (i = 0; i < nb_positive_pics; i++) {\n                delta_poc = get_ue_golomb_long(gb) + 1;\n                prev += delta_poc;\n                rps->delta_poc[rps->num_negative_pics + i] = prev;\n                rps->used[rps->num_negative_pics + i]      = get_bits1(gb);\n            }\n        }\n    }\n    return 0;\n}']