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.

2003 lines
55KB

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