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