xref: /aosp_15_r20/external/libaom/test/encode_test_driver.cc (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1 /*
2  * Copyright (c) 2016, 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 <memory>
13 #include <string>
14 
15 #include "gtest/gtest.h"
16 
17 #include "config/aom_config.h"
18 
19 #include "aom_ports/mem.h"
20 #include "test/codec_factory.h"
21 #include "test/decode_test_driver.h"
22 #include "test/encode_test_driver.h"
23 #include "test/register_state_check.h"
24 #include "test/video_source.h"
25 
26 namespace libaom_test {
InitEncoder(VideoSource * video)27 void Encoder::InitEncoder(VideoSource *video) {
28   aom_codec_err_t res;
29   const aom_image_t *img = video->img();
30 
31   if (video->img() && !encoder_.priv) {
32     cfg_.g_w = img->d_w;
33     cfg_.g_h = img->d_h;
34     cfg_.g_timebase = video->timebase();
35     cfg_.rc_twopass_stats_in = stats_->buf();
36 
37     res = aom_codec_enc_init(&encoder_, CodecInterface(), &cfg_, init_flags_);
38     ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
39   }
40 }
41 
EncodeFrame(VideoSource * video,const aom_enc_frame_flags_t frame_flags)42 void Encoder::EncodeFrame(VideoSource *video,
43                           const aom_enc_frame_flags_t frame_flags) {
44   if (video->img())
45     EncodeFrameInternal(*video, frame_flags);
46   else
47     Flush();
48 
49   // Handle twopass stats
50   CxDataIterator iter = GetCxData();
51 
52   while (const aom_codec_cx_pkt_t *pkt = iter.Next()) {
53     if (pkt->kind != AOM_CODEC_STATS_PKT) continue;
54 
55     stats_->Append(*pkt);
56   }
57 }
58 
EncodeFrameInternal(const VideoSource & video,const aom_enc_frame_flags_t frame_flags)59 void Encoder::EncodeFrameInternal(const VideoSource &video,
60                                   const aom_enc_frame_flags_t frame_flags) {
61   aom_codec_err_t res;
62   const aom_image_t *img = video.img();
63 
64   // Handle frame resizing
65   if (cfg_.g_w != img->d_w || cfg_.g_h != img->d_h) {
66     cfg_.g_w = img->d_w;
67     cfg_.g_h = img->d_h;
68     res = aom_codec_enc_config_set(&encoder_, &cfg_);
69     ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
70   }
71 
72   // Encode the frame
73   API_REGISTER_STATE_CHECK(res =
74                                aom_codec_encode(&encoder_, img, video.pts(),
75                                                 video.duration(), frame_flags));
76   ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
77 }
78 
Flush()79 void Encoder::Flush() {
80   const aom_codec_err_t res = aom_codec_encode(&encoder_, nullptr, 0, 0, 0);
81   if (!encoder_.priv)
82     ASSERT_EQ(AOM_CODEC_ERROR, res) << EncoderError();
83   else
84     ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
85 }
86 
InitializeConfig(TestMode mode)87 void EncoderTest::InitializeConfig(TestMode mode) {
88   int usage = AOM_USAGE_GOOD_QUALITY;
89   switch (mode) {
90     case kOnePassGood:
91     case kTwoPassGood: break;
92     case kRealTime: usage = AOM_USAGE_REALTIME; break;
93     case kAllIntra: usage = AOM_USAGE_ALL_INTRA; break;
94     default: ASSERT_TRUE(false) << "Unexpected mode " << mode;
95   }
96   mode_ = mode;
97   passes_ = (mode == kTwoPassGood) ? 2 : 1;
98 
99   const aom_codec_err_t res = codec_->DefaultEncoderConfig(&cfg_, usage);
100   ASSERT_EQ(AOM_CODEC_OK, res);
101 }
102 
compare_plane(const uint8_t * const buf1,int stride1,const uint8_t * const buf2,int stride2,int w,int h,int * const mismatch_row,int * const mismatch_col,int * const mismatch_pix1,int * const mismatch_pix2)103 static bool compare_plane(const uint8_t *const buf1, int stride1,
104                           const uint8_t *const buf2, int stride2, int w, int h,
105                           int *const mismatch_row, int *const mismatch_col,
106                           int *const mismatch_pix1, int *const mismatch_pix2) {
107   int r, c;
108 
109   for (r = 0; r < h; ++r) {
110     for (c = 0; c < w; ++c) {
111       const int pix1 = buf1[r * stride1 + c];
112       const int pix2 = buf2[r * stride2 + c];
113 
114       if (pix1 != pix2) {
115         if (mismatch_row != nullptr) *mismatch_row = r;
116         if (mismatch_col != nullptr) *mismatch_col = c;
117         if (mismatch_pix1 != nullptr) *mismatch_pix1 = pix1;
118         if (mismatch_pix2 != nullptr) *mismatch_pix2 = pix2;
119         return false;
120       }
121     }
122   }
123 
124   return true;
125 }
126 
127 // The function should return "true" most of the time, therefore no early
128 // break-out is implemented within the match checking process.
compare_img(const aom_image_t * img1,const aom_image_t * img2,int * const mismatch_row,int * const mismatch_col,int * const mismatch_plane,int * const mismatch_pix1,int * const mismatch_pix2)129 static bool compare_img(const aom_image_t *img1, const aom_image_t *img2,
130                         int *const mismatch_row, int *const mismatch_col,
131                         int *const mismatch_plane, int *const mismatch_pix1,
132                         int *const mismatch_pix2) {
133   if (img1->fmt != img2->fmt || img1->cp != img2->cp || img1->tc != img2->tc ||
134       img1->mc != img2->mc || img1->d_w != img2->d_w ||
135       img1->d_h != img2->d_h || img1->monochrome != img2->monochrome) {
136     if (mismatch_row != nullptr) *mismatch_row = -1;
137     if (mismatch_col != nullptr) *mismatch_col = -1;
138     return false;
139   }
140 
141   const int num_planes = img1->monochrome ? 1 : 3;
142   for (int plane = 0; plane < num_planes; plane++) {
143     if (!compare_plane(img1->planes[plane], img1->stride[plane],
144                        img2->planes[plane], img2->stride[plane],
145                        aom_img_plane_width(img1, plane),
146                        aom_img_plane_height(img1, plane), mismatch_row,
147                        mismatch_col, mismatch_pix1, mismatch_pix2)) {
148       if (mismatch_plane != nullptr) *mismatch_plane = plane;
149       return false;
150     }
151   }
152 
153   return true;
154 }
155 
MismatchHook(const aom_image_t * img_enc,const aom_image_t * img_dec)156 void EncoderTest::MismatchHook(const aom_image_t *img_enc,
157                                const aom_image_t *img_dec) {
158   int mismatch_row = 0;
159   int mismatch_col = 0;
160   int mismatch_plane = 0;
161   int mismatch_pix_enc = 0;
162   int mismatch_pix_dec = 0;
163 
164   ASSERT_FALSE(compare_img(img_enc, img_dec, &mismatch_row, &mismatch_col,
165                            &mismatch_plane, &mismatch_pix_enc,
166                            &mismatch_pix_dec));
167 
168   GTEST_FAIL() << "Encode/Decode mismatch found:" << std::endl
169                << "  pixel value enc/dec: " << mismatch_pix_enc << "/"
170                << mismatch_pix_dec << std::endl
171                << "                plane: " << mismatch_plane << std::endl
172                << "              row/col: " << mismatch_row << "/"
173                << mismatch_col << std::endl;
174 }
175 
RunLoop(VideoSource * video)176 void EncoderTest::RunLoop(VideoSource *video) {
177   stats_.Reset();
178 
179   ASSERT_TRUE(passes_ == 1 || passes_ == 2);
180   for (unsigned int pass = 0; pass < passes_; pass++) {
181     aom_codec_pts_t last_pts = 0;
182 
183     if (passes_ == 1)
184       cfg_.g_pass = AOM_RC_ONE_PASS;
185     else if (pass == 0)
186       cfg_.g_pass = AOM_RC_FIRST_PASS;
187     else
188       cfg_.g_pass = AOM_RC_LAST_PASS;
189 
190     BeginPassHook(pass);
191     std::unique_ptr<Encoder> encoder(
192         codec_->CreateEncoder(cfg_, init_flags_, &stats_));
193     ASSERT_NE(encoder, nullptr);
194 
195     ASSERT_NO_FATAL_FAILURE(video->Begin());
196     encoder->InitEncoder(video);
197 
198     if (mode_ == kRealTime) {
199       encoder->Control(AOME_SET_ENABLEAUTOALTREF, 0);
200     }
201 
202     ASSERT_FALSE(::testing::Test::HasFatalFailure());
203 #if CONFIG_AV1_DECODER
204     aom_codec_dec_cfg_t dec_cfg = aom_codec_dec_cfg_t();
205     dec_cfg.allow_lowbitdepth = 1;
206     std::unique_ptr<Decoder> decoder(
207         codec_->CreateDecoder(dec_cfg, 0 /* flags */));
208     if (decoder->IsAV1()) {
209       // Set dec_cfg.tile_row = -1 and dec_cfg.tile_col = -1 so that the whole
210       // frame is decoded.
211       decoder->Control(AV1_SET_TILE_MODE, cfg_.large_scale_tile);
212       decoder->Control(AV1D_EXT_TILE_DEBUG, 1);
213       decoder->Control(AV1_SET_DECODE_TILE_ROW, -1);
214       decoder->Control(AV1_SET_DECODE_TILE_COL, -1);
215     }
216 #endif
217 
218     int number_spatial_layers = GetNumSpatialLayers();
219 
220     bool again;
221     for (again = true; again; video->Next()) {
222       again = (video->img() != nullptr);
223 
224       for (int sl = 0; sl < number_spatial_layers; sl++) {
225         PreEncodeFrameHook(video, encoder.get());
226         encoder->EncodeFrame(video, frame_flags_);
227         PostEncodeFrameHook(encoder.get());
228         CxDataIterator iter = encoder->GetCxData();
229         bool has_cxdata = false;
230 
231 #if CONFIG_AV1_DECODER
232         bool has_dxdata = false;
233 #endif
234         while (const aom_codec_cx_pkt_t *pkt = iter.Next()) {
235           pkt = MutateEncoderOutputHook(pkt);
236           again = true;
237           switch (pkt->kind) {
238             case AOM_CODEC_CX_FRAME_PKT:  //
239               has_cxdata = true;
240 #if CONFIG_AV1_DECODER
241               if (decoder.get() != nullptr && DoDecode()) {
242                 aom_codec_err_t res_dec;
243                 if (DoDecodeInvisible()) {
244                   res_dec = decoder->DecodeFrame(
245                       (const uint8_t *)pkt->data.frame.buf, pkt->data.frame.sz);
246                 } else {
247                   res_dec = decoder->DecodeFrame(
248                       (const uint8_t *)pkt->data.frame.buf +
249                           (pkt->data.frame.sz - pkt->data.frame.vis_frame_size),
250                       pkt->data.frame.vis_frame_size);
251                 }
252 
253                 if (!HandleDecodeResult(res_dec, decoder.get())) break;
254 
255                 has_dxdata = true;
256               }
257 #endif
258               ASSERT_GE(pkt->data.frame.pts, last_pts);
259               if (sl == number_spatial_layers - 1)
260                 last_pts = pkt->data.frame.pts;
261               FramePktHook(pkt);
262               break;
263 
264             case AOM_CODEC_PSNR_PKT: PSNRPktHook(pkt); break;
265 
266             case AOM_CODEC_STATS_PKT: StatsPktHook(pkt); break;
267 
268             default: break;
269           }
270         }
271         if (has_cxdata) {
272           const aom_image_t *img_enc = encoder->GetPreviewFrame();
273           if (img_enc) {
274             CalculateFrameLevelSSIM(video->img(), img_enc, cfg_.g_bit_depth,
275                                     cfg_.g_input_bit_depth);
276           }
277 #if CONFIG_AV1_DECODER
278           if (has_dxdata) {
279             DxDataIterator dec_iter = decoder->GetDxData();
280             const aom_image_t *img_dec = dec_iter.Next();
281             if (img_enc && img_dec) {
282               const bool res = compare_img(img_enc, img_dec, nullptr, nullptr,
283                                            nullptr, nullptr, nullptr);
284               if (!res) {  // Mismatch
285                 MismatchHook(img_enc, img_dec);
286               }
287             }
288             if (img_dec) DecompressedFrameHook(*img_dec, video->pts());
289           }
290 #endif
291         }
292         if (!Continue()) break;
293       }  // Loop over spatial layers
294     }
295 
296     EndPassHook();
297 
298     if (!Continue()) break;
299   }
300 }
301 
302 }  // namespace libaom_test
303