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="" 20 21elog() { 22 echo "$@" 1>&2 23} 24 25vlog() { 26 if [ "${AOM_TEST_VERBOSE_OUTPUT}" = "yes" ]; then 27 echo "$@" 28 fi 29} 30 31# Sets $AOM_TOOL_TEST to the name specified by positional parameter one. 32test_begin() { 33 AOM_TOOL_TEST="${1}" 34} 35 36# Clears the AOM_TOOL_TEST variable after confirming that $AOM_TOOL_TEST matches 37# positional parameter one. 38test_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 44 fi 45 AOM_TOOL_TEST='<unset>' 46} 47 48# Echoes the target configuration being tested. 49test_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} 60} 61 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. 65cleanup() { 66 if [ -n "${AOM_TOOL_TEST}" ] && [ "${AOM_TOOL_TEST}" != '<unset>' ]; then 67 echo "FAIL: $AOM_TOOL_TEST" 68 fi 69 if [ "${AOM_TEST_PRESERVE_OUTPUT}" = "yes" ]; then 70 return 71 fi 72 if [ -n "${AOM_TEST_OUTPUT_DIR}" ] && [ -d "${AOM_TEST_OUTPUT_DIR}" ]; then 73 rm -rf "${AOM_TEST_OUTPUT_DIR}" 74 fi 75} 76 77# Echoes the version string assigned to the VERSION_STRING_NOSP variable defined 78# in $LIBAOM_CONFIG_PATH/config/aom_version.h to stdout. 79cmake_version() { 80 aom_version_h="${LIBAOM_CONFIG_PATH}/config/aom_version.h" 81 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}" 87} 88 89# Echoes current git version as reported by running 'git describe', or the 90# version used by the cmake build when git is unavailable. 91source_version() { 92 if git --version > /dev/null 2>&1; then 93 (cd "$(dirname "${0}")" 94 git 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_WEBM_IO. 232webm_io_available() { 233 [ "$(aom_config_option_enabled CONFIG_WEBM_IO)" = "yes" ] && echo yes 234} 235 236# Echoes yes to stdout when aom_config_option_enabled() reports yes for 237# CONFIG_REALTIME_ONLY. 238realtime_only_build() { 239 [ "$(aom_config_option_enabled CONFIG_REALTIME_ONLY)" = "yes" ] && echo yes 240} 241 242# Filters strings from $1 using the filter specified by $2. Filter behavior 243# depends on the presence of $3. When $3 is present, strings that match the 244# filter are excluded. When $3 is omitted, strings matching the filter are 245# included. 246# The filtered result is echoed to stdout. 247filter_strings() { 248 strings=${1} 249 filter=${2} 250 exclude=${3} 251 252 if [ -n "${exclude}" ]; then 253 # When positional parameter three exists the caller wants to remove strings. 254 # Tell grep to invert matches using the -v argument. 255 exclude='-v' 256 else 257 unset exclude 258 fi 259 260 if [ -n "${filter}" ]; then 261 for s in ${strings}; do 262 if echo "${s}" | egrep -q ${exclude} "${filter}" > /dev/null 2>&1; then 263 filtered_strings="${filtered_strings} ${s}" 264 fi 265 done 266 else 267 filtered_strings="${strings}" 268 fi 269 echo "${filtered_strings}" 270} 271 272# Runs user test functions passed via positional parameters one and two. 273# Functions in positional parameter one are treated as environment verification 274# functions and are run unconditionally. Functions in positional parameter two 275# are run according to the rules specified in aom_test_usage(). 276run_tests() { 277 local env_tests="verify_aom_test_environment $1" 278 local tests_to_filter="$2" 279 local test_name="${AOM_TEST_NAME}" 280 281 if [ -z "${test_name}" ]; then 282 test_name="$(basename "${0%.*}")" 283 fi 284 285 if [ "${AOM_TEST_RUN_DISABLED_TESTS}" != "yes" ]; then 286 # Filter out DISABLED tests. 287 tests_to_filter=$(filter_strings "${tests_to_filter}" ^DISABLED exclude) 288 fi 289 290 if [ -n "${AOM_TEST_FILTER}" ]; then 291 # Remove tests not matching the user's filter. 292 tests_to_filter=$(filter_strings "${tests_to_filter}" ${AOM_TEST_FILTER}) 293 fi 294 295 # User requested test listing: Dump test names and return. 296 if [ "${AOM_TEST_LIST_TESTS}" = "yes" ]; then 297 for test_name in $tests_to_filter; do 298 echo ${test_name} 299 done 300 return 301 fi 302 303 # Don't bother with the environment tests if everything else was disabled. 304 [ -z "${tests_to_filter}" ] && return 305 306 # Combine environment and actual tests. 307 local tests_to_run="${env_tests} ${tests_to_filter}" 308 309 check_version_strings 310 311 # Run tests. 312 for test in ${tests_to_run}; do 313 test_begin "${test}" 314 vlog " RUN ${test}" 315 "${test}" 316 vlog " PASS ${test}" 317 test_end "${test}" 318 done 319 320 local tested_config="$(test_configuration_target) @ $(source_version)" 321 echo "${test_name}: Done, all tests pass for ${tested_config}." 322} 323 324aom_test_usage() { 325cat << EOF 326 Usage: ${0##*/} [arguments] 327 --bin-path <path to libaom binaries directory> 328 --config-path <path to libaom config directory> 329 --filter <filter>: User test filter. Only tests matching filter are run. 330 --run-disabled-tests: Run disabled tests. 331 --help: Display this message and exit. 332 --test-data-path <path to libaom test data directory> 333 --show-program-output: Shows output from all programs being tested. 334 --prefix: Allows for a user specified prefix to be inserted before all test 335 programs. Grants the ability, for example, to run test programs 336 within valgrind. 337 --list-tests: List all test names and exit without actually running tests. 338 --verbose: Verbose output. 339 340 When the --bin-path option is not specified the script attempts to use 341 \$LIBAOM_BIN_PATH and then the current directory. 342 343 When the --config-path option is not specified the script attempts to use 344 \$LIBAOM_CONFIG_PATH and then the current directory. 345 346 When the -test-data-path option is not specified the script attempts to use 347 \$LIBAOM_TEST_DATA_PATH and then the current directory. 348EOF 349} 350 351# Returns non-zero (failure) when required environment variables are empty 352# strings. 353aom_test_check_environment() { 354 if [ -z "${LIBAOM_BIN_PATH}" ] || \ 355 [ -z "${LIBAOM_CONFIG_PATH}" ] || \ 356 [ -z "${LIBAOM_TEST_DATA_PATH}" ]; then 357 return 1 358 fi 359} 360 361# Echo aomenc command line parameters allowing use of a raw yuv file as 362# input to aomenc. 363yuv_raw_input() { 364 echo ""${YUV_RAW_INPUT}" 365 --width="${YUV_RAW_INPUT_WIDTH}" 366 --height="${YUV_RAW_INPUT_HEIGHT}"" 367} 368 369# Do a small encode for testing decoders. 370encode_yuv_raw_input_av1() { 371 if [ "$(av1_encode_available)" = "yes" ]; then 372 local output="$1" 373 local encoder="$(aom_tool_path aomenc)" 374 shift 375 eval "${encoder}" $(yuv_raw_input) \ 376 $(aomenc_encode_test_fast_params) \ 377 --output="${output}" \ 378 $@ \ 379 ${devnull} 380 381 if [ ! -e "${output}" ]; then 382 elog "Output file does not exist." 383 return 1 384 fi 385 fi 386} 387 388# Parse the command line. 389while [ -n "$1" ]; do 390 case "$1" in 391 --bin-path) 392 LIBAOM_BIN_PATH="$2" 393 shift 394 ;; 395 --config-path) 396 LIBAOM_CONFIG_PATH="$2" 397 shift 398 ;; 399 --filter) 400 AOM_TEST_FILTER="$2" 401 shift 402 ;; 403 --run-disabled-tests) 404 AOM_TEST_RUN_DISABLED_TESTS=yes 405 ;; 406 --help) 407 aom_test_usage 408 exit 409 ;; 410 --test-data-path) 411 LIBAOM_TEST_DATA_PATH="$2" 412 shift 413 ;; 414 --prefix) 415 AOM_TEST_PREFIX="$2" 416 shift 417 ;; 418 --verbose) 419 AOM_TEST_VERBOSE_OUTPUT=yes 420 ;; 421 --show-program-output) 422 devnull= 423 ;; 424 --list-tests) 425 AOM_TEST_LIST_TESTS=yes 426 ;; 427 *) 428 aom_test_usage 429 exit 1 430 ;; 431 esac 432 shift 433done 434 435# Handle running the tests from a build directory without arguments when running 436# the tests on *nix/macosx. 437LIBAOM_BIN_PATH="${LIBAOM_BIN_PATH:-.}" 438LIBAOM_CONFIG_PATH="${LIBAOM_CONFIG_PATH:-.}" 439LIBAOM_TEST_DATA_PATH="${LIBAOM_TEST_DATA_PATH:-.}" 440 441# Create a temporary directory for output files, and a trap to clean it up. 442if [ -n "${TMPDIR}" ]; then 443 AOM_TEST_TEMP_ROOT="${TMPDIR}" 444elif [ -n "${TEMPDIR}" ]; then 445 AOM_TEST_TEMP_ROOT="${TEMPDIR}" 446else 447 AOM_TEST_TEMP_ROOT=/tmp 448fi 449 450AOM_TEST_OUTPUT_DIR="${AOM_TEST_OUTPUT_DIR:-${AOM_TEST_TEMP_ROOT}/aom_test_$$}" 451 452if ! mkdir -p "${AOM_TEST_OUTPUT_DIR}" || \ 453 [ ! -d "${AOM_TEST_OUTPUT_DIR}" ]; then 454 echo "${0##*/}: Cannot create output directory, giving up." 455 echo "${0##*/}: AOM_TEST_OUTPUT_DIR=${AOM_TEST_OUTPUT_DIR}" 456 exit 1 457fi 458 459AOM_TEST_PRESERVE_OUTPUT=${AOM_TEST_PRESERVE_OUTPUT:-no} 460 461if [ "$(is_windows_target)" = "yes" ]; then 462 AOM_TEST_EXE_SUFFIX=".exe" 463fi 464 465# Variables shared by tests. 466AV1_ENCODE_CPU_USED=${AV1_ENCODE_CPU_USED:-1} 467AV1_ENCODE_TEST_FRAME_LIMIT=${AV1_ENCODE_TEST_FRAME_LIMIT:-5} 468AV1_IVF_FILE="${AV1_IVF_FILE:-${AOM_TEST_OUTPUT_DIR}/av1.ivf}" 469AV1_OBU_ANNEXB_FILE="${AV1_OBU_ANNEXB_FILE:-${AOM_TEST_OUTPUT_DIR}/av1.annexb.obu}" 470AV1_OBU_SEC5_FILE="${AV1_OBU_SEC5_FILE:-${AOM_TEST_OUTPUT_DIR}/av1.section5.obu}" 471AV1_WEBM_FILE="${AV1_WEBM_FILE:-${AOM_TEST_OUTPUT_DIR}/av1.webm}" 472 473YUV_RAW_INPUT="${LIBAOM_TEST_DATA_PATH}/hantro_collage_w352h288.yuv" 474YUV_RAW_INPUT_WIDTH=352 475YUV_RAW_INPUT_HEIGHT=288 476 477Y4M_NOSQ_PAR_INPUT="${LIBAOM_TEST_DATA_PATH}/park_joy_90p_8_420_a10-1.y4m" 478Y4M_720P_INPUT="${LIBAOM_TEST_DATA_PATH}/niklas_1280_720_30.y4m" 479 480# Setup a trap function to clean up after tests complete. 481trap cleanup EXIT 482 483vlog "$(basename "${0%.*}") test configuration: 484 LIBAOM_BIN_PATH=${LIBAOM_BIN_PATH} 485 LIBAOM_CONFIG_PATH=${LIBAOM_CONFIG_PATH} 486 LIBAOM_TEST_DATA_PATH=${LIBAOM_TEST_DATA_PATH} 487 AOM_TEST_EXE_SUFFIX=${AOM_TEST_EXE_SUFFIX} 488 AOM_TEST_FILTER=${AOM_TEST_FILTER} 489 AOM_TEST_LIST_TESTS=${AOM_TEST_LIST_TESTS} 490 AOM_TEST_OUTPUT_DIR=${AOM_TEST_OUTPUT_DIR} 491 AOM_TEST_PREFIX=${AOM_TEST_PREFIX} 492 AOM_TEST_PRESERVE_OUTPUT=${AOM_TEST_PRESERVE_OUTPUT} 493 AOM_TEST_RUN_DISABLED_TESTS=${AOM_TEST_RUN_DISABLED_TESTS} 494 AOM_TEST_SHOW_PROGRAM_OUTPUT=${AOM_TEST_SHOW_PROGRAM_OUTPUT} 495 AOM_TEST_TEMP_ROOT=${AOM_TEST_TEMP_ROOT} 496 AOM_TEST_VERBOSE_OUTPUT=${AOM_TEST_VERBOSE_OUTPUT} 497 AV1_ENCODE_CPU_USED=${AV1_ENCODE_CPU_USED} 498 AV1_ENCODE_TEST_FRAME_LIMIT=${AV1_ENCODE_TEST_FRAME_LIMIT} 499 AV1_IVF_FILE=${AV1_IVF_FILE} 500 AV1_OBU_ANNEXB_FILE=${AV1_OBU_ANNEXB_FILE} 501 AV1_OBU_SEC5_FILE=${AV1_OBU_SEC5_FILE} 502 AV1_WEBM_FILE=${AV1_WEBM_FILE} 503 YUV_RAW_INPUT=${YUV_RAW_INPUT} 504 YUV_RAW_INPUT_WIDTH=${YUV_RAW_INPUT_WIDTH} 505 YUV_RAW_INPUT_HEIGHT=${YUV_RAW_INPUT_HEIGHT} 506 Y4M_NOSQ_PAR_INPUT=${Y4M_NOSQ_PAR_INPUT}" 507 508fi # End $AOM_TEST_TOOLS_COMMON_SH pseudo include guard. 509