]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/mpegaudio.c
ogg has to be at the right of vorbis patch by ("VĂ­ctor Paesa" wzrlpy hotmail com)
[ffmpeg] / libavcodec / mpegaudio.c
index af05e292792e0bf5b45df7fcf83f23f815e89d57..d521acc588c99df03a1bac48861e1183f86a06b3 100644 (file)
@@ -1,48 +1,73 @@
 /*
  * The simplest mpeg audio layer 2 encoder
- * Copyright (c) 2000 Gerard Lantau.
+ * Copyright (c) 2000, 2001 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
  */
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <math.h>
+/**
+ * @file mpegaudio.c
+ * The simplest mpeg audio layer 2 encoder.
+ */
 #include "avcodec.h"
+#include "bitstream.h"
 #include "mpegaudio.h"
 
-#define NDEBUG
-#include <assert.h>
+/* currently, cannot change these constants (need to modify
+   quantization stage) */
+#define FRAC_BITS 15
+#define WFRAC_BITS  14
+#define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
+#define FIX(a)   ((int)((a) * (1 << FRAC_BITS)))
+
+#define SAMPLES_BUF_SIZE 4096
+
+typedef struct MpegAudioContext {
+    PutBitContext pb;
+    int nb_channels;
+    int freq, bit_rate;
+    int lsf;           /* 1 if mpeg2 low bitrate selected */
+    int bitrate_index; /* bit rate */
+    int freq_index;
+    int frame_size; /* frame size, in bits, without padding */
+    int64_t nb_samples; /* total number of samples encoded */
+    /* padding computation */
+    int frame_frac, frame_frac_incr, do_padding;
+    short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]; /* buffer for filter */
+    int samples_offset[MPA_MAX_CHANNELS];       /* offset in samples_buf */
+    int sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT];
+    unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]; /* scale factors */
+    /* code to group 3 scale factors */
+    unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];       
+    int sblimit; /* number of used subbands */
+    const unsigned char *alloc_table;
+} MpegAudioContext;
 
 /* define it to use floats in quantization (I don't like floats !) */
 //#define USE_FLOATS
 
-#define MPA_STEREO  0
-#define MPA_JSTEREO 1
-#define MPA_DUAL    2
-#define MPA_MONO    3
-
 #include "mpegaudiotab.h"
 
-int MPA_encode_init(AVCodecContext *avctx)
+static int MPA_encode_init(AVCodecContext *avctx)
 {
     MpegAudioContext *s = avctx->priv_data;
     int freq = avctx->sample_rate;
     int bitrate = avctx->bit_rate;
     int channels = avctx->channels;
-    int i, v, table, ch_bitrate;
+    int i, v, table;
     float a;
 
     if (channels > 2)
@@ -52,29 +77,32 @@ int MPA_encode_init(AVCodecContext *avctx)
     s->freq = freq;
     s->bit_rate = bitrate * 1000;
     avctx->frame_size = MPA_FRAME_SIZE;
-    avctx->key_frame = 1; /* always key frame */
 
     /* encoding freq */
     s->lsf = 0;
     for(i=0;i<3;i++) {
-        if (freq_tab[i] == freq) 
+        if (mpa_freq_tab[i] == freq) 
             break;
-        if ((freq_tab[i] / 2) == freq) {
+        if ((mpa_freq_tab[i] / 2) == freq) {
             s->lsf = 1;
             break;
         }
     }
-    if (i == 3)
+    if (i == 3){
+        av_log(avctx, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", freq);
         return -1;
+    }
     s->freq_index = i;
 
     /* encoding bitrate & frequency */
     for(i=0;i<15;i++) {
-        if (bitrate_tab[1-s->lsf][i] == bitrate) 
+        if (mpa_bitrate_tab[s->lsf][1][i] == bitrate) 
             break;
     }
-    if (i == 15)
+    if (i == 15){
+        av_log(avctx, AV_LOG_ERROR, "bitrate %d is not allowed in mp2\n", bitrate);
         return -1;
+    }
     s->bitrate_index = i;
 
     /* compute total header size & pad bit */
@@ -87,36 +115,33 @@ int MPA_encode_init(AVCodecContext *avctx)
     s->frame_frac_incr = (int)((a - floor(a)) * 65536.0);
     
     /* select the right allocation table */
-    ch_bitrate = bitrate / s->nb_channels;
-    if (!s->lsf) {
-        if ((freq == 48000 && ch_bitrate >= 56) ||
-            (ch_bitrate >= 56 && ch_bitrate <= 80)) 
-            table = 0;
-        else if (freq != 48000 && ch_bitrate >= 96) 
-            table = 1;
-        else if (freq != 32000 && ch_bitrate <= 48) 
-            table = 2;
-        else 
-            table = 3;
-    } else {
-        table = 4;
-    }
+    table = l2_select_table(bitrate, s->nb_channels, freq, s->lsf);
+
     /* number of used subbands */
     s->sblimit = sblimit_table[table];
     s->alloc_table = alloc_tables[table];
 
 #ifdef DEBUG
-    printf("%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n", 
+    av_log(avctx, AV_LOG_DEBUG, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n", 
            bitrate, freq, s->frame_size, table, s->frame_frac_incr);
 #endif
 
     for(i=0;i<s->nb_channels;i++)
         s->samples_offset[i] = 0;
 
-    for(i=0;i<512;i++) {
-        float a = enwindow[i] * 32768.0 * 16.0;
-        filter_bank[i] = (int)(a);
+    for(i=0;i<257;i++) {
+        int v;
+        v = mpa_enwindow[i];
+#if WFRAC_BITS != 16
+        v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
+#endif
+        filter_bank[i] = v;
+        if ((i & 63) != 0)
+            v = -v;
+        if (i != 0)
+            filter_bank[512 - i] = v;
     }
+
     for(i=0;i<64;i++) {
         v = (int)(pow(2.0, (3 - i) / 3.0) * (1 << 20));
         if (v <= 0)
@@ -154,11 +179,14 @@ int MPA_encode_init(AVCodecContext *avctx)
         total_quant_bits[i] = 12 * v;
     }
 
+    avctx->coded_frame= avcodec_alloc_frame();
+    avctx->coded_frame->key_frame= 1;
+
     return 0;
 }
 
-/* 32 point floating point IDCT */
-static void idct32(int *out, int *tab, int sblimit, int left_shift)
+/* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */
+static void idct32(int *out, int *tab)
 {
     int i, j;
     int *t, *t1, xr;
@@ -273,15 +301,17 @@ static void idct32(int *out, int *tab, int sblimit, int left_shift)
     } while (t >= tab);
 
     for(i=0;i<32;i++) {
-        out[i] = tab[bitinv32[i]] << left_shift;
+        out[i] = tab[bitinv32[i]];
     }
 }
 
+#define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
+
 static void filter(MpegAudioContext *s, int ch, short *samples, int incr)
 {
     short *p, *q;
-    int sum, offset, i, j, norm, n;
-    short tmp[64];
+    int sum, offset, i, j;
+    int tmp[64];
     int tmp1[32];
     int *out;
 
@@ -309,29 +339,15 @@ static void filter(MpegAudioContext *s, int ch, short *samples, int incr)
             sum += p[5*64] * q[5*64];
             sum += p[6*64] * q[6*64];
             sum += p[7*64] * q[7*64];
-            tmp[i] = sum >> 14;
+            tmp[i] = sum;
             p++;
             q++;
         }
-        tmp1[0] = tmp[16];
-        for( i=1; i<=16; i++ ) tmp1[i] = tmp[i+16]+tmp[16-i];
-        for( i=17; i<=31; i++ ) tmp1[i] = tmp[i+16]-tmp[80-i];
-
-        /* integer IDCT 32 with normalization. XXX: There may be some
-           overflow left */
-        norm = 0;
-        for(i=0;i<32;i++) {
-            norm |= abs(tmp1[i]);
-        }
-        n = log2(norm) - 12;
-        if (n > 0) {
-            for(i=0;i<32;i++) 
-                tmp1[i] >>= n;
-        } else {
-            n = 0;
-        }
+        tmp1[0] = tmp[16] >> WSHIFT;
+        for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >> WSHIFT;
+        for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >> WSHIFT;
 
-        idct32(out, tmp1, s->sblimit, n);
+        idct32(out, tmp1);
 
         /* advance of 32 samples */
         offset -= 32;
@@ -370,7 +386,7 @@ static void compute_scale_factors(unsigned char scale_code[SBLIMIT],
             }
             /* compute the scale factor index using log 2 computations */
             if (vmax > 0) {
-                n = log2(vmax);
+                n = av_log2(vmax);
                 /* n is the position of the MSB of vmax. now 
                    use at most 2 compares to find the index */
                 index = (21 - n) * 3 - 3;
@@ -381,9 +397,9 @@ static void compute_scale_factors(unsigned char scale_code[SBLIMIT],
                     index = 0; /* very unlikely case of overflow */
                 }
             } else {
-                index = 63;
+                index = 62; /* value 63 is not allowed */
             }
-            
+
 #if 0
             printf("%2d:%d in=%x %x %d\n", 
                    j, i, vmax, scale_factor_table[index], index);
@@ -451,7 +467,8 @@ static void compute_scale_factors(unsigned char scale_code[SBLIMIT],
             sf[1] = sf[2] = sf[0];
             break;
         default:
-            abort();
+            assert(0); //cant happen
+            code = 0;           /* kill warning */
         }
         
 #if 0
@@ -732,8 +749,8 @@ static void encode_frame(MpegAudioContext *s,
     flush_put_bits(p);
 }
 
-int MPA_encode_frame(AVCodecContext *avctx,
-                     unsigned char *frame, int buf_size, void *data)
+static int MPA_encode_frame(AVCodecContext *avctx,
+                           unsigned char *frame, int buf_size, void *data)
 {
     MpegAudioContext *s = avctx->priv_data;
     short *samples = data;
@@ -754,14 +771,19 @@ int MPA_encode_frame(AVCodecContext *avctx,
     }
     compute_bit_allocation(s, smr, bit_alloc, &padding);
 
-    init_put_bits(&s->pb, frame, MPA_MAX_CODED_FRAME_SIZE, NULL, NULL);
+    init_put_bits(&s->pb, frame, MPA_MAX_CODED_FRAME_SIZE);
 
     encode_frame(s, bit_alloc, padding);
     
     s->nb_samples += MPA_FRAME_SIZE;
-    return s->pb.buf_ptr - s->pb.buf;
+    return pbBufPtr(&s->pb) - s->pb.buf;
 }
 
+static int MPA_encode_close(AVCodecContext *avctx)
+{
+    av_freep(&avctx->coded_frame);
+    return 0;
+}
 
 AVCodec mp2_encoder = {
     "mp2",
@@ -770,5 +792,8 @@ AVCodec mp2_encoder = {
     sizeof(MpegAudioContext),
     MPA_encode_init,
     MPA_encode_frame,
+    MPA_encode_close,
     NULL,
 };
+
+#undef FIX