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/buffers.h" | 10 #include "media/base/buffers.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 | 14 |
14 static const int kPesStartCode = 0x000001; | 15 static const int kPesStartCode = 0x000001; |
15 | 16 |
16 // Given that |time| is coded using 33 bits, | |
17 // UnrollTimestamp returns the corresponding unrolled timestamp. | |
18 // The unrolled timestamp is defined by: | |
19 // |time| + k * (2 ^ 33) | |
20 // where k is estimated so that the unrolled timestamp | |
21 // is as close as possible to |previous_unrolled_time|. | |
22 static int64 UnrollTimestamp(int64 previous_unrolled_time, int64 time) { | |
23 // Mpeg2 TS timestamps have an accuracy of 33 bits. | |
24 const int nbits = 33; | |
25 | |
26 // |timestamp| has a precision of |nbits| | |
27 // so make sure the highest bits are set to 0. | |
28 DCHECK_EQ((time >> nbits), 0); | |
29 | |
30 // Consider 3 possibilities to estimate the missing high bits of |time|. | |
31 int64 previous_unrolled_time_high = | |
32 (previous_unrolled_time >> nbits); | |
33 int64 time0 = ((previous_unrolled_time_high - 1) << nbits) | time; | |
34 int64 time1 = ((previous_unrolled_time_high + 0) << nbits) | time; | |
35 int64 time2 = ((previous_unrolled_time_high + 1) << nbits) | time; | |
36 | |
37 // Select the min absolute difference with the current time | |
38 // so as to ensure time continuity. | |
39 int64 diff0 = time0 - previous_unrolled_time; | |
40 int64 diff1 = time1 - previous_unrolled_time; | |
41 int64 diff2 = time2 - previous_unrolled_time; | |
42 if (diff0 < 0) | |
43 diff0 = -diff0; | |
44 if (diff1 < 0) | |
45 diff1 = -diff1; | |
46 if (diff2 < 0) | |
47 diff2 = -diff2; | |
48 | |
49 int64 unrolled_time; | |
50 int64 min_diff; | |
51 if (diff1 < diff0) { | |
52 unrolled_time = time1; | |
53 min_diff = diff1; | |
54 } else { | |
55 unrolled_time = time0; | |
56 min_diff = diff0; | |
57 } | |
58 if (diff2 < min_diff) | |
59 unrolled_time = time2; | |
60 | |
61 return unrolled_time; | |
62 } | |
63 | |
64 static bool IsTimestampSectionValid(int64 timestamp_section) { | 17 static bool IsTimestampSectionValid(int64 timestamp_section) { |
65 // |pts_section| has 40 bits: | 18 // |pts_section| has 40 bits: |
66 // - starting with either '0010' or '0011' or '0001' | 19 // - starting with either '0010' or '0011' or '0001' |
67 // - and ending with a marker bit. | 20 // - and ending with a marker bit. |
68 // See ITU H.222 standard - PES section. | 21 // See ITU H.222 standard - PES section. |
69 | 22 |
70 // Verify that all the marker bits are set to one. | 23 // Verify that all the marker bits are set to one. |
71 return ((timestamp_section & 0x1) != 0) && | 24 return ((timestamp_section & 0x1) != 0) && |
72 ((timestamp_section & 0x10000) != 0) && | 25 ((timestamp_section & 0x10000) != 0) && |
73 ((timestamp_section & 0x100000000) != 0); | 26 ((timestamp_section & 0x100000000) != 0); |
74 } | 27 } |
75 | 28 |
76 static int64 ConvertTimestampSectionToTimestamp(int64 timestamp_section) { | 29 static int64 ConvertTimestampSectionToTimestamp(int64 timestamp_section) { |
77 return (((timestamp_section >> 33) & 0x7) << 30) | | 30 return (((timestamp_section >> 33) & 0x7) << 30) | |
78 (((timestamp_section >> 17) & 0x7fff) << 15) | | 31 (((timestamp_section >> 17) & 0x7fff) << 15) | |
79 (((timestamp_section >> 1) & 0x7fff) << 0); | 32 (((timestamp_section >> 1) & 0x7fff) << 0); |
80 } | 33 } |
81 | 34 |
82 namespace media { | 35 namespace media { |
83 namespace mp2t { | 36 namespace mp2t { |
84 | 37 |
85 TsSectionPes::TsSectionPes(scoped_ptr<EsParser> es_parser) | 38 TsSectionPes::TsSectionPes(scoped_ptr<EsParser> es_parser, |
| 39 TimestampUnroller* timestamp_unroller) |
86 : es_parser_(es_parser.release()), | 40 : es_parser_(es_parser.release()), |
87 wait_for_pusi_(true), | 41 wait_for_pusi_(true), |
88 previous_pts_valid_(false), | 42 timestamp_unroller_(timestamp_unroller) { |
89 previous_pts_(0), | |
90 previous_dts_valid_(false), | |
91 previous_dts_(0) { | |
92 DCHECK(es_parser_); | 43 DCHECK(es_parser_); |
| 44 DCHECK(timestamp_unroller_); |
93 } | 45 } |
94 | 46 |
95 TsSectionPes::~TsSectionPes() { | 47 TsSectionPes::~TsSectionPes() { |
96 } | 48 } |
97 | 49 |
98 bool TsSectionPes::Parse(bool payload_unit_start_indicator, | 50 bool TsSectionPes::Parse(bool payload_unit_start_indicator, |
99 const uint8* buf, int size) { | 51 const uint8* buf, int size) { |
100 // Ignore partial PES. | 52 // Ignore partial PES. |
101 if (wait_for_pusi_ && !payload_unit_start_indicator) | 53 if (wait_for_pusi_ && !payload_unit_start_indicator) |
102 return true; | 54 return true; |
(...skipping 28 matching lines...) Expand all Loading... |
131 // Try emitting a packet since we might have a pending PES packet | 83 // Try emitting a packet since we might have a pending PES packet |
132 // with an undefined size. | 84 // with an undefined size. |
133 Emit(true); | 85 Emit(true); |
134 | 86 |
135 // Flush the underlying ES parser. | 87 // Flush the underlying ES parser. |
136 es_parser_->Flush(); | 88 es_parser_->Flush(); |
137 } | 89 } |
138 | 90 |
139 void TsSectionPes::Reset() { | 91 void TsSectionPes::Reset() { |
140 ResetPesState(); | 92 ResetPesState(); |
141 | |
142 previous_pts_valid_ = false; | |
143 previous_pts_ = 0; | |
144 previous_dts_valid_ = false; | |
145 previous_dts_ = 0; | |
146 | |
147 es_parser_->Reset(); | 93 es_parser_->Reset(); |
148 } | 94 } |
149 | 95 |
150 bool TsSectionPes::Emit(bool emit_for_unknown_size) { | 96 bool TsSectionPes::Emit(bool emit_for_unknown_size) { |
151 int raw_pes_size; | 97 int raw_pes_size; |
152 const uint8* raw_pes; | 98 const uint8* raw_pes; |
153 pes_byte_queue_.Peek(&raw_pes, &raw_pes_size); | 99 pes_byte_queue_.Peek(&raw_pes, &raw_pes_size); |
154 | 100 |
155 // A PES should be at least 6 bytes. | 101 // A PES should be at least 6 bytes. |
156 // Wait for more data to come if not enough bytes. | 102 // Wait for more data to come if not enough bytes. |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 RCHECK((((dts_section >> 36) & 0xf) == 0x1) && | 208 RCHECK((((dts_section >> 36) & 0xf) == 0x1) && |
263 IsTimestampSectionValid(dts_section)); | 209 IsTimestampSectionValid(dts_section)); |
264 is_pts_valid = true; | 210 is_pts_valid = true; |
265 is_dts_valid = true; | 211 is_dts_valid = true; |
266 } | 212 } |
267 | 213 |
268 // Convert and unroll the timestamps. | 214 // Convert and unroll the timestamps. |
269 base::TimeDelta media_pts(kNoTimestamp()); | 215 base::TimeDelta media_pts(kNoTimestamp()); |
270 DecodeTimestamp media_dts(kNoDecodeTimestamp()); | 216 DecodeTimestamp media_dts(kNoDecodeTimestamp()); |
271 if (is_pts_valid) { | 217 if (is_pts_valid) { |
272 int64 pts = ConvertTimestampSectionToTimestamp(pts_section); | 218 int64 pts = timestamp_unroller_->GetUnrolledTimestamp( |
273 if (previous_pts_valid_) | 219 ConvertTimestampSectionToTimestamp(pts_section)); |
274 pts = UnrollTimestamp(previous_pts_, pts); | |
275 previous_pts_ = pts; | |
276 previous_pts_valid_ = true; | |
277 media_pts = base::TimeDelta::FromMicroseconds((1000 * pts) / 90); | 220 media_pts = base::TimeDelta::FromMicroseconds((1000 * pts) / 90); |
278 } | 221 } |
279 if (is_dts_valid) { | 222 if (is_dts_valid) { |
280 int64 dts = ConvertTimestampSectionToTimestamp(dts_section); | 223 int64 dts = timestamp_unroller_->GetUnrolledTimestamp( |
281 if (previous_dts_valid_) | 224 ConvertTimestampSectionToTimestamp(dts_section)); |
282 dts = UnrollTimestamp(previous_dts_, dts); | |
283 previous_dts_ = dts; | |
284 previous_dts_valid_ = true; | |
285 media_dts = DecodeTimestamp::FromMicroseconds((1000 * dts) / 90); | 225 media_dts = DecodeTimestamp::FromMicroseconds((1000 * dts) / 90); |
286 } | 226 } |
287 | 227 |
288 // Discard the rest of the PES packet header. | 228 // Discard the rest of the PES packet header. |
289 // TODO(damienv): check if some info of the PES packet header are useful. | 229 // TODO(damienv): check if some info of the PES packet header are useful. |
290 DCHECK_EQ(bit_reader.bits_available() % 8, 0); | 230 DCHECK_EQ(bit_reader.bits_available() % 8, 0); |
291 int pes_header_remaining_size = pes_header_data_length - | 231 int pes_header_remaining_size = pes_header_data_length - |
292 (pes_header_start_size - bit_reader.bits_available() / 8); | 232 (pes_header_start_size - bit_reader.bits_available() / 8); |
293 RCHECK(pes_header_remaining_size >= 0); | 233 RCHECK(pes_header_remaining_size >= 0); |
294 | 234 |
295 // Read the PES packet. | 235 // Read the PES packet. |
296 DVLOG(LOG_LEVEL_PES) | 236 DVLOG(LOG_LEVEL_PES) |
297 << "Emit a reassembled PES:" | 237 << "Emit a reassembled PES:" |
298 << " size=" << es_size | 238 << " size=" << es_size |
299 << " pts=" << media_pts.InMilliseconds() | 239 << " pts=" << media_pts.InMilliseconds() |
300 << " dts=" << media_dts.InMilliseconds() | 240 << " dts=" << media_dts.InMilliseconds() |
301 << " data_alignment_indicator=" << data_alignment_indicator; | 241 << " data_alignment_indicator=" << data_alignment_indicator; |
302 return es_parser_->Parse(&raw_pes[es_offset], es_size, media_pts, media_dts); | 242 return es_parser_->Parse(&raw_pes[es_offset], es_size, media_pts, media_dts); |
303 } | 243 } |
304 | 244 |
305 void TsSectionPes::ResetPesState() { | 245 void TsSectionPes::ResetPesState() { |
306 pes_byte_queue_.Reset(); | 246 pes_byte_queue_.Reset(); |
307 wait_for_pusi_ = true; | 247 wait_for_pusi_ = true; |
308 } | 248 } |
309 | 249 |
310 } // namespace mp2t | 250 } // namespace mp2t |
311 } // namespace media | 251 } // namespace media |
OLD | NEW |