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.

1983 lines
70KB

  1. /*
  2. * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /*
  21. supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR32_1, BGR24, BGR16, BGR15, RGB32, RGB32_1, RGB24, Y8/Y800, YVU9/IF09, PAL8
  22. supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
  23. {BGR,RGB}{1,4,8,15,16} support dithering
  24. unscaled special converters (YV12=I420=IYUV, Y800=Y8)
  25. YV12 -> {BGR,RGB}{1,4,8,12,15,16,24,32}
  26. x -> x
  27. YUV9 -> YV12
  28. YUV9/YV12 -> Y800
  29. Y800 -> YUV9/YV12
  30. BGR24 -> BGR32 & RGB24 -> RGB32
  31. BGR32 -> BGR24 & RGB32 -> RGB24
  32. BGR15 -> BGR16
  33. */
  34. /*
  35. tested special converters (most are tested actually, but I did not write it down ...)
  36. YV12 -> BGR12/BGR16
  37. YV12 -> YV12
  38. BGR15 -> BGR16
  39. BGR16 -> BGR16
  40. YVU9 -> YV12
  41. untested special converters
  42. YV12/I420 -> BGR15/BGR24/BGR32 (it is the yuv2rgb stuff, so it should be OK)
  43. YV12/I420 -> YV12/I420
  44. YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
  45. BGR24 -> BGR32 & RGB24 -> RGB32
  46. BGR32 -> BGR24 & RGB32 -> RGB24
  47. BGR24 -> YV12
  48. */
  49. #include <inttypes.h>
  50. #include <string.h>
  51. #include <math.h>
  52. #include <stdio.h>
  53. #include "config.h"
  54. #include <assert.h>
  55. #include "swscale.h"
  56. #include "swscale_internal.h"
  57. #include "rgb2rgb.h"
  58. #include "libavutil/intreadwrite.h"
  59. #include "libavutil/x86_cpu.h"
  60. #include "libavutil/avutil.h"
  61. #include "libavutil/mathematics.h"
  62. #include "libavutil/bswap.h"
  63. #include "libavutil/pixdesc.h"
  64. #undef MOVNTQ
  65. #undef PAVGB
  66. //#undef HAVE_MMX2
  67. //#define HAVE_AMD3DNOW
  68. //#undef HAVE_MMX
  69. //#undef ARCH_X86
  70. #define DITHER1XBPP
  71. #define FAST_BGR2YV12 // use 7 bit coefficients instead of 15 bit
  72. #define isPacked(x) ( \
  73. (x)==PIX_FMT_PAL8 \
  74. || (x)==PIX_FMT_YUYV422 \
  75. || (x)==PIX_FMT_UYVY422 \
  76. || 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},
  90. {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
  91. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
  92. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
  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},
  95. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //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_RGBA:\
  631. case PIX_FMT_BGRA:\
  632. if (CONFIG_SMALL) {\
  633. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
  634. func(uint32_t,needAlpha)\
  635. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
  636. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
  637. }\
  638. } else {\
  639. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
  640. func(uint32_t,1)\
  641. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
  642. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
  643. }\
  644. } else {\
  645. func(uint32_t,0)\
  646. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  647. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  648. }\
  649. }\
  650. }\
  651. break;\
  652. case PIX_FMT_ARGB:\
  653. case PIX_FMT_ABGR:\
  654. if (CONFIG_SMALL) {\
  655. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
  656. func(uint32_t,needAlpha)\
  657. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
  658. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
  659. }\
  660. } else {\
  661. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
  662. func(uint32_t,1)\
  663. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
  664. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
  665. }\
  666. } else {\
  667. func(uint32_t,0)\
  668. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  669. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  670. }\
  671. }\
  672. } \
  673. break;\
  674. case PIX_FMT_RGB24:\
  675. func(uint8_t,0)\
  676. ((uint8_t*)dest)[0]= r[Y1];\
  677. ((uint8_t*)dest)[1]= g[Y1];\
  678. ((uint8_t*)dest)[2]= b[Y1];\
  679. ((uint8_t*)dest)[3]= r[Y2];\
  680. ((uint8_t*)dest)[4]= g[Y2];\
  681. ((uint8_t*)dest)[5]= b[Y2];\
  682. dest+=6;\
  683. }\
  684. break;\
  685. case PIX_FMT_BGR24:\
  686. func(uint8_t,0)\
  687. ((uint8_t*)dest)[0]= b[Y1];\
  688. ((uint8_t*)dest)[1]= g[Y1];\
  689. ((uint8_t*)dest)[2]= r[Y1];\
  690. ((uint8_t*)dest)[3]= b[Y2];\
  691. ((uint8_t*)dest)[4]= g[Y2];\
  692. ((uint8_t*)dest)[5]= r[Y2];\
  693. dest+=6;\
  694. }\
  695. break;\
  696. case PIX_FMT_RGB565BE:\
  697. case PIX_FMT_RGB565LE:\
  698. case PIX_FMT_BGR565BE:\
  699. case PIX_FMT_BGR565LE:\
  700. {\
  701. const int dr1= dither_2x2_8[y&1 ][0];\
  702. const int dg1= dither_2x2_4[y&1 ][0];\
  703. const int db1= dither_2x2_8[(y&1)^1][0];\
  704. const int dr2= dither_2x2_8[y&1 ][1];\
  705. const int dg2= dither_2x2_4[y&1 ][1];\
  706. const int db2= dither_2x2_8[(y&1)^1][1];\
  707. func(uint16_t,0)\
  708. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  709. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  710. }\
  711. }\
  712. break;\
  713. case PIX_FMT_RGB555BE:\
  714. case PIX_FMT_RGB555LE:\
  715. case PIX_FMT_BGR555BE:\
  716. case PIX_FMT_BGR555LE:\
  717. {\
  718. const int dr1= dither_2x2_8[y&1 ][0];\
  719. const int dg1= dither_2x2_8[y&1 ][1];\
  720. const int db1= dither_2x2_8[(y&1)^1][0];\
  721. const int dr2= dither_2x2_8[y&1 ][1];\
  722. const int dg2= dither_2x2_8[y&1 ][0];\
  723. const int db2= dither_2x2_8[(y&1)^1][1];\
  724. func(uint16_t,0)\
  725. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  726. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  727. }\
  728. }\
  729. break;\
  730. case PIX_FMT_RGB444BE:\
  731. case PIX_FMT_RGB444LE:\
  732. case PIX_FMT_BGR444BE:\
  733. case PIX_FMT_BGR444LE:\
  734. {\
  735. const int dr1= dither_4x4_16[y&3 ][0];\
  736. const int dg1= dither_4x4_16[y&3 ][1];\
  737. const int db1= dither_4x4_16[(y&3)^3][0];\
  738. const int dr2= dither_4x4_16[y&3 ][1];\
  739. const int dg2= dither_4x4_16[y&3 ][0];\
  740. const int db2= dither_4x4_16[(y&3)^3][1];\
  741. func(uint16_t,0)\
  742. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  743. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  744. }\
  745. }\
  746. break;\
  747. case PIX_FMT_RGB8:\
  748. case PIX_FMT_BGR8:\
  749. {\
  750. const uint8_t * const d64= dither_8x8_73[y&7];\
  751. const uint8_t * const d32= dither_8x8_32[y&7];\
  752. func(uint8_t,0)\
  753. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
  754. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
  755. }\
  756. }\
  757. break;\
  758. case PIX_FMT_RGB4:\
  759. case PIX_FMT_BGR4:\
  760. {\
  761. const uint8_t * const d64= dither_8x8_73 [y&7];\
  762. const uint8_t * const d128=dither_8x8_220[y&7];\
  763. func(uint8_t,0)\
  764. ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
  765. + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
  766. }\
  767. }\
  768. break;\
  769. case PIX_FMT_RGB4_BYTE:\
  770. case PIX_FMT_BGR4_BYTE:\
  771. {\
  772. const uint8_t * const d64= dither_8x8_73 [y&7];\
  773. const uint8_t * const d128=dither_8x8_220[y&7];\
  774. func(uint8_t,0)\
  775. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
  776. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
  777. }\
  778. }\
  779. break;\
  780. case PIX_FMT_MONOBLACK:\
  781. case PIX_FMT_MONOWHITE:\
  782. {\
  783. func_monoblack\
  784. }\
  785. break;\
  786. case PIX_FMT_YUYV422:\
  787. func2\
  788. ((uint8_t*)dest)[2*i2+0]= Y1;\
  789. ((uint8_t*)dest)[2*i2+1]= U;\
  790. ((uint8_t*)dest)[2*i2+2]= Y2;\
  791. ((uint8_t*)dest)[2*i2+3]= V;\
  792. } \
  793. break;\
  794. case PIX_FMT_UYVY422:\
  795. func2\
  796. ((uint8_t*)dest)[2*i2+0]= U;\
  797. ((uint8_t*)dest)[2*i2+1]= Y1;\
  798. ((uint8_t*)dest)[2*i2+2]= V;\
  799. ((uint8_t*)dest)[2*i2+3]= Y2;\
  800. } \
  801. break;\
  802. case PIX_FMT_GRAY16BE:\
  803. func_g16\
  804. ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
  805. ((uint8_t*)dest)[2*i2+1]= Y1;\
  806. ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
  807. ((uint8_t*)dest)[2*i2+3]= Y2;\
  808. } \
  809. break;\
  810. case PIX_FMT_GRAY16LE:\
  811. func_g16\
  812. ((uint8_t*)dest)[2*i2+0]= Y1;\
  813. ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
  814. ((uint8_t*)dest)[2*i2+2]= Y2;\
  815. ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
  816. } \
  817. break;\
  818. }
  819. static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  820. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  821. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  822. {
  823. int i;
  824. 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)
  825. }
  826. static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  827. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  828. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  829. {
  830. int i;
  831. int step= c->dstFormatBpp/8;
  832. int aidx= 3;
  833. switch(c->dstFormat) {
  834. case PIX_FMT_ARGB:
  835. dest++;
  836. aidx= 0;
  837. case PIX_FMT_RGB24:
  838. aidx--;
  839. case PIX_FMT_RGBA:
  840. if (CONFIG_SMALL) {
  841. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
  842. YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
  843. dest[aidx]= needAlpha ? A : 255;
  844. dest[0]= R>>22;
  845. dest[1]= G>>22;
  846. dest[2]= B>>22;
  847. dest+= step;
  848. }
  849. } else {
  850. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  851. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
  852. dest[aidx]= A;
  853. dest[0]= R>>22;
  854. dest[1]= G>>22;
  855. dest[2]= B>>22;
  856. dest+= step;
  857. }
  858. } else {
  859. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
  860. dest[aidx]= 255;
  861. dest[0]= R>>22;
  862. dest[1]= G>>22;
  863. dest[2]= B>>22;
  864. dest+= step;
  865. }
  866. }
  867. }
  868. break;
  869. case PIX_FMT_ABGR:
  870. dest++;
  871. aidx= 0;
  872. case PIX_FMT_BGR24:
  873. aidx--;
  874. case PIX_FMT_BGRA:
  875. if (CONFIG_SMALL) {
  876. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
  877. YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
  878. dest[aidx]= needAlpha ? A : 255;
  879. dest[0]= B>>22;
  880. dest[1]= G>>22;
  881. dest[2]= R>>22;
  882. dest+= step;
  883. }
  884. } else {
  885. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  886. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
  887. dest[aidx]= A;
  888. dest[0]= B>>22;
  889. dest[1]= G>>22;
  890. dest[2]= R>>22;
  891. dest+= step;
  892. }
  893. } else {
  894. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
  895. dest[aidx]= 255;
  896. dest[0]= B>>22;
  897. dest[1]= G>>22;
  898. dest[2]= R>>22;
  899. dest+= step;
  900. }
  901. }
  902. }
  903. break;
  904. default:
  905. assert(0);
  906. }
  907. }
  908. static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
  909. {
  910. int i;
  911. uint8_t *ptr = plane + stride*y;
  912. for (i=0; i<height; i++) {
  913. memset(ptr, val, width);
  914. ptr += stride;
  915. }
  916. }
  917. static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, int width,
  918. uint32_t *unused)
  919. {
  920. int i;
  921. for (i = 0; i < width; i++) {
  922. int r = src[i*6+0];
  923. int g = src[i*6+2];
  924. int b = src[i*6+4];
  925. dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  926. }
  927. }
  928. static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
  929. const uint8_t *src1, const uint8_t *src2,
  930. int width, uint32_t *unused)
  931. {
  932. int i;
  933. assert(src1==src2);
  934. for (i = 0; i < width; i++) {
  935. int r = src1[6*i + 0];
  936. int g = src1[6*i + 2];
  937. int b = src1[6*i + 4];
  938. dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  939. dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  940. }
  941. }
  942. static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
  943. const uint8_t *src1, const uint8_t *src2,
  944. int width, uint32_t *unused)
  945. {
  946. int i;
  947. assert(src1==src2);
  948. for (i = 0; i < width; i++) {
  949. int r= src1[12*i + 0] + src1[12*i + 6];
  950. int g= src1[12*i + 2] + src1[12*i + 8];
  951. int b= src1[12*i + 4] + src1[12*i + 10];
  952. dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  953. dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  954. }
  955. }
  956. #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
  957. static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
  958. {\
  959. int i;\
  960. for (i=0; i<width; i++) {\
  961. int b= (((const type*)src)[i]>>shb)&maskb;\
  962. int g= (((const type*)src)[i]>>shg)&maskg;\
  963. int r= (((const type*)src)[i]>>shr)&maskr;\
  964. \
  965. dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
  966. }\
  967. }
  968. BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
  969. BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
  970. BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY , RGB2YUV_SHIFT+8)
  971. BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY , RGB2YUV_SHIFT+7)
  972. BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
  973. BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
  974. static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  975. {
  976. int i;
  977. for (i=0; i<width; i++) {
  978. dst[i]= src[4*i];
  979. }
  980. }
  981. #define BGR2UV(type, name, shr, shg, shb, maska, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S)\
  982. static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
  983. {\
  984. int i;\
  985. for (i=0; i<width; i++) {\
  986. int b= (((const type*)src)[i]&maskb)>>shb;\
  987. int g= (((const type*)src)[i]&maskg)>>shg;\
  988. int r= (((const type*)src)[i]&maskr)>>shr;\
  989. \
  990. dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
  991. dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
  992. }\
  993. }\
  994. static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
  995. {\
  996. int i;\
  997. for (i=0; i<width; i++) {\
  998. int pix0= ((const type*)src)[2*i+0];\
  999. int pix1= ((const type*)src)[2*i+1];\
  1000. int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
  1001. int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
  1002. int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
  1003. g&= maskg|(2*maskg);\
  1004. \
  1005. g>>=shg;\
  1006. \
  1007. dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
  1008. dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
  1009. }\
  1010. }
  1011. BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0xFF000000, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1012. BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0xFF000000, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1013. BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RU<<11, GU<<5, BU , RV<<11, GV<<5, BV , RGB2YUV_SHIFT+8)
  1014. BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RU<<10, GU<<5, BU , RV<<10, GV<<5, BV , RGB2YUV_SHIFT+7)
  1015. BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RU , GU<<5, BU<<11, RV , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
  1016. BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RU , GU<<5, BU<<10, RV , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
  1017. static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
  1018. {
  1019. int i;
  1020. for (i=0; i<width; i++) {
  1021. int d= src[i];
  1022. dst[i]= pal[d] & 0xFF;
  1023. }
  1024. }
  1025. static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
  1026. const uint8_t *src1, const uint8_t *src2,
  1027. long width, uint32_t *pal)
  1028. {
  1029. int i;
  1030. assert(src1 == src2);
  1031. for (i=0; i<width; i++) {
  1032. int p= pal[src1[i]];
  1033. dstU[i]= p>>8;
  1034. dstV[i]= p>>16;
  1035. }
  1036. }
  1037. static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  1038. {
  1039. int i, j;
  1040. for (i=0; i<width/8; i++) {
  1041. int d= ~src[i];
  1042. for(j=0; j<8; j++)
  1043. dst[8*i+j]= ((d>>(7-j))&1)*255;
  1044. }
  1045. }
  1046. static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  1047. {
  1048. int i, j;
  1049. for (i=0; i<width/8; i++) {
  1050. int d= src[i];
  1051. for(j=0; j<8; j++)
  1052. dst[8*i+j]= ((d>>(7-j))&1)*255;
  1053. }
  1054. }
  1055. //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
  1056. //Plain C versions
  1057. #if (!HAVE_MMX && !HAVE_ALTIVEC) || CONFIG_RUNTIME_CPUDETECT
  1058. #define COMPILE_C
  1059. #endif
  1060. #if ARCH_PPC
  1061. #if HAVE_ALTIVEC
  1062. #define COMPILE_ALTIVEC
  1063. #endif
  1064. #endif //ARCH_PPC
  1065. #if ARCH_X86
  1066. #if (HAVE_MMX && !HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT
  1067. #define COMPILE_MMX
  1068. #endif
  1069. #if HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT
  1070. #define COMPILE_MMX2
  1071. #endif
  1072. #if (HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT
  1073. #define COMPILE_3DNOW
  1074. #endif
  1075. #endif //ARCH_X86
  1076. #define COMPILE_TEMPLATE_MMX 0
  1077. #define COMPILE_TEMPLATE_MMX2 0
  1078. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1079. #define COMPILE_TEMPLATE_ALTIVEC 0
  1080. #ifdef COMPILE_C
  1081. #define RENAME(a) a ## _C
  1082. #include "swscale_template.c"
  1083. #endif
  1084. #ifdef COMPILE_ALTIVEC
  1085. #undef RENAME
  1086. #undef COMPILE_TEMPLATE_ALTIVEC
  1087. #define COMPILE_TEMPLATE_ALTIVEC 1
  1088. #define RENAME(a) a ## _altivec
  1089. #include "swscale_template.c"
  1090. #endif
  1091. #if ARCH_X86
  1092. //MMX versions
  1093. #ifdef COMPILE_MMX
  1094. #undef RENAME
  1095. #undef COMPILE_TEMPLATE_MMX
  1096. #undef COMPILE_TEMPLATE_MMX2
  1097. #undef COMPILE_TEMPLATE_AMD3DNOW
  1098. #define COMPILE_TEMPLATE_MMX 1
  1099. #define COMPILE_TEMPLATE_MMX2 0
  1100. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1101. #define RENAME(a) a ## _MMX
  1102. #include "swscale_template.c"
  1103. #endif
  1104. //MMX2 versions
  1105. #ifdef COMPILE_MMX2
  1106. #undef RENAME
  1107. #undef COMPILE_TEMPLATE_MMX
  1108. #undef COMPILE_TEMPLATE_MMX2
  1109. #undef COMPILE_TEMPLATE_AMD3DNOW
  1110. #define COMPILE_TEMPLATE_MMX 1
  1111. #define COMPILE_TEMPLATE_MMX2 1
  1112. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1113. #define RENAME(a) a ## _MMX2
  1114. #include "swscale_template.c"
  1115. #endif
  1116. //3DNOW versions
  1117. #ifdef COMPILE_3DNOW
  1118. #undef RENAME
  1119. #undef COMPILE_TEMPLATE_MMX
  1120. #undef COMPILE_TEMPLATE_MMX2
  1121. #undef COMPILE_TEMPLATE_AMD3DNOW
  1122. #define COMPILE_TEMPLATE_MMX 1
  1123. #define COMPILE_TEMPLATE_MMX2 0
  1124. #define COMPILE_TEMPLATE_AMD3DNOW 1
  1125. #define RENAME(a) a ## _3DNow
  1126. #include "swscale_template.c"
  1127. #endif
  1128. #endif //ARCH_X86
  1129. SwsFunc ff_getSwsFunc(SwsContext *c)
  1130. {
  1131. #if CONFIG_RUNTIME_CPUDETECT
  1132. int flags = c->flags;
  1133. #if ARCH_X86
  1134. // ordered per speed fastest first
  1135. if (flags & SWS_CPU_CAPS_MMX2) {
  1136. sws_init_swScale_MMX2(c);
  1137. return swScale_MMX2;
  1138. } else if (flags & SWS_CPU_CAPS_3DNOW) {
  1139. sws_init_swScale_3DNow(c);
  1140. return swScale_3DNow;
  1141. } else if (flags & SWS_CPU_CAPS_MMX) {
  1142. sws_init_swScale_MMX(c);
  1143. return swScale_MMX;
  1144. } else {
  1145. sws_init_swScale_C(c);
  1146. return swScale_C;
  1147. }
  1148. #else
  1149. #ifdef COMPILE_ALTIVEC
  1150. if (flags & SWS_CPU_CAPS_ALTIVEC) {
  1151. sws_init_swScale_altivec(c);
  1152. return swScale_altivec;
  1153. } else {
  1154. sws_init_swScale_C(c);
  1155. return swScale_C;
  1156. }
  1157. #endif
  1158. sws_init_swScale_C(c);
  1159. return swScale_C;
  1160. #endif /* ARCH_X86 */
  1161. #else //CONFIG_RUNTIME_CPUDETECT
  1162. #if COMPILE_TEMPLATE_MMX2
  1163. sws_init_swScale_MMX2(c);
  1164. return swScale_MMX2;
  1165. #elif COMPILE_TEMPLATE_AMD3DNOW
  1166. sws_init_swScale_3DNow(c);
  1167. return swScale_3DNow;
  1168. #elif COMPILE_TEMPLATE_MMX
  1169. sws_init_swScale_MMX(c);
  1170. return swScale_MMX;
  1171. #elif COMPILE_TEMPLATE_ALTIVEC
  1172. sws_init_swScale_altivec(c);
  1173. return swScale_altivec;
  1174. #else
  1175. sws_init_swScale_C(c);
  1176. return swScale_C;
  1177. #endif
  1178. #endif //!CONFIG_RUNTIME_CPUDETECT
  1179. }
  1180. static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1181. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1182. {
  1183. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1184. /* Copy Y plane */
  1185. if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
  1186. memcpy(dst, src[0], srcSliceH*dstStride[0]);
  1187. else {
  1188. int i;
  1189. const uint8_t *srcPtr= src[0];
  1190. uint8_t *dstPtr= dst;
  1191. for (i=0; i<srcSliceH; i++) {
  1192. memcpy(dstPtr, srcPtr, c->srcW);
  1193. srcPtr+= srcStride[0];
  1194. dstPtr+= dstStride[0];
  1195. }
  1196. }
  1197. dst = dstParam[1] + dstStride[1]*srcSliceY/2;
  1198. if (c->dstFormat == PIX_FMT_NV12)
  1199. interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
  1200. else
  1201. interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
  1202. return srcSliceH;
  1203. }
  1204. static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1205. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1206. {
  1207. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1208. yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
  1209. return srcSliceH;
  1210. }
  1211. static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1212. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1213. {
  1214. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1215. yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
  1216. return srcSliceH;
  1217. }
  1218. static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1219. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1220. {
  1221. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1222. yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
  1223. return srcSliceH;
  1224. }
  1225. static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1226. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1227. {
  1228. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1229. yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
  1230. return srcSliceH;
  1231. }
  1232. static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1233. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1234. {
  1235. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1236. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
  1237. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
  1238. yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1239. if (dstParam[3])
  1240. fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1241. return srcSliceH;
  1242. }
  1243. static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1244. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1245. {
  1246. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1247. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
  1248. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
  1249. yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1250. return srcSliceH;
  1251. }
  1252. static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1253. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1254. {
  1255. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1256. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
  1257. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
  1258. uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1259. if (dstParam[3])
  1260. fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1261. return srcSliceH;
  1262. }
  1263. static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1264. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1265. {
  1266. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1267. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
  1268. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
  1269. uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1270. return srcSliceH;
  1271. }
  1272. static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1273. int srcSliceH, uint8_t* dst[], int dstStride[])
  1274. {
  1275. const enum PixelFormat srcFormat= c->srcFormat;
  1276. const enum PixelFormat dstFormat= c->dstFormat;
  1277. void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
  1278. const uint8_t *palette)=NULL;
  1279. int i;
  1280. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1281. const uint8_t *srcPtr= src[0];
  1282. if (usePal(srcFormat)) {
  1283. switch (dstFormat) {
  1284. case PIX_FMT_RGB32 : conv = sws_convertPalette8ToPacked32; break;
  1285. case PIX_FMT_BGR32 : conv = sws_convertPalette8ToPacked32; break;
  1286. case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
  1287. case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
  1288. case PIX_FMT_RGB24 : conv = sws_convertPalette8ToPacked24; break;
  1289. case PIX_FMT_BGR24 : conv = sws_convertPalette8ToPacked24; break;
  1290. }
  1291. }
  1292. if (!conv)
  1293. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1294. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1295. else {
  1296. for (i=0; i<srcSliceH; i++) {
  1297. conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
  1298. srcPtr+= srcStride[0];
  1299. dstPtr+= dstStride[0];
  1300. }
  1301. }
  1302. return srcSliceH;
  1303. }
  1304. #define isRGBA32(x) ( \
  1305. (x) == PIX_FMT_ARGB \
  1306. || (x) == PIX_FMT_RGBA \
  1307. || (x) == PIX_FMT_BGRA \
  1308. || (x) == PIX_FMT_ABGR \
  1309. )
  1310. /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
  1311. static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1312. int srcSliceH, uint8_t* dst[], int dstStride[])
  1313. {
  1314. const enum PixelFormat srcFormat= c->srcFormat;
  1315. const enum PixelFormat dstFormat= c->dstFormat;
  1316. const int srcBpp= (c->srcFormatBpp + 7) >> 3;
  1317. const int dstBpp= (c->dstFormatBpp + 7) >> 3;
  1318. const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
  1319. const int dstId= c->dstFormatBpp >> 2;
  1320. void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
  1321. #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
  1322. if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
  1323. if ( CONV_IS(ABGR, RGBA)
  1324. || CONV_IS(ARGB, BGRA)
  1325. || CONV_IS(BGRA, ARGB)
  1326. || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
  1327. else if (CONV_IS(ABGR, ARGB)
  1328. || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
  1329. else if (CONV_IS(ABGR, BGRA)
  1330. || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
  1331. else if (CONV_IS(BGRA, RGBA)
  1332. || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
  1333. else if (CONV_IS(BGRA, ABGR)
  1334. || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
  1335. } else
  1336. /* BGR -> BGR */
  1337. if ( (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
  1338. || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
  1339. switch(srcId | (dstId<<4)) {
  1340. case 0x34: conv= rgb16to15; break;
  1341. case 0x36: conv= rgb24to15; break;
  1342. case 0x38: conv= rgb32to15; break;
  1343. case 0x43: conv= rgb15to16; break;
  1344. case 0x46: conv= rgb24to16; break;
  1345. case 0x48: conv= rgb32to16; break;
  1346. case 0x63: conv= rgb15to24; break;
  1347. case 0x64: conv= rgb16to24; break;
  1348. case 0x68: conv= rgb32to24; break;
  1349. case 0x83: conv= rgb15to32; break;
  1350. case 0x84: conv= rgb16to32; break;
  1351. case 0x86: conv= rgb24to32; break;
  1352. }
  1353. } else if ( (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
  1354. || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
  1355. switch(srcId | (dstId<<4)) {
  1356. case 0x33: conv= rgb15tobgr15; break;
  1357. case 0x34: conv= rgb16tobgr15; break;
  1358. case 0x36: conv= rgb24tobgr15; break;
  1359. case 0x38: conv= rgb32tobgr15; break;
  1360. case 0x43: conv= rgb15tobgr16; break;
  1361. case 0x44: conv= rgb16tobgr16; break;
  1362. case 0x46: conv= rgb24tobgr16; break;
  1363. case 0x48: conv= rgb32tobgr16; break;
  1364. case 0x63: conv= rgb15tobgr24; break;
  1365. case 0x64: conv= rgb16tobgr24; break;
  1366. case 0x66: conv= rgb24tobgr24; break;
  1367. case 0x68: conv= rgb32tobgr24; break;
  1368. case 0x83: conv= rgb15tobgr32; break;
  1369. case 0x84: conv= rgb16tobgr32; break;
  1370. case 0x86: conv= rgb24tobgr32; break;
  1371. }
  1372. }
  1373. if (!conv) {
  1374. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1375. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1376. } else {
  1377. const uint8_t *srcPtr= src[0];
  1378. uint8_t *dstPtr= dst[0];
  1379. if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
  1380. srcPtr += ALT32_CORR;
  1381. if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
  1382. dstPtr += ALT32_CORR;
  1383. if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
  1384. conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
  1385. else {
  1386. int i;
  1387. dstPtr += dstStride[0]*srcSliceY;
  1388. for (i=0; i<srcSliceH; i++) {
  1389. conv(srcPtr, dstPtr, c->srcW*srcBpp);
  1390. srcPtr+= srcStride[0];
  1391. dstPtr+= dstStride[0];
  1392. }
  1393. }
  1394. }
  1395. return srcSliceH;
  1396. }
  1397. static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1398. int srcSliceH, uint8_t* dst[], int dstStride[])
  1399. {
  1400. rgb24toyv12(
  1401. src[0],
  1402. dst[0]+ srcSliceY *dstStride[0],
  1403. dst[1]+(srcSliceY>>1)*dstStride[1],
  1404. dst[2]+(srcSliceY>>1)*dstStride[2],
  1405. c->srcW, srcSliceH,
  1406. dstStride[0], dstStride[1], srcStride[0]);
  1407. if (dst[3])
  1408. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1409. return srcSliceH;
  1410. }
  1411. static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1412. int srcSliceH, uint8_t* dst[], int dstStride[])
  1413. {
  1414. int i;
  1415. /* copy Y */
  1416. if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
  1417. memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
  1418. else {
  1419. const uint8_t *srcPtr= src[0];
  1420. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1421. for (i=0; i<srcSliceH; i++) {
  1422. memcpy(dstPtr, srcPtr, c->srcW);
  1423. srcPtr+= srcStride[0];
  1424. dstPtr+= dstStride[0];
  1425. }
  1426. }
  1427. if (c->dstFormat==PIX_FMT_YUV420P || c->dstFormat==PIX_FMT_YUVA420P) {
  1428. planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
  1429. srcSliceH >> 2, srcStride[1], dstStride[1]);
  1430. planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
  1431. srcSliceH >> 2, srcStride[2], dstStride[2]);
  1432. } else {
  1433. planar2x(src[1], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
  1434. srcSliceH >> 2, srcStride[1], dstStride[2]);
  1435. planar2x(src[2], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
  1436. srcSliceH >> 2, srcStride[2], dstStride[1]);
  1437. }
  1438. if (dst[3])
  1439. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1440. return srcSliceH;
  1441. }
  1442. /* unscaled copy like stuff (assumes nearly identical formats) */
  1443. static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1444. int srcSliceH, uint8_t* dst[], int dstStride[])
  1445. {
  1446. if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
  1447. memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
  1448. else {
  1449. int i;
  1450. const uint8_t *srcPtr= src[0];
  1451. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1452. int length=0;
  1453. /* universal length finder */
  1454. while(length+c->srcW <= FFABS(dstStride[0])
  1455. && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
  1456. assert(length!=0);
  1457. for (i=0; i<srcSliceH; i++) {
  1458. memcpy(dstPtr, srcPtr, length);
  1459. srcPtr+= srcStride[0];
  1460. dstPtr+= dstStride[0];
  1461. }
  1462. }
  1463. return srcSliceH;
  1464. }
  1465. static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1466. int srcSliceH, uint8_t* dst[], int dstStride[])
  1467. {
  1468. int plane, i, j;
  1469. for (plane=0; plane<4; plane++) {
  1470. int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
  1471. int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
  1472. int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
  1473. const uint8_t *srcPtr= src[plane];
  1474. uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
  1475. if (!dst[plane]) continue;
  1476. // ignore palette for GRAY8
  1477. if (plane == 1 && !dst[2]) continue;
  1478. if (!src[plane] || (plane == 1 && !src[2])) {
  1479. if(is16BPS(c->dstFormat))
  1480. length*=2;
  1481. fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
  1482. } else {
  1483. if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
  1484. if (!isBE(c->srcFormat)) srcPtr++;
  1485. for (i=0; i<height; i++) {
  1486. for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
  1487. srcPtr+= srcStride[plane];
  1488. dstPtr+= dstStride[plane];
  1489. }
  1490. } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
  1491. for (i=0; i<height; i++) {
  1492. for (j=0; j<length; j++) {
  1493. dstPtr[ j<<1 ] = srcPtr[j];
  1494. dstPtr[(j<<1)+1] = srcPtr[j];
  1495. }
  1496. srcPtr+= srcStride[plane];
  1497. dstPtr+= dstStride[plane];
  1498. }
  1499. } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
  1500. && isBE(c->srcFormat) != isBE(c->dstFormat)) {
  1501. for (i=0; i<height; i++) {
  1502. for (j=0; j<length; j++)
  1503. ((uint16_t*)dstPtr)[j] = bswap_16(((const uint16_t*)srcPtr)[j]);
  1504. srcPtr+= srcStride[plane];
  1505. dstPtr+= dstStride[plane];
  1506. }
  1507. } else if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
  1508. memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
  1509. else {
  1510. if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
  1511. length*=2;
  1512. for (i=0; i<height; i++) {
  1513. memcpy(dstPtr, srcPtr, length);
  1514. srcPtr+= srcStride[plane];
  1515. dstPtr+= dstStride[plane];
  1516. }
  1517. }
  1518. }
  1519. }
  1520. return srcSliceH;
  1521. }
  1522. int ff_hardcodedcpuflags(void)
  1523. {
  1524. int flags = 0;
  1525. #if COMPILE_TEMPLATE_MMX2
  1526. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
  1527. #elif COMPILE_TEMPLATE_AMD3DNOW
  1528. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
  1529. #elif COMPILE_TEMPLATE_MMX
  1530. flags |= SWS_CPU_CAPS_MMX;
  1531. #elif COMPILE_TEMPLATE_ALTIVEC
  1532. flags |= SWS_CPU_CAPS_ALTIVEC;
  1533. #elif ARCH_BFIN
  1534. flags |= SWS_CPU_CAPS_BFIN;
  1535. #endif
  1536. return flags;
  1537. }
  1538. void ff_get_unscaled_swscale(SwsContext *c)
  1539. {
  1540. const enum PixelFormat srcFormat = c->srcFormat;
  1541. const enum PixelFormat dstFormat = c->dstFormat;
  1542. const int flags = c->flags;
  1543. const int dstH = c->dstH;
  1544. int needsDither;
  1545. needsDither= isAnyRGB(dstFormat)
  1546. && c->dstFormatBpp < 24
  1547. && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
  1548. /* yv12_to_nv12 */
  1549. if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
  1550. c->swScale= planarToNv12Wrapper;
  1551. }
  1552. /* yuv2bgr */
  1553. if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
  1554. && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
  1555. c->swScale= ff_yuv2rgb_get_func_ptr(c);
  1556. }
  1557. if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
  1558. c->swScale= yvu9ToYv12Wrapper;
  1559. }
  1560. /* bgr24toYV12 */
  1561. if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
  1562. c->swScale= bgr24ToYv12Wrapper;
  1563. /* RGB/BGR -> RGB/BGR (no dither needed forms) */
  1564. if ( isAnyRGB(srcFormat)
  1565. && isAnyRGB(dstFormat)
  1566. && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
  1567. && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
  1568. && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
  1569. && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
  1570. && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
  1571. && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
  1572. && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
  1573. && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
  1574. && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
  1575. && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
  1576. && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
  1577. c->swScale= rgbToRgbWrapper;
  1578. if ((usePal(srcFormat) && (
  1579. dstFormat == PIX_FMT_RGB32 ||
  1580. dstFormat == PIX_FMT_RGB32_1 ||
  1581. dstFormat == PIX_FMT_RGB24 ||
  1582. dstFormat == PIX_FMT_BGR32 ||
  1583. dstFormat == PIX_FMT_BGR32_1 ||
  1584. dstFormat == PIX_FMT_BGR24)))
  1585. c->swScale= palToRgbWrapper;
  1586. if (srcFormat == PIX_FMT_YUV422P) {
  1587. if (dstFormat == PIX_FMT_YUYV422)
  1588. c->swScale= yuv422pToYuy2Wrapper;
  1589. else if (dstFormat == PIX_FMT_UYVY422)
  1590. c->swScale= yuv422pToUyvyWrapper;
  1591. }
  1592. /* LQ converters if -sws 0 or -sws 4*/
  1593. if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
  1594. /* yv12_to_yuy2 */
  1595. if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
  1596. if (dstFormat == PIX_FMT_YUYV422)
  1597. c->swScale= planarToYuy2Wrapper;
  1598. else if (dstFormat == PIX_FMT_UYVY422)
  1599. c->swScale= planarToUyvyWrapper;
  1600. }
  1601. }
  1602. if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  1603. c->swScale= yuyvToYuv420Wrapper;
  1604. if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  1605. c->swScale= uyvyToYuv420Wrapper;
  1606. if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
  1607. c->swScale= yuyvToYuv422Wrapper;
  1608. if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
  1609. c->swScale= uyvyToYuv422Wrapper;
  1610. #ifdef COMPILE_ALTIVEC
  1611. if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
  1612. !(c->flags & SWS_BITEXACT) &&
  1613. srcFormat == PIX_FMT_YUV420P) {
  1614. // unscaled YV12 -> packed YUV, we want speed
  1615. if (dstFormat == PIX_FMT_YUYV422)
  1616. c->swScale= yv12toyuy2_unscaled_altivec;
  1617. else if (dstFormat == PIX_FMT_UYVY422)
  1618. c->swScale= yv12touyvy_unscaled_altivec;
  1619. }
  1620. #endif
  1621. /* simple copy */
  1622. if ( srcFormat == dstFormat
  1623. || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
  1624. || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
  1625. || (isPlanarYUV(srcFormat) && isGray(dstFormat))
  1626. || (isPlanarYUV(dstFormat) && isGray(srcFormat))
  1627. || (isGray(dstFormat) && isGray(srcFormat))
  1628. || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
  1629. && c->chrDstHSubSample == c->chrSrcHSubSample
  1630. && c->chrDstVSubSample == c->chrSrcVSubSample
  1631. && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
  1632. && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
  1633. {
  1634. if (isPacked(c->srcFormat))
  1635. c->swScale= packedCopyWrapper;
  1636. else /* Planar YUV or gray */
  1637. c->swScale= planarCopyWrapper;
  1638. }
  1639. #if ARCH_BFIN
  1640. if (flags & SWS_CPU_CAPS_BFIN)
  1641. ff_bfin_get_unscaled_swscale (c);
  1642. #endif
  1643. }
  1644. static void reset_ptr(const uint8_t* src[], int format)
  1645. {
  1646. if(!isALPHA(format))
  1647. src[3]=NULL;
  1648. if(!isPlanarYUV(format)) {
  1649. src[3]=src[2]=NULL;
  1650. if (!usePal(format))
  1651. src[1]= NULL;
  1652. }
  1653. }
  1654. /**
  1655. * swscale wrapper, so we don't need to export the SwsContext.
  1656. * Assumes planar YUV to be in YUV order instead of YVU.
  1657. */
  1658. int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
  1659. int srcSliceH, uint8_t* const dst[], const int dstStride[])
  1660. {
  1661. int i;
  1662. const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
  1663. uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
  1664. // do not mess up sliceDir if we have a "trailing" 0-size slice
  1665. if (srcSliceH == 0)
  1666. return 0;
  1667. if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
  1668. av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
  1669. return 0;
  1670. }
  1671. if (c->sliceDir == 0) {
  1672. if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
  1673. }
  1674. if (usePal(c->srcFormat)) {
  1675. for (i=0; i<256; i++) {
  1676. int p, r, g, b,y,u,v;
  1677. if(c->srcFormat == PIX_FMT_PAL8) {
  1678. p=((const uint32_t*)(src[1]))[i];
  1679. r= (p>>16)&0xFF;
  1680. g= (p>> 8)&0xFF;
  1681. b= p &0xFF;
  1682. } else if(c->srcFormat == PIX_FMT_RGB8) {
  1683. r= (i>>5 )*36;
  1684. g= ((i>>2)&7)*36;
  1685. b= (i&3 )*85;
  1686. } else if(c->srcFormat == PIX_FMT_BGR8) {
  1687. b= (i>>6 )*85;
  1688. g= ((i>>3)&7)*36;
  1689. r= (i&7 )*36;
  1690. } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
  1691. r= (i>>3 )*255;
  1692. g= ((i>>1)&3)*85;
  1693. b= (i&1 )*255;
  1694. } else if(c->srcFormat == PIX_FMT_GRAY8) {
  1695. r = g = b = i;
  1696. } else {
  1697. assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
  1698. b= (i>>3 )*255;
  1699. g= ((i>>1)&3)*85;
  1700. r= (i&1 )*255;
  1701. }
  1702. y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1703. u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1704. v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1705. c->pal_yuv[i]= y + (u<<8) + (v<<16);
  1706. switch(c->dstFormat) {
  1707. case PIX_FMT_BGR32:
  1708. #if !HAVE_BIGENDIAN
  1709. case PIX_FMT_RGB24:
  1710. #endif
  1711. c->pal_rgb[i]= r + (g<<8) + (b<<16);
  1712. break;
  1713. case PIX_FMT_BGR32_1:
  1714. #if HAVE_BIGENDIAN
  1715. case PIX_FMT_BGR24:
  1716. #endif
  1717. c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
  1718. break;
  1719. case PIX_FMT_RGB32_1:
  1720. #if HAVE_BIGENDIAN
  1721. case PIX_FMT_RGB24:
  1722. #endif
  1723. c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
  1724. break;
  1725. case PIX_FMT_RGB32:
  1726. #if !HAVE_BIGENDIAN
  1727. case PIX_FMT_BGR24:
  1728. #endif
  1729. default:
  1730. c->pal_rgb[i]= b + (g<<8) + (r<<16);
  1731. }
  1732. }
  1733. }
  1734. // copy strides, so they can safely be modified
  1735. if (c->sliceDir == 1) {
  1736. // slices go from top to bottom
  1737. int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
  1738. int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
  1739. reset_ptr(src2, c->srcFormat);
  1740. reset_ptr((const uint8_t**)dst2, c->dstFormat);
  1741. /* reset slice direction at end of frame */
  1742. if (srcSliceY + srcSliceH == c->srcH)
  1743. c->sliceDir = 0;
  1744. return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
  1745. } else {
  1746. // slices go from bottom to top => we flip the image internally
  1747. int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
  1748. int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
  1749. src2[0] += (srcSliceH-1)*srcStride[0];
  1750. if (!usePal(c->srcFormat))
  1751. src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
  1752. src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
  1753. src2[3] += (srcSliceH-1)*srcStride[3];
  1754. dst2[0] += ( c->dstH -1)*dstStride[0];
  1755. dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
  1756. dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
  1757. dst2[3] += ( c->dstH -1)*dstStride[3];
  1758. reset_ptr(src2, c->srcFormat);
  1759. reset_ptr((const uint8_t**)dst2, c->dstFormat);
  1760. /* reset slice direction at end of frame */
  1761. if (!srcSliceY)
  1762. c->sliceDir = 0;
  1763. return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
  1764. }
  1765. }
  1766. #if LIBSWSCALE_VERSION_MAJOR < 1
  1767. int sws_scale_ordered(SwsContext *c, const uint8_t* const src[], int srcStride[], int srcSliceY,
  1768. int srcSliceH, uint8_t* dst[], int dstStride[])
  1769. {
  1770. return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
  1771. }
  1772. #endif
  1773. /* Convert the palette to the same packed 32-bit format as the palette */
  1774. void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1775. {
  1776. long i;
  1777. for (i=0; i<num_pixels; i++)
  1778. ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
  1779. }
  1780. /* Palette format: ABCD -> dst format: ABC */
  1781. void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1782. {
  1783. long i;
  1784. for (i=0; i<num_pixels; i++) {
  1785. //FIXME slow?
  1786. dst[0]= palette[src[i]*4+0];
  1787. dst[1]= palette[src[i]*4+1];
  1788. dst[2]= palette[src[i]*4+2];
  1789. dst+= 3;
  1790. }
  1791. }