1 // Copyright 2014 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.
5 #include "media/formats/mp2t/ts_section_pmt.h"
9 #include "base/logging.h"
10 #include "media/base/bit_reader.h"
11 #include "media/formats/mp2t/mp2t_common.h"
16 TsSectionPmt::TsSectionPmt(const RegisterPesCb
& register_pes_cb
)
17 : register_pes_cb_(register_pes_cb
) {
20 TsSectionPmt::~TsSectionPmt() {
23 bool TsSectionPmt::ParsePsiSection(BitReader
* bit_reader
) {
24 // Read up to |last_section_number|.
26 int section_syntax_indicator
;
32 int current_next_indicator
;
34 int last_section_number
;
35 RCHECK(bit_reader
->ReadBits(8, &table_id
));
36 RCHECK(bit_reader
->ReadBits(1, §ion_syntax_indicator
));
37 RCHECK(bit_reader
->ReadBits(1, &dummy_zero
));
38 RCHECK(bit_reader
->ReadBits(2, &reserved
));
39 RCHECK(bit_reader
->ReadBits(12, §ion_length
));
40 int section_start_marker
= bit_reader
->bits_available() / 8;
42 RCHECK(bit_reader
->ReadBits(16, &program_number
));
43 RCHECK(bit_reader
->ReadBits(2, &reserved
));
44 RCHECK(bit_reader
->ReadBits(5, &version_number
));
45 RCHECK(bit_reader
->ReadBits(1, ¤t_next_indicator
));
46 RCHECK(bit_reader
->ReadBits(8, §ion_number
));
47 RCHECK(bit_reader
->ReadBits(8, &last_section_number
));
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
);
56 RCHECK(section_length
<= 1021);
57 RCHECK(section_number
== 0);
58 RCHECK(last_section_number
== 0);
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.
64 // Read the end of the fixed length section.
66 int program_info_length
;
67 RCHECK(bit_reader
->ReadBits(3, &reserved
));
68 RCHECK(bit_reader
->ReadBits(13, &pcr_pid
));
69 RCHECK(bit_reader
->ReadBits(4, &reserved
));
70 RCHECK(bit_reader
->ReadBits(12, &program_info_length
));
71 RCHECK(program_info_length
< 1024);
73 // Read the program info descriptor.
74 // TODO(damienv): check wether any of the descriptors could be useful.
75 // Defined in section 2.6 of ISO-13818.
76 RCHECK(bit_reader
->SkipBits(8 * program_info_length
));
78 // Read the ES description table.
79 // The end of the PID map if 4 bytes away from the end of the section
80 // (4 bytes = size of the CRC).
81 int pid_map_end_marker
= section_start_marker
- section_length
+ 4;
82 std::map
<int, int> pid_map
;
83 while (bit_reader
->bits_available() > 8 * pid_map_end_marker
) {
88 RCHECK(bit_reader
->ReadBits(8, &stream_type
));
89 RCHECK(bit_reader
->ReadBits(3, &reserved
));
90 RCHECK(bit_reader
->ReadBits(13, &pid_es
));
91 RCHECK(bit_reader
->ReadBits(4, &reserved
));
92 RCHECK(bit_reader
->ReadBits(12, &es_info_length
));
94 // Do not register the PID right away.
95 // Wait for the end of the section to be fully parsed
96 // to make sure there is no error.
97 pid_map
.insert(std::pair
<int, int>(pid_es
, stream_type
));
99 // Read the ES info descriptors.
100 // TODO(damienv): check wether any of the descriptors could be useful.
101 // Defined in section 2.6 of ISO-13818.
102 RCHECK(bit_reader
->SkipBits(8 * es_info_length
));
107 RCHECK(bit_reader
->ReadBits(32, &crc32
));
109 // Once the PMT has been proved to be correct, register the PIDs.
110 for (std::map
<int, int>::iterator it
= pid_map
.begin();
111 it
!= pid_map
.end(); ++it
)
112 register_pes_cb_
.Run(it
->first
, it
->second
);
117 void TsSectionPmt::ResetPsiSection() {