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 #ifndef HEVC_UTILS_H_
18 
19 #define HEVC_UTILS_H_
20 
21 #include <stdint.h>
22 
23 #include <media/stagefright/foundation/ABase.h>
24 #include <media/stagefright/foundation/ABuffer.h>
25 #include <utils/Errors.h>
26 #include <utils/KeyedVector.h>
27 #include <utils/StrongPointer.h>
28 #include <utils/Vector.h>
29 
30 namespace android {
31 
32 enum {
33     kHevcNalUnitTypeVps = 32,
34     kHevcNalUnitTypeSps = 33,
35     kHevcNalUnitTypePps = 34,
36     kHevcNalUnitTypePrefixSei = 39,
37     kHevcNalUnitTypeSuffixSei = 40,
38 };
39 
40 enum {
41     // uint8_t
42     kGeneralProfileSpace,
43     // uint8_t
44     kGeneralTierFlag,
45     // uint8_t
46     kGeneralProfileIdc,
47     // uint32_t
48     kGeneralProfileCompatibilityFlags,
49     // uint64_t
50     kGeneralConstraintIndicatorFlags,
51     // uint8_t
52     kGeneralLevelIdc,
53     // uint8_t
54     kChromaFormatIdc,
55     // uint8_t
56     kBitDepthLumaMinus8,
57     // uint8_t
58     kBitDepthChromaMinus8,
59     // uint8_t
60     kVideoFullRangeFlag,
61     // uint8_t
62     kColourPrimaries,
63     // uint8_t
64     kTransferCharacteristics,
65     // uint8_t
66     kMatrixCoeffs,
67 };
68 
69 class HevcParameterSets {
70 public:
71     enum Info : uint32_t {
72         kInfoNone                = 0,
73         kInfoIsHdr               = 1 << 0,
74         kInfoHasColorDescription = 1 << 1,
75     };
76 
77     HevcParameterSets();
78 
79     status_t addNalUnit(const uint8_t* data, size_t size);
80 
81     bool findParam8(uint32_t key, uint8_t *param);
82     bool findParam16(uint32_t key, uint16_t *param);
83     bool findParam32(uint32_t key, uint32_t *param);
84     bool findParam64(uint32_t key, uint64_t *param);
85 
getNumNalUnits()86     inline size_t getNumNalUnits() { return mNalUnits.size(); }
87     size_t getNumNalUnitsOfType(uint8_t type);
88     uint8_t getType(size_t index);
89     size_t getSize(size_t index);
90     // Note that this method does not write the start code.
91     bool write(size_t index, uint8_t* dest, size_t size);
92     status_t makeHvcc(uint8_t *hvcc, size_t *hvccSize, size_t nalSizeLength);
93 
getInfo()94     Info getInfo() const { return mInfo; }
95 
96 private:
97     status_t parseVps(const uint8_t* data, size_t size);
98     status_t parseSps(const uint8_t* data, size_t size);
99     status_t parsePps(const uint8_t* data, size_t size);
100 
101     KeyedVector<uint32_t, uint64_t> mParams;
102     Vector<sp<ABuffer>> mNalUnits;
103     Info mInfo;
104 
105     DISALLOW_EVIL_CONSTRUCTORS(HevcParameterSets);
106 };
107 
108 }  // namespace android
109 
110 #endif  // HEVC_UTILS_H_
111