From 95a50a31688d8abbd0b5940795fc01834e346325 Mon Sep 17 00:00:00 2001 From: bnnm Date: Sun, 29 Sep 2019 20:09:28 +0200 Subject: [PATCH] Minor cleanup --- cli/vgmstream_cli.c | 6 +++++- src/coding/mpeg_decoder.c | 23 ++++++++++------------- src/meta/xmv_valve.c | 6 +++--- src/streamfile.c | 32 +++++++++++++++++++++----------- src/util.c | 8 ++++++++ src/util.h | 3 ++- 6 files changed, 49 insertions(+), 29 deletions(-) diff --git a/cli/vgmstream_cli.c b/cli/vgmstream_cli.c index 19f89f7a..effa93f1 100644 --- a/cli/vgmstream_cli.c +++ b/cli/vgmstream_cli.c @@ -435,6 +435,10 @@ int main(int argc, char ** argv) { fprintf(stderr,"failed to open %s for output\n",cfg.outfilename); goto fail; } + + /* no improvement */ + //setvbuf(outfile, NULL, _IOFBF, SAMPLE_BUFFER_SIZE * sizeof(sample_t) * input_channels); + //setvbuf(outfile, NULL, _IONBF, 0); } @@ -550,7 +554,7 @@ int main(int argc, char ** argv) { fwrite(buf + j*channels + (cfg.only_stereo*2), sizeof(sample_t), 2, outfile); } } else { - fwrite(buf, sizeof(sample_t) * channels, to_get, outfile); + fwrite(buf, sizeof(sample_t), to_get * channels, outfile); } } diff --git a/src/coding/mpeg_decoder.c b/src/coding/mpeg_decoder.c index 528d02e5..e802d418 100644 --- a/src/coding/mpeg_decoder.c +++ b/src/coding/mpeg_decoder.c @@ -234,18 +234,19 @@ void decode_mpeg(VGMSTREAM * vgmstream, sample_t * outbuf, int32_t samples_to_do */ static void decode_mpeg_standard(VGMSTREAMCHANNEL *stream, mpeg_codec_data * data, sample_t * outbuf, int32_t samples_to_do, int channels) { int samples_done = 0; - mpg123_handle *m = data->m; + unsigned char *outbytes = (unsigned char *)outbuf; while (samples_done < samples_to_do) { size_t bytes_done; - int rc; + int rc, bytes_to_do; /* read more raw data */ if (!data->buffer_full) { data->bytes_in_buffer = read_streamfile(data->buffer,stream->offset,data->buffer_size,stream->streamfile); /* end of stream, fill rest with 0s */ - if (!data->bytes_in_buffer) { + if (data->bytes_in_buffer <= 0) { + VGM_ASSERT(samples_to_do < samples_done, "MPEG: end of stream, filling %i\n", (samples_to_do - samples_done)); memset(outbuf + samples_done * channels, 0, (samples_to_do - samples_done) * channels * sizeof(sample)); break; } @@ -256,30 +257,26 @@ static void decode_mpeg_standard(VGMSTREAMCHANNEL *stream, mpeg_codec_data * dat stream->offset += data->bytes_in_buffer; } + bytes_to_do = (samples_to_do-samples_done)*sizeof(sample)*channels; + /* feed new raw data to the decoder if needed, copy decoded results to output */ if (!data->buffer_used) { - rc = mpg123_decode(m, - data->buffer,data->bytes_in_buffer, - (unsigned char *)(outbuf+samples_done*channels), - (samples_to_do-samples_done)*sizeof(sample)*channels, - &bytes_done); + rc = mpg123_decode(data->m, data->buffer,data->bytes_in_buffer, outbytes, bytes_to_do, &bytes_done); data->buffer_used = 1; } else { - rc = mpg123_decode(m, - NULL,0, - (unsigned char *)(outbuf+samples_done*channels), - (samples_to_do-samples_done)*sizeof(sample)*channels, - &bytes_done); + rc = mpg123_decode(data->m, NULL,0, outbytes, bytes_to_do, &bytes_done); } /* not enough raw data, request more */ if (rc == MPG123_NEED_MORE) { data->buffer_full = 0; } + VGM_ASSERT(rc != MPG123_NEED_MORE && rc != MPG123_OK, "MPEG: error %i\n", rc); /* update copied samples */ samples_done += bytes_done/sizeof(sample)/channels; + outbytes += bytes_done; } } diff --git a/src/meta/xmv_valve.c b/src/meta/xmv_valve.c index 99c400b4..6bc81a28 100644 --- a/src/meta/xmv_valve.c +++ b/src/meta/xmv_valve.c @@ -7,7 +7,7 @@ VGMSTREAM* init_vgmstream_xmv_valve(STREAMFILE* streamFile) { VGMSTREAM* vgmstream = NULL; int32_t loop_start; uint32_t start_offset, data_size, sample_rate, num_samples; - uint16_t loop_block, loop_start_skip, loop_end_skip; + uint16_t /*loop_block, loop_start_skip,*/ loop_end_skip; uint8_t format, freq_mode, channels; int loop_flag; @@ -29,8 +29,8 @@ VGMSTREAM* init_vgmstream_xmv_valve(STREAMFILE* streamFile) { loop_start = read_32bitBE(0x1c, streamFile); /* XMA only */ - loop_block = read_16bitBE(0x20, streamFile); - loop_start_skip = read_16bitBE(0x22, streamFile); + //loop_block = read_16bitBE(0x20, streamFile); + //loop_start_skip = read_16bitBE(0x22, streamFile); loop_end_skip = read_16bitBE(0x24, streamFile); format = read_8bit(0x28, streamFile); diff --git a/src/streamfile.c b/src/streamfile.c index b0f86aa1..bdbb2412 100644 --- a/src/streamfile.c +++ b/src/streamfile.c @@ -18,15 +18,15 @@ typedef struct { size_t buffersize; /* max buffer size */ size_t validsize; /* current buffer size */ size_t filesize; /* buffered file size */ -} STDIOSTREAMFILE; +} STDIO_STREAMFILE; static STREAMFILE * open_stdio_streamfile_buffer(const char * const filename, size_t buffersize); static STREAMFILE * open_stdio_streamfile_buffer_by_file(FILE *infile,const char * const filename, size_t buffersize); -static size_t read_stdio(STDIOSTREAMFILE *streamfile,uint8_t * dest, off_t offset, size_t length) { +static size_t read_stdio(STDIO_STREAMFILE *streamfile,uint8_t * dest, off_t offset, size_t length) { size_t length_read_total = 0; - if (!streamfile || !streamfile->infile || !dest || length <= 0 || offset < 0) + if (!streamfile->infile || !dest || length <= 0 || offset < 0) return 0; /* is the part of the requested length in the buffer? */ @@ -45,6 +45,11 @@ static size_t read_stdio(STDIOSTREAMFILE *streamfile,uint8_t * dest, off_t offse dest += length_to_read; } +#ifdef VGM_DEBUG_OUTPUT + if (offset < streamfile->buffer_offset) { + VGM_LOG("STDIO: rebuffer, requested %lx vs %lx (sf %x)\n", offset, streamfile->buffer_offset, (uint32_t)streamfile); + } +#endif /* read the rest of the requested length */ while (length > 0) { @@ -99,24 +104,24 @@ static size_t read_stdio(STDIOSTREAMFILE *streamfile,uint8_t * dest, off_t offse streamfile->offset = offset; /* last fread offset */ return length_read_total; } -static size_t get_size_stdio(STDIOSTREAMFILE * streamfile) { +static size_t get_size_stdio(STDIO_STREAMFILE * streamfile) { return streamfile->filesize; } -static off_t get_offset_stdio(STDIOSTREAMFILE *streamfile) { +static off_t get_offset_stdio(STDIO_STREAMFILE *streamfile) { return streamfile->offset; } -static void get_name_stdio(STDIOSTREAMFILE *streamfile,char *buffer,size_t length) { +static void get_name_stdio(STDIO_STREAMFILE *streamfile,char *buffer,size_t length) { strncpy(buffer,streamfile->name,length); buffer[length-1]='\0'; } -static void close_stdio(STDIOSTREAMFILE * streamfile) { +static void close_stdio(STDIO_STREAMFILE * streamfile) { if (streamfile->infile) fclose(streamfile->infile); free(streamfile->buffer); free(streamfile); } -static STREAMFILE *open_stdio(STDIOSTREAMFILE *streamFile,const char * const filename,size_t buffersize) { +static STREAMFILE *open_stdio(STDIO_STREAMFILE *streamFile,const char * const filename,size_t buffersize) { if (!filename) return NULL; @@ -143,12 +148,12 @@ static STREAMFILE *open_stdio(STDIOSTREAMFILE *streamFile,const char * const fil static STREAMFILE * open_stdio_streamfile_buffer_by_file(FILE *infile, const char * const filename, size_t buffersize) { uint8_t * buffer = NULL; - STDIOSTREAMFILE * streamfile = NULL; + STDIO_STREAMFILE * streamfile = NULL; buffer = calloc(buffersize,1); if (!buffer) goto fail; - streamfile = calloc(1,sizeof(STDIOSTREAMFILE)); + streamfile = calloc(1,sizeof(STDIO_STREAMFILE)); if (!streamfile) goto fail; streamfile->sf.read = (void*)read_stdio; @@ -236,7 +241,7 @@ typedef struct { static size_t buffer_read(BUFFER_STREAMFILE *streamfile, uint8_t * dest, off_t offset, size_t length) { size_t length_read_total = 0; - if (!streamfile || !dest || length <= 0 || offset < 0) + if (!dest || length <= 0 || offset < 0) return 0; /* is the part of the requested length in the buffer? */ @@ -255,6 +260,11 @@ static size_t buffer_read(BUFFER_STREAMFILE *streamfile, uint8_t * dest, off_t o dest += length_to_read; } +#ifdef VGM_DEBUG_OUTPUT + if (offset < streamfile->buffer_offset) { + VGM_LOG("BUFFER: rebuffer, requested %lx vs %lx (sf %x)\n", offset, streamfile->buffer_offset, (uint32_t)streamfile); + } +#endif /* read the rest of the requested length */ while (length > 0) { diff --git a/src/util.c b/src/util.c index bda83e8b..95b58a4f 100644 --- a/src/util.c +++ b/src/util.c @@ -101,14 +101,22 @@ void put_32bitBE(uint8_t * buf, int32_t i) { } void swap_samples_le(sample_t *buf, int count) { + /* Windows can't be BE... I think */ +#if !defined(_WIN32) +#if !defined(__BYTE_ORDER__) || __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__ int i; for (i = 0; i < count; i++) { + /* 16b sample in memory: aabb where aa=MSB, bb=LSB */ uint8_t b0 = buf[i] & 0xff; uint8_t b1 = buf[i] >> 8; uint8_t *p = (uint8_t*)&(buf[i]); + /* 16b sample in buffer: bbaa where bb=LSB, aa=MSB */ p[0] = b0; p[1] = b1; + /* when endianness is LE, buffer has bbaa already so this function can be skipped */ } +#endif +#endif } /* length is maximum length of dst. dst will always be null-terminated if diff --git a/src/util.h b/src/util.h index 71d99313..8b423947 100644 --- a/src/util.h +++ b/src/util.h @@ -79,7 +79,8 @@ static inline int round10(int val) { * 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); +/* swap samples in machine endianness to little endian (useful to write .wav) */ +void swap_samples_le(sample_t *buf, int count); void concatn(int length, char * dst, const char * src);