h265_common.h (4233B)
1 /* 2 * Copyright (c) 2023 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef COMMON_VIDEO_H265_H265_COMMON_H_ 12 #define COMMON_VIDEO_H265_H265_COMMON_H_ 13 14 #include <cstddef> 15 #include <cstdint> 16 #include <vector> 17 18 #include "api/array_view.h" 19 #include "rtc_base/buffer.h" 20 #include "rtc_base/system/rtc_export.h" 21 22 namespace webrtc { 23 24 namespace H265 { 25 // The size of a full NALU start sequence {0 0 0 1}, used for the first NALU 26 // of an access unit, and for SPS and PPS blocks. 27 constexpr size_t kNaluLongStartSequenceSize = 4; 28 29 // The size of a shortened NALU start sequence {0 0 1}, that may be used if 30 // not the first NALU of an access unit or an SPS or PPS block. 31 constexpr size_t kNaluShortStartSequenceSize = 3; 32 33 // The size of the NALU header byte (2). 34 constexpr size_t kNaluHeaderSize = 2; 35 36 // Type description of 0-40 is defined in Table7-1 of the H.265 spec 37 // Type desciption of 48-49 is defined in section 4.4.2 and 4.4.3 of RFC7798 38 enum NaluType : uint8_t { 39 kTrailN = 0, 40 kTrailR = 1, 41 kTsaN = 2, 42 kTsaR = 3, 43 kStsaN = 4, 44 kStsaR = 5, 45 kRadlN = 6, 46 kRadlR = 7, 47 kBlaWLp = 16, 48 kBlaWRadl = 17, 49 kBlaNLp = 18, 50 kIdrWRadl = 19, 51 kIdrNLp = 20, 52 kCra = 21, 53 kRsvIrapVcl23 = 23, 54 kRsvVcl31 = 31, 55 kVps = 32, 56 kSps = 33, 57 kPps = 34, 58 kAud = 35, 59 kPrefixSei = 39, 60 kSuffixSei = 40, 61 // Aggregation packets, refer to section 4.4.2 in RFC 7798. 62 kAp = 48, 63 // Fragmentation units, refer to section 4.4.3 in RFC 7798. 64 kFu = 49, 65 // PACI packets, refer to section 4.4.4 in RFC 7798. 66 kPaci = 50 67 }; 68 69 // Slice type definition. See table 7-7 of the H.265 spec 70 enum SliceType : uint8_t { kB = 0, kP = 1, kI = 2 }; 71 72 struct NaluIndex { 73 // Start index of NALU, including start sequence. 74 size_t start_offset = 0; 75 // Start index of NALU payload, typically type header. 76 size_t payload_start_offset = 0; 77 // Length of NALU payload, in bytes, counting from payload_start_offset. 78 size_t payload_size = 0; 79 }; 80 81 // Returns a vector of the NALU indices in the given buffer. 82 RTC_EXPORT std::vector<NaluIndex> FindNaluIndices( 83 ArrayView<const uint8_t> buffer); 84 85 // TODO: bugs.webrtc.org/42225170 - Deprecate. 86 inline std::vector<NaluIndex> FindNaluIndices(const uint8_t* buffer, 87 size_t buffer_size) { 88 return FindNaluIndices(MakeArrayView(buffer, buffer_size)); 89 } 90 91 // Get the NAL type from the header byte immediately following start sequence. 92 RTC_EXPORT NaluType ParseNaluType(uint8_t data); 93 94 // Methods for parsing and writing RBSP. See section 7.4.2 of the H.265 spec. 95 // 96 // The following sequences are illegal, and need to be escaped when encoding: 97 // 00 00 00 -> 00 00 03 00 98 // 00 00 01 -> 00 00 03 01 99 // 00 00 02 -> 00 00 03 02 100 // And things in the source that look like the emulation byte pattern (00 00 03) 101 // need to have an extra emulation byte added, so it's removed when decoding: 102 // 00 00 03 -> 00 00 03 03 103 // 104 // Decoding is simply a matter of finding any 00 00 03 sequence and removing 105 // the 03 emulation byte. 106 107 // Parse the given data and remove any emulation byte escaping. 108 std::vector<uint8_t> ParseRbsp(ArrayView<const uint8_t> data); 109 110 // TODO: bugs.webrtc.org/42225170 - Deprecate. 111 inline std::vector<uint8_t> ParseRbsp(const uint8_t* data, size_t length) { 112 return ParseRbsp(MakeArrayView(data, length)); 113 } 114 115 // Write the given data to the destination buffer, inserting and emulation 116 // bytes in order to escape any data the could be interpreted as a start 117 // sequence. 118 void WriteRbsp(ArrayView<const uint8_t> bytes, Buffer* destination); 119 120 // TODO: bugs.webrtc.org/42225170 - Deprecate. 121 inline void WriteRbsp(const uint8_t* bytes, 122 size_t length, 123 Buffer* destination) { 124 WriteRbsp(MakeArrayView(bytes, length), destination); 125 } 126 127 uint32_t Log2Ceiling(uint32_t value); 128 129 } // namespace H265 130 } // namespace webrtc 131 132 #endif // COMMON_VIDEO_H265_H265_COMMON_H_