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.

2343 lines
84KB

  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, int output_bits)
  335. {
  336. //FIXME Optimize (just quickly written not optimized..)
  337. int i;
  338. int shift = 11 + 16 - output_bits;
  339. #define output_pixel(pos, val) \
  340. if (big_endian) { \
  341. if (output_bits == 16) { \
  342. AV_WB16(pos, av_clip_uint16(val >> shift)); \
  343. } else { \
  344. AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  345. } \
  346. } else { \
  347. if (output_bits == 16) { \
  348. AV_WL16(pos, av_clip_uint16(val >> shift)); \
  349. } else { \
  350. AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  351. } \
  352. }
  353. for (i = 0; i < dstW; i++) {
  354. int val = 1 << (26-output_bits);
  355. int j;
  356. for (j = 0; j < lumFilterSize; j++)
  357. val += lumSrc[j][i] * lumFilter[j];
  358. output_pixel(&dest[i], val);
  359. }
  360. if (uDest) {
  361. for (i = 0; i < chrDstW; i++) {
  362. int u = 1 << (26-output_bits);
  363. int v = 1 << (26-output_bits);
  364. int j;
  365. for (j = 0; j < chrFilterSize; j++) {
  366. u += chrSrc[j][i ] * chrFilter[j];
  367. v += chrSrc[j][i + VOFW] * chrFilter[j];
  368. }
  369. output_pixel(&uDest[i], u);
  370. output_pixel(&vDest[i], v);
  371. }
  372. }
  373. if (CONFIG_SWSCALE_ALPHA && aDest) {
  374. for (i = 0; i < dstW; i++) {
  375. int val = 1 << (26-output_bits);
  376. int j;
  377. for (j = 0; j < lumFilterSize; j++)
  378. val += alpSrc[j][i] * lumFilter[j];
  379. output_pixel(&aDest[i], val);
  380. }
  381. }
  382. }
  383. static av_always_inline void yuv2yuvXNinC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  384. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  385. const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
  386. int dstW, int chrDstW, int big_endian, int depth)
  387. {
  388. //FIXME Optimize (just quickly written not optimized..)
  389. int i;
  390. for (i = 0; i < dstW; i++) {
  391. int val = 1 << (26-depth);
  392. int j;
  393. for (j = 0; j < lumFilterSize; j++)
  394. val += lumSrc[j][i] * lumFilter[j];
  395. if (big_endian) {
  396. AV_WB16(&dest[i], av_clip(val >> (27-depth), 0, (1<<depth)-1));
  397. } else {
  398. AV_WL16(&dest[i], av_clip(val >> (27-depth), 0, (1<<depth)-1));
  399. }
  400. }
  401. if (uDest) {
  402. for (i = 0; i < chrDstW; i++) {
  403. int u = 1 << (26-depth);
  404. int v = 1 << (26-depth);
  405. int j;
  406. for (j = 0; j < chrFilterSize; j++) {
  407. u += chrSrc[j][i ] * chrFilter[j];
  408. v += chrSrc[j][i + VOFW] * chrFilter[j];
  409. }
  410. if (big_endian) {
  411. AV_WB16(&uDest[i], av_clip(u >> (27-depth), 0, (1<<depth)-1));
  412. AV_WB16(&vDest[i], av_clip(v >> (27-depth), 0, (1<<depth)-1));
  413. } else {
  414. AV_WL16(&uDest[i], av_clip(u >> (27-depth), 0, (1<<depth)-1));
  415. AV_WL16(&vDest[i], av_clip(v >> (27-depth), 0, (1<<depth)-1));
  416. }
  417. }
  418. }
  419. }
  420. static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  421. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  422. const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
  423. enum PixelFormat dstFormat)
  424. {
  425. if (isNBPS(dstFormat)) {
  426. const int depth = av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1+1;
  427. yuv2yuvXNinC_template(lumFilter, lumSrc, lumFilterSize,
  428. chrFilter, chrSrc, chrFilterSize,
  429. alpSrc,
  430. dest, uDest, vDest, aDest,
  431. dstW, chrDstW, isBE(dstFormat), depth);
  432. } else {
  433. if (isBE(dstFormat)) {
  434. yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
  435. chrFilter, chrSrc, chrFilterSize,
  436. alpSrc,
  437. dest, uDest, vDest, aDest,
  438. dstW, chrDstW, 1, 16);
  439. } else {
  440. yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
  441. chrFilter, chrSrc, chrFilterSize,
  442. alpSrc,
  443. dest, uDest, vDest, aDest,
  444. dstW, chrDstW, 0, 16);
  445. }
  446. }
  447. }
  448. static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  449. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  450. const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
  451. {
  452. //FIXME Optimize (just quickly written not optimized..)
  453. int i;
  454. for (i=0; i<dstW; i++) {
  455. int val=1<<18;
  456. int j;
  457. for (j=0; j<lumFilterSize; j++)
  458. val += lumSrc[j][i] * lumFilter[j];
  459. dest[i]= av_clip_uint8(val>>19);
  460. }
  461. if (uDest)
  462. for (i=0; i<chrDstW; i++) {
  463. int u=1<<18;
  464. int v=1<<18;
  465. int j;
  466. for (j=0; j<chrFilterSize; j++) {
  467. u += chrSrc[j][i] * chrFilter[j];
  468. v += chrSrc[j][i + VOFW] * chrFilter[j];
  469. }
  470. uDest[i]= av_clip_uint8(u>>19);
  471. vDest[i]= av_clip_uint8(v>>19);
  472. }
  473. if (CONFIG_SWSCALE_ALPHA && aDest)
  474. for (i=0; i<dstW; i++) {
  475. int val=1<<18;
  476. int j;
  477. for (j=0; j<lumFilterSize; j++)
  478. val += alpSrc[j][i] * lumFilter[j];
  479. aDest[i]= av_clip_uint8(val>>19);
  480. }
  481. }
  482. static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  483. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  484. uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
  485. {
  486. //FIXME Optimize (just quickly written not optimized..)
  487. int i;
  488. for (i=0; i<dstW; i++) {
  489. int val=1<<18;
  490. int j;
  491. for (j=0; j<lumFilterSize; j++)
  492. val += lumSrc[j][i] * lumFilter[j];
  493. dest[i]= av_clip_uint8(val>>19);
  494. }
  495. if (!uDest)
  496. return;
  497. if (dstFormat == PIX_FMT_NV12)
  498. for (i=0; i<chrDstW; i++) {
  499. int u=1<<18;
  500. int v=1<<18;
  501. int j;
  502. for (j=0; j<chrFilterSize; j++) {
  503. u += chrSrc[j][i] * chrFilter[j];
  504. v += chrSrc[j][i + VOFW] * chrFilter[j];
  505. }
  506. uDest[2*i]= av_clip_uint8(u>>19);
  507. uDest[2*i+1]= av_clip_uint8(v>>19);
  508. }
  509. else
  510. for (i=0; i<chrDstW; i++) {
  511. int u=1<<18;
  512. int v=1<<18;
  513. int j;
  514. for (j=0; j<chrFilterSize; j++) {
  515. u += chrSrc[j][i] * chrFilter[j];
  516. v += chrSrc[j][i + VOFW] * chrFilter[j];
  517. }
  518. uDest[2*i]= av_clip_uint8(v>>19);
  519. uDest[2*i+1]= av_clip_uint8(u>>19);
  520. }
  521. }
  522. #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
  523. for (i=0; i<(dstW>>1); i++) {\
  524. int j;\
  525. int Y1 = 1<<18;\
  526. int Y2 = 1<<18;\
  527. int U = 1<<18;\
  528. int V = 1<<18;\
  529. int av_unused A1, A2;\
  530. type av_unused *r, *b, *g;\
  531. const int i2= 2*i;\
  532. \
  533. for (j=0; j<lumFilterSize; j++) {\
  534. Y1 += lumSrc[j][i2] * lumFilter[j];\
  535. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  536. }\
  537. for (j=0; j<chrFilterSize; j++) {\
  538. U += chrSrc[j][i] * chrFilter[j];\
  539. V += chrSrc[j][i+VOFW] * chrFilter[j];\
  540. }\
  541. Y1>>=19;\
  542. Y2>>=19;\
  543. U >>=19;\
  544. V >>=19;\
  545. if (alpha) {\
  546. A1 = 1<<18;\
  547. A2 = 1<<18;\
  548. for (j=0; j<lumFilterSize; j++) {\
  549. A1 += alpSrc[j][i2 ] * lumFilter[j];\
  550. A2 += alpSrc[j][i2+1] * lumFilter[j];\
  551. }\
  552. A1>>=19;\
  553. A2>>=19;\
  554. }
  555. #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
  556. YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
  557. if ((Y1|Y2|U|V)&256) {\
  558. if (Y1>255) Y1=255; \
  559. else if (Y1<0)Y1=0; \
  560. if (Y2>255) Y2=255; \
  561. else if (Y2<0)Y2=0; \
  562. if (U>255) U=255; \
  563. else if (U<0) U=0; \
  564. if (V>255) V=255; \
  565. else if (V<0) V=0; \
  566. }\
  567. if (alpha && ((A1|A2)&256)) {\
  568. A1=av_clip_uint8(A1);\
  569. A2=av_clip_uint8(A2);\
  570. }
  571. #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
  572. for (i=0; i<dstW; i++) {\
  573. int j;\
  574. int Y = 0;\
  575. int U = -128<<19;\
  576. int V = -128<<19;\
  577. int av_unused A;\
  578. int R,G,B;\
  579. \
  580. for (j=0; j<lumFilterSize; j++) {\
  581. Y += lumSrc[j][i ] * lumFilter[j];\
  582. }\
  583. for (j=0; j<chrFilterSize; j++) {\
  584. U += chrSrc[j][i ] * chrFilter[j];\
  585. V += chrSrc[j][i+VOFW] * chrFilter[j];\
  586. }\
  587. Y >>=10;\
  588. U >>=10;\
  589. V >>=10;\
  590. if (alpha) {\
  591. A = rnd;\
  592. for (j=0; j<lumFilterSize; j++)\
  593. A += alpSrc[j][i ] * lumFilter[j];\
  594. A >>=19;\
  595. if (A&256)\
  596. A = av_clip_uint8(A);\
  597. }
  598. #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
  599. YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
  600. Y-= c->yuv2rgb_y_offset;\
  601. Y*= c->yuv2rgb_y_coeff;\
  602. Y+= rnd;\
  603. R= Y + V*c->yuv2rgb_v2r_coeff;\
  604. G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
  605. B= Y + U*c->yuv2rgb_u2b_coeff;\
  606. if ((R|G|B)&(0xC0000000)) {\
  607. if (R>=(256<<22)) R=(256<<22)-1; \
  608. else if (R<0)R=0; \
  609. if (G>=(256<<22)) G=(256<<22)-1; \
  610. else if (G<0)G=0; \
  611. if (B>=(256<<22)) B=(256<<22)-1; \
  612. else if (B<0)B=0; \
  613. }
  614. #define YSCALE_YUV_2_GRAY16_C \
  615. for (i=0; i<(dstW>>1); i++) {\
  616. int j;\
  617. int Y1 = 1<<18;\
  618. int Y2 = 1<<18;\
  619. int U = 1<<18;\
  620. int V = 1<<18;\
  621. \
  622. const int i2= 2*i;\
  623. \
  624. for (j=0; j<lumFilterSize; j++) {\
  625. Y1 += lumSrc[j][i2] * lumFilter[j];\
  626. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  627. }\
  628. Y1>>=11;\
  629. Y2>>=11;\
  630. if ((Y1|Y2|U|V)&65536) {\
  631. if (Y1>65535) Y1=65535; \
  632. else if (Y1<0)Y1=0; \
  633. if (Y2>65535) Y2=65535; \
  634. else if (Y2<0)Y2=0; \
  635. }
  636. #define YSCALE_YUV_2_RGBX_C(type,alpha) \
  637. YSCALE_YUV_2_PACKEDX_C(type,alpha) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
  638. r = (type *)c->table_rV[V]; \
  639. g = (type *)(c->table_gU[U] + c->table_gV[V]); \
  640. b = (type *)c->table_bU[U];
  641. #define YSCALE_YUV_2_PACKED2_C(type,alpha) \
  642. for (i=0; i<(dstW>>1); i++) { \
  643. const int i2= 2*i; \
  644. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
  645. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
  646. int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
  647. int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
  648. type av_unused *r, *b, *g; \
  649. int av_unused A1, A2; \
  650. if (alpha) {\
  651. A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
  652. A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
  653. }
  654. #define YSCALE_YUV_2_GRAY16_2_C \
  655. for (i=0; i<(dstW>>1); i++) { \
  656. const int i2= 2*i; \
  657. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
  658. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;
  659. #define YSCALE_YUV_2_RGB2_C(type,alpha) \
  660. YSCALE_YUV_2_PACKED2_C(type,alpha)\
  661. r = (type *)c->table_rV[V];\
  662. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  663. b = (type *)c->table_bU[U];
  664. #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
  665. for (i=0; i<(dstW>>1); i++) {\
  666. const int i2= 2*i;\
  667. int Y1= buf0[i2 ]>>7;\
  668. int Y2= buf0[i2+1]>>7;\
  669. int U= (uvbuf1[i ])>>7;\
  670. int V= (uvbuf1[i+VOFW])>>7;\
  671. type av_unused *r, *b, *g;\
  672. int av_unused A1, A2;\
  673. if (alpha) {\
  674. A1= abuf0[i2 ]>>7;\
  675. A2= abuf0[i2+1]>>7;\
  676. }
  677. #define YSCALE_YUV_2_GRAY16_1_C \
  678. for (i=0; i<(dstW>>1); i++) {\
  679. const int i2= 2*i;\
  680. int Y1= buf0[i2 ]<<1;\
  681. int Y2= buf0[i2+1]<<1;
  682. #define YSCALE_YUV_2_RGB1_C(type,alpha) \
  683. YSCALE_YUV_2_PACKED1_C(type,alpha)\
  684. r = (type *)c->table_rV[V];\
  685. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  686. b = (type *)c->table_bU[U];
  687. #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
  688. for (i=0; i<(dstW>>1); i++) {\
  689. const int i2= 2*i;\
  690. int Y1= buf0[i2 ]>>7;\
  691. int Y2= buf0[i2+1]>>7;\
  692. int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
  693. int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
  694. type av_unused *r, *b, *g;\
  695. int av_unused A1, A2;\
  696. if (alpha) {\
  697. A1= abuf0[i2 ]>>7;\
  698. A2= abuf0[i2+1]>>7;\
  699. }
  700. #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
  701. YSCALE_YUV_2_PACKED1B_C(type,alpha)\
  702. r = (type *)c->table_rV[V];\
  703. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  704. b = (type *)c->table_bU[U];
  705. #define YSCALE_YUV_2_MONO2_C \
  706. const uint8_t * const d128=dither_8x8_220[y&7];\
  707. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  708. for (i=0; i<dstW-7; i+=8) {\
  709. int acc;\
  710. acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
  711. acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
  712. acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
  713. acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
  714. acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
  715. acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
  716. acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
  717. acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
  718. ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
  719. dest++;\
  720. }
  721. #define YSCALE_YUV_2_MONOX_C \
  722. const uint8_t * const d128=dither_8x8_220[y&7];\
  723. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  724. int acc=0;\
  725. for (i=0; i<dstW-1; i+=2) {\
  726. int j;\
  727. int Y1=1<<18;\
  728. int Y2=1<<18;\
  729. \
  730. for (j=0; j<lumFilterSize; j++) {\
  731. Y1 += lumSrc[j][i] * lumFilter[j];\
  732. Y2 += lumSrc[j][i+1] * lumFilter[j];\
  733. }\
  734. Y1>>=19;\
  735. Y2>>=19;\
  736. if ((Y1|Y2)&256) {\
  737. if (Y1>255) Y1=255;\
  738. else if (Y1<0)Y1=0;\
  739. if (Y2>255) Y2=255;\
  740. else if (Y2<0)Y2=0;\
  741. }\
  742. acc+= acc + g[Y1+d128[(i+0)&7]];\
  743. acc+= acc + g[Y2+d128[(i+1)&7]];\
  744. if ((i&7)==6) {\
  745. ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
  746. dest++;\
  747. }\
  748. }
  749. #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
  750. switch(c->dstFormat) {\
  751. case PIX_FMT_RGB48BE:\
  752. case PIX_FMT_RGB48LE:\
  753. func(uint8_t,0)\
  754. ((uint8_t*)dest)[ 0]= r[Y1];\
  755. ((uint8_t*)dest)[ 1]= r[Y1];\
  756. ((uint8_t*)dest)[ 2]= g[Y1];\
  757. ((uint8_t*)dest)[ 3]= g[Y1];\
  758. ((uint8_t*)dest)[ 4]= b[Y1];\
  759. ((uint8_t*)dest)[ 5]= b[Y1];\
  760. ((uint8_t*)dest)[ 6]= r[Y2];\
  761. ((uint8_t*)dest)[ 7]= r[Y2];\
  762. ((uint8_t*)dest)[ 8]= g[Y2];\
  763. ((uint8_t*)dest)[ 9]= g[Y2];\
  764. ((uint8_t*)dest)[10]= b[Y2];\
  765. ((uint8_t*)dest)[11]= b[Y2];\
  766. dest+=12;\
  767. }\
  768. break;\
  769. case PIX_FMT_BGR48BE:\
  770. case PIX_FMT_BGR48LE:\
  771. func(uint8_t,0)\
  772. ((uint8_t*)dest)[ 0] = ((uint8_t*)dest)[ 1] = b[Y1];\
  773. ((uint8_t*)dest)[ 2] = ((uint8_t*)dest)[ 3] = g[Y1];\
  774. ((uint8_t*)dest)[ 4] = ((uint8_t*)dest)[ 5] = r[Y1];\
  775. ((uint8_t*)dest)[ 6] = ((uint8_t*)dest)[ 7] = b[Y2];\
  776. ((uint8_t*)dest)[ 8] = ((uint8_t*)dest)[ 9] = g[Y2];\
  777. ((uint8_t*)dest)[10] = ((uint8_t*)dest)[11] = r[Y2];\
  778. dest+=12;\
  779. }\
  780. break;\
  781. case PIX_FMT_RGBA:\
  782. case PIX_FMT_BGRA:\
  783. if (CONFIG_SMALL) {\
  784. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
  785. func(uint32_t,needAlpha)\
  786. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
  787. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
  788. }\
  789. } else {\
  790. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
  791. func(uint32_t,1)\
  792. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
  793. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
  794. }\
  795. } else {\
  796. func(uint32_t,0)\
  797. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  798. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  799. }\
  800. }\
  801. }\
  802. break;\
  803. case PIX_FMT_ARGB:\
  804. case PIX_FMT_ABGR:\
  805. if (CONFIG_SMALL) {\
  806. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
  807. func(uint32_t,needAlpha)\
  808. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
  809. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
  810. }\
  811. } else {\
  812. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
  813. func(uint32_t,1)\
  814. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
  815. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
  816. }\
  817. } else {\
  818. func(uint32_t,0)\
  819. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  820. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  821. }\
  822. }\
  823. } \
  824. break;\
  825. case PIX_FMT_RGB24:\
  826. func(uint8_t,0)\
  827. ((uint8_t*)dest)[0]= r[Y1];\
  828. ((uint8_t*)dest)[1]= g[Y1];\
  829. ((uint8_t*)dest)[2]= b[Y1];\
  830. ((uint8_t*)dest)[3]= r[Y2];\
  831. ((uint8_t*)dest)[4]= g[Y2];\
  832. ((uint8_t*)dest)[5]= b[Y2];\
  833. dest+=6;\
  834. }\
  835. break;\
  836. case PIX_FMT_BGR24:\
  837. func(uint8_t,0)\
  838. ((uint8_t*)dest)[0]= b[Y1];\
  839. ((uint8_t*)dest)[1]= g[Y1];\
  840. ((uint8_t*)dest)[2]= r[Y1];\
  841. ((uint8_t*)dest)[3]= b[Y2];\
  842. ((uint8_t*)dest)[4]= g[Y2];\
  843. ((uint8_t*)dest)[5]= r[Y2];\
  844. dest+=6;\
  845. }\
  846. break;\
  847. case PIX_FMT_RGB565BE:\
  848. case PIX_FMT_RGB565LE:\
  849. case PIX_FMT_BGR565BE:\
  850. case PIX_FMT_BGR565LE:\
  851. {\
  852. const int dr1= dither_2x2_8[y&1 ][0];\
  853. const int dg1= dither_2x2_4[y&1 ][0];\
  854. const int db1= dither_2x2_8[(y&1)^1][0];\
  855. const int dr2= dither_2x2_8[y&1 ][1];\
  856. const int dg2= dither_2x2_4[y&1 ][1];\
  857. const int db2= dither_2x2_8[(y&1)^1][1];\
  858. func(uint16_t,0)\
  859. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  860. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  861. }\
  862. }\
  863. break;\
  864. case PIX_FMT_RGB555BE:\
  865. case PIX_FMT_RGB555LE:\
  866. case PIX_FMT_BGR555BE:\
  867. case PIX_FMT_BGR555LE:\
  868. {\
  869. const int dr1= dither_2x2_8[y&1 ][0];\
  870. const int dg1= dither_2x2_8[y&1 ][1];\
  871. const int db1= dither_2x2_8[(y&1)^1][0];\
  872. const int dr2= dither_2x2_8[y&1 ][1];\
  873. const int dg2= dither_2x2_8[y&1 ][0];\
  874. const int db2= dither_2x2_8[(y&1)^1][1];\
  875. func(uint16_t,0)\
  876. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  877. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  878. }\
  879. }\
  880. break;\
  881. case PIX_FMT_RGB444BE:\
  882. case PIX_FMT_RGB444LE:\
  883. case PIX_FMT_BGR444BE:\
  884. case PIX_FMT_BGR444LE:\
  885. {\
  886. const int dr1= dither_4x4_16[y&3 ][0];\
  887. const int dg1= dither_4x4_16[y&3 ][1];\
  888. const int db1= dither_4x4_16[(y&3)^3][0];\
  889. const int dr2= dither_4x4_16[y&3 ][1];\
  890. const int dg2= dither_4x4_16[y&3 ][0];\
  891. const int db2= dither_4x4_16[(y&3)^3][1];\
  892. func(uint16_t,0)\
  893. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  894. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  895. }\
  896. }\
  897. break;\
  898. case PIX_FMT_RGB8:\
  899. case PIX_FMT_BGR8:\
  900. {\
  901. const uint8_t * const d64= dither_8x8_73[y&7];\
  902. const uint8_t * const d32= dither_8x8_32[y&7];\
  903. func(uint8_t,0)\
  904. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
  905. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
  906. }\
  907. }\
  908. break;\
  909. case PIX_FMT_RGB4:\
  910. case PIX_FMT_BGR4:\
  911. {\
  912. const uint8_t * const d64= dither_8x8_73 [y&7];\
  913. const uint8_t * const d128=dither_8x8_220[y&7];\
  914. func(uint8_t,0)\
  915. ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
  916. + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
  917. }\
  918. }\
  919. break;\
  920. case PIX_FMT_RGB4_BYTE:\
  921. case PIX_FMT_BGR4_BYTE:\
  922. {\
  923. const uint8_t * const d64= dither_8x8_73 [y&7];\
  924. const uint8_t * const d128=dither_8x8_220[y&7];\
  925. func(uint8_t,0)\
  926. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
  927. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
  928. }\
  929. }\
  930. break;\
  931. case PIX_FMT_MONOBLACK:\
  932. case PIX_FMT_MONOWHITE:\
  933. {\
  934. func_monoblack\
  935. }\
  936. break;\
  937. case PIX_FMT_YUYV422:\
  938. func2\
  939. ((uint8_t*)dest)[2*i2+0]= Y1;\
  940. ((uint8_t*)dest)[2*i2+1]= U;\
  941. ((uint8_t*)dest)[2*i2+2]= Y2;\
  942. ((uint8_t*)dest)[2*i2+3]= V;\
  943. } \
  944. break;\
  945. case PIX_FMT_UYVY422:\
  946. func2\
  947. ((uint8_t*)dest)[2*i2+0]= U;\
  948. ((uint8_t*)dest)[2*i2+1]= Y1;\
  949. ((uint8_t*)dest)[2*i2+2]= V;\
  950. ((uint8_t*)dest)[2*i2+3]= Y2;\
  951. } \
  952. break;\
  953. case PIX_FMT_GRAY16BE:\
  954. func_g16\
  955. ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
  956. ((uint8_t*)dest)[2*i2+1]= Y1;\
  957. ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
  958. ((uint8_t*)dest)[2*i2+3]= Y2;\
  959. } \
  960. break;\
  961. case PIX_FMT_GRAY16LE:\
  962. func_g16\
  963. ((uint8_t*)dest)[2*i2+0]= Y1;\
  964. ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
  965. ((uint8_t*)dest)[2*i2+2]= Y2;\
  966. ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
  967. } \
  968. break;\
  969. }
  970. static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  971. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  972. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  973. {
  974. int i;
  975. 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)
  976. }
  977. static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  978. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  979. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  980. {
  981. int i;
  982. int step= c->dstFormatBpp/8;
  983. int aidx= 3;
  984. switch(c->dstFormat) {
  985. case PIX_FMT_ARGB:
  986. dest++;
  987. aidx= 0;
  988. case PIX_FMT_RGB24:
  989. aidx--;
  990. case PIX_FMT_RGBA:
  991. if (CONFIG_SMALL) {
  992. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
  993. YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
  994. dest[aidx]= needAlpha ? A : 255;
  995. dest[0]= R>>22;
  996. dest[1]= G>>22;
  997. dest[2]= B>>22;
  998. dest+= step;
  999. }
  1000. } else {
  1001. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  1002. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
  1003. dest[aidx]= A;
  1004. dest[0]= R>>22;
  1005. dest[1]= G>>22;
  1006. dest[2]= B>>22;
  1007. dest+= step;
  1008. }
  1009. } else {
  1010. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
  1011. dest[aidx]= 255;
  1012. dest[0]= R>>22;
  1013. dest[1]= G>>22;
  1014. dest[2]= B>>22;
  1015. dest+= step;
  1016. }
  1017. }
  1018. }
  1019. break;
  1020. case PIX_FMT_ABGR:
  1021. dest++;
  1022. aidx= 0;
  1023. case PIX_FMT_BGR24:
  1024. aidx--;
  1025. case PIX_FMT_BGRA:
  1026. if (CONFIG_SMALL) {
  1027. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
  1028. YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
  1029. dest[aidx]= needAlpha ? A : 255;
  1030. dest[0]= B>>22;
  1031. dest[1]= G>>22;
  1032. dest[2]= R>>22;
  1033. dest+= step;
  1034. }
  1035. } else {
  1036. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  1037. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
  1038. dest[aidx]= A;
  1039. dest[0]= B>>22;
  1040. dest[1]= G>>22;
  1041. dest[2]= R>>22;
  1042. dest+= step;
  1043. }
  1044. } else {
  1045. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
  1046. dest[aidx]= 255;
  1047. dest[0]= B>>22;
  1048. dest[1]= G>>22;
  1049. dest[2]= R>>22;
  1050. dest+= step;
  1051. }
  1052. }
  1053. }
  1054. break;
  1055. default:
  1056. assert(0);
  1057. }
  1058. }
  1059. static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
  1060. {
  1061. int i;
  1062. uint8_t *ptr = plane + stride*y;
  1063. for (i=0; i<height; i++) {
  1064. memset(ptr, val, width);
  1065. ptr += stride;
  1066. }
  1067. }
  1068. static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, long width,
  1069. uint32_t *unused)
  1070. {
  1071. int i;
  1072. for (i = 0; i < width; i++) {
  1073. int r = src[i*6+0];
  1074. int g = src[i*6+2];
  1075. int b = src[i*6+4];
  1076. dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1077. }
  1078. }
  1079. static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
  1080. const uint8_t *src1, const uint8_t *src2,
  1081. long width, uint32_t *unused)
  1082. {
  1083. int i;
  1084. assert(src1==src2);
  1085. for (i = 0; i < width; i++) {
  1086. int r = src1[6*i + 0];
  1087. int g = src1[6*i + 2];
  1088. int b = src1[6*i + 4];
  1089. dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1090. dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1091. }
  1092. }
  1093. static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
  1094. const uint8_t *src1, const uint8_t *src2,
  1095. long width, uint32_t *unused)
  1096. {
  1097. int i;
  1098. assert(src1==src2);
  1099. for (i = 0; i < width; i++) {
  1100. int r= src1[12*i + 0] + src1[12*i + 6];
  1101. int g= src1[12*i + 2] + src1[12*i + 8];
  1102. int b= src1[12*i + 4] + src1[12*i + 10];
  1103. dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  1104. dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  1105. }
  1106. }
  1107. static inline void bgr48ToY(uint8_t *dst, const uint8_t *src, long width,
  1108. uint32_t *unused)
  1109. {
  1110. int i;
  1111. for (i = 0; i < width; i++) {
  1112. int b = src[i*6+0];
  1113. int g = src[i*6+2];
  1114. int r = src[i*6+4];
  1115. dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1116. }
  1117. }
  1118. static inline void bgr48ToUV(uint8_t *dstU, uint8_t *dstV,
  1119. const uint8_t *src1, const uint8_t *src2,
  1120. long width, uint32_t *unused)
  1121. {
  1122. int i;
  1123. for (i = 0; i < width; i++) {
  1124. int b = src1[6*i + 0];
  1125. int g = src1[6*i + 2];
  1126. int r = src1[6*i + 4];
  1127. dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1128. dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1129. }
  1130. }
  1131. static inline void bgr48ToUV_half(uint8_t *dstU, uint8_t *dstV,
  1132. const uint8_t *src1, const uint8_t *src2,
  1133. long width, uint32_t *unused)
  1134. {
  1135. int i;
  1136. for (i = 0; i < width; i++) {
  1137. int b= src1[12*i + 0] + src1[12*i + 6];
  1138. int g= src1[12*i + 2] + src1[12*i + 8];
  1139. int r= src1[12*i + 4] + src1[12*i + 10];
  1140. dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  1141. dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  1142. }
  1143. }
  1144. #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
  1145. static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
  1146. {\
  1147. int i;\
  1148. for (i=0; i<width; i++) {\
  1149. int b= (((const type*)src)[i]>>shb)&maskb;\
  1150. int g= (((const type*)src)[i]>>shg)&maskg;\
  1151. int r= (((const type*)src)[i]>>shr)&maskr;\
  1152. \
  1153. dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
  1154. }\
  1155. }
  1156. BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
  1157. BGR2Y(uint32_t,bgr321ToY,16,16, 0, 0xFF00, 0x00FF, 0xFF00, RY , GY<<8, BY , RGB2YUV_SHIFT+8)
  1158. BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
  1159. BGR2Y(uint32_t,rgb321ToY, 0,16,16, 0xFF00, 0x00FF, 0xFF00, RY , GY<<8, BY , RGB2YUV_SHIFT+8)
  1160. BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY , RGB2YUV_SHIFT+8)
  1161. BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY , RGB2YUV_SHIFT+7)
  1162. BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
  1163. BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
  1164. static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  1165. {
  1166. int i;
  1167. for (i=0; i<width; i++) {
  1168. dst[i]= src[4*i];
  1169. }
  1170. }
  1171. #define BGR2UV(type, name, shr, shg, shb, shp, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S) \
  1172. static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
  1173. {\
  1174. int i;\
  1175. for (i=0; i<width; i++) {\
  1176. int b= ((((const type*)src)[i]>>shp)&maskb)>>shb;\
  1177. int g= ((((const type*)src)[i]>>shp)&maskg)>>shg;\
  1178. int r= ((((const type*)src)[i]>>shp)&maskr)>>shr;\
  1179. \
  1180. dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
  1181. dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
  1182. }\
  1183. }\
  1184. static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
  1185. {\
  1186. int i;\
  1187. for (i=0; i<width; i++) {\
  1188. int pix0= ((const type*)src)[2*i+0]>>shp;\
  1189. int pix1= ((const type*)src)[2*i+1]>>shp;\
  1190. int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
  1191. int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
  1192. int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
  1193. g&= maskg|(2*maskg);\
  1194. \
  1195. g>>=shg;\
  1196. \
  1197. dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
  1198. dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
  1199. }\
  1200. }
  1201. BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1202. BGR2UV(uint32_t,bgr321ToUV,16, 0, 0, 8, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1203. BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1204. BGR2UV(uint32_t,rgb321ToUV, 0, 0,16, 8, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1205. BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RU<<11, GU<<5, BU , RV<<11, GV<<5, BV , RGB2YUV_SHIFT+8)
  1206. BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RU<<10, GU<<5, BU , RV<<10, GV<<5, BV , RGB2YUV_SHIFT+7)
  1207. BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RU , GU<<5, BU<<11, RV , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
  1208. BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RU , GU<<5, BU<<10, RV , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
  1209. static inline void palToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
  1210. {
  1211. int i;
  1212. for (i=0; i<width; i++) {
  1213. int d= src[i];
  1214. dst[i]= pal[d] >> 24;
  1215. }
  1216. }
  1217. static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
  1218. {
  1219. int i;
  1220. for (i=0; i<width; i++) {
  1221. int d= src[i];
  1222. dst[i]= pal[d] & 0xFF;
  1223. }
  1224. }
  1225. static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
  1226. const uint8_t *src1, const uint8_t *src2,
  1227. long width, uint32_t *pal)
  1228. {
  1229. int i;
  1230. assert(src1 == src2);
  1231. for (i=0; i<width; i++) {
  1232. int p= pal[src1[i]];
  1233. dstU[i]= p>>8;
  1234. dstV[i]= p>>16;
  1235. }
  1236. }
  1237. static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  1238. {
  1239. int i, j;
  1240. for (i=0; i<width/8; i++) {
  1241. int d= ~src[i];
  1242. for(j=0; j<8; j++)
  1243. dst[8*i+j]= ((d>>(7-j))&1)*255;
  1244. }
  1245. }
  1246. static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  1247. {
  1248. int i, j;
  1249. for (i=0; i<width/8; i++) {
  1250. int d= src[i];
  1251. for(j=0; j<8; j++)
  1252. dst[8*i+j]= ((d>>(7-j))&1)*255;
  1253. }
  1254. }
  1255. //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
  1256. //Plain C versions
  1257. #if CONFIG_RUNTIME_CPUDETECT
  1258. # define COMPILE_C 1
  1259. # if ARCH_X86
  1260. # define COMPILE_MMX 1
  1261. # define COMPILE_MMX2 1
  1262. # define COMPILE_3DNOW 1
  1263. # elif ARCH_PPC
  1264. # define COMPILE_ALTIVEC HAVE_ALTIVEC
  1265. # endif
  1266. #else /* CONFIG_RUNTIME_CPUDETECT */
  1267. # if ARCH_X86
  1268. # if HAVE_MMX2
  1269. # define COMPILE_MMX2 1
  1270. # elif HAVE_AMD3DNOW
  1271. # define COMPILE_3DNOW 1
  1272. # elif HAVE_MMX
  1273. # define COMPILE_MMX 1
  1274. # else
  1275. # define COMPILE_C 1
  1276. # endif
  1277. # elif ARCH_PPC && HAVE_ALTIVEC
  1278. # define COMPILE_ALTIVEC 1
  1279. # else
  1280. # define COMPILE_C 1
  1281. # endif
  1282. #endif
  1283. #ifndef COMPILE_C
  1284. # define COMPILE_C 0
  1285. #endif
  1286. #ifndef COMPILE_MMX
  1287. # define COMPILE_MMX 0
  1288. #endif
  1289. #ifndef COMPILE_MMX2
  1290. # define COMPILE_MMX2 0
  1291. #endif
  1292. #ifndef COMPILE_3DNOW
  1293. # define COMPILE_3DNOW 0
  1294. #endif
  1295. #ifndef COMPILE_ALTIVEC
  1296. # define COMPILE_ALTIVEC 0
  1297. #endif
  1298. #define COMPILE_TEMPLATE_MMX 0
  1299. #define COMPILE_TEMPLATE_MMX2 0
  1300. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1301. #define COMPILE_TEMPLATE_ALTIVEC 0
  1302. #if COMPILE_C
  1303. #define RENAME(a) a ## _C
  1304. #include "swscale_template.c"
  1305. #endif
  1306. #if COMPILE_ALTIVEC
  1307. #undef RENAME
  1308. #undef COMPILE_TEMPLATE_ALTIVEC
  1309. #define COMPILE_TEMPLATE_ALTIVEC 1
  1310. #define RENAME(a) a ## _altivec
  1311. #include "swscale_template.c"
  1312. #endif
  1313. #if ARCH_X86
  1314. //MMX versions
  1315. #if COMPILE_MMX
  1316. #undef RENAME
  1317. #undef COMPILE_TEMPLATE_MMX
  1318. #undef COMPILE_TEMPLATE_MMX2
  1319. #undef COMPILE_TEMPLATE_AMD3DNOW
  1320. #define COMPILE_TEMPLATE_MMX 1
  1321. #define COMPILE_TEMPLATE_MMX2 0
  1322. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1323. #define RENAME(a) a ## _MMX
  1324. #include "swscale_template.c"
  1325. #endif
  1326. //MMX2 versions
  1327. #if COMPILE_MMX2
  1328. #undef RENAME
  1329. #undef COMPILE_TEMPLATE_MMX
  1330. #undef COMPILE_TEMPLATE_MMX2
  1331. #undef COMPILE_TEMPLATE_AMD3DNOW
  1332. #define COMPILE_TEMPLATE_MMX 1
  1333. #define COMPILE_TEMPLATE_MMX2 1
  1334. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1335. #define RENAME(a) a ## _MMX2
  1336. #include "swscale_template.c"
  1337. #endif
  1338. //3DNOW versions
  1339. #if COMPILE_3DNOW
  1340. #undef RENAME
  1341. #undef COMPILE_TEMPLATE_MMX
  1342. #undef COMPILE_TEMPLATE_MMX2
  1343. #undef COMPILE_TEMPLATE_AMD3DNOW
  1344. #define COMPILE_TEMPLATE_MMX 1
  1345. #define COMPILE_TEMPLATE_MMX2 0
  1346. #define COMPILE_TEMPLATE_AMD3DNOW 1
  1347. #define RENAME(a) a ## _3DNow
  1348. #include "swscale_template.c"
  1349. #endif
  1350. #endif //ARCH_X86
  1351. SwsFunc ff_getSwsFunc(SwsContext *c)
  1352. {
  1353. #if CONFIG_RUNTIME_CPUDETECT
  1354. int flags = c->flags;
  1355. #if ARCH_X86
  1356. // ordered per speed fastest first
  1357. if (flags & SWS_CPU_CAPS_MMX2) {
  1358. sws_init_swScale_MMX2(c);
  1359. return swScale_MMX2;
  1360. } else if (flags & SWS_CPU_CAPS_3DNOW) {
  1361. sws_init_swScale_3DNow(c);
  1362. return swScale_3DNow;
  1363. } else if (flags & SWS_CPU_CAPS_MMX) {
  1364. sws_init_swScale_MMX(c);
  1365. return swScale_MMX;
  1366. } else {
  1367. sws_init_swScale_C(c);
  1368. return swScale_C;
  1369. }
  1370. #else
  1371. #if COMPILE_ALTIVEC
  1372. if (flags & SWS_CPU_CAPS_ALTIVEC) {
  1373. sws_init_swScale_altivec(c);
  1374. return swScale_altivec;
  1375. } else {
  1376. sws_init_swScale_C(c);
  1377. return swScale_C;
  1378. }
  1379. #endif
  1380. sws_init_swScale_C(c);
  1381. return swScale_C;
  1382. #endif /* ARCH_X86 */
  1383. #else //CONFIG_RUNTIME_CPUDETECT
  1384. #if COMPILE_TEMPLATE_MMX2
  1385. sws_init_swScale_MMX2(c);
  1386. return swScale_MMX2;
  1387. #elif COMPILE_TEMPLATE_AMD3DNOW
  1388. sws_init_swScale_3DNow(c);
  1389. return swScale_3DNow;
  1390. #elif COMPILE_TEMPLATE_MMX
  1391. sws_init_swScale_MMX(c);
  1392. return swScale_MMX;
  1393. #elif COMPILE_TEMPLATE_ALTIVEC
  1394. sws_init_swScale_altivec(c);
  1395. return swScale_altivec;
  1396. #else
  1397. sws_init_swScale_C(c);
  1398. return swScale_C;
  1399. #endif
  1400. #endif //!CONFIG_RUNTIME_CPUDETECT
  1401. }
  1402. static void copyPlane(const uint8_t *src, int srcStride,
  1403. int srcSliceY, int srcSliceH, int width,
  1404. uint8_t *dst, int dstStride)
  1405. {
  1406. dst += dstStride * srcSliceY;
  1407. if (dstStride == srcStride && srcStride > 0) {
  1408. memcpy(dst, src, srcSliceH * dstStride);
  1409. } else {
  1410. int i;
  1411. for (i=0; i<srcSliceH; i++) {
  1412. memcpy(dst, src, width);
  1413. src += srcStride;
  1414. dst += dstStride;
  1415. }
  1416. }
  1417. }
  1418. static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1419. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1420. {
  1421. uint8_t *dst = dstParam[1] + dstStride[1]*srcSliceY/2;
  1422. copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
  1423. dstParam[0], dstStride[0]);
  1424. if (c->dstFormat == PIX_FMT_NV12)
  1425. interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
  1426. else
  1427. interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
  1428. return srcSliceH;
  1429. }
  1430. static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1431. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1432. {
  1433. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1434. yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
  1435. return srcSliceH;
  1436. }
  1437. static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1438. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1439. {
  1440. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1441. yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
  1442. return srcSliceH;
  1443. }
  1444. static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1445. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1446. {
  1447. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1448. yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
  1449. return srcSliceH;
  1450. }
  1451. static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1452. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1453. {
  1454. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1455. yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
  1456. return srcSliceH;
  1457. }
  1458. static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1459. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1460. {
  1461. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1462. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
  1463. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
  1464. yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1465. if (dstParam[3])
  1466. fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1467. return srcSliceH;
  1468. }
  1469. static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1470. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1471. {
  1472. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1473. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
  1474. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
  1475. yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1476. return srcSliceH;
  1477. }
  1478. static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1479. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1480. {
  1481. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1482. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
  1483. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
  1484. uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1485. if (dstParam[3])
  1486. fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1487. return srcSliceH;
  1488. }
  1489. static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1490. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1491. {
  1492. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1493. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
  1494. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
  1495. uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1496. return srcSliceH;
  1497. }
  1498. static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1499. {
  1500. long i;
  1501. for (i=0; i<num_pixels; i++)
  1502. ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | (src[(i<<1)+1] << 24);
  1503. }
  1504. static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1505. {
  1506. long i;
  1507. for (i=0; i<num_pixels; i++)
  1508. ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | src[(i<<1)+1];
  1509. }
  1510. static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1511. {
  1512. long i;
  1513. for (i=0; i<num_pixels; i++) {
  1514. //FIXME slow?
  1515. dst[0]= palette[src[i<<1]*4+0];
  1516. dst[1]= palette[src[i<<1]*4+1];
  1517. dst[2]= palette[src[i<<1]*4+2];
  1518. dst+= 3;
  1519. }
  1520. }
  1521. static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1522. int srcSliceH, uint8_t* dst[], int dstStride[])
  1523. {
  1524. const enum PixelFormat srcFormat= c->srcFormat;
  1525. const enum PixelFormat dstFormat= c->dstFormat;
  1526. void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
  1527. const uint8_t *palette)=NULL;
  1528. int i;
  1529. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1530. const uint8_t *srcPtr= src[0];
  1531. if (srcFormat == PIX_FMT_GRAY8A) {
  1532. switch (dstFormat) {
  1533. case PIX_FMT_RGB32 : conv = gray8aToPacked32; break;
  1534. case PIX_FMT_BGR32 : conv = gray8aToPacked32; break;
  1535. case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
  1536. case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
  1537. case PIX_FMT_RGB24 : conv = gray8aToPacked24; break;
  1538. case PIX_FMT_BGR24 : conv = gray8aToPacked24; break;
  1539. }
  1540. } else if (usePal(srcFormat)) {
  1541. switch (dstFormat) {
  1542. case PIX_FMT_RGB32 : conv = sws_convertPalette8ToPacked32; break;
  1543. case PIX_FMT_BGR32 : conv = sws_convertPalette8ToPacked32; break;
  1544. case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
  1545. case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
  1546. case PIX_FMT_RGB24 : conv = sws_convertPalette8ToPacked24; break;
  1547. case PIX_FMT_BGR24 : conv = sws_convertPalette8ToPacked24; break;
  1548. }
  1549. }
  1550. if (!conv)
  1551. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1552. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1553. else {
  1554. for (i=0; i<srcSliceH; i++) {
  1555. conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
  1556. srcPtr+= srcStride[0];
  1557. dstPtr+= dstStride[0];
  1558. }
  1559. }
  1560. return srcSliceH;
  1561. }
  1562. #define isRGBA32(x) ( \
  1563. (x) == PIX_FMT_ARGB \
  1564. || (x) == PIX_FMT_RGBA \
  1565. || (x) == PIX_FMT_BGRA \
  1566. || (x) == PIX_FMT_ABGR \
  1567. )
  1568. /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
  1569. static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1570. int srcSliceH, uint8_t* dst[], int dstStride[])
  1571. {
  1572. const enum PixelFormat srcFormat= c->srcFormat;
  1573. const enum PixelFormat dstFormat= c->dstFormat;
  1574. const int srcBpp= (c->srcFormatBpp + 7) >> 3;
  1575. const int dstBpp= (c->dstFormatBpp + 7) >> 3;
  1576. const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
  1577. const int dstId= c->dstFormatBpp >> 2;
  1578. void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
  1579. #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
  1580. if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
  1581. if ( CONV_IS(ABGR, RGBA)
  1582. || CONV_IS(ARGB, BGRA)
  1583. || CONV_IS(BGRA, ARGB)
  1584. || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
  1585. else if (CONV_IS(ABGR, ARGB)
  1586. || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
  1587. else if (CONV_IS(ABGR, BGRA)
  1588. || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
  1589. else if (CONV_IS(BGRA, RGBA)
  1590. || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
  1591. else if (CONV_IS(BGRA, ABGR)
  1592. || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
  1593. } else
  1594. /* BGR -> BGR */
  1595. if ( (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
  1596. || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
  1597. switch(srcId | (dstId<<4)) {
  1598. case 0x34: conv= rgb16to15; break;
  1599. case 0x36: conv= rgb24to15; break;
  1600. case 0x38: conv= rgb32to15; break;
  1601. case 0x43: conv= rgb15to16; break;
  1602. case 0x46: conv= rgb24to16; break;
  1603. case 0x48: conv= rgb32to16; break;
  1604. case 0x63: conv= rgb15to24; break;
  1605. case 0x64: conv= rgb16to24; break;
  1606. case 0x68: conv= rgb32to24; break;
  1607. case 0x83: conv= rgb15to32; break;
  1608. case 0x84: conv= rgb16to32; break;
  1609. case 0x86: conv= rgb24to32; break;
  1610. }
  1611. } else if ( (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
  1612. || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
  1613. switch(srcId | (dstId<<4)) {
  1614. case 0x33: conv= rgb15tobgr15; break;
  1615. case 0x34: conv= rgb16tobgr15; break;
  1616. case 0x36: conv= rgb24tobgr15; break;
  1617. case 0x38: conv= rgb32tobgr15; break;
  1618. case 0x43: conv= rgb15tobgr16; break;
  1619. case 0x44: conv= rgb16tobgr16; break;
  1620. case 0x46: conv= rgb24tobgr16; break;
  1621. case 0x48: conv= rgb32tobgr16; break;
  1622. case 0x63: conv= rgb15tobgr24; break;
  1623. case 0x64: conv= rgb16tobgr24; break;
  1624. case 0x66: conv= rgb24tobgr24; break;
  1625. case 0x68: conv= rgb32tobgr24; break;
  1626. case 0x83: conv= rgb15tobgr32; break;
  1627. case 0x84: conv= rgb16tobgr32; break;
  1628. case 0x86: conv= rgb24tobgr32; break;
  1629. }
  1630. }
  1631. if (!conv) {
  1632. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1633. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1634. } else {
  1635. const uint8_t *srcPtr= src[0];
  1636. uint8_t *dstPtr= dst[0];
  1637. if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
  1638. srcPtr += ALT32_CORR;
  1639. if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
  1640. dstPtr += ALT32_CORR;
  1641. if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0 && !(srcStride[0]%srcBpp))
  1642. conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
  1643. else {
  1644. int i;
  1645. dstPtr += dstStride[0]*srcSliceY;
  1646. for (i=0; i<srcSliceH; i++) {
  1647. conv(srcPtr, dstPtr, c->srcW*srcBpp);
  1648. srcPtr+= srcStride[0];
  1649. dstPtr+= dstStride[0];
  1650. }
  1651. }
  1652. }
  1653. return srcSliceH;
  1654. }
  1655. static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1656. int srcSliceH, uint8_t* dst[], int dstStride[])
  1657. {
  1658. rgb24toyv12(
  1659. src[0],
  1660. dst[0]+ srcSliceY *dstStride[0],
  1661. dst[1]+(srcSliceY>>1)*dstStride[1],
  1662. dst[2]+(srcSliceY>>1)*dstStride[2],
  1663. c->srcW, srcSliceH,
  1664. dstStride[0], dstStride[1], srcStride[0]);
  1665. if (dst[3])
  1666. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1667. return srcSliceH;
  1668. }
  1669. static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1670. int srcSliceH, uint8_t* dst[], int dstStride[])
  1671. {
  1672. copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
  1673. dst[0], dstStride[0]);
  1674. planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
  1675. srcSliceH >> 2, srcStride[1], dstStride[1]);
  1676. planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
  1677. srcSliceH >> 2, srcStride[2], dstStride[2]);
  1678. if (dst[3])
  1679. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1680. return srcSliceH;
  1681. }
  1682. /* unscaled copy like stuff (assumes nearly identical formats) */
  1683. static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1684. int srcSliceH, uint8_t* dst[], int dstStride[])
  1685. {
  1686. if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
  1687. memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
  1688. else {
  1689. int i;
  1690. const uint8_t *srcPtr= src[0];
  1691. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1692. int length=0;
  1693. /* universal length finder */
  1694. while(length+c->srcW <= FFABS(dstStride[0])
  1695. && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
  1696. assert(length!=0);
  1697. for (i=0; i<srcSliceH; i++) {
  1698. memcpy(dstPtr, srcPtr, length);
  1699. srcPtr+= srcStride[0];
  1700. dstPtr+= dstStride[0];
  1701. }
  1702. }
  1703. return srcSliceH;
  1704. }
  1705. #define DITHER_COPY(dst, dstStride, src, srcStride, bswap, dbswap)\
  1706. uint16_t scale= dither_scale[dst_depth-1][src_depth-1];\
  1707. int shift= src_depth-dst_depth + dither_scale[src_depth-2][dst_depth-1];\
  1708. for (i = 0; i < height; i++) {\
  1709. uint8_t *dither= dithers[src_depth-9][i&7];\
  1710. for (j = 0; j < length-7; j+=8){\
  1711. dst[j+0] = dbswap((bswap(src[j+0]) + dither[0])*scale>>shift);\
  1712. dst[j+1] = dbswap((bswap(src[j+1]) + dither[1])*scale>>shift);\
  1713. dst[j+2] = dbswap((bswap(src[j+2]) + dither[2])*scale>>shift);\
  1714. dst[j+3] = dbswap((bswap(src[j+3]) + dither[3])*scale>>shift);\
  1715. dst[j+4] = dbswap((bswap(src[j+4]) + dither[4])*scale>>shift);\
  1716. dst[j+5] = dbswap((bswap(src[j+5]) + dither[5])*scale>>shift);\
  1717. dst[j+6] = dbswap((bswap(src[j+6]) + dither[6])*scale>>shift);\
  1718. dst[j+7] = dbswap((bswap(src[j+7]) + dither[7])*scale>>shift);\
  1719. }\
  1720. for (; j < length; j++)\
  1721. dst[j] = dbswap((bswap(src[j]) + dither[j&7])*scale>>shift);\
  1722. dst += dstStride;\
  1723. src += srcStride;\
  1724. }
  1725. static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1726. int srcSliceH, uint8_t* dst[], int dstStride[])
  1727. {
  1728. int plane, i, j;
  1729. for (plane=0; plane<4; plane++) {
  1730. int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
  1731. int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
  1732. int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
  1733. const uint8_t *srcPtr= src[plane];
  1734. uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
  1735. if (!dst[plane]) continue;
  1736. // ignore palette for GRAY8
  1737. if (plane == 1 && !dst[2]) continue;
  1738. if (!src[plane] || (plane == 1 && !src[2])) {
  1739. if(is16BPS(c->dstFormat))
  1740. length*=2;
  1741. fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
  1742. } else {
  1743. if(isNBPS(c->srcFormat) || isNBPS(c->dstFormat)
  1744. || (is16BPS(c->srcFormat) != is16BPS(c->dstFormat))
  1745. ) {
  1746. const int src_depth = av_pix_fmt_descriptors[c->srcFormat].comp[plane].depth_minus1+1;
  1747. const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
  1748. const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
  1749. uint16_t *dstPtr2 = (uint16_t*)dstPtr;
  1750. if (dst_depth == 8) {
  1751. if(isBE(c->srcFormat) == HAVE_BIGENDIAN){
  1752. DITHER_COPY(dstPtr, dstStride[plane], srcPtr2, srcStride[plane]/2, , )
  1753. } else {
  1754. DITHER_COPY(dstPtr, dstStride[plane], srcPtr2, srcStride[plane]/2, av_bswap16, )
  1755. }
  1756. } else if (src_depth == 8) {
  1757. for (i = 0; i < height; i++) {
  1758. if(isBE(c->dstFormat)){
  1759. for (j = 0; j < length; j++)
  1760. AV_WB16(&dstPtr2[j], (srcPtr[j]<<(dst_depth-8)) |
  1761. (srcPtr[j]>>(2*8-dst_depth)));
  1762. } else {
  1763. for (j = 0; j < length; j++)
  1764. AV_WL16(&dstPtr2[j], (srcPtr[j]<<(dst_depth-8)) |
  1765. (srcPtr[j]>>(2*8-dst_depth)));
  1766. }
  1767. dstPtr2 += dstStride[plane]/2;
  1768. srcPtr += srcStride[plane];
  1769. }
  1770. } else if (src_depth <= dst_depth) {
  1771. for (i = 0; i < height; i++) {
  1772. #define COPY_UP(r,w) \
  1773. for (j = 0; j < length; j++){ \
  1774. unsigned int v= r(&srcPtr2[j]);\
  1775. w(&dstPtr2[j], (v<<(dst_depth-src_depth)) | \
  1776. (v>>(2*src_depth-dst_depth)));\
  1777. }
  1778. if(isBE(c->srcFormat)){
  1779. if(isBE(c->dstFormat)){
  1780. COPY_UP(AV_RB16, AV_WB16)
  1781. } else {
  1782. COPY_UP(AV_RB16, AV_WL16)
  1783. }
  1784. } else {
  1785. if(isBE(c->dstFormat)){
  1786. COPY_UP(AV_RL16, AV_WB16)
  1787. } else {
  1788. COPY_UP(AV_RL16, AV_WL16)
  1789. }
  1790. }
  1791. dstPtr2 += dstStride[plane]/2;
  1792. srcPtr2 += srcStride[plane]/2;
  1793. }
  1794. } else {
  1795. if(isBE(c->srcFormat) == HAVE_BIGENDIAN){
  1796. if(isBE(c->dstFormat) == HAVE_BIGENDIAN){
  1797. DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, , )
  1798. } else {
  1799. DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, , av_bswap16)
  1800. }
  1801. }else{
  1802. if(isBE(c->dstFormat) == HAVE_BIGENDIAN){
  1803. DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, av_bswap16, )
  1804. } else {
  1805. DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, av_bswap16, av_bswap16)
  1806. }
  1807. }
  1808. }
  1809. } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
  1810. && isBE(c->srcFormat) != isBE(c->dstFormat)) {
  1811. for (i=0; i<height; i++) {
  1812. for (j=0; j<length; j++)
  1813. ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
  1814. srcPtr+= srcStride[plane];
  1815. dstPtr+= dstStride[plane];
  1816. }
  1817. } else if (dstStride[plane] == srcStride[plane] &&
  1818. srcStride[plane] > 0 && srcStride[plane] == length) {
  1819. memcpy(dst[plane] + dstStride[plane]*y, src[plane],
  1820. height*dstStride[plane]);
  1821. } else {
  1822. if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
  1823. length*=2;
  1824. for (i=0; i<height; i++) {
  1825. memcpy(dstPtr, srcPtr, length);
  1826. srcPtr+= srcStride[plane];
  1827. dstPtr+= dstStride[plane];
  1828. }
  1829. }
  1830. }
  1831. }
  1832. return srcSliceH;
  1833. }
  1834. int ff_hardcodedcpuflags(void)
  1835. {
  1836. int flags = 0;
  1837. #if COMPILE_TEMPLATE_MMX2
  1838. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
  1839. #elif COMPILE_TEMPLATE_AMD3DNOW
  1840. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
  1841. #elif COMPILE_TEMPLATE_MMX
  1842. flags |= SWS_CPU_CAPS_MMX;
  1843. #elif COMPILE_TEMPLATE_ALTIVEC
  1844. flags |= SWS_CPU_CAPS_ALTIVEC;
  1845. #elif ARCH_BFIN
  1846. flags |= SWS_CPU_CAPS_BFIN;
  1847. #endif
  1848. return flags;
  1849. }
  1850. void ff_get_unscaled_swscale(SwsContext *c)
  1851. {
  1852. const enum PixelFormat srcFormat = c->srcFormat;
  1853. const enum PixelFormat dstFormat = c->dstFormat;
  1854. const int flags = c->flags;
  1855. const int dstH = c->dstH;
  1856. int needsDither;
  1857. needsDither= isAnyRGB(dstFormat)
  1858. && c->dstFormatBpp < 24
  1859. && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
  1860. /* yv12_to_nv12 */
  1861. if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
  1862. c->swScale= planarToNv12Wrapper;
  1863. }
  1864. /* yuv2bgr */
  1865. if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
  1866. && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
  1867. c->swScale= ff_yuv2rgb_get_func_ptr(c);
  1868. }
  1869. if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
  1870. c->swScale= yvu9ToYv12Wrapper;
  1871. }
  1872. /* bgr24toYV12 */
  1873. if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
  1874. c->swScale= bgr24ToYv12Wrapper;
  1875. /* RGB/BGR -> RGB/BGR (no dither needed forms) */
  1876. if ( isAnyRGB(srcFormat)
  1877. && isAnyRGB(dstFormat)
  1878. && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
  1879. && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
  1880. && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
  1881. && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
  1882. && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
  1883. && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
  1884. && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
  1885. && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
  1886. && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
  1887. && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
  1888. && srcFormat != PIX_FMT_BGR48LE && dstFormat != PIX_FMT_BGR48LE
  1889. && srcFormat != PIX_FMT_BGR48BE && dstFormat != PIX_FMT_BGR48BE
  1890. && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
  1891. c->swScale= rgbToRgbWrapper;
  1892. if ((usePal(srcFormat) && (
  1893. dstFormat == PIX_FMT_RGB32 ||
  1894. dstFormat == PIX_FMT_RGB32_1 ||
  1895. dstFormat == PIX_FMT_RGB24 ||
  1896. dstFormat == PIX_FMT_BGR32 ||
  1897. dstFormat == PIX_FMT_BGR32_1 ||
  1898. dstFormat == PIX_FMT_BGR24)))
  1899. c->swScale= palToRgbWrapper;
  1900. if (srcFormat == PIX_FMT_YUV422P) {
  1901. if (dstFormat == PIX_FMT_YUYV422)
  1902. c->swScale= yuv422pToYuy2Wrapper;
  1903. else if (dstFormat == PIX_FMT_UYVY422)
  1904. c->swScale= yuv422pToUyvyWrapper;
  1905. }
  1906. /* LQ converters if -sws 0 or -sws 4*/
  1907. if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
  1908. /* yv12_to_yuy2 */
  1909. if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
  1910. if (dstFormat == PIX_FMT_YUYV422)
  1911. c->swScale= planarToYuy2Wrapper;
  1912. else if (dstFormat == PIX_FMT_UYVY422)
  1913. c->swScale= planarToUyvyWrapper;
  1914. }
  1915. }
  1916. if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  1917. c->swScale= yuyvToYuv420Wrapper;
  1918. if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  1919. c->swScale= uyvyToYuv420Wrapper;
  1920. if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
  1921. c->swScale= yuyvToYuv422Wrapper;
  1922. if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
  1923. c->swScale= uyvyToYuv422Wrapper;
  1924. #if COMPILE_ALTIVEC
  1925. if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
  1926. !(c->flags & SWS_BITEXACT) &&
  1927. srcFormat == PIX_FMT_YUV420P) {
  1928. // unscaled YV12 -> packed YUV, we want speed
  1929. if (dstFormat == PIX_FMT_YUYV422)
  1930. c->swScale= yv12toyuy2_unscaled_altivec;
  1931. else if (dstFormat == PIX_FMT_UYVY422)
  1932. c->swScale= yv12touyvy_unscaled_altivec;
  1933. }
  1934. #endif
  1935. /* simple copy */
  1936. if ( srcFormat == dstFormat
  1937. || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
  1938. || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
  1939. || (isPlanarYUV(srcFormat) && isGray(dstFormat))
  1940. || (isPlanarYUV(dstFormat) && isGray(srcFormat))
  1941. || (isGray(dstFormat) && isGray(srcFormat))
  1942. || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
  1943. && c->chrDstHSubSample == c->chrSrcHSubSample
  1944. && c->chrDstVSubSample == c->chrSrcVSubSample
  1945. && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
  1946. && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
  1947. {
  1948. if (isPacked(c->srcFormat))
  1949. c->swScale= packedCopyWrapper;
  1950. else /* Planar YUV or gray */
  1951. c->swScale= planarCopyWrapper;
  1952. }
  1953. #if ARCH_BFIN
  1954. if (flags & SWS_CPU_CAPS_BFIN)
  1955. ff_bfin_get_unscaled_swscale (c);
  1956. #endif
  1957. }
  1958. static void reset_ptr(const uint8_t* src[], int format)
  1959. {
  1960. if(!isALPHA(format))
  1961. src[3]=NULL;
  1962. if(!isPlanarYUV(format)) {
  1963. src[3]=src[2]=NULL;
  1964. if (!usePal(format))
  1965. src[1]= NULL;
  1966. }
  1967. }
  1968. static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
  1969. const int linesizes[4])
  1970. {
  1971. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  1972. int i;
  1973. for (i = 0; i < 4; i++) {
  1974. int plane = desc->comp[i].plane;
  1975. if (!data[plane] || !linesizes[plane])
  1976. return 0;
  1977. }
  1978. return 1;
  1979. }
  1980. /**
  1981. * swscale wrapper, so we don't need to export the SwsContext.
  1982. * Assumes planar YUV to be in YUV order instead of YVU.
  1983. */
  1984. int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
  1985. int srcSliceH, uint8_t* const dst[], const int dstStride[])
  1986. {
  1987. int i;
  1988. const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
  1989. uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
  1990. // do not mess up sliceDir if we have a "trailing" 0-size slice
  1991. if (srcSliceH == 0)
  1992. return 0;
  1993. if (!check_image_pointers(src, c->srcFormat, srcStride)) {
  1994. av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
  1995. return 0;
  1996. }
  1997. if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
  1998. av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
  1999. return 0;
  2000. }
  2001. if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
  2002. av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
  2003. return 0;
  2004. }
  2005. if (c->sliceDir == 0) {
  2006. if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
  2007. }
  2008. if (usePal(c->srcFormat)) {
  2009. for (i=0; i<256; i++) {
  2010. int p, r, g, b, y, u, v, a = 0xff;
  2011. if(c->srcFormat == PIX_FMT_PAL8) {
  2012. p=((const uint32_t*)(src[1]))[i];
  2013. a= (p>>24)&0xFF;
  2014. r= (p>>16)&0xFF;
  2015. g= (p>> 8)&0xFF;
  2016. b= p &0xFF;
  2017. } else if(c->srcFormat == PIX_FMT_RGB8) {
  2018. r= (i>>5 )*36;
  2019. g= ((i>>2)&7)*36;
  2020. b= (i&3 )*85;
  2021. } else if(c->srcFormat == PIX_FMT_BGR8) {
  2022. b= (i>>6 )*85;
  2023. g= ((i>>3)&7)*36;
  2024. r= (i&7 )*36;
  2025. } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
  2026. r= (i>>3 )*255;
  2027. g= ((i>>1)&3)*85;
  2028. b= (i&1 )*255;
  2029. } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_GRAY8A) {
  2030. r = g = b = i;
  2031. } else {
  2032. assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
  2033. b= (i>>3 )*255;
  2034. g= ((i>>1)&3)*85;
  2035. r= (i&1 )*255;
  2036. }
  2037. y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  2038. u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  2039. v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  2040. c->pal_yuv[i]= y + (u<<8) + (v<<16) + (a<<24);
  2041. switch(c->dstFormat) {
  2042. case PIX_FMT_BGR32:
  2043. #if !HAVE_BIGENDIAN
  2044. case PIX_FMT_RGB24:
  2045. #endif
  2046. c->pal_rgb[i]= r + (g<<8) + (b<<16) + (a<<24);
  2047. break;
  2048. case PIX_FMT_BGR32_1:
  2049. #if HAVE_BIGENDIAN
  2050. case PIX_FMT_BGR24:
  2051. #endif
  2052. c->pal_rgb[i]= a + (r<<8) + (g<<16) + (b<<24);
  2053. break;
  2054. case PIX_FMT_RGB32_1:
  2055. #if HAVE_BIGENDIAN
  2056. case PIX_FMT_RGB24:
  2057. #endif
  2058. c->pal_rgb[i]= a + (b<<8) + (g<<16) + (r<<24);
  2059. break;
  2060. case PIX_FMT_RGB32:
  2061. #if !HAVE_BIGENDIAN
  2062. case PIX_FMT_BGR24:
  2063. #endif
  2064. default:
  2065. c->pal_rgb[i]= b + (g<<8) + (r<<16) + (a<<24);
  2066. }
  2067. }
  2068. }
  2069. // copy strides, so they can safely be modified
  2070. if (c->sliceDir == 1) {
  2071. // slices go from top to bottom
  2072. int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
  2073. int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
  2074. reset_ptr(src2, c->srcFormat);
  2075. reset_ptr((const uint8_t**)dst2, c->dstFormat);
  2076. /* reset slice direction at end of frame */
  2077. if (srcSliceY + srcSliceH == c->srcH)
  2078. c->sliceDir = 0;
  2079. return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
  2080. } else {
  2081. // slices go from bottom to top => we flip the image internally
  2082. int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
  2083. int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
  2084. src2[0] += (srcSliceH-1)*srcStride[0];
  2085. if (!usePal(c->srcFormat))
  2086. src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
  2087. src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
  2088. src2[3] += (srcSliceH-1)*srcStride[3];
  2089. dst2[0] += ( c->dstH -1)*dstStride[0];
  2090. dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
  2091. dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
  2092. dst2[3] += ( c->dstH -1)*dstStride[3];
  2093. reset_ptr(src2, c->srcFormat);
  2094. reset_ptr((const uint8_t**)dst2, c->dstFormat);
  2095. /* reset slice direction at end of frame */
  2096. if (!srcSliceY)
  2097. c->sliceDir = 0;
  2098. return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
  2099. }
  2100. }
  2101. #if LIBSWSCALE_VERSION_MAJOR < 1
  2102. int sws_scale_ordered(SwsContext *c, const uint8_t* const src[], int srcStride[], int srcSliceY,
  2103. int srcSliceH, uint8_t* dst[], int dstStride[])
  2104. {
  2105. return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
  2106. }
  2107. #endif
  2108. /* Convert the palette to the same packed 32-bit format as the palette */
  2109. void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  2110. {
  2111. long i;
  2112. for (i=0; i<num_pixels; i++)
  2113. ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
  2114. }
  2115. /* Palette format: ABCD -> dst format: ABC */
  2116. void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  2117. {
  2118. long i;
  2119. for (i=0; i<num_pixels; i++) {
  2120. //FIXME slow?
  2121. dst[0]= palette[src[i]*4+0];
  2122. dst[1]= palette[src[i]*4+1];
  2123. dst[2]= palette[src[i]*4+2];
  2124. dst+= 3;
  2125. }
  2126. }