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/mp2t/ts_packet.h" | |
| 6 | |
| 7 #include "base/memory/scoped_ptr.h" | |
| 8 #include "media/base/bit_reader.h" | |
| 9 #include "media/mp2t/mp2t_common.h" | |
| 10 | |
| 11 namespace media { | |
| 12 namespace mp2t { | |
| 13 | |
| 14 static const uint8 kTsHeaderSyncword = 0x47; | |
| 15 | |
| 16 // static | |
| 17 int TsPacket::Sync(const uint8* buf, int size) { | |
| 18 int k = 0; | |
| 19 for (; k < size; k++) { | |
| 20 // Verify that we have 4 syncwords in a row when possible, | |
| 21 // this should improve synchronization robustness. | |
| 22 // TODO(damienv): Consider the case where there is garbage | |
| 23 // between TS packets. | |
| 24 bool is_header = true; | |
| 25 for (int i = 0; i < 4; i++) { | |
| 26 int idx = k + i * kPacketSize; | |
| 27 if (idx >= size) | |
| 28 break; | |
| 29 if (buf[idx] != kTsHeaderSyncword) { | |
| 30 DVLOG(LOG_LEVEL_TS) | |
| 31 << "ByteSync" << idx << ": " | |
| 32 << std::hex << static_cast<int>(buf[idx]) << std::dec; | |
| 33 is_header = false; | |
| 34 break; | |
| 35 } | |
| 36 } | |
| 37 if (is_header) { | |
|
acolwell GONE FROM CHROMIUM
2013/09/16 06:19:30
nit: remove {}
damienv1
2013/09/17 02:58:22
Done.
| |
| 38 break; | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 LOG_IF(WARNING, k != 0) << "SYNC: nbytes_skipped=" << k; | |
|
acolwell GONE FROM CHROMIUM
2013/09/16 06:19:30
nit: convert to DVLOG
damienv1
2013/09/17 02:58:22
Done.
| |
| 43 return k; | |
| 44 } | |
| 45 | |
| 46 // static | |
| 47 TsPacket* TsPacket::Parse(const uint8* buf, int size) { | |
| 48 if (size < kPacketSize) { | |
| 49 LOG(WARNING) << "Buffer does not hold one full TS packet:" | |
|
acolwell GONE FROM CHROMIUM
2013/09/16 06:19:30
nit: convert to DVLOG
damienv1
2013/09/17 02:58:22
Done.
| |
| 50 << " buffer_size=" << size; | |
| 51 return NULL; | |
| 52 } | |
| 53 | |
| 54 DCHECK_EQ(buf[0], kTsHeaderSyncword); | |
| 55 if (buf[0] != kTsHeaderSyncword) { | |
| 56 DVLOG(1) << "Not on a TS syncword:" | |
| 57 << " buf[0]=" | |
| 58 << std::hex << static_cast<int>(buf[0]) << std::dec; | |
| 59 return NULL; | |
| 60 } | |
| 61 | |
| 62 scoped_ptr<TsPacket> ts_packet(new TsPacket()); | |
| 63 BitReader bit_reader(buf, kPacketSize); | |
| 64 | |
| 65 bool status = ts_packet->ParseHeader(&bit_reader); | |
| 66 if (!status) { | |
| 67 DVLOG(1) << "Parsing header failed"; | |
| 68 return NULL; | |
| 69 } | |
| 70 ts_packet->payload_ = buf + (kPacketSize - ts_packet->payload_size_); | |
|
acolwell GONE FROM CHROMIUM
2013/09/16 06:19:30
nit: These seems out of place. How about just pass
damienv1
2013/09/17 02:58:22
Done.
| |
| 71 | |
| 72 return ts_packet.release(); | |
| 73 } | |
| 74 | |
| 75 TsPacket::TsPacket() { | |
| 76 } | |
| 77 | |
| 78 TsPacket::~TsPacket() { | |
| 79 } | |
| 80 | |
| 81 bool TsPacket::ParseHeader(BitReader* bit_reader) { | |
| 82 payload_size_ = kPacketSize; | |
| 83 | |
| 84 // Read the TS header: 4 bytes. | |
| 85 int syncword; | |
| 86 bool transport_error_indicator; | |
| 87 bool transport_priority; | |
| 88 int transport_scrambling_control; | |
| 89 int adaptation_field_control; | |
| 90 RCHECK(bit_reader->ReadBits(8, &syncword)); | |
| 91 RCHECK(bit_reader->ReadBits(1, &transport_error_indicator)); | |
| 92 RCHECK(bit_reader->ReadBits(1, &payload_unit_start_indicator_)); | |
| 93 RCHECK(bit_reader->ReadBits(1, &transport_priority)); | |
| 94 RCHECK(bit_reader->ReadBits(13, &pid_)); | |
| 95 RCHECK(bit_reader->ReadBits(2, &transport_scrambling_control)); | |
| 96 RCHECK(bit_reader->ReadBits(2, &adaptation_field_control)); | |
| 97 RCHECK(bit_reader->ReadBits(4, &continuity_counter_)); | |
| 98 payload_size_ -= 4; | |
| 99 | |
| 100 // Default values when no adaptation field. | |
| 101 discontinuity_indicator_ = false; | |
| 102 random_access_indicator_ = false; | |
| 103 | |
| 104 // Done since no adaptation field. | |
| 105 if ((adaptation_field_control & 0x2) == 0) | |
| 106 return true; | |
| 107 | |
| 108 // Read the adaptation field if needed. | |
| 109 int adaptation_field_length; | |
| 110 RCHECK(bit_reader->ReadBits(8, &adaptation_field_length)); | |
| 111 DVLOG(LOG_LEVEL_TS) << "adaptation_field_length=" << adaptation_field_length; | |
| 112 payload_size_ -= 1; | |
| 113 if ((adaptation_field_control & 0x1) == 0 && | |
| 114 adaptation_field_length != 183) { | |
| 115 DVLOG(1) << "adaptation_field_length=" << adaptation_field_length; | |
| 116 return false; | |
| 117 } | |
| 118 if ((adaptation_field_control & 0x1) == 1 && | |
| 119 adaptation_field_length > 182) { | |
| 120 DVLOG(1) << "adaptation_field_length=" << adaptation_field_length; | |
| 121 // This is not allowed by the spec. | |
| 122 // However, some badly encoded streams are using | |
| 123 // adaptation_field_length = 183 | |
| 124 // return false; | |
|
acolwell GONE FROM CHROMIUM
2013/09/16 06:19:30
nit: We should not encourage this behavior. I thin
damienv1
2013/09/17 02:58:22
Done.
| |
| 125 } | |
| 126 | |
| 127 // adaptation_field_length = '0' is used to insert a single stuffing byte | |
| 128 // in the adaptation field of a transport stream packet. | |
| 129 if (adaptation_field_length == 0) | |
| 130 return true; | |
| 131 | |
| 132 bool status = ParseAdaptationField(bit_reader, adaptation_field_length); | |
| 133 payload_size_ -= adaptation_field_length; | |
| 134 return status; | |
| 135 } | |
| 136 | |
| 137 bool TsPacket::ParseAdaptationField(BitReader* bit_reader, | |
| 138 int adaptation_field_length) { | |
| 139 DCHECK_GT(adaptation_field_length, 0); | |
| 140 | |
| 141 bool elementary_stream_priority_indicator; | |
| 142 bool pcr_flag; | |
| 143 bool opcr_flag; | |
| 144 bool splicing_point_flag; | |
| 145 bool transport_private_data_flag; | |
| 146 bool adaptation_field_extension_flag; | |
| 147 RCHECK(bit_reader->ReadBits(1, &discontinuity_indicator_)); | |
| 148 RCHECK(bit_reader->ReadBits(1, &random_access_indicator_)); | |
| 149 RCHECK(bit_reader->ReadBits(1, &elementary_stream_priority_indicator)); | |
| 150 RCHECK(bit_reader->ReadBits(1, &pcr_flag)); | |
| 151 RCHECK(bit_reader->ReadBits(1, &opcr_flag)); | |
| 152 RCHECK(bit_reader->ReadBits(1, &splicing_point_flag)); | |
| 153 RCHECK(bit_reader->ReadBits(1, &transport_private_data_flag)); | |
| 154 RCHECK(bit_reader->ReadBits(1, &adaptation_field_extension_flag)); | |
| 155 adaptation_field_length -= 1; | |
|
acolwell GONE FROM CHROMIUM
2013/09/16 06:19:30
nit: You shouldn't need to do all these length upd
damienv1
2013/09/17 02:58:22
Done.
| |
| 156 | |
| 157 if (pcr_flag) { | |
| 158 RCHECK(adaptation_field_length >= 6); | |
|
acolwell GONE FROM CHROMIUM
2013/09/16 06:19:30
These types of checks shouldn't be needed since yo
damienv1
2013/09/17 02:58:22
Done.
| |
| 159 int64 program_clock_reference_base; | |
| 160 int reserved; | |
| 161 int program_clock_reference_extension; | |
| 162 RCHECK(bit_reader->ReadBits(33, &program_clock_reference_base)); | |
| 163 RCHECK(bit_reader->ReadBits(6, &reserved)); | |
| 164 RCHECK(bit_reader->ReadBits(9, &program_clock_reference_extension)); | |
| 165 adaptation_field_length -= 6; | |
| 166 } | |
| 167 | |
| 168 if (opcr_flag) { | |
| 169 RCHECK(adaptation_field_length >= 6); | |
| 170 int64 original_program_clock_reference_base; | |
| 171 int reserved; | |
| 172 int original_program_clock_reference_extension; | |
| 173 RCHECK(bit_reader->ReadBits(33, &original_program_clock_reference_base)); | |
| 174 RCHECK(bit_reader->ReadBits(6, &reserved)); | |
| 175 RCHECK( | |
| 176 bit_reader->ReadBits(9, &original_program_clock_reference_extension)); | |
| 177 adaptation_field_length -= 6; | |
| 178 } | |
| 179 | |
| 180 if (splicing_point_flag) { | |
| 181 RCHECK(adaptation_field_length >= 1); | |
| 182 int splice_countdown; | |
| 183 RCHECK(bit_reader->ReadBits(8, &splice_countdown)); | |
| 184 adaptation_field_length -= 1; | |
| 185 } | |
| 186 | |
| 187 if (transport_private_data_flag) { | |
| 188 RCHECK(adaptation_field_length >= 1); | |
| 189 int transport_private_data_length; | |
| 190 RCHECK(bit_reader->ReadBits(8, &transport_private_data_length)); | |
| 191 adaptation_field_length -= 1; | |
| 192 | |
| 193 RCHECK(adaptation_field_length >= transport_private_data_length); | |
| 194 for (int k = 0; k < transport_private_data_length; k++) { | |
| 195 int private_data_byte; | |
| 196 RCHECK(bit_reader->ReadBits(8, &private_data_byte)); | |
|
acolwell GONE FROM CHROMIUM
2013/09/16 06:19:30
nit: You should be able to use RCHECK(bit_reader->
damienv1
2013/09/17 02:58:22
Done.
| |
| 197 } | |
| 198 adaptation_field_length -= transport_private_data_length; | |
| 199 } | |
| 200 | |
| 201 if (adaptation_field_extension_flag) { | |
| 202 RCHECK(adaptation_field_length >= 1); | |
| 203 int adaptation_field_extension_length; | |
| 204 RCHECK(bit_reader->ReadBits(8, &adaptation_field_extension_length)); | |
| 205 adaptation_field_length -= 1; | |
| 206 | |
| 207 RCHECK(adaptation_field_length >= adaptation_field_extension_length); | |
| 208 for (int k = 0; k < adaptation_field_extension_length; k++) { | |
| 209 int dummy_byte; | |
| 210 RCHECK(bit_reader->ReadBits(8, &dummy_byte)); | |
|
acolwell GONE FROM CHROMIUM
2013/09/16 06:19:30
ditto re: SkipBits()
damienv1
2013/09/17 02:58:22
Done.
| |
| 211 } | |
| 212 adaptation_field_length -= adaptation_field_extension_length; | |
| 213 } | |
| 214 | |
| 215 for (int k = 0; k < adaptation_field_length; k++) { | |
| 216 int stuffing_byte; | |
| 217 RCHECK(bit_reader->ReadBits(8, &stuffing_byte)); | |
| 218 RCHECK(stuffing_byte == 0xff); | |
| 219 } | |
| 220 | |
| 221 DVLOG(LOG_LEVEL_TS) << "random_access_indicator=" << random_access_indicator_; | |
| 222 return true; | |
| 223 } | |
| 224 | |
| 225 const uint8* TsPacket::GetPayload() const { | |
| 226 return payload_; | |
| 227 } | |
| 228 | |
| 229 int TsPacket::GetPayloadSize() const { | |
| 230 return payload_size_; | |
| 231 } | |
| 232 | |
| 233 } // namespace mp2t | |
| 234 } // namespace media | |
| 235 | |
| OLD | NEW |