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.

2186 lines
77KB

  1. /*
  2. * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /*
  21. supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR32_1, BGR24, BGR16, BGR15, RGB32, RGB32_1, RGB24, Y8/Y800, YVU9/IF09, PAL8
  22. supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
  23. {BGR,RGB}{1,4,8,15,16} support dithering
  24. unscaled special converters (YV12=I420=IYUV, Y800=Y8)
  25. YV12 -> {BGR,RGB}{1,4,8,12,15,16,24,32}
  26. x -> x
  27. YUV9 -> YV12
  28. YUV9/YV12 -> Y800
  29. Y800 -> YUV9/YV12
  30. BGR24 -> BGR32 & RGB24 -> RGB32
  31. BGR32 -> BGR24 & RGB32 -> RGB24
  32. BGR15 -> BGR16
  33. */
  34. /*
  35. tested special converters (most are tested actually, but I did not write it down ...)
  36. YV12 -> BGR12/BGR16
  37. YV12 -> YV12
  38. BGR15 -> BGR16
  39. BGR16 -> BGR16
  40. YVU9 -> YV12
  41. untested special converters
  42. YV12/I420 -> BGR15/BGR24/BGR32 (it is the yuv2rgb stuff, so it should be OK)
  43. YV12/I420 -> YV12/I420
  44. YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
  45. BGR24 -> BGR32 & RGB24 -> RGB32
  46. BGR32 -> BGR24 & RGB32 -> RGB24
  47. BGR24 -> YV12
  48. */
  49. #include <inttypes.h>
  50. #include <string.h>
  51. #include <math.h>
  52. #include <stdio.h>
  53. #include "config.h"
  54. #include <assert.h>
  55. #include "swscale.h"
  56. #include "swscale_internal.h"
  57. #include "rgb2rgb.h"
  58. #include "libavutil/intreadwrite.h"
  59. #include "libavutil/x86_cpu.h"
  60. #include "libavutil/avutil.h"
  61. #include "libavutil/mathematics.h"
  62. #include "libavutil/bswap.h"
  63. #include "libavutil/pixdesc.h"
  64. #undef MOVNTQ
  65. #undef PAVGB
  66. //#undef HAVE_MMX2
  67. //#define HAVE_AMD3DNOW
  68. //#undef HAVE_MMX
  69. //#undef ARCH_X86
  70. #define DITHER1XBPP
  71. #define isPacked(x) ( \
  72. (x)==PIX_FMT_PAL8 \
  73. || (x)==PIX_FMT_YUYV422 \
  74. || (x)==PIX_FMT_UYVY422 \
  75. || (x)==PIX_FMT_Y400A \
  76. || isAnyRGB(x) \
  77. )
  78. #define RGB2YUV_SHIFT 15
  79. #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  80. #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  81. #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  82. #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  83. #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  84. #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  85. #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  86. #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  87. #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  88. static const double rgb2yuv_table[8][9]={
  89. {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
  90. {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
  91. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  92. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  93. {0.59 , 0.11 , 0.30 , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
  94. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  95. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  96. {0.701 , 0.087 , 0.212 , -0.384, 0.5, -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
  97. };
  98. /*
  99. NOTES
  100. Special versions: fast Y 1:1 scaling (no interpolation in y direction)
  101. TODO
  102. more intelligent misalignment avoidance for the horizontal scaler
  103. write special vertical cubic upscale version
  104. optimize C code (YV12 / minmax)
  105. add support for packed pixel YUV input & output
  106. add support for Y8 output
  107. optimize BGR24 & BGR32
  108. add BGR4 output support
  109. write special BGR->BGR scaler
  110. */
  111. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
  112. { 1, 3, 1, 3, 1, 3, 1, 3, },
  113. { 2, 0, 2, 0, 2, 0, 2, 0, },
  114. };
  115. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
  116. { 6, 2, 6, 2, 6, 2, 6, 2, },
  117. { 0, 4, 0, 4, 0, 4, 0, 4, },
  118. };
  119. DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[4][8]={
  120. { 8, 4, 11, 7, 8, 4, 11, 7, },
  121. { 2, 14, 1, 13, 2, 14, 1, 13, },
  122. { 10, 6, 9, 5, 10, 6, 9, 5, },
  123. { 0, 12, 3, 15, 0, 12, 3, 15, },
  124. };
  125. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
  126. { 17, 9, 23, 15, 16, 8, 22, 14, },
  127. { 5, 29, 3, 27, 4, 28, 2, 26, },
  128. { 21, 13, 19, 11, 20, 12, 18, 10, },
  129. { 0, 24, 6, 30, 1, 25, 7, 31, },
  130. { 16, 8, 22, 14, 17, 9, 23, 15, },
  131. { 4, 28, 2, 26, 5, 29, 3, 27, },
  132. { 20, 12, 18, 10, 21, 13, 19, 11, },
  133. { 1, 25, 7, 31, 0, 24, 6, 30, },
  134. };
  135. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
  136. { 0, 55, 14, 68, 3, 58, 17, 72, },
  137. { 37, 18, 50, 32, 40, 22, 54, 35, },
  138. { 9, 64, 5, 59, 13, 67, 8, 63, },
  139. { 46, 27, 41, 23, 49, 31, 44, 26, },
  140. { 2, 57, 16, 71, 1, 56, 15, 70, },
  141. { 39, 21, 52, 34, 38, 19, 51, 33, },
  142. { 11, 66, 7, 62, 10, 65, 6, 60, },
  143. { 48, 30, 43, 25, 47, 29, 42, 24, },
  144. };
  145. #if 1
  146. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  147. {117, 62, 158, 103, 113, 58, 155, 100, },
  148. { 34, 199, 21, 186, 31, 196, 17, 182, },
  149. {144, 89, 131, 76, 141, 86, 127, 72, },
  150. { 0, 165, 41, 206, 10, 175, 52, 217, },
  151. {110, 55, 151, 96, 120, 65, 162, 107, },
  152. { 28, 193, 14, 179, 38, 203, 24, 189, },
  153. {138, 83, 124, 69, 148, 93, 134, 79, },
  154. { 7, 172, 48, 213, 3, 168, 45, 210, },
  155. };
  156. #elif 1
  157. // tries to correct a gamma of 1.5
  158. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  159. { 0, 143, 18, 200, 2, 156, 25, 215, },
  160. { 78, 28, 125, 64, 89, 36, 138, 74, },
  161. { 10, 180, 3, 161, 16, 195, 8, 175, },
  162. {109, 51, 93, 38, 121, 60, 105, 47, },
  163. { 1, 152, 23, 210, 0, 147, 20, 205, },
  164. { 85, 33, 134, 71, 81, 30, 130, 67, },
  165. { 14, 190, 6, 171, 12, 185, 5, 166, },
  166. {117, 57, 101, 44, 113, 54, 97, 41, },
  167. };
  168. #elif 1
  169. // tries to correct a gamma of 2.0
  170. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  171. { 0, 124, 8, 193, 0, 140, 12, 213, },
  172. { 55, 14, 104, 42, 66, 19, 119, 52, },
  173. { 3, 168, 1, 145, 6, 187, 3, 162, },
  174. { 86, 31, 70, 21, 99, 39, 82, 28, },
  175. { 0, 134, 11, 206, 0, 129, 9, 200, },
  176. { 62, 17, 114, 48, 58, 16, 109, 45, },
  177. { 5, 181, 2, 157, 4, 175, 1, 151, },
  178. { 95, 36, 78, 26, 90, 34, 74, 24, },
  179. };
  180. #else
  181. // tries to correct a gamma of 2.5
  182. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  183. { 0, 107, 3, 187, 0, 125, 6, 212, },
  184. { 39, 7, 86, 28, 49, 11, 102, 36, },
  185. { 1, 158, 0, 131, 3, 180, 1, 151, },
  186. { 68, 19, 52, 12, 81, 25, 64, 17, },
  187. { 0, 119, 5, 203, 0, 113, 4, 195, },
  188. { 45, 9, 96, 33, 42, 8, 91, 30, },
  189. { 2, 172, 1, 144, 2, 165, 0, 137, },
  190. { 77, 23, 60, 15, 72, 21, 56, 14, },
  191. };
  192. #endif
  193. static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  194. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  195. const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
  196. int dstW, int chrDstW, int big_endian, int output_bits)
  197. {
  198. //FIXME Optimize (just quickly written not optimized..)
  199. int i;
  200. int shift = 11 + 16 - output_bits;
  201. #define output_pixel(pos, val) \
  202. if (big_endian) { \
  203. if (output_bits == 16) { \
  204. AV_WB16(pos, av_clip_uint16(val >> shift)); \
  205. } else { \
  206. AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  207. } \
  208. } else { \
  209. if (output_bits == 16) { \
  210. AV_WL16(pos, av_clip_uint16(val >> shift)); \
  211. } else { \
  212. AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  213. } \
  214. }
  215. for (i = 0; i < dstW; i++) {
  216. int val = 1 << 10;
  217. int j;
  218. for (j = 0; j < lumFilterSize; j++)
  219. val += lumSrc[j][i] * lumFilter[j];
  220. output_pixel(&dest[i], val);
  221. }
  222. if (uDest) {
  223. for (i = 0; i < chrDstW; i++) {
  224. int u = 1 << 10;
  225. int v = 1 << 10;
  226. int j;
  227. for (j = 0; j < chrFilterSize; j++) {
  228. u += chrSrc[j][i ] * chrFilter[j];
  229. v += chrSrc[j][i + VOFW] * chrFilter[j];
  230. }
  231. output_pixel(&uDest[i], u);
  232. output_pixel(&vDest[i], v);
  233. }
  234. }
  235. if (CONFIG_SWSCALE_ALPHA && aDest) {
  236. for (i = 0; i < dstW; i++) {
  237. int val = 1 << 10;
  238. int j;
  239. for (j = 0; j < lumFilterSize; j++)
  240. val += alpSrc[j][i] * lumFilter[j];
  241. output_pixel(&aDest[i], val);
  242. }
  243. }
  244. }
  245. #define yuv2NBPS(bits, BE_LE, is_be) \
  246. static void yuv2yuvX ## bits ## BE_LE ## _c(const int16_t *lumFilter, \
  247. const int16_t **lumSrc, int lumFilterSize, \
  248. const int16_t *chrFilter, const int16_t **chrSrc, \
  249. int chrFilterSize, const int16_t **alpSrc, \
  250. uint16_t *dest, uint16_t *uDest, uint16_t *vDest, \
  251. uint16_t *aDest, int dstW, int chrDstW) \
  252. { \
  253. yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize, \
  254. chrFilter, chrSrc, chrFilterSize, \
  255. alpSrc, \
  256. dest, uDest, vDest, aDest, \
  257. dstW, chrDstW, is_be, bits); \
  258. }
  259. yuv2NBPS( 9, BE, 1);
  260. yuv2NBPS( 9, LE, 0);
  261. yuv2NBPS(10, BE, 1);
  262. yuv2NBPS(10, LE, 0);
  263. yuv2NBPS(16, BE, 1);
  264. yuv2NBPS(16, LE, 0);
  265. static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  266. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  267. const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
  268. enum PixelFormat dstFormat)
  269. {
  270. #define conv16(bits) \
  271. if (isBE(dstFormat)) { \
  272. yuv2yuvX ## bits ## BE_c(lumFilter, lumSrc, lumFilterSize, \
  273. chrFilter, chrSrc, chrFilterSize, \
  274. alpSrc, \
  275. dest, uDest, vDest, aDest, \
  276. dstW, chrDstW); \
  277. } else { \
  278. yuv2yuvX ## bits ## LE_c(lumFilter, lumSrc, lumFilterSize, \
  279. chrFilter, chrSrc, chrFilterSize, \
  280. alpSrc, \
  281. dest, uDest, vDest, aDest, \
  282. dstW, chrDstW); \
  283. }
  284. if (is16BPS(dstFormat)) {
  285. conv16(16);
  286. } else if (av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1 == 8) {
  287. conv16(9);
  288. } else {
  289. conv16(10);
  290. }
  291. #undef conv16
  292. }
  293. static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  294. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  295. const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
  296. {
  297. //FIXME Optimize (just quickly written not optimized..)
  298. int i;
  299. for (i=0; i<dstW; i++) {
  300. int val=1<<18;
  301. int j;
  302. for (j=0; j<lumFilterSize; j++)
  303. val += lumSrc[j][i] * lumFilter[j];
  304. dest[i]= av_clip_uint8(val>>19);
  305. }
  306. if (uDest)
  307. for (i=0; i<chrDstW; i++) {
  308. int u=1<<18;
  309. int v=1<<18;
  310. int j;
  311. for (j=0; j<chrFilterSize; j++) {
  312. u += chrSrc[j][i] * chrFilter[j];
  313. v += chrSrc[j][i + VOFW] * chrFilter[j];
  314. }
  315. uDest[i]= av_clip_uint8(u>>19);
  316. vDest[i]= av_clip_uint8(v>>19);
  317. }
  318. if (CONFIG_SWSCALE_ALPHA && aDest)
  319. for (i=0; i<dstW; i++) {
  320. int val=1<<18;
  321. int j;
  322. for (j=0; j<lumFilterSize; j++)
  323. val += alpSrc[j][i] * lumFilter[j];
  324. aDest[i]= av_clip_uint8(val>>19);
  325. }
  326. }
  327. static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  328. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  329. uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
  330. {
  331. //FIXME Optimize (just quickly written not optimized..)
  332. int i;
  333. for (i=0; i<dstW; i++) {
  334. int val=1<<18;
  335. int j;
  336. for (j=0; j<lumFilterSize; j++)
  337. val += lumSrc[j][i] * lumFilter[j];
  338. dest[i]= av_clip_uint8(val>>19);
  339. }
  340. if (!uDest)
  341. return;
  342. if (dstFormat == PIX_FMT_NV12)
  343. for (i=0; i<chrDstW; i++) {
  344. int u=1<<18;
  345. int v=1<<18;
  346. int j;
  347. for (j=0; j<chrFilterSize; j++) {
  348. u += chrSrc[j][i] * chrFilter[j];
  349. v += chrSrc[j][i + VOFW] * chrFilter[j];
  350. }
  351. uDest[2*i]= av_clip_uint8(u>>19);
  352. uDest[2*i+1]= av_clip_uint8(v>>19);
  353. }
  354. else
  355. for (i=0; i<chrDstW; i++) {
  356. int u=1<<18;
  357. int v=1<<18;
  358. int j;
  359. for (j=0; j<chrFilterSize; j++) {
  360. u += chrSrc[j][i] * chrFilter[j];
  361. v += chrSrc[j][i + VOFW] * chrFilter[j];
  362. }
  363. uDest[2*i]= av_clip_uint8(v>>19);
  364. uDest[2*i+1]= av_clip_uint8(u>>19);
  365. }
  366. }
  367. #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
  368. for (i=0; i<(dstW>>1); i++) {\
  369. int j;\
  370. int Y1 = 1<<18;\
  371. int Y2 = 1<<18;\
  372. int U = 1<<18;\
  373. int V = 1<<18;\
  374. int av_unused A1, A2;\
  375. type av_unused *r, *b, *g;\
  376. const int i2= 2*i;\
  377. \
  378. for (j=0; j<lumFilterSize; j++) {\
  379. Y1 += lumSrc[j][i2] * lumFilter[j];\
  380. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  381. }\
  382. for (j=0; j<chrFilterSize; j++) {\
  383. U += chrSrc[j][i] * chrFilter[j];\
  384. V += chrSrc[j][i+VOFW] * chrFilter[j];\
  385. }\
  386. Y1>>=19;\
  387. Y2>>=19;\
  388. U >>=19;\
  389. V >>=19;\
  390. if (alpha) {\
  391. A1 = 1<<18;\
  392. A2 = 1<<18;\
  393. for (j=0; j<lumFilterSize; j++) {\
  394. A1 += alpSrc[j][i2 ] * lumFilter[j];\
  395. A2 += alpSrc[j][i2+1] * lumFilter[j];\
  396. }\
  397. A1>>=19;\
  398. A2>>=19;\
  399. }
  400. #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
  401. YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
  402. if ((Y1|Y2|U|V)&256) {\
  403. if (Y1>255) Y1=255; \
  404. else if (Y1<0)Y1=0; \
  405. if (Y2>255) Y2=255; \
  406. else if (Y2<0)Y2=0; \
  407. if (U>255) U=255; \
  408. else if (U<0) U=0; \
  409. if (V>255) V=255; \
  410. else if (V<0) V=0; \
  411. }\
  412. if (alpha && ((A1|A2)&256)) {\
  413. A1=av_clip_uint8(A1);\
  414. A2=av_clip_uint8(A2);\
  415. }
  416. #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
  417. for (i=0; i<dstW; i++) {\
  418. int j;\
  419. int Y = 0;\
  420. int U = -128<<19;\
  421. int V = -128<<19;\
  422. int av_unused A;\
  423. int R,G,B;\
  424. \
  425. for (j=0; j<lumFilterSize; j++) {\
  426. Y += lumSrc[j][i ] * lumFilter[j];\
  427. }\
  428. for (j=0; j<chrFilterSize; j++) {\
  429. U += chrSrc[j][i ] * chrFilter[j];\
  430. V += chrSrc[j][i+VOFW] * chrFilter[j];\
  431. }\
  432. Y >>=10;\
  433. U >>=10;\
  434. V >>=10;\
  435. if (alpha) {\
  436. A = rnd;\
  437. for (j=0; j<lumFilterSize; j++)\
  438. A += alpSrc[j][i ] * lumFilter[j];\
  439. A >>=19;\
  440. if (A&256)\
  441. A = av_clip_uint8(A);\
  442. }
  443. #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
  444. YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
  445. Y-= c->yuv2rgb_y_offset;\
  446. Y*= c->yuv2rgb_y_coeff;\
  447. Y+= rnd;\
  448. R= Y + V*c->yuv2rgb_v2r_coeff;\
  449. G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
  450. B= Y + U*c->yuv2rgb_u2b_coeff;\
  451. if ((R|G|B)&(0xC0000000)) {\
  452. if (R>=(256<<22)) R=(256<<22)-1; \
  453. else if (R<0)R=0; \
  454. if (G>=(256<<22)) G=(256<<22)-1; \
  455. else if (G<0)G=0; \
  456. if (B>=(256<<22)) B=(256<<22)-1; \
  457. else if (B<0)B=0; \
  458. }
  459. #define YSCALE_YUV_2_GRAY16_C \
  460. for (i=0; i<(dstW>>1); i++) {\
  461. int j;\
  462. int Y1 = 1<<18;\
  463. int Y2 = 1<<18;\
  464. int U = 1<<18;\
  465. int V = 1<<18;\
  466. \
  467. const int i2= 2*i;\
  468. \
  469. for (j=0; j<lumFilterSize; j++) {\
  470. Y1 += lumSrc[j][i2] * lumFilter[j];\
  471. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  472. }\
  473. Y1>>=11;\
  474. Y2>>=11;\
  475. if ((Y1|Y2|U|V)&65536) {\
  476. if (Y1>65535) Y1=65535; \
  477. else if (Y1<0)Y1=0; \
  478. if (Y2>65535) Y2=65535; \
  479. else if (Y2<0)Y2=0; \
  480. }
  481. #define YSCALE_YUV_2_RGBX_C(type,alpha) \
  482. YSCALE_YUV_2_PACKEDX_C(type,alpha) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
  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_PACKED2_C(type,alpha) \
  487. for (i=0; i<(dstW>>1); i++) { \
  488. const int i2= 2*i; \
  489. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
  490. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
  491. int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
  492. int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
  493. type av_unused *r, *b, *g; \
  494. int av_unused A1, A2; \
  495. if (alpha) {\
  496. A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
  497. A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
  498. }
  499. #define YSCALE_YUV_2_GRAY16_2_C \
  500. for (i=0; i<(dstW>>1); i++) { \
  501. const int i2= 2*i; \
  502. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
  503. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;
  504. #define YSCALE_YUV_2_RGB2_C(type,alpha) \
  505. YSCALE_YUV_2_PACKED2_C(type,alpha)\
  506. r = (type *)c->table_rV[V];\
  507. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  508. b = (type *)c->table_bU[U];
  509. #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
  510. for (i=0; i<(dstW>>1); i++) {\
  511. const int i2= 2*i;\
  512. int Y1= buf0[i2 ]>>7;\
  513. int Y2= buf0[i2+1]>>7;\
  514. int U= (uvbuf1[i ])>>7;\
  515. int V= (uvbuf1[i+VOFW])>>7;\
  516. type av_unused *r, *b, *g;\
  517. int av_unused A1, A2;\
  518. if (alpha) {\
  519. A1= abuf0[i2 ]>>7;\
  520. A2= abuf0[i2+1]>>7;\
  521. }
  522. #define YSCALE_YUV_2_GRAY16_1_C \
  523. for (i=0; i<(dstW>>1); i++) {\
  524. const int i2= 2*i;\
  525. int Y1= buf0[i2 ]<<1;\
  526. int Y2= buf0[i2+1]<<1;
  527. #define YSCALE_YUV_2_RGB1_C(type,alpha) \
  528. YSCALE_YUV_2_PACKED1_C(type,alpha)\
  529. r = (type *)c->table_rV[V];\
  530. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  531. b = (type *)c->table_bU[U];
  532. #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
  533. for (i=0; i<(dstW>>1); i++) {\
  534. const int i2= 2*i;\
  535. int Y1= buf0[i2 ]>>7;\
  536. int Y2= buf0[i2+1]>>7;\
  537. int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
  538. int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
  539. type av_unused *r, *b, *g;\
  540. int av_unused A1, A2;\
  541. if (alpha) {\
  542. A1= abuf0[i2 ]>>7;\
  543. A2= abuf0[i2+1]>>7;\
  544. }
  545. #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
  546. YSCALE_YUV_2_PACKED1B_C(type,alpha)\
  547. r = (type *)c->table_rV[V];\
  548. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  549. b = (type *)c->table_bU[U];
  550. #define YSCALE_YUV_2_MONO2_C \
  551. const uint8_t * const d128=dither_8x8_220[y&7];\
  552. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  553. for (i=0; i<dstW-7; i+=8) {\
  554. int acc;\
  555. acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
  556. acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
  557. acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
  558. acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
  559. acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
  560. acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
  561. acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
  562. acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
  563. ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
  564. dest++;\
  565. }
  566. #define YSCALE_YUV_2_MONOX_C \
  567. const uint8_t * const d128=dither_8x8_220[y&7];\
  568. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  569. int acc=0;\
  570. for (i=0; i<dstW-1; i+=2) {\
  571. int j;\
  572. int Y1=1<<18;\
  573. int Y2=1<<18;\
  574. \
  575. for (j=0; j<lumFilterSize; j++) {\
  576. Y1 += lumSrc[j][i] * lumFilter[j];\
  577. Y2 += lumSrc[j][i+1] * lumFilter[j];\
  578. }\
  579. Y1>>=19;\
  580. Y2>>=19;\
  581. if ((Y1|Y2)&256) {\
  582. if (Y1>255) Y1=255;\
  583. else if (Y1<0)Y1=0;\
  584. if (Y2>255) Y2=255;\
  585. else if (Y2<0)Y2=0;\
  586. }\
  587. acc+= acc + g[Y1+d128[(i+0)&7]];\
  588. acc+= acc + g[Y2+d128[(i+1)&7]];\
  589. if ((i&7)==6) {\
  590. ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
  591. dest++;\
  592. }\
  593. }
  594. #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
  595. switch(c->dstFormat) {\
  596. case PIX_FMT_RGB48BE:\
  597. case PIX_FMT_RGB48LE:\
  598. func(uint8_t,0)\
  599. ((uint8_t*)dest)[ 0]= r[Y1];\
  600. ((uint8_t*)dest)[ 1]= r[Y1];\
  601. ((uint8_t*)dest)[ 2]= g[Y1];\
  602. ((uint8_t*)dest)[ 3]= g[Y1];\
  603. ((uint8_t*)dest)[ 4]= b[Y1];\
  604. ((uint8_t*)dest)[ 5]= b[Y1];\
  605. ((uint8_t*)dest)[ 6]= r[Y2];\
  606. ((uint8_t*)dest)[ 7]= r[Y2];\
  607. ((uint8_t*)dest)[ 8]= g[Y2];\
  608. ((uint8_t*)dest)[ 9]= g[Y2];\
  609. ((uint8_t*)dest)[10]= b[Y2];\
  610. ((uint8_t*)dest)[11]= b[Y2];\
  611. dest+=12;\
  612. }\
  613. break;\
  614. case PIX_FMT_BGR48BE:\
  615. case PIX_FMT_BGR48LE:\
  616. func(uint8_t,0)\
  617. ((uint8_t*)dest)[ 0] = ((uint8_t*)dest)[ 1] = b[Y1];\
  618. ((uint8_t*)dest)[ 2] = ((uint8_t*)dest)[ 3] = g[Y1];\
  619. ((uint8_t*)dest)[ 4] = ((uint8_t*)dest)[ 5] = r[Y1];\
  620. ((uint8_t*)dest)[ 6] = ((uint8_t*)dest)[ 7] = b[Y2];\
  621. ((uint8_t*)dest)[ 8] = ((uint8_t*)dest)[ 9] = g[Y2];\
  622. ((uint8_t*)dest)[10] = ((uint8_t*)dest)[11] = r[Y2];\
  623. dest+=12;\
  624. }\
  625. break;\
  626. case PIX_FMT_RGBA:\
  627. case PIX_FMT_BGRA:\
  628. if (CONFIG_SMALL) {\
  629. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
  630. func(uint32_t,needAlpha)\
  631. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
  632. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
  633. }\
  634. } else {\
  635. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
  636. func(uint32_t,1)\
  637. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
  638. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
  639. }\
  640. } else {\
  641. func(uint32_t,0)\
  642. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  643. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  644. }\
  645. }\
  646. }\
  647. break;\
  648. case PIX_FMT_ARGB:\
  649. case PIX_FMT_ABGR:\
  650. if (CONFIG_SMALL) {\
  651. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
  652. func(uint32_t,needAlpha)\
  653. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
  654. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
  655. }\
  656. } else {\
  657. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
  658. func(uint32_t,1)\
  659. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
  660. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
  661. }\
  662. } else {\
  663. func(uint32_t,0)\
  664. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  665. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  666. }\
  667. }\
  668. } \
  669. break;\
  670. case PIX_FMT_RGB24:\
  671. func(uint8_t,0)\
  672. ((uint8_t*)dest)[0]= r[Y1];\
  673. ((uint8_t*)dest)[1]= g[Y1];\
  674. ((uint8_t*)dest)[2]= b[Y1];\
  675. ((uint8_t*)dest)[3]= r[Y2];\
  676. ((uint8_t*)dest)[4]= g[Y2];\
  677. ((uint8_t*)dest)[5]= b[Y2];\
  678. dest+=6;\
  679. }\
  680. break;\
  681. case PIX_FMT_BGR24:\
  682. func(uint8_t,0)\
  683. ((uint8_t*)dest)[0]= b[Y1];\
  684. ((uint8_t*)dest)[1]= g[Y1];\
  685. ((uint8_t*)dest)[2]= r[Y1];\
  686. ((uint8_t*)dest)[3]= b[Y2];\
  687. ((uint8_t*)dest)[4]= g[Y2];\
  688. ((uint8_t*)dest)[5]= r[Y2];\
  689. dest+=6;\
  690. }\
  691. break;\
  692. case PIX_FMT_RGB565BE:\
  693. case PIX_FMT_RGB565LE:\
  694. case PIX_FMT_BGR565BE:\
  695. case PIX_FMT_BGR565LE:\
  696. {\
  697. const int dr1= dither_2x2_8[y&1 ][0];\
  698. const int dg1= dither_2x2_4[y&1 ][0];\
  699. const int db1= dither_2x2_8[(y&1)^1][0];\
  700. const int dr2= dither_2x2_8[y&1 ][1];\
  701. const int dg2= dither_2x2_4[y&1 ][1];\
  702. const int db2= dither_2x2_8[(y&1)^1][1];\
  703. func(uint16_t,0)\
  704. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  705. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  706. }\
  707. }\
  708. break;\
  709. case PIX_FMT_RGB555BE:\
  710. case PIX_FMT_RGB555LE:\
  711. case PIX_FMT_BGR555BE:\
  712. case PIX_FMT_BGR555LE:\
  713. {\
  714. const int dr1= dither_2x2_8[y&1 ][0];\
  715. const int dg1= dither_2x2_8[y&1 ][1];\
  716. const int db1= dither_2x2_8[(y&1)^1][0];\
  717. const int dr2= dither_2x2_8[y&1 ][1];\
  718. const int dg2= dither_2x2_8[y&1 ][0];\
  719. const int db2= dither_2x2_8[(y&1)^1][1];\
  720. func(uint16_t,0)\
  721. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  722. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  723. }\
  724. }\
  725. break;\
  726. case PIX_FMT_RGB444BE:\
  727. case PIX_FMT_RGB444LE:\
  728. case PIX_FMT_BGR444BE:\
  729. case PIX_FMT_BGR444LE:\
  730. {\
  731. const int dr1= dither_4x4_16[y&3 ][0];\
  732. const int dg1= dither_4x4_16[y&3 ][1];\
  733. const int db1= dither_4x4_16[(y&3)^3][0];\
  734. const int dr2= dither_4x4_16[y&3 ][1];\
  735. const int dg2= dither_4x4_16[y&3 ][0];\
  736. const int db2= dither_4x4_16[(y&3)^3][1];\
  737. func(uint16_t,0)\
  738. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  739. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  740. }\
  741. }\
  742. break;\
  743. case PIX_FMT_RGB8:\
  744. case PIX_FMT_BGR8:\
  745. {\
  746. const uint8_t * const d64= dither_8x8_73[y&7];\
  747. const uint8_t * const d32= dither_8x8_32[y&7];\
  748. func(uint8_t,0)\
  749. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
  750. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
  751. }\
  752. }\
  753. break;\
  754. case PIX_FMT_RGB4:\
  755. case PIX_FMT_BGR4:\
  756. {\
  757. const uint8_t * const d64= dither_8x8_73 [y&7];\
  758. const uint8_t * const d128=dither_8x8_220[y&7];\
  759. func(uint8_t,0)\
  760. ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
  761. + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
  762. }\
  763. }\
  764. break;\
  765. case PIX_FMT_RGB4_BYTE:\
  766. case PIX_FMT_BGR4_BYTE:\
  767. {\
  768. const uint8_t * const d64= dither_8x8_73 [y&7];\
  769. const uint8_t * const d128=dither_8x8_220[y&7];\
  770. func(uint8_t,0)\
  771. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
  772. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
  773. }\
  774. }\
  775. break;\
  776. case PIX_FMT_MONOBLACK:\
  777. case PIX_FMT_MONOWHITE:\
  778. {\
  779. func_monoblack\
  780. }\
  781. break;\
  782. case PIX_FMT_YUYV422:\
  783. func2\
  784. ((uint8_t*)dest)[2*i2+0]= Y1;\
  785. ((uint8_t*)dest)[2*i2+1]= U;\
  786. ((uint8_t*)dest)[2*i2+2]= Y2;\
  787. ((uint8_t*)dest)[2*i2+3]= V;\
  788. } \
  789. break;\
  790. case PIX_FMT_UYVY422:\
  791. func2\
  792. ((uint8_t*)dest)[2*i2+0]= U;\
  793. ((uint8_t*)dest)[2*i2+1]= Y1;\
  794. ((uint8_t*)dest)[2*i2+2]= V;\
  795. ((uint8_t*)dest)[2*i2+3]= Y2;\
  796. } \
  797. break;\
  798. case PIX_FMT_GRAY16BE:\
  799. func_g16\
  800. ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
  801. ((uint8_t*)dest)[2*i2+1]= Y1;\
  802. ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
  803. ((uint8_t*)dest)[2*i2+3]= Y2;\
  804. } \
  805. break;\
  806. case PIX_FMT_GRAY16LE:\
  807. func_g16\
  808. ((uint8_t*)dest)[2*i2+0]= Y1;\
  809. ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
  810. ((uint8_t*)dest)[2*i2+2]= Y2;\
  811. ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
  812. } \
  813. break;\
  814. }
  815. static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  816. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  817. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  818. {
  819. int i;
  820. YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGBX_C, YSCALE_YUV_2_PACKEDX_C(void,0), YSCALE_YUV_2_GRAY16_C, YSCALE_YUV_2_MONOX_C)
  821. }
  822. static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  823. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  824. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  825. {
  826. int i;
  827. int step= c->dstFormatBpp/8;
  828. int aidx= 3;
  829. switch(c->dstFormat) {
  830. case PIX_FMT_ARGB:
  831. dest++;
  832. aidx= 0;
  833. case PIX_FMT_RGB24:
  834. aidx--;
  835. case PIX_FMT_RGBA:
  836. if (CONFIG_SMALL) {
  837. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
  838. YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
  839. dest[aidx]= needAlpha ? A : 255;
  840. dest[0]= R>>22;
  841. dest[1]= G>>22;
  842. dest[2]= B>>22;
  843. dest+= step;
  844. }
  845. } else {
  846. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  847. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
  848. dest[aidx]= A;
  849. dest[0]= R>>22;
  850. dest[1]= G>>22;
  851. dest[2]= B>>22;
  852. dest+= step;
  853. }
  854. } else {
  855. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
  856. dest[aidx]= 255;
  857. dest[0]= R>>22;
  858. dest[1]= G>>22;
  859. dest[2]= B>>22;
  860. dest+= step;
  861. }
  862. }
  863. }
  864. break;
  865. case PIX_FMT_ABGR:
  866. dest++;
  867. aidx= 0;
  868. case PIX_FMT_BGR24:
  869. aidx--;
  870. case PIX_FMT_BGRA:
  871. if (CONFIG_SMALL) {
  872. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
  873. YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
  874. dest[aidx]= needAlpha ? A : 255;
  875. dest[0]= B>>22;
  876. dest[1]= G>>22;
  877. dest[2]= R>>22;
  878. dest+= step;
  879. }
  880. } else {
  881. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  882. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
  883. dest[aidx]= A;
  884. dest[0]= B>>22;
  885. dest[1]= G>>22;
  886. dest[2]= R>>22;
  887. dest+= step;
  888. }
  889. } else {
  890. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
  891. dest[aidx]= 255;
  892. dest[0]= B>>22;
  893. dest[1]= G>>22;
  894. dest[2]= R>>22;
  895. dest+= step;
  896. }
  897. }
  898. }
  899. break;
  900. default:
  901. assert(0);
  902. }
  903. }
  904. static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
  905. {
  906. int i;
  907. uint8_t *ptr = plane + stride*y;
  908. for (i=0; i<height; i++) {
  909. memset(ptr, val, width);
  910. ptr += stride;
  911. }
  912. }
  913. static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, long width,
  914. uint32_t *unused)
  915. {
  916. int i;
  917. for (i = 0; i < width; i++) {
  918. int r = src[i*6+0];
  919. int g = src[i*6+2];
  920. int b = src[i*6+4];
  921. dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  922. }
  923. }
  924. static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
  925. const uint8_t *src1, const uint8_t *src2,
  926. long width, uint32_t *unused)
  927. {
  928. int i;
  929. assert(src1==src2);
  930. for (i = 0; i < width; i++) {
  931. int r = src1[6*i + 0];
  932. int g = src1[6*i + 2];
  933. int b = src1[6*i + 4];
  934. dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  935. dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  936. }
  937. }
  938. static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
  939. const uint8_t *src1, const uint8_t *src2,
  940. long width, uint32_t *unused)
  941. {
  942. int i;
  943. assert(src1==src2);
  944. for (i = 0; i < width; i++) {
  945. int r= src1[12*i + 0] + src1[12*i + 6];
  946. int g= src1[12*i + 2] + src1[12*i + 8];
  947. int b= src1[12*i + 4] + src1[12*i + 10];
  948. dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  949. dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  950. }
  951. }
  952. static inline void bgr48ToY(uint8_t *dst, const uint8_t *src, long width,
  953. uint32_t *unused)
  954. {
  955. int i;
  956. for (i = 0; i < width; i++) {
  957. int b = src[i*6+0];
  958. int g = src[i*6+2];
  959. int r = src[i*6+4];
  960. dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  961. }
  962. }
  963. static inline void bgr48ToUV(uint8_t *dstU, uint8_t *dstV,
  964. const uint8_t *src1, const uint8_t *src2,
  965. long width, uint32_t *unused)
  966. {
  967. int i;
  968. for (i = 0; i < width; i++) {
  969. int b = src1[6*i + 0];
  970. int g = src1[6*i + 2];
  971. int r = src1[6*i + 4];
  972. dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  973. dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  974. }
  975. }
  976. static inline void bgr48ToUV_half(uint8_t *dstU, uint8_t *dstV,
  977. const uint8_t *src1, const uint8_t *src2,
  978. long width, uint32_t *unused)
  979. {
  980. int i;
  981. for (i = 0; i < width; i++) {
  982. int b= src1[12*i + 0] + src1[12*i + 6];
  983. int g= src1[12*i + 2] + src1[12*i + 8];
  984. int r= src1[12*i + 4] + src1[12*i + 10];
  985. dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  986. dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  987. }
  988. }
  989. #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
  990. static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
  991. {\
  992. int i;\
  993. for (i=0; i<width; i++) {\
  994. int b= (((const type*)src)[i]>>shb)&maskb;\
  995. int g= (((const type*)src)[i]>>shg)&maskg;\
  996. int r= (((const type*)src)[i]>>shr)&maskr;\
  997. \
  998. dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
  999. }\
  1000. }
  1001. BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
  1002. BGR2Y(uint32_t,bgr321ToY,16,16, 0, 0xFF00, 0x00FF, 0xFF00, RY , GY<<8, BY , RGB2YUV_SHIFT+8)
  1003. BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
  1004. BGR2Y(uint32_t,rgb321ToY, 0,16,16, 0xFF00, 0x00FF, 0xFF00, RY , GY<<8, BY , RGB2YUV_SHIFT+8)
  1005. BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY , RGB2YUV_SHIFT+8)
  1006. BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY , RGB2YUV_SHIFT+7)
  1007. BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
  1008. BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
  1009. static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  1010. {
  1011. int i;
  1012. for (i=0; i<width; i++) {
  1013. dst[i]= src[4*i];
  1014. }
  1015. }
  1016. #define BGR2UV(type, name, shr, shg, shb, shp, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S) \
  1017. static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
  1018. {\
  1019. int i;\
  1020. for (i=0; i<width; i++) {\
  1021. int b= ((((const type*)src)[i]>>shp)&maskb)>>shb;\
  1022. int g= ((((const type*)src)[i]>>shp)&maskg)>>shg;\
  1023. int r= ((((const type*)src)[i]>>shp)&maskr)>>shr;\
  1024. \
  1025. dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
  1026. dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
  1027. }\
  1028. }\
  1029. static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
  1030. {\
  1031. int i;\
  1032. for (i=0; i<width; i++) {\
  1033. int pix0= ((const type*)src)[2*i+0]>>shp;\
  1034. int pix1= ((const type*)src)[2*i+1]>>shp;\
  1035. int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
  1036. int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
  1037. int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
  1038. g&= maskg|(2*maskg);\
  1039. \
  1040. g>>=shg;\
  1041. \
  1042. dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
  1043. dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
  1044. }\
  1045. }
  1046. BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1047. BGR2UV(uint32_t,bgr321ToUV,16, 0, 0, 8, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1048. BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1049. BGR2UV(uint32_t,rgb321ToUV, 0, 0,16, 8, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1050. BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RU<<11, GU<<5, BU , RV<<11, GV<<5, BV , RGB2YUV_SHIFT+8)
  1051. BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RU<<10, GU<<5, BU , RV<<10, GV<<5, BV , RGB2YUV_SHIFT+7)
  1052. BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RU , GU<<5, BU<<11, RV , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
  1053. BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RU , GU<<5, BU<<10, RV , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
  1054. static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
  1055. {
  1056. int i;
  1057. for (i=0; i<width; i++) {
  1058. int d= src[i];
  1059. dst[i]= pal[d] & 0xFF;
  1060. }
  1061. }
  1062. static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
  1063. const uint8_t *src1, const uint8_t *src2,
  1064. long width, uint32_t *pal)
  1065. {
  1066. int i;
  1067. assert(src1 == src2);
  1068. for (i=0; i<width; i++) {
  1069. int p= pal[src1[i]];
  1070. dstU[i]= p>>8;
  1071. dstV[i]= p>>16;
  1072. }
  1073. }
  1074. static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  1075. {
  1076. int i, j;
  1077. for (i=0; i<width/8; i++) {
  1078. int d= ~src[i];
  1079. for(j=0; j<8; j++)
  1080. dst[8*i+j]= ((d>>(7-j))&1)*255;
  1081. }
  1082. }
  1083. static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  1084. {
  1085. int i, j;
  1086. for (i=0; i<width/8; i++) {
  1087. int d= src[i];
  1088. for(j=0; j<8; j++)
  1089. dst[8*i+j]= ((d>>(7-j))&1)*255;
  1090. }
  1091. }
  1092. //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
  1093. //Plain C versions
  1094. #if CONFIG_RUNTIME_CPUDETECT
  1095. # define COMPILE_C 1
  1096. # if ARCH_X86
  1097. # define COMPILE_MMX HAVE_MMX
  1098. # define COMPILE_MMX2 HAVE_MMX2
  1099. # define COMPILE_3DNOW HAVE_AMD3DNOW
  1100. # elif ARCH_PPC
  1101. # define COMPILE_ALTIVEC HAVE_ALTIVEC
  1102. # endif
  1103. #else /* CONFIG_RUNTIME_CPUDETECT */
  1104. # if ARCH_X86
  1105. # if HAVE_MMX2
  1106. # define COMPILE_MMX2 1
  1107. # elif HAVE_AMD3DNOW
  1108. # define COMPILE_3DNOW 1
  1109. # elif HAVE_MMX
  1110. # define COMPILE_MMX 1
  1111. # else
  1112. # define COMPILE_C 1
  1113. # endif
  1114. # elif ARCH_PPC && HAVE_ALTIVEC
  1115. # define COMPILE_ALTIVEC 1
  1116. # else
  1117. # define COMPILE_C 1
  1118. # endif
  1119. #endif
  1120. #ifndef COMPILE_C
  1121. # define COMPILE_C 0
  1122. #endif
  1123. #ifndef COMPILE_MMX
  1124. # define COMPILE_MMX 0
  1125. #endif
  1126. #ifndef COMPILE_MMX2
  1127. # define COMPILE_MMX2 0
  1128. #endif
  1129. #ifndef COMPILE_3DNOW
  1130. # define COMPILE_3DNOW 0
  1131. #endif
  1132. #ifndef COMPILE_ALTIVEC
  1133. # define COMPILE_ALTIVEC 0
  1134. #endif
  1135. #define COMPILE_TEMPLATE_MMX 0
  1136. #define COMPILE_TEMPLATE_MMX2 0
  1137. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1138. #define COMPILE_TEMPLATE_ALTIVEC 0
  1139. #include "swscale_template.c"
  1140. #if COMPILE_ALTIVEC
  1141. #undef RENAME
  1142. #undef COMPILE_TEMPLATE_ALTIVEC
  1143. #define COMPILE_TEMPLATE_ALTIVEC 1
  1144. #define RENAME(a) a ## _altivec
  1145. #include "ppc/swscale_template.c"
  1146. #endif
  1147. #if ARCH_X86
  1148. //MMX versions
  1149. #if COMPILE_MMX
  1150. #undef RENAME
  1151. #undef COMPILE_TEMPLATE_MMX
  1152. #undef COMPILE_TEMPLATE_MMX2
  1153. #undef COMPILE_TEMPLATE_AMD3DNOW
  1154. #define COMPILE_TEMPLATE_MMX 1
  1155. #define COMPILE_TEMPLATE_MMX2 0
  1156. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1157. #define RENAME(a) a ## _MMX
  1158. #include "x86/swscale_template.c"
  1159. #endif
  1160. //MMX2 versions
  1161. #if COMPILE_MMX2
  1162. #undef RENAME
  1163. #undef COMPILE_TEMPLATE_MMX
  1164. #undef COMPILE_TEMPLATE_MMX2
  1165. #undef COMPILE_TEMPLATE_AMD3DNOW
  1166. #define COMPILE_TEMPLATE_MMX 1
  1167. #define COMPILE_TEMPLATE_MMX2 1
  1168. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1169. #define RENAME(a) a ## _MMX2
  1170. #include "x86/swscale_template.c"
  1171. #endif
  1172. //3DNOW versions
  1173. #if COMPILE_3DNOW
  1174. #undef RENAME
  1175. #undef COMPILE_TEMPLATE_MMX
  1176. #undef COMPILE_TEMPLATE_MMX2
  1177. #undef COMPILE_TEMPLATE_AMD3DNOW
  1178. #define COMPILE_TEMPLATE_MMX 1
  1179. #define COMPILE_TEMPLATE_MMX2 0
  1180. #define COMPILE_TEMPLATE_AMD3DNOW 1
  1181. #define RENAME(a) a ## _3DNow
  1182. #include "x86/swscale_template.c"
  1183. #endif
  1184. #endif //ARCH_X86
  1185. SwsFunc ff_getSwsFunc(SwsContext *c)
  1186. {
  1187. sws_init_swScale_c(c);
  1188. #if CONFIG_RUNTIME_CPUDETECT
  1189. #if ARCH_X86
  1190. // ordered per speed fastest first
  1191. if (c->flags & SWS_CPU_CAPS_MMX2) {
  1192. sws_init_swScale_MMX2(c);
  1193. return swScale_MMX2;
  1194. } else if (c->flags & SWS_CPU_CAPS_3DNOW) {
  1195. sws_init_swScale_3DNow(c);
  1196. return swScale_3DNow;
  1197. } else if (c->flags & SWS_CPU_CAPS_MMX) {
  1198. sws_init_swScale_MMX(c);
  1199. return swScale_MMX;
  1200. }
  1201. #else
  1202. #if COMPILE_ALTIVEC
  1203. if (c->flags & SWS_CPU_CAPS_ALTIVEC) {
  1204. sws_init_swScale_altivec(c);
  1205. return swScale_altivec;
  1206. }
  1207. #endif
  1208. #endif /* ARCH_X86 */
  1209. #else //CONFIG_RUNTIME_CPUDETECT
  1210. #if COMPILE_TEMPLATE_MMX2
  1211. sws_init_swScale_MMX2(c);
  1212. return swScale_MMX2;
  1213. #elif COMPILE_TEMPLATE_AMD3DNOW
  1214. sws_init_swScale_3DNow(c);
  1215. return swScale_3DNow;
  1216. #elif COMPILE_TEMPLATE_MMX
  1217. sws_init_swScale_MMX(c);
  1218. return swScale_MMX;
  1219. #elif COMPILE_TEMPLATE_ALTIVEC
  1220. sws_init_swScale_altivec(c);
  1221. return swScale_altivec;
  1222. #endif
  1223. #endif //!CONFIG_RUNTIME_CPUDETECT
  1224. return swScale_c;
  1225. }
  1226. static void copyPlane(const uint8_t *src, int srcStride,
  1227. int srcSliceY, int srcSliceH, int width,
  1228. uint8_t *dst, int dstStride)
  1229. {
  1230. dst += dstStride * srcSliceY;
  1231. if (dstStride == srcStride && srcStride > 0) {
  1232. memcpy(dst, src, srcSliceH * dstStride);
  1233. } else {
  1234. int i;
  1235. for (i=0; i<srcSliceH; i++) {
  1236. memcpy(dst, src, width);
  1237. src += srcStride;
  1238. dst += dstStride;
  1239. }
  1240. }
  1241. }
  1242. static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1243. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1244. {
  1245. uint8_t *dst = dstParam[1] + dstStride[1]*srcSliceY/2;
  1246. copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
  1247. dstParam[0], dstStride[0]);
  1248. if (c->dstFormat == PIX_FMT_NV12)
  1249. interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
  1250. else
  1251. interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
  1252. return srcSliceH;
  1253. }
  1254. static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1255. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1256. {
  1257. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1258. yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
  1259. return srcSliceH;
  1260. }
  1261. static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1262. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1263. {
  1264. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1265. yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
  1266. return srcSliceH;
  1267. }
  1268. static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1269. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1270. {
  1271. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1272. yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
  1273. return srcSliceH;
  1274. }
  1275. static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1276. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1277. {
  1278. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1279. yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
  1280. return srcSliceH;
  1281. }
  1282. static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1283. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1284. {
  1285. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1286. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
  1287. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
  1288. yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1289. if (dstParam[3])
  1290. fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1291. return srcSliceH;
  1292. }
  1293. static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1294. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1295. {
  1296. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1297. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
  1298. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
  1299. yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1300. return srcSliceH;
  1301. }
  1302. static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1303. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1304. {
  1305. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1306. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
  1307. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
  1308. uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1309. if (dstParam[3])
  1310. fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1311. return srcSliceH;
  1312. }
  1313. static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1314. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1315. {
  1316. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1317. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
  1318. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
  1319. uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1320. return srcSliceH;
  1321. }
  1322. static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1323. {
  1324. long i;
  1325. for (i=0; i<num_pixels; i++)
  1326. ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | (src[(i<<1)+1] << 24);
  1327. }
  1328. static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1329. {
  1330. long i;
  1331. for (i=0; i<num_pixels; i++)
  1332. ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | src[(i<<1)+1];
  1333. }
  1334. static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1335. {
  1336. long i;
  1337. for (i=0; i<num_pixels; i++) {
  1338. //FIXME slow?
  1339. dst[0]= palette[src[i<<1]*4+0];
  1340. dst[1]= palette[src[i<<1]*4+1];
  1341. dst[2]= palette[src[i<<1]*4+2];
  1342. dst+= 3;
  1343. }
  1344. }
  1345. static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1346. int srcSliceH, uint8_t* dst[], int dstStride[])
  1347. {
  1348. const enum PixelFormat srcFormat= c->srcFormat;
  1349. const enum PixelFormat dstFormat= c->dstFormat;
  1350. void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
  1351. const uint8_t *palette)=NULL;
  1352. int i;
  1353. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1354. const uint8_t *srcPtr= src[0];
  1355. if (srcFormat == PIX_FMT_Y400A) {
  1356. switch (dstFormat) {
  1357. case PIX_FMT_RGB32 : conv = gray8aToPacked32; break;
  1358. case PIX_FMT_BGR32 : conv = gray8aToPacked32; break;
  1359. case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
  1360. case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
  1361. case PIX_FMT_RGB24 : conv = gray8aToPacked24; break;
  1362. case PIX_FMT_BGR24 : conv = gray8aToPacked24; break;
  1363. }
  1364. } else if (usePal(srcFormat)) {
  1365. switch (dstFormat) {
  1366. case PIX_FMT_RGB32 : conv = sws_convertPalette8ToPacked32; break;
  1367. case PIX_FMT_BGR32 : conv = sws_convertPalette8ToPacked32; break;
  1368. case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
  1369. case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
  1370. case PIX_FMT_RGB24 : conv = sws_convertPalette8ToPacked24; break;
  1371. case PIX_FMT_BGR24 : conv = sws_convertPalette8ToPacked24; break;
  1372. }
  1373. }
  1374. if (!conv)
  1375. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1376. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1377. else {
  1378. for (i=0; i<srcSliceH; i++) {
  1379. conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
  1380. srcPtr+= srcStride[0];
  1381. dstPtr+= dstStride[0];
  1382. }
  1383. }
  1384. return srcSliceH;
  1385. }
  1386. #define isRGBA32(x) ( \
  1387. (x) == PIX_FMT_ARGB \
  1388. || (x) == PIX_FMT_RGBA \
  1389. || (x) == PIX_FMT_BGRA \
  1390. || (x) == PIX_FMT_ABGR \
  1391. )
  1392. /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
  1393. static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1394. int srcSliceH, uint8_t* dst[], int dstStride[])
  1395. {
  1396. const enum PixelFormat srcFormat= c->srcFormat;
  1397. const enum PixelFormat dstFormat= c->dstFormat;
  1398. const int srcBpp= (c->srcFormatBpp + 7) >> 3;
  1399. const int dstBpp= (c->dstFormatBpp + 7) >> 3;
  1400. const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
  1401. const int dstId= c->dstFormatBpp >> 2;
  1402. void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
  1403. #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
  1404. if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
  1405. if ( CONV_IS(ABGR, RGBA)
  1406. || CONV_IS(ARGB, BGRA)
  1407. || CONV_IS(BGRA, ARGB)
  1408. || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
  1409. else if (CONV_IS(ABGR, ARGB)
  1410. || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
  1411. else if (CONV_IS(ABGR, BGRA)
  1412. || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
  1413. else if (CONV_IS(BGRA, RGBA)
  1414. || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
  1415. else if (CONV_IS(BGRA, ABGR)
  1416. || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
  1417. } else
  1418. /* BGR -> BGR */
  1419. if ( (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
  1420. || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
  1421. switch(srcId | (dstId<<4)) {
  1422. case 0x34: conv= rgb16to15; break;
  1423. case 0x36: conv= rgb24to15; break;
  1424. case 0x38: conv= rgb32to15; break;
  1425. case 0x43: conv= rgb15to16; break;
  1426. case 0x46: conv= rgb24to16; break;
  1427. case 0x48: conv= rgb32to16; break;
  1428. case 0x63: conv= rgb15to24; break;
  1429. case 0x64: conv= rgb16to24; break;
  1430. case 0x68: conv= rgb32to24; break;
  1431. case 0x83: conv= rgb15to32; break;
  1432. case 0x84: conv= rgb16to32; break;
  1433. case 0x86: conv= rgb24to32; break;
  1434. }
  1435. } else if ( (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
  1436. || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
  1437. switch(srcId | (dstId<<4)) {
  1438. case 0x33: conv= rgb15tobgr15; break;
  1439. case 0x34: conv= rgb16tobgr15; break;
  1440. case 0x36: conv= rgb24tobgr15; break;
  1441. case 0x38: conv= rgb32tobgr15; break;
  1442. case 0x43: conv= rgb15tobgr16; break;
  1443. case 0x44: conv= rgb16tobgr16; break;
  1444. case 0x46: conv= rgb24tobgr16; break;
  1445. case 0x48: conv= rgb32tobgr16; break;
  1446. case 0x63: conv= rgb15tobgr24; break;
  1447. case 0x64: conv= rgb16tobgr24; break;
  1448. case 0x66: conv= rgb24tobgr24; break;
  1449. case 0x68: conv= rgb32tobgr24; break;
  1450. case 0x83: conv= rgb15tobgr32; break;
  1451. case 0x84: conv= rgb16tobgr32; break;
  1452. case 0x86: conv= rgb24tobgr32; break;
  1453. }
  1454. }
  1455. if (!conv) {
  1456. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1457. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1458. } else {
  1459. const uint8_t *srcPtr= src[0];
  1460. uint8_t *dstPtr= dst[0];
  1461. if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
  1462. srcPtr += ALT32_CORR;
  1463. if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
  1464. dstPtr += ALT32_CORR;
  1465. if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
  1466. conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
  1467. else {
  1468. int i;
  1469. dstPtr += dstStride[0]*srcSliceY;
  1470. for (i=0; i<srcSliceH; i++) {
  1471. conv(srcPtr, dstPtr, c->srcW*srcBpp);
  1472. srcPtr+= srcStride[0];
  1473. dstPtr+= dstStride[0];
  1474. }
  1475. }
  1476. }
  1477. return srcSliceH;
  1478. }
  1479. static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1480. int srcSliceH, uint8_t* dst[], int dstStride[])
  1481. {
  1482. rgb24toyv12(
  1483. src[0],
  1484. dst[0]+ srcSliceY *dstStride[0],
  1485. dst[1]+(srcSliceY>>1)*dstStride[1],
  1486. dst[2]+(srcSliceY>>1)*dstStride[2],
  1487. c->srcW, srcSliceH,
  1488. dstStride[0], dstStride[1], srcStride[0]);
  1489. if (dst[3])
  1490. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1491. return srcSliceH;
  1492. }
  1493. static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1494. int srcSliceH, uint8_t* dst[], int dstStride[])
  1495. {
  1496. copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
  1497. dst[0], dstStride[0]);
  1498. planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
  1499. srcSliceH >> 2, srcStride[1], dstStride[1]);
  1500. planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
  1501. srcSliceH >> 2, srcStride[2], dstStride[2]);
  1502. if (dst[3])
  1503. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1504. return srcSliceH;
  1505. }
  1506. /* unscaled copy like stuff (assumes nearly identical formats) */
  1507. static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1508. int srcSliceH, uint8_t* dst[], int dstStride[])
  1509. {
  1510. if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
  1511. memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
  1512. else {
  1513. int i;
  1514. const uint8_t *srcPtr= src[0];
  1515. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1516. int length=0;
  1517. /* universal length finder */
  1518. while(length+c->srcW <= FFABS(dstStride[0])
  1519. && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
  1520. assert(length!=0);
  1521. for (i=0; i<srcSliceH; i++) {
  1522. memcpy(dstPtr, srcPtr, length);
  1523. srcPtr+= srcStride[0];
  1524. dstPtr+= dstStride[0];
  1525. }
  1526. }
  1527. return srcSliceH;
  1528. }
  1529. static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1530. int srcSliceH, uint8_t* dst[], int dstStride[])
  1531. {
  1532. int plane, i, j;
  1533. for (plane=0; plane<4; plane++) {
  1534. int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
  1535. int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
  1536. int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
  1537. const uint8_t *srcPtr= src[plane];
  1538. uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
  1539. if (!dst[plane]) continue;
  1540. // ignore palette for GRAY8
  1541. if (plane == 1 && !dst[2]) continue;
  1542. if (!src[plane] || (plane == 1 && !src[2])) {
  1543. if(is16BPS(c->dstFormat))
  1544. length*=2;
  1545. fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
  1546. } else {
  1547. if(is9_OR_10BPS(c->srcFormat)) {
  1548. const int src_depth = av_pix_fmt_descriptors[c->srcFormat].comp[plane].depth_minus1+1;
  1549. const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
  1550. const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
  1551. if (is16BPS(c->dstFormat)) {
  1552. uint16_t *dstPtr2 = (uint16_t*)dstPtr;
  1553. #define COPY9_OR_10TO16(rfunc, wfunc) \
  1554. for (i = 0; i < height; i++) { \
  1555. for (j = 0; j < length; j++) { \
  1556. int srcpx = rfunc(&srcPtr2[j]); \
  1557. wfunc(&dstPtr2[j], (srcpx<<(16-src_depth)) | (srcpx>>(2*src_depth-16))); \
  1558. } \
  1559. dstPtr2 += dstStride[plane]/2; \
  1560. srcPtr2 += srcStride[plane]/2; \
  1561. }
  1562. if (isBE(c->dstFormat)) {
  1563. if (isBE(c->srcFormat)) {
  1564. COPY9_OR_10TO16(AV_RB16, AV_WB16);
  1565. } else {
  1566. COPY9_OR_10TO16(AV_RL16, AV_WB16);
  1567. }
  1568. } else {
  1569. if (isBE(c->srcFormat)) {
  1570. COPY9_OR_10TO16(AV_RB16, AV_WL16);
  1571. } else {
  1572. COPY9_OR_10TO16(AV_RL16, AV_WL16);
  1573. }
  1574. }
  1575. } else if (is9_OR_10BPS(c->dstFormat)) {
  1576. uint16_t *dstPtr2 = (uint16_t*)dstPtr;
  1577. #define COPY9_OR_10TO9_OR_10(loop) \
  1578. for (i = 0; i < height; i++) { \
  1579. for (j = 0; j < length; j++) { \
  1580. loop; \
  1581. } \
  1582. dstPtr2 += dstStride[plane]/2; \
  1583. srcPtr2 += srcStride[plane]/2; \
  1584. }
  1585. #define COPY9_OR_10TO9_OR_10_2(rfunc, wfunc) \
  1586. if (dst_depth > src_depth) { \
  1587. COPY9_OR_10TO9_OR_10(int srcpx = rfunc(&srcPtr2[j]); \
  1588. wfunc(&dstPtr2[j], (srcpx << 1) | (srcpx >> 9))); \
  1589. } else if (dst_depth < src_depth) { \
  1590. COPY9_OR_10TO9_OR_10(wfunc(&dstPtr2[j], rfunc(&srcPtr2[j]) >> 1)); \
  1591. } else { \
  1592. COPY9_OR_10TO9_OR_10(wfunc(&dstPtr2[j], rfunc(&srcPtr2[j]))); \
  1593. }
  1594. if (isBE(c->dstFormat)) {
  1595. if (isBE(c->srcFormat)) {
  1596. COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WB16);
  1597. } else {
  1598. COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WB16);
  1599. }
  1600. } else {
  1601. if (isBE(c->srcFormat)) {
  1602. COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WL16);
  1603. } else {
  1604. COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WL16);
  1605. }
  1606. }
  1607. } else {
  1608. // FIXME Maybe dither instead.
  1609. #define COPY9_OR_10TO8(rfunc) \
  1610. for (i = 0; i < height; i++) { \
  1611. for (j = 0; j < length; j++) { \
  1612. dstPtr[j] = rfunc(&srcPtr2[j])>>(src_depth-8); \
  1613. } \
  1614. dstPtr += dstStride[plane]; \
  1615. srcPtr2 += srcStride[plane]/2; \
  1616. }
  1617. if (isBE(c->srcFormat)) {
  1618. COPY9_OR_10TO8(AV_RB16);
  1619. } else {
  1620. COPY9_OR_10TO8(AV_RL16);
  1621. }
  1622. }
  1623. } else if(is9_OR_10BPS(c->dstFormat)) {
  1624. const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
  1625. uint16_t *dstPtr2 = (uint16_t*)dstPtr;
  1626. if (is16BPS(c->srcFormat)) {
  1627. const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
  1628. #define COPY16TO9_OR_10(rfunc, wfunc) \
  1629. for (i = 0; i < height; i++) { \
  1630. for (j = 0; j < length; j++) { \
  1631. wfunc(&dstPtr2[j], rfunc(&srcPtr2[j])>>(16-dst_depth)); \
  1632. } \
  1633. dstPtr2 += dstStride[plane]/2; \
  1634. srcPtr2 += srcStride[plane]/2; \
  1635. }
  1636. if (isBE(c->dstFormat)) {
  1637. if (isBE(c->srcFormat)) {
  1638. COPY16TO9_OR_10(AV_RB16, AV_WB16);
  1639. } else {
  1640. COPY16TO9_OR_10(AV_RL16, AV_WB16);
  1641. }
  1642. } else {
  1643. if (isBE(c->srcFormat)) {
  1644. COPY16TO9_OR_10(AV_RB16, AV_WL16);
  1645. } else {
  1646. COPY16TO9_OR_10(AV_RL16, AV_WL16);
  1647. }
  1648. }
  1649. } else /* 8bit */ {
  1650. #define COPY8TO9_OR_10(wfunc) \
  1651. for (i = 0; i < height; i++) { \
  1652. for (j = 0; j < length; j++) { \
  1653. const int srcpx = srcPtr[j]; \
  1654. wfunc(&dstPtr2[j], (srcpx<<(dst_depth-8)) | (srcpx >> (16-dst_depth))); \
  1655. } \
  1656. dstPtr2 += dstStride[plane]/2; \
  1657. srcPtr += srcStride[plane]; \
  1658. }
  1659. if (isBE(c->dstFormat)) {
  1660. COPY8TO9_OR_10(AV_WB16);
  1661. } else {
  1662. COPY8TO9_OR_10(AV_WL16);
  1663. }
  1664. }
  1665. } else if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
  1666. if (!isBE(c->srcFormat)) srcPtr++;
  1667. for (i=0; i<height; i++) {
  1668. for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
  1669. srcPtr+= srcStride[plane];
  1670. dstPtr+= dstStride[plane];
  1671. }
  1672. } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
  1673. for (i=0; i<height; i++) {
  1674. for (j=0; j<length; j++) {
  1675. dstPtr[ j<<1 ] = srcPtr[j];
  1676. dstPtr[(j<<1)+1] = srcPtr[j];
  1677. }
  1678. srcPtr+= srcStride[plane];
  1679. dstPtr+= dstStride[plane];
  1680. }
  1681. } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
  1682. && isBE(c->srcFormat) != isBE(c->dstFormat)) {
  1683. for (i=0; i<height; i++) {
  1684. for (j=0; j<length; j++)
  1685. ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
  1686. srcPtr+= srcStride[plane];
  1687. dstPtr+= dstStride[plane];
  1688. }
  1689. } else if (dstStride[plane] == srcStride[plane] &&
  1690. srcStride[plane] > 0 && srcStride[plane] == length) {
  1691. memcpy(dst[plane] + dstStride[plane]*y, src[plane],
  1692. height*dstStride[plane]);
  1693. } else {
  1694. if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
  1695. length*=2;
  1696. for (i=0; i<height; i++) {
  1697. memcpy(dstPtr, srcPtr, length);
  1698. srcPtr+= srcStride[plane];
  1699. dstPtr+= dstStride[plane];
  1700. }
  1701. }
  1702. }
  1703. }
  1704. return srcSliceH;
  1705. }
  1706. int ff_hardcodedcpuflags(void)
  1707. {
  1708. int flags = 0;
  1709. #if COMPILE_TEMPLATE_MMX2
  1710. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
  1711. #elif COMPILE_TEMPLATE_AMD3DNOW
  1712. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
  1713. #elif COMPILE_TEMPLATE_MMX
  1714. flags |= SWS_CPU_CAPS_MMX;
  1715. #elif COMPILE_TEMPLATE_ALTIVEC
  1716. flags |= SWS_CPU_CAPS_ALTIVEC;
  1717. #elif ARCH_BFIN
  1718. flags |= SWS_CPU_CAPS_BFIN;
  1719. #endif
  1720. return flags;
  1721. }
  1722. void ff_get_unscaled_swscale(SwsContext *c)
  1723. {
  1724. const enum PixelFormat srcFormat = c->srcFormat;
  1725. const enum PixelFormat dstFormat = c->dstFormat;
  1726. const int flags = c->flags;
  1727. const int dstH = c->dstH;
  1728. int needsDither;
  1729. needsDither= isAnyRGB(dstFormat)
  1730. && c->dstFormatBpp < 24
  1731. && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
  1732. /* yv12_to_nv12 */
  1733. if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
  1734. c->swScale= planarToNv12Wrapper;
  1735. }
  1736. /* yuv2bgr */
  1737. if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
  1738. && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
  1739. c->swScale= ff_yuv2rgb_get_func_ptr(c);
  1740. }
  1741. if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
  1742. c->swScale= yvu9ToYv12Wrapper;
  1743. }
  1744. /* bgr24toYV12 */
  1745. if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
  1746. c->swScale= bgr24ToYv12Wrapper;
  1747. /* RGB/BGR -> RGB/BGR (no dither needed forms) */
  1748. if ( isAnyRGB(srcFormat)
  1749. && isAnyRGB(dstFormat)
  1750. && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
  1751. && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
  1752. && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
  1753. && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
  1754. && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
  1755. && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
  1756. && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
  1757. && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
  1758. && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
  1759. && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
  1760. && srcFormat != PIX_FMT_BGR48LE && dstFormat != PIX_FMT_BGR48LE
  1761. && srcFormat != PIX_FMT_BGR48BE && dstFormat != PIX_FMT_BGR48BE
  1762. && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
  1763. c->swScale= rgbToRgbWrapper;
  1764. if ((usePal(srcFormat) && (
  1765. dstFormat == PIX_FMT_RGB32 ||
  1766. dstFormat == PIX_FMT_RGB32_1 ||
  1767. dstFormat == PIX_FMT_RGB24 ||
  1768. dstFormat == PIX_FMT_BGR32 ||
  1769. dstFormat == PIX_FMT_BGR32_1 ||
  1770. dstFormat == PIX_FMT_BGR24)))
  1771. c->swScale= palToRgbWrapper;
  1772. if (srcFormat == PIX_FMT_YUV422P) {
  1773. if (dstFormat == PIX_FMT_YUYV422)
  1774. c->swScale= yuv422pToYuy2Wrapper;
  1775. else if (dstFormat == PIX_FMT_UYVY422)
  1776. c->swScale= yuv422pToUyvyWrapper;
  1777. }
  1778. /* LQ converters if -sws 0 or -sws 4*/
  1779. if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
  1780. /* yv12_to_yuy2 */
  1781. if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
  1782. if (dstFormat == PIX_FMT_YUYV422)
  1783. c->swScale= planarToYuy2Wrapper;
  1784. else if (dstFormat == PIX_FMT_UYVY422)
  1785. c->swScale= planarToUyvyWrapper;
  1786. }
  1787. }
  1788. if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  1789. c->swScale= yuyvToYuv420Wrapper;
  1790. if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  1791. c->swScale= uyvyToYuv420Wrapper;
  1792. if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
  1793. c->swScale= yuyvToYuv422Wrapper;
  1794. if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
  1795. c->swScale= uyvyToYuv422Wrapper;
  1796. #if COMPILE_ALTIVEC
  1797. if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
  1798. !(c->flags & SWS_BITEXACT) &&
  1799. srcFormat == PIX_FMT_YUV420P) {
  1800. // unscaled YV12 -> packed YUV, we want speed
  1801. if (dstFormat == PIX_FMT_YUYV422)
  1802. c->swScale= yv12toyuy2_unscaled_altivec;
  1803. else if (dstFormat == PIX_FMT_UYVY422)
  1804. c->swScale= yv12touyvy_unscaled_altivec;
  1805. }
  1806. #endif
  1807. /* simple copy */
  1808. if ( srcFormat == dstFormat
  1809. || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
  1810. || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
  1811. || (isPlanarYUV(srcFormat) && isGray(dstFormat))
  1812. || (isPlanarYUV(dstFormat) && isGray(srcFormat))
  1813. || (isGray(dstFormat) && isGray(srcFormat))
  1814. || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
  1815. && c->chrDstHSubSample == c->chrSrcHSubSample
  1816. && c->chrDstVSubSample == c->chrSrcVSubSample
  1817. && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
  1818. && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
  1819. {
  1820. if (isPacked(c->srcFormat))
  1821. c->swScale= packedCopyWrapper;
  1822. else /* Planar YUV or gray */
  1823. c->swScale= planarCopyWrapper;
  1824. }
  1825. #if ARCH_BFIN
  1826. if (flags & SWS_CPU_CAPS_BFIN)
  1827. ff_bfin_get_unscaled_swscale (c);
  1828. #endif
  1829. }
  1830. static void reset_ptr(const uint8_t* src[], int format)
  1831. {
  1832. if(!isALPHA(format))
  1833. src[3]=NULL;
  1834. if(!isPlanarYUV(format)) {
  1835. src[3]=src[2]=NULL;
  1836. if (!usePal(format))
  1837. src[1]= NULL;
  1838. }
  1839. }
  1840. static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
  1841. const int linesizes[4])
  1842. {
  1843. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  1844. int i;
  1845. for (i = 0; i < 4; i++) {
  1846. int plane = desc->comp[i].plane;
  1847. if (!data[plane] || !linesizes[plane])
  1848. return 0;
  1849. }
  1850. return 1;
  1851. }
  1852. /**
  1853. * swscale wrapper, so we don't need to export the SwsContext.
  1854. * Assumes planar YUV to be in YUV order instead of YVU.
  1855. */
  1856. int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
  1857. int srcSliceH, uint8_t* const dst[], const int dstStride[])
  1858. {
  1859. int i;
  1860. const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
  1861. uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
  1862. // do not mess up sliceDir if we have a "trailing" 0-size slice
  1863. if (srcSliceH == 0)
  1864. return 0;
  1865. if (!check_image_pointers(src, c->srcFormat, srcStride)) {
  1866. av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
  1867. return 0;
  1868. }
  1869. if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
  1870. av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
  1871. return 0;
  1872. }
  1873. if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
  1874. av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
  1875. return 0;
  1876. }
  1877. if (c->sliceDir == 0) {
  1878. if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
  1879. }
  1880. if (usePal(c->srcFormat)) {
  1881. for (i=0; i<256; i++) {
  1882. int p, r, g, b,y,u,v;
  1883. if(c->srcFormat == PIX_FMT_PAL8) {
  1884. p=((const uint32_t*)(src[1]))[i];
  1885. r= (p>>16)&0xFF;
  1886. g= (p>> 8)&0xFF;
  1887. b= p &0xFF;
  1888. } else if(c->srcFormat == PIX_FMT_RGB8) {
  1889. r= (i>>5 )*36;
  1890. g= ((i>>2)&7)*36;
  1891. b= (i&3 )*85;
  1892. } else if(c->srcFormat == PIX_FMT_BGR8) {
  1893. b= (i>>6 )*85;
  1894. g= ((i>>3)&7)*36;
  1895. r= (i&7 )*36;
  1896. } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
  1897. r= (i>>3 )*255;
  1898. g= ((i>>1)&3)*85;
  1899. b= (i&1 )*255;
  1900. } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_Y400A) {
  1901. r = g = b = i;
  1902. } else {
  1903. assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
  1904. b= (i>>3 )*255;
  1905. g= ((i>>1)&3)*85;
  1906. r= (i&1 )*255;
  1907. }
  1908. y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1909. u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1910. v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1911. c->pal_yuv[i]= y + (u<<8) + (v<<16);
  1912. switch(c->dstFormat) {
  1913. case PIX_FMT_BGR32:
  1914. #if !HAVE_BIGENDIAN
  1915. case PIX_FMT_RGB24:
  1916. #endif
  1917. c->pal_rgb[i]= r + (g<<8) + (b<<16);
  1918. break;
  1919. case PIX_FMT_BGR32_1:
  1920. #if HAVE_BIGENDIAN
  1921. case PIX_FMT_BGR24:
  1922. #endif
  1923. c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
  1924. break;
  1925. case PIX_FMT_RGB32_1:
  1926. #if HAVE_BIGENDIAN
  1927. case PIX_FMT_RGB24:
  1928. #endif
  1929. c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
  1930. break;
  1931. case PIX_FMT_RGB32:
  1932. #if !HAVE_BIGENDIAN
  1933. case PIX_FMT_BGR24:
  1934. #endif
  1935. default:
  1936. c->pal_rgb[i]= b + (g<<8) + (r<<16);
  1937. }
  1938. }
  1939. }
  1940. // copy strides, so they can safely be modified
  1941. if (c->sliceDir == 1) {
  1942. // slices go from top to bottom
  1943. int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
  1944. int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
  1945. reset_ptr(src2, c->srcFormat);
  1946. reset_ptr((const uint8_t**)dst2, c->dstFormat);
  1947. /* reset slice direction at end of frame */
  1948. if (srcSliceY + srcSliceH == c->srcH)
  1949. c->sliceDir = 0;
  1950. return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
  1951. } else {
  1952. // slices go from bottom to top => we flip the image internally
  1953. int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
  1954. int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
  1955. src2[0] += (srcSliceH-1)*srcStride[0];
  1956. if (!usePal(c->srcFormat))
  1957. src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
  1958. src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
  1959. src2[3] += (srcSliceH-1)*srcStride[3];
  1960. dst2[0] += ( c->dstH -1)*dstStride[0];
  1961. dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
  1962. dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
  1963. dst2[3] += ( c->dstH -1)*dstStride[3];
  1964. reset_ptr(src2, c->srcFormat);
  1965. reset_ptr((const uint8_t**)dst2, c->dstFormat);
  1966. /* reset slice direction at end of frame */
  1967. if (!srcSliceY)
  1968. c->sliceDir = 0;
  1969. return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
  1970. }
  1971. }
  1972. /* Convert the palette to the same packed 32-bit format as the palette */
  1973. void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1974. {
  1975. long i;
  1976. for (i=0; i<num_pixels; i++)
  1977. ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
  1978. }
  1979. /* Palette format: ABCD -> dst format: ABC */
  1980. void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1981. {
  1982. long i;
  1983. for (i=0; i<num_pixels; i++) {
  1984. //FIXME slow?
  1985. dst[0]= palette[src[i]*4+0];
  1986. dst[1]= palette[src[i]*4+1];
  1987. dst[2]= palette[src[i]*4+2];
  1988. dst+= 3;
  1989. }
  1990. }