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:" | |
damienv1
2013/09/13 01:23:01
DVLOG
damienv1
2013/09/13 16:07:09
Done.
| |
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:" | |
damienv1
2013/09/13 01:23:01
DVLOG
damienv1
2013/09/13 16:07:09
Done.
| |
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 bool transport_error_indicator; | |
87 bool transport_priority; | |
88 int transport_scrambling_control; | |
89 int adaptation_field_control; | |
90 RCHECK(bit_reader->ReadBits(8, &syncword)); | |
91 RCHECK(bit_reader->ReadBits(1, &transport_error_indicator)); | |
92 RCHECK(bit_reader->ReadBits(1, &payload_unit_start_indicator_)); | |
93 RCHECK(bit_reader->ReadBits(1, &transport_priority)); | |
94 RCHECK(bit_reader->ReadBits(13, &pid_)); | |
95 RCHECK(bit_reader->ReadBits(2, &transport_scrambling_control)); | |
96 RCHECK(bit_reader->ReadBits(2, &adaptation_field_control)); | |
97 RCHECK(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 RCHECK(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. | |
damienv1
2013/09/13 01:23:01
Move comment before if and remove brackets.
damienv1
2013/09/13 16:07:09
Done.
| |
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 bool elementary_stream_priority_indicator; | |
144 bool pcr_flag; | |
145 bool opcr_flag; | |
146 bool splicing_point_flag; | |
147 bool transport_private_data_flag; | |
148 bool adaptation_field_extension_flag; | |
149 RCHECK(bit_reader->ReadBits(1, &discontinuity_indicator_)); | |
150 RCHECK(bit_reader->ReadBits(1, &random_access_indicator_)); | |
151 RCHECK(bit_reader->ReadBits(1, &elementary_stream_priority_indicator)); | |
152 RCHECK(bit_reader->ReadBits(1, &pcr_flag)); | |
153 RCHECK(bit_reader->ReadBits(1, &opcr_flag)); | |
154 RCHECK(bit_reader->ReadBits(1, &splicing_point_flag)); | |
155 RCHECK(bit_reader->ReadBits(1, &transport_private_data_flag)); | |
156 RCHECK(bit_reader->ReadBits(1, &adaptation_field_extension_flag)); | |
157 adaptation_field_length -= 1; | |
158 | |
159 if (pcr_flag) { | |
160 RCHECK(adaptation_field_length >= 6); | |
161 int64 program_clock_reference_base; | |
162 int reserved; | |
163 int program_clock_reference_extension; | |
164 RCHECK(bit_reader->ReadBits(33, &program_clock_reference_base)); | |
165 RCHECK(bit_reader->ReadBits(6, &reserved)); | |
166 RCHECK(bit_reader->ReadBits(9, &program_clock_reference_extension)); | |
167 adaptation_field_length -= 6; | |
168 } | |
169 | |
170 if (opcr_flag) { | |
171 RCHECK(adaptation_field_length >= 6); | |
172 int64 original_program_clock_reference_base; | |
173 int reserved; | |
174 int original_program_clock_reference_extension; | |
175 RCHECK(bit_reader->ReadBits(33, &original_program_clock_reference_base)); | |
176 RCHECK(bit_reader->ReadBits(6, &reserved)); | |
177 RCHECK( | |
178 bit_reader->ReadBits(9, &original_program_clock_reference_extension)); | |
179 adaptation_field_length -= 6; | |
180 } | |
181 | |
182 if (splicing_point_flag) { | |
183 RCHECK(adaptation_field_length >= 1); | |
184 int splice_countdown; | |
185 RCHECK(bit_reader->ReadBits(8, &splice_countdown)); | |
186 adaptation_field_length -= 1; | |
187 } | |
188 | |
189 if (transport_private_data_flag) { | |
190 RCHECK(adaptation_field_length >= 1); | |
191 int transport_private_data_length; | |
192 RCHECK(bit_reader->ReadBits(8, &transport_private_data_length)); | |
193 adaptation_field_length -= 1; | |
194 | |
195 RCHECK(adaptation_field_length >= transport_private_data_length); | |
196 for (int k = 0; k < transport_private_data_length; k++) { | |
197 int private_data_byte; | |
198 RCHECK(bit_reader->ReadBits(8, &private_data_byte)); | |
199 } | |
200 adaptation_field_length -= transport_private_data_length; | |
201 } | |
202 | |
203 if (adaptation_field_extension_flag) { | |
204 RCHECK(adaptation_field_length >= 1); | |
205 int adaptation_field_extension_length; | |
206 RCHECK(bit_reader->ReadBits(8, &adaptation_field_extension_length)); | |
207 adaptation_field_length -= 1; | |
208 | |
209 RCHECK(adaptation_field_length >= adaptation_field_extension_length); | |
210 for (int k = 0; k < adaptation_field_extension_length; k++) { | |
211 int dummy_byte; | |
212 RCHECK(bit_reader->ReadBits(8, &dummy_byte)); | |
213 } | |
214 adaptation_field_length -= adaptation_field_extension_length; | |
215 } | |
216 | |
217 for (int k = 0; k < adaptation_field_length; k++) { | |
218 int stuffing_byte; | |
219 RCHECK(bit_reader->ReadBits(8, &stuffing_byte)); | |
220 if (stuffing_byte != 0xff) { | |
221 LOG(WARNING) << "Invalid stuffing byte = " << stuffing_byte; | |
222 return false; | |
223 } | |
damienv1
2013/09/13 01:23:01
RCHECK(stuffing_byte == 0xff);
damienv1
2013/09/13 16:07:09
Done.
| |
224 } | |
225 | |
226 DVLOG(LOG_LEVEL_TS) << "random_access_indicator=" << random_access_indicator_; | |
227 return true; | |
228 } | |
229 | |
230 const uint8* TsPacket::GetPayload() const { | |
231 return payload_; | |
232 } | |
233 | |
234 int TsPacket::GetPayloadOffset() const { | |
235 return (kPacketSize - payload_size_); | |
236 } | |
237 | |
238 int TsPacket::GetPayloadSize() const { | |
239 return payload_size_; | |
240 } | |
241 | |
242 } // namespace mp2t | |
243 } // namespace media | |
244 | |
OLD | NEW |