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.

2119 lines
59KB

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