xref: /aosp_15_r20/external/libaom/test/time_stamp_test.cc (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2019, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker //  Test AOM timestamp handling
13*77c1e3ccSAndroid Build Coastguard Worker 
14*77c1e3ccSAndroid Build Coastguard Worker #include "gtest/gtest.h"
15*77c1e3ccSAndroid Build Coastguard Worker #include "test/codec_factory.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "test/encode_test_driver.h"
17*77c1e3ccSAndroid Build Coastguard Worker #include "test/util.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "test/video_source.h"
19*77c1e3ccSAndroid Build Coastguard Worker 
20*77c1e3ccSAndroid Build Coastguard Worker namespace {
21*77c1e3ccSAndroid Build Coastguard Worker 
22*77c1e3ccSAndroid Build Coastguard Worker const int kVideoSourceWidth = 320;
23*77c1e3ccSAndroid Build Coastguard Worker const int kVideoSourceHeight = 240;
24*77c1e3ccSAndroid Build Coastguard Worker const int kFramesToEncode = 3;
25*77c1e3ccSAndroid Build Coastguard Worker 
26*77c1e3ccSAndroid Build Coastguard Worker // A video source that exposes functions to set the timebase, framerate and
27*77c1e3ccSAndroid Build Coastguard Worker // starting pts.
28*77c1e3ccSAndroid Build Coastguard Worker class DummyTimebaseVideoSource : public ::libaom_test::DummyVideoSource {
29*77c1e3ccSAndroid Build Coastguard Worker  public:
30*77c1e3ccSAndroid Build Coastguard Worker   // Parameters num and den set the timebase for the video source.
DummyTimebaseVideoSource(int num,int den)31*77c1e3ccSAndroid Build Coastguard Worker   DummyTimebaseVideoSource(int num, int den)
32*77c1e3ccSAndroid Build Coastguard Worker       : framerate_numerator_(30), framerate_denominator_(1), starting_pts_(0) {
33*77c1e3ccSAndroid Build Coastguard Worker     SetSize(kVideoSourceWidth, kVideoSourceHeight);
34*77c1e3ccSAndroid Build Coastguard Worker     set_limit(kFramesToEncode);
35*77c1e3ccSAndroid Build Coastguard Worker     timebase_.num = num;
36*77c1e3ccSAndroid Build Coastguard Worker     timebase_.den = den;
37*77c1e3ccSAndroid Build Coastguard Worker   }
38*77c1e3ccSAndroid Build Coastguard Worker 
SetFramerate(int numerator,int denominator)39*77c1e3ccSAndroid Build Coastguard Worker   void SetFramerate(int numerator, int denominator) {
40*77c1e3ccSAndroid Build Coastguard Worker     framerate_numerator_ = numerator;
41*77c1e3ccSAndroid Build Coastguard Worker     framerate_denominator_ = denominator;
42*77c1e3ccSAndroid Build Coastguard Worker   }
43*77c1e3ccSAndroid Build Coastguard Worker 
44*77c1e3ccSAndroid Build Coastguard Worker   // Returns one frames duration in timebase units as a double.
FrameDuration() const45*77c1e3ccSAndroid Build Coastguard Worker   double FrameDuration() const {
46*77c1e3ccSAndroid Build Coastguard Worker     return (static_cast<double>(timebase_.den) / timebase_.num) /
47*77c1e3ccSAndroid Build Coastguard Worker            (static_cast<double>(framerate_numerator_) / framerate_denominator_);
48*77c1e3ccSAndroid Build Coastguard Worker   }
49*77c1e3ccSAndroid Build Coastguard Worker 
pts() const50*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_pts_t pts() const override {
51*77c1e3ccSAndroid Build Coastguard Worker     return static_cast<aom_codec_pts_t>(frame_ * FrameDuration() +
52*77c1e3ccSAndroid Build Coastguard Worker                                         starting_pts_ + 0.5);
53*77c1e3ccSAndroid Build Coastguard Worker   }
54*77c1e3ccSAndroid Build Coastguard Worker 
duration() const55*77c1e3ccSAndroid Build Coastguard Worker   unsigned long duration() const override {
56*77c1e3ccSAndroid Build Coastguard Worker     return static_cast<unsigned long>(FrameDuration() + 0.5);
57*77c1e3ccSAndroid Build Coastguard Worker   }
58*77c1e3ccSAndroid Build Coastguard Worker 
timebase() const59*77c1e3ccSAndroid Build Coastguard Worker   aom_rational_t timebase() const override { return timebase_; }
60*77c1e3ccSAndroid Build Coastguard Worker 
set_starting_pts(int64_t starting_pts)61*77c1e3ccSAndroid Build Coastguard Worker   void set_starting_pts(int64_t starting_pts) { starting_pts_ = starting_pts; }
62*77c1e3ccSAndroid Build Coastguard Worker 
63*77c1e3ccSAndroid Build Coastguard Worker  private:
64*77c1e3ccSAndroid Build Coastguard Worker   aom_rational_t timebase_;
65*77c1e3ccSAndroid Build Coastguard Worker   int framerate_numerator_;
66*77c1e3ccSAndroid Build Coastguard Worker   int framerate_denominator_;
67*77c1e3ccSAndroid Build Coastguard Worker   int64_t starting_pts_;
68*77c1e3ccSAndroid Build Coastguard Worker };
69*77c1e3ccSAndroid Build Coastguard Worker 
70*77c1e3ccSAndroid Build Coastguard Worker class TimestampTest
71*77c1e3ccSAndroid Build Coastguard Worker     : public ::libaom_test::EncoderTest,
72*77c1e3ccSAndroid Build Coastguard Worker       public ::libaom_test::CodecTestWithParam<libaom_test::TestMode> {
73*77c1e3ccSAndroid Build Coastguard Worker  protected:
TimestampTest()74*77c1e3ccSAndroid Build Coastguard Worker   TimestampTest() : EncoderTest(GET_PARAM(0)) {}
75*77c1e3ccSAndroid Build Coastguard Worker   ~TimestampTest() override = default;
76*77c1e3ccSAndroid Build Coastguard Worker 
SetUp()77*77c1e3ccSAndroid Build Coastguard Worker   void SetUp() override { InitializeConfig(GET_PARAM(1)); }
78*77c1e3ccSAndroid Build Coastguard Worker };
79*77c1e3ccSAndroid Build Coastguard Worker 
80*77c1e3ccSAndroid Build Coastguard Worker // Tests encoding in millisecond timebase.
TEST_P(TimestampTest,EncodeFrames)81*77c1e3ccSAndroid Build Coastguard Worker TEST_P(TimestampTest, EncodeFrames) {
82*77c1e3ccSAndroid Build Coastguard Worker   DummyTimebaseVideoSource video(1, 1000);
83*77c1e3ccSAndroid Build Coastguard Worker   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
84*77c1e3ccSAndroid Build Coastguard Worker }
85*77c1e3ccSAndroid Build Coastguard Worker 
TEST_P(TimestampTest,TestMicrosecondTimebase)86*77c1e3ccSAndroid Build Coastguard Worker TEST_P(TimestampTest, TestMicrosecondTimebase) {
87*77c1e3ccSAndroid Build Coastguard Worker   // Set the timebase to microseconds.
88*77c1e3ccSAndroid Build Coastguard Worker   DummyTimebaseVideoSource video(1, 1000000);
89*77c1e3ccSAndroid Build Coastguard Worker   video.set_limit(1);
90*77c1e3ccSAndroid Build Coastguard Worker   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
91*77c1e3ccSAndroid Build Coastguard Worker }
92*77c1e3ccSAndroid Build Coastguard Worker 
TEST_P(TimestampTest,TestAv1Rollover)93*77c1e3ccSAndroid Build Coastguard Worker TEST_P(TimestampTest, TestAv1Rollover) {
94*77c1e3ccSAndroid Build Coastguard Worker   DummyTimebaseVideoSource video(1, 1000);
95*77c1e3ccSAndroid Build Coastguard Worker   video.set_starting_pts(922337170351ll);
96*77c1e3ccSAndroid Build Coastguard Worker   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
97*77c1e3ccSAndroid Build Coastguard Worker }
98*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_REALTIME_ONLY
99*77c1e3ccSAndroid Build Coastguard Worker AV1_INSTANTIATE_TEST_SUITE(TimestampTest,
100*77c1e3ccSAndroid Build Coastguard Worker                            ::testing::Values(::libaom_test::kRealTime));
101*77c1e3ccSAndroid Build Coastguard Worker #else
102*77c1e3ccSAndroid Build Coastguard Worker AV1_INSTANTIATE_TEST_SUITE(TimestampTest,
103*77c1e3ccSAndroid Build Coastguard Worker                            ::testing::Values(::libaom_test::kRealTime,
104*77c1e3ccSAndroid Build Coastguard Worker                                              ::libaom_test::kTwoPassGood));
105*77c1e3ccSAndroid Build Coastguard Worker #endif
106*77c1e3ccSAndroid Build Coastguard Worker 
107*77c1e3ccSAndroid Build Coastguard Worker }  // namespace
108