OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #include "media/formats/mp2t/ts_section_pes.h" | 5 #include "media/formats/mp2t/ts_section_pes.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "media/base/bit_reader.h" | 9 #include "media/base/bit_reader.h" |
10 #include "media/base/timestamp_constants.h" | 10 #include "media/base/timestamp_constants.h" |
11 #include "media/formats/mp2t/es_parser.h" | 11 #include "media/formats/mp2t/es_parser.h" |
12 #include "media/formats/mp2t/mp2t_common.h" | 12 #include "media/formats/mp2t/mp2t_common.h" |
13 #include "media/formats/mp2t/timestamp_unroller.h" | 13 #include "media/formats/mp2t/timestamp_unroller.h" |
14 | 14 |
15 static const int kPesStartCode = 0x000001; | 15 static const int kPesStartCode = 0x000001; |
16 | 16 |
17 static bool IsTimestampSectionValid(int64 timestamp_section) { | 17 static bool IsTimestampSectionValid(int64_t timestamp_section) { |
18 // |pts_section| has 40 bits: | 18 // |pts_section| has 40 bits: |
19 // - starting with either '0010' or '0011' or '0001' | 19 // - starting with either '0010' or '0011' or '0001' |
20 // - and ending with a marker bit. | 20 // - and ending with a marker bit. |
21 // See ITU H.222 standard - PES section. | 21 // See ITU H.222 standard - PES section. |
22 | 22 |
23 // Verify that all the marker bits are set to one. | 23 // Verify that all the marker bits are set to one. |
24 return ((timestamp_section & 0x1) != 0) && | 24 return ((timestamp_section & 0x1) != 0) && |
25 ((timestamp_section & 0x10000) != 0) && | 25 ((timestamp_section & 0x10000) != 0) && |
26 ((timestamp_section & 0x100000000) != 0); | 26 ((timestamp_section & 0x100000000) != 0); |
27 } | 27 } |
28 | 28 |
29 static int64 ConvertTimestampSectionToTimestamp(int64 timestamp_section) { | 29 static int64_t ConvertTimestampSectionToTimestamp(int64_t timestamp_section) { |
30 return (((timestamp_section >> 33) & 0x7) << 30) | | 30 return (((timestamp_section >> 33) & 0x7) << 30) | |
31 (((timestamp_section >> 17) & 0x7fff) << 15) | | 31 (((timestamp_section >> 17) & 0x7fff) << 15) | |
32 (((timestamp_section >> 1) & 0x7fff) << 0); | 32 (((timestamp_section >> 1) & 0x7fff) << 0); |
33 } | 33 } |
34 | 34 |
35 namespace media { | 35 namespace media { |
36 namespace mp2t { | 36 namespace mp2t { |
37 | 37 |
38 TsSectionPes::TsSectionPes(scoped_ptr<EsParser> es_parser, | 38 TsSectionPes::TsSectionPes(scoped_ptr<EsParser> es_parser, |
39 TimestampUnroller* timestamp_unroller) | 39 TimestampUnroller* timestamp_unroller) |
40 : es_parser_(es_parser.release()), | 40 : es_parser_(es_parser.release()), |
41 wait_for_pusi_(true), | 41 wait_for_pusi_(true), |
42 timestamp_unroller_(timestamp_unroller) { | 42 timestamp_unroller_(timestamp_unroller) { |
43 DCHECK(es_parser_); | 43 DCHECK(es_parser_); |
44 DCHECK(timestamp_unroller_); | 44 DCHECK(timestamp_unroller_); |
45 } | 45 } |
46 | 46 |
47 TsSectionPes::~TsSectionPes() { | 47 TsSectionPes::~TsSectionPes() { |
48 } | 48 } |
49 | 49 |
50 bool TsSectionPes::Parse(bool payload_unit_start_indicator, | 50 bool TsSectionPes::Parse(bool payload_unit_start_indicator, |
51 const uint8* buf, int size) { | 51 const uint8_t* buf, |
| 52 int size) { |
52 // Ignore partial PES. | 53 // Ignore partial PES. |
53 if (wait_for_pusi_ && !payload_unit_start_indicator) | 54 if (wait_for_pusi_ && !payload_unit_start_indicator) |
54 return true; | 55 return true; |
55 | 56 |
56 bool parse_result = true; | 57 bool parse_result = true; |
57 if (payload_unit_start_indicator) { | 58 if (payload_unit_start_indicator) { |
58 // Try emitting a packet since we might have a pending PES packet | 59 // Try emitting a packet since we might have a pending PES packet |
59 // with an undefined size. | 60 // with an undefined size. |
60 // In this case, a unit is emitted when the next unit is coming. | 61 // In this case, a unit is emitted when the next unit is coming. |
61 int raw_pes_size; | 62 int raw_pes_size; |
62 const uint8* raw_pes; | 63 const uint8_t* raw_pes; |
63 pes_byte_queue_.Peek(&raw_pes, &raw_pes_size); | 64 pes_byte_queue_.Peek(&raw_pes, &raw_pes_size); |
64 if (raw_pes_size > 0) | 65 if (raw_pes_size > 0) |
65 parse_result = Emit(true); | 66 parse_result = Emit(true); |
66 | 67 |
67 // Reset the state. | 68 // Reset the state. |
68 ResetPesState(); | 69 ResetPesState(); |
69 | 70 |
70 // Update the state. | 71 // Update the state. |
71 wait_for_pusi_ = false; | 72 wait_for_pusi_ = false; |
72 } | 73 } |
(...skipping 15 matching lines...) Expand all Loading... |
88 es_parser_->Flush(); | 89 es_parser_->Flush(); |
89 } | 90 } |
90 | 91 |
91 void TsSectionPes::Reset() { | 92 void TsSectionPes::Reset() { |
92 ResetPesState(); | 93 ResetPesState(); |
93 es_parser_->Reset(); | 94 es_parser_->Reset(); |
94 } | 95 } |
95 | 96 |
96 bool TsSectionPes::Emit(bool emit_for_unknown_size) { | 97 bool TsSectionPes::Emit(bool emit_for_unknown_size) { |
97 int raw_pes_size; | 98 int raw_pes_size; |
98 const uint8* raw_pes; | 99 const uint8_t* raw_pes; |
99 pes_byte_queue_.Peek(&raw_pes, &raw_pes_size); | 100 pes_byte_queue_.Peek(&raw_pes, &raw_pes_size); |
100 | 101 |
101 // A PES should be at least 6 bytes. | 102 // A PES should be at least 6 bytes. |
102 // Wait for more data to come if not enough bytes. | 103 // Wait for more data to come if not enough bytes. |
103 if (raw_pes_size < 6) | 104 if (raw_pes_size < 6) |
104 return true; | 105 return true; |
105 | 106 |
106 // Check whether we have enough data to start parsing. | 107 // Check whether we have enough data to start parsing. |
107 int pes_packet_length = | 108 int pes_packet_length = |
108 (static_cast<int>(raw_pes[4]) << 8) | | 109 (static_cast<int>(raw_pes[4]) << 8) | |
(...skipping 10 matching lines...) Expand all Loading... |
119 | 120 |
120 // Parse the packet. | 121 // Parse the packet. |
121 bool parse_result = ParseInternal(raw_pes, raw_pes_size); | 122 bool parse_result = ParseInternal(raw_pes, raw_pes_size); |
122 | 123 |
123 // Reset the state. | 124 // Reset the state. |
124 ResetPesState(); | 125 ResetPesState(); |
125 | 126 |
126 return parse_result; | 127 return parse_result; |
127 } | 128 } |
128 | 129 |
129 bool TsSectionPes::ParseInternal(const uint8* raw_pes, int raw_pes_size) { | 130 bool TsSectionPes::ParseInternal(const uint8_t* raw_pes, int raw_pes_size) { |
130 BitReader bit_reader(raw_pes, raw_pes_size); | 131 BitReader bit_reader(raw_pes, raw_pes_size); |
131 | 132 |
132 // Read up to the pes_packet_length (6 bytes). | 133 // Read up to the pes_packet_length (6 bytes). |
133 int packet_start_code_prefix; | 134 int packet_start_code_prefix; |
134 int stream_id; | 135 int stream_id; |
135 int pes_packet_length; | 136 int pes_packet_length; |
136 RCHECK(bit_reader.ReadBits(24, &packet_start_code_prefix)); | 137 RCHECK(bit_reader.ReadBits(24, &packet_start_code_prefix)); |
137 RCHECK(bit_reader.ReadBits(8, &stream_id)); | 138 RCHECK(bit_reader.ReadBits(8, &stream_id)); |
138 RCHECK(bit_reader.ReadBits(16, &pes_packet_length)); | 139 RCHECK(bit_reader.ReadBits(16, &pes_packet_length)); |
139 | 140 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 // "6" for the 6 bytes read before and including |pes_packet_length|. | 193 // "6" for the 6 bytes read before and including |pes_packet_length|. |
193 // "3" for the 3 bytes read before and including |pes_header_data_length|. | 194 // "3" for the 3 bytes read before and including |pes_header_data_length|. |
194 int es_size = pes_packet_length - 3 - pes_header_data_length; | 195 int es_size = pes_packet_length - 3 - pes_header_data_length; |
195 int es_offset = 6 + 3 + pes_header_data_length; | 196 int es_offset = 6 + 3 + pes_header_data_length; |
196 RCHECK(es_size >= 0); | 197 RCHECK(es_size >= 0); |
197 RCHECK(es_offset + es_size <= raw_pes_size); | 198 RCHECK(es_offset + es_size <= raw_pes_size); |
198 | 199 |
199 // Read the timing information section. | 200 // Read the timing information section. |
200 bool is_pts_valid = false; | 201 bool is_pts_valid = false; |
201 bool is_dts_valid = false; | 202 bool is_dts_valid = false; |
202 int64 pts_section = 0; | 203 int64_t pts_section = 0; |
203 int64 dts_section = 0; | 204 int64_t dts_section = 0; |
204 if (pts_dts_flags == 0x2) { | 205 if (pts_dts_flags == 0x2) { |
205 RCHECK(bit_reader.ReadBits(40, &pts_section)); | 206 RCHECK(bit_reader.ReadBits(40, &pts_section)); |
206 RCHECK((((pts_section >> 36) & 0xf) == 0x2) && | 207 RCHECK((((pts_section >> 36) & 0xf) == 0x2) && |
207 IsTimestampSectionValid(pts_section)); | 208 IsTimestampSectionValid(pts_section)); |
208 is_pts_valid = true; | 209 is_pts_valid = true; |
209 } | 210 } |
210 if (pts_dts_flags == 0x3) { | 211 if (pts_dts_flags == 0x3) { |
211 RCHECK(bit_reader.ReadBits(40, &pts_section)); | 212 RCHECK(bit_reader.ReadBits(40, &pts_section)); |
212 RCHECK(bit_reader.ReadBits(40, &dts_section)); | 213 RCHECK(bit_reader.ReadBits(40, &dts_section)); |
213 RCHECK((((pts_section >> 36) & 0xf) == 0x3) && | 214 RCHECK((((pts_section >> 36) & 0xf) == 0x3) && |
214 IsTimestampSectionValid(pts_section)); | 215 IsTimestampSectionValid(pts_section)); |
215 RCHECK((((dts_section >> 36) & 0xf) == 0x1) && | 216 RCHECK((((dts_section >> 36) & 0xf) == 0x1) && |
216 IsTimestampSectionValid(dts_section)); | 217 IsTimestampSectionValid(dts_section)); |
217 is_pts_valid = true; | 218 is_pts_valid = true; |
218 is_dts_valid = true; | 219 is_dts_valid = true; |
219 } | 220 } |
220 | 221 |
221 // Convert and unroll the timestamps. | 222 // Convert and unroll the timestamps. |
222 base::TimeDelta media_pts(kNoTimestamp()); | 223 base::TimeDelta media_pts(kNoTimestamp()); |
223 DecodeTimestamp media_dts(kNoDecodeTimestamp()); | 224 DecodeTimestamp media_dts(kNoDecodeTimestamp()); |
224 if (is_pts_valid) { | 225 if (is_pts_valid) { |
225 int64 pts = timestamp_unroller_->GetUnrolledTimestamp( | 226 int64_t pts = timestamp_unroller_->GetUnrolledTimestamp( |
226 ConvertTimestampSectionToTimestamp(pts_section)); | 227 ConvertTimestampSectionToTimestamp(pts_section)); |
227 media_pts = base::TimeDelta::FromMicroseconds((1000 * pts) / 90); | 228 media_pts = base::TimeDelta::FromMicroseconds((1000 * pts) / 90); |
228 } | 229 } |
229 if (is_dts_valid) { | 230 if (is_dts_valid) { |
230 int64 dts = timestamp_unroller_->GetUnrolledTimestamp( | 231 int64_t dts = timestamp_unroller_->GetUnrolledTimestamp( |
231 ConvertTimestampSectionToTimestamp(dts_section)); | 232 ConvertTimestampSectionToTimestamp(dts_section)); |
232 media_dts = DecodeTimestamp::FromMicroseconds((1000 * dts) / 90); | 233 media_dts = DecodeTimestamp::FromMicroseconds((1000 * dts) / 90); |
233 } | 234 } |
234 | 235 |
235 // Discard the rest of the PES packet header. | 236 // Discard the rest of the PES packet header. |
236 // TODO(damienv): check if some info of the PES packet header are useful. | 237 // TODO(damienv): check if some info of the PES packet header are useful. |
237 DCHECK_EQ(bit_reader.bits_available() % 8, 0); | 238 DCHECK_EQ(bit_reader.bits_available() % 8, 0); |
238 int pes_header_remaining_size = pes_header_data_length - | 239 int pes_header_remaining_size = pes_header_data_length - |
239 (pes_header_start_size - bit_reader.bits_available() / 8); | 240 (pes_header_start_size - bit_reader.bits_available() / 8); |
240 RCHECK(pes_header_remaining_size >= 0); | 241 RCHECK(pes_header_remaining_size >= 0); |
241 | 242 |
242 // Read the PES packet. | 243 // Read the PES packet. |
243 DVLOG(LOG_LEVEL_PES) | 244 DVLOG(LOG_LEVEL_PES) |
244 << "Emit a reassembled PES:" | 245 << "Emit a reassembled PES:" |
245 << " size=" << es_size | 246 << " size=" << es_size |
246 << " pts=" << media_pts.InMilliseconds() | 247 << " pts=" << media_pts.InMilliseconds() |
247 << " dts=" << media_dts.InMilliseconds() | 248 << " dts=" << media_dts.InMilliseconds() |
248 << " data_alignment_indicator=" << data_alignment_indicator; | 249 << " data_alignment_indicator=" << data_alignment_indicator; |
249 return es_parser_->Parse(&raw_pes[es_offset], es_size, media_pts, media_dts); | 250 return es_parser_->Parse(&raw_pes[es_offset], es_size, media_pts, media_dts); |
250 } | 251 } |
251 | 252 |
252 void TsSectionPes::ResetPesState() { | 253 void TsSectionPes::ResetPesState() { |
253 pes_byte_queue_.Reset(); | 254 pes_byte_queue_.Reset(); |
254 wait_for_pusi_ = true; | 255 wait_for_pusi_ = true; |
255 } | 256 } |
256 | 257 |
257 } // namespace mp2t | 258 } // namespace mp2t |
258 } // namespace media | 259 } // namespace media |
OLD | NEW |