tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

dav1d_decoder_unittest.cc (4113B)


      1 /*
      2 *  Copyright (c) 2025 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 "modules/video_coding/codecs/av1/dav1d_decoder.h"
     12 
     13 #include <cstdint>
     14 #include <memory>
     15 #include <optional>
     16 #include <utility>
     17 
     18 #include "api/array_view.h"
     19 #include "api/environment/environment.h"
     20 #include "api/environment/environment_factory.h"
     21 #include "api/video/encoded_image.h"
     22 #include "api/video/video_frame.h"
     23 #include "api/video_codecs/video_decoder.h"
     24 #include "modules/video_coding/include/video_error_codes.h"
     25 #include "test/create_test_field_trials.h"
     26 #include "test/gmock.h"
     27 #include "test/gtest.h"
     28 
     29 namespace webrtc {
     30 namespace test {
     31 namespace {
     32 
     33 using ::testing::Eq;
     34 using ::testing::Not;
     35 using ::testing::NotNull;
     36 
     37 constexpr uint8_t kAv1FrameWith36x20EncodededAnd32x16RenderResolution[] = {
     38    0x12, 0x00, 0x0a, 0x06, 0x18, 0x15, 0x23, 0x9f, 0x60, 0x10, 0x32, 0x18,
     39    0x20, 0x03, 0xe0, 0x01, 0xf2, 0xb0, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
     40    0x00, 0xf2, 0x44, 0xd6, 0xa5, 0x3b, 0x7c, 0x8b, 0x7c, 0x8c, 0x6b, 0x9a};
     41 
     42 EncodedImage CreateEncodedImage(ArrayView<const uint8_t> data) {
     43  EncodedImage image;
     44  image.SetEncodedData(EncodedImageBuffer::Create(data.data(), data.size()));
     45  return image;
     46 }
     47 
     48 class TestAv1Decoder : public DecodedImageCallback {
     49 public:
     50  explicit TestAv1Decoder(const Environment& env)
     51      : decoder_(CreateDav1dDecoder(env)) {
     52    if (decoder_ == nullptr) {
     53      ADD_FAILURE() << "Failed to create decoder";
     54      return;
     55    }
     56    EXPECT_TRUE(decoder_->Configure({}));
     57    EXPECT_EQ(decoder_->RegisterDecodeCompleteCallback(this),
     58              WEBRTC_VIDEO_CODEC_OK);
     59  }
     60  // This class requires pointer stability and thus not copyable nor movable.
     61  TestAv1Decoder(const TestAv1Decoder&) = delete;
     62  TestAv1Decoder& operator=(const TestAv1Decoder&) = delete;
     63 
     64  void Decode(const EncodedImage& image) {
     65    ASSERT_THAT(decoder_, NotNull());
     66    decoded_frame_ = std::nullopt;
     67    int32_t error =
     68        decoder_->Decode(image, /*render_time_ms=*/image.capture_time_ms_);
     69    ASSERT_EQ(error, WEBRTC_VIDEO_CODEC_OK);
     70    ASSERT_THAT(decoded_frame_, Not(Eq(std::nullopt)));
     71  }
     72 
     73  VideoFrame& decoded_frame() { return *decoded_frame_; }
     74 
     75 private:
     76  int32_t Decoded(VideoFrame& decoded_frame) override {
     77    decoded_frame_ = std::move(decoded_frame);
     78    return 0;
     79  }
     80  void Decoded(VideoFrame& decoded_frame,
     81               std::optional<int32_t> /*decode_time_ms*/,
     82               std::optional<uint8_t> /*qp*/) override {
     83    Decoded(decoded_frame);
     84  }
     85 
     86  const std::unique_ptr<VideoDecoder> decoder_;
     87  std::optional<VideoFrame> decoded_frame_;
     88 };
     89 
     90 TEST(Dav1dDecoderTest, KeepsDecodedResolutionByDefault) {
     91  TestAv1Decoder decoder(CreateEnvironment());
     92  decoder.Decode(
     93      CreateEncodedImage(kAv1FrameWith36x20EncodededAnd32x16RenderResolution));
     94  EXPECT_EQ(decoder.decoded_frame().width(), 36);
     95  EXPECT_EQ(decoder.decoded_frame().height(), 20);
     96 }
     97 
     98 TEST(Dav1dDecoderTest, CropsToRenderResolutionWhenCropIsEnabled) {
     99  TestAv1Decoder decoder(CreateEnvironment(CreateTestFieldTrialsPtr(
    100      "WebRTC-Dav1dDecoder-CropToRenderResolution/Enabled/")));
    101  decoder.Decode(
    102      CreateEncodedImage(kAv1FrameWith36x20EncodededAnd32x16RenderResolution));
    103  EXPECT_EQ(decoder.decoded_frame().width(), 32);
    104  EXPECT_EQ(decoder.decoded_frame().height(), 16);
    105 }
    106 
    107 TEST(Dav1dDecoderTest, DoesNotCropToRenderResolutionWhenCropIsDisabled) {
    108  TestAv1Decoder decoder(CreateEnvironment(CreateTestFieldTrialsPtr(
    109      "WebRTC-Dav1dDecoder-CropToRenderResolution/Disabled/")));
    110  decoder.Decode(
    111      CreateEncodedImage(kAv1FrameWith36x20EncodededAnd32x16RenderResolution));
    112  EXPECT_EQ(decoder.decoded_frame().width(), 36);
    113  EXPECT_EQ(decoder.decoded_frame().height(), 20);
    114 }
    115 
    116 }  // namespace
    117 }  // namespace test
    118 }  // namespace webrtc