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: Opus gapless and glitchness encoding (Read 32277 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Re: Opus gapless and glitchness encoding

Reply #51
Conclusions so far from discussion with jmvalin:

1) The correct way to handle this is to do the split in libopusenc. This will be gapless and glitchless. Everything else is going to be some kind of workaround that can still fail for pathological samples.

2) The signal in this file behaves badly in digital filters when they assume the signal before/after they operate is 0 (this is the default). To prevent this, you need to extend the signal, for example with a simple LPC predictor. Opus does this for the *end* of files, but not at the start. (https://github.com/xiph/libopusenc/blob/master/src/opusenc.c#L1147) I confirmed by mixing the Vorbis and Opus encodes that it's the start of the second file that glitches, not the end of the first one.

3) This needs to happen in both the resampler and the encoder. Easiest would be to have the resampler always do it, and feed the extended output to the encoder.

4) Vorbis implements it also for the start of files (https://github.com/xiph/vorbis/blob/ea8b03fce93444cb3cf0131909e15b4f8856e863/lib/block.c#L416) which is why it behaves so well here. (And obviously it can't fail at the resampling step).

Re: Opus gapless and glitchness encoding

Reply #52
But why not implement extrapolation also for the start of file in opus, just like in vorbis? Or is it impossible without breaking something?

Re: Opus gapless and glitchness encoding

Reply #53
Guys, any news for fix it glitch? :'( Or OPUS no longer developing? :o


Re: Opus gapless and glitchness encoding

Reply #55
Nice, that is indeed gapless as long as decoding doesn't use Opus' resampler. That can introduce glitches again.

Re: Opus gapless and glitchness encoding

Reply #56
Nice, didn't know how exactly this is done.
I think I'm going to try replicating something similar with python & ctypes...
a fan of AutoEq + Meier Crossfeed

Re: Opus gapless and glitchness encoding

Reply #57
Conclusions so far from discussion with jmvalin:

1) The correct way to handle this is to do the split in libopusenc.
...

Guys I know this is quite old thread but to be honest I must say I dont understand what does it mean.  Is there different algorithm used ? Could you please clarify?

Re: Opus gapless and glitchness encoding

Reply #58
The only way to guarantee a smooth transition is to combine all of the tracks into one big audio stream, encode the whole thing, and then split it back apart into separate files.

When you use libopusenc, you can tell it to split the encoded Opus stream into multiple files, and there will be no gaps or glitches at track boundaries.

Re: Opus gapless and glitchness encoding

Reply #59
Thank you! Now it is clear!

Re: Opus gapless and glitchness encoding

Reply #60
I have experimented with all this including Octocontrabass's code as 192 kbit/s listening test made me reconsider my stance on Opus (and ditch Vorbis):

1st off, opusenc does some tricks that ensures that the opus signal starts and ends where the start and the end samples are even w/o any knowledge of the previous and the following tracks. If I encode with opusenc each track one by one, it does appear gapless in Linux  on mpv media player and also on sox's play. Somewhere I read, that DeadBeef also works, but I have not tested it. However, mplayer and cmus both incorrectly decode so it is not.

However, I believe, one, indeed, needs to use libopusenc (i.e. opusenc) to get this and the next level of gapness. FFmpeg does not link with libopusenc, so it might not work well in the gapless department.

The libopusenc has a provision that Octocontrabass uses which is not documented, but the best description I found is at http://lists.xiph.org/pipermail/opus/2017-November/004047.html (albeit the bloke there is chasing something completely different). However, this quote describes this even better gapless-ness:

Quote
Essentially, the last frame of a file is marked as a "keyframe" by
disabling prediction for this frame in libopus. This encoded keyframe
frame is then copied in verbatim to the next file, with the pre-skip
set to the frame length. The encoder is not reset, and reading the
keyframe will bring the decoder to the correct state in which it is
able to correctly decode the following frames.

So it is not exactly how Octocontrabass describes, but it looks better than encoding each track indiviually. I have tested that code, and it does work, indeed. That will be my choice of encoding each track per album. You must only apply albumgain before encoding as if each tracks are amplified differently by track gain, you will have discontinuities.

I am studing libopusenc now and we could add this functionaly to opusenc so that we would pass all the tracks like
opusenc in1.wav out1.opus in2wav out2.opus
I started to do that, but opusenc is messy enough for me. I think I will just extend the opusgap for my needs. I prefer clean code to ensure reliability

PS: While I read opusenc it seemed that it assumes mono/stereo input only. No-one noticed? It seems quite a limitation. I hope I read it wrong.

PS: Albeit Opus looks like a great technology, but it is disheartening that opus library needs another layer, namely libopusenc, to be able to encode gaplessly. This is really a sign of haste and bad design. I do not understand what was the rush. But at least Jean-Marc Valin seemed to save the day at least with libopusenc, so I can consider Opus now. Again, gapless playback is essential.

Re: Opus gapless and glitchness encoding

Reply #61
So it is not exactly how Octocontrabass describes, but it looks better than encoding each track indiviually.
I did see that code in libopusenc, but I figured the details weren't especially important. ;) (I think it's possible to split the stream without a keyframe, but then you would need to copy more than one frame from the previous file, and I don't know how to figure out exactly how many frames you need.)

PS: While I read opusenc it seemed that it assumes mono/stereo input only. No-one noticed? It seems quite a limitation. I hope I read it wrong.
Are you talking about opusgap? I really only wrote it for myself, so it only supports the WAV files I want to encode with it. I can add support for more WAV formats if you can provide samples of the files you want to encode.

Re: Opus gapless and glitchness encoding

Reply #62
Quote
I think it's possible to split the stream without a keyframe, but then you would need to copy more than one frame from the previous file, and I don't know how to figure out exactly how many frames you need
One naive and yet bulletproof way is to iteratively try adding 1 more frame until the required slice starts decoding identically. But it will be pretty inefficient, especially if encoding a lot of short tracks.
a fan of AutoEq + Meier Crossfeed

Re: Opus gapless and glitchness encoding

Reply #63
The Opus decoder isn't defined to be bit-exact, so you might not copy enough frames to put every decoder in the same state that way. I'm not familiar enough with the format to come up with a better solution, though.

Re: Opus gapless and glitchness encoding

Reply #64
I just saw this thread; the only proper way to solve this seems to be for the player to not dump and create a new context for a new file, the way pretty much all decoders do. mp3/aac gapless works only because decoders will hold the context open from file to file, as long as they were encoded the same way, and some even did that for Vorbis, but it seems like everyone just punted for Opus, maybe since they don't want to deal with preroll frames. Due to that, I just use a 1-second crossfade and deal with it.

Re: Opus gapless and glitchness encoding

Reply #65
I just saw this thread; the only proper way to solve this seems to be for the player to not dump and create a new context for a new file, the way pretty much all decoders do.
No, the proper way to solve this is to encode your gapless audio using the gapless support in libopusenc. ;)

I have an example encoder a few posts up if you'd like to give it a try.

Re: Opus gapless and glitchness encoding

Reply #66
Err, more than three years have passed.
Proof of concept by @Octocontrabass makes it gapless, official Opusenc does not.
How to get the case off the ground?
• We can do better about lossy music: Opus complexity & qAAC dependence on Apple is a departure from Vorbis & Musepack breakthroughs
• Clipping-free MP3 encoding for vintage gear: SoX to 44.1 kHz → LoudMax -1 dB ISP → ADClip8 → Smart dither 16 bit → Lame3995o -Q1
• Plz, update WavPack hybrid & FSLAC