Mercurial > libavcodec.hg
comparison ac3enc.c @ 1064:b32afefe7d33 libavcodec
* UINTX -> uintx_t INTX -> intx_t
author | kabi |
---|---|
date | Tue, 11 Feb 2003 16:35:48 +0000 |
parents | bb5de8a59da8 |
children | 1e39f273ecd6 |
comparison
equal
deleted
inserted
replaced
1063:fdeac9642346 | 1064:b32afefe7d33 |
---|---|
61 #define EXP_DIFF_THRESHOLD 1000 | 61 #define EXP_DIFF_THRESHOLD 1000 |
62 | 62 |
63 static void fft_init(int ln); | 63 static void fft_init(int ln); |
64 static void ac3_crc_init(void); | 64 static void ac3_crc_init(void); |
65 | 65 |
66 static inline INT16 fix15(float a) | 66 static inline int16_t fix15(float a) |
67 { | 67 { |
68 int v; | 68 int v; |
69 v = (int)(a * (float)(1 << 15)); | 69 v = (int)(a * (float)(1 << 15)); |
70 if (v < -32767) | 70 if (v < -32767) |
71 v = -32767; | 71 v = -32767; |
108 return a; | 108 return a; |
109 } | 109 } |
110 | 110 |
111 /* AC3 bit allocation. The algorithm is the one described in the AC3 | 111 /* AC3 bit allocation. The algorithm is the one described in the AC3 |
112 spec. */ | 112 spec. */ |
113 void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, UINT8 *bap, | 113 void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, uint8_t *bap, |
114 INT8 *exp, int start, int end, | 114 int8_t *exp, int start, int end, |
115 int snroffset, int fgain, int is_lfe, | 115 int snroffset, int fgain, int is_lfe, |
116 int deltbae,int deltnseg, | 116 int deltbae,int deltnseg, |
117 UINT8 *deltoffst, UINT8 *deltlen, UINT8 *deltba) | 117 uint8_t *deltoffst, uint8_t *deltlen, uint8_t *deltba) |
118 { | 118 { |
119 int bin,i,j,k,end1,v,v1,bndstrt,bndend,lowcomp,begin; | 119 int bin,i,j,k,end1,v,v1,bndstrt,bndend,lowcomp,begin; |
120 int fastleak,slowleak,address,tmp; | 120 int fastleak,slowleak,address,tmp; |
121 INT16 psd[256]; /* scaled exponents */ | 121 int16_t psd[256]; /* scaled exponents */ |
122 INT16 bndpsd[50]; /* interpolated exponents */ | 122 int16_t bndpsd[50]; /* interpolated exponents */ |
123 INT16 excite[50]; /* excitation */ | 123 int16_t excite[50]; /* excitation */ |
124 INT16 mask[50]; /* masking value */ | 124 int16_t mask[50]; /* masking value */ |
125 | 125 |
126 /* exponent mapping to PSD */ | 126 /* exponent mapping to PSD */ |
127 for(bin=start;bin<end;bin++) { | 127 for(bin=start;bin<end;bin++) { |
128 psd[bin]=(3072 - (exp[bin] << 7)); | 128 psd[bin]=(3072 - (exp[bin] << 7)); |
129 } | 129 } |
402 nloops = nloops << 1; | 402 nloops = nloops << 1; |
403 } while (nblocks != 0); | 403 } while (nblocks != 0); |
404 } | 404 } |
405 | 405 |
406 /* do a 512 point mdct */ | 406 /* do a 512 point mdct */ |
407 static void mdct512(INT32 *out, INT16 *in) | 407 static void mdct512(int32_t *out, int16_t *in) |
408 { | 408 { |
409 int i, re, im, re1, im1; | 409 int i, re, im, re1, im1; |
410 INT16 rot[N]; | 410 int16_t rot[N]; |
411 IComplex x[N/4]; | 411 IComplex x[N/4]; |
412 | 412 |
413 /* shift to simplify computations */ | 413 /* shift to simplify computations */ |
414 for(i=0;i<N/4;i++) | 414 for(i=0;i<N/4;i++) |
415 rot[i] = -in[i + 3*N/4]; | 415 rot[i] = -in[i + 3*N/4]; |
434 out[N/2-1-2*i] = re1; | 434 out[N/2-1-2*i] = re1; |
435 } | 435 } |
436 } | 436 } |
437 | 437 |
438 /* XXX: use another norm ? */ | 438 /* XXX: use another norm ? */ |
439 static int calc_exp_diff(UINT8 *exp1, UINT8 *exp2, int n) | 439 static int calc_exp_diff(uint8_t *exp1, uint8_t *exp2, int n) |
440 { | 440 { |
441 int sum, i; | 441 int sum, i; |
442 sum = 0; | 442 sum = 0; |
443 for(i=0;i<n;i++) { | 443 for(i=0;i<n;i++) { |
444 sum += abs(exp1[i] - exp2[i]); | 444 sum += abs(exp1[i] - exp2[i]); |
445 } | 445 } |
446 return sum; | 446 return sum; |
447 } | 447 } |
448 | 448 |
449 static void compute_exp_strategy(UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], | 449 static void compute_exp_strategy(uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], |
450 UINT8 exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], | 450 uint8_t exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], |
451 int ch, int is_lfe) | 451 int ch, int is_lfe) |
452 { | 452 { |
453 int i, j; | 453 int i, j; |
454 int exp_diff; | 454 int exp_diff; |
455 | 455 |
491 i = j; | 491 i = j; |
492 } | 492 } |
493 } | 493 } |
494 | 494 |
495 /* set exp[i] to min(exp[i], exp1[i]) */ | 495 /* set exp[i] to min(exp[i], exp1[i]) */ |
496 static void exponent_min(UINT8 exp[N/2], UINT8 exp1[N/2], int n) | 496 static void exponent_min(uint8_t exp[N/2], uint8_t exp1[N/2], int n) |
497 { | 497 { |
498 int i; | 498 int i; |
499 | 499 |
500 for(i=0;i<n;i++) { | 500 for(i=0;i<n;i++) { |
501 if (exp1[i] < exp[i]) | 501 if (exp1[i] < exp[i]) |
503 } | 503 } |
504 } | 504 } |
505 | 505 |
506 /* update the exponents so that they are the ones the decoder will | 506 /* update the exponents so that they are the ones the decoder will |
507 decode. Return the number of bits used to code the exponents */ | 507 decode. Return the number of bits used to code the exponents */ |
508 static int encode_exp(UINT8 encoded_exp[N/2], | 508 static int encode_exp(uint8_t encoded_exp[N/2], |
509 UINT8 exp[N/2], | 509 uint8_t exp[N/2], |
510 int nb_exps, | 510 int nb_exps, |
511 int exp_strategy) | 511 int exp_strategy) |
512 { | 512 { |
513 int group_size, nb_groups, i, j, k, recurse, exp_min, delta; | 513 int group_size, nb_groups, i, j, k, recurse, exp_min, delta; |
514 UINT8 exp1[N/2]; | 514 uint8_t exp1[N/2]; |
515 | 515 |
516 switch(exp_strategy) { | 516 switch(exp_strategy) { |
517 case EXP_D15: | 517 case EXP_D15: |
518 group_size = 1; | 518 group_size = 1; |
519 break; | 519 break; |
584 | 584 |
585 return 4 + (nb_groups / 3) * 7; | 585 return 4 + (nb_groups / 3) * 7; |
586 } | 586 } |
587 | 587 |
588 /* return the size in bits taken by the mantissa */ | 588 /* return the size in bits taken by the mantissa */ |
589 static int compute_mantissa_size(AC3EncodeContext *s, UINT8 *m, int nb_coefs) | 589 static int compute_mantissa_size(AC3EncodeContext *s, uint8_t *m, int nb_coefs) |
590 { | 590 { |
591 int bits, mant, i; | 591 int bits, mant, i; |
592 | 592 |
593 bits = 0; | 593 bits = 0; |
594 for(i=0;i<nb_coefs;i++) { | 594 for(i=0;i<nb_coefs;i++) { |
635 return bits; | 635 return bits; |
636 } | 636 } |
637 | 637 |
638 | 638 |
639 static int bit_alloc(AC3EncodeContext *s, | 639 static int bit_alloc(AC3EncodeContext *s, |
640 UINT8 bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], | 640 uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], |
641 UINT8 encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], | 641 uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], |
642 UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], | 642 uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], |
643 int frame_bits, int csnroffst, int fsnroffst) | 643 int frame_bits, int csnroffst, int fsnroffst) |
644 { | 644 { |
645 int i, ch; | 645 int i, ch; |
646 | 646 |
647 /* compute size */ | 647 /* compute size */ |
649 s->mant1_cnt = 0; | 649 s->mant1_cnt = 0; |
650 s->mant2_cnt = 0; | 650 s->mant2_cnt = 0; |
651 s->mant4_cnt = 0; | 651 s->mant4_cnt = 0; |
652 for(ch=0;ch<s->nb_all_channels;ch++) { | 652 for(ch=0;ch<s->nb_all_channels;ch++) { |
653 ac3_parametric_bit_allocation(&s->bit_alloc, | 653 ac3_parametric_bit_allocation(&s->bit_alloc, |
654 bap[i][ch], (INT8 *)encoded_exp[i][ch], | 654 bap[i][ch], (int8_t *)encoded_exp[i][ch], |
655 0, s->nb_coefs[ch], | 655 0, s->nb_coefs[ch], |
656 (((csnroffst-15) << 4) + | 656 (((csnroffst-15) << 4) + |
657 fsnroffst) << 2, | 657 fsnroffst) << 2, |
658 fgaintab[s->fgaincod[ch]], | 658 fgaintab[s->fgaincod[ch]], |
659 ch == s->lfe_channel, | 659 ch == s->lfe_channel, |
671 } | 671 } |
672 | 672 |
673 #define SNR_INC1 4 | 673 #define SNR_INC1 4 |
674 | 674 |
675 static int compute_bit_allocation(AC3EncodeContext *s, | 675 static int compute_bit_allocation(AC3EncodeContext *s, |
676 UINT8 bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], | 676 uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], |
677 UINT8 encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], | 677 uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], |
678 UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], | 678 uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], |
679 int frame_bits) | 679 int frame_bits) |
680 { | 680 { |
681 int i, ch; | 681 int i, ch; |
682 int csnroffst, fsnroffst; | 682 int csnroffst, fsnroffst; |
683 UINT8 bap1[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; | 683 uint8_t bap1[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; |
684 static int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 }; | 684 static int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 }; |
685 | 685 |
686 /* init default parameters */ | 686 /* init default parameters */ |
687 s->sdecaycod = 2; | 687 s->sdecaycod = 2; |
688 s->fdecaycod = 1; | 688 s->fdecaycod = 1; |
814 int bitrate = avctx->bit_rate; | 814 int bitrate = avctx->bit_rate; |
815 int channels = avctx->channels; | 815 int channels = avctx->channels; |
816 AC3EncodeContext *s = avctx->priv_data; | 816 AC3EncodeContext *s = avctx->priv_data; |
817 int i, j, ch; | 817 int i, j, ch; |
818 float alpha; | 818 float alpha; |
819 static const UINT8 acmod_defs[6] = { | 819 static const uint8_t acmod_defs[6] = { |
820 0x01, /* C */ | 820 0x01, /* C */ |
821 0x02, /* L R */ | 821 0x02, /* L R */ |
822 0x03, /* L C R */ | 822 0x03, /* L C R */ |
823 0x06, /* L R SL SR */ | 823 0x06, /* L R SL SR */ |
824 0x07, /* L C R SL SR */ | 824 0x07, /* L C R SL SR */ |
964 } | 964 } |
965 | 965 |
966 /* Output one audio block. There are NB_BLOCKS audio blocks in one AC3 | 966 /* Output one audio block. There are NB_BLOCKS audio blocks in one AC3 |
967 frame */ | 967 frame */ |
968 static void output_audio_block(AC3EncodeContext *s, | 968 static void output_audio_block(AC3EncodeContext *s, |
969 UINT8 exp_strategy[AC3_MAX_CHANNELS], | 969 uint8_t exp_strategy[AC3_MAX_CHANNELS], |
970 UINT8 encoded_exp[AC3_MAX_CHANNELS][N/2], | 970 uint8_t encoded_exp[AC3_MAX_CHANNELS][N/2], |
971 UINT8 bap[AC3_MAX_CHANNELS][N/2], | 971 uint8_t bap[AC3_MAX_CHANNELS][N/2], |
972 INT32 mdct_coefs[AC3_MAX_CHANNELS][N/2], | 972 int32_t mdct_coefs[AC3_MAX_CHANNELS][N/2], |
973 INT8 global_exp[AC3_MAX_CHANNELS], | 973 int8_t global_exp[AC3_MAX_CHANNELS], |
974 int block_num) | 974 int block_num) |
975 { | 975 { |
976 int ch, nb_groups, group_size, i, baie; | 976 int ch, nb_groups, group_size, i, baie; |
977 UINT8 *p; | 977 uint8_t *p; |
978 UINT16 qmant[AC3_MAX_CHANNELS][N/2]; | 978 uint16_t qmant[AC3_MAX_CHANNELS][N/2]; |
979 int exp0, exp1; | 979 int exp0, exp1; |
980 int mant1_cnt, mant2_cnt, mant4_cnt; | 980 int mant1_cnt, mant2_cnt, mant4_cnt; |
981 UINT16 *qmant1_ptr, *qmant2_ptr, *qmant4_ptr; | 981 uint16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr; |
982 int delta0, delta1, delta2; | 982 int delta0, delta1, delta2; |
983 | 983 |
984 for(ch=0;ch<s->nb_channels;ch++) | 984 for(ch=0;ch<s->nb_channels;ch++) |
985 put_bits(&s->pb, 1, 0); /* 512 point MDCT */ | 985 put_bits(&s->pb, 1, 0); /* 512 point MDCT */ |
986 for(ch=0;ch<s->nb_channels;ch++) | 986 for(ch=0;ch<s->nb_channels;ch++) |
1242 } | 1242 } |
1243 crc_table[n] = c; | 1243 crc_table[n] = c; |
1244 } | 1244 } |
1245 } | 1245 } |
1246 | 1246 |
1247 static unsigned int ac3_crc(UINT8 *data, int n, unsigned int crc) | 1247 static unsigned int ac3_crc(uint8_t *data, int n, unsigned int crc) |
1248 { | 1248 { |
1249 int i; | 1249 int i; |
1250 for(i=0;i<n;i++) { | 1250 for(i=0;i<n;i++) { |
1251 crc = (crc_table[data[i] ^ (crc >> 8)] ^ (crc << 8)) & 0xffff; | 1251 crc = (crc_table[data[i] ^ (crc >> 8)] ^ (crc << 8)) & 0xffff; |
1252 } | 1252 } |
1282 return r; | 1282 return r; |
1283 } | 1283 } |
1284 | 1284 |
1285 | 1285 |
1286 /* compute log2(max(abs(tab[]))) */ | 1286 /* compute log2(max(abs(tab[]))) */ |
1287 static int log2_tab(INT16 *tab, int n) | 1287 static int log2_tab(int16_t *tab, int n) |
1288 { | 1288 { |
1289 int i, v; | 1289 int i, v; |
1290 | 1290 |
1291 v = 0; | 1291 v = 0; |
1292 for(i=0;i<n;i++) { | 1292 for(i=0;i<n;i++) { |
1293 v |= abs(tab[i]); | 1293 v |= abs(tab[i]); |
1294 } | 1294 } |
1295 return av_log2(v); | 1295 return av_log2(v); |
1296 } | 1296 } |
1297 | 1297 |
1298 static void lshift_tab(INT16 *tab, int n, int lshift) | 1298 static void lshift_tab(int16_t *tab, int n, int lshift) |
1299 { | 1299 { |
1300 int i; | 1300 int i; |
1301 | 1301 |
1302 if (lshift > 0) { | 1302 if (lshift > 0) { |
1303 for(i=0;i<n;i++) { | 1303 for(i=0;i<n;i++) { |
1313 | 1313 |
1314 /* fill the end of the frame and compute the two crcs */ | 1314 /* fill the end of the frame and compute the two crcs */ |
1315 static int output_frame_end(AC3EncodeContext *s) | 1315 static int output_frame_end(AC3EncodeContext *s) |
1316 { | 1316 { |
1317 int frame_size, frame_size_58, n, crc1, crc2, crc_inv; | 1317 int frame_size, frame_size_58, n, crc1, crc2, crc_inv; |
1318 UINT8 *frame; | 1318 uint8_t *frame; |
1319 | 1319 |
1320 frame_size = s->frame_size; /* frame size in words */ | 1320 frame_size = s->frame_size; /* frame size in words */ |
1321 /* align to 8 bits */ | 1321 /* align to 8 bits */ |
1322 flush_put_bits(&s->pb); | 1322 flush_put_bits(&s->pb); |
1323 /* add zero bytes to reach the frame size */ | 1323 /* add zero bytes to reach the frame size */ |
1348 unsigned char *frame, int buf_size, void *data) | 1348 unsigned char *frame, int buf_size, void *data) |
1349 { | 1349 { |
1350 AC3EncodeContext *s = avctx->priv_data; | 1350 AC3EncodeContext *s = avctx->priv_data; |
1351 short *samples = data; | 1351 short *samples = data; |
1352 int i, j, k, v, ch; | 1352 int i, j, k, v, ch; |
1353 INT16 input_samples[N]; | 1353 int16_t input_samples[N]; |
1354 INT32 mdct_coef[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; | 1354 int32_t mdct_coef[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; |
1355 UINT8 exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; | 1355 uint8_t exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; |
1356 UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS]; | 1356 uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS]; |
1357 UINT8 encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; | 1357 uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; |
1358 UINT8 bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; | 1358 uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; |
1359 INT8 exp_samples[NB_BLOCKS][AC3_MAX_CHANNELS]; | 1359 int8_t exp_samples[NB_BLOCKS][AC3_MAX_CHANNELS]; |
1360 int frame_bits; | 1360 int frame_bits; |
1361 | 1361 |
1362 frame_bits = 0; | 1362 frame_bits = 0; |
1363 for(ch=0;ch<s->nb_all_channels;ch++) { | 1363 for(ch=0;ch<s->nb_all_channels;ch++) { |
1364 /* fixed mdct to the six sub blocks & exponent computation */ | 1364 /* fixed mdct to the six sub blocks & exponent computation */ |
1365 for(i=0;i<NB_BLOCKS;i++) { | 1365 for(i=0;i<NB_BLOCKS;i++) { |
1366 INT16 *sptr; | 1366 int16_t *sptr; |
1367 int sinc; | 1367 int sinc; |
1368 | 1368 |
1369 /* compute input samples */ | 1369 /* compute input samples */ |
1370 memcpy(input_samples, s->last_samples[ch], N/2 * sizeof(INT16)); | 1370 memcpy(input_samples, s->last_samples[ch], N/2 * sizeof(int16_t)); |
1371 sinc = s->nb_all_channels; | 1371 sinc = s->nb_all_channels; |
1372 sptr = samples + (sinc * (N/2) * i) + ch; | 1372 sptr = samples + (sinc * (N/2) * i) + ch; |
1373 for(j=0;j<N/2;j++) { | 1373 for(j=0;j<N/2;j++) { |
1374 v = *sptr; | 1374 v = *sptr; |
1375 input_samples[j + N/2] = v; | 1375 input_samples[j + N/2] = v; |
1430 exp[i][ch], s->nb_coefs[ch], | 1430 exp[i][ch], s->nb_coefs[ch], |
1431 exp_strategy[i][ch]); | 1431 exp_strategy[i][ch]); |
1432 /* copy encoded exponents for reuse case */ | 1432 /* copy encoded exponents for reuse case */ |
1433 for(k=i+1;k<j;k++) { | 1433 for(k=i+1;k<j;k++) { |
1434 memcpy(encoded_exp[k][ch], encoded_exp[i][ch], | 1434 memcpy(encoded_exp[k][ch], encoded_exp[i][ch], |
1435 s->nb_coefs[ch] * sizeof(UINT8)); | 1435 s->nb_coefs[ch] * sizeof(uint8_t)); |
1436 } | 1436 } |
1437 i = j; | 1437 i = j; |
1438 } | 1438 } |
1439 } | 1439 } |
1440 | 1440 |
1490 } | 1490 } |
1491 } | 1491 } |
1492 | 1492 |
1493 void mdct_test(void) | 1493 void mdct_test(void) |
1494 { | 1494 { |
1495 INT16 input[N]; | 1495 int16_t input[N]; |
1496 INT32 output[N/2]; | 1496 int32_t output[N/2]; |
1497 float input1[N]; | 1497 float input1[N]; |
1498 float output1[N/2]; | 1498 float output1[N/2]; |
1499 float s, a, err, e, emax; | 1499 float s, a, err, e, emax; |
1500 int i, k, n; | 1500 int i, k, n; |
1501 | 1501 |