aboutsummaryrefslogtreecommitdiff
path: root/common_video/h264/h264_bitstream_parser.cc
diff options
context:
space:
mode:
Diffstat (limited to 'common_video/h264/h264_bitstream_parser.cc')
-rw-r--r--common_video/h264/h264_bitstream_parser.cc183
1 files changed, 76 insertions, 107 deletions
diff --git a/common_video/h264/h264_bitstream_parser.cc b/common_video/h264/h264_bitstream_parser.cc
index 5a75f48f88..2311d0d2ee 100644
--- a/common_video/h264/h264_bitstream_parser.cc
+++ b/common_video/h264/h264_bitstream_parser.cc
@@ -15,29 +15,20 @@
#include <vector>
#include "common_video/h264/h264_common.h"
-#include "rtc_base/bit_buffer.h"
+#include "rtc_base/bitstream_reader.h"
#include "rtc_base/logging.h"
+namespace webrtc {
namespace {
-const int kMaxAbsQpDeltaValue = 51;
-const int kMinQpValue = 0;
-const int kMaxQpValue = 51;
+constexpr int kMaxAbsQpDeltaValue = 51;
+constexpr int kMinQpValue = 0;
+constexpr int kMaxQpValue = 51;
} // namespace
-namespace webrtc {
-
-#define RETURN_ON_FAIL(x, res) \
- if (!(x)) { \
- RTC_LOG_F(LS_ERROR) << "FAILED: " #x; \
- return res; \
- }
-
-#define RETURN_INV_ON_FAIL(x) RETURN_ON_FAIL(x, kInvalidStream)
-
-H264BitstreamParser::H264BitstreamParser() {}
-H264BitstreamParser::~H264BitstreamParser() {}
+H264BitstreamParser::H264BitstreamParser() = default;
+H264BitstreamParser::~H264BitstreamParser() = default;
H264BitstreamParser::Result H264BitstreamParser::ParseNonParameterSetNalu(
const uint8_t* source,
@@ -52,95 +43,90 @@ H264BitstreamParser::Result H264BitstreamParser::ParseNonParameterSetNalu(
if (slice_rbsp.size() < H264::kNaluTypeSize)
return kInvalidStream;
- rtc::BitBuffer slice_reader(slice_rbsp.data() + H264::kNaluTypeSize,
- slice_rbsp.size() - H264::kNaluTypeSize);
+ BitstreamReader slice_reader(slice_rbsp);
+ slice_reader.ConsumeBits(H264::kNaluTypeSize * 8);
+
// Check to see if this is an IDR slice, which has an extra field to parse
// out.
bool is_idr = (source[0] & 0x0F) == H264::NaluType::kIdr;
uint8_t nal_ref_idc = (source[0] & 0x60) >> 5;
- uint32_t golomb_tmp;
- uint32_t bits_tmp;
// first_mb_in_slice: ue(v)
- RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
+ slice_reader.ReadExponentialGolomb();
// slice_type: ue(v)
- uint32_t slice_type;
- RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(&slice_type));
+ uint32_t slice_type = slice_reader.ReadExponentialGolomb();
// slice_type's 5..9 range is used to indicate that all slices of a picture
// have the same value of slice_type % 5, we don't care about that, so we map
// to the corresponding 0..4 range.
slice_type %= 5;
// pic_parameter_set_id: ue(v)
- RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
+ slice_reader.ReadExponentialGolomb();
if (sps_->separate_colour_plane_flag == 1) {
// colour_plane_id
- RETURN_INV_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 2));
+ slice_reader.ConsumeBits(2);
}
// frame_num: u(v)
// Represented by log2_max_frame_num bits.
- RETURN_INV_ON_FAIL(
- slice_reader.ReadBits(&bits_tmp, sps_->log2_max_frame_num));
- uint32_t field_pic_flag = 0;
+ slice_reader.ConsumeBits(sps_->log2_max_frame_num);
+ bool field_pic_flag = false;
if (sps_->frame_mbs_only_flag == 0) {
// field_pic_flag: u(1)
- RETURN_INV_ON_FAIL(slice_reader.ReadBits(&field_pic_flag, 1));
- if (field_pic_flag != 0) {
+ field_pic_flag = slice_reader.Read<bool>();
+ if (field_pic_flag) {
// bottom_field_flag: u(1)
- RETURN_INV_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 1));
+ slice_reader.ConsumeBits(1);
}
}
if (is_idr) {
// idr_pic_id: ue(v)
- RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
+ slice_reader.ReadExponentialGolomb();
}
// pic_order_cnt_lsb: u(v)
// Represented by sps_.log2_max_pic_order_cnt_lsb bits.
if (sps_->pic_order_cnt_type == 0) {
- RETURN_INV_ON_FAIL(
- slice_reader.ReadBits(&bits_tmp, sps_->log2_max_pic_order_cnt_lsb));
- if (pps_->bottom_field_pic_order_in_frame_present_flag &&
- field_pic_flag == 0) {
+ slice_reader.ConsumeBits(sps_->log2_max_pic_order_cnt_lsb);
+ if (pps_->bottom_field_pic_order_in_frame_present_flag && !field_pic_flag) {
// delta_pic_order_cnt_bottom: se(v)
- RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
+ slice_reader.ReadExponentialGolomb();
}
}
if (sps_->pic_order_cnt_type == 1 &&
!sps_->delta_pic_order_always_zero_flag) {
// delta_pic_order_cnt[0]: se(v)
- RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
+ slice_reader.ReadExponentialGolomb();
if (pps_->bottom_field_pic_order_in_frame_present_flag && !field_pic_flag) {
// delta_pic_order_cnt[1]: se(v)
- RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
+ slice_reader.ReadExponentialGolomb();
}
}
if (pps_->redundant_pic_cnt_present_flag) {
// redundant_pic_cnt: ue(v)
- RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
+ slice_reader.ReadExponentialGolomb();
}
if (slice_type == H264::SliceType::kB) {
// direct_spatial_mv_pred_flag: u(1)
- RETURN_INV_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 1));
+ slice_reader.ConsumeBits(1);
}
switch (slice_type) {
case H264::SliceType::kP:
case H264::SliceType::kB:
case H264::SliceType::kSp:
- uint32_t num_ref_idx_active_override_flag;
// num_ref_idx_active_override_flag: u(1)
- RETURN_INV_ON_FAIL(
- slice_reader.ReadBits(&num_ref_idx_active_override_flag, 1));
- if (num_ref_idx_active_override_flag != 0) {
+ if (slice_reader.Read<bool>()) {
// num_ref_idx_l0_active_minus1: ue(v)
- RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
+ slice_reader.ReadExponentialGolomb();
if (slice_type == H264::SliceType::kB) {
// num_ref_idx_l1_active_minus1: ue(v)
- RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
+ slice_reader.ReadExponentialGolomb();
}
}
break;
default:
break;
}
+ if (!slice_reader.Ok()) {
+ return kInvalidStream;
+ }
// assume nal_unit_type != 20 && nal_unit_type != 21:
if (nalu_type == 20 || nalu_type == 21) {
RTC_LOG(LS_ERROR) << "Unsupported nal unit type.";
@@ -151,54 +137,49 @@ H264BitstreamParser::Result H264BitstreamParser::ParseNonParameterSetNalu(
// else
{
// ref_pic_list_modification():
- // |slice_type| checks here don't use named constants as they aren't named
+ // `slice_type` checks here don't use named constants as they aren't named
// in the spec for this segment. Keeping them consistent makes it easier to
// verify that they are both the same.
if (slice_type % 5 != 2 && slice_type % 5 != 4) {
// ref_pic_list_modification_flag_l0: u(1)
- uint32_t ref_pic_list_modification_flag_l0;
- RETURN_INV_ON_FAIL(
- slice_reader.ReadBits(&ref_pic_list_modification_flag_l0, 1));
- if (ref_pic_list_modification_flag_l0) {
+ if (slice_reader.Read<bool>()) {
uint32_t modification_of_pic_nums_idc;
do {
// modification_of_pic_nums_idc: ue(v)
- RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(
- &modification_of_pic_nums_idc));
+ modification_of_pic_nums_idc = slice_reader.ReadExponentialGolomb();
if (modification_of_pic_nums_idc == 0 ||
modification_of_pic_nums_idc == 1) {
// abs_diff_pic_num_minus1: ue(v)
- RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
+ slice_reader.ReadExponentialGolomb();
} else if (modification_of_pic_nums_idc == 2) {
// long_term_pic_num: ue(v)
- RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
+ slice_reader.ReadExponentialGolomb();
}
- } while (modification_of_pic_nums_idc != 3);
+ } while (modification_of_pic_nums_idc != 3 && slice_reader.Ok());
}
}
if (slice_type % 5 == 1) {
// ref_pic_list_modification_flag_l1: u(1)
- uint32_t ref_pic_list_modification_flag_l1;
- RETURN_INV_ON_FAIL(
- slice_reader.ReadBits(&ref_pic_list_modification_flag_l1, 1));
- if (ref_pic_list_modification_flag_l1) {
+ if (slice_reader.Read<bool>()) {
uint32_t modification_of_pic_nums_idc;
do {
// modification_of_pic_nums_idc: ue(v)
- RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(
- &modification_of_pic_nums_idc));
+ modification_of_pic_nums_idc = slice_reader.ReadExponentialGolomb();
if (modification_of_pic_nums_idc == 0 ||
modification_of_pic_nums_idc == 1) {
// abs_diff_pic_num_minus1: ue(v)
- RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
+ slice_reader.ReadExponentialGolomb();
} else if (modification_of_pic_nums_idc == 2) {
// long_term_pic_num: ue(v)
- RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
+ slice_reader.ReadExponentialGolomb();
}
- } while (modification_of_pic_nums_idc != 3);
+ } while (modification_of_pic_nums_idc != 3 && slice_reader.Ok());
}
}
}
+ if (!slice_reader.Ok()) {
+ return kInvalidStream;
+ }
// TODO(pbos): Do we need support for pred_weight_table()?
if ((pps_->weighted_pred_flag && (slice_type == H264::SliceType::kP ||
slice_type == H264::SliceType::kSp)) ||
@@ -215,49 +196,47 @@ H264BitstreamParser::Result H264BitstreamParser::ParseNonParameterSetNalu(
if (is_idr) {
// no_output_of_prior_pics_flag: u(1)
// long_term_reference_flag: u(1)
- RETURN_INV_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 2));
+ slice_reader.ConsumeBits(2);
} else {
// adaptive_ref_pic_marking_mode_flag: u(1)
- uint32_t adaptive_ref_pic_marking_mode_flag;
- RETURN_INV_ON_FAIL(
- slice_reader.ReadBits(&adaptive_ref_pic_marking_mode_flag, 1));
- if (adaptive_ref_pic_marking_mode_flag) {
+ if (slice_reader.Read<bool>()) {
uint32_t memory_management_control_operation;
do {
// memory_management_control_operation: ue(v)
- RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(
- &memory_management_control_operation));
+ memory_management_control_operation =
+ slice_reader.ReadExponentialGolomb();
if (memory_management_control_operation == 1 ||
memory_management_control_operation == 3) {
// difference_of_pic_nums_minus1: ue(v)
- RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
+ slice_reader.ReadExponentialGolomb();
}
if (memory_management_control_operation == 2) {
// long_term_pic_num: ue(v)
- RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
+ slice_reader.ReadExponentialGolomb();
}
if (memory_management_control_operation == 3 ||
memory_management_control_operation == 6) {
// long_term_frame_idx: ue(v)
- RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
+ slice_reader.ReadExponentialGolomb();
}
if (memory_management_control_operation == 4) {
// max_long_term_frame_idx_plus1: ue(v)
- RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
+ slice_reader.ReadExponentialGolomb();
}
- } while (memory_management_control_operation != 0);
+ } while (memory_management_control_operation != 0 && slice_reader.Ok());
}
}
}
if (pps_->entropy_coding_mode_flag && slice_type != H264::SliceType::kI &&
slice_type != H264::SliceType::kSi) {
// cabac_init_idc: ue(v)
- RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
+ slice_reader.ReadExponentialGolomb();
}
- int32_t last_slice_qp_delta;
- RETURN_INV_ON_FAIL(
- slice_reader.ReadSignedExponentialGolomb(&last_slice_qp_delta));
+ int last_slice_qp_delta = slice_reader.ReadSignedExponentialGolomb();
+ if (!slice_reader.Ok()) {
+ return kInvalidStream;
+ }
if (abs(last_slice_qp_delta) > kMaxAbsQpDeltaValue) {
// Something has gone wrong, and the parsed value is invalid.
RTC_LOG(LS_WARNING) << "Parsed QP value out of range.";
@@ -275,56 +254,46 @@ void H264BitstreamParser::ParseSlice(const uint8_t* slice, size_t length) {
sps_ = SpsParser::ParseSps(slice + H264::kNaluTypeSize,
length - H264::kNaluTypeSize);
if (!sps_)
- RTC_LOG(LS_WARNING) << "Unable to parse SPS from H264 bitstream.";
+ RTC_DLOG(LS_WARNING) << "Unable to parse SPS from H264 bitstream.";
break;
}
case H264::NaluType::kPps: {
pps_ = PpsParser::ParsePps(slice + H264::kNaluTypeSize,
length - H264::kNaluTypeSize);
if (!pps_)
- RTC_LOG(LS_WARNING) << "Unable to parse PPS from H264 bitstream.";
+ RTC_DLOG(LS_WARNING) << "Unable to parse PPS from H264 bitstream.";
break;
}
case H264::NaluType::kAud:
case H264::NaluType::kSei:
+ case H264::NaluType::kPrefix:
break; // Ignore these nalus, as we don't care about their contents.
default:
Result res = ParseNonParameterSetNalu(slice, length, nalu_type);
if (res != kOk)
- RTC_LOG(LS_INFO) << "Failed to parse bitstream. Error: " << res;
+ RTC_DLOG(LS_INFO) << "Failed to parse bitstream. Error: " << res;
break;
}
}
-void H264BitstreamParser::ParseBitstream(const uint8_t* bitstream,
- size_t length) {
+void H264BitstreamParser::ParseBitstream(
+ rtc::ArrayView<const uint8_t> bitstream) {
std::vector<H264::NaluIndex> nalu_indices =
- H264::FindNaluIndices(bitstream, length);
+ H264::FindNaluIndices(bitstream.data(), bitstream.size());
for (const H264::NaluIndex& index : nalu_indices)
- ParseSlice(&bitstream[index.payload_start_offset], index.payload_size);
+ ParseSlice(bitstream.data() + index.payload_start_offset,
+ index.payload_size);
}
-bool H264BitstreamParser::GetLastSliceQp(int* qp) const {
+absl::optional<int> H264BitstreamParser::GetLastSliceQp() const {
if (!last_slice_qp_delta_ || !pps_)
- return false;
- const int parsed_qp = 26 + pps_->pic_init_qp_minus26 + *last_slice_qp_delta_;
- if (parsed_qp < kMinQpValue || parsed_qp > kMaxQpValue) {
+ return absl::nullopt;
+ const int qp = 26 + pps_->pic_init_qp_minus26 + *last_slice_qp_delta_;
+ if (qp < kMinQpValue || qp > kMaxQpValue) {
RTC_LOG(LS_ERROR) << "Parsed invalid QP from bitstream.";
- return false;
+ return absl::nullopt;
}
- *qp = parsed_qp;
- return true;
-}
-
-void H264BitstreamParser::ParseBitstream(
- rtc::ArrayView<const uint8_t> bitstream) {
- ParseBitstream(bitstream.data(), bitstream.size());
-}
-
-absl::optional<int> H264BitstreamParser::GetLastSliceQp() const {
- int qp;
- bool success = GetLastSliceQp(&qp);
- return success ? absl::optional<int>(qp) : absl::nullopt;
+ return qp;
}
} // namespace webrtc