Revision 36 (by dpavlin, 2007/10/02 19:12:07) warn about length mismatch only with debug
#!/usr/bin/perl -w

# amv.pl
#
# 07/19/07 19:21:39 CEST Dobrica Pavlinusic <dpavlin@rot13.org>
#
# Various useful links used to produce this:
# http://www.moviecodec.com/topics/15431p1.html
# 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;

use Data::Dump qw/dump/;
use Carp qw/confess/;
use File::Path;
use Getopt::Long;

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-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
my $o = 0;

# shared data hash
my $d;

sub hex_dump {
	my ( $bytes, $offset ) = @_;
	return unless $bytes;

	my $old_o;
	if (defined($offset)) {
		$old_o = $o;
		$o = $offset;
	}

	my $ascii = $bytes;
	$ascii =~ s/\W/./gs;
	my $hex = uc( unpack('h*', $bytes) );
	$hex =~ s/(..)/$1 /g;
	# calculate number of characters for offset
	#my $d = length( sprintf("%x",length($bytes)) );
	my $d = 4;
	my $prefix = '#.';
	while ( $hex =~ s/^((?:\w\w\s){1,16})// ) {
		printf "$prefix %0${d}x | %-48s| %s\n", $o, $1, substr( $ascii, 0, 16 );
		$prefix = '##';
		if ( length($ascii) >= 16 ) {
			$ascii = substr( $ascii, 16 );
			$o += 16;
		} else {
			$o += length($ascii);
			last;
		}
	}

	$o = $old_o if $old_o;
}

sub x {
	my ($len,$format) = @_;

	my $bytes;
	read($fh, $bytes, $len);

	my $r_len = length($bytes);
	confess "read $r_len bytes, expected $len" if $len != $r_len;

	if ( $dump_amv ) {
		print "## raw $len bytes\n";
		hex_dump( $bytes );
	}

	if ( $bytes eq 'AMV_END_' ) {
		print "> end of file marker AMV_END_\n" if $dump_video;
		$d->{eof}++;
		return;
	}

	if ( $format ) {
		my @data = unpack($format, $bytes);
		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 && $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;
}

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);
	}

	if ( $dump_video ) {
		print "## quantization table original\n";
		hex_dump( $in );
		print "## quantization table for $jpeg_q %\n";
		hex_dump( $out );
	}

	return $out;
}

my @subframes;
my $frame_nr = 1;

# how many subframes to join into single frame?
my $join_subframes = 0;

sub mkjpg {
	my ($data) = @_;

	confess "no SOI marker in data" if substr($data,0,2) ne "\xFF\xD8";
	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 = 
	# Start of Image (SOI) marker
	"\xFF\xD8".
	# JFIF marker
	"\xFF\xE0".
	pack("nZ5CCCnnCC",
		16,			# length
		'JFIF',		# identifier (JFIF)
		1,1,		# version
		0,			# units (none)
		1,1,		# X,Y density
		0,0,		# X,Y thumbnail
	).
	"\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
		$h,$w,		# X,Y size
		3,			# number of components
		1,0x22,0,	# Component ID, H+V sampling factors, Quantization table number
		2,0x11,1,
		3,0x11,1,
	).
	# Define huffman table (section B.2.4.1)
	"\xFF\xC4".	# Marker
	"\x00\x1F".	# Length (31 bytes)
	"\x00".		# DC luminance, 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 luminance, 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 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)
	"\x11".		# AC chrominance, 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".
	# 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_jpeg ) {
		print "## created JPEG header...\n";
		hex_dump( $header, 0 );
	}

	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 $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 $debug && $bytes != $size;
}

#
# 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" if $verbose;

	if ( $name eq 'hdrl' ) {

		my $len = next_part( 'amvh', hex(38) );

		my @names = ( qw/ms_per_frame width height fps ss mm hh/ );
		my $h;
		map {
			my $v = $_;
			my $n = shift @names || die "no more names?";
			$h->{$n} = $v;
		} x($len, 'Vx28VVVx8CCv');

		$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},
			$duration;

		$d->{amvh} = $h;
		$fps = $h->{fps};

	} elsif ( $name eq 'strl' ) {

		next_part( 'strh', 0, 1 );
		next_part( 'strf', 0, 1 );

	} elsif ( $name eq 'movi' ) {

		my $have_parts = 1;

		while ( $have_parts ) {
			my $frame = $d->{movi}++;

			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";
	}
}

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";