encode.sh (2058B)
1 #!/usr/bin/env bash 2 3 # Copyright (c) the JPEG XL Project Authors. All rights reserved. 4 # 5 # Use of this source code is governed by a BSD-style 6 # license that can be found in the LICENSE file. 7 8 set -euo pipefail 9 10 encoder="$(dirname "$0")"/TAppEncoderHighBitDepthStatic 11 cfg_dir="$(dirname "$0")"/../../../third_party/HEVCSoftware/cfg 12 13 usage() { 14 echo "$0 [-v] [-q <N>] <input.png> <output.bin>" >&2 15 exit 1 16 } 17 18 q=27 19 verbose=0 20 21 while getopts ':hq:v' arg; do 22 case "$arg" in 23 h) 24 usage 25 ;; 26 27 q) 28 q="$OPTARG" 29 ;; 30 31 v) 32 verbose=1 33 ;; 34 35 \?) 36 echo "Unrecognized option -$OPTARG" >&2 37 exit 1 38 ;; 39 esac 40 done 41 shift $((OPTIND-1)) 42 43 if [ $# -lt 2 ]; then 44 usage 45 fi 46 47 run() { 48 if [ "$verbose" -eq 1 ]; then 49 "$@" 50 else 51 "$@" > /dev/null 2>&1 52 fi 53 } 54 55 input="$1" 56 output="$2" 57 58 yuv="$(mktemp)" 59 bin="$(mktemp)" 60 61 to_clean=("$yuv" "$bin") 62 cleanup() { 63 rm -- "${to_clean[@]}" 64 } 65 trap cleanup EXIT 66 67 run ffmpeg -hide_banner -i "$input" -pix_fmt yuv444p10le -vf scale=out_color_matrix=bt709 -color_primaries bt709 -color_trc bt709 -colorspace bt709 -f rawvideo -y "$yuv" 68 69 width="$(identify -format '%w' "$input")" 70 height="$(identify -format '%h' "$input")" 71 72 start="$EPOCHREALTIME" 73 run "$encoder" -c "$cfg_dir"/encoder_intra_main_scc_10.cfg -f 1 -fr 1 -wdt "$width" -hgt "$height" --InputChromaFormat=444 --InputBitDepth=10 --ConformanceWindowMode=1 -i "$yuv" -b "$bin" -q "$q" 74 end="$EPOCHREALTIME" 75 76 elapsed="$(echo "$end - $start" | bc)" 77 run echo "Completed in $elapsed seconds" 78 79 echo "$elapsed" > "${output%.bin}".time 80 81 icc="${output%.*}.icc" 82 if run convert "$input" "$icc"; then 83 to_clean+=("$icc") 84 fi 85 86 pack_program="$(cat <<'END' 87 use File::Copy; 88 use IO::Handle; 89 my ($width, $height, $bin, $icc, $output) = @ARGV; 90 open my $output_fh, '>:raw', $output; 91 syswrite $output_fh, pack 'NN', $width, $height; 92 syswrite $output_fh, pack 'N', -s $icc; 93 copy $icc, $output_fh; 94 copy $bin, $output_fh; 95 END 96 )" 97 run perl -Mstrict -Mwarnings -Mautodie -e "$pack_program" -- "$width" "$height" "$bin" "$icc" "$output"