home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / gfx / misc / mpeg_stat_2_2a.lha / mpeg_stat / src / block2spec.perl next >
Encoding:
Text File  |  1997-02-21  |  6.2 KB  |  233 lines

  1. #!/usr/bin/perl
  2. ;# 
  3. ;#  block2spec:  Convert "block_files" to "Spec files"
  4. ;# 
  5. ;#  ARGS:   -qs   -mv   -ftype   -ren  -noslice
  6. ;#          -start N   -end N   -o outfile
  7. ;#
  8. ;#  mpeg_stat -block_info block_file foo.mpg
  9. ;# will produce a block information file, which can then be converted
  10. ;# into a "specifics file" for mpeg_encode 1.5.  This is useful if you have
  11. ;# created an mpeg, and would like to recreate it with slightly different 
  12. ;# parameters, for example keeping all the same motion vectors, but with a 
  13. ;# different bit rate....
  14. ;# In that case you would only want the motion vectors information -mv
  15. ;#
  16. ;# a -makerelative option would be nice....
  17. ;# and something to realize all block are the same, and set frame QS etc.
  18. ;#
  19. ;# Output is to stdout, unless -o is used.
  20.  
  21.  
  22. ;($myname = $0) =~ s,.*/,,;
  23. ;$usage = <<_;
  24. Usage: $myname [-qs] [-mv] [-ftype] [-ren] [-start N] [-end N] [-o outfile] block_file
  25. Options:
  26.     -qs    Put in Qscale information
  27.     -mv    Put in motion vector information
  28.     -ftype Put in frame type information (I, P, B)
  29.     -all   All of the above (i.e. make the video the same).
  30.     -noslice   Do not copy slice information
  31.     -start N   (-sN) Begin at frame N
  32.     -end N     (-eN) End at frame N
  33.     -ren   (renumber) If used with -start, makes output frames start at 0
  34.     -o outfile  Output to file outfile
  35. _
  36.  
  37.  
  38. ;# Which parts of the spec to keep:
  39. $start = 0;
  40. $end = -1;
  41. $qs = 0;
  42. $mv = 0;
  43. $ft = 0;
  44. $noslice = 0;
  45. $renumber = 0;
  46. $outfile = "";
  47.  
  48.  
  49. ;# Go through the args....
  50. $i = 0;
  51. $ARGC = $#ARGV;
  52. $_ = $ARGV[1];
  53. if (/^-?$/ || /^-help$/) {&usage("");}
  54. while ($i < $ARGC) {
  55.     $_ = $ARGV[$i];
  56.                 # simple options
  57.     if (/^-mv$/) { $mv = 1; $i++; next;}
  58.     if (/^-qs$/) { $qs = 1; $i++; next;}
  59.     if (/^-ftype$/) { $ft = 1; $i++; next;}
  60.     if (/^-ren$/) { $renumber = 1; $i++; next;}
  61.     if (/^-renumber$/) { $renumber = 1; $i++; next;}
  62.     if (/^-noslice$/) { $noslice = 1; $i++; next;}
  63.     if (/^-all$/) { 
  64.     $mv = 1; $qs = 1; $ft = 1;
  65.     $i++; 
  66.     next;
  67.     }
  68.                 # two part options
  69.     if (/^-start$/) { $i++; $start=$ARGV[$i]; $i++; next;}
  70.     if (/^-end$/)   { $i++; $end=$ARGV[$i]; $i++; next;}
  71.     if (/^-o$/) {
  72.     $i++;
  73.     $outfile = $ARGV[$i];
  74.     $i++;
  75.     next;
  76.     }
  77.                 # appended options
  78.     ($car, $cdr) = /^-?(.)(.*)/;
  79.     if ($car eq 's') { $start=$cdr; $i++; next;}
  80.     if ($car eq 'e') { $end=$cdr; $i++; next;}
  81.     if ($car eq 'o') {
  82.     $outfile = $cdr;
  83.     $i++;
  84.     next;
  85.     }
  86.     &usage("Unknown option: $_\n\n");
  87.     $i++;
  88. }
  89.  
  90.                 # Setup output
  91. $file_name = $ARGV[$ARGC];
  92. if ($outfile ne "") {
  93.     open(OUTPUT, ">$outfile") || die "$outfile: $!\n";
  94.     select(OUTPUT);
  95. }
  96. print("/* Auto-converted block-info file */\n");
  97.  
  98. $frame = 0;
  99. $time_zero = 0;
  100. open(INPUT, $file_name) || die "$file_name: $!\n";
  101. $_ = <INPUT>;            # Get comment line
  102. print $_;            # Copy comment into output
  103. print "version 2\n";
  104. while (<INPUT>) {
  105.     ($car, $cdr) = /^(\S*) (.*)/;
  106.     $_ = $cdr;
  107.     if ($car eq "gop") {
  108.     next;
  109.     }
  110.  
  111.     if ($car eq "frame") {
  112.     ($mfnstr, $mft, $mpx, $mfn) = /(\d*) (\S) (\S*) (\d*)/;
  113.     if ($mfn == 0) {$time_zero = $mfnstr;}
  114.     $frame = $mfn+$time_zero;# Display frame number is temp_ref more than time 0
  115.     if ($renumber == 1) {    # virtual frame number?
  116.         $vfn = $frame - $start;
  117.     } else {
  118.         $vfn = $frame;
  119.     }
  120.     # handle stream order -> display order
  121.     if ($frame < $start) {next;}
  122.     if (($end >= 0) && ($frame > $end)) {last};
  123.     if ($ft == 0) {
  124.         print "frame $vfn - 0\n";
  125.     } else {
  126.         print "frame $vfn $mft 0\n";
  127.     }
  128.     next;
  129.     }
  130.     if ($frame < $start) {next;}    
  131.     if ($car eq "slice") {
  132.     if ($noslice == 1) {next;}
  133.     ($msn, $msqs) = /(\d*) (\d*)/;
  134.     if ($qs == 0) {
  135.         print "slice $msn 0\n";
  136.     } else {
  137.         print "slice $msn $msqs\n";
  138.     }
  139.     next;
  140.     }
  141.  
  142.     if ($car eq "block") {
  143.     #sample: block 1 B 6 39 forw+back <0, 1> <1, 0> 100010
  144.     @blockinfo = split(' ');
  145.     $bn = $blockinfo[0];
  146.     $bqs = $blockinfo[2];
  147.     if ($qs == 0) {
  148.         $vqs = 0;
  149.     } else {
  150.         $vqs = $bqs;
  151.     }
  152.     if ($mv == 0) {        # easy ones!
  153.         print "block $bn $vqs\n";
  154.     } else {
  155.         $bty = $blockinfo[4];
  156.         if ($bty eq "skip") {
  157.         print "block $bn $vqs skip\n";
  158.         } elsif ($bty eq intra) { # no way to specify!
  159.         print "block $bn $vqs\n";
  160.         } elsif ($bty eq "forw") {
  161.         $ymv = $blockinfo[5];
  162.         $ymv =~ s/<([-\d]*),/$1/;
  163.         $xmv = $blockinfo[6];
  164.         $xmv =~ s/>//;
  165.         print "block $bn $vqs forw $ymv $xmv\n";
  166.         } elsif ($bty eq "back") {
  167.         $ymv = $blockinfo[5];
  168.         $ymv =~ s/<([-\d]*),/$1/;
  169.         $xmv = $blockinfo[6];
  170.         $xmv =~ s/>//;
  171.         print "block $bn $vqs back $ymv $xmv\n";
  172.         } elsif ($bty eq "forw+back") {
  173.         $fymv = $blockinfo[5];
  174.         $fymv =~ s/<([-\d]*),/$1/;
  175.         $fxmv = $blockinfo[6];
  176.         $fxmv =~ s/>//;
  177.         $bymv = $blockinfo[7];
  178.         $bymv =~ s/<([-\d]*),/$1/;
  179.         $bxmv = $blockinfo[8];
  180.         $bxmv =~ s/>//;
  181.         print "block $bn $vqs bi $fymv $fxmv $bymv $bxmv\n";
  182.         } elsif ($bty eq "0") {
  183.         print "block $bn $vqs\n"; # no way to specify
  184.         } else {
  185.         print STDERR "Block what? $_\n";
  186.         }
  187.     }               
  188.     next;
  189.     }
  190.  
  191.     print STDERR "What is this?: +$car+ -$cdr-\n"
  192. }
  193.  
  194.                 # All done
  195. if ($outfile ne "") {
  196.     close(OUTPUT);
  197. }
  198. exit;
  199.  
  200.  
  201. sub usage {
  202.     select(STDERR);
  203.     print @_, $usage;
  204.     print "$rcsid\n" if $rcsid =~ /:/;
  205.     exit;
  206. }
  207.  
  208.  
  209. # Copyright (c) 1995 University of California at Berkeley
  210. # Written by Steve Smoot
  211. # My first "real" Perl program, so I apologize for any sillyness
  212. #
  213. # Permission to use, copy, modify, and distribute this software and its
  214. # documentation for any purpose, without fee, and without written agreement is
  215. # hereby granted, provided that the above copyright notices and the following
  216. # two paragraphs appear in all copies of this software.
  217. # IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  218. # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  219. # OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  220. # CALIFORNIA or the Technical University of Berlin HAS BEEN ADVISED OF THE 
  221. # POSSIBILITY OF SUCH DAMAGE.
  222. # THE UNIVERSITY OF CALIFORNIA
  223. # SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
  224. # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  225. # PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE 
  226. # UNIVERSITY OF CALIFORNIA HAS NO 
  227. # OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, 
  228. # OR MODIFICATIONS.
  229. #
  230.