#!/bin/ksh

SKIP_FRAMES=1
ZOOM_FACTOR=1.0
UPSIDE_DOWN=""
NR_OF_FRAMES=0

while ( [ ! -f "$1" ] )
do
    case $1 in
        -[1-9]*)
            SKIP_FRAMES=${1#-}
            shift
            ;;
        -nr)
            shift
            NR_OF_FRAMES=$1
            shift
            ;;
        -z)
            shift
            ZOOM_FACTOR=$1
            shift
            ;;
        -u)
            UPSIDE_DOWN="-u"
            shift
            ;;
        *)
            echo "illegal argument: >$1< (file does not exist)"
            echo "Usage: mv2mpeg [options] <SGI-movie-file>"
            echo ' options: -u                           force frames to be turned upside down'
            echo '          -z <zoom factor>             0.0 <= zoom factor <= 1.0(default)'
            echo '          -nr <total number of frames>'
            echo '          -<nr of frames to skip>'
            exit 1
            ;;
    esac
done

if [ $# -lt 1 ] ; then
    echo "Usage: mv2mpeg [options] <SGI-movie-file>"
    echo ' options: -u                           force frames to be turned upside down'
    echo '          -z <zoom factor>             0.0 <= zoom factor <= 1.0(default)'
    echo '          -nr <total number of frames>'
    echo '          -<nr of frames to skip>'
    exit 1
fi

movie_filename=$1
image_filename=${1%.*}
image_filename=${image_filename##*/}
mpeg_filename=$image_filename.mpg


if [ "$NR_OF_FRAMES" -eq 0 ] ; then
    NR_OF_FRAMES=`mv2frames -i $movie_filename`
fi

if [ "$NR_OF_FRAMES" -lt 0 ] ; then
    echo "ERROR: could not determine number of frames in movie-file."
    echo "       You may set the total number of frames by using the"
    echo "       commandline option '-nr <total number of frames>'."
    exit 1
fi

#------------------------------------------------------------------------------

cat > $PWD/movie.param << EOF
# parameter file template with lots of comments to assist you
#
# you can use this as a template, copying it to a separate file then modifying
# the copy
#
#
# any line beginning with '#' is a comment
#
# no line should be longer than 255 characters
#
#
# general format of each line is:
#   <option> <spaces and/or tabs> <value>
#
# lines can generally be in any order
#
# only exception is the option 'INPUT' which must be followed by input
# files in the order in which they must appear, followed by 'END_INPUT'
#
# <option> MUST be in UPPER CASE
#
# from a ~33 MB SGI-Movie file I got the following MPEG-movie-sizes
# PATTERN II   -> 3190497 Byte
# PATTERN IPI  -> 2381952 Byte
# PATTERN IPPI -> 1923796 Byte (faulty)
#
#PATTERN     IBBPBBI

PATTERN     IPI

OUTPUT      $PWD/$mpeg_filename

# mpeg_encode really only accepts 3 different file formats, but using a
# conversion statement it can effectively handle ANY file format
#
# you must specify whether you will convert to PNM or PPM or YUV format
#   (must be upper case)
#
BASE_FILE_FORMAT    PPM
#
# if YUV format (or using parallel version), must provide width and height
# YUV_SIZE  widthxheight
# this option is ignored if BASE_FILE_FORMAT is PPM or PNM and you're running
# on just one machine
#
YUV_SIZE    352x240

# the conversion statement
#
# Each occurrence of '*' will be replaced by the input file
#
# e.g., if you have a bunch of GIF files, then this might be:
#   INPUT_CONVERT   giftoppm *
#
# e.g., if you have a bunch of files like a.Y a.U a.V, etc., then:
#   INPUT_CONVERT   cat *.Y *.U *.V
#
# e.g., if you are grabbing from laser disc you might have something like
#   INPUT_CONVERT   goto frame *; grabppm
# 'INPUT_CONVERT *' means the files are already in the base file format
#
INPUT_CONVERT   mv2frames $UPSIDE_DOWN -z $ZOOM_FACTOR -n * -f 1 -o - $movie_filename

# number of frames in a GOP.
#
# since each GOP must have at least one I-frame, the encoder will find the
# the first I-frame after GOP_SIZE frames to start the next GOP
#
# later, will add more flexible GOP signalling
#
GOP_SIZE    2

# number of slices in a frame
#
# 1 is a good number.  another possibility is the number of macroblock rows
# (which is the height divided by 16)
#
SLICES_PER_FRAME    1

# directory to get all input files from (makes this file easier to read)
INPUT_DIR $PWD
INPUT
# '*' is replaced by the numbers 01, 02, 03, 04
# if I instead do [01-11], it would be 01, 02, ..., 09, 10, 11
# if I instead do [1-11], it would be 1, 2, 3, ..., 9, 10, 11
# if I instead do [1-11+3], it would be 1, 4, 7, 10
# the program assumes none of your input files has a name ending in ']'
# if you do, too bad!!!
#
*   [1-${NR_OF_FRAMES}+${SKIP_FRAMES}]
#
# can have more files here if you want...there is no limit on the number
# of files
END_INPUT

# all of the remaining options have to do with the motion search and qscale

# FULL or HALF -- must be upper case
#PIXEL       HALF
PIXEL       FULL

# means +/- this many pixels
RANGE       5

# this must be one of {EXHAUSTIVE, SUBSAMPLE, LOGARITHMIC}
PSEARCH_ALG LOGARITHMIC

# this must be one of {SIMPLE, CROSS2, EXHAUSTIVE}
#
# note that EXHAUSTIVE is really, really, really slow
#
BSEARCH_ALG CROSS2

#
# these specify the q-scale for I, P, and B frames
# (values must be between 1 and 31)
#
IQSCALE     8
PQSCALE     10
BQSCALE     25

# this must be ORIGINAL or DECODED
#REFERENCE_FRAME ORIGINAL
REFERENCE_FRAME DECODED

EOF

#------------------------------------------------------------------------------

mpeg_encode -quiet 30 -no_frame_summary $PWD/movie.param

/bin/rm $PWD/movie.param
