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.

2942 lines
111KB

  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/avassert.h"
  59. #include "libavutil/intreadwrite.h"
  60. #include "libavutil/cpu.h"
  61. #include "libavutil/avutil.h"
  62. #include "libavutil/mathematics.h"
  63. #include "libavutil/bswap.h"
  64. #include "libavutil/pixdesc.h"
  65. #define RGB2YUV_SHIFT 15
  66. #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  67. #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  68. #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  69. #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  70. #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  71. #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  72. #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  73. #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  74. #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  75. static const double rgb2yuv_table[8][9]={
  76. {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
  77. {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
  78. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  79. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  80. {0.59 , 0.11 , 0.30 , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
  81. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  82. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  83. {0.701 , 0.087 , 0.212 , -0.384, 0.5, -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
  84. };
  85. /*
  86. NOTES
  87. Special versions: fast Y 1:1 scaling (no interpolation in y direction)
  88. TODO
  89. more intelligent misalignment avoidance for the horizontal scaler
  90. write special vertical cubic upscale version
  91. optimize C code (YV12 / minmax)
  92. add support for packed pixel YUV input & output
  93. add support for Y8 output
  94. optimize BGR24 & BGR32
  95. add BGR4 output support
  96. write special BGR->BGR scaler
  97. */
  98. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
  99. { 1, 3, 1, 3, 1, 3, 1, 3, },
  100. { 2, 0, 2, 0, 2, 0, 2, 0, },
  101. };
  102. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
  103. { 6, 2, 6, 2, 6, 2, 6, 2, },
  104. { 0, 4, 0, 4, 0, 4, 0, 4, },
  105. };
  106. DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[4][8]={
  107. { 8, 4, 11, 7, 8, 4, 11, 7, },
  108. { 2, 14, 1, 13, 2, 14, 1, 13, },
  109. { 10, 6, 9, 5, 10, 6, 9, 5, },
  110. { 0, 12, 3, 15, 0, 12, 3, 15, },
  111. };
  112. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
  113. { 17, 9, 23, 15, 16, 8, 22, 14, },
  114. { 5, 29, 3, 27, 4, 28, 2, 26, },
  115. { 21, 13, 19, 11, 20, 12, 18, 10, },
  116. { 0, 24, 6, 30, 1, 25, 7, 31, },
  117. { 16, 8, 22, 14, 17, 9, 23, 15, },
  118. { 4, 28, 2, 26, 5, 29, 3, 27, },
  119. { 20, 12, 18, 10, 21, 13, 19, 11, },
  120. { 1, 25, 7, 31, 0, 24, 6, 30, },
  121. };
  122. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
  123. { 0, 55, 14, 68, 3, 58, 17, 72, },
  124. { 37, 18, 50, 32, 40, 22, 54, 35, },
  125. { 9, 64, 5, 59, 13, 67, 8, 63, },
  126. { 46, 27, 41, 23, 49, 31, 44, 26, },
  127. { 2, 57, 16, 71, 1, 56, 15, 70, },
  128. { 39, 21, 52, 34, 38, 19, 51, 33, },
  129. { 11, 66, 7, 62, 10, 65, 6, 60, },
  130. { 48, 30, 43, 25, 47, 29, 42, 24, },
  131. };
  132. #if 1
  133. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  134. {117, 62, 158, 103, 113, 58, 155, 100, },
  135. { 34, 199, 21, 186, 31, 196, 17, 182, },
  136. {144, 89, 131, 76, 141, 86, 127, 72, },
  137. { 0, 165, 41, 206, 10, 175, 52, 217, },
  138. {110, 55, 151, 96, 120, 65, 162, 107, },
  139. { 28, 193, 14, 179, 38, 203, 24, 189, },
  140. {138, 83, 124, 69, 148, 93, 134, 79, },
  141. { 7, 172, 48, 213, 3, 168, 45, 210, },
  142. };
  143. #elif 1
  144. // tries to correct a gamma of 1.5
  145. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  146. { 0, 143, 18, 200, 2, 156, 25, 215, },
  147. { 78, 28, 125, 64, 89, 36, 138, 74, },
  148. { 10, 180, 3, 161, 16, 195, 8, 175, },
  149. {109, 51, 93, 38, 121, 60, 105, 47, },
  150. { 1, 152, 23, 210, 0, 147, 20, 205, },
  151. { 85, 33, 134, 71, 81, 30, 130, 67, },
  152. { 14, 190, 6, 171, 12, 185, 5, 166, },
  153. {117, 57, 101, 44, 113, 54, 97, 41, },
  154. };
  155. #elif 1
  156. // tries to correct a gamma of 2.0
  157. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  158. { 0, 124, 8, 193, 0, 140, 12, 213, },
  159. { 55, 14, 104, 42, 66, 19, 119, 52, },
  160. { 3, 168, 1, 145, 6, 187, 3, 162, },
  161. { 86, 31, 70, 21, 99, 39, 82, 28, },
  162. { 0, 134, 11, 206, 0, 129, 9, 200, },
  163. { 62, 17, 114, 48, 58, 16, 109, 45, },
  164. { 5, 181, 2, 157, 4, 175, 1, 151, },
  165. { 95, 36, 78, 26, 90, 34, 74, 24, },
  166. };
  167. #else
  168. // tries to correct a gamma of 2.5
  169. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  170. { 0, 107, 3, 187, 0, 125, 6, 212, },
  171. { 39, 7, 86, 28, 49, 11, 102, 36, },
  172. { 1, 158, 0, 131, 3, 180, 1, 151, },
  173. { 68, 19, 52, 12, 81, 25, 64, 17, },
  174. { 0, 119, 5, 203, 0, 113, 4, 195, },
  175. { 45, 9, 96, 33, 42, 8, 91, 30, },
  176. { 2, 172, 1, 144, 2, 165, 0, 137, },
  177. { 77, 23, 60, 15, 72, 21, 56, 14, },
  178. };
  179. #endif
  180. DECLARE_ALIGNED(8, const uint8_t, dithers)[8][8][8]={
  181. {
  182. { 0, 1, 0, 1, 0, 1, 0, 1,},
  183. { 1, 0, 1, 0, 1, 0, 1, 0,},
  184. { 0, 1, 0, 1, 0, 1, 0, 1,},
  185. { 1, 0, 1, 0, 1, 0, 1, 0,},
  186. { 0, 1, 0, 1, 0, 1, 0, 1,},
  187. { 1, 0, 1, 0, 1, 0, 1, 0,},
  188. { 0, 1, 0, 1, 0, 1, 0, 1,},
  189. { 1, 0, 1, 0, 1, 0, 1, 0,},
  190. },{
  191. { 1, 2, 1, 2, 1, 2, 1, 2,},
  192. { 3, 0, 3, 0, 3, 0, 3, 0,},
  193. { 1, 2, 1, 2, 1, 2, 1, 2,},
  194. { 3, 0, 3, 0, 3, 0, 3, 0,},
  195. { 1, 2, 1, 2, 1, 2, 1, 2,},
  196. { 3, 0, 3, 0, 3, 0, 3, 0,},
  197. { 1, 2, 1, 2, 1, 2, 1, 2,},
  198. { 3, 0, 3, 0, 3, 0, 3, 0,},
  199. },{
  200. { 2, 4, 3, 5, 2, 4, 3, 5,},
  201. { 6, 0, 7, 1, 6, 0, 7, 1,},
  202. { 3, 5, 2, 4, 3, 5, 2, 4,},
  203. { 7, 1, 6, 0, 7, 1, 6, 0,},
  204. { 2, 4, 3, 5, 2, 4, 3, 5,},
  205. { 6, 0, 7, 1, 6, 0, 7, 1,},
  206. { 3, 5, 2, 4, 3, 5, 2, 4,},
  207. { 7, 1, 6, 0, 7, 1, 6, 0,},
  208. },{
  209. { 4, 8, 7, 11, 4, 8, 7, 11,},
  210. { 12, 0, 15, 3, 12, 0, 15, 3,},
  211. { 6, 10, 5, 9, 6, 10, 5, 9,},
  212. { 14, 2, 13, 1, 14, 2, 13, 1,},
  213. { 4, 8, 7, 11, 4, 8, 7, 11,},
  214. { 12, 0, 15, 3, 12, 0, 15, 3,},
  215. { 6, 10, 5, 9, 6, 10, 5, 9,},
  216. { 14, 2, 13, 1, 14, 2, 13, 1,},
  217. },{
  218. { 9, 17, 15, 23, 8, 16, 14, 22,},
  219. { 25, 1, 31, 7, 24, 0, 30, 6,},
  220. { 13, 21, 11, 19, 12, 20, 10, 18,},
  221. { 29, 5, 27, 3, 28, 4, 26, 2,},
  222. { 8, 16, 14, 22, 9, 17, 15, 23,},
  223. { 24, 0, 30, 6, 25, 1, 31, 7,},
  224. { 12, 20, 10, 18, 13, 21, 11, 19,},
  225. { 28, 4, 26, 2, 29, 5, 27, 3,},
  226. },{
  227. { 18, 34, 30, 46, 17, 33, 29, 45,},
  228. { 50, 2, 62, 14, 49, 1, 61, 13,},
  229. { 26, 42, 22, 38, 25, 41, 21, 37,},
  230. { 58, 10, 54, 6, 57, 9, 53, 5,},
  231. { 16, 32, 28, 44, 19, 35, 31, 47,},
  232. { 48, 0, 60, 12, 51, 3, 63, 15,},
  233. { 24, 40, 20, 36, 27, 43, 23, 39,},
  234. { 56, 8, 52, 4, 59, 11, 55, 7,},
  235. },{
  236. { 18, 34, 30, 46, 17, 33, 29, 45,},
  237. { 50, 2, 62, 14, 49, 1, 61, 13,},
  238. { 26, 42, 22, 38, 25, 41, 21, 37,},
  239. { 58, 10, 54, 6, 57, 9, 53, 5,},
  240. { 16, 32, 28, 44, 19, 35, 31, 47,},
  241. { 48, 0, 60, 12, 51, 3, 63, 15,},
  242. { 24, 40, 20, 36, 27, 43, 23, 39,},
  243. { 56, 8, 52, 4, 59, 11, 55, 7,},
  244. },{
  245. { 36, 68, 60, 92, 34, 66, 58, 90,},
  246. { 100, 4,124, 28, 98, 2,122, 26,},
  247. { 52, 84, 44, 76, 50, 82, 42, 74,},
  248. { 116, 20,108, 12,114, 18,106, 10,},
  249. { 32, 64, 56, 88, 38, 70, 62, 94,},
  250. { 96, 0,120, 24,102, 6,126, 30,},
  251. { 48, 80, 40, 72, 54, 86, 46, 78,},
  252. { 112, 16,104, 8,118, 22,110, 14,},
  253. }};
  254. static const uint8_t flat64[8]={64,64,64,64,64,64,64,64};
  255. const uint16_t dither_scale[15][16]={
  256. { 2, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,},
  257. { 2, 3, 7, 7, 13, 13, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,},
  258. { 3, 3, 4, 15, 15, 29, 57, 57, 57, 113, 113, 113, 113, 113, 113, 113,},
  259. { 3, 4, 4, 5, 31, 31, 61, 121, 241, 241, 241, 241, 481, 481, 481, 481,},
  260. { 3, 4, 5, 5, 6, 63, 63, 125, 249, 497, 993, 993, 993, 993, 993, 1985,},
  261. { 3, 5, 6, 6, 6, 7, 127, 127, 253, 505, 1009, 2017, 4033, 4033, 4033, 4033,},
  262. { 3, 5, 6, 7, 7, 7, 8, 255, 255, 509, 1017, 2033, 4065, 8129,16257,16257,},
  263. { 3, 5, 6, 8, 8, 8, 8, 9, 511, 511, 1021, 2041, 4081, 8161,16321,32641,},
  264. { 3, 5, 7, 8, 9, 9, 9, 9, 10, 1023, 1023, 2045, 4089, 8177,16353,32705,},
  265. { 3, 5, 7, 8, 10, 10, 10, 10, 10, 11, 2047, 2047, 4093, 8185,16369,32737,},
  266. { 3, 5, 7, 8, 10, 11, 11, 11, 11, 11, 12, 4095, 4095, 8189,16377,32753,},
  267. { 3, 5, 7, 9, 10, 12, 12, 12, 12, 12, 12, 13, 8191, 8191,16381,32761,},
  268. { 3, 5, 7, 9, 10, 12, 13, 13, 13, 13, 13, 13, 14,16383,16383,32765,},
  269. { 3, 5, 7, 9, 10, 12, 14, 14, 14, 14, 14, 14, 14, 15,32767,32767,},
  270. { 3, 5, 7, 9, 11, 12, 14, 15, 15, 15, 15, 15, 15, 15, 16,65535,},
  271. };
  272. static av_always_inline void
  273. yuv2yuvX16_c_template(const int16_t *lumFilter, const int32_t **lumSrc,
  274. int lumFilterSize, const int16_t *chrFilter,
  275. const int32_t **chrUSrc, const int32_t **chrVSrc,
  276. int chrFilterSize, const int32_t **alpSrc,
  277. uint16_t *dest[4], int dstW, int chrDstW,
  278. int big_endian, int output_bits)
  279. {
  280. //FIXME Optimize (just quickly written not optimized..)
  281. int i;
  282. int dword= output_bits == 16;
  283. uint16_t *yDest = dest[0], *uDest = dest[1], *vDest = dest[2],
  284. *aDest = CONFIG_SWSCALE_ALPHA ? dest[3] : NULL;
  285. int shift = 11 + 4*dword + 16 - output_bits;
  286. #define output_pixel(pos, val) \
  287. if (big_endian) { \
  288. if (output_bits == 16) { \
  289. AV_WB16(pos, av_clip_uint16(val >> shift)); \
  290. } else { \
  291. AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  292. } \
  293. } else { \
  294. if (output_bits == 16) { \
  295. AV_WL16(pos, av_clip_uint16(val >> shift)); \
  296. } else { \
  297. AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  298. } \
  299. }
  300. for (i = 0; i < dstW; i++) {
  301. int val = 1 << (26-output_bits + 4*dword);
  302. int j;
  303. for (j = 0; j < lumFilterSize; j++)
  304. val += (dword ? lumSrc[j][i] : ((int16_t**)lumSrc)[j][i]) * lumFilter[j];
  305. output_pixel(&yDest[i], val);
  306. }
  307. if (uDest) {
  308. for (i = 0; i < chrDstW; i++) {
  309. int u = 1 << (26-output_bits + 4*dword);
  310. int v = 1 << (26-output_bits + 4*dword);
  311. int j;
  312. for (j = 0; j < chrFilterSize; j++) {
  313. u += (dword ? chrUSrc[j][i] : ((int16_t**)chrUSrc)[j][i]) * chrFilter[j];
  314. v += (dword ? chrVSrc[j][i] : ((int16_t**)chrVSrc)[j][i]) * chrFilter[j];
  315. }
  316. output_pixel(&uDest[i], u);
  317. output_pixel(&vDest[i], v);
  318. }
  319. }
  320. if (CONFIG_SWSCALE_ALPHA && aDest) {
  321. for (i = 0; i < dstW; i++) {
  322. int val = 1 << (26-output_bits + 4*dword);
  323. int j;
  324. for (j = 0; j < lumFilterSize; j++)
  325. val += (dword ? alpSrc[j][i] : ((int16_t**)alpSrc)[j][i]) * lumFilter[j];
  326. output_pixel(&aDest[i], val);
  327. }
  328. }
  329. #undef output_pixel
  330. }
  331. #define yuv2NBPS(bits, BE_LE, is_be) \
  332. static void yuv2yuvX ## bits ## BE_LE ## _c(SwsContext *c, const int16_t *lumFilter, \
  333. const int16_t **_lumSrc, int lumFilterSize, \
  334. const int16_t *chrFilter, const int16_t **_chrUSrc, \
  335. const int16_t **_chrVSrc, \
  336. int chrFilterSize, const int16_t **_alpSrc, \
  337. uint8_t *_dest[4], int dstW, int chrDstW) \
  338. { \
  339. const int32_t **lumSrc = (const int32_t **) _lumSrc, \
  340. **chrUSrc = (const int32_t **) _chrUSrc, \
  341. **chrVSrc = (const int32_t **) _chrVSrc, \
  342. **alpSrc = (const int32_t **) _alpSrc; \
  343. yuv2yuvX16_c_template(lumFilter, lumSrc, lumFilterSize, \
  344. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  345. alpSrc, (uint16_t **) _dest, \
  346. dstW, chrDstW, is_be, bits); \
  347. }
  348. yuv2NBPS( 9, BE, 1);
  349. yuv2NBPS( 9, LE, 0);
  350. yuv2NBPS(10, BE, 1);
  351. yuv2NBPS(10, LE, 0);
  352. yuv2NBPS(16, BE, 1);
  353. yuv2NBPS(16, LE, 0);
  354. static void yuv2yuvX_c(SwsContext *c, const int16_t *lumFilter,
  355. const int16_t **lumSrc, int lumFilterSize,
  356. const int16_t *chrFilter, const int16_t **chrUSrc,
  357. const int16_t **chrVSrc,
  358. int chrFilterSize, const int16_t **alpSrc,
  359. uint8_t *dest[4], int dstW, int chrDstW,
  360. const uint8_t *lumDither, const uint8_t *chrDither)
  361. {
  362. uint8_t *yDest = dest[0], *uDest = dest[1], *vDest = dest[2],
  363. *aDest = CONFIG_SWSCALE_ALPHA ? dest[3] : NULL;
  364. int i;
  365. //FIXME Optimize (just quickly written not optimized..)
  366. for (i=0; i<dstW; i++) {
  367. int val = lumDither[i&7] << 12;
  368. int j;
  369. for (j=0; j<lumFilterSize; j++)
  370. val += lumSrc[j][i] * lumFilter[j];
  371. yDest[i]= av_clip_uint8(val>>19);
  372. }
  373. if (uDest)
  374. for (i=0; i<chrDstW; i++) {
  375. int u = chrDither[i&7] << 12;
  376. int v = chrDither[(i+3)&7] << 12;
  377. int j;
  378. for (j=0; j<chrFilterSize; j++) {
  379. u += chrUSrc[j][i] * chrFilter[j];
  380. v += chrVSrc[j][i] * chrFilter[j];
  381. }
  382. uDest[i]= av_clip_uint8(u>>19);
  383. vDest[i]= av_clip_uint8(v>>19);
  384. }
  385. if (CONFIG_SWSCALE_ALPHA && aDest)
  386. for (i=0; i<dstW; i++) {
  387. int val = lumDither[i&7] << 12;
  388. int j;
  389. for (j=0; j<lumFilterSize; j++)
  390. val += alpSrc[j][i] * lumFilter[j];
  391. aDest[i]= av_clip_uint8(val>>19);
  392. }
  393. }
  394. static void yuv2yuv1_c(SwsContext *c, const int16_t *lumSrc,
  395. const int16_t *chrUSrc, const int16_t *chrVSrc,
  396. const int16_t *alpSrc,
  397. uint8_t *dest[4], int dstW, int chrDstW,
  398. const uint8_t *lumDither, const uint8_t *chrDither)
  399. {
  400. uint8_t *yDest = dest[0], *uDest = dest[1], *vDest = dest[2],
  401. *aDest = CONFIG_SWSCALE_ALPHA ? dest[3] : NULL;
  402. int i;
  403. for (i=0; i<dstW; i++) {
  404. int val= (lumSrc[i]+lumDither[i&7])>>7;
  405. yDest[i]= av_clip_uint8(val);
  406. }
  407. if (uDest)
  408. for (i=0; i<chrDstW; i++) {
  409. int u=(chrUSrc[i]+chrDither[i&7])>>7;
  410. int v=(chrVSrc[i]+chrDither[(i+3)&7])>>7;
  411. uDest[i]= av_clip_uint8(u);
  412. vDest[i]= av_clip_uint8(v);
  413. }
  414. if (CONFIG_SWSCALE_ALPHA && aDest)
  415. for (i=0; i<dstW; i++) {
  416. int val= (alpSrc[i]+lumDither[i&7])>>7;
  417. aDest[i]= av_clip_uint8(val);
  418. }
  419. }
  420. static void yuv2nv12X_c(SwsContext *c, const int16_t *lumFilter,
  421. const int16_t **lumSrc, int lumFilterSize,
  422. const int16_t *chrFilter, const int16_t **chrUSrc,
  423. const int16_t **chrVSrc, int chrFilterSize,
  424. const int16_t **alpSrc, uint8_t *dest[4],
  425. int dstW, int chrDstW,
  426. const uint8_t *lumDither, const uint8_t *chrDither)
  427. {
  428. uint8_t *yDest = dest[0], *uDest = dest[1];
  429. enum PixelFormat dstFormat = c->dstFormat;
  430. //FIXME Optimize (just quickly written not optimized..)
  431. int i;
  432. for (i=0; i<dstW; i++) {
  433. int val = lumDither[i&7]<<12;
  434. int j;
  435. for (j=0; j<lumFilterSize; j++)
  436. val += lumSrc[j][i] * lumFilter[j];
  437. yDest[i]= av_clip_uint8(val>>19);
  438. }
  439. if (!uDest)
  440. return;
  441. if (dstFormat == PIX_FMT_NV12)
  442. for (i=0; i<chrDstW; i++) {
  443. int u = chrDither[i&7]<<12;
  444. int v = chrDither[(i+3)&7]<<12;
  445. int j;
  446. for (j=0; j<chrFilterSize; j++) {
  447. u += chrUSrc[j][i] * chrFilter[j];
  448. v += chrVSrc[j][i] * chrFilter[j];
  449. }
  450. uDest[2*i]= av_clip_uint8(u>>19);
  451. uDest[2*i+1]= av_clip_uint8(v>>19);
  452. }
  453. else
  454. for (i=0; i<chrDstW; i++) {
  455. int u = chrDither[i&7]<<12;
  456. int v = chrDither[(i+3)&7]<<12;
  457. int j;
  458. for (j=0; j<chrFilterSize; j++) {
  459. u += chrUSrc[j][i] * chrFilter[j];
  460. v += chrVSrc[j][i] * chrFilter[j];
  461. }
  462. uDest[2*i]= av_clip_uint8(v>>19);
  463. uDest[2*i+1]= av_clip_uint8(u>>19);
  464. }
  465. }
  466. #define output_pixel(pos, val) \
  467. if (target == PIX_FMT_GRAY16BE) { \
  468. AV_WB16(pos, val); \
  469. } else { \
  470. AV_WL16(pos, val); \
  471. }
  472. static av_always_inline void
  473. yuv2gray16_X_c_template(SwsContext *c, const int16_t *lumFilter,
  474. const int32_t **lumSrc, int lumFilterSize,
  475. const int16_t *chrFilter, const int32_t **chrUSrc,
  476. const int32_t **chrVSrc, int chrFilterSize,
  477. const int32_t **alpSrc, uint16_t *dest, int dstW,
  478. int y, enum PixelFormat target)
  479. {
  480. int i;
  481. for (i = 0; i < (dstW >> 1); i++) {
  482. int j;
  483. int Y1 = 1 << 14;
  484. int Y2 = 1 << 14;
  485. for (j = 0; j < lumFilterSize; j++) {
  486. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  487. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  488. }
  489. Y1 >>= 15;
  490. Y2 >>= 15;
  491. if ((Y1 | Y2) & 0x10000) {
  492. Y1 = av_clip_uint16(Y1);
  493. Y2 = av_clip_uint16(Y2);
  494. }
  495. output_pixel(&dest[i * 2 + 0], Y1);
  496. output_pixel(&dest[i * 2 + 1], Y2);
  497. }
  498. }
  499. static av_always_inline void
  500. yuv2gray16_2_c_template(SwsContext *c, const int32_t *buf[2],
  501. const int32_t *ubuf[2], const int32_t *vbuf[2],
  502. const int32_t *abuf[2], uint16_t *dest, int dstW,
  503. int yalpha, int uvalpha, int y,
  504. enum PixelFormat target)
  505. {
  506. int yalpha1 = 4095 - yalpha;
  507. int i;
  508. const int32_t *buf0 = buf[0], *buf1 = buf[1];
  509. for (i = 0; i < (dstW >> 1); i++) {
  510. int Y1 = (buf0[i * 2 ] * yalpha1 + buf1[i * 2 ] * yalpha) >> 15;
  511. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 15;
  512. output_pixel(&dest[i * 2 + 0], Y1);
  513. output_pixel(&dest[i * 2 + 1], Y2);
  514. }
  515. }
  516. static av_always_inline void
  517. yuv2gray16_1_c_template(SwsContext *c, const int32_t *buf0,
  518. const int32_t *ubuf[2], const int32_t *vbuf[2],
  519. const int32_t *abuf0, uint16_t *dest, int dstW,
  520. int uvalpha, int y, enum PixelFormat target)
  521. {
  522. int i;
  523. for (i = 0; i < (dstW >> 1); i++) {
  524. int Y1 = (buf0[i * 2 ]+4)>>3;
  525. int Y2 = (buf0[i * 2 + 1]+4)>>3;
  526. output_pixel(&dest[i * 2 + 0], Y1);
  527. output_pixel(&dest[i * 2 + 1], Y2);
  528. }
  529. }
  530. #undef output_pixel
  531. #define YUV2PACKED16WRAPPER(name, base, ext, fmt) \
  532. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  533. const int16_t **_lumSrc, int lumFilterSize, \
  534. const int16_t *chrFilter, const int16_t **_chrUSrc, \
  535. const int16_t **_chrVSrc, int chrFilterSize, \
  536. const int16_t **_alpSrc, uint8_t *_dest, int dstW, \
  537. int y) \
  538. { \
  539. const int32_t **lumSrc = (const int32_t **) _lumSrc, \
  540. **chrUSrc = (const int32_t **) _chrUSrc, \
  541. **chrVSrc = (const int32_t **) _chrVSrc, \
  542. **alpSrc = (const int32_t **) _alpSrc; \
  543. uint16_t *dest = (uint16_t *) _dest; \
  544. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  545. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  546. alpSrc, dest, dstW, y, fmt); \
  547. } \
  548. \
  549. static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
  550. const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
  551. const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
  552. int yalpha, int uvalpha, int y) \
  553. { \
  554. const int32_t **buf = (const int32_t **) _buf, \
  555. **ubuf = (const int32_t **) _ubuf, \
  556. **vbuf = (const int32_t **) _vbuf, \
  557. **abuf = (const int32_t **) _abuf; \
  558. uint16_t *dest = (uint16_t *) _dest; \
  559. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  560. dest, dstW, yalpha, uvalpha, y, fmt); \
  561. } \
  562. \
  563. static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
  564. const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
  565. const int16_t *_abuf0, uint8_t *_dest, int dstW, \
  566. int uvalpha, int y) \
  567. { \
  568. const int32_t *buf0 = (const int32_t *) _buf0, \
  569. **ubuf = (const int32_t **) _ubuf, \
  570. **vbuf = (const int32_t **) _vbuf, \
  571. *abuf0 = (const int32_t *) _abuf0; \
  572. uint16_t *dest = (uint16_t *) _dest; \
  573. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
  574. dstW, uvalpha, y, fmt); \
  575. }
  576. YUV2PACKED16WRAPPER(yuv2gray16,, LE, PIX_FMT_GRAY16LE);
  577. YUV2PACKED16WRAPPER(yuv2gray16,, BE, PIX_FMT_GRAY16BE);
  578. #define output_pixel(pos, acc) \
  579. if (target == PIX_FMT_MONOBLACK) { \
  580. pos = acc; \
  581. } else { \
  582. pos = ~acc; \
  583. }
  584. static av_always_inline void
  585. yuv2mono_X_c_template(SwsContext *c, const int16_t *lumFilter,
  586. const int16_t **lumSrc, int lumFilterSize,
  587. const int16_t *chrFilter, const int16_t **chrUSrc,
  588. const int16_t **chrVSrc, int chrFilterSize,
  589. const int16_t **alpSrc, uint8_t *dest, int dstW,
  590. int y, enum PixelFormat target)
  591. {
  592. const uint8_t * const d128=dither_8x8_220[y&7];
  593. uint8_t *g = c->table_gU[128] + c->table_gV[128];
  594. int i;
  595. int acc = 0;
  596. for (i = 0; i < dstW - 1; i += 2) {
  597. int j;
  598. int Y1 = 1 << 18;
  599. int Y2 = 1 << 18;
  600. for (j = 0; j < lumFilterSize; j++) {
  601. Y1 += lumSrc[j][i] * lumFilter[j];
  602. Y2 += lumSrc[j][i+1] * lumFilter[j];
  603. }
  604. Y1 >>= 19;
  605. Y2 >>= 19;
  606. if ((Y1 | Y2) & 0x100) {
  607. Y1 = av_clip_uint8(Y1);
  608. Y2 = av_clip_uint8(Y2);
  609. }
  610. acc += acc + g[Y1 + d128[(i + 0) & 7]];
  611. acc += acc + g[Y2 + d128[(i + 1) & 7]];
  612. if ((i & 7) == 6) {
  613. output_pixel(*dest++, acc);
  614. }
  615. }
  616. }
  617. static av_always_inline void
  618. yuv2mono_2_c_template(SwsContext *c, const int16_t *buf[2],
  619. const int16_t *ubuf[2], const int16_t *vbuf[2],
  620. const int16_t *abuf[2], uint8_t *dest, int dstW,
  621. int yalpha, int uvalpha, int y,
  622. enum PixelFormat target)
  623. {
  624. const int16_t *buf0 = buf[0], *buf1 = buf[1];
  625. const uint8_t * const d128 = dither_8x8_220[y & 7];
  626. uint8_t *g = c->table_gU[128] + c->table_gV[128];
  627. int yalpha1 = 4095 - yalpha;
  628. int i;
  629. for (i = 0; i < dstW - 7; i += 8) {
  630. int acc = g[((buf0[i ] * yalpha1 + buf1[i ] * yalpha) >> 19) + d128[0]];
  631. acc += acc + g[((buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19) + d128[1]];
  632. acc += acc + g[((buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19) + d128[2]];
  633. acc += acc + g[((buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19) + d128[3]];
  634. acc += acc + g[((buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19) + d128[4]];
  635. acc += acc + g[((buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19) + d128[5]];
  636. acc += acc + g[((buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19) + d128[6]];
  637. acc += acc + g[((buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19) + d128[7]];
  638. output_pixel(*dest++, acc);
  639. }
  640. }
  641. static av_always_inline void
  642. yuv2mono_1_c_template(SwsContext *c, const int16_t *buf0,
  643. const int16_t *ubuf[2], const int16_t *vbuf[2],
  644. const int16_t *abuf0, uint8_t *dest, int dstW,
  645. int uvalpha, int y, enum PixelFormat target)
  646. {
  647. const uint8_t * const d128 = dither_8x8_220[y & 7];
  648. uint8_t *g = c->table_gU[128] + c->table_gV[128];
  649. int i;
  650. for (i = 0; i < dstW - 7; i += 8) {
  651. int acc = g[(buf0[i ] >> 7) + d128[0]];
  652. acc += acc + g[(buf0[i + 1] >> 7) + d128[1]];
  653. acc += acc + g[(buf0[i + 2] >> 7) + d128[2]];
  654. acc += acc + g[(buf0[i + 3] >> 7) + d128[3]];
  655. acc += acc + g[(buf0[i + 4] >> 7) + d128[4]];
  656. acc += acc + g[(buf0[i + 5] >> 7) + d128[5]];
  657. acc += acc + g[(buf0[i + 6] >> 7) + d128[6]];
  658. acc += acc + g[(buf0[i + 7] >> 7) + d128[7]];
  659. output_pixel(*dest++, acc);
  660. }
  661. }
  662. #undef output_pixel
  663. #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
  664. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  665. const int16_t **lumSrc, int lumFilterSize, \
  666. const int16_t *chrFilter, const int16_t **chrUSrc, \
  667. const int16_t **chrVSrc, int chrFilterSize, \
  668. const int16_t **alpSrc, uint8_t *dest, int dstW, \
  669. int y) \
  670. { \
  671. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  672. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  673. alpSrc, dest, dstW, y, fmt); \
  674. } \
  675. \
  676. static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
  677. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  678. const int16_t *abuf[2], uint8_t *dest, int dstW, \
  679. int yalpha, int uvalpha, int y) \
  680. { \
  681. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  682. dest, dstW, yalpha, uvalpha, y, fmt); \
  683. } \
  684. \
  685. static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
  686. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  687. const int16_t *abuf0, uint8_t *dest, int dstW, \
  688. int uvalpha, int y) \
  689. { \
  690. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
  691. abuf0, dest, dstW, uvalpha, \
  692. y, fmt); \
  693. }
  694. YUV2PACKEDWRAPPER(yuv2mono,, white, PIX_FMT_MONOWHITE);
  695. YUV2PACKEDWRAPPER(yuv2mono,, black, PIX_FMT_MONOBLACK);
  696. #define output_pixels(pos, Y1, U, Y2, V) \
  697. if (target == PIX_FMT_YUYV422) { \
  698. dest[pos + 0] = Y1; \
  699. dest[pos + 1] = U; \
  700. dest[pos + 2] = Y2; \
  701. dest[pos + 3] = V; \
  702. } else { \
  703. dest[pos + 0] = U; \
  704. dest[pos + 1] = Y1; \
  705. dest[pos + 2] = V; \
  706. dest[pos + 3] = Y2; \
  707. }
  708. static av_always_inline void
  709. yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
  710. const int16_t **lumSrc, int lumFilterSize,
  711. const int16_t *chrFilter, const int16_t **chrUSrc,
  712. const int16_t **chrVSrc, int chrFilterSize,
  713. const int16_t **alpSrc, uint8_t *dest, int dstW,
  714. int y, enum PixelFormat target)
  715. {
  716. int i;
  717. for (i = 0; i < (dstW >> 1); i++) {
  718. int j;
  719. int Y1 = 1 << 18;
  720. int Y2 = 1 << 18;
  721. int U = 1 << 18;
  722. int V = 1 << 18;
  723. for (j = 0; j < lumFilterSize; j++) {
  724. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  725. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  726. }
  727. for (j = 0; j < chrFilterSize; j++) {
  728. U += chrUSrc[j][i] * chrFilter[j];
  729. V += chrVSrc[j][i] * chrFilter[j];
  730. }
  731. Y1 >>= 19;
  732. Y2 >>= 19;
  733. U >>= 19;
  734. V >>= 19;
  735. if ((Y1 | Y2 | U | V) & 0x100) {
  736. Y1 = av_clip_uint8(Y1);
  737. Y2 = av_clip_uint8(Y2);
  738. U = av_clip_uint8(U);
  739. V = av_clip_uint8(V);
  740. }
  741. output_pixels(4*i, Y1, U, Y2, V);
  742. }
  743. }
  744. static av_always_inline void
  745. yuv2422_2_c_template(SwsContext *c, const int16_t *buf[2],
  746. const int16_t *ubuf[2], const int16_t *vbuf[2],
  747. const int16_t *abuf[2], uint8_t *dest, int dstW,
  748. int yalpha, int uvalpha, int y,
  749. enum PixelFormat target)
  750. {
  751. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  752. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  753. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  754. int yalpha1 = 4095 - yalpha;
  755. int uvalpha1 = 4095 - uvalpha;
  756. int i;
  757. for (i = 0; i < (dstW >> 1); i++) {
  758. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
  759. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
  760. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
  761. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
  762. output_pixels(i * 4, Y1, U, Y2, V);
  763. }
  764. }
  765. static av_always_inline void
  766. yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
  767. const int16_t *ubuf[2], const int16_t *vbuf[2],
  768. const int16_t *abuf0, uint8_t *dest, int dstW,
  769. int uvalpha, int y, enum PixelFormat target)
  770. {
  771. const int16_t *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  772. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  773. int i;
  774. if (uvalpha < 2048) {
  775. for (i = 0; i < (dstW >> 1); i++) {
  776. int Y1 = buf0[i * 2] >> 7;
  777. int Y2 = buf0[i * 2 + 1] >> 7;
  778. int U = ubuf1[i] >> 7;
  779. int V = vbuf1[i] >> 7;
  780. output_pixels(i * 4, Y1, U, Y2, V);
  781. }
  782. } else {
  783. for (i = 0; i < (dstW >> 1); i++) {
  784. int Y1 = buf0[i * 2] >> 7;
  785. int Y2 = buf0[i * 2 + 1] >> 7;
  786. int U = (ubuf0[i] + ubuf1[i]) >> 8;
  787. int V = (vbuf0[i] + vbuf1[i]) >> 8;
  788. output_pixels(i * 4, Y1, U, Y2, V);
  789. }
  790. }
  791. }
  792. #undef output_pixels
  793. YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, PIX_FMT_YUYV422);
  794. YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, PIX_FMT_UYVY422);
  795. #define R_B ((target == PIX_FMT_RGB48LE || target == PIX_FMT_RGB48BE) ? R : B)
  796. #define B_R ((target == PIX_FMT_RGB48LE || target == PIX_FMT_RGB48BE) ? B : R)
  797. #define output_pixel(pos, val) \
  798. if (isBE(target)) { \
  799. AV_WB16(pos, val); \
  800. } else { \
  801. AV_WL16(pos, val); \
  802. }
  803. static av_always_inline void
  804. yuv2rgb48_X_c_template(SwsContext *c, const int16_t *lumFilter,
  805. const int32_t **lumSrc, int lumFilterSize,
  806. const int16_t *chrFilter, const int32_t **chrUSrc,
  807. const int32_t **chrVSrc, int chrFilterSize,
  808. const int32_t **alpSrc, uint16_t *dest, int dstW,
  809. int y, enum PixelFormat target)
  810. {
  811. int i;
  812. for (i = 0; i < (dstW >> 1); i++) {
  813. int j;
  814. int Y1 = 0;
  815. int Y2 = 0;
  816. int U = -128 << 23; // 19
  817. int V = -128 << 23;
  818. int R, G, B;
  819. for (j = 0; j < lumFilterSize; j++) {
  820. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  821. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  822. }
  823. for (j = 0; j < chrFilterSize; j++) {
  824. U += chrUSrc[j][i] * chrFilter[j];
  825. V += chrVSrc[j][i] * chrFilter[j];
  826. }
  827. // 8bit: 12+15=27; 16-bit: 12+19=31
  828. Y1 >>= 14; // 10
  829. Y2 >>= 14;
  830. U >>= 14;
  831. V >>= 14;
  832. // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
  833. Y1 -= c->yuv2rgb_y_offset;
  834. Y2 -= c->yuv2rgb_y_offset;
  835. Y1 *= c->yuv2rgb_y_coeff;
  836. Y2 *= c->yuv2rgb_y_coeff;
  837. Y1 += 1 << 13; // 21
  838. Y2 += 1 << 13;
  839. // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
  840. R = V * c->yuv2rgb_v2r_coeff;
  841. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  842. B = U * c->yuv2rgb_u2b_coeff;
  843. // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
  844. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  845. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  846. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  847. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  848. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  849. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  850. dest += 6;
  851. }
  852. }
  853. static av_always_inline void
  854. yuv2rgb48_2_c_template(SwsContext *c, const int32_t *buf[2],
  855. const int32_t *ubuf[2], const int32_t *vbuf[2],
  856. const int32_t *abuf[2], uint16_t *dest, int dstW,
  857. int yalpha, int uvalpha, int y,
  858. enum PixelFormat target)
  859. {
  860. const int32_t *buf0 = buf[0], *buf1 = buf[1],
  861. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  862. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  863. int yalpha1 = 4095 - yalpha;
  864. int uvalpha1 = 4095 - uvalpha;
  865. int i;
  866. for (i = 0; i < (dstW >> 1); i++) {
  867. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 14;
  868. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 14;
  869. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha + (-128 << 23)) >> 14;
  870. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha + (-128 << 23)) >> 14;
  871. int R, G, B;
  872. Y1 -= c->yuv2rgb_y_offset;
  873. Y2 -= c->yuv2rgb_y_offset;
  874. Y1 *= c->yuv2rgb_y_coeff;
  875. Y2 *= c->yuv2rgb_y_coeff;
  876. Y1 += 1 << 13;
  877. Y2 += 1 << 13;
  878. R = V * c->yuv2rgb_v2r_coeff;
  879. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  880. B = U * c->yuv2rgb_u2b_coeff;
  881. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  882. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  883. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  884. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  885. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  886. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  887. dest += 6;
  888. }
  889. }
  890. static av_always_inline void
  891. yuv2rgb48_1_c_template(SwsContext *c, const int32_t *buf0,
  892. const int32_t *ubuf[2], const int32_t *vbuf[2],
  893. const int32_t *abuf0, uint16_t *dest, int dstW,
  894. int uvalpha, int y, enum PixelFormat target)
  895. {
  896. const int32_t *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  897. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  898. int i;
  899. if (uvalpha < 2048) {
  900. for (i = 0; i < (dstW >> 1); i++) {
  901. int Y1 = (buf0[i * 2] ) >> 2;
  902. int Y2 = (buf0[i * 2 + 1]) >> 2;
  903. int U = (ubuf0[i] + (-128 << 11)) >> 2;
  904. int V = (vbuf0[i] + (-128 << 11)) >> 2;
  905. int R, G, B;
  906. Y1 -= c->yuv2rgb_y_offset;
  907. Y2 -= c->yuv2rgb_y_offset;
  908. Y1 *= c->yuv2rgb_y_coeff;
  909. Y2 *= c->yuv2rgb_y_coeff;
  910. Y1 += 1 << 13;
  911. Y2 += 1 << 13;
  912. R = V * c->yuv2rgb_v2r_coeff;
  913. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  914. B = U * c->yuv2rgb_u2b_coeff;
  915. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  916. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  917. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  918. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  919. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  920. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  921. dest += 6;
  922. }
  923. } else {
  924. for (i = 0; i < (dstW >> 1); i++) {
  925. int Y1 = (buf0[i * 2] ) >> 2;
  926. int Y2 = (buf0[i * 2 + 1]) >> 2;
  927. int U = (ubuf0[i] + ubuf1[i] + (-128 << 12)) >> 3;
  928. int V = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
  929. int R, G, B;
  930. Y1 -= c->yuv2rgb_y_offset;
  931. Y2 -= c->yuv2rgb_y_offset;
  932. Y1 *= c->yuv2rgb_y_coeff;
  933. Y2 *= c->yuv2rgb_y_coeff;
  934. Y1 += 1 << 13;
  935. Y2 += 1 << 13;
  936. R = V * c->yuv2rgb_v2r_coeff;
  937. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  938. B = U * c->yuv2rgb_u2b_coeff;
  939. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  940. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  941. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  942. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  943. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  944. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  945. dest += 6;
  946. }
  947. }
  948. }
  949. #undef output_pixel
  950. #undef r_b
  951. #undef b_r
  952. YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48be, PIX_FMT_RGB48BE);
  953. YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48le, PIX_FMT_RGB48LE);
  954. YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48be, PIX_FMT_BGR48BE);
  955. YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48le, PIX_FMT_BGR48LE);
  956. static av_always_inline void
  957. yuv2rgb_write(uint8_t *_dest, int i, int Y1, int Y2,
  958. int U, int V, int A1, int A2,
  959. const void *_r, const void *_g, const void *_b, int y,
  960. enum PixelFormat target, int hasAlpha)
  961. {
  962. if (target == PIX_FMT_ARGB || target == PIX_FMT_RGBA ||
  963. target == PIX_FMT_ABGR || target == PIX_FMT_BGRA) {
  964. uint32_t *dest = (uint32_t *) _dest;
  965. const uint32_t *r = (const uint32_t *) _r;
  966. const uint32_t *g = (const uint32_t *) _g;
  967. const uint32_t *b = (const uint32_t *) _b;
  968. #if CONFIG_SMALL
  969. int sh = hasAlpha ? ((fmt == PIX_FMT_RGB32_1 || fmt == PIX_FMT_BGR32_1) ? 0 : 24) : 0;
  970. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (hasAlpha ? A1 << sh : 0);
  971. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (hasAlpha ? A2 << sh : 0);
  972. #else
  973. if (hasAlpha) {
  974. int sh = (target == PIX_FMT_RGB32_1 || target == PIX_FMT_BGR32_1) ? 0 : 24;
  975. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
  976. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
  977. } else {
  978. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
  979. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
  980. }
  981. #endif
  982. } else if (target == PIX_FMT_RGB24 || target == PIX_FMT_BGR24) {
  983. uint8_t *dest = (uint8_t *) _dest;
  984. const uint8_t *r = (const uint8_t *) _r;
  985. const uint8_t *g = (const uint8_t *) _g;
  986. const uint8_t *b = (const uint8_t *) _b;
  987. #define r_b ((target == PIX_FMT_RGB24) ? r : b)
  988. #define b_r ((target == PIX_FMT_RGB24) ? b : r)
  989. dest[i * 6 + 0] = r_b[Y1];
  990. dest[i * 6 + 1] = g[Y1];
  991. dest[i * 6 + 2] = b_r[Y1];
  992. dest[i * 6 + 3] = r_b[Y2];
  993. dest[i * 6 + 4] = g[Y2];
  994. dest[i * 6 + 5] = b_r[Y2];
  995. #undef r_b
  996. #undef b_r
  997. } else if (target == PIX_FMT_RGB565 || target == PIX_FMT_BGR565 ||
  998. target == PIX_FMT_RGB555 || target == PIX_FMT_BGR555 ||
  999. target == PIX_FMT_RGB444 || target == PIX_FMT_BGR444) {
  1000. uint16_t *dest = (uint16_t *) _dest;
  1001. const uint16_t *r = (const uint16_t *) _r;
  1002. const uint16_t *g = (const uint16_t *) _g;
  1003. const uint16_t *b = (const uint16_t *) _b;
  1004. int dr1, dg1, db1, dr2, dg2, db2;
  1005. if (target == PIX_FMT_RGB565 || target == PIX_FMT_BGR565) {
  1006. dr1 = dither_2x2_8[ y & 1 ][0];
  1007. dg1 = dither_2x2_4[ y & 1 ][0];
  1008. db1 = dither_2x2_8[(y & 1) ^ 1][0];
  1009. dr2 = dither_2x2_8[ y & 1 ][1];
  1010. dg2 = dither_2x2_4[ y & 1 ][1];
  1011. db2 = dither_2x2_8[(y & 1) ^ 1][1];
  1012. } else if (target == PIX_FMT_RGB555 || target == PIX_FMT_BGR555) {
  1013. dr1 = dither_2x2_8[ y & 1 ][0];
  1014. dg1 = dither_2x2_8[ y & 1 ][1];
  1015. db1 = dither_2x2_8[(y & 1) ^ 1][0];
  1016. dr2 = dither_2x2_8[ y & 1 ][1];
  1017. dg2 = dither_2x2_8[ y & 1 ][0];
  1018. db2 = dither_2x2_8[(y & 1) ^ 1][1];
  1019. } else {
  1020. dr1 = dither_4x4_16[ y & 3 ][0];
  1021. dg1 = dither_4x4_16[ y & 3 ][1];
  1022. db1 = dither_4x4_16[(y & 3) ^ 3][0];
  1023. dr2 = dither_4x4_16[ y & 3 ][1];
  1024. dg2 = dither_4x4_16[ y & 3 ][0];
  1025. db2 = dither_4x4_16[(y & 3) ^ 3][1];
  1026. }
  1027. dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
  1028. dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
  1029. } else /* 8/4-bit */ {
  1030. uint8_t *dest = (uint8_t *) _dest;
  1031. const uint8_t *r = (const uint8_t *) _r;
  1032. const uint8_t *g = (const uint8_t *) _g;
  1033. const uint8_t *b = (const uint8_t *) _b;
  1034. int dr1, dg1, db1, dr2, dg2, db2;
  1035. if (target == PIX_FMT_RGB8 || target == PIX_FMT_BGR8) {
  1036. const uint8_t * const d64 = dither_8x8_73[y & 7];
  1037. const uint8_t * const d32 = dither_8x8_32[y & 7];
  1038. dr1 = dg1 = d32[(i * 2 + 0) & 7];
  1039. db1 = d64[(i * 2 + 0) & 7];
  1040. dr2 = dg2 = d32[(i * 2 + 1) & 7];
  1041. db2 = d64[(i * 2 + 1) & 7];
  1042. } else {
  1043. const uint8_t * const d64 = dither_8x8_73 [y & 7];
  1044. const uint8_t * const d128 = dither_8x8_220[y & 7];
  1045. dr1 = db1 = d128[(i * 2 + 0) & 7];
  1046. dg1 = d64[(i * 2 + 0) & 7];
  1047. dr2 = db2 = d128[(i * 2 + 1) & 7];
  1048. dg2 = d64[(i * 2 + 1) & 7];
  1049. }
  1050. if (target == PIX_FMT_RGB4 || target == PIX_FMT_BGR4) {
  1051. dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
  1052. ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
  1053. } else {
  1054. dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
  1055. dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
  1056. }
  1057. }
  1058. }
  1059. static av_always_inline void
  1060. yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
  1061. const int16_t **lumSrc, int lumFilterSize,
  1062. const int16_t *chrFilter, const int16_t **chrUSrc,
  1063. const int16_t **chrVSrc, int chrFilterSize,
  1064. const int16_t **alpSrc, uint8_t *dest, int dstW,
  1065. int y, enum PixelFormat target, int hasAlpha)
  1066. {
  1067. int i;
  1068. for (i = 0; i < (dstW >> 1); i++) {
  1069. int j;
  1070. int Y1 = 1 << 18;
  1071. int Y2 = 1 << 18;
  1072. int U = 1 << 18;
  1073. int V = 1 << 18;
  1074. int av_unused A1, A2;
  1075. const void *r, *g, *b;
  1076. for (j = 0; j < lumFilterSize; j++) {
  1077. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  1078. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  1079. }
  1080. for (j = 0; j < chrFilterSize; j++) {
  1081. U += chrUSrc[j][i] * chrFilter[j];
  1082. V += chrVSrc[j][i] * chrFilter[j];
  1083. }
  1084. Y1 >>= 19;
  1085. Y2 >>= 19;
  1086. U >>= 19;
  1087. V >>= 19;
  1088. if ((Y1 | Y2 | U | V) & 0x100) {
  1089. Y1 = av_clip_uint8(Y1);
  1090. Y2 = av_clip_uint8(Y2);
  1091. U = av_clip_uint8(U);
  1092. V = av_clip_uint8(V);
  1093. }
  1094. if (hasAlpha) {
  1095. A1 = 1 << 18;
  1096. A2 = 1 << 18;
  1097. for (j = 0; j < lumFilterSize; j++) {
  1098. A1 += alpSrc[j][i * 2 ] * lumFilter[j];
  1099. A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
  1100. }
  1101. A1 >>= 19;
  1102. A2 >>= 19;
  1103. if ((A1 | A2) & 0x100) {
  1104. A1 = av_clip_uint8(A1);
  1105. A2 = av_clip_uint8(A2);
  1106. }
  1107. }
  1108. /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/
  1109. r = c->table_rV[V];
  1110. g = (c->table_gU[U] + c->table_gV[V]);
  1111. b = c->table_bU[U];
  1112. yuv2rgb_write(dest, i, Y1, Y2, U, V, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1113. r, g, b, y, target, hasAlpha);
  1114. }
  1115. }
  1116. static av_always_inline void
  1117. yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
  1118. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1119. const int16_t *abuf[2], uint8_t *dest, int dstW,
  1120. int yalpha, int uvalpha, int y,
  1121. enum PixelFormat target, int hasAlpha)
  1122. {
  1123. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  1124. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  1125. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
  1126. *abuf0 = hasAlpha ? abuf[0] : NULL,
  1127. *abuf1 = hasAlpha ? abuf[1] : NULL;
  1128. int yalpha1 = 4095 - yalpha;
  1129. int uvalpha1 = 4095 - uvalpha;
  1130. int i;
  1131. for (i = 0; i < (dstW >> 1); i++) {
  1132. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
  1133. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
  1134. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
  1135. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
  1136. int A1, A2;
  1137. const void *r = c->table_rV[V],
  1138. *g = (c->table_gU[U] + c->table_gV[V]),
  1139. *b = c->table_bU[U];
  1140. if (hasAlpha) {
  1141. A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 19;
  1142. A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
  1143. }
  1144. yuv2rgb_write(dest, i, Y1, Y2, U, V, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1145. r, g, b, y, target, hasAlpha);
  1146. }
  1147. }
  1148. static av_always_inline void
  1149. yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
  1150. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1151. const int16_t *abuf0, uint8_t *dest, int dstW,
  1152. int uvalpha, int y, enum PixelFormat target,
  1153. int hasAlpha)
  1154. {
  1155. const int16_t *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  1156. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  1157. int i;
  1158. if (uvalpha < 2048) {
  1159. for (i = 0; i < (dstW >> 1); i++) {
  1160. int Y1 = buf0[i * 2] >> 7;
  1161. int Y2 = buf0[i * 2 + 1] >> 7;
  1162. int U = ubuf1[i] >> 7;
  1163. int V = vbuf1[i] >> 7;
  1164. int A1, A2;
  1165. const void *r = c->table_rV[V],
  1166. *g = (c->table_gU[U] + c->table_gV[V]),
  1167. *b = c->table_bU[U];
  1168. if (hasAlpha) {
  1169. A1 = abuf0[i * 2 ] >> 7;
  1170. A2 = abuf0[i * 2 + 1] >> 7;
  1171. }
  1172. yuv2rgb_write(dest, i, Y1, Y2, U, V, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1173. r, g, b, y, target, hasAlpha);
  1174. }
  1175. } else {
  1176. for (i = 0; i < (dstW >> 1); i++) {
  1177. int Y1 = buf0[i * 2] >> 7;
  1178. int Y2 = buf0[i * 2 + 1] >> 7;
  1179. int U = (ubuf0[i] + ubuf1[i]) >> 8;
  1180. int V = (vbuf0[i] + vbuf1[i]) >> 8;
  1181. int A1, A2;
  1182. const void *r = c->table_rV[V],
  1183. *g = (c->table_gU[U] + c->table_gV[V]),
  1184. *b = c->table_bU[U];
  1185. if (hasAlpha) {
  1186. A1 = abuf0[i * 2 ] >> 7;
  1187. A2 = abuf0[i * 2 + 1] >> 7;
  1188. }
  1189. yuv2rgb_write(dest, i, Y1, Y2, U, V, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1190. r, g, b, y, target, hasAlpha);
  1191. }
  1192. }
  1193. }
  1194. #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
  1195. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  1196. const int16_t **lumSrc, int lumFilterSize, \
  1197. const int16_t *chrFilter, const int16_t **chrUSrc, \
  1198. const int16_t **chrVSrc, int chrFilterSize, \
  1199. const int16_t **alpSrc, uint8_t *dest, int dstW, \
  1200. int y) \
  1201. { \
  1202. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  1203. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  1204. alpSrc, dest, dstW, y, fmt, hasAlpha); \
  1205. }
  1206. #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
  1207. YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
  1208. static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
  1209. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  1210. const int16_t *abuf[2], uint8_t *dest, int dstW, \
  1211. int yalpha, int uvalpha, int y) \
  1212. { \
  1213. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  1214. dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
  1215. } \
  1216. \
  1217. static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
  1218. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  1219. const int16_t *abuf0, uint8_t *dest, int dstW, \
  1220. int uvalpha, int y) \
  1221. { \
  1222. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
  1223. dstW, uvalpha, y, fmt, hasAlpha); \
  1224. }
  1225. #if CONFIG_SMALL
  1226. YUV2RGBWRAPPER(yuv2rgb,, 32_1, PIX_FMT_RGB32_1, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1227. YUV2RGBWRAPPER(yuv2rgb,, 32, PIX_FMT_RGB32, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1228. #else
  1229. #if CONFIG_SWSCALE_ALPHA
  1230. YUV2RGBWRAPPER(yuv2rgb,, a32_1, PIX_FMT_RGB32_1, 1);
  1231. YUV2RGBWRAPPER(yuv2rgb,, a32, PIX_FMT_RGB32, 1);
  1232. #endif
  1233. YUV2RGBWRAPPER(yuv2rgb,, x32_1, PIX_FMT_RGB32_1, 0);
  1234. YUV2RGBWRAPPER(yuv2rgb,, x32, PIX_FMT_RGB32, 0);
  1235. #endif
  1236. YUV2RGBWRAPPER(yuv2, rgb, rgb24, PIX_FMT_RGB24, 0);
  1237. YUV2RGBWRAPPER(yuv2, rgb, bgr24, PIX_FMT_BGR24, 0);
  1238. YUV2RGBWRAPPER(yuv2rgb,, 16, PIX_FMT_RGB565, 0);
  1239. YUV2RGBWRAPPER(yuv2rgb,, 15, PIX_FMT_RGB555, 0);
  1240. YUV2RGBWRAPPER(yuv2rgb,, 12, PIX_FMT_RGB444, 0);
  1241. YUV2RGBWRAPPER(yuv2rgb,, 8, PIX_FMT_RGB8, 0);
  1242. YUV2RGBWRAPPER(yuv2rgb,, 4, PIX_FMT_RGB4, 0);
  1243. YUV2RGBWRAPPER(yuv2rgb,, 4b, PIX_FMT_RGB4_BYTE, 0);
  1244. static av_always_inline void
  1245. yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
  1246. const int16_t **lumSrc, int lumFilterSize,
  1247. const int16_t *chrFilter, const int16_t **chrUSrc,
  1248. const int16_t **chrVSrc, int chrFilterSize,
  1249. const int16_t **alpSrc, uint8_t *dest,
  1250. int dstW, int y, enum PixelFormat target, int hasAlpha)
  1251. {
  1252. int i;
  1253. int step = (target == PIX_FMT_RGB24 || target == PIX_FMT_BGR24) ? 3 : 4;
  1254. for (i = 0; i < dstW; i++) {
  1255. int j;
  1256. int Y = 1<<9;
  1257. int U = (1<<9)-(128 << 19);
  1258. int V = (1<<9)-(128 << 19);
  1259. int av_unused A;
  1260. int R, G, B;
  1261. for (j = 0; j < lumFilterSize; j++) {
  1262. Y += lumSrc[j][i] * lumFilter[j];
  1263. }
  1264. for (j = 0; j < chrFilterSize; j++) {
  1265. U += chrUSrc[j][i] * chrFilter[j];
  1266. V += chrVSrc[j][i] * chrFilter[j];
  1267. }
  1268. Y >>= 10;
  1269. U >>= 10;
  1270. V >>= 10;
  1271. if (hasAlpha) {
  1272. A = 1 << 18;
  1273. for (j = 0; j < lumFilterSize; j++) {
  1274. A += alpSrc[j][i] * lumFilter[j];
  1275. }
  1276. A >>= 19;
  1277. if (A & 0x100)
  1278. A = av_clip_uint8(A);
  1279. }
  1280. Y -= c->yuv2rgb_y_offset;
  1281. Y *= c->yuv2rgb_y_coeff;
  1282. Y += 1 << 21;
  1283. R = Y + V*c->yuv2rgb_v2r_coeff;
  1284. G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
  1285. B = Y + U*c->yuv2rgb_u2b_coeff;
  1286. if ((R | G | B) & 0xC0000000) {
  1287. R = av_clip_uintp2(R, 30);
  1288. G = av_clip_uintp2(G, 30);
  1289. B = av_clip_uintp2(B, 30);
  1290. }
  1291. switch(target) {
  1292. case PIX_FMT_ARGB:
  1293. dest[0] = hasAlpha ? A : 255;
  1294. dest[1] = R >> 22;
  1295. dest[2] = G >> 22;
  1296. dest[3] = B >> 22;
  1297. break;
  1298. case PIX_FMT_RGB24:
  1299. dest[0] = R >> 22;
  1300. dest[1] = G >> 22;
  1301. dest[2] = B >> 22;
  1302. break;
  1303. case PIX_FMT_RGBA:
  1304. dest[0] = R >> 22;
  1305. dest[1] = G >> 22;
  1306. dest[2] = B >> 22;
  1307. dest[3] = hasAlpha ? A : 255;
  1308. break;
  1309. case PIX_FMT_ABGR:
  1310. dest[0] = hasAlpha ? A : 255;
  1311. dest[1] = B >> 22;
  1312. dest[2] = G >> 22;
  1313. dest[3] = R >> 22;
  1314. break;
  1315. case PIX_FMT_BGR24:
  1316. dest[0] = B >> 22;
  1317. dest[1] = G >> 22;
  1318. dest[2] = R >> 22;
  1319. break;
  1320. case PIX_FMT_BGRA:
  1321. dest[0] = B >> 22;
  1322. dest[1] = G >> 22;
  1323. dest[2] = R >> 22;
  1324. dest[3] = hasAlpha ? A : 255;
  1325. break;
  1326. }
  1327. dest += step;
  1328. }
  1329. }
  1330. #if CONFIG_SMALL
  1331. YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, PIX_FMT_BGRA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1332. YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, PIX_FMT_ABGR, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1333. YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, PIX_FMT_RGBA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1334. YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, PIX_FMT_ARGB, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1335. #else
  1336. #if CONFIG_SWSCALE_ALPHA
  1337. YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, PIX_FMT_BGRA, 1);
  1338. YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, PIX_FMT_ABGR, 1);
  1339. YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, PIX_FMT_RGBA, 1);
  1340. YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, PIX_FMT_ARGB, 1);
  1341. #endif
  1342. YUV2RGBWRAPPERX(yuv2, rgb_full, bgrx32_full, PIX_FMT_BGRA, 0);
  1343. YUV2RGBWRAPPERX(yuv2, rgb_full, xbgr32_full, PIX_FMT_ABGR, 0);
  1344. YUV2RGBWRAPPERX(yuv2, rgb_full, rgbx32_full, PIX_FMT_RGBA, 0);
  1345. YUV2RGBWRAPPERX(yuv2, rgb_full, xrgb32_full, PIX_FMT_ARGB, 0);
  1346. #endif
  1347. YUV2RGBWRAPPERX(yuv2, rgb_full, bgr24_full, PIX_FMT_BGR24, 0);
  1348. YUV2RGBWRAPPERX(yuv2, rgb_full, rgb24_full, PIX_FMT_RGB24, 0);
  1349. static av_always_inline void fillPlane(uint8_t* plane, int stride,
  1350. int width, int height,
  1351. int y, uint8_t val)
  1352. {
  1353. int i;
  1354. uint8_t *ptr = plane + stride*y;
  1355. for (i=0; i<height; i++) {
  1356. memset(ptr, val, width);
  1357. ptr += stride;
  1358. }
  1359. }
  1360. #define input_pixel(pos) (isBE(origin) ? AV_RB16(pos) : AV_RL16(pos))
  1361. #define r ((origin == PIX_FMT_BGR48BE || origin == PIX_FMT_BGR48LE) ? b_r : r_b)
  1362. #define b ((origin == PIX_FMT_BGR48BE || origin == PIX_FMT_BGR48LE) ? r_b : b_r)
  1363. static av_always_inline void
  1364. rgb48ToY_c_template(uint16_t *dst, const uint16_t *src, int width,
  1365. enum PixelFormat origin)
  1366. {
  1367. int i;
  1368. for (i = 0; i < width; i++) {
  1369. unsigned int r_b = input_pixel(&src[i*3+0]);
  1370. unsigned int g = input_pixel(&src[i*3+1]);
  1371. unsigned int b_r = input_pixel(&src[i*3+2]);
  1372. dst[i] = (RY*r + GY*g + BY*b + (0x2001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1373. }
  1374. }
  1375. static av_always_inline void
  1376. rgb48ToUV_c_template(uint16_t *dstU, uint16_t *dstV,
  1377. const uint16_t *src1, const uint16_t *src2,
  1378. int width, enum PixelFormat origin)
  1379. {
  1380. int i;
  1381. assert(src1==src2);
  1382. for (i = 0; i < width; i++) {
  1383. int r_b = input_pixel(&src1[i*3+0]);
  1384. int g = input_pixel(&src1[i*3+1]);
  1385. int b_r = input_pixel(&src1[i*3+2]);
  1386. dstU[i] = (RU*r + GU*g + BU*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1387. dstV[i] = (RV*r + GV*g + BV*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1388. }
  1389. }
  1390. static av_always_inline void
  1391. rgb48ToUV_half_c_template(uint16_t *dstU, uint16_t *dstV,
  1392. const uint16_t *src1, const uint16_t *src2,
  1393. int width, enum PixelFormat origin)
  1394. {
  1395. int i;
  1396. assert(src1==src2);
  1397. for (i = 0; i < width; i++) {
  1398. int r_b = (input_pixel(&src1[6 * i + 0]) + input_pixel(&src1[6 * i + 3]) + 1) >> 1;
  1399. int g = (input_pixel(&src1[6 * i + 1]) + input_pixel(&src1[6 * i + 4]) + 1) >> 1;
  1400. int b_r = (input_pixel(&src1[6 * i + 2]) + input_pixel(&src1[6 * i + 5]) + 1) >> 1;
  1401. dstU[i]= (RU*r + GU*g + BU*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1402. dstV[i]= (RV*r + GV*g + BV*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1403. }
  1404. }
  1405. #undef r
  1406. #undef b
  1407. #undef input_pixel
  1408. #define rgb48funcs(pattern, BE_LE, origin) \
  1409. static void pattern ## 48 ## BE_LE ## ToY_c(uint8_t *_dst, const uint8_t *_src, \
  1410. int width, uint32_t *unused) \
  1411. { \
  1412. const uint16_t *src = (const uint16_t *) _src; \
  1413. uint16_t *dst = (uint16_t *) _dst; \
  1414. rgb48ToY_c_template(dst, src, width, origin); \
  1415. } \
  1416. \
  1417. static void pattern ## 48 ## BE_LE ## ToUV_c(uint8_t *_dstU, uint8_t *_dstV, \
  1418. const uint8_t *_src1, const uint8_t *_src2, \
  1419. int width, uint32_t *unused) \
  1420. { \
  1421. const uint16_t *src1 = (const uint16_t *) _src1, \
  1422. *src2 = (const uint16_t *) _src2; \
  1423. uint16_t *dstU = (uint16_t *) _dstU, *dstV = (uint16_t *) _dstV; \
  1424. rgb48ToUV_c_template(dstU, dstV, src1, src2, width, origin); \
  1425. } \
  1426. \
  1427. static void pattern ## 48 ## BE_LE ## ToUV_half_c(uint8_t *_dstU, uint8_t *_dstV, \
  1428. const uint8_t *_src1, const uint8_t *_src2, \
  1429. int width, uint32_t *unused) \
  1430. { \
  1431. const uint16_t *src1 = (const uint16_t *) _src1, \
  1432. *src2 = (const uint16_t *) _src2; \
  1433. uint16_t *dstU = (uint16_t *) _dstU, *dstV = (uint16_t *) _dstV; \
  1434. rgb48ToUV_half_c_template(dstU, dstV, src1, src2, width, origin); \
  1435. }
  1436. rgb48funcs(rgb, LE, PIX_FMT_RGB48LE);
  1437. rgb48funcs(rgb, BE, PIX_FMT_RGB48BE);
  1438. rgb48funcs(bgr, LE, PIX_FMT_BGR48LE);
  1439. rgb48funcs(bgr, BE, PIX_FMT_BGR48BE);
  1440. #define input_pixel(i) ((origin == PIX_FMT_RGBA || origin == PIX_FMT_BGRA || \
  1441. origin == PIX_FMT_ARGB || origin == PIX_FMT_ABGR) ? AV_RN32A(&src[(i)*4]) : \
  1442. (isBE(origin) ? AV_RB16(&src[(i)*2]) : AV_RL16(&src[(i)*2])))
  1443. static av_always_inline void
  1444. rgb16_32ToY_c_template(int16_t *dst, const uint8_t *src,
  1445. int width, enum PixelFormat origin,
  1446. int shr, int shg, int shb, int shp,
  1447. int maskr, int maskg, int maskb,
  1448. int rsh, int gsh, int bsh, int S)
  1449. {
  1450. const int ry = RY << rsh, gy = GY << gsh, by = BY << bsh,
  1451. rnd = (32<<((S)-1)) + (1<<(S-7));
  1452. int i;
  1453. for (i = 0; i < width; i++) {
  1454. int px = input_pixel(i) >> shp;
  1455. int b = (px & maskb) >> shb;
  1456. int g = (px & maskg) >> shg;
  1457. int r = (px & maskr) >> shr;
  1458. dst[i] = (ry * r + gy * g + by * b + rnd) >> ((S)-6);
  1459. }
  1460. }
  1461. static av_always_inline void
  1462. rgb16_32ToUV_c_template(int16_t *dstU, int16_t *dstV,
  1463. const uint8_t *src, int width,
  1464. enum PixelFormat origin,
  1465. int shr, int shg, int shb, int shp,
  1466. int maskr, int maskg, int maskb,
  1467. int rsh, int gsh, int bsh, int S)
  1468. {
  1469. const int ru = RU << rsh, gu = GU << gsh, bu = BU << bsh,
  1470. rv = RV << rsh, gv = GV << gsh, bv = BV << bsh,
  1471. rnd = (256<<((S)-1)) + (1<<(S-7));
  1472. int i;
  1473. for (i = 0; i < width; i++) {
  1474. int px = input_pixel(i) >> shp;
  1475. int b = (px & maskb) >> shb;
  1476. int g = (px & maskg) >> shg;
  1477. int r = (px & maskr) >> shr;
  1478. dstU[i] = (ru * r + gu * g + bu * b + rnd) >> ((S)-6);
  1479. dstV[i] = (rv * r + gv * g + bv * b + rnd) >> ((S)-6);
  1480. }
  1481. }
  1482. static av_always_inline void
  1483. rgb16_32ToUV_half_c_template(int16_t *dstU, int16_t *dstV,
  1484. const uint8_t *src, int width,
  1485. enum PixelFormat origin,
  1486. int shr, int shg, int shb, int shp,
  1487. int maskr, int maskg, int maskb,
  1488. int rsh, int gsh, int bsh, int S)
  1489. {
  1490. const int ru = RU << rsh, gu = GU << gsh, bu = BU << bsh,
  1491. rv = RV << rsh, gv = GV << gsh, bv = BV << bsh,
  1492. rnd = (256U<<(S)) + (1<<(S-6)), maskgx = ~(maskr | maskb);
  1493. int i;
  1494. maskr |= maskr << 1; maskb |= maskb << 1; maskg |= maskg << 1;
  1495. for (i = 0; i < width; i++) {
  1496. int px0 = input_pixel(2 * i + 0) >> shp;
  1497. int px1 = input_pixel(2 * i + 1) >> shp;
  1498. int b, r, g = (px0 & maskgx) + (px1 & maskgx);
  1499. int rb = px0 + px1 - g;
  1500. b = (rb & maskb) >> shb;
  1501. if (shp || origin == PIX_FMT_BGR565LE || origin == PIX_FMT_BGR565BE ||
  1502. origin == PIX_FMT_RGB565LE || origin == PIX_FMT_RGB565BE) {
  1503. g >>= shg;
  1504. } else {
  1505. g = (g & maskg) >> shg;
  1506. }
  1507. r = (rb & maskr) >> shr;
  1508. dstU[i] = (ru * r + gu * g + bu * b + (unsigned)rnd) >> ((S)-6+1);
  1509. dstV[i] = (rv * r + gv * g + bv * b + (unsigned)rnd) >> ((S)-6+1);
  1510. }
  1511. }
  1512. #undef input_pixel
  1513. #define rgb16_32_wrapper(fmt, name, shr, shg, shb, shp, maskr, \
  1514. maskg, maskb, rsh, gsh, bsh, S) \
  1515. static void name ## ToY_c(uint8_t *dst, const uint8_t *src, \
  1516. int width, uint32_t *unused) \
  1517. { \
  1518. rgb16_32ToY_c_template(dst, src, width, fmt, shr, shg, shb, shp, \
  1519. maskr, maskg, maskb, rsh, gsh, bsh, S); \
  1520. } \
  1521. \
  1522. static void name ## ToUV_c(uint8_t *dstU, uint8_t *dstV, \
  1523. const uint8_t *src, const uint8_t *dummy, \
  1524. int width, uint32_t *unused) \
  1525. { \
  1526. rgb16_32ToUV_c_template(dstU, dstV, src, width, fmt, shr, shg, shb, shp, \
  1527. maskr, maskg, maskb, rsh, gsh, bsh, S); \
  1528. } \
  1529. \
  1530. static void name ## ToUV_half_c(uint8_t *dstU, uint8_t *dstV, \
  1531. const uint8_t *src, const uint8_t *dummy, \
  1532. int width, uint32_t *unused) \
  1533. { \
  1534. rgb16_32ToUV_half_c_template(dstU, dstV, src, width, fmt, shr, shg, shb, shp, \
  1535. maskr, maskg, maskb, rsh, gsh, bsh, S); \
  1536. }
  1537. rgb16_32_wrapper(PIX_FMT_BGR32, bgr32, 16, 0, 0, 0, 0xFF0000, 0xFF00, 0x00FF, 8, 0, 8, RGB2YUV_SHIFT+8);
  1538. rgb16_32_wrapper(PIX_FMT_BGR32_1, bgr321, 16, 0, 0, 8, 0xFF0000, 0xFF00, 0x00FF, 8, 0, 8, RGB2YUV_SHIFT+8);
  1539. rgb16_32_wrapper(PIX_FMT_RGB32, rgb32, 0, 0, 16, 0, 0x00FF, 0xFF00, 0xFF0000, 8, 0, 8, RGB2YUV_SHIFT+8);
  1540. rgb16_32_wrapper(PIX_FMT_RGB32_1, rgb321, 0, 0, 16, 8, 0x00FF, 0xFF00, 0xFF0000, 8, 0, 8, RGB2YUV_SHIFT+8);
  1541. rgb16_32_wrapper(PIX_FMT_BGR565LE, bgr16le, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, 11, 5, 0, RGB2YUV_SHIFT+8);
  1542. rgb16_32_wrapper(PIX_FMT_BGR555LE, bgr15le, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, 10, 5, 0, RGB2YUV_SHIFT+7);
  1543. rgb16_32_wrapper(PIX_FMT_RGB565LE, rgb16le, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, 0, 5, 11, RGB2YUV_SHIFT+8);
  1544. rgb16_32_wrapper(PIX_FMT_RGB555LE, rgb15le, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, 0, 5, 10, RGB2YUV_SHIFT+7);
  1545. rgb16_32_wrapper(PIX_FMT_BGR565BE, bgr16be, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, 11, 5, 0, RGB2YUV_SHIFT+8);
  1546. rgb16_32_wrapper(PIX_FMT_BGR555BE, bgr15be, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, 10, 5, 0, RGB2YUV_SHIFT+7);
  1547. rgb16_32_wrapper(PIX_FMT_RGB565BE, rgb16be, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, 0, 5, 11, RGB2YUV_SHIFT+8);
  1548. rgb16_32_wrapper(PIX_FMT_RGB555BE, rgb15be, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, 0, 5, 10, RGB2YUV_SHIFT+7);
  1549. static void abgrToA_c(int16_t *dst, const uint8_t *src, int width, uint32_t *unused)
  1550. {
  1551. int i;
  1552. for (i=0; i<width; i++) {
  1553. dst[i]= src[4*i]<<6;
  1554. }
  1555. }
  1556. static void rgbaToA_c(int16_t *dst, const uint8_t *src, int width, uint32_t *unused)
  1557. {
  1558. int i;
  1559. for (i=0; i<width; i++) {
  1560. dst[i]= src[4*i+3]<<6;
  1561. }
  1562. }
  1563. static void palToA_c(int16_t *dst, const uint8_t *src, int width, uint32_t *pal)
  1564. {
  1565. int i;
  1566. for (i=0; i<width; i++) {
  1567. int d= src[i];
  1568. dst[i]= (pal[d] >> 24)<<6;
  1569. }
  1570. }
  1571. static void palToY_c(int16_t *dst, const uint8_t *src, long width, uint32_t *pal)
  1572. {
  1573. int i;
  1574. for (i=0; i<width; i++) {
  1575. int d= src[i];
  1576. dst[i]= (pal[d] & 0xFF)<<6;
  1577. }
  1578. }
  1579. static void palToUV_c(uint16_t *dstU, int16_t *dstV,
  1580. const uint8_t *src1, const uint8_t *src2,
  1581. int width, uint32_t *pal)
  1582. {
  1583. int i;
  1584. assert(src1 == src2);
  1585. for (i=0; i<width; i++) {
  1586. int p= pal[src1[i]];
  1587. dstU[i]= (uint8_t)(p>> 8)<<6;
  1588. dstV[i]= (uint8_t)(p>>16)<<6;
  1589. }
  1590. }
  1591. static void monowhite2Y_c(int16_t *dst, const uint8_t *src, int width, uint32_t *unused)
  1592. {
  1593. int i, j;
  1594. for (i=0; i<width/8; i++) {
  1595. int d= ~src[i];
  1596. for(j=0; j<8; j++)
  1597. dst[8*i+j]= ((d>>(7-j))&1)*16383;
  1598. }
  1599. if(width&7){
  1600. int d= ~src[i];
  1601. for(j=0; j<(width&7); j++)
  1602. dst[8*i+j]= ((d>>(7-j))&1)*16383;
  1603. }
  1604. }
  1605. static void monoblack2Y_c(int16_t *dst, const uint8_t *src, int width, uint32_t *unused)
  1606. {
  1607. int i, j;
  1608. for (i=0; i<width/8; i++) {
  1609. int d= src[i];
  1610. for(j=0; j<8; j++)
  1611. dst[8*i+j]= ((d>>(7-j))&1)*16383;
  1612. }
  1613. if(width&7){
  1614. int d= src[i];
  1615. for(j=0; j<(width&7); j++)
  1616. dst[8*i+j]= ((d>>(7-j))&1)*16383;
  1617. }
  1618. }
  1619. //FIXME yuy2* can read up to 7 samples too much
  1620. static void yuy2ToY_c(uint8_t *dst, const uint8_t *src, int width,
  1621. uint32_t *unused)
  1622. {
  1623. int i;
  1624. for (i=0; i<width; i++)
  1625. dst[i]= src[2*i];
  1626. }
  1627. static void yuy2ToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  1628. const uint8_t *src2, int width, uint32_t *unused)
  1629. {
  1630. int i;
  1631. for (i=0; i<width; i++) {
  1632. dstU[i]= src1[4*i + 1];
  1633. dstV[i]= src1[4*i + 3];
  1634. }
  1635. assert(src1 == src2);
  1636. }
  1637. static void bswap16Y_c(uint8_t *_dst, const uint8_t *_src, int width, uint32_t *unused)
  1638. {
  1639. int i;
  1640. const uint16_t *src = (const uint16_t *) _src;
  1641. uint16_t *dst = (uint16_t *) _dst;
  1642. for (i=0; i<width; i++) {
  1643. dst[i] = av_bswap16(src[i]);
  1644. }
  1645. }
  1646. static void bswap16UV_c(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *_src1,
  1647. const uint8_t *_src2, int width, uint32_t *unused)
  1648. {
  1649. int i;
  1650. const uint16_t *src1 = (const uint16_t *) _src1,
  1651. *src2 = (const uint16_t *) _src2;
  1652. uint16_t *dstU = (uint16_t *) _dstU, *dstV = (uint16_t *) _dstV;
  1653. for (i=0; i<width; i++) {
  1654. dstU[i] = av_bswap16(src1[i]);
  1655. dstV[i] = av_bswap16(src2[i]);
  1656. }
  1657. }
  1658. /* This is almost identical to the previous, end exists only because
  1659. * yuy2ToY/UV)(dst, src+1, ...) would have 100% unaligned accesses. */
  1660. static void uyvyToY_c(uint8_t *dst, const uint8_t *src, int width,
  1661. uint32_t *unused)
  1662. {
  1663. int i;
  1664. for (i=0; i<width; i++)
  1665. dst[i]= src[2*i+1];
  1666. }
  1667. static void uyvyToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  1668. const uint8_t *src2, int width, uint32_t *unused)
  1669. {
  1670. int i;
  1671. for (i=0; i<width; i++) {
  1672. dstU[i]= src1[4*i + 0];
  1673. dstV[i]= src1[4*i + 2];
  1674. }
  1675. assert(src1 == src2);
  1676. }
  1677. static av_always_inline void nvXXtoUV_c(uint8_t *dst1, uint8_t *dst2,
  1678. const uint8_t *src, int width)
  1679. {
  1680. int i;
  1681. for (i = 0; i < width; i++) {
  1682. dst1[i] = src[2*i+0];
  1683. dst2[i] = src[2*i+1];
  1684. }
  1685. }
  1686. static void nv12ToUV_c(uint8_t *dstU, uint8_t *dstV,
  1687. const uint8_t *src1, const uint8_t *src2,
  1688. int width, uint32_t *unused)
  1689. {
  1690. nvXXtoUV_c(dstU, dstV, src1, width);
  1691. }
  1692. static void nv21ToUV_c(uint8_t *dstU, uint8_t *dstV,
  1693. const uint8_t *src1, const uint8_t *src2,
  1694. int width, uint32_t *unused)
  1695. {
  1696. nvXXtoUV_c(dstV, dstU, src1, width);
  1697. }
  1698. #define input_pixel(pos) (isBE(origin) ? AV_RB16(pos) : AV_RL16(pos))
  1699. static void bgr24ToY_c(int16_t *dst, const uint8_t *src,
  1700. int width, uint32_t *unused)
  1701. {
  1702. int i;
  1703. for (i=0; i<width; i++) {
  1704. int b= src[i*3+0];
  1705. int g= src[i*3+1];
  1706. int r= src[i*3+2];
  1707. dst[i]= ((RY*r + GY*g + BY*b + (32<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6));
  1708. }
  1709. }
  1710. static void bgr24ToUV_c(int16_t *dstU, int16_t *dstV, const uint8_t *src1,
  1711. const uint8_t *src2, int width, uint32_t *unused)
  1712. {
  1713. int i;
  1714. for (i=0; i<width; i++) {
  1715. int b= src1[3*i + 0];
  1716. int g= src1[3*i + 1];
  1717. int r= src1[3*i + 2];
  1718. dstU[i]= (RU*r + GU*g + BU*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
  1719. dstV[i]= (RV*r + GV*g + BV*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
  1720. }
  1721. assert(src1 == src2);
  1722. }
  1723. static void bgr24ToUV_half_c(int16_t *dstU, int16_t *dstV, const uint8_t *src1,
  1724. const uint8_t *src2, int width, uint32_t *unused)
  1725. {
  1726. int i;
  1727. for (i=0; i<width; i++) {
  1728. int b= src1[6*i + 0] + src1[6*i + 3];
  1729. int g= src1[6*i + 1] + src1[6*i + 4];
  1730. int r= src1[6*i + 2] + src1[6*i + 5];
  1731. dstU[i]= (RU*r + GU*g + BU*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
  1732. dstV[i]= (RV*r + GV*g + BV*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
  1733. }
  1734. assert(src1 == src2);
  1735. }
  1736. static void rgb24ToY_c(int16_t *dst, const uint8_t *src, int width,
  1737. uint32_t *unused)
  1738. {
  1739. int i;
  1740. for (i=0; i<width; i++) {
  1741. int r= src[i*3+0];
  1742. int g= src[i*3+1];
  1743. int b= src[i*3+2];
  1744. dst[i]= ((RY*r + GY*g + BY*b + (32<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6));
  1745. }
  1746. }
  1747. static void rgb24ToUV_c(int16_t *dstU, int16_t *dstV, const uint8_t *src1,
  1748. const uint8_t *src2, int width, uint32_t *unused)
  1749. {
  1750. int i;
  1751. assert(src1==src2);
  1752. for (i=0; i<width; i++) {
  1753. int r= src1[3*i + 0];
  1754. int g= src1[3*i + 1];
  1755. int b= src1[3*i + 2];
  1756. dstU[i]= (RU*r + GU*g + BU*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
  1757. dstV[i]= (RV*r + GV*g + BV*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
  1758. }
  1759. }
  1760. static void rgb24ToUV_half_c(int16_t *dstU, int16_t *dstV, const uint8_t *src1,
  1761. const uint8_t *src2, int width, uint32_t *unused)
  1762. {
  1763. int i;
  1764. assert(src1==src2);
  1765. for (i=0; i<width; i++) {
  1766. int r= src1[6*i + 0] + src1[6*i + 3];
  1767. int g= src1[6*i + 1] + src1[6*i + 4];
  1768. int b= src1[6*i + 2] + src1[6*i + 5];
  1769. dstU[i]= (RU*r + GU*g + BU*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
  1770. dstV[i]= (RV*r + GV*g + BV*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
  1771. }
  1772. }
  1773. static void hScale16_c(SwsContext *c, int16_t *_dst, int dstW, const uint8_t *_src,
  1774. const int16_t *filter,
  1775. const int16_t *filterPos, int filterSize)
  1776. {
  1777. int i;
  1778. int32_t *dst = (int32_t *) _dst;
  1779. const uint16_t *src = (const uint16_t *) _src;
  1780. int bits = av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1;
  1781. int sh = (bits <= 7) ? 11 : (bits - 4);
  1782. if((isAnyRGB(c->srcFormat) || c->srcFormat==PIX_FMT_PAL8) && av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1<15)
  1783. sh= 9;
  1784. for (i = 0; i < dstW; i++) {
  1785. int j;
  1786. int srcPos = filterPos[i];
  1787. int val = 0;
  1788. for (j = 0; j < filterSize; j++) {
  1789. val += src[srcPos + j] * filter[filterSize * i + j];
  1790. }
  1791. // filter=14 bit, input=16 bit, output=30 bit, >> 11 makes 19 bit
  1792. dst[i] = FFMIN(val >> sh, (1 << 19) - 1);
  1793. }
  1794. }
  1795. // bilinear / bicubic scaling
  1796. static void hScale_c(SwsContext *c, int16_t *dst, int dstW, const uint8_t *src,
  1797. const int16_t *filter, const int16_t *filterPos,
  1798. int filterSize)
  1799. {
  1800. int i;
  1801. for (i=0; i<dstW; i++) {
  1802. int j;
  1803. int srcPos= filterPos[i];
  1804. int val=0;
  1805. for (j=0; j<filterSize; j++) {
  1806. val += ((int)src[srcPos + j])*filter[filterSize*i + j];
  1807. }
  1808. //filter += hFilterSize;
  1809. dst[i] = FFMIN(val>>7, (1<<15)-1); // the cubic equation does overflow ...
  1810. //dst[i] = val>>7;
  1811. }
  1812. }
  1813. static inline void hScale16N_c(int16_t *dst, int dstW, const uint16_t *src, int srcW, int xInc,
  1814. const int16_t *filter, const int16_t *filterPos, long filterSize, int shift)
  1815. {
  1816. int i, j;
  1817. for (i=0; i<dstW; i++) {
  1818. int srcPos= filterPos[i];
  1819. int val=0;
  1820. for (j=0; j<filterSize; j++) {
  1821. val += ((int)src[srcPos + j])*filter[filterSize*i + j];
  1822. }
  1823. dst[i] = FFMIN(val>>shift, (1<<15)-1); // the cubic equation does overflow ...
  1824. }
  1825. }
  1826. static inline void hScale16NX_c(int16_t *dst, int dstW, const uint16_t *src, int srcW, int xInc,
  1827. const int16_t *filter, const int16_t *filterPos, long filterSize, int shift)
  1828. {
  1829. int i, j;
  1830. for (i=0; i<dstW; i++) {
  1831. int srcPos= filterPos[i];
  1832. int val=0;
  1833. for (j=0; j<filterSize; j++) {
  1834. val += ((int)av_bswap16(src[srcPos + j]))*filter[filterSize*i + j];
  1835. }
  1836. dst[i] = FFMIN(val>>shift, (1<<15)-1); // the cubic equation does overflow ...
  1837. }
  1838. }
  1839. //FIXME all pal and rgb srcFormats could do this convertion as well
  1840. //FIXME all scalers more complex than bilinear could do half of this transform
  1841. static void chrRangeToJpeg_c(int16_t *dstU, int16_t *dstV, int width)
  1842. {
  1843. int i;
  1844. for (i = 0; i < width; i++) {
  1845. dstU[i] = (FFMIN(dstU[i],30775)*4663 - 9289992)>>12; //-264
  1846. dstV[i] = (FFMIN(dstV[i],30775)*4663 - 9289992)>>12; //-264
  1847. }
  1848. }
  1849. static void chrRangeFromJpeg_c(int16_t *dstU, int16_t *dstV, int width)
  1850. {
  1851. int i;
  1852. for (i = 0; i < width; i++) {
  1853. dstU[i] = (dstU[i]*1799 + 4081085)>>11; //1469
  1854. dstV[i] = (dstV[i]*1799 + 4081085)>>11; //1469
  1855. }
  1856. }
  1857. static void lumRangeToJpeg_c(int16_t *dst, int width)
  1858. {
  1859. int i;
  1860. for (i = 0; i < width; i++)
  1861. dst[i] = (FFMIN(dst[i],30189)*19077 - 39057361)>>14;
  1862. }
  1863. static void lumRangeFromJpeg_c(int16_t *dst, int width)
  1864. {
  1865. int i;
  1866. for (i = 0; i < width; i++)
  1867. dst[i] = (dst[i]*14071 + 33561947)>>14;
  1868. }
  1869. static void chrRangeToJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
  1870. {
  1871. int i;
  1872. int32_t *dstU = (int32_t *) _dstU;
  1873. int32_t *dstV = (int32_t *) _dstV;
  1874. for (i = 0; i < width; i++) {
  1875. dstU[i] = (FFMIN(dstU[i],30775<<4)*4663 - (9289992<<4))>>12; //-264
  1876. dstV[i] = (FFMIN(dstV[i],30775<<4)*4663 - (9289992<<4))>>12; //-264
  1877. }
  1878. }
  1879. static void chrRangeFromJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
  1880. {
  1881. int i;
  1882. int32_t *dstU = (int32_t *) _dstU;
  1883. int32_t *dstV = (int32_t *) _dstV;
  1884. for (i = 0; i < width; i++) {
  1885. dstU[i] = (dstU[i]*1799 + (4081085<<4))>>11; //1469
  1886. dstV[i] = (dstV[i]*1799 + (4081085<<4))>>11; //1469
  1887. }
  1888. }
  1889. static void lumRangeToJpeg16_c(int16_t *_dst, int width)
  1890. {
  1891. int i;
  1892. int32_t *dst = (int32_t *) _dst;
  1893. for (i = 0; i < width; i++)
  1894. dst[i] = (FFMIN(dst[i],30189<<4)*4769 - (39057361<<2))>>12;
  1895. }
  1896. static void lumRangeFromJpeg16_c(int16_t *_dst, int width)
  1897. {
  1898. int i;
  1899. int32_t *dst = (int32_t *) _dst;
  1900. for (i = 0; i < width; i++)
  1901. dst[i] = (dst[i]*(14071/4) + (33561947<<4)/4)>>12;
  1902. }
  1903. static void hyscale_fast_c(SwsContext *c, int16_t *dst, int dstWidth,
  1904. const uint8_t *src, int srcW, int xInc)
  1905. {
  1906. int i;
  1907. unsigned int xpos=0;
  1908. for (i=0;i<dstWidth;i++) {
  1909. register unsigned int xx=xpos>>16;
  1910. register unsigned int xalpha=(xpos&0xFFFF)>>9;
  1911. dst[i]= (src[xx]<<7) + (src[xx+1] - src[xx])*xalpha;
  1912. xpos+=xInc;
  1913. }
  1914. for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--)
  1915. dst[i] = src[srcW-1]*128;
  1916. }
  1917. static void scale8To16Rv_c(uint16_t *_dst, const uint8_t *src, int len)
  1918. {
  1919. int i;
  1920. uint8_t *dst = (uint8_t *) _dst;
  1921. for (i = len - 1; i >= 0; i--) {
  1922. dst[i * 2] = dst[i * 2 + 1] = src[i];
  1923. }
  1924. }
  1925. static void scale19To15Fw_c(int16_t *dst, const int32_t *src, int len)
  1926. {
  1927. int i;
  1928. for (i = 0; i < len; i++) {
  1929. dst[i] = src[i] >> 4;
  1930. }
  1931. }
  1932. // *** horizontal scale Y line to temp buffer
  1933. static av_always_inline void hyscale(SwsContext *c, int16_t *dst, int dstWidth,
  1934. const uint8_t *src, int srcW, int xInc,
  1935. const int16_t *hLumFilter,
  1936. const int16_t *hLumFilterPos, int hLumFilterSize,
  1937. uint8_t *formatConvBuffer,
  1938. uint32_t *pal, int isAlpha)
  1939. {
  1940. void (*toYV12)(uint8_t *, const uint8_t *, int, uint32_t *) = isAlpha ? c->alpToYV12 : c->lumToYV12;
  1941. void (*convertRange)(int16_t *, int) = isAlpha ? NULL : c->lumConvertRange;
  1942. if (toYV12) {
  1943. toYV12(formatConvBuffer, src, srcW, pal);
  1944. src= formatConvBuffer;
  1945. }
  1946. if (av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1 < 8 && c->scalingBpp == 16 && !isAnyRGB(c->srcFormat)) {
  1947. c->scale8To16Rv((uint16_t *) formatConvBuffer, src, srcW);
  1948. src = formatConvBuffer;
  1949. }
  1950. if (c->hScale16) {
  1951. int shift= isAnyRGB(c->srcFormat) || c->srcFormat==PIX_FMT_PAL8 ? 13 : av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1;
  1952. c->hScale16(dst, dstWidth, (const uint16_t*)src, srcW, xInc, hLumFilter, hLumFilterPos, hLumFilterSize, shift);
  1953. } else if (!c->hyscale_fast) {
  1954. c->hScale(c, dst, dstWidth, src, hLumFilter, hLumFilterPos, hLumFilterSize);
  1955. } else { // fast bilinear upscale / crap downscale
  1956. c->hyscale_fast(c, dst, dstWidth, src, srcW, xInc);
  1957. }
  1958. if (convertRange)
  1959. convertRange(dst, dstWidth);
  1960. if (av_pix_fmt_descriptors[c->dstFormat].comp[0].depth_minus1 < 15 && c->scalingBpp == 16) {
  1961. c->scale19To15Fw(dst, (int32_t *) dst, dstWidth);
  1962. }
  1963. }
  1964. static void hcscale_fast_c(SwsContext *c, int16_t *dst1, int16_t *dst2,
  1965. int dstWidth, const uint8_t *src1,
  1966. const uint8_t *src2, int srcW, int xInc)
  1967. {
  1968. int i;
  1969. unsigned int xpos=0;
  1970. for (i=0;i<dstWidth;i++) {
  1971. register unsigned int xx=xpos>>16;
  1972. register unsigned int xalpha=(xpos&0xFFFF)>>9;
  1973. dst1[i]=(src1[xx]*(xalpha^127)+src1[xx+1]*xalpha);
  1974. dst2[i]=(src2[xx]*(xalpha^127)+src2[xx+1]*xalpha);
  1975. xpos+=xInc;
  1976. }
  1977. for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--) {
  1978. dst1[i] = src1[srcW-1]*128;
  1979. dst2[i] = src2[srcW-1]*128;
  1980. }
  1981. }
  1982. static av_always_inline void hcscale(SwsContext *c, int16_t *dst1, int16_t *dst2, int dstWidth,
  1983. const uint8_t *src1, const uint8_t *src2,
  1984. int srcW, int xInc, const int16_t *hChrFilter,
  1985. const int16_t *hChrFilterPos, int hChrFilterSize,
  1986. uint8_t *formatConvBuffer, uint32_t *pal)
  1987. {
  1988. if (c->chrToYV12) {
  1989. uint8_t *buf2 = formatConvBuffer + FFALIGN(srcW*2+78, 16);
  1990. c->chrToYV12(formatConvBuffer, buf2, src1, src2, srcW, pal);
  1991. src1= formatConvBuffer;
  1992. src2= buf2;
  1993. }
  1994. if (av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1 < 8 && c->scalingBpp == 16 && !isAnyRGB(c->srcFormat)) {
  1995. uint8_t *buf2 = (formatConvBuffer + FFALIGN(srcW * 2+78, 16));
  1996. c->scale8To16Rv((uint16_t *) formatConvBuffer, src1, srcW);
  1997. c->scale8To16Rv((uint16_t *) buf2, src2, srcW);
  1998. src1 = formatConvBuffer;
  1999. src2 = buf2;
  2000. }
  2001. if (c->hScale16) {
  2002. int shift= isAnyRGB(c->srcFormat) || c->srcFormat==PIX_FMT_PAL8 ? 13 : av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1;
  2003. c->hScale16(dst1, dstWidth, (const uint16_t*)src1, srcW, xInc, hChrFilter, hChrFilterPos, hChrFilterSize, shift);
  2004. c->hScale16(dst2, dstWidth, (const uint16_t*)src2, srcW, xInc, hChrFilter, hChrFilterPos, hChrFilterSize, shift);
  2005. } else if (!c->hcscale_fast) {
  2006. c->hScale(c, dst1, dstWidth, src1, hChrFilter, hChrFilterPos, hChrFilterSize);
  2007. c->hScale(c, dst2, dstWidth, src2, hChrFilter, hChrFilterPos, hChrFilterSize);
  2008. } else { // fast bilinear upscale / crap downscale
  2009. c->hcscale_fast(c, dst1, dst2, dstWidth, src1, src2, srcW, xInc);
  2010. }
  2011. if (c->chrConvertRange)
  2012. c->chrConvertRange(dst1, dst2, dstWidth);
  2013. if (av_pix_fmt_descriptors[c->dstFormat].comp[0].depth_minus1 < 15 && c->scalingBpp == 16) {
  2014. c->scale19To15Fw(dst1, (int32_t *) dst1, dstWidth);
  2015. c->scale19To15Fw(dst2, (int32_t *) dst2, dstWidth);
  2016. }
  2017. }
  2018. static av_always_inline void
  2019. find_c_packed_planar_out_funcs(SwsContext *c,
  2020. yuv2planar1_fn *yuv2yuv1, yuv2planarX_fn *yuv2yuvX,
  2021. yuv2packed1_fn *yuv2packed1, yuv2packed2_fn *yuv2packed2,
  2022. yuv2packedX_fn *yuv2packedX)
  2023. {
  2024. enum PixelFormat dstFormat = c->dstFormat;
  2025. if (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21) {
  2026. *yuv2yuvX = yuv2nv12X_c;
  2027. } else if (is16BPS(dstFormat)) {
  2028. *yuv2yuvX = isBE(dstFormat) ? yuv2yuvX16BE_c : yuv2yuvX16LE_c;
  2029. } else if (is9_OR_10BPS(dstFormat)) {
  2030. if (av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1 == 8) {
  2031. *yuv2yuvX = isBE(dstFormat) ? yuv2yuvX9BE_c : yuv2yuvX9LE_c;
  2032. } else {
  2033. *yuv2yuvX = isBE(dstFormat) ? yuv2yuvX10BE_c : yuv2yuvX10LE_c;
  2034. }
  2035. } else {
  2036. *yuv2yuv1 = yuv2yuv1_c;
  2037. *yuv2yuvX = yuv2yuvX_c;
  2038. }
  2039. if(c->flags & SWS_FULL_CHR_H_INT) {
  2040. switch (dstFormat) {
  2041. case PIX_FMT_RGBA:
  2042. #if CONFIG_SMALL
  2043. *yuv2packedX = yuv2rgba32_full_X_c;
  2044. #else
  2045. #if CONFIG_SWSCALE_ALPHA
  2046. if (c->alpPixBuf) {
  2047. *yuv2packedX = yuv2rgba32_full_X_c;
  2048. } else
  2049. #endif /* CONFIG_SWSCALE_ALPHA */
  2050. {
  2051. *yuv2packedX = yuv2rgbx32_full_X_c;
  2052. }
  2053. #endif /* !CONFIG_SMALL */
  2054. break;
  2055. case PIX_FMT_ARGB:
  2056. #if CONFIG_SMALL
  2057. *yuv2packedX = yuv2argb32_full_X_c;
  2058. #else
  2059. #if CONFIG_SWSCALE_ALPHA
  2060. if (c->alpPixBuf) {
  2061. *yuv2packedX = yuv2argb32_full_X_c;
  2062. } else
  2063. #endif /* CONFIG_SWSCALE_ALPHA */
  2064. {
  2065. *yuv2packedX = yuv2xrgb32_full_X_c;
  2066. }
  2067. #endif /* !CONFIG_SMALL */
  2068. break;
  2069. case PIX_FMT_BGRA:
  2070. #if CONFIG_SMALL
  2071. *yuv2packedX = yuv2bgra32_full_X_c;
  2072. #else
  2073. #if CONFIG_SWSCALE_ALPHA
  2074. if (c->alpPixBuf) {
  2075. *yuv2packedX = yuv2bgra32_full_X_c;
  2076. } else
  2077. #endif /* CONFIG_SWSCALE_ALPHA */
  2078. {
  2079. *yuv2packedX = yuv2bgrx32_full_X_c;
  2080. }
  2081. #endif /* !CONFIG_SMALL */
  2082. break;
  2083. case PIX_FMT_ABGR:
  2084. #if CONFIG_SMALL
  2085. *yuv2packedX = yuv2abgr32_full_X_c;
  2086. #else
  2087. #if CONFIG_SWSCALE_ALPHA
  2088. if (c->alpPixBuf) {
  2089. *yuv2packedX = yuv2abgr32_full_X_c;
  2090. } else
  2091. #endif /* CONFIG_SWSCALE_ALPHA */
  2092. {
  2093. *yuv2packedX = yuv2xbgr32_full_X_c;
  2094. }
  2095. #endif /* !CONFIG_SMALL */
  2096. break;
  2097. case PIX_FMT_RGB24:
  2098. *yuv2packedX = yuv2rgb24_full_X_c;
  2099. break;
  2100. case PIX_FMT_BGR24:
  2101. *yuv2packedX = yuv2bgr24_full_X_c;
  2102. break;
  2103. }
  2104. if(!*yuv2packedX)
  2105. goto YUV_PACKED;
  2106. } else {
  2107. YUV_PACKED:
  2108. switch (dstFormat) {
  2109. case PIX_FMT_GRAY16BE:
  2110. *yuv2packed1 = yuv2gray16BE_1_c;
  2111. *yuv2packed2 = yuv2gray16BE_2_c;
  2112. *yuv2packedX = yuv2gray16BE_X_c;
  2113. break;
  2114. case PIX_FMT_GRAY16LE:
  2115. *yuv2packed1 = yuv2gray16LE_1_c;
  2116. *yuv2packed2 = yuv2gray16LE_2_c;
  2117. *yuv2packedX = yuv2gray16LE_X_c;
  2118. break;
  2119. case PIX_FMT_MONOWHITE:
  2120. *yuv2packed1 = yuv2monowhite_1_c;
  2121. *yuv2packed2 = yuv2monowhite_2_c;
  2122. *yuv2packedX = yuv2monowhite_X_c;
  2123. break;
  2124. case PIX_FMT_MONOBLACK:
  2125. *yuv2packed1 = yuv2monoblack_1_c;
  2126. *yuv2packed2 = yuv2monoblack_2_c;
  2127. *yuv2packedX = yuv2monoblack_X_c;
  2128. break;
  2129. case PIX_FMT_YUYV422:
  2130. *yuv2packed1 = yuv2yuyv422_1_c;
  2131. *yuv2packed2 = yuv2yuyv422_2_c;
  2132. *yuv2packedX = yuv2yuyv422_X_c;
  2133. break;
  2134. case PIX_FMT_UYVY422:
  2135. *yuv2packed1 = yuv2uyvy422_1_c;
  2136. *yuv2packed2 = yuv2uyvy422_2_c;
  2137. *yuv2packedX = yuv2uyvy422_X_c;
  2138. break;
  2139. case PIX_FMT_RGB48LE:
  2140. *yuv2packed1 = yuv2rgb48le_1_c;
  2141. *yuv2packed2 = yuv2rgb48le_2_c;
  2142. *yuv2packedX = yuv2rgb48le_X_c;
  2143. break;
  2144. case PIX_FMT_RGB48BE:
  2145. *yuv2packed1 = yuv2rgb48be_1_c;
  2146. *yuv2packed2 = yuv2rgb48be_2_c;
  2147. *yuv2packedX = yuv2rgb48be_X_c;
  2148. break;
  2149. case PIX_FMT_BGR48LE:
  2150. *yuv2packed1 = yuv2bgr48le_1_c;
  2151. *yuv2packed2 = yuv2bgr48le_2_c;
  2152. *yuv2packedX = yuv2bgr48le_X_c;
  2153. break;
  2154. case PIX_FMT_BGR48BE:
  2155. *yuv2packed1 = yuv2bgr48be_1_c;
  2156. *yuv2packed2 = yuv2bgr48be_2_c;
  2157. *yuv2packedX = yuv2bgr48be_X_c;
  2158. break;
  2159. case PIX_FMT_RGB32:
  2160. case PIX_FMT_BGR32:
  2161. #if CONFIG_SMALL
  2162. *yuv2packed1 = yuv2rgb32_1_c;
  2163. *yuv2packed2 = yuv2rgb32_2_c;
  2164. *yuv2packedX = yuv2rgb32_X_c;
  2165. #else
  2166. #if CONFIG_SWSCALE_ALPHA
  2167. if (c->alpPixBuf) {
  2168. *yuv2packed1 = yuv2rgba32_1_c;
  2169. *yuv2packed2 = yuv2rgba32_2_c;
  2170. *yuv2packedX = yuv2rgba32_X_c;
  2171. } else
  2172. #endif /* CONFIG_SWSCALE_ALPHA */
  2173. {
  2174. *yuv2packed1 = yuv2rgbx32_1_c;
  2175. *yuv2packed2 = yuv2rgbx32_2_c;
  2176. *yuv2packedX = yuv2rgbx32_X_c;
  2177. }
  2178. #endif /* !CONFIG_SMALL */
  2179. break;
  2180. case PIX_FMT_RGB32_1:
  2181. case PIX_FMT_BGR32_1:
  2182. #if CONFIG_SMALL
  2183. *yuv2packed1 = yuv2rgb32_1_1_c;
  2184. *yuv2packed2 = yuv2rgb32_1_2_c;
  2185. *yuv2packedX = yuv2rgb32_1_X_c;
  2186. #else
  2187. #if CONFIG_SWSCALE_ALPHA
  2188. if (c->alpPixBuf) {
  2189. *yuv2packed1 = yuv2rgba32_1_1_c;
  2190. *yuv2packed2 = yuv2rgba32_1_2_c;
  2191. *yuv2packedX = yuv2rgba32_1_X_c;
  2192. } else
  2193. #endif /* CONFIG_SWSCALE_ALPHA */
  2194. {
  2195. *yuv2packed1 = yuv2rgbx32_1_1_c;
  2196. *yuv2packed2 = yuv2rgbx32_1_2_c;
  2197. *yuv2packedX = yuv2rgbx32_1_X_c;
  2198. }
  2199. #endif /* !CONFIG_SMALL */
  2200. break;
  2201. case PIX_FMT_RGB24:
  2202. *yuv2packed1 = yuv2rgb24_1_c;
  2203. *yuv2packed2 = yuv2rgb24_2_c;
  2204. *yuv2packedX = yuv2rgb24_X_c;
  2205. break;
  2206. case PIX_FMT_BGR24:
  2207. *yuv2packed1 = yuv2bgr24_1_c;
  2208. *yuv2packed2 = yuv2bgr24_2_c;
  2209. *yuv2packedX = yuv2bgr24_X_c;
  2210. break;
  2211. case PIX_FMT_RGB565LE:
  2212. case PIX_FMT_RGB565BE:
  2213. case PIX_FMT_BGR565LE:
  2214. case PIX_FMT_BGR565BE:
  2215. *yuv2packed1 = yuv2rgb16_1_c;
  2216. *yuv2packed2 = yuv2rgb16_2_c;
  2217. *yuv2packedX = yuv2rgb16_X_c;
  2218. break;
  2219. case PIX_FMT_RGB555LE:
  2220. case PIX_FMT_RGB555BE:
  2221. case PIX_FMT_BGR555LE:
  2222. case PIX_FMT_BGR555BE:
  2223. *yuv2packed1 = yuv2rgb15_1_c;
  2224. *yuv2packed2 = yuv2rgb15_2_c;
  2225. *yuv2packedX = yuv2rgb15_X_c;
  2226. break;
  2227. case PIX_FMT_RGB444LE:
  2228. case PIX_FMT_RGB444BE:
  2229. case PIX_FMT_BGR444LE:
  2230. case PIX_FMT_BGR444BE:
  2231. *yuv2packed1 = yuv2rgb12_1_c;
  2232. *yuv2packed2 = yuv2rgb12_2_c;
  2233. *yuv2packedX = yuv2rgb12_X_c;
  2234. break;
  2235. case PIX_FMT_RGB8:
  2236. case PIX_FMT_BGR8:
  2237. *yuv2packed1 = yuv2rgb8_1_c;
  2238. *yuv2packed2 = yuv2rgb8_2_c;
  2239. *yuv2packedX = yuv2rgb8_X_c;
  2240. break;
  2241. case PIX_FMT_RGB4:
  2242. case PIX_FMT_BGR4:
  2243. *yuv2packed1 = yuv2rgb4_1_c;
  2244. *yuv2packed2 = yuv2rgb4_2_c;
  2245. *yuv2packedX = yuv2rgb4_X_c;
  2246. break;
  2247. case PIX_FMT_RGB4_BYTE:
  2248. case PIX_FMT_BGR4_BYTE:
  2249. *yuv2packed1 = yuv2rgb4b_1_c;
  2250. *yuv2packed2 = yuv2rgb4b_2_c;
  2251. *yuv2packedX = yuv2rgb4b_X_c;
  2252. break;
  2253. }
  2254. }
  2255. }
  2256. #define DEBUG_SWSCALE_BUFFERS 0
  2257. #define DEBUG_BUFFERS(...) if (DEBUG_SWSCALE_BUFFERS) av_log(c, AV_LOG_DEBUG, __VA_ARGS__)
  2258. static int swScale(SwsContext *c, const uint8_t* src[],
  2259. int srcStride[], int srcSliceY,
  2260. int srcSliceH, uint8_t* dst[], int dstStride[])
  2261. {
  2262. /* load a few things into local vars to make the code more readable? and faster */
  2263. const int srcW= c->srcW;
  2264. const int dstW= c->dstW;
  2265. const int dstH= c->dstH;
  2266. const int chrDstW= c->chrDstW;
  2267. const int chrSrcW= c->chrSrcW;
  2268. const int lumXInc= c->lumXInc;
  2269. const int chrXInc= c->chrXInc;
  2270. const enum PixelFormat dstFormat= c->dstFormat;
  2271. const int flags= c->flags;
  2272. int16_t *vLumFilterPos= c->vLumFilterPos;
  2273. int16_t *vChrFilterPos= c->vChrFilterPos;
  2274. int16_t *hLumFilterPos= c->hLumFilterPos;
  2275. int16_t *hChrFilterPos= c->hChrFilterPos;
  2276. int16_t *vLumFilter= c->vLumFilter;
  2277. int16_t *vChrFilter= c->vChrFilter;
  2278. int16_t *hLumFilter= c->hLumFilter;
  2279. int16_t *hChrFilter= c->hChrFilter;
  2280. int32_t *lumMmxFilter= c->lumMmxFilter;
  2281. int32_t *chrMmxFilter= c->chrMmxFilter;
  2282. int32_t av_unused *alpMmxFilter= c->alpMmxFilter;
  2283. const int vLumFilterSize= c->vLumFilterSize;
  2284. const int vChrFilterSize= c->vChrFilterSize;
  2285. const int hLumFilterSize= c->hLumFilterSize;
  2286. const int hChrFilterSize= c->hChrFilterSize;
  2287. int16_t **lumPixBuf= c->lumPixBuf;
  2288. int16_t **chrUPixBuf= c->chrUPixBuf;
  2289. int16_t **chrVPixBuf= c->chrVPixBuf;
  2290. int16_t **alpPixBuf= c->alpPixBuf;
  2291. const int vLumBufSize= c->vLumBufSize;
  2292. const int vChrBufSize= c->vChrBufSize;
  2293. uint8_t *formatConvBuffer= c->formatConvBuffer;
  2294. const int chrSrcSliceY= srcSliceY >> c->chrSrcVSubSample;
  2295. const int chrSrcSliceH= -((-srcSliceH) >> c->chrSrcVSubSample);
  2296. int lastDstY;
  2297. uint32_t *pal=c->pal_yuv;
  2298. int should_dither= isNBPS(c->srcFormat) || is16BPS(c->srcFormat);
  2299. yuv2planar1_fn yuv2yuv1 = c->yuv2yuv1;
  2300. yuv2planarX_fn yuv2yuvX = c->yuv2yuvX;
  2301. yuv2packed1_fn yuv2packed1 = c->yuv2packed1;
  2302. yuv2packed2_fn yuv2packed2 = c->yuv2packed2;
  2303. yuv2packedX_fn yuv2packedX = c->yuv2packedX;
  2304. /* vars which will change and which we need to store back in the context */
  2305. int dstY= c->dstY;
  2306. int lumBufIndex= c->lumBufIndex;
  2307. int chrBufIndex= c->chrBufIndex;
  2308. int lastInLumBuf= c->lastInLumBuf;
  2309. int lastInChrBuf= c->lastInChrBuf;
  2310. if (isPacked(c->srcFormat)) {
  2311. src[0]=
  2312. src[1]=
  2313. src[2]=
  2314. src[3]= src[0];
  2315. srcStride[0]=
  2316. srcStride[1]=
  2317. srcStride[2]=
  2318. srcStride[3]= srcStride[0];
  2319. }
  2320. srcStride[1]<<= c->vChrDrop;
  2321. srcStride[2]<<= c->vChrDrop;
  2322. DEBUG_BUFFERS("swScale() %p[%d] %p[%d] %p[%d] %p[%d] -> %p[%d] %p[%d] %p[%d] %p[%d]\n",
  2323. src[0], srcStride[0], src[1], srcStride[1], src[2], srcStride[2], src[3], srcStride[3],
  2324. dst[0], dstStride[0], dst[1], dstStride[1], dst[2], dstStride[2], dst[3], dstStride[3]);
  2325. DEBUG_BUFFERS("srcSliceY: %d srcSliceH: %d dstY: %d dstH: %d\n",
  2326. srcSliceY, srcSliceH, dstY, dstH);
  2327. DEBUG_BUFFERS("vLumFilterSize: %d vLumBufSize: %d vChrFilterSize: %d vChrBufSize: %d\n",
  2328. vLumFilterSize, vLumBufSize, vChrFilterSize, vChrBufSize);
  2329. if (dstStride[0]%8 !=0 || dstStride[1]%8 !=0 || dstStride[2]%8 !=0 || dstStride[3]%8 != 0) {
  2330. static int warnedAlready=0; //FIXME move this into the context perhaps
  2331. if (flags & SWS_PRINT_INFO && !warnedAlready) {
  2332. av_log(c, AV_LOG_WARNING, "Warning: dstStride is not aligned!\n"
  2333. " ->cannot do aligned memory accesses anymore\n");
  2334. warnedAlready=1;
  2335. }
  2336. }
  2337. /* Note the user might start scaling the picture in the middle so this
  2338. will not get executed. This is not really intended but works
  2339. currently, so people might do it. */
  2340. if (srcSliceY ==0) {
  2341. lumBufIndex=-1;
  2342. chrBufIndex=-1;
  2343. dstY=0;
  2344. lastInLumBuf= -1;
  2345. lastInChrBuf= -1;
  2346. }
  2347. lastDstY= dstY;
  2348. for (;dstY < dstH; dstY++) {
  2349. const int chrDstY= dstY>>c->chrDstVSubSample;
  2350. uint8_t *dest[4] = {
  2351. dst[0] + dstStride[0] * dstY,
  2352. dst[1] + dstStride[1] * chrDstY,
  2353. dst[2] + dstStride[2] * chrDstY,
  2354. (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? dst[3] + dstStride[3] * dstY : NULL,
  2355. };
  2356. const uint8_t *lumDither= should_dither ? dithers[7][dstY &7] : flat64;
  2357. const uint8_t *chrDither= should_dither ? dithers[7][chrDstY&7] : flat64;
  2358. const int firstLumSrcY= vLumFilterPos[dstY]; //First line needed as input
  2359. const int firstLumSrcY2= vLumFilterPos[FFMIN(dstY | ((1<<c->chrDstVSubSample) - 1), dstH-1)];
  2360. const int firstChrSrcY= vChrFilterPos[chrDstY]; //First line needed as input
  2361. int lastLumSrcY= firstLumSrcY + vLumFilterSize -1; // Last line needed as input
  2362. int lastLumSrcY2=firstLumSrcY2+ vLumFilterSize -1; // Last line needed as input
  2363. int lastChrSrcY= firstChrSrcY + vChrFilterSize -1; // Last line needed as input
  2364. int enough_lines;
  2365. //handle holes (FAST_BILINEAR & weird filters)
  2366. if (firstLumSrcY > lastInLumBuf) lastInLumBuf= firstLumSrcY-1;
  2367. if (firstChrSrcY > lastInChrBuf) lastInChrBuf= firstChrSrcY-1;
  2368. assert(firstLumSrcY >= lastInLumBuf - vLumBufSize + 1);
  2369. assert(firstChrSrcY >= lastInChrBuf - vChrBufSize + 1);
  2370. DEBUG_BUFFERS("dstY: %d\n", dstY);
  2371. DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n",
  2372. firstLumSrcY, lastLumSrcY, lastInLumBuf);
  2373. DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n",
  2374. firstChrSrcY, lastChrSrcY, lastInChrBuf);
  2375. // Do we have enough lines in this slice to output the dstY line
  2376. enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH && lastChrSrcY < -((-srcSliceY - srcSliceH)>>c->chrSrcVSubSample);
  2377. if (!enough_lines) {
  2378. lastLumSrcY = srcSliceY + srcSliceH - 1;
  2379. lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1;
  2380. DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n",
  2381. lastLumSrcY, lastChrSrcY);
  2382. }
  2383. //Do horizontal scaling
  2384. while(lastInLumBuf < lastLumSrcY) {
  2385. const uint8_t *src1= src[0]+(lastInLumBuf + 1 - srcSliceY)*srcStride[0];
  2386. const uint8_t *src2= src[3]+(lastInLumBuf + 1 - srcSliceY)*srcStride[3];
  2387. lumBufIndex++;
  2388. assert(lumBufIndex < 2*vLumBufSize);
  2389. assert(lastInLumBuf + 1 - srcSliceY < srcSliceH);
  2390. assert(lastInLumBuf + 1 - srcSliceY >= 0);
  2391. hyscale(c, lumPixBuf[ lumBufIndex ], dstW, src1, srcW, lumXInc,
  2392. hLumFilter, hLumFilterPos, hLumFilterSize,
  2393. formatConvBuffer,
  2394. pal, 0);
  2395. if (CONFIG_SWSCALE_ALPHA && alpPixBuf)
  2396. hyscale(c, alpPixBuf[ lumBufIndex ], dstW, src2, srcW,
  2397. lumXInc, hLumFilter, hLumFilterPos, hLumFilterSize,
  2398. formatConvBuffer,
  2399. pal, 1);
  2400. lastInLumBuf++;
  2401. DEBUG_BUFFERS("\t\tlumBufIndex %d: lastInLumBuf: %d\n",
  2402. lumBufIndex, lastInLumBuf);
  2403. }
  2404. while(lastInChrBuf < lastChrSrcY) {
  2405. const uint8_t *src1= src[1]+(lastInChrBuf + 1 - chrSrcSliceY)*srcStride[1];
  2406. const uint8_t *src2= src[2]+(lastInChrBuf + 1 - chrSrcSliceY)*srcStride[2];
  2407. chrBufIndex++;
  2408. assert(chrBufIndex < 2*vChrBufSize);
  2409. assert(lastInChrBuf + 1 - chrSrcSliceY < (chrSrcSliceH));
  2410. assert(lastInChrBuf + 1 - chrSrcSliceY >= 0);
  2411. //FIXME replace parameters through context struct (some at least)
  2412. if (c->needs_hcscale)
  2413. hcscale(c, chrUPixBuf[chrBufIndex], chrVPixBuf[chrBufIndex],
  2414. chrDstW, src1, src2, chrSrcW, chrXInc,
  2415. hChrFilter, hChrFilterPos, hChrFilterSize,
  2416. formatConvBuffer, pal);
  2417. lastInChrBuf++;
  2418. DEBUG_BUFFERS("\t\tchrBufIndex %d: lastInChrBuf: %d\n",
  2419. chrBufIndex, lastInChrBuf);
  2420. }
  2421. //wrap buf index around to stay inside the ring buffer
  2422. if (lumBufIndex >= vLumBufSize) lumBufIndex-= vLumBufSize;
  2423. if (chrBufIndex >= vChrBufSize) chrBufIndex-= vChrBufSize;
  2424. if (!enough_lines)
  2425. break; //we can't output a dstY line so let's try with the next slice
  2426. #if HAVE_MMX
  2427. updateMMXDitherTables(c, dstY, lumBufIndex, chrBufIndex, lastInLumBuf, lastInChrBuf);
  2428. #endif
  2429. if (dstY >= dstH-2) {
  2430. // hmm looks like we can't use MMX here without overwriting this array's tail
  2431. find_c_packed_planar_out_funcs(c, &yuv2yuv1, &yuv2yuvX,
  2432. &yuv2packed1, &yuv2packed2,
  2433. &yuv2packedX);
  2434. }
  2435. {
  2436. const int16_t **lumSrcPtr= (const int16_t **) lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
  2437. const int16_t **chrUSrcPtr= (const int16_t **) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  2438. const int16_t **chrVSrcPtr= (const int16_t **) chrVPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  2439. const int16_t **alpSrcPtr= (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? (const int16_t **) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
  2440. if (isPlanarYUV(dstFormat) || dstFormat==PIX_FMT_GRAY8) { //YV12 like
  2441. const int chrSkipMask= (1<<c->chrDstVSubSample)-1;
  2442. if ((dstY&chrSkipMask) || isGray(dstFormat))
  2443. dest[1] = dest[2] = NULL; //FIXME split functions in lumi / chromi
  2444. if (c->yuv2yuv1 && vLumFilterSize == 1 && vChrFilterSize == 1) { // unscaled YV12
  2445. const int16_t *alpBuf= (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? alpSrcPtr[0] : NULL;
  2446. yuv2yuv1(c, lumSrcPtr[0], chrUSrcPtr[0], chrVSrcPtr[0], alpBuf,
  2447. dest, dstW, chrDstW, lumDither, chrDither);
  2448. } else { //General YV12
  2449. yuv2yuvX(c, vLumFilter + dstY * vLumFilterSize,
  2450. lumSrcPtr, vLumFilterSize,
  2451. vChrFilter + chrDstY * vChrFilterSize,
  2452. chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  2453. alpSrcPtr, dest, dstW, chrDstW, lumDither, chrDither);
  2454. }
  2455. } else {
  2456. assert(lumSrcPtr + vLumFilterSize - 1 < lumPixBuf + vLumBufSize*2);
  2457. assert(chrUSrcPtr + vChrFilterSize - 1 < chrUPixBuf + vChrBufSize*2);
  2458. if (c->yuv2packed1 && vLumFilterSize == 1 && vChrFilterSize == 2) { //unscaled RGB
  2459. int chrAlpha = vChrFilter[2 * dstY + 1];
  2460. yuv2packed1(c, *lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
  2461. alpPixBuf ? *alpSrcPtr : NULL,
  2462. dest[0], dstW, chrAlpha, dstY);
  2463. } else if (c->yuv2packed2 && vLumFilterSize == 2 && vChrFilterSize == 2) { //bilinear upscale RGB
  2464. int lumAlpha = vLumFilter[2 * dstY + 1];
  2465. int chrAlpha = vChrFilter[2 * dstY + 1];
  2466. lumMmxFilter[2] =
  2467. lumMmxFilter[3] = vLumFilter[2 * dstY ] * 0x10001;
  2468. chrMmxFilter[2] =
  2469. chrMmxFilter[3] = vChrFilter[2 * chrDstY] * 0x10001;
  2470. yuv2packed2(c, lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
  2471. alpPixBuf ? alpSrcPtr : NULL,
  2472. dest[0], dstW, lumAlpha, chrAlpha, dstY);
  2473. } else { //general RGB
  2474. yuv2packedX(c, vLumFilter + dstY * vLumFilterSize,
  2475. lumSrcPtr, vLumFilterSize,
  2476. vChrFilter + dstY * vChrFilterSize,
  2477. chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  2478. alpSrcPtr, dest[0], dstW, dstY);
  2479. }
  2480. }
  2481. }
  2482. }
  2483. if ((dstFormat == PIX_FMT_YUVA420P) && !alpPixBuf)
  2484. fillPlane(dst[3], dstStride[3], dstW, dstY-lastDstY, lastDstY, 255);
  2485. #if HAVE_MMX2
  2486. if (av_get_cpu_flags() & AV_CPU_FLAG_MMX2)
  2487. __asm__ volatile("sfence":::"memory");
  2488. #endif
  2489. emms_c();
  2490. /* store changed local vars back in the context */
  2491. c->dstY= dstY;
  2492. c->lumBufIndex= lumBufIndex;
  2493. c->chrBufIndex= chrBufIndex;
  2494. c->lastInLumBuf= lastInLumBuf;
  2495. c->lastInChrBuf= lastInChrBuf;
  2496. return dstY - lastDstY;
  2497. }
  2498. static av_cold void sws_init_swScale_c(SwsContext *c)
  2499. {
  2500. enum PixelFormat srcFormat = c->srcFormat;
  2501. find_c_packed_planar_out_funcs(c, &c->yuv2yuv1, &c->yuv2yuvX,
  2502. &c->yuv2packed1, &c->yuv2packed2,
  2503. &c->yuv2packedX);
  2504. c->chrToYV12 = NULL;
  2505. switch(srcFormat) {
  2506. case PIX_FMT_YUYV422 : c->chrToYV12 = yuy2ToUV_c; break;
  2507. case PIX_FMT_UYVY422 : c->chrToYV12 = uyvyToUV_c; break;
  2508. case PIX_FMT_NV12 : c->chrToYV12 = nv12ToUV_c; break;
  2509. case PIX_FMT_NV21 : c->chrToYV12 = nv21ToUV_c; break;
  2510. case PIX_FMT_RGB8 :
  2511. case PIX_FMT_BGR8 :
  2512. case PIX_FMT_PAL8 :
  2513. case PIX_FMT_BGR4_BYTE:
  2514. case PIX_FMT_RGB4_BYTE: c->chrToYV12 = palToUV_c; break;
  2515. case PIX_FMT_YUV444P9BE:
  2516. case PIX_FMT_YUV420P9BE:
  2517. case PIX_FMT_YUV444P10BE:
  2518. case PIX_FMT_YUV422P10BE:
  2519. case PIX_FMT_YUV420P10BE: c->hScale16= HAVE_BIGENDIAN ? hScale16N_c : hScale16NX_c; break;
  2520. case PIX_FMT_YUV444P9LE:
  2521. case PIX_FMT_YUV420P9LE:
  2522. case PIX_FMT_YUV422P10LE:
  2523. case PIX_FMT_YUV420P10LE:
  2524. case PIX_FMT_YUV444P10LE: c->hScale16= HAVE_BIGENDIAN ? hScale16NX_c : hScale16N_c; break;
  2525. #if HAVE_BIGENDIAN
  2526. case PIX_FMT_YUV420P16LE:
  2527. case PIX_FMT_YUV422P16LE:
  2528. case PIX_FMT_YUV444P16LE: c->chrToYV12 = bswap16UV_c; break;
  2529. #else
  2530. case PIX_FMT_YUV420P16BE:
  2531. case PIX_FMT_YUV422P16BE:
  2532. case PIX_FMT_YUV444P16BE: c->chrToYV12 = bswap16UV_c; break;
  2533. #endif
  2534. }
  2535. if (c->chrSrcHSubSample) {
  2536. switch(srcFormat) {
  2537. case PIX_FMT_RGB48BE : c->chrToYV12 = rgb48BEToUV_half_c; break;
  2538. case PIX_FMT_RGB48LE : c->chrToYV12 = rgb48LEToUV_half_c; break;
  2539. case PIX_FMT_BGR48BE : c->chrToYV12 = bgr48BEToUV_half_c; break;
  2540. case PIX_FMT_BGR48LE : c->chrToYV12 = bgr48LEToUV_half_c; break;
  2541. case PIX_FMT_RGB32 : c->chrToYV12 = bgr32ToUV_half_c; break;
  2542. case PIX_FMT_RGB32_1 : c->chrToYV12 = bgr321ToUV_half_c; break;
  2543. case PIX_FMT_BGR24 : c->chrToYV12 = bgr24ToUV_half_c; break;
  2544. case PIX_FMT_BGR565LE: c->chrToYV12 = bgr16leToUV_half_c; break;
  2545. case PIX_FMT_BGR565BE: c->chrToYV12 = bgr16beToUV_half_c; break;
  2546. case PIX_FMT_BGR555LE: c->chrToYV12 = bgr15leToUV_half_c; break;
  2547. case PIX_FMT_BGR555BE: c->chrToYV12 = bgr15beToUV_half_c; break;
  2548. case PIX_FMT_BGR32 : c->chrToYV12 = rgb32ToUV_half_c; break;
  2549. case PIX_FMT_BGR32_1 : c->chrToYV12 = rgb321ToUV_half_c; break;
  2550. case PIX_FMT_RGB24 : c->chrToYV12 = rgb24ToUV_half_c; break;
  2551. case PIX_FMT_RGB565LE: c->chrToYV12 = rgb16leToUV_half_c; break;
  2552. case PIX_FMT_RGB565BE: c->chrToYV12 = rgb16beToUV_half_c; break;
  2553. case PIX_FMT_RGB555LE: c->chrToYV12 = rgb15leToUV_half_c; break;
  2554. case PIX_FMT_RGB555BE: c->chrToYV12 = rgb15beToUV_half_c; break;
  2555. }
  2556. } else {
  2557. switch(srcFormat) {
  2558. case PIX_FMT_RGB48BE : c->chrToYV12 = rgb48BEToUV_c; break;
  2559. case PIX_FMT_RGB48LE : c->chrToYV12 = rgb48LEToUV_c; break;
  2560. case PIX_FMT_BGR48BE : c->chrToYV12 = bgr48BEToUV_c; break;
  2561. case PIX_FMT_BGR48LE : c->chrToYV12 = bgr48LEToUV_c; break;
  2562. case PIX_FMT_RGB32 : c->chrToYV12 = bgr32ToUV_c; break;
  2563. case PIX_FMT_RGB32_1 : c->chrToYV12 = bgr321ToUV_c; break;
  2564. case PIX_FMT_BGR24 : c->chrToYV12 = bgr24ToUV_c; break;
  2565. case PIX_FMT_BGR565LE: c->chrToYV12 = bgr16leToUV_c; break;
  2566. case PIX_FMT_BGR565BE: c->chrToYV12 = bgr16beToUV_c; break;
  2567. case PIX_FMT_BGR555LE: c->chrToYV12 = bgr15leToUV_c; break;
  2568. case PIX_FMT_BGR555BE: c->chrToYV12 = bgr15beToUV_c; break;
  2569. case PIX_FMT_BGR32 : c->chrToYV12 = rgb32ToUV_c; break;
  2570. case PIX_FMT_BGR32_1 : c->chrToYV12 = rgb321ToUV_c; break;
  2571. case PIX_FMT_RGB24 : c->chrToYV12 = rgb24ToUV_c; break;
  2572. case PIX_FMT_RGB565LE: c->chrToYV12 = rgb16leToUV_c; break;
  2573. case PIX_FMT_RGB565BE: c->chrToYV12 = rgb16beToUV_c; break;
  2574. case PIX_FMT_RGB555LE: c->chrToYV12 = rgb15leToUV_c; break;
  2575. case PIX_FMT_RGB555BE: c->chrToYV12 = rgb15beToUV_c; break;
  2576. }
  2577. }
  2578. c->lumToYV12 = NULL;
  2579. c->alpToYV12 = NULL;
  2580. switch (srcFormat) {
  2581. #if HAVE_BIGENDIAN
  2582. case PIX_FMT_YUV420P16LE:
  2583. case PIX_FMT_YUV422P16LE:
  2584. case PIX_FMT_YUV444P16LE:
  2585. case PIX_FMT_GRAY16LE: c->lumToYV12 = bswap16Y_c; break;
  2586. #else
  2587. case PIX_FMT_YUV420P16BE:
  2588. case PIX_FMT_YUV422P16BE:
  2589. case PIX_FMT_YUV444P16BE:
  2590. case PIX_FMT_GRAY16BE: c->lumToYV12 = bswap16Y_c; break;
  2591. #endif
  2592. case PIX_FMT_YUYV422 :
  2593. case PIX_FMT_Y400A : c->lumToYV12 = yuy2ToY_c; break;
  2594. case PIX_FMT_UYVY422 : c->lumToYV12 = uyvyToY_c; break;
  2595. case PIX_FMT_BGR24 : c->lumToYV12 = bgr24ToY_c; break;
  2596. case PIX_FMT_BGR565LE : c->lumToYV12 = bgr16leToY_c; break;
  2597. case PIX_FMT_BGR565BE : c->lumToYV12 = bgr16beToY_c; break;
  2598. case PIX_FMT_BGR555LE : c->lumToYV12 = bgr15leToY_c; break;
  2599. case PIX_FMT_BGR555BE : c->lumToYV12 = bgr15beToY_c; break;
  2600. case PIX_FMT_RGB24 : c->lumToYV12 = rgb24ToY_c; break;
  2601. case PIX_FMT_RGB565LE : c->lumToYV12 = rgb16leToY_c; break;
  2602. case PIX_FMT_RGB565BE : c->lumToYV12 = rgb16beToY_c; break;
  2603. case PIX_FMT_RGB555LE : c->lumToYV12 = rgb15leToY_c; break;
  2604. case PIX_FMT_RGB555BE : c->lumToYV12 = rgb15beToY_c; break;
  2605. case PIX_FMT_RGB8 :
  2606. case PIX_FMT_BGR8 :
  2607. case PIX_FMT_PAL8 :
  2608. case PIX_FMT_BGR4_BYTE:
  2609. case PIX_FMT_RGB4_BYTE: c->lumToYV12 = palToY_c; break;
  2610. case PIX_FMT_MONOBLACK: c->lumToYV12 = monoblack2Y_c; break;
  2611. case PIX_FMT_MONOWHITE: c->lumToYV12 = monowhite2Y_c; break;
  2612. case PIX_FMT_RGB32 : c->lumToYV12 = bgr32ToY_c; break;
  2613. case PIX_FMT_RGB32_1: c->lumToYV12 = bgr321ToY_c; break;
  2614. case PIX_FMT_BGR32 : c->lumToYV12 = rgb32ToY_c; break;
  2615. case PIX_FMT_BGR32_1: c->lumToYV12 = rgb321ToY_c; break;
  2616. case PIX_FMT_RGB48BE: c->lumToYV12 = rgb48BEToY_c; break;
  2617. case PIX_FMT_RGB48LE: c->lumToYV12 = rgb48LEToY_c; break;
  2618. case PIX_FMT_BGR48BE: c->lumToYV12 = bgr48BEToY_c; break;
  2619. case PIX_FMT_BGR48LE: c->lumToYV12 = bgr48LEToY_c; break;
  2620. }
  2621. if (c->alpPixBuf) {
  2622. switch (srcFormat) {
  2623. case PIX_FMT_BGRA:
  2624. case PIX_FMT_RGBA: c->alpToYV12 = rgbaToA_c; break;
  2625. case PIX_FMT_ABGR:
  2626. case PIX_FMT_ARGB: c->alpToYV12 = abgrToA_c; break;
  2627. case PIX_FMT_Y400A: c->alpToYV12 = uyvyToY_c; break;
  2628. case PIX_FMT_PAL8 : c->alpToYV12 = palToA_c; break;
  2629. }
  2630. }
  2631. if((isAnyRGB(c->srcFormat) && av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1<15)
  2632. || c->srcFormat == PIX_FMT_PAL8)
  2633. c->hScale16= hScale16N_c;
  2634. if (c->scalingBpp == 8) {
  2635. c->hScale = hScale_c;
  2636. if (c->flags & SWS_FAST_BILINEAR) {
  2637. c->hyscale_fast = hyscale_fast_c;
  2638. c->hcscale_fast = hcscale_fast_c;
  2639. }
  2640. if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
  2641. if (c->srcRange) {
  2642. c->lumConvertRange = lumRangeFromJpeg_c;
  2643. c->chrConvertRange = chrRangeFromJpeg_c;
  2644. } else {
  2645. c->lumConvertRange = lumRangeToJpeg_c;
  2646. c->chrConvertRange = chrRangeToJpeg_c;
  2647. }
  2648. }
  2649. } else {
  2650. if(c->hScale16 == hScale16NX_c && !isAnyRGB(c->srcFormat)){
  2651. c->chrToYV12 = bswap16UV_c;
  2652. c->lumToYV12 = bswap16Y_c;
  2653. }
  2654. c->hScale16 = NULL;
  2655. c->hScale = hScale16_c;
  2656. c->scale19To15Fw = scale19To15Fw_c;
  2657. c->scale8To16Rv = scale8To16Rv_c;
  2658. if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
  2659. if (c->srcRange) {
  2660. c->lumConvertRange = lumRangeFromJpeg16_c;
  2661. c->chrConvertRange = chrRangeFromJpeg16_c;
  2662. } else {
  2663. c->lumConvertRange = lumRangeToJpeg16_c;
  2664. c->chrConvertRange = chrRangeToJpeg16_c;
  2665. }
  2666. }
  2667. }
  2668. if (!(isGray(srcFormat) || isGray(c->dstFormat) ||
  2669. srcFormat == PIX_FMT_MONOBLACK || srcFormat == PIX_FMT_MONOWHITE))
  2670. c->needs_hcscale = 1;
  2671. }
  2672. SwsFunc ff_getSwsFunc(SwsContext *c)
  2673. {
  2674. sws_init_swScale_c(c);
  2675. if (HAVE_MMX)
  2676. ff_sws_init_swScale_mmx(c);
  2677. if (HAVE_ALTIVEC)
  2678. ff_sws_init_swScale_altivec(c);
  2679. return swScale;
  2680. }