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.

3063 lines
102KB

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