1 /*
2 * Copyright (c) 2019 The WebM 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 #include "gtest/gtest.h"
11 #include "test/codec_factory.h"
12 #include "test/encode_test_driver.h"
13 #include "test/util.h"
14 #include "test/video_source.h"
15 #include "vpx_config.h"
16
17 namespace {
18
19 const int kVideoSourceWidth = 320;
20 const int kVideoSourceHeight = 240;
21 const int kFramesToEncode = 3;
22
23 // A video source that exposes functions to set the timebase, framerate and
24 // starting pts.
25 class DummyTimebaseVideoSource : public ::libvpx_test::DummyVideoSource {
26 public:
27 // Parameters num and den set the timebase for the video source.
DummyTimebaseVideoSource(int num,int den)28 DummyTimebaseVideoSource(int num, int den)
29 : timebase_({ num, den }), framerate_numerator_(30),
30 framerate_denominator_(1), starting_pts_(0) {
31 SetSize(kVideoSourceWidth, kVideoSourceHeight);
32 set_limit(kFramesToEncode);
33 }
34
SetFramerate(int numerator,int denominator)35 void SetFramerate(int numerator, int denominator) {
36 framerate_numerator_ = numerator;
37 framerate_denominator_ = denominator;
38 }
39
40 // Returns one frames duration in timebase units as a double.
FrameDuration() const41 double FrameDuration() const {
42 return (static_cast<double>(timebase_.den) / timebase_.num) /
43 (static_cast<double>(framerate_numerator_) / framerate_denominator_);
44 }
45
pts() const46 vpx_codec_pts_t pts() const override {
47 return static_cast<vpx_codec_pts_t>(frame_ * FrameDuration() +
48 starting_pts_ + 0.5);
49 }
50
duration() const51 unsigned long duration() const override {
52 return static_cast<unsigned long>(FrameDuration() + 0.5);
53 }
54
timebase() const55 vpx_rational_t timebase() const override { return timebase_; }
56
set_starting_pts(int64_t starting_pts)57 void set_starting_pts(int64_t starting_pts) { starting_pts_ = starting_pts; }
58
59 private:
60 vpx_rational_t timebase_;
61 int framerate_numerator_;
62 int framerate_denominator_;
63 int64_t starting_pts_;
64 };
65
66 class TimestampTest
67 : public ::libvpx_test::EncoderTest,
68 public ::libvpx_test::CodecTestWithParam<libvpx_test::TestMode> {
69 protected:
TimestampTest()70 TimestampTest() : EncoderTest(GET_PARAM(0)) {}
71 ~TimestampTest() override = default;
72
SetUp()73 void SetUp() override {
74 InitializeConfig();
75 SetMode(GET_PARAM(1));
76 }
77 };
78
79 // Tests encoding in millisecond timebase.
TEST_P(TimestampTest,EncodeFrames)80 TEST_P(TimestampTest, EncodeFrames) {
81 DummyTimebaseVideoSource video(1, 1000);
82 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
83 }
84
TEST_P(TimestampTest,TestMicrosecondTimebase)85 TEST_P(TimestampTest, TestMicrosecondTimebase) {
86 // Set the timebase to microseconds.
87 DummyTimebaseVideoSource video(1, 1000000);
88 video.set_limit(1);
89 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
90 }
91
TEST_P(TimestampTest,TestVpxRollover)92 TEST_P(TimestampTest, TestVpxRollover) {
93 DummyTimebaseVideoSource video(1, 1000);
94 video.set_starting_pts(922337170351ll);
95 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
96 }
97
98 #if CONFIG_REALTIME_ONLY
99 VP8_INSTANTIATE_TEST_SUITE(TimestampTest,
100 ::testing::Values(::libvpx_test::kRealTime));
101 VP9_INSTANTIATE_TEST_SUITE(TimestampTest,
102 ::testing::Values(::libvpx_test::kRealTime));
103 #else
104 VP8_INSTANTIATE_TEST_SUITE(TimestampTest,
105 ::testing::Values(::libvpx_test::kTwoPassGood));
106 VP9_INSTANTIATE_TEST_SUITE(TimestampTest,
107 ::testing::Values(::libvpx_test::kTwoPassGood));
108 #endif
109 } // namespace
110