From 973c4bff1d64993e99b08c26f3a03ba4867f0a5b Mon Sep 17 00:00:00 2001 From: bnnm Date: Mon, 26 Dec 2016 13:27:47 +0100 Subject: [PATCH] Added namco XMA (.xma), Soul Calibur II HD XMA (.past) variations --- src/meta/xma.c | 128 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 107 insertions(+), 21 deletions(-) diff --git a/src/meta/xma.c b/src/meta/xma.c index 18444e81..32a7a31d 100644 --- a/src/meta/xma.c +++ b/src/meta/xma.c @@ -23,6 +23,7 @@ typedef struct { int32_t fmt_codec; uint8_t xma2_version; int needs_header; + int force_little_endian; /* FFmpeg can't parse big endian "fmt" chunks */ /* info */ int loop_flag; @@ -39,6 +40,7 @@ typedef struct { static int parse_header(xma_header_data * xma, STREAMFILE *streamFile); static void parse_xma1_sample_data(xma_header_data * xma, STREAMFILE *streamFile); static int create_riff_header(uint8_t * buf, size_t buf_size, xma_header_data * xma, STREAMFILE *streamFile); +static int fmt_chunk_swap_endian(uint8_t * chunk, uint16_t codec); #if ADJUST_SAMPLE_RATE static int get_xma_sample_rate(int32_t general_rate); #endif @@ -61,7 +63,9 @@ VGMSTREAM * init_vgmstream_xma(STREAMFILE *streamFile) { /* check extension, case insensitive */ streamFile->get_name(streamFile,filename,sizeof(filename)); if (strcasecmp("xma",filename_extension(filename)) - && strcasecmp("xma2",filename_extension(filename)) ) /* Skullgirls */ + && strcasecmp("xma2",filename_extension(filename)) /* Skullgirls */ + && strcasecmp("past",filename_extension(filename)) /* SoulCalibur II HD */ + ) goto fail; /* check header */ @@ -74,7 +78,7 @@ VGMSTREAM * init_vgmstream_xma(STREAMFILE *streamFile) { fake_riff_size = create_riff_header(fake_riff, FAKE_RIFF_BUFFER_SIZE, &xma, streamFile); if (fake_riff_size <= 0) goto fail; - data = init_ffmpeg_header_offset(streamFile, fake_riff, (uint64_t)fake_riff_size, xma.data_offset+4+4, xma.data_size); + data = init_ffmpeg_header_offset(streamFile, fake_riff, (uint64_t)fake_riff_size, xma.data_offset, xma.data_size); if (!data) goto fail; } else { /* no change */ @@ -130,20 +134,28 @@ static int parse_header(xma_header_data * xma, STREAMFILE *streamFile) { int32_t (*read_32bit)(off_t,STREAMFILE*) = NULL; int16_t (*read_16bit)(off_t,STREAMFILE*) = NULL; uint32_t id; - enum { RIFF } header_type; int big_endian = 0; + enum { + id_RIFF = UINT32_C(0x52494646), /* "RIFF" */ + id_RIFX = UINT32_C(0x52494658), /* "RIFX" */ + id_NXMA = UINT32_C(0x786D6100), /* "xma\0" */ + id_PASX = UINT32_C(0x50415358), /* "PASX" */ + }; /* check header */ id = read_32bitBE(0x00,streamFile); - if (id == 0x52494646 || id == 0x52494658) { /* "RIFF" / "RIFX" */ - big_endian = id == 0x52494658; - header_type = RIFF; + switch (id) { + case id_RIFF: + break; + case id_RIFX: + case id_NXMA: + case id_PASX: + big_endian = 1; + break; + default: + goto fail; } - else { - goto fail; - } - memset(xma,0,sizeof(xma_header_data)); xma->big_endian = big_endian; @@ -159,7 +171,7 @@ static int parse_header(xma_header_data * xma, STREAMFILE *streamFile) { xma->file_size = streamFile->get_size(streamFile); /* find offsets */ - if (header_type == RIFF) { /* regular RIFF header */ + if (id == id_RIFF || id == id_RIFX) { /* regular RIFF header */ off_t current_chunk = 0xc; off_t fmt_offset = 0, xma2_offset = 0; size_t riff_size = 0, fmt_size = 0, xma2_size = 0; @@ -184,7 +196,7 @@ static int parse_header(xma_header_data * xma, STREAMFILE *streamFile) { case 0x64617461: /* "data" */ if (xma->data_offset) goto fail; - xma->data_offset = current_chunk; + xma->data_offset = current_chunk + 4 + 4; xma->data_size = chunk_size; break; case 0x584D4132: /* "XMA2" */ @@ -210,10 +222,42 @@ static int parse_header(xma_header_data * xma, STREAMFILE *streamFile) { xma->chunk_offset = fmt_offset; xma->chunk_size = fmt_size; xma->fmt_codec = read_16bit(xma->chunk_offset,streamFile); + xma->force_little_endian = xma->big_endian; } else { goto fail; } - } else { + } + else if (id == id_NXMA) { /* Namco (Tekken 6, Galaga Legions DX) */ + /* custom header with a "XMA2" or "fmt " data chunk inside, most other values are unknown */ + uint32_t chunk_type = read_32bit(0xC,streamFile); + xma->data_offset = 0x100; + xma->data_size = read_32bit(0x14,streamFile); + xma->chunk_offset = 0xBC; + xma->chunk_size = read_32bit(0x24,streamFile); + if (chunk_type == 0x4) { /* "XMA2" */ + xma->xma2_version = read_8bit(xma->chunk_offset,streamFile); + } else if (chunk_type == 0x8) { /* "fmt " */ + xma->fmt_codec = read_16bit(xma->chunk_offset,streamFile); + xma->force_little_endian = 1; + } else { + goto fail; + } + xma->needs_header = 1; + + if (xma->data_size + xma->data_offset > xma->file_size) goto fail; + } + else if (id == id_PASX) { /* SoulCalibur II HD */ + /* custom header with a "fmt " data chunk inside */ + xma->chunk_size = read_32bit(0x08,streamFile); + xma->data_size = read_32bit(0x0c,streamFile); + xma->chunk_offset = read_32bit(0x10,streamFile); + /* 0x14: chunk offset end */ + xma->data_offset = read_32bit(0x18,streamFile); + xma->fmt_codec = read_16bit(xma->chunk_offset,streamFile); + xma->needs_header = 1; + xma->force_little_endian = 1; + } + else { goto fail; } @@ -237,8 +281,8 @@ static int parse_header(xma_header_data * xma, STREAMFILE *streamFile) { xma->loop_flag = (uint8_t)read_8bit(xma->chunk_offset+0x30,streamFile) > 0 /* never set in practice */ || xma->loop_end_sample; /* not needed but may affect looping? (sometimes these don't match loop/total samples) */ - /* int32_t play_begin_sample = read_32bit(xma->fmt_offset+0x28,streamFile); */ - /* int32_t play_end_sample = play_begin_sample + read_32bit(xma->fmt_offset+0x24,streamFile); */ + /* int32_t play_begin_sample = read_32bit(xma->chunk_offset+0x20,streamFile); */ + /* int32_t play_end_sample = play_begin_sample + read_32bit(xma->chunk_offset+0x24,streamFile); */ } else if (xma->fmt_codec == 0x165) { /* pure XMA1 */ xma->loop_flag = (uint8_t)read_8bit(xma->chunk_offset+0xA,streamFile) > 0; @@ -248,7 +292,7 @@ static int parse_header(xma_header_data * xma, STREAMFILE *streamFile) { /* find samples count + loop samples since they are not in the header */ parse_xma1_sample_data(xma, streamFile); } - else { /* RIFF with no XMA data or unknown version */ + else { /* unknown chunk */ goto fail; } @@ -272,9 +316,9 @@ static void parse_xma1_sample_data(xma_header_data * xma, STREAMFILE *streamFile uint32_t size; uint32_t packet_size = XMA_BYTES_PER_PACKET; - uint32_t offset = xma->data_offset + 4 + 4; + uint32_t offset = xma->data_offset; uint32_t offset_b = 0; - uint32_t stream_offset_b = (xma->data_offset + 4 + 4) * 8; + uint32_t stream_offset_b = xma->data_offset * 8; size = offset + xma->data_size; packet_size_b = packet_size*8; @@ -333,7 +377,9 @@ static int create_riff_header(uint8_t * buf, size_t buf_size, xma_header_data * uint8_t internal[FAKE_RIFF_BUFFER_SIZE]; size_t head_size, file_size, internal_size; - if (xma->big_endian) { + int use_be = xma->big_endian && !xma->force_little_endian; + + if (use_be) { put_32bit = put_32bitBE; } else { put_32bit = put_32bitLE; @@ -347,7 +393,7 @@ static int create_riff_header(uint8_t * buf, size_t buf_size, xma_header_data * if (xma->xma2_version == 3) { /* old XMA2 v3: change to v4 (extra 8 bytes in the middle) */ internal_size = 4+4+xma->chunk_size + 8; - memcpy(internal + 0x0, "XMA2", 4); /* "XMA2" chunk (interal data is BE) */ + memcpy(internal + 0x0, "XMA2", 4); /* "XMA2" chunk (internal data is BE) */ put_32bit(internal + 0x4, xma->chunk_size + 8); /* v3 > v4 size*/ put_8bit(internal + 0x8, 4); /* v4 */ memcpy(internal + 0x9, chunk+1, 15); /* first v3 part (fixed) */ @@ -358,6 +404,11 @@ static int create_riff_header(uint8_t * buf, size_t buf_size, xma_header_data * else { /* direct copy (old XMA2 v4 ignoring "fmt", pure XMA1/2) */ internal_size = 4+4+xma->chunk_size; + if (xma->force_little_endian ) { + if ( !fmt_chunk_swap_endian(chunk, xma->fmt_codec) ) + goto fail; + } + memcpy(internal + 0x0, xma->xma2_version ? "XMA2" : "fmt ", 4); put_32bit(internal + 0x4, xma->chunk_size); memcpy(internal + 0x8, chunk, xma->chunk_size); @@ -368,7 +419,7 @@ static int create_riff_header(uint8_t * buf, size_t buf_size, xma_header_data * file_size = head_size-4-4 + xma->data_size; if (head_size > buf_size) goto fail; - memcpy(buf + 0x0, xma->big_endian ? "RIFX" : "RIFF", 4); + memcpy(buf + 0x0, use_be ? "RIFX" : "RIFF", 4); put_32bit(buf + 0x4, file_size); memcpy(buf + 0x8, "WAVE", 4); memcpy(buf + 0xc, internal, internal_size); @@ -382,6 +433,41 @@ fail: } +/** + * Swaps endianness + * + * returns 0 on error + */ +static int fmt_chunk_swap_endian(uint8_t * chunk, uint16_t codec) { + if (codec != 0x166) + goto fail; + + put_16bitLE(chunk + 0x00, get_16bitBE(chunk + 0x00));/*wFormatTag*/ + put_16bitLE(chunk + 0x02, get_16bitBE(chunk + 0x02));/*nChannels*/ + put_32bitLE(chunk + 0x04, get_32bitBE(chunk + 0x04));/*nSamplesPerSec*/ + put_32bitLE(chunk + 0x08, get_32bitBE(chunk + 0x08));/*nAvgBytesPerSec*/ + put_16bitLE(chunk + 0x0c, get_16bitBE(chunk + 0x0c));/*nBlockAlign*/ + put_16bitLE(chunk + 0x0e, get_16bitBE(chunk + 0x0e));/*wBitsPerSample*/ + put_16bitLE(chunk + 0x10, get_16bitBE(chunk + 0x10));/*cbSize*/ + put_16bitLE(chunk + 0x12, get_16bitBE(chunk + 0x12));/*NumStreams*/ + put_32bitLE(chunk + 0x14, get_32bitBE(chunk + 0x14));/*ChannelMask*/ + put_32bitLE(chunk + 0x18, get_32bitBE(chunk + 0x18));/*SamplesEncoded*/ + put_32bitLE(chunk + 0x1c, get_32bitBE(chunk + 0x1c));/*BytesPerBlock*/ + put_32bitLE(chunk + 0x20, get_32bitBE(chunk + 0x20));/*PlayBegin*/ + put_32bitLE(chunk + 0x24, get_32bitBE(chunk + 0x24));/*PlayLength*/ + put_32bitLE(chunk + 0x28, get_32bitBE(chunk + 0x28));/*LoopBegin*/ + put_32bitLE(chunk + 0x2c, get_32bitBE(chunk + 0x2c));/*LoopLength*/ + /* put_8bit(chunk + 0x30, get_8bit(chunk + 0x30));*//*LoopCount*/ + /* put_8bit(chunk + 0x31, get_8bit(chunk + 0x31));*//*EncoderVersion*/ + put_16bitLE(chunk + 0x32, get_16bitBE(chunk + 0x32));/*BlockCount*/ + + return 1; + +fail: + return 0; +} + + #if ADJUST_SAMPLE_RATE /** * Get real XMA sample rate (from Microsoft docs, apparently info only and not correct for playback).