Revision b6a1e523

View differences:

libavcodec/ac3enc.c
93 93

  
94 94
    /* mantissa encoding */
95 95
    int mant1_cnt, mant2_cnt, mant4_cnt;    ///< mantissa counts for bap=1,2,4
96
    uint16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr; ///< mantissa pointers for bap=1,2,4
96 97

  
97 98
    int16_t last_samples[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE]; ///< last 256 samples from previous frame
98 99
} AC3EncodeContext;
......
968 969

  
969 970

  
970 971
/**
972
 * Quantize a set of mantissas for a single channel in a single block.
973
 */
974
static void quantize_mantissas_blk_ch(AC3EncodeContext *s,
975
                                      int32_t *mdct_coef, int8_t exp_shift,
976
                                      uint8_t *encoded_exp, uint8_t *bap,
977
                                      uint16_t *qmant, int n)
978
{
979
    int i;
980

  
981
    for (i = 0; i < n; i++) {
982
        int v;
983
        int c = mdct_coef[i];
984
        int e = encoded_exp[i] - exp_shift;
985
        int b = bap[i];
986
        switch (b) {
987
        case 0:
988
            v = 0;
989
            break;
990
        case 1:
991
            v = sym_quant(c, e, 3);
992
            switch (s->mant1_cnt) {
993
            case 0:
994
                s->qmant1_ptr = &qmant[i];
995
                v = 9 * v;
996
                s->mant1_cnt = 1;
997
                break;
998
            case 1:
999
                *s->qmant1_ptr += 3 * v;
1000
                s->mant1_cnt = 2;
1001
                v = 128;
1002
                break;
1003
            default:
1004
                *s->qmant1_ptr += v;
1005
                s->mant1_cnt = 0;
1006
                v = 128;
1007
                break;
1008
            }
1009
            break;
1010
        case 2:
1011
            v = sym_quant(c, e, 5);
1012
            switch (s->mant2_cnt) {
1013
            case 0:
1014
                s->qmant2_ptr = &qmant[i];
1015
                v = 25 * v;
1016
                s->mant2_cnt = 1;
1017
                break;
1018
            case 1:
1019
                *s->qmant2_ptr += 5 * v;
1020
                s->mant2_cnt = 2;
1021
                v = 128;
1022
                break;
1023
            default:
1024
                *s->qmant2_ptr += v;
1025
                s->mant2_cnt = 0;
1026
                v = 128;
1027
                break;
1028
            }
1029
            break;
1030
        case 3:
1031
            v = sym_quant(c, e, 7);
1032
            break;
1033
        case 4:
1034
            v = sym_quant(c, e, 11);
1035
            switch (s->mant4_cnt) {
1036
            case 0:
1037
                s->qmant4_ptr = &qmant[i];
1038
                v = 11 * v;
1039
                s->mant4_cnt = 1;
1040
                break;
1041
            default:
1042
                *s->qmant4_ptr += v;
1043
                s->mant4_cnt = 0;
1044
                v = 128;
1045
                break;
1046
            }
1047
            break;
1048
        case 5:
1049
            v = sym_quant(c, e, 15);
1050
            break;
1051
        case 14:
1052
            v = asym_quant(c, e, 14);
1053
            break;
1054
        case 15:
1055
            v = asym_quant(c, e, 16);
1056
            break;
1057
        default:
1058
            v = asym_quant(c, e, b - 1);
1059
            break;
1060
        }
1061
        qmant[i] = v;
1062
    }
1063
}
1064

  
1065

  
1066
/**
1067
 * Quantize mantissas using coefficients, exponents, and bit allocation pointers.
1068
 */
1069
static void quantize_mantissas(AC3EncodeContext *s,
1070
                               int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
1071
                               int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS],
1072
                               uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
1073
                               uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
1074
                               uint16_t qmant[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS])
1075
{
1076
    int blk, ch;
1077

  
1078

  
1079
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1080
        s->mant1_cnt  = s->mant2_cnt  = s->mant4_cnt  = 0;
1081
        s->qmant1_ptr = s->qmant2_ptr = s->qmant4_ptr = NULL;
1082

  
1083
        for (ch = 0; ch < s->channels; ch++) {
1084
            quantize_mantissas_blk_ch(s, mdct_coef[blk][ch], exp_shift[blk][ch],
1085
                                      encoded_exp[blk][ch], bap[blk][ch],
1086
                                      qmant[blk][ch], s->nb_coefs[ch]);
1087
        }
1088
    }
1089
}
1090

  
1091

  
1092
/**
971 1093
 * Write the AC-3 frame header to the output bitstream.
972 1094
 */
973 1095
static void output_frame_header(AC3EncodeContext *s)
......
1005 1127
                               uint8_t exp_strategy[AC3_MAX_CHANNELS],
1006 1128
                               uint8_t encoded_exp[AC3_MAX_CHANNELS][AC3_MAX_COEFS],
1007 1129
                               uint8_t bap[AC3_MAX_CHANNELS][AC3_MAX_COEFS],
1008
                               int32_t mdct_coef[AC3_MAX_CHANNELS][AC3_MAX_COEFS],
1009
                               int8_t exp_shift[AC3_MAX_CHANNELS],
1130
                               uint16_t qmant[AC3_MAX_CHANNELS][AC3_MAX_COEFS],
1010 1131
                               int block_num)
1011 1132
{
1012 1133
    int ch, nb_groups, group_size, i, baie, rbnd;
1013 1134
    uint8_t *p;
1014
    uint16_t qmant[AC3_MAX_CHANNELS][AC3_MAX_COEFS];
1015 1135
    int exp0, exp1;
1016
    int mant1_cnt, mant2_cnt, mant4_cnt;
1017
    uint16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr;
1018 1136
    int delta0, delta1, delta2;
1019 1137

  
1020 1138
    for (ch = 0; ch < s->fbw_channels; ch++)
......
1117 1235
    put_bits(&s->pb, 1, 0); /* no delta bit allocation */
1118 1236
    put_bits(&s->pb, 1, 0); /* no data to skip */
1119 1237

  
1120
    /* mantissa encoding : we use two passes to handle the grouping. A
1121
       one pass method may be faster, but it would necessitate to
1122
       modify the output stream. */
1123

  
1124
    /* first pass: quantize */
1125
    mant1_cnt = mant2_cnt = mant4_cnt = 0;
1126
    qmant1_ptr = qmant2_ptr = qmant4_ptr = NULL;
1127

  
1128
    for (ch = 0; ch < s->channels; ch++) {
1129
        int b, c, e, v;
1130

  
1131
        for (i = 0; i < s->nb_coefs[ch]; i++) {
1132
            c = mdct_coef[ch][i];
1133
            e = encoded_exp[ch][i] - exp_shift[ch];
1134
            b = bap[ch][i];
1135
            switch (b) {
1136
            case 0:
1137
                v = 0;
1138
                break;
1139
            case 1:
1140
                v = sym_quant(c, e, 3);
1141
                switch (mant1_cnt) {
1142
                case 0:
1143
                    qmant1_ptr = &qmant[ch][i];
1144
                    v = 9 * v;
1145
                    mant1_cnt = 1;
1146
                    break;
1147
                case 1:
1148
                    *qmant1_ptr += 3 * v;
1149
                    mant1_cnt = 2;
1150
                    v = 128;
1151
                    break;
1152
                default:
1153
                    *qmant1_ptr += v;
1154
                    mant1_cnt = 0;
1155
                    v = 128;
1156
                    break;
1157
                }
1158
                break;
1159
            case 2:
1160
                v = sym_quant(c, e, 5);
1161
                switch (mant2_cnt) {
1162
                case 0:
1163
                    qmant2_ptr = &qmant[ch][i];
1164
                    v = 25 * v;
1165
                    mant2_cnt = 1;
1166
                    break;
1167
                case 1:
1168
                    *qmant2_ptr += 5 * v;
1169
                    mant2_cnt = 2;
1170
                    v = 128;
1171
                    break;
1172
                default:
1173
                    *qmant2_ptr += v;
1174
                    mant2_cnt = 0;
1175
                    v = 128;
1176
                    break;
1177
                }
1178
                break;
1179
            case 3:
1180
                v = sym_quant(c, e, 7);
1181
                break;
1182
            case 4:
1183
                v = sym_quant(c, e, 11);
1184
                switch (mant4_cnt) {
1185
                case 0:
1186
                    qmant4_ptr = &qmant[ch][i];
1187
                    v = 11 * v;
1188
                    mant4_cnt = 1;
1189
                    break;
1190
                default:
1191
                    *qmant4_ptr += v;
1192
                    mant4_cnt = 0;
1193
                    v = 128;
1194
                    break;
1195
                }
1196
                break;
1197
            case 5:
1198
                v = sym_quant(c, e, 15);
1199
                break;
1200
            case 14:
1201
                v = asym_quant(c, e, 14);
1202
                break;
1203
            case 15:
1204
                v = asym_quant(c, e, 16);
1205
                break;
1206
            default:
1207
                v = asym_quant(c, e, b - 1);
1208
                break;
1209
            }
1210
            qmant[ch][i] = v;
1211
        }
1212
    }
1213

  
1214
    /* second pass : output the values */
1238
    /* mantissa encoding */
1215 1239
    for (ch = 0; ch < s->channels; ch++) {
1216 1240
        int b, q;
1217 1241

  
......
1313 1337
                         uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS],
1314 1338
                         uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
1315 1339
                         uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
1316
                         int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
1317
                         int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS])
1340
                         uint16_t qmant[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS])
1318 1341
{
1319 1342
    int blk;
1320 1343

  
......
1324 1347

  
1325 1348
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1326 1349
        output_audio_block(s, exp_strategy[blk], encoded_exp[blk],
1327
                           bap[blk], mdct_coef[blk], exp_shift[blk], blk);
1350
                           bap[blk], qmant[blk], blk);
1328 1351
    }
1329 1352

  
1330 1353
    output_frame_end(s);
......
1346 1369
    uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
1347 1370
    uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
1348 1371
    int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS];
1372
    uint16_t qmant[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
1349 1373
    int frame_bits;
1350 1374

  
1351 1375
    if (s->bit_alloc.sr_code == 1)
......
1359 1383

  
1360 1384
    compute_bit_allocation(s, bap, encoded_exp, exp_strategy, frame_bits);
1361 1385

  
1362
    output_frame(s, frame, exp_strategy, encoded_exp, bap, mdct_coef, exp_shift);
1386
    quantize_mantissas(s, mdct_coef, exp_shift, encoded_exp, bap, qmant);
1387

  
1388
    output_frame(s, frame, exp_strategy, encoded_exp, bap, qmant);
1363 1389

  
1364 1390
    return s->frame_size;
1365 1391
}

Also available in: Unified diff