blob: 0df0289bdddf16a3fcc760856bfbec0e1c896f4b [file] [log] [blame]
sprang52033d62016-06-02 02:43:32 -07001/*
2 * Copyright (c) 2016 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
11#include "webrtc/common_video/h264/pps_parser.h"
12
stefan8a5cef82016-09-02 04:07:28 -070013#include <memory>
kthelgason156e3af2017-03-06 00:04:32 -080014#include <vector>
stefan8a5cef82016-09-02 04:07:28 -070015
sprang52033d62016-06-02 02:43:32 -070016#include "webrtc/common_video/h264/h264_common.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020017#include "webrtc/rtc_base/bitbuffer.h"
18#include "webrtc/rtc_base/logging.h"
sprang52033d62016-06-02 02:43:32 -070019
20#define RETURN_EMPTY_ON_FAIL(x) \
21 if (!(x)) { \
22 return rtc::Optional<PpsParser::PpsState>(); \
23 }
24
kthelgasonb3363922016-12-02 01:29:48 -080025namespace {
26const int kMaxPicInitQpDeltaValue = 25;
27const int kMinPicInitQpDeltaValue = -26;
28}
29
sprang52033d62016-06-02 02:43:32 -070030namespace webrtc {
31
32// General note: this is based off the 02/2014 version of the H.264 standard.
33// You can find it on this page:
34// http://www.itu.int/rec/T-REC-H.264
35
36rtc::Optional<PpsParser::PpsState> PpsParser::ParsePps(const uint8_t* data,
37 size_t length) {
38 // First, parse out rbsp, which is basically the source buffer minus emulation
39 // bytes (the last byte of a 0x00 0x00 0x03 sequence). RBSP is defined in
40 // section 7.3.1 of the H.264 standard.
kthelgason156e3af2017-03-06 00:04:32 -080041 std::vector<uint8_t> unpacked_buffer = H264::ParseRbsp(data, length);
42 rtc::BitBuffer bit_buffer(unpacked_buffer.data(), unpacked_buffer.size());
sprang52033d62016-06-02 02:43:32 -070043 return ParseInternal(&bit_buffer);
44}
45
Stefan Holmer126ee722016-09-07 12:46:21 +020046bool PpsParser::ParsePpsIds(const uint8_t* data,
47 size_t length,
48 uint32_t* pps_id,
49 uint32_t* sps_id) {
50 RTC_DCHECK(pps_id);
51 RTC_DCHECK(sps_id);
52 // First, parse out rbsp, which is basically the source buffer minus emulation
53 // bytes (the last byte of a 0x00 0x00 0x03 sequence). RBSP is defined in
54 // section 7.3.1 of the H.264 standard.
kthelgason156e3af2017-03-06 00:04:32 -080055 std::vector<uint8_t> unpacked_buffer = H264::ParseRbsp(data, length);
56 rtc::BitBuffer bit_buffer(unpacked_buffer.data(), unpacked_buffer.size());
Stefan Holmer126ee722016-09-07 12:46:21 +020057 return ParsePpsIdsInternal(&bit_buffer, pps_id, sps_id);
58}
59
stefan8a5cef82016-09-02 04:07:28 -070060rtc::Optional<uint32_t> PpsParser::ParsePpsIdFromSlice(const uint8_t* data,
61 size_t length) {
kthelgason156e3af2017-03-06 00:04:32 -080062 std::vector<uint8_t> unpacked_buffer = H264::ParseRbsp(data, length);
63 rtc::BitBuffer slice_reader(unpacked_buffer.data(), unpacked_buffer.size());
stefan8a5cef82016-09-02 04:07:28 -070064
65 uint32_t golomb_tmp;
66 // first_mb_in_slice: ue(v)
67 if (!slice_reader.ReadExponentialGolomb(&golomb_tmp))
68 return rtc::Optional<uint32_t>();
69 // slice_type: ue(v)
70 if (!slice_reader.ReadExponentialGolomb(&golomb_tmp))
71 return rtc::Optional<uint32_t>();
72 // pic_parameter_set_id: ue(v)
73 uint32_t slice_pps_id;
74 if (!slice_reader.ReadExponentialGolomb(&slice_pps_id))
75 return rtc::Optional<uint32_t>();
76 return rtc::Optional<uint32_t>(slice_pps_id);
77}
78
sprang52033d62016-06-02 02:43:32 -070079rtc::Optional<PpsParser::PpsState> PpsParser::ParseInternal(
80 rtc::BitBuffer* bit_buffer) {
81 PpsState pps;
82
Stefan Holmer126ee722016-09-07 12:46:21 +020083 RETURN_EMPTY_ON_FAIL(ParsePpsIdsInternal(bit_buffer, &pps.id, &pps.sps_id));
84
sprang52033d62016-06-02 02:43:32 -070085 uint32_t bits_tmp;
86 uint32_t golomb_ignored;
sprang52033d62016-06-02 02:43:32 -070087 // entropy_coding_mode_flag: u(1)
88 uint32_t entropy_coding_mode_flag;
89 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&entropy_coding_mode_flag, 1));
kthelgasond020f3f2016-10-05 02:16:22 -070090 pps.entropy_coding_mode_flag = entropy_coding_mode_flag != 0;
sprang52033d62016-06-02 02:43:32 -070091 // bottom_field_pic_order_in_frame_present_flag: u(1)
92 uint32_t bottom_field_pic_order_in_frame_present_flag;
93 RETURN_EMPTY_ON_FAIL(
94 bit_buffer->ReadBits(&bottom_field_pic_order_in_frame_present_flag, 1));
95 pps.bottom_field_pic_order_in_frame_present_flag =
96 bottom_field_pic_order_in_frame_present_flag != 0;
97
98 // num_slice_groups_minus1: ue(v)
99 uint32_t num_slice_groups_minus1;
100 RETURN_EMPTY_ON_FAIL(
101 bit_buffer->ReadExponentialGolomb(&num_slice_groups_minus1));
102 if (num_slice_groups_minus1 > 0) {
103 uint32_t slice_group_map_type;
104 // slice_group_map_type: ue(v)
105 RETURN_EMPTY_ON_FAIL(
106 bit_buffer->ReadExponentialGolomb(&slice_group_map_type));
107 if (slice_group_map_type == 0) {
108 for (uint32_t i_group = 0; i_group <= num_slice_groups_minus1;
109 ++i_group) {
110 // run_length_minus1[iGroup]: ue(v)
111 RETURN_EMPTY_ON_FAIL(
112 bit_buffer->ReadExponentialGolomb(&golomb_ignored));
113 }
114 } else if (slice_group_map_type == 1) {
115 // TODO(sprang): Implement support for dispersed slice group map type.
116 // See 8.2.2.2 Specification for dispersed slice group map type.
117 } else if (slice_group_map_type == 2) {
118 for (uint32_t i_group = 0; i_group <= num_slice_groups_minus1;
119 ++i_group) {
120 // top_left[iGroup]: ue(v)
121 RETURN_EMPTY_ON_FAIL(
122 bit_buffer->ReadExponentialGolomb(&golomb_ignored));
123 // bottom_right[iGroup]: ue(v)
124 RETURN_EMPTY_ON_FAIL(
125 bit_buffer->ReadExponentialGolomb(&golomb_ignored));
126 }
127 } else if (slice_group_map_type == 3 || slice_group_map_type == 4 ||
128 slice_group_map_type == 5) {
129 // slice_group_change_direction_flag: u(1)
130 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&bits_tmp, 1));
131 // slice_group_change_rate_minus1: ue(v)
132 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
133 } else if (slice_group_map_type == 6) {
134 // pic_size_in_map_units_minus1: ue(v)
135 uint32_t pic_size_in_map_units_minus1;
136 RETURN_EMPTY_ON_FAIL(
137 bit_buffer->ReadExponentialGolomb(&pic_size_in_map_units_minus1));
138 uint32_t slice_group_id_bits = 0;
139 uint32_t num_slice_groups = num_slice_groups_minus1 + 1;
140 // If num_slice_groups is not a power of two an additional bit is required
141 // to account for the ceil() of log2() below.
142 if ((num_slice_groups & (num_slice_groups - 1)) != 0)
143 ++slice_group_id_bits;
144 while (num_slice_groups > 0) {
145 num_slice_groups >>= 1;
146 ++slice_group_id_bits;
147 }
148 for (uint32_t i = 0; i <= pic_size_in_map_units_minus1; i++) {
149 // slice_group_id[i]: u(v)
150 // Represented by ceil(log2(num_slice_groups_minus1 + 1)) bits.
151 RETURN_EMPTY_ON_FAIL(
152 bit_buffer->ReadBits(&bits_tmp, slice_group_id_bits));
153 }
154 }
155 }
156 // num_ref_idx_l0_default_active_minus1: ue(v)
157 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
158 // num_ref_idx_l1_default_active_minus1: ue(v)
159 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
160 // weighted_pred_flag: u(1)
161 uint32_t weighted_pred_flag;
162 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&weighted_pred_flag, 1));
163 pps.weighted_pred_flag = weighted_pred_flag != 0;
164 // weighted_bipred_idc: u(2)
165 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&pps.weighted_bipred_idc, 2));
166
167 // pic_init_qp_minus26: se(v)
168 RETURN_EMPTY_ON_FAIL(
169 bit_buffer->ReadSignedExponentialGolomb(&pps.pic_init_qp_minus26));
kthelgasonb3363922016-12-02 01:29:48 -0800170 // Sanity-check parsed value
171 if (pps.pic_init_qp_minus26 > kMaxPicInitQpDeltaValue ||
172 pps.pic_init_qp_minus26 < kMinPicInitQpDeltaValue) {
173 RETURN_EMPTY_ON_FAIL(false);
174 }
sprang52033d62016-06-02 02:43:32 -0700175 // pic_init_qs_minus26: se(v)
176 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
177 // chroma_qp_index_offset: se(v)
178 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
179 // deblocking_filter_control_present_flag: u(1)
180 // constrained_intra_pred_flag: u(1)
181 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&bits_tmp, 2));
182 // redundant_pic_cnt_present_flag: u(1)
183 RETURN_EMPTY_ON_FAIL(
184 bit_buffer->ReadBits(&pps.redundant_pic_cnt_present_flag, 1));
185
186 return rtc::Optional<PpsParser::PpsState>(pps);
187}
188
Stefan Holmer126ee722016-09-07 12:46:21 +0200189bool PpsParser::ParsePpsIdsInternal(rtc::BitBuffer* bit_buffer,
190 uint32_t* pps_id,
191 uint32_t* sps_id) {
192 // pic_parameter_set_id: ue(v)
193 if (!bit_buffer->ReadExponentialGolomb(pps_id))
194 return false;
195 // seq_parameter_set_id: ue(v)
196 if (!bit_buffer->ReadExponentialGolomb(sps_id))
197 return false;
198 return true;
199}
200
sprang52033d62016-06-02 02:43:32 -0700201} // namespace webrtc