| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/common_video/h264/pps_parser.h" | 11 #include "webrtc/common_video/h264/pps_parser.h" |
| 12 | 12 |
| 13 #include <memory> |
| 14 |
| 13 #include "webrtc/common_video/h264/h264_common.h" | 15 #include "webrtc/common_video/h264/h264_common.h" |
| 14 #include "webrtc/base/bitbuffer.h" | 16 #include "webrtc/base/bitbuffer.h" |
| 15 #include "webrtc/base/buffer.h" | 17 #include "webrtc/base/buffer.h" |
| 16 #include "webrtc/base/logging.h" | 18 #include "webrtc/base/logging.h" |
| 17 | 19 |
| 18 #define RETURN_EMPTY_ON_FAIL(x) \ | 20 #define RETURN_EMPTY_ON_FAIL(x) \ |
| 19 if (!(x)) { \ | 21 if (!(x)) { \ |
| 20 return rtc::Optional<PpsParser::PpsState>(); \ | 22 return rtc::Optional<PpsParser::PpsState>(); \ |
| 21 } | 23 } |
| 22 | 24 |
| 23 namespace webrtc { | 25 namespace webrtc { |
| 24 | 26 |
| 25 // General note: this is based off the 02/2014 version of the H.264 standard. | 27 // General note: this is based off the 02/2014 version of the H.264 standard. |
| 26 // You can find it on this page: | 28 // You can find it on this page: |
| 27 // http://www.itu.int/rec/T-REC-H.264 | 29 // http://www.itu.int/rec/T-REC-H.264 |
| 28 | 30 |
| 29 rtc::Optional<PpsParser::PpsState> PpsParser::ParsePps(const uint8_t* data, | 31 rtc::Optional<PpsParser::PpsState> PpsParser::ParsePps(const uint8_t* data, |
| 30 size_t length) { | 32 size_t length) { |
| 31 // First, parse out rbsp, which is basically the source buffer minus emulation | 33 // First, parse out rbsp, which is basically the source buffer minus emulation |
| 32 // bytes (the last byte of a 0x00 0x00 0x03 sequence). RBSP is defined in | 34 // bytes (the last byte of a 0x00 0x00 0x03 sequence). RBSP is defined in |
| 33 // section 7.3.1 of the H.264 standard. | 35 // section 7.3.1 of the H.264 standard. |
| 34 std::unique_ptr<rtc::Buffer> unpacked_buffer = H264::ParseRbsp(data, length); | 36 std::unique_ptr<rtc::Buffer> unpacked_buffer = H264::ParseRbsp(data, length); |
| 35 rtc::BitBuffer bit_buffer(unpacked_buffer->data(), unpacked_buffer->size()); | 37 rtc::BitBuffer bit_buffer(unpacked_buffer->data(), unpacked_buffer->size()); |
| 36 return ParseInternal(&bit_buffer); | 38 return ParseInternal(&bit_buffer); |
| 37 } | 39 } |
| 38 | 40 |
| 41 rtc::Optional<uint32_t> PpsParser::ParsePpsIdFromSlice(const uint8_t* data, |
| 42 size_t length) { |
| 43 std::unique_ptr<rtc::Buffer> slice_rbsp(H264::ParseRbsp(data, length)); |
| 44 rtc::BitBuffer slice_reader(slice_rbsp->data(), slice_rbsp->size()); |
| 45 |
| 46 uint32_t golomb_tmp; |
| 47 // first_mb_in_slice: ue(v) |
| 48 if (!slice_reader.ReadExponentialGolomb(&golomb_tmp)) |
| 49 return rtc::Optional<uint32_t>(); |
| 50 // slice_type: ue(v) |
| 51 if (!slice_reader.ReadExponentialGolomb(&golomb_tmp)) |
| 52 return rtc::Optional<uint32_t>(); |
| 53 // pic_parameter_set_id: ue(v) |
| 54 uint32_t slice_pps_id; |
| 55 if (!slice_reader.ReadExponentialGolomb(&slice_pps_id)) |
| 56 return rtc::Optional<uint32_t>(); |
| 57 return rtc::Optional<uint32_t>(slice_pps_id); |
| 58 } |
| 59 |
| 39 rtc::Optional<PpsParser::PpsState> PpsParser::ParseInternal( | 60 rtc::Optional<PpsParser::PpsState> PpsParser::ParseInternal( |
| 40 rtc::BitBuffer* bit_buffer) { | 61 rtc::BitBuffer* bit_buffer) { |
| 41 PpsState pps; | 62 PpsState pps; |
| 42 | 63 |
| 43 uint32_t bits_tmp; | 64 uint32_t bits_tmp; |
| 44 uint32_t golomb_ignored; | 65 uint32_t golomb_ignored; |
| 45 // pic_parameter_set_id: ue(v) | 66 // pic_parameter_set_id: ue(v) |
| 46 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored)); | 67 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&pps.id)); |
| 47 // seq_parameter_set_id: ue(v) | 68 // seq_parameter_set_id: ue(v) |
| 48 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored)); | 69 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&pps.sps_id)); |
| 49 // entropy_coding_mode_flag: u(1) | 70 // entropy_coding_mode_flag: u(1) |
| 50 uint32_t entropy_coding_mode_flag; | 71 uint32_t entropy_coding_mode_flag; |
| 51 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&entropy_coding_mode_flag, 1)); | 72 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&entropy_coding_mode_flag, 1)); |
| 52 // TODO(pbos): Implement CABAC support if spotted in the wild. | 73 // TODO(pbos): Implement CABAC support if spotted in the wild. |
| 53 RTC_CHECK(entropy_coding_mode_flag == 0) | 74 RTC_CHECK(entropy_coding_mode_flag == 0) |
| 54 << "Don't know how to parse CABAC streams."; | 75 << "Don't know how to parse CABAC streams."; |
| 55 // bottom_field_pic_order_in_frame_present_flag: u(1) | 76 // bottom_field_pic_order_in_frame_present_flag: u(1) |
| 56 uint32_t bottom_field_pic_order_in_frame_present_flag; | 77 uint32_t bottom_field_pic_order_in_frame_present_flag; |
| 57 RETURN_EMPTY_ON_FAIL( | 78 RETURN_EMPTY_ON_FAIL( |
| 58 bit_buffer->ReadBits(&bottom_field_pic_order_in_frame_present_flag, 1)); | 79 bit_buffer->ReadBits(&bottom_field_pic_order_in_frame_present_flag, 1)); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 // constrained_intra_pred_flag: u(1) | 160 // constrained_intra_pred_flag: u(1) |
| 140 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&bits_tmp, 2)); | 161 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&bits_tmp, 2)); |
| 141 // redundant_pic_cnt_present_flag: u(1) | 162 // redundant_pic_cnt_present_flag: u(1) |
| 142 RETURN_EMPTY_ON_FAIL( | 163 RETURN_EMPTY_ON_FAIL( |
| 143 bit_buffer->ReadBits(&pps.redundant_pic_cnt_present_flag, 1)); | 164 bit_buffer->ReadBits(&pps.redundant_pic_cnt_present_flag, 1)); |
| 144 | 165 |
| 145 return rtc::Optional<PpsParser::PpsState>(pps); | 166 return rtc::Optional<PpsParser::PpsState>(pps); |
| 146 } | 167 } |
| 147 | 168 |
| 148 } // namespace webrtc | 169 } // namespace webrtc |
| OLD | NEW |