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