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) { |
| 38 break; |
| 39 } |
| 40 } |
| 41 |
| 42 LOG_IF(WARNING, k != 0) << "SYNC: nbytes_skipped=" << k; |
| 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:" |
| 50 << " buffer_size=" << size; |
| 51 return NULL; |
| 52 } |
| 53 |
| 54 DCHECK_EQ(buf[0], kTsHeaderSyncword); |
| 55 if (buf[0] != kTsHeaderSyncword) { |
| 56 LOG(WARNING) << "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 LOG(WARNING) << "Parsing header failed"; |
| 68 return NULL; |
| 69 } |
| 70 ts_packet->payload_ = (buf + ts_packet->GetPayloadOffset()); |
| 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 DCHECK(bit_reader->ReadBits(8, &syncword)); |
| 87 bool transport_error_indicator; |
| 88 DCHECK(bit_reader->ReadBits(1, &transport_error_indicator)); |
| 89 DCHECK(bit_reader->ReadBits(1, &payload_unit_start_indicator_)); |
| 90 bool transport_priority; |
| 91 DCHECK(bit_reader->ReadBits(1, &transport_priority)); |
| 92 DCHECK(bit_reader->ReadBits(13, &pid_)); |
| 93 int transport_scrambling_control; |
| 94 DCHECK(bit_reader->ReadBits(2, &transport_scrambling_control)); |
| 95 int adaptation_field_control; |
| 96 DCHECK(bit_reader->ReadBits(2, &adaptation_field_control)); |
| 97 DCHECK(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 |
| 109 // Read the adaptation field if needed. |
| 110 int adaptation_field_length; |
| 111 DCHECK(bit_reader->ReadBits(8, &adaptation_field_length)); |
| 112 DVLOG(LOG_LEVEL_TS) << "adaptation_field_length=" << adaptation_field_length; |
| 113 payload_size_ -= 1; |
| 114 if ((adaptation_field_control & 0x1) == 0 && |
| 115 adaptation_field_length != 183) { |
| 116 LOG(WARNING) << "adaptation_field_length=" << adaptation_field_length; |
| 117 return false; |
| 118 } |
| 119 if ((adaptation_field_control & 0x1) == 1 && |
| 120 adaptation_field_length > 182) { |
| 121 LOG(WARNING) << "adaptation_field_length=" << adaptation_field_length; |
| 122 // This is not allowed by the spec. |
| 123 // However, some badly encoded streams are using |
| 124 // adaptation_field_length = 183 |
| 125 // return false; |
| 126 } |
| 127 |
| 128 if (adaptation_field_length == 0) { |
| 129 // adaptation_field_length = '0' is for inserting a single stuffing byte |
| 130 // in the adaptation field of a transport stream packet. |
| 131 return true; |
| 132 } |
| 133 |
| 134 bool status = ParseAdaptationField(bit_reader, adaptation_field_length); |
| 135 payload_size_ -= adaptation_field_length; |
| 136 return status; |
| 137 } |
| 138 |
| 139 bool TsPacket::ParseAdaptationField(BitReader* bit_reader, |
| 140 int adaptation_field_length) { |
| 141 DCHECK_GT(adaptation_field_length, 0); |
| 142 |
| 143 DCHECK(bit_reader->ReadBits(1, &discontinuity_indicator_)); |
| 144 DCHECK(bit_reader->ReadBits(1, &random_access_indicator_)); |
| 145 bool elementary_stream_priority_indicator; |
| 146 DCHECK(bit_reader->ReadBits(1, &elementary_stream_priority_indicator)); |
| 147 bool pcr_flag; |
| 148 DCHECK(bit_reader->ReadBits(1, &pcr_flag)); |
| 149 bool opcr_flag; |
| 150 DCHECK(bit_reader->ReadBits(1, &opcr_flag)); |
| 151 bool splicing_point_flag; |
| 152 DCHECK(bit_reader->ReadBits(1, &splicing_point_flag)); |
| 153 bool transport_private_data_flag; |
| 154 DCHECK(bit_reader->ReadBits(1, &transport_private_data_flag)); |
| 155 bool adaptation_field_extension_flag; |
| 156 DCHECK(bit_reader->ReadBits(1, &adaptation_field_extension_flag)); |
| 157 adaptation_field_length -= 1; |
| 158 |
| 159 if (pcr_flag) { |
| 160 if (adaptation_field_length < 6) { |
| 161 LOG(WARNING) << "adaptation_field_length=" << adaptation_field_length; |
| 162 return false; |
| 163 } |
| 164 int64 program_clock_reference_base; |
| 165 DCHECK(bit_reader->ReadBits(33, &program_clock_reference_base)); |
| 166 int reserved; |
| 167 DCHECK(bit_reader->ReadBits(6, &reserved)); |
| 168 int program_clock_reference_extension; |
| 169 DCHECK(bit_reader->ReadBits(9, &program_clock_reference_extension)); |
| 170 adaptation_field_length -= 6; |
| 171 } |
| 172 |
| 173 if (opcr_flag) { |
| 174 if (adaptation_field_length < 6) { |
| 175 LOG(WARNING) << "adaptation_field_length=" << adaptation_field_length; |
| 176 return false; |
| 177 } |
| 178 int64 original_program_clock_reference_base; |
| 179 DCHECK(bit_reader->ReadBits(33, &original_program_clock_reference_base)); |
| 180 int reserved; |
| 181 DCHECK(bit_reader->ReadBits(6, &reserved)); |
| 182 int original_program_clock_reference_extension; |
| 183 DCHECK( |
| 184 bit_reader->ReadBits(9, &original_program_clock_reference_extension)); |
| 185 adaptation_field_length -= 6; |
| 186 } |
| 187 |
| 188 if (splicing_point_flag) { |
| 189 if (adaptation_field_length < 1) { |
| 190 LOG(WARNING) << "adaptation_field_length=" << adaptation_field_length; |
| 191 return false; |
| 192 } |
| 193 int splice_countdown; |
| 194 DCHECK(bit_reader->ReadBits(8, &splice_countdown)); |
| 195 adaptation_field_length -= 1; |
| 196 } |
| 197 |
| 198 if (transport_private_data_flag) { |
| 199 if (adaptation_field_length < 1) { |
| 200 LOG(WARNING) << "adaptation_field_length=" << adaptation_field_length; |
| 201 return false; |
| 202 } |
| 203 int transport_private_data_length; |
| 204 DCHECK(bit_reader->ReadBits(8, &transport_private_data_length)); |
| 205 adaptation_field_length -= 1; |
| 206 |
| 207 if (adaptation_field_length < transport_private_data_length) { |
| 208 LOG(WARNING) << "adaptation_field_length=" << adaptation_field_length |
| 209 << " transport_private_data_length=" |
| 210 << transport_private_data_length; |
| 211 return false; |
| 212 } |
| 213 for (int k = 0; k < transport_private_data_length; k++) { |
| 214 int private_data_byte; |
| 215 DCHECK(bit_reader->ReadBits(8, &private_data_byte)); |
| 216 } |
| 217 adaptation_field_length -= transport_private_data_length; |
| 218 } |
| 219 |
| 220 if (adaptation_field_extension_flag) { |
| 221 if (adaptation_field_length < 1) { |
| 222 LOG(WARNING) << "adaptation_field_length=" << adaptation_field_length; |
| 223 return false; |
| 224 } |
| 225 int adaptation_field_extension_length; |
| 226 DCHECK(bit_reader->ReadBits(8, &adaptation_field_extension_length)); |
| 227 adaptation_field_length -= 1; |
| 228 |
| 229 if (adaptation_field_length < adaptation_field_extension_length) { |
| 230 LOG(WARNING) << "adaptation_field_length=" << adaptation_field_length; |
| 231 return false; |
| 232 } |
| 233 for (int k = 0; k < adaptation_field_extension_length; k++) { |
| 234 int dummy_byte; |
| 235 DCHECK(bit_reader->ReadBits(8, &dummy_byte)); |
| 236 } |
| 237 adaptation_field_length -= adaptation_field_extension_length; |
| 238 } |
| 239 |
| 240 for (int k = 0; k < adaptation_field_length; k++) { |
| 241 int stuffing_byte; |
| 242 DCHECK(bit_reader->ReadBits(8, &stuffing_byte)); |
| 243 if (stuffing_byte != 0xff) { |
| 244 LOG(WARNING) << "Invalid stuffing byte = " << stuffing_byte; |
| 245 return false; |
| 246 } |
| 247 } |
| 248 |
| 249 DVLOG(LOG_LEVEL_TS) << "random_access_indicator=" << random_access_indicator_; |
| 250 return true; |
| 251 } |
| 252 |
| 253 const uint8* TsPacket::GetPayload() const { |
| 254 return payload_; |
| 255 } |
| 256 |
| 257 int TsPacket::GetPayloadOffset() const { |
| 258 return (kPacketSize - payload_size_); |
| 259 } |
| 260 |
| 261 int TsPacket::GetPayloadSize() const { |
| 262 return payload_size_; |
| 263 } |
| 264 |
| 265 } // namespace mp2t |
| 266 } // namespace media |
| 267 |
OLD | NEW |