static void compute_scale_factors(unsigned char scale_code[SBLIMIT],
                                  unsigned char scale_factors[SBLIMIT][3],
                                  int sb_samples[3][12][SBLIMIT],
                                  int sblimit)
{
    int *p, vmax, v, n, i, j, k, code;
    int index, d1, d2;
    unsigned char *sf = &scale_factors[0][0];
    for(j=0;j<sblimit;j++) {
        for(i=0;i<3;i++) {
            p = &sb_samples[i][0][j];
            vmax = abs(*p);
            for(k=1;k<12;k++) {
                p += SBLIMIT;
                v = abs(*p);
                if (v > vmax)
                    vmax = v;
            }
            if (vmax > 0) {
                n = av_log2(vmax);
                index = (21 - n) * 3 - 3;
                if (index >= 0) {
                    while (vmax <= scale_factor_table[index+1])
                        index++;
                } else {
                    index = 0;
                }
            } else {
                index = 62;
            }
#if 0
            printf("%2d:%d in=%x %x %d\n",
                   j, i, vmax, scale_factor_table[index], index);
#endif
            assert(index >=0 && index <= 63);
            sf[i] = index;
        }
        d1 = scale_diff_table[sf[0] - sf[1] + 64];
        d2 = scale_diff_table[sf[1] - sf[2] + 64];
        switch(d1 * 5 + d2) {
        case 0*5+0:
        case 0*5+4:
        case 3*5+4:
        case 4*5+0:
        case 4*5+4:
            code = 0;
            break;
        case 0*5+1:
        case 0*5+2:
        case 4*5+1:
        case 4*5+2:
            code = 3;
            sf[2] = sf[1];
            break;
        case 0*5+3:
        case 4*5+3:
            code = 3;
            sf[1] = sf[2];
            break;
        case 1*5+0:
        case 1*5+4:
        case 2*5+4:
            code = 1;
            sf[1] = sf[0];
            break;
        case 1*5+1:
        case 1*5+2:
        case 2*5+0:
        case 2*5+1:
        case 2*5+2:
            code = 2;
            sf[1] = sf[2] = sf[0];
            break;
        case 2*5+3:
        case 3*5+3:
            code = 2;
            sf[0] = sf[1] = sf[2];
            break;
        case 3*5+0:
        case 3*5+1:
        case 3*5+2:
            code = 2;
            sf[0] = sf[2] = sf[1];
            break;
        case 1*5+3:
            code = 2;
            if (sf[0] > sf[2])
              sf[0] = sf[2];
            sf[1] = sf[2] = sf[0];
            break;
        default:
            assert(0);
            code = 0;
        }
#if 0
        printf("%d: %2d %2d %2d %d %d -> %d\n", j,
               sf[0], sf[1], sf[2], d1, d2, code);
#endif
        scale_code[j] = code;
        sf += 3;
    }
}
['static void compute_scale_factors(unsigned char scale_code[SBLIMIT],\n                                  unsigned char scale_factors[SBLIMIT][3],\n                                  int sb_samples[3][12][SBLIMIT],\n                                  int sblimit)\n{\n    int *p, vmax, v, n, i, j, k, code;\n    int index, d1, d2;\n    unsigned char *sf = &scale_factors[0][0];\n    for(j=0;j<sblimit;j++) {\n        for(i=0;i<3;i++) {\n            p = &sb_samples[i][0][j];\n            vmax = abs(*p);\n            for(k=1;k<12;k++) {\n                p += SBLIMIT;\n                v = abs(*p);\n                if (v > vmax)\n                    vmax = v;\n            }\n            if (vmax > 0) {\n                n = av_log2(vmax);\n                index = (21 - n) * 3 - 3;\n                if (index >= 0) {\n                    while (vmax <= scale_factor_table[index+1])\n                        index++;\n                } else {\n                    index = 0;\n                }\n            } else {\n                index = 62;\n            }\n#if 0\n            printf("%2d:%d in=%x %x %d\\n",\n                   j, i, vmax, scale_factor_table[index], index);\n#endif\n            assert(index >=0 && index <= 63);\n            sf[i] = index;\n        }\n        d1 = scale_diff_table[sf[0] - sf[1] + 64];\n        d2 = scale_diff_table[sf[1] - sf[2] + 64];\n        switch(d1 * 5 + d2) {\n        case 0*5+0:\n        case 0*5+4:\n        case 3*5+4:\n        case 4*5+0:\n        case 4*5+4:\n            code = 0;\n            break;\n        case 0*5+1:\n        case 0*5+2:\n        case 4*5+1:\n        case 4*5+2:\n            code = 3;\n            sf[2] = sf[1];\n            break;\n        case 0*5+3:\n        case 4*5+3:\n            code = 3;\n            sf[1] = sf[2];\n            break;\n        case 1*5+0:\n        case 1*5+4:\n        case 2*5+4:\n            code = 1;\n            sf[1] = sf[0];\n            break;\n        case 1*5+1:\n        case 1*5+2:\n        case 2*5+0:\n        case 2*5+1:\n        case 2*5+2:\n            code = 2;\n            sf[1] = sf[2] = sf[0];\n            break;\n        case 2*5+3:\n        case 3*5+3:\n            code = 2;\n            sf[0] = sf[1] = sf[2];\n            break;\n        case 3*5+0:\n        case 3*5+1:\n        case 3*5+2:\n            code = 2;\n            sf[0] = sf[2] = sf[1];\n            break;\n        case 1*5+3:\n            code = 2;\n            if (sf[0] > sf[2])\n              sf[0] = sf[2];\n            sf[1] = sf[2] = sf[0];\n            break;\n        default:\n            assert(0);\n            code = 0;\n        }\n#if 0\n        printf("%d: %2d %2d %2d %d %d -> %d\\n", j,\n               sf[0], sf[1], sf[2], d1, d2, code);\n#endif\n        scale_code[j] = code;\n        sf += 3;\n    }\n}']