Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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/formats/mp2t/descriptors.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "media/base/bit_reader.h" | |
| 11 #include "media/formats/mp2t/mp2t_common.h" | |
| 12 | |
| 13 namespace media { | |
| 14 namespace mp2t { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // Tag values for various kinds of descriptors for which there is specific | |
| 19 // parsing support herein. | |
| 20 enum DescriptorTag { | |
| 21 DESCRIPTOR_TAG_REGISTRATION = 5, | |
| 22 DESCRIPTOR_TAG_CA = 9, | |
| 23 DESCRIPTOR_TAG_PRIVATE_DATA_INDICATOR = 15, | |
| 24 }; | |
| 25 | |
| 26 const int kCASystemIdCenc = 0x6365; // 'ce' | |
| 27 | |
| 28 class StringBitReader : public BitReader { | |
| 29 public: | |
| 30 StringBitReader(const std::string& input); | |
| 31 ~StringBitReader() override; | |
| 32 }; | |
| 33 | |
| 34 StringBitReader::StringBitReader(const std::string& input) | |
| 35 : BitReader(reinterpret_cast<const uint8_t*>(input.data()), input.size()) {} | |
| 36 | |
| 37 StringBitReader::~StringBitReader() {} | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 41 Descriptors::Descriptors() {} | |
| 42 | |
| 43 Descriptors::Descriptors(const Descriptors& other) = default; | |
| 44 | |
| 45 Descriptors::~Descriptors() {} | |
| 46 | |
| 47 bool Descriptors::Read(BitReader* reader, int size) { | |
| 48 DCHECK(reader); | |
| 49 DCHECK(size >= 0); | |
| 50 descriptors_.clear(); | |
| 51 if (size == 0) | |
| 52 return true; | |
| 53 int initial_bits_read = reader->bits_read(); | |
| 54 int bits_read = 0; | |
| 55 int bits_available = reader->bits_available(); | |
| 56 int size_in_bits = 8 * size; | |
| 57 if (size_in_bits > bits_available) | |
| 58 return false; | |
| 59 bits_available = size_in_bits; | |
| 60 do { | |
| 61 int tag; | |
| 62 size_t length; | |
| 63 RCHECK(reader->ReadBits(8, &tag)); | |
| 64 RCHECK(reader->ReadBits(8, &length)); | |
| 65 char data[256]; | |
| 66 for (size_t i = 0; i < length; i++) { | |
| 67 RCHECK(reader->ReadBits(8, &data[i])); | |
| 68 } | |
| 69 descriptors_.insert(Descriptor(tag, std::string(data, length))); | |
|
yucliu1
2016/05/26 23:28:47
nit: Use 'emplace' instead?
dougsteed
2016/09/25 21:52:29
std::map::emplace is not yet supported.
| |
| 70 bits_read = reader->bits_read() - initial_bits_read; | |
| 71 } while (bits_read < bits_available); | |
| 72 return bits_read == bits_available; | |
| 73 } | |
| 74 | |
| 75 bool Descriptors::HasRegistrationDescriptor( | |
| 76 int64_t* format_identifier, | |
| 77 std::string* additional_info) const { | |
| 78 DCHECK(format_identifier); | |
| 79 DCHECK(additional_info); | |
| 80 auto search = descriptors_.find(DESCRIPTOR_TAG_REGISTRATION); | |
| 81 if (search == descriptors_.end()) | |
| 82 return false; | |
| 83 const std::string& data = search->second; | |
| 84 StringBitReader reader(data); | |
| 85 RCHECK(reader.ReadBits(32, format_identifier)); | |
| 86 size_t extra_bits = reader.bits_available(); | |
| 87 RCHECK(extra_bits % 8 == 0); | |
| 88 RCHECK(extra_bits > 0); | |
| 89 RCHECK(reader.ReadString(extra_bits, additional_info)); | |
| 90 return true; | |
| 91 } | |
| 92 | |
| 93 bool Descriptors::HasCADescriptor(int* system_id, | |
| 94 int* pid, | |
| 95 std::string* private_data) const { | |
| 96 DCHECK(system_id); | |
| 97 DCHECK(pid); | |
| 98 DCHECK(private_data); | |
| 99 auto search = descriptors_.find(DESCRIPTOR_TAG_CA); | |
| 100 if (search == descriptors_.end()) | |
| 101 return false; | |
| 102 const std::string& data = search->second; | |
| 103 StringBitReader reader(data); | |
| 104 RCHECK(reader.ReadBits(16, system_id)); | |
| 105 RCHECK(reader.SkipBits(3)); | |
| 106 RCHECK(reader.ReadBits(13, pid)); | |
| 107 size_t extra_bits = reader.bits_available(); | |
| 108 RCHECK(extra_bits % 8 == 0); | |
| 109 RCHECK(reader.ReadString(extra_bits, private_data)); | |
| 110 return true; | |
| 111 } | |
| 112 | |
| 113 bool Descriptors::HasCADescriptorCenc(int* ca_pid, int* pssh_pid) const { | |
| 114 DCHECK(ca_pid); | |
| 115 DCHECK(pssh_pid); | |
| 116 int system_id; | |
| 117 std::string private_data; | |
| 118 if (!HasCADescriptor(&system_id, ca_pid, &private_data)) | |
| 119 return false; | |
| 120 if (system_id != kCASystemIdCenc) | |
| 121 return false; | |
| 122 StringBitReader reader(private_data); | |
| 123 uint32_t scheme_type; | |
| 124 uint32_t scheme_version; | |
| 125 int num_systems; | |
| 126 int encryption_algorithm; | |
| 127 char pssh_system_id[16]; | |
| 128 // TODO(dougsteed). Currently we don't check many of the following values. | |
| 129 // When we flesh out this implementation to cover all of ISO/IEC 23001-9 we | |
| 130 // will need to use and check these values. | |
| 131 RCHECK(reader.ReadBits(32, &scheme_type)); | |
| 132 RCHECK(reader.ReadBits(32, &scheme_version)); | |
| 133 RCHECK(reader.ReadBits(8, &num_systems)); | |
| 134 RCHECK(num_systems == 1); | |
| 135 RCHECK(reader.ReadBits(24, &encryption_algorithm)); | |
| 136 for (size_t i = 0; i < 16; i++) { | |
| 137 RCHECK(reader.ReadBits(8, &pssh_system_id[i])); | |
| 138 } | |
| 139 RCHECK(reader.ReadBits(13, pssh_pid)); | |
| 140 return true; | |
| 141 } | |
| 142 | |
| 143 bool Descriptors::HasPrivateDataIndicator(int64_t value) const { | |
| 144 int64_t private_data_indicator; | |
| 145 auto search = descriptors_.find(DESCRIPTOR_TAG_PRIVATE_DATA_INDICATOR); | |
| 146 if (search == descriptors_.end()) | |
| 147 return false; | |
| 148 const std::string& data = search->second; | |
| 149 StringBitReader reader(data); | |
| 150 RCHECK(reader.ReadBits(32, &private_data_indicator)); | |
| 151 RCHECK(reader.bits_available() == 0); | |
| 152 return private_data_indicator == value; | |
| 153 } | |
| 154 | |
| 155 } // namespace mp2t | |
| 156 } // namespace media | |
| OLD | NEW |