|
|
@ -144,7 +144,8 @@ static void adjust_frame_size(AC3EncodeContext *s) |
|
|
|
s->bits_written -= s->bit_rate; |
|
|
|
s->bits_written -= s->bit_rate; |
|
|
|
s->samples_written -= s->sample_rate; |
|
|
|
s->samples_written -= s->sample_rate; |
|
|
|
} |
|
|
|
} |
|
|
|
s->frame_size = s->frame_size_min + 2 * (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate); |
|
|
|
s->frame_size = s->frame_size_min + |
|
|
|
|
|
|
|
2 * (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate); |
|
|
|
s->bits_written += s->frame_size * 8; |
|
|
|
s->bits_written += s->frame_size * 8; |
|
|
|
s->samples_written += AC3_FRAME_SIZE; |
|
|
|
s->samples_written += AC3_FRAME_SIZE; |
|
|
|
} |
|
|
|
} |
|
|
@ -460,10 +461,8 @@ static void extract_exponents(AC3EncodeContext *s) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int blk, ch, i; |
|
|
|
int blk, ch, i; |
|
|
|
|
|
|
|
|
|
|
|
/* extract exponents */ |
|
|
|
|
|
|
|
for (ch = 0; ch < s->channels; ch++) { |
|
|
|
for (ch = 0; ch < s->channels; ch++) { |
|
|
|
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { |
|
|
|
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { |
|
|
|
/* compute "exponents". We take into account the normalization there */ |
|
|
|
|
|
|
|
AC3Block *block = &s->blocks[blk]; |
|
|
|
AC3Block *block = &s->blocks[blk]; |
|
|
|
for (i = 0; i < AC3_MAX_COEFS; i++) { |
|
|
|
for (i = 0; i < AC3_MAX_COEFS; i++) { |
|
|
|
int e; |
|
|
|
int e; |
|
|
@ -576,7 +575,7 @@ static void compute_exp_strategy(AC3EncodeContext *s) |
|
|
|
* exponent in the same frequency bin of a following block. |
|
|
|
* exponent in the same frequency bin of a following block. |
|
|
|
* exp[i] = min(exp[i], exp1[i] |
|
|
|
* exp[i] = min(exp[i], exp1[i] |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
static void exponent_min(uint8_t exp[AC3_MAX_COEFS], uint8_t exp1[AC3_MAX_COEFS], int n) |
|
|
|
static void exponent_min(uint8_t *exp, uint8_t *exp1, int n) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int i; |
|
|
|
int i; |
|
|
|
for (i = 0; i < n; i++) { |
|
|
|
for (i = 0; i < n; i++) { |
|
|
@ -589,8 +588,7 @@ static void exponent_min(uint8_t exp[AC3_MAX_COEFS], uint8_t exp1[AC3_MAX_COEFS] |
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Update the exponents so that they are the ones the decoder will decode. |
|
|
|
* Update the exponents so that they are the ones the decoder will decode. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
static void encode_exponents_blk_ch(uint8_t encoded_exp[AC3_MAX_COEFS], |
|
|
|
static void encode_exponents_blk_ch(uint8_t *encoded_exp, uint8_t *exp, |
|
|
|
uint8_t exp[AC3_MAX_COEFS], |
|
|
|
|
|
|
|
int nb_exps, int exp_strategy, |
|
|
|
int nb_exps, int exp_strategy, |
|
|
|
uint8_t *num_exp_groups) |
|
|
|
uint8_t *num_exp_groups) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -649,12 +647,12 @@ static void encode_exponents(AC3EncodeContext *s) |
|
|
|
AC3Block *block, *block1, *block2; |
|
|
|
AC3Block *block, *block1, *block2; |
|
|
|
|
|
|
|
|
|
|
|
for (ch = 0; ch < s->channels; ch++) { |
|
|
|
for (ch = 0; ch < s->channels; ch++) { |
|
|
|
/* for the EXP_REUSE case we select the min of the exponents */ |
|
|
|
|
|
|
|
blk = 0; |
|
|
|
blk = 0; |
|
|
|
block = &s->blocks[0]; |
|
|
|
block = &s->blocks[0]; |
|
|
|
while (blk < AC3_MAX_BLOCKS) { |
|
|
|
while (blk < AC3_MAX_BLOCKS) { |
|
|
|
blk1 = blk + 1; |
|
|
|
blk1 = blk + 1; |
|
|
|
block1 = block + 1; |
|
|
|
block1 = block + 1; |
|
|
|
|
|
|
|
/* for the EXP_REUSE case we select the min of the exponents */ |
|
|
|
while (blk1 < AC3_MAX_BLOCKS && block1->exp_strategy[ch] == EXP_REUSE) { |
|
|
|
while (blk1 < AC3_MAX_BLOCKS && block1->exp_strategy[ch] == EXP_REUSE) { |
|
|
|
exponent_min(block->exp[ch], block1->exp[ch], s->nb_coefs[ch]); |
|
|
|
exponent_min(block->exp[ch], block1->exp[ch], s->nb_coefs[ch]); |
|
|
|
blk1++; |
|
|
|
blk1++; |
|
|
@ -833,26 +831,26 @@ static void count_frame_bits(AC3EncodeContext *s) |
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Calculate the number of bits needed to encode a set of mantissas. |
|
|
|
* Calculate the number of bits needed to encode a set of mantissas. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
static int compute_mantissa_size(AC3EncodeContext *s, uint8_t *m, int nb_coefs) |
|
|
|
static int compute_mantissa_size(AC3EncodeContext *s, uint8_t *bap, int nb_coefs) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int bits, mant, i; |
|
|
|
int bits, b, i; |
|
|
|
|
|
|
|
|
|
|
|
bits = 0; |
|
|
|
bits = 0; |
|
|
|
for (i = 0; i < nb_coefs; i++) { |
|
|
|
for (i = 0; i < nb_coefs; i++) { |
|
|
|
mant = m[i]; |
|
|
|
b = bap[i]; |
|
|
|
switch (mant) { |
|
|
|
switch (b) { |
|
|
|
case 0: |
|
|
|
case 0: |
|
|
|
/* nothing */ |
|
|
|
/* bap=0 mantissas are not encoded */ |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 1: |
|
|
|
case 1: |
|
|
|
/* 3 mantissa in 5 bits */ |
|
|
|
/* 3 mantissas in 5 bits */ |
|
|
|
if (s->mant1_cnt == 0) |
|
|
|
if (s->mant1_cnt == 0) |
|
|
|
bits += 5; |
|
|
|
bits += 5; |
|
|
|
if (++s->mant1_cnt == 3) |
|
|
|
if (++s->mant1_cnt == 3) |
|
|
|
s->mant1_cnt = 0; |
|
|
|
s->mant1_cnt = 0; |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 2: |
|
|
|
case 2: |
|
|
|
/* 3 mantissa in 7 bits */ |
|
|
|
/* 3 mantissas in 7 bits */ |
|
|
|
if (s->mant2_cnt == 0) |
|
|
|
if (s->mant2_cnt == 0) |
|
|
|
bits += 7; |
|
|
|
bits += 7; |
|
|
|
if (++s->mant2_cnt == 3) |
|
|
|
if (++s->mant2_cnt == 3) |
|
|
@ -862,7 +860,7 @@ static int compute_mantissa_size(AC3EncodeContext *s, uint8_t *m, int nb_coefs) |
|
|
|
bits += 3; |
|
|
|
bits += 3; |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 4: |
|
|
|
case 4: |
|
|
|
/* 2 mantissa in 7 bits */ |
|
|
|
/* 2 mantissas in 7 bits */ |
|
|
|
if (s->mant4_cnt == 0) |
|
|
|
if (s->mant4_cnt == 0) |
|
|
|
bits += 7; |
|
|
|
bits += 7; |
|
|
|
if (++s->mant4_cnt == 2) |
|
|
|
if (++s->mant4_cnt == 2) |
|
|
@ -875,7 +873,7 @@ static int compute_mantissa_size(AC3EncodeContext *s, uint8_t *m, int nb_coefs) |
|
|
|
bits += 16; |
|
|
|
bits += 16; |
|
|
|
break; |
|
|
|
break; |
|
|
|
default: |
|
|
|
default: |
|
|
|
bits += mant - 1; |
|
|
|
bits += b - 1; |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -966,9 +964,8 @@ static int cbr_bit_allocation(AC3EncodeContext *s) |
|
|
|
bit_alloc(s, s->bap, snr_offset) > bits_left) { |
|
|
|
bit_alloc(s, s->bap, snr_offset) > bits_left) { |
|
|
|
snr_offset -= 64; |
|
|
|
snr_offset -= 64; |
|
|
|
} |
|
|
|
} |
|
|
|
if (snr_offset < 0) { |
|
|
|
if (snr_offset < 0) |
|
|
|
return AVERROR(EINVAL); |
|
|
|
return AVERROR(EINVAL); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (snr_offset + 64 <= 1023 && |
|
|
|
while (snr_offset + 64 <= 1023 && |
|
|
|
bit_alloc(s, s->bap1, snr_offset + 64) <= bits_left) { |
|
|
|
bit_alloc(s, s->bap1, snr_offset + 64) <= bits_left) { |
|
|
@ -1007,10 +1004,8 @@ static int cbr_bit_allocation(AC3EncodeContext *s) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
static int compute_bit_allocation(AC3EncodeContext *s) |
|
|
|
static int compute_bit_allocation(AC3EncodeContext *s) |
|
|
|
{ |
|
|
|
{ |
|
|
|
/* count frame bits other than exponents and mantissas */ |
|
|
|
|
|
|
|
count_frame_bits(s); |
|
|
|
count_frame_bits(s); |
|
|
|
|
|
|
|
|
|
|
|
/* calculate psd and masking curve before doing bit allocation */ |
|
|
|
|
|
|
|
bit_alloc_masking(s); |
|
|
|
bit_alloc_masking(s); |
|
|
|
|
|
|
|
|
|
|
|
return cbr_bit_allocation(s); |
|
|
|
return cbr_bit_allocation(s); |
|
|
@ -1217,11 +1212,18 @@ static void output_audio_block(AC3EncodeContext *s, |
|
|
|
int ch, i, baie, rbnd; |
|
|
|
int ch, i, baie, rbnd; |
|
|
|
AC3Block *block = &s->blocks[block_num]; |
|
|
|
AC3Block *block = &s->blocks[block_num]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* block switching */ |
|
|
|
for (ch = 0; ch < s->fbw_channels; ch++) |
|
|
|
for (ch = 0; ch < s->fbw_channels; ch++) |
|
|
|
put_bits(&s->pb, 1, 0); /* no block switching */ |
|
|
|
put_bits(&s->pb, 1, 0); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* dither flags */ |
|
|
|
for (ch = 0; ch < s->fbw_channels; ch++) |
|
|
|
for (ch = 0; ch < s->fbw_channels; ch++) |
|
|
|
put_bits(&s->pb, 1, 1); /* no dither */ |
|
|
|
put_bits(&s->pb, 1, 1); |
|
|
|
put_bits(&s->pb, 1, 0); /* no dynamic range */ |
|
|
|
|
|
|
|
|
|
|
|
/* dynamic range codes */ |
|
|
|
|
|
|
|
put_bits(&s->pb, 1, 0); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* channel coupling */ |
|
|
|
if (!block_num) { |
|
|
|
if (!block_num) { |
|
|
|
put_bits(&s->pb, 1, 1); /* coupling strategy present */ |
|
|
|
put_bits(&s->pb, 1, 1); /* coupling strategy present */ |
|
|
|
put_bits(&s->pb, 1, 0); /* no coupling strategy */ |
|
|
|
put_bits(&s->pb, 1, 0); /* no coupling strategy */ |
|
|
@ -1229,6 +1231,7 @@ static void output_audio_block(AC3EncodeContext *s, |
|
|
|
put_bits(&s->pb, 1, 0); /* no new coupling strategy */ |
|
|
|
put_bits(&s->pb, 1, 0); /* no new coupling strategy */ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* stereo rematrixing */ |
|
|
|
if (s->channel_mode == AC3_CHMODE_STEREO) { |
|
|
|
if (s->channel_mode == AC3_CHMODE_STEREO) { |
|
|
|
if (!block_num) { |
|
|
|
if (!block_num) { |
|
|
|
/* first block must define rematrixing (rematstr) */ |
|
|
|
/* first block must define rematrixing (rematstr) */ |
|
|
@ -1246,7 +1249,6 @@ static void output_audio_block(AC3EncodeContext *s, |
|
|
|
/* exponent strategy */ |
|
|
|
/* exponent strategy */ |
|
|
|
for (ch = 0; ch < s->fbw_channels; ch++) |
|
|
|
for (ch = 0; ch < s->fbw_channels; ch++) |
|
|
|
put_bits(&s->pb, 2, block->exp_strategy[ch]); |
|
|
|
put_bits(&s->pb, 2, block->exp_strategy[ch]); |
|
|
|
|
|
|
|
|
|
|
|
if (s->lfe_on) |
|
|
|
if (s->lfe_on) |
|
|
|
put_bits(&s->pb, 1, block->exp_strategy[s->lfe_channel]); |
|
|
|
put_bits(&s->pb, 1, block->exp_strategy[s->lfe_channel]); |
|
|
|
|
|
|
|
|
|
|
@ -1261,15 +1263,16 @@ static void output_audio_block(AC3EncodeContext *s, |
|
|
|
if (block->exp_strategy[ch] == EXP_REUSE) |
|
|
|
if (block->exp_strategy[ch] == EXP_REUSE) |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
|
|
|
|
|
|
|
|
/* first exponent */ |
|
|
|
/* DC exponent */ |
|
|
|
put_bits(&s->pb, 4, block->grouped_exp[ch][0]); |
|
|
|
put_bits(&s->pb, 4, block->grouped_exp[ch][0]); |
|
|
|
|
|
|
|
|
|
|
|
/* next ones are delta-encoded and grouped */ |
|
|
|
/* exponent groups */ |
|
|
|
for (i = 1; i <= block->num_exp_groups[ch]; i++) |
|
|
|
for (i = 1; i <= block->num_exp_groups[ch]; i++) |
|
|
|
put_bits(&s->pb, 7, block->grouped_exp[ch][i]); |
|
|
|
put_bits(&s->pb, 7, block->grouped_exp[ch][i]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* gain range info */ |
|
|
|
if (ch != s->lfe_channel) |
|
|
|
if (ch != s->lfe_channel) |
|
|
|
put_bits(&s->pb, 2, 0); /* no gain range info */ |
|
|
|
put_bits(&s->pb, 2, 0); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* bit allocation info */ |
|
|
|
/* bit allocation info */ |
|
|
@ -1296,10 +1299,9 @@ static void output_audio_block(AC3EncodeContext *s, |
|
|
|
put_bits(&s->pb, 1, 0); /* no delta bit allocation */ |
|
|
|
put_bits(&s->pb, 1, 0); /* no delta bit allocation */ |
|
|
|
put_bits(&s->pb, 1, 0); /* no data to skip */ |
|
|
|
put_bits(&s->pb, 1, 0); /* no data to skip */ |
|
|
|
|
|
|
|
|
|
|
|
/* mantissa encoding */ |
|
|
|
/* mantissas */ |
|
|
|
for (ch = 0; ch < s->channels; ch++) { |
|
|
|
for (ch = 0; ch < s->channels; ch++) { |
|
|
|
int b, q; |
|
|
|
int b, q; |
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < s->nb_coefs[ch]; i++) { |
|
|
|
for (i = 0; i < s->nb_coefs[ch]; i++) { |
|
|
|
q = block->qmant[ch][i]; |
|
|
|
q = block->qmant[ch][i]; |
|
|
|
b = s->bap[block_num][ch][i]; |
|
|
|
b = s->bap[block_num][ch][i]; |
|
|
@ -1361,28 +1363,27 @@ static void output_frame_end(AC3EncodeContext *s) |
|
|
|
int frame_size, frame_size_58, pad_bytes, crc1, crc2, crc_inv; |
|
|
|
int frame_size, frame_size_58, pad_bytes, crc1, crc2, crc_inv; |
|
|
|
uint8_t *frame; |
|
|
|
uint8_t *frame; |
|
|
|
|
|
|
|
|
|
|
|
frame_size = s->frame_size; /* frame size in words */ |
|
|
|
frame_size = s->frame_size; |
|
|
|
/* align to 8 bits */ |
|
|
|
frame_size_58 = ((frame_size >> 2) + (frame_size >> 4)) << 1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* pad the remainder of the frame with zeros */ |
|
|
|
flush_put_bits(&s->pb); |
|
|
|
flush_put_bits(&s->pb); |
|
|
|
/* add zero bytes to reach the frame size */ |
|
|
|
|
|
|
|
frame = s->pb.buf; |
|
|
|
frame = s->pb.buf; |
|
|
|
pad_bytes = s->frame_size - (put_bits_ptr(&s->pb) - frame) - 2; |
|
|
|
pad_bytes = s->frame_size - (put_bits_ptr(&s->pb) - frame) - 2; |
|
|
|
assert(pad_bytes >= 0); |
|
|
|
assert(pad_bytes >= 0); |
|
|
|
if (pad_bytes > 0) |
|
|
|
if (pad_bytes > 0) |
|
|
|
memset(put_bits_ptr(&s->pb), 0, pad_bytes); |
|
|
|
memset(put_bits_ptr(&s->pb), 0, pad_bytes); |
|
|
|
|
|
|
|
|
|
|
|
/* Now we must compute both crcs : this is not so easy for crc1
|
|
|
|
/* compute crc1 */ |
|
|
|
because it is at the beginning of the data... */ |
|
|
|
/* this is not so easy because it is at the beginning of the data... */ |
|
|
|
frame_size_58 = ((frame_size >> 2) + (frame_size >> 4)) << 1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
crc1 = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, |
|
|
|
crc1 = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, |
|
|
|
frame + 4, frame_size_58 - 4)); |
|
|
|
frame + 4, frame_size_58 - 4)); |
|
|
|
|
|
|
|
|
|
|
|
/* XXX: could precompute crc_inv */ |
|
|
|
/* XXX: could precompute crc_inv */ |
|
|
|
crc_inv = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY); |
|
|
|
crc_inv = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY); |
|
|
|
crc1 = mul_poly(crc_inv, crc1, CRC16_POLY); |
|
|
|
crc1 = mul_poly(crc_inv, crc1, CRC16_POLY); |
|
|
|
AV_WB16(frame + 2, crc1); |
|
|
|
AV_WB16(frame + 2, crc1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* compute crc2 */ |
|
|
|
crc2 = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, |
|
|
|
crc2 = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, |
|
|
|
frame + frame_size_58, |
|
|
|
frame + frame_size_58, |
|
|
|
frame_size - frame_size_58 - 2)); |
|
|
|
frame_size - frame_size_58 - 2)); |
|
|
@ -1402,9 +1403,8 @@ static void output_frame(AC3EncodeContext *s, |
|
|
|
|
|
|
|
|
|
|
|
output_frame_header(s); |
|
|
|
output_frame_header(s); |
|
|
|
|
|
|
|
|
|
|
|
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { |
|
|
|
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) |
|
|
|
output_audio_block(s, blk); |
|
|
|
output_audio_block(s, blk); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output_frame_end(s); |
|
|
|
output_frame_end(s); |
|
|
|
} |
|
|
|
} |
|
|
|