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;
01387 int av_uninit(ch0);
01388 AC3Block *block = &s->blocks[blk];
01389
01390
01391 if (!s->eac3) {
01392 for (ch = 0; ch < s->fbw_channels; ch++)
01393 put_bits(&s->pb, 1, 0);
01394 }
01395
01396
01397 if (!s->eac3) {
01398 for (ch = 0; ch < s->fbw_channels; ch++)
01399 put_bits(&s->pb, 1, 1);
01400 }
01401
01402
01403 put_bits(&s->pb, 1, 0);
01404
01405
01406 if (s->eac3)
01407 put_bits(&s->pb, 1, 0);
01408
01409
01410 if (!s->eac3)
01411 put_bits(&s->pb, 1, block->new_cpl_strategy);
01412 if (block->new_cpl_strategy) {
01413 if (!s->eac3)
01414 put_bits(&s->pb, 1, block->cpl_in_use);
01415 if (block->cpl_in_use) {
01416 int start_sub, end_sub;
01417 if (s->eac3)
01418 put_bits(&s->pb, 1, 0);
01419 if (!s->eac3 || s->channel_mode != AC3_CHMODE_STEREO) {
01420 for (ch = 1; ch <= s->fbw_channels; ch++)
01421 put_bits(&s->pb, 1, block->channel_in_cpl[ch]);
01422 }
01423 if (s->channel_mode == AC3_CHMODE_STEREO)
01424 put_bits(&s->pb, 1, 0);
01425 start_sub = (s->start_freq[CPL_CH] - 37) / 12;
01426 end_sub = (s->cpl_end_freq - 37) / 12;
01427 put_bits(&s->pb, 4, start_sub);
01428 put_bits(&s->pb, 4, end_sub - 3);
01429
01430 if (s->eac3) {
01431 put_bits(&s->pb, 1, 0);
01432 } else {
01433 for (bnd = start_sub+1; bnd < end_sub; bnd++)
01434 put_bits(&s->pb, 1, ff_eac3_default_cpl_band_struct[bnd]);
01435 }
01436 }
01437 }
01438
01439
01440 if (block->cpl_in_use) {
01441 for (ch = 1; ch <= s->fbw_channels; ch++) {
01442 if (block->channel_in_cpl[ch]) {
01443 if (!s->eac3 || block->new_cpl_coords[ch] != 2)
01444 put_bits(&s->pb, 1, block->new_cpl_coords[ch]);
01445 if (block->new_cpl_coords[ch]) {
01446 put_bits(&s->pb, 2, block->cpl_master_exp[ch]);
01447 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
01448 put_bits(&s->pb, 4, block->cpl_coord_exp [ch][bnd]);
01449 put_bits(&s->pb, 4, block->cpl_coord_mant[ch][bnd]);
01450 }
01451 }
01452 }
01453 }
01454 }
01455
01456
01457 if (s->channel_mode == AC3_CHMODE_STEREO) {
01458 if (!s->eac3 || blk > 0)
01459 put_bits(&s->pb, 1, block->new_rematrixing_strategy);
01460 if (block->new_rematrixing_strategy) {
01461
01462 for (bnd = 0; bnd < block->num_rematrixing_bands; bnd++)
01463 put_bits(&s->pb, 1, block->rematrixing_flags[bnd]);
01464 }
01465 }
01466
01467
01468 if (!s->eac3) {
01469 for (ch = !block->cpl_in_use; ch <= s->fbw_channels; ch++)
01470 put_bits(&s->pb, 2, s->exp_strategy[ch][blk]);
01471 if (s->lfe_on)
01472 put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]);
01473 }
01474
01475
01476 for (ch = 1; ch <= s->fbw_channels; ch++) {
01477 if (s->exp_strategy[ch][blk] != EXP_REUSE && !block->channel_in_cpl[ch])
01478 put_bits(&s->pb, 6, s->bandwidth_code);
01479 }
01480
01481
01482 for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
01483 int nb_groups;
01484 int cpl = (ch == CPL_CH);
01485
01486 if (s->exp_strategy[ch][blk] == EXP_REUSE)
01487 continue;
01488
01489
01490 put_bits(&s->pb, 4, block->grouped_exp[ch][0] >> cpl);
01491
01492
01493 nb_groups = exponent_group_tab[cpl][s->exp_strategy[ch][blk]-1][block->end_freq[ch]-s->start_freq[ch]];
01494 for (i = 1; i <= nb_groups; i++)
01495 put_bits(&s->pb, 7, block->grouped_exp[ch][i]);
01496
01497
01498 if (ch != s->lfe_channel && !cpl)
01499 put_bits(&s->pb, 2, 0);
01500 }
01501
01502
01503 if (!s->eac3) {
01504 baie = (blk == 0);
01505 put_bits(&s->pb, 1, baie);
01506 if (baie) {
01507 put_bits(&s->pb, 2, s->slow_decay_code);
01508 put_bits(&s->pb, 2, s->fast_decay_code);
01509 put_bits(&s->pb, 2, s->slow_gain_code);
01510 put_bits(&s->pb, 2, s->db_per_bit_code);
01511 put_bits(&s->pb, 3, s->floor_code);
01512 }
01513 }
01514
01515
01516 if (!s->eac3) {
01517 put_bits(&s->pb, 1, block->new_snr_offsets);
01518 if (block->new_snr_offsets) {
01519 put_bits(&s->pb, 6, s->coarse_snr_offset);
01520 for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
01521 put_bits(&s->pb, 4, s->fine_snr_offset[ch]);
01522 put_bits(&s->pb, 3, s->fast_gain_code[ch]);
01523 }
01524 }
01525 } else {
01526 put_bits(&s->pb, 1, 0);
01527 }
01528
01529
01530 if (block->cpl_in_use) {
01531 if (!s->eac3 || block->new_cpl_leak != 2)
01532 put_bits(&s->pb, 1, block->new_cpl_leak);
01533 if (block->new_cpl_leak) {
01534 put_bits(&s->pb, 3, s->bit_alloc.cpl_fast_leak);
01535 put_bits(&s->pb, 3, s->bit_alloc.cpl_slow_leak);
01536 }
01537 }
01538
01539 if (!s->eac3) {
01540 put_bits(&s->pb, 1, 0);
01541 put_bits(&s->pb, 1, 0);
01542 }
01543
01544
01545 got_cpl = !block->cpl_in_use;
01546 for (ch = 1; ch <= s->channels; ch++) {
01547 int b, q;
01548
01549 if (!got_cpl && ch > 1 && block->channel_in_cpl[ch-1]) {
01550 ch0 = ch - 1;
01551 ch = CPL_CH;
01552 got_cpl = 1;
01553 }
01554 for (i = s->start_freq[ch]; i < block->end_freq[ch]; i++) {
01555 q = block->qmant[ch][i];
01556 b = s->ref_bap[ch][blk][i];
01557 switch (b) {
01558 case 0: break;
01559 case 1: if (q != 128) put_bits (&s->pb, 5, q); break;
01560 case 2: if (q != 128) put_bits (&s->pb, 7, q); break;
01561 case 3: put_sbits(&s->pb, 3, q); break;
01562 case 4: if (q != 128) put_bits (&s->pb, 7, q); break;
01563 case 14: put_sbits(&s->pb, 14, q); break;
01564 case 15: put_sbits(&s->pb, 16, q); break;
01565 default: put_sbits(&s->pb, b-1, q); break;
01566 }
01567 }
01568 if (ch == CPL_CH)
01569 ch = ch0;
01570 }
01571 }
01572
01573
01575 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
01576
01577
01578 static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly)
01579 {
01580 unsigned int c;
01581
01582 c = 0;
01583 while (a) {
01584 if (a & 1)
01585 c ^= b;
01586 a = a >> 1;
01587 b = b << 1;
01588 if (b & (1 << 16))
01589 b ^= poly;
01590 }
01591 return c;
01592 }
01593
01594
01595 static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly)
01596 {
01597 unsigned int r;
01598 r = 1;
01599 while (n) {
01600 if (n & 1)
01601 r = mul_poly(r, a, poly);
01602 a = mul_poly(a, a, poly);
01603 n >>= 1;
01604 }
01605 return r;
01606 }
01607
01608
01609
01610
01611
01612 static void output_frame_end(AC3EncodeContext *s)
01613 {
01614 const AVCRC *crc_ctx = av_crc_get_table(AV_CRC_16_ANSI);
01615 int frame_size_58, pad_bytes, crc1, crc2_partial, crc2, crc_inv;
01616 uint8_t *frame;
01617
01618 frame_size_58 = ((s->frame_size >> 2) + (s->frame_size >> 4)) << 1;
01619
01620
01621 av_assert2(s->frame_size * 8 - put_bits_count(&s->pb) >= 18);
01622 flush_put_bits(&s->pb);
01623 frame = s->pb.buf;
01624 pad_bytes = s->frame_size - (put_bits_ptr(&s->pb) - frame) - 2;
01625 av_assert2(pad_bytes >= 0);
01626 if (pad_bytes > 0)
01627 memset(put_bits_ptr(&s->pb), 0, pad_bytes);
01628
01629 if (s->eac3) {
01630
01631 crc2_partial = av_crc(crc_ctx, 0, frame + 2, s->frame_size - 5);
01632 } else {
01633
01634
01635 crc1 = av_bswap16(av_crc(crc_ctx, 0, frame + 4, frame_size_58 - 4));
01636 crc_inv = s->crc_inv[s->frame_size > s->frame_size_min];
01637 crc1 = mul_poly(crc_inv, crc1, CRC16_POLY);
01638 AV_WB16(frame + 2, crc1);
01639
01640
01641 crc2_partial = av_crc(crc_ctx, 0, frame + frame_size_58,
01642 s->frame_size - frame_size_58 - 3);
01643 }
01644 crc2 = av_crc(crc_ctx, crc2_partial, frame + s->frame_size - 3, 1);
01645
01646 if (crc2 == 0x770B) {
01647 frame[s->frame_size - 3] ^= 0x1;
01648 crc2 = av_crc(crc_ctx, crc2_partial, frame + s->frame_size - 3, 1);
01649 }
01650 crc2 = av_bswap16(crc2);
01651 AV_WB16(frame + s->frame_size - 2, crc2);
01652 }
01653
01654
01661 void ff_ac3_output_frame(AC3EncodeContext *s, unsigned char *frame)
01662 {
01663 int blk;
01664
01665 init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE);
01666
01667 s->output_frame_header(s);
01668
01669 for (blk = 0; blk < s->num_blocks; blk++)
01670 output_audio_block(s, blk);
01671
01672 output_frame_end(s);
01673 }
01674
01675
01676 static void dprint_options(AC3EncodeContext *s)
01677 {
01678 #ifdef DEBUG
01679 AVCodecContext *avctx = s->avctx;
01680 AC3EncOptions *opt = &s->options;
01681 char strbuf[32];
01682
01683 switch (s->bitstream_id) {
01684 case 6: av_strlcpy(strbuf, "AC-3 (alt syntax)", 32); break;
01685 case 8: av_strlcpy(strbuf, "AC-3 (standard)", 32); break;
01686 case 9: av_strlcpy(strbuf, "AC-3 (dnet half-rate)", 32); break;
01687 case 10: av_strlcpy(strbuf, "AC-3 (dnet quater-rate)", 32); break;
01688 case 16: av_strlcpy(strbuf, "E-AC-3 (enhanced)", 32); break;
01689 default: snprintf(strbuf, 32, "ERROR");
01690 }
01691 av_dlog(avctx, "bitstream_id: %s (%d)\n", strbuf, s->bitstream_id);
01692 av_dlog(avctx, "sample_fmt: %s\n", av_get_sample_fmt_name(avctx->sample_fmt));
01693 av_get_channel_layout_string(strbuf, 32, s->channels, avctx->channel_layout);
01694 av_dlog(avctx, "channel_layout: %s\n", strbuf);
01695 av_dlog(avctx, "sample_rate: %d\n", s->sample_rate);
01696 av_dlog(avctx, "bit_rate: %d\n", s->bit_rate);
01697 av_dlog(avctx, "blocks/frame: %d (code=%d)\n", s->num_blocks, s->num_blks_code);
01698 if (s->cutoff)
01699 av_dlog(avctx, "cutoff: %d\n", s->cutoff);
01700
01701 av_dlog(avctx, "per_frame_metadata: %s\n",
01702 opt->allow_per_frame_metadata?"on":"off");
01703 if (s->has_center)
01704 av_dlog(avctx, "center_mixlev: %0.3f (%d)\n", opt->center_mix_level,
01705 s->center_mix_level);
01706 else
01707 av_dlog(avctx, "center_mixlev: {not written}\n");
01708 if (s->has_surround)
01709 av_dlog(avctx, "surround_mixlev: %0.3f (%d)\n", opt->surround_mix_level,
01710 s->surround_mix_level);
01711 else
01712 av_dlog(avctx, "surround_mixlev: {not written}\n");
01713 if (opt->audio_production_info) {
01714 av_dlog(avctx, "mixing_level: %ddB\n", opt->mixing_level);
01715 switch (opt->room_type) {
01716 case AC3ENC_OPT_NOT_INDICATED: av_strlcpy(strbuf, "notindicated", 32); break;
01717 case AC3ENC_OPT_LARGE_ROOM: av_strlcpy(strbuf, "large", 32); break;
01718 case AC3ENC_OPT_SMALL_ROOM: av_strlcpy(strbuf, "small", 32); break;
01719 default: snprintf(strbuf, 32, "ERROR (%d)", opt->room_type);
01720 }
01721 av_dlog(avctx, "room_type: %s\n", strbuf);
01722 } else {
01723 av_dlog(avctx, "mixing_level: {not written}\n");
01724 av_dlog(avctx, "room_type: {not written}\n");
01725 }
01726 av_dlog(avctx, "copyright: %s\n", opt->copyright?"on":"off");
01727 av_dlog(avctx, "dialnorm: %ddB\n", opt->dialogue_level);
01728 if (s->channel_mode == AC3_CHMODE_STEREO) {
01729 switch (opt->dolby_surround_mode) {
01730 case AC3ENC_OPT_NOT_INDICATED: av_strlcpy(strbuf, "notindicated", 32); break;
01731 case AC3ENC_OPT_MODE_ON: av_strlcpy(strbuf, "on", 32); break;
01732 case AC3ENC_OPT_MODE_OFF: av_strlcpy(strbuf, "off", 32); break;
01733 default: snprintf(strbuf, 32, "ERROR (%d)", opt->dolby_surround_mode);
01734 }
01735 av_dlog(avctx, "dsur_mode: %s\n", strbuf);
01736 } else {
01737 av_dlog(avctx, "dsur_mode: {not written}\n");
01738 }
01739 av_dlog(avctx, "original: %s\n", opt->original?"on":"off");
01740
01741 if (s->bitstream_id == 6) {
01742 if (opt->extended_bsi_1) {
01743 switch (opt->preferred_stereo_downmix) {
01744 case AC3ENC_OPT_NOT_INDICATED: av_strlcpy(strbuf, "notindicated", 32); break;
01745 case AC3ENC_OPT_DOWNMIX_LTRT: av_strlcpy(strbuf, "ltrt", 32); break;
01746 case AC3ENC_OPT_DOWNMIX_LORO: av_strlcpy(strbuf, "loro", 32); break;
01747 default: snprintf(strbuf, 32, "ERROR (%d)", opt->preferred_stereo_downmix);
01748 }
01749 av_dlog(avctx, "dmix_mode: %s\n", strbuf);
01750 av_dlog(avctx, "ltrt_cmixlev: %0.3f (%d)\n",
01751 opt->ltrt_center_mix_level, s->ltrt_center_mix_level);
01752 av_dlog(avctx, "ltrt_surmixlev: %0.3f (%d)\n",
01753 opt->ltrt_surround_mix_level, s->ltrt_surround_mix_level);
01754 av_dlog(avctx, "loro_cmixlev: %0.3f (%d)\n",
01755 opt->loro_center_mix_level, s->loro_center_mix_level);
01756 av_dlog(avctx, "loro_surmixlev: %0.3f (%d)\n",
01757 opt->loro_surround_mix_level, s->loro_surround_mix_level);
01758 } else {
01759 av_dlog(avctx, "extended bitstream info 1: {not written}\n");
01760 }
01761 if (opt->extended_bsi_2) {
01762 switch (opt->dolby_surround_ex_mode) {
01763 case AC3ENC_OPT_NOT_INDICATED: av_strlcpy(strbuf, "notindicated", 32); break;
01764 case AC3ENC_OPT_MODE_ON: av_strlcpy(strbuf, "on", 32); break;
01765 case AC3ENC_OPT_MODE_OFF: av_strlcpy(strbuf, "off", 32); break;
01766 default: snprintf(strbuf, 32, "ERROR (%d)", opt->dolby_surround_ex_mode);
01767 }
01768 av_dlog(avctx, "dsurex_mode: %s\n", strbuf);
01769 switch (opt->dolby_headphone_mode) {
01770 case AC3ENC_OPT_NOT_INDICATED: av_strlcpy(strbuf, "notindicated", 32); break;
01771 case AC3ENC_OPT_MODE_ON: av_strlcpy(strbuf, "on", 32); break;
01772 case AC3ENC_OPT_MODE_OFF: av_strlcpy(strbuf, "off", 32); break;
01773 default: snprintf(strbuf, 32, "ERROR (%d)", opt->dolby_headphone_mode);
01774 }
01775 av_dlog(avctx, "dheadphone_mode: %s\n", strbuf);
01776
01777 switch (opt->ad_converter_type) {
01778 case AC3ENC_OPT_ADCONV_STANDARD: av_strlcpy(strbuf, "standard", 32); break;
01779 case AC3ENC_OPT_ADCONV_HDCD: av_strlcpy(strbuf, "hdcd", 32); break;
01780 default: snprintf(strbuf, 32, "ERROR (%d)", opt->ad_converter_type);
01781 }
01782 av_dlog(avctx, "ad_conv_type: %s\n", strbuf);
01783 } else {
01784 av_dlog(avctx, "extended bitstream info 2: {not written}\n");
01785 }
01786 }
01787 #endif
01788 }
01789
01790
01791 #define FLT_OPTION_THRESHOLD 0.01
01792
01793 static int validate_float_option(float v, const float *v_list, int v_list_size)
01794 {
01795 int i;
01796
01797 for (i = 0; i < v_list_size; i++) {
01798 if (v < (v_list[i] + FLT_OPTION_THRESHOLD) &&
01799 v > (v_list[i] - FLT_OPTION_THRESHOLD))
01800 break;
01801 }
01802 if (i == v_list_size)
01803 return -1;
01804
01805 return i;
01806 }
01807
01808
01809 static void validate_mix_level(void *log_ctx, const char *opt_name,
01810 float *opt_param, const float *list,
01811 int list_size, int default_value, int min_value,
01812 int *ctx_param)
01813 {
01814 int mixlev = validate_float_option(*opt_param, list, list_size);
01815 if (mixlev < min_value) {
01816 mixlev = default_value;
01817 if (*opt_param >= 0.0) {
01818 av_log(log_ctx, AV_LOG_WARNING, "requested %s is not valid. using "
01819 "default value: %0.3f\n", opt_name, list[mixlev]);
01820 }
01821 }
01822 *opt_param = list[mixlev];
01823 *ctx_param = mixlev;
01824 }
01825
01826
01833 int ff_ac3_validate_metadata(AC3EncodeContext *s)
01834 {
01835 AVCodecContext *avctx = s->avctx;
01836 AC3EncOptions *opt = &s->options;
01837
01838 opt->audio_production_info = 0;
01839 opt->extended_bsi_1 = 0;
01840 opt->extended_bsi_2 = 0;
01841 opt->eac3_mixing_metadata = 0;
01842 opt->eac3_info_metadata = 0;
01843
01844
01845 if (s->channel_mode > AC3_CHMODE_STEREO && opt->preferred_stereo_downmix != AC3ENC_OPT_NONE) {
01846 opt->extended_bsi_1 = 1;
01847 opt->eac3_mixing_metadata = 1;
01848 }
01849 if (s->has_center &&
01850 (opt->ltrt_center_mix_level >= 0 || opt->loro_center_mix_level >= 0)) {
01851 opt->extended_bsi_1 = 1;
01852 opt->eac3_mixing_metadata = 1;
01853 }
01854 if (s->has_surround &&
01855 (opt->ltrt_surround_mix_level >= 0 || opt->loro_surround_mix_level >= 0)) {
01856 opt->extended_bsi_1 = 1;
01857 opt->eac3_mixing_metadata = 1;
01858 }
01859
01860 if (s->eac3) {
01861
01862 if (avctx->audio_service_type != AV_AUDIO_SERVICE_TYPE_MAIN)
01863 opt->eac3_info_metadata = 1;
01864 if (opt->copyright != AC3ENC_OPT_NONE || opt->original != AC3ENC_OPT_NONE)
01865 opt->eac3_info_metadata = 1;
01866 if (s->channel_mode == AC3_CHMODE_STEREO &&
01867 (opt->dolby_headphone_mode != AC3ENC_OPT_NONE || opt->dolby_surround_mode != AC3ENC_OPT_NONE))
01868 opt->eac3_info_metadata = 1;
01869 if (s->channel_mode >= AC3_CHMODE_2F2R && opt->dolby_surround_ex_mode != AC3ENC_OPT_NONE)
01870 opt->eac3_info_metadata = 1;
01871 if (opt->mixing_level != AC3ENC_OPT_NONE || opt->room_type != AC3ENC_OPT_NONE ||
01872 opt->ad_converter_type != AC3ENC_OPT_NONE) {
01873 opt->audio_production_info = 1;
01874 opt->eac3_info_metadata = 1;
01875 }
01876 } else {
01877
01878 if (opt->mixing_level != AC3ENC_OPT_NONE || opt->room_type != AC3ENC_OPT_NONE)
01879 opt->audio_production_info = 1;
01880
01881
01882 if (s->channel_mode >= AC3_CHMODE_2F2R && opt->dolby_surround_ex_mode != AC3ENC_OPT_NONE)
01883 opt->extended_bsi_2 = 1;
01884 if (s->channel_mode == AC3_CHMODE_STEREO && opt->dolby_headphone_mode != AC3ENC_OPT_NONE)
01885 opt->extended_bsi_2 = 1;
01886 if (opt->ad_converter_type != AC3ENC_OPT_NONE)
01887 opt->extended_bsi_2 = 1;
01888 }
01889
01890
01891 if (!s->eac3) {
01892 if (s->has_center) {
01893 validate_mix_level(avctx, "center_mix_level", &opt->center_mix_level,
01894 cmixlev_options, CMIXLEV_NUM_OPTIONS, 1, 0,
01895 &s->center_mix_level);
01896 }
01897 if (s->has_surround) {
01898 validate_mix_level(avctx, "surround_mix_level", &opt->surround_mix_level,
01899 surmixlev_options, SURMIXLEV_NUM_OPTIONS, 1, 0,
01900 &s->surround_mix_level);
01901 }
01902 }
01903
01904
01905 if (opt->extended_bsi_1 || opt->eac3_mixing_metadata) {
01906
01907 if (opt->preferred_stereo_downmix == AC3ENC_OPT_NONE)
01908 opt->preferred_stereo_downmix = AC3ENC_OPT_NOT_INDICATED;
01909 if (!s->eac3 || s->has_center) {
01910
01911 validate_mix_level(avctx, "ltrt_center_mix_level",
01912 &opt->ltrt_center_mix_level, extmixlev_options,
01913 EXTMIXLEV_NUM_OPTIONS, 5, 0,
01914 &s->ltrt_center_mix_level);
01915
01916 validate_mix_level(avctx, "loro_center_mix_level",
01917 &opt->loro_center_mix_level, extmixlev_options,
01918 EXTMIXLEV_NUM_OPTIONS, 5, 0,
01919 &s->loro_center_mix_level);
01920 }
01921 if (!s->eac3 || s->has_surround) {
01922
01923 validate_mix_level(avctx, "ltrt_surround_mix_level",
01924 &opt->ltrt_surround_mix_level, extmixlev_options,
01925 EXTMIXLEV_NUM_OPTIONS, 6, 3,
01926 &s->ltrt_surround_mix_level);
01927
01928 validate_mix_level(avctx, "loro_surround_mix_level",
01929 &opt->loro_surround_mix_level, extmixlev_options,
01930 EXTMIXLEV_NUM_OPTIONS, 6, 3,
01931 &s->loro_surround_mix_level);
01932 }
01933 }
01934
01935
01936 if ((avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_KARAOKE &&
01937 avctx->channels == 1) ||
01938 ((avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_COMMENTARY ||
01939 avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_EMERGENCY ||
01940 avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_VOICE_OVER)
01941 && avctx->channels > 1)) {
01942 av_log(avctx, AV_LOG_ERROR, "invalid audio service type for the "
01943 "specified number of channels\n");
01944 return AVERROR(EINVAL);
01945 }
01946
01947
01948 if (opt->extended_bsi_2 || opt->eac3_info_metadata) {
01949
01950 if (opt->dolby_headphone_mode == AC3ENC_OPT_NONE)
01951 opt->dolby_headphone_mode = AC3ENC_OPT_NOT_INDICATED;
01952
01953 if (opt->dolby_surround_ex_mode == AC3ENC_OPT_NONE)
01954 opt->dolby_surround_ex_mode = AC3ENC_OPT_NOT_INDICATED;
01955
01956 if (opt->ad_converter_type == AC3ENC_OPT_NONE)
01957 opt->ad_converter_type = AC3ENC_OPT_ADCONV_STANDARD;
01958 }
01959
01960
01961 if (!s->eac3 || opt->eac3_info_metadata) {
01962
01963 if (opt->copyright == AC3ENC_OPT_NONE)
01964 opt->copyright = AC3ENC_OPT_OFF;
01965
01966 if (opt->original == AC3ENC_OPT_NONE)
01967 opt->original = AC3ENC_OPT_ON;
01968 }
01969
01970
01971 if (!s->eac3 || opt->eac3_info_metadata) {
01972 if (opt->dolby_surround_mode == AC3ENC_OPT_NONE)
01973 opt->dolby_surround_mode = AC3ENC_OPT_NOT_INDICATED;
01974 }
01975
01976
01977 if (opt->audio_production_info) {
01978 if (opt->mixing_level == AC3ENC_OPT_NONE) {
01979 av_log(avctx, AV_LOG_ERROR, "mixing_level must be set if "
01980 "room_type is set\n");
01981 return AVERROR(EINVAL);
01982 }
01983 if (opt->mixing_level < 80) {
01984 av_log(avctx, AV_LOG_ERROR, "invalid mixing level. must be between "
01985 "80dB and 111dB\n");
01986 return AVERROR(EINVAL);
01987 }
01988
01989 if (opt->room_type == AC3ENC_OPT_NONE)
01990 opt->room_type = AC3ENC_OPT_NOT_INDICATED;
01991 }
01992
01993
01994 if (!s->eac3 && (opt->extended_bsi_1 || opt->extended_bsi_2)) {
01995 if (s->bitstream_id > 8 && s->bitstream_id < 11) {
01996 static int warn_once = 1;
01997 if (warn_once) {
01998 av_log(avctx, AV_LOG_WARNING, "alternate bitstream syntax is "
01999 "not compatible with reduced samplerates. writing of "
02000 "extended bitstream information will be disabled.\n");
02001 warn_once = 0;
02002 }
02003 } else {
02004 s->bitstream_id = 6;
02005 }
02006 }
02007
02008 return 0;
02009 }
02010
02011
02017 av_cold int ff_ac3_encode_close(AVCodecContext *avctx)
02018 {
02019 int blk, ch;
02020 AC3EncodeContext *s = avctx->priv_data;
02021
02022 av_freep(&s->windowed_samples);
02023 for (ch = 0; ch < s->channels; ch++)
02024 av_freep(&s->planar_samples[ch]);
02025 av_freep(&s->planar_samples);
02026 av_freep(&s->bap_buffer);
02027 av_freep(&s->bap1_buffer);
02028 av_freep(&s->mdct_coef_buffer);
02029 av_freep(&s->fixed_coef_buffer);
02030 av_freep(&s->exp_buffer);
02031 av_freep(&s->grouped_exp_buffer);
02032 av_freep(&s->psd_buffer);
02033 av_freep(&s->band_psd_buffer);
02034 av_freep(&s->mask_buffer);
02035 av_freep(&s->qmant_buffer);
02036 av_freep(&s->cpl_coord_exp_buffer);
02037 av_freep(&s->cpl_coord_mant_buffer);
02038 for (blk = 0; blk < s->num_blocks; blk++) {
02039 AC3Block *block = &s->blocks[blk];
02040 av_freep(&block->mdct_coef);
02041 av_freep(&block->fixed_coef);
02042 av_freep(&block->exp);
02043 av_freep(&block->grouped_exp);
02044 av_freep(&block->psd);
02045 av_freep(&block->band_psd);
02046 av_freep(&block->mask);
02047 av_freep(&block->qmant);
02048 av_freep(&block->cpl_coord_exp);
02049 av_freep(&block->cpl_coord_mant);
02050 }
02051
02052 s->mdct_end(s);
02053
02054 av_freep(&avctx->coded_frame);
02055 return 0;
02056 }
02057
02058
02059
02060
02061
02062 static av_cold int set_channel_info(AC3EncodeContext *s, int channels,
02063 uint64_t *channel_layout)
02064 {
02065 int ch_layout;
02066
02067 if (channels < 1 || channels > AC3_MAX_CHANNELS)
02068 return AVERROR(EINVAL);
02069 if (*channel_layout > 0x7FF)
02070 return AVERROR(EINVAL);
02071 ch_layout = *channel_layout;
02072 if (!ch_layout)
02073 ch_layout = avcodec_guess_channel_layout(channels, CODEC_ID_AC3, NULL);
02074
02075 s->lfe_on = !!(ch_layout & AV_CH_LOW_FREQUENCY);
02076 s->channels = channels;
02077 s->fbw_channels = channels - s->lfe_on;
02078 s->lfe_channel = s->lfe_on ? s->fbw_channels + 1 : -1;
02079 if (s->lfe_on)
02080 ch_layout -= AV_CH_LOW_FREQUENCY;
02081
02082 switch (ch_layout) {
02083 case AV_CH_LAYOUT_MONO: s->channel_mode = AC3_CHMODE_MONO; break;
02084 case AV_CH_LAYOUT_STEREO: s->channel_mode = AC3_CHMODE_STEREO; break;
02085 case AV_CH_LAYOUT_SURROUND: s->channel_mode = AC3_CHMODE_3F; break;
02086 case AV_CH_LAYOUT_2_1: s->channel_mode = AC3_CHMODE_2F1R; break;
02087 case AV_CH_LAYOUT_4POINT0: s->channel_mode = AC3_CHMODE_3F1R; break;
02088 case AV_CH_LAYOUT_QUAD:
02089 case AV_CH_LAYOUT_2_2: s->channel_mode = AC3_CHMODE_2F2R; break;
02090 case AV_CH_LAYOUT_5POINT0:
02091 case AV_CH_LAYOUT_5POINT0_BACK: s->channel_mode = AC3_CHMODE_3F2R; break;
02092 default:
02093 return AVERROR(EINVAL);
02094 }
02095 s->has_center = (s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO;
02096 s->has_surround = s->channel_mode & 0x04;
02097
02098 s->channel_map = ff_ac3_enc_channel_map[s->channel_mode][s->lfe_on];
02099 *channel_layout = ch_layout;
02100 if (s->lfe_on)
02101 *channel_layout |= AV_CH_LOW_FREQUENCY;
02102
02103 return 0;
02104 }
02105
02106
02107 static av_cold int validate_options(AC3EncodeContext *s)
02108 {
02109 AVCodecContext *avctx = s->avctx;
02110 int i, ret, max_sr;
02111
02112
02113 if (!avctx->channel_layout) {
02114 av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "
02115 "encoder will guess the layout, but it "
02116 "might be incorrect.\n");
02117 }
02118 ret = set_channel_info(s, avctx->channels, &avctx->channel_layout);
02119 if (ret) {
02120 av_log(avctx, AV_LOG_ERROR, "invalid channel layout\n");
02121 return ret;
02122 }
02123
02124
02125
02126
02127
02128 max_sr = s->eac3 ? 2 : 8;
02129 for (i = 0; i <= max_sr; i++) {
02130 if ((ff_ac3_sample_rate_tab[i % 3] >> (i / 3)) == avctx->sample_rate)
02131 break;
02132 }
02133 if (i > max_sr) {
02134 av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
02135 return AVERROR(EINVAL);
02136 }
02137 s->sample_rate = avctx->sample_rate;
02138 s->bit_alloc.sr_shift = i / 3;
02139 s->bit_alloc.sr_code = i % 3;
02140 s->bitstream_id = s->eac3 ? 16 : 8 + s->bit_alloc.sr_shift;
02141
02142
02143 if (s->eac3) {
02144 int max_br, min_br, wpf, min_br_dist, min_br_code;
02145 int num_blks_code, num_blocks, frame_samples;
02146
02147
02148
02149
02150
02151 for (num_blks_code = 3; num_blks_code >= 0; num_blks_code--) {
02152 num_blocks = ((int[]){ 1, 2, 3, 6 })[num_blks_code];
02153 frame_samples = AC3_BLOCK_SIZE * num_blocks;
02154 max_br = 2048 * s->sample_rate / frame_samples * 16;
02155 min_br = ((s->sample_rate + (frame_samples-1)) / frame_samples) * 16;
02156 if (avctx->bit_rate <= max_br)
02157 break;
02158 }
02159 if (avctx->bit_rate < min_br || avctx->bit_rate > max_br) {
02160 av_log(avctx, AV_LOG_ERROR, "invalid bit rate. must be %d to %d "
02161 "for this sample rate\n", min_br, max_br);
02162 return AVERROR(EINVAL);
02163 }
02164 s->num_blks_code = num_blks_code;
02165 s->num_blocks = num_blocks;
02166
02167
02168 wpf = (avctx->bit_rate / 16) * frame_samples / s->sample_rate;
02169 av_assert1(wpf > 0 && wpf <= 2048);
02170
02171
02172
02173
02174 min_br_code = -1;
02175 min_br_dist = INT_MAX;
02176 for (i = 0; i < 19; i++) {
02177 int br_dist = abs(ff_ac3_bitrate_tab[i] * 1000 - avctx->bit_rate);
02178 if (br_dist < min_br_dist) {
02179 min_br_dist = br_dist;
02180 min_br_code = i;
02181 }
02182 }
02183
02184
02185 s->frame_size_code = min_br_code << 1;
02186 while (wpf > 1 && wpf * s->sample_rate / AC3_FRAME_SIZE * 16 > avctx->bit_rate)
02187 wpf--;
02188 s->frame_size_min = 2 * wpf;
02189 } else {
02190 for (i = 0; i < 19; i++) {
02191 if ((ff_ac3_bitrate_tab[i] >> s->bit_alloc.sr_shift)*1000 == avctx->bit_rate)
02192 break;
02193 }
02194 if (i == 19) {
02195 av_log(avctx, AV_LOG_ERROR, "invalid bit rate\n");
02196 return AVERROR(EINVAL);
02197 }
02198 s->frame_size_code = i << 1;
02199 s->frame_size_min = 2 * ff_ac3_frame_size_tab[s->frame_size_code][s->bit_alloc.sr_code];
02200 s->num_blks_code = 0x3;
02201 s->num_blocks = 6;
02202 }
02203 s->bit_rate = avctx->bit_rate;
02204 s->frame_size = s->frame_size_min;
02205
02206
02207 if (avctx->cutoff < 0) {
02208 av_log(avctx, AV_LOG_ERROR, "invalid cutoff frequency\n");
02209 return AVERROR(EINVAL);
02210 }
02211 s->cutoff = avctx->cutoff;
02212 if (s->cutoff > (s->sample_rate >> 1))
02213 s->cutoff = s->sample_rate >> 1;
02214
02215 ret = ff_ac3_validate_metadata(s);
02216 if (ret)
02217 return ret;
02218
02219 s->rematrixing_enabled = s->options.stereo_rematrixing &&
02220 (s->channel_mode == AC3_CHMODE_STEREO);
02221
02222 s->cpl_enabled = s->options.channel_coupling &&
02223 s->channel_mode >= AC3_CHMODE_STEREO;
02224
02225 return 0;
02226 }
02227
02228
02229
02230
02231
02232
02233
02234 static av_cold void set_bandwidth(AC3EncodeContext *s)
02235 {
02236 int blk, ch;
02237 int av_uninit(cpl_start);
02238
02239 if (s->cutoff) {
02240
02241 int fbw_coeffs;
02242 fbw_coeffs = s->cutoff * 2 * AC3_MAX_COEFS / s->sample_rate;
02243 s->bandwidth_code = av_clip((fbw_coeffs - 73) / 3, 0, 60);
02244 } else {
02245
02246 s->bandwidth_code = ac3_bandwidth_tab[s->fbw_channels-1][s->bit_alloc.sr_code][s->frame_size_code/2];
02247 }
02248
02249
02250 for (ch = 1; ch <= s->fbw_channels; ch++) {
02251 s->start_freq[ch] = 0;
02252 for (blk = 0; blk < s->num_blocks; blk++)
02253 s->blocks[blk].end_freq[ch] = s->bandwidth_code * 3 + 73;
02254 }
02255
02256 if (s->lfe_on) {
02257 s->start_freq[s->lfe_channel] = 0;
02258 for (blk = 0; blk < s->num_blocks; blk++)
02259 s->blocks[blk].end_freq[ch] = 7;
02260 }
02261
02262
02263 if (s->cpl_enabled) {
02264 if (s->options.cpl_start != AC3ENC_OPT_AUTO) {
02265 cpl_start = s->options.cpl_start;
02266 } else {
02267 cpl_start = ac3_coupling_start_tab[s->channel_mode-2][s->bit_alloc.sr_code][s->frame_size_code/2];
02268 if (cpl_start < 0) {
02269 if (s->options.channel_coupling == AC3ENC_OPT_AUTO)
02270 s->cpl_enabled = 0;
02271 else
02272 cpl_start = 15;
02273 }
02274 }
02275 }
02276 if (s->cpl_enabled) {
02277 int i, cpl_start_band, cpl_end_band;
02278 uint8_t *cpl_band_sizes = s->cpl_band_sizes;
02279
02280 cpl_end_band = s->bandwidth_code / 4 + 3;
02281 cpl_start_band = av_clip(cpl_start, 0, FFMIN(cpl_end_band-1, 15));
02282
02283 s->num_cpl_subbands = cpl_end_band - cpl_start_band;
02284
02285 s->num_cpl_bands = 1;
02286 *cpl_band_sizes = 12;
02287 for (i = cpl_start_band + 1; i < cpl_end_band; i++) {
02288 if (ff_eac3_default_cpl_band_struct[i]) {
02289 *cpl_band_sizes += 12;
02290 } else {
02291 s->num_cpl_bands++;
02292 cpl_band_sizes++;
02293 *cpl_band_sizes = 12;
02294 }
02295 }
02296
02297 s->start_freq[CPL_CH] = cpl_start_band * 12 + 37;
02298 s->cpl_end_freq = cpl_end_band * 12 + 37;
02299 for (blk = 0; blk < s->num_blocks; blk++)
02300 s->blocks[blk].end_freq[CPL_CH] = s->cpl_end_freq;
02301 }
02302 }
02303
02304
02305 static av_cold int allocate_buffers(AC3EncodeContext *s)
02306 {
02307 AVCodecContext *avctx = s->avctx;
02308 int blk, ch;
02309 int channels = s->channels + 1;
02310 int channel_blocks = channels * s->num_blocks;
02311 int total_coefs = AC3_MAX_COEFS * channel_blocks;
02312
02313 if (s->allocate_sample_buffers(s))
02314 goto alloc_fail;
02315
02316 FF_ALLOC_OR_GOTO(avctx, s->bap_buffer, total_coefs *
02317 sizeof(*s->bap_buffer), alloc_fail);
02318 FF_ALLOC_OR_GOTO(avctx, s->bap1_buffer, total_coefs *
02319 sizeof(*s->bap1_buffer), alloc_fail);
02320 FF_ALLOCZ_OR_GOTO(avctx, s->mdct_coef_buffer, total_coefs *
02321 sizeof(*s->mdct_coef_buffer), alloc_fail);
02322 FF_ALLOC_OR_GOTO(avctx, s->exp_buffer, total_coefs *
02323 sizeof(*s->exp_buffer), alloc_fail);
02324 FF_ALLOC_OR_GOTO(avctx, s->grouped_exp_buffer, channel_blocks * 128 *
02325 sizeof(*s->grouped_exp_buffer), alloc_fail);
02326 FF_ALLOC_OR_GOTO(avctx, s->psd_buffer, total_coefs *
02327 sizeof(*s->psd_buffer), alloc_fail);
02328 FF_ALLOC_OR_GOTO(avctx, s->band_psd_buffer, channel_blocks * 64 *
02329 sizeof(*s->band_psd_buffer), alloc_fail);
02330 FF_ALLOC_OR_GOTO(avctx, s->mask_buffer, channel_blocks * 64 *
02331 sizeof(*s->mask_buffer), alloc_fail);
02332 FF_ALLOC_OR_GOTO(avctx, s->qmant_buffer, total_coefs *
02333 sizeof(*s->qmant_buffer), alloc_fail);
02334 if (s->cpl_enabled) {
02335 FF_ALLOC_OR_GOTO(avctx, s->cpl_coord_exp_buffer, channel_blocks * 16 *
02336 sizeof(*s->cpl_coord_exp_buffer), alloc_fail);
02337 FF_ALLOC_OR_GOTO(avctx, s->cpl_coord_mant_buffer, channel_blocks * 16 *
02338 sizeof(*s->cpl_coord_mant_buffer), alloc_fail);
02339 }
02340 for (blk = 0; blk < s->num_blocks; blk++) {
02341 AC3Block *block = &s->blocks[blk];
02342 FF_ALLOCZ_OR_GOTO(avctx, block->mdct_coef, channels * sizeof(*block->mdct_coef),
02343 alloc_fail);
02344 FF_ALLOCZ_OR_GOTO(avctx, block->exp, channels * sizeof(*block->exp),
02345 alloc_fail);
02346 FF_ALLOCZ_OR_GOTO(avctx, block->grouped_exp, channels * sizeof(*block->grouped_exp),
02347 alloc_fail);
02348 FF_ALLOCZ_OR_GOTO(avctx, block->psd, channels * sizeof(*block->psd),
02349 alloc_fail);
02350 FF_ALLOCZ_OR_GOTO(avctx, block->band_psd, channels * sizeof(*block->band_psd),
02351 alloc_fail);
02352 FF_ALLOCZ_OR_GOTO(avctx, block->mask, channels * sizeof(*block->mask),
02353 alloc_fail);
02354 FF_ALLOCZ_OR_GOTO(avctx, block->qmant, channels * sizeof(*block->qmant),
02355 alloc_fail);
02356 if (s->cpl_enabled) {
02357 FF_ALLOCZ_OR_GOTO(avctx, block->cpl_coord_exp, channels * sizeof(*block->cpl_coord_exp),
02358 alloc_fail);
02359 FF_ALLOCZ_OR_GOTO(avctx, block->cpl_coord_mant, channels * sizeof(*block->cpl_coord_mant),
02360 alloc_fail);
02361 }
02362
02363 for (ch = 0; ch < channels; ch++) {
02364
02365 block->grouped_exp[ch] = &s->grouped_exp_buffer[128 * (blk * channels + ch)];
02366 block->psd[ch] = &s->psd_buffer [AC3_MAX_COEFS * (blk * channels + ch)];
02367 block->band_psd[ch] = &s->band_psd_buffer [64 * (blk * channels + ch)];
02368 block->mask[ch] = &s->mask_buffer [64 * (blk * channels + ch)];
02369 block->qmant[ch] = &s->qmant_buffer [AC3_MAX_COEFS * (blk * channels + ch)];
02370 if (s->cpl_enabled) {
02371 block->cpl_coord_exp[ch] = &s->cpl_coord_exp_buffer [16 * (blk * channels + ch)];
02372 block->cpl_coord_mant[ch] = &s->cpl_coord_mant_buffer[16 * (blk * channels + ch)];
02373 }
02374
02375
02376 block->exp[ch] = &s->exp_buffer [AC3_MAX_COEFS * (s->num_blocks * ch + blk)];
02377 block->mdct_coef[ch] = &s->mdct_coef_buffer [AC3_MAX_COEFS * (s->num_blocks * ch + blk)];
02378 }
02379 }
02380
02381 if (!s->fixed_point) {
02382 FF_ALLOCZ_OR_GOTO(avctx, s->fixed_coef_buffer, total_coefs *
02383 sizeof(*s->fixed_coef_buffer), alloc_fail);
02384 for (blk = 0; blk < s->num_blocks; blk++) {
02385 AC3Block *block = &s->blocks[blk];
02386 FF_ALLOCZ_OR_GOTO(avctx, block->fixed_coef, channels *
02387 sizeof(*block->fixed_coef), alloc_fail);
02388 for (ch = 0; ch < channels; ch++)
02389 block->fixed_coef[ch] = &s->fixed_coef_buffer[AC3_MAX_COEFS * (s->num_blocks * ch + blk)];
02390 }
02391 } else {
02392 for (blk = 0; blk < s->num_blocks; blk++) {
02393 AC3Block *block = &s->blocks[blk];
02394 FF_ALLOCZ_OR_GOTO(avctx, block->fixed_coef, channels *
02395 sizeof(*block->fixed_coef), alloc_fail);
02396 for (ch = 0; ch < channels; ch++)
02397 block->fixed_coef[ch] = (int32_t *)block->mdct_coef[ch];
02398 }
02399 }
02400
02401 return 0;
02402 alloc_fail:
02403 return AVERROR(ENOMEM);
02404 }
02405
02406
02407 av_cold int ff_ac3_encode_init(AVCodecContext *avctx)
02408 {
02409 AC3EncodeContext *s = avctx->priv_data;
02410 int ret, frame_size_58;
02411
02412 s->avctx = avctx;
02413
02414 s->eac3 = avctx->codec_id == CODEC_ID_EAC3;
02415
02416 ff_ac3_common_init();
02417
02418 ret = validate_options(s);
02419 if (ret)
02420 return ret;
02421
02422 avctx->frame_size = AC3_BLOCK_SIZE * s->num_blocks;
02423
02424 s->bitstream_mode = avctx->audio_service_type;
02425 if (s->bitstream_mode == AV_AUDIO_SERVICE_TYPE_KARAOKE)
02426 s->bitstream_mode = 0x7;
02427
02428 s->bits_written = 0;
02429 s->samples_written = 0;
02430
02431
02432 frame_size_58 = (( s->frame_size >> 2) + ( s->frame_size >> 4)) << 1;
02433 s->crc_inv[0] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);
02434 if (s->bit_alloc.sr_code == 1) {
02435 frame_size_58 = (((s->frame_size+2) >> 2) + ((s->frame_size+2) >> 4)) << 1;
02436 s->crc_inv[1] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);
02437 }
02438
02439
02440 if (CONFIG_AC3_FIXED_ENCODER && s->fixed_point) {
02441 s->mdct_end = ff_ac3_fixed_mdct_end;
02442 s->mdct_init = ff_ac3_fixed_mdct_init;
02443 s->allocate_sample_buffers = ff_ac3_fixed_allocate_sample_buffers;
02444 } else if (CONFIG_AC3_ENCODER || CONFIG_EAC3_ENCODER) {
02445 s->mdct_end = ff_ac3_float_mdct_end;
02446 s->mdct_init = ff_ac3_float_mdct_init;
02447 s->allocate_sample_buffers = ff_ac3_float_allocate_sample_buffers;
02448 }
02449 if (CONFIG_EAC3_ENCODER && s->eac3)
02450 s->output_frame_header = ff_eac3_output_frame_header;
02451 else
02452 s->output_frame_header = ac3_output_frame_header;
02453
02454 set_bandwidth(s);
02455
02456 exponent_init(s);
02457
02458 bit_alloc_init(s);
02459
02460 ret = s->mdct_init(s);
02461 if (ret)
02462 goto init_fail;
02463
02464 ret = allocate_buffers(s);
02465 if (ret)
02466 goto init_fail;
02467
02468 avctx->coded_frame= avcodec_alloc_frame();
02469
02470 dsputil_init(&s->dsp, avctx);
02471 ff_ac3dsp_init(&s->ac3dsp, avctx->flags & CODEC_FLAG_BITEXACT);
02472
02473 dprint_options(s);
02474
02475 return 0;
02476 init_fail:
02477 ff_ac3_encode_close(avctx);
02478 return ret;
02479 }