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