#!/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 x264 plugin v0.3

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

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


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


if ($command eq "version") {
    print "x264 encoder plugin v0.03\n";
    exit 0;
}


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

    if (&location("x264") eq "") {
	print "x264 was not found. Please install x264 and try again.";
	exit 1;
    }

    if (&location("mplayer") eq "") {
	print "mplayer was not found. Please install it and try again.";
	exit 1;
    }
    
    # end init code
    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 "4\n";
    exit 0;
}



if ($command eq "get_formats") {
   # for each format: -
   # return format_name|display_name|audio_types|restrictions|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
    
    print "x264_br_1080p|x264 blu-ray 1080p NTSC (Experimental)|32|size=1920x1080,fps=24000:1001;24|mpg|\n"; # NTSC 1024p
    print "x264_br_1080p|x264 blu-ray 1080p PAL (Experimental)|32|size=1920x1080,fps=25|mpg|\n"; # NTSC 1024p
    print "x264_br_720p|x264 blu-ray 720p NTSC (Experimental)|32|size=1280x720,fps=60000:1001|mpg|\n"; # 720p
    print "x264_br_720p|x264 blu-ray 720p PAL (Experimental)|32|size=1280x720,fps=50000:1001|mpg|\n"; # 720p

    exit 0;
}



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

    if ($audiofile ne "" && &location("MP4Box") eq "") {
	print STDERR "You must install MP4Box in order to encode with audio.\nTry installing the gpac package.\n";
	&sig_complete;
	exit 1;
    }

    if ($audiofile ne "" && &location("ffmpeg") eq "") {
	print STDERR "You must install ffmpeg in order to encode with audio.\n";
	&sig_complete;
	exit 1;
    }

    chdir $curtmpdir;

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

    $fifofile="264stream.y4m";
    unlink $fifofile;

    $syscom="mplayer -frames $end mf://*$img_ext -nosound -benchmark -fps $fps -ao null -vo yuv4mpeg:file=$fifofile $err </dev/null";

    print STDERR "Running stream to fifo with command:\n$syscom\n";

    system($syscom);


    $ofile = $nfile;

    if ($audiofile ne "") {
	$ofile="output.264";
    }

    $syscom="x264 --frames $end --crf 16 --threads auto --preset veryslow --tune film --weightp 0 --bframes 3 --nal-hrd vbr --vbv-maxrate 40000 --vbv-bufsize 30000 --level 4.1 --keyint 24 --b-pyramid strict --slices 4 --aud --colorprim \"bt709\" --transfer \"bt709\" --colormatrix \"bt709\" --sar 1:1 $fifofile --fps $fps -o \"$ofile\" $err";

    print STDERR "Processing fifo with:\n$syscom\n";

    system($syscom);


    if ($audiofile ne "") {

	$syscom="ffmpeg -i audiodump.wav -ab 192000 temp.aac $err";
	
	print STDERR "Encoding audio with:\n$syscom\n";

	system($syscom);

	$syscom="MP4Box -fps $fps -add output.264 -add temp.aac \"$nfile\" $err";

	
	print STDERR "Muxing with:\n$syscom\n";
	
	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

    $fifofile="264stream.yvm";
    unlink $fifofile;

    unlink "output.264";

    unlink "temp.aac";

    &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 7; # clipped wav, clipped frames
}

