5 #include <packager/media/formats/mp2t/ts_section_psi.h>
10 #include <absl/log/check.h>
11 #include <absl/log/log.h>
13 #include <packager/macros/logging.h>
14 #include <packager/media/base/bit_reader.h>
15 #include <packager/media/formats/mp2t/mp2t_common.h>
17 static bool IsCrcValid(
const uint8_t* buf,
int size) {
18 uint32_t crc = 0xffffffffu;
19 const uint32_t kCrcPoly = 0x4c11db7;
21 for (
int k = 0; k < size; k++) {
23 uint32_t data_msb_aligned = buf[k];
24 data_msb_aligned <<= (32 - nbits);
27 if ((data_msb_aligned ^ crc) & 0x80000000) {
34 data_msb_aligned <<= 1;
46 TsSectionPsi::TsSectionPsi()
47 : wait_for_pusi_(true),
48 leading_bytes_to_discard_(0) {
51 TsSectionPsi::~TsSectionPsi() {
54 bool TsSectionPsi::Parse(
bool payload_unit_start_indicator,
58 if (wait_for_pusi_ && !payload_unit_start_indicator)
61 if (payload_unit_start_indicator) {
66 wait_for_pusi_ =
false;
68 int pointer_field = buf[0];
69 leading_bytes_to_discard_ = pointer_field;
75 if (leading_bytes_to_discard_ > 0) {
76 int nbytes_to_discard = std::min(leading_bytes_to_discard_, size);
77 buf += nbytes_to_discard;
78 size -= nbytes_to_discard;
79 leading_bytes_to_discard_ -= nbytes_to_discard;
85 psi_byte_queue_.Push(buf, size);
87 const uint8_t* raw_psi;
88 psi_byte_queue_.Peek(&raw_psi, &raw_psi_size);
94 ((
static_cast<int>(raw_psi[1]) << 8) |
95 (
static_cast<int>(raw_psi[2]))) & 0xfff;
96 if (section_length >= 1021)
98 int psi_length = section_length + 3;
99 if (raw_psi_size < psi_length) {
107 if (raw_psi_size > psi_length) {
108 DVLOG(1) <<
"Trailing bytes after a PSI section: " << psi_length <<
" vs "
113 RCHECK(IsCrcValid(raw_psi, psi_length));
116 BitReader bit_reader(raw_psi, raw_psi_size);
117 bool status = ParsePsiSection(&bit_reader);
124 bool TsSectionPsi::Flush() {
128 void TsSectionPsi::Reset() {
133 void TsSectionPsi::ResetPsiState() {
134 wait_for_pusi_ =
true;
135 psi_byte_queue_.Reset();
136 leading_bytes_to_discard_ = 0;
All the methods that are virtual are virtual for mocking.