2
2
* The simplest AC3 encoder
3
3
* Copyright (c) 2000 Fabrice Bellard.
5
* This library is free software; you can redistribute it and/or
5
* This file is part of FFmpeg.
7
* FFmpeg is free software; you can redistribute it and/or
6
8
* modify it under the terms of the GNU Lesser General Public
7
9
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
12
* FFmpeg is distributed in the hope that it will be useful,
11
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
15
* Lesser General Public License for more details.
15
17
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
* License along with FFmpeg; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
46
51
short last_samples[AC3_MAX_CHANNELS][256];
47
52
unsigned int chbwcod[AC3_MAX_CHANNELS];
48
53
int nb_coefs[AC3_MAX_CHANNELS];
50
55
/* bitrate allocation control */
51
int sgaincod, sdecaycod, fdecaycod, dbkneecod, floorcod;
56
int sgaincod, sdecaycod, fdecaycod, dbkneecod, floorcod;
52
57
AC3BitAllocParameters bit_alloc;
54
59
int fgaincod[AC3_MAX_CHANNELS];
118
122
void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, uint8_t *bap,
119
123
int8_t *exp, int start, int end,
120
124
int snroffset, int fgain, int is_lfe,
121
int deltbae,int deltnseg,
125
int deltbae,int deltnseg,
122
126
uint8_t *deltoffst, uint8_t *deltlen, uint8_t *deltba)
124
128
int bin,i,j,k,end1,v,v1,bndstrt,bndend,lowcomp,begin;
190
194
if (end1 > 22) end1=22;
192
196
for (bin = begin; bin < end1; bin++) {
193
197
if (!(is_lfe && bin == 6))
194
198
lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin) ;
196
200
fastleak -= s->fdecay ;
197
201
v = bndpsd[bin] - fgain;
198
202
if (fastleak < v) fastleak = v;
200
204
slowleak -= s->sdecay ;
201
205
v = bndpsd[bin] - s->sgain;
202
206
if (slowleak < v) slowleak = v;
204
208
v=fastleak - lowcomp;
205
209
if (slowleak > v) v=slowleak;
211
215
/* coupling channel */
214
218
fastleak = (s->cplfleak << 8) + 768;
215
219
slowleak = (s->cplsleak << 8) + 768;
374
BF(p[0].re, p[0].im, p[2].re, p[2].im,
378
BF(p[0].re, p[0].im, p[2].re, p[2].im,
375
379
p[0].re, p[0].im, p[2].re, p[2].im);
376
BF(p[1].re, p[1].im, p[3].re, p[3].im,
380
BF(p[1].re, p[1].im, p[3].re, p[3].im,
377
381
p[1].re, p[1].im, p[3].im, -p[3].re);
379
383
} while (--j != 0);
461
465
/* estimate if the exponent variation & decide if they should be
462
466
reused in the next frame */
463
467
exp_strategy[0][ch] = EXP_NEW;
464
468
for(i=1;i<NB_BLOCKS;i++) {
465
469
exp_diff = calc_exp_diff(exp[i][ch], exp[i-1][ch], N/2);
467
printf("exp_diff=%d\n", exp_diff);
471
av_log(NULL, AV_LOG_DEBUG, "exp_diff=%d\n", exp_diff);
469
473
if (exp_diff > EXP_DIFF_THRESHOLD)
470
474
exp_strategy[i][ch] = EXP_NEW;
507
511
exp[i] = exp1[i];
511
515
/* update the exponents so that they are the ones the decoder will
512
516
decode. Return the number of bits used to code the exponents */
513
static int encode_exp(uint8_t encoded_exp[N/2],
517
static int encode_exp(uint8_t encoded_exp[N/2],
516
520
int exp_strategy)
518
int group_size, nb_groups, i, j, k, recurse, exp_min, delta;
522
int group_size, nb_groups, i, j, k, exp_min;
519
523
uint8_t exp1[N/2];
521
525
switch(exp_strategy) {
550
554
if (exp1[0] > 15)
553
/* Iterate until the delta constraints between each groups are
554
satisfyed. I'm sure it is possible to find a better algorithm,
558
for(i=1;i<=nb_groups;i++) {
559
delta = exp1[i] - exp1[i-1];
561
/* if delta too big, we encode a smaller exponent */
562
exp1[i] = exp1[i-1] + 2;
563
} else if (delta < -2) {
564
/* if delta is too small, we must decrease the previous
565
exponent, which means we must recurse */
567
exp1[i-1] = exp1[i] + 2;
557
/* Decrease the delta between each groups to within 2
558
* so that they can be differentially encoded */
559
for (i=1;i<=nb_groups;i++)
560
exp1[i] = FFMIN(exp1[i], exp1[i-1] + 2);
561
for (i=nb_groups-1;i>=0;i--)
562
exp1[i] = FFMIN(exp1[i], exp1[i+1] + 2);
572
564
/* now we have the exponent values the decoder will see */
573
565
encoded_exp[0] = exp1[0];
582
574
#if defined(DEBUG)
583
printf("exponents: strategy=%d\n", exp_strategy);
575
av_log(NULL, AV_LOG_DEBUG, "exponents: strategy=%d\n", exp_strategy);
584
576
for(i=0;i<=nb_groups * group_size;i++) {
585
printf("%d ", encoded_exp[i]);
577
av_log(NULL, AV_LOG_DEBUG, "%d ", encoded_exp[i]);
579
av_log(NULL, AV_LOG_DEBUG, "\n");
590
582
return 4 + (nb_groups / 3) * 7;
655
647
s->mant2_cnt = 0;
656
648
s->mant4_cnt = 0;
657
649
for(ch=0;ch<s->nb_all_channels;ch++) {
658
ac3_parametric_bit_allocation(&s->bit_alloc,
659
bap[i][ch], (int8_t *)encoded_exp[i][ch],
661
(((csnroffst-15) << 4) +
650
ac3_parametric_bit_allocation(&s->bit_alloc,
651
bap[i][ch], (int8_t *)encoded_exp[i][ch],
653
(((csnroffst-15) << 4) +
663
655
fgaintab[s->fgaincod[ch]],
664
656
ch == s->lfe_channel,
665
657
2, 0, NULL, NULL, NULL);
666
frame_bits += compute_mantissa_size(s, bap[i][ch],
658
frame_bits += compute_mantissa_size(s, bap[i][ch],
667
659
s->nb_coefs[ch]);
671
printf("csnr=%d fsnr=%d frame_bits=%d diff=%d\n",
672
csnroffst, fsnroffst, frame_bits,
663
printf("csnr=%d fsnr=%d frame_bits=%d diff=%d\n",
664
csnroffst, fsnroffst, frame_bits,
673
665
16 * s->frame_size - ((frame_bits + 7) & ~7));
675
667
return 16 * s->frame_size - frame_bits;
715
707
/* audio blocks */
716
708
for(i=0;i<NB_BLOCKS;i++) {
717
709
frame_bits += s->nb_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */
719
711
frame_bits++; /* rematstr */
712
if(i==0) frame_bits += 4;
720
714
frame_bits += 2 * s->nb_channels; /* chexpstr[2] * c */
722
frame_bits++; /* lfeexpstr */
716
frame_bits++; /* lfeexpstr */
723
717
for(ch=0;ch<s->nb_channels;ch++) {
724
718
if (exp_strategy[i][ch] != EXP_REUSE)
725
719
frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */
742
739
offset until we can pack everything in the requested frame size */
744
741
csnroffst = s->csnroffst;
745
while (csnroffst >= 0 &&
746
bit_alloc(s, bap, encoded_exp, exp_strategy, frame_bits, csnroffst, 0) < 0)
747
csnroffst -= SNR_INC1;
742
while (csnroffst >= 0 &&
743
bit_alloc(s, bap, encoded_exp, exp_strategy, frame_bits, csnroffst, 0) < 0)
744
csnroffst -= SNR_INC1;
748
745
if (csnroffst < 0) {
749
fprintf(stderr, "Yack, Error !!!\n");
746
av_log(NULL, AV_LOG_ERROR, "Bit allocation failed, try increasing the bitrate, -ab 384 for example!\n");
752
while ((csnroffst + SNR_INC1) <= 63 &&
753
bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits,
749
while ((csnroffst + SNR_INC1) <= 63 &&
750
bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits,
754
751
csnroffst + SNR_INC1, 0) >= 0) {
755
752
csnroffst += SNR_INC1;
756
753
memcpy(bap, bap1, sizeof(bap1));
758
while ((csnroffst + 1) <= 63 &&
755
while ((csnroffst + 1) <= 63 &&
759
756
bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, csnroffst + 1, 0) >= 0) {
761
758
memcpy(bap, bap1, sizeof(bap1));
765
while ((fsnroffst + SNR_INC1) <= 15 &&
766
bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits,
762
while ((fsnroffst + SNR_INC1) <= 15 &&
763
bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits,
767
764
csnroffst, fsnroffst + SNR_INC1) >= 0) {
768
765
fsnroffst += SNR_INC1;
769
766
memcpy(bap, bap1, sizeof(bap1));
771
while ((fsnroffst + 1) <= 15 &&
772
bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits,
768
while ((fsnroffst + 1) <= 15 &&
769
bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits,
773
770
csnroffst, fsnroffst + 1) >= 0) {
775
772
memcpy(bap, bap1, sizeof(bap1));
778
775
s->csnroffst = csnroffst;
779
776
for(ch=0;ch<s->nb_all_channels;ch++)
780
777
s->fsnroffst[ch] = fsnroffst;
824
821
static const uint8_t acmod_defs[6] = {
828
0x06, /* L R SL SR */
829
0x07, /* L C R SL SR */
830
0x07, /* L C R SL SR (+LFE) */
825
0x06, /* L R SL SR */
826
0x07, /* L C R SL SR */
827
0x07, /* L C R SL SR (+LFE) */
833
830
avctx->frame_size = AC3_FRAME_SIZE;
835
832
/* number of channels */
836
833
if (channels < 1 || channels > 6)
838
835
s->acmod = acmod_defs[channels - 1];
839
836
s->lfe = (channels == 6) ? 1 : 0;
840
837
s->nb_all_channels = channels;
904
900
/* output the AC3 frame header */
905
901
static void output_frame_header(AC3EncodeContext *s, unsigned char *frame)
907
init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE, NULL, NULL);
903
init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE);
909
905
put_bits(&s->pb, 16, 0x0b77); /* frame header */
910
906
put_bits(&s->pb, 16, 0); /* crc1: will be filled later */
914
910
put_bits(&s->pb, 3, s->bsmod);
915
911
put_bits(&s->pb, 3, s->acmod);
916
912
if ((s->acmod & 0x01) && s->acmod != 0x01)
917
put_bits(&s->pb, 2, 1); /* XXX -4.5 dB */
913
put_bits(&s->pb, 2, 1); /* XXX -4.5 dB */
918
914
if (s->acmod & 0x04)
919
put_bits(&s->pb, 2, 1); /* XXX -6 dB */
915
put_bits(&s->pb, 2, 1); /* XXX -6 dB */
920
916
if (s->acmod == 0x02)
921
917
put_bits(&s->pb, 2, 0); /* surround not indicated */
922
918
put_bits(&s->pb, 1, s->lfe); /* LFE */
986
982
uint16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr;
987
983
int delta0, delta1, delta2;
989
for(ch=0;ch<s->nb_channels;ch++)
985
for(ch=0;ch<s->nb_channels;ch++)
990
986
put_bits(&s->pb, 1, 0); /* 512 point MDCT */
991
for(ch=0;ch<s->nb_channels;ch++)
987
for(ch=0;ch<s->nb_channels;ch++)
992
988
put_bits(&s->pb, 1, 1); /* no dither */
993
989
put_bits(&s->pb, 1, 0); /* no dynamic range */
994
990
if (block_num == 0) {
1003
999
if (s->acmod == 2)
1007
/* first block must define rematrixing (rematstr) */
1008
put_bits(&s->pb, 1, 1);
1010
/* dummy rematrixing rematflg(1:4)=0 */
1011
for (rbnd=0;rbnd<4;rbnd++)
1012
put_bits(&s->pb, 1, 0);
1016
/* no matrixing (but should be used in the future) */
1017
put_bits(&s->pb, 1, 0);
1003
/* first block must define rematrixing (rematstr) */
1004
put_bits(&s->pb, 1, 1);
1006
/* dummy rematrixing rematflg(1:4)=0 */
1007
for (rbnd=0;rbnd<4;rbnd++)
1008
put_bits(&s->pb, 1, 0);
1012
/* no matrixing (but should be used in the future) */
1013
put_bits(&s->pb, 1, 0);
1023
1019
static int count = 0;
1024
printf("Block #%d (%d)\n", block_num, count++);
1020
av_log(NULL, AV_LOG_DEBUG, "Block #%d (%d)\n", block_num, count++);
1027
1023
/* exponent strategy */
1028
1024
for(ch=0;ch<s->nb_channels;ch++) {
1029
1025
put_bits(&s->pb, 2, exp_strategy[ch]);
1033
put_bits(&s->pb, 1, exp_strategy[s->lfe_channel]);
1029
put_bits(&s->pb, 1, exp_strategy[s->lfe_channel]);
1036
1032
for(ch=0;ch<s->nb_channels;ch++) {
1037
1033
if (exp_strategy[ch] != EXP_REUSE)
1038
1034
put_bits(&s->pb, 6, s->chbwcod[ch]);
1041
1037
/* exponents */
1042
1038
for (ch = 0; ch < s->nb_all_channels; ch++) {
1043
1039
switch(exp_strategy[ch]) {
1082
1078
put_bits(&s->pb, 7, ((delta0 * 5 + delta1) * 5) + delta2);
1085
if (ch != s->lfe_channel)
1086
put_bits(&s->pb, 2, 0); /* no gain range info */
1081
if (ch != s->lfe_channel)
1082
put_bits(&s->pb, 2, 0); /* no gain range info */
1089
1085
/* bit allocation info */
1246
/* compute the ac3 crc */
1248
1242
#define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1250
static void ac3_crc_init(void)
1252
unsigned int c, n, k;
1254
for(n=0;n<256;n++) {
1256
for (k = 0; k < 8; k++) {
1258
c = ((c << 1) & 0xffff) ^ (CRC16_POLY & 0xffff);
1266
static unsigned int ac3_crc(uint8_t *data, int n, unsigned int crc)
1270
crc = (crc_table[data[i] ^ (crc >> 8)] ^ (crc << 8)) & 0xffff;
1275
1244
static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly)
1277
1246
unsigned int c;
1345
1314
assert(n >= 0);
1347
1316
memset(pbBufPtr(&s->pb), 0, n);
1349
1318
/* Now we must compute both crcs : this is not so easy for crc1
1350
1319
because it is at the beginning of the data... */
1351
1320
frame_size_58 = (frame_size >> 1) + (frame_size >> 3);
1352
crc1 = ac3_crc(frame + 4, (2 * frame_size_58) - 4, 0);
1321
crc1 = bswap_16(av_crc(av_crc8005, 0, frame + 4, 2 * frame_size_58 - 4));
1353
1322
/* XXX: could precompute crc_inv */
1354
1323
crc_inv = pow_poly((CRC16_POLY >> 1), (16 * frame_size_58) - 16, CRC16_POLY);
1355
1324
crc1 = mul_poly(crc_inv, crc1, CRC16_POLY);
1356
1325
frame[2] = crc1 >> 8;
1357
1326
frame[3] = crc1;
1359
crc2 = ac3_crc(frame + 2 * frame_size_58, (frame_size - frame_size_58) * 2 - 2, 0);
1328
crc2 = bswap_16(av_crc(av_crc8005, 0, frame + 2 * frame_size_58, (frame_size - frame_size_58) * 2 - 2));
1360
1329
frame[2*frame_size - 2] = crc2 >> 8;
1361
1330
frame[2*frame_size - 1] = crc2;
1393
1362
for(j=0;j<N/2;j++) {
1395
1364
input_samples[j + N/2] = v;
1396
s->last_samples[ch][j] = v;
1365
s->last_samples[ch][j] = v;
1400
1369
/* apply the MDCT window */
1401
1370
for(j=0;j<N/2;j++) {
1402
input_samples[j] = MUL16(input_samples[j],
1371
input_samples[j] = MUL16(input_samples[j],
1403
1372
ac3_window[j]) >> 15;
1404
input_samples[N-j-1] = MUL16(input_samples[N-j-1],
1373
input_samples[N-j-1] = MUL16(input_samples[N-j-1],
1405
1374
ac3_window[j]) >> 15;
1408
1377
/* Normalize the samples to use the maximum available
1410
1379
v = 14 - log2_tab(input_samples, N);
1413
exp_samples[i][ch] = v - 8;
1382
exp_samples[i][ch] = v - 10;
1414
1383
lshift_tab(input_samples, N, v);
1416
1385
/* do the MDCT */
1417
1386
mdct512(mdct_coef[i][ch], input_samples);
1419
1388
/* compute "exponents". We take into account the
1420
1389
normalization there */
1421
1390
for(j=0;j<N/2;j++) {
1449
1418
frame_bits += encode_exp(encoded_exp[i][ch],
1450
exp[i][ch], s->nb_coefs[ch],
1419
exp[i][ch], s->nb_coefs[ch],
1451
1420
exp_strategy[i][ch]);
1452
1421
/* copy encoded exponents for reuse case */
1453
1422
for(k=i+1;k<j;k++) {
1454
memcpy(encoded_exp[k][ch], encoded_exp[i][ch],
1423
memcpy(encoded_exp[k][ch], encoded_exp[i][ch],
1455
1424
s->nb_coefs[ch] * sizeof(uint8_t));
1430
/* adjust for fractional frame sizes */
1431
while(s->bits_written >= s->bit_rate*1000 && s->samples_written >= s->sample_rate) {
1432
s->bits_written -= s->bit_rate*1000;
1433
s->samples_written -= s->sample_rate;
1435
s->frame_size = s->frame_size_min + (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate*1000);
1436
s->bits_written += s->frame_size * 16;
1437
s->samples_written += AC3_FRAME_SIZE;
1461
1439
compute_bit_allocation(s, bap, encoded_exp, exp_strategy, frame_bits);
1462
1440
/* everything is known... let's output the frame */
1463
1441
output_frame_header(s, frame);
1465
1443
for(i=0;i<NB_BLOCKS;i++) {
1466
output_audio_block(s, exp_strategy[i], encoded_exp[i],
1444
output_audio_block(s, exp_strategy[i], encoded_exp[i],
1467
1445
bap[i], mdct_coef[i], exp_samples[i], i);
1469
1447
return output_frame_end(s);
1505
1483
sum_re += in1[n].re * cos(a) - in1[n].im * sin(a);
1506
1484
sum_im += in1[n].re * sin(a) + in1[n].im * cos(a);
1508
printf("%3d: %6d,%6d %6.0f,%6.0f\n",
1509
k, in[k].re, in[k].im, sum_re / FN, sum_im / FN);
1486
printf("%3d: %6d,%6d %6.0f,%6.0f\n",
1487
k, in[k].re, in[k].im, sum_re / FN, sum_im / FN);