DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
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 "packager/base/memory/scoped_ptr.h"
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  scoped_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 = bit_reader->bits_available() / 8;
142 
143  int discontinuity_indicator;
144  int random_access_indicator;
145  int elementary_stream_priority_indicator;
146  int pcr_flag;
147  int opcr_flag;
148  int splicing_point_flag;
149  int transport_private_data_flag;
150  int adaptation_field_extension_flag;
151  RCHECK(bit_reader->ReadBits(1, &discontinuity_indicator));
152  RCHECK(bit_reader->ReadBits(1, &random_access_indicator));
153  RCHECK(bit_reader->ReadBits(1, &elementary_stream_priority_indicator));
154  RCHECK(bit_reader->ReadBits(1, &pcr_flag));
155  RCHECK(bit_reader->ReadBits(1, &opcr_flag));
156  RCHECK(bit_reader->ReadBits(1, &splicing_point_flag));
157  RCHECK(bit_reader->ReadBits(1, &transport_private_data_flag));
158  RCHECK(bit_reader->ReadBits(1, &adaptation_field_extension_flag));
159  discontinuity_indicator_ = (discontinuity_indicator != 0);
160  random_access_indicator_ = (random_access_indicator != 0);
161 
162  if (pcr_flag) {
163  int64_t program_clock_reference_base;
164  int reserved;
165  int program_clock_reference_extension;
166  RCHECK(bit_reader->ReadBits(33, &program_clock_reference_base));
167  RCHECK(bit_reader->ReadBits(6, &reserved));
168  RCHECK(bit_reader->ReadBits(9, &program_clock_reference_extension));
169  }
170 
171  if (opcr_flag) {
172  int64_t original_program_clock_reference_base;
173  int reserved;
174  int original_program_clock_reference_extension;
175  RCHECK(bit_reader->ReadBits(33, &original_program_clock_reference_base));
176  RCHECK(bit_reader->ReadBits(6, &reserved));
177  RCHECK(
178  bit_reader->ReadBits(9, &original_program_clock_reference_extension));
179  }
180 
181  if (splicing_point_flag) {
182  int splice_countdown;
183  RCHECK(bit_reader->ReadBits(8, &splice_countdown));
184  }
185 
186  if (transport_private_data_flag) {
187  int transport_private_data_length;
188  RCHECK(bit_reader->ReadBits(8, &transport_private_data_length));
189  RCHECK(bit_reader->SkipBits(8 * transport_private_data_length));
190  }
191 
192  if (adaptation_field_extension_flag) {
193  int adaptation_field_extension_length;
194  RCHECK(bit_reader->ReadBits(8, &adaptation_field_extension_length));
195  RCHECK(bit_reader->SkipBits(8 * adaptation_field_extension_length));
196  }
197 
198  // The rest of the adaptation field should be stuffing bytes.
199  int adaptation_field_remaining_size = adaptation_field_length -
200  (adaptation_field_start_marker - bit_reader->bits_available() / 8);
201  RCHECK(adaptation_field_remaining_size >= 0);
202  for (int k = 0; k < adaptation_field_remaining_size; k++) {
203  int stuffing_byte;
204  RCHECK(bit_reader->ReadBits(8, &stuffing_byte));
205  RCHECK(stuffing_byte == 0xff);
206  }
207 
208  DVLOG(LOG_LEVEL_TS) << "random_access_indicator=" << random_access_indicator_;
209  return true;
210 }
211 
212 } // namespace mp2t
213 } // namespace media
214 } // namespace shaka