xref: /aosp_15_r20/external/libaom/test/force_key_frame_test.cc (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1 /*
2  * Copyright (c) 2022, 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 // Tests for https://crbug.com/aomedia/3327.
13 //
14 // In good-quality mode, set cfg.g_lag_in_frames to 1 or 0 and encode two
15 // frames in one-pass mode. Pass AOM_EFLAG_FORCE_KF to the second
16 // aom_codec_encode() call. Both frames should be encoded as key frames.
17 
18 #include <memory>
19 
20 #include "aom/aomcx.h"
21 #include "aom/aom_encoder.h"
22 #include "gtest/gtest.h"
23 
24 namespace {
25 
TestOnePassMode(unsigned int lag_in_frames)26 void TestOnePassMode(unsigned int lag_in_frames) {
27   // A buffer of gray samples of size 128x128, YUV 4:2:0.
28   constexpr size_t kImageDataSize = 128 * 128 + 2 * 64 * 64;
29   std::unique_ptr<unsigned char[]> img_data(new unsigned char[kImageDataSize]);
30   ASSERT_NE(img_data, nullptr);
31   memset(img_data.get(), 128, kImageDataSize);
32 
33   aom_codec_iface_t *iface = aom_codec_av1_cx();
34   aom_codec_enc_cfg_t cfg;
35   ASSERT_EQ(AOM_CODEC_OK,
36             aom_codec_enc_config_default(iface, &cfg, AOM_USAGE_GOOD_QUALITY));
37   cfg.g_w = 128;
38   cfg.g_h = 128;
39   cfg.g_pass = AOM_RC_ONE_PASS;
40   cfg.g_lag_in_frames = lag_in_frames;
41   aom_codec_ctx_t enc;
42   EXPECT_EQ(AOM_CODEC_OK, aom_codec_enc_init(&enc, iface, &cfg, 0));
43 
44   aom_image_t img;
45   EXPECT_EQ(&img,
46             aom_img_wrap(&img, AOM_IMG_FMT_I420, 128, 128, 1, img_data.get()));
47 
48   aom_codec_iter_t iter;
49   const aom_codec_cx_pkt_t *pkt;
50   int frame_count = 0;
51 
52   EXPECT_EQ(AOM_CODEC_OK, aom_codec_encode(&enc, &img, 0, 1, 0));
53 
54   iter = nullptr;
55   while ((pkt = aom_codec_get_cx_data(&enc, &iter)) != nullptr) {
56     ASSERT_EQ(pkt->kind, AOM_CODEC_CX_FRAME_PKT);
57     EXPECT_NE(pkt->data.frame.flags & AOM_FRAME_IS_KEY, 0u)
58         << "frame " << frame_count;
59     frame_count++;
60   }
61 
62   EXPECT_EQ(AOM_CODEC_OK,
63             aom_codec_encode(&enc, &img, 1, 1, AOM_EFLAG_FORCE_KF));
64 
65   iter = nullptr;
66   while ((pkt = aom_codec_get_cx_data(&enc, &iter)) != nullptr) {
67     ASSERT_EQ(pkt->kind, AOM_CODEC_CX_FRAME_PKT);
68     EXPECT_NE(pkt->data.frame.flags & AOM_FRAME_IS_KEY, 0u)
69         << "frame " << frame_count;
70     frame_count++;
71   }
72 
73   EXPECT_EQ(AOM_CODEC_OK, aom_codec_encode(&enc, nullptr, 0, 0, 0));
74 
75   iter = nullptr;
76   while ((pkt = aom_codec_get_cx_data(&enc, &iter)) != nullptr) {
77     ASSERT_EQ(pkt->kind, AOM_CODEC_CX_FRAME_PKT);
78     EXPECT_NE(pkt->data.frame.flags & AOM_FRAME_IS_KEY, 0u)
79         << "frame " << frame_count;
80     frame_count++;
81   }
82 
83   EXPECT_EQ(frame_count, 2);
84   EXPECT_EQ(AOM_CODEC_OK, aom_codec_destroy(&enc));
85 }
86 
TEST(ForceKeyFrameTest,OnePassModeLag0)87 TEST(ForceKeyFrameTest, OnePassModeLag0) { TestOnePassMode(0); }
88 
TEST(ForceKeyFrameTest,OnePassModeLag1)89 TEST(ForceKeyFrameTest, OnePassModeLag1) { TestOnePassMode(1); }
90 
TEST(ForceKeyFrameTest,OnePassModeLag2)91 TEST(ForceKeyFrameTest, OnePassModeLag2) { TestOnePassMode(2); }
92 
93 }  // namespace
94