[FFmpeg-devel] [PATCH 1/4] Add replay_gain and find_peak_sample options to libmp3lame

Giovanni Motta giovanni.motta at gmail.com
Wed May 28 03:19:47 CEST 2014


Fixes ticket #3577

---
 doc/encoders.texi       | 11 +++++++++++
 libavcodec/libmp3lame.c | 29 +++++++++++++++++++++++++----
 2 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/doc/encoders.texi b/doc/encoders.texi
index d48df7d..1d37f37 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -810,6 +810,17 @@ Enable use of bit reservoir when set to 1. Default value is 1. LAME
 has this enabled by default, but can be overridden by use
 @option{--nores} option.

+ at item find_peak_sample (@emph{--find_peak_sample})
+When set to 1, find and store in the Xing header the peak value of the signal.
+Default value is 0. This option is available only if LAME is patched with
+ at url{http://sourceforge.net/p/lame/patches/66/} and configured with
+--enable-decoder.
+
+ at item replay_gain (@emph{--replay_gain})
+When set to 1, find and store in the Xing header the replay gain.
+Default value is 0. This option is available only if LAME is patched with
+ at url{http://sourceforge.net/p/lame/patches/66/}.
+
 @item joint_stereo (@emph{-m j})
 Enable the encoder to use (on a frame by frame basis) either L/R
 stereo or mid/side stereo. Default value is 1.
diff --git a/libavcodec/libmp3lame.c b/libavcodec/libmp3lame.c
index a8c39cc..a7274f4 100644
--- a/libavcodec/libmp3lame.c
+++ b/libavcodec/libmp3lame.c
@@ -48,6 +48,8 @@ typedef struct LAMEContext {
     int buffer_index;
     int buffer_size;
     int reservoir;
+    int find_peak_sample;
+    int replay_gain;
     int joint_stereo;
     int abr;
     float *samples_flt[2];
@@ -97,7 +99,6 @@ static av_cold int mp3lame_encode_init(AVCodecContext *avctx)
     if ((s->gfp = lame_init()) == NULL)
         return AVERROR(ENOMEM);

-
     lame_set_num_channels(s->gfp, avctx->channels);
     lame_set_mode(s->gfp, avctx->channels > 1 ? s->joint_stereo ? JOINT_STEREO : STEREO : MONO);

@@ -131,12 +132,30 @@ static av_cold int mp3lame_encode_init(AVCodecContext *avctx)
     /* bit reservoir usage */
     lame_set_disable_reservoir(s->gfp, !s->reservoir);

+    /* find peak sample */
+    if (lame_set_decode_on_the_fly(s->gfp, s->find_peak_sample) == -1) {
+        av_log(avctx, AV_LOG_ERROR, "find_peak_sample unavailable: libmp3lame is not configured with --enable-decoder\n");
+        ret = AVERROR(ENOSYS);
+        goto error;
+    }
+
+    /* compute replay gain */
+    lame_set_findReplayGain(s->gfp, s->replay_gain);
+
     /* set specified parameters */
     if (lame_init_params(s->gfp) < 0) {
         ret = -1;
         goto error;
     }

+    if ((lame_get_findReplayGain(s->gfp) != s->replay_gain) ||
+        (lame_get_decode_on_the_fly(s->gfp) != s->find_peak_sample)) {
+        av_log(avctx, AV_LOG_ERROR,
+               "replay_gain and find_peak_sample unavailable: libmp3lame is not patched with http://sourceforge.net/p/lame/patches/66/\n");
+        ret = AVERROR(ENOSYS);
+        goto error;
+    }
+
     /* get encoder delay */
     avctx->delay = lame_get_encoder_delay(s->gfp) + 528 + 1;
     ff_af_queue_init(avctx, &s->afq);
@@ -267,9 +286,11 @@ static int mp3lame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
 #define OFFSET(x) offsetof(LAMEContext, x)
 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
 static const AVOption options[] = {
-    { "reservoir",    "use bit reservoir", OFFSET(reservoir),    AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AE },
-    { "joint_stereo", "use joint stereo",  OFFSET(joint_stereo), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AE },
-    { "abr",          "use ABR",           OFFSET(abr),          AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE },
+    { "reservoir",        "use bit reservoir",   OFFSET(reservoir),        AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AE },
+    { "find_peak_sample", "find peak sample",    OFFSET(find_peak_sample), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE },
+    { "replay_gain",      "compute replay gain", OFFSET(replay_gain),      AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE },
+    { "joint_stereo",     "use joint stereo",    OFFSET(joint_stereo),     AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AE },
+    { "abr",              "use ABR",             OFFSET(abr),              AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE },
     { NULL },
 };

--
1.9.1.423.g4596e3a


More information about the ffmpeg-devel mailing list