Revision 0a0a8500

View differences:

libavcodec/ac3enc.c
313 313

  
314 314

  
315 315
/**
316
 * Apply KBD window to input samples prior to MDCT.
317
 */
318
static void apply_window(int16_t *output, const int16_t *input,
319
                         const int16_t *window, int n)
320
{
321
    int i;
322
    int n2 = n >> 1;
323

  
324
    for (i = 0; i < n2; i++) {
325
        output[i]     = MUL16(input[i],     window[i]) >> 15;
326
        output[n-i-1] = MUL16(input[n-i-1], window[i]) >> 15;
327
    }
328
}
329

  
330

  
331
/**
316 332
 * Calculate the log2() of the maximum absolute value in an array.
317 333
 * @param tab input array
318 334
 * @param n   number of values in the array
......
352 368

  
353 369

  
354 370
/**
371
 * Normalize the input samples to use the maximum available precision.
372
 * This assumes signed 16-bit input samples. Exponents are reduced by 9 to
373
 * match the 24-bit internal precision for MDCT coefficients.
374
 *
375
 * @return exponent shift
376
 */
377
static int normalize_samples(AC3EncodeContext *s,
378
                             int16_t windowed_samples[AC3_WINDOW_SIZE])
379
{
380
    int v = 14 - log2_tab(windowed_samples, AC3_WINDOW_SIZE);
381
    v = FFMAX(0, v);
382
    lshift_tab(windowed_samples, AC3_WINDOW_SIZE, v);
383
    return v - 9;
384
}
385

  
386

  
387
/**
388
 * Apply the MDCT to input samples to generate frequency coefficients.
389
 * This applies the KBD window and normalizes the input to reduce precision
390
 * loss due to fixed-point calculations.
391
 */
392
static void apply_mdct(AC3EncodeContext *s,
393
                       int16_t planar_samples[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE+AC3_FRAME_SIZE],
394
                       int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS],
395
                       int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS])
396
{
397
    int blk, ch;
398
    int16_t windowed_samples[AC3_WINDOW_SIZE];
399

  
400
    for (ch = 0; ch < s->channels; ch++) {
401
        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
402
            const int16_t *input_samples = &planar_samples[ch][blk * AC3_BLOCK_SIZE];
403

  
404
            apply_window(windowed_samples, input_samples, ff_ac3_window, AC3_WINDOW_SIZE);
405

  
406
            exp_shift[blk][ch] = normalize_samples(s, windowed_samples);
407

  
408
            mdct512(mdct_coef[blk][ch], windowed_samples);
409
        }
410
    }
411
}
412

  
413

  
414
/**
355 415
 * Calculate the sum of absolute differences (SAD) between 2 sets of exponents.
356 416
 */
357 417
static int calc_exp_diff(uint8_t *exp1, uint8_t *exp2, int n)
......
1117 1177
    int v;
1118 1178
    int blk, blk1, blk2, ch, i;
1119 1179
    int16_t planar_samples[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE+AC3_FRAME_SIZE];
1120
    int16_t windowed_samples[AC3_WINDOW_SIZE];
1121 1180
    int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
1122 1181
    uint8_t exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
1123 1182
    uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS];
......
1128 1187

  
1129 1188
    deinterleave_input_samples(s, samples, planar_samples);
1130 1189

  
1131
    /* apply MDCT */
1132
    for (ch = 0; ch < s->channels; ch++) {
1133
        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1134
            int16_t *input_samples = &planar_samples[ch][blk * AC3_BLOCK_SIZE];
1135

  
1136
            /* apply the MDCT window */
1137
            for (i = 0; i < AC3_BLOCK_SIZE; i++) {
1138
                windowed_samples[i]                   = MUL16(input_samples[i],
1139
                                                           ff_ac3_window[i]) >> 15;
1140
                windowed_samples[AC3_WINDOW_SIZE-i-1] = MUL16(input_samples[AC3_WINDOW_SIZE-i-1],
1141
                                                           ff_ac3_window[i]) >> 15;
1142
            }
1143

  
1144
            /* Normalize the samples to use the maximum available precision */
1145
            v = 14 - log2_tab(windowed_samples, AC3_WINDOW_SIZE);
1146
            if (v < 0)
1147
                v = 0;
1148
            exp_shift[blk][ch] = v - 9;
1149
            lshift_tab(windowed_samples, AC3_WINDOW_SIZE, v);
1150

  
1151
            /* do the MDCT */
1152
            mdct512(mdct_coef[blk][ch], windowed_samples);
1153
        }
1154
    }
1190
    apply_mdct(s, planar_samples, exp_shift, mdct_coef);
1155 1191

  
1156 1192
    /* extract exponents */
1157 1193
    for (ch = 0; ch < s->channels; ch++) {

Also available in: Unified diff