hls.js 1.5.14-0.canary.10517 → 1.5.14

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