1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "HevcUtils"
19
20 #include <cstring>
21 #include <utility>
22
23 #include "include/HevcUtils.h"
24 #include "include/avc_utils.h"
25
26 #include <media/stagefright/foundation/ABitReader.h>
27 #include <media/stagefright/foundation/ABuffer.h>
28 #include <media/stagefright/foundation/ADebug.h>
29 #include <media/stagefright/foundation/AMessage.h>
30 #include <media/stagefright/MediaErrors.h>
31 #include <media/stagefright/Utils.h>
32
33 namespace android {
34
35 static const uint8_t kHevcNalUnitTypes[5] = {
36 kHevcNalUnitTypeVps,
37 kHevcNalUnitTypeSps,
38 kHevcNalUnitTypePps,
39 kHevcNalUnitTypePrefixSei,
40 kHevcNalUnitTypeSuffixSei,
41 };
42
HevcParameterSets()43 HevcParameterSets::HevcParameterSets()
44 : mInfo(kInfoNone) {
45 }
46
addNalUnit(const uint8_t * data,size_t size)47 status_t HevcParameterSets::addNalUnit(const uint8_t* data, size_t size) {
48 if (size < 1) {
49 ALOGE("empty NAL b/35467107");
50 return ERROR_MALFORMED;
51 }
52 uint8_t nalUnitType = (data[0] >> 1) & 0x3f;
53 status_t err = OK;
54 switch (nalUnitType) {
55 case 32: // VPS
56 if (size < 2) {
57 ALOGE("invalid NAL/VPS size b/35467107");
58 return ERROR_MALFORMED;
59 }
60 err = parseVps(data + 2, size - 2);
61 break;
62 case 33: // SPS
63 if (size < 2) {
64 ALOGE("invalid NAL/SPS size b/35467107");
65 return ERROR_MALFORMED;
66 }
67 err = parseSps(data + 2, size - 2);
68 break;
69 case 34: // PPS
70 if (size < 2) {
71 ALOGE("invalid NAL/PPS size b/35467107");
72 return ERROR_MALFORMED;
73 }
74 err = parsePps(data + 2, size - 2);
75 break;
76 case 39: // Prefix SEI
77 case 40: // Suffix SEI
78 // Ignore
79 break;
80 default:
81 ALOGE("Unrecognized NAL unit type.");
82 return ERROR_MALFORMED;
83 }
84
85 if (err != OK) {
86 return err;
87 }
88
89 sp<ABuffer> buffer = ABuffer::CreateAsCopy(data, size);
90 buffer->setInt32Data(nalUnitType);
91 mNalUnits.push(buffer);
92 return OK;
93 }
94
95 template <typename T>
findParam(uint32_t key,T * param,KeyedVector<uint32_t,uint64_t> & params)96 static bool findParam(uint32_t key, T *param,
97 KeyedVector<uint32_t, uint64_t> ¶ms) {
98 CHECK(param);
99 if (params.indexOfKey(key) < 0) {
100 return false;
101 }
102 *param = (T) params[key];
103 return true;
104 }
105
findParam8(uint32_t key,uint8_t * param)106 bool HevcParameterSets::findParam8(uint32_t key, uint8_t *param) {
107 return findParam(key, param, mParams);
108 }
109
findParam16(uint32_t key,uint16_t * param)110 bool HevcParameterSets::findParam16(uint32_t key, uint16_t *param) {
111 return findParam(key, param, mParams);
112 }
113
findParam32(uint32_t key,uint32_t * param)114 bool HevcParameterSets::findParam32(uint32_t key, uint32_t *param) {
115 return findParam(key, param, mParams);
116 }
117
findParam64(uint32_t key,uint64_t * param)118 bool HevcParameterSets::findParam64(uint32_t key, uint64_t *param) {
119 return findParam(key, param, mParams);
120 }
121
getNumNalUnitsOfType(uint8_t type)122 size_t HevcParameterSets::getNumNalUnitsOfType(uint8_t type) {
123 size_t num = 0;
124 for (size_t i = 0; i < mNalUnits.size(); ++i) {
125 if (getType(i) == type) {
126 ++num;
127 }
128 }
129 return num;
130 }
131
getType(size_t index)132 uint8_t HevcParameterSets::getType(size_t index) {
133 CHECK_LT(index, mNalUnits.size());
134 return mNalUnits[index]->int32Data();
135 }
136
getSize(size_t index)137 size_t HevcParameterSets::getSize(size_t index) {
138 CHECK_LT(index, mNalUnits.size());
139 return mNalUnits[index]->size();
140 }
141
write(size_t index,uint8_t * dest,size_t size)142 bool HevcParameterSets::write(size_t index, uint8_t* dest, size_t size) {
143 CHECK_LT(index, mNalUnits.size());
144 const sp<ABuffer>& nalUnit = mNalUnits[index];
145 if (size < nalUnit->size()) {
146 ALOGE("dest buffer size too small: %zu vs. %zu to be written",
147 size, nalUnit->size());
148 return false;
149 }
150 memcpy(dest, nalUnit->data(), nalUnit->size());
151 return true;
152 }
153
parseVps(const uint8_t * data,size_t size)154 status_t HevcParameterSets::parseVps(const uint8_t* data, size_t size) {
155 // See Rec. ITU-T H.265 v3 (04/2015) Chapter 7.3.2.1 for reference
156 NALBitReader reader(data, size);
157 // Skip vps_video_parameter_set_id
158 reader.skipBits(4);
159 // Skip vps_base_layer_internal_flag
160 reader.skipBits(1);
161 // Skip vps_base_layer_available_flag
162 reader.skipBits(1);
163 // Skip vps_max_layers_minus_1
164 reader.skipBits(6);
165 // Skip vps_temporal_id_nesting_flags
166 reader.skipBits(1);
167 // Skip reserved
168 reader.skipBits(16);
169
170 if (reader.atLeastNumBitsLeft(96)) {
171 mParams.add(kGeneralProfileSpace, reader.getBits(2));
172 mParams.add(kGeneralTierFlag, reader.getBits(1));
173 mParams.add(kGeneralProfileIdc, reader.getBits(5));
174 mParams.add(kGeneralProfileCompatibilityFlags, reader.getBits(32));
175 mParams.add(
176 kGeneralConstraintIndicatorFlags,
177 ((uint64_t)reader.getBits(16) << 32) | reader.getBits(32));
178 mParams.add(kGeneralLevelIdc, reader.getBits(8));
179 // 96 bits total for general profile.
180 } else {
181 reader.skipBits(96);
182 }
183
184 return reader.overRead() ? ERROR_MALFORMED : OK;
185 }
186
parseSps(const uint8_t * data,size_t size)187 status_t HevcParameterSets::parseSps(const uint8_t* data, size_t size) {
188 // See Rec. ITU-T H.265 v3 (04/2015) Chapter 7.3.2.2 for reference
189 NALBitReader reader(data, size);
190 // Skip sps_video_parameter_set_id
191 reader.skipBits(4);
192 uint8_t maxSubLayersMinus1 = reader.getBitsWithFallback(3, 0);
193 // Skip sps_temporal_id_nesting_flag;
194 reader.skipBits(1);
195 // Skip general profile
196 reader.skipBits(96);
197 if (maxSubLayersMinus1 > 0) {
198 bool subLayerProfilePresentFlag[8];
199 bool subLayerLevelPresentFlag[8];
200 for (int i = 0; i < maxSubLayersMinus1; ++i) {
201 subLayerProfilePresentFlag[i] = reader.getBitsWithFallback(1, 0);
202 subLayerLevelPresentFlag[i] = reader.getBitsWithFallback(1, 0);
203 }
204 // Skip reserved
205 reader.skipBits(2 * (8 - maxSubLayersMinus1));
206 for (int i = 0; i < maxSubLayersMinus1; ++i) {
207 if (subLayerProfilePresentFlag[i]) {
208 // Skip profile
209 reader.skipBits(88);
210 }
211 if (subLayerLevelPresentFlag[i]) {
212 // Skip sub_layer_level_idc[i]
213 reader.skipBits(8);
214 }
215 }
216 }
217 // Skip sps_seq_parameter_set_id
218 skipUE(&reader);
219 uint8_t chromaFormatIdc = parseUEWithFallback(&reader, 0);
220 mParams.add(kChromaFormatIdc, chromaFormatIdc);
221 if (chromaFormatIdc == 3) {
222 // Skip separate_colour_plane_flag
223 reader.skipBits(1);
224 }
225 // Skip pic_width_in_luma_samples
226 skipUE(&reader);
227 // Skip pic_height_in_luma_samples
228 skipUE(&reader);
229 if (reader.getBitsWithFallback(1, 0) /* i.e. conformance_window_flag */) {
230 // Skip conf_win_left_offset
231 skipUE(&reader);
232 // Skip conf_win_right_offset
233 skipUE(&reader);
234 // Skip conf_win_top_offset
235 skipUE(&reader);
236 // Skip conf_win_bottom_offset
237 skipUE(&reader);
238 }
239 mParams.add(kBitDepthLumaMinus8, parseUEWithFallback(&reader, 0));
240 mParams.add(kBitDepthChromaMinus8, parseUEWithFallback(&reader, 0));
241
242 // log2_max_pic_order_cnt_lsb_minus4
243 size_t log2MaxPicOrderCntLsb = parseUEWithFallback(&reader, 0) + (size_t)4;
244 bool spsSubLayerOrderingInfoPresentFlag = reader.getBitsWithFallback(1, 0);
245 for (uint32_t i = spsSubLayerOrderingInfoPresentFlag ? 0 : maxSubLayersMinus1;
246 i <= maxSubLayersMinus1; ++i) {
247 skipUE(&reader); // sps_max_dec_pic_buffering_minus1[i]
248 skipUE(&reader); // sps_max_num_reorder_pics[i]
249 skipUE(&reader); // sps_max_latency_increase_plus1[i]
250 }
251
252 skipUE(&reader); // log2_min_luma_coding_block_size_minus3
253 skipUE(&reader); // log2_diff_max_min_luma_coding_block_size
254 skipUE(&reader); // log2_min_luma_transform_block_size_minus2
255 skipUE(&reader); // log2_diff_max_min_luma_transform_block_size
256 skipUE(&reader); // max_transform_hierarchy_depth_inter
257 skipUE(&reader); // max_transform_hierarchy_depth_intra
258 if (reader.getBitsWithFallback(1, 0)) { // scaling_list_enabled_flag u(1)
259 // scaling_list_data
260 if (reader.getBitsWithFallback(1, 0)) { // sps_scaling_list_data_present_flag
261 for (uint32_t sizeId = 0; sizeId < 4; ++sizeId) {
262 for (uint32_t matrixId = 0; matrixId < 6; matrixId += (sizeId == 3) ? 3 : 1) {
263 if (!reader.getBitsWithFallback(1, 1)) {
264 // scaling_list_pred_mode_flag[sizeId][matrixId]
265 skipUE(&reader); // scaling_list_pred_matrix_id_delta[sizeId][matrixId]
266 } else {
267 uint32_t coefNum = std::min(64, (1 << (4 + (sizeId << 1))));
268 if (sizeId > 1) {
269 skipSE(&reader); // scaling_list_dc_coef_minus8[sizeId − 2][matrixId]
270 }
271 for (uint32_t i = 0; i < coefNum; ++i) {
272 skipSE(&reader); // scaling_list_delta_coef
273 }
274 }
275 }
276 }
277 }
278 }
279 reader.skipBits(1); // amp_enabled_flag
280 reader.skipBits(1); // sample_adaptive_offset_enabled_flag u(1)
281 if (reader.getBitsWithFallback(1, 0)) { // pcm_enabled_flag
282 reader.skipBits(4); // pcm_sample_bit_depth_luma_minus1
283 reader.skipBits(4); // pcm_sample_bit_depth_chroma_minus1 u(4)
284 skipUE(&reader); // log2_min_pcm_luma_coding_block_size_minus3
285 skipUE(&reader); // log2_diff_max_min_pcm_luma_coding_block_size
286 reader.skipBits(1); // pcm_loop_filter_disabled_flag
287 }
288 uint32_t numShortTermRefPicSets = parseUEWithFallback(&reader, 0);
289 uint32_t numPics = 0;
290 for (uint32_t i = 0; i < numShortTermRefPicSets; ++i) {
291 // st_ref_pic_set(i)
292 if (i != 0 && reader.getBitsWithFallback(1, 0)) { // inter_ref_pic_set_prediction_flag
293 reader.skipBits(1); // delta_rps_sign
294 skipUE(&reader); // abs_delta_rps_minus1
295 uint32_t nextNumPics = 0;
296 for (uint32_t j = 0; j <= numPics; ++j) {
297 if (reader.getBitsWithFallback(1, 0) // used_by_curr_pic_flag[j]
298 || reader.getBitsWithFallback(1, 0)) { // use_delta_flag[j]
299 ++nextNumPics;
300 }
301 }
302 numPics = nextNumPics;
303 } else {
304 uint32_t numNegativePics = parseUEWithFallback(&reader, 0);
305 uint32_t numPositivePics = parseUEWithFallback(&reader, 0);
306 if (numNegativePics > UINT32_MAX - numPositivePics) {
307 return ERROR_MALFORMED;
308 }
309 numPics = numNegativePics + numPositivePics;
310 for (uint32_t j = 0; j < numPics; ++j) {
311 skipUE(&reader); // delta_poc_s0|1_minus1[i]
312 reader.skipBits(1); // used_by_curr_pic_s0|1_flag[i]
313 }
314 }
315 }
316 if (reader.getBitsWithFallback(1, 0)) { // long_term_ref_pics_present_flag
317 uint32_t numLongTermRefPicSps = parseUEWithFallback(&reader, 0);
318 for (uint32_t i = 0; i < numLongTermRefPicSps; ++i) {
319 reader.skipBits(log2MaxPicOrderCntLsb); // lt_ref_pic_poc_lsb_sps[i]
320 reader.skipBits(1); // used_by_curr_pic_lt_sps_flag[i]
321 }
322 }
323 reader.skipBits(1); // sps_temporal_mvp_enabled_flag
324 reader.skipBits(1); // strong_intra_smoothing_enabled_flag
325 if (reader.getBitsWithFallback(1, 0)) { // vui_parameters_present_flag
326 if (reader.getBitsWithFallback(1, 0)) { // aspect_ratio_info_present_flag
327 uint32_t aspectRatioIdc = reader.getBitsWithFallback(8, 0);
328 if (aspectRatioIdc == 0xFF /* EXTENDED_SAR */) {
329 reader.skipBits(16); // sar_width
330 reader.skipBits(16); // sar_height
331 }
332 }
333 if (reader.getBitsWithFallback(1, 0)) { // overscan_info_present_flag
334 reader.skipBits(1); // overscan_appropriate_flag
335 }
336 if (reader.getBitsWithFallback(1, 0)) { // video_signal_type_present_flag
337 reader.skipBits(3); // video_format
338 uint32_t videoFullRangeFlag;
339 if (reader.getBitsGraceful(1, &videoFullRangeFlag)) {
340 mParams.add(kVideoFullRangeFlag, videoFullRangeFlag);
341 }
342 if (reader.getBitsWithFallback(1, 0)) { // colour_description_present_flag
343 mInfo = (Info)(mInfo | kInfoHasColorDescription);
344 uint32_t colourPrimaries, transferCharacteristics, matrixCoeffs;
345 if (reader.getBitsGraceful(8, &colourPrimaries)) {
346 mParams.add(kColourPrimaries, colourPrimaries);
347 }
348 if (reader.getBitsGraceful(8, &transferCharacteristics)) {
349 mParams.add(kTransferCharacteristics, transferCharacteristics);
350 if (transferCharacteristics == 16 /* ST 2084 */
351 || transferCharacteristics == 18 /* ARIB STD-B67 HLG */) {
352 mInfo = (Info)(mInfo | kInfoIsHdr);
353 }
354 }
355 if (reader.getBitsGraceful(8, &matrixCoeffs)) {
356 mParams.add(kMatrixCoeffs, matrixCoeffs);
357 }
358 }
359 // skip rest of VUI
360 }
361 }
362
363 return reader.overRead() ? ERROR_MALFORMED : OK;
364 }
365
parsePps(const uint8_t * data __unused,size_t size __unused)366 status_t HevcParameterSets::parsePps(
367 const uint8_t* data __unused, size_t size __unused) {
368 return OK;
369 }
370
makeHvcc(uint8_t * hvcc,size_t * hvccSize,size_t nalSizeLength)371 status_t HevcParameterSets::makeHvcc(uint8_t *hvcc, size_t *hvccSize,
372 size_t nalSizeLength) {
373 if (hvcc == NULL || hvccSize == NULL
374 || (nalSizeLength != 4 && nalSizeLength != 2)) {
375 return BAD_VALUE;
376 }
377 // ISO 14496-15: HEVC file format
378 size_t size = 23; // 23 bytes in the header
379 size_t numOfArrays = 0;
380 const size_t numNalUnits = getNumNalUnits();
381 for (size_t i = 0; i < ARRAY_SIZE(kHevcNalUnitTypes); ++i) {
382 uint8_t type = kHevcNalUnitTypes[i];
383 size_t numNalus = getNumNalUnitsOfType(type);
384 if (numNalus == 0) {
385 continue;
386 }
387 ++numOfArrays;
388 size += 3;
389 for (size_t j = 0; j < numNalUnits; ++j) {
390 if (getType(j) != type) {
391 continue;
392 }
393 size += 2 + getSize(j);
394 }
395 }
396 uint8_t generalProfileSpace, generalTierFlag, generalProfileIdc;
397 if (!findParam8(kGeneralProfileSpace, &generalProfileSpace)
398 || !findParam8(kGeneralTierFlag, &generalTierFlag)
399 || !findParam8(kGeneralProfileIdc, &generalProfileIdc)) {
400 return ERROR_MALFORMED;
401 }
402 uint32_t compatibilityFlags;
403 uint64_t constraintIdcFlags;
404 if (!findParam32(kGeneralProfileCompatibilityFlags, &compatibilityFlags)
405 || !findParam64(kGeneralConstraintIndicatorFlags, &constraintIdcFlags)) {
406 return ERROR_MALFORMED;
407 }
408 uint8_t generalLevelIdc;
409 if (!findParam8(kGeneralLevelIdc, &generalLevelIdc)) {
410 return ERROR_MALFORMED;
411 }
412 uint8_t chromaFormatIdc, bitDepthLumaMinus8, bitDepthChromaMinus8;
413 if (!findParam8(kChromaFormatIdc, &chromaFormatIdc)
414 || !findParam8(kBitDepthLumaMinus8, &bitDepthLumaMinus8)
415 || !findParam8(kBitDepthChromaMinus8, &bitDepthChromaMinus8)) {
416 return ERROR_MALFORMED;
417 }
418 if (size > *hvccSize) {
419 return NO_MEMORY;
420 }
421 *hvccSize = size;
422
423 uint8_t *header = hvcc;
424 header[0] = 1;
425 header[1] = (kGeneralProfileSpace << 6) | (kGeneralTierFlag << 5) | kGeneralProfileIdc;
426 header[2] = (compatibilityFlags >> 24) & 0xff;
427 header[3] = (compatibilityFlags >> 16) & 0xff;
428 header[4] = (compatibilityFlags >> 8) & 0xff;
429 header[5] = compatibilityFlags & 0xff;
430 header[6] = (constraintIdcFlags >> 40) & 0xff;
431 header[7] = (constraintIdcFlags >> 32) & 0xff;
432 header[8] = (constraintIdcFlags >> 24) & 0xff;
433 header[9] = (constraintIdcFlags >> 16) & 0xff;
434 header[10] = (constraintIdcFlags >> 8) & 0xff;
435 header[11] = constraintIdcFlags & 0xff;
436 header[12] = generalLevelIdc;
437 // FIXME: parse min_spatial_segmentation_idc.
438 header[13] = 0xf0;
439 header[14] = 0;
440 // FIXME: derive parallelismType properly.
441 header[15] = 0xfc;
442 header[16] = 0xfc | chromaFormatIdc;
443 header[17] = 0xf8 | bitDepthLumaMinus8;
444 header[18] = 0xf8 | bitDepthChromaMinus8;
445 // FIXME: derive avgFrameRate
446 header[19] = 0;
447 header[20] = 0;
448 // constantFrameRate, numTemporalLayers, temporalIdNested all set to 0.
449 header[21] = nalSizeLength - 1;
450 header[22] = numOfArrays;
451 header += 23;
452 for (size_t i = 0; i < ARRAY_SIZE(kHevcNalUnitTypes); ++i) {
453 uint8_t type = kHevcNalUnitTypes[i];
454 size_t numNalus = getNumNalUnitsOfType(type);
455 if (numNalus == 0) {
456 continue;
457 }
458 // array_completeness set to 0.
459 header[0] = type;
460 header[1] = (numNalus >> 8) & 0xff;
461 header[2] = numNalus & 0xff;
462 header += 3;
463 for (size_t j = 0; j < numNalUnits; ++j) {
464 if (getType(j) != type) {
465 continue;
466 }
467 header[0] = (getSize(j) >> 8) & 0xff;
468 header[1] = getSize(j) & 0xff;
469 if (!write(j, header + 2, size - (header - (uint8_t *)hvcc))) {
470 return NO_MEMORY;
471 }
472 header += (2 + getSize(j));
473 }
474 }
475 CHECK_EQ(header - size, hvcc);
476
477 return OK;
478 }
479
480 } // namespace android
481