@svta/cml-iso-bmff 1.0.0-alpha.1 → 1.0.0-alpha.2

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/dist/index.js CHANGED
@@ -1,6 +1,17 @@
1
1
  import { UTF_8, decodeText, encodeText } from "@svta/cml-utils";
2
2
 
3
+ //#region src/utils/createWriterConfig.ts
4
+ function createWriterConfig(config) {
5
+ return { writers: config?.writers ?? {} };
6
+ }
7
+
8
+ //#endregion
3
9
  //#region src/utils/CONTAINERS.ts
10
+ /**
11
+ * List of container box types
12
+ *
13
+ * @public
14
+ */
4
15
  const CONTAINERS = [
5
16
  "dinf",
6
17
  "edts",
@@ -42,6 +53,15 @@ function isContainer(box) {
42
53
  return "boxes" in box || CONTAINERS.includes(box.type);
43
54
  }
44
55
 
56
+ //#endregion
57
+ //#region src/fields/TEMPLATE.ts
58
+ /**
59
+ * The template field type
60
+ *
61
+ * @public
62
+ */
63
+ const TEMPLATE = "template";
64
+
45
65
  //#endregion
46
66
  //#region src/fields/UINT.ts
47
67
  /**
@@ -133,9 +153,9 @@ var IsoBoxWriteView = class {
133
153
  new Uint8Array(this.dataView.buffer).set(data, this.cursor);
134
154
  this.cursor += data.length;
135
155
  };
136
- this.writeArray = (data, type$1, size$1) => {
137
- const write = type$1 === UINT ? this.writeUint : this.writeInt;
138
- for (const value of data) write(value, size$1);
156
+ this.writeArray = (data, type$1, size$1, length) => {
157
+ const write = type$1 === UINT ? this.writeUint : type$1 === TEMPLATE ? this.writeTemplate : this.writeInt;
158
+ for (let i = 0; i < length; i++) write(data[i] ?? 0, size$1);
139
159
  };
140
160
  this.writeTemplate = (value, size$1) => {
141
161
  const shift = size$1 === 4 ? 16 : 8;
@@ -204,12 +224,12 @@ var IsoBoxWriteView = class {
204
224
  *
205
225
  * @returns An IsoBmffWriter containing the encoded box
206
226
  */
207
- function writeContainerBox(box, writers) {
227
+ function writeContainerBox(box, config) {
208
228
  const children = [];
209
229
  const headerSize = 8;
210
230
  let childrenSize = 0;
211
231
  for (const childBox of box.boxes) {
212
- const view = writeIsoBox(childBox, writers);
232
+ const view = writeIsoBox(childBox, config);
213
233
  childrenSize += view.byteLength;
214
234
  children.push(view);
215
235
  }
@@ -220,24 +240,24 @@ function writeContainerBox(box, writers) {
220
240
  }
221
241
 
222
242
  //#endregion
223
- //#region src/writeIsoBox.ts
243
+ //#region src/writers/writeBox.ts
224
244
  /**
225
- * Write an ISO box to a Uint8Array.
245
+ * Write a box to a Uint8Array.
226
246
  *
227
247
  * @param box - The box to write
228
248
  * @param writers - The writers to use
229
249
  * @returns The written box
230
250
  *
231
- * @public
251
+ * @internal
232
252
  */
233
- function writeIsoBox(box, writers) {
253
+ function writeBox(box, config) {
234
254
  let view = null;
235
255
  if ("type" in box) {
236
256
  const { type } = box;
237
- if (type !== "" && isContainer(box)) view = writeContainerBox(box, writers);
257
+ if (type !== "" && isContainer(box)) view = writeContainerBox(box, config);
238
258
  else {
239
- const writer = writers[type];
240
- if (writer) view = writer(box);
259
+ const writer = config.writers[type];
260
+ if (writer) view = writer(box, config);
241
261
  else if ("view" in box) view = box.view;
242
262
  }
243
263
  if (!view) throw new Error(`No writer found for box type: ${type}`);
@@ -247,6 +267,21 @@ function writeIsoBox(box, writers) {
247
267
  return new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
248
268
  }
249
269
 
270
+ //#endregion
271
+ //#region src/writeIsoBox.ts
272
+ /**
273
+ * Write an ISO box to a Uint8Array.
274
+ *
275
+ * @param box - The box to write
276
+ * @param writers - The writers to use
277
+ * @returns The written box
278
+ *
279
+ * @public
280
+ */
281
+ function writeIsoBox(box, config) {
282
+ return writeBox(box, createWriterConfig(config));
283
+ }
284
+
250
285
  //#endregion
251
286
  //#region src/IsoBoxReadableStream.ts
252
287
  /**
@@ -263,13 +298,13 @@ var IsoBoxReadableStream = class extends ReadableStream {
263
298
  */
264
299
  constructor(boxes, config = {}) {
265
300
  const iterator = boxes[Symbol.iterator]();
266
- const { writers = {} } = config;
301
+ const cfg = createWriterConfig(config);
267
302
  function pull(controller) {
268
303
  const desiredSize = controller.desiredSize ?? 0;
269
304
  for (let i = 0; i < desiredSize; i++) {
270
305
  const { value, done } = iterator.next();
271
306
  if (done) controller.close();
272
- else controller.enqueue(writeIsoBox(value, writers));
307
+ else controller.enqueue(writeIsoBox(value, cfg));
273
308
  }
274
309
  }
275
310
  super({
@@ -963,7 +998,7 @@ function readMvhd(view) {
963
998
  volume: readTemplate$1(2),
964
999
  reserved1: readUint$1(2),
965
1000
  reserved2: readArray(UINT, 4, 2),
966
- matrix: readArray(UINT, 4, 9),
1001
+ matrix: readArray(TEMPLATE, 4, 9),
967
1002
  preDefined: readArray(UINT, 4, 6),
968
1003
  nextTrackId: readUint$1(4)
969
1004
  };
@@ -1500,15 +1535,6 @@ function readTfra(view) {
1500
1535
  };
1501
1536
  }
1502
1537
 
1503
- //#endregion
1504
- //#region src/fields/TEMPLATE.ts
1505
- /**
1506
- * The template field type
1507
- *
1508
- * @public
1509
- */
1510
- const TEMPLATE = "template";
1511
-
1512
1538
  //#endregion
1513
1539
  //#region src/readers/readTkhd.ts
1514
1540
  /**
@@ -2154,8 +2180,8 @@ function isFullBox(box) {
2154
2180
  * @public
2155
2181
  */
2156
2182
  function writeIsoBoxes(boxes, config) {
2157
- const { writers = {} } = config ?? {};
2158
- return Array.from(boxes, (box) => writeIsoBox(box, writers));
2183
+ const cfg = createWriterConfig(config);
2184
+ return Array.from(boxes, (box) => writeBox(box, cfg));
2159
2185
  }
2160
2186
 
2161
2187
  //#endregion
@@ -2208,18 +2234,18 @@ function writeVisualSampleEntryBox(box, type) {
2208
2234
  const preDefined3Size = 2;
2209
2235
  const configSize = box.config.length;
2210
2236
  const writer = new IsoBoxWriteView(type, headerSize + reserved1Size + dataReferenceIndexSize + preDefined1Size + reserved2Size + preDefined2Size + widthSize + heightSize + horizresolutionSize + vertresolutionSize + reserved3Size + frameCountSize + compressorNameSize + depthSize + preDefined3Size + configSize);
2211
- for (let i = 0; i < 6; i++) writer.writeUint(box.reserved1[i] ?? 0, 1);
2237
+ writer.writeArray(box.reserved1, UINT, 1, 6);
2212
2238
  writer.writeUint(box.dataReferenceIndex, 2);
2213
2239
  writer.writeUint(box.preDefined1, 2);
2214
2240
  writer.writeUint(box.reserved2, 2);
2215
- for (let i = 0; i < 3; i++) writer.writeUint(box.preDefined2[i] ?? 0, 4);
2241
+ writer.writeArray(box.preDefined2, UINT, 4, 3);
2216
2242
  writer.writeUint(box.width, 2);
2217
2243
  writer.writeUint(box.height, 2);
2218
2244
  writer.writeTemplate(box.horizresolution, 4);
2219
2245
  writer.writeTemplate(box.vertresolution, 4);
2220
2246
  writer.writeUint(box.reserved3, 4);
2221
2247
  writer.writeUint(box.frameCount, 2);
2222
- for (let i = 0; i < 32; i++) writer.writeUint(box.compressorName[i] ?? 0, 1);
2248
+ writer.writeArray(box.compressorName, UINT, 1, 32);
2223
2249
  writer.writeUint(box.depth, 2);
2224
2250
  writer.writeUint(box.preDefined3 & 65535, 2);
2225
2251
  writer.writeBytes(box.config);
@@ -2316,6 +2342,31 @@ function writeCtts(box) {
2316
2342
  return writer;
2317
2343
  }
2318
2344
 
2345
+ //#endregion
2346
+ //#region src/writers/writeDref.ts
2347
+ /**
2348
+ * Write a DataReferenceBox to an IsoDataWriter.
2349
+ *
2350
+ * @param box - The DataReferenceBox fields to write
2351
+ *
2352
+ * @returns An IsoDataWriter containing the encoded box
2353
+ *
2354
+ * @public
2355
+ */
2356
+ function writeDref(box, config) {
2357
+ const headerSize = 8;
2358
+ const fullBoxSize = 4;
2359
+ const entryCountSize = 4;
2360
+ const entryCount = box.entries.length;
2361
+ const entries = writeIsoBoxes(box.entries, config);
2362
+ const entriesSize = entries.reduce((size, entry) => size + entry.byteLength, 0);
2363
+ const writer = new IsoBoxWriteView("dref", headerSize + fullBoxSize + entryCountSize + entriesSize);
2364
+ writer.writeFullBox(box.version, box.flags);
2365
+ writer.writeUint(entryCount, 4);
2366
+ for (const entry of entries) writer.writeBytes(entry);
2367
+ return writer;
2368
+ }
2369
+
2319
2370
  //#endregion
2320
2371
  //#region src/writers/writeElng.ts
2321
2372
  /**
@@ -2435,9 +2486,9 @@ function writeEnca(box) {
2435
2486
  const samplerateSize = 4;
2436
2487
  const esdsSize = box.esds.length;
2437
2488
  const writer = new IsoBoxWriteView("enca", headerSize + reserved1Size + dataReferenceIndexSize + reserved2Size + channelcountSize + samplesizeSize + preDefinedSize + reserved3Size + samplerateSize + esdsSize);
2438
- for (let i = 0; i < 6; i++) writer.writeUint(box.reserved1[i] ?? 0, 1);
2489
+ writer.writeArray(box.reserved1, UINT, 1, 6);
2439
2490
  writer.writeUint(box.dataReferenceIndex, 2);
2440
- for (let i = 0; i < 2; i++) writer.writeUint(box.reserved2[i] ?? 0, 4);
2491
+ writer.writeArray(box.reserved2, UINT, 4, 2);
2441
2492
  writer.writeUint(box.channelcount, 2);
2442
2493
  writer.writeUint(box.samplesize, 2);
2443
2494
  writer.writeUint(box.preDefined, 2);
@@ -2549,7 +2600,7 @@ function writeHdlr(box) {
2549
2600
  writer.writeFullBox(box.version, box.flags);
2550
2601
  writer.writeUint(box.preDefined, 4);
2551
2602
  writer.writeString(box.handlerType);
2552
- for (let i = 0; i < 3; i++) writer.writeUint(box.reserved[i] ?? 0, 4);
2603
+ writer.writeArray(box.reserved, UINT, 4, 3);
2553
2604
  writer.writeTerminatedString(box.name);
2554
2605
  return writer;
2555
2606
  }
@@ -2831,9 +2882,9 @@ function writeMp4a(box) {
2831
2882
  const samplerateSize = 4;
2832
2883
  const esdsSize = box.esds.length;
2833
2884
  const writer = new IsoBoxWriteView("mp4a", headerSize + reserved1Size + dataReferenceIndexSize + reserved2Size + channelcountSize + samplesizeSize + preDefinedSize + reserved3Size + samplerateSize + esdsSize);
2834
- for (let i = 0; i < 6; i++) writer.writeUint(box.reserved1[i] ?? 0, 1);
2885
+ writer.writeArray(box.reserved1, UINT, 1, 6);
2835
2886
  writer.writeUint(box.dataReferenceIndex, 2);
2836
- for (let i = 0; i < 2; i++) writer.writeUint(box.reserved2[i] ?? 0, 4);
2887
+ writer.writeArray(box.reserved2, UINT, 4, 2);
2837
2888
  writer.writeUint(box.channelcount, 2);
2838
2889
  writer.writeUint(box.samplesize, 2);
2839
2890
  writer.writeUint(box.preDefined, 2);
@@ -2870,9 +2921,9 @@ function writeMvhd(box) {
2870
2921
  writer.writeTemplate(box.rate, 4);
2871
2922
  writer.writeTemplate(box.volume, 2);
2872
2923
  writer.writeUint(box.reserved1, 2);
2873
- for (let i = 0; i < 2; i++) writer.writeUint(box.reserved2[i] ?? 0, 4);
2874
- for (let i = 0; i < 9; i++) writer.writeTemplate(box.matrix[i] ?? 0, 4);
2875
- for (let i = 0; i < 6; i++) writer.writeUint(box.preDefined[i] ?? 0, 4);
2924
+ writer.writeArray(box.reserved2, UINT, 4, 2);
2925
+ writer.writeArray(box.matrix, TEMPLATE, 4, 9);
2926
+ writer.writeArray(box.preDefined, UINT, 4, 6);
2876
2927
  writer.writeUint(box.nextTrackId, 4);
2877
2928
  return writer;
2878
2929
  }
@@ -2974,13 +3025,13 @@ function writePssh(box) {
2974
3025
  const dataSize = box.dataSize;
2975
3026
  const writer = new IsoBoxWriteView("pssh", headerSize + fullBoxSize + systemIdSize + kidCountSize + kidSize + dataSizeField + dataSize);
2976
3027
  writer.writeFullBox(box.version, box.flags);
2977
- for (let i = 0; i < 16; i++) writer.writeUint(box.systemId[i] ?? 0, 1);
3028
+ writer.writeArray(box.systemId, UINT, 1, 16);
2978
3029
  if (box.version > 0) {
2979
3030
  writer.writeUint(box.kidCount, 4);
2980
- for (let i = 0; i < box.kidCount; i++) writer.writeUint(box.kid[i] ?? 0, 1);
3031
+ writer.writeArray(box.kid, UINT, 1, box.kidCount);
2981
3032
  }
2982
3033
  writer.writeUint(box.dataSize, 4);
2983
- for (let i = 0; i < box.dataSize; i++) writer.writeUint(box.data[i] ?? 0, 1);
3034
+ writer.writeArray(box.data, UINT, 1, box.dataSize);
2984
3035
  return writer;
2985
3036
  }
2986
3037
 
@@ -3170,6 +3221,31 @@ function writeSthd(box) {
3170
3221
  return writer;
3171
3222
  }
3172
3223
 
3224
+ //#endregion
3225
+ //#region src/writers/writeStsd.ts
3226
+ /**
3227
+ * Write a SampleDescriptionBox to an IsoDataWriter.
3228
+ *
3229
+ * @param box - The SampleDescriptionBox fields to write
3230
+ *
3231
+ * @returns An IsoDataWriter containing the encoded box
3232
+ *
3233
+ * @public
3234
+ */
3235
+ function writeStsd(box, config) {
3236
+ const headerSize = 8;
3237
+ const fullBoxSize = 4;
3238
+ const entryCountSize = 4;
3239
+ const entryCount = box.entries.length;
3240
+ const entries = writeIsoBoxes(box.entries, config);
3241
+ const entriesSize = entries.reduce((size, entry) => size + entry.byteLength, 0);
3242
+ const writer = new IsoBoxWriteView("stsd", headerSize + fullBoxSize + entryCountSize + entriesSize);
3243
+ writer.writeFullBox(box.version, box.flags);
3244
+ writer.writeUint(entryCount, 4);
3245
+ for (const entry of entries) writer.writeBytes(entry);
3246
+ return writer;
3247
+ }
3248
+
3173
3249
  //#endregion
3174
3250
  //#region src/writers/writeStss.ts
3175
3251
  /**
@@ -3320,7 +3396,7 @@ function writeTenc(box) {
3320
3396
  writer.writeFullBox(box.version, box.flags);
3321
3397
  writer.writeUint(box.defaultIsEncrypted, 3);
3322
3398
  writer.writeUint(box.defaultIvSize, 1);
3323
- for (let i = 0; i < 16; i++) writer.writeUint(box.defaultKid[i] ?? 0, 1);
3399
+ writer.writeArray(box.defaultKid, UINT, 1, 16);
3324
3400
  return writer;
3325
3401
  }
3326
3402
 
@@ -3444,12 +3520,12 @@ function writeTkhd(box) {
3444
3520
  writer.writeUint(box.trackId, 4);
3445
3521
  writer.writeUint(box.reserved1, 4);
3446
3522
  writer.writeUint(box.duration, size);
3447
- for (let i = 0; i < 2; i++) writer.writeUint(box.reserved2[i] ?? 0, 4);
3523
+ writer.writeArray(box.reserved2, UINT, 4, 2);
3448
3524
  writer.writeUint(box.layer, 2);
3449
3525
  writer.writeUint(box.alternateGroup, 2);
3450
3526
  writer.writeTemplate(box.volume, 2);
3451
3527
  writer.writeUint(box.reserved3, 2);
3452
- for (let i = 0; i < 9; i++) writer.writeTemplate(box.matrix[i] ?? 0, 4);
3528
+ writer.writeArray(box.matrix, TEMPLATE, 4, 9);
3453
3529
  writer.writeTemplate(box.width, 4);
3454
3530
  writer.writeTemplate(box.height, 4);
3455
3531
  return writer;
@@ -3600,7 +3676,7 @@ function writeVmhd(box) {
3600
3676
  const writer = new IsoBoxWriteView("vmhd", 20);
3601
3677
  writer.writeFullBox(box.version, box.flags);
3602
3678
  writer.writeUint(box.graphicsmode, 2);
3603
- for (let i = 0; i < 3; i++) writer.writeUint(box.opcolor[i] ?? 0, 2);
3679
+ writer.writeArray(box.opcolor, UINT, 2, 3);
3604
3680
  return writer;
3605
3681
  }
3606
3682
 
@@ -3636,5 +3712,5 @@ function writeVtte(_) {
3636
3712
  }
3637
3713
 
3638
3714
  //#endregion
3639
- export { IsoBoxReadableStream, createIsoBoxReadableStream, isContainer, isFullBox, readArdi, readAvc1, readAvc2, readAvc3, readAvc4, readCtts, readDref, readElng, readElst, readEmsg, readEnca, readEncv, readFree, readFrma, readFtyp, readHdlr, readHev1, readHvc1, readIden, readImda, readIsoBoxes, readKind, readLabl, readMdat, readMdhd, readMehd, readMeta, readMfhd, readMfro, readMp4a, readMvhd, readPayl, readPrft, readPrsl, readPssh, readSchm, readSdtp, readSidx, readSkip, readSmhd, readSsix, readSthd, readStsd, readStss, readSttg, readStts, readStyp, readSubs, readTenc, readTfdt, readTfhd, readTfra, readTkhd, readTrex, readTrun, readUrl, readUrn, readVlab, readVmhd, readVttC, readVtte, traverseIsoBoxes, writeArdi, writeAvc1, writeAvc2, writeAvc3, writeAvc4, writeCtts, writeElng, writeElst, writeEmsg, writeEnca, writeEncv, writeFree, writeFrma, writeFtyp, writeHdlr, writeHev1, writeHvc1, writeIden, writeImda, writeIsoBox, writeIsoBoxes, writeKind, writeLabl, writeMdat, writeMdhd, writeMehd, writeMeta, writeMfhd, writeMfro, writeMp4a, writeMvhd, writePayl, writePrft, writePrsl, writePssh, writeSchm, writeSdtp, writeSidx, writeSkip, writeSmhd, writeSsix, writeSthd, writeStss, writeSttg, writeStts, writeStyp, writeSubs, writeTenc, writeTfdt, writeTfhd, writeTfra, writeTkhd, writeTrex, writeTrun, writeUrl, writeUrn, writeVisualSampleEntryBox, writeVlab, writeVmhd, writeVttC, writeVtte };
3715
+ export { CONTAINERS, IsoBoxReadableStream, createIsoBoxReadableStream, isContainer, isFullBox, readArdi, readAvc1, readAvc2, readAvc3, readAvc4, readCtts, readDref, readElng, readElst, readEmsg, readEnca, readEncv, readFree, readFrma, readFtyp, readHdlr, readHev1, readHvc1, readIden, readImda, readIsoBoxes, readKind, readLabl, readMdat, readMdhd, readMehd, readMeta, readMfhd, readMfro, readMp4a, readMvhd, readPayl, readPrft, readPrsl, readPssh, readSchm, readSdtp, readSidx, readSkip, readSmhd, readSsix, readSthd, readStsd, readStss, readSttg, readStts, readStyp, readSubs, readTenc, readTfdt, readTfhd, readTfra, readTkhd, readTrex, readTrun, readUrl, readUrn, readVlab, readVmhd, readVttC, readVtte, traverseIsoBoxes, writeArdi, writeAvc1, writeAvc2, writeAvc3, writeAvc4, writeCtts, writeDref, writeElng, writeElst, writeEmsg, writeEnca, writeEncv, writeFree, writeFrma, writeFtyp, writeHdlr, writeHev1, writeHvc1, writeIden, writeImda, writeIsoBox, writeIsoBoxes, writeKind, writeLabl, writeMdat, writeMdhd, writeMehd, writeMeta, writeMfhd, writeMfro, writeMp4a, writeMvhd, writePayl, writePrft, writePrsl, writePssh, writeSchm, writeSdtp, writeSidx, writeSkip, writeSmhd, writeSsix, writeSthd, writeStsd, writeStss, writeSttg, writeStts, writeStyp, writeSubs, writeTenc, writeTfdt, writeTfhd, writeTfra, writeTkhd, writeTrex, writeTrun, writeUrl, writeUrn, writeVlab, writeVmhd, writeVttC, writeVtte };
3640
3716
  //# sourceMappingURL=index.js.map