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

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

Powered by Google App Engine
This is Rietveld 408576698