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/mpeg2/mpeg2ts_psi.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "media/base/bit_reader.h" |
| 9 #include "media/mpeg2/mpeg2ts_crc.h" |
| 10 |
| 11 namespace media { |
| 12 namespace mpeg2ts { |
| 13 |
| 14 Mpeg2TsPsiParser::Mpeg2TsPsiParser() |
| 15 : wait_for_pusi_(true), |
| 16 leading_bytes_to_discard_(0) { |
| 17 } |
| 18 |
| 19 Mpeg2TsPsiParser::~Mpeg2TsPsiParser() { |
| 20 } |
| 21 |
| 22 bool Mpeg2TsPsiParser::Parse(bool payload_unit_start_indicator, |
| 23 const uint8* buf, int size) { |
| 24 bool parse_result = ParseInternal(payload_unit_start_indicator, buf, size); |
| 25 if (!parse_result) |
| 26 ResetState(); |
| 27 return parse_result; |
| 28 } |
| 29 |
| 30 bool Mpeg2TsPsiParser::ParseInternal(bool payload_unit_start_indicator, |
| 31 const uint8* buf, int size) { |
| 32 // Ignore partial PSI. |
| 33 if (wait_for_pusi_ && !payload_unit_start_indicator) |
| 34 return true; |
| 35 |
| 36 if (payload_unit_start_indicator) { |
| 37 // Reset the state of the PSI section. |
| 38 ResetState(); |
| 39 |
| 40 // Update the state. |
| 41 wait_for_pusi_ = false; |
| 42 DCHECK_GE(size, 1); |
| 43 int pointer_field = buf[0]; |
| 44 leading_bytes_to_discard_ = pointer_field; |
| 45 buf++; |
| 46 size--; |
| 47 } |
| 48 |
| 49 // Discard some leading bytes if needed. |
| 50 if (leading_bytes_to_discard_ > 0) { |
| 51 int nbytes_to_discard = std::min(leading_bytes_to_discard_, size); |
| 52 buf += nbytes_to_discard; |
| 53 size -= nbytes_to_discard; |
| 54 leading_bytes_to_discard_ -= nbytes_to_discard; |
| 55 } |
| 56 if (size == 0) |
| 57 return true; |
| 58 |
| 59 // Add the data to the parser state. |
| 60 psi_byte_queue_.Push(buf, size); |
| 61 int raw_psi_size = 0; |
| 62 const uint8* raw_psi = NULL; |
| 63 psi_byte_queue_.Peek(&raw_psi, &raw_psi_size); |
| 64 |
| 65 // Check whether we have enough data to start parsing. |
| 66 if (raw_psi_size < 3) |
| 67 return true; |
| 68 int section_length = |
| 69 ((static_cast<int>(raw_psi[1]) << 8) | |
| 70 (static_cast<int>(raw_psi[2]))) & 0xfff; |
| 71 if (section_length >= 1021) |
| 72 return false; |
| 73 int psi_length = section_length + 3; |
| 74 if (raw_psi_size < psi_length) { |
| 75 // Don't throw an error when there is not enough data, |
| 76 // just wait for more data to come. |
| 77 return true; |
| 78 } |
| 79 |
| 80 // There should not be any trailing bytes after a PMT. |
| 81 // Instead, the pointer field should be used to stuff bytes. |
| 82 LOG_IF(WARNING, raw_psi_size > psi_length) |
| 83 << "Trailing bytes after a PSI section: " |
| 84 << psi_length << " vs " << raw_psi_size; |
| 85 |
| 86 // Verify the CRC. |
| 87 Mpeg2TsCrc crc; |
| 88 for (int k = 0; k < psi_length; k++) |
| 89 crc.Update(raw_psi[k], 8); |
| 90 if (!crc.IsValid()) |
| 91 return false; |
| 92 |
| 93 // Parse the PSI section. |
| 94 BitReader bit_reader(raw_psi, raw_psi_size); |
| 95 bool status = ParsePsiSection(&bit_reader); |
| 96 if (status) |
| 97 ResetState(); |
| 98 |
| 99 return status; |
| 100 } |
| 101 |
| 102 void Mpeg2TsPsiParser::Flush() { |
| 103 ResetState(); |
| 104 } |
| 105 |
| 106 void Mpeg2TsPsiParser::ResetState() { |
| 107 wait_for_pusi_ = true; |
| 108 psi_byte_queue_.Reset(); |
| 109 leading_bytes_to_discard_ = 0; |
| 110 } |
| 111 |
| 112 } // namespace mpeg2ts |
| 113 } // namespace media |
| 114 |
OLD | NEW |