#include "libavutil/crc.h"#include "avcodec.h"#include "bitstream.h"#include "ac3.h"Go to the source code of this file.
Data Structures | |
| struct | AC3EncodeContext |
| struct | IComplex |
Defines | |
| #define | MDCT_NBITS 9 |
| #define | N (1 << MDCT_NBITS) |
| #define | EXP_DIFF_THRESHOLD 1000 |
| #define | BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) |
| #define | MUL16(a, b) ((a) * (b)) |
| #define | CMUL(pre, pim, are, aim, bre, bim) |
| #define | SNR_INC1 4 |
| #define | CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16)) |
Functions | |
| static int16_t | fix15 (float a) |
| static void | fft_init (int ln) |
| static void | fft (IComplex *z, int ln) |
| static void | mdct512 (int32_t *out, int16_t *in) |
| static int | calc_exp_diff (uint8_t *exp1, uint8_t *exp2, int n) |
| static void | compute_exp_strategy (uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], uint8_t exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], int ch, int is_lfe) |
| static void | exponent_min (uint8_t exp[N/2], uint8_t exp1[N/2], int n) |
| static int | encode_exp (uint8_t encoded_exp[N/2], uint8_t exp[N/2], int nb_exps, int exp_strategy) |
| static int | compute_mantissa_size (AC3EncodeContext *s, uint8_t *m, int nb_coefs) |
| static void | bit_alloc_masking (AC3EncodeContext *s, uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], int16_t psd[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], int16_t mask[NB_BLOCKS][AC3_MAX_CHANNELS][50]) |
| static int | bit_alloc (AC3EncodeContext *s, int16_t mask[NB_BLOCKS][AC3_MAX_CHANNELS][50], int16_t psd[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], int frame_bits, int coarse_snr_offset, int fine_snr_offset) |
| static int | compute_bit_allocation (AC3EncodeContext *s, uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], int frame_bits) |
| static av_cold int | AC3_encode_init (AVCodecContext *avctx) |
| static void | output_frame_header (AC3EncodeContext *s, unsigned char *frame) |
| static int | sym_quant (int c, int e, int levels) |
| static int | asym_quant (int c, int e, int qbits) |
| static void | output_audio_block (AC3EncodeContext *s, uint8_t exp_strategy[AC3_MAX_CHANNELS], uint8_t encoded_exp[AC3_MAX_CHANNELS][N/2], uint8_t bap[AC3_MAX_CHANNELS][N/2], int32_t mdct_coefs[AC3_MAX_CHANNELS][N/2], int8_t global_exp[AC3_MAX_CHANNELS], int block_num) |
| static unsigned int | mul_poly (unsigned int a, unsigned int b, unsigned int poly) |
| static unsigned int | pow_poly (unsigned int a, unsigned int n, unsigned int poly) |
| static int | log2_tab (int16_t *tab, int n) |
| static void | lshift_tab (int16_t *tab, int n, int lshift) |
| static int | output_frame_end (AC3EncodeContext *s) |
| static int | AC3_encode_frame (AVCodecContext *avctx, unsigned char *frame, int buf_size, void *data) |
| static av_cold int | AC3_encode_close (AVCodecContext *avctx) |
Variables | |
| static int16_t | costab [64] |
| static int16_t | sintab [64] |
| static int16_t | xcos1 [128] |
| static int16_t | xsin1 [128] |
| AVCodec | ac3_encoder |
Definition in file ac3enc.c.
| #define BF | ( | pre, | |||
| pim, | |||||
| qre, | |||||
| qim, | |||||
| pre1, | |||||
| pim1, | |||||
| qre1, | |||||
| qim1 | ) |
Value:
{\
int ax, ay, bx, by;\
bx=pre1;\
by=pim1;\
ax=qre1;\
ay=qim1;\
pre = (bx + ax) >> 1;\
pim = (by + ay) >> 1;\
qre = (bx - ax) >> 1;\
qim = (by - ay) >> 1;\
}
Definition at line 106 of file ac3enc.c.
Referenced by dct32(), ff_fft_calc_c(), ff_simple_idct248_put(), and fft().
| #define CMUL | ( | pre, | |||
| pim, | |||||
| are, | |||||
| aim, | |||||
| bre, | |||||
| bim | ) |
| #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16)) |
| #define EXP_DIFF_THRESHOLD 1000 |
| #define MUL16 | ( | a, | |||
| b | ) | ((a) * (b)) |
Definition at line 119 of file ac3enc.c.
Referenced by AC3_encode_frame(), idctRowCondDC(), idctSparseCol(), idctSparseColAdd(), and idctSparseColPut().
| #define N (1 << MDCT_NBITS) |
Definition at line 71 of file ac3enc.c.
Referenced by AC3_encode_frame(), bit_alloc_masking(), compute_bit_allocation(), compute_exp_strategy(), encode_exp(), mdct512(), and output_audio_block().
| static av_cold int AC3_encode_close | ( | AVCodecContext * | avctx | ) | [static] |
Definition at line 1259 of file ac3enc.c.
References av_freep(), and AVCodecContext::coded_frame.
01260 { 01261 av_freep(&avctx->coded_frame); 01262 return 0; 01263 }
| static int AC3_encode_frame | ( | AVCodecContext * | avctx, | |
| unsigned char * | frame, | |||
| int | buf_size, | |||
| void * | data | |||
| ) | [static] |
Definition at line 1145 of file ac3enc.c.
References AC3_MAX_CHANNELS, av_log2(), AC3EncodeContext::bit_rate, AC3EncodeContext::bits_written, compute_exp_strategy(), encode_exp(), EXP_REUSE, exponent_min(), ff_ac3_window, AC3EncodeContext::last_samples, AC3EncodeContext::lfe_channel, log2_tab(), lshift_tab(), mdct512(), MUL16, N, AC3EncodeContext::nb_all_channels, NB_BLOCKS, AC3EncodeContext::nb_coefs, AVCodecContext::priv_data, AC3EncodeContext::sample_rate, samples, and AC3EncodeContext::samples_written.
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 /* fixed mdct to the six sub blocks & exponent computation */ 01163 for(i=0;i<NB_BLOCKS;i++) { 01164 int16_t *sptr; 01165 int sinc; 01166 01167 /* compute input samples */ 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 /* apply the MDCT window */ 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 /* Normalize the samples to use the maximum available 01187 precision */ 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 /* do the MDCT */ 01195 mdct512(mdct_coef[i][ch], input_samples); 01196 01197 /* compute "exponents". We take into account the 01198 normalization there */ 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 /* compute the exponents as the decoder will see them. The 01218 EXP_REUSE case must be handled carefully : we select the 01219 min of the exponents */ 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 /* copy encoded exponents for reuse case */ 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 /* adjust for fractional frame sizes */ 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 /* everything is known... let's output the frame */ 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 }
| static av_cold int AC3_encode_init | ( | AVCodecContext * | avctx | ) | [static] |
Definition at line 612 of file ac3enc.c.
References ac3_common_init(), AC3_FRAME_SIZE, av_clip(), AC3EncodeContext::bit_rate, AVCodecContext::bit_rate, AC3EncodeContext::bits_written, AC3EncodeContext::bitstream_id, AC3EncodeContext::bitstream_mode, AC3EncodeContext::channel_mode, AVCodecContext::channels, AVCodecContext::cutoff, ff_ac3_bitrate_tab, ff_ac3_frame_size_tab, ff_ac3_sample_rate_tab, AC3EncodeContext::frame_size, AVCodecContext::frame_size, AC3EncodeContext::frame_size_code, AC3EncodeContext::frame_size_min, AC3EncodeContext::lfe, AC3EncodeContext::lfe_channel, AC3EncodeContext::nb_all_channels, AC3EncodeContext::nb_channels, AVCodecContext::priv_data, AC3EncodeContext::sample_rate, AVCodecContext::sample_rate, AC3EncodeContext::samples_written, AC3EncodeContext::sr_code, and AC3EncodeContext::sr_shift.
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, /* C */ 00623 0x02, /* L R */ 00624 0x03, /* L C R */ 00625 0x06, /* L R SL SR */ 00626 0x07, /* L C R SL SR */ 00627 0x07, /* L C R SL SR (+LFE) */ 00628 }; 00629 00630 avctx->frame_size = AC3_FRAME_SIZE; 00631 00632 ac3_common_init(); 00633 00634 /* number of channels */ 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 /* frequency */ 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; /* complete main audio service */ 00656 00657 /* bitrate & frame size */ 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 /* bit allocation init */ 00672 if(avctx->cutoff) { 00673 /* calculate bandwidth based on user-specified cutoff frequency */ 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 /* use default bandwidth setting */ 00679 /* XXX: should compute the bandwidth according to the frame 00680 size, so that we avoid annoying high frequency artifacts */ 00681 bw_code = 50; 00682 } 00683 for(ch=0;ch<s->nb_channels;ch++) { 00684 /* bandwidth for each channel */ 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; /* fixed */ 00690 } 00691 /* initial snr offset */ 00692 s->coarse_snr_offset = 40; 00693 00694 /* mdct init */ 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 }
| static int asym_quant | ( | int | c, | |
| int | e, | |||
| int | qbits | |||
| ) | [inline, static] |
Definition at line 757 of file ac3enc.c.
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 /* rounding */ 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 }
| static int bit_alloc | ( | AC3EncodeContext * | s, | |
| int16_t | mask[NB_BLOCKS][AC3_MAX_CHANNELS][50], | |||
| int16_t | psd[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], | |||
| uint8_t | bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], | |||
| int | frame_bits, | |||
| int | coarse_snr_offset, | |||
| int | fine_snr_offset | |||
| ) | [static] |
Definition at line 447 of file ac3enc.c.
References AC3EncodeContext::bit_alloc, compute_mantissa_size(), ff_ac3_bap_tab, ff_ac3_bit_alloc_calc_bap(), AC3BitAllocParameters::floor, AC3EncodeContext::frame_size, AC3EncodeContext::mant1_cnt, AC3EncodeContext::mant2_cnt, AC3EncodeContext::mant4_cnt, mask, NB_BLOCKS, AC3EncodeContext::nb_coefs, and printf.
Referenced by compute_bit_allocation(), encode_frame(), mp_decode_layer2(), and MPA_encode_frame().
00452 { 00453 int i, ch; 00454 int snr_offset; 00455 00456 snr_offset = (((coarse_snr_offset - 15) << 4) + fine_snr_offset) << 2; 00457 00458 /* compute size */ 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 }
| static void bit_alloc_masking | ( | AC3EncodeContext * | s, | |
| uint8_t | encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], | |||
| uint8_t | exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], | |||
| int16_t | psd[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], | |||
| int16_t | mask[NB_BLOCKS][AC3_MAX_CHANNELS][50] | |||
| ) | [static] |
Definition at line 418 of file ac3enc.c.
References AC3EncodeContext::bit_alloc, blk, DBA_NONE, EXP_REUSE, AC3EncodeContext::fast_gain_code, ff_ac3_bit_alloc_calc_mask(), ff_ac3_bit_alloc_calc_psd(), ff_ac3_fast_gain_tab, AC3EncodeContext::lfe_channel, mask, N, AC3EncodeContext::nb_all_channels, NB_BLOCKS, AC3EncodeContext::nb_coefs, and NULL.
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 }
| static int calc_exp_diff | ( | uint8_t * | exp1, | |
| uint8_t * | exp2, | |||
| int | n | |||
| ) | [static] |
Definition at line 230 of file ac3enc.c.
Referenced by compute_exp_strategy().
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 }
| static int compute_bit_allocation | ( | AC3EncodeContext * | s, | |
| uint8_t | bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], | |||
| uint8_t | encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], | |||
| uint8_t | exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], | |||
| int | frame_bits | |||
| ) | [static] |
Definition at line 482 of file ac3enc.c.
References AC3_CHMODE_STEREO, AC3EncodeContext::bit_alloc, AC3EncodeContext::channel_mode, AC3BitAllocParameters::db_per_bit, AC3EncodeContext::db_per_bit_code, AC3BitAllocParameters::fast_decay, AC3EncodeContext::fast_decay_code, AC3EncodeContext::fast_gain_code, ff_ac3_db_per_bit_tab, ff_ac3_fast_decay_tab, ff_ac3_floor_tab, ff_ac3_slow_decay_tab, ff_ac3_slow_gain_tab, AC3BitAllocParameters::floor, AC3EncodeContext::floor_code, mask, N, AC3EncodeContext::nb_all_channels, NB_BLOCKS, AC3EncodeContext::nb_channels, AC3BitAllocParameters::slow_decay, AC3EncodeContext::slow_decay_code, AC3BitAllocParameters::slow_gain, AC3EncodeContext::slow_gain_code, AC3EncodeContext::sr_code, AC3BitAllocParameters::sr_code, AC3EncodeContext::sr_shift, and AC3BitAllocParameters::sr_shift.
Referenced by MPA_encode_frame().
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 /* init default parameters */ 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 /* compute real values */ 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 /* header size */ 00514 frame_bits += 65; 00515 // if (s->channel_mode == 2) 00516 // frame_bits += 2; 00517 frame_bits += frame_bits_inc[s->channel_mode]; 00518 00519 /* audio blocks */ 00520 for(i=0;i<NB_BLOCKS;i++) { 00521 frame_bits += s->nb_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */ 00522 if (s->channel_mode == AC3_CHMODE_STEREO) { 00523 frame_bits++; /* rematstr */ 00524 if(i==0) frame_bits += 4; 00525 } 00526 frame_bits += 2 * s->nb_channels; /* chexpstr[2] * c */ 00527 if (s->lfe) 00528 frame_bits++; /* lfeexpstr */ 00529 for(ch=0;ch<s->nb_channels;ch++) { 00530 if (exp_strategy[i][ch] != EXP_REUSE) 00531 frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */ 00532 } 00533 frame_bits++; /* baie */ 00534 frame_bits++; /* snr */ 00535 frame_bits += 2; /* delta / skip */ 00536 } 00537 frame_bits++; /* cplinu for block 0 */ 00538 /* bit alloc info */ 00539 /* sdcycod[2], fdcycod[2], sgaincod[2], dbpbcod[2], floorcod[3] */ 00540 /* csnroffset[6] */ 00541 /* (fsnoffset[4] + fgaincod[4]) * c */ 00542 frame_bits += 2*4 + 3 + 6 + s->nb_all_channels * (4 + 3); 00543 00544 /* auxdatae, crcrsv */ 00545 frame_bits += 2; 00546 00547 /* CRC */ 00548 frame_bits += 16; 00549 00550 /* calculate psd and masking curve before doing bit allocation */ 00551 bit_alloc_masking(s, encoded_exp, exp_strategy, psd, mask); 00552 00553 /* now the big work begins : do the bit allocation. Modify the snr 00554 offset until we can pack everything in the requested frame size */ 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 }
| static void compute_exp_strategy | ( | uint8_t | exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], | |
| uint8_t | exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], | |||
| int | ch, | |||
| int | is_lfe | |||
| ) | [static] |
Definition at line 240 of file ac3enc.c.
References av_log(), AV_LOG_DEBUG, calc_exp_diff(), EXP_D15, EXP_D25, EXP_D45, EXP_DIFF_THRESHOLD, EXP_NEW, EXP_REUSE, N, NB_BLOCKS, and NULL.
Referenced by AC3_encode_frame().
00243 { 00244 int i, j; 00245 int exp_diff; 00246 00247 /* estimate if the exponent variation & decide if they should be 00248 reused in the next frame */ 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 /* now select the encoding strategy type : if exponents are often 00264 recoded, we use a coarse encoding */ 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 }
| static int compute_mantissa_size | ( | AC3EncodeContext * | s, | |
| uint8_t * | m, | |||
| int | nb_coefs | |||
| ) | [static] |
Definition at line 368 of file ac3enc.c.
References bits, AC3EncodeContext::mant1_cnt, AC3EncodeContext::mant2_cnt, and AC3EncodeContext::mant4_cnt.
Referenced by bit_alloc().
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 /* nothing */ 00378 break; 00379 case 1: 00380 /* 3 mantissa in 5 bits */ 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 /* 3 mantissa in 7 bits */ 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 /* 2 mantissa in 7 bits */ 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 }
| static int encode_exp | ( | uint8_t | encoded_exp[N/2], | |
| uint8_t | exp[N/2], | |||
| int | nb_exps, | |||
| int | exp_strategy | |||
| ) | [static] |
Definition at line 299 of file ac3enc.c.
References av_log(), AV_LOG_DEBUG, EXP_D15, EXP_D25, EXP_D45, FFMIN, N, and NULL.
Referenced by AC3_encode_frame().
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 /* for each group, compute the minimum exponent */ 00322 exp1[0] = exp[0]; /* DC exponent is handled separately */ 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 /* constraint for DC exponent */ 00336 if (exp1[0] > 15) 00337 exp1[0] = 15; 00338 00339 /* Decrease the delta between each groups to within 2 00340 * so that they can be differentially encoded */ 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 /* now we have the exponent values the decoder will see */ 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 }
| static void exponent_min | ( | uint8_t | exp[N/2], | |
| uint8_t | exp1[N/2], | |||
| int | n | |||
| ) | [static] |
Definition at line 287 of file ac3enc.c.
Referenced by AC3_encode_frame().
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 }
| static void fft | ( | IComplex * | z, | |
| int | ln | |||
| ) | [static] |
Definition at line 129 of file ac3enc.c.
References BF, CMUL, costab, ff_reverse, FFSWAP, IComplex::im, IComplex::re, and sintab.
Referenced by mdct512().
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 /* reverse */ 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 /* pass 0 */ 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 /* pass 1 */ 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 /* pass 2 .. ln-1 */ 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 }
| static void fft_init | ( | int | ln | ) | [static] |
Definition at line 91 of file ac3enc.c.
References costab, fix15(), M_PI, and sintab.
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 }
| static int16_t fix15 | ( | float | a | ) | [inline, static] |
Definition at line 76 of file ac3enc.c.
Referenced by fft_init().
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 }
| static int log2_tab | ( | int16_t * | tab, | |
| int | n | |||
| ) | [static] |