Skip to content

Commit 2d1358a

Browse files
committed
swscale/range_convert: saturate output instead of limiting input
For bit depths <= 14, the result is saturated to 15 bits. For bit depths > 14, the result is saturated to 19 bits. x86_64: chrRangeFromJpeg8_1920_c: 2126.5 2127.4 (1.00x) chrRangeFromJpeg16_1920_c: 2331.4 2325.2 (1.00x) chrRangeToJpeg8_1920_c: 3163.0 3166.9 (1.00x) chrRangeToJpeg16_1920_c: 3163.7 2152.4 (1.47x) lumRangeFromJpeg8_1920_c: 1262.2 1263.0 (1.00x) lumRangeFromJpeg16_1920_c: 1079.5 1080.5 (1.00x) lumRangeToJpeg8_1920_c: 1860.5 1886.8 (0.99x) lumRangeToJpeg16_1920_c: 1910.2 1077.0 (1.77x) aarch64 A55: chrRangeFromJpeg8_1920_c: 28836.2 28835.2 (1.00x) chrRangeFromJpeg16_1920_c: 28840.1 28839.8 (1.00x) chrRangeToJpeg8_1920_c: 44196.2 23074.7 (1.92x) chrRangeToJpeg16_1920_c: 36527.3 17318.9 (2.11x) lumRangeFromJpeg8_1920_c: 15388.5 15389.7 (1.00x) lumRangeFromJpeg16_1920_c: 15389.3 15388.2 (1.00x) lumRangeToJpeg8_1920_c: 23069.7 19227.8 (1.20x) lumRangeToJpeg16_1920_c: 19227.8 15387.0 (1.25x) aarch64 A76: chrRangeFromJpeg8_1920_c: 6334.7 6324.4 (1.00x) chrRangeFromJpeg16_1920_c: 6336.0 6339.9 (1.00x) chrRangeToJpeg8_1920_c: 11474.5 9656.0 (1.19x) chrRangeToJpeg16_1920_c: 9640.5 6340.4 (1.52x) lumRangeFromJpeg8_1920_c: 4453.2 4422.0 (1.01x) lumRangeFromJpeg16_1920_c: 4414.2 4420.9 (1.00x) lumRangeToJpeg8_1920_c: 6645.0 5949.1 (1.12x) lumRangeToJpeg16_1920_c: 6005.2 4446.8 (1.35x) NOTE: all simd optimizations for range_convert have been disabled except for x86, which already had the same behaviour. they will be re-enabled when they are fixed for each architecture.
1 parent 536a44e commit 2d1358a

File tree

5 files changed

+29
-10
lines changed

5 files changed

+29
-10
lines changed

libswscale/aarch64/swscale.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ void ff_chrRangeToJpeg_neon(int16_t *dstU, int16_t *dstV, int width);
225225

226226
av_cold void ff_sws_init_range_convert_aarch64(SwsInternal *c)
227227
{
228+
/* This code is currently disabled because of changes in the base
229+
* implementation of these functions. This code should be enabled
230+
* again once those changes are ported to this architecture. */
231+
#if 0
228232
int cpu_flags = av_get_cpu_flags();
229233

230234
if (have_neon(cpu_flags)) {
@@ -238,6 +242,7 @@ av_cold void ff_sws_init_range_convert_aarch64(SwsInternal *c)
238242
}
239243
}
240244
}
245+
#endif
241246
}
242247

243248
av_cold void ff_sws_init_swscale_aarch64(SwsInternal *c)

libswscale/loongarch/swscale_init_loongarch.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626

2727
av_cold void ff_sws_init_range_convert_loongarch(SwsInternal *c)
2828
{
29+
/* This code is currently disabled because of changes in the base
30+
* implementation of these functions. This code should be enabled
31+
* again once those changes are ported to this architecture. */
32+
#if 0
2933
int cpu_flags = av_get_cpu_flags();
3034

3135
if (have_lsx(cpu_flags)) {
@@ -52,6 +56,7 @@ av_cold void ff_sws_init_range_convert_loongarch(SwsInternal *c)
5256
}
5357
}
5458
#endif // #if HAVE_LASX
59+
#endif
5560
}
5661

5762
av_cold void ff_sws_init_swscale_loongarch(SwsInternal *c)

libswscale/riscv/swscale.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ void ff_range_chr_from_jpeg_16_rvv(int16_t *, int16_t *, int);
2828

2929
av_cold void ff_sws_init_range_convert_riscv(SwsInternal *c)
3030
{
31+
/* This code is currently disabled because of changes in the base
32+
* implementation of these functions. This code should be enabled
33+
* again once those changes are ported to this architecture. */
34+
#if 0
3135
#if HAVE_RVV
3236
int flags = av_get_cpu_flags();
3337

@@ -47,6 +51,7 @@ av_cold void ff_sws_init_range_convert_riscv(SwsInternal *c)
4751
c->chrConvertRange = convs[from].chr;
4852
}
4953
#endif
54+
#endif
5055
}
5156

5257
#define RVV_INPUT(name) \

libswscale/swscale.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,10 @@ static void chrRangeToJpeg_c(int16_t *dstU, int16_t *dstV, int width)
160160
{
161161
int i;
162162
for (i = 0; i < width; i++) {
163-
dstU[i] = (FFMIN(dstU[i], 30775) * 4663 - 9289992) >> 12; // -264
164-
dstV[i] = (FFMIN(dstV[i], 30775) * 4663 - 9289992) >> 12; // -264
163+
int U = (dstU[i] * 4663 - 9289992) >> 12; // -264
164+
int V = (dstV[i] * 4663 - 9289992) >> 12; // -264
165+
dstU[i] = FFMIN(U, (1 << 15) - 1);
166+
dstV[i] = FFMIN(V, (1 << 15) - 1);
165167
}
166168
}
167169

@@ -177,8 +179,10 @@ static void chrRangeFromJpeg_c(int16_t *dstU, int16_t *dstV, int width)
177179
static void lumRangeToJpeg_c(int16_t *dst, int width)
178180
{
179181
int i;
180-
for (i = 0; i < width; i++)
181-
dst[i] = (FFMIN(dst[i], 30189) * 19077 - 39057361) >> 14;
182+
for (i = 0; i < width; i++) {
183+
int Y = (dst[i] * 19077 - 39057361) >> 14;
184+
dst[i] = FFMIN(Y, (1 << 15) - 1);
185+
}
182186
}
183187

184188
static void lumRangeFromJpeg_c(int16_t *dst, int width)
@@ -194,8 +198,10 @@ static void chrRangeToJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
194198
int32_t *dstU = (int32_t *) _dstU;
195199
int32_t *dstV = (int32_t *) _dstV;
196200
for (i = 0; i < width; i++) {
197-
dstU[i] = ((int)(FFMIN(dstU[i], 30775 << 4) * 4663U - (9289992 << 4))) >> 12; // -264
198-
dstV[i] = ((int)(FFMIN(dstV[i], 30775 << 4) * 4663U - (9289992 << 4))) >> 12; // -264
201+
int U = ((int)(dstU[i] * 4663U - (9289992 << 4))) >> 12; // -264
202+
int V = ((int)(dstV[i] * 4663U - (9289992 << 4))) >> 12; // -264
203+
dstU[i] = FFMIN(U, (1 << 19) - 1);
204+
dstV[i] = FFMIN(V, (1 << 19) - 1);
199205
}
200206
}
201207

@@ -215,7 +221,8 @@ static void lumRangeToJpeg16_c(int16_t *_dst, int width)
215221
int i;
216222
int32_t *dst = (int32_t *) _dst;
217223
for (i = 0; i < width; i++) {
218-
dst[i] = ((int)(FFMIN(dst[i], 30189 << 4) * 4769U - (39057361 << 2))) >> 12;
224+
int Y = ((int)(dst[i] * 4769U - (39057361 << 2))) >> 12;
225+
dst[i] = FFMIN(Y, (1 << 19) - 1);
219226
}
220227
}
221228

libswscale/x86/range_convert.asm

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ lum_from_offset: times 4 dd 33561947
4040

4141
SECTION .text
4242

43-
; NOTE: there is no need to clamp the input when converting to jpeg range
44-
; (like we do in the C code) because packssdw will saturate the output.
45-
4643
;-----------------------------------------------------------------------------
4744
; lumConvertRange
4845
;

0 commit comments

Comments
 (0)