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.

2896 lines
83KB

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