Verified Commit ef87ba93 authored by Mark Harris's avatar Mark Harris
Browse files

opusenc: Handle LUFS reference loudness in FLAC

Handle the FLAC comment REPLAYGAIN_REFERENCE_LOUDNESS expressed in
either dB SPL (positive) or LUFS (negative).  Although the FLAC
reference software writes dB SPL, some other software (such as
loudgain -s e) writes LUFS.

Fixes #2316
parent 1987a0a4
Pipeline #2527 passed with stage
in 1 minute and 3 seconds
...@@ -122,8 +122,9 @@ static void metadata_callback(const FLAC__StreamDecoder *decoder, ...@@ -122,8 +122,9 @@ static void metadata_callback(const FLAC__StreamDecoder *decoder,
comments=metadata->data.vorbis_comment.comments; comments=metadata->data.vorbis_comment.comments;
saw_album_gain=saw_track_gain=0; saw_album_gain=saw_track_gain=0;
album_gain=track_gain=0; album_gain=track_gain=0;
/*The default reference loudness for ReplayGain is 89.0 dB*/ /*The default reference loudness for ReplayGain is 89 dB SPL,
reference_loudness=89; or -18 LUFS measured according to ITU-R BS.1770 / EBU R128.*/
reference_loudness=-18;
/*The code below uses strtod for the gain tags, so make sure the locale is C*/ /*The code below uses strtod for the gain tags, so make sure the locale is C*/
saved_locale=setlocale(LC_NUMERIC,"C"); saved_locale=setlocale(LC_NUMERIC,"C");
for(i=0;i<num_comments;i++){ for(i=0;i<num_comments;i++){
...@@ -134,11 +135,15 @@ static void metadata_callback(const FLAC__StreamDecoder *decoder, ...@@ -134,11 +135,15 @@ static void metadata_callback(const FLAC__StreamDecoder *decoder,
/*Check for ReplayGain tags. /*Check for ReplayGain tags.
Parse the ones we have R128 equivalents for, and skip the others.*/ Parse the ones we have R128 equivalents for, and skip the others.*/
if(tagcompare(entry,"REPLAYGAIN_REFERENCE_LOUDNESS=",30)==0){ if(tagcompare(entry,"REPLAYGAIN_REFERENCE_LOUDNESS=",30)==0){
/*Reference loundness may be in dB SPL (positive) or
LUFS (negative). The 89 dB SPL reference is considered
to be the same loudness as -18 LUFS.*/
gain=strtod(entry+30,&end); gain=strtod(entry+30,&end);
if(end<=entry+30){ if(end<=entry+30){
fprintf(stderr,_("WARNING: Invalid ReplayGain tag: %s\n"),entry); fprintf(stderr,_("WARNING: Invalid ReplayGain tag: %s\n"),entry);
} }
else reference_loudness=gain; else if (gain<0) reference_loudness=gain;
else reference_loudness=gain-89-18;
continue; continue;
} }
if(tagcompare(entry,"REPLAYGAIN_ALBUM_GAIN=",22)==0){ if(tagcompare(entry,"REPLAYGAIN_ALBUM_GAIN=",22)==0){
...@@ -177,9 +182,9 @@ static void metadata_callback(const FLAC__StreamDecoder *decoder, ...@@ -177,9 +182,9 @@ static void metadata_callback(const FLAC__StreamDecoder *decoder,
} }
setlocale(LC_NUMERIC,saved_locale); setlocale(LC_NUMERIC,saved_locale);
/*Set the header gain to the album gain after converting to the R128 /*Set the header gain to the album gain after converting to the R128
reference level.*/ reference level (-23 LUFS).*/
if(saw_album_gain){ if(saw_album_gain){
gain=256*(album_gain+(84-reference_loudness))+0.5; gain=256*(album_gain+(-23-reference_loudness))+0.5;
inopt->gain=gain<-32768?-32768:gain<32767?(int)floor(gain):32767; inopt->gain=gain<-32768?-32768:gain<32767?(int)floor(gain):32767;
} }
/*If there was a track gain, then add an equivalent R128 tag for that.*/ /*If there was a track gain, then add an equivalent R128 tag for that.*/
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment