From 1b1a8a3e4fb4be48fa57f70216c24a0f8d379f19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ekstr=C3=B6m?= Date: Sat, 19 Dec 2020 17:53:47 +0200 Subject: [PATCH] demux_lavf: simplify replaygain export * Early exit if there is no useful data in the AVReplayGain structure (FFmpeg does check that gain of at least one thing is not INT32_MIN, but leaves the peak unchecked so it can be zero). * Less depth in the if structure. --- demux/demux_lavf.c | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/demux/demux_lavf.c b/demux/demux_lavf.c index f7699ffdfa630..8bba964295ae1 100644 --- a/demux/demux_lavf.c +++ b/demux/demux_lavf.c @@ -618,8 +618,15 @@ static void export_replaygain(demuxer_t *demuxer, struct sh_stream *sh, if (!av_rgain) return; - struct replaygain_data *rgain = talloc_ptrtype(demuxer, rgain); + bool track_data_available = + av_rgain->track_gain != INT32_MIN && av_rgain->track_peak != 0; + bool album_data_available = + av_rgain->album_gain != INT32_MIN && av_rgain->album_peak != 0; + + if (!track_data_available && !album_data_available) + return; + struct replaygain_data *rgain = talloc_ptrtype(demuxer, rgain); rgain->track_gain = rgain->album_gain = 0; rgain->track_peak = rgain->album_peak = 1; @@ -627,22 +634,17 @@ static void export_replaygain(demuxer_t *demuxer, struct sh_stream *sh, // if the latter is not present. This behavior matches that in // demux/demux.c's decode_rgain; if you change this, please make // equivalent changes there too. - if (av_rgain->track_gain != INT32_MIN && av_rgain->track_peak != 0.0) { - // Track gain is defined. - rgain->track_gain = av_rgain->track_gain / 100000.0f; - rgain->track_peak = av_rgain->track_peak / 100000.0f; + if (track_data_available) { + rgain->track_gain = rgain->album_gain = + av_rgain->track_gain / 100000.0f; + rgain->track_peak = rgain->album_peak = + av_rgain->track_peak / 100000.0f; + } - if (av_rgain->album_gain != INT32_MIN && - av_rgain->album_peak != 0.0) - { - // Album gain is also defined. - rgain->album_gain = av_rgain->album_gain / 100000.0f; - rgain->album_peak = av_rgain->album_peak / 100000.0f; - } else { - // Album gain is undefined; fall back to track gain. - rgain->album_gain = rgain->track_gain; - rgain->album_peak = rgain->track_peak; - } + // If album data is actually available, fill it in with proper values. + if (album_data_available) { + rgain->album_gain = av_rgain->album_gain / 100000.0f; + rgain->album_peak = av_rgain->album_peak / 100000.0f; } // This must be run only before the stream was added, otherwise there