xref: /aosp_15_r20/external/v4l2_codec2/common/include/v4l2_codec2/common/NalParser.h (revision 0ec5a0ec62797f775085659156625e7f1bdb369f)
1 // Copyright 2021 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef ANDROID_V4L2_CODEC2_COMMON_NALPARSER_H
6 #define ANDROID_V4L2_CODEC2_COMMON_NALPARSER_H
7 
8 #include <stdint.h>
9 
10 #include <media/stagefright/foundation/ABitReader.h>
11 
12 namespace android {
13 
14 // Helper class to parse NAL units from data.
15 class NalParser {
16 public:
17     // Parameters related to a video's color aspects.
18     struct ColorAspects {
19         uint32_t primaries;
20         uint32_t transfer;
21         uint32_t coeffs;
22         bool fullRange;
23     };
24 
25     NalParser(const uint8_t* data, size_t length);
26     virtual ~NalParser() = default;
27 
28     // Locates the next NAL after |mNextNalStartCodePos|. If there is one, updates |mCurrNalDataPos|
29     // to the first byte of the NAL data (start code is not included), and |mNextNalStartCodePos| to
30     // the position of the next start code, and returns true.
31     // If there is no more NAL, returns false.
32     //
33     // Note: This method must be called prior to data() and length().
34     bool locateNextNal();
35 
36     // Locate the sequence parameter set (SPS).
37     virtual bool locateSPS() = 0;
38     virtual bool locateIDR() = 0;
39 
40     // Gets current NAL data (start code is not included).
41     const uint8_t* data() const;
42 
43     // Gets the byte length of current NAL data (start code is not included).
44     size_t length() const;
45 
46     // Get the type of the current NAL unit.
47     virtual uint8_t type() const = 0;
48 
49     // Find the video's color aspects in the current SPS NAL.
50     virtual bool findCodedColorAspects(ColorAspects* colorAspects) = 0;
51 
52     // Read unsigned int encoded with exponential-golomb.
53     static bool parseUE(ABitReader* br, uint32_t* val);
54 
55     // Read signed int encoded with exponential-golomb.
56     static bool parseSE(ABitReader* br, int32_t* val);
57 
58 protected:
59     const uint8_t* findNextStartCodePos() const;
60 
61     // The byte pattern for the start of a NAL unit.
62     const uint8_t kNalStartCode[3] = {0x00, 0x00, 0x01};
63     // The length in bytes of the NAL-unit start pattern.
64     const size_t kNalStartCodeLength = 3;
65 
66     const uint8_t* mCurrNalDataPos;
67     const uint8_t* mDataEnd;
68     const uint8_t* mNextNalStartCodePos;
69 };
70 
71 }  // namespace android
72 
73 #endif  // ANDROID_V4L2_CODEC2_COMMON_NALPARSER_H
74