aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Niedermayer <michael@niedermayer.cc>2016-05-15 21:34:37 +0200
committerMichael Niedermayer <michael@niedermayer.cc>2016-06-05 02:40:31 +0200
commit1cd872a7d555bd74a53bb7538bfb7c393c04e42d (patch)
tree8f29b0c9fe3a3cf97a8a87a08dfbe2a6cc9f1e93
parentf6586db165da1007e347bfa2822d0e183dd841a5 (diff)
downloadandroid_external_ffmpeg-1cd872a7d555bd74a53bb7538bfb7c393c04e42d.tar.gz
android_external_ffmpeg-1cd872a7d555bd74a53bb7538bfb7c393c04e42d.tar.bz2
android_external_ffmpeg-1cd872a7d555bd74a53bb7538bfb7c393c04e42d.zip
swresample/rematrix: Use clipping s16 rematrixing if overflows are possible
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc> (cherry picked from commit 2f76157eb05bf63725f96167feda6b2e07501c7e) Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
-rw-r--r--libswresample/rematrix.c19
-rw-r--r--libswresample/rematrix_template.c7
2 files changed, 22 insertions, 4 deletions
diff --git a/libswresample/rematrix.c b/libswresample/rematrix.c
index f18c9f67db..ddba0433e8 100644
--- a/libswresample/rematrix.c
+++ b/libswresample/rematrix.c
@@ -32,6 +32,9 @@
#define TEMPLATE_REMATRIX_S16
#include "rematrix_template.c"
+#define TEMPLATE_CLIP
+#include "rematrix_template.c"
+#undef TEMPLATE_CLIP
#undef TEMPLATE_REMATRIX_S16
#define TEMPLATE_REMATRIX_S32
@@ -367,23 +370,33 @@ av_cold int swri_rematrix_init(SwrContext *s){
return r;
}
if (s->midbuf.fmt == AV_SAMPLE_FMT_S16P){
+ int maxsum = 0;
s->native_matrix = av_calloc(nb_in * nb_out, sizeof(int));
s->native_one = av_mallocz(sizeof(int));
if (!s->native_matrix || !s->native_one)
return AVERROR(ENOMEM);
for (i = 0; i < nb_out; i++) {
double rem = 0;
+ int sum = 0;
for (j = 0; j < nb_in; j++) {
double target = s->matrix[i][j] * 32768 + rem;
((int*)s->native_matrix)[i * nb_in + j] = lrintf(target);
rem += target - ((int*)s->native_matrix)[i * nb_in + j];
+ sum += FFABS(((int*)s->native_matrix)[i * nb_in + j]);
}
+ maxsum = FFMAX(maxsum, sum);
}
*((int*)s->native_one) = 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);
+ 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);
+ } 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);
+ }
}else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
s->native_matrix = av_calloc(nb_in * nb_out, sizeof(float));
s->native_one = av_mallocz(sizeof(float));
diff --git a/libswresample/rematrix_template.c b/libswresample/rematrix_template.c
index 95a3b9a8c0..add65e3155 100644
--- a/libswresample/rematrix_template.c
+++ b/libswresample/rematrix_template.c
@@ -31,11 +31,16 @@
# define INTER double
# define RENAME(x) x ## _double
#elif defined(TEMPLATE_REMATRIX_S16)
-# define R(x) (((x) + 16384)>>15)
# define SAMPLE int16_t
# define COEFF int
# define INTER int
+# ifdef TEMPLATE_CLIP
+# define R(x) av_clip_int16(((x) + 16384)>>15)
+# define RENAME(x) x ## _clip_s16
+# else
+# define R(x) (((x) + 16384)>>15)
# define RENAME(x) x ## _s16
+# endif
#elif defined(TEMPLATE_REMATRIX_S32)
# define R(x) (((x) + 16384)>>15)
# define SAMPLE int32_t