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

Message ID 1395829505-4123-6-git-send-email-alessandro@ghedini.me
State New
Headers show

Commit Message

Alessandro Ghedini March 26, 2014, 10:25 a.m.
---
 libavfilter/af_volume.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)

Patch

diff --git a/libavfilter/af_volume.c b/libavfilter/af_volume.c
index 393c4e7..0371aa4 100644
--- a/libavfilter/af_volume.c
+++ b/libavfilter/af_volume.c
@@ -247,24 +247,35 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
         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 {
+            uint32_t peak;
+            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;
+                peak = 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);