3 |
# amv.pl |
# amv.pl |
4 |
# |
# |
5 |
# 07/19/07 19:21:39 CEST Dobrica Pavlinusic <dpavlin@rot13.org> |
# 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; |
use strict; |
12 |
|
|
17 |
|
|
18 |
open(my $fh, '<', $path) || die "can't open $path: $!"; |
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 { |
sub hex_dump { |
27 |
my $bytes = shift || return; |
my $bytes = shift || return; |
28 |
|
|
30 |
$ascii =~ s/\W/./gs; |
$ascii =~ s/\W/./gs; |
31 |
my $hex = unpack('h*', $bytes); |
my $hex = unpack('h*', $bytes); |
32 |
$hex =~ s/(..)/$1 /g; |
$hex =~ s/(..)/$1 /g; |
|
my $o = 0; |
|
33 |
# calculate number of characters for offset |
# calculate number of characters for offset |
34 |
my $d = length( sprintf("%x",length($bytes)) ); |
#my $d = length( sprintf("%x",length($bytes)) ); |
35 |
|
my $d = 4; |
36 |
|
my $prefix = '#.'; |
37 |
while ( $hex =~ s/^((?:\w\w\s){1,16})// ) { |
while ( $hex =~ s/^((?:\w\w\s){1,16})// ) { |
38 |
printf "## %0${d}x | %-48s| %s\n", $o, $1, substr( $ascii, 0, 16 ); |
printf "$prefix %0${d}x | %-48s| %s\n", $o, $1, substr( $ascii, 0, 16 ); |
39 |
|
$prefix = '##'; |
40 |
if ( length($ascii) >= 16 ) { |
if ( length($ascii) >= 16 ) { |
41 |
$ascii = substr( $ascii, 16 ); |
$ascii = substr( $ascii, 16 ); |
42 |
|
$o += 16; |
43 |
} else { |
} else { |
44 |
|
$o += length($ascii); |
45 |
last; |
last; |
46 |
} |
} |
|
$o += 16; |
|
47 |
} |
} |
48 |
} |
} |
49 |
|
|
58 |
|
|
59 |
hex_dump( $bytes ); |
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 ) { |
if ( $format ) { |
68 |
my @data = unpack($format, $bytes); |
my @data = unpack($format, $bytes); |
69 |
dump(@data); |
warn "## unpacked = ",dump(@data),"\n"; |
70 |
return @data; |
return @data; |
71 |
} else { |
} else { |
72 |
return $bytes; |
return $bytes; |
76 |
sub next_part { |
sub next_part { |
77 |
my ( $expected_part, $expected_len, $skip ) = @_; |
my ( $expected_part, $expected_len, $skip ) = @_; |
78 |
my ( $part, $len ) = x(8,'A4V'); |
my ( $part, $len ) = x(8,'A4V'); |
79 |
|
return unless $len; |
80 |
confess "not $expected_part but $part" if $expected_part ne $part; |
confess "not $expected_part but $part" if $expected_part ne $part; |
81 |
if ( $expected_len ) { |
if ( $expected_len ) { |
82 |
confess "expected $expected_len bytes for $part got $len" if $len != $expected_len; |
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; |
printf ">> %s - %d 0x%x bytes\n", $part, $len, $len; |
85 |
x($len) if $skip; |
x($len) if $skip; |
86 |
return $len; |
return $len; |
87 |
} |
} |
90 |
die "not RIFF but $riff" if $riff ne 'RIFF'; |
die "not RIFF but $riff" if $riff ne 'RIFF'; |
91 |
die "not AMV but $amv" if $amv ne 'AMV '; |
die "not AMV but $amv" if $amv ne 'AMV '; |
92 |
|
|
93 |
my $d; |
while ( ! defined($d->{eof}) ) { |
|
|
|
|
while ( 1 ) { |
|
94 |
my ( $list, $name ) = x(12,'A4x4A4'); |
my ( $list, $name ) = x(12,'A4x4A4'); |
95 |
die "not LIST but $list" if $list ne 'LIST'; |
die "not LIST but $list" if $list ne 'LIST'; |
96 |
print "> $list .. $name\n"; |
print "> $list .. $name\n"; |
98 |
if ( $name eq 'hdrl' ) { |
if ( $name eq 'hdrl' ) { |
99 |
|
|
100 |
my $len = next_part( 'amvh', hex(38) ); |
my $len = next_part( 'amvh', hex(38) ); |
|
|
|
|
print ">> $name $len\n"; |
|
101 |
|
|
|
my ( |
|
|
$ms_per_frame, |
|
|
$width, |
|
|
$height, |
|
|
$fps, |
|
|
$ss, |
|
|
$mm, |
|
|
$hh, |
|
|
) = |
|
102 |
my @names = ( qw/ms_per_frame width height fps ss mm hh/ ); |
my @names = ( qw/ms_per_frame width height fps ss mm hh/ ); |
103 |
my $h; |
my $h; |
104 |
map { |
map { |
119 |
next_part( 'strh', 0, 1 ); |
next_part( 'strh', 0, 1 ); |
120 |
next_part( 'strf', 0, 1 ); |
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 { |
} else { |
136 |
die "unknown $list $name"; |
die "unknown $list $name"; |
137 |
} |
} |