From c0906d1b0fa63f57533999da8b5c1a02725f55d8 Mon Sep 17 00:00:00 2001 From: bnnm Date: Thu, 8 Mar 2018 23:26:10 +0100 Subject: [PATCH] Add CAF .caf/empty extension, cleanup [Baten Kaitos (GC), Fragile (Wii)] --- src/formats.c | 6 +-- src/layout/blocked.c | 4 +- src/layout/caf_blocked.c | 29 +++++------ src/layout/layout.h | 3 +- src/meta/ngc_caf.c | 102 ++++++++++++++++++--------------------- src/vgmstream.c | 2 +- src/vgmstream.h | 4 +- 7 files changed, 67 insertions(+), 83 deletions(-) diff --git a/src/formats.c b/src/formats.c index e919bd3d..2aaa5b21 100644 --- a/src/formats.c +++ b/src/formats.c @@ -88,7 +88,7 @@ static const char* extension_list[] = { "cbd2", "ccc", "cd", - "cfn", + "cfn", //fake extension/header id for .caf (to be removed) "ckd", "cnk", "cps", @@ -557,7 +557,7 @@ static const layout_info layout_info_list[] = { {layout_xa_blocked, "CD-ROM XA"}, {layout_blocked_ea_schl, "blocked (EA SCHl)"}, {layout_blocked_ea_1snh, "blocked (EA 1SNh)"}, - {layout_caf_blocked, "CAF blocked"}, + {layout_blocked_caf, "blocked (CAF)"}, {layout_wsi_blocked, ".wsi blocked"}, {layout_xvas_blocked, ".xvas blocked"}, {layout_str_snds_blocked, ".str SNDS blocked"}, @@ -651,7 +651,7 @@ static const meta_info meta_info_list[] = { {meta_DSP_STR, "assumed Conan Gamecube STR File by .str extension"}, {meta_EA_SCHL, "Electronic Arts SCHl header (variable)"}, {meta_EA_SCHL_fixed, "Electronic Arts SCHl header (fixed)"}, - {meta_CFN, "tri-Crescendo CAF Header"}, + {meta_CAF, "tri-Crescendo CAF Header"}, {meta_PS2_VPK, "VPK Header"}, {meta_GENH, "GENH Generic Header"}, {meta_DSP_SADB, "Procyon Studio SADB header"}, diff --git a/src/layout/blocked.c b/src/layout/blocked.c index b910cd9d..fb4b806d 100644 --- a/src/layout/blocked.c +++ b/src/layout/blocked.c @@ -86,8 +86,8 @@ void render_vgmstream_blocked(sample * buffer, int32_t sample_count, VGMSTREAM * case layout_blocked_ea_1snh: block_update_ea_1snh(vgmstream->next_block_offset,vgmstream); break; - case layout_caf_blocked: - caf_block_update(vgmstream->next_block_offset,vgmstream); + case layout_blocked_caf: + block_update_caf(vgmstream->next_block_offset,vgmstream); break; case layout_wsi_blocked: wsi_block_update(vgmstream->next_block_offset,vgmstream); diff --git a/src/layout/caf_blocked.c b/src/layout/caf_blocked.c index d944baa0..988e7714 100644 --- a/src/layout/caf_blocked.c +++ b/src/layout/caf_blocked.c @@ -1,26 +1,21 @@ #include "layout.h" #include "../vgmstream.h" -/* set up for the block at the given offset */ -void caf_block_update(off_t block_offset, VGMSTREAM * vgmstream) { - int i; +/* each block is a new CAF header */ +void block_update_caf(off_t block_offset, VGMSTREAM * vgmstream) { + STREAMFILE* streamFile = vgmstream->ch[0].streamfile; + int i,ch; vgmstream->current_block_offset = block_offset; - vgmstream->current_block_size = read_32bitBE( - vgmstream->current_block_offset+0x14, - vgmstream->ch[0].streamfile); - vgmstream->next_block_offset = vgmstream->current_block_offset + - (off_t)read_32bitBE(vgmstream->current_block_offset+0x04, - vgmstream->ch[0].streamfile); + vgmstream->next_block_offset = block_offset + read_32bitBE(block_offset+0x04, streamFile); + vgmstream->current_block_size = read_32bitBE(block_offset+0x14, streamFile); - for (i=0;ichannels;i++) { - vgmstream->ch[i].offset = vgmstream->current_block_offset + - read_32bitBE(block_offset+0x10+(8*i),vgmstream->ch[0].streamfile); - } + for (ch = 0; ch < vgmstream->channels; ch++) { + vgmstream->ch[ch].offset = block_offset + read_32bitBE(block_offset+0x10+(0x08*ch), streamFile); - /* coeffs */ - for (i=0;i<16;i++) { - vgmstream->ch[0].adpcm_coef[i] = read_16bitBE(block_offset+0x34+(2*i),vgmstream->ch[0].streamfile); - vgmstream->ch[1].adpcm_coef[i] = read_16bitBE(block_offset+0x60+(2*i),vgmstream->ch[0].streamfile); + /* re-read coeffs (though blocks seem to repeat them) */ + for (i = 0; i < 16; i++) { + vgmstream->ch[ch].adpcm_coef[i] = read_16bitBE(block_offset+0x34 + 0x2c*ch + 0x02*i, streamFile); + } } } diff --git a/src/layout/layout.h b/src/layout/layout.h index 41ba7735..3706c331 100644 --- a/src/layout/layout.h +++ b/src/layout/layout.h @@ -17,8 +17,7 @@ void xa_block_update(off_t block_offset, VGMSTREAM * vgmstream); void block_update_ea_schl(off_t block_offset, VGMSTREAM * vgmstream); void block_update_ea_1snh(off_t block_offset, VGMSTREAM * vgmstream); - -void caf_block_update(off_t block_offset, VGMSTREAM * vgmstream); +void block_update_caf(off_t block_offset, VGMSTREAM * vgmstream); void wsi_block_update(off_t block_offset, VGMSTREAM * vgmstream); diff --git a/src/meta/ngc_caf.c b/src/meta/ngc_caf.c index fce12488..8180fe8e 100644 --- a/src/meta/ngc_caf.c +++ b/src/meta/ngc_caf.c @@ -2,74 +2,64 @@ #include "../layout/layout.h" #include "../util.h" +/* CAF - from tri-Crescendo games [Baten Kaitos 1/2 (GC), Fragile (Wii)] */ VGMSTREAM * init_vgmstream_caf(STREAMFILE *streamFile) { VGMSTREAM * vgmstream = NULL; - char filename[PATH_LIMIT]; - - // Calculate sample length ... - int32_t num_of_samples=0; - int32_t block_count=0; + off_t start_offset, offset; + size_t file_size; + int channel_count, loop_flag; + int32_t num_samples = 0; + uint32_t loop_start = -1; - uint32_t loop_start=-1; - off_t offset=0; - off_t next_block; - off_t file_length; - int i; + /* checks */ + /* .caf: header id, .cfn: fake extension? , "" is accepted as files don't have extensions in the disc */ + if (!check_extensions(streamFile,"caf,cfn,")) + goto fail; + if (read_32bitBE(0x00,streamFile) != 0x43414620) /* "CAF " */ + goto fail; - /* check extension, case insensitive */ - streamFile->get_name(streamFile,filename,sizeof(filename)); - if (strcasecmp("cfn",filename_extension(filename))) goto fail; + /* get total samples */ + offset = 0; + file_size = get_streamfile_size(streamFile); + while (offset < file_size) { + off_t next_block = read_32bitBE(offset+0x04,streamFile); + num_samples += read_32bitBE(offset+0x14,streamFile)/8*14; - /* Check "CAF " ID */ - if (read_32bitBE(0,streamFile)!=0x43414620) goto fail; - - // Calculate sample length ... - file_length=(off_t)get_streamfile_size(streamFile); - - do { - next_block=read_32bitBE(offset+0x04,streamFile); - num_of_samples+=read_32bitBE(offset+0x14,streamFile)/8*14; - - if(read_32bitBE(offset+0x20,streamFile)==read_32bitBE(offset+0x08,streamFile)) { - loop_start=num_of_samples-read_32bitBE(offset+0x14,streamFile)/8*14; - } - offset+=next_block; - block_count++; - } while(offsetchannels=2; - vgmstream->sample_rate=32000; - vgmstream->num_samples=num_of_samples; - - if(loop_start!=-1) { - vgmstream->loop_start_sample=loop_start; - vgmstream->loop_end_sample=num_of_samples; - } - - vgmstream->coding_type = coding_NGC_DSP; - vgmstream->layout_type = layout_caf_blocked; - vgmstream->meta_type = meta_CFN; - - /* open the file for reading by each channel */ - { - for (i=0;i<2;i++) { - vgmstream->ch[i].streamfile = streamFile->open(streamFile,filename,0x8000); - - if (!vgmstream->ch[i].streamfile) goto fail; + if(read_32bitBE(offset+0x20,streamFile)==read_32bitBE(offset+0x08,streamFile)) { + loop_start = num_samples - read_32bitBE(offset+0x14,streamFile)/8*14; } + offset += next_block; } - caf_block_update(0,vgmstream); + start_offset = 0x00; + channel_count = 2; /* always stereo */ + loop_flag = (loop_start!=-1); + + + /* build the VGMSTREAM */ + vgmstream = allocate_vgmstream(channel_count,loop_flag); + if (!vgmstream) goto fail; + + vgmstream->sample_rate = 32000; + vgmstream->num_samples = num_samples; + if (loop_flag) { + vgmstream->loop_start_sample = loop_start; + vgmstream->loop_end_sample = num_samples; + } + + vgmstream->meta_type = meta_CAF; + vgmstream->coding_type = coding_NGC_DSP; + vgmstream->layout_type = layout_blocked_caf; + + if ( !vgmstream_open_stream(vgmstream,streamFile,start_offset) ) + goto fail; + + block_update_caf(start_offset,vgmstream); return vgmstream; - /* clean up anything we may have opened */ fail: - if (vgmstream) close_vgmstream(vgmstream); + close_vgmstream(vgmstream); return NULL; } diff --git a/src/vgmstream.c b/src/vgmstream.c index eb15f229..af68f893 100644 --- a/src/vgmstream.c +++ b/src/vgmstream.c @@ -956,7 +956,7 @@ void render_vgmstream(sample * buffer, int32_t sample_count, VGMSTREAM * vgmstre case layout_xa_blocked: case layout_blocked_ea_schl: case layout_blocked_ea_1snh: - case layout_caf_blocked: + case layout_blocked_caf: case layout_wsi_blocked: case layout_str_snds_blocked: case layout_ws_aud_blocked: diff --git a/src/vgmstream.h b/src/vgmstream.h index b18a0e82..f46c241b 100644 --- a/src/vgmstream.h +++ b/src/vgmstream.h @@ -225,7 +225,7 @@ typedef enum { layout_xa_blocked, layout_blocked_ea_schl, layout_blocked_ea_1snh, - layout_caf_blocked, + layout_blocked_caf, layout_wsi_blocked, layout_str_snds_blocked, layout_ws_aud_blocked, @@ -319,7 +319,7 @@ typedef enum { meta_RSF, /* Retro Studios RSF (Metroid Prime .rsf) [no header_id] */ meta_HALPST, /* HAL Labs HALPST */ meta_GCSW, /* GCSW (PCM) */ - meta_CFN, /* Namco CAF Audio File */ + meta_CAF, /* tri-Crescendo CAF */ meta_MYSPD, /* U-Sing .myspd */ meta_HIS, /* Her Ineractive .his */ meta_BNSF, /* Bandai Namco Sound Format */