From 2969db25de1960036e2dd29dd340a54132ecf6da Mon Sep 17 00:00:00 2001 From: bnnm Date: Sat, 19 Oct 2019 11:11:09 +0200 Subject: [PATCH] Clean some streamfile code --- src/coding/coding_utils.c | 21 ++------- src/meta/acb.c | 21 +++------ src/meta/aix.c | 16 +++---- src/meta/aix_streamfile.h | 32 +++----------- src/meta/nub.c | 90 +++++++++++++-------------------------- 5 files changed, 55 insertions(+), 125 deletions(-) diff --git a/src/coding/coding_utils.c b/src/coding/coding_utils.c index fbb6db35..fa83dbd8 100644 --- a/src/coding/coding_utils.c +++ b/src/coding/coding_utils.c @@ -1160,25 +1160,12 @@ int w_bits(vgm_bitstream * ob, int num_bits, uint32_t value) { /* ******************************************** */ STREAMFILE* setup_subfile_streamfile(STREAMFILE *sf, off_t subfile_offset, size_t subfile_size, const char* extension) { - STREAMFILE *temp_sf = NULL, *new_sf = NULL; + STREAMFILE *new_sf = NULL; new_sf = open_wrap_streamfile(sf); - if (!new_sf) goto fail; - temp_sf = new_sf; - - new_sf = open_clamp_streamfile(temp_sf, subfile_offset, subfile_size); - if (!new_sf) goto fail; - temp_sf = new_sf; - + new_sf = open_clamp_streamfile_f(new_sf, subfile_offset, subfile_size); if (extension) { - new_sf = open_fakename_streamfile(temp_sf, NULL, extension); - if (!new_sf) goto fail; - temp_sf = new_sf; + new_sf = open_fakename_streamfile_f(new_sf, NULL, extension); } - - return temp_sf; - -fail: - close_streamfile(temp_sf); - return NULL; + return new_sf; } diff --git a/src/meta/acb.c b/src/meta/acb.c index f25ed1d0..bfe06d88 100644 --- a/src/meta/acb.c +++ b/src/meta/acb.c @@ -70,24 +70,15 @@ fail: /* ************************************** */ +//todo maybe use reopen sf? since internal buffer is going to be read #define ACB_TABLE_BUFFER_SIZE 0x4000 -STREAMFILE* setup_acb_streamfile(STREAMFILE *streamFile, size_t buffer_size) { - STREAMFILE *temp_streamFile = NULL, *new_streamFile = NULL; +STREAMFILE* setup_acb_streamfile(STREAMFILE *sf, size_t buffer_size) { + STREAMFILE *new_sf = NULL; - new_streamFile = open_wrap_streamfile(streamFile); - if (!new_streamFile) goto fail; - temp_streamFile = new_streamFile; - - new_streamFile = open_buffer_streamfile(temp_streamFile, buffer_size); - if (!new_streamFile) goto fail; - temp_streamFile = new_streamFile; - - return temp_streamFile; - -fail: - close_streamfile(temp_streamFile); - return NULL; + new_sf = open_wrap_streamfile(sf); + new_sf = open_buffer_streamfile_f(new_sf, buffer_size); + return new_sf; } diff --git a/src/meta/aix.c b/src/meta/aix.c index 93ab5dc4..41ce1145 100644 --- a/src/meta/aix.c +++ b/src/meta/aix.c @@ -110,7 +110,7 @@ static VGMSTREAM *build_layered_vgmstream(STREAMFILE *streamFile, off_t segment_ VGMSTREAM *vgmstream = NULL; layered_layout_data* data = NULL; int i; - STREAMFILE* temp_streamFile = NULL; + STREAMFILE* temp_sf = NULL; /* build layers */ @@ -119,17 +119,17 @@ static VGMSTREAM *build_layered_vgmstream(STREAMFILE *streamFile, off_t segment_ for (i = 0; i < layer_count; i++) { /* build the layer STREAMFILE */ - temp_streamFile = setup_aix_streamfile(streamFile, segment_offset, segment_size, i, "adx"); - if (!temp_streamFile) goto fail; + temp_sf = setup_aix_streamfile(streamFile, segment_offset, segment_size, i, "adx"); + if (!temp_sf) goto fail; /* build the sub-VGMSTREAM */ - data->layers[i] = init_vgmstream_adx(temp_streamFile); + data->layers[i] = init_vgmstream_adx(temp_sf); if (!data->layers[i]) goto fail; - data->layers[i]->stream_size = get_streamfile_size(temp_streamFile); + data->layers[i]->stream_size = get_streamfile_size(temp_sf); - close_streamfile(temp_streamFile); - temp_streamFile = NULL; + close_streamfile(temp_sf); + temp_sf = NULL; } if (!setup_layout_layered(data)) @@ -145,7 +145,7 @@ static VGMSTREAM *build_layered_vgmstream(STREAMFILE *streamFile, off_t segment_ fail: if (!vgmstream) free_layout_layered(data); close_vgmstream(vgmstream); - close_streamfile(temp_streamFile); + close_streamfile(temp_sf); return NULL; } diff --git a/src/meta/aix_streamfile.h b/src/meta/aix_streamfile.h index e60537a8..cf560f18 100644 --- a/src/meta/aix_streamfile.h +++ b/src/meta/aix_streamfile.h @@ -112,40 +112,22 @@ static size_t aix_io_size(STREAMFILE *streamfile, aix_io_data* data) { } /* Handles deinterleaving of AIX blocked layer streams */ -static STREAMFILE* setup_aix_streamfile(STREAMFILE *streamFile, off_t stream_offset, size_t stream_size, int layer_number, const char* extension) { - STREAMFILE *temp_streamFile = NULL, *new_streamFile = NULL; +static STREAMFILE* setup_aix_streamfile(STREAMFILE *sf, off_t stream_offset, size_t stream_size, int layer_number, const char* extension) { + STREAMFILE *new_sf = NULL; aix_io_data io_data = {0}; - size_t io_data_size = sizeof(aix_io_data); io_data.stream_offset = stream_offset; io_data.stream_size = stream_size; io_data.layer_number = layer_number; io_data.logical_offset = -1; /* force reset */ - /* setup subfile */ - new_streamFile = open_wrap_streamfile(streamFile); - if (!new_streamFile) goto fail; - temp_streamFile = new_streamFile; - - new_streamFile = open_io_streamfile(new_streamFile, &io_data,io_data_size, aix_io_read,aix_io_size); - if (!new_streamFile) goto fail; - temp_streamFile = new_streamFile; - - new_streamFile = open_buffer_streamfile(new_streamFile,0); - if (!new_streamFile) goto fail; - temp_streamFile = new_streamFile; - + new_sf = open_wrap_streamfile(sf); + new_sf = open_io_streamfile_f(new_sf, &io_data, sizeof(aix_io_data), aix_io_read, aix_io_size); + new_sf = open_buffer_streamfile_f(new_sf, 0); if (extension) { - new_streamFile = open_fakename_streamfile(temp_streamFile, NULL,extension); - if (!new_streamFile) goto fail; - temp_streamFile = new_streamFile; + new_sf = open_fakename_streamfile_f(new_sf, NULL, extension); } - - return temp_streamFile; - -fail: - close_streamfile(temp_streamFile); - return NULL; + return new_sf; } #endif /* _AIX_STREAMFILE_H_ */ diff --git a/src/meta/nub.c b/src/meta/nub.c index 96e73817..cc5b39f9 100644 --- a/src/meta/nub.c +++ b/src/meta/nub.c @@ -2,12 +2,12 @@ #include "../coding/coding.h" -static STREAMFILE* make_nub_streamfile(STREAMFILE* streamFile, off_t header_offset, size_t header_size, off_t stream_offset, size_t stream_size, const char* fake_ext); +static STREAMFILE* setup_nub_streamfile(STREAMFILE *sf, off_t header_offset, size_t header_size, off_t stream_offset, size_t stream_size, const char *fake_ext); /* .nub - Namco's nu Sound v2 audio container */ VGMSTREAM * init_vgmstream_nub(STREAMFILE *streamFile) { VGMSTREAM *vgmstream = NULL; - STREAMFILE *temp_streamFile = NULL; + STREAMFILE *temp_sf = NULL; off_t name_offset = 0; size_t name_size = 0; int total_subsongs, target_subsong = streamFile->stream_index; @@ -133,8 +133,8 @@ VGMSTREAM * init_vgmstream_nub(STREAMFILE *streamFile) { //;VGM_LOG("NUB: subfile header=%lx + %x, offset=%lx + %x\n", header_offset, header_size, stream_offset, stream_size); - temp_streamFile = make_nub_streamfile(streamFile, header_offset, header_size, stream_offset, stream_size, fake_ext); - if (!temp_streamFile) goto fail; + temp_sf = setup_nub_streamfile(streamFile, header_offset, header_size, stream_offset, stream_size, fake_ext); + if (!temp_sf) goto fail; } /* get names */ @@ -166,66 +166,36 @@ VGMSTREAM * init_vgmstream_nub(STREAMFILE *streamFile) { } /* init the VGMSTREAM */ - vgmstream = init_vgmstream_function(temp_streamFile); + vgmstream = init_vgmstream_function(temp_sf); if (!vgmstream) goto fail; - vgmstream->stream_size = get_streamfile_size(temp_streamFile); + vgmstream->stream_size = get_streamfile_size(temp_sf); vgmstream->num_streams = total_subsongs; if (name[0] != '\0') strcpy(vgmstream->stream_name, name); - close_streamfile(temp_streamFile); + close_streamfile(temp_sf); return vgmstream; fail: - close_streamfile(temp_streamFile); + close_streamfile(temp_sf); close_vgmstream(vgmstream); return NULL; } /* *********************************************************** */ -static STREAMFILE* make_nub_streamfile(STREAMFILE* streamFile, off_t header_offset, size_t header_size, off_t stream_offset, size_t stream_size, const char* fake_ext) { - STREAMFILE *temp_streamFile = NULL, *new_streamFile = NULL; - STREAMFILE *segment_streamFiles[2] = {0}; - int i; +static STREAMFILE* setup_nub_streamfile(STREAMFILE *sf, off_t header_offset, size_t header_size, off_t stream_offset, size_t stream_size, const char *fake_ext) { + STREAMFILE *new_sf = NULL; + STREAMFILE *multi_sf[2] = {0}; - - new_streamFile = open_wrap_streamfile(streamFile); - if (!new_streamFile) goto fail; - segment_streamFiles[0] = new_streamFile; - - new_streamFile = open_wrap_streamfile(streamFile); - if (!new_streamFile) goto fail; - segment_streamFiles[1] = new_streamFile; - - new_streamFile = open_clamp_streamfile(segment_streamFiles[0], header_offset,header_size); - if (!new_streamFile) goto fail; - segment_streamFiles[0] = new_streamFile; - - new_streamFile = open_clamp_streamfile(segment_streamFiles[1], stream_offset,stream_size); - if (!new_streamFile) goto fail; - segment_streamFiles[1] = new_streamFile; - - new_streamFile = open_multifile_streamfile(segment_streamFiles, 2); - if (!new_streamFile) goto fail; - temp_streamFile = new_streamFile; - - new_streamFile = open_fakename_streamfile(temp_streamFile, NULL, fake_ext); - if (!new_streamFile) goto fail; - temp_streamFile = new_streamFile; - - return temp_streamFile; - -fail: - if (!temp_streamFile) { - for (i = 0; i < 2; i++) { - close_streamfile(segment_streamFiles[i]); - } - } else { - close_streamfile(temp_streamFile); /* closes all segments */ - } - return NULL; + multi_sf[0] = open_wrap_streamfile(sf); + multi_sf[0] = open_clamp_streamfile_f(multi_sf[0], header_offset, header_size); + multi_sf[1] = open_wrap_streamfile(sf); + multi_sf[1] = open_clamp_streamfile_f(multi_sf[1], stream_offset, stream_size); + new_sf = open_multifile_streamfile_f(multi_sf, 2); + new_sf = open_fakename_streamfile_f(new_sf, NULL, fake_ext); + return new_sf; } /* *********************************************************** */ @@ -318,7 +288,7 @@ fail: /* .nub at3 - from Namco NUB archives [Ridge Racer 7 (PS3), Katamari Forever (PS3)] */ VGMSTREAM * init_vgmstream_nub_at3(STREAMFILE *streamFile) { VGMSTREAM * vgmstream = NULL; - STREAMFILE *temp_streamFile = NULL; + STREAMFILE *temp_sf = NULL; off_t subfile_offset = 0; size_t subfile_size = 0; @@ -333,16 +303,16 @@ VGMSTREAM * init_vgmstream_nub_at3(STREAMFILE *streamFile) { subfile_offset = 0x100; subfile_size = read_32bitLE(subfile_offset + 0x04, streamFile) + 0x08; /* RIFF size */ - temp_streamFile = setup_subfile_streamfile(streamFile, subfile_offset,subfile_size, NULL); - if (!temp_streamFile) goto fail; + temp_sf = setup_subfile_streamfile(streamFile, subfile_offset,subfile_size, NULL); + if (!temp_sf) goto fail; - vgmstream = init_vgmstream_riff(temp_streamFile); + vgmstream = init_vgmstream_riff(temp_sf); if (!vgmstream) goto fail; - close_streamfile(temp_streamFile); + close_streamfile(temp_sf); return vgmstream; fail: - close_streamfile(temp_streamFile); + close_streamfile(temp_sf); close_vgmstream(vgmstream); return NULL; } @@ -512,7 +482,7 @@ fail: /* .nub is14 - from Namco NUB archives [Tales of Vesperia (PS3)] */ VGMSTREAM * init_vgmstream_nub_is14(STREAMFILE *streamFile) { VGMSTREAM *vgmstream = NULL; - STREAMFILE *temp_streamFile = NULL; + STREAMFILE *temp_sf = NULL; off_t header_offset, stream_offset; size_t header_size, stream_size, sdat_size; int32_t (*read_32bit)(off_t,STREAMFILE*) = NULL; @@ -542,16 +512,16 @@ VGMSTREAM * init_vgmstream_nub_is14(STREAMFILE *streamFile) { stream_size = sdat_size; - temp_streamFile = make_nub_streamfile(streamFile, header_offset, header_size, stream_offset, stream_size, "bnsf"); - if (!temp_streamFile) goto fail; + temp_sf = setup_nub_streamfile(streamFile, header_offset, header_size, stream_offset, stream_size, "bnsf"); + if (!temp_sf) goto fail; - vgmstream = init_vgmstream_bnsf(temp_streamFile); + vgmstream = init_vgmstream_bnsf(temp_sf); if (!vgmstream) goto fail; - close_streamfile(temp_streamFile); + close_streamfile(temp_sf); return vgmstream; fail: - close_streamfile(temp_streamFile); + close_streamfile(temp_sf); close_vgmstream(vgmstream); return NULL; }