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.

2754 lines
78KB

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