5#include <packager/media/formats/mp2t/ts_section_pes.h>
7#include <absl/log/check.h>
8#include <absl/log/log.h>
10#include <packager/macros/logging.h>
11#include <packager/media/base/bit_reader.h>
12#include <packager/media/base/timestamp.h>
13#include <packager/media/formats/mp2t/es_parser.h>
14#include <packager/media/formats/mp2t/mp2t_common.h>
16static const int kPesStartCode = 0x000001;
24static int64_t UnrollTimestamp(int64_t previous_unrolled_time, int64_t time) {
30 DCHECK_EQ((time >> nbits), 0);
33 int64_t previous_unrolled_time_high = (previous_unrolled_time >> nbits);
34 int64_t time0 = ((previous_unrolled_time_high - 1) << nbits) | time;
35 int64_t time1 = ((previous_unrolled_time_high + 0) << nbits) | time;
36 int64_t time2 = ((previous_unrolled_time_high + 1) << nbits) | time;
40 int64_t diff0 = time0 - previous_unrolled_time;
41 int64_t diff1 = time1 - previous_unrolled_time;
42 int64_t diff2 = time2 - previous_unrolled_time;
50 int64_t unrolled_time;
53 unrolled_time = time1;
56 unrolled_time = time0;
60 unrolled_time = time2;
65static bool IsTimestampSectionValid(int64_t timestamp_section) {
72 return ((timestamp_section & 0x1) != 0) &&
73 ((timestamp_section & 0x10000) != 0) &&
74 ((timestamp_section & 0x100000000LL) != 0);
77static int64_t ConvertTimestampSectionToTimestamp(int64_t timestamp_section) {
78 return (((timestamp_section >> 33) & 0x7) << 30) |
79 (((timestamp_section >> 17) & 0x7fff) << 15) |
80 (((timestamp_section >> 1) & 0x7fff) << 0);
87TsSectionPes::TsSectionPes(std::unique_ptr<EsParser> es_parser)
88 : es_parser_(es_parser.release()),
90 previous_pts_valid_(false),
92 previous_dts_valid_(false),
97TsSectionPes::~TsSectionPes() {}
99bool TsSectionPes::Parse(
bool payload_unit_start_indicator,
103 if (wait_for_pusi_ && !payload_unit_start_indicator)
106 bool parse_result =
true;
107 if (payload_unit_start_indicator) {
112 const uint8_t* raw_pes;
113 pes_byte_queue_.Peek(&raw_pes, &raw_pes_size);
114 if (raw_pes_size > 0)
115 parse_result = Emit(
true);
121 wait_for_pusi_ =
false;
126 pes_byte_queue_.Push(buf, size);
129 return (parse_result && Emit(
false));
132bool TsSectionPes::Flush() {
138 return es_parser_->Flush();
141void TsSectionPes::Reset() {
144 previous_pts_valid_ =
false;
146 previous_dts_valid_ =
false;
152bool TsSectionPes::Emit(
bool emit_for_unknown_size) {
154 const uint8_t* raw_pes;
155 pes_byte_queue_.Peek(&raw_pes, &raw_pes_size);
159 if (raw_pes_size < 6)
163 int pes_packet_length =
164 (
static_cast<int>(raw_pes[4]) << 8) | (
static_cast<int>(raw_pes[5]));
165 if ((pes_packet_length == 0 && !emit_for_unknown_size) ||
166 (pes_packet_length != 0 && raw_pes_size < pes_packet_length + 6)) {
173 DVLOG(LOG_LEVEL_PES) <<
"pes_packet_length=" << pes_packet_length;
176 bool parse_result = ParseInternal(raw_pes, raw_pes_size);
184bool TsSectionPes::ParseInternal(
const uint8_t* raw_pes,
int raw_pes_size) {
185 BitReader bit_reader(raw_pes, raw_pes_size);
188 int packet_start_code_prefix;
190 int pes_packet_length;
191 RCHECK(bit_reader.ReadBits(24, &packet_start_code_prefix));
192 RCHECK(bit_reader.ReadBits(8, &stream_id));
193 RCHECK(bit_reader.ReadBits(16, &pes_packet_length));
195 RCHECK(packet_start_code_prefix == kPesStartCode);
196 DVLOG(LOG_LEVEL_PES) <<
"stream_id=" << stream_id;
197 if (pes_packet_length == 0)
198 pes_packet_length =
static_cast<int>(bit_reader.bits_available()) / 8;
203 const int kPrivateStream1 = 0xBD;
205 bool is_audio_stream_id =
206 ((stream_id & 0xe0) == 0xc0) || stream_id == kPrivateStream1;
207 bool is_video_stream_id = ((stream_id & 0xf0) == 0xe0);
208 if (!is_audio_stream_id && !is_video_stream_id)
213 int PES_scrambling_control;
215 int data_alignment_indicator;
217 int original_or_copy;
221 int dsm_trick_mode_flag;
222 int additional_copy_info_flag;
224 int pes_extension_flag;
225 int pes_header_data_length;
226 RCHECK(bit_reader.ReadBits(2, &dummy_2));
227 RCHECK(dummy_2 == 0x2);
228 RCHECK(bit_reader.ReadBits(2, &PES_scrambling_control));
229 RCHECK(bit_reader.ReadBits(1, &PES_priority));
230 RCHECK(bit_reader.ReadBits(1, &data_alignment_indicator));
231 RCHECK(bit_reader.ReadBits(1, ©right));
232 RCHECK(bit_reader.ReadBits(1, &original_or_copy));
233 RCHECK(bit_reader.ReadBits(2, &pts_dts_flags));
234 RCHECK(bit_reader.ReadBits(1, &escr_flag));
235 RCHECK(bit_reader.ReadBits(1, &es_rate_flag));
236 RCHECK(bit_reader.ReadBits(1, &dsm_trick_mode_flag));
237 RCHECK(bit_reader.ReadBits(1, &additional_copy_info_flag));
238 RCHECK(bit_reader.ReadBits(1, &pes_crc_flag));
239 RCHECK(bit_reader.ReadBits(1, &pes_extension_flag));
240 RCHECK(bit_reader.ReadBits(8, &pes_header_data_length));
241 int pes_header_start_size =
static_cast<int>(bit_reader.bits_available()) / 8;
246 int es_size = pes_packet_length - 3 - pes_header_data_length;
247 int es_offset = 6 + 3 + pes_header_data_length;
248 RCHECK(es_size >= 0);
249 RCHECK(es_offset + es_size <= raw_pes_size);
252 bool is_pts_valid =
false;
253 bool is_dts_valid =
false;
254 int64_t pts_section = 0;
255 int64_t dts_section = 0;
256 if (pts_dts_flags == 0x2) {
257 RCHECK(bit_reader.ReadBits(40, &pts_section));
258 RCHECK((((pts_section >> 36) & 0xf) == 0x2) &&
259 IsTimestampSectionValid(pts_section));
262 if (pts_dts_flags == 0x3) {
263 RCHECK(bit_reader.ReadBits(40, &pts_section));
264 RCHECK(bit_reader.ReadBits(40, &dts_section));
265 RCHECK((((pts_section >> 36) & 0xf) == 0x3) &&
266 IsTimestampSectionValid(pts_section));
267 RCHECK((((dts_section >> 36) & 0xf) == 0x1) &&
268 IsTimestampSectionValid(dts_section));
274 int64_t media_pts(kNoTimestamp);
275 int64_t media_dts(kNoTimestamp);
277 int64_t dts = ConvertTimestampSectionToTimestamp(dts_section);
278 if (previous_dts_valid_)
279 dts = UnrollTimestamp(previous_dts_, dts);
281 previous_dts_valid_ =
true;
285 int64_t pts = ConvertTimestampSectionToTimestamp(pts_section);
286 if (previous_pts_valid_) {
287 pts = UnrollTimestamp(previous_pts_, pts);
289 if (media_dts != kNoTimestamp) {
290 pts = UnrollTimestamp(media_dts, pts);
294 previous_pts_valid_ =
true;
299 DCHECK_EQ(bit_reader.bits_available() % 8, 0u);
300 int pes_header_remaining_size =
301 pes_header_data_length -
302 (pes_header_start_size -
303 static_cast<int>(bit_reader.bits_available()) / 8);
304 RCHECK(pes_header_remaining_size >= 0);
307 DVLOG(LOG_LEVEL_PES) <<
"Emit a reassembled PES:"
308 <<
" size=" << es_size <<
" pts=" << media_pts
309 <<
" dts=" << media_dts <<
" data_alignment_indicator="
310 << data_alignment_indicator;
311 return es_parser_->Parse(&raw_pes[es_offset], es_size, media_pts, media_dts);
314void TsSectionPes::ResetPesState() {
315 pes_byte_queue_.Reset();
316 wait_for_pusi_ =
true;
All the methods that are virtual are virtual for mocking.