xiph / opus-tools

A set of tools to encode, inspect, and decode audio in the Opus format.
https://opus-codec.org/
Other
218 stars 79 forks source link

Why and how does opusdec decode to original input sample rate? #65

Closed guest271314 closed 2 years ago

guest271314 commented 2 years ago

What is the reasoning for opusenc encoding original input sample rate, and specifically, opusdec decoding to original input sample rate?

How does opusdec know what the original sample rate is, and how does opusdec resample from 48000 kHz Opus to input sample rate?

gmaxwell commented 2 years ago

What is the reasoning for opusenc encoding original input sample rate, and specifically, opusdec decoding to original input sample rate?

It's generally necessary to do so if the length is to be preserved exactly, since in a wav file the only thing determining the length is the number of samples, and there was an integer number at the original rate and for it to be the same duration the decode rate has to be an integer multiple of the original for that to be possible. Also, if the original rate were (say) 8000, then it would be kind of misleading to output a 48kHz wav file.

How does opusdec know what the original sample rate is,

It's communicated in the OggOpus header. You can tell opusdec to force decoding to 48000 if you want.

Any good resampler will be perceptually lossless, certainly more so than the opus encoding itself.

guest271314 commented 2 years ago

@gmaxwell I filed this issue because Chromium authors of WebCodecs 'opus' AudioEncoder and AudioDecoder evidently do not gather the significance of decoding to original sample rate, therefore I wanted an objective party with knowledge of the subject matter to provide reasoning for why opusdec does resample to original sample rate. If you find the time perhaps you could chime in to be redundant in this Chromium bug https://bugs.chromium.org/p/chromium/issues/detail?id=1254496, as I gather I am still not explaining what the only fix is.