Shaka Packager SDK
Loading...
Searching...
No Matches
h26x_bit_reader.h
1// Copyright 2014 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// This file contains an implementation of an H264 Annex-B video stream parser.
6
7#ifndef PACKAGER_MEDIA_CODECS_H264_BIT_READER_H_
8#define PACKAGER_MEDIA_CODECS_H264_BIT_READER_H_
9
10#include <sys/types.h>
11
12#include <cstdint>
13
14#include <packager/macros/classes.h>
15
16namespace shaka {
17namespace media {
18
19// A class to provide bit-granularity reading of H.264/H.265 streams.
20// This is not a generic bit reader class, as it takes into account
21// H.264 stream-specific constraints, such as skipping emulation-prevention
22// bytes and stop bits. See spec for more details.
24 public:
27
28 // Initialize the reader to start reading at |data|, |size| being size
29 // of |data| in bytes.
30 // Return false on insufficient size of stream..
31 bool Initialize(const uint8_t* data, off_t size);
32
33 // Read |num_bits| next bits from stream and return in |*out|, first bit
34 // from the stream starting at |num_bits| position in |*out|.
35 // |num_bits| may be 1-32, inclusive.
36 // Return false if the given number of bits cannot be read (not enough
37 // bits in the stream), true otherwise.
38 bool ReadBits(int num_bits, int* out);
39
40 // Read a single bit and return in |*out|.
41 // Return false if the bit cannot be read (not enough bits in the stream),
42 // true otherwise.
43 bool ReadBool(bool* out) {
44 int value;
45 if (!ReadBits(1, &value))
46 return false;
47 *out = (value != 0);
48 return true;
49 }
50
51 // Skips the given number of bits (does not have to be less than 32 bits).
52 // Return false if there aren't enough bits in the stream, true otherwise.
53 bool SkipBits(int num_bits);
54
55 // Exp-Golomb code parsing as specified in chapter 9.1 of the spec.
56 // Read one unsigned exp-Golomb code from the stream and return in |*val|.
57 bool ReadUE(int* val);
58
59 // Read one signed exp-Golomb code from the stream and return in |*val|.
60 bool ReadSE(int* val);
61
62 // Return the number of bits left in the stream.
63 off_t NumBitsLeft();
64
65 // See the definition of more_rbsp_data() in spec.
66 bool HasMoreRBSPData();
67
68 // Return the number of emulation prevention bytes already read.
69 size_t NumEmulationPreventionBytesRead();
70
71 private:
72 // Advance to the next byte, loading it into curr_byte_.
73 // Return false on end of stream.
74 bool UpdateCurrByte();
75
76 // Pointer to the next unread (not in curr_byte_) byte in the stream.
77 const uint8_t* data_;
78
79 // Bytes left in the stream (without the curr_byte_).
80 off_t bytes_left_;
81
82 // Contents of the current byte; first unread bit starting at position
83 // 8 - num_remaining_bits_in_curr_byte_ from MSB.
84 int curr_byte_;
85
86 // Number of bits remaining in curr_byte_
87 int num_remaining_bits_in_curr_byte_;
88
89 // Used in emulation prevention three byte detection (see spec).
90 // Initially set to 0xffff to accept all initial two-byte sequences.
91 int prev_two_bytes_;
92
93 // Number of emulation preventation bytes (0x000003) we met.
94 size_t emulation_prevention_bytes_;
95
96 DISALLOW_COPY_AND_ASSIGN(H26xBitReader);
97};
98
99} // namespace media
100} // namespace shaka
101
102#endif // PACKAGER_MEDIA_CODECS_H264_BIT_READER_H_
All the methods that are virtual are virtual for mocking.