ab_compare.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #!/bin/bash
  2. set -e
  3. USAGE=\
  4. 'This script creates images or videos that are compressed by different codecs
  5. but have nearly the same size, facilitating comparison among codecs.
  6. Usage:
  7. ab_compare.sh [OPTIONS] file1.y4m file2.y4m ... fileN.y4m
  8. Options:
  9. -c codec1,codec2,codec3 ...
  10. Specify which codecs to compare. Availaible codecs are daala, daala2 (a
  11. second build of Daala, useful for comparing two versions of Daala against
  12. each other), vp8, vp9, x264, x265, and jpeg.
  13. -v quality
  14. Match the images compressed by other codecs to the size of the Daala file
  15. compressed with that quality.
  16. -b bpp
  17. Compress the images at this many bits per pixel.
  18. -s size
  19. Compress the images to this size in bytes.
  20. -k interval
  21. Set the interval between keyframes. If not given, defaults to 256.
  22. -d daala_root
  23. The root directory of the main build of Daala. If not given, assumed to be ./ .
  24. -D daala2_root
  25. The root directory of a secondary build of Daala. Only needed if
  26. using a second version of daala.
  27. -l libvpx_root
  28. A directory containing a build of libvpx. Only needed if using vp8 or vp9.
  29. -x x264_root
  30. A directory containing a build of x264. Only needed if using x264.
  31. -X x265_root
  32. A directory containing a build of x265. Only needed if using x265.
  33. -a ab_root
  34. Specify the directory containing the other ab_compare scripts. If not given,
  35. assumed to be ./tools/ .
  36. -h
  37. Print this help text.'
  38. while getopts 'c:s:v:b:k:ha:d:D:l:x:X:' OPTIONS; do
  39. case $OPTIONS in
  40. c) CODECS="$OPTARG";;
  41. s) SIZE="$OPTARG";;
  42. v) V="$OPTARG";;
  43. b) BPP="$OPTARG";;
  44. k) KEYINT="$OPTARG";;
  45. h) echo "$USAGE"; exit 0;;
  46. a) AB_ROOT="$OPTARG";;
  47. d) DAALA_ROOT="$OPTARG";;
  48. D) DAALA2_ROOT="$OPTARG";;
  49. l) LIBVPX_ROOT="$OPTARG";;
  50. x) X264_ROOT="$OPTARG";;
  51. X) X265_ROOT="$OPTARG";;
  52. esac
  53. done
  54. shift $(($OPTIND - 1))
  55. if [ -z "$1" ]; then
  56. echo "No input files given."
  57. exit 0
  58. fi
  59. if [ -z "$AB_ROOT" ]; then
  60. AB_ROOT="./tools/"
  61. fi
  62. if [ -z "$DAALA_ROOT" ]; then
  63. DAALA_ROOT='.'
  64. fi
  65. if [ ! -x "$AB_ROOT" ]; then
  66. echo "Comparison scripts not found at '$AB_ROOT'. Please give their location"
  67. echo "with -a."
  68. exit 1
  69. fi
  70. if [ ! -x "$LIBVPX_ROOT" ] && echo "$CODECS" | grep 'vp[89]' > /dev/null; then
  71. echo "Libvpx build not found at '$LIBVPX_ROOT'. Please give its location"
  72. echo "with -l."
  73. exit 1
  74. fi
  75. if [ ! -x "$X264_ROOT" ] && echo "$CODECS" | grep 'x264' > /dev/null; then
  76. echo "x264 build not found at $X264_ROOT. Please give its location"
  77. echo "with -x."
  78. exit 1
  79. fi
  80. if [ ! -x "$X265_ROOT" ] && echo "$CODECS" | grep 'x265' > /dev/null; then
  81. echo "x265 build not found at $X265_ROOT. Please give its location"
  82. echo "with -X."
  83. exit 1
  84. fi
  85. if [ ! -x "$DAALA_ROOT" ] && echo "$CODECS" | grep 'daala$\|daala,' > /dev/null; then
  86. echo "Daala build not found at $DAALA_ROOT. Please give its location"
  87. echo "with -d."
  88. exit 1
  89. fi
  90. if [ ! -x "$DAALA2_ROOT" ] && echo "$CODECS" | grep 'daala2' > /dev/null; then
  91. echo "Second Daala build not found at $DAALA2_ROOT. Please give its location"
  92. echo "with -D."
  93. exit 1
  94. fi
  95. if [ -z "$KEYINT" ]; then
  96. KEYINT=256;
  97. fi
  98. if [ -z "$CODECS" ]; then
  99. CODECS="daala vp8 x264 x265"
  100. else
  101. CODECS=$( echo $CODECS | tr '[,]' '[ ]')
  102. fi
  103. if echo "$CODECS" | grep 'daala$\|daala ' > /dev/null; then
  104. USE_DAALA="true"
  105. else
  106. USE_DAALA="false"
  107. fi
  108. if [[ -n "$V" && "$USE_DAALA" == "false" ]]; then
  109. echo "If you give a Daala quality setting, Daala must be one of the
  110. codecs used in the comparison"
  111. exit 1
  112. fi
  113. if [[ -z "$V" && -z "$SIZE" && -z "$BPP" ]]; then
  114. echo "No method of determining file size specified."
  115. exit 1
  116. fi
  117. if [[ -n "$V" && (-n "$SIZE" || -n "$BPP") || -n "$SIZE" && -n "$BPP" ]]; then
  118. echo "More than one way of determining file size given."
  119. exit 1
  120. fi
  121. for FILE in $@; do
  122. HEADER=$(head -1 "$FILE")
  123. WIDTH=$(echo "$HEADER" | cut -d\ -f 2 | tr -d 'W')
  124. HEIGHT=$(echo "$HEADER" | cut -d\ -f 3 | tr -d 'H')
  125. # CHROMA_FACTOR is the ratio of the size of all the image data to the size of
  126. # the luma plane's data. It's 1.5 for 4:2:0, 2 for 4:2:2 and 3 for 4:4:4. We
  127. # need to know this number in order to calculate the number of frames from
  128. # the file size.
  129. # If the header says nothing about subsampling, it's 4:2:0.
  130. if ! echo "$HEADER" | grep ' C' > /dev/null; then
  131. CHROMA_FACTOR=1.5
  132. else
  133. if echo "$HEADER" | grep ' C420' > /dev/null; then
  134. CHROMA_FACTOR=1.5
  135. elif echo "$HEADER" | grep ' C422' > /dev/null; then
  136. CHROMA_FACTOR=2.0
  137. elif echo "$HEADER" | grep ' C444' > /dev/null; then
  138. CHROMA_FACTOR=3.0
  139. else
  140. echo "Chroma subsampling unknown, assuming 4:2:0."
  141. CHROMA_FACTOR=1.5
  142. fi
  143. fi
  144. INSIZE=$(ls -l "$FILE" | awk '{print $5}')
  145. # This ignores the size of the headers, but should be good enough for most
  146. # cases.
  147. FRAMES=$( echo "scale=5; $INSIZE / ( $WIDTH * $HEIGHT * $CHROMA_FACTOR )" | bc )
  148. FRAMES=$(echo "scale=0;$FRAMES/1" | bc)
  149. if [ -n "$BPP" ]; then
  150. SIZE=$(echo "scale=5;( $BPP * $HEIGHT * $WIDTH * $FRAMES) / 8 + 0.5" | bc)
  151. SIZE=$(echo "scale=0;$SIZE/1" | bc)
  152. fi
  153. if [ $USE_DAALA == "true" ]; then
  154. if [ -n "$V" ]; then
  155. $AB_ROOT/ab_compare_daala.sh -d "$DAALA_ROOT" -n "$FRAMES" -v "$V" "$FILE"
  156. SIZE=$(stat -c %s $(basename "$FILE")-$V.ogv )
  157. else
  158. $AB_ROOT/ab_compare_daala.sh -d "$DAALA_ROOT" -n "$FRAMES" -s "$SIZE" "$FILE"
  159. fi
  160. fi
  161. for CODEC in $CODECS; do
  162. case $CODEC in
  163. daala) : ;;
  164. jpeg) $AB_ROOT/ab_compare_jpeg.sh -d "$DAALA_ROOT" -n "$FRAMES" -s "$SIZE" "$FILE";;
  165. vp8) $AB_ROOT/ab_compare_libvpx.sh -d "$DAALA_ROOT" -r "$LIBVPX_ROOT" -n "$FRAMES" -k "$KEYINT" -s "$SIZE" -c vp8 "$FILE";;
  166. vp9) $AB_ROOT/ab_compare_libvpx.sh -d "$DAALA_ROOT" -r "$LIBVPX_ROOT" -n "$FRAMES" -k "$KEYINT" -s "$SIZE" -c vp9 "$FILE";;
  167. x264) $AB_ROOT/ab_compare_x264.sh -d "$DAALA_ROOT" -r "$X264_ROOT" -n "$FRAMES" -k "$KEYINT" -s "$SIZE" "$FILE";;
  168. x265) $AB_ROOT/ab_compare_x265.sh -d "$DAALA_ROOT" -r "$X265_ROOT" -n "$FRAMES" -k "$KEYINT" -s "$SIZE" "$FILE";;
  169. daala2) $AB_ROOT/ab_compare_daala.sh -d "$DAALA_ROOT" -E "$DAALA2_ROOT/examples/encoder_example" -D "$DAALA2_ROOT/examples/dump_video" -n "$FRAMES" -s "$SIZE" -b "$FILE";;
  170. *) echo "Unknown codec: $CODEC"
  171. esac
  172. done
  173. done