00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00029
00030
00031 #include <stdint.h>
00032
00033 #include "libavutil/audioconvert.h"
00034 #include "libavutil/avassert.h"
00035 #include "libavutil/avstring.h"
00036 #include "libavutil/crc.h"
00037 #include "libavutil/opt.h"
00038 #include "avcodec.h"
00039 #include "put_bits.h"
00040 #include "dsputil.h"
00041 #include "ac3dsp.h"
00042 #include "ac3.h"
00043 #include "audioconvert.h"
00044 #include "fft.h"
00045 #include "ac3enc.h"
00046 #include "eac3enc.h"
00047
00048 typedef struct AC3Mant {
00049 int16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr;
00050 int mant1_cnt, mant2_cnt, mant4_cnt;
00051 } AC3Mant;
00052
00053 #define CMIXLEV_NUM_OPTIONS 3
00054 static const float cmixlev_options[CMIXLEV_NUM_OPTIONS] = {
00055 LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB, LEVEL_MINUS_6DB
00056 };
00057
00058 #define SURMIXLEV_NUM_OPTIONS 3
00059 static const float surmixlev_options[SURMIXLEV_NUM_OPTIONS] = {
00060 LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO
00061 };
00062
00063 #define EXTMIXLEV_NUM_OPTIONS 8
00064 static const float extmixlev_options[EXTMIXLEV_NUM_OPTIONS] = {
00065 LEVEL_PLUS_3DB, LEVEL_PLUS_1POINT5DB, LEVEL_ONE, LEVEL_MINUS_4POINT5DB,
00066 LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB, LEVEL_MINUS_6DB, LEVEL_ZERO
00067 };
00068
00069
00074 static uint8_t exponent_group_tab[2][3][256];
00075
00076
00080 const uint64_t ff_ac3_channel_layouts[19] = {
00081 AV_CH_LAYOUT_MONO,
00082 AV_CH_LAYOUT_STEREO,
00083 AV_CH_LAYOUT_2_1,
00084 AV_CH_LAYOUT_SURROUND,
00085 AV_CH_LAYOUT_2_2,
00086 AV_CH_LAYOUT_QUAD,
00087 AV_CH_LAYOUT_4POINT0,
00088 AV_CH_LAYOUT_5POINT0,
00089 AV_CH_LAYOUT_5POINT0_BACK,
00090 (AV_CH_LAYOUT_MONO | AV_CH_LOW_FREQUENCY),
00091 (AV_CH_LAYOUT_STEREO | AV_CH_LOW_FREQUENCY),
00092 (AV_CH_LAYOUT_2_1 | AV_CH_LOW_FREQUENCY),
00093 (AV_CH_LAYOUT_SURROUND | AV_CH_LOW_FREQUENCY),
00094 (AV_CH_LAYOUT_2_2 | AV_CH_LOW_FREQUENCY),
00095 (AV_CH_LAYOUT_QUAD | AV_CH_LOW_FREQUENCY),
00096 (AV_CH_LAYOUT_4POINT0 | AV_CH_LOW_FREQUENCY),
00097 AV_CH_LAYOUT_5POINT1,
00098 AV_CH_LAYOUT_5POINT1_BACK,
00099 0
00100 };
00101
00102
00108 static const uint8_t ac3_bandwidth_tab[5][3][19] = {
00109
00110
00111 { { 0, 0, 0, 12, 16, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
00112 { 0, 0, 0, 16, 20, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
00113 { 0, 0, 0, 32, 40, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
00114
00115 { { 0, 0, 0, 0, 0, 0, 0, 20, 24, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
00116 { 0, 0, 0, 0, 0, 0, 4, 24, 28, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
00117 { 0, 0, 0, 0, 0, 0, 20, 44, 52, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
00118
00119 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 24, 32, 40, 48, 48, 48, 48, 48, 48 },
00120 { 0, 0, 0, 0, 0, 0, 0, 0, 4, 20, 28, 36, 44, 56, 56, 56, 56, 56, 56 },
00121 { 0, 0, 0, 0, 0, 0, 0, 0, 20, 40, 48, 60, 60, 60, 60, 60, 60, 60, 60 } },
00122
00123 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 32, 48, 48, 48, 48, 48, 48 },
00124 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 28, 36, 56, 56, 56, 56, 56, 56 },
00125 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 48, 60, 60, 60, 60, 60, 60, 60 } },
00126
00127 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 20, 32, 40, 48, 48, 48, 48 },
00128 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 36, 44, 56, 56, 56, 56 },
00129 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 44, 60, 60, 60, 60, 60, 60 } }
00130 };
00131
00132
00141 static const int8_t ac3_coupling_start_tab[6][3][19] = {
00142
00143
00144
00145 { { 0, 0, 0, 0, 0, 0, 0, 1, 1, 7, 8, 11, 12, -1, -1, -1, -1, -1, -1 },
00146 { 0, 0, 0, 0, 0, 0, 1, 3, 5, 7, 10, 12, 13, -1, -1, -1, -1, -1, -1 },
00147 { 0, 0, 0, 0, 1, 2, 2, 9, 13, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
00148
00149
00150 { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
00151 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
00152 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
00153
00154
00155 { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
00156 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
00157 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
00158
00159
00160 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
00161 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
00162 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
00163
00164
00165 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
00166 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
00167 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
00168
00169
00170 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
00171 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
00172 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
00173 };
00174
00175
00182 void ff_ac3_adjust_frame_size(AC3EncodeContext *s)
00183 {
00184 while (s->bits_written >= s->bit_rate && s->samples_written >= s->sample_rate) {
00185 s->bits_written -= s->bit_rate;
00186 s->samples_written -= s->sample_rate;
00187 }
00188 s->frame_size = s->frame_size_min +
00189 2 * (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate);
00190 s->bits_written += s->frame_size * 8;
00191 s->samples_written += AC3_BLOCK_SIZE * s->num_blocks;
00192 }
00193
00194
00200 void ff_ac3_compute_coupling_strategy(AC3EncodeContext *s)
00201 {
00202 int blk, ch;
00203 int got_cpl_snr;
00204 int num_cpl_blocks;
00205
00206
00207
00208 for (blk = 0; blk < s->num_blocks; blk++) {
00209 AC3Block *block = &s->blocks[blk];
00210 for (ch = 1; ch <= s->fbw_channels; ch++)
00211 block->channel_in_cpl[ch] = s->cpl_on;
00212 }
00213
00214
00215
00216 got_cpl_snr = 0;
00217 num_cpl_blocks = 0;
00218 for (blk = 0; blk < s->num_blocks; blk++) {
00219 AC3Block *block = &s->blocks[blk];
00220 block->num_cpl_channels = 0;
00221 for (ch = 1; ch <= s->fbw_channels; ch++)
00222 block->num_cpl_channels += block->channel_in_cpl[ch];
00223 block->cpl_in_use = block->num_cpl_channels > 1;
00224 num_cpl_blocks += block->cpl_in_use;
00225 if (!block->cpl_in_use) {
00226 block->num_cpl_channels = 0;
00227 for (ch = 1; ch <= s->fbw_channels; ch++)
00228 block->channel_in_cpl[ch] = 0;
00229 }
00230
00231 block->new_cpl_strategy = !blk;
00232 if (blk) {
00233 for (ch = 1; ch <= s->fbw_channels; ch++) {
00234 if (block->channel_in_cpl[ch] != s->blocks[blk-1].channel_in_cpl[ch]) {
00235 block->new_cpl_strategy = 1;
00236 break;
00237 }
00238 }
00239 }
00240 block->new_cpl_leak = block->new_cpl_strategy;
00241
00242 if (!blk || (block->cpl_in_use && !got_cpl_snr)) {
00243 block->new_snr_offsets = 1;
00244 if (block->cpl_in_use)
00245 got_cpl_snr = 1;
00246 } else {
00247 block->new_snr_offsets = 0;
00248 }
00249 }
00250 if (!num_cpl_blocks)
00251 s->cpl_on = 0;
00252
00253
00254 for (blk = 0; blk < s->num_blocks; blk++) {
00255 AC3Block *block = &s->blocks[blk];
00256 for (ch = 1; ch <= s->fbw_channels; ch++) {
00257 if (block->channel_in_cpl[ch])
00258 block->end_freq[ch] = s->start_freq[CPL_CH];
00259 else
00260 block->end_freq[ch] = s->bandwidth_code * 3 + 73;
00261 }
00262 }
00263 }
00264
00265
00271 void ff_ac3_apply_rematrixing(AC3EncodeContext *s)
00272 {
00273 int nb_coefs;
00274 int blk, bnd, i;
00275 int start, end;
00276 uint8_t *flags;
00277
00278 if (!s->rematrixing_enabled)
00279 return;
00280
00281 for (blk = 0; blk < s->num_blocks; blk++) {
00282 AC3Block *block = &s->blocks[blk];
00283 if (block->new_rematrixing_strategy)
00284 flags = block->rematrixing_flags;
00285 nb_coefs = FFMIN(block->end_freq[1], block->end_freq[2]);
00286 for (bnd = 0; bnd < block->num_rematrixing_bands; bnd++) {
00287 if (flags[bnd]) {
00288 start = ff_ac3_rematrix_band_tab[bnd];
00289 end = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]);
00290 for (i = start; i < end; i++) {
00291 int32_t lt = block->fixed_coef[1][i];
00292 int32_t rt = block->fixed_coef[2][i];
00293 block->fixed_coef[1][i] = (lt + rt) >> 1;
00294 block->fixed_coef[2][i] = (lt - rt) >> 1;
00295 }
00296 }
00297 }
00298 }
00299 }
00300
00301
00302
00303
00304
00305 static av_cold void exponent_init(AC3EncodeContext *s)
00306 {
00307 int expstr, i, grpsize;
00308
00309 for (expstr = EXP_D15-1; expstr <= EXP_D45-1; expstr++) {
00310 grpsize = 3 << expstr;
00311 for (i = 12; i < 256; i++) {
00312 exponent_group_tab[0][expstr][i] = (i + grpsize - 4) / grpsize;
00313 exponent_group_tab[1][expstr][i] = (i ) / grpsize;
00314 }
00315 }
00316
00317 exponent_group_tab[0][0][7] = 2;
00318
00319 if (CONFIG_EAC3_ENCODER && s->eac3)
00320 ff_eac3_exponent_init();
00321 }
00322
00323
00324
00325
00326
00327 static void extract_exponents(AC3EncodeContext *s)
00328 {
00329 int ch = !s->cpl_on;
00330 int chan_size = AC3_MAX_COEFS * s->num_blocks * (s->channels - ch + 1);
00331 AC3Block *block = &s->blocks[0];
00332
00333 s->ac3dsp.extract_exponents(block->exp[ch], block->fixed_coef[ch], chan_size);
00334 }
00335
00336
00341 #define EXP_DIFF_THRESHOLD 500
00342
00346 static const uint8_t exp_strategy_reuse_tab[4][6] = {
00347 { EXP_D15, EXP_D15, EXP_D15, EXP_D15, EXP_D15, EXP_D15 },
00348 { EXP_D15, EXP_D15, EXP_D15, EXP_D15, EXP_D15, EXP_D15 },
00349 { EXP_D25, EXP_D25, EXP_D15, EXP_D15, EXP_D15, EXP_D15 },
00350 { EXP_D45, EXP_D25, EXP_D25, EXP_D15, EXP_D15, EXP_D15 }
00351 };
00352
00353
00354
00355
00356
00357 static void compute_exp_strategy(AC3EncodeContext *s)
00358 {
00359 int ch, blk, blk1;
00360
00361 for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++) {
00362 uint8_t *exp_strategy = s->exp_strategy[ch];
00363 uint8_t *exp = s->blocks[0].exp[ch];
00364 int exp_diff;
00365
00366
00367
00368 exp_strategy[0] = EXP_NEW;
00369 exp += AC3_MAX_COEFS;
00370 for (blk = 1; blk < s->num_blocks; blk++, exp += AC3_MAX_COEFS) {
00371 if (ch == CPL_CH) {
00372 if (!s->blocks[blk-1].cpl_in_use) {
00373 exp_strategy[blk] = EXP_NEW;
00374 continue;
00375 } else if (!s->blocks[blk].cpl_in_use) {
00376 exp_strategy[blk] = EXP_REUSE;
00377 continue;
00378 }
00379 } else if (s->blocks[blk].channel_in_cpl[ch] != s->blocks[blk-1].channel_in_cpl[ch]) {
00380 exp_strategy[blk] = EXP_NEW;
00381 continue;
00382 }
00383 exp_diff = s->dsp.sad[0](NULL, exp, exp - AC3_MAX_COEFS, 16, 16);
00384 exp_strategy[blk] = EXP_REUSE;
00385 if (ch == CPL_CH && exp_diff > (EXP_DIFF_THRESHOLD * (s->blocks[blk].end_freq[ch] - s->start_freq[ch]) / AC3_MAX_COEFS))
00386 exp_strategy[blk] = EXP_NEW;
00387 else if (ch > CPL_CH && exp_diff > EXP_DIFF_THRESHOLD)
00388 exp_strategy[blk] = EXP_NEW;
00389 }
00390
00391
00392
00393 blk = 0;
00394 while (blk < s->num_blocks) {
00395 blk1 = blk + 1;
00396 while (blk1 < s->num_blocks && exp_strategy[blk1] == EXP_REUSE)
00397 blk1++;
00398 exp_strategy[blk] = exp_strategy_reuse_tab[s->num_blks_code][blk1-blk-1];
00399 blk = blk1;
00400 }
00401 }
00402 if (s->lfe_on) {
00403 ch = s->lfe_channel;
00404 s->exp_strategy[ch][0] = EXP_D15;
00405 for (blk = 1; blk < s->num_blocks; blk++)
00406 s->exp_strategy[ch][blk] = EXP_REUSE;
00407 }
00408
00409
00410 if (CONFIG_EAC3_ENCODER && s->eac3)
00411 ff_eac3_get_frame_exp_strategy(s);
00412 }
00413
00414
00423 static void encode_exponents_blk_ch(uint8_t *exp, int nb_exps, int exp_strategy,
00424 int cpl)
00425 {
00426 int nb_groups, i, k;
00427
00428 nb_groups = exponent_group_tab[cpl][exp_strategy-1][nb_exps] * 3;
00429
00430
00431 switch(exp_strategy) {
00432 case EXP_D25:
00433 for (i = 1, k = 1-cpl; i <= nb_groups; i++) {
00434 uint8_t exp_min = exp[k];
00435 if (exp[k+1] < exp_min)
00436 exp_min = exp[k+1];
00437 exp[i-cpl] = exp_min;
00438 k += 2;
00439 }
00440 break;
00441 case EXP_D45:
00442 for (i = 1, k = 1-cpl; i <= nb_groups; i++) {
00443 uint8_t exp_min = exp[k];
00444 if (exp[k+1] < exp_min)
00445 exp_min = exp[k+1];
00446 if (exp[k+2] < exp_min)
00447 exp_min = exp[k+2];
00448 if (exp[k+3] < exp_min)
00449 exp_min = exp[k+3];
00450 exp[i-cpl] = exp_min;
00451 k += 4;
00452 }
00453 break;
00454 }
00455
00456
00457 if (!cpl && exp[0] > 15)
00458 exp[0] = 15;
00459
00460
00461
00462 for (i = 1; i <= nb_groups; i++)
00463 exp[i] = FFMIN(exp[i], exp[i-1] + 2);
00464 i--;
00465 while (--i >= 0)
00466 exp[i] = FFMIN(exp[i], exp[i+1] + 2);
00467
00468 if (cpl)
00469 exp[-1] = exp[0] & ~1;
00470
00471
00472 switch (exp_strategy) {
00473 case EXP_D25:
00474 for (i = nb_groups, k = (nb_groups * 2)-cpl; i > 0; i--) {
00475 uint8_t exp1 = exp[i-cpl];
00476 exp[k--] = exp1;
00477 exp[k--] = exp1;
00478 }
00479 break;
00480 case EXP_D45:
00481 for (i = nb_groups, k = (nb_groups * 4)-cpl; i > 0; i--) {
00482 exp[k] = exp[k-1] = exp[k-2] = exp[k-3] = exp[i-cpl];
00483 k -= 4;
00484 }
00485 break;
00486 }
00487 }
00488
00489
00490
00491
00492
00493
00494
00495
00496 static void encode_exponents(AC3EncodeContext *s)
00497 {
00498 int blk, blk1, ch, cpl;
00499 uint8_t *exp, *exp_strategy;
00500 int nb_coefs, num_reuse_blocks;
00501
00502 for (ch = !s->cpl_on; ch <= s->channels; ch++) {
00503 exp = s->blocks[0].exp[ch] + s->start_freq[ch];
00504 exp_strategy = s->exp_strategy[ch];
00505
00506 cpl = (ch == CPL_CH);
00507 blk = 0;
00508 while (blk < s->num_blocks) {
00509 AC3Block *block = &s->blocks[blk];
00510 if (cpl && !block->cpl_in_use) {
00511 exp += AC3_MAX_COEFS;
00512 blk++;
00513 continue;
00514 }
00515 nb_coefs = block->end_freq[ch] - s->start_freq[ch];
00516 blk1 = blk + 1;
00517
00518
00519
00520 s->exp_ref_block[ch][blk] = blk;
00521 while (blk1 < s->num_blocks && exp_strategy[blk1] == EXP_REUSE) {
00522 s->exp_ref_block[ch][blk1] = blk;
00523 blk1++;
00524 }
00525 num_reuse_blocks = blk1 - blk - 1;
00526
00527
00528 s->ac3dsp.ac3_exponent_min(exp-s->start_freq[ch], num_reuse_blocks,
00529 AC3_MAX_COEFS);
00530
00531 encode_exponents_blk_ch(exp, nb_coefs, exp_strategy[blk], cpl);
00532
00533 exp += AC3_MAX_COEFS * (num_reuse_blocks + 1);
00534 blk = blk1;
00535 }
00536 }
00537
00538
00539 s->ref_bap_set = 0;
00540 }
00541
00542
00543
00544
00545
00546 static int count_exponent_bits(AC3EncodeContext *s)
00547 {
00548 int blk, ch;
00549 int nb_groups, bit_count;
00550
00551 bit_count = 0;
00552 for (blk = 0; blk < s->num_blocks; blk++) {
00553 AC3Block *block = &s->blocks[blk];
00554 for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
00555 int exp_strategy = s->exp_strategy[ch][blk];
00556 int cpl = (ch == CPL_CH);
00557 int nb_coefs = block->end_freq[ch] - s->start_freq[ch];
00558
00559 if (exp_strategy == EXP_REUSE)
00560 continue;
00561
00562 nb_groups = exponent_group_tab[cpl][exp_strategy-1][nb_coefs];
00563 bit_count += 4 + (nb_groups * 7);
00564 }
00565 }
00566
00567 return bit_count;
00568 }
00569
00570
00578 void ff_ac3_group_exponents(AC3EncodeContext *s)
00579 {
00580 int blk, ch, i, cpl;
00581 int group_size, nb_groups;
00582 uint8_t *p;
00583 int delta0, delta1, delta2;
00584 int exp0, exp1;
00585
00586 for (blk = 0; blk < s->num_blocks; blk++) {
00587 AC3Block *block = &s->blocks[blk];
00588 for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
00589 int exp_strategy = s->exp_strategy[ch][blk];
00590 if (exp_strategy == EXP_REUSE)
00591 continue;
00592 cpl = (ch == CPL_CH);
00593 group_size = exp_strategy + (exp_strategy == EXP_D45);
00594 nb_groups = exponent_group_tab[cpl][exp_strategy-1][block->end_freq[ch]-s->start_freq[ch]];
00595 p = block->exp[ch] + s->start_freq[ch] - cpl;
00596
00597
00598 exp1 = *p++;
00599 block->grouped_exp[ch][0] = exp1;
00600
00601
00602 for (i = 1; i <= nb_groups; i++) {
00603
00604 exp0 = exp1;
00605 exp1 = p[0];
00606 p += group_size;
00607 delta0 = exp1 - exp0 + 2;
00608 av_assert2(delta0 >= 0 && delta0 <= 4);
00609
00610 exp0 = exp1;
00611 exp1 = p[0];
00612 p += group_size;
00613 delta1 = exp1 - exp0 + 2;
00614 av_assert2(delta1 >= 0 && delta1 <= 4);
00615
00616 exp0 = exp1;
00617 exp1 = p[0];
00618 p += group_size;
00619 delta2 = exp1 - exp0 + 2;
00620 av_assert2(delta2 >= 0 && delta2 <= 4);
00621
00622 block->grouped_exp[ch][i] = ((delta0 * 5 + delta1) * 5) + delta2;
00623 }
00624 }
00625 }
00626 }
00627
00628
00636 void ff_ac3_process_exponents(AC3EncodeContext *s)
00637 {
00638 extract_exponents(s);
00639
00640 compute_exp_strategy(s);
00641
00642 encode_exponents(s);
00643
00644 emms_c();
00645 }
00646
00647
00648
00649
00650
00651
00652 static void count_frame_bits_fixed(AC3EncodeContext *s)
00653 {
00654 static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
00655 int blk;
00656 int frame_bits;
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668 frame_bits = 16;
00669 if (s->eac3) {
00670
00671 frame_bits += 35;
00672 frame_bits += 1 + 1;
00673 if (s->num_blocks != 0x6)
00674 frame_bits++;
00675 frame_bits++;
00676
00677 if (s->num_blocks == 6)
00678 frame_bits += 2;
00679 frame_bits += 10;
00680
00681 if (s->use_frame_exp_strategy)
00682 frame_bits += 5 * s->fbw_channels;
00683 else
00684 frame_bits += s->num_blocks * 2 * s->fbw_channels;
00685 if (s->lfe_on)
00686 frame_bits += s->num_blocks;
00687
00688 if (s->num_blks_code != 0x3)
00689 frame_bits++;
00690 else
00691 frame_bits += s->fbw_channels * 5;
00692
00693 frame_bits += 10;
00694
00695 if (s->num_blocks != 1)
00696 frame_bits++;
00697 } else {
00698 frame_bits += 49;
00699 frame_bits += frame_bits_inc[s->channel_mode];
00700 }
00701
00702
00703 for (blk = 0; blk < s->num_blocks; blk++) {
00704 if (!s->eac3) {
00705
00706 frame_bits += s->fbw_channels;
00707
00708
00709 frame_bits += s->fbw_channels;
00710 }
00711
00712
00713 frame_bits++;
00714
00715
00716 if (s->eac3)
00717 frame_bits++;
00718
00719 if (!s->eac3) {
00720
00721 frame_bits += 2 * s->fbw_channels;
00722 if (s->lfe_on)
00723 frame_bits++;
00724
00725
00726 frame_bits++;
00727 if (!blk)
00728 frame_bits += 2 + 2 + 2 + 2 + 3;
00729 }
00730
00731
00732 if (s->eac3)
00733 frame_bits++;
00734
00735 if (!s->eac3) {
00736
00737 frame_bits++;
00738
00739
00740 frame_bits++;
00741 }
00742 }
00743
00744
00745 frame_bits++;
00746
00747
00748 frame_bits += 1 + 16;
00749
00750 s->frame_bits_fixed = frame_bits;
00751 }
00752
00753
00754
00755
00756
00757
00758 static void bit_alloc_init(AC3EncodeContext *s)
00759 {
00760 int ch;
00761
00762
00763 s->slow_decay_code = 2;
00764 s->fast_decay_code = 1;
00765 s->slow_gain_code = 1;
00766 s->db_per_bit_code = s->eac3 ? 2 : 3;
00767 s->floor_code = 7;
00768 for (ch = 0; ch <= s->channels; ch++)
00769 s->fast_gain_code[ch] = 4;
00770
00771
00772 s->coarse_snr_offset = 40;
00773
00774
00775
00776
00777 s->bit_alloc.slow_decay = ff_ac3_slow_decay_tab[s->slow_decay_code] >> s->bit_alloc.sr_shift;
00778 s->bit_alloc.fast_decay = ff_ac3_fast_decay_tab[s->fast_decay_code] >> s->bit_alloc.sr_shift;
00779 s->bit_alloc.slow_gain = ff_ac3_slow_gain_tab[s->slow_gain_code];
00780 s->bit_alloc.db_per_bit = ff_ac3_db_per_bit_tab[s->db_per_bit_code];
00781 s->bit_alloc.floor = ff_ac3_floor_tab[s->floor_code];
00782 s->bit_alloc.cpl_fast_leak = 0;
00783 s->bit_alloc.cpl_slow_leak = 0;
00784
00785 count_frame_bits_fixed(s);
00786 }
00787
00788
00789
00790
00791
00792
00793
00794 static void count_frame_bits(AC3EncodeContext *s)
00795 {
00796 AC3EncOptions *opt = &s->options;
00797 int blk, ch;
00798 int frame_bits = 0;
00799
00800
00801 if (s->eac3) {
00802 if (opt->eac3_mixing_metadata) {
00803 if (s->channel_mode > AC3_CHMODE_STEREO)
00804 frame_bits += 2;
00805 if (s->has_center)
00806 frame_bits += 6;
00807 if (s->has_surround)
00808 frame_bits += 6;
00809 frame_bits += s->lfe_on;
00810 frame_bits += 1 + 1 + 2;
00811 if (s->channel_mode < AC3_CHMODE_STEREO)
00812 frame_bits++;
00813 frame_bits++;
00814 }
00815 if (opt->eac3_info_metadata) {
00816 frame_bits += 3 + 1 + 1;
00817 if (s->channel_mode == AC3_CHMODE_STEREO)
00818 frame_bits += 2 + 2;
00819 if (s->channel_mode >= AC3_CHMODE_2F2R)
00820 frame_bits += 2;
00821 frame_bits++;
00822 if (opt->audio_production_info)
00823 frame_bits += 5 + 2 + 1;
00824 frame_bits++;
00825 }
00826
00827 if (s->channel_mode > AC3_CHMODE_MONO) {
00828 frame_bits++;
00829 for (blk = 1; blk < s->num_blocks; blk++) {
00830 AC3Block *block = &s->blocks[blk];
00831 frame_bits++;
00832 if (block->new_cpl_strategy)
00833 frame_bits++;
00834 }
00835 }
00836
00837 if (s->cpl_on) {
00838 if (s->use_frame_exp_strategy) {
00839 frame_bits += 5 * s->cpl_on;
00840 } else {
00841 for (blk = 0; blk < s->num_blocks; blk++)
00842 frame_bits += 2 * s->blocks[blk].cpl_in_use;
00843 }
00844 }
00845 } else {
00846 if (opt->audio_production_info)
00847 frame_bits += 7;
00848 if (s->bitstream_id == 6) {
00849 if (opt->extended_bsi_1)
00850 frame_bits += 14;
00851 if (opt->extended_bsi_2)
00852 frame_bits += 14;
00853 }
00854 }
00855
00856
00857 for (blk = 0; blk < s->num_blocks; blk++) {
00858 AC3Block *block = &s->blocks[blk];
00859
00860
00861 if (!s->eac3)
00862 frame_bits++;
00863 if (block->new_cpl_strategy) {
00864 if (!s->eac3)
00865 frame_bits++;
00866 if (block->cpl_in_use) {
00867 if (s->eac3)
00868 frame_bits++;
00869 if (!s->eac3 || s->channel_mode != AC3_CHMODE_STEREO)
00870 frame_bits += s->fbw_channels;
00871 if (s->channel_mode == AC3_CHMODE_STEREO)
00872 frame_bits++;
00873 frame_bits += 4 + 4;
00874 if (s->eac3)
00875 frame_bits++;
00876 else
00877 frame_bits += s->num_cpl_subbands - 1;
00878 }
00879 }
00880
00881
00882 if (block->cpl_in_use) {
00883 for (ch = 1; ch <= s->fbw_channels; ch++) {
00884 if (block->channel_in_cpl[ch]) {
00885 if (!s->eac3 || block->new_cpl_coords[ch] != 2)
00886 frame_bits++;
00887 if (block->new_cpl_coords[ch]) {
00888 frame_bits += 2;
00889 frame_bits += (4 + 4) * s->num_cpl_bands;
00890 }
00891 }
00892 }
00893 }
00894
00895
00896 if (s->channel_mode == AC3_CHMODE_STEREO) {
00897 if (!s->eac3 || blk > 0)
00898 frame_bits++;
00899 if (s->blocks[blk].new_rematrixing_strategy)
00900 frame_bits += block->num_rematrixing_bands;
00901 }
00902
00903
00904 for (ch = 1; ch <= s->fbw_channels; ch++) {
00905 if (s->exp_strategy[ch][blk] != EXP_REUSE) {
00906 if (!block->channel_in_cpl[ch])
00907 frame_bits += 6;
00908 frame_bits += 2;
00909 }
00910 }
00911
00912
00913 if (!s->eac3 && block->cpl_in_use)
00914 frame_bits += 2;
00915
00916
00917 if (!s->eac3) {
00918 frame_bits++;
00919 if (block->new_snr_offsets)
00920 frame_bits += 6 + (s->channels + block->cpl_in_use) * (4 + 3);
00921 }
00922
00923
00924 if (block->cpl_in_use) {
00925 if (!s->eac3 || block->new_cpl_leak != 2)
00926 frame_bits++;
00927 if (block->new_cpl_leak)
00928 frame_bits += 3 + 3;
00929 }
00930 }
00931
00932 s->frame_bits = s->frame_bits_fixed + frame_bits;
00933 }
00934
00935
00936
00937
00938
00939
00940 static void bit_alloc_masking(AC3EncodeContext *s)
00941 {
00942 int blk, ch;
00943
00944 for (blk = 0; blk < s->num_blocks; blk++) {
00945 AC3Block *block = &s->blocks[blk];
00946 for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
00947
00948
00949
00950 if (s->exp_strategy[ch][blk] != EXP_REUSE) {
00951 ff_ac3_bit_alloc_calc_psd(block->exp[ch], s->start_freq[ch],
00952 block->end_freq[ch], block->psd[ch],
00953 block->band_psd[ch]);
00954 ff_ac3_bit_alloc_calc_mask(&s->bit_alloc, block->band_psd[ch],
00955 s->start_freq[ch], block->end_freq[ch],
00956 ff_ac3_fast_gain_tab[s->fast_gain_code[ch]],
00957 ch == s->lfe_channel,
00958 DBA_NONE, 0, NULL, NULL, NULL,
00959 block->mask[ch]);
00960 }
00961 }
00962 }
00963 }
00964
00965
00966
00967
00968
00969
00970 static void reset_block_bap(AC3EncodeContext *s)
00971 {
00972 int blk, ch;
00973 uint8_t *ref_bap;
00974
00975 if (s->ref_bap[0][0] == s->bap_buffer && s->ref_bap_set)
00976 return;
00977
00978 ref_bap = s->bap_buffer;
00979 for (ch = 0; ch <= s->channels; ch++) {
00980 for (blk = 0; blk < s->num_blocks; blk++)
00981 s->ref_bap[ch][blk] = ref_bap + AC3_MAX_COEFS * s->exp_ref_block[ch][blk];
00982 ref_bap += AC3_MAX_COEFS * s->num_blocks;
00983 }
00984 s->ref_bap_set = 1;
00985 }
00986
00987
00995 static void count_mantissa_bits_init(uint16_t mant_cnt[AC3_MAX_BLOCKS][16])
00996 {
00997 int blk;
00998
00999 for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
01000 memset(mant_cnt[blk], 0, sizeof(mant_cnt[blk]));
01001 mant_cnt[blk][1] = mant_cnt[blk][2] = 2;
01002 mant_cnt[blk][4] = 1;
01003 }
01004 }
01005
01006
01017 static void count_mantissa_bits_update_ch(AC3EncodeContext *s, int ch,
01018 uint16_t mant_cnt[AC3_MAX_BLOCKS][16],
01019 int start, int end)
01020 {
01021 int blk;
01022
01023 for (blk = 0; blk < s->num_blocks; blk++) {
01024 AC3Block *block = &s->blocks[blk];
01025 if (ch == CPL_CH && !block->cpl_in_use)
01026 continue;
01027 s->ac3dsp.update_bap_counts(mant_cnt[blk],
01028 s->ref_bap[ch][blk] + start,
01029 FFMIN(end, block->end_freq[ch]) - start);
01030 }
01031 }
01032
01033
01034
01035
01036
01037 static int count_mantissa_bits(AC3EncodeContext *s)
01038 {
01039 int ch, max_end_freq;
01040 LOCAL_ALIGNED_16(uint16_t, mant_cnt, [AC3_MAX_BLOCKS], [16]);
01041
01042 count_mantissa_bits_init(mant_cnt);
01043
01044 max_end_freq = s->bandwidth_code * 3 + 73;
01045 for (ch = !s->cpl_enabled; ch <= s->channels; ch++)
01046 count_mantissa_bits_update_ch(s, ch, mant_cnt, s->start_freq[ch],
01047 max_end_freq);
01048
01049 return s->ac3dsp.compute_mantissa_size(mant_cnt);
01050 }
01051
01052
01063 static int bit_alloc(AC3EncodeContext *s, int snr_offset)
01064 {
01065 int blk, ch;
01066
01067 snr_offset = (snr_offset - 240) << 2;
01068
01069 reset_block_bap(s);
01070 for (blk = 0; blk < s->num_blocks; blk++) {
01071 AC3Block *block = &s->blocks[blk];
01072
01073 for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
01074
01075
01076
01077
01078 if (s->exp_strategy[ch][blk] != EXP_REUSE) {
01079 s->ac3dsp.bit_alloc_calc_bap(block->mask[ch], block->psd[ch],
01080 s->start_freq[ch], block->end_freq[ch],
01081 snr_offset, s->bit_alloc.floor,
01082 ff_ac3_bap_tab, s->ref_bap[ch][blk]);
01083 }
01084 }
01085 }
01086 return count_mantissa_bits(s);
01087 }
01088
01089
01090
01091
01092
01093
01094 static int cbr_bit_allocation(AC3EncodeContext *s)
01095 {
01096 int ch;
01097 int bits_left;
01098 int snr_offset, snr_incr;
01099
01100 bits_left = 8 * s->frame_size - (s->frame_bits + s->exponent_bits);
01101 if (bits_left < 0)
01102 return AVERROR(EINVAL);
01103
01104 snr_offset = s->coarse_snr_offset << 4;
01105
01106
01107
01108 if ((snr_offset | s->fine_snr_offset[1]) == 1023) {
01109 if (bit_alloc(s, 1023) <= bits_left)
01110 return 0;
01111 }
01112
01113 while (snr_offset >= 0 &&
01114 bit_alloc(s, snr_offset) > bits_left) {
01115 snr_offset -= 64;
01116 }
01117 if (snr_offset < 0)
01118 return AVERROR(EINVAL);
01119
01120 FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
01121 for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
01122 while (snr_offset + snr_incr <= 1023 &&
01123 bit_alloc(s, snr_offset + snr_incr) <= bits_left) {
01124 snr_offset += snr_incr;
01125 FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
01126 }
01127 }
01128 FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
01129 reset_block_bap(s);
01130
01131 s->coarse_snr_offset = snr_offset >> 4;
01132 for (ch = !s->cpl_on; ch <= s->channels; ch++)
01133 s->fine_snr_offset[ch] = snr_offset & 0xF;
01134
01135 return 0;
01136 }
01137
01138
01139
01140
01141
01142
01143
01144
01145 int ff_ac3_compute_bit_allocation(AC3EncodeContext *s)
01146 {
01147 count_frame_bits(s);
01148
01149 s->exponent_bits = count_exponent_bits(s);
01150
01151 bit_alloc_masking(s);
01152
01153 return cbr_bit_allocation(s);
01154 }
01155
01156
01165 static inline int sym_quant(int c, int e, int levels)
01166 {
01167 int v = (((levels * c) >> (24 - e)) + levels) >> 1;
01168 av_assert2(v >= 0 && v < levels);
01169 return v;
01170 }
01171
01172
01181 static inline int asym_quant(int c, int e, int qbits)
01182 {
01183 int m;
01184
01185 c = (((c << e) >> (24 - qbits)) + 1) >> 1;
01186 m = (1 << (qbits-1));
01187 if (c >= m)
01188 c = m - 1;
01189 av_assert2(c >= -m);
01190 return c;
01191 }
01192
01193
01205 static void quantize_mantissas_blk_ch(AC3Mant *s, int32_t *fixed_coef,
01206 uint8_t *exp, uint8_t *bap,
01207 int16_t *qmant, int start_freq,
01208 int end_freq)
01209 {
01210 int i;
01211
01212 for (i = start_freq; i < end_freq; i++) {
01213 int c = fixed_coef[i];
01214 int e = exp[i];
01215 int v = bap[i];
01216 if (v)
01217 switch (v) {
01218 case 1:
01219 v = sym_quant(c, e, 3);
01220 switch (s->mant1_cnt) {
01221 case 0:
01222 s->qmant1_ptr = &qmant[i];
01223 v = 9 * v;
01224 s->mant1_cnt = 1;
01225 break;
01226 case 1:
01227 *s->qmant1_ptr += 3 * v;
01228 s->mant1_cnt = 2;
01229 v = 128;
01230 break;
01231 default:
01232 *s->qmant1_ptr += v;
01233 s->mant1_cnt = 0;
01234 v = 128;
01235 break;
01236 }
01237 break;
01238 case 2:
01239 v = sym_quant(c, e, 5);
01240 switch (s->mant2_cnt) {
01241 case 0:
01242 s->qmant2_ptr = &qmant[i];
01243 v = 25 * v;
01244 s->mant2_cnt = 1;
01245 break;
01246 case 1:
01247 *s->qmant2_ptr += 5 * v;
01248 s->mant2_cnt = 2;
01249 v = 128;
01250 break;
01251 default:
01252 *s->qmant2_ptr += v;
01253 s->mant2_cnt = 0;
01254 v = 128;
01255 break;
01256 }
01257 break;
01258 case 3:
01259 v = sym_quant(c, e, 7);
01260 break;
01261 case 4:
01262 v = sym_quant(c, e, 11);
01263 switch (s->mant4_cnt) {
01264 case 0:
01265 s->qmant4_ptr = &qmant[i];
01266 v = 11 * v;
01267 s->mant4_cnt = 1;
01268 break;
01269 default:
01270 *s->qmant4_ptr += v;
01271 s->mant4_cnt = 0;
01272 v = 128;
01273 break;
01274 }
01275 break;
01276 case 5:
01277 v = sym_quant(c, e, 15);
01278 break;
01279 case 14:
01280 v = asym_quant(c, e, 14);
01281 break;
01282 case 15:
01283 v = asym_quant(c, e, 16);
01284 break;
01285 default:
01286 v = asym_quant(c, e, v - 1);
01287 break;
01288 }
01289 qmant[i] = v;
01290 }
01291 }
01292
01293
01299 void ff_ac3_quantize_mantissas(AC3EncodeContext *s)
01300 {
01301 int blk, ch, ch0=0, got_cpl;
01302
01303 for (blk = 0; blk < s->num_blocks; blk++) {
01304 AC3Block *block = &s->blocks[blk];
01305 AC3Mant m = { 0 };
01306
01307 got_cpl = !block->cpl_in_use;
01308 for (ch = 1; ch <= s->channels; ch++) {
01309 if (!got_cpl && ch > 1 && block->channel_in_cpl[ch-1]) {
01310 ch0 = ch - 1;
01311 ch = CPL_CH;
01312 got_cpl = 1;
01313 }
01314 quantize_mantissas_blk_ch(&m, block->fixed_coef[ch],
01315 s->blocks[s->exp_ref_block[ch][blk]].exp[ch],
01316 s->ref_bap[ch][blk], block->qmant[ch],
01317 s->start_freq[ch], block->end_freq[ch]);
01318 if (ch == CPL_CH)
01319 ch = ch0;
01320 }
01321 }
01322 }
01323
01324
01325
01326
01327
01328 static void ac3_output_frame_header(AC3EncodeContext *s)
01329 {
01330 AC3EncOptions *opt = &s->options;
01331
01332 put_bits(&s->pb, 16, 0x0b77);
01333 put_bits(&s->pb, 16, 0);
01334 put_bits(&s->pb, 2, s->bit_alloc.sr_code);
01335 put_bits(&s->pb, 6, s->frame_size_code + (s->frame_size - s->frame_size_min) / 2);
01336 put_bits(&s->pb, 5, s->bitstream_id);
01337 put_bits(&s->pb, 3, s->bitstream_mode);
01338 put_bits(&s->pb, 3, s->channel_mode);
01339 if ((s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO)
01340 put_bits(&s->pb, 2, s->center_mix_level);
01341 if (s->channel_mode & 0x04)
01342 put_bits(&s->pb, 2, s->surround_mix_level);
01343 if (s->channel_mode == AC3_CHMODE_STEREO)
01344 put_bits(&s->pb, 2, opt->dolby_surround_mode);
01345 put_bits(&s->pb, 1, s->lfe_on);
01346 put_bits(&s->pb, 5, -opt->dialogue_level);
01347 put_bits(&s->pb, 1, 0);
01348 put_bits(&s->pb, 1, 0);
01349 put_bits(&s->pb, 1, opt->audio_production_info);
01350 if (opt->audio_production_info) {
01351 put_bits(&s->pb, 5, opt->mixing_level - 80);
01352 put_bits(&s->pb, 2, opt->room_type);
01353 }
01354 put_bits(&s->pb, 1, opt->copyright);
01355 put_bits(&s->pb, 1, opt->original);
01356 if (s->bitstream_id == 6) {
01357
01358 put_bits(&s->pb, 1, opt->extended_bsi_1);
01359 if (opt->extended_bsi_1) {
01360 put_bits(&s->pb, 2, opt->preferred_stereo_downmix);
01361 put_bits(&s->pb, 3, s->ltrt_center_mix_level);
01362 put_bits(&s->pb, 3, s->ltrt_surround_mix_level);
01363 put_bits(&s->pb, 3, s->loro_center_mix_level);
01364 put_bits(&s->pb, 3, s->loro_surround_mix_level);
01365 }
01366 put_bits(&s->pb, 1, opt->extended_bsi_2);
01367 if (opt->extended_bsi_2) {
01368 put_bits(&s->pb, 2, opt->dolby_surround_ex_mode);
01369 put_bits(&s->pb, 2, opt->dolby_headphone_mode);
01370 put_bits(&s->pb, 1, opt->ad_converter_type);
01371 put_bits(&s->pb, 9, 0);
01372 }
01373 } else {
01374 put_bits(&s->pb, 1, 0);
01375 put_bits(&s->pb, 1, 0);
01376 }
01377 put_bits(&s->pb, 1, 0);
01378 }
01379
01380
01381
01382
01383
01384 static void output_audio_block(AC3EncodeContext *s, int blk)
01385 {
01386 int ch, i, baie, bnd, got_cpl, ch0;
01387 AC3Block *block = &s->blocks[blk];
01388
01389
01390 if (!s->eac3) {
01391 for (ch = 0; ch < s->fbw_channels; ch++)
01392 put_bits(&s->pb, 1, 0);
01393 }
01394
01395
01396 if (!s->eac3) {
01397 for (ch = 0; ch < s->fbw_channels; ch++)
01398 put_bits(&s->pb, 1, 1);
01399 }
01400
01401
01402 put_bits(&s->pb, 1, 0);
01403
01404
01405 if (s->eac3)
01406 put_bits(&s->pb, 1, 0);
01407
01408
01409 if (!s->eac3)
01410 put_bits(&s->pb, 1, block->new_cpl_strategy);
01411 if (block->new_cpl_strategy) {
01412 if (!s->eac3)
01413 put_bits(&s->pb, 1, block->cpl_in_use);
01414 if (block->cpl_in_use) {
01415 int start_sub, end_sub;
01416 if (s->eac3)
01417 put_bits(&s->pb, 1, 0);
01418 if (!s->eac3 || s->channel_mode != AC3_CHMODE_STEREO) {
01419 for (ch = 1; ch <= s->fbw_channels; ch++)
01420 put_bits(&s->pb, 1, block->channel_in_cpl[ch]);
01421 }
01422 if (s->channel_mode == AC3_CHMODE_STEREO)
01423 put_bits(&s->pb, 1, 0);
01424 start_sub = (s->start_freq[CPL_CH] - 37) / 12;
01425 end_sub = (s->cpl_end_freq - 37) / 12;
01426 put_bits(&s->pb, 4, start_sub);
01427 put_bits(&s->pb, 4, end_sub - 3);
01428
01429 if (s->eac3) {
01430 put_bits(&s->pb, 1, 0);
01431 } else {
01432 for (bnd = start_sub+1; bnd < end_sub; bnd++)
01433 put_bits(&s->pb, 1, ff_eac3_default_cpl_band_struct[bnd]);
01434 }
01435 }
01436 }
01437
01438
01439 if (block->cpl_in_use) {
01440 for (ch = 1; ch <= s->fbw_channels; ch++) {
01441 if (block->channel_in_cpl[ch]) {
01442 if (!s->eac3 || block->new_cpl_coords[ch] != 2)
01443 put_bits(&s->pb, 1, block->new_cpl_coords[ch]);
01444 if (block->new_cpl_coords[ch]) {
01445 put_bits(&s->pb, 2, block->cpl_master_exp[ch]);
01446 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
01447 put_bits(&s->pb, 4, block->cpl_coord_exp [ch][bnd]);
01448 put_bits(&s->pb, 4, block->cpl_coord_mant[ch][bnd]);
01449 }
01450 }
01451 }
01452 }
01453 }
01454
01455
01456 if (s->channel_mode == AC3_CHMODE_STEREO) {
01457 if (!s->eac3 || blk > 0)
01458 put_bits(&s->pb, 1, block->new_rematrixing_strategy);
01459 if (block->new_rematrixing_strategy) {
01460
01461 for (bnd = 0; bnd < block->num_rematrixing_bands; bnd++)
01462 put_bits(&s->pb, 1, block->rematrixing_flags[bnd]);
01463 }
01464 }
01465
01466
01467 if (!s->eac3) {
01468 for (ch = !block->cpl_in_use; ch <= s->fbw_channels; ch++)
01469 put_bits(&s->pb, 2, s->exp_strategy[ch][blk]);
01470 if (s->lfe_on)
01471 put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]);
01472 }
01473
01474
01475 for (ch = 1; ch <= s->fbw_channels; ch++) {
01476 if (s->exp_strategy[ch][blk] != EXP_REUSE && !block->channel_in_cpl[ch])
01477 put_bits(&s->pb, 6, s->bandwidth_code);
01478 }
01479
01480
01481 for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
01482 int nb_groups;
01483 int cpl = (ch == CPL_CH);
01484
01485 if (s->exp_strategy[ch][blk] == EXP_REUSE)
01486 continue;
01487
01488
01489 put_bits(&s->pb, 4, block->grouped_exp[ch][0] >> cpl);
01490
01491
01492 nb_groups = exponent_group_tab[cpl][s->exp_strategy[ch][blk]-1][block->end_freq[ch]-s->start_freq[ch]];
01493 for (i = 1; i <= nb_groups; i++)
01494 put_bits(&s->pb, 7, block->grouped_exp[ch][i]);
01495
01496
01497 if (ch != s->lfe_channel && !cpl)
01498 put_bits(&s->pb, 2, 0);
01499 }
01500
01501
01502 if (!s->eac3) {
01503 baie = (blk == 0);
01504 put_bits(&s->pb, 1, baie);
01505 if (baie) {
01506 put_bits(&s->pb, 2, s->slow_decay_code);
01507 put_bits(&s->pb, 2, s->fast_decay_code);
01508 put_bits(&s->pb, 2, s->slow_gain_code);
01509 put_bits(&s->pb, 2, s->db_per_bit_code);
01510 put_bits(&s->pb, 3, s->floor_code);
01511 }
01512 }
01513
01514
01515 if (!s->eac3) {
01516 put_bits(&s->pb, 1, block->new_snr_offsets);
01517 if (block->new_snr_offsets) {
01518 put_bits(&s->pb, 6, s->coarse_snr_offset);
01519 for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
01520 put_bits(&s->pb, 4, s->fine_snr_offset[ch]);
01521 put_bits(&s->pb, 3, s->fast_gain_code[ch]);
01522 }
01523 }
01524 } else {
01525 put_bits(&s->pb, 1, 0);
01526 }
01527
01528
01529 if (block->cpl_in_use) {
01530 if (!s->eac3 || block->new_cpl_leak != 2)
01531 put_bits(&s->pb, 1, block->new_cpl_leak);
01532 if (block->new_cpl_leak) {
01533 put_bits(&s->pb, 3, s->bit_alloc.cpl_fast_leak);
01534 put_bits(&s->pb, 3, s->bit_alloc.cpl_slow_leak);
01535 }
01536 }
01537
01538 if (!s->eac3) {
01539 put_bits(&s->pb, 1, 0);
01540 put_bits(&s->pb, 1, 0);
01541 }
01542
01543
01544 got_cpl = !block->cpl_in_use;
01545 for (ch = 1; ch <= s->channels; ch++) {
01546 int b, q;
01547
01548 if (!got_cpl && ch > 1 && block->channel_in_cpl[ch-1]) {
01549 ch0 = ch - 1;
01550 ch = CPL_CH;
01551 got_cpl = 1;
01552 }
01553 for (i = s->start_freq[ch]; i < block->end_freq[ch]; i++) {
01554 q = block->qmant[ch][i];
01555 b = s->ref_bap[ch][blk][i];
01556 switch (b) {
01557 case 0: break;
01558 case 1: if (q != 128) put_bits (&s->pb, 5, q); break;
01559 case 2: if (q != 128) put_bits (&s->pb, 7, q); break;
01560 case 3: put_sbits(&s->pb, 3, q); break;
01561 case 4: if (q != 128) put_bits (&s->pb, 7, q); break;
01562 case 14: put_sbits(&s->pb, 14, q); break;
01563 case 15: put_sbits(&s->pb, 16, q); break;
01564 default: put_sbits(&s->pb, b-1, q); break;
01565 }
01566 }
01567 if (ch == CPL_CH)
01568 ch = ch0;
01569 }
01570 }
01571
01572
01574 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
01575
01576
01577 static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly)
01578 {
01579 unsigned int c;
01580
01581 c = 0;
01582 while (a) {
01583 if (a & 1)
01584 c ^= b;
01585 a = a >> 1;
01586 b = b << 1;
01587 if (b & (1 << 16))
01588 b ^= poly;
01589 }
01590 return c;
01591 }
01592
01593
01594 static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly)
01595 {
01596 unsigned int r;
01597 r = 1;
01598 while (n) {
01599 if (n & 1)
01600 r = mul_poly(r, a, poly);
01601 a = mul_poly(a, a, poly);
01602 n >>= 1;
01603 }
01604 return r;
01605 }
01606
01607
01608
01609
01610
01611 static void output_frame_end(AC3EncodeContext *s)
01612 {
01613 const AVCRC *crc_ctx = av_crc_get_table(AV_CRC_16_ANSI);
01614 int frame_size_58, pad_bytes, crc1, crc2_partial, crc2, crc_inv;
01615 uint8_t *frame;
01616
01617 frame_size_58 = ((s->frame_size >> 2) + (s->frame_size >> 4)) << 1;
01618
01619
01620 av_assert2(s->frame_size * 8 - put_bits_count(&s->pb) >= 18);
01621 flush_put_bits(&s->pb);
01622 frame = s->pb.buf;
01623 pad_bytes = s->frame_size - (put_bits_ptr(&s->pb) - frame) - 2;
01624 av_assert2(pad_bytes >= 0);
01625 if (pad_bytes > 0)
01626 memset(put_bits_ptr(&s->pb), 0, pad_bytes);
01627
01628 if (s->eac3) {
01629
01630 crc2_partial = av_crc(crc_ctx, 0, frame + 2, s->frame_size - 5);
01631 } else {
01632
01633
01634 crc1 = av_bswap16(av_crc(crc_ctx, 0, frame + 4, frame_size_58 - 4));
01635 crc_inv = s->crc_inv[s->frame_size > s->frame_size_min];
01636 crc1 = mul_poly(crc_inv, crc1, CRC16_POLY);
01637 AV_WB16(frame + 2, crc1);
01638
01639
01640 crc2_partial = av_crc(crc_ctx, 0, frame + frame_size_58,
01641 s->frame_size - frame_size_58 - 3);
01642 }
01643 crc2 = av_crc(crc_ctx, crc2_partial, frame + s->frame_size - 3, 1);
01644
01645 if (crc2 == 0x770B) {
01646 frame[s->frame_size - 3] ^= 0x1;
01647 crc2 = av_crc(crc_ctx, crc2_partial, frame + s->frame_size - 3, 1);
01648 }
01649 crc2 = av_bswap16(crc2);
01650 AV_WB16(frame + s->frame_size - 2, crc2);
01651 }
01652
01653
01660 void ff_ac3_output_frame(AC3EncodeContext *s, unsigned char *frame)
01661 {
01662 int blk;
01663
01664 init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE);
01665
01666 s->output_frame_header(s);
01667
01668 for (blk = 0; blk < s->num_blocks; blk++)
01669 output_audio_block(s, blk);
01670
01671 output_frame_end(s);
01672 }
01673
01674
01675 static void dprint_options(AC3EncodeContext *s)
01676 {
01677 #ifdef DEBUG
01678 AVCodecContext *avctx = s->avctx;
01679 AC3EncOptions *opt = &s->options;
01680 char strbuf[32];
01681
01682 switch (s->bitstream_id) {
01683 case 6: av_strlcpy(strbuf, "AC-3 (alt syntax)", 32); break;
01684 case 8: av_strlcpy(strbuf, "AC-3 (standard)", 32); break;
01685 case 9: av_strlcpy(strbuf, "AC-3 (dnet half-rate)", 32); break;
01686 case 10: av_strlcpy(strbuf, "AC-3 (dnet quater-rate)", 32); break;
01687 case 16: av_strlcpy(strbuf, "E-AC-3 (enhanced)", 32); break;
01688 default: snprintf(strbuf, 32, "ERROR");
01689 }
01690 av_dlog(avctx, "bitstream_id: %s (%d)\n", strbuf, s->bitstream_id);
01691 av_dlog(avctx, "sample_fmt: %s\n", av_get_sample_fmt_name(avctx->sample_fmt));
01692 av_get_channel_layout_string(strbuf, 32, s->channels, avctx->channel_layout);
01693 av_dlog(avctx, "channel_layout: %s\n", strbuf);
01694 av_dlog(avctx, "sample_rate: %d\n", s->sample_rate);
01695 av_dlog(avctx, "bit_rate: %d\n", s->bit_rate);
01696 av_dlog(avctx, "blocks/frame: %d (code=%d)\n", s->num_blocks, s->num_blks_code);
01697 if (s->cutoff)
01698 av_dlog(avctx, "cutoff: %d\n", s->cutoff);
01699
01700 av_dlog(avctx, "per_frame_metadata: %s\n",
01701 opt->allow_per_frame_metadata?"on":"off");
01702 if (s->has_center)
01703 av_dlog(avctx, "center_mixlev: %0.3f (%d)\n", opt->center_mix_level,
01704 s->center_mix_level);
01705 else
01706 av_dlog(avctx, "center_mixlev: {not written}\n");
01707 if (s->has_surround)
01708 av_dlog(avctx, "surround_mixlev: %0.3f (%d)\n", opt->surround_mix_level,
01709 s->surround_mix_level);
01710 else
01711 av_dlog(avctx, "surround_mixlev: {not written}\n");
01712 if (opt->audio_production_info) {
01713 av_dlog(avctx, "mixing_level: %ddB\n", opt->mixing_level);
01714 switch (opt->room_type) {
01715 case AC3ENC_OPT_NOT_INDICATED: av_strlcpy(strbuf, "notindicated", 32); break;
01716 case AC3ENC_OPT_LARGE_ROOM: av_strlcpy(strbuf, "large", 32); break;
01717 case AC3ENC_OPT_SMALL_ROOM: av_strlcpy(strbuf, "small", 32); break;
01718 default: snprintf(strbuf, 32, "ERROR (%d)", opt->room_type);
01719 }
01720 av_dlog(avctx, "room_type: %s\n", strbuf);
01721 } else {
01722 av_dlog(avctx, "mixing_level: {not written}\n");
01723 av_dlog(avctx, "room_type: {not written}\n");
01724 }
01725 av_dlog(avctx, "copyright: %s\n", opt->copyright?"on":"off");
01726 av_dlog(avctx, "dialnorm: %ddB\n", opt->dialogue_level);
01727 if (s->channel_mode == AC3_CHMODE_STEREO) {
01728 switch (opt->dolby_surround_mode) {
01729 case AC3ENC_OPT_NOT_INDICATED: av_strlcpy(strbuf, "notindicated", 32); break;
01730 case AC3ENC_OPT_MODE_ON: av_strlcpy(strbuf, "on", 32); break;
01731 case AC3ENC_OPT_MODE_OFF: av_strlcpy(strbuf, "off", 32); break;
01732 default: snprintf(strbuf, 32, "ERROR (%d)", opt->dolby_surround_mode);
01733 }
01734 av_dlog(avctx, "dsur_mode: %s\n", strbuf);
01735 } else {
01736 av_dlog(avctx, "dsur_mode: {not written}\n");
01737 }
01738 av_dlog(avctx, "original: %s\n", opt->original?"on":"off");
01739
01740 if (s->bitstream_id == 6) {
01741 if (opt->extended_bsi_1) {
01742 switch (opt->preferred_stereo_downmix) {
01743 case AC3ENC_OPT_NOT_INDICATED: av_strlcpy(strbuf, "notindicated", 32); break;
01744 case AC3ENC_OPT_DOWNMIX_LTRT: av_strlcpy(strbuf, "ltrt", 32); break;
01745 case AC3ENC_OPT_DOWNMIX_LORO: av_strlcpy(strbuf, "loro", 32); break;
01746 default: snprintf(strbuf, 32, "ERROR (%d)", opt->preferred_stereo_downmix);
01747 }
01748 av_dlog(avctx, "dmix_mode: %s\n", strbuf);
01749 av_dlog(avctx, "ltrt_cmixlev: %0.3f (%d)\n",
01750 opt->ltrt_center_mix_level, s->ltrt_center_mix_level);
01751 av_dlog(avctx, "ltrt_surmixlev: %0.3f (%d)\n",
01752 opt->ltrt_surround_mix_level, s->ltrt_surround_mix_level);
01753 av_dlog(avctx, "loro_cmixlev: %0.3f (%d)\n",
01754 opt->loro_center_mix_level, s->loro_center_mix_level);
01755 av_dlog(avctx, "loro_surmixlev: %0.3f (%d)\n",
01756 opt->loro_surround_mix_level, s->loro_surround_mix_level);
01757 } else {
01758 av_dlog(avctx, "extended bitstream info 1: {not written}\n");
01759 }
01760 if (opt->extended_bsi_2) {
01761 switch (opt->dolby_surround_ex_mode) {
01762 case AC3ENC_OPT_NOT_INDICATED: av_strlcpy(strbuf, "notindicated", 32); break;
01763 case AC3ENC_OPT_MODE_ON: av_strlcpy(strbuf, "on", 32); break;
01764 case AC3ENC_OPT_MODE_OFF: av_strlcpy(strbuf, "off", 32); break;
01765 default: snprintf(strbuf, 32, "ERROR (%d)", opt->dolby_surround_ex_mode);
01766 }
01767 av_dlog(avctx, "dsurex_mode: %s\n", strbuf);
01768 switch (opt->dolby_headphone_mode) {
01769 case AC3ENC_OPT_NOT_INDICATED: av_strlcpy(strbuf, "notindicated", 32); break;
01770 case AC3ENC_OPT_MODE_ON: av_strlcpy(strbuf, "on", 32); break;
01771 case AC3ENC_OPT_MODE_OFF: av_strlcpy(strbuf, "off", 32); break;
01772 default: snprintf(strbuf, 32, "ERROR (%d)", opt->dolby_headphone_mode);
01773 }
01774 av_dlog(avctx, "dheadphone_mode: %s\n", strbuf);
01775
01776 switch (opt->ad_converter_type) {
01777 case AC3ENC_OPT_ADCONV_STANDARD: av_strlcpy(strbuf, "standard", 32); break;
01778 case AC3ENC_OPT_ADCONV_HDCD: av_strlcpy(strbuf, "hdcd", 32); break;
01779 default: snprintf(strbuf, 32, "ERROR (%d)", opt->ad_converter_type);
01780 }
01781 av_dlog(avctx, "ad_conv_type: %s\n", strbuf);
01782 } else {
01783 av_dlog(avctx, "extended bitstream info 2: {not written}\n");
01784 }
01785 }
01786 #endif
01787 }
01788
01789
01790 #define FLT_OPTION_THRESHOLD 0.01
01791
01792 static int validate_float_option(float v, const float *v_list, int v_list_size)
01793 {
01794 int i;
01795
01796 for (i = 0; i < v_list_size; i++) {
01797 if (v < (v_list[i] + FLT_OPTION_THRESHOLD) &&
01798 v > (v_list[i] - FLT_OPTION_THRESHOLD))
01799 break;
01800 }
01801 if (i == v_list_size)
01802 return -1;
01803
01804 return i;
01805 }
01806
01807
01808 static void validate_mix_level(void *log_ctx, const char *opt_name,
01809 float *opt_param, const float *list,
01810 int list_size, int default_value, int min_value,
01811 int *ctx_param)
01812 {
01813 int mixlev = validate_float_option(*opt_param, list, list_size);
01814 if (mixlev < min_value) {
01815 mixlev = default_value;
01816 if (*opt_param >= 0.0) {
01817 av_log(log_ctx, AV_LOG_WARNING, "requested %s is not valid. using "
01818 "default value: %0.3f\n", opt_name, list[mixlev]);
01819 }
01820 }
01821 *opt_param = list[mixlev];
01822 *ctx_param = mixlev;
01823 }
01824
01825
01832 int ff_ac3_validate_metadata(AC3EncodeContext *s)
01833 {
01834 AVCodecContext *avctx = s->avctx;
01835 AC3EncOptions *opt = &s->options;
01836
01837 opt->audio_production_info = 0;
01838 opt->extended_bsi_1 = 0;
01839 opt->extended_bsi_2 = 0;
01840 opt->eac3_mixing_metadata = 0;
01841 opt->eac3_info_metadata = 0;
01842
01843
01844 if (s->channel_mode > AC3_CHMODE_STEREO && opt->preferred_stereo_downmix != AC3ENC_OPT_NONE) {
01845 opt->extended_bsi_1 = 1;
01846 opt->eac3_mixing_metadata = 1;
01847 }
01848 if (s->has_center &&
01849 (opt->ltrt_center_mix_level >= 0 || opt->loro_center_mix_level >= 0)) {
01850 opt->extended_bsi_1 = 1;
01851 opt->eac3_mixing_metadata = 1;
01852 }
01853 if (s->has_surround &&
01854 (opt->ltrt_surround_mix_level >= 0 || opt->loro_surround_mix_level >= 0)) {
01855 opt->extended_bsi_1 = 1;
01856 opt->eac3_mixing_metadata = 1;
01857 }
01858
01859 if (s->eac3) {
01860
01861 if (avctx->audio_service_type != AV_AUDIO_SERVICE_TYPE_MAIN)
01862 opt->eac3_info_metadata = 1;
01863 if (opt->copyright != AC3ENC_OPT_NONE || opt->original != AC3ENC_OPT_NONE)
01864 opt->eac3_info_metadata = 1;
01865 if (s->channel_mode == AC3_CHMODE_STEREO &&
01866 (opt->dolby_headphone_mode != AC3ENC_OPT_NONE || opt->dolby_surround_mode != AC3ENC_OPT_NONE))
01867 opt->eac3_info_metadata = 1;
01868 if (s->channel_mode >= AC3_CHMODE_2F2R && opt->dolby_surround_ex_mode != AC3ENC_OPT_NONE)
01869 opt->eac3_info_metadata = 1;
01870 if (opt->mixing_level != AC3ENC_OPT_NONE || opt->room_type != AC3ENC_OPT_NONE ||
01871 opt->ad_converter_type != AC3ENC_OPT_NONE) {
01872 opt->audio_production_info = 1;
01873 opt->eac3_info_metadata = 1;
01874 }
01875 } else {
01876
01877 if (opt->mixing_level != AC3ENC_OPT_NONE || opt->room_type != AC3ENC_OPT_NONE)
01878 opt->audio_production_info = 1;
01879
01880
01881 if (s->channel_mode >= AC3_CHMODE_2F2R && opt->dolby_surround_ex_mode != AC3ENC_OPT_NONE)
01882 opt->extended_bsi_2 = 1;
01883 if (s->channel_mode == AC3_CHMODE_STEREO && opt->dolby_headphone_mode != AC3ENC_OPT_NONE)
01884 opt->extended_bsi_2 = 1;
01885 if (opt->ad_converter_type != AC3ENC_OPT_NONE)
01886 opt->extended_bsi_2 = 1;
01887 }
01888
01889
01890 if (!s->eac3) {
01891 if (s->has_center) {
01892 validate_mix_level(avctx, "center_mix_level", &opt->center_mix_level,
01893 cmixlev_options, CMIXLEV_NUM_OPTIONS, 1, 0,
01894 &s->center_mix_level);
01895 }
01896 if (s->has_surround) {
01897 validate_mix_level(avctx, "surround_mix_level", &opt->surround_mix_level,
01898 surmixlev_options, SURMIXLEV_NUM_OPTIONS, 1, 0,
01899 &s->surround_mix_level);
01900 }
01901 }
01902
01903
01904 if (opt->extended_bsi_1 || opt->eac3_mixing_metadata) {
01905
01906 if (opt->preferred_stereo_downmix == AC3ENC_OPT_NONE)
01907 opt->preferred_stereo_downmix = AC3ENC_OPT_NOT_INDICATED;
01908 if (!s->eac3 || s->has_center) {
01909
01910 validate_mix_level(avctx, "ltrt_center_mix_level",
01911 &opt->ltrt_center_mix_level, extmixlev_options,
01912 EXTMIXLEV_NUM_OPTIONS, 5, 0,
01913 &s->ltrt_center_mix_level);
01914
01915 validate_mix_level(avctx, "loro_center_mix_level",
01916 &opt->loro_center_mix_level, extmixlev_options,
01917 EXTMIXLEV_NUM_OPTIONS, 5, 0,
01918 &s->loro_center_mix_level);
01919 }
01920 if (!s->eac3 || s->has_surround) {
01921
01922 validate_mix_level(avctx, "ltrt_surround_mix_level",
01923 &opt->ltrt_surround_mix_level, extmixlev_options,
01924 EXTMIXLEV_NUM_OPTIONS, 6, 3,
01925 &s->ltrt_surround_mix_level);
01926
01927 validate_mix_level(avctx, "loro_surround_mix_level",
01928 &opt->loro_surround_mix_level, extmixlev_options,
01929 EXTMIXLEV_NUM_OPTIONS, 6, 3,
01930 &s->loro_surround_mix_level);
01931 }
01932 }
01933
01934
01935 if ((avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_KARAOKE &&
01936 avctx->channels == 1) ||
01937 ((avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_COMMENTARY ||
01938 avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_EMERGENCY ||
01939 avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_VOICE_OVER)
01940 && avctx->channels > 1)) {
01941 av_log(avctx, AV_LOG_ERROR, "invalid audio service type for the "
01942 "specified number of channels\n");
01943 return AVERROR(EINVAL);
01944 }
01945
01946
01947 if (opt->extended_bsi_2 || opt->eac3_info_metadata) {
01948
01949 if (opt->dolby_headphone_mode == AC3ENC_OPT_NONE)
01950 opt->dolby_headphone_mode = AC3ENC_OPT_NOT_INDICATED;
01951
01952 if (opt->dolby_surround_ex_mode == AC3ENC_OPT_NONE)
01953 opt->dolby_surround_ex_mode = AC3ENC_OPT_NOT_INDICATED;
01954
01955 if (opt->ad_converter_type == AC3ENC_OPT_NONE)
01956 opt->ad_converter_type = AC3ENC_OPT_ADCONV_STANDARD;
01957 }
01958
01959
01960 if (!s->eac3 || opt->eac3_info_metadata) {
01961
01962 if (opt->copyright == AC3ENC_OPT_NONE)
01963 opt->copyright = AC3ENC_OPT_OFF;
01964
01965 if (opt->original == AC3ENC_OPT_NONE)
01966 opt->original = AC3ENC_OPT_ON;
01967 }
01968
01969
01970 if (!s->eac3 || opt->eac3_info_metadata) {
01971 if (opt->dolby_surround_mode == AC3ENC_OPT_NONE)
01972 opt->dolby_surround_mode = AC3ENC_OPT_NOT_INDICATED;
01973 }
01974
01975
01976 if (opt->audio_production_info) {
01977 if (opt->mixing_level == AC3ENC_OPT_NONE) {
01978 av_log(avctx, AV_LOG_ERROR, "mixing_level must be set if "
01979 "room_type is set\n");
01980 return AVERROR(EINVAL);
01981 }
01982 if (opt->mixing_level < 80) {
01983 av_log(avctx, AV_LOG_ERROR, "invalid mixing level. must be between "
01984 "80dB and 111dB\n");
01985 return AVERROR(EINVAL);
01986 }
01987
01988 if (opt->room_type == AC3ENC_OPT_NONE)
01989 opt->room_type = AC3ENC_OPT_NOT_INDICATED;
01990 }
01991
01992
01993 if (!s->eac3 && (opt->extended_bsi_1 || opt->extended_bsi_2)) {
01994 if (s->bitstream_id > 8 && s->bitstream_id < 11) {
01995 static int warn_once = 1;
01996 if (warn_once) {
01997 av_log(avctx, AV_LOG_WARNING, "alternate bitstream syntax is "
01998 "not compatible with reduced samplerates. writing of "
01999 "extended bitstream information will be disabled.\n");
02000 warn_once = 0;
02001 }
02002 } else {
02003 s->bitstream_id = 6;
02004 }
02005 }
02006
02007 return 0;
02008 }
02009
02010
02016 av_cold int ff_ac3_encode_close(AVCodecContext *avctx)
02017 {
02018 int blk, ch;
02019 AC3EncodeContext *s = avctx->priv_data;
02020
02021 av_freep(&s->windowed_samples);
02022 for (ch = 0; ch < s->channels; ch++)
02023 av_freep(&s->planar_samples[ch]);
02024 av_freep(&s->planar_samples);
02025 av_freep(&s->bap_buffer);
02026 av_freep(&s->bap1_buffer);
02027 av_freep(&s->mdct_coef_buffer);
02028 av_freep(&s->fixed_coef_buffer);
02029 av_freep(&s->exp_buffer);
02030 av_freep(&s->grouped_exp_buffer);
02031 av_freep(&s->psd_buffer);
02032 av_freep(&s->band_psd_buffer);
02033 av_freep(&s->mask_buffer);
02034 av_freep(&s->qmant_buffer);
02035 av_freep(&s->cpl_coord_exp_buffer);
02036 av_freep(&s->cpl_coord_mant_buffer);
02037 for (blk = 0; blk < s->num_blocks; blk++) {
02038 AC3Block *block = &s->blocks[blk];
02039 av_freep(&block->mdct_coef);
02040 av_freep(&block->fixed_coef);
02041 av_freep(&block->exp);
02042 av_freep(&block->grouped_exp);
02043 av_freep(&block->psd);
02044 av_freep(&block->band_psd);
02045 av_freep(&block->mask);
02046 av_freep(&block->qmant);
02047 av_freep(&block->cpl_coord_exp);
02048 av_freep(&block->cpl_coord_mant);
02049 }
02050
02051 s->mdct_end(s);
02052
02053 #if FF_API_OLD_ENCODE_AUDIO
02054 av_freep(&avctx->coded_frame);
02055 #endif
02056 return 0;
02057 }
02058
02059
02060
02061
02062
02063 static av_cold int set_channel_info(AC3EncodeContext *s, int channels,
02064 uint64_t *channel_layout)
02065 {
02066 int ch_layout;
02067
02068 if (channels < 1 || channels > AC3_MAX_CHANNELS)
02069 return AVERROR(EINVAL);
02070 if (*channel_layout > 0x7FF)
02071 return AVERROR(EINVAL);
02072 ch_layout = *channel_layout;
02073 if (!ch_layout)
02074 ch_layout = av_get_default_channel_layout(channels);
02075
02076 s->lfe_on = !!(ch_layout & AV_CH_LOW_FREQUENCY);
02077 s->channels = channels;
02078 s->fbw_channels = channels - s->lfe_on;
02079 s->lfe_channel = s->lfe_on ? s->fbw_channels + 1 : -1;
02080 if (s->lfe_on)
02081 ch_layout -= AV_CH_LOW_FREQUENCY;
02082
02083 switch (ch_layout) {
02084 case AV_CH_LAYOUT_MONO: s->channel_mode = AC3_CHMODE_MONO; break;
02085 case AV_CH_LAYOUT_STEREO: s->channel_mode = AC3_CHMODE_STEREO; break;
02086 case AV_CH_LAYOUT_SURROUND: s->channel_mode = AC3_CHMODE_3F; break;
02087 case AV_CH_LAYOUT_2_1: s->channel_mode = AC3_CHMODE_2F1R; break;
02088 case AV_CH_LAYOUT_4POINT0: s->channel_mode = AC3_CHMODE_3F1R; break;
02089 case AV_CH_LAYOUT_QUAD:
02090 case AV_CH_LAYOUT_2_2: s->channel_mode = AC3_CHMODE_2F2R; break;
02091 case AV_CH_LAYOUT_5POINT0:
02092 case AV_CH_LAYOUT_5POINT0_BACK: s->channel_mode = AC3_CHMODE_3F2R; break;
02093 default:
02094 return AVERROR(EINVAL);
02095 }
02096 s->has_center = (s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO;
02097 s->has_surround = s->channel_mode & 0x04;
02098
02099 s->channel_map = ff_ac3_enc_channel_map[s->channel_mode][s->lfe_on];
02100 *channel_layout = ch_layout;
02101 if (s->lfe_on)
02102 *channel_layout |= AV_CH_LOW_FREQUENCY;
02103
02104 return 0;
02105 }
02106
02107
02108 static av_cold int validate_options(AC3EncodeContext *s)
02109 {
02110 AVCodecContext *avctx = s->avctx;
02111 int i, ret, max_sr;
02112
02113
02114 if (!avctx->channel_layout) {
02115 av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "
02116 "encoder will guess the layout, but it "
02117 "might be incorrect.\n");
02118 }
02119 ret = set_channel_info(s, avctx->channels, &avctx->channel_layout);
02120 if (ret) {
02121 av_log(avctx, AV_LOG_ERROR, "invalid channel layout\n");
02122 return ret;
02123 }
02124
02125
02126
02127
02128
02129 max_sr = s->eac3 ? 2 : 8;
02130 for (i = 0; i <= max_sr; i++) {
02131 if ((ff_ac3_sample_rate_tab[i % 3] >> (i / 3)) == avctx->sample_rate)
02132 break;
02133 }
02134 if (i > max_sr) {
02135 av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
02136 return AVERROR(EINVAL);
02137 }
02138 s->sample_rate = avctx->sample_rate;
02139 s->bit_alloc.sr_shift = i / 3;
02140 s->bit_alloc.sr_code = i % 3;
02141 s->bitstream_id = s->eac3 ? 16 : 8 + s->bit_alloc.sr_shift;
02142
02143
02144 if (!avctx->bit_rate) {
02145 switch (s->fbw_channels) {
02146 case 1: avctx->bit_rate = 96000; break;
02147 case 2: avctx->bit_rate = 192000; break;
02148 case 3: avctx->bit_rate = 320000; break;
02149 case 4: avctx->bit_rate = 384000; break;
02150 case 5: avctx->bit_rate = 448000; break;
02151 }
02152 }
02153
02154
02155 if (s->eac3) {
02156 int max_br, min_br, wpf, min_br_dist, min_br_code;
02157 int num_blks_code, num_blocks, frame_samples;
02158
02159
02160
02161
02162
02163 for (num_blks_code = 3; num_blks_code >= 0; num_blks_code--) {
02164 num_blocks = ((int[]){ 1, 2, 3, 6 })[num_blks_code];
02165 frame_samples = AC3_BLOCK_SIZE * num_blocks;
02166 max_br = 2048 * s->sample_rate / frame_samples * 16;
02167 min_br = ((s->sample_rate + (frame_samples-1)) / frame_samples) * 16;
02168 if (avctx->bit_rate <= max_br)
02169 break;
02170 }
02171 if (avctx->bit_rate < min_br || avctx->bit_rate > max_br) {
02172 av_log(avctx, AV_LOG_ERROR, "invalid bit rate. must be %d to %d "
02173 "for this sample rate\n", min_br, max_br);
02174 return AVERROR(EINVAL);
02175 }
02176 s->num_blks_code = num_blks_code;
02177 s->num_blocks = num_blocks;
02178
02179
02180 wpf = (avctx->bit_rate / 16) * frame_samples / s->sample_rate;
02181 av_assert1(wpf > 0 && wpf <= 2048);
02182
02183
02184
02185
02186 min_br_code = -1;
02187 min_br_dist = INT_MAX;
02188 for (i = 0; i < 19; i++) {
02189 int br_dist = abs(ff_ac3_bitrate_tab[i] * 1000 - avctx->bit_rate);
02190 if (br_dist < min_br_dist) {
02191 min_br_dist = br_dist;
02192 min_br_code = i;
02193 }
02194 }
02195
02196
02197 s->frame_size_code = min_br_code << 1;
02198 while (wpf > 1 && wpf * s->sample_rate / AC3_FRAME_SIZE * 16 > avctx->bit_rate)
02199 wpf--;
02200 s->frame_size_min = 2 * wpf;
02201 } else {
02202 int best_br = 0, best_code = 0, best_diff = INT_MAX;
02203 for (i = 0; i < 19; i++) {
02204 int br = (ff_ac3_bitrate_tab[i] >> s->bit_alloc.sr_shift) * 1000;
02205 int diff = abs(br - avctx->bit_rate);
02206 if (diff < best_diff) {
02207 best_br = br;
02208 best_code = i;
02209 best_diff = diff;
02210 }
02211 if (!best_diff)
02212 break;
02213 }
02214 avctx->bit_rate = best_br;
02215 s->frame_size_code = best_code << 1;
02216 s->frame_size_min = 2 * ff_ac3_frame_size_tab[s->frame_size_code][s->bit_alloc.sr_code];
02217 s->num_blks_code = 0x3;
02218 s->num_blocks = 6;
02219 }
02220 s->bit_rate = avctx->bit_rate;
02221 s->frame_size = s->frame_size_min;
02222
02223
02224 if (avctx->cutoff < 0) {
02225 av_log(avctx, AV_LOG_ERROR, "invalid cutoff frequency\n");
02226 return AVERROR(EINVAL);
02227 }
02228 s->cutoff = avctx->cutoff;
02229 if (s->cutoff > (s->sample_rate >> 1))
02230 s->cutoff = s->sample_rate >> 1;
02231
02232 ret = ff_ac3_validate_metadata(s);
02233 if (ret)
02234 return ret;
02235
02236 s->rematrixing_enabled = s->options.stereo_rematrixing &&
02237 (s->channel_mode == AC3_CHMODE_STEREO);
02238
02239 s->cpl_enabled = s->options.channel_coupling &&
02240 s->channel_mode >= AC3_CHMODE_STEREO;
02241
02242 return 0;
02243 }
02244
02245
02246
02247
02248
02249
02250
02251 static av_cold void set_bandwidth(AC3EncodeContext *s)
02252 {
02253 int blk, ch, cpl_start;
02254
02255 if (s->cutoff) {
02256
02257 int fbw_coeffs;
02258 fbw_coeffs = s->cutoff * 2 * AC3_MAX_COEFS / s->sample_rate;
02259 s->bandwidth_code = av_clip((fbw_coeffs - 73) / 3, 0, 60);
02260 } else {
02261
02262 s->bandwidth_code = ac3_bandwidth_tab[s->fbw_channels-1][s->bit_alloc.sr_code][s->frame_size_code/2];
02263 }
02264
02265
02266 for (ch = 1; ch <= s->fbw_channels; ch++) {
02267 s->start_freq[ch] = 0;
02268 for (blk = 0; blk < s->num_blocks; blk++)
02269 s->blocks[blk].end_freq[ch] = s->bandwidth_code * 3 + 73;
02270 }
02271
02272 if (s->lfe_on) {
02273 s->start_freq[s->lfe_channel] = 0;
02274 for (blk = 0; blk < s->num_blocks; blk++)
02275 s->blocks[blk].end_freq[ch] = 7;
02276 }
02277
02278
02279 if (s->cpl_enabled) {
02280 if (s->options.cpl_start != AC3ENC_OPT_AUTO) {
02281 cpl_start = s->options.cpl_start;
02282 } else {
02283 cpl_start = ac3_coupling_start_tab[s->channel_mode-2][s->bit_alloc.sr_code][s->frame_size_code/2];
02284 if (cpl_start < 0) {
02285 if (s->options.channel_coupling == AC3ENC_OPT_AUTO)
02286 s->cpl_enabled = 0;
02287 else
02288 cpl_start = 15;
02289 }
02290 }
02291 }
02292 if (s->cpl_enabled) {
02293 int i, cpl_start_band, cpl_end_band;
02294 uint8_t *cpl_band_sizes = s->cpl_band_sizes;
02295
02296 cpl_end_band = s->bandwidth_code / 4 + 3;
02297 cpl_start_band = av_clip(cpl_start, 0, FFMIN(cpl_end_band-1, 15));
02298
02299 s->num_cpl_subbands = cpl_end_band - cpl_start_band;
02300
02301 s->num_cpl_bands = 1;
02302 *cpl_band_sizes = 12;
02303 for (i = cpl_start_band + 1; i < cpl_end_band; i++) {
02304 if (ff_eac3_default_cpl_band_struct[i]) {
02305 *cpl_band_sizes += 12;
02306 } else {
02307 s->num_cpl_bands++;
02308 cpl_band_sizes++;
02309 *cpl_band_sizes = 12;
02310 }
02311 }
02312
02313 s->start_freq[CPL_CH] = cpl_start_band * 12 + 37;
02314 s->cpl_end_freq = cpl_end_band * 12 + 37;
02315 for (blk = 0; blk < s->num_blocks; blk++)
02316 s->blocks[blk].end_freq[CPL_CH] = s->cpl_end_freq;
02317 }
02318 }
02319
02320
02321 static av_cold int allocate_buffers(AC3EncodeContext *s)
02322 {
02323 AVCodecContext *avctx = s->avctx;
02324 int blk, ch;
02325 int channels = s->channels + 1;
02326 int channel_blocks = channels * s->num_blocks;
02327 int total_coefs = AC3_MAX_COEFS * channel_blocks;
02328
02329 if (s->allocate_sample_buffers(s))
02330 goto alloc_fail;
02331
02332 FF_ALLOC_OR_GOTO(avctx, s->bap_buffer, total_coefs *
02333 sizeof(*s->bap_buffer), alloc_fail);
02334 FF_ALLOC_OR_GOTO(avctx, s->bap1_buffer, total_coefs *
02335 sizeof(*s->bap1_buffer), alloc_fail);
02336 FF_ALLOCZ_OR_GOTO(avctx, s->mdct_coef_buffer, total_coefs *
02337 sizeof(*s->mdct_coef_buffer), alloc_fail);
02338 FF_ALLOC_OR_GOTO(avctx, s->exp_buffer, total_coefs *
02339 sizeof(*s->exp_buffer), alloc_fail);
02340 FF_ALLOC_OR_GOTO(avctx, s->grouped_exp_buffer, channel_blocks * 128 *
02341 sizeof(*s->grouped_exp_buffer), alloc_fail);
02342 FF_ALLOC_OR_GOTO(avctx, s->psd_buffer, total_coefs *
02343 sizeof(*s->psd_buffer), alloc_fail);
02344 FF_ALLOC_OR_GOTO(avctx, s->band_psd_buffer, channel_blocks * 64 *
02345 sizeof(*s->band_psd_buffer), alloc_fail);
02346 FF_ALLOC_OR_GOTO(avctx, s->mask_buffer, channel_blocks * 64 *
02347 sizeof(*s->mask_buffer), alloc_fail);
02348 FF_ALLOC_OR_GOTO(avctx, s->qmant_buffer, total_coefs *
02349 sizeof(*s->qmant_buffer), alloc_fail);
02350 if (s->cpl_enabled) {
02351 FF_ALLOC_OR_GOTO(avctx, s->cpl_coord_exp_buffer, channel_blocks * 16 *
02352 sizeof(*s->cpl_coord_exp_buffer), alloc_fail);
02353 FF_ALLOC_OR_GOTO(avctx, s->cpl_coord_mant_buffer, channel_blocks * 16 *
02354 sizeof(*s->cpl_coord_mant_buffer), alloc_fail);
02355 }
02356 for (blk = 0; blk < s->num_blocks; blk++) {
02357 AC3Block *block = &s->blocks[blk];
02358 FF_ALLOCZ_OR_GOTO(avctx, block->mdct_coef, channels * sizeof(*block->mdct_coef),
02359 alloc_fail);
02360 FF_ALLOCZ_OR_GOTO(avctx, block->exp, channels * sizeof(*block->exp),
02361 alloc_fail);
02362 FF_ALLOCZ_OR_GOTO(avctx, block->grouped_exp, channels * sizeof(*block->grouped_exp),
02363 alloc_fail);
02364 FF_ALLOCZ_OR_GOTO(avctx, block->psd, channels * sizeof(*block->psd),
02365 alloc_fail);
02366 FF_ALLOCZ_OR_GOTO(avctx, block->band_psd, channels * sizeof(*block->band_psd),
02367 alloc_fail);
02368 FF_ALLOCZ_OR_GOTO(avctx, block->mask, channels * sizeof(*block->mask),
02369 alloc_fail);
02370 FF_ALLOCZ_OR_GOTO(avctx, block->qmant, channels * sizeof(*block->qmant),
02371 alloc_fail);
02372 if (s->cpl_enabled) {
02373 FF_ALLOCZ_OR_GOTO(avctx, block->cpl_coord_exp, channels * sizeof(*block->cpl_coord_exp),
02374 alloc_fail);
02375 FF_ALLOCZ_OR_GOTO(avctx, block->cpl_coord_mant, channels * sizeof(*block->cpl_coord_mant),
02376 alloc_fail);
02377 }
02378
02379 for (ch = 0; ch < channels; ch++) {
02380
02381 block->grouped_exp[ch] = &s->grouped_exp_buffer[128 * (blk * channels + ch)];
02382 block->psd[ch] = &s->psd_buffer [AC3_MAX_COEFS * (blk * channels + ch)];
02383 block->band_psd[ch] = &s->band_psd_buffer [64 * (blk * channels + ch)];
02384 block->mask[ch] = &s->mask_buffer [64 * (blk * channels + ch)];
02385 block->qmant[ch] = &s->qmant_buffer [AC3_MAX_COEFS * (blk * channels + ch)];
02386 if (s->cpl_enabled) {
02387 block->cpl_coord_exp[ch] = &s->cpl_coord_exp_buffer [16 * (blk * channels + ch)];
02388 block->cpl_coord_mant[ch] = &s->cpl_coord_mant_buffer[16 * (blk * channels + ch)];
02389 }
02390
02391
02392 block->exp[ch] = &s->exp_buffer [AC3_MAX_COEFS * (s->num_blocks * ch + blk)];
02393 block->mdct_coef[ch] = &s->mdct_coef_buffer [AC3_MAX_COEFS * (s->num_blocks * ch + blk)];
02394 }
02395 }
02396
02397 if (!s->fixed_point) {
02398 FF_ALLOCZ_OR_GOTO(avctx, s->fixed_coef_buffer, total_coefs *
02399 sizeof(*s->fixed_coef_buffer), alloc_fail);
02400 for (blk = 0; blk < s->num_blocks; blk++) {
02401 AC3Block *block = &s->blocks[blk];
02402 FF_ALLOCZ_OR_GOTO(avctx, block->fixed_coef, channels *
02403 sizeof(*block->fixed_coef), alloc_fail);
02404 for (ch = 0; ch < channels; ch++)
02405 block->fixed_coef[ch] = &s->fixed_coef_buffer[AC3_MAX_COEFS * (s->num_blocks * ch + blk)];
02406 }
02407 } else {
02408 for (blk = 0; blk < s->num_blocks; blk++) {
02409 AC3Block *block = &s->blocks[blk];
02410 FF_ALLOCZ_OR_GOTO(avctx, block->fixed_coef, channels *
02411 sizeof(*block->fixed_coef), alloc_fail);
02412 for (ch = 0; ch < channels; ch++)
02413 block->fixed_coef[ch] = (int32_t *)block->mdct_coef[ch];
02414 }
02415 }
02416
02417 return 0;
02418 alloc_fail:
02419 return AVERROR(ENOMEM);
02420 }
02421
02422
02423 av_cold int ff_ac3_encode_init(AVCodecContext *avctx)
02424 {
02425 AC3EncodeContext *s = avctx->priv_data;
02426 int ret, frame_size_58;
02427
02428 s->avctx = avctx;
02429
02430 s->eac3 = avctx->codec_id == AV_CODEC_ID_EAC3;
02431
02432 ff_ac3_common_init();
02433
02434 ret = validate_options(s);
02435 if (ret)
02436 return ret;
02437
02438 avctx->frame_size = AC3_BLOCK_SIZE * s->num_blocks;
02439 avctx->delay = AC3_BLOCK_SIZE;
02440
02441 s->bitstream_mode = avctx->audio_service_type;
02442 if (s->bitstream_mode == AV_AUDIO_SERVICE_TYPE_KARAOKE)
02443 s->bitstream_mode = 0x7;
02444
02445 s->bits_written = 0;
02446 s->samples_written = 0;
02447
02448
02449 frame_size_58 = (( s->frame_size >> 2) + ( s->frame_size >> 4)) << 1;
02450 s->crc_inv[0] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);
02451 if (s->bit_alloc.sr_code == 1) {
02452 frame_size_58 = (((s->frame_size+2) >> 2) + ((s->frame_size+2) >> 4)) << 1;
02453 s->crc_inv[1] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);
02454 }
02455
02456
02457 if (CONFIG_AC3_FIXED_ENCODER && s->fixed_point) {
02458 s->mdct_end = ff_ac3_fixed_mdct_end;
02459 s->mdct_init = ff_ac3_fixed_mdct_init;
02460 s->allocate_sample_buffers = ff_ac3_fixed_allocate_sample_buffers;
02461 } else if (CONFIG_AC3_ENCODER || CONFIG_EAC3_ENCODER) {
02462 s->mdct_end = ff_ac3_float_mdct_end;
02463 s->mdct_init = ff_ac3_float_mdct_init;
02464 s->allocate_sample_buffers = ff_ac3_float_allocate_sample_buffers;
02465 }
02466 if (CONFIG_EAC3_ENCODER && s->eac3)
02467 s->output_frame_header = ff_eac3_output_frame_header;
02468 else
02469 s->output_frame_header = ac3_output_frame_header;
02470
02471 set_bandwidth(s);
02472
02473 exponent_init(s);
02474
02475 bit_alloc_init(s);
02476
02477 ret = s->mdct_init(s);
02478 if (ret)
02479 goto init_fail;
02480
02481 ret = allocate_buffers(s);
02482 if (ret)
02483 goto init_fail;
02484
02485 #if FF_API_OLD_ENCODE_AUDIO
02486 avctx->coded_frame= avcodec_alloc_frame();
02487 if (!avctx->coded_frame) {
02488 ret = AVERROR(ENOMEM);
02489 goto init_fail;
02490 }
02491 #endif
02492
02493 ff_dsputil_init(&s->dsp, avctx);
02494 avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
02495 ff_ac3dsp_init(&s->ac3dsp, avctx->flags & CODEC_FLAG_BITEXACT);
02496
02497 dprint_options(s);
02498
02499 return 0;
02500 init_fail:
02501 ff_ac3_encode_close(avctx);
02502 return ret;
02503 }