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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: webrtc/modules/rtp_rtcp/source/h264/pps_parser.cc
diff --git a/webrtc/modules/rtp_rtcp/source/h264/pps_parser.cc b/webrtc/modules/rtp_rtcp/source/h264/pps_parser.cc
new file mode 100644
index 0000000000000000000000000000000000000000..63eeb164d4cb1ad7c9482e2a2013b5caeb237ad6
--- /dev/null
+++ b/webrtc/modules/rtp_rtcp/source/h264/pps_parser.cc
@@ -0,0 +1,142 @@
+/*
+ * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "webrtc/modules/rtp_rtcp/source/h264/pps_parser.h"
+
+#include "webrtc/modules/rtp_rtcp/source/h264/h264_common.h"
+#include "webrtc/base/bitbuffer.h"
+#include "webrtc/base/bytebuffer.h"
+#include "webrtc/base/logging.h"
+
+#define RETURN_FALSE_ON_FAIL(x) \
+ if (!(x)) { \
+ return false; \
+ }
+
+namespace webrtc {
+
+PpsParser::PpsParser(const uint8_t* buffer, size_t buffer_length)
+ : buffer_(buffer), buffer_length_(buffer_length) {}
+
+bool PpsParser::Parse() {
+ // General note: this is based off the 02/2014 version of the H.264 standard.
+ // You can find it on this page:
+ // http://www.itu.int/rec/T-REC-H.264
+
+ // First, parse out rbsp, which is basically the source buffer minus emulation
+ // bytes (the last byte of a 0x00 0x00 0x03 sequence). RBSP is defined in
+ // section 7.3.1 of the H.264 standard.
+ std::unique_ptr<rtc::ByteBufferWriter> rbsp_buffer(
+ H264Common::ParseRbsp(buffer_, buffer_length_));
+ rtc::BitBuffer parser(reinterpret_cast<const uint8_t*>(rbsp_buffer->Data()),
+ rbsp_buffer->Length());
+ PpsState pps;
+
+ uint32_t bits_tmp;
+ uint32_t golomb_ignored;
+ // pic_parameter_set_id: ue(v)
+ RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored));
+ // seq_parameter_set_id: ue(v)
+ RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored));
+ // entropy_coding_mode_flag: u(1)
+ uint32_t entropy_coding_mode_flag;
+ RETURN_FALSE_ON_FAIL(parser.ReadBits(&entropy_coding_mode_flag, 1));
+ // TODO(pbos): Implement CABAC support if spotted in the wild.
+ RTC_CHECK(entropy_coding_mode_flag == 0)
+ << "Don't know how to parse CABAC streams.";
+ // bottom_field_pic_order_in_frame_present_flag: u(1)
+ uint32_t bottom_field_pic_order_in_frame_present_flag;
+ RETURN_FALSE_ON_FAIL(
+ parser.ReadBits(&bottom_field_pic_order_in_frame_present_flag, 1));
+ pps.bottom_field_pic_order_in_frame_present_flag =
+ bottom_field_pic_order_in_frame_present_flag != 0;
+
+ // num_slice_groups_minus1: ue(v)
+ uint32_t num_slice_groups_minus1;
+ RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&num_slice_groups_minus1));
+ if (num_slice_groups_minus1 > 0) {
+ uint32_t slice_group_map_type;
+ // slice_group_map_type: ue(v)
+ RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&slice_group_map_type));
+ if (slice_group_map_type == 0) {
+ for (uint32_t i_group = 0; i_group <= num_slice_groups_minus1;
+ ++i_group) {
+ // run_length_minus1[iGroup]: ue(v)
+ RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored));
+ }
+ } else if (slice_group_map_type == 2) {
+ for (uint32_t i_group = 0; i_group <= num_slice_groups_minus1;
+ ++i_group) {
+ // top_left[iGroup]: ue(v)
+ RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored));
+ // bottom_right[iGroup]: ue(v)
+ RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored));
+ }
+ } else if (slice_group_map_type == 3 || slice_group_map_type == 4 ||
+ slice_group_map_type == 5) {
+ // slice_group_change_direction_flag: u(1)
+ RETURN_FALSE_ON_FAIL(parser.ReadBits(&bits_tmp, 1));
+ // slice_group_change_rate_minus1: ue(v)
+ RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored));
+ } else if (slice_group_map_type == 6) {
+ // pic_size_in_map_units_minus1: ue(v)
+ uint32_t pic_size_in_map_units_minus1;
+ RETURN_FALSE_ON_FAIL(
+ parser.ReadExponentialGolomb(&pic_size_in_map_units_minus1));
+ uint32_t slice_group_id_bits = 0;
+ uint32_t num_slice_groups = num_slice_groups_minus1 + 1;
+ // If num_slice_groups is not a power of two an additional bit is required
+ // to account for the ceil() of log2() below.
+ if ((num_slice_groups & (num_slice_groups - 1)) != 0)
+ ++slice_group_id_bits;
+ while (num_slice_groups > 0) {
+ num_slice_groups >>= 1;
+ ++slice_group_id_bits;
+ }
+ for (uint32_t i = 0; i <= pic_size_in_map_units_minus1; i++) {
+ // slice_group_id[i]: u(v)
+ // Represented by ceil(log2(num_slice_groups_minus1 + 1)) bits.
+ RETURN_FALSE_ON_FAIL(parser.ReadBits(&bits_tmp, slice_group_id_bits));
+ }
+ }
+ }
+ // num_ref_idx_l0_default_active_minus1: ue(v)
+ RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored));
+ // num_ref_idx_l1_default_active_minus1: ue(v)
+ RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored));
+ // weighted_pred_flag: u(1)
+ uint32_t weighted_pred_flag;
+ RETURN_FALSE_ON_FAIL(parser.ReadBits(&weighted_pred_flag, 1));
+ pps.weighted_pred_flag = weighted_pred_flag != 0;
+ // weighted_bipred_idc: u(2)
+ RETURN_FALSE_ON_FAIL(parser.ReadBits(&pps.weighted_bipred_idc, 2));
+
+ // pic_init_qp_minus26: se(v)
+ RETURN_FALSE_ON_FAIL(
+ parser.ReadSignedExponentialGolomb(&pps.pic_init_qp_minus26));
+ // pic_init_qs_minus26: se(v)
+ RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored));
+ // chroma_qp_index_offset: se(v)
+ RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored));
+ // deblocking_filter_control_present_flag: u(1)
+ // constrained_intra_pred_flag: u(1)
+ RETURN_FALSE_ON_FAIL(parser.ReadBits(&bits_tmp, 2));
+ // redundant_pic_cnt_present_flag: u(1)
+ RETURN_FALSE_ON_FAIL(parser.ReadBits(&pps.redundant_pic_cnt_present_flag, 1));
+
+ state_ = rtc::Optional<PpsState>(pps);
+ return true;
+}
+
+const rtc::Optional<PpsParser::PpsState>& PpsParser::GetState() const {
+ return state_;
+}
+
+} // namespace webrtc

Powered by Google App Engine
This is Rietveld 408576698