# @name V4L2 JPEG Encoder tests
# @desc Test case to validate v4l2 based JPEG Encoding

#Sanity tests
V4L2JPEGENC_FUNC_640_480_NV12_FAKESINK source common.sh; [[ $MACHINE == am62a* ]] ; gst-launch-1.0 videotestsrc num-buffers=200 ! video/x-raw, width=640, height=480, format=NV12 ! v4l2jpegenc ! fakesink || die "V4L2 JPEG Encoding operation did not complete successfully";
V4L2JPEGENC_FUNC_640_480_NV16_FAKESINK source common.sh; [[ $MACHINE == am62a* ]] ; gst-launch-1.0 videotestsrc num-buffers=200 ! video/x-raw, width=640, height=480, format=NV16 ! v4l2jpegenc ! fakesink || die "V4L2 JPEG Encoding operation did not complete successfully";
V4L2JPEGENC_FUNC_640_480_NV21_FAKESINK source common.sh; [[ $MACHINE == am62a* ]] ; gst-launch-1.0 videotestsrc num-buffers=200 ! video/x-raw, width=640, height=480, format=NV21 ! v4l2jpegenc ! fakesink || die "V4L2 JPEG Encoding operation did not complete successfully";
V4L2JPEGENC_FUNC_640_480_NV61_FAKESINK source common.sh; [[ $MACHINE == am62a* ]] ; gst-launch-1.0 videotestsrc num-buffers=200 ! video/x-raw, width=640, height=480, format=NV61 ! v4l2jpegenc ! fakesink || die "V4L2 JPEG Encoding operation did not complete successfully";

#File based tests
V4L2JPEGENC_FUNC_640_480_NV12_FILESINK source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); gst-launch-1.0 videotestsrc num-buffers=200 ! video/x-raw, width=640, height=480, format=NV12 ! v4l2jpegenc ! filesink location=$F || die "V4L2 JPEG Encoding operation did not complete successfully"; MD5=$(md5sum ${F}); echo $MD5 | grep 9745f488145f14a3dfef1554e7db134d || die "V4L2 JPEG Encoded file does not match expected md5sum";
V4L2JPEGENC_FUNC_640_480_NV16_FILESINK source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); gst-launch-1.0 videotestsrc num-buffers=200 ! video/x-raw, width=640, height=480, format=NV16 ! v4l2jpegenc ! filesink location=$F || die "V4L2 JPEG Encoding operation did not complete successfully"; MD5=$(md5sum ${F}); echo $MD5 | grep 48628ab8488084eac9933c43b4c4a30c || die "V4L2 JPEG Encoded file does not match expected md5sum";
V4L2JPEGENC_FUNC_640_480_NV21_FILESINK source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); gst-launch-1.0 videotestsrc num-buffers=200 ! video/x-raw, width=640, height=480, format=NV21 ! v4l2jpegenc ! filesink location=$F || die "V4L2 JPEG Encoding operation did not complete successfully"; MD5=$(md5sum ${F}); echo $MD5 | grep 9745f488145f14a3dfef1554e7db134d || die "V4L2 JPEG Encoded file does not match expected md5sum";
V4L2JPEGENC_FUNC_640_480_NV61_FILESINK source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); gst-launch-1.0 videotestsrc num-buffers=200 ! video/x-raw, width=640, height=480, format=NV61 ! v4l2jpegenc ! filesink location=$F || die "V4L2 JPEG Encoding operation did not complete successfully"; MD5=$(md5sum ${F}); echo $MD5 | grep 48628ab8488084eac9933c43b4c4a30c || die "V4L2 JPEG Encoded file does not match expected md5sum";

#Performance tests
V4L2JPEGENC_FUNC_1280_720_NV12_PERF source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); gst-launch-1.0 videotestsrc num-buffers=900 ! video/x-raw, width=1280, height=720, framerate=30/1, format=NV12 ! v4l2jpegenc ! fpsdisplaysink text-overlay=false video-sink="fakesink sync=true" -v > $F || die "V4L2 JPEG Encoding operation did not complete successfully"; FPS=`cat ${F} | grep average | tail -1 | awk -F"average: " '{print $2}' | awk -F"," '{print $1}'`; echo $FPS | grep 30 || die "fps mismatch w.r.t input file";
V4L2JPEGENC_FUNC_1280_720_NV12_PERF_MULTI source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); gst-launch-1.0 videotestsrc num-buffers=900 ! video/x-raw, width=1280, height=720, framerate=30/1, format=NV12 ! tee name=t t. ! v4l2jpegenc ! fpsdisplaysink text-overlay=false name=sink0 video-sink="fakesink sync=true" -v t. ! v4l2jpegenc ! fpsdisplaysink text-overlay=false name=sink1 video-sink="fakesink sync=true" -v > $F || die "V4L2 JPEG Encoding operation did not complete successfully"; FPS0=`cat ${F} | grep sink0 | grep average | tail -1 | awk -F"average: " '{print $2}' | awk -F"," '{print $1}'`; FPS1=`cat ${F} | grep sink1 | grep average | tail -1 | awk -F"average: " '{print $2}' | awk -F"," '{print $1}'`; echo $FPS0 | grep 30 || die "fps mismatch w.r.t input file"; echo $FPS1 | grep 30 || die "fps mismatch w.r.t input file";
V4L2JPEGENC_FUNC_1280_720_NV21_PERF source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); gst-launch-1.0 videotestsrc num-buffers=900 ! video/x-raw, width=1280, height=720, framerate=30/1, format=NV21 ! v4l2jpegenc ! fpsdisplaysink text-overlay=false video-sink="fakesink sync=true" -v > $F || die "V4L2 JPEG Encoding operation did not complete successfully"; FPS=`cat ${F} | grep average | tail -1 | awk -F"average: " '{print $2}' | awk -F"," '{print $1}'`; echo $FPS | grep 30 || die "fps mismatch w.r.t input file";
V4L2JPEGENC_FUNC_1280_720_NV16_PERF source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); gst-launch-1.0 videotestsrc num-buffers=900 ! video/x-raw, width=1280, height=720, framerate=30/1, format=NV16 ! v4l2jpegenc ! fpsdisplaysink text-overlay=false video-sink="fakesink sync=true" -v > $F || die "V4L2 JPEG Encoding operation did not complete successfully"; FPS=`cat ${F} | grep average | tail -1 | awk -F"average: " '{print $2}' | awk -F"," '{print $1}'`; echo $FPS | grep 30 || die "fps mismatch w.r.t input file";
V4L2JPEGENC_FUNC_1280_720_NV61_PERF source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); gst-launch-1.0 videotestsrc num-buffers=900 ! video/x-raw, width=1280, height=720, framerate=30/1, format=NV61 ! v4l2jpegenc ! fpsdisplaysink text-overlay=false video-sink="fakesink sync=true" -v > $F || die "V4L2 JPEG Encoding operation did not complete successfully"; FPS=`cat ${F} | grep average | tail -1 | awk -F"average: " '{print $2}' | awk -F"," '{print $1}'`; echo $FPS | grep 30 || die "fps mismatch w.r.t input file";
V4L2JPEGENC_FUNC_1920_1080_NV12_PERF source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); gst-launch-1.0 videotestsrc num-buffers=200 ! video/x-raw, width=1920, height=1080, framerate=10/1, format=NV12 ! v4l2jpegenc ! fpsdisplaysink text-overlay=false video-sink="fakesink sync=true" -v > $F || die "V4L2 JPEG Encoding operation did not complete successfully"; FPS=`cat ${F} | grep average | tail -1 | awk -F"average: " '{print $2}' | awk -F"," '{print $1}'`; echo $FPS | grep 10 || die "fps mismatch w.r.t input file";
V4L2JPEGENC_FUNC_2592_1952_NV12_PERF source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); gst-launch-1.0 videotestsrc num-buffers=200 ! video/x-raw, width=2592, height=1952, framerate=10/1, format=NV12 ! v4l2jpegenc ! fpsdisplaysink text-overlay=false video-sink="fakesink sync=true" -v > $F || die "V4L2 JPEG Encoding operation did not complete successfully"; FPS=`cat ${F} | grep average | tail -1 | awk -F"average: " '{print $2}' | awk -F"," '{print $1}'`; echo $FPS | grep 10 || die "fps mismatch w.r.t input file";
V4L2JPEGENC_FUNC_3840_2160_NV12_PERF source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); gst-launch-1.0 videotestsrc num-buffers=100 ! video/x-raw, width=3840, height=2160, framerate=5/1, format=NV12 ! v4l2jpegenc ! fpsdisplaysink text-overlay=false video-sink="fakesink sync=true" -v > $F || die "V4L2 JPEG Encoding operation did not complete successfully"; FPS=`cat ${F} | grep average | tail -1 | awk -F"average: " '{print $2}' | awk -F"," '{print $1}'`; echo $FPS | grep 5 || die "fps mismatch w.r.t input file";

#Runtime PM tests
V4L2JPEGENC_FUNC_640_480_NV12_FILESINK_RUNTIME_PM source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); PM=`cat /sys/kernel/debug/pm_genpd/pm_genpd_summary | grep jpeg | awk '{print $2}'`; echo $PM | grep -w suspended || die "JPEG Encoder not in suspended state"; gst-launch-1.0 videotestsrc num-buffers=200 ! video/x-raw, width=640, height=480, format=NV12 ! v4l2jpegenc ! filesink location=$F || die "V4L2 JPEG Encoding operation did not complete successfully"; MD5=$(md5sum ${F}); echo $MD5 | grep 9745f488145f14a3dfef1554e7db134d || die "V4L2 JPEG Encoded file does not match expected md5sum"; PM=`cat /sys/kernel/debug/pm_genpd/pm_genpd_summary | grep jpeg | awk '{print $2}'`; echo $PM | grep -w suspended || die "JPEG Encoder did not return to suspended state";
V4L2JPEGENC_FUNC_640_480_NV16_FILESINK_RUNTIME_PM source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); PM=`cat /sys/kernel/debug/pm_genpd/pm_genpd_summary | grep jpeg | awk '{print $2}'`; echo $PM | grep -w suspended || die "JPEG Encoder not in suspended state"; gst-launch-1.0 videotestsrc num-buffers=200 ! video/x-raw, width=640, height=480, format=NV16 ! v4l2jpegenc ! filesink location=$F || die "V4L2 JPEG Encoding operation did not complete successfully"; MD5=$(md5sum ${F}); echo $MD5 | grep 48628ab8488084eac9933c43b4c4a30c || die "V4L2 JPEG Encoded file does not match expected md5sum"; PM=`cat /sys/kernel/debug/pm_genpd/pm_genpd_summary | grep jpeg | awk '{print $2}'`; echo $PM | grep -w suspended || die "JPEG Encoder did not return to suspended state";
V4L2JPEGENC_FUNC_640_480_NV21_FILESINK_RUNTIME_PM source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); PM=`cat /sys/kernel/debug/pm_genpd/pm_genpd_summary | grep jpeg | awk '{print $2}'`; echo $PM | grep -w suspended || die "JPEG Encoder not in suspended state"; gst-launch-1.0 videotestsrc num-buffers=200 ! video/x-raw, width=640, height=480, format=NV21 ! v4l2jpegenc ! filesink location=$F || die "V4L2 JPEG Encoding operation did not complete successfully"; MD5=$(md5sum ${F}); echo $MD5 | grep 9745f488145f14a3dfef1554e7db134d || die "V4L2 JPEG Encoded file does not match expected md5sum"; PM=`cat /sys/kernel/debug/pm_genpd/pm_genpd_summary | grep jpeg | awk '{print $2}'`; echo $PM | grep -w suspended || die "JPEG Encoder did not return to suspended state";
V4L2JPEGENC_FUNC_640_480_NV61_FILESINK_RUNTIME_PM source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); PM=`cat /sys/kernel/debug/pm_genpd/pm_genpd_summary | grep jpeg | awk '{print $2}'`; echo $PM | grep -w suspended || die "JPEG Encoder not in suspended state"; gst-launch-1.0 videotestsrc num-buffers=200 ! video/x-raw, width=640, height=480, format=NV61 ! v4l2jpegenc ! filesink location=$F || die "V4L2 JPEG Encoding operation did not complete successfully"; MD5=$(md5sum ${F}); echo $MD5 | grep 48628ab8488084eac9933c43b4c4a30c || die "V4L2 JPEG Encoded file does not match expected md5sum"; PM=`cat /sys/kernel/debug/pm_genpd/pm_genpd_summary | grep jpeg | awk '{print $2}'`; echo $PM | grep -w suspended || die "JPEG Encoder did not return to suspended state";

# System Suspend tests
V4L2JPEGENC_FUNC_640_480_NV12_FILESINK_SYSTEM_SUSPEND source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); PM=`cat /sys/kernel/debug/pm_genpd/pm_genpd_summary | grep jpeg | awk '{print $2}'`; echo $PM | grep -w suspended || die "JPEG Encoder not in suspended state"; rtcwake -s 5 -m mem; PM=`cat /sys/kernel/debug/pm_genpd/pm_genpd_summary | grep jpeg | awk '{print $2}'`; echo $PM | grep -w suspended || die "JPEG Encoder not in suspended state"; gst-launch-1.0 videotestsrc num-buffers=200 ! video/x-raw, width=640, height=480, format=NV12 ! v4l2jpegenc ! filesink location=$F || die "V4L2 JPEG Encoding operation did not complete successfully"; MD5=$(md5sum ${F}); echo $MD5 | grep 9745f488145f14a3dfef1554e7db134d || die "V4L2 JPEG Encoded file does not match expected md5sum"; PM=`cat /sys/kernel/debug/pm_genpd/pm_genpd_summary | grep jpeg | awk '{print $2}'`; echo $PM | grep -w suspended || die "JPEG Encoder did not return to suspended state";
V4L2JPEGENC_FUNC_640_480_NV16_FILESINK_SYSTEM_SUSPEND source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); PM=`cat /sys/kernel/debug/pm_genpd/pm_genpd_summary | grep jpeg | awk '{print $2}'`; echo $PM | grep -w suspended || die "JPEG Encoder not in suspended state"; rtcwake -s 5 -m mem; PM=`cat /sys/kernel/debug/pm_genpd/pm_genpd_summary | grep jpeg | awk '{print $2}'`; echo $PM | grep -w suspended || die "JPEG Encoder not in suspended state"; gst-launch-1.0 videotestsrc num-buffers=200 ! video/x-raw, width=640, height=480, format=NV16 ! v4l2jpegenc ! filesink location=$F || die "V4L2 JPEG Encoding operation did not complete successfully"; MD5=$(md5sum ${F}); echo $MD5 | grep 48628ab8488084eac9933c43b4c4a30c || die "V4L2 JPEG Encoded file does not match expected md5sum"; PM=`cat /sys/kernel/debug/pm_genpd/pm_genpd_summary | grep jpeg | awk '{print $2}'`; echo $PM | grep -w suspended || die "JPEG Encoder did not return to suspended state";
V4L2JPEGENC_FUNC_640_480_NV21_FILESINK_SYSTEM_SUSPEND source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); PM=`cat /sys/kernel/debug/pm_genpd/pm_genpd_summary | grep jpeg | awk '{print $2}'`; echo $PM | grep -w suspended || die "JPEG Encoder not in suspended state"; rtcwake -s 5 -m mem; PM=`cat /sys/kernel/debug/pm_genpd/pm_genpd_summary | grep jpeg | awk '{print $2}'`; echo $PM | grep -w suspended || die "JPEG Encoder not in suspended state"; gst-launch-1.0 videotestsrc num-buffers=200 ! video/x-raw, width=640, height=480, format=NV21 ! v4l2jpegenc ! filesink location=$F || die "V4L2 JPEG Encoding operation did not complete successfully"; MD5=$(md5sum ${F}); echo $MD5 | grep 9745f488145f14a3dfef1554e7db134d || die "V4L2 JPEG Encoded file does not match expected md5sum"; PM=`cat /sys/kernel/debug/pm_genpd/pm_genpd_summary | grep jpeg | awk '{print $2}'`; echo $PM | grep -w suspended || die "JPEG Encoder did not return to suspended state";
V4L2JPEGENC_FUNC_640_480_NV61_FILESINK_SYSTEM_SUSPEND source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); PM=`cat /sys/kernel/debug/pm_genpd/pm_genpd_summary | grep jpeg | awk '{print $2}'`; echo $PM | grep -w suspended || die "JPEG Encoder not in suspended state"; rtcwake -s 5 -m mem; PM=`cat /sys/kernel/debug/pm_genpd/pm_genpd_summary | grep jpeg | awk '{print $2}'`; echo $PM | grep -w suspended || die "JPEG Encoder not in suspended state"; gst-launch-1.0 videotestsrc num-buffers=200 ! video/x-raw, width=640, height=480, format=NV61 ! v4l2jpegenc ! filesink location=$F || die "V4L2 JPEG Encoding operation did not complete successfully"; MD5=$(md5sum ${F}); echo $MD5 | grep 48628ab8488084eac9933c43b4c4a30c || die "V4L2 JPEG Encoded file does not match expected md5sum"; PM=`cat /sys/kernel/debug/pm_genpd/pm_genpd_summary | grep jpeg | awk '{print $2}'`; echo $PM | grep -w suspended || die "JPEG Encoder did not return to suspended state";

#Active Usecase tests
V4L2JPEGENC_FUNC_640_480_NV12_FILESINK_ACTIVE source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); PM=`cat /sys/kernel/debug/pm_genpd/pm_genpd_summary | grep jpeg | awk '{print $2}'`; echo $PM | grep -w suspended || die "JPEG Encoder not in suspended state"; `gst-launch-1.0 videotestsrc num-buffers=200 ! video/x-raw, width=640, height=480, format=NV12 ! v4l2jpegenc ! filesink location=$F sync=true &`; PID=`$!`; sleep 1; rtcwake -s 5 -m mem; wait $PID; MD5=$(md5sum ${F}); echo $MD5 | grep 9745f488145f14a3dfef1554e7db134d || die "V4L2 JPEG Encoded file does not match expected md5sum";
V4L2JPEGENC_FUNC_640_480_NV16_FILESINK_ACTIVE source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); PM=`cat /sys/kernel/debug/pm_genpd/pm_genpd_summary | grep jpeg | awk '{print $2}'`; echo $PM | grep -w suspended || die "JPEG Encoder not in suspended state"; `gst-launch-1.0 videotestsrc num-buffers=200 ! video/x-raw, width=640, height=480, format=NV16 ! v4l2jpegenc ! filesink location=$F sync=true &`; PID=`$!`; sleep 1; rtcwake -s 5 -m mem; wait $PID; MD5=$(md5sum ${F}); echo $MD5 | grep 48628ab8488084eac9933c43b4c4a30c || die "V4L2 JPEG Encoded file does not match expected md5sum";

# Crop based tests
V4L2JPEGENC_FUNC_640_480_NV12_CROP_FILESINK source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); gst-launch-1.0 videotestsrc num-buffers=200 ! video/x-raw, width=640, height=480, format=NV12 ! v4l2jpegenc 'crop-bounds=<400,80,240,400>' ! filesink location=$F || die "V4L2 JPEG Encoding operation did not complete successfully"; MD5=$(md5sum ${F}); echo $MD5 | grep e25197cb9c97c12e7b2f4ede4fdf4efe || die "V4L2 JPEG Encoded file does not match expected md5sum";
V4L2JPEGENC_FUNC_640_480_NV16_CROP_FILESINK source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); gst-launch-1.0 videotestsrc num-buffers=200 ! video/x-raw, width=640, height=480, format=NV16 ! v4l2jpegenc 'crop-bounds=<100,40,540,440>' ! filesink location=$F || die "V4L2 JPEG Encoding operation did not complete successfully"; MD5=$(md5sum ${F}); echo $MD5 | grep 6e44808ea39726f6c3b3b24ad961c3af || die "V4L2 JPEG Encoded file does not match expected md5sum";
V4L2JPEGENC_FUNC_640_480_NV21_CROP_FILESINK source common.sh; [[ $MACHINE == am62a* ]] ; F=$(mktemp); gst-launch-1.0 videotestsrc num-buffers=200 ! video/x-raw, width=640, height=480, format=NV21 ! v4l2jpegenc 'crop-bounds=<0,0,540,440>' ! filesink location=$F || die "V4L2 JPEG Encoding operation did not complete successfully"; MD5=$(md5sum ${F}); echo $MD5 | grep 9e1f1dadad1aae90e610eb5d4e4d4fa0 || die "V4L2 JPEG Encoded file does not match expected md5sum";