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.
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"
29 #if CONFIG_REALTIME_ONLY
30 const unsigned int kUsage
= AOM_USAGE_REALTIME
;
32 const unsigned int kUsage
= AOM_USAGE_GOOD_QUALITY
;
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
;
41 TEST(EncodeAPI
, InvalidParams
) {
42 uint8_t buf
[1] = { 0 };
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
));
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;
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);
94 free(glob_headers
->buf
);
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();
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 };
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
);
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
) ==
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
));
156 ASSERT_EQ(AOM_CODEC_OK
, aom_codec_enc_init(&enc
, iface
, &cfg
, 0));
157 EXPECT_EQ(AOM_CODEC_OK
, aom_codec_destroy(&enc
));
161 ASSERT_EQ(AOM_CODEC_INVALID_PARAM
, aom_codec_enc_init(&enc
, iface
, &cfg
, 0));
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
);
175 ASSERT_EQ(aom_codec_enc_init(&enc
, iface
, &cfg
, 0), AOM_CODEC_OK
);
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().
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
);
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
);
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
);
212 ASSERT_EQ(init_status
, AOM_CODEC_OK
);
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().
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
);
235 ASSERT_EQ(aom_codec_destroy(&enc
), AOM_CODEC_OK
);
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
);
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
);
250 ASSERT_EQ(init_status
, AOM_CODEC_OK
);
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().
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
);
273 ASSERT_EQ(aom_codec_destroy(&enc
), AOM_CODEC_OK
);
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
);
283 ASSERT_EQ(aom_codec_enc_init(&enc
, iface
, &cfg
, 0), AOM_CODEC_OK
);
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().
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
);
306 ASSERT_EQ(aom_codec_destroy(&enc
), AOM_CODEC_OK
);
309 aom_image_t
*CreateGrayImage(aom_img_fmt_t fmt
, unsigned int w
,
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
);
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
);
333 cfg
.g_pass
= AOM_RC_ONE_PASS
;
334 cfg
.g_lag_in_frames
= 0;
337 ASSERT_EQ(aom_codec_enc_init(&enc
, iface
, &cfg
, 0), AOM_CODEC_OK
);
340 ASSERT_EQ(aom_codec_control(&enc
, AOME_SET_CPUUSED
, speed
), AOM_CODEC_OK
);
342 const aom_enc_frame_flags_t flags
= 0;
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
);
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
);
359 ASSERT_EQ(aom_codec_enc_config_set(&enc
, &cfg
), AOM_CODEC_OK
)
360 << aom_codec_error_detail(&enc
);
364 ASSERT_EQ(aom_codec_enc_config_set(&enc
, &cfg
), AOM_CODEC_OK
)
365 << aom_codec_error_detail(&enc
);
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
);
372 while ((pkt
= aom_codec_get_cx_data(&enc
, &iter
)) != nullptr) {
373 ASSERT_EQ(pkt
->kind
, AOM_CODEC_CX_FRAME_PKT
);
377 // Flush the encoder.
380 ASSERT_EQ(aom_codec_encode(&enc
, nullptr, 0, 0, 0), AOM_CODEC_OK
);
383 while ((pkt
= aom_codec_get_cx_data(&enc
, &iter
)) != nullptr) {
384 ASSERT_EQ(pkt
->kind
, AOM_CODEC_CX_FRAME_PKT
);
389 ASSERT_EQ(aom_codec_destroy(&enc
), AOM_CODEC_OK
);
392 // Emulates the WebCodecs VideoEncoder interface.
395 explicit AV1Encoder(int speed
) : speed_(speed
) {}
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
);
403 // Flushes the encoder. Should be called after all the Encode() calls.
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() {
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
) {
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
;
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
);
444 ASSERT_EQ(usage
, cfg_
.g_usage
);
445 cfg_
.g_threads
= threads
;
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
),
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
);
468 ASSERT_EQ(pkt
->data
.frame
.flags
& AOM_FRAME_IS_KEY
, AOM_FRAME_IS_KEY
);
474 void AV1Encoder::Flush() {
477 ASSERT_EQ(aom_codec_encode(&enc_
, nullptr, 0, 0, 0), AOM_CODEC_OK
);
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
);
488 TEST(EncodeAPI
, Buganizer314858909
) {
489 AV1Encoder
encoder(7);
491 encoder
.Configure(6, 1582, 750, AOM_CBR
, AOM_USAGE_REALTIME
);
494 encoder
.Encode(false);
496 encoder
.Configure(0, 1582, 23, AOM_CBR
, AOM_USAGE_REALTIME
);
499 encoder
.Encode(false);
501 encoder
.Configure(16, 1542, 363, AOM_CBR
, AOM_USAGE_REALTIME
);
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
);
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
);
535 encoder
.Encode(false);
537 // Increase the number of threads.
538 encoder
.Configure(16, 1915, 503, AOM_CBR
, AOM_USAGE_REALTIME
);
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
);
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();
565 aom_codec_enc_cfg_t cfg
;
567 ASSERT_EQ(aom_codec_enc_config_default(iface
, &cfg
, AOM_USAGE_REALTIME
),
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);
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
);
589 aom_codec_destroy(&enc
);
592 TEST(EncodeAPI
, PtsOrDurationTooBig
) {
593 // Initialize libaom encoder.
594 aom_codec_iface_t
*const iface
= aom_codec_av1_cx();
596 aom_codec_enc_cfg_t cfg
;
598 ASSERT_EQ(aom_codec_enc_config_default(iface
, &cfg
, AOM_USAGE_REALTIME
),
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);
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
);
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
);
630 ASSERT_EQ(aom_codec_encode(&enc
, image
, INT64_MAX
/ 1000000, 1, 0),
631 AOM_CODEC_INVALID_PARAM
);
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
);
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
);
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
);
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.
687 aom_codec_encode(&enc
, image
, /*pts=*/0, /*duration=*/1, /*flags=*/0),
690 aom_codec_encode(&enc
, image
, /*pts=*/1, /*duration=*/1, /*flags=*/0),
694 ASSERT_EQ(aom_codec_destroy(&enc
), AOM_CODEC_OK
);
698 const unsigned int kUsages
[] = {
700 #if !CONFIG_REALTIME_ONLY
701 AOM_USAGE_GOOD_QUALITY
,
706 INSTANTIATE_TEST_SUITE_P(All
, EncodeAPIParameterized
,
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();
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()
731 EXPECT_EQ(AOM_CODEC_OK
,
732 aom_codec_enc_config_default(iface
, &cfg
, AOM_USAGE_ALL_INTRA
));
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
),
744 ASSERT_EQ(aom_codec_enc_init(&enc
, iface
, &cfg
, AOM_CODEC_USE_PSNR
),
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
);
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
),
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
),
780 ASSERT_EQ(aom_codec_destroy(&enc
), AOM_CODEC_OK
);
782 #endif // !CONFIG_REALTIME_ONLY