sprang | 52033d6 | 2016-06-02 02:43:32 -0700 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef COMMON_VIDEO_H264_H264_COMMON_H_ |
| 12 | #define COMMON_VIDEO_H264_H264_COMMON_H_ |
sprang | 52033d6 | 2016-06-02 02:43:32 -0700 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 16 | |
sprang | 52033d6 | 2016-06-02 02:43:32 -0700 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/buffer.h" |
sprang | 52033d6 | 2016-06-02 02:43:32 -0700 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | namespace H264 { |
| 24 | // The size of a full NALU start sequence {0 0 0 1}, used for the first NALU |
| 25 | // of an access unit, and for SPS and PPS blocks. |
| 26 | const size_t kNaluLongStartSequenceSize = 4; |
| 27 | |
| 28 | // The size of a shortened NALU start sequence {0 0 1}, that may be used if |
| 29 | // not the first NALU of an access unit or an SPS or PPS block. |
| 30 | const size_t kNaluShortStartSequenceSize = 3; |
| 31 | |
| 32 | // The size of the NALU type byte (1). |
| 33 | const size_t kNaluTypeSize = 1; |
| 34 | |
| 35 | enum NaluType : uint8_t { |
| 36 | kSlice = 1, |
| 37 | kIdr = 5, |
| 38 | kSei = 6, |
| 39 | kSps = 7, |
| 40 | kPps = 8, |
| 41 | kAud = 9, |
| 42 | kEndOfSequence = 10, |
| 43 | kEndOfStream = 11, |
| 44 | kFiller = 12, |
| 45 | kStapA = 24, |
| 46 | kFuA = 28 |
| 47 | }; |
| 48 | |
| 49 | enum SliceType : uint8_t { kP = 0, kB = 1, kI = 2, kSp = 3, kSi = 4 }; |
| 50 | |
| 51 | struct NaluIndex { |
| 52 | // Start index of NALU, including start sequence. |
| 53 | size_t start_offset; |
| 54 | // Start index of NALU payload, typically type header. |
| 55 | size_t payload_start_offset; |
| 56 | // Length of NALU payload, in bytes, counting from payload_start_offset. |
| 57 | size_t payload_size; |
| 58 | }; |
| 59 | |
| 60 | // Returns a vector of the NALU indices in the given buffer. |
| 61 | std::vector<NaluIndex> FindNaluIndices(const uint8_t* buffer, |
| 62 | size_t buffer_size); |
| 63 | |
| 64 | // Get the NAL type from the header byte immediately following start sequence. |
| 65 | NaluType ParseNaluType(uint8_t data); |
| 66 | |
| 67 | // Methods for parsing and writing RBSP. See section 7.4.1 of the H264 spec. |
| 68 | // |
| 69 | // The following sequences are illegal, and need to be escaped when encoding: |
| 70 | // 00 00 00 -> 00 00 03 00 |
| 71 | // 00 00 01 -> 00 00 03 01 |
| 72 | // 00 00 02 -> 00 00 03 02 |
| 73 | // And things in the source that look like the emulation byte pattern (00 00 03) |
| 74 | // need to have an extra emulation byte added, so it's removed when decoding: |
| 75 | // 00 00 03 -> 00 00 03 03 |
| 76 | // |
| 77 | // Decoding is simply a matter of finding any 00 00 03 sequence and removing |
| 78 | // the 03 emulation byte. |
| 79 | |
| 80 | // Parse the given data and remove any emulation byte escaping. |
kthelgason | 156e3af | 2017-03-06 00:04:32 -0800 | [diff] [blame] | 81 | std::vector<uint8_t> ParseRbsp(const uint8_t* data, size_t length); |
sprang | 52033d6 | 2016-06-02 02:43:32 -0700 | [diff] [blame] | 82 | |
| 83 | // Write the given data to the destination buffer, inserting and emulation |
| 84 | // bytes in order to escape any data the could be interpreted as a start |
| 85 | // sequence. |
| 86 | void WriteRbsp(const uint8_t* bytes, size_t length, rtc::Buffer* destination); |
| 87 | } // namespace H264 |
| 88 | } // namespace webrtc |
| 89 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 90 | #endif // COMMON_VIDEO_H264_H264_COMMON_H_ |