#!/usr/bin/perl

#######################################################################
# LiVES transcode plugin v1.1

#######################################################################

if (!defined($standalone)) {
    my $smogrify=`which smogrify`;
    chomp($smogrify);
    require $smogrify;
}
else {
    $command=$ARGV[0];
}


#######################################################################
# version history - 
# 1.1 gf - changed get_image_size so it takes a parameter ($file)
# 1.2 gf - add more formats, clean up syntax


if ($command eq "version") {
    print "transcode encoder plugin v1.2\n";
    exit 0;
}


if ($command eq "init") {
    # perform any initialisation needed
    # On error, print error message and exit 1
    # otherwise exit 0

    if (&location("transcode") eq "") {
	print "Transcode was not found. Please install transcode and try again.";
	exit 1;
    }
    
    # end init code
    print "initialised\n";
    exit 0;
}

if ($command eq "get_default_command") {
    # return the default command
    # the user can edit this, and it is passed as $encoder_command in "encode"
    print "transcode\n";
    exit 0;
}


if ($command eq "get_capabilities") {
    # return capabilities - this is a bitmap field
    # 1 = can encode with frame changes (with an event.frames file)
    # 2 = can encode with fps changes (with an event.fps file)
    print "0\n";
    exit 0;
}



if ($command eq "get_formats") {
   # for each format: -
   # return format_name|display_name|audio_types|restrictions|

   # audio types are: 0 - cannot encode audio, 1 - can encode using
   #  mp3, 2 - can encode using pcm, 3 - can encode using pcm and mp3
    
   # restrictions: currently the only one implemented is 'fps=xx.yy' which
   # means "can only encode at xx.yy frames per second" 
   # - otherwise set it to 'none'

    print "mjpeg|mjpeg (works well)|3|none|";
    print "mpeg4|mpeg4 (works well)|3|none|";
    print "mpeg2|mpeg2 (untested)|3|none|";
    print "mpeg1video|mpeg1 (no audio)|0|none|";
    print "wmv2|wmv2 (untested)|3|none|";

    exit 0;
}



if ($command eq "encode") {
    # encode
    $end--;

    # make tombstone file - transcode should pass this to ffmpeg but doesn't
    open OUT,"> comments";
    print OUT "INAM $title\n";
    print OUT "IART $author\n";
    print OUT "ICMT $comment\n";
    close OUT;

    # make a list of the frames to encode
    unlink "list";
    for ($i=$start;$i<=$end;$i++) {
	$name=&mkname($i);
	$syscom="echo $name$img_ext >> list";
	system $syscom;
	if ($i==$start) {
	    # add the first frame twice, otherwise it is cut
	    system $syscom;
	}
    }

    # get image size of start frame
    $file=$name.$img_ext;
    $imresact="none";
    &get_image_size($file);


    if ($otype eq "mpeg4"||$otype eq "mjpeg"||$otype eq "wmv2"||$otype eq "mpeg1video") {
	# use ffmpeg
	if (-f $audiofile) {
	    if (&rc_get("audio_encode_quality") eq "high") {
		#pcm encoding
		$syscom=$encoder_command . " -i list -x imlist,raw -p $audiofile -e $arate,$asamps,$achans -E $arate,$asamps,$achans -g $hsize"."x$vsize -f $fps -o \"$nfile\" -y ffmpeg -F $otype -N 0x1 -z -k --avi_comments comments >/dev/null 2>&1";
	    }
	    else {
		# mp3 encoding
		$syscom=$encoder_command . " -i list -x imlist,raw -p $audiofile -e $arate,$asamps,$achans -E $arate,$asamps,$achans -g $hsize"."x$vsize -f $fps -o \"$nfile\" -y ffmpeg -F $otype -N 0x55 -z -k --avi_comments comments >/dev/null 2>&1";
	    }
	}
	else {
	    $syscom=$encoder_command . " -i list -x imlist,null -g $hsize"."x$vsize -f $fps -o \"$nfile\" -y ffmpeg -F $otype -N 0x1 -z -k --avi_comments comments >/dev/null 2>&1";
	}
    }
    else {
	if (-f $audiofile) {
	    if (&rc_get("audio_encode_quality") eq "high") {
		#pcm encoding
		$syscom=$encoder_command . " -i list -x imlist,raw -p $audiofile -e $arate,$asamps,$achans -E $arate,$asamps,$achans -g $hsize"."x$vsize -f $fps -o \"$nfile\" -y $otype -N 0x1 -z -k --avi_comments comments >/dev/null 2>&1";
	    }
	    else {
		# mp3 encoding
		$syscom=$encoder_command . " -i list -x imlist,raw -p $audiofile -e $arate,$asamps,$achans -E $arate,$asamps,$achans -g $hsize"."x$vsize -f $fps -o \"$nfile\" -y $otype -N 0x55 -z -k --avi_comments comments >/dev/null 2>&1";
	    }
	}
	else {
	    $syscom=$encoder_command . " -i list -x imlist,null -g $hsize"."x$vsize -f $fps -o \"$nfile\" -y $otype -N 0x1 -z -k --avi_comments comments >/dev/null 2>&1";
	}
    }
    system($syscom);

    # required
    &sig_complete;
    exit 0;
}


if ($command eq "clear") {
    # this is called after "encode"
    # note that encoding could have been stopped at any time
    if (-f "list") {
	unlink "list";
    }
    if (-f "comments") {
	unlink "comments";
    }
    &sig_complete;
    exit 0;
}


if ($command eq "finalise") {
    # do any finalising code

    # ...

    # end finalising code
    print "finalised\n";
    exit 0;
}


###### subroutines #######




sub get_format_request {
    # return the code for how we would like audio and video delivered
    # this is a bitmap field composed of:
    # bit 0 - unset=raw pcm audio; set=pcm audio with wav header
    # bit 1 - unset=all audio; set=clipped audio
    # bit 2 - unset=all frames; set=frames for selection only

    return 1; # clipped raw, all frames
}

