mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-09-29 03:43:50 +02:00
Merge branch 'v2' into cursors
This commit is contained in:
@@ -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' });
|
||||
});
|
||||
@@ -0,0 +1,54 @@
|
||||
import { expect, test } from 'vitest';
|
||||
import { Input } from '../../src/input.js';
|
||||
import { BufferSource, FilePathSource } from '../../src/source.js';
|
||||
import path from 'node:path';
|
||||
import { ALL_FORMATS } from '../../src/input-format.js';
|
||||
import { Output } from '../../src/output.js';
|
||||
import { Mp4OutputFormat } from '../../src/output-format.js';
|
||||
import { BufferTarget } from '../../src/target.js';
|
||||
import { Conversion } from '../../src/conversion.js';
|
||||
import { EncodedPacketSink } from '../../src/media-sink.js';
|
||||
import { extractAvcNalUnits } from '../../src/codec-data.js';
|
||||
|
||||
const __dirname = new URL('.', import.meta.url).pathname;
|
||||
|
||||
test('Annex B to length-prefixed conversion, MP4', async () => {
|
||||
using originalInput = new Input({
|
||||
source: new FilePathSource(path.join(__dirname, '..', 'public/annex-b-avc.mkv')),
|
||||
formats: ALL_FORMATS,
|
||||
});
|
||||
const originalVideoTrack = (await originalInput.getPrimaryVideoTrack())!;
|
||||
const originalDecoderConfig = (await originalVideoTrack.getDecoderConfig())!;
|
||||
expect(originalDecoderConfig.description).toBeUndefined();
|
||||
expect(originalVideoTrack.codec).toBe('avc');
|
||||
|
||||
const originalSink = new EncodedPacketSink(originalVideoTrack);
|
||||
const originalFirstPacket = await originalSink.getFirstPacket();
|
||||
expect([...originalFirstPacket!.data.slice(0, 4)]).toEqual([0, 0, 0, 1]);
|
||||
|
||||
const originalNalUnits = extractAvcNalUnits(originalFirstPacket!.data, originalDecoderConfig);
|
||||
|
||||
const output = new Output({
|
||||
format: new Mp4OutputFormat(),
|
||||
target: new BufferTarget(),
|
||||
});
|
||||
|
||||
const conversion = await Conversion.init({ input: originalInput, output });
|
||||
await conversion.execute();
|
||||
|
||||
using newInput = new Input({
|
||||
source: new BufferSource(output.target.buffer!),
|
||||
formats: ALL_FORMATS,
|
||||
});
|
||||
const newVideoTrack = (await newInput.getPrimaryVideoTrack())!;
|
||||
const newDecoderConfig = (await newVideoTrack.getDecoderConfig())!;
|
||||
expect(newDecoderConfig.description).toBeDefined();
|
||||
expect(newVideoTrack.codec).toBe('avc');
|
||||
|
||||
const newSink = new EncodedPacketSink(newVideoTrack);
|
||||
const newFirstPacket = await newSink.getFirstPacket();
|
||||
expect([...newFirstPacket!.data.slice(0, 4)]).not.toEqual([0, 0, 0, 1]); // Successfully converted
|
||||
|
||||
const newNalUnits = extractAvcNalUnits(newFirstPacket!.data, newDecoderConfig);
|
||||
expect(newNalUnits).toEqual(originalNalUnits); // Content is the same though
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
import { test } from 'vitest';
|
||||
import { AudioSample, VideoSample } from '../../src/sample.js';
|
||||
|
||||
test('VideoSample creation from bytes', async () => {
|
||||
const bytes = new Uint8Array(1024);
|
||||
const sample = new VideoSample(bytes, {
|
||||
codedWidth: 1280,
|
||||
codedHeight: 720,
|
||||
format: 'RGBA',
|
||||
timestamp: 0,
|
||||
});
|
||||
|
||||
sample.close();
|
||||
});
|
||||
|
||||
test('AudioSample creation from bytes', async () => {
|
||||
const bytes = new Uint8Array(1024);
|
||||
const sample = new AudioSample({
|
||||
data: bytes,
|
||||
numberOfChannels: 2,
|
||||
format: 's16',
|
||||
sampleRate: 48000,
|
||||
timestamp: 0,
|
||||
});
|
||||
|
||||
sample.close();
|
||||
});
|
||||
Binary file not shown.
Reference in New Issue
Block a user