#!/bin/tcsh -f
# recording from IEEE 1394 DV packets
#  written by Kazumaro Aoki (ka at flu.hn.org)
# 09/ 5/28: sophiscated option handling for fwcontrol
# 09/ 5/ 9: add -B option for bilingual broadcasting
# 08/ 9/ 7: add -M dv option for 6.3R fwcontrol -R
# 07/ 9/ 1: stop encoding when fwcontrol with -e option
# 07/ 8/21: comment out "kill -STOP" when fecontrol -R because of NCHUNK inc
# 07/ 3/19: add -l option for specifying lavc vqscale
# 06/ 5/ 7: don't start encoding when fwcontrol and many mencoder(s) running
# 06/ 5/ 4: can specify firewire bus#
# 06/ 1/ 9: remove DV file more safely
# 05/11/19: add -ffourcc DIVX for lavc opts
# 05/11/ 8: add xvid option
# 05/10/11: use denoise filter
# 05/ 5/ 7: get eui64 automatically from fwcontrol command
# 05/ 5/ 5: 2-pass is default, add -1 option
# 05/ 4/23: touch .avi to .dv time
# 05/ 4/19: kill -STOP when fwcontrol -R
# 05/ 1/29: add -s option
# 04/12/31: more sophiscated encoding options
# 04/11/ 3: don't overwrite an existing file
# 04/10/ 2: add delay seconds and 2-pass option
# 04/ 7/11: from vrec script

# constants
#set eui64 = 0x00a0b02100003028
#set eui64 = 0x00a0b02100015512
#set eui64 = 0x00a0b0210001a733

set sleep = /bin/sleep
set rm = /bin/rm
set kill = /bin/kill
set killall = /usr/bin/killall
#set fwcontrol = /usr/sbin/fwcontrol
#set fwopt = (-M dv)
set fwcontrol = ~/ss/fwcontrol
set fwopt = ()
set grep = /usr/bin/grep
set ps = /bin/ps
set awk = /usr/bin/awk
set touch = /usr/bin/touch
set pkill = /usr/bin/pkill
set pgrep = /usr/bin/pgrep

set mencoder = /usr/local/bin/mencoder
set ht = /usr/local/bin/ht
set gvmvpidv_ctrl = $HOME/ss/gvmvpidv_ctrl

# default value
#set audiorate = 96
set audiorate = 128
set pass = 2
set bitrate = 1792
set delaysec = 0
set startDelay = 0
set qscale = 4.2
set xvid = 0
set unitNum = 0
set encStop = 0
set optB = "-m"

# check argument
set unknown = 0
top:
if ( $#argv >= 3 ) then
    switch("$argv[1]")
    case -1:
	set pass = 1
	shift argv
	goto top
    case -2:
	set pass = 2
	shift argv
	goto top
    case -b:
	set bitrate = $argv[2]
	shift argv
	shift argv
	goto top
    case -d:
	set delaysec = $argv[2]
	shift argv
	shift argv
	goto top
    case -e:
	set encStop = 1
	shift argv
	goto top
    case -3:
	set encStop = 1
	shift argv
	goto top
    case -l:
	set scale = $argv[2]
	shift argv
	shift argv
	goto top
    case -q:
	set qscale = $argv[2]
	set xvid = 1
	shift argv
	shift argv
	goto top
    case -s:
    case -ss:
	set startDelay = $argv[2]
	shift argv
	shift argv
	goto top
    case -u:
	set unitNum = $argv[2]
	shift argv
	shift argv
	goto top
    case -x:
	set xvid = 1
	shift argv
	goto top
    case -B:
	set optB = "-s"
	shift argv
	goto top
    case -?*:
	echo can\'t understand option $argv[1].
	shift argv
	set unknown = 1
	goto top
    endsw
endif
if ( $xvid ) then
    set pass = 1
endif
if ( ( $#argv != 3 ) || ( $unknown == 1) ) then
    echo Usage: $0 '[-1]' '[-2]' '[-b bitrate]' '[-l vqscale]' '[-d delaysec]' '[-s startDelay]' '[-x]' '[-q qscale]' '[-u unitNum]' '[-e]' '[-B]' ch sec file
    echo '  ' -x: xvid encoding '(-q option implies -x)'
    echo '  ' -e: encoding suspends when fwcontrol -R runs
    echo '  ' -B: recording both main and sub audios
    echo default values:
    echo '  ' lavc encoding
    echo '  ' 2-pass encoding '(only for lavc encoding)'
    echo '  ' bitrate == 2048 '[kbps]'
    echo '  ' delaysec == 0 '[sec]' '(sleep time for starting encoding)'
    echo '  ' startDelay == 0 '[sec]' '(sleep time for starting capturing)'
    echo '  ' qscale == 4.2
    echo '  ' unitNum == 0
    echo Example:
    echo '  ' $0 -1 -l 6 5 1666 hoge.avi
    echo '  ' $0 -u 1 -q 4.3 5 1711 foo.avi
    exit 1
endif


#
# set recording parameters
#
set eui64 = `$fwcontrol -u $unitNum | $grep \ 00-a0-b0-21-00- | $awk '{gsub("-","",$2);print "0x"$2}'`
if ( x"$eui64" == x ) then
    echo No device are found on unit number \#$unitNum.
    exit 2
endif

set ch = $argv[1]
set sec = $argv[2]
set file = $argv[3]
# generate fresh file name
while ( -e $file )
    set file = $file:r"-."$file:e
end
set dvfile = $file:r.dv
set p1file = $file:r.avi-
set logfile = $file:r.log

# set quality option for lavc
if ($?scale) then
    set quality = "vqscale=$scale"
else
    set quality = "vbitrate=$bitrate"
endif

# recording start
#$killall -STOP dnetc
#$ht off

$gvmvpidv_ctrl -m $optB -c $ch -u $unitNum $eui64
#$fwcontrol -u $unitNum -r
$sleep $startDelay
set fwopt = (-u $unitNum $fwopt)
($fwcontrol $fwopt -R $dvfile &) >& /dev/null
#($fwcontrol $fwopt -R $dvfile &)
#echo PPID: $$

set mencProcs = (`$ps axww | $grep -w $mencoder:t | $grep -vwE 'tv|grep' | $awk '{print $1}'`)
if ( $encStop && x"$mencProcs" != x ) then
    $kill -STOP $mencProcs
endif
$sleep $sec

#$killall fwcontrol
$pkill -f -- "$fwcontrol $fwopt -R"


# wait before encoding
#$sleep $delaysec
encodingWait:
#$pgrep $fwcontrol:t >& /dev/null
#if ( $status == 0 ) then
#    $sleep 10
#    goto encodingWait
#endif

# restart stopped process stopped when capturing
if ( $encStop && x"$mencProcs" != x ) then
    $kill -CONT $mencProcs
    set mencProcs = ()
endif
#$ht on
#$killall -CONT dnetc

# do not run many mencoders
set existMencProc = (`$pgrep $mencoder:t`)
if ( $#existMencProc > 1 ) then
    $sleep 10
    goto encodingWait
endif


if ( $pass == 1 ) then
# 1-pass encoding
#    nice +10 $mencoder -o $file -oac mp3lame -lameopts cbr:preset=$audiorate -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=$bitrate $dvfile >& /dev/null \
#    && $rm -f $dvfile
    if ( $xvid ) then
	nice +20 $mencoder -o $file -oac mp3lame -lameopts cbr:preset=$audiorate -ovc xvid -sws 2 -xvidencopts max_key_interval=300:vhq=4:max_bframes=1:trellis:chroma_me:chroma_opt:hq_ac:cartoon:fixed_quant=$qscale -vf hqdn3d=4:3:6 -noodml $dvfile >& /dev/null
    else
#	nice +20 $mencoder -o $file -oac mp3lame -lameopts cbr:preset=$audiorate -ovc lavc -lavcopts vcodec=mpeg4:vhq=4:vmax_b_frames=2:v4mv:vbitrate=$bitrate -vf hqdn3d=4:3:6 -ffourcc DIVX $dvfile >& /dev/null
	nice +20 $mencoder -o $file -oac mp3lame -lameopts cbr:preset=$audiorate -ovc lavc -lavcopts vcodec=mpeg4:vhq=4:vmax_b_frames=2:v4mv:$quality -vf hqdn3d=4:3:6 -ffourcc DIVX $dvfile >& /dev/null
    endif
    if ( ! $status ) then
	$touch -r $dvfile $file
removeWait:
#	$ps axww | $grep fwcontrol | $grep -vw grep | $grep fwcontrol >& /dev/null
	$pgrep $fwcontrol:t >& /dev/null
	if ( $status == 0 ) then
# fwcontrol is running
	    $sleep 10
	    goto removeWait
	endif
	if ( ! -z $file ) $rm -f $dvfile
    endif
# 2-pass encoding
else
#    nice +10 $mencoder -o $p1file -passlogfile $logfile -oac mp3lame -lameopts cbr:preset=$audiorate -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=${bitrate}:vpass=1 $dvfile >& /dev/null \
#    && nice +10 $mencoder -o $file -passlogfile $logfile -oac mp3lame -lameopts cbr:preset=$audiorate -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=${bitrate}:vpass=2 $dvfile >& /dev/null \
#    && $rm -f $dvfile $p1file $logfile
#    nice +10 $mencoder -o $p1file -passlogfile $logfile -oac mp3lame -lameopts cbr:preset=$audiorate -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=${bitrate}:vpass=1 $dvfile >& /dev/null \
#    && nice +10 $mencoder -o $file -passlogfile $logfile -oac mp3lame -lameopts cbr:preset=$audiorate -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=${bitrate}:vpass=2 $dvfile >& /dev/null
#    nice +20 $mencoder -o $p1file -passlogfile $logfile -oac mp3lame -lameopts cbr:preset=$audiorate -ovc lavc -lavcopts vcodec=mpeg4:vhq=4:vmax_b_frames=2:v4mv:vbitrate=${bitrate}:vpass=1 $dvfile >& /dev/null \
#    && nice +20 $mencoder -o $file -passlogfile $logfile -oac mp3lame -lameopts cbr:preset=$audiorate -ovc lavc -lavcopts vcodec=mpeg4:vhq=4:vmax_b_frames=2:v4mv:vbitrate=${bitrate}:vpass=2 $dvfile >& /dev/null
    nice +20 $mencoder -o $p1file -passlogfile $logfile -oac mp3lame -lameopts cbr:preset=$audiorate -ovc lavc -lavcopts vcodec=mpeg4:vhq=4:vmax_b_frames=2:v4mv:vbitrate=${bitrate}:vpass=1 -vf hqdn3d=4:3:6 -ffourcc DIVX $dvfile >& /dev/null \
    && nice +20 $mencoder -o $file -passlogfile $logfile -oac mp3lame -lameopts cbr:preset=$audiorate -ovc lavc -lavcopts vcodec=mpeg4:vhq=4:vmax_b_frames=2:v4mv:vbitrate=${bitrate}:vpass=2 -vf hqdn3d=4:3:6 -ffourcc DIVX $dvfile >& /dev/null
    if ( ! $status ) then
	$touch -r $dvfile $file
removeWait2:
#	$ps axww | $grep fwcontrol | $grep -vw grep | $grep fwcontrol >& /dev/null
	$pgrep $fwcontrol:t >& /dev/null
	if ( $status == 0 ) then
# fwcontrol is running
	    $sleep 10
	    goto removeWait2
	endif
	if ( ! -z $file ) $rm -f $dvfile $p1file $logfile
    endif
endif

exit 0
