Open Bug 2017589 Opened 9 months ago Updated 6 months ago

AudioData.copyTo with interleaved f32 handles frameOffset incorrectly

Categories

(Core :: Audio/Video: Web Codecs, defect, P2)

Firefox 147
defect

Tracking

()

People

(Reporter: hi, Assigned: chunmin)

Details

Attachments

(1 file)

User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36

Steps to reproduce:

This repro:

<!DOCTYPE html>
<html>
<head><title>Firefox AudioData.copyTo frameOffset bug</title></head>
<body>
<h3>Firefox AudioData.copyTo frameOffset bug on interleaved f32</h3>
<pre id="out"></pre>
<script>
const log = s => document.getElementById('out').textContent += s + '\n';

const nFrames = 8;
const nCh = 2;

// 8 frames × 2 channels, interleaved: [L0,R0, L1,R1, ... L7,R7]
const f32 = new Float32Array([
  0.1, -0.1, 0.2, -0.2, 0.3, -0.3, 0.4, -0.4,
  0.5, -0.5, 0.6, -0.6, 0.7, -0.7, 0.8, -0.8,
]);

const ad = new AudioData({
  format: 'f32',
  sampleRate: 48000,
  numberOfFrames: nFrames,
  numberOfChannels: nCh,
  timestamp: 0,
  data: f32,
});

// Read 4 frames starting at frame 4
const s16 = new Int16Array(4 * nCh);
ad.copyTo(s16, { planeIndex: 0, format: 's16', frameOffset: 4, frameCount: 4 });
ad.close();

// Expected: s16 conversion of frames 4-7 = [0.5,-0.5, 0.6,-0.6, 0.7,-0.7, 0.8,-0.8]
// Chrome produces: [16383, -16384, 19660, -19660, 22936, -22937, 26213, -26214]
//
// Firefox produces: [9830, -9830, 13107, -13107, 16384, -16384, 19660, -19660]
// which are the s16 values for frames 2-5 = [0.3,-0.3, 0.4,-0.4, 0.5,-0.5, 0.6,-0.6]
//
// Bug: Firefox treats frameOffset as a sample offset (skips 4 individual values)
//      instead of a frame offset (should skip 4 × 2-channel frames = 8 values).

const expected = [16383, -16384, 19660, -19660, 22936, -22937, 26213, -26214];
const actual = Array.from(s16);
const pass = actual.every((v, i) => Math.abs(v - expected[i]) <= 1);

log('Browser:  ' + navigator.userAgent.split(' ').pop());
log('Result:   ' + JSON.stringify(actual));
log('Expected: ' + JSON.stringify(expected));
log('');
log(pass ? 'PASS ✓' : 'FAIL ✗ — frameOffset is being treated as a sample offset, not a frame offset');
</script>
</body>
</html>

Actual results:

Works in Chrome, but fails in Firefox. Chrome's output is correct

Expected results:

The same output should have come out of Firefox.

The Bugbug bot thinks this bug should belong to the 'Core::DOM: Core & HTML' component, and is moving the bug to that component. Please correct in case you think the bot is wrong.

Component: Untriaged → DOM: Core & HTML
Product: Firefox → Core
Component: DOM: Core & HTML → Web Audio
Attached file testcase.html —

Thank you for the nice testcase. You analysis looks accurate to me.

Looks like there is a channelCount multiplier missing at https://searchfox.org/firefox-main/rev/0fce12de99c5e7d74536d8f93cf9778ce6199a1f/dom/media/webcodecs/AudioData.cpp#482

Is frameOffset missing from this case?: https://searchfox.org/firefox-main/rev/0fce12de99c5e7d74536d8f93cf9778ce6199a1f/dom/media/webcodecs/AudioData.cpp#513-521

Some of the asserts in other cases might need checking.

Severity: -- → S3
Status: UNCONFIRMED → NEW
Component: Web Audio → Audio/Video: Web Codecs
Ever confirmed: true
Flags: needinfo?(padenot)
Priority: -- → P2

Sorry I missed this. Chun-Min, can you have a look?

Flags: needinfo?(padenot) → needinfo?(cchang)

It probably has been fixed in another bug. I'll take a look

Assignee: nobody → cchang
Flags: needinfo?(cchang)
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: