blob: 0215549959e39be2d224090539f29938c096a486 [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>
14
sprang52033d62016-06-02 02:43:32 -070015#include "webrtc/common_video/h264/h264_common.h"
16#include "webrtc/base/bitbuffer.h"
17#include "webrtc/base/buffer.h"
18#include "webrtc/base/logging.h"
19
20#define RETURN_EMPTY_ON_FAIL(x) \
21 if (!(x)) { \
22 return rtc::Optional<PpsParser::PpsState>(); \
23 }
24
25namespace webrtc {
26
27// General note: this is based off the 02/2014 version of the H.264 standard.
28// You can find it on this page:
29// http://www.itu.int/rec/T-REC-H.264
30
31rtc::Optional<PpsParser::PpsState> PpsParser::ParsePps(const uint8_t* data,
32 size_t length) {
33 // First, parse out rbsp, which is basically the source buffer minus emulation
34 // bytes (the last byte of a 0x00 0x00 0x03 sequence). RBSP is defined in
35 // section 7.3.1 of the H.264 standard.
36 std::unique_ptr<rtc::Buffer> unpacked_buffer = H264::ParseRbsp(data, length);
37 rtc::BitBuffer bit_buffer(unpacked_buffer->data(), unpacked_buffer->size());
38 return ParseInternal(&bit_buffer);
39}
40
Stefan Holmer126ee722016-09-07 12:46:21 +020041bool PpsParser::ParsePpsIds(const uint8_t* data,
42 size_t length,
43 uint32_t* pps_id,
44 uint32_t* sps_id) {
45 RTC_DCHECK(pps_id);
46 RTC_DCHECK(sps_id);
47 // First, parse out rbsp, which is basically the source buffer minus emulation
48 // bytes (the last byte of a 0x00 0x00 0x03 sequence). RBSP is defined in
49 // section 7.3.1 of the H.264 standard.
50 std::unique_ptr<rtc::Buffer> unpacked_buffer = H264::ParseRbsp(data, length);
51 rtc::BitBuffer bit_buffer(unpacked_buffer->data(), unpacked_buffer->size());
52 return ParsePpsIdsInternal(&bit_buffer, pps_id, sps_id);
53}
54
stefan8a5cef82016-09-02 04:07:28 -070055rtc::Optional<uint32_t> PpsParser::ParsePpsIdFromSlice(const uint8_t* data,
56 size_t length) {
57 std::unique_ptr<rtc::Buffer> slice_rbsp(H264::ParseRbsp(data, length));
58 rtc::BitBuffer slice_reader(slice_rbsp->data(), slice_rbsp->size());
59
60 uint32_t golomb_tmp;
61 // first_mb_in_slice: ue(v)
62 if (!slice_reader.ReadExponentialGolomb(&golomb_tmp))
63 return rtc::Optional<uint32_t>();
64 // slice_type: ue(v)
65 if (!slice_reader.ReadExponentialGolomb(&golomb_tmp))
66 return rtc::Optional<uint32_t>();
67 // pic_parameter_set_id: ue(v)
68 uint32_t slice_pps_id;
69 if (!slice_reader.ReadExponentialGolomb(&slice_pps_id))
70 return rtc::Optional<uint32_t>();
71 return rtc::Optional<uint32_t>(slice_pps_id);
72}
73
sprang52033d62016-06-02 02:43:32 -070074rtc::Optional<PpsParser::PpsState> PpsParser::ParseInternal(
75 rtc::BitBuffer* bit_buffer) {
76 PpsState pps;
77
Stefan Holmer126ee722016-09-07 12:46:21 +020078 RETURN_EMPTY_ON_FAIL(ParsePpsIdsInternal(bit_buffer, &pps.id, &pps.sps_id));
79
sprang52033d62016-06-02 02:43:32 -070080 uint32_t bits_tmp;
81 uint32_t golomb_ignored;
sprang52033d62016-06-02 02:43:32 -070082 // entropy_coding_mode_flag: u(1)
83 uint32_t entropy_coding_mode_flag;
84 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&entropy_coding_mode_flag, 1));
kthelgasond020f3f2016-10-05 02:16:22 -070085 pps.entropy_coding_mode_flag = entropy_coding_mode_flag != 0;
sprang52033d62016-06-02 02:43:32 -070086 // bottom_field_pic_order_in_frame_present_flag: u(1)
87 uint32_t bottom_field_pic_order_in_frame_present_flag;
88 RETURN_EMPTY_ON_FAIL(
89 bit_buffer->ReadBits(&bottom_field_pic_order_in_frame_present_flag, 1));
90 pps.bottom_field_pic_order_in_frame_present_flag =
91 bottom_field_pic_order_in_frame_present_flag != 0;
92
93 // num_slice_groups_minus1: ue(v)
94 uint32_t num_slice_groups_minus1;
95 RETURN_EMPTY_ON_FAIL(
96 bit_buffer->ReadExponentialGolomb(&num_slice_groups_minus1));
97 if (num_slice_groups_minus1 > 0) {
98 uint32_t slice_group_map_type;
99 // slice_group_map_type: ue(v)
100 RETURN_EMPTY_ON_FAIL(
101 bit_buffer->ReadExponentialGolomb(&slice_group_map_type));
102 if (slice_group_map_type == 0) {
103 for (uint32_t i_group = 0; i_group <= num_slice_groups_minus1;
104 ++i_group) {
105 // run_length_minus1[iGroup]: ue(v)
106 RETURN_EMPTY_ON_FAIL(
107 bit_buffer->ReadExponentialGolomb(&golomb_ignored));
108 }
109 } else if (slice_group_map_type == 1) {
110 // TODO(sprang): Implement support for dispersed slice group map type.
111 // See 8.2.2.2 Specification for dispersed slice group map type.
112 } else if (slice_group_map_type == 2) {
113 for (uint32_t i_group = 0; i_group <= num_slice_groups_minus1;
114 ++i_group) {
115 // top_left[iGroup]: ue(v)
116 RETURN_EMPTY_ON_FAIL(
117 bit_buffer->ReadExponentialGolomb(&golomb_ignored));
118 // bottom_right[iGroup]: ue(v)
119 RETURN_EMPTY_ON_FAIL(
120 bit_buffer->ReadExponentialGolomb(&golomb_ignored));
121 }
122 } else if (slice_group_map_type == 3 || slice_group_map_type == 4 ||
123 slice_group_map_type == 5) {
124 // slice_group_change_direction_flag: u(1)
125 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&bits_tmp, 1));
126 // slice_group_change_rate_minus1: ue(v)
127 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
128 } else if (slice_group_map_type == 6) {
129 // pic_size_in_map_units_minus1: ue(v)
130 uint32_t pic_size_in_map_units_minus1;
131 RETURN_EMPTY_ON_FAIL(
132 bit_buffer->ReadExponentialGolomb(&pic_size_in_map_units_minus1));
133 uint32_t slice_group_id_bits = 0;
134 uint32_t num_slice_groups = num_slice_groups_minus1 + 1;
135 // If num_slice_groups is not a power of two an additional bit is required
136 // to account for the ceil() of log2() below.
137 if ((num_slice_groups & (num_slice_groups - 1)) != 0)
138 ++slice_group_id_bits;
139 while (num_slice_groups > 0) {
140 num_slice_groups >>= 1;
141 ++slice_group_id_bits;
142 }
143 for (uint32_t i = 0; i <= pic_size_in_map_units_minus1; i++) {
144 // slice_group_id[i]: u(v)
145 // Represented by ceil(log2(num_slice_groups_minus1 + 1)) bits.
146 RETURN_EMPTY_ON_FAIL(
147 bit_buffer->ReadBits(&bits_tmp, slice_group_id_bits));
148 }
149 }
150 }
151 // num_ref_idx_l0_default_active_minus1: ue(v)
152 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
153 // num_ref_idx_l1_default_active_minus1: ue(v)
154 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
155 // weighted_pred_flag: u(1)
156 uint32_t weighted_pred_flag;
157 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&weighted_pred_flag, 1));
158 pps.weighted_pred_flag = weighted_pred_flag != 0;
159 // weighted_bipred_idc: u(2)
160 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&pps.weighted_bipred_idc, 2));
161
162 // pic_init_qp_minus26: se(v)
163 RETURN_EMPTY_ON_FAIL(
164 bit_buffer->ReadSignedExponentialGolomb(&pps.pic_init_qp_minus26));
165 // pic_init_qs_minus26: se(v)
166 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
167 // chroma_qp_index_offset: se(v)
168 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
169 // deblocking_filter_control_present_flag: u(1)
170 // constrained_intra_pred_flag: u(1)
171 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&bits_tmp, 2));
172 // redundant_pic_cnt_present_flag: u(1)
173 RETURN_EMPTY_ON_FAIL(
174 bit_buffer->ReadBits(&pps.redundant_pic_cnt_present_flag, 1));
175
176 return rtc::Optional<PpsParser::PpsState>(pps);
177}
178
Stefan Holmer126ee722016-09-07 12:46:21 +0200179bool PpsParser::ParsePpsIdsInternal(rtc::BitBuffer* bit_buffer,
180 uint32_t* pps_id,
181 uint32_t* sps_id) {
182 // pic_parameter_set_id: ue(v)
183 if (!bit_buffer->ReadExponentialGolomb(pps_id))
184 return false;
185 // seq_parameter_set_id: ue(v)
186 if (!bit_buffer->ReadExponentialGolomb(sps_id))
187 return false;
188 return true;
189}
190
sprang52033d62016-06-02 02:43:32 -0700191} // namespace webrtc