Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: webrtc/modules/rtp_rtcp/source/h264/pps_parser.cc

Issue 1979443004: Add H264 bitstream rewriting to limit frame reordering marker in header (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Rewriting on the receiver side as well Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 "webrtc/modules/rtp_rtcp/source/h264/pps_parser.h"
12
13 #include "webrtc/modules/rtp_rtcp/source/h264/h264_common.h"
14 #include "webrtc/base/bitbuffer.h"
15 #include "webrtc/base/bytebuffer.h"
16 #include "webrtc/base/logging.h"
17
18 #define RETURN_FALSE_ON_FAIL(x) \
19 if (!(x)) { \
20 return false; \
21 }
22
23 namespace webrtc {
24
25 PpsParser::PpsParser(const uint8_t* buffer, size_t buffer_length)
26 : buffer_(buffer), buffer_length_(buffer_length) {}
27
28 bool PpsParser::Parse() {
29 // General note: this is based off the 02/2014 version of the H.264 standard.
30 // You can find it on this page:
31 // http://www.itu.int/rec/T-REC-H.264
32
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::ByteBufferWriter> rbsp_buffer(
37 H264Common::ParseRbsp(buffer_, buffer_length_));
38 rtc::BitBuffer parser(reinterpret_cast<const uint8_t*>(rbsp_buffer->Data()),
39 rbsp_buffer->Length());
40 PpsState pps;
41
42 uint32_t bits_tmp;
43 uint32_t golomb_ignored;
44 // pic_parameter_set_id: ue(v)
45 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored));
46 // seq_parameter_set_id: ue(v)
47 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored));
48 // entropy_coding_mode_flag: u(1)
49 uint32_t entropy_coding_mode_flag;
50 RETURN_FALSE_ON_FAIL(parser.ReadBits(&entropy_coding_mode_flag, 1));
51 // TODO(pbos): Implement CABAC support if spotted in the wild.
52 RTC_CHECK(entropy_coding_mode_flag == 0)
53 << "Don't know how to parse CABAC streams.";
54 // bottom_field_pic_order_in_frame_present_flag: u(1)
55 uint32_t bottom_field_pic_order_in_frame_present_flag;
56 RETURN_FALSE_ON_FAIL(
57 parser.ReadBits(&bottom_field_pic_order_in_frame_present_flag, 1));
58 pps.bottom_field_pic_order_in_frame_present_flag =
59 bottom_field_pic_order_in_frame_present_flag != 0;
60
61 // num_slice_groups_minus1: ue(v)
62 uint32_t num_slice_groups_minus1;
63 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&num_slice_groups_minus1));
64 if (num_slice_groups_minus1 > 0) {
65 uint32_t slice_group_map_type;
66 // slice_group_map_type: ue(v)
67 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&slice_group_map_type));
68 if (slice_group_map_type == 0) {
69 for (uint32_t i_group = 0; i_group <= num_slice_groups_minus1;
70 ++i_group) {
71 // run_length_minus1[iGroup]: ue(v)
72 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored));
73 }
74 } else if (slice_group_map_type == 2) {
75 for (uint32_t i_group = 0; i_group <= num_slice_groups_minus1;
76 ++i_group) {
77 // top_left[iGroup]: ue(v)
78 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored));
79 // bottom_right[iGroup]: ue(v)
80 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored));
81 }
82 } else if (slice_group_map_type == 3 || slice_group_map_type == 4 ||
83 slice_group_map_type == 5) {
84 // slice_group_change_direction_flag: u(1)
85 RETURN_FALSE_ON_FAIL(parser.ReadBits(&bits_tmp, 1));
86 // slice_group_change_rate_minus1: ue(v)
87 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored));
88 } else if (slice_group_map_type == 6) {
89 // pic_size_in_map_units_minus1: ue(v)
90 uint32_t pic_size_in_map_units_minus1;
91 RETURN_FALSE_ON_FAIL(
92 parser.ReadExponentialGolomb(&pic_size_in_map_units_minus1));
93 uint32_t slice_group_id_bits = 0;
94 uint32_t num_slice_groups = num_slice_groups_minus1 + 1;
95 // If num_slice_groups is not a power of two an additional bit is required
96 // to account for the ceil() of log2() below.
97 if ((num_slice_groups & (num_slice_groups - 1)) != 0)
98 ++slice_group_id_bits;
99 while (num_slice_groups > 0) {
100 num_slice_groups >>= 1;
101 ++slice_group_id_bits;
102 }
103 for (uint32_t i = 0; i <= pic_size_in_map_units_minus1; i++) {
104 // slice_group_id[i]: u(v)
105 // Represented by ceil(log2(num_slice_groups_minus1 + 1)) bits.
106 RETURN_FALSE_ON_FAIL(parser.ReadBits(&bits_tmp, slice_group_id_bits));
107 }
108 }
109 }
110 // num_ref_idx_l0_default_active_minus1: ue(v)
111 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored));
112 // num_ref_idx_l1_default_active_minus1: ue(v)
113 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored));
114 // weighted_pred_flag: u(1)
115 uint32_t weighted_pred_flag;
116 RETURN_FALSE_ON_FAIL(parser.ReadBits(&weighted_pred_flag, 1));
117 pps.weighted_pred_flag = weighted_pred_flag != 0;
118 // weighted_bipred_idc: u(2)
119 RETURN_FALSE_ON_FAIL(parser.ReadBits(&pps.weighted_bipred_idc, 2));
120
121 // pic_init_qp_minus26: se(v)
122 RETURN_FALSE_ON_FAIL(
123 parser.ReadSignedExponentialGolomb(&pps.pic_init_qp_minus26));
124 // pic_init_qs_minus26: se(v)
125 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored));
126 // chroma_qp_index_offset: se(v)
127 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored));
128 // deblocking_filter_control_present_flag: u(1)
129 // constrained_intra_pred_flag: u(1)
130 RETURN_FALSE_ON_FAIL(parser.ReadBits(&bits_tmp, 2));
131 // redundant_pic_cnt_present_flag: u(1)
132 RETURN_FALSE_ON_FAIL(parser.ReadBits(&pps.redundant_pic_cnt_present_flag, 1));
133
134 state_ = rtc::Optional<PpsState>(pps);
135 return true;
136 }
137
138 const rtc::Optional<PpsParser::PpsState>& PpsParser::GetState() const {
139 return state_;
140 }
141
142 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698