--- amv.pl 2007/08/19 11:45:39 28 +++ amv.pl 2007/10/02 19:12:07 36 @@ -46,11 +46,13 @@ my $path = shift @ARGV || die "usage: $0 movie.amv\n"; # by default, flip frames -#$jpegtran = '-flip vertical' unless defined($jpegtran); +$jpegtran = '-flip vertical' unless defined($jpegtran); rmtree $dump_dir if -e $dump_dir; mkpath $dump_dir || die "can't create $dump_dir: $!"; +$| = 1; + open(my $fh, '<', $path) || die "can't open $path: $!"; # offset in file @@ -121,16 +123,20 @@ } } +# my $len = next_part( 'boob' ); +# my ( $len, $part ) = next_part(); + sub next_part { my ( $expected_part, $expected_len, $skip ) = @_; my ( $part, $len ) = x(8,'A4V'); return unless $len; - confess "not $expected_part but $part" if $expected_part ne $part; + confess "not $expected_part but $part" if $expected_part && $expected_part ne $part; if ( $expected_len ) { confess "expected $expected_len bytes for $part got $len" if $len != $expected_len; } printf "## next_part %s - %d 0x%x bytes\n", $part, $len, $len if $debug; x($len) if $skip; + return ( $len, $part ) if wantarray; return $len; } @@ -157,69 +163,6 @@ return $out; } -sub mp3_frame { - my $frame = join('', - # Frame sync (all bits set) - 1 x 11 . - # MPEG Audio version ID - # 00 - MPEG Version 2.5 (unofficial) - # 01 - reserved - # 10 - MPEG Version 2 (ISO/IEC 13818-3) - # 11 - MPEG Version 1 (ISO/IEC 11172-3) - 1,0, - # Layer description - # 00 - reserved - # 01 - Layer III - # 10 - Layer II - # 11 - Layer I - 0,1, - # Protection bit - # 0 - Protected by CRC (16bit crc follows header) - # 1 - Not protected - 0, - # Bitrate index - 0,0,0,0, - # Sampling rate frequency index (22050) - 0,0, - # Padding bit - # 0 - frame is not padded - # 1 - frame is padded with one extra slot - 0, - # Private bit - 0, - # Channel Mode - # 00 - Stereo - # 01 - Joint stereo (Stereo) - # 10 - Dual channel (2 mono channels) - # 11 - Single channel (Mono) - 1,1, - # Mode extension (Only if Joint stereo) - 0,0, - # Copyright - 0, - # Original - 0, - # Emphasis - # 00 - none - # 01 - 50/15 ms - # 10 - reserved - # 11 - CCIT J.17 - 0,0, - ); - - die "frame must have 32 bits, not ", length($frame), " for $frame" if length($frame) != 32; - - my $bits = pack("b32", $frame); - - die "packed bits must be 4 bytes, not $bits" if length($bits) != 4; - - my $t = $frame; - $t =~ s/(.{8})/$1 /g; - warn "## mp3 frame frame = $t\n"; - - return $bits; -} - my @subframes; my $frame_nr = 1; @@ -456,7 +399,8 @@ return $pred_val; } -open(my $au_fh, '>', 'out.au') || die "can't open out.au: $!"; +my $au_path = "$dump_dir/sound.au"; +open(my $au_fh, '>', $au_path) || die "can't open $au_path: $!"; print $au_fh pack 'a4N5', ( # magic '.snd', @@ -477,71 +421,18 @@ my ( $origin, $index, $bytes ) = unpack 'ssL', substr($data,0,8); -warn "audio_frame origin $origin index $index bytes $bytes\n"; -hex_dump( substr($data,0,8) ); - $pred_val = $origin; $step_idx = $index; + my $size = 0; + foreach my $b ( map { ord($_) } split(//, substr($data,8)) ) { print $au_fh pack 'n', adpcm_decode_sample( $b >> 4 ); print $au_fh pack 'n', adpcm_decode_sample( $b & 15 ); + $size += 2; } -} - - -sub x_audio_frame { - my $data = shift || die "no data?"; - - my $apath = sprintf("$dump_dir/%04d.wav", $frame_nr ); - open(my $audio_fh, '>', $apath) || die "can't open audio file $apath: $!"; - - print $audio_fh pack 'a4Va4a4VvvVVv4', ( - # header 'RIFF', size - 'RIFF',-1, - # type: 'WAVE' - 'WAVE', - 'fmt ',0x14, - # format: DVI (IMA) ADPCM Wave Type - 0x11, - # channels - 1, - # samples/sec - 22050, - # avg. bytes/sec (for esimation) - 11567, - # block align (size of block) - 0x800, - # bits per sample (mono data) - 4, - # cbSize (ADPCM with 7 soefficient pairs) - 2, - # nSamplesPerBlock - # (((nBlockAlign - (7 * nChannels)) * 8) / (wBitsPerSample * nChannels)) + 2 - 0x03f9, - ); - - print $audio_fh pack 'a4VVa4V', ( - # time length of the data in samples - 'fact',4, - 220500, - # - 'data',-1, - ); - - my $riff_header_len = tell($audio_fh); - print $audio_fh $data; - - my $size = tell($audio_fh); - warn "## wav file $apath size: $size\n"; - - seek( $audio_fh, 4, 0 ); - print $audio_fh pack("V", $size - 8); - seek( $audio_fh, $riff_header_len - 4, 0 ); - print $audio_fh pack("V", $size - $riff_header_len); - - close($audio_fh) || die "can't close audio file $apath: $!"; + warn "length isn't corrent $bytes != $size" if $debug && $bytes != $size; } # @@ -552,6 +443,9 @@ die "$path not RIFF but $riff" if $riff ne 'RIFF'; die "$path not AMV but $amv" if $amv ne 'AMV '; +my $fps = 16; +my $duration; + while ( ! defined($d->{eof}) ) { my ( $list, $name ) = x(12,'A4x4A4'); die "not LIST but $list" if $list ne 'LIST'; @@ -569,12 +463,15 @@ $h->{$n} = $v; } x($len, 'Vx28VVVx8CCv'); - printf "## %s %d*%d %s fps (%d ms/frame) %02d:%02d:%02d\n", + $duration = sprintf('%02d:%02d:%02d', $h->{hh}, $h->{mm}, $h->{ss} ); + + printf "## %s %d*%d %s fps (%d ms/frame) %s\n", $path, $h->{width}, $h->{height}, $h->{fps}, $h->{ms_per_frame}, - $h->{hh}, $h->{mm}, $h->{ss}; + $duration; $d->{amvh} = $h; + $fps = $h->{fps}; } elsif ( $name eq 'strl' ) { @@ -583,36 +480,55 @@ } elsif ( $name eq 'movi' ) { - while (1) { + my $have_parts = 1; + + while ( $have_parts ) { my $frame = $d->{movi}++; - - my $len = next_part( '00dc' ); - last unless $len; - printf "<< %s 00dc - part %d jpeg %d 0x%x bytes\n", $name, $frame, $len, $len if $verbose; - mkjpg( x($len) ); - - $len = next_part( '01wb' ); - printf "<< %s 01wb - part %d audio %d 0x%x bytes\n", $name, $frame, $len, $len if $verbose; - - my $audio_frame = x( $len ); - - if ( $dump_audio ) { - printf "#### dumping audio frame %d 0x%x bytes\n", length($audio_frame), length($audio_frame); - hex_dump( $audio_frame ); - } - # remove 8 bytes of something -# $audio_frame = substr( $audio_frame, 8 ); + my $parts = 0; - if ( length($audio_frame) % 2 == 0 ) { - print "#### even sized frame!"; -# $audio_frame = substr( $audio_frame, 0, -1 ); - } + while ( $parts < 2 ) { + + my ( $len, $part ) = next_part(); + + if ( ! $len ) { + $have_parts = 0; + last; + } + + if ( $part eq '00dc' ) { -# print $audio_fh mp3_frame; - audio_frame( $audio_frame ); + printf "<< %s 00dc - part %d jpeg %d 0x%x bytes\n", $name, $frame, $len, $len if $verbose; + mkjpg( x($len) ); + $parts++; + + } elsif ( $part eq '01wb' ) { + printf "<< %s 01wb - part %d audio %d 0x%x bytes\n", $name, $frame, $len, $len if $verbose; + + my $audio_frame = x( $len ); + + if ( $dump_audio ) { + printf "#### dumping audio frame %d 0x%x bytes\n", length($audio_frame), length($audio_frame); + hex_dump( $audio_frame ); + } + + # print $audio_fh mp3_frame; + audio_frame( $audio_frame ); + + $parts++; + } else { + warn "unknown next part $part with $len bytes, skipping!"; + } + + warn "## #$frame_nr $name $part has $parts parts\n" if $debug; + } $frame_nr++; + + if ( $frame_nr % $fps == 0 ) { + print "\n" if ( ( $frame_nr / $fps ) % 60 == 0 ); + print "."; + } }; } else { @@ -620,7 +536,13 @@ } } -my $cmd = "ffmpeg -i $dump_dir/%04d.jpg -r 16 -y $dump_avi"; +if ( $fps == 12 ) { + warn "fixup $au_path for $fps fps -- 16000 Hz\n"; + seek($au_fh, 16, 0); # sample rate offset + print $au_fh pack 'N', 16000; +} + +my $cmd = "ffmpeg -r $fps -i $dump_dir/%04d.jpg -i $au_path -y $dump_avi"; system($cmd) == 0 || die "can't convert frames to avi using $cmd: $!"; print ">>>> created $frame_nr frames $dump_avi ", -s $dump_avi, "\n";