-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
h264.js
352 lines (315 loc) · 10.2 KB
/
h264.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.transmuxer.H264');
goog.require('shaka.util.ExpGolomb');
goog.require('shaka.util.Uint8ArrayUtils');
/**
* H.264 utils
*/
shaka.transmuxer.H264 = class {
/**
* Read a sequence parameter set and return some interesting video
* properties. A sequence parameter set is the H264 metadata that
* describes the properties of upcoming video frames.
*
* @param {!Array.<shaka.extern.VideoNalu>} nalus
* @return {?{height: number, width: number, videoConfig: !Uint8Array,
* hSpacing: number, vSpacing: number}}
*/
static parseInfo(nalus) {
const H264 = shaka.transmuxer.H264;
if (!nalus.length) {
return null;
}
const spsNalu = nalus.find((nalu) => {
return nalu.type == H264.NALU_TYPE_SPS_;
});
const ppsNalu = nalus.find((nalu) => {
return nalu.type == H264.NALU_TYPE_PPS_;
});
if (!spsNalu || !ppsNalu) {
return null;
}
const expGolombDecoder = new shaka.util.ExpGolomb(spsNalu.data);
// profile_idc
const profileIdc = expGolombDecoder.readUnsignedByte();
// constraint_set[0-5]_flag
expGolombDecoder.readUnsignedByte();
// level_idc u(8)
expGolombDecoder.readUnsignedByte();
// seq_parameter_set_id
expGolombDecoder.skipExpGolomb();
// some profiles have more optional data we don't need
if (H264.PROFILES_WITH_OPTIONAL_SPS_DATA_.includes(profileIdc)) {
const chromaFormatIdc = expGolombDecoder.readUnsignedExpGolomb();
if (chromaFormatIdc === 3) {
// separate_colour_plane_flag
expGolombDecoder.skipBits(1);
}
// bit_depth_luma_minus8
expGolombDecoder.skipExpGolomb();
// bit_depth_chroma_minus8
expGolombDecoder.skipExpGolomb();
// qpprime_y_zero_transform_bypass_flag
expGolombDecoder.skipBits(1);
// seq_scaling_matrix_present_flag
if (expGolombDecoder.readBoolean()) {
const scalingListCount = (chromaFormatIdc !== 3) ? 8 : 12;
for (let i = 0; i < scalingListCount; i++) {
// seq_scaling_list_present_flag[ i ]
if (expGolombDecoder.readBoolean()) {
if (i < 6) {
expGolombDecoder.skipScalingList(16);
} else {
expGolombDecoder.skipScalingList(64);
}
}
}
}
}
// log2_max_frame_num_minus4
expGolombDecoder.skipExpGolomb();
const picOrderCntType = expGolombDecoder.readUnsignedExpGolomb();
if (picOrderCntType === 0) {
// log2_max_pic_order_cnt_lsb_minus4
expGolombDecoder.readUnsignedExpGolomb();
} else if (picOrderCntType === 1) {
// delta_pic_order_always_zero_flag
expGolombDecoder.skipBits(1);
// offset_for_non_ref_pic
expGolombDecoder.skipExpGolomb();
// offset_for_top_to_bottom_field
expGolombDecoder.skipExpGolomb();
const numRefFramesInPicOrderCntCycle =
expGolombDecoder.readUnsignedExpGolomb();
for (let i = 0; i < numRefFramesInPicOrderCntCycle; i++) {
// offset_for_ref_frame[ i ]
expGolombDecoder.skipExpGolomb();
}
}
// max_num_ref_frames
expGolombDecoder.skipExpGolomb();
// gaps_in_frame_num_value_allowed_flag
expGolombDecoder.skipBits(1);
const picWidthInMbsMinus1 =
expGolombDecoder.readUnsignedExpGolomb();
const picHeightInMapUnitsMinus1 =
expGolombDecoder.readUnsignedExpGolomb();
const frameMbsOnlyFlag = expGolombDecoder.readBits(1);
if (frameMbsOnlyFlag === 0) {
// mb_adaptive_frame_field_flag
expGolombDecoder.skipBits(1);
}
// direct_8x8_inference_flag
expGolombDecoder.skipBits(1);
let frameCropLeftOffset = 0;
let frameCropRightOffset = 0;
let frameCropTopOffset = 0;
let frameCropBottomOffset = 0;
// frame_cropping_flag
if (expGolombDecoder.readBoolean()) {
frameCropLeftOffset = expGolombDecoder.readUnsignedExpGolomb();
frameCropRightOffset = expGolombDecoder.readUnsignedExpGolomb();
frameCropTopOffset = expGolombDecoder.readUnsignedExpGolomb();
frameCropBottomOffset = expGolombDecoder.readUnsignedExpGolomb();
}
let hSpacing = 1;
let vSpacing = 1;
// vui_parameters_present_flag
if (expGolombDecoder.readBoolean()) {
// aspect_ratio_info_present_flag
if (expGolombDecoder.readBoolean()) {
const aspectRatioIdc = expGolombDecoder.readUnsignedByte();
const hSpacingTable = [
1, 12, 10, 16, 40, 24, 20, 32, 80, 18, 15, 64, 160, 4, 3, 2,
];
const vSpacingTable = [
1, 11, 11, 11, 33, 11, 11, 11, 33, 11, 11, 33, 99, 3, 2, 1,
];
if (aspectRatioIdc > 0 && aspectRatioIdc < 16) {
hSpacing = hSpacingTable[aspectRatioIdc - 1];
vSpacing = vSpacingTable[aspectRatioIdc - 1];
} else if (aspectRatioIdc === 255) {
hSpacing = expGolombDecoder.readBits(16);
vSpacing = expGolombDecoder.readBits(16);
}
}
}
const height = ((2 - frameMbsOnlyFlag) *
(picHeightInMapUnitsMinus1 + 1) * 16) - (frameCropTopOffset * 2) -
(frameCropBottomOffset * 2);
const width = ((picWidthInMbsMinus1 + 1) * 16) -
frameCropLeftOffset * 2 - frameCropRightOffset * 2;
// assemble the SPSs
let sps = [];
const spsData = spsNalu.fullData;
sps.push((spsData.byteLength >>> 8) & 0xff);
sps.push(spsData.byteLength & 0xff);
sps = sps.concat(...spsData);
// assemble the PPSs
let pps = [];
const ppsData = ppsNalu.fullData;
pps.push((ppsData.byteLength >>> 8) & 0xff);
pps.push(ppsData.byteLength & 0xff);
pps = pps.concat(...ppsData);
const videoConfig = new Uint8Array(
[
0x01, // version
sps[3], // profile
sps[4], // profile compat
sps[5], // level
0xfc | 3, // lengthSizeMinusOne, hard-coded to 4 bytes
0xe0 | 1, // 3bit reserved (111) + numOfSequenceParameterSets
].concat(sps).concat([
1, // numOfPictureParameterSets
]).concat(pps));
return {
height,
width,
videoConfig,
hSpacing,
vSpacing,
};
}
/**
* @param {!Array.<shaka.extern.VideoNalu>} nalus
* @return {?{data: !Uint8Array, isKeyframe: boolean}}
*/
static parseFrame(nalus) {
const H264 = shaka.transmuxer.H264;
let isKeyframe = false;
const nalusData = [];
const spsNalu = nalus.find((nalu) => {
return nalu.type == H264.NALU_TYPE_SPS_;
});
let avcSample = false;
for (const nalu of nalus) {
let push = false;
switch (nalu.type) {
case H264.NALU_TYPE_NDR_: {
avcSample = true;
push = true;
const data = nalu.data;
// Only check slice type to detect KF in case SPS found in same packet
// (any keyframe is preceded by SPS ...)
if (spsNalu && data.length > 4) {
// retrieve slice type by parsing beginning of NAL unit (follow
// H264 spec,slice_header definition) to detect keyframe embedded
// in NDR
const sliceType = new shaka.util.ExpGolomb(data).readSliceType();
// 2 : I slice, 4 : SI slice, 7 : I slice, 9: SI slice
// SI slice : A slice that is coded using intra prediction only and
// using quantisation of the prediction samples.
// An SI slice can be coded such that its decoded samples can be
// constructed identically to an SP slice.
// I slice: A slice that is not an SI slice that is decoded using
// intra prediction only.
if (sliceType === 2 || sliceType === 4 ||
sliceType === 7 || sliceType === 9) {
isKeyframe = true;
}
}
break;
}
case H264.NALU_TYPE_IDR_:
avcSample = true;
push = true;
isKeyframe = true;
break;
case H264.NALU_TYPE_SEI_:
push = true;
break;
case H264.NALU_TYPE_SPS_:
push = true;
break;
case H264.NALU_TYPE_PPS_:
push = true;
break;
case H264.NALU_TYPE_AUD_:
push = true;
avcSample = true;
break;
case H264.NALU_TYPE_FILLER_DATA_:
push = true;
break;
default:
push = false;
break;
}
if (avcSample && push) {
const size = nalu.fullData.byteLength;
const naluLength = new Uint8Array(4);
naluLength[0] = (size >> 24) & 0xff;
naluLength[1] = (size >> 16) & 0xff;
naluLength[2] = (size >> 8) & 0xff;
naluLength[3] = size & 0xff;
nalusData.push(shaka.util.Uint8ArrayUtils.concat(
naluLength, nalu.fullData));
}
}
if (!nalusData.length) {
return null;
}
const data = shaka.util.Uint8ArrayUtils.concat(...nalusData);
return {
data,
isKeyframe,
};
}
};
/**
* NALU type for NDR for H.264.
* @const {number}
* @private
*/
shaka.transmuxer.H264.NALU_TYPE_NDR_ = 0x01;
/**
* NALU type for Instantaneous Decoder Refresh (IDR) for H.264.
* @const {number}
* @private
*/
shaka.transmuxer.H264.NALU_TYPE_IDR_ = 0x05;
/**
* NALU type for Supplemental Enhancement Information (SEI) for H.264.
* @const {number}
* @private
*/
shaka.transmuxer.H264.NALU_TYPE_SEI_ = 0x06;
/**
* NALU type for Sequence Parameter Set (SPS) for H.264.
* @const {number}
* @private
*/
shaka.transmuxer.H264.NALU_TYPE_SPS_ = 0x07;
/**
* NALU type for Picture Parameter Set (PPS) for H.264.
* @const {number}
* @private
*/
shaka.transmuxer.H264.NALU_TYPE_PPS_ = 0x08;
/**
* NALU type for Access Unit Delimiter (AUD) for H.264.
* @const {number}
* @private
*/
shaka.transmuxer.H264.NALU_TYPE_AUD_ = 0x09;
/**
* NALU type for Filler Data for H.264.
* @const {number}
* @private
*/
shaka.transmuxer.H264.NALU_TYPE_FILLER_DATA_ = 0x0c;
/**
* Values of profile_idc that indicate additional fields are included in the
* SPS.
* see Recommendation ITU-T H.264 (4/2013)
* 7.3.2.1.1 Sequence parameter set data syntax
*
* @const {!Array.<number>}
* @private
*/
shaka.transmuxer.H264.PROFILES_WITH_OPTIONAL_SPS_DATA_ =
[100, 110, 122, 244, 44, 83, 86, 118, 128, 138, 139, 134];