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