source: SHVCSoftware/trunk/source/App/utils/BitrateTargeting/encodeCommand.sh @ 595

Last change on this file since 595 was 595, checked in by seregin, 11 years ago

merge with SHM-5.0-dev branch

File size: 11.8 KB
RevLine 
[125]1#! /bin/sh
2
3# The copyright in this software is being made available under the BSD
4# License, included below. This software may be subject to other third party
5# and contributor rights, including patent rights, and no such rights are
6# granted under this license. 
7#
[595]8# Copyright (c) 2010-2014, ITU/ISO/IEC
[125]9# All rights reserved.
10#
11# Redistribution and use in source and binary forms, with or without
12# modification, are permitted provided that the following conditions are met:
13#
14#  * Redistributions of source code must retain the above copyright notice,
15#    this list of conditions and the following disclaimer.
16#  * Redistributions in binary form must reproduce the above copyright notice,
17#    this list of conditions and the following disclaimer in the documentation
18#    and/or other materials provided with the distribution.
19#  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
20#    be used to endorse or promote products derived from this software without
21#    specific prior written permission.
22#
23# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
27# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33# THE POSSIBILITY OF SUCH DAMAGE.
34
35# Outputs a command to run the encoder for a given set of arguments.  The encoder typically requires a lot of arguments to run, so this script can be more convenient than running the encoder direcly because it automatically generates many of the arguments.
36
37# "Include" encode.shl which contains common shell script code related to encoding
38SUB_TOOLS_DIRECTORY=$(echo "$0" | sed -e 's/[^\/]*$//')
39. ${SUB_TOOLS_DIRECTORY}encode.shl
40
41USE_LOG_FILE_OPTION="-l"
42INPUT_DIRECTORY_OPTION="-id"
43NUM_FRAMES_OPTION="-f"
44
45function outputUsageAndExit {
46  local EXECUTABLE_USAGE_STRING=executable
47  local INPUT_DIRECTORY_USAGE_STRING=inputDirectory
48  local CONFIGURATION_PATH_USAGE_STRING=configurationPath
49  local CONFIGURATION_DIRECTORY_USAGE_STRING=configurationDirectory
50  local NUM_FRAMES_USAGE_STRING=numFrames
51 
52  echo "Usage: $0 $CONFIGURATION_IDENTIFIER_OPTION $CONFIGURATION_IDENTIFIER_USAGE_STRING ($CONFIGURATION_PATH_OPTION $CONFIGURATION_PATH_USAGE_STRING -or- $CONFIGURATION_DIRECTORY_OPTION $CONFIGURATION_DIRECTORY_USAGE_STRING) $Q_OPTION $Q_USAGE_STRING $EXECUTABLE_OPTION $EXECUTABLE_USAGE_STRING [$NUM_FRAMES_OPTION $NUM_FRAMES_USAGE_STRING] [$EXTRA_ARGUMENTS_OPTION $EXTRA_ARGUMENTS_USAGE_STRING] $OUTPUT_DIRECTORY_OPTION $OUTPUT_DIRECTORY_USAGE_STRING [$USE_LOG_FILE_OPTION] [$INPUT_DIRECTORY_OPTION $INPUT_DIRECTORY_USAGE_STRING] $INPUT_NAME_USAGE_STRING" >&2
53  outputConfigurationIdentifierUsage
54  echo "${USAGE_INDENT}$CONFIGURATION_PATH_USAGE_STRING is the path of the configuration file to use.  Either this or $CONFIGURATION_DIRECTORY_USAGE_STRING must be specified (but not both)." >&2
55  echo "${USAGE_INDENT}$CONFIGURATION_DIRECTORY_USAGE_STRING is the path of the directory that contains the configuration files.  The particular file will be chosen based on $CONFIGURATION_IDENTIFIER_USAGE_STRING.  Either this or $CONFIGURATION_PATH_USAGE_STRING must be specified (but not both)." >&2
56  outputQUsage
57  echo "${USAGE_INDENT}$EXECUTABLE_USAGE_STRING is the path of the encoder executable." >&2
58  echo "${USAGE_INDENT}$NUM_FRAMES_USAGE_STRING is the number of frames to encode.  If omitted, the entire sequence will be encoded." >&2
59  echo "${USAGE_INDENT}$EXTRA_ARGUMENTS_USAGE_STRING is any extra arguments that should be passed on to the encoder." >&2
60  outputOutputDirectoryUsage
61  echo "${USAGE_INDENT}If $USE_LOG_FILE_OPTION is specified, the encoder will output to a log file.  Otherwise it will output to the standard output." >&2
62  echo "${USAGE_INDENT}$INPUT_DIRECTORY_USAGE_STRING is the directory that contains the sequences.  The default value is the SEQUENCE_DIR environment variable." >&2
63  outputInputNameUsage
64 
65  exit 1
66}
67
68# Used to lookup the width, height, number of frames, and frame rate for a given sequence
69table=`printf "${table}\nNebutaFestival_2560x1600_60_10bit_crop          2560   1600   300        60"`
70table=`printf "${table}\nSteamLocomotiveTrain_2560x1600_60_10bit_crop    2560   1600   300        60"`
71table=`printf "${table}\nTraffic_2560x1600_30_crop                       2560   1600   150        30"`
72table=`printf "${table}\nPeopleOnStreet_2560x1600_30_crop                2560   1600   150        30"`
73table=`printf "${table}\nBQTerrace_1920x1080_60                          1920   1080   600        60"`
74table=`printf "${table}\nBasketballDrive_1920x1080_50                    1920   1080   500        50"`
75table=`printf "${table}\nCactus_1920x1080_50                             1920   1080   500        50"`
76table=`printf "${table}\nKimono1_1920x1080_24                            1920   1080   240        24"`
77table=`printf "${table}\nParkScene_1920x1080_24                          1920   1080   240        24"`
78table=`printf "${table}\nvidyo1_720p_60                                  1280    720   600        60"`
79table=`printf "${table}\nvidyo3_720p_60                                  1280    720   600        60"`
80table=`printf "${table}\nvidyo4_720p_60                                  1280    720   600        60"`
81table=`printf "${table}\nRaceHorses_832x480_30                            832    480   300        30"`
82table=`printf "${table}\nBQMall_832x480_60                                832    480   600        60"`
83table=`printf "${table}\nPartyScene_832x480_50                            832    480   500        50"`
84table=`printf "${table}\nBasketballDrill_832x480_50                       832    480   500        50"`
85table=`printf "${table}\nRaceHorses_416x240_30                            416    240   300        30"`
86table=`printf "${table}\nBQSquare_416x240_60                              416    240   600        60"`
87table=`printf "${table}\nBlowingBubbles_416x240_50                        416    240   500        50"`
88table=`printf "${table}\nBasketballPass_416x240_50                        416    240   500        50"`
89table=`printf "${table}\nBasketballDrillText_832x480_50                   832    480   500        50"`
90table=`printf "${table}\nChinaspeed_1024x768_30                          1024    768   500        30"`
91table=`printf "${table}\nSlideEditing_1280x720_30                        1280    720   300        30"`
92table=`printf "${table}\nSlideShow_1280x720_20                           1280    720   500        20"`
93
94EXECUTABLE_STRING="executable ($EXECUTABLE_OPTION)"
95INPUT_DIRECTORY_STRING="input directory ($INPUT_DIRECTORY_OPTION)"
96CONFIGURATION_PATH_STRING="configuration path ($CONFIGURATION_PATH_OPTION)"
97CONFIGURATION_DIRECTORY_STRING="configuration directory ($CONFIGURATION_DIRECTORY_OPTION)"
98
99inputDirectory="$SEQUENCE_DIR"  # The default input directory is taken from this environment variable
100
101# For every argument $1
102while [ "" != "$*" ] ; do
103  case $1 in
104    $USE_LOG_FILE_OPTION)
105      useLogFile=$1
106    ;;
107    -*)
108      checkDollarTwo "$1" "$2"
109      case $1 in
110        $EXECUTABLE_OPTION) executable=$2 ;;
111        $INPUT_DIRECTORY_OPTION) inputDirectory=$2 ;;
112        $EXTRA_ARGUMENTS_OPTION) extraArguments=$2 ;;
113        $Q_OPTION) q=$2 ;;
114        $OUTPUT_DIRECTORY_OPTION) outputDirectory=$2 ;;
115        $CONFIGURATION_IDENTIFIER_OPTION) configurationIdentifier=$2 ;;
116        $CONFIGURATION_PATH_OPTION) configurationPath=$2 ;;
117        $CONFIGURATION_DIRECTORY_OPTION) configurationDirectory=$2 ;;
118        $NUM_FRAMES_OPTION) numFrames=$2 ;;
119        *)
120          printf "You entered an invalid option: \"$1\".\n" >&2
121          outputUsageAndExit
122        ;;
123      esac
124      shift
125    ;;
126    *)
127      if [[ "" == $inputName ]] ; then
128        inputName=$1
129      else
130        printf "You entered too many arguments.\n" >&2
131        outputUsageAndExit
132      fi
133    ;;
134  esac
135 
136  shift
137done
138
139verifyProvided "$EXECUTABLE_STRING" "$executable"
140verifyNotDirectory "$EXECUTABLE_STRING" "$executable"
141
142verifyDirectory "$INPUT_DIRECTORY_STRING" "$inputDirectory"
143
144verifyProvided "$Q_STRING" "$q"
145verifyQ $q
146
147verifyProvided "$OUTPUT_DIRECTORY_STRING" "$outputDirectory"
148verifyDirectory "$OUTPUT_DIRECTORY_STRING" "$outputDirectory"
149
150verifyProvided "$CONFIGURATION_IDENTIFIER_STRING" "$configurationIdentifier"
151verifyConfigurationIdentifier "$configurationIdentifier"
152
153# Validate $configurationPath or $configurationDirectory
154if [[ $configurationPath != "" ]] ; then
155  verifyNotDirectory "$CONFIGURATION_PATH_STRING" "$configurationPath"
156else
157  if [[ "" == $configurationDirectory ]] ; then
158    printf "You must enter a $CONFIGURATION_PATH_STRING or $CONFIGURATION_DIRECTORY_STRING.\n" >&2
159    outputUsageAndExit
160  else
161    verifyDirectory "$CONFIGURATION_DIRECTORY_STRING" "$configurationDirectory"
162  fi
163fi
164
165verifyProvided "$INPUT_NAME_STRING" "$inputName"
166verifyNotDirectory "$INPUT_NAME_STRING" "$inputName"
167
168# If configurationPath is not already populated, populate it based on the configuration directory and the configuration identifier
169if [[ "" == $configurationPath ]] ; then
170  configurationPath="${configurationDirectory}encoder_"
171  case $configurationIdentifier in
172    ldLC) configurationPath="${configurationPath}lowdelay_loco" ;;
173    raLC) configurationPath="${configurationPath}randomaccess_loco" ;;
174    inLC) configurationPath="${configurationPath}intra_loco" ;;
175    ldHE) configurationPath="${configurationPath}lowdelay" ;;
176    raHE) configurationPath="${configurationPath}randomaccess" ;;
177    *)    configurationPath="${configurationPath}intra" ;;  # inHE
178  esac
179  configurationPath="${configurationPath}.cfg"
180fi
181
182# Validate the input name and populate masterLine which contains the line from the table for the given sequence
183masterLine=`printf "$table" | grep -i "^$inputName "`
184if [[ "" == $masterLine ]] ; then
185  printf "Invalid input name.\n" >&2
186  outputUsageAndExit
187fi
188
189# If numFrames is not yet initialized, initialize it by looking up the values in the table
190if [[ "" == $numFrames ]] ; then
191  numFrames=`printf -- "$masterLine" | awk '{ print $4 }'`
192fi
193
194# Initialize these variables by looking up the values in the table
195frameRate=`printf -- "$masterLine" | awk '{ print $5 }'`
196width=`printf -- "$masterLine" | awk '{ print $2 }'`
197height=`printf -- "$masterLine" | awk '{ print $3 }'`
198
199# Initialize intraPeriod
200case $configurationIdentifier in
201  ld*)
202    intraPeriod="-1"
203  ;;
204  ra*)
205    if [[ 20 == "$frameRate" ]] ; then
206      intraPeriod="16"
207    else
208      intraPeriod=$(expr "$frameRate" + 4)
209      intraPeriod=$(expr "$intraPeriod" / 8)
210      intraPeriod=$(expr "$intraPeriod" \* 8)
211    fi
212  ;;
213  in*)
214    intraPeriod="1"
215  ;;
216  *)
217    outputConfigurationIdentifierErrorAndExit
218  ;;
219esac
220
221# Initialize tenBit if the given sequence is 10-bit
222printf -- "$inputName" | grep -i '10bit' > /dev/null
223case $? in
224  0)  tenBit="--InputBitDepth=10 "
225    ;;
226  1)  ;;
227  *)  exit $?
228    ;;
229esac
230
231outputPathBegin="${outputDirectory}${inputName}_${configurationIdentifier}_q${q}"
232
233# Output the command
234printf -- "$executable "
235printf -- "-c $configurationPath "
236printf -- "-i $inputDirectory$inputName.yuv "
237printf -- "-f $numFrames "
238printf -- "-fr $frameRate "
239printf -- "-wdt $width "
240printf -- "-hgt $height "
241printf -- "-ip $intraPeriod "
242printf -- "$tenBit"
243if [[ $tenBit != "" ]] ; then
244  printf " "
245fi
246printf -- "$extraArguments"
247if [[ $extraArguments != "" ]] ; then
248  printf -- " "
249fi
250printf -- "-q $q "
251printf -- "-b $outputPathBegin.bin "
252printf -- "-o $outputPathBegin.yuv "
253if [[ "" != $useLogFile ]] ; then
254  printf -- "&> $outputPathBegin.log"
255fi
256printf "\n"
257
258exit 0
Note: See TracBrowser for help on using the repository browser.