#!/bin/ksh

if [ $# -lt 1 ] ; then
cat << EOF
Usage: mv2mpeg <SGI-movie-file>
EOF
exit 1
fi

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

#
# If you chooose to uncomment the following lines, you will have to make
# some adjustments in the 'movie.param'-bit below.
# Basically commenting some lines out, while removing the '#' before others.
#
# The advantage of using 'mv2frames' as INPUT_CONVERT (as done now) is, that
# you don't have to write all the frames to disk bevor starting mpeg_encode.
#
# However, if this strategy causes trouble on your system, uncomment the
# following lines and make the necessary adjustments.
# Then all frames will be extracted from the movie file bevore starting
# mpeg_encode.
# If you do so, make sure you have enough disk-space left to hold all the
# frames. (I had a movie file with >1600 frames that amounted to >800MB of 
#          single frames saved to disk!)
#
#
#
# Extract all the frames to disk. (format PPM)
#
#mv2frames -f 1 -p -o $image_filename $movie_filename
#
#if [ $? -ne 0 ] ; then
#	exit 1
#fi
#
#NR=0
#
#for j in $image_filename.*.rgb
#do
#	let NR=NR+1
#	echo " counting pictures ... $NR               \r\c"
#done
#
#NR_OF_FRAMES=$NR

NR_OF_FRAMES=`mv2frames -i $movie_filename`


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

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 roughly 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   *
INPUT_CONVERT   mv2frames -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!!!
#
#$image_filename.*.ppm   [000001-$NR_OF_FRAMES]
*   [1-$NR_OF_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

# 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

EOF

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

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

#/bin/rm $PWD/movie.param $image_filename.*.ppm
/bin/rm $PWD/movie.param
