int HevcSpsUnit::short_term_ref_pic_set(int stRpsIdx)
{
    uint8_t rps_predict = 0;
    unsigned delta_poc;
    int k0 = 0;
    int k1 = 0;
    int k = 0;

    if (stRpsIdx != 0)
        rps_predict = m_reader.getBit();

    ShortTermRPS* rps = &st_rps[stRpsIdx];

    if (rps_predict)
    {
        const ShortTermRPS* rps_ridx;
        int delta_rps;
        unsigned abs_delta_rps;
        uint8_t use_delta_flag = 0;
        uint8_t delta_rps_sign;

        // rps_ridx = &st_rps[rps - st_rps - 1];
        rps_ridx = &st_rps[stRpsIdx - 1];

        delta_rps_sign = m_reader.getBit();
        abs_delta_rps = extractUEGolombCode() + 1;
        if (abs_delta_rps > 0x8000)
            return 1;
        delta_rps = (1 - (delta_rps_sign << 1)) * abs_delta_rps;
        for (int i = 0; i <= rps_ridx->num_delta_pocs; i++)
        {
            int used = rps->used[k] = m_reader.getBit();

            if (!used)
                use_delta_flag = m_reader.getBit();

            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;
        // sort in increasing order (smallest first)
        if (rps->num_delta_pocs != 0)
        {
            unsigned used, tmp;
            for (int 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;
            // flip the negative values to largest first
            for (size_t 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 = extractUEGolombCode();
        if (rps->num_negative_pics > 64)
            return 1;
        nb_positive_pics = extractUEGolombCode();
        if (nb_positive_pics > 64)
            return 1;
        rps->num_delta_pocs = rps->num_negative_pics + nb_positive_pics;
        if (rps->num_delta_pocs > 64)
            return 1;
        if (rps->num_delta_pocs)
        {
            prev = 0;
            for (size_t i = 0; i < rps->num_negative_pics; i++)
            {
                delta_poc = extractUEGolombCode() + 1;
                if (delta_poc > 0x8000)
                    return 1;
                prev -= delta_poc;
                rps->delta_poc[i] = prev;
                rps->used[i] = m_reader.getBit();
            }
            prev = 0;
            for (size_t i = 0; i < nb_positive_pics; i++)
            {
                delta_poc = extractUEGolombCode() + 1;
                if (delta_poc > 0x8000)
                    return 1;
                prev += delta_poc;
                rps->delta_poc[rps->num_negative_pics + i] = prev;
                rps->used[rps->num_negative_pics + i] = m_reader.getBit();
            }
        }
    }
    return 0;
}