00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00026
00027
00028 #include "libavutil/crc.h"
00029 #include "avcodec.h"
00030 #include "libavutil/common.h"
00031 #include "put_bits.h"
00032 #include "ac3.h"
00033 #include "audioconvert.h"
00034
00035 typedef struct AC3EncodeContext {
00036 PutBitContext pb;
00037 int nb_channels;
00038 int nb_all_channels;
00039 int lfe_channel;
00040 const uint8_t *channel_map;
00041 int bit_rate;
00042 unsigned int sample_rate;
00043 unsigned int bitstream_id;
00044 unsigned int frame_size_min;
00045 unsigned int frame_size;
00046 unsigned int bits_written;
00047 unsigned int samples_written;
00048 int sr_shift;
00049 unsigned int frame_size_code;
00050 unsigned int sr_code;
00051 unsigned int channel_mode;
00052 int lfe;
00053 unsigned int bitstream_mode;
00054 short last_samples[AC3_MAX_CHANNELS][256];
00055 unsigned int chbwcod[AC3_MAX_CHANNELS];
00056 int nb_coefs[AC3_MAX_CHANNELS];
00057
00058
00059 int slow_gain_code, slow_decay_code, fast_decay_code, db_per_bit_code, floor_code;
00060 AC3BitAllocParameters bit_alloc;
00061 int coarse_snr_offset;
00062 int fast_gain_code[AC3_MAX_CHANNELS];
00063 int fine_snr_offset[AC3_MAX_CHANNELS];
00064
00065 int mant1_cnt, mant2_cnt, mant4_cnt;
00066 } AC3EncodeContext;
00067
00068 static int16_t costab[64];
00069 static int16_t sintab[64];
00070 static int16_t xcos1[128];
00071 static int16_t xsin1[128];
00072
00073 #define MDCT_NBITS 9
00074 #define N (1 << MDCT_NBITS)
00075
00076
00077 #define EXP_DIFF_THRESHOLD 1000
00078
00079 static inline int16_t fix15(float a)
00080 {
00081 int v;
00082 v = (int)(a * (float)(1 << 15));
00083 if (v < -32767)
00084 v = -32767;
00085 else if (v > 32767)
00086 v = 32767;
00087 return v;
00088 }
00089
00090 typedef struct IComplex {
00091 short re,im;
00092 } IComplex;
00093
00094 static av_cold void fft_init(int ln)
00095 {
00096 int i, n;
00097 float alpha;
00098
00099 n = 1 << ln;
00100
00101 for(i=0;i<(n/2);i++) {
00102 alpha = 2 * M_PI * (float)i / (float)n;
00103 costab[i] = fix15(cos(alpha));
00104 sintab[i] = fix15(sin(alpha));
00105 }
00106 }
00107
00108
00109 #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \
00110 {\
00111 int ax, ay, bx, by;\
00112 bx=pre1;\
00113 by=pim1;\
00114 ax=qre1;\
00115 ay=qim1;\
00116 pre = (bx + ax) >> 1;\
00117 pim = (by + ay) >> 1;\
00118 qre = (bx - ax) >> 1;\
00119 qim = (by - ay) >> 1;\
00120 }
00121
00122 #define CMUL(pre, pim, are, aim, bre, bim) \
00123 {\
00124 pre = (MUL16(are, bre) - MUL16(aim, bim)) >> 15;\
00125 pim = (MUL16(are, bim) + MUL16(bre, aim)) >> 15;\
00126 }
00127
00128
00129
00130 static void fft(IComplex *z, int ln)
00131 {
00132 int j, l, np, np2;
00133 int nblocks, nloops;
00134 register IComplex *p,*q;
00135 int tmp_re, tmp_im;
00136
00137 np = 1 << ln;
00138
00139
00140 for(j=0;j<np;j++) {
00141 int k = av_reverse[j] >> (8 - ln);
00142 if (k < j)
00143 FFSWAP(IComplex, z[k], z[j]);
00144 }
00145
00146
00147
00148 p=&z[0];
00149 j=(np >> 1);
00150 do {
00151 BF(p[0].re, p[0].im, p[1].re, p[1].im,
00152 p[0].re, p[0].im, p[1].re, p[1].im);
00153 p+=2;
00154 } while (--j != 0);
00155
00156
00157
00158 p=&z[0];
00159 j=np >> 2;
00160 do {
00161 BF(p[0].re, p[0].im, p[2].re, p[2].im,
00162 p[0].re, p[0].im, p[2].re, p[2].im);
00163 BF(p[1].re, p[1].im, p[3].re, p[3].im,
00164 p[1].re, p[1].im, p[3].im, -p[3].re);
00165 p+=4;
00166 } while (--j != 0);
00167
00168
00169
00170 nblocks = np >> 3;
00171 nloops = 1 << 2;
00172 np2 = np >> 1;
00173 do {
00174 p = z;
00175 q = z + nloops;
00176 for (j = 0; j < nblocks; ++j) {
00177
00178 BF(p->re, p->im, q->re, q->im,
00179 p->re, p->im, q->re, q->im);
00180
00181 p++;
00182 q++;
00183 for(l = nblocks; l < np2; l += nblocks) {
00184 CMUL(tmp_re, tmp_im, costab[l], -sintab[l], q->re, q->im);
00185 BF(p->re, p->im, q->re, q->im,
00186 p->re, p->im, tmp_re, tmp_im);
00187 p++;
00188 q++;
00189 }
00190 p += nloops;
00191 q += nloops;
00192 }
00193 nblocks = nblocks >> 1;
00194 nloops = nloops << 1;
00195 } while (nblocks != 0);
00196 }
00197
00198
00199 static void mdct512(int32_t *out, int16_t *in)
00200 {
00201 int i, re, im, re1, im1;
00202 int16_t rot[N];
00203 IComplex x[N/4];
00204
00205
00206 for(i=0;i<N/4;i++)
00207 rot[i] = -in[i + 3*N/4];
00208 for(i=N/4;i<N;i++)
00209 rot[i] = in[i - N/4];
00210
00211
00212 for(i=0;i<N/4;i++) {
00213 re = ((int)rot[2*i] - (int)rot[N-1-2*i]) >> 1;
00214 im = -((int)rot[N/2+2*i] - (int)rot[N/2-1-2*i]) >> 1;
00215 CMUL(x[i].re, x[i].im, re, im, -xcos1[i], xsin1[i]);
00216 }
00217
00218 fft(x, MDCT_NBITS - 2);
00219
00220
00221 for(i=0;i<N/4;i++) {
00222 re = x[i].re;
00223 im = x[i].im;
00224 CMUL(re1, im1, re, im, xsin1[i], xcos1[i]);
00225 out[2*i] = im1;
00226 out[N/2-1-2*i] = re1;
00227 }
00228 }
00229
00230
00231 static int calc_exp_diff(uint8_t *exp1, uint8_t *exp2, int n)
00232 {
00233 int sum, i;
00234 sum = 0;
00235 for(i=0;i<n;i++) {
00236 sum += abs(exp1[i] - exp2[i]);
00237 }
00238 return sum;
00239 }
00240
00241 static void compute_exp_strategy(uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],
00242 uint8_t exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
00243 int ch, int is_lfe)
00244 {
00245 int i, j;
00246 int exp_diff;
00247
00248
00249
00250 exp_strategy[0][ch] = EXP_NEW;
00251 for(i=1;i<NB_BLOCKS;i++) {
00252 exp_diff = calc_exp_diff(exp[i][ch], exp[i-1][ch], N/2);
00253 dprintf(NULL, "exp_diff=%d\n", exp_diff);
00254 if (exp_diff > EXP_DIFF_THRESHOLD)
00255 exp_strategy[i][ch] = EXP_NEW;
00256 else
00257 exp_strategy[i][ch] = EXP_REUSE;
00258 }
00259 if (is_lfe)
00260 return;
00261
00262
00263
00264 i = 0;
00265 while (i < NB_BLOCKS) {
00266 j = i + 1;
00267 while (j < NB_BLOCKS && exp_strategy[j][ch] == EXP_REUSE)
00268 j++;
00269 switch(j - i) {
00270 case 1:
00271 exp_strategy[i][ch] = EXP_D45;
00272 break;
00273 case 2:
00274 case 3:
00275 exp_strategy[i][ch] = EXP_D25;
00276 break;
00277 default:
00278 exp_strategy[i][ch] = EXP_D15;
00279 break;
00280 }
00281 i = j;
00282 }
00283 }
00284
00285
00286 static void exponent_min(uint8_t exp[N/2], uint8_t exp1[N/2], int n)
00287 {
00288 int i;
00289
00290 for(i=0;i<n;i++) {
00291 if (exp1[i] < exp[i])
00292 exp[i] = exp1[i];
00293 }
00294 }
00295
00296
00297
00298 static int encode_exp(uint8_t encoded_exp[N/2],
00299 uint8_t exp[N/2],
00300 int nb_exps,
00301 int exp_strategy)
00302 {
00303 int group_size, nb_groups, i, j, k, exp_min;
00304 uint8_t exp1[N/2];
00305
00306 switch(exp_strategy) {
00307 case EXP_D15:
00308 group_size = 1;
00309 break;
00310 case EXP_D25:
00311 group_size = 2;
00312 break;
00313 default:
00314 case EXP_D45:
00315 group_size = 4;
00316 break;
00317 }
00318 nb_groups = ((nb_exps + (group_size * 3) - 4) / (3 * group_size)) * 3;
00319
00320
00321 exp1[0] = exp[0];
00322 k = 1;
00323 for(i=1;i<=nb_groups;i++) {
00324 exp_min = exp[k];
00325 assert(exp_min >= 0 && exp_min <= 24);
00326 for(j=1;j<group_size;j++) {
00327 if (exp[k+j] < exp_min)
00328 exp_min = exp[k+j];
00329 }
00330 exp1[i] = exp_min;
00331 k += group_size;
00332 }
00333
00334
00335 if (exp1[0] > 15)
00336 exp1[0] = 15;
00337
00338
00339
00340 for (i=1;i<=nb_groups;i++)
00341 exp1[i] = FFMIN(exp1[i], exp1[i-1] + 2);
00342 for (i=nb_groups-1;i>=0;i--)
00343 exp1[i] = FFMIN(exp1[i], exp1[i+1] + 2);
00344
00345
00346 encoded_exp[0] = exp1[0];
00347 k = 1;
00348 for(i=1;i<=nb_groups;i++) {
00349 for(j=0;j<group_size;j++) {
00350 encoded_exp[k+j] = exp1[i];
00351 }
00352 k += group_size;
00353 }
00354
00355 #if defined(DEBUG)
00356 av_log(NULL, AV_LOG_DEBUG, "exponents: strategy=%d\n", exp_strategy);
00357 for(i=0;i<=nb_groups * group_size;i++) {
00358 av_log(NULL, AV_LOG_DEBUG, "%d ", encoded_exp[i]);
00359 }
00360 av_log(NULL, AV_LOG_DEBUG, "\n");
00361 #endif
00362
00363 return 4 + (nb_groups / 3) * 7;
00364 }
00365
00366
00367 static int compute_mantissa_size(AC3EncodeContext *s, uint8_t *m, int nb_coefs)
00368 {
00369 int bits, mant, i;
00370
00371 bits = 0;
00372 for(i=0;i<nb_coefs;i++) {
00373 mant = m[i];
00374 switch(mant) {
00375 case 0:
00376
00377 break;
00378 case 1:
00379
00380 if (s->mant1_cnt == 0)
00381 bits += 5;
00382 if (++s->mant1_cnt == 3)
00383 s->mant1_cnt = 0;
00384 break;
00385 case 2:
00386
00387 if (s->mant2_cnt == 0)
00388 bits += 7;
00389 if (++s->mant2_cnt == 3)
00390 s->mant2_cnt = 0;
00391 break;
00392 case 3:
00393 bits += 3;
00394 break;
00395 case 4:
00396
00397 if (s->mant4_cnt == 0)
00398 bits += 7;
00399 if (++s->mant4_cnt == 2)
00400 s->mant4_cnt = 0;
00401 break;
00402 case 14:
00403 bits += 14;
00404 break;
00405 case 15:
00406 bits += 16;
00407 break;
00408 default:
00409 bits += mant - 1;
00410 break;
00411 }
00412 }
00413 return bits;
00414 }
00415
00416
00417 static void bit_alloc_masking(AC3EncodeContext *s,
00418 uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
00419 uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],
00420 int16_t psd[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
00421 int16_t mask[NB_BLOCKS][AC3_MAX_CHANNELS][50])
00422 {
00423 int blk, ch;
00424 int16_t band_psd[NB_BLOCKS][AC3_MAX_CHANNELS][50];
00425
00426 for(blk=0; blk<NB_BLOCKS; blk++) {
00427 for(ch=0;ch<s->nb_all_channels;ch++) {
00428 if(exp_strategy[blk][ch] == EXP_REUSE) {
00429 memcpy(psd[blk][ch], psd[blk-1][ch], (N/2)*sizeof(int16_t));
00430 memcpy(mask[blk][ch], mask[blk-1][ch], 50*sizeof(int16_t));
00431 } else {
00432 ff_ac3_bit_alloc_calc_psd(encoded_exp[blk][ch], 0,
00433 s->nb_coefs[ch],
00434 psd[blk][ch], band_psd[blk][ch]);
00435 ff_ac3_bit_alloc_calc_mask(&s->bit_alloc, band_psd[blk][ch],
00436 0, s->nb_coefs[ch],
00437 ff_ac3_fast_gain_tab[s->fast_gain_code[ch]],
00438 ch == s->lfe_channel,
00439 DBA_NONE, 0, NULL, NULL, NULL,
00440 mask[blk][ch]);
00441 }
00442 }
00443 }
00444 }
00445
00446 static int bit_alloc(AC3EncodeContext *s,
00447 int16_t mask[NB_BLOCKS][AC3_MAX_CHANNELS][50],
00448 int16_t psd[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
00449 uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
00450 int frame_bits, int coarse_snr_offset, int fine_snr_offset)
00451 {
00452 int i, ch;
00453 int snr_offset;
00454
00455 snr_offset = (((coarse_snr_offset - 15) << 4) + fine_snr_offset) << 2;
00456
00457
00458 for(i=0;i<NB_BLOCKS;i++) {
00459 s->mant1_cnt = 0;
00460 s->mant2_cnt = 0;
00461 s->mant4_cnt = 0;
00462 for(ch=0;ch<s->nb_all_channels;ch++) {
00463 ff_ac3_bit_alloc_calc_bap(mask[i][ch], psd[i][ch], 0,
00464 s->nb_coefs[ch], snr_offset,
00465 s->bit_alloc.floor, ff_ac3_bap_tab,
00466 bap[i][ch]);
00467 frame_bits += compute_mantissa_size(s, bap[i][ch],
00468 s->nb_coefs[ch]);
00469 }
00470 }
00471 #if 0
00472 printf("csnr=%d fsnr=%d frame_bits=%d diff=%d\n",
00473 coarse_snr_offset, fine_snr_offset, frame_bits,
00474 16 * s->frame_size - ((frame_bits + 7) & ~7));
00475 #endif
00476 return 16 * s->frame_size - frame_bits;
00477 }
00478
00479 #define SNR_INC1 4
00480
00481 static int compute_bit_allocation(AC3EncodeContext *s,
00482 uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
00483 uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
00484 uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],
00485 int frame_bits)
00486 {
00487 int i, ch;
00488 int coarse_snr_offset, fine_snr_offset;
00489 uint8_t bap1[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
00490 int16_t psd[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
00491 int16_t mask[NB_BLOCKS][AC3_MAX_CHANNELS][50];
00492 static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
00493
00494
00495 s->slow_decay_code = 2;
00496 s->fast_decay_code = 1;
00497 s->slow_gain_code = 1;
00498 s->db_per_bit_code = 2;
00499 s->floor_code = 4;
00500 for(ch=0;ch<s->nb_all_channels;ch++)
00501 s->fast_gain_code[ch] = 4;
00502
00503
00504 s->bit_alloc.sr_code = s->sr_code;
00505 s->bit_alloc.sr_shift = s->sr_shift;
00506 s->bit_alloc.slow_decay = ff_ac3_slow_decay_tab[s->slow_decay_code] >> s->sr_shift;
00507 s->bit_alloc.fast_decay = ff_ac3_fast_decay_tab[s->fast_decay_code] >> s->sr_shift;
00508 s->bit_alloc.slow_gain = ff_ac3_slow_gain_tab[s->slow_gain_code];
00509 s->bit_alloc.db_per_bit = ff_ac3_db_per_bit_tab[s->db_per_bit_code];
00510 s->bit_alloc.floor = ff_ac3_floor_tab[s->floor_code];
00511
00512
00513 frame_bits += 65;
00514
00515
00516 frame_bits += frame_bits_inc[s->channel_mode];
00517
00518
00519 for(i=0;i<NB_BLOCKS;i++) {
00520 frame_bits += s->nb_channels * 2 + 2;
00521 if (s->channel_mode == AC3_CHMODE_STEREO) {
00522 frame_bits++;
00523 if(i==0) frame_bits += 4;
00524 }
00525 frame_bits += 2 * s->nb_channels;
00526 if (s->lfe)
00527 frame_bits++;
00528 for(ch=0;ch<s->nb_channels;ch++) {
00529 if (exp_strategy[i][ch] != EXP_REUSE)
00530 frame_bits += 6 + 2;
00531 }
00532 frame_bits++;
00533 frame_bits++;
00534 frame_bits += 2;
00535 }
00536 frame_bits++;
00537
00538
00539
00540
00541 frame_bits += 2*4 + 3 + 6 + s->nb_all_channels * (4 + 3);
00542
00543
00544 frame_bits += 2;
00545
00546
00547 frame_bits += 16;
00548
00549
00550 bit_alloc_masking(s, encoded_exp, exp_strategy, psd, mask);
00551
00552
00553
00554
00555 coarse_snr_offset = s->coarse_snr_offset;
00556 while (coarse_snr_offset >= 0 &&
00557 bit_alloc(s, mask, psd, bap, frame_bits, coarse_snr_offset, 0) < 0)
00558 coarse_snr_offset -= SNR_INC1;
00559 if (coarse_snr_offset < 0) {
00560 av_log(NULL, AV_LOG_ERROR, "Bit allocation failed. Try increasing the bitrate.\n");
00561 return -1;
00562 }
00563 while ((coarse_snr_offset + SNR_INC1) <= 63 &&
00564 bit_alloc(s, mask, psd, bap1, frame_bits,
00565 coarse_snr_offset + SNR_INC1, 0) >= 0) {
00566 coarse_snr_offset += SNR_INC1;
00567 memcpy(bap, bap1, sizeof(bap1));
00568 }
00569 while ((coarse_snr_offset + 1) <= 63 &&
00570 bit_alloc(s, mask, psd, bap1, frame_bits, coarse_snr_offset + 1, 0) >= 0) {
00571 coarse_snr_offset++;
00572 memcpy(bap, bap1, sizeof(bap1));
00573 }
00574
00575 fine_snr_offset = 0;
00576 while ((fine_snr_offset + SNR_INC1) <= 15 &&
00577 bit_alloc(s, mask, psd, bap1, frame_bits,
00578 coarse_snr_offset, fine_snr_offset + SNR_INC1) >= 0) {
00579 fine_snr_offset += SNR_INC1;
00580 memcpy(bap, bap1, sizeof(bap1));
00581 }
00582 while ((fine_snr_offset + 1) <= 15 &&
00583 bit_alloc(s, mask, psd, bap1, frame_bits,
00584 coarse_snr_offset, fine_snr_offset + 1) >= 0) {
00585 fine_snr_offset++;
00586 memcpy(bap, bap1, sizeof(bap1));
00587 }
00588
00589 s->coarse_snr_offset = coarse_snr_offset;
00590 for(ch=0;ch<s->nb_all_channels;ch++)
00591 s->fine_snr_offset[ch] = fine_snr_offset;
00592 #if defined(DEBUG_BITALLOC)
00593 {
00594 int j;
00595
00596 for(i=0;i<6;i++) {
00597 for(ch=0;ch<s->nb_all_channels;ch++) {
00598 printf("Block #%d Ch%d:\n", i, ch);
00599 printf("bap=");
00600 for(j=0;j<s->nb_coefs[ch];j++) {
00601 printf("%d ",bap[i][ch][j]);
00602 }
00603 printf("\n");
00604 }
00605 }
00606 }
00607 #endif
00608 return 0;
00609 }
00610
00611 static av_cold int set_channel_info(AC3EncodeContext *s, int channels,
00612 int64_t *channel_layout)
00613 {
00614 int ch_layout;
00615
00616 if (channels < 1 || channels > AC3_MAX_CHANNELS)
00617 return -1;
00618 if ((uint64_t)*channel_layout > 0x7FF)
00619 return -1;
00620 ch_layout = *channel_layout;
00621 if (!ch_layout)
00622 ch_layout = avcodec_guess_channel_layout(channels, CODEC_ID_AC3, NULL);
00623 if (avcodec_channel_layout_num_channels(ch_layout) != channels)
00624 return -1;
00625
00626 s->lfe = !!(ch_layout & CH_LOW_FREQUENCY);
00627 s->nb_all_channels = channels;
00628 s->nb_channels = channels - s->lfe;
00629 s->lfe_channel = s->lfe ? s->nb_channels : -1;
00630 if (s->lfe)
00631 ch_layout -= CH_LOW_FREQUENCY;
00632
00633 switch (ch_layout) {
00634 case CH_LAYOUT_MONO: s->channel_mode = AC3_CHMODE_MONO; break;
00635 case CH_LAYOUT_STEREO: s->channel_mode = AC3_CHMODE_STEREO; break;
00636 case CH_LAYOUT_SURROUND: s->channel_mode = AC3_CHMODE_3F; break;
00637 case CH_LAYOUT_2_1: s->channel_mode = AC3_CHMODE_2F1R; break;
00638 case CH_LAYOUT_4POINT0: s->channel_mode = AC3_CHMODE_3F1R; break;
00639 case CH_LAYOUT_QUAD:
00640 case CH_LAYOUT_2_2: s->channel_mode = AC3_CHMODE_2F2R; break;
00641 case CH_LAYOUT_5POINT0:
00642 case CH_LAYOUT_5POINT0_BACK: s->channel_mode = AC3_CHMODE_3F2R; break;
00643 default:
00644 return -1;
00645 }
00646
00647 s->channel_map = ff_ac3_enc_channel_map[s->channel_mode][s->lfe];
00648 *channel_layout = ch_layout;
00649 if (s->lfe)
00650 *channel_layout |= CH_LOW_FREQUENCY;
00651
00652 return 0;
00653 }
00654
00655 static av_cold int AC3_encode_init(AVCodecContext *avctx)
00656 {
00657 int freq = avctx->sample_rate;
00658 int bitrate = avctx->bit_rate;
00659 AC3EncodeContext *s = avctx->priv_data;
00660 int i, j, ch;
00661 float alpha;
00662 int bw_code;
00663
00664 avctx->frame_size = AC3_FRAME_SIZE;
00665
00666 ac3_common_init();
00667
00668 if (!avctx->channel_layout) {
00669 av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "
00670 "encoder will guess the layout, but it "
00671 "might be incorrect.\n");
00672 }
00673 if (set_channel_info(s, avctx->channels, &avctx->channel_layout)) {
00674 av_log(avctx, AV_LOG_ERROR, "invalid channel layout\n");
00675 return -1;
00676 }
00677
00678
00679 for(i=0;i<3;i++) {
00680 for(j=0;j<3;j++)
00681 if ((ff_ac3_sample_rate_tab[j] >> i) == freq)
00682 goto found;
00683 }
00684 return -1;
00685 found:
00686 s->sample_rate = freq;
00687 s->sr_shift = i;
00688 s->sr_code = j;
00689 s->bitstream_id = 8 + s->sr_shift;
00690 s->bitstream_mode = 0;
00691
00692
00693 for(i=0;i<19;i++) {
00694 if ((ff_ac3_bitrate_tab[i] >> s->sr_shift)*1000 == bitrate)
00695 break;
00696 }
00697 if (i == 19)
00698 return -1;
00699 s->bit_rate = bitrate;
00700 s->frame_size_code = i << 1;
00701 s->frame_size_min = ff_ac3_frame_size_tab[s->frame_size_code][s->sr_code];
00702 s->bits_written = 0;
00703 s->samples_written = 0;
00704 s->frame_size = s->frame_size_min;
00705
00706
00707 if(avctx->cutoff) {
00708
00709 int cutoff = av_clip(avctx->cutoff, 1, s->sample_rate >> 1);
00710 int fbw_coeffs = cutoff * 512 / s->sample_rate;
00711 bw_code = av_clip((fbw_coeffs - 73) / 3, 0, 60);
00712 } else {
00713
00714
00715
00716 bw_code = 50;
00717 }
00718 for(ch=0;ch<s->nb_channels;ch++) {
00719
00720 s->chbwcod[ch] = bw_code;
00721 s->nb_coefs[ch] = bw_code * 3 + 73;
00722 }
00723 if (s->lfe) {
00724 s->nb_coefs[s->lfe_channel] = 7;
00725 }
00726
00727 s->coarse_snr_offset = 40;
00728
00729
00730 fft_init(MDCT_NBITS - 2);
00731 for(i=0;i<N/4;i++) {
00732 alpha = 2 * M_PI * (i + 1.0 / 8.0) / (float)N;
00733 xcos1[i] = fix15(-cos(alpha));
00734 xsin1[i] = fix15(-sin(alpha));
00735 }
00736
00737 avctx->coded_frame= avcodec_alloc_frame();
00738 avctx->coded_frame->key_frame= 1;
00739
00740 return 0;
00741 }
00742
00743
00744 static void output_frame_header(AC3EncodeContext *s, unsigned char *frame)
00745 {
00746 init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE);
00747
00748 put_bits(&s->pb, 16, 0x0b77);
00749 put_bits(&s->pb, 16, 0);
00750 put_bits(&s->pb, 2, s->sr_code);
00751 put_bits(&s->pb, 6, s->frame_size_code + (s->frame_size - s->frame_size_min));
00752 put_bits(&s->pb, 5, s->bitstream_id);
00753 put_bits(&s->pb, 3, s->bitstream_mode);
00754 put_bits(&s->pb, 3, s->channel_mode);
00755 if ((s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO)
00756 put_bits(&s->pb, 2, 1);
00757 if (s->channel_mode & 0x04)
00758 put_bits(&s->pb, 2, 1);
00759 if (s->channel_mode == AC3_CHMODE_STEREO)
00760 put_bits(&s->pb, 2, 0);
00761 put_bits(&s->pb, 1, s->lfe);
00762 put_bits(&s->pb, 5, 31);
00763 put_bits(&s->pb, 1, 0);
00764 put_bits(&s->pb, 1, 0);
00765 put_bits(&s->pb, 1, 0);
00766 put_bits(&s->pb, 1, 0);
00767 put_bits(&s->pb, 1, 1);
00768 put_bits(&s->pb, 1, 0);
00769 put_bits(&s->pb, 1, 0);
00770 put_bits(&s->pb, 1, 0);
00771 }
00772
00773
00774 static inline int sym_quant(int c, int e, int levels)
00775 {
00776 int v;
00777
00778 if (c >= 0) {
00779 v = (levels * (c << e)) >> 24;
00780 v = (v + 1) >> 1;
00781 v = (levels >> 1) + v;
00782 } else {
00783 v = (levels * ((-c) << e)) >> 24;
00784 v = (v + 1) >> 1;
00785 v = (levels >> 1) - v;
00786 }
00787 assert (v >= 0 && v < levels);
00788 return v;
00789 }
00790
00791
00792 static inline int asym_quant(int c, int e, int qbits)
00793 {
00794 int lshift, m, v;
00795
00796 lshift = e + qbits - 24;
00797 if (lshift >= 0)
00798 v = c << lshift;
00799 else
00800 v = c >> (-lshift);
00801
00802 v = (v + 1) >> 1;
00803 m = (1 << (qbits-1));
00804 if (v >= m)
00805 v = m - 1;
00806 assert(v >= -m);
00807 return v & ((1 << qbits)-1);
00808 }
00809
00810
00811
00812 static void output_audio_block(AC3EncodeContext *s,
00813 uint8_t exp_strategy[AC3_MAX_CHANNELS],
00814 uint8_t encoded_exp[AC3_MAX_CHANNELS][N/2],
00815 uint8_t bap[AC3_MAX_CHANNELS][N/2],
00816 int32_t mdct_coefs[AC3_MAX_CHANNELS][N/2],
00817 int8_t global_exp[AC3_MAX_CHANNELS],
00818 int block_num)
00819 {
00820 int ch, nb_groups, group_size, i, baie, rbnd;
00821 uint8_t *p;
00822 uint16_t qmant[AC3_MAX_CHANNELS][N/2];
00823 int exp0, exp1;
00824 int mant1_cnt, mant2_cnt, mant4_cnt;
00825 uint16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr;
00826 int delta0, delta1, delta2;
00827
00828 for(ch=0;ch<s->nb_channels;ch++)
00829 put_bits(&s->pb, 1, 0);
00830 for(ch=0;ch<s->nb_channels;ch++)
00831 put_bits(&s->pb, 1, 1);
00832 put_bits(&s->pb, 1, 0);
00833 if (block_num == 0) {
00834
00835
00836 put_bits(&s->pb, 1, 1);
00837 put_bits(&s->pb, 1, 0);
00838 } else {
00839 put_bits(&s->pb, 1, 0);
00840 }
00841
00842 if (s->channel_mode == AC3_CHMODE_STEREO)
00843 {
00844 if(block_num==0)
00845 {
00846
00847 put_bits(&s->pb, 1, 1);
00848
00849
00850 for (rbnd=0;rbnd<4;rbnd++)
00851 put_bits(&s->pb, 1, 0);
00852 }
00853 else
00854 {
00855
00856 put_bits(&s->pb, 1, 0);
00857 }
00858 }
00859
00860 #if defined(DEBUG)
00861 {
00862 static int count = 0;
00863 av_log(NULL, AV_LOG_DEBUG, "Block #%d (%d)\n", block_num, count++);
00864 }
00865 #endif
00866
00867 for(ch=0;ch<s->nb_channels;ch++) {
00868 put_bits(&s->pb, 2, exp_strategy[ch]);
00869 }
00870
00871 if (s->lfe) {
00872 put_bits(&s->pb, 1, exp_strategy[s->lfe_channel]);
00873 }
00874
00875 for(ch=0;ch<s->nb_channels;ch++) {
00876 if (exp_strategy[ch] != EXP_REUSE)
00877 put_bits(&s->pb, 6, s->chbwcod[ch]);
00878 }
00879
00880
00881 for (ch = 0; ch < s->nb_all_channels; ch++) {
00882 switch(exp_strategy[ch]) {
00883 case EXP_REUSE:
00884 continue;
00885 case EXP_D15:
00886 group_size = 1;
00887 break;
00888 case EXP_D25:
00889 group_size = 2;
00890 break;
00891 default:
00892 case EXP_D45:
00893 group_size = 4;
00894 break;
00895 }
00896 nb_groups = (s->nb_coefs[ch] + (group_size * 3) - 4) / (3 * group_size);
00897 p = encoded_exp[ch];
00898
00899
00900 exp1 = *p++;
00901 put_bits(&s->pb, 4, exp1);
00902
00903
00904 for(i=0;i<nb_groups;i++) {
00905
00906 exp0 = exp1;
00907 exp1 = p[0];
00908 p += group_size;
00909 delta0 = exp1 - exp0 + 2;
00910
00911 exp0 = exp1;
00912 exp1 = p[0];
00913 p += group_size;
00914 delta1 = exp1 - exp0 + 2;
00915
00916 exp0 = exp1;
00917 exp1 = p[0];
00918 p += group_size;
00919 delta2 = exp1 - exp0 + 2;
00920
00921 put_bits(&s->pb, 7, ((delta0 * 5 + delta1) * 5) + delta2);
00922 }
00923
00924 if (ch != s->lfe_channel)
00925 put_bits(&s->pb, 2, 0);
00926 }
00927
00928
00929 baie = (block_num == 0);
00930 put_bits(&s->pb, 1, baie);
00931 if (baie) {
00932 put_bits(&s->pb, 2, s->slow_decay_code);
00933 put_bits(&s->pb, 2, s->fast_decay_code);
00934 put_bits(&s->pb, 2, s->slow_gain_code);
00935 put_bits(&s->pb, 2, s->db_per_bit_code);
00936 put_bits(&s->pb, 3, s->floor_code);
00937 }
00938
00939
00940 put_bits(&s->pb, 1, baie);
00941 if (baie) {
00942 put_bits(&s->pb, 6, s->coarse_snr_offset);
00943 for(ch=0;ch<s->nb_all_channels;ch++) {
00944 put_bits(&s->pb, 4, s->fine_snr_offset[ch]);
00945 put_bits(&s->pb, 3, s->fast_gain_code[ch]);
00946 }
00947 }
00948
00949 put_bits(&s->pb, 1, 0);
00950 put_bits(&s->pb, 1, 0);
00951
00952
00953
00954
00955
00956
00957 mant1_cnt = mant2_cnt = mant4_cnt = 0;
00958 qmant1_ptr = qmant2_ptr = qmant4_ptr = NULL;
00959
00960 for (ch = 0; ch < s->nb_all_channels; ch++) {
00961 int b, c, e, v;
00962
00963 for(i=0;i<s->nb_coefs[ch];i++) {
00964 c = mdct_coefs[ch][i];
00965 e = encoded_exp[ch][i] - global_exp[ch];
00966 b = bap[ch][i];
00967 switch(b) {
00968 case 0:
00969 v = 0;
00970 break;
00971 case 1:
00972 v = sym_quant(c, e, 3);
00973 switch(mant1_cnt) {
00974 case 0:
00975 qmant1_ptr = &qmant[ch][i];
00976 v = 9 * v;
00977 mant1_cnt = 1;
00978 break;
00979 case 1:
00980 *qmant1_ptr += 3 * v;
00981 mant1_cnt = 2;
00982 v = 128;
00983 break;
00984 default:
00985 *qmant1_ptr += v;
00986 mant1_cnt = 0;
00987 v = 128;
00988 break;
00989 }
00990 break;
00991 case 2:
00992 v = sym_quant(c, e, 5);
00993 switch(mant2_cnt) {
00994 case 0:
00995 qmant2_ptr = &qmant[ch][i];
00996 v = 25 * v;
00997 mant2_cnt = 1;
00998 break;
00999 case 1:
01000 *qmant2_ptr += 5 * v;
01001 mant2_cnt = 2;
01002 v = 128;
01003 break;
01004 default:
01005 *qmant2_ptr += v;
01006 mant2_cnt = 0;
01007 v = 128;
01008 break;
01009 }
01010 break;
01011 case 3:
01012 v = sym_quant(c, e, 7);
01013 break;
01014 case 4:
01015 v = sym_quant(c, e, 11);
01016 switch(mant4_cnt) {
01017 case 0:
01018 qmant4_ptr = &qmant[ch][i];
01019 v = 11 * v;
01020 mant4_cnt = 1;
01021 break;
01022 default:
01023 *qmant4_ptr += v;
01024 mant4_cnt = 0;
01025 v = 128;
01026 break;
01027 }
01028 break;
01029 case 5:
01030 v = sym_quant(c, e, 15);
01031 break;
01032 case 14:
01033 v = asym_quant(c, e, 14);
01034 break;
01035 case 15:
01036 v = asym_quant(c, e, 16);
01037 break;
01038 default:
01039 v = asym_quant(c, e, b - 1);
01040 break;
01041 }
01042 qmant[ch][i] = v;
01043 }
01044 }
01045
01046
01047 for (ch = 0; ch < s->nb_all_channels; ch++) {
01048 int b, q;
01049
01050 for(i=0;i<s->nb_coefs[ch];i++) {
01051 q = qmant[ch][i];
01052 b = bap[ch][i];
01053 switch(b) {
01054 case 0:
01055 break;
01056 case 1:
01057 if (q != 128)
01058 put_bits(&s->pb, 5, q);
01059 break;
01060 case 2:
01061 if (q != 128)
01062 put_bits(&s->pb, 7, q);
01063 break;
01064 case 3:
01065 put_bits(&s->pb, 3, q);
01066 break;
01067 case 4:
01068 if (q != 128)
01069 put_bits(&s->pb, 7, q);
01070 break;
01071 case 14:
01072 put_bits(&s->pb, 14, q);
01073 break;
01074 case 15:
01075 put_bits(&s->pb, 16, q);
01076 break;
01077 default:
01078 put_bits(&s->pb, b - 1, q);
01079 break;
01080 }
01081 }
01082 }
01083 }
01084
01085 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
01086
01087 static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly)
01088 {
01089 unsigned int c;
01090
01091 c = 0;
01092 while (a) {
01093 if (a & 1)
01094 c ^= b;
01095 a = a >> 1;
01096 b = b << 1;
01097 if (b & (1 << 16))
01098 b ^= poly;
01099 }
01100 return c;
01101 }
01102
01103 static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly)
01104 {
01105 unsigned int r;
01106 r = 1;
01107 while (n) {
01108 if (n & 1)
01109 r = mul_poly(r, a, poly);
01110 a = mul_poly(a, a, poly);
01111 n >>= 1;
01112 }
01113 return r;
01114 }
01115
01116
01117
01118 static int log2_tab(int16_t *tab, int n)
01119 {
01120 int i, v;
01121
01122 v = 0;
01123 for(i=0;i<n;i++) {
01124 v |= abs(tab[i]);
01125 }
01126 return av_log2(v);
01127 }
01128
01129 static void lshift_tab(int16_t *tab, int n, int lshift)
01130 {
01131 int i;
01132
01133 if (lshift > 0) {
01134 for(i=0;i<n;i++) {
01135 tab[i] <<= lshift;
01136 }
01137 } else if (lshift < 0) {
01138 lshift = -lshift;
01139 for(i=0;i<n;i++) {
01140 tab[i] >>= lshift;
01141 }
01142 }
01143 }
01144
01145
01146 static int output_frame_end(AC3EncodeContext *s)
01147 {
01148 int frame_size, frame_size_58, n, crc1, crc2, crc_inv;
01149 uint8_t *frame;
01150
01151 frame_size = s->frame_size;
01152
01153 flush_put_bits(&s->pb);
01154
01155 frame = s->pb.buf;
01156 n = 2 * s->frame_size - (put_bits_ptr(&s->pb) - frame) - 2;
01157 assert(n >= 0);
01158 if(n>0)
01159 memset(put_bits_ptr(&s->pb), 0, n);
01160
01161
01162
01163 frame_size_58 = (frame_size >> 1) + (frame_size >> 3);
01164 crc1 = bswap_16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0,
01165 frame + 4, 2 * frame_size_58 - 4));
01166
01167 crc_inv = pow_poly((CRC16_POLY >> 1), (16 * frame_size_58) - 16, CRC16_POLY);
01168 crc1 = mul_poly(crc_inv, crc1, CRC16_POLY);
01169 AV_WB16(frame+2,crc1);
01170
01171 crc2 = bswap_16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0,
01172 frame + 2 * frame_size_58,
01173 (frame_size - frame_size_58) * 2 - 2));
01174 AV_WB16(frame+2*frame_size-2,crc2);
01175
01176
01177 return frame_size * 2;
01178 }
01179
01180 static int AC3_encode_frame(AVCodecContext *avctx,
01181 unsigned char *frame, int buf_size, void *data)
01182 {
01183 AC3EncodeContext *s = avctx->priv_data;
01184 int16_t *samples = data;
01185 int i, j, k, v, ch;
01186 int16_t input_samples[N];
01187 int32_t mdct_coef[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
01188 uint8_t exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
01189 uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS];
01190 uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
01191 uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
01192 int8_t exp_samples[NB_BLOCKS][AC3_MAX_CHANNELS];
01193 int frame_bits;
01194
01195 frame_bits = 0;
01196 for(ch=0;ch<s->nb_all_channels;ch++) {
01197 int ich = s->channel_map[ch];
01198
01199 for(i=0;i<NB_BLOCKS;i++) {
01200 int16_t *sptr;
01201 int sinc;
01202
01203
01204 memcpy(input_samples, s->last_samples[ich], N/2 * sizeof(int16_t));
01205 sinc = s->nb_all_channels;
01206 sptr = samples + (sinc * (N/2) * i) + ich;
01207 for(j=0;j<N/2;j++) {
01208 v = *sptr;
01209 input_samples[j + N/2] = v;
01210 s->last_samples[ich][j] = v;
01211 sptr += sinc;
01212 }
01213
01214
01215 for(j=0;j<N/2;j++) {
01216 input_samples[j] = MUL16(input_samples[j],
01217 ff_ac3_window[j]) >> 15;
01218 input_samples[N-j-1] = MUL16(input_samples[N-j-1],
01219 ff_ac3_window[j]) >> 15;
01220 }
01221
01222
01223
01224 v = 14 - log2_tab(input_samples, N);
01225 if (v < 0)
01226 v = 0;
01227 exp_samples[i][ch] = v - 9;
01228 lshift_tab(input_samples, N, v);
01229
01230
01231 mdct512(mdct_coef[i][ch], input_samples);
01232
01233
01234
01235 for(j=0;j<N/2;j++) {
01236 int e;
01237 v = abs(mdct_coef[i][ch][j]);
01238 if (v == 0)
01239 e = 24;
01240 else {
01241 e = 23 - av_log2(v) + exp_samples[i][ch];
01242 if (e >= 24) {
01243 e = 24;
01244 mdct_coef[i][ch][j] = 0;
01245 }
01246 }
01247 exp[i][ch][j] = e;
01248 }
01249 }
01250
01251 compute_exp_strategy(exp_strategy, exp, ch, ch == s->lfe_channel);
01252
01253
01254
01255
01256 i = 0;
01257 while (i < NB_BLOCKS) {
01258 j = i + 1;
01259 while (j < NB_BLOCKS && exp_strategy[j][ch] == EXP_REUSE) {
01260 exponent_min(exp[i][ch], exp[j][ch], s->nb_coefs[ch]);
01261 j++;
01262 }
01263 frame_bits += encode_exp(encoded_exp[i][ch],
01264 exp[i][ch], s->nb_coefs[ch],
01265 exp_strategy[i][ch]);
01266
01267 for(k=i+1;k<j;k++) {
01268 memcpy(encoded_exp[k][ch], encoded_exp[i][ch],
01269 s->nb_coefs[ch] * sizeof(uint8_t));
01270 }
01271 i = j;
01272 }
01273 }
01274
01275
01276 while(s->bits_written >= s->bit_rate && s->samples_written >= s->sample_rate) {
01277 s->bits_written -= s->bit_rate;
01278 s->samples_written -= s->sample_rate;
01279 }
01280 s->frame_size = s->frame_size_min + (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate);
01281 s->bits_written += s->frame_size * 16;
01282 s->samples_written += AC3_FRAME_SIZE;
01283
01284 compute_bit_allocation(s, bap, encoded_exp, exp_strategy, frame_bits);
01285
01286 output_frame_header(s, frame);
01287
01288 for(i=0;i<NB_BLOCKS;i++) {
01289 output_audio_block(s, exp_strategy[i], encoded_exp[i],
01290 bap[i], mdct_coef[i], exp_samples[i], i);
01291 }
01292 return output_frame_end(s);
01293 }
01294
01295 static av_cold int AC3_encode_close(AVCodecContext *avctx)
01296 {
01297 av_freep(&avctx->coded_frame);
01298 return 0;
01299 }
01300
01301 #if 0
01302
01303
01304
01305 #undef random
01306 #define FN (N/4)
01307
01308 void fft_test(void)
01309 {
01310 IComplex in[FN], in1[FN];
01311 int k, n, i;
01312 float sum_re, sum_im, a;
01313
01314
01315
01316 for(i=0;i<FN;i++) {
01317 in[i].re = random() % 65535 - 32767;
01318 in[i].im = random() % 65535 - 32767;
01319 in1[i] = in[i];
01320 }
01321 fft(in, 7);
01322
01323
01324 for(k=0;k<FN;k++) {
01325 sum_re = 0;
01326 sum_im = 0;
01327 for(n=0;n<FN;n++) {
01328 a = -2 * M_PI * (n * k) / FN;
01329 sum_re += in1[n].re * cos(a) - in1[n].im * sin(a);
01330 sum_im += in1[n].re * sin(a) + in1[n].im * cos(a);
01331 }
01332 printf("%3d: %6d,%6d %6.0f,%6.0f\n",
01333 k, in[k].re, in[k].im, sum_re / FN, sum_im / FN);
01334 }
01335 }
01336
01337 void mdct_test(void)
01338 {
01339 int16_t input[N];
01340 int32_t output[N/2];
01341 float input1[N];
01342 float output1[N/2];
01343 float s, a, err, e, emax;
01344 int i, k, n;
01345
01346 for(i=0;i<N;i++) {
01347 input[i] = (random() % 65535 - 32767) * 9 / 10;
01348 input1[i] = input[i];
01349 }
01350
01351 mdct512(output, input);
01352
01353
01354 for(k=0;k<N/2;k++) {
01355 s = 0;
01356 for(n=0;n<N;n++) {
01357 a = (2*M_PI*(2*n+1+N/2)*(2*k+1) / (4 * N));
01358 s += input1[n] * cos(a);
01359 }
01360 output1[k] = -2 * s / N;
01361 }
01362
01363 err = 0;
01364 emax = 0;
01365 for(i=0;i<N/2;i++) {
01366 printf("%3d: %7d %7.0f\n", i, output[i], output1[i]);
01367 e = output[i] - output1[i];
01368 if (e > emax)
01369 emax = e;
01370 err += e * e;
01371 }
01372 printf("err2=%f emax=%f\n", err / (N/2), emax);
01373 }
01374
01375 void test_ac3(void)
01376 {
01377 AC3EncodeContext ctx;
01378 unsigned char frame[AC3_MAX_CODED_FRAME_SIZE];
01379 short samples[AC3_FRAME_SIZE];
01380 int ret, i;
01381
01382 AC3_encode_init(&ctx, 44100, 64000, 1);
01383
01384 fft_test();
01385 mdct_test();
01386
01387 for(i=0;i<AC3_FRAME_SIZE;i++)
01388 samples[i] = (int)(sin(2*M_PI*i*1000.0/44100) * 10000);
01389 ret = AC3_encode_frame(&ctx, frame, samples);
01390 printf("ret=%d\n", ret);
01391 }
01392 #endif
01393
01394 AVCodec ac3_encoder = {
01395 "ac3",
01396 AVMEDIA_TYPE_AUDIO,
01397 CODEC_ID_AC3,
01398 sizeof(AC3EncodeContext),
01399 AC3_encode_init,
01400 AC3_encode_frame,
01401 AC3_encode_close,
01402 NULL,
01403 .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
01404 .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52A (AC-3)"),
01405 .channel_layouts = (const int64_t[]){
01406 CH_LAYOUT_MONO,
01407 CH_LAYOUT_STEREO,
01408 CH_LAYOUT_2_1,
01409 CH_LAYOUT_SURROUND,
01410 CH_LAYOUT_2_2,
01411 CH_LAYOUT_QUAD,
01412 CH_LAYOUT_4POINT0,
01413 CH_LAYOUT_5POINT0,
01414 CH_LAYOUT_5POINT0_BACK,
01415 (CH_LAYOUT_MONO | CH_LOW_FREQUENCY),
01416 (CH_LAYOUT_STEREO | CH_LOW_FREQUENCY),
01417 (CH_LAYOUT_2_1 | CH_LOW_FREQUENCY),
01418 (CH_LAYOUT_SURROUND | CH_LOW_FREQUENCY),
01419 (CH_LAYOUT_2_2 | CH_LOW_FREQUENCY),
01420 (CH_LAYOUT_QUAD | CH_LOW_FREQUENCY),
01421 (CH_LAYOUT_4POINT0 | CH_LOW_FREQUENCY),
01422 CH_LAYOUT_5POINT1,
01423 CH_LAYOUT_5POINT1_BACK,
01424 0 },
01425 };