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 "media/formats/mp2t/ts_packet.h"
6
7#include "base/memory/scoped_ptr.h"
8#include "media/base/bit_reader.h"
9#include "media/formats/mp2t/mp2t_common.h"
10
11namespace media {
12namespace mp2t {
13
14static const uint8 kTsHeaderSyncword = 0x47;
15
16// static
17int TsPacket::Sync(const uint8* buf, int size) {
18  int k = 0;
19  for (; k < size; k++) {
20    // Verify that we have 4 syncwords in a row when possible,
21    // this should improve synchronization robustness.
22    // TODO(damienv): Consider the case where there is garbage
23    // between TS packets.
24    bool is_header = true;
25    for (int i = 0; i < 4; i++) {
26      int idx = k + i * kPacketSize;
27      if (idx >= size)
28        break;
29      if (buf[idx] != kTsHeaderSyncword) {
30        DVLOG(LOG_LEVEL_TS)
31            << "ByteSync" << idx << ": "
32            << std::hex << static_cast<int>(buf[idx]) << std::dec;
33        is_header = false;
34        break;
35      }
36    }
37    if (is_header)
38      break;
39  }
40
41  DVLOG_IF(1, k != 0) << "SYNC: nbytes_skipped=" << k;
42  return k;
43}
44
45// static
46TsPacket* TsPacket::Parse(const uint8* buf, int size) {
47  if (size < kPacketSize) {
48    DVLOG(1) << "Buffer does not hold one full TS packet:"
49             << " buffer_size=" << size;
50    return NULL;
51  }
52
53  DCHECK_EQ(buf[0], kTsHeaderSyncword);
54  if (buf[0] != kTsHeaderSyncword) {
55    DVLOG(1) << "Not on a TS syncword:"
56             << " buf[0]="
57             << std::hex << static_cast<int>(buf[0]) << std::dec;
58    return NULL;
59  }
60
61  scoped_ptr<TsPacket> ts_packet(new TsPacket());
62  bool status = ts_packet->ParseHeader(buf);
63  if (!status) {
64    DVLOG(1) << "Parsing header failed";
65    return NULL;
66  }
67  return ts_packet.release();
68}
69
70TsPacket::TsPacket() {
71}
72
73TsPacket::~TsPacket() {
74}
75
76bool TsPacket::ParseHeader(const uint8* buf) {
77  BitReader bit_reader(buf, kPacketSize);
78  payload_ = buf;
79  payload_size_ = kPacketSize;
80
81  // Read the TS header: 4 bytes.
82  int syncword;
83  int transport_error_indicator;
84  int payload_unit_start_indicator;
85  int transport_priority;
86  int transport_scrambling_control;
87  int adaptation_field_control;
88  RCHECK(bit_reader.ReadBits(8, &syncword));
89  RCHECK(bit_reader.ReadBits(1, &transport_error_indicator));
90  RCHECK(bit_reader.ReadBits(1, &payload_unit_start_indicator));
91  RCHECK(bit_reader.ReadBits(1, &transport_priority));
92  RCHECK(bit_reader.ReadBits(13, &pid_));
93  RCHECK(bit_reader.ReadBits(2, &transport_scrambling_control));
94  RCHECK(bit_reader.ReadBits(2, &adaptation_field_control));
95  RCHECK(bit_reader.ReadBits(4, &continuity_counter_));
96  payload_unit_start_indicator_ = (payload_unit_start_indicator != 0);
97  payload_ += 4;
98  payload_size_ -= 4;
99
100  // Default values when no adaptation field.
101  discontinuity_indicator_ = false;
102  random_access_indicator_ = false;
103
104  // Done since no adaptation field.
105  if ((adaptation_field_control & 0x2) == 0)
106    return true;
107
108  // Read the adaptation field if needed.
109  int adaptation_field_length;
110  RCHECK(bit_reader.ReadBits(8, &adaptation_field_length));
111  DVLOG(LOG_LEVEL_TS) << "adaptation_field_length=" << adaptation_field_length;
112  payload_ += 1;
113  payload_size_ -= 1;
114  if ((adaptation_field_control & 0x1) == 0 &&
115       adaptation_field_length != 183) {
116    DVLOG(1) << "adaptation_field_length=" << adaptation_field_length;
117    return false;
118  }
119  if ((adaptation_field_control & 0x1) == 1 &&
120       adaptation_field_length > 182) {
121    DVLOG(1) << "adaptation_field_length=" << adaptation_field_length;
122    // This is not allowed by the spec.
123    // However, some badly encoded streams are using
124    // adaptation_field_length = 183
125    return false;
126  }
127
128  // adaptation_field_length = '0' is used to insert a single stuffing byte
129  // in the adaptation field of a transport stream packet.
130  if (adaptation_field_length == 0)
131    return true;
132
133  bool status = ParseAdaptationField(&bit_reader, adaptation_field_length);
134  payload_ += adaptation_field_length;
135  payload_size_ -= adaptation_field_length;
136  return status;
137}
138
139bool TsPacket::ParseAdaptationField(BitReader* bit_reader,
140                                    int adaptation_field_length) {
141  DCHECK_GT(adaptation_field_length, 0);
142  int adaptation_field_start_marker = 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 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 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 = adaptation_field_length -
201      (adaptation_field_start_marker - bit_reader->bits_available() / 8);
202  RCHECK(adaptation_field_remaining_size >= 0);
203  for (int k = 0; k < adaptation_field_remaining_size; k++) {
204    int stuffing_byte;
205    RCHECK(bit_reader->ReadBits(8, &stuffing_byte));
206    // Unfortunately, a lot of streams exist in the field that do not fill
207    // the remaining of the adaptation field with the expected stuffing value:
208    // do not fail if that's the case.
209    DVLOG_IF(1, stuffing_byte != 0xff)
210        << "Stream not compliant: invalid stuffing byte "
211        << std::hex << stuffing_byte;
212  }
213
214  DVLOG(LOG_LEVEL_TS) << "random_access_indicator=" << random_access_indicator_;
215  return true;
216}
217
218}  // namespace mp2t
219}  // namespace media
220
221