h264_common.h (3444B)
1 /* 2 * Copyright (c) 2016 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_H264_H264_COMMON_H_ 12 #define COMMON_VIDEO_H264_H264_COMMON_H_ 13 14 #include <stddef.h> 15 #include <stdint.h> 16 17 #include <vector> 18 19 #include "api/array_view.h" 20 #include "rtc_base/buffer.h" 21 #include "rtc_base/system/rtc_export.h" 22 23 namespace webrtc { 24 25 namespace H264 { 26 // The size of a full NALU start sequence {0 0 0 1}, used for the first NALU 27 // of an access unit, and for SPS and PPS blocks. 28 const size_t kNaluLongStartSequenceSize = 4; 29 30 // The size of a shortened NALU start sequence {0 0 1}, that may be used if 31 // not the first NALU of an access unit or an SPS or PPS block. 32 const size_t kNaluShortStartSequenceSize = 3; 33 34 // The size of the NALU type byte (1). 35 const size_t kNaluTypeSize = 1; 36 37 // Maximum reference index for reference pictures. 38 constexpr int kMaxReferenceIndex = 31; 39 40 enum NaluType : uint8_t { 41 kSlice = 1, 42 kIdr = 5, 43 kSei = 6, 44 kSps = 7, 45 kPps = 8, 46 kAud = 9, 47 kEndOfSequence = 10, 48 kEndOfStream = 11, 49 kFiller = 12, 50 kPrefix = 14, 51 kStapA = 24, 52 kFuA = 28 53 }; 54 55 enum SliceType : uint8_t { kP = 0, kB = 1, kI = 2, kSp = 3, kSi = 4 }; 56 57 struct NaluIndex { 58 // Start index of NALU, including start sequence. 59 size_t start_offset; 60 // Start index of NALU payload, typically type header. 61 size_t payload_start_offset; 62 // Length of NALU payload, in bytes, counting from payload_start_offset. 63 size_t payload_size; 64 }; 65 66 // Returns a vector of the NALU indices in the given buffer. 67 RTC_EXPORT std::vector<NaluIndex> FindNaluIndices( 68 ArrayView<const uint8_t> buffer); 69 70 // Get the NAL type from the header byte immediately following start sequence. 71 RTC_EXPORT NaluType ParseNaluType(uint8_t data); 72 73 // Methods for parsing and writing RBSP. See section 7.4.1 of the H264 spec. 74 // 75 // The following sequences are illegal, and need to be escaped when encoding: 76 // 00 00 00 -> 00 00 03 00 77 // 00 00 01 -> 00 00 03 01 78 // 00 00 02 -> 00 00 03 02 79 // And things in the source that look like the emulation byte pattern (00 00 03) 80 // need to have an extra emulation byte added, so it's removed when decoding: 81 // 00 00 03 -> 00 00 03 03 82 // 83 // Decoding is simply a matter of finding any 00 00 03 sequence and removing 84 // the 03 emulation byte. 85 86 // Parse the given data and remove any emulation byte escaping. 87 std::vector<uint8_t> ParseRbsp(ArrayView<const uint8_t> data); 88 89 // TODO: bugs.webrtc.org/42225170 - Deprecate. 90 inline std::vector<uint8_t> ParseRbsp(const uint8_t* data, size_t length) { 91 return ParseRbsp(MakeArrayView(data, length)); 92 } 93 94 // Write the given data to the destination buffer, inserting and emulation 95 // bytes in order to escape any data the could be interpreted as a start 96 // sequence. 97 void WriteRbsp(ArrayView<const uint8_t> bytes, Buffer* destination); 98 99 // TODO: bugs.webrtc.org/42225170 - Deprecate. 100 inline void WriteRbsp(const uint8_t* bytes, 101 size_t length, 102 Buffer* destination) { 103 WriteRbsp(MakeArrayView(bytes, length), destination); 104 } 105 } // namespace H264 106 } // namespace webrtc 107 108 #endif // COMMON_VIDEO_H264_H264_COMMON_H_