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