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 byte. |
Ami GONE FROM CHROMIUM
2012/08/01 23:04:00
Why is this API reasonable, given that these bits
xiaomings
2012/08/01 23:39:54
This is actually used in h264_parser.cc:1081 for c
| |
47 off_t NumBitsLeft() const; | |
42 | 48 |
43 // See the definition of more_rbsp_data() in spec. | 49 // See the definition of more_rbsp_data() in spec. |
44 bool HasMoreRBSPData(); | 50 bool HasMoreRBSPData() const; |
Ami GONE FROM CHROMIUM
2012/08/01 23:04:00
This name no longer makes as much sense since you'
xiaomings
2012/08/01 23:39:54
Removed.
On 2012/08/01 23:04:00, Ami Fischman wro
| |
45 | 51 |
46 private: | 52 private: |
47 // Advance to the next byte, loading it into curr_byte_. | 53 // Advance to the next byte, loading it into curr_byte_. |
48 // Return false on end of stream. | 54 // Return false on end of stream. |
49 bool UpdateCurrByte(); | 55 bool UpdateCurrByte(); |
50 | 56 |
57 // Remove the trailing zero bits if there are any. The trailing zero bits | |
58 // can spread among more than byte. Then remove the stop bit. So the bit | |
59 // stream 11001100 00000000 will become 11001. | |
60 // It returns true if there are still data bits left. | |
61 bool RemoveTrailingZeroAndStopBit(); | |
62 | |
51 // Pointer to the next unread (not in curr_byte_) byte in the stream. | 63 // Pointer to the next unread (not in curr_byte_) byte in the stream. |
52 const uint8* data_; | 64 const uint8* data_; |
53 | 65 |
54 // Bytes left in the stream (without the curr_byte_). | 66 // Bytes left in the stream (without the curr_byte_). |
55 off_t bytes_left_; | 67 off_t bytes_left_; |
56 | 68 |
57 // Contents of the current byte; first unread bit starting at position | 69 // Contents of the current byte; first unread bit starting at position |
58 // 8 - num_remaining_bits_in_curr_byte_ from MSB. | 70 // 8 - num_remaining_bits_in_curr_byte_ from MSB. |
59 int curr_byte_; | 71 int curr_byte_; |
60 | 72 |
61 // Number of bits remaining in curr_byte_ | 73 // Number of bits remaining in curr_byte_ |
62 int num_remaining_bits_in_curr_byte_; | 74 int num_remaining_bits_in_curr_byte_; |
63 | 75 |
64 // Used in emulation prevention three byte detection (see spec). | 76 // Used in emulation prevention three byte detection (see spec). |
65 // Initially set to 0xffff to accept all initial two-byte sequences. | 77 // Initially set to 0xffff to accept all initial two-byte sequences. |
66 int prev_two_bytes_; | 78 int prev_two_bytes_; |
67 | 79 |
80 // Save the number of trailing zero bytes to be used in NumBitsLeft. | |
81 int trailing_zero_bytes_; | |
82 | |
83 // The valid data bits in the last byte, this doesn't take stop bit and | |
84 // trailing zero bits into account. It can never be 0. | |
Ami GONE FROM CHROMIUM
2012/08/01 23:04:00
"this doesn't take ... into account"
is pretty con
xiaomings
2012/08/01 23:39:54
Modified.
On 2012/08/01 23:04:00, Ami Fischman wr
| |
85 int data_bits_in_last_byte_; | |
86 | |
68 DISALLOW_COPY_AND_ASSIGN(H264BitReader); | 87 DISALLOW_COPY_AND_ASSIGN(H264BitReader); |
69 }; | 88 }; |
70 | 89 |
71 } // namespace content | 90 } // namespace content |
72 | 91 |
73 #endif // CONTENT_COMMON_GPU_MEDIA_H264_BIT_READER_H_ | 92 #endif // CONTENT_COMMON_GPU_MEDIA_H264_BIT_READER_H_ |
OLD | NEW |