History

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)

Andreas Rheinhardt committed 1 year ago

Commit 589f1e51 parent bdb4cd42 files · +38 −22Browse files

Unified · Split

2 files
  1. libswresample/rematrix.c +15 −15
  2. libswresample/rematrix_template.c +23 −7
libswresample/rematrix.c+15−15
@@ -494,13 +494,13 @@ av_cold int swri_rematrix_init(SwrContext *s){ · 493 unmodified lines
494494 }
495495 *((int*)s->native_one) = 32768;
496496 if (maxsum <= 32768) {
497− s->mix_1_1_f = (mix_1_1_func_type*)copy_s16;
498− s->mix_2_1_f = (mix_2_1_func_type*)sum2_s16;
499− s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s16(s);
497+ s->mix_1_1_f = copy_s16;
498+ s->mix_2_1_f = sum2_s16;
499+ s->mix_any_f = get_mix_any_func_s16(s);
500500 } else {
501− s->mix_1_1_f = (mix_1_1_func_type*)copy_clip_s16;
502− s->mix_2_1_f = (mix_2_1_func_type*)sum2_clip_s16;
503− s->mix_any_f = (mix_any_func_type*)get_mix_any_func_clip_s16(s);
501+ s->mix_1_1_f = copy_clip_s16;
502+ s->mix_2_1_f = sum2_clip_s16;
503+ s->mix_any_f = get_mix_any_func_clip_s16(s);
504504 }
505505 }else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
506506 s->native_matrix = av_calloc(nb_in * nb_out, sizeof(float));
@@ -511,9 +511,9 @@ av_cold int swri_rematrix_init(SwrContext *s){ · 4 unmodified lines
511511 for (j = 0; j < nb_in; j++)
512512 ((float*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
513513 *((float*)s->native_one) = 1.0;
514− s->mix_1_1_f = (mix_1_1_func_type*)copy_float;
515− s->mix_2_1_f = (mix_2_1_func_type*)sum2_float;
516− s->mix_any_f = (mix_any_func_type*)get_mix_any_func_float(s);
514+ s->mix_1_1_f = copy_float;
515+ s->mix_2_1_f = sum2_float;
516+ s->mix_any_f = get_mix_any_func_float(s);
517517 }else if(s->midbuf.fmt == AV_SAMPLE_FMT_DBLP){
518518 s->native_matrix = av_calloc(nb_in * nb_out, sizeof(double));
519519 s->native_one = av_mallocz(sizeof(double));
@@ -523,9 +523,9 @@ av_cold int swri_rematrix_init(SwrContext *s){ · 3 unmodified lines
523523 for (j = 0; j < nb_in; j++)
524524 ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
525525 *((double*)s->native_one) = 1.0;
526− s->mix_1_1_f = (mix_1_1_func_type*)copy_double;
527− s->mix_2_1_f = (mix_2_1_func_type*)sum2_double;
528− s->mix_any_f = (mix_any_func_type*)get_mix_any_func_double(s);
526+ s->mix_1_1_f = copy_double;
527+ s->mix_2_1_f = sum2_double;
528+ s->mix_any_f = get_mix_any_func_double(s);
529529 }else if(s->midbuf.fmt == AV_SAMPLE_FMT_S32P){
530530 s->native_one = av_mallocz(sizeof(int));
531531 if (!s->native_one)
@@ -545,9 +545,9 @@ av_cold int swri_rematrix_init(SwrContext *s){ · 13 unmodified lines
545545 }
546546 }
547547 *((int*)s->native_one) = 32768;
548− s->mix_1_1_f = (mix_1_1_func_type*)copy_s32;
549− s->mix_2_1_f = (mix_2_1_func_type*)sum2_s32;
550− s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s32(s);
548+ s->mix_1_1_f = copy_s32;
549+ s->mix_2_1_f = sum2_s32;
550+ s->mix_any_f = get_mix_any_func_s32(s);
551551 }else
552552 av_assert0(0);
553553 //FIXME quantize for integeres
libswresample/rematrix_template.c+23−7
@@ -49,9 +49,12 @@ · 48 unmodified lines
4949 # define RENAME(x) x ## _s32
5050 #endif
5151
52−typedef void (RENAME(mix_any_func_type))(SAMPLE **out, const SAMPLE **in1, COEFF *coeffp, integer len);
53−
54−static void RENAME(sum2)(SAMPLE *out, const SAMPLE *in1, const SAMPLE *in2, COEFF *coeffp, integer index1, integer index2, integer len){
52+static void RENAME(sum2)(void *out_, const void *in1_, const void *in2_,
53+ void *coeffp_, integer index1, integer index2, integer len)
54+{
55+ const SAMPLE *in1 = in1_, *in2 = in2_;
56+ const COEFF *coeffp = coeffp_;
57+ SAMPLE *out = out_;
5558 int i;
5659 INTER coeff1 = coeffp[index1];
5760 INTER coeff2 = coeffp[index2];
@@ -60,14 +63,22 @@ static void RENAME(sum2)(SAMPLE *out, const SAMPLE *in1, const SAMPLE *in2, COEF · 2 unmodified lines
6063 out[i] = R(coeff1*in1[i] + coeff2*in2[i]);
6164 }
6265
63−static void RENAME(copy)(SAMPLE *out, const SAMPLE *in, COEFF *coeffp, integer index, integer len){
66+static void RENAME(copy)(void *out_, const void *in_, void *coeffp_, integer index, integer len)
67+{
68+ const COEFF *coeffp = coeffp_;
69+ const SAMPLE *in = in_;
70+ SAMPLE *out = out_;
6471 int i;
6572 INTER coeff = coeffp[index];
6673 for(i=0; i<len; i++)
6774 out[i] = R(coeff*in[i]);
6875 }
6976
70−static void RENAME(mix6to2)(SAMPLE **out, const SAMPLE **in, COEFF *coeffp, integer len){
77+static void RENAME(mix6to2)(uint8_t **out_, const uint8_t **in_, void *coeffp_, integer len)
78+{
79+ const SAMPLE *const *const in = (const SAMPLE *const *)in_;
80+ SAMPLE *const *const out = (SAMPLE *const*)out_;
81+ const COEFF *coeffp = coeffp_;
7182 int i;
7283
7384 for(i=0; i<len; i++) {
@@ -77,7 +88,11 @@ static void RENAME(mix6to2)(SAMPLE **out, const SAMPLE **in, COEFF *coeffp, inte · 3 unmodified lines
7788 }
7889 }
7990
80−static void RENAME(mix8to2)(SAMPLE **out, const SAMPLE **in, COEFF *coeffp, integer len){
91+static void RENAME(mix8to2)(uint8_t **out_, const uint8_t **in_, void *coeffp_, integer len)
92+{
93+ const SAMPLE *const *const in = (const SAMPLE *const *)in_;
94+ SAMPLE *const *const out = (SAMPLE *const*)out_;
95+ const COEFF *coeffp = coeffp_;
8196 int i;
8297
8398 for(i=0; i<len; i++) {
@@ -87,7 +102,8 @@ static void RENAME(mix8to2)(SAMPLE **out, const SAMPLE **in, COEFF *coeffp, inte · 3 unmodified lines
87102 }
88103 }
89104
90−static RENAME(mix_any_func_type) *RENAME(get_mix_any_func)(SwrContext *s){
105+static mix_any_func_type *RENAME(get_mix_any_func)(SwrContext *s)
106+{
91107 if ( !av_channel_layout_compare(&s->out_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO)
92108 && ( !av_channel_layout_compare(&s->in_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1)
93109 || !av_channel_layout_compare(&s->in_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1_BACK))

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t