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.

2881 lines
106KB

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