Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: webcodecs

Karl Seguind800f768a165
diff --git a/src/browser/js/Local.zig b/src/browser/js/Local.zig
index 05e6ed6d6..1a00867cd 100644
--- a/src/browser/js/Local.zig
+++ b/src/browser/js/Local.zig
@@ -784,6 +784,10 @@ fn jsValueToStruct(self: *const Local, comptime T: type, js_val: js.Value) !?T {
const bytes = (try jsValueToArrayBufferSlice(u8, true, js_val)) orelse return null;
return .{ .bytes = bytes };
},
+ js.AllowSharedBufferSource => {
+ const bytes = (try jsValueToArrayBufferSlice(u8, true, js_val)) orelse return null;
+ return .{ .bytes = bytes };
+ },
js.Value => js_val,
js.Value.Global => return try js_val.persist(),
js.Object => {
diff --git a/src/browser/js/Value.zig b/src/browser/js/Value.zig
index fa231f631..de0f20dd7 100644
--- a/src/browser/js/Value.zig
+++ b/src/browser/js/Value.zig
@@ -506,6 +506,10 @@ const cloneable_types = .{
@import("../webapi/DOMPoint.zig"),
@import("../webapi/DOMRectReadOnly.zig"),
@import("../webapi/DOMRect.zig"),
+ @import("../webapi/webcodecs/encoded_chunk.zig").EncodedAudioChunk,
+ @import("../webapi/webcodecs/encoded_chunk.zig").EncodedVideoChunk,
+ @import("../webapi/webcodecs/AudioData.zig"),
+ @import("../webapi/webcodecs/VideoFrame.zig"),
};
// Passed to a type's structuredSerialize hook to write its payload into the
diff --git a/src/browser/js/bridge.zig b/src/browser/js/bridge.zig
index 27fc7054d..ba4033b3f 100644
--- a/src/browser/js/bridge.zig
+++ b/src/browser/js/bridge.zig
@@ -1340,8 +1340,27 @@ pub const PageJsApis = flattenTypes(&.{
@import("../webapi/Sanitizer.zig"),
@import("../webapi/Lock.zig"),
@import("../webapi/LockManager.zig"),
+ @import("../webapi/webcodecs/encoded_chunk.zig"),
+ @import("../webapi/webcodecs/AudioData.zig"),
+ @import("../webapi/webcodecs/VideoColorSpace.zig"),
+ @import("../webapi/webcodecs/VideoFrame.zig"),
+ @import("../webapi/webcodecs/AudioDecoder.zig"),
+ @import("../webapi/webcodecs/AudioEncoder.zig"),
+ @import("../webapi/webcodecs/VideoDecoder.zig"),
+ @import("../webapi/webcodecs/VideoEncoder.zig"),
});
+const webcodecs_apis = [_]type{
+ @import("../webapi/webcodecs/encoded_chunk.zig"),
+ @import("../webapi/webcodecs/AudioData.zig"),
+ @import("../webapi/webcodecs/VideoColorSpace.zig"),
+ @import("../webapi/webcodecs/VideoFrame.zig"),
+ @import("../webapi/webcodecs/AudioDecoder.zig"),
+ @import("../webapi/webcodecs/AudioEncoder.zig"),
+ @import("../webapi/webcodecs/VideoDecoder.zig"),
+ @import("../webapi/webcodecs/VideoEncoder.zig"),
+};
+
// APIs available on EVERY worker global — dedicated, shared and service. This
// is the WebIDL `[Exposed=Worker]` set, which covers all three. Each kind's
// snapshot context adds its own global-scope type on top; dedicated and shared
@@ -1441,7 +1460,7 @@ const worker_extended_apis = worker_common_apis ++ [_]type{
@import("../webapi/FileReaderSync.zig"),
};
-pub const DedicatedWorkerJsApis = flattenTypes(&([_]type{@import("../webapi/DedicatedWorkerGlobalScope.zig")} ++ worker_extended_apis));
+pub const DedicatedWorkerJsApis = flattenTypes(&([_]type{@import("../webapi/DedicatedWorkerGlobalScope.zig")} ++ worker_extended_apis ++ webcodecs_apis));
pub const SharedWorkerJsApis = flattenTypes(&([_]type{@import("../webapi/SharedWorkerGlobalScope.zig")} ++ worker_extended_apis));
pub const ServiceWorkerJsApis = flattenTypes(&([_]type{
diff --git a/src/browser/js/js.zig b/src/browser/js/js.zig
index 98206bd49..7df199112 100644
--- a/src/browser/js/js.zig
+++ b/src/browser/js/js.zig
@@ -182,6 +182,12 @@ pub const BufferSource = struct {
bytes: []const u8,
};
+// An ArrayBuffer or any typed array kind or a DataView, exposed as its raw bytes.
+// The underlaying value *can* be a SharedBuffer
+pub const AllowSharedBufferSource = struct {
+ bytes: []u8,
+};
+
pub const ArrayType = enum(u8) {
int8,
uint8,
diff --git a/src/browser/tests/frames/cross_realm_webcodecs_flush.html b/src/browser/tests/frames/cross_realm_webcodecs_flush.html
new file mode 100644
index 000000000..7273e244c
--- /dev/null
+++ b/src/browser/tests/frames/cross_realm_webcodecs_flush.html
@@ -0,0 +1,38 @@
+<!DOCTYPE html>
+<head></head>
+<body>
+<script src="../testing.js"></script>
+
+<!--
+A codec is created in one realm and flush() is invoked on it from another
+(iframe.contentWindow.AudioDecoder.prototype.flush.call(codec)). The promise
+must be made in the caller's realm, which is the only one with an active
+local scope at that point.
+-->
+
+<iframe id=if_codec src="support/cross_realm_collection.html"></iframe>
+
+<script id=cross_realm_codec_flush type=module>
+{
+ const state = await testing.async();
+ const iframe = document.getElementById('if_codec');
+ await new Promise((resolve) => {
+ if (iframe.contentDocument.readyState === 'complete') return resolve();
+ iframe.addEventListener('load', resolve);
+ });
+
+ const decoder = new AudioDecoder({output: () => {}, error: () => {}});
+ decoder.configure({codec: 'opus', sampleRate: 48000, numberOfChannels: 2});
+ const flush = iframe.contentWindow.AudioDecoder.prototype.flush.call(decoder);
+ const unconfigured = iframe.contentWindow.VideoEncoder.prototype.flush.call(new VideoEncoder({output: () => {}, error: () => {}}));
+ state.resolve([
+ await flush.then(() => 'resolved', (e) => e.name),
+ await unconfigured.then(() => 'resolved', (e) => e.name),
+ ]);
+
+ await state.done((names) => {
+ // the codec is unsupported, so the pending configure closes it
+ testing.expectEqual(['NotSupportedError', 'InvalidStateError'], names);
+ });
+}
+</script>
diff --git a/src/browser/tests/webcodecs/audio_data.html b/src/browser/tests/webcodecs/audio_data.html
new file mode 100644
index 000000000..ee143c9e6
--- /dev/null
+++ b/src/browser/tests/webcodecs/audio_data.html
@@ -0,0 +1,89 @@
+<!DOCTYPE html>
+<script src="../testing.js"></script>
+
+<script id=construct>
+{
+ const data = new AudioData({
+ format: 'f32-planar', sampleRate: 8000, numberOfFrames: 4, numberOfChannels: 2, timestamp: 10,
+ data: new Float32Array([1, 2, 3, 4, 5, 6, 7, 8]),
+ });
+ testing.expectEqual('f32-planar', data.format);
+ testing.expectEqual(8000, data.sampleRate);
+ testing.expectEqual(4, data.numberOfFrames);
+ testing.expectEqual(2, data.numberOfChannels);
+ testing.expectEqual(10, data.timestamp);
+ testing.expectEqual(500, data.duration);
+
+ const plane = new Float32Array(4);
+ data.copyTo(plane, {planeIndex: 1});
+ testing.expectEqual([5, 6, 7, 8], Array.from(plane));
+
+ // planar to interleaved, with an offset
+ testing.expectEqual(8, data.allocationSize({planeIndex: 0, format: 'f32', frameOffset: 3}));
+ const interleaved = new Float32Array(2);
+ data.copyTo(interleaved, {planeIndex: 0, format: 'f32', frameOffset: 3});
+ testing.expectEqual([4, 8], Array.from(interleaved));
+
+ testing.expectError('RangeError', () => data.copyTo(plane, {planeIndex: 2}));
+ testing.expectError('RangeError', () => data.copyTo(plane, {planeIndex: 0, frameOffset: 4}));
+ testing.expectError('RangeError', () => data.copyTo(new Float32Array(1), {planeIndex: 0}));
+}
+</script>
+
+<script id=invalid>
+{
+ const init = {format: 's16', sampleRate: 8000, numberOfFrames: 2, numberOfChannels: 1, timestamp: 0, data: new Int16Array(2)};
+ testing.expectError('TypeError', () => new AudioData({...init, numberOfFrames: 0}));
+ testing.expectError('TypeError', () => new AudioData({...init, sampleRate: 0}));
+ testing.expectError('TypeError', () => new AudioData({...init, data: new Int16Array(1)}));
+ testing.expectError('TypeError', () => new AudioData({...init, format: 'nope'}));
+ // a duration beyond u64 saturates
+ testing.expectEqual(2 ** 64, new AudioData({...init, sampleRate: 1e-30}).duration);
+}
+</script>
+
+<script id=conversion>
+{
+ const data = new AudioData({
+ format: 'u8', sampleRate: 8000, numberOfFrames: 3, numberOfChannels: 1, timestamp: 0,
+ data: new Uint8Array([0, 128, 255]),
+ });
+ const s16 = new Int16Array(3);
+ data.copyTo(s16, {planeIndex: 0, format: 's16'});
+ testing.expectEqual([-32768, 0, 32512], Array.from(s16));
+
+ const f32 = new Float32Array(3);
+ data.copyTo(f32, {planeIndex: 0, format: 'f32-planar'});
+ testing.expectEqual([-1, 0, 1], Array.from(f32));
+
+ const back = new AudioData({
+ format: 'f32', sampleRate: 8000, numberOfFrames: 2, numberOfChannels: 1, timestamp: 0,
+ data: new Float32Array([0.5, -2]),
+ });
+ const u8 = new Uint8Array(2);
+ back.copyTo(u8, {planeIndex: 0, format: 'u8'});
+ testing.expectEqual([191, 0], Array.from(u8));
+}
+</script>
+
+<script id=close_and_clone>
+{
+ const data = new AudioData({format: 'u8', sampleRate: 8000, numberOfFrames: 1, numberOfChannels: 1, timestamp: 3, data: new Uint8Array([42])});
+ const clone = data.clone();
+ const copy = structuredClone(data);
+ data.close();
+ testing.expectEqual(null, data.format);
+ testing.expectEqual(0, data.numberOfFrames);
+ testing.expectEqual(0, data.sampleRate);
+ testing.expectEqual(3, data.timestamp);
+ testing.expectError('InvalidStateError', () => data.clone());
+ testing.expectError('InvalidStateError', () => data.allocationSize({planeIndex: 0}));
+ data.close();
+
+ for (const other of [clone, copy]) {
+ const dest = new Uint8Array(1);
+ other.copyTo(dest, {planeIndex: 0});
+ testing.expectEqual(42, dest[0]);
+ }
+}
+</script>
diff --git a/src/browser/tests/webcodecs/codecs.html b/src/browser/tests/webcodecs/codecs.html
new file mode 100644
index 000000000..56556d9e6
--- /dev/null
+++ b/src/browser/tests/webcodecs/codecs.html
@@ -0,0 +1,104 @@
+<!DOCTYPE html>
+<script src="../testing.js"></script>
+
+<script id=unconfigured>
+{
+ const callbacks = {output: () => {}, error: () => {}};
+ for (const [Codec, queueSize] of [[AudioDecoder, 'decodeQueueSize'], [VideoDecoder, 'decodeQueueSize'], [AudioEncoder, 'encodeQueueSize'], [VideoEncoder, 'encodeQueueSize']]) {
+ const codec = new Codec(callbacks);
+ testing.expectEqual('unconfigured', codec.state);
+ testing.expectEqual(0, codec[queueSize]);
+ testing.expectError('TypeError', () => new Codec({output: () => {}}));
+ codec.reset();
+ codec.close();
+ testing.expectEqual('closed', codec.state);
+ testing.expectError('InvalidStateError', () => codec.reset());
+ testing.expectError('InvalidStateError', () => codec.close());
+ }
+}
+</script>
+
+<script id=invalid_configs>
+{
+ const decoder = new AudioDecoder({output: () => {}, error: () => {}});
+ testing.expectError('TypeError', () => decoder.configure({codec: ' ', sampleRate: 48000, numberOfChannels: 2}));
+ testing.expectError('TypeError', () => decoder.configure({codec: 'opus', sampleRate: 0, numberOfChannels: 2}));
+ testing.expectError('TypeError', () => decoder.configure({codec: 'opus', numberOfChannels: 2}));
+ testing.expectEqual('unconfigured', decoder.state);
+
+ const encoder = new AudioEncoder({output: () => {}, error: () => {}});
+ testing.expectError('TypeError', () => encoder.configure({codec: 'opus', sampleRate: 48000, numberOfChannels: 2, opus: {complexity: 11}}));
+ testing.expectError('TypeError', () => encoder.configure({codec: 'opus', sampleRate: 48000, numberOfChannels: 2, bitrate: 0}));
+
+ const video = new VideoEncoder({output: () => {}, error: () => {}});
+ testing.expectError('TypeError', () => video.configure({codec: 'vp8', width: 0, height: 10}));
+}
+</script>
+
+<script id=unsupported type=module>
+ const state = await testing.async();
+
+ const errors = [];
+ const decoder = new VideoDecoder({output: () => {}, error: (e) => errors.push([e.name, decoder.state])});
+ decoder.configure({codec: 'vp09.00.10.08'});
+ testing.expectEqual('configured', decoder.state);
+
+ testing.expectError('DataError', () => decoder.decode(new EncodedVideoChunk({type: 'delta', timestamp: 0, data: new Uint8Array(1)})));
+ decoder.decode(new EncodedVideoChunk({type: 'key', timestamp: 0, data: new Uint8Array(1)}));
+ testing.expectEqual(1, decoder.decodeQueueSize);
+ const dequeued = new Promise(resolve => decoder.ondequeue = resolve);
+
+ let flushError = null;
+ try { await decoder.flush(); } catch (e) { flushError = [e.name, errors.length]; }
+ await dequeued;
+
+ state.resolve();
+ await state.done(() => {
+ testing.expectEqual([['NotSupportedError', 'closed']], errors);
+ // the error callback runs before the flush promise's handlers
+ testing.expectEqual(['NotSupportedError', 1], flushError);
+ testing.expectEqual('closed', decoder.state);
+ testing.expectEqual(0, decoder.decodeQueueSize);
+ });
+</script>
+
+<script id=reset type=module>
+ const state = await testing.async();
+
+ const errors = [];
+ const encoder = new AudioEncoder({output: () => {}, error: (e) => errors.push(e.name)});
+ encoder.configure({codec: 'opus', sampleRate: 48000, numberOfChannels: 2});
+ const flush = encoder.flush().catch(e => e.name);
+ // drops the pending configure, so the codec isn't closed
+ encoder.reset();
+ const flushError = await flush;
+ await new Promise(resolve => setTimeout(resolve, 0));
+
+ state.resolve();
+ await state.done(() => {
+ testing.expectEqual('AbortError', flushError);
+ testing.expectEqual([], errors);
+ testing.expectEqual('unconfigured', encoder.state);
+ });
+</script>
+
+<script id=is_config_supported type=module>
+ const state = await testing.async();
+
+ const support = await AudioEncoder.isConfigSupported({codec: 'opus', sampleRate: 48000, numberOfChannels: 2, unknown: 1, opus: {complexity: 5}});
+ const video = await VideoDecoder.isConfigSupported({codec: 'avc1.42001E', description: new Uint8Array([1, 2])});
+ let invalid = null;
+ try { await VideoEncoder.isConfigSupported({codec: 'vp8'}); } catch (e) { invalid = e.name; }
+
+ state.resolve();
+ await state.done(() => {
+ testing.expectEqual(false, support.supported);
+ testing.expectEqual('opus', support.config.codec);
+ testing.expectEqual(48000, support.config.sampleRate);
+ testing.expectEqual(undefined, support.config.unknown);
+ testing.expectEqual(5, support.config.opus.complexity);
+ testing.expectEqual(false, video.supported);
+ testing.expectEqual([1, 2], Array.from(new Uint8Array(video.config.description)));
+ testing.expectEqual('TypeError', invalid);
+ });
+</script>
diff --git a/src/browser/tests/webcodecs/encoded_chunk.html b/src/browser/tests/webcodecs/encoded_chunk.html
new file mode 100644
index 000000000..d3e4f3a16
--- /dev/null
+++ b/src/browser/tests/webcodecs/encoded_chunk.html
@@ -0,0 +1,62 @@
+<!DOCTYPE html>
+<script src="../testing.js"></script>
+
+<script id=construct>
+{
+ for (const Chunk of [EncodedAudioChunk, EncodedVideoChunk]) {
+ const chunk = new Chunk({type: 'key', timestamp: -10, duration: 300, data: new Uint8Array([1, 2, 3])});
+ testing.expectEqual('key', chunk.type);
+ testing.expectEqual(-10, chunk.timestamp);
+ testing.expectEqual(300, chunk.duration);
+ testing.expectEqual(3, chunk.byteLength);
+
+ const dest = new Uint8Array(4);
+ chunk.copyTo(dest);
+ testing.expectEqual([1, 2, 3, 0], Array.from(dest));
+ testing.expectError('TypeError', () => chunk.copyTo(new Uint8Array(2)));
+
+ const delta = new Chunk({type: 'delta', timestamp: 0, data: new ArrayBuffer(0)});
+ testing.expectEqual(null, delta.duration);
+ testing.expectEqual(0, delta.byteLength);
+ }
+}
+</script>
+
+<script id=invalid>
+{
+ const data = new Uint8Array(1);
+ testing.expectError('TypeError', () => new EncodedVideoChunk({type: 'nope', timestamp: 0, data}));
+ testing.expectError('TypeError', () => new EncodedVideoChunk({type: 'key', data}));
+ // [EnforceRange]
+ testing.expectError('TypeError', () => new EncodedVideoChunk({type: 'key', timestamp: Infinity, data}));
+ testing.expectError('TypeError', () => new EncodedVideoChunk({type: 'key', timestamp: 0, duration: -1, data}));
+}
+</script>
+
+<script id=transfer>
+{
+ const buffer = new Uint8Array([7, 8]).buffer;
+ const chunk = new EncodedAudioChunk({type: 'key', timestamp: 0, data: buffer, transfer: [buffer]});
+ testing.expectEqual(true, buffer.detached);
+ testing.expectEqual(2, chunk.byteLength);
+
+ const other = new ArrayBuffer(1);
+ testing.expectError('DataCloneError', () => new EncodedAudioChunk({type: 'key', timestamp: 0, data: other, transfer: [other, other]}));
+ testing.expectEqual(false, other.detached);
+ testing.expectError('DataCloneError', () => new EncodedAudioChunk({type: 'key', timestamp: 0, data: other, transfer: [buffer]}));
+}
+</script>
+
+<script id=clone>
+{
+ const chunk = new EncodedVideoChunk({type: 'delta', timestamp: 5, duration: 6, data: new Uint8Array([9])});
+ const copy = structuredClone(chunk);
+ testing.expectEqual(true, copy instanceof EncodedVideoChunk);
+ testing.expectEqual('delta', copy.type);
+ testing.expectEqual(5, copy.timestamp);
+ testing.expectEqual(6, copy.duration);
+ const dest = new Uint8Array(1);
+ copy.copyTo(dest);
+ testing.expectEqual(9, dest[0]);
+}
+</script>
diff --git a/src/browser/tests/webcodecs/video_color_space.html b/src/browser/tests/webcodecs/video_color_space.html
new file mode 100644
index 000000000..5d2d11eee
--- /dev/null
+++ b/src/browser/tests/webcodecs/video_color_space.html
@@ -0,0 +1,26 @@
+<!DOCTYPE html>
+<script src="../testing.js"></script>
+
+<script id=defaults>
+{
+ const cs = new VideoColorSpace();
+ testing.expectEqual(null, cs.primaries);
+ testing.expectEqual(null, cs.transfer);
+ testing.expectEqual(null, cs.matrix);
+ testing.expectEqual(null, cs.fullRange);
+ testing.expectEqual('{"primaries":null,"transfer":null,"matrix":null,"fullRange":null}', JSON.stringify(cs));
+}
+</script>
+
+<script id=values>
+{
+ const cs = new VideoColorSpace({primaries: 'bt2020', transfer: 'iec61966-2-1', matrix: 'bt2020-ncl', fullRange: true});
+ testing.expectEqual('bt2020', cs.primaries);
+ testing.expectEqual('iec61966-2-1', cs.transfer);
+ testing.expectEqual('bt2020-ncl', cs.matrix);
+ testing.expectEqual(true, cs.fullRange);
+ testing.expectEqual({primaries: 'bt2020', transfer: 'iec61966-2-1', matrix: 'bt2020-ncl', fullRange: true}, cs.toJSON());
+
+ testing.expectError('TypeError', () => new VideoColorSpace({primaries: 'nope'}));
+}
+</script>
diff --git a/src/browser/tests/webcodecs/video_frame.html b/src/browser/tests/webcodecs/video_frame.html
new file mode 100644
index 000000000..f21c37e71
--- /dev/null
+++ b/src/browser/tests/webcodecs/video_frame.html
@@ -0,0 +1,145 @@
+<!DOCTYPE html>
+<script src="../testing.js"></script>
+
+<script id=i420>
+{
+ // 4x2 I420: Y is 4x2, U and V 2x1
+ const data = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
+ const frame = new VideoFrame(data, {format: 'I420', timestamp: 5, codedWidth: 4, codedHeight: 2});
+ testing.expectEqual('I420', frame.format);
+ testing.expectEqual(4, frame.codedWidth);
+ testing.expectEqual(2, frame.codedHeight);
+ testing.expectEqual(4, frame.displayWidth);
+ testing.expectEqual(5, frame.timestamp);
+ testing.expectEqual(null, frame.duration);
+ const rect = frame.visibleRect;
+ testing.expectEqual([0, 0, 4, 2], [rect.x, rect.y, rect.width, rect.height]);
+ testing.expectEqual(true, rect instanceof DOMRectReadOnly);
+ // REC709 when no colorSpace is given
+ testing.expectEqual('bt709', frame.colorSpace.matrix);
+ testing.expectEqual(12, frame.allocationSize());
+ testing.expectEqual({}, frame.metadata());
+}
+</script>
+
+<script id=copy_to type=module>
+ const state = await testing.async();
+ const data = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
+ const frame = new VideoFrame(data, {format: 'I420', timestamp: 0, codedWidth: 4, codedHeight: 2});
+
+ const dest = new Uint8Array(20).fill(0xff);
+ const layout = await frame.copyTo(dest, {layout: [{offset: 0, stride: 5}, {offset: 12, stride: 2}, {offset: 16, stride: 3}]});
+ let error = null;
+ try { await frame.copyTo(new Uint8Array(11)); } catch (e) { error = e; }
+
+ state.resolve();
+ await state.done(() => {
+ testing.expectEqual([{offset: 0, stride: 5}, {offset: 12, stride: 2}, {offset: 16, stride: 3}], layout);
+ testing.expectEqual([1, 2, 3, 4, 0xff, 5, 6, 7, 8, 0xff, 0xff, 0xff, 9, 10, 0xff, 0xff, 11, 12, 0xff, 0xff], Array.from(dest));
+ testing.expectEqual('TypeError', error.name);
+ });
+</script>
+
+<script id=visible_rect type=module>
+ const state = await testing.async();
+ // The rect is cropped out of the coded frame, and becomes the coded size.
+ const data = new Uint8Array(16);
+ for (let i = 0; i < 16; i++) data[i] = i;
+ const frame = new VideoFrame(data, {format: 'RGBA', timestamp: 0, codedWidth: 2, codedHeight: 2, visibleRect: {x: 1, y: 1, width: 1, height: 1}});
+ const dest = new Uint8Array(4);
+ await frame.copyTo(dest);
+
+ state.resolve();
+ await state.done(() => {
+ testing.expectEqual(1, frame.codedWidth);
+ testing.expectEqual(0, frame.visibleRect.x);
+ testing.expectEqual([12, 13, 14, 15], Array.from(dest));
+ });
+</script>
+
+<script id=rgb_swizzle type=module>
+ const state = await testing.async();
+ const frame = new VideoFrame(new Uint8Array([1, 2, 3, 4]), {format: 'RGBA', timestamp: 0, codedWidth: 1, codedHeight: 1});
+ const bgrx = new Uint8Array(4);
+ await frame.copyTo(bgrx, {format: 'BGRX'});
+ const i420 = new VideoFrame(new Uint8Array(6), {format: 'I420', timestamp: 0, codedWidth: 2, codedHeight: 2});
+ let error = null;
+ try { await i420.copyTo(new Uint8Array(16), {format: 'RGBA'}); } catch (e) { error = e; }
+
+ state.resolve();
+ await state.done(() => {
+ testing.expectEqual([3, 2, 1, 255], Array.from(bgrx));
+ // no YUV to RGB conversion
+ testing.expectEqual('NotSupportedError', error.name);
+ });
+</script>
+
+<script id=invalid>
+{
+ const data = new Uint8Array(12);
+ const init = {format: 'I420', timestamp: 0, codedWidth: 4, codedHeight: 2};
+ testing.expectError('TypeError', () => new VideoFrame(data, {...init, codedWidth: 0}));
+ testing.expectError('TypeError', () => new VideoFrame(new Uint8Array(11), init));
+ // x must be a multiple of the chroma sub-sampling
+ testing.expectError('TypeError', () => new VideoFrame(data, {...init, visibleRect: {x: 1, y: 0, width: 2, height: 2}}));
+ testing.expectError('TypeError', () => new VideoFrame(data, {...init, displayWidth: 4}));
+ // rect members beyond u32 are rejected, not converted
+ testing.expectError('TypeError', () => new VideoFrame(data, {...init, visibleRect: {x: 1e300, y: 0, width: 2, height: 2}}));
+ testing.expectError('TypeError', () => new VideoFrame(data, init).allocationSize({rect: {x: 0, y: 0, width: 2 ** 64, height: 2}}));
+ testing.expectError('TypeError', () => new VideoFrame(data));
+ testing.expectError('TypeError', () => new VideoFrame({}));
+ // we don't decode images
+ testing.expectError('InvalidStateError', () => new VideoFrame(document.createElement('img'), {timestamp: 0}));
+}
+</script>
+
+<script id=from_frame>
+{
+ const frame = new VideoFrame(new Uint8Array(32), {format: 'RGBA', timestamp: 7, duration: 1, codedWidth: 4, codedHeight: 2, rotation: 91});
+ testing.expectEqual(90, frame.rotation);
+ // exact modulo, as Chrome's fmod: 1e300 is a multiple of 360
+ testing.expectEqual(90, new VideoFrame(frame, {rotation: 1e300}).rotation);
+ testing.expectEqual(270, new VideoFrame(frame, {rotation: -179}).rotation);
+ testing.expectEqual(2, frame.displayWidth);
+ testing.expectEqual(4, frame.displayHeight);
+
+ const copy = new VideoFrame(frame, {alpha: 'discard', timestamp: 8});
+ testing.expectEqual('RGBX', copy.format);
+ testing.expectEqual(8, copy.timestamp);
+ testing.expectEqual(1, copy.duration);
+ testing.expectEqual(90, copy.rotation);
+
+ const clone = frame.clone();
+ const serialized = structuredClone(frame);
+ frame.close();
+ testing.expectEqual(null, frame.format);
+ testing.expectEqual(0, frame.codedWidth);
+ testing.expectEqual(null, frame.visibleRect);
+ testing.expectEqual(null, frame.colorSpace.matrix);
+ testing.expectEqual(7, frame.timestamp);
+ testing.expectError('InvalidStateError', () => frame.clone());
+ testing.expectError('InvalidStateError', () => new VideoFrame(frame));
+
+ testing.expectEqual('RGBA', clone.format);
+ testing.expectEqual(4, clone.codedWidth);
+ testing.expectEqual('RGBA', serialized.format);
+ testing.expectEqual(90, serialized.rotation);
+ testing.expectEqual('rgb', serialized.colorSpace.matrix);
+}
+</script>
+
+<script id=canvas>
+{
+ const canvas = new OffscreenCanvas(3, 2);
+ testing.expectError('TypeError', () => new VideoFrame(canvas));
+ const frame = new VideoFrame(canvas, {timestamp: 1});
+ testing.expectEqual('RGBA', frame.format);
+ testing.expectEqual(3, frame.codedWidth);
+ testing.expectEqual(2, frame.displayHeight);
+ testing.expectEqual('RGBX', new VideoFrame(canvas, {timestamp: 1, alpha: 'discard'}).format);
+ testing.expectError('InvalidStateError', () => new VideoFrame(new OffscreenCanvas(0, 2), {timestamp: 1}));
+ // beyond Chrome's canvas limits: no bitmap to take, and no 40GB frame here
+ testing.expectError('InvalidStateError', () => new VideoFrame(new OffscreenCanvas(100000, 100000), {timestamp: 1}));
+ testing.expectError('InvalidStateError', () => new VideoFrame(new OffscreenCanvas(32767, 32767), {timestamp: 1}));
+}
+</script>
diff --git a/src/browser/webapi/EventTarget.zig b/src/browser/webapi/EventTarget.zig
index d773688fb..34172fafa 100644
--- a/src/browser/webapi/EventTarget.zig
+++ b/src/browser/webapi/EventTarget.zig
@@ -59,6 +59,11 @@ const TextTrackList = @import("media/TextTrackList.zig");
const Animation = @import("animation/Animation.zig");
+const AudioDecoder = @import("webcodecs/AudioDecoder.zig");
+const AudioEncoder = @import("webcodecs/AudioEncoder.zig");
+const VideoDecoder = @import("webcodecs/VideoDecoder.zig");
+const VideoEncoder = @import("webcodecs/VideoEncoder.zig");
+
const Navigation = @import("navigation/Navigation.zig");
const NavigationHistoryEntry = @import("navigation/NavigationHistoryEntry.zig");
const XMLHttpRequestEventTarget = @import("net/XMLHttpRequestEventTarget.zig");
@@ -74,6 +79,8 @@ _type: Type,
pub const Type = enum(u8) {
abort_signal,
animation,
+ audio_decoder,
+ audio_encoder,
broadcast_channel,
cookie_store,
event_source,
@@ -99,6 +106,8 @@ pub const Type = enum(u8) {
text_track,
text_track_cue,
text_track_list,
+ video_decoder,
+ video_encoder,
visual_viewport,
websocket,
window,
@@ -113,6 +122,8 @@ pub fn Subtype(comptime tag: Type) type {
return switch (tag) {
.abort_signal => AbortSignal,
.animation => Animation,
+ .audio_decoder => AudioDecoder,
+ .audio_encoder => AudioEncoder,
.broadcast_channel => BroadcastChannel,
.cookie_store => CookieStore,
.event_source => EventSource,
@@ -138,6 +149,8 @@ pub fn Subtype(comptime tag: Type) type {
.text_track => TextTrack,
.text_track_cue => TextTrackCue,
.text_track_list => TextTrackList,
+ .video_decoder => VideoDecoder,
+ .video_encoder => VideoEncoder,
.visual_viewport => VisualViewport,
.websocket => WebSocket,
.window => Window,
@@ -372,6 +385,10 @@ pub fn format(self: *EventTarget, writer: *std.Io.Writer) !void {
.xhr => writer.writeAll("<XMLHttpRequestEventTarget>"),
.abort_signal => writer.writeAll("<AbortSignal>"),
.animation => writer.writeAll("<Animation>"),
+ .audio_decoder => writer.writeAll("<AudioDecoder>"),
+ .audio_encoder => writer.writeAll("<AudioEncoder>"),
+ .video_decoder => writer.writeAll("<VideoDecoder>"),
+ .video_encoder => writer.writeAll("<VideoEncoder>"),
.media_query_list => writer.writeAll("<MediaQueryList>"),
.message_port => writer.writeAll("<MessagePort>"),
.broadcast_channel => writer.writeAll("<BroadcastChannel>"),
@@ -399,6 +416,8 @@ pub fn toString(self: *EventTarget) []const u8 {
return switch (self._type) {
.abort_signal => return "[object AbortSignal]",
.animation => return "[object Animation]",
+ .audio_decoder => return "[object AudioDecoder]",
+ .audio_encoder => return "[object AudioEncoder]",
.broadcast_channel => return "[object BroadcastChannel]",
.cookie_store => return "[object CookieStore]",
.event_source => return "[object EventSource]",
@@ -424,6 +443,8 @@ pub fn toString(self: *EventTarget) []const u8 {
.text_track => return "[object TextTrack]",
.text_track_cue => return "[object TextTrackCue]",
.text_track_list => return "[object TextTrackList]",
+ .video_decoder => return "[object VideoDecoder]",
+ .video_encoder => return "[object VideoEncoder]",
.visual_viewport => return "[object VisualViewport]",
.websocket => return "[object WebSocket]",
.window => return "[object Window]",
diff --git a/src/browser/webapi/webcodecs/AudioData.zig b/src/browser/webapi/webcodecs/AudioData.zig
new file mode 100644
index 000000000..f8de12a24
--- /dev/null
+++ b/src/browser/webapi/webcodecs/AudioData.zig
@@ -0,0 +1,472 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+// https://w3c.github.io/webcodecs/#audiodata-interface
+
+const std = @import("std");
+const lp = @import("lightpanda");
+
+const js = @import("../../js/js.zig");
+const Page = @import("../../Page.zig");
+const common = @import("common.zig");
+
+const Execution = js.Execution;
+
+const AudioData = @This();
+
+_rc: lp.RC = .{},
+_arena: *lp.Arena,
+_data_arena: ?*lp.Arena, // separate so that close() can immediately free
+_data: []const u8,
+_format: ?Format, // null once closed
+_sample_rate: f32,
+_number_of_frames: u32,
+_number_of_channels: u32,
+_timestamp: i64,
+
+pub const Format = enum {
+ u8,
+ s16,
+ s32,
+ f32,
+ @"u8-planar",
+ @"s16-planar",
+ @"s32-planar",
+ @"f32-planar",
+
+ pub fn bytesPerSample(self: Format) u32 {
+ return switch (self) {
+ .u8, .@"u8-planar" => 1,
+ .s16, .@"s16-planar" => 2,
+ .s32, .@"s32-planar", .f32, .@"f32-planar" => 4,
+ };
+ }
+
+ pub fn isPlanar(self: Format) bool {
+ return switch (self) {
+ .u8, .s16, .s32, .f32 => false,
+ else => true,
+ };
+ }
+};
+
+const Init = struct {
+ data: js.AllowSharedBufferSource,
+ format: []const u8,
+ numberOfChannels: f64,
+ numberOfFrames: f64,
+ sampleRate: f64,
+ timestamp: f64,
+ transfer: []const js.Value = &.{},
+};
+
+fn init(init_: Init, exec: *const Execution) !*AudioData {
+ const format = std.meta.stringToEnum(Format, init_.format) orelse return error.TypeError;
+ const frames = try common.enforceRange(u32, init_.numberOfFrames);
+ const channels = try common.enforceRange(u32, init_.numberOfChannels);
+ const timestamp = try common.enforceRange(i64, init_.timestamp);
+ if (std.math.isFinite(init_.sampleRate) == false) {
+ return error.TypeError;
+ }
+ const sample_rate: f32 = @floatCast(init_.sampleRate);
+
+ if (sample_rate <= 0 or frames == 0 or channels == 0) {
+ return error.TypeError;
+ }
+ const total_size = @as(u64, frames) * channels * format.bytesPerSample();
+ if (init_.data.bytes.len < total_size) {
+ return error.TypeError;
+ }
+
+ const transfer: common.Transfer = .{ .buffers = init_.transfer };
+ try transfer.check();
+
+ const self = try create(.{
+ .format = format,
+ .sample_rate = sample_rate,
+ .number_of_frames = frames,
+ .number_of_channels = channels,
+ .timestamp = timestamp,
+ }, init_.data.bytes[0..@intCast(total_size)], exec.page);
+ errdefer self.deinit(exec.page);
+
+ try transfer.detach();
+ return self;
+}
+
+const Attributes = struct {
+ format: Format,
+ sample_rate: f32,
+ number_of_frames: u32,
+ number_of_channels: u32,
+ timestamp: i64,
+};
+
+fn create(attributes: Attributes, data: []const u8, page: *Page) !*AudioData {
+ const arena = try page.getPinnedArena(.tiny, "AudioData");
+ errdefer arena.release();
+
+ const data_arena = try page.getPinnedArena(data.len, "AudioData.data");
+ errdefer data_arena.release();
+
+ const self = try arena.create(AudioData);
+ self.* = .{
+ ._arena = arena,
+ ._data_arena = data_arena,
+ ._data = try data_arena.dupe(u8, data),
+ ._format = attributes.format,
+ ._sample_rate = attributes.sample_rate,
+ ._number_of_frames = attributes.number_of_frames,
+ ._number_of_channels = attributes.number_of_channels,
+ ._timestamp = attributes.timestamp,
+ };
+ data_arena.report();
+ return self;
+}
+
+pub fn deinit(self: *AudioData, _: *Page) void {
+ if (self._data_arena) |data_arena| {
+ data_arena.release();
+ }
+ self._arena.release();
+}
+
+pub fn acquireRef(self: *AudioData) void {
+ self._rc.acquire();
+}
+
+pub fn releaseRef(self: *AudioData, page: *Page) void {
+ self._rc.release(self, page);
+}
+
+pub fn isClosed(self: *const AudioData) bool {
+ return self._format == null;
+}
+
+fn getFormat(self: *const AudioData) ?[]const u8 {
+ const format = self._format orelse return null;
+ return @tagName(format);
+}
+
+fn getSampleRate(self: *const AudioData) f32 {
+ return self._sample_rate;
+}
+
+fn getNumberOfFrames(self: *const AudioData) u32 {
+ return self._number_of_frames;
+}
+
+fn getNumberOfChannels(self: *const AudioData) u32 {
+ return self._number_of_channels;
+}
+
+fn getTimestamp(self: *const AudioData) i64 {
+ return self._timestamp;
+}
+
+fn getDuration(self: *const AudioData) u64 {
+ if (self._sample_rate == 0) {
+ return 0;
+ }
+ const seconds = @as(f64, @floatFromInt(self._number_of_frames)) / @as(f64, self._sample_rate);
+ const micros = seconds * 1_000_000;
+ // The sample rate only has to be positive, so this can exceed a u64.
+ if (micros >= 0x1p64) {
+ return std.math.maxInt(u64);
+ }
+ return @intFromFloat(micros);
+}
+
+const CopyToOptions = struct {
+ format: ?[]const u8 = null,
+ frameCount: ?f64 = null,
+ frameOffset: f64 = 0,
+ planeIndex: f64,
+};
+
+// The parsed options, after "Compute Copy Element Count".
+const Copy = struct {
+ format: Format,
+ plane_index: u32,
+ frame_offset: u32,
+ frame_count: u32,
+ element_count: u64,
+
+ fn byteLength(self: Copy) u64 {
+ return self.element_count * self.format.bytesPerSample();
+ }
+};
+
+fn computeCopy(self: *const AudioData, options: CopyToOptions) !Copy {
+ const source_format = self._format orelse return error.InvalidStateError;
+ const plane_index = try common.enforceRange(u32, options.planeIndex);
+ const frame_offset = try common.enforceRange(u32, options.frameOffset);
+ const requested_count = try common.optionalEnforceRange(u32, options.frameCount);
+
+ const format = if (options.format) |f|
+ std.meta.stringToEnum(Format, f) orelse return error.TypeError
+ else
+ source_format;
+
+ if (format.isPlanar()) {
+ if (plane_index >= self._number_of_channels) return error.RangeError;
+ } else if (plane_index > 0) {
+ return error.RangeError;
+ }
+
+ // Every plane holds every frame, whatever the arrangement.
+ const frame_count = self._number_of_frames;
+ if (frame_offset >= frame_count) {
+ return error.RangeError;
+ }
+ var copy_count = frame_count - frame_offset;
+ if (requested_count) |count| {
+ if (count > copy_count) return error.RangeError;
+ copy_count = count;
+ }
+
+ var element_count: u64 = copy_count;
+ if (format.isPlanar() == false) {
+ element_count *= self._number_of_channels;
+ }
+
+ return .{
+ .format = format,
+ .plane_index = plane_index,
+ .frame_offset = frame_offset,
+ .frame_count = copy_count,
+ .element_count = element_count,
+ };
+}
+
+fn allocationSize(self: *const AudioData, options: CopyToOptions) !u32 {
+ const copy = try self.computeCopy(options);
+ return std.math.cast(u32, copy.byteLength()) orelse error.RangeError;
+}
+
+fn copyTo(self: *const AudioData, destination: js.AllowSharedBufferSource, options: CopyToOptions) !void {
+ const copy = try self.computeCopy(options);
+ const byte_length = copy.byteLength();
+ if (byte_length > destination.bytes.len) {
+ return error.RangeError;
+ }
+
+ const source_format = self._format.?;
+ const channels = self._number_of_channels;
+ const frames = self._number_of_frames;
+ const dest = destination.bytes[0..@intCast(byte_length)];
+
+ // Same format, and the copied range is contiguous in the source.
+ if (source_format == copy.format) {
+ const bps = source_format.bytesPerSample();
+ const start: usize = if (source_format.isPlanar())
+ (@as(usize, copy.plane_index) * frames + copy.frame_offset) * bps
+ else
+ @as(usize, copy.frame_offset) * channels * bps;
+ @memcpy(dest, self._data[start..][0..dest.len]);
+ return;
+ }
+
+ var out: usize = 0;
+ for (0..copy.frame_count) |i| {
+ const frame = copy.frame_offset + i;
+ if (copy.format.isPlanar()) {
+ convertSample(self, frame, copy.plane_index, copy.format, dest, out);
+ out += 1;
+ } else {
+ for (0..channels) |channel| {
+ convertSample(self, frame, channel, copy.format, dest, out);
+ out += 1;
+ }
+ }
+ }
+}
+
+fn convertSample(self: *const AudioData, frame: usize, channel: usize, dest_format: Format, dest: []u8, index: usize) void {
+ const source_format = self._format.?;
+ const source_index = if (source_format.isPlanar())
+ channel * self._number_of_frames + frame
+ else
+ frame * self._number_of_channels + channel;
+
+ const source = Sample.read(source_format, self._data, source_index);
+ source.write(dest_format, dest, index);
+}
+
+const Sample = union(enum) {
+ u8: u8,
+ s16: i16,
+ s32: i32,
+ f32: f32,
+
+ fn read(format: Format, data: []const u8, index: usize) Sample {
+ return switch (format) {
+ .u8, .@"u8-planar" => .{ .u8 = data[index] },
+ .s16, .@"s16-planar" => .{ .s16 = std.mem.readInt(i16, data[index * 2 ..][0..2], .little) },
+ .s32, .@"s32-planar" => .{ .s32 = std.mem.readInt(i32, data[index * 4 ..][0..4], .little) },
+ .f32, .@"f32-planar" => .{ .f32 = @bitCast(std.mem.readInt(u32, data[index * 4 ..][0..4], .little)) },
+ };
+ }
+
+ fn write(self: Sample, format: Format, dest: []u8, index: usize) void {
+ switch (format) {
+ .u8, .@"u8-planar" => dest[index] = switch (self) {
+ .f32 => |f| fromFloat(u8, f),
+ else => @intCast((self.scaled() >> 24) + 128),
+ },
+ .s16, .@"s16-planar" => std.mem.writeInt(i16, dest[index * 2 ..][0..2], switch (self) {
+ .f32 => |f| fromFloat(i16, f),
+ else => @intCast(self.scaled() >> 16),
+ }, .little),
+ .s32, .@"s32-planar" => std.mem.writeInt(i32, dest[index * 4 ..][0..4], switch (self) {
+ .f32 => |f| fromFloat(i32, f),
+ else => self.scaled(),
+ }, .little),
+ .f32, .@"f32-planar" => std.mem.writeInt(u32, dest[index * 4 ..][0..4], @bitCast(self.toFloat()), .little),
+ }
+ }
+
+ // An integer sample at s32 scale.
+ fn scaled(self: Sample) i32 {
+ return switch (self) {
+ .u8 => |v| (@as(i32, v) - 128) << 24,
+ .s16 => |v| @as(i32, v) << 16,
+ .s32 => |v| v,
+ .f32 => unreachable,
+ };
+ }
+
+ fn toFloat(self: Sample) f32 {
+ return switch (self) {
+ .u8 => |v| toFloatFrom(u8, v),
+ .s16 => |v| toFloatFrom(i16, v),
+ .s32 => |v| toFloatFrom(i32, v),
+ .f32 => |v| v,
+ };
+ }
+};
+
+fn Range(comptime T: type) type {
+ return struct {
+ const min: f64 = @floatFromInt(std.math.minInt(T));
+ const max: f64 = @floatFromInt(std.math.maxInt(T));
+ // the "bias": silence
+ const zero: f64 = if (T == u8) 128 else 0;
+ };
+}
+
+fn toFloatFrom(comptime T: type, value: T) f32 {
+ const R = Range(T);
+ const v: f64 = @floatFromInt(value);
+ const result = if (v < R.zero) (v - R.zero) / (R.zero - R.min) else (v - R.zero) / (R.max - R.zero);
+ return @floatCast(result);
+}
+
+fn fromFloat(comptime T: type, value: f32) T {
+ const R = Range(T);
+ const f: f64 = if (std.math.isNan(value)) 0 else std.math.clamp(@as(f64, value), -1, 1);
+ const result = if (f < 0) f * (R.zero - R.min) + R.zero else f * (R.max - R.zero) + R.zero;
+ return @intFromFloat(result);
+}
+
+fn clone(self: *const AudioData, exec: *const Execution) !*AudioData {
+ const format = self._format orelse return error.InvalidStateError;
+ return create(.{
+ .format = format,
+ .sample_rate = self._sample_rate,
+ .number_of_frames = self._number_of_frames,
+ .number_of_channels = self._number_of_channels,
+ .timestamp = self._timestamp,
+ }, self._data, exec.page);
+}
+
+pub fn close(self: *AudioData) void {
+ if (self._data_arena) |data_arena| {
+ data_arena.release();
+ self._data_arena = null;
+ }
+ // once closed, everything reads as zero/null
+ self._data = &.{};
+ self._format = null;
+ self._sample_rate = 0;
+ self._number_of_frames = 0;
+ self._number_of_channels = 0;
+}
+
+pub fn structuredSerialize(self: *const AudioData, writer: *js.StructuredWriter) !void {
+ const format = self._format orelse return error.DataClone;
+ writer.writeUint32(@backingInt(format));
+ writer.writeUint32(@bitCast(self._sample_rate));
+ writer.writeUint32(self._number_of_frames);
+ writer.writeUint32(self._number_of_channels);
+ writer.writeUint64(@bitCast(self._timestamp));
+ writer.writeBytes(self._data);
+}
+
+pub fn structuredDeserialize(reader: *js.StructuredReader, page: *Page) !*AudioData {
+ const format = std.enums.fromInt(Format, try reader.readUint32()) orelse return error.DataClone;
+ const sample_rate: f32 = @bitCast(try reader.readUint32());
+ const frames = try reader.readUint32();
+ const channels = try reader.readUint32();
+ const timestamp: i64 = @bitCast(try reader.readUint64());
+ const data = try reader.readBytes();
+ return create(.{
+ .format = format,
+ .sample_rate = sample_rate,
+ .number_of_frames = frames,
+ .number_of_channels = channels,
+ .timestamp = timestamp,
+ }, data, page);
+}
+
+pub const JsApi = struct {
+ pub const bridge = js.Bridge(AudioData);
+
+ pub const Meta = struct {
+ pub const name = "AudioData";
+ pub const prototype_chain = bridge.prototypeChain();
+ pub var class_id: bridge.ClassId = undefined;
+ };
+
+ pub const constructor = bridge.constructor(AudioData.init, .{});
+ pub const format = bridge.accessor(AudioData.getFormat, null, .{});
+ pub const sampleRate = bridge.accessor(AudioData.getSampleRate, null, .{});
+ pub const numberOfFrames = bridge.accessor(AudioData.getNumberOfFrames, null, .{});
+ pub const numberOfChannels = bridge.accessor(AudioData.getNumberOfChannels, null, .{});
+ pub const duration = bridge.accessor(AudioData.getDuration, null, .{});
+ pub const timestamp = bridge.accessor(AudioData.getTimestamp, null, .{});
+ pub const allocationSize = bridge.function(AudioData.allocationSize, .{});
+ pub const copyTo = bridge.function(AudioData.copyTo, .{});
+ pub const clone = bridge.function(AudioData.clone, .{});
+ pub const close = bridge.function(AudioData.close, .{});
+};
+
+const testing = @import("../../../testing.zig");
+test "WebApi: AudioData" {
+ try testing.htmlRunner("webcodecs/audio_data.html", .{});
+}
+
+test "AudioData: sample conversion" {
+ const t = std.testing;
+ try t.expectEqual(@as(u8, 191), fromFloat(u8, 0.5));
+ try t.expectEqual(@as(u8, 64), fromFloat(u8, -0.5));
+ try t.expectEqual(@as(i16, 16383), fromFloat(i16, 0.5));
+ try t.expectEqual(@as(i32, 2147483647), fromFloat(i32, 1.0));
+ try t.expectEqual(@as(f32, -1), toFloatFrom(u8, 0));
+ try t.expectEqual(@as(f32, 1), toFloatFrom(i16, 32767));
+}
diff --git a/src/browser/webapi/webcodecs/AudioDecoder.zig b/src/browser/webapi/webcodecs/AudioDecoder.zig
new file mode 100644
index 000000000..e7732eb9e
--- /dev/null
+++ b/src/browser/webapi/webcodecs/AudioDecoder.zig
@@ -0,0 +1,158 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+// https://w3c.github.io/webcodecs/#audiodecoder-interface
+
+const std = @import("std");
+const lp = @import("lightpanda");
+
+const js = @import("../../js/js.zig");
+const Page = @import("../../Page.zig");
+const codec = @import("codec.zig");
+const common = @import("common.zig");
+
+const EventTarget = @import("../EventTarget.zig");
+const EncodedAudioChunk = @import("encoded_chunk.zig").EncodedAudioChunk;
+
+const Execution = js.Execution;
+
+const AudioDecoder = @This();
+
+pub const Proto = EventTarget;
+
+_proto: *EventTarget,
+_rc: lp.RC = .{},
+_arena: *lp.Arena,
+_core: codec.Core(AudioDecoder),
+
+pub const Config = struct {
+ codec: []const u8,
+ description: ?js.Value = null,
+ numberOfChannels: f64,
+ sampleRate: f64,
+
+ // "valid AudioDecoderConfig", plus the Opus registration's check.
+ pub fn isValid(self: Config) bool {
+ if (codec.isEmptyCodec(self.codec)) return false;
+ if (self.description) |d| {
+ if (codec.isDetachedBuffer(d)) return false;
+ }
+ const channels = common.enforceRange(u32, self.numberOfChannels) catch return false;
+ const sample_rate = common.enforceRange(u32, self.sampleRate) catch return false;
+ if (channels == 0 or sample_rate == 0) return false;
+ if (std.mem.eql(u8, self.codec, "opus") and channels > 2 and self.description == null) return false;
+ return true;
+ }
+};
+
+fn init(callbacks: codec.Init, exec: *Execution) !*AudioDecoder {
+ const arena = try exec.getArena(.tiny, "AudioDecoder");
+ errdefer arena.release();
+ return exec._factory.eventTargetWithAllocator(arena.allocator(), AudioDecoder{
+ ._proto = undefined,
+ ._arena = arena,
+ ._core = .init(callbacks, exec),
+ });
+}
+
+pub fn deinit(self: *AudioDecoder, page: *Page) void {
+ self._core.deinit();
+ page.removeTarget(self.asEventTarget());
+ self._arena.release();
+}
+
+pub fn acquireRef(self: *AudioDecoder) void {
+ self._rc.acquire();
+}
+
+pub fn releaseRef(self: *AudioDecoder, page: *Page) void {
+ self._rc.release(self, page);
+}
+
+pub fn asEventTarget(self: *AudioDecoder) *EventTarget {
+ return self._proto;
+}
+
+fn getState(self: *const AudioDecoder) []const u8 {
+ return self._core.getState();
+}
+
+fn getDecodeQueueSize(self: *const AudioDecoder) u32 {
+ return self._core.queue_size;
+}
+
+fn configure(self: *AudioDecoder, config: Config) !void {
+ if (config.isValid() == false) {
+ return error.TypeError;
+ }
+ return self._core.configure(true);
+}
+
+fn decode(self: *AudioDecoder, chunk: *EncodedAudioChunk) !void {
+ const core = &self._core;
+ if (core.state != .configured) {
+ return error.InvalidStateError;
+ }
+ if (core.key_chunk_required) {
+ if (chunk._type != .key) return error.DataError;
+ core.key_chunk_required = false;
+ }
+ return core.enqueue();
+}
+
+fn flush(self: *AudioDecoder, exec: *const Execution) !js.Promise {
+ return self._core.flush(true, exec);
+}
+
+fn reset(self: *AudioDecoder) !void {
+ return self._core.reset();
+}
+
+fn close(self: *AudioDecoder) !void {
+ return self._core.close();
+}
+
+fn isConfigSupported(config: js.Value, exec: *Execution) !js.Promise {
+ return codec.isConfigSupported(Config, config, exec);
+}
+
+pub const JsApi = struct {
+ pub const bridge = js.Bridge(AudioDecoder);
+
+ pub const Meta = struct {
+ pub const name = "AudioDecoder";
+ pub const prototype_chain = bridge.prototypeChain();
+ pub var class_id: bridge.ClassId = undefined;
+ };
+
+ pub const constructor = bridge.constructor(AudioDecoder.init, .{});
+ pub const state = bridge.accessor(AudioDecoder.getState, null, .{});
+ pub const decodeQueueSize = bridge.accessor(AudioDecoder.getDecodeQueueSize, null, .{});
+ pub const ondequeue = EventTarget.handlerAccessor(AudioDecoder, "dequeue");
+ pub const configure = bridge.function(AudioDecoder.configure, .{});
+ pub const decode = bridge.function(AudioDecoder.decode, .{});
+ pub const flush = bridge.function(AudioDecoder.flush, .{});
+ pub const reset = bridge.function(AudioDecoder.reset, .{});
+ pub const close = bridge.function(AudioDecoder.close, .{});
+ pub const isConfigSupported = bridge.function(AudioDecoder.isConfigSupported, .{ .static = true });
+};
+
+const testing = @import("../../../testing.zig");
+test "WebApi: codecs" {
+ try testing.htmlRunner("webcodecs/codecs.html", .{});
+}
diff --git a/src/browser/webapi/webcodecs/AudioEncoder.zig b/src/browser/webapi/webcodecs/AudioEncoder.zig
new file mode 100644
index 000000000..671f418a8
--- /dev/null
+++ b/src/browser/webapi/webcodecs/AudioEncoder.zig
@@ -0,0 +1,207 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+// https://w3c.github.io/webcodecs/#audioencoder-interface
+
+const std = @import("std");
+const lp = @import("lightpanda");
+
+const js = @import("../../js/js.zig");
+const Page = @import("../../Page.zig");
+const codec = @import("codec.zig");
+const common = @import("common.zig");
+
+const EventTarget = @import("../EventTarget.zig");
+const AudioData = @import("AudioData.zig");
+
+const Execution = js.Execution;
+
+const AudioEncoder = @This();
+
+pub const Proto = EventTarget;
+
+_proto: *EventTarget,
+_rc: lp.RC = .{},
+_arena: *lp.Arena,
+_core: codec.Core(AudioEncoder),
+
+pub const Config = struct {
+ aac: ?Aac = null,
+ bitrate: ?f64 = null,
+ bitrateMode: []const u8 = "variable",
+ codec: []const u8,
+ flac: ?Flac = null,
+ numberOfChannels: f64,
+ opus: ?Opus = null,
+ sampleRate: f64,
+
+ // AacEncoderConfig, from the AAC registration
+ const Aac = struct {
+ format: []const u8 = "aac",
+ };
+
+ // FlacEncoderConfig, from the FLAC registration
+ const Flac = struct {
+ blockSize: f64 = 0,
+ compressLevel: f64 = 5,
+ };
+
+ // OpusEncoderConfig, from the Opus registration
+ const Opus = struct {
+ application: []const u8 = "audio",
+ complexity: ?f64 = null,
+ format: []const u8 = "opus",
+ frameDuration: f64 = 20000,
+ packetlossperc: f64 = 0,
+ signal: []const u8 = "auto",
+ usedtx: bool = false,
+ useinbandfec: bool = false,
+
+ fn isValid(self: Opus) bool {
+ if (codec.checkEnum(self.application, &.{ "voip", "audio", "lowdelay" }) == false) return false;
+ if (codec.checkEnum(self.format, &.{ "opus", "ogg" }) == false) return false;
+ if (codec.checkEnum(self.signal, &.{ "auto", "music", "voice" }) == false) return false;
+ // RFC 6716 frames are 2.5 to 20ms; a packet holds up to 120ms of them
+ const duration = common.enforceRange(u64, self.frameDuration) catch return false;
+ if (duration < 2500 or duration > 120_000 or duration % 2500 != 0) return false;
+ if (common.optionalEnforceRange(u32, self.complexity) catch return false) |complexity| {
+ if (complexity > 10) return false;
+ }
+ const packetlossperc = common.enforceRange(u32, self.packetlossperc) catch return false;
+ return packetlossperc <= 100;
+ }
+ };
+
+ // "valid AudioEncoderConfig", plus the codec registrations' checks.
+ pub fn isValid(self: Config) bool {
+ if (codec.isEmptyCodec(self.codec)) return false;
+ if (codec.checkEnum(self.bitrateMode, &.{ "constant", "variable" }) == false) return false;
+ const channels = common.enforceRange(u32, self.numberOfChannels) catch return false;
+ const sample_rate = common.enforceRange(u32, self.sampleRate) catch return false;
+ if (channels == 0 or sample_rate == 0) return false;
+
+ const bitrate = common.optionalEnforceRange(u64, self.bitrate) catch return false;
+ // Not in the spec, but every engine and WPT reject it.
+ if (bitrate == 0) return false;
+
+ if (self.aac) |aac| {
+ if (codec.checkEnum(aac.format, &.{ "aac", "adts" }) == false) return false;
+ }
+ if (self.flac) |flac| {
+ _ = common.enforceRange(u32, flac.blockSize) catch return false;
+ _ = common.enforceRange(u32, flac.compressLevel) catch return false;
+ }
+ if (std.mem.eql(u8, self.codec, "opus")) {
+ if (self.opus) |opus| {
+ if (opus.isValid() == false) return false;
+ }
+ // Opus' own bitrate range, as Chrome enforces it.
+ if (bitrate) |b| {
+ if (b < 6_000 or b > 510_000) return false;
+ }
+ }
+ return true;
+ }
+};
+
+fn init(callbacks: codec.Init, exec: *Execution) !*AudioEncoder {
+ const arena = try exec.getArena(.tiny, "AudioEncoder");
+ errdefer arena.release();
+ return exec._factory.eventTargetWithAllocator(arena.allocator(), AudioEncoder{
+ ._proto = undefined,
+ ._arena = arena,
+ ._core = .init(callbacks, exec),
+ });
+}
+
+pub fn deinit(self: *AudioEncoder, page: *Page) void {
+ self._core.deinit();
+ page.removeTarget(self.asEventTarget());
+ self._arena.release();
+}
+
+pub fn acquireRef(self: *AudioEncoder) void {
+ self._rc.acquire();
+}
+
+pub fn releaseRef(self: *AudioEncoder, page: *Page) void {
+ self._rc.release(self, page);
+}
+
+pub fn asEventTarget(self: *AudioEncoder) *EventTarget {
+ return self._proto;
+}
+
+fn getState(self: *const AudioEncoder) []const u8 {
+ return self._core.getState();
+}
+
+fn getEncodeQueueSize(self: *const AudioEncoder) u32 {
+ return self._core.queue_size;
+}
+
+fn configure(self: *AudioEncoder, config: Config) !void {
+ if (config.isValid() == false) {
+ return error.TypeError;
+ }
+ return self._core.configure(false);
+}
+
+fn encode(self: *AudioEncoder, data: *AudioData) !void {
+ if (data.isClosed()) {
+ return error.TypeError;
+ }
+ return self._core.enqueue();
+}
+
+fn flush(self: *AudioEncoder, exec: *const Execution) !js.Promise {
+ return self._core.flush(false, exec);
+}
+
+fn reset(self: *AudioEncoder) !void {
+ return self._core.reset();
+}
+
+fn close(self: *AudioEncoder) !void {
+ return self._core.close();
+}
+
+fn isConfigSupported(config: js.Value, exec: *Execution) !js.Promise {
+ return codec.isConfigSupported(Config, config, exec);
+}
+
+pub const JsApi = struct {
+ pub const bridge = js.Bridge(AudioEncoder);
+
+ pub const Meta = struct {
+ pub const name = "AudioEncoder";
+ pub const prototype_chain = bridge.prototypeChain();
+ pub var class_id: bridge.ClassId = undefined;
+ };
+
+ pub const constructor = bridge.constructor(AudioEncoder.init, .{});
+ pub const state = bridge.accessor(AudioEncoder.getState, null, .{});
+ pub const encodeQueueSize = bridge.accessor(AudioEncoder.getEncodeQueueSize, null, .{});
+ pub const ondequeue = EventTarget.handlerAccessor(AudioEncoder, "dequeue");
+ pub const configure = bridge.function(AudioEncoder.configure, .{});
+ pub const encode = bridge.function(AudioEncoder.encode, .{});
+ pub const flush = bridge.function(AudioEncoder.flush, .{});
+ pub const reset = bridge.function(AudioEncoder.reset, .{});
+ pub const close = bridge.function(AudioEncoder.close, .{});
+ pub const isConfigSupported = bridge.function(AudioEncoder.isConfigSupported, .{ .static = true });
+};
diff --git a/src/browser/webapi/webcodecs/VideoColorSpace.zig b/src/browser/webapi/webcodecs/VideoColorSpace.zig
new file mode 100644
index 000000000..352c9ff6c
--- /dev/null
+++ b/src/browser/webapi/webcodecs/VideoColorSpace.zig
@@ -0,0 +1,150 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+// https://w3c.github.io/webcodecs/#videocolorspace
+
+const std = @import("std");
+const lp = @import("lightpanda");
+
+const js = @import("../../js/js.zig");
+const Page = @import("../../Page.zig");
+
+const Execution = js.Execution;
+
+const VideoColorSpace = @This();
+
+_rc: lp.RC = .{},
+_arena: *lp.Arena,
+_value: Value,
+
+pub const Primaries = enum { bt709, bt470bg, smpte170m, bt2020, smpte432 };
+pub const Transfer = enum { bt709, smpte170m, @"iec61966-2-1", linear, pq, hlg };
+pub const Matrix = enum { rgb, bt709, bt470bg, smpte170m, @"bt2020-ncl" };
+
+// Also what VideoFrame stores: it hands out a new VideoColorSpace each time.
+pub const Value = struct {
+ primaries: ?Primaries = null,
+ transfer: ?Transfer = null,
+ matrix: ?Matrix = null,
+ fullRange: ?bool = null,
+};
+
+// VideoColorSpaceInit
+pub const Init = struct {
+ fullRange: ?bool = null,
+ matrix: ?[]const u8 = null,
+ primaries: ?[]const u8 = null,
+ transfer: ?[]const u8 = null,
+
+ pub fn toValue(self: Init) !Value {
+ return .{
+ .primaries = try parseEnum(Primaries, self.primaries),
+ .transfer = try parseEnum(Transfer, self.transfer),
+ .matrix = try parseEnum(Matrix, self.matrix),
+ .fullRange = self.fullRange,
+ };
+ }
+};
+
+fn parseEnum(comptime E: type, value: ?[]const u8) !?E {
+ const str = value orelse return null;
+ return std.meta.stringToEnum(E, str) orelse error.TypeError;
+}
+
+fn constructor(init_: ?Init, exec: *const Execution) !*VideoColorSpace {
+ const init = init_ orelse Init{};
+ return create(try init.toValue(), exec.page);
+}
+
+pub fn create(value: Value, page: *Page) !*VideoColorSpace {
+ const arena = try page.getPinnedArena(.tiny, "VideoColorSpace");
+ errdefer arena.release();
+ const self = try arena.create(VideoColorSpace);
+ self.* = .{ ._arena = arena, ._value = value };
+ return self;
+}
+
+pub fn deinit(self: *VideoColorSpace, _: *Page) void {
+ self._arena.release();
+}
+
+pub fn acquireRef(self: *VideoColorSpace) void {
+ self._rc.acquire();
+}
+
+pub fn releaseRef(self: *VideoColorSpace, page: *Page) void {
+ self._rc.release(self, page);
+}
+
+fn enumName(value: anytype) ?[]const u8 {
+ return if (value) |v| @tagName(v) else null;
+}
+
+fn getPrimaries(self: *const VideoColorSpace) ?[]const u8 {
+ return enumName(self._value.primaries);
+}
+
+fn getTransfer(self: *const VideoColorSpace) ?[]const u8 {
+ return enumName(self._value.transfer);
+}
+
+fn getMatrix(self: *const VideoColorSpace) ?[]const u8 {
+ return enumName(self._value.matrix);
+}
+
+fn getFullRange(self: *const VideoColorSpace) ?bool {
+ return self._value.fullRange;
+}
+
+const Json = struct {
+ primaries: ?[]const u8,
+ transfer: ?[]const u8,
+ matrix: ?[]const u8,
+ fullRange: ?bool,
+};
+
+fn toJSON(self: *const VideoColorSpace) Json {
+ return .{
+ .primaries = self.getPrimaries(),
+ .transfer = self.getTransfer(),
+ .matrix = self.getMatrix(),
+ .fullRange = self.getFullRange(),
+ };
+}
+
+pub const JsApi = struct {
+ pub const bridge = js.Bridge(VideoColorSpace);
+
+ pub const Meta = struct {
+ pub const name = "VideoColorSpace";
+ pub const prototype_chain = bridge.prototypeChain();
+ pub var class_id: bridge.ClassId = undefined;
+ };
+
+ pub const constructor = bridge.constructor(VideoColorSpace.constructor, .{});
+ pub const primaries = bridge.accessor(VideoColorSpace.getPrimaries, null, .{});
+ pub const transfer = bridge.accessor(VideoColorSpace.getTransfer, null, .{});
+ pub const matrix = bridge.accessor(VideoColorSpace.getMatrix, null, .{});
+ pub const fullRange = bridge.accessor(VideoColorSpace.getFullRange, null, .{});
+ pub const toJSON = bridge.function(VideoColorSpace.toJSON, .{});
+};
+
+const testing = @import("../../../testing.zig");
+test "WebApi: VideoColorSpace" {
+ try testing.htmlRunner("webcodecs/video_color_space.html", .{});
+}
diff --git a/src/browser/webapi/webcodecs/VideoDecoder.zig b/src/browser/webapi/webcodecs/VideoDecoder.zig
new file mode 100644
index 000000000..d78ed5c08
--- /dev/null
+++ b/src/browser/webapi/webcodecs/VideoDecoder.zig
@@ -0,0 +1,172 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+// https://w3c.github.io/webcodecs/#videodecoder-interface
+
+const std = @import("std");
+const lp = @import("lightpanda");
+
+const js = @import("../../js/js.zig");
+const Page = @import("../../Page.zig");
+const codec = @import("codec.zig");
+const common = @import("common.zig");
+
+const EventTarget = @import("../EventTarget.zig");
+const VideoColorSpace = @import("VideoColorSpace.zig");
+const EncodedVideoChunk = @import("encoded_chunk.zig").EncodedVideoChunk;
+
+const Execution = js.Execution;
+
+const VideoDecoder = @This();
+
+pub const Proto = EventTarget;
+
+_proto: *EventTarget,
+_rc: lp.RC = .{},
+_arena: *lp.Arena,
+_core: codec.Core(VideoDecoder),
+
+pub const Config = struct {
+ codec: []const u8,
+ codedHeight: ?f64 = null,
+ codedWidth: ?f64 = null,
+ colorSpace: ?VideoColorSpace.Init = null,
+ description: ?js.Value = null,
+ displayAspectHeight: ?f64 = null,
+ displayAspectWidth: ?f64 = null,
+ flip: bool = false,
+ hardwareAcceleration: []const u8 = "no-preference",
+ optimizeForLatency: bool = false,
+ rotation: f64 = 0,
+
+ // "valid VideoDecoderConfig"
+ pub fn isValid(self: Config) bool {
+ if (codec.isEmptyCodec(self.codec)) return false;
+ if (codec.checkEnum(self.hardwareAcceleration, codec.hardware_acceleration) == false) return false;
+ if (std.math.isFinite(self.rotation) == false) return false;
+ if (self.colorSpace) |cs| {
+ _ = cs.toValue() catch return false;
+ }
+ if (pairIsInvalid(self.codedWidth, self.codedHeight)) return false;
+ if (pairIsInvalid(self.displayAspectWidth, self.displayAspectHeight)) return false;
+ if (self.description) |d| {
+ if (codec.isDetachedBuffer(d)) return false;
+ }
+ return true;
+ }
+
+ // Both or neither, and neither zero.
+ fn pairIsInvalid(a: ?f64, b: ?f64) bool {
+ if ((a == null) != (b == null)) return true;
+ const x = common.optionalEnforceRange(u32, a) catch return true;
+ const y = common.optionalEnforceRange(u32, b) catch return true;
+ return x == 0 or y == 0;
+ }
+};
+
+fn init(callbacks: codec.Init, exec: *Execution) !*VideoDecoder {
+ const arena = try exec.getArena(.tiny, "VideoDecoder");
+ errdefer arena.release();
+ return exec._factory.eventTargetWithAllocator(arena.allocator(), VideoDecoder{
+ ._proto = undefined,
+ ._arena = arena,
+ ._core = .init(callbacks, exec),
+ });
+}
+
+pub fn deinit(self: *VideoDecoder, page: *Page) void {
+ self._core.deinit();
+ page.removeTarget(self.asEventTarget());
+ self._arena.release();
+}
+
+pub fn acquireRef(self: *VideoDecoder) void {
+ self._rc.acquire();
+}
+
+pub fn releaseRef(self: *VideoDecoder, page: *Page) void {
+ self._rc.release(self, page);
+}
+
+pub fn asEventTarget(self: *VideoDecoder) *EventTarget {
+ return self._proto;
+}
+
+fn getState(self: *const VideoDecoder) []const u8 {
+ return self._core.getState();
+}
+
+fn getDecodeQueueSize(self: *const VideoDecoder) u32 {
+ return self._core.queue_size;
+}
+
+fn configure(self: *VideoDecoder, config: Config) !void {
+ if (config.isValid() == false) {
+ return error.TypeError;
+ }
+ return self._core.configure(true);
+}
+
+fn decode(self: *VideoDecoder, chunk: *EncodedVideoChunk) !void {
+ const core = &self._core;
+ if (core.state != .configured) {
+ return error.InvalidStateError;
+ }
+ if (core.key_chunk_required) {
+ if (chunk._type != .key) return error.DataError;
+ core.key_chunk_required = false;
+ }
+ return core.enqueue();
+}
+
+fn flush(self: *VideoDecoder, exec: *const Execution) !js.Promise {
+ return self._core.flush(true, exec);
+}
+
+fn reset(self: *VideoDecoder) !void {
+ return self._core.reset();
+}
+
+fn close(self: *VideoDecoder) !void {
+ return self._core.close();
+}
+
+fn isConfigSupported(config: js.Value, exec: *Execution) !js.Promise {
+ return codec.isConfigSupported(Config, config, exec);
+}
+
+pub const JsApi = struct {
+ pub const bridge = js.Bridge(VideoDecoder);
+
+ pub const Meta = struct {
+ pub const name = "VideoDecoder";
+ pub const prototype_chain = bridge.prototypeChain();
+ pub var class_id: bridge.ClassId = undefined;
+ };
+
+ pub const constructor = bridge.constructor(VideoDecoder.init, .{});
+ pub const state = bridge.accessor(VideoDecoder.getState, null, .{});
+ pub const decodeQueueSize = bridge.accessor(VideoDecoder.getDecodeQueueSize, null, .{});
+ pub const ondequeue = EventTarget.handlerAccessor(VideoDecoder, "dequeue");
+ pub const configure = bridge.function(VideoDecoder.configure, .{});
+ pub const decode = bridge.function(VideoDecoder.decode, .{});
+ pub const flush = bridge.function(VideoDecoder.flush, .{});
+ pub const reset = bridge.function(VideoDecoder.reset, .{});
+ pub const close = bridge.function(VideoDecoder.close, .{});
+ pub const isConfigSupported = bridge.function(VideoDecoder.isConfigSupported, .{ .static = true });
+};
diff --git a/src/browser/webapi/webcodecs/VideoEncoder.zig b/src/browser/webapi/webcodecs/VideoEncoder.zig
new file mode 100644
index 000000000..6d6d2d39e
--- /dev/null
+++ b/src/browser/webapi/webcodecs/VideoEncoder.zig
@@ -0,0 +1,193 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+// https://w3c.github.io/webcodecs/#videoencoder-interface
+
+const std = @import("std");
+const lp = @import("lightpanda");
+
+const js = @import("../../js/js.zig");
+const Page = @import("../../Page.zig");
+const codec = @import("codec.zig");
+const common = @import("common.zig");
+
+const EventTarget = @import("../EventTarget.zig");
+const VideoFrame = @import("VideoFrame.zig");
+
+const Execution = js.Execution;
+
+const VideoEncoder = @This();
+
+pub const Proto = EventTarget;
+
+_proto: *EventTarget,
+_rc: lp.RC = .{},
+_arena: *lp.Arena,
+_core: codec.Core(VideoEncoder),
+
+pub const Config = struct {
+ alpha: []const u8 = "discard",
+ av1: ?Av1 = null,
+ avc: ?Avc = null,
+ bitrate: ?f64 = null,
+ bitrateMode: []const u8 = "variable",
+ codec: []const u8,
+ contentHint: ?[]const u8 = null,
+ displayHeight: ?f64 = null,
+ displayWidth: ?f64 = null,
+ framerate: ?f64 = null,
+ hardwareAcceleration: []const u8 = "no-preference",
+ height: f64,
+ hevc: ?Hevc = null,
+ latencyMode: []const u8 = "quality",
+ scalabilityMode: ?[]const u8 = null,
+ width: f64,
+
+ // the codec registrations' extensions
+ const Av1 = struct {
+ forceScreenContentTools: bool = false,
+ };
+ const Avc = struct {
+ format: []const u8 = "avc",
+ };
+ const Hevc = struct {
+ format: []const u8 = "hevc",
+ };
+
+ // "valid VideoEncoderConfig"
+ pub fn isValid(self: Config) bool {
+ if (codec.isEmptyCodec(self.codec)) return false;
+ if (codec.checkEnum(self.alpha, &.{ "keep", "discard" }) == false) return false;
+ if (codec.checkEnum(self.bitrateMode, &.{ "constant", "variable", "quantizer" }) == false) return false;
+ if (codec.checkEnum(self.hardwareAcceleration, codec.hardware_acceleration) == false) return false;
+ if (codec.checkEnum(self.latencyMode, &.{ "quality", "realtime" }) == false) return false;
+ if (self.avc) |avc| {
+ if (codec.checkEnum(avc.format, &.{ "annexb", "avc" }) == false) return false;
+ }
+ if (self.hevc) |hevc| {
+ if (codec.checkEnum(hevc.format, &.{ "annexb", "hevc" }) == false) return false;
+ }
+ if (self.framerate) |framerate| {
+ if (std.math.isFinite(framerate) == false) return false;
+ }
+
+ const width = common.enforceRange(u32, self.width) catch return false;
+ const height = common.enforceRange(u32, self.height) catch return false;
+ if (width == 0 or height == 0) return false;
+ const display_width = common.optionalEnforceRange(u32, self.displayWidth) catch return false;
+ const display_height = common.optionalEnforceRange(u32, self.displayHeight) catch return false;
+ if (display_width == 0 or display_height == 0) return false;
+
+ const bitrate = common.optionalEnforceRange(u64, self.bitrate) catch return false;
+ // Not in the spec, but every engine and WPT reject it.
+ if (bitrate == 0) return false;
+ return true;
+ }
+};
+
+fn init(callbacks: codec.Init, exec: *Execution) !*VideoEncoder {
+ const arena = try exec.getArena(.tiny, "VideoEncoder");
+ errdefer arena.release();
+ return exec._factory.eventTargetWithAllocator(arena.allocator(), VideoEncoder{
+ ._proto = undefined,
+ ._arena = arena,
+ ._core = .init(callbacks, exec),
+ });
+}
+
+pub fn deinit(self: *VideoEncoder, page: *Page) void {
+ self._core.deinit();
+ page.removeTarget(self.asEventTarget());
+ self._arena.release();
+}
+
+pub fn acquireRef(self: *VideoEncoder) void {
+ self._rc.acquire();
+}
+
+pub fn releaseRef(self: *VideoEncoder, page: *Page) void {
+ self._rc.release(self, page);
+}
+
+pub fn asEventTarget(self: *VideoEncoder) *EventTarget {
+ return self._proto;
+}
+
+fn getState(self: *const VideoEncoder) []const u8 {
+ return self._core.getState();
+}
+
+fn getEncodeQueueSize(self: *const VideoEncoder) u32 {
+ return self._core.queue_size;
+}
+
+fn configure(self: *VideoEncoder, config: Config) !void {
+ if (config.isValid() == false) {
+ return error.TypeError;
+ }
+ return self._core.configure(false);
+}
+
+const EncodeOptions = struct {
+ keyFrame: bool = false,
+};
+
+fn encode(self: *VideoEncoder, frame: *VideoFrame, options: ?EncodeOptions) !void {
+ _ = options;
+ if (frame.isClosed()) {
+ return error.TypeError;
+ }
+ return self._core.enqueue();
+}
+
+fn flush(self: *VideoEncoder, exec: *const Execution) !js.Promise {
+ return self._core.flush(false, exec);
+}
+
+fn reset(self: *VideoEncoder) !void {
+ return self._core.reset();
+}
+
+fn close(self: *VideoEncoder) !void {
+ return self._core.close();
+}
+
+fn isConfigSupported(config: js.Value, exec: *Execution) !js.Promise {
+ return codec.isConfigSupported(Config, config, exec);
+}
+
+pub const JsApi = struct {
+ pub const bridge = js.Bridge(VideoEncoder);
+
+ pub const Meta = struct {
+ pub const name = "VideoEncoder";
+ pub const prototype_chain = bridge.prototypeChain();
+ pub var class_id: bridge.ClassId = undefined;
+ };
+
+ pub const constructor = bridge.constructor(VideoEncoder.init, .{});
+ pub const state = bridge.accessor(VideoEncoder.getState, null, .{});
+ pub const encodeQueueSize = bridge.accessor(VideoEncoder.getEncodeQueueSize, null, .{});
+ pub const ondequeue = EventTarget.handlerAccessor(VideoEncoder, "dequeue");
+ pub const configure = bridge.function(VideoEncoder.configure, .{});
+ pub const encode = bridge.function(VideoEncoder.encode, .{});
+ pub const flush = bridge.function(VideoEncoder.flush, .{});
+ pub const reset = bridge.function(VideoEncoder.reset, .{});
+ pub const close = bridge.function(VideoEncoder.close, .{});
+ pub const isConfigSupported = bridge.function(VideoEncoder.isConfigSupported, .{ .static = true });
+};
diff --git a/src/browser/webapi/webcodecs/VideoFrame.zig b/src/browser/webapi/webcodecs/VideoFrame.zig
new file mode 100644
index 000000000..9f510d3d3
--- /dev/null
+++ b/src/browser/webapi/webcodecs/VideoFrame.zig
@@ -0,0 +1,979 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+// https://w3c.github.io/webcodecs/#videoframe-interface
+//
+// Frames come from buffers, other frames, or canvases. Our canvases don't
+// rasterize, so a canvas-made frame holds the same transparent black that
+// getImageData() returns for it. Conversion from YUV to RGB isn't supported.
+
+const std = @import("std");
+const lp = @import("lightpanda");
+
+const js = @import("../../js/js.zig");
+const Page = @import("../../Page.zig");
+const common = @import("common.zig");
+
+const Element = @import("../Element.zig");
+const DOMRectReadOnly = @import("../DOMRectReadOnly.zig");
+const OffscreenCanvas = @import("../canvas/OffscreenCanvas.zig");
+const VideoColorSpace = @import("VideoColorSpace.zig");
+
+const Execution = js.Execution;
+
+const VideoFrame = @This();
+
+_rc: lp.RC = .{},
+_arena: *lp.Arena,
+// null once closed ([[Detached]]), like _format
+_resource: ?*Resource,
+_format: ?PixelFormat,
+_coded_width: u32,
+_coded_height: u32,
+_visible: Rect,
+_display_width: u32,
+_display_height: u32,
+_rotation: f64,
+_flip: bool,
+_timestamp: i64,
+_duration: ?u64,
+_color_space: VideoColorSpace.Value,
+
+pub const PixelFormat = enum {
+ I420,
+ I420P10,
+ I420P12,
+ I420A,
+ I420AP10,
+ I420AP12,
+ I422,
+ I422P10,
+ I422P12,
+ I422A,
+ I422AP10,
+ I422AP12,
+ I444,
+ I444P10,
+ I444P12,
+ I444A,
+ I444AP10,
+ I444AP12,
+ NV12,
+ RGBA,
+ RGBX,
+ BGRA,
+ BGRX,
+
+ pub const Plane = struct {
+ // bytes per sample times samples per position (2 for NV12's UV)
+ sample_bytes: u32,
+ // sub-sampling factors
+ width: u32,
+ height: u32,
+ };
+
+ pub fn planes(self: PixelFormat) []const Plane {
+ return switch (self) {
+ .NV12 => &.{ .{ .sample_bytes = 1, .width = 1, .height = 1 }, .{ .sample_bytes = 2, .width = 2, .height = 2 } },
+ .RGBA, .RGBX, .BGRA, .BGRX => &.{.{ .sample_bytes = 4, .width = 1, .height = 1 }},
+ inline else => |f| comptime yuvPlanes(@tagName(f)),
+ };
+ }
+
+ fn yuvPlanes(comptime name: []const u8) []const Plane {
+ const bytes: u32 = if (std.mem.endsWith(u8, name, "P10") or std.mem.endsWith(u8, name, "P12")) 2 else 1;
+ // I4[20|22|44]...: the third digit picks the chroma sub-sampling
+ const chroma: Plane = switch (name[3]) {
+ '0' => .{ .sample_bytes = bytes, .width = 2, .height = 2 },
+ '2' => .{ .sample_bytes = bytes, .width = 2, .height = 1 },
+ '4' => .{ .sample_bytes = bytes, .width = 1, .height = 1 },
+ else => unreachable,
+ };
+ const luma: Plane = .{ .sample_bytes = bytes, .width = 1, .height = 1 };
+ const alpha = name.len > 4 and name[4] == 'A';
+ return if (alpha) &.{ luma, chroma, chroma, luma } else &.{ luma, chroma, chroma };
+ }
+
+ // The format without its alpha plane/channel.
+ pub fn opaqueFormat(self: PixelFormat) PixelFormat {
+ return switch (self) {
+ .I420A => .I420,
+ .I420AP10 => .I420P10,
+ .I420AP12 => .I420P12,
+ .I422A => .I422,
+ .I422AP10 => .I422P10,
+ .I422AP12 => .I422P12,
+ .I444A => .I444,
+ .I444AP10 => .I444P10,
+ .I444AP12 => .I444P12,
+ .RGBA => .RGBX,
+ .BGRA => .BGRX,
+ else => self,
+ };
+ }
+
+ pub fn isRgb(self: PixelFormat) bool {
+ return switch (self) {
+ .RGBA, .RGBX, .BGRA, .BGRX => true,
+ else => false,
+ };
+ }
+};
+
+const Rect = struct {
+ x: u32,
+ y: u32,
+ width: u32,
+ height: u32,
+};
+
+fn ceilDiv(a: u64, b: u64) u64 {
+ return (a + b - 1) / b;
+}
+
+// The pixels, shared by a frame, its clones and the frames made from it.
+// Planes are tightly packed, one after the other, in `format`'s layout.
+const Resource = struct {
+ refs: u32,
+ arena: *lp.Arena,
+ format: PixelFormat,
+ width: u32,
+ height: u32,
+ data: []u8,
+
+ fn create(format: PixelFormat, width: u32, height: u32, page: *Page) !*Resource {
+ var size: u64 = 0;
+ for (format.planes()) |plane| {
+ size += planeStride(plane, width) * ceilDiv(height, plane.height);
+ }
+ const arena = try page.getPinnedArena(@as(usize, @intCast(size)) + 128, "VideoFrame.resource");
+ errdefer arena.release();
+
+ const self = try arena.create(Resource);
+ self.* = .{
+ .refs = 1,
+ .arena = arena,
+ .format = format,
+ .width = width,
+ .height = height,
+ .data = try arena.alloc(u8, @intCast(size)),
+ };
+ @memset(self.data, 0);
+ arena.report();
+ return self;
+ }
+
+ fn planeStride(plane: PixelFormat.Plane, width: u32) u64 {
+ return ceilDiv(width, plane.width) * plane.sample_bytes;
+ }
+
+ fn stride(self: *const Resource, index: usize) u64 {
+ return planeStride(self.format.planes()[index], self.width);
+ }
+
+ fn offset(self: *const Resource, index: usize) u64 {
+ var result: u64 = 0;
+ for (self.format.planes()[0..index]) |plane| {
+ result += planeStride(plane, self.width) * ceilDiv(self.height, plane.height);
+ }
+ return result;
+ }
+
+ fn acquire(self: *Resource) *Resource {
+ self.refs += 1;
+ return self;
+ }
+
+ fn release(self: *Resource) void {
+ self.refs -= 1;
+ if (self.refs == 0) {
+ self.arena.release();
+ }
+ }
+};
+
+const RectInit = struct {
+ height: f64 = 0,
+ width: f64 = 0,
+ x: f64 = 0,
+ y: f64 = 0,
+};
+
+const PlaneLayoutInit = struct {
+ offset: f64,
+ stride: f64,
+};
+
+const PlaneLayout = struct {
+ offset: u32,
+ stride: u32,
+};
+
+// VideoFrameBufferInit
+const BufferInit = struct {
+ codedHeight: f64,
+ codedWidth: f64,
+ colorSpace: ?VideoColorSpace.Init = null,
+ displayHeight: ?f64 = null,
+ displayWidth: ?f64 = null,
+ duration: ?f64 = null,
+ flip: bool = false,
+ format: []const u8,
+ layout: ?[]const PlaneLayoutInit = null,
+ rotation: f64 = 0,
+ timestamp: f64,
+ transfer: []const js.Value = &.{},
+ visibleRect: ?RectInit = null,
+};
+
+// VideoFrameInit
+const ImageInit = struct {
+ alpha: []const u8 = "keep",
+ displayHeight: ?f64 = null,
+ displayWidth: ?f64 = null,
+ duration: ?f64 = null,
+ flip: bool = false,
+ rotation: f64 = 0,
+ timestamp: ?f64 = null,
+ visibleRect: ?RectInit = null,
+};
+
+fn constructor(source: js.Value, init_: ?js.Value, exec: *const Execution) !*VideoFrame {
+ if (source.isArrayBuffer() or source.isArrayBufferView()) {
+ const data = try source.toZig(js.AllowSharedBufferSource);
+ const init = (try (init_ orelse return error.TypeError).toZig(?BufferInit)) orelse return error.TypeError;
+ return fromBuffer(data.bytes, init, exec.page);
+ }
+
+ const init: ImageInit = blk: {
+ const value = init_ orelse break :blk .{};
+ break :blk (try value.toZig(?ImageInit)) orelse .{};
+ };
+
+ if (source.toZig(*VideoFrame)) |other| {
+ return fromFrame(other, init, exec.page);
+ } else |_| {}
+
+ const width: u32, const height: u32 = blk: {
+ if (source.toZig(*OffscreenCanvas)) |canvas| {
+ break :blk .{ canvas.getWidth(), canvas.getHeight() };
+ } else |_| {}
+ if (source.toZig(*Element.Html.Canvas)) |canvas| {
+ break :blk .{ canvas.getWidth(), canvas.getHeight() };
+ } else |_| {}
+ // Images and videos have no pixel data for us to take: we don't
+ // decode either. "Check the usability of the image argument" fails.
+ if (source.toZig(*Element)) |_| {
+ return error.InvalidStateError;
+ } else |_| {}
+ return error.TypeError;
+ };
+ return fromCanvas(width, height, init, exec.page);
+}
+
+fn parseFormat(value: []const u8) !PixelFormat {
+ return std.meta.stringToEnum(PixelFormat, value) orelse error.TypeError;
+}
+
+// "Parse Rotation": the nearest multiple of 90 (ties up), in [0, 360).
+fn parseRotation(rotation: f64) !f64 {
+ if (std.math.isFinite(rotation) == false) {
+ return error.TypeError;
+ }
+ // @mod is exact (fmod), whatever the magnitude; the subtraction form isn't.
+ const aligned = @floor(@mod(rotation, 360) / 90 + 0.5) * 90;
+ return @mod(aligned, 360);
+}
+
+fn addRotations(base: f64, base_flip: bool, rotation: f64) f64 {
+ const combined = if (base_flip) base - rotation else base + rotation;
+ return @mod(combined, 360);
+}
+
+// The checks shared by "Parse Visible Rect" and the init validations.
+fn parseRect(init: RectInit, coded_width: u32, coded_height: u32, format: ?PixelFormat) !Rect {
+ const rect: Rect = .{
+ .x = try rectMember(init.x),
+ .y = try rectMember(init.y),
+ .width = try rectMember(init.width),
+ .height = try rectMember(init.height),
+ };
+ if (rect.width == 0 or rect.height == 0) return error.TypeError;
+ if (@as(u64, rect.x) + rect.width > coded_width) return error.TypeError;
+ if (@as(u64, rect.y) + rect.height > coded_height) return error.TypeError;
+ if (format) |f| {
+ // "Verify Rect Offset Alignment"
+ for (f.planes()) |plane| {
+ if (rect.x % plane.width != 0 or rect.y % plane.height != 0) return error.TypeError;
+ }
+ }
+ return rect;
+}
+
+// A DOMRectInit member is an unrestricted double: anything that isn't a
+// non-negative value within the coded size (so a u32) is a TypeError. The
+// range is checked before converting, which @intFromFloat can't survive.
+fn rectMember(value: f64) !u32 {
+ if (std.math.isFinite(value) == false or value < 0 or value > std.math.maxInt(u32)) {
+ return error.TypeError;
+ }
+ return @intFromFloat(value);
+}
+
+const DisplaySize = struct { width: u32, height: u32 };
+
+fn explicitDisplaySize(width: ?f64, height: ?f64) !?DisplaySize {
+ if ((width == null) != (height == null)) {
+ return error.TypeError;
+ }
+ const w = try common.optionalEnforceRange(u32, width) orelse return null;
+ const h = (try common.optionalEnforceRange(u32, height)).?;
+ if (w == 0 or h == 0) {
+ return error.TypeError;
+ }
+ return .{ .width = w, .height = h };
+}
+
+fn fromBuffer(data: []const u8, init: BufferInit, page: *Page) !*VideoFrame {
+ const format = try parseFormat(init.format);
+ const coded_width = try common.enforceRange(u32, init.codedWidth);
+ const coded_height = try common.enforceRange(u32, init.codedHeight);
+ const timestamp = try common.enforceRange(i64, init.timestamp);
+ const duration = try common.optionalEnforceRange(u64, init.duration);
+ const display = try explicitDisplaySize(init.displayWidth, init.displayHeight);
+ const rotation = try parseRotation(init.rotation);
+ const color_space: VideoColorSpace.Value = if (init.colorSpace) |cs| try cs.toValue() else defaultColorSpace(format);
+
+ if (coded_width == 0 or coded_height == 0) {
+ return error.TypeError;
+ }
+ const visible: Rect = if (init.visibleRect) |r|
+ try parseRect(r, coded_width, coded_height, format)
+ else
+ .{ .x = 0, .y = 0, .width = coded_width, .height = coded_height };
+
+ // Like Chrome, `data` (and `layout`) describes the whole coded frame; the
+ // visible rectangle is cropped out of it and becomes the new coded size.
+ const planes = format.planes();
+ const source_layout = try computeLayout(.{ .x = 0, .y = 0, .width = coded_width, .height = coded_height }, format, init.layout);
+ if (data.len < source_layout.allocation_size) {
+ return error.TypeError;
+ }
+
+ const transfer: common.Transfer = .{ .buffers = init.transfer };
+ try transfer.check();
+
+ const resource = try Resource.create(format, visible.width, visible.height, page);
+
+ for (planes, 0..) |plane, i| {
+ const source = source_layout.planes[i];
+ const row_bytes = Resource.planeStride(plane, visible.width);
+ const rows = ceilDiv(visible.height, plane.height);
+ var src: u64 = source.offset + (visible.y / plane.height) * source.stride + (visible.x / plane.width) * plane.sample_bytes;
+ var dst = resource.offset(i);
+ for (0..rows) |_| {
+ @memcpy(resource.data[@intCast(dst)..][0..@intCast(row_bytes)], data[@intCast(src)..][0..@intCast(row_bytes)]);
+ src += source.stride;
+ dst += row_bytes;
+ }
+ }
+
+ const self = try create(resource, page);
+ errdefer self.deinit(page);
+ self.* = .{
+ ._arena = self._arena,
+ ._resource = resource,
+ ._format = format,
+ ._coded_width = visible.width,
+ ._coded_height = visible.height,
+ ._visible = .{ .x = 0, .y = 0, .width = visible.width, .height = visible.height },
+ ._rotation = rotation,
+ ._flip = init.flip,
+ ._display_width = undefined,
+ ._display_height = undefined,
+ ._timestamp = timestamp,
+ ._duration = duration,
+ ._color_space = color_space,
+ };
+ if (display) |d| {
+ self._display_width = d.width;
+ self._display_height = d.height;
+ } else if (rotation == 0 or rotation == 180) {
+ self._display_width = visible.width;
+ self._display_height = visible.height;
+ } else {
+ self._display_width = visible.height;
+ self._display_height = visible.width;
+ }
+
+ try transfer.detach();
+ return self;
+}
+
+// "Pick Color Space" without an override.
+fn defaultColorSpace(format: PixelFormat) VideoColorSpace.Value {
+ if (format.isRgb()) {
+ // sRGB
+ return .{ .primaries = .bt709, .transfer = .@"iec61966-2-1", .matrix = .rgb, .fullRange = true };
+ }
+ // REC709
+ return .{ .primaries = .bt709, .transfer = .bt709, .matrix = .bt709, .fullRange = false };
+}
+
+fn parseAlpha(value: []const u8) !bool {
+ if (std.mem.eql(u8, value, "keep")) return true;
+ if (std.mem.eql(u8, value, "discard")) return false;
+ return error.TypeError;
+}
+
+// "Initialize Frame From Other Frame"
+fn fromFrame(other: *const VideoFrame, init: ImageInit, page: *Page) !*VideoFrame {
+ const other_format = other._format orelse return error.InvalidStateError;
+ const resource = other._resource.?;
+ const keep_alpha = try parseAlpha(init.alpha);
+ const format = if (keep_alpha) other_format else other_format.opaqueFormat();
+ const timestamp = if (init.timestamp) |t| try common.enforceRange(i64, t) else other._timestamp;
+ const duration = if (init.duration) |d| try common.enforceRange(u64, d) else other._duration;
+
+ const self = try create(resource.acquire(), page);
+ errdefer self.deinit(page);
+ self.* = .{
+ ._arena = self._arena,
+ ._resource = resource,
+ ._format = format,
+ ._coded_width = other._coded_width,
+ ._coded_height = other._coded_height,
+ ._visible = other._visible,
+ ._rotation = other._rotation,
+ ._flip = other._flip,
+ ._display_width = other._display_width,
+ ._display_height = other._display_height,
+ ._timestamp = timestamp,
+ ._duration = duration,
+ ._color_space = other._color_space,
+ };
+ try self.initOrientation(init, format, other._visible, other._rotation, other._flip, other._display_width, other._display_height);
+ return self;
+}
+
+// HTMLCanvasElement / OffscreenCanvas
+// Chrome's media::limits: a canvas larger than this has no bitmap there, so
+// "Check the usability of the image argument" fails. Ours would happily be
+// that large, and the frame would allocate width * height * 4 bytes.
+const max_canvas_dimension = 32767;
+const max_canvas_pixels = 1 << 28;
+
+fn fromCanvas(width: u32, height: u32, init: ImageInit, page: *Page) !*VideoFrame {
+ if (width == 0 or height == 0) {
+ return error.InvalidStateError;
+ }
+ if (width > max_canvas_dimension or height > max_canvas_dimension or @as(u64, width) * height > max_canvas_pixels) {
+ return error.InvalidStateError;
+ }
+ const timestamp = try common.enforceRange(i64, init.timestamp orelse return error.TypeError);
+ const duration = try common.optionalEnforceRange(u64, init.duration);
+ const keep_alpha = try parseAlpha(init.alpha);
+ const format: PixelFormat = if (keep_alpha) .RGBA else .RGBX;
+
+ const resource = try Resource.create(.RGBA, width, height, page);
+
+ const self = try create(resource, page);
+ errdefer self.deinit(page);
+ const visible: Rect = .{ .x = 0, .y = 0, .width = width, .height = height };
+ self.* = .{
+ ._arena = self._arena,
+ ._resource = resource,
+ ._format = format,
+ ._coded_width = width,
+ ._coded_height = height,
+ ._visible = visible,
+ ._rotation = 0,
+ ._flip = false,
+ ._display_width = width,
+ ._display_height = height,
+ ._timestamp = timestamp,
+ ._duration = duration,
+ ._color_space = .{},
+ };
+ try self.initOrientation(init, format, visible, 0, false, width, height);
+ return self;
+}
+
+// "Initialize Visible Rect, Orientation, and Display Size", plus the bits of
+// "Validate VideoFrameInit" it doesn't already cover.
+fn initOrientation(self: *VideoFrame, init: ImageInit, format: PixelFormat, default_visible: Rect, base_rotation: f64, base_flip: bool, default_display_width: u32, default_display_height: u32) !void {
+ const display = try explicitDisplaySize(init.displayWidth, init.displayHeight);
+ if (init.visibleRect) |r| {
+ self._visible = try parseRect(r, self._coded_width, self._coded_height, format);
+ }
+ const rotation = try parseRotation(init.rotation);
+ self._rotation = addRotations(base_rotation, base_flip, rotation);
+ self._flip = base_flip != init.flip;
+
+ if (display) |d| {
+ self._display_width = d.width;
+ self._display_height = d.height;
+ return;
+ }
+
+ const default_w: f64 = @floatFromInt(default_visible.width);
+ const default_h: f64 = @floatFromInt(default_visible.height);
+ const width_scale, const height_scale = if (base_rotation == 0 or base_rotation == 180)
+ .{ @as(f64, @floatFromInt(default_display_width)) / default_w, @as(f64, @floatFromInt(default_display_height)) / default_h }
+ else
+ .{ @as(f64, @floatFromInt(default_display_height)) / default_w, @as(f64, @floatFromInt(default_display_width)) / default_h };
+
+ const display_width = @round(@as(f64, @floatFromInt(self._visible.width)) * width_scale);
+ const display_height = @round(@as(f64, @floatFromInt(self._visible.height)) * height_scale);
+ if (display_width == 0 or display_height == 0) {
+ return error.TypeError;
+ }
+ const w = std.math.cast(u32, @as(u64, @intFromFloat(display_width))) orelse return error.TypeError;
+ const h = std.math.cast(u32, @as(u64, @intFromFloat(display_height))) orelse return error.TypeError;
+ if (rotation == 0 or rotation == 180) {
+ self._display_width = w;
+ self._display_height = h;
+ } else {
+ self._display_width = h;
+ self._display_height = w;
+ }
+}
+
+// Allocates the frame, which takes over the caller's reference to `resource`
+// (released here on failure). The caller fills in every other field.
+fn create(resource: *Resource, page: *Page) !*VideoFrame {
+ errdefer resource.release();
+ const arena = try page.getPinnedArena(.tiny, "VideoFrame");
+ errdefer arena.release();
+ const self = try arena.create(VideoFrame);
+ self._arena = arena;
+ self._rc = .{};
+ self._resource = resource;
+ return self;
+}
+
+pub fn deinit(self: *VideoFrame, _: *Page) void {
+ if (self._resource) |resource| {
+ resource.release();
+ }
+ self._arena.release();
+}
+
+pub fn acquireRef(self: *VideoFrame) void {
+ self._rc.acquire();
+}
+
+pub fn releaseRef(self: *VideoFrame, page: *Page) void {
+ self._rc.release(self, page);
+}
+
+pub fn isClosed(self: *const VideoFrame) bool {
+ return self._format == null;
+}
+
+fn getFormat(self: *const VideoFrame) ?[]const u8 {
+ const format = self._format orelse return null;
+ return @tagName(format);
+}
+
+fn getCodedWidth(self: *const VideoFrame) u32 {
+ return self._coded_width;
+}
+
+fn getCodedHeight(self: *const VideoFrame) u32 {
+ return self._coded_height;
+}
+
+fn getCodedRect(self: *const VideoFrame, exec: *const Execution) !?*DOMRectReadOnly {
+ if (self.isClosed()) return null;
+ return try DOMRectReadOnly.init(0, 0, @floatFromInt(self._coded_width), @floatFromInt(self._coded_height), exec);
+}
+
+fn getVisibleRect(self: *const VideoFrame, exec: *const Execution) !?*DOMRectReadOnly {
+ if (self.isClosed()) return null;
+ const v = self._visible;
+ return try DOMRectReadOnly.init(@floatFromInt(v.x), @floatFromInt(v.y), @floatFromInt(v.width), @floatFromInt(v.height), exec);
+}
+
+fn getRotation(self: *const VideoFrame) f64 {
+ return self._rotation;
+}
+
+fn getFlip(self: *const VideoFrame) bool {
+ return self._flip;
+}
+
+fn getDisplayWidth(self: *const VideoFrame) u32 {
+ return self._display_width;
+}
+
+fn getDisplayHeight(self: *const VideoFrame) u32 {
+ return self._display_height;
+}
+
+fn getTimestamp(self: *const VideoFrame) i64 {
+ return self._timestamp;
+}
+
+fn getDuration(self: *const VideoFrame) ?u64 {
+ return self._duration;
+}
+
+fn getColorSpace(self: *const VideoFrame, exec: *const Execution) !*VideoColorSpace {
+ return VideoColorSpace.create(self._color_space, exec.page);
+}
+
+// VideoFrameMetadata has no members of its own, and we support none from
+// the registry: it's always an empty dictionary.
+fn metadata(self: *const VideoFrame) !struct {} {
+ if (self.isClosed()) return error.InvalidStateError;
+ return .{};
+}
+
+const max_planes = 4;
+
+// A "combined buffer layout"
+const ComputedLayout = struct {
+ allocation_size: u32,
+ planes: [max_planes]ComputedPlane,
+};
+
+const ComputedPlane = struct {
+ // where the plane goes in the destination
+ offset: u64,
+ stride: u64,
+ // the part of the source plane copied
+ source_top: u64,
+ source_height: u64,
+ source_left_bytes: u64,
+ source_width_bytes: u64,
+};
+
+// "Compute Layout and Allocation Size"
+fn computeLayout(rect: Rect, format: PixelFormat, layout: ?[]const PlaneLayoutInit) !ComputedLayout {
+ const planes = format.planes();
+ if (layout) |l| {
+ if (l.len != planes.len) return error.TypeError;
+ }
+
+ var result: [max_planes]ComputedPlane = undefined;
+ var ends: [max_planes]u64 = undefined;
+ var min_size: u64 = 0;
+ for (planes, 0..) |plane, i| {
+ var computed: ComputedPlane = .{
+ .source_top = ceilDiv(rect.y, plane.height),
+ .source_height = ceilDiv(rect.height, plane.height),
+ .source_left_bytes = (rect.x / plane.width) * plane.sample_bytes,
+ .source_width_bytes = ceilDiv(rect.width, plane.width) * plane.sample_bytes,
+ .offset = undefined,
+ .stride = undefined,
+ };
+ if (layout) |l| {
+ const stride = try common.enforceRange(u32, l[i].stride);
+ if (stride < computed.source_width_bytes) return error.TypeError;
+ computed.offset = try common.enforceRange(u32, l[i].offset);
+ computed.stride = stride;
+ } else {
+ computed.offset = min_size;
+ computed.stride = computed.source_width_bytes;
+ }
+
+ const plane_size = computed.stride * computed.source_height;
+ const plane_end = plane_size + computed.offset;
+ if (plane_size > std.math.maxInt(u32) or plane_end > std.math.maxInt(u32)) {
+ return error.TypeError;
+ }
+ ends[i] = plane_end;
+ min_size = @max(min_size, plane_end);
+
+ for (result[0..i], 0..) |earlier, j| {
+ if (plane_end <= earlier.offset or ends[j] <= computed.offset) continue;
+ return error.TypeError;
+ }
+ result[i] = computed;
+ }
+ return .{ .allocation_size = @intCast(min_size), .planes = result };
+}
+
+const CopyToOptions = struct {
+ colorSpace: ?[]const u8 = null,
+ format: ?[]const u8 = null,
+ layout: ?[]const PlaneLayoutInit = null,
+ rect: ?RectInit = null,
+};
+
+// "Parse VideoFrameCopyToOptions"
+fn parseCopyOptions(self: *const VideoFrame, options: CopyToOptions) !struct { format: PixelFormat, layout: ComputedLayout } {
+ const source_format = self._format orelse return error.InvalidStateError;
+ const format: PixelFormat = if (options.format) |f| blk: {
+ const requested = try parseFormat(f);
+ if (requested.isRgb() == false) return error.NotSupported;
+ break :blk requested;
+ } else source_format;
+
+ if (options.colorSpace) |cs| {
+ if (std.mem.eql(u8, cs, "srgb") == false and std.mem.eql(u8, cs, "display-p3") == false) {
+ return error.TypeError;
+ }
+ }
+
+ const rect = if (options.rect) |r|
+ try parseRect(r, self._coded_width, self._coded_height, source_format)
+ else
+ self._visible;
+
+ return .{ .format = format, .layout = try computeLayout(rect, format, options.layout) };
+}
+
+fn allocationSize(self: *const VideoFrame, options_: ?CopyToOptions) !u32 {
+ const parsed = try self.parseCopyOptions(options_ orelse .{});
+ return parsed.layout.allocation_size;
+}
+
+fn copyTo(self: *const VideoFrame, destination: js.AllowSharedBufferSource, options_: ?CopyToOptions, exec: *const Execution) !js.Promise {
+ const local = exec.js.local.?;
+ const layouts = self.copyInto(destination.bytes, options_ orelse .{}) catch |err| {
+ return local.rejectPromise(switch (err) {
+ error.TypeError => .{ .type_error = "" },
+ else => .{ .dom_exception = .{ .err = err } },
+ });
+ };
+ return local.resolvePromise(layouts.slice());
+}
+
+const Layouts = struct {
+ items: [max_planes]PlaneLayout,
+ len: usize,
+
+ fn slice(self: *const Layouts) []const PlaneLayout {
+ return self.items[0..self.len];
+ }
+};
+
+fn copyInto(self: *const VideoFrame, dest: []u8, options: CopyToOptions) !Layouts {
+ const parsed = try self.parseCopyOptions(options);
+ if (dest.len < parsed.layout.allocation_size) {
+ return error.TypeError;
+ }
+
+ const source_format = self._format.?;
+ const resource = self._resource.?;
+
+ if (parsed.format != source_format) {
+ // Only between the RGB formats: a channel shuffle.
+ if (source_format.isRgb() == false) return error.NotSupported;
+ if (options.colorSpace) |cs| {
+ if (std.mem.eql(u8, cs, "srgb") == false) return error.NotSupported;
+ }
+ }
+
+ var layouts: Layouts = .{ .items = undefined, .len = 0 };
+ for (parsed.format.planes(), 0..) |_, i| {
+ const computed = parsed.layout.planes[i];
+ const source_stride = resource.stride(i);
+ var src = resource.offset(i) + computed.source_top * source_stride + computed.source_left_bytes;
+ var dst = computed.offset;
+ for (0..computed.source_height) |_| {
+ const out = dest[@intCast(dst)..][0..@intCast(computed.source_width_bytes)];
+ const in = resource.data[@intCast(src)..][0..@intCast(computed.source_width_bytes)];
+ if (parsed.format == source_format) {
+ @memcpy(out, in);
+ } else {
+ convertRgbRow(source_format, parsed.format, in, out);
+ }
+ src += source_stride;
+ dst += computed.stride;
+ }
+ // The spec says the row bytes, but Chrome (and WPT) give the
+ // destination's stride, which is what a caller needs to read it back.
+ layouts.items[i] = .{ .offset = @intCast(computed.offset), .stride = @intCast(computed.stride) };
+ layouts.len += 1;
+ }
+ return layouts;
+}
+
+fn convertRgbRow(from: PixelFormat, to: PixelFormat, in: []const u8, out: []u8) void {
+ const from_bgr = from == .BGRA or from == .BGRX;
+ const to_bgr = to == .BGRA or to == .BGRX;
+ const opaque_source = from == .RGBX or from == .BGRX;
+ const opaque_dest = to == .RGBX or to == .BGRX;
+ var i: usize = 0;
+ while (i + 4 <= in.len) : (i += 4) {
+ const px = in[i..][0..4];
+ const r, const b = if (from_bgr) .{ px[2], px[0] } else .{ px[0], px[2] };
+ out[i + 0] = if (to_bgr) b else r;
+ out[i + 1] = px[1];
+ out[i + 2] = if (to_bgr) r else b;
+ out[i + 3] = if (opaque_source or opaque_dest) 255 else px[3];
+ }
+}
+
+fn clone(self: *const VideoFrame, exec: *const Execution) !*VideoFrame {
+ if (self.isClosed()) {
+ return error.InvalidStateError;
+ }
+ const resource = self._resource.?;
+ const copy = try create(resource.acquire(), exec.page);
+ const arena = copy._arena;
+ copy.* = self.*;
+ copy._rc = .{};
+ copy._arena = arena;
+ return copy;
+}
+
+// "Close VideoFrame". The timestamp and duration survive.
+pub fn close(self: *VideoFrame) void {
+ if (self._resource) |resource| {
+ resource.release();
+ self._resource = null;
+ }
+ self._format = null;
+ self._coded_width = 0;
+ self._coded_height = 0;
+ self._visible = .{ .x = 0, .y = 0, .width = 0, .height = 0 };
+ self._rotation = 0;
+ self._display_width = 0;
+ self._display_height = 0;
+ self._flip = false;
+ self._color_space = .{};
+}
+
+pub fn structuredSerialize(self: *const VideoFrame, writer: *js.StructuredWriter) !void {
+ const format = self._format orelse return error.DataClone;
+ const resource = self._resource.?;
+ writer.writeUint32(@backingInt(resource.format));
+ writer.writeUint32(resource.width);
+ writer.writeUint32(resource.height);
+ writer.writeBytes(resource.data);
+ writer.writeUint32(@backingInt(format));
+ writer.writeUint32(self._coded_width);
+ writer.writeUint32(self._coded_height);
+ writer.writeUint32(self._visible.x);
+ writer.writeUint32(self._visible.y);
+ writer.writeUint32(self._visible.width);
+ writer.writeUint32(self._visible.height);
+ writer.writeUint32(self._display_width);
+ writer.writeUint32(self._display_height);
+ writer.writeUint32(@intFromFloat(self._rotation));
+ writer.writeUint32(@intFromBool(self._flip));
+ writer.writeUint64(@bitCast(self._timestamp));
+ writer.writeUint32(@intFromBool(self._duration != null));
+ writer.writeUint64(self._duration orelse 0);
+ const cs = self._color_space;
+ writer.writeUint32(if (cs.primaries) |v| @as(u32, @backingInt(v)) + 1 else 0);
+ writer.writeUint32(if (cs.transfer) |v| @as(u32, @backingInt(v)) + 1 else 0);
+ writer.writeUint32(if (cs.matrix) |v| @as(u32, @backingInt(v)) + 1 else 0);
+ writer.writeUint32(if (cs.fullRange) |v| @as(u32, @intFromBool(v)) + 1 else 0);
+}
+
+fn readOptionalEnum(comptime E: type, reader: *js.StructuredReader) !?E {
+ const v = try reader.readUint32();
+ if (v == 0) return null;
+ return std.enums.fromInt(E, v - 1) orelse error.DataClone;
+}
+
+pub fn structuredDeserialize(reader: *js.StructuredReader, page: *Page) !*VideoFrame {
+ const resource_format = std.enums.fromInt(PixelFormat, try reader.readUint32()) orelse return error.DataClone;
+ const width = try reader.readUint32();
+ const height = try reader.readUint32();
+ const data = try reader.readBytes();
+ const format = std.enums.fromInt(PixelFormat, try reader.readUint32()) orelse return error.DataClone;
+ const coded_width = try reader.readUint32();
+ const coded_height = try reader.readUint32();
+ const visible: Rect = .{
+ .x = try reader.readUint32(),
+ .y = try reader.readUint32(),
+ .width = try reader.readUint32(),
+ .height = try reader.readUint32(),
+ };
+ const display_width = try reader.readUint32();
+ const display_height = try reader.readUint32();
+ const rotation: f64 = @floatFromInt(try reader.readUint32());
+ const flip = try reader.readUint32() != 0;
+ const timestamp: i64 = @bitCast(try reader.readUint64());
+ const has_duration = try reader.readUint32() != 0;
+ const duration = try reader.readUint64();
+ const color_space: VideoColorSpace.Value = .{
+ .primaries = try readOptionalEnum(VideoColorSpace.Primaries, reader),
+ .transfer = try readOptionalEnum(VideoColorSpace.Transfer, reader),
+ .matrix = try readOptionalEnum(VideoColorSpace.Matrix, reader),
+ .fullRange = blk: {
+ const v = try reader.readUint32();
+ break :blk if (v == 0) null else v == 2;
+ },
+ };
+
+ const resource = try Resource.create(resource_format, width, height, page);
+ if (data.len != resource.data.len) {
+ resource.release();
+ return error.DataClone;
+ }
+ @memcpy(resource.data, data);
+
+ const self = try create(resource, page);
+ self.* = .{
+ ._arena = self._arena,
+ ._resource = resource,
+ ._format = format,
+ ._coded_width = coded_width,
+ ._coded_height = coded_height,
+ ._visible = visible,
+ ._display_width = display_width,
+ ._display_height = display_height,
+ ._rotation = rotation,
+ ._flip = flip,
+ ._timestamp = timestamp,
+ ._duration = if (has_duration) duration else null,
+ ._color_space = color_space,
+ };
+ return self;
+}
+
+pub const JsApi = struct {
+ pub const bridge = js.Bridge(VideoFrame);
+
+ pub const Meta = struct {
+ pub const name = "VideoFrame";
+ pub const prototype_chain = bridge.prototypeChain();
+ pub var class_id: bridge.ClassId = undefined;
+ };
+
+ pub const constructor = bridge.constructor(VideoFrame.constructor, .{});
+ pub const format = bridge.accessor(VideoFrame.getFormat, null, .{});
+ pub const codedWidth = bridge.accessor(VideoFrame.getCodedWidth, null, .{});
+ pub const codedHeight = bridge.accessor(VideoFrame.getCodedHeight, null, .{});
+ pub const codedRect = bridge.accessor(VideoFrame.getCodedRect, null, .{});
+ pub const visibleRect = bridge.accessor(VideoFrame.getVisibleRect, null, .{});
+ pub const rotation = bridge.accessor(VideoFrame.getRotation, null, .{});
+ pub const flip = bridge.accessor(VideoFrame.getFlip, null, .{});
+ pub const displayWidth = bridge.accessor(VideoFrame.getDisplayWidth, null, .{});
+ pub const displayHeight = bridge.accessor(VideoFrame.getDisplayHeight, null, .{});
+ pub const duration = bridge.accessor(VideoFrame.getDuration, null, .{});
+ pub const timestamp = bridge.accessor(VideoFrame.getTimestamp, null, .{});
+ pub const colorSpace = bridge.accessor(VideoFrame.getColorSpace, null, .{});
+ pub const metadata = bridge.function(VideoFrame.metadata, .{});
+ pub const allocationSize = bridge.function(VideoFrame.allocationSize, .{});
+ pub const copyTo = bridge.function(VideoFrame.copyTo, .{});
+ pub const clone = bridge.function(VideoFrame.clone, .{});
+ pub const close = bridge.function(VideoFrame.close, .{});
+};
+
+const testing = @import("../../../testing.zig");
+test "WebApi: VideoFrame" {
+ try testing.htmlRunner("webcodecs/video_frame.html", .{});
+}
diff --git a/src/browser/webapi/webcodecs/codec.zig b/src/browser/webapi/webcodecs/codec.zig
new file mode 100644
index 000000000..8f612d9ff
--- /dev/null
+++ b/src/browser/webapi/webcodecs/codec.zig
@@ -0,0 +1,337 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+// The state machine shared by AudioDecoder, VideoDecoder, AudioEncoder and
+// VideoEncoder. We support no codec at all: every configuration that gets
+// past validation fails "Check Configuration Support", so a configure()
+// always ends, a task later, in the codec closing with a NotSupportedError.
+// https://w3c.github.io/webcodecs/#codec-processing-model-section
+
+const std = @import("std");
+const lp = @import("lightpanda");
+
+const js = @import("../../js/js.zig");
+const Page = @import("../../Page.zig");
+
+const Event = @import("../Event.zig");
+const EventTarget = @import("../EventTarget.zig");
+const DOMException = @import("../DOMException.zig");
+
+const Execution = js.Execution;
+
+const log = lp.log;
+
+pub const State = enum { unconfigured, configured, closed };
+
+pub const Init = struct {
+ @"error": js.Function.Global,
+ output: js.Function.Global,
+};
+
+// `Owner` is the interface: it has a `_core: Core(Owner)` field, plus
+// asEventTarget, acquireRef and releaseRef.
+pub fn Core(comptime Owner: type) type {
+ return struct {
+ const Self = @This();
+
+ exec: *Execution,
+ state: State = .unconfigured,
+ // [[decodeQueueSize]] / [[encodeQueueSize]]
+ queue_size: u32 = 0,
+ // decoders only
+ key_chunk_required: bool = false,
+ dequeue_event_scheduled: bool = false,
+ // Bumped by every reset: a pending configure message from before it
+ // has been dropped from the control queue.
+ generation: u32 = 0,
+ output: js.Function.Global,
+ error_callback: js.Function.Global,
+ // pending flush() promises
+ flushes: std.ArrayList(js.PromiseResolver.Global) = .empty,
+
+ pub fn init(callbacks: Init, exec: *Execution) Self {
+ return .{
+ .exec = exec,
+ .output = callbacks.output,
+ .error_callback = callbacks.@"error",
+ };
+ }
+
+ pub fn deinit(self: *Self) void {
+ for (self.flushes.items) |flush_resolver| {
+ flush_resolver.release();
+ }
+ self.output.release();
+ self.error_callback.release();
+ }
+
+ fn owner(self: *Self) *Owner {
+ return @fieldParentPtr("_core", self);
+ }
+
+ pub fn getState(self: *const Self) []const u8 {
+ return @tagName(self.state);
+ }
+
+ // configure(), after the config was found valid.
+ pub fn configure(self: *Self, comptime is_decoder: bool) !void {
+ if (self.state == .closed) {
+ return error.InvalidStateError;
+ }
+ self.state = .configured;
+ if (is_decoder) {
+ self.key_chunk_required = true;
+ }
+ try self.queueTask(.configure);
+ }
+
+ // The checks decode()/encode() make before enqueuing. The work itself
+ // never happens: the configure message ahead of it closes the codec.
+ pub fn enqueue(self: *Self) !void {
+ if (self.state != .configured) {
+ return error.InvalidStateError;
+ }
+ self.queue_size += 1;
+ }
+
+ // The promise belongs to the caller's realm, which needn't be the one
+ // the codec was created in: hence `exec`, rather than `self.exec`.
+ pub fn flush(self: *Self, comptime is_decoder: bool, exec: *const Execution) !js.Promise {
+ const local = exec.js.local.?;
+ if (self.state != .configured) {
+ return local.rejectPromise(.{ .dom_exception = .{ .err = error.InvalidStateError } });
+ }
+ if (is_decoder) {
+ self.key_chunk_required = true;
+ }
+ const resolver = local.createPromiseResolver();
+ const global = try resolver.persist();
+ errdefer global.release();
+ try self.flushes.append(self.owner()._arena.allocator(), global);
+ return resolver.promise();
+ }
+
+ pub fn reset(self: *Self) !void {
+ return self.resetWith(error.AbortError);
+ }
+
+ pub fn close(self: *Self) !void {
+ if (self.state == .closed) {
+ return error.InvalidStateError;
+ }
+ self.closeWith(error.AbortError);
+ }
+
+ // "Reset"
+ fn resetWith(self: *Self, exception: anyerror) !void {
+ if (self.state == .closed) {
+ return error.InvalidStateError;
+ }
+ self.state = .unconfigured;
+ self.clearQueues();
+ self.rejectFlushes(exception);
+ }
+
+ // "Close". In a browser, the flush promises' handlers only run once
+ // the whole algorithm is done, after the error callback. Rejecting
+ // runs them right away here, so the callback goes first.
+ fn closeWith(self: *Self, exception: anyerror) void {
+ self.state = .closed;
+ self.clearQueues();
+ if (exception != error.AbortError) {
+ self.reportError(exception);
+ }
+ self.rejectFlushes(exception);
+ }
+
+ // Drops pending control messages and work.
+ fn clearQueues(self: *Self) void {
+ self.generation +%= 1;
+ if (self.queue_size > 0) {
+ self.queue_size = 0;
+ self.queueTask(.dequeue) catch |err| {
+ log.warn(.js, "codec dequeue event", .{ .err = err });
+ };
+ }
+ }
+
+ fn rejectFlushes(self: *Self, exception: anyerror) void {
+ if (self.flushes.items.len == 0) {
+ return;
+ }
+ var ls: js.Local.Scope = undefined;
+ self.exec.js.localScope(&ls);
+ defer ls.deinit();
+
+ // taken first: rejecting can't re-enter, but keep it simple
+ const flushes = self.flushes.items;
+ self.flushes = .empty;
+ for (flushes) |global| {
+ global.local(&ls.local).rejectError("codec flush", .{ .dom_exception = .{ .err = exception } });
+ global.release();
+ }
+ }
+
+ fn reportError(self: *Self, exception: anyerror) void {
+ var ls: js.Local.Scope = undefined;
+ self.exec.js.localScope(&ls);
+ defer ls.deinit();
+
+ const dom_exception = DOMException.fromError(exception) orelse return;
+ ls.toLocal(self.error_callback).callWithThisReport(js.Undefined{}, .{dom_exception}) catch |err| {
+ log.debug(.js, "codec error callback", .{ .err = err });
+ };
+ }
+
+ const TaskKind = enum { configure, dequeue };
+
+ const Task = struct {
+ core: *Self,
+ kind: TaskKind,
+ generation: u32,
+
+ fn run(ctx: *anyopaque) !?u32 {
+ const task: *Task = @ptrCast(@alignCast(ctx));
+ const core = task.core;
+ const kind = task.kind;
+ const generation = task.generation;
+ const exec = core.exec;
+ exec._factory.destroy(task);
+ defer core.owner().releaseRef(exec.page);
+
+ switch (kind) {
+ // "Check Configuration Support" says no.
+ .configure => if (generation == core.generation and core.state == .configured) {
+ core.closeWith(error.NotSupported);
+ },
+ .dequeue => {
+ core.dequeue_event_scheduled = false;
+ const event = try Event.initTrusted(comptime .wrap("dequeue"), .{}, exec.page);
+ try exec.dispatch(core.owner().asEventTarget(), event, .{ .context = "codec dequeue" });
+ },
+ }
+ return null;
+ }
+
+ fn cancelled(ctx: *anyopaque) void {
+ const task: *Task = @ptrCast(@alignCast(ctx));
+ const core = task.core;
+ const exec = core.exec;
+ exec._factory.destroy(task);
+ core.owner().releaseRef(exec.page);
+ }
+ };
+
+ fn queueTask(self: *Self, comptime kind: TaskKind) !void {
+ if (kind == .dequeue) {
+ if (self.dequeue_event_scheduled) return;
+ self.dequeue_event_scheduled = true;
+ }
+ const exec = self.exec;
+ const task = try exec._factory.create(Task{ .core = self, .kind = kind, .generation = self.generation });
+ errdefer exec._factory.destroy(task);
+
+ const o = self.owner();
+ o.acquireRef();
+ errdefer o.releaseRef(exec.page);
+
+ try exec._scheduler.add(task, Task.run, 0, .{
+ .name = "webcodecs." ++ @tagName(kind),
+ .finalizer = Task.cancelled,
+ });
+ }
+ };
+}
+
+// Codec strings never have surrounding whitespace once valid: " opus " is
+// a valid config, but an unsupported one.
+pub fn isEmptyCodec(codec: []const u8) bool {
+ return std.mem.trim(u8, codec, &std.ascii.whitespace).len == 0;
+}
+
+// The [detached] check on a config's `description`.
+pub fn isDetachedBuffer(value: js.Value) bool {
+ const buffer = if (value.isArrayBufferView())
+ (value.toObject().get("buffer") catch return false)
+ else
+ value;
+ if (buffer.isArrayBuffer() == false) {
+ return false;
+ }
+ const detached = buffer.toObject().get("detached") catch return false;
+ return detached.isTrue();
+}
+
+// isConfigSupported(): its argument is converted by hand, so that an invalid
+// dictionary rejects the promise rather than throwing.
+pub fn isConfigSupported(comptime Config: type, value: js.Value, exec: *Execution) !js.Promise {
+ const local = exec.js.local.?;
+ const config = blk: {
+ const parsed = value.toZig(?Config) catch break :blk null;
+ break :blk parsed;
+ } orelse return local.rejectPromise(.{ .type_error = "invalid configuration" });
+
+ if (config.isValid() == false) {
+ return local.rejectPromise(.{ .type_error = "invalid configuration" });
+ }
+
+ const result = local.newObject();
+ _ = try result.set("supported", false, .{});
+ _ = try result.set("config", try cloneConfig(local, config), .{});
+ return local.resolvePromise(result);
+}
+
+// "Clone Configuration": the recognized members only, buffers copied.
+fn cloneConfig(local: *const js.Local, config: anytype) !js.Object {
+ const T = @TypeOf(config);
+ const object = local.newObject();
+ inline for (@typeInfo(T).@"struct".field_names) |name| {
+ const value = @field(config, name);
+ if (@typeInfo(@TypeOf(value)) == .optional) {
+ if (value) |v| {
+ try cloneMember(local, object, name, v);
+ }
+ } else {
+ try cloneMember(local, object, name, value);
+ }
+ }
+ return object;
+}
+
+fn cloneMember(local: *const js.Local, object: js.Object, comptime name: []const u8, value: anytype) !void {
+ const T = @TypeOf(value);
+ if (T == js.Value) {
+ // description: an AllowSharedBufferSource
+ const bytes = (try value.toZig(js.AllowSharedBufferSource)).bytes;
+ _ = try object.set(name, js.ArrayBuffer{ .values = bytes }, .{});
+ } else if (T != []const u8 and @typeInfo(T) == .@"struct") {
+ _ = try object.set(name, try cloneConfig(local, value), .{});
+ } else {
+ _ = try object.set(name, value, .{});
+ }
+}
+
+// Every enum-typed config member is taken as a string, and checked here.
+pub fn checkEnum(value: []const u8, comptime allowed: []const []const u8) bool {
+ inline for (allowed) |a| {
+ if (std.mem.eql(u8, value, a)) return true;
+ }
+ return false;
+}
+
+pub const hardware_acceleration = &.{ "no-preference", "prefer-hardware", "prefer-software" };
diff --git a/src/browser/webapi/webcodecs/common.zig b/src/browser/webapi/webcodecs/common.zig
new file mode 100644
index 000000000..584fdb1f6
--- /dev/null
+++ b/src/browser/webapi/webcodecs/common.zig
@@ -0,0 +1,98 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+// Helpers shared by the WebCodecs interfaces.
+
+const std = @import("std");
+
+const js = @import("../../js/js.zig");
+
+// WebIDL [EnforceRange]: we take such members as f64 since our integer
+// conversion wraps (ToInt32 and friends) instead of throwing.
+pub fn enforceRange(comptime T: type, value: f64) !T {
+ if (std.math.isFinite(value) == false) {
+ return error.TypeError;
+ }
+ const truncated = @trunc(value);
+ const min: f64 = switch (T) {
+ u32 => 0,
+ u64 => 0,
+ i64 => -9007199254740991,
+ else => @compileError("unsupported [EnforceRange] type"),
+ };
+ const max: f64 = switch (T) {
+ u32 => 4294967295,
+ u64, i64 => 9007199254740991,
+ else => unreachable,
+ };
+ if (truncated < min or truncated > max) {
+ return error.TypeError;
+ }
+ return @intFromFloat(truncated);
+}
+
+pub fn optionalEnforceRange(comptime T: type, value: ?f64) !?T {
+ return try enforceRange(T, value orelse return null);
+}
+
+// The `transfer` member of the WebCodecs init dictionaries. Checked before
+// anything is copied, and detached once the copy is made.
+pub const Transfer = struct {
+ buffers: []const js.Value,
+
+ // DataCloneError for a duplicate or an already detached ArrayBuffer.
+ pub fn check(self: Transfer) !void {
+ for (self.buffers, 0..) |buffer, i| {
+ if (buffer.isArrayBuffer() == false) {
+ return error.TypeError;
+ }
+ if (isDetached(buffer)) {
+ return error.DataClone;
+ }
+ for (self.buffers[0..i]) |previous| {
+ if (previous.strictEquals(buffer)) {
+ return error.DataClone;
+ }
+ }
+ }
+ }
+
+ // We always copy the data, so transferring is just detaching.
+ pub fn detach(self: Transfer) !void {
+ for (self.buffers) |buffer| {
+ // ArrayBuffer.prototype.transfer() detaches the receiver. The
+ // new buffer it returns is simply dropped.
+ _ = try buffer.toObject().callMethod(js.Value, "transfer", .{@as(u32, 0)});
+ }
+ }
+
+ fn isDetached(buffer: js.Value) bool {
+ const detached = buffer.toObject().get("detached") catch return false;
+ return detached.isTrue();
+ }
+};
+
+const testing = std.testing;
+test "webcodecs: enforceRange" {
+ try testing.expectEqual(@as(u32, 5), try enforceRange(u32, 5.9));
+ try testing.expectEqual(@as(i64, -10), try enforceRange(i64, -10));
+ try testing.expectError(error.TypeError, enforceRange(u32, -1));
+ try testing.expectError(error.TypeError, enforceRange(u32, 4294967296));
+ try testing.expectError(error.TypeError, enforceRange(i64, std.math.nan(f64)));
+ try testing.expectError(error.TypeError, enforceRange(u64, std.math.inf(f64)));
+}
diff --git a/src/browser/webapi/webcodecs/encoded_chunk.zig b/src/browser/webapi/webcodecs/encoded_chunk.zig
new file mode 100644
index 000000000..f9df0d4c3
--- /dev/null
+++ b/src/browser/webapi/webcodecs/encoded_chunk.zig
@@ -0,0 +1,168 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+// EncodedAudioChunk and EncodedVideoChunk: the same interface under two names.
+// https://w3c.github.io/webcodecs/#encodedaudiochunk-interface
+
+const std = @import("std");
+const lp = @import("lightpanda");
+
+const js = @import("../../js/js.zig");
+const Page = @import("../../Page.zig");
+const common = @import("common.zig");
+
+const Execution = js.Execution;
+
+pub const EncodedAudioChunk = EncodedChunk("EncodedAudioChunk");
+pub const EncodedVideoChunk = EncodedChunk("EncodedVideoChunk");
+
+pub fn registerTypes() []const type {
+ return &.{ EncodedAudioChunk, EncodedVideoChunk };
+}
+
+pub const Type = enum { key, delta };
+
+fn EncodedChunk(comptime interface_name: [:0]const u8) type {
+ return struct {
+ const Self = @This();
+
+ _rc: lp.RC = .{},
+ _arena: *lp.Arena,
+ _type: Type,
+ _timestamp: i64,
+ _duration: ?u64,
+ _data: []const u8,
+
+ const Init = struct {
+ data: js.AllowSharedBufferSource,
+ duration: ?f64 = null,
+ timestamp: f64,
+ transfer: []const js.Value = &.{},
+ type: []const u8,
+ };
+
+ pub fn init(init_: Init, exec: *const Execution) !*Self {
+ const chunk_type = std.meta.stringToEnum(Type, init_.type) orelse return error.TypeError;
+ const timestamp = try common.enforceRange(i64, init_.timestamp);
+ const duration = try common.optionalEnforceRange(u64, init_.duration);
+
+ const transfer: common.Transfer = .{ .buffers = init_.transfer };
+ try transfer.check();
+
+ const self = try create(chunk_type, timestamp, duration, init_.data.bytes, exec.page);
+ errdefer self.deinit(exec.page);
+
+ try transfer.detach();
+ return self;
+ }
+
+ fn create(chunk_type: Type, timestamp: i64, duration: ?u64, data: []const u8, page: *Page) !*Self {
+ const arena = try page.getPinnedArena(data.len + 256, interface_name);
+ errdefer arena.release();
+
+ const self = try arena.create(Self);
+ self.* = .{
+ ._arena = arena,
+ ._type = chunk_type,
+ ._timestamp = timestamp,
+ ._duration = duration,
+ ._data = try arena.dupe(u8, data),
+ };
+ arena.report();
+ return self;
+ }
+
+ pub fn deinit(self: *Self, _: *Page) void {
+ self._arena.release();
+ }
+
+ pub fn acquireRef(self: *Self) void {
+ self._rc.acquire();
+ }
+
+ pub fn releaseRef(self: *Self, page: *Page) void {
+ self._rc.release(self, page);
+ }
+
+ pub fn bytes(self: *const Self) []const u8 {
+ return self._data;
+ }
+
+ fn getType(self: *const Self) []const u8 {
+ return @tagName(self._type);
+ }
+
+ fn getTimestamp(self: *const Self) i64 {
+ return self._timestamp;
+ }
+
+ fn getDuration(self: *const Self) ?u64 {
+ return self._duration;
+ }
+
+ fn getByteLength(self: *const Self) u32 {
+ return @intCast(self._data.len);
+ }
+
+ fn copyTo(self: *const Self, destination: js.AllowSharedBufferSource) !void {
+ if (destination.bytes.len < self._data.len) {
+ return error.TypeError;
+ }
+ @memcpy(destination.bytes[0..self._data.len], self._data);
+ }
+
+ pub fn structuredSerialize(self: *const Self, writer: *js.StructuredWriter) !void {
+ writer.writeUint32(@backingInt(self._type));
+ writer.writeUint64(@bitCast(self._timestamp));
+ writer.writeUint32(@intFromBool(self._duration != null));
+ writer.writeUint64(self._duration orelse 0);
+ writer.writeBytes(self._data);
+ }
+
+ pub fn structuredDeserialize(reader: *js.StructuredReader, page: *Page) !*Self {
+ const chunk_type = std.enums.fromInt(Type, try reader.readUint32()) orelse return error.DataClone;
+ const timestamp: i64 = @bitCast(try reader.readUint64());
+ const has_duration = try reader.readUint32() != 0;
+ const duration = try reader.readUint64();
+ const data = try reader.readBytes();
+ return create(chunk_type, timestamp, if (has_duration) duration else null, data, page);
+ }
+
+ pub const JsApi = struct {
+ pub const bridge = js.Bridge(Self);
+
+ pub const Meta = struct {
+ pub const name = interface_name;
+ pub const prototype_chain = bridge.prototypeChain();
+ pub var class_id: bridge.ClassId = undefined;
+ };
+
+ pub const constructor = bridge.constructor(Self.init, .{});
+ pub const @"type" = bridge.accessor(Self.getType, null, .{});
+ pub const timestamp = bridge.accessor(Self.getTimestamp, null, .{});
+ pub const duration = bridge.accessor(Self.getDuration, null, .{});
+ pub const byteLength = bridge.accessor(Self.getByteLength, null, .{});
+ pub const copyTo = bridge.function(Self.copyTo, .{});
+ };
+ };
+}
+
+const testing = @import("../../../testing.zig");
+test "WebApi: EncodedChunk" {
+ try testing.htmlRunner("webcodecs/encoded_chunk.html", .{});
+}