#!/usr/bin/perl

#######################################################################
# LiVES ffmpeg plugin v1.0

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

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


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


if ($command eq "init") {

    # perform any initialisation needed
    # On error, print error message and exit 1
    # otherwise exit 0
    
    if (&location("ffmpeg") eq "") {
        print "The ffmpeg binary was not found, please install it and try again !";
	exit 1;
    }

    ##############################
    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 "ffmpeg -hq\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 "divx|divx (25 fps)|3|fps=25.00|";
    print "asf|asf|3|none|";
    print "vob|vob (23.98 fps)|1|fps=23.98|";
    print "vcd|vcd (23.98 fps)|1|fps=23.98|\n";
    exit 0;
}



if ($command eq "encode") {
    # encode

    if ($otype eq "") {
	$otype="divx";
	&rc_set("output_type",$otype);
    }

    # default seems to be divx
    $vcodec="";
    if ($otype eq "asf") {
	$vcodec="-f asf";
    }
    if ($otype eq "vcd") {
	$vcodec="-f vcd";
    }
    if ($otype eq "vob") {
	$vcodec="-f vob";
    }

    # video stream
    $audio_com="";
    unless ($audiofile eq "") {
	$aq=&rc_get("audio_encode_quality");
	$audio_com="-i $audiofile";
	if ($aq eq "high") {
	    $audio_com.=" -acodec copy";
	}
	else {
	    $audio_com.=" -acodec mp2";
	}
    }
    
    $vid_length=($end-$start+1)/$fps;

    # ffmpeg doesn't seem to set any comments...
    $syscom="$encoder_command -comment \"$comment\" -author \"$author\" -title \"$title\" -y -f image -i %8d.jpg $audio_com -r $fps -t $vid_length $vcodec \"$nfile\">.err 2>.err2 &";
    system ($syscom);

    do {
	# stop when file isn't growing
	$fsize=-s "$nfile";
	`sync`;
	sleep(1);
	$newfsize=-s "$nfile";
    
    } while ($fsize!=$newfsize);
    
    # unfortunately, ffmpeg just sits in the background
    # so we have to kill it off
    `killall -9 -e \"$encoder_command -y -f image\" >/dev/null 2>&1`;
    
    # ffmpeg can segfault during encoding, we must check for that
    open IN, ".err";
    while ($line=<IN>) {
	$lastline=$line;
    }
    close IN;
    unlink ".err";
    open IN, ".err2";
    while ($line=<IN>) {
	$lastline2=$line;
    }
    close IN;
    unlink ".err2";

    if ($lastline2 =~ /(.*)(bit_rate)(.*)/) {
	&sig_error("Ffmpeg could not encode at this audio rate.","Please try resampling the audio to a standard rate.");
    }
    unless ($lastline =~ /\)/) {
	&sig_error("Unfortunately this video could not be encoded due to an error in ffmpeg.","Please check the saved file.","","See: http://sourceforge.net/tracker/index.php?func=detail&aid=774227&group_id=16082&atid=116082");
    }
    
    &sig_complete;
    exit 0;
}



if ($command eq "clear") {
    # this is called after "encode"
    # note that encoding could have been stopped at any time

    &sig_complete;
    exit 0;
}



if ($command eq "finalise") {
    # do any finalising code
    
    # end finalising code
    print "finalised\n";
    exit 0;
}

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 7; # clipped pcm wav, frames start at selection
}


