[FFmpeg-devel] [PATCH] lavc: add fixed point mp2 encoder

Paul B Mahol onemda at gmail.com
Fri Aug 3 18:49:19 CEST 2012


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---

This just adds mp2float encoder which is what mp2 was.
mp2 encoder is fixed encoder now.
This is like current state with mp2 decoders and ffmpeg -codecs
output is more in touch with reality.

 DEA D    mp2             MP2 (MPEG audio layer 2)
 DEA D    mp2float        MP2 (MPEG audio layer 2)

instead of misleading one:

 DEA D    mp2             MP2 (MPEG audio layer 2)
 D A D    mp2float        MP2 (MPEG audio layer 2)

Picking mp2float instead of mp2 is another problem unrelated
with this patch.

---
 libavcodec/Makefile             |  2 ++
 libavcodec/allcodecs.c          |  2 +-
 libavcodec/mpegaudioenc.c       | 17 +++++++++--------
 libavcodec/mpegaudioenc_float.c | 37 +++++++++++++++++++++++++++++++++++++
 libavcodec/mpegaudiotab.h       |  2 +-
 5 files changed, 50 insertions(+), 10 deletions(-)
 create mode 100644 libavcodec/mpegaudioenc_float.c

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 793a9c8..d786ede 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -278,6 +278,8 @@ OBJS-$(CONFIG_MP2_ENCODER)             += mpegaudioenc.o mpegaudio.o \
                                           mpegaudiodata.o
 OBJS-$(CONFIG_MP2FLOAT_DECODER)        += mpegaudiodec_float.o mpegaudiodecheader.o \
                                           mpegaudio.o mpegaudiodata.o
+OBJS-$(CONFIG_MP2FLOAT_ENCODER)        += mpegaudioenc_float.o mpegaudioenc.o mpegaudio.o \
+                                          mpegaudiodata.o
 OBJS-$(CONFIG_MP3ADU_DECODER)          += mpegaudiodec.o mpegaudiodecheader.o \
                                           mpegaudio.o mpegaudiodata.o
 OBJS-$(CONFIG_MP3ADUFLOAT_DECODER)     += mpegaudiodec_float.o mpegaudiodecheader.o \
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 43c6c16..6b13e17 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -301,7 +301,7 @@ void avcodec_register_all(void)
     REGISTER_DECODER (MP1, mp1);
     REGISTER_DECODER (MP1FLOAT, mp1float);
     REGISTER_ENCDEC  (MP2, mp2);
-    REGISTER_DECODER (MP2FLOAT, mp2float);
+    REGISTER_ENCDEC  (MP2FLOAT, mp2float);
     REGISTER_DECODER (MP3, mp3);
     REGISTER_DECODER (MP3FLOAT, mp3float);
     REGISTER_DECODER (MP3ADU, mp3adu);
diff --git a/libavcodec/mpegaudioenc.c b/libavcodec/mpegaudioenc.c
index ea9e55a..9f9675c 100644
--- a/libavcodec/mpegaudioenc.c
+++ b/libavcodec/mpegaudioenc.c
@@ -58,9 +58,6 @@ typedef struct MpegAudioContext {
     const unsigned char *alloc_table;
 } MpegAudioContext;
 
-/* define it to use floats in quantization (I don't like floats !) */
-#define USE_FLOATS
-
 #include "mpegaudiodata.h"
 #include "mpegaudiotab.h"
 
@@ -149,7 +146,7 @@ static av_cold int MPA_encode_init(AVCodecContext *avctx)
         if (v <= 0)
             v = 1;
         scale_factor_table[i] = v;
-#ifdef USE_FLOATS
+#if CONFIG_FLOAT
         scale_factor_inv_table[i] = pow(2.0, -(3 - i) / 3.0) / (float)(1 << 20);
 #else
 #define P 15
@@ -677,7 +674,7 @@ static void encode_frame(MpegAudioContext *s,
                         for(m=0;m<3;m++) {
                             sample = s->sb_samples[ch][k][l + m][i];
                             /* divide by scale factor */
-#ifdef USE_FLOATS
+#if CONFIG_FLOAT
                             {
                                 float a;
                                 a = (float)sample * scale_factor_inv_table[s->scale_factors[ch][i][k]];
@@ -779,6 +776,11 @@ static const AVCodecDefault mp2_defaults[] = {
     { NULL },
 };
 
+static const int mp2_sample_rates[] = {
+    44100, 48000,  32000, 22050, 24000, 16000, 0
+};
+
+#if !CONFIG_FLOAT
 AVCodec ff_mp2_encoder = {
     .name                  = "mp2",
     .type                  = AVMEDIA_TYPE_AUDIO,
@@ -789,9 +791,8 @@ AVCodec ff_mp2_encoder = {
     .close                 = MPA_encode_close,
     .sample_fmts           = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
                                                             AV_SAMPLE_FMT_NONE },
-    .supported_samplerates = (const int[]){
-        44100, 48000,  32000, 22050, 24000, 16000, 0
-    },
+    .supported_samplerates = mp2_sample_rates,
     .long_name             = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
     .defaults              = mp2_defaults,
 };
+#endif
diff --git a/libavcodec/mpegaudioenc_float.c b/libavcodec/mpegaudioenc_float.c
new file mode 100644
index 0000000..834504c
--- /dev/null
+++ b/libavcodec/mpegaudioenc_float.c
@@ -0,0 +1,37 @@
+/*
+ * Floating point MPEG Audio layer 2 encoder
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg 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.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#define CONFIG_FLOAT 1
+#include "mpegaudioenc.c"
+
+AVCodec ff_mp2float_encoder = {
+    .name                  = "mp2float",
+    .type                  = AVMEDIA_TYPE_AUDIO,
+    .id                    = CODEC_ID_MP2,
+    .priv_data_size        = sizeof(MpegAudioContext),
+    .init                  = MPA_encode_init,
+    .encode2               = MPA_encode_frame,
+    .close                 = MPA_encode_close,
+    .sample_fmts           = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
+                                                            AV_SAMPLE_FMT_NONE },
+    .supported_samplerates = mp2_sample_rates,
+    .long_name             = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
+    .defaults              = mp2_defaults,
+};
diff --git a/libavcodec/mpegaudiotab.h b/libavcodec/mpegaudiotab.h
index 35129e6..7cc8da6 100644
--- a/libavcodec/mpegaudiotab.h
+++ b/libavcodec/mpegaudiotab.h
@@ -82,7 +82,7 @@ static const int bitinv32[32] = {
 static int16_t filter_bank[512];
 
 static int scale_factor_table[64];
-#ifdef USE_FLOATS
+#if CONFIG_FLOAT
 static float scale_factor_inv_table[64];
 #else
 static int8_t scale_factor_shift[64];
-- 
1.7.11.2



More information about the ffmpeg-devel mailing list