| 1 | // Copyright 2016 Adrien Descamps |
| 2 | // Distributed under BSD 3-Clause License |
| 3 | #include "SDL_internal.h" |
| 4 | |
| 5 | #ifdef SDL_HAVE_YUV |
| 6 | |
| 7 | #include "yuv_rgb_internal.h" |
| 8 | |
| 9 | // divide by PRECISION_FACTOR and clamp to [0:255] interval |
| 10 | // input must be in the [-128*PRECISION_FACTOR:384*PRECISION_FACTOR] range |
| 11 | static uint8_t clampU8(int32_t v) |
| 12 | { |
| 13 | static const uint8_t lut[512] = |
| 14 | {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 15 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 16 | 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46, |
| 17 | 47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90, |
| 18 | 91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125, |
| 19 | 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158, |
| 20 | 159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, |
| 21 | 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224, |
| 22 | 225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255, |
| 23 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, |
| 24 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, |
| 25 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, |
| 26 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255 |
| 27 | }; |
| 28 | return lut[((v+128*PRECISION_FACTOR)>>PRECISION)&511]; |
| 29 | } |
| 30 | |
| 31 | static uint16_t clamp10(int32_t v) |
| 32 | { |
| 33 | v >>= PRECISION; |
| 34 | if (v < 0) { |
| 35 | return 0; |
| 36 | } else if (v > 1023) { |
| 37 | return 1023; |
| 38 | } else { |
| 39 | return (uint16_t)v; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | #define YUV_BITS 8 |
| 44 | |
| 45 | #define STD_FUNCTION_NAME yuv420_rgb565_std |
| 46 | #define YUV_FORMAT YUV_FORMAT_420 |
| 47 | #define RGB_FORMAT RGB_FORMAT_RGB565 |
| 48 | #include "yuv_rgb_std_func.h" |
| 49 | |
| 50 | #define STD_FUNCTION_NAME yuv420_rgb24_std |
| 51 | #define YUV_FORMAT YUV_FORMAT_420 |
| 52 | #define RGB_FORMAT RGB_FORMAT_RGB24 |
| 53 | #include "yuv_rgb_std_func.h" |
| 54 | |
| 55 | #define STD_FUNCTION_NAME yuv420_rgba_std |
| 56 | #define YUV_FORMAT YUV_FORMAT_420 |
| 57 | #define RGB_FORMAT RGB_FORMAT_RGBA |
| 58 | #include "yuv_rgb_std_func.h" |
| 59 | |
| 60 | #define STD_FUNCTION_NAME yuv420_bgra_std |
| 61 | #define YUV_FORMAT YUV_FORMAT_420 |
| 62 | #define RGB_FORMAT RGB_FORMAT_BGRA |
| 63 | #include "yuv_rgb_std_func.h" |
| 64 | |
| 65 | #define STD_FUNCTION_NAME yuv420_argb_std |
| 66 | #define YUV_FORMAT YUV_FORMAT_420 |
| 67 | #define RGB_FORMAT RGB_FORMAT_ARGB |
| 68 | #include "yuv_rgb_std_func.h" |
| 69 | |
| 70 | #define STD_FUNCTION_NAME yuv420_abgr_std |
| 71 | #define YUV_FORMAT YUV_FORMAT_420 |
| 72 | #define RGB_FORMAT RGB_FORMAT_ABGR |
| 73 | #include "yuv_rgb_std_func.h" |
| 74 | |
| 75 | #define STD_FUNCTION_NAME yuv422_rgb565_std |
| 76 | #define YUV_FORMAT YUV_FORMAT_422 |
| 77 | #define RGB_FORMAT RGB_FORMAT_RGB565 |
| 78 | #include "yuv_rgb_std_func.h" |
| 79 | |
| 80 | #define STD_FUNCTION_NAME yuv422_rgb24_std |
| 81 | #define YUV_FORMAT YUV_FORMAT_422 |
| 82 | #define RGB_FORMAT RGB_FORMAT_RGB24 |
| 83 | #include "yuv_rgb_std_func.h" |
| 84 | |
| 85 | #define STD_FUNCTION_NAME yuv422_rgba_std |
| 86 | #define YUV_FORMAT YUV_FORMAT_422 |
| 87 | #define RGB_FORMAT RGB_FORMAT_RGBA |
| 88 | #include "yuv_rgb_std_func.h" |
| 89 | |
| 90 | #define STD_FUNCTION_NAME yuv422_bgra_std |
| 91 | #define YUV_FORMAT YUV_FORMAT_422 |
| 92 | #define RGB_FORMAT RGB_FORMAT_BGRA |
| 93 | #include "yuv_rgb_std_func.h" |
| 94 | |
| 95 | #define STD_FUNCTION_NAME yuv422_argb_std |
| 96 | #define YUV_FORMAT YUV_FORMAT_422 |
| 97 | #define RGB_FORMAT RGB_FORMAT_ARGB |
| 98 | #include "yuv_rgb_std_func.h" |
| 99 | |
| 100 | #define STD_FUNCTION_NAME yuv422_abgr_std |
| 101 | #define YUV_FORMAT YUV_FORMAT_422 |
| 102 | #define RGB_FORMAT RGB_FORMAT_ABGR |
| 103 | #include "yuv_rgb_std_func.h" |
| 104 | |
| 105 | #define STD_FUNCTION_NAME yuvnv12_rgb565_std |
| 106 | #define YUV_FORMAT YUV_FORMAT_NV12 |
| 107 | #define RGB_FORMAT RGB_FORMAT_RGB565 |
| 108 | #include "yuv_rgb_std_func.h" |
| 109 | |
| 110 | #define STD_FUNCTION_NAME yuvnv12_rgb24_std |
| 111 | #define YUV_FORMAT YUV_FORMAT_NV12 |
| 112 | #define RGB_FORMAT RGB_FORMAT_RGB24 |
| 113 | #include "yuv_rgb_std_func.h" |
| 114 | |
| 115 | #define STD_FUNCTION_NAME yuvnv12_rgba_std |
| 116 | #define YUV_FORMAT YUV_FORMAT_NV12 |
| 117 | #define RGB_FORMAT RGB_FORMAT_RGBA |
| 118 | #include "yuv_rgb_std_func.h" |
| 119 | |
| 120 | #define STD_FUNCTION_NAME yuvnv12_bgra_std |
| 121 | #define YUV_FORMAT YUV_FORMAT_NV12 |
| 122 | #define RGB_FORMAT RGB_FORMAT_BGRA |
| 123 | #include "yuv_rgb_std_func.h" |
| 124 | |
| 125 | #define STD_FUNCTION_NAME yuvnv12_argb_std |
| 126 | #define YUV_FORMAT YUV_FORMAT_NV12 |
| 127 | #define RGB_FORMAT RGB_FORMAT_ARGB |
| 128 | #include "yuv_rgb_std_func.h" |
| 129 | |
| 130 | #define STD_FUNCTION_NAME yuvnv12_abgr_std |
| 131 | #define YUV_FORMAT YUV_FORMAT_NV12 |
| 132 | #define RGB_FORMAT RGB_FORMAT_ABGR |
| 133 | #include "yuv_rgb_std_func.h" |
| 134 | |
| 135 | #undef YUV_BITS |
| 136 | #define YUV_BITS 10 |
| 137 | |
| 138 | #define STD_FUNCTION_NAME yuvp010_xbgr2101010_std |
| 139 | #define YUV_FORMAT YUV_FORMAT_NV12 |
| 140 | #define RGB_FORMAT RGB_FORMAT_XBGR2101010 |
| 141 | #include "yuv_rgb_std_func.h" |
| 142 | |
| 143 | void rgb24_yuv420_std( |
| 144 | uint32_t width, uint32_t height, |
| 145 | const uint8_t *RGB, uint32_t RGB_stride, |
| 146 | uint8_t *Y, uint8_t *U, uint8_t *V, uint32_t Y_stride, uint32_t UV_stride, |
| 147 | YCbCrType yuv_type) |
| 148 | { |
| 149 | const RGB2YUVParam *const param = &(RGB2YUV[yuv_type]); |
| 150 | |
| 151 | uint32_t x, y; |
| 152 | for(y=0; y<(height-1); y+=2) |
| 153 | { |
| 154 | const uint8_t *rgb_ptr1=RGB+y*RGB_stride, |
| 155 | *rgb_ptr2=RGB+(y+1)*RGB_stride; |
| 156 | |
| 157 | uint8_t *y_ptr1=Y+y*Y_stride, |
| 158 | *y_ptr2=Y+(y+1)*Y_stride, |
| 159 | *u_ptr=U+(y/2)*UV_stride, |
| 160 | *v_ptr=V+(y/2)*UV_stride; |
| 161 | |
| 162 | for(x=0; x<(width-1); x+=2) |
| 163 | { |
| 164 | // compute yuv for the four pixels, u and v values are summed |
| 165 | int32_t y_tmp, u_tmp, v_tmp; |
| 166 | |
| 167 | y_tmp = param->matrix[0][0]*rgb_ptr1[0] + param->matrix[0][1]*rgb_ptr1[1] + param->matrix[0][2]*rgb_ptr1[2]; |
| 168 | u_tmp = param->matrix[1][0]*rgb_ptr1[0] + param->matrix[1][1]*rgb_ptr1[1] + param->matrix[1][2]*rgb_ptr1[2]; |
| 169 | v_tmp = param->matrix[2][0]*rgb_ptr1[0] + param->matrix[2][1]*rgb_ptr1[1] + param->matrix[2][2]*rgb_ptr1[2]; |
| 170 | y_ptr1[0]=clampU8(y_tmp+((param->y_shift)<<PRECISION)); |
| 171 | |
| 172 | y_tmp = param->matrix[0][0]*rgb_ptr1[3] + param->matrix[0][1]*rgb_ptr1[4] + param->matrix[0][2]*rgb_ptr1[5]; |
| 173 | u_tmp += param->matrix[1][0]*rgb_ptr1[3] + param->matrix[1][1]*rgb_ptr1[4] + param->matrix[1][2]*rgb_ptr1[5]; |
| 174 | v_tmp += param->matrix[2][0]*rgb_ptr1[3] + param->matrix[2][1]*rgb_ptr1[4] + param->matrix[2][2]*rgb_ptr1[5]; |
| 175 | y_ptr1[1]=clampU8(y_tmp+((param->y_shift)<<PRECISION)); |
| 176 | |
| 177 | y_tmp = param->matrix[0][0]*rgb_ptr2[0] + param->matrix[0][1]*rgb_ptr2[1] + param->matrix[0][2]*rgb_ptr2[2]; |
| 178 | u_tmp += param->matrix[1][0]*rgb_ptr2[0] + param->matrix[1][1]*rgb_ptr2[1] + param->matrix[1][2]*rgb_ptr2[2]; |
| 179 | v_tmp += param->matrix[2][0]*rgb_ptr2[0] + param->matrix[2][1]*rgb_ptr2[1] + param->matrix[2][2]*rgb_ptr2[2]; |
| 180 | y_ptr2[0]=clampU8(y_tmp+((param->y_shift)<<PRECISION)); |
| 181 | |
| 182 | y_tmp = param->matrix[0][0]*rgb_ptr2[3] + param->matrix[0][1]*rgb_ptr2[4] + param->matrix[0][2]*rgb_ptr2[5]; |
| 183 | u_tmp += param->matrix[1][0]*rgb_ptr2[3] + param->matrix[1][1]*rgb_ptr2[4] + param->matrix[1][2]*rgb_ptr2[5]; |
| 184 | v_tmp += param->matrix[2][0]*rgb_ptr2[3] + param->matrix[2][1]*rgb_ptr2[4] + param->matrix[2][2]*rgb_ptr2[5]; |
| 185 | y_ptr2[1]=clampU8(y_tmp+((param->y_shift)<<PRECISION)); |
| 186 | |
| 187 | u_ptr[0] = clampU8(u_tmp/4+(128<<PRECISION)); |
| 188 | v_ptr[0] = clampU8(v_tmp/4+(128<<PRECISION)); |
| 189 | |
| 190 | rgb_ptr1 += 6; |
| 191 | rgb_ptr2 += 6; |
| 192 | y_ptr1 += 2; |
| 193 | y_ptr2 += 2; |
| 194 | u_ptr += 1; |
| 195 | v_ptr += 1; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | #endif /* SDL_HAVE_YUV */ |
| 201 | |