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