1 /*
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 "common_video/h264/pps_parser.h"
12 
13 #include <cstdint>
14 #include <vector>
15 
16 #include "common_video/h264/h264_common.h"
17 #include "rtc_base/bit_buffer.h"
18 #include "rtc_base/checks.h"
19 
20 #define RETURN_EMPTY_ON_FAIL(x) \
21   if (!(x)) {                   \
22     return absl::nullopt;       \
23   }
24 
25 namespace {
26 const int kMaxPicInitQpDeltaValue = 25;
27 const int kMinPicInitQpDeltaValue = -26;
28 }  // namespace
29 
30 namespace 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 
ParsePps(const uint8_t * data,size_t length)36 absl::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.
41   std::vector<uint8_t> unpacked_buffer = H264::ParseRbsp(data, length);
42   rtc::BitBuffer bit_buffer(unpacked_buffer.data(), unpacked_buffer.size());
43   return ParseInternal(&bit_buffer);
44 }
45 
ParsePpsIds(const uint8_t * data,size_t length,uint32_t * pps_id,uint32_t * sps_id)46 bool 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.
55   std::vector<uint8_t> unpacked_buffer = H264::ParseRbsp(data, length);
56   rtc::BitBuffer bit_buffer(unpacked_buffer.data(), unpacked_buffer.size());
57   return ParsePpsIdsInternal(&bit_buffer, pps_id, sps_id);
58 }
59 
ParsePpsIdFromSlice(const uint8_t * data,size_t length)60 absl::optional<uint32_t> PpsParser::ParsePpsIdFromSlice(const uint8_t* data,
61                                                         size_t length) {
62   std::vector<uint8_t> unpacked_buffer = H264::ParseRbsp(data, length);
63   rtc::BitBuffer slice_reader(unpacked_buffer.data(), unpacked_buffer.size());
64 
65   uint32_t golomb_tmp;
66   // first_mb_in_slice: ue(v)
67   if (!slice_reader.ReadExponentialGolomb(&golomb_tmp))
68     return absl::nullopt;
69   // slice_type: ue(v)
70   if (!slice_reader.ReadExponentialGolomb(&golomb_tmp))
71     return absl::nullopt;
72   // pic_parameter_set_id: ue(v)
73   uint32_t slice_pps_id;
74   if (!slice_reader.ReadExponentialGolomb(&slice_pps_id))
75     return absl::nullopt;
76   return slice_pps_id;
77 }
78 
ParseInternal(rtc::BitBuffer * bit_buffer)79 absl::optional<PpsParser::PpsState> PpsParser::ParseInternal(
80     rtc::BitBuffer* bit_buffer) {
81   PpsState pps;
82 
83   RETURN_EMPTY_ON_FAIL(ParsePpsIdsInternal(bit_buffer, &pps.id, &pps.sps_id));
84 
85   uint32_t bits_tmp;
86   uint32_t golomb_ignored;
87   // 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));
90   pps.entropy_coding_mode_flag = entropy_coding_mode_flag != 0;
91   // 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));
170   // 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   }
175   // 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 pps;
187 }
188 
ParsePpsIdsInternal(rtc::BitBuffer * bit_buffer,uint32_t * pps_id,uint32_t * sps_id)189 bool 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 
201 }  // namespace webrtc
202