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

Side by Side 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: Address comments from patch set #3 Created 7 years, 3 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 unified diff | Download patch
OLDNEW
(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_pmt.h"
6
7 #include <map>
8
9 #include "base/logging.h"
10 #include "media/base/bit_reader.h"
11 #include "media/mpeg2/mpeg2ts_common.h"
12
13 namespace media {
14 namespace mpeg2ts {
15
16 Mpeg2TsPmtParser::Mpeg2TsPmtParser(RegisterPesCb register_pes_cb)
17 : register_pes_cb_(register_pes_cb) {
18 }
19
20 Mpeg2TsPmtParser::~Mpeg2TsPmtParser() {
21 }
22
23 bool Mpeg2TsPmtParser::ParsePsiSection(BitReader* bit_reader) {
24 // Read up to |last_section_number|.
25 if (bit_reader->bits_available() < 8 * 8) {
26 DVLOG(1) << "bits_available=" << bit_reader->bits_available();
27 return false;
28 }
29 DCHECK_GE(bit_reader->bits_available(), 8 * 8);
damienv1 2013/09/10 04:10:02 Remove DCHECK.
damienv1 2013/09/10 21:03:48 Done.
30 int table_id;
31 bool section_syntax_indicator;
32 bool dummy_zero;
33 int reserved;
34 int section_length;
35 int program_number;
36 int version_number;
37 bool current_next_indicator;
38 int section_number;
39 int last_section_number;
40 DCHECK(bit_reader->ReadBits(8, &table_id));
41 DCHECK(bit_reader->ReadBits(1, &section_syntax_indicator));
42 DCHECK(bit_reader->ReadBits(1, &dummy_zero));
43 DCHECK(bit_reader->ReadBits(2, &reserved));
44 DCHECK(bit_reader->ReadBits(12, &section_length));
45 DCHECK(bit_reader->ReadBits(16, &program_number));
46 DCHECK(bit_reader->ReadBits(2, &reserved));
47 DCHECK(bit_reader->ReadBits(5, &version_number));
48 DCHECK(bit_reader->ReadBits(1, &current_next_indicator));
49 DCHECK(bit_reader->ReadBits(8, &section_number));
50 DCHECK(bit_reader->ReadBits(8, &last_section_number));
51 section_length -= 5;
52
53 // Perform a few verifications:
54 // - table ID should be 2 for a PMT.
55 // - section_syntax_indicator should be one.
56 // - section length should not exceed 1021.
57 RCHECK(table_id == 0x2);
58 RCHECK(section_syntax_indicator);
59 RCHECK(!dummy_zero);
60 RCHECK(section_length <= 1021);
61 RCHECK(section_number == 0);
62 RCHECK(last_section_number == 0);
63
64 // TODO(damienv):
65 // Verify that there is no mismatch between the program number
66 // and the program number that was provided in a PAT for the current PMT.
67
68 // Make sure there are enough bits for the rest of the section.
69 if (bit_reader->bits_available() < section_length * 8) {
70 LOG(WARNING) << "bits_available=" << bit_reader->bits_available();
71 return false;
72 }
73
74 // Read the end of the fixed length section.
75 if (section_length < 4) {
76 LOG(WARNING) << "Section length too small";
77 return false;
78 }
79 int pcr_pid;
80 int program_info_length;
81 DCHECK(bit_reader->ReadBits(3, &reserved));
82 DCHECK(bit_reader->ReadBits(13, &pcr_pid));
83 DCHECK(bit_reader->ReadBits(4, &reserved));
84 DCHECK(bit_reader->ReadBits(12, &program_info_length));
85 RCHECK(program_info_length < 1024);
86 section_length -= 4;
87
88 // Read the program info descriptor.
89 // TODO(damienv): check wether any of the descriptors could be useful.
90 // Defined in section 2.6 of ISO-13818.
91 if (section_length < program_info_length) {
92 DVLOG(1) << "Section length too small";
93 return false;
94 }
95 for (int k = 0; k < program_info_length; k++) {
96 int dummy;
97 DCHECK(bit_reader->ReadBits(8, &dummy));
98 }
99 section_length -= program_info_length;
100
101 // Read the ES description table.
102 // 5 bytes: minimum size of an ES descriptor.
103 std::map<int, int> pid_map;
104 while (section_length >= 5) {
105 int stream_type;
106 int reserved;
107 int pid_es;
108 int es_info_length;
109 DCHECK(bit_reader->ReadBits(8, &stream_type));
110 DCHECK(bit_reader->ReadBits(3, &reserved));
111 DCHECK(bit_reader->ReadBits(13, &pid_es));
112 DCHECK(bit_reader->ReadBits(4, &reserved));
113 DCHECK(bit_reader->ReadBits(12, &es_info_length));
114 section_length -= 5;
115
116 // Do not register the PID right away.
117 // Wait for the end of the section to be fully parsed
118 // to make sure there is no error.
119 pid_map.insert(std::pair<int, int>(pid_es, stream_type));
120
121 // Read the ES info descriptors.
122 // TODO(damienv): check wether any of the descriptors could be useful.
123 // Defined in section 2.6 of ISO-13818.
124 if (section_length < es_info_length) {
125 DVLOG(1) << "section_length=" << section_length
126 << " es_info_length=" << es_info_length;
127 return false;
128 }
129 for (int k = 0; k < es_info_length; k++) {
130 int dummy;
131 DCHECK(bit_reader->ReadBits(8, &dummy));
132 }
133 section_length -= es_info_length;
134 }
135
136 // Read the CRC.
137 if (section_length < 4) {
138 DVLOG(1) << "Section length too small: " << section_length;
139 return false;
140 }
141 int crc32;
142 DCHECK(bit_reader->ReadBits(32, &crc32));
143 section_length -= 4;
144
145 // Should not happen if the PMT size was correctly encoded into the stream.
146 DVLOG_IF(1, section_length != 0)
147 << "section_length=" << section_length;
148
149 // Once the PMT has been proved to be correct, register the PIDs.
150 for (std::map<int, int>::iterator it = pid_map.begin();
151 it != pid_map.end(); ++it)
152 register_pes_cb_.Run(it->first, it->second);
153
154 return true;
155 }
156
157 void Mpeg2TsPmtParser::ResetPsiSection() {
158 }
159
160 } // namespace mpeg2ts
161 } // namespace media
162
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698