author Sam Lantinga Sat, 07 Oct 2017 15:26:55 -0700 changeset 11577 a68ad1ddb897 parent 11575 4006cd3d0a73 child 11578 6e8d0e6f4f29
Fixed bug 3857 - SDL_ConvertPixels misses YUV conversions

Sylvain

There are various YUV-RGB conversion coefficients, according to https://www.fourcc.org/fccyvrgb.php
I choose the first (from Video Demystified, with integer multiplication),
but the current SDL2 Dither functions use in fact the next one, which follows a specifications called CCIR 601.

Here's a patch to use the second ones and with previous warning corrections.
There are less multiplications involved because Chroma coefficient is 1.
Also, doing float multiplication is as efficient with vectorization.
In the end, the YUV decoding is faster: ~165 ms vs my previous 195 ms.

Moreover, if SDL2 is compiled with -march=native, then YUV decoding time drops to ~130ms, while older ones remains around ~220 ms.

For information, from jpeg-9 source code:
jpeg-9/jccolor.c

* YCbCr is defined per CCIR 601-1, except that Cb and Cr are
* normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
* The conversion equations to be implemented are therefore
* Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
* Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE
* Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE

jpeg-9/jdcolor.c

* YCbCr is defined per CCIR 601-1, except that Cb and Cr are
* normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
* The conversion equations to be implemented are therefore
*
* R = Y + 1.40200 * Cr
* G = Y - 0.34414 * Cb - 0.71414 * Cr
* B = Y + 1.77200 * Cb
 src/video/SDL_surface.c file | annotate | diff | comparison | revisions
```     1.1 --- a/src/video/SDL_surface.c	Fri Oct 06 21:43:59 2017 -0700
1.2 +++ b/src/video/SDL_surface.c	Sat Oct 07 15:26:55 2017 -0700
1.3 @@ -1176,7 +1176,7 @@
1.4                          dst_pitch = (dst_pitch + 1) / 2;
1.5                          for (i = height * 2; i--;) {
1.6                              SDL_memcpy(dst, src, width);
1.7 -                            src = (Uint8*)src + src_pitch;
1.8 +                            src = (const Uint8*)src + src_pitch;
1.9                              dst = (Uint8*)dst + dst_pitch;
1.10                          }
1.11                      } else if (src_format == SDL_PIXELFORMAT_NV12 || src_format == SDL_PIXELFORMAT_NV21) {
1.12 @@ -1187,7 +1187,7 @@
1.13                          dst_pitch = (dst_pitch + 1) / 2;
1.14                          for (i = height; i--;) {
1.15                              SDL_memcpy(dst, src, 2 * width);
1.16 -                            src = (Uint8*)src + 2 * src_pitch;
1.17 +                            src = (const Uint8*)src + 2 * src_pitch;
1.18                              dst = (Uint8*)dst + 2 * dst_pitch;
1.19                          }
1.20                      }
1.21 @@ -1321,15 +1321,31 @@
1.22  /* YUV-RGB conversion */
1.23  #define CLAMP(val) ((val) > 0 ? ((val) < 255 ? (val) : 255) : 0)
1.24
1.25 +#if 1
1.26 +
1.27 +/* Coefficients from CCIR 601 */
1.28 +#define MAKE_Y(r, g, b) (int)( 0.29900f * (r) + 0.58700f * (g) + 0.11400f * (b))
1.29 +#define MAKE_U(r, g, b) (int)(-0.16874f * (r) - 0.33126f * (g) + 0.50000f * (b) + 128)
1.30 +#define MAKE_V(r, g, b) (int)( 0.50000f * (r) - 0.41869f * (g) - 0.08131f * (b) + 128)
1.31 +
1.32 +#define MAKE_R(y, u, v) CLAMP((int)((y)                          + 1.40200f * ((v) - 128)))
1.33 +#define MAKE_G(y, u, v) CLAMP((int)((y) - 0.34414f * ((u) - 128) - 0.71414f * ((v) - 128)))
1.34 +#define MAKE_B(y, u, v) CLAMP((int)((y) + 1.77200f * ((u) - 128)                         ))
1.35 +
1.36 +#else
1.37 +
1.38 +/* Coefficients from Video Demystified */
1.39  #define MAKE_Y(r, g, b) (((  66 * (r) + 129 * (g) +  25 * (b) + 128) >> 8) + 16)
1.40  #define MAKE_U(r, g, b) ((( -38 * (r) -  74 * (g) + 112 * (b) + 128) >> 8) + 128)
1.41  #define MAKE_V(r, g, b) ((( 112 * (r) -  94 * (g) -  18 * (b) + 128) >> 8) + 128)
1.42
1.43 -
1.44  #define MAKE_R(y, u, v) CLAMP(( 298 * ((y) - 16)                     + 409 * ((v) - 128) + 128) >> 8)
1.45  #define MAKE_G(y, u, v) CLAMP(( 298 * ((y) - 16) - 100 * ((u) - 128) - 208 * ((v) - 128) + 128) >> 8)
1.46  #define MAKE_B(y, u, v) CLAMP(( 298 * ((y) - 16) + 516 * ((u) - 128)                     + 128) >> 8)
1.47
1.48 +#endif
1.49 +
1.50 +
1.51  static int
1.52  SDL_ConvertPixels_YUV_to_ARGB8888(int width, int height,
1.53           Uint32 src_format, const void *src,
1.54 @@ -1465,7 +1481,8 @@
1.55                              WRITE_RGB_PIXEL(y1, u, v);
1.56                          }
1.57                          if (width_remainder) {
1.58 -                            READ_PACKED_YUV(y, u, y1, v); /* y1 unused */
1.59 +                            READ_PACKED_YUV(y, u, y1, v);
1.60 +                            (void)y1; /* y1 unused */
1.61                              WRITE_RGB_PIXEL(y, u, v);
1.62                          }
1.64 @@ -1480,7 +1497,8 @@
1.65                              WRITE_RGB_PIXEL(y1, u, v);
1.66                          }
1.67                          if (width_remainder) {
1.68 -                            READ_PACKED_YUV(u, y, v, y1); /* y1 unused */
1.69 +                            READ_PACKED_YUV(u, y, v, y1);
1.70 +                            (void) y1; /* y1 unused */
1.71                              WRITE_RGB_PIXEL(y, u, v);
1.72                          }
1.74 @@ -1495,7 +1513,8 @@
1.75                              WRITE_RGB_PIXEL(y1, u, v);
1.76                          }
1.77                          if (width_remainder) {
1.78 -                            READ_PACKED_YUV(y, v, y1, u); /* y1 unused */
1.79 +                            READ_PACKED_YUV(y, v, y1, u);
1.80 +                            (void) y1; /* y1 unused */
1.81                              WRITE_RGB_PIXEL(y, u, v);
1.82                          }
1.84 @@ -1557,10 +1576,10 @@
1.85  #if 1
1.86  /* slightly faster */