• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 UTIL_STARTCODEDETECTOR_H
17 #define UTIL_STARTCODEDETECTOR_H
18 
19 #include <string>
20 #include <list>
21 #include <vector>
22 #include <optional>
23 #include <fstream>
24 
25 enum CodeType {
26     H264,
27     H265
28 };
29 
30 struct Sample {
31     size_t startPos;
32     size_t endPos;
33     bool isCsd;
34     bool isIdr;
35     size_t idx;
36 };
37 
38 class StartCodeDetector {
39 public:
40     size_t SetSource(const std::string &path, CodeType type);  // return sample cnt
41     bool SeekTo(size_t sampleIdx);
42     std::optional<Sample> PeekNextSample();
43     void MoveToNext();
44 
45 private:
46     struct NALUInfo {
47         size_t startPos;
48         size_t endPos;
49         uint8_t nalType;
50     };
51     using FirstByteInNalu = uint8_t;
52     enum H264NalType : uint8_t {
53         UNSPECIFIED = 0,
54         NON_IDR = 1,
55         PARTITION_A = 2,
56         PARTITION_B = 3,
57         PARTITION_C = 4,
58         IDR = 5,
59         SEI = 6,
60         SPS = 7,
61         PPS = 8,
62         AU_DELIMITER = 9,
63         END_OF_SEQUENCE = 10,
64         END_OF_STREAM = 11,
65         FILLER_DATA = 12,
66         SPS_EXT = 13,
67         PREFIX = 14,
68         SUB_SPS = 15,
69         DPS = 16,
70     };
71 
72     enum H265NalType : uint8_t {
73         HEVC_TRAIL_N = 0,
74         HEVC_TRAIL_R = 1,
75         HEVC_TSA_N = 2,
76         HEVC_TSA_R = 3,
77         HEVC_STSA_N = 4,
78         HEVC_STSA_R = 5,
79         HEVC_RADL_N = 6,
80         HEVC_RADL_R = 7,
81         HEVC_RASL_N = 8,
82         HEVC_RASL_R = 9,
83         HEVC_BLA_W_LP = 16,
84         HEVC_BLA_W_RADL = 17,
85         HEVC_BLA_N_LP = 18,
86         HEVC_IDR_W_RADL = 19,
87         HEVC_IDR_N_LP = 20,
88         HEVC_CRA_NUT = 21,
89         HEVC_VPS_NUT = 32,
90         HEVC_SPS_NUT = 33,
91         HEVC_PPS_NUT = 34,
92         HEVC_AUD_NUT = 35,
93         HEVC_EOS_NUT = 36,
94         HEVC_EOB_NUT = 37,
95         HEVC_FD_NUT = 38,
96         HEVC_PREFIX_SEI_NUT = 39,
97         HEVC_SUFFIX_SEI_NUT = 40,
98     };
99 
100 private:
101     void BuildSampleList();
102     static size_t GetFileSizeInBytes(std::ifstream &ifs);
103     static uint8_t GetNalType(uint8_t byte, CodeType type);
104     static bool IsCsd(const NALUInfo &nalu, CodeType type);
105     static bool IsIdr(const NALUInfo &nalu, CodeType type);
106     static constexpr uint8_t START_CODE[] = {0, 0, 1};
107     static constexpr size_t START_CODE_LEN = sizeof(START_CODE);
108     CodeType type_ = H264;
109     std::list<NALUInfo> nals_;
110     std::vector<Sample> samples_;
111     std::list<size_t> csdIdxList_;
112     std::list<size_t> idrIdxList_;
113 
114     std::optional<size_t> waitingCsd_;  // if user seek previously, the csd sample will be stored here
115     size_t nextSampleIdx_ = 0;
116 };
117 
118 #endif // UTIL_STARTCODEDETECTOR_H
119