hls.js 1.5.3 → 1.5.5-0.canary.9977

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 (68) hide show
  1. package/README.md +1 -0
  2. package/dist/hls-demo.js +10 -0
  3. package/dist/hls-demo.js.map +1 -1
  4. package/dist/hls.js +1954 -1103
  5. package/dist/hls.js.d.ts +63 -50
  6. package/dist/hls.js.map +1 -1
  7. package/dist/hls.light.js +1631 -784
  8. package/dist/hls.light.js.map +1 -1
  9. package/dist/hls.light.min.js +1 -1
  10. package/dist/hls.light.min.js.map +1 -1
  11. package/dist/hls.light.mjs +1428 -590
  12. package/dist/hls.light.mjs.map +1 -1
  13. package/dist/hls.min.js +1 -1
  14. package/dist/hls.min.js.map +1 -1
  15. package/dist/hls.mjs +1703 -866
  16. package/dist/hls.mjs.map +1 -1
  17. package/dist/hls.worker.js +1 -1
  18. package/dist/hls.worker.js.map +1 -1
  19. package/package.json +18 -18
  20. package/src/config.ts +3 -2
  21. package/src/controller/abr-controller.ts +24 -22
  22. package/src/controller/audio-stream-controller.ts +16 -17
  23. package/src/controller/audio-track-controller.ts +1 -1
  24. package/src/controller/base-playlist-controller.ts +7 -7
  25. package/src/controller/base-stream-controller.ts +56 -29
  26. package/src/controller/buffer-controller.ts +11 -11
  27. package/src/controller/cap-level-controller.ts +1 -2
  28. package/src/controller/cmcd-controller.ts +25 -3
  29. package/src/controller/content-steering-controller.ts +8 -6
  30. package/src/controller/eme-controller.ts +9 -22
  31. package/src/controller/error-controller.ts +6 -8
  32. package/src/controller/fps-controller.ts +2 -3
  33. package/src/controller/gap-controller.ts +43 -16
  34. package/src/controller/latency-controller.ts +9 -11
  35. package/src/controller/level-controller.ts +5 -17
  36. package/src/controller/stream-controller.ts +27 -33
  37. package/src/controller/subtitle-stream-controller.ts +14 -15
  38. package/src/controller/subtitle-track-controller.ts +5 -3
  39. package/src/controller/timeline-controller.ts +23 -30
  40. package/src/crypt/aes-crypto.ts +21 -2
  41. package/src/crypt/decrypter-aes-mode.ts +4 -0
  42. package/src/crypt/decrypter.ts +32 -18
  43. package/src/crypt/fast-aes-key.ts +24 -5
  44. package/src/demux/audio/adts.ts +9 -4
  45. package/src/demux/sample-aes.ts +2 -0
  46. package/src/demux/transmuxer-interface.ts +4 -12
  47. package/src/demux/transmuxer-worker.ts +4 -4
  48. package/src/demux/transmuxer.ts +16 -3
  49. package/src/demux/tsdemuxer.ts +63 -37
  50. package/src/demux/video/avc-video-parser.ts +208 -119
  51. package/src/demux/video/base-video-parser.ts +134 -2
  52. package/src/demux/video/exp-golomb.ts +0 -208
  53. package/src/demux/video/hevc-video-parser.ts +746 -0
  54. package/src/events.ts +7 -0
  55. package/src/hls.ts +42 -34
  56. package/src/loader/fragment-loader.ts +9 -2
  57. package/src/loader/key-loader.ts +2 -0
  58. package/src/loader/level-key.ts +10 -9
  59. package/src/remux/mp4-generator.ts +196 -1
  60. package/src/remux/mp4-remuxer.ts +23 -7
  61. package/src/task-loop.ts +5 -2
  62. package/src/types/component-api.ts +2 -0
  63. package/src/types/demuxer.ts +3 -0
  64. package/src/types/events.ts +4 -0
  65. package/src/utils/codecs.ts +33 -4
  66. package/src/utils/encryption-methods-util.ts +21 -0
  67. package/src/utils/logger.ts +53 -24
  68. package/src/utils/mp4-tools.ts +28 -9
@@ -0,0 +1,746 @@
1
+ import BaseVideoParser from './base-video-parser';
2
+ import { DemuxedVideoTrack, DemuxedUserdataTrack } from '../../types/demuxer';
3
+ import { parseSEIMessageFromNALu } from '../../utils/mp4-tools';
4
+
5
+ import type { PES } from '../tsdemuxer';
6
+
7
+ import ExpGolomb from './exp-golomb';
8
+
9
+ class HevcVideoParser extends BaseVideoParser {
10
+ protected initVPS: Uint8Array | null = null;
11
+
12
+ public parsePES(
13
+ track: DemuxedVideoTrack,
14
+ textTrack: DemuxedUserdataTrack,
15
+ pes: PES,
16
+ last: boolean,
17
+ duration: number,
18
+ ) {
19
+ const units = this.parseNALu(track, pes.data);
20
+ const debug = false;
21
+ let VideoSample = this.VideoSample;
22
+ let push: boolean;
23
+ let spsfound = false;
24
+ // free pes.data to save up some memory
25
+ (pes as any).data = null;
26
+
27
+ // if new NAL units found and last sample still there, let's push ...
28
+ // this helps parsing streams with missing AUD (only do this if AUD never found)
29
+ if (VideoSample && units.length && !track.audFound) {
30
+ this.pushAccessUnit(VideoSample, track);
31
+ VideoSample = this.VideoSample = this.createVideoSample(
32
+ false,
33
+ pes.pts,
34
+ pes.dts,
35
+ '',
36
+ );
37
+ }
38
+
39
+ units.forEach((unit) => {
40
+ switch (unit.type) {
41
+ // NON-IDR, NON RANDOM ACCESS SLICE
42
+ case 0:
43
+ case 1:
44
+ case 2:
45
+ case 3:
46
+ case 4:
47
+ case 5:
48
+ case 6:
49
+ case 7:
50
+ case 8:
51
+ case 9:
52
+ if (!VideoSample) {
53
+ VideoSample = this.VideoSample = this.createVideoSample(
54
+ false,
55
+ pes.pts,
56
+ pes.dts,
57
+ '',
58
+ );
59
+ }
60
+ if (debug) {
61
+ VideoSample.debug += 'NDR ';
62
+ }
63
+ VideoSample.frame = true;
64
+ push = true;
65
+ break;
66
+
67
+ // CRA, BLA (random access picture)
68
+ case 16:
69
+ case 17:
70
+ case 18:
71
+ case 21:
72
+ push = true;
73
+ if (spsfound) {
74
+ // handle PES not starting with AUD
75
+ // if we have frame data already, that cannot belong to the same frame, so force a push
76
+ if (VideoSample?.frame && !VideoSample.key) {
77
+ this.pushAccessUnit(VideoSample, track);
78
+ VideoSample = this.VideoSample = null;
79
+ }
80
+ }
81
+ if (!VideoSample) {
82
+ VideoSample = this.VideoSample = this.createVideoSample(
83
+ true,
84
+ pes.pts,
85
+ pes.dts,
86
+ '',
87
+ );
88
+ }
89
+
90
+ if (debug) {
91
+ VideoSample.debug += 'RAP ';
92
+ }
93
+
94
+ VideoSample.key = true;
95
+ VideoSample.frame = true;
96
+ break;
97
+
98
+ // IDR
99
+ case 19:
100
+ case 20:
101
+ push = true;
102
+ // handle PES not starting with AUD
103
+ // if we have frame data already, that cannot belong to the same frame, so force a push
104
+ if (VideoSample?.frame && !VideoSample.key) {
105
+ this.pushAccessUnit(VideoSample, track);
106
+ VideoSample = this.VideoSample = null;
107
+ }
108
+ if (!VideoSample) {
109
+ VideoSample = this.VideoSample = this.createVideoSample(
110
+ true,
111
+ pes.pts,
112
+ pes.dts,
113
+ '',
114
+ );
115
+ }
116
+
117
+ if (debug) {
118
+ VideoSample.debug += 'IDR ';
119
+ }
120
+ VideoSample.key = true;
121
+ VideoSample.frame = true;
122
+ break;
123
+
124
+ // SEI
125
+ case 39:
126
+ push = true;
127
+ if (debug && VideoSample) {
128
+ VideoSample.debug += 'SEI ';
129
+ }
130
+ parseSEIMessageFromNALu(
131
+ unit.data,
132
+ 2, // NALu header size
133
+ pes.pts as number,
134
+ textTrack.samples,
135
+ );
136
+ break;
137
+
138
+ // VPS
139
+ case 32:
140
+ push = true;
141
+ if (debug && VideoSample) {
142
+ VideoSample.debug += 'VPS ';
143
+ }
144
+ if (!track.vps) {
145
+ const config = this.readVPS(unit.data);
146
+ track.params = { ...config };
147
+ this.initVPS = unit.data;
148
+ }
149
+ track.vps = [unit.data];
150
+ break;
151
+
152
+ // SPS
153
+ case 33:
154
+ push = true;
155
+ spsfound = true;
156
+ if (debug && VideoSample) {
157
+ VideoSample.debug += 'SPS ';
158
+ }
159
+ if (typeof track.params === 'object') {
160
+ if (
161
+ track.vps !== undefined &&
162
+ track.vps[0] !== this.initVPS &&
163
+ track.sps !== undefined &&
164
+ !this.matchSPS(track.sps[0], unit.data)
165
+ ) {
166
+ this.initVPS = track.vps[0];
167
+ track.sps = track.pps = undefined;
168
+ }
169
+ if (!track.sps) {
170
+ const config = this.readSPS(unit.data);
171
+ track.width = config.width;
172
+ track.height = config.height;
173
+ track.pixelRatio = config.pixelRatio;
174
+ track.duration = duration;
175
+ track.codec = config.codecString;
176
+ track.sps = [];
177
+ for (const prop in config.params) {
178
+ track.params[prop] = config.params[prop];
179
+ }
180
+ }
181
+ if (track.vps !== undefined && track.vps[0] === this.initVPS) {
182
+ track.sps.push(unit.data);
183
+ }
184
+ }
185
+ if (!VideoSample) {
186
+ VideoSample = this.VideoSample = this.createVideoSample(
187
+ true,
188
+ pes.pts,
189
+ pes.dts,
190
+ '',
191
+ );
192
+ }
193
+ VideoSample.key = true;
194
+ break;
195
+
196
+ // PPS
197
+ case 34:
198
+ push = true;
199
+ if (debug && VideoSample) {
200
+ VideoSample.debug += 'PPS ';
201
+ }
202
+ if (typeof track.params === 'object') {
203
+ if (!track.pps) {
204
+ track.pps = [];
205
+ const config = this.readPPS(unit.data);
206
+ for (const prop in config) {
207
+ track.params[prop] = config[prop];
208
+ }
209
+ }
210
+ if (this.initVPS !== null || track.pps.length === 0) {
211
+ track.pps.push(unit.data);
212
+ }
213
+ }
214
+ break;
215
+
216
+ // ACCESS UNIT DELIMITER
217
+ case 35:
218
+ push = true;
219
+ track.audFound = true;
220
+ if (VideoSample) {
221
+ this.pushAccessUnit(VideoSample, track);
222
+ }
223
+ VideoSample = this.VideoSample = this.createVideoSample(
224
+ false,
225
+ pes.pts,
226
+ pes.dts,
227
+ debug ? 'AUD ' : '',
228
+ );
229
+ break;
230
+
231
+ default:
232
+ push = false;
233
+ if (VideoSample) {
234
+ VideoSample.debug += 'unknown or irrelevant NAL ' + unit.type + ' ';
235
+ }
236
+ break;
237
+ }
238
+ if (VideoSample && push) {
239
+ const units = VideoSample.units;
240
+ units.push(unit);
241
+ }
242
+ });
243
+ // if last PES packet, push samples
244
+ if (last && VideoSample) {
245
+ this.pushAccessUnit(VideoSample, track);
246
+ this.VideoSample = null;
247
+ }
248
+ }
249
+
250
+ protected getNALuType(data: Uint8Array, offset: number): number {
251
+ return (data[offset] & 0x7e) >>> 1;
252
+ }
253
+
254
+ protected ebsp2rbsp(arr: Uint8Array): Uint8Array {
255
+ const dst = new Uint8Array(arr.byteLength);
256
+ let dstIdx = 0;
257
+ for (let i = 0; i < arr.byteLength; i++) {
258
+ if (i >= 2) {
259
+ // Unescape: Skip 0x03 after 00 00
260
+ if (arr[i] === 0x03 && arr[i - 1] === 0x00 && arr[i - 2] === 0x00) {
261
+ continue;
262
+ }
263
+ }
264
+ dst[dstIdx] = arr[i];
265
+ dstIdx++;
266
+ }
267
+ return new Uint8Array(dst.buffer, 0, dstIdx);
268
+ }
269
+
270
+ readVPS(vps: Uint8Array): {
271
+ numTemporalLayers: number;
272
+ temporalIdNested: boolean;
273
+ } {
274
+ const eg = new ExpGolomb(vps);
275
+ // remove header
276
+ eg.readUByte();
277
+ eg.readUByte();
278
+
279
+ eg.readBits(4); // video_parameter_set_id
280
+ eg.skipBits(2);
281
+ eg.readBits(6); // max_layers_minus1
282
+ const max_sub_layers_minus1 = eg.readBits(3);
283
+ const temporal_id_nesting_flag = eg.readBoolean();
284
+ // ...vui fps can be here, but empty fps value is not critical for metadata
285
+
286
+ return {
287
+ numTemporalLayers: max_sub_layers_minus1 + 1,
288
+ temporalIdNested: temporal_id_nesting_flag,
289
+ };
290
+ }
291
+
292
+ readSPS(sps: Uint8Array): {
293
+ codecString: string;
294
+ params: object;
295
+ width: number;
296
+ height: number;
297
+ pixelRatio: [number, number];
298
+ } {
299
+ const eg = new ExpGolomb(this.ebsp2rbsp(sps));
300
+ eg.readUByte();
301
+ eg.readUByte();
302
+
303
+ eg.readBits(4); //video_parameter_set_id
304
+ const max_sub_layers_minus1 = eg.readBits(3);
305
+ eg.readBoolean(); // temporal_id_nesting_flag
306
+
307
+ // profile_tier_level
308
+ const general_profile_space = eg.readBits(2);
309
+ const general_tier_flag = eg.readBoolean();
310
+ const general_profile_idc = eg.readBits(5);
311
+ const general_profile_compatibility_flags_1 = eg.readUByte();
312
+ const general_profile_compatibility_flags_2 = eg.readUByte();
313
+ const general_profile_compatibility_flags_3 = eg.readUByte();
314
+ const general_profile_compatibility_flags_4 = eg.readUByte();
315
+ const general_constraint_indicator_flags_1 = eg.readUByte();
316
+ const general_constraint_indicator_flags_2 = eg.readUByte();
317
+ const general_constraint_indicator_flags_3 = eg.readUByte();
318
+ const general_constraint_indicator_flags_4 = eg.readUByte();
319
+ const general_constraint_indicator_flags_5 = eg.readUByte();
320
+ const general_constraint_indicator_flags_6 = eg.readUByte();
321
+ const general_level_idc = eg.readUByte();
322
+ const sub_layer_profile_present_flags: boolean[] = [];
323
+ const sub_layer_level_present_flags: boolean[] = [];
324
+ for (let i = 0; i < max_sub_layers_minus1; i++) {
325
+ sub_layer_profile_present_flags.push(eg.readBoolean());
326
+ sub_layer_level_present_flags.push(eg.readBoolean());
327
+ }
328
+ if (max_sub_layers_minus1 > 0) {
329
+ for (let i = max_sub_layers_minus1; i < 8; i++) {
330
+ eg.readBits(2);
331
+ }
332
+ }
333
+ for (let i = 0; i < max_sub_layers_minus1; i++) {
334
+ if (sub_layer_profile_present_flags[i]) {
335
+ eg.readUByte(); // sub_layer_profile_space, sub_layer_tier_flag, sub_layer_profile_idc
336
+ eg.readUByte();
337
+ eg.readUByte();
338
+ eg.readUByte();
339
+ eg.readUByte(); // sub_layer_profile_compatibility_flag
340
+ eg.readUByte();
341
+ eg.readUByte();
342
+ eg.readUByte();
343
+ eg.readUByte();
344
+ eg.readUByte();
345
+ eg.readUByte();
346
+ }
347
+ if (sub_layer_level_present_flags[i]) {
348
+ eg.readUByte();
349
+ }
350
+ }
351
+
352
+ eg.readUEG(); // seq_parameter_set_id
353
+ const chroma_format_idc = eg.readUEG();
354
+ if (chroma_format_idc == 3) {
355
+ eg.skipBits(1); //separate_colour_plane_flag
356
+ }
357
+ const pic_width_in_luma_samples = eg.readUEG();
358
+ const pic_height_in_luma_samples = eg.readUEG();
359
+ const conformance_window_flag = eg.readBoolean();
360
+ let pic_left_offset = 0,
361
+ pic_right_offset = 0,
362
+ pic_top_offset = 0,
363
+ pic_bottom_offset = 0;
364
+ if (conformance_window_flag) {
365
+ pic_left_offset += eg.readUEG();
366
+ pic_right_offset += eg.readUEG();
367
+ pic_top_offset += eg.readUEG();
368
+ pic_bottom_offset += eg.readUEG();
369
+ }
370
+ const bit_depth_luma_minus8 = eg.readUEG();
371
+ const bit_depth_chroma_minus8 = eg.readUEG();
372
+ const log2_max_pic_order_cnt_lsb_minus4 = eg.readUEG();
373
+ const sub_layer_ordering_info_present_flag = eg.readBoolean();
374
+ for (
375
+ let i = sub_layer_ordering_info_present_flag ? 0 : max_sub_layers_minus1;
376
+ i <= max_sub_layers_minus1;
377
+ i++
378
+ ) {
379
+ eg.skipUEG(); // max_dec_pic_buffering_minus1[i]
380
+ eg.skipUEG(); // max_num_reorder_pics[i]
381
+ eg.skipUEG(); // max_latency_increase_plus1[i]
382
+ }
383
+ eg.skipUEG(); // log2_min_luma_coding_block_size_minus3
384
+ eg.skipUEG(); // log2_diff_max_min_luma_coding_block_size
385
+ eg.skipUEG(); // log2_min_transform_block_size_minus2
386
+ eg.skipUEG(); // log2_diff_max_min_transform_block_size
387
+ eg.skipUEG(); // max_transform_hierarchy_depth_inter
388
+ eg.skipUEG(); // max_transform_hierarchy_depth_intra
389
+ const scaling_list_enabled_flag = eg.readBoolean();
390
+ if (scaling_list_enabled_flag) {
391
+ const sps_scaling_list_data_present_flag = eg.readBoolean();
392
+ if (sps_scaling_list_data_present_flag) {
393
+ for (let sizeId = 0; sizeId < 4; sizeId++) {
394
+ for (
395
+ let matrixId = 0;
396
+ matrixId < (sizeId === 3 ? 2 : 6);
397
+ matrixId++
398
+ ) {
399
+ const scaling_list_pred_mode_flag = eg.readBoolean();
400
+ if (!scaling_list_pred_mode_flag) {
401
+ eg.readUEG(); // scaling_list_pred_matrix_id_delta
402
+ } else {
403
+ const coefNum = Math.min(64, 1 << (4 + (sizeId << 1)));
404
+ if (sizeId > 1) {
405
+ eg.readEG();
406
+ }
407
+ for (let i = 0; i < coefNum; i++) {
408
+ eg.readEG();
409
+ }
410
+ }
411
+ }
412
+ }
413
+ }
414
+ }
415
+
416
+ eg.readBoolean(); // amp_enabled_flag
417
+ eg.readBoolean(); // sample_adaptive_offset_enabled_flag
418
+ const pcm_enabled_flag = eg.readBoolean();
419
+ if (pcm_enabled_flag) {
420
+ eg.readUByte();
421
+ eg.skipUEG();
422
+ eg.skipUEG();
423
+ eg.readBoolean();
424
+ }
425
+ const num_short_term_ref_pic_sets = eg.readUEG();
426
+ let num_delta_pocs = 0;
427
+ for (let i = 0; i < num_short_term_ref_pic_sets; i++) {
428
+ let inter_ref_pic_set_prediction_flag = false;
429
+ if (i !== 0) {
430
+ inter_ref_pic_set_prediction_flag = eg.readBoolean();
431
+ }
432
+ if (inter_ref_pic_set_prediction_flag) {
433
+ if (i === num_short_term_ref_pic_sets) {
434
+ eg.readUEG();
435
+ }
436
+ eg.readBoolean();
437
+ eg.readUEG();
438
+ let next_num_delta_pocs = 0;
439
+ for (let j = 0; j <= num_delta_pocs; j++) {
440
+ const used_by_curr_pic_flag = eg.readBoolean();
441
+ let use_delta_flag = false;
442
+ if (!used_by_curr_pic_flag) {
443
+ use_delta_flag = eg.readBoolean();
444
+ }
445
+ if (used_by_curr_pic_flag || use_delta_flag) {
446
+ next_num_delta_pocs++;
447
+ }
448
+ }
449
+ num_delta_pocs = next_num_delta_pocs;
450
+ } else {
451
+ const num_negative_pics = eg.readUEG();
452
+ const num_positive_pics = eg.readUEG();
453
+ num_delta_pocs = num_negative_pics + num_positive_pics;
454
+ for (let j = 0; j < num_negative_pics; j++) {
455
+ eg.readUEG();
456
+ eg.readBoolean();
457
+ }
458
+ for (let j = 0; j < num_positive_pics; j++) {
459
+ eg.readUEG();
460
+ eg.readBoolean();
461
+ }
462
+ }
463
+ }
464
+
465
+ const long_term_ref_pics_present_flag = eg.readBoolean();
466
+ if (long_term_ref_pics_present_flag) {
467
+ const num_long_term_ref_pics_sps = eg.readUEG();
468
+ for (let i = 0; i < num_long_term_ref_pics_sps; i++) {
469
+ for (let j = 0; j < log2_max_pic_order_cnt_lsb_minus4 + 4; j++) {
470
+ eg.readBits(1);
471
+ }
472
+ eg.readBits(1);
473
+ }
474
+ }
475
+
476
+ let min_spatial_segmentation_idc = 0;
477
+ let sar_width = 1,
478
+ sar_height = 1;
479
+ let fps_fixed = true,
480
+ fps_den = 1,
481
+ fps_num = 0;
482
+ eg.readBoolean(); // sps_temporal_mvp_enabled_flag
483
+ eg.readBoolean(); // strong_intra_smoothing_enabled_flag
484
+ let default_display_window_flag = false;
485
+ const vui_parameters_present_flag = eg.readBoolean();
486
+ if (vui_parameters_present_flag) {
487
+ const aspect_ratio_info_present_flag = eg.readBoolean();
488
+ if (aspect_ratio_info_present_flag) {
489
+ const aspect_ratio_idc = eg.readUByte();
490
+ const sar_width_table = [
491
+ 1, 12, 10, 16, 40, 24, 20, 32, 80, 18, 15, 64, 160, 4, 3, 2,
492
+ ];
493
+ const sar_height_table = [
494
+ 1, 11, 11, 11, 33, 11, 11, 11, 33, 11, 11, 33, 99, 3, 2, 1,
495
+ ];
496
+ if (aspect_ratio_idc > 0 && aspect_ratio_idc < 16) {
497
+ sar_width = sar_width_table[aspect_ratio_idc - 1];
498
+ sar_height = sar_height_table[aspect_ratio_idc - 1];
499
+ } else if (aspect_ratio_idc === 255) {
500
+ sar_width = eg.readBits(16);
501
+ sar_height = eg.readBits(16);
502
+ }
503
+ }
504
+ const overscan_info_present_flag = eg.readBoolean();
505
+ if (overscan_info_present_flag) {
506
+ eg.readBoolean();
507
+ }
508
+ const video_signal_type_present_flag = eg.readBoolean();
509
+ if (video_signal_type_present_flag) {
510
+ eg.readBits(3);
511
+ eg.readBoolean();
512
+ const colour_description_present_flag = eg.readBoolean();
513
+ if (colour_description_present_flag) {
514
+ eg.readUByte();
515
+ eg.readUByte();
516
+ eg.readUByte();
517
+ }
518
+ }
519
+ const chroma_loc_info_present_flag = eg.readBoolean();
520
+ if (chroma_loc_info_present_flag) {
521
+ eg.readUEG();
522
+ eg.readUEG();
523
+ }
524
+ eg.readBoolean(); // neutral_chroma_indication_flag
525
+ eg.readBoolean(); // field_seq_flag
526
+ eg.readBoolean(); // frame_field_info_present_flag
527
+ default_display_window_flag = eg.readBoolean();
528
+ if (default_display_window_flag) {
529
+ pic_left_offset += eg.readUEG();
530
+ pic_right_offset += eg.readUEG();
531
+ pic_top_offset += eg.readUEG();
532
+ pic_bottom_offset += eg.readUEG();
533
+ }
534
+ const vui_timing_info_present_flag = eg.readBoolean();
535
+ if (vui_timing_info_present_flag) {
536
+ fps_den = eg.readBits(32);
537
+ fps_num = eg.readBits(32);
538
+ const vui_poc_proportional_to_timing_flag = eg.readBoolean();
539
+ if (vui_poc_proportional_to_timing_flag) {
540
+ eg.readUEG();
541
+ }
542
+ const vui_hrd_parameters_present_flag = eg.readBoolean();
543
+ if (vui_hrd_parameters_present_flag) {
544
+ //const commonInfPresentFlag = true;
545
+ //if (commonInfPresentFlag) {
546
+ const nal_hrd_parameters_present_flag = eg.readBoolean();
547
+ const vcl_hrd_parameters_present_flag = eg.readBoolean();
548
+ let sub_pic_hrd_params_present_flag = false;
549
+ if (
550
+ nal_hrd_parameters_present_flag ||
551
+ vcl_hrd_parameters_present_flag
552
+ ) {
553
+ sub_pic_hrd_params_present_flag = eg.readBoolean();
554
+ if (sub_pic_hrd_params_present_flag) {
555
+ eg.readUByte();
556
+ eg.readBits(5);
557
+ eg.readBoolean();
558
+ eg.readBits(5);
559
+ }
560
+ eg.readBits(4); // bit_rate_scale
561
+ eg.readBits(4); // cpb_size_scale
562
+ if (sub_pic_hrd_params_present_flag) {
563
+ eg.readBits(4);
564
+ }
565
+ eg.readBits(5);
566
+ eg.readBits(5);
567
+ eg.readBits(5);
568
+ }
569
+ //}
570
+ for (let i = 0; i <= max_sub_layers_minus1; i++) {
571
+ fps_fixed = eg.readBoolean(); // fixed_pic_rate_general_flag
572
+ const fixed_pic_rate_within_cvs_flag =
573
+ fps_fixed || eg.readBoolean();
574
+ let low_delay_hrd_flag = false;
575
+ if (fixed_pic_rate_within_cvs_flag) {
576
+ eg.readEG();
577
+ } else {
578
+ low_delay_hrd_flag = eg.readBoolean();
579
+ }
580
+ const cpb_cnt = low_delay_hrd_flag ? 1 : eg.readUEG() + 1;
581
+ if (nal_hrd_parameters_present_flag) {
582
+ for (let j = 0; j < cpb_cnt; j++) {
583
+ eg.readUEG();
584
+ eg.readUEG();
585
+ if (sub_pic_hrd_params_present_flag) {
586
+ eg.readUEG();
587
+ eg.readUEG();
588
+ }
589
+ eg.skipBits(1);
590
+ }
591
+ }
592
+ if (vcl_hrd_parameters_present_flag) {
593
+ for (let j = 0; j < cpb_cnt; j++) {
594
+ eg.readUEG();
595
+ eg.readUEG();
596
+ if (sub_pic_hrd_params_present_flag) {
597
+ eg.readUEG();
598
+ eg.readUEG();
599
+ }
600
+ eg.skipBits(1);
601
+ }
602
+ }
603
+ }
604
+ }
605
+ }
606
+ const bitstream_restriction_flag = eg.readBoolean();
607
+ if (bitstream_restriction_flag) {
608
+ eg.readBoolean(); // tiles_fixed_structure_flag
609
+ eg.readBoolean(); // motion_vectors_over_pic_boundaries_flag
610
+ eg.readBoolean(); // restricted_ref_pic_lists_flag
611
+ min_spatial_segmentation_idc = eg.readUEG();
612
+ }
613
+ }
614
+
615
+ let width = pic_width_in_luma_samples,
616
+ height = pic_height_in_luma_samples;
617
+ if (conformance_window_flag || default_display_window_flag) {
618
+ let chroma_scale_w = 1,
619
+ chroma_scale_h = 1;
620
+ if (chroma_format_idc === 1) {
621
+ // YUV 420
622
+ chroma_scale_w = chroma_scale_h = 2;
623
+ } else if (chroma_format_idc == 2) {
624
+ // YUV 422
625
+ chroma_scale_w = 2;
626
+ }
627
+ width =
628
+ pic_width_in_luma_samples -
629
+ chroma_scale_w * pic_right_offset -
630
+ chroma_scale_w * pic_left_offset;
631
+ height =
632
+ pic_height_in_luma_samples -
633
+ chroma_scale_h * pic_bottom_offset -
634
+ chroma_scale_h * pic_top_offset;
635
+ }
636
+
637
+ const profile_space_string = general_profile_space
638
+ ? ['A', 'B', 'C'][general_profile_space]
639
+ : '';
640
+ const profile_compatibility_buf =
641
+ (general_profile_compatibility_flags_1 << 24) |
642
+ (general_profile_compatibility_flags_2 << 16) |
643
+ (general_profile_compatibility_flags_3 << 8) |
644
+ general_profile_compatibility_flags_4;
645
+ let profile_compatibility_rev = 0;
646
+ for (let i = 0; i < 32; i++) {
647
+ profile_compatibility_rev =
648
+ (profile_compatibility_rev |
649
+ (((profile_compatibility_buf >> i) & 1) << (31 - i))) >>>
650
+ 0; // reverse bit position (and cast as UInt32)
651
+ }
652
+ let profile_compatibility_flags_string =
653
+ profile_compatibility_rev.toString(16);
654
+ if (
655
+ general_profile_idc === 1 &&
656
+ profile_compatibility_flags_string === '2'
657
+ ) {
658
+ profile_compatibility_flags_string = '6';
659
+ }
660
+ const tier_flag_string = general_tier_flag ? 'H' : 'L';
661
+
662
+ return {
663
+ codecString: `hvc1.${profile_space_string}${general_profile_idc}.${profile_compatibility_flags_string}.${tier_flag_string}${general_level_idc}.B0`,
664
+ params: {
665
+ general_tier_flag,
666
+ general_profile_idc,
667
+ general_profile_space,
668
+ general_profile_compatibility_flags: [
669
+ general_profile_compatibility_flags_1,
670
+ general_profile_compatibility_flags_2,
671
+ general_profile_compatibility_flags_3,
672
+ general_profile_compatibility_flags_4,
673
+ ],
674
+ general_constraint_indicator_flags: [
675
+ general_constraint_indicator_flags_1,
676
+ general_constraint_indicator_flags_2,
677
+ general_constraint_indicator_flags_3,
678
+ general_constraint_indicator_flags_4,
679
+ general_constraint_indicator_flags_5,
680
+ general_constraint_indicator_flags_6,
681
+ ],
682
+ general_level_idc,
683
+ bit_depth: bit_depth_luma_minus8 + 8,
684
+ bit_depth_luma_minus8,
685
+ bit_depth_chroma_minus8,
686
+ min_spatial_segmentation_idc,
687
+ chroma_format_idc: chroma_format_idc,
688
+ frame_rate: {
689
+ fixed: fps_fixed,
690
+ fps: fps_num / fps_den,
691
+ },
692
+ },
693
+ width,
694
+ height,
695
+ pixelRatio: [sar_width, sar_height],
696
+ };
697
+ }
698
+
699
+ readPPS(pps: Uint8Array): {
700
+ parallelismType: number;
701
+ } {
702
+ const eg = new ExpGolomb(this.ebsp2rbsp(pps));
703
+ eg.readUByte();
704
+ eg.readUByte();
705
+ eg.skipUEG(); // pic_parameter_set_id
706
+ eg.skipUEG(); // seq_parameter_set_id
707
+ eg.skipBits(2); // dependent_slice_segments_enabled_flag, output_flag_present_flag
708
+ eg.skipBits(3); // num_extra_slice_header_bits
709
+ eg.skipBits(2); // sign_data_hiding_enabled_flag, cabac_init_present_flag
710
+ eg.skipUEG();
711
+ eg.skipUEG();
712
+ eg.skipEG(); // init_qp_minus26
713
+ eg.skipBits(2); // constrained_intra_pred_flag, transform_skip_enabled_flag
714
+ const cu_qp_delta_enabled_flag = eg.readBoolean();
715
+ if (cu_qp_delta_enabled_flag) {
716
+ eg.skipUEG();
717
+ }
718
+ eg.skipEG(); // cb_qp_offset
719
+ eg.skipEG(); // cr_qp_offset
720
+ eg.skipBits(4); // pps_slice_chroma_qp_offsets_present_flag, weighted_pred_flag, weighted_bipred_flag, transquant_bypass_enabled_flag
721
+ const tiles_enabled_flag = eg.readBoolean();
722
+ const entropy_coding_sync_enabled_flag = eg.readBoolean();
723
+ let parallelismType = 1; // slice-based parallel decoding
724
+ if (entropy_coding_sync_enabled_flag && tiles_enabled_flag) {
725
+ parallelismType = 0; // mixed-type parallel decoding
726
+ } else if (entropy_coding_sync_enabled_flag) {
727
+ parallelismType = 3; // wavefront-based parallel decoding
728
+ } else if (tiles_enabled_flag) {
729
+ parallelismType = 2; // tile-based parallel decoding
730
+ }
731
+
732
+ return {
733
+ parallelismType,
734
+ };
735
+ }
736
+
737
+ matchSPS(sps1: Uint8Array, sps2: Uint8Array): boolean {
738
+ // compare without headers and VPS related params
739
+ return (
740
+ String.fromCharCode.apply(null, sps1).substr(3) ===
741
+ String.fromCharCode.apply(null, sps2).substr(3)
742
+ );
743
+ }
744
+ }
745
+
746
+ export default HevcVideoParser;