OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // This file contains an implementation of an H264 Annex-B video stream parser. | 5 // This file contains an implementation of an H264 Annex-B video stream parser. |
6 | 6 |
7 #ifndef CONTENT_COMMON_GPU_MEDIA_H264_BIT_READER_H_ | 7 #ifndef CONTENT_COMMON_GPU_MEDIA_H264_BIT_READER_H_ |
8 #define CONTENT_COMMON_GPU_MEDIA_H264_BIT_READER_H_ | 8 #define CONTENT_COMMON_GPU_MEDIA_H264_BIT_READER_H_ |
9 | 9 |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "content/common/content_export.h" | 13 #include "content/common/content_export.h" |
14 | 14 |
15 namespace content { | 15 namespace content { |
16 | 16 |
17 // A class to provide bit-granularity reading of H.264 streams. | 17 // A class to provide bit-granularity reading of H.264 streams. |
18 // This is not a generic bit reader class, as it takes into account | 18 // This is not a generic bit reader class, as it takes into account |
19 // H.264 stream-specific constraints, such as skipping emulation-prevention | 19 // H.264 stream-specific constraints: |
20 // bytes and stop bits. See spec for more details. | 20 // - Skipping emulation-prevention bytes, i.e. any byte aligned sequence |
| 21 // 0x00 0x00 0x03 will be replaced by 0x00 0x00. |
| 22 // - Ignore trailing zero bytes/bits, i.e. any consecutive zero bytes/bits at |
| 23 // the end of the stream will be ignored. |
| 24 // - Ignore stop bit, i.e. the last non-zero bit will be ignored. |
| 25 // See ISO 14496 Part 10 - 7.3 for more details. |
21 class CONTENT_EXPORT H264BitReader { | 26 class CONTENT_EXPORT H264BitReader { |
22 public: | 27 public: |
23 H264BitReader(); | 28 H264BitReader(); |
24 ~H264BitReader(); | 29 ~H264BitReader(); |
25 | 30 |
26 // Initialize the reader to start reading at |data|, |size| being size | 31 // Initialize the reader to start reading at |data|, |size| being size |
27 // of |data| in bytes. | 32 // of |data| in bytes. |
28 // Return false on insufficient size of stream.. | 33 // Return false on insufficient size of stream.. |
29 // TODO(posciak,fischman): consider replacing Initialize() with | 34 // TODO(posciak,fischman): consider replacing Initialize() with |
30 // heap-allocating and creating bit readers on demand instead. | 35 // heap-allocating and creating bit readers on demand instead. |
31 bool Initialize(const uint8* data, off_t size); | 36 bool Initialize(const uint8* data, off_t size); |
32 | 37 |
33 // Read |num_bits| next bits from stream and return in |*out|, first bit | 38 // Read |num_bits| (1 to 31 inclusive) from the stream and return them |
34 // from the stream starting at |num_bits| position in |*out|. | 39 // in |*out|, with first bit in the stream as MSB in |*out| at position |
35 // |num_bits| may be 1-32, inclusive. | 40 // (|num_bits| - 1). |
36 // Return false if the given number of bits cannot be read (not enough | 41 // Return false if the given number of bits cannot be read (not enough |
37 // bits in the stream), true otherwise. | 42 // bits in the stream), true otherwise. |
38 bool ReadBits(int num_bits, int *out); | 43 bool ReadBits(int num_bits, int *out); |
39 | 44 |
40 // Return the number of bits left in the stream. | 45 // Return the number of bits left in the stream. This includes the |
41 off_t NumBitsLeft(); | 46 // stop bit and any trailing zero bytes. |
42 | 47 off_t NumBitsLeft() const; |
43 // See the definition of more_rbsp_data() in spec. | |
44 bool HasMoreRBSPData(); | |
45 | 48 |
46 private: | 49 private: |
47 // Advance to the next byte, loading it into curr_byte_. | 50 // Advance to the next byte, loading it into curr_byte_. |
48 // Return false on end of stream. | 51 // Return false on end of stream. |
49 bool UpdateCurrByte(); | 52 bool UpdateCurrByte(); |
50 | 53 |
| 54 // Remove the trailing zero bits if there are any. The trailing zero bits |
| 55 // can spread among more than byte. Then remove the stop bit. So the bit |
| 56 // stream 11001100 00000000 will become 11001. |
| 57 // It returns true if there are still data bits left. |
| 58 bool RemoveTrailingZeroAndStopBit(); |
| 59 |
51 // Pointer to the next unread (not in curr_byte_) byte in the stream. | 60 // Pointer to the next unread (not in curr_byte_) byte in the stream. |
52 const uint8* data_; | 61 const uint8* data_; |
53 | 62 |
54 // Bytes left in the stream (without the curr_byte_). | 63 // Bytes left in the stream (without the curr_byte_). |
55 off_t bytes_left_; | 64 off_t bytes_left_; |
56 | 65 |
57 // Contents of the current byte; first unread bit starting at position | 66 // Contents of the current byte; first unread bit starting at position |
58 // 8 - num_remaining_bits_in_curr_byte_ from MSB. | 67 // 8 - num_remaining_bits_in_curr_byte_ from MSB. |
59 int curr_byte_; | 68 int curr_byte_; |
60 | 69 |
61 // Number of bits remaining in curr_byte_ | 70 // Number of bits remaining in curr_byte_ |
62 int num_remaining_bits_in_curr_byte_; | 71 int num_remaining_bits_in_curr_byte_; |
63 | 72 |
64 // Used in emulation prevention three byte detection (see spec). | 73 // Used in emulation prevention three byte detection (see spec). |
65 // Initially set to 0xffff to accept all initial two-byte sequences. | 74 // Initially set to 0xffff to accept all initial two-byte sequences. |
66 int prev_two_bytes_; | 75 int prev_two_bytes_; |
67 | 76 |
| 77 // Save the number of trailing zero bytes to be used in NumBitsLeft. |
| 78 int trailing_zero_bytes_; |
| 79 |
| 80 // The valid data bits in the last byte, excluding stop bit and trailing |
| 81 // zero bits. It can never be 0. |
| 82 int data_bits_in_last_byte_; |
| 83 |
68 DISALLOW_COPY_AND_ASSIGN(H264BitReader); | 84 DISALLOW_COPY_AND_ASSIGN(H264BitReader); |
69 }; | 85 }; |
70 | 86 |
71 } // namespace content | 87 } // namespace content |
72 | 88 |
73 #endif // CONTENT_COMMON_GPU_MEDIA_H264_BIT_READER_H_ | 89 #endif // CONTENT_COMMON_GPU_MEDIA_H264_BIT_READER_H_ |
OLD | NEW |