5 #include "packager/media/formats/mp2t/ts_section_psi.h"
9 #include "packager/base/logging.h"
10 #include "packager/media/base/bit_reader.h"
11 #include "packager/media/formats/mp2t/mp2t_common.h"
13 static bool IsCrcValid(
const uint8_t* buf,
int size) {
14 uint32_t crc = 0xffffffffu;
15 const uint32_t kCrcPoly = 0x4c11db7;
17 for (
int k = 0; k < size; k++) {
19 uint32_t data_msb_aligned = buf[k];
20 data_msb_aligned <<= (32 - nbits);
23 if ((data_msb_aligned ^ crc) & 0x80000000) {
30 data_msb_aligned <<= 1;
42 TsSectionPsi::TsSectionPsi()
43 : wait_for_pusi_(true),
44 leading_bytes_to_discard_(0) {
47 TsSectionPsi::~TsSectionPsi() {
50 bool TsSectionPsi::Parse(
bool payload_unit_start_indicator,
54 if (wait_for_pusi_ && !payload_unit_start_indicator)
57 if (payload_unit_start_indicator) {
62 wait_for_pusi_ =
false;
64 int pointer_field = buf[0];
65 leading_bytes_to_discard_ = pointer_field;
71 if (leading_bytes_to_discard_ > 0) {
72 int nbytes_to_discard = std::min(leading_bytes_to_discard_, size);
73 buf += nbytes_to_discard;
74 size -= nbytes_to_discard;
75 leading_bytes_to_discard_ -= nbytes_to_discard;
81 psi_byte_queue_.
Push(buf, size);
83 const uint8_t* raw_psi;
84 psi_byte_queue_.
Peek(&raw_psi, &raw_psi_size);
90 ((
static_cast<int>(raw_psi[1]) << 8) |
91 (
static_cast<int>(raw_psi[2]))) & 0xfff;
92 if (section_length >= 1021)
94 int psi_length = section_length + 3;
95 if (raw_psi_size < psi_length) {
103 DVLOG_IF(1, raw_psi_size > psi_length)
104 <<
"Trailing bytes after a PSI section: "
105 << psi_length <<
" vs " << raw_psi_size;
108 RCHECK(IsCrcValid(raw_psi, psi_length));
111 BitReader bit_reader(raw_psi, raw_psi_size);
112 bool status = ParsePsiSection(&bit_reader);
119 void TsSectionPsi::Flush() {
122 void TsSectionPsi::Reset() {
127 void TsSectionPsi::ResetPsiState() {
128 wait_for_pusi_ =
true;
129 psi_byte_queue_.
Reset();
130 leading_bytes_to_discard_ = 0;