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