blob: 01a6c76d911ed4670016cf439ae1d8dd01cae52f [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
stefan8a5cef82016-09-02 04:07:28 -070041rtc::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
sprang52033d62016-06-02 02:43:32 -070060rtc::Optional<PpsParser::PpsState> PpsParser::ParseInternal(
61 rtc::BitBuffer* bit_buffer) {
62 PpsState pps;
63
64 uint32_t bits_tmp;
65 uint32_t golomb_ignored;
66 // pic_parameter_set_id: ue(v)
stefan8a5cef82016-09-02 04:07:28 -070067 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&pps.id));
sprang52033d62016-06-02 02:43:32 -070068 // seq_parameter_set_id: ue(v)
stefan8a5cef82016-09-02 04:07:28 -070069 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&pps.sps_id));
sprang52033d62016-06-02 02:43:32 -070070 // entropy_coding_mode_flag: u(1)
71 uint32_t entropy_coding_mode_flag;
72 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&entropy_coding_mode_flag, 1));
73 // TODO(pbos): Implement CABAC support if spotted in the wild.
74 RTC_CHECK(entropy_coding_mode_flag == 0)
75 << "Don't know how to parse CABAC streams.";
76 // bottom_field_pic_order_in_frame_present_flag: u(1)
77 uint32_t bottom_field_pic_order_in_frame_present_flag;
78 RETURN_EMPTY_ON_FAIL(
79 bit_buffer->ReadBits(&bottom_field_pic_order_in_frame_present_flag, 1));
80 pps.bottom_field_pic_order_in_frame_present_flag =
81 bottom_field_pic_order_in_frame_present_flag != 0;
82
83 // num_slice_groups_minus1: ue(v)
84 uint32_t num_slice_groups_minus1;
85 RETURN_EMPTY_ON_FAIL(
86 bit_buffer->ReadExponentialGolomb(&num_slice_groups_minus1));
87 if (num_slice_groups_minus1 > 0) {
88 uint32_t slice_group_map_type;
89 // slice_group_map_type: ue(v)
90 RETURN_EMPTY_ON_FAIL(
91 bit_buffer->ReadExponentialGolomb(&slice_group_map_type));
92 if (slice_group_map_type == 0) {
93 for (uint32_t i_group = 0; i_group <= num_slice_groups_minus1;
94 ++i_group) {
95 // run_length_minus1[iGroup]: ue(v)
96 RETURN_EMPTY_ON_FAIL(
97 bit_buffer->ReadExponentialGolomb(&golomb_ignored));
98 }
99 } else if (slice_group_map_type == 1) {
100 // TODO(sprang): Implement support for dispersed slice group map type.
101 // See 8.2.2.2 Specification for dispersed slice group map type.
102 } else if (slice_group_map_type == 2) {
103 for (uint32_t i_group = 0; i_group <= num_slice_groups_minus1;
104 ++i_group) {
105 // top_left[iGroup]: ue(v)
106 RETURN_EMPTY_ON_FAIL(
107 bit_buffer->ReadExponentialGolomb(&golomb_ignored));
108 // bottom_right[iGroup]: ue(v)
109 RETURN_EMPTY_ON_FAIL(
110 bit_buffer->ReadExponentialGolomb(&golomb_ignored));
111 }
112 } else if (slice_group_map_type == 3 || slice_group_map_type == 4 ||
113 slice_group_map_type == 5) {
114 // slice_group_change_direction_flag: u(1)
115 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&bits_tmp, 1));
116 // slice_group_change_rate_minus1: ue(v)
117 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
118 } else if (slice_group_map_type == 6) {
119 // pic_size_in_map_units_minus1: ue(v)
120 uint32_t pic_size_in_map_units_minus1;
121 RETURN_EMPTY_ON_FAIL(
122 bit_buffer->ReadExponentialGolomb(&pic_size_in_map_units_minus1));
123 uint32_t slice_group_id_bits = 0;
124 uint32_t num_slice_groups = num_slice_groups_minus1 + 1;
125 // If num_slice_groups is not a power of two an additional bit is required
126 // to account for the ceil() of log2() below.
127 if ((num_slice_groups & (num_slice_groups - 1)) != 0)
128 ++slice_group_id_bits;
129 while (num_slice_groups > 0) {
130 num_slice_groups >>= 1;
131 ++slice_group_id_bits;
132 }
133 for (uint32_t i = 0; i <= pic_size_in_map_units_minus1; i++) {
134 // slice_group_id[i]: u(v)
135 // Represented by ceil(log2(num_slice_groups_minus1 + 1)) bits.
136 RETURN_EMPTY_ON_FAIL(
137 bit_buffer->ReadBits(&bits_tmp, slice_group_id_bits));
138 }
139 }
140 }
141 // num_ref_idx_l0_default_active_minus1: ue(v)
142 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
143 // num_ref_idx_l1_default_active_minus1: ue(v)
144 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
145 // weighted_pred_flag: u(1)
146 uint32_t weighted_pred_flag;
147 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&weighted_pred_flag, 1));
148 pps.weighted_pred_flag = weighted_pred_flag != 0;
149 // weighted_bipred_idc: u(2)
150 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&pps.weighted_bipred_idc, 2));
151
152 // pic_init_qp_minus26: se(v)
153 RETURN_EMPTY_ON_FAIL(
154 bit_buffer->ReadSignedExponentialGolomb(&pps.pic_init_qp_minus26));
155 // pic_init_qs_minus26: se(v)
156 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
157 // chroma_qp_index_offset: se(v)
158 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
159 // deblocking_filter_control_present_flag: u(1)
160 // constrained_intra_pred_flag: u(1)
161 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&bits_tmp, 2));
162 // redundant_pic_cnt_present_flag: u(1)
163 RETURN_EMPTY_ON_FAIL(
164 bit_buffer->ReadBits(&pps.redundant_pic_cnt_present_flag, 1));
165
166 return rtc::Optional<PpsParser::PpsState>(pps);
167}
168
169} // namespace webrtc