#!/usr/bin/perl

#######################################################################
# LiVES encodedv plugin v1.0

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

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


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


if ($command eq "version") {
    print "encodedv encoder plugin v1.0\n";
    exit 0;
}


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

    if (&location("encodedv") eq "") {
	print "Encodedv was not found. Please install libdv2-apps and try again.";
	exit 1;
    }
    
    # end init code
    print "initialised\n";
    exit 0;
}

if ($command eq "get_default_command") {
    # return the default command
    # the user can edit this, and it is passed as $encoder_command in "encode"
    print "encodedv\n";
    exit 0;
}


if ($command eq "get_capabilities") {
    # return capabilities - this is a bitmap field
    # 1 = can encode with frame changes (with an event.frames file)
    # 2 = can encode with fps changes (with an event.fps file)
    print "0\n";
    exit 0;
}



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

   # 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'

    if (&rc_get("PAL/NTSC") eq "NTSC") {
	print "dv|dv|2|size=720x480,fps=29.97|"; # NTSC
    }
    else {
	print "dv|dv|2|size=720x576,fps=25.00|"; # PAL
    }
    exit 0;
}



if ($command eq "encode") {
    # encode 
    $end-=1;
    if (&rc_get("PAL/NTSC") eq "NTSC") {
	$to_ext=".ppm";
    }
    else {
	$to_ext=".pgm";
    }

    for ($i=$start;$i<=$end;$i++) {
	$name=&mkname($i);
	$syscom="$convert_command $name$img_ext $name$to_ext";
	system $syscom;
    }
    
    $syscom=$encoder_command . " -s $start -e $end %08d.$to_ext -a wav audiodump.wav > \"$nfile\"";
    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
    if (&rc_get("PAL/NTSC") eq "NTSC") {
	$to_ext=".ppm";
    }
    else {
	$to_ext=".pgm";
    }

    for ($i=$start;$i<=$end;$i++) {
	$name=&mkname($i);
	if (-f "$name$to_ext") {
	    unlink "$name$to_ext";
	}
    }
    &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 3; # clipped wav, all frames
}

