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