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
8set -euo pipefail
9
10encoder="$(dirname "$0")"/TAppEncoderHighBitDepthStatic
11cfg_dir="$(dirname "$0")"/../../../third_party/HEVCSoftware/cfg
12
13usage() {
14  echo "$0 [-v] [-q <N>] <input.png> <output.bin>" >&2
15  exit 1
16}
17
18q=27
19verbose=0
20
21while 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
40done
41shift $((OPTIND-1))
42
43if [ $# -lt 2 ]; then
44  usage
45fi
46
47run() {
48  if [ "$verbose" -eq 1 ]; then
49    "$@"
50  else
51    "$@" > /dev/null 2>&1
52  fi
53}
54
55input="$1"
56output="$2"
57
58yuv="$(mktemp)"
59bin="$(mktemp)"
60
61to_clean=("$yuv" "$bin")
62cleanup() {
63  rm -- "${to_clean[@]}"
64}
65trap cleanup EXIT
66
67run 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
69width="$(identify -format '%w' "$input")"
70height="$(identify -format '%h' "$input")"
71
72start="$EPOCHREALTIME"
73run "$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"
74end="$EPOCHREALTIME"
75
76elapsed="$(echo "$end - $start" | bc)"
77run echo "Completed in $elapsed seconds"
78
79echo "$elapsed" > "${output%.bin}".time
80
81icc="${output%.*}.icc"
82if run convert "$input" "$icc"; then
83  to_clean+=("$icc")
84fi
85
86pack_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;
95END
96)"
97run perl -Mstrict -Mwarnings -Mautodie -e "$pack_program" -- "$width" "$height" "$bin" "$icc" "$output"
98