#!/bin/bash

#   FILE: vcdize -- 
# AUTHOR: W. Michael Petullo <mike@flyn.org>
#   DATE: 22 November 2001
#
# Copyright (C) 2001 W. Michael Petullo <mike@flyn.org>
# All rights reserved.
#
# This program 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.
#
# This program 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

TYPE=vcd2
LETTERBOX=none
MULTITHREAD=false
USAGE="-i <input> [OPTION]...

  -l 2.35:1|none      letterbox type		[ $LETTERBOX ]
  -m                  use multi-threading	[ $MULTITHREAD ]
  -t vcd11|vcd2|svcd  Video CD type		[ $TYPE ]
  -h, -?              print this message"

while :;
	do case "$1" in
		-h | "-?" )	
			echo -e usage: ${0##*/} "$USAGE" >&2
			exit 1 ;;
		-i )
			INS=(${INS[*]} $2)
			shift ;;
		-l)
			LETTERBOX=$2
			shift ;;
		-m )
			MULTITHREAD=true ;;
		-t )
			TYPE=$2
			shift ;;
		-?* )
			echo "${0##*/}: unrecognised option: $1" >&2
			exit 1 ;;
		* )
			break ;;
	esac
	shift
done

if [ -z $INS ]; then
        echo "${0##*/}: $INS does not exist" >&2
	exit 1
fi

if [ x$TYPE = xvcd2 -o x$TYPE = xvcd11 ]; then
	FORMAT=VCD
	FORMATNUM=1
	MPLEXOPS="-S 1000"
	MPEG2ENCOPTS="$MPEG2ENCOPTS -S 1000"
	MP2ENCOPTS="-V"
elif [ x$TYPE = xsvcd ]; then
	FORMAT=SVCD
	FORMATNUM=4
	MPLEXOPTS="-b 200 -r 2750 -S 1000" # -r 2750 : is the calculated Audio + Video Bitrate + 1-2% multiplex information
	MPEG2ENCOPTS="$MPEGENC2OPTS -q 7 -I 0 -V 200 -S 1000"
	MP2ENCOPTS="-b 128 -s"
else
	echo "${0##*/}: $TYPE not supported" >&2
	exit 1
fi

if [ x$LETTERBOX = xnone ]; then
	DENOISEOPTS=
elif [ x$LETTERBOX = x2.35:1 ]; then
	DENOISEOPTS="-b 0,68,640,344 -F"
else
	echo "${0##*/}: $LETTERBOX not supported" >&2
	exit 1
fi

if [ x$MULTITHREAD = xtrue ]; then
	MPEG2ENCOPTS="$MPEG2ENCOPTS -M 3"
fi

CUE=${INS[0]%.*}.cue
BIN=${INS[0]%.*}.bin

for input in ${INS[*]}; do
	MPEG_VIDEO=(${MPEG_VIDEO[*]} `mktemp -u /tmp/video.$input.XXXXXX`)
	mkfifo $MPEG_VIDEO
	MPEG_AUDIO=(${MPEG_AUDIO[*]} `mktemp -u /tmp/sound.$input.XXXXXX`)
	mkfifo $MPEG_AUDIO
	MPEG_MUX=(${MPEG_MUX[*]} `mktemp /tmp/mux.input.XXXXXX`)
done
trap 'rm -f ${MPEG_AUDIO[*]} ${MPEG_VIDEO[*]} ${MPEG_MUX[*]}' SIGHUP SIGINT SIGTERM EXIT

I=0
for input in ${INS[*]}; do
	lav2yuv +n ${INS[$I]} | yuvdenoise $DENOISEOPTS | yuvscaler -n n -O $FORMAT | mpeg2enc -n n -f $FORMATNUM $MPEG2ENCOPTS -o ${MPEG_VIDEO[$I]} &
	lav2wav +n ${INS[$I]} | mp2enc $MP2ENCOPTS -o ${MPEG_AUDIO[$I]} &
	mplex $MPLEXOPTS -f $FORMATNUM ${MPEG_AUDIO[$I]} ${MPEG_VIDEO[$I]} -o ${MPEG_MUX[$I]}
	I=$(($INDEX + 1))
done

vcdimager -t $TYPE --cue-file=$CUE --bin-file=$BIN ${MPEG_MUX[*]}

if [ `du -sm $BIN | awk '{ printf $1 }'` -gt 740 ]; then
	echo
	echo "WARNING: image is larger than 740MB (~74 minutes for VCD)."
	echo
fi

echo
echo "Disc image ready, burn with:"
echo "cdrdao write --driver generic-mmc --device 0,4,0 --speed 12 $CUE"
echo
