cog/Frameworks/vgmstream/vgmstream/src/meta/ahx.c

70 lines
2.3 KiB
C
Raw Normal View History

2014-02-26 07:50:54 +00:00
#include "meta.h"
#include "../coding/coding.h"
2014-02-26 07:50:54 +00:00
#include "../util.h"
#ifdef VGM_USE_MPEG
2014-02-26 07:50:54 +00:00
/* AHX is a CRI format which contains an MPEG-2 Layer 2 audio stream.
* Although the MPEG frame headers are incorrect... */
VGMSTREAM * init_vgmstream_ahx(STREAMFILE *streamFile) {
VGMSTREAM * vgmstream = NULL;
off_t start_offset;
2014-02-26 07:50:54 +00:00
int channel_count = 1;
int loop_flag = 0;
/* check extension, case insensitive */
if ( !check_extensions(streamFile, "ahx") ) goto fail;
2014-02-26 07:50:54 +00:00
/* check first 2 bytes */
if ((uint16_t)read_16bitBE(0,streamFile)!=0x8000) goto fail;
/* get stream offset, check for CRI signature just before */
start_offset = (uint16_t)read_16bitBE(2,streamFile) + 4;
if ((uint16_t)read_16bitBE(start_offset-6,streamFile)!=0x2863 ||/* "(c" */
(uint32_t)read_32bitBE(start_offset-4,streamFile)!=0x29435249 /* ")CRI" */
2014-02-26 07:50:54 +00:00
) goto fail;
/* check for encoding type */
/* 2 is for some unknown fixed filter, 3 is standard ADX, 4 is
* ADX with exponential scale, 0x11 is AHX */
/* Sappharad reports that old AHXs (Sonic Adventure 2) don't have this flag.
* When I see one perhaps I can add an exception for those. */
if (read_8bit(4,streamFile) != 0x11) goto fail;
/* check for frame size (0 for AHX) */
if (read_8bit(5,streamFile) != 0) goto fail;
/* check for bits per sample? (0 for AHX) */
if (read_8bit(6,streamFile) != 0) goto fail;
/* check channel count (only mono AHXs are known) */
if (read_8bit(7,streamFile) != 1) goto fail;
/* At this point we almost certainly have an AHX file,
2014-02-26 07:50:54 +00:00
* so let's build the VGMSTREAM. */
vgmstream = allocate_vgmstream(channel_count,loop_flag);
if (!vgmstream) goto fail;
/* fill in the vital statistics */
vgmstream->num_samples = read_32bitBE(0xc,streamFile);
/* This is the One True Samplerate, the MPEG headers lie. */
vgmstream->sample_rate = read_32bitBE(8,streamFile);
vgmstream->coding_type = coding_fake_MPEG2_L2;
vgmstream->layout_type = layout_fake_mpeg;
vgmstream->meta_type = meta_AHX;
vgmstream->codec_data = init_mpeg_codec_data_ahx(streamFile, start_offset, channel_count);
2014-02-26 07:50:54 +00:00
if ( !vgmstream_open_stream(vgmstream, streamFile, start_offset) )
goto fail;
2014-02-26 07:50:54 +00:00
return vgmstream;
fail:
close_vgmstream(vgmstream);
2014-02-26 07:50:54 +00:00
return NULL;
}
#endif