1 /*
2 * Copyright (c) 2021, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #include <stddef.h>
13
14 #include "av1/common/common.h"
15 #include "av1/encoder/firstpass.h"
16 #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
17
18 namespace {
19
TEST(FirstpassTest,FirstpassInfoInitWithExtBuf)20 TEST(FirstpassTest, FirstpassInfoInitWithExtBuf) {
21 FIRSTPASS_INFO firstpass_info;
22 FIRSTPASS_STATS ext_stats_buf[10];
23 const int ref_stats_size = 10;
24 for (int i = 0; i < ref_stats_size; ++i) {
25 av1_zero(ext_stats_buf[i]);
26 ext_stats_buf[i].frame = i;
27 }
28 aom_codec_err_t ret =
29 av1_firstpass_info_init(&firstpass_info, ext_stats_buf, 10);
30 EXPECT_EQ(firstpass_info.stats_count, ref_stats_size);
31 EXPECT_EQ(firstpass_info.future_stats_count + firstpass_info.past_stats_count,
32 firstpass_info.stats_count);
33 EXPECT_EQ(firstpass_info.cur_index, 0);
34 EXPECT_EQ(ret, AOM_CODEC_OK);
35 }
36
TEST(FirstpassTest,FirstpassInfoInitWithStaticBuf)37 TEST(FirstpassTest, FirstpassInfoInitWithStaticBuf) {
38 FIRSTPASS_INFO firstpass_info;
39 aom_codec_err_t ret = av1_firstpass_info_init(&firstpass_info, NULL, 0);
40 EXPECT_EQ(firstpass_info.stats_count, 0);
41 EXPECT_EQ(firstpass_info.cur_index, 0);
42 EXPECT_EQ(ret, AOM_CODEC_OK);
43 }
44
TEST(FirstpassTest,FirstpassInfoPushPop)45 TEST(FirstpassTest, FirstpassInfoPushPop) {
46 FIRSTPASS_INFO firstpass_info;
47 av1_firstpass_info_init(&firstpass_info, NULL, 0);
48 EXPECT_EQ(firstpass_info.stats_buf_size, FIRSTPASS_INFO_STATIC_BUF_SIZE);
49 for (int i = 0; i < FIRSTPASS_INFO_STATIC_BUF_SIZE; ++i) {
50 FIRSTPASS_STATS stats;
51 av1_zero(stats);
52 stats.frame = i;
53 aom_codec_err_t ret = av1_firstpass_info_push(&firstpass_info, &stats);
54 EXPECT_EQ(ret, AOM_CODEC_OK);
55 }
56 EXPECT_EQ(firstpass_info.stats_count, FIRSTPASS_INFO_STATIC_BUF_SIZE);
57 const int pop_count = FIRSTPASS_INFO_STATIC_BUF_SIZE / 2;
58 for (int i = 0; i < pop_count; ++i) {
59 const FIRSTPASS_STATS *stats = av1_firstpass_info_peek(&firstpass_info, 0);
60 aom_codec_err_t ret =
61 av1_firstpass_info_move_cur_index_and_pop(&firstpass_info);
62 EXPECT_NE(stats, nullptr);
63 EXPECT_EQ(stats->frame, i);
64 EXPECT_EQ(ret, AOM_CODEC_OK);
65 }
66 EXPECT_EQ(firstpass_info.stats_count,
67 FIRSTPASS_INFO_STATIC_BUF_SIZE - pop_count);
68
69 const int push_count = FIRSTPASS_INFO_STATIC_BUF_SIZE / 2;
70 for (int i = 0; i < push_count; ++i) {
71 FIRSTPASS_STATS stats;
72 av1_zero(stats);
73 aom_codec_err_t ret = av1_firstpass_info_push(&firstpass_info, &stats);
74 EXPECT_EQ(ret, AOM_CODEC_OK);
75 }
76 EXPECT_EQ(firstpass_info.stats_count, FIRSTPASS_INFO_STATIC_BUF_SIZE);
77
78 EXPECT_EQ(firstpass_info.stats_count, firstpass_info.stats_buf_size);
79 // Push the stats when the queue is full.
80 FIRSTPASS_STATS stats;
81 av1_zero(stats);
82 aom_codec_err_t ret = av1_firstpass_info_push(&firstpass_info, &stats);
83 EXPECT_EQ(ret, AOM_CODEC_ERROR);
84 }
85
TEST(FirstpassTest,FirstpassInfoTotalStats)86 TEST(FirstpassTest, FirstpassInfoTotalStats) {
87 FIRSTPASS_INFO firstpass_info;
88 av1_firstpass_info_init(&firstpass_info, NULL, 0);
89 EXPECT_EQ(firstpass_info.total_stats.frame, 0);
90 for (int i = 0; i < 10; ++i) {
91 FIRSTPASS_STATS stats;
92 av1_zero(stats);
93 stats.count = 1;
94 av1_firstpass_info_push(&firstpass_info, &stats);
95 }
96 EXPECT_EQ(firstpass_info.total_stats.count, 10);
97 }
98
TEST(FirstpassTest,FirstpassInfoMoveCurr)99 TEST(FirstpassTest, FirstpassInfoMoveCurr) {
100 FIRSTPASS_INFO firstpass_info;
101 av1_firstpass_info_init(&firstpass_info, NULL, 0);
102 int frame_cnt = 0;
103 EXPECT_EQ(firstpass_info.stats_buf_size, FIRSTPASS_INFO_STATIC_BUF_SIZE);
104 for (int i = 0; i < FIRSTPASS_INFO_STATIC_BUF_SIZE; ++i) {
105 FIRSTPASS_STATS stats;
106 av1_zero(stats);
107 stats.frame = frame_cnt;
108 ++frame_cnt;
109 aom_codec_err_t ret = av1_firstpass_info_push(&firstpass_info, &stats);
110 EXPECT_EQ(ret, AOM_CODEC_OK);
111 }
112 EXPECT_EQ(firstpass_info.cur_index, firstpass_info.start_index);
113 aom_codec_err_t ret = av1_firstpass_info_pop(&firstpass_info);
114 // We cannot pop when cur_index == start_index
115 EXPECT_EQ(ret, AOM_CODEC_ERROR);
116 int ref_frame_cnt = 0;
117 const int move_count = FIRSTPASS_INFO_STATIC_BUF_SIZE * 2 / 3;
118 for (int i = 0; i < move_count; ++i) {
119 const FIRSTPASS_STATS *this_stats =
120 av1_firstpass_info_peek(&firstpass_info, 0);
121 EXPECT_EQ(this_stats->frame, ref_frame_cnt);
122 ++ref_frame_cnt;
123 av1_firstpass_info_move_cur_index(&firstpass_info);
124 }
125 EXPECT_EQ(firstpass_info.future_stats_count,
126 FIRSTPASS_INFO_STATIC_BUF_SIZE - move_count);
127 EXPECT_EQ(firstpass_info.past_stats_count, move_count);
128 EXPECT_EQ(firstpass_info.stats_count, FIRSTPASS_INFO_STATIC_BUF_SIZE);
129
130 const int test_count = FIRSTPASS_INFO_STATIC_BUF_SIZE / 2;
131 for (int i = 0; i < test_count; ++i) {
132 aom_codec_err_t ret = av1_firstpass_info_pop(&firstpass_info);
133 EXPECT_EQ(ret, AOM_CODEC_OK);
134 }
135
136 // Pop #test_count stats
137 for (int i = 0; i < test_count; ++i) {
138 FIRSTPASS_STATS stats;
139 av1_zero(stats);
140 stats.frame = frame_cnt;
141 ++frame_cnt;
142 aom_codec_err_t ret = av1_firstpass_info_push(&firstpass_info, &stats);
143 EXPECT_EQ(ret, AOM_CODEC_OK);
144 }
145
146 // peek and move #test_count stats
147 for (int i = 0; i < test_count; ++i) {
148 const FIRSTPASS_STATS *this_stats =
149 av1_firstpass_info_peek(&firstpass_info, 0);
150 EXPECT_EQ(this_stats->frame, ref_frame_cnt);
151 ++ref_frame_cnt;
152 av1_firstpass_info_move_cur_index(&firstpass_info);
153 }
154
155 // pop #test_count stats
156 for (int i = 0; i < test_count; ++i) {
157 aom_codec_err_t ret = av1_firstpass_info_pop(&firstpass_info);
158 EXPECT_EQ(ret, AOM_CODEC_OK);
159 }
160 }
161
162 } // namespace
163