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.

2724 lines
76KB

  1. /*
  2. Copyright (C) 2001-2003 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. */
  15. /*
  16. supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR24, BGR16, BGR15, RGB32, RGB24, Y8/Y800, YVU9/IF09
  17. supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
  18. {BGR,RGB}{1,4,8,15,16} support dithering
  19. unscaled special converters (YV12=I420=IYUV, Y800=Y8)
  20. YV12 -> {BGR,RGB}{1,4,8,15,16,24,32}
  21. x -> x
  22. YUV9 -> YV12
  23. YUV9/YV12 -> Y800
  24. Y800 -> YUV9/YV12
  25. BGR24 -> BGR32 & RGB24 -> RGB32
  26. BGR32 -> BGR24 & RGB32 -> RGB24
  27. BGR15 -> BGR16
  28. */
  29. /*
  30. tested special converters (most are tested actually but i didnt write it down ...)
  31. YV12 -> BGR16
  32. YV12 -> YV12
  33. BGR15 -> BGR16
  34. BGR16 -> BGR16
  35. YVU9 -> YV12
  36. untested special converters
  37. YV12/I420 -> BGR15/BGR24/BGR32 (its the yuv2rgb stuff, so it should be ok)
  38. YV12/I420 -> YV12/I420
  39. YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
  40. BGR24 -> BGR32 & RGB24 -> RGB32
  41. BGR32 -> BGR24 & RGB32 -> RGB24
  42. BGR24 -> YV12
  43. */
  44. #include <inttypes.h>
  45. #include <string.h>
  46. #include <math.h>
  47. #include <stdio.h>
  48. #include <unistd.h>
  49. #include "config.h"
  50. #include <assert.h>
  51. #ifdef HAVE_MALLOC_H
  52. #include <malloc.h>
  53. #else
  54. #include <stdlib.h>
  55. #endif
  56. #ifdef HAVE_SYS_MMAN_H
  57. #include <sys/mman.h>
  58. #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
  59. #define MAP_ANONYMOUS MAP_ANON
  60. #endif
  61. #endif
  62. #include "swscale.h"
  63. #include "swscale_internal.h"
  64. #include "x86_cpu.h"
  65. #include "bswap.h"
  66. #include "img_format.h"
  67. #include "rgb2rgb.h"
  68. #ifdef USE_FASTMEMCPY
  69. #include "libvo/fastmemcpy.h"
  70. #endif
  71. #undef MOVNTQ
  72. #undef PAVGB
  73. //#undef HAVE_MMX2
  74. //#define HAVE_3DNOW
  75. //#undef HAVE_MMX
  76. //#undef ARCH_X86
  77. //#define WORDS_BIGENDIAN
  78. #define DITHER1XBPP
  79. #define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
  80. #define RET 0xC3 //near return opcode for X86
  81. #ifdef MP_DEBUG
  82. #define ASSERT(x) assert(x);
  83. #else
  84. #define ASSERT(x) ;
  85. #endif
  86. #ifdef M_PI
  87. #define PI M_PI
  88. #else
  89. #define PI 3.14159265358979323846
  90. #endif
  91. //FIXME replace this with something faster
  92. #define isPlanarYUV(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_YVU9 \
  93. || (x)==IMGFMT_NV12 || (x)==IMGFMT_NV21 \
  94. || (x)==IMGFMT_444P || (x)==IMGFMT_422P || (x)==IMGFMT_411P)
  95. #define isYUV(x) ((x)==IMGFMT_UYVY || (x)==IMGFMT_YUY2 || isPlanarYUV(x))
  96. #define isGray(x) ((x)==IMGFMT_Y800)
  97. #define isRGB(x) (((x)&IMGFMT_RGB_MASK)==IMGFMT_RGB)
  98. #define isBGR(x) (((x)&IMGFMT_BGR_MASK)==IMGFMT_BGR)
  99. #define isSupportedIn(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_YUY2 || (x)==IMGFMT_UYVY\
  100. || (x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15\
  101. || (x)==IMGFMT_RGB32|| (x)==IMGFMT_RGB24\
  102. || (x)==IMGFMT_Y800 || (x)==IMGFMT_YVU9\
  103. || (x)==IMGFMT_444P || (x)==IMGFMT_422P || (x)==IMGFMT_411P)
  104. #define isSupportedOut(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_YUY2 || (x)==IMGFMT_UYVY\
  105. || (x)==IMGFMT_444P || (x)==IMGFMT_422P || (x)==IMGFMT_411P\
  106. || isRGB(x) || isBGR(x)\
  107. || (x)==IMGFMT_NV12 || (x)==IMGFMT_NV21\
  108. || (x)==IMGFMT_Y800 || (x)==IMGFMT_YVU9)
  109. #define isPacked(x) ((x)==IMGFMT_YUY2 || (x)==IMGFMT_UYVY ||isRGB(x) || isBGR(x))
  110. #define RGB2YUV_SHIFT 16
  111. #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
  112. #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
  113. #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  114. #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
  115. #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
  116. #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
  117. #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
  118. #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  119. #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
  120. extern const int32_t Inverse_Table_6_9[8][4];
  121. /*
  122. NOTES
  123. Special versions: fast Y 1:1 scaling (no interpolation in y direction)
  124. TODO
  125. more intelligent missalignment avoidance for the horizontal scaler
  126. write special vertical cubic upscale version
  127. Optimize C code (yv12 / minmax)
  128. add support for packed pixel yuv input & output
  129. add support for Y8 output
  130. optimize bgr24 & bgr32
  131. add BGR4 output support
  132. write special BGR->BGR scaler
  133. */
  134. #define MIN(a,b) ((a) > (b) ? (b) : (a))
  135. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  136. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  137. static uint64_t attribute_used __attribute__((aligned(8))) bF8= 0xF8F8F8F8F8F8F8F8LL;
  138. static uint64_t attribute_used __attribute__((aligned(8))) bFC= 0xFCFCFCFCFCFCFCFCLL;
  139. static uint64_t __attribute__((aligned(8))) w10= 0x0010001000100010LL;
  140. static uint64_t attribute_used __attribute__((aligned(8))) w02= 0x0002000200020002LL;
  141. static uint64_t attribute_used __attribute__((aligned(8))) bm00001111=0x00000000FFFFFFFFLL;
  142. static uint64_t attribute_used __attribute__((aligned(8))) bm00000111=0x0000000000FFFFFFLL;
  143. static uint64_t attribute_used __attribute__((aligned(8))) bm11111000=0xFFFFFFFFFF000000LL;
  144. static uint64_t attribute_used __attribute__((aligned(8))) bm01010101=0x00FF00FF00FF00FFLL;
  145. static volatile uint64_t attribute_used __attribute__((aligned(8))) b5Dither;
  146. static volatile uint64_t attribute_used __attribute__((aligned(8))) g5Dither;
  147. static volatile uint64_t attribute_used __attribute__((aligned(8))) g6Dither;
  148. static volatile uint64_t attribute_used __attribute__((aligned(8))) r5Dither;
  149. static uint64_t __attribute__((aligned(8))) dither4[2]={
  150. 0x0103010301030103LL,
  151. 0x0200020002000200LL,};
  152. static uint64_t __attribute__((aligned(8))) dither8[2]={
  153. 0x0602060206020602LL,
  154. 0x0004000400040004LL,};
  155. static uint64_t __attribute__((aligned(8))) b16Mask= 0x001F001F001F001FLL;
  156. static uint64_t attribute_used __attribute__((aligned(8))) g16Mask= 0x07E007E007E007E0LL;
  157. static uint64_t attribute_used __attribute__((aligned(8))) r16Mask= 0xF800F800F800F800LL;
  158. static uint64_t __attribute__((aligned(8))) b15Mask= 0x001F001F001F001FLL;
  159. static uint64_t attribute_used __attribute__((aligned(8))) g15Mask= 0x03E003E003E003E0LL;
  160. static uint64_t attribute_used __attribute__((aligned(8))) r15Mask= 0x7C007C007C007C00LL;
  161. static uint64_t attribute_used __attribute__((aligned(8))) M24A= 0x00FF0000FF0000FFLL;
  162. static uint64_t attribute_used __attribute__((aligned(8))) M24B= 0xFF0000FF0000FF00LL;
  163. static uint64_t attribute_used __attribute__((aligned(8))) M24C= 0x0000FF0000FF0000LL;
  164. #ifdef FAST_BGR2YV12
  165. static const uint64_t bgr2YCoeff attribute_used __attribute__((aligned(8))) = 0x000000210041000DULL;
  166. static const uint64_t bgr2UCoeff attribute_used __attribute__((aligned(8))) = 0x0000FFEEFFDC0038ULL;
  167. static const uint64_t bgr2VCoeff attribute_used __attribute__((aligned(8))) = 0x00000038FFD2FFF8ULL;
  168. #else
  169. static const uint64_t bgr2YCoeff attribute_used __attribute__((aligned(8))) = 0x000020E540830C8BULL;
  170. static const uint64_t bgr2UCoeff attribute_used __attribute__((aligned(8))) = 0x0000ED0FDAC23831ULL;
  171. static const uint64_t bgr2VCoeff attribute_used __attribute__((aligned(8))) = 0x00003831D0E6F6EAULL;
  172. #endif
  173. static const uint64_t bgr2YOffset attribute_used __attribute__((aligned(8))) = 0x1010101010101010ULL;
  174. static const uint64_t bgr2UVOffset attribute_used __attribute__((aligned(8)))= 0x8080808080808080ULL;
  175. static const uint64_t w1111 attribute_used __attribute__((aligned(8))) = 0x0001000100010001ULL;
  176. #endif
  177. // clipping helper table for C implementations:
  178. static unsigned char clip_table[768];
  179. static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
  180. extern const uint8_t dither_2x2_4[2][8];
  181. extern const uint8_t dither_2x2_8[2][8];
  182. extern const uint8_t dither_8x8_32[8][8];
  183. extern const uint8_t dither_8x8_73[8][8];
  184. extern const uint8_t dither_8x8_220[8][8];
  185. char *sws_format_name(int format)
  186. {
  187. static char fmt_name[64];
  188. char *res;
  189. static int buffer;
  190. res = fmt_name + buffer * 32;
  191. buffer = 1 - buffer;
  192. snprintf(res, 32, "0x%x (%c%c%c%c)", format,
  193. format >> 24, (format >> 16) & 0xFF,
  194. (format >> 8) & 0xFF,
  195. format & 0xFF);
  196. return res;
  197. }
  198. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  199. void in_asm_used_var_warning_killer()
  200. {
  201. volatile int i= bF8+bFC+w10+
  202. bm00001111+bm00000111+bm11111000+b16Mask+g16Mask+r16Mask+b15Mask+g15Mask+r15Mask+
  203. M24A+M24B+M24C+w02 + b5Dither+g5Dither+r5Dither+g6Dither+dither4[0]+dither8[0]+bm01010101;
  204. if(i) i=0;
  205. }
  206. #endif
  207. static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  208. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  209. uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW, int chrDstW)
  210. {
  211. //FIXME Optimize (just quickly writen not opti..)
  212. int i;
  213. for(i=0; i<dstW; i++)
  214. {
  215. int val=1<<18;
  216. int j;
  217. for(j=0; j<lumFilterSize; j++)
  218. val += lumSrc[j][i] * lumFilter[j];
  219. dest[i]= MIN(MAX(val>>19, 0), 255);
  220. }
  221. if(uDest != NULL)
  222. for(i=0; i<chrDstW; i++)
  223. {
  224. int u=1<<18;
  225. int v=1<<18;
  226. int j;
  227. for(j=0; j<chrFilterSize; j++)
  228. {
  229. u += chrSrc[j][i] * chrFilter[j];
  230. v += chrSrc[j][i + 2048] * chrFilter[j];
  231. }
  232. uDest[i]= MIN(MAX(u>>19, 0), 255);
  233. vDest[i]= MIN(MAX(v>>19, 0), 255);
  234. }
  235. }
  236. static inline void yuv2nv12XinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  237. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  238. uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
  239. {
  240. //FIXME Optimize (just quickly writen not opti..)
  241. int i;
  242. for(i=0; i<dstW; i++)
  243. {
  244. int val=1<<18;
  245. int j;
  246. for(j=0; j<lumFilterSize; j++)
  247. val += lumSrc[j][i] * lumFilter[j];
  248. dest[i]= MIN(MAX(val>>19, 0), 255);
  249. }
  250. if(uDest == NULL)
  251. return;
  252. if(dstFormat == IMGFMT_NV12)
  253. for(i=0; i<chrDstW; i++)
  254. {
  255. int u=1<<18;
  256. int v=1<<18;
  257. int j;
  258. for(j=0; j<chrFilterSize; j++)
  259. {
  260. u += chrSrc[j][i] * chrFilter[j];
  261. v += chrSrc[j][i + 2048] * chrFilter[j];
  262. }
  263. uDest[2*i]= MIN(MAX(u>>19, 0), 255);
  264. uDest[2*i+1]= MIN(MAX(v>>19, 0), 255);
  265. }
  266. else
  267. for(i=0; i<chrDstW; i++)
  268. {
  269. int u=1<<18;
  270. int v=1<<18;
  271. int j;
  272. for(j=0; j<chrFilterSize; j++)
  273. {
  274. u += chrSrc[j][i] * chrFilter[j];
  275. v += chrSrc[j][i + 2048] * chrFilter[j];
  276. }
  277. uDest[2*i]= MIN(MAX(v>>19, 0), 255);
  278. uDest[2*i+1]= MIN(MAX(u>>19, 0), 255);
  279. }
  280. }
  281. #define YSCALE_YUV_2_PACKEDX_C(type) \
  282. for(i=0; i<(dstW>>1); i++){\
  283. int j;\
  284. int Y1=1<<18;\
  285. int Y2=1<<18;\
  286. int U=1<<18;\
  287. int V=1<<18;\
  288. type *r, *b, *g;\
  289. const int i2= 2*i;\
  290. \
  291. for(j=0; j<lumFilterSize; j++)\
  292. {\
  293. Y1 += lumSrc[j][i2] * lumFilter[j];\
  294. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  295. }\
  296. for(j=0; j<chrFilterSize; j++)\
  297. {\
  298. U += chrSrc[j][i] * chrFilter[j];\
  299. V += chrSrc[j][i+2048] * chrFilter[j];\
  300. }\
  301. Y1>>=19;\
  302. Y2>>=19;\
  303. U >>=19;\
  304. V >>=19;\
  305. if((Y1|Y2|U|V)&256)\
  306. {\
  307. if(Y1>255) Y1=255;\
  308. else if(Y1<0)Y1=0;\
  309. if(Y2>255) Y2=255;\
  310. else if(Y2<0)Y2=0;\
  311. if(U>255) U=255;\
  312. else if(U<0) U=0;\
  313. if(V>255) V=255;\
  314. else if(V<0) V=0;\
  315. }
  316. #define YSCALE_YUV_2_RGBX_C(type) \
  317. YSCALE_YUV_2_PACKEDX_C(type)\
  318. r = c->table_rV[V];\
  319. g = c->table_gU[U] + c->table_gV[V];\
  320. b = c->table_bU[U];\
  321. #define YSCALE_YUV_2_PACKED2_C \
  322. for(i=0; i<(dstW>>1); i++){\
  323. const int i2= 2*i;\
  324. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19;\
  325. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19;\
  326. int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19;\
  327. int V= (uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>19;\
  328. #define YSCALE_YUV_2_RGB2_C(type) \
  329. YSCALE_YUV_2_PACKED2_C\
  330. type *r, *b, *g;\
  331. r = c->table_rV[V];\
  332. g = c->table_gU[U] + c->table_gV[V];\
  333. b = c->table_bU[U];\
  334. #define YSCALE_YUV_2_PACKED1_C \
  335. for(i=0; i<(dstW>>1); i++){\
  336. const int i2= 2*i;\
  337. int Y1= buf0[i2 ]>>7;\
  338. int Y2= buf0[i2+1]>>7;\
  339. int U= (uvbuf1[i ])>>7;\
  340. int V= (uvbuf1[i+2048])>>7;\
  341. #define YSCALE_YUV_2_RGB1_C(type) \
  342. YSCALE_YUV_2_PACKED1_C\
  343. type *r, *b, *g;\
  344. r = c->table_rV[V];\
  345. g = c->table_gU[U] + c->table_gV[V];\
  346. b = c->table_bU[U];\
  347. #define YSCALE_YUV_2_PACKED1B_C \
  348. for(i=0; i<(dstW>>1); i++){\
  349. const int i2= 2*i;\
  350. int Y1= buf0[i2 ]>>7;\
  351. int Y2= buf0[i2+1]>>7;\
  352. int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
  353. int V= (uvbuf0[i+2048] + uvbuf1[i+2048])>>8;\
  354. #define YSCALE_YUV_2_RGB1B_C(type) \
  355. YSCALE_YUV_2_PACKED1B_C\
  356. type *r, *b, *g;\
  357. r = c->table_rV[V];\
  358. g = c->table_gU[U] + c->table_gV[V];\
  359. b = c->table_bU[U];\
  360. #define YSCALE_YUV_2_ANYRGB_C(func, func2)\
  361. switch(c->dstFormat)\
  362. {\
  363. case IMGFMT_BGR32:\
  364. case IMGFMT_RGB32:\
  365. func(uint32_t)\
  366. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  367. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  368. } \
  369. break;\
  370. case IMGFMT_RGB24:\
  371. func(uint8_t)\
  372. ((uint8_t*)dest)[0]= r[Y1];\
  373. ((uint8_t*)dest)[1]= g[Y1];\
  374. ((uint8_t*)dest)[2]= b[Y1];\
  375. ((uint8_t*)dest)[3]= r[Y2];\
  376. ((uint8_t*)dest)[4]= g[Y2];\
  377. ((uint8_t*)dest)[5]= b[Y2];\
  378. dest+=6;\
  379. }\
  380. break;\
  381. case IMGFMT_BGR24:\
  382. func(uint8_t)\
  383. ((uint8_t*)dest)[0]= b[Y1];\
  384. ((uint8_t*)dest)[1]= g[Y1];\
  385. ((uint8_t*)dest)[2]= r[Y1];\
  386. ((uint8_t*)dest)[3]= b[Y2];\
  387. ((uint8_t*)dest)[4]= g[Y2];\
  388. ((uint8_t*)dest)[5]= r[Y2];\
  389. dest+=6;\
  390. }\
  391. break;\
  392. case IMGFMT_RGB16:\
  393. case IMGFMT_BGR16:\
  394. {\
  395. const int dr1= dither_2x2_8[y&1 ][0];\
  396. const int dg1= dither_2x2_4[y&1 ][0];\
  397. const int db1= dither_2x2_8[(y&1)^1][0];\
  398. const int dr2= dither_2x2_8[y&1 ][1];\
  399. const int dg2= dither_2x2_4[y&1 ][1];\
  400. const int db2= dither_2x2_8[(y&1)^1][1];\
  401. func(uint16_t)\
  402. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  403. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  404. }\
  405. }\
  406. break;\
  407. case IMGFMT_RGB15:\
  408. case IMGFMT_BGR15:\
  409. {\
  410. const int dr1= dither_2x2_8[y&1 ][0];\
  411. const int dg1= dither_2x2_8[y&1 ][1];\
  412. const int db1= dither_2x2_8[(y&1)^1][0];\
  413. const int dr2= dither_2x2_8[y&1 ][1];\
  414. const int dg2= dither_2x2_8[y&1 ][0];\
  415. const int db2= dither_2x2_8[(y&1)^1][1];\
  416. func(uint16_t)\
  417. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  418. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  419. }\
  420. }\
  421. break;\
  422. case IMGFMT_RGB8:\
  423. case IMGFMT_BGR8:\
  424. {\
  425. const uint8_t * const d64= dither_8x8_73[y&7];\
  426. const uint8_t * const d32= dither_8x8_32[y&7];\
  427. func(uint8_t)\
  428. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
  429. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
  430. }\
  431. }\
  432. break;\
  433. case IMGFMT_RGB4:\
  434. case IMGFMT_BGR4:\
  435. {\
  436. const uint8_t * const d64= dither_8x8_73 [y&7];\
  437. const uint8_t * const d128=dither_8x8_220[y&7];\
  438. func(uint8_t)\
  439. ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
  440. + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
  441. }\
  442. }\
  443. break;\
  444. case IMGFMT_RG4B:\
  445. case IMGFMT_BG4B:\
  446. {\
  447. const uint8_t * const d64= dither_8x8_73 [y&7];\
  448. const uint8_t * const d128=dither_8x8_220[y&7];\
  449. func(uint8_t)\
  450. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
  451. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
  452. }\
  453. }\
  454. break;\
  455. case IMGFMT_RGB1:\
  456. case IMGFMT_BGR1:\
  457. {\
  458. const uint8_t * const d128=dither_8x8_220[y&7];\
  459. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  460. for(i=0; i<dstW-7; i+=8){\
  461. int acc;\
  462. acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
  463. acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
  464. acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
  465. acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
  466. acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
  467. acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
  468. acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
  469. acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
  470. ((uint8_t*)dest)[0]= acc;\
  471. dest++;\
  472. }\
  473. \
  474. /*\
  475. ((uint8_t*)dest)-= dstW>>4;\
  476. {\
  477. int acc=0;\
  478. int left=0;\
  479. static int top[1024];\
  480. static int last_new[1024][1024];\
  481. static int last_in3[1024][1024];\
  482. static int drift[1024][1024];\
  483. int topLeft=0;\
  484. int shift=0;\
  485. int count=0;\
  486. const uint8_t * const d128=dither_8x8_220[y&7];\
  487. int error_new=0;\
  488. int error_in3=0;\
  489. int f=0;\
  490. \
  491. for(i=dstW>>1; i<dstW; i++){\
  492. int in= ((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19);\
  493. int in2 = (76309 * (in - 16) + 32768) >> 16;\
  494. int in3 = (in2 < 0) ? 0 : ((in2 > 255) ? 255 : in2);\
  495. int old= (left*7 + topLeft + top[i]*5 + top[i+1]*3)/20 + in3\
  496. + (last_new[y][i] - in3)*f/256;\
  497. int new= old> 128 ? 255 : 0;\
  498. \
  499. error_new+= ABS(last_new[y][i] - new);\
  500. error_in3+= ABS(last_in3[y][i] - in3);\
  501. f= error_new - error_in3*4;\
  502. if(f<0) f=0;\
  503. if(f>256) f=256;\
  504. \
  505. topLeft= top[i];\
  506. left= top[i]= old - new;\
  507. last_new[y][i]= new;\
  508. last_in3[y][i]= in3;\
  509. \
  510. acc+= acc + (new&1);\
  511. if((i&7)==6){\
  512. ((uint8_t*)dest)[0]= acc;\
  513. ((uint8_t*)dest)++;\
  514. }\
  515. }\
  516. }\
  517. */\
  518. }\
  519. break;\
  520. case IMGFMT_YUY2:\
  521. func2\
  522. ((uint8_t*)dest)[2*i2+0]= Y1;\
  523. ((uint8_t*)dest)[2*i2+1]= U;\
  524. ((uint8_t*)dest)[2*i2+2]= Y2;\
  525. ((uint8_t*)dest)[2*i2+3]= V;\
  526. } \
  527. break;\
  528. case IMGFMT_UYVY:\
  529. func2\
  530. ((uint8_t*)dest)[2*i2+0]= U;\
  531. ((uint8_t*)dest)[2*i2+1]= Y1;\
  532. ((uint8_t*)dest)[2*i2+2]= V;\
  533. ((uint8_t*)dest)[2*i2+3]= Y2;\
  534. } \
  535. break;\
  536. }\
  537. static inline void yuv2packedXinC(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  538. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  539. uint8_t *dest, int dstW, int y)
  540. {
  541. int i;
  542. switch(c->dstFormat)
  543. {
  544. case IMGFMT_RGB32:
  545. case IMGFMT_BGR32:
  546. YSCALE_YUV_2_RGBX_C(uint32_t)
  547. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];
  548. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];
  549. }
  550. break;
  551. case IMGFMT_RGB24:
  552. YSCALE_YUV_2_RGBX_C(uint8_t)
  553. ((uint8_t*)dest)[0]= r[Y1];
  554. ((uint8_t*)dest)[1]= g[Y1];
  555. ((uint8_t*)dest)[2]= b[Y1];
  556. ((uint8_t*)dest)[3]= r[Y2];
  557. ((uint8_t*)dest)[4]= g[Y2];
  558. ((uint8_t*)dest)[5]= b[Y2];
  559. dest+=6;
  560. }
  561. break;
  562. case IMGFMT_BGR24:
  563. YSCALE_YUV_2_RGBX_C(uint8_t)
  564. ((uint8_t*)dest)[0]= b[Y1];
  565. ((uint8_t*)dest)[1]= g[Y1];
  566. ((uint8_t*)dest)[2]= r[Y1];
  567. ((uint8_t*)dest)[3]= b[Y2];
  568. ((uint8_t*)dest)[4]= g[Y2];
  569. ((uint8_t*)dest)[5]= r[Y2];
  570. dest+=6;
  571. }
  572. break;
  573. case IMGFMT_RGB16:
  574. case IMGFMT_BGR16:
  575. {
  576. const int dr1= dither_2x2_8[y&1 ][0];
  577. const int dg1= dither_2x2_4[y&1 ][0];
  578. const int db1= dither_2x2_8[(y&1)^1][0];
  579. const int dr2= dither_2x2_8[y&1 ][1];
  580. const int dg2= dither_2x2_4[y&1 ][1];
  581. const int db2= dither_2x2_8[(y&1)^1][1];
  582. YSCALE_YUV_2_RGBX_C(uint16_t)
  583. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
  584. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
  585. }
  586. }
  587. break;
  588. case IMGFMT_RGB15:
  589. case IMGFMT_BGR15:
  590. {
  591. const int dr1= dither_2x2_8[y&1 ][0];
  592. const int dg1= dither_2x2_8[y&1 ][1];
  593. const int db1= dither_2x2_8[(y&1)^1][0];
  594. const int dr2= dither_2x2_8[y&1 ][1];
  595. const int dg2= dither_2x2_8[y&1 ][0];
  596. const int db2= dither_2x2_8[(y&1)^1][1];
  597. YSCALE_YUV_2_RGBX_C(uint16_t)
  598. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
  599. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
  600. }
  601. }
  602. break;
  603. case IMGFMT_RGB8:
  604. case IMGFMT_BGR8:
  605. {
  606. const uint8_t * const d64= dither_8x8_73[y&7];
  607. const uint8_t * const d32= dither_8x8_32[y&7];
  608. YSCALE_YUV_2_RGBX_C(uint8_t)
  609. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];
  610. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];
  611. }
  612. }
  613. break;
  614. case IMGFMT_RGB4:
  615. case IMGFMT_BGR4:
  616. {
  617. const uint8_t * const d64= dither_8x8_73 [y&7];
  618. const uint8_t * const d128=dither_8x8_220[y&7];
  619. YSCALE_YUV_2_RGBX_C(uint8_t)
  620. ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]
  621. +((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);
  622. }
  623. }
  624. break;
  625. case IMGFMT_RG4B:
  626. case IMGFMT_BG4B:
  627. {
  628. const uint8_t * const d64= dither_8x8_73 [y&7];
  629. const uint8_t * const d128=dither_8x8_220[y&7];
  630. YSCALE_YUV_2_RGBX_C(uint8_t)
  631. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];
  632. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];
  633. }
  634. }
  635. break;
  636. case IMGFMT_RGB1:
  637. case IMGFMT_BGR1:
  638. {
  639. const uint8_t * const d128=dither_8x8_220[y&7];
  640. uint8_t *g= c->table_gU[128] + c->table_gV[128];
  641. int acc=0;
  642. for(i=0; i<dstW-1; i+=2){
  643. int j;
  644. int Y1=1<<18;
  645. int Y2=1<<18;
  646. for(j=0; j<lumFilterSize; j++)
  647. {
  648. Y1 += lumSrc[j][i] * lumFilter[j];
  649. Y2 += lumSrc[j][i+1] * lumFilter[j];
  650. }
  651. Y1>>=19;
  652. Y2>>=19;
  653. if((Y1|Y2)&256)
  654. {
  655. if(Y1>255) Y1=255;
  656. else if(Y1<0)Y1=0;
  657. if(Y2>255) Y2=255;
  658. else if(Y2<0)Y2=0;
  659. }
  660. acc+= acc + g[Y1+d128[(i+0)&7]];
  661. acc+= acc + g[Y2+d128[(i+1)&7]];
  662. if((i&7)==6){
  663. ((uint8_t*)dest)[0]= acc;
  664. dest++;
  665. }
  666. }
  667. }
  668. break;
  669. case IMGFMT_YUY2:
  670. YSCALE_YUV_2_PACKEDX_C(void)
  671. ((uint8_t*)dest)[2*i2+0]= Y1;
  672. ((uint8_t*)dest)[2*i2+1]= U;
  673. ((uint8_t*)dest)[2*i2+2]= Y2;
  674. ((uint8_t*)dest)[2*i2+3]= V;
  675. }
  676. break;
  677. case IMGFMT_UYVY:
  678. YSCALE_YUV_2_PACKEDX_C(void)
  679. ((uint8_t*)dest)[2*i2+0]= U;
  680. ((uint8_t*)dest)[2*i2+1]= Y1;
  681. ((uint8_t*)dest)[2*i2+2]= V;
  682. ((uint8_t*)dest)[2*i2+3]= Y2;
  683. }
  684. break;
  685. }
  686. }
  687. //Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
  688. //Plain C versions
  689. #if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT)
  690. #define COMPILE_C
  691. #endif
  692. #ifdef ARCH_POWERPC
  693. #if defined (HAVE_ALTIVEC) || defined (RUNTIME_CPUDETECT)
  694. #define COMPILE_ALTIVEC
  695. #endif //HAVE_ALTIVEC
  696. #endif //ARCH_POWERPC
  697. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  698. #if (defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  699. #define COMPILE_MMX
  700. #endif
  701. #if defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)
  702. #define COMPILE_MMX2
  703. #endif
  704. #if (defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  705. #define COMPILE_3DNOW
  706. #endif
  707. #endif //ARCH_X86 || ARCH_X86_64
  708. #undef HAVE_MMX
  709. #undef HAVE_MMX2
  710. #undef HAVE_3DNOW
  711. #ifdef COMPILE_C
  712. #undef HAVE_MMX
  713. #undef HAVE_MMX2
  714. #undef HAVE_3DNOW
  715. #undef HAVE_ALTIVEC
  716. #define RENAME(a) a ## _C
  717. #include "swscale_template.c"
  718. #endif
  719. #ifdef ARCH_POWERPC
  720. #ifdef COMPILE_ALTIVEC
  721. #undef RENAME
  722. #define HAVE_ALTIVEC
  723. #define RENAME(a) a ## _altivec
  724. #include "swscale_template.c"
  725. #endif
  726. #endif //ARCH_POWERPC
  727. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  728. //X86 versions
  729. /*
  730. #undef RENAME
  731. #undef HAVE_MMX
  732. #undef HAVE_MMX2
  733. #undef HAVE_3DNOW
  734. #define ARCH_X86
  735. #define RENAME(a) a ## _X86
  736. #include "swscale_template.c"
  737. */
  738. //MMX versions
  739. #ifdef COMPILE_MMX
  740. #undef RENAME
  741. #define HAVE_MMX
  742. #undef HAVE_MMX2
  743. #undef HAVE_3DNOW
  744. #define RENAME(a) a ## _MMX
  745. #include "swscale_template.c"
  746. #endif
  747. //MMX2 versions
  748. #ifdef COMPILE_MMX2
  749. #undef RENAME
  750. #define HAVE_MMX
  751. #define HAVE_MMX2
  752. #undef HAVE_3DNOW
  753. #define RENAME(a) a ## _MMX2
  754. #include "swscale_template.c"
  755. #endif
  756. //3DNOW versions
  757. #ifdef COMPILE_3DNOW
  758. #undef RENAME
  759. #define HAVE_MMX
  760. #undef HAVE_MMX2
  761. #define HAVE_3DNOW
  762. #define RENAME(a) a ## _3DNow
  763. #include "swscale_template.c"
  764. #endif
  765. #endif //ARCH_X86 || ARCH_X86_64
  766. // minor note: the HAVE_xyz is messed up after that line so don't use it
  767. static double getSplineCoeff(double a, double b, double c, double d, double dist)
  768. {
  769. // printf("%f %f %f %f %f\n", a,b,c,d,dist);
  770. if(dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
  771. else return getSplineCoeff( 0.0,
  772. b+ 2.0*c + 3.0*d,
  773. c + 3.0*d,
  774. -b- 3.0*c - 6.0*d,
  775. dist-1.0);
  776. }
  777. static inline void initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
  778. int srcW, int dstW, int filterAlign, int one, int flags,
  779. SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
  780. {
  781. int i;
  782. int filterSize;
  783. int filter2Size;
  784. int minFilterSize;
  785. double *filter=NULL;
  786. double *filter2=NULL;
  787. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  788. if(flags & SWS_CPU_CAPS_MMX)
  789. asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
  790. #endif
  791. // Note the +1 is for the MMXscaler which reads over the end
  792. *filterPos = av_malloc((dstW+1)*sizeof(int16_t));
  793. if(ABS(xInc - 0x10000) <10) // unscaled
  794. {
  795. int i;
  796. filterSize= 1;
  797. filter= av_malloc(dstW*sizeof(double)*filterSize);
  798. for(i=0; i<dstW*filterSize; i++) filter[i]=0;
  799. for(i=0; i<dstW; i++)
  800. {
  801. filter[i*filterSize]=1;
  802. (*filterPos)[i]=i;
  803. }
  804. }
  805. else if(flags&SWS_POINT) // lame looking point sampling mode
  806. {
  807. int i;
  808. int xDstInSrc;
  809. filterSize= 1;
  810. filter= av_malloc(dstW*sizeof(double)*filterSize);
  811. xDstInSrc= xInc/2 - 0x8000;
  812. for(i=0; i<dstW; i++)
  813. {
  814. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  815. (*filterPos)[i]= xx;
  816. filter[i]= 1.0;
  817. xDstInSrc+= xInc;
  818. }
  819. }
  820. else if((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
  821. {
  822. int i;
  823. int xDstInSrc;
  824. if (flags&SWS_BICUBIC) filterSize= 4;
  825. else if(flags&SWS_X ) filterSize= 4;
  826. else filterSize= 2; // SWS_BILINEAR / SWS_AREA
  827. filter= av_malloc(dstW*sizeof(double)*filterSize);
  828. xDstInSrc= xInc/2 - 0x8000;
  829. for(i=0; i<dstW; i++)
  830. {
  831. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  832. int j;
  833. (*filterPos)[i]= xx;
  834. //Bilinear upscale / linear interpolate / Area averaging
  835. for(j=0; j<filterSize; j++)
  836. {
  837. double d= ABS((xx<<16) - xDstInSrc)/(double)(1<<16);
  838. double coeff= 1.0 - d;
  839. if(coeff<0) coeff=0;
  840. filter[i*filterSize + j]= coeff;
  841. xx++;
  842. }
  843. xDstInSrc+= xInc;
  844. }
  845. }
  846. else
  847. {
  848. double xDstInSrc;
  849. double sizeFactor, filterSizeInSrc;
  850. const double xInc1= (double)xInc / (double)(1<<16);
  851. if (flags&SWS_BICUBIC) sizeFactor= 4.0;
  852. else if(flags&SWS_X) sizeFactor= 8.0;
  853. else if(flags&SWS_AREA) sizeFactor= 1.0; //downscale only, for upscale it is bilinear
  854. else if(flags&SWS_GAUSS) sizeFactor= 8.0; // infinite ;)
  855. else if(flags&SWS_LANCZOS) sizeFactor= param[0] != SWS_PARAM_DEFAULT ? 2.0*param[0] : 6.0;
  856. else if(flags&SWS_SINC) sizeFactor= 20.0; // infinite ;)
  857. else if(flags&SWS_SPLINE) sizeFactor= 20.0; // infinite ;)
  858. else if(flags&SWS_BILINEAR) sizeFactor= 2.0;
  859. else {
  860. sizeFactor= 0.0; //GCC warning killer
  861. ASSERT(0)
  862. }
  863. if(xInc1 <= 1.0) filterSizeInSrc= sizeFactor; // upscale
  864. else filterSizeInSrc= sizeFactor*srcW / (double)dstW;
  865. filterSize= (int)ceil(1 + filterSizeInSrc); // will be reduced later if possible
  866. if(filterSize > srcW-2) filterSize=srcW-2;
  867. filter= av_malloc(dstW*sizeof(double)*filterSize);
  868. xDstInSrc= xInc1 / 2.0 - 0.5;
  869. for(i=0; i<dstW; i++)
  870. {
  871. int xx= (int)(xDstInSrc - (filterSize-1)*0.5 + 0.5);
  872. int j;
  873. (*filterPos)[i]= xx;
  874. for(j=0; j<filterSize; j++)
  875. {
  876. double d= ABS(xx - xDstInSrc)/filterSizeInSrc*sizeFactor;
  877. double coeff;
  878. if(flags & SWS_BICUBIC)
  879. {
  880. double B= param[0] != SWS_PARAM_DEFAULT ? param[0] : 0.0;
  881. double C= param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6;
  882. if(d<1.0)
  883. coeff = (12-9*B-6*C)*d*d*d + (-18+12*B+6*C)*d*d + 6-2*B;
  884. else if(d<2.0)
  885. coeff = (-B-6*C)*d*d*d + (6*B+30*C)*d*d + (-12*B-48*C)*d +8*B+24*C;
  886. else
  887. coeff=0.0;
  888. }
  889. /* else if(flags & SWS_X)
  890. {
  891. double p= param ? param*0.01 : 0.3;
  892. coeff = d ? sin(d*PI)/(d*PI) : 1.0;
  893. coeff*= pow(2.0, - p*d*d);
  894. }*/
  895. else if(flags & SWS_X)
  896. {
  897. double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
  898. if(d<1.0)
  899. coeff = cos(d*PI);
  900. else
  901. coeff=-1.0;
  902. if(coeff<0.0) coeff= -pow(-coeff, A);
  903. else coeff= pow( coeff, A);
  904. coeff= coeff*0.5 + 0.5;
  905. }
  906. else if(flags & SWS_AREA)
  907. {
  908. double srcPixelSize= 1.0/xInc1;
  909. if(d + srcPixelSize/2 < 0.5) coeff= 1.0;
  910. else if(d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;
  911. else coeff=0.0;
  912. }
  913. else if(flags & SWS_GAUSS)
  914. {
  915. double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
  916. coeff = pow(2.0, - p*d*d);
  917. }
  918. else if(flags & SWS_SINC)
  919. {
  920. coeff = d ? sin(d*PI)/(d*PI) : 1.0;
  921. }
  922. else if(flags & SWS_LANCZOS)
  923. {
  924. double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
  925. coeff = d ? sin(d*PI)*sin(d*PI/p)/(d*d*PI*PI/p) : 1.0;
  926. if(d>p) coeff=0;
  927. }
  928. else if(flags & SWS_BILINEAR)
  929. {
  930. coeff= 1.0 - d;
  931. if(coeff<0) coeff=0;
  932. }
  933. else if(flags & SWS_SPLINE)
  934. {
  935. double p=-2.196152422706632;
  936. coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, d);
  937. }
  938. else {
  939. coeff= 0.0; //GCC warning killer
  940. ASSERT(0)
  941. }
  942. filter[i*filterSize + j]= coeff;
  943. xx++;
  944. }
  945. xDstInSrc+= xInc1;
  946. }
  947. }
  948. /* apply src & dst Filter to filter -> filter2
  949. av_free(filter);
  950. */
  951. ASSERT(filterSize>0)
  952. filter2Size= filterSize;
  953. if(srcFilter) filter2Size+= srcFilter->length - 1;
  954. if(dstFilter) filter2Size+= dstFilter->length - 1;
  955. ASSERT(filter2Size>0)
  956. filter2= av_malloc(filter2Size*dstW*sizeof(double));
  957. for(i=0; i<dstW; i++)
  958. {
  959. int j;
  960. SwsVector scaleFilter;
  961. SwsVector *outVec;
  962. scaleFilter.coeff= filter + i*filterSize;
  963. scaleFilter.length= filterSize;
  964. if(srcFilter) outVec= sws_getConvVec(srcFilter, &scaleFilter);
  965. else outVec= &scaleFilter;
  966. ASSERT(outVec->length == filter2Size)
  967. //FIXME dstFilter
  968. for(j=0; j<outVec->length; j++)
  969. {
  970. filter2[i*filter2Size + j]= outVec->coeff[j];
  971. }
  972. (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
  973. if(outVec != &scaleFilter) sws_freeVec(outVec);
  974. }
  975. av_free(filter); filter=NULL;
  976. /* try to reduce the filter-size (step1 find size and shift left) */
  977. // Assume its near normalized (*0.5 or *2.0 is ok but * 0.001 is not)
  978. minFilterSize= 0;
  979. for(i=dstW-1; i>=0; i--)
  980. {
  981. int min= filter2Size;
  982. int j;
  983. double cutOff=0.0;
  984. /* get rid off near zero elements on the left by shifting left */
  985. for(j=0; j<filter2Size; j++)
  986. {
  987. int k;
  988. cutOff += ABS(filter2[i*filter2Size]);
  989. if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
  990. /* preserve Monotonicity because the core can't handle the filter otherwise */
  991. if(i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
  992. // Move filter coeffs left
  993. for(k=1; k<filter2Size; k++)
  994. filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
  995. filter2[i*filter2Size + k - 1]= 0.0;
  996. (*filterPos)[i]++;
  997. }
  998. cutOff=0.0;
  999. /* count near zeros on the right */
  1000. for(j=filter2Size-1; j>0; j--)
  1001. {
  1002. cutOff += ABS(filter2[i*filter2Size + j]);
  1003. if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
  1004. min--;
  1005. }
  1006. if(min>minFilterSize) minFilterSize= min;
  1007. }
  1008. if (flags & SWS_CPU_CAPS_ALTIVEC) {
  1009. // we can handle the special case 4,
  1010. // so we don't want to go to the full 8
  1011. if (minFilterSize < 5)
  1012. filterAlign = 4;
  1013. // we really don't want to waste our time
  1014. // doing useless computation, so fall-back on
  1015. // the scalar C code for very small filter.
  1016. // vectorizing is worth it only if you have
  1017. // decent-sized vector.
  1018. if (minFilterSize < 3)
  1019. filterAlign = 1;
  1020. }
  1021. ASSERT(minFilterSize > 0)
  1022. filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
  1023. ASSERT(filterSize > 0)
  1024. filter= av_malloc(filterSize*dstW*sizeof(double));
  1025. *outFilterSize= filterSize;
  1026. if(flags&SWS_PRINT_INFO)
  1027. MSG_V("SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
  1028. /* try to reduce the filter-size (step2 reduce it) */
  1029. for(i=0; i<dstW; i++)
  1030. {
  1031. int j;
  1032. for(j=0; j<filterSize; j++)
  1033. {
  1034. if(j>=filter2Size) filter[i*filterSize + j]= 0.0;
  1035. else filter[i*filterSize + j]= filter2[i*filter2Size + j];
  1036. }
  1037. }
  1038. av_free(filter2); filter2=NULL;
  1039. //FIXME try to align filterpos if possible
  1040. //fix borders
  1041. for(i=0; i<dstW; i++)
  1042. {
  1043. int j;
  1044. if((*filterPos)[i] < 0)
  1045. {
  1046. // Move filter coeffs left to compensate for filterPos
  1047. for(j=1; j<filterSize; j++)
  1048. {
  1049. int left= MAX(j + (*filterPos)[i], 0);
  1050. filter[i*filterSize + left] += filter[i*filterSize + j];
  1051. filter[i*filterSize + j]=0;
  1052. }
  1053. (*filterPos)[i]= 0;
  1054. }
  1055. if((*filterPos)[i] + filterSize > srcW)
  1056. {
  1057. int shift= (*filterPos)[i] + filterSize - srcW;
  1058. // Move filter coeffs right to compensate for filterPos
  1059. for(j=filterSize-2; j>=0; j--)
  1060. {
  1061. int right= MIN(j + shift, filterSize-1);
  1062. filter[i*filterSize +right] += filter[i*filterSize +j];
  1063. filter[i*filterSize +j]=0;
  1064. }
  1065. (*filterPos)[i]= srcW - filterSize;
  1066. }
  1067. }
  1068. // Note the +1 is for the MMXscaler which reads over the end
  1069. /* align at 16 for AltiVec (needed by hScale_altivec_real) */
  1070. *outFilter= av_malloc(*outFilterSize*(dstW+1)*sizeof(int16_t));
  1071. memset(*outFilter, 0, *outFilterSize*(dstW+1)*sizeof(int16_t));
  1072. /* Normalize & Store in outFilter */
  1073. for(i=0; i<dstW; i++)
  1074. {
  1075. int j;
  1076. double error=0;
  1077. double sum=0;
  1078. double scale= one;
  1079. for(j=0; j<filterSize; j++)
  1080. {
  1081. sum+= filter[i*filterSize + j];
  1082. }
  1083. scale/= sum;
  1084. for(j=0; j<*outFilterSize; j++)
  1085. {
  1086. double v= filter[i*filterSize + j]*scale + error;
  1087. int intV= floor(v + 0.5);
  1088. (*outFilter)[i*(*outFilterSize) + j]= intV;
  1089. error = v - intV;
  1090. }
  1091. }
  1092. (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
  1093. for(i=0; i<*outFilterSize; i++)
  1094. {
  1095. int j= dstW*(*outFilterSize);
  1096. (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
  1097. }
  1098. av_free(filter);
  1099. }
  1100. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  1101. static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
  1102. {
  1103. uint8_t *fragmentA;
  1104. long imm8OfPShufW1A;
  1105. long imm8OfPShufW2A;
  1106. long fragmentLengthA;
  1107. uint8_t *fragmentB;
  1108. long imm8OfPShufW1B;
  1109. long imm8OfPShufW2B;
  1110. long fragmentLengthB;
  1111. int fragmentPos;
  1112. int xpos, i;
  1113. // create an optimized horizontal scaling routine
  1114. //code fragment
  1115. asm volatile(
  1116. "jmp 9f \n\t"
  1117. // Begin
  1118. "0: \n\t"
  1119. "movq (%%"REG_d", %%"REG_a"), %%mm3\n\t"
  1120. "movd (%%"REG_c", %%"REG_S"), %%mm0\n\t"
  1121. "movd 1(%%"REG_c", %%"REG_S"), %%mm1\n\t"
  1122. "punpcklbw %%mm7, %%mm1 \n\t"
  1123. "punpcklbw %%mm7, %%mm0 \n\t"
  1124. "pshufw $0xFF, %%mm1, %%mm1 \n\t"
  1125. "1: \n\t"
  1126. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  1127. "2: \n\t"
  1128. "psubw %%mm1, %%mm0 \n\t"
  1129. "movl 8(%%"REG_b", %%"REG_a"), %%esi\n\t"
  1130. "pmullw %%mm3, %%mm0 \n\t"
  1131. "psllw $7, %%mm1 \n\t"
  1132. "paddw %%mm1, %%mm0 \n\t"
  1133. "movq %%mm0, (%%"REG_D", %%"REG_a")\n\t"
  1134. "add $8, %%"REG_a" \n\t"
  1135. // End
  1136. "9: \n\t"
  1137. // "int $3\n\t"
  1138. "lea 0b, %0 \n\t"
  1139. "lea 1b, %1 \n\t"
  1140. "lea 2b, %2 \n\t"
  1141. "dec %1 \n\t"
  1142. "dec %2 \n\t"
  1143. "sub %0, %1 \n\t"
  1144. "sub %0, %2 \n\t"
  1145. "lea 9b, %3 \n\t"
  1146. "sub %0, %3 \n\t"
  1147. :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
  1148. "=r" (fragmentLengthA)
  1149. );
  1150. asm volatile(
  1151. "jmp 9f \n\t"
  1152. // Begin
  1153. "0: \n\t"
  1154. "movq (%%"REG_d", %%"REG_a"), %%mm3\n\t"
  1155. "movd (%%"REG_c", %%"REG_S"), %%mm0\n\t"
  1156. "punpcklbw %%mm7, %%mm0 \n\t"
  1157. "pshufw $0xFF, %%mm0, %%mm1 \n\t"
  1158. "1: \n\t"
  1159. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  1160. "2: \n\t"
  1161. "psubw %%mm1, %%mm0 \n\t"
  1162. "movl 8(%%"REG_b", %%"REG_a"), %%esi\n\t"
  1163. "pmullw %%mm3, %%mm0 \n\t"
  1164. "psllw $7, %%mm1 \n\t"
  1165. "paddw %%mm1, %%mm0 \n\t"
  1166. "movq %%mm0, (%%"REG_D", %%"REG_a")\n\t"
  1167. "add $8, %%"REG_a" \n\t"
  1168. // End
  1169. "9: \n\t"
  1170. // "int $3\n\t"
  1171. "lea 0b, %0 \n\t"
  1172. "lea 1b, %1 \n\t"
  1173. "lea 2b, %2 \n\t"
  1174. "dec %1 \n\t"
  1175. "dec %2 \n\t"
  1176. "sub %0, %1 \n\t"
  1177. "sub %0, %2 \n\t"
  1178. "lea 9b, %3 \n\t"
  1179. "sub %0, %3 \n\t"
  1180. :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
  1181. "=r" (fragmentLengthB)
  1182. );
  1183. xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
  1184. fragmentPos=0;
  1185. for(i=0; i<dstW/numSplits; i++)
  1186. {
  1187. int xx=xpos>>16;
  1188. if((i&3) == 0)
  1189. {
  1190. int a=0;
  1191. int b=((xpos+xInc)>>16) - xx;
  1192. int c=((xpos+xInc*2)>>16) - xx;
  1193. int d=((xpos+xInc*3)>>16) - xx;
  1194. filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
  1195. filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
  1196. filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
  1197. filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
  1198. filterPos[i/2]= xx;
  1199. if(d+1<4)
  1200. {
  1201. int maxShift= 3-(d+1);
  1202. int shift=0;
  1203. memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
  1204. funnyCode[fragmentPos + imm8OfPShufW1B]=
  1205. (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
  1206. funnyCode[fragmentPos + imm8OfPShufW2B]=
  1207. a | (b<<2) | (c<<4) | (d<<6);
  1208. if(i+3>=dstW) shift=maxShift; //avoid overread
  1209. else if((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
  1210. if(shift && i>=shift)
  1211. {
  1212. funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
  1213. funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
  1214. filterPos[i/2]-=shift;
  1215. }
  1216. fragmentPos+= fragmentLengthB;
  1217. }
  1218. else
  1219. {
  1220. int maxShift= 3-d;
  1221. int shift=0;
  1222. memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
  1223. funnyCode[fragmentPos + imm8OfPShufW1A]=
  1224. funnyCode[fragmentPos + imm8OfPShufW2A]=
  1225. a | (b<<2) | (c<<4) | (d<<6);
  1226. if(i+4>=dstW) shift=maxShift; //avoid overread
  1227. else if((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //partial align
  1228. if(shift && i>=shift)
  1229. {
  1230. funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
  1231. funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
  1232. filterPos[i/2]-=shift;
  1233. }
  1234. fragmentPos+= fragmentLengthA;
  1235. }
  1236. funnyCode[fragmentPos]= RET;
  1237. }
  1238. xpos+=xInc;
  1239. }
  1240. filterPos[i/2]= xpos>>16; // needed to jump to the next part
  1241. }
  1242. #endif // ARCH_X86 || ARCH_X86_64
  1243. static void globalInit(void){
  1244. // generating tables:
  1245. int i;
  1246. for(i=0; i<768; i++){
  1247. int c= MIN(MAX(i-256, 0), 255);
  1248. clip_table[i]=c;
  1249. }
  1250. }
  1251. static SwsFunc getSwsFunc(int flags){
  1252. #ifdef RUNTIME_CPUDETECT
  1253. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  1254. // ordered per speed fasterst first
  1255. if(flags & SWS_CPU_CAPS_MMX2)
  1256. return swScale_MMX2;
  1257. else if(flags & SWS_CPU_CAPS_3DNOW)
  1258. return swScale_3DNow;
  1259. else if(flags & SWS_CPU_CAPS_MMX)
  1260. return swScale_MMX;
  1261. else
  1262. return swScale_C;
  1263. #else
  1264. #ifdef ARCH_POWERPC
  1265. if(flags & SWS_CPU_CAPS_ALTIVEC)
  1266. return swScale_altivec;
  1267. else
  1268. return swScale_C;
  1269. #endif
  1270. return swScale_C;
  1271. #endif
  1272. #else //RUNTIME_CPUDETECT
  1273. #ifdef HAVE_MMX2
  1274. return swScale_MMX2;
  1275. #elif defined (HAVE_3DNOW)
  1276. return swScale_3DNow;
  1277. #elif defined (HAVE_MMX)
  1278. return swScale_MMX;
  1279. #elif defined (HAVE_ALTIVEC)
  1280. return swScale_altivec;
  1281. #else
  1282. return swScale_C;
  1283. #endif
  1284. #endif //!RUNTIME_CPUDETECT
  1285. }
  1286. static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1287. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1288. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1289. /* Copy Y plane */
  1290. if(dstStride[0]==srcStride[0] && srcStride[0] > 0)
  1291. memcpy(dst, src[0], srcSliceH*dstStride[0]);
  1292. else
  1293. {
  1294. int i;
  1295. uint8_t *srcPtr= src[0];
  1296. uint8_t *dstPtr= dst;
  1297. for(i=0; i<srcSliceH; i++)
  1298. {
  1299. memcpy(dstPtr, srcPtr, c->srcW);
  1300. srcPtr+= srcStride[0];
  1301. dstPtr+= dstStride[0];
  1302. }
  1303. }
  1304. dst = dstParam[1] + dstStride[1]*srcSliceY/2;
  1305. if (c->dstFormat == IMGFMT_NV12)
  1306. interleaveBytes( src[1],src[2],dst,c->srcW/2,srcSliceH/2,srcStride[1],srcStride[2],dstStride[0] );
  1307. else
  1308. interleaveBytes( src[2],src[1],dst,c->srcW/2,srcSliceH/2,srcStride[2],srcStride[1],dstStride[0] );
  1309. return srcSliceH;
  1310. }
  1311. static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1312. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1313. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1314. yv12toyuy2( src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
  1315. return srcSliceH;
  1316. }
  1317. static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1318. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1319. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1320. yv12touyvy( src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
  1321. return srcSliceH;
  1322. }
  1323. /* {RGB,BGR}{15,16,24,32} -> {RGB,BGR}{15,16,24,32} */
  1324. static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1325. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1326. const int srcFormat= c->srcFormat;
  1327. const int dstFormat= c->dstFormat;
  1328. const int srcBpp= ((srcFormat&0xFF) + 7)>>3;
  1329. const int dstBpp= ((dstFormat&0xFF) + 7)>>3;
  1330. const int srcId= (srcFormat&0xFF)>>2; // 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8
  1331. const int dstId= (dstFormat&0xFF)>>2;
  1332. void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
  1333. /* BGR -> BGR */
  1334. if( (isBGR(srcFormat) && isBGR(dstFormat))
  1335. || (isRGB(srcFormat) && isRGB(dstFormat))){
  1336. switch(srcId | (dstId<<4)){
  1337. case 0x34: conv= rgb16to15; break;
  1338. case 0x36: conv= rgb24to15; break;
  1339. case 0x38: conv= rgb32to15; break;
  1340. case 0x43: conv= rgb15to16; break;
  1341. case 0x46: conv= rgb24to16; break;
  1342. case 0x48: conv= rgb32to16; break;
  1343. case 0x63: conv= rgb15to24; break;
  1344. case 0x64: conv= rgb16to24; break;
  1345. case 0x68: conv= rgb32to24; break;
  1346. case 0x83: conv= rgb15to32; break;
  1347. case 0x84: conv= rgb16to32; break;
  1348. case 0x86: conv= rgb24to32; break;
  1349. default: MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1350. sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
  1351. }
  1352. }else if( (isBGR(srcFormat) && isRGB(dstFormat))
  1353. || (isRGB(srcFormat) && isBGR(dstFormat))){
  1354. switch(srcId | (dstId<<4)){
  1355. case 0x33: conv= rgb15tobgr15; break;
  1356. case 0x34: conv= rgb16tobgr15; break;
  1357. case 0x36: conv= rgb24tobgr15; break;
  1358. case 0x38: conv= rgb32tobgr15; break;
  1359. case 0x43: conv= rgb15tobgr16; break;
  1360. case 0x44: conv= rgb16tobgr16; break;
  1361. case 0x46: conv= rgb24tobgr16; break;
  1362. case 0x48: conv= rgb32tobgr16; break;
  1363. case 0x63: conv= rgb15tobgr24; break;
  1364. case 0x64: conv= rgb16tobgr24; break;
  1365. case 0x66: conv= rgb24tobgr24; break;
  1366. case 0x68: conv= rgb32tobgr24; break;
  1367. case 0x83: conv= rgb15tobgr32; break;
  1368. case 0x84: conv= rgb16tobgr32; break;
  1369. case 0x86: conv= rgb24tobgr32; break;
  1370. case 0x88: conv= rgb32tobgr32; break;
  1371. default: MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1372. sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
  1373. }
  1374. }else{
  1375. MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1376. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1377. }
  1378. if(dstStride[0]*srcBpp == srcStride[0]*dstBpp)
  1379. conv(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
  1380. else
  1381. {
  1382. int i;
  1383. uint8_t *srcPtr= src[0];
  1384. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1385. for(i=0; i<srcSliceH; i++)
  1386. {
  1387. conv(srcPtr, dstPtr, c->srcW*srcBpp);
  1388. srcPtr+= srcStride[0];
  1389. dstPtr+= dstStride[0];
  1390. }
  1391. }
  1392. return srcSliceH;
  1393. }
  1394. static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1395. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1396. rgb24toyv12(
  1397. src[0],
  1398. dst[0]+ srcSliceY *dstStride[0],
  1399. dst[1]+(srcSliceY>>1)*dstStride[1],
  1400. dst[2]+(srcSliceY>>1)*dstStride[2],
  1401. c->srcW, srcSliceH,
  1402. dstStride[0], dstStride[1], srcStride[0]);
  1403. return srcSliceH;
  1404. }
  1405. static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1406. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1407. int i;
  1408. /* copy Y */
  1409. if(srcStride[0]==dstStride[0] && srcStride[0] > 0)
  1410. memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
  1411. else{
  1412. uint8_t *srcPtr= src[0];
  1413. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1414. for(i=0; i<srcSliceH; i++)
  1415. {
  1416. memcpy(dstPtr, srcPtr, c->srcW);
  1417. srcPtr+= srcStride[0];
  1418. dstPtr+= dstStride[0];
  1419. }
  1420. }
  1421. if(c->dstFormat==IMGFMT_YV12){
  1422. planar2x(src[1], dst[1], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[1]);
  1423. planar2x(src[2], dst[2], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[2]);
  1424. }else{
  1425. planar2x(src[1], dst[2], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[2]);
  1426. planar2x(src[2], dst[1], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[1]);
  1427. }
  1428. return srcSliceH;
  1429. }
  1430. /**
  1431. * bring pointers in YUV order instead of YVU
  1432. */
  1433. static inline void sws_orderYUV(int format, uint8_t * sortedP[], int sortedStride[], uint8_t * p[], int stride[]){
  1434. if(format == IMGFMT_YV12 || format == IMGFMT_YVU9
  1435. || format == IMGFMT_444P || format == IMGFMT_422P || format == IMGFMT_411P){
  1436. sortedP[0]= p[0];
  1437. sortedP[1]= p[2];
  1438. sortedP[2]= p[1];
  1439. sortedStride[0]= stride[0];
  1440. sortedStride[1]= stride[2];
  1441. sortedStride[2]= stride[1];
  1442. }
  1443. else if(isPacked(format) || isGray(format) || format == IMGFMT_Y8)
  1444. {
  1445. sortedP[0]= p[0];
  1446. sortedP[1]=
  1447. sortedP[2]= NULL;
  1448. sortedStride[0]= stride[0];
  1449. sortedStride[1]=
  1450. sortedStride[2]= 0;
  1451. }
  1452. else if(format == IMGFMT_I420 || format == IMGFMT_IYUV)
  1453. {
  1454. sortedP[0]= p[0];
  1455. sortedP[1]= p[1];
  1456. sortedP[2]= p[2];
  1457. sortedStride[0]= stride[0];
  1458. sortedStride[1]= stride[1];
  1459. sortedStride[2]= stride[2];
  1460. }
  1461. else if(format == IMGFMT_NV12 || format == IMGFMT_NV21)
  1462. {
  1463. sortedP[0]= p[0];
  1464. sortedP[1]= p[1];
  1465. sortedP[2]= NULL;
  1466. sortedStride[0]= stride[0];
  1467. sortedStride[1]= stride[1];
  1468. sortedStride[2]= 0;
  1469. }else{
  1470. MSG_ERR("internal error in orderYUV\n");
  1471. }
  1472. }
  1473. /* unscaled copy like stuff (assumes nearly identical formats) */
  1474. static int simpleCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1475. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1476. if(isPacked(c->srcFormat))
  1477. {
  1478. if(dstStride[0]==srcStride[0] && srcStride[0] > 0)
  1479. memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
  1480. else
  1481. {
  1482. int i;
  1483. uint8_t *srcPtr= src[0];
  1484. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1485. int length=0;
  1486. /* universal length finder */
  1487. while(length+c->srcW <= ABS(dstStride[0])
  1488. && length+c->srcW <= ABS(srcStride[0])) length+= c->srcW;
  1489. ASSERT(length!=0);
  1490. for(i=0; i<srcSliceH; i++)
  1491. {
  1492. memcpy(dstPtr, srcPtr, length);
  1493. srcPtr+= srcStride[0];
  1494. dstPtr+= dstStride[0];
  1495. }
  1496. }
  1497. }
  1498. else
  1499. { /* Planar YUV or gray */
  1500. int plane;
  1501. for(plane=0; plane<3; plane++)
  1502. {
  1503. int length= plane==0 ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
  1504. int y= plane==0 ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
  1505. int height= plane==0 ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
  1506. if((isGray(c->srcFormat) || isGray(c->dstFormat)) && plane>0)
  1507. {
  1508. if(!isGray(c->dstFormat))
  1509. memset(dst[plane], 128, dstStride[plane]*height);
  1510. }
  1511. else
  1512. {
  1513. if(dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
  1514. memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
  1515. else
  1516. {
  1517. int i;
  1518. uint8_t *srcPtr= src[plane];
  1519. uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
  1520. for(i=0; i<height; i++)
  1521. {
  1522. memcpy(dstPtr, srcPtr, length);
  1523. srcPtr+= srcStride[plane];
  1524. dstPtr+= dstStride[plane];
  1525. }
  1526. }
  1527. }
  1528. }
  1529. }
  1530. return srcSliceH;
  1531. }
  1532. static int remove_dup_fourcc(int fourcc)
  1533. {
  1534. switch(fourcc)
  1535. {
  1536. case IMGFMT_I420:
  1537. case IMGFMT_IYUV: return IMGFMT_YV12;
  1538. case IMGFMT_Y8 : return IMGFMT_Y800;
  1539. case IMGFMT_IF09: return IMGFMT_YVU9;
  1540. default: return fourcc;
  1541. }
  1542. }
  1543. static void getSubSampleFactors(int *h, int *v, int format){
  1544. switch(format){
  1545. case IMGFMT_UYVY:
  1546. case IMGFMT_YUY2:
  1547. *h=1;
  1548. *v=0;
  1549. break;
  1550. case IMGFMT_YV12:
  1551. case IMGFMT_Y800: //FIXME remove after different subsamplings are fully implemented
  1552. case IMGFMT_NV12:
  1553. case IMGFMT_NV21:
  1554. *h=1;
  1555. *v=1;
  1556. break;
  1557. case IMGFMT_YVU9:
  1558. *h=2;
  1559. *v=2;
  1560. break;
  1561. case IMGFMT_444P:
  1562. *h=0;
  1563. *v=0;
  1564. break;
  1565. case IMGFMT_422P:
  1566. *h=1;
  1567. *v=0;
  1568. break;
  1569. case IMGFMT_411P:
  1570. *h=2;
  1571. *v=0;
  1572. break;
  1573. default:
  1574. *h=0;
  1575. *v=0;
  1576. break;
  1577. }
  1578. }
  1579. static uint16_t roundToInt16(int64_t f){
  1580. int r= (f + (1<<15))>>16;
  1581. if(r<-0x7FFF) return 0x8000;
  1582. else if(r> 0x7FFF) return 0x7FFF;
  1583. else return r;
  1584. }
  1585. /**
  1586. * @param inv_table the yuv2rgb coeffs, normally Inverse_Table_6_9[x]
  1587. * @param fullRange if 1 then the luma range is 0..255 if 0 its 16..235
  1588. * @return -1 if not supported
  1589. */
  1590. int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
  1591. int64_t crv = inv_table[0];
  1592. int64_t cbu = inv_table[1];
  1593. int64_t cgu = -inv_table[2];
  1594. int64_t cgv = -inv_table[3];
  1595. int64_t cy = 1<<16;
  1596. int64_t oy = 0;
  1597. if(isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
  1598. memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
  1599. memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
  1600. c->brightness= brightness;
  1601. c->contrast = contrast;
  1602. c->saturation= saturation;
  1603. c->srcRange = srcRange;
  1604. c->dstRange = dstRange;
  1605. c->uOffset= 0x0400040004000400LL;
  1606. c->vOffset= 0x0400040004000400LL;
  1607. if(!srcRange){
  1608. cy= (cy*255) / 219;
  1609. oy= 16<<16;
  1610. }
  1611. cy = (cy *contrast )>>16;
  1612. crv= (crv*contrast * saturation)>>32;
  1613. cbu= (cbu*contrast * saturation)>>32;
  1614. cgu= (cgu*contrast * saturation)>>32;
  1615. cgv= (cgv*contrast * saturation)>>32;
  1616. oy -= 256*brightness;
  1617. c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
  1618. c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
  1619. c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
  1620. c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
  1621. c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
  1622. c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
  1623. yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
  1624. //FIXME factorize
  1625. #ifdef COMPILE_ALTIVEC
  1626. if (c->flags & SWS_CPU_CAPS_ALTIVEC)
  1627. yuv2rgb_altivec_init_tables (c, inv_table, brightness, contrast, saturation);
  1628. #endif
  1629. return 0;
  1630. }
  1631. /**
  1632. * @return -1 if not supported
  1633. */
  1634. int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
  1635. if(isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
  1636. *inv_table = c->srcColorspaceTable;
  1637. *table = c->dstColorspaceTable;
  1638. *srcRange = c->srcRange;
  1639. *dstRange = c->dstRange;
  1640. *brightness= c->brightness;
  1641. *contrast = c->contrast;
  1642. *saturation= c->saturation;
  1643. return 0;
  1644. }
  1645. SwsContext *sws_getContext(int srcW, int srcH, int origSrcFormat, int dstW, int dstH, int origDstFormat, int flags,
  1646. SwsFilter *srcFilter, SwsFilter *dstFilter, double *param){
  1647. SwsContext *c;
  1648. int i;
  1649. int usesVFilter, usesHFilter;
  1650. int unscaled, needsDither;
  1651. int srcFormat, dstFormat;
  1652. SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
  1653. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  1654. if(flags & SWS_CPU_CAPS_MMX)
  1655. asm volatile("emms\n\t"::: "memory");
  1656. #endif
  1657. #ifndef RUNTIME_CPUDETECT //ensure that the flags match the compiled variant if cpudetect is off
  1658. flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC);
  1659. #ifdef HAVE_MMX2
  1660. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
  1661. #elif defined (HAVE_3DNOW)
  1662. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
  1663. #elif defined (HAVE_MMX)
  1664. flags |= SWS_CPU_CAPS_MMX;
  1665. #elif defined (HAVE_ALTIVEC)
  1666. flags |= SWS_CPU_CAPS_ALTIVEC;
  1667. #endif
  1668. #endif
  1669. if(clip_table[512] != 255) globalInit();
  1670. if(rgb15to16 == NULL) sws_rgb2rgb_init(flags);
  1671. /* avoid duplicate Formats, so we don't need to check to much */
  1672. srcFormat = remove_dup_fourcc(origSrcFormat);
  1673. dstFormat = remove_dup_fourcc(origDstFormat);
  1674. unscaled = (srcW == dstW && srcH == dstH);
  1675. needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
  1676. && (dstFormat&0xFF)<24
  1677. && ((dstFormat&0xFF)<(srcFormat&0xFF) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
  1678. if(!isSupportedIn(srcFormat))
  1679. {
  1680. MSG_ERR("swScaler: %s is not supported as input format\n", sws_format_name(srcFormat));
  1681. return NULL;
  1682. }
  1683. if(!isSupportedOut(dstFormat))
  1684. {
  1685. MSG_ERR("swScaler: %s is not supported as output format\n", sws_format_name(dstFormat));
  1686. return NULL;
  1687. }
  1688. /* sanity check */
  1689. 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
  1690. {
  1691. MSG_ERR("swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
  1692. srcW, srcH, dstW, dstH);
  1693. return NULL;
  1694. }
  1695. if(!dstFilter) dstFilter= &dummyFilter;
  1696. if(!srcFilter) srcFilter= &dummyFilter;
  1697. c= av_malloc(sizeof(SwsContext));
  1698. memset(c, 0, sizeof(SwsContext));
  1699. c->srcW= srcW;
  1700. c->srcH= srcH;
  1701. c->dstW= dstW;
  1702. c->dstH= dstH;
  1703. c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
  1704. c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
  1705. c->flags= flags;
  1706. c->dstFormat= dstFormat;
  1707. c->srcFormat= srcFormat;
  1708. c->origDstFormat= origDstFormat;
  1709. c->origSrcFormat= origSrcFormat;
  1710. c->vRounder= 4* 0x0001000100010001ULL;
  1711. usesHFilter= usesVFilter= 0;
  1712. if(dstFilter->lumV!=NULL && dstFilter->lumV->length>1) usesVFilter=1;
  1713. if(dstFilter->lumH!=NULL && dstFilter->lumH->length>1) usesHFilter=1;
  1714. if(dstFilter->chrV!=NULL && dstFilter->chrV->length>1) usesVFilter=1;
  1715. if(dstFilter->chrH!=NULL && dstFilter->chrH->length>1) usesHFilter=1;
  1716. if(srcFilter->lumV!=NULL && srcFilter->lumV->length>1) usesVFilter=1;
  1717. if(srcFilter->lumH!=NULL && srcFilter->lumH->length>1) usesHFilter=1;
  1718. if(srcFilter->chrV!=NULL && srcFilter->chrV->length>1) usesVFilter=1;
  1719. if(srcFilter->chrH!=NULL && srcFilter->chrH->length>1) usesHFilter=1;
  1720. getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
  1721. getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
  1722. // reuse chroma for 2 pixles rgb/bgr unless user wants full chroma interpolation
  1723. if((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
  1724. // drop some chroma lines if the user wants it
  1725. c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
  1726. c->chrSrcVSubSample+= c->vChrDrop;
  1727. // drop every 2. pixel for chroma calculation unless user wants full chroma
  1728. if((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP))
  1729. c->chrSrcHSubSample=1;
  1730. if(param){
  1731. c->param[0] = param[0];
  1732. c->param[1] = param[1];
  1733. }else{
  1734. c->param[0] =
  1735. c->param[1] = SWS_PARAM_DEFAULT;
  1736. }
  1737. c->chrIntHSubSample= c->chrDstHSubSample;
  1738. c->chrIntVSubSample= c->chrSrcVSubSample;
  1739. // note the -((-x)>>y) is so that we allways round toward +inf
  1740. c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
  1741. c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
  1742. c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
  1743. c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
  1744. sws_setColorspaceDetails(c, Inverse_Table_6_9[SWS_CS_DEFAULT], 0, Inverse_Table_6_9[SWS_CS_DEFAULT] /* FIXME*/, 0, 0, 1<<16, 1<<16);
  1745. /* unscaled special Cases */
  1746. if(unscaled && !usesHFilter && !usesVFilter)
  1747. {
  1748. /* yv12_to_nv12 */
  1749. if(srcFormat == IMGFMT_YV12 && (dstFormat == IMGFMT_NV12 || dstFormat == IMGFMT_NV21))
  1750. {
  1751. c->swScale= PlanarToNV12Wrapper;
  1752. }
  1753. /* yuv2bgr */
  1754. if((srcFormat==IMGFMT_YV12 || srcFormat==IMGFMT_422P) && (isBGR(dstFormat) || isRGB(dstFormat)))
  1755. {
  1756. c->swScale= yuv2rgb_get_func_ptr(c);
  1757. }
  1758. if( srcFormat==IMGFMT_YVU9 && dstFormat==IMGFMT_YV12 )
  1759. {
  1760. c->swScale= yvu9toyv12Wrapper;
  1761. }
  1762. /* bgr24toYV12 */
  1763. if(srcFormat==IMGFMT_BGR24 && dstFormat==IMGFMT_YV12)
  1764. c->swScale= bgr24toyv12Wrapper;
  1765. /* rgb/bgr -> rgb/bgr (no dither needed forms) */
  1766. if( (isBGR(srcFormat) || isRGB(srcFormat))
  1767. && (isBGR(dstFormat) || isRGB(dstFormat))
  1768. && !needsDither)
  1769. c->swScale= rgb2rgbWrapper;
  1770. /* LQ converters if -sws 0 or -sws 4*/
  1771. if(c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
  1772. /* rgb/bgr -> rgb/bgr (dither needed forms) */
  1773. if( (isBGR(srcFormat) || isRGB(srcFormat))
  1774. && (isBGR(dstFormat) || isRGB(dstFormat))
  1775. && needsDither)
  1776. c->swScale= rgb2rgbWrapper;
  1777. /* yv12_to_yuy2 */
  1778. if(srcFormat == IMGFMT_YV12 &&
  1779. (dstFormat == IMGFMT_YUY2 || dstFormat == IMGFMT_UYVY))
  1780. {
  1781. if (dstFormat == IMGFMT_YUY2)
  1782. c->swScale= PlanarToYuy2Wrapper;
  1783. else
  1784. c->swScale= PlanarToUyvyWrapper;
  1785. }
  1786. }
  1787. #ifdef COMPILE_ALTIVEC
  1788. if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
  1789. ((srcFormat == IMGFMT_YV12 &&
  1790. (dstFormat == IMGFMT_YUY2 || dstFormat == IMGFMT_UYVY)))) {
  1791. // unscaled YV12 -> packed YUV, we want speed
  1792. if (dstFormat == IMGFMT_YUY2)
  1793. c->swScale= yv12toyuy2_unscaled_altivec;
  1794. else
  1795. c->swScale= yv12touyvy_unscaled_altivec;
  1796. }
  1797. #endif
  1798. /* simple copy */
  1799. if( srcFormat == dstFormat
  1800. || (isPlanarYUV(srcFormat) && isGray(dstFormat))
  1801. || (isPlanarYUV(dstFormat) && isGray(srcFormat))
  1802. )
  1803. {
  1804. c->swScale= simpleCopy;
  1805. }
  1806. if(c->swScale){
  1807. if(flags&SWS_PRINT_INFO)
  1808. MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
  1809. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1810. return c;
  1811. }
  1812. }
  1813. if(flags & SWS_CPU_CAPS_MMX2)
  1814. {
  1815. c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
  1816. if(!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
  1817. {
  1818. if(flags&SWS_PRINT_INFO)
  1819. MSG_INFO("SwScaler: output Width is not a multiple of 32 -> no MMX2 scaler\n");
  1820. }
  1821. if(usesHFilter) c->canMMX2BeUsed=0;
  1822. }
  1823. else
  1824. c->canMMX2BeUsed=0;
  1825. c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
  1826. c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
  1827. // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
  1828. // but only for the FAST_BILINEAR mode otherwise do correct scaling
  1829. // n-2 is the last chrominance sample available
  1830. // this is not perfect, but noone shuld notice the difference, the more correct variant
  1831. // would be like the vertical one, but that would require some special code for the
  1832. // first and last pixel
  1833. if(flags&SWS_FAST_BILINEAR)
  1834. {
  1835. if(c->canMMX2BeUsed)
  1836. {
  1837. c->lumXInc+= 20;
  1838. c->chrXInc+= 20;
  1839. }
  1840. //we don't use the x86asm scaler if mmx is available
  1841. else if(flags & SWS_CPU_CAPS_MMX)
  1842. {
  1843. c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
  1844. c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
  1845. }
  1846. }
  1847. /* precalculate horizontal scaler filter coefficients */
  1848. {
  1849. const int filterAlign=
  1850. (flags & SWS_CPU_CAPS_MMX) ? 4 :
  1851. (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
  1852. 1;
  1853. initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
  1854. srcW , dstW, filterAlign, 1<<14,
  1855. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
  1856. srcFilter->lumH, dstFilter->lumH, c->param);
  1857. initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
  1858. c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
  1859. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
  1860. srcFilter->chrH, dstFilter->chrH, c->param);
  1861. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  1862. // can't downscale !!!
  1863. if(c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
  1864. {
  1865. #define MAX_FUNNY_CODE_SIZE 10000
  1866. #ifdef MAP_ANONYMOUS
  1867. c->funnyYCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  1868. c->funnyUVCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  1869. #else
  1870. c->funnyYCode = av_malloc(MAX_FUNNY_CODE_SIZE);
  1871. c->funnyUVCode = av_malloc(MAX_FUNNY_CODE_SIZE);
  1872. #endif
  1873. c->lumMmx2Filter = av_malloc((dstW /8+8)*sizeof(int16_t));
  1874. c->chrMmx2Filter = av_malloc((c->chrDstW /4+8)*sizeof(int16_t));
  1875. c->lumMmx2FilterPos= av_malloc((dstW /2/8+8)*sizeof(int32_t));
  1876. c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
  1877. initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
  1878. initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
  1879. }
  1880. #endif
  1881. } // Init Horizontal stuff
  1882. /* precalculate vertical scaler filter coefficients */
  1883. {
  1884. const int filterAlign=
  1885. (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
  1886. 1;
  1887. initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
  1888. srcH , dstH, filterAlign, (1<<12)-4,
  1889. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
  1890. srcFilter->lumV, dstFilter->lumV, c->param);
  1891. initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
  1892. c->chrSrcH, c->chrDstH, filterAlign, (1<<12)-4,
  1893. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
  1894. srcFilter->chrV, dstFilter->chrV, c->param);
  1895. #ifdef HAVE_ALTIVEC
  1896. c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
  1897. c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
  1898. for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
  1899. int j;
  1900. short *p = (short *)&c->vYCoeffsBank[i];
  1901. for (j=0;j<8;j++)
  1902. p[j] = c->vLumFilter[i];
  1903. }
  1904. for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
  1905. int j;
  1906. short *p = (short *)&c->vCCoeffsBank[i];
  1907. for (j=0;j<8;j++)
  1908. p[j] = c->vChrFilter[i];
  1909. }
  1910. #endif
  1911. }
  1912. // Calculate Buffer Sizes so that they won't run out while handling these damn slices
  1913. c->vLumBufSize= c->vLumFilterSize;
  1914. c->vChrBufSize= c->vChrFilterSize;
  1915. for(i=0; i<dstH; i++)
  1916. {
  1917. int chrI= i*c->chrDstH / dstH;
  1918. int nextSlice= MAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
  1919. ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
  1920. nextSlice>>= c->chrSrcVSubSample;
  1921. nextSlice<<= c->chrSrcVSubSample;
  1922. if(c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
  1923. c->vLumBufSize= nextSlice - c->vLumFilterPos[i ];
  1924. if(c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
  1925. c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
  1926. }
  1927. // allocate pixbufs (we use dynamic allocation because otherwise we would need to
  1928. c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
  1929. c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
  1930. //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)
  1931. /* align at 16 bytes for AltiVec */
  1932. for(i=0; i<c->vLumBufSize; i++)
  1933. c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_malloc(4000);
  1934. for(i=0; i<c->vChrBufSize; i++)
  1935. c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc(8000);
  1936. //try to avoid drawing green stuff between the right end and the stride end
  1937. for(i=0; i<c->vLumBufSize; i++) memset(c->lumPixBuf[i], 0, 4000);
  1938. for(i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, 8000);
  1939. ASSERT(c->chrDstH <= dstH)
  1940. if(flags&SWS_PRINT_INFO)
  1941. {
  1942. #ifdef DITHER1XBPP
  1943. char *dither= " dithered";
  1944. #else
  1945. char *dither= "";
  1946. #endif
  1947. if(flags&SWS_FAST_BILINEAR)
  1948. MSG_INFO("\nSwScaler: FAST_BILINEAR scaler, ");
  1949. else if(flags&SWS_BILINEAR)
  1950. MSG_INFO("\nSwScaler: BILINEAR scaler, ");
  1951. else if(flags&SWS_BICUBIC)
  1952. MSG_INFO("\nSwScaler: BICUBIC scaler, ");
  1953. else if(flags&SWS_X)
  1954. MSG_INFO("\nSwScaler: Experimental scaler, ");
  1955. else if(flags&SWS_POINT)
  1956. MSG_INFO("\nSwScaler: Nearest Neighbor / POINT scaler, ");
  1957. else if(flags&SWS_AREA)
  1958. MSG_INFO("\nSwScaler: Area Averageing scaler, ");
  1959. else if(flags&SWS_BICUBLIN)
  1960. MSG_INFO("\nSwScaler: luma BICUBIC / chroma BILINEAR scaler, ");
  1961. else if(flags&SWS_GAUSS)
  1962. MSG_INFO("\nSwScaler: Gaussian scaler, ");
  1963. else if(flags&SWS_SINC)
  1964. MSG_INFO("\nSwScaler: Sinc scaler, ");
  1965. else if(flags&SWS_LANCZOS)
  1966. MSG_INFO("\nSwScaler: Lanczos scaler, ");
  1967. else if(flags&SWS_SPLINE)
  1968. MSG_INFO("\nSwScaler: Bicubic spline scaler, ");
  1969. else
  1970. MSG_INFO("\nSwScaler: ehh flags invalid?! ");
  1971. if(dstFormat==IMGFMT_BGR15 || dstFormat==IMGFMT_BGR16)
  1972. MSG_INFO("from %s to%s %s ",
  1973. sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
  1974. else
  1975. MSG_INFO("from %s to %s ",
  1976. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1977. if(flags & SWS_CPU_CAPS_MMX2)
  1978. MSG_INFO("using MMX2\n");
  1979. else if(flags & SWS_CPU_CAPS_3DNOW)
  1980. MSG_INFO("using 3DNOW\n");
  1981. else if(flags & SWS_CPU_CAPS_MMX)
  1982. MSG_INFO("using MMX\n");
  1983. else if(flags & SWS_CPU_CAPS_ALTIVEC)
  1984. MSG_INFO("using AltiVec\n");
  1985. else
  1986. MSG_INFO("using C\n");
  1987. }
  1988. if(flags & SWS_PRINT_INFO)
  1989. {
  1990. if(flags & SWS_CPU_CAPS_MMX)
  1991. {
  1992. if(c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
  1993. MSG_V("SwScaler: using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
  1994. else
  1995. {
  1996. if(c->hLumFilterSize==4)
  1997. MSG_V("SwScaler: using 4-tap MMX scaler for horizontal luminance scaling\n");
  1998. else if(c->hLumFilterSize==8)
  1999. MSG_V("SwScaler: using 8-tap MMX scaler for horizontal luminance scaling\n");
  2000. else
  2001. MSG_V("SwScaler: using n-tap MMX scaler for horizontal luminance scaling\n");
  2002. if(c->hChrFilterSize==4)
  2003. MSG_V("SwScaler: using 4-tap MMX scaler for horizontal chrominance scaling\n");
  2004. else if(c->hChrFilterSize==8)
  2005. MSG_V("SwScaler: using 8-tap MMX scaler for horizontal chrominance scaling\n");
  2006. else
  2007. MSG_V("SwScaler: using n-tap MMX scaler for horizontal chrominance scaling\n");
  2008. }
  2009. }
  2010. else
  2011. {
  2012. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  2013. MSG_V("SwScaler: using X86-Asm scaler for horizontal scaling\n");
  2014. #else
  2015. if(flags & SWS_FAST_BILINEAR)
  2016. MSG_V("SwScaler: using FAST_BILINEAR C scaler for horizontal scaling\n");
  2017. else
  2018. MSG_V("SwScaler: using C scaler for horizontal scaling\n");
  2019. #endif
  2020. }
  2021. if(isPlanarYUV(dstFormat))
  2022. {
  2023. if(c->vLumFilterSize==1)
  2024. MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2025. else
  2026. MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2027. }
  2028. else
  2029. {
  2030. if(c->vLumFilterSize==1 && c->vChrFilterSize==2)
  2031. MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
  2032. "SwScaler: 2-tap scaler for vertical chrominance scaling (BGR)\n",(flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2033. else if(c->vLumFilterSize==2 && c->vChrFilterSize==2)
  2034. MSG_V("SwScaler: using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2035. else
  2036. MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2037. }
  2038. if(dstFormat==IMGFMT_BGR24)
  2039. MSG_V("SwScaler: using %s YV12->BGR24 Converter\n",
  2040. (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
  2041. else if(dstFormat==IMGFMT_BGR32)
  2042. MSG_V("SwScaler: using %s YV12->BGR32 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2043. else if(dstFormat==IMGFMT_BGR16)
  2044. MSG_V("SwScaler: using %s YV12->BGR16 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2045. else if(dstFormat==IMGFMT_BGR15)
  2046. MSG_V("SwScaler: using %s YV12->BGR15 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2047. MSG_V("SwScaler: %dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
  2048. }
  2049. if(flags & SWS_PRINT_INFO)
  2050. {
  2051. MSG_DBG2("SwScaler:Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  2052. c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
  2053. MSG_DBG2("SwScaler:Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  2054. c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
  2055. }
  2056. c->swScale= getSwsFunc(flags);
  2057. return c;
  2058. }
  2059. /**
  2060. * swscale warper, so we don't need to export the SwsContext.
  2061. * assumes planar YUV to be in YUV order instead of YVU
  2062. */
  2063. int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  2064. int srcSliceH, uint8_t* dst[], int dstStride[]){
  2065. if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
  2066. MSG_ERR("swScaler: slices start in the middle!\n");
  2067. return 0;
  2068. }
  2069. if (c->sliceDir == 0) {
  2070. if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
  2071. }
  2072. // copy strides, so they can safely be modified
  2073. if (c->sliceDir == 1) {
  2074. // slices go from top to bottom
  2075. int srcStride2[3]= {srcStride[0], srcStride[1], srcStride[2]};
  2076. int dstStride2[3]= {dstStride[0], dstStride[1], dstStride[2]};
  2077. return c->swScale(c, src, srcStride2, srcSliceY, srcSliceH, dst, dstStride2);
  2078. } else {
  2079. // slices go from bottom to top => we flip the image internally
  2080. uint8_t* src2[3]= {src[0] + (srcSliceH-1)*srcStride[0],
  2081. src[1] + ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1],
  2082. src[2] + ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2]
  2083. };
  2084. uint8_t* dst2[3]= {dst[0] + (c->dstH-1)*dstStride[0],
  2085. dst[1] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1],
  2086. dst[2] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2]};
  2087. int srcStride2[3]= {-srcStride[0], -srcStride[1], -srcStride[2]};
  2088. int dstStride2[3]= {-dstStride[0], -dstStride[1], -dstStride[2]};
  2089. return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
  2090. }
  2091. }
  2092. /**
  2093. * swscale warper, so we don't need to export the SwsContext
  2094. */
  2095. int sws_scale(SwsContext *c, uint8_t* srcParam[], int srcStrideParam[], int srcSliceY,
  2096. int srcSliceH, uint8_t* dstParam[], int dstStrideParam[]){
  2097. int srcStride[3];
  2098. int dstStride[3];
  2099. uint8_t *src[3];
  2100. uint8_t *dst[3];
  2101. sws_orderYUV(c->origSrcFormat, src, srcStride, srcParam, srcStrideParam);
  2102. sws_orderYUV(c->origDstFormat, dst, dstStride, dstParam, dstStrideParam);
  2103. //printf("sws: slice %d %d\n", srcSliceY, srcSliceH);
  2104. return c->swScale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
  2105. }
  2106. SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
  2107. float lumaSharpen, float chromaSharpen,
  2108. float chromaHShift, float chromaVShift,
  2109. int verbose)
  2110. {
  2111. SwsFilter *filter= av_malloc(sizeof(SwsFilter));
  2112. if(lumaGBlur!=0.0){
  2113. filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
  2114. filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
  2115. }else{
  2116. filter->lumH= sws_getIdentityVec();
  2117. filter->lumV= sws_getIdentityVec();
  2118. }
  2119. if(chromaGBlur!=0.0){
  2120. filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
  2121. filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
  2122. }else{
  2123. filter->chrH= sws_getIdentityVec();
  2124. filter->chrV= sws_getIdentityVec();
  2125. }
  2126. if(chromaSharpen!=0.0){
  2127. SwsVector *id= sws_getIdentityVec();
  2128. sws_scaleVec(filter->chrH, -chromaSharpen);
  2129. sws_scaleVec(filter->chrV, -chromaSharpen);
  2130. sws_addVec(filter->chrH, id);
  2131. sws_addVec(filter->chrV, id);
  2132. sws_freeVec(id);
  2133. }
  2134. if(lumaSharpen!=0.0){
  2135. SwsVector *id= sws_getIdentityVec();
  2136. sws_scaleVec(filter->lumH, -lumaSharpen);
  2137. sws_scaleVec(filter->lumV, -lumaSharpen);
  2138. sws_addVec(filter->lumH, id);
  2139. sws_addVec(filter->lumV, id);
  2140. sws_freeVec(id);
  2141. }
  2142. if(chromaHShift != 0.0)
  2143. sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
  2144. if(chromaVShift != 0.0)
  2145. sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
  2146. sws_normalizeVec(filter->chrH, 1.0);
  2147. sws_normalizeVec(filter->chrV, 1.0);
  2148. sws_normalizeVec(filter->lumH, 1.0);
  2149. sws_normalizeVec(filter->lumV, 1.0);
  2150. if(verbose) sws_printVec(filter->chrH);
  2151. if(verbose) sws_printVec(filter->lumH);
  2152. return filter;
  2153. }
  2154. /**
  2155. * returns a normalized gaussian curve used to filter stuff
  2156. * quality=3 is high quality, lowwer is lowwer quality
  2157. */
  2158. SwsVector *sws_getGaussianVec(double variance, double quality){
  2159. const int length= (int)(variance*quality + 0.5) | 1;
  2160. int i;
  2161. double *coeff= av_malloc(length*sizeof(double));
  2162. double middle= (length-1)*0.5;
  2163. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2164. vec->coeff= coeff;
  2165. vec->length= length;
  2166. for(i=0; i<length; i++)
  2167. {
  2168. double dist= i-middle;
  2169. coeff[i]= exp( -dist*dist/(2*variance*variance) ) / sqrt(2*variance*PI);
  2170. }
  2171. sws_normalizeVec(vec, 1.0);
  2172. return vec;
  2173. }
  2174. SwsVector *sws_getConstVec(double c, int length){
  2175. int i;
  2176. double *coeff= av_malloc(length*sizeof(double));
  2177. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2178. vec->coeff= coeff;
  2179. vec->length= length;
  2180. for(i=0; i<length; i++)
  2181. coeff[i]= c;
  2182. return vec;
  2183. }
  2184. SwsVector *sws_getIdentityVec(void){
  2185. return sws_getConstVec(1.0, 1);
  2186. }
  2187. double sws_dcVec(SwsVector *a){
  2188. int i;
  2189. double sum=0;
  2190. for(i=0; i<a->length; i++)
  2191. sum+= a->coeff[i];
  2192. return sum;
  2193. }
  2194. void sws_scaleVec(SwsVector *a, double scalar){
  2195. int i;
  2196. for(i=0; i<a->length; i++)
  2197. a->coeff[i]*= scalar;
  2198. }
  2199. void sws_normalizeVec(SwsVector *a, double height){
  2200. sws_scaleVec(a, height/sws_dcVec(a));
  2201. }
  2202. static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
  2203. int length= a->length + b->length - 1;
  2204. double *coeff= av_malloc(length*sizeof(double));
  2205. int i, j;
  2206. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2207. vec->coeff= coeff;
  2208. vec->length= length;
  2209. for(i=0; i<length; i++) coeff[i]= 0.0;
  2210. for(i=0; i<a->length; i++)
  2211. {
  2212. for(j=0; j<b->length; j++)
  2213. {
  2214. coeff[i+j]+= a->coeff[i]*b->coeff[j];
  2215. }
  2216. }
  2217. return vec;
  2218. }
  2219. static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
  2220. int length= MAX(a->length, b->length);
  2221. double *coeff= av_malloc(length*sizeof(double));
  2222. int i;
  2223. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2224. vec->coeff= coeff;
  2225. vec->length= length;
  2226. for(i=0; i<length; i++) coeff[i]= 0.0;
  2227. for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  2228. for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
  2229. return vec;
  2230. }
  2231. static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
  2232. int length= MAX(a->length, b->length);
  2233. double *coeff= av_malloc(length*sizeof(double));
  2234. int i;
  2235. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2236. vec->coeff= coeff;
  2237. vec->length= length;
  2238. for(i=0; i<length; i++) coeff[i]= 0.0;
  2239. for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  2240. for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
  2241. return vec;
  2242. }
  2243. /* shift left / or right if "shift" is negative */
  2244. static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
  2245. int length= a->length + ABS(shift)*2;
  2246. double *coeff= av_malloc(length*sizeof(double));
  2247. int i;
  2248. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2249. vec->coeff= coeff;
  2250. vec->length= length;
  2251. for(i=0; i<length; i++) coeff[i]= 0.0;
  2252. for(i=0; i<a->length; i++)
  2253. {
  2254. coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
  2255. }
  2256. return vec;
  2257. }
  2258. void sws_shiftVec(SwsVector *a, int shift){
  2259. SwsVector *shifted= sws_getShiftedVec(a, shift);
  2260. av_free(a->coeff);
  2261. a->coeff= shifted->coeff;
  2262. a->length= shifted->length;
  2263. av_free(shifted);
  2264. }
  2265. void sws_addVec(SwsVector *a, SwsVector *b){
  2266. SwsVector *sum= sws_sumVec(a, b);
  2267. av_free(a->coeff);
  2268. a->coeff= sum->coeff;
  2269. a->length= sum->length;
  2270. av_free(sum);
  2271. }
  2272. void sws_subVec(SwsVector *a, SwsVector *b){
  2273. SwsVector *diff= sws_diffVec(a, b);
  2274. av_free(a->coeff);
  2275. a->coeff= diff->coeff;
  2276. a->length= diff->length;
  2277. av_free(diff);
  2278. }
  2279. void sws_convVec(SwsVector *a, SwsVector *b){
  2280. SwsVector *conv= sws_getConvVec(a, b);
  2281. av_free(a->coeff);
  2282. a->coeff= conv->coeff;
  2283. a->length= conv->length;
  2284. av_free(conv);
  2285. }
  2286. SwsVector *sws_cloneVec(SwsVector *a){
  2287. double *coeff= av_malloc(a->length*sizeof(double));
  2288. int i;
  2289. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2290. vec->coeff= coeff;
  2291. vec->length= a->length;
  2292. for(i=0; i<a->length; i++) coeff[i]= a->coeff[i];
  2293. return vec;
  2294. }
  2295. void sws_printVec(SwsVector *a){
  2296. int i;
  2297. double max=0;
  2298. double min=0;
  2299. double range;
  2300. for(i=0; i<a->length; i++)
  2301. if(a->coeff[i]>max) max= a->coeff[i];
  2302. for(i=0; i<a->length; i++)
  2303. if(a->coeff[i]<min) min= a->coeff[i];
  2304. range= max - min;
  2305. for(i=0; i<a->length; i++)
  2306. {
  2307. int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
  2308. MSG_DBG2("%1.3f ", a->coeff[i]);
  2309. for(;x>0; x--) MSG_DBG2(" ");
  2310. MSG_DBG2("|\n");
  2311. }
  2312. }
  2313. void sws_freeVec(SwsVector *a){
  2314. if(!a) return;
  2315. av_free(a->coeff);
  2316. a->coeff=NULL;
  2317. a->length=0;
  2318. av_free(a);
  2319. }
  2320. void sws_freeFilter(SwsFilter *filter){
  2321. if(!filter) return;
  2322. if(filter->lumH) sws_freeVec(filter->lumH);
  2323. if(filter->lumV) sws_freeVec(filter->lumV);
  2324. if(filter->chrH) sws_freeVec(filter->chrH);
  2325. if(filter->chrV) sws_freeVec(filter->chrV);
  2326. av_free(filter);
  2327. }
  2328. void sws_freeContext(SwsContext *c){
  2329. int i;
  2330. if(!c) return;
  2331. if(c->lumPixBuf)
  2332. {
  2333. for(i=0; i<c->vLumBufSize; i++)
  2334. {
  2335. av_free(c->lumPixBuf[i]);
  2336. c->lumPixBuf[i]=NULL;
  2337. }
  2338. av_free(c->lumPixBuf);
  2339. c->lumPixBuf=NULL;
  2340. }
  2341. if(c->chrPixBuf)
  2342. {
  2343. for(i=0; i<c->vChrBufSize; i++)
  2344. {
  2345. av_free(c->chrPixBuf[i]);
  2346. c->chrPixBuf[i]=NULL;
  2347. }
  2348. av_free(c->chrPixBuf);
  2349. c->chrPixBuf=NULL;
  2350. }
  2351. av_free(c->vLumFilter);
  2352. c->vLumFilter = NULL;
  2353. av_free(c->vChrFilter);
  2354. c->vChrFilter = NULL;
  2355. av_free(c->hLumFilter);
  2356. c->hLumFilter = NULL;
  2357. av_free(c->hChrFilter);
  2358. c->hChrFilter = NULL;
  2359. #ifdef HAVE_ALTIVEC
  2360. av_free(c->vYCoeffsBank);
  2361. c->vYCoeffsBank = NULL;
  2362. av_free(c->vCCoeffsBank);
  2363. c->vCCoeffsBank = NULL;
  2364. #endif
  2365. av_free(c->vLumFilterPos);
  2366. c->vLumFilterPos = NULL;
  2367. av_free(c->vChrFilterPos);
  2368. c->vChrFilterPos = NULL;
  2369. av_free(c->hLumFilterPos);
  2370. c->hLumFilterPos = NULL;
  2371. av_free(c->hChrFilterPos);
  2372. c->hChrFilterPos = NULL;
  2373. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  2374. #ifdef MAP_ANONYMOUS
  2375. if(c->funnyYCode) munmap(c->funnyYCode, MAX_FUNNY_CODE_SIZE);
  2376. if(c->funnyUVCode) munmap(c->funnyUVCode, MAX_FUNNY_CODE_SIZE);
  2377. #else
  2378. av_free(c->funnyYCode);
  2379. av_free(c->funnyUVCode);
  2380. #endif
  2381. c->funnyYCode=NULL;
  2382. c->funnyUVCode=NULL;
  2383. #endif
  2384. av_free(c->lumMmx2Filter);
  2385. c->lumMmx2Filter=NULL;
  2386. av_free(c->chrMmx2Filter);
  2387. c->chrMmx2Filter=NULL;
  2388. av_free(c->lumMmx2FilterPos);
  2389. c->lumMmx2FilterPos=NULL;
  2390. av_free(c->chrMmx2FilterPos);
  2391. c->chrMmx2FilterPos=NULL;
  2392. av_free(c->yuvTable);
  2393. c->yuvTable=NULL;
  2394. av_free(c);
  2395. }