#!/usr/bin/perl

# (c) G. Finch (salsaman) 2010

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

#######################################################################
# LiVES qtutils encoder plugin v0.3

# 0.3 add (undocumented) -l $length to get it working
# 0.31 update API

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

my $USE_STRICT = 1;
if ($USE_STRICT) {
    use strict;
}

my $USE_WARNINGS = 1;
if ($USE_WARNINGS) {
    use warnings;
}

use POSIX;     # Needed for setlocale()
setlocale(LC_NUMERIC, "C");

my $nulfile;
my $exe;

if ($^O eq "MSWin32") {
    $nulfile = "NUL";
    $exe = ".exe";
} else {
    $nulfile = "/dev/null";
    $exe = "";
}

if (!caller) {
    our $command;
    #our $otype;
    exit 0 if (!defined($ARGV[0]));
}

if (!defined($command)) {
    $command  = $ARGV[0];
}

my $oggfile="audiodump.ogg";

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


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


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

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


if ($command eq "get_capabilities") {
    # 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|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 "qt|Quicktime for Linux|8|none|mov|";

# TODO - try and fix audio encoding in qtsg
   print "qt|Quicktime for Linux|0|none|mov|";

   exit 0;
}


if ($command eq "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

    print 7; # clipped pcm wav, all frames
    exit 0;
}


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

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

    $length=($end - $start + 1);

    if (-f "audiodump.wav") {
	if (defined($DEBUG_ENCODERS)) {
	    print "encoder command 1 is: oggenc -q 10 -o $oggfile audiodump.wav\n";
	}
	system("oggenc -q 10 -o $oggfile audiodump.wav $err");
	if (defined($DEBUG_ENCODERS)) {
	    print "encoder command 2 is: qtsg -o \"$nfile\" -c twos -a $oggfile -r $fps *.jpg\n";
	}
	system("qtsg -o \"$nfile\" -c twos -a $oggfile -r $fps *.jpg $err");
    }
    else {
	if (defined($DEBUG_ENCODERS)) {
	    print "encoder command is: qtsg -o \"$nfile\" -l $length -r $fps *.jpg\n";
	}
	system("qtsg -o \"$nfile\" -l $length -r $fps *.jpg $err");
    }

    exit 0;
}


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

    if (-f $oggfile) {
	unlink $oggfile;
    }
    exit 0;
}


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

    # ...

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


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

sub location {
    # return the location of an executable
   my ($command) = shift;

    if ($^O eq "MSWin32") {
	return "$command.exe";
    }

    my ($location) = `which \"$command\" 2>$nulfile`;
    chomp($location);
    $location;
}


##### required
1;
