[v3,5/5] af_volume: implement replaygain clipping prevention

Message ID 1395958046-3679-1-git-send-email-alessandro@ghedini.me
State New
Headers show

Commit Message

Alessandro Ghedini March 27, 2014, 10:07 p.m.
---
Fixed the initialization of gain and peak. If either of the "... != INT32_MIN"
and "... != 0" checks failed, they wouldn't have gotten initialized.

 libavfilter/af_volume.c | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)

Comments

Justin Ruggles March 29, 2014, 4:49 p.m. | #1
On 03/27/2014 06:07 PM, Alessandro Ghedini wrote:
> ---
> Fixed the initialization of gain and peak. If either of the "... != INT32_MIN"
> and "... != 0" checks failed, they wouldn't have gotten initialized.
> 
>  libavfilter/af_volume.c | 29 +++++++++++++++++++----------
>  1 file changed, 19 insertions(+), 10 deletions(-)

Peak limiting should be optional, with the default being "on". A user
may want to use their own limiting algorithm or apply compression.

-Justin

Patch

diff --git a/libavfilter/af_volume.c b/libavfilter/af_volume.c
index 393c4e7..f5ab53c 100644
--- a/libavfilter/af_volume.c
+++ b/libavfilter/af_volume.c
@@ -246,25 +246,34 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
     if (sd && vol->replaygain != REPLAYGAIN_IGNORE) {
         if (vol->replaygain != REPLAYGAIN_DROP) {
             AVReplayGain *replaygain = (AVReplayGain*)sd->data;
-            int32_t gain;
-            float g;
-
-            if (vol->replaygain == REPLAYGAIN_TRACK &&
-                replaygain->track_gain != INT32_MIN)
-                gain = replaygain->track_gain;
-            else if (replaygain->album_gain != INT32_MIN)
-                gain = replaygain->album_gain;
-            else {
+            int32_t gain  = 100000;
+            uint32_t peak = 100000;
+            float g, p;
+
+            if (vol->replaygain == REPLAYGAIN_TRACK) {
+                if (replaygain->track_gain != INT32_MIN)
+                    gain = replaygain->track_gain;
+
+                if (replaygain->track_peak != 0)
+                    peak = replaygain->track_peak;
+            } else if (vol->replaygain == REPLAYGAIN_ALBUM) {
+                if (replaygain->album_gain != INT32_MIN)
+                    gain = replaygain->album_gain;
+
+                if (replaygain->album_peak != 0)
+                    peak = replaygain->album_peak;
+            } else {
                 av_log(inlink->dst, AV_LOG_WARNING, "Both ReplayGain gain "
                        "values are unknown.\n");
-                gain = 100000;
             }
             g = gain / 100000.0f;
+            p = peak / 100000.0f;
 
             av_log(inlink->dst, AV_LOG_VERBOSE,
                    "Using gain %f dB from replaygain side data.\n", g);
 
             vol->volume   = pow(10, (g + vol->replaygain_preamp) / 20);
+            vol->volume   = FFMIN(vol->volume, 1.0 / p);
             vol->volume_i = (int)(vol->volume * 256 + 0.5);
 
             volume_init(vol);