Chromium Code Reviews| 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 = | |
| 25 ParseInternal(payload_unit_start_indicator, | |
| 26 buf, size); | |
| 27 if (parse_result == false) { | |
| 28 ResetState(); | |
| 29 } | |
| 30 return parse_result; | |
| 31 } | |
| 32 | |
| 33 bool Mpeg2TsPsiParser::ParseInternal(bool payload_unit_start_indicator, | |
| 34 const uint8* buf, int size) { | |
| 35 if (wait_for_pusi_ && !payload_unit_start_indicator) { | |
| 36 // Ignore partial PSI. | |
| 37 return true; | |
| 38 } | |
| 39 | |
| 40 if (payload_unit_start_indicator) { | |
| 41 // Reset the state of the PSI section. | |
| 42 int raw_psi_size = 0; | |
|
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
nit: remove initialization since it is immediately
damienv1
2013/09/09 23:29:45
Not relevant anymore (code removed).
| |
| 43 const uint8* raw_psi = NULL; | |
| 44 psi_byte_queue_.Peek(&raw_psi, &raw_psi_size); | |
| 45 LOG_IF(WARNING, raw_psi_size > 0) | |
|
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
DVLOG here and below
damienv1
2013/09/09 23:29:45
Ditto.
| |
| 46 << "Dropping state: len=" << raw_psi_size; | |
| 47 ResetState(); | |
| 48 | |
| 49 // Update the state. | |
| 50 wait_for_pusi_ = false; | |
| 51 DCHECK_GE(size, 1); | |
| 52 int pointer_field = buf[0]; | |
| 53 leading_bytes_to_discard_ = pointer_field; | |
| 54 buf++; | |
| 55 size--; | |
| 56 } | |
| 57 | |
| 58 // Discard some leading bytes if needed. | |
| 59 if (leading_bytes_to_discard_ > 0) { | |
| 60 int nbytes_to_discard = std::min(leading_bytes_to_discard_, size); | |
| 61 buf += nbytes_to_discard; | |
| 62 size -= nbytes_to_discard; | |
| 63 leading_bytes_to_discard_ -= nbytes_to_discard; | |
| 64 } | |
| 65 if (size == 0) { | |
|
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
nit: remove{} here and all 1 line ifs below
damienv1
2013/09/09 23:29:45
Done.
| |
| 66 return true; | |
| 67 } | |
| 68 | |
| 69 // Add the data to the parser state. | |
| 70 psi_byte_queue_.Push(buf, size); | |
| 71 int raw_psi_size = 0; | |
|
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
ditto
damienv1
2013/09/09 23:29:45
Done.
| |
| 72 const uint8* raw_psi = NULL; | |
| 73 psi_byte_queue_.Peek(&raw_psi, &raw_psi_size); | |
| 74 | |
| 75 // Check whether we have enough data to start parsing. | |
| 76 if (raw_psi_size < 3) | |
| 77 return true; | |
| 78 int section_length = | |
| 79 ((static_cast<int>(raw_psi[1]) << 8) | | |
| 80 (static_cast<int>(raw_psi[2]))) & 0xfff; | |
| 81 if (section_length >= 1021) { | |
| 82 return false; | |
| 83 } | |
| 84 int psi_length = section_length + 3; | |
| 85 if (raw_psi_size < psi_length) { | |
| 86 // Don't throw an error when there is not enough data, | |
| 87 // just wait for more data to come. | |
| 88 return true; | |
| 89 } | |
| 90 | |
| 91 // There should not be any trailing bytes after a PMT. | |
| 92 // Instead, the pointer field should be used to stuff bytes. | |
| 93 LOG_IF(WARNING, raw_psi_size > psi_length) | |
| 94 << "Trailing bytes after a PSI section: " | |
| 95 << psi_length << " vs " << raw_psi_size; | |
| 96 | |
| 97 // Verify the CRC. | |
| 98 Mpeg2TsCrc crc; | |
| 99 for (int k = 0; k < psi_length; k++) { | |
| 100 crc.Update(raw_psi[k], 8); | |
|
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
This appears to be the only call site for this met
damienv1
2013/09/09 23:29:45
Done.
| |
| 101 } | |
| 102 if (!crc.IsValid()) { | |
| 103 return false; | |
| 104 } | |
| 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 ResetState(); | |
| 111 } | |
| 112 | |
| 113 return status; | |
| 114 } | |
| 115 | |
| 116 void Mpeg2TsPsiParser::Flush() { | |
| 117 ResetState(); | |
| 118 } | |
| 119 | |
| 120 void Mpeg2TsPsiParser::ResetState() { | |
| 121 wait_for_pusi_ = true; | |
| 122 psi_byte_queue_.Reset(); | |
| 123 leading_bytes_to_discard_ = 0; | |
| 124 } | |
| 125 | |
| 126 } // namespace mpeg2ts | |
| 127 } // namespace media | |
| OLD | NEW |