Add low-complexity decode mode test This is to monitor the decoder speed change for each submitted patch. Change-Id: I4618ef8672c7bb0aa89ffb67860bd6669fd0f824
diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a6551e..d9973c4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt
@@ -801,6 +801,15 @@ list(APPEND AOM_APP_TARGETS lightfield_bitstream_parsing) endif() +if(ENABLE_EXAMPLES AND CONFIG_AV1_ENCODER) + add_executable(low_complexity_mode_encoder + "${AOM_ROOT}/examples/low_complexity_mode_encoder.c" + $<TARGET_OBJECTS:aom_common_app_util> + $<TARGET_OBJECTS:aom_encoder_app_util>) + list(APPEND AOM_EXAMPLE_TARGETS low_complexity_mode_encoder) + list(APPEND AOM_APP_TARGETS low_complexity_mode_encoder) +endif() + foreach(aom_app ${AOM_APP_TARGETS}) target_link_libraries(${aom_app} ${AOM_LIB_LINK_TYPE} aom) endforeach()
diff --git a/docs.cmake b/docs.cmake index 154cb0d..1b76161 100644 --- a/docs.cmake +++ b/docs.cmake
@@ -199,6 +199,16 @@ "Lightfield bitstream parsing example.") endif() +if(CONFIG_AV1_DECODER AND CONFIG_AV1_ENCODER) + set(AOM_DOXYGEN_EXAMPLE_SOURCES + ${AOM_DOXYGEN_EXAMPLE_SOURCES} + "${AOM_ROOT}/examples/low_complexity_mode_encoder.c") + + set(AOM_DOXYGEN_EXAMPLE_DESCRIPTIONS + ${AOM_DOXYGEN_EXAMPLE_DESCRIPTIONS} + "Low complexity decode mode encoder example.") +endif() + # Iterates over list named by $list_name and appends each item to $AOM_DOXYFILE # as values assigned to $var_name with no line breaks between list items. # Appends a new line after the entire config variable is expanded.
diff --git a/examples/low_complexity_mode_encoder.c b/examples/low_complexity_mode_encoder.c new file mode 100644 index 0000000..c80326f --- /dev/null +++ b/examples/low_complexity_mode_encoder.c
@@ -0,0 +1,175 @@ +/* + * Copyright (c) 2026, Alliance for Open Media. All rights reserved. + * + * This source code is subject to the terms of the BSD 2 Clause License and + * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License + * was not distributed with this source code in the LICENSE file, you can + * obtain it at www.aomedia.org/license/software. If the Alliance for Open + * Media Patent License 1.0 was not distributed with this source code in the + * PATENTS file, you can obtain it at www.aomedia.org/license/patent. + */ + +// Low Complexity (LC) Mode Encoder +// ================================ +// +// This is an example of a low complexity mode encoder. It takes an input +// file in Y4M format, passes it through the encoder with LC mode on, and +// writes the compressed frames to disk in IVF format. + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "aom/aom_encoder.h" +#include "aom/aomcx.h" +#include "common/tools_common.h" +#include "common/video_writer.h" + +static const char *exec_name; + +void usage_exit(void) { + fprintf(stderr, + "Usage: %s <width> <height> <infile> <outfile> " + "<keyframe-interval> <frames to encode>\n", + exec_name); + exit(EXIT_FAILURE); +} + +static int encode_frame(aom_codec_ctx_t *codec, aom_image_t *img, + int frame_index, int flags, AvxVideoWriter *writer) { + int got_pkts = 0; + aom_codec_iter_t iter = NULL; + const aom_codec_cx_pkt_t *pkt = NULL; + const aom_codec_err_t res = + aom_codec_encode(codec, img, frame_index, 1, flags); + if (res != AOM_CODEC_OK) die_codec(codec, "Failed to encode frame"); + + while ((pkt = aom_codec_get_cx_data(codec, &iter)) != NULL) { + got_pkts = 1; + + if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) { + const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0; + if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf, + pkt->data.frame.sz, + pkt->data.frame.pts)) { + die_codec(codec, "Failed to write compressed frame"); + } + printf(keyframe ? "K" : "."); + fflush(stdout); + } + } + + return got_pkts; +} + +int main(int argc, char **argv) { + FILE *infile = NULL; + aom_codec_ctx_t codec; + aom_codec_enc_cfg_t cfg; + int frame_count = 0; + aom_image_t raw; + aom_codec_err_t res; + AvxVideoInfo info; + AvxVideoWriter *writer = NULL; + const int fps = 30; + const int bitrate = 400; + int keyframe_interval = 0; + int max_frames = 0; + int frames_encoded = 0; + const char *codec_arg = NULL; + const char *width_arg = NULL; + const char *height_arg = NULL; + const char *infile_arg = NULL; + const char *outfile_arg = NULL; + const char *keyframe_interval_arg = NULL; + const int usage = 0; // AOM_USAGE_GOOD_QUALITY + const int speed = 2; + + exec_name = argv[0]; + + // Clear explicitly, as simply assigning "{ 0 }" generates + // "missing-field-initializers" warning in some compilers. + memset(&info, 0, sizeof(info)); + + if (argc != 9) die("Invalid number of arguments"); + + codec_arg = argv[1]; + width_arg = argv[2]; + height_arg = argv[3]; + infile_arg = argv[4]; + outfile_arg = argv[5]; + keyframe_interval_arg = argv[6]; + max_frames = (int)strtol(argv[7], NULL, 0); + + aom_codec_iface_t *encoder = get_aom_encoder_by_short_name(codec_arg); + if (!encoder) die("Unsupported codec."); + + info.codec_fourcc = get_fourcc_by_aom_encoder(encoder); + info.frame_width = (int)strtol(width_arg, NULL, 0); + info.frame_height = (int)strtol(height_arg, NULL, 0); + info.time_base.numerator = 1; + info.time_base.denominator = fps; + + if (info.frame_width <= 0 || info.frame_height <= 0 || + (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) { + die("Invalid frame size: %dx%d", info.frame_width, info.frame_height); + } + + if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, info.frame_width, + info.frame_height, 1)) { + die("Failed to allocate image."); + } + + keyframe_interval = (int)strtol(keyframe_interval_arg, NULL, 0); + if (keyframe_interval < 0) die("Invalid keyframe interval value."); + + printf("Using %s\n", aom_codec_iface_name(encoder)); + + res = aom_codec_enc_config_default(encoder, &cfg, usage); + if (res) die_codec(&codec, "Failed to get default codec config."); + + cfg.g_w = info.frame_width; + cfg.g_h = info.frame_height; + cfg.g_timebase.num = info.time_base.numerator; + cfg.g_timebase.den = info.time_base.denominator; + cfg.rc_target_bitrate = bitrate; + + writer = aom_video_writer_open(outfile_arg, kContainerIVF, &info); + if (!writer) die("Failed to open %s for writing.", outfile_arg); + + if (!(infile = fopen(infile_arg, "rb"))) + die("Failed to open %s for reading.", infile_arg); + + if (aom_codec_enc_init(&codec, encoder, &cfg, 0)) + die("Failed to initialize encoder"); + + if (aom_codec_control(&codec, AOME_SET_CPUUSED, speed)) + die_codec(&codec, "Failed to set cpu-used"); + + if (aom_codec_control(&codec, AV1E_SET_ENABLE_LOW_COMPLEXITY_DECODE, 1)) + die_codec(&codec, "Failed to set low-complexity mode"); + + // Encode frames. + while (aom_img_read(&raw, infile)) { + int flags = 0; + if (keyframe_interval > 0 && frame_count % keyframe_interval == 0) + flags |= AOM_EFLAG_FORCE_KF; + encode_frame(&codec, &raw, frame_count++, flags, writer); + frames_encoded++; + if (max_frames > 0 && frames_encoded >= max_frames) break; + } + + // Flush encoder. + while (encode_frame(&codec, NULL, -1, 0, writer)) continue; + + printf("\n"); + fclose(infile); + printf("Processed %d frames.\n", frame_count); + + aom_img_free(&raw); + if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec."); + + aom_video_writer_close(writer); + + return EXIT_SUCCESS; +}
diff --git a/test/examples.sh b/test/examples.sh index 6869d02..7eb4464 100755 --- a/test/examples.sh +++ b/test/examples.sh
@@ -19,7 +19,7 @@ exclude_list="best_encode examples run_encodes tools_common" if [ "$(realtime_only_build)" = "yes" ]; then - exclude_list="${exclude_list} twopass_encoder simple_decoder lightfield_test" + exclude_list="${exclude_list} twopass_encoder simple_decoder lightfield_test low_complexity_decode_mode_test" fi # Filter out the scripts in $exclude_list.
diff --git a/test/low_complexity_test.sh b/test/low_complexity_test.sh new file mode 100755 index 0000000..fd494db --- /dev/null +++ b/test/low_complexity_test.sh
@@ -0,0 +1,88 @@ +#!/bin/sh +## Copyright (c) 2026, Alliance for Open Media. All rights reserved. +## +## This source code is subject to the terms of the BSD 2 Clause License and +## the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License +## was not distributed with this source code in the LICENSE file, you can +## obtain it at www.aomedia.org/license/software. If the Alliance for Open +## Media Patent License 1.0 was not distributed with this source code in the +## PATENTS file, you can obtain it at www.aomedia.org/license/patent. +## +## This file tests the libaom low-complexity decode mode using simple_encoder +## and simple_decoder example. The decoder outputs the instruction counts that +## can be used for decoder speed monitoring. +## +. $(dirname $0)/tools_common.sh + +# Environment check: $infile is required. +low_complexity_test_verify_environment() { + if [ ! "$(av1_encode_available)" = "yes" ] || \ + [ ! "$(av1_decode_available)" = "yes" ]; then + echo "Encoder and decoder needs to be available." + return 1 + fi + + local infile="${LIBAOM_TEST_DATA_PATH}/SDR_Sports_6mug_608p_30fps_90f.yuv" + if [ ! -e "${infile}" ]; then + echo "Libaom test data must exist in LIBAOM_TEST_DATA_PATH." + return 1 + fi + + command -v perf >/dev/null 2>&1 + if [ $? -ne 0 ]; then + echo "Perf does not exist. Please install it." + return 1 + fi +} + +# Runs libaom low-complexity decode mode test +low_complexity_mode_test() { + local img_width=608 + local img_height=1080 + + # Encode in low-complexity decode mode + local encoder="${LIBAOM_BIN_PATH}/low_complexity_mode_encoder${AOM_TEST_EXE_SUFFIX}" + local input_file="${LIBAOM_TEST_DATA_PATH}/SDR_Sports_6mug_608p_30fps_90f.yuv" + local ivf_file="${AOM_TEST_OUTPUT_DIR}/low_complexity_mode_encoder.bin" + + if [ ! -x "${encoder}" ]; then + elog "${encoder} does not exist or is not executable." + return 1 + fi + + echo "The LC mode encoder is started." + eval "${AOM_TEST_PREFIX}" "${encoder}" av1 "${img_width}" \ + "${img_height}" "${input_file}" "${ivf_file}" 9999 0 90 \ + ${devnull} || return 1 + + [ -e "${ivf_file}" ] || return 1 + echo "The LC mode encoder is completed." + + # Use simple_decoder to decode the generated bitstream and collect the + # instruction counts. + local decoder="${LIBAOM_BIN_PATH}/simple_decoder${AOM_TEST_EXE_SUFFIX}" + local perfstat_file="${AOM_TEST_OUTPUT_DIR}/perfstat.txt" + local perf_prefix="perf stat --no-big-num -e instructions:u -o ${perfstat_file}" + local output_file="${AOM_TEST_OUTPUT_DIR}/low_complexity_mode_encoder.out" + + if [ ! -x "${decoder}" ]; then + elog "${decoder} does not exist or is not executable." + return 1 + fi + + local dec_cmd="$(echo "${perf_prefix}" "${AOM_TEST_PREFIX}" "${decoder}" \ + "${ivf_file}" "${output_file}" ${devnull})" + echo "$dec_cmd" + eval "$dec_cmd" || return 1 + + # Get perf user instruction count in thousands from perf output + local instruction_count="0" + local line=$( cat ${perfstat_file} | grep "instructions:u" ) + instruction_count=${line%instructions:u*} + echo ${instruction_count} +} + +low_complexity_decode_mode_test="low_complexity_mode_test" + +run_tests low_complexity_test_verify_environment \ + "${low_complexity_decode_mode_test}"