Improved aom_smooth_predictor_16x 32,16,8
[aom.git] / test / tools_common.sh
blob7c4b5605599aa941e2934ae199e03a8c322ccd46
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=""
20 readonly AOM_ENCODE_TEST_FRAME_LIMIT=5
22 elog() {
23 echo "$@" 1>&2
26 vlog() {
27 if [ "${AOM_TEST_VERBOSE_OUTPUT}" = "yes" ]; then
28 echo "$@"
32 # Sets $AOM_TOOL_TEST to the name specified by positional parameter one.
33 test_begin() {
34 AOM_TOOL_TEST="${1}"
37 # Clears the AOM_TOOL_TEST variable after confirming that $AOM_TOOL_TEST matches
38 # positional parameter one.
39 test_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
46 AOM_TOOL_TEST='<unset>'
49 # Echoes the target configuration being tested.
50 test_configuration_target() {
51 aom_config_mk="${LIBAOM_CONFIG_PATH}/config.mk"
52 # TODO(tomfinegan): Remove the parts requiring config.mk when the configure
53 # script is removed from the repository.
54 if [ ! -f "${aom_config_mk}" ]; then
55 aom_config_c="${LIBAOM_CONFIG_PATH}/aom_config.c"
56 # Clean up the cfg pointer line from aom_config.c for easier re-use by
57 # someone examining a failure in the example tests.
58 # 1. Run grep on aom_config.c for cfg and limit the results to 1.
59 # 2. Split the line using ' = ' as separator.
60 # 3. Abuse sed to consume the leading " and trailing "; from the assignment
61 # to the cfg pointer.
62 cmake_config=$(awk -F ' = ' '/cfg/ { print $NF; exit }' "${aom_config_c}" \
63 | sed -e s/\"// -e s/\"\;//)
64 echo cmake generated via command: cmake path/to/aom ${cmake_config}
65 return
67 # Find the TOOLCHAIN line, split it using ':=' as the field separator, and
68 # print the last field to get the value. Then pipe the value to tr to consume
69 # any leading/trailing spaces while allowing tr to echo the output to stdout.
70 awk -F ':=' '/TOOLCHAIN/ { print $NF }' "${aom_config_mk}" | tr -d ' '
73 # Trap function used for failure reports and tool output directory removal.
74 # When the contents of $AOM_TOOL_TEST do not match the string '<unset>', reports
75 # failure of test stored in $AOM_TOOL_TEST.
76 cleanup() {
77 if [ -n "${AOM_TOOL_TEST}" ] && [ "${AOM_TOOL_TEST}" != '<unset>' ]; then
78 echo "FAIL: $AOM_TOOL_TEST"
80 if [ -n "${AOM_TEST_OUTPUT_DIR}" ] && [ -d "${AOM_TEST_OUTPUT_DIR}" ]; then
81 rm -rf "${AOM_TEST_OUTPUT_DIR}"
85 # Echoes the version string assigned to the VERSION_STRING_NOSP variable defined
86 # in $LIBAOM_CONFIG_PATH/aom_version.h to stdout.
87 cmake_version() {
88 aom_version_h="${LIBAOM_CONFIG_PATH}/aom_version.h"
90 # Find VERSION_STRING_NOSP line, split it with '"' and print the next to last
91 # field to output the version string to stdout.
92 aom_version=$(awk -F \" '/VERSION_STRING_NOSP/ {print $(NF-1)}' \
93 "${aom_version_h}")
94 echo "v${aom_version}"
97 # Echoes current git version as reported by running 'git describe', or the
98 # version used by the cmake build when git is unavailable.
99 source_version() {
100 if git --version > /dev/null 2>&1; then
101 (cd "$(dirname "${0}")"
102 git describe)
103 else
104 cmake_version
108 # Echoes warnings to stdout when source version and CMake build generated
109 # version are out of sync.
110 check_version_strings() {
111 cmake_version=$(cmake_version)
112 source_version=$(source_version)
114 if [ "${cmake_version}" != "${source_version}" ]; then
115 echo "Warning: version has changed since last cmake run."
116 vlog " cmake version: ${cmake_version} version now: ${source_version}"
120 # $1 is the name of an environment variable containing a directory name to
121 # test.
122 test_env_var_dir() {
123 local dir=$(eval echo "\${$1}")
124 if [ ! -d "${dir}" ]; then
125 elog "'${dir}': No such directory"
126 elog "The $1 environment variable must be set to a valid directory."
127 return 1
131 # This script requires that the LIBAOM_BIN_PATH, LIBAOM_CONFIG_PATH, and
132 # LIBAOM_TEST_DATA_PATH variables are in the environment: Confirm that
133 # the variables are set and that they all evaluate to directory paths.
134 verify_aom_test_environment() {
135 test_env_var_dir "LIBAOM_BIN_PATH" \
136 && test_env_var_dir "LIBAOM_CONFIG_PATH" \
137 && test_env_var_dir "LIBAOM_TEST_DATA_PATH"
140 # Greps aom_config.h in LIBAOM_CONFIG_PATH for positional parameter one, which
141 # should be a LIBAOM preprocessor flag. Echoes yes to stdout when the feature
142 # is available.
143 aom_config_option_enabled() {
144 aom_config_option="${1}"
145 aom_config_file="${LIBAOM_CONFIG_PATH}/aom_config.h"
146 config_line=$(grep "${aom_config_option}" "${aom_config_file}")
147 if echo "${config_line}" | egrep -q '1$'; then
148 echo yes
152 # Echoes yes when output of test_configuration_target() contains win32 or win64.
153 is_windows_target() {
154 if test_configuration_target \
155 | grep -q -e win32 -e win64 > /dev/null 2>&1; then
156 echo yes
160 # Echoes path to $1 when it's executable and exists in one of the directories
161 # included in $tool_paths, or an empty string. Caller is responsible for testing
162 # the string once the function returns.
163 aom_tool_path() {
164 local readonly tool_name="$1"
165 local readonly root_path="${LIBAOM_BIN_PATH}"
166 local readonly suffix="${AOM_TEST_EXE_SUFFIX}"
167 local readonly tool_paths="\
168 ${root_path}/${tool_name}${suffix} \
169 ${root_path}/../${tool_name}${suffix} \
170 ${root_path}/tools/${tool_name}${suffix} \
171 ${root_path}/../tools/${tool_name}${suffix}"
173 local toolpath=""
175 for tool_path in ${tool_paths}; do
176 if [ -x "${tool_path}" ] && [ -f "${tool_path}" ]; then
177 echo "${tool_path}"
178 return 0
180 done
182 return 1
185 # Echoes yes to stdout when the file named by positional parameter one exists
186 # in LIBAOM_BIN_PATH, and is executable.
187 aom_tool_available() {
188 local tool_name="$1"
189 local tool="${LIBAOM_BIN_PATH}/${tool_name}${AOM_TEST_EXE_SUFFIX}"
190 [ -x "${tool}" ] && echo yes
193 # Echoes yes to stdout when aom_config_option_enabled() reports yes for
194 # CONFIG_AV1_DECODER.
195 av1_decode_available() {
196 [ "$(aom_config_option_enabled CONFIG_AV1_DECODER)" = "yes" ] && echo yes
199 # Echoes yes to stdout when aom_config_option_enabled() reports yes for
200 # CONFIG_AV1_ENCODER.
201 av1_encode_available() {
202 [ "$(aom_config_option_enabled CONFIG_AV1_ENCODER)" = "yes" ] && echo yes
205 # Echoes "fast" encode params for use with aomenc.
206 aomenc_encode_test_fast_params() {
207 echo "--cpu-used=1
208 --limit=${AOM_ENCODE_TEST_FRAME_LIMIT}
209 --lag-in-frames=0
210 --test-decode=fatal"
213 # Echoes yes to stdout when aom_config_option_enabled() reports yes for
214 # CONFIG_WEBM_IO.
215 webm_io_available() {
216 [ "$(aom_config_option_enabled CONFIG_WEBM_IO)" = "yes" ] && echo yes
219 # Filters strings from $1 using the filter specified by $2. Filter behavior
220 # depends on the presence of $3. When $3 is present, strings that match the
221 # filter are excluded. When $3 is omitted, strings matching the filter are
222 # included.
223 # The filtered result is echoed to stdout.
224 filter_strings() {
225 strings=${1}
226 filter=${2}
227 exclude=${3}
229 if [ -n "${exclude}" ]; then
230 # When positional parameter three exists the caller wants to remove strings.
231 # Tell grep to invert matches using the -v argument.
232 exclude='-v'
233 else
234 unset exclude
237 if [ -n "${filter}" ]; then
238 for s in ${strings}; do
239 if echo "${s}" | egrep -q ${exclude} "${filter}" > /dev/null 2>&1; then
240 filtered_strings="${filtered_strings} ${s}"
242 done
243 else
244 filtered_strings="${strings}"
246 echo "${filtered_strings}"
249 # Runs user test functions passed via positional parameters one and two.
250 # Functions in positional parameter one are treated as environment verification
251 # functions and are run unconditionally. Functions in positional parameter two
252 # are run according to the rules specified in aom_test_usage().
253 run_tests() {
254 local env_tests="verify_aom_test_environment $1"
255 local tests_to_filter="$2"
256 local test_name="${AOM_TEST_NAME}"
258 if [ -z "${test_name}" ]; then
259 test_name="$(basename "${0%.*}")"
262 if [ "${AOM_TEST_RUN_DISABLED_TESTS}" != "yes" ]; then
263 # Filter out DISABLED tests.
264 tests_to_filter=$(filter_strings "${tests_to_filter}" ^DISABLED exclude)
267 if [ -n "${AOM_TEST_FILTER}" ]; then
268 # Remove tests not matching the user's filter.
269 tests_to_filter=$(filter_strings "${tests_to_filter}" ${AOM_TEST_FILTER})
272 # User requested test listing: Dump test names and return.
273 if [ "${AOM_TEST_LIST_TESTS}" = "yes" ]; then
274 for test_name in $tests_to_filter; do
275 echo ${test_name}
276 done
277 return
280 # Don't bother with the environment tests if everything else was disabled.
281 [ -z "${tests_to_filter}" ] && return
283 # Combine environment and actual tests.
284 local tests_to_run="${env_tests} ${tests_to_filter}"
286 check_version_strings
288 # Run tests.
289 for test in ${tests_to_run}; do
290 test_begin "${test}"
291 vlog " RUN ${test}"
292 "${test}"
293 vlog " PASS ${test}"
294 test_end "${test}"
295 done
297 local tested_config="$(test_configuration_target) @ $(source_version)"
298 echo "${test_name}: Done, all tests pass for ${tested_config}."
301 aom_test_usage() {
302 cat << EOF
303 Usage: ${0##*/} [arguments]
304 --bin-path <path to libaom binaries directory>
305 --config-path <path to libaom config directory>
306 --filter <filter>: User test filter. Only tests matching filter are run.
307 --run-disabled-tests: Run disabled tests.
308 --help: Display this message and exit.
309 --test-data-path <path to libaom test data directory>
310 --show-program-output: Shows output from all programs being tested.
311 --prefix: Allows for a user specified prefix to be inserted before all test
312 programs. Grants the ability, for example, to run test programs
313 within valgrind.
314 --list-tests: List all test names and exit without actually running tests.
315 --verbose: Verbose output.
317 When the --bin-path option is not specified the script attempts to use
318 \$LIBAOM_BIN_PATH and then the current directory.
320 When the --config-path option is not specified the script attempts to use
321 \$LIBAOM_CONFIG_PATH and then the current directory.
323 When the -test-data-path option is not specified the script attempts to use
324 \$LIBAOM_TEST_DATA_PATH and then the current directory.
328 # Returns non-zero (failure) when required environment variables are empty
329 # strings.
330 aom_test_check_environment() {
331 if [ -z "${LIBAOM_BIN_PATH}" ] || \
332 [ -z "${LIBAOM_CONFIG_PATH}" ] || \
333 [ -z "${LIBAOM_TEST_DATA_PATH}" ]; then
334 return 1
338 # Echo aomenc command line parameters allowing use of a raw yuv file as
339 # input to aomenc.
340 yuv_raw_input() {
341 echo ""${YUV_RAW_INPUT}"
342 --width="${YUV_RAW_INPUT_WIDTH}"
343 --height="${YUV_RAW_INPUT_HEIGHT}""
346 # Do a small encode for testing decoders.
347 encode_yuv_raw_input_av1() {
348 if [ "$(av1_encode_available)" = "yes" ]; then
349 local readonly output="$1"
350 local readonly encoder="$(aom_tool_path aomenc)"
351 shift
352 eval "${encoder}" $(yuv_raw_input) \
353 --codec=av1 \
354 $@ \
355 --limit=5 \
356 --output="${output}" \
357 ${devnull}
359 if [ ! -e "${output}" ]; then
360 elog "Output file does not exist."
361 return 1
366 # Parse the command line.
367 while [ -n "$1" ]; do
368 case "$1" in
369 --bin-path)
370 LIBAOM_BIN_PATH="$2"
371 shift
373 --config-path)
374 LIBAOM_CONFIG_PATH="$2"
375 shift
377 --filter)
378 AOM_TEST_FILTER="$2"
379 shift
381 --run-disabled-tests)
382 AOM_TEST_RUN_DISABLED_TESTS=yes
384 --help)
385 aom_test_usage
386 exit
388 --test-data-path)
389 LIBAOM_TEST_DATA_PATH="$2"
390 shift
392 --prefix)
393 AOM_TEST_PREFIX="$2"
394 shift
396 --verbose)
397 AOM_TEST_VERBOSE_OUTPUT=yes
399 --show-program-output)
400 devnull=
402 --list-tests)
403 AOM_TEST_LIST_TESTS=yes
406 aom_test_usage
407 exit 1
409 esac
410 shift
411 done
413 # Handle running the tests from a build directory without arguments when running
414 # the tests on *nix/macosx.
415 LIBAOM_BIN_PATH="${LIBAOM_BIN_PATH:-.}"
416 LIBAOM_CONFIG_PATH="${LIBAOM_CONFIG_PATH:-.}"
417 LIBAOM_TEST_DATA_PATH="${LIBAOM_TEST_DATA_PATH:-.}"
419 # Create a temporary directory for output files, and a trap to clean it up.
420 if [ -n "${TMPDIR}" ]; then
421 AOM_TEST_TEMP_ROOT="${TMPDIR}"
422 elif [ -n "${TEMPDIR}" ]; then
423 AOM_TEST_TEMP_ROOT="${TEMPDIR}"
424 else
425 AOM_TEST_TEMP_ROOT=/tmp
428 AOM_TEST_OUTPUT_DIR="${AOM_TEST_TEMP_ROOT}/aom_test_$$"
430 if ! mkdir -p "${AOM_TEST_OUTPUT_DIR}" || \
431 [ ! -d "${AOM_TEST_OUTPUT_DIR}" ]; then
432 echo "${0##*/}: Cannot create output directory, giving up."
433 echo "${0##*/}: AOM_TEST_OUTPUT_DIR=${AOM_TEST_OUTPUT_DIR}"
434 exit 1
437 if [ "$(is_windows_target)" = "yes" ]; then
438 AOM_TEST_EXE_SUFFIX=".exe"
441 # Variables shared by tests.
442 VP8_IVF_FILE="${LIBAOM_TEST_DATA_PATH}/vp80-00-comprehensive-001.ivf"
443 AV1_IVF_FILE=""
445 AV1_WEBM_FILE=""
446 AV1_FPM_WEBM_FILE=""
447 AV1_LT_50_FRAMES_WEBM_FILE=""
449 YUV_RAW_INPUT="${LIBAOM_TEST_DATA_PATH}/hantro_collage_w352h288.yuv"
450 YUV_RAW_INPUT_WIDTH=352
451 YUV_RAW_INPUT_HEIGHT=288
453 Y4M_NOSQ_PAR_INPUT="${LIBAOM_TEST_DATA_PATH}/park_joy_90p_8_420_a10-1.y4m"
454 Y4M_720P_INPUT="${LIBAOM_TEST_DATA_PATH}/niklas_1280_720_30.y4m"
456 # Setup a trap function to clean up after tests complete.
457 trap cleanup EXIT
459 vlog "$(basename "${0%.*}") test configuration:
460 LIBAOM_BIN_PATH=${LIBAOM_BIN_PATH}
461 LIBAOM_CONFIG_PATH=${LIBAOM_CONFIG_PATH}
462 LIBAOM_TEST_DATA_PATH=${LIBAOM_TEST_DATA_PATH}
463 AOM_IVF_FILE=${AOM_IVF_FILE}
464 AV1_IVF_FILE=${AV1_IVF_FILE}
465 AV1_WEBM_FILE=${AV1_WEBM_FILE}
466 AOM_TEST_EXE_SUFFIX=${AOM_TEST_EXE_SUFFIX}
467 AOM_TEST_FILTER=${AOM_TEST_FILTER}
468 AOM_TEST_LIST_TESTS=${AOM_TEST_LIST_TESTS}
469 AOM_TEST_OUTPUT_DIR=${AOM_TEST_OUTPUT_DIR}
470 AOM_TEST_PREFIX=${AOM_TEST_PREFIX}
471 AOM_TEST_RUN_DISABLED_TESTS=${AOM_TEST_RUN_DISABLED_TESTS}
472 AOM_TEST_SHOW_PROGRAM_OUTPUT=${AOM_TEST_SHOW_PROGRAM_OUTPUT}
473 AOM_TEST_TEMP_ROOT=${AOM_TEST_TEMP_ROOT}
474 AOM_TEST_VERBOSE_OUTPUT=${AOM_TEST_VERBOSE_OUTPUT}
475 YUV_RAW_INPUT=${YUV_RAW_INPUT}
476 YUV_RAW_INPUT_WIDTH=${YUV_RAW_INPUT_WIDTH}
477 YUV_RAW_INPUT_HEIGHT=${YUV_RAW_INPUT_HEIGHT}
478 Y4M_NOSQ_PAR_INPUT=${Y4M_NOSQ_PAR_INPUT}"
480 fi # End $AOM_TEST_TOOLS_COMMON_SH pseudo include guard.