Shaka Packager SDK
ts_section_pmt.cc
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.
4 
5 #include <packager/media/formats/mp2t/ts_section_pmt.h>
6 
7 #include <vector>
8 
9 #include <absl/log/log.h>
10 
11 #include <packager/media/base/bit_reader.h>
12 #include <packager/media/formats/mp2t/mp2t_common.h>
13 #include <packager/media/formats/mp2t/ts_audio_type.h>
14 #include <packager/media/formats/mp2t/ts_stream_type.h>
15 
16 namespace shaka {
17 namespace media {
18 namespace mp2t {
19 
20 namespace {
21 
22 const int kISO639LanguageDescriptor = 0x0A;
23 const int kMaximumBitrateDescriptor = 0x0E;
24 const int kTeletextDescriptor = 0x56;
25 const int kSubtitlingDescriptor = 0x59;
26 
27 } // namespace
28 
29 TsSectionPmt::TsSectionPmt(const RegisterPesCb& register_pes_cb)
30  : register_pes_cb_(register_pes_cb) {
31 }
32 
33 TsSectionPmt::~TsSectionPmt() {
34 }
35 
36 bool TsSectionPmt::ParsePsiSection(BitReader* bit_reader) {
37  // Read up to |last_section_number|.
38  int table_id;
39  int section_syntax_indicator;
40  int dummy_zero;
41  int reserved;
42  int section_length;
43  int program_number;
44  int version_number;
45  int current_next_indicator;
46  int section_number;
47  int last_section_number;
48  RCHECK(bit_reader->ReadBits(8, &table_id));
49  RCHECK(bit_reader->ReadBits(1, &section_syntax_indicator));
50  RCHECK(bit_reader->ReadBits(1, &dummy_zero));
51  RCHECK(bit_reader->ReadBits(2, &reserved));
52  RCHECK(bit_reader->ReadBits(12, &section_length));
53  int section_start_marker = static_cast<int>(bit_reader->bits_available()) / 8;
54 
55  RCHECK(bit_reader->ReadBits(16, &program_number));
56  RCHECK(bit_reader->ReadBits(2, &reserved));
57  RCHECK(bit_reader->ReadBits(5, &version_number));
58  RCHECK(bit_reader->ReadBits(1, &current_next_indicator));
59  RCHECK(bit_reader->ReadBits(8, &section_number));
60  RCHECK(bit_reader->ReadBits(8, &last_section_number));
61 
62  // Perform a few verifications:
63  // - table ID should be 2 for a PMT.
64  // - section_syntax_indicator should be one.
65  // - section length should not exceed 1021.
66  RCHECK(table_id == 0x2);
67  RCHECK(section_syntax_indicator);
68  RCHECK(!dummy_zero);
69  RCHECK(section_length <= 1021);
70  RCHECK(section_number == 0);
71  RCHECK(last_section_number == 0);
72 
73  // Read the end of the fixed length section.
74  int pcr_pid;
75  int program_info_length;
76  RCHECK(bit_reader->ReadBits(3, &reserved));
77  RCHECK(bit_reader->ReadBits(13, &pcr_pid));
78  RCHECK(bit_reader->ReadBits(4, &reserved));
79  RCHECK(bit_reader->ReadBits(12, &program_info_length));
80  RCHECK(program_info_length < 1024);
81 
82  // Read the program info descriptor.
83  // Defined in section 2.6 of ISO-13818.
84  RCHECK(bit_reader->SkipBits(8 * program_info_length));
85 
86  // Read the ES description table.
87  // The end of the PID map if 4 bytes away from the end of the section
88  // (4 bytes = size of the CRC).
89  int pid_map_end_marker = section_start_marker - section_length + 4;
90  struct Info {
91  int pid_es;
92  TsStreamType stream_type;
93  const uint8_t* descriptor;
94  size_t descriptor_length;
95  std::string lang;
96  uint32_t max_bitrate;
97  TsAudioType audio_type;
98  };
99  std::vector<Info> pid_info;
100  while (static_cast<int>(bit_reader->bits_available()) >
101  8 * pid_map_end_marker) {
102  TsStreamType stream_type;
103  int pid_es;
104  size_t es_info_length;
105  RCHECK(bit_reader->ReadBits(8, &stream_type));
106  RCHECK(bit_reader->SkipBits(3)); // reserved
107  RCHECK(bit_reader->ReadBits(13, &pid_es));
108  RCHECK(bit_reader->ReadBits(4, &reserved));
109  RCHECK(bit_reader->ReadBits(12, &es_info_length));
110  const uint8_t* descriptor = bit_reader->current_byte_ptr();
111 
112  // Do not register the PID right away.
113  // Wait for the end of the section to be fully parsed
114  // to make sure there is no error.
115  pid_info.push_back({pid_es, stream_type, descriptor, es_info_length, "", 0,
116  TsAudioType::kUndefined});
117 
118  // Read the ES info descriptors.
119  // Defined in section 2.6 of ISO-13818.
120  uint8_t descriptor_tag;
121  uint8_t descriptor_length;
122 
123  while (es_info_length) {
124  RCHECK(bit_reader->ReadBits(8, &descriptor_tag));
125  RCHECK(bit_reader->ReadBits(8, &descriptor_length));
126  es_info_length -= 2;
127 
128  // See ETSI EN 300 468 Section 6.1
129  if (stream_type == TsStreamType::kPesPrivateData) {
130  switch (descriptor_tag) {
131  case kTeletextDescriptor:
132  pid_info.back().stream_type = TsStreamType::kTeletextSubtitles;
133  break;
134  case kSubtitlingDescriptor:
135  pid_info.back().stream_type = TsStreamType::kDvbSubtitles;
136  break;
137  default:
138  break;
139  }
140  } else if (descriptor_tag == kISO639LanguageDescriptor &&
141  descriptor_length >= 4) {
142  // See section 2.6.19 of ISO-13818
143  // Descriptor can contain 0..N language defintions,
144  // we process only the first one
145  RCHECK(es_info_length >= 4);
146 
147  char lang[3];
148  RCHECK(bit_reader->ReadBits(8, &lang[0])); // ISO_639_language_code
149  RCHECK(bit_reader->ReadBits(8, &lang[1]));
150  RCHECK(bit_reader->ReadBits(8, &lang[2]));
151  RCHECK(bit_reader->ReadBits(8, &pid_info.back().audio_type));
152  pid_info.back().lang = std::string(lang, 3);
153 
154  es_info_length -= 4;
155  descriptor_length -= 4;
156  } else if (descriptor_tag == kMaximumBitrateDescriptor &&
157  descriptor_length >= 3) {
158  // See section 2.6.25 of ISO-13818
159  RCHECK(es_info_length >= 3);
160 
161  uint32_t max_bitrate;
162  RCHECK(bit_reader->SkipBits(2)); // reserved
163  RCHECK(bit_reader->ReadBits(22, &max_bitrate));
164  // maximum bitrate is stored in units of 50 bytes per second
165  pid_info.back().max_bitrate = 50 * 8 * max_bitrate;
166 
167  es_info_length -= 3;
168  descriptor_length -= 3;
169  }
170 
171  RCHECK(bit_reader->SkipBits(8 * descriptor_length));
172  es_info_length -= descriptor_length;
173  }
174 
175  RCHECK(bit_reader->SkipBytes(es_info_length));
176  }
177 
178  // Read the CRC.
179  int crc32;
180  RCHECK(bit_reader->ReadBits(32, &crc32));
181 
182  // Once the PMT has been proved to be correct, register the PIDs.
183  for (auto& info : pid_info) {
184  register_pes_cb_(info.pid_es, info.stream_type, info.max_bitrate, info.lang,
185  info.audio_type, info.descriptor, info.descriptor_length);
186  }
187 
188  return true;
189 }
190 
191 void TsSectionPmt::ResetPsiSection() {
192 }
193 
194 } // namespace mp2t
195 } // namespace media
196 } // namespace shaka
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66