#!/usr/bin/perl
;# 
;#  block2spec:  Convert "block_files" to "Spec files"
;# 
;#  ARGS:   -qs   -mv   -ftype   -ren  -noslice
;#          -start N   -end N   -o outfile
;#
;#  mpeg_stat -block_info block_file foo.mpg
;# will produce a block information file, which can then be converted
;# into a "specifics file" for mpeg_encode 1.5.  This is useful if you have
;# created an mpeg, and would like to recreate it with slightly different 
;# parameters, for example keeping all the same motion vectors, but with a 
;# different bit rate....
;# In that case you would only want the motion vectors information -mv
;#
;# a -makerelative option would be nice....
;# and something to realize all block are the same, and set frame QS etc.
;#
;# Output is to stdout, unless -o is used.


;($myname = $0) =~ s,.*/,,;
;$usage = <<_;
Usage: $myname [-qs] [-mv] [-ftype] [-ren] [-start N] [-end N] [-o outfile] block_file
Options:
    -qs    Put in Qscale information
    -mv    Put in motion vector information
    -ftype Put in frame type information (I, P, B)
    -all   All of the above (i.e. make the video the same).
    -noslice   Do not copy slice information
    -start N   (-sN) Begin at frame N
    -end N     (-eN) End at frame N
    -ren   (renumber) If used with -start, makes output frames start at 0
    -o outfile  Output to file outfile
_


;# Which parts of the spec to keep:
$start = 0;
$end = -1;
$qs = 0;
$mv = 0;
$ft = 0;
$noslice = 0;
$renumber = 0;
$outfile = "";


;# Go through the args....
$i = 0;
$ARGC = $#ARGV;
$_ = $ARGV[1];
if (/^-?$/ || /^-help$/) {&usage("");}
while ($i < $ARGC) {
    $_ = $ARGV[$i];
				# simple options
    if (/^-mv$/) { $mv = 1; $i++; next;}
    if (/^-qs$/) { $qs = 1; $i++; next;}
    if (/^-ftype$/) { $ft = 1; $i++; next;}
    if (/^-ren$/) { $renumber = 1; $i++; next;}
    if (/^-renumber$/) { $renumber = 1; $i++; next;}
    if (/^-noslice$/) { $noslice = 1; $i++; next;}
    if (/^-all$/) { 
	$mv = 1; $qs = 1; $ft = 1;
	$i++; 
	next;
    }
				# two part options
    if (/^-start$/) { $i++; $start=$ARGV[$i]; $i++; next;}
    if (/^-end$/)   { $i++; $end=$ARGV[$i]; $i++; next;}
    if (/^-o$/) {
	$i++;
	$outfile = $ARGV[$i];
	$i++;
	next;
    }
				# appended options
    ($car, $cdr) = /^-?(.)(.*)/;
    if ($car eq 's') { $start=$cdr; $i++; next;}
    if ($car eq 'e') { $end=$cdr; $i++; next;}
    if ($car eq 'o') {
	$outfile = $cdr;
	$i++;
	next;
    }
    &usage("Unknown option: $_\n\n");
    $i++;
}

				# Setup output
$file_name = $ARGV[$ARGC];
if ($outfile ne "") {
    open(OUTPUT, ">$outfile") || die "$outfile: $!\n";
    select(OUTPUT);
}
print("/* Auto-converted block-info file */\n");

$frame = 0;
$time_zero = 0;
open(INPUT, $file_name) || die "$file_name: $!\n";
$_ = <INPUT>;			# Get comment line
print $_;			# Copy comment into output
print "version 2\n";
while (<INPUT>) {
    ($car, $cdr) = /^(\S*) (.*)/;
    $_ = $cdr;
    if ($car eq "gop") {
	next;
    }

    if ($car eq "frame") {
	($mfnstr, $mft, $mpx, $mfn) = /(\d*) (\S) (\S*) (\d*)/;
	if ($mfn == 0) {$time_zero = $mfnstr;}
	$frame = $mfn+$time_zero;# Display frame number is temp_ref more than time 0
	if ($renumber == 1) {	# virtual frame number?
	    $vfn = $frame - $start;
	} else {
	    $vfn = $frame;
	}
	# handle stream order -> display order
	if ($frame < $start) {next;}
	if (($end >= 0) && ($frame > $end)) {last};
	if ($ft == 0) {
	    print "frame $vfn - 0\n";
	} else {
	    print "frame $vfn $mft 0\n";
	}
	next;
    }
    if ($frame < $start) {next;}    
    if ($car eq "slice") {
	if ($noslice == 1) {next;}
	($msn, $msqs) = /(\d*) (\d*)/;
	if ($qs == 0) {
	    print "slice $msn 0\n";
	} else {
	    print "slice $msn $msqs\n";
	}
	next;
    }

    if ($car eq "block") {
	#sample: block 1 B 6 39 forw+back <0, 1> <1, 0> 100010
	@blockinfo = split(' ');
	$bn = $blockinfo[0];
	$bqs = $blockinfo[2];
	if ($qs == 0) {
	    $vqs = 0;
	} else {
	    $vqs = $bqs;
	}
	if ($mv == 0) {		# easy ones!
	    print "block $bn $vqs\n";
	} else {
	    $bty = $blockinfo[4];
	    if ($bty eq "skip") {
		print "block $bn $vqs skip\n";
	    } elsif ($bty eq intra) { # no way to specify!
		print "block $bn $vqs\n";
	    } elsif ($bty eq "forw") {
		$ymv = $blockinfo[5];
		$ymv =~ s/<([-\d]*),/$1/;
		$xmv = $blockinfo[6];
		$xmv =~ s/>//;
		print "block $bn $vqs forw $ymv $xmv\n";
	    } elsif ($bty eq "back") {
		$ymv = $blockinfo[5];
		$ymv =~ s/<([-\d]*),/$1/;
		$xmv = $blockinfo[6];
		$xmv =~ s/>//;
		print "block $bn $vqs back $ymv $xmv\n";
	    } elsif ($bty eq "forw+back") {
		$fymv = $blockinfo[5];
		$fymv =~ s/<([-\d]*),/$1/;
		$fxmv = $blockinfo[6];
		$fxmv =~ s/>//;
		$bymv = $blockinfo[7];
		$bymv =~ s/<([-\d]*),/$1/;
		$bxmv = $blockinfo[8];
		$bxmv =~ s/>//;
		print "block $bn $vqs bi $fymv $fxmv $bymv $bxmv\n";
	    } elsif ($bty eq "0") {
		print "block $bn $vqs\n"; # no way to specify
	    } else {
		print STDERR "Block what? $_\n";
	    }
	}		       
	next;
    }

    print STDERR "What is this?: +$car+ -$cdr-\n"
}

				# All done
if ($outfile ne "") {
    close(OUTPUT);
}
exit;


sub usage {
    select(STDERR);
    print @_, $usage;
    print "$rcsid\n" if $rcsid =~ /:/;
    exit;
}


# Copyright (c) 1995 University of California at Berkeley
# 
# Written by Steve Smoot
# My first "real" Perl program, so I apologize for any sillyness
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose, without fee, and without written agreement is
# hereby granted, provided that the above copyright notices and the following
# two paragraphs appear in all copies of this software.
# 
# IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
# DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
# OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
# CALIFORNIA or the Technical University of Berlin HAS BEEN ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.
# 
# THE UNIVERSITY OF CALIFORNIA
# SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE 
# UNIVERSITY OF CALIFORNIA HAS NO 
# OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, 
# OR MODIFICATIONS.
#
