1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_MEDIA_FRAME_DETECTOR_H
17 #define OHOS_MEDIA_FRAME_DETECTOR_H
18 
19 #include <list>
20 #include <string>
21 
22 namespace OHOS {
23 namespace Media {
24 enum class CodeType {
25     H264,
26     H265
27 };
28 
29 class FrameDetector {
30 public:
31     static std::shared_ptr<FrameDetector> GetFrameDetector(CodeType type);
32     bool IsContainIdrFrame(const uint8_t* buff, size_t bufSize);
33 
34 protected:
35     FrameDetector() = default;
36     virtual ~FrameDetector() = default;
37 
38 private:
39     struct NALUInfo {
40         size_t startPos;
41         size_t endPos;
42         uint8_t nalType;
43     };
44 
45     virtual uint8_t GetNalType(uint8_t byte) = 0;
46     virtual bool IsPPS(uint8_t nalType) = 0;
47     virtual bool IsVCL(uint8_t nalType) = 0;
48     virtual bool IsIDR(uint8_t nalType) = 0;
IsPrefixSEI(uint8_t nalType)49     virtual bool IsPrefixSEI(uint8_t nalType) { return false; }
50 
51     static constexpr uint8_t START_CODE[] = {0, 0, 1};
52     static constexpr size_t START_CODE_LEN = sizeof(START_CODE);
53 };
54 
55 class FrameDetectorH264 : public FrameDetector {
56 public:
57     FrameDetectorH264() = default;
58     ~FrameDetectorH264() override = default;
59 
60 private:
61     enum H264NalType : uint8_t {
62         UNSPECIFIED = 0,
63         NON_IDR = 1,
64         PARTITION_A = 2,
65         PARTITION_B = 3,
66         PARTITION_C = 4,
67         IDR = 5,
68         SEI = 6,
69         SPS = 7,
70         PPS = 8,
71         AU_DELIMITER = 9,
72         END_OF_SEQUENCE = 10,
73         END_OF_STREAM = 11,
74         FILLER_DATA = 12,
75         SPS_EXT = 13,
76         PREFIX = 14,
77         SUB_SPS = 15,
78         DPS = 16,
79     };
80     uint8_t GetNalType(uint8_t byte) override;
81     bool IsPPS(uint8_t nalType) override;
82     bool IsVCL(uint8_t nalType) override;
83     bool IsIDR(uint8_t nalType) override;
84 };
85 
86 class FrameDetectorH265 : public FrameDetector {
87 public:
88     FrameDetectorH265() = default;
89     ~FrameDetectorH265() override = default;
90 
91 private:
92     enum H265NalType : uint8_t {
93         HEVC_TRAIL_N = 0,
94         HEVC_TRAIL_R = 1,
95         HEVC_TSA_N = 2,
96         HEVC_TSA_R = 3,
97         HEVC_STSA_N = 4,
98         HEVC_STSA_R = 5,
99         HEVC_RADL_N = 6,
100         HEVC_RADL_R = 7,
101         HEVC_RASL_N = 8,
102         HEVC_RASL_R = 9,
103         HEVC_BLA_W_LP = 16,
104         HEVC_BLA_W_RADL = 17,
105         HEVC_BLA_N_LP = 18,
106         HEVC_IDR_W_RADL = 19,
107         HEVC_IDR_N_LP = 20,
108         HEVC_CRA_NUT = 21,
109         HEVC_VPS_NUT = 32,
110         HEVC_SPS_NUT = 33,
111         HEVC_PPS_NUT = 34,
112         HEVC_AUD_NUT = 35,
113         HEVC_EOS_NUT = 36,
114         HEVC_EOB_NUT = 37,
115         HEVC_FD_NUT = 38,
116         HEVC_PREFIX_SEI_NUT = 39,
117         HEVC_SUFFIX_SEI_NUT = 40,
118     };
119     uint8_t GetNalType(uint8_t byte) override;
120     bool IsPPS(uint8_t nalType) override;
121     bool IsVCL(uint8_t nalType) override;
122     bool IsIDR(uint8_t nalType) override;
123     bool IsPrefixSEI(uint8_t nalType) override;
124 };
125 } // namespace Media
126 } // namespace OHOS
127 #endif // OHOS_MEDIA_FRAME_DETECTOR_H