Skip to content

Commit

Permalink
demux_lavf: simplify replaygain export
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
jeeb authored and kasper93 committed Dec 5, 2024
1 parent 1f86733 commit 1b1a8a3
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions demux/demux_lavf.c
Original file line number Diff line number Diff line change
Expand Up @@ -618,31 +618,33 @@ 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;

// 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 (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
Expand Down

0 comments on commit 1b1a8a3

Please sign in to comment.