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 DVLOG_IF(1, k != 0) << "SYNC: nbytes_skipped=" << k; |
| 42 return k; |
| 43 } |
| 44 |
| 45 // static |
| 46 TsPacket* TsPacket::Parse(const uint8* buf, int size) { |
| 47 if (size < kPacketSize) { |
| 48 DVLOG(1) << "Buffer does not hold one full TS packet:" |
| 49 << " buffer_size=" << size; |
| 50 return NULL; |
| 51 } |
| 52 |
| 53 DCHECK_EQ(buf[0], kTsHeaderSyncword); |
| 54 if (buf[0] != kTsHeaderSyncword) { |
| 55 DVLOG(1) << "Not on a TS syncword:" |
| 56 << " buf[0]=" |
| 57 << std::hex << static_cast<int>(buf[0]) << std::dec; |
| 58 return NULL; |
| 59 } |
| 60 |
| 61 scoped_ptr<TsPacket> ts_packet(new TsPacket()); |
| 62 bool status = ts_packet->ParseHeader(buf); |
| 63 if (!status) { |
| 64 DVLOG(1) << "Parsing header failed"; |
| 65 return NULL; |
| 66 } |
| 67 return ts_packet.release(); |
| 68 } |
| 69 |
| 70 TsPacket::TsPacket() { |
| 71 } |
| 72 |
| 73 TsPacket::~TsPacket() { |
| 74 } |
| 75 |
| 76 bool TsPacket::ParseHeader(const uint8* buf) { |
| 77 BitReader bit_reader(buf, kPacketSize); |
| 78 payload_ = buf; |
| 79 payload_size_ = kPacketSize; |
| 80 |
| 81 // Read the TS header: 4 bytes. |
| 82 int syncword; |
| 83 bool transport_error_indicator; |
| 84 bool transport_priority; |
| 85 int transport_scrambling_control; |
| 86 int adaptation_field_control; |
| 87 RCHECK(bit_reader.ReadBits(8, &syncword)); |
| 88 RCHECK(bit_reader.ReadBits(1, &transport_error_indicator)); |
| 89 RCHECK(bit_reader.ReadBits(1, &payload_unit_start_indicator_)); |
| 90 RCHECK(bit_reader.ReadBits(1, &transport_priority)); |
| 91 RCHECK(bit_reader.ReadBits(13, &pid_)); |
| 92 RCHECK(bit_reader.ReadBits(2, &transport_scrambling_control)); |
| 93 RCHECK(bit_reader.ReadBits(2, &adaptation_field_control)); |
| 94 RCHECK(bit_reader.ReadBits(4, &continuity_counter_)); |
| 95 payload_ += 4; |
| 96 payload_size_ -= 4; |
| 97 |
| 98 // Default values when no adaptation field. |
| 99 discontinuity_indicator_ = false; |
| 100 random_access_indicator_ = false; |
| 101 |
| 102 // Done since no adaptation field. |
| 103 if ((adaptation_field_control & 0x2) == 0) |
| 104 return true; |
| 105 |
| 106 // Read the adaptation field if needed. |
| 107 int adaptation_field_length; |
| 108 RCHECK(bit_reader.ReadBits(8, &adaptation_field_length)); |
| 109 DVLOG(LOG_LEVEL_TS) << "adaptation_field_length=" << adaptation_field_length; |
| 110 payload_ += 1; |
| 111 payload_size_ -= 1; |
| 112 if ((adaptation_field_control & 0x1) == 0 && |
| 113 adaptation_field_length != 183) { |
| 114 DVLOG(1) << "adaptation_field_length=" << adaptation_field_length; |
| 115 return false; |
| 116 } |
| 117 if ((adaptation_field_control & 0x1) == 1 && |
| 118 adaptation_field_length > 182) { |
| 119 DVLOG(1) << "adaptation_field_length=" << adaptation_field_length; |
| 120 // This is not allowed by the spec. |
| 121 // However, some badly encoded streams are using |
| 122 // adaptation_field_length = 183 |
| 123 return false; |
| 124 } |
| 125 |
| 126 // adaptation_field_length = '0' is used to insert a single stuffing byte |
| 127 // in the adaptation field of a transport stream packet. |
| 128 if (adaptation_field_length == 0) |
| 129 return true; |
| 130 |
| 131 bool status = ParseAdaptationField(&bit_reader, adaptation_field_length); |
| 132 payload_ += 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 int adaptation_field_start_marker = bit_reader->bits_available() / 8; |
| 141 |
| 142 bool elementary_stream_priority_indicator; |
| 143 bool pcr_flag; |
| 144 bool opcr_flag; |
| 145 bool splicing_point_flag; |
| 146 bool transport_private_data_flag; |
| 147 bool adaptation_field_extension_flag; |
| 148 RCHECK(bit_reader->ReadBits(1, &discontinuity_indicator_)); |
| 149 RCHECK(bit_reader->ReadBits(1, &random_access_indicator_)); |
| 150 RCHECK(bit_reader->ReadBits(1, &elementary_stream_priority_indicator)); |
| 151 RCHECK(bit_reader->ReadBits(1, &pcr_flag)); |
| 152 RCHECK(bit_reader->ReadBits(1, &opcr_flag)); |
| 153 RCHECK(bit_reader->ReadBits(1, &splicing_point_flag)); |
| 154 RCHECK(bit_reader->ReadBits(1, &transport_private_data_flag)); |
| 155 RCHECK(bit_reader->ReadBits(1, &adaptation_field_extension_flag)); |
| 156 |
| 157 if (pcr_flag) { |
| 158 int64 program_clock_reference_base; |
| 159 int reserved; |
| 160 int program_clock_reference_extension; |
| 161 RCHECK(bit_reader->ReadBits(33, &program_clock_reference_base)); |
| 162 RCHECK(bit_reader->ReadBits(6, &reserved)); |
| 163 RCHECK(bit_reader->ReadBits(9, &program_clock_reference_extension)); |
| 164 } |
| 165 |
| 166 if (opcr_flag) { |
| 167 int64 original_program_clock_reference_base; |
| 168 int reserved; |
| 169 int original_program_clock_reference_extension; |
| 170 RCHECK(bit_reader->ReadBits(33, &original_program_clock_reference_base)); |
| 171 RCHECK(bit_reader->ReadBits(6, &reserved)); |
| 172 RCHECK( |
| 173 bit_reader->ReadBits(9, &original_program_clock_reference_extension)); |
| 174 } |
| 175 |
| 176 if (splicing_point_flag) { |
| 177 int splice_countdown; |
| 178 RCHECK(bit_reader->ReadBits(8, &splice_countdown)); |
| 179 } |
| 180 |
| 181 if (transport_private_data_flag) { |
| 182 int transport_private_data_length; |
| 183 RCHECK(bit_reader->ReadBits(8, &transport_private_data_length)); |
| 184 RCHECK(bit_reader->SkipBits(8 * transport_private_data_length)); |
| 185 } |
| 186 |
| 187 if (adaptation_field_extension_flag) { |
| 188 int adaptation_field_extension_length; |
| 189 RCHECK(bit_reader->ReadBits(8, &adaptation_field_extension_length)); |
| 190 RCHECK(bit_reader->SkipBits(8 * adaptation_field_extension_length)); |
| 191 } |
| 192 |
| 193 // The rest of the adaptation field should be stuffing bytes. |
| 194 int adaptation_field_remaining_size = adaptation_field_length - |
| 195 (adaptation_field_start_marker - bit_reader->bits_available() / 8); |
| 196 RCHECK(adaptation_field_remaining_size >= 0); |
| 197 for (int k = 0; k < adaptation_field_remaining_size; k++) { |
| 198 int stuffing_byte; |
| 199 RCHECK(bit_reader->ReadBits(8, &stuffing_byte)); |
| 200 RCHECK(stuffing_byte == 0xff); |
| 201 } |
| 202 |
| 203 DVLOG(LOG_LEVEL_TS) << "random_access_indicator=" << random_access_indicator_; |
| 204 return true; |
| 205 } |
| 206 |
| 207 } // namespace mp2t |
| 208 } // namespace media |
| 209 |
OLD | NEW |