Shaka Packager SDK
ts_packet.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_packet.h>
6 
7 #include <memory>
8 
9 #include <absl/log/check.h>
10 
11 #include <packager/macros/logging.h>
12 #include <packager/media/base/bit_reader.h>
13 #include <packager/media/formats/mp2t/mp2t_common.h>
14 
15 namespace shaka {
16 namespace media {
17 namespace mp2t {
18 
19 static const uint8_t kTsHeaderSyncword = 0x47;
20 
21 // static
22 int TsPacket::Sync(const uint8_t* buf, int size) {
23  int k = 0;
24  for (; k < size; k++) {
25  // Verify that we have 4 syncwords in a row when possible,
26  // this should improve synchronization robustness.
27  bool is_header = true;
28  for (int i = 0; i < 4; i++) {
29  int idx = k + i * kPacketSize;
30  if (idx >= size)
31  break;
32  if (buf[idx] != kTsHeaderSyncword) {
33  DVLOG(LOG_LEVEL_TS)
34  << "ByteSync" << idx << ": "
35  << std::hex << static_cast<int>(buf[idx]) << std::dec;
36  is_header = false;
37  break;
38  }
39  }
40  if (is_header)
41  break;
42  }
43 
44  if (k != 0) {
45  DVLOG(1) << "SYNC: nbytes_skipped=" << k;
46  }
47  return k;
48 }
49 
50 // static
51 TsPacket* TsPacket::Parse(const uint8_t* buf, int size) {
52  if (size < kPacketSize) {
53  DVLOG(1) << "Buffer does not hold one full TS packet:"
54  << " buffer_size=" << size;
55  return NULL;
56  }
57 
58  DCHECK_EQ(buf[0], kTsHeaderSyncword);
59  if (buf[0] != kTsHeaderSyncword) {
60  DVLOG(1) << "Not on a TS syncword:"
61  << " buf[0]="
62  << std::hex << static_cast<int>(buf[0]) << std::dec;
63  return NULL;
64  }
65 
66  std::unique_ptr<TsPacket> ts_packet(new TsPacket());
67  bool status = ts_packet->ParseHeader(buf);
68  if (!status) {
69  DVLOG(1) << "Parsing header failed";
70  return NULL;
71  }
72  return ts_packet.release();
73 }
74 
75 TsPacket::TsPacket() {
76 }
77 
78 TsPacket::~TsPacket() {
79 }
80 
81 bool TsPacket::ParseHeader(const uint8_t* buf) {
82  BitReader bit_reader(buf, kPacketSize);
83  payload_ = buf;
84  payload_size_ = kPacketSize;
85 
86  // Read the TS header: 4 bytes.
87  int syncword;
88  int transport_error_indicator;
89  int payload_unit_start_indicator;
90  int transport_priority;
91  int transport_scrambling_control;
92  int adaptation_field_control;
93  RCHECK(bit_reader.ReadBits(8, &syncword));
94  RCHECK(bit_reader.ReadBits(1, &transport_error_indicator));
95  RCHECK(bit_reader.ReadBits(1, &payload_unit_start_indicator));
96  RCHECK(bit_reader.ReadBits(1, &transport_priority));
97  RCHECK(bit_reader.ReadBits(13, &pid_));
98  RCHECK(bit_reader.ReadBits(2, &transport_scrambling_control));
99  RCHECK(bit_reader.ReadBits(2, &adaptation_field_control));
100  RCHECK(bit_reader.ReadBits(4, &continuity_counter_));
101  payload_unit_start_indicator_ = (payload_unit_start_indicator != 0);
102  payload_ += 4;
103  payload_size_ -= 4;
104 
105  // Default values when no adaptation field.
106  discontinuity_indicator_ = false;
107  random_access_indicator_ = false;
108 
109  // Done since no adaptation field.
110  if ((adaptation_field_control & 0x2) == 0)
111  return true;
112 
113  // Read the adaptation field if needed.
114  int adaptation_field_length;
115  RCHECK(bit_reader.ReadBits(8, &adaptation_field_length));
116  DVLOG(LOG_LEVEL_TS) << "adaptation_field_length=" << adaptation_field_length;
117  payload_ += 1;
118  payload_size_ -= 1;
119  if ((adaptation_field_control & 0x1) == 0 &&
120  adaptation_field_length != 183) {
121  DVLOG(1) << "adaptation_field_length=" << adaptation_field_length;
122  return false;
123  }
124  if ((adaptation_field_control & 0x1) == 1 &&
125  adaptation_field_length > 182) {
126  DVLOG(1) << "adaptation_field_length=" << adaptation_field_length;
127  // This is not allowed by the spec.
128  // However, some badly encoded streams are using
129  // adaptation_field_length = 183
130  return false;
131  }
132 
133  // adaptation_field_length = '0' is used to insert a single stuffing byte
134  // in the adaptation field of a transport stream packet.
135  if (adaptation_field_length == 0)
136  return true;
137 
138  bool status = ParseAdaptationField(&bit_reader, adaptation_field_length);
139  payload_ += adaptation_field_length;
140  payload_size_ -= adaptation_field_length;
141  return status;
142 }
143 
144 bool TsPacket::ParseAdaptationField(BitReader* bit_reader,
145  int adaptation_field_length) {
146  DCHECK_GT(adaptation_field_length, 0);
147  int adaptation_field_start_marker =
148  static_cast<int>(bit_reader->bits_available()) / 8;
149 
150  int discontinuity_indicator;
151  int random_access_indicator;
152  int elementary_stream_priority_indicator;
153  int pcr_flag;
154  int opcr_flag;
155  int splicing_point_flag;
156  int transport_private_data_flag;
157  int adaptation_field_extension_flag;
158  RCHECK(bit_reader->ReadBits(1, &discontinuity_indicator));
159  RCHECK(bit_reader->ReadBits(1, &random_access_indicator));
160  RCHECK(bit_reader->ReadBits(1, &elementary_stream_priority_indicator));
161  RCHECK(bit_reader->ReadBits(1, &pcr_flag));
162  RCHECK(bit_reader->ReadBits(1, &opcr_flag));
163  RCHECK(bit_reader->ReadBits(1, &splicing_point_flag));
164  RCHECK(bit_reader->ReadBits(1, &transport_private_data_flag));
165  RCHECK(bit_reader->ReadBits(1, &adaptation_field_extension_flag));
166  discontinuity_indicator_ = (discontinuity_indicator != 0);
167  random_access_indicator_ = (random_access_indicator != 0);
168 
169  if (pcr_flag) {
170  int64_t program_clock_reference_base;
171  int reserved;
172  int program_clock_reference_extension;
173  RCHECK(bit_reader->ReadBits(33, &program_clock_reference_base));
174  RCHECK(bit_reader->ReadBits(6, &reserved));
175  RCHECK(bit_reader->ReadBits(9, &program_clock_reference_extension));
176  }
177 
178  if (opcr_flag) {
179  int64_t original_program_clock_reference_base;
180  int reserved;
181  int original_program_clock_reference_extension;
182  RCHECK(bit_reader->ReadBits(33, &original_program_clock_reference_base));
183  RCHECK(bit_reader->ReadBits(6, &reserved));
184  RCHECK(
185  bit_reader->ReadBits(9, &original_program_clock_reference_extension));
186  }
187 
188  if (splicing_point_flag) {
189  int splice_countdown;
190  RCHECK(bit_reader->ReadBits(8, &splice_countdown));
191  }
192 
193  if (transport_private_data_flag) {
194  int transport_private_data_length;
195  RCHECK(bit_reader->ReadBits(8, &transport_private_data_length));
196  RCHECK(bit_reader->SkipBits(8 * transport_private_data_length));
197  }
198 
199  if (adaptation_field_extension_flag) {
200  int adaptation_field_extension_length;
201  RCHECK(bit_reader->ReadBits(8, &adaptation_field_extension_length));
202  RCHECK(bit_reader->SkipBits(8 * adaptation_field_extension_length));
203  }
204 
205  // The rest of the adaptation field should be stuffing bytes.
206  int adaptation_field_remaining_size =
207  adaptation_field_length -
208  (adaptation_field_start_marker -
209  static_cast<int>(bit_reader->bits_available()) / 8);
210  RCHECK(adaptation_field_remaining_size >= 0);
211  for (int k = 0; k < adaptation_field_remaining_size; k++) {
212  int stuffing_byte;
213  RCHECK(bit_reader->ReadBits(8, &stuffing_byte));
214  RCHECK(stuffing_byte == 0xff);
215  }
216 
217  DVLOG(LOG_LEVEL_TS) << "random_access_indicator=" << random_access_indicator_;
218  return true;
219 }
220 
221 } // namespace mp2t
222 } // namespace media
223 } // namespace shaka
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66