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.

1127 lines
31KB

  1. // Software scaling and colorspace conversion routines for MPlayer
  2. // Orginal C implementation by A'rpi/ESP-team <arpi@thot.banki.hu>
  3. // current version mostly by Michael Niedermayer (michaelni@gmx.at)
  4. // the parts written by michael are under GNU GPL
  5. /*
  6. supported Input formats: YV12 (grayscale soon too)
  7. supported output formats: YV12, BGR15, BGR16, BGR24, BGR32 (grayscale soon too)
  8. */
  9. #include <inttypes.h>
  10. #include <string.h>
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include "../config.h"
  14. #include "../mangle.h"
  15. #ifdef HAVE_MALLOC_H
  16. #include <malloc.h>
  17. #endif
  18. #include "swscale.h"
  19. #include "../cpudetect.h"
  20. #include "../libvo/img_format.h"
  21. #undef MOVNTQ
  22. #undef PAVGB
  23. //#undef HAVE_MMX2
  24. //#define HAVE_3DNOW
  25. //#undef HAVE_MMX
  26. //#undef ARCH_X86
  27. #define DITHER1XBPP
  28. #define RET 0xC3 //near return opcode
  29. #ifdef MP_DEBUG
  30. #define ASSERT(x) if(!(x)) { printf("ASSERT " #x " failed\n"); *((int*)0)=0; }
  31. #else
  32. #define ASSERT(x) ;
  33. #endif
  34. #ifdef M_PI
  35. #define PI M_PI
  36. #else
  37. #define PI 3.14159265358979323846
  38. #endif
  39. extern int verbose; // defined in mplayer.c
  40. /*
  41. NOTES
  42. known BUGS with known cause (no bugreports please!, but patches are welcome :) )
  43. horizontal fast_bilinear MMX2 scaler reads 1-7 samples too much (might cause a sig11)
  44. Supported output formats BGR15 BGR16 BGR24 BGR32 YV12
  45. BGR15 & BGR16 MMX verions support dithering
  46. Special versions: fast Y 1:1 scaling (no interpolation in y direction)
  47. TODO
  48. more intelligent missalignment avoidance for the horizontal scaler
  49. dither in C
  50. change the distance of the u & v buffer
  51. Move static / global vars into a struct so multiple scalers can be used
  52. write special vertical cubic upscale version
  53. Optimize C code (yv12 / minmax)
  54. */
  55. #define ABS(a) ((a) > 0 ? (a) : (-(a)))
  56. #define MIN(a,b) ((a) > (b) ? (b) : (a))
  57. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  58. #ifdef ARCH_X86
  59. #define CAN_COMPILE_X86_ASM
  60. #endif
  61. #ifdef CAN_COMPILE_X86_ASM
  62. static uint64_t __attribute__((aligned(8))) yCoeff= 0x2568256825682568LL;
  63. static uint64_t __attribute__((aligned(8))) vrCoeff= 0x3343334333433343LL;
  64. static uint64_t __attribute__((aligned(8))) ubCoeff= 0x40cf40cf40cf40cfLL;
  65. static uint64_t __attribute__((aligned(8))) vgCoeff= 0xE5E2E5E2E5E2E5E2LL;
  66. static uint64_t __attribute__((aligned(8))) ugCoeff= 0xF36EF36EF36EF36ELL;
  67. static uint64_t __attribute__((aligned(8))) bF8= 0xF8F8F8F8F8F8F8F8LL;
  68. static uint64_t __attribute__((aligned(8))) bFC= 0xFCFCFCFCFCFCFCFCLL;
  69. static uint64_t __attribute__((aligned(8))) w400= 0x0400040004000400LL;
  70. static uint64_t __attribute__((aligned(8))) w80= 0x0080008000800080LL;
  71. static uint64_t __attribute__((aligned(8))) w10= 0x0010001000100010LL;
  72. static uint64_t __attribute__((aligned(8))) w02= 0x0002000200020002LL;
  73. static uint64_t __attribute__((aligned(8))) bm00001111=0x00000000FFFFFFFFLL;
  74. static uint64_t __attribute__((aligned(8))) bm00000111=0x0000000000FFFFFFLL;
  75. static uint64_t __attribute__((aligned(8))) bm11111000=0xFFFFFFFFFF000000LL;
  76. static volatile uint64_t __attribute__((aligned(8))) b5Dither;
  77. static volatile uint64_t __attribute__((aligned(8))) g5Dither;
  78. static volatile uint64_t __attribute__((aligned(8))) g6Dither;
  79. static volatile uint64_t __attribute__((aligned(8))) r5Dither;
  80. static uint64_t __attribute__((aligned(8))) dither4[2]={
  81. 0x0103010301030103LL,
  82. 0x0200020002000200LL,};
  83. static uint64_t __attribute__((aligned(8))) dither8[2]={
  84. 0x0602060206020602LL,
  85. 0x0004000400040004LL,};
  86. static uint64_t __attribute__((aligned(8))) b16Mask= 0x001F001F001F001FLL;
  87. static uint64_t __attribute__((aligned(8))) g16Mask= 0x07E007E007E007E0LL;
  88. static uint64_t __attribute__((aligned(8))) r16Mask= 0xF800F800F800F800LL;
  89. static uint64_t __attribute__((aligned(8))) b15Mask= 0x001F001F001F001FLL;
  90. static uint64_t __attribute__((aligned(8))) g15Mask= 0x03E003E003E003E0LL;
  91. static uint64_t __attribute__((aligned(8))) r15Mask= 0x7C007C007C007C00LL;
  92. static uint64_t __attribute__((aligned(8))) M24A= 0x00FF0000FF0000FFLL;
  93. static uint64_t __attribute__((aligned(8))) M24B= 0xFF0000FF0000FF00LL;
  94. static uint64_t __attribute__((aligned(8))) M24C= 0x0000FF0000FF0000LL;
  95. // FIXME remove
  96. static uint64_t __attribute__((aligned(8))) asm_yalpha1;
  97. static uint64_t __attribute__((aligned(8))) asm_uvalpha1;
  98. #endif
  99. // clipping helper table for C implementations:
  100. static unsigned char clip_table[768];
  101. static unsigned short clip_table16b[768];
  102. static unsigned short clip_table16g[768];
  103. static unsigned short clip_table16r[768];
  104. static unsigned short clip_table15b[768];
  105. static unsigned short clip_table15g[768];
  106. static unsigned short clip_table15r[768];
  107. // yuv->rgb conversion tables:
  108. static int yuvtab_2568[256];
  109. static int yuvtab_3343[256];
  110. static int yuvtab_0c92[256];
  111. static int yuvtab_1a1e[256];
  112. static int yuvtab_40cf[256];
  113. // Needed for cubic scaler to catch overflows
  114. static int clip_yuvtab_2568[768];
  115. static int clip_yuvtab_3343[768];
  116. static int clip_yuvtab_0c92[768];
  117. static int clip_yuvtab_1a1e[768];
  118. static int clip_yuvtab_40cf[768];
  119. //global sws_flags from the command line
  120. int sws_flags=0;
  121. /* cpuCaps combined from cpudetect and whats actually compiled in
  122. (if there is no support for something compiled in it wont appear here) */
  123. static CpuCaps cpuCaps;
  124. void (*swScale)(SwsContext *context, uint8_t* src[], int srcStride[], int srcSliceY,
  125. int srcSliceH, uint8_t* dst[], int dstStride[])=NULL;
  126. #ifdef CAN_COMPILE_X86_ASM
  127. void in_asm_used_var_warning_killer()
  128. {
  129. volatile int i= yCoeff+vrCoeff+ubCoeff+vgCoeff+ugCoeff+bF8+bFC+w400+w80+w10+
  130. bm00001111+bm00000111+bm11111000+b16Mask+g16Mask+r16Mask+b15Mask+g15Mask+r15Mask+asm_yalpha1+ asm_uvalpha1+
  131. M24A+M24B+M24C+w02 + b5Dither+g5Dither+r5Dither+g6Dither+dither4[0]+dither8[0];
  132. if(i) i=0;
  133. }
  134. #endif
  135. static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  136. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  137. uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW)
  138. {
  139. //FIXME Optimize (just quickly writen not opti..)
  140. int i;
  141. for(i=0; i<dstW; i++)
  142. {
  143. int val=0;
  144. int j;
  145. for(j=0; j<lumFilterSize; j++)
  146. val += lumSrc[j][i] * lumFilter[j];
  147. dest[i]= MIN(MAX(val>>19, 0), 255);
  148. }
  149. if(uDest != NULL)
  150. for(i=0; i<(dstW>>1); i++)
  151. {
  152. int u=0;
  153. int v=0;
  154. int j;
  155. for(j=0; j<chrFilterSize; j++)
  156. {
  157. u += chrSrc[j][i] * chrFilter[j];
  158. v += chrSrc[j][i + 2048] * chrFilter[j];
  159. }
  160. uDest[i]= MIN(MAX(u>>19, 0), 255);
  161. vDest[i]= MIN(MAX(v>>19, 0), 255);
  162. }
  163. }
  164. static inline void yuv2rgbXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  165. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  166. uint8_t *dest, int dstW, int dstFormat)
  167. {
  168. if(dstFormat==IMGFMT_BGR32)
  169. {
  170. int i;
  171. for(i=0; i<(dstW>>1); i++){
  172. int j;
  173. int Y1=0;
  174. int Y2=0;
  175. int U=0;
  176. int V=0;
  177. int Cb, Cr, Cg;
  178. for(j=0; j<lumFilterSize; j++)
  179. {
  180. Y1 += lumSrc[j][2*i] * lumFilter[j];
  181. Y2 += lumSrc[j][2*i+1] * lumFilter[j];
  182. }
  183. for(j=0; j<chrFilterSize; j++)
  184. {
  185. U += chrSrc[j][i] * chrFilter[j];
  186. V += chrSrc[j][i+2048] * chrFilter[j];
  187. }
  188. Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
  189. Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
  190. U >>= 19;
  191. V >>= 19;
  192. Cb= clip_yuvtab_40cf[U+ 256];
  193. Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
  194. Cr= clip_yuvtab_3343[V+ 256];
  195. dest[8*i+0]=clip_table[((Y1 + Cb) >>13)];
  196. dest[8*i+1]=clip_table[((Y1 + Cg) >>13)];
  197. dest[8*i+2]=clip_table[((Y1 + Cr) >>13)];
  198. dest[8*i+4]=clip_table[((Y2 + Cb) >>13)];
  199. dest[8*i+5]=clip_table[((Y2 + Cg) >>13)];
  200. dest[8*i+6]=clip_table[((Y2 + Cr) >>13)];
  201. }
  202. }
  203. else if(dstFormat==IMGFMT_BGR24)
  204. {
  205. int i;
  206. for(i=0; i<(dstW>>1); i++){
  207. int j;
  208. int Y1=0;
  209. int Y2=0;
  210. int U=0;
  211. int V=0;
  212. int Cb, Cr, Cg;
  213. for(j=0; j<lumFilterSize; j++)
  214. {
  215. Y1 += lumSrc[j][2*i] * lumFilter[j];
  216. Y2 += lumSrc[j][2*i+1] * lumFilter[j];
  217. }
  218. for(j=0; j<chrFilterSize; j++)
  219. {
  220. U += chrSrc[j][i] * chrFilter[j];
  221. V += chrSrc[j][i+2048] * chrFilter[j];
  222. }
  223. Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
  224. Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
  225. U >>= 19;
  226. V >>= 19;
  227. Cb= clip_yuvtab_40cf[U+ 256];
  228. Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
  229. Cr= clip_yuvtab_3343[V+ 256];
  230. dest[0]=clip_table[((Y1 + Cb) >>13)];
  231. dest[1]=clip_table[((Y1 + Cg) >>13)];
  232. dest[2]=clip_table[((Y1 + Cr) >>13)];
  233. dest[3]=clip_table[((Y2 + Cb) >>13)];
  234. dest[4]=clip_table[((Y2 + Cg) >>13)];
  235. dest[5]=clip_table[((Y2 + Cr) >>13)];
  236. dest+=6;
  237. }
  238. }
  239. else if(dstFormat==IMGFMT_BGR16)
  240. {
  241. int i;
  242. for(i=0; i<(dstW>>1); i++){
  243. int j;
  244. int Y1=0;
  245. int Y2=0;
  246. int U=0;
  247. int V=0;
  248. int Cb, Cr, Cg;
  249. for(j=0; j<lumFilterSize; j++)
  250. {
  251. Y1 += lumSrc[j][2*i] * lumFilter[j];
  252. Y2 += lumSrc[j][2*i+1] * lumFilter[j];
  253. }
  254. for(j=0; j<chrFilterSize; j++)
  255. {
  256. U += chrSrc[j][i] * chrFilter[j];
  257. V += chrSrc[j][i+2048] * chrFilter[j];
  258. }
  259. Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
  260. Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
  261. U >>= 19;
  262. V >>= 19;
  263. Cb= clip_yuvtab_40cf[U+ 256];
  264. Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
  265. Cr= clip_yuvtab_3343[V+ 256];
  266. ((uint16_t*)dest)[2*i] =
  267. clip_table16b[(Y1 + Cb) >>13] |
  268. clip_table16g[(Y1 + Cg) >>13] |
  269. clip_table16r[(Y1 + Cr) >>13];
  270. ((uint16_t*)dest)[2*i+1] =
  271. clip_table16b[(Y2 + Cb) >>13] |
  272. clip_table16g[(Y2 + Cg) >>13] |
  273. clip_table16r[(Y2 + Cr) >>13];
  274. }
  275. }
  276. else if(dstFormat==IMGFMT_BGR15)
  277. {
  278. int i;
  279. for(i=0; i<(dstW>>1); i++){
  280. int j;
  281. int Y1=0;
  282. int Y2=0;
  283. int U=0;
  284. int V=0;
  285. int Cb, Cr, Cg;
  286. for(j=0; j<lumFilterSize; j++)
  287. {
  288. Y1 += lumSrc[j][2*i] * lumFilter[j];
  289. Y2 += lumSrc[j][2*i+1] * lumFilter[j];
  290. }
  291. for(j=0; j<chrFilterSize; j++)
  292. {
  293. U += chrSrc[j][i] * chrFilter[j];
  294. V += chrSrc[j][i+2048] * chrFilter[j];
  295. }
  296. Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
  297. Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
  298. U >>= 19;
  299. V >>= 19;
  300. Cb= clip_yuvtab_40cf[U+ 256];
  301. Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
  302. Cr= clip_yuvtab_3343[V+ 256];
  303. ((uint16_t*)dest)[2*i] =
  304. clip_table15b[(Y1 + Cb) >>13] |
  305. clip_table15g[(Y1 + Cg) >>13] |
  306. clip_table15r[(Y1 + Cr) >>13];
  307. ((uint16_t*)dest)[2*i+1] =
  308. clip_table15b[(Y2 + Cb) >>13] |
  309. clip_table15g[(Y2 + Cg) >>13] |
  310. clip_table15r[(Y2 + Cr) >>13];
  311. }
  312. }
  313. }
  314. //Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
  315. //Plain C versions
  316. #if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT)
  317. #define COMPILE_C
  318. #endif
  319. #ifdef CAN_COMPILE_X86_ASM
  320. #if (defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  321. #define COMPILE_MMX
  322. #endif
  323. #if defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)
  324. #define COMPILE_MMX2
  325. #endif
  326. #if (defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  327. #define COMPILE_3DNOW
  328. #endif
  329. #endif //CAN_COMPILE_X86_ASM
  330. #undef HAVE_MMX
  331. #undef HAVE_MMX2
  332. #undef HAVE_3DNOW
  333. #undef ARCH_X86
  334. #ifdef COMPILE_C
  335. #undef HAVE_MMX
  336. #undef HAVE_MMX2
  337. #undef HAVE_3DNOW
  338. #undef ARCH_X86
  339. #define RENAME(a) a ## _C
  340. #include "swscale_template.c"
  341. #endif
  342. #ifdef CAN_COMPILE_X86_ASM
  343. //X86 versions
  344. /*
  345. #undef RENAME
  346. #undef HAVE_MMX
  347. #undef HAVE_MMX2
  348. #undef HAVE_3DNOW
  349. #define ARCH_X86
  350. #define RENAME(a) a ## _X86
  351. #include "swscale_template.c"
  352. */
  353. //MMX versions
  354. #ifdef COMPILE_MMX
  355. #undef RENAME
  356. #define HAVE_MMX
  357. #undef HAVE_MMX2
  358. #undef HAVE_3DNOW
  359. #define ARCH_X86
  360. #define RENAME(a) a ## _MMX
  361. #include "swscale_template.c"
  362. #endif
  363. //MMX2 versions
  364. #ifdef COMPILE_MMX2
  365. #undef RENAME
  366. #define HAVE_MMX
  367. #define HAVE_MMX2
  368. #undef HAVE_3DNOW
  369. #define ARCH_X86
  370. #define RENAME(a) a ## _MMX2
  371. #include "swscale_template.c"
  372. #endif
  373. //3DNOW versions
  374. #ifdef COMPILE_3DNOW
  375. #undef RENAME
  376. #define HAVE_MMX
  377. #undef HAVE_MMX2
  378. #define HAVE_3DNOW
  379. #define ARCH_X86
  380. #define RENAME(a) a ## _3DNow
  381. #include "swscale_template.c"
  382. #endif
  383. #endif //CAN_COMPILE_X86_ASM
  384. // minor note: the HAVE_xyz is messed up after that line so dont use it
  385. // old global scaler, dont use for new code
  386. // will use sws_flags from the command line
  387. void SwScale_YV12slice(unsigned char* src[], int srcStride[], int srcSliceY ,
  388. int srcSliceH, uint8_t* dst[], int dstStride, int dstbpp,
  389. int srcW, int srcH, int dstW, int dstH){
  390. static SwsContext *context=NULL;
  391. int dstFormat;
  392. int flags=0;
  393. static int firstTime=1;
  394. int dstStride3[3]= {dstStride, dstStride>>1, dstStride>>1};
  395. if(firstTime)
  396. {
  397. flags= SWS_PRINT_INFO;
  398. firstTime=0;
  399. }
  400. switch(dstbpp)
  401. {
  402. case 8 : dstFormat= IMGFMT_Y8; break;
  403. case 12: dstFormat= IMGFMT_YV12; break;
  404. case 15: dstFormat= IMGFMT_BGR15; break;
  405. case 16: dstFormat= IMGFMT_BGR16; break;
  406. case 24: dstFormat= IMGFMT_BGR24; break;
  407. case 32: dstFormat= IMGFMT_BGR32; break;
  408. default: return;
  409. }
  410. switch(sws_flags)
  411. {
  412. case 0: flags|= SWS_FAST_BILINEAR; break;
  413. case 1: flags|= SWS_BILINEAR; break;
  414. case 2: flags|= SWS_BICUBIC; break;
  415. case 3: flags|= SWS_X; break;
  416. default:flags|= SWS_BILINEAR; break;
  417. }
  418. if(!context) context=getSwsContext(srcW, srcH, IMGFMT_YV12, dstW, dstH, dstFormat, flags, NULL, NULL);
  419. swScale(context, src, srcStride, srcSliceY, srcSliceH, dst, dstStride3);
  420. }
  421. static inline void initFilter(int16_t *dstFilter, int16_t *filterPos, int *filterSize, int xInc,
  422. int srcW, int dstW, int filterAlign, int one, int flags)
  423. {
  424. int i;
  425. double filter[10000];
  426. #ifdef ARCH_X86
  427. if(gCpuCaps.hasMMX)
  428. asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
  429. #endif
  430. if(ABS(xInc - 0x10000) <10) // unscaled
  431. {
  432. int i;
  433. *filterSize= (1 +(filterAlign-1)) & (~(filterAlign-1)); // 1 or 4 normaly
  434. for(i=0; i<dstW*(*filterSize); i++) filter[i]=0;
  435. for(i=0; i<dstW; i++)
  436. {
  437. filter[i*(*filterSize)]=1;
  438. filterPos[i]=i;
  439. }
  440. }
  441. else if(xInc <= (1<<16) || (flags&SWS_FAST_BILINEAR)) // upscale
  442. {
  443. int i;
  444. int xDstInSrc;
  445. if (flags&SWS_BICUBIC) *filterSize= 4;
  446. else if(flags&SWS_X ) *filterSize= 4;
  447. else *filterSize= 2;
  448. // printf("%d %d %d\n", filterSize, srcW, dstW);
  449. *filterSize= (*filterSize +(filterAlign-1)) & (~(filterAlign-1));
  450. xDstInSrc= xInc/2 - 0x8000;
  451. for(i=0; i<dstW; i++)
  452. {
  453. int xx= (xDstInSrc>>16) - (*filterSize>>1) + 1;
  454. int j;
  455. filterPos[i]= xx;
  456. if((flags & SWS_BICUBIC) || (flags & SWS_X))
  457. {
  458. double d= ABS(((xx+1)<<16) - xDstInSrc)/(double)(1<<16);
  459. double y1,y2,y3,y4;
  460. double A= -0.6;
  461. if(flags & SWS_BICUBIC){
  462. // Equation is from VirtualDub
  463. y1 = ( + A*d - 2.0*A*d*d + A*d*d*d);
  464. y2 = (+ 1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
  465. y3 = ( - A*d + (2.0*A+3.0)*d*d - (A+2.0)*d*d*d);
  466. y4 = ( + A*d*d - A*d*d*d);
  467. }else{
  468. // cubic interpolation (derived it myself)
  469. y1 = ( -2.0*d + 3.0*d*d - 1.0*d*d*d)/6.0;
  470. y2 = (6.0 -3.0*d - 6.0*d*d + 3.0*d*d*d)/6.0;
  471. y3 = ( +6.0*d + 3.0*d*d - 3.0*d*d*d)/6.0;
  472. y4 = ( -1.0*d + 1.0*d*d*d)/6.0;
  473. }
  474. // printf("%d %d %d \n", coeff, (int)d, xDstInSrc);
  475. filter[i*(*filterSize) + 0]= y1;
  476. filter[i*(*filterSize) + 1]= y2;
  477. filter[i*(*filterSize) + 2]= y3;
  478. filter[i*(*filterSize) + 3]= y4;
  479. // printf("%1.3f %1.3f %1.3f %1.3f %1.3f\n",d , y1, y2, y3, y4);
  480. }
  481. else
  482. {
  483. for(j=0; j<*filterSize; j++)
  484. {
  485. double d= ABS((xx<<16) - xDstInSrc)/(double)(1<<16);
  486. double coeff= 1.0 - d;
  487. if(coeff<0) coeff=0;
  488. // printf("%d %d %d \n", coeff, (int)d, xDstInSrc);
  489. filter[i*(*filterSize) + j]= coeff;
  490. xx++;
  491. }
  492. }
  493. xDstInSrc+= xInc;
  494. }
  495. }
  496. else // downscale
  497. {
  498. int xDstInSrc;
  499. if(flags&SWS_BICUBIC) *filterSize= (int)ceil(1 + 4.0*srcW / (double)dstW);
  500. else if(flags&SWS_X) *filterSize= (int)ceil(1 + 4.0*srcW / (double)dstW);
  501. else *filterSize= (int)ceil(1 + 2.0*srcW / (double)dstW);
  502. // printf("%d %d %d\n", *filterSize, srcW, dstW);
  503. *filterSize= (*filterSize +(filterAlign-1)) & (~(filterAlign-1));
  504. xDstInSrc= xInc/2 - 0x8000;
  505. for(i=0; i<dstW; i++)
  506. {
  507. int xx= (int)((double)xDstInSrc/(double)(1<<16) - ((*filterSize)-1)*0.5 + 0.5);
  508. int j;
  509. filterPos[i]= xx;
  510. for(j=0; j<*filterSize; j++)
  511. {
  512. double d= ABS((xx<<16) - xDstInSrc)/(double)xInc;
  513. double coeff;
  514. if((flags & SWS_BICUBIC) || (flags & SWS_X))
  515. {
  516. double A= -0.75;
  517. // d*=2;
  518. // Equation is from VirtualDub
  519. if(d<1.0)
  520. coeff = (1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
  521. else if(d<2.0)
  522. coeff = (-4.0*A + 8.0*A*d - 5.0*A*d*d + A*d*d*d);
  523. else
  524. coeff=0.0;
  525. }
  526. /* else if(flags & SWS_X)
  527. {
  528. }*/
  529. else
  530. {
  531. coeff= 1.0 - d;
  532. if(coeff<0) coeff=0;
  533. }
  534. // printf("%1.3f %d %d \n", coeff, (int)d, xDstInSrc);
  535. filter[i*(*filterSize) + j]= coeff;
  536. xx++;
  537. }
  538. xDstInSrc+= xInc;
  539. }
  540. }
  541. //fix borders
  542. for(i=0; i<dstW; i++)
  543. {
  544. int j;
  545. if(filterPos[i] < 0)
  546. {
  547. // Move filter coeffs left to compensate for filterPos
  548. for(j=1; j<*filterSize; j++)
  549. {
  550. int left= MAX(j + filterPos[i], 0);
  551. filter[i*(*filterSize) + left] += filter[i*(*filterSize) + j];
  552. filter[i*(*filterSize) + j]=0;
  553. }
  554. filterPos[i]= 0;
  555. }
  556. if(filterPos[i] + (*filterSize) > srcW)
  557. {
  558. int shift= filterPos[i] + (*filterSize) - srcW;
  559. // Move filter coeffs right to compensate for filterPos
  560. for(j=(*filterSize)-2; j>=0; j--)
  561. {
  562. int right= MIN(j + shift, (*filterSize)-1);
  563. filter[i*(*filterSize) +right] += filter[i*(*filterSize) +j];
  564. filter[i*(*filterSize) +j]=0;
  565. }
  566. filterPos[i]= srcW - (*filterSize);
  567. }
  568. }
  569. //FIXME try to align filterpos if possible / try to shift filterpos to put zeros at the end
  570. // and skip these than later
  571. //Normalize
  572. for(i=0; i<dstW; i++)
  573. {
  574. int j;
  575. double sum=0;
  576. double scale= one;
  577. for(j=0; j<*filterSize; j++)
  578. {
  579. sum+= filter[i*(*filterSize) + j];
  580. }
  581. scale/= sum;
  582. for(j=0; j<*filterSize; j++)
  583. {
  584. dstFilter[i*(*filterSize) + j]= (int)(filter[i*(*filterSize) + j]*scale);
  585. }
  586. }
  587. }
  588. #ifdef ARCH_X86
  589. static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode)
  590. {
  591. uint8_t *fragment;
  592. int imm8OfPShufW1;
  593. int imm8OfPShufW2;
  594. int fragmentLength;
  595. int xpos, i;
  596. // create an optimized horizontal scaling routine
  597. //code fragment
  598. asm volatile(
  599. "jmp 9f \n\t"
  600. // Begin
  601. "0: \n\t"
  602. "movq (%%esi), %%mm0 \n\t" //FIXME Alignment
  603. "movq %%mm0, %%mm1 \n\t"
  604. "psrlq $8, %%mm0 \n\t"
  605. "punpcklbw %%mm7, %%mm1 \n\t"
  606. "movq %%mm2, %%mm3 \n\t"
  607. "punpcklbw %%mm7, %%mm0 \n\t"
  608. "addw %%bx, %%cx \n\t" //2*xalpha += (4*lumXInc)&0xFFFF
  609. "pshufw $0xFF, %%mm1, %%mm1 \n\t"
  610. "1: \n\t"
  611. "adcl %%edx, %%esi \n\t" //xx+= (4*lumXInc)>>16 + carry
  612. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  613. "2: \n\t"
  614. "psrlw $9, %%mm3 \n\t"
  615. "psubw %%mm1, %%mm0 \n\t"
  616. "pmullw %%mm3, %%mm0 \n\t"
  617. "paddw %%mm6, %%mm2 \n\t" // 2*alpha += xpos&0xFFFF
  618. "psllw $7, %%mm1 \n\t"
  619. "paddw %%mm1, %%mm0 \n\t"
  620. "movq %%mm0, (%%edi, %%eax) \n\t"
  621. "addl $8, %%eax \n\t"
  622. // End
  623. "9: \n\t"
  624. // "int $3\n\t"
  625. "leal 0b, %0 \n\t"
  626. "leal 1b, %1 \n\t"
  627. "leal 2b, %2 \n\t"
  628. "decl %1 \n\t"
  629. "decl %2 \n\t"
  630. "subl %0, %1 \n\t"
  631. "subl %0, %2 \n\t"
  632. "leal 9b, %3 \n\t"
  633. "subl %0, %3 \n\t"
  634. :"=r" (fragment), "=r" (imm8OfPShufW1), "=r" (imm8OfPShufW2),
  635. "=r" (fragmentLength)
  636. );
  637. xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
  638. for(i=0; i<dstW/8; i++)
  639. {
  640. int xx=xpos>>16;
  641. if((i&3) == 0)
  642. {
  643. int a=0;
  644. int b=((xpos+xInc)>>16) - xx;
  645. int c=((xpos+xInc*2)>>16) - xx;
  646. int d=((xpos+xInc*3)>>16) - xx;
  647. memcpy(funnyCode + fragmentLength*i/4, fragment, fragmentLength);
  648. funnyCode[fragmentLength*i/4 + imm8OfPShufW1]=
  649. funnyCode[fragmentLength*i/4 + imm8OfPShufW2]=
  650. a | (b<<2) | (c<<4) | (d<<6);
  651. // if we dont need to read 8 bytes than dont :), reduces the chance of
  652. // crossing a cache line
  653. if(d<3) funnyCode[fragmentLength*i/4 + 1]= 0x6E;
  654. funnyCode[fragmentLength*(i+4)/4]= RET;
  655. }
  656. xpos+=xInc;
  657. }
  658. }
  659. #endif // ARCH_X86
  660. //FIXME remove
  661. void SwScale_Init(){
  662. }
  663. static void globalInit(){
  664. // generating tables:
  665. int i;
  666. for(i=0; i<768; i++){
  667. int c= MIN(MAX(i-256, 0), 255);
  668. clip_table[i]=c;
  669. yuvtab_2568[c]= clip_yuvtab_2568[i]=(0x2568*(c-16))+(256<<13);
  670. yuvtab_3343[c]= clip_yuvtab_3343[i]=0x3343*(c-128);
  671. yuvtab_0c92[c]= clip_yuvtab_0c92[i]=-0x0c92*(c-128);
  672. yuvtab_1a1e[c]= clip_yuvtab_1a1e[i]=-0x1a1e*(c-128);
  673. yuvtab_40cf[c]= clip_yuvtab_40cf[i]=0x40cf*(c-128);
  674. }
  675. for(i=0; i<768; i++)
  676. {
  677. int v= clip_table[i];
  678. clip_table16b[i]= v>>3;
  679. clip_table16g[i]= (v<<3)&0x07E0;
  680. clip_table16r[i]= (v<<8)&0xF800;
  681. clip_table15b[i]= v>>3;
  682. clip_table15g[i]= (v<<2)&0x03E0;
  683. clip_table15r[i]= (v<<7)&0x7C00;
  684. }
  685. cpuCaps= gCpuCaps;
  686. #ifdef RUNTIME_CPUDETECT
  687. #ifdef CAN_COMPILE_X86_ASM
  688. // ordered per speed fasterst first
  689. if(gCpuCaps.hasMMX2)
  690. swScale= swScale_MMX2;
  691. else if(gCpuCaps.has3DNow)
  692. swScale= swScale_3DNow;
  693. else if(gCpuCaps.hasMMX)
  694. swScale= swScale_MMX;
  695. else
  696. swScale= swScale_C;
  697. #else
  698. swScale= swScale_C;
  699. cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
  700. #endif
  701. #else //RUNTIME_CPUDETECT
  702. #ifdef HAVE_MMX2
  703. swScale= swScale_MMX2;
  704. cpuCaps.has3DNow = 0;
  705. #elif defined (HAVE_3DNOW)
  706. swScale= swScale_3DNow;
  707. cpuCaps.hasMMX2 = 0;
  708. #elif defined (HAVE_MMX)
  709. swScale= swScale_MMX;
  710. cpuCaps.hasMMX2 = cpuCaps.has3DNow = 0;
  711. #else
  712. swScale= swScale_C;
  713. cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
  714. #endif
  715. #endif //!RUNTIME_CPUDETECT
  716. }
  717. SwsContext *getSwsContext(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int flags,
  718. SwsFilter *srcFilter, SwsFilter *dstFilter){
  719. const int widthAlign= dstFormat==IMGFMT_YV12 ? 16 : 8;
  720. SwsContext *c;
  721. int i;
  722. //const int bytespp= (dstbpp+1)/8; //(12->1, 15&16->2, 24->3, 32->4)
  723. //const int over= dstFormat==IMGFMT_YV12 ? (((dstW+15)&(~15))) - dststride
  724. // : (((dstW+7)&(~7)))*bytespp - dststride;
  725. if(swScale==NULL) globalInit();
  726. /* sanity check */
  727. if(srcW<1 || srcH<1 || dstW<1 || dstH<1) return NULL;
  728. if(srcW>=SWS_MAX_SIZE || dstW>=SWS_MAX_SIZE || srcH>=SWS_MAX_SIZE || dstH>=SWS_MAX_SIZE)
  729. {
  730. fprintf(stderr, "size is too large, increase SWS_MAX_SIZE\n");
  731. return NULL;
  732. }
  733. /* FIXME
  734. if(dstStride[0]%widthAlign !=0 )
  735. {
  736. if(flags & SWS_PRINT_INFO)
  737. fprintf(stderr, "SwScaler: Warning: dstStride is not a multiple of %d!\n"
  738. "SwScaler: ->cannot do aligned memory acesses anymore\n",
  739. widthAlign);
  740. }
  741. */
  742. c= memalign(64, sizeof(SwsContext));
  743. c->srcW= srcW;
  744. c->srcH= srcH;
  745. c->dstW= dstW;
  746. c->dstH= dstH;
  747. c->lumXInc= ((srcW<<16) + (1<<15))/dstW;
  748. c->lumYInc= ((srcH<<16) + (1<<15))/dstH;
  749. c->flags= flags;
  750. c->dstFormat= dstFormat;
  751. c->srcFormat= srcFormat;
  752. if(cpuCaps.hasMMX2)
  753. {
  754. c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
  755. if(!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
  756. {
  757. if(flags&SWS_PRINT_INFO)
  758. fprintf(stderr, "SwScaler: output Width is not a multiple of 32 -> no MMX2 scaler\n");
  759. }
  760. }
  761. else
  762. c->canMMX2BeUsed=0;
  763. // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
  764. // but only for the FAST_BILINEAR mode otherwise do correct scaling
  765. // n-2 is the last chrominance sample available
  766. // this is not perfect, but noone shuld notice the difference, the more correct variant
  767. // would be like the vertical one, but that would require some special code for the
  768. // first and last pixel
  769. if(flags&SWS_FAST_BILINEAR)
  770. {
  771. if(c->canMMX2BeUsed) c->lumXInc+= 20;
  772. //we dont use the x86asm scaler if mmx is available
  773. else if(cpuCaps.hasMMX) c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
  774. }
  775. /* set chrXInc & chrDstW */
  776. if((flags&SWS_FULL_UV_IPOL) && dstFormat!=IMGFMT_YV12)
  777. c->chrXInc= c->lumXInc>>1, c->chrDstW= dstW;
  778. else
  779. c->chrXInc= c->lumXInc, c->chrDstW= (dstW+1)>>1;
  780. /* set chrYInc & chrDstH */
  781. if(dstFormat==IMGFMT_YV12) c->chrYInc= c->lumYInc, c->chrDstH= (dstH+1)>>1;
  782. else c->chrYInc= c->lumYInc>>1, c->chrDstH= dstH;
  783. /* precalculate horizontal scaler filter coefficients */
  784. {
  785. const int filterAlign= cpuCaps.hasMMX ? 4 : 1;
  786. initFilter(c->hLumFilter, c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
  787. srcW , dstW, filterAlign, 1<<14, flags);
  788. initFilter(c->hChrFilter, c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
  789. (srcW+1)>>1, c->chrDstW, filterAlign, 1<<14, flags);
  790. #ifdef ARCH_X86
  791. // cant downscale !!!
  792. if(c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
  793. {
  794. initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode);
  795. initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode);
  796. }
  797. #endif
  798. } // Init Horizontal stuff
  799. /* precalculate vertical scaler filter coefficients */
  800. initFilter(c->vLumFilter, c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
  801. srcH , dstH, 1, (1<<12)-4, flags);
  802. initFilter(c->vChrFilter, c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
  803. (srcH+1)>>1, c->chrDstH, 1, (1<<12)-4, flags);
  804. // Calculate Buffer Sizes so that they wont run out while handling these damn slices
  805. c->vLumBufSize= c->vLumFilterSize;
  806. c->vChrBufSize= c->vChrFilterSize;
  807. for(i=0; i<dstH; i++)
  808. {
  809. int chrI= i*c->chrDstH / dstH;
  810. int nextSlice= MAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
  811. ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<1));
  812. nextSlice&= ~1; // Slices start at even boundaries
  813. if(c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
  814. c->vLumBufSize= nextSlice - c->vLumFilterPos[i ];
  815. if(c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>1))
  816. c->vChrBufSize= (nextSlice>>1) - c->vChrFilterPos[chrI];
  817. }
  818. // allocate pixbufs (we use dynamic allocation because otherwise we would need to
  819. // allocate several megabytes to handle all possible cases)
  820. for(i=0; i<c->vLumBufSize; i++)
  821. c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= (uint16_t*)memalign(8, 4000);
  822. for(i=0; i<c->vChrBufSize; i++)
  823. c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= (uint16_t*)memalign(8, 8000);
  824. //try to avoid drawing green stuff between the right end and the stride end
  825. for(i=0; i<c->vLumBufSize; i++) memset(c->lumPixBuf[i], 0, 4000);
  826. for(i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, 8000);
  827. ASSERT(c->chrDstH <= dstH)
  828. ASSERT(c->vLumFilterSize* dstH*4 <= SWS_MAX_SIZE*20)
  829. ASSERT(c->vChrFilterSize*c->chrDstH*4 <= SWS_MAX_SIZE*20)
  830. // pack filter data for mmx code
  831. if(cpuCaps.hasMMX)
  832. {
  833. for(i=0; i<c->vLumFilterSize*dstH; i++)
  834. c->lumMmxFilter[4*i]=c->lumMmxFilter[4*i+1]=c->lumMmxFilter[4*i+2]=c->lumMmxFilter[4*i+3]=
  835. c->vLumFilter[i];
  836. for(i=0; i<c->vChrFilterSize*c->chrDstH; i++)
  837. c->chrMmxFilter[4*i]=c->chrMmxFilter[4*i+1]=c->chrMmxFilter[4*i+2]=c->chrMmxFilter[4*i+3]=
  838. c->vChrFilter[i];
  839. }
  840. if(flags&SWS_PRINT_INFO)
  841. {
  842. #ifdef DITHER1XBPP
  843. char *dither= cpuCaps.hasMMX ? " dithered" : "";
  844. #endif
  845. if(flags&SWS_FAST_BILINEAR)
  846. fprintf(stderr, "\nSwScaler: FAST_BILINEAR scaler ");
  847. else if(flags&SWS_BILINEAR)
  848. fprintf(stderr, "\nSwScaler: BILINEAR scaler ");
  849. else if(flags&SWS_BICUBIC)
  850. fprintf(stderr, "\nSwScaler: BICUBIC scaler ");
  851. else
  852. fprintf(stderr, "\nSwScaler: ehh flags invalid?! ");
  853. if(dstFormat==IMGFMT_BGR15)
  854. fprintf(stderr, "with%s BGR15 output ", dither);
  855. else if(dstFormat==IMGFMT_BGR16)
  856. fprintf(stderr, "with%s BGR16 output ", dither);
  857. else if(dstFormat==IMGFMT_BGR24)
  858. fprintf(stderr, "with BGR24 output ");
  859. else if(dstFormat==IMGFMT_BGR32)
  860. fprintf(stderr, "with BGR32 output ");
  861. else if(dstFormat==IMGFMT_YV12)
  862. fprintf(stderr, "with YV12 output ");
  863. else
  864. fprintf(stderr, "without output ");
  865. if(cpuCaps.hasMMX2)
  866. fprintf(stderr, "using MMX2\n");
  867. else if(cpuCaps.has3DNow)
  868. fprintf(stderr, "using 3DNOW\n");
  869. else if(cpuCaps.hasMMX)
  870. fprintf(stderr, "using MMX\n");
  871. else
  872. fprintf(stderr, "using C\n");
  873. }
  874. if((flags & SWS_PRINT_INFO) && verbose)
  875. {
  876. if(cpuCaps.hasMMX)
  877. {
  878. if(c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
  879. printf("SwScaler: using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
  880. else
  881. {
  882. if(c->hLumFilterSize==4)
  883. printf("SwScaler: using 4-tap MMX scaler for horizontal luminance scaling\n");
  884. else if(c->hLumFilterSize==8)
  885. printf("SwScaler: using 8-tap MMX scaler for horizontal luminance scaling\n");
  886. else
  887. printf("SwScaler: using n-tap MMX scaler for horizontal luminance scaling\n");
  888. if(c->hChrFilterSize==4)
  889. printf("SwScaler: using 4-tap MMX scaler for horizontal chrominance scaling\n");
  890. else if(c->hChrFilterSize==8)
  891. printf("SwScaler: using 8-tap MMX scaler for horizontal chrominance scaling\n");
  892. else
  893. printf("SwScaler: using n-tap MMX scaler for horizontal chrominance scaling\n");
  894. }
  895. }
  896. else
  897. {
  898. #ifdef ARCH_X86
  899. printf("SwScaler: using X86-Asm scaler for horizontal scaling\n");
  900. #else
  901. if(flags & SWS_FAST_BILINEAR)
  902. printf("SwScaler: using FAST_BILINEAR C scaler for horizontal scaling\n");
  903. else
  904. printf("SwScaler: using C scaler for horizontal scaling\n");
  905. #endif
  906. }
  907. if(dstFormat==IMGFMT_YV12)
  908. {
  909. if(c->vLumFilterSize==1)
  910. printf("SwScaler: using 1-tap %s \"scaler\" for vertical scaling (YV12)\n", cpuCaps.hasMMX ? "MMX" : "C");
  911. else
  912. printf("SwScaler: using n-tap %s scaler for vertical scaling (YV12)\n", cpuCaps.hasMMX ? "MMX" : "C");
  913. }
  914. else
  915. {
  916. if(c->vLumFilterSize==1 && c->vChrFilterSize==2)
  917. printf("SwScaler: using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
  918. "SwScaler: 2-tap scaler for vertical chrominance scaling (BGR)\n",cpuCaps.hasMMX ? "MMX" : "C");
  919. else if(c->vLumFilterSize==2 && c->vChrFilterSize==2)
  920. printf("SwScaler: using 2-tap linear %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
  921. else
  922. printf("SwScaler: using n-tap %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
  923. }
  924. if(dstFormat==IMGFMT_BGR24)
  925. printf("SwScaler: using %s YV12->BGR24 Converter\n",
  926. cpuCaps.hasMMX2 ? "MMX2" : (cpuCaps.hasMMX ? "MMX" : "C"));
  927. else
  928. printf("SwScaler: using %s YV12->BGR Converter\n", cpuCaps.hasMMX ? "MMX" : "C");//FIXME print format
  929. printf("SwScaler: %dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
  930. }
  931. return c;
  932. }
  933. /**
  934. * returns a normalized gaussian curve used to filter stuff
  935. * quality=3 is high quality, lowwer is lowwer quality
  936. */
  937. double *getGaussian(double variance, double quality){
  938. const int length= (int)(variance*quality + 0.5) | 1;
  939. int i;
  940. double *coeff= memalign(sizeof(double), length*sizeof(double));
  941. double middle= (length-1)*0.5;
  942. for(i=0; i<length; i++)
  943. {
  944. double dist= i-middle;
  945. coeff[i]= exp( -dist*dist/(2*variance*variance) ) / sqrt(2*variance*PI);
  946. }
  947. normalize(coeff, length, 1.0);
  948. return coeff;
  949. }
  950. void normalize(double *coeff, int length, double height){
  951. int i;
  952. double sum=0;
  953. double inv;
  954. for(i=0; i<length; i++)
  955. sum+= coeff[i];
  956. inv= height/sum;
  957. for(i=0; i<length; i++)
  958. coeff[i]*= height;
  959. }
  960. double *conv(double *a, int aLength, double *b, int bLength){
  961. int length= aLength + bLength - 1;
  962. double *coeff= memalign(sizeof(double), length*sizeof(double));
  963. int i, j;
  964. for(i=0; i<length; i++) coeff[i]= 0.0;
  965. for(i=0; i<aLength; i++)
  966. {
  967. for(j=0; j<bLength; j++)
  968. {
  969. coeff[i+j]+= a[i]*b[j];
  970. }
  971. }
  972. return coeff;
  973. }
  974. /*
  975. double *sum(double *a, int aLength, double *b, int bLength){
  976. int length= MAX(aLength, bLength);
  977. double *coeff= memalign(sizeof(double), length*sizeof(double));
  978. int i;
  979. for(i=0; i<length; i++) coeff[i]= 0.0;
  980. for(i=0; i<aLength; i++) coeff[i]+= a[i];
  981. }
  982. */