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.

1689 lines
46KB

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