Created basic scripts for generating test encoder scripts and running tests

Change-Id: Ice5caeb4b6eb9dc60f3526c47417c5f4ef55b810
diff --git a/README.new b/README.new
new file mode 100644
index 0000000..5cfad27
--- /dev/null
+++ b/README.new
@@ -0,0 +1,25 @@
+This directory contains video speed and quality tests.
+
+The structure is as follows:
+
+video/ - contains the source .yuv files for the tests
+
+The results storage:
+
+<codecname> - contains the data for one particular codec (VP8 or H.264)
+  /<crypticdir>/ - contains runs for one set of codec settings
+     encoder - script that encodes the file with the given settings
+               The <crypticdir> is the MD5 of this file.
+     measurer - script that generates the .results file
+     <sourcename>.<ext> - the encoded file
+     <sourcename>.results - the measured results for that file
+
+Scripts:
+
+generate_settings - generate a set of tests
+
+run_tests - runs the encoder for each dir that's missing a <ext> file.
+    Then runs the measurer for each dir where an <ext> file is present.
+
+report_results - picks a set of results (how?) and reports on them.
+
diff --git a/generate_settings b/generate_settings
new file mode 100755
index 0000000..6dc555b
--- /dev/null
+++ b/generate_settings
@@ -0,0 +1,126 @@
+#!/bin/sh
+#
+# Generate one or more settings files for codec comparision tests.
+#
+set -e
+
+TEMPFILE=$(mktemp ./tempXXXXencoder)
+
+extension() {
+  if [ $CODEC == "vp8" ]; then
+    echo "webm"
+  elif [ $CODEC == "h264" ]; then
+    echo "mkv"
+  else
+    echo "Unknown codec $CODEC"
+    exit 1
+  fi
+}
+
+vp8encoder() {
+# Generate the encoder file. Note that the fingerprint on this is the ID of the
+# encoder instance, so don't make changes here lightly!
+cat << EoF > $TEMPFILE
+#!/bin/sh
+# Generated encoder for encoding VP8 at $rate kbps.
+# Command line format: ./encoder <filename> <framerate> <width> <height>
+# The script must be executed within the directory it's placed in.
+filename=\$1
+frame_rate=\$2
+width=\$3
+height=\$4
+rate=$rate
+
+basename=\$(basename \$filename .yuv)
+
+../../bin/vpxenc --lag-in-frames=0 --target-bitrate=\${rate} --kf-min-dist=3000 \
+      --kf-max-dist=3000 --cpu-used=0 --fps=\${frame_rate}/1 --static-thresh=0 \
+      --token-parts=1 --drop-frame=0 --end-usage=cbr --min-q=2 --max-q=56 \
+      --undershoot-pct=100 --overshoot-pct=15 --buf-sz=1000 \
+      --buf-initial-sz=800 --buf-optimal-sz=1000 --max-intra-rate=1200 \
+      --resize-allowed=0 --drop-frame=0 --passes=1 --good --noise-sensitivity=0 \
+      -w \${width} -h \${height} \${filename} --codec=vp8 \
+      -o ./\$basename.webm \
+
+EoF
+}
+
+h264encoder() {
+# Generate the encoder file. Note that the fingerprint on this is the ID of the
+# encoder instance, so don't make changes here lightly!
+cat << EoF > $TEMPFILE
+#!/bin/sh
+# Generated encoder for encoding H.264 at $rate kbps.
+# Command line format: ./encoder <filename> <framerate> <width> <height>
+# The script must be executed within the directory it's placed in.
+filename=\$1
+frame_rate=\$2
+width=\$3
+height=\$4
+rate=$rate
+
+basename=\$(basename \$filename .yuv)
+
+   ../../bin/x264 --vbv-maxrate \${rate} --vbv-bufsize \${rate} \
+      --vbv-init 0.8 --bitrate \${rate} --fps \${frame_rate} \
+      --profile baseline --no-scenecut --keyint infinite --preset veryslow \
+      --input-res \${width}x\${height} \
+      --tune psnr \
+      -o ./\$basename.mkv \$filename
+EoF
+}
+
+encoder() {
+  if [ $CODEC == "vp8" ]; then
+    vp8encoder
+  else
+    h264encoder
+  fi
+}
+
+measurer() {
+  cat << EoF > $1/measurer
+#!/bin/sh
+# Generated measurer for an encode of $CODEC at target rate $rate kbps.
+# Command line format: measurer filename framerate width heigth
+#
+set -e
+
+filename=\$1
+frame_rate=\$2
+width=\$3
+height=\$4
+extension=$(extension)
+
+basename=\$(basename \$filename .yuv)
+
+rm -f tempyuvfile.yuv
+encoded_rate=( \$(../../bin/ffmpeg -i ./\$basename.\$extension \
+      tempyuvfile.yuv 2>&1 | awk '/bitrate:/ { print \$6 }') )
+psnr=\$(../../bin/psnr \$filename tempyuvfile.yuv \$width \$height 9999)
+cat << EoF2 > \$basename.results
+target_rate=$rate
+encoded_rate=\$encoded_rate
+psnr=\$psnr
+EoF2
+EoF
+}
+
+for (( rate=200; rate<=1600; rate+=200 )); do
+  for CODEC in "vp8" "h264"; do
+    if [ ! -d $CODEC ]; then
+      mkdir $CODEC
+    fi
+    encoder
+    # Move the encoder file into the workdir for this encoding.
+    DIR=$(md5sum $TEMPFILE | cut -b1-12)
+    if [ ! -d $CODEC/$DIR ]; then
+      mkdir $CODEC/$DIR
+    fi
+    mv $TEMPFILE $CODEC/$DIR/encoder
+    measurer $CODEC/$DIR
+    chmod +x $CODEC/$DIR/encoder
+    chmod +x $CODEC/$DIR/measurer
+  done
+done
+
diff --git a/run_tests b/run_tests
new file mode 100755
index 0000000..997c067
--- /dev/null
+++ b/run_tests
@@ -0,0 +1,53 @@
+#!/bin/sh
+#
+# Runs all encoding tests that have not been run, or are missing data.
+#
+# Argument: Filenames.
+
+set -e
+
+for sourcefile in $@; do
+
+  # filename format: <path>/<clip_name>_<width>_<height>_<frame_rate>.yuv
+  pathless=$(basename ${sourcefile})
+  clip_stem=${pathless%.*}
+  part=($(echo $clip_stem | tr "_" "\n"))
+  width=${part[1]}
+  height=${part[2]}
+  frame_rate=${part[3]}
+
+  echo "Clip $clip_stem"
+  # Detecting test dirs by looking for the file name "measurer". Somewhat
+  # fragile.
+  for measurer in */*/measurer; do
+     dir=$(dirname $measurer)
+     echo "Settings $dir"
+     encoded=0
+     results=0
+     for result in $(ls $dir/$clip_stem.* 2>/dev/null); do
+       case $result in
+         *.results)
+           results=1
+           ;;
+         *.webm)
+           encoded=1
+           ;;
+         *.mkv)
+           encoded=1
+           ;;
+         *)
+           echo "What is $result doing here?"
+         esac
+      done
+     if [ $encoded == 0 ]; then
+        (cd $dir; ./encoder ../../$sourcefile $frame_rate $width $height)
+     else
+        echo "Already encoded $clip_stem"
+     fi
+     if [ $results == 0 ]; then
+        (cd $dir; ./measurer ../../$sourcefile $frame_rate $width $height)
+     else
+        echo "Known results for $clip_stem"
+     fi
+  done
+done