#!/bin/sh
## Copyright (c) 2021, Alliance for Open Media. All rights reserved
##
## This source code is subject to the terms of the BSD 3-Clause Clear License and the
## Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear License was
## not distributed with this source code in the LICENSE file, you can obtain it
## at aomedia.org/license/software-license/bsd-3-c-c/.  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 aomedia.org/license/patent-license/.
##
## This file tests avmdec. To add new tests to this file, do the following:
##   1. Write a shell function (this is your test).
##   2. Add the function to avmdec_tests (on a new line).
##
. $(dirname $0)/tools_common.sh

# Environment check: Make sure input is available.
avmdec_verify_environment() {
  if [ "$(av2_encode_available)" != "yes" ] ; then
    if [ ! -e "${AV2_IVF_FILE}" ] || \
       [ ! -e "${AV2_OBU_FILE}" ] || \
       [ ! -e "${AV2_WEBM_FILE}" ]; then
      elog "Libavm test data must exist before running this test script when " \
           " encoding is disabled. "
      return 1
    fi
  fi
  if [ -z "$(avm_tool_path avmdec)" ]; then
    elog "avmdec not found. It must exist in LIBAVM_BIN_PATH or its parent."
    return 1
  fi
}

# Wrapper function for running avmdec with pipe input. Requires that
# LIBAVM_BIN_PATH points to the directory containing avmdec. $1 is used as the
# input file path and shifted away. All remaining parameters are passed through
# to avmdec.
avmdec_pipe() {
  local input="$1"
  shift
  if [ ! -e "${input}" ]; then
    elog "Input file ($input) missing in avmdec_pipe()"
    return 1
  fi
  cat "${file}" | avmdec - "$@" ${devnull}
}


# Wrapper function for running avmdec. Requires that LIBAVM_BIN_PATH points to
# the directory containing avmdec. $1 one is used as the input file path and
# shifted away. All remaining parameters are passed through to avmdec.
avmdec() {
  local decoder="$(avm_tool_path avmdec)"
  local input="$1"
  shift
  eval "${AVM_TEST_PREFIX}" "${decoder}" "$input" "$@" ${devnull}
}

avmdec_can_decode_av2() {
  if [ "$(av2_decode_available)" = "yes" ]; then
    echo yes
  fi
}

avmdec_av2_ivf() {
  if [ "$(avmdec_can_decode_av2)" = "yes" ]; then
    local file="${AV2_IVF_FILE}"
    if [ ! -e "${file}" ]; then
      encode_yuv_raw_input_av2 "${file}" --ivf || return 1
    fi
    avmdec "${AV2_IVF_FILE}" --summary --noblit
  fi
}

avmdec_av2_ivf_error_resilient() {
  if [ "$(avmdec_can_decode_av2)" = "yes" ]; then
    local file="av2.error-resilient.ivf"
    if [ ! -e "${file}" ]; then
      encode_yuv_raw_input_av2 "${file}" --ivf --global-error-resilient=1 || return 1
    fi
    avmdec "${file}" --summary --noblit
  fi
}

avmdec_av2_ivf_multithread() {
  if [ "$(avmdec_can_decode_av2)" = "yes" ]; then
    local file="${AV2_IVF_FILE}"
    if [ ! -e "${file}" ]; then
      encode_yuv_raw_input_av2 "${file}" --ivf || return 1
    fi
    for threads in 2 3 4 5 6 7 8; do
      avmdec "${file}" --summary --noblit --threads=$threads || return 1
    done
  fi
}

avmdec_avm_ivf_pipe_input() {
  if [ "$(avmdec_can_decode_av2)" = "yes" ]; then
    local file="${AV2_IVF_FILE}"
    if [ ! -e "${file}" ]; then
      encode_yuv_raw_input_av2 "${file}" --ivf || return 1
    fi
    avmdec_pipe "${AV2_IVF_FILE}" --summary --noblit
  fi
}

avmdec_av2_obu() {
  if [ "$(avmdec_can_decode_av2)" = "yes" ]; then
    local file="${AV2_OBU_FILE}"
    if [ ! -e "${file}" ]; then
      encode_yuv_raw_input_av2 "${file}" --obu || return 1
    fi
    avmdec "${file}" --summary --noblit
  fi
}

avmdec_av2_webm() {
  if [ "$(avmdec_can_decode_av2)" = "yes" ] && \
     [ "$(webm_io_available)" = "yes" ]; then
    local file="${AV2_WEBM_FILE}"
    if [ ! -e "${file}" ]; then
      encode_yuv_raw_input_av2 "${file}" || return 1
    fi
    avmdec "${AV2_WEBM_FILE}" --summary --noblit
  fi
}

avmdec_random_access_common() {
  if [ "$(avmdec_can_decode_av2)" = "yes" ]; then
    local kf_filter="$1"
    vlog "Testing with --enable-keyframe-filtering=${kf_filter}"

    # Encode with multiple keyframes.
    local output="${AVM_TEST_OUTPUT_DIR}/av2_test_random_access_kf${kf_filter}.obu"
    if [ ! -e "${output}" ]; then
      encode_yuv_raw_input_with_olks_and_kf_filter "${kf_filter}" "${output}" || return 1
    fi

    # Run decoder with different random access point indices.
    for idx in 0 1 2 3; do
      local dec_output="${AVM_TEST_OUTPUT_DIR}/test_rap_${idx}_kf${kf_filter}.y4m"
      vlog "Testing decoder with --random-access-point-index=${idx}"

      local log="${AVM_TEST_OUTPUT_DIR}/dec_log_${idx}_kf${kf_filter}.log"
      avmdec --summary "${output}" -o "${dec_output}" --random-access-point-index=${idx} > "${log}" 2>&1 || return 1
      cat "${log}"

      # Verify that decoded file is generated.
      if [ ! -e "${dec_output}" ]; then
        elog "Decoder output file test_rap_${idx}_kf${kf_filter}.y4m does not exist."
        return 1
      fi

      # Verify that decoder decoded correct number of frames.
      # Assumes that 49 frames are encoded with keyframe interval of 16.
      local expected_n=$((49 - idx * 16))
      if ! grep -q "${expected_n} showed frames" "${log}"; then
        elog "Expected ${expected_n} showed frames in decoder log."
        return 1
      fi
    done
  fi
}

avmdec_random_access_kf0() {
  avmdec_random_access_common 0
}

avmdec_random_access_kf1() {
  avmdec_random_access_common 1
}

avmdec_random_access_kf2() {
  avmdec_random_access_common 2
}



avmdec_tests="avmdec_av2_ivf
              avmdec_av2_ivf_error_resilient
              avmdec_av2_ivf_multithread
              avmdec_avm_ivf_pipe_input
              avmdec_av2_obu
              avmdec_av2_webm
              avmdec_random_access_kf0
              avmdec_random_access_kf1
              avmdec_random_access_kf2"

run_tests avmdec_verify_environment "${avmdec_tests}"
