DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
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 <stdint.h>
8 
9 #include "packager/base/logging.h"
10 #include "packager/media/base/bit_reader.h"
11 #include "packager/media/formats/mp2t/mp2t_common.h"
12 
13 static bool IsCrcValid(const uint8_t* buf, int size) {
14  uint32_t crc = 0xffffffffu;
15  const uint32_t kCrcPoly = 0x4c11db7;
16 
17  for (int k = 0; k < size; k++) {
18  int nbits = 8;
19  uint32_t data_msb_aligned = buf[k];
20  data_msb_aligned <<= (32 - nbits);
21 
22  while (nbits > 0) {
23  if ((data_msb_aligned ^ crc) & 0x80000000) {
24  crc <<= 1;
25  crc ^= kCrcPoly;
26  } else {
27  crc <<= 1;
28  }
29 
30  data_msb_aligned <<= 1;
31  nbits--;
32  }
33  }
34 
35  return (crc == 0);
36 }
37 
38 namespace shaka {
39 namespace media {
40 namespace mp2t {
41 
42 TsSectionPsi::TsSectionPsi()
43  : wait_for_pusi_(true),
44  leading_bytes_to_discard_(0) {
45 }
46 
47 TsSectionPsi::~TsSectionPsi() {
48 }
49 
50 bool 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) |
91  (static_cast<int>(raw_psi[2]))) & 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  DVLOG_IF(1, raw_psi_size > psi_length)
104  << "Trailing bytes after a PSI section: "
105  << psi_length << " vs " << raw_psi_size;
106 
107  // Verify the CRC.
108  RCHECK(IsCrcValid(raw_psi, psi_length));
109 
110  // Parse the PSI section.
111  BitReader bit_reader(raw_psi, raw_psi_size);
112  bool status = ParsePsiSection(&bit_reader);
113  if (status)
114  ResetPsiState();
115 
116  return status;
117 }
118 
119 void TsSectionPsi::Flush() {
120 }
121 
122 void TsSectionPsi::Reset() {
123  ResetPsiSection();
124  ResetPsiState();
125 }
126 
127 void TsSectionPsi::ResetPsiState() {
128  wait_for_pusi_ = true;
129  psi_byte_queue_.Reset();
130  leading_bytes_to_discard_ = 0;
131 }
132 
133 } // namespace mp2t
134 } // namespace media
135 } // namespace shaka
void Push(const uint8_t *data, int size)
Append new bytes to the end of the queue.
Definition: byte_queue.cc:29
void Reset()
Reset the queue to the empty state.
Definition: byte_queue.cc:24
void Peek(const uint8_t **data, int *size) const
Definition: byte_queue.cc:63