#!/usr/bin/perl

# (c) G. Finch (salsaman)

# released under the GNU GPL 3 or later
# see file COPYING or www.gnu.org for details


#######################################################################
# LiVES ffmpeg plugin v2.1
# v 2.1 - add experimental 3gp support
# v 2.2 - format changed in ffmpeg from image to image2, update

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

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


if ($command eq "version") {
    print "ffmpeg encoder plugin v2.1\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_capabilities") {
    # return capabilities - this is a bitmap field
    # bit 0 - takes extra parameters (using RFX request)
    # bit 1 - unused
    # bit 2 - can encode png
    # bit 3 - not pure perl
    print "0\n";
    exit 0;
}


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

   # 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)|5|fps=25.00|avi|\n";
    print "asf|asf|5|none|asf|\n";
    print "3gp_h263|3gp (h263)|96|size=176x144,arate=8000|3gp|\n";
    print "3gp_mp4|3gp (mp4)|96|size=176x144,arate=8000|3gp|\n";
    #print "theora|ogg-theora (experimental, requires ffmpeg2theora)|5|none|ogv\n";
    exit 0;
}



if ($command eq "encode") {
    # encode
    $encoder_command="ffmpeg";

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

    # default seems to be divx
    $vcodec="";
    if ($otype eq "asf") {
	$vcodec="-f asf";
    }
    elsif ($otype eq "3gp_h263") {
	$vcodec="-f h263";
    }
    elsif ($otype eq "3gp_mp4") {
	$vcodec="-f mp4";
    }
    # video stream
    $audio_com="";
    unless ($audiofile eq "") {
	$aq=&rc_get("encoder_acodec");
	$audio_com="-i $audiofile";
	if ($aq==0) {
	    $audio_com.=" -acodec copy";
	}
	elsif ($aq==5) {
	    $audio_com=" -acodec aac -ac 1 -ab 12 -ar 8000";
	}
	elsif ($aq==5) {
	    $audio_com=" -acodec amr_nb -ac 1 -ab 12 -ar 8000";
	}
	else {
	    $audio_com.=" -acodec mp2";
	}
    }
    
    $vid_length=($end-$start+1)/$fps;

    $err=">/dev/null 2>&1";
    if (defined($DEBUG_ENCODERS)) {
	$err="1>&2";
    }

    # unfortunately this does not work yet...
    if ($otype eq "theora") {
	$syscom="ffmpeg2theora -o \"$nfile\" -f image2 -i %8d$img_ext $audio_com -d off $err";
    }
    elsif ($otype eq "3gp") {
	$passfile="passfile";
	for $pass (1,2) {
	    $syscom="$encoder_command -strict 1 -pass $pass -passfile $passfile -y -f image2 -i %8d$img_ext $audio_com -r $fps -t $vid_length $vcodec \"$nfile\" $err";
	    if ($pass==1) {
		if (defined($DEBUG_ENCODERS)) {
		    print STDERR "ffmpeg_encoder command is: $syscom\n";
		}
		
		system ($syscom);
	    }
	}
    }
    else {
	$syscom="$encoder_command -comment \"$comment\" -author \"$author\" -title \"$title\" -y -f image2 -i %8d$img_ext $audio_com -r $fps -t $vid_length $vcodec \"$nfile\" $err";
    }

    if (defined($DEBUG_ENCODERS)) {
	print STDERR "ffmpeg_encoder command is: $syscom\n";
    }

    system ($syscom);

    &sig_complete;
    exit 0;
}



if ($command eq "clear") {
    # this is called after "encode"
    # note that encoding could have been stopped at any time
    unlink "passfile";
    &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
}


