Revision 266d24be libavcodec/ac3enc.c
libavcodec/ac3enc.c  

101  101 
uint16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr; ///< mantissa pointers for bap=1,2,4 
102  102  
103  103 
int16_t last_samples[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE]; ///< last 256 samples from previous frame 
104 
int16_t planar_samples[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE+AC3_FRAME_SIZE]; 

105 
int16_t windowed_samples[AC3_WINDOW_SIZE]; 

106 
int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 

107 
uint8_t exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 

108 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS]; 

109 
uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 

110 
uint8_t num_exp_groups[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS]; 

111 
uint8_t grouped_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_EXP_GROUPS]; 

112 
int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 

113 
int16_t band_psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_CRITICAL_BANDS]; 

114 
int16_t mask[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_CRITICAL_BANDS]; 

115 
uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 

116 
uint8_t bap1[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 

117 
int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS]; 

118 
uint16_t qmant[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 

104  119 
} AC3EncodeContext; 
105  120  
106  121  
...  ...  
132  147 
* Channels are reordered from FFmpeg's default order to AC3 order. 
133  148 
*/ 
134  149 
static void deinterleave_input_samples(AC3EncodeContext *s, 
135 
const int16_t *samples, 

136 
int16_t planar_samples[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE+AC3_FRAME_SIZE]) 

150 
const int16_t *samples) 

137  151 
{ 
138  152 
int ch, i; 
139  153  
...  ...  
143  157 
int sinc; 
144  158  
145  159 
/* copy last 256 samples of previous frame to the start of the current frame */ 
146 
memcpy(&planar_samples[ch][0], s>last_samples[ch], 

147 
AC3_BLOCK_SIZE * sizeof(planar_samples[0][0])); 

160 
memcpy(&s>planar_samples[ch][0], s>last_samples[ch],


161 
AC3_BLOCK_SIZE * sizeof(s>planar_samples[0][0]));


148  162  
149  163 
/* deinterleave */ 
150  164 
sinc = s>channels; 
151  165 
sptr = samples + s>channel_map[ch]; 
152  166 
for (i = AC3_BLOCK_SIZE; i < AC3_FRAME_SIZE+AC3_BLOCK_SIZE; i++) { 
153 
planar_samples[ch][i] = *sptr; 

167 
s>planar_samples[ch][i] = *sptr;


154  168 
sptr += sinc; 
155  169 
} 
156  170  
157  171 
/* save last 256 samples for next frame */ 
158 
memcpy(s>last_samples[ch], &planar_samples[ch][6* AC3_BLOCK_SIZE], 

159 
AC3_BLOCK_SIZE * sizeof(planar_samples[0][0])); 

172 
memcpy(s>last_samples[ch], &s>planar_samples[ch][6* AC3_BLOCK_SIZE],


173 
AC3_BLOCK_SIZE * sizeof(s>planar_samples[0][0]));


160  174 
} 
161  175 
} 
162  176  
...  ...  
396  410 
* 
397  411 
* @return exponent shift 
398  412 
*/ 
399 
static int normalize_samples(AC3EncodeContext *s, 

400 
int16_t windowed_samples[AC3_WINDOW_SIZE]) 

413 
static int normalize_samples(AC3EncodeContext *s) 

401  414 
{ 
402 
int v = 14  log2_tab(windowed_samples, AC3_WINDOW_SIZE); 

415 
int v = 14  log2_tab(s>windowed_samples, AC3_WINDOW_SIZE);


403  416 
v = FFMAX(0, v); 
404 
lshift_tab(windowed_samples, AC3_WINDOW_SIZE, v); 

417 
lshift_tab(s>windowed_samples, AC3_WINDOW_SIZE, v);


405  418 
return v  9; 
406  419 
} 
407  420  
...  ...  
411  424 
* This applies the KBD window and normalizes the input to reduce precision 
412  425 
* loss due to fixedpoint calculations. 
413  426 
*/ 
414 
static void apply_mdct(AC3EncodeContext *s, 

415 
int16_t planar_samples[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE+AC3_FRAME_SIZE], 

416 
int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 

417 
int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]) 

427 
static void apply_mdct(AC3EncodeContext *s) 

418  428 
{ 
419  429 
int blk, ch; 
420 
int16_t windowed_samples[AC3_WINDOW_SIZE]; 

421  430  
422  431 
for (ch = 0; ch < s>channels; ch++) { 
423  432 
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { 
424 
const int16_t *input_samples = &planar_samples[ch][blk * AC3_BLOCK_SIZE]; 

433 
const int16_t *input_samples = &s>planar_samples[ch][blk * AC3_BLOCK_SIZE];


425  434  
426 
apply_window(windowed_samples, input_samples, ff_ac3_window, AC3_WINDOW_SIZE); 

435 
apply_window(s>windowed_samples, input_samples, ff_ac3_window, AC3_WINDOW_SIZE);


427  436  
428 
exp_shift[blk][ch] = normalize_samples(s, windowed_samples);


437 
s>exp_shift[blk][ch] = normalize_samples(s);


429  438  
430 
mdct512(mdct_coef[blk][ch], windowed_samples);


439 
mdct512(s>mdct_coef[blk][ch], s>windowed_samples);


431  440 
} 
432  441 
} 
433  442 
} 
...  ...  
438  447 
* This takes into account the normalization that was done to the input samples 
439  448 
* by adjusting the exponents by the exponent shift values. 
440  449 
*/ 
441 
static void extract_exponents(AC3EncodeContext *s, 

442 
int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 

443 
int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 

444 
uint8_t exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]) 

450 
static void extract_exponents(AC3EncodeContext *s) 

445  451 
{ 
446  452 
int blk, ch, i; 
447  453  
...  ...  
451  457 
/* compute "exponents". We take into account the normalization there */ 
452  458 
for (i = 0; i < AC3_MAX_COEFS; i++) { 
453  459 
int e; 
454 
int v = abs(mdct_coef[blk][ch][i]); 

460 
int v = abs(s>mdct_coef[blk][ch][i]);


455  461 
if (v == 0) 
456  462 
e = 24; 
457  463 
else { 
458 
e = 23  av_log2(v) + exp_shift[blk][ch]; 

464 
e = 23  av_log2(v) + s>exp_shift[blk][ch];


459  465 
if (e >= 24) { 
460  466 
e = 24; 
461 
mdct_coef[blk][ch][i] = 0; 

467 
s>mdct_coef[blk][ch][i] = 0;


462  468 
} 
463  469 
} 
464 
exp[blk][ch][i] = e; 

470 
s>exp[blk][ch][i] = e;


465  471 
} 
466  472 
} 
467  473 
} 
...  ...  
529  535 
* Calculate exponent strategies for all channels. 
530  536 
* Array arrangement is reversed to simplify the perchannel calculation. 
531  537 
*/ 
532 
static void compute_exp_strategy(AC3EncodeContext *s, 

533 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 

534 
uint8_t exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]) 

538 
static void compute_exp_strategy(AC3EncodeContext *s) 

535  539 
{ 
536  540 
uint8_t *exp1[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; 
537  541 
uint8_t exp_str1[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; 
...  ...  
539  543  
540  544 
for (ch = 0; ch < s>fbw_channels; ch++) { 
541  545 
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { 
542 
exp1[ch][blk] = exp[blk][ch]; 

543 
exp_str1[ch][blk] = exp_strategy[blk][ch]; 

546 
exp1[ch][blk] = s>exp[blk][ch];


547 
exp_str1[ch][blk] = s>exp_strategy[blk][ch];


544  548 
} 
545  549  
546  550 
compute_exp_strategy_ch(exp_str1[ch], exp1[ch]); 
547  551  
548  552 
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) 
549 
exp_strategy[blk][ch] = exp_str1[ch][blk]; 

553 
s>exp_strategy[blk][ch] = exp_str1[ch][blk];


550  554 
} 
551  555 
if (s>lfe_on) { 
552  556 
ch = s>lfe_channel; 
553 
exp_strategy[0][ch] = EXP_D15; 

557 
s>exp_strategy[0][ch] = EXP_D15;


554  558 
for (blk = 1; blk < 5; blk++) 
555 
exp_strategy[blk][ch] = EXP_REUSE; 

559 
s>exp_strategy[blk][ch] = EXP_REUSE;


556  560 
} 
557  561 
} 
558  562  
...  ...  
629  633 
* deltas between adjacent exponent groups so that they can be differentially 
630  634 
* encoded. 
631  635 
*/ 
632 
static void encode_exponents(AC3EncodeContext *s, 

633 
uint8_t exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 

634 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 

635 
uint8_t num_exp_groups[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 

636 
uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]) 

636 
static void encode_exponents(AC3EncodeContext *s) 

637  637 
{ 
638  638 
int blk, blk1, blk2, ch; 
639  639  
...  ...  
642  642 
blk = 0; 
643  643 
while (blk < AC3_MAX_BLOCKS) { 
644  644 
blk1 = blk + 1; 
645 
while (blk1 < AC3_MAX_BLOCKS && exp_strategy[blk1][ch] == EXP_REUSE) { 

646 
exponent_min(exp[blk][ch], exp[blk1][ch], s>nb_coefs[ch]);


645 
while (blk1 < AC3_MAX_BLOCKS && s>exp_strategy[blk1][ch] == EXP_REUSE) {


646 
exponent_min(s>exp[blk][ch], s>exp[blk1][ch], s>nb_coefs[ch]);


647  647 
blk1++; 
648  648 
} 
649 
encode_exponents_blk_ch(encoded_exp[blk][ch], 

650 
exp[blk][ch], s>nb_coefs[ch], 

651 
exp_strategy[blk][ch], 

652 
&num_exp_groups[blk][ch]); 

649 
encode_exponents_blk_ch(s>encoded_exp[blk][ch],


650 
s>exp[blk][ch], s>nb_coefs[ch],


651 
s>exp_strategy[blk][ch],


652 
&s>num_exp_groups[blk][ch]);


653  653 
/* copy encoded exponents for reuse case */ 
654  654 
for (blk2 = blk+1; blk2 < blk1; blk2++) { 
655 
memcpy(encoded_exp[blk2][ch], encoded_exp[blk][ch],


655 
memcpy(s>encoded_exp[blk2][ch], s>encoded_exp[blk][ch],


656  656 
s>nb_coefs[ch] * sizeof(uint8_t)); 
657  657 
} 
658  658 
blk = blk1; 
...  ...  
666  666 
* 3 deltaencoded exponents are in each 7bit group. The number of groups 
667  667 
* varies depending on exponent strategy and bandwidth. 
668  668 
*/ 
669 
static void group_exponents(AC3EncodeContext *s, 

670 
uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 

671 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 

672 
uint8_t num_exp_groups[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 

673 
uint8_t grouped_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_EXP_GROUPS]) 

669 
static void group_exponents(AC3EncodeContext *s) 

674  670 
{ 
675  671 
int blk, ch, i; 
676  672 
int group_size, bit_count; 
...  ...  
681  677 
bit_count = 0; 
682  678 
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { 
683  679 
for (ch = 0; ch < s>channels; ch++) { 
684 
if (exp_strategy[blk][ch] == EXP_REUSE) { 

685 
num_exp_groups[blk][ch] = 0; 

680 
if (s>exp_strategy[blk][ch] == EXP_REUSE) {


681 
s>num_exp_groups[blk][ch] = 0;


686  682 
continue; 
687  683 
} 
688 
group_size = exp_strategy[blk][ch] + (exp_strategy[blk][ch] == EXP_D45);


689 
bit_count += 4 + (num_exp_groups[blk][ch] * 7); 

690 
p = encoded_exp[blk][ch]; 

684 
group_size = s>exp_strategy[blk][ch] + (s>exp_strategy[blk][ch] == EXP_D45);


685 
bit_count += 4 + (s>num_exp_groups[blk][ch] * 7);


686 
p = s>encoded_exp[blk][ch];


691  687  
692  688 
/* DC exponent */ 
693  689 
exp1 = *p++; 
694 
grouped_exp[blk][ch][0] = exp1; 

690 
s>grouped_exp[blk][ch][0] = exp1;


695  691  
696  692 
/* remaining exponents are delta encoded */ 
697 
for (i = 1; i <= num_exp_groups[blk][ch]; i++) { 

693 
for (i = 1; i <= s>num_exp_groups[blk][ch]; i++) {


698  694 
/* merge three delta in one code */ 
699  695 
exp0 = exp1; 
700  696 
exp1 = p[0]; 
...  ...  
711  707 
p += group_size; 
712  708 
delta2 = exp1  exp0 + 2; 
713  709  
714 
grouped_exp[blk][ch][i] = ((delta0 * 5 + delta1) * 5) + delta2; 

710 
s>grouped_exp[blk][ch][i] = ((delta0 * 5 + delta1) * 5) + delta2;


715  711 
} 
716  712 
} 
717  713 
} 
...  ...  
725  721 
* Extract exponents from MDCT coefficients, calculate exponent strategies, 
726  722 
* and encode final exponents. 
727  723 
*/ 
728 
static void process_exponents(AC3EncodeContext *s, 

729 
int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 

730 
int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 

731 
uint8_t exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 

732 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 

733 
uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 

734 
uint8_t num_exp_groups[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 

735 
uint8_t grouped_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_EXP_GROUPS]) 

724 
static void process_exponents(AC3EncodeContext *s) 

736  725 
{ 
737 
extract_exponents(s, mdct_coef, exp_shift, exp);


726 
extract_exponents(s); 

738  727  
739 
compute_exp_strategy(s, exp_strategy, exp);


728 
compute_exp_strategy(s); 

740  729  
741 
encode_exponents(s, exp, exp_strategy, num_exp_groups, encoded_exp);


730 
encode_exponents(s); 

742  731  
743 
group_exponents(s, encoded_exp, exp_strategy, num_exp_groups, grouped_exp);


732 
group_exponents(s); 

744  733 
} 
745  734  
746  735  
...  ...  
778  767 
/** 
779  768 
* Count the bits used to encode the frame, minus exponents and mantissas. 
780  769 
*/ 
781 
static void count_frame_bits(AC3EncodeContext *s, 

782 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS]) 

770 
static void count_frame_bits(AC3EncodeContext *s) 

783  771 
{ 
784  772 
static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 }; 
785  773 
int blk, ch; 
...  ...  
801  789 
if (s>lfe_on) 
802  790 
frame_bits++; /* lfeexpstr */ 
803  791 
for (ch = 0; ch < s>fbw_channels; ch++) { 
804 
if (exp_strategy[blk][ch] != EXP_REUSE) 

792 
if (s>exp_strategy[blk][ch] != EXP_REUSE)


805  793 
frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */ 
806  794 
} 
807  795 
frame_bits++; /* baie */ 
...  ...  
882  870 
* Calculate masking curve based on the final exponents. 
883  871 
* Also calculate the power spectral densities to use in future calculations. 
884  872 
*/ 
885 
static void bit_alloc_masking(AC3EncodeContext *s, 

886 
uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 

887 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 

888 
int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 

889 
int16_t mask[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_CRITICAL_BANDS]) 

873 
static void bit_alloc_masking(AC3EncodeContext *s) 

890  874 
{ 
891  875 
int blk, ch; 
892 
int16_t band_psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_CRITICAL_BANDS]; 

893  876  
894  877 
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { 
895  878 
for (ch = 0; ch < s>channels; ch++) { 
896 
if(exp_strategy[blk][ch] == EXP_REUSE) {


897 
memcpy(psd[blk][ch], psd[blk1][ch], AC3_MAX_COEFS*sizeof(psd[0][0][0]));


898 
memcpy(mask[blk][ch], mask[blk1][ch], AC3_CRITICAL_BANDS*sizeof(mask[0][0][0]));


879 
if (s>exp_strategy[blk][ch] == EXP_REUSE) {


880 
memcpy(s>psd[blk][ch], s>psd[blk1][ch], AC3_MAX_COEFS*sizeof(s>psd[0][0][0]));


881 
memcpy(s>mask[blk][ch], s>mask[blk1][ch], AC3_CRITICAL_BANDS*sizeof(s>mask[0][0][0]));


899  882 
} else { 
900 
ff_ac3_bit_alloc_calc_psd(encoded_exp[blk][ch], 0, 

883 
ff_ac3_bit_alloc_calc_psd(s>encoded_exp[blk][ch], 0,


901  884 
s>nb_coefs[ch], 
902 
psd[blk][ch], band_psd[blk][ch]);


903 
ff_ac3_bit_alloc_calc_mask(&s>bit_alloc, band_psd[blk][ch], 

885 
s>psd[blk][ch], s>band_psd[blk][ch]);


886 
ff_ac3_bit_alloc_calc_mask(&s>bit_alloc, s>band_psd[blk][ch],


904  887 
0, s>nb_coefs[ch], 
905  888 
ff_ac3_fast_gain_tab[s>fast_gain_code[ch]], 
906  889 
ch == s>lfe_channel, 
907  890 
DBA_NONE, 0, NULL, NULL, NULL, 
908 
mask[blk][ch]); 

891 
s>mask[blk][ch]);


909  892 
} 
910  893 
} 
911  894 
} 
...  ...  
920  903 
* is used. 
921  904 
*/ 
922  905 
static int bit_alloc(AC3EncodeContext *s, 
923 
int16_t mask[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_CRITICAL_BANDS], 

924 
int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 

925  906 
uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
926  907 
int snr_offset) 
927  908 
{ 
...  ...  
936  917 
s>mant2_cnt = 0; 
937  918 
s>mant4_cnt = 0; 
938  919 
for (ch = 0; ch < s>channels; ch++) { 
939 
ff_ac3_bit_alloc_calc_bap(mask[blk][ch], psd[blk][ch], 0,


920 
ff_ac3_bit_alloc_calc_bap(s>mask[blk][ch], s>psd[blk][ch], 0,


940  921 
s>nb_coefs[ch], snr_offset, 
941  922 
s>bit_alloc.floor, ff_ac3_bap_tab, 
942  923 
bap[blk][ch]); 
...  ...  
953  934 
* frame size. Output is the SNR offset and a set of bit allocation pointers 
954  935 
* used to quantize the mantissas. 
955  936 
*/ 
956 
static int compute_bit_allocation(AC3EncodeContext *s, 

957 
uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 

958 
uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 

959 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS]) 

937 
static int compute_bit_allocation(AC3EncodeContext *s) 

960  938 
{ 
961  939 
int ch; 
962  940 
int bits_left; 
963  941 
int snr_offset; 
964 
uint8_t bap1[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 

965 
int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 

966 
int16_t mask[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_CRITICAL_BANDS]; 

967  942  
968  943 
/* count frame bits other than exponents and mantissas */ 
969 
count_frame_bits(s, exp_strategy);


944 
count_frame_bits(s); 

970  945  
971  946 
/* calculate psd and masking curve before doing bit allocation */ 
972 
bit_alloc_masking(s, encoded_exp, exp_strategy, psd, mask);


947 
bit_alloc_masking(s); 

973  948  
974  949 
/* now the big work begins : do the bit allocation. Modify the snr 
975  950 
offset until we can pack everything in the requested frame size */ 
...  ...  
979  954 
snr_offset = s>coarse_snr_offset << 4; 
980  955  
981  956 
while (snr_offset >= 0 && 
982 
bit_alloc(s, mask, psd, bap, snr_offset) > bits_left) {


957 
bit_alloc(s, s>bap, snr_offset) > bits_left) {


983  958 
snr_offset = 64; 
984  959 
} 
985  960 
if (snr_offset < 0) { 
...  ...  
987  962 
} 
988  963  
989  964 
while (snr_offset + 64 <= 1023 && 
990 
bit_alloc(s, mask, psd, bap1, snr_offset + 64) <= bits_left) {


965 
bit_alloc(s, s>bap1, snr_offset + 64) <= bits_left) {


991  966 
snr_offset += 64; 
992 
memcpy(bap, bap1, sizeof(bap1));


967 
memcpy(s>bap, s>bap1, sizeof(s>bap1));


993  968 
} 
994  969 
while (snr_offset + 16 <= 1023 && 
995 
bit_alloc(s, mask, psd, bap1, snr_offset + 16) <= bits_left) {


970 
bit_alloc(s, s>bap1, snr_offset + 16) <= bits_left) {


996  971 
snr_offset += 16; 
997 
memcpy(bap, bap1, sizeof(bap1));


972 
memcpy(s>bap, s>bap1, sizeof(s>bap1));


998  973 
} 
999  974 
while (snr_offset + 4 <= 1023 && 
1000 
bit_alloc(s, mask, psd, bap1, snr_offset + 4) <= bits_left) {


975 
bit_alloc(s, s>bap1, snr_offset + 4) <= bits_left) {


1001  976 
snr_offset += 4; 
1002 
memcpy(bap, bap1, sizeof(bap1));


977 
memcpy(s>bap, s>bap1, sizeof(s>bap1));


1003  978 
} 
1004  979 
while (snr_offset + 1 <= 1023 && 
1005 
bit_alloc(s, mask, psd, bap1, snr_offset + 1) <= bits_left) {


980 
bit_alloc(s, s>bap1, snr_offset + 1) <= bits_left) {


1006  981 
snr_offset++; 
1007 
memcpy(bap, bap1, sizeof(bap1));


982 
memcpy(s>bap, s>bap1, sizeof(s>bap1));


1008  983 
} 
1009  984  
1010  985 
s>coarse_snr_offset = snr_offset >> 4; 
...  ...  
1156  1131 
/** 
1157  1132 
* Quantize mantissas using coefficients, exponents, and bit allocation pointers. 
1158  1133 
*/ 
1159 
static void quantize_mantissas(AC3EncodeContext *s, 

1160 
int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 

1161 
int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 

1162 
uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 

1163 
uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 

1164 
uint16_t qmant[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]) 

1134 
static void quantize_mantissas(AC3EncodeContext *s) 

1165  1135 
{ 
1166  1136 
int blk, ch; 
1167  1137  
...  ...  
1171  1141 
s>qmant1_ptr = s>qmant2_ptr = s>qmant4_ptr = NULL; 
1172  1142  
1173  1143 
for (ch = 0; ch < s>channels; ch++) { 
1174 
quantize_mantissas_blk_ch(s, mdct_coef[blk][ch], exp_shift[blk][ch],


1175 
encoded_exp[blk][ch], bap[blk][ch],


1176 
qmant[blk][ch], s>nb_coefs[ch]); 

1144 
quantize_mantissas_blk_ch(s, s>mdct_coef[blk][ch], s>exp_shift[blk][ch],


1145 
s>encoded_exp[blk][ch], s>bap[blk][ch],


1146 
s>qmant[blk][ch], s>nb_coefs[ch]);


1177  1147 
} 
1178  1148 
} 
1179  1149 
} 
...  ...  
1214  1184 
* Write one audio block to the output bitstream. 
1215  1185 
*/ 
1216  1186 
static void output_audio_block(AC3EncodeContext *s, 
1217 
uint8_t exp_strategy[AC3_MAX_CHANNELS], 

1218 
uint8_t num_exp_groups[AC3_MAX_CHANNELS], 

1219 
uint8_t grouped_exp[AC3_MAX_CHANNELS][AC3_MAX_EXP_GROUPS], 

1220 
uint8_t bap[AC3_MAX_CHANNELS][AC3_MAX_COEFS], 

1221 
uint16_t qmant[AC3_MAX_CHANNELS][AC3_MAX_COEFS], 

1222  1187 
int block_num) 
1223  1188 
{ 
1224  1189 
int ch, i, baie, rbnd; 
...  ...  
1251  1216  
1252  1217 
/* exponent strategy */ 
1253  1218 
for (ch = 0; ch < s>fbw_channels; ch++) 
1254 
put_bits(&s>pb, 2, exp_strategy[ch]);


1219 
put_bits(&s>pb, 2, s>exp_strategy[block_num][ch]);


1255  1220  
1256  1221 
if (s>lfe_on) 
1257 
put_bits(&s>pb, 1, exp_strategy[s>lfe_channel]);


1222 
put_bits(&s>pb, 1, s>exp_strategy[block_num][s>lfe_channel]);


1258  1223  
1259  1224 
/* bandwidth */ 
1260  1225 
for (ch = 0; ch < s>fbw_channels; ch++) { 
1261 
if (exp_strategy[ch] != EXP_REUSE)


1226 
if (s>exp_strategy[block_num][ch] != EXP_REUSE)


1262  1227 
put_bits(&s>pb, 6, s>bandwidth_code[ch]); 
1263  1228 
} 
1264  1229  
1265  1230 
/* exponents */ 
1266  1231 
for (ch = 0; ch < s>channels; ch++) { 
1267 
if (exp_strategy[ch] == EXP_REUSE)


1232 
if (s>exp_strategy[block_num][ch] == EXP_REUSE)


1268  1233 
continue; 
1269  1234  
1270  1235 
/* first exponent */ 
1271 
put_bits(&s>pb, 4, grouped_exp[ch][0]);


1236 
put_bits(&s>pb, 4, s>grouped_exp[block_num][ch][0]);


1272  1237  
1273  1238 
/* next ones are deltaencoded and grouped */ 
1274 
for (i = 1; i <= num_exp_groups[ch]; i++)


1275 
put_bits(&s>pb, 7, grouped_exp[ch][i]);


1239 
for (i = 1; i <= s>num_exp_groups[block_num][ch]; i++)


1240 
put_bits(&s>pb, 7, s>grouped_exp[block_num][ch][i]);


1276  1241  
1277  1242 
if (ch != s>lfe_channel) 
1278  1243 
put_bits(&s>pb, 2, 0); /* no gain range info */ 
...  ...  
1307  1272 
int b, q; 
1308  1273  
1309  1274 
for (i = 0; i < s>nb_coefs[ch]; i++) { 
1310 
q = qmant[ch][i];


1311 
b = bap[ch][i];


1275 
q = s>qmant[block_num][ch][i];


1276 
b = s>bap[block_num][ch][i];


1312  1277 
switch (b) { 
1313  1278 
case 0: break; 
1314  1279 
case 1: if (q != 128) put_bits(&s>pb, 5, q); break; 
...  ...  
1400  1365 
* Write the frame to the output bitstream. 
1401  1366 
*/ 
1402  1367 
static void output_frame(AC3EncodeContext *s, 
1403 
unsigned char *frame, 

1404 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 

1405 
uint8_t num_exp_groups[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 

1406 
uint8_t grouped_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_EXP_GROUPS], 

1407 
uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 

1408 
uint16_t qmant[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]) 

1368 
unsigned char *frame) 

1409  1369 
{ 
1410  1370 
int blk; 
1411  1371  
...  ...  
1414  1374 
output_frame_header(s); 
1415  1375  
1416  1376 
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { 
1417 
output_audio_block(s, exp_strategy[blk], num_exp_groups[blk], 

1418 
grouped_exp[blk], bap[blk], qmant[blk], blk); 

1377 
output_audio_block(s, blk); 

1419  1378 
} 
1420  1379  
1421  1380 
output_frame_end(s); 
...  ...  
1430  1389 
{ 
1431  1390 
AC3EncodeContext *s = avctx>priv_data; 
1432  1391 
const int16_t *samples = data; 
1433 
int16_t planar_samples[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE+AC3_FRAME_SIZE]; 

1434 
int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 

1435 
uint8_t exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 

1436 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS]; 

1437 
uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 

1438 
uint8_t num_exp_groups[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS]; 

1439 
uint8_t grouped_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_EXP_GROUPS]; 

1440 
uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 

1441 
int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS]; 

1442 
uint16_t qmant[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 

1443  1392 
int ret; 
1444  1393  
1445  1394 
if (s>bit_alloc.sr_code == 1) 
1446  1395 
adjust_frame_size(s); 
1447  1396  
1448 
deinterleave_input_samples(s, samples, planar_samples);


1397 
deinterleave_input_samples(s, samples); 

1449  1398  
1450 
apply_mdct(s, planar_samples, exp_shift, mdct_coef);


1399 
apply_mdct(s); 

1451  1400  
1452 
process_exponents(s, mdct_coef, exp_shift, exp, exp_strategy, encoded_exp, 

1453 
num_exp_groups, grouped_exp); 

1401 
process_exponents(s); 

1454  1402  
1455 
ret = compute_bit_allocation(s, bap, encoded_exp, exp_strategy);


1403 
ret = compute_bit_allocation(s); 

1456  1404 
if (ret) { 
1457  1405 
av_log(avctx, AV_LOG_ERROR, "Bit allocation failed. Try increasing the bitrate.\n"); 
1458  1406 
return ret; 
1459  1407 
} 
1460  1408  
1461 
quantize_mantissas(s, mdct_coef, exp_shift, encoded_exp, bap, qmant);


1409 
quantize_mantissas(s); 

1462  1410  
1463 
output_frame(s, frame, exp_strategy, num_exp_groups, grouped_exp, bap, qmant);


1411 
output_frame(s, frame); 

1464  1412  
1465  1413 
return s>frame_size; 
1466  1414 
} 
Also available in: Unified diff