alpha2 0.4.6 → 0.5.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.
Files changed (123) hide show
  1. package/README.md +5 -4
  2. package/THIRD_PARTY_NOTICES.md +4 -0
  3. package/dist/abort.d.ts +12 -0
  4. package/dist/abort.js +48 -0
  5. package/dist/active-clock.d.ts +35 -0
  6. package/dist/active-clock.js +99 -0
  7. package/dist/assets.d.ts +51 -22
  8. package/dist/assets.js +8 -12
  9. package/dist/audio-layout.d.ts +52 -74
  10. package/dist/audio-layout.js +114 -148
  11. package/dist/audio-timeline.d.ts +89 -117
  12. package/dist/audio-timeline.js +102 -118
  13. package/dist/audio-worker.d.ts +12 -21
  14. package/dist/audio-worker.js +8 -13
  15. package/dist/audio-worklet-processor.js +45 -58
  16. package/dist/audio-worklet.d.ts +45 -76
  17. package/dist/audio-worklet.js +44 -79
  18. package/dist/audio.d.ts +15 -23
  19. package/dist/audio.js +9 -13
  20. package/dist/builder.d.ts +45 -63
  21. package/dist/builder.js +49 -45
  22. package/dist/capabilities.d.ts +99 -16
  23. package/dist/capabilities.js +2 -2
  24. package/dist/captions.d.ts +45 -22
  25. package/dist/captions.js +60 -15
  26. package/dist/chroma-siting.d.ts +24 -41
  27. package/dist/chroma-siting.js +38 -71
  28. package/dist/containers.d.ts +41 -30
  29. package/dist/containers.js +59 -27
  30. package/dist/decoder-fallback.d.ts +13 -0
  31. package/dist/decoder-fallback.js +38 -0
  32. package/dist/decoder-order.d.ts +24 -39
  33. package/dist/decoder-order.js +28 -42
  34. package/dist/edit.d.ts +77 -154
  35. package/dist/edit.js +98 -180
  36. package/dist/effects.d.ts +125 -176
  37. package/dist/effects.js +7 -9
  38. package/dist/export-destination.d.ts +25 -41
  39. package/dist/export-destination.js +20 -32
  40. package/dist/export-failure.d.ts +34 -59
  41. package/dist/export-failure.js +54 -48
  42. package/dist/export-metrics.d.ts +116 -0
  43. package/dist/export-metrics.js +74 -0
  44. package/dist/export-retry.d.ts +46 -29
  45. package/dist/export-retry.js +86 -51
  46. package/dist/exporter.d.ts +67 -116
  47. package/dist/exporter.js +133 -133
  48. package/dist/extensions.d.ts +69 -115
  49. package/dist/extensions.js +55 -82
  50. package/dist/frame-intake.d.ts +92 -0
  51. package/dist/frame-intake.js +114 -0
  52. package/dist/frame-supply.d.ts +96 -162
  53. package/dist/frame-supply.js +181 -171
  54. package/dist/frames.d.ts +23 -0
  55. package/dist/frames.js +34 -0
  56. package/dist/generated/project-schema.d.ts +712 -485
  57. package/dist/generated/project-schema.js +55 -2
  58. package/dist/generated/project-shape.js +276 -16
  59. package/dist/geometry.d.ts +28 -34
  60. package/dist/geometry.js +18 -23
  61. package/dist/gpu-recovery.d.ts +92 -0
  62. package/dist/gpu-recovery.js +135 -0
  63. package/dist/hdr-frames.d.ts +82 -67
  64. package/dist/hdr-frames.js +112 -49
  65. package/dist/index.d.ts +6 -0
  66. package/dist/index.js +6 -0
  67. package/dist/json-text.d.ts +13 -23
  68. package/dist/json-text.js +10 -20
  69. package/dist/layout-values.d.ts +14 -22
  70. package/dist/layout-values.js +18 -21
  71. package/dist/layout.d.ts +48 -63
  72. package/dist/layout.js +37 -37
  73. package/dist/loudness.d.ts +27 -32
  74. package/dist/loudness.js +19 -23
  75. package/dist/media.d.ts +85 -237
  76. package/dist/media.js +133 -287
  77. package/dist/mediabunny.d.ts +178 -246
  78. package/dist/mediabunny.js +781 -583
  79. package/dist/paint.d.ts +23 -0
  80. package/dist/paint.js +37 -0
  81. package/dist/physical-frames.d.ts +5 -17
  82. package/dist/physical-frames.js +4 -11
  83. package/dist/player.d.ts +56 -47
  84. package/dist/player.js +155 -40
  85. package/dist/presented-frames.d.ts +2 -7
  86. package/dist/presented-frames.js +3 -8
  87. package/dist/project.d.ts +74 -118
  88. package/dist/project.js +384 -445
  89. package/dist/rate-control.d.ts +9 -23
  90. package/dist/rate-control.js +7 -18
  91. package/dist/renderer-choice.d.ts +41 -0
  92. package/dist/renderer-choice.js +82 -0
  93. package/dist/repacked-bytes.d.ts +0 -2
  94. package/dist/repacked-bytes.js +2 -5
  95. package/dist/robustness.d.ts +25 -37
  96. package/dist/robustness.js +71 -54
  97. package/dist/sha256.d.ts +5 -13
  98. package/dist/sha256.js +9 -24
  99. package/dist/text.d.ts +97 -71
  100. package/dist/text.js +33 -70
  101. package/dist/video-frames.d.ts +7 -25
  102. package/dist/video-frames.js +12 -29
  103. package/dist/waveform.d.ts +5 -11
  104. package/dist/waveform.js +4 -9
  105. package/dist/worker-error.d.ts +5 -20
  106. package/dist/worker-error.js +8 -25
  107. package/dist/worker.d.ts +82 -95
  108. package/dist/worker.js +235 -143
  109. package/package.json +26 -2
  110. package/wasm/alpha2_audio_worklet.wasm +0 -0
  111. package/wasm/alpha2_extensions.d.ts +26 -47
  112. package/wasm/alpha2_extensions.js +26 -47
  113. package/wasm/alpha2_extensions_bg.wasm +0 -0
  114. package/wasm/alpha2_wasm.d.ts +501 -587
  115. package/wasm/alpha2_wasm.js +661 -616
  116. package/wasm/alpha2_wasm_bg.wasm +0 -0
  117. package/wasm/alpha2_wasm_bg.wasm.d.ts +22 -12
  118. package/wasm/rust-crate-licenses.md +2 -2
  119. package/wasm/snippets/{alpha2-wasm-c62e60c964e88a52 → alpha2-wasm-070d94113471ef0a}/inline1.js +1 -0
  120. package/wasm/snippets/alpha2-wasm-070d94113471ef0a/inline2.js +82 -0
  121. package/wasm/snippets/alpha2-wasm-c62e60c964e88a52/inline2.js +0 -52
  122. /package/wasm/snippets/{alpha2-wasm-c62e60c964e88a52 → alpha2-wasm-070d94113471ef0a}/inline0.js +0 -0
  123. /package/wasm/snippets/{alpha2-wasm-c62e60c964e88a52 → alpha2-wasm-070d94113471ef0a}/inline3.js +0 -0
@@ -1,71 +1,54 @@
1
1
  /**
2
- * Where a subsampled frame's chroma samples sit relative to its luma.
3
- *
4
- * WebCodecs does not report it, so the demuxer, which knows the stream, supplies it. The
5
- * conversion must place chroma where the encoder did, or every chroma edge lands half a sample
6
- * off. `unspecified` is converted as centered.
2
+ * Position of subsampled chroma relative to luma. WebCodecs does not report it, so the demuxer
3
+ * supplies it; a wrong siting shifts chroma edges by half a sample. `unspecified` converts as
4
+ * centered.
7
5
  */
8
6
  export type ChromaSiting = "left" | "center" | "top-left" | "unspecified";
9
7
  /**
10
- * The chroma siting a codec's streams have unless they signal otherwise.
11
- *
12
- * H.264, HEVC, and MPEG-4 Part 2 default 4:2:0 chroma to the left (H.273
13
- * chroma sample location type 0), as FFmpeg reports them natively. Other
14
- * codecs stay unspecified, which the engine reads as centered, again as
15
- * natively.
8
+ * Default chroma siting for a codec. H.264, HEVC and MPEG-4 Part 2 default to `left` (H.273 type
9
+ * 0); other codecs are `unspecified`.
16
10
  */
17
11
  export declare function chromaSitingForCodec(codec: string | null | undefined): ChromaSiting;
18
12
  /**
19
- * The chroma siting a stream signals, as FFmpeg's decoder reports it on the
20
- * frames it decodes.
13
+ * Chroma siting a stream signals, matching the native host's reading of FFmpeg's decoder.
21
14
  *
22
- * H.264 and HEVC signal it in the sequence parameter set's VUI
23
- * (`chroma_sample_loc_type_top_field`), AV1 in the sequence header
24
- * (`chroma_sample_position`). They are read from the decoder configuration's
25
- * `description` (`avcC`, `hvcC`, `av1C`) or, when it has none (Annex B
26
- * streams, AV1 in MP4), from the stream's first packet. With neither, or for
27
- * another codec, the codec's default ({@link chromaSitingForCodec}).
15
+ * Read from the H.264/HEVC SPS VUI or the AV1 sequence header, found in the configuration's
16
+ * `description` (`avcC`, `hvcC`, `av1C`) or else in the first packet. Falls back to
17
+ * {@link chromaSitingForCodec}.
28
18
  *
29
- * The values follow the native decoder, so both hosts convert alike: FFmpeg's
30
- * reading, except that an H.264 stream with no VUI or a reserved type (above
31
- * 5) is `left`, as H.264 infers it without one (E.2.1) and the native host
32
- * reads what FFmpeg leaves unspecified. An H.264 or HEVC type from 3 to 5 is
33
- * `unspecified`, as are AV1's unknown position and HEVC that is not 4:2:0.
19
+ * An H.264 stream with no VUI or a reserved type (above 5) is `left`, as H.264 infers (E.2.1).
20
+ * H.264 or HEVC types 3 to 5, AV1's unknown position, and non-4:2:0 HEVC are `unspecified`.
34
21
  */
35
22
  export declare function chromaSitingForStream(codec: string | null | undefined, description?: AllowSharedBufferSource | null, firstPacket?: AllowSharedBufferSource | null): ChromaSiting;
36
- /** A sample (pixel) aspect ratio: a sample is `num / den` as wide as it is tall. */
23
+ /** Sample (pixel) aspect ratio: width over height of one sample. */
37
24
  export interface SampleAspect {
38
25
  readonly num: number;
39
26
  readonly den: number;
40
27
  }
41
28
  /**
42
- * The sample aspect ratio an H.264 or HEVC stream signals in its sequence parameter set's VUI
43
- * (`aspect_ratio_idc`, or `sar_width`/`sar_height` for Extended_SAR), read from the decoder
44
- * configuration's `description` (`avcC`, `hvcC`) or, without one, from the stream's first packet.
45
- * `undefined` when the stream signals none (no VUI, `aspect_ratio_idc` 0, a reserved index, or a
46
- * zero term), for another codec, or when the parameter set does not parse: as FFmpeg's decoder
47
- * reports it, which a native probe falls back to when the container declares no ratio
48
- * (`av_guess_sample_aspect_ratio`).
29
+ * Sample aspect ratio from an H.264 or HEVC SPS VUI, read from `description` or else the first
30
+ * packet. `undefined` when none is signalled, for other codecs, or when the SPS does not parse,
31
+ * matching FFmpeg's decoder (which a native probe uses when the container declares no ratio).
49
32
  */
50
33
  export declare function sampleAspectForStream(codec: string | null | undefined, description?: AllowSharedBufferSource | null, firstPacket?: AllowSharedBufferSource | null): SampleAspect | undefined;
51
34
  /**
52
- * `vui.chroma_location` as FFmpeg's H.264 parser derives it from one SPS NAL unit, starting at
53
- * its one-byte NAL header. A location FFmpeg leaves unspecified is read as H.264 infers it: left.
35
+ * Chroma siting from one H.264 SPS NAL unit (including its one-byte header), as FFmpeg's parser
36
+ * derives `vui.chroma_location`. An unspecified location reads as `left`, as H.264 infers.
54
37
  *
55
- * Throws `RangeError` when the SPS ends early.
38
+ * @throws RangeError When the SPS ends early.
56
39
  */
57
40
  export declare function h264SpsSiting(nal: Uint8Array): ChromaSiting;
58
41
  /**
59
- * `avctx->chroma_sample_location` as FFmpeg's HEVC decoder sets it from one SPS NAL unit, starting
60
- * at its two-byte NAL header.
42
+ * Chroma siting from one HEVC SPS NAL unit (including its two-byte header), as FFmpeg's decoder
43
+ * sets `avctx->chroma_sample_location`.
61
44
  *
62
- * Throws `RangeError` when the SPS ends early.
45
+ * @throws RangeError When the SPS ends early.
63
46
  */
64
47
  export declare function hevcSpsSiting(nal: Uint8Array): ChromaSiting;
65
48
  /**
66
- * `avctx->chroma_sample_location` as FFmpeg's AV1 decoders set it from a sequence header OBU's
67
- * payload.
49
+ * Chroma siting from an AV1 sequence header OBU payload, as FFmpeg's AV1 decoders set
50
+ * `avctx->chroma_sample_location`.
68
51
  *
69
- * Throws `RangeError` when the header ends early.
52
+ * @throws RangeError When the header ends early.
70
53
  */
71
54
  export declare function av1SequenceHeaderSiting(payload: Uint8Array): ChromaSiting;
@@ -1,12 +1,8 @@
1
- // Reads chroma siting and sample shape from a stream's parameter sets as FFmpeg does, so browser
2
- // decodes convert and probe the same as a native decode of the stream.
1
+ // Reads chroma siting and sample aspect from parameter sets as FFmpeg does, so browser and native
2
+ // decodes agree.
3
3
  /**
4
- * The chroma siting a codec's streams have unless they signal otherwise.
5
- *
6
- * H.264, HEVC, and MPEG-4 Part 2 default 4:2:0 chroma to the left (H.273
7
- * chroma sample location type 0), as FFmpeg reports them natively. Other
8
- * codecs stay unspecified, which the engine reads as centered, again as
9
- * natively.
4
+ * Default chroma siting for a codec. H.264, HEVC and MPEG-4 Part 2 default to `left` (H.273 type
5
+ * 0); other codecs are `unspecified`.
10
6
  */
11
7
  export function chromaSitingForCodec(codec) {
12
8
  switch (codecFamily(codec)) {
@@ -19,21 +15,14 @@ export function chromaSitingForCodec(codec) {
19
15
  }
20
16
  }
21
17
  /**
22
- * The chroma siting a stream signals, as FFmpeg's decoder reports it on the
23
- * frames it decodes.
18
+ * Chroma siting a stream signals, matching the native host's reading of FFmpeg's decoder.
24
19
  *
25
- * H.264 and HEVC signal it in the sequence parameter set's VUI
26
- * (`chroma_sample_loc_type_top_field`), AV1 in the sequence header
27
- * (`chroma_sample_position`). They are read from the decoder configuration's
28
- * `description` (`avcC`, `hvcC`, `av1C`) or, when it has none (Annex B
29
- * streams, AV1 in MP4), from the stream's first packet. With neither, or for
30
- * another codec, the codec's default ({@link chromaSitingForCodec}).
20
+ * Read from the H.264/HEVC SPS VUI or the AV1 sequence header, found in the configuration's
21
+ * `description` (`avcC`, `hvcC`, `av1C`) or else in the first packet. Falls back to
22
+ * {@link chromaSitingForCodec}.
31
23
  *
32
- * The values follow the native decoder, so both hosts convert alike: FFmpeg's
33
- * reading, except that an H.264 stream with no VUI or a reserved type (above
34
- * 5) is `left`, as H.264 infers it without one (E.2.1) and the native host
35
- * reads what FFmpeg leaves unspecified. An H.264 or HEVC type from 3 to 5 is
36
- * `unspecified`, as are AV1's unknown position and HEVC that is not 4:2:0.
24
+ * An H.264 stream with no VUI or a reserved type (above 5) is `left`, as H.264 infers (E.2.1).
25
+ * H.264 or HEVC types 3 to 5, AV1's unknown position, and non-4:2:0 HEVC are `unspecified`.
37
26
  */
38
27
  export function chromaSitingForStream(codec, description, firstPacket) {
39
28
  const family = codecFamily(codec);
@@ -51,20 +40,16 @@ export function chromaSitingForStream(codec, description, firstPacket) {
51
40
  }
52
41
  }
53
42
  catch {
54
- // A truncated or malformed parameter set says nothing a decoder would use.
43
+ // Decoders ignore a truncated or malformed parameter set too.
55
44
  return undefined;
56
45
  }
57
46
  })();
58
47
  return signalled ?? chromaSitingForCodec(codec);
59
48
  }
60
49
  /**
61
- * The sample aspect ratio an H.264 or HEVC stream signals in its sequence parameter set's VUI
62
- * (`aspect_ratio_idc`, or `sar_width`/`sar_height` for Extended_SAR), read from the decoder
63
- * configuration's `description` (`avcC`, `hvcC`) or, without one, from the stream's first packet.
64
- * `undefined` when the stream signals none (no VUI, `aspect_ratio_idc` 0, a reserved index, or a
65
- * zero term), for another codec, or when the parameter set does not parse: as FFmpeg's decoder
66
- * reports it, which a native probe falls back to when the container declares no ratio
67
- * (`av_guess_sample_aspect_ratio`).
50
+ * Sample aspect ratio from an H.264 or HEVC SPS VUI, read from `description` or else the first
51
+ * packet. `undefined` when none is signalled, for other codecs, or when the SPS does not parse,
52
+ * matching FFmpeg's decoder (which a native probe uses when the container declares no ratio).
68
53
  */
69
54
  export function sampleAspectForStream(codec, description, firstPacket) {
70
55
  try {
@@ -108,10 +93,7 @@ function bytesOf(source) {
108
93
  }
109
94
  return new Uint8Array(source);
110
95
  }
111
- /**
112
- * FFmpeg's `AVChromaLocation` for an H.273 chroma sample location type, using the engine's names
113
- * for the ones it samples.
114
- */
96
+ /** Maps an H.273 chroma sample location type as FFmpeg's `AVChromaLocation` does. */
115
97
  function sitingOfLocationType(type) {
116
98
  switch (type) {
117
99
  case 0:
@@ -121,12 +103,11 @@ function sitingOfLocationType(type) {
121
103
  case 2:
122
104
  return "top-left";
123
105
  default:
124
- // Top, bottom-left, and bottom (3 to 5) are sampled centered natively; anything above 5 is
125
- // reserved.
106
+ // Types 3 to 5 sample as centered natively; above 5 is reserved.
126
107
  return "unspecified";
127
108
  }
128
109
  }
129
- /** An H.264 stream's first SPS NAL unit: its `avcC` record's, or its first packet's. */
110
+ /** First H.264 SPS NAL unit, from the `avcC` record or else the first packet. */
130
111
  function avcSps(description, firstPacket) {
131
112
  return (avcConfigurationSps(bytesOf(description)) ??
132
113
  packetNalUnits(bytesOf(firstPacket)).find((unit) => unit.length > 0 && (unit[0] & 0x1f) === 7));
@@ -135,7 +116,6 @@ function avcSiting(description, firstPacket) {
135
116
  const sps = avcSps(description, firstPacket);
136
117
  return sps === undefined ? undefined : h264SpsSiting(sps);
137
118
  }
138
- /** The first SPS NAL unit of an `avcC` record. */
139
119
  function avcConfigurationSps(record) {
140
120
  if (record === undefined || record.length < 8 || record[0] !== 1) {
141
121
  return undefined;
@@ -148,22 +128,19 @@ function avcConfigurationSps(record) {
148
128
  ? record.subarray(8, 8 + length)
149
129
  : undefined;
150
130
  }
151
- /**
152
- * The profiles whose SPS carries `chroma_format_idc` and the fields after it, as FFmpeg lists them.
153
- */
131
+ /** Profiles whose SPS carries `chroma_format_idc` and following fields (FFmpeg's list). */
154
132
  const H264_HIGH_PROFILES = new Set([
155
133
  100, 110, 122, 244, 44, 83, 86, 118, 128, 138, 144,
156
134
  ]);
157
135
  /**
158
- * `vui.chroma_location` as FFmpeg's H.264 parser derives it from one SPS NAL unit, starting at
159
- * its one-byte NAL header. A location FFmpeg leaves unspecified is read as H.264 infers it: left.
136
+ * Chroma siting from one H.264 SPS NAL unit (including its one-byte header), as FFmpeg's parser
137
+ * derives `vui.chroma_location`. An unspecified location reads as `left`, as H.264 infers.
160
138
  *
161
- * Throws `RangeError` when the SPS ends early.
139
+ * @throws RangeError When the SPS ends early.
162
140
  */
163
141
  export function h264SpsSiting(nal) {
164
142
  return h264Sps(nal).siting;
165
143
  }
166
- /** What one H.264 SPS NAL unit says of its chroma siting and its sample aspect ratio. */
167
144
  function h264Sps(nal) {
168
145
  const bits = new BitReader(unescape(nal.subarray(1)));
169
146
  const profile = bits.bits(8);
@@ -212,8 +189,7 @@ function h264Sps(nal) {
212
189
  bits.ue();
213
190
  bits.ue();
214
191
  }
215
- // FFmpeg's SPS starts zeroed, so it reports no chroma location for a stream without VUI, where
216
- // H.264 infers type 0.
192
+ // Without VUI, H.264 infers type 0.
217
193
  if (!bits.flag())
218
194
  return { siting: "left", aspect: undefined };
219
195
  return commonVui(bits, "left");
@@ -227,10 +203,7 @@ function skipH264ScalingList(bits, size) {
227
203
  last = next === 0 ? last : next;
228
204
  }
229
205
  }
230
- /**
231
- * The sample aspect ratios of `aspect_ratio_idc` 1 to 16 (H.264 table E-1, HEVC table E.1), as
232
- * FFmpeg's `ff_h2645_pixel_aspect` lists them.
233
- */
206
+ /** Sample aspect ratios for `aspect_ratio_idc` 1 to 16 (H.264 table E-1, HEVC table E.1). */
234
207
  const PIXEL_ASPECT = [
235
208
  [1, 1],
236
209
  [12, 11],
@@ -250,8 +223,8 @@ const PIXEL_ASPECT = [
250
223
  [2, 1],
251
224
  ];
252
225
  /**
253
- * `ff_h2645_decode_common_vui_params` up to the chroma location: the sample aspect ratio, and the
254
- * siting, left unless the VUI says otherwise; a reserved type (above 5) maps to `reserved`.
226
+ * Mirrors `ff_h2645_decode_common_vui_params` up to the chroma location. Siting defaults to
227
+ * `left`; a reserved type (above 5) maps to `reserved`.
255
228
  */
256
229
  function commonVui(bits, reserved = "unspecified") {
257
230
  let aspect;
@@ -280,7 +253,7 @@ function commonVui(bits, reserved = "unspecified") {
280
253
  aspect,
281
254
  };
282
255
  }
283
- /** An HEVC stream's first SPS NAL unit: its `hvcC` record's, or its first packet's. */
256
+ /** First HEVC SPS NAL unit, from the `hvcC` record or else the first packet. */
284
257
  function hevcSps(description, firstPacket) {
285
258
  return (hevcConfigurationSps(bytesOf(description)) ??
286
259
  packetNalUnits(bytesOf(firstPacket)).find((unit) => unit.length > 1 && ((unit[0] >> 1) & 0x3f) === 33));
@@ -289,7 +262,6 @@ function hevcSiting(description, firstPacket) {
289
262
  const sps = hevcSps(description, firstPacket);
290
263
  return sps === undefined ? undefined : hevcSpsSiting(sps);
291
264
  }
292
- /** The first SPS NAL unit of an `hvcC` record. */
293
265
  function hevcConfigurationSps(record) {
294
266
  if (record === undefined || record.length < 23 || record[0] !== 1) {
295
267
  return undefined;
@@ -317,15 +289,14 @@ function hevcConfigurationSps(record) {
317
289
  return undefined;
318
290
  }
319
291
  /**
320
- * `avctx->chroma_sample_location` as FFmpeg's HEVC decoder sets it from one SPS NAL unit, starting
321
- * at its two-byte NAL header.
292
+ * Chroma siting from one HEVC SPS NAL unit (including its two-byte header), as FFmpeg's decoder
293
+ * sets `avctx->chroma_sample_location`.
322
294
  *
323
- * Throws `RangeError` when the SPS ends early.
295
+ * @throws RangeError When the SPS ends early.
324
296
  */
325
297
  export function hevcSpsSiting(nal) {
326
298
  return hevcSpsFacts(nal).siting;
327
299
  }
328
- /** What one HEVC SPS NAL unit says of its chroma siting and its sample aspect ratio. */
329
300
  function hevcSpsFacts(nal) {
330
301
  const bits = new BitReader(unescape(nal.subarray(2)));
331
302
  bits.skip(4); // sps_video_parameter_set_id
@@ -379,7 +350,7 @@ function hevcSpsFacts(nal) {
379
350
  bits.skip(pocLsbBits + 1);
380
351
  }
381
352
  bits.skip(2); // sps_temporal_mvp_enabled_flag, strong_intra_smoothing_enabled_flag
382
- // FFmpeg reports a location only for 4:2:0; without VUI its zeroed flags read as left.
353
+ // FFmpeg reports a location only for 4:2:0; without VUI it reads as left.
383
354
  const vui = bits.flag() ? commonVui(bits) : undefined;
384
355
  return {
385
356
  siting: chromaFormat !== 1 ? "unspecified" : (vui?.siting ?? "left"),
@@ -419,10 +390,7 @@ function skipHevcScalingListData(bits) {
419
390
  }
420
391
  }
421
392
  }
422
- /**
423
- * Reads `count` `st_ref_pic_set`s. Each may be predicted from the one before, so their delta POCs
424
- * are kept.
425
- */
393
+ /** Skips `count` `st_ref_pic_set`s, tracking delta POCs because each may predict the next. */
426
394
  function skipShortTermRefPicSets(bits, count) {
427
395
  const sets = [];
428
396
  for (let index = 0; index < count; index += 1) {
@@ -487,15 +455,14 @@ function skipShortTermRefPicSets(bits, count) {
487
455
  }
488
456
  }
489
457
  function av1Siting(description, firstPacket) {
490
- // `av1C` is four bytes of fields, then the configuration OBUs, which may hold the sequence
491
- // header.
458
+ // `av1C` has four bytes of fields, then configuration OBUs that may hold the sequence header.
492
459
  const record = bytesOf(description);
493
460
  const header = (record !== undefined && record.length > 4 && (record[0] & 0x7f) === 1
494
461
  ? sequenceHeaderObu(record.subarray(4))
495
462
  : undefined) ?? sequenceHeaderObu(bytesOf(firstPacket));
496
463
  return header === undefined ? undefined : av1SequenceHeaderSiting(header);
497
464
  }
498
- /** The payload of the first sequence header OBU in a low-overhead bitstream. */
465
+ /** Payload of the first sequence header OBU in a low-overhead bitstream. */
499
466
  function sequenceHeaderObu(data) {
500
467
  if (data === undefined)
501
468
  return undefined;
@@ -527,10 +494,10 @@ function sequenceHeaderObu(data) {
527
494
  return undefined;
528
495
  }
529
496
  /**
530
- * `avctx->chroma_sample_location` as FFmpeg's AV1 decoders set it from a sequence header OBU's
531
- * payload.
497
+ * Chroma siting from an AV1 sequence header OBU payload, as FFmpeg's AV1 decoders set
498
+ * `avctx->chroma_sample_location`.
532
499
  *
533
- * Throws `RangeError` when the header ends early.
500
+ * @throws RangeError When the header ends early.
534
501
  */
535
502
  export function av1SequenceHeaderSiting(payload) {
536
503
  const bits = new BitReader(payload);
@@ -630,7 +597,7 @@ export function av1SequenceHeaderSiting(payload) {
630
597
  return "unspecified";
631
598
  }
632
599
  }
633
- /** NAL units of a packet, Annex B (start codes) or four-byte length-prefixed. */
600
+ /** Splits a packet into NAL units, Annex B or four-byte length-prefixed. */
634
601
  function packetNalUnits(packet) {
635
602
  if (packet === undefined || packet.length < 4)
636
603
  return [];
@@ -669,7 +636,7 @@ function packetNalUnits(packet) {
669
636
  }
670
637
  return units;
671
638
  }
672
- /** RBSP of a NAL unit payload: emulation prevention bytes removed. */
639
+ /** Removes emulation prevention bytes from a NAL unit payload. */
673
640
  function unescape(payload) {
674
641
  const output = new Uint8Array(payload.length);
675
642
  let length = 0;
@@ -1,49 +1,39 @@
1
1
  /**
2
- * Export containers: the file formats an export is written in, and which codecs each one offers.
2
+ * Export containers and the codecs each offers.
3
3
  *
4
- * This is the engine's container policy (`alpha2_media::Container`), mirrored here as a constant so
5
- * a browser sink, or a client of a native host, can validate a request before it opens an encoder
6
- * or starts a host. Both copies are pinned by the shared corpus
7
- * `tests/cases/export-container-corpus.json`. A container offers only the codecs its mainstream
8
- * players decode in it, which is narrower than what mediabunny writes: QuickTime does not play
9
- * VP9, AV1 or Opus in a MOV.
4
+ * Mirrors the engine's `alpha2_media::Container` policy so requests can be validated before an
5
+ * encoder or host starts. A container offers only what mainstream players decode in it, which is
6
+ * narrower than what mediabunny can write (QuickTime does not play VP9, AV1 or Opus in MOV).
10
7
  */
11
8
  import { type BrowserVideoCodec, type EngineVideoCodec } from "./rate-control.js";
12
- /** The containers an export can be written in. */
13
9
  export type ExportContainer = "mp4" | "mov" | "mkv" | "webm";
14
- /** The audio codecs an export can carry. */
15
10
  export type ExportAudioCodec = "aac" | "opus";
16
11
  /** One container's policy. */
17
12
  export interface ContainerPolicy {
18
13
  readonly id: ExportContainer;
19
- /** Lowercase file extensions without the dot; the first names an exported file. */
14
+ /** Lowercase extensions without the dot; the first is used for exported files. */
20
15
  readonly extensions: readonly string[];
21
16
  readonly mimeType: string;
22
- /** Offered video codecs in the engine's spelling, the default first. */
17
+ /** Video codecs in the engine's spelling, default first. */
23
18
  readonly video: readonly EngineVideoCodec[];
24
- /** Offered audio codecs, the default first; also the order a browser falls back in. */
19
+ /** Audio codecs, default first; also the browser's fallback order. */
25
20
  readonly audio: readonly ExportAudioCodec[];
26
21
  }
27
- /** Every container's policy, in the engine's order. */
28
22
  export declare const EXPORT_CONTAINERS: readonly ContainerPolicy[];
29
23
  /** Why an export format request cannot be resolved. */
30
- export type ExportFormatErrorReason = "unknown" | "unknown-extension" | "extension-mismatch" | "video-codec-not-offered" | "audio-codec-not-offered";
31
- /** A container or codec request the policy refuses. */
24
+ export type ExportFormatErrorReason = "unknown" | "unknown-extension" | "extension-mismatch" | "video-codec-not-offered" | "audio-codec-not-offered" | "alpha-unsupported" | "alpha-unsupported-in-container" | "bit-depth-unsupported";
25
+ /** Thrown when a container or codec request is not allowed. */
32
26
  export declare class ExportFormatError extends RangeError {
33
27
  readonly reason: ExportFormatErrorReason;
34
28
  constructor(reason: ExportFormatErrorReason, message: string);
35
29
  }
36
- /**
37
- * The policy of a container.
38
- *
39
- * @throws ExportFormatError When `container` is not one of {@link EXPORT_CONTAINERS}.
40
- */
30
+ /** @throws ExportFormatError When `container` is not one of {@link EXPORT_CONTAINERS}. */
41
31
  export declare function containerPolicy(container: string): ContainerPolicy;
42
- /** The container a file extension (without the dot) names, case-insensitively. */
32
+ /** Looks up a container by extension (without the dot), case-insensitively. */
43
33
  export declare function containerFromExtension(extension: string): ExportContainer | undefined;
44
- /** The video codec a browser export in `container` gets when it names none. */
34
+ /** Default browser video codec for `container`. */
45
35
  export declare function defaultVideoCodec(container: ExportContainer): BrowserVideoCodec;
46
- /** The audio codecs `container` offers, in the order a browser tries them. */
36
+ /** Audio codecs `container` offers, in browser fallback order. */
47
37
  export declare function offeredAudioCodecs(container: ExportContainer): readonly ExportAudioCodec[];
48
38
  /** A resolved export format, in the engine's codec spelling. */
49
39
  export interface EngineExportFormat {
@@ -52,13 +42,12 @@ export interface EngineExportFormat {
52
42
  readonly audioCodec: ExportAudioCodec;
53
43
  }
54
44
  /**
55
- * Resolves an export's container and codecs, in the engine's codec spelling (`h264`): the same rule
56
- * as the engine's `resolve_export_format`, which a native host applies to the same request.
45
+ * Resolves an export's container and codecs in the engine's codec spelling (`h264`), matching the
46
+ * engine's `resolve_export_format`.
57
47
  *
58
- * - `container` names the container. When it is absent, `extension` names it, and with neither
59
- * (a browser export, which writes no path) it is MP4.
60
- * - A named container and an extension must agree.
61
- * - An absent codec is the container's default, and a named one must be one it offers.
48
+ * - The container comes from `container`, else from `extension`, else MP4.
49
+ * - A given container and extension must agree.
50
+ * - A missing codec takes the container's default; a given one must be offered.
62
51
  *
63
52
  * @throws ExportFormatError When the request breaks one of these rules.
64
53
  */
@@ -76,7 +65,7 @@ export interface ExportFormat {
76
65
  readonly audioCodec: ExportAudioCodec;
77
66
  }
78
67
  /**
79
- * {@link resolveEngineExportFormat} in the browser's codec spelling (`avc`), for a browser sink.
68
+ * {@link resolveEngineExportFormat} in the browser's codec spelling (`avc`).
80
69
  *
81
70
  * @throws ExportFormatError When the request breaks one of its rules.
82
71
  */
@@ -87,3 +76,25 @@ export declare function resolveExportFormat(request: {
87
76
  readonly videoCodec?: BrowserVideoCodec;
88
77
  readonly audioCodec?: ExportAudioCodec;
89
78
  }): ExportFormat;
79
+ /** Output pixel layout, matching the engine's `PixelLayout`. */
80
+ export interface PixelLayout {
81
+ readonly chroma: "420" | "422" | "444";
82
+ readonly bitDepth: 8 | 10;
83
+ readonly alpha: boolean;
84
+ /** `FFmpeg` pixel format name, such as `yuv420p` or `yuva444p10le`. */
85
+ readonly format: string;
86
+ }
87
+ /**
88
+ * Resolves the pixel layout for `codec` in `container`, matching the engine's `resolve_pixels`.
89
+ * `bitDepth` defaults to 10 for ProRes and 8 otherwise.
90
+ *
91
+ * H.264 and MPEG-4 Part 2 write 8-bit 4:2:0. HEVC and AV1 also allow 10-bit. VP9 allows 10-bit, or
92
+ * 8-bit alpha outside MP4. ProRes is 10-bit 4:2:2 (422 HQ), or 4:4:4 with alpha (4444). The
93
+ * container must offer the codec ({@link resolveEngineExportFormat} checks that).
94
+ *
95
+ * @throws ExportFormatError When the codec cannot be written with this depth and alpha.
96
+ */
97
+ export declare function resolvePixels(codec: EngineVideoCodec, container: ExportContainer, request?: {
98
+ readonly bitDepth?: number;
99
+ readonly alpha?: boolean;
100
+ }): PixelLayout;
@@ -1,15 +1,11 @@
1
1
  /**
2
- * Export containers: the file formats an export is written in, and which codecs each one offers.
2
+ * Export containers and the codecs each offers.
3
3
  *
4
- * This is the engine's container policy (`alpha2_media::Container`), mirrored here as a constant so
5
- * a browser sink, or a client of a native host, can validate a request before it opens an encoder
6
- * or starts a host. Both copies are pinned by the shared corpus
7
- * `tests/cases/export-container-corpus.json`. A container offers only the codecs its mainstream
8
- * players decode in it, which is narrower than what mediabunny writes: QuickTime does not play
9
- * VP9, AV1 or Opus in a MOV.
4
+ * Mirrors the engine's `alpha2_media::Container` policy so requests can be validated before an
5
+ * encoder or host starts. A container offers only what mainstream players decode in it, which is
6
+ * narrower than what mediabunny can write (QuickTime does not play VP9, AV1 or Opus in MOV).
10
7
  */
11
8
  import { engineVideoCodec, } from "./rate-control.js";
12
- /** Every container's policy, in the engine's order. */
13
9
  export const EXPORT_CONTAINERS = [
14
10
  {
15
11
  id: "mp4",
@@ -22,7 +18,7 @@ export const EXPORT_CONTAINERS = [
22
18
  id: "mov",
23
19
  extensions: ["mov"],
24
20
  mimeType: "video/quicktime",
25
- video: ["h264", "hevc", "mpeg4-part2"],
21
+ video: ["h264", "hevc", "mpeg4-part2", "prores"],
26
22
  audio: ["aac"],
27
23
  },
28
24
  {
@@ -40,7 +36,7 @@ export const EXPORT_CONTAINERS = [
40
36
  audio: ["opus"],
41
37
  },
42
38
  ];
43
- /** A container or codec request the policy refuses. */
39
+ /** Thrown when a container or codec request is not allowed. */
44
40
  export class ExportFormatError extends RangeError {
45
41
  reason;
46
42
  constructor(reason, message) {
@@ -49,39 +45,34 @@ export class ExportFormatError extends RangeError {
49
45
  this.reason = reason;
50
46
  }
51
47
  }
52
- /**
53
- * The policy of a container.
54
- *
55
- * @throws ExportFormatError When `container` is not one of {@link EXPORT_CONTAINERS}.
56
- */
48
+ /** @throws ExportFormatError When `container` is not one of {@link EXPORT_CONTAINERS}. */
57
49
  export function containerPolicy(container) {
58
50
  const policy = EXPORT_CONTAINERS.find((entry) => entry.id === container);
59
51
  if (policy === undefined)
60
52
  throw new ExportFormatError("unknown", `unknown container "${container}"; expected one of ${EXPORT_CONTAINERS.map((entry) => entry.id).join(", ")}`);
61
53
  return policy;
62
54
  }
63
- /** The container a file extension (without the dot) names, case-insensitively. */
55
+ /** Looks up a container by extension (without the dot), case-insensitively. */
64
56
  export function containerFromExtension(extension) {
65
57
  const lower = extension.toLowerCase();
66
58
  return EXPORT_CONTAINERS.find((entry) => entry.extensions.includes(lower))
67
59
  ?.id;
68
60
  }
69
- /** The video codec a browser export in `container` gets when it names none. */
61
+ /** Default browser video codec for `container`. */
70
62
  export function defaultVideoCodec(container) {
71
63
  return browserVideoCodec(containerPolicy(container).video[0]);
72
64
  }
73
- /** The audio codecs `container` offers, in the order a browser tries them. */
65
+ /** Audio codecs `container` offers, in browser fallback order. */
74
66
  export function offeredAudioCodecs(container) {
75
67
  return containerPolicy(container).audio;
76
68
  }
77
69
  /**
78
- * Resolves an export's container and codecs, in the engine's codec spelling (`h264`): the same rule
79
- * as the engine's `resolve_export_format`, which a native host applies to the same request.
70
+ * Resolves an export's container and codecs in the engine's codec spelling (`h264`), matching the
71
+ * engine's `resolve_export_format`.
80
72
  *
81
- * - `container` names the container. When it is absent, `extension` names it, and with neither
82
- * (a browser export, which writes no path) it is MP4.
83
- * - A named container and an extension must agree.
84
- * - An absent codec is the container's default, and a named one must be one it offers.
73
+ * - The container comes from `container`, else from `extension`, else MP4.
74
+ * - A given container and extension must agree.
75
+ * - A missing codec takes the container's default; a given one must be offered.
85
76
  *
86
77
  * @throws ExportFormatError When the request breaks one of these rules.
87
78
  */
@@ -100,7 +91,7 @@ export function resolveEngineExportFormat(request) {
100
91
  };
101
92
  }
102
93
  /**
103
- * {@link resolveEngineExportFormat} in the browser's codec spelling (`avc`), for a browser sink.
94
+ * {@link resolveEngineExportFormat} in the browser's codec spelling (`avc`).
104
95
  *
105
96
  * @throws ExportFormatError When the request breaks one of its rules.
106
97
  */
@@ -144,7 +135,48 @@ function dotted(extensions) {
144
135
  function browserVideoCodec(codec) {
145
136
  if (codec === "h264")
146
137
  return "avc";
147
- if (codec === "mpeg4-part2")
148
- throw new ExportFormatError("video-codec-not-offered", "the browser has no mpeg4-part2 encoder");
138
+ if (codec === "mpeg4-part2" || codec === "prores")
139
+ throw new ExportFormatError("video-codec-not-offered", `the browser has no ${codec} encoder`);
149
140
  return codec;
150
141
  }
142
+ /**
143
+ * Resolves the pixel layout for `codec` in `container`, matching the engine's `resolve_pixels`.
144
+ * `bitDepth` defaults to 10 for ProRes and 8 otherwise.
145
+ *
146
+ * H.264 and MPEG-4 Part 2 write 8-bit 4:2:0. HEVC and AV1 also allow 10-bit. VP9 allows 10-bit, or
147
+ * 8-bit alpha outside MP4. ProRes is 10-bit 4:2:2 (422 HQ), or 4:4:4 with alpha (4444). The
148
+ * container must offer the codec ({@link resolveEngineExportFormat} checks that).
149
+ *
150
+ * @throws ExportFormatError When the codec cannot be written with this depth and alpha.
151
+ */
152
+ export function resolvePixels(codec, container, request = {}) {
153
+ const bitDepth = request.bitDepth ?? (codec === "prores" ? 10 : 8);
154
+ if (bitDepth !== 8 && bitDepth !== 10)
155
+ throw new ExportFormatError("bit-depth-unsupported", `bitDepth: ${bitDepth} is not 8 or 10`);
156
+ const alpha = request.alpha === true;
157
+ const refuseDepth = () => new ExportFormatError("bit-depth-unsupported", `video codec ${codec} cannot be written at ${bitDepth} bits${alpha ? " with alpha" : ""}`);
158
+ let chroma = "420";
159
+ if (codec === "prores") {
160
+ if (bitDepth === 8)
161
+ throw refuseDepth();
162
+ chroma = alpha ? "444" : "422";
163
+ }
164
+ else if (codec === "vp9") {
165
+ if (alpha && container === "mp4")
166
+ throw new ExportFormatError("alpha-unsupported-in-container", `${container} cannot carry vp9 alpha; use webm or mkv`);
167
+ if (alpha && bitDepth === 10)
168
+ throw refuseDepth();
169
+ }
170
+ else {
171
+ if (alpha)
172
+ throw new ExportFormatError("alpha-unsupported", `video codec ${codec} cannot keep alpha; use vp9 (webm, mkv) or prores (mov)`);
173
+ if (bitDepth === 10 && codec !== "hevc" && codec !== "av1")
174
+ throw refuseDepth();
175
+ }
176
+ return {
177
+ chroma,
178
+ bitDepth,
179
+ alpha,
180
+ format: `yuv${alpha ? "a" : ""}${chroma}p${bitDepth === 10 ? "10le" : ""}`,
181
+ };
182
+ }