Bug 1882465 - Update .hg-annotate-ignore-revs and .git-blame-ignore-revs to reflect...
[gecko.git] / third_party / aom / test / encode_api_test.cc
bloba7d5b3aa3c7dbcfce8e726fca1f1670ed878e53c
1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
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.
12 #include <cassert>
13 #include <climits>
14 #include <cstdint>
15 #include <cstdlib>
16 #include <cstring>
17 #include <tuple>
19 #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
21 #include "config/aom_config.h"
23 #include "aom/aomcx.h"
24 #include "aom/aom_encoder.h"
25 #include "aom/aom_image.h"
27 namespace {
29 #if CONFIG_REALTIME_ONLY
30 const unsigned int kUsage = AOM_USAGE_REALTIME;
31 #else
32 const unsigned int kUsage = AOM_USAGE_GOOD_QUALITY;
33 #endif
35 static void *Memset16(void *dest, int val, size_t length) {
36 uint16_t *dest16 = (uint16_t *)dest;
37 for (size_t i = 0; i < length; ++i) *dest16++ = val;
38 return dest;
41 TEST(EncodeAPI, InvalidParams) {
42 uint8_t buf[1] = { 0 };
43 aom_image_t img;
44 aom_codec_ctx_t enc;
45 aom_codec_enc_cfg_t cfg;
47 EXPECT_EQ(&img, aom_img_wrap(&img, AOM_IMG_FMT_I420, 1, 1, 1, buf));
49 EXPECT_EQ(AOM_CODEC_INVALID_PARAM,
50 aom_codec_enc_init(nullptr, nullptr, nullptr, 0));
51 EXPECT_EQ(AOM_CODEC_INVALID_PARAM,
52 aom_codec_enc_init(&enc, nullptr, nullptr, 0));
53 EXPECT_EQ(AOM_CODEC_INVALID_PARAM,
54 aom_codec_encode(nullptr, nullptr, 0, 0, 0));
55 EXPECT_EQ(AOM_CODEC_INVALID_PARAM, aom_codec_encode(nullptr, &img, 0, 0, 0));
56 EXPECT_EQ(AOM_CODEC_INVALID_PARAM, aom_codec_destroy(nullptr));
57 EXPECT_EQ(AOM_CODEC_INVALID_PARAM,
58 aom_codec_enc_config_default(nullptr, nullptr, 0));
59 EXPECT_EQ(AOM_CODEC_INVALID_PARAM,
60 aom_codec_enc_config_default(nullptr, &cfg, 0));
61 EXPECT_NE(aom_codec_error(nullptr), nullptr);
63 aom_codec_iface_t *iface = aom_codec_av1_cx();
64 SCOPED_TRACE(aom_codec_iface_name(iface));
65 EXPECT_EQ(AOM_CODEC_INVALID_PARAM,
66 aom_codec_enc_init(nullptr, iface, nullptr, 0));
67 EXPECT_EQ(AOM_CODEC_INVALID_PARAM,
68 aom_codec_enc_init(&enc, iface, nullptr, 0));
69 EXPECT_EQ(AOM_CODEC_INVALID_PARAM,
70 aom_codec_enc_config_default(iface, &cfg, 3));
71 EXPECT_EQ(AOM_CODEC_OK, aom_codec_enc_config_default(iface, &cfg, kUsage));
72 cfg.g_w = 1 << 16;
73 cfg.g_h = (1 << 14) + 1;
74 EXPECT_EQ(AOM_CODEC_INVALID_PARAM, aom_codec_enc_init(&enc, iface, &cfg, 0));
75 EXPECT_EQ(AOM_CODEC_OK, aom_codec_enc_config_default(iface, &cfg, kUsage));
76 cfg.g_w = (1 << 14) + 1;
77 cfg.g_h = 1 << 16;
78 EXPECT_EQ(AOM_CODEC_INVALID_PARAM, aom_codec_enc_init(&enc, iface, &cfg, 0));
79 EXPECT_EQ(AOM_CODEC_OK, aom_codec_enc_config_default(iface, &cfg, kUsage));
80 cfg.g_forced_max_frame_width = 1 << 16;
81 cfg.g_forced_max_frame_height = (1 << 14) + 1;
82 EXPECT_EQ(AOM_CODEC_INVALID_PARAM, aom_codec_enc_init(&enc, iface, &cfg, 0));
83 EXPECT_EQ(AOM_CODEC_OK, aom_codec_enc_config_default(iface, &cfg, kUsage));
84 cfg.g_forced_max_frame_width = (1 << 14) + 1;
85 cfg.g_forced_max_frame_height = 1 << 16;
86 EXPECT_EQ(AOM_CODEC_INVALID_PARAM, aom_codec_enc_init(&enc, iface, &cfg, 0));
87 EXPECT_EQ(AOM_CODEC_OK, aom_codec_enc_config_default(iface, &cfg, kUsage));
88 EXPECT_EQ(AOM_CODEC_OK, aom_codec_enc_init(&enc, iface, &cfg, 0));
89 EXPECT_EQ(nullptr, aom_codec_get_global_headers(nullptr));
91 aom_fixed_buf_t *glob_headers = aom_codec_get_global_headers(&enc);
92 EXPECT_NE(glob_headers->buf, nullptr);
93 if (glob_headers) {
94 free(glob_headers->buf);
95 free(glob_headers);
97 EXPECT_EQ(AOM_CODEC_OK, aom_codec_encode(&enc, nullptr, 0, 0, 0));
98 EXPECT_EQ(AOM_CODEC_OK, aom_codec_destroy(&enc));
101 TEST(EncodeAPI, InvalidControlId) {
102 aom_codec_iface_t *iface = aom_codec_av1_cx();
103 aom_codec_ctx_t enc;
104 aom_codec_enc_cfg_t cfg;
105 EXPECT_EQ(AOM_CODEC_OK, aom_codec_enc_config_default(iface, &cfg, kUsage));
106 EXPECT_EQ(AOM_CODEC_OK, aom_codec_enc_init(&enc, iface, &cfg, 0));
107 EXPECT_EQ(AOM_CODEC_ERROR, aom_codec_control(&enc, -1, 0));
108 EXPECT_EQ(AOM_CODEC_INVALID_PARAM, aom_codec_control(&enc, 0, 0));
109 EXPECT_EQ(AOM_CODEC_OK, aom_codec_destroy(&enc));
112 void EncodeSetSFrameOnFirstFrame(aom_img_fmt fmt, aom_codec_flags_t flag) {
113 constexpr int kWidth = 2;
114 constexpr int kHeight = 128;
115 unsigned char kBuffer[kWidth * kHeight * 3] = { 0 };
116 aom_image_t img;
117 ASSERT_EQ(aom_img_wrap(&img, fmt, kWidth, kHeight, 1, kBuffer), &img);
119 aom_codec_iface_t *iface = aom_codec_av1_cx();
120 aom_codec_enc_cfg_t cfg;
121 ASSERT_EQ(aom_codec_enc_config_default(iface, &cfg, kUsage), AOM_CODEC_OK);
122 cfg.g_w = kWidth;
123 cfg.g_h = kHeight;
125 aom_codec_ctx_t enc;
126 ASSERT_EQ(aom_codec_enc_init(&enc, iface, &cfg, flag), AOM_CODEC_OK);
127 // One of these aom_codec_encode() calls should fail.
128 if (aom_codec_encode(&enc, &img, 0, 1, AOM_EFLAG_SET_S_FRAME) ==
129 AOM_CODEC_OK) {
130 EXPECT_NE(aom_codec_encode(&enc, nullptr, 0, 0, 0), AOM_CODEC_OK);
132 EXPECT_EQ(aom_codec_destroy(&enc), AOM_CODEC_OK);
135 TEST(EncodeAPI, SetSFrameOnFirstFrame) {
136 EncodeSetSFrameOnFirstFrame(AOM_IMG_FMT_I420, 0);
139 #if CONFIG_AV1_HIGHBITDEPTH
140 TEST(EncodeAPI, SetSFrameOnFirstFrameHighbd) {
141 EncodeSetSFrameOnFirstFrame(AOM_IMG_FMT_I42016, AOM_CODEC_USE_HIGHBITDEPTH);
143 #endif // CONFIG_AV1_HIGHBITDEPTH
145 TEST(EncodeAPI, MonochromeInProfiles) {
146 aom_codec_iface_t *iface = aom_codec_av1_cx();
147 aom_codec_enc_cfg_t cfg;
148 ASSERT_EQ(AOM_CODEC_OK, aom_codec_enc_config_default(iface, &cfg, kUsage));
149 cfg.g_w = 128;
150 cfg.g_h = 128;
151 cfg.monochrome = 1;
152 aom_codec_ctx_t enc;
154 // Test Profile 0
155 cfg.g_profile = 0;
156 ASSERT_EQ(AOM_CODEC_OK, aom_codec_enc_init(&enc, iface, &cfg, 0));
157 EXPECT_EQ(AOM_CODEC_OK, aom_codec_destroy(&enc));
159 // Test Profile 1
160 cfg.g_profile = 1;
161 ASSERT_EQ(AOM_CODEC_INVALID_PARAM, aom_codec_enc_init(&enc, iface, &cfg, 0));
163 // Test Profile 3
164 cfg.g_profile = 2;
165 ASSERT_EQ(AOM_CODEC_OK, aom_codec_enc_init(&enc, iface, &cfg, 0));
166 EXPECT_EQ(AOM_CODEC_OK, aom_codec_destroy(&enc));
169 TEST(EncodeAPI, LowBDEncoderLowBDImage) {
170 aom_codec_iface_t *iface = aom_codec_av1_cx();
171 aom_codec_enc_cfg_t cfg;
172 ASSERT_EQ(aom_codec_enc_config_default(iface, &cfg, kUsage), AOM_CODEC_OK);
174 aom_codec_ctx_t enc;
175 ASSERT_EQ(aom_codec_enc_init(&enc, iface, &cfg, 0), AOM_CODEC_OK);
177 aom_image_t *image =
178 aom_img_alloc(nullptr, AOM_IMG_FMT_I420, cfg.g_w, cfg.g_h, 0);
179 ASSERT_NE(image, nullptr);
181 // Set the image to two colors so that av1_set_screen_content_options() will
182 // call av1_get_perpixel_variance().
183 int luma_value = 0;
184 for (unsigned int i = 0; i < image->d_h; ++i) {
185 memset(image->planes[0] + i * image->stride[0], luma_value, image->d_w);
186 luma_value = 255 - luma_value;
188 unsigned int uv_h = (image->d_h + 1) / 2;
189 unsigned int uv_w = (image->d_w + 1) / 2;
190 for (unsigned int i = 0; i < uv_h; ++i) {
191 memset(image->planes[1] + i * image->stride[1], 128, uv_w);
192 memset(image->planes[2] + i * image->stride[2], 128, uv_w);
195 ASSERT_EQ(aom_codec_encode(&enc, image, 0, 1, 0), AOM_CODEC_OK);
197 aom_img_free(image);
198 ASSERT_EQ(aom_codec_destroy(&enc), AOM_CODEC_OK);
201 TEST(EncodeAPI, HighBDEncoderHighBDImage) {
202 aom_codec_iface_t *iface = aom_codec_av1_cx();
203 aom_codec_enc_cfg_t cfg;
204 ASSERT_EQ(aom_codec_enc_config_default(iface, &cfg, kUsage), AOM_CODEC_OK);
206 aom_codec_ctx_t enc;
207 aom_codec_err_t init_status =
208 aom_codec_enc_init(&enc, iface, &cfg, AOM_CODEC_USE_HIGHBITDEPTH);
209 #if !CONFIG_AV1_HIGHBITDEPTH
210 ASSERT_EQ(init_status, AOM_CODEC_INCAPABLE);
211 #else
212 ASSERT_EQ(init_status, AOM_CODEC_OK);
214 aom_image_t *image =
215 aom_img_alloc(nullptr, AOM_IMG_FMT_I42016, cfg.g_w, cfg.g_h, 0);
216 ASSERT_NE(image, nullptr);
218 // Set the image to two colors so that av1_set_screen_content_options() will
219 // call av1_get_perpixel_variance().
220 int luma_value = 0;
221 for (unsigned int i = 0; i < image->d_h; ++i) {
222 Memset16(image->planes[0] + i * image->stride[0], luma_value, image->d_w);
223 luma_value = 255 - luma_value;
225 unsigned int uv_h = (image->d_h + 1) / 2;
226 unsigned int uv_w = (image->d_w + 1) / 2;
227 for (unsigned int i = 0; i < uv_h; ++i) {
228 Memset16(image->planes[1] + i * image->stride[1], 128, uv_w);
229 Memset16(image->planes[2] + i * image->stride[2], 128, uv_w);
232 ASSERT_EQ(aom_codec_encode(&enc, image, 0, 1, 0), AOM_CODEC_OK);
234 aom_img_free(image);
235 ASSERT_EQ(aom_codec_destroy(&enc), AOM_CODEC_OK);
236 #endif
239 TEST(EncodeAPI, HighBDEncoderLowBDImage) {
240 aom_codec_iface_t *iface = aom_codec_av1_cx();
241 aom_codec_enc_cfg_t cfg;
242 ASSERT_EQ(aom_codec_enc_config_default(iface, &cfg, kUsage), AOM_CODEC_OK);
244 aom_codec_ctx_t enc;
245 aom_codec_err_t init_status =
246 aom_codec_enc_init(&enc, iface, &cfg, AOM_CODEC_USE_HIGHBITDEPTH);
247 #if !CONFIG_AV1_HIGHBITDEPTH
248 ASSERT_EQ(init_status, AOM_CODEC_INCAPABLE);
249 #else
250 ASSERT_EQ(init_status, AOM_CODEC_OK);
252 aom_image_t *image =
253 aom_img_alloc(nullptr, AOM_IMG_FMT_I420, cfg.g_w, cfg.g_h, 0);
254 ASSERT_NE(image, nullptr);
256 // Set the image to two colors so that av1_set_screen_content_options() will
257 // call av1_get_perpixel_variance().
258 int luma_value = 0;
259 for (unsigned int i = 0; i < image->d_h; ++i) {
260 memset(image->planes[0] + i * image->stride[0], luma_value, image->d_w);
261 luma_value = 255 - luma_value;
263 unsigned int uv_h = (image->d_h + 1) / 2;
264 unsigned int uv_w = (image->d_w + 1) / 2;
265 for (unsigned int i = 0; i < uv_h; ++i) {
266 memset(image->planes[1] + i * image->stride[1], 128, uv_w);
267 memset(image->planes[2] + i * image->stride[2], 128, uv_w);
270 ASSERT_EQ(aom_codec_encode(&enc, image, 0, 1, 0), AOM_CODEC_INVALID_PARAM);
272 aom_img_free(image);
273 ASSERT_EQ(aom_codec_destroy(&enc), AOM_CODEC_OK);
274 #endif
277 TEST(EncodeAPI, LowBDEncoderHighBDImage) {
278 aom_codec_iface_t *iface = aom_codec_av1_cx();
279 aom_codec_enc_cfg_t cfg;
280 ASSERT_EQ(aom_codec_enc_config_default(iface, &cfg, kUsage), AOM_CODEC_OK);
282 aom_codec_ctx_t enc;
283 ASSERT_EQ(aom_codec_enc_init(&enc, iface, &cfg, 0), AOM_CODEC_OK);
285 aom_image_t *image =
286 aom_img_alloc(nullptr, AOM_IMG_FMT_I42016, cfg.g_w, cfg.g_h, 0);
287 ASSERT_NE(image, nullptr);
289 // Set the image to two colors so that av1_set_screen_content_options() will
290 // call av1_get_perpixel_variance().
291 int luma_value = 0;
292 for (unsigned int i = 0; i < image->d_h; ++i) {
293 Memset16(image->planes[0] + i * image->stride[0], luma_value, image->d_w);
294 luma_value = 255 - luma_value;
296 unsigned int uv_h = (image->d_h + 1) / 2;
297 unsigned int uv_w = (image->d_w + 1) / 2;
298 for (unsigned int i = 0; i < uv_h; ++i) {
299 Memset16(image->planes[1] + i * image->stride[1], 128, uv_w);
300 Memset16(image->planes[2] + i * image->stride[2], 128, uv_w);
303 ASSERT_EQ(aom_codec_encode(&enc, image, 0, 1, 0), AOM_CODEC_INVALID_PARAM);
305 aom_img_free(image);
306 ASSERT_EQ(aom_codec_destroy(&enc), AOM_CODEC_OK);
309 aom_image_t *CreateGrayImage(aom_img_fmt_t fmt, unsigned int w,
310 unsigned int h) {
311 aom_image_t *const image = aom_img_alloc(nullptr, fmt, w, h, 1);
312 if (!image) return image;
314 for (unsigned int i = 0; i < image->d_h; ++i) {
315 memset(image->planes[0] + i * image->stride[0], 128, image->d_w);
317 const unsigned int uv_h = (image->d_h + 1) / 2;
318 const unsigned int uv_w = (image->d_w + 1) / 2;
319 for (unsigned int i = 0; i < uv_h; ++i) {
320 memset(image->planes[1] + i * image->stride[1], 128, uv_w);
321 memset(image->planes[2] + i * image->stride[2], 128, uv_w);
323 return image;
326 TEST(EncodeAPI, Buganizer310548198) {
327 aom_codec_iface_t *const iface = aom_codec_av1_cx();
328 aom_codec_enc_cfg_t cfg;
329 const unsigned int usage = AOM_USAGE_REALTIME;
330 ASSERT_EQ(aom_codec_enc_config_default(iface, &cfg, usage), AOM_CODEC_OK);
331 cfg.g_w = 1;
332 cfg.g_h = 444;
333 cfg.g_pass = AOM_RC_ONE_PASS;
334 cfg.g_lag_in_frames = 0;
336 aom_codec_ctx_t enc;
337 ASSERT_EQ(aom_codec_enc_init(&enc, iface, &cfg, 0), AOM_CODEC_OK);
339 const int speed = 6;
340 ASSERT_EQ(aom_codec_control(&enc, AOME_SET_CPUUSED, speed), AOM_CODEC_OK);
342 const aom_enc_frame_flags_t flags = 0;
343 int frame_index = 0;
345 // Encode a frame.
346 aom_image_t *image = CreateGrayImage(AOM_IMG_FMT_I420, cfg.g_w, cfg.g_h);
347 ASSERT_NE(image, nullptr);
348 ASSERT_EQ(aom_codec_encode(&enc, image, frame_index, 1, flags), AOM_CODEC_OK);
349 frame_index++;
350 const aom_codec_cx_pkt_t *pkt;
351 aom_codec_iter_t iter = nullptr;
352 while ((pkt = aom_codec_get_cx_data(&enc, &iter)) != nullptr) {
353 ASSERT_EQ(pkt->kind, AOM_CODEC_CX_FRAME_PKT);
355 aom_img_free(image);
357 cfg.g_w = 1;
358 cfg.g_h = 254;
359 ASSERT_EQ(aom_codec_enc_config_set(&enc, &cfg), AOM_CODEC_OK)
360 << aom_codec_error_detail(&enc);
362 cfg.g_w = 1;
363 cfg.g_h = 154;
364 ASSERT_EQ(aom_codec_enc_config_set(&enc, &cfg), AOM_CODEC_OK)
365 << aom_codec_error_detail(&enc);
367 // Encode a frame.
368 image = CreateGrayImage(AOM_IMG_FMT_I420, cfg.g_w, cfg.g_h);
369 ASSERT_EQ(aom_codec_encode(&enc, image, frame_index, 1, flags), AOM_CODEC_OK);
370 frame_index++;
371 iter = nullptr;
372 while ((pkt = aom_codec_get_cx_data(&enc, &iter)) != nullptr) {
373 ASSERT_EQ(pkt->kind, AOM_CODEC_CX_FRAME_PKT);
375 aom_img_free(image);
377 // Flush the encoder.
378 bool got_data;
379 do {
380 ASSERT_EQ(aom_codec_encode(&enc, nullptr, 0, 0, 0), AOM_CODEC_OK);
381 got_data = false;
382 iter = nullptr;
383 while ((pkt = aom_codec_get_cx_data(&enc, &iter)) != nullptr) {
384 ASSERT_EQ(pkt->kind, AOM_CODEC_CX_FRAME_PKT);
385 got_data = true;
387 } while (got_data);
389 ASSERT_EQ(aom_codec_destroy(&enc), AOM_CODEC_OK);
392 // Emulates the WebCodecs VideoEncoder interface.
393 class AV1Encoder {
394 public:
395 explicit AV1Encoder(int speed) : speed_(speed) {}
396 ~AV1Encoder();
398 void Configure(unsigned int threads, unsigned int width, unsigned int height,
399 aom_rc_mode end_usage, unsigned int usage);
400 void Encode(bool key_frame);
402 private:
403 // Flushes the encoder. Should be called after all the Encode() calls.
404 void Flush();
406 const int speed_;
407 bool initialized_ = false;
408 aom_codec_enc_cfg_t cfg_;
409 aom_codec_ctx_t enc_;
410 int frame_index_ = 0;
413 AV1Encoder::~AV1Encoder() {
414 if (initialized_) {
415 Flush();
416 EXPECT_EQ(aom_codec_destroy(&enc_), AOM_CODEC_OK);
420 void AV1Encoder::Configure(unsigned int threads, unsigned int width,
421 unsigned int height, aom_rc_mode end_usage,
422 unsigned int usage) {
423 if (!initialized_) {
424 aom_codec_iface_t *const iface = aom_codec_av1_cx();
425 ASSERT_EQ(aom_codec_enc_config_default(iface, &cfg_, usage), AOM_CODEC_OK);
426 cfg_.g_threads = threads;
427 cfg_.g_w = width;
428 cfg_.g_h = height;
429 cfg_.g_forced_max_frame_width = cfg_.g_w;
430 cfg_.g_forced_max_frame_height = cfg_.g_h;
431 cfg_.g_timebase.num = 1;
432 cfg_.g_timebase.den = 1000 * 1000; // microseconds
433 cfg_.g_pass = AOM_RC_ONE_PASS;
434 cfg_.g_lag_in_frames = 0;
435 cfg_.rc_end_usage = end_usage;
436 cfg_.rc_min_quantizer = 2;
437 cfg_.rc_max_quantizer = 58;
438 ASSERT_EQ(aom_codec_enc_init(&enc_, iface, &cfg_, 0), AOM_CODEC_OK);
439 ASSERT_EQ(aom_codec_control(&enc_, AOME_SET_CPUUSED, speed_), AOM_CODEC_OK);
440 initialized_ = true;
441 return;
444 ASSERT_EQ(usage, cfg_.g_usage);
445 cfg_.g_threads = threads;
446 cfg_.g_w = width;
447 cfg_.g_h = height;
448 cfg_.rc_end_usage = end_usage;
449 ASSERT_EQ(aom_codec_enc_config_set(&enc_, &cfg_), AOM_CODEC_OK)
450 << aom_codec_error_detail(&enc_);
453 void AV1Encoder::Encode(bool key_frame) {
454 assert(initialized_);
455 // TODO(wtc): Support high bit depths and other YUV formats.
456 aom_image_t *const image =
457 CreateGrayImage(AOM_IMG_FMT_I420, cfg_.g_w, cfg_.g_h);
458 ASSERT_NE(image, nullptr);
459 const aom_enc_frame_flags_t flags = key_frame ? AOM_EFLAG_FORCE_KF : 0;
460 ASSERT_EQ(aom_codec_encode(&enc_, image, frame_index_, 1, flags),
461 AOM_CODEC_OK);
462 frame_index_++;
463 const aom_codec_cx_pkt_t *pkt;
464 aom_codec_iter_t iter = nullptr;
465 while ((pkt = aom_codec_get_cx_data(&enc_, &iter)) != nullptr) {
466 ASSERT_EQ(pkt->kind, AOM_CODEC_CX_FRAME_PKT);
467 if (key_frame) {
468 ASSERT_EQ(pkt->data.frame.flags & AOM_FRAME_IS_KEY, AOM_FRAME_IS_KEY);
471 aom_img_free(image);
474 void AV1Encoder::Flush() {
475 bool got_data;
476 do {
477 ASSERT_EQ(aom_codec_encode(&enc_, nullptr, 0, 0, 0), AOM_CODEC_OK);
478 got_data = false;
479 const aom_codec_cx_pkt_t *pkt;
480 aom_codec_iter_t iter = nullptr;
481 while ((pkt = aom_codec_get_cx_data(&enc_, &iter)) != nullptr) {
482 ASSERT_EQ(pkt->kind, AOM_CODEC_CX_FRAME_PKT);
483 got_data = true;
485 } while (got_data);
488 TEST(EncodeAPI, Buganizer314858909) {
489 AV1Encoder encoder(7);
491 encoder.Configure(6, 1582, 750, AOM_CBR, AOM_USAGE_REALTIME);
493 // Encode a frame.
494 encoder.Encode(false);
496 encoder.Configure(0, 1582, 23, AOM_CBR, AOM_USAGE_REALTIME);
498 // Encode a frame..
499 encoder.Encode(false);
501 encoder.Configure(16, 1542, 363, AOM_CBR, AOM_USAGE_REALTIME);
503 // Encode a frame..
504 encoder.Encode(false);
507 // Run this test to reproduce the bug in fuzz test: ASSERT: cpi->rec_sse !=
508 // UINT64_MAX in av1_rc_bits_per_mb.
509 TEST(EncodeAPI, Buganizer310766628) {
510 AV1Encoder encoder(7);
512 encoder.Configure(16, 759, 383, AOM_CBR, AOM_USAGE_REALTIME);
514 // Encode a frame.
515 encoder.Encode(false);
517 encoder.Configure(2, 759, 383, AOM_VBR, AOM_USAGE_REALTIME);
519 // Encode a frame. This will trigger the assertion failure.
520 encoder.Encode(false);
523 // This test covers a possible use case where the change of frame sizes and
524 // thread numbers happens before and after the first frame coding.
525 TEST(EncodeAPI, Buganizer310455204) {
526 AV1Encoder encoder(7);
528 encoder.Configure(0, 1915, 503, AOM_VBR, AOM_USAGE_REALTIME);
530 encoder.Configure(4, 1, 1, AOM_VBR, AOM_USAGE_REALTIME);
532 encoder.Configure(6, 559, 503, AOM_CBR, AOM_USAGE_REALTIME);
534 // Encode a frame.
535 encoder.Encode(false);
537 // Increase the number of threads.
538 encoder.Configure(16, 1915, 503, AOM_CBR, AOM_USAGE_REALTIME);
540 // Encode a frame.
541 encoder.Encode(false);
544 // Run this test to reproduce the bug in fuzz test: Float-cast-overflow in
545 // av1_rc_bits_per_mb.
546 TEST(EncodeAPI, Buganizer310457427) {
547 AV1Encoder encoder(7);
549 encoder.Configure(12, 896, 1076, AOM_CBR, AOM_USAGE_REALTIME);
551 encoder.Configure(6, 609, 1076, AOM_VBR, AOM_USAGE_REALTIME);
553 // Encode a frame.
554 encoder.Encode(false);
556 // Encode a frame. This will trigger the float-cast-overflow bug which was
557 // caused by division by zero.
558 encoder.Encode(false);
561 TEST(EncodeAPI, PtsSmallerThanInitialPts) {
562 // Initialize libaom encoder.
563 aom_codec_iface_t *const iface = aom_codec_av1_cx();
564 aom_codec_ctx_t enc;
565 aom_codec_enc_cfg_t cfg;
567 ASSERT_EQ(aom_codec_enc_config_default(iface, &cfg, AOM_USAGE_REALTIME),
568 AOM_CODEC_OK);
570 cfg.g_w = 1280;
571 cfg.g_h = 720;
572 cfg.rc_target_bitrate = 1000;
574 ASSERT_EQ(aom_codec_enc_init(&enc, iface, &cfg, 0), AOM_CODEC_OK);
576 // Create input image.
577 aom_image_t *const image =
578 CreateGrayImage(AOM_IMG_FMT_I420, cfg.g_w, cfg.g_h);
579 ASSERT_NE(image, nullptr);
581 // Encode frame.
582 ASSERT_EQ(aom_codec_encode(&enc, image, 12, 1, 0), AOM_CODEC_OK);
583 ASSERT_EQ(aom_codec_encode(&enc, image, 13, 1, 0), AOM_CODEC_OK);
584 // pts (10) is smaller than the initial pts (12).
585 ASSERT_EQ(aom_codec_encode(&enc, image, 10, 1, 0), AOM_CODEC_INVALID_PARAM);
587 // Free resources.
588 aom_img_free(image);
589 aom_codec_destroy(&enc);
592 TEST(EncodeAPI, PtsOrDurationTooBig) {
593 // Initialize libaom encoder.
594 aom_codec_iface_t *const iface = aom_codec_av1_cx();
595 aom_codec_ctx_t enc;
596 aom_codec_enc_cfg_t cfg;
598 ASSERT_EQ(aom_codec_enc_config_default(iface, &cfg, AOM_USAGE_REALTIME),
599 AOM_CODEC_OK);
601 cfg.g_w = 1280;
602 cfg.g_h = 720;
603 cfg.rc_target_bitrate = 1000;
605 ASSERT_EQ(aom_codec_enc_init(&enc, iface, &cfg, 0), AOM_CODEC_OK);
607 // Create input image.
608 aom_image_t *const image =
609 CreateGrayImage(AOM_IMG_FMT_I420, cfg.g_w, cfg.g_h);
610 ASSERT_NE(image, nullptr);
612 // Encode frame.
613 ASSERT_EQ(aom_codec_encode(&enc, image, 0, 1, 0), AOM_CODEC_OK);
614 // pts, when converted to ticks, is too big.
615 ASSERT_EQ(aom_codec_encode(&enc, image, INT64_MAX / 1000000 + 1, 1, 0),
616 AOM_CODEC_INVALID_PARAM);
617 #if ULONG_MAX > INT64_MAX
618 // duration is too big.
619 ASSERT_EQ(aom_codec_encode(&enc, image, 0, (1ul << 63), 0),
620 AOM_CODEC_INVALID_PARAM);
621 // pts + duration is too big.
622 ASSERT_EQ(aom_codec_encode(&enc, image, 1, INT64_MAX, 0),
623 AOM_CODEC_INVALID_PARAM);
624 #endif
625 // pts + duration, when converted to ticks, is too big.
626 #if ULONG_MAX > INT64_MAX
627 ASSERT_EQ(aom_codec_encode(&enc, image, 0, 0x1c0a0a1a3232, 0),
628 AOM_CODEC_INVALID_PARAM);
629 #endif
630 ASSERT_EQ(aom_codec_encode(&enc, image, INT64_MAX / 1000000, 1, 0),
631 AOM_CODEC_INVALID_PARAM);
633 // Free resources.
634 aom_img_free(image);
635 aom_codec_destroy(&enc);
638 class EncodeAPIParameterized
639 : public testing::TestWithParam<std::tuple<
640 /*usage=*/unsigned int, /*speed=*/int, /*aq_mode=*/unsigned int>> {};
642 // Encodes two frames at a given usage, speed, and aq_mode setting.
643 // Reproduces b/303023614
644 TEST_P(EncodeAPIParameterized, HighBDEncoderHighBDFrames) {
645 const unsigned int usage = std::get<0>(GetParam());
646 int speed = std::get<1>(GetParam());
648 if (speed == 10 && usage != AOM_USAGE_REALTIME) {
649 speed = 9; // 10 is only allowed in AOM_USAGE_REALTIME
652 aom_codec_iface_t *iface = aom_codec_av1_cx();
653 aom_codec_enc_cfg_t cfg;
654 ASSERT_EQ(aom_codec_enc_config_default(iface, &cfg, usage), AOM_CODEC_OK);
655 cfg.g_w = 500;
656 cfg.g_h = 400;
658 aom_codec_ctx_t enc;
659 aom_codec_err_t init_status =
660 aom_codec_enc_init(&enc, iface, &cfg, AOM_CODEC_USE_HIGHBITDEPTH);
661 #if !CONFIG_AV1_HIGHBITDEPTH
662 ASSERT_EQ(init_status, AOM_CODEC_INCAPABLE);
663 #else
664 ASSERT_EQ(init_status, AOM_CODEC_OK);
666 const unsigned int aq_mode = std::get<2>(GetParam());
668 ASSERT_EQ(aom_codec_control(&enc, AOME_SET_CPUUSED, speed), AOM_CODEC_OK);
669 ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_AQ_MODE, aq_mode), AOM_CODEC_OK);
671 aom_image_t *image =
672 aom_img_alloc(nullptr, AOM_IMG_FMT_I42016, cfg.g_w, cfg.g_h, 0);
673 ASSERT_NE(image, nullptr);
675 for (unsigned int i = 0; i < image->d_h; ++i) {
676 Memset16(image->planes[0] + i * image->stride[0], 128, image->d_w);
678 unsigned int uv_h = (image->d_h + 1) / 2;
679 unsigned int uv_w = (image->d_w + 1) / 2;
680 for (unsigned int i = 0; i < uv_h; ++i) {
681 Memset16(image->planes[1] + i * image->stride[1], 128, uv_w);
682 Memset16(image->planes[2] + i * image->stride[2], 128, uv_w);
685 // Encode two frames.
686 ASSERT_EQ(
687 aom_codec_encode(&enc, image, /*pts=*/0, /*duration=*/1, /*flags=*/0),
688 AOM_CODEC_OK);
689 ASSERT_EQ(
690 aom_codec_encode(&enc, image, /*pts=*/1, /*duration=*/1, /*flags=*/0),
691 AOM_CODEC_OK);
693 aom_img_free(image);
694 ASSERT_EQ(aom_codec_destroy(&enc), AOM_CODEC_OK);
695 #endif
698 const unsigned int kUsages[] = {
699 AOM_USAGE_REALTIME,
700 #if !CONFIG_REALTIME_ONLY
701 AOM_USAGE_GOOD_QUALITY,
702 AOM_USAGE_ALL_INTRA,
703 #endif
706 INSTANTIATE_TEST_SUITE_P(All, EncodeAPIParameterized,
707 testing::Combine(
708 /*usage=*/testing::ValuesIn(kUsages),
709 /*speed=*/testing::Values(6, 7, 10),
710 /*aq_mode=*/testing::Values(0, 1, 2, 3)));
712 #if !CONFIG_REALTIME_ONLY
713 TEST(EncodeAPI, AllIntraMode) {
714 aom_codec_iface_t *iface = aom_codec_av1_cx();
715 aom_codec_ctx_t enc;
716 aom_codec_enc_cfg_t cfg;
717 EXPECT_EQ(AOM_CODEC_OK,
718 aom_codec_enc_config_default(iface, &cfg, AOM_USAGE_ALL_INTRA));
719 EXPECT_EQ(AOM_CODEC_OK, aom_codec_enc_init(&enc, iface, &cfg, 0));
720 EXPECT_EQ(AOM_CODEC_OK, aom_codec_destroy(&enc));
722 // Set g_lag_in_frames to a nonzero value. This should cause
723 // aom_codec_enc_init() to fail.
724 EXPECT_EQ(AOM_CODEC_OK,
725 aom_codec_enc_config_default(iface, &cfg, AOM_USAGE_ALL_INTRA));
726 cfg.g_lag_in_frames = 1;
727 EXPECT_EQ(AOM_CODEC_INVALID_PARAM, aom_codec_enc_init(&enc, iface, &cfg, 0));
729 // Set kf_max_dist to a nonzero value. This should cause aom_codec_enc_init()
730 // to fail.
731 EXPECT_EQ(AOM_CODEC_OK,
732 aom_codec_enc_config_default(iface, &cfg, AOM_USAGE_ALL_INTRA));
733 cfg.kf_max_dist = 1;
734 EXPECT_EQ(AOM_CODEC_INVALID_PARAM, aom_codec_enc_init(&enc, iface, &cfg, 0));
737 TEST(EncodeAPI, AllIntraAndUsePsnr) {
738 aom_codec_iface_t *iface = aom_codec_av1_cx();
739 aom_codec_enc_cfg_t cfg;
740 ASSERT_EQ(aom_codec_enc_config_default(iface, &cfg, AOM_USAGE_ALL_INTRA),
741 AOM_CODEC_OK);
743 aom_codec_ctx_t enc;
744 ASSERT_EQ(aom_codec_enc_init(&enc, iface, &cfg, AOM_CODEC_USE_PSNR),
745 AOM_CODEC_OK);
747 aom_image_t *image = CreateGrayImage(AOM_IMG_FMT_I420, cfg.g_w, cfg.g_h);
748 ASSERT_NE(image, nullptr);
750 ASSERT_EQ(aom_codec_encode(&enc, image, 0, 1, 0), AOM_CODEC_OK);
751 const aom_codec_cx_pkt_t *pkt;
752 aom_codec_iter_t iter = nullptr;
753 while ((pkt = aom_codec_get_cx_data(&enc, &iter)) != nullptr) {
754 if (pkt->kind != AOM_CODEC_CX_FRAME_PKT) {
755 ASSERT_EQ(pkt->kind, AOM_CODEC_PSNR_PKT);
759 aom_img_free(image);
760 ASSERT_EQ(aom_codec_destroy(&enc), AOM_CODEC_OK);
763 // A test that reproduces bug aomedia:3534.
764 TEST(EncodeAPI, AllIntraAndNoRefLast) {
765 aom_codec_iface_t *iface = aom_codec_av1_cx();
766 aom_codec_enc_cfg_t cfg;
767 ASSERT_EQ(aom_codec_enc_config_default(iface, &cfg, AOM_USAGE_ALL_INTRA),
768 AOM_CODEC_OK);
770 aom_codec_ctx_t enc;
771 ASSERT_EQ(aom_codec_enc_init(&enc, iface, &cfg, 0), AOM_CODEC_OK);
773 aom_image_t *image = CreateGrayImage(AOM_IMG_FMT_I420, cfg.g_w, cfg.g_h);
774 ASSERT_NE(image, nullptr);
776 ASSERT_EQ(aom_codec_encode(&enc, image, 0, 1, AOM_EFLAG_NO_REF_LAST),
777 AOM_CODEC_OK);
779 aom_img_free(image);
780 ASSERT_EQ(aom_codec_destroy(&enc), AOM_CODEC_OK);
782 #endif // !CONFIG_REALTIME_ONLY
784 } // namespace