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.
5 #include "media/formats/mp2t/ts_section_psi.h"
7 #include "base/basictypes.h"
8 #include "base/logging.h"
9 #include "media/base/bit_reader.h"
10 #include "media/formats/mp2t/mp2t_common.h"
12 static bool IsCrcValid(const uint8
* buf
, int size
) {
13 uint32 crc
= 0xffffffffu
;
14 const uint32 kCrcPoly
= 0x4c11db7;
16 for (int k
= 0; k
< size
; k
++) {
18 uint32 data_msb_aligned
= buf
[k
];
19 data_msb_aligned
<<= (32 - nbits
);
22 if ((data_msb_aligned
^ crc
) & 0x80000000) {
29 data_msb_aligned
<<= 1;
40 TsSectionPsi::TsSectionPsi()
41 : wait_for_pusi_(true),
42 leading_bytes_to_discard_(0) {
45 TsSectionPsi::~TsSectionPsi() {
48 bool TsSectionPsi::Parse(bool payload_unit_start_indicator
,
49 const uint8
* buf
, int size
) {
50 // Ignore partial PSI.
51 if (wait_for_pusi_
&& !payload_unit_start_indicator
)
54 if (payload_unit_start_indicator
) {
55 // Reset the state of the PSI section.
59 wait_for_pusi_
= false;
61 int pointer_field
= buf
[0];
62 leading_bytes_to_discard_
= pointer_field
;
67 // Discard some leading bytes if needed.
68 if (leading_bytes_to_discard_
> 0) {
69 int nbytes_to_discard
= std::min(leading_bytes_to_discard_
, size
);
70 buf
+= nbytes_to_discard
;
71 size
-= nbytes_to_discard
;
72 leading_bytes_to_discard_
-= nbytes_to_discard
;
77 // Add the data to the parser state.
78 psi_byte_queue_
.Push(buf
, size
);
81 psi_byte_queue_
.Peek(&raw_psi
, &raw_psi_size
);
83 // Check whether we have enough data to start parsing.
87 ((static_cast<int>(raw_psi
[1]) << 8) |
88 (static_cast<int>(raw_psi
[2]))) & 0xfff;
89 if (section_length
>= 1021)
91 int psi_length
= section_length
+ 3;
92 if (raw_psi_size
< psi_length
) {
93 // Don't throw an error when there is not enough data,
94 // just wait for more data to come.
98 // There should not be any trailing bytes after a PMT.
99 // Instead, the pointer field should be used to stuff bytes.
100 DVLOG_IF(1, raw_psi_size
> psi_length
)
101 << "Trailing bytes after a PSI section: "
102 << psi_length
<< " vs " << raw_psi_size
;
105 RCHECK(IsCrcValid(raw_psi
, psi_length
));
107 // Parse the PSI section.
108 BitReader
bit_reader(raw_psi
, raw_psi_size
);
109 bool status
= ParsePsiSection(&bit_reader
);
116 void TsSectionPsi::Flush() {
119 void TsSectionPsi::Reset() {
124 void TsSectionPsi::ResetPsiState() {
125 wait_for_pusi_
= true;
126 psi_byte_queue_
.Reset();
127 leading_bytes_to_discard_
= 0;