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.

1850 lines
51KB

  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, RGB32, RGB24, Y8, Y800
  17. supported output formats: YV12, I420, IYUV, BGR15, BGR16, BGR24, BGR32 (grayscale soon too)
  18. BGR15/16 support dithering
  19. unscaled special converters
  20. YV12/I420/IYUV -> BGR15/BGR16/BGR24/BGR32
  21. YV12/I420/IYUV -> YV12/I420/IYUV
  22. YUY2/BGR15/BGR16/BGR24/BGR32/RGB24/RGB32 -> same format
  23. */
  24. #include <inttypes.h>
  25. #include <string.h>
  26. #include <math.h>
  27. #include <stdio.h>
  28. #include "../config.h"
  29. #include "../mangle.h"
  30. #ifdef HAVE_MALLOC_H
  31. #include <malloc.h>
  32. #endif
  33. #include "swscale.h"
  34. #include "../cpudetect.h"
  35. #include "../bswap.h"
  36. #include "../libvo/img_format.h"
  37. #include "rgb2rgb.h"
  38. #undef MOVNTQ
  39. #undef PAVGB
  40. //#undef HAVE_MMX2
  41. //#define HAVE_3DNOW
  42. //#undef HAVE_MMX
  43. //#undef ARCH_X86
  44. #define DITHER1XBPP
  45. #define RET 0xC3 //near return opcode for X86
  46. #ifdef MP_DEBUG
  47. #define ASSERT(x) if(!(x)) { printf("ASSERT " #x " failed\n"); *((int*)0)=0; }
  48. #else
  49. #define ASSERT(x) ;
  50. #endif
  51. #ifdef M_PI
  52. #define PI M_PI
  53. #else
  54. #define PI 3.14159265358979323846
  55. #endif
  56. //FIXME replace this with something faster
  57. #define isPlanarYUV(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420)
  58. #define isYUV(x) ((x)==IMGFMT_YUY2 || isPlanarYUV(x))
  59. #define isHalfChrV(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420)
  60. #define isHalfChrH(x) ((x)==IMGFMT_YUY2 || (x)==IMGFMT_YV12 || (x)==IMGFMT_I420)
  61. #define isPacked(x) ((x)==IMGFMT_YUY2 || ((x)&IMGFMT_BGR_MASK)==IMGFMT_BGR || ((x)&IMGFMT_RGB_MASK)==IMGFMT_RGB)
  62. #define isGray(x) ((x)==IMGFMT_Y800)
  63. #define isSupportedIn(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 || (x)==IMGFMT_YUY2 \
  64. || (x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24\
  65. || (x)==IMGFMT_RGB32|| (x)==IMGFMT_RGB24\
  66. || (x)==IMGFMT_Y800)
  67. #define isSupportedOut(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 \
  68. || (x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15)
  69. #define isBGR(x) ((x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15)
  70. #define RGB2YUV_SHIFT 16
  71. #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
  72. #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
  73. #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  74. #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
  75. #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
  76. #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
  77. #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
  78. #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  79. #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
  80. extern int verbose; // defined in mplayer.c
  81. /*
  82. NOTES
  83. known BUGS with known cause (no bugreports please!, but patches are welcome :) )
  84. horizontal fast_bilinear MMX2 scaler reads 1-7 samples too much (might cause a sig11)
  85. Special versions: fast Y 1:1 scaling (no interpolation in y direction)
  86. TODO
  87. more intelligent missalignment avoidance for the horizontal scaler
  88. write special vertical cubic upscale version
  89. Optimize C code (yv12 / minmax)
  90. add support for packed pixel yuv input & output
  91. add support for Y8 output
  92. optimize bgr24 & bgr32
  93. add BGR4 output support
  94. write special BGR->BGR scaler
  95. deglobalize yuv2rgb*.c
  96. */
  97. #define ABS(a) ((a) > 0 ? (a) : (-(a)))
  98. #define MIN(a,b) ((a) > (b) ? (b) : (a))
  99. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  100. #ifdef ARCH_X86
  101. #define CAN_COMPILE_X86_ASM
  102. #endif
  103. #ifdef CAN_COMPILE_X86_ASM
  104. static uint64_t __attribute__((aligned(8))) yCoeff= 0x2568256825682568LL;
  105. static uint64_t __attribute__((aligned(8))) vrCoeff= 0x3343334333433343LL;
  106. static uint64_t __attribute__((aligned(8))) ubCoeff= 0x40cf40cf40cf40cfLL;
  107. static uint64_t __attribute__((aligned(8))) vgCoeff= 0xE5E2E5E2E5E2E5E2LL;
  108. static uint64_t __attribute__((aligned(8))) ugCoeff= 0xF36EF36EF36EF36ELL;
  109. static uint64_t __attribute__((aligned(8))) bF8= 0xF8F8F8F8F8F8F8F8LL;
  110. static uint64_t __attribute__((aligned(8))) bFC= 0xFCFCFCFCFCFCFCFCLL;
  111. static uint64_t __attribute__((aligned(8))) w400= 0x0400040004000400LL;
  112. static uint64_t __attribute__((aligned(8))) w80= 0x0080008000800080LL;
  113. static uint64_t __attribute__((aligned(8))) w10= 0x0010001000100010LL;
  114. static uint64_t __attribute__((aligned(8))) w02= 0x0002000200020002LL;
  115. static uint64_t __attribute__((aligned(8))) bm00001111=0x00000000FFFFFFFFLL;
  116. static uint64_t __attribute__((aligned(8))) bm00000111=0x0000000000FFFFFFLL;
  117. static uint64_t __attribute__((aligned(8))) bm11111000=0xFFFFFFFFFF000000LL;
  118. static uint64_t __attribute__((aligned(8))) bm01010101=0x00FF00FF00FF00FFLL;
  119. static volatile uint64_t __attribute__((aligned(8))) b5Dither;
  120. static volatile uint64_t __attribute__((aligned(8))) g5Dither;
  121. static volatile uint64_t __attribute__((aligned(8))) g6Dither;
  122. static volatile uint64_t __attribute__((aligned(8))) r5Dither;
  123. static uint64_t __attribute__((aligned(8))) dither4[2]={
  124. 0x0103010301030103LL,
  125. 0x0200020002000200LL,};
  126. static uint64_t __attribute__((aligned(8))) dither8[2]={
  127. 0x0602060206020602LL,
  128. 0x0004000400040004LL,};
  129. static uint64_t __attribute__((aligned(8))) b16Mask= 0x001F001F001F001FLL;
  130. static uint64_t __attribute__((aligned(8))) g16Mask= 0x07E007E007E007E0LL;
  131. static uint64_t __attribute__((aligned(8))) r16Mask= 0xF800F800F800F800LL;
  132. static uint64_t __attribute__((aligned(8))) b15Mask= 0x001F001F001F001FLL;
  133. static uint64_t __attribute__((aligned(8))) g15Mask= 0x03E003E003E003E0LL;
  134. static uint64_t __attribute__((aligned(8))) r15Mask= 0x7C007C007C007C00LL;
  135. static uint64_t __attribute__((aligned(8))) M24A= 0x00FF0000FF0000FFLL;
  136. static uint64_t __attribute__((aligned(8))) M24B= 0xFF0000FF0000FF00LL;
  137. static uint64_t __attribute__((aligned(8))) M24C= 0x0000FF0000FF0000LL;
  138. // FIXME remove
  139. static uint64_t __attribute__((aligned(8))) asm_yalpha1;
  140. static uint64_t __attribute__((aligned(8))) asm_uvalpha1;
  141. #endif
  142. // clipping helper table for C implementations:
  143. static unsigned char clip_table[768];
  144. static unsigned short clip_table16b[768];
  145. static unsigned short clip_table16g[768];
  146. static unsigned short clip_table16r[768];
  147. static unsigned short clip_table15b[768];
  148. static unsigned short clip_table15g[768];
  149. static unsigned short clip_table15r[768];
  150. // yuv->rgb conversion tables:
  151. static int yuvtab_2568[256];
  152. static int yuvtab_3343[256];
  153. static int yuvtab_0c92[256];
  154. static int yuvtab_1a1e[256];
  155. static int yuvtab_40cf[256];
  156. // Needed for cubic scaler to catch overflows
  157. static int clip_yuvtab_2568[768];
  158. static int clip_yuvtab_3343[768];
  159. static int clip_yuvtab_0c92[768];
  160. static int clip_yuvtab_1a1e[768];
  161. static int clip_yuvtab_40cf[768];
  162. //global sws_flags from the command line
  163. int sws_flags=2;
  164. //global srcFilter
  165. SwsFilter src_filter= {NULL, NULL, NULL, NULL};
  166. float sws_lum_gblur= 0.0;
  167. float sws_chr_gblur= 0.0;
  168. int sws_chr_vshift= 0;
  169. int sws_chr_hshift= 0;
  170. float sws_chr_sharpen= 0.0;
  171. float sws_lum_sharpen= 0.0;
  172. /* cpuCaps combined from cpudetect and whats actually compiled in
  173. (if there is no support for something compiled in it wont appear here) */
  174. static CpuCaps cpuCaps;
  175. void (*swScale)(SwsContext *context, uint8_t* src[], int srcStride[], int srcSliceY,
  176. int srcSliceH, uint8_t* dst[], int dstStride[])=NULL;
  177. static SwsVector *getConvVec(SwsVector *a, SwsVector *b);
  178. #ifdef CAN_COMPILE_X86_ASM
  179. void in_asm_used_var_warning_killer()
  180. {
  181. volatile int i= yCoeff+vrCoeff+ubCoeff+vgCoeff+ugCoeff+bF8+bFC+w400+w80+w10+
  182. bm00001111+bm00000111+bm11111000+b16Mask+g16Mask+r16Mask+b15Mask+g15Mask+r15Mask+asm_yalpha1+ asm_uvalpha1+
  183. M24A+M24B+M24C+w02 + b5Dither+g5Dither+r5Dither+g6Dither+dither4[0]+dither8[0]+bm01010101;
  184. if(i) i=0;
  185. }
  186. #endif
  187. static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  188. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  189. uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW)
  190. {
  191. //FIXME Optimize (just quickly writen not opti..)
  192. int i;
  193. for(i=0; i<dstW; i++)
  194. {
  195. int val=0;
  196. int j;
  197. for(j=0; j<lumFilterSize; j++)
  198. val += lumSrc[j][i] * lumFilter[j];
  199. dest[i]= MIN(MAX(val>>19, 0), 255);
  200. }
  201. if(uDest != NULL)
  202. for(i=0; i<(dstW>>1); i++)
  203. {
  204. int u=0;
  205. int v=0;
  206. int j;
  207. for(j=0; j<chrFilterSize; j++)
  208. {
  209. u += chrSrc[j][i] * chrFilter[j];
  210. v += chrSrc[j][i + 2048] * chrFilter[j];
  211. }
  212. uDest[i]= MIN(MAX(u>>19, 0), 255);
  213. vDest[i]= MIN(MAX(v>>19, 0), 255);
  214. }
  215. }
  216. static inline void yuv2rgbXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  217. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  218. uint8_t *dest, int dstW, int dstFormat)
  219. {
  220. if(dstFormat==IMGFMT_BGR32)
  221. {
  222. int i;
  223. for(i=0; i<(dstW>>1); i++){
  224. int j;
  225. int Y1=0;
  226. int Y2=0;
  227. int U=0;
  228. int V=0;
  229. int Cb, Cr, Cg;
  230. for(j=0; j<lumFilterSize; j++)
  231. {
  232. Y1 += lumSrc[j][2*i] * lumFilter[j];
  233. Y2 += lumSrc[j][2*i+1] * lumFilter[j];
  234. }
  235. for(j=0; j<chrFilterSize; j++)
  236. {
  237. U += chrSrc[j][i] * chrFilter[j];
  238. V += chrSrc[j][i+2048] * chrFilter[j];
  239. }
  240. Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
  241. Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
  242. U >>= 19;
  243. V >>= 19;
  244. Cb= clip_yuvtab_40cf[U+ 256];
  245. Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
  246. Cr= clip_yuvtab_3343[V+ 256];
  247. dest[8*i+0]=clip_table[((Y1 + Cb) >>13)];
  248. dest[8*i+1]=clip_table[((Y1 + Cg) >>13)];
  249. dest[8*i+2]=clip_table[((Y1 + Cr) >>13)];
  250. dest[8*i+4]=clip_table[((Y2 + Cb) >>13)];
  251. dest[8*i+5]=clip_table[((Y2 + Cg) >>13)];
  252. dest[8*i+6]=clip_table[((Y2 + Cr) >>13)];
  253. }
  254. }
  255. else if(dstFormat==IMGFMT_BGR24)
  256. {
  257. int i;
  258. for(i=0; i<(dstW>>1); i++){
  259. int j;
  260. int Y1=0;
  261. int Y2=0;
  262. int U=0;
  263. int V=0;
  264. int Cb, Cr, Cg;
  265. for(j=0; j<lumFilterSize; j++)
  266. {
  267. Y1 += lumSrc[j][2*i] * lumFilter[j];
  268. Y2 += lumSrc[j][2*i+1] * lumFilter[j];
  269. }
  270. for(j=0; j<chrFilterSize; j++)
  271. {
  272. U += chrSrc[j][i] * chrFilter[j];
  273. V += chrSrc[j][i+2048] * chrFilter[j];
  274. }
  275. Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
  276. Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
  277. U >>= 19;
  278. V >>= 19;
  279. Cb= clip_yuvtab_40cf[U+ 256];
  280. Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
  281. Cr= clip_yuvtab_3343[V+ 256];
  282. dest[0]=clip_table[((Y1 + Cb) >>13)];
  283. dest[1]=clip_table[((Y1 + Cg) >>13)];
  284. dest[2]=clip_table[((Y1 + Cr) >>13)];
  285. dest[3]=clip_table[((Y2 + Cb) >>13)];
  286. dest[4]=clip_table[((Y2 + Cg) >>13)];
  287. dest[5]=clip_table[((Y2 + Cr) >>13)];
  288. dest+=6;
  289. }
  290. }
  291. else if(dstFormat==IMGFMT_BGR16)
  292. {
  293. int i;
  294. #ifdef DITHER1XBPP
  295. static int ditherb1=1<<14;
  296. static int ditherg1=1<<13;
  297. static int ditherr1=2<<14;
  298. static int ditherb2=3<<14;
  299. static int ditherg2=3<<13;
  300. static int ditherr2=0<<14;
  301. ditherb1 ^= (1^2)<<14;
  302. ditherg1 ^= (1^2)<<13;
  303. ditherr1 ^= (1^2)<<14;
  304. ditherb2 ^= (3^0)<<14;
  305. ditherg2 ^= (3^0)<<13;
  306. ditherr2 ^= (3^0)<<14;
  307. #else
  308. const int ditherb1=0;
  309. const int ditherg1=0;
  310. const int ditherr1=0;
  311. const int ditherb2=0;
  312. const int ditherg2=0;
  313. const int ditherr2=0;
  314. #endif
  315. for(i=0; i<(dstW>>1); i++){
  316. int j;
  317. int Y1=0;
  318. int Y2=0;
  319. int U=0;
  320. int V=0;
  321. int Cb, Cr, Cg;
  322. for(j=0; j<lumFilterSize; j++)
  323. {
  324. Y1 += lumSrc[j][2*i] * lumFilter[j];
  325. Y2 += lumSrc[j][2*i+1] * lumFilter[j];
  326. }
  327. for(j=0; j<chrFilterSize; j++)
  328. {
  329. U += chrSrc[j][i] * chrFilter[j];
  330. V += chrSrc[j][i+2048] * chrFilter[j];
  331. }
  332. Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
  333. Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
  334. U >>= 19;
  335. V >>= 19;
  336. Cb= clip_yuvtab_40cf[U+ 256];
  337. Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
  338. Cr= clip_yuvtab_3343[V+ 256];
  339. ((uint16_t*)dest)[2*i] =
  340. clip_table16b[(Y1 + Cb + ditherb1) >>13] |
  341. clip_table16g[(Y1 + Cg + ditherg1) >>13] |
  342. clip_table16r[(Y1 + Cr + ditherr1) >>13];
  343. ((uint16_t*)dest)[2*i+1] =
  344. clip_table16b[(Y2 + Cb + ditherb2) >>13] |
  345. clip_table16g[(Y2 + Cg + ditherg2) >>13] |
  346. clip_table16r[(Y2 + Cr + ditherr2) >>13];
  347. }
  348. }
  349. else if(dstFormat==IMGFMT_BGR15)
  350. {
  351. int i;
  352. #ifdef DITHER1XBPP
  353. static int ditherb1=1<<14;
  354. static int ditherg1=1<<14;
  355. static int ditherr1=2<<14;
  356. static int ditherb2=3<<14;
  357. static int ditherg2=3<<14;
  358. static int ditherr2=0<<14;
  359. ditherb1 ^= (1^2)<<14;
  360. ditherg1 ^= (1^2)<<14;
  361. ditherr1 ^= (1^2)<<14;
  362. ditherb2 ^= (3^0)<<14;
  363. ditherg2 ^= (3^0)<<14;
  364. ditherr2 ^= (3^0)<<14;
  365. #else
  366. const int ditherb1=0;
  367. const int ditherg1=0;
  368. const int ditherr1=0;
  369. const int ditherb2=0;
  370. const int ditherg2=0;
  371. const int ditherr2=0;
  372. #endif
  373. for(i=0; i<(dstW>>1); i++){
  374. int j;
  375. int Y1=0;
  376. int Y2=0;
  377. int U=0;
  378. int V=0;
  379. int Cb, Cr, Cg;
  380. for(j=0; j<lumFilterSize; j++)
  381. {
  382. Y1 += lumSrc[j][2*i] * lumFilter[j];
  383. Y2 += lumSrc[j][2*i+1] * lumFilter[j];
  384. }
  385. for(j=0; j<chrFilterSize; j++)
  386. {
  387. U += chrSrc[j][i] * chrFilter[j];
  388. V += chrSrc[j][i+2048] * chrFilter[j];
  389. }
  390. Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
  391. Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
  392. U >>= 19;
  393. V >>= 19;
  394. Cb= clip_yuvtab_40cf[U+ 256];
  395. Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
  396. Cr= clip_yuvtab_3343[V+ 256];
  397. ((uint16_t*)dest)[2*i] =
  398. clip_table15b[(Y1 + Cb + ditherb1) >>13] |
  399. clip_table15g[(Y1 + Cg + ditherg1) >>13] |
  400. clip_table15r[(Y1 + Cr + ditherr1) >>13];
  401. ((uint16_t*)dest)[2*i+1] =
  402. clip_table15b[(Y2 + Cb + ditherb2) >>13] |
  403. clip_table15g[(Y2 + Cg + ditherg2) >>13] |
  404. clip_table15r[(Y2 + Cr + ditherr2) >>13];
  405. }
  406. }
  407. }
  408. //Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
  409. //Plain C versions
  410. #if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT)
  411. #define COMPILE_C
  412. #endif
  413. #ifdef CAN_COMPILE_X86_ASM
  414. #if (defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  415. #define COMPILE_MMX
  416. #endif
  417. #if defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)
  418. #define COMPILE_MMX2
  419. #endif
  420. #if (defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  421. #define COMPILE_3DNOW
  422. #endif
  423. #endif //CAN_COMPILE_X86_ASM
  424. #undef HAVE_MMX
  425. #undef HAVE_MMX2
  426. #undef HAVE_3DNOW
  427. #ifdef COMPILE_C
  428. #undef HAVE_MMX
  429. #undef HAVE_MMX2
  430. #undef HAVE_3DNOW
  431. #define RENAME(a) a ## _C
  432. #include "swscale_template.c"
  433. #endif
  434. #ifdef CAN_COMPILE_X86_ASM
  435. //X86 versions
  436. /*
  437. #undef RENAME
  438. #undef HAVE_MMX
  439. #undef HAVE_MMX2
  440. #undef HAVE_3DNOW
  441. #define ARCH_X86
  442. #define RENAME(a) a ## _X86
  443. #include "swscale_template.c"
  444. */
  445. //MMX versions
  446. #ifdef COMPILE_MMX
  447. #undef RENAME
  448. #define HAVE_MMX
  449. #undef HAVE_MMX2
  450. #undef HAVE_3DNOW
  451. #define RENAME(a) a ## _MMX
  452. #include "swscale_template.c"
  453. #endif
  454. //MMX2 versions
  455. #ifdef COMPILE_MMX2
  456. #undef RENAME
  457. #define HAVE_MMX
  458. #define HAVE_MMX2
  459. #undef HAVE_3DNOW
  460. #define RENAME(a) a ## _MMX2
  461. #include "swscale_template.c"
  462. #endif
  463. //3DNOW versions
  464. #ifdef COMPILE_3DNOW
  465. #undef RENAME
  466. #define HAVE_MMX
  467. #undef HAVE_MMX2
  468. #define HAVE_3DNOW
  469. #define RENAME(a) a ## _3DNow
  470. #include "swscale_template.c"
  471. #endif
  472. #endif //CAN_COMPILE_X86_ASM
  473. // minor note: the HAVE_xyz is messed up after that line so dont use it
  474. // old global scaler, dont use for new code
  475. // will use sws_flags from the command line
  476. void SwScale_YV12slice(unsigned char* src[], int srcStride[], int srcSliceY ,
  477. int srcSliceH, uint8_t* dst[], int dstStride, int dstbpp,
  478. int srcW, int srcH, int dstW, int dstH){
  479. static SwsContext *context=NULL;
  480. int dstFormat;
  481. int dstStride3[3]= {dstStride, dstStride>>1, dstStride>>1};
  482. switch(dstbpp)
  483. {
  484. case 8 : dstFormat= IMGFMT_Y8; break;
  485. case 12: dstFormat= IMGFMT_YV12; break;
  486. case 15: dstFormat= IMGFMT_BGR15; break;
  487. case 16: dstFormat= IMGFMT_BGR16; break;
  488. case 24: dstFormat= IMGFMT_BGR24; break;
  489. case 32: dstFormat= IMGFMT_BGR32; break;
  490. default: return;
  491. }
  492. if(!context) context=getSwsContextFromCmdLine(srcW, srcH, IMGFMT_YV12, dstW, dstH, dstFormat);
  493. context->swScale(context, src, srcStride, srcSliceY, srcSliceH, dst, dstStride3);
  494. }
  495. // will use sws_flags & src_filter (from cmd line)
  496. SwsContext *getSwsContextFromCmdLine(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat)
  497. {
  498. int flags=0;
  499. static int firstTime=1;
  500. #ifdef ARCH_X86
  501. if(gCpuCaps.hasMMX)
  502. asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
  503. #endif
  504. if(firstTime)
  505. {
  506. firstTime=0;
  507. flags= SWS_PRINT_INFO;
  508. }
  509. else if(verbose>1) flags= SWS_PRINT_INFO;
  510. if(src_filter.lumH) freeVec(src_filter.lumH);
  511. if(src_filter.lumV) freeVec(src_filter.lumV);
  512. if(src_filter.chrH) freeVec(src_filter.chrH);
  513. if(src_filter.chrV) freeVec(src_filter.chrV);
  514. if(sws_lum_gblur!=0.0){
  515. src_filter.lumH= getGaussianVec(sws_lum_gblur, 3.0);
  516. src_filter.lumV= getGaussianVec(sws_lum_gblur, 3.0);
  517. }else{
  518. src_filter.lumH= getIdentityVec();
  519. src_filter.lumV= getIdentityVec();
  520. }
  521. if(sws_chr_gblur!=0.0){
  522. src_filter.chrH= getGaussianVec(sws_chr_gblur, 3.0);
  523. src_filter.chrV= getGaussianVec(sws_chr_gblur, 3.0);
  524. }else{
  525. src_filter.chrH= getIdentityVec();
  526. src_filter.chrV= getIdentityVec();
  527. }
  528. if(sws_chr_sharpen!=0.0){
  529. SwsVector *g= getConstVec(-1.0, 3);
  530. SwsVector *id= getConstVec(10.0/sws_chr_sharpen, 1);
  531. g->coeff[1]=2.0;
  532. addVec(id, g);
  533. convVec(src_filter.chrH, id);
  534. convVec(src_filter.chrV, id);
  535. freeVec(g);
  536. freeVec(id);
  537. }
  538. if(sws_lum_sharpen!=0.0){
  539. SwsVector *g= getConstVec(-1.0, 3);
  540. SwsVector *id= getConstVec(10.0/sws_lum_sharpen, 1);
  541. g->coeff[1]=2.0;
  542. addVec(id, g);
  543. convVec(src_filter.lumH, id);
  544. convVec(src_filter.lumV, id);
  545. freeVec(g);
  546. freeVec(id);
  547. }
  548. if(sws_chr_hshift)
  549. shiftVec(src_filter.chrH, sws_chr_hshift);
  550. if(sws_chr_vshift)
  551. shiftVec(src_filter.chrV, sws_chr_vshift);
  552. normalizeVec(src_filter.chrH, 1.0);
  553. normalizeVec(src_filter.chrV, 1.0);
  554. normalizeVec(src_filter.lumH, 1.0);
  555. normalizeVec(src_filter.lumV, 1.0);
  556. if(verbose > 1) printVec(src_filter.chrH);
  557. if(verbose > 1) printVec(src_filter.lumH);
  558. switch(sws_flags)
  559. {
  560. case 0: flags|= SWS_FAST_BILINEAR; break;
  561. case 1: flags|= SWS_BILINEAR; break;
  562. case 2: flags|= SWS_BICUBIC; break;
  563. case 3: flags|= SWS_X; break;
  564. case 4: flags|= SWS_POINT; break;
  565. case 5: flags|= SWS_AREA; break;
  566. default:flags|= SWS_BILINEAR; break;
  567. }
  568. return getSwsContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat, flags, &src_filter, NULL);
  569. }
  570. static inline void initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
  571. int srcW, int dstW, int filterAlign, int one, int flags,
  572. SwsVector *srcFilter, SwsVector *dstFilter)
  573. {
  574. int i;
  575. int filterSize;
  576. int filter2Size;
  577. int minFilterSize;
  578. double *filter=NULL;
  579. double *filter2=NULL;
  580. #ifdef ARCH_X86
  581. if(gCpuCaps.hasMMX)
  582. asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
  583. #endif
  584. // Note the +1 is for the MMXscaler which reads over the end
  585. *filterPos = (int16_t*)memalign(8, (dstW+1)*sizeof(int16_t));
  586. if(ABS(xInc - 0x10000) <10) // unscaled
  587. {
  588. int i;
  589. filterSize= 1;
  590. filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
  591. for(i=0; i<dstW*filterSize; i++) filter[i]=0;
  592. for(i=0; i<dstW; i++)
  593. {
  594. filter[i*filterSize]=1;
  595. (*filterPos)[i]=i;
  596. }
  597. }
  598. else if(flags&SWS_POINT) // lame looking point sampling mode
  599. {
  600. int i;
  601. int xDstInSrc;
  602. filterSize= 1;
  603. filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
  604. xDstInSrc= xInc/2 - 0x8000;
  605. for(i=0; i<dstW; i++)
  606. {
  607. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  608. (*filterPos)[i]= xx;
  609. filter[i]= 1.0;
  610. xDstInSrc+= xInc;
  611. }
  612. }
  613. else if(xInc <= (1<<16) || (flags&SWS_FAST_BILINEAR)) // upscale
  614. {
  615. int i;
  616. int xDstInSrc;
  617. if (flags&SWS_BICUBIC) filterSize= 4;
  618. else if(flags&SWS_X ) filterSize= 4;
  619. else filterSize= 2; // SWS_BILINEAR / SWS_AREA
  620. // printf("%d %d %d\n", filterSize, srcW, dstW);
  621. filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
  622. xDstInSrc= xInc/2 - 0x8000;
  623. for(i=0; i<dstW; i++)
  624. {
  625. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  626. int j;
  627. (*filterPos)[i]= xx;
  628. if((flags & SWS_BICUBIC) || (flags & SWS_X))
  629. {
  630. double d= ABS(((xx+1)<<16) - xDstInSrc)/(double)(1<<16);
  631. double y1,y2,y3,y4;
  632. double A= -0.6;
  633. if(flags & SWS_BICUBIC){
  634. // Equation is from VirtualDub
  635. y1 = ( + A*d - 2.0*A*d*d + A*d*d*d);
  636. y2 = (+ 1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
  637. y3 = ( - A*d + (2.0*A+3.0)*d*d - (A+2.0)*d*d*d);
  638. y4 = ( + A*d*d - A*d*d*d);
  639. }else{
  640. // cubic interpolation (derived it myself)
  641. y1 = ( -2.0*d + 3.0*d*d - 1.0*d*d*d)/6.0;
  642. y2 = (6.0 -3.0*d - 6.0*d*d + 3.0*d*d*d)/6.0;
  643. y3 = ( +6.0*d + 3.0*d*d - 3.0*d*d*d)/6.0;
  644. y4 = ( -1.0*d + 1.0*d*d*d)/6.0;
  645. }
  646. // printf("%d %d %d \n", coeff, (int)d, xDstInSrc);
  647. filter[i*filterSize + 0]= y1;
  648. filter[i*filterSize + 1]= y2;
  649. filter[i*filterSize + 2]= y3;
  650. filter[i*filterSize + 3]= y4;
  651. // printf("%1.3f %1.3f %1.3f %1.3f %1.3f\n",d , y1, y2, y3, y4);
  652. }
  653. else
  654. {
  655. //Bilinear upscale / linear interpolate / Area averaging
  656. for(j=0; j<filterSize; j++)
  657. {
  658. double d= ABS((xx<<16) - xDstInSrc)/(double)(1<<16);
  659. double coeff= 1.0 - d;
  660. if(coeff<0) coeff=0;
  661. // printf("%d %d %d \n", coeff, (int)d, xDstInSrc);
  662. filter[i*filterSize + j]= coeff;
  663. xx++;
  664. }
  665. }
  666. xDstInSrc+= xInc;
  667. }
  668. }
  669. else // downscale
  670. {
  671. int xDstInSrc;
  672. if(flags&SWS_BICUBIC) filterSize= (int)ceil(1 + 4.0*srcW / (double)dstW);
  673. else if(flags&SWS_X) filterSize= (int)ceil(1 + 4.0*srcW / (double)dstW);
  674. else if(flags&SWS_AREA) filterSize= (int)ceil(1 + 1.0*srcW / (double)dstW);
  675. else /* BILINEAR */ filterSize= (int)ceil(1 + 2.0*srcW / (double)dstW);
  676. // printf("%d %d %d\n", *filterSize, srcW, dstW);
  677. filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
  678. xDstInSrc= xInc/2 - 0x8000;
  679. for(i=0; i<dstW; i++)
  680. {
  681. int xx= (int)((double)xDstInSrc/(double)(1<<16) - (filterSize-1)*0.5 + 0.5);
  682. int j;
  683. (*filterPos)[i]= xx;
  684. for(j=0; j<filterSize; j++)
  685. {
  686. double d= ABS((xx<<16) - xDstInSrc)/(double)xInc;
  687. double coeff;
  688. if((flags & SWS_BICUBIC) || (flags & SWS_X))
  689. {
  690. double A= -0.75;
  691. // d*=2;
  692. // Equation is from VirtualDub
  693. if(d<1.0)
  694. coeff = (1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
  695. else if(d<2.0)
  696. coeff = (-4.0*A + 8.0*A*d - 5.0*A*d*d + A*d*d*d);
  697. else
  698. coeff=0.0;
  699. }
  700. else if(flags & SWS_AREA)
  701. {
  702. double srcPixelSize= (1<<16)/(double)xInc;
  703. if(d + srcPixelSize/2 < 0.5) coeff= 1.0;
  704. else if(d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;
  705. else coeff=0.0;
  706. }
  707. else
  708. {
  709. coeff= 1.0 - d;
  710. if(coeff<0) coeff=0;
  711. }
  712. // printf("%1.3f %2.3f %d \n", coeff, d, xDstInSrc);
  713. filter[i*filterSize + j]= coeff;
  714. xx++;
  715. }
  716. xDstInSrc+= xInc;
  717. }
  718. }
  719. /* apply src & dst Filter to filter -> filter2
  720. free(filter);
  721. */
  722. filter2Size= filterSize;
  723. if(srcFilter) filter2Size+= srcFilter->length - 1;
  724. if(dstFilter) filter2Size+= dstFilter->length - 1;
  725. filter2= (double*)memalign(8, filter2Size*dstW*sizeof(double));
  726. for(i=0; i<dstW; i++)
  727. {
  728. int j;
  729. SwsVector scaleFilter;
  730. SwsVector *outVec;
  731. scaleFilter.coeff= filter + i*filterSize;
  732. scaleFilter.length= filterSize;
  733. if(srcFilter) outVec= getConvVec(srcFilter, &scaleFilter);
  734. else outVec= &scaleFilter;
  735. ASSERT(outVec->length == filter2Size)
  736. //FIXME dstFilter
  737. for(j=0; j<outVec->length; j++)
  738. {
  739. filter2[i*filter2Size + j]= outVec->coeff[j];
  740. }
  741. (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
  742. if(outVec != &scaleFilter) freeVec(outVec);
  743. }
  744. free(filter); filter=NULL;
  745. /* try to reduce the filter-size (step1 find size and shift left) */
  746. // Assume its near normalized (*0.5 or *2.0 is ok but * 0.001 is not)
  747. minFilterSize= 0;
  748. for(i=dstW-1; i>=0; i--)
  749. {
  750. int min= filter2Size;
  751. int j;
  752. double cutOff=0.0;
  753. /* get rid off near zero elements on the left by shifting left */
  754. for(j=0; j<filter2Size; j++)
  755. {
  756. int k;
  757. cutOff += ABS(filter2[i*filter2Size]);
  758. if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
  759. /* preserve Monotonicity because the core cant handle the filter otherwise */
  760. if(i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
  761. // Move filter coeffs left
  762. for(k=1; k<filter2Size; k++)
  763. filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
  764. filter2[i*filter2Size + k - 1]= 0.0;
  765. (*filterPos)[i]++;
  766. }
  767. cutOff=0.0;
  768. /* count near zeros on the right */
  769. for(j=filter2Size-1; j>0; j--)
  770. {
  771. cutOff += ABS(filter2[i*filter2Size + j]);
  772. if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
  773. min--;
  774. }
  775. if(min>minFilterSize) minFilterSize= min;
  776. }
  777. filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
  778. filter= (double*)memalign(8, filterSize*dstW*sizeof(double));
  779. *outFilterSize= filterSize;
  780. if((flags&SWS_PRINT_INFO) && verbose)
  781. printf("SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
  782. /* try to reduce the filter-size (step2 reduce it) */
  783. for(i=0; i<dstW; i++)
  784. {
  785. int j;
  786. for(j=0; j<filterSize; j++)
  787. {
  788. if(j>=filter2Size) filter[i*filterSize + j]= 0.0;
  789. else filter[i*filterSize + j]= filter2[i*filter2Size + j];
  790. }
  791. }
  792. free(filter2); filter2=NULL;
  793. ASSERT(filterSize > 0)
  794. //FIXME try to align filterpos if possible
  795. //fix borders
  796. for(i=0; i<dstW; i++)
  797. {
  798. int j;
  799. if((*filterPos)[i] < 0)
  800. {
  801. // Move filter coeffs left to compensate for filterPos
  802. for(j=1; j<filterSize; j++)
  803. {
  804. int left= MAX(j + (*filterPos)[i], 0);
  805. filter[i*filterSize + left] += filter[i*filterSize + j];
  806. filter[i*filterSize + j]=0;
  807. }
  808. (*filterPos)[i]= 0;
  809. }
  810. if((*filterPos)[i] + filterSize > srcW)
  811. {
  812. int shift= (*filterPos)[i] + filterSize - srcW;
  813. // Move filter coeffs right to compensate for filterPos
  814. for(j=filterSize-2; j>=0; j--)
  815. {
  816. int right= MIN(j + shift, filterSize-1);
  817. filter[i*filterSize +right] += filter[i*filterSize +j];
  818. filter[i*filterSize +j]=0;
  819. }
  820. (*filterPos)[i]= srcW - filterSize;
  821. }
  822. }
  823. // Note the +1 is for the MMXscaler which reads over the end
  824. *outFilter= (int16_t*)memalign(8, *outFilterSize*(dstW+1)*sizeof(int16_t));
  825. memset(*outFilter, 0, *outFilterSize*(dstW+1)*sizeof(int16_t));
  826. /* Normalize & Store in outFilter */
  827. for(i=0; i<dstW; i++)
  828. {
  829. int j;
  830. double sum=0;
  831. double scale= one;
  832. for(j=0; j<filterSize; j++)
  833. {
  834. sum+= filter[i*filterSize + j];
  835. }
  836. scale/= sum;
  837. for(j=0; j<filterSize; j++)
  838. {
  839. (*outFilter)[i*(*outFilterSize) + j]= (int)(filter[i*filterSize + j]*scale);
  840. }
  841. }
  842. (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
  843. for(i=0; i<*outFilterSize; i++)
  844. {
  845. int j= dstW*(*outFilterSize);
  846. (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
  847. }
  848. free(filter);
  849. }
  850. #ifdef ARCH_X86
  851. static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode)
  852. {
  853. uint8_t *fragment;
  854. int imm8OfPShufW1;
  855. int imm8OfPShufW2;
  856. int fragmentLength;
  857. int xpos, i;
  858. // create an optimized horizontal scaling routine
  859. //code fragment
  860. asm volatile(
  861. "jmp 9f \n\t"
  862. // Begin
  863. "0: \n\t"
  864. "movq (%%esi), %%mm0 \n\t" //FIXME Alignment
  865. "movq %%mm0, %%mm1 \n\t"
  866. "psrlq $8, %%mm0 \n\t"
  867. "punpcklbw %%mm7, %%mm1 \n\t"
  868. "movq %%mm2, %%mm3 \n\t"
  869. "punpcklbw %%mm7, %%mm0 \n\t"
  870. "addw %%bx, %%cx \n\t" //2*xalpha += (4*lumXInc)&0xFFFF
  871. "pshufw $0xFF, %%mm1, %%mm1 \n\t"
  872. "1: \n\t"
  873. "adcl %%edx, %%esi \n\t" //xx+= (4*lumXInc)>>16 + carry
  874. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  875. "2: \n\t"
  876. "psrlw $9, %%mm3 \n\t"
  877. "psubw %%mm1, %%mm0 \n\t"
  878. "pmullw %%mm3, %%mm0 \n\t"
  879. "paddw %%mm6, %%mm2 \n\t" // 2*alpha += xpos&0xFFFF
  880. "psllw $7, %%mm1 \n\t"
  881. "paddw %%mm1, %%mm0 \n\t"
  882. "movq %%mm0, (%%edi, %%eax) \n\t"
  883. "addl $8, %%eax \n\t"
  884. // End
  885. "9: \n\t"
  886. // "int $3\n\t"
  887. "leal 0b, %0 \n\t"
  888. "leal 1b, %1 \n\t"
  889. "leal 2b, %2 \n\t"
  890. "decl %1 \n\t"
  891. "decl %2 \n\t"
  892. "subl %0, %1 \n\t"
  893. "subl %0, %2 \n\t"
  894. "leal 9b, %3 \n\t"
  895. "subl %0, %3 \n\t"
  896. :"=r" (fragment), "=r" (imm8OfPShufW1), "=r" (imm8OfPShufW2),
  897. "=r" (fragmentLength)
  898. );
  899. xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
  900. for(i=0; i<dstW/8; i++)
  901. {
  902. int xx=xpos>>16;
  903. if((i&3) == 0)
  904. {
  905. int a=0;
  906. int b=((xpos+xInc)>>16) - xx;
  907. int c=((xpos+xInc*2)>>16) - xx;
  908. int d=((xpos+xInc*3)>>16) - xx;
  909. memcpy(funnyCode + fragmentLength*i/4, fragment, fragmentLength);
  910. funnyCode[fragmentLength*i/4 + imm8OfPShufW1]=
  911. funnyCode[fragmentLength*i/4 + imm8OfPShufW2]=
  912. a | (b<<2) | (c<<4) | (d<<6);
  913. // if we dont need to read 8 bytes than dont :), reduces the chance of
  914. // crossing a cache line
  915. if(d<3) funnyCode[fragmentLength*i/4 + 1]= 0x6E;
  916. funnyCode[fragmentLength*(i+4)/4]= RET;
  917. }
  918. xpos+=xInc;
  919. }
  920. }
  921. #endif // ARCH_X86
  922. //FIXME remove
  923. void SwScale_Init(){
  924. }
  925. static void globalInit(){
  926. // generating tables:
  927. int i;
  928. for(i=0; i<768; i++){
  929. int c= MIN(MAX(i-256, 0), 255);
  930. clip_table[i]=c;
  931. yuvtab_2568[c]= clip_yuvtab_2568[i]=(0x2568*(c-16))+(256<<13);
  932. yuvtab_3343[c]= clip_yuvtab_3343[i]=0x3343*(c-128);
  933. yuvtab_0c92[c]= clip_yuvtab_0c92[i]=-0x0c92*(c-128);
  934. yuvtab_1a1e[c]= clip_yuvtab_1a1e[i]=-0x1a1e*(c-128);
  935. yuvtab_40cf[c]= clip_yuvtab_40cf[i]=0x40cf*(c-128);
  936. }
  937. for(i=0; i<768; i++)
  938. {
  939. int v= clip_table[i];
  940. clip_table16b[i]= le2me_16( v>>3);
  941. clip_table16g[i]= le2me_16((v<<3)&0x07E0);
  942. clip_table16r[i]= le2me_16((v<<8)&0xF800);
  943. clip_table15b[i]= le2me_16( v>>3);
  944. clip_table15g[i]= le2me_16((v<<2)&0x03E0);
  945. clip_table15r[i]= le2me_16((v<<7)&0x7C00);
  946. }
  947. cpuCaps= gCpuCaps;
  948. #ifdef RUNTIME_CPUDETECT
  949. #ifdef CAN_COMPILE_X86_ASM
  950. // ordered per speed fasterst first
  951. if(gCpuCaps.hasMMX2)
  952. swScale= swScale_MMX2;
  953. else if(gCpuCaps.has3DNow)
  954. swScale= swScale_3DNow;
  955. else if(gCpuCaps.hasMMX)
  956. swScale= swScale_MMX;
  957. else
  958. swScale= swScale_C;
  959. #else
  960. swScale= swScale_C;
  961. cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
  962. #endif
  963. #else //RUNTIME_CPUDETECT
  964. #ifdef HAVE_MMX2
  965. swScale= swScale_MMX2;
  966. cpuCaps.has3DNow = 0;
  967. #elif defined (HAVE_3DNOW)
  968. swScale= swScale_3DNow;
  969. cpuCaps.hasMMX2 = 0;
  970. #elif defined (HAVE_MMX)
  971. swScale= swScale_MMX;
  972. cpuCaps.hasMMX2 = cpuCaps.has3DNow = 0;
  973. #else
  974. swScale= swScale_C;
  975. cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
  976. #endif
  977. #endif //!RUNTIME_CPUDETECT
  978. }
  979. /* Warper functions for yuv2bgr */
  980. static void planarYuvToBgr(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  981. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  982. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  983. if(c->srcFormat==IMGFMT_YV12)
  984. yuv2rgb( dst,src[0],src[1],src[2],c->srcW,srcSliceH,dstStride[0],srcStride[0],srcStride[1] );
  985. else /* I420 & IYUV */
  986. yuv2rgb( dst,src[0],src[2],src[1],c->srcW,srcSliceH,dstStride[0],srcStride[0],srcStride[1] );
  987. }
  988. /* unscaled copy like stuff (assumes nearly identical formats) */
  989. static void simpleCopy(SwsContext *c, uint8_t* srcParam[], int srcStrideParam[], int srcSliceY,
  990. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  991. int srcStride[3];
  992. uint8_t *src[3];
  993. uint8_t *dst[3];
  994. if(c->srcFormat == IMGFMT_I420){
  995. src[0]= srcParam[0];
  996. src[1]= srcParam[2];
  997. src[2]= srcParam[1];
  998. srcStride[0]= srcStrideParam[0];
  999. srcStride[1]= srcStrideParam[2];
  1000. srcStride[2]= srcStrideParam[1];
  1001. }
  1002. else if(c->srcFormat==IMGFMT_YV12){
  1003. src[0]= srcParam[0];
  1004. src[1]= srcParam[1];
  1005. src[2]= srcParam[2];
  1006. srcStride[0]= srcStrideParam[0];
  1007. srcStride[1]= srcStrideParam[1];
  1008. srcStride[2]= srcStrideParam[2];
  1009. }
  1010. else if(isPacked(c->srcFormat) || isGray(c->srcFormat)){
  1011. src[0]= srcParam[0];
  1012. src[1]=
  1013. src[2]= NULL;
  1014. srcStride[0]= srcStrideParam[0];
  1015. srcStride[1]=
  1016. srcStride[2]= 0;
  1017. }
  1018. if(c->dstFormat == IMGFMT_I420){
  1019. dst[0]= dstParam[0];
  1020. dst[1]= dstParam[2];
  1021. dst[2]= dstParam[1];
  1022. }else{
  1023. dst[0]= dstParam[0];
  1024. dst[1]= dstParam[1];
  1025. dst[2]= dstParam[2];
  1026. }
  1027. if(isPacked(c->srcFormat))
  1028. {
  1029. if(dstStride[0]==srcStride[0])
  1030. memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
  1031. else
  1032. {
  1033. int i;
  1034. uint8_t *srcPtr= src[0];
  1035. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1036. int length=0;
  1037. /* universal length finder */
  1038. while(length+c->srcW <= dstStride[0]
  1039. && length+c->srcW <= srcStride[0]) length+= c->srcW;
  1040. ASSERT(length!=0);
  1041. for(i=0; i<srcSliceH; i++)
  1042. {
  1043. memcpy(dstPtr, srcPtr, length);
  1044. srcPtr+= srcStride[0];
  1045. dstPtr+= dstStride[0];
  1046. }
  1047. }
  1048. }
  1049. else
  1050. { /* Planar YUV */
  1051. int plane;
  1052. for(plane=0; plane<3; plane++)
  1053. {
  1054. int length= plane==0 ? c->srcW : ((c->srcW+1)>>1);
  1055. int y= plane==0 ? srcSliceY: ((srcSliceY+1)>>1);
  1056. int height= plane==0 ? srcSliceH: ((srcSliceH+1)>>1);
  1057. if(dstStride[plane]==srcStride[plane])
  1058. memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
  1059. else
  1060. {
  1061. int i;
  1062. uint8_t *srcPtr= src[plane];
  1063. uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
  1064. for(i=0; i<height; i++)
  1065. {
  1066. memcpy(dstPtr, srcPtr, length);
  1067. srcPtr+= srcStride[plane];
  1068. dstPtr+= dstStride[plane];
  1069. }
  1070. }
  1071. }
  1072. }
  1073. }
  1074. SwsContext *getSwsContext(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int flags,
  1075. SwsFilter *srcFilter, SwsFilter *dstFilter){
  1076. SwsContext *c;
  1077. int i;
  1078. int usesFilter;
  1079. SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
  1080. #ifdef ARCH_X86
  1081. if(gCpuCaps.hasMMX)
  1082. asm volatile("emms\n\t"::: "memory");
  1083. #endif
  1084. if(swScale==NULL) globalInit();
  1085. /* avoid dupplicate Formats, so we dont need to check to much */
  1086. if(srcFormat==IMGFMT_IYUV) srcFormat=IMGFMT_I420;
  1087. if(srcFormat==IMGFMT_Y8) srcFormat=IMGFMT_Y800;
  1088. if(dstFormat==IMGFMT_Y8) dstFormat=IMGFMT_Y800;
  1089. if(!isSupportedIn(srcFormat))
  1090. {
  1091. fprintf(stderr, "swScaler: %s is not supported as input format\n", vo_format_name(srcFormat));
  1092. return NULL;
  1093. }
  1094. if(!isSupportedOut(dstFormat))
  1095. {
  1096. fprintf(stderr, "swScaler: %s is not supported as output format\n", vo_format_name(dstFormat));
  1097. return NULL;
  1098. }
  1099. /* sanity check */
  1100. 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
  1101. {
  1102. fprintf(stderr, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
  1103. srcW, srcH, dstW, dstH);
  1104. return NULL;
  1105. }
  1106. if(!dstFilter) dstFilter= &dummyFilter;
  1107. if(!srcFilter) srcFilter= &dummyFilter;
  1108. c= memalign(64, sizeof(SwsContext));
  1109. memset(c, 0, sizeof(SwsContext));
  1110. c->srcW= srcW;
  1111. c->srcH= srcH;
  1112. c->dstW= dstW;
  1113. c->dstH= dstH;
  1114. c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
  1115. c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
  1116. c->flags= flags;
  1117. c->dstFormat= dstFormat;
  1118. c->srcFormat= srcFormat;
  1119. usesFilter=0;
  1120. if(dstFilter->lumV!=NULL && dstFilter->lumV->length>1) usesFilter=1;
  1121. if(dstFilter->lumH!=NULL && dstFilter->lumH->length>1) usesFilter=1;
  1122. if(dstFilter->chrV!=NULL && dstFilter->chrV->length>1) usesFilter=1;
  1123. if(dstFilter->chrH!=NULL && dstFilter->chrH->length>1) usesFilter=1;
  1124. if(srcFilter->lumV!=NULL && srcFilter->lumV->length>1) usesFilter=1;
  1125. if(srcFilter->lumH!=NULL && srcFilter->lumH->length>1) usesFilter=1;
  1126. if(srcFilter->chrV!=NULL && srcFilter->chrV->length>1) usesFilter=1;
  1127. if(srcFilter->chrH!=NULL && srcFilter->chrH->length>1) usesFilter=1;
  1128. /* special Cases */
  1129. if(srcW==dstW && srcH==dstH && !usesFilter)
  1130. {
  1131. /* yuv2bgr */
  1132. if(isPlanarYUV(srcFormat) && isBGR(dstFormat))
  1133. {
  1134. // FIXME multiple yuv2rgb converters wont work that way cuz that thing is full of globals&statics
  1135. yuv2rgb_init( dstFormat&0xFF /* =bpp */, MODE_RGB);
  1136. c->swScale= planarYuvToBgr;
  1137. if(flags&SWS_PRINT_INFO)
  1138. printf("SwScaler: using unscaled %s -> %s special converter\n",
  1139. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1140. return c;
  1141. }
  1142. /* simple copy */
  1143. if(srcFormat == dstFormat || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)))
  1144. {
  1145. c->swScale= simpleCopy;
  1146. if(flags&SWS_PRINT_INFO)
  1147. printf("SwScaler: using unscaled %s -> %s special converter\n",
  1148. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1149. return c;
  1150. }
  1151. }
  1152. if(cpuCaps.hasMMX2)
  1153. {
  1154. c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
  1155. if(!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
  1156. {
  1157. if(flags&SWS_PRINT_INFO)
  1158. fprintf(stderr, "SwScaler: output Width is not a multiple of 32 -> no MMX2 scaler\n");
  1159. }
  1160. }
  1161. else
  1162. c->canMMX2BeUsed=0;
  1163. /* dont use full vertical UV input/internaly if the source doesnt even have it */
  1164. if(isHalfChrV(srcFormat)) c->flags= flags= flags&(~SWS_FULL_CHR_V);
  1165. /* dont use full horizontal UV input if the source doesnt even have it */
  1166. if(isHalfChrH(srcFormat)) c->flags= flags= flags&(~SWS_FULL_CHR_H_INP);
  1167. /* dont use full horizontal UV internally if the destination doesnt even have it */
  1168. if(isHalfChrH(dstFormat)) c->flags= flags= flags&(~SWS_FULL_CHR_H_INT);
  1169. if(flags&SWS_FULL_CHR_H_INP) c->chrSrcW= srcW;
  1170. else c->chrSrcW= (srcW+1)>>1;
  1171. if(flags&SWS_FULL_CHR_H_INT) c->chrDstW= dstW;
  1172. else c->chrDstW= (dstW+1)>>1;
  1173. if(flags&SWS_FULL_CHR_V) c->chrSrcH= srcH;
  1174. else c->chrSrcH= (srcH+1)>>1;
  1175. if(isHalfChrV(dstFormat)) c->chrDstH= (dstH+1)>>1;
  1176. else c->chrDstH= dstH;
  1177. c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
  1178. c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
  1179. // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
  1180. // but only for the FAST_BILINEAR mode otherwise do correct scaling
  1181. // n-2 is the last chrominance sample available
  1182. // this is not perfect, but noone shuld notice the difference, the more correct variant
  1183. // would be like the vertical one, but that would require some special code for the
  1184. // first and last pixel
  1185. if(flags&SWS_FAST_BILINEAR)
  1186. {
  1187. if(c->canMMX2BeUsed)
  1188. {
  1189. c->lumXInc+= 20;
  1190. c->chrXInc+= 20;
  1191. }
  1192. //we dont use the x86asm scaler if mmx is available
  1193. else if(cpuCaps.hasMMX)
  1194. {
  1195. c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
  1196. c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
  1197. }
  1198. }
  1199. /* precalculate horizontal scaler filter coefficients */
  1200. {
  1201. const int filterAlign= cpuCaps.hasMMX ? 4 : 1;
  1202. initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
  1203. srcW , dstW, filterAlign, 1<<14, flags,
  1204. srcFilter->lumH, dstFilter->lumH);
  1205. initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
  1206. (srcW+1)>>1, c->chrDstW, filterAlign, 1<<14, flags,
  1207. srcFilter->chrH, dstFilter->chrH);
  1208. #ifdef ARCH_X86
  1209. // cant downscale !!!
  1210. if(c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
  1211. {
  1212. initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode);
  1213. initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode);
  1214. }
  1215. #endif
  1216. } // Init Horizontal stuff
  1217. /* precalculate vertical scaler filter coefficients */
  1218. initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
  1219. srcH , dstH, 1, (1<<12)-4, flags,
  1220. srcFilter->lumV, dstFilter->lumV);
  1221. initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
  1222. (srcH+1)>>1, c->chrDstH, 1, (1<<12)-4, flags,
  1223. srcFilter->chrV, dstFilter->chrV);
  1224. // Calculate Buffer Sizes so that they wont run out while handling these damn slices
  1225. c->vLumBufSize= c->vLumFilterSize;
  1226. c->vChrBufSize= c->vChrFilterSize;
  1227. for(i=0; i<dstH; i++)
  1228. {
  1229. int chrI= i*c->chrDstH / dstH;
  1230. int nextSlice= MAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
  1231. ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<1));
  1232. nextSlice&= ~1; // Slices start at even boundaries
  1233. if(c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
  1234. c->vLumBufSize= nextSlice - c->vLumFilterPos[i ];
  1235. if(c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>1))
  1236. c->vChrBufSize= (nextSlice>>1) - c->vChrFilterPos[chrI];
  1237. }
  1238. // allocate pixbufs (we use dynamic allocation because otherwise we would need to
  1239. c->lumPixBuf= (int16_t**)memalign(4, c->vLumBufSize*2*sizeof(int16_t*));
  1240. c->chrPixBuf= (int16_t**)memalign(4, c->vChrBufSize*2*sizeof(int16_t*));
  1241. //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)
  1242. for(i=0; i<c->vLumBufSize; i++)
  1243. c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= (uint16_t*)memalign(8, 4000);
  1244. for(i=0; i<c->vChrBufSize; i++)
  1245. c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= (uint16_t*)memalign(8, 8000);
  1246. //try to avoid drawing green stuff between the right end and the stride end
  1247. for(i=0; i<c->vLumBufSize; i++) memset(c->lumPixBuf[i], 0, 4000);
  1248. for(i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, 8000);
  1249. ASSERT(c->chrDstH <= dstH)
  1250. // pack filter data for mmx code
  1251. if(cpuCaps.hasMMX)
  1252. {
  1253. c->lumMmxFilter= (int16_t*)memalign(8, c->vLumFilterSize* dstH*4*sizeof(int16_t));
  1254. c->chrMmxFilter= (int16_t*)memalign(8, c->vChrFilterSize*c->chrDstH*4*sizeof(int16_t));
  1255. for(i=0; i<c->vLumFilterSize*dstH; i++)
  1256. c->lumMmxFilter[4*i]=c->lumMmxFilter[4*i+1]=c->lumMmxFilter[4*i+2]=c->lumMmxFilter[4*i+3]=
  1257. c->vLumFilter[i];
  1258. for(i=0; i<c->vChrFilterSize*c->chrDstH; i++)
  1259. c->chrMmxFilter[4*i]=c->chrMmxFilter[4*i+1]=c->chrMmxFilter[4*i+2]=c->chrMmxFilter[4*i+3]=
  1260. c->vChrFilter[i];
  1261. }
  1262. if(flags&SWS_PRINT_INFO)
  1263. {
  1264. #ifdef DITHER1XBPP
  1265. char *dither= " dithered";
  1266. #else
  1267. char *dither= "";
  1268. #endif
  1269. if(flags&SWS_FAST_BILINEAR)
  1270. fprintf(stderr, "\nSwScaler: FAST_BILINEAR scaler, ");
  1271. else if(flags&SWS_BILINEAR)
  1272. fprintf(stderr, "\nSwScaler: BILINEAR scaler, ");
  1273. else if(flags&SWS_BICUBIC)
  1274. fprintf(stderr, "\nSwScaler: BICUBIC scaler, ");
  1275. else if(flags&SWS_X)
  1276. fprintf(stderr, "\nSwScaler: Experimental scaler, ");
  1277. else if(flags&SWS_POINT)
  1278. fprintf(stderr, "\nSwScaler: Nearest Neighbor / POINT scaler, ");
  1279. else if(flags&SWS_AREA)
  1280. fprintf(stderr, "\nSwScaler: Area Averageing scaler, ");
  1281. else
  1282. fprintf(stderr, "\nSwScaler: ehh flags invalid?! ");
  1283. if(dstFormat==IMGFMT_BGR15 || dstFormat==IMGFMT_BGR16)
  1284. fprintf(stderr, "from %s to%s %s ",
  1285. vo_format_name(srcFormat), dither, vo_format_name(dstFormat));
  1286. else
  1287. fprintf(stderr, "from %s to %s ",
  1288. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1289. if(cpuCaps.hasMMX2)
  1290. fprintf(stderr, "using MMX2\n");
  1291. else if(cpuCaps.has3DNow)
  1292. fprintf(stderr, "using 3DNOW\n");
  1293. else if(cpuCaps.hasMMX)
  1294. fprintf(stderr, "using MMX\n");
  1295. else
  1296. fprintf(stderr, "using C\n");
  1297. }
  1298. if((flags & SWS_PRINT_INFO) && verbose)
  1299. {
  1300. if(cpuCaps.hasMMX)
  1301. {
  1302. if(c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
  1303. printf("SwScaler: using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
  1304. else
  1305. {
  1306. if(c->hLumFilterSize==4)
  1307. printf("SwScaler: using 4-tap MMX scaler for horizontal luminance scaling\n");
  1308. else if(c->hLumFilterSize==8)
  1309. printf("SwScaler: using 8-tap MMX scaler for horizontal luminance scaling\n");
  1310. else
  1311. printf("SwScaler: using n-tap MMX scaler for horizontal luminance scaling\n");
  1312. if(c->hChrFilterSize==4)
  1313. printf("SwScaler: using 4-tap MMX scaler for horizontal chrominance scaling\n");
  1314. else if(c->hChrFilterSize==8)
  1315. printf("SwScaler: using 8-tap MMX scaler for horizontal chrominance scaling\n");
  1316. else
  1317. printf("SwScaler: using n-tap MMX scaler for horizontal chrominance scaling\n");
  1318. }
  1319. }
  1320. else
  1321. {
  1322. #ifdef ARCH_X86
  1323. printf("SwScaler: using X86-Asm scaler for horizontal scaling\n");
  1324. #else
  1325. if(flags & SWS_FAST_BILINEAR)
  1326. printf("SwScaler: using FAST_BILINEAR C scaler for horizontal scaling\n");
  1327. else
  1328. printf("SwScaler: using C scaler for horizontal scaling\n");
  1329. #endif
  1330. }
  1331. if(isPlanarYUV(dstFormat))
  1332. {
  1333. if(c->vLumFilterSize==1)
  1334. printf("SwScaler: using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", cpuCaps.hasMMX ? "MMX" : "C");
  1335. else
  1336. printf("SwScaler: using n-tap %s scaler for vertical scaling (YV12 like)\n", cpuCaps.hasMMX ? "MMX" : "C");
  1337. }
  1338. else
  1339. {
  1340. if(c->vLumFilterSize==1 && c->vChrFilterSize==2)
  1341. printf("SwScaler: using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
  1342. "SwScaler: 2-tap scaler for vertical chrominance scaling (BGR)\n",cpuCaps.hasMMX ? "MMX" : "C");
  1343. else if(c->vLumFilterSize==2 && c->vChrFilterSize==2)
  1344. printf("SwScaler: using 2-tap linear %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
  1345. else
  1346. printf("SwScaler: using n-tap %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
  1347. }
  1348. if(dstFormat==IMGFMT_BGR24)
  1349. printf("SwScaler: using %s YV12->BGR24 Converter\n",
  1350. cpuCaps.hasMMX2 ? "MMX2" : (cpuCaps.hasMMX ? "MMX" : "C"));
  1351. else if(dstFormat==IMGFMT_BGR32)
  1352. printf("SwScaler: using %s YV12->BGR32 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
  1353. else if(dstFormat==IMGFMT_BGR16)
  1354. printf("SwScaler: using %s YV12->BGR16 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
  1355. else if(dstFormat==IMGFMT_BGR15)
  1356. printf("SwScaler: using %s YV12->BGR15 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
  1357. printf("SwScaler: %dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
  1358. }
  1359. if((flags & SWS_PRINT_INFO) && verbose>1)
  1360. {
  1361. printf("SwScaler:Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  1362. c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
  1363. printf("SwScaler:Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  1364. c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
  1365. }
  1366. c->swScale= swScale;
  1367. return c;
  1368. }
  1369. /**
  1370. * returns a normalized gaussian curve used to filter stuff
  1371. * quality=3 is high quality, lowwer is lowwer quality
  1372. */
  1373. SwsVector *getGaussianVec(double variance, double quality){
  1374. const int length= (int)(variance*quality + 0.5) | 1;
  1375. int i;
  1376. double *coeff= memalign(sizeof(double), length*sizeof(double));
  1377. double middle= (length-1)*0.5;
  1378. SwsVector *vec= malloc(sizeof(SwsVector));
  1379. vec->coeff= coeff;
  1380. vec->length= length;
  1381. for(i=0; i<length; i++)
  1382. {
  1383. double dist= i-middle;
  1384. coeff[i]= exp( -dist*dist/(2*variance*variance) ) / sqrt(2*variance*PI);
  1385. }
  1386. normalizeVec(vec, 1.0);
  1387. return vec;
  1388. }
  1389. SwsVector *getConstVec(double c, int length){
  1390. int i;
  1391. double *coeff= memalign(sizeof(double), length*sizeof(double));
  1392. SwsVector *vec= malloc(sizeof(SwsVector));
  1393. vec->coeff= coeff;
  1394. vec->length= length;
  1395. for(i=0; i<length; i++)
  1396. coeff[i]= c;
  1397. return vec;
  1398. }
  1399. SwsVector *getIdentityVec(void){
  1400. double *coeff= memalign(sizeof(double), sizeof(double));
  1401. SwsVector *vec= malloc(sizeof(SwsVector));
  1402. coeff[0]= 1.0;
  1403. vec->coeff= coeff;
  1404. vec->length= 1;
  1405. return vec;
  1406. }
  1407. void normalizeVec(SwsVector *a, double height){
  1408. int i;
  1409. double sum=0;
  1410. double inv;
  1411. for(i=0; i<a->length; i++)
  1412. sum+= a->coeff[i];
  1413. inv= height/sum;
  1414. for(i=0; i<a->length; i++)
  1415. a->coeff[i]*= height;
  1416. }
  1417. void scaleVec(SwsVector *a, double scalar){
  1418. int i;
  1419. for(i=0; i<a->length; i++)
  1420. a->coeff[i]*= scalar;
  1421. }
  1422. static SwsVector *getConvVec(SwsVector *a, SwsVector *b){
  1423. int length= a->length + b->length - 1;
  1424. double *coeff= memalign(sizeof(double), length*sizeof(double));
  1425. int i, j;
  1426. SwsVector *vec= malloc(sizeof(SwsVector));
  1427. vec->coeff= coeff;
  1428. vec->length= length;
  1429. for(i=0; i<length; i++) coeff[i]= 0.0;
  1430. for(i=0; i<a->length; i++)
  1431. {
  1432. for(j=0; j<b->length; j++)
  1433. {
  1434. coeff[i+j]+= a->coeff[i]*b->coeff[j];
  1435. }
  1436. }
  1437. return vec;
  1438. }
  1439. static SwsVector *sumVec(SwsVector *a, SwsVector *b){
  1440. int length= MAX(a->length, b->length);
  1441. double *coeff= memalign(sizeof(double), length*sizeof(double));
  1442. int i;
  1443. SwsVector *vec= malloc(sizeof(SwsVector));
  1444. vec->coeff= coeff;
  1445. vec->length= length;
  1446. for(i=0; i<length; i++) coeff[i]= 0.0;
  1447. for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  1448. for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
  1449. return vec;
  1450. }
  1451. static SwsVector *diffVec(SwsVector *a, SwsVector *b){
  1452. int length= MAX(a->length, b->length);
  1453. double *coeff= memalign(sizeof(double), length*sizeof(double));
  1454. int i;
  1455. SwsVector *vec= malloc(sizeof(SwsVector));
  1456. vec->coeff= coeff;
  1457. vec->length= length;
  1458. for(i=0; i<length; i++) coeff[i]= 0.0;
  1459. for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  1460. for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
  1461. return vec;
  1462. }
  1463. /* shift left / or right if "shift" is negative */
  1464. static SwsVector *getShiftedVec(SwsVector *a, int shift){
  1465. int length= a->length + ABS(shift)*2;
  1466. double *coeff= memalign(sizeof(double), length*sizeof(double));
  1467. int i;
  1468. SwsVector *vec= malloc(sizeof(SwsVector));
  1469. vec->coeff= coeff;
  1470. vec->length= length;
  1471. for(i=0; i<length; i++) coeff[i]= 0.0;
  1472. for(i=0; i<a->length; i++)
  1473. {
  1474. coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
  1475. }
  1476. return vec;
  1477. }
  1478. void shiftVec(SwsVector *a, int shift){
  1479. SwsVector *shifted= getShiftedVec(a, shift);
  1480. free(a->coeff);
  1481. a->coeff= shifted->coeff;
  1482. a->length= shifted->length;
  1483. free(shifted);
  1484. }
  1485. void addVec(SwsVector *a, SwsVector *b){
  1486. SwsVector *sum= sumVec(a, b);
  1487. free(a->coeff);
  1488. a->coeff= sum->coeff;
  1489. a->length= sum->length;
  1490. free(sum);
  1491. }
  1492. void subVec(SwsVector *a, SwsVector *b){
  1493. SwsVector *diff= diffVec(a, b);
  1494. free(a->coeff);
  1495. a->coeff= diff->coeff;
  1496. a->length= diff->length;
  1497. free(diff);
  1498. }
  1499. void convVec(SwsVector *a, SwsVector *b){
  1500. SwsVector *conv= getConvVec(a, b);
  1501. free(a->coeff);
  1502. a->coeff= conv->coeff;
  1503. a->length= conv->length;
  1504. free(conv);
  1505. }
  1506. SwsVector *cloneVec(SwsVector *a){
  1507. double *coeff= memalign(sizeof(double), a->length*sizeof(double));
  1508. int i;
  1509. SwsVector *vec= malloc(sizeof(SwsVector));
  1510. vec->coeff= coeff;
  1511. vec->length= a->length;
  1512. for(i=0; i<a->length; i++) coeff[i]= a->coeff[i];
  1513. return vec;
  1514. }
  1515. void printVec(SwsVector *a){
  1516. int i;
  1517. double max=0;
  1518. double min=0;
  1519. double range;
  1520. for(i=0; i<a->length; i++)
  1521. if(a->coeff[i]>max) max= a->coeff[i];
  1522. for(i=0; i<a->length; i++)
  1523. if(a->coeff[i]<min) min= a->coeff[i];
  1524. range= max - min;
  1525. for(i=0; i<a->length; i++)
  1526. {
  1527. int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
  1528. printf("%1.3f ", a->coeff[i]);
  1529. for(;x>0; x--) printf(" ");
  1530. printf("|\n");
  1531. }
  1532. }
  1533. void freeVec(SwsVector *a){
  1534. if(!a) return;
  1535. if(a->coeff) free(a->coeff);
  1536. a->coeff=NULL;
  1537. a->length=0;
  1538. free(a);
  1539. }
  1540. void freeSwsContext(SwsContext *c){
  1541. int i;
  1542. if(!c) return;
  1543. if(c->lumPixBuf)
  1544. {
  1545. for(i=0; i<c->vLumBufSize; i++)
  1546. {
  1547. if(c->lumPixBuf[i]) free(c->lumPixBuf[i]);
  1548. c->lumPixBuf[i]=NULL;
  1549. }
  1550. free(c->lumPixBuf);
  1551. c->lumPixBuf=NULL;
  1552. }
  1553. if(c->chrPixBuf)
  1554. {
  1555. for(i=0; i<c->vChrBufSize; i++)
  1556. {
  1557. if(c->chrPixBuf[i]) free(c->chrPixBuf[i]);
  1558. c->chrPixBuf[i]=NULL;
  1559. }
  1560. free(c->chrPixBuf);
  1561. c->chrPixBuf=NULL;
  1562. }
  1563. if(c->vLumFilter) free(c->vLumFilter);
  1564. c->vLumFilter = NULL;
  1565. if(c->vChrFilter) free(c->vChrFilter);
  1566. c->vChrFilter = NULL;
  1567. if(c->hLumFilter) free(c->hLumFilter);
  1568. c->hLumFilter = NULL;
  1569. if(c->hChrFilter) free(c->hChrFilter);
  1570. c->hChrFilter = NULL;
  1571. if(c->vLumFilterPos) free(c->vLumFilterPos);
  1572. c->vLumFilterPos = NULL;
  1573. if(c->vChrFilterPos) free(c->vChrFilterPos);
  1574. c->vChrFilterPos = NULL;
  1575. if(c->hLumFilterPos) free(c->hLumFilterPos);
  1576. c->hLumFilterPos = NULL;
  1577. if(c->hChrFilterPos) free(c->hChrFilterPos);
  1578. c->hChrFilterPos = NULL;
  1579. if(c->lumMmxFilter) free(c->lumMmxFilter);
  1580. c->lumMmxFilter = NULL;
  1581. if(c->chrMmxFilter) free(c->chrMmxFilter);
  1582. c->chrMmxFilter = NULL;
  1583. free(c);
  1584. }