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.

2930 lines
99KB

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