Chromium Code Reviews| Index: media/mpeg2/mpeg2ts_pes.cc |
| diff --git a/media/mpeg2/mpeg2ts_pes.cc b/media/mpeg2/mpeg2ts_pes.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..955ad3a58f4cf01fbca33353181d787570461617 |
| --- /dev/null |
| +++ b/media/mpeg2/mpeg2ts_pes.cc |
| @@ -0,0 +1,355 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/mpeg2/mpeg2ts_pes.h" |
| + |
| +#include "base/logging.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "media/base/bit_reader.h" |
| +#include "media/base/buffers.h" |
| +#include "media/mpeg2/es_parser.h" |
| +#include "media/mpeg2/mpeg2ts_common.h" |
| + |
| +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.
|
| + |
| +const int kPesStartCode = 0x000001; |
| + |
| +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.
|
| + int64 time, |
| + int nbits) { |
| + // |timestamp| has a precision of |nbits| |
| + // so make sure the highest bits are set to 0. |
| + DCHECK_EQ((time >> nbits), 0); |
| + |
| + // Consider 3 possibilities to estimate the missing high bits of |time|. |
| + int64 previous_unrolled_time_high = |
| + (previous_unrolled_time >> nbits); |
| + int64 time0 = ((previous_unrolled_time_high - 1) << nbits) | time; |
| + int64 time1 = ((previous_unrolled_time_high + 0) << nbits) | time; |
| + int64 time2 = ((previous_unrolled_time_high + 1) << nbits) | time; |
| + |
| + // Select the min absolute difference with the current time |
| + // so as to ensure time continuity. |
| + int64 diff0 = time0 - previous_unrolled_time; |
| + int64 diff1 = time1 - previous_unrolled_time; |
| + int64 diff2 = time2 - previous_unrolled_time; |
| + 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:
|
| + diff0 = -diff0; |
| + } |
| + if (diff1 < 0) { |
| + diff1 = -diff1; |
| + } |
| + if (diff2 < 0) { |
| + diff2 = -diff2; |
| + } |
| + |
| + int64 unrolled_time; |
| + int64 min_diff; |
| + if (diff1 < diff0) { |
| + unrolled_time = time1; |
| + min_diff = diff1; |
| + } else { |
| + unrolled_time = time0; |
| + min_diff = diff0; |
| + } |
| + if (diff2 < min_diff) { |
|
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
nit: remove {}
damienv1
2013/09/09 23:29:45
Done.
|
| + unrolled_time = time2; |
| + } |
| + 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.
|
| + << "Unrolling time:" |
| + << " previous_unrolled_time=" << previous_unrolled_time |
| + << " time=" << time |
| + << " unrolled_time=" << unrolled_time; |
| + |
| + return unrolled_time; |
| +} |
| + |
| +bool IsTimestampSectionValid(int64 timestamp_section) { |
| + // |pts_section| has 40 bits: |
| + // - starting with either '0010' or '0011' or '0001' |
| + // - and ending with a marker bit. |
| + // See ITU H.222 standard - PES section. |
| + |
| + // Verify that all the marker bits are one. |
| + 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.
|
| + ((timestamp_section & 0x1) != 0) && |
| + ((timestamp_section & 0x10000) != 0) && |
| + ((timestamp_section & 0x100000000) != 0); |
| + return is_valid; |
| +} |
| + |
| +int64 ConvertTimestampSectionToTimestamp(int64 timestamp_section) { |
| + int64 timestamp = |
|
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
ditto
damienv1
2013/09/09 23:29:45
Done.
|
| + (((timestamp_section >> 33) & 0x7) << 30) | |
| + (((timestamp_section >> 17) & 0x7fff) << 15) | |
| + (((timestamp_section >> 1) & 0x7fff) << 0); |
| + return timestamp; |
| +} |
| + |
| +} // namespace |
| + |
| +namespace media { |
| +namespace mpeg2ts { |
| + |
| +Mpeg2TsPesParser::Mpeg2TsPesParser(EsParser* es_parser) |
| + : es_parser_(es_parser), |
| + wait_for_pusi_(true), |
| + previous_pts_valid_(false), |
| + previous_pts_(0), |
| + previous_dts_valid_(false), |
| + previous_dts_(0) { |
| +} |
| + |
| +Mpeg2TsPesParser::~Mpeg2TsPesParser() { |
| +} |
| + |
| +bool Mpeg2TsPesParser::Parse(bool payload_unit_start_indicator, |
| + const uint8* buf, int size) { |
| + if (wait_for_pusi_ && !payload_unit_start_indicator) { |
| + // Ignore partial PES. |
| + return true; |
| + } |
| + |
| + bool parse_result = true; |
| + if (payload_unit_start_indicator) { |
| + // Try emitting a packet since we might have a pending PES packet |
| + // with an undefined size. |
| + // In this case, a unit is emitted when the next unit is coming. |
| + 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.
|
| + const uint8* raw_pes = NULL; |
| + pes_byte_queue_.Peek(&raw_pes, &raw_pes_size); |
| + if (raw_pes_size > 0) |
| + parse_result = Emit(true); |
| + |
| + // Reset the state. |
| + ResetState(); |
| + |
| + // Update the state. |
| + wait_for_pusi_ = false; |
| + } |
| + |
| + // Add the data to the parser state. |
| + if (size > 0) |
| + pes_byte_queue_.Push(buf, size); |
| + |
| + // Try emitting the current PES packet. |
| + parse_result = parse_result && Emit(false); |
| + |
| + return parse_result; |
| +} |
| + |
| +void Mpeg2TsPesParser::Flush() { |
| + // Try emitting a packet since we might have a pending PES packet |
| + // with an undefined size. |
| + Emit(true); |
| + |
| + // Flush the underlying ES parser. |
| + es_parser_->Flush(); |
| +} |
| + |
| +bool Mpeg2TsPesParser::Emit(bool emit_for_unknown_size) { |
| + 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.
|
| + const uint8* raw_pes = NULL; |
| + pes_byte_queue_.Peek(&raw_pes, &raw_pes_size); |
| + |
| + // A PES should be at least 6 bytes. |
| + if (raw_pes_size < 6) { |
| + // Wait for more data to come. |
| + return true; |
| + } |
| + |
| + // Check whether we have enough data to start parsing. |
| + int pes_packet_length = |
| + (static_cast<int>(raw_pes[4]) << 8) | |
| + (static_cast<int>(raw_pes[5])); |
| + if ((pes_packet_length == 0 && !emit_for_unknown_size) || |
| + (pes_packet_length != 0 && raw_pes_size < pes_packet_length + 6)) { |
| + // Wait for more data to come either because: |
| + // - there are not enough bytes, |
| + // - or the PES size is unknown and the "force emit" flag is not set. |
| + // (PES size might be unknown for video PES packet). |
| + return true; |
| + } |
| + DVLOG(LOG_LEVEL_PES) << "pes_packet_length=" << pes_packet_length; |
| + |
| + // Parse the packet. |
| + bool parse_result = ParseInternal(); |
| + |
| + // Reset the state. |
| + ResetState(); |
| + |
| + return parse_result; |
| +} |
| + |
| +bool Mpeg2TsPesParser::ParseInternal() { |
| + 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).
|
| + const uint8* raw_pes = NULL; |
| + pes_byte_queue_.Peek(&raw_pes, &raw_pes_size); |
| + |
| + BitReader bit_reader(raw_pes, raw_pes_size); |
| + int remaining_size = raw_pes_size; |
| + |
| + // Read up to the pes_packet_length (6 bytes). |
| + if (remaining_size < 6) { |
| + return false; |
| + } |
| + int packet_start_code_prefix; |
| + 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
|
| + int stream_id; |
| + DCHECK(bit_reader.ReadBits(8, &stream_id)); |
| + int pes_packet_length; |
| + DCHECK(bit_reader.ReadBits(16, &pes_packet_length)); |
| + 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.
|
| + |
| + RCHECK(packet_start_code_prefix == kPesStartCode); |
| + DVLOG(LOG_LEVEL_PES) << "stream_id=" << std::hex << stream_id << std::dec; |
| + bool pes_packet_length_defined = (pes_packet_length != 0); |
| + |
| + // Ignore the PES for unknown stream IDs. |
| + // See ITU H.222 Table 2-22 "Stream_id assignments" |
| + bool is_audio_stream_id = ((stream_id & 0xe0) == 0xc0); |
| + bool is_video_stream_id = ((stream_id & 0xf0) == 0xe0); |
| + if (!is_audio_stream_id && !is_video_stream_id) { |
| + return true; |
| + } |
| + |
| + // Read up to "pes_header_data_length". |
| + if (remaining_size < 3) { |
| + return false; |
| + } |
| + int dummy_2; |
| + DCHECK(bit_reader.ReadBits(2, &dummy_2)); |
| + RCHECK(dummy_2 == 0x2); |
| + int PES_scrambling_control; |
| + DCHECK(bit_reader.ReadBits(2, &PES_scrambling_control)); |
| + bool PES_priority; |
| + DCHECK(bit_reader.ReadBits(1, &PES_priority)); |
| + bool data_alignment_indicator; |
| + DCHECK(bit_reader.ReadBits(1, &data_alignment_indicator)); |
| + bool copyright; |
| + DCHECK(bit_reader.ReadBits(1, ©right)); |
| + bool original_or_copy; |
| + DCHECK(bit_reader.ReadBits(1, &original_or_copy)); |
| + int pts_dts_flags; |
| + DCHECK(bit_reader.ReadBits(2, &pts_dts_flags)); |
| + bool escr_flag; |
| + DCHECK(bit_reader.ReadBits(1, &escr_flag)); |
| + bool es_rate_flag; |
| + DCHECK(bit_reader.ReadBits(1, &es_rate_flag)); |
| + bool dsm_trick_mode_flag; |
| + DCHECK(bit_reader.ReadBits(1, &dsm_trick_mode_flag)); |
| + bool additional_copy_info_flag; |
| + DCHECK(bit_reader.ReadBits(1, &additional_copy_info_flag)); |
| + bool pes_crc_flag; |
| + DCHECK(bit_reader.ReadBits(1, &pes_crc_flag)); |
| + bool pes_extension_flag; |
| + DCHECK(bit_reader.ReadBits(1, &pes_extension_flag)); |
| + int pes_header_data_length; |
| + DCHECK(bit_reader.ReadBits(8, &pes_header_data_length)); |
| + remaining_size -= 3; |
| + pes_packet_length -= 3; |
| + |
| + // Read the timing information section. |
| + bool is_pts_valid = false; |
| + bool is_dts_valid = false; |
| + int64 pts_section = 0; |
| + int64 dts_section = 0; |
| + if (pts_dts_flags == 0x2) { |
| + 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.
|
| + return false; |
| + } |
| + DCHECK(bit_reader.ReadBits(40, &pts_section)); |
| + RCHECK((((pts_section >> 36) & 0xf) == 0x2) && |
| + IsTimestampSectionValid(pts_section)); |
| + is_pts_valid = true; |
| + remaining_size -= 5; |
| + pes_packet_length -= 5; |
| + pes_header_data_length -= 5; |
| + } |
| + if (pts_dts_flags == 0x3) { |
| + if (remaining_size < 10) { |
| + return false; |
| + } |
| + DCHECK(bit_reader.ReadBits(40, &pts_section)); |
| + DCHECK(bit_reader.ReadBits(40, &dts_section)); |
| + RCHECK((((pts_section >> 36) & 0xf) == 0x3) && |
| + IsTimestampSectionValid(pts_section)); |
| + RCHECK((((dts_section >> 36) & 0xf) == 0x1) && |
| + IsTimestampSectionValid(dts_section)); |
| + is_pts_valid = true; |
| + is_dts_valid = true; |
| + remaining_size -= 10; |
| + pes_packet_length -= 10; |
| + pes_header_data_length -= 10; |
| + } |
| + |
| + // Convert and unroll the timestamps. |
| + base::TimeDelta media_pts(kNoTimestamp()); |
| + base::TimeDelta media_dts(kNoTimestamp()); |
| + if (is_pts_valid) { |
| + int64 pts = ConvertTimestampSectionToTimestamp(pts_section); |
| + if (previous_pts_valid_) { |
| + pts = UnrollTimestamp(previous_pts_, pts, 33); |
| + } |
| + previous_pts_ = pts; |
| + previous_pts_valid_ = true; |
| + media_pts = base::TimeDelta::FromMicroseconds((1000 * pts) / 90); |
| + } |
| + if (is_dts_valid) { |
| + int64 dts = ConvertTimestampSectionToTimestamp(dts_section); |
| + if (previous_dts_valid_) { |
| + dts = UnrollTimestamp(previous_dts_, dts, 33); |
| + } |
| + previous_dts_ = dts; |
| + previous_dts_valid_ = true; |
| + media_dts = base::TimeDelta::FromMicroseconds((1000 * dts) / 90); |
| + } |
| + |
| + // Discard the rest of the PES packet header. |
| + // TODO(damienv): check if some info of the PES packet header are useful. |
| + if (pes_header_data_length < 0) { |
| + 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.
|
| + return false; |
| + } |
| + if (remaining_size < pes_header_data_length) { |
| + return false; |
| + } |
| + for (int k = 0; k < pes_header_data_length; k++) { |
| + int dummy; |
| + DCHECK(bit_reader.ReadBits(8, &dummy)); |
| + } |
| + remaining_size -= pes_header_data_length; |
| + pes_packet_length -= pes_header_data_length; |
| + |
| + // Read the PES packet. |
| + if (pes_packet_length_defined && pes_packet_length < 0) { |
| + LOG(WARNING) << "Invalid ES length: " << pes_packet_length; |
| + return false; |
| + } |
| + int es_size = pes_packet_length; |
| + if (!pes_packet_length_defined) { |
| + es_size = remaining_size; |
| + } |
| + DVLOG(LOG_LEVEL_PES) |
| + << "Emit a reassembled PES:" |
| + << " size=" << es_size |
| + << " pts=" << media_pts.InMilliseconds() |
| + << " dts=" << media_dts.InMilliseconds() |
| + << " data_alignment_indicator=" << data_alignment_indicator; |
| + |
| + int es_offset = raw_pes_size - remaining_size; |
| + if (es_parser_) { |
| + 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.
|
| + media_pts, media_dts); |
| + } |
| + return true; |
| +} |
| + |
| +void Mpeg2TsPesParser::ResetState() { |
| + pes_byte_queue_.Reset(); |
| + wait_for_pusi_ = true; |
| +} |
| + |
| +} // namespace mpeg2ts |
| +} // namespace media |