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.

2731 lines
77KB

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