--- amv.pl 2007/07/21 11:05:43 15 +++ amv.pl 2007/10/01 20:12:50 32 @@ -9,6 +9,8 @@ # http://en.wikipedia.org/wiki/RIFF_(File_format) # 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; @@ -17,22 +19,40 @@ use File::Path; use Getopt::Long; -my $dump = 0; +my $dump_amv = 0; +my $dump_video = 0; +my $dump_jpeg = 0; +my $dump_audio = 0; my $debug = 0; +my $verbose = 0; my $dump_dir = '/tmp/dump/'; +my $dump_avi = "dump.avi"; +my $no_jpeg_header = 0; +my $jpeg_q = 100; +my $jpegtran; GetOptions( - "dump!" => \$dump, + "dump-amv!" => \$dump_amv, + "dump-video!" => \$dump_video, + "dump-jpeg!" => \$dump_jpeg, + "dump-audio!" => \$dump_audio, "debug!" => \$debug, "dump-dir=s" => \$dump_dir, + "no-jpeg-headers!" => \$no_jpeg_header, + "jpegtran=s" => \$jpegtran, + "verbose!" => \$verbose, ); my $path = shift @ARGV || die "usage: $0 movie.amv\n"; +# by default, flip frames +$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 @@ -42,8 +62,6 @@ my $d; sub hex_dump { - return unless $dump; - my ( $bytes, $offset ) = @_; return unless $bytes; @@ -85,281 +103,210 @@ my $r_len = length($bytes); confess "read $r_len bytes, expected $len" if $len != $r_len; - hex_dump( $bytes ); + if ( $dump_amv ) { + print "## raw $len bytes\n"; + hex_dump( $bytes ); + } if ( $bytes eq 'AMV_END_' ) { - warn "> end of file marker AMV_END_\n"; + print "> end of file marker AMV_END_\n" if $dump_video; $d->{eof}++; return; } if ( $format ) { my @data = unpack($format, $bytes); - warn "## unpacked = ",dump(@data),"\n" if $debug; + print "## unpacked = ",dump(@data),"\n" if $debug; return @data; } else { return $bytes; } } +# 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 "<< %s - %d 0x%x bytes\n", $part, $len, $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; } -sub huffman { +sub quality { + my @table = @_; + die "quantization matrice needs to have 64 bytes!" if $#table != 63; + + my $in = join('', map { chr($_) } @table ); + my $out; + + foreach my $t ( @table ) { + $t = int( ( $t * $jpeg_q ) / 100 ); + $t = 255 if $t > 255; + $out .= chr($t); + } -# JPEG DHT Segment for YCrCb omitted from MJPG data -return -"\xFF\xC4\x01\xA2" . -"\x00\x00\x01\x05\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00" . -"\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x01" . -"\x00\x03\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00" . -"\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x10\x00" . -"\x02\x01\x03\x03\x02\x04\x03\x05\x05\x04\x04\x00\x00\x01\x7D" . -"\x01\x02\x03\x00\x04\x11\x05\x12\x21\x31\x41\x06\x13\x51\x61" . -"\x07\x22\x71\x14\x32\x81\x91\xA1\x08\x23\x42\xB1\xC1\x15\x52" . -"\xD1\xF0\x24\x33\x62\x72\x82\x09\x0A\x16\x17\x18\x19\x1A\x25" . -"\x26\x27\x28\x29\x2A\x34\x35\x36\x37\x38\x39\x3A\x43\x44\x45" . -"\x46\x47\x48\x49\x4A\x53\x54\x55\x56\x57\x58\x59\x5A\x63\x64" . -"\x65\x66\x67\x68\x69\x6A\x73\x74\x75\x76\x77\x78\x79\x7A\x83" . -"\x84\x85\x86\x87\x88\x89\x8A\x92\x93\x94\x95\x96\x97\x98\x99" . -"\x9A\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xB2\xB3\xB4\xB5\xB6" . -"\xB7\xB8\xB9\xBA\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xD2\xD3" . -"\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8" . -"\xE9\xEA\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\x11\x00\x02" . -"\x01\x02\x04\x04\x03\x04\x07\x05\x04\x04\x00\x01\x02\x77\x00" . -"\x01\x02\x03\x11\x04\x05\x21\x31\x06\x12\x41\x51\x07\x61\x71" . -"\x13\x22\x32\x81\x08\x14\x42\x91\xA1\xB1\xC1\x09\x23\x33\x52" . -"\xF0\x15\x62\x72\xD1\x0A\x16\x24\x34\xE1\x25\xF1\x17\x18\x19" . -"\x1A\x26\x27\x28\x29\x2A\x35\x36\x37\x38\x39\x3A\x43\x44\x45" . -"\x46\x47\x48\x49\x4A\x53\x54\x55\x56\x57\x58\x59\x5A\x63\x64" . -"\x65\x66\x67\x68\x69\x6A\x73\x74\x75\x76\x77\x78\x79\x7A\x82" . -"\x83\x84\x85\x86\x87\x88\x89\x8A\x92\x93\x94\x95\x96\x97\x98" . -"\x99\x9A\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xB2\xB3\xB4\xB5" . -"\xB6\xB7\xB8\xB9\xBA\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xD2" . -"\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xE2\xE3\xE4\xE5\xE6\xE7\xE8" . -"\xE9\xEA\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA"; + if ( $dump_video ) { + print "## quantization table original\n"; + hex_dump( $in ); + print "## quantization table for $jpeg_q %\n"; + hex_dump( $out ); + } + return $out; } +=for obsolete + +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; +} + +=cut + +my @subframes; +my $frame_nr = 1; + +# how many subframes to join into single frame? +my $join_subframes = 0; + sub mkjpg { - my ($path,$data) = @_; - open(my $fh, '>', $path) || die "can't create $path: $!"; + my ($data) = @_; confess "no SOI marker in data" if substr($data,0,2) ne "\xFF\xD8"; - $data = substr($data,2); + confess "no EOI marker in data" if substr($data,-2,2) ne "\xFF\xD9"; + $data = substr($data,2,-2); + + if ( $#subframes < ( $join_subframes - 1 ) ) { + push @subframes, $data; + print "## saved $frame_nr/", $#subframes + 1, " subframe of ", length($data), " bytes\n" if $debug; + return; + } + + my $w = $d->{amvh}->{width} || die "no width?"; + my $h = $d->{amvh}->{height} || confess "no height?"; my $header = - "\xFF\xD8". # Start of Image (SOI) marker -#------------------------------------------------------------------ - "\xFF\xE0". # JFIF marker + # Start of Image (SOI) marker + "\xFF\xD8". + # JFIF marker + "\xFF\xE0". pack("nZ5CCCnnCC", 16, # length - 'JFIF', # identifier + 'JFIF', # identifier (JFIF) 1,1, # version 0, # units (none) 1,1, # X,Y density 0,0, # X,Y thumbnail ). -#------------------------------------------------------------------ - "\xFF\xC0". # Start of frame + "\xFF\xFE". + "\x00\x3CCREATOR: amv dumper (compat. IJG JPEG v62), quality = 100\n". + # quantization table (quaility=100%) + "\xFF\xDB". + "\x00\x43". + # 8 bit values, table 1 + "\x00". + quality( + 0x10, 0x0B, 0x0C, 0x0E, 0x0C, 0x0A, 0x10, 0x0E, + 0x0D, 0x0E, 0x12, 0x11, 0x10, 0x13, 0x18, 0x28, + 0x1A, 0x18, 0x16, 0x16, 0x18, 0x31, 0x23, 0x25, + 0x1D, 0x28, 0x3A, 0x33, 0x3D, 0x3C, 0x39, 0x33, + 0x38, 0x37, 0x40, 0x48, 0x5C, 0x4E, 0x40, 0x44, + 0x57, 0x45, 0x37, 0x38, 0x50, 0x6D, 0x51, 0x57, + 0x5F, 0x62, 0x67, 0x68, 0x67, 0x3E, 0x4D, 0x71, + 0x79, 0x70, 0x64, 0x78, 0x5C, 0x65, 0x67, 0x63, + ). + "\xFF\xDB". + "\x00\x43". + # 8 bit values, table 1 + "\x01". + quality( + 0x11, 0x12, 0x12, 0x18, 0x15, 0x18, 0x2F, 0x1A, + 0x1A, 0x2F, 0x63, 0x42, 0x38, 0x42, 0x63, 0x63, + 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, + 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, + 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, + 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, + 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, + 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, + ). + # start of frame + "\xFF\xC0". pack("ncnncc9", 17, # len 8, # sample precision in bits - 120,160, # X,Y size + $h,$w, # X,Y size 3, # number of components - 1,0x21,0, # Component ID, H+V sampling factors, Quantization table number + 1,0x22,0, # Component ID, H+V sampling factors, Quantization table number 2,0x11,1, 3,0x11,1, ). -#------------------------------------------------------------------ - "\xFF\xDB". # Define Quantization table marker - "\x00\x84". # len - "\x00". # 8 bit values, (byte) table 0 - "\x10\x0B\x0C\x0E\x0C". - "\x0A\x10\x0E\x0D\x0E". - "\x12\x11\x10\x13\x18". - "\x28\x1A\x18\x16\x16". - "\x18\x31\x23\x25\x1D". - "\x28\x3A\x33\x3D\x3C". - "\x39\x33\x38\x37\x40". - "\x48\x5C\x4E\x40\x44". - "\x57\x45\x37\x38\x50". - "\x6D\x51\x57\x5F\x62". - "\x67\x68\x67\x3E\x4D". - "\x71\x79\x70\x64\x78". - "\x5C\x65\x67\x63". - "\x01". # 8 bit values, (byte) table 1 - "\x11\x12\x12\x18\x15". - "\x18\x2F\x1A\x1A\x2F". - "\x63\x42\x38\x42\x63". - "\x63\x63\x63\x63\x63". - "\x63\x63\x63\x63\x63". - "\x63\x63\x63\x63\x63". - "\x63\x63\x63\x63\x63". - "\x63\x63\x63\x63\x63". - "\x63\x63\x63\x63\x63". - "\x63\x63\x63\x63\x63". - "\x63\x63\x63\x63\x63". - "\x63\x63\x63\x63\x63". - "\x63\x63\x63\x63". -#------------------------------------------------------------------ -# huffman("\x00"). # 0 DC -# huffman("\x01"). # 1 DC -# huffman("\x10"). # 0 AC -# huffman("\x11"). # 1 AC -#------------------------------------------------------------------ - # Define huffman table (section B.2.4.1) - "\xFF\xC4". # Marker - "\x00\x1F". # Length (31 bytes) - "\x00". # DC, (byte) table 0 - "\x00\x01\x05\x01\x01". - "\x01\x01\x01\x01\x00". - "\x00\x00\x00\x00\x00". - "\x00\x00\x01\x02\x03". - "\x04\x05\x06\x07\x08". - "\x09\x0A\x0B". - # Define huffman table (section B.2.4.1) - "\xFF\xC4". # Marker - "\x00\xB5". # Length (181 bytes) - "\x10". # AC, (byte) table 0 - "\x00\x02\x01\x03\x03". - "\x02\x04\x03\x05\x05". - "\x04\x04\x00\x00\x01". - "\x7D\x01\x02\x03\x00". - "\x04\x11\x05\x12\x21". - "\x31\x41\x06\x13\x51". - "\x61\x07\x22\x71\x14". - "\x32\x81\x91\xA1\x08". - "\x23\x42\xB1\xC1\x15". - "\x52\xD1\xF0\x24\x33". - "\x62\x72\x82\x09\x0A". - "\x16\x17\x18\x19\x1A". - "\x25\x26\x27\x28\x29". - "\x2A\x34\x35\x36\x37". - "\x38\x39\x3A\x43\x44". - "\x45\x46\x47\x48\x49". - "\x4A\x53\x54\x55\x56". - "\x57\x58\x59\x5A\x63". - "\x64\x65\x66\x67\x68". - "\x69\x6A\x73\x74\x75". - "\x76\x77\x78\x79\x7A". - "\x83\x84\x85\x86\x87". - "\x88\x89\x8A\x92\x93". - "\x94\x95\x96\x97\x98". - "\x99\x9A\xA2\xA3\xA4". - "\xA5\xA6\xA7\xA8\xA9". - "\xAA\xB2\xB3\xB4\xB5". - "\xB6\xB7\xB8\xB9\xBA". - "\xC2\xC3\xC4\xC5\xC6". - "\xC7\xC8\xC9\xCA\xD2". - "\xD3\xD4\xD5\xD6\xD7". - "\xD8\xD9\xDA\xE1\xE2". - "\xE3\xE4\xE5\xE6\xE7". - "\xE8\xE9\xEA\xF1\xF2". - "\xF3\xF4\xF5\xF6\xF7". - "\xF8\xF9\xFA". - # Define huffman table (section B.2.4.1) - "\xFF\xC4". # Marker - "\x00\x1F". # Length (31 bytes) - "\x01". # DC, (byte) table 1 - "\x00\x03\x01\x01\x01". - "\x01\x01\x01\x01\x01". - "\x01\x00\x00\x00\x00". - "\x00\x00\x01\x02\x03". - "\x04\x05\x06\x07\x08". - "\x09\x0A\x0B". - # Define huffman table (section B.2.4.1) - "\xFF\xC4". # Marker - "\x00\xB5". # Length (181 bytes) - "\x11". # AC, (byte) table 1 - "\x00\x02\x01\x02\x04". - "\x04\x03\x04\x07\x05". - "\x04\x04\x00\x01\x02". - "\x77\x00\x01\x02\x03". - "\x11\x04\x05\x21\x31". - "\x06\x12\x41\x51\x07". - "\x61\x71\x13\x22\x32". - "\x81\x08\x14\x42\x91". - "\xA1\xB1\xC1\x09\x23". - "\x33\x52\xF0\x15\x62". - "\x72\xD1\x0A\x16\x24". - "\x34\xE1\x25\xF1\x17". - "\x18\x19\x1A\x26\x27". - "\x28\x29\x2A\x35\x36". - "\x37\x38\x39\x3A\x43". - "\x44\x45\x46\x47\x48". - "\x49\x4A\x53\x54\x55". - "\x56\x57\x58\x59\x5A". - "\x63\x64\x65\x66\x67". - "\x68\x69\x6A\x73\x74". - "\x75\x76\x77\x78\x79". - "\x7A\x82\x83\x84\x85". - "\x86\x87\x88\x89\x8A". - "\x92\x93\x94\x95\x96". - "\x97\x98\x99\x9A\xA2". - "\xA3\xA4\xA5\xA6\xA7". - "\xA8\xA9\xAA\xB2\xB3". - "\xB4\xB5\xB6\xB7\xB8". - "\xB9\xBA\xC2\xC3\xC4". - "\xC5\xC6\xC7\xC8\xC9". - "\xCA\xD2\xD3\xD4\xD5". - "\xD6\xD7\xD8\xD9\xDA". - "\xE2\xE3\xE4\xE5\xE6". - "\xE7\xE8\xE9\xEA\xF2". - "\xF3\xF4\xF5\xF6\xF7". - "\xF8\xF9\xFA". -#------------------------------------------------------------------ - "\xFF\xDA". # Start of Scan marker - pack("nC11", - 12, # length - 3, # number of components - 1,0x00, # Scan 1: use DC/AC huff tables 0/0 - 2,0x11, # Scan 2: use DC/AC huff tables 1/1 - 3,0x11, # Scan 3: use DC/AC huff tables 1/1 - 0,0x3f, # Ss, Se - 0, # Ah, Ai (not used) - ); -#------------------------------------------------------------------ - - my $header = - # SOI - "\xFF\xD8". - # JFIF - "\xFF\xE0". - "\x00\x0e\x41\x56\x49\x31\x00\x00\x00\x00\x00\x00\x00\x00". - "\xff\xDD\x00\x04\x00\x00". - # quantization table - "\xFF\xDB". - "\x00\x43\x00\x21\x16\x18". - "\x1D\x18\x14\x21\x1D\x1B\x1D\x25". - "\x23\x21\x27\x31\x53\x36\x31\x2D". - "\x2D\x31\x65\x48\x4c\x3c\x53\x78". - "\x6a\x7e\x7c\x76\x6a\x74\x72\x85". - "\x95\xbf\xa2\x85\x8d\xb4\x8f\x72". - "\x74\xa6\xe2\xa8\xb4\xc5\xcb\xd6". - "\xd8\xd6\x80\xa0\xeb\xfb\xe8\xd0". - "\xf9\xbf\xd2\xd6\xcd". - # quantization table - "\xff\xdb". - "\x00\x43\x01\x23\x25\x25\x31\x2b\x31". - "\x61\x36\x36\x61\xcd\x89\x74\x89". - "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd". - "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd". - "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd". - "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd". - "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd". - "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd". - "\xcd\xcd". # Define huffman table (section B.2.4.1) "\xFF\xC4". # Marker "\x00\x1F". # Length (31 bytes) @@ -368,13 +315,6 @@ "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B". # Define huffman table (section B.2.4.1) "\xFF\xC4". # Marker - "\x00\x1F". # Length (31 bytes) - "\x01". # DC chrominance, table 1 - "\x00\x03\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00". - "\x00\x00\x00\x00". - "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B". - # Define huffman table (section B.2.4.1) - "\xFF\xC4". # Marker "\x00\xB5". # Length (181 bytes) "\x10". # AC luminance, table 0 "\x00\x02\x01\x03\x03\x02\x04\x03\x05\x05\x04\x04\x00\x00\x01\x7D". @@ -392,6 +332,13 @@ "\xC6\xC7\xC8\xC9\xCA\xD2\xD3\xD4\xD5\xD6\xD7\xD8". "\xD9\xDA\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA". "\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA". + # Define huffman table (section B.2.4.1) + "\xFF\xC4". # Marker + "\x00\x1F". # Length (31 bytes) + "\x01". # DC chrominance, table 1 + "\x00\x03\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00". + "\x00\x00\x00\x00". + "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B". #/* Define huffman table (section B.2.4.1) */ "\xFF\xC4". # Marker "\x00\xB5". # Length (181 bytes) @@ -412,35 +359,219 @@ "\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xD2\xD3\xD4\xD5\xD6". "\xD7\xD8\xD9\xDA\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9". "\xEA\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA". - # start of frame - "\xff\xc0". - "\x00\x11\x08". - pack("nn", 120, 160). - "\x03\x01\x21\x00\x02\x11\x01\x03\x11\x01". - # - "\xff\xda". - "\x00\x0c\x03\x01\x00\x02\x11\x03\x11". - "\x00\x3f\x00"; + # Start of Scan marker + "\xFF\xDA". + pack("nC10", + 12, # length + 3, # number of components + 1,0x00, # Scan 1: use DC/AC huff tables 0/0 + 2,0x11, # Scan 2: use DC/AC huff tables 1/1 + 3,0x11, # Scan 3: use DC/AC huff tables 1/1 + 0,0x3f, # Ss, Se + 0, # Ah, Ai (not used) + ); - if ( $dump ) { - warn "## created JPEG header...\n"; + if ( $dump_jpeg ) { + print "## created JPEG header...\n"; hex_dump( $header, 0 ); } - print $fh $header || die "can't write header into $path: $!"; - print $fh $data || die "can't write frame into $path: $!"; + my $frame = join('', @subframes ) . $data; + @subframes = (); + + my $path = sprintf("$dump_dir/%04d.jpg", $frame_nr ); + + my $fh; + if ( $jpegtran ) { + open($fh, '|-', "jpegtran $jpegtran > $path") || die "can't create $path: $!"; + } else { + open($fh, '>', $path) || die "can't create $path: $!"; + } + + if ( ! $no_jpeg_header ) { + print $fh $header . $frame . "\xFF\xD9" || die "can't write jpeg $path: $!"; + } else { + print $fh $frame || die "can't write raw jpeg $path: $!"; + } close $fh || die "can't close $path: $!"; - print ">> created $path ", -s $path, " bytes\n"; + print ">> created $frame_nr ", $no_jpeg_header ? 'raw' : '', " jpeg $path ", -s $path, " bytes\n" if $verbose; +} + +# +# 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 $pred_val = 0; +my $step_idx = 0; + +# 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, + #channels + 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; + + 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'); die "not LIST but $list" if $list ne 'LIST'; - print "< $list * $name\n"; + print "< $list * $name\n" if $verbose; if ( $name eq 'hdrl' ) { @@ -454,12 +585,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' ) { @@ -468,19 +602,63 @@ } 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 - frame %d jpeg %d 0x%x bytes\n", $name, $frame, $len, $len; - mkjpg( sprintf("$dump_dir/%03d.jpg", $frame ), x($len) ); - $len = next_part( '01wb', 0, 1 ); - printf "<< %s 01wb - frame %d audio %d 0x%x bytes\n", $name, $frame, $len, $len; + my $parts = 0; + + while ( $parts < 2 ) { + + my ( $len, $part ) = next_part(); + + if ( ! $len ) { + $have_parts = 0; + last; + } + + if ( $part eq '00dc' ) { + + 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 { die "unknown $list $name"; } } + +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";