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

Side by Side Diff: media/mpeg2/es_parser_h264.cc

Issue 23566013: Mpeg2 TS stream parser for media source. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments from patch set #3 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/mpeg2/es_parser_h264.h"
6
7 #include "base/basictypes.h"
8 #include "base/logging.h"
9 #include "media/base/bit_reader.h"
10 #include "media/base/buffers.h"
11 #include "media/base/stream_parser_buffer.h"
12 #include "media/base/video_decoder_config.h"
13 #include "media/base/video_frame.h"
14 #include "media/mpeg2/mpeg2ts_common.h"
15 #include "ui/gfx/rect.h"
16 #include "ui/gfx/size.h"
17
18 static const int kExtendedSar = 255;
19
20 static const int kTableSarWidth[14] = {
21 1, 1, 12, 10, 16, 40, 24, 20, 32, 80, 18, 15, 64, 160
22 };
23
24 static const int kTableSarHeight[14] = {
25 1, 1, 11, 11, 11, 33, 11, 11, 11, 33, 11, 11, 33, 99
26 };
27
28 namespace media {
29 namespace mpeg2ts {
30
31 class BitReaderH264 : public BitReader {
32 public:
33 BitReaderH264(const uint8* data, off_t size)
34 : BitReader(data, size) { }
35
36 // Read an unsigned exp-golomb value.
37 // Return true if successful.
38 bool ReadBitsExpGolomb(uint32* exp_golomb_value);
39 };
40
41 bool BitReaderH264::ReadBitsExpGolomb(uint32* exp_golomb_value) {
42 // Get the number of leading zeros.
43 int zero_count = 0;
44 while (true) {
45 int one_bit;
46 RCHECK(ReadBits(1, &one_bit));
47 if (one_bit != 0)
48 break;
49 zero_count++;
50 }
51
52 // Read the actual value.
53 uint32 base_value = (1 << zero_count) - 1;
54 uint32 value = 0;
55 for (int bit_count = 0; bit_count < zero_count; bit_count++) {
56 int one_bit;
57 RCHECK(ReadBits(1, &one_bit));
damienv1 2013/09/10 15:28:25 Would be more efficient to read |zero_count| bits
damienv1 2013/09/10 21:03:48 Done.
58 if (one_bit != 0)
59 value += (1 << (zero_count - 1 - bit_count));
60 }
61
62 // If zero_count, the value that was calculated is undefined.
63 // We read the full exp-golomb field but returns an error.
64 if (zero_count > 31)
65 return false;
66
67 *exp_golomb_value = base_value + value;
68 return true;
69 }
70
71 EsParserH264::EsParserH264(
72 NewVideoConfigCB new_video_config_cb,
73 EmitBufferCB emit_buffer_cb)
74 : nal_es_pos_(0),
75 new_video_config_cb_(new_video_config_cb),
76 emit_buffer_cb_(emit_buffer_cb),
77 is_video_config_known_(false),
78 profile_idc_(0),
79 level_idc_(0),
80 pic_width_in_mbs_minus1_(0),
81 pic_height_in_map_units_minus1_(0) {
82 }
83
84 EsParserH264::~EsParserH264() {
85 }
86
87 bool EsParserH264::Parse(const uint8* buf, int size,
88 base::TimeDelta pts,
89 base::TimeDelta dts) {
90 // Note: Parse is invoked each time a PES packet has been reassembled.
91 // Unfortunately, a PES packet does not necessarily map
92 // to an h264 access unit, although the HLS recommandation is to use one PES
93 // for each access unit (but this is just a recommandation and some streams
94 // do not comply with this recommandation).
95
96 // Link position |raw_es_size| in the ES stream with a timing descriptor.
97 // HLS recommandation: "In AVC video, you should have both a DTS and a
98 // PTS in each PES header".
99 if (dts == kNoTimestamp() && pts == kNoTimestamp()) {
100 DVLOG(1) << "A timestamp must be provided for each reassembled PES";
101 Reset();
102 return false;
103 }
104 TimingDesc timing_desc;
105 timing_desc.pts = pts;
106 timing_desc.dts = (dts != kNoTimestamp()) ? dts : pts;
107
108 int raw_es_size;
109 const uint8* raw_es;
110 es_byte_queue_.Peek(&raw_es, &raw_es_size);
111 timing_desc_list_.push_back(
112 std::pair<int, TimingDesc>(raw_es_size, timing_desc));
113
114 // Add the incoming bytes to the ES queue.
115 es_byte_queue_.Push(buf, size);
116 es_byte_queue_.Peek(&raw_es, &raw_es_size);
117
118 // Add NALs from the incoming buffer.
119 FindNals();
120
121 // Find access units based on AUD.
122 std::list<NalDescList::const_iterator> access_unit_list;
123 FindAccessUnits(&access_unit_list);
124 if (access_unit_list.empty()) {
125 DiscardEs(raw_es_size - 4);
damienv1 2013/09/10 15:28:25 Equivalent, but strictly should be |nal_es_pos_| a
damienv1 2013/09/10 21:03:48 Done.
126 return true;
127 }
128
129 // Emit all frames.
130 std::list<NalDescList::const_iterator>::iterator it0 =
131 access_unit_list.begin();
132 std::list<NalDescList::const_iterator>::iterator it1 = it0;
133 ++it1;
134 LOG_IF(WARNING, (*it0)->position != 0)
135 << "Needs to discard some ES data before getting the 1st access unit: "
136 << (*it0)->position;
137 for (; it1 != access_unit_list.end(); ++it0, ++it1) {
138 int nxt_frame_position = (*it1)->position;
139 bool status = EmitFrame(*it0, *it1, nxt_frame_position);
140 if (!status) {
141 Reset();
142 return false;
143 }
144 }
145
146 // Discard emitted frames.
147 int last_position = access_unit_list.back()->position;
148 DiscardEs(last_position);
149
150 return true;
151 }
152
153 void EsParserH264::Flush() {
154 // Find access units based on AUD.
155 std::list<NalDescList::const_iterator> access_unit_list;
156 FindAccessUnits(&access_unit_list);
157
158 // At this point, there can be at most one access unit in the buffer.
159 DCHECK_GE(access_unit_list.size(), 1u);
160 if (!access_unit_list.empty()) {
161 // Force emitting the last access unit (even if it might be incomplete).
162 int nxt_frame_position = 0;
163 const uint8* raw_es = NULL;
164 es_byte_queue_.Peek(&raw_es, &nxt_frame_position);
165 NalDescList::const_iterator cur_frame = *(access_unit_list.begin());
166 NalDescList::const_iterator nxt_frame = nal_desc_list_.end();
167 EmitFrame(cur_frame, nxt_frame, nxt_frame_position);
168 }
169 }
170
171 void EsParserH264::Reset() {
172 DVLOG(1) << "EsParserH264::Reset";
173 es_byte_queue_.Reset();
174 timing_desc_list_.clear();
175 nal_desc_list_.clear();
176 nal_es_pos_ = 0;
177 is_video_config_known_ = false;
178 }
179
180 void EsParserH264::FindNals() {
181 int raw_es_size;
182 const uint8* raw_es;
183 es_byte_queue_.Peek(&raw_es, &raw_es_size);
184
185 DCHECK_GE(nal_es_pos_, 0);
186 DCHECK_LT(nal_es_pos_, raw_es_size);
187
188 // Resume NAL segmentation where it was left.
189 for ( ; nal_es_pos_ < raw_es_size - 4; nal_es_pos_++) {
190 // Make sure the syncword is either 00 00 00 01 or 00 00 01
191 if (raw_es[nal_es_pos_ + 0] != 0 ||
192 raw_es[nal_es_pos_ + 1] != 0) {
193 continue;
194 }
195 int syncword_length = 0;
196 if (raw_es[nal_es_pos_ + 2] == 0 &&
197 raw_es[nal_es_pos_ + 3] == 1) {
198 syncword_length = 4;
199 } else if (raw_es[nal_es_pos_ + 2] == 1) {
200 syncword_length = 3;
201 } else {
202 continue;
203 }
204
205 // Retrieve the NAL type.
206 int nal_header = raw_es[nal_es_pos_ + syncword_length];
207 int forbidden_zero_bit = (nal_header >> 7) & 0x1;
208 NalDesc nal_desc;
209 nal_desc.position = nal_es_pos_;
210 nal_desc.nal_unit_type = static_cast<NalUnitType>(nal_header & 0x1f);
211 if (forbidden_zero_bit != 0)
212 nal_desc.nal_unit_type = kNalUnitTypeInvalid;
213 DVLOG(LOG_LEVEL_ES) << "nal: offset=" << nal_desc.position
214 << " type=" << nal_desc.nal_unit_type;
215 nal_desc_list_.push_back(nal_desc);
216 nal_es_pos_ += syncword_length;
217 }
218 }
219
220 void EsParserH264::FindAccessUnits(
221 std::list<NalDescList::const_iterator>* access_unit_list) {
222 // Get the H264 access units based on AUD.
223 // Mpeg2TS spec: "2.14 Carriage of Rec. ITU-T H.264 | ISO/IEC 14496-10 video"
224 // "Each AVC access unit shall contain an access unit delimiter NAL Unit;"
225 for (NalDescList::const_iterator it = nal_desc_list_.begin();
226 it != nal_desc_list_.end(); ++it) {
227 if (it->nal_unit_type == kNalUnitTypeAUD) {
228 DVLOG(LOG_LEVEL_ES) << "aud found @ pos=" << it->position;
229 access_unit_list->push_back(it);
230 }
231 }
232 }
233
234 bool EsParserH264::EmitFrame(
235 NalDescList::const_iterator& cur_frame,
236 NalDescList::const_iterator& nxt_frame,
237 int nxt_frame_position) {
238 int raw_es_size;
239 const uint8* raw_es;
240 es_byte_queue_.Peek(&raw_es, &raw_es_size);
241
242 // Current frame position = position of the 1st NAL of the frame.
243 int cur_frame_position = cur_frame->position;
244 int access_unit_size = nxt_frame_position - cur_frame_position;
245
246 // Get the access unit timing info.
247 TimingDesc current_timing_desc;
248 while (!timing_desc_list_.empty() &&
249 timing_desc_list_.front().first <= cur_frame_position) {
250 current_timing_desc = timing_desc_list_.front().second;
251 timing_desc_list_.pop_front();
252 }
253
254 // Check whether this is a key frame + light NAL parsing to get some
255 // relevant information (e.g. SPS/PPS).
256 // Note: it would have been nice to get the keyframe decision based
257 // on the Mpeg2TS random_access_indicator but encoders sometimes just don't
258 // bother setting this flag in the MPEG2 TS stream.
259 bool is_key_frame = true;
260 for (NalDescList::const_iterator it = cur_frame; it != nxt_frame; ++it) {
261 if (it->nal_unit_type == kNalUnitTypeNonIdrSlice)
262 is_key_frame = false;
263 NalDescList::const_iterator next_nal_it = it;
264 ++next_nal_it;
265 int cur_nal_position = it->position;
266 int nxt_nal_position = (next_nal_it == nxt_frame)
267 ? nxt_frame_position : next_nal_it->position;
268 int nal_size = nxt_nal_position - cur_nal_position;
269 DCHECK_LE(cur_nal_position + nal_size, raw_es_size);
270 bool nal_status = NalParser(&raw_es[cur_nal_position], nal_size);
271 if (!nal_status)
272 return false;
273 }
274
275 // Emit the current frame.
276 DVLOG(LOG_LEVEL_ES) << "is_key_frame = " << is_key_frame;
277 scoped_refptr<StreamParserBuffer> stream_parser_buffer =
278 StreamParserBuffer::CopyFrom(
279 &raw_es[cur_frame_position],
280 access_unit_size,
281 is_key_frame);
282 stream_parser_buffer->SetDecodeTimestamp(current_timing_desc.dts);
283 stream_parser_buffer->set_timestamp(current_timing_desc.pts);
284 emit_buffer_cb_.Run(stream_parser_buffer);
285
286 return true;
287 }
288
289 void EsParserH264::DiscardEs(int nbytes) {
290 if (nbytes <= 0)
291 return;
292
293 // Update the NAL list accordingly.
294 while (!nal_desc_list_.empty() &&
295 nal_desc_list_.front().position < nbytes) {
damienv1 2013/09/10 15:28:25 Remove brackets.
damienv1 2013/09/10 21:03:48 Done.
296 nal_desc_list_.pop_front();
297 }
298 for (NalDescList::iterator it = nal_desc_list_.begin();
299 it != nal_desc_list_.end(); ++it) {
300 DCHECK(it->position >= nbytes);
301 it->position -= nbytes;
302 }
303 nal_es_pos_ -= nbytes;
304 if (nal_es_pos_ < 0)
305 nal_es_pos_ = 0;
306
307 // Update the timing information accordingly.
308 std::list<std::pair<int, TimingDesc> >::iterator timing_it
309 = timing_desc_list_.begin();
310 for (; timing_it != timing_desc_list_.end(); ++timing_it)
311 timing_it->first -= nbytes;
312
313 // Discard |nbytes| of ES.
314 es_byte_queue_.Pop(nbytes);
315 }
316
317 bool EsParserH264::NalParser(const uint8* buf, int size) {
318 // Discard the annexB syncword.
319 if (size < 3 || buf[0] != 0 || buf[1] != 0 ||
320 !(buf[2] == 1 || (size >= 4 && buf[2] == 0 && buf[3] == 1))) {
321 DVLOG(1) << "NalParser: bad annexB start code";
322 return false;
323 }
324 if (buf[2] == 1) {
325 buf += 3;
326 size -= 3;
327 } else {
328 buf += 4;
329 size -= 4;
330 }
331
332 // Get the NAL header.
333 if (size < 1) {
334 LOG(WARNING) << "NalParser: incomplete NAL";
damienv1 2013/09/10 15:28:25 DVLOG(1)
damienv1 2013/09/10 21:03:48 Done.
335 return false;
336 }
337 int nal_header = buf[0];
338 buf += 1;
339 size -= 1;
340
341 int forbidden_zero_bit = (nal_header >> 7) & 0x1;
342 if (forbidden_zero_bit != 0)
343 return false;
344 int nal_ref_idc = (nal_header >> 5) & 0x3;
345 int nal_unit_type = nal_header & 0x1f;
346
347 // TODO(damienv):
348 // The nal start code emulation prevention should be un-done,
349 // before parsing the NAL content.
350
351 // Process the NAL content.
352 if (nal_unit_type == kNalUnitTypeSPS) {
353 DVLOG(LOG_LEVEL_ES) << "NAL: SPS";
354 // |nal_ref_idc| should not be 0 for a SPS.
355 if (nal_ref_idc == 0)
356 return false;
357 return ProcessSPS(buf, size);
358 }
359 if (nal_unit_type == kNalUnitTypeIdrSlice) {
360 DVLOG(LOG_LEVEL_ES) << "NAL: IDR slice";
361 return true;
362 }
363 if (nal_unit_type == kNalUnitTypeNonIdrSlice) {
364 DVLOG(LOG_LEVEL_ES) << "NAL: Non IDR slice";
365 return true;
366 }
367 if (nal_unit_type == kNalUnitTypePPS) {
368 DVLOG(LOG_LEVEL_ES) << "NAL: PPS";
369 return true;
370 }
371 if (nal_unit_type == kNalUnitTypeAUD) {
372 DVLOG(LOG_LEVEL_ES) << "NAL: AUD";
373 return true;
374 }
375
376 DVLOG(LOG_LEVEL_ES) << "NAL: " << nal_unit_type;
377 return true;
378 }
379
380 bool EsParserH264::ProcessSPS(const uint8* buf, int size) {
381 if (size <= 0)
382 return false;
383 BitReaderH264 bit_reader(buf, size);
384
385 int profile_idc;
386 int constraint_setX_flag;
387 int level_idc;
388 uint32 seq_parameter_set_id;
389 uint32 log2_max_frame_num_minus4;
390 uint32 pic_order_cnt_type;
391 RCHECK(bit_reader.ReadBits(8, &profile_idc));
392 RCHECK(bit_reader.ReadBits(8, &constraint_setX_flag));
393 RCHECK(bit_reader.ReadBits(8, &level_idc));
394 RCHECK(bit_reader.ReadBitsExpGolomb(&seq_parameter_set_id));
395 RCHECK(bit_reader.ReadBitsExpGolomb(&log2_max_frame_num_minus4));
396 RCHECK(bit_reader.ReadBitsExpGolomb(&pic_order_cnt_type));
397
398 if (pic_order_cnt_type > 2) {
399 // Bitstream error: pic_order_cnt_type shall be in the range of 0 to 2.
damienv1 2013/09/10 15:28:25 Move comment above and remove brackets.
damienv1 2013/09/10 21:03:48 Done.
400 return false;
401 }
402 if (pic_order_cnt_type == 0) {
403 uint32 log2_max_pic_order_cnt_lsb_minus4;
404 RCHECK(bit_reader.ReadBitsExpGolomb(&log2_max_pic_order_cnt_lsb_minus4));
405 } else if (pic_order_cnt_type == 1) {
406 NOTIMPLEMENTED();
407 return false;
408 }
409
410 uint32 num_ref_frames;
411 int gaps_in_frame_num_value_allowed_flag;
412 uint32 pic_width_in_mbs_minus1;
413 uint32 pic_height_in_map_units_minus1;
414 RCHECK(bit_reader.ReadBitsExpGolomb(&num_ref_frames));
415 RCHECK(bit_reader.ReadBits(1, &gaps_in_frame_num_value_allowed_flag));
416 RCHECK(bit_reader.ReadBitsExpGolomb(&pic_width_in_mbs_minus1));
417 RCHECK(bit_reader.ReadBitsExpGolomb(&pic_height_in_map_units_minus1));
418
419 int frame_mbs_only_flag;
420 RCHECK(bit_reader.ReadBits(1, &frame_mbs_only_flag));
421 if (!frame_mbs_only_flag) {
422 int mb_adaptive_frame_field_flag;
423 RCHECK(bit_reader.ReadBits(1, &mb_adaptive_frame_field_flag));
424 }
425
426 int direct_8x8_inference_flag;
427 RCHECK(bit_reader.ReadBits(1, &direct_8x8_inference_flag));
428
429 bool frame_cropping_flag;
430 uint32 frame_crop_left_offset = 0;
431 uint32 frame_crop_right_offset = 0;
432 uint32 frame_crop_top_offset = 0;
433 uint32 frame_crop_bottom_offset = 0;
434 RCHECK(bit_reader.ReadBits(1, &frame_cropping_flag));
435 if (frame_cropping_flag) {
436 RCHECK(bit_reader.ReadBitsExpGolomb(&frame_crop_left_offset));
437 RCHECK(bit_reader.ReadBitsExpGolomb(&frame_crop_right_offset));
438 RCHECK(bit_reader.ReadBitsExpGolomb(&frame_crop_top_offset));
439 RCHECK(bit_reader.ReadBitsExpGolomb(&frame_crop_bottom_offset));
440 }
441
442 bool vui_parameters_present_flag;
443 RCHECK(bit_reader.ReadBits(1, &vui_parameters_present_flag));
444 int sar_width = 1;
445 int sar_height = 1;
446 if (vui_parameters_present_flag) {
447 // Read only the aspect ratio information from the VUI section.
448 // TODO(damienv): check whether other VUI info are useful.
449 bool aspect_ratio_info_present_flag = false;
450 RCHECK(bit_reader.ReadBits(1, &aspect_ratio_info_present_flag));
451 if (aspect_ratio_info_present_flag) {
452 int aspect_ratio_idc;
453 RCHECK(bit_reader.ReadBits(8, &aspect_ratio_idc));
454 if (aspect_ratio_idc == kExtendedSar) {
455 RCHECK(bit_reader.ReadBits(16, &sar_width));
456 RCHECK(bit_reader.ReadBits(16, &sar_height));
457 } else if (aspect_ratio_idc < 14) {
458 sar_width = kTableSarWidth[aspect_ratio_idc];
459 sar_height = kTableSarHeight[aspect_ratio_idc];
460 }
461 }
462 }
463
464 if (sar_width != sar_height) {
465 // TODO(damienv): Support non square pixels.
466 DVLOG(1)
467 << "Non square pixel not supported yet:"
468 << " sar_width=" << sar_width
469 << " sar_height=" << sar_height;
470 return false;
471 }
472
473 if (is_video_config_known_ &&
474 profile_idc == profile_idc_ &&
475 level_idc == level_idc_ &&
476 pic_width_in_mbs_minus1 == pic_width_in_mbs_minus1_ &&
477 pic_height_in_map_units_minus1 == pic_height_in_map_units_minus1_) {
478 // This is the same SPS as the previous one.
479 return true;
480 }
481 is_video_config_known_ = true;
482 profile_idc_ = profile_idc;
483 level_idc_ = level_idc;
484 pic_width_in_mbs_minus1_ = pic_width_in_mbs_minus1;
485 pic_height_in_map_units_minus1_ = pic_height_in_map_units_minus1;
486
487 // TODO(damienv):
488 // Assuming the SPS is used right away by the PPS
489 // and the slice headers is a strong assumption.
490 // In theory, we should process the SPS and PPS
491 // and only when one of the slice header is switching
492 // the PPS id, the video decoder config should be changed.
493 DVLOG(1) << "Profile IDC: " << profile_idc;
494 DVLOG(1) << "Level IDC: " << level_idc;
495 DVLOG(1) << "Pic width: " << (pic_width_in_mbs_minus1 + 1) * 16;
496 DVLOG(1) << "Pic height: " << (pic_height_in_map_units_minus1 + 1) * 16;
497 DVLOG(1) << "log2_max_frame_num_minus4: " << log2_max_frame_num_minus4;
498
499 // TODO(damienv): a MAP unit can be either 16 or 32 pixels.
500 // although it's 16 pixels for progressive non MBAFF frames.
501 gfx::Size coded_size((pic_width_in_mbs_minus1 + 1) * 16,
502 (pic_height_in_map_units_minus1 + 1) * 16);
503 gfx::Rect visible_rect(
504 frame_crop_left_offset,
505 frame_crop_top_offset,
506 (coded_size.width() - frame_crop_right_offset) - frame_crop_left_offset,
507 (coded_size.height() - frame_crop_bottom_offset) - frame_crop_top_offset);
508
509 // TODO(damienv): calculate the natural size based
510 // on the possible aspect ratio coded in the VUI parameters.
511 gfx::Size natural_size(visible_rect.width(),
512 visible_rect.height());
513
514 VideoDecoderConfig video_decoder_config(
515 kCodecH264,
516 VIDEO_CODEC_PROFILE_UNKNOWN, // TODO(damienv)
517 VideoFrame::YV12,
518 coded_size,
519 visible_rect,
520 natural_size,
521 NULL, 0,
522 false);
523 new_video_config_cb_.Run(video_decoder_config);
524
525 return true;
526 }
527
528 } // namespace mpeg2ts
529 } // namespace media
530
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698