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