xref: /aosp_15_r20/external/libaom/test/firstpass_test.cc (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
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 "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, nullptr, 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, nullptr, 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   {
80     // Push the stats when the queue is full.
81     FIRSTPASS_STATS stats;
82     av1_zero(stats);
83     aom_codec_err_t ret = av1_firstpass_info_push(&firstpass_info, &stats);
84     EXPECT_EQ(ret, AOM_CODEC_ERROR);
85   }
86 }
87 
TEST(FirstpassTest,FirstpassInfoTotalStats)88 TEST(FirstpassTest, FirstpassInfoTotalStats) {
89   FIRSTPASS_INFO firstpass_info;
90   av1_firstpass_info_init(&firstpass_info, nullptr, 0);
91   EXPECT_EQ(firstpass_info.total_stats.frame, 0);
92   for (int i = 0; i < 10; ++i) {
93     FIRSTPASS_STATS stats;
94     av1_zero(stats);
95     stats.count = 1;
96     av1_firstpass_info_push(&firstpass_info, &stats);
97   }
98   EXPECT_EQ(firstpass_info.total_stats.count, 10);
99 }
100 
TEST(FirstpassTest,FirstpassInfoMoveCurr)101 TEST(FirstpassTest, FirstpassInfoMoveCurr) {
102   FIRSTPASS_INFO firstpass_info;
103   av1_firstpass_info_init(&firstpass_info, nullptr, 0);
104   int frame_cnt = 0;
105   EXPECT_EQ(firstpass_info.stats_buf_size, FIRSTPASS_INFO_STATIC_BUF_SIZE);
106   for (int i = 0; i < FIRSTPASS_INFO_STATIC_BUF_SIZE; ++i) {
107     FIRSTPASS_STATS stats;
108     av1_zero(stats);
109     stats.frame = frame_cnt;
110     ++frame_cnt;
111     aom_codec_err_t ret = av1_firstpass_info_push(&firstpass_info, &stats);
112     EXPECT_EQ(ret, AOM_CODEC_OK);
113   }
114   EXPECT_EQ(firstpass_info.cur_index, firstpass_info.start_index);
115   {
116     aom_codec_err_t ret = av1_firstpass_info_pop(&firstpass_info);
117     // We cannot pop when cur_index == start_index
118     EXPECT_EQ(ret, AOM_CODEC_ERROR);
119   }
120   int ref_frame_cnt = 0;
121   const int move_count = FIRSTPASS_INFO_STATIC_BUF_SIZE * 2 / 3;
122   for (int i = 0; i < move_count; ++i) {
123     const FIRSTPASS_STATS *this_stats =
124         av1_firstpass_info_peek(&firstpass_info, 0);
125     EXPECT_EQ(this_stats->frame, ref_frame_cnt);
126     ++ref_frame_cnt;
127     av1_firstpass_info_move_cur_index(&firstpass_info);
128   }
129   EXPECT_EQ(firstpass_info.future_stats_count,
130             FIRSTPASS_INFO_STATIC_BUF_SIZE - move_count);
131   EXPECT_EQ(firstpass_info.past_stats_count, move_count);
132   EXPECT_EQ(firstpass_info.stats_count, FIRSTPASS_INFO_STATIC_BUF_SIZE);
133 
134   const int test_count = FIRSTPASS_INFO_STATIC_BUF_SIZE / 2;
135   for (int i = 0; i < test_count; ++i) {
136     aom_codec_err_t ret = av1_firstpass_info_pop(&firstpass_info);
137     EXPECT_EQ(ret, AOM_CODEC_OK);
138   }
139 
140   // Pop #test_count stats
141   for (int i = 0; i < test_count; ++i) {
142     FIRSTPASS_STATS stats;
143     av1_zero(stats);
144     stats.frame = frame_cnt;
145     ++frame_cnt;
146     aom_codec_err_t ret = av1_firstpass_info_push(&firstpass_info, &stats);
147     EXPECT_EQ(ret, AOM_CODEC_OK);
148   }
149 
150   // peek and move #test_count stats
151   for (int i = 0; i < test_count; ++i) {
152     const FIRSTPASS_STATS *this_stats =
153         av1_firstpass_info_peek(&firstpass_info, 0);
154     EXPECT_EQ(this_stats->frame, ref_frame_cnt);
155     ++ref_frame_cnt;
156     av1_firstpass_info_move_cur_index(&firstpass_info);
157   }
158 
159   // pop #test_count stats
160   for (int i = 0; i < test_count; ++i) {
161     aom_codec_err_t ret = av1_firstpass_info_pop(&firstpass_info);
162     EXPECT_EQ(ret, AOM_CODEC_OK);
163   }
164 }
165 
166 }  // namespace
167