Index: media/mpeg2/mpeg2ts_pmt.cc |
diff --git a/media/mpeg2/mpeg2ts_pmt.cc b/media/mpeg2/mpeg2ts_pmt.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b2497e05ea5dc82f633a2cbc218daa0ab1b694a4 |
--- /dev/null |
+++ b/media/mpeg2/mpeg2ts_pmt.cc |
@@ -0,0 +1,159 @@ |
+// 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_pmt.h" |
+ |
+#include <map> |
+ |
+#include "base/logging.h" |
+#include "media/base/bit_reader.h" |
+#include "media/mpeg2/mpeg2ts_common.h" |
+ |
+namespace media { |
+namespace mpeg2ts { |
+ |
+Mpeg2TsPmtParser::Mpeg2TsPmtParser(RegisterPesCb register_pes_cb) |
+ : register_pes_cb_(register_pes_cb) { |
+} |
+ |
+Mpeg2TsPmtParser::~Mpeg2TsPmtParser() { |
+} |
+ |
+bool Mpeg2TsPmtParser::ParsePsiSection(BitReader* bit_reader) { |
+ // Read up to |last_section_number|. |
+ DCHECK_GE(bit_reader->bits_available(), 8 * 8); |
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
RCHECK here and everywhere below.
damienv1
2013/09/09 23:29:45
Please let me know if you prefer using RCHECK at t
|
+ int table_id; |
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
group declarations above to improve readability.
damienv1
2013/09/09 23:29:45
Done.
|
+ DCHECK(bit_reader->ReadBits(8, &table_id)); |
+ bool section_syntax_indicator; |
+ DCHECK(bit_reader->ReadBits(1, §ion_syntax_indicator)); |
+ bool dummy_zero; |
+ DCHECK(bit_reader->ReadBits(1, &dummy_zero)); |
+ int reserved; |
+ DCHECK(bit_reader->ReadBits(2, &reserved)); |
+ int section_length; |
+ DCHECK(bit_reader->ReadBits(12, §ion_length)); |
+ int program_number; |
+ DCHECK(bit_reader->ReadBits(16, &program_number)); |
+ DCHECK(bit_reader->ReadBits(2, &reserved)); |
+ int version_number; |
+ DCHECK(bit_reader->ReadBits(5, &version_number)); |
+ bool current_next_indicator; |
+ DCHECK(bit_reader->ReadBits(1, ¤t_next_indicator)); |
+ int section_number; |
+ DCHECK(bit_reader->ReadBits(8, §ion_number)); |
+ int last_section_number; |
+ DCHECK(bit_reader->ReadBits(8, &last_section_number)); |
+ section_length -= 5; |
+ |
+ // Perform a few verifications: |
+ // - table ID should be 2 for a PMT. |
+ // - section_syntax_indicator should be one. |
+ // - section length should not exceed 1021. |
+ RCHECK(table_id == 0x2); |
+ RCHECK(section_syntax_indicator); |
+ RCHECK(!dummy_zero); |
+ RCHECK(section_length <= 1021); |
+ RCHECK(section_number == 0); |
+ RCHECK(last_section_number == 0); |
+ |
+ // TODO(damienv): |
+ // Verify that there is no mismatch between the program number |
+ // and the program number that was provided in a PAT for the current PMT. |
+ |
+ // Make sure there are enough bits for the rest of the section. |
+ int remaining_size = bit_reader->bits_available() / 8; |
+ if (remaining_size < section_length) { |
+ LOG(WARNING) << "remaining_size=" << remaining_size; |
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
DVLOG here and below
damienv1
2013/09/09 23:29:45
Done.
|
+ return false; |
+ } |
+ LOG_IF(WARNING, remaining_size > section_length) |
+ << "Trailing bytes after the PMT: nbytes=" |
+ << (remaining_size - section_length); |
+ |
+ // Read the end of the fixed length section. |
+ if (section_length < 4) { |
+ LOG(WARNING) << "Section length too small"; |
+ return false; |
+ } |
+ DCHECK(bit_reader->ReadBits(3, &reserved)); |
+ int pcr_pid; |
+ DCHECK(bit_reader->ReadBits(13, &pcr_pid)); |
+ DCHECK(bit_reader->ReadBits(4, &reserved)); |
+ int program_info_length; |
+ DCHECK(bit_reader->ReadBits(12, &program_info_length)); |
+ RCHECK(program_info_length < 1024); |
+ section_length -= 4; |
+ |
+ // Read the program info descriptor. |
+ // TODO(damienv): check wether any of the descriptors could be useful. |
+ // Defined in section 2.6 of ISO-13818. |
+ if (section_length < program_info_length) { |
+ LOG(WARNING) << "Section length too small"; |
+ return false; |
+ } |
+ for (int k = 0; k < program_info_length; k++) { |
+ int dummy; |
+ DCHECK(bit_reader->ReadBits(8, &dummy)); |
+ } |
+ section_length -= program_info_length; |
+ |
+ // Read the ES description table. |
+ // 5 bytes: minimum size of an ES descriptor. |
+ std::map<int, int> pid_map; |
+ while (section_length >= 5) { |
+ int stream_type; |
+ DCHECK(bit_reader->ReadBits(8, &stream_type)); |
+ int reserved; |
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
group declarations
damienv1
2013/09/09 23:29:45
Done.
|
+ DCHECK(bit_reader->ReadBits(3, &reserved)); |
+ int pid_es; |
+ DCHECK(bit_reader->ReadBits(13, &pid_es)); |
+ DCHECK(bit_reader->ReadBits(4, &reserved)); |
+ int es_info_length; |
+ DCHECK(bit_reader->ReadBits(12, &es_info_length)); |
+ section_length -= 5; |
+ |
+ // Do not register the PID right away. |
+ // Wait for the end of the section to be fully parsed |
+ // to make sure there is no error. |
+ pid_map.insert(std::pair<int,int>(pid_es, stream_type)); |
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
nit: space after ,
damienv1
2013/09/09 23:29:45
Done.
|
+ |
+ // Read the ES info descriptors. |
+ // TODO(damienv): check wether any of the descriptors could be useful. |
+ // Defined in section 2.6 of ISO-13818. |
+ if (section_length < es_info_length) { |
+ LOG(WARNING) << "section_length=" << section_length |
+ << " es_info_length=" << es_info_length; |
+ return false; |
+ } |
+ for (int k = 0; k < es_info_length; k++) { |
+ int dummy; |
+ DCHECK(bit_reader->ReadBits(8, &dummy)); |
+ } |
+ section_length -= es_info_length; |
+ } |
+ |
+ // Read the CRC. |
+ if (section_length < 4) { |
+ LOG(WARNING) << "Section length too small: " << section_length; |
+ return false; |
+ } |
+ int crc32; |
+ DCHECK(bit_reader->ReadBits(32, &crc32)); |
+ section_length -= 4; |
+ |
+ // Should not happen if the PMT size was correctly encoded into the stream. |
+ LOG_IF(WARNING, section_length != 0) |
+ << "section_length=" << section_length; |
+ |
+ // Once the PMT has been proved to be correct, register the PIDs. |
+ for (std::map<int, int>::iterator it = pid_map.begin(); |
+ it != pid_map.end(); ++it) { |
+ register_pes_cb_.Run(it->first, it->second); |
+ } |
+ |
+ return true; |
+} |
+ |
+} // namespace mpeg2ts |
+} // namespace media |