Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(162)

Side by Side Diff: media/mp2t/ts_section_pes.cc

Issue 23566013: Mpeg2 TS stream parser for media source. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Improve buffer emission + Cleanup Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_section_pes.h"
6
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "media/base/bit_reader.h"
10 #include "media/base/buffers.h"
11 #include "media/mp2t/es_parser.h"
12 #include "media/mp2t/mp2t_common.h"
13
14 static const int kPesStartCode = 0x000001;
15
16 // Given that |time| is coded using 33 bits,
17 // UnrollTimestamp returns the corresponding unrolled timestamp.
18 // The unrolled timestamp is defined by:
19 // |time| + k * (2 ^ 33)
20 // where k is estimated so that the unrolled timestamp
21 // is as close as possible to |previous_unrolled_time|.
22 static int64 UnrollTimestamp(int64 previous_unrolled_time, int64 time) {
23 // Mpeg2 TS timestamps have an accuracy of 33 bits.
24 const int nbits = 33;
25
26 // |timestamp| has a precision of |nbits|
27 // so make sure the highest bits are set to 0.
28 DCHECK_EQ((time >> nbits), 0);
29
30 // Consider 3 possibilities to estimate the missing high bits of |time|.
31 int64 previous_unrolled_time_high =
32 (previous_unrolled_time >> nbits);
33 int64 time0 = ((previous_unrolled_time_high - 1) << nbits) | time;
34 int64 time1 = ((previous_unrolled_time_high + 0) << nbits) | time;
35 int64 time2 = ((previous_unrolled_time_high + 1) << nbits) | time;
36
37 // Select the min absolute difference with the current time
38 // so as to ensure time continuity.
39 int64 diff0 = time0 - previous_unrolled_time;
40 int64 diff1 = time1 - previous_unrolled_time;
41 int64 diff2 = time2 - previous_unrolled_time;
42 if (diff0 < 0)
43 diff0 = -diff0;
44 if (diff1 < 0)
45 diff1 = -diff1;
46 if (diff2 < 0)
47 diff2 = -diff2;
48
49 int64 unrolled_time;
50 int64 min_diff;
51 if (diff1 < diff0) {
52 unrolled_time = time1;
53 min_diff = diff1;
54 } else {
55 unrolled_time = time0;
56 min_diff = diff0;
57 }
58 if (diff2 < min_diff)
59 unrolled_time = time2;
60
61 return unrolled_time;
62 }
63
64 static bool IsTimestampSectionValid(int64 timestamp_section) {
65 // |pts_section| has 40 bits:
66 // - starting with either '0010' or '0011' or '0001'
67 // - and ending with a marker bit.
68 // See ITU H.222 standard - PES section.
69
70 // Verify that all the marker bits are set to one.
71 return ((timestamp_section & 0x1) != 0) &&
72 ((timestamp_section & 0x10000) != 0) &&
73 ((timestamp_section & 0x100000000) != 0);
74 }
75
76 static int64 ConvertTimestampSectionToTimestamp(int64 timestamp_section) {
77 return (((timestamp_section >> 33) & 0x7) << 30) |
78 (((timestamp_section >> 17) & 0x7fff) << 15) |
79 (((timestamp_section >> 1) & 0x7fff) << 0);
80 }
81
82 namespace media {
83 namespace mp2t {
84
85 TsSectionPes::TsSectionPes(scoped_ptr<EsParser> es_parser)
86 : es_parser_(es_parser.release()),
87 wait_for_pusi_(true),
88 previous_pts_valid_(false),
89 previous_pts_(0),
90 previous_dts_valid_(false),
91 previous_dts_(0) {
92 DCHECK(es_parser_);
93 }
94
95 TsSectionPes::~TsSectionPes() {
96 }
97
98 bool TsSectionPes::Parse(bool payload_unit_start_indicator,
99 const uint8* buf, int size) {
100 // Ignore partial PES.
101 if (wait_for_pusi_ && !payload_unit_start_indicator)
102 return true;
103
104 bool parse_result = true;
105 if (payload_unit_start_indicator) {
106 // Try emitting a packet since we might have a pending PES packet
107 // with an undefined size.
108 // In this case, a unit is emitted when the next unit is coming.
109 int raw_pes_size;
110 const uint8* raw_pes;
111 pes_byte_queue_.Peek(&raw_pes, &raw_pes_size);
112 if (raw_pes_size > 0)
113 parse_result = Emit(true);
114
115 // Reset the state.
116 ResetPesState();
117
118 // Update the state.
119 wait_for_pusi_ = false;
120 }
121
122 // Add the data to the parser state.
123 if (size > 0)
124 pes_byte_queue_.Push(buf, size);
125
126 // Try emitting the current PES packet.
127 parse_result = parse_result && Emit(false);
acolwell GONE FROM CHROMIUM 2013/09/18 01:46:05 nit: just return here since you don't use the upda
damienv1 2013/09/18 21:40:17 Done.
128
129 return parse_result;
130 }
131
132 void TsSectionPes::Flush() {
133 // Try emitting a packet since we might have a pending PES packet
134 // with an undefined size.
135 Emit(true);
136
137 // Flush the underlying ES parser.
138 es_parser_->Flush();
139 }
140
141 void TsSectionPes::Reset() {
142 ResetPesState();
143
144 previous_pts_valid_ = false;
145 previous_pts_ = 0;
146 previous_dts_valid_ = false;
147 previous_dts_ = 0;
148
149 es_parser_->Reset();
150 }
151
152 bool TsSectionPes::Emit(bool emit_for_unknown_size) {
153 int raw_pes_size;
154 const uint8* raw_pes;
155 pes_byte_queue_.Peek(&raw_pes, &raw_pes_size);
156
157 // A PES should be at least 6 bytes.
158 // Wait for more data to come if not enough bytes.
159 if (raw_pes_size < 6)
160 return true;
161
162 // Check whether we have enough data to start parsing.
163 int pes_packet_length =
164 (static_cast<int>(raw_pes[4]) << 8) |
165 (static_cast<int>(raw_pes[5]));
166 if ((pes_packet_length == 0 && !emit_for_unknown_size) ||
167 (pes_packet_length != 0 && raw_pes_size < pes_packet_length + 6)) {
168 // Wait for more data to come either because:
169 // - there are not enough bytes,
170 // - or the PES size is unknown and the "force emit" flag is not set.
171 // (PES size might be unknown for video PES packet).
172 return true;
173 }
174 DVLOG(LOG_LEVEL_PES) << "pes_packet_length=" << pes_packet_length;
175
176 // Parse the packet.
177 bool parse_result = ParseInternal(raw_pes, raw_pes_size);
178
179 // Reset the state.
180 ResetPesState();
181
182 return parse_result;
183 }
184
185 bool TsSectionPes::ParseInternal(const uint8* raw_pes, int raw_pes_size) {
186 BitReader bit_reader(raw_pes, raw_pes_size);
187
188 // Read up to the pes_packet_length (6 bytes).
189 int packet_start_code_prefix;
190 int stream_id;
191 int pes_packet_length;
192 RCHECK(bit_reader.ReadBits(24, &packet_start_code_prefix));
193 RCHECK(bit_reader.ReadBits(8, &stream_id));
194 RCHECK(bit_reader.ReadBits(16, &pes_packet_length));
195
196 RCHECK(packet_start_code_prefix == kPesStartCode);
197 DVLOG(LOG_LEVEL_PES) << "stream_id=" << std::hex << stream_id << std::dec;
198 if (pes_packet_length == 0)
199 pes_packet_length = bit_reader.bits_available() / 8;
200
201 // Ignore the PES for unknown stream IDs.
202 // See ITU H.222 Table 2-22 "Stream_id assignments"
203 bool is_audio_stream_id = ((stream_id & 0xe0) == 0xc0);
204 bool is_video_stream_id = ((stream_id & 0xf0) == 0xe0);
205 if (!is_audio_stream_id && !is_video_stream_id)
206 return true;
207
208 // Read up to "pes_header_data_length".
209 int dummy_2;
210 int PES_scrambling_control;
211 bool PES_priority;
212 bool data_alignment_indicator;
213 bool copyright;
214 bool original_or_copy;
215 int pts_dts_flags;
216 bool escr_flag;
217 bool es_rate_flag;
218 bool dsm_trick_mode_flag;
219 bool additional_copy_info_flag;
220 bool pes_crc_flag;
221 bool pes_extension_flag;
222 int pes_header_data_length;
223 RCHECK(bit_reader.ReadBits(2, &dummy_2));
224 RCHECK(dummy_2 == 0x2);
225 RCHECK(bit_reader.ReadBits(2, &PES_scrambling_control));
226 RCHECK(bit_reader.ReadBits(1, &PES_priority));
227 RCHECK(bit_reader.ReadBits(1, &data_alignment_indicator));
228 RCHECK(bit_reader.ReadBits(1, &copyright));
229 RCHECK(bit_reader.ReadBits(1, &original_or_copy));
230 RCHECK(bit_reader.ReadBits(2, &pts_dts_flags));
231 RCHECK(bit_reader.ReadBits(1, &escr_flag));
232 RCHECK(bit_reader.ReadBits(1, &es_rate_flag));
233 RCHECK(bit_reader.ReadBits(1, &dsm_trick_mode_flag));
234 RCHECK(bit_reader.ReadBits(1, &additional_copy_info_flag));
235 RCHECK(bit_reader.ReadBits(1, &pes_crc_flag));
236 RCHECK(bit_reader.ReadBits(1, &pes_extension_flag));
237 RCHECK(bit_reader.ReadBits(8, &pes_header_data_length));
238 int pes_header_start_size = bit_reader.bits_available() / 8;
239
240 // Compute the size and the offset of the ES payload.
241 // "6" for the 6 bytes read before and including |pes_packet_length|.
242 // "3" for the 3 bytes read before and including |pes_header_data_length|.
243 int es_size = pes_packet_length - 3 - pes_header_data_length;
244 int es_offset = 6 + 3 + pes_header_data_length;
245 RCHECK(es_size >= 0);
246 RCHECK(es_offset + es_size <= raw_pes_size);
247
248 // Read the timing information section.
249 bool is_pts_valid = false;
250 bool is_dts_valid = false;
251 int64 pts_section = 0;
252 int64 dts_section = 0;
253 if (pts_dts_flags == 0x2) {
254 RCHECK(bit_reader.ReadBits(40, &pts_section));
255 RCHECK((((pts_section >> 36) & 0xf) == 0x2) &&
256 IsTimestampSectionValid(pts_section));
257 is_pts_valid = true;
258 }
259 if (pts_dts_flags == 0x3) {
260 RCHECK(bit_reader.ReadBits(40, &pts_section));
261 RCHECK(bit_reader.ReadBits(40, &dts_section));
262 RCHECK((((pts_section >> 36) & 0xf) == 0x3) &&
263 IsTimestampSectionValid(pts_section));
264 RCHECK((((dts_section >> 36) & 0xf) == 0x1) &&
265 IsTimestampSectionValid(dts_section));
266 is_pts_valid = true;
267 is_dts_valid = true;
268 }
269
270 // Convert and unroll the timestamps.
271 base::TimeDelta media_pts(kNoTimestamp());
272 base::TimeDelta media_dts(kNoTimestamp());
273 if (is_pts_valid) {
274 int64 pts = ConvertTimestampSectionToTimestamp(pts_section);
275 if (previous_pts_valid_)
276 pts = UnrollTimestamp(previous_pts_, pts);
277 previous_pts_ = pts;
278 previous_pts_valid_ = true;
279 media_pts = base::TimeDelta::FromMicroseconds((1000 * pts) / 90);
280 }
281 if (is_dts_valid) {
282 int64 dts = ConvertTimestampSectionToTimestamp(dts_section);
283 if (previous_dts_valid_)
284 dts = UnrollTimestamp(previous_dts_, dts);
285 previous_dts_ = dts;
286 previous_dts_valid_ = true;
287 media_dts = base::TimeDelta::FromMicroseconds((1000 * dts) / 90);
288 }
289
290 // Discard the rest of the PES packet header.
291 // TODO(damienv): check if some info of the PES packet header are useful.
292 DCHECK_EQ(bit_reader.bits_available() % 8, 0);
293 int pes_header_remaining_size = pes_header_data_length -
294 (pes_header_start_size - bit_reader.bits_available() / 8);
295 RCHECK(pes_header_remaining_size >= 0);
296
297 // Read the PES packet.
298 DVLOG(LOG_LEVEL_PES)
299 << "Emit a reassembled PES:"
300 << " size=" << es_size
301 << " pts=" << media_pts.InMilliseconds()
302 << " dts=" << media_dts.InMilliseconds()
303 << " data_alignment_indicator=" << data_alignment_indicator;
304 return es_parser_->Parse(&raw_pes[es_offset], es_size, media_pts, media_dts);
305 }
306
307 void TsSectionPes::ResetPesState() {
308 pes_byte_queue_.Reset();
309 wait_for_pusi_ = true;
310 }
311
312 } // namespace mp2t
313 } // namespace media
314
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698