Chromium Code Reviews| Index: media/mp2t/ts_section_psi.cc |
| diff --git a/media/mp2t/ts_section_psi.cc b/media/mp2t/ts_section_psi.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7271ad078737a62770a19f45755776faa5b2af6a |
| --- /dev/null |
| +++ b/media/mp2t/ts_section_psi.cc |
| @@ -0,0 +1,151 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/mp2t/ts_section_psi.h" |
| + |
| +#include "base/logging.h" |
| +#include "media/base/bit_reader.h" |
| + |
| +namespace media { |
| +namespace mp2t { |
| + |
| +class Mpeg2TsCrc { |
| + public: |
| + Mpeg2TsCrc(); |
| + |
| + // Update the CRC with 8 bits of |data|. |
| + void Update(uint8 data); |
| + |
| + bool IsValid(); |
| + |
| + private: |
| + uint32 crc_; |
| +}; |
| + |
| +Mpeg2TsCrc::Mpeg2TsCrc() |
| + : crc_(0xffffffffu) { |
| +} |
| + |
| +void Mpeg2TsCrc::Update(uint8 data) { |
| + const uint32_t kCrcPoly = 0x4c11db7; |
| + |
| + // Align the bits to the MSB. |
| + int nbits = 8; |
| + uint32 data_msb_aligned = data; |
| + data_msb_aligned <<= (32 - nbits); |
| + |
| + while (nbits > 0) { |
| + if ((data_msb_aligned ^ crc_) & 0x80000000) { |
| + crc_ <<= 1; |
| + crc_ ^= kCrcPoly; |
| + } else { |
| + crc_ <<= 1; |
| + } |
| + |
| + data_msb_aligned <<= 1; |
| + nbits--; |
| + } |
| +} |
| + |
| +bool Mpeg2TsCrc::IsValid() { |
| + return (crc_ == 0); |
| +} |
| + |
| +TsSectionPsi::TsSectionPsi() |
| + : wait_for_pusi_(true), |
| + leading_bytes_to_discard_(0) { |
| +} |
| + |
| +TsSectionPsi::~TsSectionPsi() { |
| +} |
| + |
| +bool TsSectionPsi::Parse(bool payload_unit_start_indicator, |
| + const uint8* buf, int size) { |
| + // Ignore partial PSI. |
| + if (wait_for_pusi_ && !payload_unit_start_indicator) |
| + return true; |
| + |
| + if (payload_unit_start_indicator) { |
| + // Reset the state of the PSI section. |
| + ResetPsiState(); |
| + |
| + // Update the state. |
| + wait_for_pusi_ = false; |
| + DCHECK_GE(size, 1); |
| + int pointer_field = buf[0]; |
| + leading_bytes_to_discard_ = pointer_field; |
| + buf++; |
| + size--; |
| + } |
| + |
| + // Discard some leading bytes if needed. |
| + if (leading_bytes_to_discard_ > 0) { |
| + int nbytes_to_discard = std::min(leading_bytes_to_discard_, size); |
| + buf += nbytes_to_discard; |
| + size -= nbytes_to_discard; |
| + leading_bytes_to_discard_ -= nbytes_to_discard; |
| + } |
| + if (size == 0) |
| + return true; |
| + |
| + // Add the data to the parser state. |
| + psi_byte_queue_.Push(buf, size); |
| + int raw_psi_size; |
| + const uint8* raw_psi; |
| + psi_byte_queue_.Peek(&raw_psi, &raw_psi_size); |
| + |
| + // Check whether we have enough data to start parsing. |
| + if (raw_psi_size < 3) |
| + return true; |
| + int section_length = |
| + ((static_cast<int>(raw_psi[1]) << 8) | |
| + (static_cast<int>(raw_psi[2]))) & 0xfff; |
| + if (section_length >= 1021) |
| + return false; |
| + int psi_length = section_length + 3; |
| + if (raw_psi_size < psi_length) { |
| + // Don't throw an error when there is not enough data, |
| + // just wait for more data to come. |
| + return true; |
| + } |
| + |
| + // There should not be any trailing bytes after a PMT. |
| + // Instead, the pointer field should be used to stuff bytes. |
| + DVLOG_IF(1, raw_psi_size > psi_length) |
| + << "Trailing bytes after a PSI section: " |
| + << psi_length << " vs " << raw_psi_size; |
| + |
| + // Verify the CRC. |
| + Mpeg2TsCrc crc; |
| + for (int k = 0; k < psi_length; k++) |
|
acolwell GONE FROM CHROMIUM
2013/09/16 06:19:30
nit: Since this CRC class doesn't appear to be eve
damienv1
2013/09/17 02:58:23
Done.
|
| + crc.Update(raw_psi[k]); |
| + if (!crc.IsValid()) |
| + return false; |
| + |
| + // Parse the PSI section. |
| + BitReader bit_reader(raw_psi, raw_psi_size); |
| + bool status = ParsePsiSection(&bit_reader); |
| + if (status) |
| + ResetPsiState(); |
| + |
| + return status; |
| +} |
| + |
| +void TsSectionPsi::Flush() { |
| +} |
| + |
| +void TsSectionPsi::Reset() { |
| + ResetPsiSection(); |
| + ResetPsiState(); |
| +} |
| + |
| +void TsSectionPsi::ResetPsiState() { |
| + wait_for_pusi_ = true; |
| + psi_byte_queue_.Reset(); |
| + leading_bytes_to_discard_ = 0; |
| +} |
| + |
| +} // namespace mp2t |
| +} // namespace media |
| + |