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.

2317 lines
83KB

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