static int lag_read_prob_header(lag_rac *rac, GetBitContext *gb)

{

    int i, j, scale_factor;

    unsigned prob, cumulative_target;

    unsigned cumul_prob = 0;

    unsigned scaled_cumul_prob = 0;



    rac->prob[0] = 0;

    rac->prob[257] = UINT_MAX;

    /* Read probabilities from bitstream */

    for (i = 1; i < 257; i++) {

        if (lag_decode_prob(gb, &rac->prob[i]) < 0) {

            av_log(rac->avctx, AV_LOG_ERROR, "Invalid probability encountered.\n");

            return -1;

        }

        if ((uint64_t)cumul_prob + rac->prob[i] > UINT_MAX) {

            av_log(rac->avctx, AV_LOG_ERROR, "Integer overflow encountered in cumulative probability calculation.\n");

            return -1;

        }

        cumul_prob += rac->prob[i];

        if (!rac->prob[i]) {

            if (lag_decode_prob(gb, &prob)) {

                av_log(rac->avctx, AV_LOG_ERROR, "Invalid probability run encountered.\n");

                return -1;

            }

            if (prob > 256 - i)

                prob = 256 - i;

            for (j = 0; j < prob; j++)

                rac->prob[++i] = 0;

        }

    }



    if (!cumul_prob) {

        av_log(rac->avctx, AV_LOG_ERROR, "All probabilities are 0!\n");

        return -1;

    }



    /* Scale probabilities so cumulative probability is an even power of 2. */

    scale_factor = av_log2(cumul_prob);



    if (cumul_prob & (cumul_prob - 1)) {

        uint64_t mul = softfloat_reciprocal(cumul_prob);

        for (i = 1; i <= 128; i++) {

            rac->prob[i] = softfloat_mul(rac->prob[i], mul);

            scaled_cumul_prob += rac->prob[i];

        }

        if (scaled_cumul_prob <= 0) {

            av_log(rac->avctx, AV_LOG_ERROR, "Scaled probabilities invalid\n");

            return AVERROR_INVALIDDATA;

        }

        for (; i < 257; i++) {

            rac->prob[i] = softfloat_mul(rac->prob[i], mul);

            scaled_cumul_prob += rac->prob[i];

        }



        scale_factor++;

        cumulative_target = 1 << scale_factor;



        if (scaled_cumul_prob > cumulative_target) {

            av_log(rac->avctx, AV_LOG_ERROR,

                   "Scaled probabilities are larger than target!\n");

            return -1;

        }



        scaled_cumul_prob = cumulative_target - scaled_cumul_prob;



        for (i = 1; scaled_cumul_prob; i = (i & 0x7f) + 1) {

            if (rac->prob[i]) {

                rac->prob[i]++;

                scaled_cumul_prob--;

            }

            /* Comment from reference source:

             * if (b & 0x80 == 0) {     // order of operations is 'wrong'; it has been left this way

             *                          // since the compression change is negligible and fixing it

             *                          // breaks backwards compatibility

             *      b =- (signed int)b;

             *      b &= 0xFF;

             * } else {

             *      b++;

             *      b &= 0x7f;

             * }

             */

        }

    }



    rac->scale = scale_factor;



    /* Fill probability array with cumulative probability for each symbol. */

    for (i = 1; i < 257; i++)

        rac->prob[i] += rac->prob[i - 1];



    return 0;

}

static int lag_read_prob_header(lag_rac *rac, GetBitContext *gb)

{

    int i, j, scale_factor;

    unsigned prob, cumulative_target;

    unsigned cumul_prob = 0;

    unsigned scaled_cumul_prob = 0;



    rac->prob[0] = 0;

    rac->prob[257] = UINT_MAX;

    

    for (i = 1; i < 257; i++) {

        if (lag_decode_prob(gb, &rac->prob[i]) < 0) {

            av_log(rac->avctx, AV_LOG_ERROR, "Invalid probability encountered.\n");

            return -1;

        }

        if ((uint64_t)cumul_prob + rac->prob[i] > UINT_MAX) {

            av_log(rac->avctx, AV_LOG_ERROR, "Integer overflow encountered in cumulative probability calculation.\n");

            return -1;

        }

        cumul_prob += rac->prob[i];

        if (!rac->prob[i]) {

            if (lag_decode_prob(gb, &prob)) {

                av_log(rac->avctx, AV_LOG_ERROR, "Invalid probability run encountered.\n");

                return -1;

            }

            if (prob > 256 - i)

                prob = 256 - i;

            for (j = 0; j < prob; j++)

                rac->prob[++i] = 0;

        }

    }



    if (!cumul_prob) {

        av_log(rac->avctx, AV_LOG_ERROR, "All probabilities are 0!\n");

        return -1;

    }



    

    scale_factor = av_log2(cumul_prob);



    if (cumul_prob & (cumul_prob - 1)) {

        uint64_t mul = softfloat_reciprocal(cumul_prob);

        for (i = 1; i <= 128; i++) {

            rac->prob[i] = softfloat_mul(rac->prob[i], mul);

            scaled_cumul_prob += rac->prob[i];

        }

        if (scaled_cumul_prob <= 0) {

            av_log(rac->avctx, AV_LOG_ERROR, "Scaled probabilities invalid\n");

            return AVERROR_INVALIDDATA;

        }

        for (; i < 257; i++) {

            rac->prob[i] = softfloat_mul(rac->prob[i], mul);

            scaled_cumul_prob += rac->prob[i];

        }



        scale_factor++;

        cumulative_target = 1 << scale_factor;



        if (scaled_cumul_prob > cumulative_target) {

            av_log(rac->avctx, AV_LOG_ERROR,

                   "Scaled probabilities are larger than target!\n");

            return -1;

        }



        scaled_cumul_prob = cumulative_target - scaled_cumul_prob;



        for (i = 1; scaled_cumul_prob; i = (i & 0x7f) + 1) {

            if (rac->prob[i]) {

                rac->prob[i]++;

                scaled_cumul_prob--;

            }

            





















        }

    }



    rac->scale = scale_factor;



    

    for (i = 1; i < 257; i++)

        rac->prob[i] += rac->prob[i - 1];



    return 0;

}

        cumulative_target = 1 << scale_factor;
static int FUNC_0(lag_rac *VAR_0, GetBitContext *VAR_1)

{

    int VAR_2, VAR_3, VAR_4;

    unsigned VAR_5, VAR_6;

    unsigned VAR_7 = 0;

    unsigned VAR_8 = 0;



    VAR_0->VAR_5[0] = 0;

    VAR_0->VAR_5[257] = UINT_MAX;

    

    for (VAR_2 = 1; VAR_2 < 257; VAR_2++) {

        if (lag_decode_prob(VAR_1, &VAR_0->VAR_5[VAR_2]) < 0) {

            av_log(VAR_0->avctx, AV_LOG_ERROR, "Invalid probability encountered.\n");

            return -1;

        }

        if ((uint64_t)VAR_7 + VAR_0->VAR_5[VAR_2] > UINT_MAX) {

            av_log(VAR_0->avctx, AV_LOG_ERROR, "Integer overflow encountered in cumulative probability calculation.\n");

            return -1;

        }

        VAR_7 += VAR_0->VAR_5[VAR_2];

        if (!VAR_0->VAR_5[VAR_2]) {

            if (lag_decode_prob(VAR_1, &VAR_5)) {

                av_log(VAR_0->avctx, AV_LOG_ERROR, "Invalid probability run encountered.\n");

                return -1;

            }

            if (VAR_5 > 256 - VAR_2)

                VAR_5 = 256 - VAR_2;

            for (VAR_3 = 0; VAR_3 < VAR_5; VAR_3++)

                VAR_0->VAR_5[++VAR_2] = 0;

        }

    }



    if (!VAR_7) {

        av_log(VAR_0->avctx, AV_LOG_ERROR, "All probabilities are 0!\n");

        return -1;

    }



    

    VAR_4 = av_log2(VAR_7);



    if (VAR_7 & (VAR_7 - 1)) {

        uint64_t mul = softfloat_reciprocal(VAR_7);

        for (VAR_2 = 1; VAR_2 <= 128; VAR_2++) {

            VAR_0->VAR_5[VAR_2] = softfloat_mul(VAR_0->VAR_5[VAR_2], mul);

            VAR_8 += VAR_0->VAR_5[VAR_2];

        }

        if (VAR_8 <= 0) {

            av_log(VAR_0->avctx, AV_LOG_ERROR, "Scaled probabilities invalid\n");

            return AVERROR_INVALIDDATA;

        }

        for (; VAR_2 < 257; VAR_2++) {

            VAR_0->VAR_5[VAR_2] = softfloat_mul(VAR_0->VAR_5[VAR_2], mul);

            VAR_8 += VAR_0->VAR_5[VAR_2];

        }



        VAR_4++;

        VAR_6 = 1 << VAR_4;



        if (VAR_8 > VAR_6) {

            av_log(VAR_0->avctx, AV_LOG_ERROR,

                   "Scaled probabilities are larger than target!\n");

            return -1;

        }



        VAR_8 = VAR_6 - VAR_8;



        for (VAR_2 = 1; VAR_8; VAR_2 = (VAR_2 & 0x7f) + 1) {

            if (VAR_0->VAR_5[VAR_2]) {

                VAR_0->VAR_5[VAR_2]++;

                VAR_8--;

            }

            





















        }

    }



    VAR_0->scale = VAR_4;



    

    for (VAR_2 = 1; VAR_2 < 257; VAR_2++)

        VAR_0->VAR_5[VAR_2] += VAR_0->VAR_5[VAR_2 - 1];



    return 0;

}

static int FUNC_0(lag_rac *VAR_0, GetBitContext *VAR_1)
{ int VAR_2, VAR_3, VAR_4; unsigned VAR_5, VAR_6; unsigned VAR_7 = 0; unsigned VAR_8 = 0; VAR_0->VAR_5[0] = 0; VAR_0->VAR_5[257] = UINT_MAX; for (VAR_2 = 1; VAR_2 < 257; VAR_2++) { if (lag_decode_prob(VAR_1, &VAR_0->VAR_5[VAR_2]) < 0) { av_log(VAR_0->avctx, AV_LOG_ERROR, "Invalid probability encountered.\n"); return -1; } if ((uint64_t)VAR_7 + VAR_0->VAR_5[VAR_2] > UINT_MAX) { av_log(VAR_0->avctx, AV_LOG_ERROR, "Integer overflow encountered in cumulative probability calculation.\n"); return -1; } VAR_7 += VAR_0->VAR_5[VAR_2]; if (!VAR_0->VAR_5[VAR_2]) { if (lag_decode_prob(VAR_1, &VAR_5)) { av_log(VAR_0->avctx, AV_LOG_ERROR, "Invalid probability run encountered.\n"); return -1; } if (VAR_5 > 256 - VAR_2)
VAR_5 = 256 - VAR_2; for (VAR_3 = 0; VAR_3 < VAR_5; VAR_3++) VAR_0->VAR_5[++VAR_2] = 0; } } if (!VAR_7) { av_log(VAR_0->avctx, AV_LOG_ERROR, "All probabilities are 0!\n"); return -1; } VAR_4 = av_log2(VAR_7); if (VAR_7 & (VAR_7 - 1)) { uint64_t mul = softfloat_reciprocal(VAR_7); for (VAR_2 = 1; VAR_2 <= 128; VAR_2++) { VAR_0->VAR_5[VAR_2] = softfloat_mul(VAR_0->VAR_5[VAR_2], mul); VAR_8 += VAR_0->VAR_5[VAR_2]; } if (VAR_8 <= 0) { av_log(VAR_0->avctx, AV_LOG_ERROR, "Scaled probabilities invalid\n"); return AVERROR_INVALIDDATA; } for (; VAR_2 < 257; VAR_2++) { VAR_0->VAR_5[VAR_2] = softfloat_mul(VAR_0->VAR_5[VAR_2], mul); VAR_8 += VAR_0->VAR_5[VAR_2]; } VAR_4++; VAR_6 = 1 << VAR_4; if (VAR_8 > VAR_6) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"Scaled probabilities are larger than target!\n"); return -1; } VAR_8 = VAR_6 - VAR_8; for (VAR_2 = 1; VAR_8; VAR_2 = (VAR_2 & 0x7f) + 1) { if (VAR_0->VAR_5[VAR_2]) { VAR_0->VAR_5[VAR_2]++; VAR_8--; } } } VAR_0->scale = VAR_4; for (VAR_2 = 1; VAR_2 < 257; VAR_2++) VAR_0->VAR_5[VAR_2] += VAR_0->VAR_5[VAR_2 - 1]; return 0; }