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