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 namespace android { 11 12 // Helper class to parse H264 NAL units from data. 13 class NalParser { 14 public: 15 // Type of a IDR Slice NAL unit. 16 static constexpr uint8_t kIDRType = 5; 17 // Type of a SPS NAL unit. 18 static constexpr uint8_t kSPSType = 7; 19 // Type of a PPS NAL unit. 20 static constexpr uint8_t kPPSType = 8; 21 22 // Parameters related to a video's color aspects. 23 struct ColorAspects { 24 uint32_t primaries; 25 uint32_t transfer; 26 uint32_t coeffs; 27 bool fullRange; 28 }; 29 30 NalParser(const uint8_t* data, size_t length); 31 32 // Locates the next NAL after |mNextNalStartCodePos|. If there is one, updates |mCurrNalDataPos| 33 // to the first byte of the NAL data (start code is not included), and |mNextNalStartCodePos| to 34 // the position of the next start code, and returns true. 35 // If there is no more NAL, returns false. 36 // 37 // Note: This method must be called prior to data() and length(). 38 bool locateNextNal(); 39 40 // Locate the sequence parameter set (SPS). 41 bool locateSPS(); 42 43 // Gets current NAL data (start code is not included). 44 const uint8_t* data() const; 45 46 // Gets the byte length of current NAL data (start code is not included). 47 size_t length() const; 48 49 // Get the type of the current NAL unit. 50 uint8_t type() const; 51 52 // Find the H.264 video's color aspects in the current SPS NAL. 53 bool findCodedColorAspects(ColorAspects* colorAspects); 54 55 private: 56 const uint8_t* findNextStartCodePos() const; 57 58 // The byte pattern for the start of a H264 NAL unit. 59 const uint8_t kNalStartCode[3] = {0x00, 0x00, 0x01}; 60 // The length in bytes of the NAL-unit start pattern. 61 const size_t kNalStartCodeLength = 3; 62 63 const uint8_t* mCurrNalDataPos; 64 const uint8_t* mDataEnd; 65 const uint8_t* mNextNalStartCodePos; 66 }; 67 68 } // namespace android 69 70 #endif // ANDROID_V4L2_CODEC2_COMMON_NALPARSER_H 71