From be90f2c8dd0431e252e43d5249e89446309113af Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Tue, 18 Dec 2018 15:15:09 -0500 Subject: demux: make ALBUM ReplayGain tags optional when using libavformat Commit e392d6610d1e35cc0190c794c151211b0aae83e6 modified the native demuxer to use track gain as a fallback for album gain if the latter is not present. This commit makes functionally equivalent changes in the libavformat demuxer. --- demux/demux.c | 5 +++++ demux/demux_lavf.c | 33 ++++++++++++++++++++++----------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/demux/demux.c b/demux/demux.c index f02f9b77aa..a8b392d74e 100644 --- a/demux/demux.c +++ b/demux/demux.c @@ -1946,12 +1946,17 @@ static struct replaygain_data *decode_rgain(struct mp_log *log, { struct replaygain_data rg = {0}; + // Set values in *rg, using track gain as a fallback for album gain if the + // latter is not present. This behavior matches that in demux/demux_lavf.c's + // export_replaygain; if you change this, please make equivalent changes + // there too. if (decode_gain(log, tags, "REPLAYGAIN_TRACK_GAIN", &rg.track_gain) >= 0 && decode_peak(log, tags, "REPLAYGAIN_TRACK_PEAK", &rg.track_peak) >= 0) { if (decode_gain(log, tags, "REPLAYGAIN_ALBUM_GAIN", &rg.album_gain) < 0 || decode_peak(log, tags, "REPLAYGAIN_ALBUM_PEAK", &rg.album_peak) < 0) { + // Album gain is undefined; fall back to track gain. rg.album_gain = rg.track_gain; rg.album_peak = rg.track_peak; } diff --git a/demux/demux_lavf.c b/demux/demux_lavf.c index b1800018c4..4802278af5 100644 --- a/demux/demux_lavf.c +++ b/demux/demux_lavf.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2004 Michael Niedermayer + * Copyright (C) 2018 Google LLC * * This file is part of mpv. * @@ -561,17 +562,27 @@ static void export_replaygain(demuxer_t *demuxer, struct sh_stream *sh, av_rgain = (AVReplayGain*)src_sd->data; rgain = talloc_ptrtype(demuxer, rgain); - rgain->track_gain = (av_rgain->track_gain != INT32_MIN) ? - av_rgain->track_gain / 100000.0f : 0.0; - - rgain->track_peak = (av_rgain->track_peak != 0.0) ? - av_rgain->track_peak / 100000.0f : 1.0; - - rgain->album_gain = (av_rgain->album_gain != INT32_MIN) ? - av_rgain->album_gain / 100000.0f : 0.0; - - rgain->album_peak = (av_rgain->album_peak != 0.0) ? - av_rgain->album_peak / 100000.0f : 1.0; + // Set values in *rgain, using track gain as a fallback for album gain + // 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 (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; + } + } // This must be run only before the stream was added, otherwise there // will be race conditions with accesses from the user thread. -- cgit v1.2.3