From 5cb3faf69b88f1ee030d89c0d1faa4e1c442b99f Mon Sep 17 00:00:00 2001 From: SmiVan Date: Mon, 21 Dec 2020 14:03:51 +0300 Subject: [PATCH 001/204] Marked unimplemented dither enums in the header file as such. --- include/sndfile.h.in | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/sndfile.h.in b/include/sndfile.h.in index 917ff4b5..970fb260 100644 --- a/include/sndfile.h.in +++ b/include/sndfile.h.in @@ -173,6 +173,7 @@ enum SFC_SET_RAW_START_OFFSET = 0x1090, + /* Commands reserved for dithering, which is not implemented. */ SFC_SET_DITHER_ON_WRITE = 0x10A0, SFC_SET_DITHER_ON_READ = 0x10A1, @@ -386,8 +387,7 @@ typedef struct /* ** Enums and typedefs for adding dither on read and write. -** See the html documentation for sf_command(), SFC_SET_DITHER_ON_WRITE -** and SFC_SET_DITHER_ON_READ. +** Reserved for future implementation. */ enum From 251a435566bd798a45629425f3a61df8d61e3ba1 Mon Sep 17 00:00:00 2001 From: evpobr Date: Tue, 26 Jan 2021 10:21:35 +0500 Subject: [PATCH 002/204] Fix typo Thanks to @dvzrv. --- docs/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/index.md b/docs/index.md index 3779fdfd..ee6bd07f 100644 --- a/docs/index.md +++ b/docs/index.md @@ -194,7 +194,7 @@ long as you abide by [the license](http://www.gnu.org/copyleft/lesser.html). Check latest version on [GitHub Releases page](https://github.com/libsndfile/libsndfile/releases/). -Binatries and source packages are signed by current releaser David Seifert aka +Binaries and source packages are signed by current releaser David Seifert aka @SoapGentoo. You can verify signatures with his public GPG key: ``` From 89bed47c4593afa9985596d3dfa2b911be047673 Mon Sep 17 00:00:00 2001 From: evpobr Date: Sun, 31 Jan 2021 11:39:44 +0500 Subject: [PATCH 003/204] Add CHANGELOG.md Other changes: * Rename the old `NEWS` file to `NEWS.OLD`. * Update documentation links. --- CHANGELOG.md | 25 +++++++++++++++++++++++++ Makefile.am | 2 +- NEWS => NEWS.OLD | 0 docs/_layouts/home.html | 2 +- 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 CHANGELOG.md rename NEWS => NEWS.OLD (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..5bdd82fa --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,25 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +### Added + +* This `CHANGELOG.md`. All notable changes to this project will be documented in + this file. The old `NEWS` file has been renamed to `NEWS.OLD` and is no longer + updated. + +### Changed + +* `SFC_SET_DITHER_ON_READ` and `SFC_SET_DITHER_ON_WRITE` enums comments in + public header (#677). + +### Fixed + +* Typo in `docs/index.md`. + +[Unreleased]: https://github.com/libsndfile/libsndfile/compare/1.0.31...HEAD diff --git a/Makefile.am b/Makefile.am index 0b8bfa14..13ead221 100644 --- a/Makefile.am +++ b/Makefile.am @@ -13,7 +13,7 @@ endif endif EXTRA_DIST = libsndfile.spec.in sndfile.pc.in Scripts/android-configure.sh \ - Scripts/linux-to-win-cross-configure.sh \ + NEWS.OLD CHANGELOG.md Scripts/linux-to-win-cross-configure.sh \ CMakeLists.txt $(cmake_files) Win32 cmake_files = cmake/ClipMode.cmake cmake/FindFLAC.cmake \ diff --git a/NEWS b/NEWS.OLD similarity index 100% rename from NEWS rename to NEWS.OLD diff --git a/docs/_layouts/home.html b/docs/_layouts/home.html index 5a20c136..6c8119d1 100644 --- a/docs/_layouts/home.html +++ b/docs/_layouts/home.html @@ -12,7 +12,7 @@ title: libsndfile home page History -+- Features -+- Similar or related projects -+- - News + News
Development -+- Programming interface -+- From dca6f8abbb160697280203953799e07defbaeacf Mon Sep 17 00:00:00 2001 From: evpobr Date: Mon, 8 Feb 2021 09:18:19 +0500 Subject: [PATCH 004/204] Fix memory leak in caf_read_header() https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=30375 Credit to OSS-Fuzz. --- CHANGELOG.md | 1 + src/caf.c | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bdd82fa..21706791 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,5 +21,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed * Typo in `docs/index.md`. +* Memory leak in `caf_read_header`(), credit to OSS-Fuzz ([issue 30375](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=30375)). [Unreleased]: https://github.com/libsndfile/libsndfile/compare/1.0.31...HEAD diff --git a/src/caf.c b/src/caf.c index 45b1dba8..0eaa8eaa 100644 --- a/src/caf.c +++ b/src/caf.c @@ -416,6 +416,11 @@ caf_read_header (SF_PRIVATE *psf) return SFE_CAF_BAD_PEAK ; } ; + if (psf->peak_info) + { psf_log_printf (psf, "*** Found existing peak info, using last one.\n") ; + free (psf->peak_info) ; + psf->peak_info = NULL ; + } ; if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL) return SFE_MALLOC_FAILED ; From b11477eeff03c087c677579cae8c0e6c84ce4c9d Mon Sep 17 00:00:00 2001 From: evpobr Date: Mon, 8 Feb 2021 16:21:55 +0500 Subject: [PATCH 005/204] Use proper error value for Win32 file handle It is INVALID_HANDLE_VALUE, no idea why we should pretend that it is NULL. --- src/file_io.c | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/src/file_io.c b/src/file_io.c index 9cd9379c..252df427 100644 --- a/src/file_io.c +++ b/src/file_io.c @@ -620,7 +620,7 @@ psf_fopen (SF_PRIVATE *psf) psf->error = 0 ; psf->file.handle = psf_open_handle (&psf->file) ; - if (psf->file.handle == NULL) + if (psf->file.handle == INVALID_HANDLE_VALUE) psf_log_syserr (psf, GetLastError ()) ; return psf->error ; @@ -634,14 +634,14 @@ psf_fclose (SF_PRIVATE *psf) return 0 ; if (psf->file.do_not_close_descriptor) - { psf->file.handle = NULL ; + { psf->file.handle = INVALID_HANDLE_VALUE ; return 0 ; } ; if ((retval = psf_close_handle (psf->file.handle)) == -1) psf_log_syserr (psf, GetLastError ()) ; - psf->file.handle = NULL ; + psf->file.handle = INVALID_HANDLE_VALUE ; return retval ; } /* psf_fclose */ @@ -649,13 +649,13 @@ psf_fclose (SF_PRIVATE *psf) /* USE_WINDOWS_API */ int psf_open_rsrc (SF_PRIVATE *psf) { - if (psf->rsrc.handle != NULL) + if (psf->rsrc.handle != INVALID_HANDLE_VALUE) return 0 ; /* Test for MacOSX style resource fork on HPFS or HPFS+ filesystems. */ snprintf (psf->rsrc.path.c, sizeof (psf->rsrc.path.c), "%s/rsrc", psf->file.path.c) ; psf->error = SFE_NO_ERROR ; - if ((psf->rsrc.handle = psf_open_handle (&psf->rsrc)) != NULL) + if ((psf->rsrc.handle = psf_open_handle (&psf->rsrc)) != INVALID_HANDLE_VALUE) { psf->rsrclength = psf_get_filelen_handle (psf->rsrc.handle) ; return SFE_NO_ERROR ; } ; @@ -666,7 +666,7 @@ psf_open_rsrc (SF_PRIVATE *psf) */ snprintf (psf->rsrc.path.c, sizeof (psf->rsrc.path.c), "%s._%s", psf->file.dir.c, psf->file.name.c) ; psf->error = SFE_NO_ERROR ; - if ((psf->rsrc.handle = psf_open_handle (&psf->rsrc)) != NULL) + if ((psf->rsrc.handle = psf_open_handle (&psf->rsrc)) != INVALID_HANDLE_VALUE) { psf->rsrclength = psf_get_filelen_handle (psf->rsrc.handle) ; return SFE_NO_ERROR ; } ; @@ -677,17 +677,15 @@ psf_open_rsrc (SF_PRIVATE *psf) */ snprintf (psf->rsrc.path.c, sizeof (psf->rsrc.path.c), "%s.AppleDouble/%s", psf->file.dir.c, psf->file.name.c) ; psf->error = SFE_NO_ERROR ; - if ((psf->rsrc.handle = psf_open_handle (&psf->rsrc)) != NULL) + if ((psf->rsrc.handle = psf_open_handle (&psf->rsrc)) != INVALID_HANDLE_VALUE) { psf->rsrclength = psf_get_filelen_handle (psf->rsrc.handle) ; return SFE_NO_ERROR ; } ; /* No resource file found. */ - if (psf->rsrc.handle == NULL) + if (psf->rsrc.handle == INVALID_HANDLE_VALUE) psf_log_syserr (psf, GetLastError ()) ; - psf->rsrc.handle = NULL ; - return psf->error ; } /* psf_open_rsrc */ @@ -738,9 +736,9 @@ psf_get_filelen (SF_PRIVATE *psf) /* USE_WINDOWS_API */ void psf_init_files (SF_PRIVATE *psf) -{ psf->file.handle = NULL ; - psf->rsrc.handle = NULL ; - psf->file.hsaved = NULL ; +{ psf->file.handle = INVALID_HANDLE_VALUE ; + psf->rsrc.handle = INVALID_HANDLE_VALUE ; + psf->file.hsaved = INVALID_HANDLE_VALUE ; } /* psf_init_files */ /* USE_WINDOWS_API */ void @@ -798,9 +796,6 @@ psf_open_handle (PSF_FILE * pfile) handle = CreateFile2 (pfile->path.wc, dwDesiredAccess, dwShareMode, dwCreationDistribution, &cfParams) ; - if (handle == INVALID_HANDLE_VALUE) - return NULL ; - return handle ; #else if (pfile->use_wchar) @@ -824,9 +819,6 @@ psf_open_handle (PSF_FILE * pfile) NULL /* handle to file with attributes to copy */ ) ; - if (handle == INVALID_HANDLE_VALUE) - return NULL ; - return handle ; #endif } /* psf_open_handle */ @@ -860,14 +852,14 @@ psf_log_syserr (SF_PRIVATE *psf, int error) /* USE_WINDOWS_API */ int psf_close_rsrc (SF_PRIVATE *psf) { psf_close_handle (psf->rsrc.handle) ; - psf->rsrc.handle = NULL ; + psf->rsrc.handle = INVALID_HANDLE_VALUE ; return 0 ; } /* psf_close_rsrc */ /* USE_WINDOWS_API */ int psf_set_stdio (SF_PRIVATE *psf) -{ HANDLE handle = NULL ; +{ HANDLE handle = INVALID_HANDLE_VALUE ; int error = 0 ; switch (psf->file.mode) @@ -909,9 +901,7 @@ psf_set_file (SF_PRIVATE *psf, int fd) /* USE_WINDOWS_API */ int psf_file_valid (SF_PRIVATE *psf) -{ if (psf->file.handle == NULL) - return SF_FALSE ; - if (psf->file.handle == INVALID_HANDLE_VALUE) +{ if (psf->file.handle == INVALID_HANDLE_VALUE) return SF_FALSE ; return SF_TRUE ; } /* psf_set_file */ From 06fd6f626e75bc57caa9ff43cfa913b66216cd66 Mon Sep 17 00:00:00 2001 From: evpobr Date: Tue, 9 Feb 2021 12:55:21 +0500 Subject: [PATCH 006/204] Fix more NULL file handle values --- src/file_io.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/file_io.c b/src/file_io.c index 252df427..7bcd907b 100644 --- a/src/file_io.c +++ b/src/file_io.c @@ -783,12 +783,12 @@ psf_open_handle (PSF_FILE * pfile) break ; default : - return NULL ; + return INVALID_HANDLE_VALUE ; } ; #if defined (WINAPI_FAMILY_PARTITION) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM) if (!pfile->use_wchar) - return NULL ; + return INVALID_HANDLE_VALUE ; CREATEFILE2_EXTENDED_PARAMETERS cfParams = { 0 } ; cfParams.dwSize = sizeof (CREATEFILE2_EXTENDED_PARAMETERS) ; @@ -1063,7 +1063,7 @@ psf_ftell (SF_PRIVATE *psf) /* USE_WINDOWS_API */ static int psf_close_handle (HANDLE handle) -{ if (handle == NULL) +{ if (handle == INVALID_HANDLE_VALUE) return 0 ; if (CloseHandle (handle) == 0) From ecb9672aaaf2fbf39ec841460b3f705f72432ca9 Mon Sep 17 00:00:00 2001 From: bobsayshilol Date: Sun, 14 Feb 2021 04:45:07 +0000 Subject: [PATCH 007/204] Fix int normalisation when reading in floats with the replacement reader (#702) * Use the correct scale when normalising an integer read on the fallback floating point path. Without this the values were only being scaled to a short's max value. * Add a second pass over the floating point tests that enables the replacement read functionality. These cover the issue fixed by the previous commit. * Add an entry to CHANGELOG.md for #702. --- CHANGELOG.md | 2 ++ src/float32.c | 2 +- tests/floating_point_test.tpl | 14 ++++++++------ 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21706791..6f63854e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,5 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Typo in `docs/index.md`. * Memory leak in `caf_read_header`(), credit to OSS-Fuzz ([issue 30375](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=30375)). +* Normalisation issue when scaling floating point data to `int` in + `replace_read_f2i`() (#702). [Unreleased]: https://github.com/libsndfile/libsndfile/compare/1.0.31...HEAD diff --git a/src/float32.c b/src/float32.c index a63413d2..e8b0aded 100644 --- a/src/float32.c +++ b/src/float32.c @@ -787,7 +787,7 @@ replace_read_f2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) float scale ; bufferlen = ARRAY_LEN (ubuf.fbuf) ; - scale = (psf->float_int_mult == 0) ? 1.0 : 0x7FFF / psf->float_max ; + scale = (psf->float_int_mult == 0) ? 1.0 : 2147483648.0f / psf->float_max ; while (len > 0) { if (len < bufferlen) diff --git a/tests/floating_point_test.tpl b/tests/floating_point_test.tpl index 7258fa23..bfa7a75f 100644 --- a/tests/floating_point_test.tpl +++ b/tests/floating_point_test.tpl @@ -42,7 +42,7 @@ static void float_scaled_test (const char *filename, int allow_exit, int replace static void double_scaled_test (const char *filename, int allow_exit, int replace_float, int filetype, double target_snr) ; [+ FOR float_type +][+ FOR int_type +][+ FOR endian_type -+]static void [+ (get "float_name") +]_[+ (get "int_name") +]_[+ (get "end_name") +]_test (const char * filename) ; ++]static void [+ (get "float_name") +]_[+ (get "int_name") +]_[+ (get "end_name") +]_test (const char * filename, int replace_float) ; [+ ENDFOR endian_type +][+ ENDFOR int_type +][+ ENDFOR float_type +] @@ -187,10 +187,10 @@ main (int argc, char *argv []) putchar ('\n') ; /* Float int tests. */ -[+ FOR float_type +][+ FOR int_type +][+ FOR endian_type -+] [+ (get "float_name") +]_[+ (get "int_name") +]_[+ (get "end_name") +]_test ("[+ (get "float_name") +]_[+ (get "int_name") +]_[+ (get "end_name") +].au") ; -[+ ENDFOR endian_type +][+ ENDFOR int_type +][+ ENDFOR float_type -+] +[+ FOR float_type +][+ FOR int_type +][+ FOR endian_type +] +[+ (get "float_name") +]_[+ (get "int_name") +]_[+ (get "end_name") +]_test ("[+ (get "float_name") +]_[+ (get "int_name") +]_[+ (get "end_name") +].au", SF_FALSE) ; +[+ (get "float_name") +]_[+ (get "int_name") +]_[+ (get "end_name") +]_test ("[+ (get "float_name") +]_[+ (get "int_name") +]_[+ (get "end_name") +]_replace.au", SF_TRUE) ; +[+ ENDFOR endian_type +][+ ENDFOR int_type +][+ ENDFOR float_type +] return 0 ; } /* main */ @@ -311,7 +311,7 @@ double_scaled_test (const char *filename, int allow_exit, int replace_float, int [+ FOR float_type +][+ FOR int_type +][+ FOR endian_type +] static void -[+ (get "float_name") +]_[+ (get "int_name") +]_[+ (get "end_name") +]_test (const char * filename) +[+ (get "float_name") +]_[+ (get "int_name") +]_[+ (get "end_name") +]_test (const char * filename, int replace_float) { SNDFILE *file ; SF_INFO sfinfo ; int max ; @@ -328,10 +328,12 @@ static void sfinfo.format = [+ (get "end_type") +] | SF_FORMAT_AU | [+ (get "minor_type") +] ; file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; + sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ; test_write_[+ (get "float_name") +]_or_die (file, 0, [+ (get "float_name") +]_data, ARRAY_LEN ([+ (get "float_name") +]_data), __LINE__) ; sf_close (file) ; file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; + sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ; if (sfinfo.frames != ARRAY_LEN ([+ (get "float_name") +]_data)) { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, DFT_DATA_LENGTH) ; From 5ba69401c162c418188d66dbd70e4220905d57be Mon Sep 17 00:00:00 2001 From: bobsayshilol Date: Sun, 14 Feb 2021 05:16:33 +0000 Subject: [PATCH 008/204] Fix pedantic warnings (#703) * Fix warnings about incorrect format specifiers. GCC only wants to see void pointers for %p formatters, so cast as appropriate. * Fix a warning about an invalid enum value. The following warning was observed when compiling with -pedantic: warning: ISO C restricts enumerator values to range of 'int' (2147483648 is too large) The values of these enums don't escape wav_read_header() and are only used to set bits in a bitmask, so simply use the next bit rather than jumping to a huge number. * Give anonymous unions a name. This fixes the 'anonymous unions are a C11 extension' warning when compiling with -pedantic. * Use flexible array members rather than zero sized arrays. This flagged up the following warning when compiling with -pedantic: warning: zero size arrays are an extension [-Wzero-length-array] * Safely return the smallest value of a short. GCC gives the following warning when compiling with -pedantic: warning: overflow in conversion from 'int' to 'short int' changes value from '32768' to '-32768' [-Woverflow] This looks intentional from the surrounding code, so return -32768 in a legal way rather than depending on undefined behaviour. * Use the same min/max approach with all compilers. The GCC specific code gives the following warning from GCC when compiled with -pedantic: warning: ISO C forbids braced-groups within expressions [-pedantic] This code was also a fundamentally different way of calculating min/max since it didn't evaluate the inputs more than once, which meant there was potentially a difference in behaviour when the library was built with different compilers if the evaluation of 'a' or 'b' had side effects. --- regtest/database.c | 4 ++-- src/ALAC/alac_codec.h | 2 +- src/ALAC/alac_decoder.c | 40 ++++++++++++++++++++-------------------- src/alac.c | 18 +++++++++--------- src/common.c | 6 +++--- src/common.h | 14 -------------- src/test_broadcast_var.c | 2 +- src/wav.c | 14 +++++++------- tests/error_test.c | 2 +- 9 files changed, 44 insertions(+), 58 deletions(-) diff --git a/regtest/database.c b/regtest/database.c index 1048ebb0..58a1a52f 100644 --- a/regtest/database.c +++ b/regtest/database.c @@ -267,14 +267,14 @@ db_check_all (REG_DB * db_handle) int db_list_all (REG_DB * db_handle) { - printf ("%s : %p\n", __func__, db_handle) ; + printf ("%s : %p\n", __func__, (void *) db_handle) ; return 0 ; } /* db_list_all */ int db_del_entry (REG_DB * db_handle, const char * entry) { - printf ("%s : %p %s\n", __func__, db_handle, entry) ; + printf ("%s : %p %s\n", __func__, (void *) db_handle, entry) ; return 0 ; } /* db_del_entry */ diff --git a/src/ALAC/alac_codec.h b/src/ALAC/alac_codec.h index c73347d1..e762b279 100644 --- a/src/ALAC/alac_codec.h +++ b/src/ALAC/alac_codec.h @@ -48,7 +48,7 @@ typedef struct alac_decoder_s { int32_t mPredictor [ALAC_FRAME_LENGTH] ; uint16_t mShiftBuffer [ALAC_FRAME_LENGTH] ; - } ; + } u ; uint32_t mNumChannels ; } ALAC_DECODER ; diff --git a/src/ALAC/alac_decoder.c b/src/ALAC/alac_decoder.c index 6c124e65..52b7a486 100644 --- a/src/ALAC/alac_decoder.c +++ b/src/ALAC/alac_decoder.c @@ -110,7 +110,7 @@ alac_decoder_init (ALAC_DECODER *p, void * inMagicCookie, uint32_t inMagicCookie RequireAction (p->mConfig.compatibleVersion <= kALACVersion, return kALAC_IncompatibleVersion ;) ; RequireAction ((p->mConfig.bitDepth >= 8 && p->mConfig.bitDepth <= 32), return kALAC_BadBitWidth ;) ; - RequireAction ((p->mMixBufferU != NULL) && (p->mMixBufferV != NULL) && (p->mPredictor != NULL), + RequireAction ((p->mMixBufferU != NULL) && (p->mMixBufferV != NULL) && (p->u.mPredictor != NULL), status = kALAC_MemFullError ; goto Exit ;) ; } else @@ -247,18 +247,18 @@ alac_decode (ALAC_DECODER *p, struct BitBuffer * bits, int32_t * sampleBuffer, u // decompress set_ag_params (&agParams, p->mConfig.mb, (pb * pbFactorU) / 4, p->mConfig.kb, numSamples, numSamples, p->mConfig.maxRun) ; - status = dyn_decomp (&agParams, bits, p->mPredictor, numSamples, chanBits, &bits1) ; + status = dyn_decomp (&agParams, bits, p->u.mPredictor, numSamples, chanBits, &bits1) ; RequireNoErr (status, goto Exit ;) ; if (modeU == 0) { - unpc_block (p->mPredictor, p->mMixBufferU, numSamples, &coefsU [0], numU, chanBits, denShiftU) ; + unpc_block (p->u.mPredictor, p->mMixBufferU, numSamples, &coefsU [0], numU, chanBits, denShiftU) ; } else { // the special "numActive == 31" mode can be done in-place - unpc_block (p->mPredictor, p->mPredictor, numSamples, NULL, 31, chanBits, 0) ; - unpc_block (p->mPredictor, p->mMixBufferU, numSamples, &coefsU [0], numU, chanBits, denShiftU) ; + unpc_block (p->u.mPredictor, p->u.mPredictor, numSamples, NULL, 31, chanBits, 0) ; + unpc_block (p->u.mPredictor, p->mMixBufferU, numSamples, &coefsU [0], numU, chanBits, denShiftU) ; } } else @@ -300,7 +300,7 @@ alac_decode (ALAC_DECODER *p, struct BitBuffer * bits, int32_t * sampleBuffer, u //Assert (shift <= 16) ; for (i = 0 ; i < numSamples ; i++) - p->mShiftBuffer [i] = (uint16_t) BitBufferRead (&shiftBits, (uint8_t) shift) ; + p->u.mShiftBuffer [i] = (uint16_t) BitBufferRead (&shiftBits, (uint8_t) shift) ; } // convert 32-bit integers into output buffer @@ -318,14 +318,14 @@ alac_decode (ALAC_DECODER *p, struct BitBuffer * bits, int32_t * sampleBuffer, u case 24: out32 = sampleBuffer + channelIndex ; if (bytesShifted != 0) - copyPredictorTo24Shift (p->mMixBufferU, p->mShiftBuffer, out32, numChannels, numSamples, bytesShifted) ; + copyPredictorTo24Shift (p->mMixBufferU, p->u.mShiftBuffer, out32, numChannels, numSamples, bytesShifted) ; else copyPredictorTo24 (p->mMixBufferU, out32, numChannels, numSamples) ; break ; case 32: out32 = sampleBuffer + channelIndex ; if (bytesShifted != 0) - copyPredictorTo32Shift (p->mMixBufferU, p->mShiftBuffer, out32, numChannels, numSamples, bytesShifted) ; + copyPredictorTo32Shift (p->mMixBufferU, p->u.mShiftBuffer, out32, numChannels, numSamples, bytesShifted) ; else copyPredictorTo32 (p->mMixBufferU, out32, numChannels, numSamples) ; break ; @@ -408,34 +408,34 @@ alac_decode (ALAC_DECODER *p, struct BitBuffer * bits, int32_t * sampleBuffer, u // decompress and run predictor for "left" channel set_ag_params (&agParams, p->mConfig.mb, (pb * pbFactorU) / 4, p->mConfig.kb, numSamples, numSamples, p->mConfig.maxRun) ; - status = dyn_decomp (&agParams, bits, p->mPredictor, numSamples, chanBits, &bits1) ; + status = dyn_decomp (&agParams, bits, p->u.mPredictor, numSamples, chanBits, &bits1) ; RequireNoErr (status, goto Exit ;) ; if (modeU == 0) { - unpc_block (p->mPredictor, p->mMixBufferU, numSamples, &coefsU [0], numU, chanBits, denShiftU) ; + unpc_block (p->u.mPredictor, p->mMixBufferU, numSamples, &coefsU [0], numU, chanBits, denShiftU) ; } else { // the special "numActive == 31" mode can be done in-place - unpc_block (p->mPredictor, p->mPredictor, numSamples, NULL, 31, chanBits, 0) ; - unpc_block (p->mPredictor, p->mMixBufferU, numSamples, &coefsU [0], numU, chanBits, denShiftU) ; + unpc_block (p->u.mPredictor, p->u.mPredictor, numSamples, NULL, 31, chanBits, 0) ; + unpc_block (p->u.mPredictor, p->mMixBufferU, numSamples, &coefsU [0], numU, chanBits, denShiftU) ; } // decompress and run predictor for "right" channel set_ag_params (&agParams, p->mConfig.mb, (pb * pbFactorV) / 4, p->mConfig.kb, numSamples, numSamples, p->mConfig.maxRun) ; - status = dyn_decomp (&agParams, bits, p->mPredictor, numSamples, chanBits, &bits2) ; + status = dyn_decomp (&agParams, bits, p->u.mPredictor, numSamples, chanBits, &bits2) ; RequireNoErr (status, goto Exit ;) ; if (modeV == 0) { - unpc_block (p->mPredictor, p->mMixBufferV, numSamples, &coefsV [0], numV, chanBits, denShiftV) ; + unpc_block (p->u.mPredictor, p->mMixBufferV, numSamples, &coefsV [0], numV, chanBits, denShiftV) ; } else { // the special "numActive == 31" mode can be done in-place - unpc_block (p->mPredictor, p->mPredictor, numSamples, NULL, 31, chanBits, 0) ; - unpc_block (p->mPredictor, p->mMixBufferV, numSamples, &coefsV [0], numV, chanBits, denShiftV) ; + unpc_block (p->u.mPredictor, p->u.mPredictor, numSamples, NULL, 31, chanBits, 0) ; + unpc_block (p->u.mPredictor, p->mMixBufferV, numSamples, &coefsV [0], numV, chanBits, denShiftV) ; } } else @@ -488,8 +488,8 @@ alac_decode (ALAC_DECODER *p, struct BitBuffer * bits, int32_t * sampleBuffer, u for (i = 0 ; i < (numSamples * 2) ; i += 2) { - p->mShiftBuffer [i + 0] = (uint16_t) BitBufferRead (&shiftBits, (uint8_t) shift) ; - p->mShiftBuffer [i + 1] = (uint16_t) BitBufferRead (&shiftBits, (uint8_t) shift) ; + p->u.mShiftBuffer [i + 0] = (uint16_t) BitBufferRead (&shiftBits, (uint8_t) shift) ; + p->u.mShiftBuffer [i + 1] = (uint16_t) BitBufferRead (&shiftBits, (uint8_t) shift) ; } } @@ -508,12 +508,12 @@ alac_decode (ALAC_DECODER *p, struct BitBuffer * bits, int32_t * sampleBuffer, u case 24: out32 = sampleBuffer + channelIndex ; unmix24 (p->mMixBufferU, p->mMixBufferV, out32, numChannels, numSamples, - mixBits, mixRes, p->mShiftBuffer, bytesShifted) ; + mixBits, mixRes, p->u.mShiftBuffer, bytesShifted) ; break ; case 32: out32 = sampleBuffer + channelIndex ; unmix32 (p->mMixBufferU, p->mMixBufferV, out32, numChannels, numSamples, - mixBits, mixRes, p->mShiftBuffer, bytesShifted) ; + mixBits, mixRes, p->u.mShiftBuffer, bytesShifted) ; break ; } diff --git a/src/alac.c b/src/alac.c index f83dd2fa..6a354a9c 100644 --- a/src/alac.c +++ b/src/alac.c @@ -51,12 +51,12 @@ typedef struct /* Can't have a decoder and an encoder at the same time so stick - ** them in an un-named union. + ** them in a union. */ union { ALAC_DECODER decoder ; ALAC_ENCODER encoder ; - } ; + } u ; char enctmpname [512] ; FILE *enctmp ; @@ -169,7 +169,7 @@ alac_close (SF_PRIVATE *psf) plac = psf->codec_data ; if (psf->file.mode == SFM_WRITE) - { ALAC_ENCODER *penc = &plac->encoder ; + { ALAC_ENCODER *penc = &plac->u.encoder ; SF_CHUNK_INFO chunk_info ; sf_count_t readcount ; uint8_t kuki_data [1024] ; @@ -268,14 +268,14 @@ alac_reader_init (SF_PRIVATE *psf, const ALAC_DECODER_INFO * info) /* Read in the ALAC cookie data and pass it to the init function. */ kuki_size = alac_kuki_read (psf, info->kuki_offset, u.kuki, sizeof (u.kuki)) ; - if ((error = alac_decoder_init (&plac->decoder, u.kuki, kuki_size)) != ALAC_noErr) + if ((error = alac_decoder_init (&plac->u.decoder, u.kuki, kuki_size)) != ALAC_noErr) { psf_log_printf (psf, "*** alac_decoder_init() returned %s. ***\n", alac_error_string (error)) ; return SFE_INTERNAL ; } ; - if (plac->decoder.mNumChannels != (unsigned) psf->sf.channels) - { psf_log_printf (psf, "*** Initialized decoder has %u channels, but it should be %d. ***\n", plac->decoder.mNumChannels, psf->sf.channels) ; + if (plac->u.decoder.mNumChannels != (unsigned) psf->sf.channels) + { psf_log_printf (psf, "*** Initialized decoder has %u channels, but it should be %d. ***\n", plac->u.decoder.mNumChannels, psf->sf.channels) ; return SFE_INTERNAL ; } ; @@ -357,7 +357,7 @@ alac_writer_init (SF_PRIVATE *psf) return SFE_ALAC_FAIL_TMPFILE ; } ; - alac_encoder_init (&plac->encoder, psf->sf.samplerate, psf->sf.channels, alac_format_flags, ALAC_FRAME_LENGTH) ; + alac_encoder_init (&plac->u.encoder, psf->sf.samplerate, psf->sf.channels, alac_format_flags, ALAC_FRAME_LENGTH) ; return 0 ; } /* alac_writer_init */ @@ -402,7 +402,7 @@ alac_reader_calc_frames (SF_PRIVATE *psf, ALAC_PRIVATE *plac) static int alac_decode_block (SF_PRIVATE *psf, ALAC_PRIVATE *plac) -{ ALAC_DECODER *pdec = &plac->decoder ; +{ ALAC_DECODER *pdec = &plac->u.decoder ; uint32_t packet_size ; BitBuffer bit_buffer ; @@ -437,7 +437,7 @@ alac_decode_block (SF_PRIVATE *psf, ALAC_PRIVATE *plac) static int alac_encode_block (ALAC_PRIVATE *plac) -{ ALAC_ENCODER *penc = &plac->encoder ; +{ ALAC_ENCODER *penc = &plac->u.encoder ; uint32_t num_bytes = 0 ; alac_encode (penc, plac->partial_block_frames, plac->buffer, plac->byte_buffer, &num_bytes) ; diff --git a/src/common.c b/src/common.c index c9737a92..90371cb0 100644 --- a/src/common.c +++ b/src/common.c @@ -1246,7 +1246,7 @@ psf_memset (void *s, int c, sf_count_t len) ** bodgy something up instead. */ -typedef SF_CUES_VAR (0) SF_CUES_0 ; +typedef SF_CUES_VAR () SF_CUES_0 ; /* calculate size of SF_CUES struct given number of cues */ #define SF_CUES_VAR_SIZE(count) (sizeof (SF_CUES_0) + count * sizeof (SF_CUE_POINT)) @@ -1625,7 +1625,7 @@ psf_f2s_clip_array (const float *src, short *dest, int count, int normalize) continue ; } ; if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x1000)) - { dest [count] = 0x8000 ; + { dest [count] = -0x7FFF - 1 ; continue ; } ; @@ -1659,7 +1659,7 @@ psf_d2s_clip_array (const double *src, short *dest, int count, int normalize) continue ; } ; if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x1000)) - { dest [count] = 0x8000 ; + { dest [count] = -0x7FFF - 1 ; continue ; } ; diff --git a/src/common.h b/src/common.h index 08360d06..b2add65a 100644 --- a/src/common.h +++ b/src/common.h @@ -101,22 +101,8 @@ #define NOT(x) (! (x)) -#if COMPILER_IS_GCC -#define SF_MAX(x, y) ({ \ - typeof (x) sf_max_x1 = (x) ; \ - typeof (y) sf_max_y1 = (y) ; \ - (void) (&sf_max_x1 == &sf_max_y1) ; \ - sf_max_x1 > sf_max_y1 ? sf_max_x1 : sf_max_y1 ; }) - -#define SF_MIN(x, y) ({ \ - typeof (x) sf_min_x2 = (x) ; \ - typeof (y) sf_min_y2 = (y) ; \ - (void) (&sf_min_x2 == &sf_min_y2) ; \ - sf_min_x2 < sf_min_y2 ? sf_min_x2 : sf_min_y2 ; }) -#else #define SF_MAX(a, b) ((a) > (b) ? (a) : (b)) #define SF_MIN(a, b) ((a) < (b) ? (a) : (b)) -#endif #define COMPILE_TIME_ASSERT(e) (sizeof (struct { int : - !! (e) ; })) diff --git a/src/test_broadcast_var.c b/src/test_broadcast_var.c index dbea9431..e75dc750 100644 --- a/src/test_broadcast_var.c +++ b/src/test_broadcast_var.c @@ -89,7 +89,7 @@ test_broadcast_var_set (void) static void test_broadcast_var_zero (void) { SF_PRIVATE sf_private, *psf ; - SF_BROADCAST_INFO_VAR (0) bi ; + SF_BROADCAST_INFO_VAR () bi ; psf = &sf_private ; memset (psf, 0, sizeof (sf_private)) ; diff --git a/src/wav.c b/src/wav.c index fd3e453f..ba05fa52 100644 --- a/src/wav.c +++ b/src/wav.c @@ -77,13 +77,13 @@ enum -{ HAVE_RIFF = 0x01, - HAVE_WAVE = 0x02, - HAVE_fmt = 0x04, - HAVE_fact = 0x08, - HAVE_PEAK = 0x10, - HAVE_data = 0x20, - HAVE_other = 0x80000000 +{ HAVE_RIFF = 1 << 0, + HAVE_WAVE = 1 << 1, + HAVE_fmt = 1 << 2, + HAVE_fact = 1 << 3, + HAVE_PEAK = 1 << 4, + HAVE_data = 1 << 5, + HAVE_other = 1 << 6 } ; diff --git a/tests/error_test.c b/tests/error_test.c index 2e348e47..248f26ff 100644 --- a/tests/error_test.c +++ b/tests/error_test.c @@ -255,7 +255,7 @@ unrecognised_test (void) sndfile = sf_open (filename, SFM_READ, &sfinfo) ; exit_if_true (sndfile != NULL, - "\n\nLine %d : SNDFILE* pointer (%p) should ne NULL.\n", __LINE__, sndfile + "\n\nLine %d : SNDFILE* pointer (%p) should be NULL.\n", __LINE__, (void *) sndfile ) ; k = sf_error (sndfile) ; From 23daaebc2aaae41daadf4783c06886293fea9d1d Mon Sep 17 00:00:00 2001 From: evpobr Date: Sun, 14 Feb 2021 10:32:13 +0500 Subject: [PATCH 009/204] Add commit message requirements to CONTRIBUTING.md 1. Separate subject from body with a blank line 2. Limit the subject line to 50 characters 3. Capitalize the subject line 3. Do not end the subject line with a period 4. Use the imperative mood in the subject line 5. Wrap the body at 72 characters 6. Use the body to explain what and why vs. how --- CONTRIBUTING.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index afdcb55e..5439b018 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -18,6 +18,7 @@ * Patches should always be submitted via a either Github "pull request" or a via emailed patches created using "git format-patch". * Patches for new features should include tests and documentation. +* Commit messages should follow the ["How to Write a Git Commit Message"](https://chris.beams.io/posts/git-commit/) guide. * Patches to fix bugs should either pass all tests, or modify the tests in some sane way. * When a new feature is added for a particular file format and that feature From 2db6a8fd2e1e7d9d1ad309d4f4110712f7cb4e5d Mon Sep 17 00:00:00 2001 From: evpobr Date: Sun, 14 Feb 2021 10:53:28 +0500 Subject: [PATCH 010/204] Change SNDFILE type to be a typedef of a real struct For developers it is typedef to the actual `sf_private_tag` struct now. For users it is still opaque type (no API break). --- include/sndfile.h.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/sndfile.h.in b/include/sndfile.h.in index 970fb260..5e30c405 100644 --- a/include/sndfile.h.in +++ b/include/sndfile.h.in @@ -337,7 +337,7 @@ enum /* A SNDFILE* pointer can be passed around much like stdio.h's FILE* pointer. */ -typedef struct SNDFILE_tag SNDFILE ; +typedef struct sf_private_tag SNDFILE ; /* The following typedef is system specific and is defined when libsndfile is ** compiled. sf_count_t will be a 64 bit value when the underlying OS allows From 3970faeb7fc8cee60cc5260ab4102433c37d5d63 Mon Sep 17 00:00:00 2001 From: bobsayshilol Date: Sun, 14 Feb 2021 13:09:18 +0000 Subject: [PATCH 011/204] Make sure that there's enough space to store decoded nibbles in when reading IMA ADPCM data The loop in wavlike_ima_decode_block() that pulls apart the nibbles assumes that there's 8 nibbles worth of data for every channel, so rather than adding a branch in the hot path I've opted for allocating slightly more than we might need. This was a blocker caught by fuzzing locally. This also appears to be the same crash as https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=25530. --- src/ima_adpcm.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ima_adpcm.c b/src/ima_adpcm.c index 8c9bbff3..28c20ca3 100644 --- a/src/ima_adpcm.c +++ b/src/ima_adpcm.c @@ -182,7 +182,12 @@ ima_reader_init (SF_PRIVATE *psf, int blockalign, int samplesperblock) if (psf->file.mode != SFM_READ) return SFE_BAD_MODE_RW ; - pimasize = sizeof (IMA_ADPCM_PRIVATE) + blockalign * psf->sf.channels + 3 * psf->sf.channels * samplesperblock ; + /* + ** Allocate enough space for 1 more than a multiple of 8 samples + ** to avoid having to branch when pulling apart the nibbles. + */ + count = ((samplesperblock - 2) | 7) + 2 ; + pimasize = sizeof (IMA_ADPCM_PRIVATE) + psf->sf.channels * (blockalign + samplesperblock + sizeof(short) * count) ; if (! (pima = calloc (1, pimasize))) return SFE_MALLOC_FAILED ; From 678411acba9ba96319e027e0d2209adcc46d2628 Mon Sep 17 00:00:00 2001 From: bobsayshilol Date: Sun, 14 Feb 2021 13:18:11 +0000 Subject: [PATCH 012/204] Jump back to the start of guess_file_type() rather than recursing into it My fuzzer crashes out with a very large stacktrace if this recurses too many times. --- src/sndfile.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/sndfile.c b/src/sndfile.c index 63beda8d..99be2337 100644 --- a/src/sndfile.c +++ b/src/sndfile.c @@ -2680,6 +2680,7 @@ static int guess_file_type (SF_PRIVATE *psf) { uint32_t buffer [3], format ; +retry: if (psf_binheader_readf (psf, "b", &buffer, SIGNED_SIZEOF (buffer)) != SIGNED_SIZEOF (buffer)) { psf->error = SFE_BAD_FILE_READ ; return 0 ; @@ -2780,7 +2781,7 @@ guess_file_type (SF_PRIVATE *psf) || buffer [0] == MAKE_MARKER ('I', 'D', '3', 4)) { psf_log_printf (psf, "Found 'ID3' marker.\n") ; if (id3_skip (psf)) - return guess_file_type (psf) ; + goto retry ; return 0 ; } ; From 40cb9e3c149a2e5295e3dbaad5a3dc2a8e828095 Mon Sep 17 00:00:00 2001 From: bobsayshilol Date: Sun, 14 Feb 2021 13:23:20 +0000 Subject: [PATCH 013/204] Jump forwards to the next 4 byte aligned offset rather than always jumping backwards by 3 bytes This was causing an infinite loop on malformed input generated by fuzzing. Note that jumping backwards to the previous 4 byte aligned offset also resulted in the same infinite loop since psf_ftell() was returning an offset ending in 0xf. --- src/svx.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/svx.c b/src/svx.c index 72d803f1..19e93ea4 100644 --- a/src/svx.c +++ b/src/svx.c @@ -307,7 +307,8 @@ svx_read_header (SF_PRIVATE *psf) if ((chunk_size = psf_ftell (psf)) & 0x03) { psf_log_printf (psf, " Unknown chunk marker at position %d. Resynching.\n", chunk_size - 4) ; - psf_binheader_readf (psf, "j", -3) ; + chunk_size = chunk_size & 3 ; + psf_binheader_readf (psf, "j", 4 - chunk_size) ; break ; } ; psf_log_printf (psf, "*** Unknown chunk marker (%X) at position %D. Exiting parser.\n", marker, psf_ftell (psf) - 8) ; From 9e0e55f8bfa60bddca083ff85699f855c91c42e7 Mon Sep 17 00:00:00 2001 From: bobsayshilol Date: Sun, 14 Feb 2021 13:51:50 +0000 Subject: [PATCH 014/204] Move these magic numbers into the common header and give them some error messages Without this the client gets the message "Not a valid error number (666)" printed to stdout, which doesn't explain what the issue is. These were found via fuzzing where the fuzzer was spamming these messages. --- src/avr.c | 6 ++---- src/caf.c | 4 ---- src/common.h | 9 +++++++++ src/mpc2k.c | 2 -- src/sndfile.c | 9 +++++++++ 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/avr.c b/src/avr.c index bd6b00f4..6c78ff69 100644 --- a/src/avr.c +++ b/src/avr.c @@ -28,8 +28,6 @@ #define TWOBIT_MARKER (MAKE_MARKER ('2', 'B', 'I', 'T')) #define AVR_HDR_SIZE 128 -#define SFE_AVR_X 666 - /* ** From: hyc@hanauma.Jpl.Nasa.Gov (Howard Chu) ** @@ -116,7 +114,7 @@ avr_read_header (SF_PRIVATE *psf) psf_log_printf (psf, "%M\n", hdr.marker) ; if (hdr.marker != TWOBIT_MARKER) - return SFE_AVR_X ; + return SFE_AVR_NOT_AVR ; psf_log_printf (psf, " Name : %s\n", hdr.name) ; @@ -145,7 +143,7 @@ avr_read_header (SF_PRIVATE *psf) default : psf_log_printf (psf, "Error : bad rez/sign combination.\n") ; - return SFE_AVR_X ; + return SFE_AVR_BAD_REZ_SIGN ; } ; psf_binheader_readf (psf, "E4444", &hdr.srate, &hdr.frames, &hdr.lbeg, &hdr.lend) ; diff --git a/src/caf.c b/src/caf.c index 0eaa8eaa..8e629ad6 100644 --- a/src/caf.c +++ b/src/caf.c @@ -66,10 +66,6 @@ #define CAF_PEAK_CHUNK_SIZE(ch) ((int) (sizeof (int) + ch * (sizeof (float) + 8))) -#define SFE_CAF_NOT_CAF 666 -#define SFE_CAF_NO_DESC 667 -#define SFE_CAF_BAD_PEAK 668 - /*------------------------------------------------------------------------------ ** Typedefs. */ diff --git a/src/common.h b/src/common.h index b2add65a..b94e678a 100644 --- a/src/common.h +++ b/src/common.h @@ -754,6 +754,15 @@ enum SFE_OPUS_BAD_SAMPLERATE, + SFE_CAF_NOT_CAF, + SFE_CAF_NO_DESC, + SFE_CAF_BAD_PEAK, + + SFE_AVR_NOT_AVR, + SFE_AVR_BAD_REZ_SIGN, + + SFE_MPC_NO_MARKER, + SFE_MAX_ERROR /* This must be last in list. */ } ; diff --git a/src/mpc2k.c b/src/mpc2k.c index aef21ad9..9f490eab 100644 --- a/src/mpc2k.c +++ b/src/mpc2k.c @@ -46,8 +46,6 @@ #define HEADER_LENGTH 42 /* Sum of above data fields. */ #define HEADER_NAME_LEN 17 /* Length of name string. */ -#define SFE_MPC_NO_MARKER 666 - /*------------------------------------------------------------------------------ ** Private static functions. */ diff --git a/src/sndfile.c b/src/sndfile.c index 99be2337..6de3963d 100644 --- a/src/sndfile.c +++ b/src/sndfile.c @@ -279,6 +279,15 @@ ErrorStruct SndfileErrors [] = { SFE_OPUS_BAD_SAMPLERATE , "Error : Opus only supports sample rates of 8000, 12000, 16000, 24000 and 48000." }, + { SFE_CAF_NOT_CAF , "Error : Not a CAF file." }, + { SFE_CAF_NO_DESC , "Error : No 'desc' marker in CAF file." }, + { SFE_CAF_BAD_PEAK , "Error : Bad 'PEAK' chunk in CAF file." }, + + { SFE_AVR_NOT_AVR , "Error : Not an AVR file." }, + { SFE_AVR_BAD_REZ_SIGN , "Error : Bad rez/sign combination." }, + + { SFE_MPC_NO_MARKER , "Error : No marker in MPC2K file." }, + { SFE_MAX_ERROR , "Maximum error number." }, { SFE_MAX_ERROR + 1 , NULL } } ; From ad2990ec02d3cf9bcde62f88bf1bde67a9d6c6b6 Mon Sep 17 00:00:00 2001 From: bobsayshilol Date: Sun, 31 Jan 2021 21:19:59 +0000 Subject: [PATCH 015/204] Add the generate example to the CMake build --- CMakeLists.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index de98145f..f2d4223e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -647,6 +647,11 @@ if (BUILD_EXAMPLES) $<$:m> ) +# generate + + add_executable (generate examples/generate.c) + target_link_libraries (generate PRIVATE sndfile) + # sndfilehandle add_executable (sndfilehandle examples/sndfilehandle.cc) @@ -658,6 +663,7 @@ if (BUILD_EXAMPLES) make_sine sfprocess list_formats + generate sndfilehandle ) From abe9dcc0f10049b65f71edc28948aff9d3b61423 Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Thu, 19 Nov 2020 22:58:18 -0800 Subject: [PATCH 016/204] ogg_vorbis: Fix granule position when seeking Vorbis streams Previously the vorbis_seek function did not take the lapped nature of Vorbis block into account when using the bisection search. Also, introduce vorbis_skip(), which can skip through packets without having to decode all the way to samples. --- src/ogg_vorbis.c | 210 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 150 insertions(+), 60 deletions(-) diff --git a/src/ogg_vorbis.c b/src/ogg_vorbis.c index 5f536516..5e1fb5b6 100644 --- a/src/ogg_vorbis.c +++ b/src/ogg_vorbis.c @@ -1,4 +1,5 @@ /* +** Copyright (C) 2018-2021 Arthur Taylor ** Copyright (C) 2002-2016 Erik de Castro Lopo ** Copyright (C) 2002-2005 Michael Smith ** Copyright (C) 2007 John ffitch @@ -19,7 +20,7 @@ */ /* -** Much of this code is based on the examples in libvorbis from the +** Much of this code is based on the examples in libvorbis from the ** XIPHOPHORUS Company http://www.xiph.org/ which has a BSD-style Licence ** Copyright (c) 2002, Xiph.org Foundation ** @@ -78,6 +79,9 @@ #include "ogg.h" +/* How many seconds in the future to not bother bisection searching for. */ +#define VORBIS_SEEK_THRESHOLD 2 + typedef int convert_func (SF_PRIVATE *psf, int, void *, int, int, float **) ; static int vorbis_read_header (SF_PRIVATE *psf) ; @@ -85,6 +89,8 @@ static int vorbis_write_header (SF_PRIVATE *psf, int calc_length) ; static int vorbis_close (SF_PRIVATE *psf) ; static int vorbis_command (SF_PRIVATE *psf, int command, void *data, int datasize) ; static int vorbis_byterate (SF_PRIVATE *psf) ; +static int vorbis_skip (SF_PRIVATE *psf, uint64_t target_gp) ; +static int vorbis_seek_trysearch (SF_PRIVATE *psf, uint64_t target_gp) ; static sf_count_t vorbis_calculate_page_duration (SF_PRIVATE *psf) ; static sf_count_t vorbis_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ; static sf_count_t vorbis_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ; @@ -115,26 +121,22 @@ static STR_PAIRS vorbis_metatypes [] = { SF_STR_ALBUM, "Album" }, { SF_STR_LICENSE, "License" }, { SF_STR_TRACKNUMBER, "Tracknumber" }, - { SF_STR_GENRE, "Genre" }, + { SF_STR_GENRE, "Genre" }, } ; typedef struct -{ /* Count current location */ - sf_count_t loc ; +{ /* Current granule position. */ + uint64_t loc ; /* Struct that stores all the static vorbis bitstream settings */ vorbis_info vinfo ; /* Struct that stores all the bitstream user comments */ vorbis_comment vcomment ; - /* Ventral working state for the packet->PCM decoder */ + /* Central working state for the packet->PCM decoder */ vorbis_dsp_state vdsp ; /* Local working space for packet->PCM decode */ vorbis_block vblock ; - /* Encoding quality in range [0.0, 1.0]. */ double quality ; - - /* Current granule position. */ - uint64_t pcm_current ; /* Offset of the first samples' granule position. */ uint64_t pcm_start ; /* Last valid samples' granule position. */ @@ -184,8 +186,8 @@ vorbis_read_header (SF_PRIVATE *psf) ** Vorbis decoder. ** ** The next two packets in order are the comment and codebook headers. - ** They're likely large and may span multiple pages. Thus we reead - ** and submit data until we get our two pacakets, watching that no + ** They're likely large and may span multiple pages. Thus we read + ** and submit data until we get our two packets, watching that no ** pages are missing. If a page is missing, error out ; losing a ** header page is the only place where missing data is fatal. */ @@ -231,7 +233,7 @@ vorbis_read_header (SF_PRIVATE *psf) psf->dataoffset = ogg_sync_ftell (psf) ; /* - ** Caculate the granule position offset. The first page with a payload + ** Calculate the granule position offset. The first page with a payload ** packet shouldn't end in a continued packet. The difference between the ** page's granule position and the sum of frames on the page tells us the ** granule position offset. @@ -240,7 +242,6 @@ vorbis_read_header (SF_PRIVATE *psf) ogg_stream_unpack_page (psf, odata) ; vdata->pcm_start = odata->pkt [odata->pkt_len - 1].granulepos ; duration = vorbis_calculate_page_duration (psf) ; - if (duration < (sf_count_t) vdata->pcm_start) vdata->pcm_start -= duration ; else @@ -649,8 +650,8 @@ vorbis_read_sample (SF_PRIVATE *psf, void *ptr, sf_count_t lens, convert_func *t return i ; if (nn == 2) { /* Ran over a hole. loc is now out of date, need to recalculate. */ - vdata->loc = odata->pkt [odata->pkt_len - 1].granulepos ; - vdata->loc -= vorbis_calculate_page_duration (psf) ; + vdata->loc = odata->pkt [odata->pkt_len - 1].granulepos - + vorbis_calculate_page_duration (psf) ; } } ; @@ -791,11 +792,130 @@ vorbis_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t lens) return lens ; } /* vorbis_write_d */ +static int +vorbis_skip (SF_PRIVATE *psf, uint64_t target) +{ OGG_PRIVATE *odata = (OGG_PRIVATE *) psf->container_data ; + VORBIS_PRIVATE *vdata = (VORBIS_PRIVATE *) psf->codec_data ; + ogg_packet *pkt ; + int thisblock, lastblock, nn ; + const int blocksize = vorbis_info_blocksize (&vdata->vinfo, 1) ; + + /* Read out any samples in the decoder if any. */ + thisblock = vorbis_synthesis_pcmout (&vdata->vdsp, NULL) ; + if (thisblock > 0) + { if ((uint64_t) thisblock + vdata->loc >= target) + thisblock = SF_MIN (thisblock, (int) (target - vdata->loc)) ; + + vorbis_synthesis_read (&vdata->vdsp, thisblock) ; + vdata->loc += thisblock ; + if (vdata->loc == target) + return 0 ; + } ; + + lastblock = 0 ; + for ( ; vdata->loc < target ; ) + { /* Ensure there are unpacked packets. */ + if (odata->pkt_indx == odata->pkt_len) + { /* Page out of packets, load and unpack the next page. */ + nn = ogg_stream_unpack_page (psf, odata) ; + if (nn < 0) + return nn ; + if (nn == 0) + break ; + if (nn == 2) + { /* Ran over a hole. loc is now out of date, need to recalculate. */ + vdata->loc = odata->pkt [odata->pkt_len - 1].granulepos - + vorbis_calculate_page_duration (psf) ; + } + + if (target < vdata->loc) + { /* Our target is inside the hole :-( */ + return 0 ; + } ; + } ; + + pkt = &odata->pkt [odata->pkt_indx] ; + thisblock = vorbis_packet_blocksize (&vdata->vinfo, pkt) ; + if (thisblock < 0) + { /* Not an audio packet */ + odata->pkt_indx++ ; + continue ; + } ; + + if (lastblock) + { vdata->loc += ((lastblock + thisblock) / 4) ; + } ; + + /* Check to see if the block contains our target */ + if (vdata->loc + ((thisblock + blocksize) / 4) >= target) + break ; + + /* Block is before the target. Track for state, but don't decode. */ + odata->pkt_indx++ ; + vorbis_synthesis_trackonly (&vdata->vblock, pkt) ; + vorbis_synthesis_blockin (&vdata->vdsp, &vdata->vblock) ; + lastblock = thisblock ; + } ; + + /* We are at the correct block, but still need to consume samples to reach + ** our target. */ + vorbis_read_sample (psf, (void *) NULL, (target - vdata->loc) * psf->sf.channels, vorbis_rnull) ; + + return 0 ; +} /* vorbis_skip */ + +static int +vorbis_seek_trysearch (SF_PRIVATE *psf, uint64_t target_gp) +{ OGG_PRIVATE *odata = (OGG_PRIVATE *) psf->container_data ; + VORBIS_PRIVATE *vdata = (VORBIS_PRIVATE *) psf->codec_data ; + uint64_t best_gp, search_target_gp ; + int ret ; + + /* Can't bisect a file we don't know the end of (cannot seek). */ + if (vdata->pcm_end == (uint64_t) -1) + return 0 ; + + /* If the target is for the near future, don't bother bisecting, just skip + ** to it. */ + if (target_gp >= vdata->loc && + target_gp - vdata->loc < ((unsigned) (VORBIS_SEEK_THRESHOLD) * psf->sf.samplerate)) + return 0 ; + + /* Search for a position a half large-block before our target. As Vorbis is + ** lapped, every sample position come from two blocks, the "left" half of + ** one block and the "right" half of the previous block. The granule + ** position of an Ogg page of a Vorbis stream is the sample offset of the + ** last finished sample in the stream that can be decoded from a page. A + ** page also contains another half-block of samples waiting to be lapped + ** with the first half-block of samples from the next page. + ** + ** Searching for a sample one half of a large block before our target + ** guarantees we always load a page containing the previous half block + ** required to decode the target. Downside is we can't use best_gp + ** parameter of the page seek function. */ + search_target_gp = vorbis_info_blocksize (&vdata->vinfo, 1) / 2 ; + search_target_gp = search_target_gp < target_gp ? target_gp - search_target_gp : 0 ; + + ret = ogg_stream_seek_page_search (psf, odata, search_target_gp, vdata->pcm_start, + vdata->pcm_end, &best_gp, psf->dataoffset, vdata->last_page) ; + if (ret >= 0) + { ret = ogg_stream_unpack_page (psf, odata) ; + if (ret == 1) + { /* Recalculate our granule position */ + vdata->loc = odata->pkt [odata->pkt_len - 1].granulepos - + vorbis_calculate_page_duration (psf) ; + vorbis_synthesis_restart (&vdata->vdsp) ; + } ; + } ; + + return ret ; +} /* vorbis_seek_trysearch */ + static sf_count_t vorbis_seek (SF_PRIVATE *psf, int UNUSED (mode), sf_count_t offset) { OGG_PRIVATE *odata = (OGG_PRIVATE *) psf->container_data ; VORBIS_PRIVATE *vdata = (VORBIS_PRIVATE *) psf->codec_data ; - sf_count_t target ; + uint64_t target_gp ; int ret ; if (odata == NULL || vdata == NULL) @@ -807,63 +927,29 @@ vorbis_seek (SF_PRIVATE *psf, int UNUSED (mode), sf_count_t offset) } ; if (psf->file.mode == SFM_READ) - { target = offset + vdata->pcm_start ; + { target_gp = (uint64_t) offset + vdata->pcm_start ; + ret = vorbis_seek_trysearch (psf, target_gp) ; - /* - ** If the end of the file is know, and the seek isn't for the near - ** future, do a search of the file for a good place to start. - */ - ret = 0 ; - if ((vdata->pcm_end != (uint64_t) -1) && - (target < vdata->loc || target - vdata->loc > (2 * psf->sf.samplerate))) - { uint64_t best_gp ; - - best_gp = vdata->pcm_start ; - - ret = ogg_stream_seek_page_search (psf, odata, target, vdata->pcm_start, - vdata->pcm_end, &best_gp, psf->dataoffset, vdata->last_page) ; - if (ret >= 0) - { ret = ogg_stream_unpack_page (psf, odata) ; - if (ret == 1) - { vdata->loc = best_gp ; - vorbis_synthesis_restart (&vdata->vdsp) ; - } ; - } ; - } ; - - if (ret >= 0 && offset + (sf_count_t) vdata->pcm_start >= vdata->loc) - target = offset + vdata->pcm_start - vdata->loc ; - else + if (ret < 0 || vdata->loc > target_gp) { /* Search failed (bad data?), reset to the beginning of the stream. */ + psf_log_printf (psf, "Seek search failed. Reading through stream from start.\n") ; ogg_stream_reset_serialno (&odata->ostream, odata->ostream.serialno) ; odata->pkt_len = 0 ; odata->pkt_indx = 0 ; ogg_sync_fseek (psf, psf->dataoffset, SEEK_SET) ; vdata->loc = 0 ; vorbis_synthesis_restart (&vdata->vdsp) ; - target = offset ; } ; - while (target > 0) - { sf_count_t m = target > 4096 ? 4096 : target ; - - /* - ** Need to multiply by channels here because the seek is done in - ** terms of frames and the read function is done in terms of - ** samples. - */ - vorbis_read_sample (psf, (void *) NULL, m * psf->sf.channels, vorbis_rnull) ; - - target -= m ; - } ; + vorbis_skip (psf, target_gp) ; return vdata->loc - vdata->pcm_start ; } ; - return 0 ; + psf->error = SFE_BAD_SEEK ; + return ((sf_count_t) -1) ; } /* vorbis_seek */ - static int vorbis_byterate (SF_PRIVATE *psf) { @@ -877,9 +963,13 @@ static sf_count_t vorbis_calculate_page_duration (SF_PRIVATE *psf) { OGG_PRIVATE *odata = (OGG_PRIVATE *) psf->container_data ; VORBIS_PRIVATE *vdata = (VORBIS_PRIVATE *) psf->codec_data ; - long thisblock, lastblock ; + int thisblock, lastblock, i ; sf_count_t duration ; - int i ; + + /* + ** Calculate how many samples can be decoded from blocks in this page, + ** accounting for the fact that blocks are 1/2 lapped. + */ lastblock = -1 ; duration = 0 ; @@ -893,7 +983,7 @@ vorbis_calculate_page_duration (SF_PRIVATE *psf) } ; return duration ; -} +} /* vorbis_calculate_page_duration */ #else /* HAVE_EXTERNAL_XIPH_LIBS */ From 31859403019c9397d8d30536bb768a022a73b623 Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Mon, 15 Feb 2021 12:05:02 -0800 Subject: [PATCH 017/204] ogg_vorbis: Improve granule position calculation Improve granule position calculation, centralising logic in one function, adding warning messages for malformed data conditions and commenting about corner and degenerate cases. --- src/ogg_vorbis.c | 204 +++++++++++++++++++++++++++++------------------ 1 file changed, 128 insertions(+), 76 deletions(-) diff --git a/src/ogg_vorbis.c b/src/ogg_vorbis.c index 5e1fb5b6..1b00af51 100644 --- a/src/ogg_vorbis.c +++ b/src/ogg_vorbis.c @@ -89,9 +89,9 @@ static int vorbis_write_header (SF_PRIVATE *psf, int calc_length) ; static int vorbis_close (SF_PRIVATE *psf) ; static int vorbis_command (SF_PRIVATE *psf, int command, void *data, int datasize) ; static int vorbis_byterate (SF_PRIVATE *psf) ; +static int vorbis_calculate_granulepos (SF_PRIVATE *psf, uint64_t *gp_out) ; static int vorbis_skip (SF_PRIVATE *psf, uint64_t target_gp) ; static int vorbis_seek_trysearch (SF_PRIVATE *psf, uint64_t target_gp) ; -static sf_count_t vorbis_calculate_page_duration (SF_PRIVATE *psf) ; static sf_count_t vorbis_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ; static sf_count_t vorbis_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ; static sf_count_t vorbis_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ; @@ -126,7 +126,7 @@ static STR_PAIRS vorbis_metatypes [] = typedef struct { /* Current granule position. */ - uint64_t loc ; + uint64_t gp ; /* Struct that stores all the static vorbis bitstream settings */ vorbis_info vinfo ; /* Struct that stores all the bitstream user comments */ @@ -149,9 +149,10 @@ static int vorbis_read_header (SF_PRIVATE *psf) { OGG_PRIVATE *odata = (OGG_PRIVATE *) psf->container_data ; VORBIS_PRIVATE *vdata = (VORBIS_PRIVATE *) psf->codec_data ; - sf_count_t duration ; int printed_metadata_msg = 0 ; - int i, k, nn ; + int i, nn ; + sf_count_t last_page ; + sf_count_t saved_offset ; /* ** The first page of the Ogg stream we are told to try and open as Vorbis @@ -240,24 +241,16 @@ vorbis_read_header (SF_PRIVATE *psf) ** See https://xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-132000A.2 */ ogg_stream_unpack_page (psf, odata) ; - vdata->pcm_start = odata->pkt [odata->pkt_len - 1].granulepos ; - duration = vorbis_calculate_page_duration (psf) ; - if (duration < (sf_count_t) vdata->pcm_start) - vdata->pcm_start -= duration ; - else - vdata->pcm_start = 0 ; + vorbis_calculate_granulepos (psf, &vdata->pcm_start) ; + vdata->gp = vdata->pcm_start ; /* ** Find the end of the stream, save it. Only works if the file is seekable. */ - vdata->loc = vdata->pcm_start ; vdata->pcm_end = (uint64_t) -1 ; psf->datalength = psf->filelength ; if (!psf->is_pipe) - { sf_count_t last_page ; - sf_count_t saved_offset ; - - saved_offset = ogg_sync_ftell (psf) ; + { saved_offset = ogg_sync_ftell (psf) ; last_page = ogg_sync_last_page_before (psf, odata, &vdata->pcm_end, psf->filelength, odata->ostream.serialno) ; if (last_page > 0) { if (!ogg_page_eos (&odata->opage)) @@ -278,10 +271,10 @@ vorbis_read_header (SF_PRIVATE *psf) psf_log_printf (psf, "PCM end : unknown\n") ; /* Throw the comments plus a few lines about the bitstream we're decoding. */ - for (k = 0 ; k < ARRAY_LEN (vorbis_metatypes) ; k++) + for (i = 0 ; i < ARRAY_LEN (vorbis_metatypes) ; i++) { char *dd ; - dd = vorbis_comment_query (&vdata->vcomment, vorbis_metatypes [k].name, 0) ; + dd = vorbis_comment_query (&vdata->vcomment, vorbis_metatypes [i].name, 0) ; if (dd == NULL) continue ; @@ -290,8 +283,8 @@ vorbis_read_header (SF_PRIVATE *psf) printed_metadata_msg = 1 ; } ; - psf_store_string (psf, vorbis_metatypes [k].id, dd) ; - psf_log_printf (psf, " %-10s : %s\n", vorbis_metatypes [k].name, dd) ; + psf_store_string (psf, vorbis_metatypes [i].id, dd) ; + psf_log_printf (psf, " %-10s : %s\n", vorbis_metatypes [i].name, dd) ; } ; psf_log_printf (psf, "End\n") ; @@ -307,7 +300,7 @@ vorbis_read_header (SF_PRIVATE *psf) /* Local state for most of the decode so multiple block decodes can ** proceed in parallel. We could init multiple vorbis_block structures - ** for vd here. */ + ** for vdsp here. */ vorbis_block_init (&vdata->vdsp, &vdata->vblock) ; return 0 ; @@ -335,7 +328,7 @@ vorbis_write_header (SF_PRIVATE *psf, int UNUSED (calc_length)) if (ret) return SFE_BAD_OPEN_FORMAT ; - vdata->loc = 0 ; + vdata->gp = 0 ; /* add a comment */ vorbis_comment_init (&vdata->vcomment) ; @@ -349,15 +342,15 @@ vorbis_write_header (SF_PRIVATE *psf, int UNUSED (calc_length)) switch (psf->strings.data [k].type) { case SF_STR_TITLE : name = "TITLE" ; break ; - case SF_STR_COPYRIGHT : name = "COPYRIGHT" ; break ; + case SF_STR_COPYRIGHT : name = "COPYRIGHT" ; break ; case SF_STR_SOFTWARE : name = "SOFTWARE" ; break ; case SF_STR_ARTIST : name = "ARTIST" ; break ; case SF_STR_COMMENT : name = "COMMENT" ; break ; case SF_STR_DATE : name = "DATE" ; break ; case SF_STR_ALBUM : name = "ALBUM" ; break ; case SF_STR_LICENSE : name = "LICENSE" ; break ; - case SF_STR_TRACKNUMBER : name = "Tracknumber" ; break ; - case SF_STR_GENRE : name = "Genre" ; break ; + case SF_STR_TRACKNUMBER : name = "Tracknumber" ; break ; + case SF_STR_GENRE : name = "Genre" ; break ; default : continue ; } ; @@ -637,9 +630,9 @@ vorbis_read_sample (SF_PRIVATE *psf, void *ptr, sf_count_t lens, convert_func *t len -= samples ; /* tell libvorbis how many samples we actually consumed */ vorbis_synthesis_read (&vdata->vdsp, samples) ; - vdata->loc += samples ; + vdata->gp += samples ; if (len == 0) - return i ; /* Is this necessary */ + return i ; } ; /* Out of samples, load the next packet. */ @@ -649,9 +642,9 @@ vorbis_read_sample (SF_PRIVATE *psf, void *ptr, sf_count_t lens, convert_func *t if (nn <= 0) return i ; if (nn == 2) - { /* Ran over a hole. loc is now out of date, need to recalculate. */ - vdata->loc = odata->pkt [odata->pkt_len - 1].granulepos - - vorbis_calculate_page_duration (psf) ; + { /* Ran over a hole. gp is now out of date, need to recalculate. */ + vorbis_synthesis_restart (&vdata->vdsp) ; + vorbis_calculate_granulepos (psf, &vdata->gp) ; } } ; @@ -723,7 +716,7 @@ vorbis_write_samples (SF_PRIVATE *psf, OGG_PRIVATE *odata, VORBIS_PRIVATE *vdata } ; } ; - vdata->loc += in_frames ; + vdata->gp += in_frames ; } /* vorbis_write_data */ @@ -800,20 +793,22 @@ vorbis_skip (SF_PRIVATE *psf, uint64_t target) int thisblock, lastblock, nn ; const int blocksize = vorbis_info_blocksize (&vdata->vinfo, 1) ; - /* Read out any samples in the decoder if any. */ + /* Read out any samples that may be in the decoder from a seek without a + ** search. */ thisblock = vorbis_synthesis_pcmout (&vdata->vdsp, NULL) ; if (thisblock > 0) - { if ((uint64_t) thisblock + vdata->loc >= target) - thisblock = SF_MIN (thisblock, (int) (target - vdata->loc)) ; + { if ((uint64_t) thisblock + vdata->gp >= target) + thisblock = SF_MIN (thisblock, (int) (target - vdata->gp)) ; vorbis_synthesis_read (&vdata->vdsp, thisblock) ; - vdata->loc += thisblock ; - if (vdata->loc == target) + vdata->gp += thisblock ; + if (vdata->gp == target) return 0 ; } ; + /* Read through packets that are before our target */ lastblock = 0 ; - for ( ; vdata->loc < target ; ) + for ( ; vdata->gp < target ; ) { /* Ensure there are unpacked packets. */ if (odata->pkt_indx == odata->pkt_len) { /* Page out of packets, load and unpack the next page. */ @@ -823,15 +818,14 @@ vorbis_skip (SF_PRIVATE *psf, uint64_t target) if (nn == 0) break ; if (nn == 2) - { /* Ran over a hole. loc is now out of date, need to recalculate. */ - vdata->loc = odata->pkt [odata->pkt_len - 1].granulepos - - vorbis_calculate_page_duration (psf) ; - } - - if (target < vdata->loc) + { /* Ran over a hole. gp is now out of date, need to recalculate. */ + vorbis_synthesis_restart (&vdata->vdsp) ; + vorbis_calculate_granulepos (psf, &vdata->gp) ; + if (target < vdata->gp) { /* Our target is inside the hole :-( */ return 0 ; } ; + } ; } ; pkt = &odata->pkt [odata->pkt_indx] ; @@ -843,11 +837,11 @@ vorbis_skip (SF_PRIVATE *psf, uint64_t target) } ; if (lastblock) - { vdata->loc += ((lastblock + thisblock) / 4) ; + { vdata->gp += ((lastblock + thisblock) / 4) ; } ; /* Check to see if the block contains our target */ - if (vdata->loc + ((thisblock + blocksize) / 4) >= target) + if (vdata->gp + ((thisblock + blocksize) / 4) >= target) break ; /* Block is before the target. Track for state, but don't decode. */ @@ -859,7 +853,7 @@ vorbis_skip (SF_PRIVATE *psf, uint64_t target) /* We are at the correct block, but still need to consume samples to reach ** our target. */ - vorbis_read_sample (psf, (void *) NULL, (target - vdata->loc) * psf->sf.channels, vorbis_rnull) ; + vorbis_read_sample (psf, (void *) NULL, (target - vdata->gp) * psf->sf.channels, vorbis_rnull) ; return 0 ; } /* vorbis_skip */ @@ -877,8 +871,8 @@ vorbis_seek_trysearch (SF_PRIVATE *psf, uint64_t target_gp) /* If the target is for the near future, don't bother bisecting, just skip ** to it. */ - if (target_gp >= vdata->loc && - target_gp - vdata->loc < ((unsigned) (VORBIS_SEEK_THRESHOLD) * psf->sf.samplerate)) + if (target_gp >= vdata->gp && + target_gp - vdata->gp < ((unsigned) (VORBIS_SEEK_THRESHOLD) * psf->sf.samplerate)) return 0 ; /* Search for a position a half large-block before our target. As Vorbis is @@ -898,14 +892,14 @@ vorbis_seek_trysearch (SF_PRIVATE *psf, uint64_t target_gp) ret = ogg_stream_seek_page_search (psf, odata, search_target_gp, vdata->pcm_start, vdata->pcm_end, &best_gp, psf->dataoffset, vdata->last_page) ; - if (ret >= 0) - { ret = ogg_stream_unpack_page (psf, odata) ; - if (ret == 1) - { /* Recalculate our granule position */ - vdata->loc = odata->pkt [odata->pkt_len - 1].granulepos - - vorbis_calculate_page_duration (psf) ; - vorbis_synthesis_restart (&vdata->vdsp) ; - } ; + if (ret < 0) + return ret ; + + ret = ogg_stream_unpack_page (psf, odata) ; + if (ret > 0) + { /* Reset the decoder, recalculate position */ + vorbis_synthesis_restart (&vdata->vdsp) ; + ret = vorbis_calculate_granulepos (psf, &vdata->gp) ; } ; return ret ; @@ -928,22 +922,23 @@ vorbis_seek (SF_PRIVATE *psf, int UNUSED (mode), sf_count_t offset) if (psf->file.mode == SFM_READ) { target_gp = (uint64_t) offset + vdata->pcm_start ; + ret = vorbis_seek_trysearch (psf, target_gp) ; - if (ret < 0 || vdata->loc > target_gp) + if (ret < 0 || vdata->gp > target_gp) { /* Search failed (bad data?), reset to the beginning of the stream. */ - psf_log_printf (psf, "Seek search failed. Reading through stream from start.\n") ; + psf_log_printf (psf, "Vorbis: Seek search failed. Reading through stream from start.\n") ; ogg_stream_reset_serialno (&odata->ostream, odata->ostream.serialno) ; odata->pkt_len = 0 ; odata->pkt_indx = 0 ; ogg_sync_fseek (psf, psf->dataoffset, SEEK_SET) ; - vdata->loc = 0 ; + vdata->gp = vdata->pcm_start ; vorbis_synthesis_restart (&vdata->vdsp) ; } ; vorbis_skip (psf, target_gp) ; - return vdata->loc - vdata->pcm_start ; + return vdata->gp - vdata->pcm_start ; } ; psf->error = SFE_BAD_SEEK ; @@ -959,31 +954,88 @@ vorbis_byterate (SF_PRIVATE *psf) return -1 ; } /* vorbis_byterate */ -static sf_count_t -vorbis_calculate_page_duration (SF_PRIVATE *psf) +static int +vorbis_calculate_granulepos (SF_PRIVATE *psf, uint64_t *gp_out) { OGG_PRIVATE *odata = (OGG_PRIVATE *) psf->container_data ; VORBIS_PRIVATE *vdata = (VORBIS_PRIVATE *) psf->codec_data ; + ogg_packet *pkt ; + uint64_t last_gp ; int thisblock, lastblock, i ; - sf_count_t duration ; + unsigned duration ; - /* - ** Calculate how many samples can be decoded from blocks in this page, - ** accounting for the fact that blocks are 1/2 lapped. - */ + /* Calculate the granule position when dropped into the middle of a stream + ** with an un-primed decoder. + ** + ** Normally the last unpacked packet contains the granule position of the + ** last completed sample from decoding all the blocks in the page's + ** packets. By calculating how many samples we can decode from the blocks + ** in the page's packets and subtracting it from the final packet's granule + ** position we get the position of the first sample to be output from the + ** decoder after it primes. That is, the current granule position. + ** + ** However, there is an ambiguity if this is the last page of a stream. The + ** last page of a stream may have a granule position of fewer samples than + ** the page actually contains. The excess samples are padding leftovers + ** for and exact sample length file. */ - lastblock = -1 ; - duration = 0 ; - for (i = 0 ; i < odata->pkt_len ; i++) - { thisblock = vorbis_packet_blocksize (&vdata->vinfo, &(odata->pkt [i])) ; - if (thisblock >= 0) - { if (lastblock != -1) - duration += (lastblock + thisblock) >> 2 ; - lastblock = thisblock ; + if (odata->pkt_len > 0) + { /* Calculate how many samples can be decoded from blocks in this page, + ** accounting for the fact that blocks are 1/2 lapped. */ + lastblock = -1 ; + duration = 0 ; + pkt = odata->pkt ; + for (i = 0 ; i < odata->pkt_len ; i++) + { thisblock = vorbis_packet_blocksize (&vdata->vinfo, &pkt [i]) ; + if (thisblock >= 0) + { if (lastblock != -1) + duration += (lastblock + thisblock) >> 2 ; + lastblock = thisblock ; + } ; } ; + + pkt = &odata->pkt [odata->pkt_len - 1] ; + last_gp = pkt->granulepos ; + if (last_gp == (uint64_t) -1) + { psf_log_printf (psf, "Vorbis: Ogg page has no granule position, cannot calculate sample position!\n") ; + psf->error = SFE_MALFORMED_FILE ; + return -1 ; + } ; + + if (pkt->e_o_s) + { if (last_gp <= duration) + { /* Corner case: One page stream. Ogg/Vorbis spec dictates the + ** granule position offset MUST be zero, hence this first (and + ** only) page must start at 0. */ + *gp_out = 0 ; + return 1 ; + } ; + + /* Otherwise, we cannot know where we are without looking at the + ** blocks of the previous page. (The granule position of the + ** previous page is not enough, we need the block sizes.) + ** + ** We avoid this case by never allowing a bisection search to seek + ** beyond the second-to-last page, so the last page is always + ** approached with a known location and never dropped into. + ** + ** The only way we should be able to end up here is if there was a + ** hole in stream just before the last page, in which case all bets + ** are off anyways. */ + psf_log_printf (psf, "Vorbis: Cannot calculate ambiguous last page duration. Sample count may be wrong.\n") ; + } ; + + if (last_gp < duration) + { psf_log_printf (psf, "Vorbis: Granule position is nonsensical! (Missing end-of-stream marker?)\n") ; + psf->error = SFE_MALFORMED_FILE ; + return -1 ; + } ; + + *gp_out = last_gp - duration ; + return 1 ; } ; - return duration ; -} /* vorbis_calculate_page_duration */ + return 0 ; +} /* vorbis_calculate_granulepos */ #else /* HAVE_EXTERNAL_XIPH_LIBS */ From c4015330170081de77a1c39c44a7bfae1ca176bd Mon Sep 17 00:00:00 2001 From: evpobr Date: Tue, 16 Feb 2021 10:44:12 +0500 Subject: [PATCH 018/204] Update CHANGELOG.md --- CHANGELOG.md | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f63854e..9d286bf5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,13 +16,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed * `SFC_SET_DITHER_ON_READ` and `SFC_SET_DITHER_ON_WRITE` enums comments in - public header (#677). + public header, thanks @SmiVan (issue #677). ### Fixed * Typo in `docs/index.md`. * Memory leak in `caf_read_header`(), credit to OSS-Fuzz ([issue 30375](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=30375)). +* Stack overflow in `guess_file_type`(), thanks @bobsayshilol, credit to + OSS-Fuzz ([issue 29339](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=29339)). +* Abort in fuzzer, thanks @bobsayshilol, credit to OSS-Fuzz + ([issue 26257](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=26257)). +* Infinite loop in `svx_read_header`(), thanks @bobsayshilol, credit to OSS-Fuzz + ([issue 25442](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=25442)). +* GCC and Clang pedantic warnings, thanks @bobsayshilol. * Normalisation issue when scaling floating point data to `int` in - `replace_read_f2i`() (#702). + `replace_read_f2i`(), thanks @bobsayshilol, (issue #702). +* Missing samples when doing a partial read of Ogg file from index till the end + of file, thanks @arthurt (issue #643). + +### Security + +* Heap buffer overflow in `wavlike_ima_decode_block`(), thanks @bobsayshilol, + credit to OSS-Fuzz ([issue 25530](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=25530)). [Unreleased]: https://github.com/libsndfile/libsndfile/compare/1.0.31...HEAD From 1f324b7998371b09b452e7a6b12ce5a7514adcd0 Mon Sep 17 00:00:00 2001 From: evpobr Date: Tue, 16 Feb 2021 10:58:53 +0500 Subject: [PATCH 019/204] Remove file IO dead code This code has never actually been used. --- src/file_io.c | 383 +------------------------------------------------- 1 file changed, 1 insertion(+), 382 deletions(-) diff --git a/src/file_io.c b/src/file_io.c index 7bcd907b..d9b694ad 100644 --- a/src/file_io.c +++ b/src/file_io.c @@ -602,7 +602,7 @@ psf_fsync (SF_PRIVATE *psf) #endif } /* psf_fsync */ -#elif USE_WINDOWS_API +#else /* Win32 file i/o functions implemented using native Win32 API */ @@ -1177,386 +1177,5 @@ psf_ftruncate (SF_PRIVATE *psf, sf_count_t len) return retval ; } /* psf_ftruncate */ - -#else -/* Win32 file i/o functions implemented using Unix-style file i/o API */ - -/* Win32 has a 64 file offset seek function: -** -** __int64 _lseeki64 (int handle, __int64 offset, int origin) ; -** -** It also has a 64 bit fstat function: -** -** int fstati64 (int, struct _stati64) ; -** -** but the fscking thing doesn't work!!!!! The file size parameter returned -** by this function is only valid up until more data is written at the end of -** the file. That makes this function completely 100% useless. -*/ - -#include -#include - -/* Win32 */ int -psf_fopen (SF_PRIVATE *psf, const char *pathname, int open_mode) -{ int oflag, mode ; - - switch (open_mode) - { case SFM_READ : - oflag = O_RDONLY | O_BINARY ; - mode = 0 ; - break ; - - case SFM_WRITE : - oflag = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY ; - mode = S_IRUSR | S_IWUSR | S_IRGRP ; - break ; - - case SFM_RDWR : - oflag = O_RDWR | O_CREAT | O_BINARY ; - mode = S_IRUSR | S_IWUSR | S_IRGRP ; - break ; - - default : - psf->error = SFE_BAD_OPEN_MODE ; - return -1 ; - break ; - } ; - - if (mode == 0) - psf->file.filedes = open (pathname, oflag) ; - else - psf->file.filedes = open (pathname, oflag, mode) ; - - if (psf->file.filedes == -1) - psf_log_syserr (psf, errno) ; - - return psf->file.filedes ; -} /* psf_fopen */ - -/* Win32 */ sf_count_t -psf_fseek (SF_PRIVATE *psf, sf_count_t offset, int whence) -{ sf_count_t new_position ; - - if (psf->virtual_io) - return psf->vio.seek (offset, whence, psf->vio_user_data) ; - - switch (whence) - { case SEEK_SET : - offset += psf->fileoffset ; - break ; - - case SEEK_END : - if (psf->file.mode == SFM_WRITE) - { new_position = _lseeki64 (psf->file.filedes, offset, whence) ; - - if (new_position < 0) - psf_log_syserr (psf, errno) ; - - return new_position - psf->fileoffset ; - } ; - - /* Transform SEEK_END into a SEEK_SET, ie find the file - ** length add the requested offset (should be <= 0) to - ** get the offset wrt the start of file. - */ - whence = SEEK_SET ; - offset = _lseeki64 (psf->file.filedes, 0, SEEK_END) + offset ; - break ; - - default : - /* No need to do anything about SEEK_CUR. */ - break ; - } ; - - /* - ** Bypass weird Win32-ism if necessary. - ** _lseeki64() returns an "invalid parameter" error if called with the - ** offset == 0 and whence == SEEK_CUR. - *** Use the _telli64() function instead. - */ - if (offset == 0 && whence == SEEK_CUR) - new_position = _telli64 (psf->file.filedes) ; - else - new_position = _lseeki64 (psf->file.filedes, offset, whence) ; - - if (new_position < 0) - psf_log_syserr (psf, errno) ; - - new_position -= psf->fileoffset ; - - return new_position ; -} /* psf_fseek */ - -/* Win32 */ sf_count_t -psf_fread (void *ptr, sf_count_t bytes, sf_count_t items, SF_PRIVATE *psf) -{ sf_count_t total = 0 ; - ssize_t count ; - - if (psf->virtual_io) - return psf->vio.read (ptr, bytes*items, psf->vio_user_data) / bytes ; - - items *= bytes ; - - /* Do this check after the multiplication above. */ - if (items <= 0) - return 0 ; - - while (items > 0) - { /* Break the writes down to a sensible size. */ - count = (items > SENSIBLE_SIZE) ? SENSIBLE_SIZE : (ssize_t) items ; - - count = read (psf->file.filedes, ((char*) ptr) + total, (size_t) count) ; - - if (count == -1) - { if (errno == EINTR) - continue ; - - psf_log_syserr (psf, errno) ; - break ; - } ; - - if (count == 0) - break ; - - total += count ; - items -= count ; - } ; - - return total / bytes ; -} /* psf_fread */ - -/* Win32 */ sf_count_t -psf_fwrite (const void *ptr, sf_count_t bytes, sf_count_t items, SF_PRIVATE *psf) -{ sf_count_t total = 0 ; - ssize_t count ; - - if (psf->virtual_io) - return psf->vio.write (ptr, bytes*items, psf->vio_user_data) / bytes ; - - items *= bytes ; - - /* Do this check after the multiplication above. */ - if (items <= 0) - return 0 ; - - while (items > 0) - { /* Break the writes down to a sensible size. */ - count = (items > SENSIBLE_SIZE) ? SENSIBLE_SIZE : items ; - - count = write (psf->file.filedes, ((const char*) ptr) + total, count) ; - - if (count == -1) - { if (errno == EINTR) - continue ; - - psf_log_syserr (psf, errno) ; - break ; - } ; - - if (count == 0) - break ; - - total += count ; - items -= count ; - } ; - - return total / bytes ; -} /* psf_fwrite */ - -/* Win32 */ sf_count_t -psf_ftell (SF_PRIVATE *psf) -{ sf_count_t pos ; - - if (psf->virtual_io) - return psf->vio.tell (psf->vio_user_data) ; - - pos = _telli64 (psf->file.filedes) ; - - if (pos == ((sf_count_t) -1)) - { psf_log_syserr (psf, errno) ; - return -1 ; - } ; - - return pos - psf->fileoffset ; -} /* psf_ftell */ - -/* Win32 */ int -psf_fclose (SF_PRIVATE *psf) -{ int retval ; - - while ((retval = close (psf->file.filedes)) == -1 && errno == EINTR) - /* Do nothing. */ ; - - if (retval == -1) - psf_log_syserr (psf, errno) ; - - psf->file.filedes = -1 ; - - return retval ; -} /* psf_fclose */ - -/* Win32 */ sf_count_t -psf_fgets (char *buffer, sf_count_t bufsize, SF_PRIVATE *psf) -{ sf_count_t k = 0 ; - sf_count_t count ; - - while (k < bufsize - 1) - { count = read (psf->file.filedes, &(buffer [k]), 1) ; - - if (count == -1) - { if (errno == EINTR) - continue ; - - psf_log_syserr (psf, errno) ; - break ; - } ; - - if (count == 0 || buffer [k++] == '\n') - break ; - } ; - - buffer [k] = 0 ; - - return k ; -} /* psf_fgets */ - -/* Win32 */ int -psf_is_pipe (SF_PRIVATE *psf) -{ struct stat statbuf ; - - if (psf->virtual_io) - return SF_FALSE ; - - /* Not sure if this works. */ - if (fstat (psf->file.filedes, &statbuf) == -1) - { psf_log_syserr (psf, errno) ; - /* Default to maximum safety. */ - return SF_TRUE ; - } ; - - /* These macros are defined in Win32/unistd.h. */ - if (S_ISFIFO (statbuf.st_mode) || S_ISSOCK (statbuf.st_mode)) - return SF_TRUE ; - - return SF_FALSE ; -} /* psf_checkpipe */ - -/* Win32 */ sf_count_t -psf_get_filelen (SF_PRIVATE *psf) -{ -#if 0 - /* - ** Windoze is SOOOOO FUCKED!!!!!!! - ** This code should work but doesn't. Why? - ** Code below does work. - */ - struct _stati64 statbuf ; - - if (_fstati64 (psf->file.filedes, &statbuf)) - { psf_log_syserr (psf, errno) ; - return (sf_count_t) -1 ; - } ; - - return statbuf.st_size ; -#else - sf_count_t current, filelen ; - - if (psf->virtual_io) - return psf->vio.get_filelen (psf->vio_user_data) ; - - if ((current = _telli64 (psf->file.filedes)) < 0) - { psf_log_syserr (psf, errno) ; - return (sf_count_t) -1 ; - } ; - - /* - ** Lets face it, windoze if FUBAR!!! - ** - ** For some reason, I have to call _lseeki64() TWICE to get to the - ** end of the file. - ** - ** This might have been avoided if windows had implemented the POSIX - ** standard function fsync() but NO, that would have been too easy. - ** - ** I am VERY close to saying that windoze will no longer be supported - ** by libsndfile and changing the license to GPL at the same time. - */ - - _lseeki64 (psf->file.filedes, 0, SEEK_END) ; - - if ((filelen = _lseeki64 (psf->file.filedes, 0, SEEK_END)) < 0) - { psf_log_syserr (psf, errno) ; - return (sf_count_t) -1 ; - } ; - - if (filelen > current) - _lseeki64 (psf->file.filedes, current, SEEK_SET) ; - - switch (psf->file.mode) - { case SFM_WRITE : - filelen = filelen - psf->fileoffset ; - break ; - - case SFM_READ : - if (psf->fileoffset > 0 && psf->filelength > 0) - filelen = psf->filelength ; - break ; - - case SFM_RDWR : - /* - ** Cannot open embedded files SFM_RDWR so we don't need to - ** subtract psf->fileoffset. We already have the answer we - ** need. - */ - break ; - - default : - filelen = 0 ; - } ; - - return filelen ; -#endif -} /* psf_get_filelen */ - -/* Win32 */ int -psf_ftruncate (SF_PRIVATE *psf, sf_count_t len) -{ int retval ; - - /* Returns 0 on success, non-zero on failure. */ - if (len < 0) - return 1 ; - - /* The global village idiots at micorsoft decided to implement - ** nearly all the required 64 bit file offset functions except - ** for one, truncate. The fscking morons! - ** - ** This is not 64 bit file offset clean. Somone needs to clean - ** this up. - */ - if (len > 0x7FFFFFFF) - return -1 ; - - retval = chsize (psf->file.filedes, len) ; - - if (retval == -1) - psf_log_syserr (psf, errno) ; - - return retval ; -} /* psf_ftruncate */ - - -static void -psf_log_syserr (SF_PRIVATE *psf, int error) -{ - /* Only log an error if no error has been set yet. */ - if (psf->error == 0) - { psf->error = SFE_SYSTEM ; - snprintf (psf->syserr, sizeof (psf->syserr), "System error : %s", strerror (error)) ; - } ; - - return ; -} /* psf_log_syserr */ - #endif From 610c6392cbc0c0bd97d146bee9853383cf094241 Mon Sep 17 00:00:00 2001 From: evpobr Date: Wed, 17 Feb 2021 13:44:15 +0500 Subject: [PATCH 020/204] Fix deprecated function warning The `GetVersionEx` function has been deprecated. Since it doesn't make much sense here, it has been deleted. --- tests/error_test.c | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/tests/error_test.c b/tests/error_test.c index 248f26ff..495d88b5 100644 --- a/tests/error_test.c +++ b/tests/error_test.c @@ -29,10 +29,6 @@ #include "sf_unistd.h" #endif -#if OS_IS_WIN32 -#include -#endif - #include #include "utils.h" @@ -213,19 +209,10 @@ error_close_test (void) if (sf_close (sndfile) == 0) { -#if OS_IS_WIN32 - OSVERSIONINFOEX osvi ; - - memset (&osvi, 0, sizeof (OSVERSIONINFOEX)) ; - osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX) ; - - if (GetVersionEx ((OSVERSIONINFO *) &osvi)) - { printf ("\n\nLine %d : sf_close should not have returned zero.\n", __LINE__) ; - printf ("\nHowever, this is a known bug in version %d.%d of windows so we'll ignore it.\n\n", - (int) osvi.dwMajorVersion, (int) osvi.dwMinorVersion) ; - } ; -#else printf ("\n\nLine %d : sf_close should not have returned zero.\n", __LINE__) ; +#if OS_IS_WIN32 + printf ("\nHowever, this is a known bug on windows platform so we'll ignore it.\n\n") ; +#else exit (1) ; #endif } ; From 5d5eb6fed5940e130e749d632da916356568fa08 Mon Sep 17 00:00:00 2001 From: evpobr Date: Wed, 17 Feb 2021 15:14:59 +0500 Subject: [PATCH 021/204] Fix use of flexible arrays with MSVC Introduced by 0c80547. --- src/common.c | 4 ++++ src/test_broadcast_var.c | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/common.c b/src/common.c index 90371cb0..e0d25b83 100644 --- a/src/common.c +++ b/src/common.c @@ -1246,7 +1246,11 @@ psf_memset (void *s, int c, sf_count_t len) ** bodgy something up instead. */ +#ifdef _MSC_VER +typedef SF_CUES_VAR (0) SF_CUES_0 ; +#else typedef SF_CUES_VAR () SF_CUES_0 ; +#endif /* calculate size of SF_CUES struct given number of cues */ #define SF_CUES_VAR_SIZE(count) (sizeof (SF_CUES_0) + count * sizeof (SF_CUE_POINT)) diff --git a/src/test_broadcast_var.c b/src/test_broadcast_var.c index e75dc750..5ae81fb1 100644 --- a/src/test_broadcast_var.c +++ b/src/test_broadcast_var.c @@ -89,7 +89,11 @@ test_broadcast_var_set (void) static void test_broadcast_var_zero (void) { SF_PRIVATE sf_private, *psf ; +#ifdef _MSC_VER + SF_BROADCAST_INFO_VAR (0) bi ; +#else SF_BROADCAST_INFO_VAR () bi ; +#endif psf = &sf_private ; memset (psf, 0, sizeof (sf_private)) ; From d7e17811b16e2b340fa288b3f3a9f85c4e214a1f Mon Sep 17 00:00:00 2001 From: evpobr Date: Wed, 17 Feb 2021 13:33:15 +0500 Subject: [PATCH 022/204] Update commit message contributing guidlines Related to #705. --- CONTRIBUTING.md | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5439b018..0bf14b76 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -18,7 +18,30 @@ * Patches should always be submitted via a either Github "pull request" or a via emailed patches created using "git format-patch". * Patches for new features should include tests and documentation. -* Commit messages should follow the ["How to Write a Git Commit Message"](https://chris.beams.io/posts/git-commit/) guide. +* Commit messages should follow the ["How to Write a Git Commit Message"](https://chris.beams.io/posts/git-commit/) guide: + 1. Separate subject from body with a blank line + 2. Limit the subject line to 50 characters + 3. Capitalize the subject line + 4. Do not end the subject line with a period + 5. Use the imperative mood in the subject line + 6. Wrap the body at 72 characters + 7. Use the body to explain what and why vs. how + + Additional rule: the commit message may contain a prefix. The prefix must + contain the name of the feature or source file related to the commit and must + end with a colon followed by the message body. + + Examples of good commit messages: + 1. Fix typo + 2. Update CHANGELOG.md + 3. Add ACT file format support + 4. ogg_vorbis: Fix granule position when seeking Vorbis streams + + Examples of bad commit messages: + 1. Fixed bug (rule 5) + 2. update docs (rule 3) + 3. Add very cool feature. (rule 4) + * Patches to fix bugs should either pass all tests, or modify the tests in some sane way. * When a new feature is added for a particular file format and that feature From d11a64c7c45e52ac49b8f8c54bd4fc7143ebffb9 Mon Sep 17 00:00:00 2001 From: evpobr Date: Thu, 18 Feb 2021 13:09:16 +0500 Subject: [PATCH 023/204] Deprecate ENABLE_SNDFILE_WINDOWS_PROTOTYPES define Use standard `_WIN32` define instead. Any sane compiler on Windows platfrom defines `_WIN32`. The `wpath` parameter type has been changed to an equivalent type that does not require the inclusion of the `windows.h` header. --- CHANGELOG.md | 15 +++++++++++++++ include/sndfile.h.in | 10 ++-------- include/sndfile.hh | 8 ++++---- src/windows.c | 1 - tests/locale_test.c | 1 - 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d286bf5..3766784d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * `SFC_SET_DITHER_ON_READ` and `SFC_SET_DITHER_ON_WRITE` enums comments in public header, thanks @SmiVan (issue #677). +* `ENABLE_SNDFILE_WINDOWS_PROTOTYPES` define is deprecated and not needed + anymore. + + Previously, in order for the [`sf_wchar_open`()](http://libsndfile.github.io/libsndfile/api.html#open) + function to become available on the Windows platform, it was required to + perform certain actions: + + ```c + #include + #define ENABLE_SNDFILE_WINDOWS_PROTOTYPES 1 + #including + ``` + + These steps are no longer required and the `sf_wchar_open`() function is + always available on the Windows platform. ### Fixed diff --git a/include/sndfile.h.in b/include/sndfile.h.in index 5e30c405..98c1ac34 100644 --- a/include/sndfile.h.in +++ b/include/sndfile.h.in @@ -755,16 +755,10 @@ void sf_write_sync (SNDFILE *sndfile) ; /* The function sf_wchar_open() is Windows Only! ** Open a file passing in a Windows Unicode filename. Otherwise, this is ** the same as sf_open(). -** -** In order for this to work, you need to do the following: -** -** #include -** #define ENABLE_SNDFILE_WINDOWS_PROTOTYPES 1 -** #including */ -#if (defined (ENABLE_SNDFILE_WINDOWS_PROTOTYPES) && ENABLE_SNDFILE_WINDOWS_PROTOTYPES) -SNDFILE* sf_wchar_open (LPCWSTR wpath, int mode, SF_INFO *sfinfo) ; +#ifdef _WIN32 +SNDFILE* sf_wchar_open (const wchar_t *wpath, int mode, SF_INFO *sfinfo) ; #endif diff --git a/include/sndfile.hh b/include/sndfile.hh index ae9af3f3..dc1df391 100644 --- a/include/sndfile.hh +++ b/include/sndfile.hh @@ -88,8 +88,8 @@ class SndfileHandle SndfileHandle (SF_VIRTUAL_IO &sfvirtual, void *user_data, int mode = SFM_READ, int format = 0, int channels = 0, int samplerate = 0) ; -#ifdef ENABLE_SNDFILE_WINDOWS_PROTOTYPES - SndfileHandle (LPCWSTR wpath, int mode = SFM_READ, +#ifdef _WIN32 + SndfileHandle (const wchar_t *wpath, int mode = SFM_READ, int format = 0, int channels = 0, int samplerate = 0) ; #endif @@ -422,10 +422,10 @@ SndfileHandle::takeOwnership (void) return sf ; } -#ifdef ENABLE_SNDFILE_WINDOWS_PROTOTYPES +#ifdef _WIN32 inline -SndfileHandle::SndfileHandle (LPCWSTR wpath, int mode, int fmt, int chans, int srate) +SndfileHandle::SndfileHandle (const wchar_t *wpath, int mode, int fmt, int chans, int srate) : p (nullptr) { p = new (std::nothrow) SNDFILE_ref () ; diff --git a/src/windows.c b/src/windows.c index 2ef3002f..e24f8c25 100644 --- a/src/windows.c +++ b/src/windows.c @@ -27,7 +27,6 @@ #if OS_IS_WIN32 #include -#define ENABLE_SNDFILE_WINDOWS_PROTOTYPES 1 #include "sndfile.h" #include "common.h" diff --git a/tests/locale_test.c b/tests/locale_test.c index b2653016..932c72ef 100644 --- a/tests/locale_test.c +++ b/tests/locale_test.c @@ -34,7 +34,6 @@ #if OS_IS_WIN32 #include -#define ENABLE_SNDFILE_WINDOWS_PROTOTYPES 1 #endif #include "sndfile.h" From 2a8feebcdb288f8ddc1b0734c743f63f42734eb9 Mon Sep 17 00:00:00 2001 From: evpobr Date: Tue, 16 Feb 2021 12:08:06 +0500 Subject: [PATCH 024/204] Use UTF-8 as internal path encoding This is an internal change to unify and simplify the handling of file paths. On the Windows platform, the file path is always converted to UTF-8 and converted to UTF-16 only for calls to WinAPI functions. The behavior of the functions for opening files on non-Windows platforms does not change. --- CHANGELOG.md | 9 ++++ src/common.h | 29 ++++-------- src/file_io.c | 98 ++++++++++++++++++++++++-------------- src/mpc2k.c | 2 +- src/sd2.c | 2 +- src/sndfile.c | 104 ++++++++++++++++++++++++++++------------- src/svx.c | 10 ++-- src/test_conversions.c | 4 +- src/test_file_io.c | 12 ++--- src/windows.c | 47 ++++++------------- 10 files changed, 180 insertions(+), 137 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3766784d..5d68c579 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 These steps are no longer required and the `sf_wchar_open`() function is always available on the Windows platform. +* Use UTF-8 as internal path encoding on Windows platform. + + This is an internal change to unify and simplify the handling of file paths. + + On the Windows platform, the file path is always converted to UTF-8 and + converted to UTF-16 only for calls to WinAPI functions. + + The behavior of the functions for opening files on other platforms does not + change. ### Fixed diff --git a/src/common.h b/src/common.h index b94e678a..022e938d 100644 --- a/src/common.h +++ b/src/common.h @@ -323,36 +323,23 @@ psf_isprint (int ch) typedef struct { - union - { char c [SF_FILENAME_LEN] ; - sfwchar_t wc [SF_FILENAME_LEN] ; - } path ; - - union - { char c [SF_FILENAME_LEN] ; - sfwchar_t wc [SF_FILENAME_LEN] ; - } dir ; - - union - { char c [SF_FILENAME_LEN / 4] ; - sfwchar_t wc [SF_FILENAME_LEN / 4] ; - } name ; + char path [SF_FILENAME_LEN] ; + char dir [SF_FILENAME_LEN] ; + char name [SF_FILENAME_LEN / 4] ; #if USE_WINDOWS_API /* ** These fields can only be used in src/file_io.c. ** They are basically the same as a windows file HANDLE. */ - void *handle, *hsaved ; - - int use_wchar ; + void *handle, *hsaved ; #else /* These fields can only be used in src/file_io.c. */ - int filedes, savedes ; + int filedes, savedes ; #endif - int do_not_close_descriptor ; - int mode ; /* Open mode : SFM_READ, SFM_WRITE or SFM_RDWR. */ + int do_not_close_descriptor ; + int mode ; /* Open mode : SFM_READ, SFM_WRITE or SFM_RDWR. */ } PSF_FILE ; @@ -878,6 +865,8 @@ int psf_fclose (SF_PRIVATE *psf) ; int psf_open_rsrc (SF_PRIVATE *psf) ; int psf_close_rsrc (SF_PRIVATE *psf) ; +int psf_copy_filename (SF_PRIVATE *psf, const char *path) ; + /* void psf_fclearerr (SF_PRIVATE *psf) ; int psf_ferror (SF_PRIVATE *psf) ; diff --git a/src/file_io.c b/src/file_io.c index d9b694ad..93d604e7 100644 --- a/src/file_io.c +++ b/src/file_io.c @@ -76,6 +76,34 @@ static void psf_log_syserr (SF_PRIVATE *psf, int error) ; +int +psf_copy_filename (SF_PRIVATE *psf, const char *path) +{ const char *ccptr ; + char *cptr ; + + if (strlen (path) > 1 && strlen (path) - 1 >= sizeof (psf->file.path)) + { psf->error = SFE_FILENAME_TOO_LONG ; + return psf->error ; + } ; + + snprintf (psf->file.path, sizeof (psf->file.path), "%s", path) ; + if ((ccptr = strrchr (path, '/')) || (ccptr = strrchr (path, '\\'))) + ccptr ++ ; + else + ccptr = path ; + + snprintf (psf->file.name, sizeof (psf->file.name), "%s", ccptr) ; + + /* Now grab the directory. */ + snprintf (psf->file.dir, sizeof (psf->file.dir), "%s", path) ; + if ((cptr = strrchr (psf->file.dir, '/')) || (cptr = strrchr (psf->file.dir, '\\'))) + cptr [1] = 0 ; + else + psf->file.dir [0] = 0 ; + + return 0 ; +} /* psf_copy_filename */ + #if (USE_WINDOWS_API == 0) /*------------------------------------------------------------------------------ @@ -132,9 +160,9 @@ psf_open_rsrc (SF_PRIVATE *psf) return 0 ; /* Test for MacOSX style resource fork on HPFS or HPFS+ filesystems. */ - count = snprintf (psf->rsrc.path.c, sizeof (psf->rsrc.path.c), "%s/..namedfork/rsrc", psf->file.path.c) ; + count = snprintf (psf->rsrc.path, sizeof (psf->rsrc.path), "%s/..namedfork/rsrc", psf->file.path) ; psf->error = SFE_NO_ERROR ; - if (count < sizeof (psf->rsrc.path.c)) + if (count < sizeof (psf->rsrc.path)) { if ((psf->rsrc.filedes = psf_open_fd (&psf->rsrc)) >= 0) { psf->rsrclength = psf_get_filelen_fd (psf->rsrc.filedes) ; if (psf->rsrclength > 0 || (psf->rsrc.mode & SFM_WRITE)) @@ -153,9 +181,9 @@ psf_open_rsrc (SF_PRIVATE *psf) ** Now try for a resource fork stored as a separate file in the same ** directory, but preceded with a dot underscore. */ - count = snprintf (psf->rsrc.path.c, sizeof (psf->rsrc.path.c), "%s._%s", psf->file.dir.c, psf->file.name.c) ; + count = snprintf (psf->rsrc.path, sizeof (psf->rsrc.path), "%s._%s", psf->file.dir, psf->file.name) ; psf->error = SFE_NO_ERROR ; - if (count < sizeof (psf->rsrc.path.c) && (psf->rsrc.filedes = psf_open_fd (&psf->rsrc)) >= 0) + if (count < sizeof (psf->rsrc.path) && (psf->rsrc.filedes = psf_open_fd (&psf->rsrc)) >= 0) { psf->rsrclength = psf_get_filelen_fd (psf->rsrc.filedes) ; return SFE_NO_ERROR ; } ; @@ -164,9 +192,9 @@ psf_open_rsrc (SF_PRIVATE *psf) ** Now try for a resource fork stored in a separate file in the ** .AppleDouble/ directory. */ - count = snprintf (psf->rsrc.path.c, sizeof (psf->rsrc.path.c), "%s.AppleDouble/%s", psf->file.dir.c, psf->file.name.c) ; + count = snprintf (psf->rsrc.path, sizeof (psf->rsrc.path), "%s.AppleDouble/%s", psf->file.dir, psf->file.name) ; psf->error = SFE_NO_ERROR ; - if (count < sizeof (psf->rsrc.path.c)) + if (count < sizeof (psf->rsrc.path)) { if ((psf->rsrc.filedes = psf_open_fd (&psf->rsrc)) >= 0) { psf->rsrclength = psf_get_filelen_fd (psf->rsrc.filedes) ; return SFE_NO_ERROR ; @@ -572,9 +600,9 @@ psf_open_fd (PSF_FILE * pfile) } ; if (mode == 0) - fd = open (pfile->path.c, oflag) ; + fd = open (pfile->path, oflag) ; else - fd = open (pfile->path.c, oflag, mode) ; + fd = open (pfile->path, oflag, mode) ; return fd ; } /* psf_open_fd */ @@ -653,7 +681,7 @@ psf_open_rsrc (SF_PRIVATE *psf) return 0 ; /* Test for MacOSX style resource fork on HPFS or HPFS+ filesystems. */ - snprintf (psf->rsrc.path.c, sizeof (psf->rsrc.path.c), "%s/rsrc", psf->file.path.c) ; + snprintf (psf->rsrc.path, sizeof (psf->rsrc.path), "%s/rsrc", psf->file.path) ; psf->error = SFE_NO_ERROR ; if ((psf->rsrc.handle = psf_open_handle (&psf->rsrc)) != INVALID_HANDLE_VALUE) { psf->rsrclength = psf_get_filelen_handle (psf->rsrc.handle) ; @@ -664,7 +692,7 @@ psf_open_rsrc (SF_PRIVATE *psf) ** Now try for a resource fork stored as a separate file in the same ** directory, but preceded with a dot underscore. */ - snprintf (psf->rsrc.path.c, sizeof (psf->rsrc.path.c), "%s._%s", psf->file.dir.c, psf->file.name.c) ; + snprintf (psf->rsrc.path, sizeof (psf->rsrc.path), "%s._%s", psf->file.dir, psf->file.name) ; psf->error = SFE_NO_ERROR ; if ((psf->rsrc.handle = psf_open_handle (&psf->rsrc)) != INVALID_HANDLE_VALUE) { psf->rsrclength = psf_get_filelen_handle (psf->rsrc.handle) ; @@ -675,7 +703,7 @@ psf_open_rsrc (SF_PRIVATE *psf) ** Now try for a resource fork stored in a separate file in the ** .AppleDouble/ directory. */ - snprintf (psf->rsrc.path.c, sizeof (psf->rsrc.path.c), "%s.AppleDouble/%s", psf->file.dir.c, psf->file.name.c) ; + snprintf (psf->rsrc.path, sizeof (psf->rsrc.path), "%s.AppleDouble/%s", psf->file.dir, psf->file.name) ; psf->error = SFE_NO_ERROR ; if ((psf->rsrc.handle = psf_open_handle (&psf->rsrc)) != INVALID_HANDLE_VALUE) { psf->rsrclength = psf_get_filelen_handle (psf->rsrc.handle) ; @@ -762,6 +790,7 @@ psf_open_handle (PSF_FILE * pfile) DWORD dwShareMode ; DWORD dwCreationDistribution ; HANDLE handle ; + LPCWSTR pwszPath = NULL ; switch (pfile->mode) { case SFM_READ : @@ -786,6 +815,17 @@ psf_open_handle (PSF_FILE * pfile) return INVALID_HANDLE_VALUE ; } ; + int nResult = MultiByteToWideChar (CP_UTF8, 0, pfile->path, -1, NULL, 0) ; + pwszPath = malloc (nResult * sizeof (WCHAR)) ; + if (!pwszPath) + return INVALID_HANDLE_VALUE ; + + int nResult2 = MultiByteToWideChar (CP_UTF8, 0, pfile->path, -1, pwszPath, nResult) ; + if (nResult != nResult2) + { free (pwszPath) ; + return INVALID_HANDLE_VALUE ; + } ; + #if defined (WINAPI_FAMILY_PARTITION) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM) if (!pfile->use_wchar) return INVALID_HANDLE_VALUE ; @@ -794,33 +834,21 @@ psf_open_handle (PSF_FILE * pfile) cfParams.dwSize = sizeof (CREATEFILE2_EXTENDED_PARAMETERS) ; cfParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL ; - handle = CreateFile2 (pfile->path.wc, dwDesiredAccess, dwShareMode, dwCreationDistribution, &cfParams) ; - - return handle ; + handle = CreateFile2 (pwszPath, dwDesiredAccess, dwShareMode, dwCreationDistribution, &cfParams) ; #else - if (pfile->use_wchar) - handle = CreateFileW ( - pfile->path.wc, /* pointer to name of the file */ - dwDesiredAccess, /* access (read-write) mode */ - dwShareMode, /* share mode */ - 0, /* pointer to security attributes */ - dwCreationDistribution, /* how to create */ - FILE_ATTRIBUTE_NORMAL, /* file attributes (could use FILE_FLAG_SEQUENTIAL_SCAN) */ - NULL /* handle to file with attributes to copy */ - ) ; - else - handle = CreateFileA ( - pfile->path.c, /* pointer to name of the file */ - dwDesiredAccess, /* access (read-write) mode */ - dwShareMode, /* share mode */ - 0, /* pointer to security attributes */ - dwCreationDistribution, /* how to create */ - FILE_ATTRIBUTE_NORMAL, /* file attributes (could use FILE_FLAG_SEQUENTIAL_SCAN) */ - NULL /* handle to file with attributes to copy */ - ) ; + handle = CreateFileW ( + pwszPath, /* pointer to name of the file */ + dwDesiredAccess, /* access (read-write) mode */ + dwShareMode, /* share mode */ + 0, /* pointer to security attributes */ + dwCreationDistribution, /* how to create */ + FILE_ATTRIBUTE_NORMAL, /* file attributes (could use FILE_FLAG_SEQUENTIAL_SCAN) */ + NULL /* handle to file with attributes to copy */ + ) ; +#endif + free (pwszPath) ; return handle ; -#endif } /* psf_open_handle */ /* USE_WINDOWS_API */ static void diff --git a/src/mpc2k.c b/src/mpc2k.c index 9f490eab..733f1f4b 100644 --- a/src/mpc2k.c +++ b/src/mpc2k.c @@ -129,7 +129,7 @@ mpc2k_write_header (SF_PRIVATE *psf, int calc_length) if (psf->is_pipe == SF_FALSE) psf_fseek (psf, 0, SEEK_SET) ; - snprintf (sample_name, sizeof (sample_name), "%-*.*s", HEADER_NAME_LEN, HEADER_NAME_LEN, psf->file.name.c) ; + snprintf (sample_name, sizeof (sample_name), "%-*.*s", HEADER_NAME_LEN, HEADER_NAME_LEN, psf->file.name) ; psf_binheader_writef (psf, "e11b", BHW1 (1), BHW1 (4), BHWv (sample_name), BHWz (HEADER_NAME_LEN)) ; psf_binheader_writef (psf, "e111", BHW1 (100), BHW1 (0), BHW1 ((psf->sf.channels - 1) & 1)) ; diff --git a/src/sd2.c b/src/sd2.c index dd3dbef6..f5ef9de2 100644 --- a/src/sd2.c +++ b/src/sd2.c @@ -240,7 +240,7 @@ sd2_write_rsrc_fork (SF_PRIVATE *psf, int UNUSED (calc_length)) /* Very start of resource fork. */ psf_binheader_writef (psf, "E444", BHW4 (rsrc.data_offset), BHW4 (rsrc.map_offset), BHW4 (rsrc.data_length)) ; - psf_binheader_writef (psf, "Eop", BHWo (0x30), BHWp (psf->file.name.c)) ; + psf_binheader_writef (psf, "Eop", BHWo (0x30), BHWp (psf->file.name)) ; psf_binheader_writef (psf, "Eo2mm", BHWo (0x50), BHW2 (0), BHWm (Sd2f_MARKER), BHWm (lsf1_MARKER)) ; /* Very start of resource map. */ diff --git a/src/sndfile.c b/src/sndfile.c index 6de3963d..f2b49f63 100644 --- a/src/sndfile.c +++ b/src/sndfile.c @@ -33,6 +33,11 @@ #include #endif +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#endif + #define SNDFILE_MAGICK 0x1234C0DE #ifdef __APPLE__ @@ -300,7 +305,6 @@ static int guess_file_type (SF_PRIVATE *psf) ; static int validate_sfinfo (SF_INFO *sfinfo) ; static int validate_psf (SF_PRIVATE *psf) ; static void save_header_info (SF_PRIVATE *psf) ; -static int copy_filename (SF_PRIVATE *psf, const char *path) ; static int psf_close (SF_PRIVATE *psf) ; static int try_resource_fork (SF_PRIVATE * psf) ; @@ -341,6 +345,14 @@ static char sf_syserr [SF_SYSERR_LEN] = { 0 } ; SNDFILE* sf_open (const char *path, int mode, SF_INFO *sfinfo) { SF_PRIVATE *psf ; + const char *utf8path_ptr ; +#ifdef _WIN32 + LPWSTR wpath ; + int nResult ; + int wpath_len ; + char utf8path [SF_FILENAME_LEN] ; + DWORD dwError ; +#endif /* Ultimate sanity check. */ assert (sizeof (sf_count_t) == 8) ; @@ -354,8 +366,62 @@ sf_open (const char *path, int mode, SF_INFO *sfinfo) psf_log_printf (psf, "File : %s\n", path) ; - if (copy_filename (psf, path) != 0) +#ifdef _WIN32 + nResult = MultiByteToWideChar (CP_ACP, 0, path, -1, NULL, 0) ; + if (nResult == 0) + { sf_errno = SF_ERR_UNSUPPORTED_ENCODING ; + psf_close (psf) ; + return NULL ; + } ; + + wpath_len = nResult ; + wpath = malloc (wpath_len * sizeof (WCHAR)) ; + if (!wpath) + { sf_errno = SFE_MALLOC_FAILED ; + psf_close (psf) ; + return NULL ; + } ; + + nResult = MultiByteToWideChar (CP_ACP, 0, path, -1, wpath, wpath_len) ; + if (nResult == 0) + { sf_errno = SF_ERR_UNSUPPORTED_ENCODING ; + free (wpath) ; + psf_close (psf) ; + return NULL ; + } ; + + nResult = WideCharToMultiByte (CP_UTF8, 0, wpath, wpath_len, NULL, 0, NULL, + NULL) ; + if (nResult == 0) + { sf_errno = SF_ERR_UNSUPPORTED_ENCODING ; + free (wpath) ; + psf_close (psf) ; + return NULL ; + } ; + + nResult = WideCharToMultiByte (CP_UTF8, 0, wpath, wpath_len, utf8path, + SF_FILENAME_LEN, NULL, NULL) ; + + free (wpath) ; + + if (nResult == 0) + { dwError = GetLastError () ; + if (dwError == ERROR_INSUFFICIENT_BUFFER) + sf_errno = SFE_FILENAME_TOO_LONG ; + else + sf_errno = SF_ERR_UNSUPPORTED_ENCODING ; + psf_close (psf) ; + return NULL ; + } ; + + utf8path_ptr = utf8path ; +#else + utf8path_ptr = path ; +#endif + + if (psf_copy_filename (psf, utf8path_ptr) != 0) { sf_errno = psf->error ; + psf_close (psf) ; return NULL ; } ; @@ -390,7 +456,7 @@ sf_open_fd (int fd, int mode, SF_INFO *sfinfo, int close_desc) } ; psf_init_files (psf) ; - copy_filename (psf, "") ; + psf_copy_filename (psf, "") ; psf->file.mode = mode ; psf_set_file (psf, fd) ; @@ -2621,7 +2687,7 @@ try_resource_fork (SF_PRIVATE * psf) } ; /* More checking here. */ - psf_log_printf (psf, "Resource fork : %s\n", psf->rsrc.path.c) ; + psf_log_printf (psf, "Resource fork : %s\n", psf->rsrc.path) ; return SF_FORMAT_SD2 ; } /* try_resource_fork */ @@ -2632,7 +2698,7 @@ format_from_extension (SF_PRIVATE *psf) char buffer [16] ; int format = 0 ; - if ((cptr = strrchr (psf->file.name.c, '.')) == NULL) + if ((cptr = strrchr (psf->file.name, '.')) == NULL) return 0 ; cptr ++ ; @@ -2854,34 +2920,6 @@ save_header_info (SF_PRIVATE *psf) { snprintf (sf_parselog, sizeof (sf_parselog), "%s", psf->parselog.buf) ; } /* save_header_info */ -static int -copy_filename (SF_PRIVATE *psf, const char *path) -{ const char *ccptr ; - char *cptr ; - - if (strlen (path) > 1 && strlen (path) - 1 >= sizeof (psf->file.path.c)) - { psf->error = SFE_FILENAME_TOO_LONG ; - return psf->error ; - } ; - - snprintf (psf->file.path.c, sizeof (psf->file.path.c), "%s", path) ; - if ((ccptr = strrchr (path, '/')) || (ccptr = strrchr (path, '\\'))) - ccptr ++ ; - else - ccptr = path ; - - snprintf (psf->file.name.c, sizeof (psf->file.name.c), "%s", ccptr) ; - - /* Now grab the directory. */ - snprintf (psf->file.dir.c, sizeof (psf->file.dir.c), "%s", path) ; - if ((cptr = strrchr (psf->file.dir.c, '/')) || (cptr = strrchr (psf->file.dir.c, '\\'))) - cptr [1] = 0 ; - else - psf->file.dir.c [0] = 0 ; - - return 0 ; -} /* copy_filename */ - /*============================================================================== */ diff --git a/src/svx.c b/src/svx.c index 19e93ea4..a6b72324 100644 --- a/src/svx.c +++ b/src/svx.c @@ -240,12 +240,12 @@ svx_read_header (SF_PRIVATE *psf) psf_log_printf (psf, " %M : %u\n", marker, chunk_size) ; - if (strlen (psf->file.name.c) != chunk_size) - { if (chunk_size > sizeof (psf->file.name.c) - 1) + if (strlen (psf->file.name) != chunk_size) + { if (chunk_size > sizeof (psf->file.name) - 1) return SFE_SVX_BAD_NAME_LENGTH ; - psf_binheader_readf (psf, "b", psf->file.name.c, chunk_size) ; - psf->file.name.c [chunk_size] = 0 ; + psf_binheader_readf (psf, "b", psf->file.name, chunk_size) ; + psf->file.name [chunk_size] = 0 ; } else psf_binheader_readf (psf, "j", chunk_size) ; @@ -381,7 +381,7 @@ svx_write_header (SF_PRIVATE *psf, int calc_length) psf_binheader_writef (psf, "Em44", BHWm (CHAN_MARKER), BHW4 (4), BHW4 (6)) ; /* Filename and annotation strings. */ - psf_binheader_writef (psf, "Emsms", BHWm (NAME_MARKER), BHWs (psf->file.name.c), BHWm (ANNO_MARKER), BHWs (annotation)) ; + psf_binheader_writef (psf, "Emsms", BHWm (NAME_MARKER), BHWs (psf->file.name), BHWm (ANNO_MARKER), BHWs (annotation)) ; /* BODY marker and size. */ psf_binheader_writef (psf, "Etm8", BHWm (BODY_MARKER), BHW8 ((psf->datalength < 0) ? diff --git a/src/test_conversions.c b/src/test_conversions.c index 329e2260..f6ee36e0 100644 --- a/src/test_conversions.c +++ b/src/test_conversions.c @@ -62,7 +62,7 @@ conversion_test (char endian) memset (psf, 0, sizeof (sf_private)) ; psf->file.mode = SFM_WRITE ; - snprintf (psf->file.path.c, sizeof (psf->file.path.c), "%s", filename) ; + snprintf (psf->file.path, sizeof (psf->file.path), "%s", filename) ; if (psf_fopen (psf) != 0) { printf ("\n\nError : failed to open file '%s' for write.\n\n", filename) ; @@ -77,7 +77,7 @@ conversion_test (char endian) memset (psf, 0, sizeof (sf_private)) ; psf->file.mode = SFM_READ ; - snprintf (psf->file.path.c, sizeof (psf->file.path.c), "%s", filename) ; + snprintf (psf->file.path, sizeof (psf->file.path), "%s", filename) ; if (psf_fopen (psf) != 0) { printf ("\n\nError : failed to open file '%s' for read.\n\n", filename) ; diff --git a/src/test_file_io.c b/src/test_file_io.c index 36a85b0c..850cae2b 100644 --- a/src/test_file_io.c +++ b/src/test_file_io.c @@ -73,7 +73,7 @@ file_open_test (const char *filename) } ; psf->file.mode = SFM_READ ; - snprintf (psf->file.path.c, sizeof (psf->file.path.c), "%s", filename) ; + snprintf (psf->file.path, sizeof (psf->file.path), "%s", filename) ; /* Test that open for read fails if the file doesn't exist. */ error = psf_fopen (psf) ; @@ -91,7 +91,7 @@ file_open_test (const char *filename) test_close_or_die (psf, __LINE__) ; - unlink (psf->file.path.c) ; + unlink (psf->file.path) ; /* Test file open in read/write mode for a non-existant file. */ psf->file.mode = SFM_RDWR ; @@ -105,7 +105,7 @@ file_open_test (const char *filename) test_close_or_die (psf, __LINE__) ; - unlink (psf->file.path.c) ; + unlink (psf->file.path) ; puts ("ok") ; } /* file_open_test */ @@ -126,7 +126,7 @@ file_read_write_test (const char *filename) memset (&sf_data, 0, sizeof (sf_data)) ; psf = &sf_data ; - snprintf (psf->file.path.c, sizeof (psf->file.path.c), "%s", filename) ; + snprintf (psf->file.path, sizeof (psf->file.path), "%s", filename) ; /* Test file open in write mode. */ psf->file.mode = SFM_WRITE ; @@ -282,7 +282,7 @@ file_truncate_test (const char *filename) memset (buffer, 0xEE, sizeof (buffer)) ; psf = &sf_data ; - snprintf (psf->file.path.c, sizeof (psf->file.path.c), "%s", filename) ; + snprintf (psf->file.path, sizeof (psf->file.path), "%s", filename) ; /* ** Open the file write mode, write 0xEE data and then extend the file @@ -339,7 +339,7 @@ file_seek_with_offset_test (const char *filename) memset (&sf_data, 0, sizeof (sf_data)) ; psf = &sf_data ; psf->file.mode = SFM_READ ; - snprintf (psf->file.path.c, sizeof (psf->file.path.c), "%s", filename) ; + snprintf (psf->file.path, sizeof (psf->file.path), "%s", filename) ; test_open_or_die (psf, __LINE__) ; /* Gather basic info before setting offset. */ diff --git a/src/windows.c b/src/windows.c index e24f8c25..c11ed9b4 100644 --- a/src/windows.c +++ b/src/windows.c @@ -32,12 +32,11 @@ extern int sf_errno ; -static void copy_filename (SF_PRIVATE * psf, LPCWSTR wpath) ; - SNDFILE* sf_wchar_open (LPCWSTR wpath, int mode, SF_INFO *sfinfo) { SF_PRIVATE *psf ; - char utf8name [512] ; + char utf8name [SF_BUFFER_LEN] ; + DWORD dwError ; if ((psf = psf_allocate ()) == NULL) { sf_errno = SFE_MALLOC_FAILED ; @@ -47,12 +46,20 @@ sf_wchar_open (LPCWSTR wpath, int mode, SF_INFO *sfinfo) psf_init_files (psf) ; if (WideCharToMultiByte (CP_UTF8, 0, wpath, -1, utf8name, sizeof (utf8name), NULL, NULL) == 0) - psf->file.path.wc [0] = 0 ; + { dwError = GetLastError () ; + if (dwError == ERROR_INSUFFICIENT_BUFFER) + sf_errno = SFE_FILENAME_TOO_LONG ; + else + sf_errno = SF_ERR_UNSUPPORTED_ENCODING ; + + sf_close (psf) ; + + return NULL ; + } ; psf_log_printf (psf, "File : '%s' (utf-8 converted from ucs-2)\n", utf8name) ; - copy_filename (psf, wpath) ; - psf->file.use_wchar = SF_TRUE ; + psf_copy_filename (psf, utf8name) ; psf->file.mode = mode ; psf->error = psf_fopen (psf) ; @@ -60,32 +67,4 @@ sf_wchar_open (LPCWSTR wpath, int mode, SF_INFO *sfinfo) return psf_open_file (psf, sfinfo) ; } /* sf_wchar_open */ - -static void -copy_filename (SF_PRIVATE *psf, LPCWSTR wpath) -{ const wchar_t *cwcptr ; - wchar_t *wcptr ; - - wcsncpy (psf->file.path.wc, wpath, ARRAY_LEN (psf->file.path.wc)) ; - psf->file.path.wc [ARRAY_LEN (psf->file.path.wc) - 1] = 0 ; - if ((cwcptr = wcsrchr (wpath, '/')) || (cwcptr = wcsrchr (wpath, '\\'))) - cwcptr ++ ; - else - cwcptr = wpath ; - - wcsncpy (psf->file.name.wc, cwcptr, ARRAY_LEN (psf->file.name.wc)) ; - psf->file.name.wc [ARRAY_LEN (psf->file.name.wc) - 1] = 0 ; - - /* Now grab the directory. */ - wcsncpy (psf->file.dir.wc, wpath, ARRAY_LEN (psf->file.dir.wc)) ; - psf->file.dir.wc [ARRAY_LEN (psf->file.dir.wc) - 1] = 0 ; - - if ((wcptr = wcsrchr (psf->file.dir.wc, '/')) || (wcptr = wcsrchr (psf->file.dir.wc, '\\'))) - wcptr [1] = 0 ; - else - psf->file.dir.wc [0] = 0 ; - - return ; -} /* copy_filename */ - #endif From ac9a7bbad4e088361fc54d4b6c81eab1f9115e6c Mon Sep 17 00:00:00 2001 From: evpobr Date: Fri, 19 Feb 2021 16:50:46 +0500 Subject: [PATCH 025/204] Remove useless union from sf_private_tag struct --- src/common.h | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/common.h b/src/common.h index 022e938d..52a34a6f 100644 --- a/src/common.h +++ b/src/common.h @@ -363,13 +363,6 @@ typedef union typedef struct sf_private_tag { - /* Canary in a coal mine. */ - union - { /* Place a double here to encourage double alignment. */ - double d [2] ; - char c [16] ; - } canary ; - PSF_FILE file, rsrc ; char syserr [SF_SYSERR_LEN] ; From ef03e3c9ddcebc71967ccc8406f435a579339364 Mon Sep 17 00:00:00 2001 From: evpobr Date: Fri, 19 Feb 2021 18:03:08 +0500 Subject: [PATCH 026/204] Fix compiler warning --- src/file_io.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/file_io.c b/src/file_io.c index 93d604e7..334ba2a2 100644 --- a/src/file_io.c +++ b/src/file_io.c @@ -790,7 +790,7 @@ psf_open_handle (PSF_FILE * pfile) DWORD dwShareMode ; DWORD dwCreationDistribution ; HANDLE handle ; - LPCWSTR pwszPath = NULL ; + LPWSTR pwszPath = NULL ; switch (pfile->mode) { case SFM_READ : From 26b4ae781d7d44d998a4a7436a4ce864a169ab38 Mon Sep 17 00:00:00 2001 From: David Seifert Date: Sat, 20 Feb 2021 16:18:19 +0100 Subject: [PATCH 027/204] Delete temporary files for invalid opus versions * Previously, temporary files in the testsuite weren't cleaned up properly, and would fail if unique filenames (#704) were used. --- tests/lossy_comp_test.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/lossy_comp_test.c b/tests/lossy_comp_test.c index cca274cc..dac085ae 100644 --- a/tests/lossy_comp_test.c +++ b/tests/lossy_comp_test.c @@ -1470,6 +1470,7 @@ channels = 1 ; if ((filetype & SF_FORMAT_SUBMASK) == SF_FORMAT_OPUS && !check_opus_version (file)) { sf_close (file) ; + unlink (filename) ; return ; } ; @@ -1690,6 +1691,7 @@ channels = 1 ; if ((filetype & SF_FORMAT_SUBMASK) == SF_FORMAT_OPUS && !check_opus_version (file)) { sf_close (file) ; + unlink (filename) ; return ; } ; @@ -1935,6 +1937,7 @@ channels = 1 ; if ((filetype & SF_FORMAT_SUBMASK) == SF_FORMAT_OPUS && !check_opus_version (file)) { sf_close (file) ; + unlink (filename) ; return ; } ; @@ -2175,6 +2178,7 @@ channels = 1 ; if ((filetype & SF_FORMAT_SUBMASK) == SF_FORMAT_OPUS && !check_opus_version (file)) { sf_close (file) ; + unlink (filename) ; return ; } ; From a9815b3f228df00086e0a40bcc43162fc19896a1 Mon Sep 17 00:00:00 2001 From: bobsayshilol Date: Wed, 17 Feb 2021 23:21:48 +0000 Subject: [PATCH 028/204] wavlike: Fix incorrect size check The SF_CART_INFO_16K struct has an additional 4 byte field to hold the size of 'tag_text' which the file header doesn't, so don't include it as part of the check when looking for the max length. https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=26026 --- src/wavlike.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/wavlike.c b/src/wavlike.c index b59c6847..1c1ed2cf 100644 --- a/src/wavlike.c +++ b/src/wavlike.c @@ -830,7 +830,11 @@ wavlike_read_cart_chunk (SF_PRIVATE *psf, uint32_t chunksize) return 0 ; } ; - if (chunksize >= sizeof (SF_CART_INFO_16K)) + /* + ** SF_CART_INFO_16K has an extra field 'tag_text_size' that isn't part + ** of the chunk, so don't include it in the size check. + */ + if (chunksize >= sizeof (SF_CART_INFO_16K) - 4) { psf_log_printf (psf, "cart : %u too big to be handled\n", chunksize) ; psf_binheader_readf (psf, "j", chunksize) ; return 0 ; From deb669ee8be55a94565f6f8a6b60890c2e7c6f32 Mon Sep 17 00:00:00 2001 From: bobsayshilol Date: Thu, 18 Feb 2021 21:52:09 +0000 Subject: [PATCH 029/204] ms_adpcm: Fix and extend size checks 'blockalign' is the size of a block, and each block contains 7 samples per channel as part of the preamble, so check against 'samplesperblock' rather than 'blockalign'. Also add an additional check that the block is big enough to hold the samples it claims to hold. https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=26803 --- src/ms_adpcm.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/ms_adpcm.c b/src/ms_adpcm.c index 5e8f1a31..a21cb994 100644 --- a/src/ms_adpcm.c +++ b/src/ms_adpcm.c @@ -128,8 +128,14 @@ wavlike_msadpcm_init (SF_PRIVATE *psf, int blockalign, int samplesperblock) if (psf->file.mode == SFM_WRITE) samplesperblock = 2 + 2 * (blockalign - 7 * psf->sf.channels) / psf->sf.channels ; - if (blockalign < 7 * psf->sf.channels) - { psf_log_printf (psf, "*** Error blockalign (%d) should be > %d.\n", blockalign, 7 * psf->sf.channels) ; + /* There's 7 samples per channel in the preamble of each block */ + if (samplesperblock < 7 * psf->sf.channels) + { psf_log_printf (psf, "*** Error samplesperblock (%d) should be >= %d.\n", samplesperblock, 7 * psf->sf.channels) ; + return SFE_INTERNAL ; + } ; + + if (2 * blockalign < samplesperblock * psf->sf.channels) + { psf_log_printf (psf, "*** Error blockalign (%d) should be >= %d.\n", blockalign, samplesperblock * psf->sf.channels / 2) ; return SFE_INTERNAL ; } ; From aa3346cc6ed670835f9b81d0bccac992669a734c Mon Sep 17 00:00:00 2001 From: bobsayshilol Date: Sun, 21 Feb 2021 10:11:34 +0000 Subject: [PATCH 030/204] tests: Make temporary filenames unique per test (#704) When two tests share a temporary filename they can cause ctest to fail when run in parallel mode since both would attempt to operate on the same file if scheduled to run at the same time. To fix this, filenames in the smaller tests have been individually renamed, and for the bigger tests a prefix has been added to make them unique. The complete list of conflicting filenames (which were then manually inspected) was generated by running the following command in 'tests': for file in $(grep -hoER \"\\w+\\.\\w+\" | sort | uniq -d) do echo $file grep -Rn $file echo done | less --- tests/alaw_test.c | 2 +- tests/command_test.c | 4 ++-- tests/compression_size_test.c | 6 ++++++ tests/floating_point_test.tpl | 5 +++++ tests/header_test.tpl | 2 +- tests/headerless_test.c | 8 ++++---- tests/lossy_comp_test.c | 13 +++++++++++++ tests/string_test.c | 10 ++++++++++ tests/ulaw_test.c | 2 +- tests/utils.tpl | 11 +++++++++++ tests/write_read_test.tpl | 4 ++++ 11 files changed, 58 insertions(+), 9 deletions(-) diff --git a/tests/alaw_test.c b/tests/alaw_test.c index b928083c..01b3dc4f 100644 --- a/tests/alaw_test.c +++ b/tests/alaw_test.c @@ -49,7 +49,7 @@ main (void) print_test_name ("alaw_test", "encoder") ; - filename = "test.raw" ; + filename = "alaw_test.raw" ; sf_info_setup (&sfinfo, SF_FORMAT_RAW | SF_FORMAT_ALAW, 44100, 1) ; diff --git a/tests/command_test.c b/tests/command_test.c index 625d8e56..8ca23b27 100644 --- a/tests/command_test.c +++ b/tests/command_test.c @@ -115,8 +115,8 @@ main (int argc, char *argv []) { /* Preliminary float/double normalisation tests. More testing ** is done in the program 'floating_point_test'. */ - float_norm_test ("float.wav") ; - double_norm_test ("double.wav") ; + float_norm_test ("cmd_float.wav") ; + double_norm_test ("cmd_double.wav") ; test_count ++ ; } ; diff --git a/tests/compression_size_test.c b/tests/compression_size_test.c index c8f43c81..3e3059db 100644 --- a/tests/compression_size_test.c +++ b/tests/compression_size_test.c @@ -37,6 +37,8 @@ #define SAMPLE_RATE 16000 #define DATA_LENGTH (SAMPLE_RATE) +static const char CMP_TEST_PREFIX[] = "cmp" ; + static float data_out [DATA_LENGTH] ; static inline float @@ -53,6 +55,8 @@ vorbis_test (void) float max_abs = 0.0 ; unsigned k ; + get_unique_test_name (&filename, CMP_TEST_PREFIX) ; + print_test_name ("vorbis_test", filename) ; /* Generate float data. */ @@ -123,6 +127,8 @@ compression_size_test (int format, const char * filename) double quality ; int k ; + get_unique_test_name (&filename, CMP_TEST_PREFIX) ; + snprintf (q3_fname, sizeof (q3_fname), "q3_%s", filename) ; snprintf (q6_fname, sizeof (q6_fname), "q6_%s", filename) ; diff --git a/tests/floating_point_test.tpl b/tests/floating_point_test.tpl index bfa7a75f..4eb0ce6e 100644 --- a/tests/floating_point_test.tpl +++ b/tests/floating_point_test.tpl @@ -38,6 +38,8 @@ #define SAMPLE_RATE 16000 +static const char FPT_TEST_PREFIX[] = "fpt" ; + static void float_scaled_test (const char *filename, int allow_exit, int replace_float, int filetype, double target_snr) ; static void double_scaled_test (const char *filename, int allow_exit, int replace_float, int filetype, double target_snr) ; @@ -206,6 +208,7 @@ float_scaled_test (const char *filename, int allow_exit, int replace_float, int double snr ; int byterate ; + get_unique_test_name (&filename, FPT_TEST_PREFIX) ; print_test_name ("float_scaled_test", filename) ; gen_windowed_sine_float (float_data, DFT_DATA_LENGTH, 0.9999) ; @@ -259,6 +262,7 @@ double_scaled_test (const char *filename, int allow_exit, int replace_float, int double snr ; int byterate ; + get_unique_test_name (&filename, FPT_TEST_PREFIX) ; print_test_name ("double_scaled_test", filename) ; gen_windowed_sine_double (double_data, DFT_DATA_LENGTH, 0.9999) ; @@ -317,6 +321,7 @@ static void int max ; unsigned k ; + get_unique_test_name (&filename, FPT_TEST_PREFIX) ; print_test_name ("[+ (get "float_name") +]_[+ (get "int_name") +]_[+ (get "end_name") +]_test", filename) ; gen_windowed_sine_[+ (get "float_name") +] ([+ (get "float_name") +]_data, ARRAY_LEN ([+ (get "float_name") +]_data), 0.9999) ; diff --git a/tests/header_test.tpl b/tests/header_test.tpl index f4dd5b5b..b02fbef8 100644 --- a/tests/header_test.tpl +++ b/tests/header_test.tpl @@ -106,7 +106,7 @@ main (int argc, char *argv []) update_seek_int_test ("header_int.aiff", SF_FORMAT_AIFF) ; update_seek_float_test ("header_float.aiff", SF_FORMAT_AIFF) ; update_seek_double_test ("header_double.aiff", SF_FORMAT_AIFF) ; - header_shrink_test ("header_shrink.wav", SF_FORMAT_AIFF) ; + header_shrink_test ("header_shrink.aiff", SF_FORMAT_AIFF) ; extra_header_test ("extra.aiff", SF_FORMAT_AIFF) ; test_count++ ; } ; diff --git a/tests/headerless_test.c b/tests/headerless_test.c index 059fa683..aeaad1ec 100644 --- a/tests/headerless_test.c +++ b/tests/headerless_test.c @@ -44,11 +44,11 @@ main (void) { old_test () ; - headerless_test ("raw.vox", SF_FORMAT_VOX_ADPCM, SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM) ; - headerless_test ("raw.gsm", SF_FORMAT_GSM610, SF_FORMAT_RAW | SF_FORMAT_GSM610) ; + headerless_test ("headerless.vox", SF_FORMAT_VOX_ADPCM, SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM) ; + headerless_test ("headerless.gsm", SF_FORMAT_GSM610, SF_FORMAT_RAW | SF_FORMAT_GSM610) ; - headerless_test ("raw.snd", SF_FORMAT_ULAW, SF_FORMAT_RAW | SF_FORMAT_ULAW) ; - headerless_test ("raw.au" , SF_FORMAT_ULAW, SF_FORMAT_RAW | SF_FORMAT_ULAW) ; + headerless_test ("headerless.snd", SF_FORMAT_ULAW, SF_FORMAT_RAW | SF_FORMAT_ULAW) ; + headerless_test ("headerless.au" , SF_FORMAT_ULAW, SF_FORMAT_RAW | SF_FORMAT_ULAW) ; return 0 ; } /* main */ diff --git a/tests/lossy_comp_test.c b/tests/lossy_comp_test.c index dac085ae..bdd65b2e 100644 --- a/tests/lossy_comp_test.c +++ b/tests/lossy_comp_test.c @@ -44,6 +44,8 @@ #define LCT_MAX(x, y) ((x) > (y) ? (x) : (y)) +static const char LCT_TEST_PREFIX[] = "lct" ; + static void lcomp_test_short (const char *filename, int filetype, int chan, double margin) ; static void lcomp_test_int (const char *filename, int filetype, int chan, double margin) ; static void lcomp_test_float (const char *filename, int filetype, int chan, double margin) ; @@ -651,6 +653,7 @@ lcomp_test_short (const char *filename, int filetype, int channels, double margi sf_count_t datalen ; short *orig, *data ; + get_unique_test_name (&filename, LCT_TEST_PREFIX) ; print_test_name ("lcomp_test_short", filename) ; datalen = BUFFER_SIZE / channels ; @@ -838,6 +841,7 @@ lcomp_test_int (const char *filename, int filetype, int channels, double margin) double scale, max_val ; int *orig, *data ; + get_unique_test_name (&filename, LCT_TEST_PREFIX) ; print_test_name ("lcomp_test_int", filename) ; datalen = BUFFER_SIZE / channels ; @@ -1035,6 +1039,7 @@ lcomp_test_float (const char *filename, int filetype, int channels, double margi float *orig, *data ; double half_max_abs ; + get_unique_test_name (&filename, LCT_TEST_PREFIX) ; print_test_name ("lcomp_test_float", filename) ; datalen = BUFFER_SIZE / channels ; @@ -1229,6 +1234,7 @@ lcomp_test_double (const char *filename, int filetype, int channels, double marg double *orig, *data ; double half_max_abs ; + get_unique_test_name (&filename, LCT_TEST_PREFIX) ; print_test_name ("lcomp_test_double", filename) ; datalen = BUFFER_SIZE / channels ; @@ -1424,6 +1430,8 @@ sdlcomp_test_short (const char *filename, int filetype, int channels, double mar short *orig, *data, *smooth ; channels = 1 ; + + get_unique_test_name (&filename, LCT_TEST_PREFIX) ; print_test_name ("sdlcomp_test_short", filename) ; datalen = BUFFER_SIZE ; @@ -1644,6 +1652,7 @@ sdlcomp_test_int (const char *filename, int filetype, int channels, double margi channels = 1 ; + get_unique_test_name (&filename, LCT_TEST_PREFIX) ; print_test_name ("sdlcomp_test_int", filename) ; datalen = BUFFER_SIZE ; @@ -1867,6 +1876,7 @@ sdlcomp_test_float (const char *filename, int filetype, int channels, double mar channels = 1 ; + get_unique_test_name (&filename, LCT_TEST_PREFIX) ; print_test_name ("sdlcomp_test_float", filename) ; switch ((filetype & SF_FORMAT_SUBMASK)) @@ -2111,6 +2121,8 @@ sdlcomp_test_double (const char *filename, int filetype, int channels, double ma double *orig, *data, *smooth, half_max_abs, scale ; channels = 1 ; + + get_unique_test_name (&filename, LCT_TEST_PREFIX) ; print_test_name ("sdlcomp_test_double", filename) ; switch ((filetype & SF_FORMAT_SUBMASK)) @@ -2352,6 +2364,7 @@ read_raw_test (const char *filename, int filetype, int channels) short *orig, *data ; int k ; + get_unique_test_name (&filename, LCT_TEST_PREFIX) ; print_test_name ("read_raw_test", filename) ; datalen = ARRAY_LEN (orig_buffer.s) / 2 ; diff --git a/tests/string_test.c b/tests/string_test.c index 70a0dfbf..0e7a01e8 100644 --- a/tests/string_test.c +++ b/tests/string_test.c @@ -37,6 +37,8 @@ #define BUFFER_LEN (1 << 10) #define LOG_BUFFER_SIZE 1024 +static const char STR_TEST_PREFIX[] = "str" ; + static void string_start_test (const char *filename, int typemajor) ; static void string_start_end_test (const char *filename, int typemajor) ; static void string_multi_set_test (const char *filename, int typemajor) ; @@ -207,6 +209,7 @@ string_start_end_test (const char *filename, int typemajor) SF_INFO sfinfo ; int errors = 0 ; + get_unique_test_name (&filename, STR_TEST_PREFIX) ; print_test_name ("string_start_end_test", filename) ; memset (&sfinfo, 0, sizeof (sfinfo)) ; @@ -356,6 +359,7 @@ string_start_test (const char *filename, int formattype) int errors = 0 ; int typemajor = SF_FORMAT_TYPEMASK & formattype ; + get_unique_test_name (&filename, STR_TEST_PREFIX) ; print_test_name ("string_start_test", filename) ; memset (&sfinfo, 0, sizeof (sfinfo)) ; @@ -497,6 +501,7 @@ string_multi_set_test (const char *filename, int typemajor) SF_INFO sfinfo ; int count ; + get_unique_test_name (&filename, STR_TEST_PREFIX) ; print_test_name (__func__, filename) ; memset (&sfinfo, 0, sizeof (sfinfo)) ; @@ -566,6 +571,7 @@ string_rdwr_test (const char *filename, int typemajor) sf_count_t frames ; const char * str ; + get_unique_test_name (&filename, STR_TEST_PREFIX) ; print_test_name (__func__, filename) ; create_short_sndfile (filename, typemajor | SF_FORMAT_PCM_16, 2) ; @@ -618,6 +624,7 @@ string_short_rdwr_test (const char *filename, int typemajor) sf_count_t frames = BUFFER_LEN ; const char * str ; + get_unique_test_name (&filename, STR_TEST_PREFIX) ; print_test_name (__func__, filename) ; memset (&sfinfo, 0, sizeof (sfinfo)) ; @@ -687,6 +694,7 @@ static void software_string_test (const char *filename) { size_t k ; + get_unique_test_name (&filename, STR_TEST_PREFIX) ; print_test_name (__func__, filename) ; for (k = 0 ; k < 50 ; k++) @@ -727,6 +735,7 @@ string_rdwr_grow_test (const char *filename, int typemajor) sf_count_t frames ; const char * str ; + get_unique_test_name (&filename, STR_TEST_PREFIX) ; print_test_name (__func__, filename) ; /* Create a file that contains some strings. Then open the file in RDWR mode and @@ -785,6 +794,7 @@ string_header_update (const char *filename, int typemajor) const char * str ; const int GROW_BUFFER_AMOUNT = 4 ; /* this should be less than half the size of the string header */ + get_unique_test_name (&filename, STR_TEST_PREFIX) ; print_test_name (__func__, filename) ; /* Create a short file. */ diff --git a/tests/ulaw_test.c b/tests/ulaw_test.c index 9e0eba9b..cf2084bd 100644 --- a/tests/ulaw_test.c +++ b/tests/ulaw_test.c @@ -49,7 +49,7 @@ main (void) print_test_name ("ulaw_test", "encoder") ; - filename = "test.raw" ; + filename = "ulaw_test.raw" ; sf_info_setup (&sfinfo, SF_FORMAT_RAW | SF_FORMAT_ULAW, 44100, 1) ; diff --git a/tests/utils.tpl b/tests/utils.tpl index 7be35fad..c68e3a26 100644 --- a/tests/utils.tpl +++ b/tests/utils.tpl @@ -104,6 +104,8 @@ void count_open_files (void) ; void increment_open_file_count (void) ; void check_open_file_count_or_die (int lineno) ; +void get_unique_test_name (const char ** filename, const char * test) ; + #ifdef SNDFILE_H static inline void @@ -851,6 +853,15 @@ check_open_file_count_or_die (int lineno) #endif } /* check_open_file_count_or_die */ +void +get_unique_test_name (const char ** filename, const char * test) +{ static char buffer [1024] ; + + snprintf (buffer, sizeof (buffer), "%s_%s", test, *filename) ; + + *filename = buffer ; +} /* get_unique_test_name */ + void write_mono_file (const char * filename, int format, int srate, float * output, int len) { SNDFILE * file ; diff --git a/tests/write_read_test.tpl b/tests/write_read_test.tpl index af87e12a..ed152f89 100644 --- a/tests/write_read_test.tpl +++ b/tests/write_read_test.tpl @@ -42,6 +42,8 @@ #define SILLY_WRITE_COUNT (234) +static const char WRT_TEST_PREFIX[] = "wrt" ; + [+ FOR data_type +]static void pcm_test_[+ (get "type_name") +] (const char *str, int format, int long_file_ok) ; [+ ENDFOR data_type @@ -446,6 +448,7 @@ pcm_test_[+ (get "type_name") +] (const char *filename, int format, int long_fil /* Sd2 files cannot be opened from an existing file descriptor. */ allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ; + get_unique_test_name (&filename, WRT_TEST_PREFIX) ; print_test_name ("pcm_test_[+ (get "type_name") +]", filename) ; sfinfo.samplerate = 44100 ; @@ -1008,6 +1011,7 @@ empty_file_test (const char *filename, int format) /* Sd2 files cannot be opened from an existing file descriptor. */ allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ; + get_unique_test_name (&filename, WRT_TEST_PREFIX) ; print_test_name ("empty_file_test", filename) ; unlink (filename) ; From a201cf27e9233079f73894ebb037902306a1afef Mon Sep 17 00:00:00 2001 From: evpobr Date: Mon, 22 Feb 2021 23:01:05 +0500 Subject: [PATCH 031/204] dither: Fix invalid type casts --- src/dither.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dither.c b/src/dither.c index a36705e5..6f23c674 100644 --- a/src/dither.c +++ b/src/dither.c @@ -327,7 +327,7 @@ dither_write_float (SF_PRIVATE *psf, const float *ptr, sf_count_t len) bufferlen = sizeof (pdither->buffer) / (sizeof (float)) ; while (len > 0) - { writecount = (len >= bufferlen) ? bufferlen : (float) len ; + { writecount = (len >= bufferlen) ? bufferlen : (int) len ; writecount /= psf->sf.channels ; writecount *= psf->sf.channels ; @@ -373,7 +373,7 @@ dither_write_double (SF_PRIVATE *psf, const double *ptr, sf_count_t len) bufferlen = sizeof (pdither->buffer) / sizeof (double) ; while (len > 0) - { writecount = (len >= bufferlen) ? bufferlen : (double) len ; + { writecount = (len >= bufferlen) ? bufferlen : (int) len ; writecount /= psf->sf.channels ; writecount *= psf->sf.channels ; From 2e7b55944a1ac328e9eea2863c94c8fa62ceedbd Mon Sep 17 00:00:00 2001 From: evpobr Date: Thu, 25 Feb 2021 11:07:53 +0500 Subject: [PATCH 032/204] Fix some potential comparisons of a constants --- src/common.c | 32 +++++++++---- src/pcm.c | 128 ++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 120 insertions(+), 40 deletions(-) diff --git a/src/common.c b/src/common.c index e0d25b83..e2abccd6 100644 --- a/src/common.c +++ b/src/common.c @@ -1624,14 +1624,18 @@ psf_f2s_clip_array (const float *src, short *dest, int count, int normalize) while (--count >= 0) { scaled_value = src [count] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFF)) +#if CPU_CLIPS_POSITIVE == 0 + if (scaled_value >= (1.0 * 0x7FFF)) { dest [count] = 0x7FFF ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x1000)) +#endif +#if CPU_CLIPS_NEGATIVE == 0 + if (scaled_value <= (-8.0 * 0x1000)) { dest [count] = -0x7FFF - 1 ; continue ; } ; +#endif dest [count] = psf_lrintf (scaled_value) ; } ; @@ -1658,14 +1662,18 @@ psf_d2s_clip_array (const double *src, short *dest, int count, int normalize) while (--count >= 0) { scaled_value = src [count] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFF)) +#if CPU_CLIPS_POSITIVE == 0 + if (scaled_value >= (1.0 * 0x7FFF)) { dest [count] = 0x7FFF ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x1000)) +#endif +#if CPU_CLIPS_NEGATIVE == 0 + if (scaled_value <= (-8.0 * 0x1000)) { dest [count] = -0x7FFF - 1 ; continue ; } ; +#endif dest [count] = psf_lrint (scaled_value) ; } ; @@ -1693,14 +1701,18 @@ psf_f2i_clip_array (const float *src, int *dest, int count, int normalize) while (--count >= 0) { scaled_value = src [count] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF)) +#if CPU_CLIPS_POSITIVE == 0 + if (scaled_value >= (1.0 * 0x7FFFFFFF)) { dest [count] = 0x7FFFFFFF ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000)) +#endif +#if CPU_CLIPS_NEGATIVE == 0 + if (scaled_value <= (-8.0 * 0x10000000)) { dest [count] = 0x80000000 ; continue ; } ; +#endif dest [count] = psf_lrintf (scaled_value) ; } ; @@ -1727,14 +1739,18 @@ psf_d2i_clip_array (const double *src, int *dest, int count, int normalize) while (--count >= 0) { scaled_value = src [count] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF)) +#if CPU_CLIPS_POSITIVE == 0 + if (scaled_value >= (1.0 * 0x7FFFFFFF)) { dest [count] = 0x7FFFFFFF ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000)) +#endif +#if CPU_CLIPS_NEGATIVE == 0 + if (scaled_value <= (-8.0 * 0x10000000)) { dest [count] = 0x80000000 ; continue ; } ; +#endif dest [count] = psf_lrint (scaled_value) ; } ; diff --git a/src/pcm.c b/src/pcm.c index 3ea0d090..14990cdc 100644 --- a/src/pcm.c +++ b/src/pcm.c @@ -1778,14 +1778,18 @@ f2sc_clip_array (const float *src, signed char *dest, int count, int normalize) while (--count >= 0) { scaled_value = src [count] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF)) +#if CPU_CLIPS_POSITIVE == 0 + if (scaled_value >= (1.0 * 0x7FFFFFFF)) { dest [count] = 127 ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000)) +#endif +#if CPU_CLIPS_NEGATIVE == 0 + if (scaled_value <= (-8.0 * 0x10000000)) { dest [count] = -128 ; continue ; } ; +#endif dest [count] = psf_lrintf (scaled_value) >> 24 ; } ; @@ -1837,14 +1841,18 @@ f2uc_clip_array (const float *src, unsigned char *dest, int count, int normalize while (--count >= 0) { scaled_value = src [count] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF)) +#if CPU_CLIPS_POSITIVE == 0 + if (scaled_value >= (1.0 * 0x7FFFFFFF)) { dest [count] = 0xFF ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000)) +#endif +#if CPU_CLIPS_NEGATIVE == 0 + if (scaled_value <= (-8.0 * 0x10000000)) { dest [count] = 0 ; continue ; } ; +#endif dest [count] = (psf_lrintf (scaled_value) >> 24) + 128 ; } ; @@ -1906,16 +1914,20 @@ f2bes_clip_array (const float *src, short *dest, int count, int normalize) while (--count >= 0) { ucptr -= 2 ; scaled_value = src [count] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF)) +#if CPU_CLIPS_POSITIVE == 0 + if (scaled_value >= (1.0 * 0x7FFFFFFF)) { ucptr [1] = 0xFF ; ucptr [0] = 0x7F ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000)) +#endif +#if CPU_CLIPS_NEGATIVE == 0 + if (scaled_value <= (-8.0 * 0x10000000)) { ucptr [1] = 0x00 ; ucptr [0] = 0x80 ; continue ; } ; +#endif value = psf_lrintf (scaled_value) ; ucptr [1] = value >> 16 ; @@ -1979,16 +1991,20 @@ f2les_clip_array (const float *src, short *dest, int count, int normalize) while (--count >= 0) { ucptr -= 2 ; scaled_value = src [count] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF)) +#if CPU_CLIPS_POSITIVE == 0 + if (scaled_value >= (1.0 * 0x7FFFFFFF)) { ucptr [0] = 0xFF ; ucptr [1] = 0x7F ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000)) +#endif +#if CPU_CLIPS_NEGATIVE == 0 + if (scaled_value <= (-8.0 * 0x10000000)) { ucptr [0] = 0x00 ; ucptr [1] = 0x80 ; continue ; } ; +#endif value = psf_lrintf (scaled_value) ; ucptr [0] = value >> 16 ; @@ -2047,18 +2063,22 @@ f2let_clip_array (const float *src, tribyte *dest, int count, int normalize) while (--count >= 0) { scaled_value = src [count] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF)) +#if CPU_CLIPS_POSITIVE == 0 + if (scaled_value >= (1.0 * 0x7FFFFFFF)) { dest [count].bytes [0] = 0xFF ; dest [count].bytes [1] = 0xFF ; dest [count].bytes [2] = 0x7F ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000)) +#endif +#if CPU_CLIPS_NEGATIVE == 0 + if (scaled_value <= (-8.0 * 0x10000000)) { dest [count].bytes [0] = 0x00 ; dest [count].bytes [1] = 0x00 ; dest [count].bytes [2] = 0x80 ; continue ; } ; +#endif value = psf_lrintf (scaled_value) ; dest [count].bytes [0] = value >> 8 ; @@ -2118,18 +2138,22 @@ f2bet_clip_array (const float *src, tribyte *dest, int count, int normalize) while (--count >= 0) { scaled_value = src [count] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF)) +#if CPU_CLIPS_POSITIVE == 0 + if (scaled_value >= (1.0 * 0x7FFFFFFF)) { dest [count].bytes [0] = 0x7F ; dest [count].bytes [1] = 0xFF ; dest [count].bytes [2] = 0xFF ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000)) +#endif +#if CPU_CLIPS_NEGATIVE == 0 + if (scaled_value <= (-8.0 * 0x10000000)) { dest [count].bytes [0] = 0x80 ; dest [count].bytes [1] = 0x00 ; dest [count].bytes [2] = 0x00 ; continue ; } ; +#endif value = psf_lrint (scaled_value) ; dest [count].bytes [0] = value >> 24 ; @@ -2195,20 +2219,24 @@ f2bei_clip_array (const float *src, int *dest, int count, int normalize) while (--count >= 0) { ucptr -= 4 ; scaled_value = src [count] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= 1.0 * 0x7FFFFFFF) +#if CPU_CLIPS_POSITIVE == 0 + if (scaled_value >= 1.0 * 0x7FFFFFFF) { ucptr [0] = 0x7F ; ucptr [1] = 0xFF ; ucptr [2] = 0xFF ; ucptr [3] = 0xFF ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000)) +#endif +#if CPU_CLIPS_NEGATIVE == 0 + if (scaled_value <= (-8.0 * 0x10000000)) { ucptr [0] = 0x80 ; ucptr [1] = 0x00 ; ucptr [2] = 0x00 ; ucptr [3] = 0x00 ; continue ; } ; +#endif value = psf_lrintf (scaled_value) ; ucptr [0] = value >> 24 ; @@ -2276,20 +2304,24 @@ f2lei_clip_array (const float *src, int *dest, int count, int normalize) while (--count >= 0) { ucptr -= 4 ; scaled_value = src [count] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF)) +#if CPU_CLIPS_POSITIVE == 0 + if (scaled_value >= (1.0 * 0x7FFFFFFF)) { ucptr [0] = 0xFF ; ucptr [1] = 0xFF ; ucptr [2] = 0xFF ; ucptr [3] = 0x7F ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000)) +#endif +#if CPU_CLIPS_NEGATIVE == 0 + if (scaled_value <= (-8.0 * 0x10000000)) { ucptr [0] = 0x00 ; ucptr [1] = 0x00 ; ucptr [2] = 0x00 ; ucptr [3] = 0x80 ; continue ; } ; +#endif value = psf_lrintf (scaled_value) ; ucptr [0] = value ; @@ -2345,14 +2377,18 @@ d2sc_clip_array (const double *src, signed char *dest, int count, int normalize) while (--count >= 0) { scaled_value = src [count] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF)) +#if CPU_CLIPS_POSITIVE == 0 + if (scaled_value >= (1.0 * 0x7FFFFFFF)) { dest [count] = 127 ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000)) +#endif +#if CPU_CLIPS_NEGATIVE == 0 + if (scaled_value <= (-8.0 * 0x10000000)) { dest [count] = -128 ; continue ; } ; +#endif dest [count] = psf_lrintf (scaled_value) >> 24 ; } ; @@ -2404,14 +2440,18 @@ d2uc_clip_array (const double *src, unsigned char *dest, int count, int normaliz while (--count >= 0) { scaled_value = src [count] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF)) +#if CPU_CLIPS_POSITIVE == 0 + if (scaled_value >= (1.0 * 0x7FFFFFFF)) { dest [count] = 255 ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000)) +#endif +#if CPU_CLIPS_NEGATIVE == 0 + if (scaled_value <= (-8.0 * 0x10000000)) { dest [count] = 0 ; continue ; } ; +#endif dest [count] = (psf_lrint (src [count] * normfact) >> 24) + 128 ; } ; @@ -2473,16 +2513,20 @@ d2bes_clip_array (const double *src, short *dest, int count, int normalize) while (--count >= 0) { ucptr -= 2 ; scaled_value = src [count] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF)) +#if CPU_CLIPS_POSITIVE == 0 + if (scaled_value >= (1.0 * 0x7FFFFFFF)) { ucptr [1] = 0xFF ; ucptr [0] = 0x7F ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000)) +#endif +#if CPU_CLIPS_NEGATIVE == 0 + if (scaled_value <= (-8.0 * 0x10000000)) { ucptr [1] = 0x00 ; ucptr [0] = 0x80 ; continue ; } ; +#endif value = psf_lrint (scaled_value) ; ucptr [1] = value >> 16 ; @@ -2546,16 +2590,20 @@ d2les_clip_array (const double *src, short *dest, int count, int normalize) while (--count >= 0) { ucptr -= 2 ; scaled_value = src [count] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF)) +#if CPU_CLIPS_POSITIVE == 0 + if (scaled_value >= (1.0 * 0x7FFFFFFF)) { ucptr [0] = 0xFF ; ucptr [1] = 0x7F ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000)) +#endif +#if CPU_CLIPS_NEGATIVE == 0 + if (scaled_value <= (-8.0 * 0x10000000)) { ucptr [0] = 0x00 ; ucptr [1] = 0x80 ; continue ; } ; +#endif value = psf_lrint (scaled_value) ; ucptr [0] = value >> 16 ; @@ -2614,18 +2662,22 @@ d2let_clip_array (const double *src, tribyte *dest, int count, int normalize) while (--count >= 0) { scaled_value = src [count] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF)) +#if CPU_CLIPS_POSITIVE == 0 + if (scaled_value >= (1.0 * 0x7FFFFFFF)) { dest [count].bytes [0] = 0xFF ; dest [count].bytes [1] = 0xFF ; dest [count].bytes [2] = 0x7F ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000)) +#endif +#if CPU_CLIPS_NEGATIVE == 0 + if (scaled_value <= (-8.0 * 0x10000000)) { dest [count].bytes [0] = 0x00 ; dest [count].bytes [1] = 0x00 ; dest [count].bytes [2] = 0x80 ; continue ; } ; +#endif value = psf_lrint (scaled_value) ; dest [count].bytes [0] = value >> 8 ; @@ -2685,18 +2737,22 @@ d2bet_clip_array (const double *src, tribyte *dest, int count, int normalize) while (--count >= 0) { scaled_value = src [count] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF)) +#if CPU_CLIPS_POSITIVE == 0 + if (scaled_value >= (1.0 * 0x7FFFFFFF)) { dest [count].bytes [2] = 0xFF ; dest [count].bytes [1] = 0xFF ; dest [count].bytes [0] = 0x7F ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000)) +#endif +#if CPU_CLIPS_NEGATIVE == 0 + if (scaled_value <= (-8.0 * 0x10000000)) { dest [count].bytes [2] = 0x00 ; dest [count].bytes [1] = 0x00 ; dest [count].bytes [0] = 0x80 ; continue ; } ; +#endif value = psf_lrint (scaled_value) ; dest [count].bytes [2] = value >> 8 ; @@ -2763,20 +2819,24 @@ d2bei_clip_array (const double *src, int *dest, int count, int normalize) while (--count >= 0) { ucptr -= 4 ; scaled_value = src [count] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF)) +#if CPU_CLIPS_POSITIVE == 0 + if (scaled_value >= (1.0 * 0x7FFFFFFF)) { ucptr [3] = 0xFF ; ucptr [2] = 0xFF ; ucptr [1] = 0xFF ; ucptr [0] = 0x7F ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000)) +#endif +#if CPU_CLIPS_NEGATIVE == 0 + if (scaled_value <= (-8.0 * 0x10000000)) { ucptr [3] = 0x00 ; ucptr [2] = 0x00 ; ucptr [1] = 0x00 ; ucptr [0] = 0x80 ; continue ; } ; +#endif value = psf_lrint (scaled_value) ; ucptr [0] = value >> 24 ; @@ -2844,20 +2904,24 @@ d2lei_clip_array (const double *src, int *dest, int count, int normalize) while (--count >= 0) { ucptr -= 4 ; scaled_value = src [count] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF)) +#if CPU_CLIPS_POSITIVE == 0 + if (scaled_value >= (1.0 * 0x7FFFFFFF)) { ucptr [0] = 0xFF ; ucptr [1] = 0xFF ; ucptr [2] = 0xFF ; ucptr [3] = 0x7F ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000)) +#endif +#if CPU_CLIPS_NEGATIVE == 0 + if (scaled_value <= (-8.0 * 0x10000000)) { ucptr [0] = 0x00 ; ucptr [1] = 0x00 ; ucptr [2] = 0x00 ; ucptr [3] = 0x80 ; continue ; } ; +#endif value = psf_lrint (scaled_value) ; ucptr [0] = value ; From a53048792a864cbe6922897f60b63212d4c42e27 Mon Sep 17 00:00:00 2001 From: evpobr Date: Thu, 25 Feb 2021 12:36:35 +0500 Subject: [PATCH 033/204] alac: Use proper format specifiers --- src/alac.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/alac.c b/src/alac.c index 6a354a9c..1cfa4782 100644 --- a/src/alac.c +++ b/src/alac.c @@ -850,7 +850,7 @@ alac_pakt_read_decode (SF_PRIVATE * psf, uint32_t UNUSED (pakt_offset)) count ++ ; if (count > 5 || bcount + count > pakt_size) - { printf ("%s %d : Ooops! count %d bcount %d\n", __func__, __LINE__, count, bcount) ; + { printf ("%s %d : Ooops! count %" PRIi32 " bcount %" PRIu32 "\n", __func__, __LINE__, count, bcount) ; value = 0 ; break ; } ; From 7e6cc7cb19f084178177d847382a34fbad56f2b1 Mon Sep 17 00:00:00 2001 From: evpobr Date: Thu, 25 Feb 2021 12:38:12 +0500 Subject: [PATCH 034/204] ima_adpcm: Fix unitialized value --- src/ima_adpcm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ima_adpcm.c b/src/ima_adpcm.c index 28c20ca3..d90d9ac0 100644 --- a/src/ima_adpcm.c +++ b/src/ima_adpcm.c @@ -391,7 +391,7 @@ aiff_ima_encode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima) static int wavlike_ima_decode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima) { int chan, k, predictor, blockindx, indx, indxstart, diff ; - short step, bytecode, stepindx [2] ; + short step, bytecode, stepindx [2] = { 0 }; pima->blockcount ++ ; pima->samplecount = 0 ; From f44d0988e4702f8079fc4fb2650e7e2de7fec259 Mon Sep 17 00:00:00 2001 From: evpobr Date: Thu, 25 Feb 2021 12:41:02 +0500 Subject: [PATCH 035/204] alac: Fix NULL pointer dereferencing --- src/alac.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/alac.c b/src/alac.c index 1cfa4782..2eddd512 100644 --- a/src/alac.c +++ b/src/alac.c @@ -824,6 +824,8 @@ alac_pakt_read_decode (SF_PRIVATE * psf, uint32_t UNUSED (pakt_offset)) pakt_size = chunk_info.datalen ; chunk_info.data = pakt_data = malloc (pakt_size + 5) ; + if (!chunk_info.data) + return NULL ; if ((bcount = psf->get_chunk_data (psf, chunk_iterator, &chunk_info)) != SF_ERR_NO_ERROR) { while (chunk_iterator) From c29a002749a2283f2c7ed23391f6ab225a143961 Mon Sep 17 00:00:00 2001 From: evpobr Date: Thu, 25 Feb 2021 12:47:27 +0500 Subject: [PATCH 036/204] sndfile-info: Use proper format specifiers --- programs/sndfile-info.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/programs/sndfile-info.c b/programs/sndfile-info.c index e0e48821..b45f7b95 100644 --- a/programs/sndfile-info.c +++ b/programs/sndfile-info.c @@ -313,7 +313,9 @@ instrument_dump (const char *filename) printf (" Loop points : %d\n", inst.loop_count) ; for (k = 0 ; k < inst.loop_count ; k++) - printf (" %-2d Mode : %s Start : %6d End : %6d Count : %6d\n", k, str_of_type (inst.loops [k].mode), inst.loops [k].start, inst.loops [k].end, inst.loops [k].count) ; + printf (" %-2d Mode : %s Start : %6" PRIu32 " End : %6" PRIu32 + " Count : %6" PRIu32 "\n", k, str_of_type (inst.loops [k].mode), + inst.loops [k].start, inst.loops [k].end, inst.loops [k].count) ; putchar ('\n') ; return 0 ; From cab39856afd0ec1fc8fad02150b936840bd82ab4 Mon Sep 17 00:00:00 2001 From: evpobr Date: Thu, 25 Feb 2021 12:51:30 +0500 Subject: [PATCH 037/204] broadcast: Use proper format specifiers --- src/broadcast.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/broadcast.c b/src/broadcast.c index 16575bb5..100cbbd5 100644 --- a/src/broadcast.c +++ b/src/broadcast.c @@ -146,7 +146,7 @@ gen_coding_history (char * added_history, int added_history_max, const SF_INFO * break ; default : - snprintf (chnstr, sizeof (chnstr), "%uchn", psfinfo->channels) ; + snprintf (chnstr, sizeof (chnstr), "%dchn", psfinfo->channels) ; break ; } ; @@ -180,7 +180,7 @@ gen_coding_history (char * added_history, int added_history_max, const SF_INFO * } ; count = snprintf (added_history, added_history_max, - "A=PCM,F=%u,W=%d,M=%s,T=%s-%s\r\n", + "A=PCM,F=%d,W=%d,M=%s,T=%s-%s\r\n", psfinfo->samplerate, width, chnstr, PACKAGE_NAME, PACKAGE_VERSION) ; if (count >= added_history_max) From 3078185b6a95b9a4c5b4034770f66afdc82c3f67 Mon Sep 17 00:00:00 2001 From: evpobr Date: Thu, 25 Feb 2021 12:58:11 +0500 Subject: [PATCH 038/204] common: Fix undefined behaviour --- src/common.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/common.h b/src/common.h index 52a34a6f..61a9a1cc 100644 --- a/src/common.h +++ b/src/common.h @@ -302,8 +302,13 @@ typedef int16_t sfwchar_t ; static inline void * psf_memdup (const void *src, size_t n) -{ void * mem = calloc (1, n & 3 ? n + 4 - (n & 3) : n) ; - return memcpy (mem, src, n) ; +{ if (src == NULL) + return NULL ; + + void * mem = calloc (1, n & 3 ? n + 4 - (n & 3) : n) ; + if (mem != NULL) + memcpy (mem, src, n) ; + return mem ; } /* psf_memdup */ /* From b20886b7abfaa6bdf68f76ed93ff74afcd79bac5 Mon Sep 17 00:00:00 2001 From: evpobr Date: Thu, 25 Feb 2021 13:02:30 +0500 Subject: [PATCH 039/204] test_file_io: Use proper format specifiers --- src/test_file_io.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test_file_io.c b/src/test_file_io.c index 850cae2b..1e5b0f56 100644 --- a/src/test_file_io.c +++ b/src/test_file_io.c @@ -302,13 +302,13 @@ file_truncate_test (const char *filename) for (k = 0 ; k < SIGNED_SIZEOF (buffer) / 2 ; k++) if (buffer [k] != 0xEE) - { printf ("\n\nLine %d : buffer [%d] = %d (should be 0xEE)\n\n", __LINE__, k, buffer [k]) ; + { printf ("\n\nLine %d : buffer [%d] = %hhu (should be 0xEE)\n\n", __LINE__, k, buffer [k]) ; exit (1) ; } ; for (k = SIGNED_SIZEOF (buffer) / 2 ; k < SIGNED_SIZEOF (buffer) ; k++) if (buffer [k] != 0) - { printf ("\n\nLine %d : buffer [%d] = %d (should be 0)\n\n", __LINE__, k, buffer [k]) ; + { printf ("\n\nLine %d : buffer [%d] = %hhu (should be 0)\n\n", __LINE__, k, buffer [k]) ; exit (1) ; } ; From 96755fa3e5c8b96de61548f9acf87f74f21fd369 Mon Sep 17 00:00:00 2001 From: evpobr Date: Thu, 25 Feb 2021 13:05:12 +0500 Subject: [PATCH 040/204] alac: Fix unitialized value --- src/aiff.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aiff.c b/src/aiff.c index d872a892..a157576a 100644 --- a/src/aiff.c +++ b/src/aiff.c @@ -234,7 +234,7 @@ static int aiff_get_chunk_data (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * itera int aiff_open (SF_PRIVATE *psf) { COMM_CHUNK comm_fmt ; - int error, subformat ; + int error = 0, subformat ; memset (&comm_fmt, 0, sizeof (comm_fmt)) ; From 8b31aea3fb3ab47702c451a96cc1bdad35322551 Mon Sep 17 00:00:00 2001 From: evpobr Date: Thu, 4 Mar 2021 20:10:40 +0500 Subject: [PATCH 041/204] sfendian: Fix possible arithmetic overflows --- src/sfendian.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/sfendian.h b/src/sfendian.h index 9367a96d..0189d7df 100644 --- a/src/sfendian.h +++ b/src/sfendian.h @@ -210,16 +210,16 @@ static inline int64_t psf_get_be64 (const uint8_t *ptr, int offset) { int64_t value ; - value = ((uint32_t) ptr [offset]) << 24 ; - value += ptr [offset + 1] << 16 ; - value += ptr [offset + 2] << 8 ; + value = (int64_t) ((uint64_t) ptr [offset] << 24) ; + value += (int64_t) ((uint64_t) ptr [offset + 1] << 16) ; + value += (int64_t) ((uint64_t) ptr [offset + 2] << 8) ; value += ptr [offset + 3] ; value = (int64_t) (((uint64_t) value) << 32) ; - value += ((uint32_t) ptr [offset + 4]) << 24 ; - value += ptr [offset + 5] << 16 ; - value += ptr [offset + 6] << 8 ; + value += (int64_t) ((uint64_t) ptr [offset + 4] << 24) ; + value += (int64_t) ((uint64_t) ptr [offset + 5] << 16) ; + value += (int64_t) ((uint64_t) ptr [offset + 6] << 8) ; value += ptr [offset + 7] ; return value ; } /* psf_get_be64 */ @@ -228,16 +228,16 @@ static inline int64_t psf_get_le64 (const uint8_t *ptr, int offset) { int64_t value ; - value = ((uint32_t) ptr [offset + 7]) << 24 ; - value += ptr [offset + 6] << 16 ; - value += ptr [offset + 5] << 8 ; + value = (int64_t) ((uint64_t) ptr [offset + 7] << 24) ; + value += (int64_t) ((uint64_t) ptr [offset + 6] << 16) ; + value += (int64_t) ((uint64_t) ptr [offset + 5] << 8) ; value += ptr [offset + 4] ; value = (int64_t) (((uint64_t) value) << 32) ; - value += ((uint32_t) ptr [offset + 3]) << 24 ; - value += ptr [offset + 2] << 16 ; - value += ptr [offset + 1] << 8 ; + value += (int64_t) ((uint64_t) ptr [offset + 3] << 24) ; + value += (int64_t) ((uint64_t) ptr [offset + 2] << 16) ; + value += (int64_t) ((uint64_t) ptr [offset + 1] << 8) ; value += ptr [offset] ; return value ; } /* psf_get_le64 */ From 490dcc642fd529ba223dfbf2d10fc4c2ab0776df Mon Sep 17 00:00:00 2001 From: evpobr Date: Thu, 4 Mar 2021 20:13:34 +0500 Subject: [PATCH 042/204] wav: Use proper format specifiers --- src/wav.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/wav.c b/src/wav.c index ba05fa52..7ee94570 100644 --- a/src/wav.c +++ b/src/wav.c @@ -1325,8 +1325,9 @@ wav_read_smpl_chunk (SF_PRIVATE *psf, uint32_t chunklen) psf_log_printf (psf, " SMPTE Format : %u\n", dword) ; bytesread += psf_binheader_readf (psf, "4", &dword) ; - snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d %02d", - (dword >> 24) & 0x7F, (dword >> 16) & 0x7F, (dword >> 8) & 0x7F, dword & 0x7F) ; + snprintf (buffer, sizeof (buffer), "%02"PRIu32 ":%02"PRIu32 ":%02"PRIu32 + " %02"PRIu32 "", (dword >> 24) & 0x7F, (dword >> 16) & 0x7F, + (dword >> 8) & 0x7F, dword & 0x7F) ; psf_log_printf (psf, " SMPTE Offset : %s\n", buffer) ; bytesread += psf_binheader_readf (psf, "4", &loop_count) ; From bea8fa5d0eb7470f9df1b7a71d5bb0f0bb079188 Mon Sep 17 00:00:00 2001 From: evpobr Date: Thu, 4 Mar 2021 20:16:24 +0500 Subject: [PATCH 043/204] common: Fix NULL pointer dereferencing --- src/common.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/common.c b/src/common.c index e2abccd6..243e60a0 100644 --- a/src/common.c +++ b/src/common.c @@ -1261,8 +1261,9 @@ typedef SF_CUES_VAR () SF_CUES_0 ; SF_CUES * psf_cues_alloc (uint32_t cue_count) { SF_CUES *pcues = calloc (1, SF_CUES_VAR_SIZE (cue_count)) ; - - pcues->cue_count = cue_count ; + if (pcues) + { pcues->cue_count = cue_count ; + } ; return pcues ; } /* psf_cues_alloc */ From bd2f8226d943d214e7c008d38f8e7ca8fe5fff30 Mon Sep 17 00:00:00 2001 From: evpobr Date: Thu, 4 Mar 2021 20:20:52 +0500 Subject: [PATCH 044/204] chunk: Fix NULL pointer dereferencings --- src/chunk.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/chunk.c b/src/chunk.c index 471c5a44..afd051ef 100644 --- a/src/chunk.c +++ b/src/chunk.c @@ -112,6 +112,9 @@ psf_store_read_chunk (READ_CHUNKS * pchk, const READ_CHUNK * rchunk) { pchk->used = 0 ; pchk->count = 20 ; pchk->chunks = calloc (pchk->count, sizeof (READ_CHUNK)) ; + if (!pchk->chunks) + { return SFE_MALLOC_FAILED ; + } ; } else if (pchk->used > pchk->count) return SFE_INTERNAL ; @@ -227,6 +230,9 @@ psf_save_write_chunk (WRITE_CHUNKS * pchk, const SF_CHUNK_INFO * chunk_info) { pchk->used = 0 ; pchk->count = 20 ; pchk->chunks = calloc (pchk->count, sizeof (WRITE_CHUNK)) ; + if (!pchk->chunks) + { return SFE_MALLOC_FAILED ; + } ; } else if (pchk->used >= pchk->count) { WRITE_CHUNK * old_ptr = pchk->chunks ; From 3b8e3ef80737038824f55f41e48aa85b38a46fb6 Mon Sep 17 00:00:00 2001 From: evpobr Date: Thu, 4 Mar 2021 20:28:58 +0500 Subject: [PATCH 045/204] sndfile-deinterleave: Fix large stack allocation --- programs/sndfile-deinterleave.c | 47 ++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/programs/sndfile-deinterleave.c b/programs/sndfile-deinterleave.c index 616761e4..e47ff574 100644 --- a/programs/sndfile-deinterleave.c +++ b/programs/sndfile-deinterleave.c @@ -65,43 +65,48 @@ static void deinterleave_double (STATE * state) ; int main (int argc, char **argv) -{ STATE state ; +{ STATE *state = NULL ; SF_INFO sfinfo ; char pathname [512], ext [32], *cptr ; - int ch, double_split ; + int ch, double_split, ret = 1 ; if (argc != 2) { if (argc != 1) puts ("\nError : need a single input file.\n") ; usage_exit () ; + goto cleanup ; } ; - memset (&state, 0, sizeof (state)) ; + state = calloc (1, sizeof (state)) ; + if (!state) + { printf ("\nError : Out of memory.\n") ; + goto cleanup ; + } ; memset (&sfinfo, 0, sizeof (sfinfo)) ; - if ((state.infile = sf_open (argv [1], SFM_READ, &sfinfo)) == NULL) + if ((state->infile = sf_open (argv [1], SFM_READ, &sfinfo)) == NULL) { printf ("\nError : Not able to open input file '%s'\n%s\n", argv [1], sf_strerror (NULL)) ; - exit (1) ; + goto cleanup ; } ; if (sfinfo.channels < 2) { printf ("\nError : Input file '%s' only has one channel.\n", argv [1]) ; - exit (1) ; + goto cleanup ; } ; if (sfinfo.channels > MAX_CHANNELS) { printf ("\nError : Input file '%s' has too many (%d) channels. Limit is %d.\n", argv [1], sfinfo.channels, MAX_CHANNELS) ; - exit (1) ; + goto cleanup ; } ; - state.channels = sfinfo.channels ; + state->channels = sfinfo.channels ; sfinfo.channels = 1 ; if (snprintf (pathname, sizeof (pathname), "%s", argv [1]) > (int) sizeof (pathname)) { printf ("\nError : Length of provided filename '%s' exceeds MAX_PATH (%d).\n", argv [1], (int) sizeof (pathname)) ; - exit (1) ; + goto cleanup ; } ; if ((cptr = strrchr (pathname, '.')) == NULL) @@ -114,7 +119,7 @@ main (int argc, char **argv) printf ("Input file : %s\n", pathname) ; puts ("Output files :") ; - for (ch = 0 ; ch < state.channels ; ch++) + for (ch = 0 ; ch < state->channels ; ch++) { char filename [520] ; size_t count ; @@ -124,9 +129,9 @@ main (int argc, char **argv) { printf ("File name truncated to %s\n", filename) ; } ; - if ((state.outfile [ch] = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL) + if ((state->outfile [ch] = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL) { printf ("Not able to open output file '%s'\n%s\n", filename, sf_strerror (NULL)) ; - exit (1) ; + goto cleanup ; } ; printf (" %s\n", filename) ; @@ -145,16 +150,22 @@ main (int argc, char **argv) } ; if (double_split) - deinterleave_double (&state) ; + deinterleave_double (state) ; else - deinterleave_int (&state) ; + deinterleave_int (state) ; - sf_close (state.infile) ; + sf_close (state->infile) ; for (ch = 0 ; ch < MAX_CHANNELS ; ch++) - if (state.outfile [ch] != NULL) - sf_close (state.outfile [ch]) ; + if (state->outfile [ch] != NULL) + sf_close (state->outfile [ch]) ; - return 0 ; + ret = 0 ; + +cleanup : + + free (state) ; + + return ret ; } /* main */ /*------------------------------------------------------------------------------ From 6d77865775ba9254af0eb0ecf79004ab0bef5885 Mon Sep 17 00:00:00 2001 From: evpobr Date: Fri, 5 Mar 2021 11:43:08 +0500 Subject: [PATCH 046/204] common: Remove unused fourcc_to_marker() function --- src/common.h | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/common.h b/src/common.h index 61a9a1cc..fa7562a7 100644 --- a/src/common.h +++ b/src/common.h @@ -953,17 +953,6 @@ int psf_find_read_chunk_iterator (const READ_CHUNKS * pchk, const SF_CHUNK_ITER int psf_find_write_chunk (WRITE_CHUNKS * pchk, const char * marker) ; -static inline int -fourcc_to_marker (const SF_CHUNK_INFO * chunk_info) -{ const unsigned char * cptr ; - - if (chunk_info->id_size != 4) - return 0 ; - - cptr = (const unsigned char *) chunk_info->id ; - return (cptr [3] << 24) + (cptr [2] << 16) + (cptr [1] << 8) + cptr [0] ; -} /* fourcc_to_marker */ - /*------------------------------------------------------------------------------------ ** Functions that work like OpenBSD's strlcpy/strlcat to replace strncpy/strncat. ** From db41920b6d9c8e78d56c7a86d3ad499ac0ef1980 Mon Sep 17 00:00:00 2001 From: evpobr Date: Fri, 5 Mar 2021 11:50:17 +0500 Subject: [PATCH 047/204] common: Move psf_strlcat() and psf_strlcpy() to source file --- src/common.c | 15 +++++++++++++++ src/common.h | 13 ++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/common.c b/src/common.c index 243e60a0..ae27cabd 100644 --- a/src/common.c +++ b/src/common.c @@ -1221,6 +1221,21 @@ psf_log_SF_INFO (SF_PRIVATE *psf) /*======================================================================================== */ +void +psf_strlcat (char *dest, size_t n, const char *src) +{ strncat (dest, src, n - strlen (dest) - 1) ; + dest [n - 1] = 0 ; +} /* psf_strlcat */ + +void +psf_strlcpy (char *dest, size_t n, const char *src) +{ strncpy (dest, src, n - 1) ; + dest [n - 1] = 0 ; +} /* psf_strlcpy */ + +/*======================================================================================== +*/ + void* psf_memset (void *s, int c, sf_count_t len) { char *ptr ; diff --git a/src/common.h b/src/common.h index fa7562a7..578fedc1 100644 --- a/src/common.h +++ b/src/common.h @@ -965,17 +965,8 @@ int psf_find_write_chunk (WRITE_CHUNKS * pchk, const char * marker) ; ** compiler errors if code is carelessly converted from one to the other. */ -static inline void -psf_strlcat (char *dest, size_t n, const char *src) -{ strncat (dest, src, n - strlen (dest) - 1) ; - dest [n - 1] = 0 ; -} /* psf_strlcat */ - -static inline void -psf_strlcpy (char *dest, size_t n, const char *src) -{ strncpy (dest, src, n - 1) ; - dest [n - 1] = 0 ; -} /* psf_strlcpy */ +void psf_strlcat (char *dest, size_t n, const char *src) ; +void psf_strlcpy (char *dest, size_t n, const char *src) ; /*------------------------------------------------------------------------------------ ** SIMD optimized math functions. From 6477f6cd83d830c1ee2e4321a0d11e89a209d697 Mon Sep 17 00:00:00 2001 From: evpobr Date: Fri, 5 Mar 2021 12:00:54 +0500 Subject: [PATCH 048/204] common: Move psf_memdup() to source file --- src/common.c | 11 +++++++++++ src/common.h | 11 +---------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/common.c b/src/common.c index ae27cabd..20ad46c5 100644 --- a/src/common.c +++ b/src/common.c @@ -1236,6 +1236,17 @@ psf_strlcpy (char *dest, size_t n, const char *src) /*======================================================================================== */ +void * +psf_memdup (const void *src, size_t n) +{ if (src == NULL) + return NULL ; + + void * mem = calloc (1, n & 3 ? n + 4 - (n & 3) : n) ; + if (mem != NULL) + memcpy (mem, src, n) ; + return mem ; +} /* psf_memdup */ + void* psf_memset (void *s, int c, sf_count_t len) { char *ptr ; diff --git a/src/common.h b/src/common.h index 578fedc1..4d94f49e 100644 --- a/src/common.h +++ b/src/common.h @@ -300,16 +300,7 @@ typedef int16_t sfwchar_t ; #endif -static inline void * -psf_memdup (const void *src, size_t n) -{ if (src == NULL) - return NULL ; - - void * mem = calloc (1, n & 3 ? n + 4 - (n & 3) : n) ; - if (mem != NULL) - memcpy (mem, src, n) ; - return mem ; -} /* psf_memdup */ +void *psf_memdup (const void *src, size_t n) ; /* ** This version of isprint specifically ignores any locale info. Its used for From 1578687eb3790792f8f2b110fc527c3863c61fe1 Mon Sep 17 00:00:00 2001 From: evpobr Date: Fri, 5 Mar 2021 12:02:01 +0500 Subject: [PATCH 049/204] common: Move psf_isprint() to source file --- src/common.c | 5 +++++ src/common.h | 5 +---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/common.c b/src/common.c index 20ad46c5..98bd65c0 100644 --- a/src/common.c +++ b/src/common.c @@ -1221,6 +1221,11 @@ psf_log_SF_INFO (SF_PRIVATE *psf) /*======================================================================================== */ +int +psf_isprint (int ch) +{ return (ch >= ' ' && ch <= '~') ; +} /* psf_isprint */ + void psf_strlcat (char *dest, size_t n, const char *src) { strncat (dest, src, n - strlen (dest) - 1) ; diff --git a/src/common.h b/src/common.h index 4d94f49e..8be6f0eb 100644 --- a/src/common.h +++ b/src/common.h @@ -306,10 +306,7 @@ void *psf_memdup (const void *src, size_t n) ; ** This version of isprint specifically ignores any locale info. Its used for ** determining which characters can be printed in things like hexdumps. */ -static inline int -psf_isprint (int ch) -{ return (ch >= ' ' && ch <= '~') ; -} /* psf_isprint */ +int psf_isprint (int ch) ; /*======================================================================================= ** SF_PRIVATE stuct - a pointer to this struct is passed back to the caller of the From 4ff3b84eff00c72d8710e234654944da731ced60 Mon Sep 17 00:00:00 2001 From: evpobr Date: Sat, 6 Mar 2021 10:08:58 +0500 Subject: [PATCH 050/204] msvc: Fix annoying conversion warnings #304 --- examples/generate.c | 2 +- examples/make_sine.c | 6 +- examples/sfprocess.c | 2 +- examples/sndfile-loopify.c | 2 +- examples/sndfile-to-text.c | 2 +- programs/common.c | 8 +- programs/sndfile-concat.c | 4 +- programs/sndfile-convert.c | 2 +- programs/sndfile-deinterleave.c | 4 +- programs/sndfile-interleave.c | 4 +- programs/sndfile-salvage.c | 2 +- regtest/database.c | 2 +- src/alac.c | 16 +-- src/alaw.c | 16 +-- src/broadcast.c | 2 +- src/caf.c | 16 +-- src/cart.c | 2 +- src/chunk.c | 4 +- src/command.c | 4 +- src/common.c | 28 ++-- src/dither.c | 8 +- src/double64.c | 30 ++-- src/dwvw.c | 14 +- src/float32.c | 32 ++--- src/g72x.c | 12 +- src/gsm610.c | 20 +-- src/ima_adpcm.c | 8 +- src/ms_adpcm.c | 32 ++--- src/nms_adpcm.c | 16 +-- src/paf.c | 16 +-- src/pcm.c | 122 ++++++++-------- src/sds.c | 26 ++-- src/ulaw.c | 16 +-- src/vox_adpcm.c | 4 +- src/w64.c | 4 +- src/xi.c | 36 ++--- tests/chunk_test.c | 16 +-- tests/command_test.c | 36 ++--- tests/compression_size_test.c | 6 +- tests/dwvw_test.c | 2 +- tests/generate.c | 20 +-- tests/headerless_test.c | 6 +- tests/long_read_write_test.c | 2 +- tests/lossy_comp_test.c | 238 ++++++++++++++++---------------- tests/multi_file_test.c | 2 +- tests/ogg_opus_test.c | 2 +- tests/ogg_test.c | 2 +- tests/stdin_test.c | 2 +- tests/stdout_test.c | 2 +- tests/virtual_io_test.c | 2 +- 50 files changed, 432 insertions(+), 430 deletions(-) diff --git a/examples/generate.c b/examples/generate.c index dade7d90..9b59f4ad 100644 --- a/examples/generate.c +++ b/examples/generate.c @@ -118,7 +118,7 @@ encode_file (const char *infilename, const char *outfilename, int filetype) exit (1) ; } ; - while ((readcount = sf_read_float (infile, buffer, BUFFER_LEN)) > 0) + while ((readcount = (int) sf_read_float (infile, buffer, BUFFER_LEN)) > 0) sf_write_float (outfile, buffer, readcount) ; sf_close (infile) ; diff --git a/examples/make_sine.c b/examples/make_sine.c index 76f35b0b..b905b2af 100644 --- a/examples/make_sine.c +++ b/examples/make_sine.c @@ -74,12 +74,12 @@ main (void) if (sfinfo.channels == 1) { for (k = 0 ; k < SAMPLE_COUNT ; k++) - buffer [k] = AMPLITUDE * sin (LEFT_FREQ * 2 * k * M_PI) ; + buffer [k] = (int) (AMPLITUDE * sin (LEFT_FREQ * 2 * k * M_PI)) ; } else if (sfinfo.channels == 2) { for (k = 0 ; k < SAMPLE_COUNT ; k++) - { buffer [2 * k] = AMPLITUDE * sin (LEFT_FREQ * 2 * k * M_PI) ; - buffer [2 * k + 1] = AMPLITUDE * sin (RIGHT_FREQ * 2 * k * M_PI) ; + { buffer [2 * k] = (int) (AMPLITUDE * sin (LEFT_FREQ * 2 * k * M_PI)) ; + buffer [2 * k + 1] = (int) (AMPLITUDE * sin (RIGHT_FREQ * 2 * k * M_PI)) ; } ; } else diff --git a/examples/sfprocess.c b/examples/sfprocess.c index 1c141a49..2a98d2fa 100644 --- a/examples/sfprocess.c +++ b/examples/sfprocess.c @@ -110,7 +110,7 @@ main (void) /* While there are.frames in the input file, read them, process ** them and write them to the output file. */ - while ((readcount = sf_read_double (infile, data, BUFFER_LEN))) + while ((readcount = (int) sf_read_double (infile, data, BUFFER_LEN))) { process_data (data, readcount, sfinfo.channels) ; sf_write_double (outfile, data, readcount) ; } ; diff --git a/examples/sndfile-loopify.c b/examples/sndfile-loopify.c index b0aded4a..776d25ba 100644 --- a/examples/sndfile-loopify.c +++ b/examples/sndfile-loopify.c @@ -143,7 +143,7 @@ sfe_copy_data_int (SNDFILE *outfile, SNDFILE *infile, int channels) readcount = frames ; while (readcount > 0) - { readcount = sf_readf_int (infile, data, frames) ; + { readcount = (int) sf_readf_int (infile, data, frames) ; sf_writef_int (outfile, data, readcount) ; } ; diff --git a/examples/sndfile-to-text.c b/examples/sndfile-to-text.c index d5d0ec97..25767942 100644 --- a/examples/sndfile-to-text.c +++ b/examples/sndfile-to-text.c @@ -68,7 +68,7 @@ convert_to_text (SNDFILE * infile, FILE * outfile, int channels, int full_precis frames = BLOCK_SIZE / channels ; - while ((readcount = sf_readf_float (infile, buf, frames)) > 0) + while ((readcount = (int) sf_readf_float (infile, buf, frames)) > 0) { for (k = 0 ; k < readcount ; k++) { for (m = 0 ; m < channels ; m++) if (full_precision) diff --git a/programs/common.c b/programs/common.c index fb479f09..b0567677 100644 --- a/programs/common.c +++ b/programs/common.c @@ -91,7 +91,7 @@ sfe_copy_data_int (SNDFILE *outfile, SNDFILE *infile, int channels) readcount = frames ; while (readcount > 0) - { readcount = sf_readf_int (infile, data, frames) ; + { readcount = (int) sf_readf_int (infile, data, frames) ; sf_writef_int (outfile, data, readcount) ; } ; @@ -157,7 +157,7 @@ merge_broadcast_info (SNDFILE * infile, SNDFILE * outfile, int format, const MET /* Special case loudness values */ #define REPLACE_IF_NEW_INT(x) \ if (info->x != NULL) \ - { binfo.x = round (atof (info->x) * 100.0) ; \ + { binfo.x = (int16_t) round (atof (info->x) * 100.0) ; \ } ; REPLACE_IF_NEW_INT (loudness_value) ; @@ -177,7 +177,7 @@ merge_broadcast_info (SNDFILE * infile, SNDFILE * outfile, int format, const MET /* Special case for coding_history because we may want to append. */ if (info->coding_history != NULL) { if (info->coding_hist_append) - { int slen = strlen (binfo.coding_history) ; + { int slen = (int) strlen (binfo.coding_history) ; while (slen > 1 && isspace (binfo.coding_history [slen - 1])) slen -- ; @@ -189,7 +189,7 @@ merge_broadcast_info (SNDFILE * infile, SNDFILE * outfile, int format, const MET memset (binfo.coding_history, 0, sizeof (binfo.coding_history)) ; memcpy (binfo.coding_history, info->coding_history, slen) ; - binfo.coding_history_size = slen ; + binfo.coding_history_size = (uint32_t) slen ; } ; } ; diff --git a/programs/sndfile-concat.c b/programs/sndfile-concat.c index 3a02a94d..49380fd0 100644 --- a/programs/sndfile-concat.c +++ b/programs/sndfile-concat.c @@ -143,7 +143,7 @@ concat_data_fp (SNDFILE *wfile, SNDFILE *rofile, int channels) sf_seek (wfile, 0, SEEK_END) ; while (readcount > 0) - { readcount = sf_readf_double (rofile, data, frames) ; + { readcount = (int) sf_readf_double (rofile, data, frames) ; sf_writef_double (wfile, data, readcount) ; } ; @@ -161,7 +161,7 @@ concat_data_int (SNDFILE *wfile, SNDFILE *rofile, int channels) sf_seek (wfile, 0, SEEK_END) ; while (readcount > 0) - { readcount = sf_readf_int (rofile, data, frames) ; + { readcount = (int) sf_readf_int (rofile, data, frames) ; sf_writef_int (wfile, data, readcount) ; } ; diff --git a/programs/sndfile-convert.c b/programs/sndfile-convert.c index 5aa4fdfb..12f28c56 100644 --- a/programs/sndfile-convert.c +++ b/programs/sndfile-convert.c @@ -388,7 +388,7 @@ copy_metadata (SNDFILE *outfile, SNDFILE *infile, int channels) memset (&binfo, 0, sizeof (binfo)) ; if (channels < ARRAY_LEN (chanmap)) - { size_t size = channels * sizeof (chanmap [0]) ; + { int size = channels * sizeof (chanmap [0]) ; if (sf_command (infile, SFC_GET_CHANNEL_MAP_INFO, chanmap, size) == SF_TRUE) sf_command (outfile, SFC_SET_CHANNEL_MAP_INFO, chanmap, size) ; diff --git a/programs/sndfile-deinterleave.c b/programs/sndfile-deinterleave.c index e47ff574..ade29629 100644 --- a/programs/sndfile-deinterleave.c +++ b/programs/sndfile-deinterleave.c @@ -190,7 +190,7 @@ deinterleave_int (STATE * state) int ch, k ; do - { read_len = sf_readf_int (state->infile, state->din.i, BUFFER_LEN) ; + { read_len = (int) sf_readf_int (state->infile, state->din.i, BUFFER_LEN) ; for (ch = 0 ; ch < state->channels ; ch ++) { for (k = 0 ; k < read_len ; k++) @@ -208,7 +208,7 @@ deinterleave_double (STATE * state) int ch, k ; do - { read_len = sf_readf_double (state->infile, state->din.d, BUFFER_LEN) ; + { read_len = (int) sf_readf_double (state->infile, state->din.d, BUFFER_LEN) ; for (ch = 0 ; ch < state->channels ; ch ++) { for (k = 0 ; k < read_len ; k++) diff --git a/programs/sndfile-interleave.c b/programs/sndfile-interleave.c index 4c044431..07f1b488 100644 --- a/programs/sndfile-interleave.c +++ b/programs/sndfile-interleave.c @@ -159,7 +159,7 @@ interleave_int (STATE * state) { max_read_len = 0 ; for (ch = 0 ; ch < state->channels ; ch ++) - { read_len = sf_read_int (state->infile [ch], state->din.i, BUFFER_LEN) ; + { read_len = (int) sf_read_int (state->infile [ch], state->din.i, BUFFER_LEN) ; if (read_len < BUFFER_LEN) memset (state->din.i + read_len, 0, sizeof (state->din.i [0]) * (BUFFER_LEN - read_len)) ; @@ -185,7 +185,7 @@ interleave_double (STATE * state) { max_read_len = 0 ; for (ch = 0 ; ch < state->channels ; ch ++) - { read_len = sf_read_double (state->infile [ch], state->din.d, BUFFER_LEN) ; + { read_len = (int) sf_read_double (state->infile [ch], state->din.d, BUFFER_LEN) ; if (read_len < BUFFER_LEN) memset (state->din.d + read_len, 0, sizeof (state->din.d [0]) * (BUFFER_LEN - read_len)) ; diff --git a/programs/sndfile-salvage.c b/programs/sndfile-salvage.c index 9c21d523..f60ad2ad 100644 --- a/programs/sndfile-salvage.c +++ b/programs/sndfile-salvage.c @@ -233,7 +233,7 @@ find_data_offset (int fd, int format) exit (1) ; } ; - slen = strlen (target) ; + slen = (int) strlen (target) ; lseek_or_die (fd, 0, SEEK_SET) ; diff --git a/regtest/database.c b/regtest/database.c index 58a1a52f..1f4c8d7b 100644 --- a/regtest/database.c +++ b/regtest/database.c @@ -428,7 +428,7 @@ static void get_filename_pathname (REGTEST_DB * db, const char *filepath) { const char * basename = db_basename (filepath) ; - int slen ; + size_t slen ; /* Test for a relative path */ diff --git a/src/alac.c b/src/alac.c index 2eddd512..a6668f36 100644 --- a/src/alac.c +++ b/src/alac.c @@ -471,7 +471,7 @@ alac_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) break ; readcount = (plac->frames_this_block - plac->partial_block_frames) * plac->channels ; - readcount = readcount > len ? len : readcount ; + readcount = readcount > len ? (int) len : readcount ; iptr = plac->buffer + plac->partial_block_frames * plac->channels ; @@ -501,7 +501,7 @@ alac_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) break ; readcount = (plac->frames_this_block - plac->partial_block_frames) * plac->channels ; - readcount = readcount > len ? len : readcount ; + readcount = readcount > len ? (int) len : readcount ; iptr = plac->buffer + plac->partial_block_frames * plac->channels ; @@ -534,7 +534,7 @@ alac_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) break ; readcount = (plac->frames_this_block - plac->partial_block_frames) * plac->channels ; - readcount = readcount > len ? len : readcount ; + readcount = readcount > len ? (int) len : readcount ; iptr = plac->buffer + plac->partial_block_frames * plac->channels ; @@ -567,7 +567,7 @@ alac_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) break ; readcount = (plac->frames_this_block - plac->partial_block_frames) * plac->channels ; - readcount = readcount > len ? len : readcount ; + readcount = readcount > len ? (int) len : readcount ; iptr = plac->buffer + plac->partial_block_frames * plac->channels ; @@ -648,7 +648,7 @@ alac_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len) while (len > 0) { writecount = (plac->frames_per_block - plac->partial_block_frames) * plac->channels ; - writecount = (writecount == 0 || writecount > len) ? len : writecount ; + writecount = (writecount == 0 || writecount > len) ? (int) len : writecount ; iptr = plac->buffer + plac->partial_block_frames * plac->channels ; @@ -679,7 +679,7 @@ alac_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len) while (len > 0) { writecount = (plac->frames_per_block - plac->partial_block_frames) * plac->channels ; - writecount = (writecount == 0 || writecount > len) ? len : writecount ; + writecount = (writecount == 0 || writecount > len) ? (int) len : writecount ; iptr = plac->buffer + plac->partial_block_frames * plac->channels ; @@ -713,7 +713,7 @@ alac_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) while (len > 0) { writecount = (plac->frames_per_block - plac->partial_block_frames) * plac->channels ; - writecount = (writecount == 0 || writecount > len) ? len : writecount ; + writecount = (writecount == 0 || writecount > len) ? (int) len : writecount ; iptr = plac->buffer + plac->partial_block_frames * plac->channels ; @@ -746,7 +746,7 @@ alac_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) while (len > 0) { writecount = (plac->frames_per_block - plac->partial_block_frames) * plac->channels ; - writecount = (writecount == 0 || writecount > len) ? len : writecount ; + writecount = (writecount == 0 || writecount > len) ? (int) len : writecount ; iptr = plac->buffer + plac->partial_block_frames * plac->channels ; diff --git a/src/alaw.c b/src/alaw.c index 6e0a7946..399135bc 100644 --- a/src/alaw.c +++ b/src/alaw.c @@ -372,7 +372,7 @@ alaw_read_alaw2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ucbuf, 1, bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ucbuf, 1, bufferlen, psf) ; alaw2s_array (ubuf.ucbuf, readcount, ptr + total) ; total += readcount ; if (readcount < bufferlen) @@ -394,7 +394,7 @@ alaw_read_alaw2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ucbuf, 1, bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ucbuf, 1, bufferlen, psf) ; alaw2i_array (ubuf.ucbuf, readcount, ptr + total) ; total += readcount ; if (readcount < bufferlen) @@ -419,7 +419,7 @@ alaw_read_alaw2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ucbuf, 1, bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ucbuf, 1, bufferlen, psf) ; alaw2f_array (ubuf.ucbuf, readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -443,7 +443,7 @@ alaw_read_alaw2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ucbuf, 1, bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ucbuf, 1, bufferlen, psf) ; alaw2d_array (ubuf.ucbuf, readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -469,7 +469,7 @@ alaw_write_s2alaw (SF_PRIVATE *psf, const short *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; s2alaw_array (ptr + total, bufferlen, ubuf.ucbuf) ; - writecount = psf_fwrite (ubuf.ucbuf, 1, bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ucbuf, 1, bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -491,7 +491,7 @@ alaw_write_i2alaw (SF_PRIVATE *psf, const int *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; i2alaw_array (ptr + total, bufferlen, ubuf.ucbuf) ; - writecount = psf_fwrite (ubuf.ucbuf, 1, bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ucbuf, 1, bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -516,7 +516,7 @@ alaw_write_f2alaw (SF_PRIVATE *psf, const float *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; f2alaw_array (ptr + total, bufferlen, ubuf.ucbuf, normfact) ; - writecount = psf_fwrite (ubuf.ucbuf, 1, bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ucbuf, 1, bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -541,7 +541,7 @@ alaw_write_d2alaw (SF_PRIVATE *psf, const double *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; d2alaw_array (ptr + total, bufferlen, ubuf.ucbuf, normfact) ; - writecount = psf_fwrite (ubuf.ucbuf, 1, bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ucbuf, 1, bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; diff --git a/src/broadcast.c b/src/broadcast.c index 100cbbd5..aad5f880 100644 --- a/src/broadcast.c +++ b/src/broadcast.c @@ -84,7 +84,7 @@ broadcast_var_set (SF_PRIVATE *psf, const SF_BROADCAST_INFO * info, size_t datas /* Force coding_history_size to be even. */ len = strlen (psf->broadcast_16k->coding_history) ; len += (len & 1) ? 1 : 0 ; - psf->broadcast_16k->coding_history_size = len ; + psf->broadcast_16k->coding_history_size = (uint32_t) len ; /* Currently writing this version. */ psf->broadcast_16k->version = 2 ; diff --git a/src/caf.c b/src/caf.c index 8e629ad6..a886bf56 100644 --- a/src/caf.c +++ b/src/caf.c @@ -407,7 +407,7 @@ caf_read_header (SF_PRIVATE *psf) { case peak_MARKER : psf_log_printf (psf, "%M : %D\n", marker, chunk_size) ; if (chunk_size != CAF_PEAK_CHUNK_SIZE (psf->sf.channels)) - { psf_binheader_readf (psf, "j", make_size_t (chunk_size)) ; + { psf_binheader_readf (psf, "j", (size_t) chunk_size) ; psf_log_printf (psf, "*** File PEAK chunk %D should be %d.\n", chunk_size, CAF_PEAK_CHUNK_SIZE (psf->sf.channels)) ; return SFE_CAF_BAD_PEAK ; } ; @@ -443,7 +443,7 @@ caf_read_header (SF_PRIVATE *psf) case chan_MARKER : if (chunk_size < 12) { psf_log_printf (psf, "%M : %D (should be >= 12)\n", marker, chunk_size) ; - psf_binheader_readf (psf, "j", make_size_t (chunk_size)) ; + psf_binheader_readf (psf, "j", (size_t) chunk_size) ; break ; } @@ -455,7 +455,7 @@ caf_read_header (SF_PRIVATE *psf) case free_MARKER : psf_log_printf (psf, "%M : %D\n", marker, chunk_size) ; - psf_binheader_readf (psf, "j", make_size_t (chunk_size)) ; + psf_binheader_readf (psf, "j", (size_t) chunk_size) ; break ; case data_MARKER : @@ -480,14 +480,14 @@ caf_read_header (SF_PRIVATE *psf) if (psf->datalength + psf->dataoffset < psf->filelength) psf->dataend = psf->datalength + psf->dataoffset ; - psf_binheader_readf (psf, "j", make_size_t (psf->datalength)) ; + psf_binheader_readf (psf, "j", (size_t) psf->datalength) ; have_data = 1 ; break ; case kuki_MARKER : psf_log_printf (psf, "%M : %D\n", marker, chunk_size) ; pcaf->alac.kuki_offset = psf_ftell (psf) - 12 ; - psf_binheader_readf (psf, "j", make_size_t (chunk_size)) ; + psf_binheader_readf (psf, "j", (size_t) chunk_size) ; break ; case pakt_MARKER : @@ -519,7 +519,7 @@ caf_read_header (SF_PRIVATE *psf) psf_log_printf (psf, "*** 'pakt' chunk header is all zero.\n") ; pcaf->alac.pakt_offset = psf_ftell (psf) - 12 ; - psf_binheader_readf (psf, "j", make_size_t (chunk_size) - 24) ; + psf_binheader_readf (psf, "j", (size_t) chunk_size - 24) ; break ; case info_MARKER : @@ -538,7 +538,7 @@ caf_read_header (SF_PRIVATE *psf) default : psf_log_printf (psf, "%M : %D (skipped)\n", marker, chunk_size) ; - psf_binheader_readf (psf, "j", make_size_t (chunk_size)) ; + psf_binheader_readf (psf, "j", (size_t) chunk_size) ; break ; } ; @@ -846,7 +846,7 @@ caf_read_strings (SF_PRIVATE * psf, sf_count_t chunk_size) if ((buf = malloc (chunk_size + 1)) == NULL) return (psf->error = SFE_MALLOC_FAILED) ; - psf_binheader_readf (psf, "E4b", &count, buf, make_size_t (chunk_size)) ; + psf_binheader_readf (psf, "E4b", &count, buf, (size_t) chunk_size) ; psf_log_printf (psf, " count: %u\n", count) ; /* Force terminate `buf` to make sure. */ diff --git a/src/cart.c b/src/cart.c index a56ed60e..8b00eab1 100644 --- a/src/cart.c +++ b/src/cart.c @@ -79,7 +79,7 @@ cart_var_set (SF_PRIVATE *psf, const SF_CART_INFO * info, size_t datasize) len = strlen (psf->cart_16k->tag_text) ; len += (len & 1) ? 1 : 2 ; - psf->cart_16k->tag_text_size = len ; + psf->cart_16k->tag_text_size = (uint32_t) len ; return SF_TRUE ; } /* cart_var_set */ diff --git a/src/chunk.c b/src/chunk.c index afd051ef..08411d3e 100644 --- a/src/chunk.c +++ b/src/chunk.c @@ -75,7 +75,7 @@ psf_get_chunk_iterator (SF_PRIVATE * psf, const char * marker_str) hash = marker_len > 4 ? hash_of_str (marker_str) : u.marker ; memcpy (psf->iterator->id, marker_str, marker_len) ; - psf->iterator->id_size = marker_len ; + psf->iterator->id_size = (unsigned) marker_len ; psf->iterator->hash = hash ; } @@ -211,7 +211,7 @@ psf_store_read_chunk_str (READ_CHUNKS * pchk, const char * marker_str, sf_count_ rchunk.offset = offset ; rchunk.len = len ; - rchunk.id_size = marker_len > 64 ? 64 : marker_len ; + rchunk.id_size = marker_len > 64 ? 64 : (unsigned) marker_len ; memcpy (rchunk.id, marker_str, rchunk.id_size) ; return psf_store_read_chunk (pchk, &rchunk) ; diff --git a/src/command.c b/src/command.c index 15037ab5..a5f8bb8b 100644 --- a/src/command.c +++ b/src/command.c @@ -309,7 +309,7 @@ psf_calc_signal_max (SF_PRIVATE *psf, int normalize) len = ARRAY_LEN (ubuf.dbuf) - (ARRAY_LEN (ubuf.dbuf) % psf->sf.channels) ; for (readcount = 1, max_val = 0.0 ; readcount > 0 ; /* nothing */) - { readcount = sf_read_double ((SNDFILE*) psf, data, len) ; + { readcount = (int) sf_read_double ((SNDFILE*) psf, data, len) ; for (k = 0 ; k < readcount ; k++) { temp = fabs (data [k]) ; max_val = temp > max_val ? temp : max_val ; @@ -354,7 +354,7 @@ psf_calc_max_all_channels (SF_PRIVATE *psf, double *peaks, int normalize) chan = 0 ; readcount = len ; while (readcount > 0) - { readcount = sf_read_double ((SNDFILE*) psf, data, len) ; + { readcount = (int) sf_read_double ((SNDFILE*) psf, data, len) ; for (k = 0 ; k < readcount ; k++) { temp = fabs (data [k]) ; peaks [chan] = temp > peaks [chan] ? temp : peaks [chan] ; diff --git a/src/common.c b/src/common.c index 98bd65c0..b65b9158 100644 --- a/src/common.c +++ b/src/common.c @@ -525,24 +525,24 @@ header_put_le_int (SF_PRIVATE *psf, int x) static inline void header_put_be_8byte (SF_PRIVATE *psf, sf_count_t x) { psf->header.ptr [psf->header.indx++] = (x >> 56) ; - psf->header.ptr [psf->header.indx++] = (x >> 48) ; - psf->header.ptr [psf->header.indx++] = (x >> 40) ; - psf->header.ptr [psf->header.indx++] = (x >> 32) ; - psf->header.ptr [psf->header.indx++] = (x >> 24) ; - psf->header.ptr [psf->header.indx++] = (x >> 16) ; - psf->header.ptr [psf->header.indx++] = (x >> 8) ; - psf->header.ptr [psf->header.indx++] = x ; + psf->header.ptr [psf->header.indx++] = (unsigned char) (x >> 48) ; + psf->header.ptr [psf->header.indx++] = (unsigned char) (x >> 40) ; + psf->header.ptr [psf->header.indx++] = (unsigned char) (x >> 32) ; + psf->header.ptr [psf->header.indx++] = (unsigned char) (x >> 24) ; + psf->header.ptr [psf->header.indx++] = (unsigned char) (x >> 16) ; + psf->header.ptr [psf->header.indx++] = (unsigned char) (x >> 8) ; + psf->header.ptr [psf->header.indx++] = (unsigned char) x ; } /* header_put_be_8byte */ static inline void header_put_le_8byte (SF_PRIVATE *psf, sf_count_t x) -{ psf->header.ptr [psf->header.indx++] = x ; - psf->header.ptr [psf->header.indx++] = (x >> 8) ; - psf->header.ptr [psf->header.indx++] = (x >> 16) ; - psf->header.ptr [psf->header.indx++] = (x >> 24) ; - psf->header.ptr [psf->header.indx++] = (x >> 32) ; - psf->header.ptr [psf->header.indx++] = (x >> 40) ; - psf->header.ptr [psf->header.indx++] = (x >> 48) ; +{ psf->header.ptr [psf->header.indx++] = (unsigned char) x ; + psf->header.ptr [psf->header.indx++] = (unsigned char) (x >> 8) ; + psf->header.ptr [psf->header.indx++] = (unsigned char) (x >> 16) ; + psf->header.ptr [psf->header.indx++] = (unsigned char) (x >> 24) ; + psf->header.ptr [psf->header.indx++] = (unsigned char) (x >> 32) ; + psf->header.ptr [psf->header.indx++] = (unsigned char) (x >> 40) ; + psf->header.ptr [psf->header.indx++] = (unsigned char) (x >> 48) ; psf->header.ptr [psf->header.indx++] = (x >> 56) ; } /* header_put_le_8byte */ diff --git a/src/dither.c b/src/dither.c index 6f23c674..b0ef7311 100644 --- a/src/dither.c +++ b/src/dither.c @@ -242,7 +242,7 @@ dither_write_short (SF_PRIVATE *psf, const short *ptr, sf_count_t len) dither_short (ptr, (short*) pdither->buffer, writecount / psf->sf.channels, psf->sf.channels) ; - thiswrite = pdither->write_short (psf, (short*) pdither->buffer, writecount) ; + thiswrite = (int) pdither->write_short (psf, (short*) pdither->buffer, writecount) ; total += thiswrite ; len -= thiswrite ; if (thiswrite < writecount) @@ -288,7 +288,7 @@ dither_write_int (SF_PRIVATE *psf, const int *ptr, sf_count_t len) dither_int (ptr, (int*) pdither->buffer, writecount / psf->sf.channels, psf->sf.channels) ; - thiswrite = pdither->write_int (psf, (int*) pdither->buffer, writecount) ; + thiswrite = (int) pdither->write_int (psf, (int*) pdither->buffer, writecount) ; total += thiswrite ; len -= thiswrite ; if (thiswrite < writecount) @@ -333,7 +333,7 @@ dither_write_float (SF_PRIVATE *psf, const float *ptr, sf_count_t len) dither_float (ptr, (float*) pdither->buffer, writecount / psf->sf.channels, psf->sf.channels) ; - thiswrite = pdither->write_float (psf, (float*) pdither->buffer, writecount) ; + thiswrite = (int) pdither->write_float (psf, (float*) pdither->buffer, writecount) ; total += thiswrite ; len -= thiswrite ; if (thiswrite < writecount) @@ -379,7 +379,7 @@ dither_write_double (SF_PRIVATE *psf, const double *ptr, sf_count_t len) dither_double (ptr, (double*) pdither->buffer, writecount / psf->sf.channels, psf->sf.channels) ; - thiswrite = pdither->write_double (psf, (double*) pdither->buffer, writecount) ; + thiswrite = (int) pdither->write_double (psf, (double*) pdither->buffer, writecount) ; total += thiswrite ; len -= thiswrite ; if (thiswrite < writecount) diff --git a/src/double64.c b/src/double64.c index fb6d468c..ff317773 100644 --- a/src/double64.c +++ b/src/double64.c @@ -572,7 +572,7 @@ host_read_d2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.dbuf, sizeof (double), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.dbuf, sizeof (double), bufferlen, psf) ; if (psf->data_endswap == SF_TRUE) endswap_double_array (ubuf.dbuf, readcount) ; @@ -602,7 +602,7 @@ host_read_d2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.dbuf, sizeof (double), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.dbuf, sizeof (double), bufferlen, psf) ; if (psf->data_endswap == SF_TRUE) endswap_double_array (ubuf.dbuf, bufferlen) ; @@ -628,7 +628,7 @@ host_read_d2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.dbuf, sizeof (double), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.dbuf, sizeof (double), bufferlen, psf) ; if (psf->data_endswap == SF_TRUE) endswap_double_array (ubuf.dbuf, readcount) ; @@ -695,7 +695,7 @@ host_write_s2d (SF_PRIVATE *psf, const short *ptr, sf_count_t len) if (psf->data_endswap == SF_TRUE) endswap_double_array (ubuf.dbuf, bufferlen) ; - writecount = psf_fwrite (ubuf.dbuf, sizeof (double), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.dbuf, sizeof (double), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -726,7 +726,7 @@ host_write_i2d (SF_PRIVATE *psf, const int *ptr, sf_count_t len) if (psf->data_endswap == SF_TRUE) endswap_double_array (ubuf.dbuf, bufferlen) ; - writecount = psf_fwrite (ubuf.dbuf, sizeof (double), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.dbuf, sizeof (double), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -755,7 +755,7 @@ host_write_f2d (SF_PRIVATE *psf, const float *ptr, sf_count_t len) if (psf->data_endswap == SF_TRUE) endswap_double_array (ubuf.dbuf, bufferlen) ; - writecount = psf_fwrite (ubuf.dbuf, sizeof (double), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.dbuf, sizeof (double), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -785,7 +785,7 @@ host_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) endswap_double_copy (ubuf.dbuf, ptr + total, bufferlen) ; - writecount = psf_fwrite (ubuf.dbuf, sizeof (double), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.dbuf, sizeof (double), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -811,7 +811,7 @@ replace_read_d2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.dbuf, sizeof (double), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.dbuf, sizeof (double), bufferlen, psf) ; if (psf->data_endswap == SF_TRUE) endswap_double_array (ubuf.dbuf, bufferlen) ; @@ -841,7 +841,7 @@ replace_read_d2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.dbuf, sizeof (double), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.dbuf, sizeof (double), bufferlen, psf) ; if (psf->data_endswap == SF_TRUE) endswap_double_array (ubuf.dbuf, bufferlen) ; @@ -869,7 +869,7 @@ replace_read_d2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.dbuf, sizeof (double), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.dbuf, sizeof (double), bufferlen, psf) ; if (psf->data_endswap == SF_TRUE) endswap_double_array (ubuf.dbuf, bufferlen) ; @@ -899,7 +899,7 @@ replace_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.dbuf, sizeof (double), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.dbuf, sizeof (double), bufferlen, psf) ; if (psf->data_endswap == SF_TRUE) endswap_double_array (ubuf.dbuf, readcount) ; @@ -940,7 +940,7 @@ replace_write_s2d (SF_PRIVATE *psf, const short *ptr, sf_count_t len) if (psf->data_endswap == SF_TRUE) endswap_double_array (ubuf.dbuf, bufferlen) ; - writecount = psf_fwrite (ubuf.dbuf, sizeof (double), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.dbuf, sizeof (double), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -973,7 +973,7 @@ replace_write_i2d (SF_PRIVATE *psf, const int *ptr, sf_count_t len) if (psf->data_endswap == SF_TRUE) endswap_double_array (ubuf.dbuf, bufferlen) ; - writecount = psf_fwrite (ubuf.dbuf, sizeof (double), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.dbuf, sizeof (double), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -1001,7 +1001,7 @@ replace_write_f2d (SF_PRIVATE *psf, const float *ptr, sf_count_t len) if (psf->data_endswap == SF_TRUE) endswap_double_array (ubuf.dbuf, bufferlen) ; - writecount = psf_fwrite (ubuf.dbuf, sizeof (double), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.dbuf, sizeof (double), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -1034,7 +1034,7 @@ replace_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) if (psf->data_endswap == SF_TRUE) endswap_double_array (ubuf.dbuf, bufferlen) ; - writecount = psf_fwrite (ubuf.dbuf, sizeof (double), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.dbuf, sizeof (double), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; diff --git a/src/dwvw.c b/src/dwvw.c index 13985ad7..a015fceb 100644 --- a/src/dwvw.c +++ b/src/dwvw.c @@ -198,7 +198,7 @@ dwvw_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) iptr = ubuf.ibuf ; bufferlen = ARRAY_LEN (ubuf.ibuf) ; while (len > 0) - { readcount = (len >= bufferlen) ? bufferlen : len ; + { readcount = (len >= bufferlen) ? bufferlen : (int) len ; count = dwvw_decode_data (psf, pdwvw, iptr, readcount) ; for (k = 0 ; k < readcount ; k++) ptr [total + k] = iptr [k] >> 16 ; @@ -255,7 +255,7 @@ dwvw_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) iptr = ubuf.ibuf ; bufferlen = ARRAY_LEN (ubuf.ibuf) ; while (len > 0) - { readcount = (len >= bufferlen) ? bufferlen : len ; + { readcount = (len >= bufferlen) ? bufferlen : (int) len ; count = dwvw_decode_data (psf, pdwvw, iptr, readcount) ; for (k = 0 ; k < readcount ; k++) ptr [total + k] = normfact * (float) (iptr [k]) ; @@ -287,7 +287,7 @@ dwvw_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) iptr = ubuf.ibuf ; bufferlen = ARRAY_LEN (ubuf.ibuf) ; while (len > 0) - { readcount = (len >= bufferlen) ? bufferlen : len ; + { readcount = (len >= bufferlen) ? bufferlen : (int) len ; count = dwvw_decode_data (psf, pdwvw, iptr, readcount) ; for (k = 0 ; k < readcount ; k++) ptr [total + k] = normfact * (double) (iptr [k]) ; @@ -377,7 +377,7 @@ dwvw_decode_load_bits (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int bit_count) /* Load bits in bit reseviour. */ while (pdwvw->bit_count < bit_count) { if (pdwvw->b.index >= pdwvw->b.end) - { pdwvw->b.end = psf_fread (pdwvw->b.buffer, 1, sizeof (pdwvw->b.buffer), psf) ; + { pdwvw->b.end = (int) psf_fread (pdwvw->b.buffer, 1, sizeof (pdwvw->b.buffer), psf) ; pdwvw->b.index = 0 ; } ; @@ -569,7 +569,7 @@ dwvw_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len) iptr = ubuf.ibuf ; bufferlen = ARRAY_LEN (ubuf.ibuf) ; while (len > 0) - { writecount = (len >= bufferlen) ? bufferlen : len ; + { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) iptr [k] = arith_shift_left (ptr [total + k], 16) ; count = dwvw_encode_data (psf, pdwvw, iptr, writecount) ; @@ -626,7 +626,7 @@ dwvw_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) iptr = ubuf.ibuf ; bufferlen = ARRAY_LEN (ubuf.ibuf) ; while (len > 0) - { writecount = (len >= bufferlen) ? bufferlen : len ; + { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) iptr [k] = psf_lrintf (normfact * ptr [total + k]) ; count = dwvw_encode_data (psf, pdwvw, iptr, writecount) ; @@ -658,7 +658,7 @@ dwvw_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) iptr = ubuf.ibuf ; bufferlen = ARRAY_LEN (ubuf.ibuf) ; while (len > 0) - { writecount = (len >= bufferlen) ? bufferlen : len ; + { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) iptr [k] = psf_lrint (normfact * ptr [total + k]) ; count = dwvw_encode_data (psf, pdwvw, iptr, writecount) ; diff --git a/src/float32.c b/src/float32.c index e8b0aded..e5eddaec 100644 --- a/src/float32.c +++ b/src/float32.c @@ -523,7 +523,7 @@ host_read_f2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.fbuf, sizeof (float), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.fbuf, sizeof (float), bufferlen, psf) ; /* Fix me : Need lef2s_array */ if (psf->data_endswap == SF_TRUE) @@ -554,7 +554,7 @@ host_read_f2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.fbuf, sizeof (float), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.fbuf, sizeof (float), bufferlen, psf) ; if (psf->data_endswap == SF_TRUE) endswap_int_array (ubuf.ibuf, bufferlen) ; @@ -583,7 +583,7 @@ host_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.fbuf, sizeof (float), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.fbuf, sizeof (float), bufferlen, psf) ; endswap_int_copy ((int*) (ptr + total), ubuf.ibuf, readcount) ; @@ -607,7 +607,7 @@ host_read_f2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.fbuf, sizeof (float), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.fbuf, sizeof (float), bufferlen, psf) ; if (psf->data_endswap == SF_TRUE) endswap_int_array (ubuf.ibuf, bufferlen) ; @@ -645,7 +645,7 @@ host_write_s2f (SF_PRIVATE *psf, const short *ptr, sf_count_t len) if (psf->data_endswap == SF_TRUE) endswap_int_array (ubuf.ibuf, bufferlen) ; - writecount = psf_fwrite (ubuf.fbuf, sizeof (float), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.fbuf, sizeof (float), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -676,7 +676,7 @@ host_write_i2f (SF_PRIVATE *psf, const int *ptr, sf_count_t len) if (psf->data_endswap == SF_TRUE) endswap_int_array (ubuf.ibuf, bufferlen) ; - writecount = psf_fwrite (ubuf.fbuf, sizeof (float) , bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.fbuf, sizeof (float) , bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -706,7 +706,7 @@ host_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) endswap_int_copy (ubuf.ibuf, (const int*) (ptr + total), bufferlen) ; - writecount = psf_fwrite (ubuf.fbuf, sizeof (float), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.fbuf, sizeof (float), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -736,7 +736,7 @@ host_write_d2f (SF_PRIVATE *psf, const double *ptr, sf_count_t len) if (psf->data_endswap == SF_TRUE) endswap_int_array (ubuf.ibuf, bufferlen) ; - writecount = psf_fwrite (ubuf.fbuf, sizeof (float), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.fbuf, sizeof (float), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -762,7 +762,7 @@ replace_read_f2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.fbuf, sizeof (float), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.fbuf, sizeof (float), bufferlen, psf) ; if (psf->data_endswap == SF_TRUE) endswap_int_array (ubuf.ibuf, bufferlen) ; @@ -792,7 +792,7 @@ replace_read_f2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.fbuf, sizeof (float), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.fbuf, sizeof (float), bufferlen, psf) ; if (psf->data_endswap == SF_TRUE) endswap_int_array (ubuf.ibuf, bufferlen) ; @@ -822,7 +822,7 @@ replace_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.fbuf, sizeof (float), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.fbuf, sizeof (float), bufferlen, psf) ; if (psf->data_endswap == SF_TRUE) endswap_int_array (ubuf.ibuf, bufferlen) ; @@ -851,7 +851,7 @@ replace_read_f2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.fbuf, sizeof (float), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.fbuf, sizeof (float), bufferlen, psf) ; if (psf->data_endswap == SF_TRUE) endswap_int_array (ubuf.ibuf, bufferlen) ; @@ -891,7 +891,7 @@ replace_write_s2f (SF_PRIVATE *psf, const short *ptr, sf_count_t len) if (psf->data_endswap == SF_TRUE) endswap_int_array (ubuf.ibuf, bufferlen) ; - writecount = psf_fwrite (ubuf.fbuf, sizeof (float), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.fbuf, sizeof (float), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -924,7 +924,7 @@ replace_write_i2f (SF_PRIVATE *psf, const int *ptr, sf_count_t len) if (psf->data_endswap == SF_TRUE) endswap_int_array (ubuf.ibuf, bufferlen) ; - writecount = psf_fwrite (ubuf.fbuf, sizeof (float), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.fbuf, sizeof (float), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -957,7 +957,7 @@ replace_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) if (psf->data_endswap == SF_TRUE) endswap_int_array (ubuf.ibuf, bufferlen) ; - writecount = psf_fwrite (ubuf.fbuf, sizeof (float) , bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.fbuf, sizeof (float) , bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -988,7 +988,7 @@ replace_write_d2f (SF_PRIVATE *psf, const double *ptr, sf_count_t len) if (psf->data_endswap == SF_TRUE) endswap_int_array (ubuf.ibuf, bufferlen) ; - writecount = psf_fwrite (ubuf.fbuf, sizeof (float), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.fbuf, sizeof (float), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; diff --git a/src/g72x.c b/src/g72x.c index 563a9092..8698ebf7 100644 --- a/src/g72x.c +++ b/src/g72x.c @@ -264,7 +264,7 @@ g72x_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = SF_BUFFER_LEN / sizeof (short) ; while (len > 0) - { readcount = (len >= bufferlen) ? bufferlen : len ; + { readcount = (len >= bufferlen) ? bufferlen : (int) len ; count = g72x_read_block (psf, pg72x, sptr, readcount) ; for (k = 0 ; k < readcount ; k++) @@ -297,7 +297,7 @@ g72x_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = SF_BUFFER_LEN / sizeof (short) ; while (len > 0) - { readcount = (len >= bufferlen) ? bufferlen : len ; + { readcount = (len >= bufferlen) ? bufferlen : (int) len ; count = g72x_read_block (psf, pg72x, sptr, readcount) ; for (k = 0 ; k < readcount ; k++) ptr [total + k] = normfact * sptr [k] ; @@ -329,7 +329,7 @@ g72x_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = SF_BUFFER_LEN / sizeof (short) ; while (len > 0) - { readcount = (len >= bufferlen) ? bufferlen : len ; + { readcount = (len >= bufferlen) ? bufferlen : (int) len ; count = g72x_read_block (psf, pg72x, sptr, readcount) ; for (k = 0 ; k < readcount ; k++) ptr [total + k] = normfact * (double) (sptr [k]) ; @@ -505,7 +505,7 @@ g72x_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = SF_BUFFER_LEN / sizeof (short) ; while (len > 0) - { writecount = (len >= bufferlen) ? bufferlen : len ; + { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) sptr [k] = ptr [total + k] >> 16 ; count = g72x_write_block (psf, pg72x, sptr, writecount) ; @@ -536,7 +536,7 @@ g72x_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = SF_BUFFER_LEN / sizeof (short) ; while (len > 0) - { writecount = (len >= bufferlen) ? bufferlen : len ; + { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) sptr [k] = psf_lrintf (normfact * ptr [total + k]) ; count = g72x_write_block (psf, pg72x, sptr, writecount) ; @@ -568,7 +568,7 @@ g72x_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = SF_BUFFER_LEN / sizeof (short) ; while (len > 0) - { writecount = (len >= bufferlen) ? bufferlen : len ; + { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) sptr [k] = psf_lrint (normfact * ptr [total + k]) ; count = g72x_write_block (psf, pg72x, sptr, writecount) ; diff --git a/src/gsm610.c b/src/gsm610.c index 06519bc2..77d4f1fe 100644 --- a/src/gsm610.c +++ b/src/gsm610.c @@ -198,7 +198,7 @@ gsm610_wav_decode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) return 1 ; } ; - if ((k = psf_fread (pgsm610->block, 1, WAVLIKE_GSM610_BLOCKSIZE, psf)) != WAVLIKE_GSM610_BLOCKSIZE) + if ((k = (int) psf_fread (pgsm610->block, 1, WAVLIKE_GSM610_BLOCKSIZE, psf)) != WAVLIKE_GSM610_BLOCKSIZE) psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, WAVLIKE_GSM610_BLOCKSIZE) ; if (gsm_decode (pgsm610->gsm_data, pgsm610->block, pgsm610->samples) < 0) @@ -226,7 +226,7 @@ gsm610_decode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) return 1 ; } ; - if ((k = psf_fread (pgsm610->block, 1, GSM610_BLOCKSIZE, psf)) != GSM610_BLOCKSIZE) + if ((k = (int) psf_fread (pgsm610->block, 1, GSM610_BLOCKSIZE, psf)) != GSM610_BLOCKSIZE) psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, GSM610_BLOCKSIZE) ; if (gsm_decode (pgsm610->gsm_data, pgsm610->block, pgsm610->samples) < 0) @@ -302,7 +302,7 @@ gsm610_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = ARRAY_LEN (ubuf.sbuf) ; while (len > 0) - { readcount = (len >= bufferlen) ? bufferlen : len ; + { readcount = (len >= bufferlen) ? bufferlen : (int) len ; count = gsm610_read_block (psf, pgsm610, sptr, readcount) ; for (k = 0 ; k < readcount ; k++) ptr [total + k] = arith_shift_left (sptr [k], 16) ; @@ -331,7 +331,7 @@ gsm610_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = ARRAY_LEN (ubuf.sbuf) ; while (len > 0) - { readcount = (len >= bufferlen) ? bufferlen : len ; + { readcount = (len >= bufferlen) ? bufferlen : (int) len ; count = gsm610_read_block (psf, pgsm610, sptr, readcount) ; for (k = 0 ; k < readcount ; k++) ptr [total + k] = normfact * sptr [k] ; @@ -360,7 +360,7 @@ gsm610_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = ARRAY_LEN (ubuf.sbuf) ; while (len > 0) - { readcount = (len >= bufferlen) ? bufferlen : len ; + { readcount = (len >= bufferlen) ? bufferlen : (int) len ; count = gsm610_read_block (psf, pgsm610, sptr, readcount) ; for (k = 0 ; k < readcount ; k++) ptr [total + k] = normfact * sptr [k] ; @@ -437,7 +437,7 @@ gsm610_encode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) gsm_encode (pgsm610->gsm_data, pgsm610->samples, pgsm610->block) ; /* Write the block to disk. */ - if ((k = psf_fwrite (pgsm610->block, 1, GSM610_BLOCKSIZE, psf)) != GSM610_BLOCKSIZE) + if ((k = (int) psf_fwrite (pgsm610->block, 1, GSM610_BLOCKSIZE, psf)) != GSM610_BLOCKSIZE) psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, GSM610_BLOCKSIZE) ; pgsm610->samplecount = 0 ; @@ -458,7 +458,7 @@ gsm610_wav_encode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) gsm_encode (pgsm610->gsm_data, pgsm610->samples+WAVLIKE_GSM610_SAMPLES / 2, pgsm610->block+WAVLIKE_GSM610_BLOCKSIZE / 2) ; /* Write the block to disk. */ - if ((k = psf_fwrite (pgsm610->block, 1, WAVLIKE_GSM610_BLOCKSIZE, psf)) != WAVLIKE_GSM610_BLOCKSIZE) + if ((k = (int) psf_fwrite (pgsm610->block, 1, WAVLIKE_GSM610_BLOCKSIZE, psf)) != WAVLIKE_GSM610_BLOCKSIZE) psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, WAVLIKE_GSM610_BLOCKSIZE) ; pgsm610->samplecount = 0 ; @@ -532,7 +532,7 @@ gsm610_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = ARRAY_LEN (ubuf.sbuf) ; while (len > 0) - { writecount = (len >= bufferlen) ? bufferlen : len ; + { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) sptr [k] = ptr [total + k] >> 16 ; count = gsm610_write_block (psf, pgsm610, sptr, writecount) ; @@ -561,7 +561,7 @@ gsm610_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = ARRAY_LEN (ubuf.sbuf) ; while (len > 0) - { writecount = (len >= bufferlen) ? bufferlen : len ; + { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) sptr [k] = psf_lrintf (normfact * ptr [total + k]) ; count = gsm610_write_block (psf, pgsm610, sptr, writecount) ; @@ -592,7 +592,7 @@ gsm610_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = ARRAY_LEN (ubuf.sbuf) ; while (len > 0) - { writecount = (len >= bufferlen) ? bufferlen : len ; + { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) sptr [k] = psf_lrint (normfact * ptr [total + k]) ; count = gsm610_write_block (psf, pgsm610, sptr, writecount) ; diff --git a/src/ima_adpcm.c b/src/ima_adpcm.c index d90d9ac0..bc61f4e5 100644 --- a/src/ima_adpcm.c +++ b/src/ima_adpcm.c @@ -273,7 +273,7 @@ count ++ ; return 1 ; } ; - if ((k = psf_fread (pima->block, 1, pima->blocksize * pima->channels, psf)) != pima->blocksize * pima->channels) + if ((k = (int) psf_fread (pima->block, 1, pima->blocksize * pima->channels, psf)) != pima->blocksize * pima->channels) psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pima->blocksize) ; /* Read and check the block header. */ @@ -378,7 +378,7 @@ aiff_ima_encode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima) } ; /* Write the block to disk. */ - if ((k = psf_fwrite (pima->block, 1, pima->channels * pima->blocksize, psf)) != pima->channels * pima->blocksize) + if ((k = (int) psf_fwrite (pima->block, 1, pima->channels * pima->blocksize, psf)) != pima->channels * pima->blocksize) psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, pima->channels * pima->blocksize) ; memset (pima->block, 0, pima->channels * pima->blocksize) ; @@ -401,7 +401,7 @@ wavlike_ima_decode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima) return 1 ; } ; - if ((k = psf_fread (pima->block, 1, pima->blocksize, psf)) != pima->blocksize) + if ((k = (int) psf_fread (pima->block, 1, pima->blocksize, psf)) != pima->blocksize) psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pima->blocksize) ; /* Read and check the block header. */ @@ -557,7 +557,7 @@ wavlike_ima_encode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima) /* Write the block to disk. */ - if ((k = psf_fwrite (pima->block, 1, pima->blocksize, psf)) != pima->blocksize) + if ((k = (int) psf_fwrite (pima->block, 1, pima->blocksize, psf)) != pima->blocksize) psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, pima->blocksize) ; memset (pima->samples, 0, pima->samplesperblock * sizeof (short)) ; diff --git a/src/ms_adpcm.c b/src/ms_adpcm.c index a21cb994..0e05181a 100644 --- a/src/ms_adpcm.c +++ b/src/ms_adpcm.c @@ -230,7 +230,7 @@ msadpcm_decode_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms) return 1 ; } ; - if ((k = psf_fread (pms->block, 1, pms->blocksize, psf)) != pms->blocksize) + if ((k = (int) psf_fread (pms->block, 1, pms->blocksize, psf)) != pms->blocksize) { psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pms->blocksize) ; if (k <= 0) return 1 ; @@ -355,7 +355,7 @@ msadpcm_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) while (len > 0) { readcount = (len > 0x10000000) ? 0x10000000 : (int) len ; - if ((count = msadpcm_read_block (psf, pms, ptr, readcount)) <= 0) + if ((count = (int) msadpcm_read_block (psf, pms, ptr, readcount)) <= 0) return -1 ; total += count ; @@ -382,9 +382,9 @@ msadpcm_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = ARRAY_LEN (ubuf.sbuf) ; while (len > 0) - { readcount = (len >= bufferlen) ? bufferlen : len ; + { readcount = (len >= bufferlen) ? bufferlen : (int) len ; - if ((count = msadpcm_read_block (psf, pms, sptr, readcount)) <= 0) + if ((count = (int) msadpcm_read_block (psf, pms, sptr, readcount)) <= 0) return -1 ; for (k = 0 ; k < readcount ; k++) @@ -414,9 +414,9 @@ msadpcm_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = ARRAY_LEN (ubuf.sbuf) ; while (len > 0) - { readcount = (len >= bufferlen) ? bufferlen : len ; + { readcount = (len >= bufferlen) ? bufferlen : (int) len ; - if ((count = msadpcm_read_block (psf, pms, sptr, readcount)) <= 0) + if ((count = (int) msadpcm_read_block (psf, pms, sptr, readcount)) <= 0) return -1 ; for (k = 0 ; k < readcount ; k++) @@ -446,9 +446,9 @@ msadpcm_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = ARRAY_LEN (ubuf.sbuf) ; while (len > 0) - { readcount = (len >= bufferlen) ? bufferlen : len ; + { readcount = (len >= bufferlen) ? bufferlen : (int) len ; - if ((count = msadpcm_read_block (psf, pms, sptr, readcount)) <= 0) + if ((count = (int) msadpcm_read_block (psf, pms, sptr, readcount)) <= 0) return -1 ; for (k = 0 ; k < readcount ; k++) @@ -633,7 +633,7 @@ msadpcm_encode_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms) /* Write the block to disk. */ - if ((k = psf_fwrite (pms->block, 1, pms->blocksize, psf)) != pms->blocksize) + if ((k = (int) psf_fwrite (pms->block, 1, pms->blocksize, psf)) != pms->blocksize) psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, pms->blocksize) ; memset (pms->samples, 0, pms->samplesperblock * sizeof (short)) ; @@ -679,7 +679,7 @@ msadpcm_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len) while (len > 0) { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ; - count = msadpcm_write_block (psf, pms, ptr, writecount) ; + count = (int) msadpcm_write_block (psf, pms, ptr, writecount) ; total += count ; len -= count ; @@ -705,10 +705,10 @@ msadpcm_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = ARRAY_LEN (ubuf.sbuf) ; while (len > 0) - { writecount = (len >= bufferlen) ? bufferlen : len ; + { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) sptr [k] = ptr [total + k] >> 16 ; - count = msadpcm_write_block (psf, pms, sptr, writecount) ; + count = (int) msadpcm_write_block (psf, pms, sptr, writecount) ; total += count ; len -= writecount ; if (count != writecount) @@ -735,10 +735,10 @@ msadpcm_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = ARRAY_LEN (ubuf.sbuf) ; while (len > 0) - { writecount = (len >= bufferlen) ? bufferlen : len ; + { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) sptr [k] = psf_lrintf (normfact * ptr [total + k]) ; - count = msadpcm_write_block (psf, pms, sptr, writecount) ; + count = (int) msadpcm_write_block (psf, pms, sptr, writecount) ; total += count ; len -= writecount ; if (count != writecount) @@ -765,10 +765,10 @@ msadpcm_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = ARRAY_LEN (ubuf.sbuf) ; while (len > 0) - { writecount = (len >= bufferlen) ? bufferlen : len ; + { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) sptr [k] = psf_lrint (normfact * ptr [total + k]) ; - count = msadpcm_write_block (psf, pms, sptr, writecount) ; + count = (int) msadpcm_write_block (psf, pms, sptr, writecount) ; total += count ; len -= writecount ; if (count != writecount) diff --git a/src/nms_adpcm.c b/src/nms_adpcm.c index 40f56f59..96d6ad26 100644 --- a/src/nms_adpcm.c +++ b/src/nms_adpcm.c @@ -702,7 +702,7 @@ static int psf_nms_adpcm_decode_block (SF_PRIVATE *psf, NMS_ADPCM_PRIVATE *pnms) { int k ; - if ((k = psf_fread (pnms->block, sizeof (short), pnms->shortsperblock, psf)) != pnms->shortsperblock) + if ((k = (int) psf_fread (pnms->block, sizeof (short), pnms->shortsperblock, psf)) != pnms->shortsperblock) { psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pnms->shortsperblock) ; memset (pnms->block + (k * sizeof (short)), 0, (pnms->shortsperblock - k) * sizeof (short)) ; } ; @@ -785,7 +785,7 @@ nms_adpcm_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = SF_BUFFER_LEN / sizeof (short) ; while (len > 0) - { readcount = (len >= bufferlen) ? bufferlen : len ; + { readcount = (len >= bufferlen) ? bufferlen : (int) len ; count = nms_adpcm_read_block (psf, pnms, sptr, readcount) ; for (k = 0 ; k < readcount ; k++) @@ -818,7 +818,7 @@ nms_adpcm_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = SF_BUFFER_LEN / sizeof (short) ; while (len > 0) - { readcount = (len >= bufferlen) ? bufferlen : len ; + { readcount = (len >= bufferlen) ? bufferlen : (int) len ; count = nms_adpcm_read_block (psf, pnms, sptr, readcount) ; for (k = 0 ; k < readcount ; k++) ptr [total + k] = normfact * sptr [k] ; @@ -850,7 +850,7 @@ nms_adpcm_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = SF_BUFFER_LEN / sizeof (short) ; while (len > 0) - { readcount = (len >= bufferlen) ? bufferlen : len ; + { readcount = (len >= bufferlen) ? bufferlen : (int) len ; count = nms_adpcm_read_block (psf, pnms, sptr, readcount) ; for (k = 0 ; k < readcount ; k++) ptr [total + k] = normfact * (double) (sptr [k]) ; @@ -875,7 +875,7 @@ psf_nms_adpcm_encode_block (SF_PRIVATE *psf, NMS_ADPCM_PRIVATE *pnms) endswap_short_array ((signed short *) pnms->block, pnms->shortsperblock) ; /* Write the block to disk. */ - if ((k = psf_fwrite (pnms->block, sizeof (short), pnms->shortsperblock, psf)) != pnms->shortsperblock) + if ((k = (int) psf_fwrite (pnms->block, sizeof (short), pnms->shortsperblock, psf)) != pnms->shortsperblock) psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, pnms->shortsperblock) ; pnms->sample_curr = 0 ; @@ -945,7 +945,7 @@ nms_adpcm_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = SF_BUFFER_LEN / sizeof (short) ; while (len > 0) - { writecount = (len >= bufferlen) ? bufferlen : len ; + { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) sptr [k] = ptr [total + k] >> 16 ; count = nms_adpcm_write_block (psf, pnms, sptr, writecount) ; @@ -976,7 +976,7 @@ nms_adpcm_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = SF_BUFFER_LEN / sizeof (short) ; while (len > 0) - { writecount = (len >= bufferlen) ? bufferlen : len ; + { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) sptr [k] = psf_lrintf (normfact * ptr [total + k]) ; count = nms_adpcm_write_block (psf, pnms, sptr, writecount) ; @@ -1008,7 +1008,7 @@ nms_adpcm_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) sptr = ubuf.sbuf ; bufferlen = SF_BUFFER_LEN / sizeof (short) ; while (len > 0) - { writecount = (len >= bufferlen) ? bufferlen : len ; + { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) sptr [k] = psf_lrint (normfact * ptr [total + k]) ; count = nms_adpcm_write_block (psf, pnms, sptr, writecount) ; diff --git a/src/paf.c b/src/paf.c index 459bc469..871931c6 100644 --- a/src/paf.c +++ b/src/paf.c @@ -493,7 +493,7 @@ paf24_read_block (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24) } ; /* Read the block. */ - if ((k = psf_fread (ppaf24->block, 1, ppaf24->blocksize, psf)) != ppaf24->blocksize) + if ((k = (int) psf_fread (ppaf24->block, 1, ppaf24->blocksize, psf)) != ppaf24->blocksize) psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, ppaf24->blocksize) ; /* Do endian swapping if necessary. */ @@ -549,7 +549,7 @@ paf24_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) iptr = ubuf.ibuf ; bufferlen = ARRAY_LEN (ubuf.ibuf) ; while (len > 0) - { readcount = (len >= bufferlen) ? bufferlen : len ; + { readcount = (len >= bufferlen) ? bufferlen : (int) len ; count = paf24_read (psf, ppaf24, iptr, readcount) ; for (k = 0 ; k < readcount ; k++) ptr [total + k] = iptr [k] >> 16 ; @@ -591,7 +591,7 @@ paf24_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) iptr = ubuf.ibuf ; bufferlen = ARRAY_LEN (ubuf.ibuf) ; while (len > 0) - { readcount = (len >= bufferlen) ? bufferlen : len ; + { readcount = (len >= bufferlen) ? bufferlen : (int) len ; count = paf24_read (psf, ppaf24, iptr, readcount) ; for (k = 0 ; k < readcount ; k++) ptr [total + k] = normfact * iptr [k] ; @@ -619,7 +619,7 @@ paf24_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) iptr = ubuf.ibuf ; bufferlen = ARRAY_LEN (ubuf.ibuf) ; while (len > 0) - { readcount = (len >= bufferlen) ? bufferlen : len ; + { readcount = (len >= bufferlen) ? bufferlen : (int) len ; count = paf24_read (psf, ppaf24, iptr, readcount) ; for (k = 0 ; k < readcount ; k++) ptr [total + k] = normfact * iptr [k] ; @@ -668,7 +668,7 @@ paf24_write_block (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24) } ; /* Write block to disk. */ - if ((k = psf_fwrite (ppaf24->block, 1, ppaf24->blocksize, psf)) != ppaf24->blocksize) + if ((k = (int) psf_fwrite (ppaf24->block, 1, ppaf24->blocksize, psf)) != ppaf24->blocksize) psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, ppaf24->blocksize) ; if (ppaf24->sample_count < ppaf24->write_block * PAF24_SAMPLES_PER_BLOCK + ppaf24->write_count) @@ -718,7 +718,7 @@ paf24_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len) iptr = ubuf.ibuf ; bufferlen = ARRAY_LEN (ubuf.ibuf) ; while (len > 0) - { writecount = (len >= bufferlen) ? bufferlen : len ; + { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) iptr [k] = ptr [total + k] << 16 ; count = paf24_write (psf, ppaf24, iptr, writecount) ; @@ -772,7 +772,7 @@ paf24_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) iptr = ubuf.ibuf ; bufferlen = ARRAY_LEN (ubuf.ibuf) ; while (len > 0) - { writecount = (len >= bufferlen) ? bufferlen : len ; + { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) iptr [k] = psf_lrintf (normfact * ptr [total + k]) ; count = paf24_write (psf, ppaf24, iptr, writecount) ; @@ -803,7 +803,7 @@ paf24_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) iptr = ubuf.ibuf ; bufferlen = ARRAY_LEN (ubuf.ibuf) ; while (len > 0) - { writecount = (len >= bufferlen) ? bufferlen : len ; + { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) iptr [k] = psf_lrint (normfact * ptr [total+k]) ; count = paf24_write (psf, ppaf24, iptr, writecount) ; diff --git a/src/pcm.c b/src/pcm.c index 14990cdc..7e05219c 100644 --- a/src/pcm.c +++ b/src/pcm.c @@ -672,7 +672,7 @@ pcm_read_sc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; sc2s_array (ubuf.scbuf, readcount, ptr + total) ; total += readcount ; if (readcount < bufferlen) @@ -694,7 +694,7 @@ pcm_read_uc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ucbuf, sizeof (unsigned char), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ucbuf, sizeof (unsigned char), bufferlen, psf) ; uc2s_array (ubuf.ucbuf, readcount, ptr + total) ; total += readcount ; if (readcount < bufferlen) @@ -709,7 +709,7 @@ static sf_count_t pcm_read_bes2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) { int total ; - total = psf_fread (ptr, sizeof (short), len, psf) ; + total = (int) psf_fread (ptr, sizeof (short), len, psf) ; if (CPU_IS_LITTLE_ENDIAN) endswap_short_array (ptr, len) ; @@ -738,7 +738,7 @@ pcm_read_bet2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; bet2s_array ((tribyte*) (ubuf.ucbuf), readcount, ptr + total) ; total += readcount ; if (readcount < bufferlen) @@ -760,7 +760,7 @@ pcm_read_let2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; let2s_array ((tribyte*) (ubuf.ucbuf), readcount, ptr + total) ; total += readcount ; if (readcount < bufferlen) @@ -782,7 +782,7 @@ pcm_read_bei2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ibuf, sizeof (int), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ibuf, sizeof (int), bufferlen, psf) ; bei2s_array (ubuf.ibuf, readcount, ptr + total) ; total += readcount ; if (readcount < bufferlen) @@ -804,7 +804,7 @@ pcm_read_lei2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ibuf, sizeof (int), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ibuf, sizeof (int), bufferlen, psf) ; lei2s_array (ubuf.ibuf, readcount, ptr + total) ; total += readcount ; if (readcount < bufferlen) @@ -829,7 +829,7 @@ pcm_read_sc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; sc2i_array (ubuf.scbuf, readcount, ptr + total) ; total += readcount ; if (readcount < bufferlen) @@ -851,7 +851,7 @@ pcm_read_uc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ucbuf, sizeof (unsigned char), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ucbuf, sizeof (unsigned char), bufferlen, psf) ; uc2i_array (ubuf.ucbuf, readcount, ptr + total) ; total += readcount ; if (readcount < bufferlen) @@ -873,7 +873,7 @@ pcm_read_bes2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ; bes2i_array (ubuf.sbuf, readcount, ptr + total) ; total += readcount ; if (readcount < bufferlen) @@ -895,7 +895,7 @@ pcm_read_les2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ; les2i_array (ubuf.sbuf, readcount, ptr + total) ; total += readcount ; if (readcount < bufferlen) @@ -917,7 +917,7 @@ pcm_read_bet2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; bet2i_array ((tribyte*) (ubuf.ucbuf), readcount, ptr + total) ; total += readcount ; if (readcount < bufferlen) @@ -939,7 +939,7 @@ pcm_read_let2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; let2i_array ((tribyte*) (ubuf.ucbuf), readcount, ptr + total) ; total += readcount ; if (readcount < bufferlen) @@ -989,7 +989,7 @@ pcm_read_sc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; sc2f_array (ubuf.scbuf, readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -1014,7 +1014,7 @@ pcm_read_uc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ucbuf, sizeof (unsigned char), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ucbuf, sizeof (unsigned char), bufferlen, psf) ; uc2f_array (ubuf.ucbuf, readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -1039,7 +1039,7 @@ pcm_read_bes2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ; bes2f_array (ubuf.sbuf, readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -1064,7 +1064,7 @@ pcm_read_les2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ; les2f_array (ubuf.sbuf, readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -1090,7 +1090,7 @@ pcm_read_bet2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; bet2f_array ((tribyte*) (ubuf.ucbuf), readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -1116,7 +1116,7 @@ pcm_read_let2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; let2f_array ((tribyte*) (ubuf.ucbuf), readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -1141,7 +1141,7 @@ pcm_read_bei2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ibuf, sizeof (int), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ibuf, sizeof (int), bufferlen, psf) ; bei2f_array (ubuf.ibuf, readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -1166,7 +1166,7 @@ pcm_read_lei2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ibuf, sizeof (int), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ibuf, sizeof (int), bufferlen, psf) ; lei2f_array (ubuf.ibuf, readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -1194,7 +1194,7 @@ pcm_read_sc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; sc2d_array (ubuf.scbuf, readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -1219,7 +1219,7 @@ pcm_read_uc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ucbuf, sizeof (unsigned char), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ucbuf, sizeof (unsigned char), bufferlen, psf) ; uc2d_array (ubuf.ucbuf, readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -1244,7 +1244,7 @@ pcm_read_bes2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ; bes2d_array (ubuf.sbuf, readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -1269,7 +1269,7 @@ pcm_read_les2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ; les2d_array (ubuf.sbuf, readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -1294,7 +1294,7 @@ pcm_read_bet2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; bet2d_array ((tribyte*) (ubuf.ucbuf), readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -1320,7 +1320,7 @@ pcm_read_let2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; let2d_array ((tribyte*) (ubuf.ucbuf), readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -1345,7 +1345,7 @@ pcm_read_bei2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ibuf, sizeof (int), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ibuf, sizeof (int), bufferlen, psf) ; bei2d_array (ubuf.ibuf, readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -1370,7 +1370,7 @@ pcm_read_lei2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ibuf, sizeof (int), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ibuf, sizeof (int), bufferlen, psf) ; lei2d_array (ubuf.ibuf, readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -1398,7 +1398,7 @@ pcm_write_s2sc (SF_PRIVATE *psf, const short *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; s2sc_array (ptr + total, ubuf.scbuf, bufferlen) ; - writecount = psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -1420,7 +1420,7 @@ pcm_write_s2uc (SF_PRIVATE *psf, const short *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; s2uc_array (ptr + total, ubuf.ucbuf, bufferlen) ; - writecount = psf_fwrite (ubuf.ucbuf, sizeof (unsigned char), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ucbuf, sizeof (unsigned char), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -1446,7 +1446,7 @@ pcm_write_s2bes (SF_PRIVATE *psf, const short *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; endswap_short_copy (ubuf.sbuf, ptr + total, bufferlen) ; - writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -1471,7 +1471,7 @@ pcm_write_s2les (SF_PRIVATE *psf, const short *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; endswap_short_copy (ubuf.sbuf, ptr + total, bufferlen) ; - writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -1493,7 +1493,7 @@ pcm_write_s2bet (SF_PRIVATE *psf, const short *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; s2bet_array (ptr + total, (tribyte*) (ubuf.ucbuf), bufferlen) ; - writecount = psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -1515,7 +1515,7 @@ pcm_write_s2let (SF_PRIVATE *psf, const short *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; s2let_array (ptr + total, (tribyte*) (ubuf.ucbuf), bufferlen) ; - writecount = psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -1537,7 +1537,7 @@ pcm_write_s2bei (SF_PRIVATE *psf, const short *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; s2bei_array (ptr + total, ubuf.ibuf, bufferlen) ; - writecount = psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -1559,7 +1559,7 @@ pcm_write_s2lei (SF_PRIVATE *psf, const short *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; s2lei_array (ptr + total, ubuf.ibuf, bufferlen) ; - writecount = psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -1584,7 +1584,7 @@ pcm_write_i2sc (SF_PRIVATE *psf, const int *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; i2sc_array (ptr + total, ubuf.scbuf, bufferlen) ; - writecount = psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -1606,7 +1606,7 @@ pcm_write_i2uc (SF_PRIVATE *psf, const int *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; i2uc_array (ptr + total, ubuf.ucbuf, bufferlen) ; - writecount = psf_fwrite (ubuf.ucbuf, sizeof (signed char), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ucbuf, sizeof (signed char), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -1628,7 +1628,7 @@ pcm_write_i2bes (SF_PRIVATE *psf, const int *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; i2bes_array (ptr + total, ubuf.sbuf, bufferlen) ; - writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -1650,7 +1650,7 @@ pcm_write_i2les (SF_PRIVATE *psf, const int *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; i2les_array (ptr + total, ubuf.sbuf, bufferlen) ; - writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -1672,7 +1672,7 @@ pcm_write_i2bet (SF_PRIVATE *psf, const int *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; i2bet_array (ptr + total, (tribyte*) (ubuf.ucbuf), bufferlen) ; - writecount = psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -1694,7 +1694,7 @@ pcm_write_i2let (SF_PRIVATE *psf, const int *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; i2let_array (ptr + total, (tribyte*) (ubuf.ucbuf), bufferlen) ; - writecount = psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -1719,7 +1719,7 @@ pcm_write_i2bei (SF_PRIVATE *psf, const int *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; endswap_int_copy (ubuf.ibuf, ptr + total, bufferlen) ; - writecount = psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -1744,7 +1744,7 @@ pcm_write_i2lei (SF_PRIVATE *psf, const int *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; endswap_int_copy (ubuf.ibuf, ptr + total, bufferlen) ; - writecount = psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -1809,7 +1809,7 @@ pcm_write_f2sc (SF_PRIVATE *psf, const float *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; convert (ptr + total, ubuf.scbuf, bufferlen, psf->norm_float) ; - writecount = psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -1872,7 +1872,7 @@ pcm_write_f2uc (SF_PRIVATE *psf, const float *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; convert (ptr + total, ubuf.ucbuf, bufferlen, psf->norm_float) ; - writecount = psf_fwrite (ubuf.ucbuf, sizeof (unsigned char), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ucbuf, sizeof (unsigned char), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -1949,7 +1949,7 @@ pcm_write_f2bes (SF_PRIVATE *psf, const float *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; convert (ptr + total, ubuf.sbuf, bufferlen, psf->norm_float) ; - writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -2026,7 +2026,7 @@ pcm_write_f2les (SF_PRIVATE *psf, const float *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; convert (ptr + total, ubuf.sbuf, bufferlen, psf->norm_float) ; - writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -2101,7 +2101,7 @@ pcm_write_f2let (SF_PRIVATE *psf, const float *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; convert (ptr + total, (tribyte*) (ubuf.ucbuf), bufferlen, psf->norm_float) ; - writecount = psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -2176,7 +2176,7 @@ pcm_write_f2bet (SF_PRIVATE *psf, const float *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; convert (ptr + total, (tribyte*) (ubuf.ucbuf), bufferlen, psf->norm_float) ; - writecount = psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -2260,7 +2260,7 @@ pcm_write_f2bei (SF_PRIVATE *psf, const float *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; convert (ptr + total, ubuf.ibuf, bufferlen, psf->norm_float) ; - writecount = psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -2345,7 +2345,7 @@ pcm_write_f2lei (SF_PRIVATE *psf, const float *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; convert (ptr + total, ubuf.ibuf, bufferlen, psf->norm_float) ; - writecount = psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -2408,7 +2408,7 @@ pcm_write_d2sc (SF_PRIVATE *psf, const double *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; convert (ptr + total, ubuf.scbuf, bufferlen, psf->norm_double) ; - writecount = psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -2471,7 +2471,7 @@ pcm_write_d2uc (SF_PRIVATE *psf, const double *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; convert (ptr + total, ubuf.ucbuf, bufferlen, psf->norm_double) ; - writecount = psf_fwrite (ubuf.ucbuf, sizeof (unsigned char), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ucbuf, sizeof (unsigned char), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -2548,7 +2548,7 @@ pcm_write_d2bes (SF_PRIVATE *psf, const double *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; convert (ptr + total, ubuf.sbuf, bufferlen, psf->norm_double) ; - writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -2625,7 +2625,7 @@ pcm_write_d2les (SF_PRIVATE *psf, const double *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; convert (ptr + total, ubuf.sbuf, bufferlen, psf->norm_double) ; - writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -2700,7 +2700,7 @@ pcm_write_d2let (SF_PRIVATE *psf, const double *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; convert (ptr + total, (tribyte*) (ubuf.ucbuf), bufferlen, psf->norm_double) ; - writecount = psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -2775,7 +2775,7 @@ pcm_write_d2bet (SF_PRIVATE *psf, const double *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; convert (ptr + total, (tribyte*) (ubuf.ucbuf), bufferlen, psf->norm_double) ; - writecount = psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -2860,7 +2860,7 @@ pcm_write_d2bei (SF_PRIVATE *psf, const double *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; convert (ptr + total, ubuf.ibuf, bufferlen, psf->norm_double) ; - writecount = psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -2945,7 +2945,7 @@ pcm_write_d2lei (SF_PRIVATE *psf, const double *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; convert (ptr + total, ubuf.ibuf, bufferlen, psf->norm_double) ; - writecount = psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; diff --git a/src/sds.c b/src/sds.c index 85c8c11f..6bc76171 100644 --- a/src/sds.c +++ b/src/sds.c @@ -281,7 +281,7 @@ sds_read_header (SF_PRIVATE *psf, SDS_PRIVATE *psds) for (blockcount = 0 ; bytesread < psf->filelength ; blockcount++) { - bytesread += psf_fread (&marker, 1, 2, psf) ; + bytesread += (int) psf_fread (&marker, 1, 2, psf) ; if (marker == 0) break ; @@ -431,7 +431,7 @@ sds_2byte_read (SF_PRIVATE *psf, SDS_PRIVATE *psds) return 1 ; } ; - if ((k = psf_fread (psds->read_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE) + if ((k = (int) psf_fread (psds->read_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE) psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, SDS_BLOCK_SIZE) ; if (psds->read_data [0] != 0xF0) @@ -475,7 +475,7 @@ sds_3byte_read (SF_PRIVATE *psf, SDS_PRIVATE *psds) return 1 ; } ; - if ((k = psf_fread (psds->read_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE) + if ((k = (int) psf_fread (psds->read_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE) psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, SDS_BLOCK_SIZE) ; if (psds->read_data [0] != 0xF0) @@ -519,7 +519,7 @@ sds_4byte_read (SF_PRIVATE *psf, SDS_PRIVATE *psds) return 1 ; } ; - if ((k = psf_fread (psds->read_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE) + if ((k = (int) psf_fread (psds->read_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE) psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, SDS_BLOCK_SIZE) ; if (psds->read_data [0] != 0xF0) @@ -565,7 +565,7 @@ sds_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) iptr = ubuf.ibuf ; bufferlen = ARRAY_LEN (ubuf.ibuf) ; while (len > 0) - { readcount = (len >= bufferlen) ? bufferlen : len ; + { readcount = (len >= bufferlen) ? bufferlen : (int) len ; count = sds_read (psf, psds, iptr, readcount) ; for (k = 0 ; k < readcount ; k++) ptr [total + k] = iptr [k] >> 16 ; @@ -611,7 +611,7 @@ sds_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) iptr = ubuf.ibuf ; bufferlen = ARRAY_LEN (ubuf.ibuf) ; while (len > 0) - { readcount = (len >= bufferlen) ? bufferlen : len ; + { readcount = (len >= bufferlen) ? bufferlen : (int) len ; count = sds_read (psf, psds, iptr, readcount) ; for (k = 0 ; k < readcount ; k++) ptr [total + k] = normfact * iptr [k] ; @@ -643,7 +643,7 @@ sds_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) iptr = ubuf.ibuf ; bufferlen = ARRAY_LEN (ubuf.ibuf) ; while (len > 0) - { readcount = (len >= bufferlen) ? bufferlen : len ; + { readcount = (len >= bufferlen) ? bufferlen : (int) len ; count = sds_read (psf, psds, iptr, readcount) ; for (k = 0 ; k < readcount ; k++) ptr [total + k] = normfact * iptr [k] ; @@ -794,7 +794,7 @@ sds_2byte_write (SF_PRIVATE *psf, SDS_PRIVATE *psds) psds->write_data [SDS_BLOCK_SIZE - 2] = checksum ; psds->write_data [SDS_BLOCK_SIZE - 1] = 0xF7 ; - if ((k = psf_fwrite (psds->write_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE) + if ((k = (int) psf_fwrite (psds->write_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE) psf_log_printf (psf, "*** Warning : psf_fwrite (%d != %d).\n", k, SDS_BLOCK_SIZE) ; psds->write_block ++ ; @@ -836,7 +836,7 @@ sds_3byte_write (SF_PRIVATE *psf, SDS_PRIVATE *psds) psds->write_data [SDS_BLOCK_SIZE - 2] = checksum ; psds->write_data [SDS_BLOCK_SIZE - 1] = 0xF7 ; - if ((k = psf_fwrite (psds->write_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE) + if ((k = (int) psf_fwrite (psds->write_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE) psf_log_printf (psf, "*** Warning : psf_fwrite (%d != %d).\n", k, SDS_BLOCK_SIZE) ; psds->write_block ++ ; @@ -879,7 +879,7 @@ sds_4byte_write (SF_PRIVATE *psf, SDS_PRIVATE *psds) psds->write_data [SDS_BLOCK_SIZE - 2] = checksum ; psds->write_data [SDS_BLOCK_SIZE - 1] = 0xF7 ; - if ((k = psf_fwrite (psds->write_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE) + if ((k = (int) psf_fwrite (psds->write_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE) psf_log_printf (psf, "*** Warning : psf_fwrite (%d != %d).\n", k, SDS_BLOCK_SIZE) ; psds->write_block ++ ; @@ -908,7 +908,7 @@ sds_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len) iptr = ubuf.ibuf ; bufferlen = ARRAY_LEN (ubuf.ibuf) ; while (len > 0) - { writecount = (len >= bufferlen) ? bufferlen : len ; + { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) iptr [k] = arith_shift_left (ptr [total + k], 16) ; count = sds_write (psf, psds, iptr, writecount) ; @@ -956,7 +956,7 @@ sds_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) iptr = ubuf.ibuf ; bufferlen = ARRAY_LEN (ubuf.ibuf) ; while (len > 0) - { writecount = (len >= bufferlen) ? bufferlen : len ; + { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) iptr [k] = normfact * ptr [total + k] ; count = sds_write (psf, psds, iptr, writecount) ; @@ -989,7 +989,7 @@ sds_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) iptr = ubuf.ibuf ; bufferlen = ARRAY_LEN (ubuf.ibuf) ; while (len > 0) - { writecount = (len >= bufferlen) ? bufferlen : len ; + { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) iptr [k] = normfact * ptr [total + k] ; count = sds_write (psf, psds, iptr, writecount) ; diff --git a/src/ulaw.c b/src/ulaw.c index 744e3541..69d38bd8 100644 --- a/src/ulaw.c +++ b/src/ulaw.c @@ -873,7 +873,7 @@ ulaw_read_ulaw2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ucbuf, 1, bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ucbuf, 1, bufferlen, psf) ; ulaw2s_array (ubuf.ucbuf, readcount, ptr + total) ; total += readcount ; if (readcount < bufferlen) @@ -895,7 +895,7 @@ ulaw_read_ulaw2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ucbuf, 1, bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ucbuf, 1, bufferlen, psf) ; ulaw2i_array (ubuf.ucbuf, readcount, ptr + total) ; total += readcount ; if (readcount < bufferlen) @@ -920,7 +920,7 @@ ulaw_read_ulaw2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ucbuf, 1, bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ucbuf, 1, bufferlen, psf) ; ulaw2f_array (ubuf.ucbuf, readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -944,7 +944,7 @@ ulaw_read_ulaw2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.ucbuf, 1, bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.ucbuf, 1, bufferlen, psf) ; ulaw2d_array (ubuf.ucbuf, readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -970,7 +970,7 @@ ulaw_write_s2ulaw (SF_PRIVATE *psf, const short *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; s2ulaw_array (ptr + total, bufferlen, ubuf.ucbuf) ; - writecount = psf_fwrite (ubuf.ucbuf, 1, bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ucbuf, 1, bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -992,7 +992,7 @@ ulaw_write_i2ulaw (SF_PRIVATE *psf, const int *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; i2ulaw_array (ptr + total, bufferlen, ubuf.ucbuf) ; - writecount = psf_fwrite (ubuf.ucbuf, 1, bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ucbuf, 1, bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -1018,7 +1018,7 @@ ulaw_write_f2ulaw (SF_PRIVATE *psf, const float *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; f2ulaw_array (ptr + total, bufferlen, ubuf.ucbuf, normfact) ; - writecount = psf_fwrite (ubuf.ucbuf, 1, bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ucbuf, 1, bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -1044,7 +1044,7 @@ ulaw_write_d2ulaw (SF_PRIVATE *psf, const double *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; d2ulaw_array (ptr + total, bufferlen, ubuf.ucbuf, normfact) ; - writecount = psf_fwrite (ubuf.ucbuf, 1, bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.ucbuf, 1, bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; diff --git a/src/vox_adpcm.c b/src/vox_adpcm.c index 50c3d05d..e206675c 100644 --- a/src/vox_adpcm.c +++ b/src/vox_adpcm.c @@ -128,7 +128,7 @@ vox_read_block (SF_PRIVATE *psf, IMA_OKI_ADPCM *pvox, short *ptr, int len) while (indx < len) { pvox->code_count = (len - indx > IMA_OKI_ADPCM_PCM_LEN) ? IMA_OKI_ADPCM_CODE_LEN : (len - indx + 1) / 2 ; - if ((k = psf_fread (pvox->codes, 1, pvox->code_count, psf)) != pvox->code_count) + if ((k = (int) psf_fread (pvox->codes, 1, pvox->code_count, psf)) != pvox->code_count) { if (psf_ftell (psf) != psf->filelength) psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pvox->code_count) ; if (k == 0) @@ -275,7 +275,7 @@ vox_write_block (SF_PRIVATE *psf, IMA_OKI_ADPCM *pvox, const short *ptr, int len ima_oki_adpcm_encode_block (pvox) ; - if ((k = psf_fwrite (pvox->codes, 1, pvox->code_count, psf)) != pvox->code_count) + if ((k = (int) psf_fwrite (pvox->codes, 1, pvox->code_count, psf)) != pvox->code_count) psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, pvox->code_count) ; indx += pvox->pcm_count ; diff --git a/src/w64.c b/src/w64.c index 426480b6..14346cd2 100644 --- a/src/w64.c +++ b/src/w64.c @@ -241,7 +241,9 @@ w64_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock) psf_binheader_readf (psf, "j", 8 - (psf->header.indx & 0x7)) ; /* Generate hash of 16 byte marker. */ - marker = chunk_size = 0 ; + marker = 0 ; + chunk_size = 0 ; + bytesread = psf_binheader_readf (psf, "eh8", &marker, &chunk_size) ; if (bytesread == 0) break ; diff --git a/src/xi.c b/src/xi.c index c102060f..72b4adaa 100644 --- a/src/xi.c +++ b/src/xi.c @@ -252,7 +252,7 @@ dpcm_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) bufferlen = ARRAY_LEN (ubuf.sbuf) ; while (total > 0) { len = (total > bufferlen) ? bufferlen : total ; - total -= dpcm_read_dles2s (psf, ubuf.sbuf, len) ; + total -= (int) dpcm_read_dles2s (psf, ubuf.sbuf, len) ; } ; } else @@ -260,7 +260,7 @@ dpcm_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) bufferlen = ARRAY_LEN (ubuf.sbuf) ; while (total > 0) { len = (total > bufferlen) ? bufferlen : total ; - total -= dpcm_read_dsc2s (psf, ubuf.sbuf, len) ; + total -= (int) dpcm_read_dsc2s (psf, ubuf.sbuf, len) ; } ; } ; @@ -522,7 +522,7 @@ dpcm_read_dsc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; dsc2s_array (pxi, ubuf.scbuf, readcount, ptr + total) ; total += readcount ; if (readcount < bufferlen) @@ -548,7 +548,7 @@ dpcm_read_dsc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; dsc2i_array (pxi, ubuf.scbuf, readcount, ptr + total) ; total += readcount ; if (readcount < bufferlen) @@ -577,7 +577,7 @@ dpcm_read_dsc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; dsc2f_array (pxi, ubuf.scbuf, readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -606,7 +606,7 @@ dpcm_read_dsc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; dsc2d_array (pxi, ubuf.scbuf, readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -635,7 +635,7 @@ dpcm_read_dles2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ; dles2s_array (pxi, ubuf.sbuf, readcount, ptr + total) ; total += readcount ; if (readcount < bufferlen) @@ -661,7 +661,7 @@ dpcm_read_dles2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ; dles2i_array (pxi, ubuf.sbuf, readcount, ptr + total) ; total += readcount ; if (readcount < bufferlen) @@ -690,7 +690,7 @@ dpcm_read_dles2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ; dles2f_array (pxi, ubuf.sbuf, readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -719,7 +719,7 @@ dpcm_read_dles2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) while (len > 0) { if (len < bufferlen) bufferlen = (int) len ; - readcount = psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ; + readcount = (int) psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ; dles2d_array (pxi, ubuf.sbuf, readcount, ptr + total, normfact) ; total += readcount ; if (readcount < bufferlen) @@ -760,7 +760,7 @@ dpcm_write_s2dsc (SF_PRIVATE *psf, const short *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; s2dsc_array (pxi, ptr + total, ubuf.scbuf, bufferlen) ; - writecount = psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -786,7 +786,7 @@ dpcm_write_i2dsc (SF_PRIVATE *psf, const int *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; i2dsc_array (pxi, ptr + total, ubuf.scbuf, bufferlen) ; - writecount = psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -815,7 +815,7 @@ dpcm_write_f2dsc (SF_PRIVATE *psf, const float *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; f2dsc_array (pxi, ptr + total, ubuf.scbuf, bufferlen, normfact) ; - writecount = psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -844,7 +844,7 @@ dpcm_write_d2dsc (SF_PRIVATE *psf, const double *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; d2dsc_array (pxi, ptr + total, ubuf.scbuf, bufferlen, normfact) ; - writecount = psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -871,7 +871,7 @@ dpcm_write_s2dles (SF_PRIVATE *psf, const short *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; s2dles_array (pxi, ptr + total, ubuf.sbuf, bufferlen) ; - writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -897,7 +897,7 @@ dpcm_write_i2dles (SF_PRIVATE *psf, const int *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; i2dles_array (pxi, ptr + total, ubuf.sbuf, bufferlen) ; - writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -926,7 +926,7 @@ dpcm_write_f2dles (SF_PRIVATE *psf, const float *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; f2dles_array (pxi, ptr + total, ubuf.sbuf, bufferlen, normfact) ; - writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; @@ -955,7 +955,7 @@ dpcm_write_d2dles (SF_PRIVATE *psf, const double *ptr, sf_count_t len) { if (len < bufferlen) bufferlen = (int) len ; d2dles_array (pxi, ptr + total, ubuf.sbuf, bufferlen, normfact) ; - writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; + writecount = (int) psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ; total += writecount ; if (writecount < bufferlen) break ; diff --git a/tests/chunk_test.c b/tests/chunk_test.c index ed340643..0e358e81 100644 --- a/tests/chunk_test.c +++ b/tests/chunk_test.c @@ -38,8 +38,8 @@ #define LOG_BUFFER_SIZE 1024 static void chunk_test (const char *filename, int format) ; -static void wav_subchunk_test (size_t chunk_size) ; -static void large_free_test (const char *filename, int format, size_t chunk_size) ; +static void wav_subchunk_test (unsigned int chunk_size) ; +static void large_free_test (const char *filename, int format, unsigned int chunk_size) ; int main (int argc, char *argv []) @@ -133,7 +133,7 @@ chunk_test_helper (const char *filename, int format, const char * testdata) snprintf (chunk_info.id, sizeof (chunk_info.id), "Test") ; chunk_info.id_size = 4 ; chunk_info.data = strdup (testdata) ; - chunk_info.datalen = strlen (chunk_info.data) ; + chunk_info.datalen = (unsigned int) strlen (chunk_info.data) ; length_before = chunk_info.datalen ; @@ -224,7 +224,7 @@ multichunk_test_helper (const char *filename, int format, const char * testdata chunk_info.id_size = 4 ; chunk_info.data = strdup (testdata [i]) ; - chunk_info.datalen = strlen (chunk_info.data) ; + chunk_info.datalen = (unsigned int) strlen (chunk_info.data) ; length_before [i] = chunk_info.datalen ; @@ -316,7 +316,7 @@ chunk_test (const char *filename, int format) static void -wav_subchunk_test (size_t chunk_size) +wav_subchunk_test (unsigned int chunk_size) { SNDFILE * file ; SF_INFO sfinfo ; SF_CHUNK_INFO chunk_info ; @@ -325,7 +325,7 @@ wav_subchunk_test (size_t chunk_size) short audio [16] ; int err, value ; - snprintf (filename, sizeof (filename), "subchunk_%04d.wav", (int) chunk_size) ; + snprintf (filename, sizeof (filename), "subchunk_%04u.wav", chunk_size) ; print_test_name (__func__, filename) ; exit_if_true (sizeof (chunk_data) < chunk_size, "\n\nLine %d : sizeof (data) < chunk_size\n\n", __LINE__) ; @@ -353,7 +353,7 @@ wav_subchunk_test (size_t chunk_size) snprintf (chunk_info.id, sizeof (chunk_info.id), "LIST") ; chunk_info.id_size = 4 ; chunk_info.data = chunk_data ; - chunk_info.datalen = chunk_size ; + chunk_info.datalen = (unsigned int) chunk_size ; err = sf_set_chunk (file, &chunk_info) ; exit_if_true ( @@ -386,7 +386,7 @@ wav_subchunk_test (size_t chunk_size) } /* wav_subchunk_test */ static void -large_free_test (const char *filename, int format, size_t chunk_size) +large_free_test (const char *filename, int format, unsigned int chunk_size) { SNDFILE * file ; SF_INFO sfinfo ; SF_CHUNK_INFO chunk_info ; diff --git a/tests/command_test.c b/tests/command_test.c index 8ca23b27..c2379201 100644 --- a/tests/command_test.c +++ b/tests/command_test.c @@ -239,9 +239,9 @@ float_norm_test (const char *filename) /* Create float_data with all values being less than 1.0. */ for (k = 0 ; k < BUFFER_LEN / 2 ; k++) - float_data [k] = (k + 5) / (2.0 * BUFFER_LEN) ; + float_data [k] = (k + 5) / (2.0f * BUFFER_LEN) ; for (k = BUFFER_LEN / 2 ; k < BUFFER_LEN ; k++) - float_data [k] = (k + 5) ; + float_data [k] = (float) (k + 5) ; if (! (file = sf_open (filename, SFM_WRITE, &sfinfo))) { printf ("Line %d: sf_open_write failed with error : ", __LINE__) ; @@ -252,7 +252,7 @@ float_norm_test (const char *filename) /* Normalisation is on by default so no need to do anything here. */ - if ((k = sf_write_float (file, float_data, BUFFER_LEN / 2)) != BUFFER_LEN / 2) + if ((k = (unsigned int) sf_write_float (file, float_data, BUFFER_LEN / 2)) != BUFFER_LEN / 2) { printf ("Line %d: sf_write_float failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ; exit (1) ; } ; @@ -260,7 +260,7 @@ float_norm_test (const char *filename) /* Turn normalisation off. */ sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ; - if ((k = sf_write_float (file, float_data + BUFFER_LEN / 2, BUFFER_LEN / 2)) != BUFFER_LEN / 2) + if ((k = (unsigned int) sf_write_float (file, float_data + BUFFER_LEN / 2, BUFFER_LEN / 2)) != BUFFER_LEN / 2) { printf ("Line %d: sf_write_float failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ; exit (1) ; } ; @@ -291,7 +291,7 @@ float_norm_test (const char *filename) } ; /* Read float_data and check that it is normalised (ie default). */ - if ((k = sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN) + if ((k = (unsigned int) sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN) { printf ("\n\nLine %d: sf_read_float failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ; exit (1) ; } ; @@ -306,7 +306,7 @@ float_norm_test (const char *filename) sf_seek (file, 0, SEEK_SET) ; sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ; - if ((k = sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN) + if ((k = (unsigned int) sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN) { printf ("\n\nLine %d: sf_read_float failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ; exit (1) ; } ; @@ -321,7 +321,7 @@ float_norm_test (const char *filename) sf_seek (file, 0, SEEK_SET) ; sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_TRUE) ; - if ((k = sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN) + if ((k = (unsigned int) sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN) { printf ("\n\nLine %d: sf_read_float failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ; exit (1) ; } ; @@ -369,7 +369,7 @@ double_norm_test (const char *filename) /* Normailsation is on by default so no need to do anything here. */ /*-sf_command (file, "set-norm-double", "true", 0) ;-*/ - if ((k = sf_write_double (file, double_data, BUFFER_LEN / 2)) != BUFFER_LEN / 2) + if ((k = (unsigned int) sf_write_double (file, double_data, BUFFER_LEN / 2)) != BUFFER_LEN / 2) { printf ("Line %d: sf_write_double failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ; exit (1) ; } ; @@ -377,7 +377,7 @@ double_norm_test (const char *filename) /* Turn normalisation off. */ sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ; - if ((k = sf_write_double (file, double_data + BUFFER_LEN / 2, BUFFER_LEN / 2)) != BUFFER_LEN / 2) + if ((k = (unsigned int) sf_write_double (file, double_data + BUFFER_LEN / 2, BUFFER_LEN / 2)) != BUFFER_LEN / 2) { printf ("Line %d: sf_write_double failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ; exit (1) ; } ; @@ -407,7 +407,7 @@ double_norm_test (const char *filename) } ; /* Read double_data and check that it is normalised (ie default). */ - if ((k = sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN) + if ((k = (unsigned int) sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN) { printf ("\n\nLine %d: sf_read_double failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ; exit (1) ; } ; @@ -422,7 +422,7 @@ double_norm_test (const char *filename) sf_seek (file, 0, SEEK_SET) ; sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ; - if ((k = sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN) + if ((k = (unsigned int) sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN) { printf ("\n\nLine %d: sf_read_double failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ; exit (1) ; } ; @@ -437,7 +437,7 @@ double_norm_test (const char *filename) sf_seek (file, 0, SEEK_SET) ; sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_TRUE) ; - if ((k = sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN) + if ((k = (unsigned int) sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN) { printf ("\n\nLine %d: sf_read_double failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ; exit (1) ; } ; @@ -1021,7 +1021,7 @@ cue_test_var (const char *filename, int filetype, int count) sfinfo.channels = 1 ; file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; - if (sf_command (file, SFC_SET_CUE, write_cue, cues_size) == SF_FALSE) + if (sf_command (file, SFC_SET_CUE, write_cue, (int) cues_size) == SF_FALSE) { printf ("\n\nLine %d : sf_command (SFC_SET_CUE) failed with %d cues, datasize %zu --> error: %s\n\n", __LINE__, count, cues_size, sf_strerror (file)) ; exit (1) ; } ; @@ -1032,7 +1032,7 @@ cue_test_var (const char *filename, int filetype, int count) file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; - if (sf_command (file, SFC_GET_CUE, read_cue, cues_size) == SF_FALSE) + if (sf_command (file, SFC_GET_CUE, read_cue, (int) cues_size) == SF_FALSE) { printf ("\n\nLine %d : sf_command (SFC_GET_CUE) failed with %d cues, datasize %zu --> error: %s\n\n", __LINE__, count, cues_size, sf_strerror (file)) ; exit (1) ; } ; @@ -1351,7 +1351,7 @@ broadcast_coding_history_test (const char *filename) exit (1) ; } ; - bc_write.coding_history_size = strlen (supplied_history) ; + bc_write.coding_history_size = (uint32_t) strlen (supplied_history) ; bc_write.coding_history_size = snprintf (bc_write.coding_history, sizeof (bc_write.coding_history), "%s", supplied_history) ; file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; @@ -1423,7 +1423,7 @@ broadcast_coding_history_size (const char *filename) for (k = 0 ; bc_write.coding_history_size < 512 ; k++) { snprintf (bc_write.coding_history + bc_write.coding_history_size, sizeof (bc_write.coding_history) - bc_write.coding_history_size, "line %4d\n", k) ; - bc_write.coding_history_size = strlen (bc_write.coding_history) ; + bc_write.coding_history_size = (uint32_t) strlen (bc_write.coding_history) ; } ; exit_if_true (bc_write.coding_history_size < 512, @@ -1493,7 +1493,7 @@ cart_test (const char *filename, int filetype) ca_write.level_reference = 42 ; snprintf (ca_write.url, sizeof (ca_write.url), "http://www.test.com/test_url") ; snprintf (ca_write.tag_text, sizeof (ca_write.tag_text), "tag text test! \r\n") ; // must be terminated \r\n to be valid - ca_write.tag_text_size = strlen (ca_write.tag_text) ; + ca_write.tag_text_size = (uint32_t) strlen (ca_write.tag_text) ; file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; if (sf_command (file, SFC_SET_CART_INFO, &ca_write, sizeof (ca_write)) == SF_FALSE) @@ -1652,7 +1652,7 @@ cart_rdwr_test (const char *filename, int filetype) cinfo.level_reference = 42 ; snprintf (cinfo.url, sizeof (cinfo.url), "http://www.test.com/test_url") ; snprintf (cinfo.tag_text, sizeof (cinfo.tag_text), "tag text test!\r\n") ; - cinfo.tag_text_size = strlen (cinfo.tag_text) ; + cinfo.tag_text_size = (uint32_t) strlen (cinfo.tag_text) ; file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ; frames = sfinfo.frames ; diff --git a/tests/compression_size_test.c b/tests/compression_size_test.c index 3e3059db..aceceed6 100644 --- a/tests/compression_size_test.c +++ b/tests/compression_size_test.c @@ -102,7 +102,7 @@ vorbis_test (void) sf_close (file) ; for (k = 0 ; k < ARRAY_LEN (float_data) ; k ++) - max_abs = max_float (max_abs, fabs (float_data [k])) ; + max_abs = max_float (max_abs, fabsf (float_data [k])) ; exit_if_true (max_abs > 1.023, "\n\nLine %d : max_abs %f should be < 1.023.\n\n", __LINE__, max_abs) ; @@ -160,8 +160,8 @@ compression_size_test (int format, const char * filename) sf_close (q3_file) ; sf_close (q6_file) ; - q3_size = file_length (q3_fname) ; - q6_size = file_length (q6_fname) ; + q3_size = (int) file_length (q3_fname) ; + q6_size = (int) file_length (q6_fname) ; exit_if_true (q3_size >= q6_size, "\n\nLine %d : q3 size (%d) >= q6 size (%d)\n\n", __LINE__, q3_size, q6_size) ; diff --git a/tests/dwvw_test.c b/tests/dwvw_test.c index b2cae067..0c92f874 100644 --- a/tests/dwvw_test.c +++ b/tests/dwvw_test.c @@ -86,7 +86,7 @@ dwvw_test (const char *filename, int format, int bit_width) file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; - if ((k = sf_read_int (file, read_buf, BUFFER_SIZE)) != BUFFER_SIZE) + if ((k = (int) sf_read_int (file, read_buf, BUFFER_SIZE)) != BUFFER_SIZE) { printf ("Error (line %d) : Only read %d/%d.frames.\n", __LINE__, k, BUFFER_SIZE) ; exit (1) ; } diff --git a/tests/generate.c b/tests/generate.c index 87efbb44..8ed6a6de 100644 --- a/tests/generate.c +++ b/tests/generate.c @@ -39,10 +39,10 @@ generate_file (const char * filename, int format, int len) output = calloc (len, sizeof (float)) ; - maxabs = crappy_snare (output, len, 0, 0.95, maxabs) ; - maxabs = crappy_snare (output, len, len / 4, 0.85, maxabs) ; - maxabs = crappy_snare (output, len, 2 * len / 4, 0.85, maxabs) ; - crappy_snare (output, len, 3 * len / 4, 0.85, maxabs) ; + maxabs = crappy_snare (output, len, 0, 0.95f, maxabs) ; + maxabs = crappy_snare (output, len, len / 4, 0.85f, maxabs) ; + maxabs = crappy_snare (output, len, 2 * len / 4, 0.85f, maxabs) ; + crappy_snare (output, len, 3 * len / 4, 0.85f, maxabs) ; write_mono_file (filename, format, 44100, output, len) ; @@ -51,24 +51,24 @@ generate_file (const char * filename, int format, int len) static inline float rand_float (void) -{ return rand () / (0.5 * RAND_MAX) - 1.0 ; +{ return rand () / (0.5f * RAND_MAX) - 1.0f ; } /* rand_float */ static float crappy_snare (float *output, int len, int offset, float gain, float maxabs) { int k ; - float env = 0.0 ; + float env = 0.0f ; for (k = offset ; k < len && env < gain ; k++) - { env += 0.03 ; + { env += 0.03f ; output [k] += env * rand_float () ; - maxabs = SF_MAX (maxabs, fabs (output [k])) ; + maxabs = SF_MAX (maxabs, fabsf (output [k])) ; } ; for ( ; k < len && env > 1e-8 ; k++) - { env *= 0.995 ; + { env *= 0.995f ; output [k] += env * rand_float () ; - maxabs = SF_MAX (maxabs, fabs (output [k])) ; + maxabs = SF_MAX (maxabs, fabsf (output [k])) ; } ; return maxabs ; diff --git a/tests/headerless_test.c b/tests/headerless_test.c index aeaad1ec..86aa6db9 100644 --- a/tests/headerless_test.c +++ b/tests/headerless_test.c @@ -74,7 +74,7 @@ headerless_test (const char * filename, int format, int expected) file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; - if ((k = sf_write_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE) + if ((k = (int) sf_write_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE) { printf ("Line %d: sf_write_short failed with short write (%d => %d).\n", __LINE__, BUFFER_SIZE, k) ; fflush (stdout) ; puts (sf_strerror (file)) ; @@ -135,7 +135,7 @@ old_test (void) file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; - if ((k = sf_write_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE) + if ((k = (int) sf_write_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE) { printf ("Line %d: sf_write_short failed with short write (%d => %d).\n", __LINE__, BUFFER_SIZE, k) ; fflush (stdout) ; puts (sf_strerror (file)) ; @@ -168,7 +168,7 @@ old_test (void) check_log_buffer_or_die (file, __LINE__) ; - if ((k = sf_read_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE) + if ((k = (int) sf_read_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE) { printf ("Line %d: short read (%d).\n", __LINE__, k) ; exit (1) ; } ; diff --git a/tests/long_read_write_test.c b/tests/long_read_write_test.c index 48624687..0629caef 100644 --- a/tests/long_read_write_test.c +++ b/tests/long_read_write_test.c @@ -66,7 +66,7 @@ main (int argc, char *argv []) { int value = k / 32 ; int_data [k] = (value & 1 ? -1 : 1) * value ; short_data [k] = int_data [k] ; - float_data [k] = int_data [k] / 32000.0 ; + float_data [k] = int_data [k] / 32000.0f ; double_data [k] = int_data [k] / 32000.0 ; } diff --git a/tests/lossy_comp_test.c b/tests/lossy_comp_test.c index bdd65b2e..92803213 100644 --- a/tests/lossy_comp_test.c +++ b/tests/lossy_comp_test.c @@ -661,7 +661,7 @@ lcomp_test_short (const char *filename, int filetype, int channels, double margi data = data_buffer.s ; orig = orig_buffer.s ; - gen_signal_double (orig_buffer.d, 32000.0, channels, datalen) ; + gen_signal_double (orig_buffer.d, 32000.0, channels, (int) datalen) ; for (k = 0 ; k < channels * datalen ; k++) orig [k] = (short) (orig_buffer.d [k]) ; @@ -712,7 +712,7 @@ lcomp_test_short (const char *filename, int filetype, int channels, double margi for (k = 0 ; k < datalen ; k++) { if (error_function (data [k], orig [k], margin)) { printf ("\n\nLine %d: Incorrect sample A (#%d : %d should be %d).\n", __LINE__, k, data [k], orig [k]) ; - oct_save_short (orig, data, datalen) ; + oct_save_short (orig, data, (int) datalen) ; exit (1) ; } ; half_max_abs = LCT_MAX (half_max_abs, abs (data [k] / 2)) ; @@ -723,7 +723,7 @@ lcomp_test_short (const char *filename, int filetype, int channels, double margi exit (1) ; } ; - if ((k = sf_readf_short (file, data, datalen)) != sfinfo.frames - datalen) + if ((k = (int) sf_readf_short (file, data, datalen)) != sfinfo.frames - datalen) { printf ("\n\nLine %d: Incorrect read length (%" PRId64 " should be %d).\n", __LINE__, channels * sfinfo.frames - datalen, k) ; exit (1) ; @@ -747,7 +747,7 @@ lcomp_test_short (const char *filename, int filetype, int channels, double margi /* Now test sf_seek function. */ - if ((k = sf_seek (file, 0, SEEK_SET)) != 0) + if ((k = (int) sf_seek (file, 0, SEEK_SET)) != 0) { printf ("\n\nLine %d: Seek to start of file failed (%d).\n", __LINE__, k) ; exit (1) ; } ; @@ -768,7 +768,7 @@ lcomp_test_short (const char *filename, int filetype, int channels, double margi seekpos = BUFFER_SIZE / 10 ; /* Check seek from start of file. */ - if ((k = sf_seek (file, seekpos, SEEK_SET)) != seekpos) + if ((k = (int) sf_seek (file, seekpos, SEEK_SET)) != seekpos) { printf ("Seek to start of file + %d failed (%d).\n", seekpos, k) ; exit (1) ; } ; @@ -780,23 +780,23 @@ lcomp_test_short (const char *filename, int filetype, int channels, double margi exit (1) ; } ; - if ((k = sf_seek (file, 0, SEEK_CUR)) != seekpos + 1) + if ((k = (int) sf_seek (file, 0, SEEK_CUR)) != seekpos + 1) { printf ("\n\nLine %d: sf_seek (SEEK_CUR) with 0 offset failed (%d should be %d)\n", __LINE__, k, seekpos + 1) ; exit (1) ; } ; - seekpos = sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ; - k = sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ; + seekpos = (int) sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ; + k = (int) sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ; test_readf_short_or_die (file, 0, data, 1, __LINE__) ; if (error_function (1.0 * data [0], 1.0 * orig [seekpos * channels], margin) || k != seekpos) { printf ("\n\nLine %d: sf_seek (forwards, SEEK_CUR) followed by sf_readf_short failed (%d, %d) (%d, %d).\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos + 1) ; - oct_save_short (orig, data, datalen) ; + oct_save_short (orig, data, (int) datalen) ; exit (1) ; } ; - seekpos = sf_seek (file, 0, SEEK_CUR) - 20 ; + seekpos = (int) sf_seek (file, 0, SEEK_CUR) - 20 ; /* Check seek backward from current position. */ - k = sf_seek (file, -20, SEEK_CUR) ; + k = (int) sf_seek (file, -20, SEEK_CUR) ; test_readf_short_or_die (file, 0, data, 1, __LINE__) ; if (error_function (1.0 * data [0], 1.0 * orig [seekpos * channels], margin) || k != seekpos) { printf ("\nLine %d: sf_seek (backwards, SEEK_CUR) followed by sf_readf_short failed (%d, %d) (%d, %d).\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos) ; @@ -806,13 +806,13 @@ lcomp_test_short (const char *filename, int filetype, int channels, double margi /* Check that read past end of file returns number of items. */ sf_seek (file, sfinfo.frames, SEEK_SET) ; - if ((k = sf_readf_short (file, data, datalen)) != 0) + if ((k = (int) sf_readf_short (file, data, datalen)) != 0) { printf ("\n\nLine %d: Return value from sf_readf_short past end of file incorrect (%d).\n", __LINE__, k) ; exit (1) ; } ; /* Check seek backward from end. */ - if ((k = sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5) + if ((k = (int) sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5) { printf ("\n\nLine %d: sf_seek (SEEK_END) returned %d instead of %d.\n", __LINE__, k, 5) ; exit (1) ; } ; @@ -858,7 +858,7 @@ lcomp_test_int (const char *filename, int filetype, int channels, double margin) data = data_buffer.i ; orig = orig_buffer.i ; - gen_signal_double (orig_buffer.d, max_val, channels, datalen) ; + gen_signal_double (orig_buffer.d, max_val, channels, (int) datalen) ; for (k = 0 ; k < channels * datalen ; k++) orig [k] = lrint (orig_buffer.d [k]) ; @@ -910,7 +910,7 @@ lcomp_test_int (const char *filename, int filetype, int channels, double margin) for (k = 0 ; k < datalen ; k++) { if (error_function (data [k] / scale, orig [k] / scale, margin)) { printf ("\nLine %d: Incorrect sample (#%d : %f should be %f).\n", __LINE__, k, data [k] / scale, orig [k] / scale) ; - oct_save_int (orig, data, datalen) ; + oct_save_int (orig, data, (int) datalen) ; exit (1) ; } ; half_max_abs = LCT_MAX (half_max_abs, abs (data [k] / 2)) ; @@ -921,7 +921,7 @@ lcomp_test_int (const char *filename, int filetype, int channels, double margin) exit (1) ; } ; - if ((k = sf_readf_int (file, data, datalen)) != sfinfo.frames - datalen) + if ((k = (int) sf_readf_int (file, data, datalen)) != sfinfo.frames - datalen) { printf ("\n\nLine %d: Incorrect read length (%" PRId64 " should be %d).\n", __LINE__, channels * sfinfo.frames - datalen, k) ; exit (1) ; @@ -946,7 +946,7 @@ lcomp_test_int (const char *filename, int filetype, int channels, double margin) /* Now test sf_seek function. */ - if ((k = sf_seek (file, 0, SEEK_SET)) != 0) + if ((k = (int) sf_seek (file, 0, SEEK_SET)) != 0) { printf ("\n\nLine %d: Seek to start of file failed (%d).\n", __LINE__, k) ; exit (1) ; } ; @@ -967,7 +967,7 @@ lcomp_test_int (const char *filename, int filetype, int channels, double margin) seekpos = BUFFER_SIZE / 10 ; /* Check seek from start of file. */ - if ((k = sf_seek (file, seekpos, SEEK_SET)) != seekpos) + if ((k = (int) sf_seek (file, seekpos, SEEK_SET)) != seekpos) { printf ("Seek to start of file + %" PRId64 " failed (%d).\n", seekpos, k) ; exit (1) ; } ; @@ -979,13 +979,13 @@ lcomp_test_int (const char *filename, int filetype, int channels, double margin) exit (1) ; } ; - if ((k = sf_seek (file, 0, SEEK_CUR)) != seekpos + 1) + if ((k = (int) sf_seek (file, 0, SEEK_CUR)) != seekpos + 1) { printf ("\n\nLine %d: sf_seek (SEEK_CUR) with 0 offset failed (%d should be %" PRId64 ")\n", __LINE__, k, seekpos + 1) ; exit (1) ; } ; seekpos = sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ; - k = sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ; + k = (int) sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ; test_readf_int_or_die (file, 0, data, 1, __LINE__) ; if (error_function (1.0 * data [0], 1.0 * orig [seekpos * channels], margin) || k != seekpos) { printf ("\nLine %d: sf_seek (forwards, SEEK_CUR) followed by sf_readf_int failed (%d, %d) (%d, %" PRId64 ").\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos + 1) ; @@ -994,7 +994,7 @@ lcomp_test_int (const char *filename, int filetype, int channels, double margin) seekpos = sf_seek (file, 0, SEEK_CUR) - 20 ; /* Check seek backward from current position. */ - k = sf_seek (file, -20, SEEK_CUR) ; + k = (int) sf_seek (file, -20, SEEK_CUR) ; test_readf_int_or_die (file, 0, data, 1, __LINE__) ; if (error_function (1.0 * data [0], 1.0 * orig [seekpos * channels], margin) || k != seekpos) { printf ("\nLine %d: sf_seek (backwards, SEEK_CUR) followed by sf_readf_int failed (%d, %d) (%d, %" PRId64 ").\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos) ; @@ -1004,13 +1004,13 @@ lcomp_test_int (const char *filename, int filetype, int channels, double margin) /* Check that read past end of file returns number of items. */ sf_seek (file, sfinfo.frames, SEEK_SET) ; - if ((k = sf_readf_int (file, data, datalen)) != 0) + if ((k = (int) sf_readf_int (file, data, datalen)) != 0) { printf ("\n\nLine %d: Return value from sf_readf_int past end of file incorrect (%d).\n", __LINE__, k) ; exit (1) ; } ; /* Check seek backward from end. */ - if ((k = sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5) + if ((k = (int) sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5) { printf ("\n\nLine %d: sf_seek (SEEK_END) returned %d instead of %d.\n", __LINE__, k, 5) ; exit (1) ; } ; @@ -1047,9 +1047,9 @@ lcomp_test_float (const char *filename, int filetype, int channels, double margi data = data_buffer.f ; orig = orig_buffer.f ; - gen_signal_double (orig_buffer.d, 32000.0, channels, datalen) ; + gen_signal_double (orig_buffer.d, 32000.0, channels, (int) datalen) ; for (k = 0 ; k < channels * datalen ; k++) - orig [k] = orig_buffer.d [k] ; + orig [k] = (float) orig_buffer.d [k] ; sfinfo.samplerate = SAMPLE_RATE ; sfinfo.frames = 123456789 ; /* Ridiculous value. */ @@ -1105,7 +1105,7 @@ lcomp_test_float (const char *filename, int filetype, int channels, double margi for (k = 0 ; k < datalen ; k++) { if (error_function (data [k], orig [k], margin)) { printf ("\nLine %d: Incorrect sample A (#%d : %f should be %f).\n", __LINE__, k, data [k], orig [k]) ; - oct_save_float (orig, data, datalen) ; + oct_save_float (orig, data, (int) datalen) ; exit (1) ; } ; half_max_abs = LCT_MAX (half_max_abs, fabs (0.5 * data [k])) ; @@ -1116,7 +1116,7 @@ lcomp_test_float (const char *filename, int filetype, int channels, double margi exit (1) ; } ; - if ((k = sf_readf_float (file, data, datalen)) != sfinfo.frames - datalen) + if ((k = (int) sf_readf_float (file, data, datalen)) != sfinfo.frames - datalen) { printf ("\n\nLine %d: Incorrect read length (%" PRId64 " should be %d).\n", __LINE__, channels * sfinfo.frames - datalen, k) ; exit (1) ; @@ -1141,7 +1141,7 @@ lcomp_test_float (const char *filename, int filetype, int channels, double margi /* Now test sf_seek function. */ - if ((k = sf_seek (file, 0, SEEK_SET)) != 0) + if ((k = (int) sf_seek (file, 0, SEEK_SET)) != 0) { printf ("\n\nLine %d: Seek to start of file failed (%d).\n", __LINE__, k) ; exit (1) ; } ; @@ -1162,7 +1162,7 @@ lcomp_test_float (const char *filename, int filetype, int channels, double margi seekpos = BUFFER_SIZE / 10 ; /* Check seek from start of file. */ - if ((k = sf_seek (file, seekpos, SEEK_SET)) != seekpos) + if ((k = (int) sf_seek (file, seekpos, SEEK_SET)) != seekpos) { printf ("Seek to start of file + %d failed (%d).\n", seekpos, k) ; exit (1) ; } ; @@ -1174,22 +1174,22 @@ lcomp_test_float (const char *filename, int filetype, int channels, double margi exit (1) ; } ; - if ((k = sf_seek (file, 0, SEEK_CUR)) != seekpos + 1) + if ((k = (int) sf_seek (file, 0, SEEK_CUR)) != seekpos + 1) { printf ("\n\nLine %d: sf_seek (SEEK_CUR) with 0 offset failed (%d should be %d)\n", __LINE__, k, seekpos + 1) ; exit (1) ; } ; - seekpos = sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ; - k = sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ; + seekpos = (int) sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ; + k = (int) sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ; test_readf_float_or_die (file, 0, data, 1, __LINE__) ; if (error_function (data [0], orig [seekpos * channels], margin) || k != seekpos) { printf ("\nLine %d: sf_seek (forwards, SEEK_CUR) followed by sf_readf_float failed (%f, %f) (%d, %d).\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos + 1) ; exit (1) ; } ; - seekpos = sf_seek (file, 0, SEEK_CUR) - 20 ; + seekpos = (int) sf_seek (file, 0, SEEK_CUR) - 20 ; /* Check seek backward from current position. */ - k = sf_seek (file, -20, SEEK_CUR) ; + k = (int) sf_seek (file, -20, SEEK_CUR) ; test_readf_float_or_die (file, 0, data, 1, __LINE__) ; if (error_function (data [0], orig [seekpos * channels], margin) || k != seekpos) { printf ("\nLine %d: sf_seek (backwards, SEEK_CUR) followed by sf_readf_float failed (%f, %f) (%d, %d).\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos) ; @@ -1199,13 +1199,13 @@ lcomp_test_float (const char *filename, int filetype, int channels, double margi /* Check that read past end of file returns number of items. */ sf_seek (file, sfinfo.frames, SEEK_SET) ; - if ((k = sf_readf_float (file, data, datalen)) != 0) + if ((k = (int) sf_readf_float (file, data, datalen)) != 0) { printf ("\n\nLine %d: Return value from sf_readf_float past end of file incorrect (%d).\n", __LINE__, k) ; exit (1) ; } ; /* Check seek backward from end. */ - if ((k = sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5) + if ((k = (int) sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5) { printf ("\n\nLine %d: sf_seek (SEEK_END) returned %d instead of %d.\n", __LINE__, k, 5) ; exit (1) ; } ; @@ -1242,7 +1242,7 @@ lcomp_test_double (const char *filename, int filetype, int channels, double marg data = data_buffer.d ; orig = orig_buffer.d ; - gen_signal_double (orig_buffer.d, 32000.0, channels, datalen) ; + gen_signal_double (orig_buffer.d, 32000.0, channels, (int) datalen) ; for (k = 0 ; k < channels * datalen ; k++) orig [k] = orig_buffer.d [k] ; @@ -1300,7 +1300,7 @@ lcomp_test_double (const char *filename, int filetype, int channels, double marg for (k = 0 ; k < datalen ; k++) { if (error_function (data [k], orig [k], margin)) { printf ("\nLine %d: Incorrect sample A (#%d : %f should be %f).\n", __LINE__, k, data [k], orig [k]) ; - oct_save_double (orig, data, datalen) ; + oct_save_double (orig, data, (int) datalen) ; exit (1) ; } ; half_max_abs = LCT_MAX (half_max_abs, ABS (0.5 * data [k])) ; @@ -1311,7 +1311,7 @@ lcomp_test_double (const char *filename, int filetype, int channels, double marg exit (1) ; } ; - if ((k = sf_readf_double (file, data, datalen)) != sfinfo.frames - datalen) + if ((k = (int) sf_readf_double (file, data, datalen)) != sfinfo.frames - datalen) { printf ("\n\nLine %d: Incorrect read length (%" PRId64 " should be %d).\n", __LINE__, channels * sfinfo.frames - datalen, k) ; exit (1) ; @@ -1336,7 +1336,7 @@ lcomp_test_double (const char *filename, int filetype, int channels, double marg /* Now test sf_seek function. */ - if ((k = sf_seek (file, 0, SEEK_SET)) != 0) + if ((k = (int) sf_seek (file, 0, SEEK_SET)) != 0) { printf ("\n\nLine %d: Seek to start of file failed (%d).\n", __LINE__, k) ; exit (1) ; } ; @@ -1357,7 +1357,7 @@ lcomp_test_double (const char *filename, int filetype, int channels, double marg seekpos = BUFFER_SIZE / 10 ; /* Check seek from start of file. */ - if ((k = sf_seek (file, seekpos, SEEK_SET)) != seekpos) + if ((k = (int) sf_seek (file, seekpos, SEEK_SET)) != seekpos) { printf ("Seek to start of file + %d failed (%d).\n", seekpos, k) ; exit (1) ; } ; @@ -1369,22 +1369,22 @@ lcomp_test_double (const char *filename, int filetype, int channels, double marg exit (1) ; } ; - if ((k = sf_seek (file, 0, SEEK_CUR)) != seekpos + 1) + if ((k = (int) sf_seek (file, 0, SEEK_CUR)) != seekpos + 1) { printf ("\n\nLine %d: sf_seek (SEEK_CUR) with 0 offset failed (%d should be %d)\n", __LINE__, k, seekpos + 1) ; exit (1) ; } ; - seekpos = sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ; - k = sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ; + seekpos = (int) sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ; + k = (int) sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ; test_readf_double_or_die (file, 0, data, 1, __LINE__) ; if (error_function (data [0], orig [seekpos * channels], margin) || k != seekpos) { printf ("\nLine %d: sf_seek (forwards, SEEK_CUR) followed by sf_readf_double failed (%f, %f) (%d, %d).\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos + 1) ; exit (1) ; } ; - seekpos = sf_seek (file, 0, SEEK_CUR) - 20 ; + seekpos = (int) sf_seek (file, 0, SEEK_CUR) - 20 ; /* Check seek backward from current position. */ - k = sf_seek (file, -20, SEEK_CUR) ; + k = (int) sf_seek (file, -20, SEEK_CUR) ; test_readf_double_or_die (file, 0, data, 1, __LINE__) ; if (error_function (data [0], orig [seekpos * channels], margin) || k != seekpos) { printf ("\nLine %d: sf_seek (backwards, SEEK_CUR) followed by sf_readf_double failed (%f, %f) (%d, %d).\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos) ; @@ -1394,13 +1394,13 @@ lcomp_test_double (const char *filename, int filetype, int channels, double marg /* Check that read past end of file returns number of items. */ sf_seek (file, sfinfo.frames, SEEK_SET) ; - if ((k = sf_readf_double (file, data, datalen)) != 0) + if ((k = (int) sf_readf_double (file, data, datalen)) != 0) { printf ("\n\nLine %d: Return value from sf_readf_double past end of file incorrect (%d).\n", __LINE__, k) ; exit (1) ; } ; /* Check seek backward from end. */ - if ((k = sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5) + if ((k = (int) sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5) { printf ("\n\nLine %d: sf_seek (SEEK_END) returned %d instead of %d.\n", __LINE__, k, 5) ; exit (1) ; } ; @@ -1440,9 +1440,9 @@ channels = 1 ; data = data_buffer.s ; smooth = smooth_buffer.s ; - gen_signal_double (orig_buffer.d, 32000.0, channels, datalen) ; + gen_signal_double (orig_buffer.d, 32000.0, channels, (int) datalen) ; for (k = 0 ; k < datalen ; k++) - orig [k] = lrint (orig_buffer.d [k]) ; + orig [k] = (short) lrint (orig_buffer.d [k]) ; sfinfo.samplerate = SAMPLE_RATE ; sfinfo.frames = 123456789 ; /* Ridiculous value. */ @@ -1522,17 +1522,17 @@ channels = 1 ; test_readf_short_or_die (file, 0, data, datalen, __LINE__) ; memcpy (smooth, orig, datalen * sizeof (short)) ; - smoothed_diff_short (data, datalen) ; - smoothed_diff_short (smooth, datalen) ; + smoothed_diff_short (data, (unsigned int) datalen) ; + smoothed_diff_short (smooth, (unsigned int) datalen) ; - half_max_abs = 0.0 ; + half_max_abs = 0 ; for (k = 0 ; k < datalen ; k++) { if (error_function (1.0 * data [k], 1.0 * smooth [k], margin)) { printf ("\nLine %d: Incorrect sample (#%d : %d should be %d).\n", __LINE__, k, data [k], smooth [k]) ; - oct_save_short (orig, smooth, datalen) ; + oct_save_short (orig, smooth, (int) datalen) ; exit (1) ; } ; - half_max_abs = LCT_MAX (half_max_abs, ABS (0.5 * data [k])) ; + half_max_abs = (int) (LCT_MAX (half_max_abs, ABS (0.5 * data [k]))) ; } ; if (half_max_abs < 1) @@ -1540,7 +1540,7 @@ channels = 1 ; exit (1) ; } ; - if ((k = sf_read_short (file, data, datalen)) != sfinfo.frames - datalen) + if ((k = (int) sf_read_short (file, data, datalen)) != sfinfo.frames - datalen) { printf ("\n\nLine %d: Incorrect read length (%d should be %" PRId64 ").\n", __LINE__, k, sfinfo.frames - datalen) ; exit (1) ; } ; @@ -1555,7 +1555,7 @@ channels = 1 ; /* Now test sf_seek function. */ if (sfinfo.seekable) - { if ((k = sf_seek (file, 0, SEEK_SET)) != 0) + { if ((k = (int) sf_seek (file, 0, SEEK_SET)) != 0) { printf ("\n\nLine %d: Seek to start of file failed (%d).\n", __LINE__, k) ; exit (1) ; } ; @@ -1563,9 +1563,9 @@ channels = 1 ; for (m = 0 ; m < 3 ; m++) { test_readf_short_or_die (file, m, data, datalen / 7, __LINE__) ; - smoothed_diff_short (data, datalen / 7) ; + smoothed_diff_short (data, (unsigned int) (datalen / 7)) ; memcpy (smooth, orig + m * datalen / 7, datalen / 7 * sizeof (short)) ; - smoothed_diff_short (smooth, datalen / 7) ; + smoothed_diff_short (smooth, (unsigned int) (datalen / 7)) ; for (k = 0 ; k < datalen / 7 ; k++) if (error_function (1.0 * data [k], 1.0 * smooth [k], margin)) @@ -1580,7 +1580,7 @@ channels = 1 ; seekpos = BUFFER_SIZE / 10 ; /* Check seek from start of file. */ - if ((k = sf_seek (file, seekpos, SEEK_SET)) != seekpos) + if ((k = (int) sf_seek (file, seekpos, SEEK_SET)) != seekpos) { printf ("Seek to start of file + %d failed (%d).\n", seekpos, k) ; exit (1) ; } ; @@ -1591,22 +1591,22 @@ channels = 1 ; exit (1) ; } ; - if ((k = sf_seek (file, 0, SEEK_CUR)) != seekpos + 1) + if ((k = (int) sf_seek (file, 0, SEEK_CUR)) != seekpos + 1) { printf ("\n\nLine %d: sf_seek (SEEK_CUR) with 0 offset failed (%d should be %d)\n", __LINE__, k, seekpos + 1) ; exit (1) ; } ; - seekpos = sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ; - k = sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ; + seekpos = (int) sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ; + k = (int) sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ; test_readf_short_or_die (file, 0, data, 1, __LINE__) ; if (error_function (1.0 * data [0], 1.0 * orig [seekpos * channels], margin) || k != seekpos) { printf ("\nLine %d: sf_seek (forwards, SEEK_CUR) followed by sf_read_short failed (%d, %d) (%d, %d).\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos + 1) ; exit (1) ; } ; - seekpos = sf_seek (file, 0, SEEK_CUR) - 20 ; + seekpos = (int) sf_seek (file, 0, SEEK_CUR) - 20 ; /* Check seek backward from current position. */ - k = sf_seek (file, -20, SEEK_CUR) ; + k = (int) sf_seek (file, -20, SEEK_CUR) ; test_readf_short_or_die (file, 0, data, 1, __LINE__) ; if (error_function (1.0 * data [0], 1.0 * orig [seekpos * channels], margin) || k != seekpos) { printf ("\nLine %d: sf_seek (backwards, SEEK_CUR) followed by sf_read_short failed (%d, %d) (%d, %d).\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos) ; @@ -1616,14 +1616,14 @@ channels = 1 ; /* Check that read past end of file returns number of items. */ sf_seek (file, sfinfo.frames, SEEK_SET) ; - if ((k = sf_read_short (file, data, datalen)) != 0) + if ((k = (int) sf_read_short (file, data, datalen)) != 0) { printf ("\n\nLine %d: Return value from sf_read_short past end of file incorrect (%d).\n", __LINE__, k) ; exit (1) ; } ; /* Check seek backward from end. */ - if ((k = sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5) + if ((k = (int) sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5) { printf ("\n\nLine %d: sf_seek (SEEK_END) returned %d instead of %d.\n", __LINE__, k, 5) ; exit (1) ; } ; @@ -1662,7 +1662,7 @@ channels = 1 ; data = data_buffer.i ; smooth = smooth_buffer.i ; - gen_signal_double (orig_buffer.d, 32000.0 * scale, channels, datalen) ; + gen_signal_double (orig_buffer.d, 32000.0 * scale, channels, (int) datalen) ; for (k = 0 ; k < datalen ; k++) orig [k] = lrint (orig_buffer.d [k]) ; @@ -1740,14 +1740,14 @@ channels = 1 ; test_readf_int_or_die (file, 0, data, datalen, __LINE__) ; memcpy (smooth, orig, datalen * sizeof (int)) ; - smoothed_diff_int (data, datalen) ; - smoothed_diff_int (smooth, datalen) ; + smoothed_diff_int (data, (unsigned int) datalen) ; + smoothed_diff_int (smooth, (unsigned int) datalen) ; half_max_abs = abs (data [0] >> 16) ; for (k = 1 ; k < datalen ; k++) { if (error_function (data [k] / scale, smooth [k] / scale, margin)) { printf ("\nLine %d: Incorrect sample (#%d : %d should be %d).\n", __LINE__, k, data [k], smooth [k]) ; - oct_save_int (orig, smooth, datalen) ; + oct_save_int (orig, smooth, (int) datalen) ; exit (1) ; } ; half_max_abs = LCT_MAX (half_max_abs, abs (data [k] / 2)) ; @@ -1758,7 +1758,7 @@ channels = 1 ; exit (1) ; } ; - if ((k = sf_readf_int (file, data, datalen)) != sfinfo.frames - datalen) + if ((k = (int) sf_readf_int (file, data, datalen)) != sfinfo.frames - datalen) { printf ("\n\nLine %d: Incorrect read length (%d should be %" PRId64 ").\n", __LINE__, k, sfinfo.frames - datalen) ; exit (1) ; } ; @@ -1779,7 +1779,7 @@ channels = 1 ; /* Now test sf_seek function. */ if (sfinfo.seekable) - { if ((k = sf_seek (file, 0, SEEK_SET)) != 0) + { if ((k = (int) sf_seek (file, 0, SEEK_SET)) != 0) { printf ("\n\nLine %d: Seek to start of file failed (%d).\n", __LINE__, k) ; exit (1) ; } ; @@ -1787,9 +1787,9 @@ channels = 1 ; for (m = 0 ; m < 3 ; m++) { test_readf_int_or_die (file, m, data, datalen / 7, __LINE__) ; - smoothed_diff_int (data, datalen / 7) ; + smoothed_diff_int (data, (unsigned int) (datalen / 7)) ; memcpy (smooth, orig + m * datalen / 7, datalen / 7 * sizeof (int)) ; - smoothed_diff_int (smooth, datalen / 7) ; + smoothed_diff_int (smooth, (unsigned int) (datalen / 7)) ; for (k = 0 ; k < datalen / 7 ; k++) if (error_function (data [k] / scale, smooth [k] / scale, margin)) @@ -1804,7 +1804,7 @@ channels = 1 ; seekpos = BUFFER_SIZE / 10 ; /* Check seek from start of file. */ - if ((k = sf_seek (file, seekpos, SEEK_SET)) != seekpos) + if ((k = (int) sf_seek (file, seekpos, SEEK_SET)) != seekpos) { printf ("Seek to start of file + %d failed (%d).\n", seekpos, k) ; exit (1) ; } ; @@ -1815,22 +1815,22 @@ channels = 1 ; exit (1) ; } ; - if ((k = sf_seek (file, 0, SEEK_CUR)) != seekpos + 1) + if ((k = (int) sf_seek (file, 0, SEEK_CUR)) != seekpos + 1) { printf ("\n\nLine %d: sf_seek (SEEK_CUR) with 0 offset failed (%d should be %d)\n", __LINE__, k, seekpos + 1) ; exit (1) ; } ; - seekpos = sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ; - k = sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ; + seekpos = (int) sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ; + k = (int) sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ; test_readf_int_or_die (file, 0, data, 1, __LINE__) ; if (error_function (1.0 * data [0], 1.0 * orig [seekpos * channels], margin) || k != seekpos) { printf ("\nLine %d: sf_seek (forwards, SEEK_CUR) followed by sf_readf_int failed (%d, %d) (%d, %d).\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos + 1) ; exit (1) ; } ; - seekpos = sf_seek (file, 0, SEEK_CUR) - 20 ; + seekpos = (int) sf_seek (file, 0, SEEK_CUR) - 20 ; /* Check seek backward from current position. */ - k = sf_seek (file, -20, SEEK_CUR) ; + k = (int) sf_seek (file, -20, SEEK_CUR) ; test_readf_int_or_die (file, 0, data, 1, __LINE__) ; if (error_function (1.0 * data [0], 1.0 * orig [seekpos * channels], margin) || k != seekpos) { printf ("\nLine %d: sf_seek (backwards, SEEK_CUR) followed by sf_readf_int failed (%d, %d) (%d, %d).\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos) ; @@ -1840,14 +1840,14 @@ channels = 1 ; /* Check that read past end of file returns number of items. */ sf_seek (file, sfinfo.frames, SEEK_SET) ; - if ((k = sf_readf_int (file, data, datalen)) != 0) + if ((k = (int) sf_readf_int (file, data, datalen)) != 0) { printf ("\n\nLine %d: Return value from sf_readf_int past end of file incorrect (%d).\n", __LINE__, k) ; exit (1) ; } ; /* Check seek backward from end. */ - if ((k = sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5) + if ((k = (int) sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5) { printf ("\n\nLine %d: sf_seek (SEEK_END) returned %d instead of %d.\n", __LINE__, k, 5) ; exit (1) ; } ; @@ -1908,9 +1908,9 @@ channels = 1 ; data = data_buffer.f ; smooth = smooth_buffer.f ; - gen_signal_double (orig_buffer.d, 32000.0 / scale, channels, datalen) ; + gen_signal_double (orig_buffer.d, 32000.0 / scale, channels, (int) datalen) ; for (k = 0 ; k < datalen ; k++) - orig [k] = orig_buffer.d [k] ; + orig [k] = (float) orig_buffer.d [k] ; sfinfo.samplerate = SAMPLE_RATE ; sfinfo.frames = 123456789 ; /* Ridiculous value. */ @@ -1992,14 +1992,14 @@ channels = 1 ; test_read_float_or_die (file, 0, data, datalen, __LINE__) ; memcpy (smooth, orig, datalen * sizeof (float)) ; - smoothed_diff_float (data, datalen) ; - smoothed_diff_float (smooth, datalen) ; + smoothed_diff_float (data, (unsigned int) datalen) ; + smoothed_diff_float (smooth, (unsigned int) datalen) ; half_max_abs = fabs (data [0]) ; for (k = 1 ; k < datalen ; k++) { if (error_function (data [k] * scale, smooth [k] * scale, margin)) { printf ("\nLine %d: Incorrect sample (#%d : %d should be %d).\n", __LINE__, k, (int) (data [k] * scale), (int) (smooth [k] * scale)) ; - oct_save_float (orig, smooth, datalen) ; + oct_save_float (orig, smooth, (int) datalen) ; exit (1) ; } ; half_max_abs = LCT_MAX (half_max_abs, ABS (0.5 * data [k] * scale)) ; @@ -2011,7 +2011,7 @@ channels = 1 ; exit (1) ; } ; - if ((k = sf_read_float (file, data, datalen)) != sfinfo.frames - datalen) + if ((k = (int) sf_read_float (file, data, datalen)) != sfinfo.frames - datalen) { printf ("\n\nLine %d: Incorrect read length (%d should be %" PRId64 ").\n", __LINE__, k, sfinfo.frames - datalen) ; exit (1) ; } ; @@ -2026,7 +2026,7 @@ channels = 1 ; /* Now test sf_seek function. */ if (sfinfo.seekable) - { if ((k = sf_seek (file, 0, SEEK_SET)) != 0) + { if ((k = (int) sf_seek (file, 0, SEEK_SET)) != 0) { printf ("\n\nLine %d: Seek to start of file failed (%d).\n", __LINE__, k) ; exit (1) ; } ; @@ -2034,9 +2034,9 @@ channels = 1 ; for (m = 0 ; m < 3 ; m++) { test_read_float_or_die (file, 0, data, datalen / 7, __LINE__) ; - smoothed_diff_float (data, datalen / 7) ; + smoothed_diff_float (data, (unsigned int) (datalen / 7)) ; memcpy (smooth, orig + m * datalen / 7, datalen / 7 * sizeof (float)) ; - smoothed_diff_float (smooth, datalen / 7) ; + smoothed_diff_float (smooth, (unsigned int) (datalen / 7)) ; for (k = 0 ; k < datalen / 7 ; k++) if (error_function (data [k] * scale, smooth [k] * scale, margin)) @@ -2051,7 +2051,7 @@ channels = 1 ; seekpos = BUFFER_SIZE / 10 ; /* Check seek from start of file. */ - if ((k = sf_seek (file, seekpos, SEEK_SET)) != seekpos) + if ((k = (int) sf_seek (file, seekpos, SEEK_SET)) != seekpos) { printf ("Seek to start of file + %d failed (%d).\n", seekpos, k) ; exit (1) ; } ; @@ -2062,22 +2062,22 @@ channels = 1 ; exit (1) ; } ; - if ((k = sf_seek (file, 0, SEEK_CUR)) != seekpos + 1) + if ((k = (int) sf_seek (file, 0, SEEK_CUR)) != seekpos + 1) { printf ("\n\nLine %d: sf_seek (SEEK_CUR) with 0 offset failed (%d should be %d)\n", __LINE__, k, seekpos + 1) ; exit (1) ; } ; - seekpos = sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ; - k = sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ; + seekpos = (int) sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ; + k = (int) sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ; test_read_float_or_die (file, 0, data, channels, __LINE__) ; if (error_function (data [0] * scale, orig [seekpos * channels] * scale, margin) || k != seekpos) { printf ("\nLine %d: sf_seek (forwards, SEEK_CUR) followed by sf_read_float failed (%d, %d) (%d, %d).\n", __LINE__, (int) (data [0] * scale), (int) (orig [seekpos * channels] * scale), k, seekpos + 1) ; exit (1) ; } ; - seekpos = sf_seek (file, 0, SEEK_CUR) - 20 ; + seekpos = (int) sf_seek (file, 0, SEEK_CUR) - 20 ; /* Check seek backward from current position. */ - k = sf_seek (file, -20, SEEK_CUR) ; + k = (int) sf_seek (file, -20, SEEK_CUR) ; test_read_float_or_die (file, 0, data, channels, __LINE__) ; if (error_function (data [0] * scale, orig [seekpos * channels] * scale, margin) || k != seekpos) { printf ("\nLine %d: sf_seek (backwards, SEEK_CUR) followed by sf_read_float failed (%d, %d) (%d, %d).\n", __LINE__, (int) (data [0] * scale), (int) (orig [seekpos * channels] * scale), k, seekpos) ; @@ -2087,14 +2087,14 @@ channels = 1 ; /* Check that read past end of file returns number of items. */ sf_seek (file, sfinfo.frames, SEEK_SET) ; - if ((k = sf_read_float (file, data, datalen)) != 0) + if ((k = (int) sf_read_float (file, data, datalen)) != 0) { printf ("\n\nLine %d: Return value from sf_read_float past end of file incorrect (%d).\n", __LINE__, k) ; exit (1) ; } ; /* Check seek backward from end. */ - if ((k = sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5) + if ((k = (int) sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5) { printf ("\n\nLine %d: sf_seek (SEEK_END) returned %d instead of %d.\n", __LINE__, k, 5) ; exit (1) ; } ; @@ -2154,7 +2154,7 @@ channels = 1 ; data = data_buffer.d ; smooth = smooth_buffer.d ; - gen_signal_double (orig_buffer.d, 32000.0 / scale, channels, datalen) ; + gen_signal_double (orig_buffer.d, 32000.0 / scale, channels, (int) datalen) ; sfinfo.samplerate = SAMPLE_RATE ; sfinfo.frames = 123456789 ; /* Ridiculous value. */ @@ -2237,14 +2237,14 @@ channels = 1 ; test_read_double_or_die (file, 0, data, datalen, __LINE__) ; memcpy (smooth, orig, datalen * sizeof (double)) ; - smoothed_diff_double (data, datalen) ; - smoothed_diff_double (smooth, datalen) ; + smoothed_diff_double (data, (unsigned int) datalen) ; + smoothed_diff_double (smooth, (unsigned int) datalen) ; half_max_abs = 0.0 ; for (k = 0 ; k < datalen ; k++) { if (error_function (data [k] * scale, smooth [k] * scale, margin)) { printf ("\n\nLine %d: Incorrect sample (#%d : %d should be %d).\n", __LINE__, k, (int) (data [k] * scale), (int) (smooth [k] * scale)) ; - oct_save_double (orig, smooth, datalen) ; + oct_save_double (orig, smooth, (int) datalen) ; exit (1) ; } ; half_max_abs = LCT_MAX (half_max_abs, 0.5 * fabs (data [k] * scale)) ; @@ -2255,7 +2255,7 @@ channels = 1 ; exit (1) ; } ; - if ((k = sf_read_double (file, data, datalen)) != sfinfo.frames - datalen) + if ((k = (int) sf_read_double (file, data, datalen)) != sfinfo.frames - datalen) { printf ("\n\nLine %d: Incorrect read length (%d should be %" PRId64 ").\n", __LINE__, k, sfinfo.frames - datalen) ; exit (1) ; } ; @@ -2270,7 +2270,7 @@ channels = 1 ; /* Now test sf_seek function. */ if (sfinfo.seekable) - { if ((k = sf_seek (file, 0, SEEK_SET)) != 0) + { if ((k = (int) sf_seek (file, 0, SEEK_SET)) != 0) { printf ("\n\nLine %d: Seek to start of file failed (%d).\n", __LINE__, k) ; exit (1) ; } ; @@ -2278,9 +2278,9 @@ channels = 1 ; for (m = 0 ; m < 3 ; m++) { test_read_double_or_die (file, m, data, datalen / 7, __LINE__) ; - smoothed_diff_double (data, datalen / 7) ; + smoothed_diff_double (data, (unsigned int) (datalen / 7)) ; memcpy (smooth, orig + m * datalen / 7, datalen / 7 * sizeof (double)) ; - smoothed_diff_double (smooth, datalen / 7) ; + smoothed_diff_double (smooth, (unsigned int) (datalen / 7)) ; for (k = 0 ; k < datalen / 7 ; k++) if (error_function (data [k] * scale, smooth [k] * scale, margin)) @@ -2295,7 +2295,7 @@ channels = 1 ; seekpos = BUFFER_SIZE / 10 ; /* Check seek from start of file. */ - if ((k = sf_seek (file, seekpos, SEEK_SET)) != seekpos) + if ((k = (int) sf_seek (file, seekpos, SEEK_SET)) != seekpos) { printf ("Seek to start of file + %d failed (%d).\n", seekpos, k) ; exit (1) ; } ; @@ -2306,22 +2306,22 @@ channels = 1 ; exit (1) ; } ; - if ((k = sf_seek (file, 0, SEEK_CUR)) != seekpos + 1) + if ((k = (int) sf_seek (file, 0, SEEK_CUR)) != seekpos + 1) { printf ("\n\nLine %d: sf_seek (SEEK_CUR) with 0 offset failed (%d should be %d)\n", __LINE__, k, seekpos + 1) ; exit (1) ; } ; - seekpos = sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ; - k = sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ; + seekpos = (int) sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ; + k = (int) sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ; test_read_double_or_die (file, 0, data, channels, __LINE__) ; if (error_function (data [0] * scale, orig [seekpos * channels] * scale, margin) || k != seekpos) { printf ("\nLine %d: sf_seek (forwards, SEEK_CUR) followed by sf_read_double failed (%d, %d) (%d, %d).\n", __LINE__, (int) (data [0] * scale), (int) (orig [seekpos * channels] * scale), k, seekpos + 1) ; exit (1) ; } ; - seekpos = sf_seek (file, 0, SEEK_CUR) - 20 ; + seekpos = (int) sf_seek (file, 0, SEEK_CUR) - 20 ; /* Check seek backward from current position. */ - k = sf_seek (file, -20, SEEK_CUR) ; + k = (int) sf_seek (file, -20, SEEK_CUR) ; test_read_double_or_die (file, 0, data, channels, __LINE__) ; if (error_function (data [0] * scale, orig [seekpos * channels] * scale, margin) || k != seekpos) { printf ("\nLine %d: sf_seek (backwards, SEEK_CUR) followed by sf_read_double failed (%d, %d) (%d, %d).\n", __LINE__, (int) (data [0] * scale), (int) (orig [seekpos * channels] * scale), k, seekpos) ; @@ -2331,14 +2331,14 @@ channels = 1 ; /* Check that read past end of file returns number of items. */ sf_seek (file, sfinfo.frames, SEEK_SET) ; - if ((k = sf_read_double (file, data, datalen)) != 0) + if ((k = (int) sf_read_double (file, data, datalen)) != 0) { printf ("\n\nLine %d: Return value from sf_read_double past end of file incorrect (%d).\n", __LINE__, k) ; exit (1) ; } ; /* Check seek backward from end. */ - if ((k = sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5) + if ((k = (int) sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5) { printf ("\n\nLine %d: sf_seek (SEEK_END) returned %d instead of %d.\n", __LINE__, k, 5) ; exit (1) ; } ; @@ -2372,9 +2372,9 @@ read_raw_test (const char *filename, int filetype, int channels) orig = orig_buffer.s ; data = data_buffer.s ; - gen_signal_double (orig_buffer.d, 32000.0, channels, datalen) ; + gen_signal_double (orig_buffer.d, 32000.0, channels, (int) datalen) ; for (k = 0 ; k < datalen ; k++) - orig [k] = lrint (orig_buffer.d [k]) ; + orig [k] = (short) lrint (orig_buffer.d [k]) ; sfinfo.samplerate = SAMPLE_RATE ; sfinfo.frames = 123456789 ; /* Ridiculous value. */ @@ -2533,7 +2533,7 @@ smoothed_diff_float (float *data, unsigned int datalen) /* Calculate the smoothed sample-to-sample difference. */ for (k = 0 ; k < datalen - 1 ; k++) - { memory = 0.7 * memory + (1 - 0.7) * (data [k+1] - data [k]) ; + { memory = (float) (0.7 * memory + (1 - 0.7) * (data [k+1] - data [k])) ; data [k] = memory ; } ; data [datalen-1] = data [datalen-2] ; diff --git a/tests/multi_file_test.c b/tests/multi_file_test.c index 502d15a6..69069731 100644 --- a/tests/multi_file_test.c +++ b/tests/multi_file_test.c @@ -161,7 +161,7 @@ multi_file_test (const char *filename, int *formats, int format_count) printf ("This offset : %" PRId64 "\n", embed_info.offset + embed_info.length) ; } ; - if (lseek (fd, embed_info.offset + embed_info.length, SEEK_SET) < 0) + if (lseek (fd, (long) (embed_info.offset + embed_info.length), SEEK_SET) < 0) { printf ("\n\nLine %d: lseek failed : %s\n", __LINE__, strerror (errno)) ; exit (1) ; } ; diff --git a/tests/ogg_opus_test.c b/tests/ogg_opus_test.c index 37532bec..a850908c 100644 --- a/tests/ogg_opus_test.c +++ b/tests/ogg_opus_test.c @@ -62,7 +62,7 @@ ogg_opus_short_test (void) /* Convert to short. */ for (k = 0 ; k < ARRAY_LEN (data_out.s) ; k++) - data_out.s [k] = lrintf (data_out.f [k]) ; + data_out.s [k] = (short) lrintf (data_out.f [k]) ; memset (&sfinfo, 0, sizeof (sfinfo)) ; diff --git a/tests/ogg_test.c b/tests/ogg_test.c index ba3d8992..dcc5ed19 100644 --- a/tests/ogg_test.c +++ b/tests/ogg_test.c @@ -62,7 +62,7 @@ ogg_short_test (void) /* Convert to shorteger. */ for (k = 0 ; k < ARRAY_LEN (data_out.s) ; k++) - data_out.s [k] = lrintf (data_out.f [k]) ; + data_out.s [k] = (short) lrintf (data_out.f [k]) ; memset (&sfinfo, 0, sizeof (sfinfo)) ; diff --git a/tests/stdin_test.c b/tests/stdin_test.c index 651614ae..1a9c67b8 100644 --- a/tests/stdin_test.c +++ b/tests/stdin_test.c @@ -185,7 +185,7 @@ stdin_test (int typemajor, int count) } ; total = 0 ; - while ((k = sf_read_short (file, data + total, BUFFER_LEN - total)) > 0) + while ((k = (int) sf_read_short (file, data + total, BUFFER_LEN - total)) > 0) total += k ; if (total != count) diff --git a/tests/stdout_test.c b/tests/stdout_test.c index a71ed643..e2cbe8d8 100644 --- a/tests/stdout_test.c +++ b/tests/stdout_test.c @@ -155,7 +155,7 @@ stdout_test (int typemajor, int count) while (total < count) { this_write = (count - total > 1024) ? 1024 : count - total ; - if ((k = sf_write_short (file, data + total, this_write)) != this_write) + if ((k = (int) sf_write_short (file, data + total, this_write)) != this_write) { fprintf (stderr, "sf_write_short # %d failed with short write (%d -> %d)\n", count, this_write, k) ; exit (1) ; } ; diff --git a/tests/virtual_io_test.c b/tests/virtual_io_test.c index 854bc302..b5f84ac2 100644 --- a/tests/virtual_io_test.c +++ b/tests/virtual_io_test.c @@ -216,7 +216,7 @@ vio_test (const char *fname, int format) fflush (stdout) ; puts (sf_strerror (NULL)) ; - dump_data_to_file (fname, vio_data.data, vio_data.length) ; + dump_data_to_file (fname, vio_data.data, (unsigned int) vio_data.length) ; exit (1) ; } ; From 61bcb5e979cbd203f7b346d2231276a431871262 Mon Sep 17 00:00:00 2001 From: evpobr Date: Sat, 6 Mar 2021 11:54:08 +0500 Subject: [PATCH 051/204] Fix previous commit --- programs/common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/programs/common.c b/programs/common.c index b0567677..33d7ed7e 100644 --- a/programs/common.c +++ b/programs/common.c @@ -157,7 +157,7 @@ merge_broadcast_info (SNDFILE * infile, SNDFILE * outfile, int format, const MET /* Special case loudness values */ #define REPLACE_IF_NEW_INT(x) \ if (info->x != NULL) \ - { binfo.x = (int16_t) round (atof (info->x) * 100.0) ; \ + { binfo.x = (int16_t) (double) round (atof (info->x) * 100.0) ; \ } ; REPLACE_IF_NEW_INT (loudness_value) ; From 36c5198d9108787755f6d32e57de519c5e69046e Mon Sep 17 00:00:00 2001 From: evpobr Date: Sat, 6 Mar 2021 12:00:31 +0500 Subject: [PATCH 052/204] common: Fix regression after 4ff3b84 --- programs/common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/programs/common.c b/programs/common.c index 33d7ed7e..84e4cc35 100644 --- a/programs/common.c +++ b/programs/common.c @@ -157,7 +157,7 @@ merge_broadcast_info (SNDFILE * infile, SNDFILE * outfile, int format, const MET /* Special case loudness values */ #define REPLACE_IF_NEW_INT(x) \ if (info->x != NULL) \ - { binfo.x = (int16_t) (double) round (atof (info->x) * 100.0) ; \ + { binfo.x = round (atof (info->x) * 100.0) ; \ } ; REPLACE_IF_NEW_INT (loudness_value) ; From 6dee7120ab5583056577412558f183ed7d4d980f Mon Sep 17 00:00:00 2001 From: evpobr Date: Sat, 6 Mar 2021 12:22:14 +0500 Subject: [PATCH 053/204] sndfile-salvage: Handle files > 4 GB on Windows OS --- CHANGELOG.md | 1 + programs/sndfile-salvage.c | 29 ++++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d68c579..a5c307f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `replace_read_f2i`(), thanks @bobsayshilol, (issue #702). * Missing samples when doing a partial read of Ogg file from index till the end of file, thanks @arthurt (issue #643). +* sndfile-salvage: Handle files > 4 GB on Windows OS ### Security diff --git a/programs/sndfile-salvage.c b/programs/sndfile-salvage.c index f60ad2ad..1a76dc3a 100644 --- a/programs/sndfile-salvage.c +++ b/programs/sndfile-salvage.c @@ -56,6 +56,12 @@ #define NOT(x) (! (x)) +#ifndef _WIN32 +typedef off_t sf_off_t ; +#else +typedef long long sf_off_t ; +#endif + static void usage_exit (const char *progname) ; static void salvage_file (const char * broken_wav, const char * fixed_w64) ; @@ -74,8 +80,8 @@ main (int argc, char *argv []) /*============================================================================== */ -static void lseek_or_die (int fd, off_t offset, int whence) ; -static sf_count_t get_file_length (int fd, const char * name) ; +static void lseek_or_die (int fd, sf_off_t offset, int whence) ; +static sf_off_t get_file_length (int fd, const char * name) ; static sf_count_t find_data_offset (int fd, int format) ; static void copy_data (int fd, SNDFILE * sndfile, int readsize) ; @@ -180,9 +186,13 @@ salvage_file (const char * broken_wav, const char * fixed_w64) */ static void -lseek_or_die (int fd, off_t offset, int whence) +lseek_or_die (int fd, sf_off_t offset, int whence) { +#ifndef _WIN32 if (lseek (fd, offset, whence) < 0) +#else + if (_lseeki64 (fd, offset, whence) < 0) +#endif { printf ("lseek failed : %s\n", strerror (errno)) ; exit (1) ; } ; @@ -191,9 +201,14 @@ lseek_or_die (int fd, off_t offset, int whence) } /* lseek_or_die */ -static sf_count_t +static sf_off_t get_file_length (int fd, const char * name) -{ struct stat sbuf ; +{ +#ifndef _WIN32 + struct stat sbuf ; +#else + struct _stat64 sbuf ; +#endif if (sizeof (sbuf.st_size) != 8) { puts ("Error : sizeof (sbuf.st_size) != 8. Was program compiled with\n" @@ -201,7 +216,11 @@ get_file_length (int fd, const char * name) exit (1) ; } ; +#ifndef _WIN32 if (fstat (fd, &sbuf) != 0) +#else + if (_fstat64 (fd, &sbuf) != 0) +#endif { printf ("Error : fstat ('%s') failed : %s\n", name, strerror (errno)) ; exit (1) ; } ; From 97f9e64b39e0c4a880c67c8c4cdd9ade7e494ca2 Mon Sep 17 00:00:00 2001 From: evpobr Date: Sun, 7 Mar 2021 11:23:28 +0500 Subject: [PATCH 054/204] sfendian: Fix byteswap optimizations detection --- src/sfendian.h | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/sfendian.h b/src/sfendian.h index 0189d7df..4921bb9e 100644 --- a/src/sfendian.h +++ b/src/sfendian.h @@ -24,6 +24,10 @@ #include #include +#ifndef __has_builtin +#define __has_builtin(x) 0 +#endif + #if HAVE_BYTESWAP_H /* Linux, any CPU */ #include @@ -31,19 +35,11 @@ #define ENDSWAP_32(x) (bswap_32 (x)) #define ENDSWAP_64(x) (bswap_64 (x)) -#elif defined __has_builtin +#elif __has_builtin(__builtin_bswap16) && __has_builtin(__builtin_bswap32) && __has_builtin(__builtin_bswap64) -#if __has_builtin (__builtin_bswap16) #define ENDSWAP_16(x) ((int16_t) __builtin_bswap16 ((uint16_t) x)) -#endif - -#if __has_builtin (__builtin_bswap32) #define ENDSWAP_32(x) ((int32_t) __builtin_bswap32 ((uint32_t) x)) -#endif - -#if __has_builtin (__builtin_bswap64) #define ENDSWAP_64(x) ((int64_t) __builtin_bswap64 ((uint64_t) x)) -#endif #elif COMPILER_IS_GCC From 5645fa8ee8757207c7c2e536c157983fc5620991 Mon Sep 17 00:00:00 2001 From: evpobr Date: Sun, 7 Mar 2021 11:37:19 +0500 Subject: [PATCH 055/204] ci: Speed up Autotools tests We run them twice. --- .github/workflows/action.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index edbbc233..c646921f 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -198,9 +198,9 @@ jobs: run: | ./autogen.sh if [[ "${CC}" == "clang" ]]; then - ./configure --enable-werror && make clean all check && make distcheck + ./configure --enable-werror && make distcheck else - Scripts/asan-configure.sh --enable-werror && make clean all check && make distcheck + Scripts/asan-configure.sh --enable-werror && make distcheck fi - name: Configure, build and test with CMake From b46de402d5475e2dc6b8f804fce0e5b130365af3 Mon Sep 17 00:00:00 2001 From: brianlheim Date: Sun, 28 Feb 2021 12:27:08 +0100 Subject: [PATCH 056/204] Revert "Fix zero-as-null-pointer-constant-warning" This reverts commit 2269af84173c1f70b6d700fa167e1e8ea01d7247. The code which defined nullptr mysteriously broke the Windows build for SuperCollider. After some investigation I discovered this commit to be the cause, as MSVC by default does not set __cplusplus to anything greater than 199711L unless explicitly asked to. In general, #defining a keyword like nullptr in a library header is not courteous, as it can affect compilation of later code and cause significant confusion. This was exactly the situation I encountered, and it took several days to track down the source. The stated reason for the commit reverted by this was to prevent a specific warning from appearing during compilation. However, there are a few less intrusive ways of accomplishing this. For example, by using `#pragma`s offered by all major compilers which temporarily disable warnings, or by including headers with `-isystem` or any similar mechanism which disables warnings from "system" headers. For code which is intended to support C++98 and newer standards, preventing all compilers from emitting any warnings is a losing battle. It is easier in my view for consuming code to handle this as it needs to. There are several options here for resolving this. I could just fix my project, but that would not prevent others from encountering the same issue. I could try for a clever-er version of this using a macro not named nullptr and an additional check for MSVC, but this seems to offer diminishing returns considering how easily any warnings about NULL can be silenced, and carries the risk of introducing new and unforeseen problems. So, I have here simply reverted the commit so the code uses NULL once again. --- include/sndfile.hh | 54 +++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/include/sndfile.hh b/include/sndfile.hh index dc1df391..41a71e16 100644 --- a/include/sndfile.hh +++ b/include/sndfile.hh @@ -57,12 +57,6 @@ #include #include // for std::nothrow -#if __cplusplus < 201100 -#ifndef nullptr -#define nullptr NULL -#endif -#endif - class SndfileHandle { private : struct SNDFILE_ref @@ -78,7 +72,7 @@ class SndfileHandle public : /* Default constructor */ - SndfileHandle (void) : p (nullptr) {} ; + SndfileHandle (void) : p (NULL) {} ; SndfileHandle (const char *path, int mode = SFM_READ, int format = 0, int channels = 0, int samplerate = 0) ; SndfileHandle (std::string const & path, int mode = SFM_READ, @@ -99,9 +93,9 @@ class SndfileHandle SndfileHandle & operator = (const SndfileHandle &rhs) ; /* Mainly for debugging/testing. */ - int refCount (void) const { return (p == nullptr) ? 0 : p->ref ; } + int refCount (void) const { return (p == NULL) ? 0 : p->ref ; } - operator bool () const { return (p != nullptr) ; } + operator bool () const { return (p != NULL) ; } bool operator == (const SndfileHandle &rhs) const { return (p == rhs.p) ; } @@ -161,20 +155,20 @@ class SndfileHandle inline SndfileHandle::SNDFILE_ref::SNDFILE_ref (void) -: sf (nullptr), sfinfo (), ref (1) +: sf (NULL), sfinfo (), ref (1) {} inline SndfileHandle::SNDFILE_ref::~SNDFILE_ref (void) -{ if (sf != nullptr) sf_close (sf) ; } +{ if (sf != NULL) sf_close (sf) ; } inline SndfileHandle::SndfileHandle (const char *path, int mode, int fmt, int chans, int srate) -: p (nullptr) +: p (NULL) { p = new (std::nothrow) SNDFILE_ref () ; - if (p != nullptr) + if (p != NULL) { p->ref = 1 ; p->sfinfo.frames = 0 ; @@ -192,11 +186,11 @@ SndfileHandle::SndfileHandle (const char *path, int mode, int fmt, int chans, in inline SndfileHandle::SndfileHandle (std::string const & path, int mode, int fmt, int chans, int srate) -: p (nullptr) +: p (NULL) { p = new (std::nothrow) SNDFILE_ref () ; - if (p != nullptr) + if (p != NULL) { p->ref = 1 ; p->sfinfo.frames = 0 ; @@ -214,14 +208,14 @@ SndfileHandle::SndfileHandle (std::string const & path, int mode, int fmt, int c inline SndfileHandle::SndfileHandle (int fd, bool close_desc, int mode, int fmt, int chans, int srate) -: p (nullptr) +: p (NULL) { if (fd < 0) return ; p = new (std::nothrow) SNDFILE_ref () ; - if (p != nullptr) + if (p != NULL) { p->ref = 1 ; p->sfinfo.frames = 0 ; @@ -239,11 +233,11 @@ SndfileHandle::SndfileHandle (int fd, bool close_desc, int mode, int fmt, int ch inline SndfileHandle::SndfileHandle (SF_VIRTUAL_IO &sfvirtual, void *user_data, int mode, int fmt, int chans, int srate) -: p (nullptr) +: p (NULL) { p = new (std::nothrow) SNDFILE_ref () ; - if (p != nullptr) + if (p != NULL) { p->ref = 1 ; p->sfinfo.frames = 0 ; @@ -261,7 +255,7 @@ SndfileHandle::SndfileHandle (SF_VIRTUAL_IO &sfvirtual, void *user_data, int mod inline SndfileHandle::~SndfileHandle (void) -{ if (p != nullptr && -- p->ref == 0) +{ if (p != NULL && -- p->ref == 0) delete p ; } /* SndfileHandle destructor */ @@ -269,7 +263,7 @@ SndfileHandle::~SndfileHandle (void) inline SndfileHandle::SndfileHandle (const SndfileHandle &orig) : p (orig.p) -{ if (p != nullptr) +{ if (p != NULL) ++ p->ref ; } /* SndfileHandle copy constructor */ @@ -278,11 +272,11 @@ SndfileHandle::operator = (const SndfileHandle &rhs) { if (&rhs == this) return *this ; - if (p != nullptr && -- p->ref == 0) + if (p != NULL && -- p->ref == 0) delete p ; p = rhs.p ; - if (p != nullptr) + if (p != NULL) ++ p->ref ; return *this ; @@ -407,18 +401,18 @@ SndfileHandle::writeRaw (const void *ptr, sf_count_t bytes) inline SNDFILE * SndfileHandle::rawHandle (void) -{ return (p ? p->sf : nullptr) ; } +{ return (p ? p->sf : NULL) ; } inline SNDFILE * SndfileHandle::takeOwnership (void) { - if (p == nullptr || (p->ref != 1)) - return nullptr ; + if (p == NULL || (p->ref != 1)) + return NULL ; SNDFILE * sf = p->sf ; - p->sf = nullptr ; + p->sf = NULL ; delete p ; - p = nullptr ; + p = NULL ; return sf ; } @@ -426,11 +420,11 @@ SndfileHandle::takeOwnership (void) inline SndfileHandle::SndfileHandle (const wchar_t *wpath, int mode, int fmt, int chans, int srate) -: p (nullptr) +: p (NULL) { p = new (std::nothrow) SNDFILE_ref () ; - if (p != nullptr) + if (p != NULL) { p->ref = 1 ; p->sfinfo.frames = 0 ; From 1c48ce31f6d8ab7b3f5fa1f8ef6af4b0c304c62b Mon Sep 17 00:00:00 2001 From: brianlheim Date: Sun, 7 Mar 2021 17:50:45 +0100 Subject: [PATCH 057/204] sndfile.hh: use nullptr when available --- include/sndfile.hh | 54 +++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/include/sndfile.hh b/include/sndfile.hh index 41a71e16..d781a173 100644 --- a/include/sndfile.hh +++ b/include/sndfile.hh @@ -57,6 +57,12 @@ #include #include // for std::nothrow +#if ((defined (_MSC_VER) && (_MSC_VER >= 1600)) || (__cplusplus >= 201100L)) +#define SF_NULL nullptr +#else +#define SF_NULL NULL +#endif + class SndfileHandle { private : struct SNDFILE_ref @@ -72,7 +78,7 @@ class SndfileHandle public : /* Default constructor */ - SndfileHandle (void) : p (NULL) {} ; + SndfileHandle (void) : p (SF_NULL) {} ; SndfileHandle (const char *path, int mode = SFM_READ, int format = 0, int channels = 0, int samplerate = 0) ; SndfileHandle (std::string const & path, int mode = SFM_READ, @@ -93,9 +99,9 @@ class SndfileHandle SndfileHandle & operator = (const SndfileHandle &rhs) ; /* Mainly for debugging/testing. */ - int refCount (void) const { return (p == NULL) ? 0 : p->ref ; } + int refCount (void) const { return (p == SF_NULL) ? 0 : p->ref ; } - operator bool () const { return (p != NULL) ; } + operator bool () const { return (p != SF_NULL) ; } bool operator == (const SndfileHandle &rhs) const { return (p == rhs.p) ; } @@ -155,20 +161,20 @@ class SndfileHandle inline SndfileHandle::SNDFILE_ref::SNDFILE_ref (void) -: sf (NULL), sfinfo (), ref (1) +: sf (SF_NULL), sfinfo (), ref (1) {} inline SndfileHandle::SNDFILE_ref::~SNDFILE_ref (void) -{ if (sf != NULL) sf_close (sf) ; } +{ if (sf != SF_NULL) sf_close (sf) ; } inline SndfileHandle::SndfileHandle (const char *path, int mode, int fmt, int chans, int srate) -: p (NULL) +: p (SF_NULL) { p = new (std::nothrow) SNDFILE_ref () ; - if (p != NULL) + if (p != SF_NULL) { p->ref = 1 ; p->sfinfo.frames = 0 ; @@ -186,11 +192,11 @@ SndfileHandle::SndfileHandle (const char *path, int mode, int fmt, int chans, in inline SndfileHandle::SndfileHandle (std::string const & path, int mode, int fmt, int chans, int srate) -: p (NULL) +: p (SF_NULL) { p = new (std::nothrow) SNDFILE_ref () ; - if (p != NULL) + if (p != SF_NULL) { p->ref = 1 ; p->sfinfo.frames = 0 ; @@ -208,14 +214,14 @@ SndfileHandle::SndfileHandle (std::string const & path, int mode, int fmt, int c inline SndfileHandle::SndfileHandle (int fd, bool close_desc, int mode, int fmt, int chans, int srate) -: p (NULL) +: p (SF_NULL) { if (fd < 0) return ; p = new (std::nothrow) SNDFILE_ref () ; - if (p != NULL) + if (p != SF_NULL) { p->ref = 1 ; p->sfinfo.frames = 0 ; @@ -233,11 +239,11 @@ SndfileHandle::SndfileHandle (int fd, bool close_desc, int mode, int fmt, int ch inline SndfileHandle::SndfileHandle (SF_VIRTUAL_IO &sfvirtual, void *user_data, int mode, int fmt, int chans, int srate) -: p (NULL) +: p (SF_NULL) { p = new (std::nothrow) SNDFILE_ref () ; - if (p != NULL) + if (p != SF_NULL) { p->ref = 1 ; p->sfinfo.frames = 0 ; @@ -255,7 +261,7 @@ SndfileHandle::SndfileHandle (SF_VIRTUAL_IO &sfvirtual, void *user_data, int mod inline SndfileHandle::~SndfileHandle (void) -{ if (p != NULL && -- p->ref == 0) +{ if (p != SF_NULL && -- p->ref == 0) delete p ; } /* SndfileHandle destructor */ @@ -263,7 +269,7 @@ SndfileHandle::~SndfileHandle (void) inline SndfileHandle::SndfileHandle (const SndfileHandle &orig) : p (orig.p) -{ if (p != NULL) +{ if (p != SF_NULL) ++ p->ref ; } /* SndfileHandle copy constructor */ @@ -272,11 +278,11 @@ SndfileHandle::operator = (const SndfileHandle &rhs) { if (&rhs == this) return *this ; - if (p != NULL && -- p->ref == 0) + if (p != SF_NULL && -- p->ref == 0) delete p ; p = rhs.p ; - if (p != NULL) + if (p != SF_NULL) ++ p->ref ; return *this ; @@ -401,18 +407,18 @@ SndfileHandle::writeRaw (const void *ptr, sf_count_t bytes) inline SNDFILE * SndfileHandle::rawHandle (void) -{ return (p ? p->sf : NULL) ; } +{ return (p ? p->sf : SF_NULL) ; } inline SNDFILE * SndfileHandle::takeOwnership (void) { - if (p == NULL || (p->ref != 1)) - return NULL ; + if (p == SF_NULL || (p->ref != 1)) + return SF_NULL ; SNDFILE * sf = p->sf ; - p->sf = NULL ; + p->sf = SF_NULL ; delete p ; - p = NULL ; + p = SF_NULL ; return sf ; } @@ -420,11 +426,11 @@ SndfileHandle::takeOwnership (void) inline SndfileHandle::SndfileHandle (const wchar_t *wpath, int mode, int fmt, int chans, int srate) -: p (NULL) +: p (SF_NULL) { p = new (std::nothrow) SNDFILE_ref () ; - if (p != NULL) + if (p != SF_NULL) { p->ref = 1 ; p->sfinfo.frames = 0 ; From 61b5343584bad9928c0393a24e10df3ee0dd8695 Mon Sep 17 00:00:00 2001 From: brianlheim Date: Sun, 7 Mar 2021 17:51:06 +0100 Subject: [PATCH 058/204] sndfile.hh: add portability documentation --- include/sndfile.hh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/sndfile.hh b/include/sndfile.hh index d781a173..a8db0de8 100644 --- a/include/sndfile.hh +++ b/include/sndfile.hh @@ -47,6 +47,8 @@ ** ** API documentation is in the doc/ directory of the source code tarball ** and at http://libsndfile.github.io/libsndfile/api.html. +** +** This file is intended to compile with C++98 and newer. */ #ifndef SNDFILE_HH From e8293c29da1ce2ed75eae087a29c53e2c7e4ae1a Mon Sep 17 00:00:00 2001 From: evpobr Date: Sun, 14 Mar 2021 19:24:25 +0500 Subject: [PATCH 059/204] pcm: Wrap CPU endian specific code in #ifdefs --- src/pcm.c | 70 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 29 deletions(-) diff --git a/src/pcm.c b/src/pcm.c index 7e05219c..79f194f2 100644 --- a/src/pcm.c +++ b/src/pcm.c @@ -134,10 +134,11 @@ pcm_init (SF_PRIVATE *psf) else if ((SF_CODEC (psf->sf.format)) == SF_FORMAT_PCM_U8) chars = SF_CHARS_UNSIGNED ; - if (CPU_IS_BIG_ENDIAN) - psf->data_endswap = (psf->endian == SF_ENDIAN_BIG) ? SF_FALSE : SF_TRUE ; - else - psf->data_endswap = (psf->endian == SF_ENDIAN_LITTLE) ? SF_FALSE : SF_TRUE ; +#if CPU_IS_BIG_ENDIAN + psf->data_endswap = (psf->endian == SF_ENDIAN_BIG) ? SF_FALSE : SF_TRUE ; +#else + psf->data_endswap = (psf->endian == SF_ENDIAN_LITTLE) ? SF_FALSE : SF_TRUE ; +#endif if (psf->file.mode == SFM_READ || psf->file.mode == SFM_RDWR) { switch (psf->bytewidth * 0x10000 + psf->endian + chars) @@ -710,8 +711,9 @@ pcm_read_bes2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) { int total ; total = (int) psf_fread (ptr, sizeof (short), len, psf) ; - if (CPU_IS_LITTLE_ENDIAN) - endswap_short_array (ptr, len) ; +#if CPU_IS_LITTLE_ENDIAN + endswap_short_array (ptr, len) ; +#endif return total ; } /* pcm_read_bes2s */ @@ -721,8 +723,9 @@ pcm_read_les2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) { int total ; total = psf_fread (ptr, sizeof (short), len, psf) ; - if (CPU_IS_BIG_ENDIAN) - endswap_short_array (ptr, len) ; +#if CPU_IS_BIG_ENDIAN + endswap_short_array (ptr, len) ; +#endif return total ; } /* pcm_read_les2s */ @@ -955,8 +958,9 @@ pcm_read_bei2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) { int total ; total = psf_fread (ptr, sizeof (int), len, psf) ; - if (CPU_IS_LITTLE_ENDIAN) - endswap_int_array (ptr, len) ; +#if CPU_IS_LITTLE_ENDIAN + endswap_int_array (ptr, len) ; +#endif return total ; } /* pcm_read_bei2i */ @@ -966,8 +970,9 @@ pcm_read_lei2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) { int total ; total = psf_fread (ptr, sizeof (int), len, psf) ; - if (CPU_IS_BIG_ENDIAN) - endswap_int_array (ptr, len) ; +#if CPU_IS_BIG_ENDIAN + endswap_int_array (ptr, len) ; +#endif return total ; } /* pcm_read_lei2i */ @@ -1432,14 +1437,14 @@ pcm_write_s2uc (SF_PRIVATE *psf, const short *ptr, sf_count_t len) static sf_count_t pcm_write_s2bes (SF_PRIVATE *psf, const short *ptr, sf_count_t len) -{ BUF_UNION ubuf ; +{ +#if CPU_IS_BIG_ENDIAN + return psf_fwrite (ptr, sizeof (short), len, psf) ; +#else + BUF_UNION ubuf ; int bufferlen, writecount ; sf_count_t total = 0 ; - if (CPU_IS_BIG_ENDIAN) - return psf_fwrite (ptr, sizeof (short), len, psf) ; - else - bufferlen = ARRAY_LEN (ubuf.sbuf) ; while (len > 0) @@ -1454,17 +1459,19 @@ pcm_write_s2bes (SF_PRIVATE *psf, const short *ptr, sf_count_t len) } ; return total ; +#endif } /* pcm_write_s2bes */ static sf_count_t pcm_write_s2les (SF_PRIVATE *psf, const short *ptr, sf_count_t len) -{ BUF_UNION ubuf ; +{ +#if CPU_IS_LITTLE_ENDIAN + return psf_fwrite (ptr, sizeof (short), len, psf) ; +#else + BUF_UNION ubuf ; int bufferlen, writecount ; sf_count_t total = 0 ; - if (CPU_IS_LITTLE_ENDIAN) - return psf_fwrite (ptr, sizeof (short), len, psf) ; - bufferlen = ARRAY_LEN (ubuf.sbuf) ; while (len > 0) @@ -1479,6 +1486,7 @@ pcm_write_s2les (SF_PRIVATE *psf, const short *ptr, sf_count_t len) } ; return total ; +#endif } /* pcm_write_s2les */ static sf_count_t @@ -1706,13 +1714,14 @@ pcm_write_i2let (SF_PRIVATE *psf, const int *ptr, sf_count_t len) static sf_count_t pcm_write_i2bei (SF_PRIVATE *psf, const int *ptr, sf_count_t len) -{ BUF_UNION ubuf ; +{ +#if CPU_IS_BIG_ENDIAN + return psf_fwrite (ptr, sizeof (int), len, psf) ; +#else + BUF_UNION ubuf ; int bufferlen, writecount ; sf_count_t total = 0 ; - if (CPU_IS_BIG_ENDIAN) - return psf_fwrite (ptr, sizeof (int), len, psf) ; - bufferlen = ARRAY_LEN (ubuf.ibuf) ; while (len > 0) @@ -1727,17 +1736,19 @@ pcm_write_i2bei (SF_PRIVATE *psf, const int *ptr, sf_count_t len) } ; return total ; +#endif } /* pcm_write_i2bei */ static sf_count_t pcm_write_i2lei (SF_PRIVATE *psf, const int *ptr, sf_count_t len) -{ BUF_UNION ubuf ; +{ +#if CPU_IS_LITTLE_ENDIAN + return psf_fwrite (ptr, sizeof (int), len, psf) ; +#else + BUF_UNION ubuf ; int bufferlen, writecount ; sf_count_t total = 0 ; - if (CPU_IS_LITTLE_ENDIAN) - return psf_fwrite (ptr, sizeof (int), len, psf) ; - bufferlen = ARRAY_LEN (ubuf.ibuf) ; while (len > 0) @@ -1752,6 +1763,7 @@ pcm_write_i2lei (SF_PRIVATE *psf, const int *ptr, sf_count_t len) } ; return total ; +#endif } /* pcm_write_i2lei */ /*------------------------------------------------------------------------------ From dadf54b91e3146936ebbe5d1502069feb0e2e904 Mon Sep 17 00:00:00 2001 From: evpobr Date: Sun, 14 Mar 2021 21:50:48 +0500 Subject: [PATCH 060/204] ci: Fix test failure --- tests/generate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/generate.c b/tests/generate.c index 8ed6a6de..b49b2718 100644 --- a/tests/generate.c +++ b/tests/generate.c @@ -51,7 +51,7 @@ generate_file (const char * filename, int format, int len) static inline float rand_float (void) -{ return rand () / (0.5f * RAND_MAX) - 1.0f ; +{ return rand () / (0.5f * (float) RAND_MAX) - 1.0f ; } /* rand_float */ static float From 9dee1e060b8dd0ba359e62e5e6f33904db80261b Mon Sep 17 00:00:00 2001 From: evpobr Date: Mon, 15 Mar 2021 16:02:16 +0500 Subject: [PATCH 061/204] aiff: Fix typos --- src/aiff.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/aiff.c b/src/aiff.c index a157576a..7ad03126 100644 --- a/src/aiff.c +++ b/src/aiff.c @@ -507,7 +507,7 @@ aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt) if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL) return SFE_MALLOC_FAILED ; - /* read in rest of PEAK chunk. */ + /* Read in rest of PEAK chunk. */ psf_binheader_readf (psf, "E44", &(psf->peak_info->version), &(psf->peak_info->timestamp)) ; if (psf->peak_info->version != 1) @@ -1003,7 +1003,7 @@ aiff_read_comm_chunk (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt) ubuf.scbuf [0] = 0 ; /* The COMM chunk has an int aligned to an odd word boundary. Some - ** procesors are not able to deal with this (ie bus fault) so we have + ** processors are not able to deal with this (ie bus fault) so we have ** to take special care. */ From 19a44987360f8a521271ff6d5bec45d9692a191a Mon Sep 17 00:00:00 2001 From: evpobr Date: Tue, 16 Mar 2021 16:26:29 +0500 Subject: [PATCH 062/204] sndfile-deinterleave: Fix resource cleanup --- programs/sndfile-deinterleave.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/programs/sndfile-deinterleave.c b/programs/sndfile-deinterleave.c index ade29629..26d4a3ec 100644 --- a/programs/sndfile-deinterleave.c +++ b/programs/sndfile-deinterleave.c @@ -154,15 +154,17 @@ main (int argc, char **argv) else deinterleave_int (state) ; - sf_close (state->infile) ; - for (ch = 0 ; ch < MAX_CHANNELS ; ch++) - if (state->outfile [ch] != NULL) - sf_close (state->outfile [ch]) ; - ret = 0 ; cleanup : + if (state != NULL) + { sf_close (state->infile) ; + for (ch = 0 ; ch < MAX_CHANNELS ; ch++) + if (state->outfile [ch] != NULL) + sf_close (state->outfile [ch]) ; + } ; + free (state) ; return ret ; @@ -181,7 +183,6 @@ usage_exit (void) "a_00.wav, a_01.wav and so on.\n" ) ; printf ("Using %s.\n\n", sf_version_string ()) ; - exit (1) ; } /* usage_exit */ static void From 6fb03135630bfb0b737d3f558da84dfc847c8d31 Mon Sep 17 00:00:00 2001 From: evpobr Date: Tue, 16 Mar 2021 16:29:55 +0500 Subject: [PATCH 063/204] sndfile-interleave: Fix large stack allocation --- programs/sndfile-interleave.c | 61 ++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/programs/sndfile-interleave.c b/programs/sndfile-interleave.c index 07f1b488..2b44efb2 100644 --- a/programs/sndfile-interleave.c +++ b/programs/sndfile-interleave.c @@ -60,46 +60,54 @@ typedef struct } STATE ; -static void usage_exit (void) ; +static void print_usage (void) ; static void interleave_int (STATE * state) ; static void interleave_double (STATE * state) ; int main (int argc, char **argv) -{ STATE state ; +{ STATE *state = NULL ; SF_INFO sfinfo ; int k, double_merge = 0 ; + int ret = 1 ; if (argc < 5) { if (argc > 1) puts ("\nError : need at least 2 input files.") ; - usage_exit () ; + print_usage () ; + goto cleanup ; } ; if (strcmp (argv [argc - 2], "-o") != 0) { puts ("\nError : second last command line parameter should be '-o'.\n") ; - usage_exit () ; + print_usage () ; + goto cleanup ; } ; if (argc - 3 > MAX_INPUTS) { printf ("\nError : Cannot handle more than %d input channels.\n\n", MAX_INPUTS) ; - exit (1) ; + goto cleanup ; + } ; + + state = calloc (1, sizeof (STATE)) ; + if (state == NULL) + { puts ("\nError : out of memory.\n") ; + goto cleanup ; } ; - memset (&state, 0, sizeof (state)) ; memset (&sfinfo, 0, sizeof (sfinfo)) ; for (k = 1 ; k < argc - 2 ; k++) { - if ((state.infile [k - 1] = sf_open (argv [k], SFM_READ, &sfinfo)) == NULL) + if ((state->infile [k - 1] = sf_open (argv [k], SFM_READ, &sfinfo)) == NULL) { printf ("\nError : Not able to open input file '%s'\n%s\n", argv [k], sf_strerror (NULL)) ; - exit (1) ; + goto cleanup ; } ; if (sfinfo.channels != 1) { printf ("\bError : Input file '%s' should be mono (has %d channels).\n", argv [k], sfinfo.channels) ; - exit (1) ; + goto cleanup ; } ; switch (sfinfo.format & SF_FORMAT_SUBMASK) @@ -113,28 +121,36 @@ main (int argc, char **argv) break ; } ; - state.channels ++ ; + state->channels ++ ; } ; - sfinfo.channels = state.channels ; + sfinfo.channels = state->channels ; sfinfo.format = sfe_file_type_of_ext (argv [argc - 1], sfinfo.format) ; - if ((state.outfile = sf_open (argv [argc - 1], SFM_WRITE, &sfinfo)) == NULL) + if ((state->outfile = sf_open (argv [argc - 1], SFM_WRITE, &sfinfo)) == NULL) { printf ("Not able to open output file '%s'\n%s\n", argv [argc - 1], sf_strerror (NULL)) ; - exit (1) ; + goto cleanup ; } ; if (double_merge) - interleave_double (&state) ; + interleave_double (state) ; else - interleave_int (&state) ; + interleave_int (state) ; - for (k = 0 ; k < MAX_INPUTS ; k++) - if (state.infile [k] != NULL) - sf_close (state.infile [k]) ; - sf_close (state.outfile) ; + ret = 0 ; - return 0 ; +cleanup : + + if (state != NULL) + { for (k = 0 ; k < MAX_INPUTS ; k++) + if (state->infile [k] != NULL) + sf_close (state->infile [k]) ; + sf_close (state->outfile) ; + } + + free (state) ; + + return ret ; } /* main */ /*------------------------------------------------------------------------------ @@ -142,12 +158,11 @@ main (int argc, char **argv) static void -usage_exit (void) +print_usage (void) { puts ("\nUsage : sndfile-interleave ... -o \n") ; puts ("Merge two or more mono files into a single multi-channel file.\n") ; printf ("Using %s.\n\n", sf_version_string ()) ; - exit (1) ; -} /* usage_exit */ +} /* print_usage */ static void From 8ff8ed6967c8135398a704ee42c06a428a5172d8 Mon Sep 17 00:00:00 2001 From: evpobr Date: Tue, 16 Mar 2021 16:54:17 +0500 Subject: [PATCH 064/204] sndfile-to-text: Fix large stack allocation --- examples/sndfile-to-text.c | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/examples/sndfile-to-text.c b/examples/sndfile-to-text.c index 25767942..a76bb48c 100644 --- a/examples/sndfile-to-text.c +++ b/examples/sndfile-to-text.c @@ -60,12 +60,18 @@ print_usage (char *progname) } /* print_usage */ -static void +static int convert_to_text (SNDFILE * infile, FILE * outfile, int channels, int full_precision) -{ float buf [BLOCK_SIZE] ; +{ float *buf ; sf_count_t frames ; int k, m, readcount ; + buf = malloc (BLOCK_SIZE * sizeof (float)) ; + if (buf == NULL) + { printf ("Error : Out of memory.\n\n") ; + return 1 ; + } ; + frames = BLOCK_SIZE / channels ; while ((readcount = (int) sf_readf_float (infile, buf, frames)) > 0) @@ -79,7 +85,9 @@ convert_to_text (SNDFILE * infile, FILE * outfile, int channels, int full_precis } ; } ; - return ; + free (buf) ; + + return 0 ; } /* convert_to_text */ int @@ -89,6 +97,7 @@ main (int argc, char * argv []) FILE *outfile = NULL ; SF_INFO sfinfo ; int full_precision = 0 ; + int ret = 1 ; progname = strrchr (argv [0], '/') ; progname = progname ? progname + 1 : argv [0] ; @@ -97,7 +106,7 @@ main (int argc, char * argv []) { case 4 : if (!strcmp ("--full-precision", argv [3])) { print_usage (progname) ; - return 1 ; + goto cleanup ; } ; full_precision = 1 ; argv++ ; @@ -105,7 +114,7 @@ main (int argc, char * argv []) break ; default: print_usage (progname) ; - return 1 ; + goto cleanup ; } ; infilename = argv [1] ; @@ -114,19 +123,19 @@ main (int argc, char * argv []) if (strcmp (infilename, outfilename) == 0) { printf ("Error : Input and output filenames are the same.\n\n") ; print_usage (progname) ; - return 1 ; + goto cleanup ; } ; if (infilename [0] == '-') { printf ("Error : Input filename (%s) looks like an option.\n\n", infilename) ; print_usage (progname) ; - return 1 ; + goto cleanup ; } ; if (outfilename [0] == '-') { printf ("Error : Output filename (%s) looks like an option.\n\n", outfilename) ; print_usage (progname) ; - return 1 ; + goto cleanup ; } ; memset (&sfinfo, 0, sizeof (sfinfo)) ; @@ -134,22 +143,25 @@ main (int argc, char * argv []) if ((infile = sf_open (infilename, SFM_READ, &sfinfo)) == NULL) { printf ("Not able to open input file %s.\n", infilename) ; puts (sf_strerror (NULL)) ; - return 1 ; + goto cleanup ; } ; /* Open the output file. */ if ((outfile = fopen (outfilename, "w")) == NULL) { printf ("Not able to open output file %s : %s\n", outfilename, sf_strerror (NULL)) ; - return 1 ; + goto cleanup ; } ; fprintf (outfile, "# Converted from file %s.\n", infilename) ; fprintf (outfile, "# Channels %d, Sample rate %d\n", sfinfo.channels, sfinfo.samplerate) ; - convert_to_text (infile, outfile, sfinfo.channels, full_precision) ; + ret = convert_to_text (infile, outfile, sfinfo.channels, full_precision) ; + +cleanup : sf_close (infile) ; - fclose (outfile) ; + if (outfile != NULL) + fclose (outfile) ; return 0 ; } /* main */ From c0c81a5bde33a2fb84227dbfb8f8546bfddebff9 Mon Sep 17 00:00:00 2001 From: evpobr Date: Tue, 16 Mar 2021 18:56:26 +0500 Subject: [PATCH 065/204] sndfile-to-text: Return proper error code --- examples/sndfile-to-text.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/sndfile-to-text.c b/examples/sndfile-to-text.c index a76bb48c..58ee0694 100644 --- a/examples/sndfile-to-text.c +++ b/examples/sndfile-to-text.c @@ -163,6 +163,6 @@ cleanup : if (outfile != NULL) fclose (outfile) ; - return 0 ; + return ret ; } /* main */ From 40fdc3121ba2804a2b1b081cb06f335f855ebe6b Mon Sep 17 00:00:00 2001 From: evpobr Date: Wed, 17 Mar 2021 10:58:10 +0500 Subject: [PATCH 066/204] common: Fix initialized va_list leaks Thanks to clang-tidy. --- src/common.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/common.c b/src/common.c index b65b9158..18279f47 100644 --- a/src/common.c +++ b/src/common.c @@ -571,7 +571,7 @@ psf_binheader_writef (SF_PRIVATE *psf, const char *format, ...) while ((c = *format++)) { if (psf->header.indx + 16 >= psf->header.len && psf_bump_header_allocation (psf, 16)) - return count ; + break ; switch (c) { case ' ' : /* Do nothing. Just used to space out format string. */ @@ -687,7 +687,7 @@ psf_binheader_writef (SF_PRIVATE *psf, const char *format, ...) size = strlen (strptr) + 1 ; if (psf->header.indx + 4 + (sf_count_t) size + (sf_count_t) (size & 1) > psf->header.len && psf_bump_header_allocation (psf, 4 + size + (size & 1))) - return count ; + break ; if (psf->rwf_endian == SF_ENDIAN_BIG) header_put_be_int (psf, size + (size & 1)) ; @@ -708,7 +708,7 @@ psf_binheader_writef (SF_PRIVATE *psf, const char *format, ...) strptr = va_arg (argptr, char *) ; size = strlen (strptr) ; if (psf->header.indx + 4 + (sf_count_t) size + (sf_count_t) (size & 1) > psf->header.len && psf_bump_header_allocation (psf, 4 + size + (size & 1))) - return count ; + break ; if (psf->rwf_endian == SF_ENDIAN_BIG) header_put_be_int (psf, size) ; else @@ -728,7 +728,7 @@ psf_binheader_writef (SF_PRIVATE *psf, const char *format, ...) size = (size > 254) ? 254 : size ; if (psf->header.indx + 1 + (sf_count_t) size > psf->header.len && psf_bump_header_allocation (psf, 1 + size)) - return count ; + break ; header_put_byte (psf, size) ; memcpy (&(psf->header.ptr [psf->header.indx]), strptr, size) ; @@ -741,7 +741,7 @@ psf_binheader_writef (SF_PRIVATE *psf, const char *format, ...) size = va_arg (argptr, size_t) ; if (psf->header.indx + (sf_count_t) size > psf->header.len && psf_bump_header_allocation (psf, size)) - return count ; + break ; memcpy (&(psf->header.ptr [psf->header.indx]), bindata, size) ; psf->header.indx += size ; @@ -752,7 +752,7 @@ psf_binheader_writef (SF_PRIVATE *psf, const char *format, ...) size = va_arg (argptr, size_t) ; if (psf->header.indx + (sf_count_t) size > psf->header.len && psf_bump_header_allocation (psf, size)) - return count ; + break ; count += size ; while (size) @@ -773,7 +773,7 @@ psf_binheader_writef (SF_PRIVATE *psf, const char *format, ...) size = va_arg (argptr, size_t) ; if (psf->header.indx + (sf_count_t) size > psf->header.len && psf_bump_header_allocation (psf, size)) - return count ; + break ; psf->header.indx += size ; count += size ; @@ -783,7 +783,7 @@ psf_binheader_writef (SF_PRIVATE *psf, const char *format, ...) size = va_arg (argptr, size_t) ; if ((sf_count_t) size >= psf->header.len && psf_bump_header_allocation (psf, size)) - return count ; + break ; psf->header.indx = size ; break ; @@ -971,7 +971,7 @@ psf_binheader_readf (SF_PRIVATE *psf, char const *format, ...) while ((c = *format++)) { if (psf->header.indx + 16 >= psf->header.len && psf_bump_header_allocation (psf, 16)) - return count ; + break ; switch (c) { case 'e' : /* All conversions are now from LE to host. */ @@ -1098,7 +1098,7 @@ psf_binheader_readf (SF_PRIVATE *psf, char const *format, ...) memset (charptr, 0, count) ; if (psf->header.indx + count >= psf->header.len && psf_bump_header_allocation (psf, count)) - return 0 ; + break ; byte_count += header_gets (psf, charptr, count) ; break ; From b1d642078f1278f8a1be20ff1f9206415fd34100 Mon Sep 17 00:00:00 2001 From: evpobr Date: Thu, 18 Mar 2021 10:28:48 +0500 Subject: [PATCH 067/204] sndfile: Improve SF_INFO correctness check Limit maximum `samplerate` and `channels` values. Update API documentation to reflect changes. This should also fix some possible integer overflows as: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=28441 Credit to OSS-Fuzz. --- docs/api.md | 5 +++++ src/common.h | 2 ++ src/sndfile.c | 4 ++-- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/docs/api.md b/docs/api.md index f009b74d..ac21299d 100644 --- a/docs/api.md +++ b/docs/api.md @@ -97,6 +97,11 @@ calling **sf_open**(). The only exception to this is the case of RAW files where the caller has to set the **samplerate**, **channels** and **format** fields to valid values. All other fields of the structure are filled in by the library. +**Note:** The libsndfile library will reject values ​​for **samplerate** field +that are greater than `655350` and values ​​for field **channels** that are +greater than `1024`. These values ​​represent the maximum theoretical limit and +may be less for specific formats. + When opening a file for write, the caller must fill in structure members **samplerate**, **channels**, and **format**. diff --git a/src/common.h b/src/common.h index 8be6f0eb..cb82579e 100644 --- a/src/common.h +++ b/src/common.h @@ -109,6 +109,8 @@ #define SF_MAX_CHANNELS 1024 +/* Max FLAC sample rate : https://xiph.org/flac/format.html */ +#define SF_MAX_SAMPLERATE 655350 /* diff --git a/src/sndfile.c b/src/sndfile.c index f2b49f63..71f060f1 100644 --- a/src/sndfile.c +++ b/src/sndfile.c @@ -2881,11 +2881,11 @@ retry: static int validate_sfinfo (SF_INFO *sfinfo) -{ if (sfinfo->samplerate < 1) +{ if ((sfinfo->samplerate < 1) || (sfinfo->samplerate > SF_MAX_SAMPLERATE)) return 0 ; if (sfinfo->frames < 0) return 0 ; - if (sfinfo->channels < 1) + if ((sfinfo->channels < 1) || (sfinfo->channels > SF_MAX_CHANNELS)) return 0 ; if ((SF_CONTAINER (sfinfo->format)) == 0) return 0 ; From d617fafe50006b9f51bbd1b96782e8f6bf8a0476 Mon Sep 17 00:00:00 2001 From: evpobr Date: Thu, 18 Mar 2021 13:55:50 +0500 Subject: [PATCH 068/204] dwd: Use fixed width integer types --- src/dwd.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/dwd.c b/src/dwd.c index af4d9f0e..a15750d4 100644 --- a/src/dwd.c +++ b/src/dwd.c @@ -115,9 +115,9 @@ dwd_close (SF_PRIVATE * UNUSED (psf)) ** No assumptions are made about the packing of this struct. */ typedef struct -{ unsigned char major, minor, compression, channels, bitwidth ; - unsigned short srate, maxval ; - unsigned int id, datalen, frames, offset ; +{ uint8_t major, minor, compression, channels, bitwidth ; + uint16_t srate, maxval ; + uint32_t id, datalen, frames, offset ; } DWD_HEADER ; static int @@ -173,7 +173,7 @@ dwd_read_header (SF_PRIVATE *psf) if (psf->filelength != dwdh.offset + dwdh.datalen) { psf_log_printf (psf, " Data Length : %d (should be %D)\n", dwdh.datalen, psf->filelength - dwdh.offset) ; - dwdh.datalen = (unsigned int) (psf->filelength - dwdh.offset) ; + dwdh.datalen = (uint32_t) (psf->filelength - dwdh.offset) ; } else psf_log_printf (psf, " Data Length : %d\n", dwdh.datalen) ; From e85da03c6a3e05d7a12ccdd9ed6e1a0c9eb5b649 Mon Sep 17 00:00:00 2001 From: evpobr Date: Thu, 18 Mar 2021 14:36:35 +0500 Subject: [PATCH 069/204] common: Remove NOT macro --- src/common.h | 2 -- src/rf64.c | 6 +++--- src/sndfile.c | 4 ++-- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/common.h b/src/common.h index cb82579e..d8a6fb00 100644 --- a/src/common.h +++ b/src/common.h @@ -99,8 +99,6 @@ #define ARRAY_LEN(x) ((int) (sizeof (x) / sizeof ((x) [0]))) -#define NOT(x) (! (x)) - #define SF_MAX(a, b) ((a) > (b) ? (a) : (b)) #define SF_MIN(a, b) ((a) < (b) ? (a) : (b)) diff --git a/src/rf64.c b/src/rf64.c index 876cd451..123db445 100644 --- a/src/rf64.c +++ b/src/rf64.c @@ -197,7 +197,7 @@ rf64_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock) else psf_log_printf (psf, "%M : 0x%x (should be 0xFFFFFFFF)\n %M\n", RF64_MARKER, WAVE_MARKER) ; - while (NOT (done)) + while (!done) { marker = chunk_size = 0 ; psf_binheader_readf (psf, "em4", &marker, &chunk_size) ; @@ -328,7 +328,7 @@ rf64_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock) if (psf->datalength + psf->dataoffset < psf->filelength) psf->dataend = psf->datalength + psf->dataoffset ; - if (NOT (psf->sf.seekable) || psf->dataoffset < 0) + if (!psf->sf.seekable || psf->dataoffset < 0) break ; /* Seek past data and continue reading header. */ @@ -762,7 +762,7 @@ rf64_write_header (SF_PRIVATE *psf, int calc_length) psf->dataoffset = psf->header.indx ; - if (NOT (has_data)) + if (!has_data) psf_fseek (psf, psf->dataoffset, SEEK_SET) ; else if (current > 0) psf_fseek (psf, current, SEEK_SET) ; diff --git a/src/sndfile.c b/src/sndfile.c index 71f060f1..c7145c7f 100644 --- a/src/sndfile.c +++ b/src/sndfile.c @@ -1301,7 +1301,7 @@ sf_command (SNDFILE *sndfile, int command, void *data, int datasize) return SF_FALSE ; } ; - if (NOT (broadcast_var_set (psf, data, datasize))) + if (!broadcast_var_set (psf, data, datasize)) return SF_FALSE ; if (psf->write_header) @@ -1330,7 +1330,7 @@ sf_command (SNDFILE *sndfile, int command, void *data, int datasize) { psf->error = SFE_CMD_HAS_DATA ; return SF_FALSE ; } ; - if (NOT (cart_var_set (psf, data, datasize))) + if (!cart_var_set (psf, data, datasize)) return SF_FALSE ; if (psf->write_header) psf->write_header (psf, SF_TRUE) ; From 1875f49bbf75d471de72da25db66d50008311545 Mon Sep 17 00:00:00 2001 From: evpobr Date: Thu, 18 Mar 2021 14:52:14 +0500 Subject: [PATCH 070/204] common: Allow to use C99 bool --- cmake/SndFileChecks.cmake | 1 + configure.ac | 1 + src/common.h | 3 +++ src/config.h.cmake | 3 +++ 4 files changed, 8 insertions(+) diff --git a/cmake/SndFileChecks.cmake b/cmake/SndFileChecks.cmake index 9b52d152..c0d09910 100644 --- a/cmake/SndFileChecks.cmake +++ b/cmake/SndFileChecks.cmake @@ -70,6 +70,7 @@ check_include_file (sys/time.h HAVE_SYS_TIME_H) check_include_file (sys/types.h HAVE_SYS_TYPES_H) check_include_file (unistd.h HAVE_UNISTD_H) check_include_file (immintrin.h HAVE_IMMINTRIN_H) +check_include_file (stdbool.h HAVE_STDBOOL_H) check_cpu_arch_x86 (CPU_IS_X86) check_cpu_arch_x64 (CPU_IS_X64) diff --git a/configure.ac b/configure.ac index 78a79da5..ef6c2cc5 100644 --- a/configure.ac +++ b/configure.ac @@ -95,6 +95,7 @@ AC_CHECK_HEADERS([byteswap.h]) AC_CHECK_HEADERS([locale.h]) AC_CHECK_HEADERS([sys/time.h]) AC_CHECK_HEADERS([immintrin.h]) +AC_CHECK_HEADERS([stdbool.h]) AC_HEADER_SYS_WAIT diff --git a/src/common.h b/src/common.h index d8a6fb00..16f515d1 100644 --- a/src/common.h +++ b/src/common.h @@ -23,6 +23,9 @@ #include #include +#if HAVE_STDBOOL_H +#include +#endif #if HAVE_INTTYPES_H #include diff --git a/src/config.h.cmake b/src/config.h.cmake index c33bcbc8..3d0b2f14 100644 --- a/src/config.h.cmake +++ b/src/config.h.cmake @@ -175,6 +175,9 @@ /* Define to 1 if you have the header file. */ #cmakedefine01 HAVE_IMMINTRIN_H +/* Define to 1 if you have the header file. */ +#cmakedefine01 HAVE_STDBOOL_H + /* Define to 1 if you have the `vsnprintf' function. */ #cmakedefine01 HAVE_VSNPRINTF From 4539826643f647d52d67d64ff6eda6a7315f25db Mon Sep 17 00:00:00 2001 From: evpobr Date: Fri, 19 Mar 2021 11:33:49 +0500 Subject: [PATCH 071/204] Update CHANGELOG.md --- CHANGELOG.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5c307f2..78681028 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,10 +58,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Missing samples when doing a partial read of Ogg file from index till the end of file, thanks @arthurt (issue #643). * sndfile-salvage: Handle files > 4 GB on Windows OS +* Undefined shift in `dyn_get_32bit`(), credit to OSS-Fuzz + ([issue 27366](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=27366)). +* Integer overflow in `nms_adpcm_update`(), credit to OSS-Fuzz + ([issue 25522](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=25522)). ### Security * Heap buffer overflow in `wavlike_ima_decode_block`(), thanks @bobsayshilol, credit to OSS-Fuzz ([issue 25530](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=25530)). +* Heap buffer overflow in `msadpcm_decode_block`(), thanks @bobsayshilol, + credit to OSS-Fuzz ([issue 26803](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=26803)). +* Heap buffer overflow in `psf_binheader_readf`(), thanks @bobsayshilol, + credit to OSS-Fuzz ([issue 26026](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=26026)). +* Index out of bounds in `psf_nms_adpcm_decode_block`(), credit to OSS-Fuzz + ([issue 25561](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=25561)). [Unreleased]: https://github.com/libsndfile/libsndfile/compare/1.0.31...HEAD From bde4a1364eeca952af588bfeea0ace05a27d0487 Mon Sep 17 00:00:00 2001 From: evpobr Date: Mon, 22 Mar 2021 12:39:52 +0500 Subject: [PATCH 072/204] sfendian: Use C99 variable declarations --- src/sfendian.h | 40 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/src/sfendian.h b/src/sfendian.h index 4921bb9e..b0c0cd65 100644 --- a/src/sfendian.h +++ b/src/sfendian.h @@ -222,9 +222,7 @@ psf_get_be64 (const uint8_t *ptr, int offset) static inline int64_t psf_get_le64 (const uint8_t *ptr, int offset) -{ int64_t value ; - - value = (int64_t) ((uint64_t) ptr [offset + 7] << 24) ; +{ int64_t value = (int64_t) ((uint64_t) ptr [offset + 7] << 24) ; value += (int64_t) ((uint64_t) ptr [offset + 6] << 16) ; value += (int64_t) ((uint64_t) ptr [offset + 5] << 8) ; value += ptr [offset + 4] ; @@ -240,9 +238,7 @@ psf_get_le64 (const uint8_t *ptr, int offset) static inline int32_t psf_get_be32 (const uint8_t *ptr, int offset) -{ int32_t value ; - - value = ((uint32_t) ptr [offset]) << 24 ; +{ int32_t value = ((uint32_t) ptr [offset]) << 24 ; value += ptr [offset + 1] << 16 ; value += ptr [offset + 2] << 8 ; value += ptr [offset + 3] ; @@ -251,9 +247,7 @@ psf_get_be32 (const uint8_t *ptr, int offset) static inline int32_t psf_get_le32 (const uint8_t *ptr, int offset) -{ int32_t value ; - - value = ((uint32_t) ptr [offset + 3]) << 24 ; +{ int32_t value = ((uint32_t) ptr [offset + 3]) << 24 ; value += ptr [offset + 2] << 16 ; value += ptr [offset + 1] << 8 ; value += ptr [offset] ; @@ -262,9 +256,7 @@ psf_get_le32 (const uint8_t *ptr, int offset) static inline int32_t psf_get_be24 (const uint8_t *ptr, int offset) -{ int32_t value ; - - value = ((uint32_t) ptr [offset]) << 24 ; +{ int32_t value = ((uint32_t) ptr [offset]) << 24 ; value += ptr [offset + 1] << 16 ; value += ptr [offset + 2] << 8 ; return value ; @@ -272,9 +264,7 @@ psf_get_be24 (const uint8_t *ptr, int offset) static inline int32_t psf_get_le24 (const uint8_t *ptr, int offset) -{ int32_t value ; - - value = ((uint32_t) ptr [offset + 2]) << 24 ; +{ int32_t value = ((uint32_t) ptr [offset + 2]) << 24 ; value += ptr [offset + 1] << 16 ; value += ptr [offset] << 8 ; return value ; @@ -291,10 +281,9 @@ psf_get_be16 (const uint8_t *ptr, int offset) static inline void endswap_short_array (short *ptr, int len) -{ short temp ; - +{ while (--len >= 0) - { temp = ptr [len] ; + { short temp = ptr [len] ; ptr [len] = ENDSWAP_16 (temp) ; } ; } /* endswap_short_array */ @@ -309,10 +298,9 @@ endswap_short_copy (short *dest, const short *src, int len) static inline void endswap_int_array (int *ptr, int len) -{ int temp ; - +{ while (--len >= 0) - { temp = ptr [len] ; + { int temp = ptr [len] ; ptr [len] = ENDSWAP_32 (temp) ; } ; } /* endswap_int_array */ @@ -330,20 +318,18 @@ endswap_int_copy (int *dest, const int *src, int len) static inline void endswap_int64_t_array (int64_t *ptr, int len) -{ int64_t value ; - +{ while (--len >= 0) - { value = ptr [len] ; + { int64_t value = ptr [len] ; ptr [len] = ENDSWAP_64 (value) ; } ; } /* endswap_int64_t_array */ static inline void endswap_int64_t_copy (int64_t *dest, const int64_t *src, int len) -{ int64_t value ; - +{ while (--len >= 0) - { value = src [len] ; + { int64_t value = src [len] ; dest [len] = ENDSWAP_64 (value) ; } ; } /* endswap_int64_t_copy */ From be637260f704f177b0d9d542154a3d29cade9d82 Mon Sep 17 00:00:00 2001 From: evpobr Date: Wed, 24 Mar 2021 08:17:54 +0500 Subject: [PATCH 073/204] xi: Remove extra blank lines --- src/xi.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/xi.c b/src/xi.c index 72b4adaa..e8169471 100644 --- a/src/xi.c +++ b/src/xi.c @@ -312,10 +312,6 @@ xi_write_header (SF_PRIVATE *psf, int UNUSED (calc_length)) psf_binheader_writef (psf, "b", BHWv (pxi->sample_name), BHWz (sizeof (pxi->sample_name))) ; - - - - /* Header construction complete so write it out. */ psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ; @@ -409,8 +405,6 @@ xi_read_header (SF_PRIVATE *psf) psf_log_printf (psf, " size : %d\n", sample_sizes [k]) ; - - psf_log_printf (psf, " loop\n begin : %d\n end : %d\n", loop_begin, loop_end) ; psf_log_printf (psf, " volume : %u\n f. tune : %d\n flags : 0x%02X ", @@ -1227,4 +1221,3 @@ d2dles_array (XI_PRIVATE *pxi, const double *src, short *dest, int count, double pxi->last_16 = last_val ; } /* d2dles_array */ - From 663a59aa6ea5e24cf5159b8e1c2b0735712ea74e Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Sat, 9 Feb 2019 22:45:55 -0800 Subject: [PATCH 074/204] mpeg: Add MPEG Encode, Decode and Tests Use SF_FORMAT_MP3 major and SF_FORMAT_MPEG_LAYER_(I|II|III) subformats. --- .github/workflows/action.yml | 12 +- CMakeLists.txt | 35 +- Makefile.am | 20 +- cmake/FindLame.cmake | 66 +++ cmake/FindMpg123.cmake | 64 +++ cmake/SndFileChecks.cmake | 8 + configure.ac | 54 +++ include/sndfile.h.in | 16 + src/common.c | 21 +- src/common.h | 5 +- src/config.h.cmake | 3 + src/id3.c | 65 +++ src/id3.h | 29 ++ src/mp3.c | 174 ++++++++ src/mpeg.h | 74 ++++ src/mpeg_decode.c | 659 ++++++++++++++++++++++++++++ src/mpeg_l3_encode.c | 784 ++++++++++++++++++++++++++++++++++ src/sndfile.c | 43 +- src/test_log_printf.c | 2 + tests/compression_size_test.c | 41 +- tests/floating_point_test.tpl | 8 + tests/mp3_test.c | 348 +++++++++++++++ tests/string_test.c | 116 +++-- tests/test_wrapper.sh.in | 9 + 24 files changed, 2593 insertions(+), 63 deletions(-) create mode 100644 cmake/FindLame.cmake create mode 100644 cmake/FindMpg123.cmake create mode 100644 src/id3.h create mode 100644 src/mp3.c create mode 100644 src/mpeg.h create mode 100644 src/mpeg_decode.c create mode 100644 src/mpeg_l3_encode.c create mode 100644 tests/mp3_test.c diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index c646921f..b0fe7bd3 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -28,24 +28,28 @@ jobs: os: ubuntu-latest cc: gcc cxx: g++ + autotools-options: --enable-werror build-system: autotools - name: ubuntu-clang-autotools os: ubuntu-latest cc: clang cxx: clang++ + autotools-options: --enable-werror build-system: autotools - name: ubuntu-gcc-ossfuzz os: ubuntu-latest cc: gcc cxx: g++ + autotools-options: --enable-werror build-system: ossfuzz - name: macos-autotools os: macos-latest cc: clang cxx: clang++ + autotools-options: --enable-werror build-system: autotools - name: ubuntu-gcc-cmake @@ -179,16 +183,16 @@ jobs: - name: Install MacOS dependencies if: startsWith(matrix.os,'macos') run: | - brew install automake autogen speex + brew install automake autogen speex mpg123 - - name: Install Lunux dependencies + - name: Install Linux dependencies if: startsWith(matrix.os,'ubuntu') - run: sudo apt-get install -y autogen ninja-build libogg-dev libvorbis-dev libflac-dev libopus-dev libasound2-dev libsqlite3-dev libspeex-dev + run: sudo apt-get install -y autogen ninja-build libogg-dev libvorbis-dev libflac-dev libopus-dev libasound2-dev libsqlite3-dev libspeex-dev libmp3lame-dev libmpg123-dev - name: Install Windows dependencies if: startsWith(matrix.os,'windows') run: | - vcpkg install libvorbis libflac opus sqlite3 speex --triplet ${{matrix.triplet}} + vcpkg install libvorbis libflac opus sqlite3 speex mp3lame mpg123 --triplet ${{matrix.triplet}} - name: Configure, build and test with Autotools env: diff --git a/CMakeLists.txt b/CMakeLists.txt index f2d4223e..bf861ef9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -78,6 +78,7 @@ include(SndFileChecks) cmake_dependent_option (BUILD_REGTEST "Build regtest" ON "SQLITE3_FOUND" OFF) cmake_dependent_option (ENABLE_EXTERNAL_LIBS "Enable FLAC, Vorbis, and Opus codecs" ON "Vorbis_FOUND;FLAC_FOUND;OPUS_FOUND" OFF) +cmake_dependent_option (ENABLE_MPEG "Enable MPEG codecs" ON "LAME_FOUND;MPG123_FOUND" OFF) cmake_dependent_option (ENABLE_CPU_CLIP "Enable tricky cpu specific clipper" ON "CPU_CLIPS_POSITIVE;CPU_CLIPS_NEGATIVE" OFF) if (NOT ENABLE_CPU_CLIP) set (CPU_CLIPS_POSITIVE FALSE) @@ -104,11 +105,12 @@ set (HAVE_ALSA_ASOUNDLIB_H ${ALSA_FOUND}) set (HAVE_SNDIO_H ${SNDIO_FOUND}) set (ENABLE_EXPERIMENTAL_CODE ${ENABLE_EXPERIMENTAL}) +set (HAVE_MPEG_LIBS ${ENABLE_MPEG}) set (HAVE_SPEEX ${ENABLE_EXPERIMENTAL}) -set (HAVE_OPUS ${ENABLE_EXPERIMENTAL}) add_feature_info (BUILD_SHARED_LIBS BUILD_SHARED_LIBS "build shared libraries") add_feature_info (ENABLE_EXTERNAL_LIBS ENABLE_EXTERNAL_LIBS "enable FLAC, Vorbis, and Opus codecs") +add_feature_info (ENABLE_MPEG ENABLE_MPEG "enable MPEG audio (including mp3) codecs") add_feature_info (ENABLE_EXPERIMENTAL ENABLE_EXPERIMENTAL "enable experimental code") add_feature_info (BUILD_TESTING BUILD_TESTING "build tests") add_feature_info (BUILD_REGTEST BUILD_REGTEST "build regtest") @@ -147,6 +149,18 @@ set_package_properties (FLAC PROPERTIES DESCRIPTION "Free Lossless Audio Codec Library" PURPOSE "Enables FLAC support" ) +set_package_properties (LAME PROPERTIES + TYPE OPTIONAL + URL "https://lame.sourceforge.io/" + DESCRIPTION "High quality MPEG Audio Layer III (MP3) encoder" + PURPOSE "Enables MPEG layer III (MP3) writing support" + ) +set_package_properties (MPG123 PROPERTIES + TYPE OPTIONAL + URL "https://www.mpg123.de/" + DESCRIPTION "MPEG Audio Layer I/II/III decoder" + PURPOSE "Enables MPEG Audio reading support" + ) set_package_properties(Opus PROPERTIES TYPE RECOMMENDED URL "www.opus-codec.org/" @@ -247,6 +261,7 @@ add_library (sndfile src/ogg.c src/chanmap.h src/chanmap.c + src/id3.h src/id3.c $<$:src/windows.c> src/sndfile.c @@ -284,8 +299,12 @@ add_library (sndfile src/ogg_speex.c src/ogg_pcm.c src/ogg_opus.c + src/ogg_vcomment.h src/ogg_vcomment.c src/nms_adpcm.c + src/mp3.c + src/mpeg_decode.c + src/mpeg_l3_encode.c src/GSM610/config.h src/GSM610/gsm.h src/GSM610/gsm610_priv.h @@ -349,6 +368,8 @@ target_link_libraries (sndfile $<$:FLAC::FLAC> $<$,$,$>:Speex::Speex> $<$:Opus::opus> + $<$:MPG123::libmpg123> + $<$:Lame::Lame> ) set_target_properties (sndfile PROPERTIES PUBLIC_HEADER "${sndfile_HDRS}" @@ -1187,6 +1208,14 @@ if (BUILD_TESTING) $<$:m> ) + add_executable (mp3_test tests/mp3_test.c) + target_link_libraries (mp3_test + PRIVATE + sndfile + test_utils + $<$:m> + ) + add_executable (stdin_test tests/stdin_test.c) target_link_libraries (stdin_test PRIVATE @@ -1418,6 +1447,10 @@ if (BUILD_TESTING) add_test (lossy_comp_test_ogg_opus lossy_comp_test ogg_opus) add_test (string_test_opus string_test opus) + ### mp3-tests ### + add_test (mp3_test mp3_test) + add_test (compression_size_test_mpeg compression_size_test mpeg) + ### io-tests add_test (stdio_test stdio_test) add_test (pipe_test pipe_test) diff --git a/Makefile.am b/Makefile.am index 13ead221..da1ae5e4 100644 --- a/Makefile.am +++ b/Makefile.am @@ -62,26 +62,26 @@ endif lib_LTLIBRARIES = src/libsndfile.la include_HEADERS = include/sndfile.hh nodist_include_HEADERS = include/sndfile.h -src_libsndfile_la_CFLAGS = $(EXTERNAL_XIPH_CFLAGS) +src_libsndfile_la_CFLAGS = $(EXTERNAL_XIPH_CFLAGS) $(MPEG_CFLAGS) # MinGW requires -no-undefined if a DLL is to be built. src_libsndfile_la_LDFLAGS = -no-undefined -version-info $(SHARED_VERSION_INFO) $(SHLIB_VERSION_ARG) src_libsndfile_la_SOURCES = src/sndfile.c src/aiff.c src/au.c src/avr.c src/caf.c src/dwd.c src/flac.c src/g72x.c src/htk.c src/ircam.c \ src/macos.c src/mat4.c src/mat5.c src/nist.c src/paf.c src/pvf.c src/raw.c src/rx2.c src/sd2.c \ src/sds.c src/svx.c src/txw.c src/voc.c src/wve.c src/w64.c src/wavlike.c src/wav.c src/xi.c src/mpc2k.c src/rf64.c \ - src/ogg_vorbis.c src/ogg_speex.c src/ogg_pcm.c src/ogg_opus.c src/ogg_vcomment.c \ - src/common.h src/sfconfig.h src/sfendian.h src/wavlike.h src/sf_unistd.h src/ogg.h src/chanmap.h src/ogg_vcomment.h + src/ogg.c src/ogg.h src/ogg_vorbis.c src/ogg_speex.c src/ogg_pcm.c src/ogg_opus.c src/ogg_vcomment.c src/ogg_vcomment.h \ + src/common.h src/sfconfig.h src/sfendian.h src/wavlike.h src/sf_unistd.h src/chanmap.h src/mp3.c nodist_src_libsndfile_la_SOURCES = $(nodist_include_HEADERS) src_libsndfile_la_LIBADD = src/GSM610/libgsm.la src/G72x/libg72x.la src/ALAC/libalac.la \ - src/libcommon.la $(EXTERNAL_XIPH_LIBS) -lm + src/libcommon.la $(EXTERNAL_XIPH_LIBS) -lm $(MPEG_LIBS) EXTRA_src_libsndfile_la_DEPENDENCIES = $(SYMBOL_FILES) noinst_LTLIBRARIES = src/libcommon.la -src_libcommon_la_CFLAGS = $(EXTERNAL_XIPH_CFLAGS) +src_libcommon_la_CFLAGS = $(EXTERNAL_XIPH_CFLAGS) $(MPEG_CFLAGS) src_libcommon_la_SOURCES = src/common.c src/file_io.c src/command.c src/pcm.c src/ulaw.c src/alaw.c \ src/float32.c src/double64.c src/ima_adpcm.c src/ms_adpcm.c src/gsm610.c src/dwvw.c src/vox_adpcm.c \ src/interleave.c src/strings.c src/dither.c src/cart.c src/broadcast.c src/audio_detect.c \ - src/ima_oki_adpcm.c src/ima_oki_adpcm.h src/alac.c src/chunk.c src/ogg.c src/chanmap.c \ - src/windows.c src/id3.c src/nms_adpcm.c $(WIN_VERSION_FILE) + src/ima_oki_adpcm.c src/ima_oki_adpcm.h src/alac.c src/chunk.c src/chanmap.c \ + src/windows.c src/id3.c src/id3.h src/nms_adpcm.c src/mpeg_decode.c src/mpeg_l3_encode.c src/mpeg.h $(WIN_VERSION_FILE) check_PROGRAMS = src/test_main src_test_main_SOURCES = src/test_main.c src/test_main.h src/test_conversions.c src/test_float.c src/test_endswap.c \ @@ -221,7 +221,8 @@ check_PROGRAMS += tests/sfversion tests/floating_point_test tests/write_read_tes tests/locale_test tests/win32_ordinal_test tests/ogg_test tests/compression_size_test \ tests/checksum_test tests/external_libs_test tests/rdwr_test tests/format_check_test $(CPP_TEST) \ tests/channel_test tests/long_read_write_test tests/stdin_test tests/stdout_test \ - tests/dither_test tests/fix_this tests/largefile_test tests/benchmark tests/ogg_opus_test + tests/dither_test tests/fix_this tests/largefile_test tests/benchmark tests/ogg_opus_test \ + tests/mp3_test BUILT_SOURCES += \ tests/write_read_test.c \ @@ -377,6 +378,9 @@ tests_cpp_test_LDADD = src/libsndfile.la tests_checksum_test_SOURCES = tests/checksum_test.c tests/utils.c tests/utils.h tests_checksum_test_LDADD = src/libsndfile.la +tests_mp3_test_SOURCES = tests/mp3_test.c tests/utils.c tests/utils.h +tests_mp3_test_LDADD = src/libsndfile.la + # Lite remove start tests_dwvw_test_SOURCES = tests/dwvw_test.c tests/utils.c tests/utils.h tests_dwvw_test_LDADD = src/libsndfile.la diff --git a/cmake/FindLame.cmake b/cmake/FindLame.cmake new file mode 100644 index 00000000..be32475a --- /dev/null +++ b/cmake/FindLame.cmake @@ -0,0 +1,66 @@ +# - Find lame +# Find the native lame includes and libraries +# +# LAME_INCLUDE_DIRS - where to find lame.h, etc. +# LAME_LIBRARIES - List of libraries when using lame. +# LAME_FOUND - True if Lame found. + +if (LAME_INCLUDE_DIR) + # Already in cache, be silent + set(LAME_FIND_QUIETLY TRUE) +endif () + +find_path (LAME_INCLUDE_DIR lame/lame.h + HINTS + ${LAME_ROOT} + ) + +# MSVC built lame may be named mp3lame_static. +# The provided project files name the library with the lib prefix. + +find_library (LAME_LIBRARY + NAMES + mp3lame + mp3lame_static + libmp3lame + libmp3lame_static + libmp3lame-static + HINTS + ${LAME_ROOT} + ) + +find_library (LAME_HIP_LIBRARY + NAMES + mpghip-static + libmpghip-static + HINTS + ${LAME_ROOT} + ) + +# Handle the QUIETLY and REQUIRED arguments and set LAME_FOUND +# to TRUE if all listed variables are TRUE. +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args (Lame + REQUIRED_VARS + LAME_LIBRARY + LAME_INCLUDE_DIR + ) + +if (LAME_FOUND) + set (LAME_LIBRARIES ${LAME_LIBRARY} ${LAME_HIP_LIBRARY}) + set (LAME_INCLUDE_DIRS ${LAME_INCLUDE_DIR}) + + if (NOT TARGET Lame::Lame) + add_library (Lame::Lame UNKNOWN IMPORTED) + set_target_properties (Lame::Lame PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${LAME_INCLUDE_DIRS}" + IMPORTED_LOCATION "${LAME_LIBRARY}" + ) + if (LAME_HIP_LIBRARY) + set_property (TARGET Lame::Lame APPEND PROPERTY + INTERFACE_LINK_LIBRARIES "${LAME_HIP_LIBRARY}") + endif () + endif () +endif () + +mark_as_advanced(LAME_INCLUDE_DIR LAME_LIBRARY LAME_HIP_LIBRARY) diff --git a/cmake/FindMpg123.cmake b/cmake/FindMpg123.cmake new file mode 100644 index 00000000..88b16224 --- /dev/null +++ b/cmake/FindMpg123.cmake @@ -0,0 +1,64 @@ +# - Find mpg123 +# Find the native mpg123 includes and libraries +# +# MPG123_INCLUDE_DIRS - where to find mpg123.h, etc. +# MPG123_LIBRARIES - List of libraries when using mpg123. +# MPG123_FOUND - True if Mpg123 found. + +if (MPG123_INCLUDE_DIR) + # Already in cache, be silent + set(MPG123_FIND_QUIETLY TRUE) +endif () + +find_package (PkgConfig QUIET) +pkg_check_modules(PC_MPG123 QUIET libmpg123>=1.25.10) + +set (MPG123_VERSION ${PC_MPG123_VERSION}) + +find_path (MPG123_INCLUDE_DIR mpg123.h + HINTS + ${PC_MPG123_INCLUDEDIR} + ${PC_MPG123_INCLUDE_DIRS} + ${MPG123_ROOT} + ) + +# MSVC built mpg123 may be named mpg123_static. +# The provided project files name the library with the lib prefix. + +find_library (MPG123_LIBRARY + NAMES + mpg123 + mpg123_static + libmpg123 + libmpg123_static + HINTS + ${PC_MPG123_LIBDIR} + ${PC_MPG123_LIBRARY_DIRS} + ${MPG123_ROOT} + ) + +# Handle the QUIETLY and REQUIRED arguments and set MPG123_FOUND +# to TRUE if all listed variables are TRUE. +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args (Mpg123 + REQUIRED_VARS + MPG123_LIBRARY + MPG123_INCLUDE_DIR + VERSION_VAR + MPG123_VERSION + ) + +if (MPG123_FOUND) + set (MPG123_LIBRARIES ${MPG123_LIBRARY}) + set (MPG123_INCLUDE_DIRS ${MPG123_INCLUDE_DIR}) + + if (NOT TARGET MPG123::libmpg123) + add_library (MPG123::libmpg123 UNKNOWN IMPORTED) + set_target_properties (MPG123::libmpg123 PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${MPG123_INCLUDE_DIRS}" + IMPORTED_LOCATION "${MPG123_LIBRARIES}" + ) + endif () +endif () + +mark_as_advanced(MPG123_INCLUDE_DIR MPG123_LIBRARY) diff --git a/cmake/SndFileChecks.cmake b/cmake/SndFileChecks.cmake index c0d09910..d776fa8a 100644 --- a/cmake/SndFileChecks.cmake +++ b/cmake/SndFileChecks.cmake @@ -56,6 +56,14 @@ else () set (HAVE_EXTERNAL_XIPH_LIBS 0) endif () +find_package (Lame) +find_package (Mpg123 1.25.10) +if (LAME_FOUND AND MPG123_FOUND) + set (HAVE_MPEG_LIBS 1) +else () + set (HAVE_MPEG_LIBS 0) +endif() + find_package (Speex) find_package (SQLite3) diff --git a/configure.ac b/configure.ac index ef6c2cc5..ef47a2fa 100644 --- a/configure.ac +++ b/configure.ac @@ -148,6 +148,9 @@ AC_ARG_ENABLE([alsa], AC_ARG_ENABLE([external-libs], [AS_HELP_STRING([--disable-external-libs], [disable use of FLAC, Ogg and Vorbis [[default=no]]])]) +AC_ARG_ENABLE([mpeg], + [AS_HELP_STRING([--disable-mpeg], [disable use of LAME/MPG123 for MPEG (MP3) [[defaults=no]]])]) + AC_ARG_ENABLE(octave, [AS_HELP_STRING([--enable-octave], [enable building of GNU Octave module])]) @@ -388,6 +391,51 @@ AS_IF([test -n "$PKG_CONFIG"], [ AC_DEFINE_UNQUOTED([HAVE_EXTERNAL_XIPH_LIBS], [$HAVE_EXTERNAL_XIPH_LIBS], [Will be set to 1 if flac, ogg, vorbis, and opus are available.]) +dnl ==================================================================================== +dnl Check for MPEG libraris liblame + +ac_cv_lame="no" +ac_cv_mpg123="no" +HAVE_MPEG=0 +MPEG_CFLAGS="" +MPEG_LIBS="" + +AS_IF([test -n "$PKG_CONFIG"], [ + AS_IF([test "x$enable_mpeg" = "xno"], [ + AC_MSG_WARN([[*** MPEG (Lame/MPG123) disabled. ***]]) + ], [ + AC_CHECK_HEADER(lame/lame.h, + lame_header_found="yes", + lame_header_found="no") + AC_SEARCH_LIBS(lame_set_VBR_q, [lame mp3lame], [lame_lib_found="yes"], [lame_lib_found="no"]) + AS_IF([test "x$lame_lib_found$lame_header_found" = "xyesyes"], [ + ac_cv_lame="yes" + ], [ + AC_MSG_WARN([["MPEG support selected but external Lame library cannot be found.]]) + ]) + + PKG_CHECK_MOD_VERSION([MPG123], [libmpg123 >= 1.25.10], [ + ac_cv_mpg123="yes" + ], [ + AC_MSG_WARN([["MPEG support selected but external MPG123 library cannot be found.]]) + ]) + + AS_IF([test "x$ac_cv_lame$ac_cv_mpg123" = "xyesyes"], [ + enable_mpeg="yes" + HAVE_MPEG=1 + MPEG_CFLAGS="$MPG123_CFLAGS" + MPEG_LIBS="$MPG123_LIBS" + ], [ + enable_mpeg="no" + AS_ECHO([""]) + AC_MSG_WARN([[*** MPEG support disabled.]]) + AS_ECHO([""]) + ]) + ]) + ]) + +AC_DEFINE_UNQUOTED([HAVE_MPEG], [$HAVE_MPEG], [Will be set to 1 if lame, mpg123 mpeg support is available.]) + dnl ==================================================================================== dnl Check for libsqlite3 (only used in regtest). @@ -666,6 +714,7 @@ AC_SUBST(GEN_TOOL) AC_SUBST(WIN_RC_VERSION) AC_SUBST(HAVE_EXTERNAL_XIPH_LIBS) +AC_SUBST(HAVE_MPEG) AC_SUBST(OS_SPECIFIC_CFLAGS) AC_SUBST(OS_SPECIFIC_LINKS) AC_SUBST(SNDIO_LIBS) @@ -673,6 +722,10 @@ AC_SUBST(SNDIO_LIBS) AC_SUBST(EXTERNAL_XIPH_CFLAGS) AC_SUBST(EXTERNAL_XIPH_LIBS) AC_SUBST(EXTERNAL_XIPH_REQUIRE) +AC_SUBST(MPG123_CFLAGS) +AC_SUBST(MPG123_LIBS) +AC_SUBST(MPEG_CFLAGS) +AC_SUBST(MPEG_LIBS) AC_SUBST(SRC_BINDIR) AC_SUBST(TEST_BINDIR) @@ -718,6 +771,7 @@ AC_MSG_RESULT([ Experimental code : ................... ${enable_experimental:-no} Using ALSA in example programs : ...... ${enable_alsa:-no} External FLAC/Ogg/Vorbis/Opus : ....... ${enable_external_libs:-no} + External MPEG Lame/MPG123 : ........... ${enable_mpeg:-no} Building Octave interface : ........... ${OCTAVE_BUILD} Tools : diff --git a/include/sndfile.h.in b/include/sndfile.h.in index 98c1ac34..ac65e3d9 100644 --- a/include/sndfile.h.in +++ b/include/sndfile.h.in @@ -71,6 +71,7 @@ enum SF_FORMAT_OGG = 0x200000, /* Xiph OGG container */ SF_FORMAT_MPC2K = 0x210000, /* Akai MPC 2000 sampler */ SF_FORMAT_RF64 = 0x220000, /* RF64 WAV file */ + SF_FORMAT_MP3 = 0x230000, /* So-called MP3 file. */ /* Subtypes from here on. */ @@ -116,6 +117,10 @@ enum SF_FORMAT_ALAC_24 = 0x0072, /* Apple Lossless Audio Codec (24 bit). */ SF_FORMAT_ALAC_32 = 0x0073, /* Apple Lossless Audio Codec (32 bit). */ + SF_FORMAT_MPEG_LAYER_I = 0x0080, /* MPEG-1 Audio Layer I */ + SF_FORMAT_MPEG_LAYER_II = 0x0081, /* MPEG-1 Audio Layer II */ + SF_FORMAT_MPEG_LAYER_III = 0x0082, /* MPEG-2 Audio Layer III */ + /* Endian-ness options. */ SF_ENDIAN_FILE = 0x00000000, /* Default file endian-ness. */ @@ -218,6 +223,9 @@ enum SFC_SET_OGG_PAGE_LATENCY_MS = 0x1302, SFC_SET_OGG_PAGE_LATENCY = 0x1303, + SFC_GET_BITRATE_MODE = 0x1302, + SFC_SET_BITRATE_MODE = 0x1303, + /* Cart Chunk support */ SFC_SET_CART_INFO = 0x1400, SFC_GET_CART_INFO = 0x1401, @@ -334,6 +342,14 @@ enum SF_CHANNEL_MAP_MAX } ; +/* Bitrate mode values (for use with SFC_GET/SET_BITRATE_MODE) +*/ +enum +{ SF_BITRATE_MODE_CONSTANT = 0, + SF_BITRATE_MODE_AVERAGE, + SF_BITRATE_MODE_VARIABLE +} ; + /* A SNDFILE* pointer can be passed around much like stdio.h's FILE* pointer. */ diff --git a/src/common.c b/src/common.c index 18279f47..a087c714 100644 --- a/src/common.c +++ b/src/common.c @@ -104,7 +104,7 @@ void psf_log_printf (SF_PRIVATE *psf, const char *format, ...) { va_list ap ; uint32_t u ; - int d, tens, shift, width, width_specifier, left_align, slen ; + int d, tens, shift, width, width_specifier, left_align, slen, precision ; char c, *strptr, istr [5], lead_char, sign_char ; va_start (ap, format) ; @@ -153,6 +153,12 @@ psf_log_printf (SF_PRIVATE *psf, const char *format, ...) while ((c = *format++) && isdigit (c)) width_specifier = width_specifier * 10 + (c - '0') ; + precision = 0 ; + if (c == '.') + { while ((c = *format++) && isdigit (c)) + precision = precision * 10 + (c - '0') ; + } ; + switch (c) { case 0 : /* NULL character. */ va_end (ap) ; @@ -162,12 +168,15 @@ psf_log_printf (SF_PRIVATE *psf, const char *format, ...) strptr = va_arg (ap, char *) ; if (strptr == NULL) break ; - slen = strlen (strptr) ; + if (precision > 0) + slen = strnlen (strptr, precision) ; + else + slen = strlen (strptr) ; width_specifier = width_specifier >= slen ? width_specifier - slen : 0 ; if (left_align == SF_FALSE) while (width_specifier -- > 0) log_putchar (psf, ' ') ; - while (*strptr) + while (slen--) log_putchar (psf, *strptr++) ; while (width_specifier -- > 0) log_putchar (psf, ' ') ; @@ -877,7 +886,7 @@ header_seek (SF_PRIVATE *psf, sf_count_t position, int whence) psf_bump_header_allocation (psf, position) ; if (position > psf->header.len) { /* Too much header to cache so just seek instead. */ - psf->header.indx = psf->header.end ; + psf->header.indx = psf->header.end = 0 ; psf_fseek (psf, position, whence) ; return ; } ; @@ -1563,6 +1572,7 @@ str_of_major_format (int format) CASE_NAME (SF_FORMAT_CAF) ; CASE_NAME (SF_FORMAT_WVE) ; CASE_NAME (SF_FORMAT_OGG) ; + CASE_NAME (SF_FORMAT_MP3) ; default : break ; } ; @@ -1599,6 +1609,9 @@ str_of_minor_format (int format) CASE_NAME (SF_FORMAT_DPCM_8) ; CASE_NAME (SF_FORMAT_DPCM_16) ; CASE_NAME (SF_FORMAT_VORBIS) ; + CASE_NAME (SF_FORMAT_MPEG_LAYER_I) ; + CASE_NAME (SF_FORMAT_MPEG_LAYER_II) ; + CASE_NAME (SF_FORMAT_MPEG_LAYER_III) ; default : break ; } ; diff --git a/src/common.h b/src/common.h index 16f515d1..8a2e6497 100644 --- a/src/common.h +++ b/src/common.h @@ -739,6 +739,8 @@ enum SFE_MPC_NO_MARKER, + SFE_MPEG_BAD_SAMPLERATE, + SFE_MAX_ERROR /* This must be last in list. */ } ; @@ -894,10 +896,10 @@ int ogg_pcm_open (SF_PRIVATE *psf) ; int ogg_opus_open (SF_PRIVATE *psf) ; int ogg_open (SF_PRIVATE *psf) ; +int mp3_open (SF_PRIVATE *psf) ; /* In progress. Do not currently work. */ -int mpeg_open (SF_PRIVATE *psf) ; int rx2_open (SF_PRIVATE *psf) ; int txw_open (SF_PRIVATE *psf) ; int wve_open (SF_PRIVATE *psf) ; @@ -1014,6 +1016,7 @@ typedef struct int audio_detect (SF_PRIVATE * psf, AUDIO_DETECT *ad, const unsigned char * data, int datalen) ; int id3_skip (SF_PRIVATE * psf) ; +const char *id3_lookup_v1_genre (int number) ; void alac_get_desc_chunk_items (int subformat, uint32_t *fmt_flags, uint32_t *frames_per_packet) ; diff --git a/src/config.h.cmake b/src/config.h.cmake index 3d0b2f14..2872e7d1 100644 --- a/src/config.h.cmake +++ b/src/config.h.cmake @@ -43,6 +43,9 @@ /* Will be set to 1 if flac, ogg and vorbis are available. */ #cmakedefine01 HAVE_EXTERNAL_XIPH_LIBS +/* Will be set to 1 if lame and mpg123 are available. */ +#cmakedefine01 HAVE_MPEG + /* Define to 1 if you have the `floor' function. */ #cmakedefine01 HAVE_FLOOR diff --git a/src/id3.c b/src/id3.c index 2b82d0fa..6302f5b0 100644 --- a/src/id3.c +++ b/src/id3.c @@ -26,6 +26,43 @@ #include "sndfile.h" #include "sfendian.h" #include "common.h" +#include "id3.h" + +#if HAVE_MPEG +#include + +struct id3v1_genre_handler_userdata +{ int number ; + const char *ret ; +} ; + +static void +id3v1_genre_handler (int number, const char *description, void *userdata) +{ struct id3v1_genre_handler_userdata *data = (struct id3v1_genre_handler_userdata *) userdata ; + if (data->number == number) + data->ret = description ; +} + +const char * +id3_lookup_v1_genre (int number) +{ struct id3v1_genre_handler_userdata data ; + + data.number = number ; + data.ret = NULL ; + id3tag_genre_list (id3v1_genre_handler, &data) ; + + return data.ret ; +} + +#else /* HAVE_MPEG */ + +const char * +id3_lookup_v1_genre (int UNUSED (number)) +{ return NULL ; + } + +#endif + int id3_skip (SF_PRIVATE * psf) @@ -57,3 +94,31 @@ id3_skip (SF_PRIVATE * psf) return 0 ; } /* id3_skip */ + +const char * +id3_process_v2_genre (const char *genre) +{ int num = 0 ; + char c ; + const char *ptr ; + + if (!genre) + return NULL ; + + /* + ** Genre may require more processing. + ** + ** It is allowed to have numeric references to the genre table from ID3v1. + ** We'll just convert the simple case here, strings of the format "(nnn)". + */ + ptr = genre ; + if (ptr [0] == '(' && (c = *++ ptr) && isdigit (c)) + { num = c - '0' ; + while ((c == *++ ptr) && isdigit (c)) + num = num * 10 + (c - '0') ; + if (c == ')' && (c = *++ ptr) == '\0' && num < 256) + if ((ptr = id3_lookup_v1_genre (num))) + return ptr ; + } ; + + return genre ; +} /* id3_process_v2_genre */ diff --git a/src/id3.h b/src/id3.h new file mode 100644 index 00000000..4f54c4fc --- /dev/null +++ b/src/id3.h @@ -0,0 +1,29 @@ +/* +** Copyright (C) 2008-2019 Erik de Castro Lopo +** Copyright (C) 2019 Arthur Taylor +** +** This program is free software ; you can redistribute it and/or modify +** it under the terms of the GNU Lesser General Public License as published by +** the Free Software Foundation ; either version 2.1 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY ; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU Lesser General Public License for more details. +** +** You should have received a copy of the GNU Lesser General Public License +** along with this program ; if not, write to the Free Software +** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#ifndef SF_SRC_ID3_H +#define SF_SRC_ID3_H + +int id3_skip (SF_PRIVATE * psf) ; + +const char *id3_lookup_v1_genre (int number) ; + +const char *id3_process_v2_genre (const char *genre) ; + +#endif /* SF_SRC_ID3_H */ diff --git a/src/mp3.c b/src/mp3.c new file mode 100644 index 00000000..6f63b252 --- /dev/null +++ b/src/mp3.c @@ -0,0 +1,174 @@ +/* +** Copyright (C) 2019 Erik de Castro Lopo +** Copyright (C) 2019 Arthur Taylor +** +** This program is free software ; you can redistribute it and/or modify +** it under the terms of the GNU Lesser General Public License as published by +** the Free Software Foundation ; either version 2.1 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY ; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU Lesser General Public License for more details. +** +** You should have received a copy of the GNU Lesser General Public License +** along with this program ; if not, write to the Free Software +** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +/* +** What is an MP3 file anyways? +** +** Believe it or not, MP3 files don't exist. +** +** The MPEG-1 standard defined a few audio codecs. The standard only defined a +** streaming format of semi-independent frames of audio meant for broadcasting, +** with no details or hints about stored on-disk formats. Each frame defines +** it's own bitrate, channel count, sample rate. However, they aren't +** completely independent. +** +** With its amazing-for-the-time compression ratio, the layer III audio codec +** became quite popular with file sharers and the internet. A stream of layer +** III audio would simply be written as a file, usually with the extension +** .mp3. Over time enthusiast and proprietary encoders sprung up adding +** different metadata headers and trailers, file seeking tables, and fiddling +** with the codecs parameters. These changes are only really based on consensus. +** +** MPEG-1 I/II/III audio can be embedded in a few container formats (including +** WAV), stored raw, or with additional metadata extension headers and trailers. +** +** This file is concerned only with the most common case of MPEG Layer III +** audio without a container but with the additional metadata standards. +** +** For the purposes of libsndfile, the major format of SF_FORMAT_MP3 means the +** following assumptions. A file of major format type SF_FORMAT_MP3: +** - Contains only layer III audio frames (SF_FORMAT_MPEG_LAYER_III) +** - All MPEG frames contained in the file have the same channel count +** - All MPEG frames contained in the file have the same samplerate +** - Has at least one of: +** - an ID3v1 trailer +** - an ID3v2 header or trailer +** - A Lame/Xing/Info header +** +** Testing has revealed that, more than any other format, MP3 suffers from +** corrupt files in the wild that most other software 'just works' with. This is +** usually because the MP3 decoders are very lenient. They are aided by the +** presence of a regular sync frame, but this makes it hard to classify them +** in a library that consumes other better-specified file formats. +*/ + +#include "sfconfig.h" + +#include "sndfile.h" +#include "common.h" + +#if HAVE_MPEG + +#include "mpeg.h" + +static int mp3_write_header (SF_PRIVATE *psf, int calc_length) ; +static int mp3_command (SF_PRIVATE *psf, int command, void *data, int datasize) ; + +/*------------------------------------------------------------------------------ + * Private functions + */ + +static int +mp3_write_header (SF_PRIVATE *psf, int UNUSED (calc_length)) +{ + if (psf->have_written) + return 0 ; + + return mpeg_l3_encoder_write_id3tag (psf) ; +} ; + +static int +mp3_command (SF_PRIVATE *psf, int command, void *data, int datasize) +{ int bitrate_mode ; + + switch (command) + { case SFC_SET_COMPRESSION_LEVEL : + if (data == NULL || datasize != sizeof (double)) + { psf->error = SFE_BAD_COMMAND_PARAM ; + return SF_FALSE ; + } ; + if (psf->file.mode != SFM_WRITE) + { psf->error = SFE_NOT_WRITEMODE ; + return SF_FALSE ; + } ; + return mpeg_l3_encoder_set_quality (psf, *(double *) data) ; + + case SFC_SET_BITRATE_MODE : + if (psf->file.mode != SFM_WRITE) + { psf->error = SFE_NOT_WRITEMODE ; + return SF_FALSE ; + } ; + if (data == NULL || datasize != sizeof (int)) + { psf->error = SFE_BAD_COMMAND_PARAM ; + return SF_FALSE ; + } ; + bitrate_mode = *(int *) data ; + return mpeg_l3_encoder_set_bitrate_mode (psf, bitrate_mode) ; + + case SFC_GET_BITRATE_MODE : + if (psf->file.mode == SFM_READ) + return mpeg_decoder_get_bitrate_mode (psf) ; + else + return mpeg_l3_encoder_get_bitrate_mode (psf) ; + + default : + return SF_FALSE ; + } ; + + return SF_FALSE ; +} /* mpeg_command */ + +/*------------------------------------------------------------------------------ + * Public functions + */ + +int +mp3_open (SF_PRIVATE *psf) +{ int error ; + + if (psf->file.mode == SFM_RDWR) + return SFE_BAD_MODE_RW ; + + if (psf->file.mode == SFM_WRITE) + { if (SF_CODEC (psf->sf.format) != SF_FORMAT_MPEG_LAYER_III) + return SFE_BAD_OPEN_FORMAT ; + + if ((error = mpeg_l3_encoder_init (psf, SF_TRUE))) + return error ; + + /* Choose variable bitrate mode by default for standalone (mp3) files.*/ + mpeg_l3_encoder_set_bitrate_mode (psf, SF_BITRATE_MODE_VARIABLE) ; + + /* ID3 support */ + psf->strings.flags = SF_STR_ALLOW_START ; + psf->write_header = mp3_write_header ; + psf->datalength = 0 ; + psf->dataoffset = 0 ; + } ; + + if (psf->file.mode == SFM_READ) + { if ((error = mpeg_decoder_init (psf))) + return error ; + } ; + + psf->command = mp3_command ; + + return 0 ; +} /* mpeg_open */ + +#else /* HAVE_MPEG */ + +int +mp3_open (SF_PRIVATE *psf) +{ + psf_log_printf (psf, "This version of libsndfile was compiled without MP3 support.\n") ; + return SFE_UNIMPLEMENTED ; +} /* mpeg_open */ + +#endif diff --git a/src/mpeg.h b/src/mpeg.h new file mode 100644 index 00000000..f335745f --- /dev/null +++ b/src/mpeg.h @@ -0,0 +1,74 @@ +/* +** Copyright (C) 2019 Erik de Castro Lopo +** Copyright (C) 2019 Arthur Taylor +** +** This program is free software ; you can redistribute it and/or modify +** it under the terms of the GNU Lesser General Public License as published by +** the Free Software Foundation ; either version 2.1 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY ; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU Lesser General Public License for more details. +** +** You should have received a copy of the GNU Lesser General Public License +** along with this program ; if not, write to the Free Software +** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#ifndef SNDFILE_MPEG_H +#define SNDFILE_MPEG_H + +#include "common.h" + +int mpeg_decoder_init (SF_PRIVATE *psf) ; + +/* +** Get the file bitrate mode, returning one of the SF_BITRATE_MODE_ enum +** values. Purely informative, 'Frankenstein' files and VBR files without an +** Xing/LAME/Info header may not be detected properly. +*/ +int mpeg_decoder_get_bitrate_mode (SF_PRIVATE *psf) ; + + +/* +** Initialize an encoder instance for writing. If parameter info_tag is +** SF_TRUE, a Xing/LAME/Info header is written at the beginning of the file, +** (unless the file cannot seek.) +*/ +int mpeg_l3_encoder_init (SF_PRIVATE *psf, int info_tag) ; + + +/* +** Write an ID3v2 header from the sndfile string metadata. Must be called +** before any audio data is written. Writing an ID3v2 header will also cause +** a ID3v1 trailer to be written on close automatically. +*/ +int mpeg_l3_encoder_write_id3tag (SF_PRIVATE *psf) ; + +/* +** Set the encoder quality setting. Argument to compression should be identical +** to that for SFC_SET_COMPRESSION_LEVEL; It should be in the range [0-1], +** with 0 being highest quality, least compression, and 1 being the opposite. +** Returns SF_TRUE on success, SF_FALSE otherwise. +*/ +int mpeg_l3_encoder_set_quality (SF_PRIVATE *psf, double compression) ; + +/* +** Set the encoder bitrate mode. Can only be called before any data has been +** written. Argument mode should be one of the SF_BITRATE_MODE_ enum values. +** Returns SF_TRUE on success, SF_FALSE otherwise. The SF_BITRATE_MODE_FILE +** enum value should not be passed here but rather intercepted at the container +** level and translated according to the container. +*/ +int mpeg_l3_encoder_set_bitrate_mode (SF_PRIVATE *psf, int mode) ; + +/* +** Get the encoder bitrate mode in use. Returns a SF_BITRATE_MODE_ enum value. +** Will not return SF_BITRATE_MODE_FILE. +*/ +int mpeg_l3_encoder_get_bitrate_mode (SF_PRIVATE *psf) ; + + +#endif /* SNDFILE_MPEG_H */ diff --git a/src/mpeg_decode.c b/src/mpeg_decode.c new file mode 100644 index 00000000..1ab34c38 --- /dev/null +++ b/src/mpeg_decode.c @@ -0,0 +1,659 @@ +/* +** Copyright (C) 2019 Erik de Castro Lopo +** Copyright (C) 2019 Arthur Taylor +** +** This program is free software ; you can redistribute it and/or modify +** it under the terms of the GNU Lesser General Public License as published by +** the Free Software Foundation ; either version 2.1 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY ; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU Lesser General Public License for more details. +** +** You should have received a copy of the GNU Lesser General Public License +** along with this program ; if not, write to the Free Software +** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include "sfconfig.h" + +#include + +#include "sndfile.h" +#include "common.h" +#include "mpeg.h" + +#if HAVE_MPEG + +#include "sfendian.h" +#include "id3.h" + +#include + +typedef struct +{ mpg123_handle *pmh ; + size_t header_remaining ; + unsigned unique_id ; +} MPEG_DEC_PRIVATE ; + +static int mpeg_dec_close (SF_PRIVATE *psf) ; +static sf_count_t mpeg_dec_seek (SF_PRIVATE *psf, int whence, sf_count_t count) ; + +static ssize_t mpeg_dec_io_read (void *priv, void *buffer, size_t nbytes) ; +static off_t mpeg_dec_io_lseek (void *priv, off_t offset, int whence) ; + +static ssize_t +mpeg_dec_io_read (void *priv, void *buffer, size_t nbytes) +{ SF_PRIVATE *psf = (SF_PRIVATE *) priv ; + MPEG_DEC_PRIVATE *pmp3d = (MPEG_DEC_PRIVATE *) psf->codec_data ; + + if (pmp3d->header_remaining) + { if (pmp3d->header_remaining < nbytes) + nbytes = pmp3d->header_remaining ; + psf_binheader_readf (psf, "b", buffer, nbytes) ; + pmp3d->header_remaining -= nbytes ; + return nbytes ; + } ; + + return psf_fread (buffer, 1, nbytes, psf) ; +} /* mpeg_dec_io_read */ + +static off_t +mpeg_dec_io_lseek (void *priv, off_t offset, int whence) +{ SF_PRIVATE *psf = (SF_PRIVATE *) priv ; + + return psf_fseek (psf, offset, whence) ; +} /* mpeg_dec_io_lseek */ + +static int +mpeg_dec_close (SF_PRIVATE *psf) +{ MPEG_DEC_PRIVATE *pmp3d = (MPEG_DEC_PRIVATE *) psf->codec_data ; + + if (pmp3d) + { if (pmp3d->pmh) + { mpg123_close (pmp3d->pmh) ; + mpg123_delete (pmp3d->pmh) ; + pmp3d->pmh = NULL ; + } + free (psf->codec_data) ; + psf->codec_data = NULL ; + } ; + + return 0 ; +} /* mpeg_dec_close */ + +static sf_count_t mpeg_dec_decode (SF_PRIVATE *psf, MPEG_DEC_PRIVATE *pmp3d, float *ptr, sf_count_t len) +{ size_t done ; + int error ; + + error = mpg123_read (pmp3d->pmh, (unsigned char *) ptr, len * sizeof (float), &done) ; + + if (error == MPG123_OK) + return done / sizeof (float) ; + + if (error == MPG123_DONE) + return 0 ; + + if (error == MPG123_NEW_FORMAT) + { psf->error = SFE_MALFORMED_FILE ; + return -1 ; + } ; + + psf->error = SFE_INTERNAL ; + return -1 ; +} /* mpeg_dec_decode */ + +static inline void +f2s_array (const float *src, int count, short *dest) +{ while (--count >= 0) + { dest [count] = psf_lrintf (src [count] * (float) 0x7FFF) ; + } ; +} /* f2s_array */ + +static sf_count_t +mpeg_dec_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) +{ BUF_UNION ubuf ; + MPEG_DEC_PRIVATE *pmp3d = (MPEG_DEC_PRIVATE *) psf->codec_data ; + sf_count_t total = 0 ; + sf_count_t done ; + const sf_count_t buflen = ARRAY_LEN (ubuf.fbuf) ; + + while (len > 0) + { done = mpeg_dec_decode (psf, pmp3d, ubuf.fbuf, SF_MIN (buflen, len)) ; + + if (done <= 0) + break ; + + f2s_array (ubuf.fbuf, done, ptr + total) ; + + total += done ; + len -= done ; + } + + return total ; +} /*mpeg_dec_read_s */ + +static inline void +f2i_array (const float *src, int count, int *dest) +{ while (--count >= 0) + { dest [count] = psf_lrintf (src [count] * (float) 0x7FFFFFFF) ; + } ; +} /* f2i_array */ + +static sf_count_t +mpeg_dec_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) +{ BUF_UNION ubuf ; + MPEG_DEC_PRIVATE *pmp3d = (MPEG_DEC_PRIVATE *) psf->codec_data ; + sf_count_t total = 0 ; + sf_count_t done ; + const sf_count_t buflen = ARRAY_LEN (ubuf.fbuf) ; + + while (len > 0) + { done = mpeg_dec_decode (psf, pmp3d, ubuf.fbuf, SF_MIN (buflen, len)) ; + + if (done <= 0) + break ; + + f2i_array (ubuf.fbuf, done, ptr + total) ; + + total += done ; + len -= done ; + } + + return total ; +} /* mpeg_dec_read_i */ + +static sf_count_t +mpeg_dec_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) +{ MPEG_DEC_PRIVATE *pmp3d = (MPEG_DEC_PRIVATE *) psf->codec_data ; + sf_count_t done ; + sf_count_t count ; + + done = mpeg_dec_decode (psf, pmp3d, ptr, len) ; + + if (done <= 0) + return 0 ; + + if (psf->norm_float == SF_FALSE) + { count = done ; + while (--count >= 0) + { ptr [count] *= (double) 0x8000 ; + } ; + } ; + + return done ; +} /* mpeg_dec_read_f */ + +static inline void +f2d_array (const float *src, int count, double *dest, double normfact) +{ while (--count >= 0) + { dest [count] = src [count] * normfact ; + } +} /* f2d_array */ + +static sf_count_t +mpeg_dec_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) +{ MPEG_DEC_PRIVATE *pmp3d = (MPEG_DEC_PRIVATE *) psf->codec_data ; + sf_count_t done ; + double normfact ; + + normfact = (psf->norm_double == SF_TRUE) ? 1.0 : (double) 0x8000 ; + + done = mpeg_dec_decode (psf, pmp3d, (float *) ptr, len) ; + + if (done <= 0) + return 0 ; + + f2d_array ((float *) ptr, done, ptr, normfact) ; + + return done ; +} /* mpeg_dec_read_d */ + +static sf_count_t +mpeg_dec_seek (SF_PRIVATE *psf, int mode, sf_count_t count) +{ MPEG_DEC_PRIVATE *pmp3d = (MPEG_DEC_PRIVATE *) psf->codec_data ; + off_t ret ; + + if (mode != SFM_READ || psf->file.mode != SFM_READ) + { psf->error = SFE_BAD_SEEK ; + return PSF_SEEK_ERROR ; + } ; + + ret = mpg123_seek (pmp3d->pmh, count, SEEK_SET) ; + + if (ret < 0) + return PSF_SEEK_ERROR ; + + return (sf_count_t) ret ; +} /* mpeg_dec_seek */ + +static int +mpeg_dec_fill_sfinfo (mpg123_handle *mh, SF_INFO *info) +{ int error ; + int channels ; + int encoding ; + long rate ; + off_t length ; + + error = mpg123_getformat (mh, &rate, &channels, &encoding) ; + if (error != MPG123_OK) + return error ; + + info->samplerate = rate ; + info->channels = channels ; + + length = mpg123_length (mh) ; + if (length >= 0) + { info->frames = length ; + info->seekable = SF_TRUE ; + } + else + { info->frames = SF_COUNT_MAX ; + info->seekable = SF_FALSE ; + } + + /* Force 32-bit float samples. */ + if (encoding != MPG123_ENC_FLOAT_32) + { error = mpg123_format (mh, rate, channels, MPG123_ENC_FLOAT_32) ; + } ; + + return error ; +} /* mpeg_dec_fill_sfinfo */ + +static void +mpeg_dec_print_frameinfo (SF_PRIVATE *psf, const struct mpg123_frameinfo *fi) +{ psf_log_printf (psf, "MPEG-1/2 Audio\n----------------------------------------\n") ; + psf_log_printf (psf, " version: %s\n", + fi->version == MPG123_1_0 ? "MPEG 1.0" : + fi->version == MPG123_2_0 ? "MPEG 2.0" : + fi->version == MPG123_2_5 ? "MPEG 2.5" : "?") ; + psf_log_printf (psf, " layer: %d\n", fi->layer) ; + psf_log_printf (psf, " rate: %d\n", fi->rate) ; + psf_log_printf (psf, " mode: %s\n", + fi->mode == MPG123_M_STEREO ? "stereo" : + fi->mode == MPG123_M_JOINT ? "joint stereo" : + fi->mode == MPG123_M_DUAL ? "dual channel" : + fi->mode == MPG123_M_MONO ? "mono" : "?") ; + psf_log_printf (psf, " mode ext: %d\n", fi->mode_ext) ; + psf_log_printf (psf, " framesize: %d\n", fi->framesize) ; + psf_log_printf (psf, " crc: %c\n", fi->flags & MPG123_CRC ? '1' : '0') ; + psf_log_printf (psf, " copyright flag: %c\n", fi->flags & MPG123_COPYRIGHT ? '1' : '0') ; + psf_log_printf (psf, " private flag: %c\n", fi->flags & MPG123_PRIVATE ? '1' : '0') ; + psf_log_printf (psf, " original flag: %c\n", fi->flags & MPG123_ORIGINAL ? '1' : '0') ; + psf_log_printf (psf, " emphasis: %d\n", fi->emphasis) ; + psf_log_printf (psf, " bitrate mode: ") ; + switch (fi->vbr) + { case MPG123_CBR : + psf_log_printf (psf, "constant\n") ; + break ; + case MPG123_VBR : + psf_log_printf (psf, "variable\n") ; + break ; + + case MPG123_ABR : + psf_log_printf (psf, "average\n") ; + psf_log_printf (psf, " ABR target: %d\n", fi->abr_rate) ; + break ; + } ; + psf_log_printf (psf, " bitrate: %d kbps\n", fi->bitrate) ; +} /* mpeg_dec_print_frameinfo */ + +/* + * Like strlcpy, except the size argument is the maximum size of the input, + * always null terminates the output string. Thus, up to size + 1 bytes may be + * written. + * + * Returns the length of the copied string. + */ +static int +strcpy_inbounded (char *dest, size_t size, const char *src) +{ char *c = memccpy (dest, src, '\0', size) ; + if (!c) + c = dest + size ; + *c = '\0' ; + return c - dest ; +} /* strcpy_inbounded */ + +static void +mpeg_decoder_read_strings_id3v1 (SF_PRIVATE *psf, mpg123_id3v1 *tags) +{ const char *genre ; + char buf [31] ; + + psf_log_printf (psf, "ID3v1 Tags\n") ; + + if (strcpy_inbounded (buf, ARRAY_LEN (tags->title), tags->title)) + { psf_log_printf (psf, " Title : %s\n", buf) ; + psf_store_string (psf, SF_STR_TITLE, buf) ; + } ; + + if (strcpy_inbounded (buf, ARRAY_LEN (tags->artist), tags->artist)) + { psf_log_printf (psf, " Artist : %s\n", buf) ; + psf_store_string (psf, SF_STR_ARTIST, buf) ; + } ; + + if (strcpy_inbounded (buf, ARRAY_LEN (tags->album), tags->album)) + { psf_log_printf (psf, " Album : %s\n", buf) ; + psf_store_string (psf, SF_STR_ALBUM, buf) ; + } ; + + if (strcpy_inbounded (buf, ARRAY_LEN (tags->year), tags->year)) + { psf_log_printf (psf, " Year : %s\n", buf) ; + psf_store_string (psf, SF_STR_DATE, buf) ; + } ; + + if (strcpy_inbounded (buf, ARRAY_LEN (tags->comment), tags->comment)) + { psf_log_printf (psf, " Comment : %s\n", buf) ; + psf_store_string (psf, SF_STR_COMMENT, buf) ; + } ; + + /* ID3v1.1 Tracknumber */ + if (tags->comment [28] == '\0' && tags->comment [29] != '\0') + { snprintf (buf, ARRAY_LEN (buf), "%hhu", (unsigned char) tags->comment [29]) ; + psf_log_printf (psf, " Tracknumber : %s\n", buf) ; + psf_store_string (psf, SF_STR_TRACKNUMBER, buf) ; + } ; + + if ((genre = id3_lookup_v1_genre (tags->genre)) != NULL) + { psf_log_printf (psf, " Genre : %s\n", genre) ; + psf_store_string (psf, SF_STR_GENRE, genre) ; + } ; +} /* mpeg_decoder_read_strings_id3v1 */ + +static void +mpeg_decoder_read_strings_id3v2 (SF_PRIVATE *psf, mpg123_id3v2 *tags) +{ mpg123_text *text_frame ; + size_t i ; + uint32_t marker ; + const char *title = NULL ; + const char *copyright = NULL ; + const char *software = NULL ; + const char *artist = NULL ; + const char *comment = NULL ; + const char *date = NULL ; + const char *album = NULL ; + const char *license = NULL ; + const char *tracknumber = NULL ; + const char *genre = NULL ; + const char *tlen = NULL ; + + psf_log_printf (psf, "ID3v2 Tags\n") ; + + // Read the parsed text tags + for (i = 0 ; i < tags->texts ; i++) + { text_frame = &tags->text [i] ; + psf_log_printf (psf, " %.4s : %s\n", text_frame->id, text_frame->text.p) ; + + // Thankfully mpg123 translates v2.2 3-byte frames to v2.3 4-byte for us. + marker = MAKE_MARKER (text_frame->id [0], text_frame->id [1], + text_frame->id [2], text_frame->id [3]) ; + + /* Use our own map of frame types to metadata for text frames */ + switch (marker) + { case MAKE_MARKER ('T', 'I', 'T', '2') : + title = text_frame->text.p ; + break ; + + case MAKE_MARKER ('T', 'C', 'O', 'P') : + copyright = text_frame->text.p ; + break ; + + case MAKE_MARKER ('T', 'E', 'N', 'C') : + case MAKE_MARKER ('T', 'S', 'S', 'E') : + software = text_frame->text.p ; + break ; + + case MAKE_MARKER ('T', 'P', 'E', '1') : + artist = text_frame->text.p ; + break ; + + case MAKE_MARKER ('T', 'A', 'L', 'B') : + album = text_frame->text.p ; + break ; + + case MAKE_MARKER ('T', 'R', 'C', 'K') : + tracknumber = text_frame->text.p ; + break ; + + case MAKE_MARKER ('T', 'Y', 'E', 'R') : + case MAKE_MARKER ('T', 'D', 'R', 'C') : + /* TODO (maybe) + case MAKE_MARKER ('T', 'D', 'A', 'T') : + case MAKE_MARKER ('T', 'I', 'M', 'E') : + case MAKE_MARKER ('T', 'D', 'R', 'A') : + */ + date = text_frame->text.p ; + break ; + + case MAKE_MARKER ('T', 'O', 'W', 'N') : + tracknumber = text_frame->text.p ; + break ; + + case MAKE_MARKER ('T', 'C', 'O', 'N') : + genre = text_frame->text.p ; + break ; + + case MAKE_MARKER ('T', 'L', 'E', 'N') : + tlen = text_frame->text.p ; + break ; + } ; + } ; + + /* Use mpg123's handling of comment headers, but print all the comment headers anyways. */ + if (tags->comment) + comment = tags->comment->p ; + for (i = 0 ; i < tags->comments ; i++) + { text_frame = &tags->comment_list [i] ; + psf_log_printf (psf, " %.4s : (%s)[%s] %s\n", text_frame->id, + text_frame->description. p, text_frame->lang, text_frame->text.p) ; + } ; + + /* Print extra headers */ + for (i = 0 ; i < tags->extras ; i++) + { text_frame = &tags->extra [i] ; + psf_log_printf (psf, " %.4s : (%s) %s\n", text_frame->id, + text_frame->description.p, text_frame->text.p) ; + } ; + + if (title) + psf_store_string (psf, SF_STR_TITLE, title) ; + if (copyright) + psf_store_string (psf, SF_STR_COPYRIGHT, copyright) ; + if (software) + psf_store_string (psf, SF_STR_SOFTWARE, software) ; + if (artist) + psf_store_string (psf, SF_STR_ARTIST, artist) ; + if (comment) + psf_store_string (psf, SF_STR_COMMENT, comment) ; + if (date) + psf_store_string (psf, SF_STR_DATE, date) ; + if (album) + psf_store_string (psf, SF_STR_ALBUM, album) ; + if (license) + psf_store_string (psf, SF_STR_LICENSE, license) ; + if (tracknumber) + psf_store_string (psf, SF_STR_TRACKNUMBER, tracknumber) ; + if (genre) + psf_store_string (psf, SF_STR_GENRE, id3_process_v2_genre (genre)) ; + if (tlen) + { /* If non-seekable, set framecount? Can we trust it? */ + } ; +} /* mpeg_decoder_read_strings_id3v2 */ + +static void +mpeg_decoder_read_strings (SF_PRIVATE *psf) +{ MPEG_DEC_PRIVATE *pmp3d = (MPEG_DEC_PRIVATE *) psf->codec_data ; + mpg123_id3v1 *v1_tags ; + mpg123_id3v2 *v2_tags ; + + if (mpg123_id3 (pmp3d->pmh, &v1_tags, &v2_tags) != MPG123_OK) + return ; + + if (v1_tags != NULL) + mpeg_decoder_read_strings_id3v1 (psf, v1_tags) ; + + if (v2_tags != NULL) + mpeg_decoder_read_strings_id3v2 (psf, v2_tags) ; +} /* mpeg_decoder_read_strings */ + +static int +mpeg_dec_byterate (SF_PRIVATE *psf) +{ MPEG_DEC_PRIVATE *pmp3d = (MPEG_DEC_PRIVATE *) psf->codec_data ; + struct mpg123_frameinfo fi ; + + if (mpg123_info (pmp3d->pmh, &fi) == MPG123_OK) + return (fi.bitrate + 7) / 8 ; + + return -1 ; + +} /* mpeg_dec_byterate */ + +/*============================================================================== +** exported functions +*/ + +int +mpeg_decoder_init (SF_PRIVATE *psf) +{ MPEG_DEC_PRIVATE *pmp3d ; + struct mpg123_frameinfo fi ; + int error ; + + if (! (psf->file.mode & SFM_READ)) + return SFE_INTERNAL ; + + /* + ** Check to see if we have already successfully opened the file when we + ** guessed it was an MP3 based on seeing an ID3 header. + */ + if (psf->codec_data != NULL && + (SF_CODEC (psf->sf.format) == SF_FORMAT_MPEG_LAYER_I + || SF_CODEC (psf->sf.format) == SF_FORMAT_MPEG_LAYER_II + || SF_CODEC (psf->sf.format) == SF_FORMAT_MPEG_LAYER_III) + && ((MPEG_DEC_PRIVATE *) psf->codec_data)->unique_id == psf->unique_id) + return 0 ; + + /* + ** *** FIXME - Threading issues *** + ** + ** mpg123_init() is a global call that should only be called once, and + ** should be paried with mpg123_exit() when done. libsndfile does not + ** provide for these requirements. + ** + ** Currently this is a moot issue as mpg123_init() non-conditionally writes + ** static areas with calculated data, and mpg123_exit() is a NOP, but this + ** could change in a future version of it! + ** + ** From mpg123.h: + ** > This should be called once in a non-parallel context. It is not explicitly + ** > thread-safe, but repeated/concurrent calls still _should_ be safe as static + ** > tables are filled with the same values anyway. + ** + ** Note that calling mpg123_init() after it has already completed is a NOP. + */ + if (mpg123_init () != MPG123_OK) + return SFE_INTERNAL ; + + psf->codec_data = pmp3d = calloc (1, sizeof (MPEG_DEC_PRIVATE)) ; + if (!psf->codec_data) + return SFE_MALLOC_FAILED ; + + pmp3d->pmh = mpg123_new (NULL, &error) ; + if (!pmp3d->pmh) + { psf_log_printf (psf, "Could not obtain a mpg123 handle: %s\n", mpg123_plain_strerror (error)) ; + return SFE_INTERNAL ; + } ; + + psf->codec_close = mpeg_dec_close ; + + mpg123_replace_reader_handle (pmp3d->pmh, + mpeg_dec_io_read, mpeg_dec_io_lseek, NULL) ; + + mpg123_param (pmp3d->pmh, MPG123_REMOVE_FLAGS, MPG123_AUTO_RESAMPLE, 1.0) ; + mpg123_param (pmp3d->pmh, MPG123_ADD_FLAGS, MPG123_FORCE_FLOAT | MPG123_GAPLESS, 1.0) ; +#if MPG123_API_VERSION >= 45 + mpg123_param (pmp3d->pmh, MPG123_ADD_FLAGS, MPG123_NO_FRANKENSTEIN, 1.0) ; +#endif + + psf->dataoffset = 0 ; + + if (psf->is_pipe) + { // Need to read data in the binheader buffer. Avoid a file seek. + psf_binheader_readf (psf, "p", 0) ; + pmp3d->header_remaining = psf_binheader_readf (psf, NULL) ; + mpg123_param (pmp3d->pmh, MPG123_ADD_FLAGS, MPG123_NO_PEEK_END, 1.0) ; + } + else + psf_fseek (psf, 0, SEEK_SET) ; + + error = mpg123_open_handle (pmp3d->pmh, psf) ; + if (error != MPG123_OK) + { psf_log_printf (psf, "mpg123 could not open the file: %s\n", mpg123_plain_strerror (error)) ; + return SFE_BAD_FILE ; + } ; + + if (mpeg_dec_fill_sfinfo (pmp3d->pmh, &psf->sf) != MPG123_OK) + { psf_log_printf (psf, "Cannot get MPEG decoder configuration: %s\n", mpg123_plain_strerror (error)) ; + return SFE_BAD_FILE ; + } ; + + error = mpg123_info (pmp3d->pmh, &fi) ; + if (error != MPG123_OK) + { psf_log_printf (psf, "Cannot get MPEG frame info: %s\n", mpg123_plain_strerror (error)) ; + return SFE_INTERNAL ; + } + + switch (fi.layer) + { case 1 : psf->sf.format |= SF_FORMAT_MPEG_LAYER_I ; break ; + case 2 : psf->sf.format |= SF_FORMAT_MPEG_LAYER_II ; break ; + case 3 : psf->sf.format |= SF_FORMAT_MPEG_LAYER_III ; break ; + default : + return SFE_BAD_FILE ; + } ; + mpeg_dec_print_frameinfo (psf, &fi) ; + + psf->read_short = mpeg_dec_read_s ; + psf->read_int = mpeg_dec_read_i ; + psf->read_float = mpeg_dec_read_f ; + psf->read_double = mpeg_dec_read_d ; + psf->seek = mpeg_dec_seek ; + psf->byterate = mpeg_dec_byterate ; + + mpeg_decoder_read_strings (psf) ; + + if (psf->filelength != SF_COUNT_MAX) + psf->datalength = psf->filelength - psf->dataoffset ; + else + psf->datalength = SF_COUNT_MAX ; + + pmp3d->unique_id = psf->unique_id ; + return 0 ; +} /* mpeg_decoder_init */ + +int +mpeg_decoder_get_bitrate_mode (SF_PRIVATE *psf) +{ MPEG_DEC_PRIVATE *pmp3d = (MPEG_DEC_PRIVATE *) psf->codec_data ; + struct mpg123_frameinfo fi ; + + if (mpg123_info (pmp3d->pmh, &fi) == MPG123_OK) + { + switch (fi.vbr) + { case MPG123_CBR : return SF_BITRATE_MODE_CONSTANT ; + case MPG123_ABR : return SF_BITRATE_MODE_AVERAGE ; + case MPG123_VBR : return SF_BITRATE_MODE_VARIABLE ; + default : break ; + } ; + } ; + + psf_log_printf (psf, "Cannot determine MPEG bitrate mode.\n") ; + return -1 ; +} /* mpeg_decoder_get_bitrate_mode */ + +#else /* HAVE_MPEG */ + +int mpeg_decoder_init (SF_PRIVATE *psf) +{ psf_log_printf (psf, "This version of libsndfile was compiled without MPEG decode support.\n") ; + return SFE_UNIMPLEMENTED ; +} /* mpeg_decoder_init */ + +#endif diff --git a/src/mpeg_l3_encode.c b/src/mpeg_l3_encode.c new file mode 100644 index 00000000..1c7f2ae6 --- /dev/null +++ b/src/mpeg_l3_encode.c @@ -0,0 +1,784 @@ +/* +** Copyright (C) 2020 Arthur Taylor +** Copyright (C) 2019 Erik de Castro Lopo +** +** This program is free software ; you can redistribute it and/or modify +** it under the terms of the GNU Lesser General Public License as published by +** the Free Software Foundation ; either version 2.1 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY ; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU Lesser General Public License for more details. +** +** You should have received a copy of the GNU Lesser General Public License +** along with this program ; if not, write to the Free Software +** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include "sfconfig.h" +#include "sndfile.h" +#include "common.h" +#include "mpeg.h" + + +#if HAVE_MPEG + +#include + +/* + * RANT RANT RANT + * + * Lame has 11 functions for inputing sample data of various types and + * configurations, but due to bad definitions, or missing combinations, they + * aren't really of much help to us. + * + */ + +typedef struct +{ lame_t lamef ; + unsigned char *block ; + size_t block_len ; + int frame_samples ; + double compression ; + int initialized : 1 ; +} MPEG_L3_ENC_PRIVATE ; + + +/*----------------------------------------------------------------------------------------------- +** Private function prototypes. +*/ + +static int mpeg_l3_encoder_close (SF_PRIVATE *psf) ; +static int mpeg_l3_encoder_construct (SF_PRIVATE *psf) ; +static int mpeg_l3_encoder_byterate (SF_PRIVATE *psf) ; + +static sf_count_t mpeg_l3_encode_write_short_stereo (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ; +static sf_count_t mpeg_l3_encode_write_int_stereo (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ; +static sf_count_t mpeg_l3_encode_write_float_stereo (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ; +static sf_count_t mpeg_l3_encode_write_double_stereo (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ; +static sf_count_t mpeg_l3_encode_write_short_mono (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ; +static sf_count_t mpeg_l3_encode_write_int_mono (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ; +static sf_count_t mpeg_l3_encode_write_float_mono (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ; +static sf_count_t mpeg_l3_encode_write_double_mono (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ; + +/*----------------------------------------------------------------------------------------------- +** Exported functions. +*/ + +int +mpeg_l3_encoder_init (SF_PRIVATE *psf, int info_tag) +{ MPEG_L3_ENC_PRIVATE* pmpeg = NULL ; + + if (psf->file.mode == SFM_RDWR) + return SFE_BAD_MODE_RW ; + + if (psf->file.mode != SFM_WRITE) + return SFE_INTERNAL ; + + psf->codec_data = pmpeg = calloc (1, sizeof (MPEG_L3_ENC_PRIVATE)) ; + if (!pmpeg) + return SFE_MALLOC_FAILED ; + + if (psf->sf.channels < 1 || psf->sf.channels > 2) + return SFE_BAD_OPEN_FORMAT ; + + if (! (pmpeg->lamef = lame_init ())) + return SFE_MALLOC_FAILED ; + + pmpeg->compression = -1.0 ; /* Unset */ + + lame_set_in_samplerate (pmpeg->lamef, psf->sf.samplerate) ; + lame_set_num_channels (pmpeg->lamef, psf->sf.channels) ; + if (lame_set_out_samplerate (pmpeg->lamef, psf->sf.samplerate) < 0) + return SFE_MPEG_BAD_SAMPLERATE ; + + lame_set_write_id3tag_automatic (pmpeg->lamef, 0) ; + + if (!info_tag || psf->is_pipe) + { /* Can't seek back, so force disable Xing/Lame/Info header. */ + lame_set_bWriteVbrTag (pmpeg->lamef, 0) ; + } ; + + if (psf->sf.channels == 2) + { psf->write_short = mpeg_l3_encode_write_short_stereo ; + psf->write_int = mpeg_l3_encode_write_int_stereo ; + psf->write_float = mpeg_l3_encode_write_float_stereo ; + psf->write_double = mpeg_l3_encode_write_double_stereo ; + } + else + { psf->write_short = mpeg_l3_encode_write_short_mono ; + psf->write_int = mpeg_l3_encode_write_int_mono ; + psf->write_float = mpeg_l3_encode_write_float_mono ; + psf->write_double = mpeg_l3_encode_write_double_mono ; + } + + psf->sf.seekable = 0 ; + psf->codec_close = mpeg_l3_encoder_close ; + psf->byterate = mpeg_l3_encoder_byterate ; + psf->dataoffset = 0 ; + psf->datalength = 0 ; + + return 0 ; +} /* mpeg_l3_encoder_init */ + +int +mpeg_l3_encoder_write_id3tag (SF_PRIVATE *psf) +{ MPEG_L3_ENC_PRIVATE *pmpeg = (MPEG_L3_ENC_PRIVATE *) psf->codec_data ; + unsigned char *id3v2_buffer ; + int i, id3v2_size ; + + if (psf->have_written) + return 0 ; + + if ((i = mpeg_l3_encoder_construct (psf))) + return i ; + + if (psf_fseek (psf, 0, SEEK_SET) != 0) + return SFE_NOT_SEEKABLE ; + + /* Safe to call multiple times. */ + id3tag_init (pmpeg->lamef) ; + + for (i = 0 ; i < SF_MAX_STRINGS ; i++) + { switch (psf->strings.data [i].type) + { case SF_STR_TITLE : + id3tag_set_title (pmpeg->lamef, psf->strings.storage + psf->strings.data [i].offset) ; + break ; + + case SF_STR_ARTIST : + id3tag_set_artist (pmpeg->lamef, psf->strings.storage + psf->strings.data [i].offset) ; + break ; + + case SF_STR_ALBUM : + id3tag_set_album (pmpeg->lamef, psf->strings.storage + psf->strings.data [i].offset) ; + break ; + + case SF_STR_DATE : + id3tag_set_year (pmpeg->lamef, psf->strings.storage + psf->strings.data [i].offset) ; + break ; + + case SF_STR_COMMENT : + id3tag_set_comment (pmpeg->lamef, psf->strings.storage + psf->strings.data [i].offset) ; + break ; + + case SF_STR_GENRE : + id3tag_set_genre (pmpeg->lamef, psf->strings.storage + psf->strings.data [i].offset) ; + break ; + + case SF_STR_TRACKNUMBER : + id3tag_set_track (pmpeg->lamef, psf->strings.storage + psf->strings.data [i].offset) ; + break ; + + default: + break ; + } ; + } ; + + /* The header in this case is the ID3v2 tag header. */ + id3v2_size = lame_get_id3v2_tag (pmpeg->lamef, 0, 0) ; + if (id3v2_size > 0) + { psf_log_printf (psf, "Writing ID3v2 header.\n") ; + if (! (id3v2_buffer = malloc (id3v2_size))) + return SFE_MALLOC_FAILED ; + lame_get_id3v2_tag (pmpeg->lamef, id3v2_buffer, id3v2_size) ; + psf_fwrite (id3v2_buffer, 1, id3v2_size, psf) ; + psf->dataoffset = id3v2_size ; + free (id3v2_buffer) ; + } ; + + return 0 ; +} + +int +mpeg_l3_encoder_set_quality (SF_PRIVATE *psf, double compression) +{ MPEG_L3_ENC_PRIVATE *pmpeg = (MPEG_L3_ENC_PRIVATE *) psf->codec_data ; + int bitrate_mode ; + int bitrate ; + int ret ; + + if (compression < 0.0 || compression > 1.0) + return SF_FALSE ; + + /* + ** Save the compression setting, as we may have to re-interpret it if + ** the bitrate mode changes. + */ + pmpeg->compression = compression ; + + bitrate_mode = mpeg_l3_encoder_get_bitrate_mode (psf) ; + if (bitrate_mode == SF_BITRATE_MODE_VARIABLE) + { ret = lame_set_VBR_quality (pmpeg->lamef, compression * 10.0) ; + } + else + { /* Choose a bitrate. */ + if (psf->sf.samplerate >= 32000) + { /* MPEG-1.0, bitrates are [32,320] kbps */ + bitrate = (320.0 - (compression * (320.0 - 32.0))) ; + } + else if (psf->sf.samplerate >= 16000) + { /* MPEG-2.0, bitrates are [8,160] */ + bitrate = (160.0 - (compression * (160.0 - 8.0))) ; + } + else + { /* MPEG-2.5, bitrates are [8,64] */ + bitrate = (64.0 - (compression * (64.0 - 8.0))) ; + } + + if (bitrate_mode == SF_BITRATE_MODE_AVERAGE) + ret = lame_set_VBR_mean_bitrate_kbps (pmpeg->lamef, bitrate) ; + else + ret = lame_set_brate (pmpeg->lamef, bitrate) ; + } ; + + if (ret == LAME_OKAY) + return SF_TRUE ; + + psf_log_printf (psf, "Failed to set lame encoder quality.\n") ; + return SF_FALSE ; +} /* mpeg_l3_encoder_set_quality */ + +int +mpeg_l3_encoder_set_bitrate_mode (SF_PRIVATE *psf, int mode) +{ MPEG_L3_ENC_PRIVATE *pmpeg = (MPEG_L3_ENC_PRIVATE *) psf->codec_data ; + enum vbr_mode_e vbr_mode ; + + if (pmpeg->initialized) + { psf->error = SFE_CMD_HAS_DATA ; + return SF_FALSE ; + } ; + + switch (mode) + { case SF_BITRATE_MODE_CONSTANT : vbr_mode = vbr_off ; break ; + case SF_BITRATE_MODE_AVERAGE : vbr_mode = vbr_abr ; break ; + case SF_BITRATE_MODE_VARIABLE : vbr_mode = vbr_default ; break ; + default : + psf->error = SFE_BAD_COMMAND_PARAM ; + return SF_FALSE ; + } ; + + if (lame_set_VBR (pmpeg->lamef, vbr_mode) == LAME_OKAY) + { /* Re-evaluate the compression setting. */ + return mpeg_l3_encoder_set_quality (psf, pmpeg->compression) ; + } ; + + psf_log_printf (psf, "Failed to set LAME vbr mode to %d.\n", vbr_mode) ; + return SF_FALSE ; +} /* mpeg_l3_encoder_set_bitrate_mode */ + +int +mpeg_l3_encoder_get_bitrate_mode (SF_PRIVATE *psf) +{ MPEG_L3_ENC_PRIVATE *pmpeg = (MPEG_L3_ENC_PRIVATE *) psf->codec_data ; + enum vbr_mode_e vbr_mode ; + + vbr_mode = lame_get_VBR (pmpeg->lamef) ; + + if (vbr_mode == vbr_off) + return SF_BITRATE_MODE_CONSTANT ; + if (vbr_mode == vbr_abr) + return SF_BITRATE_MODE_AVERAGE ; + if (vbr_mode == vbr_default || vbr_mode < vbr_max_indicator) + return SF_BITRATE_MODE_VARIABLE ; + + /* Something is wrong. */ + psf->error = SFE_INTERNAL ; + return -1 ; +} /* mpeg_l3_encoder_get_bitrate_mode */ + + +/*----------------------------------------------------------------------------------------------- +** Private functions. +*/ + +static int +mpeg_l3_encoder_close (SF_PRIVATE *psf) +{ MPEG_L3_ENC_PRIVATE* pmpeg = (MPEG_L3_ENC_PRIVATE *) psf->codec_data ; + int ret, len ; + sf_count_t pos ; + unsigned char *buffer ; + + /* Magic number 7200 comes from a comment in lame.h */ + len = 7200 ; + if (! (buffer = malloc (len))) + return SFE_MALLOC_FAILED ; + ret = lame_encode_flush (pmpeg->lamef, buffer, len) ; + if (ret > 0) + psf_fwrite (buffer, 1, ret, psf) ; + + /* + ** Write an IDv1 trailer. The whole tag structure is always 128 bytes, so is + ** guaranteed to fit in the buffer allocated above. + */ + ret = lame_get_id3v1_tag (pmpeg->lamef, buffer, len) ; + if (ret > 0) + { psf_log_printf (psf, " Writing ID3v1 trailer.\n") ; + psf_fwrite (buffer, 1, ret, psf) ; + } ; + + /* + ** If possible, seek back and write the LAME/XING/Info headers. This + ** contains information about the whole file and a seek table, and can + ** only be written after encoding. + ** + ** If enabled, Lame wrote an empty header at the beginning of the data + ** that we now fill in. + */ + ret = lame_get_lametag_frame (pmpeg->lamef, 0, 0) ; + if (ret > 0) + { if (ret > len) + { len = ret ; + free (buffer) ; + if (! (buffer = malloc (len))) + return SFE_MALLOC_FAILED ; + } ; + psf_log_printf (psf, " Writing LAME info header at offset %d, %d bytes.\n", + psf->dataoffset, len) ; + lame_get_lametag_frame (pmpeg->lamef, buffer, len) ; + pos = psf_ftell (psf) ; + if (psf_fseek (psf, psf->dataoffset, SEEK_SET) == psf->dataoffset) + { psf_fwrite (buffer, 1, ret, psf) ; + psf_fseek (psf, pos, SEEK_SET) ; + } ; + } ; + free (buffer) ; + + free (pmpeg->block) ; + pmpeg->block = NULL ; + + if (pmpeg->lamef) + { lame_close (pmpeg->lamef) ; + pmpeg->lamef = NULL ; + } ; + + return 0 ; +} /* mpeg_l3_encoder_close */ + +static void +mpeg_l3_encoder_log_config (SF_PRIVATE *psf, lame_t lamef) +{ const char *version ; + const char *chn_mode ; + + switch (lame_get_version (lamef)) + { case 0 : version = "2" ; break ; + case 1 : version = "1" ; break ; + case 2 : version = "2.5" ; break ; + default : version = "unknown!?" ; break ; + } ; + switch (lame_get_mode (lamef)) + { case STEREO : chn_mode = "stereo" ; break ; + case JOINT_STEREO : chn_mode = "joint-stereo" ; break ; + case MONO : chn_mode = "mono" ; break ; + default : chn_mode = "unknown!?" ; break ; + } ; + psf_log_printf (psf, " MPEG Version : %s\n", version) ; + psf_log_printf (psf, " Channel mode : %s\n", chn_mode) ; + psf_log_printf (psf, " Samplerate : %d\n", lame_get_out_samplerate (lamef)) ; + psf_log_printf (psf, " Encoder mode : ") ; + switch (lame_get_VBR (lamef)) + { case vbr_off : + psf_log_printf (psf, "CBR\n") ; + psf_log_printf (psf, " Bitrate : %d kbps\n", lame_get_brate (lamef)) ; + break ; + case vbr_abr : + psf_log_printf (psf, "ABR\n") ; + psf_log_printf (psf, " Mean Bitrate : %d kbps\n", lame_get_VBR_mean_bitrate_kbps (lamef)) ; + break ; + + case vbr_mt : + case vbr_default : + psf_log_printf (psf, "VBR\n") ; + psf_log_printf (psf, " Quality : %d\n", lame_get_VBR_q (lamef)) ; + break ; + + default: + psf_log_printf (psf, "Unknown!? (%d)\n", lame_get_VBR (lamef)) ; + break ; + } ; + + psf_log_printf (psf, " Encoder delay : %d\n", lame_get_encoder_delay (lamef)) ; + psf_log_printf (psf, " Write INFO header : %d\n", lame_get_bWriteVbrTag (lamef)) ; +} /* mpeg_l3_encoder_log_config */ + +static int +mpeg_l3_encoder_construct (SF_PRIVATE *psf) +{ MPEG_L3_ENC_PRIVATE *pmpeg = (MPEG_L3_ENC_PRIVATE *) psf->codec_data ; + int frame_samples_per_channel ; + + if (pmpeg->initialized == SF_FALSE) + { if (lame_init_params (pmpeg->lamef) < 0) + { psf_log_printf (psf, "Failed to initialize lame encoder!\n") ; + return SFE_INTERNAL ; + } ; + + psf_log_printf (psf, "Initialized LAME encoder.\n") ; + mpeg_l3_encoder_log_config (psf, pmpeg->lamef) ; + + frame_samples_per_channel = lame_get_framesize (pmpeg->lamef) ; + + /* + * Suggested output buffer size in bytes from lame.h comment is + * 1.25 * samples + 7200 + */ + pmpeg->block_len = (frame_samples_per_channel * 4) / 3 + 7200 ; + pmpeg->frame_samples = frame_samples_per_channel * psf->sf.channels ; + + pmpeg->block = malloc (pmpeg->block_len) ; + if (!pmpeg->block) + return SFE_MALLOC_FAILED ; + + pmpeg->initialized = SF_TRUE ; + } ; + + return 0 ; +} /* mpeg_l3_encoder_construct */ + +static int +mpeg_l3_encoder_byterate (SF_PRIVATE *psf) +{ MPEG_L3_ENC_PRIVATE *pmpeg = (MPEG_L3_ENC_PRIVATE *) psf->codec_data ; + int bitrate_mode ; + int byterate ; + float calculated_byterate ; + + bitrate_mode = mpeg_l3_encoder_get_bitrate_mode (psf) ; + byterate = (lame_get_brate (pmpeg->lamef) + 7) / 8 ; + + if (bitrate_mode == SF_BITRATE_MODE_VARIABLE) + { /* + ** For VBR, lame_get_brate returns the minimum bitrate, so calculate the + ** average byterate so far. + */ + calculated_byterate = psf_ftell (psf) - psf->dataoffset ; + calculated_byterate /= (float) psf->write_current ; + calculated_byterate *= (float) psf->sf.samplerate ; + + return SF_MIN (byterate, (int) calculated_byterate) ; + } + + return byterate ; +} /* mpeg_l3_encoder_byterate */ + +static sf_count_t +mpeg_l3_encode_write_short_mono (SF_PRIVATE *psf, const short *ptr, sf_count_t len) +{ MPEG_L3_ENC_PRIVATE *pmpeg = (MPEG_L3_ENC_PRIVATE*) psf->codec_data ; + sf_count_t total = 0 ; + int nbytes, writecount, writen ; + + if ((psf->error = mpeg_l3_encoder_construct (psf))) + return 0 ; + + while (len) + { writecount = SF_MIN (len, (sf_count_t) pmpeg->frame_samples) ; + + nbytes = lame_encode_buffer (pmpeg->lamef, ptr + total, NULL, writecount, pmpeg->block, pmpeg->block_len) ; + if (nbytes < 0) + { psf_log_printf (psf, "lame_encode_buffer returned %d\n", nbytes) ; + break ; + } ; + + if (nbytes) + { writen = psf_fwrite (pmpeg->block, 1, nbytes, psf) ; + if (writen != nbytes) + { psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", writen, nbytes) ; + } ; + } ; + + total += writecount ; + len -= writecount ; + } ; + + return total ; +} + + +static sf_count_t +mpeg_l3_encode_write_short_stereo (SF_PRIVATE *psf, const short *ptr, sf_count_t len) +{ BUF_UNION ubuf ; + MPEG_L3_ENC_PRIVATE *pmpeg = (MPEG_L3_ENC_PRIVATE*) psf->codec_data ; + sf_count_t total = 0 ; + int nbytes, writecount, writen ; + const sf_count_t max_samples = SF_MIN (ARRAY_LEN (ubuf.sbuf), pmpeg->frame_samples) ; + + if ((psf->error = mpeg_l3_encoder_construct (psf))) + return 0 ; + + while (len) + { writecount = SF_MIN (len, max_samples) ; + /* + * An oversight, but lame_encode_buffer_interleaved() lacks a const. + * As such, need another memcpy to not cause a warning. + */ + memcpy (ubuf.sbuf, ptr + total, writecount) ; + nbytes = lame_encode_buffer_interleaved (pmpeg->lamef, ubuf.sbuf, writecount / 2, pmpeg->block, pmpeg->block_len) ; + if (nbytes < 0) + { psf_log_printf (psf, "lame_encode_buffer returned %d\n", nbytes) ; + break ; + } ; + + if (nbytes) + { writen = psf_fwrite (pmpeg->block, 1, nbytes, psf) ; + if (writen != nbytes) + { psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", writen, nbytes) ; + } ; + } ; + + total += writecount ; + len -= writecount ; + } ; + + return total ; +} + + +static sf_count_t +mpeg_l3_encode_write_int_mono (SF_PRIVATE *psf, const int *ptr, sf_count_t len) +{ MPEG_L3_ENC_PRIVATE *pmpeg = (MPEG_L3_ENC_PRIVATE*) psf->codec_data ; + sf_count_t total = 0 ; + int nbytes, writecount, writen ; + + if ((psf->error = mpeg_l3_encoder_construct (psf))) + return 0 ; + + while (len) + { writecount = SF_MIN (len, (sf_count_t) pmpeg->frame_samples) ; + + nbytes = lame_encode_buffer_int (pmpeg->lamef, ptr + total, NULL, writecount, pmpeg->block, pmpeg->block_len) ; + if (nbytes < 0) + { psf_log_printf (psf, "lame_encode_buffer returned %d\n", nbytes) ; + break ; + } ; + + if (nbytes) + { writen = psf_fwrite (pmpeg->block, 1, nbytes, psf) ; + if (writen != nbytes) + { psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", writen, nbytes) ; + } ; + } ; + + total += writecount ; + len -= writecount ; + } ; + + return total ; +} + + +static sf_count_t +mpeg_l3_encode_write_int_stereo (SF_PRIVATE *psf, const int *ptr, sf_count_t len) +{ MPEG_L3_ENC_PRIVATE *pmpeg = (MPEG_L3_ENC_PRIVATE*) psf->codec_data ; + sf_count_t total = 0 ; + int nbytes, writecount, writen ; + + if ((psf->error = mpeg_l3_encoder_construct (psf))) + return 0 ; + + while (len) + { writecount = SF_MIN (len, (sf_count_t) pmpeg->frame_samples) ; + + nbytes = lame_encode_buffer_interleaved_int (pmpeg->lamef, ptr + total, writecount / 2, pmpeg->block, pmpeg->block_len) ; + if (nbytes < 0) + { psf_log_printf (psf, "lame_encode_buffer returned %d\n", nbytes) ; + break ; + } ; + + if (nbytes) + { writen = psf_fwrite (pmpeg->block, 1, nbytes, psf) ; + if (writen != nbytes) + { psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", writen, nbytes) ; + } ; + } ; + + total += writecount ; + len -= writecount ; + } ; + + return total ; +} + + +static sf_count_t +mpeg_l3_encode_write_float_mono (SF_PRIVATE *psf, const float *ptr, sf_count_t len) +{ MPEG_L3_ENC_PRIVATE *pmpeg = (MPEG_L3_ENC_PRIVATE*) psf->codec_data ; + sf_count_t total = 0 ; + int nbytes, writecount, writen ; + + if ((psf->error = mpeg_l3_encoder_construct (psf))) + return 0 ; + + while (len) + { writecount = SF_MIN (len, (sf_count_t) pmpeg->frame_samples) ; + + if (psf->norm_float) + nbytes = lame_encode_buffer_ieee_float (pmpeg->lamef, ptr + total, NULL, writecount, pmpeg->block, pmpeg->block_len) ; + else + nbytes = lame_encode_buffer_float (pmpeg->lamef, ptr + total, NULL, writecount, pmpeg->block, pmpeg->block_len) ; + if (nbytes < 0) + { psf_log_printf (psf, "lame_encode_buffer returned %d\n", nbytes) ; + break ; + } ; + + if (nbytes) + { writen = psf_fwrite (pmpeg->block, 1, nbytes, psf) ; + if (writen != nbytes) + { psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", writen, nbytes) ; + } ; + } ; + + total += writecount ; + len -= writecount ; + } ; + + return total ; +} + + +static inline void +normalize_float (float *dest, const float *src, sf_count_t count, float norm_fact) +{ while (--count >= 0) + { dest [count] = src [count] * norm_fact ; + } ; +} + + +static sf_count_t +mpeg_l3_encode_write_float_stereo (SF_PRIVATE *psf, const float *ptr, sf_count_t len) +{ BUF_UNION ubuf ; + MPEG_L3_ENC_PRIVATE *pmpeg = (MPEG_L3_ENC_PRIVATE*) psf->codec_data ; + sf_count_t total = 0 ; + int nbytes, writecount, writen ; + const sf_count_t max_samples = SF_MIN (ARRAY_LEN (ubuf.fbuf), pmpeg->frame_samples) ; + + if ((psf->error = mpeg_l3_encoder_construct (psf))) + return 0 ; + + while (len) + { writecount = SF_MIN (len, max_samples) ; + + if (psf->norm_float) + nbytes = lame_encode_buffer_interleaved_ieee_float (pmpeg->lamef, ptr + total, writecount / 2, pmpeg->block, pmpeg->block_len) ; + else + { /* Lame lacks a non-normalized interleaved float write. Bummer. */ + normalize_float (ubuf.fbuf, ptr + total, writecount, 1.0 / (float) 0x8000) ; + nbytes = lame_encode_buffer_interleaved_ieee_float (pmpeg->lamef, ubuf.fbuf, writecount / 2, pmpeg->block, pmpeg->block_len) ; + } + + if (nbytes < 0) + { psf_log_printf (psf, "lame_encode_buffer returned %d\n", nbytes) ; + break ; + } ; + + if (nbytes) + { writen = psf_fwrite (pmpeg->block, 1, nbytes, psf) ; + if (writen != nbytes) + { psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", writen, nbytes) ; + } ; + } ; + + total += writecount ; + len -= writecount ; + } ; + + return total ; +} + + +static inline void +normalize_double (double *dest, const double *src, sf_count_t count, double norm_fact) +{ while (--count >= 0) + { dest [count] = src [count] * norm_fact ; + } ; +} + + +static sf_count_t +mpeg_l3_encode_write_double_mono (SF_PRIVATE *psf, const double *ptr, sf_count_t len) +{ BUF_UNION ubuf ; + MPEG_L3_ENC_PRIVATE *pmpeg = (MPEG_L3_ENC_PRIVATE*) psf->codec_data ; + sf_count_t total = 0 ; + int nbytes, writecount, writen ; + const sf_count_t max_samples = SF_MIN (ARRAY_LEN (ubuf.dbuf), pmpeg->frame_samples) ; + + if ((psf->error = mpeg_l3_encoder_construct (psf))) + return 0 ; + + while (len) + { writecount = SF_MIN (len, max_samples) ; + + if (psf->norm_double) + nbytes = lame_encode_buffer_ieee_double (pmpeg->lamef, ptr + total, NULL, writecount, pmpeg->block, pmpeg->block_len) ; + else + { /* Lame lacks non-normalized double writing */ + normalize_double (ubuf.dbuf, ptr + total, writecount, 1.0 / (double) 0x8000) ; + nbytes = lame_encode_buffer_ieee_double (pmpeg->lamef, ubuf.dbuf, NULL, writecount, pmpeg->block, pmpeg->block_len) ; + } + + if (nbytes < 0) + { psf_log_printf (psf, "lame_encode_buffer returned %d\n", nbytes) ; + break ; + } ; + + if (nbytes) + { writen = psf_fwrite (pmpeg->block, 1, nbytes, psf) ; + if (writen != nbytes) + { psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", writen, nbytes) ; + } ; + } ; + + total += writecount ; + len -= writecount ; + } ; + + return total ; +} + + +static sf_count_t +mpeg_l3_encode_write_double_stereo (SF_PRIVATE *psf, const double *ptr, sf_count_t len) +{ BUF_UNION ubuf ; + MPEG_L3_ENC_PRIVATE *pmpeg = (MPEG_L3_ENC_PRIVATE*) psf->codec_data ; + sf_count_t total = 0 ; + int nbytes, writecount, writen ; + const sf_count_t max_samples = SF_MIN (ARRAY_LEN (ubuf.dbuf), pmpeg->frame_samples) ; + + if ((psf->error = mpeg_l3_encoder_construct (psf))) + return 0 ; + + while (len) + { writecount = SF_MIN (len, max_samples) ; + + if (psf->norm_double) + nbytes = lame_encode_buffer_interleaved_ieee_double (pmpeg->lamef, ptr + total, writecount / 2, pmpeg->block, pmpeg->block_len) ; + else + { /* Lame lacks interleaved non-normalized double writing */ + normalize_double (ubuf.dbuf, ptr + total, writecount, 1.0 / (double) 0x8000) ; + nbytes = lame_encode_buffer_interleaved_ieee_double (pmpeg->lamef, ubuf.dbuf, writecount / 2, pmpeg->block, pmpeg->block_len) ; + } + + if (nbytes < 0) + { psf_log_printf (psf, "lame_encode_buffer returned %d\n", nbytes) ; + break ; + } ; + + if (nbytes) + { writen = psf_fwrite (pmpeg->block, 1, nbytes, psf) ; + if (writen != nbytes) + { psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", writen, nbytes) ; + } ; + } ; + + total += writecount ; + len -= writecount ; + } ; + + return total ; +} + +#else /* HAVE_MPEG */ + +int +mpeg_l3_encoder_init (SF_PRIVATE *psf, int UNUSED (vbr)) +{ psf_log_printf (psf, "This version of libsndfile was compiled without MPEG Layer 3 encoding support.\n") ; + return SFE_UNIMPLEMENTED ; +} /* mpeg_l3_encoder_init */ + +#endif diff --git a/src/sndfile.c b/src/sndfile.c index c7145c7f..3ba8addb 100644 --- a/src/sndfile.c +++ b/src/sndfile.c @@ -282,7 +282,9 @@ ErrorStruct SndfileErrors [] = { SFE_FILENAME_TOO_LONG , "Error : Supplied filename too long." }, { SFE_NEGATIVE_RW_LEN , "Error : Length parameter passed to read/write is negative." }, - { SFE_OPUS_BAD_SAMPLERATE , "Error : Opus only supports sample rates of 8000, 12000, 16000, 24000 and 48000." }, + { SFE_OPUS_BAD_SAMPLERATE , "Error : Opus only supports sample rates of 8000, 12000, 16000, 24000, and 48000." }, + + { SFE_MPEG_BAD_SAMPLERATE , "Error : MPEG-1/2/2.5 only supports sample rates of 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, and 48000." }, { SFE_CAF_NOT_CAF , "Error : Not a CAF file." }, { SFE_CAF_NO_DESC , "Error : No 'desc' marker in CAF file." }, @@ -959,6 +961,16 @@ sf_format_check (const SF_INFO *info) if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE) return 1 ; break ; + + case SF_FORMAT_MP3 : + if (info->channels > 2) + return 0 ; + if (endian != SF_ENDIAN_FILE) + return 0 ; + /* TODO */ + if (subformat == SF_FORMAT_MPEG_LAYER_I || subformat == SF_FORMAT_MPEG_LAYER_II || subformat == SF_FORMAT_MPEG_LAYER_III) + return 1 ; + break ; default : break ; } ; @@ -2743,6 +2755,13 @@ format_from_extension (SF_PRIVATE *psf) psf->sf.samplerate = 8000 ; format = SF_FORMAT_RAW | SF_FORMAT_GSM610 ; } + else if (strcmp (cptr, "mp3") == 0) + { /* + * MPEG streams are quite tollerate of crap. If we couldn't identify a + * MP3 stream, but it has a .mp3 extension, let libmpg123 have a try. + */ + format = SF_FORMAT_MP3 ; + } /* For RAW files, make sure the dataoffset if set correctly. */ if ((SF_CONTAINER (format)) == SF_FORMAT_RAW) @@ -2852,9 +2871,26 @@ retry: if (buffer [0] == MAKE_MARKER ('R', 'F', '6', '4') && buffer [2] == MAKE_MARKER ('W', 'A', 'V', 'E')) return SF_FORMAT_RF64 ; + if ((buffer [0] & MAKE_MARKER (0xFF, 0xE0, 0, 0)) == MAKE_MARKER (0xFF, 0xE0, 0, 0) && /* Frame sync */ + (buffer [0] & MAKE_MARKER (0, 0x18, 0, 0)) != MAKE_MARKER (0, 0x08, 0, 0) && /* Valid MPEG version */ + (buffer [0] & MAKE_MARKER (0, 0x06, 0, 0)) != MAKE_MARKER (0, 0, 0, 0) && /* Valid layer description */ + (buffer [0] & MAKE_MARKER (0, 0, 0xF0, 0)) != MAKE_MARKER (0, 0, 0xF0, 0) && /* Valid bitrate */ + (buffer [0] & MAKE_MARKER (0, 0, 0x0C, 0)) != MAKE_MARKER (0, 0, 0x0C, 0)) /* Valid samplerate */ + return SF_FORMAT_MP3 ; + if (buffer [0] == MAKE_MARKER ('I', 'D', '3', 2) || buffer [0] == MAKE_MARKER ('I', 'D', '3', 3) || buffer [0] == MAKE_MARKER ('I', 'D', '3', 4)) { psf_log_printf (psf, "Found 'ID3' marker.\n") ; + + /* Guess MP3, try and open it as such. Allows libmpg123 to parse the ID3v2 headers */ + if (psf->file.mode == SFM_READ) + { if (mp3_open (psf) == 0) + return SF_FORMAT_MP3 | ((~SF_FORMAT_TYPEMASK) & psf->sf.format) ; + else if (psf->codec_close) + psf->codec_close (psf) ; + } ; + + /* Otherwise, seek to after the ID3 header */ if (id3_skip (psf)) goto retry ; return 0 ; @@ -3233,6 +3269,10 @@ psf_open_file (SF_PRIVATE *psf, SF_INFO *sfinfo) error = mpc2k_open (psf) ; break ; + case SF_FORMAT_MP3 : + error = mp3_open (psf) ; + break ; + /* Lite remove end */ default : @@ -3253,6 +3293,7 @@ psf_open_file (SF_PRIVATE *psf, SF_INFO *sfinfo) /* Actual embedded files. */ break ; + case SF_FORMAT_MP3 : case SF_FORMAT_FLAC : /* Flac with an ID3v2 header? */ break ; diff --git a/src/test_log_printf.c b/src/test_log_printf.c index e1806a14..ada62377 100644 --- a/src/test_log_printf.c +++ b/src/test_log_printf.c @@ -113,6 +113,8 @@ test_log_printf (void) /* Test printing of strings. */ CMP_4_ARGS (__LINE__, errors, "B %s, %3s, %8s, %-8s", "str") ; + CMP_4_ARGS (__LINE__, errors, "B %.2s, %.8s, %-8.8s, %-4.2s", "str") ; + if (errors) { puts ("\nExiting due to errors.\n") ; exit (1) ; diff --git a/tests/compression_size_test.c b/tests/compression_size_test.c index aceceed6..8f760af5 100644 --- a/tests/compression_size_test.c +++ b/tests/compression_size_test.c @@ -184,33 +184,50 @@ main (int argc, char *argv []) " vorbis - test Ogg/Vorbis\n" " flac - test FLAC\n" " opus - test Opus\n" + " mpeg - test mpeg\n" " all - perform all tests\n", argv [0]) ; exit (0) ; } ; - if (! HAVE_EXTERNAL_XIPH_LIBS) - { puts (" No Ogg/Vorbis tests because Ogg/Vorbis support was not compiled in.") ; - return 0 ; - } ; - if (strcmp (argv [1], "all") == 0) all_tests = 1 ; if (all_tests || strcmp (argv [1], "vorbis") == 0) - { vorbis_test () ; - compression_size_test (SF_FORMAT_OGG | SF_FORMAT_VORBIS, "vorbis.oga") ; - tests ++ ; + { if (HAVE_EXTERNAL_XIPH_LIBS) + { vorbis_test () ; + compression_size_test (SF_FORMAT_OGG | SF_FORMAT_VORBIS, "vorbis.oga") ; + tests ++ ; + } + else + puts (" No Ogg Vorbis tests because support was not compiled in.") ; } ; if (all_tests || strcmp (argv [1], "flac") == 0) - { compression_size_test (SF_FORMAT_FLAC | SF_FORMAT_PCM_16, "pcm16.flac") ; - tests ++ ; + { if (HAVE_EXTERNAL_XIPH_LIBS) + { compression_size_test (SF_FORMAT_FLAC | SF_FORMAT_PCM_16, "pcm16.flac") ; + tests ++ ; + } + else + puts (" No FLAC tests because support was not compiled in.") ; } ; if (all_tests || strcmp (argv [1], "opus") == 0) - { compression_size_test (SF_FORMAT_OGG | SF_FORMAT_OPUS, "opus.opus") ; - tests ++ ; + { if (HAVE_EXTERNAL_XIPH_LIBS) + { compression_size_test (SF_FORMAT_OGG | SF_FORMAT_OPUS, "opus.opus") ; + tests ++ ; + } + else + puts (" No Opus tests because support was not compiled in.") ; + } ; + + if (all_tests || strcmp (argv [1], "mpeg") == 0) + { if (HAVE_MPEG) + { compression_size_test (SF_FORMAT_MP3 | SF_FORMAT_MPEG_LAYER_III, "mpeg.mp3") ; + tests ++ ; + } + else + puts (" No MPEG tests because support was not compiled in.") ; } ; return 0 ; diff --git a/tests/floating_point_test.tpl b/tests/floating_point_test.tpl index 4eb0ce6e..0b11c1e3 100644 --- a/tests/floating_point_test.tpl +++ b/tests/floating_point_test.tpl @@ -127,6 +127,10 @@ main (int argc, char *argv []) float_scaled_test ("opus.opus", allow_exit, SF_FALSE, SF_FORMAT_OGG | SF_FORMAT_OPUS, -32.0) ; #endif +#if HAVE_MPEG + float_scaled_test ("mpeg.mp3", allow_exit, SF_FALSE, SF_FORMAT_MP3 | SF_FORMAT_MPEG_LAYER_III, -52.0) ; +#endif + float_scaled_test ("replace_float.raw", allow_exit, SF_TRUE, SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_FLOAT, -163.0) ; /*============================================================================== @@ -185,6 +189,10 @@ main (int argc, char *argv []) double_scaled_test ("opus.opus", allow_exit, SF_FALSE, SF_FORMAT_OGG | SF_FORMAT_OPUS, -32.0) ; #endif +#if HAVE_MPEG + double_scaled_test ("mpeg.mp3", allow_exit, SF_FALSE, SF_FORMAT_MP3 | SF_FORMAT_MPEG_LAYER_III, -52.0) ; +#endif + double_scaled_test ("replace_double.raw", allow_exit, SF_TRUE, SF_FORMAT_RAW | SF_FORMAT_DOUBLE, -201.0) ; putchar ('\n') ; diff --git a/tests/mp3_test.c b/tests/mp3_test.c new file mode 100644 index 00000000..76c64629 --- /dev/null +++ b/tests/mp3_test.c @@ -0,0 +1,348 @@ +/* +** Copyright (C) 2007-2019 Erik de Castro Lopo +** Copyright (C) 2019 John ffitch +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation; either version 2 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include "sfconfig.h" + +#include +#include +#include +#if HAVE_UNISTD_H +#include +#else +#include "sf_unistd.h" +#endif + +#include + +#include + +#include "utils.h" + +#define SAMPLE_RATE 44100 +#define DATA_LENGTH (SAMPLE_RATE / 8) + +typedef union +{ double d [DATA_LENGTH] ; + float f [DATA_LENGTH] ; + int i [DATA_LENGTH] ; + short s [DATA_LENGTH] ; +} BUFFER ; + +static BUFFER data_out ; +static BUFFER data_in ; + +static void +mp3_short_test (void) +{ const char * filename = "mpeg_short.mp3" ; + + SNDFILE * file ; + SF_INFO sfinfo ; + short seek_data [10] ; + unsigned k ; + + print_test_name ("mpeg_short_test", filename) ; + + /* Generate float data. */ + gen_windowed_sine_float (data_out.f, ARRAY_LEN (data_out.f), 1.0 * 0x7F00) ; + + /* Convert to shorteger. */ + for (k = 0 ; k < ARRAY_LEN (data_out.s) ; k++) + data_out.s [k] = lrintf (data_out.f [k]) ; + + memset (&sfinfo, 0, sizeof (sfinfo)) ; + + /* Set up output file type. */ + sfinfo.format = SF_FORMAT_MP3 | SF_FORMAT_MPEG_LAYER_III ; + sfinfo.channels = 1 ; + sfinfo.samplerate = SAMPLE_RATE ; + + /* Write the output file. */ + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ; + test_write_short_or_die (file, 0, data_out.s, ARRAY_LEN (data_out.s), __LINE__) ; + sf_close (file) ; + + /* Read the file in again. */ + memset (&sfinfo, 0, sizeof (sfinfo)) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; + test_read_short_or_die (file, 0, data_in.s, ARRAY_LEN (data_in.s), __LINE__) ; + sf_close (file) ; + + puts ("ok") ; + + /* Test seeking. */ + print_test_name ("mpeg_seek_test", filename) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; + + test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ; + test_read_short_or_die (file, 0, seek_data, ARRAY_LEN (seek_data), __LINE__) ; + compare_short_or_die (seek_data, data_in.s + 10, ARRAY_LEN (seek_data), __LINE__) ; + + /* Test seek to end of file. */ + test_seek_or_die (file, 0, SEEK_END, sfinfo.frames, sfinfo.channels, __LINE__) ; + + sf_close (file) ; + + puts ("ok") ; + + unlink (filename) ; +} /* mpeg_short_test */ + +static void +mp3_int_test (void) +{ const char * filename = "mpeg_int.mp3" ; + + SNDFILE * file ; + SF_INFO sfinfo ; + int seek_data [10] ; + unsigned k ; + + print_test_name ("mpeg_int_test", filename) ; + + /* Generate float data. */ + gen_windowed_sine_float (data_out.f, ARRAY_LEN (data_out.f), 1.0 * 0x7FFF0000) ; + + /* Convert to integer. */ + for (k = 0 ; k < ARRAY_LEN (data_out.i) ; k++) + data_out.i [k] = lrintf (data_out.f [k]) ; + + memset (&sfinfo, 0, sizeof (sfinfo)) ; + + /* Set up output file type. */ + sfinfo.format = SF_FORMAT_MP3 | SF_FORMAT_MPEG_LAYER_III ; + sfinfo.channels = 1 ; + sfinfo.samplerate = SAMPLE_RATE ; + + /* Write the output file. */ + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ; + test_write_int_or_die (file, 0, data_out.i, ARRAY_LEN (data_out.i), __LINE__) ; + sf_close (file) ; + + /* Read the file in again. */ + memset (&sfinfo, 0, sizeof (sfinfo)) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; + test_read_int_or_die (file, 0, data_in.i, ARRAY_LEN (data_in.i), __LINE__) ; + sf_close (file) ; + + puts ("ok") ; + + /* Test seeking. */ + print_test_name ("mpeg_seek_test", filename) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; + + test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ; + test_read_int_or_die (file, 0, seek_data, ARRAY_LEN (seek_data), __LINE__) ; + compare_int_or_die (seek_data, data_in.i + 10, ARRAY_LEN (seek_data), __LINE__) ; + + sf_close (file) ; + + puts ("ok") ; + + unlink (filename) ; +} /* mpeg_int_test */ + +static void +mp3_float_test (void) +{ const char * filename = "mpeg_float.mp3" ; + + SNDFILE * file ; + SF_INFO sfinfo ; + float seek_data [10] ; + + print_test_name ("mpeg_float_test", filename) ; + + gen_windowed_sine_float (data_out.f, ARRAY_LEN (data_out.f), 0.95) ; + + memset (&sfinfo, 0, sizeof (sfinfo)) ; + + /* Set up output file type. */ + sfinfo.format = SF_FORMAT_MP3 | SF_FORMAT_MPEG_LAYER_III ; + sfinfo.channels = 1 ; + sfinfo.samplerate = SAMPLE_RATE ; + + /* Write the output file. */ + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ; + test_write_float_or_die (file, 0, data_out.f, ARRAY_LEN (data_out.f), __LINE__) ; + sf_close (file) ; + + /* Read the file in again. */ + memset (&sfinfo, 0, sizeof (sfinfo)) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; + test_read_float_or_die (file, 0, data_in.f, ARRAY_LEN (data_in.f), __LINE__) ; + sf_close (file) ; + + puts ("ok") ; + + /* Test seeking. */ + print_test_name ("mpeg_seek_test", filename) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; + + test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ; + test_read_float_or_die (file, 0, seek_data, ARRAY_LEN (seek_data), __LINE__) ; + compare_float_or_die (seek_data, data_in.f + 10, ARRAY_LEN (seek_data), __LINE__) ; + + sf_close (file) ; + + puts ("ok") ; + + unlink (filename) ; +} /* mpeg_float_test */ + +static void +mp3_double_test (void) +{ const char * filename = "mpeg_double.mp3" ; + + SNDFILE * file ; + SF_INFO sfinfo ; + double seek_data [10] ; + + print_test_name ("mpeg_double_test", filename) ; + + gen_windowed_sine_double (data_out.d, ARRAY_LEN (data_out.d), 0.95) ; + + memset (&sfinfo, 0, sizeof (sfinfo)) ; + + /* Set up output file type. */ + sfinfo.format = SF_FORMAT_MP3 | SF_FORMAT_MPEG_LAYER_III ; + sfinfo.channels = 1 ; + sfinfo.samplerate = SAMPLE_RATE ; + + /* Write the output file. */ + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ; + test_write_double_or_die (file, 0, data_out.d, ARRAY_LEN (data_out.d), __LINE__) ; + sf_close (file) ; + + /* Read the file in again. */ + memset (&sfinfo, 0, sizeof (sfinfo)) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; + test_read_double_or_die (file, 0, data_in.d, ARRAY_LEN (data_in.d), __LINE__) ; + sf_close (file) ; + + puts ("ok") ; + + /* Test seeking. */ + print_test_name ("mpeg_seek_test", filename) ; + + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; + + test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ; + test_read_double_or_die (file, 0, seek_data, ARRAY_LEN (seek_data), __LINE__) ; + compare_double_or_die (seek_data, data_in.d + 10, ARRAY_LEN (seek_data), __LINE__) ; + + sf_close (file) ; + + puts ("ok") ; + + unlink (filename) ; +} /* mpeg_double_test */ + + +static void +mp3_stereo_seek_test (const char * filename, int format) +{ static float data [SAMPLE_RATE] ; + static float stereo_out [SAMPLE_RATE * 2] ; + + SNDFILE * file ; + SF_INFO sfinfo ; + sf_count_t pos ; + unsigned k ; + + print_test_name (__func__, filename) ; + + gen_windowed_sine_float (data, ARRAY_LEN (data), 0.95) ; + for (k = 0 ; k < ARRAY_LEN (data) ; k++) + { stereo_out [2 * k] = data [k] ; + stereo_out [2 * k + 1] = data [ARRAY_LEN (data) - k - 1] ; + } ; + + memset (&sfinfo, 0, sizeof (sfinfo)) ; + + /* Set up output file type. */ + sfinfo.format = format ; + sfinfo.channels = 2 ; + sfinfo.samplerate = SAMPLE_RATE ; + + /* Write the output file. */ + file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ; + test_write_float_or_die (file, 0, stereo_out, ARRAY_LEN (stereo_out), __LINE__) ; + sf_close (file) ; + + /* Open file in again for reading. */ + memset (&sfinfo, 0, sizeof (sfinfo)) ; + file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; + + /* Read in the whole file. */ + test_read_float_or_die (file, 0, stereo_out, ARRAY_LEN (stereo_out), __LINE__) ; + + /* Now hammer seeking code. */ + test_seek_or_die (file, 234, SEEK_SET, 234, sfinfo.channels, __LINE__) ; + test_readf_float_or_die (file, 0, data, 10, __LINE__) ; + compare_float_or_die (data, stereo_out + (234 * sfinfo.channels), 10, __LINE__) ; + + test_seek_or_die (file, 442, SEEK_SET, 442, sfinfo.channels, __LINE__) ; + test_readf_float_or_die (file, 0, data, 10, __LINE__) ; + compare_float_or_die (data, stereo_out + (442 * sfinfo.channels), 10, __LINE__) ; + + test_seek_or_die (file, 12, SEEK_CUR, 442 + 10 + 12, sfinfo.channels, __LINE__) ; + test_readf_float_or_die (file, 0, data, 10, __LINE__) ; + compare_float_or_die (data, stereo_out + ((442 + 10 + 12) * sfinfo.channels), 10, __LINE__) ; + + test_seek_or_die (file, 12, SEEK_CUR, 442 + 20 + 24, sfinfo.channels, __LINE__) ; + test_readf_float_or_die (file, 0, data, 10, __LINE__) ; + compare_float_or_die (data, stereo_out + ((442 + 20 + 24) * sfinfo.channels), 10, __LINE__) ; + + pos = 500 - sfinfo.frames ; + test_seek_or_die (file, pos, SEEK_END, 500, sfinfo.channels, __LINE__) ; + test_readf_float_or_die (file, 0, data, 10, __LINE__) ; + compare_float_or_die (data, stereo_out + (500 * sfinfo.channels), 10, __LINE__) ; + + pos = 10 - sfinfo.frames ; + test_seek_or_die (file, pos, SEEK_END, 10, sfinfo.channels, __LINE__) ; + test_readf_float_or_die (file, 0, data, 10, __LINE__) ; + compare_float_or_die (data, stereo_out + (10 * sfinfo.channels), 10, __LINE__) ; + + sf_close (file) ; + + puts ("ok") ; + unlink (filename) ; +} /* mpeg_stereo_seek_test */ + + +int +main (void) +{ + if (HAVE_MPEG) + { mp3_short_test () ; + mp3_int_test () ; + mp3_float_test () ; + mp3_double_test () ; + + mp3_stereo_seek_test ("mpeg_seek.mp3", SF_FORMAT_MP3 | SF_FORMAT_MPEG_LAYER_III) ; + } + else + puts (" No MPEG tests because mpg123/lame support was not compiled in.") ; + return 0 ; +} /* main */ + diff --git a/tests/string_test.c b/tests/string_test.c index 0e7a01e8..dd9bb278 100644 --- a/tests/string_test.c +++ b/tests/string_test.c @@ -115,6 +115,14 @@ main (int argc, char *argv []) test_count++ ; } ; + if (do_all || ! strcmp (argv [1], "mpeg")) + { if (HAVE_MPEG) + string_start_test ("mpeg.mp3", SF_FORMAT_MP3 | SF_FORMAT_MPEG_LAYER_III) ; + else + puts (" No MP3 tests because MPEG support was not compiled in.") ; + test_count++ ; + } ; + if (do_all || ! strcmp (argv [1], "ogg")) { if (HAVE_EXTERNAL_XIPH_LIBS) string_start_test ("vorbis.oga", SF_FORMAT_OGG | SF_FORMAT_VORBIS) ; @@ -197,7 +205,9 @@ static const char long_title [] = "This is a very long and very boring title for this file", long_artist [] = "The artist who kept on changing its name", genre [] = "The genre", - trackno [] = "Track three" ; + trackno [] = "Track three", + id3v1_genre [] = "Rock", + year [] = "2001" ; static short data_out [BUFFER_LEN] ; @@ -374,6 +384,7 @@ string_start_test (const char *filename, int formattype) break ; case SF_FORMAT_OGG | SF_FORMAT_VORBIS : + case SF_FORMAT_MP3 | SF_FORMAT_MPEG_LAYER_III : break ; default : @@ -390,9 +401,15 @@ string_start_test (const char *filename, int formattype) sf_set_string (file, SF_STR_ARTIST, artist) ; sf_set_string (file, SF_STR_COPYRIGHT, copyright) ; sf_set_string (file, SF_STR_COMMENT, comment) ; - sf_set_string (file, SF_STR_DATE, date) ; sf_set_string (file, SF_STR_ALBUM, album) ; sf_set_string (file, SF_STR_LICENSE, license) ; + if (typemajor == SF_FORMAT_MP3) + { sf_set_string (file, SF_STR_GENRE, id3v1_genre) ; + sf_set_string (file, SF_STR_DATE, year) ; + } + else + { sf_set_string (file, SF_STR_DATE, date) ; + } ; /* Write data to file. */ test_write_short_or_die (file, 0, data_out, BUFFER_LEN, __LINE__) ; @@ -415,24 +432,35 @@ string_start_test (const char *filename, int formattype) printf (" Bad filename : %s\n", cptr) ; } ; - cptr = sf_get_string (file, SF_STR_COPYRIGHT) ; - if (cptr == NULL || strcmp (copyright, cptr) != 0) - { if (errors++ == 0) - puts ("\n") ; - printf (" Bad copyright : %s\n", cptr) ; + if (typemajor != SF_FORMAT_MP3) + { cptr = sf_get_string (file, SF_STR_COPYRIGHT) ; + if (cptr == NULL || strcmp (copyright, cptr) != 0) + { if (errors++ == 0) + puts ("\n") ; + printf (" Bad copyright : %s\n", cptr) ; + } ; + + cptr = sf_get_string (file, SF_STR_SOFTWARE) ; + if (cptr == NULL || strstr (cptr, software) != cptr) + { if (errors++ == 0) + puts ("\n") ; + printf (" Bad software : %s\n", cptr) ; + } ; + + if (cptr && str_count (cptr, "libsndfile") != 1) + { if (errors++ == 0) + puts ("\n") ; + printf (" Bad software : %s\n", cptr) ; + } ; } ; - cptr = sf_get_string (file, SF_STR_SOFTWARE) ; - if (cptr == NULL || strstr (cptr, software) != cptr) - { if (errors++ == 0) - puts ("\n") ; - printf (" Bad software : %s\n", cptr) ; - } ; - - if (cptr && str_count (cptr, "libsndfile") != 1) - { if (errors++ == 0) - puts ("\n") ; - printf (" Bad software : %s\n", cptr) ; + if (typemajor == SF_FORMAT_MP3) + { cptr = sf_get_string (file, SF_STR_GENRE) ; + if (cptr == NULL || strcmp (id3v1_genre, cptr) != 0) + { if (errors++ == 0) + puts ("\n") ; + printf (" Bad genre : %s\n", cptr) ; + } ; } ; cptr = sf_get_string (file, SF_STR_ARTIST) ; @@ -449,13 +477,29 @@ string_start_test (const char *filename, int formattype) printf (" Bad comment : %s\n", cptr) ; } ; - if (typemajor != SF_FORMAT_AIFF) - { cptr = sf_get_string (file, SF_STR_DATE) ; - if (cptr == NULL || strcmp (date, cptr) != 0) - { if (errors++ == 0) - puts ("\n") ; - printf (" Bad date : %s\n", cptr) ; - } ; + switch (typemajor) + { case SF_FORMAT_AIFF : + /* not supported */ + break ; + + case SF_FORMAT_MP3 : + /* id3 only supports years */ + cptr = sf_get_string (file, SF_STR_DATE) ; + if (cptr == NULL || strcmp (year, cptr) != 0) + { if (errors++ == 0) + puts ("\n") ; + printf (" Bad date : %s\n", cptr) ; + } ; + break ; + + default : + cptr = sf_get_string (file, SF_STR_DATE) ; + if (cptr == NULL || strcmp (date, cptr) != 0) + { if (errors++ == 0) + puts ("\n") ; + printf (" Bad date : %s\n", cptr) ; + } ; + break ; } ; if (typemajor != SF_FORMAT_WAV && typemajor != SF_FORMAT_AIFF) @@ -467,13 +511,21 @@ string_start_test (const char *filename, int formattype) } ; } ; - if (typemajor != SF_FORMAT_WAV && typemajor != SF_FORMAT_AIFF && typemajor != SF_FORMAT_RF64) - { cptr = sf_get_string (file, SF_STR_LICENSE) ; - if (cptr == NULL || strcmp (license, cptr) != 0) - { if (errors++ == 0) - puts ("\n") ; - printf (" Bad license : %s\n", cptr) ; - } ; + switch (typemajor) + { case SF_FORMAT_WAV : + case SF_FORMAT_AIFF : + case SF_FORMAT_RF64 : + case SF_FORMAT_MP3 : + /* not supported */ + break ; + + default: + cptr = sf_get_string (file, SF_STR_LICENSE) ; + if (cptr == NULL || strcmp (license, cptr) != 0) + { if (errors++ == 0) + puts ("\n") ; + printf (" Bad license : %s\n", cptr) ; + } ; } ; if (errors > 0) diff --git a/tests/test_wrapper.sh.in b/tests/test_wrapper.sh.in index b06a1788..b74b7664 100644 --- a/tests/test_wrapper.sh.in +++ b/tests/test_wrapper.sh.in @@ -352,6 +352,15 @@ echo "----------------------------------------------------------------------" echo " $sfversion passed tests on OPUS files." echo "----------------------------------------------------------------------" +# mpeg-tests +./tests/mp3_test@EXEEXT@ +./tests/compression_size_test@EXEEXT@ mpeg +./tests/string_test@EXEEXT@ mpeg + +echo "----------------------------------------------------------------------" +echo " $sfversion passed tests on MPEG files." +echo "----------------------------------------------------------------------" + # io-tests ./tests/stdio_test@EXEEXT@ ./tests/pipe_test@EXEEXT@ From d2c836d7321d06c768c11efcee859c764d289743 Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Sun, 29 Nov 2020 19:26:20 -0800 Subject: [PATCH 075/204] Workaround vcpkg yasm issues. See https://github.com/microsoft/vcpkg/pull/14003 --- .github/workflows/action.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index b0fe7bd3..fee3bb07 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -189,6 +189,10 @@ jobs: if: startsWith(matrix.os,'ubuntu') run: sudo apt-get install -y autogen ninja-build libogg-dev libvorbis-dev libflac-dev libopus-dev libasound2-dev libsqlite3-dev libspeex-dev libmp3lame-dev libmpg123-dev + - name: Workaround vcpkg yasm-tools for x64-windows + if: (startsWith(matrix.os, 'windows') && startsWith(matrix.triplet, 'x64-windows')) + run: vcpkg install yasm-tool:x86-windows + - name: Install Windows dependencies if: startsWith(matrix.os,'windows') run: | From dae19b950286226f0433e82c2097917ad0bfd5ba Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Mon, 11 Jan 2021 21:18:23 -0800 Subject: [PATCH 076/204] Rename SF_FORMAT_MP3 to SF_FORMAT_MPEG --- include/sndfile.h.in | 2 +- src/common.c | 2 +- src/mp3.c | 4 ++-- src/sndfile.c | 12 ++++++------ tests/compression_size_test.c | 2 +- tests/floating_point_test.tpl | 4 ++-- tests/mp3_test.c | 10 +++++----- tests/string_test.c | 14 +++++++------- 8 files changed, 25 insertions(+), 25 deletions(-) diff --git a/include/sndfile.h.in b/include/sndfile.h.in index ac65e3d9..2f07c2e7 100644 --- a/include/sndfile.h.in +++ b/include/sndfile.h.in @@ -71,7 +71,7 @@ enum SF_FORMAT_OGG = 0x200000, /* Xiph OGG container */ SF_FORMAT_MPC2K = 0x210000, /* Akai MPC 2000 sampler */ SF_FORMAT_RF64 = 0x220000, /* RF64 WAV file */ - SF_FORMAT_MP3 = 0x230000, /* So-called MP3 file. */ + SF_FORMAT_MPEG = 0x230000, /* MPEG-1/2 audio stream */ /* Subtypes from here on. */ diff --git a/src/common.c b/src/common.c index a087c714..a198c28d 100644 --- a/src/common.c +++ b/src/common.c @@ -1572,7 +1572,7 @@ str_of_major_format (int format) CASE_NAME (SF_FORMAT_CAF) ; CASE_NAME (SF_FORMAT_WVE) ; CASE_NAME (SF_FORMAT_OGG) ; - CASE_NAME (SF_FORMAT_MP3) ; + CASE_NAME (SF_FORMAT_MPEG) ; default : break ; } ; diff --git a/src/mp3.c b/src/mp3.c index 6f63b252..8e400f3a 100644 --- a/src/mp3.c +++ b/src/mp3.c @@ -41,8 +41,8 @@ ** This file is concerned only with the most common case of MPEG Layer III ** audio without a container but with the additional metadata standards. ** -** For the purposes of libsndfile, the major format of SF_FORMAT_MP3 means the -** following assumptions. A file of major format type SF_FORMAT_MP3: +** For the purposes of libsndfile, the major format of SF_FORMAT_MPEG means the +** following assumptions. A file of major format type SF_FORMAT_MPEG: ** - Contains only layer III audio frames (SF_FORMAT_MPEG_LAYER_III) ** - All MPEG frames contained in the file have the same channel count ** - All MPEG frames contained in the file have the same samplerate diff --git a/src/sndfile.c b/src/sndfile.c index 3ba8addb..a07b4462 100644 --- a/src/sndfile.c +++ b/src/sndfile.c @@ -962,7 +962,7 @@ sf_format_check (const SF_INFO *info) return 1 ; break ; - case SF_FORMAT_MP3 : + case SF_FORMAT_MPEG : if (info->channels > 2) return 0 ; if (endian != SF_ENDIAN_FILE) @@ -2760,7 +2760,7 @@ format_from_extension (SF_PRIVATE *psf) * MPEG streams are quite tollerate of crap. If we couldn't identify a * MP3 stream, but it has a .mp3 extension, let libmpg123 have a try. */ - format = SF_FORMAT_MP3 ; + format = SF_FORMAT_MPEG ; } /* For RAW files, make sure the dataoffset if set correctly. */ @@ -2876,7 +2876,7 @@ retry: (buffer [0] & MAKE_MARKER (0, 0x06, 0, 0)) != MAKE_MARKER (0, 0, 0, 0) && /* Valid layer description */ (buffer [0] & MAKE_MARKER (0, 0, 0xF0, 0)) != MAKE_MARKER (0, 0, 0xF0, 0) && /* Valid bitrate */ (buffer [0] & MAKE_MARKER (0, 0, 0x0C, 0)) != MAKE_MARKER (0, 0, 0x0C, 0)) /* Valid samplerate */ - return SF_FORMAT_MP3 ; + return SF_FORMAT_MPEG ; if (buffer [0] == MAKE_MARKER ('I', 'D', '3', 2) || buffer [0] == MAKE_MARKER ('I', 'D', '3', 3) || buffer [0] == MAKE_MARKER ('I', 'D', '3', 4)) @@ -2885,7 +2885,7 @@ retry: /* Guess MP3, try and open it as such. Allows libmpg123 to parse the ID3v2 headers */ if (psf->file.mode == SFM_READ) { if (mp3_open (psf) == 0) - return SF_FORMAT_MP3 | ((~SF_FORMAT_TYPEMASK) & psf->sf.format) ; + return SF_FORMAT_MPEG | ((~SF_FORMAT_TYPEMASK) & psf->sf.format) ; else if (psf->codec_close) psf->codec_close (psf) ; } ; @@ -3269,7 +3269,7 @@ psf_open_file (SF_PRIVATE *psf, SF_INFO *sfinfo) error = mpc2k_open (psf) ; break ; - case SF_FORMAT_MP3 : + case SF_FORMAT_MPEG : error = mp3_open (psf) ; break ; @@ -3293,7 +3293,7 @@ psf_open_file (SF_PRIVATE *psf, SF_INFO *sfinfo) /* Actual embedded files. */ break ; - case SF_FORMAT_MP3 : + case SF_FORMAT_MPEG : case SF_FORMAT_FLAC : /* Flac with an ID3v2 header? */ break ; diff --git a/tests/compression_size_test.c b/tests/compression_size_test.c index 8f760af5..b7dae300 100644 --- a/tests/compression_size_test.c +++ b/tests/compression_size_test.c @@ -223,7 +223,7 @@ main (int argc, char *argv []) if (all_tests || strcmp (argv [1], "mpeg") == 0) { if (HAVE_MPEG) - { compression_size_test (SF_FORMAT_MP3 | SF_FORMAT_MPEG_LAYER_III, "mpeg.mp3") ; + { compression_size_test (SF_FORMAT_MPEG | SF_FORMAT_MPEG_LAYER_III, "mpeg.mp3") ; tests ++ ; } else diff --git a/tests/floating_point_test.tpl b/tests/floating_point_test.tpl index 0b11c1e3..9651391c 100644 --- a/tests/floating_point_test.tpl +++ b/tests/floating_point_test.tpl @@ -128,7 +128,7 @@ main (int argc, char *argv []) #endif #if HAVE_MPEG - float_scaled_test ("mpeg.mp3", allow_exit, SF_FALSE, SF_FORMAT_MP3 | SF_FORMAT_MPEG_LAYER_III, -52.0) ; + float_scaled_test ("mpeg.mp3", allow_exit, SF_FALSE, SF_FORMAT_MPEG | SF_FORMAT_MPEG_LAYER_III, -52.0) ; #endif float_scaled_test ("replace_float.raw", allow_exit, SF_TRUE, SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_FLOAT, -163.0) ; @@ -190,7 +190,7 @@ main (int argc, char *argv []) #endif #if HAVE_MPEG - double_scaled_test ("mpeg.mp3", allow_exit, SF_FALSE, SF_FORMAT_MP3 | SF_FORMAT_MPEG_LAYER_III, -52.0) ; + double_scaled_test ("mpeg.mp3", allow_exit, SF_FALSE, SF_FORMAT_MPEG | SF_FORMAT_MPEG_LAYER_III, -52.0) ; #endif double_scaled_test ("replace_double.raw", allow_exit, SF_TRUE, SF_FORMAT_RAW | SF_FORMAT_DOUBLE, -201.0) ; diff --git a/tests/mp3_test.c b/tests/mp3_test.c index 76c64629..50faea41 100644 --- a/tests/mp3_test.c +++ b/tests/mp3_test.c @@ -68,7 +68,7 @@ mp3_short_test (void) memset (&sfinfo, 0, sizeof (sfinfo)) ; /* Set up output file type. */ - sfinfo.format = SF_FORMAT_MP3 | SF_FORMAT_MPEG_LAYER_III ; + sfinfo.format = SF_FORMAT_MPEG | SF_FORMAT_MPEG_LAYER_III ; sfinfo.channels = 1 ; sfinfo.samplerate = SAMPLE_RATE ; @@ -126,7 +126,7 @@ mp3_int_test (void) memset (&sfinfo, 0, sizeof (sfinfo)) ; /* Set up output file type. */ - sfinfo.format = SF_FORMAT_MP3 | SF_FORMAT_MPEG_LAYER_III ; + sfinfo.format = SF_FORMAT_MPEG | SF_FORMAT_MPEG_LAYER_III ; sfinfo.channels = 1 ; sfinfo.samplerate = SAMPLE_RATE ; @@ -175,7 +175,7 @@ mp3_float_test (void) memset (&sfinfo, 0, sizeof (sfinfo)) ; /* Set up output file type. */ - sfinfo.format = SF_FORMAT_MP3 | SF_FORMAT_MPEG_LAYER_III ; + sfinfo.format = SF_FORMAT_MPEG | SF_FORMAT_MPEG_LAYER_III ; sfinfo.channels = 1 ; sfinfo.samplerate = SAMPLE_RATE ; @@ -224,7 +224,7 @@ mp3_double_test (void) memset (&sfinfo, 0, sizeof (sfinfo)) ; /* Set up output file type. */ - sfinfo.format = SF_FORMAT_MP3 | SF_FORMAT_MPEG_LAYER_III ; + sfinfo.format = SF_FORMAT_MPEG | SF_FORMAT_MPEG_LAYER_III ; sfinfo.channels = 1 ; sfinfo.samplerate = SAMPLE_RATE ; @@ -339,7 +339,7 @@ main (void) mp3_float_test () ; mp3_double_test () ; - mp3_stereo_seek_test ("mpeg_seek.mp3", SF_FORMAT_MP3 | SF_FORMAT_MPEG_LAYER_III) ; + mp3_stereo_seek_test ("mpeg_seek.mp3", SF_FORMAT_MPEG | SF_FORMAT_MPEG_LAYER_III) ; } else puts (" No MPEG tests because mpg123/lame support was not compiled in.") ; diff --git a/tests/string_test.c b/tests/string_test.c index dd9bb278..ab07df7c 100644 --- a/tests/string_test.c +++ b/tests/string_test.c @@ -117,7 +117,7 @@ main (int argc, char *argv []) if (do_all || ! strcmp (argv [1], "mpeg")) { if (HAVE_MPEG) - string_start_test ("mpeg.mp3", SF_FORMAT_MP3 | SF_FORMAT_MPEG_LAYER_III) ; + string_start_test ("mpeg.mp3", SF_FORMAT_MPEG | SF_FORMAT_MPEG_LAYER_III) ; else puts (" No MP3 tests because MPEG support was not compiled in.") ; test_count++ ; @@ -384,7 +384,7 @@ string_start_test (const char *filename, int formattype) break ; case SF_FORMAT_OGG | SF_FORMAT_VORBIS : - case SF_FORMAT_MP3 | SF_FORMAT_MPEG_LAYER_III : + case SF_FORMAT_MPEG | SF_FORMAT_MPEG_LAYER_III : break ; default : @@ -403,7 +403,7 @@ string_start_test (const char *filename, int formattype) sf_set_string (file, SF_STR_COMMENT, comment) ; sf_set_string (file, SF_STR_ALBUM, album) ; sf_set_string (file, SF_STR_LICENSE, license) ; - if (typemajor == SF_FORMAT_MP3) + if (typemajor == SF_FORMAT_MPEG) { sf_set_string (file, SF_STR_GENRE, id3v1_genre) ; sf_set_string (file, SF_STR_DATE, year) ; } @@ -432,7 +432,7 @@ string_start_test (const char *filename, int formattype) printf (" Bad filename : %s\n", cptr) ; } ; - if (typemajor != SF_FORMAT_MP3) + if (typemajor != SF_FORMAT_MPEG) { cptr = sf_get_string (file, SF_STR_COPYRIGHT) ; if (cptr == NULL || strcmp (copyright, cptr) != 0) { if (errors++ == 0) @@ -454,7 +454,7 @@ string_start_test (const char *filename, int formattype) } ; } ; - if (typemajor == SF_FORMAT_MP3) + if (typemajor == SF_FORMAT_MPEG) { cptr = sf_get_string (file, SF_STR_GENRE) ; if (cptr == NULL || strcmp (id3v1_genre, cptr) != 0) { if (errors++ == 0) @@ -482,7 +482,7 @@ string_start_test (const char *filename, int formattype) /* not supported */ break ; - case SF_FORMAT_MP3 : + case SF_FORMAT_MPEG : /* id3 only supports years */ cptr = sf_get_string (file, SF_STR_DATE) ; if (cptr == NULL || strcmp (year, cptr) != 0) @@ -515,7 +515,7 @@ string_start_test (const char *filename, int formattype) { case SF_FORMAT_WAV : case SF_FORMAT_AIFF : case SF_FORMAT_RF64 : - case SF_FORMAT_MP3 : + case SF_FORMAT_MPEG : /* not supported */ break ; From 163f725631d519967beb791697a47469a5e4706c Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Mon, 11 Jan 2021 21:51:16 -0800 Subject: [PATCH 077/204] Rename mp3 -> mpeg (file sources and function names) --- CMakeLists.txt | 10 +++--- Makefile.am | 8 ++--- src/common.h | 2 +- src/{mp3.c => mpeg.c} | 59 +++++-------------------------- src/sndfile.c | 4 +-- tests/{mp3_test.c => mpeg_test.c} | 20 +++++------ tests/test_wrapper.sh.in | 2 +- 7 files changed, 32 insertions(+), 73 deletions(-) rename src/{mp3.c => mpeg.c} (55%) rename tests/{mp3_test.c => mpeg_test.c} (96%) diff --git a/CMakeLists.txt b/CMakeLists.txt index bf861ef9..6f58d8d9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -302,7 +302,7 @@ add_library (sndfile src/ogg_vcomment.h src/ogg_vcomment.c src/nms_adpcm.c - src/mp3.c + src/mpeg.c src/mpeg_decode.c src/mpeg_l3_encode.c src/GSM610/config.h @@ -1208,8 +1208,8 @@ if (BUILD_TESTING) $<$:m> ) - add_executable (mp3_test tests/mp3_test.c) - target_link_libraries (mp3_test + add_executable (mpeg_test tests/mpeg_test.c) + target_link_libraries (mpeg_test PRIVATE sndfile test_utils @@ -1447,8 +1447,8 @@ if (BUILD_TESTING) add_test (lossy_comp_test_ogg_opus lossy_comp_test ogg_opus) add_test (string_test_opus string_test opus) - ### mp3-tests ### - add_test (mp3_test mp3_test) + ### mpeg-tests ### + add_test (mpeg_test mpeg_test) add_test (compression_size_test_mpeg compression_size_test mpeg) ### io-tests diff --git a/Makefile.am b/Makefile.am index da1ae5e4..61bcc1f8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -69,7 +69,7 @@ src_libsndfile_la_SOURCES = src/sndfile.c src/aiff.c src/au.c src/avr.c src/caf. src/macos.c src/mat4.c src/mat5.c src/nist.c src/paf.c src/pvf.c src/raw.c src/rx2.c src/sd2.c \ src/sds.c src/svx.c src/txw.c src/voc.c src/wve.c src/w64.c src/wavlike.c src/wav.c src/xi.c src/mpc2k.c src/rf64.c \ src/ogg.c src/ogg.h src/ogg_vorbis.c src/ogg_speex.c src/ogg_pcm.c src/ogg_opus.c src/ogg_vcomment.c src/ogg_vcomment.h \ - src/common.h src/sfconfig.h src/sfendian.h src/wavlike.h src/sf_unistd.h src/chanmap.h src/mp3.c + src/common.h src/sfconfig.h src/sfendian.h src/wavlike.h src/sf_unistd.h src/chanmap.h src/mpeg.c nodist_src_libsndfile_la_SOURCES = $(nodist_include_HEADERS) src_libsndfile_la_LIBADD = src/GSM610/libgsm.la src/G72x/libg72x.la src/ALAC/libalac.la \ src/libcommon.la $(EXTERNAL_XIPH_LIBS) -lm $(MPEG_LIBS) @@ -222,7 +222,7 @@ check_PROGRAMS += tests/sfversion tests/floating_point_test tests/write_read_tes tests/checksum_test tests/external_libs_test tests/rdwr_test tests/format_check_test $(CPP_TEST) \ tests/channel_test tests/long_read_write_test tests/stdin_test tests/stdout_test \ tests/dither_test tests/fix_this tests/largefile_test tests/benchmark tests/ogg_opus_test \ - tests/mp3_test + tests/mpeg_test BUILT_SOURCES += \ tests/write_read_test.c \ @@ -378,8 +378,8 @@ tests_cpp_test_LDADD = src/libsndfile.la tests_checksum_test_SOURCES = tests/checksum_test.c tests/utils.c tests/utils.h tests_checksum_test_LDADD = src/libsndfile.la -tests_mp3_test_SOURCES = tests/mp3_test.c tests/utils.c tests/utils.h -tests_mp3_test_LDADD = src/libsndfile.la +tests_mpeg_test_SOURCES = tests/mpeg_test.c tests/utils.c tests/utils.h +tests_mpeg_test_LDADD = src/libsndfile.la # Lite remove start tests_dwvw_test_SOURCES = tests/dwvw_test.c tests/utils.c tests/utils.h diff --git a/src/common.h b/src/common.h index 8a2e6497..59e6922d 100644 --- a/src/common.h +++ b/src/common.h @@ -896,7 +896,7 @@ int ogg_pcm_open (SF_PRIVATE *psf) ; int ogg_opus_open (SF_PRIVATE *psf) ; int ogg_open (SF_PRIVATE *psf) ; -int mp3_open (SF_PRIVATE *psf) ; +int mpeg_open (SF_PRIVATE *psf) ; /* In progress. Do not currently work. */ diff --git a/src/mp3.c b/src/mpeg.c similarity index 55% rename from src/mp3.c rename to src/mpeg.c index 8e400f3a..776420b1 100644 --- a/src/mp3.c +++ b/src/mpeg.c @@ -1,6 +1,6 @@ /* ** Copyright (C) 2019 Erik de Castro Lopo -** Copyright (C) 2019 Arthur Taylor +** Copyright (C) 2021 Arthur Taylor ** ** This program is free software ; you can redistribute it and/or modify ** it under the terms of the GNU Lesser General Public License as published by @@ -17,47 +17,6 @@ ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/* -** What is an MP3 file anyways? -** -** Believe it or not, MP3 files don't exist. -** -** The MPEG-1 standard defined a few audio codecs. The standard only defined a -** streaming format of semi-independent frames of audio meant for broadcasting, -** with no details or hints about stored on-disk formats. Each frame defines -** it's own bitrate, channel count, sample rate. However, they aren't -** completely independent. -** -** With its amazing-for-the-time compression ratio, the layer III audio codec -** became quite popular with file sharers and the internet. A stream of layer -** III audio would simply be written as a file, usually with the extension -** .mp3. Over time enthusiast and proprietary encoders sprung up adding -** different metadata headers and trailers, file seeking tables, and fiddling -** with the codecs parameters. These changes are only really based on consensus. -** -** MPEG-1 I/II/III audio can be embedded in a few container formats (including -** WAV), stored raw, or with additional metadata extension headers and trailers. -** -** This file is concerned only with the most common case of MPEG Layer III -** audio without a container but with the additional metadata standards. -** -** For the purposes of libsndfile, the major format of SF_FORMAT_MPEG means the -** following assumptions. A file of major format type SF_FORMAT_MPEG: -** - Contains only layer III audio frames (SF_FORMAT_MPEG_LAYER_III) -** - All MPEG frames contained in the file have the same channel count -** - All MPEG frames contained in the file have the same samplerate -** - Has at least one of: -** - an ID3v1 trailer -** - an ID3v2 header or trailer -** - A Lame/Xing/Info header -** -** Testing has revealed that, more than any other format, MP3 suffers from -** corrupt files in the wild that most other software 'just works' with. This is -** usually because the MP3 decoders are very lenient. They are aided by the -** presence of a regular sync frame, but this makes it hard to classify them -** in a library that consumes other better-specified file formats. -*/ - #include "sfconfig.h" #include "sndfile.h" @@ -67,15 +26,15 @@ #include "mpeg.h" -static int mp3_write_header (SF_PRIVATE *psf, int calc_length) ; -static int mp3_command (SF_PRIVATE *psf, int command, void *data, int datasize) ; +static int mpeg_write_header (SF_PRIVATE *psf, int calc_length) ; +static int mpeg_command (SF_PRIVATE *psf, int command, void *data, int datasize) ; /*------------------------------------------------------------------------------ * Private functions */ static int -mp3_write_header (SF_PRIVATE *psf, int UNUSED (calc_length)) +mpeg_write_header (SF_PRIVATE *psf, int UNUSED (calc_length)) { if (psf->have_written) return 0 ; @@ -84,7 +43,7 @@ mp3_write_header (SF_PRIVATE *psf, int UNUSED (calc_length)) } ; static int -mp3_command (SF_PRIVATE *psf, int command, void *data, int datasize) +mpeg_command (SF_PRIVATE *psf, int command, void *data, int datasize) { int bitrate_mode ; switch (command) @@ -129,7 +88,7 @@ mp3_command (SF_PRIVATE *psf, int command, void *data, int datasize) */ int -mp3_open (SF_PRIVATE *psf) +mpeg_open (SF_PRIVATE *psf) { int error ; if (psf->file.mode == SFM_RDWR) @@ -147,7 +106,7 @@ mp3_open (SF_PRIVATE *psf) /* ID3 support */ psf->strings.flags = SF_STR_ALLOW_START ; - psf->write_header = mp3_write_header ; + psf->write_header = mpeg_write_header ; psf->datalength = 0 ; psf->dataoffset = 0 ; } ; @@ -157,7 +116,7 @@ mp3_open (SF_PRIVATE *psf) return error ; } ; - psf->command = mp3_command ; + psf->command = mpeg_command ; return 0 ; } /* mpeg_open */ @@ -165,7 +124,7 @@ mp3_open (SF_PRIVATE *psf) #else /* HAVE_MPEG */ int -mp3_open (SF_PRIVATE *psf) +mpeg_open (SF_PRIVATE *psf) { psf_log_printf (psf, "This version of libsndfile was compiled without MP3 support.\n") ; return SFE_UNIMPLEMENTED ; diff --git a/src/sndfile.c b/src/sndfile.c index a07b4462..58df240a 100644 --- a/src/sndfile.c +++ b/src/sndfile.c @@ -2884,7 +2884,7 @@ retry: /* Guess MP3, try and open it as such. Allows libmpg123 to parse the ID3v2 headers */ if (psf->file.mode == SFM_READ) - { if (mp3_open (psf) == 0) + { if (mpeg_open (psf) == 0) return SF_FORMAT_MPEG | ((~SF_FORMAT_TYPEMASK) & psf->sf.format) ; else if (psf->codec_close) psf->codec_close (psf) ; @@ -3270,7 +3270,7 @@ psf_open_file (SF_PRIVATE *psf, SF_INFO *sfinfo) break ; case SF_FORMAT_MPEG : - error = mp3_open (psf) ; + error = mpeg_open (psf) ; break ; /* Lite remove end */ diff --git a/tests/mp3_test.c b/tests/mpeg_test.c similarity index 96% rename from tests/mp3_test.c rename to tests/mpeg_test.c index 50faea41..fe7f53d1 100644 --- a/tests/mp3_test.c +++ b/tests/mpeg_test.c @@ -48,7 +48,7 @@ static BUFFER data_out ; static BUFFER data_in ; static void -mp3_short_test (void) +mpeg_short_test (void) { const char * filename = "mpeg_short.mp3" ; SNDFILE * file ; @@ -106,7 +106,7 @@ mp3_short_test (void) } /* mpeg_short_test */ static void -mp3_int_test (void) +mpeg_int_test (void) { const char * filename = "mpeg_int.mp3" ; SNDFILE * file ; @@ -161,7 +161,7 @@ mp3_int_test (void) } /* mpeg_int_test */ static void -mp3_float_test (void) +mpeg_float_test (void) { const char * filename = "mpeg_float.mp3" ; SNDFILE * file ; @@ -210,7 +210,7 @@ mp3_float_test (void) } /* mpeg_float_test */ static void -mp3_double_test (void) +mpeg_double_test (void) { const char * filename = "mpeg_double.mp3" ; SNDFILE * file ; @@ -260,7 +260,7 @@ mp3_double_test (void) static void -mp3_stereo_seek_test (const char * filename, int format) +mpeg_stereo_seek_test (const char * filename, int format) { static float data [SAMPLE_RATE] ; static float stereo_out [SAMPLE_RATE * 2] ; @@ -334,12 +334,12 @@ int main (void) { if (HAVE_MPEG) - { mp3_short_test () ; - mp3_int_test () ; - mp3_float_test () ; - mp3_double_test () ; + { mpeg_short_test () ; + mpeg_int_test () ; + mpeg_float_test () ; + mpeg_double_test () ; - mp3_stereo_seek_test ("mpeg_seek.mp3", SF_FORMAT_MPEG | SF_FORMAT_MPEG_LAYER_III) ; + mpeg_stereo_seek_test ("mpeg_seek.mp3", SF_FORMAT_MPEG | SF_FORMAT_MPEG_LAYER_III) ; } else puts (" No MPEG tests because mpg123/lame support was not compiled in.") ; diff --git a/tests/test_wrapper.sh.in b/tests/test_wrapper.sh.in index b74b7664..ab92f7c4 100644 --- a/tests/test_wrapper.sh.in +++ b/tests/test_wrapper.sh.in @@ -353,7 +353,7 @@ echo " $sfversion passed tests on OPUS files." echo "----------------------------------------------------------------------" # mpeg-tests -./tests/mp3_test@EXEEXT@ +./tests/mpeg_test@EXEEXT@ ./tests/compression_size_test@EXEEXT@ mpeg ./tests/string_test@EXEEXT@ mpeg From fe2ed77134db8dd4ea20dee8e5c1235d8336b981 Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Sat, 23 Jan 2021 11:04:32 -0800 Subject: [PATCH 078/204] Add MPEG FORMAT to SF_COMMAND_*FORMAT, program common. --- programs/common.c | 1 + src/command.c | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/programs/common.c b/programs/common.c index 84e4cc35..3c28f2ae 100644 --- a/programs/common.c +++ b/programs/common.c @@ -344,6 +344,7 @@ static OUTPUT_FORMAT_MAP format_map [] = { "opus", 0, SF_FORMAT_OGG | SF_FORMAT_OPUS }, { "mpc", 0, SF_FORMAT_MPC2K }, { "rf64", 0, SF_FORMAT_RF64 }, + { "mp3", 0, SF_FORMAT_MPEG | SF_FORMAT_MPEG_LAYER_III }, } ; /* format_map */ int diff --git a/src/command.c b/src/command.c index a5f8bb8b..d4837dab 100644 --- a/src/command.c +++ b/src/command.c @@ -61,6 +61,12 @@ static SF_FORMAT_INFO const simple_formats [] = }, #endif +#if HAVE_MPEG + { SF_FORMAT_MPEG | SF_FORMAT_MPEG_LAYER_III, + "MPEG Layer 3", "mp3" + }, +#endif + { SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM, "OKI Dialogic VOX ADPCM", "vox" }, @@ -133,6 +139,9 @@ static SF_FORMAT_INFO const major_formats [] = { SF_FORMAT_MAT4, "MAT4 (GNU Octave 2.0 / Matlab 4.2)", "mat" }, { SF_FORMAT_MAT5, "MAT5 (GNU Octave 2.1 / Matlab 5.0)", "mat" }, { SF_FORMAT_MPC2K, "MPC (Akai MPC 2k)", "mpc" }, +#if HAVE_MPEG + { SF_FORMAT_MPEG, "MPEG-1/2 Audio", "m1a" }, +#endif #if HAVE_EXTERNAL_XIPH_LIBS { SF_FORMAT_OGG, "OGG (OGG Container format)", "oga" }, #endif @@ -215,6 +224,12 @@ static SF_FORMAT_INFO subtype_formats [] = { SF_FORMAT_OPUS, "Opus", NULL }, #endif +#if HAVE_MPEG + { SF_FORMAT_MPEG_LAYER_I, "MPEG Layer I", "mp1" }, + { SF_FORMAT_MPEG_LAYER_II, "MPEG Layer II", "mp2" }, + { SF_FORMAT_MPEG_LAYER_III, "MPEG Layer III", "mp3" }, +#endif + { SF_FORMAT_ALAC_16, "16 bit ALAC", NULL }, { SF_FORMAT_ALAC_20, "20 bit ALAC", NULL }, { SF_FORMAT_ALAC_24, "24 bit ALAC", NULL }, From 30ba21d6c2b9aabf29ac3a02d309f49211859b5d Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Sat, 23 Jan 2021 12:43:11 -0800 Subject: [PATCH 079/204] mpeg: Align decode log printing --- src/mpeg_decode.c | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/mpeg_decode.c b/src/mpeg_decode.c index 1ab34c38..bf0a827e 100644 --- a/src/mpeg_decode.c +++ b/src/mpeg_decode.c @@ -265,28 +265,29 @@ mpeg_dec_fill_sfinfo (mpg123_handle *mh, SF_INFO *info) static void mpeg_dec_print_frameinfo (SF_PRIVATE *psf, const struct mpg123_frameinfo *fi) { psf_log_printf (psf, "MPEG-1/2 Audio\n----------------------------------------\n") ; - psf_log_printf (psf, " version: %s\n", + psf_log_printf (psf, " MPEG version : %s\n", fi->version == MPG123_1_0 ? "MPEG 1.0" : fi->version == MPG123_2_0 ? "MPEG 2.0" : - fi->version == MPG123_2_5 ? "MPEG 2.5" : "?") ; - psf_log_printf (psf, " layer: %d\n", fi->layer) ; - psf_log_printf (psf, " rate: %d\n", fi->rate) ; - psf_log_printf (psf, " mode: %s\n", + fi->version == MPG123_2_5 ? "MPEG 2.5" : "???") ; + psf_log_printf (psf, " layer : %d\n", fi->layer) ; + psf_log_printf (psf, " rate : %d\n", fi->rate) ; + psf_log_printf (psf, " mode : %s\n", fi->mode == MPG123_M_STEREO ? "stereo" : fi->mode == MPG123_M_JOINT ? "joint stereo" : fi->mode == MPG123_M_DUAL ? "dual channel" : - fi->mode == MPG123_M_MONO ? "mono" : "?") ; - psf_log_printf (psf, " mode ext: %d\n", fi->mode_ext) ; - psf_log_printf (psf, " framesize: %d\n", fi->framesize) ; - psf_log_printf (psf, " crc: %c\n", fi->flags & MPG123_CRC ? '1' : '0') ; - psf_log_printf (psf, " copyright flag: %c\n", fi->flags & MPG123_COPYRIGHT ? '1' : '0') ; - psf_log_printf (psf, " private flag: %c\n", fi->flags & MPG123_PRIVATE ? '1' : '0') ; - psf_log_printf (psf, " original flag: %c\n", fi->flags & MPG123_ORIGINAL ? '1' : '0') ; - psf_log_printf (psf, " emphasis: %d\n", fi->emphasis) ; - psf_log_printf (psf, " bitrate mode: ") ; + fi->mode == MPG123_M_MONO ? "mono" : "???") ; + psf_log_printf (psf, " mode ext : %d\n", fi->mode_ext) ; + psf_log_printf (psf, " framesize : %d\n", fi->framesize) ; + psf_log_printf (psf, " crc : %d\n", !! (fi->flags & MPG123_CRC)) ; + psf_log_printf (psf, " copyright flag : %d\n", !! (fi->flags & MPG123_COPYRIGHT)) ; + psf_log_printf (psf, " private flag : %d\n", !! (fi->flags & MPG123_PRIVATE)) ; + psf_log_printf (psf, " original flag : %d\n", !! (fi->flags & MPG123_ORIGINAL)) ; + psf_log_printf (psf, " emphasis : %d\n", fi->emphasis) ; + psf_log_printf (psf, " bitrate mode : ") ; switch (fi->vbr) { case MPG123_CBR : psf_log_printf (psf, "constant\n") ; + psf_log_printf (psf, " bitrate : %d kbps\n", fi->bitrate) ; break ; case MPG123_VBR : psf_log_printf (psf, "variable\n") ; @@ -294,10 +295,13 @@ mpeg_dec_print_frameinfo (SF_PRIVATE *psf, const struct mpg123_frameinfo *fi) case MPG123_ABR : psf_log_printf (psf, "average\n") ; - psf_log_printf (psf, " ABR target: %d\n", fi->abr_rate) ; + psf_log_printf (psf, " ABR target : %d\n", fi->abr_rate) ; + break ; + + default : + psf_log_printf (psf, "(%d) ???\n", fi->vbr) ; break ; } ; - psf_log_printf (psf, " bitrate: %d kbps\n", fi->bitrate) ; } /* mpeg_dec_print_frameinfo */ /* From 64ce116687290689b20aed287c808f65cb78f8a0 Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Sat, 23 Jan 2021 14:50:00 -0800 Subject: [PATCH 080/204] Fix command_test Subformat count --- tests/command_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/command_test.c b/tests/command_test.c index c2379201..86cbcfb3 100644 --- a/tests/command_test.c +++ b/tests/command_test.c @@ -532,7 +532,7 @@ format_tests (void) /* Now test subtype formats. */ sf_command (NULL, SFC_GET_FORMAT_SUBTYPE_COUNT, &count, sizeof (int)) ; - if (count < 0 || count > 30) + if (count < 0 || count > 33) { printf ("Line %d: Weird count.\n", __LINE__) ; exit (1) ; } ; From f401000fd04b74e98a8d0399721c53756c4e1e3a Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Sat, 23 Jan 2021 14:56:09 -0800 Subject: [PATCH 081/204] Skip format_check_test for asymmetric MPEG Layer I/II libmpg123 supports decoding MPEG Layers I, II, and III, but we only have encode support for Layer III. Skip checks for I and II as they don't have write support. --- tests/format_check_test.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/format_check_test.c b/tests/format_check_test.c index 7ab71c9b..eda557a4 100644 --- a/tests/format_check_test.c +++ b/tests/format_check_test.c @@ -121,6 +121,11 @@ format_combo_test (void) __LINE__ ) ; + /* Only have decode, not encode support for MPEG Layer I and II */ + if (subtype_fmt_info.format == SF_FORMAT_MPEG_LAYER_I || + subtype_fmt_info.format == SF_FORMAT_MPEG_LAYER_II) + continue ; + snprintf (filename, sizeof (filename), "format-check.%s", major_fmt_info.extension) ; sndfile = sf_open (filename, SFM_WRITE, &info) ; From b00db8b15c23383d92800c0df06c04987f934687 Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Sun, 24 Jan 2021 21:12:48 -0800 Subject: [PATCH 082/204] Add mpeg_init() for initializing the codec without file assumptions. Future proofing for MPEG in WAV support. --- src/common.h | 1 + src/mpeg.c | 52 +++++++++++++++++++++++++++++++++----------- src/mpeg_l3_encode.c | 1 - 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/common.h b/src/common.h index 59e6922d..bc7df5ca 100644 --- a/src/common.h +++ b/src/common.h @@ -921,6 +921,7 @@ int vox_adpcm_init (SF_PRIVATE *psf) ; int flac_init (SF_PRIVATE *psf) ; int g72x_init (SF_PRIVATE * psf) ; int alac_init (SF_PRIVATE *psf, const ALAC_DECODER_INFO * info) ; +int mpeg_init (SF_PRIVATE *psf, int bitrate_mode, int write_metadata) ; int dither_init (SF_PRIVATE *psf, int mode) ; diff --git a/src/mpeg.c b/src/mpeg.c index 776420b1..31a19e03 100644 --- a/src/mpeg.c +++ b/src/mpeg.c @@ -88,27 +88,34 @@ mpeg_command (SF_PRIVATE *psf, int command, void *data, int datasize) */ int -mpeg_open (SF_PRIVATE *psf) +mpeg_init (SF_PRIVATE *psf, int bitrate_mode, int write_metadata) { int error ; if (psf->file.mode == SFM_RDWR) return SFE_BAD_MODE_RW ; if (psf->file.mode == SFM_WRITE) - { if (SF_CODEC (psf->sf.format) != SF_FORMAT_MPEG_LAYER_III) - return SFE_BAD_OPEN_FORMAT ; + { switch (SF_CODEC (psf->sf.format)) + { case SF_FORMAT_MPEG_LAYER_III : + if ((error = mpeg_l3_encoder_init (psf, write_metadata))) + return error ; + mpeg_l3_encoder_set_bitrate_mode (psf, bitrate_mode) ; + if (write_metadata) + { /* ID3 support */ + psf->strings.flags = SF_STR_ALLOW_START ; + psf->write_header = mpeg_write_header ; + } ; + break ; - if ((error = mpeg_l3_encoder_init (psf, SF_TRUE))) - return error ; + case SF_FORMAT_MPEG_LAYER_I : + case SF_FORMAT_MPEG_LAYER_II : + psf_log_printf (psf, "MPEG Layer I and II encoding is not yet supported.\n") ; + return SFE_UNIMPLEMENTED ; - /* Choose variable bitrate mode by default for standalone (mp3) files.*/ - mpeg_l3_encoder_set_bitrate_mode (psf, SF_BITRATE_MODE_VARIABLE) ; - - /* ID3 support */ - psf->strings.flags = SF_STR_ALLOW_START ; - psf->write_header = mpeg_write_header ; - psf->datalength = 0 ; - psf->dataoffset = 0 ; + default: + psf_log_printf (psf, "%s: bad psf->sf.format 0x%x.\n", __func__, psf->sf.format) ; + return SFE_INTERNAL ; + } ; } ; if (psf->file.mode == SFM_READ) @@ -116,6 +123,18 @@ mpeg_open (SF_PRIVATE *psf) return error ; } ; + return 0 ; +} /* mpeg_init */ + +int +mpeg_open (SF_PRIVATE *psf) +{ int error ; + + /* Choose variable bitrate mode by default for standalone files.*/ + if ((error = mpeg_init (psf, SF_BITRATE_MODE_VARIABLE, SF_TRUE))) + return error ; + + psf->dataoffset = 0 ; psf->command = mpeg_command ; return 0 ; @@ -123,6 +142,13 @@ mpeg_open (SF_PRIVATE *psf) #else /* HAVE_MPEG */ +int +mpeg_init (SF_PRIVATE *psf, int UNUSED (bitrate_mode) , int UNUSED (write_metadata)) +{ + psf_log_printf (psf, "This version of libsndfile was compiled without MPEG support.\n") ; + return SFE_UNIMPLEMENTED ; +} /* mpeg_init */ + int mpeg_open (SF_PRIVATE *psf) { diff --git a/src/mpeg_l3_encode.c b/src/mpeg_l3_encode.c index 1c7f2ae6..5a9424d4 100644 --- a/src/mpeg_l3_encode.c +++ b/src/mpeg_l3_encode.c @@ -117,7 +117,6 @@ mpeg_l3_encoder_init (SF_PRIVATE *psf, int info_tag) psf->sf.seekable = 0 ; psf->codec_close = mpeg_l3_encoder_close ; psf->byterate = mpeg_l3_encoder_byterate ; - psf->dataoffset = 0 ; psf->datalength = 0 ; return 0 ; From f97fa5e6ba43baa8a67245ce0223836e5175cbd9 Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Mon, 22 Mar 2021 11:05:57 -0700 Subject: [PATCH 083/204] id3: Remember id3v2 Header Info. Do Call mpeg_decode() Twice. --- src/common.c | 4 ++++ src/common.h | 8 ++++++++ src/id3.c | 24 ++++++++++++++++-------- src/mpeg_decode.c | 31 ++++++++++++++++++------------- src/sndfile.c | 11 ----------- 5 files changed, 46 insertions(+), 32 deletions(-) diff --git a/src/common.c b/src/common.c index a198c28d..b8cc2c19 100644 --- a/src/common.c +++ b/src/common.c @@ -1136,6 +1136,10 @@ psf_binheader_readf (SF_PRIVATE *psf, char const *format, ...) byte_count += count ; break ; + case '!' : /* Clear buffer, forcing re-read. */ + psf->header.end = psf->header.indx = 0 ; + break ; + default : psf_log_printf (psf, "*** Invalid format specifier `%c'\n", c) ; psf->error = SFE_INTERNAL ; diff --git a/src/common.h b/src/common.h index bc7df5ca..6ca0648f 100644 --- a/src/common.h +++ b/src/common.h @@ -296,6 +296,12 @@ typedef SF_BROADCAST_INFO_VAR (16 * 1024) SF_BROADCAST_INFO_16K ; typedef SF_CART_INFO_VAR (16 * 1024) SF_CART_INFO_16K ; +typedef struct +{ sf_count_t offset ; + sf_count_t len ; + unsigned minor_version ; +} ID3V2_HEADER_INFO ; + #if SIZEOF_WCHAR_T == 2 typedef wchar_t sfwchar_t ; #else @@ -516,6 +522,8 @@ typedef struct sf_private_tag int (*get_chunk_data) (struct sf_private_tag*, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info) ; int cpu_flags ; + + ID3V2_HEADER_INFO id3_header ; } SF_PRIVATE ; diff --git a/src/id3.c b/src/id3.c index 6302f5b0..1516ee31 100644 --- a/src/id3.c +++ b/src/id3.c @@ -63,31 +63,39 @@ id3_lookup_v1_genre (int UNUSED (number)) #endif - int id3_skip (SF_PRIVATE * psf) { unsigned char buf [10] ; + int offset ; memset (buf, 0, sizeof (buf)) ; psf_binheader_readf (psf, "pb", 0, buf, 10) ; if (buf [0] == 'I' && buf [1] == 'D' && buf [2] == '3') - { int offset = buf [6] & 0x7f ; + { psf->id3_header.minor_version = buf [3] ; + offset = buf [6] & 0x7f ; offset = (offset << 7) | (buf [7] & 0x7f) ; offset = (offset << 7) | (buf [8] & 0x7f) ; offset = (offset << 7) | (buf [9] & 0x7f) ; - psf_log_printf (psf, "ID3 length : %d\n--------------------\n", offset) ; + /* + ** ID3 count field is how many bytes of ID3v2 header FOLLOW the ten + ** bytes of header magic and offset, NOT the total ID3v2 header len. + */ + psf->id3_header.len = offset + 10 ; + psf->id3_header.offset = psf->fileoffset ; + + psf_log_printf (psf, " ID3v2.%d header length : %d\n----------------------------------------\n", + psf->id3_header.minor_version, psf->id3_header.len) ; /* Never want to jump backwards in a file. */ if (offset < 0) return 0 ; - /* Calculate new file offset and position ourselves there. */ - offset += 10 ; - if (psf->fileoffset + offset < psf->filelength) - { psf_binheader_readf (psf, "p", offset) ; - psf->fileoffset += offset ; + /* Position ourselves at the new file offset. */ + if (psf->fileoffset + psf->id3_header.len < psf->filelength) + { psf_binheader_readf (psf, "p!", psf->id3_header.len) ; + psf->fileoffset += psf->id3_header.len ; return 1 ; } ; } ; diff --git a/src/mpeg_decode.c b/src/mpeg_decode.c index bf0a827e..64efa564 100644 --- a/src/mpeg_decode.c +++ b/src/mpeg_decode.c @@ -526,17 +526,6 @@ mpeg_decoder_init (SF_PRIVATE *psf) if (! (psf->file.mode & SFM_READ)) return SFE_INTERNAL ; - /* - ** Check to see if we have already successfully opened the file when we - ** guessed it was an MP3 based on seeing an ID3 header. - */ - if (psf->codec_data != NULL && - (SF_CODEC (psf->sf.format) == SF_FORMAT_MPEG_LAYER_I - || SF_CODEC (psf->sf.format) == SF_FORMAT_MPEG_LAYER_II - || SF_CODEC (psf->sf.format) == SF_FORMAT_MPEG_LAYER_III) - && ((MPEG_DEC_PRIVATE *) psf->codec_data)->unique_id == psf->unique_id) - return 0 ; - /* ** *** FIXME - Threading issues *** ** @@ -580,15 +569,31 @@ mpeg_decoder_init (SF_PRIVATE *psf) #endif psf->dataoffset = 0 ; - + /* + ** Need to pass the first MPEG frame to libmpg123, but that frame was read + ** into psf->binheader in order that we could identify the stream. + */ if (psf->is_pipe) - { // Need to read data in the binheader buffer. Avoid a file seek. + { /* + ** Can't seek, so setup our libmpg123 io callbacks to read the binheader + ** buffer first. + */ psf_binheader_readf (psf, "p", 0) ; pmp3d->header_remaining = psf_binheader_readf (psf, NULL) ; + + /* Tell libmpg123 we can't seek the file. */ mpg123_param (pmp3d->pmh, MPG123_ADD_FLAGS, MPG123_NO_PEEK_END, 1.0) ; } else + { /* + ** libmpg123 can parse the ID3v2 header. Undo the embedded file offset if the + ** enclosing file data is the ID3v2 header. + */ + if (psf->id3_header.len > 0 && psf->id3_header.len + psf->id3_header.offset == psf->fileoffset) + psf->fileoffset = psf->id3_header.offset ; + psf_fseek (psf, 0, SEEK_SET) ; + } ; error = mpg123_open_handle (pmp3d->pmh, psf) ; if (error != MPG123_OK) diff --git a/src/sndfile.c b/src/sndfile.c index 58df240a..365c1361 100644 --- a/src/sndfile.c +++ b/src/sndfile.c @@ -967,7 +967,6 @@ sf_format_check (const SF_INFO *info) return 0 ; if (endian != SF_ENDIAN_FILE) return 0 ; - /* TODO */ if (subformat == SF_FORMAT_MPEG_LAYER_I || subformat == SF_FORMAT_MPEG_LAYER_II || subformat == SF_FORMAT_MPEG_LAYER_III) return 1 ; break ; @@ -2881,16 +2880,6 @@ retry: if (buffer [0] == MAKE_MARKER ('I', 'D', '3', 2) || buffer [0] == MAKE_MARKER ('I', 'D', '3', 3) || buffer [0] == MAKE_MARKER ('I', 'D', '3', 4)) { psf_log_printf (psf, "Found 'ID3' marker.\n") ; - - /* Guess MP3, try and open it as such. Allows libmpg123 to parse the ID3v2 headers */ - if (psf->file.mode == SFM_READ) - { if (mpeg_open (psf) == 0) - return SF_FORMAT_MPEG | ((~SF_FORMAT_TYPEMASK) & psf->sf.format) ; - else if (psf->codec_close) - psf->codec_close (psf) ; - } ; - - /* Otherwise, seek to after the ID3 header */ if (id3_skip (psf)) goto retry ; return 0 ; From 72521cf4f7645ff58cc59b73ff01a4ceb7fa7022 Mon Sep 17 00:00:00 2001 From: evpobr Date: Fri, 26 Mar 2021 12:10:48 +0500 Subject: [PATCH 084/204] doc: Add information about MPEG support --- CHANGELOG.md | 21 ++++++++ docs/FAQ.md | 6 ++- docs/api.md | 140 +++++++++++++++++++++++++----------------------- docs/command.md | 62 +++++++++++++++++++++ docs/formats.md | 3 +- 5 files changed, 161 insertions(+), 71 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 78681028..5a0ec0f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +* MPEG Encode/Decode Support. + + Uses libmpg123 for decode, liblame for encode. Encoding and decoding support + is independent of each other and is split into separate files. MPEG support + is generalized as subformats, `SF_FORMAT_MPEG_LAYER`(I,II,III) so that it + might be used by other containers (`MPEG1WAVEFORMAT` for example), but also + contains a major format `SF_FORMAT_MPEG` for 'mp3 files.' + + Encoding Status: + * Layer III encoding + * ID3v1 writing + * ID3v2 writing + * Lame/Xing Tag writing + * Bitrate selection command + * VBR or CBR + + Decoding Status: + * Layers I/II/III decoding + * ID3v1 reading + * ID3v2 reading + * Seeking * This `CHANGELOG.md`. All notable changes to this project will be documented in this file. The old `NEWS` file has been renamed to `NEWS.OLD` and is no longer updated. diff --git a/docs/FAQ.md b/docs/FAQ.md index cca4714d..e7c98e25 100644 --- a/docs/FAQ.md +++ b/docs/FAQ.md @@ -438,10 +438,12 @@ Use CMake project. ## Q20 : Why doesn't libsndfile support MP3? {#Q020} -In the past, MP3 was not supported because the technology behind MP3 was +~~In the past, MP3 was not supported because the technology behind MP3 was patented. Those patents have now expired and there is an [open ticket](https://github.com/libsndfile/libsndfile/issues/258) to implement -MP3 support. +MP3 support.~~ + +**Update :** Starting from version 1.1.0 libsndfile supports MP3 format. ## Q21 : How do I use libsndfile in a closed source or commercial program and comply with the license? {#Q021} diff --git a/docs/api.md b/docs/api.md index ac21299d..7025443b 100644 --- a/docs/api.md +++ b/docs/api.md @@ -112,74 +112,78 @@ value. The currently understood formats are listed in *sndfile.h* as follows and also include bitmasks for separating major and minor file types. Not all combinations of endian-ness and major and minor file types are valid. -| Name | Value | Description | -|:-----------------------|:-----------|:-------------------------------------------| -| **Major formats.** | -| SF_FORMAT_WAV | 0x010000 | Microsoft WAV format (little endian). | -| SF_FORMAT_AIFF | 0x020000 | Apple/SGI AIFF format (big endian). | -| SF_FORMAT_AU | 0x030000 | Sun/NeXT AU format (big endian). | -| SF_FORMAT_RAW | 0x040000 | RAW PCM data. | -| SF_FORMAT_PAF | 0x050000 | Ensoniq PARIS file format. | -| SF_FORMAT_SVX | 0x060000 | Amiga IFF / SVX8 / SV16 format. | -| SF_FORMAT_NIST | 0x070000 | Sphere NIST format. | -| SF_FORMAT_VOC | 0x080000 | VOC files. | -| SF_FORMAT_IRCAM | 0x0A0000 | Berkeley/IRCAM/CARL | -| SF_FORMAT_W64 | 0x0B0000 | Sonic Foundry's 64 bit RIFF/WAV | -| SF_FORMAT_MAT4 | 0x0C0000 | Matlab (tm) V4.2 / GNU Octave 2.0 | -| SF_FORMAT_MAT5 | 0x0D0000 | Matlab (tm) V5.0 / GNU Octave 2.1 | -| SF_FORMAT_PVF | 0x0E0000 | Portable Voice Format | -| SF_FORMAT_XI | 0x0F0000 | Fasttracker 2 Extended Instrument | -| SF_FORMAT_HTK | 0x100000 | HMM Tool Kit format | -| SF_FORMAT_SDS | 0x110000 | Midi Sample Dump Standard | -| SF_FORMAT_AVR | 0x120000 | Audio Visual Research | -| SF_FORMAT_WAVEX | 0x130000 | MS WAVE with WAVEFORMATEX | -| SF_FORMAT_SD2 | 0x160000 | Sound Designer 2 | -| SF_FORMAT_FLAC | 0x170000 | FLAC lossless file format | -| SF_FORMAT_CAF | 0x180000 | Core Audio File format | -| SF_FORMAT_WVE | 0x190000 | Psion WVE format | -| SF_FORMAT_OGG | 0x200000 | Xiph OGG container | -| SF_FORMAT_MPC2K | 0x210000 | Akai MPC 2000 sampler | -| SF_FORMAT_RF64 | 0x220000 | RF64 WAV file | -| **Subtypes.** | -| SF_FORMAT_PCM_S8 | 0x0001 | Signed 8 bit data | -| SF_FORMAT_PCM_16 | 0x0002 | Signed 16 bit data | -| SF_FORMAT_PCM_24 | 0x0003 | Signed 24 bit data | -| SF_FORMAT_PCM_32 | 0x0004 | Signed 32 bit data | -| SF_FORMAT_PCM_U8 | 0x0005 | Unsigned 8 bit data (WAV and RAW only) | -| SF_FORMAT_FLOAT | 0x0006 | 32 bit float data | -| SF_FORMAT_DOUBLE | 0x0007 | 64 bit float data | -| SF_FORMAT_ULAW | 0x0010 | U-Law encoded. | -| SF_FORMAT_ALAW | 0x0011 | A-Law encoded. | -| SF_FORMAT_IMA_ADPCM | 0x0012 | IMA ADPCM. | -| SF_FORMAT_MS_ADPCM | 0x0013 | Microsoft ADPCM. | -| SF_FORMAT_GSM610 | 0x0020 | GSM 6.10 encoding. | -| SF_FORMAT_VOX_ADPCM | 0x0021 | OKI / Dialogix ADPCM | -| SF_FORMAT_NMS_ADPCM_16 | 0x0022 | 16kbs NMS G721-variant encoding. | -| SF_FORMAT_NMS_ADPCM_24 | 0x0023 | 24kbs NMS G721-variant encoding. | -| SF_FORMAT_NMS_ADPCM_32 | 0x0024 | 32kbs NMS G721-variant encoding. | -| SF_FORMAT_G721_32 | 0x0030 | 32kbs G721 ADPCM encoding. | -| SF_FORMAT_G723_24 | 0x0031 | 24kbs G723 ADPCM encoding. | -| SF_FORMAT_G723_40 | 0x0032 | 40kbs G723 ADPCM encoding. | -| SF_FORMAT_DWVW_12 | 0x0040 | 12 bit Delta Width Variable Word encoding. | -| SF_FORMAT_DWVW_16 | 0x0041 | 16 bit Delta Width Variable Word encoding. | -| SF_FORMAT_DWVW_24 | 0x0042 | 24 bit Delta Width Variable Word encoding. | -| SF_FORMAT_DWVW_N | 0x0043 | N bit Delta Width Variable Word encoding. | -| SF_FORMAT_DPCM_8 | 0x0050 | 8 bit differential PCM (XI only) | -| SF_FORMAT_DPCM_16 | 0x0051 | 16 bit differential PCM (XI only) | -| SF_FORMAT_VORBIS | 0x0060 | Xiph Vorbis encoding. | -| SF_FORMAT_OPUS | 0x0064 | Xiph/Skype Opus encoding. | -| SF_FORMAT_ALAC_16 | 0x0070 | Apple Lossless Audio Codec (16 bit). | -| SF_FORMAT_ALAC_20 | 0x0071 | Apple Lossless Audio Codec (20 bit). | -| SF_FORMAT_ALAC_24 | 0x0072 | Apple Lossless Audio Codec (24 bit). | -| SF_FORMAT_ALAC_32 | 0x0073 | Apple Lossless Audio Codec (32 bit). | -| **Endian-ness options.** | -| SF_ENDIAN_FILE | 0x00000000 | Default file endian-ness. | -| SF_ENDIAN_LITTLE | 0x10000000 | Force little endian-ness. | -| SF_ENDIAN_BIG | 0x20000000 | Force big endian-ness. | -| SF_ENDIAN_CPU | 0x30000000 | Force CPU endian-ness. | -| SF_FORMAT_SUBMASK | 0x0000FFFF | | -| SF_FORMAT_TYPEMASK | 0x0FFF0000 | | -| SF_FORMAT_ENDMASK | 0x30000000 | | +| Name | Value | Description | +|:-------------------------|:-----------|:-------------------------------------------| +| **Major formats.** | +| SF_FORMAT_WAV | 0x010000 | Microsoft WAV format (little endian). | +| SF_FORMAT_AIFF | 0x020000 | Apple/SGI AIFF format (big endian). | +| SF_FORMAT_AU | 0x030000 | Sun/NeXT AU format (big endian). | +| SF_FORMAT_RAW | 0x040000 | RAW PCM data. | +| SF_FORMAT_PAF | 0x050000 | Ensoniq PARIS file format. | +| SF_FORMAT_SVX | 0x060000 | Amiga IFF / SVX8 / SV16 format. | +| SF_FORMAT_NIST | 0x070000 | Sphere NIST format. | +| SF_FORMAT_VOC | 0x080000 | VOC files. | +| SF_FORMAT_IRCAM | 0x0A0000 | Berkeley/IRCAM/CARL | +| SF_FORMAT_W64 | 0x0B0000 | Sonic Foundry's 64 bit RIFF/WAV | +| SF_FORMAT_MAT4 | 0x0C0000 | Matlab (tm) V4.2 / GNU Octave 2.0 | +| SF_FORMAT_MAT5 | 0x0D0000 | Matlab (tm) V5.0 / GNU Octave 2.1 | +| SF_FORMAT_PVF | 0x0E0000 | Portable Voice Format | +| SF_FORMAT_XI | 0x0F0000 | Fasttracker 2 Extended Instrument | +| SF_FORMAT_HTK | 0x100000 | HMM Tool Kit format | +| SF_FORMAT_SDS | 0x110000 | Midi Sample Dump Standard | +| SF_FORMAT_AVR | 0x120000 | Audio Visual Research | +| SF_FORMAT_WAVEX | 0x130000 | MS WAVE with WAVEFORMATEX | +| SF_FORMAT_SD2 | 0x160000 | Sound Designer 2 | +| SF_FORMAT_FLAC | 0x170000 | FLAC lossless file format | +| SF_FORMAT_CAF | 0x180000 | Core Audio File format | +| SF_FORMAT_WVE | 0x190000 | Psion WVE format | +| SF_FORMAT_OGG | 0x200000 | Xiph OGG container | +| SF_FORMAT_MPC2K | 0x210000 | Akai MPC 2000 sampler | +| SF_FORMAT_RF64 | 0x220000 | RF64 WAV file | +| SF_FORMAT_MPEG | 0x230000 | MPEG-1/2 audio stream | +| **Subtypes.** | +| SF_FORMAT_PCM_S8 | 0x0001 | Signed 8 bit data | +| SF_FORMAT_PCM_16 | 0x0002 | Signed 16 bit data | +| SF_FORMAT_PCM_24 | 0x0003 | Signed 24 bit data | +| SF_FORMAT_PCM_32 | 0x0004 | Signed 32 bit data | +| SF_FORMAT_PCM_U8 | 0x0005 | Unsigned 8 bit data (WAV and RAW only) | +| SF_FORMAT_FLOAT | 0x0006 | 32 bit float data | +| SF_FORMAT_DOUBLE | 0x0007 | 64 bit float data | +| SF_FORMAT_ULAW | 0x0010 | U-Law encoded. | +| SF_FORMAT_ALAW | 0x0011 | A-Law encoded. | +| SF_FORMAT_IMA_ADPCM | 0x0012 | IMA ADPCM. | +| SF_FORMAT_MS_ADPCM | 0x0013 | Microsoft ADPCM. | +| SF_FORMAT_GSM610 | 0x0020 | GSM 6.10 encoding. | +| SF_FORMAT_VOX_ADPCM | 0x0021 | OKI / Dialogix ADPCM | +| SF_FORMAT_NMS_ADPCM_16 | 0x0022 | 16kbs NMS G721-variant encoding. | +| SF_FORMAT_NMS_ADPCM_24 | 0x0023 | 24kbs NMS G721-variant encoding. | +| SF_FORMAT_NMS_ADPCM_32 | 0x0024 | 32kbs NMS G721-variant encoding. | +| SF_FORMAT_G721_32 | 0x0030 | 32kbs G721 ADPCM encoding. | +| SF_FORMAT_G723_24 | 0x0031 | 24kbs G723 ADPCM encoding. | +| SF_FORMAT_G723_40 | 0x0032 | 40kbs G723 ADPCM encoding. | +| SF_FORMAT_DWVW_12 | 0x0040 | 12 bit Delta Width Variable Word encoding. | +| SF_FORMAT_DWVW_16 | 0x0041 | 16 bit Delta Width Variable Word encoding. | +| SF_FORMAT_DWVW_24 | 0x0042 | 24 bit Delta Width Variable Word encoding. | +| SF_FORMAT_DWVW_N | 0x0043 | N bit Delta Width Variable Word encoding. | +| SF_FORMAT_DPCM_8 | 0x0050 | 8 bit differential PCM (XI only) | +| SF_FORMAT_DPCM_16 | 0x0051 | 16 bit differential PCM (XI only) | +| SF_FORMAT_VORBIS | 0x0060 | Xiph Vorbis encoding. | +| SF_FORMAT_OPUS | 0x0064 | Xiph/Skype Opus encoding. | +| SF_FORMAT_ALAC_16 | 0x0070 | Apple Lossless Audio Codec (16 bit). | +| SF_FORMAT_ALAC_20 | 0x0071 | Apple Lossless Audio Codec (20 bit). | +| SF_FORMAT_ALAC_24 | 0x0072 | Apple Lossless Audio Codec (24 bit). | +| SF_FORMAT_ALAC_32 | 0x0073 | Apple Lossless Audio Codec (32 bit). | +| SF_FORMAT_MPEG_LAYER_I | 0x0080 | MPEG-1 Audio Layer I. | +| SF_FORMAT_MPEG_LAYER_II | 0x0081 | MPEG-1 Audio Layer II. | +| SF_FORMAT_MPEG_LAYER_III | 0x0082 | MPEG-2 Audio Layer III. | +| **Endian-ness options.** | +| SF_ENDIAN_FILE | 0x00000000 | Default file endian-ness. | +| SF_ENDIAN_LITTLE | 0x10000000 | Force little endian-ness. | +| SF_ENDIAN_BIG | 0x20000000 | Force big endian-ness. | +| SF_ENDIAN_CPU | 0x30000000 | Force CPU endian-ness. | +| SF_FORMAT_SUBMASK | 0x0000FFFF | | +| SF_FORMAT_TYPEMASK | 0x0FFF0000 | | +| SF_FORMAT_ENDMASK | 0x30000000 | | Every call to **sf_open**() should be matched with a call to [**sf_close**()](#close) to free up memory allocated during the call to **sf_open**(). diff --git a/docs/command.md b/docs/command.md index 5e6606fb..6d2133ad 100644 --- a/docs/command.md +++ b/docs/command.md @@ -81,6 +81,8 @@ The available commands are as follows: | [SFC_RF64_AUTO_DOWNGRADE](#sfc_rf64_auto_downgrade) | Set auto downgrade from RF64 to WAV. | | [SFC_GET_ORIGINAL_SAMPLERATE](#sfc_get_original_samplerate) | Get original samplerate metadata. | | [SFC_SET_ORIGINAL_SAMPLERATE](#sfc_set_original_samplerate) | Set original samplerate metadata. | +| [SFC_GET_BITRATE_MODE](#sfc_get_bitrate_mode) | Get bitrate mode. +| [SFC_SET_BITRATE_MODE](#sfc_set_bitrate_mode) | Set bitrate mode. --- @@ -1927,3 +1929,63 @@ Returns SF_TRUE on success, SF_FALSE otherwise. On write, can only succeed if no data has been written. On read, if successful, [SFC_GET_CURRENT_SF_INFO](#sfc_get_current_sf_info) should be called to determine the new frames count and samplerate + +## SFC_GET_BITRATE_MODE + +Get bitrate mode. + +The bitrate mode is one of: + +| Name | Value | Description | +|:-------------------------|:------|:------------------| +| SF_BITRATE_MODE_CONSTANT | 800 | Constant bitrate. | +| SF_BITRATE_MODE_AVERAGE | 801 | Average bitrate. | +| SF_BITRATE_MODE_VARIABLE | 802 | Variable bitrate. | + +### Parameters + +sndfile +: A valid SNDFILE* pointer + +cmd +: SFC_GET_BITRATE_MODE + +data +: NULL + +datasize +: anything + +### Return value + +Returns one of `SF_BITRATE_MODE_XXX` on success, `-1` otherwise. + +## SFC_SET_BITRATE_MODE + +Set bitrate mode. + +The bitrate mode is one of: + +| Name | Value | Description | +|:-------------------------|:------|:------------------| +| SF_BITRATE_MODE_CONSTANT | 800 | Constant bitrate. | +| SF_BITRATE_MODE_AVERAGE | 801 | Average bitrate. | +| SF_BITRATE_MODE_VARIABLE | 802 | Variable bitrate. | + +### Parameters + +sndfile +: A valid SNDFILE* pointer + +cmd +: SFC_SET_BITRATE_MODE + +data +: pointer to an integer + +datasize +: sizeof (int) + +### Return value + +Returns `SF_TRUE` on success, `SF_FALSE` otherwise. diff --git a/docs/formats.md b/docs/formats.md index 13bab526..1d96737d 100644 --- a/docs/formats.md +++ b/docs/formats.md @@ -40,11 +40,12 @@ From version 1.0.18, libsndfile also reads and writes From version 1.0.29, libsndfile can read and write [Ogg/Opus](https://opus-codec.org/). +From version 1.1.0, libsndfile can read and write MP3. + Some of the file formats I am also interested in adding are: - Kurzweil K2000 sampler files. - Ogg Speex. -- MPEG Layer 3 (commonly known as MP3) Other file formats may also be added on request. From 89214bdf05ab2d36ff478fee7543a1699ed3844c Mon Sep 17 00:00:00 2001 From: evpobr Date: Fri, 26 Mar 2021 12:32:45 +0500 Subject: [PATCH 085/204] cmake: Fix undefined HAVE_MPEG --- CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f58d8d9..770b8145 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -78,7 +78,7 @@ include(SndFileChecks) cmake_dependent_option (BUILD_REGTEST "Build regtest" ON "SQLITE3_FOUND" OFF) cmake_dependent_option (ENABLE_EXTERNAL_LIBS "Enable FLAC, Vorbis, and Opus codecs" ON "Vorbis_FOUND;FLAC_FOUND;OPUS_FOUND" OFF) -cmake_dependent_option (ENABLE_MPEG "Enable MPEG codecs" ON "LAME_FOUND;MPG123_FOUND" OFF) +cmake_dependent_option (ENABLE_MPEG "Enable MPEG codecs" ON "HAVE_MPEG_LIBS" OFF) cmake_dependent_option (ENABLE_CPU_CLIP "Enable tricky cpu specific clipper" ON "CPU_CLIPS_POSITIVE;CPU_CLIPS_NEGATIVE" OFF) if (NOT ENABLE_CPU_CLIP) set (CPU_CLIPS_POSITIVE FALSE) @@ -105,7 +105,7 @@ set (HAVE_ALSA_ASOUNDLIB_H ${ALSA_FOUND}) set (HAVE_SNDIO_H ${SNDIO_FOUND}) set (ENABLE_EXPERIMENTAL_CODE ${ENABLE_EXPERIMENTAL}) -set (HAVE_MPEG_LIBS ${ENABLE_MPEG}) +set (HAVE_MPEG ${ENABLE_MPEG}) set (HAVE_SPEEX ${ENABLE_EXPERIMENTAL}) add_feature_info (BUILD_SHARED_LIBS BUILD_SHARED_LIBS "build shared libraries") @@ -368,8 +368,8 @@ target_link_libraries (sndfile $<$:FLAC::FLAC> $<$,$,$>:Speex::Speex> $<$:Opus::opus> - $<$:MPG123::libmpg123> - $<$:Lame::Lame> + $<$:MPG123::libmpg123> + $<$:Lame::Lame> ) set_target_properties (sndfile PROPERTIES PUBLIC_HEADER "${sndfile_HDRS}" From b7c22419b6f038ac7766f810f30a1c6e56ccb881 Mon Sep 17 00:00:00 2001 From: evpobr Date: Sat, 27 Mar 2021 11:53:51 +0500 Subject: [PATCH 086/204] cmake: Improve mpg123 handling mpg123 has official imported target name. --- cmake/SndFileChecks.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/SndFileChecks.cmake b/cmake/SndFileChecks.cmake index d776fa8a..f5d5994b 100644 --- a/cmake/SndFileChecks.cmake +++ b/cmake/SndFileChecks.cmake @@ -58,7 +58,7 @@ endif () find_package (Lame) find_package (Mpg123 1.25.10) -if (LAME_FOUND AND MPG123_FOUND) +if (LAME_FOUND AND (TARGET MPG123::libmpg123)) set (HAVE_MPEG_LIBS 1) else () set (HAVE_MPEG_LIBS 0) From 16251b2d110a95ff0af057903b80413ab24dd09b Mon Sep 17 00:00:00 2001 From: evpobr Date: Sat, 27 Mar 2021 11:56:08 +0500 Subject: [PATCH 087/204] mpeg: Fix Windows build Windows MSBuild builds of mpg123 have wrong MPG123_API_VERSION. --- src/mpeg_decode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mpeg_decode.c b/src/mpeg_decode.c index 64efa564..dc56ead3 100644 --- a/src/mpeg_decode.c +++ b/src/mpeg_decode.c @@ -564,7 +564,7 @@ mpeg_decoder_init (SF_PRIVATE *psf) mpg123_param (pmp3d->pmh, MPG123_REMOVE_FLAGS, MPG123_AUTO_RESAMPLE, 1.0) ; mpg123_param (pmp3d->pmh, MPG123_ADD_FLAGS, MPG123_FORCE_FLOAT | MPG123_GAPLESS, 1.0) ; -#if MPG123_API_VERSION >= 45 +#ifdef MPG123_NO_FRANKENSTEIN mpg123_param (pmp3d->pmh, MPG123_ADD_FLAGS, MPG123_NO_FRANKENSTEIN, 1.0) ; #endif From 0d79fbef8bfb0dbe6c57769a917df2cb8274ad77 Mon Sep 17 00:00:00 2001 From: evpobr Date: Sat, 27 Mar 2021 12:27:32 +0500 Subject: [PATCH 088/204] Update README.md --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c12bc1a9..31dd1136 100644 --- a/README.md +++ b/README.md @@ -33,14 +33,15 @@ below. Setting up a build environment for libsndfile on Debian or Ubuntu is as simple as: sudo apt install autoconf autogen automake build-essential libasound2-dev \ - libflac-dev libogg-dev libtool libvorbis-dev libopus-dev pkg-config python + libflac-dev libogg-dev libtool libvorbis-dev libopus-dev libmp3lame-dev \ + libmpg123-dev pkg-config python For other Linux distributions or any of the *BSDs, the setup should be similar although the package install tools and package names may be slightly different. Similarly on Mac OS X, assuming [brew] is already installed: - brew install autoconf autogen automake flac libogg libtool libvorbis opus pkg-config + brew install autoconf autogen automake flac libogg libtool libvorbis opus mpg123 pkg-config Once the build environment has been set up, building and testing libsndfile is as simple as: @@ -134,6 +135,8 @@ You can pass additional options with `/D=` when you run `ON` by default. Setting `BUILD_SHARED_LIBS` to `ON` disables this option. * `ENABLE_EXTERNAL_LIBS` - enable Ogg, Vorbis, FLAC and Opus support. This option is available and set to `ON` if all dependency libraries were found. +* `ENABLE_MPEG` - MP3 support. This option is available and set to `ON` if all + dependency libraries were found. * `ENABLE_CPU_CLIP` - enable tricky cpu specific clipper. Enabled and set to `ON` when CPU clips negative\positive. Don't touch it if you are not sure * `ENABLE_BOW_DOCS` - enable black-on-white documentation theme, `OFF` by @@ -276,6 +279,7 @@ libvorbis, libflac and libopus libraries: vcpkg install libogg:x64-windows-static libvorbis:x64-windows-static libflac:x64-windows-static opus:x64-windows-static libogg:x86-windows-static libvorbis:x86-windows-static libflac:x86-windows-static opus:x86-windows-static + mp3lame:x86-windows-static mpg123:x86-windows-static Then and add this parameter to cmake command line: From 7fde60ac68a52768714c879fde60fe8ae88e29e5 Mon Sep 17 00:00:00 2001 From: evpobr Date: Sat, 27 Mar 2021 12:31:29 +0500 Subject: [PATCH 089/204] cmake: Fix package properties --- CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 770b8145..b859da5e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -149,14 +149,14 @@ set_package_properties (FLAC PROPERTIES DESCRIPTION "Free Lossless Audio Codec Library" PURPOSE "Enables FLAC support" ) -set_package_properties (LAME PROPERTIES - TYPE OPTIONAL +set_package_properties (Lame PROPERTIES + TYPE RECOMMENDED URL "https://lame.sourceforge.io/" DESCRIPTION "High quality MPEG Audio Layer III (MP3) encoder" PURPOSE "Enables MPEG layer III (MP3) writing support" ) set_package_properties (MPG123 PROPERTIES - TYPE OPTIONAL + TYPE RECOMMENDED URL "https://www.mpg123.de/" DESCRIPTION "MPEG Audio Layer I/II/III decoder" PURPOSE "Enables MPEG Audio reading support" From cad58f39cab98528dcc883062f7a5040d26bb9d0 Mon Sep 17 00:00:00 2001 From: evpobr Date: Sun, 28 Mar 2021 13:33:19 +0500 Subject: [PATCH 090/204] [mpeg] Fix MPG123_NO_FRANKENSTEIN mode --- src/mpeg_decode.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/mpeg_decode.c b/src/mpeg_decode.c index dc56ead3..e8b99d5b 100644 --- a/src/mpeg_decode.c +++ b/src/mpeg_decode.c @@ -564,9 +564,17 @@ mpeg_decoder_init (SF_PRIVATE *psf) mpg123_param (pmp3d->pmh, MPG123_REMOVE_FLAGS, MPG123_AUTO_RESAMPLE, 1.0) ; mpg123_param (pmp3d->pmh, MPG123_ADD_FLAGS, MPG123_FORCE_FLOAT | MPG123_GAPLESS, 1.0) ; -#ifdef MPG123_NO_FRANKENSTEIN - mpg123_param (pmp3d->pmh, MPG123_ADD_FLAGS, MPG123_NO_FRANKENSTEIN, 1.0) ; -#endif + /* + ** Enable useful MPG123_NO_FRANKENSTEIN mode if supported (disallow + ** different MPEG streams stiched together). + ** Do not use MPG123_NO_FRANKENSTEIN flag directly, because this mode is too + ** new and must be checked with MPG123_API_VERSION, but on Windows platform + ** libmpg123 built with MS Studio project has broken (unconfigured) + ** MPG123_API_VERSION value. + ** If MPG123_NO_FRANKENSTEIN is not supported, mpg123_param () silently + ** rejects this flag. + */ + mpg123_param (pmp3d->pmh, MPG123_ADD_FLAGS, 0x1000000, 1.0) ; psf->dataoffset = 0 ; /* From d820a2d257b244812abbf1bec034f5a7e1b0bb19 Mon Sep 17 00:00:00 2001 From: evpobr Date: Wed, 31 Mar 2021 13:00:15 +0500 Subject: [PATCH 091/204] [mpeg] Temporarily disable MPG123_NO_FRANKENSTEIN flag Related to #499, microsoft/vcpkg#16910. --- src/mpeg_decode.c | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/mpeg_decode.c b/src/mpeg_decode.c index e8b99d5b..08f76815 100644 --- a/src/mpeg_decode.c +++ b/src/mpeg_decode.c @@ -564,17 +564,6 @@ mpeg_decoder_init (SF_PRIVATE *psf) mpg123_param (pmp3d->pmh, MPG123_REMOVE_FLAGS, MPG123_AUTO_RESAMPLE, 1.0) ; mpg123_param (pmp3d->pmh, MPG123_ADD_FLAGS, MPG123_FORCE_FLOAT | MPG123_GAPLESS, 1.0) ; - /* - ** Enable useful MPG123_NO_FRANKENSTEIN mode if supported (disallow - ** different MPEG streams stiched together). - ** Do not use MPG123_NO_FRANKENSTEIN flag directly, because this mode is too - ** new and must be checked with MPG123_API_VERSION, but on Windows platform - ** libmpg123 built with MS Studio project has broken (unconfigured) - ** MPG123_API_VERSION value. - ** If MPG123_NO_FRANKENSTEIN is not supported, mpg123_param () silently - ** rejects this flag. - */ - mpg123_param (pmp3d->pmh, MPG123_ADD_FLAGS, 0x1000000, 1.0) ; psf->dataoffset = 0 ; /* From fe51e9b0933c3ed109a5df95e11f112e61bf798c Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Wed, 31 Mar 2021 10:15:02 -0700 Subject: [PATCH 092/204] programs: Add MPEG related handling, other misc Ogg handling fixes. - Add MPEG major and minor formats to string printing routines. - Don't warn when adding a broadcast chunk to a MPEG WAV. - sndfile-convert: Infer that ".ogg" suffix means Ogg+Vorbis. This aligns with Xiph.org - sndfile-info: Properly calculate the dB of Vorbis and Opus files. The handling of the allowable sample range of these files may need to be investigated in future. --- programs/common.c | 9 +++++++-- programs/sndfile-convert.c | 5 ++++- programs/sndfile-info.c | 2 ++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/programs/common.c b/programs/common.c index 3c28f2ae..7ff0d41a 100644 --- a/programs/common.c +++ b/programs/common.c @@ -73,7 +73,7 @@ sfe_copy_data_fp (SNDFILE *outfile, SNDFILE *infile, int channels, int normalize { data [k] /= max ; if (!isfinite (data [k])) /* infinite or NaN */ - return 1; + return 1 ; } sf_writef_double (outfile, data, readcount) ; } ; @@ -119,6 +119,7 @@ merge_broadcast_info (SNDFILE * infile, SNDFILE * outfile, int format, const MET { case SF_FORMAT_PCM_16 : case SF_FORMAT_PCM_24 : case SF_FORMAT_PCM_32 : + case SF_FORMAT_MPEG_LAYER_III : break ; default : @@ -339,8 +340,8 @@ static OUTPUT_FORMAT_MAP format_map [] = { "caf", 0, SF_FORMAT_CAF }, { "wve", 0, SF_FORMAT_WVE }, { "prc", 0, SF_FORMAT_WVE }, - { "ogg", 0, SF_FORMAT_OGG }, { "oga", 0, SF_FORMAT_OGG }, + { "ogg", 0, SF_FORMAT_OGG | SF_FORMAT_VORBIS }, { "opus", 0, SF_FORMAT_OGG | SF_FORMAT_OPUS }, { "mpc", 0, SF_FORMAT_MPC2K }, { "rf64", 0, SF_FORMAT_RF64 }, @@ -454,6 +455,7 @@ sfe_container_name (int format) case SF_FORMAT_OGG : return "OGG" ; case SF_FORMAT_MPC2K : return "MPC2K" ; case SF_FORMAT_RF64 : return "RF64" ; + case SF_FORMAT_MPEG : return "MPEG" ; default : break ; } ; @@ -492,6 +494,9 @@ sfe_codec_name (int format) case SF_FORMAT_ALAC_24 : return "24 bit ALAC" ; case SF_FORMAT_ALAC_32 : return "32 bit ALAC" ; case SF_FORMAT_OPUS : return "Opus" ; + case SF_FORMAT_MPEG_LAYER_I : return "MPEG layer 1" ; + case SF_FORMAT_MPEG_LAYER_II : return "MPEG layer 2" ; + case SF_FORMAT_MPEG_LAYER_III : return "MPEG layer 3" ; default : break ; } ; return "unknown" ; diff --git a/programs/sndfile-convert.c b/programs/sndfile-convert.c index 12f28c56..c8484f10 100644 --- a/programs/sndfile-convert.c +++ b/programs/sndfile-convert.c @@ -354,7 +354,10 @@ main (int argc, char * argv []) || (outfileminor == SF_FORMAT_DOUBLE) || (outfileminor == SF_FORMAT_FLOAT) || (infileminor == SF_FORMAT_DOUBLE) || (infileminor == SF_FORMAT_FLOAT) || (infileminor == SF_FORMAT_OPUS) || (outfileminor == SF_FORMAT_OPUS) - || (infileminor == SF_FORMAT_VORBIS) || (outfileminor == SF_FORMAT_VORBIS)) + || (infileminor == SF_FORMAT_VORBIS) || (outfileminor == SF_FORMAT_VORBIS) + || (infileminor == SF_FORMAT_MPEG_LAYER_I) + || (infileminor == SF_FORMAT_MPEG_LAYER_II) + || (infileminor == SF_FORMAT_MPEG_LAYER_III) || (outfileminor == SF_FORMAT_MPEG_LAYER_III)) { if (sfe_copy_data_fp (outfile, infile, sfinfo.channels, normalize) != 0) { printf ("Error : Not able to decode input file %s.\n", infilename) ; return 1 ; diff --git a/programs/sndfile-info.c b/programs/sndfile-info.c index b45f7b95..8ddd9e07 100644 --- a/programs/sndfile-info.c +++ b/programs/sndfile-info.c @@ -167,6 +167,8 @@ calc_decibels (SF_INFO * sfinfo, double max) case SF_FORMAT_FLOAT : case SF_FORMAT_DOUBLE : + case SF_FORMAT_VORBIS : + case SF_FORMAT_OPUS : decibels = max / 1.0 ; break ; From e79f45dac7dafced96e5b471dc919d6459b387b4 Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Wed, 31 Mar 2021 10:25:15 -0700 Subject: [PATCH 093/204] ogg_opus: Fix tabs messing up printf alignment. --- src/ogg_opus.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ogg_opus.c b/src/ogg_opus.c index 9be6e914..dfa446ee 100644 --- a/src/ogg_opus.c +++ b/src/ogg_opus.c @@ -410,10 +410,10 @@ static void opus_print_header (SF_PRIVATE *psf, OpusHeader *h) { psf_log_printf (psf, "Opus Header Metadata\n") ; psf_log_printf (psf, " OggOpus version : %d\n", h->version) ; - psf_log_printf (psf, " Channels : %d\n", h->channels) ; - psf_log_printf (psf, " Preskip : %d samples @48kHz\n", h->preskip) ; + psf_log_printf (psf, " Channels : %d\n", h->channels) ; + psf_log_printf (psf, " Preskip : %d samples @48kHz\n", h->preskip) ; psf_log_printf (psf, " Input Samplerate : %d Hz\n", h->input_samplerate) ; - psf_log_printf (psf, " Gain : %d.%d\n", arith_shift_right (h->gain & 0xF0, 8), h->gain & 0x0F) ; + psf_log_printf (psf, " Gain : %d.%d\n", arith_shift_right (h->gain & 0xF0, 8), h->gain & 0x0F) ; psf_log_printf (psf, " Channel Mapping : ") ; switch (h->channel_mapping) { case 0 : psf_log_printf (psf, "0 (mono or stereo)\n") ; break ; @@ -426,7 +426,7 @@ opus_print_header (SF_PRIVATE *psf, OpusHeader *h) { int i ; psf_log_printf (psf, " streams total : %d\n", h->nb_streams) ; psf_log_printf (psf, " streams coupled : %d\n", h->nb_coupled) ; - psf_log_printf (psf, " stream mapping : [") ; + psf_log_printf (psf, " stream mapping : [") ; for (i = 0 ; i < h->channels - 1 ; i++) psf_log_printf (psf, "%d,", h->stream_map [i]) ; psf_log_printf (psf, "%d]\n", h->stream_map [i]) ; From 9349a566e298ef6d6bed6c275dc1f5502bb2e028 Mon Sep 17 00:00:00 2001 From: evpobr Date: Sun, 4 Apr 2021 19:16:38 +0500 Subject: [PATCH 094/204] [cmake] Separate API and ABI versions --- CMakeLists.txt | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b859da5e..2c72f677 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,15 @@ endif () project(libsndfile VERSION 1.0.31) +# +# ABI version of library. +# + +set (SNDFILE_ABI_VERSION_MAJOR 1) +set (SNDFILE_ABI_VERSION_MINOR 0) +set (SNDFILE_ABI_VERSION_PATCH 31) +set (SNDFILE_ABI_VERSION "${SNDFILE_ABI_VERSION_MAJOR}.${SNDFILE_ABI_VERSION_MINOR}.${SNDFILE_ABI_VERSION_PATCH}") + # # Variables # @@ -402,8 +411,8 @@ if (BUILD_SHARED_LIBS) endif () set_target_properties (sndfile PROPERTIES - SOVERSION ${PROJECT_VERSION_MAJOR} - VERSION ${PROJECT_VERSION} + SOVERSION ${SNDFILE_ABI_VERSION_MAJOR} + VERSION ${SNDFILE_ABI_VERSION} ) # Symbol files generation @@ -425,7 +434,7 @@ if (BUILD_SHARED_LIBS) if (DEFINED SYMBOL_OS) add_custom_command ( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/src/${SYMBOL_FILENAME} - COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/src/create_symbols_file.py ${SYMBOL_OS} ${PROJECT_VERSION} > ${CMAKE_CURRENT_BINARY_DIR}/src/${SYMBOL_FILENAME} + COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/src/create_symbols_file.py ${SYMBOL_OS} ${SNDFILE_ABI_VERSION} > ${CMAKE_CURRENT_BINARY_DIR}/src/${SYMBOL_FILENAME} COMMENT "Generating ${SYMBOL_FILENAME}..." ) From 34bd39b9b513ce6267d7b1b7cd2af56697eb2f24 Mon Sep 17 00:00:00 2001 From: evpobr Date: Sun, 4 Apr 2021 09:07:57 +0500 Subject: [PATCH 095/204] [mpeg] Restore MPG123_NO_FRANKENSTEIN --- src/mpeg_decode.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/mpeg_decode.c b/src/mpeg_decode.c index 08f76815..f9b23493 100644 --- a/src/mpeg_decode.c +++ b/src/mpeg_decode.c @@ -564,6 +564,9 @@ mpeg_decoder_init (SF_PRIVATE *psf) mpg123_param (pmp3d->pmh, MPG123_REMOVE_FLAGS, MPG123_AUTO_RESAMPLE, 1.0) ; mpg123_param (pmp3d->pmh, MPG123_ADD_FLAGS, MPG123_FORCE_FLOAT | MPG123_GAPLESS, 1.0) ; + #if MPG123_API_VERSION >= 45 + mpg123_param (pmp3d->pmh, MPG123_ADD_FLAGS, MPG123_NO_FRANKENSTEIN, 1.0) ; + #endif psf->dataoffset = 0 ; /* From ced91d7b971be6173b604154c39279ce90ad87cc Mon Sep 17 00:00:00 2001 From: yuan Date: Tue, 20 Apr 2021 16:16:32 +0800 Subject: [PATCH 096/204] flac: Fix improper buffer reusing (#732) --- src/flac.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/flac.c b/src/flac.c index 64d0172e..e3320450 100644 --- a/src/flac.c +++ b/src/flac.c @@ -948,7 +948,11 @@ flac_read_loop (SF_PRIVATE *psf, unsigned len) /* Decode some more. */ while (pflac->pos < pflac->len) { if (FLAC__stream_decoder_process_single (pflac->fsd) == 0) + { psf_log_printf (psf, "FLAC__stream_decoder_process_single returned false\n") ; + /* Current frame is busted, so NULL the pointer. */ + pflac->frame = NULL ; break ; + } ; state = FLAC__stream_decoder_get_state (pflac->fsd) ; if (state >= FLAC__STREAM_DECODER_END_OF_STREAM) { psf_log_printf (psf, "FLAC__stream_decoder_get_state returned %s\n", FLAC__StreamDecoderStateString [state]) ; From f1db22760b17626db15d1643ae0b545c3dde4764 Mon Sep 17 00:00:00 2001 From: evpobr Date: Tue, 20 Apr 2021 13:26:37 +0500 Subject: [PATCH 097/204] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a0ec0f7..cb179fa1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -94,5 +94,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 credit to OSS-Fuzz ([issue 26026](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=26026)). * Index out of bounds in `psf_nms_adpcm_decode_block`(), credit to OSS-Fuzz ([issue 25561](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=25561)). +* Heap buffer overflow in `flac_buffer_copy`(), thanks @yuawn, @bobsayshilol. [Unreleased]: https://github.com/libsndfile/libsndfile/compare/1.0.31...HEAD From 949c89122d1081d69260a16a56350624c4db5916 Mon Sep 17 00:00:00 2001 From: bobsayshilol Date: Sun, 25 Apr 2021 19:10:07 +0100 Subject: [PATCH 098/204] ALAC: Enable release check from original code The original code that this was copied from only disabled this check in debug builds as described in the comment, however doing so also allows `channelIndex` to increase without bounds and can lead to writing past the end of the output buffer. https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=27503 --- src/ALAC/alac_decoder.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ALAC/alac_decoder.c b/src/ALAC/alac_decoder.c index 52b7a486..46d33306 100644 --- a/src/ALAC/alac_decoder.c +++ b/src/ALAC/alac_decoder.c @@ -554,7 +554,7 @@ alac_decode (ALAC_DECODER *p, struct BitBuffer * bits, int32_t * sampleBuffer, u } } -#if 0 // ! DEBUG +#if 1 // ! DEBUG // if we've decoded all of our channels, bail (but not in debug b/c we want to know if we're seeing bad bits) // - this also protects us if the config does not match the bitstream or crap data bits follow the audio bits if (channelIndex >= numChannels) From eddac7625d1f62dcc9711ae2d7d34b556e81df6a Mon Sep 17 00:00:00 2001 From: evpobr Date: Wed, 12 May 2021 09:03:44 +0500 Subject: [PATCH 099/204] ci: Fix Ubuntu builds --- .github/workflows/action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index fee3bb07..7e51374b 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -187,7 +187,7 @@ jobs: - name: Install Linux dependencies if: startsWith(matrix.os,'ubuntu') - run: sudo apt-get install -y autogen ninja-build libogg-dev libvorbis-dev libflac-dev libopus-dev libasound2-dev libsqlite3-dev libspeex-dev libmp3lame-dev libmpg123-dev + run: sudo apt-get update -y && sudo apt-get install -y autogen ninja-build libogg-dev libvorbis-dev libflac-dev libopus-dev libasound2-dev libsqlite3-dev libspeex-dev libmp3lame-dev libmpg123-dev - name: Workaround vcpkg yasm-tools for x64-windows if: (startsWith(matrix.os, 'windows') && startsWith(matrix.triplet, 'x64-windows')) From 0cbfc93a3e1a08136a15eb9ced2054402dff6233 Mon Sep 17 00:00:00 2001 From: evpobr Date: Sun, 16 May 2021 14:51:13 +0500 Subject: [PATCH 100/204] cmake: Fix shared library ABI on Apple platforms Now ABI version must be compatible with Autotools builds. Note that this change requires CMake >= 3.17 for building dylib on Apple platforms. --- CHANGELOG.md | 5 +++++ CMakeLists.txt | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb179fa1..ff0fc741 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -83,6 +83,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([issue 27366](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=27366)). * Integer overflow in `nms_adpcm_update`(), credit to OSS-Fuzz ([issue 25522](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=25522)). +* ABI version incompatibility between Autotools and CMake build on Apple + platforms. + + Now ABI must be compatible with Autotools builds. Note that this change + requires CMake >= 3.17 for building dylib on Apple platforms. ### Security diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c72f677..5df890fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,14 @@ set (SNDFILE_ABI_VERSION_MINOR 0) set (SNDFILE_ABI_VERSION_PATCH 31) set (SNDFILE_ABI_VERSION "${SNDFILE_ABI_VERSION_MAJOR}.${SNDFILE_ABI_VERSION_MINOR}.${SNDFILE_ABI_VERSION_PATCH}") +# +# Apple platform current and compatibility versions. +# + +math (EXPR SNDFILE_MACHO_CURRENT_VERSION_MAJOR "${SNDFILE_ABI_VERSION_MAJOR} + ${SNDFILE_ABI_VERSION_MINOR} + 1") +set (SNDFILE_MACHO_CURRENT_VERSION "${SNDFILE_MACHO_CURRENT_VERSION_MAJOR}.${SNDFILE_ABI_VERSION_PATCH}.0") +set (SNDFILE_MACHO_COMPATIBILITY_VERSION "${SNDFILE_MACHO_CURRENT_VERSION_MAJOR}.0.0") + # # Variables # @@ -410,11 +418,23 @@ if (BUILD_SHARED_LIBS) target_sources (sndfile PRIVATE ${PROJECT_BINARY_DIR}/src/version-metadata.rc) endif () + set_target_properties (sndfile PROPERTIES SOVERSION ${SNDFILE_ABI_VERSION_MAJOR} VERSION ${SNDFILE_ABI_VERSION} ) + if (APPLE) + if (NOT (CMAKE_VERSION VERSION_LESS 3.17)) + set_target_properties (sndfile PROPERTIES + MACHO_CURRENT_VERSION ${SNDFILE_MACHO_CURRENT_VERSION} + MACHO_COMPATIBILITY_VERSION ${SNDFILE_MACHO_COMPATIBILITY_VERSION} + ) + else () + message (FATAL_ERROR "Apple platform requires cmake >= 3.17 to build dylib.") + endif () + endif () + # Symbol files generation if (WIN32) From 494a6d633e7ef09a03e7a15d289db3e9b2ef724b Mon Sep 17 00:00:00 2001 From: evpobr Date: Wed, 19 May 2021 14:50:24 +0500 Subject: [PATCH 101/204] autotools: Use ABI version for symbol scripts --- Makefile.am | 10 +++++----- configure.ac | 18 ++++++++++++++++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Makefile.am b/Makefile.am index 61bcc1f8..6cd0a7d6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -135,19 +135,19 @@ src_ALAC_libalac_la_SOURCES = src/ALAC/ALACAudioTypes.h src/ALAC/ALACBitUtilitie SYMBOL_SCRIPT = $(top_srcdir)/src/create_symbols_file.py src/Symbols.gnu-binutils: $(SYMBOL_SCRIPT) - $(PYTHON) $(SYMBOL_SCRIPT) linux $(VERSION) > $(top_srcdir)/$@ + $(PYTHON) $(SYMBOL_SCRIPT) linux $(ABI_VERSION) > $(top_srcdir)/$@ src/Symbols.darwin: $(SYMBOL_SCRIPT) - $(PYTHON) $(SYMBOL_SCRIPT) darwin $(VERSION) > $(top_srcdir)/$@ + $(PYTHON) $(SYMBOL_SCRIPT) darwin $(ABI_VERSION) > $(top_srcdir)/$@ src/libsndfile-1.def: $(SYMBOL_SCRIPT) - $(PYTHON) $(SYMBOL_SCRIPT) win32 $(VERSION) > $(top_srcdir)/$@ + $(PYTHON) $(SYMBOL_SCRIPT) win32 $(ABI_VERSION) > $(top_srcdir)/$@ src/Symbols.os2: $(SYMBOL_SCRIPT) - $(PYTHON) $(SYMBOL_SCRIPT) os2 $(VERSION) > $(top_srcdir)/$@ + $(PYTHON) $(SYMBOL_SCRIPT) os2 $(ABI_VERSION) > $(top_srcdir)/$@ src/Symbols.static: $(SYMBOL_SCRIPT) - $(PYTHON) $(SYMBOL_SCRIPT) static $(VERSION) > $(top_srcdir)/$@ + $(PYTHON) $(SYMBOL_SCRIPT) static $(ABI_VERSION) > $(top_srcdir)/$@ #=============================================================================== # Building windows resource files (if needed). diff --git a/configure.ac b/configure.ac index ef47a2fa..1ddfd98e 100644 --- a/configure.ac +++ b/configure.ac @@ -84,7 +84,20 @@ dnl 5. If any interfaces have been added since the last public release, then i dnl age. dnl 6. If any interfaces have been removed since the last public release, then set age dnl to 0. -SHARED_VERSION_INFO="1:31:0" + +dnl This is libtool version of library, we add it to `--version-info` property. + +m4_define([lt_current], [1]) +m4_define([lt_revision], [31]) +m4_define([lt_age], [0]) + +dnl This is ABI version for linker scripts, CMake uses the same format for +dnl VERSION property of shared library. +dnl The formula is: c:r:a -> c-a:a:r + +m4_define([abi_version_major], [m4_eval(lt_current - lt_age)]) +m4_define([abi_version_minor], [lt_age]) +m4_define([abi_version_patch], [lt_revision]) dnl ------------------------------------------------------------------------------------ @@ -706,7 +719,8 @@ AC_SUBST(HTML_BGCOLOUR) AC_SUBST(HTML_FGCOLOUR) AC_SUBST(SHLIB_VERSION_ARG) -AC_SUBST(SHARED_VERSION_INFO) +AC_SUBST([SHARED_VERSION_INFO], [lt_current:lt_revision:lt_age]) +AC_SUBST([ABI_VERSION], [abi_version_major.abi_version_minor.abi_version_patch]) AC_SUBST(CLEAN_VERSION) AC_SUBST(VERSION_MAJOR) AC_SUBST(GEN_TOOL) From 383e6e90e4ddf1e583fedf0d3c2f2c0dfbb8b81d Mon Sep 17 00:00:00 2001 From: evpobr Date: Tue, 25 May 2021 12:41:25 +0500 Subject: [PATCH 102/204] cmake: Read ABI version from `configure.ac` --- CMakeLists.txt | 39 ++++++++++++++----------- Makefile.am | 3 +- cmake/SetupABIVersions.cmake | 56 ++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 18 deletions(-) create mode 100644 cmake/SetupABIVersions.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 5df890fb..b31da37d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,23 +17,6 @@ endif () project(libsndfile VERSION 1.0.31) -# -# ABI version of library. -# - -set (SNDFILE_ABI_VERSION_MAJOR 1) -set (SNDFILE_ABI_VERSION_MINOR 0) -set (SNDFILE_ABI_VERSION_PATCH 31) -set (SNDFILE_ABI_VERSION "${SNDFILE_ABI_VERSION_MAJOR}.${SNDFILE_ABI_VERSION_MINOR}.${SNDFILE_ABI_VERSION_PATCH}") - -# -# Apple platform current and compatibility versions. -# - -math (EXPR SNDFILE_MACHO_CURRENT_VERSION_MAJOR "${SNDFILE_ABI_VERSION_MAJOR} + ${SNDFILE_ABI_VERSION_MINOR} + 1") -set (SNDFILE_MACHO_CURRENT_VERSION "${SNDFILE_MACHO_CURRENT_VERSION_MAJOR}.${SNDFILE_ABI_VERSION_PATCH}.0") -set (SNDFILE_MACHO_COMPATIBILITY_VERSION "${SNDFILE_MACHO_CURRENT_VERSION_MAJOR}.0.0") - # # Variables # @@ -406,6 +389,28 @@ endif () if (BUILD_SHARED_LIBS) + # + # ABI version of library. + # + + # + # Read libtool version from `configure.ac` and set libsndfile ABI version: + # + # SNDFILE_ABI_VERSION_MAJOR + # SNDFILE_ABI_VERSION_MINOR + # SNDFILE_ABI_VERSION_PATCH + # SNDFILE_ABI_VERSION + # + # and Mach-O current and compatibility versions: + # + # SNDFILE_MACHO_CURRENT_VERSION + # SNDFILE_MACHO_COMPATIBILITY_VERSION + # + + include (SetupABIVersions) + + setup_abi_versions() + if (WIN32) set (VERSION_MAJOR ${CPACK_PACKAGE_VERSION_MAJOR}) set (GEN_TOOL cmake) diff --git a/Makefile.am b/Makefile.am index 6cd0a7d6..b1f77571 100644 --- a/Makefile.am +++ b/Makefile.am @@ -23,7 +23,8 @@ cmake_files = cmake/ClipMode.cmake cmake/FindFLAC.cmake \ cmake/SndFileChecks.cmake cmake/TestInline.cmake \ cmake/TestLargeFiles.cmake cmake/TestInline.c.in \ cmake/FindOpus.cmake cmake/SndFileConfig.cmake.in \ - cmake/CheckCPUArch.cmake cmake/CheckCPUArch.c.in + cmake/CheckCPUArch.cmake cmake/CheckCPUArch.c.in \ + cmake/SetupABIVersions.cmake pkgconfig_DATA = sndfile.pc diff --git a/cmake/SetupABIVersions.cmake b/cmake/SetupABIVersions.cmake new file mode 100644 index 00000000..331a2bd9 --- /dev/null +++ b/cmake/SetupABIVersions.cmake @@ -0,0 +1,56 @@ +# Inspiration: https://github.com/ros2-dotnet/Fast-RTPS + +macro (SETUP_ABI_VERSIONS) + + file (STRINGS ${PROJECT_SOURCE_DIR}/configure.ac CONFIGURE_AC_CONTENT) + file (STRINGS + configure.ac + SNDFILE_LT_CURRENT_TMP + REGEX "^m4_define\\(\\[?lt_current\\]?, *\\[?[0-9]+\\]?\\)" + ) + string (REGEX REPLACE "m4_define\\(\\[?lt_current\\]?, *\\[?([0-9]+)\\]?\\)" + "\\1" + SNDFILE_LT_CURRENT + ${SNDFILE_LT_CURRENT_TMP} + ) + + file (STRINGS + configure.ac + SNDFILE_LT_REVISION_TMP + REGEX "^m4_define\\(\\[?lt_revision\\]?, *\\[?[0-9]+\\]?\\)" + ) + string (REGEX REPLACE "m4_define\\(\\[?lt_revision\\]?, *\\[?([0-9]+)\\]?\\)" + "\\1" + SNDFILE_LT_REVISION + ${SNDFILE_LT_REVISION_TMP} + ) + + file (STRINGS + configure.ac + SNDFILE_LT_AGE_TMP + REGEX "^m4_define\\(\\[?lt_age\\]?, *\\[?[0-9]+\\]?\\)" + ) + string (REGEX REPLACE "m4_define\\(\\[?lt_age\\]?, *\\[?([0-9]+)\\]?\\)" + "\\1" + SNDFILE_LT_AGE + ${SNDFILE_LT_AGE_TMP} + ) + + # + # Calculate CMake compatible ABI version from libtool version. + # + + math (EXPR SNDFILE_ABI_VERSION_MAJOR "${SNDFILE_LT_CURRENT} - ${SNDFILE_LT_AGE}") + set (SNDFILE_ABI_VERSION_MINOR ${SNDFILE_LT_AGE}) + set (SNDFILE_ABI_VERSION_PATCH ${SNDFILE_LT_REVISION}) + set (SNDFILE_ABI_VERSION "${SNDFILE_ABI_VERSION_MAJOR}.${SNDFILE_ABI_VERSION_MINOR}.${SNDFILE_ABI_VERSION_PATCH}") + + # + # Apple platform current and compatibility versions. + # + + math (EXPR SNDFILE_MACHO_CURRENT_VERSION_MAJOR "${SNDFILE_ABI_VERSION_MAJOR} + ${SNDFILE_ABI_VERSION_MINOR} + 1") + set (SNDFILE_MACHO_CURRENT_VERSION "${SNDFILE_MACHO_CURRENT_VERSION_MAJOR}.${SNDFILE_ABI_VERSION_PATCH}.0") + set (SNDFILE_MACHO_COMPATIBILITY_VERSION "${SNDFILE_MACHO_CURRENT_VERSION_MAJOR}.0.0") + +endmacro (SETUP_ABI_VERSIONS) From 85aea74e578d6f33c733ebdb775299bc39df2a02 Mon Sep 17 00:00:00 2001 From: evpobr Date: Tue, 20 Apr 2021 14:22:00 +0500 Subject: [PATCH 103/204] Update sndfile.pc for MPEG support --- CMakeLists.txt | 17 +++++++++++++++++ configure.ac | 7 +++++-- sndfile.pc.in | 3 ++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b31da37d..18468df7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -210,6 +210,23 @@ if (INSTALL_PKGCONFIG_MODULE) set (EXTERNAL_XIPH_REQUIRE "${EXTERNAL_XIPH_REQUIRE} speex") endif () endif () + if (ENABLE_MPEG) + set (EXTERNAL_MPEG_REQUIRE "libmpg123") + get_filename_component(LAME_WE ${LAME_LIBRARY} NAME_WE) + if (LAME_HIP_LIBRARY) + get_filename_component(LAME_HIP_WE ${LAME_HIP_LIBRARY} NAME_WE) + endif () + if (CMAKE_IMPORT_LIBRARY_PREFIX) + string (REGEX REPLACE "^${CMAKE_IMPORT_LIBRARY_PREFIX}" "" LAME_WE_NO_PREFIX ${LAME_WE}) + if (LAME_HIP_LIBRARY) + string (REGEX REPLACE "^${CMAKE_IMPORT_LIBRARY_PREFIX}" "" LAME_HIP_WE_NO_PREFIX ${LAME_HIP_WE}) + endif () + endif () + set (EXTERNAL_MPEG_LIBS "-l${LAME_WE_NO_PREFIX}") + if (LAME_HIP_LIBRARY) + set (EXTERNAL_MPEG_LIBS "${MPEG_LIBS} -l${LAME_HIP_WE}") + endif () + endif () configure_file (sndfile.pc.in sndfile.pc @ONLY) diff --git a/configure.ac b/configure.ac index 1ddfd98e..70de6442 100644 --- a/configure.ac +++ b/configure.ac @@ -410,8 +410,8 @@ dnl Check for MPEG libraris liblame ac_cv_lame="no" ac_cv_mpg123="no" HAVE_MPEG=0 -MPEG_CFLAGS="" -MPEG_LIBS="" +EXTERNAL_MPEG_REQUIRE="" +EXTERNAL_MPEG_LIBS="" AS_IF([test -n "$PKG_CONFIG"], [ AS_IF([test "x$enable_mpeg" = "xno"], [ @@ -436,6 +436,8 @@ AS_IF([test -n "$PKG_CONFIG"], [ AS_IF([test "x$ac_cv_lame$ac_cv_mpg123" = "xyesyes"], [ enable_mpeg="yes" HAVE_MPEG=1 + EXTERNAL_MPEG_REQUIRE="libmpg123" + EXTERNAL_MPEG_LIBS="-lmp3lame" MPEG_CFLAGS="$MPG123_CFLAGS" MPEG_LIBS="$MPG123_LIBS" ], [ @@ -736,6 +738,7 @@ AC_SUBST(SNDIO_LIBS) AC_SUBST(EXTERNAL_XIPH_CFLAGS) AC_SUBST(EXTERNAL_XIPH_LIBS) AC_SUBST(EXTERNAL_XIPH_REQUIRE) +AC_SUBST(EXTERNAL_MPEG_REQUIRE) AC_SUBST(MPG123_CFLAGS) AC_SUBST(MPG123_LIBS) AC_SUBST(MPEG_CFLAGS) diff --git a/sndfile.pc.in b/sndfile.pc.in index 961cf93c..6b884537 100644 --- a/sndfile.pc.in +++ b/sndfile.pc.in @@ -6,7 +6,8 @@ includedir=@includedir@ Name: sndfile Description: A library for reading and writing audio files Requires: -Requires.private: @EXTERNAL_XIPH_REQUIRE@ +Requires.private: @EXTERNAL_XIPH_REQUIRE@ @EXTERNAL_MPEG_REQUIRE@ Version: @VERSION@ Libs: -L${libdir} -lsndfile +Libs.private: @EXTERNAL_MPEG_LIBS@ Cflags: -I${includedir} From 8cb0169f5870c24006d69471df8e981fbff9fed2 Mon Sep 17 00:00:00 2001 From: evpobr Date: Tue, 25 May 2021 17:10:29 +0500 Subject: [PATCH 104/204] cmake: Support pre-release versions --- CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 18468df7..0a4ab731 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1557,5 +1557,8 @@ if (BUILD_TESTING) endif () if (ENABLE_CPACK) + if ((NOT CPACK_PACKAGE_VERSION) AND CPACK_PACKAGE_VERSION_STAGE) + set(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_FULL}") + endif () include (CPack) endif () From 5300a9e18c3fff781cbbf66b1acdbdc045939937 Mon Sep 17 00:00:00 2001 From: evpobr Date: Tue, 1 Jun 2021 10:25:51 +0500 Subject: [PATCH 105/204] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff0fc741..a481f331 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -100,5 +100,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Index out of bounds in `psf_nms_adpcm_decode_block`(), credit to OSS-Fuzz ([issue 25561](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=25561)). * Heap buffer overflow in `flac_buffer_copy`(), thanks @yuawn, @bobsayshilol. +* Heap buffer overflow in `copyPredictorTo24`(), thanks @bobsayshilol, + credit to OSS-Fuzz ([issue 27503](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=27503)). + [Unreleased]: https://github.com/libsndfile/libsndfile/compare/1.0.31...HEAD From 62b7fb3b35ef8a0519af6af90d38981366d4665c Mon Sep 17 00:00:00 2001 From: evpobr Date: Sun, 6 Jun 2021 15:57:39 +0500 Subject: [PATCH 106/204] Define ALWAYS_INLINE to __forceinline for MSVC --- src/ALAC/ag_dec.c | 2 ++ src/ALAC/ag_enc.c | 2 ++ src/ALAC/dp_dec.c | 2 ++ src/ALAC/dp_enc.c | 2 ++ src/ALAC/shift.h | 2 ++ src/G72x/g72x_priv.h | 2 ++ src/GSM610/gsm610_priv.h | 2 ++ 7 files changed, 14 insertions(+) diff --git a/src/ALAC/ag_dec.c b/src/ALAC/ag_dec.c index e7e4c62a..37671d24 100644 --- a/src/ALAC/ag_dec.c +++ b/src/ALAC/ag_dec.c @@ -42,6 +42,8 @@ #if __GNUC__ #define ALWAYS_INLINE __attribute__ ((always_inline)) +#elif defined _MSC_VER +#define ALWAYS_INLINE __forceinline #else #define ALWAYS_INLINE #endif diff --git a/src/ALAC/ag_enc.c b/src/ALAC/ag_enc.c index 2b1df194..4737b96e 100644 --- a/src/ALAC/ag_enc.c +++ b/src/ALAC/ag_enc.c @@ -44,6 +44,8 @@ #if __GNUC__ #define ALWAYS_INLINE __attribute__ ((always_inline)) +#elif defined _MSC_VER +#define ALWAYS_INLINE __forceinline #else #define ALWAYS_INLINE #endif diff --git a/src/ALAC/dp_dec.c b/src/ALAC/dp_dec.c index 4f5bc1d6..0d1c10d5 100644 --- a/src/ALAC/dp_dec.c +++ b/src/ALAC/dp_dec.c @@ -34,6 +34,8 @@ #if __GNUC__ #define ALWAYS_INLINE __attribute__ ((always_inline)) +#elif defined _MSC_VER +#define ALWAYS_INLINE __forceinline #else #define ALWAYS_INLINE #endif diff --git a/src/ALAC/dp_enc.c b/src/ALAC/dp_enc.c index ad098a4f..01db8d40 100644 --- a/src/ALAC/dp_enc.c +++ b/src/ALAC/dp_enc.c @@ -33,6 +33,8 @@ #if __GNUC__ #define ALWAYS_INLINE __attribute__ ((always_inline)) +#elif defined _MSC_VER +#define ALWAYS_INLINE __forceinline #else #define ALWAYS_INLINE #endif diff --git a/src/ALAC/shift.h b/src/ALAC/shift.h index 7c2d6f69..bce404d2 100644 --- a/src/ALAC/shift.h +++ b/src/ALAC/shift.h @@ -18,6 +18,8 @@ #if __GNUC__ #define ALWAYS_INLINE __attribute__ ((always_inline)) +#elif defined _MSC_VER +#define ALWAYS_INLINE __forceinline #else #define ALWAYS_INLINE #endif diff --git a/src/G72x/g72x_priv.h b/src/G72x/g72x_priv.h index e418fb75..6a194ec3 100644 --- a/src/G72x/g72x_priv.h +++ b/src/G72x/g72x_priv.h @@ -108,6 +108,8 @@ void private_init_state (G72x_STATE *state_ptr) ; #if __GNUC__ #define ALWAYS_INLINE __attribute__ ((always_inline)) +#elif defined _MSC_VER +#define ALWAYS_INLINE __forceinline #else #define ALWAYS_INLINE #endif diff --git a/src/GSM610/gsm610_priv.h b/src/GSM610/gsm610_priv.h index dc95e3a0..7ffaddc1 100644 --- a/src/GSM610/gsm610_priv.h +++ b/src/GSM610/gsm610_priv.h @@ -294,6 +294,8 @@ extern int16_t gsm_FAC [8] ; #if __GNUC__ #define ALWAYS_INLINE __attribute__ ((always_inline)) +#elif defined _MSC_VER +#define ALWAYS_INLINE __forceinline #else #define ALWAYS_INLINE #endif From dfdad63ff2c792299d3553f04153afe7f695033a Mon Sep 17 00:00:00 2001 From: David Seifert Date: Sun, 13 Jun 2021 15:33:10 +0200 Subject: [PATCH 107/204] Release 1.1.0beta1 --- CMakeLists.txt | 4 ++-- configure.ac | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0a4ab731..48ba905e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,7 @@ if (POLICY CMP0091) endif () endif () -project(libsndfile VERSION 1.0.31) +project(libsndfile VERSION 1.1.0) # # Variables @@ -29,7 +29,7 @@ set (PACKAGE_NAME ${PROJECT_NAME}) set (CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR}) set (CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR}) set (CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH}) -set (CPACK_PACKAGE_VERSION_STAGE "") +set (CPACK_PACKAGE_VERSION_STAGE "beta1") set (CPACK_PACKAGE_VERSION_FULL "${PROJECT_VERSION}${CPACK_PACKAGE_VERSION_STAGE}") # diff --git a/configure.ac b/configure.ac index 70de6442..74a35352 100644 --- a/configure.ac +++ b/configure.ac @@ -3,7 +3,7 @@ dnl Copyright (C) 1999-2021 Erik de Castro Lopo . dnl Require autoconf version >= 2.69 AC_PREREQ([2.69]) -AC_INIT([libsndfile],[1.0.31],[sndfile@mega-nerd.com], +AC_INIT([libsndfile],[1.1.0beta1],[sndfile@mega-nerd.com], [libsndfile],[http://libsndfile.github.io/libsndfile/]) dnl Check whether we want to set defaults for CFLAGS, CXXFLAGS, CPPFLAGS and LDFLAGS @@ -88,7 +88,7 @@ dnl to 0. dnl This is libtool version of library, we add it to `--version-info` property. m4_define([lt_current], [1]) -m4_define([lt_revision], [31]) +m4_define([lt_revision], [32]) m4_define([lt_age], [0]) dnl This is ABI version for linker scripts, CMake uses the same format for From 01628f48b9aacea1d1939935325cd8ff87c0eeaf Mon Sep 17 00:00:00 2001 From: Ioseb Laghidze Date: Tue, 15 Jun 2021 20:29:22 +0400 Subject: [PATCH 108/204] Add cmake build instructions for Android (#751) Co-authored-by: I.Laghidze Co-authored-by: evpobr --- Building-for-Android.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Building-for-Android.md b/Building-for-Android.md index 72b302d4..c664281b 100644 --- a/Building-for-Android.md +++ b/Building-for-Android.md @@ -15,3 +15,32 @@ by setting them before the script is run. Since I (erikd), do almost zero Android development, I am happy accept patches for this documentation and script to improve its utility for real Android developers. + +--- + +## Using CMake + +(Tested on Linux) + +For convenience, export the following variables: + +``` +export ANDROID_ABI=arm64-v8a +export ANDROID_PLATFORM_API_LEVEL=29 +export NDK_ROOT=/path/to/android/ndk +``` + +Set `ANDROID_ABI`, `ANDROID_PLATFORM_API_LEVEL` according to your target system. Now cd into the libsndfile root directory, and run + +``` +cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=$NDK_ROOT/build/cmake/android.toolchain.cmake -DANDROID_ABI=$ANDROID_ABI -DANDROID_PLATFORM=$ANDROID_PLATFORM_API_LEVEL +``` + +cd into `build` and run make + +``` +cd build +make [-j ] +``` + +This will build libsndfile for android. From e46c4b9eb270afb1c626b239a7c71cec563a5101 Mon Sep 17 00:00:00 2001 From: evpobr Date: Wed, 16 Jun 2021 12:20:41 +0500 Subject: [PATCH 109/204] mpeg: Fix int overflow, closes #756 --- src/mpeg_l3_encode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mpeg_l3_encode.c b/src/mpeg_l3_encode.c index 5a9424d4..9b9186aa 100644 --- a/src/mpeg_l3_encode.c +++ b/src/mpeg_l3_encode.c @@ -42,7 +42,7 @@ typedef struct size_t block_len ; int frame_samples ; double compression ; - int initialized : 1 ; + int initialized ; } MPEG_L3_ENC_PRIVATE ; From d948e9b13a3dd0f1569914abbaf3f473173d938e Mon Sep 17 00:00:00 2001 From: evpobr Date: Wed, 16 Jun 2021 12:22:38 +0500 Subject: [PATCH 110/204] mpeg: Fix pedantic warning ISO C does not allow extra ';' outside of a function [-Wpedantic]. --- src/mpeg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mpeg.c b/src/mpeg.c index 31a19e03..fe13e917 100644 --- a/src/mpeg.c +++ b/src/mpeg.c @@ -40,7 +40,7 @@ mpeg_write_header (SF_PRIVATE *psf, int UNUSED (calc_length)) return 0 ; return mpeg_l3_encoder_write_id3tag (psf) ; -} ; +} static int mpeg_command (SF_PRIVATE *psf, int command, void *data, int datasize) From 682eee35f362793affe13a09d8989f4cd2aff32e Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Wed, 16 Jun 2021 13:49:04 -0700 Subject: [PATCH 111/204] configure.ac: Handle 'beta' suffix in WIN_RC_VERSION generation --- configure.ac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 74a35352..93f56439 100644 --- a/configure.ac +++ b/configure.ac @@ -702,7 +702,7 @@ AS_IF([test "$HOST_TRIPLET" = "x86_64-w64-mingw32"], [ OS_SPECIFIC_LINKS=" -static-libgcc $OS_SPECIFIC_LINKS" ]) -WIN_RC_VERSION=`echo $PACKAGE_VERSION | $SED -e "s/p.*//" -e "s/\./,/g"` +WIN_RC_VERSION=`echo $PACKAGE_VERSION | $SED -e "s/pre.*//" -e "s/beta.*//" -e "s/\./,/g"` AS_IF([test "x$enable_static" = "xno"], [ From 16b089a8a2386edd0db332d3e16e2bac046fdaa1 Mon Sep 17 00:00:00 2001 From: evpobr Date: Fri, 18 Jun 2021 11:23:02 +0500 Subject: [PATCH 112/204] m4/add_fortify_source.m4: Update to fix MinGW bug See https://github.com/msys2/MINGW-packages/issues/5803 for details. --- CHANGELOG.md | 4 +++ m4/ax_add_fortify_source.m4 | 55 +++++++++++++++++++++++++++---------- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a481f331..5b21205a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -89,6 +89,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 Now ABI must be compatible with Autotools builds. Note that this change requires CMake >= 3.17 for building dylib on Apple platforms. +* Fix build with Autotools + MinGW toolchain on Windows platform. + + See https://github.com/msys2/MINGW-packages/issues/5803 for details. + ### Security * Heap buffer overflow in `wavlike_ima_decode_block`(), thanks @bobsayshilol, diff --git a/m4/ax_add_fortify_source.m4 b/m4/ax_add_fortify_source.m4 index d443814b..7e153127 100644 --- a/m4/ax_add_fortify_source.m4 +++ b/m4/ax_add_fortify_source.m4 @@ -1,5 +1,5 @@ # =========================================================================== -# http://www.gnu.org/software/autoconf-archive/ax_add_fortify_source.html +# https://www.gnu.org/software/autoconf-archive/ax_add_fortify_source.html # =========================================================================== # # SYNOPSIS @@ -9,9 +9,9 @@ # DESCRIPTION # # Check whether -D_FORTIFY_SOURCE=2 can be added to CPPFLAGS without macro -# redefinition warnings. Some distributions (such as Gentoo Linux) enable -# _FORTIFY_SOURCE globally in their compilers, leading to unnecessary -# warnings in the form of +# redefinition warnings, other cpp warnings or linker. Some distributions +# (such as Gentoo Linux) enable _FORTIFY_SOURCE globally in their +# compilers, leading to unnecessary warnings in the form of # # :0:0: error: "_FORTIFY_SOURCE" redefined [-Werror] # : note: this is the location of the previous definition @@ -20,34 +20,61 @@ # _FORTIFY_SOURCE is already defined, and if not, adds -D_FORTIFY_SOURCE=2 # to CPPFLAGS. # +# Newer mingw-w64 msys2 package comes with a bug in +# headers-git-7.0.0.5546.d200317d-1. It broke -D_FORTIFY_SOURCE support, +# and would need -lssp or -fstack-protector. See +# https://github.com/msys2/MINGW-packages/issues/5803. Try to actually +# link it. +# # LICENSE # # Copyright (c) 2017 David Seifert +# Copyright (c) 2019 Reini Urban # # Copying and distribution of this file, with or without modification, are # permitted in any medium without royalty provided the copyright notice # and this notice are preserved. This file is offered as-is, without any # warranty. -#serial 1 +#serial 4 AC_DEFUN([AX_ADD_FORTIFY_SOURCE],[ + ac_save_cflags=$CFLAGS + ac_cwerror_flag=yes + AX_CHECK_COMPILE_FLAG([-Werror],[CFLAGS="$CFLAGS -Werror"]) AC_MSG_CHECKING([whether to add -D_FORTIFY_SOURCE=2 to CPPFLAGS]) AC_LINK_IFELSE([ - AC_LANG_SOURCE( + AC_LANG_PROGRAM([], [[ - int main() { #ifndef _FORTIFY_SOURCE return 0; #else this_is_an_error; #endif - } ]] - )], [ - AC_MSG_RESULT([yes]) - CPPFLAGS="$CPPFLAGS -D_FORTIFY_SOURCE=2" - ], [ - AC_MSG_RESULT([no]) - ]) + )], + AC_LINK_IFELSE([ + AC_LANG_SOURCE([[ + #define _FORTIFY_SOURCE 2 + #include + int main() { + char *s = " "; + strcpy(s, "x"); + return strlen(s)-1; + } + ]] + )], + [ + AC_MSG_RESULT([yes]) + CFLAGS=$ac_save_cflags + CPPFLAGS="$CPPFLAGS -D_FORTIFY_SOURCE=2" + ], [ + AC_MSG_RESULT([no]) + CFLAGS=$ac_save_cflags + ], + ), + [ + AC_MSG_RESULT([no]) + CFLAGS=$ac_save_cflags + ]) ]) From e31880158835b48516ed31472c5b55f94eae93d6 Mon Sep 17 00:00:00 2001 From: Daniel G Date: Fri, 2 Jul 2021 10:59:07 +0200 Subject: [PATCH 113/204] Fix the typo (#763) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 31dd1136..ca52cc89 100644 --- a/README.md +++ b/README.md @@ -235,7 +235,7 @@ You can find related option in Visual Studio project properties: C/C++ -> Code Generation -> Runtime Library -Dynamic version of system CRT library is defaut and it means that end user needs +Dynamic version of system CRT library is default and it means that end user needs to have the same runtime library installed on his system. Most likely it is so, but if it is not, the user will see this error message using libsndfile DLL: From ccfd37ab922ee437122c3a16bf16be9c85498680 Mon Sep 17 00:00:00 2001 From: evpobr Date: Mon, 5 Jul 2021 09:22:11 +0500 Subject: [PATCH 114/204] Use forward for loops (#760) * Use forward for loops * mpeg: Rewrite decode read conversion copy loops Rewrite the mpeg_dec_read_X() conversion copy loops for clarity and bug fixes. - Use two buffers rather than in-place expansion for float -> double conversion of mpeg_dec_read_double(). The previous method required a less-compiler optimizer friendly backwards looping. This also broke strict-aliasing rules as well, (although this could be worked around with a union pointer.) - Add optional clipping support for float->short and float->int reads. - Don't bother deref'ing and passing the otherwise unused MPEG_DEC_PRIVATE pointer, instead deref it as needed in mpeg_dec_decode(). Co-authored-by: Arthur Taylor --- src/alaw.c | 56 ++--- src/common.c | 56 ++--- src/double64.c | 52 ++--- src/flac.c | 108 +++++----- src/float32.c | 52 ++--- src/mpeg_decode.c | 101 ++++----- src/pcm.c | 529 ++++++++++++++++++++++------------------------ src/sfendian.h | 32 +-- src/ulaw.c | 56 ++--- 9 files changed, 500 insertions(+), 542 deletions(-) diff --git a/src/alaw.c b/src/alaw.c index 399135bc..4be642e5 100644 --- a/src/alaw.c +++ b/src/alaw.c @@ -292,69 +292,69 @@ unsigned char alaw_encode [2048 + 1] = static inline void alaw2s_array (unsigned char *buffer, int count, short *ptr) -{ while (--count >= 0) - ptr [count] = alaw_decode [(int) buffer [count]] ; +{ for (int i = 0 ; i < count ; i++) + ptr [i] = alaw_decode [(int) buffer [i]] ; } /* alaw2s_array */ static inline void alaw2i_array (unsigned char *buffer, int count, int *ptr) -{ while (--count >= 0) - ptr [count] = ((uint32_t) alaw_decode [(int) buffer [count]]) << 16 ; +{ for (int i = 0 ; i < count ; i++) + ptr [i] = ((uint32_t) alaw_decode [(int) buffer [i]]) << 16 ; } /* alaw2i_array */ static inline void alaw2f_array (unsigned char *buffer, int count, float *ptr, float normfact) -{ while (--count >= 0) - ptr [count] = normfact * alaw_decode [(int) buffer [count]] ; +{ for (int i = 0 ; i < count ; i++) + ptr [i] = normfact * alaw_decode [(int) buffer [i]] ; } /* alaw2f_array */ static inline void alaw2d_array (unsigned char *buffer, int count, double *ptr, double normfact) -{ while (--count >= 0) - ptr [count] = normfact * alaw_decode [(int) buffer [count]] ; +{ for (int i = 0 ; i < count ; i++) + ptr [i] = normfact * alaw_decode [(int) buffer [i]] ; } /* alaw2d_array */ static inline void s2alaw_array (const short *ptr, int count, unsigned char *buffer) -{ while (--count >= 0) - { if (ptr [count] >= 0) - buffer [count] = alaw_encode [ptr [count] / 16] ; +{ for (int i = 0 ; i < count ; i++) + { if (ptr [i] >= 0) + buffer [i] = alaw_encode [ptr [i] / 16] ; else - buffer [count] = 0x7F & alaw_encode [ptr [count] / -16] ; + buffer [i] = 0x7F & alaw_encode [ptr [i] / -16] ; } ; } /* s2alaw_array */ static inline void i2alaw_array (const int *ptr, int count, unsigned char *buffer) -{ while (--count >= 0) - { if (ptr [count] == INT_MIN) - buffer [count] = alaw_encode [INT_MAX >> (16 + 4)] ; - else if (ptr [count] >= 0) - buffer [count] = alaw_encode [ptr [count] >> (16 + 4)] ; +{ for (int i = 0 ; i < count ; i++) + { if (ptr [i] == INT_MIN) + buffer [i] = alaw_encode [INT_MAX >> (16 + 4)] ; + else if (ptr [i] >= 0) + buffer [i] = alaw_encode [ptr [i] >> (16 + 4)] ; else - buffer [count] = 0x7F & alaw_encode [- ptr [count] >> (16 + 4)] ; + buffer [i] = 0x7F & alaw_encode [- ptr [i] >> (16 + 4)] ; } ; } /* i2alaw_array */ static inline void f2alaw_array (const float *ptr, int count, unsigned char *buffer, float normfact) -{ while (--count >= 0) - { if (ptr [count] >= 0) - buffer [count] = alaw_encode [psf_lrintf (normfact * ptr [count])] ; +{ for (int i = 0 ; i < count ; i++) + { if (ptr [i] >= 0) + buffer [i] = alaw_encode [psf_lrintf (normfact * ptr [i])] ; else - buffer [count] = 0x7F & alaw_encode [- psf_lrintf (normfact * ptr [count])] ; + buffer [i] = 0x7F & alaw_encode [- psf_lrintf (normfact * ptr [i])] ; } ; } /* f2alaw_array */ static inline void d2alaw_array (const double *ptr, int count, unsigned char *buffer, double normfact) -{ while (--count >= 0) - { if (!isfinite (ptr [count])) - buffer [count] = 0 ; - else if (ptr [count] >= 0) - buffer [count] = alaw_encode [psf_lrint (normfact * ptr [count])] ; +{ for (int i = 0 ; i < count ; i++) + { if (!isfinite (ptr [i])) + buffer [i] = 0 ; + else if (ptr [i] >= 0) + buffer [i] = alaw_encode [psf_lrint (normfact * ptr [i])] ; else - buffer [count] = 0x7F & alaw_encode [- psf_lrint (normfact * ptr [count])] ; + buffer [i] = 0x7F & alaw_encode [- psf_lrint (normfact * ptr [i])] ; } ; } /* d2alaw_array */ diff --git a/src/common.c b/src/common.c index b8cc2c19..5f09ec0e 100644 --- a/src/common.c +++ b/src/common.c @@ -1659,8 +1659,8 @@ psf_f2s_array (const float *src, short *dest, int count, int normalize) { float normfact ; normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ; - while (--count >= 0) - dest [count] = psf_lrintf (src [count] * normfact) ; + for (int i = 0 ; i < count ; i++) + dest [i] = psf_lrintf (src [i] * normfact) ; return ; } /* psf_f2s_array */ @@ -1671,22 +1671,22 @@ psf_f2s_clip_array (const float *src, short *dest, int count, int normalize) normfact = normalize ? (1.0 * 0x8000) : 1.0 ; - while (--count >= 0) - { scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { scaled_value = src [i] * normfact ; #if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFF)) - { dest [count] = 0x7FFF ; + { dest [i] = 0x7FFF ; continue ; } ; #endif #if CPU_CLIPS_NEGATIVE == 0 if (scaled_value <= (-8.0 * 0x1000)) - { dest [count] = -0x7FFF - 1 ; + { dest [i] = -0x7FFF - 1 ; continue ; } ; #endif - dest [count] = psf_lrintf (scaled_value) ; + dest [i] = psf_lrintf (scaled_value) ; } ; return ; @@ -1697,8 +1697,8 @@ psf_d2s_array (const double *src, short *dest, int count, int normalize) { double normfact ; normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ; - while (--count >= 0) - dest [count] = psf_lrint (src [count] * normfact) ; + for (int i = 0 ; i < count ; i++) + dest [i] = psf_lrint (src [i] * normfact) ; return ; } /* psf_f2s_array */ @@ -1709,22 +1709,22 @@ psf_d2s_clip_array (const double *src, short *dest, int count, int normalize) normfact = normalize ? (1.0 * 0x8000) : 1.0 ; - while (--count >= 0) - { scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { scaled_value = src [i] * normfact ; #if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFF)) - { dest [count] = 0x7FFF ; + { dest [i] = 0x7FFF ; continue ; } ; #endif #if CPU_CLIPS_NEGATIVE == 0 if (scaled_value <= (-8.0 * 0x1000)) - { dest [count] = -0x7FFF - 1 ; + { dest [i] = -0x7FFF - 1 ; continue ; } ; #endif - dest [count] = psf_lrint (scaled_value) ; + dest [i] = psf_lrint (scaled_value) ; } ; return ; @@ -1736,8 +1736,8 @@ psf_f2i_array (const float *src, int *dest, int count, int normalize) { float normfact ; normfact = normalize ? (1.0 * 0x7FFFFFFF) : 1.0 ; - while (--count >= 0) - dest [count] = psf_lrintf (src [count] * normfact) ; + for (int i = 0 ; i < count ; i++) + dest [i] = psf_lrintf (src [i] * normfact) ; return ; } /* psf_f2i_array */ @@ -1748,22 +1748,22 @@ psf_f2i_clip_array (const float *src, int *dest, int count, int normalize) normfact = normalize ? (8.0 * 0x10000000) : 1.0 ; - while (--count >= 0) - { scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { scaled_value = src [i] * normfact ; #if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) - { dest [count] = 0x7FFFFFFF ; + { dest [i] = 0x7FFFFFFF ; continue ; } ; #endif #if CPU_CLIPS_NEGATIVE == 0 if (scaled_value <= (-8.0 * 0x10000000)) - { dest [count] = 0x80000000 ; + { dest [i] = 0x80000000 ; continue ; } ; #endif - dest [count] = psf_lrintf (scaled_value) ; + dest [i] = psf_lrintf (scaled_value) ; } ; return ; @@ -1774,8 +1774,8 @@ psf_d2i_array (const double *src, int *dest, int count, int normalize) { double normfact ; normfact = normalize ? (1.0 * 0x7FFFFFFF) : 1.0 ; - while (--count >= 0) - dest [count] = psf_lrint (src [count] * normfact) ; + for (int i = 0 ; i < count ; i++) + dest [i] = psf_lrint (src [i] * normfact) ; return ; } /* psf_f2i_array */ @@ -1786,22 +1786,22 @@ psf_d2i_clip_array (const double *src, int *dest, int count, int normalize) normfact = normalize ? (8.0 * 0x10000000) : 1.0 ; - while (--count >= 0) - { scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { scaled_value = src [i] * normfact ; #if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) - { dest [count] = 0x7FFFFFFF ; + { dest [i] = 0x7FFFFFFF ; continue ; } ; #endif #if CPU_CLIPS_NEGATIVE == 0 if (scaled_value <= (-8.0 * 0x10000000)) - { dest [count] = 0x80000000 ; + { dest [i] = 0x80000000 ; continue ; } ; #endif - dest [count] = psf_lrint (scaled_value) ; + dest [i] = psf_lrint (scaled_value) ; } ; return ; diff --git a/src/double64.c b/src/double64.c index ff317773..df49a8ff 100644 --- a/src/double64.c +++ b/src/double64.c @@ -486,71 +486,71 @@ double64_get_capability (SF_PRIVATE *psf) static void d2s_array (const double *src, int count, short *dest, double scale) -{ while (--count >= 0) - { dest [count] = psf_lrint (scale * src [count]) ; +{ for (int i = 0 ; i < count ; i++) + { dest [i] = psf_lrint (scale * src [i]) ; } ; } /* d2s_array */ static void d2s_clip_array (const double *src, int count, short *dest, double scale) -{ while (--count >= 0) - { double tmp = scale * src [count] ; +{ for (int i = 0 ; i < count ; i++) + { double tmp = scale * src [i] ; if (CPU_CLIPS_POSITIVE == 0 && tmp > 32767.0) - dest [count] = SHRT_MAX ; + dest [i] = SHRT_MAX ; else if (CPU_CLIPS_NEGATIVE == 0 && tmp < -32768.0) - dest [count] = SHRT_MIN ; + dest [i] = SHRT_MIN ; else - dest [count] = psf_lrint (tmp) ; + dest [i] = psf_lrint (tmp) ; } ; } /* d2s_clip_array */ static void d2i_array (const double *src, int count, int *dest, double scale) -{ while (--count >= 0) - { dest [count] = psf_lrint (scale * src [count]) ; +{ for (int i = 0 ; i < count ; i++) + { dest [i] = psf_lrint (scale * src [i]) ; } ; } /* d2i_array */ static void d2i_clip_array (const double *src, int count, int *dest, double scale) -{ while (--count >= 0) - { float tmp = scale * src [count] ; +{ for (int i = 0 ; i < count ; i++) + { float tmp = scale * src [i] ; if (CPU_CLIPS_POSITIVE == 0 && tmp > (1.0 * INT_MAX)) - dest [count] = INT_MAX ; + dest [i] = INT_MAX ; else if (CPU_CLIPS_NEGATIVE == 0 && tmp < (-1.0 * INT_MAX)) - dest [count] = INT_MIN ; + dest [i] = INT_MIN ; else - dest [count] = psf_lrint (tmp) ; + dest [i] = psf_lrint (tmp) ; } ; } /* d2i_clip_array */ static inline void d2f_array (const double *src, int count, float *dest) -{ while (--count >= 0) - { dest [count] = src [count] ; +{ for (int i = 0 ; i < count ; i++) + { dest [i] = src [i] ; } ; } /* d2f_array */ static inline void s2d_array (const short *src, double *dest, int count, double scale) -{ while (--count >= 0) - { dest [count] = scale * src [count] ; +{ for (int i = 0 ; i < count ; i++) + { dest [i] = scale * src [i] ; } ; } /* s2d_array */ static inline void i2d_array (const int *src, double *dest, int count, double scale) -{ while (--count >= 0) - { dest [count] = scale * src [count] ; +{ for (int i = 0 ; i < count ; i++) + { dest [i] = scale * src [i] ; } ; } /* i2d_array */ static inline void f2d_array (const float *src, double *dest, int count) -{ while (--count >= 0) - { dest [count] = src [count] ; +{ for (int i = 0 ; i < count ; i++) + { dest [i] = src [i] ; } ; } /* f2d_array */ @@ -1049,15 +1049,15 @@ replace_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) static void d2bd_read (double *buffer, int count) -{ while (--count >= 0) - { buffer [count] = DOUBLE64_READ ((unsigned char *) (buffer + count)) ; +{ for (int i = 0 ; i < count ; i++) + { buffer [i] = DOUBLE64_READ ((unsigned char *) &buffer [i]) ; } ; } /* d2bd_read */ static void bd2d_write (double *buffer, int count) -{ while (--count >= 0) - { DOUBLE64_WRITE (buffer [count], (unsigned char*) (buffer + count)) ; +{ for (int i = 0 ; i < count ; i++) + { DOUBLE64_WRITE (buffer [i], (unsigned char*) &buffer [i]) ; } ; } /* bd2d_write */ diff --git a/src/flac.c b/src/flac.c index e3320450..8caf4fa1 100644 --- a/src/flac.c +++ b/src/flac.c @@ -127,39 +127,39 @@ static FLAC__StreamEncoderWriteStatus sf_flac_enc_write_callback (const FLAC__St static void s2flac8_array (const short *src, int32_t *dest, int count) -{ while (--count >= 0) - dest [count] = src [count] >> 8 ; +{ for (int i = 0 ; i < count ; i++) + dest [i] = src [i] >> 8 ; } /* s2flac8_array */ static void s2flac16_array (const short *src, int32_t *dest, int count) -{ while (--count >= 0) - dest [count] = src [count] ; +{ for (int i = 0 ; i < count ; i++) + dest [i] = src [i] ; } /* s2flac16_array */ static void s2flac24_array (const short *src, int32_t *dest, int count) -{ while (--count >= 0) - dest [count] = src [count] << 8 ; +{ for (int i = 0 ; i < count ; i++) + dest [i] = src [i] << 8 ; } /* s2flac24_array */ static void i2flac8_array (const int *src, int32_t *dest, int count) -{ while (--count >= 0) - dest [count] = src [count] >> 24 ; +{ for (int i = 0 ; i < count ; i++) + dest [i] = src [i] >> 24 ; } /* i2flac8_array */ static void i2flac16_array (const int *src, int32_t *dest, int count) { - while (--count >= 0) - dest [count] = src [count] >> 16 ; + for (int i = 0 ; i < count ; i++) + dest [i] = src [i] >> 16 ; } /* i2flac16_array */ static void i2flac24_array (const int *src, int32_t *dest, int count) -{ while (--count >= 0) - dest [count] = src [count] >> 8 ; +{ for (int i = 0 ; i < count ; i++) + dest [i] = src [i] >> 8 ; } /* i2flac24_array */ static sf_count_t @@ -1180,17 +1180,17 @@ f2flac8_clip_array (const float *src, int32_t *dest, int count, int normalize) normfact = normalize ? (8.0 * 0x10) : 1.0 ; - while (--count >= 0) - { scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { scaled_value = src [i] * normfact ; if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7F)) - { dest [count] = 0x7F ; + { dest [i] = 0x7F ; continue ; } ; if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10)) - { dest [count] = -0x80 ; + { dest [i] = -0x80 ; continue ; } ; - dest [count] = psf_lrintf (scaled_value) ; + dest [i] = psf_lrintf (scaled_value) ; } ; return ; @@ -1202,17 +1202,17 @@ f2flac16_clip_array (const float *src, int32_t *dest, int count, int normalize) normfact = normalize ? (8.0 * 0x1000) : 1.0 ; - while (--count >= 0) - { scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { scaled_value = src [i] * normfact ; if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFF)) - { dest [count] = 0x7FFF ; + { dest [i] = 0x7FFF ; continue ; } ; if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x1000)) - { dest [count] = -0x8000 ; + { dest [i] = -0x8000 ; continue ; } ; - dest [count] = psf_lrintf (scaled_value) ; + dest [i] = psf_lrintf (scaled_value) ; } ; } /* f2flac16_clip_array */ @@ -1222,18 +1222,18 @@ f2flac24_clip_array (const float *src, int32_t *dest, int count, int normalize) normfact = normalize ? (8.0 * 0x100000) : 1.0 ; - while (--count >= 0) - { scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { scaled_value = src [i] * normfact ; if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFF)) - { dest [count] = 0x7FFFFF ; + { dest [i] = 0x7FFFFF ; continue ; } ; if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x100000)) - { dest [count] = -0x800000 ; + { dest [i] = -0x800000 ; continue ; } - dest [count] = psf_lrintf (scaled_value) ; + dest [i] = psf_lrintf (scaled_value) ; } ; return ; @@ -1243,24 +1243,24 @@ static void f2flac8_array (const float *src, int32_t *dest, int count, int normalize) { float normfact = normalize ? (1.0 * 0x7F) : 1.0 ; - while (--count >= 0) - dest [count] = psf_lrintf (src [count] * normfact) ; + for (int i = 0 ; i < count ; i++) + dest [i] = psf_lrintf (src [i] * normfact) ; } /* f2flac8_array */ static void f2flac16_array (const float *src, int32_t *dest, int count, int normalize) { float normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ; - while (--count >= 0) - dest [count] = psf_lrintf (src [count] * normfact) ; + for (int i = 0 ; i < count ; i++) + dest [i] = psf_lrintf (src [i] * normfact) ; } /* f2flac16_array */ static void f2flac24_array (const float *src, int32_t *dest, int count, int normalize) { float normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ; - while (--count >= 0) - dest [count] = psf_lrintf (src [count] * normfact) ; + for (int i = 0 ; i < count ; i++) + dest [i] = psf_lrintf (src [i] * normfact) ; } /* f2flac24_array */ static sf_count_t @@ -1311,17 +1311,17 @@ d2flac8_clip_array (const double *src, int32_t *dest, int count, int normalize) normfact = normalize ? (8.0 * 0x10) : 1.0 ; - while (--count >= 0) - { scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { scaled_value = src [i] * normfact ; if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7F)) - { dest [count] = 0x7F ; + { dest [i] = 0x7F ; continue ; } ; if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10)) - { dest [count] = -0x80 ; + { dest [i] = -0x80 ; continue ; } ; - dest [count] = psf_lrint (scaled_value) ; + dest [i] = psf_lrint (scaled_value) ; } ; return ; @@ -1333,17 +1333,17 @@ d2flac16_clip_array (const double *src, int32_t *dest, int count, int normalize) normfact = normalize ? (8.0 * 0x1000) : 1.0 ; - while (--count >= 0) - { scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { scaled_value = src [i] * normfact ; if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFF)) - { dest [count] = 0x7FFF ; + { dest [i] = 0x7FFF ; continue ; } ; if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x1000)) - { dest [count] = -0x8000 ; + { dest [i] = -0x8000 ; continue ; } ; - dest [count] = psf_lrint (scaled_value) ; + dest [i] = psf_lrint (scaled_value) ; } ; return ; @@ -1355,17 +1355,17 @@ d2flac24_clip_array (const double *src, int32_t *dest, int count, int normalize) normfact = normalize ? (8.0 * 0x100000) : 1.0 ; - while (--count >= 0) - { scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { scaled_value = src [i] * normfact ; if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFF)) - { dest [count] = 0x7FFFFF ; + { dest [i] = 0x7FFFFF ; continue ; } ; if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x100000)) - { dest [count] = -0x800000 ; + { dest [i] = -0x800000 ; continue ; } ; - dest [count] = psf_lrint (scaled_value) ; + dest [i] = psf_lrint (scaled_value) ; } ; return ; @@ -1375,24 +1375,24 @@ static void d2flac8_array (const double *src, int32_t *dest, int count, int normalize) { double normfact = normalize ? (1.0 * 0x7F) : 1.0 ; - while (--count >= 0) - dest [count] = psf_lrint (src [count] * normfact) ; + for (int i = 0 ; i < count ; i++) + dest [i] = psf_lrint (src [i] * normfact) ; } /* d2flac8_array */ static void d2flac16_array (const double *src, int32_t *dest, int count, int normalize) { double normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ; - while (--count >= 0) - dest [count] = psf_lrint (src [count] * normfact) ; + for (int i = 0 ; i < count ; i++) + dest [i] = psf_lrint (src [i] * normfact) ; } /* d2flac16_array */ static void d2flac24_array (const double *src, int32_t *dest, int count, int normalize) { double normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ; - while (--count >= 0) - dest [count] = psf_lrint (src [count] * normfact) ; + for (int i = 0 ; i < count ; i++) + dest [i] = psf_lrint (src [i] * normfact) ; } /* d2flac24_array */ static sf_count_t diff --git a/src/float32.c b/src/float32.c index e5eddaec..2e4d4440 100644 --- a/src/float32.c +++ b/src/float32.c @@ -437,71 +437,71 @@ float32_get_capability (SF_PRIVATE *psf) static void f2s_array (const float *src, int count, short *dest, float scale) { - while (--count >= 0) - { dest [count] = psf_lrintf (scale * src [count]) ; + for (int i = 0 ; i < count ; i++) + { dest [i] = psf_lrintf (scale * src [i]) ; } ; } /* f2s_array */ static void f2s_clip_array (const float *src, int count, short *dest, float scale) -{ while (--count >= 0) - { float tmp = scale * src [count] ; +{ for (int i = 0 ; i < count ; i++) + { float tmp = scale * src [i] ; if (CPU_CLIPS_POSITIVE == 0 && tmp > 32767.0) - dest [count] = SHRT_MAX ; + dest [i] = SHRT_MAX ; else if (CPU_CLIPS_NEGATIVE == 0 && tmp < -32768.0) - dest [count] = SHRT_MIN ; + dest [i] = SHRT_MIN ; else - dest [count] = psf_lrintf (tmp) ; + dest [i] = psf_lrintf (tmp) ; } ; } /* f2s_clip_array */ static inline void f2i_array (const float *src, int count, int *dest, float scale) -{ while (--count >= 0) - { dest [count] = psf_lrintf (scale * src [count]) ; +{ for (int i = 0 ; i < count ; i++) + { dest [i] = psf_lrintf (scale * src [i]) ; } ; } /* f2i_array */ static inline void f2i_clip_array (const float *src, int count, int *dest, float scale) -{ while (--count >= 0) - { float tmp = scale * src [count] ; +{ for (int i = 0 ; i < count ; i++) + { float tmp = scale * src [i] ; if (CPU_CLIPS_POSITIVE == 0 && tmp > (1.0 * INT_MAX)) - dest [count] = INT_MAX ; + dest [i] = INT_MAX ; else if (CPU_CLIPS_NEGATIVE == 0 && tmp < (-1.0 * INT_MAX)) - dest [count] = INT_MIN ; + dest [i] = INT_MIN ; else - dest [count] = psf_lrintf (tmp) ; + dest [i] = psf_lrintf (tmp) ; } ; } /* f2i_clip_array */ static inline void f2d_array (const float *src, int count, double *dest) -{ while (--count >= 0) - { dest [count] = src [count] ; +{ for (int i = 0 ; i < count ; i++) + { dest [i] = src [i] ; } ; } /* f2d_array */ static inline void s2f_array (const short *src, float *dest, int count, float scale) -{ while (--count >= 0) - { dest [count] = scale * src [count] ; +{ for (int i = 0 ; i < count ; i++) + { dest [i] = scale * src [i] ; } ; } /* s2f_array */ static inline void i2f_array (const int *src, float *dest, int count, float scale) -{ while (--count >= 0) - { dest [count] = scale * src [count] ; +{ for (int i = 0 ; i < count ; i++) + { dest [i] = scale * src [i] ; } ; } /* i2f_array */ static inline void d2f_array (const double *src, float *dest, int count) -{ while (--count >= 0) - { dest [count] = src [count] ; +{ for (int i = 0 ; i < count ; i++) + { dest [i] = src [i] ; } ; } /* d2f_array */ @@ -1003,15 +1003,15 @@ replace_write_d2f (SF_PRIVATE *psf, const double *ptr, sf_count_t len) static void bf2f_array (float *buffer, int count) -{ while (--count >= 0) - { buffer [count] = FLOAT32_READ ((unsigned char *) (buffer + count)) ; +{ for (int i = 0 ; i < count ; i++) + { buffer [i] = FLOAT32_READ ((unsigned char *) &buffer [i]) ; } ; } /* bf2f_array */ static void f2bf_array (float *buffer, int count) -{ while (--count >= 0) - { FLOAT32_WRITE (buffer [count], (unsigned char*) (buffer + count)) ; +{ for (int i = 0 ; i < count ; i++) + { FLOAT32_WRITE (buffer [i], (unsigned char*) &buffer [i]) ; } ; } /* f2bf_array */ diff --git a/src/mpeg_decode.c b/src/mpeg_decode.c index f9b23493..b2c8fb61 100644 --- a/src/mpeg_decode.c +++ b/src/mpeg_decode.c @@ -84,8 +84,10 @@ mpeg_dec_close (SF_PRIVATE *psf) return 0 ; } /* mpeg_dec_close */ -static sf_count_t mpeg_dec_decode (SF_PRIVATE *psf, MPEG_DEC_PRIVATE *pmp3d, float *ptr, sf_count_t len) -{ size_t done ; +static sf_count_t +mpeg_dec_decode (SF_PRIVATE *psf, float *ptr, sf_count_t len) +{ MPEG_DEC_PRIVATE *pmp3d = (MPEG_DEC_PRIVATE *) psf->codec_data ; + size_t done ; int error ; error = mpg123_read (pmp3d->pmh, (unsigned char *) ptr, len * sizeof (float), &done) ; @@ -105,110 +107,85 @@ static sf_count_t mpeg_dec_decode (SF_PRIVATE *psf, MPEG_DEC_PRIVATE *pmp3d, flo return -1 ; } /* mpeg_dec_decode */ -static inline void -f2s_array (const float *src, int count, short *dest) -{ while (--count >= 0) - { dest [count] = psf_lrintf (src [count] * (float) 0x7FFF) ; - } ; -} /* f2s_array */ - static sf_count_t mpeg_dec_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) { BUF_UNION ubuf ; - MPEG_DEC_PRIVATE *pmp3d = (MPEG_DEC_PRIVATE *) psf->codec_data ; - sf_count_t total = 0 ; - sf_count_t done ; + sf_count_t total, readlen ; + void (*convert) (const float *, short *, int, int) ; const sf_count_t buflen = ARRAY_LEN (ubuf.fbuf) ; - while (len > 0) - { done = mpeg_dec_decode (psf, pmp3d, ubuf.fbuf, SF_MIN (buflen, len)) ; - - if (done <= 0) + convert = (psf->add_clipping) ? psf_f2s_clip_array : psf_f2s_array ; + for (total = 0 ; total < len ; total += readlen) + { readlen = mpeg_dec_decode (psf, ubuf.fbuf, SF_MIN (buflen, len - total)) ; + if (readlen <= 0) break ; - f2s_array (ubuf.fbuf, done, ptr + total) ; - - total += done ; - len -= done ; - } + convert (ubuf.fbuf, ptr + total, readlen, SF_TRUE) ; + } ; return total ; } /*mpeg_dec_read_s */ -static inline void -f2i_array (const float *src, int count, int *dest) -{ while (--count >= 0) - { dest [count] = psf_lrintf (src [count] * (float) 0x7FFFFFFF) ; - } ; -} /* f2i_array */ - static sf_count_t mpeg_dec_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) { BUF_UNION ubuf ; - MPEG_DEC_PRIVATE *pmp3d = (MPEG_DEC_PRIVATE *) psf->codec_data ; - sf_count_t total = 0 ; - sf_count_t done ; + sf_count_t total, readlen ; + void (*convert) (const float *, int *, int, int) ; const sf_count_t buflen = ARRAY_LEN (ubuf.fbuf) ; - while (len > 0) - { done = mpeg_dec_decode (psf, pmp3d, ubuf.fbuf, SF_MIN (buflen, len)) ; - - if (done <= 0) + convert = (psf->add_clipping) ? psf_f2i_clip_array : psf_f2i_array ; + for (total = 0 ; total < len ; total += readlen) + { readlen = mpeg_dec_decode (psf, ubuf.fbuf, SF_MIN (buflen, len - total)) ; + if (readlen <= 0) break ; - f2i_array (ubuf.fbuf, done, ptr + total) ; - - total += done ; - len -= done ; - } + convert (ubuf.fbuf, ptr + total, readlen, SF_TRUE) ; + } ; return total ; } /* mpeg_dec_read_i */ static sf_count_t mpeg_dec_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) -{ MPEG_DEC_PRIVATE *pmp3d = (MPEG_DEC_PRIVATE *) psf->codec_data ; - sf_count_t done ; - sf_count_t count ; +{ sf_count_t readlen ; - done = mpeg_dec_decode (psf, pmp3d, ptr, len) ; - - if (done <= 0) + readlen = mpeg_dec_decode (psf, ptr, len) ; + if (readlen <= 0) return 0 ; if (psf->norm_float == SF_FALSE) - { count = done ; - while (--count >= 0) - { ptr [count] *= (double) 0x8000 ; + for (int i = 0 ; i < readlen ; i++) + { ptr [i] *= (1.0f * 0x8000) ; } ; - } ; - return done ; + return readlen ; } /* mpeg_dec_read_f */ static inline void f2d_array (const float *src, int count, double *dest, double normfact) -{ while (--count >= 0) - { dest [count] = src [count] * normfact ; +{ for (int i = 0 ; i < count ; i++) + { dest [i] = src [i] * normfact ; } } /* f2d_array */ static sf_count_t mpeg_dec_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) -{ MPEG_DEC_PRIVATE *pmp3d = (MPEG_DEC_PRIVATE *) psf->codec_data ; - sf_count_t done ; +{ BUF_UNION ubuf ; + sf_count_t total, readlen ; double normfact ; + const sf_count_t buflen = ARRAY_LEN (ubuf.fbuf) ; - normfact = (psf->norm_double == SF_TRUE) ? 1.0 : (double) 0x8000 ; + normfact = (psf->norm_double == SF_TRUE) ? 1.0 : (1.0 * 0x8000) ; - done = mpeg_dec_decode (psf, pmp3d, (float *) ptr, len) ; + for (total = 0 ; total < len ; total += readlen) + { readlen = mpeg_dec_decode (psf, ubuf.fbuf, SF_MIN (buflen, len - total)) ; + if (readlen <= 0) + break ; - if (done <= 0) - return 0 ; + f2d_array (ubuf.fbuf, readlen, ptr + total, normfact) ; + } ; - f2d_array ((float *) ptr, done, ptr, normfact) ; - - return done ; + return total ; } /* mpeg_dec_read_d */ static sf_count_t diff --git a/src/pcm.c b/src/pcm.c index 79f194f2..7e5fdf62 100644 --- a/src/pcm.c +++ b/src/pcm.c @@ -284,37 +284,37 @@ pcm_init (SF_PRIVATE *psf) static inline void sc2s_array (const signed char *src, int count, short *dest) -{ while (--count >= 0) - { dest [count] = ((uint16_t) src [count]) << 8 ; +{ for (int i = 0 ; i < count ; i++) + { dest [i] = ((uint16_t) src [i]) << 8 ; } ; } /* sc2s_array */ static inline void uc2s_array (const unsigned char *src, int count, short *dest) -{ while (--count >= 0) - { dest [count] = (((uint32_t) src [count]) - 0x80) << 8 ; +{ for (int i = 0 ; i < count ; i++) + { dest [i] = (((uint32_t) src [i]) - 0x80) << 8 ; } ; } /* uc2s_array */ static inline void let2s_array (const tribyte *src, int count, short *dest) -{ while (--count >= 0) - dest [count] = LET2H_16_PTR (src [count].bytes) ; +{ for (int i = 0 ; i < count ; i++) + dest [i] = LET2H_16_PTR (src [i].bytes) ; } /* let2s_array */ static inline void bet2s_array (const tribyte *src, int count, short *dest) -{ while (--count >= 0) - dest [count] = BET2H_16_PTR (src [count].bytes) ; +{ for (int i = 0 ; i < count ; i++) + dest [i] = BET2H_16_PTR (src [i].bytes) ; } /* bet2s_array */ static inline void lei2s_array (const int *src, int count, short *dest) { int value ; - while (--count >= 0) - { value = LE2H_32 (src [count]) ; - dest [count] = value >> 16 ; + for (int i = 0 ; i < count ; i++) + { value = LE2H_32 (src [i]) ; + dest [i] = value >> 16 ; } ; } /* lei2s_array */ @@ -322,9 +322,9 @@ static inline void bei2s_array (const int *src, int count, short *dest) { int value ; - while (--count >= 0) - { value = BE2H_32 (src [count]) ; - dest [count] = value >> 16 ; + for (int i = 0 ; i < count ; i++) + { value = BE2H_32 (src [i]) ; + dest [i] = value >> 16 ; } ; } /* bei2s_array */ @@ -333,15 +333,15 @@ bei2s_array (const int *src, int count, short *dest) static inline void sc2i_array (const signed char *src, int count, int *dest) -{ while (--count >= 0) - { dest [count] = arith_shift_left ((int) src [count], 24) ; +{ for (int i = 0 ; i < count ; i++) + { dest [i] = arith_shift_left ((int) src [i], 24) ; } ; } /* sc2i_array */ static inline void uc2i_array (const unsigned char *src, int count, int *dest) -{ while (--count >= 0) - { dest [count] = arith_shift_left (((int) src [count]) - 128, 24) ; +{ for (int i = 0 ; i < count ; i++) + { dest [i] = arith_shift_left (((int) src [i]) - 128, 24) ; } ; } /* uc2i_array */ @@ -349,9 +349,9 @@ static inline void bes2i_array (const short *src, int count, int *dest) { short value ; - while (--count >= 0) - { value = BE2H_16 (src [count]) ; - dest [count] = arith_shift_left (value, 16) ; + for (int i = 0 ; i < count ; i++) + { value = BE2H_16 (src [i]) ; + dest [i] = arith_shift_left (value, 16) ; } ; } /* bes2i_array */ @@ -359,22 +359,22 @@ static inline void les2i_array (const short *src, int count, int *dest) { short value ; - while (--count >= 0) - { value = LE2H_16 (src [count]) ; - dest [count] = arith_shift_left (value, 16) ; + for (int i = 0 ; i < count ; i++) + { value = LE2H_16 (src [i]) ; + dest [i] = arith_shift_left (value, 16) ; } ; } /* les2i_array */ static inline void bet2i_array (const tribyte *src, int count, int *dest) -{ while (--count >= 0) - dest [count] = psf_get_be24 (src [count].bytes, 0) ; +{ for (int i = 0 ; i < count ; i++) + dest [i] = psf_get_be24 (src [i].bytes, 0) ; } /* bet2i_array */ static inline void let2i_array (const tribyte *src, int count, int *dest) -{ while (--count >= 0) - dest [count] = psf_get_le24 (src [count].bytes, 0) ; +{ for (int i = 0 ; i < count ; i++) + dest [i] = psf_get_le24 (src [i].bytes, 0) ; } /* let2i_array */ /*-------------------------------------------------------------------------- @@ -382,24 +382,24 @@ let2i_array (const tribyte *src, int count, int *dest) static inline void sc2f_array (const signed char *src, int count, float *dest, float normfact) -{ while (--count >= 0) - dest [count] = ((float) src [count]) * normfact ; +{ for (int i = 0 ; i < count ; i++) + dest [i] = ((float) src [i]) * normfact ; } /* sc2f_array */ static inline void uc2f_array (const unsigned char *src, int count, float *dest, float normfact) -{ while (--count >= 0) - dest [count] = (((int) src [count]) - 128) * normfact ; +{ for (int i = 0 ; i < count ; i++) + dest [i] = (((int) src [i]) - 128) * normfact ; } /* uc2f_array */ static inline void les2f_array (const short *src, int count, float *dest, float normfact) { short value ; - while (--count >= 0) - { value = src [count] ; + for (int i = 0 ; i < count ; i++) + { value = src [i] ; value = LE2H_16 (value) ; - dest [count] = ((float) value) * normfact ; + dest [i] = ((float) value) * normfact ; } ; } /* les2f_array */ @@ -407,10 +407,10 @@ static inline void bes2f_array (const short *src, int count, float *dest, float normfact) { short value ; - while (--count >= 0) - { value = src [count] ; + for (int i = 0 ; i < count ; i++) + { value = src [i] ; value = BE2H_16 (value) ; - dest [count] = ((float) value) * normfact ; + dest [i] = ((float) value) * normfact ; } ; } /* bes2f_array */ @@ -418,9 +418,9 @@ static inline void let2f_array (const tribyte *src, int count, float *dest, float normfact) { int value ; - while (--count >= 0) - { value = psf_get_le24 (src [count].bytes, 0) ; - dest [count] = ((float) value) * normfact ; + for (int i = 0 ; i < count ; i++) + { value = psf_get_le24 (src [i].bytes, 0) ; + dest [i] = ((float) value) * normfact ; } ; } /* let2f_array */ @@ -428,9 +428,9 @@ static inline void bet2f_array (const tribyte *src, int count, float *dest, float normfact) { int value ; - while (--count >= 0) - { value = psf_get_be24 (src [count].bytes, 0) ; - dest [count] = ((float) value) * normfact ; + for (int i = 0 ; i < count ; i++) + { value = psf_get_be24 (src [i].bytes, 0) ; + dest [i] = ((float) value) * normfact ; } ; } /* bet2f_array */ @@ -438,10 +438,10 @@ static inline void lei2f_array (const int *src, int count, float *dest, float normfact) { int value ; - while (--count >= 0) - { value = src [count] ; + for (int i = 0 ; i < count ; i++) + { value = src [i] ; value = LE2H_32 (value) ; - dest [count] = ((float) value) * normfact ; + dest [i] = ((float) value) * normfact ; } ; } /* lei2f_array */ @@ -449,10 +449,10 @@ static inline void bei2f_array (const int *src, int count, float *dest, float normfact) { int value ; - while (--count >= 0) - { value = src [count] ; + for (int i = 0 ; i < count ; i++) + { value = src [i] ; value = BE2H_32 (value) ; - dest [count] = ((float) value) * normfact ; + dest [i] = ((float) value) * normfact ; } ; } /* bei2f_array */ @@ -461,24 +461,24 @@ bei2f_array (const int *src, int count, float *dest, float normfact) static inline void sc2d_array (const signed char *src, int count, double *dest, double normfact) -{ while (--count >= 0) - dest [count] = ((double) src [count]) * normfact ; +{ for (int i = 0 ; i < count ; i++) + dest [i] = ((double) src [i]) * normfact ; } /* sc2d_array */ static inline void uc2d_array (const unsigned char *src, int count, double *dest, double normfact) -{ while (--count >= 0) - dest [count] = (((int) src [count]) - 128) * normfact ; +{ for (int i = 0 ; i < count ; i++) + dest [i] = (((int) src [i]) - 128) * normfact ; } /* uc2d_array */ static inline void les2d_array (const short *src, int count, double *dest, double normfact) { short value ; - while (--count >= 0) - { value = src [count] ; + for (int i = 0 ; i < count ; i++) + { value = src [i] ; value = LE2H_16 (value) ; - dest [count] = ((double) value) * normfact ; + dest [i] = ((double) value) * normfact ; } ; } /* les2d_array */ @@ -486,10 +486,10 @@ static inline void bes2d_array (const short *src, int count, double *dest, double normfact) { short value ; - while (--count >= 0) - { value = src [count] ; + for (int i = 0 ; i < count ; i++) + { value = src [i] ; value = BE2H_16 (value) ; - dest [count] = ((double) value) * normfact ; + dest [i] = ((double) value) * normfact ; } ; } /* bes2d_array */ @@ -497,9 +497,9 @@ static inline void let2d_array (const tribyte *src, int count, double *dest, double normfact) { int value ; - while (--count >= 0) - { value = psf_get_le24 (src [count].bytes, 0) ; - dest [count] = ((double) value) * normfact ; + for (int i = 0 ; i < count ; i++) + { value = psf_get_le24 (src [i].bytes, 0) ; + dest [i] = ((double) value) * normfact ; } ; } /* let2d_array */ @@ -507,9 +507,9 @@ static inline void bet2d_array (const tribyte *src, int count, double *dest, double normfact) { int value ; - while (--count >= 0) - { value = psf_get_be24 (src [count].bytes, 0) ; - dest [count] = ((double) value) * normfact ; + for (int i = 0 ; i < count ; i++) + { value = psf_get_be24 (src [i].bytes, 0) ; + dest [i] = ((double) value) * normfact ; } ; } /* bet2d_array */ @@ -517,10 +517,10 @@ static inline void lei2d_array (const int *src, int count, double *dest, double normfact) { int value ; - while (--count >= 0) - { value = src [count] ; + for (int i = 0 ; i < count ; i++) + { value = src [i] ; value = LE2H_32 (value) ; - dest [count] = ((double) value) * normfact ; + dest [i] = ((double) value) * normfact ; } ; } /* lei2d_array */ @@ -528,10 +528,10 @@ static inline void bei2d_array (const int *src, int count, double *dest, double normfact) { int value ; - while (--count >= 0) - { value = src [count] ; + for (int i = 0 ; i < count ; i++) + { value = src [i] ; value = BE2H_32 (value) ; - dest [count] = ((double) value) * normfact ; + dest [i] = ((double) value) * normfact ; } ; } /* bei2d_array */ @@ -540,31 +540,31 @@ bei2d_array (const int *src, int count, double *dest, double normfact) static inline void s2sc_array (const short *src, signed char *dest, int count) -{ while (--count >= 0) - dest [count] = src [count] >> 8 ; +{ for (int i = 0 ; i < count ; i++) + dest [i] = src [i] >> 8 ; } /* s2sc_array */ static inline void s2uc_array (const short *src, unsigned char *dest, int count) -{ while (--count >= 0) - dest [count] = (src [count] >> 8) + 0x80 ; +{ for (int i = 0 ; i < count ; i++) + dest [i] = (src [i] >> 8) + 0x80 ; } /* s2uc_array */ static inline void s2let_array (const short *src, tribyte *dest, int count) -{ while (--count >= 0) - { dest [count].bytes [0] = 0 ; - dest [count].bytes [1] = src [count] ; - dest [count].bytes [2] = src [count] >> 8 ; +{ for (int i = 0 ; i < count ; i++) + { dest [i].bytes [0] = 0 ; + dest [i].bytes [1] = src [i] ; + dest [i].bytes [2] = src [i] >> 8 ; } ; } /* s2let_array */ static inline void s2bet_array (const short *src, tribyte *dest, int count) -{ while (--count >= 0) - { dest [count].bytes [2] = 0 ; - dest [count].bytes [1] = src [count] ; - dest [count].bytes [0] = src [count] >> 8 ; +{ for (int i = 0 ; i < count ; i++) + { dest [i].bytes [2] = 0 ; + dest [i].bytes [1] = src [i] ; + dest [i].bytes [0] = src [i] >> 8 ; } ; } /* s2bet_array */ @@ -572,13 +572,12 @@ static inline void s2lei_array (const short *src, int *dest, int count) { unsigned char *ucptr ; - ucptr = ((unsigned char*) dest) + 4 * count ; - while (--count >= 0) - { ucptr -= 4 ; + for (int i = 0 ; i < count ; i++) + { ucptr = (unsigned char*) &dest [i] ; ucptr [0] = 0 ; ucptr [1] = 0 ; - ucptr [2] = src [count] ; - ucptr [3] = src [count] >> 8 ; + ucptr [2] = src [i] ; + ucptr [3] = src [i] >> 8 ; } ; } /* s2lei_array */ @@ -586,11 +585,10 @@ static inline void s2bei_array (const short *src, int *dest, int count) { unsigned char *ucptr ; - ucptr = ((unsigned char*) dest) + 4 * count ; - while (--count >= 0) - { ucptr -= 4 ; - ucptr [0] = src [count] >> 8 ; - ucptr [1] = src [count] ; + for (int i = 0 ; i < count ; i++) + { ucptr = (unsigned char*) &dest [i] ; + ucptr [0] = src [i] >> 8 ; + ucptr [1] = src [i] ; ucptr [2] = 0 ; ucptr [3] = 0 ; } ; @@ -601,25 +599,24 @@ s2bei_array (const short *src, int *dest, int count) static inline void i2sc_array (const int *src, signed char *dest, int count) -{ while (--count >= 0) - dest [count] = (src [count] >> 24) ; +{ for (int i = 0 ; i < count ; i++) + dest [i] = (src [i] >> 24) ; } /* i2sc_array */ static inline void i2uc_array (const int *src, unsigned char *dest, int count) -{ while (--count >= 0) - dest [count] = ((src [count] >> 24) + 128) ; +{ for (int i = 0 ; i < count ; i++) + dest [i] = ((src [i] >> 24) + 128) ; } /* i2uc_array */ static inline void i2bes_array (const int *src, short *dest, int count) { unsigned char *ucptr ; - ucptr = ((unsigned char*) dest) + 2 * count ; - while (--count >= 0) - { ucptr -= 2 ; - ucptr [0] = src [count] >> 24 ; - ucptr [1] = src [count] >> 16 ; + for (int i = 0 ; i < count ; i++) + { ucptr = (unsigned char*) &dest [i] ; + ucptr [0] = src [i] >> 24 ; + ucptr [1] = src [i] >> 16 ; } ; } /* i2bes_array */ @@ -627,11 +624,10 @@ static inline void i2les_array (const int *src, short *dest, int count) { unsigned char *ucptr ; - ucptr = ((unsigned char*) dest) + 2 * count ; - while (--count >= 0) - { ucptr -= 2 ; - ucptr [0] = src [count] >> 16 ; - ucptr [1] = src [count] >> 24 ; + for (int i = 0 ; i < count ; i++) + { ucptr = (unsigned char*) &dest [i] ; + ucptr [0] = src [i] >> 16 ; + ucptr [1] = src [i] >> 24 ; } ; } /* i2les_array */ @@ -639,11 +635,11 @@ static inline void i2let_array (const int *src, tribyte *dest, int count) { int value ; - while (--count >= 0) - { value = src [count] >> 8 ; - dest [count].bytes [0] = value ; - dest [count].bytes [1] = value >> 8 ; - dest [count].bytes [2] = value >> 16 ; + for (int i = 0 ; i < count ; i++) + { value = src [i] >> 8 ; + dest [i].bytes [0] = value ; + dest [i].bytes [1] = value >> 8 ; + dest [i].bytes [2] = value >> 16 ; } ; } /* i2let_array */ @@ -651,11 +647,11 @@ static inline void i2bet_array (const int *src, tribyte *dest, int count) { int value ; - while (--count >= 0) - { value = src [count] >> 8 ; - dest [count].bytes [2] = value ; - dest [count].bytes [1] = value >> 8 ; - dest [count].bytes [0] = value >> 16 ; + for (int i = 0 ; i < count ; i++) + { value = src [i] >> 8 ; + dest [i].bytes [2] = value ; + dest [i].bytes [1] = value >> 8 ; + dest [i].bytes [0] = value >> 16 ; } ; } /* i2bet_array */ @@ -1777,8 +1773,8 @@ f2sc_array (const float *src, signed char *dest, int count, int normalize) normfact = normalize ? (1.0 * 0x7F) : 1.0 ; - while (--count >= 0) - { dest [count] = psf_lrintf (src [count] * normfact) ; + for (int i = 0 ; i < count ; i++) + { dest [i] = psf_lrintf (src [i] * normfact) ; } ; } /* f2sc_array */ @@ -1788,22 +1784,22 @@ f2sc_clip_array (const float *src, signed char *dest, int count, int normalize) normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x1000000) ; - while (--count >= 0) - { scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { scaled_value = src [i] * normfact ; #if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) - { dest [count] = 127 ; + { dest [i] = 127 ; continue ; } ; #endif #if CPU_CLIPS_NEGATIVE == 0 if (scaled_value <= (-8.0 * 0x10000000)) - { dest [count] = -128 ; + { dest [i] = -128 ; continue ; } ; #endif - dest [count] = psf_lrintf (scaled_value) >> 24 ; + dest [i] = psf_lrintf (scaled_value) >> 24 ; } ; } /* f2sc_clip_array */ @@ -1840,8 +1836,8 @@ f2uc_array (const float *src, unsigned char *dest, int count, int normalize) normfact = normalize ? (1.0 * 0x7F) : 1.0 ; - while (--count >= 0) - { dest [count] = psf_lrintf (src [count] * normfact) + 128 ; + for (int i = 0 ; i < count ; i++) + { dest [i] = psf_lrintf (src [i] * normfact) + 128 ; } ; } /* f2uc_array */ @@ -1851,22 +1847,22 @@ f2uc_clip_array (const float *src, unsigned char *dest, int count, int normalize normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x1000000) ; - while (--count >= 0) - { scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { scaled_value = src [i] * normfact ; #if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) - { dest [count] = 0xFF ; + { dest [i] = 0xFF ; continue ; } ; #endif #if CPU_CLIPS_NEGATIVE == 0 if (scaled_value <= (-8.0 * 0x10000000)) - { dest [count] = 0 ; + { dest [i] = 0 ; continue ; } ; #endif - dest [count] = (psf_lrintf (scaled_value) >> 24) + 128 ; + dest [i] = (psf_lrintf (scaled_value) >> 24) + 128 ; } ; } /* f2uc_clip_array */ @@ -1904,11 +1900,10 @@ f2bes_array (const float *src, short *dest, int count, int normalize) short value ; normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ; - ucptr = ((unsigned char*) dest) + 2 * count ; - while (--count >= 0) - { ucptr -= 2 ; - value = psf_lrintf (src [count] * normfact) ; + for (int i = 0 ; i < count ; i++) + { ucptr = (unsigned char*) &dest [i] ; + value = psf_lrintf (src [i] * normfact) ; ucptr [1] = value ; ucptr [0] = value >> 8 ; } ; @@ -1921,11 +1916,10 @@ f2bes_clip_array (const float *src, short *dest, int count, int normalize) int value ; normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x10000) ; - ucptr = ((unsigned char*) dest) + 2 * count ; - while (--count >= 0) - { ucptr -= 2 ; - scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { ucptr = (unsigned char*) &dest [i] ; + scaled_value = src [i] * normfact ; #if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) { ucptr [1] = 0xFF ; @@ -1981,11 +1975,10 @@ f2les_array (const float *src, short *dest, int count, int normalize) int value ; normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ; - ucptr = ((unsigned char*) dest) + 2 * count ; - while (--count >= 0) - { ucptr -= 2 ; - value = psf_lrintf (src [count] * normfact) ; + for (int i = 0 ; i < count ; i++) + { ucptr = (unsigned char*) &dest [i] ; + value = psf_lrintf (src [i] * normfact) ; ucptr [0] = value ; ucptr [1] = value >> 8 ; } ; @@ -1998,11 +1991,10 @@ f2les_clip_array (const float *src, short *dest, int count, int normalize) int value ; normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x10000) ; - ucptr = ((unsigned char*) dest) + 2 * count ; - while (--count >= 0) - { ucptr -= 2 ; - scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { ucptr = (unsigned char*) &dest [i] ; + scaled_value = src [i] * normfact ; #if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) { ucptr [0] = 0xFF ; @@ -2058,11 +2050,11 @@ f2let_array (const float *src, tribyte *dest, int count, int normalize) normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ; - while (--count >= 0) - { value = psf_lrintf (src [count] * normfact) ; - dest [count].bytes [0] = value ; - dest [count].bytes [1] = value >> 8 ; - dest [count].bytes [2] = value >> 16 ; + for (int i = 0 ; i < count ; i++) + { value = psf_lrintf (src [i] * normfact) ; + dest [i].bytes [0] = value ; + dest [i].bytes [1] = value >> 8 ; + dest [i].bytes [2] = value >> 16 ; } ; } /* f2let_array */ @@ -2073,29 +2065,29 @@ f2let_clip_array (const float *src, tribyte *dest, int count, int normalize) normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x100) ; - while (--count >= 0) - { scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { scaled_value = src [i] * normfact ; #if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) - { dest [count].bytes [0] = 0xFF ; - dest [count].bytes [1] = 0xFF ; - dest [count].bytes [2] = 0x7F ; + { dest [i].bytes [0] = 0xFF ; + dest [i].bytes [1] = 0xFF ; + dest [i].bytes [2] = 0x7F ; continue ; } ; #endif #if CPU_CLIPS_NEGATIVE == 0 if (scaled_value <= (-8.0 * 0x10000000)) - { dest [count].bytes [0] = 0x00 ; - dest [count].bytes [1] = 0x00 ; - dest [count].bytes [2] = 0x80 ; + { dest [i].bytes [0] = 0x00 ; + dest [i].bytes [1] = 0x00 ; + dest [i].bytes [2] = 0x80 ; continue ; } ; #endif value = psf_lrintf (scaled_value) ; - dest [count].bytes [0] = value >> 8 ; - dest [count].bytes [1] = value >> 16 ; - dest [count].bytes [2] = value >> 24 ; + dest [i].bytes [0] = value >> 8 ; + dest [i].bytes [1] = value >> 16 ; + dest [i].bytes [2] = value >> 24 ; } ; } /* f2let_clip_array */ @@ -2133,11 +2125,11 @@ f2bet_array (const float *src, tribyte *dest, int count, int normalize) normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ; - while (--count >= 0) - { value = psf_lrintf (src [count] * normfact) ; - dest [count].bytes [0] = value >> 16 ; - dest [count].bytes [1] = value >> 8 ; - dest [count].bytes [2] = value ; + for (int i = 0 ; i < count ; i++) + { value = psf_lrintf (src [i] * normfact) ; + dest [i].bytes [0] = value >> 16 ; + dest [i].bytes [1] = value >> 8 ; + dest [i].bytes [2] = value ; } ; } /* f2bet_array */ @@ -2148,29 +2140,29 @@ f2bet_clip_array (const float *src, tribyte *dest, int count, int normalize) normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x100) ; - while (--count >= 0) - { scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { scaled_value = src [i] * normfact ; #if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) - { dest [count].bytes [0] = 0x7F ; - dest [count].bytes [1] = 0xFF ; - dest [count].bytes [2] = 0xFF ; + { dest [i].bytes [0] = 0x7F ; + dest [i].bytes [1] = 0xFF ; + dest [i].bytes [2] = 0xFF ; continue ; } ; #endif #if CPU_CLIPS_NEGATIVE == 0 if (scaled_value <= (-8.0 * 0x10000000)) - { dest [count].bytes [0] = 0x80 ; - dest [count].bytes [1] = 0x00 ; - dest [count].bytes [2] = 0x00 ; + { dest [i].bytes [0] = 0x80 ; + dest [i].bytes [1] = 0x00 ; + dest [i].bytes [2] = 0x00 ; continue ; } ; #endif value = psf_lrint (scaled_value) ; - dest [count].bytes [0] = value >> 24 ; - dest [count].bytes [1] = value >> 16 ; - dest [count].bytes [2] = value >> 8 ; + dest [i].bytes [0] = value >> 24 ; + dest [i].bytes [1] = value >> 16 ; + dest [i].bytes [2] = value >> 8 ; } ; } /* f2bet_clip_array */ @@ -2208,10 +2200,10 @@ f2bei_array (const float *src, int *dest, int count, int normalize) int value ; normfact = normalize ? (1.0 * 0x7FFFFFFF) : 1.0 ; - ucptr = ((unsigned char*) dest) + 4 * count ; - while (--count >= 0) - { ucptr -= 4 ; - value = psf_lrintf (src [count] * normfact) ; + + for (int i = 0 ; i < count ; i++) + { ucptr = (unsigned char*) &dest [i] ; + value = psf_lrintf (src [i] * normfact) ; ucptr [0] = value >> 24 ; ucptr [1] = value >> 16 ; ucptr [2] = value >> 8 ; @@ -2226,11 +2218,10 @@ f2bei_clip_array (const float *src, int *dest, int count, int normalize) int value ; normfact = normalize ? (8.0 * 0x10000000) : 1.0 ; - ucptr = ((unsigned char*) dest) + 4 * count ; - while (--count >= 0) - { ucptr -= 4 ; - scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { ucptr = (unsigned char*) &dest [i] ; + scaled_value = src [i] * normfact ; #if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= 1.0 * 0x7FFFFFFF) { ucptr [0] = 0x7F ; @@ -2292,11 +2283,10 @@ f2lei_array (const float *src, int *dest, int count, int normalize) int value ; normfact = normalize ? (1.0 * 0x7FFFFFFF) : 1.0 ; - ucptr = ((unsigned char*) dest) + 4 * count ; - while (--count >= 0) - { ucptr -= 4 ; - value = psf_lrintf (src [count] * normfact) ; + for (int i = 0 ; i < count ; i++) + { ucptr = (unsigned char*) &dest [i] ; + value = psf_lrintf (src [i] * normfact) ; ucptr [0] = value ; ucptr [1] = value >> 8 ; ucptr [2] = value >> 16 ; @@ -2311,11 +2301,10 @@ f2lei_clip_array (const float *src, int *dest, int count, int normalize) int value ; normfact = normalize ? (8.0 * 0x10000000) : 1.0 ; - ucptr = ((unsigned char*) dest) + 4 * count ; - while (--count >= 0) - { ucptr -= 4 ; - scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { ucptr = (unsigned char*) &dest [i] ; + scaled_value = src [i] * normfact ; #if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) { ucptr [0] = 0xFF ; @@ -2376,8 +2365,8 @@ d2sc_array (const double *src, signed char *dest, int count, int normalize) normfact = normalize ? (1.0 * 0x7F) : 1.0 ; - while (--count >= 0) - { dest [count] = psf_lrint (src [count] * normfact) ; + for (int i = 0 ; i < count ; i++) + { dest [i] = psf_lrint (src [i] * normfact) ; } ; } /* d2sc_array */ @@ -2387,22 +2376,22 @@ d2sc_clip_array (const double *src, signed char *dest, int count, int normalize) normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x1000000) ; - while (--count >= 0) - { scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { scaled_value = src [i] * normfact ; #if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) - { dest [count] = 127 ; + { dest [i] = 127 ; continue ; } ; #endif #if CPU_CLIPS_NEGATIVE == 0 if (scaled_value <= (-8.0 * 0x10000000)) - { dest [count] = -128 ; + { dest [i] = -128 ; continue ; } ; #endif - dest [count] = psf_lrintf (scaled_value) >> 24 ; + dest [i] = psf_lrintf (scaled_value) >> 24 ; } ; } /* d2sc_clip_array */ @@ -2439,8 +2428,8 @@ d2uc_array (const double *src, unsigned char *dest, int count, int normalize) normfact = normalize ? (1.0 * 0x7F) : 1.0 ; - while (--count >= 0) - { dest [count] = psf_lrint (src [count] * normfact) + 128 ; + for (int i = 0 ; i < count ; i++) + { dest [i] = psf_lrint (src [i] * normfact) + 128 ; } ; } /* d2uc_array */ @@ -2450,22 +2439,22 @@ d2uc_clip_array (const double *src, unsigned char *dest, int count, int normaliz normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x1000000) ; - while (--count >= 0) - { scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { scaled_value = src [i] * normfact ; #if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) - { dest [count] = 255 ; + { dest [i] = 255 ; continue ; } ; #endif #if CPU_CLIPS_NEGATIVE == 0 if (scaled_value <= (-8.0 * 0x10000000)) - { dest [count] = 0 ; + { dest [i] = 0 ; continue ; } ; #endif - dest [count] = (psf_lrint (src [count] * normfact) >> 24) + 128 ; + dest [i] = (psf_lrint (src [i] * normfact) >> 24) + 128 ; } ; } /* d2uc_clip_array */ @@ -2503,11 +2492,10 @@ d2bes_array (const double *src, short *dest, int count, int normalize) double normfact ; normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ; - ucptr = ((unsigned char*) dest) + 2 * count ; - while (--count >= 0) - { ucptr -= 2 ; - value = psf_lrint (src [count] * normfact) ; + for (int i = 0 ; i < count ; i++) + { ucptr = (unsigned char*) &dest [i] ; + value = psf_lrint (src [i] * normfact) ; ucptr [1] = value ; ucptr [0] = value >> 8 ; } ; @@ -2520,11 +2508,10 @@ d2bes_clip_array (const double *src, short *dest, int count, int normalize) int value ; normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x10000) ; - ucptr = ((unsigned char*) dest) + 2 * count ; - while (--count >= 0) - { ucptr -= 2 ; - scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { ucptr = (unsigned char*) &dest [i] ; + scaled_value = src [i] * normfact ; #if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) { ucptr [1] = 0xFF ; @@ -2580,11 +2567,10 @@ d2les_array (const double *src, short *dest, int count, int normalize) double normfact ; normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ; - ucptr = ((unsigned char*) dest) + 2 * count ; - while (--count >= 0) - { ucptr -= 2 ; - value = psf_lrint (src [count] * normfact) ; + for (int i = 0 ; i < count ; i++) + { ucptr = (unsigned char*) &dest [i] ; + value = psf_lrint (src [i] * normfact) ; ucptr [0] = value ; ucptr [1] = value >> 8 ; } ; @@ -2597,11 +2583,10 @@ d2les_clip_array (const double *src, short *dest, int count, int normalize) double normfact, scaled_value ; normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x10000) ; - ucptr = ((unsigned char*) dest) + 2 * count ; - while (--count >= 0) - { ucptr -= 2 ; - scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { ucptr = (unsigned char*) &dest [i] ; + scaled_value = src [i] * normfact ; #if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) { ucptr [0] = 0xFF ; @@ -2657,11 +2642,11 @@ d2let_array (const double *src, tribyte *dest, int count, int normalize) normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ; - while (--count >= 0) - { value = psf_lrint (src [count] * normfact) ; - dest [count].bytes [0] = value ; - dest [count].bytes [1] = value >> 8 ; - dest [count].bytes [2] = value >> 16 ; + for (int i = 0 ; i < count ; i++) + { value = psf_lrint (src [i] * normfact) ; + dest [i].bytes [0] = value ; + dest [i].bytes [1] = value >> 8 ; + dest [i].bytes [2] = value >> 16 ; } ; } /* d2let_array */ @@ -2672,29 +2657,29 @@ d2let_clip_array (const double *src, tribyte *dest, int count, int normalize) normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x100) ; - while (--count >= 0) - { scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { scaled_value = src [i] * normfact ; #if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) - { dest [count].bytes [0] = 0xFF ; - dest [count].bytes [1] = 0xFF ; - dest [count].bytes [2] = 0x7F ; + { dest [i].bytes [0] = 0xFF ; + dest [i].bytes [1] = 0xFF ; + dest [i].bytes [2] = 0x7F ; continue ; } ; #endif #if CPU_CLIPS_NEGATIVE == 0 if (scaled_value <= (-8.0 * 0x10000000)) - { dest [count].bytes [0] = 0x00 ; - dest [count].bytes [1] = 0x00 ; - dest [count].bytes [2] = 0x80 ; + { dest [i].bytes [0] = 0x00 ; + dest [i].bytes [1] = 0x00 ; + dest [i].bytes [2] = 0x80 ; continue ; } ; #endif value = psf_lrint (scaled_value) ; - dest [count].bytes [0] = value >> 8 ; - dest [count].bytes [1] = value >> 16 ; - dest [count].bytes [2] = value >> 24 ; + dest [i].bytes [0] = value >> 8 ; + dest [i].bytes [1] = value >> 16 ; + dest [i].bytes [2] = value >> 24 ; } ; } /* d2let_clip_array */ @@ -2732,11 +2717,11 @@ d2bet_array (const double *src, tribyte *dest, int count, int normalize) normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ; - while (--count >= 0) - { value = psf_lrint (src [count] * normfact) ; - dest [count].bytes [2] = value ; - dest [count].bytes [1] = value >> 8 ; - dest [count].bytes [0] = value >> 16 ; + for (int i = 0 ; i < count ; i++) + { value = psf_lrint (src [i] * normfact) ; + dest [i].bytes [2] = value ; + dest [i].bytes [1] = value >> 8 ; + dest [i].bytes [0] = value >> 16 ; } ; } /* d2bet_array */ @@ -2747,29 +2732,29 @@ d2bet_clip_array (const double *src, tribyte *dest, int count, int normalize) normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x100) ; - while (--count >= 0) - { scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { scaled_value = src [i] * normfact ; #if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) - { dest [count].bytes [2] = 0xFF ; - dest [count].bytes [1] = 0xFF ; - dest [count].bytes [0] = 0x7F ; + { dest [i].bytes [2] = 0xFF ; + dest [i].bytes [1] = 0xFF ; + dest [i].bytes [0] = 0x7F ; continue ; } ; #endif #if CPU_CLIPS_NEGATIVE == 0 if (scaled_value <= (-8.0 * 0x10000000)) - { dest [count].bytes [2] = 0x00 ; - dest [count].bytes [1] = 0x00 ; - dest [count].bytes [0] = 0x80 ; + { dest [i].bytes [2] = 0x00 ; + dest [i].bytes [1] = 0x00 ; + dest [i].bytes [0] = 0x80 ; continue ; } ; #endif value = psf_lrint (scaled_value) ; - dest [count].bytes [2] = value >> 8 ; - dest [count].bytes [1] = value >> 16 ; - dest [count].bytes [0] = value >> 24 ; + dest [i].bytes [2] = value >> 8 ; + dest [i].bytes [1] = value >> 16 ; + dest [i].bytes [0] = value >> 24 ; } ; } /* d2bet_clip_array */ @@ -2807,11 +2792,10 @@ d2bei_array (const double *src, int *dest, int count, int normalize) double normfact ; normfact = normalize ? (1.0 * 0x7FFFFFFF) : 1.0 ; - ucptr = ((unsigned char*) dest) + 4 * count ; - while (--count >= 0) - { ucptr -= 4 ; - value = psf_lrint (src [count] * normfact) ; + for (int i = 0 ; i < count ; i++) + { ucptr = (unsigned char*) &dest [i] ; + value = psf_lrint (src [i] * normfact) ; ucptr [0] = value >> 24 ; ucptr [1] = value >> 16 ; ucptr [2] = value >> 8 ; @@ -2826,11 +2810,10 @@ d2bei_clip_array (const double *src, int *dest, int count, int normalize) double normfact, scaled_value ; normfact = normalize ? (8.0 * 0x10000000) : 1.0 ; - ucptr = ((unsigned char*) dest) + 4 * count ; - while (--count >= 0) - { ucptr -= 4 ; - scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { ucptr = (unsigned char*) &dest [i] ; + scaled_value = src [i] * normfact ; #if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) { ucptr [3] = 0xFF ; @@ -2892,11 +2875,10 @@ d2lei_array (const double *src, int *dest, int count, int normalize) double normfact ; normfact = normalize ? (1.0 * 0x7FFFFFFF) : 1.0 ; - ucptr = ((unsigned char*) dest) + 4 * count ; - while (--count >= 0) - { ucptr -= 4 ; - value = psf_lrint (src [count] * normfact) ; + for (int i = 0 ; i < count ; i++) + { ucptr = (unsigned char*) &dest [i] ; + value = psf_lrint (src [i] * normfact) ; ucptr [0] = value ; ucptr [1] = value >> 8 ; ucptr [2] = value >> 16 ; @@ -2911,11 +2893,10 @@ d2lei_clip_array (const double *src, int *dest, int count, int normalize) double normfact, scaled_value ; normfact = normalize ? (8.0 * 0x10000000) : 1.0 ; - ucptr = ((unsigned char*) dest) + 4 * count ; - while (--count >= 0) - { ucptr -= 4 ; - scaled_value = src [count] * normfact ; + for (int i = 0 ; i < count ; i++) + { ucptr = (unsigned char*) &dest [i] ; + scaled_value = src [i] * normfact ; #if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) { ucptr [0] = 0xFF ; diff --git a/src/sfendian.h b/src/sfendian.h index b0c0cd65..ab02742b 100644 --- a/src/sfendian.h +++ b/src/sfendian.h @@ -282,34 +282,34 @@ psf_get_be16 (const uint8_t *ptr, int offset) static inline void endswap_short_array (short *ptr, int len) { - while (--len >= 0) - { short temp = ptr [len] ; - ptr [len] = ENDSWAP_16 (temp) ; + for (int i = 0 ; i < len ; i++) + { short temp = ptr [i] ; + ptr [i] = ENDSWAP_16 (temp) ; } ; } /* endswap_short_array */ static inline void endswap_short_copy (short *dest, const short *src, int len) { - while (--len >= 0) - { dest [len] = ENDSWAP_16 (src [len]) ; + for (int i = 0 ; i < len ; i++) + { dest [i] = ENDSWAP_16 (src [i]) ; } ; } /* endswap_short_copy */ static inline void endswap_int_array (int *ptr, int len) { - while (--len >= 0) - { int temp = ptr [len] ; - ptr [len] = ENDSWAP_32 (temp) ; + for (int i = 0 ; i < len ; i++) + { int temp = ptr [i] ; + ptr [i] = ENDSWAP_32 (temp) ; } ; } /* endswap_int_array */ static inline void endswap_int_copy (int *dest, const int *src, int len) { - while (--len >= 0) - { dest [len] = ENDSWAP_32 (src [len]) ; + for (int i = 0 ; i < len ; i++) + { dest [i] = ENDSWAP_32 (src [i]) ; } ; } /* endswap_int_copy */ @@ -319,18 +319,18 @@ endswap_int_copy (int *dest, const int *src, int len) static inline void endswap_int64_t_array (int64_t *ptr, int len) { - while (--len >= 0) - { int64_t value = ptr [len] ; - ptr [len] = ENDSWAP_64 (value) ; + for (int i = 0 ; i < len ; i++) + { int64_t value = ptr [i] ; + ptr [i] = ENDSWAP_64 (value) ; } ; } /* endswap_int64_t_array */ static inline void endswap_int64_t_copy (int64_t *dest, const int64_t *src, int len) { - while (--len >= 0) - { int64_t value = src [len] ; - dest [len] = ENDSWAP_64 (value) ; + for (int i = 0 ; i < len ; i++) + { int64_t value = src [i] ; + dest [i] = ENDSWAP_64 (value) ; } ; } /* endswap_int64_t_copy */ diff --git a/src/ulaw.c b/src/ulaw.c index 69d38bd8..ed2f8ff0 100644 --- a/src/ulaw.c +++ b/src/ulaw.c @@ -793,69 +793,69 @@ unsigned char ulaw_encode [8193] = static inline void ulaw2s_array (unsigned char *buffer, int count, short *ptr) -{ while (--count >= 0) - ptr [count] = ulaw_decode [(int) buffer [count]] ; +{ for (int i = 0 ; i < count ; i++) + ptr [i] = ulaw_decode [(int) buffer [i]] ; } /* ulaw2s_array */ static inline void ulaw2i_array (unsigned char *buffer, int count, int *ptr) -{ while (--count >= 0) - ptr [count] = ((uint32_t) ulaw_decode [buffer [count]]) << 16 ; +{ for (int i = 0 ; i < count ; i++) + ptr [i] = ((uint32_t) ulaw_decode [buffer [i]]) << 16 ; } /* ulaw2i_array */ static inline void ulaw2f_array (unsigned char *buffer, int count, float *ptr, float normfact) -{ while (--count >= 0) - ptr [count] = normfact * ulaw_decode [(int) buffer [count]] ; +{ for (int i = 0 ; i < count ; i++) + ptr [i] = normfact * ulaw_decode [(int) buffer [i]] ; } /* ulaw2f_array */ static inline void ulaw2d_array (const unsigned char *buffer, int count, double *ptr, double normfact) -{ while (--count >= 0) - ptr [count] = normfact * ulaw_decode [(int) buffer [count]] ; +{ for (int i = 0 ; i < count ; i++) + ptr [i] = normfact * ulaw_decode [(int) buffer [i]] ; } /* ulaw2d_array */ static inline void s2ulaw_array (const short *ptr, int count, unsigned char *buffer) -{ while (--count >= 0) - { if (ptr [count] >= 0) - buffer [count] = ulaw_encode [ptr [count] / 4] ; +{ for (int i = 0 ; i < count ; i++) + { if (ptr [i] >= 0) + buffer [i] = ulaw_encode [ptr [i] / 4] ; else - buffer [count] = 0x7F & ulaw_encode [ptr [count] / -4] ; + buffer [i] = 0x7F & ulaw_encode [ptr [i] / -4] ; } ; } /* s2ulaw_array */ static inline void i2ulaw_array (const int *ptr, int count, unsigned char *buffer) -{ while (--count >= 0) - { if (ptr [count] == INT_MIN) - buffer [count] = ulaw_encode [INT_MAX >> (16 + 2)] ; - else if (ptr [count] >= 0) - buffer [count] = ulaw_encode [ptr [count] >> (16 + 2)] ; +{ for (int i = 0 ; i < count ; i++) + { if (ptr [i] == INT_MIN) + buffer [i] = ulaw_encode [INT_MAX >> (16 + 2)] ; + else if (ptr [i] >= 0) + buffer [i] = ulaw_encode [ptr [i] >> (16 + 2)] ; else - buffer [count] = 0x7F & ulaw_encode [-ptr [count] >> (16 + 2)] ; + buffer [i] = 0x7F & ulaw_encode [-ptr [i] >> (16 + 2)] ; } ; } /* i2ulaw_array */ static inline void f2ulaw_array (const float *ptr, int count, unsigned char *buffer, float normfact) -{ while (--count >= 0) - { if (ptr [count] >= 0) - buffer [count] = ulaw_encode [psf_lrintf (normfact * ptr [count])] ; +{ for (int i = 0 ; i < count ; i++) + { if (ptr [i] >= 0) + buffer [i] = ulaw_encode [psf_lrintf (normfact * ptr [i])] ; else - buffer [count] = 0x7F & ulaw_encode [- psf_lrintf (normfact * ptr [count])] ; + buffer [i] = 0x7F & ulaw_encode [- psf_lrintf (normfact * ptr [i])] ; } ; } /* f2ulaw_array */ static inline void d2ulaw_array (const double *ptr, int count, unsigned char *buffer, double normfact) -{ while (--count >= 0) - { if (!isfinite (ptr [count])) - buffer [count] = 0 ; - else if (ptr [count] >= 0) - buffer [count] = ulaw_encode [psf_lrint (normfact * ptr [count])] ; +{ for (int i = 0 ; i < count ; i++) + { if (!isfinite (ptr [i])) + buffer [i] = 0 ; + else if (ptr [i] >= 0) + buffer [i] = ulaw_encode [psf_lrint (normfact * ptr [i])] ; else - buffer [count] = 0x7F & ulaw_encode [- psf_lrint (normfact * ptr [count])] ; + buffer [i] = 0x7F & ulaw_encode [- psf_lrint (normfact * ptr [i])] ; } ; } /* d2ulaw_array */ From 86c9f9eb7022d186ad4d0689487e7d4f04ce2b29 Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Sun, 4 Jul 2021 21:48:15 -0700 Subject: [PATCH 115/204] mpeg: decode cleanups of copyright, comments, copyright, unused --- src/mpeg_decode.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/mpeg_decode.c b/src/mpeg_decode.c index b2c8fb61..dd236999 100644 --- a/src/mpeg_decode.c +++ b/src/mpeg_decode.c @@ -1,6 +1,6 @@ /* +** Copyright (C) 2019 - 2021 Arthur Taylor ** Copyright (C) 2019 Erik de Castro Lopo -** Copyright (C) 2019 Arthur Taylor ** ** This program is free software ; you can redistribute it and/or modify ** it under the terms of the GNU Lesser General Public License as published by @@ -35,7 +35,6 @@ typedef struct { mpg123_handle *pmh ; size_t header_remaining ; - unsigned unique_id ; } MPEG_DEC_PRIVATE ; static int mpeg_dec_close (SF_PRIVATE *psf) ; @@ -437,27 +436,27 @@ mpeg_decoder_read_strings_id3v2 (SF_PRIVATE *psf, mpg123_id3v2 *tags) text_frame->description.p, text_frame->text.p) ; } ; - if (title) + if (title != NULL) psf_store_string (psf, SF_STR_TITLE, title) ; - if (copyright) + if (copyright != NULL) psf_store_string (psf, SF_STR_COPYRIGHT, copyright) ; - if (software) + if (software != NULL) psf_store_string (psf, SF_STR_SOFTWARE, software) ; - if (artist) + if (artist != NULL) psf_store_string (psf, SF_STR_ARTIST, artist) ; - if (comment) + if (comment != NULL) psf_store_string (psf, SF_STR_COMMENT, comment) ; - if (date) + if (date != NULL) psf_store_string (psf, SF_STR_DATE, date) ; - if (album) + if (album != NULL) psf_store_string (psf, SF_STR_ALBUM, album) ; - if (license) + if (license != NULL) psf_store_string (psf, SF_STR_LICENSE, license) ; - if (tracknumber) + if (tracknumber != NULL) psf_store_string (psf, SF_STR_TRACKNUMBER, tracknumber) ; - if (genre) + if (genre != NULL) psf_store_string (psf, SF_STR_GENRE, id3_process_v2_genre (genre)) ; - if (tlen) + if (tlen != NULL) { /* If non-seekable, set framecount? Can we trust it? */ } ; } /* mpeg_decoder_read_strings_id3v2 */ @@ -520,6 +519,10 @@ mpeg_decoder_init (SF_PRIVATE *psf) ** > tables are filled with the same values anyway. ** ** Note that calling mpg123_init() after it has already completed is a NOP. + ** + ** Update 2021-07-04 + ** mpg123 upstream has confirmed that mpg132_init() will become a NOP in future, + ** so this is moot. */ if (mpg123_init () != MPG123_OK) return SFE_INTERNAL ; @@ -541,9 +544,9 @@ mpeg_decoder_init (SF_PRIVATE *psf) mpg123_param (pmp3d->pmh, MPG123_REMOVE_FLAGS, MPG123_AUTO_RESAMPLE, 1.0) ; mpg123_param (pmp3d->pmh, MPG123_ADD_FLAGS, MPG123_FORCE_FLOAT | MPG123_GAPLESS, 1.0) ; - #if MPG123_API_VERSION >= 45 - mpg123_param (pmp3d->pmh, MPG123_ADD_FLAGS, MPG123_NO_FRANKENSTEIN, 1.0) ; - #endif +#if MPG123_API_VERSION >= 45 + mpg123_param (pmp3d->pmh, MPG123_ADD_FLAGS, MPG123_NO_FRANKENSTEIN, 1.0) ; +#endif psf->dataoffset = 0 ; /* @@ -612,7 +615,6 @@ mpeg_decoder_init (SF_PRIVATE *psf) else psf->datalength = SF_COUNT_MAX ; - pmp3d->unique_id = psf->unique_id ; return 0 ; } /* mpeg_decoder_init */ @@ -642,4 +644,4 @@ int mpeg_decoder_init (SF_PRIVATE *psf) return SFE_UNIMPLEMENTED ; } /* mpeg_decoder_init */ -#endif +#endif /* HAVE_MPEG */ From 3ef35eea25f384d7a9e23c03e599992675cf48e2 Mon Sep 17 00:00:00 2001 From: evpobr Date: Fri, 9 Jul 2021 14:38:10 +0500 Subject: [PATCH 116/204] sndfile.h: Fix get/set bitrate commands values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Numeric values of SFC_GET_BITRATE_MODE and SFC_SET_BITRATE_MODE commands matched the values ​​of commands SFC_SET_OGG_PAGE_LATENCY_MS and SFC_SET_OGG_PAGE_LATENCY. --- include/sndfile.h.in | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/sndfile.h.in b/include/sndfile.h.in index 2f07c2e7..2e658ff1 100644 --- a/include/sndfile.h.in +++ b/include/sndfile.h.in @@ -223,8 +223,8 @@ enum SFC_SET_OGG_PAGE_LATENCY_MS = 0x1302, SFC_SET_OGG_PAGE_LATENCY = 0x1303, - SFC_GET_BITRATE_MODE = 0x1302, - SFC_SET_BITRATE_MODE = 0x1303, + SFC_GET_BITRATE_MODE = 0x1303, + SFC_SET_BITRATE_MODE = 0x1304, /* Cart Chunk support */ SFC_SET_CART_INFO = 0x1400, From dc1c8c2492a776c60360fa0054978ad0352aace6 Mon Sep 17 00:00:00 2001 From: evpobr Date: Sat, 17 Jul 2021 09:48:43 +0500 Subject: [PATCH 117/204] sndfile.h: Fix (really) get/set bitrate commands values --- include/sndfile.h.in | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/sndfile.h.in b/include/sndfile.h.in index 2e658ff1..2bcbdb1f 100644 --- a/include/sndfile.h.in +++ b/include/sndfile.h.in @@ -223,8 +223,8 @@ enum SFC_SET_OGG_PAGE_LATENCY_MS = 0x1302, SFC_SET_OGG_PAGE_LATENCY = 0x1303, - SFC_GET_BITRATE_MODE = 0x1303, - SFC_SET_BITRATE_MODE = 0x1304, + SFC_GET_BITRATE_MODE = 0x1304, + SFC_SET_BITRATE_MODE = 0x1305, /* Cart Chunk support */ SFC_SET_CART_INFO = 0x1400, From 110e26d9c2c96b65b85c74f7ac7230cadcc7656a Mon Sep 17 00:00:00 2001 From: DavidKorczynski Date: Sun, 15 Aug 2021 05:38:48 +0100 Subject: [PATCH 118/204] Add new fuzzer for OSS-Fuzz (#769) Adds a fuzzer targeting sf_read_double, sf_read_int and sf_read_short. Co-authored-by: evpobr --- CHANGELOG.md | 1 + Makefile.am | 8 +- ossfuzz/ossfuzz.sh | 1 + ossfuzz/sndfile_alt_fuzzer.cc | 79 +++++++++++++++++ ossfuzz/sndfile_fuzz_header.h | 119 +++++++++++++++++++++++++ ossfuzz/sndfile_fuzzer.cc | 158 +++++----------------------------- 6 files changed, 228 insertions(+), 138 deletions(-) create mode 100644 ossfuzz/sndfile_alt_fuzzer.cc create mode 100644 ossfuzz/sndfile_fuzz_header.h diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b21205a..c8d79a56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * ID3v1 reading * ID3v2 reading * Seeking +* New fuzzer for OSS-Fuzz, thanks @DavidKorczynski. * This `CHANGELOG.md`. All notable changes to this project will be documented in this file. The old `NEWS` file has been renamed to `NEWS.OLD` and is no longer updated. diff --git a/Makefile.am b/Makefile.am index b1f77571..77ab8aaf 100644 --- a/Makefile.am +++ b/Makefile.am @@ -462,7 +462,8 @@ endif if USE_OSSFUZZERS noinst_PROGRAMS += \ - ossfuzz/sndfile_fuzzer + ossfuzz/sndfile_fuzzer \ + ossfuzz/sndfile_alt_fuzzer noinst_LTLIBRARIES += \ ossfuzz/libstandaloneengine.la @@ -473,6 +474,11 @@ ossfuzz_sndfile_fuzzer_CXXFLAGS = $(AM_CXXFLAGS) $(FUZZ_FLAG) ossfuzz_sndfile_fuzzer_LDFLAGS = $(AM_LDFLAGS) -static ossfuzz_sndfile_fuzzer_LDADD = src/libsndfile.la $(FUZZ_LDADD) +ossfuzz_sndfile_alt_fuzzer_SOURCES = ossfuzz/sndfile_alt_fuzzer.cc +ossfuzz_sndfile_alt_fuzzer_CXXFLAGS = $(AM_CXXFLAGS) $(FUZZ_FLAG) +ossfuzz_sndfile_alt_fuzzer_LDFLAGS = $(AM_LDFLAGS) -static +ossfuzz_sndfile_alt_fuzzer_LDADD = src/libsndfile.la $(FUZZ_LDADD) + ossfuzz_libstandaloneengine_la_SOURCES = ossfuzz/standaloneengine.cc ossfuzz/testinput.h ossfuzz_libstandaloneengine_la_CXXFLAGS = $(AM_CXXFLAGS) diff --git a/ossfuzz/ossfuzz.sh b/ossfuzz/ossfuzz.sh index b93d4fcc..a4d98d93 100755 --- a/ossfuzz/ossfuzz.sh +++ b/ossfuzz/ossfuzz.sh @@ -29,3 +29,4 @@ make V=1 # Copy the fuzzer to the output directory. cp -v ossfuzz/sndfile_fuzzer $OUT/ +cp -v ossfuzz/sndfile_alt_fuzzer $OUT/ diff --git a/ossfuzz/sndfile_alt_fuzzer.cc b/ossfuzz/sndfile_alt_fuzzer.cc new file mode 100644 index 00000000..e69fcdf3 --- /dev/null +++ b/ossfuzz/sndfile_alt_fuzzer.cc @@ -0,0 +1,79 @@ +#include +#include +#include +#include +#include + +#include "sndfile_fuzz_header.h" + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ // One byte is needed for deciding which function to target. + if (size == 0) + return 0 ; + + const uint8_t decider = *data ; + data += 1 ; + size -= 1 ; + + SF_INFO sndfile_info ; + VIO_DATA vio_data ; + SF_VIRTUAL_IO vio ; + SNDFILE *sndfile = NULL ; + int err = sf_init_file(data, size, &sndfile, &vio_data, &vio, &sndfile_info) ; + if (err) + goto EXIT_LABEL ; + + // Just the right number of channels. Create some buffer space for reading. + switch (decider % 3) + { case 0 : + { + short* read_buffer = NULL ; + read_buffer = (short*)malloc(sizeof(short) * size); + if (read_buffer == NULL) + abort() ; + + while (sf_read_short(sndfile, read_buffer, size)) + { + // Do nothing with the data. + } + free(read_buffer) ; + } + break ; + case 1 : + { + int* read_buffer = NULL ; + read_buffer = (int*)malloc(sizeof(int) * size) ; + if (read_buffer == NULL) + abort() ; + + while (sf_read_int(sndfile, read_buffer, size)) + { + // Do nothing with the data. + } + free(read_buffer) ; + } + break ; + case 2 : + { + double* read_buffer = NULL ; + read_buffer = (double*)malloc(sizeof(double) * size) ; + if (read_buffer == NULL) + abort() ; + + while (sf_read_double(sndfile, read_buffer, size)) + { + // Do nothing with the data. + } + free(read_buffer) ; + } + break ; + default : + break ; + } ; + + EXIT_LABEL: + if (sndfile != NULL) + sf_close(sndfile); + + return 0 ; +} diff --git a/ossfuzz/sndfile_fuzz_header.h b/ossfuzz/sndfile_fuzz_header.h new file mode 100644 index 00000000..898aec44 --- /dev/null +++ b/ossfuzz/sndfile_fuzz_header.h @@ -0,0 +1,119 @@ +#ifndef SNDFILE_FUZZ_HEADER_H +#define SNDFILE_FUZZ_HEADER_H + +typedef struct +{ + sf_count_t offset ; + sf_count_t length ; + const unsigned char *data ; +} VIO_DATA ; + +static sf_count_t vfget_filelen (void *user_data) +{ VIO_DATA *vf = (VIO_DATA *)user_data ; + return vf->length ; +} + +static sf_count_t vfseek (sf_count_t offset, int whence, void *user_data) +{ + VIO_DATA *vf = (VIO_DATA *)user_data ; + sf_count_t new_offset ; + + switch (whence) + { case SEEK_SET : + new_offset = offset ; + break ; + + case SEEK_CUR : + new_offset = vf->offset + offset ; + break ; + + case SEEK_END : + new_offset = vf->length + offset ; + break ; + + default : + break ; + } + + /* Ensure you can't seek outside the data */ + if (new_offset > vf->length) + { /* Trying to seek past the end of the data */ + printf("vf overseek: new_offset(%" PRId64 ") > vf->length(%" PRId64 ");" + " whence(%d), vf->offset(%" PRId64 "), offset(%" PRId64 ")\n", + new_offset, vf->length, whence, vf->offset, offset) ; + new_offset = vf->length ; + } + else if (new_offset < 0) + { /* Trying to seek before the start of the data */ + printf("vf underseek: new_offset(%" PRId64 ") < 0; whence(%d), vf->offset" + "(%" PRId64 "), vf->length(%" PRId64 "), offset(%" PRId64 ")\n", + new_offset, whence, vf->offset, vf->length, offset) ; + new_offset = 0 ; + } + vf->offset = new_offset ; + + return vf->offset ; +} + +static sf_count_t vfread (void *ptr, sf_count_t count, void *user_data) +{ VIO_DATA *vf = (VIO_DATA *)user_data ; + + if (vf->offset + count > vf->length) + count = vf->length - vf->offset ; + + memcpy(ptr, vf->data + vf->offset, count) ; + vf->offset += count ; + + return count ; +} + +static sf_count_t vfwrite (const void *ptr, sf_count_t count, void *user_data) +{ + (void)ptr ; + (void)count ; + (void)user_data ; + + // Cannot write to this virtual file. + return 0; +} + +static sf_count_t vftell (void *user_data) +{ VIO_DATA *vf = (VIO_DATA *)user_data ; + + return vf->offset ; +} + +int sf_init_file(const uint8_t *data, + size_t size, + SNDFILE **sndfile, + VIO_DATA *vio_data, + SF_VIRTUAL_IO *vio, SF_INFO *sndfile_info) +{ float* read_buffer = NULL ; + + // Initialize the virtual IO structure. + vio->get_filelen = vfget_filelen ; + vio->seek = vfseek ; + vio->read = vfread ; + vio->write = vfwrite ; + vio->tell = vftell ; + + // Initialize the VIO user data. + vio_data->data = data ; + vio_data->length = size ; + vio_data->offset = 0 ; + + memset(sndfile_info, 0, sizeof(SF_INFO)) ; + + // Try and open the virtual file. + *sndfile = sf_open_virtual(vio, SFM_READ, sndfile_info, vio_data) ; + + if (sndfile_info->channels == 0) + return -1 ; + + if (sndfile_info->channels > 1024 * 1024) + return -1 ; + + return 0; +} + +#endif diff --git a/ossfuzz/sndfile_fuzzer.cc b/ossfuzz/sndfile_fuzzer.cc index 3c850730..2a3d6934 100644 --- a/ossfuzz/sndfile_fuzzer.cc +++ b/ossfuzz/sndfile_fuzzer.cc @@ -5,151 +5,35 @@ #include #include -typedef struct -{ - sf_count_t offset; - sf_count_t length; - const unsigned char *data; -} VIO_DATA; - -static sf_count_t vfget_filelen (void *user_data) -{ - VIO_DATA *vf = (VIO_DATA *)user_data; - return vf->length; -} - -static sf_count_t vfseek (sf_count_t offset, int whence, void *user_data) -{ - VIO_DATA *vf = (VIO_DATA *)user_data; - sf_count_t new_offset; - - switch (whence) - { - case SEEK_SET: - new_offset = offset; - break ; - - case SEEK_CUR: - new_offset = vf->offset + offset; - break ; - - case SEEK_END: - new_offset = vf->length + offset; - break; - - default: - break; - } - - /* Ensure you can't seek outside the data */ - if (new_offset > vf->length) - { - /* Trying to seek past the end of the data */ - printf("vf overseek: new_offset(%" PRId64 ") > vf->length(%" PRId64 ");" - " whence(%d), vf->offset(%" PRId64 "), offset(%" PRId64 ")\n", - new_offset, vf->length, whence, vf->offset, offset); - new_offset = vf->length; - } - else if (new_offset < 0) - { - /* Trying to seek before the start of the data */ - printf("vf underseek: new_offset(%" PRId64 ") < 0; whence(%d), vf->offset" - "(%" PRId64 "), vf->length(%" PRId64 "), offset(%" PRId64 ")\n", - new_offset, whence, vf->offset, vf->length, offset); - new_offset = 0; - } - vf->offset = new_offset; - - return vf->offset; -} - -static sf_count_t vfread (void *ptr, sf_count_t count, void *user_data) -{ - VIO_DATA *vf = (VIO_DATA *)user_data; - - if (vf->offset + count > vf->length) - { - count = vf->length - vf->offset; - } - - memcpy(ptr, vf->data + vf->offset, count); - vf->offset += count; - - return count; -} - -static sf_count_t vfwrite (const void *ptr, sf_count_t count, void *user_data) -{ - (void)ptr; - (void)count; - (void)user_data; - - // Cannot write to this virtual file. - return 0; -} - -static sf_count_t vftell (void *user_data) -{ VIO_DATA *vf = (VIO_DATA *)user_data; - - return vf->offset; -} +#include "sndfile_fuzz_header.h" extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) -{ - VIO_DATA vio_data; - SF_VIRTUAL_IO vio; - SF_INFO sndfile_info; - SNDFILE *sndfile = NULL; - float* read_buffer = NULL; +{ VIO_DATA vio_data ; + SF_VIRTUAL_IO vio ; + SF_INFO sndfile_info ; + SNDFILE *sndfile = NULL ; + float* read_buffer = NULL ; - // Initialize the virtual IO structure. - vio.get_filelen = vfget_filelen; - vio.seek = vfseek; - vio.read = vfread; - vio.write = vfwrite; - vio.tell = vftell; + int err = sf_init_file(data, size, &sndfile, &vio_data, &vio, &sndfile_info) ; + if (err) + goto EXIT_LABEL ; - // Initialize the VIO user data. - vio_data.data = data; - vio_data.length = size; - vio_data.offset = 0; + // Just the right number of channels. Create some buffer space for reading. + read_buffer = (float*)malloc(sizeof(float) * sndfile_info.channels); + if (read_buffer == NULL) + abort() ; - memset(&sndfile_info, 0, sizeof(SF_INFO)); - - // Try and open the virtual file. - sndfile = sf_open_virtual(&vio, SFM_READ, &sndfile_info, &vio_data); - - if (sndfile_info.channels == 0) - { - // No sound channels in file. - goto EXIT_LABEL; - } - else if (sndfile_info.channels > 1024 * 1024) - { - // Too many channels to handle. - goto EXIT_LABEL; - } - - // Just the right number of channels. Create some buffer space for reading. - read_buffer = (float*)malloc(sizeof(float) * sndfile_info.channels); - if (read_buffer == NULL) - { - abort(); - } - - while (sf_readf_float(sndfile, read_buffer, 1)) - { - // Do nothing with the data. - } + while (sf_readf_float(sndfile, read_buffer, 1)) + { + // Do nothing with the data. + } EXIT_LABEL: - if (sndfile != NULL) - { - sf_close(sndfile); - } + if (sndfile != NULL) + sf_close(sndfile) ; - free(read_buffer); + free(read_buffer) ; - return 0; + return 0 ; } From 497a177177d484f3c1448f05c4c37c2164fb159a Mon Sep 17 00:00:00 2001 From: Peter Lemenkov Date: Tue, 24 Aug 2021 11:46:47 +0200 Subject: [PATCH 119/204] docs: Fix link to the main repository Signed-off-by: Peter Lemenkov --- docs/development.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/development.md b/docs/development.md index c5b39de4..068e910a 100644 --- a/docs/development.md +++ b/docs/development.md @@ -9,7 +9,7 @@ interested in helping can raise an issue on Github. The main repository can be found on Github: - + and includes [instuctions](https://github.com/libsndfile/libsndfile/blob/master/README.md) on how to build libsndfile from the Git repo. From 9f5d66e91d7bc026fa08933d0c0da8c453980e6b Mon Sep 17 00:00:00 2001 From: evpobr Date: Tue, 24 Aug 2021 14:59:39 +0500 Subject: [PATCH 120/204] README.md: Fix link to the main repository --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ca52cc89..43a98cc5 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ for details. ## Hacking The canonical source code repository for libsndfile is at -[http://libsndfile.github.io/libsndfile/][github]. +. You can grab the source code using: From 04ae52469e5688ea5bda01079d7e9e3a8b55c61b Mon Sep 17 00:00:00 2001 From: evpobr Date: Tue, 31 Aug 2021 12:57:54 +0500 Subject: [PATCH 121/204] autotools: Fix missing external libs message Add libopus to make missing external libraries warning correct. --- configure.ac | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index 93f56439..bb2a27bc 100644 --- a/configure.ac +++ b/configure.ac @@ -391,8 +391,8 @@ AS_IF([test -n "$PKG_CONFIG"], [ fi ], [ AS_ECHO([""]) - AC_MSG_WARN([[*** One or more of the external libraries (ie libflac, libogg and]]) - AC_MSG_WARN([[*** libvorbis) is either missing (possibly only the development]]) + AC_MSG_WARN([[*** One or more of the external libraries (ie libflac, libogg,]]) + AC_MSG_WARN([[*** libvorbis and libopus) is either missing (possibly only the development]]) AC_MSG_WARN([[*** headers) or is of an unsupported version.]]) AC_MSG_WARN([[***]]) AC_MSG_WARN([[*** Unfortunately, for ease of maintenance, the external libs]]) From c284c0508aa0fc341455c905f1ae4e7018581ad3 Mon Sep 17 00:00:00 2001 From: evpobr Date: Tue, 31 Aug 2021 13:51:09 +0500 Subject: [PATCH 122/204] sndfile-loopify: Remove unused struct --- examples/sndfile-loopify.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/examples/sndfile-loopify.c b/examples/sndfile-loopify.c index 776d25ba..034f8e81 100644 --- a/examples/sndfile-loopify.c +++ b/examples/sndfile-loopify.c @@ -46,12 +46,6 @@ #define BUFFER_LEN (1 << 14) - -typedef struct -{ char *infilename, *outfilename ; - SF_INFO infileinfo, outfileinfo ; -} OptionData ; - const char * program_name (const char * argv0) ; static void sfe_copy_data_int (SNDFILE *outfile, SNDFILE *infile, int channels) ; static void add_instrument_data (SNDFILE *outfile, const SF_INFO * in_info) ; From b2998c9fc6c6033cd81365ecb7a653fd7304d320 Mon Sep 17 00:00:00 2001 From: evpobr Date: Tue, 31 Aug 2021 13:54:13 +0500 Subject: [PATCH 123/204] sndfile-loopify: Fix memory leak --- examples/sndfile-loopify.c | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/sndfile-loopify.c b/examples/sndfile-loopify.c index 034f8e81..12b6b6bb 100644 --- a/examples/sndfile-loopify.c +++ b/examples/sndfile-loopify.c @@ -99,6 +99,7 @@ main (int argc, char * argv []) /* Open the output file. */ if ((outfile = sf_open (outfilename, SFM_WRITE, &out_sfinfo)) == NULL) { printf ("Not able to open output file %s : %s\n", outfilename, sf_strerror (NULL)) ; + sf_close (infile) ; return 1 ; } ; From 0956676eb1c59a6228ac13b05326cd513f687a77 Mon Sep 17 00:00:00 2001 From: evpobr Date: Wed, 1 Sep 2021 10:01:43 +0500 Subject: [PATCH 124/204] sfprocess: Fix memory leaks --- examples/sfprocess.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/sfprocess.c b/examples/sfprocess.c index 2a98d2fa..ce1bda8a 100644 --- a/examples/sfprocess.c +++ b/examples/sfprocess.c @@ -98,12 +98,14 @@ main (void) if (sfinfo.channels > MAX_CHANNELS) { printf ("Not able to process more than %d channels\n", MAX_CHANNELS) ; + sf_close (infile) ; return 1 ; } ; /* Open the output file. */ if (! (outfile = sf_open (outfilename, SFM_WRITE, &sfinfo))) { printf ("Not able to open output file %s.\n", outfilename) ; puts (sf_strerror (NULL)) ; + sf_close (infile) ; return 1 ; } ; From 2421c8501e3e390b16ae48f4ac0b2d4c1cf6db63 Mon Sep 17 00:00:00 2001 From: evpobr Date: Wed, 1 Sep 2021 11:55:31 +0500 Subject: [PATCH 125/204] sndfile-info: Remove duplicate array --- programs/sndfile-info.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/programs/sndfile-info.c b/programs/sndfile-info.c index 8ddd9e07..ae446549 100644 --- a/programs/sndfile-info.c +++ b/programs/sndfile-info.c @@ -110,8 +110,6 @@ main (int argc, char *argv []) ** Print version and usage. */ -static double data [BUFFER_LEN] ; - static void usage_exit (const char *progname) { printf ("Usage :\n %s ...\n", progname) ; From 8aba5d7f5be278681d35f3c630d9d73359c36e49 Mon Sep 17 00:00:00 2001 From: David Seifert Date: Sun, 5 Sep 2021 15:01:07 +0200 Subject: [PATCH 126/204] Update `AC_HELP_STRING` -> `AS_HELP_STRING` --- m4/mkoctfile_version.m4 | 2 +- m4/octave.m4 | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/m4/mkoctfile_version.m4 b/m4/mkoctfile_version.m4 index c17333ea..04a58e57 100644 --- a/m4/mkoctfile_version.m4 +++ b/m4/mkoctfile_version.m4 @@ -16,7 +16,7 @@ AC_DEFUN([OCTAVE_MKOCTFILE_VERSION], AC_ARG_WITH(mkoctfile, - AC_HELP_STRING([--with-mkoctfile], [choose the mkoctfile version]), + AS_HELP_STRING([--with-mkoctfile], [choose the mkoctfile version]), [ with_mkoctfile=$withval ]) test -z "$with_mkoctfile" && with_mkoctfile=mkoctfile diff --git a/m4/octave.m4 b/m4/octave.m4 index 88d5a5b9..fda043d0 100644 --- a/m4/octave.m4 +++ b/m4/octave.m4 @@ -29,7 +29,7 @@ AC_DEFUN([AC_OCTAVE_VERSION], [ AC_ARG_WITH(octave, - AC_HELP_STRING([--with-octave], [choose the octave version]), + AS_HELP_STRING([--with-octave], [choose the octave version]), [ with_octave=$withval ]) test -z "$with_octave" && with_octave=octave @@ -63,7 +63,7 @@ AC_DEFUN([AC_OCTAVE_CONFIG_VERSION], [ AC_ARG_WITH(octave-config, - AC_HELP_STRING([--with-octave-config], [choose the octave-config version]), + AS_HELP_STRING([--with-octave-config], [choose the octave-config version]), [ with_octave_config=$withval ]) test -z "$with_octave_config" && with_octave_config=octave-config From 5e321c80cf62dc6924c9fed80af73998b78c9c7d Mon Sep 17 00:00:00 2001 From: David Seifert Date: Sun, 5 Sep 2021 15:01:08 +0200 Subject: [PATCH 127/204] Update `$as_echo_n` -> `AS_ECHO_N` --- m4/extra_pkg.m4 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/m4/extra_pkg.m4 b/m4/extra_pkg.m4 index ce4ecb8e..8a04c359 100644 --- a/m4/extra_pkg.m4 +++ b/m4/extra_pkg.m4 @@ -55,10 +55,10 @@ AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include ]], [puts ("")])], pkg_link= CFLAGS=$pkg_link_saved_CFLAGS LIBS=$pkg_link_saved_LIBS -if test $pkg_link = no ; then - $as_echo_n "link failed ... " +AS_IF([test x$pkg_link = xno], [ + AS_ECHO_N(["link failed ... "]) pkg_failed=yes - fi +]) m4_define([_PKG_TEXT], [Alternatively, you may set the environment variables $1[]_CFLAGS and $1[]_LIBS to avoid the need to call pkg-config. From 475ddd7a46230f961ebaa010088ced5cbd37267e Mon Sep 17 00:00:00 2001 From: David Seifert Date: Sun, 5 Sep 2021 15:01:09 +0200 Subject: [PATCH 128/204] Switch release tarballs `.bz2` -> `.xz` * `.bz2` = 0.86 MiB * `.xz` = 0.70 MiB by switching, we save nearly 20% --- .gitignore | 3 ++- CHANGELOG.md | 1 + configure.ac | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 91f3f47c..05f79a93 100644 --- a/.gitignore +++ b/.gitignore @@ -48,6 +48,7 @@ build-test-tarball.mk config.log config.status configure +configure~ doc/AUTHORS doc/ChangeLog doc/NEWS @@ -62,7 +63,7 @@ examples/sndfile-loopify examples/sndfile-to-text examples/sndfilehandle include/sndfile.h -libsndfile-1.0.* +libsndfile-*.tar.xz libsndfile-testsuite-* libsndfile.spec libtool diff --git a/CHANGELOG.md b/CHANGELOG.md index c8d79a56..32dbb654 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 The behavior of the functions for opening files on other platforms does not change. +* Switch to .xz over .bz2 for release tarballs. ### Fixed diff --git a/configure.ac b/configure.ac index bb2a27bc..cf937754 100644 --- a/configure.ac +++ b/configure.ac @@ -30,7 +30,7 @@ AC_CANONICAL_HOST AC_CONFIG_MACRO_DIR([m4]) AC_CONFIG_HEADERS([src/config.h]) -AM_INIT_AUTOMAKE([1.14 foreign dist-bzip2 no-dist-gzip serial-tests subdir-objects]) +AM_INIT_AUTOMAKE([1.14 foreign dist-xz no-dist-gzip serial-tests subdir-objects]) AM_SILENT_RULES([yes]) dnl ==================================================================================== From f1bfc717b1c46a6f8bbb636a30a9c314a567bff5 Mon Sep 17 00:00:00 2001 From: David Seifert Date: Sun, 5 Sep 2021 15:01:10 +0200 Subject: [PATCH 129/204] Remove `AC_HEADER_STDC` * No point in checking for C89 headers in 2021 Fixes #774 --- configure.ac | 2 -- 1 file changed, 2 deletions(-) diff --git a/configure.ac b/configure.ac index cf937754..23bee4a6 100644 --- a/configure.ac +++ b/configure.ac @@ -101,8 +101,6 @@ m4_define([abi_version_patch], [lt_revision]) dnl ------------------------------------------------------------------------------------ -AC_HEADER_STDC - AC_CHECK_HEADERS([endian.h]) AC_CHECK_HEADERS([byteswap.h]) AC_CHECK_HEADERS([locale.h]) From f65e8bc9d1f880e0834ceb7aa10738ba6de891ee Mon Sep 17 00:00:00 2001 From: papadave66 Date: Wed, 8 Sep 2021 16:05:15 +0800 Subject: [PATCH 130/204] Fix duplicated case value of ESTRPIPE on BSD --- programs/sndfile-play.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/programs/sndfile-play.c b/programs/sndfile-play.c index cd5cc278..c2818476 100644 --- a/programs/sndfile-play.c +++ b/programs/sndfile-play.c @@ -328,10 +328,12 @@ alsa_write_float (snd_pcm_t *alsa_dev, float *data, int frames, int channels) return 0 ; break ; +#if defined ESTRPIPE && ESTRPIPE != EPIPE case -ESTRPIPE : fprintf (stderr, "alsa_write_float: Suspend event.n") ; return 0 ; break ; +#endif case -EIO : puts ("alsa_write_float: EIO") ; From 3ead8d226694cd765cf1aaa7ba6e02b698f0eaba Mon Sep 17 00:00:00 2001 From: nia <29542929+alarixnia@users.noreply.github.com> Date: Tue, 21 Sep 2021 12:15:14 +0000 Subject: [PATCH 131/204] sndfile-play: Add NetBSD support (#776) * sndfile-play: Use Sun-compatible audio device on NetBSD Keep the gain unchanged. Avoids distortion from playing sounds too loudly and preserves the user's existing volume settings. --- man/sndfile-play.1 | 4 +++- programs/sndfile-play.c | 10 ++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/man/sndfile-play.1 b/man/sndfile-play.1 index f36d1315..4d570eaf 100644 --- a/man/sndfile-play.1 +++ b/man/sndfile-play.1 @@ -1,4 +1,4 @@ -.Dd November 2, 2014 +.Dd September 10, 2021 .Dt SNDFILE-PLAY 1 .Os .Sh NAME @@ -19,6 +19,8 @@ ALSA or OSS sndio .It FreeBSD /dev/dsp (OSS) +.It NetBSD +/dev/audio .It Solaris /dev/audio .It MacOSX 10.6 diff --git a/programs/sndfile-play.c b/programs/sndfile-play.c index c2818476..16aaf4d2 100644 --- a/programs/sndfile-play.c +++ b/programs/sndfile-play.c @@ -64,7 +64,7 @@ #elif HAVE_SNDIO_H #include -#elif (defined (sun) && defined (unix)) +#elif (defined (sun) && defined (unix)) || defined(__NetBSD__) #include #include #include @@ -729,7 +729,7 @@ sndio_play (int argc, char *argv []) ** Solaris. */ -#if (defined (sun) && defined (unix)) /* ie Solaris */ +#if (defined (sun) && defined (unix)) || defined(__NetBSD__) static void solaris_play (int argc, char *argv []) @@ -766,8 +766,6 @@ solaris_play (int argc, char *argv []) audio_info.play.channels = sfinfo.channels ; audio_info.play.precision = 16 ; audio_info.play.encoding = AUDIO_ENCODING_LINEAR ; - audio_info.play.gain = AUDIO_MAX_GAIN ; - audio_info.play.balance = AUDIO_MID_BALANCE ; if ((error = ioctl (audio_fd, AUDIO_SETINFO, &audio_info))) { perror ("ioctl (AUDIO_SETINFO) failed") ; @@ -809,7 +807,7 @@ solaris_play (int argc, char *argv []) return ; } /* solaris_play */ -#endif /* Solaris */ +#endif /* Solaris or NetBSD */ /*============================================================================== ** Main function. @@ -847,7 +845,7 @@ main (int argc, char *argv []) opensoundsys_play (argc, argv) ; #elif HAVE_SNDIO_H sndio_play (argc, argv) ; -#elif (defined (sun) && defined (unix)) +#elif (defined (sun) && defined (unix)) || defined(__NetBSD__) solaris_play (argc, argv) ; #elif (OS_IS_WIN32 == 1) win32_play (argc, argv) ; From d5a036e11a5716c4cd1b5a21ff9d97a7082758bf Mon Sep 17 00:00:00 2001 From: David Seifert <16636962+SoapGentoo@users.noreply.github.com> Date: Tue, 21 Sep 2021 14:51:52 +0200 Subject: [PATCH 132/204] configure.ac: Remove stack smash protection (#778) * This is something deployers should enable if they want it. --- configure.ac | 9 ------ m4/stack_protect.m4 | 73 --------------------------------------------- 2 files changed, 82 deletions(-) delete mode 100644 m4/stack_protect.m4 diff --git a/configure.ac b/configure.ac index 23bee4a6..75a0e3d0 100644 --- a/configure.ac +++ b/configure.ac @@ -141,9 +141,6 @@ AC_DEFINE_UNQUOTED([ENABLE_EXPERIMENTAL_CODE], [${EXPERIMENTAL_CODE}], [Set to 1 AC_ARG_ENABLE([werror], [AS_HELP_STRING([--enable-werror], [enable -Werror in all Makefiles])]) -AC_ARG_ENABLE([stack-smash-protection], - [AS_HELP_STRING([--enable-stack-smash-protection], [Enable GNU GCC stack smash protection])]) - AC_ARG_ENABLE([cpu-clip], [AS_HELP_STRING([--disable-cpu-clip], [disable tricky cpu specific clipper])]) @@ -643,11 +640,6 @@ AC_LANG_PUSH([C++]) AX_APPEND_COMPILE_FLAGS([${common_flags} -Wctor-dtor-privacy -Wnon-virtual-dtor -Woverloaded-virtual -Wreorder -Wsign-promo], [CXXFLAGS]) AC_LANG_POP([C++]) -AS_IF([test "x$enable_stack_smash_protection" = "xyes"], [ - XIPH_GCC_STACK_PROTECTOR - XIPH_GXX_STACK_PROTECTOR - ]) - AS_IF([test "x$enable_test_coverage" = "xyes"], [ AX_APPEND_COMPILE_FLAGS([-coverage], [CFLAGS]) ]) @@ -795,7 +787,6 @@ AC_MSG_RESULT([ CXX Compiler Vendor is : .............. ${ax_cv_cxx_compiler_vendor} (${ax_cv_cxx_compiler_version}) Sanitizer enabled : ................... ${enable_sanitizer:-no} - Stack smash protection : .............. ${enable_stack_smash_protection:-no} Installation directories : diff --git a/m4/stack_protect.m4 b/m4/stack_protect.m4 deleted file mode 100644 index bf27e6e7..00000000 --- a/m4/stack_protect.m4 +++ /dev/null @@ -1,73 +0,0 @@ -dnl Copyright (C) 2013 Xiph.org Foundation -dnl -dnl Redistribution and use in source and binary forms, with or without -dnl modification, are permitted provided that the following conditions -dnl are met: -dnl -dnl - Redistributions of source code must retain the above copyright -dnl notice, this list of conditions and the following disclaimer. -dnl -dnl - Redistributions in binary form must reproduce the above copyright -dnl notice, this list of conditions and the following disclaimer in the -dnl documentation and/or other materials provided with the distribution. -dnl -dnl - Neither the name of the Xiph.org Foundation nor the names of its -dnl contributors may be used to endorse or promote products derived from -dnl this software without specific prior written permission. -dnl -dnl THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -dnl ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -dnl LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -dnl A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR -dnl CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -dnl EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -dnl PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -dnl PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -dnl LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -dnl NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -dnl SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -dnl Want to know of GCC stack protector works, botfor the C and for the C++ -dnl compiler. -dnl -dnl Just checking if the compiler accepts the required CFLAGSs is not enough -dnl because we have seen at least one instance where this check was -dnl in-sufficient. -dnl -dnl Instead, try to compile and link a test program with the stack protector -dnl flags. If that works, we use it. - -AC_DEFUN([XIPH_GCC_STACK_PROTECTOR], -[AC_LANG_ASSERT(C) - AC_MSG_CHECKING([if $CC supports stack smash protection]) - xiph_stack_check_old_cflags="$CFLAGS" - SSP_FLAGS="-fstack-protector --param ssp-buffer-size=4" - CFLAGS=$SSP_FLAGS - AC_TRY_LINK([ - #include - ], - [puts("Hello, World!"); return 0;], - AC_MSG_RESULT([yes]) - CFLAGS="$xiph_stack_check_old_cflags $SSP_FLAGS", - AC_MSG_RESULT([no]) - CFLAGS="$xiph_stack_check_old_cflags" - ) -])# XIPH_GCC_STACK_PROTECTOR - -AC_DEFUN([XIPH_GXX_STACK_PROTECTOR], -[AC_LANG_PUSH([C++]) - AC_MSG_CHECKING([if $CXX supports stack smash protection]) - xiph_stack_check_old_cflags="$CFLAGS" - SSP_FLAGS="-fstack-protector --param ssp-buffer-size=4" - CFLAGS=$SSP_FLAGS - AC_TRY_LINK([ - #include - ], - [puts("Hello, World!"); return 0;], - AC_MSG_RESULT([yes]) - CFLAGS="$xiph_stack_check_old_cflags $SSP_FLAGS", - AC_MSG_RESULT([no]) - CFLAGS="$xiph_stack_check_old_cflags" - ) - AC_LANG_POP([C++]) -])# XIPH_GXX_STACK_PROTECTOR From 444d9a4c8af0f2f4e0b35ed1cdf9203451853620 Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Thu, 25 Mar 2021 15:34:15 -0700 Subject: [PATCH 133/204] wav: Detect ID3v1 trailers, do not parse as a chunk --- src/wav.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/wav.c b/src/wav.c index 7ee94570..47f09cf9 100644 --- a/src/wav.c +++ b/src/wav.c @@ -73,6 +73,10 @@ #define wvpk_MARKER (MAKE_MARKER ('w', 'v', 'p', 'k')) #define OggS_MARKER (MAKE_MARKER ('O', 'g', 'g', 'S')) +/* ID3v1 trailer which can show up at the end and erronerously look like a chunk. */ +#define TAG__MARKER (MAKE_MARKER ('T', 'A', 'G', 0)) +#define TAG__MARKER_MASK (MAKE_MARKER (0xff, 0xff, 0xff, 0)) + #define WAVLIKE_PEAK_CHUNK_SIZE(ch) (2 * sizeof (int) + ch * (sizeof (float) + sizeof (int))) @@ -605,6 +609,15 @@ wav_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock) break ; } ; + if ((marker & TAG__MARKER_MASK) == TAG__MARKER && + psf_ftell (psf) - 8 + 128 == psf->filelength) + { psf_log_printf (psf, "*** Hit ID3v1 trailer. Exiting parser.\n") ; + chunk_size = 128 ; + done = SF_TRUE ; + parsestage |= HAVE_other ; + break ; + } ; + if (psf_isprint ((marker >> 24) & 0xFF) && psf_isprint ((marker >> 16) & 0xFF) && psf_isprint ((marker >> 8) & 0xFF) && psf_isprint (marker & 0xFF)) { psf_log_printf (psf, "*** %M : %u (unknown marker)\n", marker, chunk_size) ; From 1a5d40cf213d38a95d2de68c58a23dd1bcb59de3 Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Thu, 25 Mar 2021 15:37:42 -0700 Subject: [PATCH 134/204] mpeg: Bug fix for mpeg encode, using frame_size before encoder init. --- src/mpeg_l3_encode.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/mpeg_l3_encode.c b/src/mpeg_l3_encode.c index 9b9186aa..97324f79 100644 --- a/src/mpeg_l3_encode.c +++ b/src/mpeg_l3_encode.c @@ -371,6 +371,7 @@ mpeg_l3_encoder_log_config (SF_PRIVATE *psf, lame_t lamef) default : chn_mode = "unknown!?" ; break ; } ; psf_log_printf (psf, " MPEG Version : %s\n", version) ; + psf_log_printf (psf, " Block samples : %d\n", lame_get_framesize (lamef)) ; psf_log_printf (psf, " Channel mode : %s\n", chn_mode) ; psf_log_printf (psf, " Samplerate : %d\n", lame_get_out_samplerate (lamef)) ; psf_log_printf (psf, " Encoder mode : ") ; @@ -496,11 +497,11 @@ mpeg_l3_encode_write_short_stereo (SF_PRIVATE *psf, const short *ptr, sf_count_t MPEG_L3_ENC_PRIVATE *pmpeg = (MPEG_L3_ENC_PRIVATE*) psf->codec_data ; sf_count_t total = 0 ; int nbytes, writecount, writen ; - const sf_count_t max_samples = SF_MIN (ARRAY_LEN (ubuf.sbuf), pmpeg->frame_samples) ; if ((psf->error = mpeg_l3_encoder_construct (psf))) return 0 ; + const sf_count_t max_samples = SF_MIN (ARRAY_LEN (ubuf.sbuf), pmpeg->frame_samples) ; while (len) { writecount = SF_MIN (len, max_samples) ; /* @@ -645,11 +646,11 @@ mpeg_l3_encode_write_float_stereo (SF_PRIVATE *psf, const float *ptr, sf_count_t MPEG_L3_ENC_PRIVATE *pmpeg = (MPEG_L3_ENC_PRIVATE*) psf->codec_data ; sf_count_t total = 0 ; int nbytes, writecount, writen ; - const sf_count_t max_samples = SF_MIN (ARRAY_LEN (ubuf.fbuf), pmpeg->frame_samples) ; if ((psf->error = mpeg_l3_encoder_construct (psf))) return 0 ; + const sf_count_t max_samples = SF_MIN (ARRAY_LEN (ubuf.fbuf), pmpeg->frame_samples) ; while (len) { writecount = SF_MIN (len, max_samples) ; @@ -695,11 +696,11 @@ mpeg_l3_encode_write_double_mono (SF_PRIVATE *psf, const double *ptr, sf_count_t MPEG_L3_ENC_PRIVATE *pmpeg = (MPEG_L3_ENC_PRIVATE*) psf->codec_data ; sf_count_t total = 0 ; int nbytes, writecount, writen ; - const sf_count_t max_samples = SF_MIN (ARRAY_LEN (ubuf.dbuf), pmpeg->frame_samples) ; if ((psf->error = mpeg_l3_encoder_construct (psf))) return 0 ; + const sf_count_t max_samples = SF_MIN (ARRAY_LEN (ubuf.dbuf), pmpeg->frame_samples) ; while (len) { writecount = SF_MIN (len, max_samples) ; @@ -737,11 +738,11 @@ mpeg_l3_encode_write_double_stereo (SF_PRIVATE *psf, const double *ptr, sf_count MPEG_L3_ENC_PRIVATE *pmpeg = (MPEG_L3_ENC_PRIVATE*) psf->codec_data ; sf_count_t total = 0 ; int nbytes, writecount, writen ; - const sf_count_t max_samples = SF_MIN (ARRAY_LEN (ubuf.dbuf), pmpeg->frame_samples) ; if ((psf->error = mpeg_l3_encoder_construct (psf))) return 0 ; + const sf_count_t max_samples = SF_MIN (ARRAY_LEN (ubuf.dbuf), pmpeg->frame_samples) ; while (len) { writecount = SF_MIN (len, max_samples) ; From 294eb05633aa8b23de4e0bb29644602eaa175fc2 Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Thu, 25 Mar 2021 15:40:02 -0700 Subject: [PATCH 135/204] mpeg: Heed dataoffset to avoid passing RIFF header when reading from pipes. --- src/mpeg_decode.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/mpeg_decode.c b/src/mpeg_decode.c index dd236999..78b5d282 100644 --- a/src/mpeg_decode.c +++ b/src/mpeg_decode.c @@ -548,7 +548,6 @@ mpeg_decoder_init (SF_PRIVATE *psf) mpg123_param (pmp3d->pmh, MPG123_ADD_FLAGS, MPG123_NO_FRANKENSTEIN, 1.0) ; #endif - psf->dataoffset = 0 ; /* ** Need to pass the first MPEG frame to libmpg123, but that frame was read ** into psf->binheader in order that we could identify the stream. @@ -558,8 +557,8 @@ mpeg_decoder_init (SF_PRIVATE *psf) ** Can't seek, so setup our libmpg123 io callbacks to read the binheader ** buffer first. */ - psf_binheader_readf (psf, "p", 0) ; - pmp3d->header_remaining = psf_binheader_readf (psf, NULL) ; + psf_binheader_readf (psf, "p", psf->dataoffset) ; + pmp3d->header_remaining = psf_binheader_readf (psf, NULL) - psf->dataoffset ; /* Tell libmpg123 we can't seek the file. */ mpg123_param (pmp3d->pmh, MPG123_ADD_FLAGS, MPG123_NO_PEEK_END, 1.0) ; From c42e41324b639e54ebede4c77d43f75d6b1354a3 Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Thu, 25 Mar 2021 15:43:00 -0700 Subject: [PATCH 136/204] wavlike: Add fmt MPEGLAYER3 See Microsoft's documentation for . --- src/wavlike.c | 21 ++++++++++++++++++++- src/wavlike.h | 16 ++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/wavlike.c b/src/wavlike.c index 1c1ed2cf..a3388d75 100644 --- a/src/wavlike.c +++ b/src/wavlike.c @@ -173,6 +173,7 @@ wavlike_read_fmt_chunk (SF_PRIVATE *psf, int fmtsize) { switch (wav_fmt->format) { case WAVE_FORMAT_GSM610 : case WAVE_FORMAT_IPP_ITU_G_723_1 : + case WAVE_FORMAT_MPEGLAYER3 : psf_log_printf (psf, " Bit Width : %d\n", wav_fmt->min.bitwidth) ; break ; default : @@ -183,7 +184,8 @@ wavlike_read_fmt_chunk (SF_PRIVATE *psf, int fmtsize) { switch (wav_fmt->format) { case WAVE_FORMAT_GSM610 : case WAVE_FORMAT_IPP_ITU_G_723_1 : - psf_log_printf (psf, " Bit Width : %d (should be 0)\n", wav_fmt->min.bitwidth) ; + case WAVE_FORMAT_MPEGLAYER3 : + psf_log_printf (psf, " Bit Width : %d (should be 0)\n", wav_fmt->min.bitwidth) ; break ; default : psf_log_printf (psf, " Bit Width : %d\n", wav_fmt->min.bitwidth) ; @@ -306,6 +308,23 @@ wavlike_read_fmt_chunk (SF_PRIVATE *psf, int fmtsize) psf_log_printf (psf, " Samples/Block : %d\n", wav_fmt->gsm610.samplesperblock) ; break ; + case WAVE_FORMAT_MPEGLAYER3 : + bytesread += psf_binheader_readf (psf, "24222", &(wav_fmt->mpeg3.extrabytes), + &(wav_fmt->mpeg3.id), &(wav_fmt->mpeg3.flags), &(wav_fmt->mpeg3.blocksize), + &(wav_fmt->mpeg3.samplesperblock), &(wav_fmt->mpeg3.codecdelay)) ; + + psf_log_printf (psf, " Bytes/sec : %d\n", wav_fmt->mpeg3.bytespersec) ; + psf_log_printf (psf, " Extra Bytes : %d\n", wav_fmt->mpeg3.extrabytes) ; + if (wav_fmt->mpeg3.id != 1) + psf_log_printf (psf, " ID : %d (unknown, should be 1)\n", wav_fmt->mpeg3.id) ; + else + psf_log_printf (psf, " ID : MPEGLAYER3_ID_MPEG\n") ; + psf_log_printf (psf, " Flags : 0x%08x\n", wav_fmt->mpeg3.flags) ; + psf_log_printf (psf, " Block Size : %d\n", wav_fmt->mpeg3.blocksize) ; + psf_log_printf (psf, " Samples/Block : %d\n", wav_fmt->mpeg3.samplesperblock) ; + psf_log_printf (psf, " Codec Delay : %d samples\n", wav_fmt->mpeg3.codecdelay) ; + break ; + case WAVE_FORMAT_EXTENSIBLE : if (wav_fmt->ext.bytespersec != wav_fmt->ext.samplerate * wav_fmt->ext.blockalign) psf_log_printf (psf, " Bytes/sec : %d (should be %d)\n", wav_fmt->ext.bytespersec, wav_fmt->ext.samplerate * wav_fmt->ext.blockalign) ; diff --git a/src/wavlike.h b/src/wavlike.h index 4cde1e93..686287d8 100644 --- a/src/wavlike.h +++ b/src/wavlike.h @@ -263,6 +263,21 @@ typedef struct unsigned short samplesperblock ; } GSM610_WAV_FMT ; +typedef struct +{ unsigned short format ; + unsigned short channels ; + unsigned int samplerate ; + unsigned int bytespersec ; + unsigned short blockalign ; + unsigned short bitwidth ; + unsigned short extrabytes ; + unsigned short id ; + unsigned int flags ; + unsigned short blocksize ; + unsigned short samplesperblock ; + unsigned short codecdelay ; +} MPEGLAYER3_WAV_FMT ; + typedef struct { unsigned int esf_field1 ; unsigned short esf_field2 ; @@ -291,6 +306,7 @@ typedef union G72x_ADPCM_WAV_FMT g72x ; EXTENSIBLE_WAV_FMT ext ; GSM610_WAV_FMT gsm610 ; + MPEGLAYER3_WAV_FMT mpeg3 ; WAV_FMT_SIZE20 size20 ; char padding [512] ; } WAV_FMT ; From 4b8209e650d5b4de2843870943ae7fb37d9b4823 Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Thu, 25 Mar 2021 16:01:08 -0700 Subject: [PATCH 137/204] wav: Add R/W support for MPEG_LAYER_III subformat in wav --- src/sndfile.c | 2 ++ src/wav.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/sndfile.c b/src/sndfile.c index 365c1361..25177230 100644 --- a/src/sndfile.c +++ b/src/sndfile.c @@ -691,6 +691,8 @@ sf_format_check (const SF_INFO *info) if ((subformat == SF_FORMAT_NMS_ADPCM_16 || subformat == SF_FORMAT_NMS_ADPCM_24 || subformat == SF_FORMAT_NMS_ADPCM_32) && info->channels == 1) return 1 ; + if (subformat == SF_FORMAT_MPEG_LAYER_III && info->channels <= 2) + return 1 ; break ; case SF_FORMAT_WAVEX : diff --git a/src/wav.c b/src/wav.c index 47f09cf9..12c0c254 100644 --- a/src/wav.c +++ b/src/wav.c @@ -91,7 +91,6 @@ enum } ; - /* known WAVEFORMATEXTENSIBLE GUIDS */ static const EXT_SUBFORMAT MSGUID_SUBTYPE_PCM = { 0x00000001, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 } @@ -278,6 +277,10 @@ wav_open (SF_PRIVATE *psf) error = gsm610_init (psf) ; break ; + case SF_FORMAT_MPEG_LAYER_III : + error = mpeg_init (psf, SF_BITRATE_MODE_CONSTANT, SF_FALSE) ; + break ; + default : return SFE_UNIMPLEMENTED ; } ; @@ -696,8 +699,6 @@ wav_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock) break ; case WAVE_FORMAT_NMS_VBXADPCM : - *blockalign = wav_fmt->min.blockalign ; - *framesperblock = 160 ; switch (wav_fmt->min.bitwidth) { case 2 : psf->sf.format = SF_FORMAT_WAV | SF_FORMAT_NMS_ADPCM_16 ; @@ -753,6 +754,12 @@ wav_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock) psf->sf.format = SF_FORMAT_WAV | SF_FORMAT_G721_32 ; break ; + case WAVE_FORMAT_MPEGLAYER3 : + psf->sf.format = SF_FORMAT_WAV | SF_FORMAT_MPEG_LAYER_III ; + if (parsestage & HAVE_fact) + psf->sf.frames = fact_chunk.frames ; + break ; + default : return SFE_UNIMPLEMENTED ; } ; @@ -937,6 +944,57 @@ wav_write_fmt_chunk (SF_PRIVATE *psf) add_fact_chunk = SF_TRUE ; break ; + case SF_FORMAT_MPEG_LAYER_III : + { int bytespersec, blockalign, flags, blocksize, samplesperblock, codecdelay ; + + /* Intended to be set as the average sample rate. + ** TODO: Maybe re-write this on close with final average + ** byterate? */ + bytespersec = psf->byterate (psf) ; + + /* Average block size. Info only I think. */ + blocksize = (1152 * bytespersec) / psf->sf.samplerate ; + + /* Can be set to block size IFF the block size is + ** constant, set to 1 otherwise. Constant sized + ** MPEG block streams are uncommon (CBR @ 32kHz and + ** 48kHz only. Meh. */ + blockalign = 1 ; + + /* TODO: Only flags defined are padding-type. I /think/ + ** Lame does ISO style padding by default, which has a + ** flag value of 0. + */ + flags = 0 ; + + /* Should only vary per MPEG 1.0/2.0 vs '2.5'. + ** TODO: Move this out to MPEG specific place? */ + samplesperblock = psf->sf.samplerate >= 32000 ? 1152 : 576 ; + + /* Set as 0 if unknown. + ** TODO: Plumb this cleanly from Lame. + */ + codecdelay = 0 ; + + /* fmt chunk. */ + fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 + 4 + 2 + 2 + 2 ; + + /* fmt : size, WAV format type, channels. */ + psf_binheader_writef (psf, "422", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_MPEGLAYER3), BHW2 (psf->sf.channels)) ; + + /* fmt : samplerate, bytespersec. */ + psf_binheader_writef (psf, "44", BHW4 (psf->sf.samplerate), BHW4 (bytespersec)) ; + + /* fmt : blockalign, bitwidth, extrabytes, id. */ + psf_binheader_writef (psf, "2222", BHW2 (blockalign), BHW2 (0), BHW2 (12), BHW2 (1)) ; + + /* fmt : flags, blocksize, samplesperblock, codecdelay */ + psf_binheader_writef (psf, "4222", BHW4 (flags), BHW2 (blocksize), BHW2 (samplesperblock), BHW2 (codecdelay)) ; + } ; + + add_fact_chunk = SF_TRUE ; + break ; + default : return SFE_UNIMPLEMENTED ; } ; From d1c87ec1c749b6da0070e9010a6c18bedcdc7356 Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Wed, 20 Oct 2021 17:26:34 -0700 Subject: [PATCH 138/204] mpeg: Disable experimental writing MPEG subformats in WAV --- src/wav.c | 10 ++++++++++ tests/format_check_test.c | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/src/wav.c b/src/wav.c index 12c0c254..0dd6c730 100644 --- a/src/wav.c +++ b/src/wav.c @@ -207,6 +207,14 @@ wav_open (SF_PRIVATE *psf) psf->sf.frames = 0 ; } ; +#if (ENABLE_EXPERIMENTAL_CODE == 0) + /* For now, don't support writing MPEGLAYER3 WAVs, as we can't guarentee that + ** such a file written by libsndfile would have the same length when opened again. + */ + if (subformat == SF_FORMAT_MPEG_LAYER_III) + return SFE_UNSUPPORTED_ENCODING ; +#endif + if (subformat == SF_FORMAT_IMA_ADPCM || subformat == SF_FORMAT_MS_ADPCM) { blockalign = wavlike_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ; framesperblock = -1 ; /* Corrected later. */ @@ -944,6 +952,7 @@ wav_write_fmt_chunk (SF_PRIVATE *psf) add_fact_chunk = SF_TRUE ; break ; +#if (ENABLE_EXPERIMENTAL_CODE == 0) case SF_FORMAT_MPEG_LAYER_III : { int bytespersec, blockalign, flags, blocksize, samplesperblock, codecdelay ; @@ -994,6 +1003,7 @@ wav_write_fmt_chunk (SF_PRIVATE *psf) add_fact_chunk = SF_TRUE ; break ; +#endif default : return SFE_UNIMPLEMENTED ; } ; diff --git a/tests/format_check_test.c b/tests/format_check_test.c index eda557a4..2e76679a 100644 --- a/tests/format_check_test.c +++ b/tests/format_check_test.c @@ -126,6 +126,11 @@ format_combo_test (void) subtype_fmt_info.format == SF_FORMAT_MPEG_LAYER_II) continue ; + /* MPEG Layer III in WAV is decode only currently */ + if (subtype_fmt_info.format == SF_FORMAT_MPEG_LAYER_III && + major_fmt_info.format == SF_FORMAT_WAV) + continue ; + snprintf (filename, sizeof (filename), "format-check.%s", major_fmt_info.extension) ; sndfile = sf_open (filename, SFM_WRITE, &info) ; From e09e5dd043fcb32f380a0bc2a66390378058d254 Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Wed, 20 Oct 2021 17:27:03 -0700 Subject: [PATCH 139/204] mpeg: Move dataoffset check from codec to format init --- src/mpeg.c | 6 ++++++ src/mpeg_decode.c | 5 ----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/mpeg.c b/src/mpeg.c index fe13e917..44db0adf 100644 --- a/src/mpeg.c +++ b/src/mpeg.c @@ -137,6 +137,12 @@ mpeg_open (SF_PRIVATE *psf) psf->dataoffset = 0 ; psf->command = mpeg_command ; + if (psf->filelength != SF_COUNT_MAX) + psf->datalength = psf->filelength - psf->dataoffset ; + else + psf->datalength = SF_COUNT_MAX ; + + return 0 ; } /* mpeg_open */ diff --git a/src/mpeg_decode.c b/src/mpeg_decode.c index 78b5d282..20fac4f9 100644 --- a/src/mpeg_decode.c +++ b/src/mpeg_decode.c @@ -609,11 +609,6 @@ mpeg_decoder_init (SF_PRIVATE *psf) mpeg_decoder_read_strings (psf) ; - if (psf->filelength != SF_COUNT_MAX) - psf->datalength = psf->filelength - psf->dataoffset ; - else - psf->datalength = SF_COUNT_MAX ; - return 0 ; } /* mpeg_decoder_init */ From aaac071f6083d7e8c5fa47fa7aaa8a6a03181cf0 Mon Sep 17 00:00:00 2001 From: David Seifert Date: Sun, 24 Oct 2021 13:37:08 +0200 Subject: [PATCH 140/204] update CHANGELOG.md for MPEG-III-in-wav --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32dbb654..17e576a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * This `CHANGELOG.md`. All notable changes to this project will be documented in this file. The old `NEWS` file has been renamed to `NEWS.OLD` and is no longer updated. +* Add support for decoding MPEG III Audio in WAV files. ### Changed From 4b64fd767a1ecdd33291551f91146745a853eeb8 Mon Sep 17 00:00:00 2001 From: David Seifert Date: Sun, 24 Oct 2021 14:26:31 +0200 Subject: [PATCH 141/204] programs/sndfile-deinterleave.c: fix broken calloc call Fixes #783 --- programs/sndfile-deinterleave.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/programs/sndfile-deinterleave.c b/programs/sndfile-deinterleave.c index 26d4a3ec..42d47509 100644 --- a/programs/sndfile-deinterleave.c +++ b/programs/sndfile-deinterleave.c @@ -77,7 +77,7 @@ main (int argc, char **argv) goto cleanup ; } ; - state = calloc (1, sizeof (state)) ; + state = calloc (1, sizeof (*state)) ; if (!state) { printf ("\nError : Out of memory.\n") ; goto cleanup ; From 1c407fe8234c242588c778355c2e8f74c41f0d58 Mon Sep 17 00:00:00 2001 From: David Seifert Date: Sun, 24 Oct 2021 14:53:11 +0200 Subject: [PATCH 142/204] libtool: don't build static archives by default --- CHANGELOG.md | 2 ++ configure.ac | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17e576a1..34082245 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,6 +65,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 The behavior of the functions for opening files on other platforms does not change. * Switch to .xz over .bz2 for release tarballs. +* Disable static builds using Autotools by default. If you want static + libraries, pass --enable-static to ./configure ### Fixed diff --git a/configure.ac b/configure.ac index 75a0e3d0..8e0e191e 100644 --- a/configure.ac +++ b/configure.ac @@ -57,7 +57,7 @@ AC_LANG_POP([C++]) AC_PROG_SED AM_PROG_AR -LT_INIT([win32-dll]) +LT_INIT([disable-static win32-dll]) LT_PROG_RC AC_PROG_INSTALL From f1495b4bcc4f7f0fedb10119e6b5b5b2f1946d4b Mon Sep 17 00:00:00 2001 From: David Seifert Date: Sun, 24 Oct 2021 14:53:12 +0200 Subject: [PATCH 143/204] Release 1.1.0beta2 Fixes #784 --- CMakeLists.txt | 2 +- configure.ac | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 48ba905e..fe50f6e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,7 @@ set (PACKAGE_NAME ${PROJECT_NAME}) set (CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR}) set (CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR}) set (CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH}) -set (CPACK_PACKAGE_VERSION_STAGE "beta1") +set (CPACK_PACKAGE_VERSION_STAGE "beta2") set (CPACK_PACKAGE_VERSION_FULL "${PROJECT_VERSION}${CPACK_PACKAGE_VERSION_STAGE}") # diff --git a/configure.ac b/configure.ac index 8e0e191e..34b44409 100644 --- a/configure.ac +++ b/configure.ac @@ -3,7 +3,7 @@ dnl Copyright (C) 1999-2021 Erik de Castro Lopo . dnl Require autoconf version >= 2.69 AC_PREREQ([2.69]) -AC_INIT([libsndfile],[1.1.0beta1],[sndfile@mega-nerd.com], +AC_INIT([libsndfile],[1.1.0beta2],[sndfile@mega-nerd.com], [libsndfile],[http://libsndfile.github.io/libsndfile/]) dnl Check whether we want to set defaults for CFLAGS, CXXFLAGS, CPPFLAGS and LDFLAGS @@ -88,7 +88,7 @@ dnl to 0. dnl This is libtool version of library, we add it to `--version-info` property. m4_define([lt_current], [1]) -m4_define([lt_revision], [32]) +m4_define([lt_revision], [33]) m4_define([lt_age], [0]) dnl This is ABI version for linker scripts, CMake uses the same format for From 404bf9af791cb60813af382ec8090423d4a0ad86 Mon Sep 17 00:00:00 2001 From: evpobr Date: Wed, 3 Nov 2021 12:33:24 +0500 Subject: [PATCH 144/204] Fix integer overflow in psf_log_printf() https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=28441 Credit to OSS-Fuzz. --- CHANGELOG.md | 2 ++ src/common.c | 43 +++++++++++++++++++++++++++---------------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 34082245..4b4b6bbd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -88,6 +88,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([issue 27366](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=27366)). * Integer overflow in `nms_adpcm_update`(), credit to OSS-Fuzz ([issue 25522](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=25522)). +* Integer overflow in `psf_log_printf`(), credit to OSS-Fuzz + ([issue 28441](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=28441)). * ABI version incompatibility between Autotools and CMake build on Apple platforms. diff --git a/src/common.c b/src/common.c index 5f09ec0e..f236ca63 100644 --- a/src/common.c +++ b/src/common.c @@ -103,8 +103,8 @@ log_putchar (SF_PRIVATE *psf, char ch) void psf_log_printf (SF_PRIVATE *psf, const char *format, ...) { va_list ap ; - uint32_t u ; - int d, tens, shift, width, width_specifier, left_align, slen, precision ; + uint32_t u, tens ; + int d, shift, width, width_specifier, left_align, slen, precision ; char c, *strptr, istr [5], lead_char, sign_char ; va_start (ap, format) ; @@ -186,15 +186,19 @@ psf_log_printf (SF_PRIVATE *psf, const char *format, ...) d = va_arg (ap, int) ; if (d < 0) - { d = -d ; - sign_char = '-' ; + { sign_char = '-' ; if (lead_char != '0' && left_align == SF_FALSE) width_specifier -- ; - } ; + + u = - ((unsigned) d) ; + } + else + { u = (unsigned) d ; + } tens = 1 ; width = 1 ; - while (d / tens >= 10) + while (u / tens >= 10) { tens *= 10 ; width ++ ; } ; @@ -224,8 +228,8 @@ psf_log_printf (SF_PRIVATE *psf, const char *format, ...) log_putchar (psf, lead_char) ; while (tens > 0) - { log_putchar (psf, '0' + d / tens) ; - d %= tens ; + { log_putchar (psf, '0' + u / tens) ; + u %= tens ; tens /= 10 ; } ; @@ -234,7 +238,8 @@ psf_log_printf (SF_PRIVATE *psf, const char *format, ...) break ; case 'D': /* sf_count_t */ - { sf_count_t D, Tens ; + { sf_count_t D ; + uint64_t U, Tens ; D = va_arg (ap, sf_count_t) ; @@ -244,13 +249,19 @@ psf_log_printf (SF_PRIVATE *psf, const char *format, ...) log_putchar (psf, '0') ; break ; } - if (D < 0) - { log_putchar (psf, '-') ; - D = -D ; - } ; + else + { if (D < 0) + { log_putchar (psf, '-') ; + U = -((uint64_t) D) ; + } + else + { U = (uint64_t) D; + } + } + Tens = 1 ; width = 1 ; - while (D / Tens >= 10) + while (U / Tens >= 10) { Tens *= 10 ; width ++ ; } ; @@ -261,8 +272,8 @@ psf_log_printf (SF_PRIVATE *psf, const char *format, ...) } ; while (Tens > 0) - { log_putchar (psf, '0' + D / Tens) ; - D %= Tens ; + { log_putchar (psf, '0' + U / Tens) ; + U %= Tens ; Tens /= 10 ; } ; } ; From b1307c77551b82f89653868c037c5eea5e89b316 Mon Sep 17 00:00:00 2001 From: evpobr Date: Tue, 9 Nov 2021 14:39:33 +0500 Subject: [PATCH 145/204] ci: Remove yasm-tools for x64-windows workaround --- .github/workflows/action.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index 7e51374b..029388cb 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -189,10 +189,6 @@ jobs: if: startsWith(matrix.os,'ubuntu') run: sudo apt-get update -y && sudo apt-get install -y autogen ninja-build libogg-dev libvorbis-dev libflac-dev libopus-dev libasound2-dev libsqlite3-dev libspeex-dev libmp3lame-dev libmpg123-dev - - name: Workaround vcpkg yasm-tools for x64-windows - if: (startsWith(matrix.os, 'windows') && startsWith(matrix.triplet, 'x64-windows')) - run: vcpkg install yasm-tool:x86-windows - - name: Install Windows dependencies if: startsWith(matrix.os,'windows') run: | From c7b69d7589d70de3765d0aab15dad8d6b307d3c7 Mon Sep 17 00:00:00 2001 From: evpobr Date: Tue, 9 Nov 2021 21:01:06 +0500 Subject: [PATCH 146/204] Update CHANGELOG.md Commit 404bf9a also fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=25624. --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b4b6bbd..3ca90fb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -89,7 +89,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Integer overflow in `nms_adpcm_update`(), credit to OSS-Fuzz ([issue 25522](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=25522)). * Integer overflow in `psf_log_printf`(), credit to OSS-Fuzz - ([issue 28441](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=28441)). + ([issue 28441](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=28441)), + ([issue 25624](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=25624)). * ABI version incompatibility between Autotools and CMake build on Apple platforms. From 7e2db6e8ef160ef3aa9f2df4e5205cd3e9cc270e Mon Sep 17 00:00:00 2001 From: evpobr Date: Sun, 24 Oct 2021 15:32:58 +0500 Subject: [PATCH 147/204] Create SECURITY.md --- CHANGELOG.md | 2 ++ Makefile.am | 2 +- SECURITY.md | 11 +++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 SECURITY.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ca90fb0..9eacc3d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 this file. The old `NEWS` file has been renamed to `NEWS.OLD` and is no longer updated. * Add support for decoding MPEG III Audio in WAV files. +* `SECURITY.md` file to give people instructions for reporting security + vulnerabilities, thanks @zidingz. ### Changed diff --git a/Makefile.am b/Makefile.am index 77ab8aaf..d2bbdcbd 100644 --- a/Makefile.am +++ b/Makefile.am @@ -14,7 +14,7 @@ endif EXTRA_DIST = libsndfile.spec.in sndfile.pc.in Scripts/android-configure.sh \ NEWS.OLD CHANGELOG.md Scripts/linux-to-win-cross-configure.sh \ - CMakeLists.txt $(cmake_files) Win32 + CMakeLists.txt $(cmake_files) Win32 SECURITY.md cmake_files = cmake/ClipMode.cmake cmake/FindFLAC.cmake \ cmake/CMakeAutoGen.cmake cmake/CMakeAutoGenScript.cmake \ diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 00000000..c9e960dc --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,11 @@ +# Security Policy + +## Supported Versions + +| Version | Supported | +| ------- | ------------------ | +| 1.0.x | :white_check_mark: | + +## Reporting a Vulnerability + +Please send report privately to evpobry@gmail.com, and include how would you like to be credited. From 739bc8cdac8d56c2c5f097a50cbf20fa941ff59d Mon Sep 17 00:00:00 2001 From: evpobr Date: Thu, 18 Nov 2021 10:07:00 +0500 Subject: [PATCH 148/204] Fix unintialized variable in psf_binheader_readf() Thanks to @shao-hua-li --- CHANGELOG.md | 2 ++ src/common.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9eacc3d0..9faf4c64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -103,6 +103,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 See https://github.com/msys2/MINGW-packages/issues/5803 for details. +* Fix unintialized variable in psf_binheader_readf(), thanks @shao-hua-li + ### Security * Heap buffer overflow in `wavlike_ima_decode_block`(), thanks @bobsayshilol, diff --git a/src/common.c b/src/common.c index f236ca63..c4ef4a66 100644 --- a/src/common.c +++ b/src/common.c @@ -974,7 +974,7 @@ int psf_binheader_readf (SF_PRIVATE *psf, char const *format, ...) { va_list argptr ; sf_count_t *countptr, countdata ; - unsigned char *ucptr, sixteen_bytes [16] ; + unsigned char *ucptr, sixteen_bytes [16] = { 0 } ; unsigned int *intptr, intdata ; unsigned short *shortptr ; char *charptr ; From 0c10913b9260a263107244ca0ce85a84d2a3d3cc Mon Sep 17 00:00:00 2001 From: evpobr Date: Sun, 21 Nov 2021 14:18:47 +0500 Subject: [PATCH 149/204] Add support for Vcpkg manifest mode https://vcpkg.readthedocs.io/en/latest/users/manifests/ --- .github/workflows/action.yml | 5 ----- CHANGELOG.md | 4 ++++ CMakeLists.txt | 33 ++++++++++++++++++++++++++---- README.md | 31 +++++++++++++++++++--------- vcpkg.json | 39 ++++++++++++++++++++++++++++++++++++ 5 files changed, 94 insertions(+), 18 deletions(-) create mode 100644 vcpkg.json diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index 029388cb..6765eba4 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -189,11 +189,6 @@ jobs: if: startsWith(matrix.os,'ubuntu') run: sudo apt-get update -y && sudo apt-get install -y autogen ninja-build libogg-dev libvorbis-dev libflac-dev libopus-dev libasound2-dev libsqlite3-dev libspeex-dev libmp3lame-dev libmpg123-dev - - name: Install Windows dependencies - if: startsWith(matrix.os,'windows') - run: | - vcpkg install libvorbis libflac opus sqlite3 speex mp3lame mpg123 --triplet ${{matrix.triplet}} - - name: Configure, build and test with Autotools env: CC: ${{ matrix.cc }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 9faf4c64..f6ca5bbc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Add support for decoding MPEG III Audio in WAV files. * `SECURITY.md` file to give people instructions for reporting security vulnerabilities, thanks @zidingz. +* Support for [Vcpkg manifest mode](https://vcpkg.readthedocs.io/en/latest/users/manifests/). + + If you have problems with manifest mode, disable it with `VCPKG_MANIFEST_MODE` + switch. ### Changed diff --git a/CMakeLists.txt b/CMakeLists.txt index fe50f6e3..1a8562b0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,26 @@ if (POLICY CMP0091) endif () endif () +option (ENABLE_EXTERNAL_LIBS "Enable FLAC, Vorbis, and Opus codecs" ON) +if (ENABLE_EXTERNAL_LIBS) + list (APPEND VCPKG_MANIFEST_FEATURES "external-libs") +endif () + +option (ENABLE_MPEG "Enable MPEG codecs" ON) +if (ENABLE_MPEG) + list (APPEND VCPKG_MANIFEST_FEATURES "mpeg") +endif () + +option (ENABLE_EXPERIMENTAL "Enable experimental code" OFF) +if (ENABLE_EXPERIMENTAL) + list (APPEND VCPKG_MANIFEST_FEATURES "speex") +endif () + +option (BUILD_REGTEST "Build regtest" OFF) +if (BUILD_REGTEST) + list (APPEND VCPKG_MANIFEST_FEATURES "regtest") +endif () + project(libsndfile VERSION 1.1.0) # @@ -54,7 +74,6 @@ endif () option (BUILD_PROGRAMS "Build programs" ON) option (BUILD_EXAMPLES "Build examples" ON) option (ENABLE_CPACK "Enable CPack support" ON) -option (ENABLE_EXPERIMENTAL "Enable experimental code" OFF) option (ENABLE_BOW_DOCS "Enable black-on-white html docs" OFF) if (MSVC AND (DEFINED ENABLE_STATIC_RUNTIME)) option (ENABLE_STATIC_RUNTIME "Enable static runtime" ${ENABLE_STATIC_RUNTIME}) @@ -75,10 +94,16 @@ endif () include(SndFileChecks) +if (ENABLE_EXTERNAL_LIBS AND NOT (Vorbis_FOUND OR FLAC_FOUND OR OPUS_FOUND)) + set (ENABLE_EXTERNAL_LIBS OFF) +endif() +if(ENABLE_MPEG AND (NOT HAVE_MPEG_LIBS)) + set (ENABLE_MPEG OFF) +endif() +if (BUILD_REGTEST AND (NOT SQLITE3_FOUND)) + set (BUILD_REGTEST OFF) +endif() -cmake_dependent_option (BUILD_REGTEST "Build regtest" ON "SQLITE3_FOUND" OFF) -cmake_dependent_option (ENABLE_EXTERNAL_LIBS "Enable FLAC, Vorbis, and Opus codecs" ON "Vorbis_FOUND;FLAC_FOUND;OPUS_FOUND" OFF) -cmake_dependent_option (ENABLE_MPEG "Enable MPEG codecs" ON "HAVE_MPEG_LIBS" OFF) cmake_dependent_option (ENABLE_CPU_CLIP "Enable tricky cpu specific clipper" ON "CPU_CLIPS_POSITIVE;CPU_CLIPS_NEGATIVE" OFF) if (NOT ENABLE_CPU_CLIP) set (CPU_CLIPS_POSITIVE FALSE) diff --git a/README.md b/README.md index 43a98cc5..82b0b390 100644 --- a/README.md +++ b/README.md @@ -273,22 +273,35 @@ have standard option `CMAKE_MSVC_RUNTIME_LIBRARY` now. Second advice is about Ogg, Vorbis FLAC and Opus support. Searching external libraries under Windows is a little bit tricky. The best way is to use -[Vcpkg](https://github.com/Microsoft/vcpkg). You need to install static libogg, -libvorbis, libflac and libopus libraries: +[Vcpkg](https://github.com/Microsoft/vcpkg). - vcpkg install libogg:x64-windows-static libvorbis:x64-windows-static - libflac:x64-windows-static opus:x64-windows-static libogg:x86-windows-static - libvorbis:x86-windows-static libflac:x86-windows-static opus:x86-windows-static - mp3lame:x86-windows-static mpg123:x86-windows-static - -Then and add this parameter to cmake command line: +Install Vcpkg and then add this parameter to cmake command line: -DCMAKE_TOOLCHAIN_FILE=/scripts/buildsystems/vcpkg.cmake -You also need to set `VCPKG_TARGET_TRIPLET` because you use static libraries: +You also need to set `VCPKG_TARGET_TRIPLET` if you want to use static libraries: -DVCPKG_TARGET_TRIPLET=x64-windows-static +Then you need to install static libogg, libvorbis, libflac, libopus, mpg123 and +mp3lame Vcpkg packages. + +After 1.1.0beta2 you don't need to install dependencies manually. Libsndfile +now supports [Vcpkg manifest mode](https://vcpkg.readthedocs.io/en/latest/users/manifests/) +and all dependencies are installed automatically. + +However, you can turn off the manifest mode and return to the classic mode using +the `VCPKG_MANIFEST_MODE` parameter from the command line: + + -DVCPKG_MANIFEST_MODE=OFF + +In classic mode, you need to install the required libraries manually: + + vcpkg install libvorbis:x64-windows-static libflac:x64-windows-static + opus:x64-windows-static mp3lame:x86-windows-static mpg123:x86-windows-static + libvorbis:x86-windows-static libflac:x86-windows-static + opus:x86-windows-static mp3lame:x86-windows-static mpg123:x86-windows-static + **Note**: Use must use the same CRT library for external libraries and the libsndfile library itself. For `*-static` triplets Vcpkg uses [static CRT](https://vcpkg.readthedocs.io/en/latest/users/triplets/). diff --git a/vcpkg.json b/vcpkg.json new file mode 100644 index 00000000..d6356ef0 --- /dev/null +++ b/vcpkg.json @@ -0,0 +1,39 @@ +{ + "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json", + "name": "libsndfile", + "description": "A library for reading and writing audio files", + "version-semver": "1.1.0-beta.2", + "default-features": [ + "external-libs", + "mpeg" + ], + "features": { + "external-libs": { + "description": "Enable FLAC, Vorbis, and Opus codecs", + "dependencies": [ + "libvorbis", + "libflac", + "opus" + ] + }, + "mpeg": { + "description": "Enable MPEG codecs", + "dependencies": [ + "mpg123", + "mp3lame" + ] + }, + "regtest": { + "description": "Build regtest", + "dependencies": [ + "sqlite3" + ] + }, + "experimental": { + "description": "Enable experimental code", + "dependencies": [ + "speex" + ] + } + } +} From 759592e4df4f9cac5fbfc6892209c6d1cd00edc1 Mon Sep 17 00:00:00 2001 From: evpobr Date: Sun, 21 Nov 2021 18:47:22 +0500 Subject: [PATCH 150/204] Add Vcpkg binary caching Speed up GitHub Actions CI using Vcpkg binary caching feature. Dependencies are built once and on next run restored from GitHub packages. --- .github/workflows/action.yml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index 6765eba4..23bb27a0 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -189,6 +189,20 @@ jobs: if: startsWith(matrix.os,'ubuntu') run: sudo apt-get update -y && sudo apt-get install -y autogen ninja-build libogg-dev libvorbis-dev libflac-dev libopus-dev libasound2-dev libsqlite3-dev libspeex-dev libmp3lame-dev libmpg123-dev + - name: Setup NuGet Credentials + env: + VCPKG_BINARY_SOURCES: 'clear;nuget,GitHub,readwrite' + if: startsWith(matrix.os,'windows') + shell: 'bash' + run: > + `vcpkg fetch nuget | tail -n 1` + sources add + -source "https://nuget.pkg.github.com/libsndfile/index.json" + -storepasswordincleartext + -name "GitHub" + -username "evpobr" + -password "${{ secrets.GITHUB_TOKEN }}" + - name: Configure, build and test with Autotools env: CC: ${{ matrix.cc }} @@ -206,6 +220,7 @@ jobs: env: CC: ${{ matrix.cc }} CXX: ${{ matrix.cxx }} + VCPKG_BINARY_SOURCES: 'clear;nuget,GitHub,readwrite' if: startsWith(matrix.build-system,'cmake') run: | mkdir build From 47383a6cc2a7e29e2ef30f855a238b95dec65ed8 Mon Sep 17 00:00:00 2001 From: evpobr Date: Wed, 24 Nov 2021 09:43:36 +0500 Subject: [PATCH 151/204] Update CHANGELOG.md --- CHANGELOG.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6ca5bbc..2ee33bda 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -107,8 +107,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 See https://github.com/msys2/MINGW-packages/issues/5803 for details. -* Fix unintialized variable in psf_binheader_readf(), thanks @shao-hua-li - ### Security * Heap buffer overflow in `wavlike_ima_decode_block`(), thanks @bobsayshilol, @@ -122,6 +120,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Heap buffer overflow in `flac_buffer_copy`(), thanks @yuawn, @bobsayshilol. * Heap buffer overflow in `copyPredictorTo24`(), thanks @bobsayshilol, credit to OSS-Fuzz ([issue 27503](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=27503)). - +* Uninitialized variable in `psf_binheader_readf`(), thanks @shao-hua-li, + credit to OSS-Fuzz ([issue 25364](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=25364)). [Unreleased]: https://github.com/libsndfile/libsndfile/compare/1.0.31...HEAD From ca2008903f66f856de4cd86e83a387b56260d5b7 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 24 Nov 2021 05:52:36 +0100 Subject: [PATCH 152/204] sndfile-convert: fix typo in error message (#793) Co-authored-by: evpobr --- CHANGELOG.md | 1 + programs/sndfile-convert.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ee33bda..dbe555ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -77,6 +77,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed * Typo in `docs/index.md`. +* Typo in `programs/sndfile-convert.c`, thanks @fjl. * Memory leak in `caf_read_header`(), credit to OSS-Fuzz ([issue 30375](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=30375)). * Stack overflow in `guess_file_type`(), thanks @bobsayshilol, credit to OSS-Fuzz ([issue 29339](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=29339)). diff --git a/programs/sndfile-convert.c b/programs/sndfile-convert.c index c8484f10..b9468624 100644 --- a/programs/sndfile-convert.c +++ b/programs/sndfile-convert.c @@ -281,7 +281,7 @@ main (int argc, char * argv []) continue ; } ; - printf ("Error : Not able to decode argunment '%s'.\n", argv [k]) ; + printf ("Error : Not able to decode argument '%s'.\n", argv [k]) ; exit (1) ; } ; From f44c8dbe1d30988801104c95589f1eecae340134 Mon Sep 17 00:00:00 2001 From: AdamKorcz <44787359+AdamKorcz@users.noreply.github.com> Date: Tue, 14 Dec 2021 03:32:18 +0000 Subject: [PATCH 153/204] Fuzzing: Add CIFuzz (#796) --- .github/workflows/cifuzz.yml | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 .github/workflows/cifuzz.yml diff --git a/.github/workflows/cifuzz.yml b/.github/workflows/cifuzz.yml new file mode 100644 index 00000000..58ef9c4a --- /dev/null +++ b/.github/workflows/cifuzz.yml @@ -0,0 +1,26 @@ +name: CIFuzz +on: [pull_request] +jobs: + Fuzzing: + runs-on: ubuntu-latest + steps: + - name: Build Fuzzers + id: build + uses: google/oss-fuzz/infra/cifuzz/actions/build_fuzzers@master + with: + oss-fuzz-project-name: 'libsndfile' + dry-run: false + language: c + - name: Run Fuzzers + uses: google/oss-fuzz/infra/cifuzz/actions/run_fuzzers@master + with: + oss-fuzz-project-name: 'libsndfile' + fuzz-seconds: 600 + dry-run: false + language: c + - name: Upload Crash + uses: actions/upload-artifact@v1 + if: failure() && steps.build.outcome == 'success' + with: + name: artifacts + path: ./out/artifacts From 59f1622c422ddeb8ba11874018158f28de8cc3b3 Mon Sep 17 00:00:00 2001 From: evpobr Date: Thu, 16 Dec 2021 10:49:14 +0500 Subject: [PATCH 154/204] cmake: Export targets from the build tree --- CMakeLists.txt | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a8562b0..dffdb8db 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -819,6 +819,8 @@ if (ENABLE_PACKAGE_CONFIG) PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) + export (EXPORT SndFileTargets NAMESPACE SndFile:: FILE ${PROJECT_BINARY_DIR}/SndFileTargets.cmake) + include (CMakePackageConfigHelpers) if (ENABLE_EXTERNAL_LIBS) @@ -829,6 +831,11 @@ if (ENABLE_PACKAGE_CONFIG) set (INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR}) configure_package_config_file(cmake/SndFileConfig.cmake.in SndFileConfig.cmake + INSTALL_DESTINATION ${PROJECT_BINARY_DIR} + INSTALL_PREFIX ${PROJECT_BINARY_DIR} + PATH_VARS INCLUDE_INSTALL_DIR + ) + configure_package_config_file(cmake/SndFileConfig.cmake.in SndFileConfig2.cmake INSTALL_DESTINATION ${CMAKE_INSTALL_PACKAGEDIR} PATH_VARS INCLUDE_INSTALL_DIR ) @@ -838,9 +845,13 @@ if (ENABLE_PACKAGE_CONFIG) NAMESPACE SndFile:: DESTINATION ${CMAKE_INSTALL_PACKAGEDIR} ) + install( + FILES ${CMAKE_CURRENT_BINARY_DIR}/SndFileConfig2.cmake + RENAME SndFileConfig.cmake + DESTINATION ${CMAKE_INSTALL_PACKAGEDIR} + ) install( FILES - ${CMAKE_CURRENT_BINARY_DIR}/SndFileConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/SndFileConfigVersion.cmake DESTINATION ${CMAKE_INSTALL_PACKAGEDIR} ) From 77acc6d3623b06c75461a8bfdeff0066278c2eea Mon Sep 17 00:00:00 2001 From: evpobr Date: Thu, 16 Dec 2021 12:17:11 +0500 Subject: [PATCH 155/204] cmake: Add config-file package MPEG dependency --- CMakeLists.txt | 6 ++++++ cmake/SndFileConfig.cmake.in | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index dffdb8db..31131de4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -829,6 +829,12 @@ if (ENABLE_PACKAGE_CONFIG) set (SndFile_WITH_EXTERNAL_LIBS 0) endif () + if(ENABLE_MPEG) + set (SndFile_WITH_MPEG 1) + else () + set (SndFile_WITH_MPEG 0) + endif () + set (INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR}) configure_package_config_file(cmake/SndFileConfig.cmake.in SndFileConfig.cmake INSTALL_DESTINATION ${PROJECT_BINARY_DIR} diff --git a/cmake/SndFileConfig.cmake.in b/cmake/SndFileConfig.cmake.in index e33c56bd..1d5463aa 100644 --- a/cmake/SndFileConfig.cmake.in +++ b/cmake/SndFileConfig.cmake.in @@ -4,6 +4,7 @@ set(SndFile_VERSION_MINOR @PROJECT_VERSION_MINOR@) set(SndFile_VERSION_PATCH @PROJECT_VERSION_PATCH@) set (SndFile_WITH_EXTERNAL_LIBS @SndFile_WITH_EXTERNAL_LIBS@) +set (SndFile_WITH_MPEG @SndFile_WITH_MPEG@) @PACKAGE_INIT@ @@ -16,6 +17,11 @@ if (SndFile_WITH_EXTERNAL_LIBS AND NOT @BUILD_SHARED_LIBS@) find_dependency (Opus) endif () +if (SndFile_WITH_MPEG AND NOT @BUILD_SHARED_LIBS@) + find_dependency (Lame) + find_dependency (MPG123) +endif () + include (${CMAKE_CURRENT_LIST_DIR}/SndFileTargets.cmake) set_and_check (SndFile_INCLUDE_DIR "@PACKAGE_INCLUDE_INSTALL_DIR@") From 4b01368fd8a1e4541d7a5d125f70e99d30851718 Mon Sep 17 00:00:00 2001 From: evpobr Date: Sat, 19 Mar 2022 09:32:03 +0500 Subject: [PATCH 156/204] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dbe555ef..dbc2a9d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 If you have problems with manifest mode, disable it with `VCPKG_MANIFEST_MODE` switch. +* [Export CMake targets from the build tree (PR #802)](https://cmake.org/cmake/help/latest/guide/importing-exporting/index.html#exporting-targets-from-the-build-tree) +* CIFuzz fuzzer, thanks to @AdamKorcz (PR #796) ### Changed From ce57da8fd462c393af7975b2d666eb249239c518 Mon Sep 17 00:00:00 2001 From: David Seifert Date: Sun, 27 Mar 2022 13:20:49 +0200 Subject: [PATCH 157/204] Only add `-Wno-format-truncation` when using GCC * Clang produces warnings such as warning: unknown warning option '-Wno-format-truncation' [-Wunknown-warning-option] --- configure.ac | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index 34b44409..b7524bcf 100644 --- a/configure.ac +++ b/configure.ac @@ -633,11 +633,22 @@ AS_IF([test "x$enable_werror" = "xyes"], [ AC_LANG_POP([C++]) ]) -common_flags="-Wall -Wextra -Wpointer-arith -Wcast-align -Wcast-qual -Wshadow -Wwrite-strings -Wundef -Wuninitialized -Winit-self -Wno-format-truncation" -AX_APPEND_COMPILE_FLAGS([${common_flags} -Wvla -Wbad-function-cast -Wnested-externs -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Waggregate-return], [CFLAGS]) +common_flags="-Wall -Wextra -Wpointer-arith -Wcast-align -Wcast-qual -Wshadow -Wwrite-strings -Wundef -Wuninitialized -Winit-self" +common_cflags="${common_flags}" +dnl Clang doesn't know about -Wno-format-truncation +dnl and would spew tons of warnings otherwise. +AS_IF([test "x$ax_cv_c_compiler_vendor" = "xgnu"], [ + common_cflags+=" -Wno-format-truncation" + ]) +common_cxxflags="${common_flags}" +AS_IF([test "x$ax_cv_cxx_compiler_vendor" = "xgnu"], [ + common_cxxflags+=" -Wno-format-truncation" + ]) + +AX_APPEND_COMPILE_FLAGS([${common_cflags} -Wvla -Wbad-function-cast -Wnested-externs -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Waggregate-return], [CFLAGS]) AC_LANG_PUSH([C++]) -AX_APPEND_COMPILE_FLAGS([${common_flags} -Wctor-dtor-privacy -Wnon-virtual-dtor -Woverloaded-virtual -Wreorder -Wsign-promo], [CXXFLAGS]) +AX_APPEND_COMPILE_FLAGS([${common_cxxflags} -Wctor-dtor-privacy -Wnon-virtual-dtor -Woverloaded-virtual -Wreorder -Wsign-promo], [CXXFLAGS]) AC_LANG_POP([C++]) AS_IF([test "x$enable_test_coverage" = "xyes"], [ From 89e7c02464c1d3d1d407db26a7c86d24d6626af1 Mon Sep 17 00:00:00 2001 From: evpobr Date: Sun, 27 Mar 2022 15:31:49 +0500 Subject: [PATCH 158/204] Prepare to release --- CHANGELOG.md | 5 ++++- README | 2 +- SECURITY.md | 2 +- docs/_config.yml | 2 +- docs/index.md | 2 ++ vcpkg.json | 2 +- 6 files changed, 10 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dbc2a9d3..3719e0f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.1.0] - 2022-03-27 + ### Added * MPEG Encode/Decode Support. @@ -126,4 +128,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Uninitialized variable in `psf_binheader_readf`(), thanks @shao-hua-li, credit to OSS-Fuzz ([issue 25364](https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=25364)). -[Unreleased]: https://github.com/libsndfile/libsndfile/compare/1.0.31...HEAD +[Unreleased]: https://github.com/libsndfile/libsndfile/compare/1.1.0...HEAD +[1.1.0]: https://github.com/libsndfile/libsndfile/compare/1.0.31...1.1.0 diff --git a/README b/README index 93b4b29a..3ad4b34c 100644 --- a/README +++ b/README @@ -1,4 +1,4 @@ -This is libsndfile, 1.0.29 +This is libsndfile, 1.1.0 libsndfile is a library of C routines for reading and writing files containing sampled audio data. diff --git a/SECURITY.md b/SECURITY.md index c9e960dc..a32393d7 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -4,7 +4,7 @@ | Version | Supported | | ------- | ------------------ | -| 1.0.x | :white_check_mark: | +| 1.1.x | :white_check_mark: | ## Reporting a Vulnerability diff --git a/docs/_config.yml b/docs/_config.yml index c9f19d76..b3b6ea99 100644 --- a/docs/_config.yml +++ b/docs/_config.yml @@ -1,6 +1,6 @@ --- author: "The libsndfile team" -version: "1.0.31" +version: "1.1.0" markdown: kramdown plugins: diff --git a/docs/index.md b/docs/index.md index ee6bd07f..b6663352 100644 --- a/docs/index.md +++ b/docs/index.md @@ -142,6 +142,8 @@ Here is the release history for libsndfile: problems, delete old generated sndfile.h from $(top_builddir)/src. * Version 1.0.31 (January 24 2021) Bugfix release. Fix multiple memory leaks reported by OSS-Fuzz. More SSE2-optimized functions for x86 and amd64. +* Version 1.1.0 (March 27 2022) Minor release, backward compatible with previous + releases. Added long-awaited MP3 support. Numerous improvements and bugfixes. ## Similar or Related Projects diff --git a/vcpkg.json b/vcpkg.json index d6356ef0..a37a18b4 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -2,7 +2,7 @@ "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json", "name": "libsndfile", "description": "A library for reading and writing audio files", - "version-semver": "1.1.0-beta.2", + "version-semver": "1.1.0", "default-features": [ "external-libs", "mpeg" From ea3ac90e98c6a98cd52cae39010446fba368a2e3 Mon Sep 17 00:00:00 2001 From: David Seifert Date: Sun, 27 Mar 2022 13:48:57 +0200 Subject: [PATCH 159/204] Release 1.1.0 --- CMakeLists.txt | 2 +- configure.ac | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 31131de4..b1b49e95 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,7 +49,7 @@ set (PACKAGE_NAME ${PROJECT_NAME}) set (CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR}) set (CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR}) set (CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH}) -set (CPACK_PACKAGE_VERSION_STAGE "beta2") +set (CPACK_PACKAGE_VERSION_STAGE "") set (CPACK_PACKAGE_VERSION_FULL "${PROJECT_VERSION}${CPACK_PACKAGE_VERSION_STAGE}") # diff --git a/configure.ac b/configure.ac index b7524bcf..727b67bc 100644 --- a/configure.ac +++ b/configure.ac @@ -1,9 +1,9 @@ -dnl Copyright (C) 1999-2021 Erik de Castro Lopo . +dnl Copyright (C) 1999-2022 Erik de Castro Lopo . dnl Require autoconf version >= 2.69 AC_PREREQ([2.69]) -AC_INIT([libsndfile],[1.1.0beta2],[sndfile@mega-nerd.com], +AC_INIT([libsndfile],[1.1.0],[sndfile@mega-nerd.com], [libsndfile],[http://libsndfile.github.io/libsndfile/]) dnl Check whether we want to set defaults for CFLAGS, CXXFLAGS, CPPFLAGS and LDFLAGS @@ -88,7 +88,7 @@ dnl to 0. dnl This is libtool version of library, we add it to `--version-info` property. m4_define([lt_current], [1]) -m4_define([lt_revision], [33]) +m4_define([lt_revision], [34]) m4_define([lt_age], [0]) dnl This is ABI version for linker scripts, CMake uses the same format for From 33e765ccba9a0eb225694fdbf9e299683a8338ee Mon Sep 17 00:00:00 2001 From: David Seifert Date: Sat, 2 Apr 2022 20:53:09 +0200 Subject: [PATCH 160/204] Remove obsolete SF_PLATFORM_S64() macro --- src/aiff.c | 2 +- src/common.h | 14 -------------- src/svx.c | 2 +- src/test_conversions.c | 2 +- src/wav.c | 2 +- 5 files changed, 4 insertions(+), 18 deletions(-) diff --git a/src/aiff.c b/src/aiff.c index 7ad03126..4c662454 100644 --- a/src/aiff.c +++ b/src/aiff.c @@ -404,7 +404,7 @@ aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt) char *cptr ; int instr_found = 0, mark_found = 0 ; - if (psf->filelength > SF_PLATFORM_S64 (0xffffffff)) + if (psf->filelength > 0xFFFFFFFFLL) psf_log_printf (psf, "Warning : filelength > 0xffffffff. This is bad!!!!\n") ; if ((paiff = psf->container_data) == NULL) diff --git a/src/common.h b/src/common.h index 6ca0648f..a8ef0b6c 100644 --- a/src/common.h +++ b/src/common.h @@ -50,20 +50,6 @@ #error "This code is not designed to be compiled with a C++ compiler." #endif -#ifdef INT64_C -# define SF_PLATFORM_S64(x) INT64_C (x) -#elif (SIZEOF_LONG == 8) -# define SF_PLATFORM_S64(x) x##l -#elif (SIZEOF_LONG_LONG == 8) -# define SF_PLATFORM_S64(x) x##ll -#elif COMPILER_IS_GCC -# define SF_PLATFORM_S64(x) x##ll -#elif OS_IS_WIN32 -# define SF_PLATFORM_S64(x) x##I64 -#else -# error "Don't know how to define a 64 bit integer constant." -#endif - /* diff --git a/src/svx.c b/src/svx.c index a6b72324..342b0062 100644 --- a/src/svx.c +++ b/src/svx.c @@ -134,7 +134,7 @@ svx_read_header (SF_PRIVATE *psf) int filetype = 0, parsestage = 0, done = 0 ; int bytecount = 0, channels ; - if (psf->filelength > SF_PLATFORM_S64 (0xffffffff)) + if (psf->filelength > 0xFFFFFFFFLL) psf_log_printf (psf, "Warning : filelength > 0xffffffff. This is bad!!!!\n") ; memset (&vhdr, 0, sizeof (vhdr)) ; diff --git a/src/test_conversions.c b/src/test_conversions.c index f6ee36e0..f717fa58 100644 --- a/src/test_conversions.c +++ b/src/test_conversions.c @@ -44,7 +44,7 @@ conversion_test (char endian) { SF_PRIVATE sf_private, *psf ; const char * filename = "conversion.bin" ; - int64_t i64 = SF_PLATFORM_S64 (0x0123456789abcdef), t64 = 0 ; + int64_t i64 = 0x0123456789ABCDEFLL, t64 = 0 ; char format_str [16] ; char test_name [64] ; char i8 = 12, t8 = 0 ; diff --git a/src/wav.c b/src/wav.c index 0dd6c730..80baf288 100644 --- a/src/wav.c +++ b/src/wav.c @@ -310,7 +310,7 @@ wav_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock) uint32_t marker, chunk_size = 0, RIFFsize = 0, done = 0 ; int parsestage = 0, error, format = 0 ; - if (psf->is_pipe == 0 && psf->filelength > SF_PLATFORM_S64 (0xffffffff)) + if (psf->is_pipe == 0 && psf->filelength > 0xFFFFFFFFLL) psf_log_printf (psf, "Warning : filelength > 0xffffffff. This is bad!!!!\n") ; if ((wpriv = psf->container_data) == NULL) From eff5f0a640ef2a21ac77b42aa22d42b30fd46c97 Mon Sep 17 00:00:00 2001 From: evpobr Date: Sat, 2 Apr 2022 11:04:55 +0500 Subject: [PATCH 161/204] cmake: fix LAME dependency search --- CHANGELOG.md | 4 +++ CMakeLists.txt | 19 ++-------- cmake/FindLame.cmake | 66 ----------------------------------- cmake/Findmp3lame.cmake | 67 ++++++++++++++++++++++++++++++++++++ cmake/SndFileChecks.cmake | 4 +-- cmake/SndFileConfig.cmake.in | 2 +- 6 files changed, 77 insertions(+), 85 deletions(-) delete mode 100644 cmake/FindLame.cmake create mode 100644 cmake/Findmp3lame.cmake diff --git a/CHANGELOG.md b/CHANGELOG.md index 3719e0f8..19eda17e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +* Searching for LAME dependency with CMake build system (issue #821). + ## [1.1.0] - 2022-03-27 ### Added diff --git a/CMakeLists.txt b/CMakeLists.txt index b1b49e95..ae8ebec7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -174,7 +174,7 @@ set_package_properties (FLAC PROPERTIES DESCRIPTION "Free Lossless Audio Codec Library" PURPOSE "Enables FLAC support" ) -set_package_properties (Lame PROPERTIES +set_package_properties (mp3lame PROPERTIES TYPE RECOMMENDED URL "https://lame.sourceforge.io/" DESCRIPTION "High quality MPEG Audio Layer III (MP3) encoder" @@ -237,20 +237,7 @@ if (INSTALL_PKGCONFIG_MODULE) endif () if (ENABLE_MPEG) set (EXTERNAL_MPEG_REQUIRE "libmpg123") - get_filename_component(LAME_WE ${LAME_LIBRARY} NAME_WE) - if (LAME_HIP_LIBRARY) - get_filename_component(LAME_HIP_WE ${LAME_HIP_LIBRARY} NAME_WE) - endif () - if (CMAKE_IMPORT_LIBRARY_PREFIX) - string (REGEX REPLACE "^${CMAKE_IMPORT_LIBRARY_PREFIX}" "" LAME_WE_NO_PREFIX ${LAME_WE}) - if (LAME_HIP_LIBRARY) - string (REGEX REPLACE "^${CMAKE_IMPORT_LIBRARY_PREFIX}" "" LAME_HIP_WE_NO_PREFIX ${LAME_HIP_WE}) - endif () - endif () - set (EXTERNAL_MPEG_LIBS "-l${LAME_WE_NO_PREFIX}") - if (LAME_HIP_LIBRARY) - set (EXTERNAL_MPEG_LIBS "${MPEG_LIBS} -l${LAME_HIP_WE}") - endif () + set (EXTERNAL_MPEG_LIBS "-lmp3lame") endif () configure_file (sndfile.pc.in sndfile.pc @ONLY) @@ -411,7 +398,7 @@ target_link_libraries (sndfile $<$,$,$>:Speex::Speex> $<$:Opus::opus> $<$:MPG123::libmpg123> - $<$:Lame::Lame> + $<$:mp3lame::mp3lame> ) set_target_properties (sndfile PROPERTIES PUBLIC_HEADER "${sndfile_HDRS}" diff --git a/cmake/FindLame.cmake b/cmake/FindLame.cmake deleted file mode 100644 index be32475a..00000000 --- a/cmake/FindLame.cmake +++ /dev/null @@ -1,66 +0,0 @@ -# - Find lame -# Find the native lame includes and libraries -# -# LAME_INCLUDE_DIRS - where to find lame.h, etc. -# LAME_LIBRARIES - List of libraries when using lame. -# LAME_FOUND - True if Lame found. - -if (LAME_INCLUDE_DIR) - # Already in cache, be silent - set(LAME_FIND_QUIETLY TRUE) -endif () - -find_path (LAME_INCLUDE_DIR lame/lame.h - HINTS - ${LAME_ROOT} - ) - -# MSVC built lame may be named mp3lame_static. -# The provided project files name the library with the lib prefix. - -find_library (LAME_LIBRARY - NAMES - mp3lame - mp3lame_static - libmp3lame - libmp3lame_static - libmp3lame-static - HINTS - ${LAME_ROOT} - ) - -find_library (LAME_HIP_LIBRARY - NAMES - mpghip-static - libmpghip-static - HINTS - ${LAME_ROOT} - ) - -# Handle the QUIETLY and REQUIRED arguments and set LAME_FOUND -# to TRUE if all listed variables are TRUE. -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args (Lame - REQUIRED_VARS - LAME_LIBRARY - LAME_INCLUDE_DIR - ) - -if (LAME_FOUND) - set (LAME_LIBRARIES ${LAME_LIBRARY} ${LAME_HIP_LIBRARY}) - set (LAME_INCLUDE_DIRS ${LAME_INCLUDE_DIR}) - - if (NOT TARGET Lame::Lame) - add_library (Lame::Lame UNKNOWN IMPORTED) - set_target_properties (Lame::Lame PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES "${LAME_INCLUDE_DIRS}" - IMPORTED_LOCATION "${LAME_LIBRARY}" - ) - if (LAME_HIP_LIBRARY) - set_property (TARGET Lame::Lame APPEND PROPERTY - INTERFACE_LINK_LIBRARIES "${LAME_HIP_LIBRARY}") - endif () - endif () -endif () - -mark_as_advanced(LAME_INCLUDE_DIR LAME_LIBRARY LAME_HIP_LIBRARY) diff --git a/cmake/Findmp3lame.cmake b/cmake/Findmp3lame.cmake new file mode 100644 index 00000000..223dd08f --- /dev/null +++ b/cmake/Findmp3lame.cmake @@ -0,0 +1,67 @@ +# - Find lame +# Find the native lame includes and libraries +# +# MP3LAME_INCLUDE_DIRS - where to find lame.h, etc. +# MP3LAME_LIBRARIES - List of libraries when using lame. +# MP3LAME_FOUND - True if Lame found. + +if (MP3LAME_INCLUDE_DIR) + # Already in cache, be silent + set(MP3LAME_FIND_QUIETLY TRUE) +endif () + +find_path (MP3LAME_INCLUDE_DIR lame/lame.h + HINTS + ${LAME_ROOT} + ) + +# MSVC built lame may be named mp3lame_static. +# The provided project files name the library with the lib prefix. + +find_library (MP3LAME_LIBRARY + NAMES + mp3lame + mp3lame_static + libmp3lame + libmp3lame_static + libmp3lame-static + HINTS + ${MP3LAME_ROOT} + ) + +find_library (MP3LAME_HIP_LIBRARY + NAMES + mpghip-static + libmpghip-static + HINTS + ${MP3LAME_ROOT} + ) + +# Handle the QUIETLY and REQUIRED arguments and set LAME_FOUND +# to TRUE if all listed variables are TRUE. +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args (mp3lame + REQUIRED_VARS + MP3LAME_LIBRARY + MP3LAME_INCLUDE_DIR + ) + +if (MP3LAME_FOUND) + set (MP3LAME_LIBRARIES ${MP3LAME_LIBRARY} ${MP3LAME_HIP_LIBRARY}) + set (MP3LAME_INCLUDE_DIRS ${MP3LAME_INCLUDE_DIR}) + + if (NOT TARGET mp3lame::mp3lame) + add_library (mp3lame::mp3lame UNKNOWN IMPORTED) + set_target_properties (mp3lame::mp3lame PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${MP3LAME_INCLUDE_DIRS}" + IMPORTED_LOCATION "${MP3LAME_LIBRARY}" + ) + if (MP3LAME_HIP_LIBRARY AND (NOT TARGET mp3lame::mpghip)) + add_library (mp3lame::mpghip STATIC IMPORTED) + set_property (mp3lame::mpghip PROPERTY IMPORTED_LOCATION "${MP3LAME_HIP_LIBRARY}") + set_property (TARGET mp3lame::mp3lame PROPERTY INTERFACE_LINK_LIBRARIES "mp3lame::mpghip") + endif () + endif () +endif () + +mark_as_advanced(MP3LAME_INCLUDE_DIR MP3LAME_LIBRARY MP3LAME_HIP_LIBRARY) diff --git a/cmake/SndFileChecks.cmake b/cmake/SndFileChecks.cmake index f5d5994b..2e09ba6f 100644 --- a/cmake/SndFileChecks.cmake +++ b/cmake/SndFileChecks.cmake @@ -56,9 +56,9 @@ else () set (HAVE_EXTERNAL_XIPH_LIBS 0) endif () -find_package (Lame) +find_package (mp3lame) find_package (Mpg123 1.25.10) -if (LAME_FOUND AND (TARGET MPG123::libmpg123)) +if (TARGET mp3lame::mp3lame AND (TARGET MPG123::libmpg123)) set (HAVE_MPEG_LIBS 1) else () set (HAVE_MPEG_LIBS 0) diff --git a/cmake/SndFileConfig.cmake.in b/cmake/SndFileConfig.cmake.in index 1d5463aa..0c98e7e2 100644 --- a/cmake/SndFileConfig.cmake.in +++ b/cmake/SndFileConfig.cmake.in @@ -18,7 +18,7 @@ if (SndFile_WITH_EXTERNAL_LIBS AND NOT @BUILD_SHARED_LIBS@) endif () if (SndFile_WITH_MPEG AND NOT @BUILD_SHARED_LIBS@) - find_dependency (Lame) + find_dependency (mp3lame) find_dependency (MPG123) endif () From 269ae40a3b44ae16e35a3c30330bafce992e4387 Mon Sep 17 00:00:00 2001 From: evpobr Date: Sun, 3 Apr 2022 11:53:26 +0500 Subject: [PATCH 162/204] Fix CMake build from Autotools tarball, closes #816 --- CHANGELOG.md | 1 + Makefile.am | 1 + 2 files changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19eda17e..38dd2f77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed * Searching for LAME dependency with CMake build system (issue #821). +* CMake build from Autotools tarball (issue #816). ## [1.1.0] - 2022-03-27 diff --git a/Makefile.am b/Makefile.am index d2bbdcbd..5efbf9a8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -24,6 +24,7 @@ cmake_files = cmake/ClipMode.cmake cmake/FindFLAC.cmake \ cmake/TestLargeFiles.cmake cmake/TestInline.c.in \ cmake/FindOpus.cmake cmake/SndFileConfig.cmake.in \ cmake/CheckCPUArch.cmake cmake/CheckCPUArch.c.in \ + cmake/Findmp3lame.cmake cmake/FindMpg123.cmake \ cmake/SetupABIVersions.cmake pkgconfig_DATA = sndfile.pc From fc298c9d9324b1fe01329f675118561eef92b9e4 Mon Sep 17 00:00:00 2001 From: evpobr Date: Sun, 3 Apr 2022 12:11:11 +0500 Subject: [PATCH 163/204] Fix build on UWP platform, closes #824 --- CHANGELOG.md | 1 + src/file_io.c | 3 --- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38dd2f77..e87e69c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Searching for LAME dependency with CMake build system (issue #821). * CMake build from Autotools tarball (issue #816). +* Build on UWP platform (isse #824). ## [1.1.0] - 2022-03-27 diff --git a/src/file_io.c b/src/file_io.c index 334ba2a2..90c96085 100644 --- a/src/file_io.c +++ b/src/file_io.c @@ -827,9 +827,6 @@ psf_open_handle (PSF_FILE * pfile) } ; #if defined (WINAPI_FAMILY_PARTITION) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM) - if (!pfile->use_wchar) - return INVALID_HANDLE_VALUE ; - CREATEFILE2_EXTENDED_PARAMETERS cfParams = { 0 } ; cfParams.dwSize = sizeof (CREATEFILE2_EXTENDED_PARAMETERS) ; cfParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL ; From d6f83cd4feb154efcf5614601985ae2ce9d9fa6d Mon Sep 17 00:00:00 2001 From: evpobr Date: Wed, 6 Apr 2022 10:02:27 +0500 Subject: [PATCH 164/204] gsm610: Fix signed integer overflow Related to libsndfile#785 Co-authored-by: evpobr --- CHANGELOG.md | 1 + src/gsm610.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e87e69c2..4dd522da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Searching for LAME dependency with CMake build system (issue #821). * CMake build from Autotools tarball (issue #816). * Build on UWP platform (isse #824). +* Fix signed integer overflow (issue #785). ## [1.1.0] - 2022-03-27 diff --git a/src/gsm610.c b/src/gsm610.c index 77d4f1fe..933f03f0 100644 --- a/src/gsm610.c +++ b/src/gsm610.c @@ -150,7 +150,7 @@ Need separate gsm_data structs for encode and decode. pgsm610->blocks = psf->datalength / pgsm610->blocksize + 1 ; } ; - psf->sf.frames = pgsm610->samplesperblock * pgsm610->blocks ; + psf->sf.frames = (sf_count_t) pgsm610->samplesperblock * pgsm610->blocks ; psf_fseek (psf, psf->dataoffset, SEEK_SET) ; From 83e583dd2c69f0c43b8b7e822e9693b1277eb668 Mon Sep 17 00:00:00 2001 From: evpobr Date: Wed, 6 Apr 2022 10:18:19 +0500 Subject: [PATCH 165/204] Fix typo --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4dd522da..f3877099 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Searching for LAME dependency with CMake build system (issue #821). * CMake build from Autotools tarball (issue #816). -* Build on UWP platform (isse #824). +* Build on UWP platform (issue #824). * Fix signed integer overflow (issue #785). ## [1.1.0] - 2022-03-27 From 3baa16571f90ed67408982d4c86b22a9109e3358 Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Wed, 26 Jan 2022 13:01:00 -0800 Subject: [PATCH 166/204] ogg: Fix for issue-794, buffering of searched page is wrong. The function ogg_stream_seek_page_search() is a reimplementation of op_pcm_seek_page from libopusfile. This original function contained a bug a bug caused by a non-braced if body followed by two statements that should be conditional. Being non-braced, only the first statement was conditional. The bug caused previous-page buffering always to be on. Previous page buffering is an efficiency gain for seeking to positions that are part of a continued packet. Erroneously always turning it on prevents the loading of the correct page at the end of the search in some conditions. This manifested as seek-search working for some positions and not others, depending on the order of found pages near the final target. --- src/ogg.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ogg.c b/src/ogg.c index 7a4a167f..3bd510fa 100644 --- a/src/ogg.c +++ b/src/ogg.c @@ -624,13 +624,13 @@ ogg_stream_seek_page_search (SF_PRIVATE *psf, OGG_PRIVATE *odata, uint64_t targe ** remember the end of the page. */ best_start = page_offset ; - } ; - /* - ** Then force buffering on, so that if a packet starts (but - ** does not end) on the next page, we still avoid the extra - ** seek back. - */ - buffering = SF_TRUE ; + /* + ** Then force buffering on, so that if a packet starts (but + ** does not end) on the next page, we still avoid the extra + ** seek back. + */ + buffering = SF_TRUE ; + } ; *best_gp = pcm_start = gp ; if (target_gp - gp > 48000) { /* Out by over a second. Try another bisection. */ From 3aab1c56178576818474a360ed6f9979c3abd031 Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Wed, 26 Jan 2022 13:37:10 -0800 Subject: [PATCH 167/204] ogg: Add command SFC_GET_OGG_STREAM_SERIALNO Add command SFC_GET_OGG_STREAM_SERIALNO returning the current stream serialno. Useful for debugging. --- docs/command.md | 29 +++++++++++++++++++++++++++-- include/sndfile.h.in | 3 +++ src/ogg_opus.c | 7 +++++++ src/ogg_vorbis.c | 10 +++++++++- 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/docs/command.md b/docs/command.md index 6d2133ad..9014b172 100644 --- a/docs/command.md +++ b/docs/command.md @@ -64,6 +64,7 @@ The available commands are as follows: | [SFC_WAVEX_SET_AMBISONIC](#sfc_wavex_set_ambisonic) | Modify a WAVEX header for Ambisonic format. | | [SFC_SET_VBR_ENCODING_QUALITY](#sfc_set_vbr_encoding_quality) | Set the Variable Bit Rate encoding quality. | | [SFC_SET_OGG_PAGE_LATENCY_MS](#sfc_set_ogg_page_latency_ms) | Set Ogg page latency for Opus file. | +| [SFC_GET_OGG_STREAM_SERIALNO](#sfc_get_ogg_stream_serialno) | Get Ogg stream serial number. | | [SFC_SET_COMPRESSION_LEVEL](#sfc_set_compression_level) | Set the compression level. | | [SFC_RAW_DATA_NEEDS_ENDSWAP](#sfc_raw_data_needs_endswap) | Determine if raw data needs endswapping. | | [SFC_GET_BROADCAST_INFO](#sfc_get_broadcast_info) | Get the Broadcast Chunk info. | @@ -81,8 +82,8 @@ The available commands are as follows: | [SFC_RF64_AUTO_DOWNGRADE](#sfc_rf64_auto_downgrade) | Set auto downgrade from RF64 to WAV. | | [SFC_GET_ORIGINAL_SAMPLERATE](#sfc_get_original_samplerate) | Get original samplerate metadata. | | [SFC_SET_ORIGINAL_SAMPLERATE](#sfc_set_original_samplerate) | Set original samplerate metadata. | -| [SFC_GET_BITRATE_MODE](#sfc_get_bitrate_mode) | Get bitrate mode. -| [SFC_SET_BITRATE_MODE](#sfc_set_bitrate_mode) | Set bitrate mode. +| [SFC_GET_BITRATE_MODE](#sfc_get_bitrate_mode) | Get bitrate mode. | +| [SFC_SET_BITRATE_MODE](#sfc_set_bitrate_mode) | Set bitrate mode. | --- @@ -1273,6 +1274,30 @@ datasize 0 on success and non-zero otherwise. +## SFC_GET_OGG_STREAM_SERIALNO + +Get the Ogg stream serial number for files with the Ogg major format. Ogg +stream serail numbers are a randomly chosen 32-bit value, used for +differentiating logical Ogg streams. + +### Parameters + +sndfile +: A valid SNDFILE* pointer + +cmd +: SFC_SET_OGG_PAGE_LATENCY_MS + +data +: A pointer to a 32-bit int value + +datasize +: sizeof (int32_t) = 4 + +### Return value + +0 on success and non-zero otherwise. + ## SFC_SET_COMPRESSION_LEVEL Set the compression level. The compression level should be between 0.0 (minimum diff --git a/include/sndfile.h.in b/include/sndfile.h.in index 2bcbdb1f..61a36540 100644 --- a/include/sndfile.h.in +++ b/include/sndfile.h.in @@ -220,8 +220,11 @@ enum SFC_SET_VBR_ENCODING_QUALITY = 0x1300, SFC_SET_COMPRESSION_LEVEL = 0x1301, + + /* Ogg format commands */ SFC_SET_OGG_PAGE_LATENCY_MS = 0x1302, SFC_SET_OGG_PAGE_LATENCY = 0x1303, + SFC_GET_OGG_STREAM_SERIALNO = 0x1304, SFC_GET_BITRATE_MODE = 0x1304, SFC_SET_BITRATE_MODE = 0x1305, diff --git a/src/ogg_opus.c b/src/ogg_opus.c index dfa446ee..4e3f9e57 100644 --- a/src/ogg_opus.c +++ b/src/ogg_opus.c @@ -1733,6 +1733,13 @@ ogg_opus_command (SF_PRIVATE *psf, int command, void *data, int datasize) *((int *) data) = oopus->header.input_samplerate ; return SF_TRUE ; + case SFC_GET_OGG_STREAM_SERIALNO : + if (data == NULL || datasize != sizeof (int32_t)) + return SF_FALSE ; + + *((int32_t *) data) = odata->ostream.serialno ; + return SF_TRUE ; + default : break ; } diff --git a/src/ogg_vorbis.c b/src/ogg_vorbis.c index 1b00af51..b2324dd6 100644 --- a/src/ogg_vorbis.c +++ b/src/ogg_vorbis.c @@ -511,7 +511,8 @@ ogg_vorbis_open (SF_PRIVATE *psf) static int vorbis_command (SF_PRIVATE *psf, int command, void * data, int datasize) -{ VORBIS_PRIVATE *vdata = (VORBIS_PRIVATE *) psf->codec_data ; +{ OGG_PRIVATE* odata = psf->container_data ; + VORBIS_PRIVATE *vdata = (VORBIS_PRIVATE *) psf->codec_data ; switch (command) { case SFC_SET_COMPRESSION_LEVEL : @@ -529,6 +530,13 @@ vorbis_command (SF_PRIVATE *psf, int command, void * data, int datasize) psf_log_printf (psf, "%s : Setting SFC_SET_VBR_ENCODING_QUALITY to %f.\n", __func__, vdata->quality) ; return SF_TRUE ; + case SFC_GET_OGG_STREAM_SERIALNO : + if (data == NULL || datasize != sizeof (int32_t)) + return SF_FALSE ; + + *((int32_t *) data) = odata->ostream.serialno ; + return SF_TRUE ; + default : return SF_FALSE ; } ; From b56aba563395f16b32da9a483967a6db830ba84f Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Wed, 26 Jan 2022 14:29:51 -0800 Subject: [PATCH 168/204] ogg: Correct various psf_log_printf() argument length issues. --- src/ogg_opus.c | 35 ++++++++++++++++++++--------------- src/ogg_vorbis.c | 4 ++-- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/ogg_opus.c b/src/ogg_opus.c index 4e3f9e57..79ad4df2 100644 --- a/src/ogg_opus.c +++ b/src/ogg_opus.c @@ -409,27 +409,27 @@ ogg_opus_close (SF_PRIVATE *psf) static void opus_print_header (SF_PRIVATE *psf, OpusHeader *h) { psf_log_printf (psf, "Opus Header Metadata\n") ; - psf_log_printf (psf, " OggOpus version : %d\n", h->version) ; - psf_log_printf (psf, " Channels : %d\n", h->channels) ; - psf_log_printf (psf, " Preskip : %d samples @48kHz\n", h->preskip) ; - psf_log_printf (psf, " Input Samplerate : %d Hz\n", h->input_samplerate) ; - psf_log_printf (psf, " Gain : %d.%d\n", arith_shift_right (h->gain & 0xF0, 8), h->gain & 0x0F) ; + psf_log_printf (psf, " OggOpus version : %d\n", (int) h->version) ; + psf_log_printf (psf, " Channels : %d\n", (int) h->channels) ; + psf_log_printf (psf, " Preskip : %d samples @48kHz\n", (int) h->preskip) ; + psf_log_printf (psf, " Input Samplerate : %d Hz\n", (int) h->input_samplerate) ; + psf_log_printf (psf, " Gain : %d.%d\n", (int) arith_shift_right (h->gain & 0xF0, 8), h->gain & 0x0F) ; psf_log_printf (psf, " Channel Mapping : ") ; switch (h->channel_mapping) { case 0 : psf_log_printf (psf, "0 (mono or stereo)\n") ; break ; case 1 : psf_log_printf (psf, "1 (surround, AC3 channel order)\n") ; break ; case 255 : psf_log_printf (psf, "255 (no channel order)\n") ; break ; - default : psf_log_printf (psf, "%d (unknown or unsupported)\n", h->channel_mapping) ; break ; + default : psf_log_printf (psf, "%d (unknown or unsupported)\n", (int) h->channel_mapping) ; break ; } ; if (h->channel_mapping > 0) { int i ; - psf_log_printf (psf, " streams total : %d\n", h->nb_streams) ; - psf_log_printf (psf, " streams coupled : %d\n", h->nb_coupled) ; + psf_log_printf (psf, " streams total : %d\n", (int) h->nb_streams) ; + psf_log_printf (psf, " streams coupled : %d\n", (int) h->nb_coupled) ; psf_log_printf (psf, " stream mapping : [") ; for (i = 0 ; i < h->channels - 1 ; i++) - psf_log_printf (psf, "%d,", h->stream_map [i]) ; - psf_log_printf (psf, "%d]\n", h->stream_map [i]) ; + psf_log_printf (psf, "%d,", (int) (h->stream_map [i])) ; + psf_log_printf (psf, "%d]\n", (int) (h->stream_map [i])) ; } ; } /* opus_print_header */ @@ -458,7 +458,7 @@ opus_read_header_packet (SF_PRIVATE *psf, OpusHeader *h, ogg_packet *opacket) count = psf_binheader_readf (psf, "ep1", 8, &h->version) ; if (! (h->version == 1 || h->version == 0)) - { psf_log_printf (psf, "Opus : Unknown / unsupported embedding scheme version: %d.\n", h->version) ; + { psf_log_printf (psf, "Opus : Unknown / unsupported embedding scheme version: %d.\n", (int) h->version) ; return SFE_UNIMPLEMENTED ; } ; @@ -955,8 +955,8 @@ ogg_opus_unpack_next_page (SF_PRIVATE *psf, OGG_PRIVATE *odata, OPUS_PRIVATE *oo oopus->pg_pos = odata->pkt [odata->pkt_len - 1].granulepos ; gp = ogg_opus_calculate_page_duration (odata) ; oopus->pkt_pos = oopus->pg_pos - gp ; - psf_log_printf (psf, "Opus : Hole found appears to be of length %d samples.\n", - (oopus->pkt_pos - last_page) / oopus->sr_factor) ; + psf_log_printf (psf, "Opus : Hole found appears to be of length %D samples.\n", + (oopus->pkt_pos - last_page) / (uint64_t) oopus->sr_factor) ; /* ** Could save the hole size here, and have ogg_opus_read_refill() ** do packet loss concealment until the hole is gone, but libopus does @@ -1050,7 +1050,7 @@ ogg_opus_read_refill (SF_PRIVATE *psf, OGG_PRIVATE *odata, OPUS_PRIVATE *oopus) ** MAY defer this action until it decodes the last packet ** completed on that page. */ - psf_log_printf (psf, "Opus : Mid-strem page's granule position %d is less than total samples of %d\n", oopus->pg_pos, pkt_granulepos) ; + psf_log_printf (psf, "Opus : Mid-stream page's granule position %D is less than total samples of %D\n", oopus->pg_pos, pkt_granulepos) ; psf->error = SFE_MALFORMED_FILE ; return -1 ; } ; @@ -1467,7 +1467,12 @@ ogg_opus_analyze_file (SF_PRIVATE *psf) { psf->sf.frames = (oopus->u.decode.gp_end - oopus->u.decode.gp_start - oopus->header.preskip) / oopus->sr_factor ; } ; - } + } ; + + + psf_log_printf (psf, " Granule pos offset : %D\n", oopus->u.decode.gp_start) ; + if (oopus->u.decode.gp_end != (uint64_t) -1) + psf_log_printf (psf, " Last Granule pos : %D\n", oopus->u.decode.gp_end) ; /* Go back to where we left off. */ ogg_sync_fseek (psf, saved_offset, SEEK_SET) ; diff --git a/src/ogg_vorbis.c b/src/ogg_vorbis.c index b2324dd6..916f04f0 100644 --- a/src/ogg_vorbis.c +++ b/src/ogg_vorbis.c @@ -264,9 +264,9 @@ vorbis_read_header (SF_PRIVATE *psf) ogg_sync_fseek (psf, saved_offset, SEEK_SET) ; } - psf_log_printf (psf, "PCM offset : %d\n", vdata->pcm_start) ; + psf_log_printf (psf, "PCM offset : %D\n", vdata->pcm_start) ; if (vdata->pcm_end != (uint64_t) -1) - psf_log_printf (psf, "PCM end : %d\n", vdata->pcm_end) ; + psf_log_printf (psf, "PCM end : %D\n", vdata->pcm_end) ; else psf_log_printf (psf, "PCM end : unknown\n") ; From 70175e9cc1d6aa88af903525f9f5c27b57f8754a Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Wed, 26 Jan 2022 15:01:27 -0800 Subject: [PATCH 169/204] ogg: Use macros for undocumented ogg_stream state queries. --- src/ogg.c | 2 +- src/ogg.h | 7 +++++++ src/ogg_opus.c | 6 +----- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/ogg.c b/src/ogg.c index 3bd510fa..3fdcee04 100644 --- a/src/ogg.c +++ b/src/ogg.c @@ -614,7 +614,7 @@ ogg_stream_seek_page_search (SF_PRIVATE *psf, OGG_PRIVATE *odata, uint64_t targe if (buffering) ogg_stream_reset (&odata->ostream) ; /* Check to see if the last packet continues. */ - if (page.header [27 + page.header [26] - 1] == 255) + if (ogg_page_continues (&page)) { ogg_page_search_continued_data (odata, &page) ; /* ** If we have a continued packet, remember the offset of diff --git a/src/ogg.h b/src/ogg.h index 5c837a8e..6a376858 100644 --- a/src/ogg.h +++ b/src/ogg.h @@ -57,6 +57,13 @@ typedef struct ((buf [base + 2] <<16) & 0xff0000) | \ ((buf [base + 1] << 8) & 0xff00) | \ (buf [base] & 0xff)) +/* +** LibOgg documentation is noted as being bad by it's author. +** Add some useful utility macros for introspecting Ogg pages. +*/ +#define ogg_page_segments(page_ptr) ((page_ptr)->header [26]) + +#define ogg_page_continues(page_ptr) ((page_ptr)->header [27 + (page_ptr)->header [26] - 1] == 255) int ogg_read_first_page (SF_PRIVATE *, OGG_PRIVATE *) ; diff --git a/src/ogg_opus.c b/src/ogg_opus.c index 79ad4df2..ad92b209 100644 --- a/src/ogg_opus.c +++ b/src/ogg_opus.c @@ -1118,11 +1118,7 @@ ogg_opus_write_out (SF_PRIVATE *psf, OGG_PRIVATE *odata, OPUS_PRIVATE *oopus) else nbytes = ogg_stream_pageout_fill (&odata->ostream, &odata->opage, 255 * 255) ; if (nbytes > 0) - { /* - ** LibOgg documentation is noted as being bad by it's author. Ogg - ** page header byte 26 is the segment count. - */ - oopus->u.encode.last_segments -= odata->opage.header [26] ; + { oopus->u.encode.last_segments -= ogg_page_segments (&odata->opage) ; oopus->pg_pos = oopus->pkt_pos ; ogg_write_page (psf, &odata->opage) ; } From 66bbb78801d9875b12e4b471f7ebb9308d2092b6 Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Tue, 5 Apr 2022 00:12:28 -0700 Subject: [PATCH 170/204] ogg_opus: Grow pre-roll based on experimentation, update comments --- src/ogg_opus.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/ogg_opus.c b/src/ogg_opus.c index ad92b209..ad20ca51 100644 --- a/src/ogg_opus.c +++ b/src/ogg_opus.c @@ -171,16 +171,26 @@ #define OGG_OPUS_COMMENT_PAD (512) /* Same as oggenc default */ /* -** Opus packets can be any multiple of 2.5ms (at 48kHz). We use the recommended +** When encoding, we can choose the size of the Opus frames. +** Valid values are 2.5, 5, 10, 20, 40, and 60 milliseconds. +** +** Frames smaller than 10ms can't use CELT (MDCT) mode. +** Frames larger than 20ms "are only interesting at fairly low bitrates." +** +** We choose the suggested default of 20ms for high-fidelity audio, however, +** maybe this could be user-selected, or triggered by bitrate command. ** default for non-realtime of 20ms. While longer packets reduce the overhead ** data somewhat, it also decreases the quality. */ #define OGG_OPUS_ENCODE_PACKET_LEN(samplerate) ((20 * (samplerate)) / 1000) /* -** How long does it take for a decoder to converge (avoiding flush on seek. +** The pre-roll is how long it takes for the decoder to converge. It converges +** pretty quickly, to within -40db within 80ms. However, this also depends on +** the signal. From experimentation, use the conservative pre-roll amount of +** 660ms after which the output is 32-bit-exact with high probability. */ -#define OGG_OPUS_PREROLL (80 * 48) /* 80 milliseconds */ +#define OGG_OPUS_PREROLL (660 * 48) /* 660 milliseconds (33 packets of 20ms) */ typedef struct { uint8_t version ; @@ -215,10 +225,12 @@ typedef struct { uint32_t serialno ; OpusHeader header ; - /* Granule position before the current packet */ + /* Encode: Granule position after the previous packet. + * Decode: Granule position after the current packet */ uint64_t pkt_pos ; - /* Granule position at the end of the current page (encode: last completed) */ + /* Encode: Granule position at the end of the previous page. + * Decode: Granule position at the end of the current page. */ uint64_t pg_pos ; /* integer coefficient of (current sample rate) / 48000Hz */ From 63338de7988f723b10ddf79ed54b4bbce5ad98ea Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Tue, 5 Apr 2022 00:14:31 -0700 Subject: [PATCH 171/204] ogg_opus: Fix double-counting of len on seek_null_read --- src/ogg_opus.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/ogg_opus.c b/src/ogg_opus.c index ad20ca51..19eb041f 100644 --- a/src/ogg_opus.c +++ b/src/ogg_opus.c @@ -1498,13 +1498,15 @@ ogg_opus_seek_null_read (SF_PRIVATE *psf, sf_count_t offset) { OGG_PRIVATE *odata = (OGG_PRIVATE *) psf->container_data ; OPUS_PRIVATE *oopus = (OPUS_PRIVATE *) psf->codec_data ; sf_count_t total ; - sf_count_t readlen ; - - total = oopus->pkt_pos / oopus->sr_factor ; - total += oopus->loc ; + total = (oopus->pkt_pos / oopus->sr_factor) - (oopus->len - oopus->loc) ; for ( ; total < offset ; ) - { if (oopus->loc == oopus->len) + { sf_count_t readlen = SF_MIN ((int) (offset - total), (oopus->len - oopus->loc)) ; + if (readlen > 0) + { total += readlen ; + oopus->loc += readlen ; + } ; + if (oopus->loc == oopus->len) { if (ogg_opus_read_refill (psf, odata, oopus) <= 0) return total ; /* @@ -1513,12 +1515,6 @@ ogg_opus_seek_null_read (SF_PRIVATE *psf, sf_count_t offset) */ oopus->loc = 0 ; } ; - - readlen = SF_MIN ((int) (offset - total), (oopus->len - oopus->loc)) ; - if (readlen > 0) - { total += readlen ; - oopus->loc += readlen ; - } ; } ; return total ; } /* ogg_opus_seek_null_read */ @@ -1610,12 +1606,20 @@ ogg_opus_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) return PSF_SEEK_ERROR ; } ; - current = oopus->pkt_pos + oopus->loc * oopus->sr_factor ; + /* + ** Use the start of the current decoded buffer as the current position, + ** avoids doing an actual seek if the target is within the current buffer. + */ + oopus->loc = 0 ; + current = oopus->pkt_pos - (uint64_t) (oopus->len * oopus->sr_factor) ; + /* ** Remember, there are preskip granulepos worth of samples at the front of ** the stream which are bunk. Also, granule positions can be offset. */ - target_gp = offset * oopus->sr_factor + oopus->u.decode.gp_start + oopus->header.preskip ; + target_gp = offset * oopus->sr_factor ; + target_gp += oopus->u.decode.gp_start ; + target_gp += oopus->header.preskip ; if (oopus->u.decode.gp_end == (uint64_t) -1) { /* @@ -1628,9 +1632,9 @@ ogg_opus_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) { /* ** Avoid seeking in the file if where we want is just ahead or exactly ** were we are. To avoid needing to flush the decoder we choose pre- - ** roll plus 10ms. + ** roll. */ - if (target_gp < current || target_gp - current > OGG_OPUS_PREROLL + 10 * 48) + if (target_gp < current || target_gp - current > OGG_OPUS_PREROLL) { ret = ogg_opus_seek_page_search (psf, target_gp) ; if (ret < 0) { /* From 08c34a81a5e28f566b593680e0f35779592c6690 Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Tue, 5 Apr 2022 00:16:00 -0700 Subject: [PATCH 172/204] ogg_opus: Cleanups and robustness fixes to seek_page_search --- src/ogg_opus.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/ogg_opus.c b/src/ogg_opus.c index 19eb041f..f0700c6f 100644 --- a/src/ogg_opus.c +++ b/src/ogg_opus.c @@ -1532,31 +1532,42 @@ ogg_opus_seek_page_search (SF_PRIVATE *psf, uint64_t target_gp) uint64_t best_gp ; sf_count_t begin ; sf_count_t end ; + sf_count_t old_pos ; int ret ; best_gp = pcm_start = oopus->u.decode.gp_start ; pcm_end = oopus->u.decode.gp_end ; begin = psf->dataoffset ; - /* Adjust the target to give time to converge. */ - if (target_gp >= OGG_OPUS_PREROLL) - target_gp -= OGG_OPUS_PREROLL ; - if (target_gp < pcm_start) - target_gp = pcm_start ; - - /* Seek to beginning special case */ - if (target_gp < pcm_start + (uint64_t) oopus->header.preskip) - end = begin ; - else + /* + ** Adjust the search for a page at least OGG_OPUS_PREROLL before our actual + ** target, to give time for the decoder to converge. + */ + if (target_gp >= OGG_OPUS_PREROLL + pcm_start + (uint64_t) oopus->header.preskip) + { target_gp -= OGG_OPUS_PREROLL ; end = oopus->u.decode.last_offset ; + } + else + { target_gp = pcm_start + (uint64_t) oopus->header.preskip ; + end = begin ; + } ; - ogg_stream_seek_page_search (psf, odata, target_gp, pcm_start, pcm_end, &best_gp, begin, end) ; + /* Search the Ogg stream for such a page */ + old_pos = ogg_sync_ftell (psf) ; + ret = ogg_stream_seek_page_search (psf, odata, target_gp, pcm_start, pcm_end, &best_gp, begin, end) ; + if (ret != 0) + { ogg_sync_fseek (psf, old_pos, SEEK_SET) ; + return ret ; + } ; + /* Load the page that contains our pre-roll target */ oopus->loc = 0 ; oopus->len = 0 ; if ((ret = ogg_opus_unpack_next_page (psf, odata, oopus)) != 1) return ret ; oopus->pkt_pos = best_gp ; + + /* Reset the decoder (gain settings survive the reset) */ opus_multistream_decoder_ctl (oopus->u.decode.state, OPUS_RESET_STATE) ; /* Gain decoder settings survive resets. */ From fdeb8438add455e60650a3ea7e823eb5addd129e Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Tue, 5 Apr 2022 02:08:00 -0700 Subject: [PATCH 173/204] ogg_opus: Fix preskip of granulepos offset files --- src/ogg_opus.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ogg_opus.c b/src/ogg_opus.c index f0700c6f..f91c027a 100644 --- a/src/ogg_opus.c +++ b/src/ogg_opus.c @@ -1084,8 +1084,8 @@ ogg_opus_read_refill (SF_PRIVATE *psf, OGG_PRIVATE *odata, OPUS_PRIVATE *oopus) ** skip point, indicating that these samples are padding to get the decoder ** to converge and should be dropped. */ - if (oopus->pkt_pos < (unsigned) oopus->header.preskip) - oopus->loc = SF_MIN ((oopus->header.preskip - (int) oopus->pkt_pos) / oopus->sr_factor, oopus->len) ; + if (oopus->pkt_pos < oopus->u.decode.gp_start + (uint64_t) oopus->header.preskip) + oopus->loc = SF_MIN ((int) ((uint64_t) oopus->header.preskip + oopus->u.decode.gp_start - oopus->pkt_pos) / oopus->sr_factor, oopus->len) ; else oopus->loc = 0 ; From 79e654fe6bcc0c3c3d8c89bda157a9264d297fd2 Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Tue, 5 Apr 2022 02:37:17 -0700 Subject: [PATCH 174/204] ogg_opus: Rearange seek pre-roll logic --- src/ogg_opus.c | 100 +++++++++++++++++++++++++------------------------ 1 file changed, 51 insertions(+), 49 deletions(-) diff --git a/src/ogg_opus.c b/src/ogg_opus.c index f91c027a..296d96b1 100644 --- a/src/ogg_opus.c +++ b/src/ogg_opus.c @@ -1084,8 +1084,8 @@ ogg_opus_read_refill (SF_PRIVATE *psf, OGG_PRIVATE *odata, OPUS_PRIVATE *oopus) ** skip point, indicating that these samples are padding to get the decoder ** to converge and should be dropped. */ - if (oopus->pkt_pos < oopus->u.decode.gp_start + (uint64_t) oopus->header.preskip) - oopus->loc = SF_MIN ((int) ((uint64_t) oopus->header.preskip + oopus->u.decode.gp_start - oopus->pkt_pos) / oopus->sr_factor, oopus->len) ; + if (oopus->pkt_pos < (unsigned) oopus->header.preskip) + oopus->loc = SF_MIN ((oopus->header.preskip - (int) oopus->pkt_pos) / oopus->sr_factor, oopus->len) ; else oopus->loc = 0 ; @@ -1538,19 +1538,7 @@ ogg_opus_seek_page_search (SF_PRIVATE *psf, uint64_t target_gp) best_gp = pcm_start = oopus->u.decode.gp_start ; pcm_end = oopus->u.decode.gp_end ; begin = psf->dataoffset ; - - /* - ** Adjust the search for a page at least OGG_OPUS_PREROLL before our actual - ** target, to give time for the decoder to converge. - */ - if (target_gp >= OGG_OPUS_PREROLL + pcm_start + (uint64_t) oopus->header.preskip) - { target_gp -= OGG_OPUS_PREROLL ; - end = oopus->u.decode.last_offset ; - } - else - { target_gp = pcm_start + (uint64_t) oopus->header.preskip ; - end = begin ; - } ; + end = oopus->u.decode.last_offset ; /* Search the Ogg stream for such a page */ old_pos = ogg_sync_ftell (psf) ; @@ -1569,7 +1557,6 @@ ogg_opus_seek_page_search (SF_PRIVATE *psf, uint64_t target_gp) /* Reset the decoder (gain settings survive the reset) */ opus_multistream_decoder_ctl (oopus->u.decode.state, OPUS_RESET_STATE) ; - /* Gain decoder settings survive resets. */ return 0 ; } /* ogg_opus_seek_page_search */ @@ -1581,11 +1568,6 @@ ogg_opus_seek_manual (SF_PRIVATE *psf, uint64_t target_gp) sf_count_t pos ; int nn ; - if (target_gp > OGG_OPUS_PREROLL) - target_gp -= OGG_OPUS_PREROLL ; - if (target_gp < oopus->pg_pos) - target_gp = oopus->pg_pos ; - if (oopus->pg_pos > target_gp) { ogg_stream_reset (&odata->ostream) ; pos = ogg_sync_fseek (psf, psf->dataoffset, SEEK_SET) ; @@ -1606,9 +1588,9 @@ ogg_opus_seek_manual (SF_PRIVATE *psf, uint64_t target_gp) static sf_count_t ogg_opus_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) -{ OPUS_PRIVATE *oopus = (OPUS_PRIVATE *) psf->codec_data ; - uint64_t target_gp ; - uint64_t current ; +{ OGG_PRIVATE *odata = (OGG_PRIVATE *) psf->container_data ; + OPUS_PRIVATE *oopus = (OPUS_PRIVATE *) psf->codec_data ; + uint64_t target_gp, current_gp ; int ret ; /* Only support seeking in read mode. */ @@ -1617,36 +1599,40 @@ ogg_opus_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) return PSF_SEEK_ERROR ; } ; - /* - ** Use the start of the current decoded buffer as the current position, - ** avoids doing an actual seek if the target is within the current buffer. - */ + /* Figure out the current position granule pos. Use the start of the + * current buffer, to avoid backwards seeking if the target is on the page + * but before the current locaiton. */ oopus->loc = 0 ; - current = oopus->pkt_pos - (uint64_t) (oopus->len * oopus->sr_factor) ; + current_gp = oopus->pkt_pos - (uint64_t) (oopus->len * oopus->sr_factor) ; - /* - ** Remember, there are preskip granulepos worth of samples at the front of - ** the stream which are bunk. Also, granule positions can be offset. - */ + /* Calculate the target granule pos. This includes the decoder delay and + * the file granule position offset. */ target_gp = offset * oopus->sr_factor ; target_gp += oopus->u.decode.gp_start ; target_gp += oopus->header.preskip ; - if (oopus->u.decode.gp_end == (uint64_t) -1) - { /* - ** Don't know the end of the file. Could be a chained file we don't yet - ** support. Oh well, just do it manually. - */ - ogg_opus_seek_manual (psf, target_gp) ; - } - else - { /* - ** Avoid seeking in the file if where we want is just ahead or exactly - ** were we are. To avoid needing to flush the decoder we choose pre- - ** roll. - */ - if (target_gp < current || target_gp - current > OGG_OPUS_PREROLL) - { ret = ogg_opus_seek_page_search (psf, target_gp) ; + /* Check if we need to do a page seek. */ + if (target_gp < current_gp || target_gp - current_gp > OGG_OPUS_PREROLL) + { uint64_t preroll_gp ; + + /* For a page seek, use an earlier target granule pos, giving the + * decoder samples to converge before the actual target. */ + if (target_gp >= OGG_OPUS_PREROLL + oopus->u.decode.gp_start + (uint64_t) oopus->header.preskip) + { preroll_gp = target_gp - OGG_OPUS_PREROLL ; + } + else + { preroll_gp = oopus->u.decode.gp_start + (uint64_t) oopus->header.preskip ; + } ; + + if (oopus->u.decode.gp_end == (uint64_t) -1) + { /* + ** Don't know the end of the file. Could be a chained file we don't yet + ** support. Oh well, just do it manually. + */ + ogg_opus_seek_manual (psf, preroll_gp) ; + } + else + { ret = ogg_opus_seek_page_search (psf, preroll_gp) ; if (ret < 0) { /* ** Page seek failed, what to do? Could be bad data. We can @@ -1654,7 +1640,7 @@ ogg_opus_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ** from the beginning has the advantage of finding where the ** file goes bad. */ - ret = ogg_opus_seek_manual (psf, target_gp) ; + ret = ogg_opus_seek_manual (psf, preroll_gp) ; if (ret < 0) { /* ** If were here, and there is no error, we can be pretty @@ -1666,6 +1652,22 @@ ogg_opus_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) } ; } ; } ; + + /* + ** Skip over packets on the found page that are before our pre-roll + ** target to avoid unnecessary decoding, and make decoder convergence + ** independent of page boundaries for more visible errors. + */ + for ( ; odata->pkt_indx != odata->pkt_len ; ) + { ogg_packet *ppkt = &odata->pkt [odata->pkt_indx] ; + int nsamp = opus_packet_get_nb_samples (ppkt->packet, ppkt->bytes, 48000) ; + if (oopus->pkt_pos + nsamp < preroll_gp) + { oopus->pkt_pos += nsamp ; + odata->pkt_indx++ ; + } + else + break ; + } ; } ; /* From 5c79b988909dd5c0ffc8f3941d65ac1061383b7d Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Tue, 5 Apr 2022 02:41:45 -0700 Subject: [PATCH 175/204] ogg_opus: Rename page seeking funcitons --- src/ogg_opus.c | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/ogg_opus.c b/src/ogg_opus.c index 296d96b1..889f53d7 100644 --- a/src/ogg_opus.c +++ b/src/ogg_opus.c @@ -303,9 +303,9 @@ static sf_count_t ogg_opus_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_ static sf_count_t ogg_opus_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ; static sf_count_t ogg_opus_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ; -static sf_count_t ogg_opus_seek_null_read (SF_PRIVATE *psf, sf_count_t offset) ; -static sf_count_t ogg_opus_seek_manual (SF_PRIVATE *psf, uint64_t target_gp) ; -static int ogg_opus_seek_page_search (SF_PRIVATE *psf, uint64_t target_gp) ; +static sf_count_t ogg_opus_null_read (SF_PRIVATE *psf, sf_count_t offset) ; +static sf_count_t ogg_opus_page_seek_manual (SF_PRIVATE *psf, uint64_t target_gp) ; +static int ogg_opus_page_seek_search (SF_PRIVATE *psf, uint64_t target_gp) ; static int ogg_opus_analyze_file (SF_PRIVATE *psf) ; static int ogg_opus_command (SF_PRIVATE *psf, int command, void *data, int datasize) ; @@ -1488,13 +1488,13 @@ ogg_opus_analyze_file (SF_PRIVATE *psf) } /* ogg_opus_analyze_file */ /* -** ogg_opus_seek_null_read +** ogg_opus_null_read ** ** Decode samples, doing nothing with them, until the desired granule position ** is reached. */ static sf_count_t -ogg_opus_seek_null_read (SF_PRIVATE *psf, sf_count_t offset) +ogg_opus_null_read (SF_PRIVATE *psf, sf_count_t offset) { OGG_PRIVATE *odata = (OGG_PRIVATE *) psf->container_data ; OPUS_PRIVATE *oopus = (OPUS_PRIVATE *) psf->codec_data ; sf_count_t total ; @@ -1517,14 +1517,16 @@ ogg_opus_seek_null_read (SF_PRIVATE *psf, sf_count_t offset) } ; } ; return total ; -} /* ogg_opus_seek_null_read */ +} /* ogg_opus_null_read */ /* +** ogg_opus_page_seek_search +** ** Search within the file for the page with the highest granule position at or ** before our target. */ static int -ogg_opus_seek_page_search (SF_PRIVATE *psf, uint64_t target_gp) +ogg_opus_page_seek_search (SF_PRIVATE *psf, uint64_t target_gp) { OGG_PRIVATE *odata = (OGG_PRIVATE *) psf->container_data ; OPUS_PRIVATE *oopus = (OPUS_PRIVATE *) psf->codec_data ; uint64_t pcm_start ; @@ -1559,10 +1561,16 @@ ogg_opus_seek_page_search (SF_PRIVATE *psf, uint64_t target_gp) opus_multistream_decoder_ctl (oopus->u.decode.state, OPUS_RESET_STATE) ; return 0 ; -} /* ogg_opus_seek_page_search */ +} /* ogg_opus_page_seek_search */ +/* +** ogg_opus_page_seek_manual +** +** Seek to the beginning of the Ogg stream and read pages until we find one with +** a granule position at or before our target. +*/ static sf_count_t -ogg_opus_seek_manual (SF_PRIVATE *psf, uint64_t target_gp) +ogg_opus_page_seek_manual (SF_PRIVATE *psf, uint64_t target_gp) { OGG_PRIVATE *odata = (OGG_PRIVATE *) psf->container_data ; OPUS_PRIVATE *oopus = (OPUS_PRIVATE *) psf->codec_data ; sf_count_t pos ; @@ -1584,7 +1592,7 @@ ogg_opus_seek_manual (SF_PRIVATE *psf, uint64_t target_gp) } ; return 1 ; -} /* ogg_opus_seek_manual */ +} /* ogg_opus_page_seek_manual */ static sf_count_t ogg_opus_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) @@ -1629,10 +1637,10 @@ ogg_opus_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ** Don't know the end of the file. Could be a chained file we don't yet ** support. Oh well, just do it manually. */ - ogg_opus_seek_manual (psf, preroll_gp) ; + ogg_opus_page_seek_manual (psf, preroll_gp) ; } else - { ret = ogg_opus_seek_page_search (psf, preroll_gp) ; + { ret = ogg_opus_page_seek_search (psf, preroll_gp) ; if (ret < 0) { /* ** Page seek failed, what to do? Could be bad data. We can @@ -1640,7 +1648,7 @@ ogg_opus_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ** from the beginning has the advantage of finding where the ** file goes bad. */ - ret = ogg_opus_seek_manual (psf, preroll_gp) ; + ret = ogg_opus_page_seek_manual (psf, preroll_gp) ; if (ret < 0) { /* ** If were here, and there is no error, we can be pretty @@ -1674,7 +1682,7 @@ ogg_opus_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ** We've seeked or skipped through pages until just before our target, ** now decode until we hit it. */ - offset = ogg_opus_seek_null_read (psf, target_gp / oopus->sr_factor) ; + offset = ogg_opus_null_read (psf, target_gp / oopus->sr_factor) ; return offset - ((oopus->header.preskip + oopus->u.decode.gp_start) / oopus->sr_factor) ; } /* ogg_opus_seek */ From bbe95850d9c099818e161d2a6328263c9c10102d Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Fri, 8 Apr 2022 09:41:14 -0700 Subject: [PATCH 176/204] sndfile.h: Fix accidental command enum reuse for GET_OGG_STERAM_SERIALNO --- docs/command.md | 2 +- include/sndfile.h.in | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/command.md b/docs/command.md index 9014b172..b5a76f6d 100644 --- a/docs/command.md +++ b/docs/command.md @@ -1286,7 +1286,7 @@ sndfile : A valid SNDFILE* pointer cmd -: SFC_SET_OGG_PAGE_LATENCY_MS +: SFC_SET_OGG_STREAM_SERIALNO data : A pointer to a 32-bit int value diff --git a/include/sndfile.h.in b/include/sndfile.h.in index 61a36540..8842fd48 100644 --- a/include/sndfile.h.in +++ b/include/sndfile.h.in @@ -224,7 +224,7 @@ enum /* Ogg format commands */ SFC_SET_OGG_PAGE_LATENCY_MS = 0x1302, SFC_SET_OGG_PAGE_LATENCY = 0x1303, - SFC_GET_OGG_STREAM_SERIALNO = 0x1304, + SFC_GET_OGG_STREAM_SERIALNO = 0x1306, SFC_GET_BITRATE_MODE = 0x1304, SFC_SET_BITRATE_MODE = 0x1305, From 7c2053ac96d408b8861b261f680adb957c62026a Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Fri, 8 Apr 2022 10:14:51 -0700 Subject: [PATCH 177/204] ogg: convert helper macros to inline functions, documentation updates --- src/ogg.h | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/ogg.h b/src/ogg.h index 6a376858..b0e80574 100644 --- a/src/ogg.h +++ b/src/ogg.h @@ -57,14 +57,36 @@ typedef struct ((buf [base + 2] <<16) & 0xff0000) | \ ((buf [base + 1] << 8) & 0xff00) | \ (buf [base] & 0xff)) +/*----------------------------------------------------------------------------------------------- +** Inline functions. +*/ + /* ** LibOgg documentation is noted as being bad by it's author. -** Add some useful utility macros for introspecting Ogg pages. +** Add some useful utility inline functions for introspecting Ogg pages. */ -#define ogg_page_segments(page_ptr) ((page_ptr)->header [26]) -#define ogg_page_continues(page_ptr) ((page_ptr)->header [27 + (page_ptr)->header [26] - 1] == 255) +/* ogg_page_segments returns how many segments are in this page. */ +static inline int +ogg_page_segments (ogg_page *pg) +{ return (int) (pg->header [26]) ; } +/* ogg_page_continues returns true if this page ends in a continued packet. */ +static inline int +ogg_page_continues (ogg_page *pg) +{ return pg->header [27 + pg->header [26] - 1] == 255 ; +} + +/*----------------------------------------------------------------------------------------------- +** Exported functions. +*/ + +/* +** ogg_read_first_page loads the first Ogg page found in the file, and sets the +** OGG_PRIVATE serialno to match the logical stream of the page. Data is read +** without seeking backwards, loading any data present from psf->header into +** the ogg_sync state first, so that this function works with pipes. +*/ int ogg_read_first_page (SF_PRIVATE *, OGG_PRIVATE *) ; /* From 718e305dc1e9af3e26faace0fcacf8a95e743b5f Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Fri, 8 Apr 2022 11:13:35 -0700 Subject: [PATCH 178/204] ogg: Correct Opus-assumption of gp rate in _seek_page_search() --- src/ogg.c | 6 ++++-- src/ogg.h | 12 +++++++++--- src/ogg_opus.c | 2 +- src/ogg_vorbis.c | 2 +- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/ogg.c b/src/ogg.c index 3fdcee04..f8cb0a47 100644 --- a/src/ogg.c +++ b/src/ogg.c @@ -447,7 +447,9 @@ ogg_sync_last_page_before (SF_PRIVATE *psf, OGG_PRIVATE *odata, uint64_t *gp_out } /* ogg_sync_last_page_before */ int -ogg_stream_seek_page_search (SF_PRIVATE *psf, OGG_PRIVATE *odata, uint64_t target_gp, uint64_t pcm_start, uint64_t pcm_end, uint64_t *best_gp, sf_count_t begin, sf_count_t end) +ogg_stream_seek_page_search (SF_PRIVATE *psf, OGG_PRIVATE *odata, + uint64_t target_gp, uint64_t pcm_start, uint64_t pcm_end, uint64_t *best_gp, + sf_count_t begin, sf_count_t end, uint64_t gp_rate) { ogg_page page ; uint64_t gp ; sf_count_t d0, d1, d2 ; @@ -632,7 +634,7 @@ ogg_stream_seek_page_search (SF_PRIVATE *psf, OGG_PRIVATE *odata, uint64_t targe buffering = SF_TRUE ; } ; *best_gp = pcm_start = gp ; - if (target_gp - gp > 48000) + if (target_gp - gp > gp_rate) { /* Out by over a second. Try another bisection. */ break ; } diff --git a/src/ogg.h b/src/ogg.h index b0e80574..8b2927a9 100644 --- a/src/ogg.h +++ b/src/ogg.h @@ -154,10 +154,16 @@ int ogg_stream_unpack_page (SF_PRIVATE *psf, OGG_PRIVATE *odata) ; ** Preforms a bisection search. If not found exactly, the best result is ** returned in *best_gp. Found page is loaded into the virtual bitstream, ** ready for unpacking. Arguments pcm_start and pcm_end are the highest and -** lowest granule positions of the file. begin and end are the file offsets. +** lowest granule positions of the file. begin and end are the file offset +** range to search. gp_rate is an information hint so granule positions can +** be correlated to playback time, so the search can figure out how close it +** is, should be granule positions per second. */ int ogg_stream_seek_page_search (SF_PRIVATE *psf, OGG_PRIVATE *odata, - uint64_t target_gp, uint64_t pcm_start, uint64_t pcm_end, - uint64_t *best_gp, sf_count_t begin, sf_count_t end) ; + uint64_t target_gp, + uint64_t pcm_start, uint64_t pcm_end, + uint64_t *best_gp, + sf_count_t begin, sf_count_t end, + uint64_t gp_rate) ; #endif /* SF_SRC_OGG_H */ diff --git a/src/ogg_opus.c b/src/ogg_opus.c index 889f53d7..c9383624 100644 --- a/src/ogg_opus.c +++ b/src/ogg_opus.c @@ -1544,7 +1544,7 @@ ogg_opus_page_seek_search (SF_PRIVATE *psf, uint64_t target_gp) /* Search the Ogg stream for such a page */ old_pos = ogg_sync_ftell (psf) ; - ret = ogg_stream_seek_page_search (psf, odata, target_gp, pcm_start, pcm_end, &best_gp, begin, end) ; + ret = ogg_stream_seek_page_search (psf, odata, target_gp, pcm_start, pcm_end, &best_gp, begin, end, 48000) ; if (ret != 0) { ogg_sync_fseek (psf, old_pos, SEEK_SET) ; return ret ; diff --git a/src/ogg_vorbis.c b/src/ogg_vorbis.c index 916f04f0..f9428ed1 100644 --- a/src/ogg_vorbis.c +++ b/src/ogg_vorbis.c @@ -899,7 +899,7 @@ vorbis_seek_trysearch (SF_PRIVATE *psf, uint64_t target_gp) search_target_gp = search_target_gp < target_gp ? target_gp - search_target_gp : 0 ; ret = ogg_stream_seek_page_search (psf, odata, search_target_gp, vdata->pcm_start, - vdata->pcm_end, &best_gp, psf->dataoffset, vdata->last_page) ; + vdata->pcm_end, &best_gp, psf->dataoffset, vdata->last_page, vdata->vinfo.rate) ; if (ret < 0) return ret ; From a67c601b0ca4bb1d605164746a929ec74cdc8ebb Mon Sep 17 00:00:00 2001 From: Stefan Westerfeld Date: Thu, 31 Mar 2022 16:16:28 +0200 Subject: [PATCH 179/204] Support skipping large wav chunks on stdin. Some wav files can have rather large chunks with chunk types that are not supported by libsndfile at all. For those, header_seek will just skip the whole chunk. This commit makes skipping the chunk work even if the input is stdin (which doesn't support psf_fseek), by reading over the header using psf_fread in that case. --- CHANGELOG.md | 1 + src/common.c | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3877099..18ad419d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * CMake build from Autotools tarball (issue #816). * Build on UWP platform (issue #824). * Fix signed integer overflow (issue #785). +* Skipping large wav chunks on stdin (PR #819). ## [1.1.0] - 2022-03-27 diff --git a/src/common.c b/src/common.c index c4ef4a66..ddf621b0 100644 --- a/src/common.c +++ b/src/common.c @@ -925,8 +925,22 @@ header_seek (SF_PRIVATE *psf, sf_count_t position, int whence) if (psf->header.indx + position > psf->header.len) { /* Need to jump this without caching it. */ + position -= (psf->header.end - psf->header.indx) ; psf->header.indx = psf->header.end ; - psf_fseek (psf, position, SEEK_CUR) ; + if (psf->is_pipe) + { + /* seeking is not supported on pipe input, so we read instead */ + size_t skip = position ; + while (skip) + { char junk [16 * 1024] ; + size_t to_skip = SF_MIN (skip, sizeof (junk)) ; + psf_fread (junk, 1, to_skip, psf) ; + skip -= to_skip ; + } + } + else + { psf_fseek (psf, position, SEEK_CUR) ; + } break ; } ; From e4fdaeefddd39bae1db27d48ccb7db7733e0c009 Mon Sep 17 00:00:00 2001 From: Michael Cho Date: Sun, 17 Apr 2022 21:31:07 -0700 Subject: [PATCH 180/204] configure.ac: substitute EXTERNAL_MPEG_LIBS in sndfile.pc --- configure.ac | 1 + 1 file changed, 1 insertion(+) diff --git a/configure.ac b/configure.ac index 727b67bc..a4c776d7 100644 --- a/configure.ac +++ b/configure.ac @@ -739,6 +739,7 @@ AC_SUBST(SNDIO_LIBS) AC_SUBST(EXTERNAL_XIPH_CFLAGS) AC_SUBST(EXTERNAL_XIPH_LIBS) AC_SUBST(EXTERNAL_XIPH_REQUIRE) +AC_SUBST(EXTERNAL_MPEG_LIBS) AC_SUBST(EXTERNAL_MPEG_REQUIRE) AC_SUBST(MPG123_CFLAGS) AC_SUBST(MPG123_LIBS) From a17e32fda6ed6883bebe0d5f7e1c83cd88409bd6 Mon Sep 17 00:00:00 2001 From: evpobr Date: Mon, 18 Apr 2022 13:42:20 +0500 Subject: [PATCH 181/204] Use Visual Studio 2022 for GitHub Actions --- .github/workflows/action.yml | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index 23bb27a0..65a1a04c 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -18,10 +18,10 @@ jobs: ubuntu-clang-cmake-shared, macos-cmake, macos-cmake-shared, - windows-vs2019-x64, - windows-vs2019-x64-shared, - windows-vs2019-Win32, - windows-vs2019-Win32-shared + windows-vs2022-x64, + windows-vs2022-x64-shared, + windows-vs2022-Win32, + windows-vs2022-Win32-shared ] include: - name: ubuntu-gcc-autotools @@ -121,22 +121,22 @@ jobs: -DCMAKE_C_FLAGS="-Wall -Wextra" -DCMAKE_VERBOSE_MAKEFILE=ON - - name: windows-vs2019-x64 + - name: windows-vs2022-x64 os: windows-latest triplet: 'x64-windows-static' build-system: cmake - cmake-generator: 'Visual Studio 16 2019' + cmake-generator: 'Visual Studio 17 2022' cmake-options: >- -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded$<$:Debug> -DCMAKE_BUILD_TYPE=Release -DVCPKG_TARGET_TRIPLET=x64-windows-static -DCMAKE_TOOLCHAIN_FILE=c:/vcpkg/scripts/buildsystems/vcpkg.cmake - - name: windows-vs2019-x64-shared + - name: windows-vs2022-x64-shared os: windows-latest triplet: 'x64-windows-static' build-system: cmake - cmake-generator: 'Visual Studio 16 2019' + cmake-generator: 'Visual Studio 17 2022' cmake-options: >- -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded$<$:Debug> -DBUILD_SHARED_LIBS=ON @@ -147,11 +147,11 @@ jobs: -DVCPKG_TARGET_TRIPLET=x64-windows-static -DCMAKE_TOOLCHAIN_FILE=c:/vcpkg/scripts/buildsystems/vcpkg.cmake - - name: windows-vs2019-Win32 + - name: windows-vs2022-Win32 os: windows-latest triplet: 'x86-windows-static' build-system: cmake - cmake-generator: 'Visual Studio 16 2019' + cmake-generator: 'Visual Studio 17 2022' cmake-options: >- -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded$<$:Debug> -DCMAKE_GENERATOR_PLATFORM=Win32 @@ -159,11 +159,11 @@ jobs: -DVCPKG_TARGET_TRIPLET=x86-windows-static -DCMAKE_TOOLCHAIN_FILE=c:/vcpkg/scripts/buildsystems/vcpkg.cmake - - name: windows-vs2019-Win32-shared + - name: windows-vs2022-Win32-shared os: windows-latest triplet: 'x86-windows-static' build-system: cmake - cmake-generator: 'Visual Studio 16 2019' + cmake-generator: 'Visual Studio 17 2022' cmake-options: >- -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded$<$:Debug> -DCMAKE_GENERATOR_PLATFORM=Win32 From a4f1387ab8084fac7f0301b18e01011be9381672 Mon Sep 17 00:00:00 2001 From: Shaohua Li Date: Mon, 30 May 2022 10:01:37 +0200 Subject: [PATCH 182/204] Update g72x.c Fixing signed integer overflow as reported in https://github.com/libsndfile/libsndfile/issues/757 . A similar fix was applied before https://github.com/libsndfile/libsndfile/pull/818 --- src/g72x.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/g72x.c b/src/g72x.c index 8698ebf7..62e40051 100644 --- a/src/g72x.c +++ b/src/g72x.c @@ -141,7 +141,7 @@ g72x_init (SF_PRIVATE * psf) else pg72x->blocks_total = psf->datalength / pg72x->blocksize ; - psf->sf.frames = pg72x->blocks_total * pg72x->samplesperblock ; + psf->sf.frames = (sf_count_t) pg72x->blocks_total * pg72x->samplesperblock ; psf_g72x_decode_block (psf, pg72x) ; } From 53e7dee23435f16671e00a22ab1277624bae6f8e Mon Sep 17 00:00:00 2001 From: Justace Clutter Date: Mon, 11 Jul 2022 04:32:40 -0400 Subject: [PATCH 183/204] Remove samplerate < SF_MAX_SAMPLERATE requirement (#851) Related to #850. Co-authored-by: evpobr --- CHANGELOG.md | 7 +++++++ docs/api.md | 7 +++---- src/common.h | 3 --- src/sndfile.c | 2 +- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18ad419d..d61761ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Fix signed integer overflow (issue #785). * Skipping large wav chunks on stdin (PR #819). +### Removed + +* Maximum samplerate limit, thanks @drmpeg, @justacec (issue #850). + + In version 1.1.0, an artificial limit of 655350 Hz was created, but as it + turned out, this is not enough for some scenarios. + ## [1.1.0] - 2022-03-27 ### Added diff --git a/docs/api.md b/docs/api.md index 7025443b..047fb4ab 100644 --- a/docs/api.md +++ b/docs/api.md @@ -97,10 +97,9 @@ calling **sf_open**(). The only exception to this is the case of RAW files where the caller has to set the **samplerate**, **channels** and **format** fields to valid values. All other fields of the structure are filled in by the library. -**Note:** The libsndfile library will reject values ​​for **samplerate** field -that are greater than `655350` and values ​​for field **channels** that are -greater than `1024`. These values ​​represent the maximum theoretical limit and -may be less for specific formats. +**Note:** The libsndfile library will reject values ​​for field **channels** that +are greater than `1024`. These value ​​represent the maximum theoretical limit +and may be less for specific formats. When opening a file for write, the caller must fill in structure members **samplerate**, **channels**, and **format**. diff --git a/src/common.h b/src/common.h index a8ef0b6c..c7747c5c 100644 --- a/src/common.h +++ b/src/common.h @@ -96,9 +96,6 @@ #define SF_MAX_CHANNELS 1024 -/* Max FLAC sample rate : https://xiph.org/flac/format.html */ -#define SF_MAX_SAMPLERATE 655350 - /* * Macros for spliting the format file of SF_INFO into container type, diff --git a/src/sndfile.c b/src/sndfile.c index 25177230..21975f65 100644 --- a/src/sndfile.c +++ b/src/sndfile.c @@ -2908,7 +2908,7 @@ retry: static int validate_sfinfo (SF_INFO *sfinfo) -{ if ((sfinfo->samplerate < 1) || (sfinfo->samplerate > SF_MAX_SAMPLERATE)) +{ if (sfinfo->samplerate < 1) return 0 ; if (sfinfo->frames < 0) return 0 ; From 36216ba105fb22c625955c056d4935b733945702 Mon Sep 17 00:00:00 2001 From: Benjamin Moody Date: Tue, 26 Jul 2022 14:49:41 -0400 Subject: [PATCH 184/204] flac: fix reading a large number of samples at once When reading a FLAC file, the internal function flac_read_loop is used to read a block of samples and copy them to the application-provided output buffer. The 'len' argument to flac_read_loop is the total number of samples to read (i.e., duration times number of channels.) Various fields in the FLAC_PRIVATE structure, as well as local variables in various internal functions, are 'unsigned', which means that trying to read into a buffer larger than UINT_MAX will cause problems. To avoid these problems, each function that calls flac_read_loop breaks up the operation into buffers of at most 0x1000000 (2^24) total samples. However, flac_read_loop will also fail (due to the condition at line 224) if the requested 'len' is not a multiple of the number of channels. Consequently, if an application tried to read: - more than 5592405 frames at once from a three-channel FLAC file - more than 3355443 frames at once from a five-channel FLAC file - more than 2796202 frames at once from a six-channel FLAC file - more than 2396745 frames at once from a seven-channel FLAC file then sf_read_* or sf_readf_* would incorrectly fail and return zero. As a simple fix, change the internal size limit from 0x1000000 to 0x690000 (2^16 * 3 * 5 * 7). This is still quite arbitrary, but is guaranteed to be divisible by the number of channels (which must be between 1 and 8); it also ought to be large enough to avoid any performance overhead, and small enough to be easy to test. --- src/flac.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/flac.c b/src/flac.c index 8caf4fa1..20cbb1b9 100644 --- a/src/flac.c +++ b/src/flac.c @@ -43,6 +43,15 @@ #define ENC_BUFFER_SIZE 8192 +/* +** READ_LOOP_MAX_LEN is the maximum 'len' that will be passed to +** flac_read_loop(). This is somewhat arbitrary, but must be less +** than (UINT_MAX - FLAC__MAX_CHANNELS * FLAC__MAX_BLOCK_SIZE) to +** avoid overflows, and must also be a multiple of the number of +** channels (which is between 1 and 8.) +*/ +#define READ_LOOP_MAX_LEN (0x10000 * 3 * 5 * 7) + typedef enum { PFLAC_PCM_SHORT = 50, PFLAC_PCM_INT = 51, @@ -977,7 +986,7 @@ flac_read_flac2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) while (total < len) { pflac->ptr = ptr + total ; - readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ; + readlen = (len - total > READ_LOOP_MAX_LEN) ? READ_LOOP_MAX_LEN : (unsigned) (len - total) ; current = flac_read_loop (psf, readlen) ; if (current == 0) break ; @@ -997,7 +1006,7 @@ flac_read_flac2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) while (total < len) { pflac->ptr = ptr + total ; - readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ; + readlen = (len - total > READ_LOOP_MAX_LEN) ? READ_LOOP_MAX_LEN : (unsigned) (len - total) ; current = flac_read_loop (psf, readlen) ; if (current == 0) break ; @@ -1017,7 +1026,7 @@ flac_read_flac2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) while (total < len) { pflac->ptr = ptr + total ; - readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ; + readlen = (len - total > READ_LOOP_MAX_LEN) ? READ_LOOP_MAX_LEN : (unsigned) (len - total) ; current = flac_read_loop (psf, readlen) ; if (current == 0) break ; @@ -1037,7 +1046,7 @@ flac_read_flac2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) while (total < len) { pflac->ptr = ptr + total ; - readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ; + readlen = (len - total > READ_LOOP_MAX_LEN) ? READ_LOOP_MAX_LEN : (unsigned) (len - total) ; current = flac_read_loop (psf, readlen) ; if (current == 0) From cefd7b59df628eca240af3c136d66137c8e94888 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?IOhannes=20m=20zm=C3=B6lnig?= Date: Thu, 8 Sep 2022 10:49:36 +0200 Subject: [PATCH 185/204] tests: Use fuzzy comparison in test-suite MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Using exact comparison ("a == b") when comparing expected with computed test data fails the test-suite on many architectures (including, but not limited to armhf and arm64). Instead, use epsilon(for now, FLT_EPSILON and DBL_EPSILON) to compare floating point numbers for equality. Closes: https://github.com/libsndfile/libsndfile/issues/866 Signed-off-by: IOhannes m zmölnig --- tests/utils.tpl | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/tests/utils.tpl b/tests/utils.tpl index c68e3a26..0d1cd8bb 100644 --- a/tests/utils.tpl +++ b/tests/utils.tpl @@ -193,6 +193,7 @@ sf_count_t file_length_fd (int fd) ; #include #include #include +#include #include #include @@ -215,6 +216,28 @@ sf_count_t file_length_fd (int fd) ; #define O_BINARY 0 #endif + +/* +** Compare for equality, with epsilon +*/ +static inline int +equals_short (const short a, const short b) +{ return (a == b); +} /* equals_short */ +static inline int +equals_int (const int a, const int b) +{ return (a == b); +} /* equals_int */ +static inline int +equals_float (const float a, const float b) +{ return (fabsf(a - b) <= FLT_EPSILON); +} /* equals_float */ +static inline int +equals_double (const double a, const double b) +{ return (fabs(a - b) <= DBL_EPSILON); +} /* equals_double */ + + [+ FOR float_type +] void gen_windowed_sine_[+ (get "name") +] ([+ (get "name") +] *data, int len, double maximum) @@ -752,8 +775,8 @@ compare_[+ (get "io_element") +]_or_die (const [+ (get "io_element") +] *expecte unsigned k ; for (k = 0 ; k < count ; k++) - if (expected [k] != actual [k]) - { printf ("\n\nLine %d : Error at index %d, got " [+ (get "format_str") +] ", should be " [+ (get "format_str") +] ".\n\n", line_num, k, actual [k], expected [k]) ; + if (!equals_[+ (get "io_element") +](expected [k], actual [k])) + { printf ("\n\nLine %d : Error at index %d, got " [+ (get "format_str") +] ", should be " [+ (get "format_str") +] "(delta=" [+ (get "format_str") +] " ).\n\n", line_num, k, actual [k], expected [k], actual [k] - expected [k]) ; exit (1) ; } ; From bd951f9a47adec1040e6fabcce1ee592e3be219b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Heusipp?= Date: Sun, 11 Sep 2022 14:01:11 +0200 Subject: [PATCH 186/204] Make sndfile.h independent of build system variables Fixes . --- CMakeLists.txt | 4 +-- Makefile.am | 5 ++- cmake/SndFileChecks.cmake | 29 --------------- configure.ac | 55 +---------------------------- docs/new_file_type_howto.md | 2 +- include/{sndfile.h.in => sndfile.h} | 4 +-- src/common.c | 6 ---- src/config.h.cmake | 9 ----- src/file_io.c | 2 +- tests/pedantic-header-test.sh.in | 2 +- 10 files changed, 9 insertions(+), 109 deletions(-) rename include/{sndfile.h.in => sndfile.h} (99%) diff --git a/CMakeLists.txt b/CMakeLists.txt index ae8ebec7..904a1a20 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -220,8 +220,6 @@ feature_summary (WHAT ALL) configure_file (src/config.h.cmake src/config.h) -configure_file (include/sndfile.h.in include/sndfile.h) - if (INSTALL_PKGCONFIG_MODULE) set (prefix ${CMAKE_INSTALL_PREFIX}) @@ -250,8 +248,8 @@ endif () # Public libsndfile headers set (sndfile_HDRS + include/sndfile.h include/sndfile.hh - ${CMAKE_CURRENT_BINARY_DIR}/include/sndfile.h ) # diff --git a/Makefile.am b/Makefile.am index 5efbf9a8..7bd00d3a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -46,7 +46,7 @@ BUILT_SOURCES = src/test_endswap.c SYMBOL_FILES = src/Symbols.gnu-binutils src/Symbols.darwin src/libsndfile-1.def src/Symbols.os2 src/Symbols.static -EXTRA_DIST += include/sndfile.h.in src/config.h.in src/test_endswap.tpl src/test_endswap.def \ +EXTRA_DIST += src/config.h.in src/test_endswap.tpl src/test_endswap.def \ $(SYMBOL_FILES) src/create_symbols_file.py src/binheader_writef_check.py \ src/GSM610/README src/GSM610/COPYRIGHT src/GSM610/ChangeLog \ src/G72x/README src/G72x/README.original src/G72x/ChangeLog \ @@ -62,8 +62,7 @@ endif #=============================================================================== lib_LTLIBRARIES = src/libsndfile.la -include_HEADERS = include/sndfile.hh -nodist_include_HEADERS = include/sndfile.h +include_HEADERS = include/sndfile.h include/sndfile.hh src_libsndfile_la_CFLAGS = $(EXTERNAL_XIPH_CFLAGS) $(MPEG_CFLAGS) # MinGW requires -no-undefined if a DLL is to be built. src_libsndfile_la_LDFLAGS = -no-undefined -version-info $(SHARED_VERSION_INFO) $(SHLIB_VERSION_ARG) diff --git a/cmake/SndFileChecks.cmake b/cmake/SndFileChecks.cmake index 2e09ba6f..19c7b4b9 100644 --- a/cmake/SndFileChecks.cmake +++ b/cmake/SndFileChecks.cmake @@ -16,13 +16,6 @@ if (LARGE_FILES_DEFINITIONS) add_definitions(${LARGE_FILES_DEFINITIONS}) endif () -if (WIN32) - set(TYPEOF_SF_COUNT_T __int64) -else () - set(TYPEOF_SF_COUNT_T int64_t) -endif () -set (SF_COUNT_MAX 0x7fffffffffffffffll) - if (CMAKE_SYSTEM_NAME STREQUAL "OpenBSD") find_package (Sndio) elseif (NOT WIN32) @@ -121,28 +114,6 @@ if (ENABLE_TESTING) check_type_size (void* SIZEOF_VOIDP) endif() -if ((SIZEOF_OFF_T EQUAL 8) OR (SIZEOF_LOFF_T EQUAL 8) OR (SIZEOF_OFF64_T EQUAL 8)) - set (TYPEOF_SF_COUNT_T "int64_t") - set (SF_COUNT_MAX "0x7FFFFFFFFFFFFFFFLL") - set (SIZEOF_SF_COUNT_T 8) -else () - if (WIN32) - set (TYPEOF_SF_COUNT_T "__int64") - set (SF_COUNT_MAX "0x7FFFFFFFFFFFFFFFLL") - set (SIZEOF_SF_COUNT_T 8) - else () - message ("") - message ("*** The configure process has determined that this system is capable") - message ("*** of Large File Support but has not been able to find a type which") - message ("*** is an unambiguous 64 bit file offset.") - message ("*** Please contact the author to help resolve this problem.") - message ("") - message (FATAL_ERROR "Bad file offset type.") - endif () -endif () - -check_type_size (${TYPEOF_SF_COUNT_T} SIZEOF_SF_COUNT_T) - if (NOT WIN32) check_library_exists (m floor "" LIBM_REQUIRED) if (LIBM_REQUIRED) diff --git a/configure.ac b/configure.ac index a4c776d7..95756a11 100644 --- a/configure.ac +++ b/configure.ac @@ -194,12 +194,6 @@ AC_CHECK_SIZEOF([size_t], [4]) AC_CHECK_SIZEOF([int64_t], [8]) AC_CHECK_SIZEOF([long long], [8]) -dnl ==================================================================================== -dnl Find an appropriate type for sf_count_t. -dnl On systems supporting files larger than 2 Gig, sf_count_t must be a 64 bit value. -dnl Unfortunately there is more than one way of ensuring this so need to do some -dnl pretty rigourous testing here. - dnl Check for common 64 bit file offset types. AC_CHECK_SIZEOF([off_t], [1]) @@ -209,25 +203,12 @@ AS_IF([test "x$enable_largefile:$ac_cv_sizeof_off_t" = "xno:8"], [ AS_CASE([$host_os], [mingw32*], [ - TYPEOF_SF_COUNT_T="__int64" - SF_COUNT_MAX="0x7FFFFFFFFFFFFFFFLL" - SIZEOF_SF_COUNT_T=8 AC_DEFINE([__USE_MINGW_ANSI_STDIO], [1], [Set to 1 to use C99 printf/snprintf in MinGW.]) ], - [linux-android*], [ - TYPEOF_SF_COUNT_T="int64_t" - SF_COUNT_MAX="0x7FFFFFFFFFFFFFFFLL" - SIZEOF_SF_COUNT_T=8 - ], - [ - SIZEOF_SF_COUNT_T=0 AS_IF([test "x$ac_cv_sizeof_off_t" = "x8"], [ dnl If sizeof (off_t) is 8, no further checking is needed. - TYPEOF_SF_COUNT_T="int64_t" - SF_COUNT_MAX="0x7FFFFFFFFFFFFFFFLL" - SIZEOF_SF_COUNT_T=8 ], [ dnl Save the old sizeof (off_t) value and then unset it to see if it dnl changes when Large File Support is enabled. @@ -246,43 +227,9 @@ AS_CASE([$host_os], ]) AC_CHECK_SIZEOF(off_t,1) - - AS_IF([test "x$ac_cv_sizeof_off_t" = "x8"], [ - TYPEOF_SF_COUNT_T="int64_t" - SF_COUNT_MAX="0x7FFFFFFFFFFFFFFFLL" - SIZEOF_SF_COUNT_T=8 - ], [test "x$TYPEOF_SF_COUNT_T" = "xunknown"], [ - AS_ECHO([""]) - AS_ECHO(["*** The configure process has determined that this system is capable"]) - AS_ECHO(["*** of Large File Support but has not been able to find a type which"]) - AS_ECHO(["*** is an unambiguous 64 bit file offset."]) - AS_ECHO(["*** Please contact the author to help resolve this problem."]) - AS_ECHO([""]) - AC_MSG_ERROR([[Bad file offset type.]]) - ]) ]) ]) -AS_CASE([$host_vendor], - [vita], [ - TYPEOF_SF_COUNT_T="int64_t" - SF_COUNT_MAX="0x7FFFFFFFFFFFFFFFLL" - SIZEOF_SF_COUNT_T=8 - ]) - -AS_IF([test "x$SIZEOF_SF_COUNT_T" = "x4"], [ - SF_COUNT_MAX="0x7FFFFFFF" - ]) - -AC_DEFINE_UNQUOTED([TYPEOF_SF_COUNT_T], [${TYPEOF_SF_COUNT_T}], [Set to long if unknown.]) -AC_SUBST(TYPEOF_SF_COUNT_T) - -AC_DEFINE_UNQUOTED([SIZEOF_SF_COUNT_T], [${SIZEOF_SF_COUNT_T}], [Set to sizeof (long) if unknown.]) -AC_SUBST(SIZEOF_SF_COUNT_T) - -AC_DEFINE_UNQUOTED([SF_COUNT_MAX], [${SF_COUNT_MAX}], [Set to maximum allowed value of sf_count_t type.]) -AC_SUBST(SF_COUNT_MAX) - AC_TYPE_SSIZE_T dnl ==================================================================================== @@ -750,7 +697,7 @@ AC_SUBST(TEST_BINDIR) AC_CONFIG_FILES([ Makefile Octave/Makefile - src/version-metadata.rc include/sndfile.h + src/version-metadata.rc tests/test_wrapper.sh tests/pedantic-header-test.sh libsndfile.spec sndfile.pc Scripts/build-test-tarball.mk diff --git a/docs/new_file_type_howto.md b/docs/new_file_type_howto.md index 05853946..bb422081 100644 --- a/docs/new_file_type_howto.md +++ b/docs/new_file_type_howto.md @@ -55,7 +55,7 @@ to disable gcc optimisations and make debugging with gdb/ddd easier. Add a unique identifier for the new file type. -Edit src/sndfile.h.in and find the enum containing the SF_FORMAT_XXX identifiers. +Edit src/sndfile.h and find the enum containing the SF_FORMAT_XXX identifiers. Since you will be adding a major file type you should add your identifier to the top part of the list where the values are above 0x10000 in value. The easiest way to do this is to find the largest value in the list, add 0x10000 to it and diff --git a/include/sndfile.h.in b/include/sndfile.h similarity index 99% rename from include/sndfile.h.in rename to include/sndfile.h index 8842fd48..61eba6f4 100644 --- a/include/sndfile.h.in +++ b/include/sndfile.h @@ -365,9 +365,9 @@ typedef struct sf_private_tag SNDFILE ; ** and the Microsoft compiler. */ -typedef @TYPEOF_SF_COUNT_T@ sf_count_t ; +typedef int64_t sf_count_t ; #ifndef SF_COUNT_MAX -#define SF_COUNT_MAX @SF_COUNT_MAX@ +#define SF_COUNT_MAX 0x7FFFFFFFFFFFFFFFLL #endif diff --git a/src/common.c b/src/common.c index ddf621b0..40c26b97 100644 --- a/src/common.c +++ b/src/common.c @@ -540,8 +540,6 @@ header_put_le_int (SF_PRIVATE *psf, int x) psf->header.ptr [psf->header.indx++] = (x >> 24) ; } /* header_put_le_int */ -#if (SIZEOF_SF_COUNT_T == 8) - static inline void header_put_be_8byte (SF_PRIVATE *psf, sf_count_t x) { psf->header.ptr [psf->header.indx++] = (x >> 56) ; @@ -566,10 +564,6 @@ header_put_le_8byte (SF_PRIVATE *psf, sf_count_t x) psf->header.ptr [psf->header.indx++] = (x >> 56) ; } /* header_put_le_8byte */ -#else -#error "SIZEOF_SF_COUNT_T != 8" -#endif - int psf_binheader_writef (SF_PRIVATE *psf, const char *format, ...) { va_list argptr ; diff --git a/src/config.h.cmake b/src/config.h.cmake index 2872e7d1..71fdb4f2 100644 --- a/src/config.h.cmake +++ b/src/config.h.cmake @@ -223,9 +223,6 @@ /* Define to the version of this package. */ #define PACKAGE_VERSION "@CPACK_PACKAGE_VERSION_FULL@" -/* Set to maximum allowed value of sf_count_t type. */ -#define SF_COUNT_MAX @SF_COUNT_MAX@ - /* The size of `double', as computed by sizeof. */ @SIZEOF_DOUBLE_CODE@ @@ -253,9 +250,6 @@ /* The size of `off_t', as computed by sizeof. */ @SIZEOF_OFF_T_CODE@ -/* Set to sizeof (long) if unknown. */ -@SIZEOF_SF_COUNT_T_CODE@ - /* The size of `short', as computed by sizeof. */ @SIZEOF_SHORT_CODE@ @@ -271,9 +265,6 @@ /* The size of `wchar_t', as computed by sizeof. */ @SIZEOF_WCHAR_T_CODE@ -/* Set to long if unknown. */ -#define TYPEOF_SF_COUNT_T @TYPEOF_SF_COUNT_T@ - /* Enable extensions on AIX 3, Interix. */ #ifndef _ALL_SOURCE # undef _ALL_SOURCE diff --git a/src/file_io.c b/src/file_io.c index 90c96085..b7f7ed77 100644 --- a/src/file_io.c +++ b/src/file_io.c @@ -505,7 +505,7 @@ psf_is_pipe (SF_PRIVATE *psf) static sf_count_t psf_get_filelen_fd (int fd) { -#if (SIZEOF_OFF_T == 4 && SIZEOF_SF_COUNT_T == 8 && HAVE_FSTAT64) +#if (SIZEOF_OFF_T == 4 && HAVE_FSTAT64) struct stat64 statbuf ; if (fstat64 (fd, &statbuf) == -1) diff --git a/tests/pedantic-header-test.sh.in b/tests/pedantic-header-test.sh.in index 283af645..561628dc 100644 --- a/tests/pedantic-header-test.sh.in +++ b/tests/pedantic-header-test.sh.in @@ -42,7 +42,7 @@ if test -n "@GCC_MAJOR_VERSION@" ; then CC=`echo "@CC@" | sed "s/.*shave cc //"` # Compile with -Werror and -pedantic. - $CC -std=c99 -Werror -pedantic -I@top_srcdir@/src -I@abs_top_builddir@/src -I@abs_top_builddir@/include -c @top_srcdir@/tests/sfversion.c -o /dev/null + $CC -std=c99 -Werror -pedantic -I@top_srcdir@/src -I@abs_top_builddir@/src -I@top_srcdir@/include -c @top_srcdir@/tests/sfversion.c -o /dev/null # Check compiler return status. if test $? -ne 0 ; then From 3bd5048f8c2f7285743e9922c195c7a08f3f5551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Heusipp?= Date: Mon, 12 Sep 2022 18:16:58 +0200 Subject: [PATCH 187/204] Define SF_COUNT_MAX as INT64_MAX. INT64_MAX instead of 0x7FFFFFFFFFFFFFFFLL avoids type discrepancy between int64_t and LL (long long) suffix when int64_t is not long long. --- include/sndfile.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/sndfile.h b/include/sndfile.h index 61eba6f4..ed992083 100644 --- a/include/sndfile.h +++ b/include/sndfile.h @@ -367,7 +367,7 @@ typedef struct sf_private_tag SNDFILE ; typedef int64_t sf_count_t ; #ifndef SF_COUNT_MAX -#define SF_COUNT_MAX 0x7FFFFFFFFFFFFFFFLL +#define SF_COUNT_MAX INT64_MAX #endif From bc116009863f53c8f0bcfb30451ea398db63d5e9 Mon Sep 17 00:00:00 2001 From: David Seifert Date: Tue, 13 Sep 2022 10:30:28 +0200 Subject: [PATCH 188/204] configure.ac: fix broken `--enable-ossfuzzers` option * Passing `--disable-ossfuzzers` would actually enable fuzzers. --- configure.ac | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/configure.ac b/configure.ac index 95756a11..ec4463db 100644 --- a/configure.ac +++ b/configure.ac @@ -171,10 +171,8 @@ AC_ARG_ENABLE([test-coverage], AM_CONDITIONAL([ENABLE_TEST_COVERAGE], [test "x$enable_test_coverage" = "xyes"]) AC_ARG_ENABLE([ossfuzzers], - [AS_HELP_STRING([--enable-ossfuzzers], - [Whether to generate the fuzzers for OSS-Fuzz])], - [have_ossfuzzers=yes], [have_ossfuzzers=no]) -AM_CONDITIONAL([USE_OSSFUZZERS], [test "x$have_ossfuzzers" = "xyes"]) + [AS_HELP_STRING([--enable-ossfuzzers], [Whether to generate the fuzzers for OSS-Fuzz])]) +AM_CONDITIONAL([USE_OSSFUZZERS], [test "x$enable_ossfuzzers" = "xyes"]) AC_SUBST([LIB_FUZZING_ENGINE]) AM_CONDITIONAL([USE_OSSFUZZ_FLAG], [test "x$LIB_FUZZING_ENGINE" = "x-fsanitize=fuzzer"]) From 005a36b41bd90fefaaa876df9b607f11c11b5e73 Mon Sep 17 00:00:00 2001 From: David Seifert Date: Sat, 24 Sep 2022 12:51:49 +0200 Subject: [PATCH 189/204] Remove `autogen.sh` * Users should just use `autoreconf -vif` instead of brittle autogen.sh scripts. Fixes: #878 --- .github/workflows/action.yml | 2 +- Building-for-Android.md | 2 +- README.md | 2 +- Scripts/static-deps-build.mk | 4 +- autogen.sh | 176 ----------------------------------- ossfuzz/ossfuzz.sh | 2 +- 6 files changed, 6 insertions(+), 182 deletions(-) delete mode 100755 autogen.sh diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index 65a1a04c..4c24215a 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -209,7 +209,7 @@ jobs: CXX: ${{ matrix.cxx }} if: startsWith(matrix.build-system,'autotools') run: | - ./autogen.sh + autoreconf -vif if [[ "${CC}" == "clang" ]]; then ./configure --enable-werror && make distcheck else diff --git a/Building-for-Android.md b/Building-for-Android.md index c664281b..eb5a0d35 100644 --- a/Building-for-Android.md +++ b/Building-for-Android.md @@ -3,7 +3,7 @@ Assuming the Android Ndk is installed at location `/path/to/toolchain`, building libsndfile for Android (arm-linux-androideabi) should be as simple as: ``` -./autogen.sh +autoreconf -vif export ANDROID_TOOLCHAIN_HOME=/path/to/android/toolchain ./Scripts/android-configure.sh make diff --git a/README.md b/README.md index 82b0b390..f14afcd6 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ Similarly on Mac OS X, assuming [brew] is already installed: Once the build environment has been set up, building and testing libsndfile is as simple as: - ./autogen.sh + autoreconf -vif ./configure --enable-werror make make check diff --git a/Scripts/static-deps-build.mk b/Scripts/static-deps-build.mk index a2c19694..bbf2f0d7 100755 --- a/Scripts/static-deps-build.mk +++ b/Scripts/static-deps-build.mk @@ -95,7 +95,7 @@ Build/Stamp/build-ogg : Build/Stamp/sha256sum ifeq ($(disable_ogg_crc), true) echo "Ogg/CRC enabled" (cd Build && git clone https://github.com/xiph/ogg $(ogg_version)) - (cd Build/$(ogg_version) && ./autogen.sh && CFLAGS=-fPIC ./configure $(config_options) --disable-crc && make all install) + (cd Build/$(ogg_version) && autoreconf -vif && CFLAGS=-fPIC ./configure $(config_options) --disable-crc && make all install) else echo "Ogg/CRC disabled" (cd Build && tar xf Tarballs/$(ogg_tarball)) @@ -110,7 +110,7 @@ Build/Stamp/install-libs : Build/Stamp/extract Build/Stamp/build-ogg touch $@ configure : configure.ac - ./autogen.sh + autoreconf -vif Build/Stamp/configure : Build/Stamp/install-libs configure PKG_CONFIG_LIBDIR=Build/lib/pkgconfig ./configure diff --git a/autogen.sh b/autogen.sh deleted file mode 100755 index dbdb5ded..00000000 --- a/autogen.sh +++ /dev/null @@ -1,176 +0,0 @@ -#!/bin/sh -# Run this to set up the build system: configure, makefiles, etc. -# (based on the version in enlightenment's cvs) - -package="libsndfile" - -olddir=`pwd` -srcdir=`dirname $0` -test -z "$srcdir" && srcdir=. - -cd "$srcdir" -DIE=0 - -printf "checking for autogen ... " -result="yes" -(autogen --version) < /dev/null > /dev/null 2>&1 || { - echo - echo "You must have GNU autogen installed to compile $package." - echo "Download the appropriate package for your distribution," - echo "or get the source tarball at ftp://ftp.gnu.org/pub/gnu/" - result="no" - DIE=1 -} -echo $result - -printf "checking for autoconf ... " -result="yes" -(autoconf --version) < /dev/null > /dev/null 2>&1 || { - echo - echo "You must have autoconf installed to compile $package." - echo "Download the appropriate package for your distribution," - echo "or get the source tarball at ftp://ftp.gnu.org/pub/gnu/" - result="no" - DIE=1 -} -echo $result - -VERSIONGREP="sed -e s/.*[^0-9\.]\([0-9][0-9]*\.[0-9][0-9]*\).*/\1/" -VERSIONMKMAJ="sed -e s/\([0-9][0-9]*\)[^0-9].*/\\1/" -VERSIONMKMIN="sed -e s/.*[0-9][0-9]*\.//" - -# do we need automake? -if test -r Makefile.am; then - AM_OPTIONS=`fgrep AUTOMAKE_OPTIONS Makefile.am` - AM_NEEDED=`echo $AM_OPTIONS | $VERSIONGREP` - if test x"$AM_NEEDED" = "x$AM_OPTIONS"; then - AM_NEEDED="" - fi - if test -z $AM_NEEDED; then - printf "checking for automake ... " - AUTOMAKE=automake - ACLOCAL=aclocal - if ($AUTOMAKE --version < /dev/null > /dev/null 2>&1); then - echo "yes" - else - echo "no" - AUTOMAKE= - fi - else - printf "checking for automake $AM_NEEDED or later ... " - majneeded=`echo $AM_NEEDED | $VERSIONMKMAJ` - minneeded=`echo $AM_NEEDED | $VERSIONMKMIN` - for am in automake-$AM_NEEDED automake$AM_NEEDED \ - automake automake-1.7 automake-1.8 automake-1.9 automake-1.10; do - ($am --version < /dev/null > /dev/null 2>&1) || continue - ver=`$am --version < /dev/null | head -n 1 | $VERSIONGREP` - maj=`echo $ver | $VERSIONMKMAJ` - min=`echo $ver | $VERSIONMKMIN` - if test $maj -eq $majneeded -a $min -ge $minneeded; then - AUTOMAKE=$am - echo $AUTOMAKE - break - fi - done - test -z $AUTOMAKE && echo "no" - printf "checking for aclocal $AM_NEEDED or later ... " - for ac in aclocal-$AM_NEEDED aclocal$AM_NEEDED \ - aclocal aclocal-1.7 aclocal-1.8 aclocal-1.9 aclocal-1.10; do - ($ac --version < /dev/null > /dev/null 2>&1) || continue - ver=`$ac --version < /dev/null | head -n 1 | $VERSIONGREP` - maj=`echo $ver | $VERSIONMKMAJ` - min=`echo $ver | $VERSIONMKMIN` - if test $maj -eq $majneeded -a $min -ge $minneeded; then - ACLOCAL=$ac - echo $ACLOCAL - break - fi - done - test -z $ACLOCAL && echo "no" - fi - test -z $AUTOMAKE || test -z $ACLOCAL && { - echo - echo "You must have automake installed to compile $package." - echo "Download the appropriate package for your distribution," - echo "or get the source tarball at ftp://ftp.gnu.org/pub/gnu/" - exit 1 - } -fi - -printf "checking for libtool ... " -for LIBTOOLIZE in libtoolize glibtoolize nope; do - ($LIBTOOLIZE --version) < /dev/null > /dev/null 2>&1 && break -done -if test x$LIBTOOLIZE = xnope; then - echo "nope." - LIBTOOLIZE=libtoolize -else - echo $LIBTOOLIZE -fi -($LIBTOOLIZE --version) < /dev/null > /dev/null 2>&1 || { - echo - echo "You must have libtool installed to compile $package." - echo "Download the appropriate package for your system," - echo "or get the source from one of the GNU ftp sites" - echo "listed in http://www.gnu.org/order/ftp.html" - DIE=1 -} - -printf "checking for pkg-config ... " -result="yes" -(pkg-config --version) < /dev/null > /dev/null 2>&1 || { - echo - echo "You must have pkg-config installed to compile $package." - echo "Download the appropriate package for your distribution." - result="no" - DIE=1 -} -echo $result - - -printf "checking for python ... " -result="yes" -(python --version) < /dev/null > /dev/null 2>&1 || { - echo - echo "You must have Python installed to compile $package." - echo "Download the appropriate package for your distribution," - echo "or get the source tarball at http://python.org/" - result="no" - DIE=1 -} -echo $result - -if test "$DIE" -eq 1; then - exit 1 -fi - -echo "Generating configuration files for $package, please wait ... " - -echo " $ACLOCAL $ACLOCAL_FLAGS" -$ACLOCAL $ACLOCAL_FLAGS || exit 1 -echo " $LIBTOOLIZE --automake --force" -$LIBTOOLIZE --automake --force || exit 1 -echo " autoheader" -autoheader || exit 1 -echo " $AUTOMAKE --add-missing $AUTOMAKE_FLAGS" -$AUTOMAKE --add-missing $AUTOMAKE_FLAGS || exit 1 -echo " autoconf" -autoconf || exit 1 - -# Generate the src/cmake-config.h.in from src/config.h.in. -# CMake process src/cmake-config.h to create src/config.h. -rm -f src/config.h src/cmake-config.h - -cd $olddir - -if test -d .git/ ; then - fprecommit=.git/hooks/pre-commit - if test ! -f $fprecommit ; then - echo - echo "Installing git pre-commit hook for this project." - printf "#/bin/sh\nexec Scripts/git-pre-commit-hook\n" > $fprecommit - chmod u+x $fprecommit - echo - fi - fi - diff --git a/ossfuzz/ossfuzz.sh b/ossfuzz/ossfuzz.sh index a4d98d93..7b3666ff 100755 --- a/ossfuzz/ossfuzz.sh +++ b/ossfuzz/ossfuzz.sh @@ -23,7 +23,7 @@ apt-get -y install autoconf autogen automake libtool pkg-config python # libasound2-dev libflac-dev libogg-dev libopus-dev libvorbis-dev # Compile the fuzzer. -./autogen.sh +autoreconf -vif ./configure --disable-shared --enable-ossfuzzers make V=1 From 0faf893fd21866bf08a5e64adf2ccf8d7ab0a9de Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Mon, 3 Oct 2022 00:28:18 -0700 Subject: [PATCH 190/204] mpeg_decode: Fix bug where bytes from mpg123_read where ignored. Most every read api I've ever worked with return no error and positive byte counts, or zero bytes and an end or error condition. When the end of stream is reached, the last read will contain the last bytes, and the next call to read returns an error indicating that end has been reached. But not mpg123, no, when reaching the last bytes, it returns the last bytes AND sets the error condition to done. Assumptions make and ass of you an mumptions. --- src/mpeg_decode.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/mpeg_decode.c b/src/mpeg_decode.c index 20fac4f9..582c5e1a 100644 --- a/src/mpeg_decode.c +++ b/src/mpeg_decode.c @@ -91,12 +91,9 @@ mpeg_dec_decode (SF_PRIVATE *psf, float *ptr, sf_count_t len) error = mpg123_read (pmp3d->pmh, (unsigned char *) ptr, len * sizeof (float), &done) ; - if (error == MPG123_OK) + if (error == MPG123_OK || error == MPG123_DONE) return done / sizeof (float) ; - if (error == MPG123_DONE) - return 0 ; - if (error == MPG123_NEW_FORMAT) { psf->error = SFE_MALFORMED_FILE ; return -1 ; From cc5be43f094a08bd8912cc76ac3b1408685bdd57 Mon Sep 17 00:00:00 2001 From: Mads Kiilerich Date: Wed, 26 Oct 2022 14:57:02 +0200 Subject: [PATCH 191/204] tests/error_test.c: introduce wav_list_recover_test to cover #374 The test is currently been negated so it expects to hit the problem. That will change when the problem is fixed. --- tests/error_test.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/error_test.c b/tests/error_test.c index 495d88b5..e89382d6 100644 --- a/tests/error_test.c +++ b/tests/error_test.c @@ -174,6 +174,47 @@ bad_wav_test (const char * filename) puts ("ok") ; } /* bad_wav_test */ +static void +wav_list_recover_test (const char * filename) +{ SNDFILE *sndfile ; + SF_INFO sfinfo ; + + FILE *file ; + const char data [] = + "RIFF" "J\000\000\000" + "WAVE" "fmt " "\020\000\000\000" "\001\000\001\000D\254\000\000\210X\001\000\002\000\020\000" + "LIST" "\014\000\000\000" "test" "\010\000\000\000" "1234" /* test is 4 bytes short inside LIST container */ + "data" "\004\000\000\000" "abcd" ; + + print_test_name (__func__, filename) ; + + if ((file = fopen (filename, "w")) == NULL) + { printf ("\n\nLine %d : fopen returned NULL.\n", __LINE__) ; + exit (1) ; + } ; + + exit_if_true (fwrite (data, sizeof (data) - 1, 1, file) != 1, "\n\nLine %d : fwrite failed.\n", __LINE__) ; + fclose (file) ; + + memset (&sfinfo, 0, sizeof (sfinfo)) ; + sndfile = sf_open (filename, SFM_READ, &sfinfo) ; + + if (sndfile) + { printf ("\n\nLine %d : expected failure - issue 374 reports no recovery from bogus LIST content.\n", __LINE__) ; + exit (1) ; + } ; + + /* + if (sfinfo.frames != 2) + { printf ("\n\nLine %d : Should have read data chunk with 2 stereo frames, got %ld.\n\n", __LINE__, (long)sfinfo.frames) ; + exit (1) ; + } ; + */ + + unlink (filename) ; + puts ("ok") ; +} /* wav_list_recover_test */ + static void error_close_test (void) { static short buffer [SHORT_BUFFER] ; @@ -266,6 +307,7 @@ main (void) no_file_test ("no_file.wav") ; zero_length_test ("zero_length.wav") ; bad_wav_test ("bad_wav.wav") ; + wav_list_recover_test ("list_recover.wav") ; unrecognised_test () ; From bc2e8bebe40dacbc47065a68031aeefc2b9cdaea Mon Sep 17 00:00:00 2001 From: Mads Kiilerich Date: Wed, 26 Oct 2022 13:07:11 +0200 Subject: [PATCH 192/204] src/wavlike.c: Fix wavlike_subchunk_parse skipping of erroneous subchunks, closes #374 Drop invalid +4 as in 65eabcbf7. --- src/wavlike.c | 2 +- tests/error_test.c | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/wavlike.c b/src/wavlike.c index a3388d75..0db71323 100644 --- a/src/wavlike.c +++ b/src/wavlike.c @@ -1089,7 +1089,7 @@ wavlike_subchunk_parse (SF_PRIVATE *psf, int chunk, uint32_t chunk_length) chunk_size += (chunk_size & 1) ; psf_log_printf (psf, " *** %M : %u\n", chunk, chunk_size) ; if (bytesread + chunk_size > chunk_length) - { bytesread += psf_binheader_readf (psf, "j", chunk_length - bytesread + 4) ; + { bytesread += psf_binheader_readf (psf, "j", chunk_length - bytesread) ; continue ; } else diff --git a/tests/error_test.c b/tests/error_test.c index e89382d6..9cdabf4e 100644 --- a/tests/error_test.c +++ b/tests/error_test.c @@ -199,17 +199,15 @@ wav_list_recover_test (const char * filename) memset (&sfinfo, 0, sizeof (sfinfo)) ; sndfile = sf_open (filename, SFM_READ, &sfinfo) ; - if (sndfile) - { printf ("\n\nLine %d : expected failure - issue 374 reports no recovery from bogus LIST content.\n", __LINE__) ; + if (!sndfile) + { printf ("\n\nLine %d : expected recovery from bogus LIST content.\n", __LINE__) ; exit (1) ; } ; - /* if (sfinfo.frames != 2) { printf ("\n\nLine %d : Should have read data chunk with 2 stereo frames, got %ld.\n\n", __LINE__, (long)sfinfo.frames) ; exit (1) ; } ; - */ unlink (filename) ; puts ("ok") ; From 140b241837adac30b54347cdbfd5a91ee8456971 Mon Sep 17 00:00:00 2001 From: Mads Kiilerich Date: Wed, 26 Oct 2022 13:26:46 +0200 Subject: [PATCH 193/204] src/wavlike.c: Fix wavlike_subchunk_parse default logging Follow the general convention and only use '***' for errors. But when chunk_size is too big, log it as "too big" as for DISP_MARKER etc. --- src/wavlike.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/wavlike.c b/src/wavlike.c index 0db71323..eddad703 100644 --- a/src/wavlike.c +++ b/src/wavlike.c @@ -1087,13 +1087,15 @@ wavlike_subchunk_parse (SF_PRIVATE *psf, int chunk, uint32_t chunk_length) default : bytesread += psf_binheader_readf (psf, "4", &chunk_size) ; chunk_size += (chunk_size & 1) ; - psf_log_printf (psf, " *** %M : %u\n", chunk, chunk_size) ; if (bytesread + chunk_size > chunk_length) - { bytesread += psf_binheader_readf (psf, "j", chunk_length - bytesread) ; + { psf_log_printf (psf, " *** %M : %u (too big)\n", chunk, chunk_size) ; + bytesread += psf_binheader_readf (psf, "j", chunk_length - bytesread) ; continue ; } else + { psf_log_printf (psf, " %M : %u\n", chunk, chunk_size) ; bytesread += psf_binheader_readf (psf, "j", chunk_size) ; + } ; if (chunk_size >= chunk_length) return 0 ; From c9ee5f2ff2868b172ab4ec2b2a21b97d5b276f43 Mon Sep 17 00:00:00 2001 From: Mads Kiilerich Date: Wed, 26 Oct 2022 13:10:30 +0200 Subject: [PATCH 194/204] src/wavlike.c: Drop wavlike_subchunk_parse unreachable error handling code We already read the chunk type so bytesread is always > 0, and (chunk_size >= chunk_length) will thus always have been covered by (bytesread + chunk_size > chunk_length) just above. --- src/wavlike.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/wavlike.c b/src/wavlike.c index eddad703..aa143f83 100644 --- a/src/wavlike.c +++ b/src/wavlike.c @@ -1096,9 +1096,6 @@ wavlike_subchunk_parse (SF_PRIVATE *psf, int chunk, uint32_t chunk_length) { psf_log_printf (psf, " %M : %u\n", chunk, chunk_size) ; bytesread += psf_binheader_readf (psf, "j", chunk_size) ; } ; - - if (chunk_size >= chunk_length) - return 0 ; break ; } ; From b4de42495981cdc26f516ec22deab80aef89802b Mon Sep 17 00:00:00 2001 From: Mads Kiilerich Date: Wed, 26 Oct 2022 13:21:21 +0200 Subject: [PATCH 195/204] src/wavlike.c: let wavlike_subchunk_parse consistently use cleanup_subchunk_parse The special logging for zero markers doesn't seem more relevant than for all the other error handling cases. --- src/wavlike.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/wavlike.c b/src/wavlike.c index aa143f83..9cd59cf1 100644 --- a/src/wavlike.c +++ b/src/wavlike.c @@ -1003,10 +1003,7 @@ wavlike_subchunk_parse (SF_PRIVATE *psf, int chunk, uint32_t chunk_length) ** the rest of the chunk is garbage. */ psf_log_printf (psf, " *** Found weird-ass zero marker. Jumping to end of chunk.\n") ; - if (bytesread < chunk_length) - bytesread += psf_binheader_readf (psf, "j", chunk_length - bytesread) ; - psf_log_printf (psf, " *** Offset is now : 0x%X\n", psf_fseek (psf, 0, SEEK_CUR)) ; - return 0 ; + goto cleanup_subchunk_parse ; default : break ; @@ -1089,8 +1086,7 @@ wavlike_subchunk_parse (SF_PRIVATE *psf, int chunk, uint32_t chunk_length) chunk_size += (chunk_size & 1) ; if (bytesread + chunk_size > chunk_length) { psf_log_printf (psf, " *** %M : %u (too big)\n", chunk, chunk_size) ; - bytesread += psf_binheader_readf (psf, "j", chunk_length - bytesread) ; - continue ; + goto cleanup_subchunk_parse ; } else { psf_log_printf (psf, " %M : %u\n", chunk, chunk_size) ; From 1534936c10c00cb812b5fec224d1e33006a40710 Mon Sep 17 00:00:00 2001 From: Mads Kiilerich Date: Wed, 26 Oct 2022 13:54:42 +0200 Subject: [PATCH 196/204] src/wavlike.c: let wavlike_subchunk_parse consistently consider bytesread when checking chunk_size --- src/wavlike.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wavlike.c b/src/wavlike.c index 9cd59cf1..97b62429 100644 --- a/src/wavlike.c +++ b/src/wavlike.c @@ -1026,7 +1026,7 @@ wavlike_subchunk_parse (SF_PRIVATE *psf, int chunk, uint32_t chunk_length) case ITRK_MARKER : bytesread += psf_binheader_readf (psf, "4", &chunk_size) ; chunk_size += (chunk_size & 1) ; - if (chunk_size >= SIGNED_SIZEOF (buffer) || chunk_size >= chunk_length) + if (chunk_size >= SIGNED_SIZEOF (buffer) || bytesread + chunk_size > chunk_length) { psf_log_printf (psf, " *** %M : %u (too big)\n", chunk, chunk_size) ; goto cleanup_subchunk_parse ; } ; @@ -1042,7 +1042,7 @@ wavlike_subchunk_parse (SF_PRIVATE *psf, int chunk, uint32_t chunk_length) bytesread += psf_binheader_readf (psf, "44", &chunk_size, &mark_id) ; chunk_size -= 4 ; chunk_size += (chunk_size & 1) ; - if (chunk_size < 1 || chunk_size >= SIGNED_SIZEOF (buffer) || chunk_size >= chunk_length) + if (chunk_size < 1 || chunk_size >= SIGNED_SIZEOF (buffer) || bytesread + chunk_size > chunk_length) { psf_log_printf (psf, " *** %M : %u (too big)\n", chunk, chunk_size) ; goto cleanup_subchunk_parse ; } ; @@ -1073,7 +1073,7 @@ wavlike_subchunk_parse (SF_PRIVATE *psf, int chunk, uint32_t chunk_length) case note_MARKER : bytesread += psf_binheader_readf (psf, "4", &chunk_size) ; chunk_size += (chunk_size & 1) ; - if (chunk_size >= SIGNED_SIZEOF (buffer) || chunk_size >= chunk_length) + if (chunk_size >= SIGNED_SIZEOF (buffer) || bytesread + chunk_size > chunk_length) { psf_log_printf (psf, " *** %M : %u (too big)\n", chunk, chunk_size) ; goto cleanup_subchunk_parse ; } ; From e2c38cd5c4852d7c83b74defd86a6d35e4403ad5 Mon Sep 17 00:00:00 2001 From: Mads Kiilerich Date: Wed, 26 Oct 2022 13:22:44 +0200 Subject: [PATCH 197/204] src/common.c: psf_log_printf should not emit ASCII control characters with %M --- src/common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common.c b/src/common.c index 40c26b97..1bbe00f5 100644 --- a/src/common.c +++ b/src/common.c @@ -375,7 +375,7 @@ psf_log_printf (SF_PRIVATE *psf, const char *format, ...) strptr = istr ; while (*strptr) { c = *strptr++ ; - log_putchar (psf, c) ; + log_putchar (psf, psf_isprint (c) ? c : '.') ; } ; break ; From 0a52a40111466b82d3dc40031fe0b4e9d1f2f507 Mon Sep 17 00:00:00 2001 From: Mads Kiilerich Date: Wed, 26 Oct 2022 14:06:51 +0200 Subject: [PATCH 198/204] tests/cpp_test.cc: improve error reporting in check_title The test somehow briefly failed for me. This will help debug if it happens again. --- tests/cpp_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cpp_test.cc b/tests/cpp_test.cc index 6e39da6d..e05a7370 100644 --- a/tests/cpp_test.cc +++ b/tests/cpp_test.cc @@ -106,7 +106,7 @@ check_title (const SndfileHandle & file, const char * filename) title = file.getString (SF_STR_TITLE) ; if (title == NULL) - { printf ("\n\n%s %d : Error : No title.\n\n", __func__, __LINE__) ; + { printf ("\n\n%s %d : Error : No title for %s.\n\n", __func__, __LINE__, filename) ; exit (1) ; } ; From c59207ef9e302ac123d9af5fcd9591939c85c04a Mon Sep 17 00:00:00 2001 From: Mads Kiilerich Date: Wed, 26 Oct 2022 16:04:31 +0200 Subject: [PATCH 199/204] src/wavlike.c: fix typo --- src/wavlike.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wavlike.c b/src/wavlike.c index 97b62429..ea42b8ca 100644 --- a/src/wavlike.c +++ b/src/wavlike.c @@ -981,7 +981,7 @@ wavlike_subchunk_parse (SF_PRIVATE *psf, int chunk, uint32_t chunk_length) switch (chunk) { case adtl_MARKER : case INFO_MARKER : - /* These markers don't contain anything, not even a chunk lebgth. */ + /* These markers don't contain anything, not even a chunk length. */ psf_log_printf (psf, " %M\n", chunk) ; continue ; From d8503a6645b6cb37e3f91c37b0b206601a12da64 Mon Sep 17 00:00:00 2001 From: evpobr Date: Thu, 6 Oct 2022 13:52:17 +0500 Subject: [PATCH 200/204] Fix clipping of non-int32 numbers Thanks @umlaeute. --- src/common.c | 8 -------- src/double64.c | 4 ++-- src/flac.c | 24 ++++++++++++------------ src/float32.c | 4 ++-- src/pcm.c | 32 -------------------------------- 5 files changed, 16 insertions(+), 56 deletions(-) diff --git a/src/common.c b/src/common.c index 1bbe00f5..b58c9db5 100644 --- a/src/common.c +++ b/src/common.c @@ -1692,18 +1692,14 @@ psf_f2s_clip_array (const float *src, short *dest, int count, int normalize) for (int i = 0 ; i < count ; i++) { scaled_value = src [i] * normfact ; -#if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFF)) { dest [i] = 0x7FFF ; continue ; } ; -#endif -#if CPU_CLIPS_NEGATIVE == 0 if (scaled_value <= (-8.0 * 0x1000)) { dest [i] = -0x7FFF - 1 ; continue ; } ; -#endif dest [i] = psf_lrintf (scaled_value) ; } ; @@ -1730,18 +1726,14 @@ psf_d2s_clip_array (const double *src, short *dest, int count, int normalize) for (int i = 0 ; i < count ; i++) { scaled_value = src [i] * normfact ; -#if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFF)) { dest [i] = 0x7FFF ; continue ; } ; -#endif -#if CPU_CLIPS_NEGATIVE == 0 if (scaled_value <= (-8.0 * 0x1000)) { dest [i] = -0x7FFF - 1 ; continue ; } ; -#endif dest [i] = psf_lrint (scaled_value) ; } ; diff --git a/src/double64.c b/src/double64.c index df49a8ff..0c79cd58 100644 --- a/src/double64.c +++ b/src/double64.c @@ -496,9 +496,9 @@ d2s_clip_array (const double *src, int count, short *dest, double scale) { for (int i = 0 ; i < count ; i++) { double tmp = scale * src [i] ; - if (CPU_CLIPS_POSITIVE == 0 && tmp > 32767.0) + if (tmp > 32767.0) dest [i] = SHRT_MAX ; - else if (CPU_CLIPS_NEGATIVE == 0 && tmp < -32768.0) + else if (tmp < -32768.0) dest [i] = SHRT_MIN ; else dest [i] = psf_lrint (tmp) ; diff --git a/src/flac.c b/src/flac.c index 20cbb1b9..073ce553 100644 --- a/src/flac.c +++ b/src/flac.c @@ -1191,11 +1191,11 @@ f2flac8_clip_array (const float *src, int32_t *dest, int count, int normalize) for (int i = 0 ; i < count ; i++) { scaled_value = src [i] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7F)) + if (scaled_value >= (1.0 * 0x7F)) { dest [i] = 0x7F ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10)) + if (scaled_value <= (-8.0 * 0x10)) { dest [i] = -0x80 ; continue ; } ; @@ -1213,11 +1213,11 @@ f2flac16_clip_array (const float *src, int32_t *dest, int count, int normalize) for (int i = 0 ; i < count ; i++) { scaled_value = src [i] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFF)) + if (scaled_value >= (1.0 * 0x7FFF)) { dest [i] = 0x7FFF ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x1000)) + if (scaled_value <= (-8.0 * 0x1000)) { dest [i] = -0x8000 ; continue ; } ; @@ -1233,12 +1233,12 @@ f2flac24_clip_array (const float *src, int32_t *dest, int count, int normalize) for (int i = 0 ; i < count ; i++) { scaled_value = src [i] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFF)) + if (scaled_value >= (1.0 * 0x7FFFFF)) { dest [i] = 0x7FFFFF ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x100000)) + if (scaled_value <= (-8.0 * 0x100000)) { dest [i] = -0x800000 ; continue ; } @@ -1322,11 +1322,11 @@ d2flac8_clip_array (const double *src, int32_t *dest, int count, int normalize) for (int i = 0 ; i < count ; i++) { scaled_value = src [i] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7F)) + if (scaled_value >= (1.0 * 0x7F)) { dest [i] = 0x7F ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10)) + if (scaled_value <= (-8.0 * 0x10)) { dest [i] = -0x80 ; continue ; } ; @@ -1344,11 +1344,11 @@ d2flac16_clip_array (const double *src, int32_t *dest, int count, int normalize) for (int i = 0 ; i < count ; i++) { scaled_value = src [i] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFF)) + if (scaled_value >= (1.0 * 0x7FFF)) { dest [i] = 0x7FFF ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x1000)) + if (scaled_value <= (-8.0 * 0x1000)) { dest [i] = -0x8000 ; continue ; } ; @@ -1366,11 +1366,11 @@ d2flac24_clip_array (const double *src, int32_t *dest, int count, int normalize) for (int i = 0 ; i < count ; i++) { scaled_value = src [i] * normfact ; - if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFF)) + if (scaled_value >= (1.0 * 0x7FFFFF)) { dest [i] = 0x7FFFFF ; continue ; } ; - if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x100000)) + if (scaled_value <= (-8.0 * 0x100000)) { dest [i] = -0x800000 ; continue ; } ; diff --git a/src/float32.c b/src/float32.c index 2e4d4440..431e39bb 100644 --- a/src/float32.c +++ b/src/float32.c @@ -447,9 +447,9 @@ f2s_clip_array (const float *src, int count, short *dest, float scale) { for (int i = 0 ; i < count ; i++) { float tmp = scale * src [i] ; - if (CPU_CLIPS_POSITIVE == 0 && tmp > 32767.0) + if (tmp > 32767.0) dest [i] = SHRT_MAX ; - else if (CPU_CLIPS_NEGATIVE == 0 && tmp < -32768.0) + else if (tmp < -32768.0) dest [i] = SHRT_MIN ; else dest [i] = psf_lrintf (tmp) ; diff --git a/src/pcm.c b/src/pcm.c index 7e5fdf62..d7dc82c4 100644 --- a/src/pcm.c +++ b/src/pcm.c @@ -1786,18 +1786,14 @@ f2sc_clip_array (const float *src, signed char *dest, int count, int normalize) for (int i = 0 ; i < count ; i++) { scaled_value = src [i] * normfact ; -#if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) { dest [i] = 127 ; continue ; } ; -#endif -#if CPU_CLIPS_NEGATIVE == 0 if (scaled_value <= (-8.0 * 0x10000000)) { dest [i] = -128 ; continue ; } ; -#endif dest [i] = psf_lrintf (scaled_value) >> 24 ; } ; @@ -1849,18 +1845,14 @@ f2uc_clip_array (const float *src, unsigned char *dest, int count, int normalize for (int i = 0 ; i < count ; i++) { scaled_value = src [i] * normfact ; -#if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) { dest [i] = 0xFF ; continue ; } ; -#endif -#if CPU_CLIPS_NEGATIVE == 0 if (scaled_value <= (-8.0 * 0x10000000)) { dest [i] = 0 ; continue ; } ; -#endif dest [i] = (psf_lrintf (scaled_value) >> 24) + 128 ; } ; @@ -1920,20 +1912,16 @@ f2bes_clip_array (const float *src, short *dest, int count, int normalize) for (int i = 0 ; i < count ; i++) { ucptr = (unsigned char*) &dest [i] ; scaled_value = src [i] * normfact ; -#if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) { ucptr [1] = 0xFF ; ucptr [0] = 0x7F ; continue ; } ; -#endif -#if CPU_CLIPS_NEGATIVE == 0 if (scaled_value <= (-8.0 * 0x10000000)) { ucptr [1] = 0x00 ; ucptr [0] = 0x80 ; continue ; } ; -#endif value = psf_lrintf (scaled_value) ; ucptr [1] = value >> 16 ; @@ -1995,20 +1983,16 @@ f2les_clip_array (const float *src, short *dest, int count, int normalize) for (int i = 0 ; i < count ; i++) { ucptr = (unsigned char*) &dest [i] ; scaled_value = src [i] * normfact ; -#if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) { ucptr [0] = 0xFF ; ucptr [1] = 0x7F ; continue ; } ; -#endif -#if CPU_CLIPS_NEGATIVE == 0 if (scaled_value <= (-8.0 * 0x10000000)) { ucptr [0] = 0x00 ; ucptr [1] = 0x80 ; continue ; } ; -#endif value = psf_lrintf (scaled_value) ; ucptr [0] = value >> 16 ; @@ -2378,18 +2362,14 @@ d2sc_clip_array (const double *src, signed char *dest, int count, int normalize) for (int i = 0 ; i < count ; i++) { scaled_value = src [i] * normfact ; -#if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) { dest [i] = 127 ; continue ; } ; -#endif -#if CPU_CLIPS_NEGATIVE == 0 if (scaled_value <= (-8.0 * 0x10000000)) { dest [i] = -128 ; continue ; } ; -#endif dest [i] = psf_lrintf (scaled_value) >> 24 ; } ; @@ -2441,18 +2421,14 @@ d2uc_clip_array (const double *src, unsigned char *dest, int count, int normaliz for (int i = 0 ; i < count ; i++) { scaled_value = src [i] * normfact ; -#if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) { dest [i] = 255 ; continue ; } ; -#endif -#if CPU_CLIPS_NEGATIVE == 0 if (scaled_value <= (-8.0 * 0x10000000)) { dest [i] = 0 ; continue ; } ; -#endif dest [i] = (psf_lrint (src [i] * normfact) >> 24) + 128 ; } ; @@ -2512,20 +2488,16 @@ d2bes_clip_array (const double *src, short *dest, int count, int normalize) for (int i = 0 ; i < count ; i++) { ucptr = (unsigned char*) &dest [i] ; scaled_value = src [i] * normfact ; -#if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) { ucptr [1] = 0xFF ; ucptr [0] = 0x7F ; continue ; } ; -#endif -#if CPU_CLIPS_NEGATIVE == 0 if (scaled_value <= (-8.0 * 0x10000000)) { ucptr [1] = 0x00 ; ucptr [0] = 0x80 ; continue ; } ; -#endif value = psf_lrint (scaled_value) ; ucptr [1] = value >> 16 ; @@ -2587,20 +2559,16 @@ d2les_clip_array (const double *src, short *dest, int count, int normalize) for (int i = 0 ; i < count ; i++) { ucptr = (unsigned char*) &dest [i] ; scaled_value = src [i] * normfact ; -#if CPU_CLIPS_POSITIVE == 0 if (scaled_value >= (1.0 * 0x7FFFFFFF)) { ucptr [0] = 0xFF ; ucptr [1] = 0x7F ; continue ; } ; -#endif -#if CPU_CLIPS_NEGATIVE == 0 if (scaled_value <= (-8.0 * 0x10000000)) { ucptr [0] = 0x00 ; ucptr [1] = 0x80 ; continue ; } ; -#endif value = psf_lrint (scaled_value) ; ucptr [0] = value >> 16 ; From 043c9d8a8bb934651cac393883498ec56aa73a7d Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Mon, 12 Dec 2022 13:31:58 -0800 Subject: [PATCH 201/204] ogg: Check for NULL returns from ogg_sync_buffer (issue 863) --- src/ogg.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/ogg.c b/src/ogg.c index f8cb0a47..f3e92e14 100644 --- a/src/ogg.c +++ b/src/ogg.c @@ -165,6 +165,8 @@ ogg_read_first_page (SF_PRIVATE *psf, OGG_PRIVATE *odata) return SFE_NOT_SEEKABLE ; buffer = ogg_sync_buffer (&odata->osync, psf->header.indx) ; + if (buffer == NULL) + return SFE_MALLOC_FAILED ; memcpy (buffer, psf->header.ptr, psf->header.indx) ; ogg_sync_wrote (&odata->osync, psf->header.indx) ; @@ -297,6 +299,10 @@ ogg_sync_next_page (SF_PRIVATE * psf, ogg_page *og, sf_count_t readmax, sf_count else nb_read = OGG_SYNC_READ_SIZE ; buffer = (unsigned char *) ogg_sync_buffer (&odata->osync, nb_read) ; + if (buffer == NULL) + { psf->error = SFE_MALLOC_FAILED ; + return -1 ; + } read_ret = psf_fread (buffer, 1, nb_read, psf) ; if (read_ret == 0) return psf->error ? -1 : 0 ; From e79da07a0a402f3a5a9eb7ad87db41cc41ea357b Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Mon, 12 Dec 2022 14:34:38 -0800 Subject: [PATCH 202/204] wavlike: Find a use for unused bytes total --- src/wavlike.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/wavlike.c b/src/wavlike.c index ea42b8ca..7acdc80e 100644 --- a/src/wavlike.c +++ b/src/wavlike.c @@ -902,6 +902,9 @@ wavlike_read_cart_chunk (SF_PRIVATE *psf, uint32_t chunksize) bytes += psf_binheader_readf (psf, "b", c->tag_text, make_size_t (c->tag_text_size)) ; } ; + if (bytes < chunksize) + psf_log_printf (psf, " %d trailing bytes in cart chunk.\n", chunksize - bytes) ; + return 0 ; } /* wavlike_read_cart_chunk */ From a19fe100cbebec4ffda1eead0a06a5d6c24a2ef2 Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Mon, 12 Dec 2022 14:10:08 -0800 Subject: [PATCH 203/204] sndfile: Identify 'naked' MPEG streams last, even after rforks (issue 830) --- src/sndfile.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/sndfile.c b/src/sndfile.c index 21975f65..397740f0 100644 --- a/src/sndfile.c +++ b/src/sndfile.c @@ -2771,6 +2771,17 @@ format_from_extension (SF_PRIVATE *psf) return format ; } /* format_from_extension */ +static int +identify_mpeg (uint32_t marker) +{ if ((marker & MAKE_MARKER (0xFF, 0xE0, 0, 0)) == MAKE_MARKER (0xFF, 0xE0, 0, 0) && /* Frame sync */ + (marker & MAKE_MARKER (0, 0x18, 0, 0)) != MAKE_MARKER (0, 0x08, 0, 0) && /* Valid MPEG version */ + (marker & MAKE_MARKER (0, 0x06, 0, 0)) != MAKE_MARKER (0, 0, 0, 0) && /* Valid layer description */ + (marker & MAKE_MARKER (0, 0, 0xF0, 0)) != MAKE_MARKER (0, 0, 0xF0, 0) && /* Valid bitrate */ + (marker & MAKE_MARKER (0, 0, 0x0C, 0)) != MAKE_MARKER (0, 0, 0x0C, 0)) /* Valid samplerate */ + return SF_FORMAT_MPEG ; + return 0 ; +} /* identify_mpeg */ + static int guess_file_type (SF_PRIVATE *psf) { uint32_t buffer [3], format ; @@ -2872,13 +2883,6 @@ retry: if (buffer [0] == MAKE_MARKER ('R', 'F', '6', '4') && buffer [2] == MAKE_MARKER ('W', 'A', 'V', 'E')) return SF_FORMAT_RF64 ; - if ((buffer [0] & MAKE_MARKER (0xFF, 0xE0, 0, 0)) == MAKE_MARKER (0xFF, 0xE0, 0, 0) && /* Frame sync */ - (buffer [0] & MAKE_MARKER (0, 0x18, 0, 0)) != MAKE_MARKER (0, 0x08, 0, 0) && /* Valid MPEG version */ - (buffer [0] & MAKE_MARKER (0, 0x06, 0, 0)) != MAKE_MARKER (0, 0, 0, 0) && /* Valid layer description */ - (buffer [0] & MAKE_MARKER (0, 0, 0xF0, 0)) != MAKE_MARKER (0, 0, 0xF0, 0) && /* Valid bitrate */ - (buffer [0] & MAKE_MARKER (0, 0, 0x0C, 0)) != MAKE_MARKER (0, 0, 0x0C, 0)) /* Valid samplerate */ - return SF_FORMAT_MPEG ; - if (buffer [0] == MAKE_MARKER ('I', 'D', '3', 2) || buffer [0] == MAKE_MARKER ('I', 'D', '3', 3) || buffer [0] == MAKE_MARKER ('I', 'D', '3', 4)) { psf_log_printf (psf, "Found 'ID3' marker.\n") ; @@ -2887,6 +2891,10 @@ retry: return 0 ; } ; + /* ID3v2 tags + MPEG */ + if (psf->id3_header.len > 0 && (format = identify_mpeg (buffer [0])) != 0) + return format ; + /* Turtle Beach SMP 16-bit */ if (buffer [0] == MAKE_MARKER ('S', 'O', 'U', 'N') && buffer [1] == MAKE_MARKER ('D', ' ', 'S', 'A')) return 0 ; @@ -2898,10 +2906,16 @@ retry: if (buffer [0] == MAKE_MARKER ('a', 'j', 'k', 'g')) return 0 /*-SF_FORMAT_SHN-*/ ; - /* This must be the last one. */ + /* This must be (almost) the last one. */ if (psf->filelength > 0 && (format = try_resource_fork (psf)) != 0) return format ; + /* MPEG with no ID3v2 tags. Only have the MPEG sync header for + * identification and it is quite brief, and prone to false positives. + * Check for this last, even after resource forks. */ + if (psf->id3_header.len == 0 && (format = identify_mpeg (buffer [0])) != 0) + return format ; + return 0 ; } /* guess_file_type */ From 2ed38b57c5838ba6b7c8e726a62855845bd42ec5 Mon Sep 17 00:00:00 2001 From: David Seifert Date: Sun, 25 Dec 2022 11:19:16 +0100 Subject: [PATCH 204/204] Release 1.2.0 --- CHANGELOG.md | 2 +- CMakeLists.txt | 2 +- configure.ac | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d61761ee..5014508f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Unreleased] +## [1.2.0] - 2022-12-25 ### Fixed diff --git a/CMakeLists.txt b/CMakeLists.txt index 904a1a20..dda22431 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,7 +35,7 @@ if (BUILD_REGTEST) list (APPEND VCPKG_MANIFEST_FEATURES "regtest") endif () -project(libsndfile VERSION 1.1.0) +project(libsndfile VERSION 1.2.0) # # Variables diff --git a/configure.ac b/configure.ac index ec4463db..7b4fa61b 100644 --- a/configure.ac +++ b/configure.ac @@ -3,7 +3,7 @@ dnl Copyright (C) 1999-2022 Erik de Castro Lopo . dnl Require autoconf version >= 2.69 AC_PREREQ([2.69]) -AC_INIT([libsndfile],[1.1.0],[sndfile@mega-nerd.com], +AC_INIT([libsndfile],[1.2.0],[sndfile@mega-nerd.com], [libsndfile],[http://libsndfile.github.io/libsndfile/]) dnl Check whether we want to set defaults for CFLAGS, CXXFLAGS, CPPFLAGS and LDFLAGS @@ -88,7 +88,7 @@ dnl to 0. dnl This is libtool version of library, we add it to `--version-info` property. m4_define([lt_current], [1]) -m4_define([lt_revision], [34]) +m4_define([lt_revision], [35]) m4_define([lt_age], [0]) dnl This is ABI version for linker scripts, CMake uses the same format for