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