static void synthfilt_build_sb_samples (QDM2Context *q, GetBitContext *gb, int length, int sb_min, int sb_max)
{
    int sb, j, k, n, ch, run, channels;
    int joined_stereo, zero_encoding, chs;
    int type34_first;
    float type34_div = 0;
    float type34_predictor;
    float samples[10], sign_bits[16];
    if (length == 0) {
        for (sb=sb_min; sb < sb_max; sb++)
            build_sb_samples_from_noise (q, sb);
        return;
    }
    for (sb = sb_min; sb < sb_max; sb++) {
        FIX_NOISE_IDX(q->noise_idx);
        channels = q->nb_channels;
        if (q->nb_channels <= 1 || sb < 12)
            joined_stereo = 0;
        else if (sb >= 24)
            joined_stereo = 1;
        else
            joined_stereo = (BITS_LEFT(length,gb) >= 1) ? get_bits1 (gb) : 0;
        if (joined_stereo) {
            if (BITS_LEFT(length,gb) >= 16)
                for (j = 0; j < 16; j++)
                    sign_bits[j] = get_bits1 (gb);
            for (j = 0; j < 64; j++)
                if (q->coding_method[1][sb][j] > q->coding_method[0][sb][j])
                    q->coding_method[0][sb][j] = q->coding_method[1][sb][j];
            fix_coding_method_array(sb, q->nb_channels, q->coding_method);
            channels = 1;
        }
        for (ch = 0; ch < channels; ch++) {
            zero_encoding = (BITS_LEFT(length,gb) >= 1) ? get_bits1(gb) : 0;
            type34_predictor = 0.0;
            type34_first = 1;
            for (j = 0; j < 128; ) {
                switch (q->coding_method[ch][sb][j / 2]) {
                    case 8:
                        if (BITS_LEFT(length,gb) >= 10) {
                            if (zero_encoding) {
                                for (k = 0; k < 5; k++) {
                                    if ((j + 2 * k) >= 128)
                                        break;
                                    samples[2 * k] = get_bits1(gb) ? dequant_1bit[joined_stereo][2 * get_bits1(gb)] : 0;
                                }
                            } else {
                                n = get_bits(gb, 8);
                                for (k = 0; k < 5; k++)
                                    samples[2 * k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
                            }
                            for (k = 0; k < 5; k++)
                                samples[2 * k + 1] = SB_DITHERING_NOISE(sb,q->noise_idx);
                        } else {
                            for (k = 0; k < 10; k++)
                                samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
                        }
                        run = 10;
                        break;
                    case 10:
                        if (BITS_LEFT(length,gb) >= 1) {
                            float f = 0.81;
                            if (get_bits1(gb))
                                f = -f;
                            f -= noise_samples[((sb + 1) * (j +5 * ch + 1)) & 127] * 9.0 / 40.0;
                            samples[0] = f;
                        } else {
                            samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
                        }
                        run = 1;
                        break;
                    case 16:
                        if (BITS_LEFT(length,gb) >= 10) {
                            if (zero_encoding) {
                                for (k = 0; k < 5; k++) {
                                    if ((j + k) >= 128)
                                        break;
                                    samples[k] = (get_bits1(gb) == 0) ? 0 : dequant_1bit[joined_stereo][2 * get_bits1(gb)];
                                }
                            } else {
                                n = get_bits (gb, 8);
                                for (k = 0; k < 5; k++)
                                    samples[k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
                            }
                        } else {
                            for (k = 0; k < 5; k++)
                                samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
                        }
                        run = 5;
                        break;
                    case 24:
                        if (BITS_LEFT(length,gb) >= 7) {
                            n = get_bits(gb, 7);
                            for (k = 0; k < 3; k++)
                                samples[k] = (random_dequant_type24[n][k] - 2.0) * 0.5;
                        } else {
                            for (k = 0; k < 3; k++)
                                samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
                        }
                        run = 3;
                        break;
                    case 30:
                        if (BITS_LEFT(length,gb) >= 4)
                            samples[0] = type30_dequant[qdm2_get_vlc(gb, &vlc_tab_type30, 0, 1)];
                        else
                            samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
                        run = 1;
                        break;
                    case 34:
                        if (BITS_LEFT(length,gb) >= 7) {
                            if (type34_first) {
                                type34_div = (float)(1 << get_bits(gb, 2));
                                samples[0] = ((float)get_bits(gb, 5) - 16.0) / 15.0;
                                type34_predictor = samples[0];
                                type34_first = 0;
                            } else {
                                samples[0] = type34_delta[qdm2_get_vlc(gb, &vlc_tab_type34, 0, 1)] / type34_div + type34_predictor;
                                type34_predictor = samples[0];
                            }
                        } else {
                            samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
                        }
                        run = 1;
                        break;
                    default:
                        samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
                        run = 1;
                        break;
                }
                if (joined_stereo) {
                    float tmp[10][MPA_MAX_CHANNELS];
                    for (k = 0; k < run; k++) {
                        tmp[k][0] = samples[k];
                        tmp[k][1] = (sign_bits[(j + k) / 8]) ? -samples[k] : samples[k];
                    }
                    for (chs = 0; chs < q->nb_channels; chs++)
                        for (k = 0; k < run; k++)
                            if ((j + k) < 128)
                                q->sb_samples[chs][j + k][sb] = (int32_t)(f2i_scale * q->tone_level[chs][sb][((j + k)/2)] * tmp[k][chs] + .5);
                } else {
                    for (k = 0; k < run; k++)
                        if ((j + k) < 128)
                            q->sb_samples[ch][j + k][sb] = (int32_t)(f2i_scale * q->tone_level[ch][sb][(j + k)/2] * samples[k] + .5);
                }
                j += run;
            }
        }
    }
}
['static void synthfilt_build_sb_samples (QDM2Context *q, GetBitContext *gb, int length, int sb_min, int sb_max)\n{\n    int sb, j, k, n, ch, run, channels;\n    int joined_stereo, zero_encoding, chs;\n    int type34_first;\n    float type34_div = 0;\n    float type34_predictor;\n    float samples[10], sign_bits[16];\n    if (length == 0) {\n        for (sb=sb_min; sb < sb_max; sb++)\n            build_sb_samples_from_noise (q, sb);\n        return;\n    }\n    for (sb = sb_min; sb < sb_max; sb++) {\n        FIX_NOISE_IDX(q->noise_idx);\n        channels = q->nb_channels;\n        if (q->nb_channels <= 1 || sb < 12)\n            joined_stereo = 0;\n        else if (sb >= 24)\n            joined_stereo = 1;\n        else\n            joined_stereo = (BITS_LEFT(length,gb) >= 1) ? get_bits1 (gb) : 0;\n        if (joined_stereo) {\n            if (BITS_LEFT(length,gb) >= 16)\n                for (j = 0; j < 16; j++)\n                    sign_bits[j] = get_bits1 (gb);\n            for (j = 0; j < 64; j++)\n                if (q->coding_method[1][sb][j] > q->coding_method[0][sb][j])\n                    q->coding_method[0][sb][j] = q->coding_method[1][sb][j];\n            fix_coding_method_array(sb, q->nb_channels, q->coding_method);\n            channels = 1;\n        }\n        for (ch = 0; ch < channels; ch++) {\n            zero_encoding = (BITS_LEFT(length,gb) >= 1) ? get_bits1(gb) : 0;\n            type34_predictor = 0.0;\n            type34_first = 1;\n            for (j = 0; j < 128; ) {\n                switch (q->coding_method[ch][sb][j / 2]) {\n                    case 8:\n                        if (BITS_LEFT(length,gb) >= 10) {\n                            if (zero_encoding) {\n                                for (k = 0; k < 5; k++) {\n                                    if ((j + 2 * k) >= 128)\n                                        break;\n                                    samples[2 * k] = get_bits1(gb) ? dequant_1bit[joined_stereo][2 * get_bits1(gb)] : 0;\n                                }\n                            } else {\n                                n = get_bits(gb, 8);\n                                for (k = 0; k < 5; k++)\n                                    samples[2 * k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];\n                            }\n                            for (k = 0; k < 5; k++)\n                                samples[2 * k + 1] = SB_DITHERING_NOISE(sb,q->noise_idx);\n                        } else {\n                            for (k = 0; k < 10; k++)\n                                samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);\n                        }\n                        run = 10;\n                        break;\n                    case 10:\n                        if (BITS_LEFT(length,gb) >= 1) {\n                            float f = 0.81;\n                            if (get_bits1(gb))\n                                f = -f;\n                            f -= noise_samples[((sb + 1) * (j +5 * ch + 1)) & 127] * 9.0 / 40.0;\n                            samples[0] = f;\n                        } else {\n                            samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);\n                        }\n                        run = 1;\n                        break;\n                    case 16:\n                        if (BITS_LEFT(length,gb) >= 10) {\n                            if (zero_encoding) {\n                                for (k = 0; k < 5; k++) {\n                                    if ((j + k) >= 128)\n                                        break;\n                                    samples[k] = (get_bits1(gb) == 0) ? 0 : dequant_1bit[joined_stereo][2 * get_bits1(gb)];\n                                }\n                            } else {\n                                n = get_bits (gb, 8);\n                                for (k = 0; k < 5; k++)\n                                    samples[k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];\n                            }\n                        } else {\n                            for (k = 0; k < 5; k++)\n                                samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);\n                        }\n                        run = 5;\n                        break;\n                    case 24:\n                        if (BITS_LEFT(length,gb) >= 7) {\n                            n = get_bits(gb, 7);\n                            for (k = 0; k < 3; k++)\n                                samples[k] = (random_dequant_type24[n][k] - 2.0) * 0.5;\n                        } else {\n                            for (k = 0; k < 3; k++)\n                                samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);\n                        }\n                        run = 3;\n                        break;\n                    case 30:\n                        if (BITS_LEFT(length,gb) >= 4)\n                            samples[0] = type30_dequant[qdm2_get_vlc(gb, &vlc_tab_type30, 0, 1)];\n                        else\n                            samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);\n                        run = 1;\n                        break;\n                    case 34:\n                        if (BITS_LEFT(length,gb) >= 7) {\n                            if (type34_first) {\n                                type34_div = (float)(1 << get_bits(gb, 2));\n                                samples[0] = ((float)get_bits(gb, 5) - 16.0) / 15.0;\n                                type34_predictor = samples[0];\n                                type34_first = 0;\n                            } else {\n                                samples[0] = type34_delta[qdm2_get_vlc(gb, &vlc_tab_type34, 0, 1)] / type34_div + type34_predictor;\n                                type34_predictor = samples[0];\n                            }\n                        } else {\n                            samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);\n                        }\n                        run = 1;\n                        break;\n                    default:\n                        samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);\n                        run = 1;\n                        break;\n                }\n                if (joined_stereo) {\n                    float tmp[10][MPA_MAX_CHANNELS];\n                    for (k = 0; k < run; k++) {\n                        tmp[k][0] = samples[k];\n                        tmp[k][1] = (sign_bits[(j + k) / 8]) ? -samples[k] : samples[k];\n                    }\n                    for (chs = 0; chs < q->nb_channels; chs++)\n                        for (k = 0; k < run; k++)\n                            if ((j + k) < 128)\n                                q->sb_samples[chs][j + k][sb] = (int32_t)(f2i_scale * q->tone_level[chs][sb][((j + k)/2)] * tmp[k][chs] + .5);\n                } else {\n                    for (k = 0; k < run; k++)\n                        if ((j + k) < 128)\n                            q->sb_samples[ch][j + k][sb] = (int32_t)(f2i_scale * q->tone_level[ch][sb][(j + k)/2] * samples[k] + .5);\n                }\n                j += run;\n            }\n        }\n    }\n}']