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.

2840 lines
105KB

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