1 |
#!/usr/bin/perl -w |
2 |
|
3 |
# amv.pl |
4 |
# |
5 |
# 07/19/07 19:21:39 CEST Dobrica Pavlinusic <dpavlin@rot13.org> |
6 |
|
7 |
use strict; |
8 |
|
9 |
use Data::Dump qw/dump/; |
10 |
use Carp qw/confess/; |
11 |
|
12 |
my $path = shift @ARGV || die "usage: $0 movie.amv\n"; |
13 |
|
14 |
open(my $fh, '<', $path) || die "can't open $path: $!"; |
15 |
|
16 |
# offset in file |
17 |
my $o = 0; |
18 |
|
19 |
# shared data hash |
20 |
my $d; |
21 |
|
22 |
sub hex_dump { |
23 |
my $bytes = shift || return; |
24 |
|
25 |
my $ascii = $bytes; |
26 |
$ascii =~ s/\W/./gs; |
27 |
my $hex = unpack('h*', $bytes); |
28 |
$hex =~ s/(..)/$1 /g; |
29 |
# calculate number of characters for offset |
30 |
#my $d = length( sprintf("%x",length($bytes)) ); |
31 |
my $d = 4; |
32 |
while ( $hex =~ s/^((?:\w\w\s){1,16})// ) { |
33 |
printf "## %0${d}x | %-48s| %s\n", $o, $1, substr( $ascii, 0, 16 ); |
34 |
if ( length($ascii) >= 16 ) { |
35 |
$ascii = substr( $ascii, 16 ); |
36 |
$o += 16; |
37 |
} else { |
38 |
$o += length($ascii); |
39 |
last; |
40 |
} |
41 |
} |
42 |
} |
43 |
|
44 |
sub x { |
45 |
my ($len,$format) = @_; |
46 |
|
47 |
my $bytes; |
48 |
read($fh, $bytes, $len); |
49 |
|
50 |
my $r_len = length($bytes); |
51 |
confess "read $r_len bytes, expected $len" if $len != $r_len; |
52 |
|
53 |
hex_dump( $bytes ); |
54 |
|
55 |
if ( $bytes eq 'AMV_END_' ) { |
56 |
warn "> end of file marker AMV_END_\n"; |
57 |
$d->{eof}++; |
58 |
return; |
59 |
} |
60 |
|
61 |
if ( $format ) { |
62 |
my @data = unpack($format, $bytes); |
63 |
warn "## unpacked = ",dump(@data),"\n"; |
64 |
return @data; |
65 |
} else { |
66 |
return $bytes; |
67 |
} |
68 |
} |
69 |
|
70 |
sub next_part { |
71 |
my ( $expected_part, $expected_len, $skip ) = @_; |
72 |
my ( $part, $len ) = x(8,'A4V'); |
73 |
return unless $len; |
74 |
confess "not $expected_part but $part" if $expected_part ne $part; |
75 |
if ( $expected_len ) { |
76 |
confess "expected $expected_len bytes for $part got $len" if $len != $expected_len; |
77 |
} |
78 |
printf ">> %s - %d 0x%x bytes\n", $part, $len, $len; |
79 |
x($len) if $skip; |
80 |
return $len; |
81 |
} |
82 |
|
83 |
my ( $riff, $amv ) = x(12, 'Z8Z4'); |
84 |
die "not RIFF but $riff" if $riff ne 'RIFF'; |
85 |
die "not AMV but $amv" if $amv ne 'AMV '; |
86 |
|
87 |
while ( ! defined($d->{eof}) ) { |
88 |
my ( $list, $name ) = x(12,'A4x4A4'); |
89 |
die "not LIST but $list" if $list ne 'LIST'; |
90 |
print "> $list .. $name\n"; |
91 |
|
92 |
if ( $name eq 'hdrl' ) { |
93 |
|
94 |
my $len = next_part( 'amvh', hex(38) ); |
95 |
|
96 |
my @names = ( qw/ms_per_frame width height fps ss mm hh/ ); |
97 |
my $h; |
98 |
map { |
99 |
my $v = $_; |
100 |
my $n = shift @names || die "no more names?"; |
101 |
$h->{$n} = $v; |
102 |
} x($len, 'Vx28VVVx8CCv'); |
103 |
|
104 |
printf "## %s %d*%d %s fps (%d ms/frame) %02d:%02d:%02d\n", |
105 |
$h->{path}, |
106 |
$h->{width}, $h->{height}, $h->{fps}, $h->{ms_per_frame}, |
107 |
$h->{hh}, $h->{mm}, $h->{ss}; |
108 |
|
109 |
$d->{amvh} = $h; |
110 |
|
111 |
} elsif ( $name eq 'strl' ) { |
112 |
|
113 |
next_part( 'strh', 0, 1 ); |
114 |
next_part( 'strf', 0, 1 ); |
115 |
|
116 |
} elsif ( $name eq 'movi' ) { |
117 |
|
118 |
while (1) { |
119 |
my $frame = $d->{movi}++; |
120 |
|
121 |
my $len = next_part( '00dc', 0, 1 ); |
122 |
last unless $len; |
123 |
printf ">> %s 00dc - frame %d jpeg %d 0x%x bytes\n", $name, $frame, $len, $len; |
124 |
|
125 |
my $len = next_part( '01wb', 0, 1 ); |
126 |
printf ">> %s 01wb - frame %d audio %d 0x%x bytes\n", $name, $frame, $len, $len; |
127 |
}; |
128 |
|
129 |
} else { |
130 |
die "unknown $list $name"; |
131 |
} |
132 |
} |