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.

3069 lines
115KB

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