VVC_ENC_BIN='/data/src/vvc-vtm/bin/EncoderAppStatic'
VVC_DEC_BIN='/data/src/vvc-vtm/bin/DecoderAppStatic'
Y4M2YUV='/opt/daalatool/tools/y4m2yuv'
YUV2YUV4MPEG='/opt/daalatool/tools/yuv2yuv4mpeg'
VMAF='vmaf'
#for set_name in /media/aom*;do
#  if [ $set_name == '/media/aomctc-a1-4k-as-short' ]; then
#      continue
#  fi
#  for vid_name in $set_name/*.y4m;do
#    echo $vid_name
#done
#done
#/home/xiph/awcy_temp/slot0/vvc-vtm-as-ctc/bin/EncoderAppStatic -i
# /media/aomctc-a5-270p/SparksElevator_480x270p_5994_10bit.y4m
vid_name=$1
FILE=$vid_name
QP=50
FPS_NUM=$(grep -o -a -m 1 -P "(?<=F)([0-9]+)(?=:[0-9]+)" "$FILE")
FPS_DEN=$(grep -o -a -m 1 -P "(?<=F$FPS_NUM:)([0-9]+)" "$FILE")
FPS=$(bc <<< 'scale=3; '$FPS_NUM' / '$FPS_DEN'')
WIDTH=$(head -1 $FILE | tr ' ' '\n' | grep -E '\bW' | tr -d 'W')
HEIGHT=$(head -1 $FILE | tr ' ' '\n' | grep -E '\bH' | tr -d 'H')
CHROMA=$(head -1 $FILE | tr ' ' '\n' | grep -E '\bC')
YUV_CHROMA=$CHROMA
DEPTH=8
case $CHROMA in
C444p10)
  DEPTH=10
  YUV_CHROMA=444
  ;;
C420p10)
  DEPTH=10
  YUV_CHROMA=420
  ;;
C420jpeg | C420mpeg2)
  YUV_CHROMA=420
esac

CONFIG_FILE='/data/src/vvc-vtm/cfg/encoder_randomaccess_vtm_gop16.cfg'
THIS_VID=$(basename $vid_name)
#RECON_FILE_Y4M=${THIS_VID}'-recon_file_y4m.yuv'
#BITSTREAM_Y4M=${THIS_VID}'-32-2f_y4m.bin'
#DEC_STREAM_Y4M_YUV=${THIS_VID}'-32-2f_y4m-dec_stream.yuv'
#DEC_STREAM_Y4M_Y4M=${THIS_VID}'-32-2f_y4m-dec_stream.y4m'
#VMAF_XML=${THIS_VID}'-32-2f_y4m-vmaf.xml'
SUFFIX='32-2f'
RECON_FILE=${THIS_VID}'-'${SUFFIX}'-recon.yuv'
BITSTREAM=${THIS_VID}'-'${SUFFIX}'-bitstream.bin'
DEC_STREAM_YUV=${THIS_VID}'-'${SUFFIX}'-decoded.yuv'
DEC_STREAM_Y4M_Y4M=${THIS_VID}'-32-2f_y4m-dec_stream.y4m'
VMAF_XML=${THIS_VID}'-'${SUFFIX}'-vmaf.xml'

CTC_ENC_PARAMS_Y4M='-i '${FILE}' -c '${CONFIG_FILE}
CTC_ENC_PARAMS_Y4M+=' --FramesToBeEncoded=2 --QP='$QP' --ReconFile=y4m/'${RECON_FILE}
CTC_ENC_PARAMS_Y4M+=' -b y4m/'${BITSTREAM}' '
CTC_DEC_PARAMS_Y4M='-b y4m/'${BITSTREAM}' -d '${DEPTH}' -o y4m/'${DEC_STREAM_YUV}

$VVC_ENC_BIN $CTC_ENC_PARAMS_Y4M
$VVC_DEC_BIN $CTC_DEC_PARAMS_Y4M
$YUV2YUV4MPEG y4m/$(basename $DEC_STREAM_YUV .yuv)  -an1 -ad1 -w$WIDTH -h$HEIGHT -fn$FPS_NUM -fd$FPS_DEN -c$YUV_CHROMA -b$DEPTH
$VMAF -r $FILE -d y4m/$(basename $DEC_STREAM_YUV .yuv).y4m --aom_ctc v3.0 --xml -o y4m/$VMAF_XML --thread 2
# Code-path for YUV Encodes in VVC

$Y4M2YUV $FILE -o ${THIS_VID}_src.yuv

CTC_ENC_PARAMS_YUV='-i '${THIS_VID}'_src.yuv -c '${CONFIG_FILE}
CTC_ENC_PARAMS_YUV+=' --SourceWidth='${WIDTH}' --SourceHeight='${HEIGHT}' --FrameRate='${FPS}' --InputBitDepth='${DEPTH}
CTC_ENC_PARAMS_YUV+=' --FramesToBeEncoded=2 --QP='$QP' --ReconFile=yuv/'${RECON_FILE}
CTC_ENC_PARAMS_YUV+=' -b yuv/'${BITSTREAM}' '
CTC_DEC_PARAMS_YUV=' -b yuv/'${BITSTREAM}' -d '${DEPTH}' -o yuv/'${DEC_STREAM_YUV}' '

$VVC_ENC_BIN $CTC_ENC_PARAMS_YUV
$VVC_DEC_BIN $CTC_DEC_PARAMS_YUV
$YUV2YUV4MPEG yuv/$(basename $DEC_STREAM_YUV .yuv)  -an1 -ad1 -w$WIDTH -h$HEIGHT -fn$FPS_NUM -fd$FPS_DEN -c$YUV_CHROMA -b$DEPTH
$VMAF -r $FILE -d yuv/$(basename $DEC_STREAM_YUV .yuv).y4m --aom_ctc v3.0 --xml -o yuv/$VMAF_XML --thread 2
rm ${THIS_VID}_src.yuv

# Now do MD5 Checksum of only bitstreams
MD5_Y4M=$(md5sum y4m/$BITSTREAM| cut -d " " -f1)
MD5_YUV=$(md5sum yuv/$BITSTREAM| cut -d " " -f1)
if cmp -s y4m/$BITSTREAM yuv/$BITSTREAM
then
  echo $THIS_VID ',OK,'$MD5_Y4M','$MD5_YUV
else
  echo $THIS_VID ',FAIL,'$MD5_Y4M','$MD5_YUV
fi

