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.

1965 lines
69KB

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