You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2622 lines
74KB

  1. /*
  2. Copyright (C) 2001-2002 Michael Niedermayer <michaelni@gmx.at>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. /*
  16. supported Input formats: YV12, I420/IYUV, YUY2, BGR32, BGR24, BGR16, BGR15, RGB32, RGB24, Y8/Y800, YVU9/IF09
  17. supported output formats: YV12, I420/IYUV, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
  18. {BGR,RGB}{1,4,8,15,16} support dithering
  19. unscaled special converters (YV12=I420=IYUV, Y800=Y8)
  20. YV12 -> {BGR,RGB}{1,4,8,15,16,24,32}
  21. x -> x
  22. YUV9 -> YV12
  23. YUV9/YV12 -> Y800
  24. Y800 -> YUV9/YV12
  25. BGR24 -> BGR32 & RGB24 -> RGB32
  26. BGR32 -> BGR24 & RGB32 -> RGB24
  27. BGR15 -> BGR16
  28. */
  29. /*
  30. tested special converters (most are tested actually but i didnt write it down ...)
  31. YV12 -> BGR16
  32. YV12 -> YV12
  33. BGR15 -> BGR16
  34. BGR16 -> BGR16
  35. YVU9 -> YV12
  36. untested special converters
  37. YV12/I420 -> BGR15/BGR24/BGR32 (its the yuv2rgb stuff, so it should be ok)
  38. YV12/I420 -> YV12/I420
  39. YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
  40. BGR24 -> BGR32 & RGB24 -> RGB32
  41. BGR32 -> BGR24 & RGB32 -> RGB24
  42. BGR24 -> YV12
  43. */
  44. #include <inttypes.h>
  45. #include <string.h>
  46. #include <math.h>
  47. #include <stdio.h>
  48. #include "../config.h"
  49. #include "../mangle.h"
  50. #include <assert.h>
  51. #ifdef HAVE_MALLOC_H
  52. #include <malloc.h>
  53. #else
  54. #include <stdlib.h>
  55. #endif
  56. #include "swscale.h"
  57. #include "../cpudetect.h"
  58. #include "../bswap.h"
  59. #include "../libvo/img_format.h"
  60. #include "rgb2rgb.h"
  61. #include "../libvo/fastmemcpy.h"
  62. #include "../mp_msg.h"
  63. #define MSG_WARN(args...) mp_msg(MSGT_SWS,MSGL_WARN, ##args )
  64. #define MSG_FATAL(args...) mp_msg(MSGT_SWS,MSGL_FATAL, ##args )
  65. #define MSG_ERR(args...) mp_msg(MSGT_SWS,MSGL_ERR, ##args )
  66. #define MSG_V(args...) mp_msg(MSGT_SWS,MSGL_V, ##args )
  67. #define MSG_DBG2(args...) mp_msg(MSGT_SWS,MSGL_DBG2, ##args )
  68. #define MSG_INFO(args...) mp_msg(MSGT_SWS,MSGL_INFO, ##args )
  69. #undef MOVNTQ
  70. #undef PAVGB
  71. //#undef HAVE_MMX2
  72. //#define HAVE_3DNOW
  73. //#undef HAVE_MMX
  74. //#undef ARCH_X86
  75. //#define WORDS_BIGENDIAN
  76. #define DITHER1XBPP
  77. #define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
  78. #define RET 0xC3 //near return opcode for X86
  79. #ifdef MP_DEBUG
  80. #define ASSERT(x) assert(x);
  81. #else
  82. #define ASSERT(x) ;
  83. #endif
  84. #ifdef M_PI
  85. #define PI M_PI
  86. #else
  87. #define PI 3.14159265358979323846
  88. #endif
  89. //FIXME replace this with something faster
  90. #define isPlanarYUV(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 || (x)==IMGFMT_YVU9)
  91. #define isYUV(x) ((x)==IMGFMT_YUY2 || isPlanarYUV(x))
  92. #define isGray(x) ((x)==IMGFMT_Y800)
  93. #define isRGB(x) (((x)&IMGFMT_RGB_MASK)==IMGFMT_RGB)
  94. #define isBGR(x) (((x)&IMGFMT_BGR_MASK)==IMGFMT_BGR)
  95. #define isSupportedIn(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 || (x)==IMGFMT_YUY2 \
  96. || (x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15\
  97. || (x)==IMGFMT_RGB32|| (x)==IMGFMT_RGB24\
  98. || (x)==IMGFMT_Y800 || (x)==IMGFMT_YVU9)
  99. #define isSupportedOut(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 \
  100. || isRGB(x) || isBGR(x)\
  101. || (x)==IMGFMT_Y800 || (x)==IMGFMT_YVU9)
  102. #define isPacked(x) ((x)==IMGFMT_YUY2 || isRGB(x) || isBGR(x))
  103. #define RGB2YUV_SHIFT 16
  104. #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
  105. #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
  106. #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  107. #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
  108. #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
  109. #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
  110. #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
  111. #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  112. #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
  113. extern int verbose; // defined in mplayer.c
  114. /*
  115. NOTES
  116. Special versions: fast Y 1:1 scaling (no interpolation in y direction)
  117. TODO
  118. more intelligent missalignment avoidance for the horizontal scaler
  119. write special vertical cubic upscale version
  120. Optimize C code (yv12 / minmax)
  121. add support for packed pixel yuv input & output
  122. add support for Y8 output
  123. optimize bgr24 & bgr32
  124. add BGR4 output support
  125. write special BGR->BGR scaler
  126. deglobalize yuv2rgb*.c
  127. */
  128. #define ABS(a) ((a) > 0 ? (a) : (-(a)))
  129. #define MIN(a,b) ((a) > (b) ? (b) : (a))
  130. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  131. #ifdef ARCH_X86
  132. #define CAN_COMPILE_X86_ASM
  133. #endif
  134. #ifdef CAN_COMPILE_X86_ASM
  135. static uint64_t __attribute__((aligned(8))) yCoeff= 0x2568256825682568LL;
  136. static uint64_t __attribute__((aligned(8))) vrCoeff= 0x3343334333433343LL;
  137. static uint64_t __attribute__((aligned(8))) ubCoeff= 0x40cf40cf40cf40cfLL;
  138. static uint64_t __attribute__((aligned(8))) vgCoeff= 0xE5E2E5E2E5E2E5E2LL;
  139. static uint64_t __attribute__((aligned(8))) ugCoeff= 0xF36EF36EF36EF36ELL;
  140. static uint64_t __attribute__((aligned(8))) bF8= 0xF8F8F8F8F8F8F8F8LL;
  141. static uint64_t __attribute__((aligned(8))) bFC= 0xFCFCFCFCFCFCFCFCLL;
  142. static uint64_t __attribute__((aligned(8))) w400= 0x0400040004000400LL;
  143. static uint64_t __attribute__((aligned(8))) w80= 0x0080008000800080LL;
  144. static uint64_t __attribute__((aligned(8))) w10= 0x0010001000100010LL;
  145. static uint64_t __attribute__((aligned(8))) w02= 0x0002000200020002LL;
  146. static uint64_t __attribute__((aligned(8))) bm00001111=0x00000000FFFFFFFFLL;
  147. static uint64_t __attribute__((aligned(8))) bm00000111=0x0000000000FFFFFFLL;
  148. static uint64_t __attribute__((aligned(8))) bm11111000=0xFFFFFFFFFF000000LL;
  149. static uint64_t __attribute__((aligned(8))) bm01010101=0x00FF00FF00FF00FFLL;
  150. static volatile uint64_t __attribute__((aligned(8))) b5Dither;
  151. static volatile uint64_t __attribute__((aligned(8))) g5Dither;
  152. static volatile uint64_t __attribute__((aligned(8))) g6Dither;
  153. static volatile uint64_t __attribute__((aligned(8))) r5Dither;
  154. static uint64_t __attribute__((aligned(8))) dither4[2]={
  155. 0x0103010301030103LL,
  156. 0x0200020002000200LL,};
  157. static uint64_t __attribute__((aligned(8))) dither8[2]={
  158. 0x0602060206020602LL,
  159. 0x0004000400040004LL,};
  160. static uint64_t __attribute__((aligned(8))) b16Mask= 0x001F001F001F001FLL;
  161. static uint64_t __attribute__((aligned(8))) g16Mask= 0x07E007E007E007E0LL;
  162. static uint64_t __attribute__((aligned(8))) r16Mask= 0xF800F800F800F800LL;
  163. static uint64_t __attribute__((aligned(8))) b15Mask= 0x001F001F001F001FLL;
  164. static uint64_t __attribute__((aligned(8))) g15Mask= 0x03E003E003E003E0LL;
  165. static uint64_t __attribute__((aligned(8))) r15Mask= 0x7C007C007C007C00LL;
  166. static uint64_t __attribute__((aligned(8))) M24A= 0x00FF0000FF0000FFLL;
  167. static uint64_t __attribute__((aligned(8))) M24B= 0xFF0000FF0000FF00LL;
  168. static uint64_t __attribute__((aligned(8))) M24C= 0x0000FF0000FF0000LL;
  169. #ifdef FAST_BGR2YV12
  170. static const uint64_t bgr2YCoeff __attribute__((aligned(8))) = 0x000000210041000DULL;
  171. static const uint64_t bgr2UCoeff __attribute__((aligned(8))) = 0x0000FFEEFFDC0038ULL;
  172. static const uint64_t bgr2VCoeff __attribute__((aligned(8))) = 0x00000038FFD2FFF8ULL;
  173. #else
  174. static const uint64_t bgr2YCoeff __attribute__((aligned(8))) = 0x000020E540830C8BULL;
  175. static const uint64_t bgr2UCoeff __attribute__((aligned(8))) = 0x0000ED0FDAC23831ULL;
  176. static const uint64_t bgr2VCoeff __attribute__((aligned(8))) = 0x00003831D0E6F6EAULL;
  177. #endif
  178. static const uint64_t bgr2YOffset __attribute__((aligned(8))) = 0x1010101010101010ULL;
  179. static const uint64_t bgr2UVOffset __attribute__((aligned(8)))= 0x8080808080808080ULL;
  180. static const uint64_t w1111 __attribute__((aligned(8))) = 0x0001000100010001ULL;
  181. #endif
  182. // clipping helper table for C implementations:
  183. static unsigned char clip_table[768];
  184. //global sws_flags from the command line
  185. int sws_flags=2;
  186. //global srcFilter
  187. SwsFilter src_filter= {NULL, NULL, NULL, NULL};
  188. float sws_lum_gblur= 0.0;
  189. float sws_chr_gblur= 0.0;
  190. int sws_chr_vshift= 0;
  191. int sws_chr_hshift= 0;
  192. float sws_chr_sharpen= 0.0;
  193. float sws_lum_sharpen= 0.0;
  194. /* cpuCaps combined from cpudetect and whats actually compiled in
  195. (if there is no support for something compiled in it wont appear here) */
  196. static CpuCaps cpuCaps;
  197. void (*swScale)(SwsContext *context, uint8_t* src[], int srcStride[], int srcSliceY,
  198. int srcSliceH, uint8_t* dst[], int dstStride[])=NULL;
  199. static SwsVector *getConvVec(SwsVector *a, SwsVector *b);
  200. static inline void orderYUV(int format, uint8_t * sortedP[], int sortedStride[], uint8_t * p[], int stride[]);
  201. void *yuv2rgb_c_init (unsigned bpp, int mode, void *table_rV[256], void *table_gU[256], int table_gV[256], void *table_bU[256]);
  202. extern const uint8_t dither_2x2_4[2][8];
  203. extern const uint8_t dither_2x2_8[2][8];
  204. extern const uint8_t dither_8x8_32[8][8];
  205. extern const uint8_t dither_8x8_73[8][8];
  206. extern const uint8_t dither_8x8_220[8][8];
  207. #ifdef CAN_COMPILE_X86_ASM
  208. void in_asm_used_var_warning_killer()
  209. {
  210. volatile int i= yCoeff+vrCoeff+ubCoeff+vgCoeff+ugCoeff+bF8+bFC+w400+w80+w10+
  211. bm00001111+bm00000111+bm11111000+b16Mask+g16Mask+r16Mask+b15Mask+g15Mask+r15Mask+
  212. M24A+M24B+M24C+w02 + b5Dither+g5Dither+r5Dither+g6Dither+dither4[0]+dither8[0]+bm01010101;
  213. if(i) i=0;
  214. }
  215. #endif
  216. static int testFormat[]={
  217. IMGFMT_YVU9,
  218. IMGFMT_YV12,
  219. //IMGFMT_IYUV,
  220. IMGFMT_I420,
  221. IMGFMT_BGR15,
  222. IMGFMT_BGR16,
  223. IMGFMT_BGR24,
  224. IMGFMT_BGR32,
  225. IMGFMT_RGB24,
  226. IMGFMT_RGB32,
  227. //IMGFMT_Y8,
  228. IMGFMT_Y800,
  229. //IMGFMT_YUY2,
  230. 0
  231. };
  232. static uint64_t getSSD(uint8_t *src1, uint8_t *src2, int stride1, int stride2, int w, int h){
  233. int x,y;
  234. uint64_t ssd=0;
  235. for(y=0; y<h; y++){
  236. for(x=0; x<w; x++){
  237. int d= src1[x + y*stride1] - src2[x + y*stride2];
  238. ssd+= d*d;
  239. }
  240. }
  241. return ssd;
  242. }
  243. // test by ref -> src -> dst -> out & compare out against ref
  244. // ref & out are YV12
  245. static void doTest(uint8_t *ref[3], int refStride[3], int w, int h, int srcFormat, int dstFormat,
  246. int srcW, int srcH, int dstW, int dstH, int flags){
  247. uint8_t *src[3];
  248. uint8_t *dst[3];
  249. uint8_t *out[3];
  250. int srcStride[3], dstStride[3];
  251. int i;
  252. uint64_t ssdY, ssdU, ssdV;
  253. SwsContext *srcContext, *dstContext, *outContext;
  254. for(i=0; i<3; i++){
  255. // avoid stride % bpp != 0
  256. if(srcFormat==IMGFMT_RGB24 || srcFormat==IMGFMT_BGR24)
  257. srcStride[i]= srcW*3;
  258. else
  259. srcStride[i]= srcW*4;
  260. if(dstFormat==IMGFMT_RGB24 || dstFormat==IMGFMT_BGR24)
  261. dstStride[i]= dstW*3;
  262. else
  263. dstStride[i]= dstW*4;
  264. src[i]= malloc(srcStride[i]*srcH);
  265. dst[i]= malloc(dstStride[i]*dstH);
  266. out[i]= malloc(refStride[i]*h);
  267. }
  268. srcContext= getSwsContext(w, h, IMGFMT_YV12, srcW, srcH, srcFormat, flags, NULL, NULL);
  269. dstContext= getSwsContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat, flags, NULL, NULL);
  270. outContext= getSwsContext(dstW, dstH, dstFormat, w, h, IMGFMT_YV12, flags, NULL, NULL);
  271. if(srcContext==NULL ||dstContext==NULL ||outContext==NULL){
  272. printf("Failed allocating swsContext\n");
  273. goto end;
  274. }
  275. // printf("test %X %X %X -> %X %X %X\n", (int)ref[0], (int)ref[1], (int)ref[2],
  276. // (int)src[0], (int)src[1], (int)src[2]);
  277. srcContext->swScale(srcContext, ref, refStride, 0, h , src, srcStride);
  278. dstContext->swScale(dstContext, src, srcStride, 0, srcH, dst, dstStride);
  279. outContext->swScale(outContext, dst, dstStride, 0, dstH, out, refStride);
  280. ssdY= getSSD(ref[0], out[0], refStride[0], refStride[0], w, h);
  281. ssdU= getSSD(ref[1], out[1], refStride[1], refStride[1], (w+1)>>1, (h+1)>>1);
  282. ssdV= getSSD(ref[2], out[2], refStride[2], refStride[2], (w+1)>>1, (h+1)>>1);
  283. if(isGray(srcFormat) || isGray(dstFormat)) ssdU=ssdV=0; //FIXME check that output is really gray
  284. ssdY/= w*h;
  285. ssdU/= w*h/4;
  286. ssdV/= w*h/4;
  287. if(ssdY>100 || ssdU>50 || ssdV>50){
  288. printf(" %s %dx%d -> %s %4dx%4d flags=%2d SSD=%5lld,%5lld,%5lld\n",
  289. vo_format_name(srcFormat), srcW, srcH,
  290. vo_format_name(dstFormat), dstW, dstH,
  291. flags,
  292. ssdY, ssdU, ssdV);
  293. }
  294. end:
  295. freeSwsContext(srcContext);
  296. freeSwsContext(dstContext);
  297. freeSwsContext(outContext);
  298. for(i=0; i<3; i++){
  299. free(src[i]);
  300. free(dst[i]);
  301. free(out[i]);
  302. }
  303. }
  304. static void selfTest(uint8_t *src[3], int stride[3], int w, int h){
  305. int srcFormat, dstFormat, srcFormatIndex, dstFormatIndex;
  306. int srcW, srcH, dstW, dstH;
  307. int flags;
  308. for(srcFormatIndex=0; ;srcFormatIndex++){
  309. srcFormat= testFormat[srcFormatIndex];
  310. if(!srcFormat) break;
  311. for(dstFormatIndex=0; ;dstFormatIndex++){
  312. dstFormat= testFormat[dstFormatIndex];
  313. if(!dstFormat) break;
  314. if(!isSupportedOut(dstFormat)) continue;
  315. printf("%s -> %s\n",
  316. vo_format_name(srcFormat),
  317. vo_format_name(dstFormat));
  318. srcW= w+w/3;
  319. srcH= h+h/3;
  320. for(dstW=w; dstW<w*2; dstW+= dstW/3){
  321. for(dstH=h; dstH<h*2; dstH+= dstH/3){
  322. for(flags=1; flags<33; flags*=2)
  323. doTest(src, stride, w, h, srcFormat, dstFormat,
  324. srcW, srcH, dstW, dstH, flags);
  325. }
  326. }
  327. }
  328. }
  329. }
  330. static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  331. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  332. uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW, int chrDstW)
  333. {
  334. //FIXME Optimize (just quickly writen not opti..)
  335. int i;
  336. for(i=0; i<dstW; i++)
  337. {
  338. int val=0;
  339. int j;
  340. for(j=0; j<lumFilterSize; j++)
  341. val += lumSrc[j][i] * lumFilter[j];
  342. dest[i]= MIN(MAX(val>>19, 0), 255);
  343. }
  344. if(uDest != NULL)
  345. for(i=0; i<chrDstW; i++)
  346. {
  347. int u=0;
  348. int v=0;
  349. int j;
  350. for(j=0; j<chrFilterSize; j++)
  351. {
  352. u += chrSrc[j][i] * chrFilter[j];
  353. v += chrSrc[j][i + 2048] * chrFilter[j];
  354. }
  355. uDest[i]= MIN(MAX(u>>19, 0), 255);
  356. vDest[i]= MIN(MAX(v>>19, 0), 255);
  357. }
  358. }
  359. #define YSCALE_YUV_2_RGBX_C(type) \
  360. for(i=0; i<(dstW>>1); i++){\
  361. int j;\
  362. int Y1=0;\
  363. int Y2=0;\
  364. int U=0;\
  365. int V=0;\
  366. type *r, *b, *g;\
  367. const int i2= 2*i;\
  368. \
  369. for(j=0; j<lumFilterSize; j++)\
  370. {\
  371. Y1 += lumSrc[j][i2] * lumFilter[j];\
  372. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  373. }\
  374. for(j=0; j<chrFilterSize; j++)\
  375. {\
  376. U += chrSrc[j][i] * chrFilter[j];\
  377. V += chrSrc[j][i+2048] * chrFilter[j];\
  378. }\
  379. Y1>>=19;\
  380. Y2>>=19;\
  381. U >>=19;\
  382. V >>=19;\
  383. if((Y1|Y2|U|V)&256)\
  384. {\
  385. if(Y1>255) Y1=255;\
  386. else if(Y1<0)Y1=0;\
  387. if(Y2>255) Y2=255;\
  388. else if(Y2<0)Y2=0;\
  389. if(U>255) U=255;\
  390. else if(U<0) U=0;\
  391. if(V>255) V=255;\
  392. else if(V<0) V=0;\
  393. }\
  394. r = c->table_rV[V];\
  395. g = c->table_gU[U] + c->table_gV[V];\
  396. b = c->table_bU[U];\
  397. #define YSCALE_YUV_2_RGB2_C(type) \
  398. for(i=0; i<(dstW>>1); i++){\
  399. const int i2= 2*i;\
  400. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19;\
  401. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19;\
  402. int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19;\
  403. int V= (uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>19;\
  404. type *r, *b, *g;\
  405. r = c->table_rV[V];\
  406. g = c->table_gU[U] + c->table_gV[V];\
  407. b = c->table_bU[U];\
  408. #define YSCALE_YUV_2_RGB1_C(type) \
  409. for(i=0; i<(dstW>>1); i++){\
  410. const int i2= 2*i;\
  411. int Y1= buf0[i2 ]>>7;\
  412. int Y2= buf0[i2+1]>>7;\
  413. int U= (uvbuf1[i ])>>7;\
  414. int V= (uvbuf1[i+2048])>>7;\
  415. type *r, *b, *g;\
  416. r = c->table_rV[V];\
  417. g = c->table_gU[U] + c->table_gV[V];\
  418. b = c->table_bU[U];\
  419. #define YSCALE_YUV_2_RGB1B_C(type) \
  420. for(i=0; i<(dstW>>1); i++){\
  421. const int i2= 2*i;\
  422. int Y1= buf0[i2 ]>>7;\
  423. int Y2= buf0[i2+1]>>7;\
  424. int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
  425. int V= (uvbuf0[i+2048] + uvbuf1[i+2048])>>8;\
  426. type *r, *b, *g;\
  427. r = c->table_rV[V];\
  428. g = c->table_gU[U] + c->table_gV[V];\
  429. b = c->table_bU[U];\
  430. #define YSCALE_YUV_2_ANYRGB_C(func)\
  431. switch(c->dstFormat)\
  432. {\
  433. case IMGFMT_BGR32:\
  434. case IMGFMT_RGB32:\
  435. func(uint32_t)\
  436. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  437. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  438. } \
  439. break;\
  440. case IMGFMT_RGB24:\
  441. func(uint8_t)\
  442. ((uint8_t*)dest)[0]= r[Y1];\
  443. ((uint8_t*)dest)[1]= g[Y1];\
  444. ((uint8_t*)dest)[2]= b[Y1];\
  445. ((uint8_t*)dest)[3]= r[Y2];\
  446. ((uint8_t*)dest)[4]= g[Y2];\
  447. ((uint8_t*)dest)[5]= b[Y2];\
  448. ((uint8_t*)dest)+=6;\
  449. }\
  450. break;\
  451. case IMGFMT_BGR24:\
  452. func(uint8_t)\
  453. ((uint8_t*)dest)[0]= b[Y1];\
  454. ((uint8_t*)dest)[1]= g[Y1];\
  455. ((uint8_t*)dest)[2]= r[Y1];\
  456. ((uint8_t*)dest)[3]= b[Y2];\
  457. ((uint8_t*)dest)[4]= g[Y2];\
  458. ((uint8_t*)dest)[5]= r[Y2];\
  459. ((uint8_t*)dest)+=6;\
  460. }\
  461. break;\
  462. case IMGFMT_RGB16:\
  463. case IMGFMT_BGR16:\
  464. {\
  465. const int dr1= dither_2x2_8[y&1 ][0];\
  466. const int dg1= dither_2x2_4[y&1 ][0];\
  467. const int db1= dither_2x2_8[(y&1)^1][0];\
  468. const int dr2= dither_2x2_8[y&1 ][1];\
  469. const int dg2= dither_2x2_4[y&1 ][1];\
  470. const int db2= dither_2x2_8[(y&1)^1][1];\
  471. func(uint16_t)\
  472. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  473. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  474. }\
  475. }\
  476. break;\
  477. case IMGFMT_RGB15:\
  478. case IMGFMT_BGR15:\
  479. {\
  480. const int dr1= dither_2x2_8[y&1 ][0];\
  481. const int dg1= dither_2x2_8[y&1 ][1];\
  482. const int db1= dither_2x2_8[(y&1)^1][0];\
  483. const int dr2= dither_2x2_8[y&1 ][1];\
  484. const int dg2= dither_2x2_8[y&1 ][0];\
  485. const int db2= dither_2x2_8[(y&1)^1][1];\
  486. func(uint16_t)\
  487. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  488. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  489. }\
  490. }\
  491. break;\
  492. case IMGFMT_RGB8:\
  493. case IMGFMT_BGR8:\
  494. {\
  495. const uint8_t * const d64= dither_8x8_73[y&7];\
  496. const uint8_t * const d32= dither_8x8_32[y&7];\
  497. func(uint8_t)\
  498. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
  499. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
  500. }\
  501. }\
  502. break;\
  503. case IMGFMT_RGB4:\
  504. case IMGFMT_BGR4:\
  505. {\
  506. const uint8_t * const d64= dither_8x8_73 [y&7];\
  507. const uint8_t * const d128=dither_8x8_220[y&7];\
  508. func(uint8_t)\
  509. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
  510. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
  511. }\
  512. }\
  513. break;\
  514. case IMGFMT_RGB1:\
  515. case IMGFMT_BGR1:\
  516. {\
  517. const uint8_t * const d128=dither_8x8_220[y&7];\
  518. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  519. for(i=0; i<dstW-7; i+=8){\
  520. int acc;\
  521. acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
  522. acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
  523. acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
  524. acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
  525. acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
  526. acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
  527. acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
  528. acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
  529. ((uint8_t*)dest)[0]= acc;\
  530. ((uint8_t*)dest)++;\
  531. }\
  532. \
  533. /*\
  534. ((uint8_t*)dest)-= dstW>>4;\
  535. {\
  536. int acc=0;\
  537. int left=0;\
  538. static int top[1024];\
  539. static int last_new[1024][1024];\
  540. static int last_in3[1024][1024];\
  541. static int drift[1024][1024];\
  542. int topLeft=0;\
  543. int shift=0;\
  544. int count=0;\
  545. const uint8_t * const d128=dither_8x8_220[y&7];\
  546. int error_new=0;\
  547. int error_in3=0;\
  548. int f=0;\
  549. \
  550. for(i=dstW>>1; i<dstW; i++){\
  551. int in= ((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19);\
  552. int in2 = (76309 * (in - 16) + 32768) >> 16;\
  553. int in3 = (in2 < 0) ? 0 : ((in2 > 255) ? 255 : in2);\
  554. int old= (left*7 + topLeft + top[i]*5 + top[i+1]*3)/20 + in3\
  555. + (last_new[y][i] - in3)*f/256;\
  556. int new= old> 128 ? 255 : 0;\
  557. \
  558. error_new+= ABS(last_new[y][i] - new);\
  559. error_in3+= ABS(last_in3[y][i] - in3);\
  560. f= error_new - error_in3*4;\
  561. if(f<0) f=0;\
  562. if(f>256) f=256;\
  563. \
  564. topLeft= top[i];\
  565. left= top[i]= old - new;\
  566. last_new[y][i]= new;\
  567. last_in3[y][i]= in3;\
  568. \
  569. acc+= acc + (new&1);\
  570. if((i&7)==6){\
  571. ((uint8_t*)dest)[0]= acc;\
  572. ((uint8_t*)dest)++;\
  573. }\
  574. }\
  575. }\
  576. */\
  577. }\
  578. break;\
  579. }\
  580. static inline void yuv2rgbXinC(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  581. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  582. uint8_t *dest, int dstW, int y)
  583. {
  584. int i;
  585. switch(c->dstFormat)
  586. {
  587. case IMGFMT_RGB32:
  588. case IMGFMT_BGR32:
  589. YSCALE_YUV_2_RGBX_C(uint32_t)
  590. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];
  591. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];
  592. }
  593. break;
  594. case IMGFMT_RGB24:
  595. YSCALE_YUV_2_RGBX_C(uint8_t)
  596. ((uint8_t*)dest)[0]= r[Y1];
  597. ((uint8_t*)dest)[1]= g[Y1];
  598. ((uint8_t*)dest)[2]= b[Y1];
  599. ((uint8_t*)dest)[3]= r[Y2];
  600. ((uint8_t*)dest)[4]= g[Y2];
  601. ((uint8_t*)dest)[5]= b[Y2];
  602. ((uint8_t*)dest)+=6;
  603. }
  604. break;
  605. case IMGFMT_BGR24:
  606. YSCALE_YUV_2_RGBX_C(uint8_t)
  607. ((uint8_t*)dest)[0]= b[Y1];
  608. ((uint8_t*)dest)[1]= g[Y1];
  609. ((uint8_t*)dest)[2]= r[Y1];
  610. ((uint8_t*)dest)[3]= b[Y2];
  611. ((uint8_t*)dest)[4]= g[Y2];
  612. ((uint8_t*)dest)[5]= r[Y2];
  613. ((uint8_t*)dest)+=6;
  614. }
  615. break;
  616. case IMGFMT_RGB16:
  617. case IMGFMT_BGR16:
  618. {
  619. const int dr1= dither_2x2_8[y&1 ][0];
  620. const int dg1= dither_2x2_4[y&1 ][0];
  621. const int db1= dither_2x2_8[(y&1)^1][0];
  622. const int dr2= dither_2x2_8[y&1 ][1];
  623. const int dg2= dither_2x2_4[y&1 ][1];
  624. const int db2= dither_2x2_8[(y&1)^1][1];
  625. YSCALE_YUV_2_RGBX_C(uint16_t)
  626. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
  627. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
  628. }
  629. }
  630. break;
  631. case IMGFMT_RGB15:
  632. case IMGFMT_BGR15:
  633. {
  634. const int dr1= dither_2x2_8[y&1 ][0];
  635. const int dg1= dither_2x2_8[y&1 ][1];
  636. const int db1= dither_2x2_8[(y&1)^1][0];
  637. const int dr2= dither_2x2_8[y&1 ][1];
  638. const int dg2= dither_2x2_8[y&1 ][0];
  639. const int db2= dither_2x2_8[(y&1)^1][1];
  640. YSCALE_YUV_2_RGBX_C(uint16_t)
  641. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
  642. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
  643. }
  644. }
  645. break;
  646. case IMGFMT_RGB8:
  647. case IMGFMT_BGR8:
  648. {
  649. const uint8_t * const d64= dither_8x8_73[y&7];
  650. const uint8_t * const d32= dither_8x8_32[y&7];
  651. YSCALE_YUV_2_RGBX_C(uint8_t)
  652. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];
  653. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];
  654. }
  655. }
  656. break;
  657. case IMGFMT_RGB4:
  658. case IMGFMT_BGR4:
  659. {
  660. const uint8_t * const d64= dither_8x8_73 [y&7];
  661. const uint8_t * const d128=dither_8x8_220[y&7];
  662. YSCALE_YUV_2_RGBX_C(uint8_t)
  663. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];
  664. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];
  665. }
  666. }
  667. break;
  668. case IMGFMT_RGB1:
  669. case IMGFMT_BGR1:
  670. {
  671. const uint8_t * const d128=dither_8x8_220[y&7];
  672. uint8_t *g= c->table_gU[128] + c->table_gV[128];
  673. int acc=0;
  674. for(i=0; i<dstW-1; i+=2){
  675. int j;
  676. int Y1=0;
  677. int Y2=0;
  678. for(j=0; j<lumFilterSize; j++)
  679. {
  680. Y1 += lumSrc[j][i] * lumFilter[j];
  681. Y2 += lumSrc[j][i+1] * lumFilter[j];
  682. }
  683. Y1>>=19;
  684. Y2>>=19;
  685. if((Y1|Y2)&256)
  686. {
  687. if(Y1>255) Y1=255;
  688. else if(Y1<0)Y1=0;
  689. if(Y2>255) Y2=255;
  690. else if(Y2<0)Y2=0;
  691. }
  692. acc+= acc + g[Y1+d128[(i+0)&7]];
  693. acc+= acc + g[Y2+d128[(i+1)&7]];
  694. if((i&7)==6){
  695. ((uint8_t*)dest)[0]= acc;
  696. ((uint8_t*)dest)++;
  697. }
  698. }
  699. }
  700. break;
  701. }
  702. }
  703. //Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
  704. //Plain C versions
  705. #if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT)
  706. #define COMPILE_C
  707. #endif
  708. #ifdef CAN_COMPILE_X86_ASM
  709. #if (defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  710. #define COMPILE_MMX
  711. #endif
  712. #if defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)
  713. #define COMPILE_MMX2
  714. #endif
  715. #if (defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  716. #define COMPILE_3DNOW
  717. #endif
  718. #endif //CAN_COMPILE_X86_ASM
  719. #undef HAVE_MMX
  720. #undef HAVE_MMX2
  721. #undef HAVE_3DNOW
  722. #ifdef COMPILE_C
  723. #undef HAVE_MMX
  724. #undef HAVE_MMX2
  725. #undef HAVE_3DNOW
  726. #define RENAME(a) a ## _C
  727. #include "swscale_template.c"
  728. #endif
  729. #ifdef CAN_COMPILE_X86_ASM
  730. //X86 versions
  731. /*
  732. #undef RENAME
  733. #undef HAVE_MMX
  734. #undef HAVE_MMX2
  735. #undef HAVE_3DNOW
  736. #define ARCH_X86
  737. #define RENAME(a) a ## _X86
  738. #include "swscale_template.c"
  739. */
  740. //MMX versions
  741. #ifdef COMPILE_MMX
  742. #undef RENAME
  743. #define HAVE_MMX
  744. #undef HAVE_MMX2
  745. #undef HAVE_3DNOW
  746. #define RENAME(a) a ## _MMX
  747. #include "swscale_template.c"
  748. #endif
  749. //MMX2 versions
  750. #ifdef COMPILE_MMX2
  751. #undef RENAME
  752. #define HAVE_MMX
  753. #define HAVE_MMX2
  754. #undef HAVE_3DNOW
  755. #define RENAME(a) a ## _MMX2
  756. #include "swscale_template.c"
  757. #endif
  758. //3DNOW versions
  759. #ifdef COMPILE_3DNOW
  760. #undef RENAME
  761. #define HAVE_MMX
  762. #undef HAVE_MMX2
  763. #define HAVE_3DNOW
  764. #define RENAME(a) a ## _3DNow
  765. #include "swscale_template.c"
  766. #endif
  767. #endif //CAN_COMPILE_X86_ASM
  768. // minor note: the HAVE_xyz is messed up after that line so dont use it
  769. // old global scaler, dont use for new code
  770. // will use sws_flags from the command line
  771. void SwScale_YV12slice(unsigned char* src[], int srcStride[], int srcSliceY ,
  772. int srcSliceH, uint8_t* dst[], int dstStride, int dstbpp,
  773. int srcW, int srcH, int dstW, int dstH){
  774. static SwsContext *context=NULL;
  775. int dstFormat;
  776. int dstStride3[3]= {dstStride, dstStride>>1, dstStride>>1};
  777. switch(dstbpp)
  778. {
  779. case 8 : dstFormat= IMGFMT_Y8; break;
  780. case 12: dstFormat= IMGFMT_YV12; break;
  781. case 15: dstFormat= IMGFMT_BGR15; break;
  782. case 16: dstFormat= IMGFMT_BGR16; break;
  783. case 24: dstFormat= IMGFMT_BGR24; break;
  784. case 32: dstFormat= IMGFMT_BGR32; break;
  785. default: return;
  786. }
  787. if(!context) context=getSwsContextFromCmdLine(srcW, srcH, IMGFMT_YV12, dstW, dstH, dstFormat);
  788. context->swScale(context, src, srcStride, srcSliceY, srcSliceH, dst, dstStride3);
  789. }
  790. void swsGetFlagsAndFilterFromCmdLine(int *flags, SwsFilter **srcFilterParam, SwsFilter **dstFilterParam)
  791. {
  792. static int firstTime=1;
  793. *flags=0;
  794. #ifdef ARCH_X86
  795. if(gCpuCaps.hasMMX)
  796. asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
  797. #endif
  798. if(firstTime)
  799. {
  800. firstTime=0;
  801. *flags= SWS_PRINT_INFO;
  802. }
  803. else if(verbose>1) *flags= SWS_PRINT_INFO;
  804. if(src_filter.lumH) freeVec(src_filter.lumH);
  805. if(src_filter.lumV) freeVec(src_filter.lumV);
  806. if(src_filter.chrH) freeVec(src_filter.chrH);
  807. if(src_filter.chrV) freeVec(src_filter.chrV);
  808. if(sws_lum_gblur!=0.0){
  809. src_filter.lumH= getGaussianVec(sws_lum_gblur, 3.0);
  810. src_filter.lumV= getGaussianVec(sws_lum_gblur, 3.0);
  811. }else{
  812. src_filter.lumH= getIdentityVec();
  813. src_filter.lumV= getIdentityVec();
  814. }
  815. if(sws_chr_gblur!=0.0){
  816. src_filter.chrH= getGaussianVec(sws_chr_gblur, 3.0);
  817. src_filter.chrV= getGaussianVec(sws_chr_gblur, 3.0);
  818. }else{
  819. src_filter.chrH= getIdentityVec();
  820. src_filter.chrV= getIdentityVec();
  821. }
  822. if(sws_chr_sharpen!=0.0){
  823. SwsVector *g= getConstVec(-1.0, 3);
  824. SwsVector *id= getConstVec(10.0/sws_chr_sharpen, 1);
  825. g->coeff[1]=2.0;
  826. addVec(id, g);
  827. convVec(src_filter.chrH, id);
  828. convVec(src_filter.chrV, id);
  829. freeVec(g);
  830. freeVec(id);
  831. }
  832. if(sws_lum_sharpen!=0.0){
  833. SwsVector *g= getConstVec(-1.0, 3);
  834. SwsVector *id= getConstVec(10.0/sws_lum_sharpen, 1);
  835. g->coeff[1]=2.0;
  836. addVec(id, g);
  837. convVec(src_filter.lumH, id);
  838. convVec(src_filter.lumV, id);
  839. freeVec(g);
  840. freeVec(id);
  841. }
  842. if(sws_chr_hshift)
  843. shiftVec(src_filter.chrH, sws_chr_hshift);
  844. if(sws_chr_vshift)
  845. shiftVec(src_filter.chrV, sws_chr_vshift);
  846. normalizeVec(src_filter.chrH, 1.0);
  847. normalizeVec(src_filter.chrV, 1.0);
  848. normalizeVec(src_filter.lumH, 1.0);
  849. normalizeVec(src_filter.lumV, 1.0);
  850. if(verbose > 1) printVec(src_filter.chrH);
  851. if(verbose > 1) printVec(src_filter.lumH);
  852. switch(sws_flags)
  853. {
  854. case 0: *flags|= SWS_FAST_BILINEAR; break;
  855. case 1: *flags|= SWS_BILINEAR; break;
  856. case 2: *flags|= SWS_BICUBIC; break;
  857. case 3: *flags|= SWS_X; break;
  858. case 4: *flags|= SWS_POINT; break;
  859. case 5: *flags|= SWS_AREA; break;
  860. case 6: *flags|= SWS_BICUBLIN; break;
  861. case 7: *flags|= SWS_GAUSS; break;
  862. case 8: *flags|= SWS_SINC; break;
  863. case 9: *flags|= SWS_LANCZOS; break;
  864. case 10:*flags|= SWS_SPLINE; break;
  865. default:*flags|= SWS_BILINEAR; break;
  866. }
  867. *srcFilterParam= &src_filter;
  868. *dstFilterParam= NULL;
  869. }
  870. // will use sws_flags & src_filter (from cmd line)
  871. SwsContext *getSwsContextFromCmdLine(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat)
  872. {
  873. int flags;
  874. SwsFilter *dstFilterParam, *srcFilterParam;
  875. swsGetFlagsAndFilterFromCmdLine(&flags, &srcFilterParam, &dstFilterParam);
  876. return getSwsContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat, flags, srcFilterParam, dstFilterParam);
  877. }
  878. static double getSplineCoeff(double a, double b, double c, double d, double dist)
  879. {
  880. // printf("%f %f %f %f %f\n", a,b,c,d,dist);
  881. if(dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
  882. else return getSplineCoeff( 0.0,
  883. b+ 2.0*c + 3.0*d,
  884. c + 3.0*d,
  885. -b- 3.0*c - 6.0*d,
  886. dist-1.0);
  887. }
  888. static inline void initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
  889. int srcW, int dstW, int filterAlign, int one, int flags,
  890. SwsVector *srcFilter, SwsVector *dstFilter)
  891. {
  892. int i;
  893. int filterSize;
  894. int filter2Size;
  895. int minFilterSize;
  896. double *filter=NULL;
  897. double *filter2=NULL;
  898. #ifdef ARCH_X86
  899. if(gCpuCaps.hasMMX)
  900. asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
  901. #endif
  902. // Note the +1 is for the MMXscaler which reads over the end
  903. *filterPos = (int16_t*)memalign(8, (dstW+1)*sizeof(int16_t));
  904. if(ABS(xInc - 0x10000) <10) // unscaled
  905. {
  906. int i;
  907. filterSize= 1;
  908. filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
  909. for(i=0; i<dstW*filterSize; i++) filter[i]=0;
  910. for(i=0; i<dstW; i++)
  911. {
  912. filter[i*filterSize]=1;
  913. (*filterPos)[i]=i;
  914. }
  915. }
  916. else if(flags&SWS_POINT) // lame looking point sampling mode
  917. {
  918. int i;
  919. int xDstInSrc;
  920. filterSize= 1;
  921. filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
  922. xDstInSrc= xInc/2 - 0x8000;
  923. for(i=0; i<dstW; i++)
  924. {
  925. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  926. (*filterPos)[i]= xx;
  927. filter[i]= 1.0;
  928. xDstInSrc+= xInc;
  929. }
  930. }
  931. else if((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
  932. {
  933. int i;
  934. int xDstInSrc;
  935. if (flags&SWS_BICUBIC) filterSize= 4;
  936. else if(flags&SWS_X ) filterSize= 4;
  937. else filterSize= 2; // SWS_BILINEAR / SWS_AREA
  938. filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
  939. xDstInSrc= xInc/2 - 0x8000;
  940. for(i=0; i<dstW; i++)
  941. {
  942. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  943. int j;
  944. (*filterPos)[i]= xx;
  945. //Bilinear upscale / linear interpolate / Area averaging
  946. for(j=0; j<filterSize; j++)
  947. {
  948. double d= ABS((xx<<16) - xDstInSrc)/(double)(1<<16);
  949. double coeff= 1.0 - d;
  950. if(coeff<0) coeff=0;
  951. filter[i*filterSize + j]= coeff;
  952. xx++;
  953. }
  954. xDstInSrc+= xInc;
  955. }
  956. }
  957. else
  958. {
  959. double xDstInSrc;
  960. double sizeFactor, filterSizeInSrc;
  961. const double xInc1= (double)xInc / (double)(1<<16);
  962. int param= (flags&SWS_PARAM_MASK)>>SWS_PARAM_SHIFT;
  963. if (flags&SWS_BICUBIC) sizeFactor= 4.0;
  964. else if(flags&SWS_X) sizeFactor= 8.0;
  965. else if(flags&SWS_AREA) sizeFactor= 1.0; //downscale only, for upscale it is bilinear
  966. else if(flags&SWS_GAUSS) sizeFactor= 8.0; // infinite ;)
  967. else if(flags&SWS_LANCZOS) sizeFactor= param ? 2.0*param : 6.0;
  968. else if(flags&SWS_SINC) sizeFactor= 20.0; // infinite ;)
  969. else if(flags&SWS_SPLINE) sizeFactor= 20.0; // infinite ;)
  970. else if(flags&SWS_BILINEAR) sizeFactor= 2.0;
  971. else {
  972. sizeFactor= 0.0; //GCC warning killer
  973. ASSERT(0)
  974. }
  975. if(xInc1 <= 1.0) filterSizeInSrc= sizeFactor; // upscale
  976. else filterSizeInSrc= sizeFactor*srcW / (double)dstW;
  977. filterSize= (int)ceil(1 + filterSizeInSrc); // will be reduced later if possible
  978. if(filterSize > srcW-2) filterSize=srcW-2;
  979. filter= (double*)memalign(16, dstW*sizeof(double)*filterSize);
  980. xDstInSrc= xInc1 / 2.0 - 0.5;
  981. for(i=0; i<dstW; i++)
  982. {
  983. int xx= (int)(xDstInSrc - (filterSize-1)*0.5 + 0.5);
  984. int j;
  985. (*filterPos)[i]= xx;
  986. for(j=0; j<filterSize; j++)
  987. {
  988. double d= ABS(xx - xDstInSrc)/filterSizeInSrc*sizeFactor;
  989. double coeff;
  990. if(flags & SWS_BICUBIC)
  991. {
  992. double A= param ? -param*0.01 : -0.60;
  993. // Equation is from VirtualDub
  994. if(d<1.0)
  995. coeff = (1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
  996. else if(d<2.0)
  997. coeff = (-4.0*A + 8.0*A*d - 5.0*A*d*d + A*d*d*d);
  998. else
  999. coeff=0.0;
  1000. }
  1001. /* else if(flags & SWS_X)
  1002. {
  1003. double p= param ? param*0.01 : 0.3;
  1004. coeff = d ? sin(d*PI)/(d*PI) : 1.0;
  1005. coeff*= pow(2.0, - p*d*d);
  1006. }*/
  1007. else if(flags & SWS_X)
  1008. {
  1009. double A= param ? param*0.1 : 1.0;
  1010. if(d<1.0)
  1011. coeff = cos(d*PI);
  1012. else
  1013. coeff=-1.0;
  1014. if(coeff<0.0) coeff= -pow(-coeff, A);
  1015. else coeff= pow( coeff, A);
  1016. coeff= coeff*0.5 + 0.5;
  1017. }
  1018. else if(flags & SWS_AREA)
  1019. {
  1020. double srcPixelSize= 1.0/xInc1;
  1021. if(d + srcPixelSize/2 < 0.5) coeff= 1.0;
  1022. else if(d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;
  1023. else coeff=0.0;
  1024. }
  1025. else if(flags & SWS_GAUSS)
  1026. {
  1027. double p= param ? param*0.1 : 3.0;
  1028. coeff = pow(2.0, - p*d*d);
  1029. }
  1030. else if(flags & SWS_SINC)
  1031. {
  1032. coeff = d ? sin(d*PI)/(d*PI) : 1.0;
  1033. }
  1034. else if(flags & SWS_LANCZOS)
  1035. {
  1036. double p= param ? param : 3.0;
  1037. coeff = d ? sin(d*PI)*sin(d*PI/p)/(d*d*PI*PI/p) : 1.0;
  1038. if(d>p) coeff=0;
  1039. }
  1040. else if(flags & SWS_BILINEAR)
  1041. {
  1042. coeff= 1.0 - d;
  1043. if(coeff<0) coeff=0;
  1044. }
  1045. else if(flags & SWS_SPLINE)
  1046. {
  1047. double p=-2.196152422706632;
  1048. coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, d);
  1049. }
  1050. else {
  1051. coeff= 0.0; //GCC warning killer
  1052. ASSERT(0)
  1053. }
  1054. filter[i*filterSize + j]= coeff;
  1055. xx++;
  1056. }
  1057. xDstInSrc+= xInc1;
  1058. }
  1059. }
  1060. /* apply src & dst Filter to filter -> filter2
  1061. free(filter);
  1062. */
  1063. ASSERT(filterSize>0)
  1064. filter2Size= filterSize;
  1065. if(srcFilter) filter2Size+= srcFilter->length - 1;
  1066. if(dstFilter) filter2Size+= dstFilter->length - 1;
  1067. ASSERT(filter2Size>0)
  1068. filter2= (double*)memalign(8, filter2Size*dstW*sizeof(double));
  1069. for(i=0; i<dstW; i++)
  1070. {
  1071. int j;
  1072. SwsVector scaleFilter;
  1073. SwsVector *outVec;
  1074. scaleFilter.coeff= filter + i*filterSize;
  1075. scaleFilter.length= filterSize;
  1076. if(srcFilter) outVec= getConvVec(srcFilter, &scaleFilter);
  1077. else outVec= &scaleFilter;
  1078. ASSERT(outVec->length == filter2Size)
  1079. //FIXME dstFilter
  1080. for(j=0; j<outVec->length; j++)
  1081. {
  1082. filter2[i*filter2Size + j]= outVec->coeff[j];
  1083. }
  1084. (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
  1085. if(outVec != &scaleFilter) freeVec(outVec);
  1086. }
  1087. free(filter); filter=NULL;
  1088. /* try to reduce the filter-size (step1 find size and shift left) */
  1089. // Assume its near normalized (*0.5 or *2.0 is ok but * 0.001 is not)
  1090. minFilterSize= 0;
  1091. for(i=dstW-1; i>=0; i--)
  1092. {
  1093. int min= filter2Size;
  1094. int j;
  1095. double cutOff=0.0;
  1096. /* get rid off near zero elements on the left by shifting left */
  1097. for(j=0; j<filter2Size; j++)
  1098. {
  1099. int k;
  1100. cutOff += ABS(filter2[i*filter2Size]);
  1101. if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
  1102. /* preserve Monotonicity because the core cant handle the filter otherwise */
  1103. if(i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
  1104. // Move filter coeffs left
  1105. for(k=1; k<filter2Size; k++)
  1106. filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
  1107. filter2[i*filter2Size + k - 1]= 0.0;
  1108. (*filterPos)[i]++;
  1109. }
  1110. cutOff=0.0;
  1111. /* count near zeros on the right */
  1112. for(j=filter2Size-1; j>0; j--)
  1113. {
  1114. cutOff += ABS(filter2[i*filter2Size + j]);
  1115. if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
  1116. min--;
  1117. }
  1118. if(min>minFilterSize) minFilterSize= min;
  1119. }
  1120. ASSERT(minFilterSize > 0)
  1121. filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
  1122. ASSERT(filterSize > 0)
  1123. filter= (double*)memalign(8, filterSize*dstW*sizeof(double));
  1124. *outFilterSize= filterSize;
  1125. if(flags&SWS_PRINT_INFO)
  1126. MSG_INFO("SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
  1127. /* try to reduce the filter-size (step2 reduce it) */
  1128. for(i=0; i<dstW; i++)
  1129. {
  1130. int j;
  1131. for(j=0; j<filterSize; j++)
  1132. {
  1133. if(j>=filter2Size) filter[i*filterSize + j]= 0.0;
  1134. else filter[i*filterSize + j]= filter2[i*filter2Size + j];
  1135. }
  1136. }
  1137. free(filter2); filter2=NULL;
  1138. //FIXME try to align filterpos if possible
  1139. //fix borders
  1140. for(i=0; i<dstW; i++)
  1141. {
  1142. int j;
  1143. if((*filterPos)[i] < 0)
  1144. {
  1145. // Move filter coeffs left to compensate for filterPos
  1146. for(j=1; j<filterSize; j++)
  1147. {
  1148. int left= MAX(j + (*filterPos)[i], 0);
  1149. filter[i*filterSize + left] += filter[i*filterSize + j];
  1150. filter[i*filterSize + j]=0;
  1151. }
  1152. (*filterPos)[i]= 0;
  1153. }
  1154. if((*filterPos)[i] + filterSize > srcW)
  1155. {
  1156. int shift= (*filterPos)[i] + filterSize - srcW;
  1157. // Move filter coeffs right to compensate for filterPos
  1158. for(j=filterSize-2; j>=0; j--)
  1159. {
  1160. int right= MIN(j + shift, filterSize-1);
  1161. filter[i*filterSize +right] += filter[i*filterSize +j];
  1162. filter[i*filterSize +j]=0;
  1163. }
  1164. (*filterPos)[i]= srcW - filterSize;
  1165. }
  1166. }
  1167. // Note the +1 is for the MMXscaler which reads over the end
  1168. *outFilter= (int16_t*)memalign(8, *outFilterSize*(dstW+1)*sizeof(int16_t));
  1169. memset(*outFilter, 0, *outFilterSize*(dstW+1)*sizeof(int16_t));
  1170. /* Normalize & Store in outFilter */
  1171. for(i=0; i<dstW; i++)
  1172. {
  1173. int j;
  1174. double sum=0;
  1175. double scale= one;
  1176. for(j=0; j<filterSize; j++)
  1177. {
  1178. sum+= filter[i*filterSize + j];
  1179. }
  1180. scale/= sum;
  1181. for(j=0; j<*outFilterSize; j++)
  1182. {
  1183. (*outFilter)[i*(*outFilterSize) + j]= (int)(filter[i*filterSize + j]*scale);
  1184. }
  1185. }
  1186. (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
  1187. for(i=0; i<*outFilterSize; i++)
  1188. {
  1189. int j= dstW*(*outFilterSize);
  1190. (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
  1191. }
  1192. free(filter);
  1193. }
  1194. #ifdef ARCH_X86
  1195. static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
  1196. {
  1197. uint8_t *fragmentA;
  1198. int imm8OfPShufW1A;
  1199. int imm8OfPShufW2A;
  1200. int fragmentLengthA;
  1201. uint8_t *fragmentB;
  1202. int imm8OfPShufW1B;
  1203. int imm8OfPShufW2B;
  1204. int fragmentLengthB;
  1205. int fragmentPos;
  1206. int xpos, i;
  1207. // create an optimized horizontal scaling routine
  1208. //code fragment
  1209. asm volatile(
  1210. "jmp 9f \n\t"
  1211. // Begin
  1212. "0: \n\t"
  1213. "movq (%%edx, %%eax), %%mm3 \n\t"
  1214. "movd (%%ecx, %%esi), %%mm0 \n\t"
  1215. "movd 1(%%ecx, %%esi), %%mm1 \n\t"
  1216. "punpcklbw %%mm7, %%mm1 \n\t"
  1217. "punpcklbw %%mm7, %%mm0 \n\t"
  1218. "pshufw $0xFF, %%mm1, %%mm1 \n\t"
  1219. "1: \n\t"
  1220. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  1221. "2: \n\t"
  1222. "psubw %%mm1, %%mm0 \n\t"
  1223. "movl 8(%%ebx, %%eax), %%esi \n\t"
  1224. "pmullw %%mm3, %%mm0 \n\t"
  1225. "psllw $7, %%mm1 \n\t"
  1226. "paddw %%mm1, %%mm0 \n\t"
  1227. "movq %%mm0, (%%edi, %%eax) \n\t"
  1228. "addl $8, %%eax \n\t"
  1229. // End
  1230. "9: \n\t"
  1231. // "int $3\n\t"
  1232. "leal 0b, %0 \n\t"
  1233. "leal 1b, %1 \n\t"
  1234. "leal 2b, %2 \n\t"
  1235. "decl %1 \n\t"
  1236. "decl %2 \n\t"
  1237. "subl %0, %1 \n\t"
  1238. "subl %0, %2 \n\t"
  1239. "leal 9b, %3 \n\t"
  1240. "subl %0, %3 \n\t"
  1241. :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
  1242. "=r" (fragmentLengthA)
  1243. );
  1244. asm volatile(
  1245. "jmp 9f \n\t"
  1246. // Begin
  1247. "0: \n\t"
  1248. "movq (%%edx, %%eax), %%mm3 \n\t"
  1249. "movd (%%ecx, %%esi), %%mm0 \n\t"
  1250. "punpcklbw %%mm7, %%mm0 \n\t"
  1251. "pshufw $0xFF, %%mm0, %%mm1 \n\t"
  1252. "1: \n\t"
  1253. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  1254. "2: \n\t"
  1255. "psubw %%mm1, %%mm0 \n\t"
  1256. "movl 8(%%ebx, %%eax), %%esi \n\t"
  1257. "pmullw %%mm3, %%mm0 \n\t"
  1258. "psllw $7, %%mm1 \n\t"
  1259. "paddw %%mm1, %%mm0 \n\t"
  1260. "movq %%mm0, (%%edi, %%eax) \n\t"
  1261. "addl $8, %%eax \n\t"
  1262. // End
  1263. "9: \n\t"
  1264. // "int $3\n\t"
  1265. "leal 0b, %0 \n\t"
  1266. "leal 1b, %1 \n\t"
  1267. "leal 2b, %2 \n\t"
  1268. "decl %1 \n\t"
  1269. "decl %2 \n\t"
  1270. "subl %0, %1 \n\t"
  1271. "subl %0, %2 \n\t"
  1272. "leal 9b, %3 \n\t"
  1273. "subl %0, %3 \n\t"
  1274. :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
  1275. "=r" (fragmentLengthB)
  1276. );
  1277. xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
  1278. fragmentPos=0;
  1279. for(i=0; i<dstW/numSplits; i++)
  1280. {
  1281. int xx=xpos>>16;
  1282. if((i&3) == 0)
  1283. {
  1284. int a=0;
  1285. int b=((xpos+xInc)>>16) - xx;
  1286. int c=((xpos+xInc*2)>>16) - xx;
  1287. int d=((xpos+xInc*3)>>16) - xx;
  1288. filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
  1289. filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
  1290. filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
  1291. filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
  1292. filterPos[i/2]= xx;
  1293. if(d+1<4)
  1294. {
  1295. int maxShift= 3-(d+1);
  1296. int shift=0;
  1297. memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
  1298. funnyCode[fragmentPos + imm8OfPShufW1B]=
  1299. (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
  1300. funnyCode[fragmentPos + imm8OfPShufW2B]=
  1301. a | (b<<2) | (c<<4) | (d<<6);
  1302. if(i+3>=dstW) shift=maxShift; //avoid overread
  1303. else if((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
  1304. if(shift && i>=shift)
  1305. {
  1306. funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
  1307. funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
  1308. filterPos[i/2]-=shift;
  1309. }
  1310. fragmentPos+= fragmentLengthB;
  1311. }
  1312. else
  1313. {
  1314. int maxShift= 3-d;
  1315. int shift=0;
  1316. memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
  1317. funnyCode[fragmentPos + imm8OfPShufW1A]=
  1318. funnyCode[fragmentPos + imm8OfPShufW2A]=
  1319. a | (b<<2) | (c<<4) | (d<<6);
  1320. if(i+4>=dstW) shift=maxShift; //avoid overread
  1321. else if((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //partial align
  1322. if(shift && i>=shift)
  1323. {
  1324. funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
  1325. funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
  1326. filterPos[i/2]-=shift;
  1327. }
  1328. fragmentPos+= fragmentLengthA;
  1329. }
  1330. funnyCode[fragmentPos]= RET;
  1331. }
  1332. xpos+=xInc;
  1333. }
  1334. filterPos[i/2]= xpos>>16; // needed to jump to the next part
  1335. }
  1336. #endif // ARCH_X86
  1337. //FIXME remove
  1338. void SwScale_Init(){
  1339. }
  1340. static void globalInit(){
  1341. // generating tables:
  1342. int i;
  1343. for(i=0; i<768; i++){
  1344. int c= MIN(MAX(i-256, 0), 255);
  1345. clip_table[i]=c;
  1346. }
  1347. cpuCaps= gCpuCaps;
  1348. #ifdef RUNTIME_CPUDETECT
  1349. #ifdef CAN_COMPILE_X86_ASM
  1350. // ordered per speed fasterst first
  1351. if(gCpuCaps.hasMMX2)
  1352. swScale= swScale_MMX2;
  1353. else if(gCpuCaps.has3DNow)
  1354. swScale= swScale_3DNow;
  1355. else if(gCpuCaps.hasMMX)
  1356. swScale= swScale_MMX;
  1357. else
  1358. swScale= swScale_C;
  1359. #else
  1360. swScale= swScale_C;
  1361. cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
  1362. #endif
  1363. #else //RUNTIME_CPUDETECT
  1364. #ifdef HAVE_MMX2
  1365. swScale= swScale_MMX2;
  1366. cpuCaps.has3DNow = 0;
  1367. #elif defined (HAVE_3DNOW)
  1368. swScale= swScale_3DNow;
  1369. cpuCaps.hasMMX2 = 0;
  1370. #elif defined (HAVE_MMX)
  1371. swScale= swScale_MMX;
  1372. cpuCaps.hasMMX2 = cpuCaps.has3DNow = 0;
  1373. #else
  1374. swScale= swScale_C;
  1375. cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
  1376. #endif
  1377. #endif //!RUNTIME_CPUDETECT
  1378. }
  1379. static void PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1380. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1381. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1382. /* Copy Y plane */
  1383. if(dstStride[0]==srcStride[0])
  1384. memcpy(dst, src[0], srcSliceH*dstStride[0]);
  1385. else
  1386. {
  1387. int i;
  1388. uint8_t *srcPtr= src[0];
  1389. uint8_t *dstPtr= dst;
  1390. for(i=0; i<srcSliceH; i++)
  1391. {
  1392. memcpy(dstPtr, srcPtr, srcStride[0]);
  1393. srcPtr+= srcStride[0];
  1394. dstPtr+= dstStride[0];
  1395. }
  1396. }
  1397. dst = dstParam[1] + dstStride[1]*srcSliceY;
  1398. if(c->srcFormat==IMGFMT_YV12)
  1399. interleaveBytes( src[1],src[2],dst,c->srcW,srcSliceH,srcStride[1],srcStride[2],dstStride[0] );
  1400. else /* I420 & IYUV */
  1401. interleaveBytes( src[2],src[1],dst,c->srcW,srcSliceH,srcStride[2],srcStride[1],dstStride[0] );
  1402. }
  1403. /* Warper functions for yuv2bgr */
  1404. static void planarYuvToBgr(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1405. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1406. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1407. if(c->srcFormat==IMGFMT_YV12)
  1408. yuv2rgb( dst,src[0],src[1],src[2],c->srcW,srcSliceH,dstStride[0],srcStride[0],srcStride[1] );
  1409. else /* I420 & IYUV */
  1410. yuv2rgb( dst,src[0],src[2],src[1],c->srcW,srcSliceH,dstStride[0],srcStride[0],srcStride[1] );
  1411. }
  1412. static void PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1413. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1414. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1415. if(c->srcFormat==IMGFMT_YV12)
  1416. yv12toyuy2( src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
  1417. else /* I420 & IYUV */
  1418. yv12toyuy2( src[0],src[2],src[1],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
  1419. }
  1420. /* {RGB,BGR}{15,16,24,32} -> {RGB,BGR}{15,16,24,32} */
  1421. static void rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1422. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1423. const int srcFormat= c->srcFormat;
  1424. const int dstFormat= c->dstFormat;
  1425. const int srcBpp= ((srcFormat&0xFF) + 7)>>3;
  1426. const int dstBpp= ((dstFormat&0xFF) + 7)>>3;
  1427. const int srcId= (srcFormat&0xFF)>>2; // 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8
  1428. const int dstId= (dstFormat&0xFF)>>2;
  1429. void (*conv)(const uint8_t *src, uint8_t *dst, unsigned src_size)=NULL;
  1430. /* BGR -> BGR */
  1431. if(isBGR(srcFormat) && isBGR(dstFormat)){
  1432. switch(srcId | (dstId<<4)){
  1433. case 0x34: conv= rgb16to15; break;
  1434. case 0x36: conv= rgb24to15; break;
  1435. case 0x38: conv= rgb32to15; break;
  1436. case 0x43: conv= rgb15to16; break;
  1437. case 0x46: conv= rgb24to16; break;
  1438. case 0x48: conv= rgb32to16; break;
  1439. case 0x63: conv= rgb15to24; break;
  1440. case 0x64: conv= rgb16to24; break;
  1441. case 0x68: conv= rgb32to24; break;
  1442. case 0x83: conv= rgb15to32; break;
  1443. case 0x84: conv= rgb16to32; break;
  1444. case 0x86: conv= rgb24to32; break;
  1445. default: MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1446. vo_format_name(srcFormat), vo_format_name(dstFormat)); break;
  1447. }
  1448. }else if(isBGR(srcFormat) && isRGB(dstFormat)){
  1449. switch(srcId | (dstId<<4)){
  1450. case 0x33: conv= rgb15tobgr15; break;
  1451. case 0x34: conv= rgb16tobgr15; break;
  1452. case 0x36: conv= rgb24tobgr15; break;
  1453. case 0x38: conv= rgb32tobgr15; break;
  1454. case 0x43: conv= rgb15tobgr16; break;
  1455. case 0x44: conv= rgb16tobgr16; break;
  1456. case 0x46: conv= rgb24tobgr16; break;
  1457. case 0x48: conv= rgb32tobgr16; break;
  1458. case 0x63: conv= rgb15tobgr24; break;
  1459. case 0x64: conv= rgb16tobgr24; break;
  1460. case 0x66: conv= rgb24tobgr24; break;
  1461. case 0x68: conv= rgb32tobgr24; break;
  1462. case 0x83: conv= rgb15tobgr32; break;
  1463. case 0x84: conv= rgb16tobgr32; break;
  1464. case 0x86: conv= rgb24tobgr32; break;
  1465. case 0x88: conv= rgb32tobgr32; break;
  1466. default: MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1467. vo_format_name(srcFormat), vo_format_name(dstFormat)); break;
  1468. }
  1469. }else if(isRGB(srcFormat) && isRGB(dstFormat)){
  1470. switch(srcId | (dstId<<4)){
  1471. case 0x34: conv= rgb16to15; break;
  1472. case 0x36: conv= rgb24to15; break;
  1473. case 0x38: conv= rgb32to15; break;
  1474. case 0x43: conv= rgb15to16; break;
  1475. case 0x46: conv= rgb24to16; break;
  1476. case 0x48: conv= rgb32to16; break;
  1477. case 0x63: conv= rgb15to24; break;
  1478. case 0x64: conv= rgb16to24; break;
  1479. case 0x68: conv= rgb32to24; break;
  1480. case 0x83: conv= rgb15to32; break;
  1481. case 0x84: conv= rgb16to32; break;
  1482. case 0x86: conv= rgb24to32; break;
  1483. default: MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1484. vo_format_name(srcFormat), vo_format_name(dstFormat)); break;
  1485. }
  1486. }else if(isRGB(srcFormat) && isBGR(dstFormat)){
  1487. switch(srcId | (dstId<<4)){
  1488. case 0x33: conv= rgb15tobgr15; break;
  1489. case 0x34: conv= rgb16tobgr15; break;
  1490. case 0x36: conv= rgb24tobgr15; break;
  1491. case 0x38: conv= rgb32tobgr15; break;
  1492. case 0x43: conv= rgb15tobgr16; break;
  1493. case 0x44: conv= rgb16tobgr16; break;
  1494. case 0x46: conv= rgb24tobgr16; break;
  1495. case 0x48: conv= rgb32tobgr16; break;
  1496. case 0x63: conv= rgb15tobgr24; break;
  1497. case 0x64: conv= rgb16tobgr24; break;
  1498. case 0x66: conv= rgb24tobgr24; break;
  1499. case 0x68: conv= rgb32tobgr24; break;
  1500. case 0x83: conv= rgb15tobgr32; break;
  1501. case 0x84: conv= rgb16tobgr32; break;
  1502. case 0x86: conv= rgb24tobgr32; break;
  1503. case 0x88: conv= rgb32tobgr32; break;
  1504. default: MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1505. vo_format_name(srcFormat), vo_format_name(dstFormat)); break;
  1506. }
  1507. }
  1508. if(dstStride[0]*srcBpp == srcStride[0]*dstBpp)
  1509. conv(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
  1510. else
  1511. {
  1512. int i;
  1513. uint8_t *srcPtr= src[0];
  1514. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1515. for(i=0; i<srcSliceH; i++)
  1516. {
  1517. conv(srcPtr, dstPtr, c->srcW*srcBpp);
  1518. srcPtr+= srcStride[0];
  1519. dstPtr+= dstStride[0];
  1520. }
  1521. }
  1522. }
  1523. static void bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1524. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1525. rgb24toyv12(
  1526. src[0],
  1527. dst[0]+ srcSliceY *dstStride[0],
  1528. dst[1]+(srcSliceY>>1)*dstStride[1],
  1529. dst[2]+(srcSliceY>>1)*dstStride[2],
  1530. c->srcW, srcSliceH,
  1531. dstStride[0], dstStride[1], srcStride[0]);
  1532. }
  1533. static void yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1534. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1535. int i;
  1536. /* copy Y */
  1537. if(srcStride[0]==dstStride[0])
  1538. memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
  1539. else{
  1540. uint8_t *srcPtr= src[0];
  1541. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1542. for(i=0; i<srcSliceH; i++)
  1543. {
  1544. memcpy(dstPtr, srcPtr, c->srcW);
  1545. srcPtr+= srcStride[0];
  1546. dstPtr+= dstStride[0];
  1547. }
  1548. }
  1549. if(c->dstFormat==IMGFMT_YV12){
  1550. planar2x(src[1], dst[1], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[1]);
  1551. planar2x(src[2], dst[2], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[2]);
  1552. }else{
  1553. planar2x(src[1], dst[2], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[2]);
  1554. planar2x(src[2], dst[1], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[1]);
  1555. }
  1556. }
  1557. /**
  1558. * bring pointers in YUV order instead of YVU
  1559. */
  1560. static inline void orderYUV(int format, uint8_t * sortedP[], int sortedStride[], uint8_t * p[], int stride[]){
  1561. if(format == IMGFMT_YV12 || format == IMGFMT_YVU9){
  1562. sortedP[0]= p[0];
  1563. sortedP[1]= p[1];
  1564. sortedP[2]= p[2];
  1565. sortedStride[0]= stride[0];
  1566. sortedStride[1]= stride[1];
  1567. sortedStride[2]= stride[2];
  1568. }
  1569. else if(isPacked(format) || isGray(format))
  1570. {
  1571. sortedP[0]= p[0];
  1572. sortedP[1]=
  1573. sortedP[2]= NULL;
  1574. sortedStride[0]= stride[0];
  1575. sortedStride[1]=
  1576. sortedStride[2]= 0;
  1577. }
  1578. else if(format == IMGFMT_I420)
  1579. {
  1580. sortedP[0]= p[0];
  1581. sortedP[1]= p[2];
  1582. sortedP[2]= p[1];
  1583. sortedStride[0]= stride[0];
  1584. sortedStride[1]= stride[2];
  1585. sortedStride[2]= stride[1];
  1586. }else{
  1587. MSG_ERR("internal error in orderYUV\n");
  1588. }
  1589. }
  1590. /* unscaled copy like stuff (assumes nearly identical formats) */
  1591. static void simpleCopy(SwsContext *c, uint8_t* srcParam[], int srcStrideParam[], int srcSliceY,
  1592. int srcSliceH, uint8_t* dstParam[], int dstStrideParam[]){
  1593. int srcStride[3];
  1594. int dstStride[3];
  1595. uint8_t *src[3];
  1596. uint8_t *dst[3];
  1597. orderYUV(c->srcFormat, src, srcStride, srcParam, srcStrideParam);
  1598. orderYUV(c->dstFormat, dst, dstStride, dstParam, dstStrideParam);
  1599. if(isPacked(c->srcFormat))
  1600. {
  1601. if(dstStride[0]==srcStride[0])
  1602. memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
  1603. else
  1604. {
  1605. int i;
  1606. uint8_t *srcPtr= src[0];
  1607. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1608. int length=0;
  1609. /* universal length finder */
  1610. while(length+c->srcW <= ABS(dstStride[0])
  1611. && length+c->srcW <= ABS(srcStride[0])) length+= c->srcW;
  1612. ASSERT(length!=0);
  1613. for(i=0; i<srcSliceH; i++)
  1614. {
  1615. memcpy(dstPtr, srcPtr, length);
  1616. srcPtr+= srcStride[0];
  1617. dstPtr+= dstStride[0];
  1618. }
  1619. }
  1620. }
  1621. else
  1622. { /* Planar YUV or gray */
  1623. int plane;
  1624. for(plane=0; plane<3; plane++)
  1625. {
  1626. int length= plane==0 ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
  1627. int y= plane==0 ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
  1628. int height= plane==0 ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
  1629. if((isGray(c->srcFormat) || isGray(c->dstFormat)) && plane>0)
  1630. {
  1631. if(!isGray(c->dstFormat))
  1632. memset(dst[plane], 128, dstStride[plane]*height);
  1633. }
  1634. else
  1635. {
  1636. if(dstStride[plane]==srcStride[plane])
  1637. memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
  1638. else
  1639. {
  1640. int i;
  1641. uint8_t *srcPtr= src[plane];
  1642. uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
  1643. for(i=0; i<height; i++)
  1644. {
  1645. memcpy(dstPtr, srcPtr, length);
  1646. srcPtr+= srcStride[plane];
  1647. dstPtr+= dstStride[plane];
  1648. }
  1649. }
  1650. }
  1651. }
  1652. }
  1653. }
  1654. static int remove_dup_fourcc(int fourcc)
  1655. {
  1656. switch(fourcc)
  1657. {
  1658. case IMGFMT_IYUV: return IMGFMT_I420;
  1659. case IMGFMT_Y8 : return IMGFMT_Y800;
  1660. case IMGFMT_IF09: return IMGFMT_YVU9;
  1661. default: return fourcc;
  1662. }
  1663. }
  1664. static void getSubSampleFactors(int *h, int *v, int format){
  1665. switch(format){
  1666. case IMGFMT_YUY2:
  1667. *h=1;
  1668. *v=0;
  1669. break;
  1670. case IMGFMT_YV12:
  1671. case IMGFMT_I420:
  1672. case IMGFMT_Y800: //FIXME remove after different subsamplings are fully implemented
  1673. *h=1;
  1674. *v=1;
  1675. break;
  1676. case IMGFMT_YVU9:
  1677. *h=2;
  1678. *v=2;
  1679. break;
  1680. default:
  1681. *h=0;
  1682. *v=0;
  1683. break;
  1684. }
  1685. }
  1686. SwsContext *getSwsContext(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int flags,
  1687. SwsFilter *srcFilter, SwsFilter *dstFilter){
  1688. SwsContext *c;
  1689. int i;
  1690. int usesFilter;
  1691. int unscaled, needsDither;
  1692. SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
  1693. #ifdef ARCH_X86
  1694. if(gCpuCaps.hasMMX)
  1695. asm volatile("emms\n\t"::: "memory");
  1696. #endif
  1697. if(swScale==NULL) globalInit();
  1698. //srcFormat= IMGFMT_Y800;
  1699. //dstFormat= IMGFMT_Y800;
  1700. /* avoid dupplicate Formats, so we dont need to check to much */
  1701. srcFormat = remove_dup_fourcc(srcFormat);
  1702. dstFormat = remove_dup_fourcc(dstFormat);
  1703. unscaled = (srcW == dstW && srcH == dstH);
  1704. needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
  1705. && (dstFormat&0xFF)<24
  1706. && ((dstFormat&0xFF)<(srcFormat&0xFF) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
  1707. if(!isSupportedIn(srcFormat))
  1708. {
  1709. MSG_ERR("swScaler: %s is not supported as input format\n", vo_format_name(srcFormat));
  1710. return NULL;
  1711. }
  1712. if(!isSupportedOut(dstFormat))
  1713. {
  1714. MSG_ERR("swScaler: %s is not supported as output format\n", vo_format_name(dstFormat));
  1715. return NULL;
  1716. }
  1717. /* sanity check */
  1718. if(srcW<4 || srcH<1 || dstW<8 || dstH<1) //FIXME check if these are enough and try to lowwer them after fixing the relevant parts of the code
  1719. {
  1720. MSG_ERR("swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
  1721. srcW, srcH, dstW, dstH);
  1722. return NULL;
  1723. }
  1724. if(!dstFilter) dstFilter= &dummyFilter;
  1725. if(!srcFilter) srcFilter= &dummyFilter;
  1726. c= memalign(64, sizeof(SwsContext));
  1727. memset(c, 0, sizeof(SwsContext));
  1728. c->srcW= srcW;
  1729. c->srcH= srcH;
  1730. c->dstW= dstW;
  1731. c->dstH= dstH;
  1732. c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
  1733. c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
  1734. c->flags= flags;
  1735. c->dstFormat= dstFormat;
  1736. c->srcFormat= srcFormat;
  1737. usesFilter=0;
  1738. if(dstFilter->lumV!=NULL && dstFilter->lumV->length>1) usesFilter=1;
  1739. if(dstFilter->lumH!=NULL && dstFilter->lumH->length>1) usesFilter=1;
  1740. if(dstFilter->chrV!=NULL && dstFilter->chrV->length>1) usesFilter=1;
  1741. if(dstFilter->chrH!=NULL && dstFilter->chrH->length>1) usesFilter=1;
  1742. if(srcFilter->lumV!=NULL && srcFilter->lumV->length>1) usesFilter=1;
  1743. if(srcFilter->lumH!=NULL && srcFilter->lumH->length>1) usesFilter=1;
  1744. if(srcFilter->chrV!=NULL && srcFilter->chrV->length>1) usesFilter=1;
  1745. if(srcFilter->chrH!=NULL && srcFilter->chrH->length>1) usesFilter=1;
  1746. getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
  1747. getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
  1748. // reuse chroma for 2 pixles rgb/bgr unless user wants full chroma interpolation
  1749. if((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
  1750. // drop some chroma lines if the user wants it
  1751. c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
  1752. c->chrSrcVSubSample+= c->vChrDrop;
  1753. // drop every 2. pixel for chroma calculation unless user wants full chroma
  1754. if((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP))
  1755. c->chrSrcHSubSample=1;
  1756. c->chrIntHSubSample= c->chrDstHSubSample;
  1757. c->chrIntVSubSample= c->chrSrcVSubSample;
  1758. // note the -((-x)>>y) is so that we allways round toward +inf
  1759. c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
  1760. c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
  1761. c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
  1762. c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
  1763. if(isBGR(dstFormat))
  1764. c->yuvTable= yuv2rgb_c_init(dstFormat & 0xFF, MODE_RGB, c->table_rV, c->table_gU, c->table_gV, c->table_bU);
  1765. if(isRGB(dstFormat))
  1766. c->yuvTable= yuv2rgb_c_init(dstFormat & 0xFF, MODE_BGR, c->table_rV, c->table_gU, c->table_gV, c->table_bU);
  1767. /* unscaled special Cases */
  1768. if(unscaled && !usesFilter)
  1769. {
  1770. /* yv12_to_nv12 */
  1771. if((srcFormat == IMGFMT_YV12||srcFormat==IMGFMT_I420)&&dstFormat == IMGFMT_NV12)
  1772. {
  1773. c->swScale= PlanarToNV12Wrapper;
  1774. if(flags&SWS_PRINT_INFO)
  1775. MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
  1776. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1777. return c;
  1778. }
  1779. /* yv12_to_yuy2 */
  1780. if((srcFormat == IMGFMT_YV12||srcFormat==IMGFMT_I420)&&dstFormat == IMGFMT_YUY2)
  1781. {
  1782. c->swScale= PlanarToYuy2Wrapper;
  1783. if(flags&SWS_PRINT_INFO)
  1784. MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
  1785. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1786. return c;
  1787. }
  1788. /* yuv2bgr */
  1789. if((srcFormat==IMGFMT_YV12 || srcFormat==IMGFMT_I420) && isBGR(dstFormat))
  1790. {
  1791. // FIXME multiple yuv2rgb converters wont work that way cuz that thing is full of globals&statics
  1792. //FIXME rgb vs. bgr ?
  1793. #ifdef WORDS_BIGENDIAN
  1794. if(dstFormat==IMGFMT_BGR32)
  1795. yuv2rgb_init( dstFormat&0xFF /* =bpp */, MODE_BGR);
  1796. else
  1797. yuv2rgb_init( dstFormat&0xFF /* =bpp */, MODE_RGB);
  1798. #else
  1799. yuv2rgb_init( dstFormat&0xFF /* =bpp */, MODE_RGB);
  1800. #endif
  1801. c->swScale= planarYuvToBgr;
  1802. if(flags&SWS_PRINT_INFO)
  1803. MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
  1804. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1805. return c;
  1806. }
  1807. /* simple copy */
  1808. if( srcFormat == dstFormat
  1809. || (srcFormat==IMGFMT_YV12 && dstFormat==IMGFMT_I420)
  1810. || (srcFormat==IMGFMT_I420 && dstFormat==IMGFMT_YV12)
  1811. || (isPlanarYUV(srcFormat) && isGray(dstFormat))
  1812. || (isPlanarYUV(dstFormat) && isGray(srcFormat))
  1813. )
  1814. {
  1815. c->swScale= simpleCopy;
  1816. if(flags&SWS_PRINT_INFO)
  1817. MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
  1818. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1819. return c;
  1820. }
  1821. if( srcFormat==IMGFMT_YVU9 && (dstFormat==IMGFMT_YV12 || dstFormat==IMGFMT_I420) )
  1822. {
  1823. c->swScale= yvu9toyv12Wrapper;
  1824. if(flags&SWS_PRINT_INFO)
  1825. MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
  1826. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1827. return c;
  1828. }
  1829. /* bgr24toYV12 */
  1830. if(srcFormat==IMGFMT_BGR24 && dstFormat==IMGFMT_YV12)
  1831. c->swScale= bgr24toyv12Wrapper;
  1832. /* rgb/bgr -> rgb/bgr (no dither needed forms) */
  1833. if( (isBGR(srcFormat) || isRGB(srcFormat))
  1834. && (isBGR(dstFormat) || isRGB(dstFormat))
  1835. && !needsDither)
  1836. c->swScale= rgb2rgbWrapper;
  1837. /* LQ converters if -sws 0 or -sws 4*/
  1838. if(c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
  1839. /* rgb/bgr -> rgb/bgr (dither needed forms) */
  1840. if( (isBGR(srcFormat) || isRGB(srcFormat))
  1841. && (isBGR(dstFormat) || isRGB(dstFormat))
  1842. && needsDither)
  1843. c->swScale= rgb2rgbWrapper;
  1844. }
  1845. if(c->swScale){
  1846. if(flags&SWS_PRINT_INFO)
  1847. MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
  1848. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1849. return c;
  1850. }
  1851. }
  1852. if(cpuCaps.hasMMX2)
  1853. {
  1854. c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
  1855. if(!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
  1856. {
  1857. if(flags&SWS_PRINT_INFO)
  1858. MSG_INFO("SwScaler: output Width is not a multiple of 32 -> no MMX2 scaler\n");
  1859. }
  1860. }
  1861. else
  1862. c->canMMX2BeUsed=0;
  1863. c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
  1864. c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
  1865. // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
  1866. // but only for the FAST_BILINEAR mode otherwise do correct scaling
  1867. // n-2 is the last chrominance sample available
  1868. // this is not perfect, but noone shuld notice the difference, the more correct variant
  1869. // would be like the vertical one, but that would require some special code for the
  1870. // first and last pixel
  1871. if(flags&SWS_FAST_BILINEAR)
  1872. {
  1873. if(c->canMMX2BeUsed)
  1874. {
  1875. c->lumXInc+= 20;
  1876. c->chrXInc+= 20;
  1877. }
  1878. //we dont use the x86asm scaler if mmx is available
  1879. else if(cpuCaps.hasMMX)
  1880. {
  1881. c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
  1882. c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
  1883. }
  1884. }
  1885. /* precalculate horizontal scaler filter coefficients */
  1886. {
  1887. const int filterAlign= cpuCaps.hasMMX ? 4 : 1;
  1888. initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
  1889. srcW , dstW, filterAlign, 1<<14,
  1890. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
  1891. srcFilter->lumH, dstFilter->lumH);
  1892. initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
  1893. c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
  1894. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
  1895. srcFilter->chrH, dstFilter->chrH);
  1896. #ifdef ARCH_X86
  1897. // cant downscale !!!
  1898. if(c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
  1899. {
  1900. c->lumMmx2Filter = (int16_t*)memalign(8, (dstW /8+8)*sizeof(int16_t));
  1901. c->chrMmx2Filter = (int16_t*)memalign(8, (c->chrDstW /4+8)*sizeof(int16_t));
  1902. c->lumMmx2FilterPos= (int32_t*)memalign(8, (dstW /2/8+8)*sizeof(int32_t));
  1903. c->chrMmx2FilterPos= (int32_t*)memalign(8, (c->chrDstW/2/4+8)*sizeof(int32_t));
  1904. initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
  1905. initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
  1906. }
  1907. #endif
  1908. } // Init Horizontal stuff
  1909. /* precalculate vertical scaler filter coefficients */
  1910. initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
  1911. srcH , dstH, 1, (1<<12)-4,
  1912. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
  1913. srcFilter->lumV, dstFilter->lumV);
  1914. initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
  1915. c->chrSrcH, c->chrDstH, 1, (1<<12)-4,
  1916. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
  1917. srcFilter->chrV, dstFilter->chrV);
  1918. // Calculate Buffer Sizes so that they wont run out while handling these damn slices
  1919. c->vLumBufSize= c->vLumFilterSize;
  1920. c->vChrBufSize= c->vChrFilterSize;
  1921. for(i=0; i<dstH; i++)
  1922. {
  1923. int chrI= i*c->chrDstH / dstH;
  1924. int nextSlice= MAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
  1925. ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
  1926. nextSlice&= ~3; // Slices start at boundaries which are divisable through 4
  1927. if(c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
  1928. c->vLumBufSize= nextSlice - c->vLumFilterPos[i ];
  1929. if(c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
  1930. c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
  1931. }
  1932. // allocate pixbufs (we use dynamic allocation because otherwise we would need to
  1933. c->lumPixBuf= (int16_t**)memalign(4, c->vLumBufSize*2*sizeof(int16_t*));
  1934. c->chrPixBuf= (int16_t**)memalign(4, c->vChrBufSize*2*sizeof(int16_t*));
  1935. //Note we need at least one pixel more at the end because of the mmx code (just in case someone wanna replace the 4000/8000)
  1936. for(i=0; i<c->vLumBufSize; i++)
  1937. c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= (uint16_t*)memalign(8, 4000);
  1938. for(i=0; i<c->vChrBufSize; i++)
  1939. c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= (uint16_t*)memalign(8, 8000);
  1940. //try to avoid drawing green stuff between the right end and the stride end
  1941. for(i=0; i<c->vLumBufSize; i++) memset(c->lumPixBuf[i], 0, 4000);
  1942. for(i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, 8000);
  1943. ASSERT(c->chrDstH <= dstH)
  1944. // pack filter data for mmx code
  1945. if(cpuCaps.hasMMX)
  1946. {
  1947. c->lumMmxFilter= (int16_t*)memalign(8, c->vLumFilterSize* dstH*4*sizeof(int16_t));
  1948. c->chrMmxFilter= (int16_t*)memalign(8, c->vChrFilterSize*c->chrDstH*4*sizeof(int16_t));
  1949. for(i=0; i<c->vLumFilterSize*dstH; i++)
  1950. c->lumMmxFilter[4*i]=c->lumMmxFilter[4*i+1]=c->lumMmxFilter[4*i+2]=c->lumMmxFilter[4*i+3]=
  1951. c->vLumFilter[i];
  1952. for(i=0; i<c->vChrFilterSize*c->chrDstH; i++)
  1953. c->chrMmxFilter[4*i]=c->chrMmxFilter[4*i+1]=c->chrMmxFilter[4*i+2]=c->chrMmxFilter[4*i+3]=
  1954. c->vChrFilter[i];
  1955. }
  1956. if(flags&SWS_PRINT_INFO)
  1957. {
  1958. #ifdef DITHER1XBPP
  1959. char *dither= " dithered";
  1960. #else
  1961. char *dither= "";
  1962. #endif
  1963. if(flags&SWS_FAST_BILINEAR)
  1964. MSG_INFO("\nSwScaler: FAST_BILINEAR scaler, ");
  1965. else if(flags&SWS_BILINEAR)
  1966. MSG_INFO("\nSwScaler: BILINEAR scaler, ");
  1967. else if(flags&SWS_BICUBIC)
  1968. MSG_INFO("\nSwScaler: BICUBIC scaler, ");
  1969. else if(flags&SWS_X)
  1970. MSG_INFO("\nSwScaler: Experimental scaler, ");
  1971. else if(flags&SWS_POINT)
  1972. MSG_INFO("\nSwScaler: Nearest Neighbor / POINT scaler, ");
  1973. else if(flags&SWS_AREA)
  1974. MSG_INFO("\nSwScaler: Area Averageing scaler, ");
  1975. else if(flags&SWS_BICUBLIN)
  1976. MSG_INFO("\nSwScaler: luma BICUBIC / chroma BILINEAR scaler, ");
  1977. else if(flags&SWS_GAUSS)
  1978. MSG_INFO("\nSwScaler: Gaussian scaler, ");
  1979. else if(flags&SWS_SINC)
  1980. MSG_INFO("\nSwScaler: Sinc scaler, ");
  1981. else if(flags&SWS_LANCZOS)
  1982. MSG_INFO("\nSwScaler: Lanczos scaler, ");
  1983. else if(flags&SWS_SPLINE)
  1984. MSG_INFO("\nSwScaler: Bicubic spline scaler, ");
  1985. else
  1986. MSG_INFO("\nSwScaler: ehh flags invalid?! ");
  1987. if(dstFormat==IMGFMT_BGR15 || dstFormat==IMGFMT_BGR16)
  1988. MSG_INFO("from %s to%s %s ",
  1989. vo_format_name(srcFormat), dither, vo_format_name(dstFormat));
  1990. else
  1991. MSG_INFO("from %s to %s ",
  1992. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1993. if(cpuCaps.hasMMX2)
  1994. MSG_INFO("using MMX2\n");
  1995. else if(cpuCaps.has3DNow)
  1996. MSG_INFO("using 3DNOW\n");
  1997. else if(cpuCaps.hasMMX)
  1998. MSG_INFO("using MMX\n");
  1999. else
  2000. MSG_INFO("using C\n");
  2001. }
  2002. if((flags & SWS_PRINT_INFO) && verbose)
  2003. {
  2004. if(cpuCaps.hasMMX)
  2005. {
  2006. if(c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
  2007. MSG_V("SwScaler: using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
  2008. else
  2009. {
  2010. if(c->hLumFilterSize==4)
  2011. MSG_V("SwScaler: using 4-tap MMX scaler for horizontal luminance scaling\n");
  2012. else if(c->hLumFilterSize==8)
  2013. MSG_V("SwScaler: using 8-tap MMX scaler for horizontal luminance scaling\n");
  2014. else
  2015. MSG_V("SwScaler: using n-tap MMX scaler for horizontal luminance scaling\n");
  2016. if(c->hChrFilterSize==4)
  2017. MSG_V("SwScaler: using 4-tap MMX scaler for horizontal chrominance scaling\n");
  2018. else if(c->hChrFilterSize==8)
  2019. MSG_V("SwScaler: using 8-tap MMX scaler for horizontal chrominance scaling\n");
  2020. else
  2021. MSG_V("SwScaler: using n-tap MMX scaler for horizontal chrominance scaling\n");
  2022. }
  2023. }
  2024. else
  2025. {
  2026. #ifdef ARCH_X86
  2027. MSG_V("SwScaler: using X86-Asm scaler for horizontal scaling\n");
  2028. #else
  2029. if(flags & SWS_FAST_BILINEAR)
  2030. MSG_V("SwScaler: using FAST_BILINEAR C scaler for horizontal scaling\n");
  2031. else
  2032. MSG_V("SwScaler: using C scaler for horizontal scaling\n");
  2033. #endif
  2034. }
  2035. if(isPlanarYUV(dstFormat))
  2036. {
  2037. if(c->vLumFilterSize==1)
  2038. MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", cpuCaps.hasMMX ? "MMX" : "C");
  2039. else
  2040. MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (YV12 like)\n", cpuCaps.hasMMX ? "MMX" : "C");
  2041. }
  2042. else
  2043. {
  2044. if(c->vLumFilterSize==1 && c->vChrFilterSize==2)
  2045. MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
  2046. "SwScaler: 2-tap scaler for vertical chrominance scaling (BGR)\n",cpuCaps.hasMMX ? "MMX" : "C");
  2047. else if(c->vLumFilterSize==2 && c->vChrFilterSize==2)
  2048. MSG_V("SwScaler: using 2-tap linear %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
  2049. else
  2050. MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
  2051. }
  2052. if(dstFormat==IMGFMT_BGR24)
  2053. MSG_V("SwScaler: using %s YV12->BGR24 Converter\n",
  2054. cpuCaps.hasMMX2 ? "MMX2" : (cpuCaps.hasMMX ? "MMX" : "C"));
  2055. else if(dstFormat==IMGFMT_BGR32)
  2056. MSG_V("SwScaler: using %s YV12->BGR32 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
  2057. else if(dstFormat==IMGFMT_BGR16)
  2058. MSG_V("SwScaler: using %s YV12->BGR16 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
  2059. else if(dstFormat==IMGFMT_BGR15)
  2060. MSG_V("SwScaler: using %s YV12->BGR15 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
  2061. MSG_V("SwScaler: %dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
  2062. }
  2063. if((flags & SWS_PRINT_INFO) && verbose>1)
  2064. {
  2065. MSG_DBG2("SwScaler:Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  2066. c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
  2067. MSG_DBG2("SwScaler:Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  2068. c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
  2069. }
  2070. c->swScale= swScale;
  2071. return c;
  2072. }
  2073. /**
  2074. * returns a normalized gaussian curve used to filter stuff
  2075. * quality=3 is high quality, lowwer is lowwer quality
  2076. */
  2077. SwsVector *getGaussianVec(double variance, double quality){
  2078. const int length= (int)(variance*quality + 0.5) | 1;
  2079. int i;
  2080. double *coeff= memalign(sizeof(double), length*sizeof(double));
  2081. double middle= (length-1)*0.5;
  2082. SwsVector *vec= malloc(sizeof(SwsVector));
  2083. vec->coeff= coeff;
  2084. vec->length= length;
  2085. for(i=0; i<length; i++)
  2086. {
  2087. double dist= i-middle;
  2088. coeff[i]= exp( -dist*dist/(2*variance*variance) ) / sqrt(2*variance*PI);
  2089. }
  2090. normalizeVec(vec, 1.0);
  2091. return vec;
  2092. }
  2093. SwsVector *getConstVec(double c, int length){
  2094. int i;
  2095. double *coeff= memalign(sizeof(double), length*sizeof(double));
  2096. SwsVector *vec= malloc(sizeof(SwsVector));
  2097. vec->coeff= coeff;
  2098. vec->length= length;
  2099. for(i=0; i<length; i++)
  2100. coeff[i]= c;
  2101. return vec;
  2102. }
  2103. SwsVector *getIdentityVec(void){
  2104. double *coeff= memalign(sizeof(double), sizeof(double));
  2105. SwsVector *vec= malloc(sizeof(SwsVector));
  2106. coeff[0]= 1.0;
  2107. vec->coeff= coeff;
  2108. vec->length= 1;
  2109. return vec;
  2110. }
  2111. void normalizeVec(SwsVector *a, double height){
  2112. int i;
  2113. double sum=0;
  2114. double inv;
  2115. for(i=0; i<a->length; i++)
  2116. sum+= a->coeff[i];
  2117. inv= height/sum;
  2118. for(i=0; i<a->length; i++)
  2119. a->coeff[i]*= height;
  2120. }
  2121. void scaleVec(SwsVector *a, double scalar){
  2122. int i;
  2123. for(i=0; i<a->length; i++)
  2124. a->coeff[i]*= scalar;
  2125. }
  2126. static SwsVector *getConvVec(SwsVector *a, SwsVector *b){
  2127. int length= a->length + b->length - 1;
  2128. double *coeff= memalign(sizeof(double), length*sizeof(double));
  2129. int i, j;
  2130. SwsVector *vec= malloc(sizeof(SwsVector));
  2131. vec->coeff= coeff;
  2132. vec->length= length;
  2133. for(i=0; i<length; i++) coeff[i]= 0.0;
  2134. for(i=0; i<a->length; i++)
  2135. {
  2136. for(j=0; j<b->length; j++)
  2137. {
  2138. coeff[i+j]+= a->coeff[i]*b->coeff[j];
  2139. }
  2140. }
  2141. return vec;
  2142. }
  2143. static SwsVector *sumVec(SwsVector *a, SwsVector *b){
  2144. int length= MAX(a->length, b->length);
  2145. double *coeff= memalign(sizeof(double), length*sizeof(double));
  2146. int i;
  2147. SwsVector *vec= malloc(sizeof(SwsVector));
  2148. vec->coeff= coeff;
  2149. vec->length= length;
  2150. for(i=0; i<length; i++) coeff[i]= 0.0;
  2151. for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  2152. for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
  2153. return vec;
  2154. }
  2155. static SwsVector *diffVec(SwsVector *a, SwsVector *b){
  2156. int length= MAX(a->length, b->length);
  2157. double *coeff= memalign(sizeof(double), length*sizeof(double));
  2158. int i;
  2159. SwsVector *vec= malloc(sizeof(SwsVector));
  2160. vec->coeff= coeff;
  2161. vec->length= length;
  2162. for(i=0; i<length; i++) coeff[i]= 0.0;
  2163. for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  2164. for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
  2165. return vec;
  2166. }
  2167. /* shift left / or right if "shift" is negative */
  2168. static SwsVector *getShiftedVec(SwsVector *a, int shift){
  2169. int length= a->length + ABS(shift)*2;
  2170. double *coeff= memalign(sizeof(double), length*sizeof(double));
  2171. int i;
  2172. SwsVector *vec= malloc(sizeof(SwsVector));
  2173. vec->coeff= coeff;
  2174. vec->length= length;
  2175. for(i=0; i<length; i++) coeff[i]= 0.0;
  2176. for(i=0; i<a->length; i++)
  2177. {
  2178. coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
  2179. }
  2180. return vec;
  2181. }
  2182. void shiftVec(SwsVector *a, int shift){
  2183. SwsVector *shifted= getShiftedVec(a, shift);
  2184. free(a->coeff);
  2185. a->coeff= shifted->coeff;
  2186. a->length= shifted->length;
  2187. free(shifted);
  2188. }
  2189. void addVec(SwsVector *a, SwsVector *b){
  2190. SwsVector *sum= sumVec(a, b);
  2191. free(a->coeff);
  2192. a->coeff= sum->coeff;
  2193. a->length= sum->length;
  2194. free(sum);
  2195. }
  2196. void subVec(SwsVector *a, SwsVector *b){
  2197. SwsVector *diff= diffVec(a, b);
  2198. free(a->coeff);
  2199. a->coeff= diff->coeff;
  2200. a->length= diff->length;
  2201. free(diff);
  2202. }
  2203. void convVec(SwsVector *a, SwsVector *b){
  2204. SwsVector *conv= getConvVec(a, b);
  2205. free(a->coeff);
  2206. a->coeff= conv->coeff;
  2207. a->length= conv->length;
  2208. free(conv);
  2209. }
  2210. SwsVector *cloneVec(SwsVector *a){
  2211. double *coeff= memalign(sizeof(double), a->length*sizeof(double));
  2212. int i;
  2213. SwsVector *vec= malloc(sizeof(SwsVector));
  2214. vec->coeff= coeff;
  2215. vec->length= a->length;
  2216. for(i=0; i<a->length; i++) coeff[i]= a->coeff[i];
  2217. return vec;
  2218. }
  2219. void printVec(SwsVector *a){
  2220. int i;
  2221. double max=0;
  2222. double min=0;
  2223. double range;
  2224. for(i=0; i<a->length; i++)
  2225. if(a->coeff[i]>max) max= a->coeff[i];
  2226. for(i=0; i<a->length; i++)
  2227. if(a->coeff[i]<min) min= a->coeff[i];
  2228. range= max - min;
  2229. for(i=0; i<a->length; i++)
  2230. {
  2231. int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
  2232. MSG_DBG2("%1.3f ", a->coeff[i]);
  2233. for(;x>0; x--) MSG_DBG2(" ");
  2234. MSG_DBG2("|\n");
  2235. }
  2236. }
  2237. void freeVec(SwsVector *a){
  2238. if(!a) return;
  2239. if(a->coeff) free(a->coeff);
  2240. a->coeff=NULL;
  2241. a->length=0;
  2242. free(a);
  2243. }
  2244. void freeSwsContext(SwsContext *c){
  2245. int i;
  2246. if(!c) return;
  2247. if(c->lumPixBuf)
  2248. {
  2249. for(i=0; i<c->vLumBufSize; i++)
  2250. {
  2251. if(c->lumPixBuf[i]) free(c->lumPixBuf[i]);
  2252. c->lumPixBuf[i]=NULL;
  2253. }
  2254. free(c->lumPixBuf);
  2255. c->lumPixBuf=NULL;
  2256. }
  2257. if(c->chrPixBuf)
  2258. {
  2259. for(i=0; i<c->vChrBufSize; i++)
  2260. {
  2261. if(c->chrPixBuf[i]) free(c->chrPixBuf[i]);
  2262. c->chrPixBuf[i]=NULL;
  2263. }
  2264. free(c->chrPixBuf);
  2265. c->chrPixBuf=NULL;
  2266. }
  2267. if(c->vLumFilter) free(c->vLumFilter);
  2268. c->vLumFilter = NULL;
  2269. if(c->vChrFilter) free(c->vChrFilter);
  2270. c->vChrFilter = NULL;
  2271. if(c->hLumFilter) free(c->hLumFilter);
  2272. c->hLumFilter = NULL;
  2273. if(c->hChrFilter) free(c->hChrFilter);
  2274. c->hChrFilter = NULL;
  2275. if(c->vLumFilterPos) free(c->vLumFilterPos);
  2276. c->vLumFilterPos = NULL;
  2277. if(c->vChrFilterPos) free(c->vChrFilterPos);
  2278. c->vChrFilterPos = NULL;
  2279. if(c->hLumFilterPos) free(c->hLumFilterPos);
  2280. c->hLumFilterPos = NULL;
  2281. if(c->hChrFilterPos) free(c->hChrFilterPos);
  2282. c->hChrFilterPos = NULL;
  2283. if(c->lumMmxFilter) free(c->lumMmxFilter);
  2284. c->lumMmxFilter = NULL;
  2285. if(c->chrMmxFilter) free(c->chrMmxFilter);
  2286. c->chrMmxFilter = NULL;
  2287. if(c->lumMmx2Filter) free(c->lumMmx2Filter);
  2288. c->lumMmx2Filter=NULL;
  2289. if(c->chrMmx2Filter) free(c->chrMmx2Filter);
  2290. c->chrMmx2Filter=NULL;
  2291. if(c->lumMmx2FilterPos) free(c->lumMmx2FilterPos);
  2292. c->lumMmx2FilterPos=NULL;
  2293. if(c->chrMmx2FilterPos) free(c->chrMmx2FilterPos);
  2294. c->chrMmx2FilterPos=NULL;
  2295. if(c->yuvTable) free(c->yuvTable);
  2296. c->yuvTable=NULL;
  2297. free(c);
  2298. }