OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/formats/mp2t/ts_packet.h" | 5 #include "media/formats/mp2t/ts_packet.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "media/base/bit_reader.h" | 8 #include "media/base/bit_reader.h" |
9 #include "media/formats/mp2t/mp2t_common.h" | 9 #include "media/formats/mp2t/mp2t_common.h" |
10 | 10 |
11 namespace media { | 11 namespace media { |
12 namespace mp2t { | 12 namespace mp2t { |
13 | 13 |
14 static const uint8 kTsHeaderSyncword = 0x47; | 14 static const uint8_t kTsHeaderSyncword = 0x47; |
15 | 15 |
16 // static | 16 // static |
17 int TsPacket::Sync(const uint8* buf, int size) { | 17 int TsPacket::Sync(const uint8_t* buf, int size) { |
18 int k = 0; | 18 int k = 0; |
19 for (; k < size; k++) { | 19 for (; k < size; k++) { |
20 // Verify that we have 4 syncwords in a row when possible, | 20 // Verify that we have 4 syncwords in a row when possible, |
21 // this should improve synchronization robustness. | 21 // this should improve synchronization robustness. |
22 // TODO(damienv): Consider the case where there is garbage | 22 // TODO(damienv): Consider the case where there is garbage |
23 // between TS packets. | 23 // between TS packets. |
24 bool is_header = true; | 24 bool is_header = true; |
25 for (int i = 0; i < 4; i++) { | 25 for (int i = 0; i < 4; i++) { |
26 int idx = k + i * kPacketSize; | 26 int idx = k + i * kPacketSize; |
27 if (idx >= size) | 27 if (idx >= size) |
28 break; | 28 break; |
29 if (buf[idx] != kTsHeaderSyncword) { | 29 if (buf[idx] != kTsHeaderSyncword) { |
30 DVLOG(LOG_LEVEL_TS) | 30 DVLOG(LOG_LEVEL_TS) |
31 << "ByteSync" << idx << ": " | 31 << "ByteSync" << idx << ": " |
32 << std::hex << static_cast<int>(buf[idx]) << std::dec; | 32 << std::hex << static_cast<int>(buf[idx]) << std::dec; |
33 is_header = false; | 33 is_header = false; |
34 break; | 34 break; |
35 } | 35 } |
36 } | 36 } |
37 if (is_header) | 37 if (is_header) |
38 break; | 38 break; |
39 } | 39 } |
40 | 40 |
41 DVLOG_IF(1, k != 0) << "SYNC: nbytes_skipped=" << k; | 41 DVLOG_IF(1, k != 0) << "SYNC: nbytes_skipped=" << k; |
42 return k; | 42 return k; |
43 } | 43 } |
44 | 44 |
45 // static | 45 // static |
46 TsPacket* TsPacket::Parse(const uint8* buf, int size) { | 46 TsPacket* TsPacket::Parse(const uint8_t* buf, int size) { |
47 if (size < kPacketSize) { | 47 if (size < kPacketSize) { |
48 DVLOG(1) << "Buffer does not hold one full TS packet:" | 48 DVLOG(1) << "Buffer does not hold one full TS packet:" |
49 << " buffer_size=" << size; | 49 << " buffer_size=" << size; |
50 return NULL; | 50 return NULL; |
51 } | 51 } |
52 | 52 |
53 DCHECK_EQ(buf[0], kTsHeaderSyncword); | 53 DCHECK_EQ(buf[0], kTsHeaderSyncword); |
54 if (buf[0] != kTsHeaderSyncword) { | 54 if (buf[0] != kTsHeaderSyncword) { |
55 DVLOG(1) << "Not on a TS syncword:" | 55 DVLOG(1) << "Not on a TS syncword:" |
56 << " buf[0]=" | 56 << " buf[0]=" |
57 << std::hex << static_cast<int>(buf[0]) << std::dec; | 57 << std::hex << static_cast<int>(buf[0]) << std::dec; |
58 return NULL; | 58 return NULL; |
59 } | 59 } |
60 | 60 |
61 scoped_ptr<TsPacket> ts_packet(new TsPacket()); | 61 scoped_ptr<TsPacket> ts_packet(new TsPacket()); |
62 bool status = ts_packet->ParseHeader(buf); | 62 bool status = ts_packet->ParseHeader(buf); |
63 if (!status) { | 63 if (!status) { |
64 DVLOG(1) << "Parsing header failed"; | 64 DVLOG(1) << "Parsing header failed"; |
65 return NULL; | 65 return NULL; |
66 } | 66 } |
67 return ts_packet.release(); | 67 return ts_packet.release(); |
68 } | 68 } |
69 | 69 |
70 TsPacket::TsPacket() { | 70 TsPacket::TsPacket() { |
71 } | 71 } |
72 | 72 |
73 TsPacket::~TsPacket() { | 73 TsPacket::~TsPacket() { |
74 } | 74 } |
75 | 75 |
76 bool TsPacket::ParseHeader(const uint8* buf) { | 76 bool TsPacket::ParseHeader(const uint8_t* buf) { |
77 BitReader bit_reader(buf, kPacketSize); | 77 BitReader bit_reader(buf, kPacketSize); |
78 payload_ = buf; | 78 payload_ = buf; |
79 payload_size_ = kPacketSize; | 79 payload_size_ = kPacketSize; |
80 | 80 |
81 // Read the TS header: 4 bytes. | 81 // Read the TS header: 4 bytes. |
82 int syncword; | 82 int syncword; |
83 int transport_error_indicator; | 83 int transport_error_indicator; |
84 int payload_unit_start_indicator; | 84 int payload_unit_start_indicator; |
85 int transport_priority; | 85 int transport_priority; |
86 int transport_scrambling_control; | 86 int transport_scrambling_control; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 RCHECK(bit_reader->ReadBits(1, &elementary_stream_priority_indicator)); | 154 RCHECK(bit_reader->ReadBits(1, &elementary_stream_priority_indicator)); |
155 RCHECK(bit_reader->ReadBits(1, &pcr_flag)); | 155 RCHECK(bit_reader->ReadBits(1, &pcr_flag)); |
156 RCHECK(bit_reader->ReadBits(1, &opcr_flag)); | 156 RCHECK(bit_reader->ReadBits(1, &opcr_flag)); |
157 RCHECK(bit_reader->ReadBits(1, &splicing_point_flag)); | 157 RCHECK(bit_reader->ReadBits(1, &splicing_point_flag)); |
158 RCHECK(bit_reader->ReadBits(1, &transport_private_data_flag)); | 158 RCHECK(bit_reader->ReadBits(1, &transport_private_data_flag)); |
159 RCHECK(bit_reader->ReadBits(1, &adaptation_field_extension_flag)); | 159 RCHECK(bit_reader->ReadBits(1, &adaptation_field_extension_flag)); |
160 discontinuity_indicator_ = (discontinuity_indicator != 0); | 160 discontinuity_indicator_ = (discontinuity_indicator != 0); |
161 random_access_indicator_ = (random_access_indicator != 0); | 161 random_access_indicator_ = (random_access_indicator != 0); |
162 | 162 |
163 if (pcr_flag) { | 163 if (pcr_flag) { |
164 int64 program_clock_reference_base; | 164 int64_t program_clock_reference_base; |
165 int reserved; | 165 int reserved; |
166 int program_clock_reference_extension; | 166 int program_clock_reference_extension; |
167 RCHECK(bit_reader->ReadBits(33, &program_clock_reference_base)); | 167 RCHECK(bit_reader->ReadBits(33, &program_clock_reference_base)); |
168 RCHECK(bit_reader->ReadBits(6, &reserved)); | 168 RCHECK(bit_reader->ReadBits(6, &reserved)); |
169 RCHECK(bit_reader->ReadBits(9, &program_clock_reference_extension)); | 169 RCHECK(bit_reader->ReadBits(9, &program_clock_reference_extension)); |
170 } | 170 } |
171 | 171 |
172 if (opcr_flag) { | 172 if (opcr_flag) { |
173 int64 original_program_clock_reference_base; | 173 int64_t original_program_clock_reference_base; |
174 int reserved; | 174 int reserved; |
175 int original_program_clock_reference_extension; | 175 int original_program_clock_reference_extension; |
176 RCHECK(bit_reader->ReadBits(33, &original_program_clock_reference_base)); | 176 RCHECK(bit_reader->ReadBits(33, &original_program_clock_reference_base)); |
177 RCHECK(bit_reader->ReadBits(6, &reserved)); | 177 RCHECK(bit_reader->ReadBits(6, &reserved)); |
178 RCHECK( | 178 RCHECK( |
179 bit_reader->ReadBits(9, &original_program_clock_reference_extension)); | 179 bit_reader->ReadBits(9, &original_program_clock_reference_extension)); |
180 } | 180 } |
181 | 181 |
182 if (splicing_point_flag) { | 182 if (splicing_point_flag) { |
183 int splice_countdown; | 183 int splice_countdown; |
(...skipping 27 matching lines...) Expand all Loading... |
211 << std::hex << stuffing_byte; | 211 << std::hex << stuffing_byte; |
212 } | 212 } |
213 | 213 |
214 DVLOG(LOG_LEVEL_TS) << "random_access_indicator=" << random_access_indicator_; | 214 DVLOG(LOG_LEVEL_TS) << "random_access_indicator=" << random_access_indicator_; |
215 return true; | 215 return true; |
216 } | 216 } |
217 | 217 |
218 } // namespace mp2t | 218 } // namespace mp2t |
219 } // namespace media | 219 } // namespace media |
220 | 220 |
OLD | NEW |