| Index: media/mpeg2/mpeg2ts_psi.cc
|
| diff --git a/media/mpeg2/mpeg2ts_psi.cc b/media/mpeg2/mpeg2ts_psi.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..1100d73ffed3eb850de51564907c4a3dd24b0242
|
| --- /dev/null
|
| +++ b/media/mpeg2/mpeg2ts_psi.cc
|
| @@ -0,0 +1,123 @@
|
| +// 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/mpeg2/mpeg2ts_psi.h"
|
| +
|
| +#include "base/logging.h"
|
| +#include "media/base/bit_reader.h"
|
| +#include "media/mpeg2/mpeg2ts_crc.h"
|
| +
|
| +namespace media {
|
| +namespace mpeg2ts {
|
| +
|
| +Mpeg2TsPsiParser::Mpeg2TsPsiParser()
|
| + : wait_for_pusi_(true),
|
| + leading_bytes_to_discard_(0) {
|
| +}
|
| +
|
| +Mpeg2TsPsiParser::~Mpeg2TsPsiParser() {
|
| +}
|
| +
|
| +bool Mpeg2TsPsiParser::Parse(bool payload_unit_start_indicator,
|
| + const uint8* buf, int size) {
|
| + bool parse_result =
|
| + ParseInternal(payload_unit_start_indicator,
|
| + buf, size);
|
| + if (parse_result == false) {
|
| + ResetState();
|
| + }
|
| + return parse_result;
|
| +}
|
| +
|
| +bool Mpeg2TsPsiParser::ParseInternal(bool payload_unit_start_indicator,
|
| + const uint8* buf, int size) {
|
| + if (wait_for_pusi_ && !payload_unit_start_indicator) {
|
| + // Ignore partial PSI.
|
| + return true;
|
| + }
|
| +
|
| + if (payload_unit_start_indicator) {
|
| + // Reset the state of the PSI section.
|
| + LOG_IF(WARNING, raw_psi_.size() > 0)
|
| + << "Dropping state: len=" << raw_psi_.size();
|
| + ResetState();
|
| +
|
| + // 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.
|
| + int old_size = raw_psi_.size();
|
| + raw_psi_.resize(old_size + size);
|
| + memcpy(&raw_psi_[old_size], buf, 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 (static_cast<int>(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.
|
| + LOG_IF(WARNING, static_cast<int>(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++) {
|
| + crc.Update(raw_psi_[k], 8);
|
| + }
|
| + if (!crc.IsValid()) {
|
| + return false;
|
| + }
|
| +
|
| + // Parse the PSI section.
|
| + BitReader bit_reader(&raw_psi_[0], raw_psi_.size());
|
| + bool status = ParsePsiSection(&bit_reader);
|
| + if (status) {
|
| + ResetState();
|
| + }
|
| +
|
| + return status;
|
| +}
|
| +
|
| +void Mpeg2TsPsiParser::Flush() {
|
| + ResetState();
|
| +}
|
| +
|
| +void Mpeg2TsPsiParser::ResetState() {
|
| + wait_for_pusi_ = true;
|
| + raw_psi_.resize(0);
|
| + leading_bytes_to_discard_ = 0;
|
| +}
|
| +
|
| +} // namespace mpeg2ts
|
| +} // namespace media
|
|
|