5 #include <packager/media/formats/mp2t/ts_packet.h>
9 #include <absl/log/check.h>
11 #include <packager/macros/logging.h>
12 #include <packager/media/base/bit_reader.h>
13 #include <packager/media/formats/mp2t/mp2t_common.h>
19 static const uint8_t kTsHeaderSyncword = 0x47;
22 int TsPacket::Sync(
const uint8_t* buf,
int size) {
24 for (; k < size; k++) {
27 bool is_header =
true;
28 for (
int i = 0; i < 4; i++) {
29 int idx = k + i * kPacketSize;
32 if (buf[idx] != kTsHeaderSyncword) {
34 <<
"ByteSync" << idx <<
": "
35 << std::hex << static_cast<int>(buf[idx]) << std::dec;
45 DVLOG(1) <<
"SYNC: nbytes_skipped=" << k;
51 TsPacket* TsPacket::Parse(
const uint8_t* buf,
int size) {
52 if (size < kPacketSize) {
53 DVLOG(1) <<
"Buffer does not hold one full TS packet:"
54 <<
" buffer_size=" << size;
58 DCHECK_EQ(buf[0], kTsHeaderSyncword);
59 if (buf[0] != kTsHeaderSyncword) {
60 DVLOG(1) <<
"Not on a TS syncword:"
62 << std::hex << static_cast<int>(buf[0]) << std::dec;
66 std::unique_ptr<TsPacket> ts_packet(
new TsPacket());
67 bool status = ts_packet->ParseHeader(buf);
69 DVLOG(1) <<
"Parsing header failed";
72 return ts_packet.release();
75 TsPacket::TsPacket() {
78 TsPacket::~TsPacket() {
81 bool TsPacket::ParseHeader(
const uint8_t* buf) {
82 BitReader bit_reader(buf, kPacketSize);
84 payload_size_ = kPacketSize;
88 int transport_error_indicator;
89 int payload_unit_start_indicator;
90 int transport_priority;
91 int transport_scrambling_control;
92 int adaptation_field_control;
93 RCHECK(bit_reader.ReadBits(8, &syncword));
94 RCHECK(bit_reader.ReadBits(1, &transport_error_indicator));
95 RCHECK(bit_reader.ReadBits(1, &payload_unit_start_indicator));
96 RCHECK(bit_reader.ReadBits(1, &transport_priority));
97 RCHECK(bit_reader.ReadBits(13, &pid_));
98 RCHECK(bit_reader.ReadBits(2, &transport_scrambling_control));
99 RCHECK(bit_reader.ReadBits(2, &adaptation_field_control));
100 RCHECK(bit_reader.ReadBits(4, &continuity_counter_));
101 payload_unit_start_indicator_ = (payload_unit_start_indicator != 0);
106 discontinuity_indicator_ =
false;
107 random_access_indicator_ =
false;
110 if ((adaptation_field_control & 0x2) == 0)
114 int adaptation_field_length;
115 RCHECK(bit_reader.ReadBits(8, &adaptation_field_length));
116 DVLOG(LOG_LEVEL_TS) <<
"adaptation_field_length=" << adaptation_field_length;
119 if ((adaptation_field_control & 0x1) == 0 &&
120 adaptation_field_length != 183) {
121 DVLOG(1) <<
"adaptation_field_length=" << adaptation_field_length;
124 if ((adaptation_field_control & 0x1) == 1 &&
125 adaptation_field_length > 182) {
126 DVLOG(1) <<
"adaptation_field_length=" << adaptation_field_length;
135 if (adaptation_field_length == 0)
138 bool status = ParseAdaptationField(&bit_reader, adaptation_field_length);
139 payload_ += adaptation_field_length;
140 payload_size_ -= adaptation_field_length;
144 bool TsPacket::ParseAdaptationField(BitReader* bit_reader,
145 int adaptation_field_length) {
146 DCHECK_GT(adaptation_field_length, 0);
147 int adaptation_field_start_marker =
148 static_cast<int>(bit_reader->bits_available()) / 8;
150 int discontinuity_indicator;
151 int random_access_indicator;
152 int elementary_stream_priority_indicator;
155 int splicing_point_flag;
156 int transport_private_data_flag;
157 int adaptation_field_extension_flag;
158 RCHECK(bit_reader->ReadBits(1, &discontinuity_indicator));
159 RCHECK(bit_reader->ReadBits(1, &random_access_indicator));
160 RCHECK(bit_reader->ReadBits(1, &elementary_stream_priority_indicator));
161 RCHECK(bit_reader->ReadBits(1, &pcr_flag));
162 RCHECK(bit_reader->ReadBits(1, &opcr_flag));
163 RCHECK(bit_reader->ReadBits(1, &splicing_point_flag));
164 RCHECK(bit_reader->ReadBits(1, &transport_private_data_flag));
165 RCHECK(bit_reader->ReadBits(1, &adaptation_field_extension_flag));
166 discontinuity_indicator_ = (discontinuity_indicator != 0);
167 random_access_indicator_ = (random_access_indicator != 0);
170 int64_t program_clock_reference_base;
172 int program_clock_reference_extension;
173 RCHECK(bit_reader->ReadBits(33, &program_clock_reference_base));
174 RCHECK(bit_reader->ReadBits(6, &reserved));
175 RCHECK(bit_reader->ReadBits(9, &program_clock_reference_extension));
179 int64_t original_program_clock_reference_base;
181 int original_program_clock_reference_extension;
182 RCHECK(bit_reader->ReadBits(33, &original_program_clock_reference_base));
183 RCHECK(bit_reader->ReadBits(6, &reserved));
185 bit_reader->ReadBits(9, &original_program_clock_reference_extension));
188 if (splicing_point_flag) {
189 int splice_countdown;
190 RCHECK(bit_reader->ReadBits(8, &splice_countdown));
193 if (transport_private_data_flag) {
194 int transport_private_data_length;
195 RCHECK(bit_reader->ReadBits(8, &transport_private_data_length));
196 RCHECK(bit_reader->SkipBits(8 * transport_private_data_length));
199 if (adaptation_field_extension_flag) {
200 int adaptation_field_extension_length;
201 RCHECK(bit_reader->ReadBits(8, &adaptation_field_extension_length));
202 RCHECK(bit_reader->SkipBits(8 * adaptation_field_extension_length));
206 int adaptation_field_remaining_size =
207 adaptation_field_length -
208 (adaptation_field_start_marker -
209 static_cast<int>(bit_reader->bits_available()) / 8);
210 RCHECK(adaptation_field_remaining_size >= 0);
211 for (
int k = 0; k < adaptation_field_remaining_size; k++) {
213 RCHECK(bit_reader->ReadBits(8, &stuffing_byte));
214 RCHECK(stuffing_byte == 0xff);
217 DVLOG(LOG_LEVEL_TS) <<
"random_access_indicator=" << random_access_indicator_;
All the methods that are virtual are virtual for mocking.