Bug 229251

Summary: [WebAudio/WebM] Incorrect number of frames returned if decoding frame rate doesn't match original
Product: WebKit Reporter: Jean-Yves Avenard [:jya] <jean-yves.avenard>
Component: Web AudioAssignee: Jean-Yves Avenard [:jya] <jean-yves.avenard>
Status: RESOLVED FIXED    
Severity: Normal CC: cdumez, eric.carlson, ews-watchlist, glenn, jer.noble, philipj, sergio, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: Other   
Hardware: Unspecified   
OS: Unspecified   
Bug Depends on:    
Bug Blocks: 227111    
Attachments:
Description Flags
Patch
none
Patch none

Description Jean-Yves Avenard [:jya] 2021-08-18 14:56:04 PDT
When calling decodeAudioData with a context where a sampling rate set is different to the original content, the number of frames returned may be incorrect

STR:
1- Open https://jyavenard.github.io/htmltests/tests/webaudio/decodeAudioData.html
2- Select opus.webm as the file to use
3- Set the sampling rate to 44100
4- Press play

The numbers showing are 88254 frames 2.001224489795918
which indicates that we've returned 88254 frames and that the length of the blob decoded is over 2s

This file is originally encoded at 48kHz and is exactly 2s long. When forcing the content to be decoded at 44.1kHz we should be getting 88200 frames and a 2s duration.

Something is wrong in the resampling and the trimming.
Comment 1 Radar WebKit Bug Importer 2021-08-18 14:56:31 PDT
<rdar://problem/82095650>
Comment 2 Jean-Yves Avenard [:jya] 2021-09-08 03:40:10 PDT
Created attachment 437617 [details]
Patch
Comment 3 Eric Carlson 2021-09-08 15:19:30 PDT
Comment on attachment 437617 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=437617&action=review

> Source/WebCore/platform/audio/cocoa/AudioFileReaderCocoa.cpp:339
> +    uint32_t leadingTrim = m_webmData->m_track->codecDelay().value_or(MediaTime()).toDouble() * outFormat.mSampleRate;

`MediaTime::zeroTime()` should be cheaper than `MediaTime()`

> Source/WebCore/platform/audio/cocoa/AudioFileReaderCocoa.cpp:341
> +    uint32_t trailingTrim = (m_webmData->m_track->codecDelay().value_or(MediaTime()) + m_webmData->m_track->discardPadding().value_or(MediaTime())).toDouble() * outFormat.mSampleRate - leadingTrim + 0.5;

Ditto
Comment 4 Jean-Yves Avenard [:jya] 2021-09-08 19:34:27 PDT
Created attachment 437698 [details]
Patch
Comment 5 EWS 2021-09-09 00:02:29 PDT
Committed r282196 (241483@main): <https://commits.webkit.org/241483@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 437698 [details].