When resampling first, a call that produced no output passed midbuf to
swri_rematrix() before it was ever allocated, and copied channels did a
memcpy() of zero bytes from NULL. Skip rematrixing zero samples, as the
output conversion already does.
Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
swr_init() failures were only printed and the test still exited with 0.
Return failure through the cleanup instead, also for the allocation
failures that returned without it.
Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
The two contexts and the layouts were never released, which trips leak
checking once the test runs under FATE.
Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
With a matrix, samples of more than 16 bits are mixed in floating
point, which takes the lowest bits of s32. A matrix that only copies
channels computes nothing, so select the internal format like without
a matrix. Layouts that differ in the order of their channels or in
unused ones get such a matrix.
The matrix is built before the format is selected for this. The limit
for its coefficients no longer looks at the selected format. For
mixing that is an integer format only if the output has one or if it
was set, which is what the limit looks at now.
s64 still goes through double, the matrix has no functions for it.
Bump the micro version, as this and the previous commits change which
custom layouts can be used.
Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
Layouts with the same channels in another order need a matrix, but
nothing is mixed. The checks for channels that can be mixed rejected
them anyway, for example BL+BR to BR+BL.
Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
The pair is mixed like stereo if the other side has no such channels.
It was only recognized as the native layout, so a custom layout with
the pair in another order or next to unused channels was rejected for
the lack of a front channel.
Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
A layout of one channel is mixed like mono, whatever the channel is.
Unused channels carry no audio, so the same holds for a custom layout
that has a single channel next to them. It was rejected for being
asymmetric.
Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
It only wrapped math.h and mathematics.h since the fallbacks went away.
Include math.h and mathematics.h directly where needed. internal.h keeps
both so that the many translation units relying on them transitively
keep building.
Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
Convert DSD to PCM with the same 96-tap symmetric lowpass filter the
libavcodec DSD decoders use. The implementation is copied from
libavcodec/dsd.c, minus the LSBF table variants, since AV_SAMPLE_FMT_DSD
is defined as MSBF.
The libavcodec copy will be removed in future commits as decoders are
transitioned to SWR.
The conversion is stateful (per-channel filter history), so the audio
conversion functions gain a per-channel state argument.
DSD silence is the 0x69 bit pattern instead of the 0x80 used for PCM.
Only a direct DSD to float conversion function is provided. For format
pairs without a direct conversion function (e.g. DSD to s16 with equal
rates), swr_init() now falls back from the full_convert shortcut to
the generic two-stage path instead of failing, and reports unsupported
conversions with a proper error message instead of ENOMEM.
Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
The channel count passed as a 32-bit register (w3) is later used
as a 64-bit register (x3) for pointer offsets without proper sign
extension, resulting in misbehavior. This commit adds an explicit
sxtw instruction to ensure the channel count is properly sign-extended.
Additionally, standardizes the use of the 32-bit w2 and w12 registers
in the core length logic across functions to prevent sign extension
issues with lengths without relying on explicit 64-bit casting, matching
expected input bounds tracking.
Signed-off-by: Hao Chen <haochenc@google.com>
Said #define ASSERT_LEVEL 2 was ineffective because it followed
the inclusion of avassert.h. It only leads to warnings when
ASSERT_LEVEL is defined to something other than 2 (it is not
defined by default).
Also avoid using av_assert2() in a test tool: These are supposed to
be picky, so using always-enabled av_assert0 is appropriate.
This was probably the intention of the ASSERT_LEVEL override
all along.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
custom layouts containing AV_CHAN_UNUSED were rejected by
swr_build_matrix2().
accept unused channels in input and output layouts. preserve their
physical positions, clear input columns, and leave output rows silent.
add fate coverage for both cases. this fixes#24094.
Signed-off-by: Ayoub Nabil Boubagrat <237098474+ayoubnabil@users.noreply.github.com>
top-back channels are currently left unaccounted and can be dropped during downmixing.
when the output retains top-front channels, follow IAMF 1.1.0 and fold top-back into top-front at 0.707. prefer this path over ear-level rear channels to preserve the height layer in x.1.4 to x.1.2 downmixes.
when no matching height output remains, map top-back to back or side channels, then fall back to front or mono outputs. handle top-back center separately and add direct tests for every matrix path.
Signed-off-by: Ayoub Nabil Boubagrat <237098474+ayoubnabil@users.noreply.github.com>
ITU-R BS.2127-1 maps U+030 and U-030 to the matching ear-level front channels at unity gain.
the current matrix uses 0.707 when the input also contains front channels. use 1.0 instead.
Signed-off-by: Ayoub Nabil Boubagrat <237098474+ayoubnabil@users.noreply.github.com>
The resample asm code as it is currently handles 1 sample at a time
The asm code should be redesigned and handle more than 1 sample at a
time. That is the whole purpose of SIMD. There is also multiple samples
available that need identical handling like from several channels or
similar handling from other points in time.
Such redesign would make the resampler faster and would change the
requirements of padding and maybe memory layout. So it seems simpler
to just avoid overwriting in the asm as it is today than to have
the allocation handle specific overallocation for asm code that
ideally should be redesigned
Fixes writing 16bits over the end of the array
This is an alternative fix for https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/23053
Found-by: Ivan Grigorev <ivangrigoriev@meta.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
Add a regression test exercising the swr_convert(N) -> swr_convert(2N)
edge case: the second call reuses the internal preout buffer at full
capacity, with no trailing slack from swri_realloc_audio()'s amortized
doubling. internal_sample_fmt is forced to S16P to reach the int16 SIMD
resample path, where ff_resample_common_int16_sse2 overruns its
destination by 2 bytes on the last iteration.
Without a resampler fix this test fails under valgrind/ASAN with a
heap-buffer-overflow (Invalid write of size 4, 2 bytes past the end).
Signed-off-by: Ivan Grigorev <ivangrigoriev@meta.com>
resample_linear can produce overflows with craftet input,
The added casts should have no effect on the binary output or the operations they
just change things to a defined regime
Fixes: signed integer overflow: 2069416960 + 78151680 cannot be represented in type 'int'
Fixes: 472047214/clusterfuzz-testcase-minimized-ffmpeg_SWR_fuzzer-6374046976770048
Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
All callers in FFmpeg check this already, but it is a public
function that can plausibly be given more channels.
In which case out of array writes would occur
This is likely a regression from when channel layouts where extended
to support more than 64 channels
Found-by: 이동준 <ldj6192@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
There are no MMX functions left in all of libswresample, so checking
for it makes no sense. Notice that configure currently disables x86asm
if mmx is disabled, so the check is also redundant.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
To do so, simply add these init files to X86ASM-OBJS instead of OBJS
in the Makefile. The former is already used for the actual assembly
files, but using them for the C init files just works, because the build
system uses file extensions to derive whether it is a C or a NASM file.
This avoids compiling unused function stubs and also reduces our
reliance on DCE: We don't add %if checks to the asm files except
for AVX, AVX2, FMA3, FMA4, XOP and AVX512, so all the MMX-SSE4
functions will be available. It also allows to remove HAVE_X86ASM checks
in these init files.
(x86/ops.c has already been put in X86ASM-OBJS.)
Reviewed-by: Kacper Michajłow <kasper93@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Before commit e96d90eed6 lavu/internal.h
contained redefined various discouraged/forbidden functions to induce
compilation failures upon use, like e.g.
#define malloc please_use_av_malloc
In order to use these functions, some files had to undefine these
macros. This commit removes the remaining pointless undefs.
Reviewed-by: Rémi Denis-Courmont <remi@remlab.net>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This is required placement by standard [[maybe_unused]] attribute, works
the same for __attribute__((unused)).
Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
Saves 16KiB from SwrContext.
(FATE would also pass if one made the double matrix part of
the union, too, but I don't know whether this is truely correct,
because swri_rematrix() accesses the double matrix (to check whether
it is equal to 1.0) even when one of the other matrices is in use.)
Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>