Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: opusenc applies replaygain from FLAC while transcoding (Read 13780 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

opusenc applies replaygain from FLAC while transcoding

Hello,

I observed some strange behaviour of opusenc, when I use it to encode .flac files which contain replaygain data.
Code: [Select]
$metaflac --list audio.flac
...
comment[7]: replaygain_album_gain=-6.81 dB
comment[8]: replaygain_album_peak=1.000000
comment[9]: replaygain_track_gain=-6.37 dB
comment[10]: replaygain_track_peak=0.988525
...


Then, I encode that file with oggenc to .ogg and opusenc to .opus:

Code: [Select]
$oggenc audio.flac
$ogginfo audio.ogg
...
replaygain_album_gain=-6.81 dB
replaygain_album_peak=1.000000
replaygain_track_gain=-6.37 dB
replaygain_track_peak=0.988525
...
$opusenc audio.flac audio.opus
$opusinfo audio.opus
...
Opus stream 1:
Pre-skip: 356
Playback gain: -11.8086 dB
...


First strange observation is the very high gain value.
Second, even stranger observation: Even if I disable replaygain application in audio players, the opus file is much quiter than the ogg or flac file.

When I look at the waveform of the files in audacity (I also tried to manually decode the ogg and the opus file with oggdec/opusdec and load the wav files, same result), I see that the opus file indeed is much quiter (audacity does not apply replaygain, afaik. The first waveform is the opus file.):

[attachment=8176:audacity.png]

It seems like oggenc works correctly: It decodes the flac file (without applying replaygain), encodes to ogg and rewrites the replaygain tag fields. opusenc does not work as expected, I assume it applies replaygain when decoding the flac file and stores wrong replaygain values. This behaviour can be observed with any of my tagged flac files.

If I use the flag --discard-comments, then the loudness is correct, but I lose all tags.

By the way, that's the versions I'm using:
Code: [Select]
$ opusenc --version
opusenc opus-tools 0.1.9 (using libopus 1.1)
Copyright (C) 2008-2013 Xiph.Org Foundation
$ oggenc --version
oggenc from vorbis-tools 1.4.0
$metaflac --version
metaflac 1.3.1
$ flac --version
flac 1.3.1

opusenc applies replaygain from FLAC while transcoding

Reply #1
First strange observation is the very high gain value.

This comes from difference in reference level. Replaygain uses 89dB and opus (based on R128) uses 84dB. Therefore, they differ by 5dB.

Quote
Second, even stranger observation: Even if I disable replaygain application in audio players, the opus file is much quiter than the ogg or flac file.

As is written in https://tools.ietf.org/html/draft-ietf-code...-06#section-5.1 (6.  *Output Gain*), the gain value is written in the ID header of the ogg container, and the spec says "Virtually all players and media frameworks SHOULD apply it by default."

On the implementation side, opusfile (decoder wrapper library for oggopus) automatically applies gain to the decoded samples. So does libavcodec (of ffmpeg).
In other words, gain application is usually done not by applications but at decoder library level to enforce gain application written in the spec.
This is different from other formats where gain application is done at application(player) level, where user might choose how to apply gain.
This is why you get gain-applied result even if you configured player not to.

Quote
opusenc does not work as expected, I assume it applies replaygain when decoding the flac file and stores wrong replaygain values. This behaviour can be observed with any of my tagged flac files.

So, this is a wrong guess.

opusenc applies replaygain from FLAC while transcoding

Reply #2
And if you want to confirm that opusenc is not messing around with input before encoding, just remux opus to matroska by mkvmerge then decode it. You will get raw decoded samples without gain application.

opusenc applies replaygain from FLAC while transcoding

Reply #3
Great, thanks for your explanation!  So basically, I only need to take care when transcoding opus files (as gain is applied to e.g. decoded wav, which is not reversible).

Additionally, can you provide a link to the specification of "R128" or explain why opus defaults to 84db, which is pretty less in my opinion? What's the reason to not use 89db?

opusenc applies replaygain from FLAC while transcoding

Reply #4
The Opus spec mandates this "interesting" solution: there isn't a separate ReplayGain album gain tag, but the output gain (which is almost always applied) is where you are supposed to put that information.

There is a long thread about it here...
http://www.hydrogenaud.io/forums/index.php?showtopic=104878
...though you can get the gist from the first two posts.

Cheers,
David.

opusenc applies replaygain from FLAC while transcoding

Reply #5
The Opus spec mandates this "interesting" solution: there isn't a separate ReplayGain album gain tag, but the output gain (which is almost always applied) is where you are supposed to put that information.

There is a long thread about it here...
http://www.hydrogenaud.io/forums/index.php?showtopic=104878
...though you can get the gist from the first two posts.

Cheers,
David.


The latest spec does specify separate (R128) album and track replaygain tags.  It simply wasn't practical in the real world to work without both tags.  There is still weirdness related to the gain contained in the Opus header and some vagueness about whether some or all of the replaygain should be in the header instead of the tags, but there are at least two tags to work with.  There is still a philosophical opposition to peak tags or the traditional REPLAYGAIN_* tags.

https://tools.ietf.org/html/draft-ietf-codec-oggopus-06

opusenc applies replaygain from FLAC while transcoding

Reply #6
Good, that's progress. No need for actual REPLAYGAIN_* tags when the format includes an equivalent. What they say about the interaction of R128_*_GAIN and the 'output gain' field makes sense. It's rather like mp3 and the global gain field.

Thank you for the link lithopsian.

Cheers,
David.

opusenc applies replaygain from FLAC while transcoding

Reply #7
Hi there,

sorry for rolling up this thread again. I need your help once more to finally get rid of all questions about opus and gain adjustment.

To shortly sum up what has been written before:
At the moment, I am using oggenc to transcode my flac files to ogg vorbis for portable devices. My flac files have replaygain tags, and these tags are copied to the ogg vorbis files. That's great, because I don't want to apply Replaygain on my portable devices.
However, when I use opusenc to transcode my flac files to opus, I can't do that. opusenc applies the values in the FLAC's replaygain tag to the R128 header(?). Even after removing all metadata from an opus files, the playback volume is very low (see audacity screenshot in previous post).
If I remove the replaygain tag from my flac file, and then run opusenc, everything is fine. But that's not an applicable solution for me, I want to run opusenc on my flac archive in order to output the opus files on my portable device.


So, is there any way to prevent opusenc from "applying" the data from replaygain tags to the R128(?) header? Such that I can handle these opus files like my ogg files (being able to disable Replaygain application)?
I would really like to switch from ogg vorbis to opus, but the automatic, non-reversible application of replaygain values from flac files to the opus header is not acceptable for my scenario.

PS: I am using the command line encoder on my linux system, oggenc 1.4.0.
Edit: Players like foobar2000 work fine, as they seem to recognize the R128 data and use the metadata stored in the tag, in order to reverse gain application. My android player (Rocket Player) doesn't seem to support this feature, so tracks encoded from flac files with replaygain tags are always much quiter than their flac/ogg vorbis equivalent. GoneMAD, which also supports opus, is the same.

Edit 2: This piece of source code is responsible for the undesired behaviour (line 124-190): https://github.com/gcp/opus-tools/blob/89c2...6008/src/flac.c
opusenc scans flac files' replaygain tags and applies the value to R128. IMO, we need an option to bypass this, as there are plenty of players which can not reverse the application upon playback..


Thanks and regards
vibee

opusenc applies replaygain from FLAC while transcoding

Reply #8
Opus is built this way for a reason: they want that header gain applied whether you like it or not.  The R128 reference level is about 6dB quieter than the level used by almost every implementation of the REPLAYGAIN tags.  It is also difficult to turn off because opusenc applies the gain in the special header field which most players don't support turning off.  With the design goal of being "just right" for everyone they have made it massively incompatible with almost every music player and other audio format, and the spec does not encourage applications to allow messing with it at the risk of being considered non-compliant.

For your information, what is probably happening is the following:
1. opusenc reads your Flac files and finds an album gain, which it stuffs in the header gain field (adjusted to R128 levels by adding the difference between 84 and the reference loudness, default 89, plus an extra 0.5 for good measure).
2. opusenc places the difference between the track gain and the album gain into metadata, so the sum of the header and the tag value is the track gain.  The spec now supports both a track gain tag and an album gain tag, but I don't think opusenc ever writes an album gain tag, plus relatively few players recognise it.

You now have a track that most likely plays nearly 6dB (possibly more) quieter than the Flac whether or not you use replaygain in your player.  The solution to that isn't simple.

One solution is not to encode Flac tracks with replaygain tags in them in Opus.  Then you'll get an Opus file that plays at the same level as the original Flac.  You may be able to apply replay gain to the Opus track from scratch, but not many tools for that at the moment and it will still likely be at R128 levels (r128gain allows you to use the "wrong" reference level, I think?).  You may also be able to edit the header to contain a different value which your player will then blindly apply.  Or you may be able to find a smarter player that skirts round the edges of the spec to play something at a sensible level within your control.  I think FB200 fudges its way from assumed-R128 back to a level compatible with standard replaygain tags?  Don't quote me on that, but I thought I read it somewhere.  Deadbeef (with the native Opus plugin, not ffmpeg) supports playback of Opus files adjusted to any level you like, as well as editing the header gain field, but it can't do it from the command line so not exactly convenient for you.

opusenc applies replaygain from FLAC while transcoding

Reply #9
It occurred to me that it might be simpler to hack out a couple of lines from opusenc, recompile, and have yourself a merry old time

opusenc applies replaygain from FLAC while transcoding

Reply #10
If ffmpeg is used instead of opusenc, REPLAYGAIN tags will be copied as is. And playback gain will not be set.

opusenc applies replaygain from FLAC while transcoding

Reply #11
It occurred to me that it might be simpler to hack out a couple of lines from opusenc, recompile, and have yourself a merry old time


I also thought about this solution, like adding my -nogain command line flag to bypass the code.

If ffmpeg is used instead of opusenc, REPLAYGAIN tags will be copied as is. And playback gain will not be set.


Now that's a great solution, thank you! Just tried it and it works. A simple
Code: [Select]
ffmpeg -i $FLACFILE -c libopus $OPUSFILE
did the trick. I never used ffmpeg on the shell before. Is it correct that I can't access all opusenc options, but only those which are availible in the ffmpeg API (listed with ffmpeg -h)?

Thanks!

opusenc applies replaygain from FLAC while transcoding

Reply #12
If ffmpeg is used instead of opusenc, REPLAYGAIN tags will be copied as is. And playback gain will not be set.


Now that's a great solution, thank you! Just tried it and it works. A simple
Code: [Select]
ffmpeg -i $FLACFILE -c libopus $OPUSFILE
did the trick. I never used ffmpeg on the shell before. Is it correct that I can't access all opusenc options, but only those which are availible in the ffmpeg API (listed with ffmpeg -h)?

Thanks!


All relevant options are mapped. And it's all documented in man pages:
https://ffmpeg.org/ffmpeg-codecs.html#libopus-1

opusenc applies replaygain from FLAC while transcoding

Reply #13
hm, maybe that could be the reason why i am able to abx opus reliable at 96k vbr
Code: [Select]
foo_abx 2.0.1 report
foobar2000 v1.3.8
2015-06-16 23:57:24

File A: bad.wav
SHA1: 73d1e11f1f670690bca9fbf51b0f2d576ba2b9b4
File B: bad.opus
SHA1: 72128f4e98e3674b6cf2d750ac8770ef7e2f6c0c

Output:
DS : Primärer Soundtreiber, 16-bit
Crossfading: NO

23:57:24 : Test started.
23:59:11 : 00/01
23:59:48 : 01/02
00:00:48 : 02/03
00:01:01 : 03/04
00:01:12 : 03/05
00:01:40 : 04/06
00:01:57 : 05/07
00:02:09 : 06/08
00:02:32 : 07/09
00:02:45 : 08/10
00:02:45 : Test finished.

----------
Total: 8/10
Probability that you were guessing: 5.5%

-- signature --
edbf3ef539e14458b5c44865c8b4703bb9b561e1
plus 7/10 at 128k, 9/10 at 64k

"Bad" from Guetta+Showtek, FLAC Download
2005ish laptop's internal soundcard, TDK inEars

i get only 4/10 with qaac at ~130kbit tvbr,
however most times could even bust ogg q5...

hard to explain but the wav sounds like "more"

opusenc applies replaygain from FLAC while transcoding

Reply #14
hm, maybe that could be the reason why i am able to abx opus reliable at 96k vbr


Compare the replaygain values for the two files. 


opusenc applies replaygain from FLAC while transcoding

Reply #16
I stumbled over Opus' special way of handling gain yesterday and wanted to add that ffmpeg's addition of REPLAYGAIN tags won't always work.
  • If I have an .opus file and ReplayGain it with Foobar2000, it plays fine in Foobar2000 but too quiet on GStreamer-based players (on Linux) because the player needs to know about this quirk.
  • If I gain it with something based on ffmpeg (e.g. bs1770gain --replaygain), Foobar2000 will play it fine, GStreamer refuses to play due to a "stream error".
  • If I ReplayGain it with Foobar2000 and then with e.g. bs1770gain, the file will have the unchanged R128_* tags, the unchanged playback gain and now, additionally, REPLAYGAIN tracks. GStreamer still refuses to play, Foobar2000 will play too loud and with horrible clipping.

So yeah. I've started going around and filing bug reports.

Why do only I and a few other freaks care about R128/ReplayGain 2.0? There would be less of these bugs around and broader support from players and media frameworks... And why is this still an optional disabled-by-default feature of media players? It should be a core feature of any player. The world needs more R128! Automatically and for the entire collection! And there should be laws to enforce mastering everything to R128.

Re: opusenc applies replaygain from FLAC while transcoding

Reply #17
Ok, I found this thread after experimenting with moving some of my collection over from Flac. I see that it's pretty old, but I'm hoping that jumping in here will be more fruitful than starting a new thread.

I'm grappling with this gain stuff; all the opus files I'm encoding are WAAAAAY quieter on all of my playback software (I'm using VOX on macos, and RocketPlayer on Android, so no, I'm not using FooBar.) After reading through this and other threads, it seems likely to be a player problem, so I dug in a little deeper.

I took a well-made flac file (that I ripped and that has good metadata), let's call it file.flac. I made an opus from it (file.opus) with
Code: [Select]
opusenc --bitrate 160 file.flac
.

When listening on my player(s), the opus file is way quieter. Enough so that if I turn it up to "reasonable", I'm worried that I'll hurt myself if/when I play something else, since I'm listening on headphones.

Ok, so then I thought I'd use reference tools to convert my files into wav files:

Code: [Select]
opusdec file.opus opus.wav
flac -d file.flac -o flac.wav

But the opus-derived wav is still much quieter. Must be that 89db vs 84db thing, right?

Code: [Select]
opusdec --gain 5.0 file.opus opus5db.wav

Nope, still quieter.

Code: [Select]
opusdec --gain 6.0 file.opus opus6db.wav

Still quieter.

These files should be substantially similar; in fact, unless I have supra-human hearing, I shouldn't be able to tell the difference.

Can anyone lend some expertise? Thanks!

FYI, opus tools is opus-tools 0.1.9 (using libopus 1.1.2) and flac 1.3.1.



Re: opusenc applies replaygain from FLAC while transcoding

Reply #18
The expected 5/6 dB difference is certainly noticeable but I wouldn't ever describe it as threatening to break speakers or eardrums.  I suspect something else.  opusenc has a habit of putting gain tags into an Opus header field which is then (almost) always applied by decoders.  You can see it as "Output Gain" by running opusinfo.  It may be unexpectedly large, or you might have also have gain tags which are doubling the effect.

Re: opusenc applies replaygain from FLAC while transcoding

Reply #19
I've tried to understand the situation, even reading the thread where Garf replied but... couldn't wrap my head around it.

My situation is: I play opus files transcoded from Madsonic, on my Nexus 6P with Gone Mad Music Player.
The line I use to transcode is this:

ffmpeg -i %s -f opus -acodec libopus -b:a 96000 -

where %s is the file to be transcoded, for those unfamiliar with Madsonic/Subsonic. FFMPEG is latest Zeranoe build, with libopus 1.1.3.

The resulting files have replaygain tags copied, both from FLAC and MP3 files (I know transcoding from lossy is frowned upon, these are my ears and in the car it sounds good enough anyway, considering the sources have high bitrates).

Now... how is Gone Mad Music Player handling those tags? If I disable ReplayGain in preferences I definitely feel the volume increasing but... am I risking too much volume attenuation? Could it be applying both replaygain subtraction and this sort of "default" attenuation that Opus standard calls for?

Re: opusenc applies replaygain from FLAC while transcoding

Reply #20
I don't use ffmpeg for Opus, but I don't think it sets the gain header (naughty, naughty!), or at least leaves it at zero.  Instead it copies any (traditional) replaygain tags that it finds into the Opus file.  Replaygain values are not strictly transferable in this way, but the result should be close enough for your needs.  In short, you should be good to go.

Re: opusenc applies replaygain from FLAC while transcoding

Reply #21
Replaygain values are not strictly transferable in this way
Sorry, I knew about this and should have specified it. "Close enough" was good enough for me. Thanks for your answer.

Re: opusenc applies replaygain from FLAC while transcoding

Reply #22
Horseshoes, hand-grenades and loudness normalization.

I recently re-encoded a large number of albums using R-128 (I use Lame's --scale switch) and, if anything, squashed tracks are less even against dynamic tracks than they were with David's algorithm with my library.  I have no idea what Opus would do.

Re: opusenc applies replaygain from FLAC while transcoding

Reply #23
Horseshoes, hand-grenades and loudness normalization.

I recently re-encoded a large number of albums using R-128 (I use Lame's --scale switch) and, if anything, squashed tracks are less even against dynamic tracks than they were with David's algorithm with my library.  I have no idea what Opus would do.
What the heck is this R- 128 you're all talking about? Is it a new RG standard? I'm still "stuck" at the old replaygain at 89db. Has that changed somehow? Sorry for my ignorance.

Re: opusenc applies replaygain from FLAC while transcoding

Reply #24
89 (more precisely: 83+6; at least I think) is still 89.

https://www.google.com/webhp?ie=utf-8&oe=utf-8#q=r-128+loudness
https://www.google.com/search?q=r-128+replaygain&ie=utf-8&oe=utf-8