From 3380451c8042e78ef2af78c46f67097f06523223 Mon Sep 17 00:00:00 2001 From: bnnm Date: Wed, 6 Dec 2017 15:32:52 +0100 Subject: [PATCH] Code cleanup (renames, comments, nitpicks) --- src/coding/ima_decoder.c | 81 +++-- src/formats.c | 1 + src/meta/{afc_header.c => afc.c} | 0 src/meta/{gh3_bar.c => bar.c} | 4 +- ...{gh3_bar_streamfile.h => bar_streamfile.h} | 6 +- src/meta/meta.h | 25 +- src/util.h | 15 +- src/vgmstream.c | 289 ++++++++---------- src/vgmstream.h | 33 +- 9 files changed, 196 insertions(+), 258 deletions(-) rename src/meta/{afc_header.c => afc.c} (100%) rename src/meta/{gh3_bar.c => bar.c} (96%) rename src/meta/{gh3_bar_streamfile.h => bar_streamfile.h} (93%) diff --git a/src/coding/ima_decoder.c b/src/coding/ima_decoder.c index 94a6b318..c8f56ef1 100644 --- a/src/coding/ima_decoder.c +++ b/src/coding/ima_decoder.c @@ -18,8 +18,7 @@ * Officially defined in "Microsoft Multimedia Standards Update" doc (RIFFNEW.pdf). */ -static const int32_t ADPCMTable[89] = -{ +static const int ADPCMTable[89] = { 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, @@ -34,8 +33,7 @@ static const int32_t ADPCMTable[89] = 32767 }; -static const int IMA_IndexTable[16] = -{ +static const int IMA_IndexTable[16] = { -1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8 }; @@ -45,17 +43,22 @@ static const int IMA_IndexTable[16] = static void std_ima_expand_nibble(VGMSTREAMCHANNEL * stream, off_t byte_offset, int nibble_shift, int32_t * hist1, int32_t * step_index) { int sample_nibble, sample_decoded, step, delta; - sample_nibble = (read_8bit(byte_offset,stream->streamfile) >> nibble_shift)&0xf; - sample_decoded = *hist1; - step = ADPCMTable[*step_index]; + /* calculate diff = [signed] (step / 8) + (step / 4) + (step / 2) + (step) [when code = 4+2+1] + * simplified through math, using bitwise ops to avoid rounding: + * diff = (code + 1/2) * (step / 4) + * > diff = (step * nibble / 4) + (step / 8) + * > diff = (((step * nibble) + (step / 2)) / 4) */ + + sample_nibble = (read_8bit(byte_offset,stream->streamfile) >> nibble_shift)&0xf; /* ADPCM code */ + sample_decoded = *hist1; /* predictor value */ + step = ADPCMTable[*step_index]; /* current step */ + delta = step >> 3; if (sample_nibble & 1) delta += step >> 2; if (sample_nibble & 2) delta += step >> 1; if (sample_nibble & 4) delta += step; - if (sample_nibble & 8) - sample_decoded -= delta; - else - sample_decoded += delta; + if (sample_nibble & 8) delta = -delta; + sample_decoded += delta; *hist1 = clamp16(sample_decoded); *step_index += IMA_IndexTable[sample_nibble]; @@ -63,21 +66,20 @@ static void std_ima_expand_nibble(VGMSTREAMCHANNEL * stream, off_t byte_offset, if (*step_index > 88) *step_index=88; } -/* Apple's IMA variation. Exactly the same except it uses 16b history (probably more sensitive to overflow/sign extend) */ +/* Apple's IMA variation. Exactly the same except it uses 16b history (probably more sensitive to overflow/sign extend?) */ static void std_ima_expand_nibble_16(VGMSTREAMCHANNEL * stream, off_t byte_offset, int nibble_shift, int16_t * hist1, int32_t * step_index) { int sample_nibble, sample_decoded, step, delta; sample_nibble = (read_8bit(byte_offset,stream->streamfile) >> nibble_shift)&0xf; sample_decoded = *hist1; step = ADPCMTable[*step_index]; + delta = step >> 3; if (sample_nibble & 1) delta += step >> 2; if (sample_nibble & 2) delta += step >> 1; if (sample_nibble & 4) delta += step; - if (sample_nibble & 8) - sample_decoded -= delta; - else - sample_decoded += delta; + if (sample_nibble & 8) delta = -delta; + sample_decoded += delta; *hist1 = clamp16(sample_decoded); //no need for this actually *step_index += IMA_IndexTable[sample_nibble]; @@ -91,14 +93,12 @@ static void n3ds_ima_expand_nibble(VGMSTREAMCHANNEL * stream, off_t byte_offset, sample_nibble = (read_8bit(byte_offset,stream->streamfile) >> nibble_shift)&0xf; sample_decoded = *hist1; + step = ADPCMTable[*step_index]; sample_decoded = sample_decoded << 3; - step = ADPCMTable[*step_index]; - delta = step * (sample_nibble & 7) * 2 + step; - if (sample_nibble & 8) - sample_decoded -= delta; - else - sample_decoded += delta; + delta = step * (sample_nibble & 7) * 2 + step; /* custom */ + if (sample_nibble & 8) delta = -delta; + sample_decoded += delta; sample_decoded = sample_decoded >> 3; *hist1 = clamp16(sample_decoded); @@ -107,41 +107,41 @@ static void n3ds_ima_expand_nibble(VGMSTREAMCHANNEL * stream, off_t byte_offset, if (*step_index > 88) *step_index=88; } -/* update step_index before doing current sample */ +/* The Incredibles PC, updates step_index before doing current sample */ static void snds_ima_expand_nibble(VGMSTREAMCHANNEL * stream, off_t byte_offset, int nibble_shift, int32_t * hist1, int32_t * step_index) { int sample_nibble, sample_decoded, step, delta; sample_nibble = (read_8bit(byte_offset,stream->streamfile) >> nibble_shift)&0xf; + sample_decoded = *hist1; *step_index += IMA_IndexTable[sample_nibble]; if (*step_index < 0) *step_index=0; if (*step_index > 88) *step_index=88; step = ADPCMTable[*step_index]; - delta = (sample_nibble & 7) * step / 4 + step / 8; - if (sample_nibble & 8) - delta = -delta; - sample_decoded = *hist1 + delta; + + delta = (sample_nibble & 7) * step / 4 + step / 8; /* standard IMA */ + if (sample_nibble & 8) delta = -delta; + sample_decoded += delta; *hist1 = clamp16(sample_decoded); } -/* algorithm by aluigi, unsure if it's a known IMA variation */ +/* Omikron: The Nomad Soul, algorithm by aluigi */ static void otns_ima_expand_nibble(VGMSTREAMCHANNEL * stream, off_t byte_offset, int nibble_shift, int32_t * hist1, int32_t * step_index) { int sample_nibble, sample_decoded, step, delta; sample_nibble = (read_8bit(byte_offset,stream->streamfile) >> nibble_shift)&0xf; sample_decoded = *hist1; step = ADPCMTable[*step_index]; + delta = 0; if(sample_nibble & 4) delta = step << 2; if(sample_nibble & 2) delta += step << 1; if(sample_nibble & 1) delta += step; delta >>= 2; - if(sample_nibble & 8) - sample_decoded -= delta; - else - sample_decoded += delta; + if (sample_nibble & 8) delta = -delta; + sample_decoded += delta; *hist1 = clamp16(sample_decoded); *step_index += IMA_IndexTable[sample_nibble]; @@ -149,23 +149,22 @@ static void otns_ima_expand_nibble(VGMSTREAMCHANNEL * stream, off_t byte_offset, if (*step_index > 88) *step_index=88; } -/* algorithm by Zench (https://bitbucket.org/Zenchreal/decubisnd) */ +/* Ubisoft games, algorithm by Zench (https://bitbucket.org/Zenchreal/decubisnd) */ static void ubi_ima_expand_nibble(VGMSTREAMCHANNEL * stream, off_t byte_offset, int nibble_shift, int32_t * hist1, int32_t * step_index) { int sample_nibble, sample_decoded, step, delta; sample_nibble = (read_8bit(byte_offset,stream->streamfile) >> nibble_shift)&0xf; - + sample_decoded = *hist1; step = ADPCMTable[*step_index]; + + delta = (((sample_nibble & 7) * 2 + 1) * step) >> 3; /* custom */ + if (sample_nibble & 8) delta = -delta; + sample_decoded += delta; + + *hist1 = clamp16(sample_decoded); *step_index += IMA_IndexTable[sample_nibble]; if (*step_index < 0) *step_index=0; if (*step_index > 88) *step_index=88; - - delta = (((sample_nibble & 7) * 2 + 1) * step) >> 3; - if (sample_nibble & 8) - delta = -delta; - sample_decoded = *hist1 + delta; - - *hist1 = clamp16(sample_decoded); } /* ************************************ */ diff --git a/src/formats.c b/src/formats.c index 3b1e8c59..7384de24 100644 --- a/src/formats.c +++ b/src/formats.c @@ -382,6 +382,7 @@ static const char* extension_list[] = { //, NULL //end mark }; +/* List supported formats and return elements in the list, for plugins that need to know. */ const char ** vgmstream_get_formats(size_t * size) { *size = sizeof(extension_list) / sizeof(char*); return extension_list; diff --git a/src/meta/afc_header.c b/src/meta/afc.c similarity index 100% rename from src/meta/afc_header.c rename to src/meta/afc.c diff --git a/src/meta/gh3_bar.c b/src/meta/bar.c similarity index 96% rename from src/meta/gh3_bar.c rename to src/meta/bar.c index 558e32cd..636e8002 100644 --- a/src/meta/gh3_bar.c +++ b/src/meta/bar.c @@ -1,8 +1,8 @@ #include "meta.h" -#include "gh3_bar_streamfile.h" +#include "bar_streamfile.h" /* Guitar Hero III Mobile .bar */ -VGMSTREAM * init_vgmstream_gh3_bar(STREAMFILE *streamFile) { +VGMSTREAM * init_vgmstream_bar(STREAMFILE *streamFile) { VGMSTREAM * vgmstream = NULL; STREAMFILE* streamFileBAR = NULL; // don't close, this is just the source streamFile wrapped char filename[PATH_LIMIT]; diff --git a/src/meta/gh3_bar_streamfile.h b/src/meta/bar_streamfile.h similarity index 93% rename from src/meta/gh3_bar_streamfile.h rename to src/meta/bar_streamfile.h index 95a4d9aa..8305020c 100644 --- a/src/meta/gh3_bar_streamfile.h +++ b/src/meta/bar_streamfile.h @@ -1,5 +1,5 @@ -#ifndef _GH3_BAR_STREAMFILE_H_ -#define _GH3_BAR_STREAMFILE_H_ +#ifndef _BAR_STREAMFILE_H_ +#define _BAR_STREAMFILE_H_ #include "../streamfile.h" /* a streamfile wrapping another for decryption */ @@ -97,4 +97,4 @@ int (*get_error_count)(BARSTREAMFILE *streamFile) { return &streamfile->sf; } -#endif /* _GH3_BAR_STREAMFILE_H_ */ +#endif /* _BAR_STREAMFILE_H_ */ diff --git a/src/meta/meta.h b/src/meta/meta.h index 9fc0fa43..af6a0489 100644 --- a/src/meta/meta.h +++ b/src/meta/meta.h @@ -80,8 +80,6 @@ VGMSTREAM * init_vgmstream_xbox_xwav(STREAMFILE *streamFile); VGMSTREAM * init_vgmstream_ngc_str(STREAMFILE *streamFile); -VGMSTREAM * init_vgmstream_ea_schl(STREAMFILE *streamFile); - VGMSTREAM * init_vgmstream_caf(STREAMFILE *streamFile); VGMSTREAM * init_vgmstream_ps2_vpk(STREAMFILE *streamFile); @@ -173,7 +171,6 @@ VGMSTREAM * init_vgmstream_aus(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_rws(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_fsb(STREAMFILE * streamFile); - VGMSTREAM * init_vgmstream_fsb4_wav(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_fsb5(STREAMFILE * streamFile); @@ -243,11 +240,9 @@ VGMSTREAM * init_vgmstream_ngc_swd(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_capdsp(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_xbox_wvs(STREAMFILE *streamFile); - VGMSTREAM * init_vgmstream_ngc_wvs(STREAMFILE *streamFile); VGMSTREAM * init_vgmstream_dc_str(STREAMFILE *streamFile); - VGMSTREAM * init_vgmstream_dc_str_v2(STREAMFILE *streamFile); VGMSTREAM * init_vgmstream_xbox_matx(STREAMFILE *streamFile); @@ -276,12 +271,8 @@ VGMSTREAM * init_vgmstream_ps2_omu(STREAMFILE *streamFile); VGMSTREAM * init_vgmstream_ps2_xa2(STREAMFILE * streamFile); -//VGMSTREAM * init_vgmstream_idsp(STREAMFILE * streamFile); - VGMSTREAM * init_vgmstream_idsp2(STREAMFILE * streamFile); - VGMSTREAM * init_vgmstream_idsp3(STREAMFILE * streamFile); - VGMSTREAM * init_vgmstream_idsp4(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_ngc_ymf(STREAMFILE * streamFile); @@ -321,7 +312,6 @@ VGMSTREAM * init_vgmstream_dc_asd(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_naomi_spsd(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_bgw(STREAMFILE * streamFile); - VGMSTREAM * init_vgmstream_spw(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_ps2_ass(STREAMFILE * streamFile); @@ -359,7 +349,6 @@ VGMSTREAM * init_vgmstream_dc_dcsw_dcs(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_wii_smp(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_emff_ps2(STREAMFILE * streamFile); - VGMSTREAM * init_vgmstream_emff_ngc(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_thp(STREAMFILE *streamFile); @@ -387,6 +376,7 @@ VGMSTREAM * init_vgmstream_ps2_vsf(STREAMFILE *streamFile); VGMSTREAM * init_vgmstream_nds_rrds(STREAMFILE *streamFile); VGMSTREAM * init_vgmstream_ps2_tk5(STREAMFILE *streamFile); +VGMSTREAM * init_vgmstream_ps2_tk1(STREAMFILE* streamFile); VGMSTREAM * init_vgmstream_ps2_vsf_tta(STREAMFILE *streamFile); @@ -447,7 +437,6 @@ VGMSTREAM * init_vgmstream_wii_bns(STREAMFILE* streamFile); VGMSTREAM * init_vgmstream_wii_was(STREAMFILE* streamFile); VGMSTREAM * init_vgmstream_pona_3do(STREAMFILE* streamFile); - VGMSTREAM * init_vgmstream_pona_psx(STREAMFILE* streamFile); VGMSTREAM * init_vgmstream_xbox_hlwav(STREAMFILE* streamFile); @@ -492,8 +481,6 @@ VGMSTREAM * init_vgmstream_dsp_ddsp(STREAMFILE* streamFile); VGMSTREAM * init_vgmstream_p3d(STREAMFILE* streamFile); -VGMSTREAM * init_vgmstream_ps2_tk1(STREAMFILE* streamFile); - VGMSTREAM * init_vgmstream_ps2_adsc(STREAMFILE* streamFile); VGMSTREAM * init_vgmstream_ngc_dsp_mpds(STREAMFILE* streamFile); @@ -503,9 +490,7 @@ VGMSTREAM * init_vgmstream_dsp_str_ig(STREAMFILE* streamFile); VGMSTREAM * init_vgmstream_psx_mgav(STREAMFILE* streamFile); VGMSTREAM * init_vgmstream_ngc_dsp_sth_str1(STREAMFILE* streamFile); - VGMSTREAM * init_vgmstream_ngc_dsp_sth_str2(STREAMFILE* streamFile); - VGMSTREAM * init_vgmstream_ngc_dsp_sth_str3(STREAMFILE* streamFile); VGMSTREAM * init_vgmstream_ps2_b1s(STREAMFILE* streamFile); @@ -526,7 +511,7 @@ VGMSTREAM * init_vgmstream_ps2_vms(STREAMFILE* streamFile); VGMSTREAM * init_vgmstream_xau(STREAMFILE* streamFile); -VGMSTREAM * init_vgmstream_gh3_bar(STREAMFILE* streamFile); +VGMSTREAM * init_vgmstream_bar(STREAMFILE* streamFile); VGMSTREAM * init_vgmstream_ffw(STREAMFILE* streamFile); @@ -583,7 +568,6 @@ VGMSTREAM * init_vgmstream_pc_adp_bos(STREAMFILE* streamFile); VGMSTREAM * init_vgmstream_pc_adp_otns(STREAMFILE* streamFile); VGMSTREAM * init_vgmstream_eb_sfx(STREAMFILE* streamFile); - VGMSTREAM * init_vgmstream_eb_sf0(STREAMFILE* streamFile); VGMSTREAM * init_vgmstream_ps3_klbs(STREAMFILE* streamFile); @@ -669,6 +653,7 @@ VGMSTREAM * init_vgmstream_wii_04sw(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_txth(STREAMFILE * streamFile); +VGMSTREAM * init_vgmstream_ea_schl(STREAMFILE *streamFile); VGMSTREAM * init_vgmstream_ea_bnk(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_ea_schl_fixed(STREAMFILE * streamFile); @@ -677,8 +662,6 @@ VGMSTREAM * init_vgmstream_sk_aud(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_stm(STREAMFILE * streamFile); -VGMSTREAM * init_vgmstream_ea_snu(STREAMFILE * streamFile); - VGMSTREAM * init_vgmstream_awc(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_nsw_opus(STREAMFILE * streamFile); @@ -695,8 +678,8 @@ VGMSTREAM * init_vgmstream_ezw(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_vxn(STREAMFILE * streamFile); +VGMSTREAM * init_vgmstream_ea_snu(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_ea_snr_sns(STREAMFILE * streamFile); - VGMSTREAM * init_vgmstream_ea_sps(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_ngc_vid1(STREAMFILE * streamFile); diff --git a/src/util.h b/src/util.h index 0325743f..a2c5765f 100644 --- a/src/util.h +++ b/src/util.h @@ -61,17 +61,16 @@ static inline int get_low_nibble_signed(uint8_t n) { return nibble_to_int[n&0xf]; } -/* return a file's extension (a pointer to the first character of the - * extension in the original filename or the ending null byte if no extension - */ -const char * filename_extension(const char * filename); - static inline int clamp16(int32_t val) { - if (val>32767) return 32767; - if (val<-32768) return -32768; - return val; + if (val>32767) return 32767; + if (val<-32768) return -32768; + return val; } +/* return a file's extension (a pointer to the first character of the + * extension in the original filename or the ending null byte if no extension */ +const char * filename_extension(const char * filename); + void swap_samples_le(sample *buf, int count); void concatn(int length, char * dst, const char * src); diff --git a/src/vgmstream.c b/src/vgmstream.c index dbd0e076..f8630091 100644 --- a/src/vgmstream.c +++ b/src/vgmstream.c @@ -147,7 +147,6 @@ VGMSTREAM * (*init_vgmstream_functions[])(STREAMFILE *streamFile) = { init_vgmstream_kraw, init_vgmstream_ps2_omu, init_vgmstream_ps2_xa2, - //init_vgmstream_idsp, init_vgmstream_idsp2, init_vgmstream_idsp3, init_vgmstream_idsp4, @@ -279,7 +278,7 @@ VGMSTREAM * (*init_vgmstream_functions[])(STREAMFILE *streamFile) = { init_vgmstream_dsp_bdsp, init_vgmstream_ps2_vms, init_vgmstream_xau, - init_vgmstream_gh3_bar, + init_vgmstream_bar, init_vgmstream_ffw, init_vgmstream_dsp_dspw, init_vgmstream_ps2_jstm, @@ -440,21 +439,6 @@ static VGMSTREAM * init_vgmstream_internal(STREAMFILE *streamFile) { } -#ifdef VGM_DEBUG_OUTPUT -#ifdef VGM_USE_FFMPEG - /* debug fun */ - if (vgmstream->coding_type != coding_FFmpeg){ - int i = 0; - - /* probable segfault but some layouts/codecs can ignore these */ - for (i = 0; i < vgmstream->channels; i++) { - VGM_ASSERT(vgmstream->ch[i].streamfile == NULL, "VGMSTREAM: null streamfile in ch%i\n",i); - } - } -#endif -#endif/*VGM_DEBUG_OUTPUT*/ - - #ifdef VGM_USE_FFMPEG /* check FFmpeg streams here, for lack of a better place */ if (vgmstream->coding_type == coding_FFmpeg) { @@ -577,28 +561,6 @@ void reset_vgmstream(VGMSTREAM * vgmstream) { } } - if (vgmstream->layout_type==layout_aix) { - aix_codec_data *data = vgmstream->codec_data; - int i; - - data->current_segment = 0; - for (i=0;isegment_count*data->stream_count;i++) - { - reset_vgmstream(data->adxs[i]); - } - } - - if (vgmstream->layout_type==layout_aax) { - aax_codec_data *data = vgmstream->codec_data; - int i; - - data->current_segment = 0; - for (i=0;isegment_count;i++) - { - reset_vgmstream(data->adxs[i]); - } - } - if ( vgmstream->coding_type == coding_NWA0 || vgmstream->coding_type == coding_NWA1 || @@ -611,19 +573,38 @@ void reset_vgmstream(VGMSTREAM * vgmstream) { reset_nwa(data->nwa); } + + if (vgmstream->layout_type==layout_aix) { + aix_codec_data *data = vgmstream->codec_data; + int i; + + data->current_segment = 0; + for (i=0;isegment_count*data->stream_count;i++) { + reset_vgmstream(data->adxs[i]); + } + } + + if (vgmstream->layout_type==layout_aax) { + aax_codec_data *data = vgmstream->codec_data; + int i; + + data->current_segment = 0; + for (i=0;isegment_count;i++) { + reset_vgmstream(data->adxs[i]); + } + } + if (vgmstream->layout_type==layout_scd_int) { scd_int_codec_data *data = vgmstream->codec_data; int i; - for (i=0;isubstream_count;i++) - { + for (i=0;isubstream_count;i++) { reset_vgmstream(data->substreams[i]); } } - } -/* simply allocate memory for the VGMSTREAM and its channels */ +/* Allocate memory and setup a VGMSTREAM */ VGMSTREAM * allocate_vgmstream(int channel_count, int looped) { VGMSTREAM * vgmstream; VGMSTREAM * start_vgmstream; @@ -705,7 +686,7 @@ void close_vgmstream(VGMSTREAM * vgmstream) { free_hca(vgmstream->codec_data); vgmstream->codec_data = NULL; } - + if (vgmstream->coding_type==coding_EA_MT) { free_ea_mt(vgmstream->codec_data); vgmstream->codec_data = NULL; @@ -780,53 +761,6 @@ void close_vgmstream(VGMSTREAM * vgmstream) { } } - if (vgmstream->layout_type==layout_aix) { - aix_codec_data *data = (aix_codec_data *) vgmstream->codec_data; - - if (data) { - if (data->adxs) { - int i; - for (i=0;isegment_count*data->stream_count;i++) { - - /* note that the AIX close_streamfile won't do anything but - * deallocate itself, there is only one open file and that - * is in vgmstream->ch[0].streamfile */ - close_vgmstream(data->adxs[i]); - } - free(data->adxs); - } - if (data->sample_counts) { - free(data->sample_counts); - } - - free(data); - } - vgmstream->codec_data = NULL; - } - if (vgmstream->layout_type==layout_aax) { - aax_codec_data *data = (aax_codec_data *) vgmstream->codec_data; - - if (data) { - if (data->adxs) { - int i; - for (i=0;isegment_count;i++) { - - /* note that the AAX close_streamfile won't do anything but - * deallocate itself, there is only one open file and that - * is in vgmstream->ch[0].streamfile */ - close_vgmstream(data->adxs[i]); - } - free(data->adxs); - } - if (data->sample_counts) { - free(data->sample_counts); - } - - free(data); - } - vgmstream->codec_data = NULL; - } - if ( vgmstream->coding_type == coding_NWA0 || vgmstream->coding_type == coding_NWA1 || @@ -842,6 +776,51 @@ void close_vgmstream(VGMSTREAM * vgmstream) { vgmstream->codec_data = NULL; } + + if (vgmstream->layout_type==layout_aix) { + aix_codec_data *data = (aix_codec_data *) vgmstream->codec_data; + + if (data) { + if (data->adxs) { + int i; + for (i=0;isegment_count*data->stream_count;i++) { + /* note that the close_streamfile won't do anything but deallocate itself, + * there is only one open file in vgmstream->ch[0].streamfile */ + close_vgmstream(data->adxs[i]); + } + free(data->adxs); + } + if (data->sample_counts) { + free(data->sample_counts); + } + + free(data); + } + vgmstream->codec_data = NULL; + } + + if (vgmstream->layout_type==layout_aax) { + aax_codec_data *data = (aax_codec_data *) vgmstream->codec_data; + + if (data) { + if (data->adxs) { + int i; + for (i=0;isegment_count;i++) { + /* note that the close_streamfile won't do anything but deallocate itself, + * there is only one open file in vgmstream->ch[0].streamfile */ + close_vgmstream(data->adxs[i]); + } + free(data->adxs); + } + if (data->sample_counts) { + free(data->sample_counts); + } + + free(data); + } + vgmstream->codec_data = NULL; + } + if (vgmstream->layout_type==layout_scd_int) { scd_int_codec_data *data = (scd_int_codec_data *) vgmstream->codec_data; @@ -849,10 +828,8 @@ void close_vgmstream(VGMSTREAM * vgmstream) { if (data->substreams) { int i; for (i=0;isubstream_count;i++) { - - /* note that the scd_int close_streamfile won't do anything - * but deallocate itself, there is only one open file and - * that is in vgmstream->ch[0].streamfile */ + /* note that the close_streamfile won't do anything but deallocate itself, + * there is only one open file in vgmstream->ch[0].streamfile */ close_vgmstream(data->substreams[i]); if(data->intfiles[i]) close_streamfile(data->intfiles[i]); } @@ -865,6 +842,7 @@ void close_vgmstream(VGMSTREAM * vgmstream) { vgmstream->codec_data = NULL; } + /* now that the special cases have had their chance, clean up the standard items */ { int i,j; @@ -989,7 +967,7 @@ void render_vgmstream(sample * buffer, int32_t sample_count, VGMSTREAM * vgmstre } } -/* get the size in samples of a single frame (1 or N channels), for interleaved/blocked layouts */ +/* Get the number of samples of a single frame (smallest self-contained sample group, 1/N channels) */ int get_vgmstream_samples_per_frame(VGMSTREAM * vgmstream) { switch (vgmstream->coding_type) { case coding_CRI_ADX: @@ -1141,16 +1119,7 @@ int get_vgmstream_samples_per_frame(VGMSTREAM * vgmstream) { } } -int get_vgmstream_samples_per_shortframe(VGMSTREAM * vgmstream) { - switch (vgmstream->coding_type) { - case coding_NDS_IMA: - return (vgmstream->interleave_smallblock_size-4)*2; - default: - return get_vgmstream_samples_per_frame(vgmstream); - } -} - -/* get the data size of a single frame (1 or N channels), for interleaved/blocked layouts */ +/* Get the number of bytes of a single frame (smallest self-contained byte group, 1/N channels) */ int get_vgmstream_frame_size(VGMSTREAM * vgmstream) { switch (vgmstream->coding_type) { case coding_CRI_ADX: @@ -1275,6 +1244,15 @@ int get_vgmstream_frame_size(VGMSTREAM * vgmstream) { } } +/* In NDS IMA the frame size is the block size, so the last one is short */ +int get_vgmstream_samples_per_shortframe(VGMSTREAM * vgmstream) { + switch (vgmstream->coding_type) { + case coding_NDS_IMA: + return (vgmstream->interleave_smallblock_size-4)*2; + default: + return get_vgmstream_samples_per_frame(vgmstream); + } +} int get_vgmstream_shortframe_size(VGMSTREAM * vgmstream) { switch (vgmstream->coding_type) { case coding_NDS_IMA: @@ -1284,6 +1262,7 @@ int get_vgmstream_shortframe_size(VGMSTREAM * vgmstream) { } } +/* Special case for to decode from a buffer */ void decode_vgmstream_mem(VGMSTREAM * vgmstream, int samples_written, int samples_to_do, sample * buffer, uint8_t * data, int channel) { switch (vgmstream->coding_type) { @@ -1298,6 +1277,8 @@ void decode_vgmstream_mem(VGMSTREAM * vgmstream, int samples_written, int sample } } +/* Decode samples into the buffer. Assume that we have written samples_written into the + * buffer already, and we have samples_to_do consecutive samples ahead of us. */ void decode_vgmstream(VGMSTREAM * vgmstream, int samples_written, int samples_to_do, sample * buffer) { int chan; @@ -1873,6 +1854,7 @@ void decode_vgmstream(VGMSTREAM * vgmstream, int samples_written, int samples_to } } +/* Calculate number of consecutive samples to do (taking into account stopping for loop start and end) */ int vgmstream_samples_to_do(int samples_this_block, int samples_per_frame, VGMSTREAM * vgmstream) { int samples_to_do; int samples_left_this_block; @@ -1904,7 +1886,7 @@ int vgmstream_samples_to_do(int samples_this_block, int samples_per_frame, VGMST return samples_to_do; } -/* loop if end sample is reached, and return 1 if we did loop */ +/* Detect loop start and save values, or detect loop end and restore (loop back). Returns 1 if loop was done. */ int vgmstream_do_loop(VGMSTREAM * vgmstream) { /*if (!vgmstream->loop_flag) return 0;*/ @@ -2026,7 +2008,8 @@ int vgmstream_do_loop(VGMSTREAM * vgmstream) { return 0; /* not looped */ } -/* build a descriptive string */ +/* Write a description of the stream into array pointed by desc, which must be length bytes long. + * Will always be null-terminated if length > 0 */ void describe_vgmstream(VGMSTREAM * vgmstream, char * desc, int length) { #define TEMPSIZE 256 char temp[TEMPSIZE]; @@ -2337,67 +2320,45 @@ fail: return; } -static int get_vgmstream_channel_count(VGMSTREAM * vgmstream) +/* average bitrate helper */ +static int get_vgmstream_average_bitrate_channel_count(VGMSTREAM * vgmstream) { + //AAX, AIX, ACM? + if (vgmstream->layout_type==layout_scd_int) { scd_int_codec_data *data = (scd_int_codec_data *) vgmstream->codec_data; - if (data) { - return data->substream_count; - } - else { - return 0; - } + return (data) ? data->substream_count : 0; } #ifdef VGM_USE_VORBIS if (vgmstream->coding_type==coding_ogg_vorbis) { ogg_vorbis_codec_data *data = (ogg_vorbis_codec_data *) vgmstream->codec_data; - - if (data) { - return 1; - } - else { - return 0; - } + return (data) ? 1 : 0; } #endif if (vgmstream->coding_type==coding_CRI_HCA) { hca_codec_data *data = (hca_codec_data *) vgmstream->codec_data; - - if (data) { - return 1; - } - else { - return 0; - } + return (data) ? 1 : 0; } #ifdef VGM_USE_FFMPEG if (vgmstream->coding_type==coding_FFmpeg) { ffmpeg_codec_data *data = (ffmpeg_codec_data *) vgmstream->codec_data; - - if (data) { - return 1; - } - else { - return 0; - } + return (data) ? 1 : 0; } #endif #if defined(VGM_USE_MP4V2) && defined(VGM_USE_FDKAAC) if (vgmstream->coding_type==coding_MP4_AAC) { mp4_aac_codec_data *data = (mp4_aac_codec_data *) vgmstream->codec_data; - if (data) { - return 1; - } - else { - return 0; - } + return (data) ? 1 : 0; } #endif return vgmstream->channels; } -static STREAMFILE * get_vgmstream_streamfile(VGMSTREAM * vgmstream, int channel) +/* average bitrate helper */ +static STREAMFILE * get_vgmstream_average_bitrate_channel_streamfile(VGMSTREAM * vgmstream, int channel) { + //AAX, AIX, ACM? + if (vgmstream->layout_type==layout_scd_int) { scd_int_codec_data *data = (scd_int_codec_data *) vgmstream->codec_data; return data->intfiles[channel]; @@ -2405,19 +2366,16 @@ static STREAMFILE * get_vgmstream_streamfile(VGMSTREAM * vgmstream, int channel) #ifdef VGM_USE_VORBIS if (vgmstream->coding_type==coding_ogg_vorbis) { ogg_vorbis_codec_data *data = (ogg_vorbis_codec_data *) vgmstream->codec_data; - return data->ov_streamfile.streamfile; } #endif if (vgmstream->coding_type==coding_CRI_HCA) { hca_codec_data *data = (hca_codec_data *) vgmstream->codec_data; - return data->streamfile; } #ifdef VGM_USE_FFMPEG if (vgmstream->coding_type==coding_FFmpeg) { ffmpeg_codec_data *data = (ffmpeg_codec_data *) vgmstream->codec_data; - return data->streamfile; } #endif @@ -2430,42 +2388,41 @@ static STREAMFILE * get_vgmstream_streamfile(VGMSTREAM * vgmstream, int channel) return vgmstream->ch[channel].streamfile; } -static int get_vgmstream_channel_average_bitrate(STREAMFILE * streamfile, int sample_rate, int length_samples) -{ +static int get_vgmstream_average_bitrate_from_streamfile(STREAMFILE * streamfile, int sample_rate, int length_samples) { + // todo: not correct in subsongs or formats which only use part of the data return (int)((int64_t)get_streamfile_size(streamfile) * 8 * sample_rate / length_samples); } -int get_vgmstream_average_bitrate(VGMSTREAM * vgmstream) -{ +/* Return the average bitrate in bps of all unique files contained within this stream. */ +int get_vgmstream_average_bitrate(VGMSTREAM * vgmstream) { char path_current[PATH_LIMIT]; char path_compare[PATH_LIMIT]; - + unsigned int i, j; int bitrate = 0; int sample_rate = vgmstream->sample_rate; int length_samples = vgmstream->num_samples; - int channels = get_vgmstream_channel_count(vgmstream); + int channels = get_vgmstream_average_bitrate_channel_count(vgmstream); STREAMFILE * streamFile; if (!sample_rate || !channels || !length_samples) return 0; - + if (channels >= 1) { - streamFile = get_vgmstream_streamfile(vgmstream, 0); + streamFile = get_vgmstream_average_bitrate_channel_streamfile(vgmstream, 0); if (streamFile) { - bitrate += get_vgmstream_channel_average_bitrate(streamFile, sample_rate, length_samples); + bitrate += get_vgmstream_average_bitrate_from_streamfile(streamFile, sample_rate, length_samples); } } - for (i = 1; i < channels; ++i) - { - streamFile = get_vgmstream_streamfile(vgmstream, i); + /* Compares files by absolute paths, so bitrate doesn't multiply when the same STREAMFILE is reopened per channel */ + for (i = 1; i < channels; ++i) { + streamFile = get_vgmstream_average_bitrate_channel_streamfile(vgmstream, i); if (!streamFile) continue; streamFile->get_name(streamFile, path_current, sizeof(path_current)); - for (j = 0; j < i; ++j) - { - STREAMFILE * compareFile = get_vgmstream_streamfile(vgmstream, j); + for (j = 0; j < i; ++j) { + STREAMFILE * compareFile = get_vgmstream_average_bitrate_channel_streamfile(vgmstream, j); if (!compareFile) continue; streamFile->get_name(compareFile, path_compare, sizeof(path_compare)); @@ -2473,9 +2430,9 @@ int get_vgmstream_average_bitrate(VGMSTREAM * vgmstream) break; } if (j == i) - bitrate += get_vgmstream_channel_average_bitrate(streamFile, sample_rate, length_samples); + bitrate += get_vgmstream_average_bitrate_from_streamfile(streamFile, sample_rate, length_samples); } - + return bitrate; } @@ -2494,8 +2451,10 @@ int vgmstream_open_stream(VGMSTREAM * vgmstream, STREAMFILE *streamFile, off_t s int use_same_offset_per_channel = 0; - /* stream/offsets not needed, scd manages itself */ - if (vgmstream->layout_type == layout_scd_int) + /* stream/offsets not needed, manages themselves */ + if (vgmstream->layout_type == layout_aix || + vgmstream->layout_type == layout_aax || + vgmstream->layout_type == layout_scd_int) return 1; #ifdef VGM_USE_FFMPEG diff --git a/src/vgmstream.h b/src/vgmstream.h index bd454768..5887a3a6 100644 --- a/src/vgmstream.h +++ b/src/vgmstream.h @@ -1177,45 +1177,42 @@ int32_t get_vgmstream_play_samples(double looptimes, double fadeseconds, double /* render! */ void render_vgmstream(sample * buffer, int32_t sample_count, VGMSTREAM * vgmstream); -/* Write a description of the stream into array pointed by desc, - * which must be length bytes long. Will always be null-terminated if length > 0 */ +/* Write a description of the stream into array pointed by desc, which must be length bytes long. + * Will always be null-terminated if length > 0 */ void describe_vgmstream(VGMSTREAM * vgmstream, char * desc, int length); -/* Return the average bitrate in bps of all unique files contained within this - * stream. Compares files by absolute paths. */ +/* Return the average bitrate in bps of all unique files contained within this stream. */ int get_vgmstream_average_bitrate(VGMSTREAM * vgmstream); -/* List of supported formats and elements in the list, for plugins that need to know. */ +/* List supported formats and return elements in the list, for plugins that need to know. */ const char ** vgmstream_get_formats(size_t * size); /* -------------------------------------------------------------------------*/ /* vgmstream "private" API */ /* -------------------------------------------------------------------------*/ -/* allocate a VGMSTREAM and channel stuff */ +/* Allocate memory and setup a VGMSTREAM */ VGMSTREAM * allocate_vgmstream(int channel_count, int looped); -/* smallest self-contained group of samples is a frame */ +/* Get the number of samples of a single frame (smallest self-contained sample group, 1/N channels) */ int get_vgmstream_samples_per_frame(VGMSTREAM * vgmstream); -/* number of bytes per frame */ +/* Get the number of bytes of a single frame (smallest self-contained byte group, 1/N channels) */ int get_vgmstream_frame_size(VGMSTREAM * vgmstream); -/* in NDS IMA the frame size is the block size, so the last one is short */ +/* In NDS IMA the frame size is the block size, so the last one is short */ int get_vgmstream_samples_per_shortframe(VGMSTREAM * vgmstream); int get_vgmstream_shortframe_size(VGMSTREAM * vgmstream); -/* Assume that we have written samples_written into the buffer already, and we have samples_to_do consecutive - * samples ahead of us. Decode those samples into the buffer. */ -void decode_vgmstream(VGMSTREAM * vgmstream, int samples_written, int samples_to_do, sample * buffer); - -/* Assume additionally that we have samples_to_do consecutive samples in "data", - * and this this is for channel number "channel" */ +/* Special case for to decode from a buffer */ void decode_vgmstream_mem(VGMSTREAM * vgmstream, int samples_written, int samples_to_do, sample * buffer, uint8_t * data, int channel); -/* calculate number of consecutive samples to do (taking into account stopping for loop start and end) */ +/* Decode samples into the buffer. Assume that we have written samples_written into the + * buffer already, and we have samples_to_do consecutive samples ahead of us. */ +void decode_vgmstream(VGMSTREAM * vgmstream, int samples_written, int samples_to_do, sample * buffer); + +/* Calculate number of consecutive samples to do (taking into account stopping for loop start and end) */ int vgmstream_samples_to_do(int samples_this_block, int samples_per_frame, VGMSTREAM * vgmstream); -/* Detect start and save values, also detect end and restore values. Only works on exact sample values. - * Returns 1 if loop was done. */ +/* Detect loop start and save values, or detect loop end and restore (loop back). Returns 1 if loop was done. */ int vgmstream_do_loop(VGMSTREAM * vgmstream); /* Open the stream for reading at offset (standarized taking into account layouts, channels and so on).