blob: a48d27bfc7fd8c2706b78dc7b72b41a12d429ac4 [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));
85 // TODO(pbos): Implement CABAC support if spotted in the wild.
86 RTC_CHECK(entropy_coding_mode_flag == 0)
87 << "Don't know how to parse CABAC streams.";
88 // bottom_field_pic_order_in_frame_present_flag: u(1)
89 uint32_t bottom_field_pic_order_in_frame_present_flag;
90 RETURN_EMPTY_ON_FAIL(
91 bit_buffer->ReadBits(&bottom_field_pic_order_in_frame_present_flag, 1));
92 pps.bottom_field_pic_order_in_frame_present_flag =
93 bottom_field_pic_order_in_frame_present_flag != 0;
94
95 // num_slice_groups_minus1: ue(v)
96 uint32_t num_slice_groups_minus1;
97 RETURN_EMPTY_ON_FAIL(
98 bit_buffer->ReadExponentialGolomb(&num_slice_groups_minus1));
99 if (num_slice_groups_minus1 > 0) {
100 uint32_t slice_group_map_type;
101 // slice_group_map_type: ue(v)
102 RETURN_EMPTY_ON_FAIL(
103 bit_buffer->ReadExponentialGolomb(&slice_group_map_type));
104 if (slice_group_map_type == 0) {
105 for (uint32_t i_group = 0; i_group <= num_slice_groups_minus1;
106 ++i_group) {
107 // run_length_minus1[iGroup]: ue(v)
108 RETURN_EMPTY_ON_FAIL(
109 bit_buffer->ReadExponentialGolomb(&golomb_ignored));
110 }
111 } else if (slice_group_map_type == 1) {
112 // TODO(sprang): Implement support for dispersed slice group map type.
113 // See 8.2.2.2 Specification for dispersed slice group map type.
114 } else if (slice_group_map_type == 2) {
115 for (uint32_t i_group = 0; i_group <= num_slice_groups_minus1;
116 ++i_group) {
117 // top_left[iGroup]: ue(v)
118 RETURN_EMPTY_ON_FAIL(
119 bit_buffer->ReadExponentialGolomb(&golomb_ignored));
120 // bottom_right[iGroup]: ue(v)
121 RETURN_EMPTY_ON_FAIL(
122 bit_buffer->ReadExponentialGolomb(&golomb_ignored));
123 }
124 } else if (slice_group_map_type == 3 || slice_group_map_type == 4 ||
125 slice_group_map_type == 5) {
126 // slice_group_change_direction_flag: u(1)
127 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&bits_tmp, 1));
128 // slice_group_change_rate_minus1: ue(v)
129 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
130 } else if (slice_group_map_type == 6) {
131 // pic_size_in_map_units_minus1: ue(v)
132 uint32_t pic_size_in_map_units_minus1;
133 RETURN_EMPTY_ON_FAIL(
134 bit_buffer->ReadExponentialGolomb(&pic_size_in_map_units_minus1));
135 uint32_t slice_group_id_bits = 0;
136 uint32_t num_slice_groups = num_slice_groups_minus1 + 1;
137 // If num_slice_groups is not a power of two an additional bit is required
138 // to account for the ceil() of log2() below.
139 if ((num_slice_groups & (num_slice_groups - 1)) != 0)
140 ++slice_group_id_bits;
141 while (num_slice_groups > 0) {
142 num_slice_groups >>= 1;
143 ++slice_group_id_bits;
144 }
145 for (uint32_t i = 0; i <= pic_size_in_map_units_minus1; i++) {
146 // slice_group_id[i]: u(v)
147 // Represented by ceil(log2(num_slice_groups_minus1 + 1)) bits.
148 RETURN_EMPTY_ON_FAIL(
149 bit_buffer->ReadBits(&bits_tmp, slice_group_id_bits));
150 }
151 }
152 }
153 // num_ref_idx_l0_default_active_minus1: ue(v)
154 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
155 // num_ref_idx_l1_default_active_minus1: ue(v)
156 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
157 // weighted_pred_flag: u(1)
158 uint32_t weighted_pred_flag;
159 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&weighted_pred_flag, 1));
160 pps.weighted_pred_flag = weighted_pred_flag != 0;
161 // weighted_bipred_idc: u(2)
162 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&pps.weighted_bipred_idc, 2));
163
164 // pic_init_qp_minus26: se(v)
165 RETURN_EMPTY_ON_FAIL(
166 bit_buffer->ReadSignedExponentialGolomb(&pps.pic_init_qp_minus26));
167 // pic_init_qs_minus26: se(v)
168 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
169 // chroma_qp_index_offset: se(v)
170 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
171 // deblocking_filter_control_present_flag: u(1)
172 // constrained_intra_pred_flag: u(1)
173 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&bits_tmp, 2));
174 // redundant_pic_cnt_present_flag: u(1)
175 RETURN_EMPTY_ON_FAIL(
176 bit_buffer->ReadBits(&pps.redundant_pic_cnt_present_flag, 1));
177
178 return rtc::Optional<PpsParser::PpsState>(pps);
179}
180
Stefan Holmer126ee722016-09-07 12:46:21 +0200181bool PpsParser::ParsePpsIdsInternal(rtc::BitBuffer* bit_buffer,
182 uint32_t* pps_id,
183 uint32_t* sps_id) {
184 // pic_parameter_set_id: ue(v)
185 if (!bit_buffer->ReadExponentialGolomb(pps_id))
186 return false;
187 // seq_parameter_set_id: ue(v)
188 if (!bit_buffer->ReadExponentialGolomb(sps_id))
189 return false;
190 return true;
191}
192
sprang52033d62016-06-02 02:43:32 -0700193} // namespace webrtc