/*
* MPEG Audio decoder
- * Copyright (c) 2001 Gerard Lantau.
+ * Copyright (c) 2001, 2002 Fabrice Bellard.
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
*
- * This program is distributed in the hope that it will be useful,
+ * This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
*
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
//#define DEBUG
#include "avcodec.h"
-#include <math.h>
#include "mpegaudio.h"
/*
/* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg
audio decoder */
-//#define USE_HIGHPRECISION
+#ifdef CONFIG_MPEGAUDIO_HP
+#define USE_HIGHPRECISION
+#endif
#ifdef USE_HIGHPRECISION
#define FRAC_BITS 23 /* fractional bits for sb_samples and dct */
static UINT16 band_index_long[9][23];
/* XXX: free when all decoders are closed */
#define TABLE_4_3_SIZE (8191 + 16)
-static UINT8 *table_4_3_exp;
+static INT8 *table_4_3_exp;
#if FRAC_BITS <= 15
static UINT16 *table_4_3_value;
#else
{ FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
static INT32 scale_factor_mult2[3][3] = {
- SCALE_GEN(1.0 / 3.0), /* 3 steps */
- SCALE_GEN(1.0 / 5.0), /* 5 steps */
- SCALE_GEN(1.0 / 9.0), /* 9 steps */
+ SCALE_GEN(4.0 / 3.0), /* 3 steps */
+ SCALE_GEN(4.0 / 5.0), /* 5 steps */
+ SCALE_GEN(4.0 / 9.0), /* 9 steps */
};
/* 2^(n/4) */
shift >>= 2;
val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
shift += n;
- return (int)((val + (1 << (shift - 1))) >> shift);
+ /* NOTE: at this point, 1 <= shift >= 21 + 15 */
+ return (int)((val + (1LL << (shift - 1))) >> shift);
}
static inline int l2_unscale_group(int steps, int mant, int scale_factor)
shift = scale_factor_modshift[scale_factor];
mod = shift & 3;
shift >>= 2;
- /* XXX: store the result directly */
- val = (2 * (mant - (steps >> 1))) * scale_factor_mult2[steps >> 2][mod];
- return (val + (1 << (shift - 1))) >> shift;
+
+ val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
+ /* NOTE: at this point, 0 <= shift <= 21 */
+ if (shift > 0)
+ val = (val + (1 << (shift - 1))) >> shift;
+ return val;
}
/* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
#endif
}
+/* all integer n^(4/3) computation code */
+#define DEV_ORDER 13
+
+#define POW_FRAC_BITS 24
+#define POW_FRAC_ONE (1 << POW_FRAC_BITS)
+#define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
+#define POW_MULL(a,b) (((INT64)(a) * (INT64)(b)) >> POW_FRAC_BITS)
+
+static int dev_4_3_coefs[DEV_ORDER];
+
+static int pow_mult3[3] = {
+ POW_FIX(1.0),
+ POW_FIX(1.25992104989487316476),
+ POW_FIX(1.58740105196819947474),
+};
+
+static void int_pow_init(void)
+{
+ int i, a;
+
+ a = POW_FIX(1.0);
+ for(i=0;i<DEV_ORDER;i++) {
+ a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
+ dev_4_3_coefs[i] = a;
+ }
+}
+
+/* return the mantissa and the binary exponent */
+static int int_pow(int i, int *exp_ptr)
+{
+ int e, er, eq, j;
+ int a, a1;
+
+ /* renormalize */
+ a = i;
+ e = POW_FRAC_BITS;
+ while (a < (1 << (POW_FRAC_BITS - 1))) {
+ a = a << 1;
+ e--;
+ }
+ a -= (1 << POW_FRAC_BITS);
+ a1 = 0;
+ for(j = DEV_ORDER - 1; j >= 0; j--)
+ a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
+ a = (1 << POW_FRAC_BITS) + a1;
+ /* exponent compute (exact) */
+ e = e * 4;
+ er = e % 3;
+ eq = e / 3;
+ a = POW_MULL(a, pow_mult3[er]);
+ while (a >= 2 * POW_FRAC_ONE) {
+ a = a >> 1;
+ eq++;
+ }
+ /* convert to float */
+ while (a < POW_FRAC_ONE) {
+ a = a << 1;
+ eq--;
+ }
+ /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
+#if POW_FRAC_BITS > FRAC_BITS
+ a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
+ /* correct overflow */
+ if (a >= 2 * (1 << FRAC_BITS)) {
+ a = a >> 1;
+ eq++;
+ }
+#endif
+ *exp_ptr = eq;
+ return a;
+}
static int decode_init(AVCodecContext * avctx)
{
MPADecodeContext *s = avctx->priv_data;
- static int init;
+ static int init=0;
int i, j, k;
if(!init) {
for(i=0;i<64;i++) {
int shift, mod;
/* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
- shift = (i / 3) - 1;
+ shift = (i / 3);
mod = i % 3;
-#if FRAC_BITS <= 15
- if (shift > 31)
- shift = 31;
-#endif
scale_factor_modshift[i] = mod | (shift << 2);
}
int n, norm;
n = i + 2;
norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
- scale_factor_mult[i][0] = MULL(FIXR(1.0), norm);
- scale_factor_mult[i][1] = MULL(FIXR(0.7937005259), norm);
- scale_factor_mult[i][2] = MULL(FIXR(0.6299605249), norm);
+ scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm);
+ scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm);
+ scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm);
dprintf("%d: norm=%x s=%x %x %x\n",
i, norm,
scale_factor_mult[i][0],
band_index_long[i][22] = k;
}
- /* compute n ^ (4/3) and store it in mantissa/exp format */
- table_4_3_exp = av_mallocz(TABLE_4_3_SIZE *
- sizeof(table_4_3_exp[0]));
- if (!table_4_3_exp)
- return -1;
- table_4_3_value = av_mallocz(TABLE_4_3_SIZE *
- sizeof(table_4_3_value[0]));
- if (!table_4_3_value) {
- free(table_4_3_exp);
+ /* compute n ^ (4/3) and store it in mantissa/exp format */
+ if (!av_mallocz_static(&table_4_3_exp,
+ TABLE_4_3_SIZE * sizeof(table_4_3_exp[0])))
+ return -1;
+ if (!av_mallocz_static(&table_4_3_value,
+ TABLE_4_3_SIZE * sizeof(table_4_3_value[0])))
return -1;
- }
+ int_pow_init();
for(i=1;i<TABLE_4_3_SIZE;i++) {
- double f, fm;
int e, m;
- f = pow((double)i, 4.0 / 3.0);
- fm = frexp(f, &e);
- m = FIXR(2 * fm);
+ m = int_pow(i, &e);
+#if 0
+ /* test code */
+ {
+ double f, fm;
+ int e1, m1;
+ f = pow((double)i, 4.0 / 3.0);
+ fm = frexp(f, &e1);
+ m1 = FIXR(2 * fm);
#if FRAC_BITS <= 15
- if ((unsigned short)m != m)
- m = 65535;
+ if ((unsigned short)m1 != m1) {
+ m1 = m1 >> 1;
+ e1++;
+ }
+#endif
+ e1--;
+ if (m != m1 || e != e1) {
+ printf("%4d: m=%x m1=%x e=%d e1=%d\n",
+ i, m, m1, e, e1);
+ }
+ }
#endif
/* normalized to FRAC_BITS */
table_4_3_value[i] = m;
- table_4_3_exp[i] = e - 1;
+ table_4_3_exp[i] = e;
}
-
for(i=0;i<7;i++) {
float f;
return 0;
}
-/* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */;
+/* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
/* cos(i*pi/64) */
for(j=0;j<32;j++) {
v = tmp[j];
#if FRAC_BITS <= 15
+ /* NOTE: can cause a loss in precision if very high amplitude
+ sound */
if (v > 32767)
v = 32767;
else if (v < -32768)
/* extract frequency */
sample_rate_index = (header >> 10) & 3;
sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
- if (sample_rate == 0)
- return 1;
sample_rate_index += 3 * (s->lsf + mpeg25);
s->sample_rate_index = sample_rate_index;
s->error_protection = ((header >> 16) & 1) ^ 1;
+ s->sample_rate = sample_rate;
bitrate_index = (header >> 12) & 0xf;
padding = (header >> 9) & 1;
break;
}
}
- s->sample_rate = sample_rate;
-#ifdef DEBUG
+#if defined(DEBUG)
printf("layer%d, %d Hz, %d kbits/s, ",
s->layer, s->sample_rate, s->bit_rate);
if (s->nb_channels == 2) {
UINT8 *ptr;
/* compute current position in stream */
-#ifdef ALT_BITSTREAM_READER
- ptr = s->gb.buffer + (s->gb.index>>3);
-#else
- ptr = s->gb.buf_ptr - (s->gb.bit_cnt >> 3);
-#endif
+ ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
+
/* copy old data before current one */
ptr -= backstep;
memcpy(ptr, s->inbuf1[s->inbuf_index ^ 1] +
BACKSTEP_SIZE + s->old_frame_size - backstep, backstep);
/* init get bits again */
- init_get_bits(&s->gb, ptr, s->frame_size + backstep);
+ init_get_bits(&s->gb, ptr, (s->frame_size + backstep)*8);
/* prepare next buffer */
s->inbuf_index ^= 1;
{
int s_index;
int linbits, code, x, y, l, v, i, j, k, pos;
- UINT8 *last_buf_ptr;
- UINT32 last_bit_buf;
- int last_bit_cnt;
+ GetBitContext last_gb;
VLC *vlc;
UINT8 *code_table;
/* high frequencies */
vlc = &huff_quad_vlc[g->count1table_select];
- last_buf_ptr = NULL;
- last_bit_buf = 0;
- last_bit_cnt = 0;
+ last_gb.buffer = NULL;
while (s_index <= 572) {
pos = get_bits_count(&s->gb);
if (pos >= end_pos) {
- if (pos > end_pos && last_buf_ptr != NULL) {
+ if (pos > end_pos && last_gb.buffer != NULL) {
/* some encoders generate an incorrect size for this
part. We must go back into the data */
s_index -= 4;
-#ifdef ALT_BITSTREAM_READER
- s->gb.buffer = last_buf_ptr;
- s->gb.index = last_bit_cnt;
-#else
- s->gb.buf_ptr = last_buf_ptr;
- s->gb.bit_buf = last_bit_buf;
- s->gb.bit_cnt = last_bit_cnt;
-#endif
+ s->gb = last_gb;
}
break;
}
-#ifdef ALT_BITSTREAM_READER
- last_buf_ptr = s->gb.buffer;
- last_bit_cnt = s->gb.index;
-#else
- last_buf_ptr = s->gb.buf_ptr;
- last_bit_buf = s->gb.bit_buf;
- last_bit_cnt = s->gb.bit_cnt;
-#endif
-
+ last_gb= s->gb;
+
code = get_vlc(&s->gb, vlc);
dprintf("t=%d code=%d\n", g->count1table_select, code);
if (code < 0)
}
}
-#ifdef DEBUG
+#if defined(DEBUG)
void sample_dump(int fnum, INT32 *tab, int n)
{
static FILE *files[16], *f;
char buf[512];
-
+ int i;
+ INT32 v;
+
f = files[fnum];
if (!f) {
- sprintf(buf, "/tmp/out%d.pcm", fnum);
+ sprintf(buf, "/tmp/out%d.%s.pcm",
+ fnum,
+#ifdef USE_HIGHPRECISION
+ "hp"
+#else
+ "lp"
+#endif
+ );
f = fopen(buf, "w");
if (!f)
return;
}
if (fnum == 0) {
- int i;
static int pos = 0;
printf("pos=%d\n", pos);
for(i=0;i<n;i++) {
- printf(" %f", (double)tab[i] / 32768.0);
+ printf(" %0.4f", (double)tab[i] / FRAC_ONE);
if ((i % 18) == 17)
printf("\n");
}
pos += n;
}
-
- fwrite(tab, 1, n * sizeof(INT32), f);
+ for(i=0;i<n;i++) {
+ /* normalize to 23 frac bits */
+ v = tab[i] << (23 - FRAC_BITS);
+ fwrite(&v, 1, sizeof(INT32), f);
+ }
}
#endif
}
g->scale_factors[j++] = 0;
}
-#ifdef DEBUG
+#if defined(DEBUG)
{
printf("scfsi=%x gr=%d ch=%d scale_factors:\n",
g->scfsi, gr, ch);
/* XXX: should compute exact size */
for(;j<40;j++)
g->scale_factors[j] = 0;
-#ifdef DEBUG
+#if defined(DEBUG)
{
printf("gr=%d ch=%d scale_factors:\n",
gr, ch);
if (huffman_decode(s, g, exponents,
bits_pos + g->part2_3_length) < 0)
return -1;
-#if defined(DEBUG) && 0
- sample_dump(3, g->sb_hybrid, 576);
+#if defined(DEBUG)
+ sample_dump(0, g->sb_hybrid, 576);
#endif
/* skip extension bits */
g = &granules[ch][gr];
reorder_block(s, g);
-#ifdef DEBUG
+#if defined(DEBUG)
sample_dump(0, g->sb_hybrid, 576);
#endif
compute_antialias(s, g);
-#ifdef DEBUG
+#if defined(DEBUG)
sample_dump(1, g->sb_hybrid, 576);
#endif
compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
-#ifdef DEBUG
+#if defined(DEBUG)
sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
#endif
}
short *samples_ptr;
init_get_bits(&s->gb, s->inbuf + HEADER_SIZE,
- s->inbuf_ptr - s->inbuf - HEADER_SIZE);
+ (s->inbuf_ptr - s->inbuf - HEADER_SIZE)*8);
/* skip error protection field */
if (s->error_protection)
s->free_format_frame_size = 0;
} else {
if (decode_header(s, header) == 1) {
- /* free format: compute frame size */
+ /* free format: prepare to compute frame size */
s->frame_size = -1;
- memcpy(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
- s->inbuf_ptr--;
- } else {
- /* update codec info */
- avctx->sample_rate = s->sample_rate;
- avctx->channels = s->nb_channels;
- avctx->bit_rate = s->bit_rate;
}
+ /* update codec info */
+ avctx->sample_rate = s->sample_rate;
+ avctx->channels = s->nb_channels;
+ avctx->bit_rate = s->bit_rate;
+ avctx->frame_size = s->frame_size;
}
}
} else if (s->frame_size == -1) {
if (len > buf_size)
len = buf_size;
if (len == 0) {
- /* frame too long: resync */
+ /* frame too long: resync */
s->frame_size = 0;
+ memcpy(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
+ s->inbuf_ptr--;
} else {
UINT8 *p, *pend;
UINT32 header1;
len = s->frame_size - len;
if (len > buf_size)
len = buf_size;
- else if (len < 4)
- len = buf_size > 4 ? 4 : buf_size;
memcpy(s->inbuf_ptr, buf_ptr, len);
buf_ptr += len;
s->inbuf_ptr += len;
break;
}
next_data:
+ ;
}
return buf_ptr - buf;
}
-AVCodec mp3_decoder =
+AVCodec mp2_decoder =
{
- "mpegaudio",
+ "mp2",
CODEC_TYPE_AUDIO,
CODEC_ID_MP2,
sizeof(MPADecodeContext),
NULL,
decode_frame,
};
+
+AVCodec mp3_decoder =
+{
+ "mp3",
+ CODEC_TYPE_AUDIO,
+ CODEC_ID_MP3LAME,
+ sizeof(MPADecodeContext),
+ decode_init,
+ NULL,
+ NULL,
+ decode_frame,
+};
+
+#undef C1
+#undef C2
+#undef C3
+#undef C4
+#undef C5
+#undef C6
+#undef C7
+#undef C8
+#undef FRAC_BITS
+#undef HEADER_SIZE