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

Paul B Mahol onemda at gmail.com
Fri Aug 3 21:28:07 CEST 2012


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 libavcodec/Makefile             |  2 ++
 libavcodec/allcodecs.c          |  2 +-
 libavcodec/mpegaudioenc.c       | 17 +++++++++--------
 libavcodec/mpegaudioenc_float.c | 37 +++++++++++++++++++++++++++++++++++++
 libavcodec/mpegaudiotab.h       |  2 +-
 tests/fate/acodec.mak           |  4 ++++
 tests/ref/fate/acodec-mp2       |  6 +++---
 tests/ref/fate/acodec-mp2float  |  4 ++++
 8 files changed, 61 insertions(+), 13 deletions(-)
 create mode 100644 libavcodec/mpegaudioenc_float.c
 create mode 100644 tests/ref/fate/acodec-mp2float

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];
diff --git a/tests/fate/acodec.mak b/tests/fate/acodec.mak
index 31f3441..bb4f165 100644
--- a/tests/fate/acodec.mak
+++ b/tests/fate/acodec.mak
@@ -36,6 +36,10 @@ FATE_ACODEC += fate-acodec-mp2
 fate-acodec-mp2: FMT = mp2
 fate-acodec-mp2: CMP_SHIFT = -1924
 
+FATE_ACODEC += fate-acodec-mp2float
+fate-acodec-mp2float: FMT = mp2
+fate-acodec-mp2float: CMP_SHIFT = -1924
+
 FATE_ACODEC += fate-acodec-alac
 fate-acodec-alac: FMT = mov
 fate-acodec-alac: CODEC = alac -compression_level 1
diff --git a/tests/ref/fate/acodec-mp2 b/tests/ref/fate/acodec-mp2
index 42381b4..1eb3f01 100644
--- a/tests/ref/fate/acodec-mp2
+++ b/tests/ref/fate/acodec-mp2
@@ -1,4 +1,4 @@
-f6eb0a205350bbd7fb1028a01c7ae8aa *tests/data/fate/acodec-mp2.mp2
+28fbc7485c7939f40368f79adccb3e3d *tests/data/fate/acodec-mp2.mp2
 96130 tests/data/fate/acodec-mp2.mp2
-5a669ca7321adc6ab66a3eade4035909 *tests/data/fate/acodec-mp2.out.wav
-stddev: 4384.33 PSNR: 23.49 MAXDIFF:52631 bytes:  1058400/  1057916
+87461bd4ce4b0e0cbbf6c43621baf261 *tests/data/fate/acodec-mp2.out.wav
+stddev: 4384.26 PSNR: 23.49 MAXDIFF:52632 bytes:  1058400/  1057916
diff --git a/tests/ref/fate/acodec-mp2float b/tests/ref/fate/acodec-mp2float
new file mode 100644
index 0000000..4c3b2b6
--- /dev/null
+++ b/tests/ref/fate/acodec-mp2float
@@ -0,0 +1,4 @@
+f6eb0a205350bbd7fb1028a01c7ae8aa *tests/data/fate/acodec-mp2float.mp2
+96130 tests/data/fate/acodec-mp2float.mp2
+5a669ca7321adc6ab66a3eade4035909 *tests/data/fate/acodec-mp2float.out.wav
+stddev: 4384.33 PSNR: 23.49 MAXDIFF:52631 bytes:  1058400/  1057916
-- 
1.7.11.2



More information about the ffmpeg-devel mailing list