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