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