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

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

Powered by Google App Engine
This is Rietveld 408576698