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