#!/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.1

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

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


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


if ($command eq "version") {
    print "x264 encoder plugin v0.01\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)|0|size=1920x1080,fps=24000:1001;24|mpg|\n"; # NTSC 1024p
    print "x264_br_1080p|x264 blu-ray 1080p PAL (Experimental)|0|size=1920x1080,fps=25|mpg|\n"; # NTSC 1024p
    print "x264_br_720p|x264 blu-ray 720p NTSC (Experimental)|0|size=1280x720,fps=60000:1001|mpg|\n"; # 720p
    print "x264_br_720p|x264 blu-ray 720p PAL (Experimental)|0|size=1280x720,fps=50000:1001|mpg|\n"; # 720p

    exit 0;
}



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

    chdir $curtmpdir;

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

    `/bin/mkfifo $fifofile`;

    $syscom="mplayer -really-quiet -fps 1000000 mf://*$img_ext -vo yuv4mpeg:file=$fifofile &";
    system($syscom);

    $syscom="x264 --crf 16 --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 $outfile";
    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;

    &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
}

