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.

2113 lines
74KB

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