Chromium Code Reviews| Index: media/mp2t/ts_packet.cc |
| diff --git a/media/mp2t/ts_packet.cc b/media/mp2t/ts_packet.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c876b0969b677307ab56bcb83da037a2505f3e48 |
| --- /dev/null |
| +++ b/media/mp2t/ts_packet.cc |
| @@ -0,0 +1,235 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/mp2t/ts_packet.h" |
| + |
| +#include "base/memory/scoped_ptr.h" |
| +#include "media/base/bit_reader.h" |
| +#include "media/mp2t/mp2t_common.h" |
| + |
| +namespace media { |
| +namespace mp2t { |
| + |
| +static const uint8 kTsHeaderSyncword = 0x47; |
| + |
| +// static |
| +int TsPacket::Sync(const uint8* buf, int size) { |
| + int k = 0; |
| + for (; k < size; k++) { |
| + // Verify that we have 4 syncwords in a row when possible, |
| + // this should improve synchronization robustness. |
| + // TODO(damienv): Consider the case where there is garbage |
| + // between TS packets. |
| + bool is_header = true; |
| + for (int i = 0; i < 4; i++) { |
| + int idx = k + i * kPacketSize; |
| + if (idx >= size) |
| + break; |
| + if (buf[idx] != kTsHeaderSyncword) { |
| + DVLOG(LOG_LEVEL_TS) |
| + << "ByteSync" << idx << ": " |
| + << std::hex << static_cast<int>(buf[idx]) << std::dec; |
| + is_header = false; |
| + break; |
| + } |
| + } |
| + if (is_header) { |
|
acolwell GONE FROM CHROMIUM
2013/09/16 06:19:30
nit: remove {}
damienv1
2013/09/17 02:58:22
Done.
|
| + break; |
| + } |
| + } |
| + |
| + 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.
|
| + return k; |
| +} |
| + |
| +// static |
| +TsPacket* TsPacket::Parse(const uint8* buf, int size) { |
| + if (size < kPacketSize) { |
| + 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.
|
| + << " buffer_size=" << size; |
| + return NULL; |
| + } |
| + |
| + DCHECK_EQ(buf[0], kTsHeaderSyncword); |
| + if (buf[0] != kTsHeaderSyncword) { |
| + DVLOG(1) << "Not on a TS syncword:" |
| + << " buf[0]=" |
| + << std::hex << static_cast<int>(buf[0]) << std::dec; |
| + return NULL; |
| + } |
| + |
| + scoped_ptr<TsPacket> ts_packet(new TsPacket()); |
| + BitReader bit_reader(buf, kPacketSize); |
| + |
| + bool status = ts_packet->ParseHeader(&bit_reader); |
| + if (!status) { |
| + DVLOG(1) << "Parsing header failed"; |
| + return NULL; |
| + } |
| + 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.
|
| + |
| + return ts_packet.release(); |
| +} |
| + |
| +TsPacket::TsPacket() { |
| +} |
| + |
| +TsPacket::~TsPacket() { |
| +} |
| + |
| +bool TsPacket::ParseHeader(BitReader* bit_reader) { |
| + payload_size_ = kPacketSize; |
| + |
| + // Read the TS header: 4 bytes. |
| + int syncword; |
| + bool transport_error_indicator; |
| + bool transport_priority; |
| + int transport_scrambling_control; |
| + int adaptation_field_control; |
| + RCHECK(bit_reader->ReadBits(8, &syncword)); |
| + RCHECK(bit_reader->ReadBits(1, &transport_error_indicator)); |
| + RCHECK(bit_reader->ReadBits(1, &payload_unit_start_indicator_)); |
| + RCHECK(bit_reader->ReadBits(1, &transport_priority)); |
| + RCHECK(bit_reader->ReadBits(13, &pid_)); |
| + RCHECK(bit_reader->ReadBits(2, &transport_scrambling_control)); |
| + RCHECK(bit_reader->ReadBits(2, &adaptation_field_control)); |
| + RCHECK(bit_reader->ReadBits(4, &continuity_counter_)); |
| + payload_size_ -= 4; |
| + |
| + // Default values when no adaptation field. |
| + discontinuity_indicator_ = false; |
| + random_access_indicator_ = false; |
| + |
| + // Done since no adaptation field. |
| + if ((adaptation_field_control & 0x2) == 0) |
| + return true; |
| + |
| + // Read the adaptation field if needed. |
| + int adaptation_field_length; |
| + RCHECK(bit_reader->ReadBits(8, &adaptation_field_length)); |
| + DVLOG(LOG_LEVEL_TS) << "adaptation_field_length=" << adaptation_field_length; |
| + payload_size_ -= 1; |
| + if ((adaptation_field_control & 0x1) == 0 && |
| + adaptation_field_length != 183) { |
| + DVLOG(1) << "adaptation_field_length=" << adaptation_field_length; |
| + return false; |
| + } |
| + if ((adaptation_field_control & 0x1) == 1 && |
| + adaptation_field_length > 182) { |
| + DVLOG(1) << "adaptation_field_length=" << adaptation_field_length; |
| + // This is not allowed by the spec. |
| + // However, some badly encoded streams are using |
| + // adaptation_field_length = 183 |
| + // 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.
|
| + } |
| + |
| + // adaptation_field_length = '0' is used to insert a single stuffing byte |
| + // in the adaptation field of a transport stream packet. |
| + if (adaptation_field_length == 0) |
| + return true; |
| + |
| + bool status = ParseAdaptationField(bit_reader, adaptation_field_length); |
| + payload_size_ -= adaptation_field_length; |
| + return status; |
| +} |
| + |
| +bool TsPacket::ParseAdaptationField(BitReader* bit_reader, |
| + int adaptation_field_length) { |
| + DCHECK_GT(adaptation_field_length, 0); |
| + |
| + bool elementary_stream_priority_indicator; |
| + bool pcr_flag; |
| + bool opcr_flag; |
| + bool splicing_point_flag; |
| + bool transport_private_data_flag; |
| + bool adaptation_field_extension_flag; |
| + RCHECK(bit_reader->ReadBits(1, &discontinuity_indicator_)); |
| + RCHECK(bit_reader->ReadBits(1, &random_access_indicator_)); |
| + RCHECK(bit_reader->ReadBits(1, &elementary_stream_priority_indicator)); |
| + RCHECK(bit_reader->ReadBits(1, &pcr_flag)); |
| + RCHECK(bit_reader->ReadBits(1, &opcr_flag)); |
| + RCHECK(bit_reader->ReadBits(1, &splicing_point_flag)); |
| + RCHECK(bit_reader->ReadBits(1, &transport_private_data_flag)); |
| + RCHECK(bit_reader->ReadBits(1, &adaptation_field_extension_flag)); |
| + 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.
|
| + |
| + if (pcr_flag) { |
| + 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.
|
| + int64 program_clock_reference_base; |
| + int reserved; |
| + int program_clock_reference_extension; |
| + RCHECK(bit_reader->ReadBits(33, &program_clock_reference_base)); |
| + RCHECK(bit_reader->ReadBits(6, &reserved)); |
| + RCHECK(bit_reader->ReadBits(9, &program_clock_reference_extension)); |
| + adaptation_field_length -= 6; |
| + } |
| + |
| + if (opcr_flag) { |
| + RCHECK(adaptation_field_length >= 6); |
| + int64 original_program_clock_reference_base; |
| + int reserved; |
| + int original_program_clock_reference_extension; |
| + RCHECK(bit_reader->ReadBits(33, &original_program_clock_reference_base)); |
| + RCHECK(bit_reader->ReadBits(6, &reserved)); |
| + RCHECK( |
| + bit_reader->ReadBits(9, &original_program_clock_reference_extension)); |
| + adaptation_field_length -= 6; |
| + } |
| + |
| + if (splicing_point_flag) { |
| + RCHECK(adaptation_field_length >= 1); |
| + int splice_countdown; |
| + RCHECK(bit_reader->ReadBits(8, &splice_countdown)); |
| + adaptation_field_length -= 1; |
| + } |
| + |
| + if (transport_private_data_flag) { |
| + RCHECK(adaptation_field_length >= 1); |
| + int transport_private_data_length; |
| + RCHECK(bit_reader->ReadBits(8, &transport_private_data_length)); |
| + adaptation_field_length -= 1; |
| + |
| + RCHECK(adaptation_field_length >= transport_private_data_length); |
| + for (int k = 0; k < transport_private_data_length; k++) { |
| + int private_data_byte; |
| + 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.
|
| + } |
| + adaptation_field_length -= transport_private_data_length; |
| + } |
| + |
| + if (adaptation_field_extension_flag) { |
| + RCHECK(adaptation_field_length >= 1); |
| + int adaptation_field_extension_length; |
| + RCHECK(bit_reader->ReadBits(8, &adaptation_field_extension_length)); |
| + adaptation_field_length -= 1; |
| + |
| + RCHECK(adaptation_field_length >= adaptation_field_extension_length); |
| + for (int k = 0; k < adaptation_field_extension_length; k++) { |
| + int dummy_byte; |
| + 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.
|
| + } |
| + adaptation_field_length -= adaptation_field_extension_length; |
| + } |
| + |
| + for (int k = 0; k < adaptation_field_length; k++) { |
| + int stuffing_byte; |
| + RCHECK(bit_reader->ReadBits(8, &stuffing_byte)); |
| + RCHECK(stuffing_byte == 0xff); |
| + } |
| + |
| + DVLOG(LOG_LEVEL_TS) << "random_access_indicator=" << random_access_indicator_; |
| + return true; |
| +} |
| + |
| +const uint8* TsPacket::GetPayload() const { |
| + return payload_; |
| +} |
| + |
| +int TsPacket::GetPayloadSize() const { |
| + return payload_size_; |
| +} |
| + |
| +} // namespace mp2t |
| +} // namespace media |
| + |