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

#######################################################################
my $oggfile="audiodump.ogg";


if (!defined($standalone)) {
    my $smogrify;
    if ($^O eq "MSWin32") {
	$smogrify="smogrify";
    }
    else {
	$smogrify=`which smogrify`;
	chomp($smogrify);
    }
    if ($smogrify ne "") {
	require $smogrify;
    }
}
else {
    $command=$ARGV[0];
}


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


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 "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");
    }
    # required
    &sig_complete;
    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;
    }
    &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 pcm wav, clipped frames
}

