Bug 1882465 - Update .hg-annotate-ignore-revs and .git-blame-ignore-revs to reflect...
[gecko.git] / third_party / aom / test / tools_common.sh
blobcb9eba1727c128dc75a294ee0e57c354279280d0
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.
11 ## This file contains shell code shared by test scripts for libaom tools.
13 # Use $AOM_TEST_TOOLS_COMMON_SH as a pseudo include guard.
14 if [ -z "${AOM_TEST_TOOLS_COMMON_SH}" ]; then
15 AOM_TEST_TOOLS_COMMON_SH=included
17 set -e
18 devnull='> /dev/null 2>&1'
19 AOM_TEST_PREFIX=""
21 elog() {
22 echo "$@" 1>&2
25 vlog() {
26 if [ "${AOM_TEST_VERBOSE_OUTPUT}" = "yes" ]; then
27 echo "$@"
31 # Sets $AOM_TOOL_TEST to the name specified by positional parameter one.
32 test_begin() {
33 AOM_TOOL_TEST="${1}"
36 # Clears the AOM_TOOL_TEST variable after confirming that $AOM_TOOL_TEST matches
37 # positional parameter one.
38 test_end() {
39 if [ "$1" != "${AOM_TOOL_TEST}" ]; then
40 echo "FAIL completed test mismatch!."
41 echo " completed test: ${1}"
42 echo " active test: ${AOM_TOOL_TEST}."
43 return 1
45 AOM_TOOL_TEST='<unset>'
48 # Echoes the target configuration being tested.
49 test_configuration_target() {
50 aom_config_c="${LIBAOM_CONFIG_PATH}/config/aom_config.c"
51 # Clean up the cfg pointer line from aom_config.c for easier re-use by
52 # someone examining a failure in the example tests.
53 # 1. Run grep on aom_config.c for cfg and limit the results to 1.
54 # 2. Split the line using ' = ' as separator.
55 # 3. Abuse sed to consume the leading " and trailing "; from the assignment
56 # to the cfg pointer.
57 cmake_config=$(awk -F ' = ' '/cfg/ { print $NF; exit }' "${aom_config_c}" \
58 | sed -e s/\"// -e s/\"\;//)
59 echo cmake generated via command: cmake path/to/aom ${cmake_config}
62 # Trap function used for failure reports and tool output directory removal.
63 # When the contents of $AOM_TOOL_TEST do not match the string '<unset>', reports
64 # failure of test stored in $AOM_TOOL_TEST.
65 cleanup() {
66 if [ -n "${AOM_TOOL_TEST}" ] && [ "${AOM_TOOL_TEST}" != '<unset>' ]; then
67 echo "FAIL: $AOM_TOOL_TEST"
69 if [ "${AOM_TEST_PRESERVE_OUTPUT}" = "yes" ]; then
70 return
72 if [ -n "${AOM_TEST_OUTPUT_DIR}" ] && [ -d "${AOM_TEST_OUTPUT_DIR}" ]; then
73 rm -rf "${AOM_TEST_OUTPUT_DIR}"
77 # Echoes the version string assigned to the VERSION_STRING_NOSP variable defined
78 # in $LIBAOM_CONFIG_PATH/config/aom_version.h to stdout.
79 cmake_version() {
80 aom_version_h="${LIBAOM_CONFIG_PATH}/config/aom_version.h"
82 # Find VERSION_STRING_NOSP line, split it with '"' and print the next to last
83 # field to output the version string to stdout.
84 aom_version=$(awk -F \" '/VERSION_STRING_NOSP/ {print $(NF-1)}' \
85 "${aom_version_h}")
86 echo "v${aom_version}"
89 # Echoes current git version as reported by running 'git describe', or the
90 # version used by the cmake build when git is unavailable.
91 source_version() {
92 if git --version > /dev/null 2>&1; then
93 (cd "$(dirname "${0}")"
94 git describe)
95 else
96 cmake_version
100 # Echoes warnings to stdout when source version and CMake build generated
101 # version are out of sync.
102 check_version_strings() {
103 cmake_version=$(cmake_version)
104 source_version=$(source_version)
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}"
112 # $1 is the name of an environment variable containing a directory name to
113 # test.
114 test_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
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.
126 verify_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"
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.
135 aom_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
144 # Echoes yes when output of test_configuration_target() contains win32 or win64.
145 is_windows_target() {
146 if test_configuration_target \
147 | grep -q -e win32 -e win64 > /dev/null 2>&1; then
148 echo yes
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.
155 aom_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}"
165 local toolpath=""
167 for tool_path in ${tool_paths}; do
168 if [ -x "${tool_path}" ] && [ -f "${tool_path}" ]; then
169 echo "${tool_path}"
170 return 0
172 done
174 return 1
177 # Echoes yes to stdout when the file named by positional parameter one exists
178 # in LIBAOM_BIN_PATH, and is executable.
179 aom_tool_available() {
180 local tool_name="$1"
181 local tool="${LIBAOM_BIN_PATH}/${tool_name}${AOM_TEST_EXE_SUFFIX}"
182 [ -x "${tool}" ] && echo yes
185 # Echoes yes to stdout when aom_config_option_enabled() reports yes for
186 # CONFIG_AV1_DECODER.
187 av1_decode_available() {
188 [ "$(aom_config_option_enabled CONFIG_AV1_DECODER)" = "yes" ] && echo yes
191 # Echoes yes to stdout when aom_config_option_enabled() reports yes for
192 # CONFIG_AV1_ENCODER.
193 av1_encode_available() {
194 [ "$(aom_config_option_enabled CONFIG_AV1_ENCODER)" = "yes" ] && echo yes
197 # Echoes "fast" encode params for use with aomenc.
198 aomenc_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"
205 # Echoes realtime encode params for use with aomenc.
206 aomenc_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"
230 # Echoes yes to stdout when aom_config_option_enabled() reports yes for
231 # CONFIG_AV1_HIGHBITDEPTH.
232 highbitdepth_available() {
233 [ "$(aom_config_option_enabled CONFIG_AV1_HIGHBITDEPTH)" = "yes" ] && echo yes
236 # Echoes yes to stdout when aom_config_option_enabled() reports yes for
237 # CONFIG_WEBM_IO.
238 webm_io_available() {
239 [ "$(aom_config_option_enabled CONFIG_WEBM_IO)" = "yes" ] && echo yes
242 # Echoes yes to stdout when aom_config_option_enabled() reports yes for
243 # CONFIG_REALTIME_ONLY.
244 realtime_only_build() {
245 [ "$(aom_config_option_enabled CONFIG_REALTIME_ONLY)" = "yes" ] && echo yes
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.
253 filter_strings() {
254 strings=${1}
255 filter=${2}
256 exclude=${3}
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
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}"
271 done
272 else
273 filtered_strings="${strings}"
275 echo "${filtered_strings}"
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().
282 run_tests() {
283 local env_tests="verify_aom_test_environment $1"
284 local tests_to_filter="$2"
285 local test_name="${AOM_TEST_NAME}"
287 if [ -z "${test_name}" ]; then
288 test_name="$(basename "${0%.*}")"
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)
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})
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
309 # Don't bother with the environment tests if everything else was disabled.
310 [ -z "${tests_to_filter}" ] && return
312 # Combine environment and actual tests.
313 local tests_to_run="${env_tests} ${tests_to_filter}"
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
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
330 local tested_config="$(test_configuration_target) @ $(source_version)"
331 echo "${test_name}: Done, all tests pass for ${tested_config}."
334 aom_test_usage() {
335 cat << 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.
350 When the --bin-path option is not specified the script attempts to use
351 \$LIBAOM_BIN_PATH and then the current directory.
353 When the --config-path option is not specified the script attempts to use
354 \$LIBAOM_CONFIG_PATH and then the current directory.
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.
361 # Returns non-zero (failure) when required environment variables are empty
362 # strings.
363 aom_test_check_environment() {
364 if [ -z "${LIBAOM_BIN_PATH}" ] || \
365 [ -z "${LIBAOM_CONFIG_PATH}" ] || \
366 [ -z "${LIBAOM_TEST_DATA_PATH}" ]; then
367 return 1
371 # Echo aomenc command line parameters allowing use of a raw yuv file as
372 # input to aomenc.
373 yuv_raw_input() {
374 echo ""${YUV_RAW_INPUT}"
375 --width="${YUV_RAW_INPUT_WIDTH}"
376 --height="${YUV_RAW_INPUT_HEIGHT}""
379 # Do a small encode for testing decoders.
380 encode_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}
391 if [ ! -e "${output}" ]; then
392 elog "Output file does not exist."
393 return 1
398 # Parse the command line.
399 while [ -n "$1" ]; do
400 case "$1" in
401 --bin-path)
402 LIBAOM_BIN_PATH="$2"
403 shift
405 --config-path)
406 LIBAOM_CONFIG_PATH="$2"
407 shift
409 --filter)
410 AOM_TEST_FILTER="$2"
411 shift
413 --run-disabled-tests)
414 AOM_TEST_RUN_DISABLED_TESTS=yes
416 --help)
417 aom_test_usage
418 exit
420 --test-data-path)
421 LIBAOM_TEST_DATA_PATH="$2"
422 shift
424 --prefix)
425 AOM_TEST_PREFIX="$2"
426 shift
428 --verbose)
429 AOM_TEST_VERBOSE_OUTPUT=yes
431 --show-program-output)
432 devnull=
434 --list-tests)
435 AOM_TEST_LIST_TESTS=yes
438 aom_test_usage
439 exit 1
441 esac
442 shift
443 done
445 # Handle running the tests from a build directory without arguments when running
446 # the tests on *nix/macosx.
447 LIBAOM_BIN_PATH="${LIBAOM_BIN_PATH:-.}"
448 LIBAOM_CONFIG_PATH="${LIBAOM_CONFIG_PATH:-.}"
449 LIBAOM_TEST_DATA_PATH="${LIBAOM_TEST_DATA_PATH:-.}"
451 # Create a temporary directory for output files, and a trap to clean it up.
452 if [ -n "${TMPDIR}" ]; then
453 AOM_TEST_TEMP_ROOT="${TMPDIR}"
454 elif [ -n "${TEMPDIR}" ]; then
455 AOM_TEST_TEMP_ROOT="${TEMPDIR}"
456 else
457 AOM_TEST_TEMP_ROOT=/tmp
460 AOM_TEST_OUTPUT_DIR="${AOM_TEST_OUTPUT_DIR:-${AOM_TEST_TEMP_ROOT}/aom_test_$$}"
462 if ! 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
469 AOM_TEST_PRESERVE_OUTPUT=${AOM_TEST_PRESERVE_OUTPUT:-no}
471 # This checking requires config/aom_config.c that is available in Jenkins
472 # testing.
473 if [ "$(is_windows_target)" = "yes" ]; then
474 AOM_TEST_EXE_SUFFIX=".exe"
477 # Variables shared by tests.
478 AV1_ENCODE_CPU_USED=${AV1_ENCODE_CPU_USED:-1}
479 AV1_ENCODE_TEST_FRAME_LIMIT=${AV1_ENCODE_TEST_FRAME_LIMIT:-5}
480 AV1_IVF_FILE="${AV1_IVF_FILE:-${AOM_TEST_OUTPUT_DIR}/av1.ivf}"
481 AV1_OBU_ANNEXB_FILE="${AV1_OBU_ANNEXB_FILE:-${AOM_TEST_OUTPUT_DIR}/av1.annexb.obu}"
482 AV1_OBU_SEC5_FILE="${AV1_OBU_SEC5_FILE:-${AOM_TEST_OUTPUT_DIR}/av1.section5.obu}"
483 AV1_WEBM_FILE="${AV1_WEBM_FILE:-${AOM_TEST_OUTPUT_DIR}/av1.webm}"
485 YUV_RAW_INPUT="${LIBAOM_TEST_DATA_PATH}/hantro_collage_w352h288.yuv"
486 YUV_RAW_INPUT_WIDTH=352
487 YUV_RAW_INPUT_HEIGHT=288
489 Y4M_NOSQ_PAR_INPUT="${LIBAOM_TEST_DATA_PATH}/park_joy_90p_8_420_a10-1.y4m"
490 Y4M_720P_INPUT="${LIBAOM_TEST_DATA_PATH}/niklas_1280_720_30.y4m"
492 # Setup a trap function to clean up after tests complete.
493 trap cleanup EXIT
495 vlog "$(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}"
520 fi # End $AOM_TEST_TOOLS_COMMON_SH pseudo include guard.