5 #include "packager/media/formats/mp2t/ts_packet.h"
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"
15 static const uint8_t kTsHeaderSyncword = 0x47;
18 int TsPacket::Sync(
const uint8_t* buf,
int size) {
20 for (; k < size; k++) {
23 bool is_header =
true;
24 for (
int i = 0; i < 4; i++) {
25 int idx = k + i * kPacketSize;
28 if (buf[idx] != kTsHeaderSyncword) {
30 <<
"ByteSync" << idx <<
": "
31 << std::hex << static_cast<int>(buf[idx]) << std::dec;
40 DVLOG_IF(1, k != 0) <<
"SYNC: nbytes_skipped=" << k;
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;
52 DCHECK_EQ(buf[0], kTsHeaderSyncword);
53 if (buf[0] != kTsHeaderSyncword) {
54 DVLOG(1) <<
"Not on a TS syncword:"
56 << std::hex << static_cast<int>(buf[0]) << std::dec;
60 scoped_ptr<TsPacket> ts_packet(
new TsPacket());
61 bool status = ts_packet->ParseHeader(buf);
63 DVLOG(1) <<
"Parsing header failed";
66 return ts_packet.release();
69 TsPacket::TsPacket() {
72 TsPacket::~TsPacket() {
75 bool TsPacket::ParseHeader(
const uint8_t* buf) {
76 BitReader bit_reader(buf, kPacketSize);
78 payload_size_ = kPacketSize;
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);
100 discontinuity_indicator_ =
false;
101 random_access_indicator_ =
false;
104 if ((adaptation_field_control & 0x2) == 0)
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;
113 if ((adaptation_field_control & 0x1) == 0 &&
114 adaptation_field_length != 183) {
115 DVLOG(1) <<
"adaptation_field_length=" << adaptation_field_length;
118 if ((adaptation_field_control & 0x1) == 1 &&
119 adaptation_field_length > 182) {
120 DVLOG(1) <<
"adaptation_field_length=" << adaptation_field_length;
129 if (adaptation_field_length == 0)
132 bool status = ParseAdaptationField(&bit_reader, adaptation_field_length);
133 payload_ += adaptation_field_length;
134 payload_size_ -= adaptation_field_length;
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;
143 int discontinuity_indicator;
144 int random_access_indicator;
145 int elementary_stream_priority_indicator;
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);
163 int64_t program_clock_reference_base;
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));
172 int64_t original_program_clock_reference_base;
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));
178 bit_reader->ReadBits(9, &original_program_clock_reference_extension));
181 if (splicing_point_flag) {
182 int splice_countdown;
183 RCHECK(bit_reader->ReadBits(8, &splice_countdown));
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));
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));
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++) {
204 RCHECK(bit_reader->ReadBits(8, &stuffing_byte));
205 RCHECK(stuffing_byte == 0xff);
208 DVLOG(LOG_LEVEL_TS) <<
"random_access_indicator=" << random_access_indicator_;