Add a script for downsample, compression, upsample

Adds an utility script and documentation for downsampling,
compression, and psampling pipeline.

Also improves the previous script on downsampling and
upsampling pipeline.

The README is also appropriately updated.

Change-Id: Ia68a70f902dab47a308482ea5f400e05bb0b3258
diff --git a/tools/lanczos/lanczos_README.txt b/tools/lanczos/lanczos_README.txt
index d55b3ba..545ba39 100644
--- a/tools/lanczos/lanczos_README.txt
+++ b/tools/lanczos/lanczos_README.txt
@@ -15,15 +15,24 @@
 
 For questions and technical support, please contact debargha@google.com
 
+LIBRARY
+#######
+
 The main library source and header files are:
 lanczos_resample.c
 lanczos_resample.h
 
+
+UTILITIES
+#########
+
 In addition, two utilities are provided to resample y4m and yuv videos
 respectively:
 lanczos_resample_y4m (built using source lanczos_resample_y4m.c)
 lanczos_resample_y4m (built using source lanczos_resample_yuv.c)
 
+lanczos_resample_y4m
+--------------------
 The usage of lanczos_resample_y4m is:
   lanczos_resample_y4m
       <y4m_input>
@@ -85,6 +94,8 @@
     lanczos_resample_y4m /tmp/down.y4m 10 3:2:6:i0.125 4:3:6 /tmp/downup.y4m \
                          1920x1080
 
+lanczos_resample_yuv
+--------------------
 The usage of lanczos_resample_yuv is similar but with two extra
 arguments to specify the input format:
   lanczos_resample_yuv
@@ -117,10 +128,20 @@
           is regarded as a no-op in that direction.
 
 
+SCRIPTS
+#######
 
-In addition to these utilities, a script lanczos_downup.sh is provided
-downsample a video with specified parameters and then reversing the
-process using two lanczos_resample_y4m commands. The usage for the script is:
+In addition to these utilities, two convenience scripts are provided:
+lanczos_downup.sh
+lanczos_downcompup.sh
+They can be invoked from the build directory directly after a build that
+produces lanczos_resample_y4m, aomenc and aomdec applications.
+
+lanczos_downup.sh
+-----------------
+The script lanczos_downup.sh resamples a video with specified parameters
+and then reverses the process using two lanczos_resample_y4m commands.
+The usage for the script is:
 
   lanczos_downup.sh
       <y4m_input>
@@ -140,5 +161,67 @@
               <vert_p>:<vert_q>:<Lanczos_vert_a>[:vert_x0]
           similar to what is used by lanczos_resample_y4m utility.
       <downup_y4m> is the output y4m video.
-      <down_y4m> is optional. If skipped the intermediate resolution
+      <down_y4m> provides the intermedite resampled file as an
+          optional parameter. If skipped the intermediate resampled
           file is deleted.
+
+lanczos_downcompup.sh
+---------------------
+The script lanczos_downcompup.sh resamples a video with specified parameters
+using lanczos_resample_y4m, then comprsses and decompresses using aomenc and
+aomdec respectively, and finally reverse resamples the decompressed video to
+the source resolution using another lanczos_resample_y4m command.
+The usage for the script is:
+
+  lanczos_downcompup.sh <input_y4m> <num_frames>
+                        <resample_config_horz>
+                        <resample_config_vert>
+                        <cq_level>[:<cpu_used>]
+                        <downcompup_y4m>
+                        [[<down_y4m>]:[<downcomp_bit>]:[<downcomp_y4m]]
+
+  Notes:
+      <y4m_input> is input y4m video
+      <num_frames> is number of frames to process
+      <horz_resampling_config> is in the format:
+              <horz_p>:<horz_q>:<Lanczos_horz_a>[:horz_x0]
+          similar to what is used by lanczos_resample_y4m utility.
+      <vert_resampling_config> is in the format:
+              <vert_p>:<vert_q>:<Lanczos_vert_a>[:vert_x0]
+          similar to what is used by lanczos_resample_y4m utility.
+      <cq_level>[:<cpu_used>] provides the cq_level parameter of
+          compression along with an optional cpu_used parameter.
+      <downcompup_y4m> is the output y4m video.
+      The last param [[<down_y4m>]:[<downcomp_bit>]:[<downcomp_y4m]]
+          provides names of intermediate files where:
+	      down_y4m is the resampled source
+	      downcomp_bit is the compressed resampled bitstream
+	      downcomp_y4m is the reconstructed bitstream.
+          This parameter string is entirely optional.
+          Besides if provided, each of down_y4m, downcomp_bit and
+          downcomp_y4m are optional by themselves where each can be
+          either provided or empty. If empty the corresponding
+	  intermediate file is deleted.
+
+Example usages:
+4. Similar to use case 1a and 1b above with a compression step in between.
+
+  From build directory run:
+  /path/to/script/lanczos_downcompup.sh Boat_1920x1080_60fps_10bit_420.y4m \
+      20 2:3:6 3:4:6 40:5 /tmp/downup.y4m
+          [Here no intermediate files are stored]
+
+  From build directory run:
+  /path/to/script/lanczos_downcompup.sh Boat_1920x1080_60fps_10bit_420.y4m \
+      20 2:3:6 3:4:6 40:5 /tmp/downup.y4m i\
+      /tmp/down.y4m::/tmp/downrec.y4m
+          [Here the resampled source and its compressed reconstruction are
+           stoted in /tmp/down.y4m and /tmp/downrec.y4m respectively].
+
+  From build directory run:
+  /path/to/script/lanczos_downcompup.sh Boat_1920x1080_60fps_10bit_420.y4m \
+      20 2:3:6 3:4:6 40:5 /tmp/downup.y4m \
+      /tmp/down.y4m:/tmp/down.bit:/tmp/downrec.y4m
+          [Here the resampled source, its compressed bitstream, and the
+	   conpressed reconstruction are stoted in /tmp/down.y4m,
+	   /tmp/downcomp.bit and /tmp/downrec.y4m respectively].
diff --git a/tools/lanczos/lanczos_downcompup.sh b/tools/lanczos/lanczos_downcompup.sh
new file mode 100755
index 0000000..6bf793a
--- /dev/null
+++ b/tools/lanczos/lanczos_downcompup.sh
@@ -0,0 +1,129 @@
+#!/bin/bash
+#
+# Usage:
+#   lanczos_downcompup.sh <input_y4m> <num_frames>
+#                         <resample_config_horz>
+#                         <resample_config_vert>
+#                         <cq_level>[:<cpu_used>]
+#                         <downcompup_y4m>
+#                         [[<down_y4m>]:[<downcomp_bit>]:[<downcomp_y4m]]
+#
+#   Notes:
+#       <y4m_input> is input y4m video
+#       <num_frames> is number of frames to process
+#       <horz_resampling_config> is in the format:
+#               <horz_p>:<horz_q>:<Lanczos_horz_a>[:horz_x0]
+#           similar to what is used by lanczos_resample_y4m utility.
+#       <vert_resampling_config> is in the format:
+#               <vert_p>:<vert_q>:<Lanczos_vert_a>[:vert_x0]
+#           similar to what is used by lanczos_resample_y4m utility.
+#       <cq_level>[:<cpu_used>] provides the cq_level parameter of
+#           compression along with an optional cpu_used parameter.
+#       <downcompup_y4m> is the output y4m video.
+#       The last param [[<down_y4m>]:[<downcomp_bit>]:[<downcomp_y4m]]
+#           provides names of intermediate files where:
+#               down_y4m is the resampled source
+#               downcomp_bit is the compressed resampled bitstream
+#               downcomp_y4m is the reconstructed bitstream.
+#           This parameter string is entirely optional.
+#           Besides if provided, each of down_y4m, downcomp_bit and
+#           downcomp_y4m are optional by themselves where each can be
+#           either provided or empty. If empty the corresponding
+#           intermediate file is deleted.
+#
+
+set -e
+
+tmpdir="/tmp"
+AOMENC="${tmpdir}/aomenc_$$"
+AOMDEC="${tmpdir}/aomdec_$$"
+RESAMPLE="${tmpdir}/lanczos_resample_y4m_$$"
+cp ./aomenc $AOMENC
+cp ./aomdec $AOMDEC
+cp ./lanczos_resample_y4m $RESAMPLE
+
+trap 'echo "Exiting..."; rm -f ${AOMENC} ${AOMDEC} ${RESAMPLE}' EXIT
+
+extra=""
+
+input_y4m=$1
+nframes=$2
+hdconfig=$3
+vdconfig=$4
+codecparams=$5
+downcompup_y4m=$6
+intfiles=$7
+
+#Get codec params cq_level and cpu_used
+OIFS="$IFS"; IFS=':' codecparams_arr=($codecparams); IFS="$OIFS"
+cq_level=${codecparams_arr[0]}
+cpu_used=${codecparams_arr[1]}
+if [[ -z ${cpu_used} ]]; then
+  cpu_used="0"
+fi
+echo "cq_level: ${cq_level}"
+echo "cpu_used: ${cpu_used}"
+
+#Get width and height
+hdr=$(head -1 $input_y4m)
+twidth=$(awk -F ' ' '{ print $2 }' <<< "${hdr}")
+theight=$(awk -F ' ' '{ print $3 }' <<< "${hdr}")
+width=${twidth:1}
+height=${theight:1}
+
+#Parse the intermediate files parameter
+OIFS="$IFS"; IFS=':' intfiles_arr=($intfiles); IFS="$OIFS"
+down_y4m=${intfiles_arr[0]}
+downcomp_bit=${intfiles_arr[1]}
+downcomp_y4m=${intfiles_arr[2]}
+if [[ -z ${down_y4m} ]]; then
+  down_y4m=${tmpdir}/down_$$.y4m
+fi
+if [[ -z ${downcomp_bit} ]]; then
+  downcomp_bit=${tmpdir}/downcomp_$$.bit
+fi
+if [[ -z ${downcomp_y4m} ]]; then
+  downcomp_y4m=${tmpdir}/downcomp_$$.y4m
+fi
+
+#Obtain the horizontal and vertical upsampling configs
+hdconfig_arr=(${hdconfig//:/ })
+huconfig="${hdconfig_arr[1]}:${hdconfig_arr[0]}:${hdconfig_arr[2]}"
+if [[ -n ${hdconfig_arr[3]} ]]; then
+  huconfig="${huconfig}:i${hdconfig_arr[3]}"
+fi
+vdconfig_arr=(${vdconfig//:/ })
+vuconfig="${vdconfig_arr[1]}:${vdconfig_arr[0]}:${vdconfig_arr[2]}"
+if [[ -n ${vdconfig_arr[3]} ]]; then
+  vuconfig="${vuconfig}:i${vdconfig_arr[3]}"
+fi
+
+#Downsample
+$RESAMPLE $input_y4m $nframes $hdconfig $vdconfig $down_y4m
+
+#Compress
+$AOMENC -o $downcomp_bit $down_y4m \
+        --codec=av1 --good --threads=0 --passes=1 --lag-in-frames=19 \
+        --kf-max-dist=65 --kf-min-dist=0 --test-decode=warn -v --psnr \
+        --end-usage=q \
+        --cq-level=${cq_level} \
+        --cpu-used=${cpu_used} \
+        --limit=${nframes} \
+        ${extra}
+$AOMDEC --progress -S --codec=av1 -o $downcomp_y4m $downcomp_bit
+
+#Upsample
+$RESAMPLE $downcomp_y4m $nframes $huconfig $vuconfig $downcompup_y4m ${width}x${height}
+
+#Compute metrics
+#tiny_ssim_highbd $input_y4m $downcompup_y4m
+
+if [[ -z ${intfiles_arr[0]} ]]; then
+  rm $down_y4m
+fi
+if [[ -z ${intfiles_arr[1]} ]]; then
+  rm $downcomp_bit
+fi
+if [[ -z ${intfiles_arr[2]} ]]; then
+  rm $downcomp_y4m
+fi
diff --git a/tools/lanczos/lanczos_downup.sh b/tools/lanczos/lanczos_downup.sh
index ae32133..1607a40 100755
--- a/tools/lanczos/lanczos_downup.sh
+++ b/tools/lanczos/lanczos_downup.sh
@@ -2,30 +2,59 @@
 #
 # Usage:
 #   lanczos_downup.sh <input_y4m> <num_frames>
-#                     <resample_config_horz> <resample_config_vert>
-#                     <downup_y4m> [<down_y4m>]
-#             down_y4m is optional.
+#                     <resample_config_horz>
+#                     <resample_config_vert>
+#                     <downup_y4m>
+#                     [<down_y4m>]
 #
+#   Notes:
+#       <y4m_input> is input y4m video
+#       <num_frames> is number of frames to process
+#       <horz_resampling_config> is in the format:
+#               <horz_p>:<horz_q>:<Lanczos_horz_a>[:horz_x0]
+#           similar to what is used by lanczos_resample_y4m utility.
+#       <vert_resampling_config> is in the format:
+#               <vert_p>:<vert_q>:<Lanczos_vert_a>[:vert_x0]
+#           similar to what is used by lanczos_resample_y4m utility.
+#       <downup_y4m> is the output y4m video.
+#       <down_y4m> provides the intermedite resampled file as an
+#           optional parameter. If skipped the intermediate resampled
+#           file is deleted.
+#
+
+set -e
+
+tmpdir="/tmp"
+AOMENC="${tmpdir}/aomenc_$$"
+AOMDEC="${tmpdir}/aomdec_$$"
+RESAMPLE="${tmpdir}/lanczos_resample_y4m_$$"
+cp ./aomenc $AOMENC
+cp ./aomdec $AOMDEC
+cp ./lanczos_resample_y4m $RESAMPLE
+
+trap 'echo "Exiting..."; rm -f ${AOMENC} ${AOMDEC} ${RESAMPLE}' EXIT
 
 input_y4m=$1
 nframes=$2
 hdconfig=$3
 vdconfig=$4
+downup_y4m=$5
 
+#Get width and height
 hdr=$(head -1 $input_y4m)
 twidth=$(awk -F ' ' '{ print $2 }' <<< "${hdr}")
 theight=$(awk -F ' ' '{ print $3 }' <<< "${hdr}")
 width=${twidth:1}
 height=${theight:1}
 
-downup_y4m=$5
-
+#Get intermediate (down) file
 if [[ -z $6 ]]; then
   down_y4m=/tmp/down_$$.y4m
 else
   down_y4m=$6
 fi
 
+#Obtain the horizontal and vertical upsampling configs
 hdconfig_arr=(${hdconfig//:/ })
 huconfig="${hdconfig_arr[1]}:${hdconfig_arr[0]}:${hdconfig_arr[2]}"
 if [[ -n ${hdconfig_arr[3]} ]]; then
@@ -37,9 +66,8 @@
   vuconfig="${vuconfig}:i${vdconfig_arr[3]}"
 fi
 
-./lanczos_resample_y4m $input_y4m $nframes $hdconfig $vdconfig $down_y4m &&
-./lanczos_resample_y4m $down_y4m $nframes $huconfig $vuconfig \
-    $downup_y4m ${width}x${height}
+$RESAMPLE $input_y4m $nframes $hdconfig $vdconfig $down_y4m &&
+$RESAMPLE $down_y4m $nframes $huconfig $vuconfig $downup_y4m ${width}x${height}
 
 #tiny_ssim_highbd $input_y4m $downup_y4m