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.

2143 lines
75KB

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