Merge branch 'main' into v2

This commit is contained in:
Vanilagy
2025-12-29 21:19:20 +01:00
66 changed files with 2463 additions and 936 deletions
+1 -1
View File
@@ -19,7 +19,7 @@ test('can decode samples from a FLAC file', async () => {
const sink = new AudioSampleSink(track);
const sample = await sink.getSample(1);
using sample = await sink.getSample(1);
assert(sample);
expect(sample.timestamp).toBe(0.9287981859410431);
});
+9 -9
View File
@@ -21,7 +21,7 @@ test('Can decode transparent video', async () => {
expect(await videoTrack.canBeTransparent()).toBe(true);
const sink = new VideoSampleSink(videoTrack);
const sample = (await sink.getSample(0.5))!;
using sample = (await sink.getSample(0.5))!;
expect(sample.format).toContain('A'); // Probably RGBA
expect(sample.hasAlpha).toBe(true);
@@ -47,10 +47,10 @@ test('Can decode faulty transparent video and behaves gracefully', async () => {
const sink = new VideoSampleSink(videoTrack);
const startSample = (await sink.getSample(await videoTrack.getFirstTimestamp()))!;
using startSample = (await sink.getSample(await videoTrack.getFirstTimestamp()))!;
expect(startSample.format).toContain('A');
const secondSample = (await sink.getSample(secondKeyPacket.timestamp))!;
using secondSample = (await sink.getSample(secondKeyPacket.timestamp))!;
expect(secondSample.format).not.toContain('A'); // There was no alpha key frame for this one
expect(secondSample.hasAlpha).toBe(false);
});
@@ -164,7 +164,7 @@ test('Can encode transparent video', async () => {
const sink = new VideoSampleSink(videoTrack);
const firstSample = (await sink.getSample(0))!;
using firstSample = (await sink.getSample(0))!;
expect(firstSample.format).toContain('A');
probeContext.clearRect(0, 0, probeCanvas.width, probeCanvas.height);
@@ -201,7 +201,7 @@ test('Can encode video with alternating transparency', async () => {
await output.start();
for (let i = 0; i < 64; i++) {
const sample = new VideoSample(new Uint8Array(640 * 480 * 4), {
using sample = new VideoSample(new Uint8Array(640 * 480 * 4), {
format: i % 2 ? 'RGBX' : 'RGBA',
codedWidth: 640,
codedHeight: 480,
@@ -235,7 +235,7 @@ test('Can encode video with alternating transparency', async () => {
const sampleSink = new VideoSampleSink(videoTrack);
i = 0;
for await (const sample of sampleSink.samples()) {
for await (using sample of sampleSink.samples()) {
if (i % 2) {
expect(sample.format).not.toContain('A');
} else {
@@ -299,7 +299,7 @@ test('Can transmux transparent video, discards alpha by default', async () => {
expect(await videoTrack.canBeTransparent()).toBe(false);
const sink = new VideoSampleSink(videoTrack);
const sample = (await sink.getSample(await videoTrack.getFirstTimestamp()))!;
using sample = (await sink.getSample(await videoTrack.getFirstTimestamp()))!;
expect(sample.hasAlpha).toBe(false);
});
@@ -331,7 +331,7 @@ test('Can transmux transparent video, can keep alpha', async () => {
expect(await videoTrack.canBeTransparent()).toBe(true);
const sink = new VideoSampleSink(videoTrack);
const sample = (await sink.getSample(await videoTrack.getFirstTimestamp()))!;
using sample = (await sink.getSample(await videoTrack.getFirstTimestamp()))!;
expect(sample.format).toContain('A');
expect(sample.hasAlpha).toBe(true);
});
@@ -370,7 +370,7 @@ test('Can reencode transparent video, keeping alpha', async () => {
expect(videoTrack.displayWidth).toBe(320);
const sink = new VideoSampleSink(videoTrack);
const sample = (await sink.getSample(await videoTrack.getFirstTimestamp()))!;
using sample = (await sink.getSample(await videoTrack.getFirstTimestamp()))!;
expect(sample.format).toContain('A');
expect(sample.hasAlpha).toBe(true);
});
+220
View File
@@ -0,0 +1,220 @@
import { expect, test } from 'vitest';
import { VideoSample } from '../../src/sample.js';
test('allocationSize', async () => {
{
const canvas = new OffscreenCanvas(1280, 720);
canvas.getContext('2d');
using sample = new VideoSample(canvas, { timestamp: 0 });
const size1 = sample.allocationSize();
expect(size1).toBe(1280 * 720 * 4);
const size2 = sample.allocationSize({
layout: [{
offset: 0,
stride: 1300 * 4,
}],
});
expect(size2).toBe(1300 * 720 * 4);
}
{
const data = new Uint8Array(1280 * 720 * 4);
using sample = new VideoSample(data, {
timestamp: 0,
codedWidth: 1280,
codedHeight: 720,
format: 'RGBA',
});
const size1 = sample.allocationSize();
expect(size1).toBe(1280 * 720 * 4);
const size2 = sample.allocationSize({
layout: [{
offset: 0,
stride: 1300 * 4,
}],
});
expect(size2).toBe(1300 * 720 * 4);
}
{
const data = new Uint8Array(1280 * 720 * 1.5);
using sample = new VideoSample(data, {
timestamp: 0,
codedWidth: 1280,
codedHeight: 720,
format: 'I420',
});
const size1 = sample.allocationSize();
expect(size1).toBe(1280 * 720 * 1.5);
const size2 = sample.allocationSize({ format: 'RGBA' });
expect(size2).toBe(1280 * 720 * 4);
const size3 = sample.allocationSize({
format: 'RGBA',
layout: [{
offset: 0,
stride: 1300 * 4,
}],
});
expect(size3).toBe(1300 * 720 * 4);
}
});
test('copyTo and plane layouts', async () => {
const buffer = new ArrayBuffer(1e7);
{
const canvas = new OffscreenCanvas(1280, 720);
canvas.getContext('2d');
using sample = new VideoSample(canvas, { timestamp: 0 });
const layout = await sample.copyTo(buffer);
expect(layout).toEqual([{
offset: 0,
stride: 1280 * 4,
}]);
}
{
const canvas = new OffscreenCanvas(1280, 720);
canvas.getContext('2d');
using sample = new VideoSample(canvas, { timestamp: 0 });
const layout = await sample.copyTo(buffer, {
layout: [{
offset: 0,
stride: 1300 * 4,
}],
});
expect(layout).toEqual([{
offset: 0,
stride: 1300 * 4,
}]);
}
{
const data = new Uint8Array(1280 * 720 * 4);
using sample = new VideoSample(data, {
timestamp: 0,
codedWidth: 1280,
codedHeight: 720,
format: 'RGBA',
});
const layout = await sample.copyTo(buffer);
expect(layout).toEqual([{
offset: 0,
stride: 1280 * 4,
}]);
}
{
const data = new Uint8Array(1280 * 720 * 4);
using sample = new VideoSample(data, {
timestamp: 0,
codedWidth: 1280,
codedHeight: 720,
format: 'RGBA',
});
const layout = await sample.copyTo(buffer, {
layout: [{
offset: 0,
stride: 1300 * 4,
}],
});
expect(layout).toEqual([{
offset: 0,
stride: 1300 * 4,
}]);
}
{
const data = new Uint8Array(1300 * 720 * 4);
using sample = new VideoSample(data, {
timestamp: 0,
codedWidth: 1280,
codedHeight: 720,
format: 'RGBA',
layout: [{
offset: 0,
stride: 1300 * 4,
}],
});
const layout = await sample.copyTo(buffer);
expect(layout).toEqual([{
offset: 0,
stride: 1300 * 4,
}]);
using clone = sample.clone();
const clonedLayout = await clone.copyTo(buffer);
expect(clonedLayout).toEqual([{
offset: 0,
stride: 1300 * 4,
}]);
}
{
const data = new Uint8Array(1280 * 720 * 1.5);
using sample = new VideoSample(data, {
timestamp: 0,
codedWidth: 1280,
codedHeight: 720,
format: 'I420',
});
const layout = await sample.copyTo(buffer);
expect(layout).toEqual([{
offset: 0,
stride: 1280,
}, {
offset: 1280 * 720,
stride: 1280 / 2,
}, {
offset: 1280 * 720 + (1280 / 2) * (720 / 2),
stride: 1280 / 2,
}]);
}
});
test('null format', async () => {
const canvas = new OffscreenCanvas(1280, 720);
canvas.getContext('2d');
const frame = new VideoFrame(canvas, { timestamp: 0 });
using sample = new VideoSample(frame);
// @ts-expect-error Sybau
sample.format = null;
expect(() => sample.allocationSize()).toThrow('when format is null');
await expect(async () => sample.copyTo(new ArrayBuffer())).rejects.toThrow('when format is null');
const size = sample.allocationSize({ format: 'RGBA' });
expect(size).toBe(1280 * 720 * 4);
const buffer = new ArrayBuffer(size);
const layout = await sample.copyTo(buffer, { format: 'RGBA' });
expect(layout).toEqual([{
offset: 0,
stride: 1280 * 4,
}]);
sample.allocationSize({ format: 'RGBX' });
sample.allocationSize({ format: 'BGRA' });
sample.allocationSize({ format: 'BGRX' });
await sample.copyTo(buffer, { format: 'RGBX' });
await sample.copyTo(buffer, { format: 'BGRA' });
await sample.copyTo(buffer, { format: 'BGRX' });
});