1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

cosmetics: pretty-printing, alignment, etc...

Originally committed as revision 25958 to svn://svn.ffmpeg.org/ffmpeg/trunk
This commit is contained in:
Justin Ruggles 2010-12-14 14:51:09 +00:00
parent a913b5cf60
commit 6a988808a4

View File

@ -23,6 +23,7 @@
* @file
* The simplest AC-3 encoder.
*/
//#define DEBUG
#include "libavcore/audioconvert.h"
@ -84,9 +85,6 @@ static int16_t xsin1[128];
#define MDCT_NBITS 9
#define MDCT_SAMPLES (1 << MDCT_NBITS)
/* new exponents are sent if their Norm 1 exceed this number */
#define EXP_DIFF_THRESHOLD 1000
#define FIX15(a) av_clip_int16(SCALE_FLOAT(a, 15))
typedef struct IComplex {
@ -100,7 +98,7 @@ static av_cold void fft_init(int ln)
n = 1 << ln;
for(i=0;i<(n/2);i++) {
for (i = 0; i < n/2; i++) {
alpha = 2 * M_PI * (float)i / (float)n;
costab[i] = FIX15(cos(alpha));
sintab[i] = FIX15(sin(alpha));
@ -164,12 +162,12 @@ static void fft(IComplex *z, int ln)
/* pass 0 */
p = &z[0];
j=(np >> 1);
j = np >> 1;
do {
BF(p[0].re, p[0].im, p[1].re, p[1].im,
p[0].re, p[0].im, p[1].re, p[1].im);
p += 2;
} while (--j != 0);
} while (--j);
/* pass 1 */
@ -181,7 +179,7 @@ static void fft(IComplex *z, int ln)
BF(p[1].re, p[1].im, p[3].re, p[3].im,
p[1].re, p[1].im, p[3].im, -p[3].re);
p+=4;
} while (--j != 0);
} while (--j);
/* pass 2 .. ln-1 */
@ -191,11 +189,9 @@ static void fft(IComplex *z, int ln)
do {
p = z;
q = z + nloops;
for (j = 0; j < nblocks; ++j) {
for (j = 0; j < nblocks; j++) {
BF(p->re, p->im, q->re, q->im,
p->re, p->im, q->re, q->im);
p++;
q++;
for(l = nblocks; l < np2; l += nblocks) {
@ -210,10 +206,9 @@ static void fft(IComplex *z, int ln)
}
nblocks = nblocks >> 1;
nloops = nloops << 1;
} while (nblocks != 0);
} while (nblocks);
}
/* do a 512 point mdct */
static void mdct512(int32_t *out, int16_t *in)
{
int i, re, im, re1, im1;
@ -223,7 +218,7 @@ static void mdct512(int32_t *out, int16_t *in)
/* shift to simplify computations */
for (i = 0; i < MDCT_SAMPLES/4; i++)
rot[i] = -in[i + 3*MDCT_SAMPLES/4];
for(i=MDCT_SAMPLES/4;i<MDCT_SAMPLES;i++)
for (;i < MDCT_SAMPLES; i++)
rot[i] = in[i - MDCT_SAMPLES/4];
/* pre rotation */
@ -245,17 +240,18 @@ static void mdct512(int32_t *out, int16_t *in)
}
}
/* XXX: use another norm ? */
static int calc_exp_diff(uint8_t *exp1, uint8_t *exp2, int n)
{
int sum, i;
sum = 0;
for(i=0;i<n;i++) {
for (i = 0; i < n; i++)
sum += abs(exp1[i] - exp2[i]);
}
return sum;
}
/* new exponents are sent if their Norm 1 exceed this number */
#define EXP_DIFF_THRESHOLD 1000
static void compute_exp_strategy(uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS],
uint8_t exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
int ch, int is_lfe)
@ -303,7 +299,6 @@ static void compute_exp_strategy(uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CH
static void exponent_min(uint8_t exp[AC3_MAX_COEFS], uint8_t exp1[AC3_MAX_COEFS], int n)
{
int i;
for (i = 0; i < n; i++) {
if (exp1[i] < exp[i])
exp[i] = exp1[i];
@ -314,8 +309,7 @@ static void exponent_min(uint8_t exp[AC3_MAX_COEFS], uint8_t exp1[AC3_MAX_COEFS]
decode. Return the number of bits used to code the exponents */
static int encode_exp(uint8_t encoded_exp[AC3_MAX_COEFS],
uint8_t exp[AC3_MAX_COEFS],
int nb_exps,
int exp_strategy)
int nb_exps, int exp_strategy)
{
int group_size, nb_groups, i, j, k, exp_min;
uint8_t exp1[AC3_MAX_COEFS];
@ -352,8 +346,8 @@ static int encode_exp(uint8_t encoded_exp[AC3_MAX_COEFS],
if (exp1[0] > 15)
exp1[0] = 15;
/* Decrease the delta between each groups to within 2
* so that they can be differentially encoded */
/* decrease the delta between each groups to within 2 so that they can be
differentially encoded */
for (i = 1; i <= nb_groups; i++)
exp1[i] = FFMIN(exp1[i], exp1[i-1] + 2);
for (i = nb_groups-1; i >= 0; i--)
@ -363,9 +357,8 @@ static int encode_exp(uint8_t encoded_exp[AC3_MAX_COEFS],
encoded_exp[0] = exp1[0];
k = 1;
for (i = 1; i <= nb_groups; i++) {
for(j=0;j<group_size;j++) {
for (j = 0; j < group_size; j++)
encoded_exp[k+j] = exp1[i];
}
k += group_size;
}
@ -463,7 +456,6 @@ static int bit_alloc(AC3EncodeContext *s,
snr_offset = (((coarse_snr_offset - 15) << 4) + fine_snr_offset) << 2;
/* compute size */
for (i = 0; i < AC3_MAX_BLOCKS; i++) {
s->mant1_cnt = 0;
s->mant2_cnt = 0;
@ -473,8 +465,7 @@ static int bit_alloc(AC3EncodeContext *s,
s->nb_coefs[ch], snr_offset,
s->bit_alloc.floor, ff_ac3_bap_tab,
bap[i][ch]);
frame_bits += compute_mantissa_size(s, bap[i][ch],
s->nb_coefs[ch]);
frame_bits += compute_mantissa_size(s, bap[i][ch], s->nb_coefs[ch]);
}
}
return 16 * s->frame_size - frame_bits;
@ -522,7 +513,8 @@ static int compute_bit_allocation(AC3EncodeContext *s,
frame_bits += s->fbw_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */
if (s->channel_mode == AC3_CHMODE_STEREO) {
frame_bits++; /* rematstr */
if(i==0) frame_bits += 4;
if (!i)
frame_bits += 4;
}
frame_bits += 2 * s->fbw_channels; /* chexpstr[2] * c */
if (s->lfe_on)
@ -562,26 +554,26 @@ static int compute_bit_allocation(AC3EncodeContext *s,
av_log(NULL, AV_LOG_ERROR, "Bit allocation failed. Try increasing the bitrate.\n");
return -1;
}
while ((coarse_snr_offset + SNR_INC1) <= 63 &&
while (coarse_snr_offset + SNR_INC1 <= 63 &&
bit_alloc(s, mask, psd, bap1, frame_bits,
coarse_snr_offset + SNR_INC1, 0) >= 0) {
coarse_snr_offset += SNR_INC1;
memcpy(bap, bap1, sizeof(bap1));
}
while ((coarse_snr_offset + 1) <= 63 &&
while (coarse_snr_offset + 1 <= 63 &&
bit_alloc(s, mask, psd, bap1, frame_bits, coarse_snr_offset + 1, 0) >= 0) {
coarse_snr_offset++;
memcpy(bap, bap1, sizeof(bap1));
}
fine_snr_offset = 0;
while ((fine_snr_offset + SNR_INC1) <= 15 &&
while (fine_snr_offset + SNR_INC1 <= 15 &&
bit_alloc(s, mask, psd, bap1, frame_bits,
coarse_snr_offset, fine_snr_offset + SNR_INC1) >= 0) {
fine_snr_offset += SNR_INC1;
memcpy(bap, bap1, sizeof(bap1));
}
while ((fine_snr_offset + 1) <= 15 &&
while (fine_snr_offset + 1 <= 15 &&
bit_alloc(s, mask, psd, bap1, frame_bits,
coarse_snr_offset, fine_snr_offset + 1) >= 0) {
fine_snr_offset++;
@ -689,7 +681,7 @@ static av_cold int AC3_encode_init(AVCodecContext *avctx)
s->samples_written = 0;
s->frame_size = s->frame_size_min;
/* bit allocation init */
/* set bandwidth */
if(avctx->cutoff) {
/* calculate bandwidth based on user-specified cutoff frequency */
int cutoff = av_clip(avctx->cutoff, 1, s->sample_rate >> 1);
@ -706,9 +698,9 @@ static av_cold int AC3_encode_init(AVCodecContext *avctx)
s->bandwidth_code[ch] = bw_code;
s->nb_coefs[ch] = bw_code * 3 + 73;
}
if (s->lfe_on) {
s->nb_coefs[s->lfe_channel] = 7; /* fixed */
}
if (s->lfe_on)
s->nb_coefs[s->lfe_channel] = 7; /* LFE channel always has 7 coefs */
/* initial snr offset */
s->coarse_snr_offset = 40;
@ -806,46 +798,39 @@ static void output_audio_block(AC3EncodeContext *s,
int delta0, delta1, delta2;
for (ch = 0; ch < s->fbw_channels; ch++)
put_bits(&s->pb, 1, 0); /* 512 point MDCT */
put_bits(&s->pb, 1, 0); /* no block switching */
for (ch = 0; ch < s->fbw_channels; ch++)
put_bits(&s->pb, 1, 1); /* no dither */
put_bits(&s->pb, 1, 0); /* no dynamic range */
if (block_num == 0) {
/* for block 0, even if no coupling, we must say it. This is a
waste of bit :-) */
if (!block_num) {
put_bits(&s->pb, 1, 1); /* coupling strategy present */
put_bits(&s->pb, 1, 0); /* no coupling strategy */
} else {
put_bits(&s->pb, 1, 0); /* no new coupling strategy */
}
if (s->channel_mode == AC3_CHMODE_STEREO)
{
if(block_num==0)
{
if (s->channel_mode == AC3_CHMODE_STEREO) {
if (!block_num) {
/* first block must define rematrixing (rematstr) */
put_bits(&s->pb, 1, 1);
/* dummy rematrixing rematflg(1:4)=0 */
for (rbnd = 0; rbnd < 4; rbnd++)
put_bits(&s->pb, 1, 0);
}
else
{
} else {
/* no matrixing (but should be used in the future) */
put_bits(&s->pb, 1, 0);
}
}
/* exponent strategy */
for(ch=0;ch<s->fbw_channels;ch++) {
for (ch = 0; ch < s->fbw_channels; ch++)
put_bits(&s->pb, 2, exp_strategy[ch]);
}
if (s->lfe_on) {
if (s->lfe_on)
put_bits(&s->pb, 1, exp_strategy[s->lfe_channel]);
}
/* bandwidth */
for (ch = 0; ch < s->fbw_channels; ch++) {
if (exp_strategy[ch] != EXP_REUSE)
put_bits(&s->pb, 6, s->bandwidth_code[ch]);
@ -911,7 +896,7 @@ static void output_audio_block(AC3EncodeContext *s,
}
/* snr offset */
put_bits(&s->pb, 1, baie); /* always present with bai */
put_bits(&s->pb, 1, baie);
if (baie) {
put_bits(&s->pb, 6, s->coarse_snr_offset);
for (ch = 0; ch < s->channels; ch++) {
@ -1025,32 +1010,14 @@ static void output_audio_block(AC3EncodeContext *s,
q = qmant[ch][i];
b = bap[ch][i];
switch (b) {
case 0:
break;
case 1:
if (q != 128)
put_bits(&s->pb, 5, q);
break;
case 2:
if (q != 128)
put_bits(&s->pb, 7, q);
break;
case 3:
put_bits(&s->pb, 3, q);
break;
case 4:
if (q != 128)
put_bits(&s->pb, 7, q);
break;
case 14:
put_bits(&s->pb, 14, q);
break;
case 15:
put_bits(&s->pb, 16, q);
break;
default:
put_bits(&s->pb, b - 1, q);
break;
case 0: break;
case 1: if (q != 128) put_bits(&s->pb, 5, q); break;
case 2: if (q != 128) put_bits(&s->pb, 7, q); break;
case 3: put_bits(&s->pb, 3, q); break;
case 4: if (q != 128) put_bits(&s->pb, 7, q); break;
case 14: put_bits(&s->pb, 14, q); break;
case 15: put_bits(&s->pb, 16, q); break;
default: put_bits(&s->pb, b-1, q); break;
}
}
}
@ -1094,9 +1061,9 @@ static int log2_tab(int16_t *tab, int n)
int i, v;
v = 0;
for(i=0;i<n;i++) {
for (i = 0; i < n; i++)
v |= abs(tab[i]);
}
return av_log2(v);
}
@ -1105,16 +1072,14 @@ static void lshift_tab(int16_t *tab, int n, int lshift)
int i;
if (lshift > 0) {
for(i=0;i<n;i++) {
for(i = 0; i < n; i++)
tab[i] <<= lshift;
}
} else if (lshift < 0) {
lshift = -lshift;
for(i=0;i<n;i++) {
for (i = 0; i < n; i++)
tab[i] >>= lshift;
}
}
}
/* fill the end of the frame and compute the two crcs */
static int output_frame_end(AC3EncodeContext *s)
@ -1135,8 +1100,10 @@ static int output_frame_end(AC3EncodeContext *s)
/* Now we must compute both crcs : this is not so easy for crc1
because it is at the beginning of the data... */
frame_size_58 = (frame_size >> 1) + (frame_size >> 3);
crc1 = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0,
frame + 4, 2 * frame_size_58 - 4));
/* XXX: could precompute crc_inv */
crc_inv = pow_poly((CRC16_POLY >> 1), (16 * frame_size_58) - 16, CRC16_POLY);
crc1 = mul_poly(crc_inv, crc1, CRC16_POLY);
@ -1147,7 +1114,6 @@ static int output_frame_end(AC3EncodeContext *s)
(frame_size - frame_size_58) * 2 - 2));
AV_WB16(frame + 2*frame_size - 2, crc2);
// printf("n=%d frame_size=%d\n", n, frame_size);
return frame_size * 2;
}
@ -1193,8 +1159,7 @@ static int AC3_encode_frame(AVCodecContext *avctx,
ff_ac3_window[j]) >> 15;
}
/* Normalize the samples to use the maximum available
precision */
/* Normalize the samples to use the maximum available precision */
v = 14 - log2_tab(input_samples, AC3_WINDOW_SIZE);
if (v < 0)
v = 0;
@ -1204,8 +1169,7 @@ static int AC3_encode_frame(AVCodecContext *avctx,
/* do the MDCT */
mdct512(mdct_coef[i][ch], input_samples);
/* compute "exponents". We take into account the
normalization there */
/* compute "exponents". We take into account the normalization there */
for (j = 0; j < AC3_MAX_COEFS; j++) {
int e;
v = abs(mdct_coef[i][ch][j]);
@ -1286,8 +1250,6 @@ static void fft_test(AVLFG *lfg)
int k, n, i;
float sum_re, sum_im, a;
/* FFT test */
for (i = 0; i < FN; i++) {
in[i].re = av_lfg_get(lfg) % 65535 - 32767;
in[i].im = av_lfg_get(lfg) % 65535 - 32767;