Revision dc7e07ac libavcodec/ac3enc.c

View differences:

libavcodec/ac3enc.c
45 45
/** Maximum number of exponent groups. +1 for separate DC exponent. */
46 46
#define AC3_MAX_EXP_GROUPS 85
47 47

  
48
/* stereo rematrixing algorithms */
49
#define AC3_REMATRIXING_IS_STATIC 0x1
50
#define AC3_REMATRIXING_SUMS    0
51
#define AC3_REMATRIXING_NONE    1
52
#define AC3_REMATRIXING_ALWAYS  3
53

  
48 54
/** Scale a float value by 2^bits and convert to an integer. */
49 55
#define SCALE_FLOAT(a, bits) lrintf((a) * (float)(1 << (bits)))
50 56

  
......
71 77
    uint16_t **qmant;                           ///< quantized mantissas
72 78
    uint8_t  exp_strategy[AC3_MAX_CHANNELS];    ///< exponent strategies
73 79
    int8_t   exp_shift[AC3_MAX_CHANNELS];       ///< exponent shift values
80
    uint8_t  new_rematrixing_strategy;          ///< send new rematrixing flags in this block
81
    uint8_t  rematrixing_flags[4];              ///< rematrixing flags
74 82
} AC3Block;
75 83

  
76 84
/**
......
107 115
    int bandwidth_code[AC3_MAX_CHANNELS];   ///< bandwidth code (0 to 60)               (chbwcod)
108 116
    int nb_coefs[AC3_MAX_CHANNELS];
109 117

  
118
    int rematrixing;                        ///< determines how rematrixing strategy is calculated
119

  
110 120
    /* bitrate allocation control */
111 121
    int slow_gain_code;                     ///< slow gain code                         (sgaincod)
112 122
    int slow_decay_code;                    ///< slow decay code                        (sdcycod)
......
262 272

  
263 273

  
264 274
/**
275
 * Initialize stereo rematrixing.
276
 * If the strategy does not change for each frame, set the rematrixing flags.
277
 */
278
static void rematrixing_init(AC3EncodeContext *s)
279
{
280
    if (s->channel_mode == AC3_CHMODE_STEREO)
281
        s->rematrixing = AC3_REMATRIXING_SUMS;
282
    else
283
        s->rematrixing = AC3_REMATRIXING_NONE;
284
    /* NOTE: AC3_REMATRIXING_ALWAYS might be used in
285
             the future in conjunction with channel coupling. */
286

  
287
    if (s->rematrixing & AC3_REMATRIXING_IS_STATIC) {
288
        int flag = (s->rematrixing == AC3_REMATRIXING_ALWAYS);
289
        s->blocks[0].new_rematrixing_strategy = 1;
290
        memset(s->blocks[0].rematrixing_flags, flag,
291
               sizeof(s->blocks[0].rematrixing_flags));
292
    }
293
}
294

  
295

  
296
/**
297
 * Determine rematrixing flags for each block and band.
298
 */
299
static void compute_rematrixing_strategy(AC3EncodeContext *s)
300
{
301
    int nb_coefs;
302
    int blk, bnd, i;
303
    AC3Block *block, *block0;
304

  
305
    if (s->rematrixing & AC3_REMATRIXING_IS_STATIC)
306
        return;
307

  
308
    nb_coefs = FFMIN(s->nb_coefs[0], s->nb_coefs[1]);
309

  
310
    s->blocks[0].new_rematrixing_strategy = 1;
311
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
312
        block = &s->blocks[blk];
313
        for (bnd = 0; bnd < 4; bnd++) {
314
            /* calculate calculate sum of squared coeffs for one band in one block */
315
            int start = ff_ac3_rematrix_band_tab[bnd];
316
            int end   = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]);
317
            CoefSumType sum[4] = {0,};
318
            for (i = start; i < end; i++) {
319
                CoefType lt = block->mdct_coef[0][i];
320
                CoefType rt = block->mdct_coef[1][i];
321
                CoefType md = lt + rt;
322
                CoefType sd = lt - rt;
323
                sum[0] += lt * lt;
324
                sum[1] += rt * rt;
325
                sum[2] += md * md;
326
                sum[3] += sd * sd;
327
            }
328

  
329
            /* compare sums to determine if rematrixing will be used for this band */
330
            if (FFMIN(sum[2], sum[3]) < FFMIN(sum[0], sum[1]))
331
                block->rematrixing_flags[bnd] = 1;
332
            else
333
                block->rematrixing_flags[bnd] = 0;
334

  
335
            /* determine if new rematrixing flags will be sent */
336
            if (blk &&
337
                !block->new_rematrixing_strategy &&
338
                block->rematrixing_flags[bnd] != block0->rematrixing_flags[bnd]) {
339
                block->new_rematrixing_strategy = 1;
340
            }
341
        }
342
        block0 = block;
343
    }
344
}
345

  
346

  
347
/**
348
 * Apply stereo rematrixing to coefficients based on rematrixing flags.
349
 */
350
static void apply_rematrixing(AC3EncodeContext *s)
351
{
352
    int nb_coefs;
353
    int blk, bnd, i;
354
    int start, end;
355
    uint8_t *flags;
356

  
357
    if (s->rematrixing == AC3_REMATRIXING_NONE)
358
        return;
359

  
360
    nb_coefs = FFMIN(s->nb_coefs[0], s->nb_coefs[1]);
361

  
362
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
363
        AC3Block *block = &s->blocks[blk];
364
        if (block->new_rematrixing_strategy)
365
            flags = block->rematrixing_flags;
366
        for (bnd = 0; bnd < 4; bnd++) {
367
            if (flags[bnd]) {
368
                start = ff_ac3_rematrix_band_tab[bnd];
369
                end   = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]);
370
                for (i = start; i < end; i++) {
371
                    int32_t lt = block->fixed_coef[0][i];
372
                    int32_t rt = block->fixed_coef[1][i];
373
                    block->fixed_coef[0][i] = (lt + rt) >> 1;
374
                    block->fixed_coef[1][i] = (lt - rt) >> 1;
375
                }
376
            }
377
        }
378
    }
379
}
380

  
381

  
382
/**
265 383
 * Initialize exponent tables.
266 384
 */
267 385
static av_cold void exponent_init(AC3EncodeContext *s)
......
592 710
    /* assumptions:
593 711
     *   no dynamic range codes
594 712
     *   no channel coupling
595
     *   no rematrixing
596 713
     *   bit allocation parameters do not change between blocks
597 714
     *   SNR offsets do not change between blocks
598 715
     *   no delta bit allocation
......
609 726
        frame_bits += s->fbw_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */
610 727
        if (s->channel_mode == AC3_CHMODE_STEREO) {
611 728
            frame_bits++; /* rematstr */
612
            if (!blk)
613
                frame_bits += 4;
614 729
        }
615 730
        frame_bits += 2 * s->fbw_channels; /* chexpstr[2] * c */
616 731
        if (s->lfe_on)
......
681 796

  
682 797
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
683 798
        uint8_t *exp_strategy = s->blocks[blk].exp_strategy;
799

  
800
        /* stereo rematrixing */
801
        if (s->channel_mode == AC3_CHMODE_STEREO &&
802
            s->blocks[blk].new_rematrixing_strategy) {
803
            frame_bits += 4;
804
        }
805

  
684 806
        for (ch = 0; ch < s->fbw_channels; ch++) {
685 807
            if (exp_strategy[ch] != EXP_REUSE)
686 808
                frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */
......
1194 1316

  
1195 1317
    /* stereo rematrixing */
1196 1318
    if (s->channel_mode == AC3_CHMODE_STEREO) {
1197
        if (!block_num) {
1198
            /* first block must define rematrixing (rematstr) */
1199
            put_bits(&s->pb, 1, 1);
1200

  
1201
            /* dummy rematrixing rematflg(1:4)=0 */
1319
        put_bits(&s->pb, 1, block->new_rematrixing_strategy);
1320
        if (block->new_rematrixing_strategy) {
1321
            /* rematrixing flags */
1202 1322
            for (rbnd = 0; rbnd < 4; rbnd++)
1203
                put_bits(&s->pb, 1, 0);
1204
        } else {
1205
            /* no matrixing (but should be used in the future) */
1206
            put_bits(&s->pb, 1, 0);
1323
                put_bits(&s->pb, 1, block->rematrixing_flags[rbnd]);
1207 1324
        }
1208 1325
    }
1209 1326

  
......
1394 1511

  
1395 1512
    apply_mdct(s);
1396 1513

  
1514
    compute_rematrixing_strategy(s);
1515

  
1397 1516
    scale_coefficients(s);
1398 1517

  
1518
    apply_rematrixing(s);
1519

  
1399 1520
    process_exponents(s);
1400 1521

  
1401 1522
    ret = compute_bit_allocation(s);
......
1707 1828

  
1708 1829
    set_bandwidth(s);
1709 1830

  
1831
    rematrixing_init(s);
1832

  
1710 1833
    exponent_init(s);
1711 1834

  
1712 1835
    bit_alloc_init(s);

Also available in: Unified diff