From b163359751af17eeb5d5c6e45fb51e9d18bb982f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A1ri=20Tristan=20Helgason?= Date: Fri, 22 Mar 2019 11:19:08 +0100 Subject: [PATCH] Add interface class for bitstream parser. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: webrtc:10439 Change-Id: I0decbbf4aa21a96db50f340f200ccf8adc9e8b53 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/128760 Commit-Queue: Kári Helgason Reviewed-by: Karl Wiberg Cr-Commit-Position: refs/heads/master@{#27237} --- api/video_codecs/BUILD.gn | 10 +++++++ api/video_codecs/bitstream_parser.h | 35 ++++++++++++++++++++++ common_video/BUILD.gn | 1 + common_video/h264/h264_bitstream_parser.cc | 13 ++++++++ common_video/h264/h264_bitstream_parser.h | 26 ++++++++-------- 5 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 api/video_codecs/bitstream_parser.h diff --git a/api/video_codecs/BUILD.gn b/api/video_codecs/BUILD.gn index 24b1298bd2..e219353276 100644 --- a/api/video_codecs/BUILD.gn +++ b/api/video_codecs/BUILD.gn @@ -52,6 +52,16 @@ rtc_source_set("video_codecs_api") { ] } +rtc_source_set("bitstream_parser_api") { + visibility = [ "*" ] + sources = [ + "bitstream_parser.h", + ] + deps = [ + "..:array_view", + ] +} + rtc_static_library("builtin_video_decoder_factory") { visibility = [ "*" ] allow_poison = [ diff --git a/api/video_codecs/bitstream_parser.h b/api/video_codecs/bitstream_parser.h new file mode 100644 index 0000000000..0d8d014d62 --- /dev/null +++ b/api/video_codecs/bitstream_parser.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef API_VIDEO_CODECS_BITSTREAM_PARSER_H_ +#define API_VIDEO_CODECS_BITSTREAM_PARSER_H_ +#include +#include + +#include "api/array_view.h" + +namespace webrtc { + +// This class is an interface for bitstream parsers. +class BitstreamParser { + public: + virtual ~BitstreamParser() = default; + + // Parse an additional chunk of the bitstream. + virtual void ParseBitstream(rtc::ArrayView bitstream) = 0; + + // Get the last extracted QP value from the parsed bitstream. If no QP + // value could be parsed, returns absl::nullopt. + virtual absl::optional GetLastSliceQp() const = 0; +}; + +} // namespace webrtc + +#endif // API_VIDEO_CODECS_BITSTREAM_PARSER_H_ diff --git a/common_video/BUILD.gn b/common_video/BUILD.gn index 2b0712cb1d..fdc586f09f 100644 --- a/common_video/BUILD.gn +++ b/common_video/BUILD.gn @@ -47,6 +47,7 @@ rtc_static_library("common_video") { "../api/video:video_bitrate_allocator", "../api/video:video_frame", "../api/video:video_frame_i420", + "../api/video_codecs:bitstream_parser_api", "../media:rtc_h264_profile_id", "../rtc_base", "../rtc_base:checks", diff --git a/common_video/h264/h264_bitstream_parser.cc b/common_video/h264/h264_bitstream_parser.cc index e71d1cabd7..f1ad84f4db 100644 --- a/common_video/h264/h264_bitstream_parser.cc +++ b/common_video/h264/h264_bitstream_parser.cc @@ -18,9 +18,11 @@ #include "rtc_base/logging.h" namespace { + const int kMaxAbsQpDeltaValue = 51; const int kMinQpValue = 0; const int kMaxQpValue = 51; + } // namespace namespace webrtc { @@ -313,4 +315,15 @@ bool H264BitstreamParser::GetLastSliceQp(int* qp) const { return true; } +void H264BitstreamParser::ParseBitstream( + rtc::ArrayView bitstream) { + ParseBitstream(bitstream.data(), bitstream.size()); +} + +absl::optional H264BitstreamParser::GetLastSliceQp() const { + int qp; + bool success = GetLastSliceQp(&qp); + return success ? absl::optional(qp) : absl::nullopt; +} + } // namespace webrtc diff --git a/common_video/h264/h264_bitstream_parser.h b/common_video/h264/h264_bitstream_parser.h index 962c9c16ad..48190665f0 100644 --- a/common_video/h264/h264_bitstream_parser.h +++ b/common_video/h264/h264_bitstream_parser.h @@ -14,6 +14,7 @@ #include #include "absl/types/optional.h" +#include "api/video_codecs/bitstream_parser.h" #include "common_video/h264/pps_parser.h" #include "common_video/h264/sps_parser.h" @@ -25,24 +26,25 @@ namespace webrtc { // TODO(pbos): If/when this gets used on the receiver side CHECKs must be // removed and gracefully abort as we have no control over receive-side // bitstreams. -class H264BitstreamParser { +class H264BitstreamParser : public BitstreamParser { public: + H264BitstreamParser(); + ~H264BitstreamParser() override; + + // These are here for backwards-compatability for the time being. + void ParseBitstream(const uint8_t* bitstream, size_t length); + bool GetLastSliceQp(int* qp) const; + + // New interface. + void ParseBitstream(rtc::ArrayView bitstream) override; + absl::optional GetLastSliceQp() const override; + + protected: enum Result { kOk, kInvalidStream, kUnsupportedStream, }; - - H264BitstreamParser(); - virtual ~H264BitstreamParser(); - - // Parse an additional chunk of H264 bitstream. - void ParseBitstream(const uint8_t* bitstream, size_t length); - - // Get the last extracted QP value from the parsed bitstream. - bool GetLastSliceQp(int* qp) const; - - protected: void ParseSlice(const uint8_t* slice, size_t length); Result ParseNonParameterSetNalu(const uint8_t* source, size_t source_length,