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.

2920 lines
109KB

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