00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <inttypes.h>
00029 #include <assert.h>
00030
00031 #include "libavutil/cpu.h"
00032 #include "libavutil/bswap.h"
00033 #include "config.h"
00034 #include "rgb2rgb.h"
00035 #include "swscale.h"
00036 #include "swscale_internal.h"
00037 #include "libavutil/pixdesc.h"
00038
00039 extern const uint8_t dither_4x4_16[4][8];
00040 extern const uint8_t dither_8x8_32[8][8];
00041 extern const uint8_t dither_8x8_73[8][8];
00042 extern const uint8_t dither_8x8_220[8][8];
00043
00044 const int32_t ff_yuv2rgb_coeffs[8][4] = {
00045 { 117504, 138453, 13954, 34903 },
00046 { 117504, 138453, 13954, 34903 },
00047 { 104597, 132201, 25675, 53279 },
00048 { 104597, 132201, 25675, 53279 },
00049 { 104448, 132798, 24759, 53109 },
00050 { 104597, 132201, 25675, 53279 },
00051 { 104597, 132201, 25675, 53279 },
00052 { 117579, 136230, 16907, 35559 }
00053 };
00054
00055 const int *sws_getCoefficients(int colorspace)
00056 {
00057 if (colorspace > 7 || colorspace < 0)
00058 colorspace = SWS_CS_DEFAULT;
00059 return ff_yuv2rgb_coeffs[colorspace];
00060 }
00061
00062 #define LOADCHROMA(i) \
00063 U = pu[i]; \
00064 V = pv[i]; \
00065 r = (void *)c->table_rV[V+YUVRGB_TABLE_HEADROOM]; \
00066 g = (void *)(c->table_gU[U+YUVRGB_TABLE_HEADROOM] + c->table_gV[V+YUVRGB_TABLE_HEADROOM]); \
00067 b = (void *)c->table_bU[U+YUVRGB_TABLE_HEADROOM];
00068
00069 #define PUTRGB(dst, src, i) \
00070 Y = src[2 * i]; \
00071 dst[2 * i] = r[Y] + g[Y] + b[Y]; \
00072 Y = src[2 * i + 1]; \
00073 dst[2 * i + 1] = r[Y] + g[Y] + b[Y];
00074
00075 #define PUTRGB24(dst, src, i) \
00076 Y = src[2 * i]; \
00077 dst[6 * i + 0] = r[Y]; \
00078 dst[6 * i + 1] = g[Y]; \
00079 dst[6 * i + 2] = b[Y]; \
00080 Y = src[2 * i + 1]; \
00081 dst[6 * i + 3] = r[Y]; \
00082 dst[6 * i + 4] = g[Y]; \
00083 dst[6 * i + 5] = b[Y];
00084
00085 #define PUTBGR24(dst, src, i) \
00086 Y = src[2 * i]; \
00087 dst[6 * i + 0] = b[Y]; \
00088 dst[6 * i + 1] = g[Y]; \
00089 dst[6 * i + 2] = r[Y]; \
00090 Y = src[2 * i + 1]; \
00091 dst[6 * i + 3] = b[Y]; \
00092 dst[6 * i + 4] = g[Y]; \
00093 dst[6 * i + 5] = r[Y];
00094
00095 #define PUTRGBA(dst, ysrc, asrc, i, s) \
00096 Y = ysrc[2 * i]; \
00097 dst[2 * i] = r[Y] + g[Y] + b[Y] + (asrc[2 * i] << s); \
00098 Y = ysrc[2 * i + 1]; \
00099 dst[2 * i + 1] = r[Y] + g[Y] + b[Y] + (asrc[2 * i + 1] << s);
00100
00101 #define PUTRGB48(dst, src, i) \
00102 Y = src[ 2 * i]; \
00103 dst[12 * i + 0] = dst[12 * i + 1] = r[Y]; \
00104 dst[12 * i + 2] = dst[12 * i + 3] = g[Y]; \
00105 dst[12 * i + 4] = dst[12 * i + 5] = b[Y]; \
00106 Y = src[ 2 * i + 1]; \
00107 dst[12 * i + 6] = dst[12 * i + 7] = r[Y]; \
00108 dst[12 * i + 8] = dst[12 * i + 9] = g[Y]; \
00109 dst[12 * i + 10] = dst[12 * i + 11] = b[Y];
00110
00111 #define PUTBGR48(dst, src, i) \
00112 Y = src[2 * i]; \
00113 dst[12 * i + 0] = dst[12 * i + 1] = b[Y]; \
00114 dst[12 * i + 2] = dst[12 * i + 3] = g[Y]; \
00115 dst[12 * i + 4] = dst[12 * i + 5] = r[Y]; \
00116 Y = src[2 * i + 1]; \
00117 dst[12 * i + 6] = dst[12 * i + 7] = b[Y]; \
00118 dst[12 * i + 8] = dst[12 * i + 9] = g[Y]; \
00119 dst[12 * i + 10] = dst[12 * i + 11] = r[Y];
00120
00121 #define YUV2RGBFUNC(func_name, dst_type, alpha) \
00122 static int func_name(SwsContext *c, const uint8_t *src[], \
00123 int srcStride[], int srcSliceY, int srcSliceH, \
00124 uint8_t *dst[], int dstStride[]) \
00125 { \
00126 int y; \
00127 \
00128 if (!alpha && c->srcFormat == PIX_FMT_YUV422P) { \
00129 srcStride[1] *= 2; \
00130 srcStride[2] *= 2; \
00131 } \
00132 for (y = 0; y < srcSliceH; y += 2) { \
00133 dst_type *dst_1 = \
00134 (dst_type *)(dst[0] + (y + srcSliceY) * dstStride[0]); \
00135 dst_type *dst_2 = \
00136 (dst_type *)(dst[0] + (y + srcSliceY + 1) * dstStride[0]); \
00137 dst_type av_unused *r, *g, *b; \
00138 const uint8_t *py_1 = src[0] + y * srcStride[0]; \
00139 const uint8_t *py_2 = py_1 + srcStride[0]; \
00140 const uint8_t *pu = src[1] + (y >> 1) * srcStride[1]; \
00141 const uint8_t *pv = src[2] + (y >> 1) * srcStride[2]; \
00142 const uint8_t av_unused *pa_1, *pa_2; \
00143 unsigned int h_size = c->dstW >> 3; \
00144 if (alpha) { \
00145 pa_1 = src[3] + y * srcStride[3]; \
00146 pa_2 = pa_1 + srcStride[3]; \
00147 } \
00148 while (h_size--) { \
00149 int av_unused U, V, Y; \
00150
00151 #define ENDYUV2RGBLINE(dst_delta) \
00152 pu += 4; \
00153 pv += 4; \
00154 py_1 += 8; \
00155 py_2 += 8; \
00156 dst_1 += dst_delta; \
00157 dst_2 += dst_delta; \
00158 } \
00159 if (c->dstW & 4) { \
00160 int av_unused Y, U, V; \
00161
00162 #define ENDYUV2RGBFUNC() \
00163 } \
00164 } \
00165 return srcSliceH; \
00166 }
00167
00168 #define CLOSEYUV2RGBFUNC(dst_delta) \
00169 ENDYUV2RGBLINE(dst_delta) \
00170 ENDYUV2RGBFUNC()
00171
00172 YUV2RGBFUNC(yuv2rgb_c_48, uint8_t, 0)
00173 LOADCHROMA(0);
00174 PUTRGB48(dst_1, py_1, 0);
00175 PUTRGB48(dst_2, py_2, 0);
00176
00177 LOADCHROMA(1);
00178 PUTRGB48(dst_2, py_2, 1);
00179 PUTRGB48(dst_1, py_1, 1);
00180
00181 LOADCHROMA(2);
00182 PUTRGB48(dst_1, py_1, 2);
00183 PUTRGB48(dst_2, py_2, 2);
00184
00185 LOADCHROMA(3);
00186 PUTRGB48(dst_2, py_2, 3);
00187 PUTRGB48(dst_1, py_1, 3);
00188 ENDYUV2RGBLINE(48)
00189 LOADCHROMA(0);
00190 PUTRGB48(dst_1, py_1, 0);
00191 PUTRGB48(dst_2, py_2, 0);
00192
00193 LOADCHROMA(1);
00194 PUTRGB48(dst_2, py_2, 1);
00195 PUTRGB48(dst_1, py_1, 1);
00196 ENDYUV2RGBFUNC()
00197
00198 YUV2RGBFUNC(yuv2rgb_c_bgr48, uint8_t, 0)
00199 LOADCHROMA(0);
00200 PUTBGR48(dst_1, py_1, 0);
00201 PUTBGR48(dst_2, py_2, 0);
00202
00203 LOADCHROMA(1);
00204 PUTBGR48(dst_2, py_2, 1);
00205 PUTBGR48(dst_1, py_1, 1);
00206
00207 LOADCHROMA(2);
00208 PUTBGR48(dst_1, py_1, 2);
00209 PUTBGR48(dst_2, py_2, 2);
00210
00211 LOADCHROMA(3);
00212 PUTBGR48(dst_2, py_2, 3);
00213 PUTBGR48(dst_1, py_1, 3);
00214 ENDYUV2RGBLINE(48)
00215 LOADCHROMA(0);
00216 PUTBGR48(dst_1, py_1, 0);
00217 PUTBGR48(dst_2, py_2, 0);
00218
00219 LOADCHROMA(1);
00220 PUTBGR48(dst_2, py_2, 1);
00221 PUTBGR48(dst_1, py_1, 1);
00222 ENDYUV2RGBFUNC()
00223
00224 YUV2RGBFUNC(yuv2rgb_c_32, uint32_t, 0)
00225 LOADCHROMA(0);
00226 PUTRGB(dst_1, py_1, 0);
00227 PUTRGB(dst_2, py_2, 0);
00228
00229 LOADCHROMA(1);
00230 PUTRGB(dst_2, py_2, 1);
00231 PUTRGB(dst_1, py_1, 1);
00232
00233 LOADCHROMA(2);
00234 PUTRGB(dst_1, py_1, 2);
00235 PUTRGB(dst_2, py_2, 2);
00236
00237 LOADCHROMA(3);
00238 PUTRGB(dst_2, py_2, 3);
00239 PUTRGB(dst_1, py_1, 3);
00240 ENDYUV2RGBLINE(8)
00241 LOADCHROMA(0);
00242 PUTRGB(dst_1, py_1, 0);
00243 PUTRGB(dst_2, py_2, 0);
00244
00245 LOADCHROMA(1);
00246 PUTRGB(dst_2, py_2, 1);
00247 PUTRGB(dst_1, py_1, 1);
00248 ENDYUV2RGBFUNC()
00249
00250 YUV2RGBFUNC(yuva2rgba_c, uint32_t, 1)
00251 LOADCHROMA(0);
00252 PUTRGBA(dst_1, py_1, pa_1, 0, 24);
00253 PUTRGBA(dst_2, py_2, pa_2, 0, 24);
00254
00255 LOADCHROMA(1);
00256 PUTRGBA(dst_2, py_2, pa_1, 1, 24);
00257 PUTRGBA(dst_1, py_1, pa_2, 1, 24);
00258
00259 LOADCHROMA(2);
00260 PUTRGBA(dst_1, py_1, pa_1, 2, 24);
00261 PUTRGBA(dst_2, py_2, pa_2, 2, 24);
00262
00263 LOADCHROMA(3);
00264 PUTRGBA(dst_2, py_2, pa_1, 3, 24);
00265 PUTRGBA(dst_1, py_1, pa_2, 3, 24);
00266 pa_1 += 8; \
00267 pa_2 += 8; \
00268 ENDYUV2RGBLINE(8)
00269 LOADCHROMA(0);
00270 PUTRGBA(dst_1, py_1, pa_1, 0, 24);
00271 PUTRGBA(dst_2, py_2, pa_2, 0, 24);
00272
00273 LOADCHROMA(1);
00274 PUTRGBA(dst_2, py_2, pa_1, 1, 24);
00275 PUTRGBA(dst_1, py_1, pa_2, 1, 24);
00276 ENDYUV2RGBFUNC()
00277
00278 YUV2RGBFUNC(yuva2argb_c, uint32_t, 1)
00279 LOADCHROMA(0);
00280 PUTRGBA(dst_1, py_1, pa_1, 0, 0);
00281 PUTRGBA(dst_2, py_2, pa_2, 0, 0);
00282
00283 LOADCHROMA(1);
00284 PUTRGBA(dst_2, py_2, pa_2, 1, 0);
00285 PUTRGBA(dst_1, py_1, pa_1, 1, 0);
00286
00287 LOADCHROMA(2);
00288 PUTRGBA(dst_1, py_1, pa_1, 2, 0);
00289 PUTRGBA(dst_2, py_2, pa_2, 2, 0);
00290
00291 LOADCHROMA(3);
00292 PUTRGBA(dst_2, py_2, pa_2, 3, 0);
00293 PUTRGBA(dst_1, py_1, pa_1, 3, 0);
00294 pa_1 += 8; \
00295 pa_2 += 8; \
00296 ENDYUV2RGBLINE(8)
00297 LOADCHROMA(0);
00298 PUTRGBA(dst_1, py_1, pa_1, 0, 0);
00299 PUTRGBA(dst_2, py_2, pa_2, 0, 0);
00300
00301 LOADCHROMA(1);
00302 PUTRGBA(dst_2, py_2, pa_2, 1, 0);
00303 PUTRGBA(dst_1, py_1, pa_1, 1, 0);
00304 ENDYUV2RGBFUNC()
00305
00306 YUV2RGBFUNC(yuv2rgb_c_24_rgb, uint8_t, 0)
00307 LOADCHROMA(0);
00308 PUTRGB24(dst_1, py_1, 0);
00309 PUTRGB24(dst_2, py_2, 0);
00310
00311 LOADCHROMA(1);
00312 PUTRGB24(dst_2, py_2, 1);
00313 PUTRGB24(dst_1, py_1, 1);
00314
00315 LOADCHROMA(2);
00316 PUTRGB24(dst_1, py_1, 2);
00317 PUTRGB24(dst_2, py_2, 2);
00318
00319 LOADCHROMA(3);
00320 PUTRGB24(dst_2, py_2, 3);
00321 PUTRGB24(dst_1, py_1, 3);
00322 ENDYUV2RGBLINE(24)
00323 LOADCHROMA(0);
00324 PUTRGB24(dst_1, py_1, 0);
00325 PUTRGB24(dst_2, py_2, 0);
00326
00327 LOADCHROMA(1);
00328 PUTRGB24(dst_2, py_2, 1);
00329 PUTRGB24(dst_1, py_1, 1);
00330 ENDYUV2RGBFUNC()
00331
00332
00333 YUV2RGBFUNC(yuv2rgb_c_24_bgr, uint8_t, 0)
00334 LOADCHROMA(0);
00335 PUTBGR24(dst_1, py_1, 0);
00336 PUTBGR24(dst_2, py_2, 0);
00337
00338 LOADCHROMA(1);
00339 PUTBGR24(dst_2, py_2, 1);
00340 PUTBGR24(dst_1, py_1, 1);
00341
00342 LOADCHROMA(2);
00343 PUTBGR24(dst_1, py_1, 2);
00344 PUTBGR24(dst_2, py_2, 2);
00345
00346 LOADCHROMA(3);
00347 PUTBGR24(dst_2, py_2, 3);
00348 PUTBGR24(dst_1, py_1, 3);
00349 ENDYUV2RGBLINE(24)
00350 LOADCHROMA(0);
00351 PUTBGR24(dst_1, py_1, 0);
00352 PUTBGR24(dst_2, py_2, 0);
00353
00354 LOADCHROMA(1);
00355 PUTBGR24(dst_2, py_2, 1);
00356 PUTBGR24(dst_1, py_1, 1);
00357 ENDYUV2RGBFUNC()
00358
00359
00360
00361 YUV2RGBFUNC(yuv2rgb_c_16, uint16_t, 0)
00362 LOADCHROMA(0);
00363 PUTRGB(dst_1, py_1, 0);
00364 PUTRGB(dst_2, py_2, 0);
00365
00366 LOADCHROMA(1);
00367 PUTRGB(dst_2, py_2, 1);
00368 PUTRGB(dst_1, py_1, 1);
00369
00370 LOADCHROMA(2);
00371 PUTRGB(dst_1, py_1, 2);
00372 PUTRGB(dst_2, py_2, 2);
00373
00374 LOADCHROMA(3);
00375 PUTRGB(dst_2, py_2, 3);
00376 PUTRGB(dst_1, py_1, 3);
00377 CLOSEYUV2RGBFUNC(8)
00378
00379
00380 YUV2RGBFUNC(yuv2rgb_c_12_ordered_dither, uint16_t, 0)
00381 const uint8_t *d16 = dither_4x4_16[y & 3];
00382
00383 #define PUTRGB12(dst, src, i, o) \
00384 Y = src[2 * i]; \
00385 dst[2 * i] = r[Y + d16[0 + o]] + \
00386 g[Y + d16[0 + o]] + \
00387 b[Y + d16[0 + o]]; \
00388 Y = src[2 * i + 1]; \
00389 dst[2 * i + 1] = r[Y + d16[1 + o]] + \
00390 g[Y + d16[1 + o]] + \
00391 b[Y + d16[1 + o]];
00392
00393 LOADCHROMA(0);
00394 PUTRGB12(dst_1, py_1, 0, 0);
00395 PUTRGB12(dst_2, py_2, 0, 0 + 8);
00396
00397 LOADCHROMA(1);
00398 PUTRGB12(dst_2, py_2, 1, 2 + 8);
00399 PUTRGB12(dst_1, py_1, 1, 2);
00400
00401 LOADCHROMA(2);
00402 PUTRGB12(dst_1, py_1, 2, 4);
00403 PUTRGB12(dst_2, py_2, 2, 4 + 8);
00404
00405 LOADCHROMA(3);
00406 PUTRGB12(dst_2, py_2, 3, 6 + 8);
00407 PUTRGB12(dst_1, py_1, 3, 6);
00408 CLOSEYUV2RGBFUNC(8)
00409
00410
00411 YUV2RGBFUNC(yuv2rgb_c_8_ordered_dither, uint8_t, 0)
00412 const uint8_t *d32 = dither_8x8_32[y & 7];
00413 const uint8_t *d64 = dither_8x8_73[y & 7];
00414
00415 #define PUTRGB8(dst, src, i, o) \
00416 Y = src[2 * i]; \
00417 dst[2 * i] = r[Y + d32[0 + o]] + \
00418 g[Y + d32[0 + o]] + \
00419 b[Y + d64[0 + o]]; \
00420 Y = src[2 * i + 1]; \
00421 dst[2 * i + 1] = r[Y + d32[1 + o]] + \
00422 g[Y + d32[1 + o]] + \
00423 b[Y + d64[1 + o]];
00424
00425 LOADCHROMA(0);
00426 PUTRGB8(dst_1, py_1, 0, 0);
00427 PUTRGB8(dst_2, py_2, 0, 0 + 8);
00428
00429 LOADCHROMA(1);
00430 PUTRGB8(dst_2, py_2, 1, 2 + 8);
00431 PUTRGB8(dst_1, py_1, 1, 2);
00432
00433 LOADCHROMA(2);
00434 PUTRGB8(dst_1, py_1, 2, 4);
00435 PUTRGB8(dst_2, py_2, 2, 4 + 8);
00436
00437 LOADCHROMA(3);
00438 PUTRGB8(dst_2, py_2, 3, 6 + 8);
00439 PUTRGB8(dst_1, py_1, 3, 6);
00440 CLOSEYUV2RGBFUNC(8)
00441
00442 YUV2RGBFUNC(yuv2rgb_c_4_ordered_dither, uint8_t, 0)
00443 const uint8_t * d64 = dither_8x8_73[y & 7];
00444 const uint8_t *d128 = dither_8x8_220[y & 7];
00445 int acc;
00446
00447 #define PUTRGB4D(dst, src, i, o) \
00448 Y = src[2 * i]; \
00449 acc = r[Y + d128[0 + o]] + \
00450 g[Y + d64[0 + o]] + \
00451 b[Y + d128[0 + o]]; \
00452 Y = src[2 * i + 1]; \
00453 acc |= (r[Y + d128[1 + o]] + \
00454 g[Y + d64[1 + o]] + \
00455 b[Y + d128[1 + o]]) << 4; \
00456 dst[i] = acc;
00457
00458 LOADCHROMA(0);
00459 PUTRGB4D(dst_1, py_1, 0, 0);
00460 PUTRGB4D(dst_2, py_2, 0, 0 + 8);
00461
00462 LOADCHROMA(1);
00463 PUTRGB4D(dst_2, py_2, 1, 2 + 8);
00464 PUTRGB4D(dst_1, py_1, 1, 2);
00465
00466 LOADCHROMA(2);
00467 PUTRGB4D(dst_1, py_1, 2, 4);
00468 PUTRGB4D(dst_2, py_2, 2, 4 + 8);
00469
00470 LOADCHROMA(3);
00471 PUTRGB4D(dst_2, py_2, 3, 6 + 8);
00472 PUTRGB4D(dst_1, py_1, 3, 6);
00473 CLOSEYUV2RGBFUNC(4)
00474
00475 YUV2RGBFUNC(yuv2rgb_c_4b_ordered_dither, uint8_t, 0)
00476 const uint8_t *d64 = dither_8x8_73[y & 7];
00477 const uint8_t *d128 = dither_8x8_220[y & 7];
00478
00479 #define PUTRGB4DB(dst, src, i, o) \
00480 Y = src[2 * i]; \
00481 dst[2 * i] = r[Y + d128[0 + o]] + \
00482 g[Y + d64[0 + o]] + \
00483 b[Y + d128[0 + o]]; \
00484 Y = src[2 * i + 1]; \
00485 dst[2 * i + 1] = r[Y + d128[1 + o]] + \
00486 g[Y + d64[1 + o]] + \
00487 b[Y + d128[1 + o]];
00488
00489 LOADCHROMA(0);
00490 PUTRGB4DB(dst_1, py_1, 0, 0);
00491 PUTRGB4DB(dst_2, py_2, 0, 0 + 8);
00492
00493 LOADCHROMA(1);
00494 PUTRGB4DB(dst_2, py_2, 1, 2 + 8);
00495 PUTRGB4DB(dst_1, py_1, 1, 2);
00496
00497 LOADCHROMA(2);
00498 PUTRGB4DB(dst_1, py_1, 2, 4);
00499 PUTRGB4DB(dst_2, py_2, 2, 4 + 8);
00500
00501 LOADCHROMA(3);
00502 PUTRGB4DB(dst_2, py_2, 3, 6 + 8);
00503 PUTRGB4DB(dst_1, py_1, 3, 6);
00504 CLOSEYUV2RGBFUNC(8)
00505
00506 YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither, uint8_t, 0)
00507 const uint8_t *d128 = dither_8x8_220[y & 7];
00508 char out_1 = 0, out_2 = 0;
00509 g = c->table_gU[128 + YUVRGB_TABLE_HEADROOM] + c->table_gV[128 + YUVRGB_TABLE_HEADROOM];
00510
00511 #define PUTRGB1(out, src, i, o) \
00512 Y = src[2 * i]; \
00513 out += out + g[Y + d128[0 + o]]; \
00514 Y = src[2 * i + 1]; \
00515 out += out + g[Y + d128[1 + o]];
00516
00517 PUTRGB1(out_1, py_1, 0, 0);
00518 PUTRGB1(out_2, py_2, 0, 0 + 8);
00519
00520 PUTRGB1(out_2, py_2, 1, 2 + 8);
00521 PUTRGB1(out_1, py_1, 1, 2);
00522
00523 PUTRGB1(out_1, py_1, 2, 4);
00524 PUTRGB1(out_2, py_2, 2, 4 + 8);
00525
00526 PUTRGB1(out_2, py_2, 3, 6 + 8);
00527 PUTRGB1(out_1, py_1, 3, 6);
00528
00529 dst_1[0] = out_1;
00530 dst_2[0] = out_2;
00531 CLOSEYUV2RGBFUNC(1)
00532
00533 SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c)
00534 {
00535 SwsFunc t = NULL;
00536
00537 if (HAVE_MMX)
00538 t = ff_yuv2rgb_init_mmx(c);
00539 else if (HAVE_VIS)
00540 t = ff_yuv2rgb_init_vis(c);
00541 else if (HAVE_ALTIVEC)
00542 t = ff_yuv2rgb_init_altivec(c);
00543 else if (ARCH_BFIN)
00544 t = ff_yuv2rgb_get_func_ptr_bfin(c);
00545
00546 if (t)
00547 return t;
00548
00549 av_log(c, AV_LOG_WARNING,
00550 "No accelerated colorspace conversion found from %s to %s.\n",
00551 av_get_pix_fmt_name(c->srcFormat), av_get_pix_fmt_name(c->dstFormat));
00552
00553 switch (c->dstFormat) {
00554 case PIX_FMT_BGR48BE:
00555 case PIX_FMT_BGR48LE:
00556 return yuv2rgb_c_bgr48;
00557 case PIX_FMT_RGB48BE:
00558 case PIX_FMT_RGB48LE:
00559 return yuv2rgb_c_48;
00560 case PIX_FMT_ARGB:
00561 case PIX_FMT_ABGR:
00562 if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat))
00563 return yuva2argb_c;
00564 case PIX_FMT_RGBA:
00565 case PIX_FMT_BGRA:
00566 return (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat)) ? yuva2rgba_c : yuv2rgb_c_32;
00567 case PIX_FMT_RGB24:
00568 return yuv2rgb_c_24_rgb;
00569 case PIX_FMT_BGR24:
00570 return yuv2rgb_c_24_bgr;
00571 case PIX_FMT_RGB565:
00572 case PIX_FMT_BGR565:
00573 case PIX_FMT_RGB555:
00574 case PIX_FMT_BGR555:
00575 return yuv2rgb_c_16;
00576 case PIX_FMT_RGB444:
00577 case PIX_FMT_BGR444:
00578 return yuv2rgb_c_12_ordered_dither;
00579 case PIX_FMT_RGB8:
00580 case PIX_FMT_BGR8:
00581 return yuv2rgb_c_8_ordered_dither;
00582 case PIX_FMT_RGB4:
00583 case PIX_FMT_BGR4:
00584 return yuv2rgb_c_4_ordered_dither;
00585 case PIX_FMT_RGB4_BYTE:
00586 case PIX_FMT_BGR4_BYTE:
00587 return yuv2rgb_c_4b_ordered_dither;
00588 case PIX_FMT_MONOBLACK:
00589 return yuv2rgb_c_1_ordered_dither;
00590 default:
00591 assert(0);
00592 }
00593 return NULL;
00594 }
00595
00596 static void fill_table(uint8_t* table[256 + 2*YUVRGB_TABLE_HEADROOM], const int elemsize,
00597 const int inc, void *y_tab)
00598 {
00599 int i;
00600 uint8_t *y_table = y_tab;
00601
00602 y_table -= elemsize * (inc >> 9);
00603
00604 for (i = 0; i < 256 + 2*YUVRGB_TABLE_HEADROOM; i++) {
00605 int64_t cb = av_clip(i-YUVRGB_TABLE_HEADROOM, 0, 255)*inc;
00606 table[i] = y_table + elemsize * (cb >> 16);
00607 }
00608 }
00609
00610 static void fill_gv_table(int table[256 + 2*YUVRGB_TABLE_HEADROOM], const int elemsize, const int inc)
00611 {
00612 int i;
00613 int off = -(inc >> 9);
00614
00615 for (i = 0; i < 256 + 2*YUVRGB_TABLE_HEADROOM; i++) {
00616 int64_t cb = av_clip(i-YUVRGB_TABLE_HEADROOM, 0, 255)*inc;
00617 table[i] = elemsize * (off + (cb >> 16));
00618 }
00619 }
00620
00621 static uint16_t roundToInt16(int64_t f)
00622 {
00623 int r = (f + (1 << 15)) >> 16;
00624
00625 if (r < -0x7FFF)
00626 return 0x8000;
00627 else if (r > 0x7FFF)
00628 return 0x7FFF;
00629 else
00630 return r;
00631 }
00632
00633 av_cold int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
00634 int fullRange, int brightness,
00635 int contrast, int saturation)
00636 {
00637 const int isRgb = c->dstFormat == PIX_FMT_RGB32 ||
00638 c->dstFormat == PIX_FMT_RGB32_1 ||
00639 c->dstFormat == PIX_FMT_BGR24 ||
00640 c->dstFormat == PIX_FMT_RGB565BE ||
00641 c->dstFormat == PIX_FMT_RGB565LE ||
00642 c->dstFormat == PIX_FMT_RGB555BE ||
00643 c->dstFormat == PIX_FMT_RGB555LE ||
00644 c->dstFormat == PIX_FMT_RGB444BE ||
00645 c->dstFormat == PIX_FMT_RGB444LE ||
00646 c->dstFormat == PIX_FMT_RGB8 ||
00647 c->dstFormat == PIX_FMT_RGB4 ||
00648 c->dstFormat == PIX_FMT_RGB4_BYTE ||
00649 c->dstFormat == PIX_FMT_MONOBLACK;
00650 const int isNotNe = c->dstFormat == PIX_FMT_NE(RGB565LE, RGB565BE) ||
00651 c->dstFormat == PIX_FMT_NE(RGB555LE, RGB555BE) ||
00652 c->dstFormat == PIX_FMT_NE(RGB444LE, RGB444BE) ||
00653 c->dstFormat == PIX_FMT_NE(BGR565LE, BGR565BE) ||
00654 c->dstFormat == PIX_FMT_NE(BGR555LE, BGR555BE) ||
00655 c->dstFormat == PIX_FMT_NE(BGR444LE, BGR444BE);
00656 const int bpp = c->dstFormatBpp;
00657 uint8_t *y_table;
00658 uint16_t *y_table16;
00659 uint32_t *y_table32;
00660 int i, base, rbase, gbase, bbase, av_uninit(abase), needAlpha;
00661 const int yoffs = fullRange ? 384 : 326;
00662
00663 int64_t crv = inv_table[0];
00664 int64_t cbu = inv_table[1];
00665 int64_t cgu = -inv_table[2];
00666 int64_t cgv = -inv_table[3];
00667 int64_t cy = 1 << 16;
00668 int64_t oy = 0;
00669 int64_t yb = 0;
00670
00671 if (!fullRange) {
00672 cy = (cy * 255) / 219;
00673 oy = 16 << 16;
00674 } else {
00675 crv = (crv * 224) / 255;
00676 cbu = (cbu * 224) / 255;
00677 cgu = (cgu * 224) / 255;
00678 cgv = (cgv * 224) / 255;
00679 }
00680
00681 cy = (cy * contrast) >> 16;
00682 crv = (crv * contrast * saturation) >> 32;
00683 cbu = (cbu * contrast * saturation) >> 32;
00684 cgu = (cgu * contrast * saturation) >> 32;
00685 cgv = (cgv * contrast * saturation) >> 32;
00686 oy -= 256 * brightness;
00687
00688 c->uOffset = 0x0400040004000400LL;
00689 c->vOffset = 0x0400040004000400LL;
00690 c->yCoeff = roundToInt16(cy * 8192) * 0x0001000100010001ULL;
00691 c->vrCoeff = roundToInt16(crv * 8192) * 0x0001000100010001ULL;
00692 c->ubCoeff = roundToInt16(cbu * 8192) * 0x0001000100010001ULL;
00693 c->vgCoeff = roundToInt16(cgv * 8192) * 0x0001000100010001ULL;
00694 c->ugCoeff = roundToInt16(cgu * 8192) * 0x0001000100010001ULL;
00695 c->yOffset = roundToInt16(oy * 8) * 0x0001000100010001ULL;
00696
00697 c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy << 13);
00698 c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
00699 c->yuv2rgb_v2r_coeff = (int16_t)roundToInt16(crv << 13);
00700 c->yuv2rgb_v2g_coeff = (int16_t)roundToInt16(cgv << 13);
00701 c->yuv2rgb_u2g_coeff = (int16_t)roundToInt16(cgu << 13);
00702 c->yuv2rgb_u2b_coeff = (int16_t)roundToInt16(cbu << 13);
00703
00704
00705 crv = ((crv << 16) + 0x8000) / cy;
00706 cbu = ((cbu << 16) + 0x8000) / cy;
00707 cgu = ((cgu << 16) + 0x8000) / cy;
00708 cgv = ((cgv << 16) + 0x8000) / cy;
00709
00710 av_free(c->yuvTable);
00711
00712 switch (bpp) {
00713 case 1:
00714 c->yuvTable = av_malloc(1024);
00715 y_table = c->yuvTable;
00716 yb = -(384 << 16) - oy;
00717 for (i = 0; i < 1024 - 110; i++) {
00718 y_table[i + 110] = av_clip_uint8((yb + 0x8000) >> 16) >> 7;
00719 yb += cy;
00720 }
00721 fill_table(c->table_gU, 1, cgu, y_table + yoffs);
00722 fill_gv_table(c->table_gV, 1, cgv);
00723 break;
00724 case 4:
00725 case 4 | 128:
00726 rbase = isRgb ? 3 : 0;
00727 gbase = 1;
00728 bbase = isRgb ? 0 : 3;
00729 c->yuvTable = av_malloc(1024 * 3);
00730 y_table = c->yuvTable;
00731 yb = -(384 << 16) - oy;
00732 for (i = 0; i < 1024 - 110; i++) {
00733 int yval = av_clip_uint8((yb + 0x8000) >> 16);
00734 y_table[i + 110] = (yval >> 7) << rbase;
00735 y_table[i + 37 + 1024] = ((yval + 43) / 85) << gbase;
00736 y_table[i + 110 + 2048] = (yval >> 7) << bbase;
00737 yb += cy;
00738 }
00739 fill_table(c->table_rV, 1, crv, y_table + yoffs);
00740 fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
00741 fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
00742 fill_gv_table(c->table_gV, 1, cgv);
00743 break;
00744 case 8:
00745 rbase = isRgb ? 5 : 0;
00746 gbase = isRgb ? 2 : 3;
00747 bbase = isRgb ? 0 : 6;
00748 c->yuvTable = av_malloc(1024 * 3);
00749 y_table = c->yuvTable;
00750 yb = -(384 << 16) - oy;
00751 for (i = 0; i < 1024 - 38; i++) {
00752 int yval = av_clip_uint8((yb + 0x8000) >> 16);
00753 y_table[i + 16] = ((yval + 18) / 36) << rbase;
00754 y_table[i + 16 + 1024] = ((yval + 18) / 36) << gbase;
00755 y_table[i + 37 + 2048] = ((yval + 43) / 85) << bbase;
00756 yb += cy;
00757 }
00758 fill_table(c->table_rV, 1, crv, y_table + yoffs);
00759 fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
00760 fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
00761 fill_gv_table(c->table_gV, 1, cgv);
00762 break;
00763 case 12:
00764 rbase = isRgb ? 8 : 0;
00765 gbase = 4;
00766 bbase = isRgb ? 0 : 8;
00767 c->yuvTable = av_malloc(1024 * 3 * 2);
00768 y_table16 = c->yuvTable;
00769 yb = -(384 << 16) - oy;
00770 for (i = 0; i < 1024; i++) {
00771 uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
00772 y_table16[i] = (yval >> 4) << rbase;
00773 y_table16[i + 1024] = (yval >> 4) << gbase;
00774 y_table16[i + 2048] = (yval >> 4) << bbase;
00775 yb += cy;
00776 }
00777 if (isNotNe)
00778 for (i = 0; i < 1024 * 3; i++)
00779 y_table16[i] = av_bswap16(y_table16[i]);
00780 fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
00781 fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
00782 fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
00783 fill_gv_table(c->table_gV, 2, cgv);
00784 break;
00785 case 15:
00786 case 16:
00787 rbase = isRgb ? bpp - 5 : 0;
00788 gbase = 5;
00789 bbase = isRgb ? 0 : (bpp - 5);
00790 c->yuvTable = av_malloc(1024 * 3 * 2);
00791 y_table16 = c->yuvTable;
00792 yb = -(384 << 16) - oy;
00793 for (i = 0; i < 1024; i++) {
00794 uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
00795 y_table16[i] = (yval >> 3) << rbase;
00796 y_table16[i + 1024] = (yval >> (18 - bpp)) << gbase;
00797 y_table16[i + 2048] = (yval >> 3) << bbase;
00798 yb += cy;
00799 }
00800 if (isNotNe)
00801 for (i = 0; i < 1024 * 3; i++)
00802 y_table16[i] = av_bswap16(y_table16[i]);
00803 fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
00804 fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
00805 fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
00806 fill_gv_table(c->table_gV, 2, cgv);
00807 break;
00808 case 24:
00809 case 48:
00810 c->yuvTable = av_malloc(1024);
00811 y_table = c->yuvTable;
00812 yb = -(384 << 16) - oy;
00813 for (i = 0; i < 1024; i++) {
00814 y_table[i] = av_clip_uint8((yb + 0x8000) >> 16);
00815 yb += cy;
00816 }
00817 fill_table(c->table_rV, 1, crv, y_table + yoffs);
00818 fill_table(c->table_gU, 1, cgu, y_table + yoffs);
00819 fill_table(c->table_bU, 1, cbu, y_table + yoffs);
00820 fill_gv_table(c->table_gV, 1, cgv);
00821 break;
00822 case 32:
00823 base = (c->dstFormat == PIX_FMT_RGB32_1 ||
00824 c->dstFormat == PIX_FMT_BGR32_1) ? 8 : 0;
00825 rbase = base + (isRgb ? 16 : 0);
00826 gbase = base + 8;
00827 bbase = base + (isRgb ? 0 : 16);
00828 needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
00829 if (!needAlpha)
00830 abase = (base + 24) & 31;
00831 c->yuvTable = av_malloc(1024 * 3 * 4);
00832 y_table32 = c->yuvTable;
00833 yb = -(384 << 16) - oy;
00834 for (i = 0; i < 1024; i++) {
00835 unsigned yval = av_clip_uint8((yb + 0x8000) >> 16);
00836 y_table32[i] = (yval << rbase) +
00837 (needAlpha ? 0 : (255u << abase));
00838 y_table32[i + 1024] = yval << gbase;
00839 y_table32[i + 2048] = yval << bbase;
00840 yb += cy;
00841 }
00842 fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
00843 fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + 1024);
00844 fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2048);
00845 fill_gv_table(c->table_gV, 4, cgv);
00846 break;
00847 default:
00848 c->yuvTable = NULL;
00849 av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
00850 return -1;
00851 }
00852 return 0;
00853 }