2012-08-30 14:35:12 +02:00
|
|
|
#include "../vgmstream.h"
|
|
|
|
#include "meta.h"
|
|
|
|
|
2013-06-14 02:42:24 +02:00
|
|
|
#if defined(VGM_USE_MP4V2) && defined(VGM_USE_FDKAAC)
|
2017-02-12 14:42:02 +01:00
|
|
|
/* AKB (AAC only) - found in SQEX iOS games */
|
2012-08-30 14:35:12 +02:00
|
|
|
VGMSTREAM * init_vgmstream_akb(STREAMFILE *streamFile) {
|
|
|
|
VGMSTREAM * vgmstream = NULL;
|
|
|
|
|
|
|
|
size_t filesize;
|
|
|
|
uint32_t loop_start, loop_end;
|
|
|
|
|
|
|
|
if ((uint32_t)read_32bitBE(0, streamFile) != 0x414b4220) goto fail;
|
|
|
|
|
|
|
|
loop_start = read_32bitLE(0x14, streamFile);
|
|
|
|
loop_end = read_32bitLE(0x18, streamFile);
|
|
|
|
|
|
|
|
filesize = get_streamfile_size( streamFile );
|
|
|
|
|
|
|
|
vgmstream = init_vgmstream_mp4_aac_offset( streamFile, 0x20, filesize - 0x20 );
|
|
|
|
if ( !vgmstream ) goto fail;
|
|
|
|
|
|
|
|
if ( loop_start || loop_end ) {
|
|
|
|
vgmstream->loop_flag = 1;
|
|
|
|
vgmstream->loop_start_sample = loop_start;
|
|
|
|
vgmstream->loop_end_sample = loop_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return NULL;
|
2013-06-14 02:42:24 +02:00
|
|
|
}
|
|
|
|
#endif
|
2017-02-12 14:42:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* AKB - found in SQEX iOS games */
|
|
|
|
VGMSTREAM * init_vgmstream_akb_multi(STREAMFILE *streamFile) {
|
|
|
|
VGMSTREAM * vgmstream = NULL;
|
2017-04-28 20:10:18 +02:00
|
|
|
off_t start_offset, extra_data_offset = 0;
|
|
|
|
size_t file_size, header_size, extra_header_size = 0, extra_data_size = 0;
|
2017-02-12 14:42:02 +01:00
|
|
|
int loop_flag = 0, channel_count, codec;
|
|
|
|
|
|
|
|
/* check extensions */
|
2017-04-28 20:10:18 +02:00
|
|
|
/* .akb.bytes is the usual extension in later games */
|
2017-02-12 14:42:02 +01:00
|
|
|
if ( !check_extensions(streamFile, "akb") )
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* check header */
|
|
|
|
if (read_32bitBE(0x00,streamFile) != 0x414B4220) /* "AKB " */
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
channel_count = read_8bit(0x0d,streamFile);
|
|
|
|
loop_flag = read_32bitLE(0x18,streamFile) > 0;
|
|
|
|
|
|
|
|
/* build the VGMSTREAM */
|
|
|
|
vgmstream = allocate_vgmstream(channel_count,loop_flag);
|
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
2017-04-28 20:10:18 +02:00
|
|
|
/* 0x04 (2): version (iPad/IPhone?) */
|
|
|
|
header_size = read_16bitLE(0x06,streamFile);
|
|
|
|
file_size = read_32bitLE(0x08,streamFile);
|
2017-02-12 14:42:02 +01:00
|
|
|
codec = read_8bit(0x0c,streamFile);
|
|
|
|
vgmstream->sample_rate = (uint16_t)read_16bitLE(0x0e,streamFile);
|
|
|
|
vgmstream->num_samples = read_32bitLE(0x10,streamFile);
|
|
|
|
vgmstream->loop_start_sample = read_32bitLE(0x14,streamFile);
|
2017-04-28 20:10:18 +02:00
|
|
|
vgmstream->loop_end_sample = read_32bitLE(0x18,streamFile);
|
2017-02-12 14:42:02 +01:00
|
|
|
vgmstream->meta_type = meta_AKB;
|
|
|
|
|
2017-04-28 20:10:18 +02:00
|
|
|
/* should be ok but not exact (probably more complex, see AKB2) */
|
|
|
|
if ( header_size >= 0x44) { /* v2 */
|
|
|
|
/* 0x10+: config? (pan, volume), 0x24: file_id? */
|
|
|
|
extra_data_size = read_16bitLE(0x1c,streamFile);
|
|
|
|
extra_header_size = read_16bitLE(0x28,streamFile);
|
|
|
|
extra_data_offset = header_size + extra_header_size;
|
|
|
|
start_offset = extra_data_offset + extra_data_size;
|
|
|
|
/* ~num_samples at extra_data_offset + 0x04/0x0c? */
|
|
|
|
}
|
|
|
|
else { /* v0 */
|
|
|
|
start_offset = header_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-12 14:42:02 +01:00
|
|
|
switch (codec) {
|
2017-04-28 20:10:18 +02:00
|
|
|
case 0x02: { /* MSAPDCM [various SFX] */
|
|
|
|
vgmstream->coding_type = coding_MSADPCM;
|
|
|
|
vgmstream->layout_type = layout_none;
|
|
|
|
vgmstream->interleave_block_size = read_16bitLE(extra_data_offset + 0x02,streamFile);
|
|
|
|
|
|
|
|
vgmstream->num_samples = read_32bitLE(extra_data_offset + 0x04, streamFile); /* lower than header num_samples */
|
2017-02-12 14:42:02 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef VGM_USE_FFMPEG
|
|
|
|
case 0x05: { /* ogg vorbis [Final Fantasy VI, Dragon Quest II-VI] */
|
|
|
|
/* Starting from an offset in the current libvorbis code is a bit hard so just use FFmpeg.
|
2017-04-28 20:10:18 +02:00
|
|
|
* Decoding seems to produce the same output with (inaudible) +-1 lower byte differences due to rounding. */
|
2017-02-12 14:42:02 +01:00
|
|
|
ffmpeg_codec_data *ffmpeg_data;
|
|
|
|
|
2017-04-28 20:10:18 +02:00
|
|
|
ffmpeg_data = init_ffmpeg_offset(streamFile, start_offset,file_size-start_offset);
|
2017-02-12 14:42:02 +01:00
|
|
|
if ( !ffmpeg_data ) goto fail;
|
|
|
|
|
|
|
|
vgmstream->codec_data = ffmpeg_data;
|
|
|
|
vgmstream->coding_type = coding_FFmpeg;
|
|
|
|
vgmstream->layout_type = layout_none;
|
|
|
|
/* These oggs have loop info in the comments, too */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 0x06: { /* aac [The World Ends with You (iPad)] */
|
|
|
|
/* init_vgmstream_akb above has priority, but this works fine too */
|
|
|
|
ffmpeg_codec_data *ffmpeg_data;
|
|
|
|
|
2017-04-28 20:10:18 +02:00
|
|
|
ffmpeg_data = init_ffmpeg_offset(streamFile, start_offset,file_size-start_offset);
|
2017-02-12 14:42:02 +01:00
|
|
|
if ( !ffmpeg_data ) goto fail;
|
|
|
|
|
|
|
|
vgmstream->codec_data = ffmpeg_data;
|
|
|
|
vgmstream->coding_type = coding_FFmpeg;
|
|
|
|
vgmstream->layout_type = layout_none;
|
|
|
|
|
|
|
|
/* remove encoder delay from the "global" sample values */
|
|
|
|
vgmstream->num_samples -= ffmpeg_data->skipSamples;
|
|
|
|
vgmstream->loop_start_sample -= ffmpeg_data->skipSamples;
|
|
|
|
vgmstream->loop_end_sample -= ffmpeg_data->skipSamples;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open the file for reading */
|
|
|
|
if ( !vgmstream_open_stream(vgmstream, streamFile, start_offset) )
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
close_vgmstream(vgmstream);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* AKB2 - found in later SQEX iOS games */
|
|
|
|
VGMSTREAM * init_vgmstream_akb2_multi(STREAMFILE *streamFile) {
|
|
|
|
VGMSTREAM * vgmstream = NULL;
|
2017-02-12 15:43:57 +01:00
|
|
|
off_t start_offset, header_offset;
|
2017-02-12 14:42:02 +01:00
|
|
|
size_t datasize;
|
|
|
|
int loop_flag = 0, channel_count, codec;
|
2017-04-20 21:21:51 +02:00
|
|
|
int akb_header_size, sound_index = 0, sound_offset_data, sound, sound_header_size, material_offset_data, material_index = 0, material, extradata, encryption_flag;
|
2017-02-12 14:42:02 +01:00
|
|
|
|
|
|
|
/* check extensions */
|
2017-04-28 20:10:18 +02:00
|
|
|
/* .akb.bytes is the usual extension in later games */
|
2017-02-12 14:42:02 +01:00
|
|
|
if ( !check_extensions(streamFile, "akb") )
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* check header */
|
|
|
|
if (read_32bitBE(0x00,streamFile) != 0x414B4232) /* "AKB2" */
|
|
|
|
goto fail;
|
|
|
|
|
2017-04-20 21:21:51 +02:00
|
|
|
akb_header_size = read_16bitLE(0x06, streamFile);
|
|
|
|
sound_offset_data = akb_header_size + sound_index * 0x10;
|
|
|
|
sound = read_32bitLE(sound_offset_data + 0x04, streamFile);
|
|
|
|
sound_header_size = read_16bitLE(sound + 0x02, streamFile);
|
|
|
|
material_offset_data = sound + sound_header_size + material_index * 0x10;
|
|
|
|
material = sound + read_32bitLE(material_offset_data + 0x04, streamFile);
|
|
|
|
encryption_flag = read_8bit(material + 0x03, streamFile) & 0x08;
|
|
|
|
extradata = material + read_16bitLE(material + 0x04, streamFile);
|
|
|
|
|
|
|
|
start_offset = material + read_16bitLE(material + 0x04, streamFile) + read_32bitLE(material + 0x18, streamFile);
|
|
|
|
header_offset = material;
|
2017-02-12 15:43:57 +01:00
|
|
|
|
|
|
|
channel_count = read_8bit(header_offset+0x02,streamFile);
|
|
|
|
loop_flag = read_32bitLE(header_offset+0x14,streamFile) > 0;
|
2017-02-12 14:42:02 +01:00
|
|
|
|
|
|
|
/* build the VGMSTREAM */
|
|
|
|
vgmstream = allocate_vgmstream(channel_count,loop_flag);
|
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
|
|
|
/* 0x04: version? 0x08: filesize, 0x28: file_id?, others: no idea */
|
2017-02-12 15:43:57 +01:00
|
|
|
codec = read_8bit(header_offset+0x01,streamFile);
|
|
|
|
datasize = read_32bitLE(header_offset+0x08,streamFile);
|
|
|
|
vgmstream->sample_rate = (uint16_t)read_16bitLE(header_offset+0x06,streamFile);
|
2017-02-12 14:42:02 +01:00
|
|
|
/* When loop_flag num_samples may be much larger than real num_samples (it's fine when looping is off)
|
|
|
|
* Actual num_samples would be loop_end_sample+1, but more testing is needed */
|
2017-02-12 15:43:57 +01:00
|
|
|
vgmstream->num_samples = read_32bitLE(header_offset+0x0c,streamFile);
|
|
|
|
vgmstream->loop_start_sample = read_32bitLE(header_offset+0x10,streamFile);
|
|
|
|
vgmstream->loop_end_sample = read_32bitLE(header_offset+0x14,streamFile);
|
2017-02-12 14:42:02 +01:00
|
|
|
|
|
|
|
vgmstream->meta_type = meta_AKB;
|
|
|
|
|
|
|
|
switch (codec) {
|
2017-04-20 21:21:51 +02:00
|
|
|
case 0x02: { /* msadpcm [The Irregular at Magic High School Lost Zero (Android)] */
|
|
|
|
if (encryption_flag) goto fail;
|
|
|
|
vgmstream->num_samples = read_32bitLE(extradata + 0x04, streamFile);
|
|
|
|
vgmstream->coding_type = coding_MSADPCM;
|
|
|
|
vgmstream->layout_type = layout_none;
|
|
|
|
vgmstream->interleave_block_size = read_16bitLE(extradata + 0x02, streamFile);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-02-12 14:42:02 +01:00
|
|
|
#ifdef VGM_USE_FFMPEG
|
|
|
|
case 0x05: { /* ogg vorbis [The World Ends with You (iPhone / latest update)] */
|
|
|
|
ffmpeg_codec_data *ffmpeg_data;
|
|
|
|
|
|
|
|
ffmpeg_data = init_ffmpeg_offset(streamFile, start_offset,datasize);
|
|
|
|
if ( !ffmpeg_data ) goto fail;
|
|
|
|
|
|
|
|
vgmstream->codec_data = ffmpeg_data;
|
|
|
|
vgmstream->coding_type = coding_FFmpeg;
|
|
|
|
vgmstream->layout_type = layout_none;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open the file for reading */
|
|
|
|
if ( !vgmstream_open_stream(vgmstream, streamFile, start_offset) )
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
close_vgmstream(vgmstream);
|
|
|
|
return NULL;
|
|
|
|
}
|