#
# Convert an input stream to the specified interlace mode, where the modes are:
#
# 0 - none/progressive
# 1 - top-field-first interlaced
# 2 - bottom-field-first interlaced
#
# Of course, it reads from STDIN and writes to STDOUT, as you'd expect.
#

use YUV4MPEG;

if (($#ARGV < 0) || ($ARGV[0] < 0) || ($ARGV[0] > 2))
{ die "Invalid interlace mode" }

my $ilace_mode = $ARGV[0];
my $frameinfo = new YUV4MPEG::FrameInfo;
my $instreaminfo = new YUV4MPEG::StreamInfo;
my $outstreaminfo;

my ($inframe, $outframe);
my $yuv;

$instreaminfo->read(\*STDIN);

$outstreaminfo = new YUV4MPEG::StreamInfo($instreaminfo);

$outstreaminfo->set_interlace($ilace_mode);
$outstreaminfo->write(\*STDOUT);

$inframe = new YUV4MPEG::Frame($instreaminfo);
$outframe = new YUV4MPEG::Frame($outstreaminfo);

while ($inframe->read(\*STDIN, $frameinfo) == Y4M_OK)
{
    $yuv = $inframe->get_packed_data();
    
    $outframe->set_packed_data($yuv);
    $outframe->write(\*STDOUT, $frameinfo);
    
    $frameinfo = new YUV4MPEG::FrameInfo;
}
