|
| 1 | +#!/bin/bash |
| 2 | +# Copyright 2025 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +source $(dirname "$0")/cmd_test_common.sh |
| 17 | + |
| 18 | +# Input file paths. |
| 19 | +INPUT_Y4M_STILL="${TESTDATA_DIR}/kodim03_yuv420_8bpc.y4m" |
| 20 | +INPUT_Y4M_ANIMATED="${TESTDATA_DIR}/webp_logo_animated.y4m" |
| 21 | +# Output file names. |
| 22 | +ENCODED_FILE_REGULAR="avif_test_cmd_stdin_encoded.avif" |
| 23 | +ENCODED_FILE_STDIN="avif_test_cmd_stdin_encoded_with_stdin.avif" |
| 24 | + |
| 25 | +# Cleanup |
| 26 | +cleanup() { |
| 27 | + pushd ${TMP_DIR} |
| 28 | + rm -f -- "${ENCODED_FILE_REGULAR}" "${ENCODED_FILE_STDIN}" |
| 29 | + popd |
| 30 | +} |
| 31 | +trap cleanup EXIT |
| 32 | + |
| 33 | +strip_header_if() { |
| 34 | + FILE="$1" |
| 35 | + STRIP_HEADER="$2" |
| 36 | + |
| 37 | + if ${STRIP_HEADER}; then |
| 38 | + grep --help |
| 39 | + grep mdat "${FILE}" |
| 40 | + grep --text mdat "${FILE}" |
| 41 | + grep --text --max-count=1 mdat "${FILE}" |
| 42 | + grep --text --max-count=1 --byte-offset mdat "${FILE}" |
| 43 | + grep --text --max-count=1 --byte-offset --only-matching mdat "${FILE}" |
| 44 | + grep --text --max-count=1 --byte-offset --only-matching --binary mdat "${FILE}" |
| 45 | + grep -o -b -a mdat "${FILE}" |
| 46 | + MDAT_OFFSET=$(grep -o -b -a mdat "${FILE}" | cut -f1 -d:) |
| 47 | + FILE_CONTENTS_AFTER_HEADER=$(tail -c +${MDAT_OFFSET} < "${FILE}") |
| 48 | + echo "${FILE_CONTENTS_AFTER_HEADER}" > "${FILE}" |
| 49 | + fi |
| 50 | +} |
| 51 | + |
| 52 | +test_stdin() { |
| 53 | + INPUT_Y4M="$1" |
| 54 | + STRIP_HEADER="$2" |
| 55 | + |
| 56 | + # Make sure that --stdin can be replaced with a file path and that it leads to |
| 57 | + # the same encoded bytes. |
| 58 | + |
| 59 | + "${AVIFENC}" -s 8 -o "${ENCODED_FILE_REGULAR}" "${INPUT_Y4M}" |
| 60 | + "${AVIFENC}" -s 8 -o "${ENCODED_FILE_STDIN}" --stdin < "${INPUT_Y4M}" |
| 61 | + strip_header_if "${ENCODED_FILE_REGULAR}" ${STRIP_HEADER} |
| 62 | + strip_header_if "${ENCODED_FILE_STDIN}" ${STRIP_HEADER} |
| 63 | + cmp --silent "${ENCODED_FILE_REGULAR}" "${ENCODED_FILE_STDIN}" |
| 64 | + |
| 65 | + "${AVIFENC}" -s 9 "${INPUT_Y4M}" -o "${ENCODED_FILE_REGULAR}" |
| 66 | + "${AVIFENC}" -s 9 --stdin -o "${ENCODED_FILE_STDIN}" < "${INPUT_Y4M}" |
| 67 | + strip_header_if "${ENCODED_FILE_REGULAR}" ${STRIP_HEADER} |
| 68 | + strip_header_if "${ENCODED_FILE_STDIN}" ${STRIP_HEADER} |
| 69 | + cmp --silent "${ENCODED_FILE_REGULAR}" "${ENCODED_FILE_STDIN}" |
| 70 | + |
| 71 | + "${AVIFENC}" -s 10 "${INPUT_Y4M}" "${ENCODED_FILE_REGULAR}" |
| 72 | + "${AVIFENC}" -s 10 --stdin "${ENCODED_FILE_STDIN}" < "${INPUT_Y4M}" |
| 73 | + strip_header_if "${ENCODED_FILE_REGULAR}" ${STRIP_HEADER} |
| 74 | + strip_header_if "${ENCODED_FILE_STDIN}" ${STRIP_HEADER} |
| 75 | + cmp --silent "${ENCODED_FILE_REGULAR}" "${ENCODED_FILE_STDIN}" |
| 76 | + "${AVIFENC}" -s 10 "${ENCODED_FILE_STDIN}" --stdin < "${INPUT_Y4M}" |
| 77 | + strip_header_if "${ENCODED_FILE_STDIN}" ${STRIP_HEADER} |
| 78 | + cmp --silent "${ENCODED_FILE_REGULAR}" "${ENCODED_FILE_STDIN}" |
| 79 | + |
| 80 | + "${AVIFENC}" -s 10 "${INPUT_Y4M}" -q 90 "${ENCODED_FILE_REGULAR}" |
| 81 | + "${AVIFENC}" -s 10 --stdin -q 90 "${ENCODED_FILE_STDIN}" < "${INPUT_Y4M}" |
| 82 | + strip_header_if "${ENCODED_FILE_REGULAR}" ${STRIP_HEADER} |
| 83 | + strip_header_if "${ENCODED_FILE_STDIN}" ${STRIP_HEADER} |
| 84 | + cmp --silent "${ENCODED_FILE_REGULAR}" "${ENCODED_FILE_STDIN}" |
| 85 | + "${AVIFENC}" -s 10 "${ENCODED_FILE_STDIN}" -q 90 --stdin < "${INPUT_Y4M}" |
| 86 | + strip_header_if "${ENCODED_FILE_STDIN}" ${STRIP_HEADER} |
| 87 | + cmp --silent "${ENCODED_FILE_REGULAR}" "${ENCODED_FILE_STDIN}" |
| 88 | + "${AVIFENC}" -s 10 --stdin "${ENCODED_FILE_STDIN}" -q 90 < "${INPUT_Y4M}" |
| 89 | + strip_header_if "${ENCODED_FILE_STDIN}" ${STRIP_HEADER} |
| 90 | + cmp --silent "${ENCODED_FILE_REGULAR}" "${ENCODED_FILE_STDIN}" |
| 91 | + |
| 92 | + # Negative tests. |
| 93 | + |
| 94 | + # WARNING: Trailing options with update suffix has no effect. Place them before the input you intend to apply to. |
| 95 | + "${AVIFENC}" -s 10 --stdin "${ENCODED_FILE_STDIN}" -q:u 90 < "${INPUT_Y4M}" |
| 96 | + cmp --silent "${ENCODED_FILE_REGULAR}" "${ENCODED_FILE_STDIN}" && exit 1 |
| 97 | + |
| 98 | + # ERROR: there cannot be any other input if --stdin is specified |
| 99 | + "${AVIFENC}" --stdin && exit 1 |
| 100 | + "${AVIFENC}" --stdin "${ENCODED_FILE_STDIN}" "${ENCODED_FILE_STDIN}" && exit 1 |
| 101 | + "${AVIFENC}" "${ENCODED_FILE_STDIN}" --stdin "${ENCODED_FILE_STDIN}" && exit 1 |
| 102 | + "${AVIFENC}" "${ENCODED_FILE_STDIN}" "${ENCODED_FILE_STDIN}" --stdin && exit 1 |
| 103 | + |
| 104 | + return 0 |
| 105 | +} |
| 106 | + |
| 107 | +pushd ${TMP_DIR} |
| 108 | + test_stdin "${INPUT_Y4M_STILL}" false |
| 109 | + |
| 110 | + # The output of avifenc for animations is not deterministic because of boxes |
| 111 | + # such as mvhd and its field creation_time. Strip the whole header to compare |
| 112 | + # only the encoded samples. |
| 113 | + test_stdin "${INPUT_Y4M_ANIMATED}" true |
| 114 | +popd |
| 115 | + |
| 116 | +exit 0 |
0 commit comments