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