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