---
libavcodec/Makefile | 1 +
libavcodec/ac3enc.c | 121 ++++++++++++++++++++++++++-------------------------
2 files changed, 62 insertions(+), 60 deletions(-)
diff --git libavcodec/Makefile libavcodec/Makefile
index 04fe935..47166bb 100644
--- libavcodec/Makefile
+++ libavcodec/Makefile
@@ -638,6 +638,7 @@ SKIPHEADERS += mpegaudio3.h
EXAMPLES = api
TESTPROGS = cabac dct eval fft h264 iirfilter rangecoder snow
+TESTPROGS-$(CONFIG_AC3_ENCODER) = ac3enc
TESTPROGS-$(HAVE_MMX) += motion
TESTOBJS = dctref.o
diff --git libavcodec/ac3enc.c libavcodec/ac3enc.c
index 352ff82..d923f7e 100644
--- libavcodec/ac3enc.c
+++ libavcodec/ac3enc.c
@@ -23,8 +23,10 @@
* @file
* The simplest AC-3 encoder.
*/
+
//#define DEBUG
//#define DEBUG_BITALLOC
+
#include "libavutil/crc.h"
#include "avcodec.h"
#include "libavutil/common.h" /* for av_reverse */
@@ -33,6 +35,7 @@
#include "audioconvert.h"
typedef struct AC3EncodeContext {
+ AVCodecContext *avctx;
PutBitContext pb;
int bitstream_id;
@@ -110,6 +113,18 @@ static av_cold void fft_init(int ln)
}
}
+static av_cold void mdct_init(void)
+{
+ int i;
+
+ fft_init(MDCT_NBITS - 2);
+ for(i=0;i<MDCT_SAMPLES/4;i++) {
+ float alpha = 2 * M_PI * (i + 1.0 / 8.0) / (float)MDCT_SAMPLES;
+ xcos1[i] = fix15(-cos(alpha));
+ xsin1[i] = fix15(-sin(alpha));
+ }
+}
+
/* butter fly op */
#define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \
{\
@@ -357,12 +372,13 @@ static int encode_exp(uint8_t encoded_exp[AC3_MAX_COEFS],
k += group_size;
}
-#if defined(DEBUG)
- av_log(NULL, AV_LOG_DEBUG, "exponents: strategy=%d\n", exp_strategy);
+#ifdef DEBUG
+ dprintf(NULL, "exponents: strategy=%s\n",
+ exp_strategy==EXP_D15?"D15":exp_strategy==EXP_D25?"D25":"D45");
for(i=0;i<=nb_groups * group_size;i++) {
- av_log(NULL, AV_LOG_DEBUG, "%d ", encoded_exp[i]);
+ dprintf(NULL, "%d ", encoded_exp[i]);
}
- av_log(NULL, AV_LOG_DEBUG, "\n");
+ dprintf(NULL, "\n");
#endif
return 4 + (nb_groups / 3) * 7;
@@ -592,18 +608,17 @@ static int compute_bit_allocation(AC3EncodeContext *s,
s->coarse_snr_offset = coarse_snr_offset;
for(ch=0;ch<s->channels;ch++)
s->fine_snr_offset[ch] = fine_snr_offset;
-#if defined(DEBUG_BITALLOC)
+#ifdef DEBUG_BITALLOC
{
- int j;
+ int blk;
- for(i=0;i<6;i++) {
+ for(blk=0;blk<AC3_MAX_BLOCKS;i++) {
for(ch=0;ch<s->channels;ch++) {
- printf("Block #%d Ch%d:\n", i, ch);
- printf("bap=");
- for(j=0;j<s->nb_coefs[ch];j++) {
- printf("%d ",bap[i][ch][j]);
+ dprintf(s->avctx, "bap[blk=%d][ch=%d]=", blk, ch);
+ for(i=0;i<s->nb_coefs[ch];i++) {
+ dprintf(s->avctx, "%d ",bap[blk][ch][i]);
}
- printf("\n");
+ dprintf(s->avctx, "\n");
}
}
}
@@ -661,9 +676,10 @@ static av_cold int AC3_encode_init(AVCodecContext *avctx)
int bitrate = avctx->bit_rate;
AC3EncodeContext *s = avctx->priv_data;
int i, j, ch;
- float alpha;
int bw_code;
+ s->avctx = avctx;
+
avctx->frame_size = AC3_FRAME_SIZE;
ac3_common_init();
@@ -729,13 +745,7 @@ static av_cold int AC3_encode_init(AVCodecContext *avctx)
/* initial snr offset */
s->coarse_snr_offset = 40;
- /* mdct init */
- fft_init(MDCT_NBITS - 2);
- for(i=0;i<MDCT_SAMPLES/4;i++) {
- alpha = 2 * M_PI * (i + 1.0 / 8.0) / (float)MDCT_SAMPLES;
- xcos1[i] = fix15(-cos(alpha));
- xsin1[i] = fix15(-sin(alpha));
- }
+ mdct_init();
avctx->coded_frame= avcodec_alloc_frame();
avctx->coded_frame->key_frame= 1;
@@ -860,12 +870,6 @@ static void output_audio_block(AC3EncodeContext *s,
}
}
-#if defined(DEBUG)
- {
- static int count = 0;
- av_log(NULL, AV_LOG_DEBUG, "Block #%d (%d)\n", block_num, count++);
- }
-#endif
/* exponent strategy */
for(ch=0;ch<s->fbw_channels;ch++) {
put_bits(&s->pb, 2, exp_strategy[ch]);
@@ -1301,14 +1305,15 @@ static av_cold int AC3_encode_close(AVCodecContext *avctx)
return 0;
}
-#if 0
+#ifdef TEST
/*************************************************************************/
/* TEST */
-#undef random
-#define FN (N/4)
+#include "libavutil/lfg.h"
-void fft_test(void)
+#define FN (MDCT_SAMPLES/4)
+
+static void fft_test(AVLFG *lfg)
{
IComplex in[FN], in1[FN];
int k, n, i;
@@ -1317,8 +1322,8 @@ void fft_test(void)
/* FFT test */
for(i=0;i<FN;i++) {
- in[i].re = random() % 65535 - 32767;
- in[i].im = random() % 65535 - 32767;
+ in[i].re = av_lfg_get(lfg) % 65535 - 32767;
+ in[i].im = av_lfg_get(lfg) % 65535 - 32767;
in1[i] = in[i];
}
fft(in, 7);
@@ -1332,67 +1337,63 @@ void fft_test(void)
sum_re += in1[n].re * cos(a) - in1[n].im * sin(a);
sum_im += in1[n].re * sin(a) + in1[n].im * cos(a);
}
- printf("%3d: %6d,%6d %6.0f,%6.0f\n",
+ av_log(NULL, AV_LOG_DEBUG, "%3d: %6d,%6d %6.0f,%6.0f\n",
k, in[k].re, in[k].im, sum_re / FN, sum_im / FN);
}
}
-void mdct_test(void)
+static void mdct_test(AVLFG *lfg)
{
- int16_t input[N];
- int32_t output[N/2];
- float input1[N];
- float output1[N/2];
+ int16_t input[MDCT_SAMPLES];
+ int32_t output[AC3_MAX_COEFS];
+ float input1[MDCT_SAMPLES];
+ float output1[AC3_MAX_COEFS];
float s, a, err, e, emax;
int i, k, n;
- for(i=0;i<N;i++) {
- input[i] = (random() % 65535 - 32767) * 9 / 10;
+ for(i=0;i<MDCT_SAMPLES;i++) {
+ input[i] = (av_lfg_get(lfg) % 65535 - 32767) * 9 / 10;
input1[i] = input[i];
}
mdct512(output, input);
/* do it by hand */
- for(k=0;k<N/2;k++) {
+ for(k=0;k<AC3_MAX_COEFS;k++) {
s = 0;
- for(n=0;n<N;n++) {
- a = (2*M_PI*(2*n+1+N/2)*(2*k+1) / (4 * N));
+ for(n=0;n<MDCT_SAMPLES;n++) {
+ a = (2*M_PI*(2*n+1+MDCT_SAMPLES/2)*(2*k+1) / (4 * MDCT_SAMPLES));
s += input1[n] * cos(a);
}
- output1[k] = -2 * s / N;
+ output1[k] = -2 * s / MDCT_SAMPLES;
}
err = 0;
emax = 0;
- for(i=0;i<N/2;i++) {
- printf("%3d: %7d %7.0f\n", i, output[i], output1[i]);
+ for(i=0;i<AC3_MAX_COEFS;i++) {
+ av_log(NULL, AV_LOG_DEBUG, "%3d: %7d %7.0f\n", i, output[i], output1[i]);
e = output[i] - output1[i];
if (e > emax)
emax = e;
err += e * e;
}
- printf("err2=%f emax=%f\n", err / (N/2), emax);
+ av_log(NULL, AV_LOG_DEBUG, "err2=%f emax=%f\n", err / AC3_MAX_COEFS, emax);
}
-void test_ac3(void)
+int main(void)
{
- AC3EncodeContext ctx;
- unsigned char frame[AC3_MAX_CODED_FRAME_SIZE];
- int16_t samples[AC3_FRAME_SIZE];
- int ret, i;
+ AVLFG lfg;
- AC3_encode_init(&ctx, 44100, 64000, 1);
+ av_log_set_level(AV_LOG_DEBUG);
+ mdct_init();
+ av_lfg_init(&lfg, 0xdeadbeef);
- fft_test();
- mdct_test();
+ fft_test(&lfg);
+ mdct_test(&lfg);
- for(i=0;i<AC3_FRAME_SIZE;i++)
- samples[i] = (int)(sin(2*M_PI*i*1000.0/44100) * 10000);
- ret = AC3_encode_frame(&ctx, frame, samples);
- printf("ret=%d\n", ret);
+ return 0;
}
-#endif
+#endif /* TEST */
AVCodec ac3_encoder = {
"ac3",
_______________________________________________
FFmpeg-soc mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-soc