Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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 #include "media/mpeg2/mpeg2ts_pes.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/strings/string_number_conversions.h" | |
| 9 #include "media/base/bit_reader.h" | |
| 10 #include "media/base/buffers.h" | |
| 11 #include "media/mpeg2/es_parser.h" | |
| 12 #include "media/mpeg2/mpeg2ts_common.h" | |
| 13 | |
| 14 namespace { | |
|
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
nit: Use static to match the style of the rest of
damienv1
2013/09/09 23:29:45
Done.
| |
| 15 | |
| 16 const int kPesStartCode = 0x000001; | |
| 17 | |
| 18 int64 UnrollTimestamp(int64 previous_unrolled_time, | |
|
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
docs
damienv1
2013/09/09 23:29:45
Done.
| |
| 19 int64 time, | |
| 20 int nbits) { | |
| 21 // |timestamp| has a precision of |nbits| | |
| 22 // so make sure the highest bits are set to 0. | |
| 23 DCHECK_EQ((time >> nbits), 0); | |
| 24 | |
| 25 // Consider 3 possibilities to estimate the missing high bits of |time|. | |
| 26 int64 previous_unrolled_time_high = | |
| 27 (previous_unrolled_time >> nbits); | |
| 28 int64 time0 = ((previous_unrolled_time_high - 1) << nbits) | time; | |
| 29 int64 time1 = ((previous_unrolled_time_high + 0) << nbits) | time; | |
| 30 int64 time2 = ((previous_unrolled_time_high + 1) << nbits) | time; | |
| 31 | |
| 32 // Select the min absolute difference with the current time | |
| 33 // so as to ensure time continuity. | |
| 34 int64 diff0 = time0 - previous_unrolled_time; | |
| 35 int64 diff1 = time1 - previous_unrolled_time; | |
| 36 int64 diff2 = time2 - previous_unrolled_time; | |
| 37 if (diff0 < 0) { | |
|
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
nit: use std::abs() above instead of doing this.
damienv1
2013/09/09 23:29:45
I prefer to keep the way it is.
Before C++11, std:
| |
| 38 diff0 = -diff0; | |
| 39 } | |
| 40 if (diff1 < 0) { | |
| 41 diff1 = -diff1; | |
| 42 } | |
| 43 if (diff2 < 0) { | |
| 44 diff2 = -diff2; | |
| 45 } | |
| 46 | |
| 47 int64 unrolled_time; | |
| 48 int64 min_diff; | |
| 49 if (diff1 < diff0) { | |
| 50 unrolled_time = time1; | |
| 51 min_diff = diff1; | |
| 52 } else { | |
| 53 unrolled_time = time0; | |
| 54 min_diff = diff0; | |
| 55 } | |
| 56 if (diff2 < min_diff) { | |
|
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
nit: remove {}
damienv1
2013/09/09 23:29:45
Done.
| |
| 57 unrolled_time = time2; | |
| 58 } | |
| 59 LOG_IF(INFO, unrolled_time != time1) | |
|
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
Use DVLOG
damienv1
2013/09/09 23:29:45
I removed this piece of code.
| |
| 60 << "Unrolling time:" | |
| 61 << " previous_unrolled_time=" << previous_unrolled_time | |
| 62 << " time=" << time | |
| 63 << " unrolled_time=" << unrolled_time; | |
| 64 | |
| 65 return unrolled_time; | |
| 66 } | |
| 67 | |
| 68 bool IsTimestampSectionValid(int64 timestamp_section) { | |
| 69 // |pts_section| has 40 bits: | |
| 70 // - starting with either '0010' or '0011' or '0001' | |
| 71 // - and ending with a marker bit. | |
| 72 // See ITU H.222 standard - PES section. | |
| 73 | |
| 74 // Verify that all the marker bits are one. | |
| 75 bool is_valid = | |
|
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
nit: variable isn't necessary. Just use return her
damienv1
2013/09/09 23:29:45
Done.
| |
| 76 ((timestamp_section & 0x1) != 0) && | |
| 77 ((timestamp_section & 0x10000) != 0) && | |
| 78 ((timestamp_section & 0x100000000) != 0); | |
| 79 return is_valid; | |
| 80 } | |
| 81 | |
| 82 int64 ConvertTimestampSectionToTimestamp(int64 timestamp_section) { | |
| 83 int64 timestamp = | |
|
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
ditto
damienv1
2013/09/09 23:29:45
Done.
| |
| 84 (((timestamp_section >> 33) & 0x7) << 30) | | |
| 85 (((timestamp_section >> 17) & 0x7fff) << 15) | | |
| 86 (((timestamp_section >> 1) & 0x7fff) << 0); | |
| 87 return timestamp; | |
| 88 } | |
| 89 | |
| 90 } // namespace | |
| 91 | |
| 92 namespace media { | |
| 93 namespace mpeg2ts { | |
| 94 | |
| 95 Mpeg2TsPesParser::Mpeg2TsPesParser(EsParser* es_parser) | |
| 96 : es_parser_(es_parser), | |
| 97 wait_for_pusi_(true), | |
| 98 previous_pts_valid_(false), | |
| 99 previous_pts_(0), | |
| 100 previous_dts_valid_(false), | |
| 101 previous_dts_(0) { | |
| 102 } | |
| 103 | |
| 104 Mpeg2TsPesParser::~Mpeg2TsPesParser() { | |
| 105 } | |
| 106 | |
| 107 bool Mpeg2TsPesParser::Parse(bool payload_unit_start_indicator, | |
| 108 const uint8* buf, int size) { | |
| 109 if (wait_for_pusi_ && !payload_unit_start_indicator) { | |
| 110 // Ignore partial PES. | |
| 111 return true; | |
| 112 } | |
| 113 | |
| 114 bool parse_result = true; | |
| 115 if (payload_unit_start_indicator) { | |
| 116 // Try emitting a packet since we might have a pending PES packet | |
| 117 // with an undefined size. | |
| 118 // In this case, a unit is emitted when the next unit is coming. | |
| 119 int raw_pes_size = 0; | |
|
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
nit: initialization is not neccesary here since it
damienv1
2013/09/09 23:29:45
Done.
| |
| 120 const uint8* raw_pes = NULL; | |
| 121 pes_byte_queue_.Peek(&raw_pes, &raw_pes_size); | |
| 122 if (raw_pes_size > 0) | |
| 123 parse_result = Emit(true); | |
| 124 | |
| 125 // Reset the state. | |
| 126 ResetState(); | |
| 127 | |
| 128 // Update the state. | |
| 129 wait_for_pusi_ = false; | |
| 130 } | |
| 131 | |
| 132 // Add the data to the parser state. | |
| 133 if (size > 0) | |
| 134 pes_byte_queue_.Push(buf, size); | |
| 135 | |
| 136 // Try emitting the current PES packet. | |
| 137 parse_result = parse_result && Emit(false); | |
| 138 | |
| 139 return parse_result; | |
| 140 } | |
| 141 | |
| 142 void Mpeg2TsPesParser::Flush() { | |
| 143 // Try emitting a packet since we might have a pending PES packet | |
| 144 // with an undefined size. | |
| 145 Emit(true); | |
| 146 | |
| 147 // Flush the underlying ES parser. | |
| 148 es_parser_->Flush(); | |
| 149 } | |
| 150 | |
| 151 bool Mpeg2TsPesParser::Emit(bool emit_for_unknown_size) { | |
| 152 int raw_pes_size = 0; | |
|
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
nit: initialization not needed here and below.
damienv1
2013/09/09 23:29:45
Done.
| |
| 153 const uint8* raw_pes = NULL; | |
| 154 pes_byte_queue_.Peek(&raw_pes, &raw_pes_size); | |
| 155 | |
| 156 // A PES should be at least 6 bytes. | |
| 157 if (raw_pes_size < 6) { | |
| 158 // Wait for more data to come. | |
| 159 return true; | |
| 160 } | |
| 161 | |
| 162 // Check whether we have enough data to start parsing. | |
| 163 int pes_packet_length = | |
| 164 (static_cast<int>(raw_pes[4]) << 8) | | |
| 165 (static_cast<int>(raw_pes[5])); | |
| 166 if ((pes_packet_length == 0 && !emit_for_unknown_size) || | |
| 167 (pes_packet_length != 0 && raw_pes_size < pes_packet_length + 6)) { | |
| 168 // Wait for more data to come either because: | |
| 169 // - there are not enough bytes, | |
| 170 // - or the PES size is unknown and the "force emit" flag is not set. | |
| 171 // (PES size might be unknown for video PES packet). | |
| 172 return true; | |
| 173 } | |
| 174 DVLOG(LOG_LEVEL_PES) << "pes_packet_length=" << pes_packet_length; | |
| 175 | |
| 176 // Parse the packet. | |
| 177 bool parse_result = ParseInternal(); | |
| 178 | |
| 179 // Reset the state. | |
| 180 ResetState(); | |
| 181 | |
| 182 return parse_result; | |
| 183 } | |
| 184 | |
| 185 bool Mpeg2TsPesParser::ParseInternal() { | |
| 186 int raw_pes_size = 0; | |
|
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
ditto
damienv1
2013/09/09 23:29:45
Not relevant anymore (code removed).
| |
| 187 const uint8* raw_pes = NULL; | |
| 188 pes_byte_queue_.Peek(&raw_pes, &raw_pes_size); | |
| 189 | |
| 190 BitReader bit_reader(raw_pes, raw_pes_size); | |
| 191 int remaining_size = raw_pes_size; | |
| 192 | |
| 193 // Read up to the pes_packet_length (6 bytes). | |
| 194 if (remaining_size < 6) { | |
| 195 return false; | |
| 196 } | |
| 197 int packet_start_code_prefix; | |
| 198 DCHECK(bit_reader.ReadBits(24, &packet_start_code_prefix)); | |
|
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
Use RCHECK here and in every similar usage below.
damienv1
2013/09/09 23:29:45
RCHECK should be used if the remaining size is not
| |
| 199 int stream_id; | |
| 200 DCHECK(bit_reader.ReadBits(8, &stream_id)); | |
| 201 int pes_packet_length; | |
| 202 DCHECK(bit_reader.ReadBits(16, &pes_packet_length)); | |
| 203 remaining_size -= 6; | |
|
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
Instead of modifying remaining_size in several pla
damienv1
2013/09/09 23:29:45
Agree.
| |
| 204 | |
| 205 RCHECK(packet_start_code_prefix == kPesStartCode); | |
| 206 DVLOG(LOG_LEVEL_PES) << "stream_id=" << std::hex << stream_id << std::dec; | |
| 207 bool pes_packet_length_defined = (pes_packet_length != 0); | |
| 208 | |
| 209 // Ignore the PES for unknown stream IDs. | |
| 210 // See ITU H.222 Table 2-22 "Stream_id assignments" | |
| 211 bool is_audio_stream_id = ((stream_id & 0xe0) == 0xc0); | |
| 212 bool is_video_stream_id = ((stream_id & 0xf0) == 0xe0); | |
| 213 if (!is_audio_stream_id && !is_video_stream_id) { | |
| 214 return true; | |
| 215 } | |
| 216 | |
| 217 // Read up to "pes_header_data_length". | |
| 218 if (remaining_size < 3) { | |
| 219 return false; | |
| 220 } | |
| 221 int dummy_2; | |
| 222 DCHECK(bit_reader.ReadBits(2, &dummy_2)); | |
| 223 RCHECK(dummy_2 == 0x2); | |
| 224 int PES_scrambling_control; | |
| 225 DCHECK(bit_reader.ReadBits(2, &PES_scrambling_control)); | |
| 226 bool PES_priority; | |
| 227 DCHECK(bit_reader.ReadBits(1, &PES_priority)); | |
| 228 bool data_alignment_indicator; | |
| 229 DCHECK(bit_reader.ReadBits(1, &data_alignment_indicator)); | |
| 230 bool copyright; | |
| 231 DCHECK(bit_reader.ReadBits(1, ©right)); | |
| 232 bool original_or_copy; | |
| 233 DCHECK(bit_reader.ReadBits(1, &original_or_copy)); | |
| 234 int pts_dts_flags; | |
| 235 DCHECK(bit_reader.ReadBits(2, &pts_dts_flags)); | |
| 236 bool escr_flag; | |
| 237 DCHECK(bit_reader.ReadBits(1, &escr_flag)); | |
| 238 bool es_rate_flag; | |
| 239 DCHECK(bit_reader.ReadBits(1, &es_rate_flag)); | |
| 240 bool dsm_trick_mode_flag; | |
| 241 DCHECK(bit_reader.ReadBits(1, &dsm_trick_mode_flag)); | |
| 242 bool additional_copy_info_flag; | |
| 243 DCHECK(bit_reader.ReadBits(1, &additional_copy_info_flag)); | |
| 244 bool pes_crc_flag; | |
| 245 DCHECK(bit_reader.ReadBits(1, &pes_crc_flag)); | |
| 246 bool pes_extension_flag; | |
| 247 DCHECK(bit_reader.ReadBits(1, &pes_extension_flag)); | |
| 248 int pes_header_data_length; | |
| 249 DCHECK(bit_reader.ReadBits(8, &pes_header_data_length)); | |
| 250 remaining_size -= 3; | |
| 251 pes_packet_length -= 3; | |
| 252 | |
| 253 // Read the timing information section. | |
| 254 bool is_pts_valid = false; | |
| 255 bool is_dts_valid = false; | |
| 256 int64 pts_section = 0; | |
| 257 int64 dts_section = 0; | |
| 258 if (pts_dts_flags == 0x2) { | |
| 259 if (remaining_size < 5) { | |
|
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
nit: remove {} here and in several one-line ifs be
damienv1
2013/09/09 23:29:45
Done.
| |
| 260 return false; | |
| 261 } | |
| 262 DCHECK(bit_reader.ReadBits(40, &pts_section)); | |
| 263 RCHECK((((pts_section >> 36) & 0xf) == 0x2) && | |
| 264 IsTimestampSectionValid(pts_section)); | |
| 265 is_pts_valid = true; | |
| 266 remaining_size -= 5; | |
| 267 pes_packet_length -= 5; | |
| 268 pes_header_data_length -= 5; | |
| 269 } | |
| 270 if (pts_dts_flags == 0x3) { | |
| 271 if (remaining_size < 10) { | |
| 272 return false; | |
| 273 } | |
| 274 DCHECK(bit_reader.ReadBits(40, &pts_section)); | |
| 275 DCHECK(bit_reader.ReadBits(40, &dts_section)); | |
| 276 RCHECK((((pts_section >> 36) & 0xf) == 0x3) && | |
| 277 IsTimestampSectionValid(pts_section)); | |
| 278 RCHECK((((dts_section >> 36) & 0xf) == 0x1) && | |
| 279 IsTimestampSectionValid(dts_section)); | |
| 280 is_pts_valid = true; | |
| 281 is_dts_valid = true; | |
| 282 remaining_size -= 10; | |
| 283 pes_packet_length -= 10; | |
| 284 pes_header_data_length -= 10; | |
| 285 } | |
| 286 | |
| 287 // Convert and unroll the timestamps. | |
| 288 base::TimeDelta media_pts(kNoTimestamp()); | |
| 289 base::TimeDelta media_dts(kNoTimestamp()); | |
| 290 if (is_pts_valid) { | |
| 291 int64 pts = ConvertTimestampSectionToTimestamp(pts_section); | |
| 292 if (previous_pts_valid_) { | |
| 293 pts = UnrollTimestamp(previous_pts_, pts, 33); | |
| 294 } | |
| 295 previous_pts_ = pts; | |
| 296 previous_pts_valid_ = true; | |
| 297 media_pts = base::TimeDelta::FromMicroseconds((1000 * pts) / 90); | |
| 298 } | |
| 299 if (is_dts_valid) { | |
| 300 int64 dts = ConvertTimestampSectionToTimestamp(dts_section); | |
| 301 if (previous_dts_valid_) { | |
| 302 dts = UnrollTimestamp(previous_dts_, dts, 33); | |
| 303 } | |
| 304 previous_dts_ = dts; | |
| 305 previous_dts_valid_ = true; | |
| 306 media_dts = base::TimeDelta::FromMicroseconds((1000 * dts) / 90); | |
| 307 } | |
| 308 | |
| 309 // Discard the rest of the PES packet header. | |
| 310 // TODO(damienv): check if some info of the PES packet header are useful. | |
| 311 if (pes_header_data_length < 0) { | |
| 312 LOG(WARNING) << "Invalid PES header length"; | |
|
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
DVLOG
damienv1
2013/09/09 23:29:45
Done.
| |
| 313 return false; | |
| 314 } | |
| 315 if (remaining_size < pes_header_data_length) { | |
| 316 return false; | |
| 317 } | |
| 318 for (int k = 0; k < pes_header_data_length; k++) { | |
| 319 int dummy; | |
| 320 DCHECK(bit_reader.ReadBits(8, &dummy)); | |
| 321 } | |
| 322 remaining_size -= pes_header_data_length; | |
| 323 pes_packet_length -= pes_header_data_length; | |
| 324 | |
| 325 // Read the PES packet. | |
| 326 if (pes_packet_length_defined && pes_packet_length < 0) { | |
| 327 LOG(WARNING) << "Invalid ES length: " << pes_packet_length; | |
| 328 return false; | |
| 329 } | |
| 330 int es_size = pes_packet_length; | |
| 331 if (!pes_packet_length_defined) { | |
| 332 es_size = remaining_size; | |
| 333 } | |
| 334 DVLOG(LOG_LEVEL_PES) | |
| 335 << "Emit a reassembled PES:" | |
| 336 << " size=" << es_size | |
| 337 << " pts=" << media_pts.InMilliseconds() | |
| 338 << " dts=" << media_dts.InMilliseconds() | |
| 339 << " data_alignment_indicator=" << data_alignment_indicator; | |
| 340 | |
| 341 int es_offset = raw_pes_size - remaining_size; | |
| 342 if (es_parser_) { | |
| 343 es_parser_->Parse(&raw_pes[es_offset], es_size, | |
|
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
errors returned by the parser should be bubbled up
damienv1
2013/09/09 23:29:45
Done.
| |
| 344 media_pts, media_dts); | |
| 345 } | |
| 346 return true; | |
| 347 } | |
| 348 | |
| 349 void Mpeg2TsPesParser::ResetState() { | |
| 350 pes_byte_queue_.Reset(); | |
| 351 wait_for_pusi_ = true; | |
| 352 } | |
| 353 | |
| 354 } // namespace mpeg2ts | |
| 355 } // namespace media | |
| OLD | NEW |