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
58KB

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