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.

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