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_pat.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "media/base/bit_reader.h" |
| 9 |
| 10 namespace media { |
| 11 namespace mpeg2ts { |
| 12 |
| 13 Mpeg2TsPatParser::Mpeg2TsPatParser(RegisterPmtCb register_pmt_cb) |
| 14 : register_pmt_cb_(register_pmt_cb), |
| 15 version_number_(-1) { |
| 16 } |
| 17 |
| 18 Mpeg2TsPatParser::~Mpeg2TsPatParser() { |
| 19 } |
| 20 |
| 21 bool Mpeg2TsPatParser::ParsePsiSection(BitReader* bit_reader) { |
| 22 int remaining_size = bit_reader->bits_available() / 8; |
| 23 |
| 24 // Read the fixed section length. |
| 25 if (remaining_size < 8) { |
| 26 // This should not happen if the PAT size |
| 27 // was correctly encoded into the stream. |
| 28 LOG(WARNING) << "remaining_size=" << remaining_size; |
| 29 return false; |
| 30 } |
| 31 int table_id; |
| 32 DCHECK(bit_reader->ReadBits(8, &table_id)); |
| 33 if (table_id != 0x0) { |
| 34 // Table ID should be 0 for a PAT. |
| 35 LOG(WARNING) << "Unexpected table id: " << table_id; |
| 36 return false; |
| 37 } |
| 38 bool section_syntax_indicator; |
| 39 DCHECK(bit_reader->ReadBits(1, §ion_syntax_indicator)); |
| 40 if (!section_syntax_indicator) { |
| 41 LOG(WARNING) << "Unexpected section_syntax_indicator"; |
| 42 return false; |
| 43 } |
| 44 bool dummy_zero; |
| 45 DCHECK(bit_reader->ReadBits(1, &dummy_zero)); |
| 46 if (dummy_zero) { |
| 47 LOG(WARNING) << "Unexpected bit"; |
| 48 return false; |
| 49 } |
| 50 int reserved; |
| 51 DCHECK(bit_reader->ReadBits(2, &reserved)); |
| 52 int section_length; |
| 53 DCHECK(bit_reader->ReadBits(12, §ion_length)); |
| 54 if (section_length >= 1021) { |
| 55 return false; |
| 56 } |
| 57 int transport_stream_id; |
| 58 DCHECK(bit_reader->ReadBits(16, &transport_stream_id)); |
| 59 DCHECK(bit_reader->ReadBits(2, &reserved)); |
| 60 int version_number; |
| 61 DCHECK(bit_reader->ReadBits(5, &version_number)); |
| 62 bool current_next_indicator; |
| 63 DCHECK(bit_reader->ReadBits(1, ¤t_next_indicator)); |
| 64 int section_number; |
| 65 DCHECK(bit_reader->ReadBits(8, §ion_number)); |
| 66 int last_section_number; |
| 67 DCHECK(bit_reader->ReadBits(8, &last_section_number)); |
| 68 remaining_size -= 8; |
| 69 section_length -= 5; |
| 70 |
| 71 if (remaining_size < section_length) { |
| 72 // This should not happen if the PAT size |
| 73 // was correctly encoded into the stream. |
| 74 LOG(WARNING) << "remaining_size=" << remaining_size; |
| 75 return false; |
| 76 } |
| 77 |
| 78 // Minus 4 to account for the following CRC. |
| 79 if ((section_length & 0x3) != 0) { |
| 80 LOG(WARNING) << "PAT table size should be a multiple of 4"; |
| 81 return false; |
| 82 } |
| 83 int pmt_pid_count = (section_length - 4) / 4; |
| 84 std::vector<int> program_number_array(pmt_pid_count); |
| 85 std::vector<int> pmt_pid_array(pmt_pid_count); |
| 86 for (int k = 0; k < pmt_pid_count; k++) { |
| 87 if (remaining_size < 4) { |
| 88 // This should not happen if the PAT size |
| 89 // was correctly encoded into the stream. |
| 90 LOG(WARNING) << "remaining_size=" << remaining_size; |
| 91 return false; |
| 92 } |
| 93 DCHECK(bit_reader->ReadBits(16, &program_number_array[k])); |
| 94 int reserved; |
| 95 DCHECK(bit_reader->ReadBits(3, &reserved)); |
| 96 DCHECK(bit_reader->ReadBits(13, &pmt_pid_array[k])); |
| 97 remaining_size -= 4; |
| 98 } |
| 99 |
| 100 // Read the CRC. |
| 101 if (remaining_size < 4) { |
| 102 // This should not happen if the PAT size |
| 103 // was correctly encoded into the stream. |
| 104 LOG(WARNING) << "remaining_size=" << remaining_size; |
| 105 return false; |
| 106 } |
| 107 int crc32; |
| 108 DCHECK(bit_reader->ReadBits(32, &crc32)); |
| 109 remaining_size -= 4; |
| 110 |
| 111 // TODO(damienv): current_next_indicator flag is not handled at all. |
| 112 |
| 113 // Register the PMT only once the PAT is known to be well formed |
| 114 // and the version number has changed. |
| 115 if (version_number_ != version_number) { |
| 116 for (int k = 0; k < pmt_pid_count; k++) { |
| 117 if (program_number_array[k] != 0) { |
| 118 // Program numbers different from 0 correspond to PMT. |
| 119 register_pmt_cb_.Run(program_number_array[k], pmt_pid_array[k]); |
| 120 // Even if there are multiple programs, we can support only one. |
| 121 break; |
| 122 } |
| 123 } |
| 124 version_number_ = version_number; |
| 125 } |
| 126 |
| 127 // This should not happen if the PAT size |
| 128 // was correctly encoded into the stream. |
| 129 LOG_IF(WARNING, remaining_size != 0) |
| 130 << "remaining_size=" << remaining_size; |
| 131 |
| 132 return true; |
| 133 } |
| 134 |
| 135 } // namespace mpeg2ts |
| 136 } // namespace media |
OLD | NEW |