#!/bin/bash
#
# choose_x264_encoding_parameters.sh
#
# shamelessly copied from h264enc ;-)
# as I don't have much experience with x264 encoding profiles
# I would like to thank the Author(s) of h264enc.
#
# This is a extra program because I use it in several scripts. 
# I just added interfaces necessary to be called by my scripts. 
# All the gratitude for collecting and supporting the presets belongs to the original author(s) of h264enc. 
# I hope someone else will find this useful.
# 
# The interaction with this script works through output files that are interpreted by the calling script. 
# To see an example how this works, see menc, riptoh264 or blu2mkv scripts.  
#
# Changelog: 
#    06.02.2010 added support for processing geometries and expansion sets depending on input and output geometry,
#                  comparing source video geometry with user or preset specified output video geometries and output 
#                  of mencoder -vf filterchain to scale/expand video if necessary
#               added support to check video bitrate against preset limits
#               fixed a bug where a wrong begin of the optionslist stops encoding
#               fixed a bug that specified vbv_maxrate and vbv_bufsize get overwritten by presets. Now the smaller one is used.
#               
#
# Here is the original header of h264enc
############################################################################
# $Id: h264enc, v 9.1.0, 2009/11/09, gn Exp $
# UUID: 099ef3da-dc34-41bd-a488-5786f78ab95b
#
# Encode DVDs/video files to the H.264/AVC/MPEG-4 Part 10/JVT/H26L
# video format using MEncoder from MPlayer
#
# Copyright (C) 2006-2009, Grozdan Nikolov
#
# h264enc is free software ; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation ; either version 2 of the License, or
# (at your option) any later version.
#
# h264enc is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY ; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program ; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
############################################################################

Versionnumber=0.2.1

outputfile="choose_x264_encoding_parameters.sh_log"
level="4.1"
FPS="23.976"
bitrate=""
me=""
me_range=""
dct_decimate=""
interlaced=""
a8x8dct=""
fast_pskip=""
trellis=""
scenecut=""
chroma_qp_offset=""
nr=""
ip_factor=""
pb_factor=""
cplx_blur=""
qblur=""
qcomp=""
slices=""
slice_max_size=""
slice_max_mbs=""
partitions=""
mixed_refs=""
qp_min=""
qp_max=""
qp_step=""
keyint=""
keyint_min=""
psy_rd=""
nopsy=""
frameref=""
bframes=""
b_adapt=""
b_bias=""
b_pyramid=""
weight_b=""
weightp=""
direct_pred=""
subq=""
mbtree=""
rc_lookahead=""
sync_lookahead=""
constrained_intra=""
chroma_me=""
cabac=""
aud=""
nal_hrd=""
aq_strength=""
aq_mode=""
deblock=""
stats=""
ratetol=""
maxrate=""
bufsize=""
vbv_init=""
cqm=""
deadzone_inter=""
deadzone_intra=""
global_header=""
level_idc=""
targetwidth=""
targetheight=""
CROP=""
expandvideo="no"
doscale="no"
maxwidth=0
maxheight=0
targetwidth=""
targetheight=""
sourcewidth=""
sourceheight=""
bitrate=""
maxwidth=1920
maxheight=1080

function green { echo -e "\e[1;32m$1\e[0;39;49m"; }

function profile_list
{
   echo "            Use a predefined H.264 quality preset. You can"
   echo "            choose from 61 different presets:"
   echo ""
   echo "            PC Presets"
   echo "            ~~~~~~~~~~"
   echo "            ulq -------> Ultra Low Quality preset"
   echo "            elq -------> Extreme Low Quality preset"
   echo "            vlq -------> Very Low Quality preset"
   echo "            lq --------> Low Quality preset"
   echo "            mq --------> Medium Quality preset"
   echo "            nq --------> Normal Quality preset"
   echo '            hq --------> High Quality preset (recommended)'
   echo '            vhq -------> Very High Quality preset (recommended)'
   echo '            ehq -------> Extreme High Quality preset (recommended)'
   echo "            uhq -------> Ultra High Quality preset"
   echo "            ihq -------> Insane High Quality preset"
   echo "            nlq -------> Near Lossless Quality preset"
   echo "            fghq ------> Film Grain optimized High Quality preset"
   echo "            ani -------> Anime preset"
   echo "            anihq -----> Anime High Quality preset"
   echo '            vdhq ------> VDPAU/DXVA High Quality (High@L4.1) preset'
   echo '            vdehq -----> VDPAU/DXVA Extreme High Quality (High@L4.1) preset'
   echo '            vdihq -----> VDPAU/DXVA Insane High Quality (High@L4.1) preset'
   echo ""
   echo "            Software Compatible Presets"
   echo "            ~~~~~~~~~~~~~~~~~~~~~~~~~~~"
   echo "            fl --------> Flash Player preset"
   echo "            flhq ------> Flash Player High Quality preset"
   echo "            qt --------> QuickTime preset"
   echo "            qthq ------> QuickTime High Quality preset"
   echo ""
   echo "            Device Presets"
   echo "            ~~~~~~~~~~~~~~"
   echo '            bd40 ------> Blu-ray (Main@L4.0) preset'
   echo '            bdhq40 ----> Blu-ray High Quality (High@L4.0) preset'
   echo '            bd41 ------> Blu-ray (Main@L4.1) preset'
   echo '            bdhq41 ----> Blu-ray High Quality (High@L4.1) preset'
   echo '            avchd -----> AVCHD (Main@L4.0) preset'
   echo '            avchdhq ---> AVCHD High Quality (High@L4.1) preset'
   echo '            sdb -------> Stand-alone HW players SD (Baseline@L3.0) preset'
   echo '            sdm -------> Stand-alone HW players SD (Main@L3.0) preset'
   echo '            sdh -------> Stand-alone HW players SD (High@L3.0) preset'
   echo '            hdb -------> Stand-alone HW players HD (Baseline@L4.0) preset'
   echo '            hdm -------> Stand-alone HW players HD (Main@L4.0) preset'
   echo '            hdh -------> Stand-alone HW players HD (High@L4.0) preset'
   echo '            ag1 -------> Android G1 preset'
   echo "            ag1hq -----> Android G1 High Quality preset"
   echo "            ipc -------> Apple iPod Classic preset"
   echo "            ipchq -----> Apple iPod Classic High Quality preset"
   echo "            ip --------> Apple iPod preset"
   echo "            iphq ------> Apple iPod High Quality preset"
   echo "            iph -------> Apple iPhone preset"
   echo "            iphhq -----> Apple iPhone High Quality preset"
   echo "            atv -------> AppleTV preset"
   echo "            atvhq -----> AppleTV High Quality preset"
   echo "            ar --------> Archos 605 preset"
   echo "            arhq ------> Archos 605 High Quality preset"
   echo "            ar5 -------> Archos 5 preset"
   echo "            ar5hq -----> Archos 5 High Quality preset"
   echo "            bb --------> Blackberry Bold 9000 preset"
   echo "            bbhq ------> Blackberry Bold 9000 High Quality preset"
   echo "            nks60 -----> Nokia S60 preset"
   echo "            nks60hq ---> Nokia S60 High Quality preset"
   echo "            psp -------> Sony PSP preset"
   echo "            psphq -----> Sony PSP High Quality preset"
   echo "            ps3 -------> Sony PS3 preset"
   echo "            ps3hq -----> Sony PS3 High Quality preset"
   echo "            mz --------> Microsoft Zune preset"
   echo "            mzhq ------> Microsoft Zune High Quality preset"
   echo "            mx --------> Microsoft XBOX 360 preset"
   echo "            mxhq ------> Microsoft XBOX 360 High Quality preset"
   echo "            htm -------> HTC Magic"
   echo ""
}

function helptext 
{
cat <<EOF
${0##*/} Version $Versionnumber
 
Program to choose x264 encoding presets.
 
Usage: ${0##*/} -p <preset>  -W <sourcewidth> -H <sourceheight> -f <fps> -o <outputfile> -B <bitrate>
                [-eL] [-c <CROPparameters>] [-w <outputwidth>] [-h <outputheight>] 
                [-l <level>] [-b <max_bufsize>] [-r <max_bitrate]  
 
 -o <outputfile>        
            specify outputfile to log settings (mandatory)
 -f <fps>
            frames per second of source video (mandatory)
 -p <preset>
            x264 encoding preset (mandatory)
 -W <sourcewidth>
 -H <sourceheight>
            input video width and height (mandatory)       
 -B <bitrate>
            video bitrate (mandatory)
 -w <outputwidth>
 -h <outputheight>
            output video width and height (optional)
            when none of them are given, source video or preset maximum geometry is used
            if only one is given, the other is calculated from the given value and the source or target aspect ratio
               if -e option is present, black borders are added to reach output aspect ratio
            if both are given, these are used
 -c <CROPparameters>
            parameters created by a "mplayer -cropdetect" run (optional)
 -l <level>
            h.264 level [Default: 4.1]
 -b <max_bufsize>
            maximum bufsize for video stream (optional)
 -r <max_bitrate>
            maximum bitrate for video stream (optional)
 -e         expand video with black borders to match given or demanded aspect ratio (optional) 
 -L         list available presets and exit (optional)

EOF
   profile_list
   exit 0
}

function display_quality_preset_func {
   case "$1" in
      cp)      echo "-> Using custom preset file: '$2'" ;;
      ulq)      echo "-> Using \"Ultra Low Quality\" preset" ;;
      elq)      echo "-> Using \"Extreme Low Quality\" preset" ;;
      vlq)      echo "-> Using \"Very Low Quality\" preset" ;;
      lq)      echo "-> Using \"Low Quality\" preset" ;;
      mq)      echo "-> Using \"Medium Quality\" preset" ;;
      nq)      echo "-> Using \"Normal Quality\" preset" ;;
      hq)      echo "-> Using \"High Quality\" preset" ;;
      vhq)      echo "-> Using \"Very High Quality\" preset" ;;
      ehq)      echo "-> Using \"Extreme High Quality\" preset" ;;
      uhq)      echo "-> Using \"Ultra High Quality\" preset" ;;
      ihq)      echo "-> Using \"Insane High Quality\" preset" ;;
      nlq)      echo "-> Using \"Near Lossless Quality\" preset" ;;
      fghq)      echo "-> Using \"Film Grain optimized High Quality\" preset" ;;
      ani)      echo "-> Using \"Anime\" preset" ;;
      anihq)      echo "-> Using \"Anime High Quality\" preset" ;;
      vdhq)      echo "-> Using \"VDPAU/DXVA High Quality (High@L4.1)\" preset" ;;
      vdehq)      echo "-> Using \"VDPAU/DXVA Extreme High Quality (High@L4.1)\" preset" ;;
      vdihq)      echo "-> Using \"VDPAU/DXVA Insane High Quality (High@L4.1)\" preset" ;;
      fl)      echo "-> Using \"Flash Player\" preset" ;;
      flhq)      echo "-> Using \"Flash Player High Quality\" preset" ;;
      qt)      echo "-> Using \"QuickTime\" preset" ;;
      qthq)      echo "-> Using \"QuickTime High Quality\" preset" ;;
      bd40)      echo "-> Using \"Blu-ray (Main@L4.0)\" preset" ;;
      bdhq40)      echo "-> Using \"Blu-ray High Quality (High@L4.0)\" preset" ;;
      bd41)      echo "-> Using \"Blu-ray (Main@L4.0)\" preset" ;;
      bdhq41)      echo "-> Using \"Blu-ray High Quality (High@L4.1)\" preset" ;;
      avchd)      echo "-> Using \"AVCHD (Main@L4.0)\" preset" ;;
      avchdhq)   echo "-> Using \"AVCHD High Quality (High@L4.1)\" preset" ;;
      sdb)      echo "-> Using \"Stand-alone HW Players SD (Baseline@L3.0)\" preset" ;;
      sdm)      echo "-> Using \"Stand-alone HW Players SD (Main@L3.0)\" preset" ;;
      sdh)      echo "-> Using \"Stand-alone HW Players SD (High@L3.0)\" preset" ;;
      hdb)      echo "-> Using \"Stand-alone HW Players HD (Baseline@L4.0)\" preset" ;;
      hdm)      echo "-> Using \"Stand-alone HW Players HD (Main@L4.0)\" preset" ;;
      hdh)      echo "-> Using \"Stand-alone HW Players HD (High@L4.0)\" preset" ;;
      ag1)      echo "-> Using \"Android G1\" preset" ;;
      ag1hq)      echo "-> Using \"Android G1 High Quality\" preset" ;;
      ipc)      echo "-> Using \"Apple iPod Classic\" preset" ;;
      ipchq)      echo "-> Using \"Apple iPod Classic High Quality\" preset" ;;
      ip)      echo "-> Using \"Apple iPod\" preset" ;;
      iphq)      echo "-> Using \"Apple iPod High Quality\" preset" ;;
      iph)      echo "-> Using \"Apple iPhone\" preset" ;;
      iphhq)      echo "-> Using \"Apple iPhone High Quality\" preset" ;;
      atv)      echo "-> Using \"AppleTV\" preset" ;;
      atvhq)      echo "-> Using \"AppleTV High Quality\" preset" ;;
      ar)      echo "-> Using \"Archos 605\" preset" ;;
      arhq)      echo "-> Using \"Archos 605 High Quality\" preset" ;;
      ar5)      echo "-> Using \"Archos 5\" preset" ;;
      ar5hq)      echo "-> Using \"Archos 5 High Quality\" preset" ;;
      bb)      echo "-> Using \"Blackberry 9000\" preset" ;;
      bbhq)      echo "-> Using \"Blackberry 9000 High Quality\" preset" ;;
      nks60)      echo "-> Using \"Nokia S60\" preset" ;;
      nks60hq)   echo "-> Using \"Nokia S60 High Quality\" preset" ;;
      psp)      echo "-> Using \"Sony PSP\" preset" ;;
      psphq)      echo "-> Using \"Sony PSP High Quality\" preset" ;;
      ps3)      echo "-> Using \"Sony PS3\" preset" ;;
      ps3hq)      echo "-> Using \"Sony PS3 High Quality\" preset" ;;
      mz)      echo "-> Using \"Microsoft Zune\" preset" ;;
      mzhq)      echo "-> Using \"Microsoft Zune High Quality\" preset" ;;
      mx)      echo "-> Using \"Microsoft XBOX 360\" preset" ;;
      mxhq)      echo "-> Using \"Microsoft XBOX 360\" preset" ;;
      htm)       echo "-> Using \"HTC Magic\" preset";;
   esac
}

function device_resolution_mapping {
   case "$profile" in
      fl|flhq)                   echo "-> Valid Flash Player resolutions: unrestricted, up to 1080p (1920x1080)"  >> "$outputfile"
                                 if [ $targetwidth -gt 1920 ] || [ $targetheight -gt 1080 ] ; then 
                                    doscale="yes"
                                    maxwidth=1920
                                    maxheight=1080  
                                    echo "-> rescaling to ${maxwidth}x$maxheight" >> "$outputfile" 
                                 fi                                
                                 ;;
      ag1|ag1hq)                 echo "-> Valid Android G1 resolutions: up to 480x368"  >> "$outputfile"
                                 doscale="yes"
                                 maxwidth=480
                                 maxheight=368
                                 echo "-> rescaling to ${maxwidth}x$maxheight" >> "$outputfile" 
                                 ;;
      ipc|ipchq)                 echo "-> Valid iPod Classic resolutions: 320x240"  >> "$outputfile"
                                 doscale="yes"
                                 maxwidth=480
                                 maxheight=368
                                 echo "-> rescaling to ${maxwidth}x$maxheight" >> "$outputfile" 
                                 ;;
      ip|iphq)                   echo "-> Valid iPod resolutions: 320x240|480x320|640x480"  >> "$outputfile"
                                 case "${targetwidth}x$targetheight" in 
                                    320x240) echo "-> resolition o.k." >> "$outputfile" ;;
                                    480x320) echo "-> resolition o.k." >> "$outputfile" ;;
                                    640x480) echo "-> resolition o.k." >> "$outputfile" ;;
                                    *) doscale="yes"
                                       maxwidth=640
                                       maxheight=480
                                       echo "-> rescaling to ${maxwidth}x$maxheight" >> "$outputfile" 
                                       ;;
                                 esac
                                 ;;
      iph|iphhq)                 echo "-> Valid iPhone resolutions: 320x240|640x480"  >> "$outputfile"
                                 case "${targetwidth}x$targetheight" in 
                                    320x240) echo "-> resolition o.k." >> "$outputfile" ;;
                                    640x480) echo "-> resolition o.k." >> "$outputfile" ;;
                                    *) doscale="yes"
                                       maxwidth=640
                                       maxheight=480
                                       echo "-> rescaling to ${maxwidth}x$maxheight" >> "$outputfile" 
                                       ;;
                                 esac
                                 ;;
      atv|atvhq)                 echo "-> Valid AppleTV resolutions: unrestricted, up to 720p (1280x720)" >> "$outputfile"
                                 if [ $targetwidth -gt 1280 ] || [ $targetheight -gt 720 ] ; then 
                                     doscale="yes"
                                    maxwidth=1280
                                    maxheight=720  
                                    echo "-> rescaling to ${maxwidth}x$maxheight" >> "$outputfile" 
                                 fi                                
                                 ;;
      ar|arhq|ar5|ar5hq)         echo "-> Valid Archos 605/5 resolutions: up to 720x576"  >> "$outputfile";
                                 if [ $targetwidth -gt 720 ] || [ $targetheight -gt 576 ] ; then 
                                     doscale="yes"
                                    maxwidth=720
                                    maxheight=576  
                                    echo "-> rescaling to ${maxwidth}x$maxheight" >> "$outputfile" 
                                 fi                                
                                 ;;
      bb|bbhq)                   echo "-> Valid Blackberry 9000 resolutions: 320x240|480x320|640x480"  >> "$outputfile"
                                 case "${targetwidth}x$targetheight" in 
                                    320x240) echo "-> resolition o.k." >> "$outputfile" ;;
                                    480x320) echo "-> resolition o.k." >> "$outputfile" ;;
                                    640x480) echo "-> resolition o.k." >> "$outputfile" ;;
                                    *) doscale="yes"
                                       maxwidth=640
                                       maxheight=480
                                       echo "-> rescaling to ${maxwidth}x$maxheight" >> "$outputfile" 
                                       ;;
                                 esac
                                 ;;
      nks60|nks60hq)             echo "-> Valid Nokia S60 resolutions: 320x240|352x416"  >> "$outputfile"
                                 case "${targetwidth}x$targetheight" in 
                                    320x240) echo "-> resolition o.k." >> "$outputfile" ;;
                                    352x416) echo "-> resolition o.k." >> "$outputfile" ;;
                                    *) doscale="yes"
                                       maxwidth=352
                                       maxheight=416
                                       echo "-> rescaling to ${maxwidth}x$maxheight" >> "$outputfile" 
                                       ;;
                                 esac
                                 ;;
      psp|psphq)                 echo "-> Valid Sony PSP resolutions: 320x240|368x208|352x480|480x272|720x480" >> "$outputfile" 
                                 case "${targetwidth}x$targetheight" in 
                                    320x240) echo "-> resolition o.k." >> "$outputfile" ;;
                                    368x208) echo "-> resolition o.k." >> "$outputfile" ;;
                                    352x480) echo "-> resolition o.k." >> "$outputfile" ;;
                                    480x272) echo "-> resolition o.k." >> "$outputfile" ;;
                                    720x480) echo "-> resolition o.k." >> "$outputfile" ;;
                                    *) doscale="yes"
                                       maxwidth=720
                                       maxheight=480
                                       echo "-> rescaling to ${maxwidth}x$maxheight" >> "$outputfile" 
                                       ;;
                                 esac
                                 ;;
      ps3|ps3hq)                 echo "-> Valid Sony PS3 resolutions: unrestricted, up to 1080p (1920x1080)" >> "$outputfile" 
                                 if [ $targetwidth -gt 1920 ] || [ $targetheight -gt 1080 ] ; then 
                                    doscale="yes"
                                    maxwidth=1920
                                    maxheight=1080  
                                    echo "-> rescaling to ${maxwidth}x$maxheight" >> "$outputfile" 
                                 fi                                
                                 ;;
      mz|mzhq)                   echo "-> Valid MS Zune resolutions: 320x240|720x480|720x576" >> "$outputfile" 
                                 case "${targetwidth}x$targetheight" in 
                                    320x240) echo "-> resolition o.k." >> "$outputfile" ;;
                                    720x480) echo "-> resolition o.k." >> "$outputfile" ;;
                                    720x576) echo "-> resolition o.k." >> "$outputfile" ;;
                                    *) doscale="yes"
                                       maxwidth=720
                                       maxheight=576
                                       echo "-> rescaling to ${maxwidth}x$maxheight" >> "$outputfile" 
                                       ;;
                                 esac
                                 ;;
      mx|mxhq)                   echo "-> Valid XBOX 360 resolutions: unrestricted, up to 1080p (1920x1080)" >> "$outputfile" 
                                 if [ $targetwidth -gt 1920 ] || [ $targetheight -gt 1080 ] ; then 
                                     doscale="yes"
                                    maxwidth=1920
                                    maxheight=1080  
                                    echo "-> rescaling to ${maxwidth}x$maxheight" >> "$outputfile" 
                                 fi                                
                                 ;;
      bd40|bdhq40|bd41|bdhq41)   echo "-> Valid Blu-ray resolutions: 720i/p (1280x720) and 1080i/p (1920x1080)" >> "$outputfile" 
                                 case "${targetwidth}x$targetheight" in 
                                    1280x720) echo "-> resolition o.k." >> "$outputfile" ;;
                                    1920x1080) echo "-> resolition o.k." >> "$outputfile" ;;
                                    *) doscale="yes"
                                       maxwidth=1920
                                       maxheight=1080
                                       echo "-> rescaling to ${maxwidth}x$maxheight" >> "$outputfile" 
                                       ;;
                                 esac
                                 ;;
      avchd|avchdhq)             echo "-> Valid AVCHD resolutions: 1920x1080, 1440x1080, 1280x720, 720x480, 720x576" >> "$outputfile"
                                 case "${targetwidth}x$targetheight" in 
                                    1920x1080) echo "-> resolition o.k." >> "$outputfile" ;;
                                    1440x1080) echo "-> resolition o.k." >> "$outputfile" ;;
                                    1280x720) echo "-> resolition o.k." >> "$outputfile" ;;
                                    720x480) echo "-> resolition o.k." >> "$outputfile" ;;
                                    720x576) echo "-> resolition o.k." >> "$outputfile" ;;
                                    *) doscale="yes"
                                       maxwidth=1920
                                       maxheight=1080
                                       echo "-> rescaling to ${maxwidth}x$maxheight" >> "$outputfile" 
                                       ;;
                                 esac
                                 ;;
   esac
}

function pre_calculate_geometry
{
   local croppedwidth=""
   local croppedheight=""
   local inaspectheight=""
   local inaspectwidth=""
   local expandedwidth=""
   local expandedheight=""
   
   if [ ! "$CROP" = "" ] ; then
      # overwrite original source geometry with geometry after cropping black borders
      croppedwidth=`echo "$CROP" | sed -r "s/[-]?([0-9]+):[-]?[0-9]+:[-]?[0-9]+:[-]?[0-9]+/\\1/"`
      croppedheight=`echo "$CROP" | sed -r "s/[-]?[0-9]+:[-]?([0-9]+):[-]?[0-9]+:[-]?[0-9]+/\\1/"`
      sourceaspect=`perl -e "print ( $croppedwidth / $croppedheight )"`
      sourcewidth="$croppedwidth"
      sourceheight="$croppedheight"
   else
      sourceaspect=`perl -e "print ( $sourcewidth / $targetheight )"`
   fi   
  
   if [ "$targetwidth" = "" ] && [ "$targetheight" = "" ] ; then
      # none specified
      targetwidth="$sourcewidth"
      targetheight="$sourceheight"
   elif [ ! "$targetwidth" = "" ] && [ "$targetheight" = "" ] ; then
      # making sure input is a multiple of 16 (MPEG standard)
      expandedwidth=`perl -e "print ( $sourcewidth + ${sourcewidth}%16)"` 
      # calculate target height
      inaspectheight=`perl -e "use integer; print ( $expandedwidth / $sourceaspect )"`
      targetheight=`perl -e "if ( $inaspectheight > $sourceheight ) { if ( ${inaspectheight}%16 < 7 ) { print ( $inaspectheight - ${inaspectheight}%16 ); } else { print ( $inaspectheight + ( 16 - ${inaspectheight}%16 ))}} else { print $sourceheight ; }"`
   elif [ "$targetwidth" = "" ] && [ ! "$targetheight" = "" ] ; then
      # making sure input is a multiple of 16 (MPEG standard)
      expandedheight=`perl -e "print ( $sourceheight + ${sourceheight}%16)"` 
      # calculate target width
      inaspectwidth=`perl -e "use integer; print ( $expandedheight * $sourceaspect )"`
      targetwidth=`perl -e "if ( $inaspectwidth > $sourcewidth ) { if ( ${inaspectwidth}%16 < 7 ) { print ( $inaspectwidth - ${inaspectwidth}%16 ); } else { print ( $inaspectwidth + ( 16 - ${inaspectwidth}%16 ))}} else { print $sourcewidth ; }"`
   fi
   targetaspect=`perl -e "print ( $targetwidth / $targetheight )"`
}

function build_filter_chain 
{
   local inaspectheight=""
   local inaspectwidth=""
   local outputwidth=""
   local outputheight=""
   local SCALE=""
   
   if [ "$doscale" = "yes" ] ; then
      presetaspect=`perl -e "print ( $maxwidth / $maxheight )"`
      if [ 0 -lt `perl -e "if ( $targetaspect > $presetaspect ) { print 1 ; } else { print 0 ; }"` ] ; then
         echo "source is wider than selected output geometry"
         expandvideo="yes"
         outputwidth=$maxwidth
         # calculate target height
         inaspectheight=`perl -e "print ( int ( $maxwidth / $presetaspect ) )"`
         outputheight=`perl -e "if ( $inaspectheight > $maxheight ) { if ( ${inaspectheight}%16 < 7 ) { print ( $inaspectheight - ${inaspectheight}%16 ); } else { print ( $inaspectheight + ( 16 - ${inaspectheight}%16 ))}} else { print $maxheight ; }"`
      elif [ 0 -lt `perl -e "if ( $targetaspect < $presetaspect ) { print 1 ; } else { print 0 ; }"` ] ; then
         echo "source is higher than selected output geometry"
         expandvideo="yes"
         outputheight=$maxheight
         # calculate target width
         inaspectwidth=`perl -e "print ( int ( $maxheight * $presetaspect ) )"`
         outputwidth=`perl -e "if ( $inaspectwidth > $maxwidth ) { if ( ${inaspectwidth}%16 < 7 ) { print ( $inaspectwidth - ${inaspectwidth}%16 ); } else { print ( $inaspectwidth + ( 16 - ${inaspectwidth}%16 ))}} else { print $maxwidth ; }"`
      else
         echo "exactly matching aspect ratios"
         outputwidth=$maxwidth
         outputheight=$maxheight
      fi
      SCALE="scale=$outputwidth:$outputheight"
   else
      outputwidth=$targetwidth
      outputheight=$targetheight      
   fi
   # add black borders to reach a preset aspect ratio?
   if [ "$expandvideo" = "yes" ] ; then    
      if [ $maxheight -gt $outputheight ] || [ $maxwidth -gt $outputheight ] ; then
         if [ "$SCALE" = "" ] ; then
            SCALE="expand=$maxwidth:$maxheight"
         else
            SCALE="${SCALE},expand=$maxwidth:$maxheight"
         fi
      fi
   fi
   if [ ! "$SCALE" = "" ] ; then
      echo "vf_scale_expand_filter_chain $SCALE" >> "$outputfile"
   fi
}

function bitrate_limit_check
{
	case "$profile" in
		bd40)
		if [ $bitrate -gt 20000 ]; then
			echo "-> Blu-ray video bitrate may not exceed 20000 kbps!"
			bitrate="20000"
		fi
		;;
		bdhq40)
		if [ $bitrate -gt 25000 ]; then
			echo "-> Blu-ray video bitrate may not exceed 25000 kbps!"
			bitrate="25000"
		fi
		;;
		bd41|bdhq41)
		if [ $bitrate -gt 30000 ]; then
			echo "-> Blu-ray video bitrate may not exceed 30000 kbps!"
			bitrate="30000"
		fi
		;;
		avchd|avchdhq)
		if [ $bitrate -gt 17000 ]; then
			echo "-> AVCHD bitrate may not exceed 17000 kbps!"
			bitrate="17000"
		fi
		;;
		ag1|ag1hq)
		if [ $bitrate -gt 2000 ]; then
			echo "-> Android G1 video bitrate may not exceed 2000 kbps!"
			bitrate="2000"
		fi
		;;
		ipc|ipchq)
		if [ $bitrate -gt 768 ]; then
			echo ""
			echo "-> iPod Classic bitrate may not exceed 768 kbps!"
			echo "-> Falling back to maximum allowed bitrate!"
			bitrate="768"
		fi
		;;
		ip|iphq|mz|mzhq)
		if [ $bitrate -gt 2500 ]; then
			echo "-> iPod/Zune video bitrate may not exceed 2500 kbps!"
			bitrate="2500"
		fi
		;;
		iph|iphhq)
		if [ $bitrate -gt 1500 ]; then
			echo "-> iPhone video bitrate may not exceed 1500 kbps!"
			bitrate="1500"
		fi
		;;
		atv|atvhq)
		if [ $bitrate -gt 12000 ]; then
			echo "-> AppleTV video bitrate may not exceed 12000 kbps!"
			bitrate="12000"
		fi
		;;
		ar|arhq)
		if [ $bitrate -gt 5000 ]; then
			echo "-> Archos 605 video bitrate may not exceed 5000 kbps!"
			bitrate="5000"
		fi
		;;
		ar5|ar5hq)
		if [ $bitrate -gt 2000 ]; then
			echo "-> Archos 5 video bitrate may not exceed 2000 kbps!"
			bitrate="2000"
		fi
		;;
		bb|bbhq)
		if [ $bitrate -gt 1500 ]; then
			echo "-> Blackberry 9000 video bitrate may not exceed 1500 kbps!"
			bitrate="1500"
		fi
		;;
		nks60|nks60hq)
		if [ $bitrate -gt 384 ]; then
			echo ""
			echo "-> Nokia S60 video bitrate may not exceed 384 kbps!"
			echo "-> Falling back to maximum allowed bitrate!"
			bitrate="384"
		fi
		;;
		psp|psphq)
		if [ $bitrate -gt 4000 ]; then
			echo "-> PSP video bitrate may not exceed 4000 kbps!"
			bitrate="4000"
		fi
		;;
		ps3|ps3hq)
		if [ $bitrate -gt 20000 ]; then
			echo "-> PS3 video bitrate may not exceed 20000 kbps!"
			bitrate="20000"
		fi
		;;
		mx|mxhq)
		if [ $bitrate -gt 20000 ]; then
			echo "-> XBOX 360 video bitrate may not exceed 20000 kbps!"
			bitrate="20000"
		fi
		;;
	esac
}

while getopts Ll:p:b:r:o:f:w:W:h:H:B:c:e option;
do
  case "$option"
  in
    L)  profile_list; exit;;
    l)  level="$OPTARG";;
    p)  profile="$OPTARG";;
    w)  targetwidth="$OPTARG";;
    h)  targetheight="$OPTARG";;
    W)  sourcewidth="$OPTARG";;
    H)  sourceheight="$OPTARG";;
    c)  CROP="$OPTARG";;
    r)  maxrate=":vbv_maxrate=$OPTARG";;
    b)  bufsize=":vbv_bufsize=$OPTARG";;
    o)  outputfile="$OPTARG";;
    f)  FPS="$OPTARG";;
    B)  bitrate="$OPTARG";;
    e)  expandvideo="yes";;
    *)  helptext ;;
  esac
done
shift $(($OPTIND - 1))

if [ -f "$outputfile" ] ; then
   rm "$outputfile";
fi

case "$profile" in
   ulq|elq|vlq|lq|mq|nq|hq|vhq|ehq|uhq|ihq|nlq|fghq|ani|anihq|vdhq|vdehq|vdihq|sdb|sdm|sdh|hdb|hdm|hdh|fl|flhq|qt|qthq|bd40|bdhq40|bd41|bdhq41|avchd|avchdhq|ag1|ag1hq|ipc|ipchq|ip|iphq|iph|iphhq|atv|atvhq|ar|arhq|ar5|ar5hq|bb|bbhq|nks60|nks60hq|psp|psphq|ps3|ps3hq|mz|mzhq|mx|mxhq|htm)
      display_quality_preset_func >> "$outputfile"
      ;;
   "")
      echo "-> No preset specified!"
      helptext
      exit 1
   ;;
   *)
      echo "-> Unknown preset"
      echo "-> Use: ${0##*/} "
      exit 1
   ;;
esac

if [ ! "$bitrate" = "" ] ; then 
   bitrate_limit_check
fi

case "$FPS" in
   5.000|5.00)         keyint=":keyint=50" ;;
   10.000|10.00)         keyint=":keyint=100" ;;
   12.000|12.00)         keyint=":keyint=120" ;;
   15.000|15.00)         keyint=":keyint=150" ;;
   15.98[4-5]|15.98)      keyint=":keyint=160" ;;
   17.982|17.98)         keyint=":keyint=180" ;;
   25.000|25.00)         keyint=":keyint=250" ;;
   50.000|50.00)         keyint=":keyint=500" ;;
   100.000|100.00)         keyint=":keyint=1000" ;;
   23.976|24.000|23.97|24.00)   keyint=":keyint=240" ;;
   29.970|29.97|30.000|30.00)   keyint=":keyint=300" ;;
   59.940|59.94|60.000|60.00)   keyint=":keyint=600" ;;
   119.880|119.88)         keyint=":keyint=1200" ;;
   *|"")            keyint=":keyint=250" ;;
esac
#keyint2=$(($(echo $keyint | sed 's/:keyint=//')/10))
#keyint_min=":keyint_min=$(($(echo $keyint | awk -F= '{print $2}')/10))"

case "$level" in
   1.[0-3]|2.[0-2]|3.[0-2]|4.[0-2]|5.[0-1]|"")
      if [ -z "$level" ]; then
         h264level=":level_idc=41"
      else
         h264level=":level_idc=$(echo $level | sed 's|\.||')"
      fi
   ;;
      auto)
      true
   ;;
   *)
      echo
      echo "-> Unknown H.264 level: $level"
      echo "-> Valid levels: 1.0, 1.1, 1.2, 1.3, 2.0, 2.1, 2.2, 3.0"
      echo "                 3.1, 3.2, 4.0, 4.1, 4.2, 5.0, 5.1, auto"
      echo "-> Falling back to level 4.1"
      echo
      h264level=":level_idc=41"
   ;;
esac

case "$profile" in
   
	ulq)
	# Ultra Low Quality
	frameref=":frameref=0"
	mixed_refs=":nomixed_refs"
	bframes=":bframes=0"
	b_adapt=":b_adapt=0"
	b_pyramid=":b_pyramid=none"
	weight_b=":noweight_b"
	weightp=":weightp=0"
	direct_pred=":direct_pred=none"
	aq_mode=":aq_mode=2"
	me=":me=dia"
	subq=":subq=1"
	mbtree=":nombtree"
	chroma_me=":nochroma_me"
	trellis=":trellis=0"
	cabac=":nocabac"
	deblock=":nodeblock"
	a8x8dct=":no8x8dct"
	partitions=":partitions=none"
	fast_pskip=":fast_pskip"
	dct_decimate=":dct_decimate"
	level_idc="$h264level"
	;;
	elq)
	# Extreme Low Quality
	frameref=":frameref=1"
	mixed_refs=":nomixed_refs"
	bframes=":bframes=0"
	b_adapt=":b_adapt=0"
	b_pyramid=":b_pyramid=none"
	weight_b=":noweight_b"
	weightp=":weightp=0"
	direct_pred=":direct_pred=none"
	aq_mode=":aq_mode=2"
	me=":me=dia"
	subq=":subq=2"
	mbtree=":nombtree"
	chroma_me=":nochroma_me"
	trellis=":trellis=0"
	cabac=":nocabac"
	deblock=":nodeblock"
	a8x8dct=":no8x8dct"
	partitions=":partitions=none"
	fast_pskip=":fast_pskip"
	dct_decimate=":dct_decimate"
	level_idc="$h264level"
	;;
	vlq)
	# Very Low Quality
	frameref=":frameref=1"
	mixed_refs=":nomixed_refs"
	bframes=":bframes=4"
	b_adapt=":b_adapt=0"
	b_pyramid=":b_pyramid=none"
	weight_b=":noweight_b"
	weightp=":weightp=0"
	direct_pred=":direct_pred=none"
	aq_mode=":aq_mode=2"
	me=":me=dia"
	subq=":subq=3"
	mbtree=":nombtree"
	chroma_me=":nochroma_me"
	trellis=":trellis=0"
	cabac=":nocabac"
	deblock=":deblock"
	a8x8dct=":no8x8dct"
	partitions=":partitions=p8x8,b8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":dct_decimate"
	level_idc="$h264level"
	;;
	lq)
	# Low Quality
	frameref=":frameref=2"
	mixed_refs=":nomixed_refs"
	bframes=":bframes=6"
	b_adapt=":b_adapt=1"
	b_pyramid=":b_pyramid=normal"
	weight_b=":noweight_b"
	weightp=":weightp=0"
	direct_pred=":direct_pred=spatial"
	aq_mode=":aq_mode=2"
	me=":me=dia"
	subq=":subq=4"
	mbtree=":nombtree"
	chroma_me=":nochroma_me"
	trellis=":trellis=0"
	cabac=":cabac"
	deblock=":deblock"
	a8x8dct=":no8x8dct"
	partitions=":partitions=p8x8,b8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":dct_decimate"
	level_idc="$h264level"
	;;
	mq)
	# Medium Quality
	frameref=":frameref=2"
	mixed_refs=":mixed_refs"
	bframes=":bframes=8"
	b_adapt=":b_adapt=1"
	b_pyramid=":b_pyramid=normal"
	weight_b=":weight_b"
	weightp=":weightp=0"
	direct_pred=":direct_pred=spatial"
	aq_mode=":aq_mode=2"
	me=":me=hex"
	subq=":subq=4"
	mbtree=":nombtree"
	chroma_me=":nochroma_me"
	trellis=":trellis=0"
	cabac=":cabac"
	deblock=":deblock"
	a8x8dct=":no8x8dct"
	partitions=":partitions=p8x8,b8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":dct_decimate"
	level_idc="$h264level"
	;;
	nq)
	# Normal Quality
	frameref=":frameref=3"
	mixed_refs=":mixed_refs"
	bframes=":bframes=10"
	b_adapt=":b_adapt=1"
	b_pyramid=":b_pyramid=normal"
	weight_b=":weight_b"
	weightp=":weightp=1"
	direct_pred=":direct_pred=spatial"
	aq_mode=":aq_mode=2"
	me=":me=hex"
	subq=":subq=5"
	mbtree=":nombtree"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	deblock=":deblock"
	a8x8dct=":no8x8dct"
	partitions=":partitions=p8x8,b8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc="$h264level"
	;;
	hq)
	# High Quality - from here till IHQ
	# it's High profile
	frameref=":frameref=3"
	mixed_refs=":mixed_refs"
	bframes=":bframes=4"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=normal"
	weight_b=":weight_b"
	weightp=":weightp=1"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=1"
	me=":me=umh"
	me_range=":me_range=16"
	subq=":subq=6"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.8,0.2"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	deblock=":deblock"
	a8x8dct=":8x8dct"
	partitions=":partitions=p8x8,b8x8,i8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc="$h264level"
	;;
	vhq)
	# Very High Quality
	frameref=":frameref=4"
	mixed_refs=":mixed_refs"
	bframes=":bframes=5"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=normal"
	weight_b=":weight_b"
	weightp=":weightp=2"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=1"
	me=":me=umh"
	me_range=":me_range=24"
	subq=":subq=7"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.8,0.2"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	deblock=":deblock"
	a8x8dct=":8x8dct"
	partitions=":partitions=p8x8,b8x8,i8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc="$h264level"
	;;
	ehq)
	# Extreme High Quality
	frameref=":frameref=5"
	mixed_refs=":mixed_refs"
	bframes=":bframes=6"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=normal"
	weight_b=":weight_b"
	weightp=":weightp=2"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=1"
	me=":me=umh"
	me_range=":me_range=32"
	subq=":subq=8"
	mbtree=":mbtree"
	rc_lookahead=":rc_lookahead=50"
	psy_rd=":psy_rd=0.8,0.2"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	deblock=":deblock"
	a8x8dct=":8x8dct"
	partitions=":partitions=p8x8,b8x8,i8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc="$h264level"
	;;
	uhq)
	# Ultra High Quality
	frameref=":frameref=6"
	mixed_refs=":mixed_refs"
	bframes=":bframes=7"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=normal"
	weight_b=":weight_b"
	weightp=":weightp=2"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=1"
	me=":me=esa"
	me_range=":me_range=48"
	subq=":subq=9"
	mbtree=":mbtree"
	rc_lookahead=":rc_lookahead=60"
	psy_rd=":psy_rd=0.8,0.2"
	chroma_me=":chroma_me"
	trellis=":trellis=2"
	cabac=":cabac"
	deblock=":deblock"
	a8x8dct=":8x8dct"
	partitions=":partitions=p8x8,b8x8,i8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc="$h264level"
	;;
	ihq)
	# Insane High Quality
	frameref=":frameref=10"
	mixed_refs=":mixed_refs"
	bframes=":bframes=7"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=normal"
	weight_b=":weight_b"
	weightp=":weightp=2"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=1"
	me=":me=tesa"
	me_range=":me_range=64"
	subq=":subq=10"
	mbtree=":mbtree"
	rc_lookahead=":rc_lookahead=60"
	psy_rd=":psy_rd=0.8,0.2"
	chroma_me=":chroma_me"
	trellis=":trellis=2"
	cabac=":cabac"
	deblock=":deblock"
	a8x8dct=":8x8dct"
	partitions=":partitions=all"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc="$h264level"
	;;
	nlq)
	# Near Lossless Quality
	bitrate="crf=15"
	frameref=":frameref=3"
	bframes=":bframes=3"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=normal"
	weight_b=":weight_b"
	weightp=":weightp=1"
	subq=":subq=6"
	mbtree=":mbtree"
	me=":me=hex"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=1"
	partitions=":partitions=p8x8,b8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	a8x8dct=":no8x8dct"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc="$h264level"
	;;
	fghq)
	# Film Grain optimized High Quality
	frameref=":frameref=4"
	mixed_refs=":mixed_refs"
	bframes=":bframes=5"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=normal"
	weight_b=":weight_b"
	weightp=":weightp=2"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=1"
	aq_strength=":aq_strength=0.5"
	me=":me=umh"
	me_range=":me_range=24"
	subq=":subq=7"
	mbtree=":mbtree"
	psy_rd=":psy_rd=1.0,0.25"
	chroma_me=":chroma_me"
	trellis=":trellis=0"
	cabac=":cabac"
	deblock=":deblock=-2,-2"
	ip_factor=":ip_factor=1.1"
	pb_factor=":pb_factor=1.1"
	deadzone_intra=":deadzone_intra=6"
	deadzone_inter=":deadzone_inter=6"
	qcomp=":qcomp=0.8"
	a8x8dct=":8x8dct"
	partitions=":partitions=p8x8,b8x8,i8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc="$h264level"
	;;
	ani)
	# Anime
	frameref=":frameref=8"
	mixed_refs=":mixed_refs"
	bframes=":bframes=5"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=normal"
	weight_b=":noweight_b"
	weightp=":weightp=1"
	direct_pred=":direct_pred=auto"
	me=":me=hex"
	me_range=":me_range=16"
	subq=":subq=6"
	mbtree=":nombtree"
	psy_rd=":psy_rd=0.3,0.0"
	aq_mode=":aq_mode=2"
	aq_strength=":aq_strength=0.5"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	deblock=":deblock"
	a8x8dct=":8x8dct"
	partitions=":partitions=p8x8,b8x8,i8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc="$h264level"
	;;
	anihq)
	# Anime High-Quality
	frameref=":frameref=10"
	mixed_refs=":mixed_refs"
	bframes=":bframes=5"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=normal"
	weight_b=":noweight_b"
	weightp=":weightp=2"
	direct_pred=":direct_pred=auto"
	me=":me=umh"
	me_range=":me_range=24"
	subq=":subq=7"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.3,0.0"
	aq_mode=":aq_mode=1"
	aq_strength=":aq_strength=0.5"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	deblock=":deblock"
	a8x8dct=":8x8dct"
	partitions=":partitions=p8x8,b8x8,i8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc="$h264level"
	;;
	vdhq)
	# VDPAU/DXVA HQ
	frameref=":frameref=4"
	mixed_refs=":mixed_refs"
	bframes=":bframes=3"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=normal"
	weight_b=":weight_b"
	weightp=":weightp=1"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=1"
	me=":me=umh"
	me_range=":me_range=24"
	subq=":subq=7"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.8,0.2"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	deblock=":deblock"
	a8x8dct=":8x8dct"
	partitions=":partitions=p8x8,b8x8,i8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc=":level_idc=41"
	vbv_maxrate=":vbv_maxrate=50000"
	vbv_bufsize=":vbv_bufsize=50000"
	;;
	vdehq)
	# VDPAU/DXVA EHQ
	frameref=":frameref=4"
	mixed_refs=":mixed_refs"
	bframes=":bframes=3"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=normal"
	weight_b=":weight_b"
	weightp=":weightp=2"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=1"
	me=":me=umh"
	me_range=":me_range=24"
	subq=":subq=8"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.8,0.2"
	chroma_me=":chroma_me"
	trellis=":trellis=2"
	cabac=":cabac"
	deblock=":deblock"
	a8x8dct=":8x8dct"
	partitions=":partitions=p8x8,b8x8,i8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc=":level_idc=41"
	vbv_maxrate=":vbv_maxrate=50000"
	vbv_bufsize=":vbv_bufsize=50000"
	;;
	vdihq)
	# VDPAU/DXVA IHQ
	frameref=":frameref=4"
	mixed_refs=":mixed_refs"
	bframes=":bframes=3"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=normal"
	weight_b=":weight_b"
	weightp=":weightp=2"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=1"
	me=":me=tesa"
	me_range=":me_range=32"
	subq=":subq=9"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.8,0.2"
	chroma_me=":chroma_me"
	trellis=":trellis=2"
	cabac=":cabac"
	deblock=":deblock"
	a8x8dct=":8x8dct"
	partitions=":partitions=p8x8,b8x8,i8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc=":level_idc=41"
	vbv_maxrate=":vbv_maxrate=50000"
	vbv_bufsize=":vbv_bufsize=50000"
	;;
	fl)
	# Flash Player
	frameref=":frameref=2"
	mixed_refs=":mixed_refs"
	bframes=":bframes=3"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=none"
	weight_b=":weight_b"
	weightp=":weightp=1"
	direct_pred=":direct_pred=spatial"
	aq_mode=":aq_mode=2"
	me=":me=hex"
	me_range=":me_range=16"
	subq=":subq=6"
	mbtree=":nombtree"
	psy_rd=":psy_rd=0.8,0.2"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	deblock=":deblock"
	a8x8dct=":no8x8dct"
	partitions=":partitions=p8x8,b8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc=":level_idc=41"
	;;
	flhq)
	# Flash Player HQ
	frameref=":frameref=3"
	mixed_refs=":mixed_refs"
	bframes=":bframes=6"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=normal"
	weight_b=":weight_b"
	weightp=":weightp=2"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=1"
	me=":me=umh"
	me_range=":me_range=24"
	subq=":subq=7"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.8,0.2"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	deblock=":deblock"
	a8x8dct=":8x8dct"
	partitions=":partitions=p8x8,b8x8,i8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc=":level_idc=41"
	;;
	qt)
	# QuickTime
	frameref=":frameref=3"
	mixed_refs=":mixed_refs"
	bframes=":bframes=3"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=none"
	weight_b=":weight_b"
	weightp=":weightp=1"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=2"
	me=":me=hex"
	me_range=":me_range=16"
	subq=":subq=6"
	mbtree=":nombtree"
	psy_rd=":psy_rd=0.8,0.2"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	deblock=":deblock"
	a8x8dct=":no8x8dct"
	partitions=":partitions=p8x8,b8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc=":level_idc=41"
	;;
	qthq)
	# QuickTime HQ
	frameref=":frameref=3"
	mixed_refs=":mixed_refs"
	bframes=":bframes=3"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=normal"
	weight_b=":weight_b"
	weightp=":weightp=2"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=1"
	me=":me=umh"
	me_range=":me_range=24"
	subq=":subq=7"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.8,0.2"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	deblock=":deblock"
	a8x8dct=":no8x8dct"
	partitions=":partitions=p8x8,b8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc=":level_idc=41"
	;;
	bd40)
	# Blu-ray (Main@L4.0)
	# For strict compliancy with BD hardware players
	# we have to use access unit delimiters, NAL HRD
	# signaling, one second keyframe interval and
	# four slices
	frameref=":frameref=3"
	mixed_refs=":mixed_refs"
	bframes=":bframes=3"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=strict"
	weight_b=":weight_b"
	weightp=":weightp=1"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=2"
	me=":me=hex"
	me_range=":me_range=16"
	subq=":subq=6"
	mbtree=":nombtree"
	psy_rd=":psy_rd=0.8,0.2"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	deblock=":deblock"
	a8x8dct=":no8x8dct"
	slices=":slices=4"
	partitions=":partitions=p8x8,b8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc=":level_idc=40"
	vbv_maxrate=":vbv_maxrate=20000"
	vbv_bufsize=":vbv_bufsize=20000"
	keyint=":keyint=$(($(echo $keyint | sed 's/:keyint=//')/10))"
	keyint_min=":keyint_min=2"
	nal_hrd=":nal_hrd"
	aud=":aud"
	;;
	bdhq40)
	# Blu-ray HQ (High@L4.0)
	# maxrate set to 25000 (max by Level)
	frameref=":frameref=3"
	mixed_refs=":mixed_refs"
	bframes=":bframes=3"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=strict"
	weight_b=":weight_b"
	weightp=":weightp=2"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=1"
	me=":me=umh"
	me_range=":me_range=24"
	subq=":subq=7"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.8,0.2"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	deblock=":deblock"
	a8x8dct=":8x8dct"
	slices=":slices=4"
	partitions=":partitions=p8x8,b8x8,i8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc=":level_idc=40"
	vbv_maxrate=":vbv_maxrate=25000"
	vbv_bufsize=":vbv_bufsize=20000"
	keyint=":keyint=$(($(echo $keyint | sed 's/:keyint=//')/10))"
	keyint_min=":keyint_min=2"
	nal_hrd=":nal_hrd"
	aud=":aud"
	;;
	bd41)
	# Blu-ray (Main@L4.1)
	frameref=":frameref=3"
	mixed_refs=":mixed_refs"
	bframes=":bframes=3"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=strict"
	weight_b=":weight_b"
	weightp=":weightp=1"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=2"
	me=":me=hex"
	me_range=":me_range=16"
	subq=":subq=6"
	mbtree=":nombtree"
	psy_rd=":psy_rd=0.8,0.2"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	deblock=":deblock"
	a8x8dct=":no8x8dct"
	slices=":slices=4"
	partitions=":partitions=p8x8,b8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc=":level_idc=41"
	vbv_maxrate=":vbv_maxrate=30000"
	vbv_bufsize=":vbv_bufsize=30000"
	keyint=":keyint=$(($(echo $keyint | sed 's/:keyint=//')/10))"
	keyint_min=":keyint_min=2"
	nal_hrd=":nal_hrd"
	aud=":aud"
	;;
	bdhq41)
	# Blu-ray HQ (High@L4.1)
	frameref=":frameref=3"
	mixed_refs=":mixed_refs"
	bframes=":bframes=3"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=strict"
	weight_b=":weight_b"
	weightp=":weightp=2"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=1"
	me=":me=umh"
	me_range=":me_range=24"
	subq=":subq=7"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.8,0.2"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	deblock=":deblock"
	a8x8dct=":8x8dct"
	slices=":slices=4"
	partitions=":partitions=p8x8,b8x8,i8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc=":level_idc=41"
	vbv_maxrate=":vbv_maxrate=30000"
	vbv_bufsize=":vbv_bufsize=30000"
	keyint=":keyint=$(($(echo $keyint | sed 's/:keyint=//')/10))"
	keyint_min=":keyint_min=2"
	nal_hrd=":nal_hrd"
	aud=":aud"
	;;
	avchd)
	# AVCHD
	frameref=":frameref=3"
	mixed_refs=":mixed_refs"
	bframes=":bframes=3"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=strict"
	weight_b=":weight_b"
	weightp=":weightp=1"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=2"
	me=":me=umh"
	me_range=":me_range=16"
	subq=":subq=6"
	mbtree=":nombtree"
	psy_rd=":psy_rd=0.8,0.2"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	deblock=":deblock"
	a8x8dct=":no8x8dct"
	slices=":slices=4"
	partitions=":partitions=p8x8,b8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc=":level_idc=40"
	vbv_maxrate=":vbv_maxrate=17000"
	vbv_bufsize=":vbv_bufsize=16000"
	keyint=":keyint=$(($(echo $keyint | sed 's/:keyint=//')/10))"
	keyint_min=":keyint_min=2"
	nal_hrd=":nal_hrd"
	aud=":aud"
	;;
	avchdhq)
	# AVCHD HQ
	frameref=":frameref=3"
	mixed_refs=":mixed_refs"
	bframes=":bframes=3"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=strict"
	weight_b=":weight_b"
	weightp=":weightp=2"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=1"
	me=":me=umh"
	me_range=":me_range=24"
	subq=":subq=7"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.8,0.2"
	chroma_me=":chroma_me"
	trellis=":trellis=2"
	cabac=":cabac"
	deblock=":deblock"
	a8x8dct=":8x8dct"
	slices=":slices=4"
	partitions=":partitions=p8x8,b8x8,i8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc=":level_idc=41"
	vbv_maxrate=":vbv_maxrate=17000"
	vbv_bufsize=":vbv_bufsize=16000"
	keyint=":keyint=$(($(echo $keyint | sed 's/:keyint=//')/10))"
	keyint_min=":keyint_min=2"
	nal_hrd=":nal_hrd"
	aud=":aud"
	;;
	sdb|hdb)
	# Stand-alone HW players SD/HD (Baseline)
	frameref=":frameref=3"
	mixed_refs=":mixed_refs"
	bframes=":bframes=0"
	b_adapt=":b_adapt=0"
	b_pyramid=":b_pyramid=strict"
	weight_b=":noweight_b"
	weightp=":weightp=0"
	direct_pred=":direct_pred=none"
	aq_mode=":aq_mode=2"
	me=":me=umh"
	me_range=":me_range=24"
	subq=":subq=6"
	mbtree=":nombtree"
	psy_rd=":psy_rd=0.8,0.0"
	chroma_me=":chroma_me"
	trellis=":trellis=0"
	cabac=":nocabac"
	deblock=":deblock"
	a8x8dct=":no8x8dct"
	partitions=":partitions=p8x8,i4x4"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	aud=":aud"
	case "$3" in
		sdb)
		level_idc=":level_idc=30"
		vbv_maxrate=":vbv_maxrate=10000"
		vbv_bufsize=":vbv_bufsize=5000"
		;;
		hdb)
		level_idc=":level_idc=40"
		vbv_maxrate=":vbv_maxrate=20000"
		vbv_bufsize=":vbv_bufsize=14475"
		;;
	esac
	;;
	sdm|sdh|hdm|hdh)
	# Stand-alone HW players SD/HD (Main/High)
	frameref=":frameref=3"
	mixed_refs=":mixed_refs"
	bframes=":bframes=3"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=strict"
	weight_b=":weight_b"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=1"
	me=":me=umh"
	me_range=":me_range=24"
	subq=":subq=7"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.8,0.2"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	deblock=":deblock"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	aud=":aud"
	case "$3" in
		sdm)
		weightp=":weightp=1"
		a8x8dct=":no8x8dct"
		partitions=":partitions=p8x8,b8x8,i4x4"
		level_idc=":level_idc=30"
		vbv_maxrate=":vbv_maxrate=10000"
		vbv_bufsize=":vbv_bufsize=5000"
		;;
		sdh)
		weightp=":weightp=2"
		a8x8dct=":8x8dct"
		partitions=":partitions=p8x8,b8x8,i8x8,i4x4"
		level_idc=":level_idc=30"
		vbv_maxrate=":vbv_maxrate=12500"
		vbv_bufsize=":vbv_bufsize=5000"
		;;
		hdm)
		weightp=":weightp=1"
		a8x8dct=":no8x8dct"
		partitions=":partitions=p8x8,b8x8,i4x4"
		level_idc=":level_idc=40"
		vbv_maxrate=":vbv_maxrate=20000"
		vbv_bufsize=":vbv_bufsize=14475"
		;;
		hdh)
		weightp=":weightp=2"
		a8x8dct=":8x8dct"
		partitions=":partitions=p8x8,b8x8,i8x8,i4x4"
		level_idc=":level_idc=40"
		vbv_maxrate=":vbv_maxrate=25000"
		vbv_bufsize=":vbv_bufsize=14475"
		;;
	esac
	;;
	ag1)
	# Android G1
	frameref=":frameref=1"
	mixed_refs=":nomixed_refs"
	bframes=":bframes=0"
	b_adapt=":b_adapt=0"
	b_pyramid=":b_pyramid=none"
	weight_b=":noweight_b"
	weightp=":weightp=0"
	direct_pred=":direct_pred=none"
	aq_mode=":aq_mode=2"
	subq=":subq=6"
	mbtree=":nombtree"
	psy_rd=":psy_rd=0.8,0.0"
	me=":me=hex"
	partitions=":partitions=p8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=0"
	cabac=":nocabac"
	a8x8dct=":no8x8dct"
	fast_pskip=":fast_pskip"
	dct_decimate=":dct_decimate"
	level_idc=":level_idc=30"
	vbv_maxrate=":vbv_maxrate=2000"
	vbv_bufsize=":vbv_bufsize=2000"
	;;
	ag1hq)
	# Android G1 HQ
	frameref=":frameref=1"
	mixed_refs=":nomixed_refs"
	bframes=":bframes=0"
	b_adapt=":b_adapt=0"
	b_pyramid=":b_pyramid=none"
	weight_b=":noweight_b"
	weightp=":weightp=0"
	direct_pred=":direct_pred=none"
	aq_mode=":aq_mode=1"
	subq=":subq=8"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.8,0.0"
	me=":me=umh"
	partitions=":partitions=p8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=0"
	cabac=":nocabac"
	a8x8dct=":no8x8dct"
	fast_pskip=":fast_pskip"
	dct_decimate=":dct_decimate"
	level_idc=":level_idc=30"
	vbv_maxrate=":vbv_maxrate=2000"
	vbv_bufsize=":vbv_bufsize=2000"
	;;
	ipc)
	# iPod Classic
	frameref=":frameref=1"
	mixed_refs=":nomixed_refs"
	bframes=":bframes=0"
	b_adapt=":b_adapt=0"
	b_pyramid=":b_pyramid=none"
	weight_b=":noweight_b"
	weightp=":weightp=0"
	direct_pred=":direct_pred=none"
	aq_mode=":aq_mode=2"
	subq=":subq=6"
	mbtree=":nombtree"
	psy_rd=":psy_rd=0.8,0.0"
	me=":me=hex"
	partitions=":partitions=p8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=0"
	cabac=":nocabac"
	a8x8dct=":no8x8dct"
	fast_pskip=":fast_pskip"
	dct_decimate=":dct_decimate"
	level_idc=":level_idc=13"
	vbv_maxrate=":vbv_maxrate=768"
	vbv_bufsize=":vbv_bufsize=500"
	;;
	ipchq)
	# iPod Classic HQ
	display_quality_preset_func $3
	frameref=":frameref=1"
	mixed_refs=":nomixed_refs"
	bframes=":bframes=0"
	b_adapt=":b_adapt=0"
	b_pyramid=":b_pyramid=none"
	weight_b=":noweight_b"
	weightp=":weightp=0"
	direct_pred=":direct_pred=none"
	aq_mode=":aq_mode=1"
	subq=":subq=8"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.8,0.0"
	me=":me=umh"
	me_range=":me_range=16"
	partitions=":partitions=p8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=0"
	cabac=":nocabac"
	a8x8dct=":no8x8dct"
	fast_pskip=":nofast_pskip"
	dct_decimate=":dct_decimate"
	level_idc=":level_idc=13"
	vbv_maxrate=":vbv_maxrate=768"
	vbv_bufsize=":vbv_bufsize=500"
	;;
	ip|iph)
	# iPod/iPhone
	frameref=":frameref=1"
	mixed_refs=":nomixed_refs"
	bframes=":bframes=0"
	b_adapt=":b_adapt=0"
	b_pyramid=":b_pyramid=none"
	weight_b=":noweight_b"
	weightp=":weightp=0"
	direct_pred=":direct_pred=none"
	aq_mode=":aq_mode=2"
	subq=":subq=6"
	mbtree=":nombtree"
	psy_rd=":psy_rd=0.8,0.0"
	me=":me=hex"
	partitions=":partitions=p8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=0"
	cabac=":nocabac"
	a8x8dct=":no8x8dct"
	fast_pskip=":fast_pskip"
	dct_decimate=":dct_decimate"
	level_idc=":level_idc=30"
	case "$3" in
		ip)
		vbv_maxrate=":vbv_maxrate=2500"
		vbv_bufsize=":vbv_bufsize=2000"
		;;
		iph)
		vbv_maxrate=":vbv_maxrate=1500"
		vbv_bufsize=":vbv_bufsize=1000"
		;;
	esac
	;;
	iphq|iphhq)
	# iPod/iPhone HQ
	frameref=":frameref=1"
	mixed_refs=":nomixed_refs"
	bframes=":bframes=0"
	b_adapt=":b_adapt=0"
	b_pyramid=":b_pyramid=none"
	weight_b=":noweight_b"
	weightp=":weightp=0"
	direct_pred=":direct_pred=none"
	aq_mode=":aq_mode=1"
	subq=":subq=8"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.8,0.0"
	me=":me=umh"
	me_range=":me_range=16"
	partitions=":partitions=p8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=0"
	cabac=":nocabac"
	a8x8dct=":no8x8dct"
	fast_pskip=":nofast_pskip"
	dct_decimate=":dct_decimate"
	level_idc=":level_idc=30"
	case "$3" in
		iphq)
		vbv_maxrate=":vbv_maxrate=2500"
		vbv_bufsize=":vbv_bufsize=2000"
		;;
		iphhq)
		vbv_maxrate=":vbv_maxrate=1500"
		vbv_bufsize=":vbv_bufsize=1000"
		;;
	esac
	;;
	atv)
	# AppleTV
	frameref=":frameref=1"
	mixed_refs=":nomixed_refs"
	bframes=":bframes=2"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=none"
	weight_b=":weight_b"
	weightp=":weightp=0"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=2"
	subq=":subq=6"
	mbtree=":nombtree"
	psy_rd=":psy_rd=0.8,0.0"
	me=":me=hex"
	me_range=
	partitions=":partitions=p8x8,b8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=0"
	cabac=":nocabac"
	a8x8dct=":no8x8dct"
	fast_pskip=":nofast_pskip"
	dct_decimate=":dct_decimate"
	level_idc=":level_idc=30"
	vbv_maxrate=":vbv_maxrate=12000"
	vbv_bufsize=":vbv_bufsize=5000"
	;;
	atvhq)
	# AppleTV HQ
	frameref=":frameref=3"
	mixed_refs=":mixed_refs"
	bframes=":bframes=3"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=normal"
	weight_b=":weight_b"
	weightp=":weightp=0"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=1"
	subq=":subq=7"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.8,0.0"
	me=":me=umh"
	me_range=":me_range=24"
	partitions=":partitions=p8x8,b8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=0"
	cabac=":nocabac"
	a8x8dct=":8x8dct"
	fast_pskip=":nofast_pskip"
	dct_decimate=":dct_decimate"
	level_idc=":level_idc=30"
	vbv_maxrate=":vbv_maxrate=12000"
	vbv_bufsize=":vbv_bufsize=5000"
	;;
	ar)
	# Archos 605
	frameref=":frameref=1"
	mixed_refs=":nomixed_refs"
	bframes=":bframes=0"
	b_adapt=":b_adapt=0"
	b_pyramid=":b_pyramid=none"
	weight_b=":noweight_b"
	weightp=":weightp=0"
	direct_pred=":direct_pred=none"
	aq_mode=":aq_mode=2"
	subq=":subq=6"
	mbtree=":nombtree"
	psy_rd=":psy_rd=0.8,0.0"
	me=":me=hex"
	me_range=
	partitions=":partitions=p8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=0"
	cabac=":nocabac"
	a8x8dct=":no8x8dct"
	fast_pskip=":nofast_pskip"
	dct_decimate=":dct_decimate"
	vbv_maxrate=":vbv_maxrate=5000"
	vbv_bufsize=":vbv_bufsize=3000"
	level_idc=":level_idc=30"
	;;
	arhq)
	# Archos 605 HQ
	frameref=":frameref=1"
	mixed_refs=":nomixed_refs"
	bframes=":bframes=0"
	b_adapt=":b_adapt=0"
	b_pyramid=":b_pyramid=normal"
	weight_b=":noweight_b"
	weightp=":weightp=0"
	direct_pred=":direct_pred=none"
	aq_mode=":aq_mode=1"
	subq=":subq=8"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.8,0.0"
	me=":me=umh"
	me_range=":me_range=24"
	partitions=":partitions=p8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=0"
	cabac=":nocabac"
	a8x8dct=":no8x8dct"
	fast_pskip=":nofast_pskip"
	dct_decimate=":dct_decimate"
	vbv_maxrate=":vbv_maxrate=5000"
	vbv_bufsize=":vbv_bufsize=3000"
	level_idc=":level_idc=30"
	;;
	ar5)
	# Archos 5
	frameref=":frameref=3"
	mixed_refs=":mixed_refs"
	bframes=":bframes=2"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=strict"
	weight_b=":weight_b"
	weightp=":weightp=1"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=2"
	subq=":subq=6"
	mbtree=":nombtree"
	psy_rd=":psy_rd=0.8,0.2"
	me=":me=hex"
	partitions=":partitions=p8x8,b8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	a8x8dct=":no8x8dct"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	vbv_maxrate=":vbv_maxrate=2000"
	vbv_bufsize=":vbv_bufsize=1700"
	level_idc=":level_idc=40"
	;;
	ar5hq)
	# Archos 5 HQ
	frameref=":frameref=4"
	mixed_refs=":mixed_refs"
	bframes=":bframes=4"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=strict"
	weight_b=":weight_b"
	weightp=":weightp=2"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=1"
	subq=":subq=7"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.8,0.2"
	me=":me=umh"
	me_range=":me_range=24"
	partitions=":partitions=p8x8,b8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	a8x8dct=":no8x8dct"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	vbv_maxrate=":vbv_maxrate=2000"
	vbv_bufsize=":vbv_bufsize=1700"
	level_idc=":level_idc=40"
	;;
	bb)
	# Blackberry 9000
	frameref=":frameref=1"
	mixed_refs=":nomixed_refs"
	bframes=":bframes=0"
	b_adapt=":b_adapt=0"
	b_pyramid=":b_pyramid=none"
	weight_b=":noweight_b"
	weightp=":weightp=0"
	direct_pred=":direct_pred=none"
	aq_mode=":aq_mode=2"
	subq=":subq=6"
	mbtree=":nombtree"
	psy_rd=":psy_rd=0.8,0.0"
	me=":me=hex"
	me_range=
	partitions=":partitions=p8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=0"
	cabac=":nocabac"
	a8x8dct=":no8x8dct"
	fast_pskip=":nofast_pskip"
	dct_decimate=":dct_decimate"
	vbv_maxrate=":vbv_maxrate=1500"
	vbv_bufsize=":vbv_bufsize=1000"
	level_idc=":level_idc=30"
	;;
	bbhq)
	# Blackberry 9000 HQ
	frameref=":frameref=2"
	mixed_refs=":nomixed_refs"
	bframes=":bframes=0"
	b_adapt=":b_adapt=0"
	b_pyramid=":b_pyramid=none"
	weight_b=":noweight_b"
	weightp=":weightp=0"
	direct_pred=":direct_pred=none"
	aq_mode=":aq_mode=1"
	subq=":subq=8"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.8,0.0"
	me=":me=umh"
	me_range=":me_range=24"
	partitions=":partitions=p8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=0"
	cabac=":nocabac"
	a8x8dct=":no8x8dct"
	fast_pskip=":nofast_pskip"
	dct_decimate=":dct_decimate"
	vbv_maxrate=":vbv_maxrate=1500"
	vbv_bufsize=":vbv_bufsize=1000"
	level_idc=":level_idc=30"
	;;
	nks60)
	# Nokia S60
	frameref=":frameref=2"
	mixed_refs=":mixed_refs"
	bframes=":bframes=0"
	b_adapt=":b_adapt=0"
	b_pyramid=":b_pyramid=none"
	weight_b=":noweight_b"
	weightp=":weightp=0"
	direct_pred=":direct_pred=none"
	aq_mode=":aq_mode=2"
	subq=":subq=6"
	mbtree=":nombtree"
	psy_rd=":psy_rd=0.8,0.0"
	me=":me=hex"
	me_range=
	partitions=":partitions=p8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=0"
	cabac=":nocabac"
	a8x8dct=":no8x8dct"
	fast_pskip=":nofast_pskip"
	dct_decimate=":dct_decimate"
	vbv_maxrate=":vbv_maxrate=384"
	vbv_bufsize=":vbv_bufsize=900"
	level_idc=":level_idc=12"
	;;
	nks60hq)
	# Nokia S60 HQ
	frameref=":frameref=3"
	mixed_refs=":mixed_refs"
	bframes=":bframes=0"
	b_adapt=":b_adapt=0"
	b_pyramid=":b_pyramid=none"
	weight_b=":noweight_b"
	weightp=":weightp=0"
	direct_pred=":direct_pred=none"
	aq_mode=":aq_mode=1"
	subq=":subq=7"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.8,0.0"
	me=":me=umh"
	me_range=":me_range=24"
	partitions=":partitions=p8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=0"
	cabac=":nocabac"
	a8x8dct=":no8x8dct"
	fast_pskip=":nofast_pskip"
	dct_decimate=":dct_decimate"
	vbv_maxrate=":vbv_maxrate=384"
	vbv_bufsize=":vbv_bufsize=900"
	level_idc=":level_idc=12"
	;;
	psp)
	# PSP
	frameref=":frameref=2"
	mixed_refs=":mixed_refs"
	bframes=":bframes=3"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=none"
	weight_b=":weight_b"
	weightp=":weightp=1"
	subq=":subq=6"
	mbtree=":nombtree"
	psy_rd=":psy_rd=0.8,0.2"
	me=":me=hex"
	direct_pred=":direct_pred=spatial"
	aq_mode=":aq_mode=2"
	partitions=":partitions=p8x8,b8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	a8x8dct=":no8x8dct"
	fast_pskip=":fast_pskip"
	dct_decimate=":nodct_decimate"
	vbv_maxrate=":vbv_maxrate=4000"
	vbv_bufsize=":vbv_bufsize=2500"
	level_idc=":level_idc=30"
	;;
	psphq)
	# PSP HQ
	frameref=":frameref=2"
	mixed_refs=":mixed_refs"
	bframes=":bframes=3"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=normal"
	weight_b=":weight_b"
	weightp=":weightp=2"
	me_range=":me_range=24"
	subq=":subq=7"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.8,0.2"
	me=":me=umh"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=1"
	partitions=":partitions=p8x8,b8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	a8x8dct=":no8x8dct"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	vbv_maxrate=":vbv_maxrate=4000"
	vbv_bufsize=":vbv_bufsize=2500"
	level_idc=":level_idc=30"
	;;
	ps3)
	# PS3
	frameref=":frameref=3"
	mixed_refs=":mixed_refs"
	bframes=":bframes=3"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=none"
	weight_b=":weight_b"
	weightp=":weightp=1"
	subq=":subq=6"
	mbtree=":nombtree"
	psy_rd=":psy_rd=0.8,0.2"
	me=":me=hex"
	me_range=
	direct_pred=":direct_pred=spatial"
	aq_mode=":aq_mode=2"
	partitions=":partitions=p8x8,b8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	a8x8dct=":no8x8dct"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc=":level_idc=41"
	vbv_maxrate=":vbv_maxrate=20000"
	vbv_bufsize=":vbv_bufsize=20000"
	aud=":aud"
	;;
	ps3hq)
	# PS3 HQ
	frameref=":frameref=3"
	mixed_refs=":mixed_refs"
	bframes=":bframes=3"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=normal"
	weight_b=":weight_b"
	weightp=":weightp=2"
	subq=":subq=7"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.8,0.2"
	me_range=":me_range=24"
	me=":me=umh"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=1"
	partitions=":partitions=p8x8,b8x8,i8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	a8x8dct=":8x8dct"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc=":level_idc=41"
	vbv_maxrate=":vbv_maxrate=20000"
	vbv_bufsize=":vbv_bufsize=20000"
	aud=":aud"
	;;
	mz)
	# Zune
	frameref=":frameref=1"
	mixed_refs=":nomixed_refs"
	bframes=":bframes=0"
	b_adapt=":b_adapt=0"
	b_pyramid=":b_pyramid=none"
	weight_b=":noweight_b"
	weightp=":weightp=0"
	direct_pred=":direct_pred=none"
	aq_mode=":aq_mode=2"
	subq=":subq=6"
	mbtree=":nombtree"
	psy_rd=":psy_rd=0.8,0.0"
	me=":me=hex"
	partitions=":partitions=p8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=0"
	cabac=":nocabac"
	a8x8dct=":no8x8dct"
	fast_pskip=":fast_pskip"
	dct_decimate=":dct_decimate"
	vbv_maxrate=":vbv_maxrate=2500"
	vbv_bufsize=":vbv_bufsize=2000"
	level_idc=":level_idc=30"
	;;
	mzhq)
	# Zune HQ
	frameref=":frameref=3"
	mixed_refs=":mixed_refs"
	bframes=":bframes=0"
	b_adapt=":b_adapt=0"
	b_pyramid=":b_pyramid=none"
	weight_b=":noweight_b"
	weightp=":weightp=0"
	direct_pred=":direct_pred=none"
	aq_mode=":aq_mode=1"
	subq=":subq=7"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.8,0.0"
	me=":me=umh"
	me_range=":me_range=24"
	partitions=":partitions=p8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=0"
	cabac=":nocabac"
	a8x8dct=":no8x8dct"
	fast_pskip=":nofast_pskip"
	dct_decimate=":dct_decimate"
	vbv_maxrate=":vbv_maxrate=2500"
	vbv_bufsize=":vbv_bufsize=2000"
	level_idc=":level_idc=30"
	;;
	mx)
	# XBOX 360
	frameref=":frameref=2"
	mixed_refs=":mixed_refs"
	bframes=":bframes=3"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=none"
	weight_b=":weight_b"
	weightp=":weightp=1"
	subq=":subq=6"
	mbtree=":nombtree"
	psy_rd=":psy_rd=0.8,0.2"
	me=":me=hex"
	direct_pred=":direct_pred=spatial"
	aq_mode=":aq_mode=2"
	partitions=":partitions=p8x8,b8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	a8x8dct=":no8x8dct"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc=":level_idc=41"
	vbv_maxrate=":vbv_maxrate=20000"
	vbv_bufsize=":vbv_bufsize=20000"
	;;
	mxhq)
	# XBOX 360 HQ
	frameref=":frameref=3"
	mixed_refs=":mixed_refs"
	bframes=":bframes=6"
	b_adapt=":b_adapt=2"
	b_pyramid=":b_pyramid=normal"
	weight_b=":weight_b"
	weightp=":weightp=2"
	subq=":subq=7"
	mbtree=":mbtree"
	psy_rd=":psy_rd=0.8,0.2"
	me_range=":me_range=24"
	me=":me=umh"
	direct_pred=":direct_pred=auto"
	aq_mode=":aq_mode=1"
	partitions=":partitions=p8x8,b8x8,i8x8,i4x4"
	deblock=":deblock"
	chroma_me=":chroma_me"
	trellis=":trellis=1"
	cabac=":cabac"
	a8x8dct=":8x8dct"
	fast_pskip=":nofast_pskip"
	dct_decimate=":nodct_decimate"
	level_idc=":level_idc=41"
	vbv_maxrate=":vbv_maxrate=20000"
	vbv_bufsize=":vbv_bufsize=20000"
	;;
   htm)
   # HTC Magic
   #"bitrate=512:global_header"
   frameref=":frameref=6"
   bframes=":bframes=0"
   b_pyramid=":b_pyramid=none"
   subq=":subq=5"
   mbtree=":nombtree"
   me=":me=umh"
   partitions=":partitions=all"
   chroma_me=":chroma_me"
   trellis=":trellis=1"
   cabac=":nocabac"
   dct_decimate=":dct_decimate"
   vbv_maxrate=":vbv_maxrate=2500"
   vbv_bufsize=":vbv_bufsize=2000"
   level_idc=":level_idc=30"
   ;;
esac

if [ ! "$bitrate" = "" ] ; then
   BITRATE=":bitrate=${bitrate}"
else
   BITRATE=""
fi
if [ ! "$maxrate" = "" ] ; then
   if [ "$vbv_maxrate" = "" ] ; then
      vbv_maxrate="$maxrate"
   else
      if [ "$maxrate" -lt $(echo $vbv_maxrate | awk -F= '{print $2}') ] ; then
          vbv_maxrate=":vbv_maxrate=$maxrate"
      fi
   fi
fi
if [ ! "$bufsize" = "" ] ; then
   if [ "$vbv_bufsize" = "" ] ; then
      vbv_bufsize="$bufsize"
   else
      if [ "$bufsize" -lt $(echo $vbv_bufsize | awk -F= '{print $2}') ] ; then
          vbv_bufsize=":vbv_maxrate=$bufsize"
      fi
   fi
fi

x264encoptions="$me$me_range$dct_decimate$interlaced$a8x8dct$fast_pskip$trellis$scenecut$chroma_qp_offset$nr\
$ip_factor$pb_factor$cplx_blur$qblur$qcomp$slices$slice_max_size$slice_max_mbs$partitions$mixed_refs$qp_min\
$qp_max$qp_step$keyint$keyint_min$psy_rd$nopsy$frameref$bframes$b_adapt$b_bias$b_pyramid$weight_b$weightp\
$direct_pred$subq$mbtree$rc_lookahead$sync_lookahead$constrained_intra$chroma_me$cabac$aud$nal_hrd$aq_strength\
$aq_mode$deblock$stats$ratetol$vbv_maxrate$vbv_bufsize$vbv_init$cqm$deadzone_inter$deadzone_intra$global_header\
$level_idc:threads=auto:ssim:psnr${BITRATE}"

if [ ! "$x264encoptions" = "" ] ; then
   if [ "$(echo $x264encoptions | cut -c 1)" = ":" ] ; then
      # removing leading colon
      x264encoptions="$(echo $x264encoptions | cut -c 2-)"
   fi
   echo  "x264encoptions $x264encoptions" >> "$outputfile"
fi

# Video scaling section
if [ ! "$sourcewidth" = "" ] ; then
   pre_calculate_geometry

   device_resolution_mapping

   build_filter_chain
fi

exit 0;


