AudioEncoder output timestamps don't reset after a flush() and ignore the input timestamps
Categories
(Core :: Audio/Video: Web Codecs, defect, P3)
Tracking
()
People
(Reporter: dpayr, Unassigned, NeedInfo)
References
(Blocks 1 open bug)
Details
User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/158.0.0.0 Safari/537.36
Steps to reproduce
Repro here: https://willowy-druid-450481.netlify.app/
For the code look at the page's source.
Actual results
After flush(), an AudioEncoder (configured with Opus, in this case) doesn't base the timestamps of new output chunks on the new input AudioData. It keeps counting from where the previous segment ended, as if an internal sample counter carries over the flush.
Encoding 0.1 s of mono 48 kHz audio at timestamp 0, flushing, and then encoding the same 0.1 s at timestamp 0 again gives these chunk timestamps:
First segment: 0, 13500, 33500, 53500, 73500, 93500
Second segment: 93500, 113500, 133500, 153500, 173500, 193500
Expected results
Each segment's chunk timestamps should follow its input timestamps, as in Chrome and Safari:
First segment: 0, 20000, 40000, 60000, 80000, 100000
Second segment: 0, 20000, 40000, 60000, 80000, 100000
Because of this, an encoder can't be reused across flush() for discontinuous input unless the output timestamps are fixed up manually.
Comment 1•2 days ago
|
||
The Bugbug bot thinks this bug should belong to the 'Core::Audio/Video: Web Codecs' component, and is moving the bug to that component. Please correct in case you think the bot is wrong.
Comment 2•1 day ago
|
||
| gecko-triage | ||
Thanks for the clear test case. A code reading at the current tree explains both timestamp problems. The steps reproduce them every time.
Analysis
On desktop, the Opus encoder for WebCodecs is FFmpegAudioEncoder. It creates its TimedPacketizer only once, from the mTime of the first AudioData (FFmpegAudioEncoder.cpp). Each packet then gets the previous timestamp plus the packet size (TimedPacketizer.h). Later input timestamps are only logged, and nothing calls TimedPacketizer::Discontinuity().
On flush(), the drain rebuilds the libopus context (FFmpegDataEncoder.cpp). It keeps the packetizer and mFirstPacketPts. So the second segment continues from 4800 samples, and the timestamps start at 93500 µs in place of 0.
The 6.5 ms shift in the first segment is the Opus lookahead (312 samples at 48 kHz). The ffvpx libopus wrapper subtracts it from each packet timestamp. ToMediaRawData moves a packet back only when it is earlier than mFirstPacketPts (FFmpegAudioEncoder.cpp). So only the first chunk goes back to 0, and the other chunks keep the shift: 13500, 33500 and so on. In the second segment, all values are above 0, so no chunk moves back. The code marks this handling as interim until w3c/webcodecs#626 is settled.
Fix direction
- After a successful drain in
FFmpegAudioEncoder::DrainWithModernAPIs, reset the packetizer and clearmFirstPacketPts. The next segment then takes its start time from its own input. - Optionally, call
Discontinuity()when the input timestamps have a gap, after the partial packet drains. - To match Chrome and Safari (0, 20000, 40000), add the lookahead back to every packet timestamp. The pre-skip is already in the OpusHead description. This part depends on w3c/webcodecs#626.
A WPT in webcodecs/audio-encoder.https.any.js can show the fix: encode, flush, encode again at timestamp 0, and check the first chunk of the second segment. No regression is known: this behavior is present since the first FFmpegAudioEncoder (bug 1749046). This bug blocks the WebCodecs meta bug 1746557.
Calvin, could you please decide how the encoder must align the Opus lookahead (step 3) while w3c/webcodecs#626 is open?
If you'd like to provide feedback on this comment, please use the 👍 or 👎 reaction.
Description
•