#!/bin/csh
# Written by Bill Brown, to ease making of mpeg movies from rgb files.
# May want to experiment with some of the things in parameter file -
# see documentation for mpeg_encode
# This version uses ppm files as the conversion format


if ($#argv < 3) then
    echo
    echo USAGE: make.mpeg \[-s scalefactor\] moviename rgbfile1 rgbfile2...
    echo
    exit 1
endif

if ($#argv > 1000) then
    echo
    echo Too many files to use this script!
    echo
    exit 1
endif

if ($#argv < 8) then
    echo
    echo Too few frames to use this script!
    echo
    exit 1
endif

set THISDIR=`pwd`

set firstfile
set num
@ num=1

set FSTART="0"

if( "$1" == "-s" ) then
    set SCALE=$2
    @ firstfile=4
    set MOVIENAME=$3
else
    set SCALE="none"
    @ firstfile=2
    set MOVIENAME=$1
endif

# prevent no match error
echo NULL > m_mpeg.000
/bin/rm -f m_mpeg.*
    

foreach i ($*)

    if ( "$i" == "$argv[$firstfile]" ) then
	set FSTART="1"
    endif

    if ( "$FSTART" != "0" ) then
	echo $i

	set SUFFIX=`echo "$num"`
        if ( $num < 10 ) then
            set SUFFIX=`echo 00"$num"`
        else if ( $num < 100 ) then
            set SUFFIX=`echo 0"$num"`
        endif

        set TNAME=m_mpeg.$SUFFIX

	if ( $SCALE != "none") then
	    izoom $i $THISDIR/$TNAME $SCALE $SCALE
	    if ( "$FSTART" == "1" ) then
		set FSTART="2"
	    endif
	else 
	    ln $i $THISDIR/$TNAME
	endif

	@ num= $num + 1
    endif

end

set PFILE=$MOVIENAME.param

@ num= $num - 1

#make parameter file
set COM1=`echo "#" parameter file for "$MOVIENAME".mpg moviefile`
set COM2=`echo "#" used by mpeg_encode when creating .mpg file from images`
echo $COM1 > $PFILE
echo $COM2 >> $PFILE
echo >> $PFILE
echo

#echo "PATTERN         IBBPBB" >> $PFILE
#echo "PATTERN         IBBPBBI" >> $PFILE

# use this for better quality, less compression
echo "PATTERN         IBPB" >> $PFILE

echo FORCE_ENCODE_LAST_FRAME >> $PFILE
echo >> $PFILE
echo OUTPUT          "$MOVIENAME".mpg >> $PFILE
echo >> $PFILE
echo "INPUT_DIR       ." >> $PFILE
echo "INPUT" >> $PFILE
echo "m_mpeg.*        [001-"$SUFFIX"]" >> $PFILE
echo "END_INPUT" >> $PFILE
echo >> $PFILE
echo "BASE_FILE_FORMAT        PPM" >> $PFILE

# Use this line if you have SDSC imagetools
#echo "INPUT_CONVERT   imconv -rgb * -ppm - " >> $PFILE

# Otherwise use this line
echo "INPUT_CONVERT   toppm * tmp.ppm; cat tmp.ppm " >> $PFILE

echo "GOP_SIZE        30" >> $PFILE
echo "SLICES_PER_FRAME  1" >> $PFILE
echo >> $PFILE
echo "PIXEL           HALF" >> $PFILE
echo "RANGE           10" >> $PFILE
echo >> $PFILE
echo "PSEARCH_ALG     TWOLEVEL" >> $PFILE
echo "BSEARCH_ALG     CROSS2" >> $PFILE
echo >> $PFILE
echo "IQSCALE         8" >> $PFILE
echo "PQSCALE         10" >> $PFILE
echo "BQSCALE         25" >> $PFILE
echo >> $PFILE
echo "REFERENCE_FRAME DECODED" >> $PFILE
#echo "REFERENCE_FRAME ORIGINAL" >> $PFILE

mpeg_encode -stat $MOVIENAME.stat $MOVIENAME.param

# clean up

/bin/rm -f $THISDIR/m_mpeg.*
/bin/rm -f $MOVIENAME.stat $MOVIENAME.param
/bin/rm -f tmp.ppm

