--- amv.pl 2007/08/18 11:21:40 27 +++ amv.pl 2007/08/19 11:45:39 28 @@ -387,47 +387,170 @@ 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; +} + +open(my $au_fh, '>', 'out.au') || die "can't open out.au: $!"; +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); -my $riff_header_len = tell($audio_fh); +warn "audio_frame origin $origin index $index bytes $bytes\n"; +hex_dump( substr($data,0,8) ); + + $pred_val = $origin; + $step_idx = $index; + + 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 ); + } +} + + +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 '; while ( ! defined($d->{eof}) ) { my ( $list, $name ) = x(12,'A4x4A4'); @@ -479,7 +602,7 @@ } # remove 8 bytes of something - $audio_frame = substr( $audio_frame, 8 ); +# $audio_frame = substr( $audio_frame, 8 ); if ( length($audio_frame) % 2 == 0 ) { print "#### even sized frame!"; @@ -487,7 +610,7 @@ } # print $audio_fh mp3_frame; - print $audio_fh $audio_frame || die "can't write audio frame in $apath: $!"; + audio_frame( $audio_frame ); $frame_nr++; }; @@ -500,14 +623,4 @@ my $cmd = "ffmpeg -i $dump_dir/%04d.jpg -r 16 -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";