Revision 70fcd7ea libavcodec/ac3enc.c
libavcodec/ac3enc.c  

916  916 
* Run the bit allocation with a given SNR offset. 
917  917 
* This calculates the bit allocation pointers that will be used to determine 
918  918 
* the quantization of each mantissa. 
919 
* @return the number of remaining bits (positive or negative) if the given


920 
* SNR offset is used to quantize the mantissas.


919 
* @return the number of bits needed for mantissas if the given SNR offset is


920 
* is used.


921  921 
*/ 
922  922 
static int bit_alloc(AC3EncodeContext *s, 
923  923 
int16_t mask[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_CRITICAL_BANDS], 
924  924 
int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
925  925 
uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
926 
int frame_bits, int coarse_snr_offset, int fine_snr_offset)


926 
int coarse_snr_offset, int fine_snr_offset) 

927  927 
{ 
928  928 
int blk, ch; 
929 
int snr_offset; 

929 
int snr_offset, mantissa_bits;


930  930  
931  931 
snr_offset = (((coarse_snr_offset  15) << 4) + fine_snr_offset) << 2; 
932  932  
933 
mantissa_bits = 0; 

933  934 
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { 
934  935 
s>mant1_cnt = 0; 
935  936 
s>mant2_cnt = 0; 
...  ...  
939  940 
s>nb_coefs[ch], snr_offset, 
940  941 
s>bit_alloc.floor, ff_ac3_bap_tab, 
941  942 
bap[blk][ch]); 
942 
frame_bits += compute_mantissa_size(s, bap[blk][ch], s>nb_coefs[ch]);


943 
mantissa_bits += compute_mantissa_size(s, bap[blk][ch], s>nb_coefs[ch]);


943  944 
} 
944  945 
} 
945 
return 8 * s>frame_size  frame_bits;


946 
return mantissa_bits;


946  947 
} 
947  948  
948  949  
...  ...  
960  961 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS]) 
961  962 
{ 
962  963 
int ch; 
963 
int frame_bits;


964 
int bits_left;


964  965 
int coarse_snr_offset, fine_snr_offset; 
965  966 
uint8_t bap1[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 
966  967 
int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 
...  ...  
975  976 
/* now the big work begins : do the bit allocation. Modify the snr 
976  977 
offset until we can pack everything in the requested frame size */ 
977  978  
978 
frame_bits = s>frame_bits + s>exponent_bits;


979 
bits_left = 8 * s>frame_size  (s>frame_bits + s>exponent_bits);


979  980 
coarse_snr_offset = s>coarse_snr_offset; 
980  981 
while (coarse_snr_offset >= 0 && 
981 
bit_alloc(s, mask, psd, bap, frame_bits, coarse_snr_offset, 0) < 0)


982 
bit_alloc(s, mask, psd, bap, coarse_snr_offset, 0) > bits_left)


982  983 
coarse_snr_offset = SNR_INC1; 
983  984 
if (coarse_snr_offset < 0) { 
984  985 
return AVERROR(EINVAL); 
985  986 
} 
986  987 
while (coarse_snr_offset + SNR_INC1 <= 63 && 
987 
bit_alloc(s, mask, psd, bap1, frame_bits,


988 
coarse_snr_offset + SNR_INC1, 0) >= 0) {


988 
bit_alloc(s, mask, psd, bap1, 

989 
coarse_snr_offset + SNR_INC1, 0) <= bits_left) {


989  990 
coarse_snr_offset += SNR_INC1; 
990  991 
memcpy(bap, bap1, sizeof(bap1)); 
991  992 
} 
992  993 
while (coarse_snr_offset + 1 <= 63 && 
993 
bit_alloc(s, mask, psd, bap1, frame_bits, coarse_snr_offset + 1, 0) >= 0) {


994 
bit_alloc(s, mask, psd, bap1, coarse_snr_offset + 1, 0) <= bits_left) {


994  995 
coarse_snr_offset++; 
995  996 
memcpy(bap, bap1, sizeof(bap1)); 
996  997 
} 
997  998  
998  999 
fine_snr_offset = 0; 
999  1000 
while (fine_snr_offset + SNR_INC1 <= 15 && 
1000 
bit_alloc(s, mask, psd, bap1, frame_bits,


1001 
coarse_snr_offset, fine_snr_offset + SNR_INC1) >= 0) {


1001 
bit_alloc(s, mask, psd, bap1, 

1002 
coarse_snr_offset, fine_snr_offset + SNR_INC1) <= bits_left) {


1002  1003 
fine_snr_offset += SNR_INC1; 
1003  1004 
memcpy(bap, bap1, sizeof(bap1)); 
1004  1005 
} 
1005  1006 
while (fine_snr_offset + 1 <= 15 && 
1006 
bit_alloc(s, mask, psd, bap1, frame_bits,


1007 
coarse_snr_offset, fine_snr_offset + 1) >= 0) {


1007 
bit_alloc(s, mask, psd, bap1, 

1008 
coarse_snr_offset, fine_snr_offset + 1) <= bits_left) {


1008  1009 
fine_snr_offset++; 
1009  1010 
memcpy(bap, bap1, sizeof(bap1)); 
1010  1011 
} 
Also available in: Unified diff