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.

2691 lines
76KB

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