5 #include "packager/media/formats/mp2t/ts_packet.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 std::unique_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 =
142 static_cast<int>(bit_reader->bits_available()) / 8;
144 int discontinuity_indicator;
145 int random_access_indicator;
146 int elementary_stream_priority_indicator;
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);
164 int64_t program_clock_reference_base;
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));
173 int64_t original_program_clock_reference_base;
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));
179 bit_reader->ReadBits(9, &original_program_clock_reference_extension));
182 if (splicing_point_flag) {
183 int splice_countdown;
184 RCHECK(bit_reader->ReadBits(8, &splice_countdown));
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));
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));
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++) {
207 RCHECK(bit_reader->ReadBits(8, &stuffing_byte));
208 RCHECK(stuffing_byte == 0xff);
211 DVLOG(LOG_LEVEL_TS) <<
"random_access_indicator=" << random_access_indicator_;
All the methods that are virtual are virtual for mocking.