h264_bitstream_parser.cc revision 8c266e6baff043a1fa5c9134f46042908a376d5b
1/* 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10#include "webrtc/modules/rtp_rtcp/source/h264_bitstream_parser.h" 11 12#include <vector> 13 14#include "webrtc/base/bitbuffer.h" 15#include "webrtc/base/bytebuffer.h" 16#include "webrtc/base/checks.h" 17#include "webrtc/base/logging.h" 18#include "webrtc/base/scoped_ptr.h" 19 20namespace webrtc { 21namespace { 22// The size of a NALU header {0 0 0 1}. 23static const size_t kNaluHeaderSize = 4; 24 25// The size of a NALU header plus the type byte. 26static const size_t kNaluHeaderAndTypeSize = kNaluHeaderSize + 1; 27 28// The NALU type. 29static const uint8_t kNaluSps = 0x7; 30static const uint8_t kNaluPps = 0x8; 31static const uint8_t kNaluIdr = 0x5; 32static const uint8_t kNaluTypeMask = 0x1F; 33 34static const uint8_t kSliceTypeP = 0x0; 35static const uint8_t kSliceTypeB = 0x1; 36static const uint8_t kSliceTypeSp = 0x3; 37 38// Returns a vector of the NALU start sequences (0 0 0 1) in the given buffer. 39std::vector<size_t> FindNaluStartSequences(const uint8_t* buffer, 40 size_t buffer_size) { 41 std::vector<size_t> sequences; 42 // This is sorta like Boyer-Moore, but with only the first optimization step: 43 // given a 4-byte sequence we're looking at, if the 4th byte isn't 1 or 0, 44 // skip ahead to the next 4-byte sequence. 0s and 1s are relatively rare, so 45 // this will skip the majority of reads/checks. 46 const uint8_t* end = buffer + buffer_size - 4; 47 for (const uint8_t* head = buffer; head < end;) { 48 if (head[3] > 1) { 49 head += 4; 50 } else if (head[3] == 1 && head[2] == 0 && head[1] == 0 && head[0] == 0) { 51 sequences.push_back(static_cast<size_t>(head - buffer)); 52 head += 4; 53 } else { 54 head++; 55 } 56 } 57 58 return sequences; 59} 60} // namespace 61 62// Parses RBSP from source bytes. Removes emulation bytes, but leaves the 63// rbsp_trailing_bits() in the stream, since none of the parsing reads all the 64// way to the end of a parsed RBSP sequence. When writing, that means the 65// rbsp_trailing_bits() should be preserved and don't need to be restored (i.e. 66// the rbsp_stop_one_bit, which is just a 1, then zero padded), and alignment 67// should "just work". 68// TODO(pbos): Make parsing RBSP something that can be integrated into BitBuffer 69// so we don't have to copy the entire frames when only interested in the 70// headers. 71rtc::ByteBuffer* ParseRbsp(const uint8_t* bytes, size_t length) { 72 // Copied from webrtc::H264SpsParser::Parse. 73 rtc::ByteBuffer* rbsp_buffer = new rtc::ByteBuffer; 74 for (size_t i = 0; i < length;) { 75 if (length - i >= 3 && bytes[i] == 0 && bytes[i + 1] == 0 && 76 bytes[i + 2] == 3) { 77 rbsp_buffer->WriteBytes(reinterpret_cast<const char*>(bytes) + i, 2); 78 i += 3; 79 } else { 80 rbsp_buffer->WriteBytes(reinterpret_cast<const char*>(bytes) + i, 1); 81 i++; 82 } 83 } 84 return rbsp_buffer; 85} 86 87#define RETURN_FALSE_ON_FAIL(x) \ 88 if (!(x)) { \ 89 LOG_F(LS_ERROR) << "FAILED: " #x; \ 90 return false; \ 91 } 92 93H264BitstreamParser::PpsState::PpsState() { 94} 95 96H264BitstreamParser::SpsState::SpsState() { 97} 98 99// These functions are similar to webrtc::H264SpsParser::Parse, and based on the 100// same version of the H.264 standard. You can find it here: 101// http://www.itu.int/rec/T-REC-H.264 102bool H264BitstreamParser::ParseSpsNalu(const uint8_t* sps, size_t length) { 103 // Reset SPS state. 104 sps_ = SpsState(); 105 sps_parsed_ = false; 106 // Parse out the SPS RBSP. It should be small, so it's ok that we create a 107 // copy. We'll eventually write this back. 108 rtc::scoped_ptr<rtc::ByteBuffer> sps_rbsp( 109 ParseRbsp(sps + kNaluHeaderAndTypeSize, length - kNaluHeaderAndTypeSize)); 110 rtc::BitBuffer sps_parser(reinterpret_cast<const uint8*>(sps_rbsp->Data()), 111 sps_rbsp->Length()); 112 113 uint8_t byte_tmp; 114 uint32_t golomb_tmp; 115 uint32_t bits_tmp; 116 117 // profile_idc: u(8). 118 uint8 profile_idc; 119 RETURN_FALSE_ON_FAIL(sps_parser.ReadUInt8(&profile_idc)); 120 // constraint_set0_flag through constraint_set5_flag + reserved_zero_2bits 121 // 1 bit each for the flags + 2 bits = 8 bits = 1 byte. 122 RETURN_FALSE_ON_FAIL(sps_parser.ReadUInt8(&byte_tmp)); 123 // level_idc: u(8) 124 RETURN_FALSE_ON_FAIL(sps_parser.ReadUInt8(&byte_tmp)); 125 // seq_parameter_set_id: ue(v) 126 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); 127 sps_.separate_colour_plane_flag = 0; 128 // See if profile_idc has chroma format information. 129 if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 || 130 profile_idc == 244 || profile_idc == 44 || profile_idc == 83 || 131 profile_idc == 86 || profile_idc == 118 || profile_idc == 128 || 132 profile_idc == 138 || profile_idc == 139 || profile_idc == 134) { 133 // chroma_format_idc: ue(v) 134 uint32 chroma_format_idc; 135 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&chroma_format_idc)); 136 if (chroma_format_idc == 3) { 137 // separate_colour_plane_flag: u(1) 138 RETURN_FALSE_ON_FAIL( 139 sps_parser.ReadBits(&sps_.separate_colour_plane_flag, 1)); 140 } 141 // bit_depth_luma_minus8: ue(v) 142 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); 143 // bit_depth_chroma_minus8: ue(v) 144 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); 145 // qpprime_y_zero_transform_bypass_flag: u(1) 146 RETURN_FALSE_ON_FAIL(sps_parser.ReadBits(&bits_tmp, 1)); 147 // seq_scaling_matrix_present_flag: u(1) 148 uint32_t seq_scaling_matrix_present_flag; 149 RETURN_FALSE_ON_FAIL( 150 sps_parser.ReadBits(&seq_scaling_matrix_present_flag, 1)); 151 if (seq_scaling_matrix_present_flag) { 152 // seq_scaling_list_present_flags. Either 8 or 12, depending on 153 // chroma_format_idc. 154 uint32_t seq_scaling_list_present_flags; 155 if (chroma_format_idc != 3) { 156 RETURN_FALSE_ON_FAIL( 157 sps_parser.ReadBits(&seq_scaling_list_present_flags, 8)); 158 } else { 159 RETURN_FALSE_ON_FAIL( 160 sps_parser.ReadBits(&seq_scaling_list_present_flags, 12)); 161 } 162 // TODO(pbos): Support parsing scaling lists if they're seen in practice. 163 RTC_CHECK(seq_scaling_list_present_flags == 0) 164 << "SPS contains scaling lists, which are unsupported."; 165 } 166 } 167 // log2_max_frame_num_minus4: ue(v) 168 RETURN_FALSE_ON_FAIL( 169 sps_parser.ReadExponentialGolomb(&sps_.log2_max_frame_num_minus4)); 170 // pic_order_cnt_type: ue(v) 171 RETURN_FALSE_ON_FAIL( 172 sps_parser.ReadExponentialGolomb(&sps_.pic_order_cnt_type)); 173 174 if (sps_.pic_order_cnt_type == 0) { 175 // log2_max_pic_order_cnt_lsb_minus4: ue(v) 176 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb( 177 &sps_.log2_max_pic_order_cnt_lsb_minus4)); 178 } else if (sps_.pic_order_cnt_type == 1) { 179 // delta_pic_order_always_zero_flag: u(1) 180 RETURN_FALSE_ON_FAIL( 181 sps_parser.ReadBits(&sps_.delta_pic_order_always_zero_flag, 1)); 182 // offset_for_non_ref_pic: se(v) 183 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); 184 // offset_for_top_to_bottom_field: se(v) 185 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); 186 uint32_t num_ref_frames_in_pic_order_cnt_cycle; 187 // num_ref_frames_in_pic_order_cnt_cycle: ue(v) 188 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb( 189 &num_ref_frames_in_pic_order_cnt_cycle)); 190 for (uint32_t i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++) { 191 // offset_for_ref_frame[i]: se(v) 192 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); 193 } 194 } 195 // max_num_ref_frames: ue(v) 196 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); 197 // gaps_in_frame_num_value_allowed_flag: u(1) 198 RETURN_FALSE_ON_FAIL(sps_parser.ReadBits(&bits_tmp, 1)); 199 // pic_width_in_mbs_minus1: ue(v) 200 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); 201 // pic_height_in_map_units_minus1: ue(v) 202 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); 203 // frame_mbs_only_flag: u(1) 204 RETURN_FALSE_ON_FAIL(sps_parser.ReadBits(&sps_.frame_mbs_only_flag, 1)); 205 sps_parsed_ = true; 206 return true; 207} 208 209bool H264BitstreamParser::ParsePpsNalu(const uint8_t* pps, size_t length) { 210 RTC_CHECK(sps_parsed_); 211 // We're starting a new stream, so reset picture type rewriting values. 212 pps_ = PpsState(); 213 pps_parsed_ = false; 214 rtc::scoped_ptr<rtc::ByteBuffer> buffer( 215 ParseRbsp(pps + kNaluHeaderAndTypeSize, length - kNaluHeaderAndTypeSize)); 216 rtc::BitBuffer parser(reinterpret_cast<const uint8*>(buffer->Data()), 217 buffer->Length()); 218 219 uint32_t bits_tmp; 220 uint32_t golomb_ignored; 221 // pic_parameter_set_id: ue(v) 222 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); 223 // seq_parameter_set_id: ue(v) 224 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); 225 // entropy_coding_mode_flag: u(1) 226 uint32_t entropy_coding_mode_flag; 227 RETURN_FALSE_ON_FAIL(parser.ReadBits(&entropy_coding_mode_flag, 1)); 228 // TODO(pbos): Implement CABAC support if spotted in the wild. 229 RTC_CHECK(entropy_coding_mode_flag == 0) 230 << "Don't know how to parse CABAC streams."; 231 // bottom_field_pic_order_in_frame_present_flag: u(1) 232 uint32_t bottom_field_pic_order_in_frame_present_flag; 233 RETURN_FALSE_ON_FAIL( 234 parser.ReadBits(&bottom_field_pic_order_in_frame_present_flag, 1)); 235 pps_.bottom_field_pic_order_in_frame_present_flag = 236 bottom_field_pic_order_in_frame_present_flag != 0; 237 238 // num_slice_groups_minus1: ue(v) 239 uint32_t num_slice_groups_minus1; 240 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&num_slice_groups_minus1)); 241 if (num_slice_groups_minus1 > 0) { 242 uint32_t slice_group_map_type; 243 // slice_group_map_type: ue(v) 244 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&slice_group_map_type)); 245 if (slice_group_map_type == 0) { 246 for (uint32_t i_group = 0; i_group <= num_slice_groups_minus1; 247 ++i_group) { 248 // run_length_minus1[iGroup]: ue(v) 249 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); 250 } 251 } else if (slice_group_map_type == 2) { 252 for (uint32_t i_group = 0; i_group <= num_slice_groups_minus1; 253 ++i_group) { 254 // top_left[iGroup]: ue(v) 255 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); 256 // bottom_right[iGroup]: ue(v) 257 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); 258 } 259 } else if (slice_group_map_type == 3 || slice_group_map_type == 4 || 260 slice_group_map_type == 5) { 261 // slice_group_change_direction_flag: u(1) 262 RETURN_FALSE_ON_FAIL(parser.ReadBits(&bits_tmp, 1)); 263 // slice_group_change_rate_minus1: ue(v) 264 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); 265 } else if (slice_group_map_type == 6) { 266 // pic_size_in_map_units_minus1: ue(v) 267 uint32_t pic_size_in_map_units_minus1; 268 RETURN_FALSE_ON_FAIL( 269 parser.ReadExponentialGolomb(&pic_size_in_map_units_minus1)); 270 uint32_t slice_group_id_bits = 0; 271 uint32_t num_slice_groups = num_slice_groups_minus1 + 1; 272 // If num_slice_groups is not a power of two an additional bit is required 273 // to account for the ceil() of log2() below. 274 if ((num_slice_groups & (num_slice_groups - 1)) != 0) 275 ++slice_group_id_bits; 276 while (num_slice_groups > 0) { 277 num_slice_groups >>= 1; 278 ++slice_group_id_bits; 279 } 280 for (uint32_t i = 0; i <= pic_size_in_map_units_minus1; i++) { 281 // slice_group_id[i]: u(v) 282 // Represented by ceil(log2(num_slice_groups_minus1 + 1)) bits. 283 RETURN_FALSE_ON_FAIL(parser.ReadBits(&bits_tmp, slice_group_id_bits)); 284 } 285 } 286 } 287 // num_ref_idx_l0_default_active_minus1: ue(v) 288 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); 289 // num_ref_idx_l1_default_active_minus1: ue(v) 290 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); 291 // weighted_pred_flag: u(1) 292 uint32_t weighted_pred_flag; 293 RETURN_FALSE_ON_FAIL(parser.ReadBits(&weighted_pred_flag, 1)); 294 pps_.weighted_pred_flag = weighted_pred_flag != 0; 295 // weighted_bipred_idc: u(2) 296 RETURN_FALSE_ON_FAIL(parser.ReadBits(&pps_.weighted_bipred_idc, 2)); 297 298 // pic_init_qp_minus26: se(v) 299 RETURN_FALSE_ON_FAIL( 300 parser.ReadSignedExponentialGolomb(&pps_.pic_init_qp_minus26)); 301 // pic_init_qs_minus26: se(v) 302 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); 303 // chroma_qp_index_offset: se(v) 304 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); 305 // deblocking_filter_control_present_flag: u(1) 306 // constrained_intra_pred_flag: u(1) 307 RETURN_FALSE_ON_FAIL(parser.ReadBits(&bits_tmp, 2)); 308 // redundant_pic_cnt_present_flag: u(1) 309 RETURN_FALSE_ON_FAIL( 310 parser.ReadBits(&pps_.redundant_pic_cnt_present_flag, 1)); 311 312 pps_parsed_ = true; 313 return true; 314} 315 316bool H264BitstreamParser::ParseNonParameterSetNalu(const uint8_t* source, 317 size_t source_length, 318 uint8_t nalu_type) { 319 RTC_CHECK(sps_parsed_); 320 RTC_CHECK(pps_parsed_); 321 last_slice_qp_delta_parsed_ = false; 322 rtc::scoped_ptr<rtc::ByteBuffer> slice_rbsp(ParseRbsp( 323 source + kNaluHeaderAndTypeSize, source_length - kNaluHeaderAndTypeSize)); 324 rtc::BitBuffer slice_reader( 325 reinterpret_cast<const uint8*>(slice_rbsp->Data()), slice_rbsp->Length()); 326 // Check to see if this is an IDR slice, which has an extra field to parse 327 // out. 328 bool is_idr = (source[kNaluHeaderSize] & 0x0F) == kNaluIdr; 329 uint8_t nal_ref_idc = (source[kNaluHeaderSize] & 0x60) >> 5; 330 uint32_t golomb_tmp; 331 uint32_t bits_tmp; 332 333 // first_mb_in_slice: ue(v) 334 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); 335 // slice_type: ue(v) 336 uint32_t slice_type; 337 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&slice_type)); 338 // slice_type's 5..9 range is used to indicate that all slices of a picture 339 // have the same value of slice_type % 5, we don't care about that, so we map 340 // to the corresponding 0..4 range. 341 slice_type %= 5; 342 // pic_parameter_set_id: ue(v) 343 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); 344 if (sps_.separate_colour_plane_flag == 1) { 345 // colour_plane_id 346 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 2)); 347 } 348 // frame_num: u(v) 349 // Represented by log2_max_frame_num_minus4 + 4 bits. 350 RETURN_FALSE_ON_FAIL( 351 slice_reader.ReadBits(&bits_tmp, sps_.log2_max_frame_num_minus4 + 4)); 352 uint32 field_pic_flag = 0; 353 if (sps_.frame_mbs_only_flag == 0) { 354 // field_pic_flag: u(1) 355 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&field_pic_flag, 1)); 356 if (field_pic_flag != 0) { 357 // bottom_field_flag: u(1) 358 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 1)); 359 } 360 } 361 if (is_idr) { 362 // idr_pic_id: ue(v) 363 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); 364 } 365 // pic_order_cnt_lsb: u(v) 366 // Represented by sps_.log2_max_pic_order_cnt_lsb_minus4 + 4 bits. 367 if (sps_.pic_order_cnt_type == 0) { 368 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits( 369 &bits_tmp, sps_.log2_max_pic_order_cnt_lsb_minus4 + 4)); 370 if (pps_.bottom_field_pic_order_in_frame_present_flag && 371 field_pic_flag == 0) { 372 // delta_pic_order_cnt_bottom: se(v) 373 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); 374 } 375 } 376 if (sps_.pic_order_cnt_type == 1 && !sps_.delta_pic_order_always_zero_flag) { 377 // delta_pic_order_cnt[0]: se(v) 378 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); 379 if (pps_.bottom_field_pic_order_in_frame_present_flag && !field_pic_flag) { 380 // delta_pic_order_cnt[1]: se(v) 381 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); 382 } 383 } 384 if (pps_.redundant_pic_cnt_present_flag) { 385 // redundant_pic_cnt: ue(v) 386 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); 387 } 388 if (slice_type == kSliceTypeB) { 389 // direct_spatial_mv_pred_flag: u(1) 390 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 1)); 391 } 392 if (slice_type == kSliceTypeP || slice_type == kSliceTypeSp || 393 slice_type == kSliceTypeB) { 394 uint32_t num_ref_idx_active_override_flag; 395 // num_ref_idx_active_override_flag: u(1) 396 RETURN_FALSE_ON_FAIL( 397 slice_reader.ReadBits(&num_ref_idx_active_override_flag, 1)); 398 if (num_ref_idx_active_override_flag != 0) { 399 // num_ref_idx_l0_active_minus1: ue(v) 400 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); 401 if (slice_type == kSliceTypeB) { 402 // num_ref_idx_l1_active_minus1: ue(v) 403 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); 404 } 405 } 406 } 407 // assume nal_unit_type != 20 && nal_unit_type != 21: 408 RTC_CHECK_NE(nalu_type, 20); 409 RTC_CHECK_NE(nalu_type, 21); 410 // if (nal_unit_type == 20 || nal_unit_type == 21) 411 // ref_pic_list_mvc_modification() 412 // else 413 { 414 // ref_pic_list_modification(): 415 // |slice_type| checks here don't use named constants as they aren't named 416 // in the spec for this segment. Keeping them consistent makes it easier to 417 // verify that they are both the same. 418 if (slice_type % 5 != 2 && slice_type % 5 != 4) { 419 // ref_pic_list_modification_flag_l0: u(1) 420 uint32_t ref_pic_list_modification_flag_l0; 421 RETURN_FALSE_ON_FAIL( 422 slice_reader.ReadBits(&ref_pic_list_modification_flag_l0, 1)); 423 if (ref_pic_list_modification_flag_l0) { 424 uint32_t modification_of_pic_nums_idc; 425 do { 426 // modification_of_pic_nums_idc: ue(v) 427 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb( 428 &modification_of_pic_nums_idc)); 429 if (modification_of_pic_nums_idc == 0 || 430 modification_of_pic_nums_idc == 1) { 431 // abs_diff_pic_num_minus1: ue(v) 432 RETURN_FALSE_ON_FAIL( 433 slice_reader.ReadExponentialGolomb(&golomb_tmp)); 434 } else if (modification_of_pic_nums_idc == 2) { 435 // long_term_pic_num: ue(v) 436 RETURN_FALSE_ON_FAIL( 437 slice_reader.ReadExponentialGolomb(&golomb_tmp)); 438 } 439 } while (modification_of_pic_nums_idc != 3); 440 } 441 } 442 if (slice_type % 5 == 1) { 443 // ref_pic_list_modification_flag_l1: u(1) 444 uint32_t ref_pic_list_modification_flag_l1; 445 RETURN_FALSE_ON_FAIL( 446 slice_reader.ReadBits(&ref_pic_list_modification_flag_l1, 1)); 447 if (ref_pic_list_modification_flag_l1) { 448 uint32_t modification_of_pic_nums_idc; 449 do { 450 // modification_of_pic_nums_idc: ue(v) 451 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb( 452 &modification_of_pic_nums_idc)); 453 if (modification_of_pic_nums_idc == 0 || 454 modification_of_pic_nums_idc == 1) { 455 // abs_diff_pic_num_minus1: ue(v) 456 RETURN_FALSE_ON_FAIL( 457 slice_reader.ReadExponentialGolomb(&golomb_tmp)); 458 } else if (modification_of_pic_nums_idc == 2) { 459 // long_term_pic_num: ue(v) 460 RETURN_FALSE_ON_FAIL( 461 slice_reader.ReadExponentialGolomb(&golomb_tmp)); 462 } 463 } while (modification_of_pic_nums_idc != 3); 464 } 465 } 466 } 467 // TODO(pbos): Do we need support for pred_weight_table()? 468 RTC_CHECK(!((pps_.weighted_pred_flag && 469 (slice_type == kSliceTypeP || slice_type == kSliceTypeSp)) || 470 (pps_.weighted_bipred_idc != 0 && slice_type == kSliceTypeB))) 471 << "Missing support for pred_weight_table()."; 472 // if ((weighted_pred_flag && (slice_type == P || slice_type == SP)) || 473 // (weighted_bipred_idc == 1 && slice_type == B)) { 474 // pred_weight_table() 475 // } 476 if (nal_ref_idc != 0) { 477 // dec_ref_pic_marking(): 478 if (is_idr) { 479 // no_output_of_prior_pics_flag: u(1) 480 // long_term_reference_flag: u(1) 481 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 2)); 482 } else { 483 // adaptive_ref_pic_marking_mode_flag: u(1) 484 uint32_t adaptive_ref_pic_marking_mode_flag; 485 RETURN_FALSE_ON_FAIL( 486 slice_reader.ReadBits(&adaptive_ref_pic_marking_mode_flag, 1)); 487 if (adaptive_ref_pic_marking_mode_flag) { 488 uint32_t memory_management_control_operation; 489 do { 490 // memory_management_control_operation: ue(v) 491 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb( 492 &memory_management_control_operation)); 493 if (memory_management_control_operation == 1 || 494 memory_management_control_operation == 3) { 495 // difference_of_pic_nums_minus1: ue(v) 496 RETURN_FALSE_ON_FAIL( 497 slice_reader.ReadExponentialGolomb(&golomb_tmp)); 498 } 499 if (memory_management_control_operation == 2) { 500 // long_term_pic_num: ue(v) 501 RETURN_FALSE_ON_FAIL( 502 slice_reader.ReadExponentialGolomb(&golomb_tmp)); 503 } 504 if (memory_management_control_operation == 3 || 505 memory_management_control_operation == 6) { 506 // long_term_frame_idx: ue(v) 507 RETURN_FALSE_ON_FAIL( 508 slice_reader.ReadExponentialGolomb(&golomb_tmp)); 509 } 510 if (memory_management_control_operation == 4) { 511 // max_long_term_frame_idx_plus1: ue(v) 512 RETURN_FALSE_ON_FAIL( 513 slice_reader.ReadExponentialGolomb(&golomb_tmp)); 514 } 515 } while (memory_management_control_operation != 0); 516 } 517 } 518 } 519 // cabac not supported: entropy_coding_mode_flag == 0 asserted above. 520 // if (entropy_coding_mode_flag && slice_type != I && slice_type != SI) 521 // cabac_init_idc 522 RETURN_FALSE_ON_FAIL( 523 slice_reader.ReadSignedExponentialGolomb(&last_slice_qp_delta_)); 524 last_slice_qp_delta_parsed_ = true; 525 return true; 526} 527 528void H264BitstreamParser::ParseSlice(const uint8_t* slice, size_t length) { 529 uint8_t nalu_type = slice[4] & kNaluTypeMask; 530 switch (nalu_type) { 531 case kNaluSps: 532 RTC_CHECK(ParseSpsNalu(slice, length)) 533 << "Failed to parse bitstream SPS."; 534 break; 535 case kNaluPps: 536 RTC_CHECK(ParsePpsNalu(slice, length)) 537 << "Failed to parse bitstream PPS."; 538 break; 539 default: 540 RTC_CHECK(ParseNonParameterSetNalu(slice, length, nalu_type)) 541 << "Failed to parse picture slice."; 542 break; 543 } 544} 545 546void H264BitstreamParser::ParseBitstream(const uint8_t* bitstream, 547 size_t length) { 548 RTC_CHECK_GE(length, 4u); 549 std::vector<size_t> slice_markers = FindNaluStartSequences(bitstream, length); 550 RTC_CHECK(!slice_markers.empty()); 551 for (size_t i = 0; i < slice_markers.size() - 1; ++i) { 552 ParseSlice(bitstream + slice_markers[i], 553 slice_markers[i + 1] - slice_markers[i]); 554 } 555 // Parse the last slice. 556 ParseSlice(bitstream + slice_markers.back(), length - slice_markers.back()); 557} 558 559bool H264BitstreamParser::GetLastSliceQp(int* qp) const { 560 if (!last_slice_qp_delta_parsed_) 561 return false; 562 *qp = 26 + pps_.pic_init_qp_minus26 + last_slice_qp_delta_; 563 return true; 564} 565 566} // namespace webrtc 567