Browse code

ac3enc: add channel coupling support

Channel coupling is an optional AC-3 feature that increases quality by
combining high frequency information from multiple channels into a
single channel. The per-channel high frequency information is sent with
less accuracy in both the frequency and time domains. This allows more
bits to be used for lower frequencies while preserving enough
information to reconstruct the high frequencies.

Justin Ruggles authored on 2011/05/24 00:45:51
Showing 10 changed files
... ...
@@ -365,4 +365,47 @@ is highly recommended that it be left as enabled except for testing purposes.
365 365
 
366 366
 @end table
367 367
 
368
+@subheading Floating-Point-Only AC-3 Encoding Options
369
+
370
+These options are only valid for the floating-point encoder and do not exist
371
+for the fixed-point encoder due to the corresponding features not being
372
+implemented in fixed-point.
373
+
374
+@table @option
375
+
376
+@item -channel_coupling @var{boolean}
377
+Enables/Disables use of channel coupling, which is an optional AC-3 feature
378
+that increases quality by combining high frequency information from multiple
379
+channels into a single channel. The per-channel high frequency information is
380
+sent with less accuracy in both the frequency and time domains. This allows
381
+more bits to be used for lower frequencies while preserving enough information
382
+to reconstruct the high frequencies. This option is enabled by default for the
383
+floating-point encoder and should generally be left as enabled except for
384
+testing purposes or to increase encoding speed.
385
+@table @option
386
+@item -1
387
+@itemx auto
388
+Selected by Encoder (default)
389
+@item 0
390
+@itemx off
391
+Disable Channel Coupling
392
+@item 1
393
+@itemx on
394
+Enable Channel Coupling
395
+@end table
396
+
397
+@item -cpl_start_band @var{number}
398
+Coupling Start Band. Sets the channel coupling start band, from 1 to 15. If a
399
+value higher than the bandwidth is used, it will be reduced to 1 less than the
400
+coupling end band. If @var{auto} is used, the start band will be determined by
401
+the encoder based on the bit rate, sample rate, and channel layout. This option
402
+has no effect if channel coupling is disabled.
403
+@table @option
404
+@item -1
405
+@itemx auto
406
+Selected by Encoder (default)
407
+@end table
408
+
409
+@end table
410
+
368 411
 @c man end ENCODERS
... ...
@@ -28,7 +28,8 @@
28 28
 #define AVCODEC_AC3_H
29 29
 
30 30
 #define AC3_MAX_CODED_FRAME_SIZE 3840 /* in bytes */
31
-#define AC3_MAX_CHANNELS 6 /* including LFE channel */
31
+#define AC3_MAX_CHANNELS 7            /**< maximum number of channels, including coupling channel */
32
+#define CPL_CH 0                      /**< coupling channel index */
32 33
 
33 34
 #define AC3_MAX_COEFS   256
34 35
 #define AC3_BLOCK_SIZE  256
... ...
@@ -58,11 +58,6 @@
58 58
 #include "fft.h"
59 59
 #include "fmtconvert.h"
60 60
 
61
-/* override ac3.h to include coupling channel */
62
-#undef AC3_MAX_CHANNELS
63
-#define AC3_MAX_CHANNELS 7
64
-#define CPL_CH 0
65
-
66 61
 #define AC3_OUTPUT_LFEON  8
67 62
 
68 63
 #define SPX_MAX_BANDS    17
... ...
@@ -54,12 +54,6 @@ const uint8_t ff_eac3_hebap_tab[64] = {
54 54
 };
55 55
 
56 56
 /**
57
- * Table E2.16 Default Coupling Banding Structure
58
- */
59
-const uint8_t ff_eac3_default_cpl_band_struct[18] =
60
-{ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1 };
61
-
62
-/**
63 57
  * Table E2.15 Default Spectral Extension Banding Structure
64 58
  */
65 59
 const uint8_t ff_eac3_default_spx_band_struct[17] =
... ...
@@ -27,7 +27,6 @@
27 27
 extern const uint8_t ff_ac3_ungroup_3_in_5_bits_tab[32][3];
28 28
 
29 29
 extern const uint8_t ff_eac3_hebap_tab[64];
30
-extern const uint8_t ff_eac3_default_cpl_band_struct[18];
31 30
 extern const uint8_t ff_eac3_default_spx_band_struct[17];
32 31
 
33 32
 #endif /* AVCODEC_AC3DEC_DATA_H */
... ...
@@ -99,6 +99,8 @@ typedef struct AC3EncOptions {
99 99
     /* other encoding options */
100 100
     int allow_per_frame_metadata;
101 101
     int stereo_rematrixing;
102
+    int channel_coupling;
103
+    int cpl_start;
102 104
 } AC3EncOptions;
103 105
 
104 106
 /**
... ...
@@ -114,10 +116,22 @@ typedef struct AC3Block {
114 114
     int16_t  **band_psd;                        ///< psd per critical band
115 115
     int16_t  **mask;                            ///< masking curve
116 116
     uint16_t **qmant;                           ///< quantized mantissas
117
+    uint8_t  **cpl_coord_exp;                   ///< coupling coord exponents           (cplcoexp)
118
+    uint8_t  **cpl_coord_mant;                  ///< coupling coord mantissas           (cplcomant)
117 119
     uint8_t  coeff_shift[AC3_MAX_CHANNELS];     ///< fixed-point coefficient shift values
118 120
     uint8_t  new_rematrixing_strategy;          ///< send new rematrixing flags in this block
121
+    int      num_rematrixing_bands;             ///< number of rematrixing bands
119 122
     uint8_t  rematrixing_flags[4];              ///< rematrixing flags
120 123
     struct AC3Block *exp_ref_block[AC3_MAX_CHANNELS]; ///< reference blocks for EXP_REUSE
124
+    int      new_cpl_strategy;                  ///< send new coupling strategy
125
+    int      cpl_in_use;                        ///< coupling in use for this block     (cplinu)
126
+    uint8_t  channel_in_cpl[AC3_MAX_CHANNELS];  ///< channel in coupling                (chincpl)
127
+    int      num_cpl_channels;                  ///< number of channels in coupling
128
+    uint8_t  new_cpl_coords;                    ///< send new coupling coordinates      (cplcoe)
129
+    uint8_t  cpl_master_exp[AC3_MAX_CHANNELS];  ///< coupling coord master exponents    (mstrcplco)
130
+    int      new_snr_offsets;                   ///< send new SNR offsets
131
+    int      new_cpl_leak;                      ///< send new coupling leak info
132
+    int      end_freq[AC3_MAX_CHANNELS];        ///< end frequency bin                  (endmant)
121 133
 } AC3Block;
122 134
 
123 135
 /**
... ...
@@ -164,10 +178,16 @@ typedef struct AC3EncodeContext {
164 164
 
165 165
     int cutoff;                             ///< user-specified cutoff frequency, in Hz
166 166
     int bandwidth_code;                     ///< bandwidth code (0 to 60)               (chbwcod)
167
-    int nb_coefs[AC3_MAX_CHANNELS];
167
+    int start_freq[AC3_MAX_CHANNELS];       ///< start frequency bin                    (strtmant)
168
+    int cpl_end_freq;                       ///< coupling channel end frequency bin
169
+
170
+    int cpl_on;                             ///< coupling turned on for this frame
171
+    int cpl_enabled;                        ///< coupling enabled for all frames
172
+    int num_cpl_subbands;                   ///< number of coupling subbands            (ncplsubnd)
173
+    int num_cpl_bands;                      ///< number of coupling bands               (ncplbnd)
174
+    uint8_t cpl_band_sizes[AC3_MAX_CPL_BANDS];  ///< number of coeffs in each coupling band
168 175
 
169 176
     int rematrixing_enabled;                ///< stereo rematrixing enabled
170
-    int num_rematrixing_bands;              ///< number of rematrixing bands
171 177
 
172 178
     /* bitrate allocation control */
173 179
     int slow_gain_code;                     ///< slow gain code                         (sgaincod)
... ...
@@ -194,6 +214,8 @@ typedef struct AC3EncodeContext {
194 194
     int16_t *band_psd_buffer;
195 195
     int16_t *mask_buffer;
196 196
     uint16_t *qmant_buffer;
197
+    uint8_t *cpl_coord_exp_buffer;
198
+    uint8_t *cpl_coord_mant_buffer;
197 199
 
198 200
     uint8_t exp_strategy[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< exponent strategies
199 201
 
... ...
@@ -267,6 +289,12 @@ static const AVOption options[] = {
267 267
     {"hdcd",     "HDCD",               0, FF_OPT_TYPE_CONST, {.dbl = 1 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "ad_conv_type"},
268 268
 /* Other Encoding Options */
269 269
 {"stereo_rematrixing", "Stereo Rematrixing", OFFSET(stereo_rematrixing), FF_OPT_TYPE_INT, {.dbl = 1 }, 0, 1, AC3ENC_PARAM},
270
+#if CONFIG_AC3ENC_FLOAT
271
+{"channel_coupling",   "Channel Coupling",   OFFSET(channel_coupling),   FF_OPT_TYPE_INT, {.dbl = 1 }, 0, 1, AC3ENC_PARAM, "channel_coupling"},
272
+    {"auto", "Selected by the Encoder", 0, FF_OPT_TYPE_CONST, {.dbl = -1 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "channel_coupling"},
273
+{"cpl_start_band", "Coupling Start Band", OFFSET(cpl_start), FF_OPT_TYPE_INT, {.dbl = -1 }, -1, 15, AC3ENC_PARAM, "cpl_start_band"},
274
+    {"auto", "Selected by the Encoder", 0, FF_OPT_TYPE_CONST, {.dbl = -1 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "cpl_start_band"},
275
+#endif
270 276
 {NULL}
271 277
 };
272 278
 
... ...
@@ -296,9 +324,9 @@ static void scale_coefficients(AC3EncodeContext *s);
296 296
 
297 297
 /**
298 298
  * LUT for number of exponent groups.
299
- * exponent_group_tab[exponent strategy-1][number of coefficients]
299
+ * exponent_group_tab[coupling][exponent strategy-1][number of coefficients]
300 300
  */
301
-static uint8_t exponent_group_tab[3][256];
301
+static uint8_t exponent_group_tab[2][3][256];
302 302
 
303 303
 
304 304
 /**
... ...
@@ -358,6 +386,49 @@ static const uint8_t ac3_bandwidth_tab[5][3][19] = {
358 358
 
359 359
 
360 360
 /**
361
+ * LUT to select the coupling start band based on the bit rate, sample rate, and
362
+ * number of full-bandwidth channels. -1 = coupling off
363
+ * ac3_coupling_start_tab[channel_mode-2][sample rate code][bit rate code]
364
+ *
365
+ * TODO: more testing for optimal parameters.
366
+ *       multi-channel tests at 44.1kHz and 32kHz.
367
+ */
368
+static const int8_t ac3_coupling_start_tab[6][3][19] = {
369
+//      32  40  48  56  64  80  96 112 128 160 192 224 256 320 384 448 512 576 640
370
+
371
+    // 2/0
372
+    { {  0,  0,  0,  0,  0,  0,  0,  1,  1,  7,  8, 11, 12, -1, -1, -1, -1, -1, -1 },
373
+      {  0,  0,  0,  0,  0,  0,  1,  3,  5,  7, 10, 12, 13, -1, -1, -1, -1, -1, -1 },
374
+      {  0,  0,  0,  0,  1,  2,  2,  9, 13, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
375
+
376
+    // 3/0
377
+    { {  0,  0,  0,  0,  0,  0,  0,  0,  2,  2,  6,  9, 11, 12, 13, -1, -1, -1, -1 },
378
+      {  0,  0,  0,  0,  0,  0,  0,  0,  2,  2,  6,  9, 11, 12, 13, -1, -1, -1, -1 },
379
+      { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
380
+
381
+    // 2/1 - untested
382
+    { {  0,  0,  0,  0,  0,  0,  0,  0,  2,  2,  6,  9, 11, 12, 13, -1, -1, -1, -1 },
383
+      {  0,  0,  0,  0,  0,  0,  0,  0,  2,  2,  6,  9, 11, 12, 13, -1, -1, -1, -1 },
384
+      { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
385
+
386
+    // 3/1
387
+    { {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  3,  2, 10, 11, 11, 12, 12, 14, -1 },
388
+      {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  3,  2, 10, 11, 11, 12, 12, 14, -1 },
389
+      { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
390
+
391
+    // 2/2 - untested
392
+    { {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  3,  2, 10, 11, 11, 12, 12, 14, -1 },
393
+      {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  3,  2, 10, 11, 11, 12, 12, 14, -1 },
394
+      { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
395
+
396
+    // 3/2
397
+    { {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  6,  8, 11, 12, 12, -1, -1 },
398
+      {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  6,  8, 11, 12, 12, -1, -1 },
399
+      { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
400
+};
401
+
402
+
403
+/**
361 404
  * Adjust the frame size to make the average bit rate match the target bit rate.
362 405
  * This is only needed for 11025, 22050, and 44100 sample rates.
363 406
  */
... ...
@@ -419,15 +490,297 @@ static void apply_mdct(AC3EncodeContext *s)
419 419
 
420 420
             apply_window(&s->dsp, s->windowed_samples, input_samples, s->mdct.window, AC3_WINDOW_SIZE);
421 421
 
422
-            block->coeff_shift[ch] = normalize_samples(s);
422
+            block->coeff_shift[ch+1] = normalize_samples(s);
423 423
 
424
-            s->mdct.fft.mdct_calcw(&s->mdct.fft, block->mdct_coef[ch],
424
+            s->mdct.fft.mdct_calcw(&s->mdct.fft, block->mdct_coef[ch+1],
425 425
                                    s->windowed_samples);
426 426
         }
427 427
     }
428 428
 }
429 429
 
430 430
 
431
+static void compute_coupling_strategy(AC3EncodeContext *s)
432
+{
433
+    int blk, ch;
434
+    int got_cpl_snr;
435
+
436
+    /* set coupling use flags for each block/channel */
437
+    /* TODO: turn coupling on/off and adjust start band based on bit usage */
438
+    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
439
+        AC3Block *block = &s->blocks[blk];
440
+        for (ch = 1; ch <= s->fbw_channels; ch++)
441
+            block->channel_in_cpl[ch] = s->cpl_on;
442
+    }
443
+
444
+    /* enable coupling for each block if at least 2 channels have coupling
445
+       enabled for that block */
446
+    got_cpl_snr = 0;
447
+    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
448
+        AC3Block *block = &s->blocks[blk];
449
+        block->num_cpl_channels = 0;
450
+        for (ch = 1; ch <= s->fbw_channels; ch++)
451
+            block->num_cpl_channels += block->channel_in_cpl[ch];
452
+        block->cpl_in_use = block->num_cpl_channels > 1;
453
+        if (!block->cpl_in_use) {
454
+            block->num_cpl_channels = 0;
455
+            for (ch = 1; ch <= s->fbw_channels; ch++)
456
+                block->channel_in_cpl[ch] = 0;
457
+        }
458
+
459
+        block->new_cpl_strategy = !blk;
460
+        if (blk) {
461
+            for (ch = 1; ch <= s->fbw_channels; ch++) {
462
+                if (block->channel_in_cpl[ch] != s->blocks[blk-1].channel_in_cpl[ch]) {
463
+                    block->new_cpl_strategy = 1;
464
+                    break;
465
+                }
466
+            }
467
+        }
468
+        block->new_cpl_leak = block->new_cpl_strategy;
469
+
470
+        if (!blk || (block->cpl_in_use && !got_cpl_snr)) {
471
+            block->new_snr_offsets = 1;
472
+            if (block->cpl_in_use)
473
+                got_cpl_snr = 1;
474
+        } else {
475
+            block->new_snr_offsets = 0;
476
+        }
477
+    }
478
+
479
+    /* set bandwidth for each channel */
480
+    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
481
+        AC3Block *block = &s->blocks[blk];
482
+        for (ch = 1; ch <= s->fbw_channels; ch++) {
483
+            if (block->channel_in_cpl[ch])
484
+                block->end_freq[ch] = s->start_freq[CPL_CH];
485
+            else
486
+                block->end_freq[ch] = s->bandwidth_code * 3 + 73;
487
+        }
488
+    }
489
+}
490
+
491
+
492
+/**
493
+ * Calculate a single coupling coordinate.
494
+ */
495
+static inline float calc_cpl_coord(float energy_ch, float energy_cpl)
496
+{
497
+    float coord = 0.125;
498
+    if (energy_cpl > 0)
499
+        coord *= sqrtf(energy_ch / energy_cpl);
500
+    return coord;
501
+}
502
+
503
+
504
+/**
505
+ * Calculate coupling channel and coupling coordinates.
506
+ * TODO: Currently this is only used for the floating-point encoder. I was
507
+ *       able to make it work for the fixed-point encoder, but quality was
508
+ *       generally lower in most cases than not using coupling. If a more
509
+ *       adaptive coupling strategy were to be implemented it might be useful
510
+ *       at that time to use coupling for the fixed-point encoder as well.
511
+ */
512
+static void apply_channel_coupling(AC3EncodeContext *s)
513
+{
514
+#if CONFIG_AC3ENC_FLOAT
515
+    DECLARE_ALIGNED(16, float,   cpl_coords)      [AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][16];
516
+    DECLARE_ALIGNED(16, int32_t, fixed_cpl_coords)[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][16];
517
+    int blk, ch, bnd, i, j;
518
+    CoefSumType energy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][16] = {{{0}}};
519
+    int num_cpl_coefs = s->num_cpl_subbands * 12;
520
+
521
+    /* calculate coupling channel from fbw channels */
522
+    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
523
+        AC3Block *block = &s->blocks[blk];
524
+        CoefType *cpl_coef = &block->mdct_coef[CPL_CH][s->start_freq[CPL_CH]];
525
+        if (!block->cpl_in_use)
526
+            continue;
527
+        memset(cpl_coef-1, 0, (num_cpl_coefs+4) * sizeof(*cpl_coef));
528
+        for (ch = 1; ch <= s->fbw_channels; ch++) {
529
+            CoefType *ch_coef = &block->mdct_coef[ch][s->start_freq[CPL_CH]];
530
+            if (!block->channel_in_cpl[ch])
531
+                continue;
532
+            for (i = 0; i < num_cpl_coefs; i++)
533
+                cpl_coef[i] += ch_coef[i];
534
+        }
535
+        /* note: coupling start bin % 4 will always be 1 and num_cpl_coefs
536
+                 will always be a multiple of 12, so we need to subtract 1 from
537
+                 the start and add 4 to the length when using optimized
538
+                 functions which require 16-byte alignment. */
539
+
540
+        /* coefficients must be clipped to +/- 1.0 in order to be encoded */
541
+        s->dsp.vector_clipf(cpl_coef-1, cpl_coef-1, -1.0f, 1.0f, num_cpl_coefs+4);
542
+
543
+        /* scale coupling coefficients from float to 24-bit fixed-point */
544
+        s->ac3dsp.float_to_fixed24(&block->fixed_coef[CPL_CH][s->start_freq[CPL_CH]-1],
545
+                                   cpl_coef-1, num_cpl_coefs+4);
546
+    }
547
+
548
+    /* calculate energy in each band in coupling channel and each fbw channel */
549
+    /* TODO: possibly use SIMD to speed up energy calculation */
550
+    bnd = 0;
551
+    i = s->start_freq[CPL_CH];
552
+    while (i < s->cpl_end_freq) {
553
+        int band_size = s->cpl_band_sizes[bnd];
554
+        for (ch = CPL_CH; ch <= s->fbw_channels; ch++) {
555
+            for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
556
+                AC3Block *block = &s->blocks[blk];
557
+                if (!block->cpl_in_use || (ch > CPL_CH && !block->channel_in_cpl[ch]))
558
+                    continue;
559
+                for (j = 0; j < band_size; j++) {
560
+                    CoefType v = block->mdct_coef[ch][i+j];
561
+                    MAC_COEF(energy[blk][ch][bnd], v, v);
562
+                }
563
+            }
564
+        }
565
+        i += band_size;
566
+        bnd++;
567
+    }
568
+
569
+    /* determine which blocks to send new coupling coordinates for */
570
+    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
571
+        AC3Block *block  = &s->blocks[blk];
572
+        AC3Block *block0 = blk ? &s->blocks[blk-1] : NULL;
573
+        int new_coords = 0;
574
+        CoefSumType coord_diff[AC3_MAX_CHANNELS] = {0,};
575
+
576
+        if (block->cpl_in_use) {
577
+            /* calculate coupling coordinates for all blocks and calculate the
578
+               average difference between coordinates in successive blocks */
579
+            for (ch = 1; ch <= s->fbw_channels; ch++) {
580
+                if (!block->channel_in_cpl[ch])
581
+                    continue;
582
+
583
+                for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
584
+                    cpl_coords[blk][ch][bnd] = calc_cpl_coord(energy[blk][ch][bnd],
585
+                                                              energy[blk][CPL_CH][bnd]);
586
+                    if (blk > 0 && block0->cpl_in_use &&
587
+                        block0->channel_in_cpl[ch]) {
588
+                        coord_diff[ch] += fabs(cpl_coords[blk-1][ch][bnd] -
589
+                                               cpl_coords[blk  ][ch][bnd]);
590
+                    }
591
+                }
592
+                coord_diff[ch] /= s->num_cpl_bands;
593
+            }
594
+
595
+            /* send new coordinates if this is the first block, if previous
596
+             * block did not use coupling but this block does, the channels
597
+             * using coupling has changed from the previous block, or the
598
+             * coordinate difference from the last block for any channel is
599
+             * greater than a threshold value. */
600
+            if (blk == 0) {
601
+                new_coords = 1;
602
+            } else if (!block0->cpl_in_use) {
603
+                new_coords = 1;
604
+            } else {
605
+                for (ch = 1; ch <= s->fbw_channels; ch++) {
606
+                    if (block->channel_in_cpl[ch] && !block0->channel_in_cpl[ch]) {
607
+                        new_coords = 1;
608
+                        break;
609
+                    }
610
+                }
611
+                if (!new_coords) {
612
+                    for (ch = 1; ch <= s->fbw_channels; ch++) {
613
+                        if (block->channel_in_cpl[ch] && coord_diff[ch] > 0.04) {
614
+                            new_coords = 1;
615
+                            break;
616
+                        }
617
+                    }
618
+                }
619
+            }
620
+        }
621
+        block->new_cpl_coords = new_coords;
622
+    }
623
+
624
+    /* calculate final coupling coordinates, taking into account reusing of
625
+       coordinates in successive blocks */
626
+    for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
627
+        blk = 0;
628
+        while (blk < AC3_MAX_BLOCKS) {
629
+            int blk1;
630
+            CoefSumType energy_cpl;
631
+            AC3Block *block  = &s->blocks[blk];
632
+
633
+            if (!block->cpl_in_use) {
634
+                blk++;
635
+                continue;
636
+            }
637
+
638
+            energy_cpl = energy[blk][CPL_CH][bnd];
639
+            blk1 = blk+1;
640
+            while (!s->blocks[blk1].new_cpl_coords && blk1 < AC3_MAX_BLOCKS) {
641
+                if (s->blocks[blk1].cpl_in_use)
642
+                    energy_cpl += energy[blk1][CPL_CH][bnd];
643
+                blk1++;
644
+            }
645
+
646
+            for (ch = 1; ch <= s->fbw_channels; ch++) {
647
+                CoefType energy_ch;
648
+                if (!block->channel_in_cpl[ch])
649
+                    continue;
650
+                energy_ch = energy[blk][ch][bnd];
651
+                blk1 = blk+1;
652
+                while (!s->blocks[blk1].new_cpl_coords && blk1 < AC3_MAX_BLOCKS) {
653
+                    if (s->blocks[blk1].cpl_in_use)
654
+                        energy_ch += energy[blk1][ch][bnd];
655
+                    blk1++;
656
+                }
657
+                cpl_coords[blk][ch][bnd] = calc_cpl_coord(energy_ch, energy_cpl);
658
+            }
659
+            blk = blk1;
660
+        }
661
+    }
662
+
663
+    /* calculate exponents/mantissas for coupling coordinates */
664
+    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
665
+        AC3Block *block = &s->blocks[blk];
666
+        if (!block->cpl_in_use || !block->new_cpl_coords)
667
+            continue;
668
+
669
+        s->ac3dsp.float_to_fixed24(fixed_cpl_coords[blk][1],
670
+                                   cpl_coords[blk][1],
671
+                                   s->fbw_channels * 16);
672
+        s->ac3dsp.extract_exponents(block->cpl_coord_exp[1],
673
+                                    fixed_cpl_coords[blk][1],
674
+                                    s->fbw_channels * 16);
675
+
676
+        for (ch = 1; ch <= s->fbw_channels; ch++) {
677
+            int bnd, min_exp, max_exp, master_exp;
678
+
679
+            /* determine master exponent */
680
+            min_exp = max_exp = block->cpl_coord_exp[ch][0];
681
+            for (bnd = 1; bnd < s->num_cpl_bands; bnd++) {
682
+                int exp = block->cpl_coord_exp[ch][bnd];
683
+                min_exp = FFMIN(exp, min_exp);
684
+                max_exp = FFMAX(exp, max_exp);
685
+            }
686
+            master_exp = ((max_exp - 15) + 2) / 3;
687
+            master_exp = FFMAX(master_exp, 0);
688
+            while (min_exp < master_exp * 3)
689
+                master_exp--;
690
+            for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
691
+                block->cpl_coord_exp[ch][bnd] = av_clip(block->cpl_coord_exp[ch][bnd] -
692
+                                                        master_exp * 3, 0, 15);
693
+            }
694
+            block->cpl_master_exp[ch] = master_exp;
695
+
696
+            /* quantize mantissas */
697
+            for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
698
+                int cpl_exp  = block->cpl_coord_exp[ch][bnd];
699
+                int cpl_mant = (fixed_cpl_coords[blk][ch][bnd] << (5 + cpl_exp + master_exp * 3)) >> 24;
700
+                if (cpl_exp == 15)
701
+                    cpl_mant >>= 1;
702
+                else
703
+                    cpl_mant -= 16;
704
+
705
+                block->cpl_coord_mant[ch][bnd] = cpl_mant;
706
+            }
707
+        }
708
+    }
709
+#endif /* CONFIG_AC3ENC_FLOAT */
710
+}
711
+
712
+
431 713
 /**
432 714
  * Determine rematrixing flags for each block and band.
433 715
  */
... ...
@@ -440,23 +793,32 @@ static void compute_rematrixing_strategy(AC3EncodeContext *s)
440 440
     if (s->channel_mode != AC3_CHMODE_STEREO)
441 441
         return;
442 442
 
443
-    s->num_rematrixing_bands = 4;
444
-
445
-    nb_coefs = FFMIN(s->nb_coefs[0], s->nb_coefs[1]);
446
-
447 443
     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
448 444
         block = &s->blocks[blk];
449 445
         block->new_rematrixing_strategy = !blk;
450
-        if (!s->rematrixing_enabled)
446
+
447
+        if (!s->rematrixing_enabled) {
448
+            block0 = block;
451 449
             continue;
452
-        for (bnd = 0; bnd < s->num_rematrixing_bands; bnd++) {
450
+        }
451
+
452
+        block->num_rematrixing_bands = 4;
453
+        if (block->cpl_in_use) {
454
+            block->num_rematrixing_bands -= (s->start_freq[CPL_CH] <= 61);
455
+            block->num_rematrixing_bands -= (s->start_freq[CPL_CH] == 37);
456
+            if (blk && block->num_rematrixing_bands != block0->num_rematrixing_bands)
457
+                block->new_rematrixing_strategy = 1;
458
+        }
459
+        nb_coefs = FFMIN(block->end_freq[1], block->end_freq[2]);
460
+
461
+        for (bnd = 0; bnd < block->num_rematrixing_bands; bnd++) {
453 462
             /* calculate calculate sum of squared coeffs for one band in one block */
454 463
             int start = ff_ac3_rematrix_band_tab[bnd];
455 464
             int end   = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]);
456 465
             CoefSumType sum[4] = {0,};
457 466
             for (i = start; i < end; i++) {
458
-                CoefType lt = block->mdct_coef[0][i];
459
-                CoefType rt = block->mdct_coef[1][i];
467
+                CoefType lt = block->mdct_coef[1][i];
468
+                CoefType rt = block->mdct_coef[2][i];
460 469
                 CoefType md = lt + rt;
461 470
                 CoefType sd = lt - rt;
462 471
                 MAC_COEF(sum[0], lt, lt);
... ...
@@ -495,21 +857,20 @@ static void apply_rematrixing(AC3EncodeContext *s)
495 495
     if (!s->rematrixing_enabled)
496 496
         return;
497 497
 
498
-    nb_coefs = FFMIN(s->nb_coefs[0], s->nb_coefs[1]);
499
-
500 498
     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
501 499
         AC3Block *block = &s->blocks[blk];
502 500
         if (block->new_rematrixing_strategy)
503 501
             flags = block->rematrixing_flags;
504
-        for (bnd = 0; bnd < s->num_rematrixing_bands; bnd++) {
502
+        nb_coefs = FFMIN(block->end_freq[1], block->end_freq[2]);
503
+        for (bnd = 0; bnd < block->num_rematrixing_bands; bnd++) {
505 504
             if (flags[bnd]) {
506 505
                 start = ff_ac3_rematrix_band_tab[bnd];
507 506
                 end   = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]);
508 507
                 for (i = start; i < end; i++) {
509
-                    int32_t lt = block->fixed_coef[0][i];
510
-                    int32_t rt = block->fixed_coef[1][i];
511
-                    block->fixed_coef[0][i] = (lt + rt) >> 1;
512
-                    block->fixed_coef[1][i] = (lt - rt) >> 1;
508
+                    int32_t lt = block->fixed_coef[1][i];
509
+                    int32_t rt = block->fixed_coef[2][i];
510
+                    block->fixed_coef[1][i] = (lt + rt) >> 1;
511
+                    block->fixed_coef[2][i] = (lt - rt) >> 1;
513 512
                 }
514 513
             }
515 514
         }
... ...
@@ -526,12 +887,13 @@ static av_cold void exponent_init(AC3EncodeContext *s)
526 526
 
527 527
     for (expstr = EXP_D15-1; expstr <= EXP_D45-1; expstr++) {
528 528
         grpsize = 3 << expstr;
529
-        for (i = 73; i < 256; i++) {
530
-            exponent_group_tab[expstr][i] = (i + grpsize - 4) / grpsize;
529
+        for (i = 12; i < 256; i++) {
530
+            exponent_group_tab[0][expstr][i] = (i + grpsize - 4) / grpsize;
531
+            exponent_group_tab[1][expstr][i] = (i              ) / grpsize;
531 532
         }
532 533
     }
533 534
     /* LFE */
534
-    exponent_group_tab[0][7] = 2;
535
+    exponent_group_tab[0][0][7] = 2;
535 536
 }
536 537
 
537 538
 
... ...
@@ -544,7 +906,7 @@ static void extract_exponents(AC3EncodeContext *s)
544 544
 {
545 545
     int blk, ch;
546 546
 
547
-    for (ch = 0; ch < s->channels; ch++) {
547
+    for (ch = !s->cpl_on; ch <= s->channels; ch++) {
548 548
         for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
549 549
             AC3Block *block = &s->blocks[blk];
550 550
             s->ac3dsp.extract_exponents(block->exp[ch], block->fixed_coef[ch],
... ...
@@ -569,7 +931,7 @@ static void compute_exp_strategy(AC3EncodeContext *s)
569 569
 {
570 570
     int ch, blk, blk1;
571 571
 
572
-    for (ch = 0; ch < s->fbw_channels; ch++) {
572
+    for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++) {
573 573
         uint8_t *exp_strategy = s->exp_strategy[ch];
574 574
         uint8_t *exp          = s->blocks[0].exp[ch];
575 575
         int exp_diff;
... ...
@@ -578,13 +940,18 @@ static void compute_exp_strategy(AC3EncodeContext *s)
578 578
            reused in the next frame */
579 579
         exp_strategy[0] = EXP_NEW;
580 580
         exp += AC3_MAX_COEFS;
581
-        for (blk = 1; blk < AC3_MAX_BLOCKS; blk++) {
581
+        for (blk = 1; blk < AC3_MAX_BLOCKS; blk++, exp += AC3_MAX_COEFS) {
582
+            if ((ch == CPL_CH && (!s->blocks[blk].cpl_in_use || !s->blocks[blk-1].cpl_in_use)) ||
583
+                (ch  > CPL_CH && (s->blocks[blk].channel_in_cpl[ch] != s->blocks[blk-1].channel_in_cpl[ch]))) {
584
+                exp_strategy[blk] = EXP_NEW;
585
+                continue;
586
+            }
582 587
             exp_diff = s->dsp.sad[0](NULL, exp, exp - AC3_MAX_COEFS, 16, 16);
583
-            if (exp_diff > EXP_DIFF_THRESHOLD)
588
+            exp_strategy[blk] = EXP_REUSE;
589
+            if (ch == CPL_CH && exp_diff > (EXP_DIFF_THRESHOLD * (s->blocks[blk].end_freq[ch] - s->start_freq[ch]) / AC3_MAX_COEFS))
590
+                exp_strategy[blk] = EXP_NEW;
591
+            else if (ch > CPL_CH && exp_diff > EXP_DIFF_THRESHOLD)
584 592
                 exp_strategy[blk] = EXP_NEW;
585
-            else
586
-                exp_strategy[blk] = EXP_REUSE;
587
-            exp += AC3_MAX_COEFS;
588 593
         }
589 594
 
590 595
         /* now select the encoding strategy type : if exponents are often
... ...
@@ -615,25 +982,26 @@ static void compute_exp_strategy(AC3EncodeContext *s)
615 615
 /**
616 616
  * Update the exponents so that they are the ones the decoder will decode.
617 617
  */
618
-static void encode_exponents_blk_ch(uint8_t *exp, int nb_exps, int exp_strategy)
618
+static void encode_exponents_blk_ch(uint8_t *exp, int nb_exps, int exp_strategy,
619
+                                    int cpl)
619 620
 {
620 621
     int nb_groups, i, k;
621 622
 
622
-    nb_groups = exponent_group_tab[exp_strategy-1][nb_exps] * 3;
623
+    nb_groups = exponent_group_tab[cpl][exp_strategy-1][nb_exps] * 3;
623 624
 
624 625
     /* for each group, compute the minimum exponent */
625 626
     switch(exp_strategy) {
626 627
     case EXP_D25:
627
-        for (i = 1, k = 1; i <= nb_groups; i++) {
628
+        for (i = 1, k = 1-cpl; i <= nb_groups; i++) {
628 629
             uint8_t exp_min = exp[k];
629 630
             if (exp[k+1] < exp_min)
630 631
                 exp_min = exp[k+1];
631
-            exp[i] = exp_min;
632
+            exp[i-cpl] = exp_min;
632 633
             k += 2;
633 634
         }
634 635
         break;
635 636
     case EXP_D45:
636
-        for (i = 1, k = 1; i <= nb_groups; i++) {
637
+        for (i = 1, k = 1-cpl; i <= nb_groups; i++) {
637 638
             uint8_t exp_min = exp[k];
638 639
             if (exp[k+1] < exp_min)
639 640
                 exp_min = exp[k+1];
... ...
@@ -641,14 +1009,14 @@ static void encode_exponents_blk_ch(uint8_t *exp, int nb_exps, int exp_strategy)
641 641
                 exp_min = exp[k+2];
642 642
             if (exp[k+3] < exp_min)
643 643
                 exp_min = exp[k+3];
644
-            exp[i] = exp_min;
644
+            exp[i-cpl] = exp_min;
645 645
             k += 4;
646 646
         }
647 647
         break;
648 648
     }
649 649
 
650 650
     /* constraint for DC exponent */
651
-    if (exp[0] > 15)
651
+    if (!cpl && exp[0] > 15)
652 652
         exp[0] = 15;
653 653
 
654 654
     /* decrease the delta between each groups to within 2 so that they can be
... ...
@@ -659,18 +1027,21 @@ static void encode_exponents_blk_ch(uint8_t *exp, int nb_exps, int exp_strategy)
659 659
     while (--i >= 0)
660 660
         exp[i] = FFMIN(exp[i], exp[i+1] + 2);
661 661
 
662
+    if (cpl)
663
+        exp[-1] = exp[0] & ~1;
664
+
662 665
     /* now we have the exponent values the decoder will see */
663 666
     switch (exp_strategy) {
664 667
     case EXP_D25:
665
-        for (i = nb_groups, k = nb_groups * 2; i > 0; i--) {
666
-            uint8_t exp1 = exp[i];
668
+        for (i = nb_groups, k = (nb_groups * 2)-cpl; i > 0; i--) {
669
+            uint8_t exp1 = exp[i-cpl];
667 670
             exp[k--] = exp1;
668 671
             exp[k--] = exp1;
669 672
         }
670 673
         break;
671 674
     case EXP_D45:
672
-        for (i = nb_groups, k = nb_groups * 4; i > 0; i--) {
673
-            exp[k] = exp[k-1] = exp[k-2] = exp[k-3] = exp[i];
675
+        for (i = nb_groups, k = (nb_groups * 4)-cpl; i > 0; i--) {
676
+            exp[k] = exp[k-1] = exp[k-2] = exp[k-3] = exp[i-cpl];
674 677
             k -= 4;
675 678
         }
676 679
         break;
... ...
@@ -686,32 +1057,40 @@ static void encode_exponents_blk_ch(uint8_t *exp, int nb_exps, int exp_strategy)
686 686
  */
687 687
 static void encode_exponents(AC3EncodeContext *s)
688 688
 {
689
-    int blk, blk1, ch;
689
+    int blk, blk1, ch, cpl;
690 690
     uint8_t *exp, *exp_strategy;
691 691
     int nb_coefs, num_reuse_blocks;
692 692
 
693
-    for (ch = 0; ch < s->channels; ch++) {
694
-        exp          = s->blocks[0].exp[ch];
693
+    for (ch = !s->cpl_on; ch <= s->channels; ch++) {
694
+        exp          = s->blocks[0].exp[ch] + s->start_freq[ch];
695 695
         exp_strategy = s->exp_strategy[ch];
696
-        nb_coefs     = s->nb_coefs[ch];
697 696
 
697
+        cpl = (ch == CPL_CH);
698 698
         blk = 0;
699 699
         while (blk < AC3_MAX_BLOCKS) {
700
+            AC3Block *block = &s->blocks[blk];
701
+            if (cpl && !block->cpl_in_use) {
702
+                exp += AC3_MAX_COEFS;
703
+                blk++;
704
+                continue;
705
+            }
706
+            nb_coefs = block->end_freq[ch] - s->start_freq[ch];
700 707
             blk1 = blk + 1;
701 708
 
702 709
             /* count the number of EXP_REUSE blocks after the current block
703 710
                and set exponent reference block pointers */
704
-            s->blocks[blk].exp_ref_block[ch] = &s->blocks[blk];
711
+            block->exp_ref_block[ch] = block;
705 712
             while (blk1 < AC3_MAX_BLOCKS && exp_strategy[blk1] == EXP_REUSE) {
706
-                s->blocks[blk1].exp_ref_block[ch] = &s->blocks[blk];
713
+                s->blocks[blk1].exp_ref_block[ch] = block;
707 714
                 blk1++;
708 715
             }
709 716
             num_reuse_blocks = blk1 - blk - 1;
710 717
 
711 718
             /* for the EXP_REUSE case we select the min of the exponents */
712
-            s->ac3dsp.ac3_exponent_min(exp, num_reuse_blocks, nb_coefs);
719
+            s->ac3dsp.ac3_exponent_min(exp-s->start_freq[ch], num_reuse_blocks,
720
+                                       AC3_MAX_COEFS);
713 721
 
714
-            encode_exponents_blk_ch(exp, nb_coefs, exp_strategy[blk]);
722
+            encode_exponents_blk_ch(exp, nb_coefs, exp_strategy[blk], cpl);
715 723
 
716 724
             exp += AC3_MAX_COEFS * (num_reuse_blocks + 1);
717 725
             blk = blk1;
... ...
@@ -727,7 +1106,7 @@ static void encode_exponents(AC3EncodeContext *s)
727 727
  */
728 728
 static void group_exponents(AC3EncodeContext *s)
729 729
 {
730
-    int blk, ch, i;
730
+    int blk, ch, i, cpl;
731 731
     int group_size, nb_groups, bit_count;
732 732
     uint8_t *p;
733 733
     int delta0, delta1, delta2;
... ...
@@ -736,14 +1115,15 @@ static void group_exponents(AC3EncodeContext *s)
736 736
     bit_count = 0;
737 737
     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
738 738
         AC3Block *block = &s->blocks[blk];
739
-        for (ch = 0; ch < s->channels; ch++) {
739
+        for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
740 740
             int exp_strategy = s->exp_strategy[ch][blk];
741 741
             if (exp_strategy == EXP_REUSE)
742 742
                 continue;
743
+            cpl = (ch == CPL_CH);
743 744
             group_size = exp_strategy + (exp_strategy == EXP_D45);
744
-            nb_groups = exponent_group_tab[exp_strategy-1][s->nb_coefs[ch]];
745
+            nb_groups = exponent_group_tab[cpl][exp_strategy-1][block->end_freq[ch]-s->start_freq[ch]];
745 746
             bit_count += 4 + (nb_groups * 7);
746
-            p = block->exp[ch];
747
+            p = block->exp[ch] + s->start_freq[ch] - cpl;
747 748
 
748 749
             /* DC exponent */
749 750
             exp1 = *p++;
... ...
@@ -810,9 +1190,7 @@ static void count_frame_bits_fixed(AC3EncodeContext *s)
810 810
 
811 811
     /* assumptions:
812 812
      *   no dynamic range codes
813
-     *   no channel coupling
814 813
      *   bit allocation parameters do not change between blocks
815
-     *   SNR offsets do not change between blocks
816 814
      *   no delta bit allocation
817 815
      *   no skipped data
818 816
      *   no auxilliary data
... ...
@@ -833,11 +1211,6 @@ static void count_frame_bits_fixed(AC3EncodeContext *s)
833 833
         /* dynamic range */
834 834
         frame_bits++;
835 835
 
836
-        /* coupling strategy */
837
-        frame_bits++;
838
-        if (!blk)
839
-            frame_bits++;
840
-
841 836
         /* exponent strategy */
842 837
         frame_bits += 2 * s->fbw_channels;
843 838
         if (s->lfe_on)
... ...
@@ -848,11 +1221,6 @@ static void count_frame_bits_fixed(AC3EncodeContext *s)
848 848
         if (!blk)
849 849
             frame_bits += 2 + 2 + 2 + 2 + 3;
850 850
 
851
-        /* snr offsets and fast gain codes */
852
-        frame_bits++;
853
-        if (!blk)
854
-            frame_bits += 6 + s->channels * (4 + 3);
855
-
856 851
         /* delta bit allocation */
857 852
         frame_bits++;
858 853
 
... ...
@@ -884,7 +1252,7 @@ static void bit_alloc_init(AC3EncodeContext *s)
884 884
     s->slow_gain_code  = 1;
885 885
     s->db_per_bit_code = 3;
886 886
     s->floor_code      = 7;
887
-    for (ch = 0; ch < s->channels; ch++)
887
+    for (ch = 0; ch <= s->channels; ch++)
888 888
         s->fast_gain_code[ch] = 4;
889 889
 
890 890
     /* initial snr offset */
... ...
@@ -898,6 +1266,8 @@ static void bit_alloc_init(AC3EncodeContext *s)
898 898
     s->bit_alloc.slow_gain  = ff_ac3_slow_gain_tab[s->slow_gain_code];
899 899
     s->bit_alloc.db_per_bit = ff_ac3_db_per_bit_tab[s->db_per_bit_code];
900 900
     s->bit_alloc.floor      = ff_ac3_floor_tab[s->floor_code];
901
+    s->bit_alloc.cpl_fast_leak = 0;
902
+    s->bit_alloc.cpl_slow_leak = 0;
901 903
 
902 904
     count_frame_bits_fixed(s);
903 905
 }
... ...
@@ -926,17 +1296,64 @@ static void count_frame_bits(AC3EncodeContext *s)
926 926
 
927 927
     /* audio blocks */
928 928
     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
929
+        AC3Block *block = &s->blocks[blk];
930
+
931
+        /* coupling strategy */
932
+        frame_bits++;
933
+        if (block->new_cpl_strategy) {
934
+            frame_bits++;
935
+            if (block->cpl_in_use) {
936
+                frame_bits += s->fbw_channels;
937
+                if (s->channel_mode == AC3_CHMODE_STEREO)
938
+                    frame_bits++;
939
+                frame_bits += 4 + 4;
940
+                frame_bits += s->num_cpl_subbands - 1;
941
+            }
942
+        }
943
+
944
+        /* coupling coordinates */
945
+        if (block->cpl_in_use) {
946
+            for (ch = 1; ch <= s->fbw_channels; ch++) {
947
+                if (block->channel_in_cpl[ch]) {
948
+                    frame_bits++;
949
+                    if (block->new_cpl_coords) {
950
+                        frame_bits += 2;
951
+                        frame_bits += (4 + 4) * s->num_cpl_bands;
952
+                    }
953
+                }
954
+            }
955
+        }
956
+
929 957
         /* stereo rematrixing */
930 958
         if (s->channel_mode == AC3_CHMODE_STEREO) {
931 959
             frame_bits++;
932 960
             if (s->blocks[blk].new_rematrixing_strategy)
933
-                frame_bits += s->num_rematrixing_bands;
961
+                frame_bits += block->num_rematrixing_bands;
934 962
         }
935 963
 
936 964
         /* bandwidth codes & gain range */
937
-        for (ch = 0; ch < s->fbw_channels; ch++) {
938
-            if (s->exp_strategy[ch][blk] != EXP_REUSE)
939
-                frame_bits += 6 + 2;
965
+        for (ch = 1; ch <= s->fbw_channels; ch++) {
966
+            if (s->exp_strategy[ch][blk] != EXP_REUSE) {
967
+                if (!block->channel_in_cpl[ch])
968
+                    frame_bits += 6;
969
+                frame_bits += 2;
970
+            }
971
+        }
972
+
973
+        /* coupling exponent strategy */
974
+        if (block->cpl_in_use)
975
+            frame_bits += 2;
976
+
977
+        /* snr offsets and fast gain codes */
978
+        frame_bits++;
979
+        if (block->new_snr_offsets)
980
+            frame_bits += 6 + (s->channels + block->cpl_in_use) * (4 + 3);
981
+
982
+        /* coupling leak info */
983
+        if (block->cpl_in_use) {
984
+            frame_bits++;
985
+            if (block->new_cpl_leak)
986
+                frame_bits += 3 + 3;
940 987
         }
941 988
     }
942 989
 
... ...
@@ -970,16 +1387,16 @@ static void bit_alloc_masking(AC3EncodeContext *s)
970 970
 
971 971
     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
972 972
         AC3Block *block = &s->blocks[blk];
973
-        for (ch = 0; ch < s->channels; ch++) {
973
+        for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
974 974
             /* We only need psd and mask for calculating bap.
975 975
                Since we currently do not calculate bap when exponent
976 976
                strategy is EXP_REUSE we do not need to calculate psd or mask. */
977 977
             if (s->exp_strategy[ch][blk] != EXP_REUSE) {
978
-                ff_ac3_bit_alloc_calc_psd(block->exp[ch], 0,
979
-                                          s->nb_coefs[ch],
980
-                                          block->psd[ch], block->band_psd[ch]);
978
+                ff_ac3_bit_alloc_calc_psd(block->exp[ch], s->start_freq[ch],
979
+                                          block->end_freq[ch], block->psd[ch],
980
+                                          block->band_psd[ch]);
981 981
                 ff_ac3_bit_alloc_calc_mask(&s->bit_alloc, block->band_psd[ch],
982
-                                           0, s->nb_coefs[ch],
982
+                                           s->start_freq[ch], block->end_freq[ch],
983 983
                                            ff_ac3_fast_gain_tab[s->fast_gain_code[ch]],
984 984
                                            ch == s->lfe_channel,
985 985
                                            DBA_NONE, 0, NULL, NULL, NULL,
... ...
@@ -997,11 +1414,12 @@ static void bit_alloc_masking(AC3EncodeContext *s)
997 997
 static void reset_block_bap(AC3EncodeContext *s)
998 998
 {
999 999
     int blk, ch;
1000
+    int channels = s->channels + 1;
1000 1001
     if (s->blocks[0].bap[0] == s->bap_buffer)
1001 1002
         return;
1002 1003
     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1003
-        for (ch = 0; ch < s->channels; ch++) {
1004
-            s->blocks[blk].bap[ch] = &s->bap_buffer[AC3_MAX_COEFS * (blk * s->channels + ch)];
1004
+        for (ch = 0; ch < channels; ch++) {
1005
+            s->blocks[blk].bap[ch] = &s->bap_buffer[AC3_MAX_COEFS * (blk * channels + ch)];
1005 1006
         }
1006 1007
     }
1007 1008
 }
... ...
@@ -1027,28 +1445,37 @@ static int bit_alloc(AC3EncodeContext *s, int snr_offset)
1027 1027
     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1028 1028
         AC3Block *block = &s->blocks[blk];
1029 1029
         AC3Block *ref_block;
1030
+        int av_uninit(ch0);
1031
+        int got_cpl = !block->cpl_in_use;
1030 1032
         // initialize grouped mantissa counts. these are set so that they are
1031 1033
         // padded to the next whole group size when bits are counted in
1032 1034
         // compute_mantissa_size_final
1033 1035
         mant_cnt[0] = mant_cnt[3] = 0;
1034 1036
         mant_cnt[1] = mant_cnt[2] = 2;
1035 1037
         mant_cnt[4] = 1;
1036
-        for (ch = 0; ch < s->channels; ch++) {
1038
+        for (ch = 1; ch <= s->channels; ch++) {
1039
+            if (!got_cpl && ch > 1 && block->channel_in_cpl[ch-1]) {
1040
+                ch0     = ch - 1;
1041
+                ch      = CPL_CH;
1042
+                got_cpl = 1;
1043
+            }
1044
+
1037 1045
             /* Currently the only bit allocation parameters which vary across
1038 1046
                blocks within a frame are the exponent values.  We can take
1039 1047
                advantage of that by reusing the bit allocation pointers
1040 1048
                whenever we reuse exponents. */
1041 1049
             ref_block = block->exp_ref_block[ch];
1042 1050
             if (s->exp_strategy[ch][blk] != EXP_REUSE) {
1043
-                s->ac3dsp.bit_alloc_calc_bap(ref_block->mask[ch],
1044
-                                             ref_block->psd[ch], 0,
1045
-                                             s->nb_coefs[ch], snr_offset,
1046
-                                             s->bit_alloc.floor, ff_ac3_bap_tab,
1047
-                                             ref_block->bap[ch]);
1051
+                s->ac3dsp.bit_alloc_calc_bap(ref_block->mask[ch], ref_block->psd[ch],
1052
+                                             s->start_freq[ch], block->end_freq[ch],
1053
+                                             snr_offset, s->bit_alloc.floor,
1054
+                                             ff_ac3_bap_tab, ref_block->bap[ch]);
1048 1055
             }
1049 1056
             mantissa_bits += s->ac3dsp.compute_mantissa_size(mant_cnt,
1050
-                                                             ref_block->bap[ch],
1051
-                                                             s->nb_coefs[ch]);
1057
+                                                             ref_block->bap[ch]+s->start_freq[ch],
1058
+                                                             block->end_freq[ch]-s->start_freq[ch]);
1059
+            if (ch == CPL_CH)
1060
+                ch = ch0;
1052 1061
         }
1053 1062
         mantissa_bits += compute_mantissa_size_final(mant_cnt);
1054 1063
     }
... ...
@@ -1074,7 +1501,7 @@ static int cbr_bit_allocation(AC3EncodeContext *s)
1074 1074
 
1075 1075
     /* if previous frame SNR offset was 1023, check if current frame can also
1076 1076
        use SNR offset of 1023. if so, skip the search. */
1077
-    if ((snr_offset | s->fine_snr_offset[0]) == 1023) {
1077
+    if ((snr_offset | s->fine_snr_offset[1]) == 1023) {
1078 1078
         if (bit_alloc(s, 1023) <= bits_left)
1079 1079
             return 0;
1080 1080
     }
... ...
@@ -1098,7 +1525,7 @@ static int cbr_bit_allocation(AC3EncodeContext *s)
1098 1098
     reset_block_bap(s);
1099 1099
 
1100 1100
     s->coarse_snr_offset = snr_offset >> 4;
1101
-    for (ch = 0; ch < s->channels; ch++)
1101
+    for (ch = !s->cpl_on; ch <= s->channels; ch++)
1102 1102
         s->fine_snr_offset[ch] = snr_offset & 0xF;
1103 1103
 
1104 1104
     return 0;
... ...
@@ -1116,26 +1543,26 @@ static int downgrade_exponents(AC3EncodeContext *s)
1116 1116
 {
1117 1117
     int ch, blk;
1118 1118
 
1119
-    for (ch = 0; ch < s->fbw_channels; ch++) {
1120
-        for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) {
1119
+    for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) {
1120
+        for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++) {
1121 1121
             if (s->exp_strategy[ch][blk] == EXP_D15) {
1122 1122
                 s->exp_strategy[ch][blk] = EXP_D25;
1123 1123
                 return 0;
1124 1124
             }
1125 1125
         }
1126 1126
     }
1127
-    for (ch = 0; ch < s->fbw_channels; ch++) {
1128
-        for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) {
1127
+    for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) {
1128
+        for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++) {
1129 1129
             if (s->exp_strategy[ch][blk] == EXP_D25) {
1130 1130
                 s->exp_strategy[ch][blk] = EXP_D45;
1131 1131
                 return 0;
1132 1132
             }
1133 1133
         }
1134 1134
     }
1135
-    for (ch = 0; ch < s->fbw_channels; ch++) {
1136
-        /* block 0 cannot reuse exponents, so only downgrade D45 to REUSE if
1137
-           the block number > 0 */
1138
-        for (blk = AC3_MAX_BLOCKS-1; blk > 0; blk--) {
1135
+    /* block 0 cannot reuse exponents, so only downgrade D45 to REUSE if
1136
+       the block number > 0 */
1137
+    for (blk = AC3_MAX_BLOCKS-1; blk > 0; blk--) {
1138
+        for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++) {
1139 1139
             if (s->exp_strategy[ch][blk] > EXP_REUSE) {
1140 1140
                 s->exp_strategy[ch][blk] = EXP_REUSE;
1141 1141
                 return 0;
... ...
@@ -1162,7 +1589,18 @@ static int compute_bit_allocation(AC3EncodeContext *s)
1162 1162
 
1163 1163
     ret = cbr_bit_allocation(s);
1164 1164
     while (ret) {
1165
-        /* fallback 1: downgrade exponents */
1165
+        /* fallback 1: disable channel coupling */
1166
+        if (s->cpl_on) {
1167
+            s->cpl_on = 0;
1168
+            compute_coupling_strategy(s);
1169
+            compute_rematrixing_strategy(s);
1170
+            apply_rematrixing(s);
1171
+            process_exponents(s);
1172
+            ret = compute_bit_allocation(s);
1173
+            continue;
1174
+        }
1175
+
1176
+        /* fallback 2: downgrade exponents */
1166 1177
         if (!downgrade_exponents(s)) {
1167 1178
             extract_exponents(s);
1168 1179
             encode_exponents(s);
... ...
@@ -1216,12 +1654,13 @@ static inline int asym_quant(int c, int e, int qbits)
1216 1216
  * Quantize a set of mantissas for a single channel in a single block.
1217 1217
  */
1218 1218
 static void quantize_mantissas_blk_ch(AC3Mant *s, int32_t *fixed_coef,
1219
-                                      uint8_t *exp,
1220
-                                      uint8_t *bap, uint16_t *qmant, int n)
1219
+                                      uint8_t *exp, uint8_t *bap,
1220
+                                      uint16_t *qmant, int start_freq,
1221
+                                      int end_freq)
1221 1222
 {
1222 1223
     int i;
1223 1224
 
1224
-    for (i = 0; i < n; i++) {
1225
+    for (i = start_freq; i < end_freq; i++) {
1225 1226
         int v;
1226 1227
         int c = fixed_coef[i];
1227 1228
         int e = exp[i];
... ...
@@ -1311,19 +1750,27 @@ static void quantize_mantissas_blk_ch(AC3Mant *s, int32_t *fixed_coef,
1311 1311
  */
1312 1312
 static void quantize_mantissas(AC3EncodeContext *s)
1313 1313
 {
1314
-    int blk, ch;
1315
-
1314
+    int blk, ch, ch0=0, got_cpl;
1316 1315
 
1317 1316
     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1318 1317
         AC3Block *block = &s->blocks[blk];
1319 1318
         AC3Block *ref_block;
1320 1319
         AC3Mant m = { 0 };
1321 1320
 
1322
-        for (ch = 0; ch < s->channels; ch++) {
1321
+        got_cpl = !block->cpl_in_use;
1322
+        for (ch = 1; ch <= s->channels; ch++) {
1323
+            if (!got_cpl && ch > 1 && block->channel_in_cpl[ch-1]) {
1324
+                ch0     = ch - 1;
1325
+                ch      = CPL_CH;
1326
+                got_cpl = 1;
1327
+            }
1323 1328
             ref_block = block->exp_ref_block[ch];
1324 1329
             quantize_mantissas_blk_ch(&m, block->fixed_coef[ch],
1325
-                                      ref_block->exp[ch], ref_block->bap[ch],
1326
-                                      block->qmant[ch], s->nb_coefs[ch]);
1330
+                                      ref_block->exp[ch],
1331
+                                      ref_block->bap[ch], block->qmant[ch],
1332
+                                      s->start_freq[ch], block->end_freq[ch]);
1333
+            if (ch == CPL_CH)
1334
+                ch = ch0;
1327 1335
         }
1328 1336
     }
1329 1337
 }
... ...
@@ -1390,7 +1837,8 @@ static void output_frame_header(AC3EncodeContext *s)
1390 1390
  */
1391 1391
 static void output_audio_block(AC3EncodeContext *s, int blk)
1392 1392
 {
1393
-    int ch, i, baie, rbnd;
1393
+    int ch, i, baie, bnd, got_cpl;
1394
+    int av_uninit(ch0);
1394 1395
     AC3Block *block = &s->blocks[blk];
1395 1396
 
1396 1397
     /* block switching */
... ...
@@ -1405,11 +1853,38 @@ static void output_audio_block(AC3EncodeContext *s, int blk)
1405 1405
     put_bits(&s->pb, 1, 0);
1406 1406
 
1407 1407
     /* channel coupling */
1408
-    if (!blk) {
1409
-        put_bits(&s->pb, 1, 1); /* coupling strategy present */
1410
-        put_bits(&s->pb, 1, 0); /* no coupling strategy */
1411
-    } else {
1412
-        put_bits(&s->pb, 1, 0); /* no new coupling strategy */
1408
+    put_bits(&s->pb, 1, block->new_cpl_strategy);
1409
+    if (block->new_cpl_strategy) {
1410
+        put_bits(&s->pb, 1, block->cpl_in_use);
1411
+        if (block->cpl_in_use) {
1412
+            int start_sub, end_sub;
1413
+            for (ch = 1; ch <= s->fbw_channels; ch++)
1414
+                put_bits(&s->pb, 1, block->channel_in_cpl[ch]);
1415
+            if (s->channel_mode == AC3_CHMODE_STEREO)
1416
+                put_bits(&s->pb, 1, 0); /* phase flags in use */
1417
+            start_sub = (s->start_freq[CPL_CH] - 37) / 12;
1418
+            end_sub   = (s->cpl_end_freq       - 37) / 12;
1419
+            put_bits(&s->pb, 4, start_sub);
1420
+            put_bits(&s->pb, 4, end_sub - 3);
1421
+            for (bnd = start_sub+1; bnd < end_sub; bnd++)
1422
+                put_bits(&s->pb, 1, ff_eac3_default_cpl_band_struct[bnd]);
1423
+        }
1424
+    }
1425
+
1426
+    /* coupling coordinates */
1427
+    if (block->cpl_in_use) {
1428
+        for (ch = 1; ch <= s->fbw_channels; ch++) {
1429
+            if (block->channel_in_cpl[ch]) {
1430
+                put_bits(&s->pb, 1, block->new_cpl_coords);
1431
+                if (block->new_cpl_coords) {
1432
+                    put_bits(&s->pb, 2, block->cpl_master_exp[ch]);
1433
+                    for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
1434
+                        put_bits(&s->pb, 4, block->cpl_coord_exp [ch][bnd]);
1435
+                        put_bits(&s->pb, 4, block->cpl_coord_mant[ch][bnd]);
1436
+                    }
1437
+                }
1438
+            }
1439
+        }
1413 1440
     }
1414 1441
 
1415 1442
     /* stereo rematrixing */
... ...
@@ -1417,40 +1892,41 @@ static void output_audio_block(AC3EncodeContext *s, int blk)
1417 1417
         put_bits(&s->pb, 1, block->new_rematrixing_strategy);
1418 1418
         if (block->new_rematrixing_strategy) {
1419 1419
             /* rematrixing flags */
1420
-            for (rbnd = 0; rbnd < s->num_rematrixing_bands; rbnd++)
1421
-                put_bits(&s->pb, 1, block->rematrixing_flags[rbnd]);
1420
+            for (bnd = 0; bnd < block->num_rematrixing_bands; bnd++)
1421
+                put_bits(&s->pb, 1, block->rematrixing_flags[bnd]);
1422 1422
         }
1423 1423
     }
1424 1424
 
1425 1425
     /* exponent strategy */
1426
-    for (ch = 0; ch < s->fbw_channels; ch++)
1426
+    for (ch = !block->cpl_in_use; ch <= s->fbw_channels; ch++)
1427 1427
         put_bits(&s->pb, 2, s->exp_strategy[ch][blk]);
1428 1428
     if (s->lfe_on)
1429 1429
         put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]);
1430 1430
 
1431 1431
     /* bandwidth */
1432
-    for (ch = 0; ch < s->fbw_channels; ch++) {
1433
-        if (s->exp_strategy[ch][blk] != EXP_REUSE)
1432
+    for (ch = 1; ch <= s->fbw_channels; ch++) {
1433
+        if (s->exp_strategy[ch][blk] != EXP_REUSE && !block->channel_in_cpl[ch])
1434 1434
             put_bits(&s->pb, 6, s->bandwidth_code);
1435 1435
     }
1436 1436
 
1437 1437
     /* exponents */
1438
-    for (ch = 0; ch < s->channels; ch++) {
1438
+    for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
1439 1439
         int nb_groups;
1440
+        int cpl = (ch == CPL_CH);
1440 1441
 
1441 1442
         if (s->exp_strategy[ch][blk] == EXP_REUSE)
1442 1443
             continue;
1443 1444
 
1444 1445
         /* DC exponent */
1445
-        put_bits(&s->pb, 4, block->grouped_exp[ch][0]);
1446
+        put_bits(&s->pb, 4, block->grouped_exp[ch][0] >> cpl);
1446 1447
 
1447 1448
         /* exponent groups */
1448
-        nb_groups = exponent_group_tab[s->exp_strategy[ch][blk]-1][s->nb_coefs[ch]];
1449
+        nb_groups = exponent_group_tab[cpl][s->exp_strategy[ch][blk]-1][block->end_freq[ch]-s->start_freq[ch]];
1449 1450
         for (i = 1; i <= nb_groups; i++)
1450 1451
             put_bits(&s->pb, 7, block->grouped_exp[ch][i]);
1451 1452
 
1452 1453
         /* gain range info */
1453
-        if (ch != s->lfe_channel)
1454
+        if (ch != s->lfe_channel && !cpl)
1454 1455
             put_bits(&s->pb, 2, 0);
1455 1456
     }
1456 1457
 
... ...
@@ -1466,23 +1942,40 @@ static void output_audio_block(AC3EncodeContext *s, int blk)
1466 1466
     }
1467 1467
 
1468 1468
     /* snr offset */
1469
-    put_bits(&s->pb, 1, baie);
1470
-    if (baie) {
1469
+    put_bits(&s->pb, 1, block->new_snr_offsets);
1470
+    if (block->new_snr_offsets) {
1471 1471
         put_bits(&s->pb, 6, s->coarse_snr_offset);
1472
-        for (ch = 0; ch < s->channels; ch++) {
1472
+        for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
1473 1473
             put_bits(&s->pb, 4, s->fine_snr_offset[ch]);
1474 1474
             put_bits(&s->pb, 3, s->fast_gain_code[ch]);
1475 1475
         }
1476 1476
     }
1477 1477
 
1478
+    /* coupling leak */
1479
+    if (block->cpl_in_use) {
1480
+        put_bits(&s->pb, 1, block->new_cpl_leak);
1481
+        if (block->new_cpl_leak) {
1482
+            put_bits(&s->pb, 3, s->bit_alloc.cpl_fast_leak);
1483
+            put_bits(&s->pb, 3, s->bit_alloc.cpl_slow_leak);
1484
+        }
1485
+    }
1486
+
1478 1487
     put_bits(&s->pb, 1, 0); /* no delta bit allocation */
1479 1488
     put_bits(&s->pb, 1, 0); /* no data to skip */
1480 1489
 
1481 1490
     /* mantissas */
1482
-    for (ch = 0; ch < s->channels; ch++) {
1491
+    got_cpl = !block->cpl_in_use;
1492
+    for (ch = 1; ch <= s->channels; ch++) {
1483 1493
         int b, q;
1484
-        AC3Block *ref_block = block->exp_ref_block[ch];
1485
-        for (i = 0; i < s->nb_coefs[ch]; i++) {
1494
+        AC3Block *ref_block;
1495
+
1496
+        if (!got_cpl && ch > 1 && block->channel_in_cpl[ch-1]) {
1497
+            ch0     = ch - 1;
1498
+            ch      = CPL_CH;
1499
+            got_cpl = 1;
1500
+        }
1501
+        ref_block = block->exp_ref_block[ch];
1502
+        for (i = s->start_freq[ch]; i < block->end_freq[ch]; i++) {
1486 1503
             q = block->qmant[ch][i];
1487 1504
             b = ref_block->bap[ch][i];
1488 1505
             switch (b) {
... ...
@@ -1496,6 +1989,8 @@ static void output_audio_block(AC3EncodeContext *s, int blk)
1496 1496
             default:              put_bits(&s->pb, b-1, q); break;
1497 1497
             }
1498 1498
         }
1499
+        if (ch == CPL_CH)
1500
+            ch = ch0;
1499 1501
     }
1500 1502
 }
1501 1503
 
... ...
@@ -1881,6 +2376,12 @@ static int ac3_encode_frame(AVCodecContext *avctx, unsigned char *frame,
1881 1881
 
1882 1882
     scale_coefficients(s);
1883 1883
 
1884
+    s->cpl_on = s->cpl_enabled;
1885
+    compute_coupling_strategy(s);
1886
+
1887
+    if (s->cpl_on)
1888
+        apply_channel_coupling(s);
1889
+
1884 1890
     compute_rematrixing_strategy(s);
1885 1891
 
1886 1892
     apply_rematrixing(s);
... ...
@@ -1961,7 +2462,7 @@ static av_cold int set_channel_info(AC3EncodeContext *s, int channels,
1961 1961
     s->lfe_on       = !!(ch_layout & AV_CH_LOW_FREQUENCY);
1962 1962
     s->channels     = channels;
1963 1963
     s->fbw_channels = channels - s->lfe_on;
1964
-    s->lfe_channel  = s->lfe_on ? s->fbw_channels : -1;
1964
+    s->lfe_channel  = s->lfe_on ? s->fbw_channels + 1 : -1;
1965 1965
     if (s->lfe_on)
1966 1966
         ch_layout -= AV_CH_LOW_FREQUENCY;
1967 1967
 
... ...
@@ -2060,6 +2561,10 @@ static av_cold int validate_options(AVCodecContext *avctx, AC3EncodeContext *s)
2060 2060
     s->rematrixing_enabled = s->options.stereo_rematrixing &&
2061 2061
                              (s->channel_mode == AC3_CHMODE_STEREO);
2062 2062
 
2063
+    s->cpl_enabled = s->options.channel_coupling &&
2064
+                     s->channel_mode >= AC3_CHMODE_STEREO &&
2065
+                     CONFIG_AC3ENC_FLOAT;
2066
+
2063 2067
     return 0;
2064 2068
 }
2065 2069
 
... ...
@@ -2071,7 +2576,8 @@ static av_cold int validate_options(AVCodecContext *avctx, AC3EncodeContext *s)
2071 2071
  */
2072 2072
 static av_cold void set_bandwidth(AC3EncodeContext *s)
2073 2073
 {
2074
-    int ch;
2074
+    int blk, ch;
2075
+    int av_uninit(cpl_start);
2075 2076
 
2076 2077
     if (s->cutoff) {
2077 2078
         /* calculate bandwidth based on user-specified cutoff frequency */
... ...
@@ -2084,11 +2590,54 @@ static av_cold void set_bandwidth(AC3EncodeContext *s)
2084 2084
     }
2085 2085
 
2086 2086
     /* set number of coefficients for each channel */
2087
-    for (ch = 0; ch < s->fbw_channels; ch++) {
2088
-        s->nb_coefs[ch] = s->bandwidth_code * 3 + 73;
2087
+    for (ch = 1; ch <= s->fbw_channels; ch++) {
2088
+        s->start_freq[ch] = 0;
2089
+        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
2090
+            s->blocks[blk].end_freq[ch] = s->bandwidth_code * 3 + 73;
2091
+    }
2092
+    /* LFE channel always has 7 coefs */
2093
+    if (s->lfe_on) {
2094
+        s->start_freq[s->lfe_channel] = 0;
2095
+        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
2096
+            s->blocks[blk].end_freq[ch] = 7;
2097
+    }
2098
+
2099
+    /* initialize coupling strategy */
2100
+    if (s->cpl_enabled) {
2101
+        if (s->options.cpl_start >= 0) {
2102
+            cpl_start = s->options.cpl_start;
2103
+        } else {
2104
+            cpl_start = ac3_coupling_start_tab[s->channel_mode-2][s->bit_alloc.sr_code][s->frame_size_code/2];
2105
+            if (cpl_start < 0)
2106
+                s->cpl_enabled = 0;
2107
+        }
2108
+    }
2109
+    if (s->cpl_enabled) {
2110
+        int i, cpl_start_band, cpl_end_band;
2111
+        uint8_t *cpl_band_sizes = s->cpl_band_sizes;
2112
+
2113
+        cpl_end_band   = s->bandwidth_code / 4 + 3;
2114
+        cpl_start_band = av_clip(cpl_start, 0, FFMIN(cpl_end_band-1, 15));
2115
+
2116
+        s->num_cpl_subbands = cpl_end_band - cpl_start_band;
2117
+
2118
+        s->num_cpl_bands = 1;
2119
+        *cpl_band_sizes  = 12;
2120
+        for (i = cpl_start_band + 1; i < cpl_end_band; i++) {
2121
+            if (ff_eac3_default_cpl_band_struct[i]) {
2122
+                *cpl_band_sizes += 12;
2123
+            } else {
2124
+                s->num_cpl_bands++;
2125
+                cpl_band_sizes++;
2126
+                *cpl_band_sizes = 12;
2127
+            }
2128
+        }
2129
+
2130
+        s->start_freq[CPL_CH] = cpl_start_band * 12 + 37;
2131
+        s->cpl_end_freq       = cpl_end_band   * 12 + 37;
2132
+        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
2133
+            s->blocks[blk].end_freq[CPL_CH] = s->cpl_end_freq;
2089 2134
     }
2090
-    if (s->lfe_on)
2091
-        s->nb_coefs[s->lfe_channel] = 7; /* LFE channel always has 7 coefs */
2092 2135
 }
2093 2136
 
2094 2137
 
... ...
@@ -2096,6 +2645,7 @@ static av_cold int allocate_buffers(AVCodecContext *avctx)
2096 2096
 {
2097 2097
     int blk, ch;
2098 2098
     AC3EncodeContext *s = avctx->priv_data;
2099
+    int channels = s->channels + 1; /* includes coupling channel */
2099 2100
 
2100 2101
     FF_ALLOC_OR_GOTO(avctx, s->planar_samples, s->channels * sizeof(*s->planar_samples),
2101 2102
                      alloc_fail);
... ...
@@ -2104,74 +2654,90 @@ static av_cold int allocate_buffers(AVCodecContext *avctx)
2104 2104
                           (AC3_FRAME_SIZE+AC3_BLOCK_SIZE) * sizeof(**s->planar_samples),
2105 2105
                           alloc_fail);
2106 2106
     }
2107
-    FF_ALLOC_OR_GOTO(avctx, s->bap_buffer,  AC3_MAX_BLOCKS * s->channels *
2107
+    FF_ALLOC_OR_GOTO(avctx, s->bap_buffer,  AC3_MAX_BLOCKS * channels *
2108 2108
                      AC3_MAX_COEFS * sizeof(*s->bap_buffer),  alloc_fail);
2109
-    FF_ALLOC_OR_GOTO(avctx, s->bap1_buffer, AC3_MAX_BLOCKS * s->channels *
2109
+    FF_ALLOC_OR_GOTO(avctx, s->bap1_buffer, AC3_MAX_BLOCKS * channels *
2110 2110
                      AC3_MAX_COEFS * sizeof(*s->bap1_buffer), alloc_fail);
2111
-    FF_ALLOC_OR_GOTO(avctx, s->mdct_coef_buffer, AC3_MAX_BLOCKS * s->channels *
2111
+    FF_ALLOC_OR_GOTO(avctx, s->mdct_coef_buffer, AC3_MAX_BLOCKS * channels *
2112 2112
                      AC3_MAX_COEFS * sizeof(*s->mdct_coef_buffer), alloc_fail);
2113
-    FF_ALLOC_OR_GOTO(avctx, s->exp_buffer, AC3_MAX_BLOCKS * s->channels *
2113
+    FF_ALLOC_OR_GOTO(avctx, s->exp_buffer, AC3_MAX_BLOCKS * channels *
2114 2114
                      AC3_MAX_COEFS * sizeof(*s->exp_buffer), alloc_fail);
2115
-    FF_ALLOC_OR_GOTO(avctx, s->grouped_exp_buffer, AC3_MAX_BLOCKS * s->channels *
2115
+    FF_ALLOC_OR_GOTO(avctx, s->grouped_exp_buffer, AC3_MAX_BLOCKS * channels *
2116 2116
                      128 * sizeof(*s->grouped_exp_buffer), alloc_fail);
2117
-    FF_ALLOC_OR_GOTO(avctx, s->psd_buffer, AC3_MAX_BLOCKS * s->channels *
2117
+    FF_ALLOC_OR_GOTO(avctx, s->psd_buffer, AC3_MAX_BLOCKS * channels *
2118 2118
                      AC3_MAX_COEFS * sizeof(*s->psd_buffer), alloc_fail);
2119
-    FF_ALLOC_OR_GOTO(avctx, s->band_psd_buffer, AC3_MAX_BLOCKS * s->channels *
2119
+    FF_ALLOC_OR_GOTO(avctx, s->band_psd_buffer, AC3_MAX_BLOCKS * channels *
2120 2120
                      64 * sizeof(*s->band_psd_buffer), alloc_fail);
2121
-    FF_ALLOC_OR_GOTO(avctx, s->mask_buffer, AC3_MAX_BLOCKS * s->channels *
2121
+    FF_ALLOC_OR_GOTO(avctx, s->mask_buffer, AC3_MAX_BLOCKS * channels *
2122 2122
                      64 * sizeof(*s->mask_buffer), alloc_fail);
2123
-    FF_ALLOC_OR_GOTO(avctx, s->qmant_buffer, AC3_MAX_BLOCKS * s->channels *
2123
+    FF_ALLOC_OR_GOTO(avctx, s->qmant_buffer, AC3_MAX_BLOCKS * channels *
2124 2124
                      AC3_MAX_COEFS * sizeof(*s->qmant_buffer), alloc_fail);
2125
+    if (s->cpl_enabled) {
2126
+        FF_ALLOC_OR_GOTO(avctx, s->cpl_coord_exp_buffer, AC3_MAX_BLOCKS * channels *
2127
+                         16 * sizeof(*s->cpl_coord_exp_buffer), alloc_fail);
2128
+        FF_ALLOC_OR_GOTO(avctx, s->cpl_coord_mant_buffer, AC3_MAX_BLOCKS * channels *
2129
+                         16 * sizeof(*s->cpl_coord_mant_buffer), alloc_fail);
2130
+    }
2125 2131
     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
2126 2132
         AC3Block *block = &s->blocks[blk];
2127
-        FF_ALLOC_OR_GOTO(avctx, block->bap, s->channels * sizeof(*block->bap),
2133
+        FF_ALLOC_OR_GOTO(avctx, block->bap, channels * sizeof(*block->bap),
2128 2134
                          alloc_fail);
2129
-        FF_ALLOCZ_OR_GOTO(avctx, block->mdct_coef, s->channels * sizeof(*block->mdct_coef),
2135
+        FF_ALLOCZ_OR_GOTO(avctx, block->mdct_coef, channels * sizeof(*block->mdct_coef),
2130 2136
                           alloc_fail);
2131
-        FF_ALLOCZ_OR_GOTO(avctx, block->exp, s->channels * sizeof(*block->exp),
2137
+        FF_ALLOCZ_OR_GOTO(avctx, block->exp, channels * sizeof(*block->exp),
2132 2138
                           alloc_fail);
2133
-        FF_ALLOCZ_OR_GOTO(avctx, block->grouped_exp, s->channels * sizeof(*block->grouped_exp),
2139
+        FF_ALLOCZ_OR_GOTO(avctx, block->grouped_exp, channels * sizeof(*block->grouped_exp),
2134 2140
                           alloc_fail);
2135
-        FF_ALLOCZ_OR_GOTO(avctx, block->psd, s->channels * sizeof(*block->psd),
2141
+        FF_ALLOCZ_OR_GOTO(avctx, block->psd, channels * sizeof(*block->psd),
2136 2142
                           alloc_fail);
2137
-        FF_ALLOCZ_OR_GOTO(avctx, block->band_psd, s->channels * sizeof(*block->band_psd),
2143
+        FF_ALLOCZ_OR_GOTO(avctx, block->band_psd, channels * sizeof(*block->band_psd),
2138 2144
                           alloc_fail);
2139
-        FF_ALLOCZ_OR_GOTO(avctx, block->mask, s->channels * sizeof(*block->mask),
2145
+        FF_ALLOCZ_OR_GOTO(avctx, block->mask, channels * sizeof(*block->mask),
2140 2146
                           alloc_fail);
2141
-        FF_ALLOCZ_OR_GOTO(avctx, block->qmant, s->channels * sizeof(*block->qmant),
2147
+        FF_ALLOCZ_OR_GOTO(avctx, block->qmant, channels * sizeof(*block->qmant),
2142 2148
                           alloc_fail);
2149
+        if (s->cpl_enabled) {
2150
+            FF_ALLOCZ_OR_GOTO(avctx, block->cpl_coord_exp, channels * sizeof(*block->cpl_coord_exp),
2151
+                              alloc_fail);
2152
+            FF_ALLOCZ_OR_GOTO(avctx, block->cpl_coord_mant, channels * sizeof(*block->cpl_coord_mant),
2153
+                              alloc_fail);
2154
+        }
2143 2155
 
2144
-        for (ch = 0; ch < s->channels; ch++) {
2156
+        for (ch = 0; ch < channels; ch++) {
2145 2157
             /* arrangement: block, channel, coeff */
2146
-            block->bap[ch]         = &s->bap_buffer        [AC3_MAX_COEFS * (blk * s->channels + ch)];
2147
-            block->mdct_coef[ch]   = &s->mdct_coef_buffer  [AC3_MAX_COEFS * (blk * s->channels + ch)];
2148
-            block->grouped_exp[ch] = &s->grouped_exp_buffer[128           * (blk * s->channels + ch)];
2149
-            block->psd[ch]         = &s->psd_buffer        [AC3_MAX_COEFS * (blk * s->channels + ch)];
2150
-            block->band_psd[ch]    = &s->band_psd_buffer   [64            * (blk * s->channels + ch)];
2151
-            block->mask[ch]        = &s->mask_buffer       [64            * (blk * s->channels + ch)];
2152
-            block->qmant[ch]       = &s->qmant_buffer      [AC3_MAX_COEFS * (blk * s->channels + ch)];
2158
+            block->bap[ch]         = &s->bap_buffer        [AC3_MAX_COEFS * (blk * channels + ch)];
2159
+            block->grouped_exp[ch] = &s->grouped_exp_buffer[128           * (blk * channels + ch)];
2160
+            block->psd[ch]         = &s->psd_buffer        [AC3_MAX_COEFS * (blk * channels + ch)];
2161
+            block->band_psd[ch]    = &s->band_psd_buffer   [64            * (blk * channels + ch)];
2162
+            block->mask[ch]        = &s->mask_buffer       [64            * (blk * channels + ch)];
2163
+            block->qmant[ch]       = &s->qmant_buffer      [AC3_MAX_COEFS * (blk * channels + ch)];
2164
+            if (s->cpl_enabled) {
2165
+                block->cpl_coord_exp[ch]  = &s->cpl_coord_exp_buffer [16  * (blk * channels + ch)];
2166
+                block->cpl_coord_mant[ch] = &s->cpl_coord_mant_buffer[16  * (blk * channels + ch)];
2167
+            }
2153 2168
 
2154 2169
             /* arrangement: channel, block, coeff */
2155 2170
             block->exp[ch]         = &s->exp_buffer        [AC3_MAX_COEFS * (AC3_MAX_BLOCKS * ch + blk)];
2171
+            block->mdct_coef[ch]   = &s->mdct_coef_buffer  [AC3_MAX_COEFS * (AC3_MAX_BLOCKS * ch + blk)];
2156 2172
         }
2157 2173
     }
2158 2174
 
2159 2175
     if (CONFIG_AC3ENC_FLOAT) {
2160
-        FF_ALLOC_OR_GOTO(avctx, s->fixed_coef_buffer, AC3_MAX_BLOCKS * s->channels *
2176
+        FF_ALLOC_OR_GOTO(avctx, s->fixed_coef_buffer, AC3_MAX_BLOCKS * channels *
2161 2177
                          AC3_MAX_COEFS * sizeof(*s->fixed_coef_buffer), alloc_fail);
2162 2178
         for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
2163 2179
             AC3Block *block = &s->blocks[blk];
2164
-            FF_ALLOCZ_OR_GOTO(avctx, block->fixed_coef, s->channels *
2180
+            FF_ALLOCZ_OR_GOTO(avctx, block->fixed_coef, channels *
2165 2181
                               sizeof(*block->fixed_coef), alloc_fail);
2166
-            for (ch = 0; ch < s->channels; ch++)
2167
-                block->fixed_coef[ch] = &s->fixed_coef_buffer[AC3_MAX_COEFS * (blk * s->channels + ch)];
2182
+            for (ch = 0; ch < channels; ch++)
2183
+                block->fixed_coef[ch] = &s->fixed_coef_buffer[AC3_MAX_COEFS * (AC3_MAX_BLOCKS * ch + blk)];
2168 2184
         }
2169 2185
     } else {
2170 2186
         for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
2171 2187
             AC3Block *block = &s->blocks[blk];
2172
-            FF_ALLOCZ_OR_GOTO(avctx, block->fixed_coef, s->channels *
2188
+            FF_ALLOCZ_OR_GOTO(avctx, block->fixed_coef, channels *
2173 2189
                               sizeof(*block->fixed_coef), alloc_fail);
2174
-            for (ch = 0; ch < s->channels; ch++)
2190
+            for (ch = 0; ch < channels; ch++)
2175 2191
                 block->fixed_coef[ch] = (int32_t *)block->mdct_coef[ch];
2176 2192
         }
2177 2193
     }
... ...
@@ -101,7 +101,7 @@ static void scale_coefficients(AC3EncodeContext *s)
101 101
 
102 102
     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
103 103
         AC3Block *block = &s->blocks[blk];
104
-        for (ch = 0; ch < s->channels; ch++) {
104
+        for (ch = 1; ch <= s->channels; ch++) {
105 105
             s->ac3dsp.ac3_rshift_int32(block->mdct_coef[ch], AC3_MAX_COEFS,
106 106
                                        block->coeff_shift[ch]);
107 107
         }
... ...
@@ -93,8 +93,10 @@ static int normalize_samples(AC3EncodeContext *s)
93 93
  */
94 94
 static void scale_coefficients(AC3EncodeContext *s)
95 95
 {
96
-    s->ac3dsp.float_to_fixed24(s->fixed_coef_buffer, s->mdct_coef_buffer,
97
-                               AC3_MAX_COEFS * AC3_MAX_BLOCKS * s->channels);
96
+    int chan_size = AC3_MAX_COEFS * AC3_MAX_BLOCKS;
97
+    s->ac3dsp.float_to_fixed24(s->fixed_coef_buffer + chan_size,
98
+                               s->mdct_coef_buffer  + chan_size,
99
+                               chan_size * s->channels);
98 100
 }
99 101
 
100 102
 
... ...
@@ -138,6 +138,13 @@ const uint16_t ff_ac3_bitrate_tab[19] = {
138 138
  */
139 139
 const uint8_t ff_ac3_rematrix_band_tab[5] = { 13, 25, 37, 61, 253 };
140 140
 
141
+/**
142
+ * Table E2.16 Default Coupling Banding Structure
143
+ */
144
+const uint8_t ff_eac3_default_cpl_band_struct[18] = {
145
+    0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1
146
+};
147
+
141 148
 /* AC-3 MDCT window */
142 149
 
143 150
 /* MDCT window */
... ...
@@ -39,6 +39,7 @@ extern const uint8_t  ff_ac3_dec_channel_map[8][2][6];
39 39
 extern const uint16_t ff_ac3_sample_rate_tab[3];
40 40
 extern const uint16_t ff_ac3_bitrate_tab[19];
41 41
 extern const uint8_t  ff_ac3_rematrix_band_tab[5];
42
+extern const uint8_t  ff_eac3_default_cpl_band_struct[18];
42 43
 extern const int16_t  ff_ac3_window[AC3_WINDOW_SIZE/2];
43 44
 extern const uint8_t  ff_ac3_log_add_tab[260];
44 45
 extern const uint16_t ff_ac3_hearing_threshold_tab[AC3_CRITICAL_BANDS][3];