* [FFmpeg-devel] [PATCH 1/2] swresample/rematrix: Use correct function pointer types for calls
@ 2025-06-23 0:19 Andreas Rheinhardt
0 siblings, 0 replies; only message in thread
From: Andreas Rheinhardt @ 2025-06-23 0:19 UTC (permalink / raw)
To: FFmpeg development discussions and patches
[-- Attachment #1: Type: text/plain, Size: 29 bytes --]
Patches attached.
- Andreas
[-- Attachment #2: 0001-swresample-rematrix-Use-correct-function-pointer-typ.patch --]
[-- Type: text/x-patch, Size: 7276 bytes --]
From 520002b8a95d06baaf2a10c660e21bf84d036787 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Mon, 23 Jun 2025 02:02:54 +0200
Subject: [PATCH 1/2] swresample/rematrix: Use correct function pointer types
for calls
Calling a function via a different function pointer type is
undefined behavior (C11, 6.3.2.3 8); two pointer parameters
of different type are not equivalent in this regard, although
it happens to work in practice; the current code relies on this.
This patch brings the code in line with the spec. This fixes the
following FATE-tests when run with Clang-UBSan:
ac3-fixed-encode-2 audiomatch-afconvert-{16000,44100}-mono-he-{adts,m4a}
audiomatch-dolby-44100-mono-he-mp4 filter-metadata-avf-aphase-meter-mono
filter-pan-{downmix1,downmix2,mono2,stereo2,stereo3,stereo4,upmix1,upmix2}
lavf-dv_{pal,ntsc} matroska-encoding-delay
The error was something like
src/libswresample/rematrix.c:621:17: runtime error: call to function sum2_float through pointer to incorrect function type 'void (*)(void *, const void *, const void *, void *, int, int, int)
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
libswresample/rematrix.c | 30 +++++++++++++++---------------
libswresample/rematrix_template.c | 30 +++++++++++++++++++++++-------
2 files changed, 38 insertions(+), 22 deletions(-)
diff --git a/libswresample/rematrix.c b/libswresample/rematrix.c
index 5929ae4098..ed2d9fd883 100644
--- a/libswresample/rematrix.c
+++ b/libswresample/rematrix.c
@@ -494,13 +494,13 @@ av_cold int swri_rematrix_init(SwrContext *s){
}
*((int*)s->native_one) = 32768;
if (maxsum <= 32768) {
- s->mix_1_1_f = (mix_1_1_func_type*)copy_s16;
- s->mix_2_1_f = (mix_2_1_func_type*)sum2_s16;
- s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s16(s);
+ s->mix_1_1_f = copy_s16;
+ s->mix_2_1_f = sum2_s16;
+ s->mix_any_f = get_mix_any_func_s16(s);
} else {
- s->mix_1_1_f = (mix_1_1_func_type*)copy_clip_s16;
- s->mix_2_1_f = (mix_2_1_func_type*)sum2_clip_s16;
- s->mix_any_f = (mix_any_func_type*)get_mix_any_func_clip_s16(s);
+ s->mix_1_1_f = copy_clip_s16;
+ s->mix_2_1_f = sum2_clip_s16;
+ s->mix_any_f = get_mix_any_func_clip_s16(s);
}
}else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
s->native_matrix = av_calloc(nb_in * nb_out, sizeof(float));
@@ -511,9 +511,9 @@ av_cold int swri_rematrix_init(SwrContext *s){
for (j = 0; j < nb_in; j++)
((float*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
*((float*)s->native_one) = 1.0;
- s->mix_1_1_f = (mix_1_1_func_type*)copy_float;
- s->mix_2_1_f = (mix_2_1_func_type*)sum2_float;
- s->mix_any_f = (mix_any_func_type*)get_mix_any_func_float(s);
+ s->mix_1_1_f = copy_float;
+ s->mix_2_1_f = sum2_float;
+ s->mix_any_f = get_mix_any_func_float(s);
}else if(s->midbuf.fmt == AV_SAMPLE_FMT_DBLP){
s->native_matrix = av_calloc(nb_in * nb_out, sizeof(double));
s->native_one = av_mallocz(sizeof(double));
@@ -523,9 +523,9 @@ av_cold int swri_rematrix_init(SwrContext *s){
for (j = 0; j < nb_in; j++)
((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
*((double*)s->native_one) = 1.0;
- s->mix_1_1_f = (mix_1_1_func_type*)copy_double;
- s->mix_2_1_f = (mix_2_1_func_type*)sum2_double;
- s->mix_any_f = (mix_any_func_type*)get_mix_any_func_double(s);
+ s->mix_1_1_f = copy_double;
+ s->mix_2_1_f = sum2_double;
+ s->mix_any_f = get_mix_any_func_double(s);
}else if(s->midbuf.fmt == AV_SAMPLE_FMT_S32P){
s->native_one = av_mallocz(sizeof(int));
if (!s->native_one)
@@ -545,9 +545,9 @@ av_cold int swri_rematrix_init(SwrContext *s){
}
}
*((int*)s->native_one) = 32768;
- s->mix_1_1_f = (mix_1_1_func_type*)copy_s32;
- s->mix_2_1_f = (mix_2_1_func_type*)sum2_s32;
- s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s32(s);
+ s->mix_1_1_f = copy_s32;
+ s->mix_2_1_f = sum2_s32;
+ s->mix_any_f = get_mix_any_func_s32(s);
}else
av_assert0(0);
//FIXME quantize for integeres
diff --git a/libswresample/rematrix_template.c b/libswresample/rematrix_template.c
index d4726c4dbb..62ba3e53bf 100644
--- a/libswresample/rematrix_template.c
+++ b/libswresample/rematrix_template.c
@@ -49,9 +49,12 @@
# define RENAME(x) x ## _s32
#endif
-typedef void (RENAME(mix_any_func_type))(SAMPLE **out, const SAMPLE **in1, COEFF *coeffp, integer len);
-
-static void RENAME(sum2)(SAMPLE *out, const SAMPLE *in1, const SAMPLE *in2, COEFF *coeffp, integer index1, integer index2, integer len){
+static void RENAME(sum2)(void *out_, const void *in1_, const void *in2_,
+ void *coeffp_, integer index1, integer index2, integer len)
+{
+ const SAMPLE *in1 = in1_, *in2 = in2_;
+ const COEFF *coeffp = coeffp_;
+ SAMPLE *out = out_;
int i;
INTER coeff1 = coeffp[index1];
INTER coeff2 = coeffp[index2];
@@ -60,14 +63,22 @@ static void RENAME(sum2)(SAMPLE *out, const SAMPLE *in1, const SAMPLE *in2, COEF
out[i] = R(coeff1*in1[i] + coeff2*in2[i]);
}
-static void RENAME(copy)(SAMPLE *out, const SAMPLE *in, COEFF *coeffp, integer index, integer len){
+static void RENAME(copy)(void *out_, const void *in_, void *coeffp_, integer index, integer len)
+{
+ const COEFF *coeffp = coeffp_;
+ const SAMPLE *in = in_;
+ SAMPLE *out = out_;
int i;
INTER coeff = coeffp[index];
for(i=0; i<len; i++)
out[i] = R(coeff*in[i]);
}
-static void RENAME(mix6to2)(SAMPLE **out, const SAMPLE **in, COEFF *coeffp, integer len){
+static void RENAME(mix6to2)(uint8_t **out_, const uint8_t **in_, void *coeffp_, integer len)
+{
+ const SAMPLE *const *const in = (const SAMPLE *const *)in_;
+ SAMPLE *const *const out = (SAMPLE *const*)out_;
+ const COEFF *coeffp = coeffp_;
int i;
for(i=0; i<len; i++) {
@@ -77,7 +88,11 @@ static void RENAME(mix6to2)(SAMPLE **out, const SAMPLE **in, COEFF *coeffp, inte
}
}
-static void RENAME(mix8to2)(SAMPLE **out, const SAMPLE **in, COEFF *coeffp, integer len){
+static void RENAME(mix8to2)(uint8_t **out_, const uint8_t **in_, void *coeffp_, integer len)
+{
+ const SAMPLE *const *const in = (const SAMPLE *const *)in_;
+ SAMPLE *const *const out = (SAMPLE *const*)out_;
+ const COEFF *coeffp = coeffp_;
int i;
for(i=0; i<len; i++) {
@@ -87,7 +102,8 @@ static void RENAME(mix8to2)(SAMPLE **out, const SAMPLE **in, COEFF *coeffp, inte
}
}
-static RENAME(mix_any_func_type) *RENAME(get_mix_any_func)(SwrContext *s){
+static mix_any_func_type *RENAME(get_mix_any_func)(SwrContext *s)
+{
if ( !av_channel_layout_compare(&s->out_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO)
&& ( !av_channel_layout_compare(&s->in_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1)
|| !av_channel_layout_compare(&s->in_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1_BACK))
--
2.45.2
[-- Attachment #3: 0002-swresample-rematrix-Constify.patch --]
[-- Type: text/x-patch, Size: 4066 bytes --]
From 45fb4685dd504c6058f8cbfb703324d22e447b4b Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Mon, 23 Jun 2025 02:15:00 +0200
Subject: [PATCH 2/2] swresample/rematrix: Constify
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
libswresample/rematrix.c | 2 +-
libswresample/rematrix_template.c | 11 +++++++----
libswresample/swresample_internal.h | 6 +++---
3 files changed, 11 insertions(+), 8 deletions(-)
diff --git a/libswresample/rematrix.c b/libswresample/rematrix.c
index ed2d9fd883..6e1fb0c66f 100644
--- a/libswresample/rematrix.c
+++ b/libswresample/rematrix.c
@@ -581,7 +581,7 @@ int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mus
int off = 0;
if(s->mix_any_f) {
- s->mix_any_f(out->ch, (const uint8_t **)in->ch, s->native_matrix, len);
+ s->mix_any_f(out->ch, (const uint8_t *const *)in->ch, s->native_matrix, len);
return 0;
}
diff --git a/libswresample/rematrix_template.c b/libswresample/rematrix_template.c
index 62ba3e53bf..b70e7489b0 100644
--- a/libswresample/rematrix_template.c
+++ b/libswresample/rematrix_template.c
@@ -50,7 +50,7 @@
#endif
static void RENAME(sum2)(void *out_, const void *in1_, const void *in2_,
- void *coeffp_, integer index1, integer index2, integer len)
+ const void *coeffp_, integer index1, integer index2, integer len)
{
const SAMPLE *in1 = in1_, *in2 = in2_;
const COEFF *coeffp = coeffp_;
@@ -63,7 +63,8 @@ static void RENAME(sum2)(void *out_, const void *in1_, const void *in2_,
out[i] = R(coeff1*in1[i] + coeff2*in2[i]);
}
-static void RENAME(copy)(void *out_, const void *in_, void *coeffp_, integer index, integer len)
+static void RENAME(copy)(void *out_, const void *in_, const void *coeffp_,
+ integer index, integer len)
{
const COEFF *coeffp = coeffp_;
const SAMPLE *in = in_;
@@ -74,7 +75,8 @@ static void RENAME(copy)(void *out_, const void *in_, void *coeffp_, integer ind
out[i] = R(coeff*in[i]);
}
-static void RENAME(mix6to2)(uint8_t **out_, const uint8_t **in_, void *coeffp_, integer len)
+static void RENAME(mix6to2)(uint8_t *const *out_, const uint8_t *const *in_,
+ const void *coeffp_, integer len)
{
const SAMPLE *const *const in = (const SAMPLE *const *)in_;
SAMPLE *const *const out = (SAMPLE *const*)out_;
@@ -88,7 +90,8 @@ static void RENAME(mix6to2)(uint8_t **out_, const uint8_t **in_, void *coeffp_,
}
}
-static void RENAME(mix8to2)(uint8_t **out_, const uint8_t **in_, void *coeffp_, integer len)
+static void RENAME(mix8to2)(uint8_t *const *out_, const uint8_t *const *in_,
+ const void *coeffp_, integer len)
{
const SAMPLE *const *const in = (const SAMPLE *const *)in_;
SAMPLE *const *const out = (SAMPLE *const*)out_;
diff --git a/libswresample/swresample_internal.h b/libswresample/swresample_internal.h
index 7e46b16fb2..21c9e33fa1 100644
--- a/libswresample/swresample_internal.h
+++ b/libswresample/swresample_internal.h
@@ -37,10 +37,10 @@ typedef int64_t integer;
typedef int integer;
#endif
-typedef void (mix_1_1_func_type)(void *out, const void *in, void *coeffp, integer index, integer len);
-typedef void (mix_2_1_func_type)(void *out, const void *in1, const void *in2, void *coeffp, integer index1, integer index2, integer len);
+typedef void (mix_1_1_func_type)(void *out, const void *in, const void *coeffp, integer index, integer len);
+typedef void (mix_2_1_func_type)(void *out, const void *in1, const void *in2, const void *coeffp, integer index1, integer index2, integer len);
-typedef void (mix_any_func_type)(uint8_t **out, const uint8_t **in1, void *coeffp, integer len);
+typedef void (mix_any_func_type)(uint8_t *const *out, const uint8_t *const *in1, const void *coeffp, integer len);
typedef struct AudioData{
uint8_t *ch[SWR_CH_MAX]; ///< samples buffer per channel
--
2.45.2
[-- Attachment #4: Type: text/plain, Size: 251 bytes --]
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2025-06-23 0:19 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-06-23 0:19 [FFmpeg-devel] [PATCH 1/2] swresample/rematrix: Use correct function pointer types for calls Andreas Rheinhardt
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git