int ff_hevc_decode_short_term_rps(HEVCContext *s, ShortTermRPS *rps,
                                  const HEVCSPS *sps, int is_slice_header)
{
    HEVCLocalContext *lc = &s->HEVClc;
    uint8_t rps_predict = 0;
    int delta_poc;
    int k0 = 0;
    int k1 = 0;
    int k  = 0;
    int i;
    GetBitContext *gb = &lc->gb;
    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(s->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];
        } 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(s->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_decode_short_term_rps(HEVCContext *s, ShortTermRPS *rps,\n                                  const HEVCSPS *sps, int is_slice_header)\n{\n    HEVCLocalContext *lc = &s->HEVClc;\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    GetBitContext *gb = &lc->gb;\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(s->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        } 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(s->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}']