--- amv.pl 2007/08/18 11:20:25 25 +++ amv.pl 2007/09/14 19:35:32 30 @@ -10,6 +10,7 @@ # http://www.obrador.com/essentialjpeg/HeaderInfo.htm # http://lists.helixcommunity.org/pipermail/datatype-dev/2005-January/001886.html # http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm +# http://wiki.multimedia.cx/index.php?title=IMA_ADPCM use strict; @@ -45,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 @@ -386,47 +389,176 @@ print ">> created $frame_nr ", $no_jpeg_header ? 'raw' : '', " jpeg $path ", -s $path, " bytes\n" if $verbose; } -my ( $riff, $amv ) = x(12, 'Z4x4Z4'); -die "$path not RIFF but $riff" if $riff ne 'RIFF'; -die "$path not AMV but $amv" if $amv ne 'AMV '; +# +# IMA ADPCM decoder +# + +my @index_adjust = ( -1, -1, -1, -1, 2, 4, 6, 8 ); + +my @step_size = ( + 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, + 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, + 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, + 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, + 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, + 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, + 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, + 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, + 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 +); -my $apath = "$dump_dir/audio.wav"; -open(my $audio_fh, '>', $apath) || die "can't open audio file $apath: $!"; +my $pred_val = 0; +my $step_idx = 0; -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 +# This code is "borrowed" from the ALSA library +# http://www.alsa-project.org + +sub adpcm_decode_sample { + my $code = shift; + + my $pred_diff; # Predicted difference to next sample + my $step; # holds previous step_size value + + # Separate sign and magnitude + my $sign = $code & 0x8; + $code &= 0x7; + + # Computes pred_diff = (code + 0.5) * step / 4, + # but see comment in adpcm_coder. + + $step = $step_size[$step_idx] || die "no step_size[$step_idx]"; + + # Compute difference and new predicted value + $pred_diff = $step >> 3; + my $i = 0x4; + while( $i ) { + if ($code & $i) { + $pred_diff += $step; + } + $i >>= 1; + $step >>= 1; + } + $pred_val += $sign ? -$pred_diff : $pred_diff; + + # Clamp output value + if ($pred_val > 32767) { + $pred_val = 32767; + } elsif ($pred_val < -32768) { + $pred_val = -32768; + } + + # Find new step_size index value + $step_idx += $index_adjust[$code]; + + if ($step_idx < 0) { + $step_idx = 0; + } elsif ($step_idx > 88) { + $step_idx = 88; + } + return $pred_val; +} + +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', + # data offset + 24, + # data size + -1, + # encoding - 16-bit linear PCM + 3, + # sample rate 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 - 0x0ff9, + #channels + 1, ); -print $audio_fh pack 'a4VVa4V', ( - # time length of the data in samples - 'fact',4, - 220500, - # - 'data',-1, -); +sub audio_frame { + my $data = shift || die "no data?"; + + my ( $origin, $index, $bytes ) = unpack 'ssL', substr($data,0,8); + + $pred_val = $origin; + $step_idx = $index; + + my $size = 0; -my $riff_header_len = tell($audio_fh); + 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; + } + + warn "length isn't corrent $bytes != $size" if $bytes != $size; +} + + +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: $!"; +} + +# +# read AMV file +# + +my ( $riff, $amv ) = x(12, 'Z4x4Z4'); +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'); @@ -445,12 +577,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' ) { @@ -477,18 +612,15 @@ hex_dump( $audio_frame ); } - # remove 8 bytes of something - $audio_frame = substr( $audio_frame, 8 ); - - if ( length($audio_frame) % 2 == 0 ) { - print "#### even sized frame!"; -# $audio_frame = substr( $audio_frame, 0, -1 ); - } - # print $audio_fh mp3_frame; - print $audio_fh $audio_frame || die "can't write audio frame in $apath: $!"; + audio_frame( $audio_frame ); $frame_nr++; + + if ( $frame_nr % $fps == 0 ) { + print "\n" if ( ( $frame_nr / $fps ) % 60 == 0 ); + print "."; + } }; } else { @@ -496,17 +628,7 @@ } } -my $cmd = "ffmpeg -i $dump_dir/%04d.jpg -r 16 -y $dump_avi"; +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: $!"; -my $size = tell($audio_fh); -warn "## wav file 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: $!"; - -print ">>>> created $frame_nr frames $dump_avi ", -s $dump_avi, " and $apath ", -s $apath, "\n"; +print ">>>> created $frame_nr frames $dump_avi ", -s $dump_avi, "\n";