static void residue_encode(venc_context_t * venc, residue_t * rc, PutBitContext * pb, float * coeffs, int samples, int real_ch) {

    int pass, i, j, p, k;

    int psize = rc->partition_size;

    int partitions = (rc->end - rc->begin) / psize;

    int channels = (rc->type == 2) ? 1 : real_ch;

    int classes[channels][partitions];

    int classwords = venc->codebooks[rc->classbook].ndimentions;



    assert(rc->type == 2);

    assert(real_ch == 2);

    for (p = 0; p < partitions; p++) {

        float max1 = 0., max2 = 0.;

        int s = rc->begin + p * psize;

        for (k = s; k < s + psize; k += 2) {

            max1 = FFMAX(max1, fabs(coeffs[          k / real_ch]));

            max2 = FFMAX(max2, fabs(coeffs[samples + k / real_ch]));

        }



        for (i = 0; i < rc->classifications - 1; i++) {

            if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1]) break;

        }

        classes[0][p] = i;

    }



    for (pass = 0; pass < 8; pass++) {

        p = 0;

        while (p < partitions) {

            if (pass == 0) for (j = 0; j < channels; j++) {

                codebook_t * book = &venc->codebooks[rc->classbook];

                int entry = 0;

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

                    entry *= rc->classifications;

                    entry += classes[j][p + i];

                }

                put_codeword(pb, book, entry);

            }

            for (i = 0; i < classwords && p < partitions; i++, p++) {

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

                    int nbook = rc->books[classes[j][p]][pass];

                    codebook_t * book = &venc->codebooks[nbook];

                    float * buf = coeffs + samples*j + rc->begin + p*psize;

                    if (nbook == -1) continue;



                    assert(rc->type == 0 || rc->type == 2);

                    assert(!(psize % book->ndimentions));



                    if (rc->type == 0) {

                        for (k = 0; k < psize; k += book->ndimentions) {

                            float * a = put_vector(book, pb, &buf[k]);

                            int l;

                            for (l = 0; l < book->ndimentions; l++) buf[k + l] -= a[l];

                        }

                    } else {

                        for (k = 0; k < psize; k += book->ndimentions) {

                            int dim = book->ndimentions, s = rc->begin + p * psize + k, l;

                            float vec[dim], * a = vec;

                            for (l = s; l < s + dim; l++)

                                *a++ = coeffs[(l % real_ch) * samples + l / real_ch];

                            a = put_vector(book, pb, vec);

                            for (l = s; l < s + dim; l++)

                                coeffs[(l % real_ch) * samples + l / real_ch] -= *a++;

                        }

                    }

                }

            }

        }

    }

}

static void residue_encode(venc_context_t * venc, residue_t * rc, PutBitContext * pb, float * coeffs, int samples, int real_ch) {

    int pass, i, j, p, k;

    int psize = rc->partition_size;

    int partitions = (rc->end - rc->begin) / psize;

    int channels = (rc->type == 2) ? 1 : real_ch;

    int classes[channels][partitions];

    int classwords = venc->codebooks[rc->classbook].ndimentions;



    assert(rc->type == 2);

    assert(real_ch == 2);

    for (p = 0; p < partitions; p++) {

        float max1 = 0., max2 = 0.;

        int s = rc->begin + p * psize;

        for (k = s; k < s + psize; k += 2) {

            max1 = FFMAX(max1, fabs(coeffs[          k / real_ch]));

            max2 = FFMAX(max2, fabs(coeffs[samples + k / real_ch]));

        }



        for (i = 0; i < rc->classifications - 1; i++) {

            if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1]) break;

        }

        classes[0][p] = i;

    }



    for (pass = 0; pass < 8; pass++) {

        p = 0;

        while (p < partitions) {

            if (pass == 0) for (j = 0; j < channels; j++) {

                codebook_t * book = &venc->codebooks[rc->classbook];

                int entry = 0;

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

                    entry *= rc->classifications;

                    entry += classes[j][p + i];

                }

                put_codeword(pb, book, entry);

            }

            for (i = 0; i < classwords && p < partitions; i++, p++) {

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

                    int nbook = rc->books[classes[j][p]][pass];

                    codebook_t * book = &venc->codebooks[nbook];

                    float * buf = coeffs + samples*j + rc->begin + p*psize;

                    if (nbook == -1) continue;



                    assert(rc->type == 0 || rc->type == 2);

                    assert(!(psize % book->ndimentions));



                    if (rc->type == 0) {

                        for (k = 0; k < psize; k += book->ndimentions) {

                            float * a = put_vector(book, pb, &buf[k]);

                            int l;

                            for (l = 0; l < book->ndimentions; l++) buf[k + l] -= a[l];

                        }

                    } else {

                        for (k = 0; k < psize; k += book->ndimentions) {

                            int dim = book->ndimentions, s = rc->begin + p * psize + k, l;

                            float vec[dim], * a = vec;

                            for (l = s; l < s + dim; l++)

                                *a++ = coeffs[(l % real_ch) * samples + l / real_ch];

                            a = put_vector(book, pb, vec);

                            for (l = s; l < s + dim; l++)

                                coeffs[(l % real_ch) * samples + l / real_ch] -= *a++;

                        }

                    }

                }

            }

        }

    }

}

static void FUNC_0(venc_context_t * VAR_0, residue_t * VAR_1, PutBitContext * VAR_2, float * VAR_3, int VAR_4, int VAR_5) {

    int VAR_6, VAR_7, VAR_8, VAR_9, VAR_10;

    int VAR_11 = VAR_1->partition_size;

    int VAR_12 = (VAR_1->end - VAR_1->begin) / VAR_11;

    int VAR_13 = (VAR_1->type == 2) ? 1 : VAR_5;

    int VAR_14[VAR_13][VAR_12];

    int VAR_15 = VAR_0->codebooks[VAR_1->classbook].ndimentions;



    assert(VAR_1->type == 2);

    assert(VAR_5 == 2);

    for (VAR_9 = 0; VAR_9 < VAR_12; VAR_9++) {

        float VAR_16 = 0., VAR_17 = 0.;

        int VAR_25 = VAR_1->begin + VAR_9 * VAR_11;

        for (VAR_10 = VAR_25; VAR_10 < VAR_25 + VAR_11; VAR_10 += 2) {

            VAR_16 = FFMAX(VAR_16, fabs(VAR_3[          VAR_10 / VAR_5]));

            VAR_17 = FFMAX(VAR_17, fabs(VAR_3[VAR_4 + VAR_10 / VAR_5]));

        }



        for (VAR_7 = 0; VAR_7 < VAR_1->classifications - 1; VAR_7++) {

            if (VAR_16 < VAR_1->maxes[VAR_7][0] && VAR_17 < VAR_1->maxes[VAR_7][1]) break;

        }

        VAR_14[0][VAR_9] = VAR_7;

    }



    for (VAR_6 = 0; VAR_6 < 8; VAR_6++) {

        VAR_9 = 0;

        while (VAR_9 < VAR_12) {

            if (VAR_6 == 0) for (VAR_8 = 0; VAR_8 < VAR_13; VAR_8++) {

                codebook_t * book = &VAR_0->codebooks[VAR_1->classbook];

                int VAR_19 = 0;

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

                    VAR_19 *= VAR_1->classifications;

                    VAR_19 += VAR_14[VAR_8][VAR_9 + VAR_7];

                }

                put_codeword(VAR_2, book, VAR_19);

            }

            for (VAR_7 = 0; VAR_7 < VAR_15 && VAR_9 < VAR_12; VAR_7++, VAR_9++) {

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

                    int VAR_20 = VAR_1->books[VAR_14[VAR_8][VAR_9]][VAR_6];

                    codebook_t * book = &VAR_0->codebooks[VAR_20];

                    float * VAR_21 = VAR_3 + VAR_4*VAR_8 + VAR_1->begin + VAR_9*VAR_11;

                    if (VAR_20 == -1) continue;



                    assert(VAR_1->type == 0 || VAR_1->type == 2);

                    assert(!(VAR_11 % book->ndimentions));



                    if (VAR_1->type == 0) {

                        for (VAR_10 = 0; VAR_10 < VAR_11; VAR_10 += book->ndimentions) {

                            float * VAR_26 = put_vector(book, VAR_2, &VAR_21[VAR_10]);

                            int VAR_25;

                            for (VAR_25 = 0; VAR_25 < book->ndimentions; VAR_25++) VAR_21[VAR_10 + VAR_25] -= VAR_26[VAR_25];

                        }

                    } else {

                        for (VAR_10 = 0; VAR_10 < VAR_11; VAR_10 += book->ndimentions) {

                            int VAR_24 = book->ndimentions, VAR_25 = VAR_1->begin + VAR_9 * VAR_11 + VAR_10, VAR_25;

                            float VAR_25[VAR_24], * VAR_26 = VAR_25;

                            for (VAR_25 = VAR_25; VAR_25 < VAR_25 + VAR_24; VAR_25++)

                                *VAR_26++ = VAR_3[(VAR_25 % VAR_5) * VAR_4 + VAR_25 / VAR_5];

                            VAR_26 = put_vector(book, VAR_2, VAR_25);

                            for (VAR_25 = VAR_25; VAR_25 < VAR_25 + VAR_24; VAR_25++)

                                VAR_3[(VAR_25 % VAR_5) * VAR_4 + VAR_25 / VAR_5] -= *VAR_26++;

                        }

                    }

                }

            }

        }

    }

}

static void FUNC_0(venc_context_t * VAR_0, residue_t * VAR_1, PutBitContext * VAR_2, float * VAR_3, int VAR_4, int VAR_5) { int VAR_6, VAR_7, VAR_8, VAR_9, VAR_10; int VAR_11 = VAR_1->partition_size; int VAR_12 = (VAR_1->end - VAR_1->begin) / VAR_11; int VAR_13 = (VAR_1->type == 2) ? 1 : VAR_5; int VAR_14[VAR_13][VAR_12]; int VAR_15 = VAR_0->codebooks[VAR_1->classbook].ndimentions; assert(VAR_1->type == 2); assert(VAR_5 == 2); for (VAR_9 = 0; VAR_9 < VAR_12; VAR_9++) { float VAR_16 = 0., VAR_17 = 0.; int VAR_25 = VAR_1->begin + VAR_9 * VAR_11; for (VAR_10 = VAR_25; VAR_10 < VAR_25 + VAR_11; VAR_10 += 2) { VAR_16 = FFMAX(VAR_16, fabs(VAR_3[          VAR_10 / VAR_5])); VAR_17 = FFMAX(VAR_17, fabs(VAR_3[VAR_4 + VAR_10 / VAR_5])); } for (VAR_7 = 0; VAR_7 < VAR_1->classifications - 1; VAR_7++) { if (VAR_16 < VAR_1->maxes[VAR_7][0] && VAR_17 < VAR_1->maxes[VAR_7][1]) break; } VAR_14[0][VAR_9] = VAR_7; } for (VAR_6 = 0; VAR_6 < 8; VAR_6++) { VAR_9 = 0; while (VAR_9 < VAR_12) { if (VAR_6 == 0) for (VAR_8 = 0; VAR_8 < VAR_13; VAR_8++) { codebook_t * book = &VAR_0->codebooks[VAR_1->classbook]; int VAR_19 = 0; for (VAR_7 = 0; VAR_7 < VAR_15; VAR_7++) { VAR_19 *= VAR_1->classifications; VAR_19 += VAR_14[VAR_8][VAR_9 + VAR_7]; } put_codeword(VAR_2, book, VAR_19); } for (VAR_7 = 0; VAR_7 < VAR_15 && VAR_9 < VAR_12; VAR_7++, VAR_9++) { for (VAR_8 = 0; VAR_8 < VAR_13; VAR_8++) { int VAR_20 = VAR_1->books[VAR_14[VAR_8][VAR_9]][VAR_6]; codebook_t * book = &VAR_0->codebooks[VAR_20]; float * VAR_21 = VAR_3 + VAR_4*VAR_8 + VAR_1->begin + VAR_9*VAR_11; if (VAR_20 == -1) continue; assert(VAR_1->type == 0 || VAR_1->type == 2); assert(!(VAR_11 % book->ndimentions)); if (VAR_1->type == 0) { for (VAR_10 = 0; VAR_10 < VAR_11; VAR_10 += book->ndimentions) { float * VAR_26 = put_vector(book, VAR_2, &VAR_21[VAR_10]); int VAR_25; for (VAR_25 = 0; VAR_25 < book->ndimentions; VAR_25++) VAR_21[VAR_10 + VAR_25] -= VAR_26[VAR_25]; } } else { for (VAR_10 = 0; VAR_10 < VAR_11; VAR_10 += book->ndimentions) { int VAR_24 = book->ndimentions, VAR_25 = VAR_1->begin + VAR_9 * VAR_11 + VAR_10, VAR_25; float VAR_25[VAR_24], * VAR_26 = VAR_25; for (VAR_25 = VAR_25; VAR_25 < VAR_25 + VAR_24; VAR_25++) *VAR_26++ = VAR_3[(VAR_25 % VAR_5) * VAR_4 + VAR_25 / VAR_5]; VAR_26 = put_vector(book, VAR_2, VAR_25); for (VAR_25 = VAR_25; VAR_25 < VAR_25 + VAR_24; VAR_25++) VAR_3[(VAR_25 % VAR_5) * VAR_4 + VAR_25 / VAR_5] -= *VAR_26++; } } } } } } }