Shaka Packager SDK
Loading...
Searching...
No Matches
ts_section_psi.cc
1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <packager/media/formats/mp2t/ts_section_psi.h>
6
7#include <algorithm>
8#include <cstdint>
9
10#include <absl/log/check.h>
11#include <absl/log/log.h>
12
13#include <packager/media/base/bit_reader.h>
14#include <packager/media/formats/mp2t/mp2t_common.h>
15
16static bool IsCrcValid(const uint8_t* buf, int size) {
17 uint32_t crc = 0xffffffffu;
18 const uint32_t kCrcPoly = 0x4c11db7;
19
20 for (int k = 0; k < size; k++) {
21 int nbits = 8;
22 uint32_t data_msb_aligned = buf[k];
23 data_msb_aligned <<= (32 - nbits);
24
25 while (nbits > 0) {
26 if ((data_msb_aligned ^ crc) & 0x80000000) {
27 crc <<= 1;
28 crc ^= kCrcPoly;
29 } else {
30 crc <<= 1;
31 }
32
33 data_msb_aligned <<= 1;
34 nbits--;
35 }
36 }
37
38 return (crc == 0);
39}
40
41namespace shaka {
42namespace media {
43namespace mp2t {
44
45TsSectionPsi::TsSectionPsi()
46 : wait_for_pusi_(true), leading_bytes_to_discard_(0) {}
47
48TsSectionPsi::~TsSectionPsi() {}
49
50bool TsSectionPsi::Parse(bool payload_unit_start_indicator,
51 const uint8_t* buf,
52 int size) {
53 // Ignore partial PSI.
54 if (wait_for_pusi_ && !payload_unit_start_indicator)
55 return true;
56
57 if (payload_unit_start_indicator) {
58 // Reset the state of the PSI section.
59 ResetPsiState();
60
61 // Update the state.
62 wait_for_pusi_ = false;
63 DCHECK_GE(size, 1);
64 int pointer_field = buf[0];
65 leading_bytes_to_discard_ = pointer_field;
66 buf++;
67 size--;
68 }
69
70 // Discard some leading bytes if needed.
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;
76 }
77 if (size == 0)
78 return true;
79
80 // Add the data to the parser state.
81 psi_byte_queue_.Push(buf, size);
82 int raw_psi_size;
83 const uint8_t* raw_psi;
84 psi_byte_queue_.Peek(&raw_psi, &raw_psi_size);
85
86 // Check whether we have enough data to start parsing.
87 if (raw_psi_size < 3)
88 return true;
89 int section_length =
90 ((static_cast<int>(raw_psi[1]) << 8) | (static_cast<int>(raw_psi[2]))) &
91 0xfff;
92 if (section_length >= 1021)
93 return false;
94 int psi_length = section_length + 3;
95 if (raw_psi_size < psi_length) {
96 // Don't throw an error when there is not enough data,
97 // just wait for more data to come.
98 return true;
99 }
100
101 // There should not be any trailing bytes after a PMT.
102 // Instead, the pointer field should be used to stuff bytes.
103 if (raw_psi_size > psi_length) {
104 DVLOG(1) << "Trailing bytes after a PSI section: " << psi_length << " vs "
105 << raw_psi_size;
106 }
107
108 // Verify the CRC.
109 RCHECK(IsCrcValid(raw_psi, psi_length));
110
111 // Parse the PSI section.
112 BitReader bit_reader(raw_psi, raw_psi_size);
113 bool status = ParsePsiSection(&bit_reader);
114 if (status)
115 ResetPsiState();
116
117 return status;
118}
119
120bool TsSectionPsi::Flush() {
121 return true;
122}
123
124void TsSectionPsi::Reset() {
125 ResetPsiSection();
126 ResetPsiState();
127}
128
129void TsSectionPsi::ResetPsiState() {
130 wait_for_pusi_ = true;
131 psi_byte_queue_.Reset();
132 leading_bytes_to_discard_ = 0;
133}
134
135} // namespace mp2t
136} // namespace media
137} // namespace shaka
All the methods that are virtual are virtual for mocking.