xref: /aosp_15_r20/external/libaom/test/tools_common.sh (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1#!/bin/sh
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##  This file contains shell code shared by test scripts for libaom tools.
12
13# Use $AOM_TEST_TOOLS_COMMON_SH as a pseudo include guard.
14if [ -z "${AOM_TEST_TOOLS_COMMON_SH}" ]; then
15AOM_TEST_TOOLS_COMMON_SH=included
16
17set -e
18devnull='> /dev/null 2>&1'
19AOM_TEST_PREFIX=""
20TOOLS_COMMON_DIR=$(cd "$(dirname "$0")"; pwd)
21
22elog() {
23  echo "$@" 1>&2
24}
25
26vlog() {
27  if [ "${AOM_TEST_VERBOSE_OUTPUT}" = "yes" ]; then
28    echo "$@"
29  fi
30}
31
32# Sets $AOM_TOOL_TEST to the name specified by positional parameter one.
33test_begin() {
34  AOM_TOOL_TEST="${1}"
35}
36
37# Clears the AOM_TOOL_TEST variable after confirming that $AOM_TOOL_TEST matches
38# positional parameter one.
39test_end() {
40  if [ "$1" != "${AOM_TOOL_TEST}" ]; then
41    echo "FAIL completed test mismatch!."
42    echo "  completed test: ${1}"
43    echo "  active test: ${AOM_TOOL_TEST}."
44    return 1
45  fi
46  AOM_TOOL_TEST='<unset>'
47}
48
49# Echoes the target configuration being tested.
50test_configuration_target() {
51  aom_config_c="${LIBAOM_CONFIG_PATH}/config/aom_config.c"
52  # Clean up the cfg pointer line from aom_config.c for easier re-use by
53  # someone examining a failure in the example tests.
54  # 1. Run grep on aom_config.c for cfg and limit the results to 1.
55  # 2. Split the line using ' = ' as separator.
56  # 3. Abuse sed to consume the leading " and trailing "; from the assignment
57  #    to the cfg pointer.
58  cmake_config=$(awk -F ' = ' '/cfg/ { print $NF; exit }' "${aom_config_c}" \
59    | sed -e s/\"// -e s/\"\;//)
60  echo cmake generated via command: cmake path/to/aom ${cmake_config}
61}
62
63# Trap function used for failure reports and tool output directory removal.
64# When the contents of $AOM_TOOL_TEST do not match the string '<unset>', reports
65# failure of test stored in $AOM_TOOL_TEST.
66cleanup() {
67  if [ -n "${AOM_TOOL_TEST}" ] && [ "${AOM_TOOL_TEST}" != '<unset>' ]; then
68    echo "FAIL: $AOM_TOOL_TEST"
69  fi
70  if [ "${AOM_TEST_PRESERVE_OUTPUT}" = "yes" ]; then
71    return
72  fi
73  if [ -n "${AOM_TEST_OUTPUT_DIR}" ] && [ -d "${AOM_TEST_OUTPUT_DIR}" ]; then
74    rm -rf "${AOM_TEST_OUTPUT_DIR}"
75  fi
76}
77
78# Echoes the version string assigned to the VERSION_STRING_NOSP variable defined
79# in $LIBAOM_CONFIG_PATH/config/aom_version.h to stdout.
80cmake_version() {
81  aom_version_h="${LIBAOM_CONFIG_PATH}/config/aom_version.h"
82
83  # Find VERSION_STRING_NOSP line, split it with '"' and print the next to last
84  # field to output the version string to stdout.
85  aom_version=$(awk -F \" '/VERSION_STRING_NOSP/ {print $(NF-1)}' \
86    "${aom_version_h}")
87  echo "v${aom_version}"
88}
89
90# Echoes current git version as reported by running 'git describe', or the
91# version used by the cmake build when git is unavailable.
92source_version() {
93  if git --version > /dev/null 2>&1; then
94    git -C "${TOOLS_COMMON_DIR}" describe
95  else
96    cmake_version
97  fi
98}
99
100# Echoes warnings to stdout when source version and CMake build generated
101# version are out of sync.
102check_version_strings() {
103  cmake_version=$(cmake_version)
104  source_version=$(source_version)
105
106  if [ "${cmake_version}" != "${source_version}" ]; then
107    echo "Warning: version has changed since last cmake run."
108    vlog "  cmake version: ${cmake_version} version now: ${source_version}"
109  fi
110}
111
112# $1 is the name of an environment variable containing a directory name to
113# test.
114test_env_var_dir() {
115  local dir=$(eval echo "\${$1}")
116  if [ ! -d "${dir}" ]; then
117    elog "'${dir}': No such directory"
118    elog "The $1 environment variable must be set to a valid directory."
119    return 1
120  fi
121}
122
123# This script requires that the LIBAOM_BIN_PATH, LIBAOM_CONFIG_PATH, and
124# LIBAOM_TEST_DATA_PATH variables are in the environment: Confirm that
125# the variables are set and that they all evaluate to directory paths.
126verify_aom_test_environment() {
127  test_env_var_dir "LIBAOM_BIN_PATH" \
128    && test_env_var_dir "LIBAOM_CONFIG_PATH" \
129    && test_env_var_dir "LIBAOM_TEST_DATA_PATH"
130}
131
132# Greps aom_config.h in LIBAOM_CONFIG_PATH for positional parameter one, which
133# should be a LIBAOM preprocessor flag. Echoes yes to stdout when the feature
134# is available.
135aom_config_option_enabled() {
136  aom_config_option="${1}"
137  aom_config_file="${LIBAOM_CONFIG_PATH}/config/aom_config.h"
138  config_line=$(grep "${aom_config_option}" "${aom_config_file}")
139  if echo "${config_line}" | egrep -q '1$'; then
140    echo yes
141  fi
142}
143
144# Echoes yes when output of test_configuration_target() contains win32 or win64.
145is_windows_target() {
146  if test_configuration_target \
147     | grep -q -e win32 -e win64 > /dev/null 2>&1; then
148    echo yes
149  fi
150}
151
152# Echoes path to $1 when it's executable and exists in one of the directories
153# included in $tool_paths, or an empty string. Caller is responsible for testing
154# the string once the function returns.
155aom_tool_path() {
156  local tool_name="$1"
157  local root_path="${LIBAOM_BIN_PATH}"
158  local suffix="${AOM_TEST_EXE_SUFFIX}"
159  local tool_paths="\
160    ${root_path}/${tool_name}${suffix} \
161    ${root_path}/../${tool_name}${suffix} \
162    ${root_path}/tools/${tool_name}${suffix} \
163    ${root_path}/../tools/${tool_name}${suffix}"
164
165  local toolpath=""
166
167  for tool_path in ${tool_paths}; do
168    if [ -x "${tool_path}" ] && [ -f "${tool_path}" ]; then
169      echo "${tool_path}"
170      return 0
171    fi
172  done
173
174  return 1
175}
176
177# Echoes yes to stdout when the file named by positional parameter one exists
178# in LIBAOM_BIN_PATH, and is executable.
179aom_tool_available() {
180  local tool_name="$1"
181  local tool="${LIBAOM_BIN_PATH}/${tool_name}${AOM_TEST_EXE_SUFFIX}"
182  [ -x "${tool}" ] && echo yes
183}
184
185# Echoes yes to stdout when aom_config_option_enabled() reports yes for
186# CONFIG_AV1_DECODER.
187av1_decode_available() {
188  [ "$(aom_config_option_enabled CONFIG_AV1_DECODER)" = "yes" ] && echo yes
189}
190
191# Echoes yes to stdout when aom_config_option_enabled() reports yes for
192# CONFIG_AV1_ENCODER.
193av1_encode_available() {
194  [ "$(aom_config_option_enabled CONFIG_AV1_ENCODER)" = "yes" ] && echo yes
195}
196
197# Echoes "fast" encode params for use with aomenc.
198aomenc_encode_test_fast_params() {
199  echo "--cpu-used=2
200        --limit=${AV1_ENCODE_TEST_FRAME_LIMIT}
201        --lag-in-frames=0
202        --test-decode=fatal"
203}
204
205# Echoes realtime encode params for use with aomenc.
206aomenc_encode_test_rt_params() {
207  echo "--limit=${AV1_ENCODE_TEST_FRAME_LIMIT}
208        --test-decode=fatal
209        --enable-tpl-model=0
210        --deltaq-mode=0
211        --enable-order-hint=0
212        --profile=0
213        --static-thresh=0
214        --end-usage=cbr
215        --cpu-used=7
216        --passes=1
217        --usage=1
218        --lag-in-frames=0
219        --aq-mode=3
220        --enable-obmc=0
221        --enable-warped-motion=0
222        --enable-ref-frame-mvs=0
223        --enable-cdef=1
224        --enable-order-hint=0
225        --coeff-cost-upd-freq=3
226        --mode-cost-upd-freq=3
227        --mv-cost-upd-freq=3"
228}
229
230# Echoes yes to stdout when aom_config_option_enabled() reports yes for
231# CONFIG_AV1_HIGHBITDEPTH.
232highbitdepth_available() {
233  [ "$(aom_config_option_enabled CONFIG_AV1_HIGHBITDEPTH)" = "yes" ] && echo yes
234}
235
236# Echoes yes to stdout when aom_config_option_enabled() reports yes for
237# CONFIG_WEBM_IO.
238webm_io_available() {
239  [ "$(aom_config_option_enabled CONFIG_WEBM_IO)" = "yes" ] && echo yes
240}
241
242# Echoes yes to stdout when aom_config_option_enabled() reports yes for
243# CONFIG_REALTIME_ONLY.
244realtime_only_build() {
245  [ "$(aom_config_option_enabled CONFIG_REALTIME_ONLY)" = "yes" ] && echo yes
246}
247
248# Filters strings from $1 using the filter specified by $2. Filter behavior
249# depends on the presence of $3. When $3 is present, strings that match the
250# filter are excluded. When $3 is omitted, strings matching the filter are
251# included.
252# The filtered result is echoed to stdout.
253filter_strings() {
254  strings=${1}
255  filter=${2}
256  exclude=${3}
257
258  if [ -n "${exclude}" ]; then
259    # When positional parameter three exists the caller wants to remove strings.
260    # Tell grep to invert matches using the -v argument.
261    exclude='-v'
262  else
263    unset exclude
264  fi
265
266  if [ -n "${filter}" ]; then
267    for s in ${strings}; do
268      if echo "${s}" | egrep -q ${exclude} "${filter}" > /dev/null 2>&1; then
269        filtered_strings="${filtered_strings} ${s}"
270      fi
271    done
272  else
273    filtered_strings="${strings}"
274  fi
275  echo "${filtered_strings}"
276}
277
278# Runs user test functions passed via positional parameters one and two.
279# Functions in positional parameter one are treated as environment verification
280# functions and are run unconditionally. Functions in positional parameter two
281# are run according to the rules specified in aom_test_usage().
282run_tests() {
283  local env_tests="verify_aom_test_environment $1"
284  local tests_to_filter="$2"
285  local test_name="${AOM_TEST_NAME}"
286
287  if [ -z "${test_name}" ]; then
288    test_name="$(basename "${0%.*}")"
289  fi
290
291  if [ "${AOM_TEST_RUN_DISABLED_TESTS}" != "yes" ]; then
292    # Filter out DISABLED tests.
293    tests_to_filter=$(filter_strings "${tests_to_filter}" ^DISABLED exclude)
294  fi
295
296  if [ -n "${AOM_TEST_FILTER}" ]; then
297    # Remove tests not matching the user's filter.
298    tests_to_filter=$(filter_strings "${tests_to_filter}" ${AOM_TEST_FILTER})
299  fi
300
301  # User requested test listing: Dump test names and return.
302  if [ "${AOM_TEST_LIST_TESTS}" = "yes" ]; then
303    for test_name in $tests_to_filter; do
304      echo ${test_name}
305    done
306    return
307  fi
308
309  # Don't bother with the environment tests if everything else was disabled.
310  [ -z "${tests_to_filter}" ] && return
311
312  # Combine environment and actual tests.
313  local tests_to_run="${env_tests} ${tests_to_filter}"
314
315  # av1_c_vs_simd_encode is a standalone test, and it doesn't need to check the
316  # version string.
317  if [ "${test_name}" != "av1_c_vs_simd_encode" ]; then
318    check_version_strings
319  fi
320
321  # Run tests.
322  for test in ${tests_to_run}; do
323    test_begin "${test}"
324    vlog "  RUN  ${test}"
325    "${test}"
326    vlog "  PASS ${test}"
327    test_end "${test}"
328  done
329
330  local tested_config="$(test_configuration_target) @ $(source_version)"
331  echo "${test_name}: Done, all tests pass for ${tested_config}."
332}
333
334aom_test_usage() {
335cat << EOF
336  Usage: ${0##*/} [arguments]
337    --bin-path <path to libaom binaries directory>
338    --config-path <path to libaom config directory>
339    --filter <filter>: User test filter. Only tests matching filter are run.
340    --run-disabled-tests: Run disabled tests.
341    --help: Display this message and exit.
342    --test-data-path <path to libaom test data directory>
343    --show-program-output: Shows output from all programs being tested.
344    --prefix: Allows for a user specified prefix to be inserted before all test
345              programs. Grants the ability, for example, to run test programs
346              within valgrind.
347    --list-tests: List all test names and exit without actually running tests.
348    --verbose: Verbose output.
349
350    When the --bin-path option is not specified the script attempts to use
351    \$LIBAOM_BIN_PATH and then the current directory.
352
353    When the --config-path option is not specified the script attempts to use
354    \$LIBAOM_CONFIG_PATH and then the current directory.
355
356    When the -test-data-path option is not specified the script attempts to use
357    \$LIBAOM_TEST_DATA_PATH and then the current directory.
358EOF
359}
360
361# Returns non-zero (failure) when required environment variables are empty
362# strings.
363aom_test_check_environment() {
364  if [ -z "${LIBAOM_BIN_PATH}" ] || \
365     [ -z "${LIBAOM_CONFIG_PATH}" ] || \
366     [ -z "${LIBAOM_TEST_DATA_PATH}" ]; then
367    return 1
368  fi
369}
370
371# Echo aomenc command line parameters allowing use of a raw yuv file as
372# input to aomenc.
373yuv_raw_input() {
374  echo ""${YUV_RAW_INPUT}"
375       --width="${YUV_RAW_INPUT_WIDTH}"
376       --height="${YUV_RAW_INPUT_HEIGHT}""
377}
378
379# Do a small encode for testing decoders.
380encode_yuv_raw_input_av1() {
381  if [ "$(av1_encode_available)" = "yes" ]; then
382    local output="$1"
383    local encoder="$(aom_tool_path aomenc)"
384    shift
385    eval "${encoder}" $(yuv_raw_input) \
386      $(aomenc_encode_test_fast_params) \
387      --output="${output}" \
388      $@ \
389      ${devnull}
390
391    if [ ! -e "${output}" ]; then
392      elog "Output file does not exist."
393      return 1
394    fi
395  fi
396}
397
398# Parse the command line.
399while [ -n "$1" ]; do
400  case "$1" in
401    --bin-path)
402      LIBAOM_BIN_PATH="$2"
403      shift
404      ;;
405    --config-path)
406      LIBAOM_CONFIG_PATH="$2"
407      shift
408      ;;
409    --filter)
410      AOM_TEST_FILTER="$2"
411      shift
412      ;;
413    --run-disabled-tests)
414      AOM_TEST_RUN_DISABLED_TESTS=yes
415      ;;
416    --help)
417      aom_test_usage
418      exit
419      ;;
420    --test-data-path)
421      LIBAOM_TEST_DATA_PATH="$2"
422      shift
423      ;;
424    --prefix)
425      AOM_TEST_PREFIX="$2"
426      shift
427      ;;
428    --verbose)
429      AOM_TEST_VERBOSE_OUTPUT=yes
430      ;;
431    --show-program-output)
432      devnull=
433      ;;
434    --list-tests)
435      AOM_TEST_LIST_TESTS=yes
436      ;;
437    *)
438      aom_test_usage
439      exit 1
440      ;;
441  esac
442  shift
443done
444
445# Handle running the tests from a build directory without arguments when running
446# the tests on *nix/macosx.
447LIBAOM_BIN_PATH="${LIBAOM_BIN_PATH:-.}"
448LIBAOM_CONFIG_PATH="${LIBAOM_CONFIG_PATH:-.}"
449LIBAOM_TEST_DATA_PATH="${LIBAOM_TEST_DATA_PATH:-.}"
450
451# Create a temporary directory for output files, and a trap to clean it up.
452if [ -n "${TMPDIR}" ]; then
453  AOM_TEST_TEMP_ROOT="${TMPDIR}"
454elif [ -n "${TEMPDIR}" ]; then
455  AOM_TEST_TEMP_ROOT="${TEMPDIR}"
456else
457  AOM_TEST_TEMP_ROOT=/tmp
458fi
459
460AOM_TEST_OUTPUT_DIR="${AOM_TEST_OUTPUT_DIR:-${AOM_TEST_TEMP_ROOT}/aom_test_$$}"
461
462if ! mkdir -p "${AOM_TEST_OUTPUT_DIR}" || \
463   [ ! -d "${AOM_TEST_OUTPUT_DIR}" ]; then
464  echo "${0##*/}: Cannot create output directory, giving up."
465  echo "${0##*/}:   AOM_TEST_OUTPUT_DIR=${AOM_TEST_OUTPUT_DIR}"
466  exit 1
467fi
468
469AOM_TEST_PRESERVE_OUTPUT=${AOM_TEST_PRESERVE_OUTPUT:-no}
470
471# This checking requires config/aom_config.c that is available in Jenkins
472# testing.
473if [ "$(is_windows_target)" = "yes" ]; then
474  AOM_TEST_EXE_SUFFIX=".exe"
475fi
476
477# Variables shared by tests.
478AV1_ENCODE_CPU_USED=${AV1_ENCODE_CPU_USED:-1}
479AV1_ENCODE_TEST_FRAME_LIMIT=${AV1_ENCODE_TEST_FRAME_LIMIT:-5}
480AV1_IVF_FILE="${AV1_IVF_FILE:-${AOM_TEST_OUTPUT_DIR}/av1.ivf}"
481AV1_OBU_ANNEXB_FILE="${AV1_OBU_ANNEXB_FILE:-${AOM_TEST_OUTPUT_DIR}/av1.annexb.obu}"
482AV1_OBU_SEC5_FILE="${AV1_OBU_SEC5_FILE:-${AOM_TEST_OUTPUT_DIR}/av1.section5.obu}"
483AV1_WEBM_FILE="${AV1_WEBM_FILE:-${AOM_TEST_OUTPUT_DIR}/av1.webm}"
484
485YUV_RAW_INPUT="${LIBAOM_TEST_DATA_PATH}/hantro_collage_w352h288.yuv"
486YUV_RAW_INPUT_WIDTH=352
487YUV_RAW_INPUT_HEIGHT=288
488
489Y4M_NOSQ_PAR_INPUT="${LIBAOM_TEST_DATA_PATH}/park_joy_90p_8_420_a10-1.y4m"
490Y4M_720P_INPUT="${LIBAOM_TEST_DATA_PATH}/niklas_1280_720_30.y4m"
491
492# Setup a trap function to clean up after tests complete.
493trap cleanup EXIT
494
495vlog "$(basename "${0%.*}") test configuration:
496  LIBAOM_BIN_PATH=${LIBAOM_BIN_PATH}
497  LIBAOM_CONFIG_PATH=${LIBAOM_CONFIG_PATH}
498  LIBAOM_TEST_DATA_PATH=${LIBAOM_TEST_DATA_PATH}
499  AOM_TEST_EXE_SUFFIX=${AOM_TEST_EXE_SUFFIX}
500  AOM_TEST_FILTER=${AOM_TEST_FILTER}
501  AOM_TEST_LIST_TESTS=${AOM_TEST_LIST_TESTS}
502  AOM_TEST_OUTPUT_DIR=${AOM_TEST_OUTPUT_DIR}
503  AOM_TEST_PREFIX=${AOM_TEST_PREFIX}
504  AOM_TEST_PRESERVE_OUTPUT=${AOM_TEST_PRESERVE_OUTPUT}
505  AOM_TEST_RUN_DISABLED_TESTS=${AOM_TEST_RUN_DISABLED_TESTS}
506  AOM_TEST_SHOW_PROGRAM_OUTPUT=${AOM_TEST_SHOW_PROGRAM_OUTPUT}
507  AOM_TEST_TEMP_ROOT=${AOM_TEST_TEMP_ROOT}
508  AOM_TEST_VERBOSE_OUTPUT=${AOM_TEST_VERBOSE_OUTPUT}
509  AV1_ENCODE_CPU_USED=${AV1_ENCODE_CPU_USED}
510  AV1_ENCODE_TEST_FRAME_LIMIT=${AV1_ENCODE_TEST_FRAME_LIMIT}
511  AV1_IVF_FILE=${AV1_IVF_FILE}
512  AV1_OBU_ANNEXB_FILE=${AV1_OBU_ANNEXB_FILE}
513  AV1_OBU_SEC5_FILE=${AV1_OBU_SEC5_FILE}
514  AV1_WEBM_FILE=${AV1_WEBM_FILE}
515  YUV_RAW_INPUT=${YUV_RAW_INPUT}
516  YUV_RAW_INPUT_WIDTH=${YUV_RAW_INPUT_WIDTH}
517  YUV_RAW_INPUT_HEIGHT=${YUV_RAW_INPUT_HEIGHT}
518  Y4M_NOSQ_PAR_INPUT=${Y4M_NOSQ_PAR_INPUT}"
519
520fi  # End $AOM_TEST_TOOLS_COMMON_SH pseudo include guard.
521