#include "libavutil/audioconvert.h"
#include "libavutil/avassert.h"
#include "libavutil/crc.h"
#include "avcodec.h"
#include "put_bits.h"
#include "dsputil.h"
#include "ac3dsp.h"
#include "ac3.h"
#include "audioconvert.h"
#include "ac3enc_fixed.h"
#include "ac3enc_float.h"
Go to the source code of this file.
Data Structures | |
struct | AC3Block |
Data for a single audio block. More... | |
struct | AC3EncodeContext |
AC-3 encoder private context. More... | |
Defines | |
#define | CONFIG_AC3ENC_FLOAT 0 |
#define | AC3_MAX_EXP_GROUPS 85 |
Maximum number of exponent groups. | |
#define | AC3_REMATRIXING_IS_STATIC 0x1 |
#define | AC3_REMATRIXING_SUMS 0 |
#define | AC3_REMATRIXING_NONE 1 |
#define | AC3_REMATRIXING_ALWAYS 3 |
#define | SCALE_FLOAT(a, bits) lrintf((a) * (float)(1 << (bits))) |
Scale a float value by 2^bits and convert to an integer. | |
#define | EXP_DIFF_THRESHOLD 500 |
Exponent Difference Threshold. | |
#define | CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16)) |
CRC-16 Polynomial. | |
Functions | |
static av_cold void | mdct_end (AC3MDCTContext *mdct) |
static av_cold int | mdct_init (AVCodecContext *avctx, AC3MDCTContext *mdct, int nbits) |
static void | mdct512 (AC3MDCTContext *mdct, CoefType *out, SampleType *in) |
static void | apply_window (DSPContext *dsp, SampleType *output, const SampleType *input, const SampleType *window, int n) |
static int | normalize_samples (AC3EncodeContext *s) |
static void | scale_coefficients (AC3EncodeContext *s) |
static void | adjust_frame_size (AC3EncodeContext *s) |
Adjust the frame size to make the average bit rate match the target bit rate. | |
static void | deinterleave_input_samples (AC3EncodeContext *s, const SampleType *samples) |
Deinterleave input samples. | |
static void | apply_mdct (AC3EncodeContext *s) |
Apply the MDCT to input samples to generate frequency coefficients. | |
static void | rematrixing_init (AC3EncodeContext *s) |
Initialize stereo rematrixing. | |
static void | compute_rematrixing_strategy (AC3EncodeContext *s) |
Determine rematrixing flags for each block and band. | |
static void | apply_rematrixing (AC3EncodeContext *s) |
Apply stereo rematrixing to coefficients based on rematrixing flags. | |
static av_cold void | exponent_init (AC3EncodeContext *s) |
Initialize exponent tables. | |
static void | extract_exponents (AC3EncodeContext *s) |
Extract exponents from the MDCT coefficients. | |
static void | compute_exp_strategy_ch (AC3EncodeContext *s, uint8_t *exp_strategy, uint8_t *exp) |
Calculate exponent strategies for all blocks in a single channel. | |
static void | compute_exp_strategy (AC3EncodeContext *s) |
Calculate exponent strategies for all channels. | |
static void | encode_exponents_blk_ch (uint8_t *exp, int nb_exps, int exp_strategy) |
Update the exponents so that they are the ones the decoder will decode. | |
static void | encode_exponents (AC3EncodeContext *s) |
Encode exponents from original extracted form to what the decoder will see. | |
static void | group_exponents (AC3EncodeContext *s) |
Group exponents. | |
static void | process_exponents (AC3EncodeContext *s) |
Calculate final exponents from the supplied MDCT coefficients and exponent shift. | |
static void | count_frame_bits_fixed (AC3EncodeContext *s) |
Count frame bits that are based solely on fixed parameters. | |
static void | bit_alloc_init (AC3EncodeContext *s) |
Initialize bit allocation. | |
static void | count_frame_bits (AC3EncodeContext *s) |
Count the bits used to encode the frame, minus exponents and mantissas. | |
static int | compute_mantissa_size (int mant_cnt[5], uint8_t *bap, int nb_coefs) |
Calculate the number of bits needed to encode a set of mantissas. | |
static int | compute_mantissa_size_final (int mant_cnt[5]) |
Finalize the mantissa bit count by adding in the grouped mantissas. | |
static void | bit_alloc_masking (AC3EncodeContext *s) |
Calculate masking curve based on the final exponents. | |
static void | reset_block_bap (AC3EncodeContext *s) |
Ensure that bap for each block and channel point to the current bap_buffer. | |
static int | bit_alloc (AC3EncodeContext *s, int snr_offset) |
Run the bit allocation with a given SNR offset. | |
static int | cbr_bit_allocation (AC3EncodeContext *s) |
Constant bitrate bit allocation search. | |
static int | downgrade_exponents (AC3EncodeContext *s) |
Downgrade exponent strategies to reduce the bits used by the exponents. | |
static int | reduce_bandwidth (AC3EncodeContext *s, int min_bw_code) |
Reduce the bandwidth to reduce the number of bits used for a given SNR offset. | |
static int | compute_bit_allocation (AC3EncodeContext *s) |
Perform bit allocation search. | |
static int | sym_quant (int c, int e, int levels) |
Symmetric quantization on 'levels' levels. | |
static int | asym_quant (int c, int e, int qbits) |
Asymmetric quantization on 2^qbits levels. | |
static void | quantize_mantissas_blk_ch (AC3EncodeContext *s, int32_t *fixed_coef, int8_t exp_shift, uint8_t *exp, uint8_t *bap, uint16_t *qmant, int n) |
Quantize a set of mantissas for a single channel in a single block. | |
static void | quantize_mantissas (AC3EncodeContext *s) |
Quantize mantissas using coefficients, exponents, and bit allocation pointers. | |
static void | output_frame_header (AC3EncodeContext *s) |
Write the AC-3 frame header to the output bitstream. | |
static void | output_audio_block (AC3EncodeContext *s, int blk) |
Write one audio block to the output bitstream. | |
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 void | output_frame_end (AC3EncodeContext *s) |
Fill the end of the frame with 0's and compute the two CRCs. | |
static void | output_frame (AC3EncodeContext *s, unsigned char *frame) |
Write the frame to the output bitstream. | |
static int | ac3_encode_frame (AVCodecContext *avctx, unsigned char *frame, int buf_size, void *data) |
Encode a single AC-3 frame. | |
static av_cold int | ac3_encode_close (AVCodecContext *avctx) |
Finalize encoding and free any memory allocated by the encoder. | |
static av_cold int | set_channel_info (AC3EncodeContext *s, int channels, int64_t *channel_layout) |
Set channel information during initialization. | |
static av_cold int | validate_options (AVCodecContext *avctx, AC3EncodeContext *s) |
static av_cold void | set_bandwidth (AC3EncodeContext *s) |
Set bandwidth for all channels. | |
static av_cold int | allocate_buffers (AVCodecContext *avctx) |
static av_cold int | ac3_encode_init (AVCodecContext *avctx) |
Initialize the encoder. | |
Variables | |
static uint8_t | exponent_group_tab [3][256] |
LUT for number of exponent groups. | |
static const int64_t | ac3_channel_layouts [] |
List of supported channel layouts. |
Definition in file ac3enc.c.
#define AC3_MAX_EXP_GROUPS 85 |
#define AC3_REMATRIXING_ALWAYS 3 |
#define AC3_REMATRIXING_IS_STATIC 0x1 |
Definition at line 52 of file ac3enc.c.
Referenced by compute_rematrixing_strategy(), and rematrixing_init().
#define AC3_REMATRIXING_NONE 1 |
#define AC3_REMATRIXING_SUMS 0 |
#define CONFIG_AC3ENC_FLOAT 0 |
#define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16)) |
CRC-16 Polynomial.
Definition at line 1400 of file ac3enc.c.
Referenced by ac3_encode_init(), and output_frame_end().
#define EXP_DIFF_THRESHOLD 500 |
Exponent Difference Threshold.
New exponents are sent if their SAD exceed this number.
Definition at line 444 of file ac3enc.c.
Referenced by compute_exp_strategy_ch().
Scale a float value by 2^bits and convert to an integer.
Definition at line 58 of file ac3enc.c.
Referenced by scale_coefficients().
static av_cold int ac3_encode_close | ( | AVCodecContext * | avctx | ) | [static] |
Finalize encoding and free any memory allocated by the encoder.
Definition at line 1535 of file ac3enc.c.
Referenced by ac3_encode_init().
static int ac3_encode_frame | ( | AVCodecContext * | avctx, | |
unsigned char * | frame, | |||
int | buf_size, | |||
void * | data | |||
) | [static] |
static av_cold int ac3_encode_init | ( | AVCodecContext * | avctx | ) | [static] |
static void adjust_frame_size | ( | AC3EncodeContext * | s | ) | [static] |
Adjust the frame size to make the average bit rate match the target bit rate.
This is only needed for 11025, 22050, and 44100 sample rates.
Definition at line 214 of file ac3enc.c.
Referenced by ac3_encode_frame().
static av_cold int allocate_buffers | ( | AVCodecContext * | avctx | ) | [static] |
Definition at line 1706 of file ac3enc.c.
Referenced by ac3_encode_init(), alac_set_info(), decode_frame(), flac_decode_init(), parse_streaminfo(), and shorten_decode_frame().
static void apply_mdct | ( | AC3EncodeContext * | s | ) | [static] |
Apply the MDCT to input samples to generate frequency coefficients.
This applies the KBD window and normalizes the input to reduce precision loss due to fixed-point calculations.
Definition at line 261 of file ac3enc.c.
Referenced by ac3_encode_frame(), and encode_block().
static void apply_rematrixing | ( | AC3EncodeContext * | s | ) | [static] |
Apply stereo rematrixing to coefficients based on rematrixing flags.
Definition at line 357 of file ac3enc.c.
Referenced by ac3_encode_frame().
static void apply_window | ( | DSPContext * | dsp, | |
SampleType * | output, | |||
const SampleType * | input, | |||
const SampleType * | window, | |||
int | n | |||
) | [static] |
Referenced by apply_mdct(), apply_window_mp3(), backward_filter(), and do_hybrid_window().
static int asym_quant | ( | int | c, | |
int | e, | |||
int | qbits | |||
) | [inline, static] |
Asymmetric quantization on 2^qbits levels.
Definition at line 1119 of file ac3enc.c.
Referenced by quantize_mantissas_blk_ch().
static int bit_alloc | ( | AC3EncodeContext * | s, | |
int | snr_offset | |||
) | [static] |
Run the bit allocation with a given SNR offset.
This calculates the bit allocation pointers that will be used to determine the quantization of each mantissa.
Definition at line 904 of file ac3enc.c.
Referenced by cbr_bit_allocation(), compute_bit_allocation(), encode_frame(), mp_decode_layer2(), and MPA_encode_frame().
static void bit_alloc_init | ( | AC3EncodeContext * | s | ) | [static] |
Initialize bit allocation.
Set default parameter codes and calculate parameter values.
Definition at line 754 of file ac3enc.c.
Referenced by ac3_encode_init().
static void bit_alloc_masking | ( | AC3EncodeContext * | s | ) | [static] |
Calculate masking curve based on the final exponents.
Also calculate the power spectral densities to use in future calculations.
Definition at line 854 of file ac3enc.c.
Referenced by compute_bit_allocation().
static int cbr_bit_allocation | ( | AC3EncodeContext * | s | ) | [static] |
Constant bitrate bit allocation search.
Find the largest SNR offset that will allow data to fit in the frame.
Definition at line 947 of file ac3enc.c.
Referenced by compute_bit_allocation().
static int compute_bit_allocation | ( | AC3EncodeContext * | s | ) | [static] |
Perform bit allocation search.
Finds the SNR offset value that maximizes quality and fits in the specified frame size. Output is the SNR offset and a set of bit allocation pointers used to quantize the mantissas.
Definition at line 1059 of file ac3enc.c.
Referenced by ac3_encode_frame(), and MPA_encode_frame().
static void compute_exp_strategy | ( | AC3EncodeContext * | s | ) | [static] |
Calculate exponent strategies for all channels.
Array arrangement is reversed to simplify the per-channel calculation.
Definition at line 491 of file ac3enc.c.
Referenced by process_exponents().
static void compute_exp_strategy_ch | ( | AC3EncodeContext * | s, | |
uint8_t * | exp_strategy, | |||
uint8_t * | exp | |||
) | [static] |
Calculate exponent strategies for all blocks in a single channel.
Definition at line 450 of file ac3enc.c.
Referenced by compute_exp_strategy().
static int compute_mantissa_size | ( | int | mant_cnt[5], | |
uint8_t * | bap, | |||
int | nb_coefs | |||
) | [static] |
Calculate the number of bits needed to encode a set of mantissas.
Definition at line 812 of file ac3enc.c.
Referenced by bit_alloc().
static int compute_mantissa_size_final | ( | int | mant_cnt[5] | ) | [static] |
Finalize the mantissa bit count by adding in the grouped mantissas.
Definition at line 837 of file ac3enc.c.
Referenced by bit_alloc().
static void compute_rematrixing_strategy | ( | AC3EncodeContext * | s | ) | [static] |
Determine rematrixing flags for each block and band.
Definition at line 305 of file ac3enc.c.
Referenced by ac3_encode_frame().
static void count_frame_bits | ( | AC3EncodeContext * | s | ) | [static] |
Count the bits used to encode the frame, minus exponents and mantissas.
Bits based on fixed parameters have already been counted, so now we just have to add the bits based on parameters that change during encoding.
Definition at line 788 of file ac3enc.c.
Referenced by compute_bit_allocation().
static void count_frame_bits_fixed | ( | AC3EncodeContext * | s | ) | [static] |
Count frame bits that are based solely on fixed parameters.
This only has to be run once when the encoder is initialized.
Definition at line 700 of file ac3enc.c.
Referenced by bit_alloc_init().
static void deinterleave_input_samples | ( | AC3EncodeContext * | s, | |
const SampleType * | samples | |||
) | [static] |
Deinterleave input samples.
Channels are reordered from FFmpeg's default order to AC-3 order.
Definition at line 231 of file ac3enc.c.
Referenced by ac3_encode_frame().
static int downgrade_exponents | ( | AC3EncodeContext * | s | ) | [static] |
Downgrade exponent strategies to reduce the bits used by the exponents.
This is a fallback for when bit allocation fails with the normal exponent strategies. Each time this function is run it only downgrades the strategy in 1 channel of 1 block.
Definition at line 998 of file ac3enc.c.
Referenced by compute_bit_allocation().
static void encode_exponents | ( | AC3EncodeContext * | s | ) | [static] |
Encode exponents from original extracted form to what the decoder will see.
This copies and groups exponents based on exponent strategy and reduces deltas between adjacent exponent groups so that they can be differentially encoded.
Definition at line 579 of file ac3enc.c.
Referenced by compute_bit_allocation(), and process_exponents().
static void encode_exponents_blk_ch | ( | uint8_t * | exp, | |
int | nb_exps, | |||
int | exp_strategy | |||
) | [static] |
Update the exponents so that they are the ones the decoder will decode.
Definition at line 510 of file ac3enc.c.
Referenced by encode_exponents().
static av_cold void exponent_init | ( | AC3EncodeContext * | s | ) | [static] |
Initialize exponent tables.
Definition at line 392 of file ac3enc.c.
Referenced by ac3_encode_init().
static void extract_exponents | ( | AC3EncodeContext * | s | ) | [static] |
Extract exponents from the MDCT coefficients.
This takes into account the normalization that was done to the input samples by adjusting the exponents by the exponent shift values.
Definition at line 410 of file ac3enc.c.
Referenced by compute_bit_allocation(), and process_exponents().
static void group_exponents | ( | AC3EncodeContext * | s | ) | [static] |
Group exponents.
3 delta-encoded exponents are in each 7-bit group. The number of groups varies depending on exponent strategy and bandwidth.
Definition at line 623 of file ac3enc.c.
Referenced by compute_bit_allocation(), and process_exponents().
static void mdct512 | ( | AC3MDCTContext * | mdct, | |
CoefType * | out, | |||
SampleType * | in | |||
) | [static] |
Referenced by apply_mdct().
static av_cold void mdct_end | ( | AC3MDCTContext * | mdct | ) | [static] |
Referenced by ac3_encode_close(), fft_init(), and mdct_init().
static av_cold int mdct_init | ( | AVCodecContext * | avctx, | |
AC3MDCTContext * | mdct, | |||
int | nbits | |||
) | [static] |
Referenced by ac3_encode_init().
static unsigned int mul_poly | ( | unsigned int | a, | |
unsigned int | b, | |||
unsigned int | poly | |||
) | [static] |
static int normalize_samples | ( | AC3EncodeContext * | s | ) | [static] |
Referenced by apply_mdct().
static void output_audio_block | ( | AC3EncodeContext * | s, | |
int | blk | |||
) | [static] |
Write one audio block to the output bitstream.
Definition at line 1288 of file ac3enc.c.
Referenced by output_frame().
static void output_frame | ( | AC3EncodeContext * | s, | |
unsigned char * | frame | |||
) | [static] |
Write the frame to the output bitstream.
Definition at line 1478 of file ac3enc.c.
Referenced by ac3_encode_frame().
static void output_frame_end | ( | AC3EncodeContext * | s | ) | [static] |
Fill the end of the frame with 0's and compute the two CRCs.
Definition at line 1437 of file ac3enc.c.
Referenced by output_frame().
static void output_frame_header | ( | AC3EncodeContext * | s | ) | [static] |
Write the AC-3 frame header to the output bitstream.
Definition at line 1257 of file ac3enc.c.
Referenced by output_frame().
static unsigned int pow_poly | ( | unsigned int | a, | |
unsigned int | n, | |||
unsigned int | poly | |||
) | [static] |
static void process_exponents | ( | AC3EncodeContext * | s | ) | [static] |
Calculate final exponents from the supplied MDCT coefficients and exponent shift.
Extract exponents from MDCT coefficients, calculate exponent strategies, and encode final exponents.
Definition at line 682 of file ac3enc.c.
Referenced by ac3_encode_frame(), and compute_bit_allocation().
static void quantize_mantissas | ( | AC3EncodeContext * | s | ) | [static] |
Quantize mantissas using coefficients, exponents, and bit allocation pointers.
Definition at line 1235 of file ac3enc.c.
Referenced by ac3_encode_frame().
static void quantize_mantissas_blk_ch | ( | AC3EncodeContext * | s, | |
int32_t * | fixed_coef, | |||
int8_t | exp_shift, | |||
uint8_t * | exp, | |||
uint8_t * | bap, | |||
uint16_t * | qmant, | |||
int | n | |||
) | [static] |
Quantize a set of mantissas for a single channel in a single block.
Definition at line 1141 of file ac3enc.c.
Referenced by quantize_mantissas().
static int reduce_bandwidth | ( | AC3EncodeContext * | s, | |
int | min_bw_code | |||
) | [static] |
Reduce the bandwidth to reduce the number of bits used for a given SNR offset.
This is a second fallback for when bit allocation still fails after exponents have been downgraded.
Definition at line 1038 of file ac3enc.c.
Referenced by compute_bit_allocation().
static void rematrixing_init | ( | AC3EncodeContext * | s | ) | [static] |
Initialize stereo rematrixing.
If the strategy does not change for each frame, set the rematrixing flags.
Definition at line 284 of file ac3enc.c.
Referenced by ac3_encode_init().
static void reset_block_bap | ( | AC3EncodeContext * | s | ) | [static] |
Ensure that bap for each block and channel point to the current bap_buffer.
They may have been switched during the bit allocation search.
Definition at line 884 of file ac3enc.c.
Referenced by bit_alloc(), and cbr_bit_allocation().
static void scale_coefficients | ( | AC3EncodeContext * | s | ) | [static] |
Referenced by ac3_encode_frame().
static av_cold void set_bandwidth | ( | AC3EncodeContext * | s | ) | [static] |
Set bandwidth for all channels.
The user can optionally supply a cutoff frequency. Otherwise an appropriate default value will be used.
Definition at line 1680 of file ac3enc.c.
Referenced by ac3_encode_init().
static av_cold int set_channel_info | ( | AC3EncodeContext * | s, | |
int | channels, | |||
int64_t * | channel_layout | |||
) | [static] |
Set channel information during initialization.
Definition at line 1576 of file ac3enc.c.
Referenced by validate_options().
static int sym_quant | ( | int | c, | |
int | e, | |||
int | levels | |||
) | [inline, static] |
Symmetric quantization on 'levels' levels.
Definition at line 1098 of file ac3enc.c.
Referenced by quantize_mantissas_blk_ch().
static av_cold int validate_options | ( | AVCodecContext * | avctx, | |
AC3EncodeContext * | s | |||
) | [static] |
const int64_t ac3_channel_layouts[] [static] |
Initial value:
{ AV_CH_LAYOUT_MONO, AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_2_1, AV_CH_LAYOUT_SURROUND, AV_CH_LAYOUT_2_2, AV_CH_LAYOUT_QUAD, AV_CH_LAYOUT_4POINT0, AV_CH_LAYOUT_5POINT0, AV_CH_LAYOUT_5POINT0_BACK, (AV_CH_LAYOUT_MONO | AV_CH_LOW_FREQUENCY), (AV_CH_LAYOUT_STEREO | AV_CH_LOW_FREQUENCY), (AV_CH_LAYOUT_2_1 | AV_CH_LOW_FREQUENCY), (AV_CH_LAYOUT_SURROUND | AV_CH_LOW_FREQUENCY), (AV_CH_LAYOUT_2_2 | AV_CH_LOW_FREQUENCY), (AV_CH_LAYOUT_QUAD | AV_CH_LOW_FREQUENCY), (AV_CH_LAYOUT_4POINT0 | AV_CH_LOW_FREQUENCY), AV_CH_LAYOUT_5POINT1, AV_CH_LAYOUT_5POINT1_BACK, 0 }
uint8_t exponent_group_tab[3][256] [static] |
LUT for number of exponent groups.
exponent_group_tab[exponent strategy-1][number of coefficients]
Definition at line 181 of file ac3enc.c.
Referenced by encode_exponents_blk_ch(), exponent_init(), group_exponents(), and output_audio_block().