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