#!/usr/bin/perl

#######################################################################
# LiVES mencoder plugin v1.0

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

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


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


if ($command eq "version") {
    print "mencoder 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("mencoder") eq "") {
	print "Mencoder was not found. Please install it 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 "mencoder\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'

   print "mjpeg|MJPG|2|none|";
   print "mpg1|mpeg1 (23.98 fps)|2|fps=23.98|";
   exit 0;
}



if ($command eq "encode") {
    # encode 
    if ($otype eq "") {
	$otype="mjpeg";
	&rc_set("output_type",$otype);
    }

    # set the codec
    $vcodec="mjpeg";
    
    if ($otype eq "mpg1") {
	$vcodec="mpeg1video";
    }
    
    $mf_ver=&rc_get("mplayer_mf_version");
    
    #mencoder assumes 25fps when calculating the start
    $vid_start=($start-1)/25;    
    $tmpvid="tmpvid";
    
    #prepare video stream
    if ($mf_ver==1) {
	$syscom=$encoder_command . " -mf on -mf type=jpg -o \"" . $tmpvid . "\" -ovc lavc -lavcopts vcodec=mjpeg -noskip -ofps " . $fps . " \\*" . $img_ext . " -ss " . $vid_start . " -frames " . ($end-$start+1) . " >/dev/null 2>&1";	    }
    else {
	$syscom=$encoder_command . " -mf type=jpg -o \"" . $tmpvid . "\" -ovc lavc -lavcopts vcodec=mjpeg -noskip -ofps " . $fps . " mf://*" . $img_ext . " -ss " . $vid_start . " -frames " . ($end-$start+1) . " >/dev/null 2>&1";
    }
    system($syscom);

    $audio_com="";
    unless ($audiofile eq "") {
	$audio_com=" -oac copy -audiofile " . $audiofile;
    }

    $syscom=$encoder_command . " -mc 0 -o \"$nfile\" $audio_com -ovc lavc -lavcopts vcodec=$vcodec -noskip -ofps $fps $tmpvid -info comment=\"$comment\":name=\"$title\":artist=\"$author\" >/dev/null 2>&1";
    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
    $tmpvid="tmpvid";
    if (-f $tmpvid) {
	unlink $tmpvid;
    }
    &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 pcm wav, all frames
}

