Revision dc7e07ac

View differences:

libavcodec/ac3dec_data.c
43 43
    { 3, 0, 1 }, { 3, 0, 2 }, { 3, 1, 0 }, { 3, 1, 1 }
44 44
};
45 45

  
46
/**
47
 * Table of bin locations for rematrixing bands
48
 * reference: Section 7.5.2 Rematrixing : Frequency Band Definitions
49
 */
50
const uint8_t ff_ac3_rematrix_band_tab[5] = { 13, 25, 37, 61, 253 };
51

  
52 46
const uint8_t ff_eac3_hebap_tab[64] = {
53 47
    0, 1, 2, 3, 4, 5, 6, 7, 8, 8,
54 48
    8, 8, 9, 9, 9, 10, 10, 10, 10, 11,
libavcodec/ac3dec_data.h
25 25
#include <stdint.h>
26 26

  
27 27
extern const uint8_t ff_ac3_ungroup_3_in_5_bits_tab[32][3];
28
extern const uint8_t ff_ac3_rematrix_band_tab[5];
29 28

  
30 29
extern const uint8_t ff_eac3_hebap_tab[64];
31 30
extern const uint8_t ff_eac3_default_cpl_band_struct[18];
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);
libavcodec/ac3enc_fixed.h
34 34

  
35 35
typedef int16_t SampleType;
36 36
typedef int32_t CoefType;
37
typedef int64_t CoefSumType;
37 38

  
38 39

  
39 40
/**
libavcodec/ac3enc_float.h
34 34

  
35 35
typedef float SampleType;
36 36
typedef float CoefType;
37
typedef float CoefSumType;
37 38

  
38 39

  
39 40
typedef struct AC3MDCTContext {
libavcodec/ac3tab.c
132 132
    160, 192, 224, 256, 320, 384, 448, 512, 576, 640
133 133
};
134 134

  
135
/**
136
 * Table of bin locations for rematrixing bands
137
 * reference: Section 7.5.2 Rematrixing : Frequency Band Definitions
138
 */
139
const uint8_t ff_ac3_rematrix_band_tab[5] = { 13, 25, 37, 61, 253 };
140

  
135 141
/* AC-3 MDCT window */
136 142

  
137 143
/* MDCT window */
libavcodec/ac3tab.h
32 32
extern const uint8_t  ff_ac3_dec_channel_map[8][2][6];
33 33
extern const uint16_t ff_ac3_sample_rate_tab[3];
34 34
extern const uint16_t ff_ac3_bitrate_tab[19];
35
extern const uint8_t  ff_ac3_rematrix_band_tab[5];
35 36
extern const int16_t  ff_ac3_window[AC3_WINDOW_SIZE/2];
36 37
extern const uint8_t  ff_ac3_log_add_tab[260];
37 38
extern const uint16_t ff_ac3_hearing_threshold_tab[AC3_CRITICAL_BANDS][3];
tests/ref/acodec/ac3_fixed
1
b315176b519a63a35cb91566e768f62b *./tests/data/acodec/ac3.rm
1
9823c8f74097eab5d148cf0536ae932e *./tests/data/acodec/ac3.rm
2 2
98751 ./tests/data/acodec/ac3.rm
tests/ref/seek/ac3_rm
5 5
ret:-1         st: 0 flags:0  ts: 0.788000
6 6
ret: 0         st: 0 flags:1  ts:-0.317000
7 7
ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    271 size:   556
8
ret: 0         st:-1 flags:0  ts: 2.576668
9
ret: 0         st: 0 flags:1 dts: 4.179000 pts: 4.179000 pos:  68585 size:   556
8
ret:-1         st:-1 flags:0  ts: 2.576668
10 9
ret:-1         st:-1 flags:1  ts: 1.470835
11
ret: 0         st: 0 flags:0  ts: 0.365000
12
ret: 0         st: 0 flags:1 dts: 0.383000 pts: 0.383000 pos:   6533 size:   558
10
ret:-1         st: 0 flags:0  ts: 0.365000
13 11
ret: 0         st: 0 flags:1  ts:-0.741000
14 12
ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    271 size:   556
15 13
ret:-1         st:-1 flags:0  ts: 2.153336
......
18 16
ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    271 size:   556
19 17
ret:-1         st: 0 flags:1  ts: 2.836000
20 18
ret:-1         st:-1 flags:0  ts: 1.730004
21
ret: 0         st:-1 flags:1  ts: 0.624171
22
ret: 0         st: 0 flags:1 dts: 0.452000 pts: 0.452000 pos:   7671 size:   558
19
ret:-1         st:-1 flags:1  ts: 0.624171
23 20
ret: 0         st: 0 flags:0  ts:-0.482000
24 21
ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    271 size:   556
25
ret: 0         st: 0 flags:1  ts: 2.413000
26
ret: 0         st: 0 flags:1 dts: 2.333000 pts: 2.333000 pos:  38413 size:   556
22
ret:-1         st: 0 flags:1  ts: 2.413000
27 23
ret:-1         st:-1 flags:0  ts: 1.306672
28 24
ret:-1         st:-1 flags:1  ts: 0.200839
29 25
ret: 0         st: 0 flags:0  ts:-0.905000
30 26
ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    271 size:   556
31
ret: 0         st: 0 flags:1  ts: 1.989000
32
ret: 0         st: 0 flags:1 dts: 1.985000 pts: 1.985000 pos:  32719 size:   558
27
ret:-1         st: 0 flags:1  ts: 1.989000
33 28
ret:-1         st:-1 flags:0  ts: 0.883340
34 29
ret: 0         st:-1 flags:1  ts:-0.222493
35 30
ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    271 size:   556
36
ret: 0         st: 0 flags:0  ts: 2.672000
37
ret: 0         st: 0 flags:1 dts: 4.179000 pts: 4.179000 pos:  68585 size:   556
31
ret:-1         st: 0 flags:0  ts: 2.672000
38 32
ret:-1         st: 0 flags:1  ts: 1.566000
39
ret: 0         st:-1 flags:0  ts: 0.460008
40
ret: 0         st: 0 flags:1 dts: 4.179000 pts: 4.179000 pos:  68585 size:   556
33
ret:-1         st:-1 flags:0  ts: 0.460008
41 34
ret: 0         st:-1 flags:1  ts:-0.645825
42 35
ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    271 size:   556

Also available in: Unified diff