diff --git a/BUILD.gn b/BUILD.gn index 168dcf9bad..e60a909290 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -264,6 +264,10 @@ config("common_config") { defines += [ "RTC_ENABLE_VP9" ] } + if (rtc_include_dav1d_in_internal_decoder_factory) { + defines += [ "RTC_DAV1D_IN_INTERNAL_DECODER_FACTORY" ] + } + if (rtc_enable_sctp) { defines += [ "WEBRTC_HAVE_SCTP" ] } diff --git a/DEPS b/DEPS index 64591e015a..550a089437 100644 --- a/DEPS +++ b/DEPS @@ -219,6 +219,8 @@ deps = { 'https://chromium.googlesource.com/chromium/deps/libjpeg_turbo.git@49836d72bd22c7a78bc0250483f04162278cdc6a', 'src/third_party/libsrtp': 'https://chromium.googlesource.com/chromium/deps/libsrtp.git@5b7c744eb8310250ccc534f3f86a2015b3887a0a', + 'src/third_party/dav1d/libdav1d': + 'https://chromium.googlesource.com/external/github.com/videolan/dav1d.git@692c0ce873d7d823f2255968e32b233d71d88b43', 'src/third_party/libaom/source/libaom': 'https://aomedia.googlesource.com/aom.git@b5719d38f3eb67e405b9fd7c90945f0a7ece10c0', 'src/third_party/libunwindstack': { diff --git a/media/BUILD.gn b/media/BUILD.gn index ffbb4f0907..89a5d46a57 100644 --- a/media/BUILD.gn +++ b/media/BUILD.gn @@ -215,8 +215,12 @@ rtc_library("rtc_internal_video_codecs") { "../rtc_base:checks", "../rtc_base:rtc_base_approved", "../rtc_base/system:rtc_export", + "../system_wrappers:field_trial", "../test:fake_video_codecs", ] + if (rtc_include_dav1d_in_internal_decoder_factory) { + deps += [ "../modules/video_coding/codecs/av1:dav1d_decoder" ] + } absl_deps = [ "//third_party/abseil-cpp/absl/strings", "//third_party/abseil-cpp/absl/types:optional", diff --git a/media/engine/internal_decoder_factory.cc b/media/engine/internal_decoder_factory.cc index a9f30c2cd6..faac91e70f 100644 --- a/media/engine/internal_decoder_factory.cc +++ b/media/engine/internal_decoder_factory.cc @@ -21,8 +21,25 @@ #include "modules/video_coding/codecs/vp9/include/vp9.h" #include "rtc_base/checks.h" #include "rtc_base/logging.h" +#include "system_wrappers/include/field_trial.h" + +#if defined(RTC_DAV1D_IN_INTERNAL_DECODER_FACTORY) +#include "modules/video_coding/codecs/av1/dav1d_decoder.h" +#endif namespace webrtc { +namespace { +constexpr char kDav1dFieldTrial[] = "WebRTC-Dav1dDecoder"; +#if defined(RTC_DAV1D_IN_INTERNAL_DECODER_FACTORY) +constexpr bool kDav1dIsIncluded = true; +#else +constexpr bool kDav1dIsIncluded = false; +std::unique_ptr CreateDav1dDecoder() { + return nullptr; +} +#endif + +} // namespace std::vector InternalDecoderFactory::GetSupportedFormats() const { @@ -32,8 +49,12 @@ std::vector InternalDecoderFactory::GetSupportedFormats() formats.push_back(format); for (const SdpVideoFormat& h264_format : SupportedH264Codecs()) formats.push_back(h264_format); - if (kIsLibaomAv1DecoderSupported) + + if (kIsLibaomAv1DecoderSupported || + (kDav1dIsIncluded && field_trial::IsEnabled(kDav1dFieldTrial))) { formats.push_back(SdpVideoFormat(cricket::kAv1CodecName)); + } + return formats; } @@ -69,9 +90,16 @@ std::unique_ptr InternalDecoderFactory::CreateVideoDecoder( return VP9Decoder::Create(); if (absl::EqualsIgnoreCase(format.name, cricket::kH264CodecName)) return H264Decoder::Create(); - if (kIsLibaomAv1DecoderSupported && - absl::EqualsIgnoreCase(format.name, cricket::kAv1CodecName)) + + if (absl::EqualsIgnoreCase(format.name, cricket::kAv1CodecName) && + kDav1dIsIncluded && field_trial::IsEnabled(kDav1dFieldTrial)) { + return CreateDav1dDecoder(); + } + + if (absl::EqualsIgnoreCase(format.name, cricket::kAv1CodecName) && + kIsLibaomAv1DecoderSupported) { return CreateLibaomAv1Decoder(); + } RTC_DCHECK_NOTREACHED(); return nullptr; diff --git a/modules/video_coding/BUILD.gn b/modules/video_coding/BUILD.gn index 1a5ebd6081..eb65533b2b 100644 --- a/modules/video_coding/BUILD.gn +++ b/modules/video_coding/BUILD.gn @@ -915,6 +915,7 @@ if (rtc_include_tests) { "codecs/h264/test/h264_impl_unittest.cc", "codecs/multiplex/test/multiplex_adapter_unittest.cc", "codecs/test/video_encoder_decoder_instantiation_tests.cc", + "codecs/test/videocodec_test_av1.cc", "codecs/test/videocodec_test_libvpx.cc", "codecs/vp8/test/vp8_impl_unittest.cc", ] @@ -923,10 +924,6 @@ if (rtc_include_tests) { sources += [ "codecs/vp9/test/vp9_impl_unittest.cc" ] } - # TODO(jianj): Fix crash on iOS and re-enable - if (enable_libaom && !is_ios) { - sources += [ "codecs/test/videocodec_test_libaom.cc" ] - } if (rtc_use_h264) { sources += [ "codecs/test/videocodec_test_openh264.cc" ] } @@ -970,6 +967,8 @@ if (rtc_include_tests) { "../../test:test_support", "../../test:video_test_common", "../rtp_rtcp:rtp_rtcp_format", + "codecs/av1:dav1d_decoder", + "codecs/av1:libaom_av1_decoder", "//third_party/libyuv", ] absl_deps = [ "//third_party/abseil-cpp/absl/types:optional" ] diff --git a/modules/video_coding/codecs/av1/BUILD.gn b/modules/video_coding/codecs/av1/BUILD.gn index 8162d359eb..7106ca4809 100644 --- a/modules/video_coding/codecs/av1/BUILD.gn +++ b/modules/video_coding/codecs/av1/BUILD.gn @@ -23,6 +23,25 @@ rtc_library("av1_svc_config") { ] } +rtc_library("dav1d_decoder") { + poisonous = [ "software_video_codecs" ] + public = [ "dav1d_decoder.h" ] + sources = [ "dav1d_decoder.cc" ] + + deps = [ + "../..:video_codec_interface", + "../../../../api:scoped_refptr", + "../../../../api/video:encoded_image", + "../../../../api/video:video_frame", + "../../../../api/video_codecs:video_codecs_api", + "../../../../common_video", + "../../../../rtc_base:logging", + "//third_party/dav1d", + "//third_party/libyuv", + ] + absl_deps = [ "//third_party/abseil-cpp/absl/types:optional" ] +} + rtc_library("libaom_av1_decoder") { visibility = [ "*" ] poisonous = [ "software_video_codecs" ] diff --git a/modules/video_coding/codecs/av1/DEPS b/modules/video_coding/codecs/av1/DEPS index 25779919a7..bfb1c733d4 100644 --- a/modules/video_coding/codecs/av1/DEPS +++ b/modules/video_coding/codecs/av1/DEPS @@ -1,3 +1,4 @@ include_rules = [ "+third_party/libaom", + "+third_party/dav1d", ] diff --git a/modules/video_coding/codecs/av1/dav1d_decoder.cc b/modules/video_coding/codecs/av1/dav1d_decoder.cc new file mode 100644 index 0000000000..a5e4784839 --- /dev/null +++ b/modules/video_coding/codecs/av1/dav1d_decoder.cc @@ -0,0 +1,197 @@ +/* + * Copyright (c) 2021 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 "modules/video_coding/codecs/av1/dav1d_decoder.h" + +#include + +#include "api/scoped_refptr.h" +#include "api/video/encoded_image.h" +#include "api/video/i420_buffer.h" +#include "common_video/include/video_frame_buffer_pool.h" +#include "modules/video_coding/include/video_error_codes.h" +#include "rtc_base/logging.h" +#include "third_party/dav1d/libdav1d/include/dav1d/dav1d.h" +#include "third_party/libyuv/include/libyuv/convert.h" + +namespace webrtc { +namespace { + +class Dav1dDecoder : public VideoDecoder { + public: + Dav1dDecoder(); + Dav1dDecoder(const Dav1dDecoder&) = delete; + Dav1dDecoder& operator=(const Dav1dDecoder&) = delete; + + ~Dav1dDecoder() override; + + bool Configure(const Settings& settings) override; + int32_t Decode(const EncodedImage& encoded_image, + bool missing_frames, + int64_t render_time_ms) override; + int32_t RegisterDecodeCompleteCallback( + DecodedImageCallback* callback) override; + int32_t Release() override; + DecoderInfo GetDecoderInfo() const override; + const char* ImplementationName() const override; + + private: + VideoFrameBufferPool buffer_pool_; + Dav1dContext* context_ = nullptr; + DecodedImageCallback* decode_complete_callback_ = nullptr; +}; + +class ScopedDav1dData { + public: + ~ScopedDav1dData() { dav1d_data_unref(&data_); } + + Dav1dData& Data() { return data_; } + + private: + Dav1dData data_ = {}; +}; + +class ScopedDav1dPicture { + public: + ~ScopedDav1dPicture() { dav1d_picture_unref(&picture_); } + + Dav1dPicture& Picture() { return picture_; } + + private: + Dav1dPicture picture_ = {}; +}; + +constexpr char kDav1dName[] = "dav1d"; + +// Calling `dav1d_data_wrap` requires a `free_callback` to be registered. +void NullFreeCallback(const uint8_t* buffer, void* opaque) {} + +Dav1dDecoder::Dav1dDecoder() + : buffer_pool_(/*zero_initialize=*/false, /*max_number_of_buffers=*/150) {} + +Dav1dDecoder::~Dav1dDecoder() { + Release(); +} + +bool Dav1dDecoder::Configure(const Settings& settings) { + Dav1dSettings s; + dav1d_default_settings(&s); + + s.n_threads = std::max(2, settings.number_of_cores()); + s.max_frame_delay = 1; // For low latency decoding. + s.all_layers = 0; // Don't output a frame for every spatial layer. + s.operating_point = 31; // Decode all operating points. + + return dav1d_open(&context_, &s) == 0; +} + +int32_t Dav1dDecoder::RegisterDecodeCompleteCallback( + DecodedImageCallback* decode_complete_callback) { + decode_complete_callback_ = decode_complete_callback; + return WEBRTC_VIDEO_CODEC_OK; +} + +int32_t Dav1dDecoder::Release() { + dav1d_close(&context_); + if (context_ != nullptr) { + return WEBRTC_VIDEO_CODEC_MEMORY; + } + buffer_pool_.Release(); + return WEBRTC_VIDEO_CODEC_OK; +} + +VideoDecoder::DecoderInfo Dav1dDecoder::GetDecoderInfo() const { + DecoderInfo info; + info.implementation_name = kDav1dName; + info.is_hardware_accelerated = false; + return info; +} + +const char* Dav1dDecoder::ImplementationName() const { + return kDav1dName; +} + +int32_t Dav1dDecoder::Decode(const EncodedImage& encoded_image, + bool /*missing_frames*/, + int64_t /*render_time_ms*/) { + if (!context_ || decode_complete_callback_ == nullptr) { + return WEBRTC_VIDEO_CODEC_UNINITIALIZED; + } + + ScopedDav1dData scoped_dav1d_data; + Dav1dData& dav1d_data = scoped_dav1d_data.Data(); + dav1d_data_wrap(&dav1d_data, encoded_image.data(), encoded_image.size(), + /*free_callback=*/&NullFreeCallback, + /*user_data=*/nullptr); + + if (int decode_res = dav1d_send_data(context_, &dav1d_data)) { + RTC_LOG(LS_WARNING) + << "Dav1dDecoder::Decode decoding failed with error code " + << decode_res; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + ScopedDav1dPicture scoped_dav1d_picture; + Dav1dPicture& dav1d_picture = scoped_dav1d_picture.Picture(); + if (int get_picture_res = dav1d_get_picture(context_, &dav1d_picture)) { + RTC_LOG(LS_WARNING) + << "Dav1dDecoder::Decode getting picture failed with error code " + << get_picture_res; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + // Only accept I420 pixel format and 8 bit depth. + if (dav1d_picture.p.layout != DAV1D_PIXEL_LAYOUT_I420 || + dav1d_picture.p.bpc != 8) { + return WEBRTC_VIDEO_CODEC_ERROR; + } + + rtc::scoped_refptr buffer = + buffer_pool_.CreateI420Buffer(dav1d_picture.p.w, dav1d_picture.p.h); + if (!buffer.get()) { + RTC_LOG(LS_WARNING) + << "Dav1dDecoder::Decode failed to get frame from the buffer pool."; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + uint8_t* y_data = static_cast(dav1d_picture.data[0]); + uint8_t* u_data = static_cast(dav1d_picture.data[1]); + uint8_t* v_data = static_cast(dav1d_picture.data[2]); + int y_stride = dav1d_picture.stride[0]; + int uv_stride = dav1d_picture.stride[1]; + libyuv::I420Copy(y_data, y_stride, // + u_data, uv_stride, // + v_data, uv_stride, // + buffer->MutableDataY(), buffer->StrideY(), // + buffer->MutableDataU(), buffer->StrideU(), // + buffer->MutableDataV(), buffer->StrideV(), // + dav1d_picture.p.w, // + dav1d_picture.p.h); // + + VideoFrame decoded_frame = VideoFrame::Builder() + .set_video_frame_buffer(buffer) + .set_timestamp_rtp(encoded_image.Timestamp()) + .set_ntp_time_ms(encoded_image.ntp_time_ms_) + .set_color_space(encoded_image.ColorSpace()) + .build(); + + decode_complete_callback_->Decoded(decoded_frame, absl::nullopt, + absl::nullopt); + + return WEBRTC_VIDEO_CODEC_OK; +} + +} // namespace + +std::unique_ptr CreateDav1dDecoder() { + return std::make_unique(); +} + +} // namespace webrtc diff --git a/modules/video_coding/codecs/av1/dav1d_decoder.h b/modules/video_coding/codecs/av1/dav1d_decoder.h new file mode 100644 index 0000000000..c9396d1e03 --- /dev/null +++ b/modules/video_coding/codecs/av1/dav1d_decoder.h @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021 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 MODULES_VIDEO_CODING_CODECS_AV1_DAV1D_DECODER_H_ +#define MODULES_VIDEO_CODING_CODECS_AV1_DAV1D_DECODER_H_ + +#include + +#include "api/video_codecs/video_decoder.h" + +namespace webrtc { + +std::unique_ptr CreateDav1dDecoder(); + +} // namespace webrtc + +#endif // MODULES_VIDEO_CODING_CODECS_AV1_DAV1D_DECODER_H_ diff --git a/modules/video_coding/codecs/test/videocodec_test_libaom.cc b/modules/video_coding/codecs/test/videocodec_test_av1.cc similarity index 77% rename from modules/video_coding/codecs/test/videocodec_test_libaom.cc rename to modules/video_coding/codecs/test/videocodec_test_av1.cc index c3263e7134..03ea1b7ec0 100644 --- a/modules/video_coding/codecs/test/videocodec_test_libaom.cc +++ b/modules/video_coding/codecs/test/videocodec_test_av1.cc @@ -18,6 +18,8 @@ #include "media/engine/internal_decoder_factory.h" #include "media/engine/internal_encoder_factory.h" #include "media/engine/simulcast_encoder_adapter.h" +#include "modules/video_coding/codecs/av1/libaom_av1_decoder.h" +#include "test/field_trial.h" #include "test/gtest.h" #include "test/testsupport/file_utils.h" @@ -38,7 +40,15 @@ VideoCodecTestFixture::Config CreateConfig(std::string filename) { return config; } -TEST(VideoCodecTestLibaom, HighBitrateAV1) { +class VideoCodecTestAv1 : public ::testing::TestWithParam { + public: + VideoCodecTestAv1() : scoped_field_trial_(GetParam()) {} + + private: + ScopedFieldTrials scoped_field_trial_; +}; + +TEST_P(VideoCodecTestAv1, HighBitrate) { auto config = CreateConfig("foreman_cif"); config.SetCodecSettings(cricket::kAv1CodecName, 1, 1, 1, false, true, true, kCifWidth, kCifHeight); @@ -56,7 +66,7 @@ TEST(VideoCodecTestLibaom, HighBitrateAV1) { fixture->RunTest(rate_profiles, &rc_thresholds, &quality_thresholds, nullptr); } -TEST(VideoCodecTestLibaom, VeryLowBitrateAV1) { +TEST_P(VideoCodecTestAv1, VeryLowBitrate) { auto config = CreateConfig("foreman_cif"); config.SetCodecSettings(cricket::kAv1CodecName, 1, 1, 1, false, true, true, kCifWidth, kCifHeight); @@ -76,7 +86,7 @@ TEST(VideoCodecTestLibaom, VeryLowBitrateAV1) { #if !defined(WEBRTC_ANDROID) constexpr int kHdWidth = 1280; constexpr int kHdHeight = 720; -TEST(VideoCodecTestLibaom, HdAV1) { +TEST_P(VideoCodecTestAv1, Hd) { auto config = CreateConfig("ConferenceMotion_1280_720_50"); config.SetCodecSettings(cricket::kAv1CodecName, 1, 1, 1, false, true, true, kHdWidth, kHdHeight); @@ -95,6 +105,23 @@ TEST(VideoCodecTestLibaom, HdAV1) { } #endif +std::vector GetTestValues() { + std::vector field_trial_values; +#if defined(RTC_DAV1D_IN_INTERNAL_DECODER_FACTORY) + field_trial_values.push_back("WebRTC-Dav1dDecoder/Enabled/"); +#endif + if (kIsLibaomAv1DecoderSupported) { + // As long as the field trial doesn't enable dav1d the libaom decoder will + // be used instead. + field_trial_values.push_back(""); + } + return field_trial_values; +} + +INSTANTIATE_TEST_SUITE_P(Decoder, + VideoCodecTestAv1, + testing::ValuesIn(GetTestValues())); + } // namespace } // namespace test } // namespace webrtc diff --git a/webrtc.gni b/webrtc.gni index 6baf175fea..15ddba0e36 100644 --- a/webrtc.gni +++ b/webrtc.gni @@ -224,6 +224,9 @@ declare_args() { # Windows.Graphics.Capture APIs will be available for use. This introduces a # dependency on the Win 10 SDK v10.0.17763.0. rtc_enable_win_wgc = is_win + + # Includes the dav1d decoder in the internal decoder factory when set to true. + rtc_include_dav1d_in_internal_decoder_factory = true } if (!build_with_mozilla) {