alpha2 0.2.2 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -4
- package/dist/containers.d.ts +89 -0
- package/dist/containers.js +150 -0
- package/dist/effects.d.ts +12 -2
- package/dist/frame-supply.d.ts +33 -0
- package/dist/frame-supply.js +161 -23
- package/dist/generated/project-schema.d.ts +564 -100
- package/dist/generated/project-schema.js +36 -0
- package/dist/generated/project-shape.js +345 -55
- package/dist/geometry.d.ts +98 -0
- package/dist/geometry.js +71 -0
- package/dist/hdr-frames.d.ts +16 -6
- package/dist/hdr-frames.js +47 -39
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/mediabunny.d.ts +36 -27
- package/dist/mediabunny.js +62 -15
- package/dist/player.d.ts +5 -0
- package/dist/project.d.ts +26 -2
- package/dist/project.js +358 -55
- package/dist/worker.js +1 -0
- package/package.json +9 -1
- package/wasm/alpha2_extensions_bg.wasm +0 -0
- package/wasm/alpha2_wasm.d.ts +116 -8
- package/wasm/alpha2_wasm.js +369 -34
- package/wasm/alpha2_wasm_bg.wasm +0 -0
- package/wasm/alpha2_wasm_bg.wasm.d.ts +16 -4
- /package/wasm/snippets/{alpha2-wasm-e7f44ec6b15db6c6 → alpha2-wasm-c9e145ef5a7999c5}/inline0.js +0 -0
- /package/wasm/snippets/{alpha2-wasm-e7f44ec6b15db6c6 → alpha2-wasm-c9e145ef5a7999c5}/inline1.js +0 -0
- /package/wasm/snippets/{alpha2-wasm-e7f44ec6b15db6c6 → alpha2-wasm-c9e145ef5a7999c5}/inline2.js +0 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shape geometry for editors, answered in wasm by the code the engine draws shapes with: where a
|
|
3
|
+
* shapes generator's items are, which paint is under a point, how long a path is, and paths cut,
|
|
4
|
+
* resampled, or converted. A hit test or a trim here agrees with what a frame shows.
|
|
5
|
+
*
|
|
6
|
+
* Positions are normalized as the project document's are, `(0, 0)` top-left to `(1, 1)`
|
|
7
|
+
* bottom-right of a `width` by `height` picture; lengths and distances are in its pixels. Times
|
|
8
|
+
* are ticks, as keyframe times are.
|
|
9
|
+
*
|
|
10
|
+
* Every function throws an `INVALID_INPUT` error for a picture outside 1 to 16384 pixels a side, a
|
|
11
|
+
* time that is not a whole number of ticks, or input the model rejects (a content tree or geometry
|
|
12
|
+
* that is not valid, a path point or tangent that is not finite).
|
|
13
|
+
*/
|
|
14
|
+
import type { AnimatedPoint, Path, ShapeGeometry, ShapeItem, Ticks, Vec2 } from "./project.js";
|
|
15
|
+
/** The part of `alpha2/wasm` these functions use. */
|
|
16
|
+
export interface GeometryBinding {
|
|
17
|
+
shapeBounds(itemsJson: string, time: number, width: number, height: number): string;
|
|
18
|
+
hitTest(itemsJson: string, time: number, width: number, height: number, x: number, y: number): string | undefined;
|
|
19
|
+
nearestPoint(pathJson: string, width: number, height: number, x: number, y: number): string;
|
|
20
|
+
pathLength(pathJson: string, width: number, height: number): string;
|
|
21
|
+
trimPath(pathJson: string, width: number, height: number, start: number, end: number, offset: number): string;
|
|
22
|
+
resamplePath(pathJson: string, width: number, height: number, vertexCounts: Uint32Array): string;
|
|
23
|
+
toPath(geometryJson: string, time: number, width: number, height: number): string;
|
|
24
|
+
motionPath(pointJson: string, width: number, height: number): string;
|
|
25
|
+
}
|
|
26
|
+
/** The size of the picture shapes are drawn on, in pixels. */
|
|
27
|
+
export interface PictureSize {
|
|
28
|
+
readonly width: number;
|
|
29
|
+
readonly height: number;
|
|
30
|
+
}
|
|
31
|
+
/** A normalized rectangle: `x0`, `y0` its top-left corner, `x1`, `y1` its bottom-right. */
|
|
32
|
+
export interface Bounds {
|
|
33
|
+
readonly x0: number;
|
|
34
|
+
readonly y0: number;
|
|
35
|
+
readonly x1: number;
|
|
36
|
+
readonly y1: number;
|
|
37
|
+
}
|
|
38
|
+
/** An item's bounds, by its place in the tree (`items[0].items[2]`). */
|
|
39
|
+
export interface ItemBounds {
|
|
40
|
+
readonly at: string;
|
|
41
|
+
/** What it covers: geometry's outline, a paint's area; `null` for an item that shows nothing. */
|
|
42
|
+
readonly bounds: Bounds | null;
|
|
43
|
+
}
|
|
44
|
+
/** The point of a path nearest another. */
|
|
45
|
+
export interface NearestPoint {
|
|
46
|
+
/** The subpath. */
|
|
47
|
+
readonly subpath: number;
|
|
48
|
+
/** The segment within it: segment `i` runs from vertex `i` to the next. */
|
|
49
|
+
readonly segment: number;
|
|
50
|
+
/** The parameter along the segment, in `0..=1`. */
|
|
51
|
+
readonly t: number;
|
|
52
|
+
/** The point, normalized. */
|
|
53
|
+
readonly point: Vec2;
|
|
54
|
+
/** Its distance from the point asked about, in pixels. */
|
|
55
|
+
readonly distance: number;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Each item's bounds at `time`, in document order: a group's are its items', a stroke's include
|
|
59
|
+
* its width.
|
|
60
|
+
*
|
|
61
|
+
* @throws When `items` is not a valid content tree.
|
|
62
|
+
*/
|
|
63
|
+
export declare function shapeBounds(binding: GeometryBinding, items: readonly ShapeItem[], time: Ticks, size: PictureSize): ItemBounds[];
|
|
64
|
+
/**
|
|
65
|
+
* The front-most fill or stroke at normalized `point` at `time`, by its fill rule (a stroke by
|
|
66
|
+
* its outline): its place in the tree, or `undefined` where nothing is painted.
|
|
67
|
+
*
|
|
68
|
+
* @throws When `items` is not a valid content tree.
|
|
69
|
+
*/
|
|
70
|
+
export declare function hitTest(binding: GeometryBinding, items: readonly ShapeItem[], time: Ticks, size: PictureSize, point: Vec2): string | undefined;
|
|
71
|
+
/** The point of `path` nearest normalized `point`, or `undefined` for an empty path. */
|
|
72
|
+
export declare function nearestPoint(binding: GeometryBinding, path: Path, size: PictureSize, point: Vec2): NearestPoint | undefined;
|
|
73
|
+
/** Each subpath's length, in pixels. */
|
|
74
|
+
export declare function pathLength(binding: GeometryBinding, path: Path, size: PictureSize): number[];
|
|
75
|
+
/**
|
|
76
|
+
* The part of `path` a trim item keeps: from `start` to `end` (fractions of its pixel length, all
|
|
77
|
+
* subpaths end to end, clamped to `0..=1`), shifted by `offset` turns. Nothing when `start` is not
|
|
78
|
+
* before `end`.
|
|
79
|
+
*/
|
|
80
|
+
export declare function trimPath(binding: GeometryBinding, path: Path, size: PictureSize, start: number, end: number, offset?: number): Path;
|
|
81
|
+
/**
|
|
82
|
+
* `path` with `vertexCounts[i]` vertices on subpath `i`, evenly along its length, so two paths can
|
|
83
|
+
* be given the same structure and their keyframes can morph. The engine never resamples on its
|
|
84
|
+
* own. A subpath whose count is missing, below 2 (3 when closed), or that has no length is kept as
|
|
85
|
+
* it is.
|
|
86
|
+
*
|
|
87
|
+
* @throws When a count is over `MAX_PATH_VERTICES`.
|
|
88
|
+
*/
|
|
89
|
+
export declare function resamplePath(binding: GeometryBinding, path: Path, size: PictureSize, vertexCounts: readonly number[]): Path;
|
|
90
|
+
/**
|
|
91
|
+
* A rectangle, ellipse, or polystar at `time` as a path of the same outline, from the same start
|
|
92
|
+
* point and in the same direction: "convert to path".
|
|
93
|
+
*
|
|
94
|
+
* @throws When `geometry` is not valid.
|
|
95
|
+
*/
|
|
96
|
+
export declare function toPath(binding: GeometryBinding, geometry: ShapeGeometry, time: Ticks, size: PictureSize): Path;
|
|
97
|
+
/** The polyline a position travels between its keyframes, along their spatial tangents. */
|
|
98
|
+
export declare function motionPath(binding: GeometryBinding, position: AnimatedPoint, size: PictureSize): Vec2[];
|
package/dist/geometry.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shape geometry for editors, answered in wasm by the code the engine draws shapes with: where a
|
|
3
|
+
* shapes generator's items are, which paint is under a point, how long a path is, and paths cut,
|
|
4
|
+
* resampled, or converted. A hit test or a trim here agrees with what a frame shows.
|
|
5
|
+
*
|
|
6
|
+
* Positions are normalized as the project document's are, `(0, 0)` top-left to `(1, 1)`
|
|
7
|
+
* bottom-right of a `width` by `height` picture; lengths and distances are in its pixels. Times
|
|
8
|
+
* are ticks, as keyframe times are.
|
|
9
|
+
*
|
|
10
|
+
* Every function throws an `INVALID_INPUT` error for a picture outside 1 to 16384 pixels a side, a
|
|
11
|
+
* time that is not a whole number of ticks, or input the model rejects (a content tree or geometry
|
|
12
|
+
* that is not valid, a path point or tangent that is not finite).
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Each item's bounds at `time`, in document order: a group's are its items', a stroke's include
|
|
16
|
+
* its width.
|
|
17
|
+
*
|
|
18
|
+
* @throws When `items` is not a valid content tree.
|
|
19
|
+
*/
|
|
20
|
+
export function shapeBounds(binding, items, time, size) {
|
|
21
|
+
return JSON.parse(binding.shapeBounds(JSON.stringify(items), time, size.width, size.height));
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The front-most fill or stroke at normalized `point` at `time`, by its fill rule (a stroke by
|
|
25
|
+
* its outline): its place in the tree, or `undefined` where nothing is painted.
|
|
26
|
+
*
|
|
27
|
+
* @throws When `items` is not a valid content tree.
|
|
28
|
+
*/
|
|
29
|
+
export function hitTest(binding, items, time, size, point) {
|
|
30
|
+
return (binding.hitTest(JSON.stringify(items), time, size.width, size.height, point.x, point.y) ?? undefined);
|
|
31
|
+
}
|
|
32
|
+
/** The point of `path` nearest normalized `point`, or `undefined` for an empty path. */
|
|
33
|
+
export function nearestPoint(binding, path, size, point) {
|
|
34
|
+
return (JSON.parse(binding.nearestPoint(JSON.stringify(path), size.width, size.height, point.x, point.y)) ?? undefined);
|
|
35
|
+
}
|
|
36
|
+
/** Each subpath's length, in pixels. */
|
|
37
|
+
export function pathLength(binding, path, size) {
|
|
38
|
+
return JSON.parse(binding.pathLength(JSON.stringify(path), size.width, size.height));
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The part of `path` a trim item keeps: from `start` to `end` (fractions of its pixel length, all
|
|
42
|
+
* subpaths end to end, clamped to `0..=1`), shifted by `offset` turns. Nothing when `start` is not
|
|
43
|
+
* before `end`.
|
|
44
|
+
*/
|
|
45
|
+
export function trimPath(binding, path, size, start, end, offset = 0) {
|
|
46
|
+
return JSON.parse(binding.trimPath(JSON.stringify(path), size.width, size.height, start, end, offset));
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* `path` with `vertexCounts[i]` vertices on subpath `i`, evenly along its length, so two paths can
|
|
50
|
+
* be given the same structure and their keyframes can morph. The engine never resamples on its
|
|
51
|
+
* own. A subpath whose count is missing, below 2 (3 when closed), or that has no length is kept as
|
|
52
|
+
* it is.
|
|
53
|
+
*
|
|
54
|
+
* @throws When a count is over `MAX_PATH_VERTICES`.
|
|
55
|
+
*/
|
|
56
|
+
export function resamplePath(binding, path, size, vertexCounts) {
|
|
57
|
+
return JSON.parse(binding.resamplePath(JSON.stringify(path), size.width, size.height, Uint32Array.from(vertexCounts)));
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* A rectangle, ellipse, or polystar at `time` as a path of the same outline, from the same start
|
|
61
|
+
* point and in the same direction: "convert to path".
|
|
62
|
+
*
|
|
63
|
+
* @throws When `geometry` is not valid.
|
|
64
|
+
*/
|
|
65
|
+
export function toPath(binding, geometry, time, size) {
|
|
66
|
+
return JSON.parse(binding.toPath(JSON.stringify(geometry), time, size.width, size.height));
|
|
67
|
+
}
|
|
68
|
+
/** The polyline a position travels between its keyframes, along their spatial tangents. */
|
|
69
|
+
export function motionPath(binding, position, size) {
|
|
70
|
+
return JSON.parse(binding.motionPath(JSON.stringify(position), size.width, size.height));
|
|
71
|
+
}
|
package/dist/hdr-frames.d.ts
CHANGED
|
@@ -2,6 +2,13 @@ import type { ChromaSiting } from "./chroma-siting.js";
|
|
|
2
2
|
import type { FrameUploadReceipt } from "./effects.js";
|
|
3
3
|
import type { ColorMetadata } from "./generated/project-schema.js";
|
|
4
4
|
export { type ChromaSiting, chromaSitingForCodec, chromaSitingForStream, type SampleAspect, sampleAspectForStream, } from "./chroma-siting.js";
|
|
5
|
+
/** What a planar copy carries of its frame's identity beyond the frame itself. */
|
|
6
|
+
export interface PlanarFrameIdentity {
|
|
7
|
+
readonly physicalTimeMicroseconds?: number;
|
|
8
|
+
readonly rotation?: 0 | 90 | 180 | 270;
|
|
9
|
+
readonly mirror?: boolean;
|
|
10
|
+
readonly chromaSiting?: ChromaSiting;
|
|
11
|
+
}
|
|
5
12
|
/** A decoded frame copied out as planar YUV, with the color it carries. */
|
|
6
13
|
export interface PlanarYuvFrame {
|
|
7
14
|
readonly kind: "yuv-frame";
|
|
@@ -103,12 +110,15 @@ export declare class PlanarCopyPool {
|
|
|
103
110
|
* With `pool`, the copy's `data` is a buffer from it, which the caller gives back with
|
|
104
111
|
* {@link PlanarCopyPool.release} after uploading; a failed copy gives it back itself.
|
|
105
112
|
*/
|
|
106
|
-
export declare function copyPlanarYuvFrame(frame: VideoFrame, declared: ColorMetadata | null, identity?:
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
113
|
+
export declare function copyPlanarYuvFrame(frame: VideoFrame, declared: ColorMetadata | null, identity?: PlanarFrameIdentity, pool?: PlanarCopyPool): Promise<PlanarYuvFrame>;
|
|
114
|
+
/**
|
|
115
|
+
* {@link copyPlanarYuvFrame} without closing `frame`: the caller owns it whether the copy succeeds
|
|
116
|
+
* or not, so a frame whose planes the browser could not read back can be read again. Rejects as
|
|
117
|
+
* {@link copyPlanarYuvFrame} does; with `pool`, a failed copy gives its buffer back.
|
|
118
|
+
*
|
|
119
|
+
* @internal For `FrameSupply`.
|
|
120
|
+
*/
|
|
121
|
+
export declare function readPlanarYuvFrame(frame: VideoFrame, declared: ColorMetadata | null, identity?: PlanarFrameIdentity, pool?: PlanarCopyPool): Promise<PlanarYuvFrame>;
|
|
112
122
|
/**
|
|
113
123
|
* Uploads a planar frame for the engine to convert and present at
|
|
114
124
|
* `outputWidth` x `outputHeight`, the size it is shown upright at. Throws what
|
package/dist/hdr-frames.js
CHANGED
|
@@ -146,50 +146,58 @@ export class PlanarCopyPool {
|
|
|
146
146
|
*/
|
|
147
147
|
export async function copyPlanarYuvFrame(frame, declared, identity = {}, pool) {
|
|
148
148
|
try {
|
|
149
|
-
|
|
150
|
-
if (format === null || !isPlanarVideoFrame(frame)) {
|
|
151
|
-
throw new Error(`frame has no planar YUV format the engine reads (${format ?? "opaque"}); decode HDR sources, and sources whose color the project declares, in software`);
|
|
152
|
-
}
|
|
153
|
-
const visible = frame.visibleRect;
|
|
154
|
-
if (!visible)
|
|
155
|
-
throw new Error("frame has no visible rectangle");
|
|
156
|
-
// copyTo copies the visible rectangle by default, so the copy is exactly the picture.
|
|
157
|
-
const size = frame.allocationSize();
|
|
158
|
-
const data = pool?.acquire(size) ?? new Uint8Array(size);
|
|
159
|
-
let planes;
|
|
160
|
-
try {
|
|
161
|
-
planes = await frame.copyTo(data);
|
|
162
|
-
}
|
|
163
|
-
catch (error) {
|
|
164
|
-
pool?.release(data);
|
|
165
|
-
throw error;
|
|
166
|
-
}
|
|
167
|
-
return {
|
|
168
|
-
kind: "yuv-frame",
|
|
169
|
-
format,
|
|
170
|
-
codedWidth: visible.width,
|
|
171
|
-
codedHeight: visible.height,
|
|
172
|
-
data,
|
|
173
|
-
layout: planes.flatMap((plane) => [plane.offset, plane.stride]),
|
|
174
|
-
visibleRect: [0, 0, visible.width, visible.height],
|
|
175
|
-
color: declared ?? colorMetadataFromVideoColorSpace(frame.colorSpace),
|
|
176
|
-
chromaSiting: identity.chromaSiting ?? "unspecified",
|
|
177
|
-
displayWidth: frame.displayWidth,
|
|
178
|
-
displayHeight: frame.displayHeight,
|
|
179
|
-
physicalTimeMicroseconds: identity.physicalTimeMicroseconds ?? frame.timestamp,
|
|
180
|
-
...(frame.duration === null
|
|
181
|
-
? {}
|
|
182
|
-
: { durationMicroseconds: frame.duration }),
|
|
183
|
-
...(identity.rotation === undefined
|
|
184
|
-
? {}
|
|
185
|
-
: { rotation: identity.rotation }),
|
|
186
|
-
...(identity.mirror === undefined ? {} : { mirror: identity.mirror }),
|
|
187
|
-
};
|
|
149
|
+
return await readPlanarYuvFrame(frame, declared, identity, pool);
|
|
188
150
|
}
|
|
189
151
|
finally {
|
|
190
152
|
frame.close();
|
|
191
153
|
}
|
|
192
154
|
}
|
|
155
|
+
/**
|
|
156
|
+
* {@link copyPlanarYuvFrame} without closing `frame`: the caller owns it whether the copy succeeds
|
|
157
|
+
* or not, so a frame whose planes the browser could not read back can be read again. Rejects as
|
|
158
|
+
* {@link copyPlanarYuvFrame} does; with `pool`, a failed copy gives its buffer back.
|
|
159
|
+
*
|
|
160
|
+
* @internal For `FrameSupply`.
|
|
161
|
+
*/
|
|
162
|
+
export async function readPlanarYuvFrame(frame, declared, identity = {}, pool) {
|
|
163
|
+
const format = frame.format;
|
|
164
|
+
if (format === null || !isPlanarVideoFrame(frame)) {
|
|
165
|
+
throw new Error(`frame has no planar YUV format the engine reads (${format ?? "opaque"}); decode HDR sources, and sources whose color the project declares, in software`);
|
|
166
|
+
}
|
|
167
|
+
const visible = frame.visibleRect;
|
|
168
|
+
if (!visible)
|
|
169
|
+
throw new Error("frame has no visible rectangle");
|
|
170
|
+
// copyTo copies the visible rectangle by default, so the copy is exactly the picture.
|
|
171
|
+
const size = frame.allocationSize();
|
|
172
|
+
const data = pool?.acquire(size) ?? new Uint8Array(size);
|
|
173
|
+
let planes;
|
|
174
|
+
try {
|
|
175
|
+
planes = await frame.copyTo(data);
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
pool?.release(data);
|
|
179
|
+
throw error;
|
|
180
|
+
}
|
|
181
|
+
return {
|
|
182
|
+
kind: "yuv-frame",
|
|
183
|
+
format,
|
|
184
|
+
codedWidth: visible.width,
|
|
185
|
+
codedHeight: visible.height,
|
|
186
|
+
data,
|
|
187
|
+
layout: planes.flatMap((plane) => [plane.offset, plane.stride]),
|
|
188
|
+
visibleRect: [0, 0, visible.width, visible.height],
|
|
189
|
+
color: declared ?? colorMetadataFromVideoColorSpace(frame.colorSpace),
|
|
190
|
+
chromaSiting: identity.chromaSiting ?? "unspecified",
|
|
191
|
+
displayWidth: frame.displayWidth,
|
|
192
|
+
displayHeight: frame.displayHeight,
|
|
193
|
+
physicalTimeMicroseconds: identity.physicalTimeMicroseconds ?? frame.timestamp,
|
|
194
|
+
...(frame.duration === null
|
|
195
|
+
? {}
|
|
196
|
+
: { durationMicroseconds: frame.duration }),
|
|
197
|
+
...(identity.rotation === undefined ? {} : { rotation: identity.rotation }),
|
|
198
|
+
...(identity.mirror === undefined ? {} : { mirror: identity.mirror }),
|
|
199
|
+
};
|
|
200
|
+
}
|
|
193
201
|
/**
|
|
194
202
|
* Uploads a planar frame for the engine to convert and present at
|
|
195
203
|
* `outputWidth` x `outputHeight`, the size it is shown upright at. Throws what
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export * from "./audio-worklet.js";
|
|
|
4
4
|
export * from "./audio.js";
|
|
5
5
|
export * from "./audio-timeline.js";
|
|
6
6
|
export * from "./audio-worker.js";
|
|
7
|
+
export * from "./containers.js";
|
|
7
8
|
export * from "./effects.js";
|
|
8
9
|
export * from "./exporter.js";
|
|
9
10
|
export * from "./extensions.js";
|
package/dist/index.js
CHANGED
|
@@ -4,6 +4,7 @@ export * from "./audio-worklet.js";
|
|
|
4
4
|
export * from "./audio.js";
|
|
5
5
|
export * from "./audio-timeline.js";
|
|
6
6
|
export * from "./audio-worker.js";
|
|
7
|
+
export * from "./containers.js";
|
|
7
8
|
export * from "./effects.js";
|
|
8
9
|
export * from "./exporter.js";
|
|
9
10
|
export * from "./extensions.js";
|
package/dist/mediabunny.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type { AudioDecoderRefillRequest, AudioSpeaker, BrowserDecodedFrame, Brow
|
|
|
3
3
|
import type { BrowserAudioBlockSink } from "./audio.js";
|
|
4
4
|
import type { BrowserEncodeSink, ExportedFrame } from "./exporter.js";
|
|
5
5
|
import type { VideoRateControl } from "./rate-control.js";
|
|
6
|
+
import { type ExportContainer } from "./containers.js";
|
|
6
7
|
import type { PositionedWrite } from "./export-destination.js";
|
|
7
8
|
/** Inputs supported by the optional pinned mediabunny adapter. */
|
|
8
9
|
export type MediabunnyInput = Blob | ArrayBuffer | ArrayBufferView;
|
|
@@ -39,7 +40,9 @@ export declare class MediabunnyVideoDecoder implements BrowserVideoDecoder {
|
|
|
39
40
|
private constructor();
|
|
40
41
|
/**
|
|
41
42
|
* Opens `resource`'s primary video track. Rejects when it has none, or when this browser cannot
|
|
42
|
-
* decode it, so a host can fall back before the first request.
|
|
43
|
+
* decode it, so a host can fall back before the first request. With `hardwareAcceleration:
|
|
44
|
+
* "prefer-software"`, a track the browser has no software decoder for rejects with `code:
|
|
45
|
+
* "software_decode_unavailable"`.
|
|
43
46
|
*/
|
|
44
47
|
static open(sourceId: string, resource: MediabunnyInput, options?: MediabunnyVideoDecoderOptions): Promise<MediabunnyVideoDecoder>;
|
|
45
48
|
/** Bytes both windows hold: decoder frames and reverse copies. */
|
|
@@ -143,9 +146,14 @@ export declare class MediabunnyAudioDecoder implements BrowserAudioDecoder {
|
|
|
143
146
|
close(): void;
|
|
144
147
|
}
|
|
145
148
|
/**
|
|
146
|
-
* Options for {@link
|
|
149
|
+
* Options for {@link MediabunnySink}. At least one of `videoCodec` and `audioCodec` is required.
|
|
147
150
|
*/
|
|
148
|
-
export interface
|
|
151
|
+
export interface MediabunnySinkOptions {
|
|
152
|
+
/**
|
|
153
|
+
* The file format. The codecs must be ones it offers (see `alpha2/containers`): MOV takes no VP9,
|
|
154
|
+
* AV1 or Opus, and WebM takes only VP9, AV1 and Opus.
|
|
155
|
+
*/
|
|
156
|
+
readonly container: ExportContainer;
|
|
149
157
|
/** Adds a video track in this codec; requires `videoRateControl`. */
|
|
150
158
|
readonly videoCodec?: "avc" | "hevc" | "vp9" | "av1";
|
|
151
159
|
/**
|
|
@@ -193,19 +201,20 @@ export interface MediabunnyMp4SinkOptions {
|
|
|
193
201
|
*/
|
|
194
202
|
readonly audioResampler?: AudioResamplerBinding;
|
|
195
203
|
/**
|
|
196
|
-
* Where the file goes. `stream` writes a
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
* stream is closed (committed) only once
|
|
200
|
-
* and aborted (discarded) when the export
|
|
201
|
-
*
|
|
202
|
-
* caller sizes (and budgets for, see
|
|
203
|
-
* fails with `RESOURCE_LIMIT`
|
|
204
|
+
* Where the file goes. `stream` writes through a seekable positioned stream as it is muxed (see
|
|
205
|
+
* `WritableFileDestination` in `alpha2/export-destination`): MP4 and MOV put `moov` at the end,
|
|
206
|
+
* without fast start, and MKV and WebM patch sizes and Cues in place, so no part of the encoded
|
|
207
|
+
* output accumulates in memory; the stream is closed (committed) only once
|
|
208
|
+
* {@link MediabunnySink.finish} has finished the file, and aborted (discarded) when the export
|
|
209
|
+
* fails or is cancelled. `memory` keeps it in an `ArrayBuffer`, for small outputs only:
|
|
210
|
+
* `maxBytes` is the most the output may reach, which the caller sizes (and budgets for, see
|
|
211
|
+
* `memoryOutputPeakBytes`) before starting; past it the export fails with `RESOURCE_LIMIT`
|
|
212
|
+
* instead of growing.
|
|
204
213
|
*/
|
|
205
|
-
readonly destination:
|
|
214
|
+
readonly destination: ExportDestination;
|
|
206
215
|
}
|
|
207
|
-
/** Where a {@link
|
|
208
|
-
export type
|
|
216
|
+
/** Where a {@link MediabunnySink} writes. */
|
|
217
|
+
export type ExportDestination = {
|
|
209
218
|
readonly kind: "stream";
|
|
210
219
|
readonly writable: WritableStream<PositionedWrite>;
|
|
211
220
|
} | {
|
|
@@ -214,12 +223,12 @@ export type Mp4Destination = {
|
|
|
214
223
|
};
|
|
215
224
|
/**
|
|
216
225
|
* The mediabunny quality a video track encodes at for the engine's rate control: what
|
|
217
|
-
* {@link
|
|
226
|
+
* {@link MediabunnySink} configures its encoder with.
|
|
218
227
|
*/
|
|
219
228
|
export declare function videoEncoderQuality(rate: VideoRateControl): Quality;
|
|
220
229
|
/**
|
|
221
230
|
* Whether this browser can encode `codec` video at `width`x`height` with `rate`, asked with the
|
|
222
|
-
* configurations {@link
|
|
231
|
+
* configurations {@link MediabunnySink} would try. WebCodecs leaves the codecs to the browser
|
|
223
232
|
* (Chrome on Linux encodes no HEVC, for one), so an export asks before it opens its encoder.
|
|
224
233
|
*/
|
|
225
234
|
export declare function canEncodeVideoTrack(codec: "avc" | "hevc" | "vp9" | "av1", options: {
|
|
@@ -230,7 +239,7 @@ export declare function canEncodeVideoTrack(codec: "avc" | "hevc" | "vp9" | "av1
|
|
|
230
239
|
/** How a browser's video encoder is opened: see {@link videoEncoderLatencyMode}. */
|
|
231
240
|
export type VideoEncoderLatencyMode = "quality" | "realtime";
|
|
232
241
|
/**
|
|
233
|
-
* The latency mode {@link
|
|
242
|
+
* The latency mode {@link MediabunnySink} opens `codec`'s encoder with: `"realtime"` only where a
|
|
234
243
|
* quality-mode encoder puts out nothing (Safari 27's H.264) and a realtime one works. Measured once
|
|
235
244
|
* per codec.
|
|
236
245
|
*/
|
|
@@ -239,7 +248,7 @@ export declare function videoEncoderLatencyMode(codec: "avc" | "hevc" | "vp9" |
|
|
|
239
248
|
* The rate this browser encodes `codec` audio of a project at `sampleRate` with, by the rule a
|
|
240
249
|
* native host follows: the project rate when the browser's encoder takes it, else the lowest rate
|
|
241
250
|
* the codec carries above it that the encoder takes, else the highest below it; `null` when the
|
|
242
|
-
* encoder takes none. Pass it to {@link
|
|
251
|
+
* encoder takes none. Pass it to {@link MediabunnySink} as `audioSampleRate`, so every project
|
|
243
252
|
* rate exports where the browser encodes the codec at all.
|
|
244
253
|
*/
|
|
245
254
|
export declare function audioEncoderSampleRate(codec: "aac" | "opus", options: {
|
|
@@ -247,8 +256,8 @@ export declare function audioEncoderSampleRate(codec: "aac" | "opus", options: {
|
|
|
247
256
|
readonly numberOfChannels: number;
|
|
248
257
|
readonly bitrate: number;
|
|
249
258
|
}): Promise<number | null>;
|
|
250
|
-
/** What a finished {@link
|
|
251
|
-
export type
|
|
259
|
+
/** What a finished {@link MediabunnySink} produced. */
|
|
260
|
+
export type ExportOutput = {
|
|
252
261
|
readonly kind: "stream";
|
|
253
262
|
readonly bytes: number;
|
|
254
263
|
} | {
|
|
@@ -268,18 +277,18 @@ export interface AppliedVideoRateControl {
|
|
|
268
277
|
* The sink is the file: its `finish` finalizes every track, which suits one runner, or a host that
|
|
269
278
|
* writes both tracks itself and finishes once. When two runners share the file (`exportToSink` for
|
|
270
279
|
* the video and `renderOfflineAudio` for the audio, at once or one after the other), give each its
|
|
271
|
-
* track instead, {@link
|
|
280
|
+
* track instead, {@link MediabunnySink.videoTrack} and {@link MediabunnySink.audioTrack}: a
|
|
272
281
|
* runner's `finish` then ends its own track, and the file is finalized when every configured track
|
|
273
282
|
* has ended. Either track's `abort` discards the whole file, and the other runner's next write
|
|
274
283
|
* fails.
|
|
275
284
|
*
|
|
276
|
-
* The constructor throws `RangeError` when no track is configured,
|
|
277
|
-
*
|
|
278
|
-
* range.
|
|
285
|
+
* The constructor throws `RangeError` when no track is configured, the container does not offer a
|
|
286
|
+
* codec (an `ExportFormatError`), a codec lacks its rate, a memory destination's `maxBytes` is not
|
|
287
|
+
* a positive safe integer, or `stallTimeoutMs` is out of range.
|
|
279
288
|
*/
|
|
280
|
-
export declare class
|
|
289
|
+
export declare class MediabunnySink implements BrowserEncodeSink, BrowserAudioBlockSink {
|
|
281
290
|
#private;
|
|
282
|
-
constructor(options:
|
|
291
|
+
constructor(options: MediabunnySinkOptions);
|
|
283
292
|
/** The latency mode the video encoder was opened with; undefined until the sink has started. */
|
|
284
293
|
get videoLatencyMode(): VideoEncoderLatencyMode | undefined;
|
|
285
294
|
/**
|
|
@@ -332,7 +341,7 @@ export declare class MediabunnyMp4Sink implements BrowserEncodeSink, BrowserAudi
|
|
|
332
341
|
* The finished output: its length for a stream, the bytes in memory. Null until {@link finish}
|
|
333
342
|
* resolves.
|
|
334
343
|
*/
|
|
335
|
-
get output():
|
|
344
|
+
get output(): ExportOutput | null;
|
|
336
345
|
/** Bytes muxed so far: the file's length as written. */
|
|
337
346
|
get bytesWritten(): number;
|
|
338
347
|
}
|
package/dist/mediabunny.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { readAudioSpeakers } from "./audio-layout.js";
|
|
2
2
|
import { chromaSitingForStream } from "./chroma-siting.js";
|
|
3
3
|
import { decoderOrderKey, decoderPlaneOrder } from "./decoder-order.js";
|
|
4
|
-
import { ALL_FORMATS, AudioSample, AudioSampleSink, AudioSampleSource, BlobSource, BufferTarget, BufferSource, canEncodeAudio, canEncodeVideo, StreamTarget, EncodedPacketSink, Input, Mp4OutputFormat, Output, Quality, VideoSample, VideoSampleSink, VideoSampleSource, } from "mediabunny";
|
|
4
|
+
import { ALL_FORMATS, AudioSample, AudioSampleSink, AudioSampleSource, BlobSource, BufferTarget, BufferSource, canEncodeAudio, canEncodeVideo, StreamTarget, EncodedPacketSink, Input, MkvOutputFormat, MovOutputFormat, Mp4OutputFormat, Output, Quality, VideoSample, VideoSampleSink, VideoSampleSource, WebMOutputFormat, } from "mediabunny";
|
|
5
5
|
import { audioFrameGrid, MAX_AUDIO_CHANNELS, microsecondsToTicks, placeOnAudioFrameGrid, presentationSlackTicks, readMatroskaCodecDelay, readVideoOrientation, roundingQuantum, ROUNDING_SCAN_BYTES, ROUNDING_SCAN_FRAMES, validateStallTimeout, } from "./media.js";
|
|
6
|
+
import { resolveExportFormat } from "./containers.js";
|
|
6
7
|
import { ExportFailureError } from "./export-failure.js";
|
|
7
8
|
/**
|
|
8
9
|
* How far past the decode cursor a request may be (2 s) and still be reached by decoding forward;
|
|
@@ -112,7 +113,9 @@ export class MediabunnyVideoDecoder {
|
|
|
112
113
|
}
|
|
113
114
|
/**
|
|
114
115
|
* Opens `resource`'s primary video track. Rejects when it has none, or when this browser cannot
|
|
115
|
-
* decode it, so a host can fall back before the first request.
|
|
116
|
+
* decode it, so a host can fall back before the first request. With `hardwareAcceleration:
|
|
117
|
+
* "prefer-software"`, a track the browser has no software decoder for rejects with `code:
|
|
118
|
+
* "software_decode_unavailable"`.
|
|
116
119
|
*/
|
|
117
120
|
static async open(sourceId, resource, options = {}) {
|
|
118
121
|
const limits = {
|
|
@@ -138,6 +141,20 @@ export class MediabunnyVideoDecoder {
|
|
|
138
141
|
throw new Error("mediabunny input has no video track");
|
|
139
142
|
if (!(await track.canDecode()))
|
|
140
143
|
throw new Error("this browser cannot decode the video track");
|
|
144
|
+
// `canDecode` does not ask for a software decoder: a codec only hardware decodes would open
|
|
145
|
+
// and fail on its first read. Without WebCodecs only mediabunny's own decoders decode.
|
|
146
|
+
if (options.hardwareAcceleration === "prefer-software" &&
|
|
147
|
+
typeof VideoDecoder !== "undefined") {
|
|
148
|
+
const config = await track.getDecoderConfig();
|
|
149
|
+
const support = config === null
|
|
150
|
+
? { supported: false }
|
|
151
|
+
: await VideoDecoder.isConfigSupported({
|
|
152
|
+
...config,
|
|
153
|
+
hardwareAcceleration: "prefer-software",
|
|
154
|
+
});
|
|
155
|
+
if (support.supported !== true)
|
|
156
|
+
throw Object.assign(new Error(`this browser has no software decoder for ${config?.codec ?? track.codec ?? "the video track"}`), { code: "software_decode_unavailable" });
|
|
157
|
+
}
|
|
141
158
|
// mediabunny reports a turn but not a mirror, and reads a mirroring matrix's angle otherwise
|
|
142
159
|
// than native decoding does.
|
|
143
160
|
const orientation = (await readVideoOrientation(resource, track.id)) ?? {
|
|
@@ -1084,7 +1101,7 @@ export class MediabunnyAudioDecoder {
|
|
|
1084
1101
|
}
|
|
1085
1102
|
/**
|
|
1086
1103
|
* The mediabunny quality a video track encodes at for the engine's rate control: what
|
|
1087
|
-
* {@link
|
|
1104
|
+
* {@link MediabunnySink} configures its encoder with.
|
|
1088
1105
|
*/
|
|
1089
1106
|
export function videoEncoderQuality(rate) {
|
|
1090
1107
|
return new Quality({
|
|
@@ -1095,7 +1112,7 @@ export function videoEncoderQuality(rate) {
|
|
|
1095
1112
|
}
|
|
1096
1113
|
/**
|
|
1097
1114
|
* Whether this browser can encode `codec` video at `width`x`height` with `rate`, asked with the
|
|
1098
|
-
* configurations {@link
|
|
1115
|
+
* configurations {@link MediabunnySink} would try. WebCodecs leaves the codecs to the browser
|
|
1099
1116
|
* (Chrome on Linux encodes no HEVC, for one), so an export asks before it opens its encoder.
|
|
1100
1117
|
*/
|
|
1101
1118
|
export async function canEncodeVideoTrack(codec, options) {
|
|
@@ -1105,7 +1122,10 @@ export async function canEncodeVideoTrack(codec, options) {
|
|
|
1105
1122
|
quality: videoEncoderQuality(options.rate),
|
|
1106
1123
|
}).catch(() => false);
|
|
1107
1124
|
}
|
|
1108
|
-
/**
|
|
1125
|
+
/**
|
|
1126
|
+
* The profiles mediabunny opens (H.264 High, HEVC Main, VP9 profile 0, AV1 Main), at a level for
|
|
1127
|
+
* the probe's frames.
|
|
1128
|
+
*/
|
|
1109
1129
|
const LATENCY_PROBE_CODECS = {
|
|
1110
1130
|
avc: "avc1.64001e",
|
|
1111
1131
|
hevc: "hev1.1.6.L93.B0",
|
|
@@ -1120,7 +1140,7 @@ const LATENCY_PROBE_FRAMES = 6;
|
|
|
1120
1140
|
const LATENCY_PROBE_TIMEOUT_MS = 1_500;
|
|
1121
1141
|
const latencyModes = new Map();
|
|
1122
1142
|
/**
|
|
1123
|
-
* The latency mode {@link
|
|
1143
|
+
* The latency mode {@link MediabunnySink} opens `codec`'s encoder with: `"realtime"` only where a
|
|
1124
1144
|
* quality-mode encoder puts out nothing (Safari 27's H.264) and a realtime one works. Measured once
|
|
1125
1145
|
* per codec.
|
|
1126
1146
|
*/
|
|
@@ -1218,7 +1238,7 @@ async function encoderPutsOut(codec, latencyMode) {
|
|
|
1218
1238
|
}
|
|
1219
1239
|
}
|
|
1220
1240
|
/**
|
|
1221
|
-
* The sample rates each audio codec a {@link
|
|
1241
|
+
* The sample rates each audio codec a {@link MediabunnySink} writes can carry, lowest first:
|
|
1222
1242
|
* AAC's MPEG-4 rates up to the project limit, and Opus's.
|
|
1223
1243
|
*/
|
|
1224
1244
|
const ENCODER_SAMPLE_RATES = {
|
|
@@ -1232,7 +1252,7 @@ const ENCODER_SAMPLE_RATES = {
|
|
|
1232
1252
|
* The rate this browser encodes `codec` audio of a project at `sampleRate` with, by the rule a
|
|
1233
1253
|
* native host follows: the project rate when the browser's encoder takes it, else the lowest rate
|
|
1234
1254
|
* the codec carries above it that the encoder takes, else the highest below it; `null` when the
|
|
1235
|
-
* encoder takes none. Pass it to {@link
|
|
1255
|
+
* encoder takes none. Pass it to {@link MediabunnySink} as `audioSampleRate`, so every project
|
|
1236
1256
|
* rate exports where the browser encodes the codec at all.
|
|
1237
1257
|
*/
|
|
1238
1258
|
export async function audioEncoderSampleRate(codec, options) {
|
|
@@ -1337,22 +1357,39 @@ class EncoderRateConverter {
|
|
|
1337
1357
|
return { startSample: start, channels };
|
|
1338
1358
|
}
|
|
1339
1359
|
}
|
|
1360
|
+
/**
|
|
1361
|
+
* The muxer for `container`. MP4 and MOV go without fast start and MKV and WebM are not
|
|
1362
|
+
* append-only, so the payload is never held in memory or rewritten: the muxer seeks back only to
|
|
1363
|
+
* patch headers.
|
|
1364
|
+
*/
|
|
1365
|
+
function outputFormat(container) {
|
|
1366
|
+
switch (container) {
|
|
1367
|
+
case "mp4":
|
|
1368
|
+
return new Mp4OutputFormat({ fastStart: false });
|
|
1369
|
+
case "mov":
|
|
1370
|
+
return new MovOutputFormat({ fastStart: false });
|
|
1371
|
+
case "mkv":
|
|
1372
|
+
return new MkvOutputFormat({ appendOnly: false });
|
|
1373
|
+
case "webm":
|
|
1374
|
+
return new WebMOutputFormat({ appendOnly: false });
|
|
1375
|
+
}
|
|
1376
|
+
}
|
|
1340
1377
|
/**
|
|
1341
1378
|
* Optional mediabunny encoder/muxer implementing both public alpha2 sinks.
|
|
1342
1379
|
*
|
|
1343
1380
|
* The sink is the file: its `finish` finalizes every track, which suits one runner, or a host that
|
|
1344
1381
|
* writes both tracks itself and finishes once. When two runners share the file (`exportToSink` for
|
|
1345
1382
|
* the video and `renderOfflineAudio` for the audio, at once or one after the other), give each its
|
|
1346
|
-
* track instead, {@link
|
|
1383
|
+
* track instead, {@link MediabunnySink.videoTrack} and {@link MediabunnySink.audioTrack}: a
|
|
1347
1384
|
* runner's `finish` then ends its own track, and the file is finalized when every configured track
|
|
1348
1385
|
* has ended. Either track's `abort` discards the whole file, and the other runner's next write
|
|
1349
1386
|
* fails.
|
|
1350
1387
|
*
|
|
1351
|
-
* The constructor throws `RangeError` when no track is configured,
|
|
1352
|
-
*
|
|
1353
|
-
* range.
|
|
1388
|
+
* The constructor throws `RangeError` when no track is configured, the container does not offer a
|
|
1389
|
+
* codec (an `ExportFormatError`), a codec lacks its rate, a memory destination's `maxBytes` is not
|
|
1390
|
+
* a positive safe integer, or `stallTimeoutMs` is out of range.
|
|
1354
1391
|
*/
|
|
1355
|
-
export class
|
|
1392
|
+
export class MediabunnySink {
|
|
1356
1393
|
#target;
|
|
1357
1394
|
/**
|
|
1358
1395
|
* A stream destination, written through the muxer and committed or discarded by this sink alone:
|
|
@@ -1385,6 +1422,17 @@ export class MediabunnyMp4Sink {
|
|
|
1385
1422
|
#videoTrack;
|
|
1386
1423
|
#audioTrack;
|
|
1387
1424
|
constructor(options) {
|
|
1425
|
+
// Before anything is opened or locked: a pair the container does not offer is the caller's
|
|
1426
|
+
// mistake, not a failed export.
|
|
1427
|
+
resolveExportFormat({
|
|
1428
|
+
container: options.container,
|
|
1429
|
+
...(options.videoCodec === undefined
|
|
1430
|
+
? {}
|
|
1431
|
+
: { videoCodec: options.videoCodec }),
|
|
1432
|
+
...(options.audioCodec === undefined
|
|
1433
|
+
? {}
|
|
1434
|
+
: { audioCodec: options.audioCodec }),
|
|
1435
|
+
});
|
|
1388
1436
|
const destination = options.destination;
|
|
1389
1437
|
if (destination.kind === "memory") {
|
|
1390
1438
|
if (!Number.isSafeInteger(destination.maxBytes) ||
|
|
@@ -1428,8 +1476,7 @@ export class MediabunnyMp4Sink {
|
|
|
1428
1476
|
};
|
|
1429
1477
|
}
|
|
1430
1478
|
this.#output = new Output({
|
|
1431
|
-
|
|
1432
|
-
format: new Mp4OutputFormat({ fastStart: false }),
|
|
1479
|
+
format: outputFormat(options.container),
|
|
1433
1480
|
target: this.#target,
|
|
1434
1481
|
});
|
|
1435
1482
|
this.#hasVideo = options.videoCodec !== undefined;
|
package/dist/player.d.ts
CHANGED
|
@@ -48,6 +48,11 @@ export interface PresentedFrame {
|
|
|
48
48
|
readonly missingFrames: readonly MissingFrame[];
|
|
49
49
|
/** Whether the previous picture was shown again instead of a new one. */
|
|
50
50
|
readonly repeated: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Whether the frame equals the one on screen, so nothing was rendered or presented: the canvas
|
|
53
|
+
* still shows it.
|
|
54
|
+
*/
|
|
55
|
+
readonly unchanged: boolean;
|
|
51
56
|
}
|
|
52
57
|
/** The worker-side renderer an {@link Alpha2Player} drives. */
|
|
53
58
|
export interface RenderBackend {
|