Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1087)

Unified Diff: media/mpeg2/mpeg2ts_pmt.cc

Issue 23566013: Mpeg2 TS stream parser for media source. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..3b3af14f1dbe1df0dc27934207fb523ce94d0de4
--- /dev/null
+++ b/media/mpeg2/mpeg2ts_pmt.cc
@@ -0,0 +1,176 @@
+// 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"
+
+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 the section length.
+ DCHECK_GE(bit_reader->bits_available(), 3*8);
+ int table_id;
+ DCHECK(bit_reader->ReadBits(8, &table_id));
+ if (table_id != 0x2) {
+ // Table ID should be 2 for a PMT.
+ LOG(WARNING) << "Unexpected table id: " << table_id;
+ return false;
+ }
+ bool section_syntax_indicator;
+ DCHECK(bit_reader->ReadBits(1, &section_syntax_indicator));
+ if (!section_syntax_indicator) {
+ LOG(WARNING) << "Unexpected section_syntax_indicator";
+ return false;
+ }
+ bool dummy_zero;
+ DCHECK(bit_reader->ReadBits(1, &dummy_zero));
+ if (dummy_zero) {
+ LOG(WARNING) << "Unexpected bit";
+ return false;
+ }
+ int reserved;
+ DCHECK(bit_reader->ReadBits(2, &reserved));
+ int section_length;
+ DCHECK(bit_reader->ReadBits(12, &section_length));
+ if (section_length >= 1021) {
+ return false;
+ }
+
+ // Read the rest of the fixed size section.
+ if (section_length < 5) {
+ LOG(WARNING) << "Section length too small";
+ return false;
+ }
+ 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, &current_next_indicator));
+ int section_number;
+ DCHECK(bit_reader->ReadBits(8, &section_number));
+ int last_section_number;
+ DCHECK(bit_reader->ReadBits(8, &last_section_number));
+ section_length -= 5;
+
+ if (section_number != 0) {
+ // Should be 0 for a PMT.
+ return false;
+ }
+ if (last_section_number != 0) {
+ // Should be 0 for a PMT.
+ return false;
+ }
+ // 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.
+ // - What if the program number is not registered yet ?
+ // Do we want to add an implicitly defined program number ?
+
+ // 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));
+ if (program_info_length >= 1024) {
+ return false;
+ }
+ 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;
+ 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));
+
+ // 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) {
+ // This should not happen if the PMT size
+ // was correctly encoded into the stream.
+ 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;
+
+ // This 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

Powered by Google App Engine
This is Rietveld 408576698