werift 0.18.13 → 0.18.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 (94) hide show
  1. package/lib/common/src/index.d.ts +0 -1
  2. package/lib/common/src/index.js +0 -1
  3. package/lib/common/src/index.js.map +1 -1
  4. package/lib/rtp/src/container/index.d.ts +2 -1
  5. package/lib/rtp/src/container/index.js +2 -1
  6. package/lib/rtp/src/container/index.js.map +1 -1
  7. package/lib/rtp/src/container/mp4/container.d.ts +66 -0
  8. package/lib/rtp/src/container/mp4/container.js +262 -0
  9. package/lib/rtp/src/container/mp4/container.js.map +1 -0
  10. package/lib/rtp/src/container/mp4/exp-golomb.d.ts +20 -0
  11. package/lib/rtp/src/container/mp4/exp-golomb.js +144 -0
  12. package/lib/rtp/src/container/mp4/exp-golomb.js.map +1 -0
  13. package/lib/rtp/src/container/mp4/h264.d.ts +42 -0
  14. package/lib/rtp/src/container/mp4/h264.js +240 -0
  15. package/lib/rtp/src/container/mp4/h264.js.map +1 -0
  16. package/lib/rtp/src/container/mp4/index.d.ts +2 -0
  17. package/lib/rtp/src/container/mp4/index.js +19 -0
  18. package/lib/rtp/src/container/mp4/index.js.map +1 -0
  19. package/lib/rtp/src/container/mp4/mp4box.d.ts +5 -0
  20. package/lib/rtp/src/container/mp4/mp4box.js +32 -0
  21. package/lib/rtp/src/container/mp4/mp4box.js.map +1 -0
  22. package/lib/rtp/src/container/mp4/sps-parser.d.ts +40 -0
  23. package/lib/rtp/src/container/mp4/sps-parser.js +291 -0
  24. package/lib/rtp/src/container/mp4/sps-parser.js.map +1 -0
  25. package/lib/rtp/src/container/ogg/index.d.ts +1 -0
  26. package/lib/rtp/src/container/ogg/index.js +18 -0
  27. package/lib/rtp/src/container/ogg/index.js.map +1 -0
  28. package/lib/rtp/src/container/ogg/parser.d.ts +12 -0
  29. package/lib/rtp/src/container/ogg/parser.js +100 -0
  30. package/lib/rtp/src/container/ogg/parser.js.map +1 -0
  31. package/lib/rtp/src/container/{webm.d.ts → webm/container.d.ts} +1 -1
  32. package/lib/rtp/src/container/{webm.js → webm/container.js} +6 -6
  33. package/lib/rtp/src/container/webm/container.js.map +1 -0
  34. package/lib/rtp/src/container/webm/ebml/ebml.js.map +1 -0
  35. package/lib/rtp/src/container/webm/ebml/id.js.map +1 -0
  36. package/lib/rtp/src/container/webm/ebml/index.js.map +1 -0
  37. package/lib/rtp/src/container/webm/ebml/typedArrayUtils.js.map +1 -0
  38. package/lib/rtp/src/container/webm/index.d.ts +2 -0
  39. package/lib/rtp/src/container/webm/index.js +19 -0
  40. package/lib/rtp/src/container/webm/index.js.map +1 -0
  41. package/lib/rtp/src/processor/index.d.ts +2 -0
  42. package/lib/rtp/src/processor/index.js +2 -0
  43. package/lib/rtp/src/processor/index.js.map +1 -1
  44. package/lib/rtp/src/processor/mp4.d.ts +50 -0
  45. package/lib/rtp/src/processor/mp4.js +162 -0
  46. package/lib/rtp/src/processor/mp4.js.map +1 -0
  47. package/lib/rtp/src/processor/mp4Callback.d.ts +12 -0
  48. package/lib/rtp/src/processor/mp4Callback.js +83 -0
  49. package/lib/rtp/src/processor/mp4Callback.js.map +1 -0
  50. package/lib/rtp/src/processor/mute.js +6 -3
  51. package/lib/rtp/src/processor/mute.js.map +1 -1
  52. package/lib/rtp/src/processor/nack.js +2 -3
  53. package/lib/rtp/src/processor/nack.js.map +1 -1
  54. package/lib/rtp/src/processor/webm.d.ts +1 -1
  55. package/lib/rtp/src/processor/webm.js +1 -1
  56. package/lib/rtp/src/processor/webm.js.map +1 -1
  57. package/lib/rtp/src/processor/webmCallback.d.ts +1 -1
  58. package/lib/rtp/src/processor/webmCallback.js.map +1 -1
  59. package/lib/rtp/src/processor/webmStream.d.ts +1 -1
  60. package/lib/rtp/src/processor/webmStream.js.map +1 -1
  61. package/lib/rtp/src/rtcp/rr.js +2 -6
  62. package/lib/rtp/src/rtcp/rr.js.map +1 -1
  63. package/lib/rtp/src/rtcp/rtpfb/nack.js +5 -12
  64. package/lib/rtp/src/rtcp/rtpfb/nack.js.map +1 -1
  65. package/lib/rtp/src/rtcp/rtpfb/twcc.js +11 -7
  66. package/lib/rtp/src/rtcp/rtpfb/twcc.js.map +1 -1
  67. package/lib/rtp/src/rtcp/sr.js +1 -5
  68. package/lib/rtp/src/rtcp/sr.js.map +1 -1
  69. package/lib/rtp/src/rtp/headerExtension.d.ts +5 -0
  70. package/lib/rtp/src/rtp/headerExtension.js +35 -9
  71. package/lib/rtp/src/rtp/headerExtension.js.map +1 -1
  72. package/lib/webrtc/src/nonstandard/recorder/writer/webm.js.map +1 -1
  73. package/lib/webrtc/src/utils.d.ts +1 -0
  74. package/lib/webrtc/src/utils.js +9 -1
  75. package/lib/webrtc/src/utils.js.map +1 -1
  76. package/package.json +1 -1
  77. package/src/nonstandard/recorder/writer/webm.ts +1 -1
  78. package/src/utils.ts +9 -0
  79. package/lib/common/src/array.d.ts +0 -1
  80. package/lib/common/src/array.js +0 -15
  81. package/lib/common/src/array.js.map +0 -1
  82. package/lib/rtp/src/container/ebml/ebml.js.map +0 -1
  83. package/lib/rtp/src/container/ebml/id.js.map +0 -1
  84. package/lib/rtp/src/container/ebml/index.js.map +0 -1
  85. package/lib/rtp/src/container/ebml/typedArrayUtils.js.map +0 -1
  86. package/lib/rtp/src/container/webm.js.map +0 -1
  87. /package/lib/rtp/src/container/{ebml → webm/ebml}/ebml.d.ts +0 -0
  88. /package/lib/rtp/src/container/{ebml → webm/ebml}/ebml.js +0 -0
  89. /package/lib/rtp/src/container/{ebml → webm/ebml}/id.d.ts +0 -0
  90. /package/lib/rtp/src/container/{ebml → webm/ebml}/id.js +0 -0
  91. /package/lib/rtp/src/container/{ebml → webm/ebml}/index.d.ts +0 -0
  92. /package/lib/rtp/src/container/{ebml → webm/ebml}/index.js +0 -0
  93. /package/lib/rtp/src/container/{ebml → webm/ebml}/typedArrayUtils.d.ts +0 -0
  94. /package/lib/rtp/src/container/{ebml → webm/ebml}/typedArrayUtils.js +0 -0
@@ -0,0 +1,240 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.annexb2avcc = exports.AVCDecoderConfigurationRecord = exports.H264AnnexBParser = exports.H264NaluAVC1 = exports.H264NaluPayload = exports.H264NaluType = void 0;
7
+ const sps_parser_1 = __importDefault(require("./sps-parser"));
8
+ var H264NaluType;
9
+ (function (H264NaluType) {
10
+ H264NaluType[H264NaluType["kUnspecified"] = 0] = "kUnspecified";
11
+ H264NaluType[H264NaluType["kSliceNonIDR"] = 1] = "kSliceNonIDR";
12
+ H264NaluType[H264NaluType["kSliceDPA"] = 2] = "kSliceDPA";
13
+ H264NaluType[H264NaluType["kSliceDPB"] = 3] = "kSliceDPB";
14
+ H264NaluType[H264NaluType["kSliceDPC"] = 4] = "kSliceDPC";
15
+ H264NaluType[H264NaluType["kSliceIDR"] = 5] = "kSliceIDR";
16
+ H264NaluType[H264NaluType["kSliceSEI"] = 6] = "kSliceSEI";
17
+ H264NaluType[H264NaluType["kSliceSPS"] = 7] = "kSliceSPS";
18
+ H264NaluType[H264NaluType["kSlicePPS"] = 8] = "kSlicePPS";
19
+ H264NaluType[H264NaluType["kSliceAUD"] = 9] = "kSliceAUD";
20
+ H264NaluType[H264NaluType["kEndOfSequence"] = 10] = "kEndOfSequence";
21
+ H264NaluType[H264NaluType["kEndOfStream"] = 11] = "kEndOfStream";
22
+ H264NaluType[H264NaluType["kFiller"] = 12] = "kFiller";
23
+ H264NaluType[H264NaluType["kSPSExt"] = 13] = "kSPSExt";
24
+ H264NaluType[H264NaluType["kReserved0"] = 14] = "kReserved0";
25
+ })(H264NaluType = exports.H264NaluType || (exports.H264NaluType = {}));
26
+ class H264NaluPayload {
27
+ constructor() {
28
+ Object.defineProperty(this, "type", {
29
+ enumerable: true,
30
+ configurable: true,
31
+ writable: true,
32
+ value: void 0
33
+ });
34
+ Object.defineProperty(this, "data", {
35
+ enumerable: true,
36
+ configurable: true,
37
+ writable: true,
38
+ value: void 0
39
+ });
40
+ }
41
+ }
42
+ exports.H264NaluPayload = H264NaluPayload;
43
+ class H264NaluAVC1 {
44
+ constructor(nalu) {
45
+ Object.defineProperty(this, "type", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: void 0
50
+ });
51
+ Object.defineProperty(this, "data", {
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ value: void 0
56
+ });
57
+ const nalu_size = nalu.data.byteLength;
58
+ this.type = nalu.type;
59
+ this.data = new Uint8Array(4 + nalu_size); // 4 byte length-header + nalu payload
60
+ const v = new DataView(this.data.buffer);
61
+ // Fill 4 byte length-header
62
+ v.setUint32(0, nalu_size);
63
+ // Copy payload
64
+ this.data.set(nalu.data, 4);
65
+ }
66
+ }
67
+ exports.H264NaluAVC1 = H264NaluAVC1;
68
+ class H264AnnexBParser {
69
+ constructor(data) {
70
+ Object.defineProperty(this, "TAG", {
71
+ enumerable: true,
72
+ configurable: true,
73
+ writable: true,
74
+ value: "H264AnnexBParser"
75
+ });
76
+ Object.defineProperty(this, "data_", {
77
+ enumerable: true,
78
+ configurable: true,
79
+ writable: true,
80
+ value: void 0
81
+ });
82
+ Object.defineProperty(this, "current_startcode_offset_", {
83
+ enumerable: true,
84
+ configurable: true,
85
+ writable: true,
86
+ value: 0
87
+ });
88
+ Object.defineProperty(this, "eof_flag_", {
89
+ enumerable: true,
90
+ configurable: true,
91
+ writable: true,
92
+ value: false
93
+ });
94
+ this.data_ = data;
95
+ this.current_startcode_offset_ = this.findNextStartCodeOffset(0);
96
+ }
97
+ findNextStartCodeOffset(start_offset) {
98
+ let i = start_offset;
99
+ const data = this.data_;
100
+ for (;;) {
101
+ if (i + 3 >= data.byteLength) {
102
+ this.eof_flag_ = true;
103
+ return data.byteLength;
104
+ }
105
+ // search 00 00 00 01 or 00 00 01
106
+ const uint32 = (data[i + 0] << 24) |
107
+ (data[i + 1] << 16) |
108
+ (data[i + 2] << 8) |
109
+ data[i + 3];
110
+ const uint24 = (data[i + 0] << 16) | (data[i + 1] << 8) | data[i + 2];
111
+ if (uint32 === 0x00000001 || uint24 === 0x000001) {
112
+ return i;
113
+ }
114
+ else {
115
+ i++;
116
+ }
117
+ }
118
+ }
119
+ readNextNaluPayload() {
120
+ const data = this.data_;
121
+ let nalu_payload = null;
122
+ while (nalu_payload == null) {
123
+ if (this.eof_flag_) {
124
+ break;
125
+ }
126
+ // offset pointed to start code
127
+ const startcode_offset = this.current_startcode_offset_;
128
+ // nalu payload start offset
129
+ let offset = startcode_offset;
130
+ const u32 = (data[offset] << 24) |
131
+ (data[offset + 1] << 16) |
132
+ (data[offset + 2] << 8) |
133
+ data[offset + 3];
134
+ if (u32 === 0x00000001) {
135
+ offset += 4;
136
+ }
137
+ else {
138
+ offset += 3;
139
+ }
140
+ const nalu_type = data[offset] & 0x1f;
141
+ const forbidden_bit = (data[offset] & 0x80) >>> 7;
142
+ const next_startcode_offset = this.findNextStartCodeOffset(offset);
143
+ this.current_startcode_offset_ = next_startcode_offset;
144
+ if (nalu_type >= H264NaluType.kReserved0) {
145
+ continue;
146
+ }
147
+ if (forbidden_bit !== 0) {
148
+ // Log.e(this.TAG, `forbidden_bit near offset ${offset} should be 0 but has value ${forbidden_bit}`);
149
+ continue;
150
+ }
151
+ const payload_data = data.subarray(offset, next_startcode_offset);
152
+ nalu_payload = new H264NaluPayload();
153
+ nalu_payload.type = nalu_type;
154
+ nalu_payload.data = payload_data;
155
+ }
156
+ return nalu_payload;
157
+ }
158
+ }
159
+ exports.H264AnnexBParser = H264AnnexBParser;
160
+ class AVCDecoderConfigurationRecord {
161
+ // sps, pps: require Nalu without 4 byte length-header
162
+ constructor(sps, pps, sps_details) {
163
+ Object.defineProperty(this, "data", {
164
+ enumerable: true,
165
+ configurable: true,
166
+ writable: true,
167
+ value: void 0
168
+ });
169
+ let length = 6 + 2 + sps.byteLength + 1 + 2 + pps.byteLength;
170
+ let need_extra_fields = false;
171
+ if (sps[3] !== 66 && sps[3] !== 77 && sps[3] !== 88) {
172
+ need_extra_fields = true;
173
+ length += 4;
174
+ }
175
+ const data = (this.data = new Uint8Array(length));
176
+ data[0] = 0x01; // configurationVersion
177
+ data[1] = sps[1]; // AVCProfileIndication
178
+ data[2] = sps[2]; // profile_compatibility
179
+ data[3] = sps[3]; // AVCLevelIndication
180
+ data[4] = 0xff; // 111111 + lengthSizeMinusOne(3)
181
+ data[5] = 0xe0 | 0x01; // 111 + numOfSequenceParameterSets
182
+ const sps_length = sps.byteLength;
183
+ data[6] = sps_length >>> 8; // sequenceParameterSetLength
184
+ data[7] = sps_length & 0xff;
185
+ let offset = 8;
186
+ data.set(sps, 8);
187
+ offset += sps_length;
188
+ data[offset] = 1; // numOfPictureParameterSets
189
+ const pps_length = pps.byteLength;
190
+ data[offset + 1] = pps_length >>> 8; // pictureParameterSetLength
191
+ data[offset + 2] = pps_length & 0xff;
192
+ data.set(pps, offset + 3);
193
+ offset += 3 + pps_length;
194
+ if (need_extra_fields) {
195
+ data[offset] = 0xfc | sps_details.chroma_format_idc;
196
+ data[offset + 1] = 0xf8 | (sps_details.bit_depth_luma - 8);
197
+ data[offset + 2] = 0xf8 | (sps_details.bit_depth_chroma - 8);
198
+ data[offset + 3] = 0x00; // number of sps ext
199
+ offset += 4;
200
+ }
201
+ }
202
+ getData() {
203
+ return this.data;
204
+ }
205
+ }
206
+ exports.AVCDecoderConfigurationRecord = AVCDecoderConfigurationRecord;
207
+ function annexb2avcc(data) {
208
+ const annexb_parser = new H264AnnexBParser(data);
209
+ let nalu_payload = null;
210
+ const video_init_segment_dispatched_ = false;
211
+ const video_metadata_changed_ = false;
212
+ const video_metadata_ = {
213
+ sps: undefined,
214
+ pps: undefined,
215
+ details: undefined,
216
+ };
217
+ while ((nalu_payload = annexb_parser.readNextNaluPayload()) != null) {
218
+ const nalu_avc1 = new H264NaluAVC1(nalu_payload);
219
+ if (nalu_avc1.type === H264NaluType.kSliceSPS) {
220
+ // Notice: parseSPS requires Nalu without startcode or length-header
221
+ const details = sps_parser_1.default.parseSPS(nalu_payload.data);
222
+ if (!video_init_segment_dispatched_) {
223
+ video_metadata_.sps = nalu_avc1;
224
+ video_metadata_.details = details;
225
+ }
226
+ }
227
+ else if (nalu_avc1.type === H264NaluType.kSlicePPS) {
228
+ if (!video_init_segment_dispatched_ || video_metadata_changed_) {
229
+ video_metadata_.pps = nalu_avc1;
230
+ }
231
+ }
232
+ }
233
+ const sps_without_header = video_metadata_.sps.data.subarray(4);
234
+ const pps_without_header = video_metadata_.pps.data.subarray(4);
235
+ const details = video_metadata_.details;
236
+ const avcc = new AVCDecoderConfigurationRecord(sps_without_header, pps_without_header, details);
237
+ return avcc.getData();
238
+ }
239
+ exports.annexb2avcc = annexb2avcc;
240
+ //# sourceMappingURL=h264.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"h264.js","sourceRoot":"","sources":["../../../../../../rtp/src/container/mp4/h264.ts"],"names":[],"mappings":";;;;;;AAAA,8DAAqC;AAErC,IAAY,YAgBX;AAhBD,WAAY,YAAY;IACtB,+DAAgB,CAAA;IAChB,+DAAY,CAAA;IACZ,yDAAS,CAAA;IACT,yDAAS,CAAA;IACT,yDAAS,CAAA;IACT,yDAAS,CAAA;IACT,yDAAS,CAAA;IACT,yDAAS,CAAA;IACT,yDAAS,CAAA;IACT,yDAAS,CAAA;IACT,oEAAc,CAAA;IACd,gEAAY,CAAA;IACZ,sDAAO,CAAA;IACP,sDAAO,CAAA;IACP,4DAAU,CAAA;AACZ,CAAC,EAhBW,YAAY,GAAZ,oBAAY,KAAZ,oBAAY,QAgBvB;AAED,MAAa,eAAe;IAA5B;QACE;;;;;WAAoB;QACpB;;;;;WAAkB;IACpB,CAAC;CAAA;AAHD,0CAGC;AAED,MAAa,YAAY;IAIvB,YAAY,IAAqB;QAHjC;;;;;WAAmB;QACnB;;;;;WAAiB;QAGf,MAAM,SAAS,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;IAED,MAAM,kBAAkB,GAAG,eAAe,CAAC,GAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IACjE,MAAM,kBAAkB,GAAG,eAAe,CAAC,GAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IACjE,MAAM,OAAO,GAAG,eAAe,CAAC,OAAO,CAAC;IACxC,MAAM,IAAI,GAAG,IAAI,6BAA6B,CAC5C,kBAAkB,EAClB,kBAAkB,EAClB,OAAO,CACR,CAAC;IACF,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC;AACxB,CAAC;AA1CD,kCA0CC","sourcesContent":["import SPSParser from \"./sps-parser\";\n\nexport enum H264NaluType {\n kUnspecified = 0,\n kSliceNonIDR,\n kSliceDPA,\n kSliceDPB,\n kSliceDPC,\n kSliceIDR,\n kSliceSEI,\n kSliceSPS,\n kSlicePPS,\n kSliceAUD,\n kEndOfSequence,\n kEndOfStream,\n kFiller,\n kSPSExt,\n kReserved0,\n}\n\nexport class H264NaluPayload {\n type!: H264NaluType;\n data!: Uint8Array;\n}\n\nexport class H264NaluAVC1 {\n type: H264NaluType;\n data: Uint8Array;\n\n constructor(nalu: H264NaluPayload) {\n const nalu_size = nalu.data.byteLength;\n\n this.type = nalu.type;\n this.data = new Uint8Array(4 + nalu_size); // 4 byte length-header + nalu payload\n\n const v = new DataView(this.data.buffer);\n // Fill 4 byte length-header\n v.setUint32(0, nalu_size);\n // Copy payload\n this.data.set(nalu.data, 4);\n }\n}\n\nexport class H264AnnexBParser {\n private readonly TAG: string = \"H264AnnexBParser\";\n\n private data_: Uint8Array;\n private current_startcode_offset_: number = 0;\n private eof_flag_: boolean = false;\n\n public constructor(data: Uint8Array) {\n this.data_ = data;\n this.current_startcode_offset_ = this.findNextStartCodeOffset(0);\n }\n\n private findNextStartCodeOffset(start_offset: number) {\n let i = start_offset;\n const data = this.data_;\n\n for (;;) {\n if (i + 3 >= data.byteLength) {\n this.eof_flag_ = true;\n return data.byteLength;\n }\n\n // search 00 00 00 01 or 00 00 01\n const uint32 =\n (data[i + 0] << 24) |\n (data[i + 1] << 16) |\n (data[i + 2] << 8) |\n data[i + 3];\n const uint24 = (data[i + 0] << 16) | (data[i + 1] << 8) | data[i + 2];\n if (uint32 === 0x00000001 || uint24 === 0x000001) {\n return i;\n } else {\n i++;\n }\n }\n }\n\n public readNextNaluPayload(): H264NaluPayload | null {\n const data = this.data_;\n let nalu_payload: H264NaluPayload | null = null;\n\n while (nalu_payload == null) {\n if (this.eof_flag_) {\n break;\n }\n // offset pointed to start code\n const startcode_offset = this.current_startcode_offset_;\n\n // nalu payload start offset\n let offset = startcode_offset;\n const u32 =\n (data[offset] << 24) |\n (data[offset + 1] << 16) |\n (data[offset + 2] << 8) |\n data[offset + 3];\n if (u32 === 0x00000001) {\n offset += 4;\n } else {\n offset += 3;\n }\n\n const nalu_type: H264NaluType = data[offset] & 0x1f;\n const forbidden_bit = (data[offset] & 0x80) >>> 7;\n\n const next_startcode_offset = this.findNextStartCodeOffset(offset);\n this.current_startcode_offset_ = next_startcode_offset;\n\n if (nalu_type >= H264NaluType.kReserved0) {\n continue;\n }\n if (forbidden_bit !== 0) {\n // Log.e(this.TAG, `forbidden_bit near offset ${offset} should be 0 but has value ${forbidden_bit}`);\n continue;\n }\n\n const payload_data = data.subarray(offset, next_startcode_offset);\n\n nalu_payload = new H264NaluPayload();\n nalu_payload.type = nalu_type;\n nalu_payload.data = payload_data;\n }\n\n return nalu_payload;\n }\n}\n\nexport class AVCDecoderConfigurationRecord {\n private data: Uint8Array;\n\n // sps, pps: require Nalu without 4 byte length-header\n public constructor(sps: Uint8Array, pps: Uint8Array, sps_details: any) {\n let length = 6 + 2 + sps.byteLength + 1 + 2 + pps.byteLength;\n let need_extra_fields = false;\n\n if (sps[3] !== 66 && sps[3] !== 77 && sps[3] !== 88) {\n need_extra_fields = true;\n length += 4;\n }\n\n const data = (this.data = new Uint8Array(length));\n\n data[0] = 0x01; // configurationVersion\n data[1] = sps[1]; // AVCProfileIndication\n data[2] = sps[2]; // profile_compatibility\n data[3] = sps[3]; // AVCLevelIndication\n data[4] = 0xff; // 111111 + lengthSizeMinusOne(3)\n\n data[5] = 0xe0 | 0x01; // 111 + numOfSequenceParameterSets\n\n const sps_length = sps.byteLength;\n data[6] = sps_length >>> 8; // sequenceParameterSetLength\n data[7] = sps_length & 0xff;\n\n let offset = 8;\n data.set(sps, 8);\n offset += sps_length;\n\n data[offset] = 1; // numOfPictureParameterSets\n\n const pps_length = pps.byteLength;\n data[offset + 1] = pps_length >>> 8; // pictureParameterSetLength\n data[offset + 2] = pps_length & 0xff;\n\n data.set(pps, offset + 3);\n offset += 3 + pps_length;\n\n if (need_extra_fields) {\n data[offset] = 0xfc | sps_details.chroma_format_idc;\n data[offset + 1] = 0xf8 | (sps_details.bit_depth_luma - 8);\n data[offset + 2] = 0xf8 | (sps_details.bit_depth_chroma - 8);\n data[offset + 3] = 0x00; // number of sps ext\n offset += 4;\n }\n }\n\n public getData() {\n return this.data;\n }\n}\n\nexport function annexb2avcc(data: Buffer) {\n const annexb_parser = new H264AnnexBParser(data);\n let nalu_payload: H264NaluPayload | null = null;\n const video_init_segment_dispatched_ = false;\n const video_metadata_changed_ = false;\n\n const video_metadata_: {\n sps: H264NaluAVC1 | undefined;\n pps: H264NaluAVC1 | undefined;\n details: any;\n } = {\n sps: undefined,\n pps: undefined,\n details: undefined,\n };\n\n while ((nalu_payload = annexb_parser.readNextNaluPayload()) != null) {\n const nalu_avc1 = new H264NaluAVC1(nalu_payload);\n\n if (nalu_avc1.type === H264NaluType.kSliceSPS) {\n // Notice: parseSPS requires Nalu without startcode or length-header\n const details = SPSParser.parseSPS(nalu_payload.data);\n if (!video_init_segment_dispatched_) {\n video_metadata_.sps = nalu_avc1;\n video_metadata_.details = details;\n }\n } else if (nalu_avc1.type === H264NaluType.kSlicePPS) {\n if (!video_init_segment_dispatched_ || video_metadata_changed_) {\n video_metadata_.pps = nalu_avc1;\n }\n }\n }\n\n const sps_without_header = video_metadata_.sps!.data.subarray(4);\n const pps_without_header = video_metadata_.pps!.data.subarray(4);\n const details = video_metadata_.details;\n const avcc = new AVCDecoderConfigurationRecord(\n sps_without_header,\n pps_without_header,\n details\n );\n return avcc.getData();\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export * from "./container";
2
+ export * from "./h264";
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./container"), exports);
18
+ __exportStar(require("./h264"), exports);
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../rtp/src/container/mp4/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,8CAA4B;AAC5B,yCAAuB","sourcesContent":["export * from \"./container\";\nexport * from \"./h264\";\n"]}
@@ -0,0 +1,5 @@
1
+ export type { MP4ArrayBuffer as ArrayBuffer, MP4AudioTrack as AudioTrack, MP4File as File, MP4Info as Info, Sample, SampleOptions, MP4Track as Track, TrackOptions, MP4VideoTrack as VideoTrack, } from "mp4box";
2
+ export { BoxParser, ISOFile, Log, createFile as New, DataStream as Stream, } from "mp4box";
3
+ import { MP4AudioTrack, MP4Track, MP4VideoTrack } from "mp4box";
4
+ export declare function isAudioTrack(track: MP4Track): track is MP4AudioTrack;
5
+ export declare function isVideoTrack(track: MP4Track): track is MP4VideoTrack;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isVideoTrack = exports.isAudioTrack = exports.Stream = exports.New = exports.Log = exports.ISOFile = exports.BoxParser = void 0;
4
+ var mp4box_1 = require("mp4box");
5
+ Object.defineProperty(exports, "BoxParser", { enumerable: true, get: function () { return mp4box_1.BoxParser; } });
6
+ Object.defineProperty(exports, "ISOFile", { enumerable: true, get: function () { return mp4box_1.ISOFile; } });
7
+ Object.defineProperty(exports, "Log", { enumerable: true, get: function () { return mp4box_1.Log; } });
8
+ Object.defineProperty(exports, "New", { enumerable: true, get: function () { return mp4box_1.createFile; } });
9
+ Object.defineProperty(exports, "Stream", { enumerable: true, get: function () { return mp4box_1.DataStream; } });
10
+ const mp4box_2 = require("mp4box");
11
+ function isAudioTrack(track) {
12
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
13
+ return track.audio !== undefined;
14
+ }
15
+ exports.isAudioTrack = isAudioTrack;
16
+ function isVideoTrack(track) {
17
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
18
+ return track.video !== undefined;
19
+ }
20
+ exports.isVideoTrack = isVideoTrack;
21
+ // TODO contribute to mp4box
22
+ mp4box_2.BoxParser.dOpsBox.prototype.write = function (stream) {
23
+ this.size = 11;
24
+ this.writeHeader(stream);
25
+ stream.writeUint8(0);
26
+ stream.writeUint8(this.OutputChannelCount);
27
+ stream.writeUint16(this.PreSkip);
28
+ stream.writeUint32(this.InputSampleRate);
29
+ stream.writeUint16(0);
30
+ stream.writeUint8(0);
31
+ };
32
+ //# sourceMappingURL=mp4box.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mp4box.js","sourceRoot":"","sources":["../../../../../../rtp/src/container/mp4/mp4box.ts"],"names":[],"mappings":";;;AAYA,iCAMgB;AALd,mGAAA,SAAS,OAAA;AACT,iGAAA,OAAO,OAAA;AACP,6FAAA,GAAG,OAAA;AACH,6FAAA,UAAU,OAAO;AACjB,gGAAA,UAAU,OAAU;AAGtB,mCAMgB;AAEhB,SAAgB,YAAY,CAAC,KAAe;IAC1C,uEAAuE;IACvE,OAAQ,KAAuB,CAAC,KAAK,KAAK,SAAS,CAAC;AACtD,CAAC;AAHD,oCAGC;AAED,SAAgB,YAAY,CAAC,KAAe;IAC1C,uEAAuE;IACvE,OAAQ,KAAuB,CAAC,KAAK,KAAK,SAAS,CAAC;AACtD,CAAC;AAHD,oCAGC;AAED,4BAA4B;AAC5B,kBAAS,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,GAAG,UAAU,MAAkB;IAC9D,IAAI,CAAC,IAAI,GAAG,EAAE,CAAC;IACf,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;IAEzB,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;IACrB,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAC;IAC3C,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACjC,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;IACzC,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;IACtB,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AACvB,CAAC,CAAC","sourcesContent":["// Rename some stuff so it's on brand.\nexport type {\n MP4ArrayBuffer as ArrayBuffer,\n MP4AudioTrack as AudioTrack,\n MP4File as File,\n MP4Info as Info,\n Sample,\n SampleOptions,\n MP4Track as Track,\n TrackOptions,\n MP4VideoTrack as VideoTrack,\n} from \"mp4box\";\nexport {\n BoxParser,\n ISOFile,\n Log,\n createFile as New,\n DataStream as Stream,\n} from \"mp4box\";\n\nimport {\n BoxParser,\n DataStream,\n MP4AudioTrack,\n MP4Track,\n MP4VideoTrack,\n} from \"mp4box\";\n\nexport function isAudioTrack(track: MP4Track): track is MP4AudioTrack {\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n return (track as MP4AudioTrack).audio !== undefined;\n}\n\nexport function isVideoTrack(track: MP4Track): track is MP4VideoTrack {\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n return (track as MP4VideoTrack).video !== undefined;\n}\n\n// TODO contribute to mp4box\nBoxParser.dOpsBox.prototype.write = function (stream: DataStream) {\n this.size = 11;\n this.writeHeader(stream);\n\n stream.writeUint8(0);\n stream.writeUint8(this.OutputChannelCount);\n stream.writeUint16(this.PreSkip);\n stream.writeUint32(this.InputSampleRate);\n stream.writeUint16(0);\n stream.writeUint8(0);\n};\n"]}
@@ -0,0 +1,40 @@
1
+ declare class SPSParser {
2
+ static _ebsp2rbsp(uint8array: any): Uint8Array;
3
+ static parseSPS(uint8array: any): {
4
+ codec_mimetype: string;
5
+ profile_idc: number;
6
+ level_idc: number;
7
+ profile_string: string;
8
+ level_string: string;
9
+ chroma_format_idc: number;
10
+ bit_depth: number;
11
+ bit_depth_luma: number;
12
+ bit_depth_chroma: number;
13
+ ref_frames: number;
14
+ chroma_format: number;
15
+ chroma_format_string: string;
16
+ frame_rate: {
17
+ fixed: boolean;
18
+ fps: number;
19
+ fps_den: number;
20
+ fps_num: number;
21
+ };
22
+ sar_ratio: {
23
+ width: number;
24
+ height: number;
25
+ };
26
+ codec_size: {
27
+ width: number;
28
+ height: number;
29
+ };
30
+ present_size: {
31
+ width: number;
32
+ height: number;
33
+ };
34
+ };
35
+ static _skipScalingList(gb: any, count: any): void;
36
+ static getProfileString(profile_idc: any): "Baseline" | "Main" | "Extended" | "High" | "High10" | "High422" | "High444" | "Unknown";
37
+ static getLevelString(level_idc: any): string;
38
+ static getChromaFormatString(chroma: any): "Unknown" | "4:2:0" | "4:2:2" | "4:4:4";
39
+ }
40
+ export default SPSParser;
@@ -0,0 +1,291 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (C) 2016 Bilibili. All Rights Reserved.
4
+ *
5
+ * @author zheng qian <xqq@xqq.im>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+ var __importDefault = (this && this.__importDefault) || function (mod) {
20
+ return (mod && mod.__esModule) ? mod : { "default": mod };
21
+ };
22
+ Object.defineProperty(exports, "__esModule", { value: true });
23
+ const exp_golomb_1 = __importDefault(require("./exp-golomb"));
24
+ class SPSParser {
25
+ static _ebsp2rbsp(uint8array) {
26
+ const src = uint8array;
27
+ const src_length = src.byteLength;
28
+ const dst = new Uint8Array(src_length);
29
+ let dst_idx = 0;
30
+ for (let i = 0; i < src_length; i++) {
31
+ if (i >= 2) {
32
+ // Unescape: Skip 0x03 after 00 00
33
+ if (src[i] === 0x03 && src[i - 1] === 0x00 && src[i - 2] === 0x00) {
34
+ continue;
35
+ }
36
+ }
37
+ dst[dst_idx] = src[i];
38
+ dst_idx++;
39
+ }
40
+ return new Uint8Array(dst.buffer, 0, dst_idx);
41
+ }
42
+ static parseSPS(uint8array) {
43
+ const codec_array = uint8array.subarray(1, 4);
44
+ let codec_mimetype = "avc1.";
45
+ for (let j = 0; j < 3; j++) {
46
+ let h = codec_array[j].toString(16);
47
+ if (h.length < 2) {
48
+ h = "0" + h;
49
+ }
50
+ codec_mimetype += h;
51
+ }
52
+ const rbsp = SPSParser._ebsp2rbsp(uint8array);
53
+ let gb = new exp_golomb_1.default(rbsp);
54
+ gb.readByte();
55
+ const profile_idc = gb.readByte(); // profile_idc
56
+ gb.readByte(); // constraint_set_flags[5] + reserved_zero[3]
57
+ const level_idc = gb.readByte(); // level_idc
58
+ gb.readUEG(); // seq_parameter_set_id
59
+ const profile_string = SPSParser.getProfileString(profile_idc);
60
+ const level_string = SPSParser.getLevelString(level_idc);
61
+ let chroma_format_idc = 1;
62
+ let chroma_format = 420;
63
+ const chroma_format_table = [0, 420, 422, 444];
64
+ let bit_depth_luma = 8;
65
+ let bit_depth_chroma = 8;
66
+ if (profile_idc === 100 ||
67
+ profile_idc === 110 ||
68
+ profile_idc === 122 ||
69
+ profile_idc === 244 ||
70
+ profile_idc === 44 ||
71
+ profile_idc === 83 ||
72
+ profile_idc === 86 ||
73
+ profile_idc === 118 ||
74
+ profile_idc === 128 ||
75
+ profile_idc === 138 ||
76
+ profile_idc === 144) {
77
+ chroma_format_idc = gb.readUEG();
78
+ if (chroma_format_idc === 3) {
79
+ gb.readBits(1); // separate_colour_plane_flag
80
+ }
81
+ if (chroma_format_idc <= 3) {
82
+ chroma_format = chroma_format_table[chroma_format_idc];
83
+ }
84
+ bit_depth_luma = gb.readUEG() + 8; // bit_depth_luma_minus8
85
+ bit_depth_chroma = gb.readUEG() + 8; // bit_depth_chroma_minus8
86
+ gb.readBits(1); // qpprime_y_zero_transform_bypass_flag
87
+ if (gb.readBool()) {
88
+ // seq_scaling_matrix_present_flag
89
+ const scaling_list_count = chroma_format_idc !== 3 ? 8 : 12;
90
+ for (let i = 0; i < scaling_list_count; i++) {
91
+ if (gb.readBool()) {
92
+ // seq_scaling_list_present_flag
93
+ if (i < 6) {
94
+ SPSParser._skipScalingList(gb, 16);
95
+ }
96
+ else {
97
+ SPSParser._skipScalingList(gb, 64);
98
+ }
99
+ }
100
+ }
101
+ }
102
+ }
103
+ gb.readUEG(); // log2_max_frame_num_minus4
104
+ const pic_order_cnt_type = gb.readUEG();
105
+ if (pic_order_cnt_type === 0) {
106
+ gb.readUEG(); // log2_max_pic_order_cnt_lsb_minus_4
107
+ }
108
+ else if (pic_order_cnt_type === 1) {
109
+ gb.readBits(1); // delta_pic_order_always_zero_flag
110
+ gb.readSEG(); // offset_for_non_ref_pic
111
+ gb.readSEG(); // offset_for_top_to_bottom_field
112
+ const num_ref_frames_in_pic_order_cnt_cycle = gb.readUEG();
113
+ for (let i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++) {
114
+ gb.readSEG(); // offset_for_ref_frame
115
+ }
116
+ }
117
+ const ref_frames = gb.readUEG(); // max_num_ref_frames
118
+ gb.readBits(1); // gaps_in_frame_num_value_allowed_flag
119
+ const pic_width_in_mbs_minus1 = gb.readUEG();
120
+ const pic_height_in_map_units_minus1 = gb.readUEG();
121
+ const frame_mbs_only_flag = gb.readBits(1);
122
+ if (frame_mbs_only_flag === 0) {
123
+ gb.readBits(1); // mb_adaptive_frame_field_flag
124
+ }
125
+ gb.readBits(1); // direct_8x8_inference_flag
126
+ let frame_crop_left_offset = 0;
127
+ let frame_crop_right_offset = 0;
128
+ let frame_crop_top_offset = 0;
129
+ let frame_crop_bottom_offset = 0;
130
+ const frame_cropping_flag = gb.readBool();
131
+ if (frame_cropping_flag) {
132
+ frame_crop_left_offset = gb.readUEG();
133
+ frame_crop_right_offset = gb.readUEG();
134
+ frame_crop_top_offset = gb.readUEG();
135
+ frame_crop_bottom_offset = gb.readUEG();
136
+ }
137
+ let sar_width = 1, sar_height = 1;
138
+ let fps = 0, fps_fixed = true, fps_num = 0, fps_den = 0;
139
+ const vui_parameters_present_flag = gb.readBool();
140
+ if (vui_parameters_present_flag) {
141
+ if (gb.readBool()) {
142
+ // aspect_ratio_info_present_flag
143
+ const aspect_ratio_idc = gb.readByte();
144
+ const sar_w_table = [
145
+ 1, 12, 10, 16, 40, 24, 20, 32, 80, 18, 15, 64, 160, 4, 3, 2,
146
+ ];
147
+ const sar_h_table = [
148
+ 1, 11, 11, 11, 33, 11, 11, 11, 33, 11, 11, 33, 99, 3, 2, 1,
149
+ ];
150
+ if (aspect_ratio_idc > 0 && aspect_ratio_idc < 16) {
151
+ sar_width = sar_w_table[aspect_ratio_idc - 1];
152
+ sar_height = sar_h_table[aspect_ratio_idc - 1];
153
+ }
154
+ else if (aspect_ratio_idc === 255) {
155
+ sar_width = (gb.readByte() << 8) | gb.readByte();
156
+ sar_height = (gb.readByte() << 8) | gb.readByte();
157
+ }
158
+ }
159
+ if (gb.readBool()) {
160
+ // overscan_info_present_flag
161
+ gb.readBool(); // overscan_appropriate_flag
162
+ }
163
+ if (gb.readBool()) {
164
+ // video_signal_type_present_flag
165
+ gb.readBits(4); // video_format & video_full_range_flag
166
+ if (gb.readBool()) {
167
+ // colour_description_present_flag
168
+ gb.readBits(24); // colour_primaries & transfer_characteristics & matrix_coefficients
169
+ }
170
+ }
171
+ if (gb.readBool()) {
172
+ // chroma_loc_info_present_flag
173
+ gb.readUEG(); // chroma_sample_loc_type_top_field
174
+ gb.readUEG(); // chroma_sample_loc_type_bottom_field
175
+ }
176
+ if (gb.readBool()) {
177
+ // timing_info_present_flag
178
+ const num_units_in_tick = gb.readBits(32);
179
+ const time_scale = gb.readBits(32);
180
+ fps_fixed = gb.readBool(); // fixed_frame_rate_flag
181
+ fps_num = time_scale;
182
+ fps_den = num_units_in_tick * 2;
183
+ fps = fps_num / fps_den;
184
+ }
185
+ }
186
+ let sarScale = 1;
187
+ if (sar_width !== 1 || sar_height !== 1) {
188
+ sarScale = sar_width / sar_height;
189
+ }
190
+ let crop_unit_x = 0, crop_unit_y = 0;
191
+ if (chroma_format_idc === 0) {
192
+ crop_unit_x = 1;
193
+ crop_unit_y = 2 - frame_mbs_only_flag;
194
+ }
195
+ else {
196
+ const sub_wc = chroma_format_idc === 3 ? 1 : 2;
197
+ const sub_hc = chroma_format_idc === 1 ? 2 : 1;
198
+ crop_unit_x = sub_wc;
199
+ crop_unit_y = sub_hc * (2 - frame_mbs_only_flag);
200
+ }
201
+ let codec_width = (pic_width_in_mbs_minus1 + 1) * 16;
202
+ let codec_height = (2 - frame_mbs_only_flag) * ((pic_height_in_map_units_minus1 + 1) * 16);
203
+ codec_width -=
204
+ (frame_crop_left_offset + frame_crop_right_offset) * crop_unit_x;
205
+ codec_height -=
206
+ (frame_crop_top_offset + frame_crop_bottom_offset) * crop_unit_y;
207
+ const present_width = Math.ceil(codec_width * sarScale);
208
+ gb.destroy();
209
+ gb = null;
210
+ return {
211
+ codec_mimetype,
212
+ profile_idc,
213
+ level_idc,
214
+ profile_string,
215
+ level_string,
216
+ chroma_format_idc,
217
+ bit_depth: bit_depth_luma,
218
+ bit_depth_luma,
219
+ bit_depth_chroma,
220
+ ref_frames,
221
+ chroma_format,
222
+ chroma_format_string: SPSParser.getChromaFormatString(chroma_format),
223
+ frame_rate: {
224
+ fixed: fps_fixed,
225
+ fps: fps,
226
+ fps_den: fps_den,
227
+ fps_num: fps_num,
228
+ },
229
+ sar_ratio: {
230
+ width: sar_width,
231
+ height: sar_height,
232
+ },
233
+ codec_size: {
234
+ width: codec_width,
235
+ height: codec_height,
236
+ },
237
+ present_size: {
238
+ width: present_width,
239
+ height: codec_height,
240
+ },
241
+ };
242
+ }
243
+ static _skipScalingList(gb, count) {
244
+ let last_scale = 8, next_scale = 8;
245
+ let delta_scale = 0;
246
+ for (let i = 0; i < count; i++) {
247
+ if (next_scale !== 0) {
248
+ delta_scale = gb.readSEG();
249
+ next_scale = (last_scale + delta_scale + 256) % 256;
250
+ }
251
+ last_scale = next_scale === 0 ? last_scale : next_scale;
252
+ }
253
+ }
254
+ static getProfileString(profile_idc) {
255
+ switch (profile_idc) {
256
+ case 66:
257
+ return "Baseline";
258
+ case 77:
259
+ return "Main";
260
+ case 88:
261
+ return "Extended";
262
+ case 100:
263
+ return "High";
264
+ case 110:
265
+ return "High10";
266
+ case 122:
267
+ return "High422";
268
+ case 244:
269
+ return "High444";
270
+ default:
271
+ return "Unknown";
272
+ }
273
+ }
274
+ static getLevelString(level_idc) {
275
+ return (level_idc / 10).toFixed(1);
276
+ }
277
+ static getChromaFormatString(chroma) {
278
+ switch (chroma) {
279
+ case 420:
280
+ return "4:2:0";
281
+ case 422:
282
+ return "4:2:2";
283
+ case 444:
284
+ return "4:4:4";
285
+ default:
286
+ return "Unknown";
287
+ }
288
+ }
289
+ }
290
+ exports.default = SPSParser;
291
+ //# sourceMappingURL=sps-parser.js.map