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.

1729 lines
47KB

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