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.

1579 lines
42KB

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