5 #include "packager/media/formats/mp2t/ts_section_psi.h" 11 #include "packager/base/logging.h" 12 #include "packager/media/base/bit_reader.h" 13 #include "packager/media/formats/mp2t/mp2t_common.h" 15 static bool IsCrcValid(
const uint8_t* buf,
int size) {
16 uint32_t crc = 0xffffffffu;
17 const uint32_t kCrcPoly = 0x4c11db7;
19 for (
int k = 0; k < size; k++) {
21 uint32_t data_msb_aligned = buf[k];
22 data_msb_aligned <<= (32 - nbits);
25 if ((data_msb_aligned ^ crc) & 0x80000000) {
32 data_msb_aligned <<= 1;
44 TsSectionPsi::TsSectionPsi()
45 : wait_for_pusi_(true),
46 leading_bytes_to_discard_(0) {
49 TsSectionPsi::~TsSectionPsi() {
52 bool TsSectionPsi::Parse(
bool payload_unit_start_indicator,
56 if (wait_for_pusi_ && !payload_unit_start_indicator)
59 if (payload_unit_start_indicator) {
64 wait_for_pusi_ =
false;
66 int pointer_field = buf[0];
67 leading_bytes_to_discard_ = pointer_field;
73 if (leading_bytes_to_discard_ > 0) {
74 int nbytes_to_discard = std::min(leading_bytes_to_discard_, size);
75 buf += nbytes_to_discard;
76 size -= nbytes_to_discard;
77 leading_bytes_to_discard_ -= nbytes_to_discard;
83 psi_byte_queue_.Push(buf, size);
85 const uint8_t* raw_psi;
86 psi_byte_queue_.Peek(&raw_psi, &raw_psi_size);
92 ((
static_cast<int>(raw_psi[1]) << 8) |
93 (
static_cast<int>(raw_psi[2]))) & 0xfff;
94 if (section_length >= 1021)
96 int psi_length = section_length + 3;
97 if (raw_psi_size < psi_length) {
105 DVLOG_IF(1, raw_psi_size > psi_length)
106 <<
"Trailing bytes after a PSI section: " 107 << psi_length <<
" vs " << raw_psi_size;
110 RCHECK(IsCrcValid(raw_psi, psi_length));
113 BitReader bit_reader(raw_psi, raw_psi_size);
114 bool status = ParsePsiSection(&bit_reader);
121 void TsSectionPsi::Flush() {
124 void TsSectionPsi::Reset() {
129 void TsSectionPsi::ResetPsiState() {
130 wait_for_pusi_ =
true;
131 psi_byte_queue_.Reset();
132 leading_bytes_to_discard_ = 0;
All the methods that are virtual are virtual for mocking.