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.

1978 lines
72KB

  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. static const double rgb2yuv_table[8][9]={
  76. {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
  77. {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
  78. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  79. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  80. {0.59 , 0.11 , 0.30 , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
  81. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  82. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  83. {0.701 , 0.087 , 0.212 , -0.384, 0.5, -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
  84. };
  85. /*
  86. NOTES
  87. Special versions: fast Y 1:1 scaling (no interpolation in y direction)
  88. TODO
  89. more intelligent misalignment avoidance for the horizontal scaler
  90. write special vertical cubic upscale version
  91. optimize C code (YV12 / minmax)
  92. add support for packed pixel YUV input & output
  93. add support for Y8 output
  94. optimize BGR24 & BGR32
  95. add BGR4 output support
  96. write special BGR->BGR scaler
  97. */
  98. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
  99. { 1, 3, 1, 3, 1, 3, 1, 3, },
  100. { 2, 0, 2, 0, 2, 0, 2, 0, },
  101. };
  102. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
  103. { 6, 2, 6, 2, 6, 2, 6, 2, },
  104. { 0, 4, 0, 4, 0, 4, 0, 4, },
  105. };
  106. DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[4][8]={
  107. { 8, 4, 11, 7, 8, 4, 11, 7, },
  108. { 2, 14, 1, 13, 2, 14, 1, 13, },
  109. { 10, 6, 9, 5, 10, 6, 9, 5, },
  110. { 0, 12, 3, 15, 0, 12, 3, 15, },
  111. };
  112. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
  113. { 17, 9, 23, 15, 16, 8, 22, 14, },
  114. { 5, 29, 3, 27, 4, 28, 2, 26, },
  115. { 21, 13, 19, 11, 20, 12, 18, 10, },
  116. { 0, 24, 6, 30, 1, 25, 7, 31, },
  117. { 16, 8, 22, 14, 17, 9, 23, 15, },
  118. { 4, 28, 2, 26, 5, 29, 3, 27, },
  119. { 20, 12, 18, 10, 21, 13, 19, 11, },
  120. { 1, 25, 7, 31, 0, 24, 6, 30, },
  121. };
  122. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
  123. { 0, 55, 14, 68, 3, 58, 17, 72, },
  124. { 37, 18, 50, 32, 40, 22, 54, 35, },
  125. { 9, 64, 5, 59, 13, 67, 8, 63, },
  126. { 46, 27, 41, 23, 49, 31, 44, 26, },
  127. { 2, 57, 16, 71, 1, 56, 15, 70, },
  128. { 39, 21, 52, 34, 38, 19, 51, 33, },
  129. { 11, 66, 7, 62, 10, 65, 6, 60, },
  130. { 48, 30, 43, 25, 47, 29, 42, 24, },
  131. };
  132. #if 1
  133. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  134. {117, 62, 158, 103, 113, 58, 155, 100, },
  135. { 34, 199, 21, 186, 31, 196, 17, 182, },
  136. {144, 89, 131, 76, 141, 86, 127, 72, },
  137. { 0, 165, 41, 206, 10, 175, 52, 217, },
  138. {110, 55, 151, 96, 120, 65, 162, 107, },
  139. { 28, 193, 14, 179, 38, 203, 24, 189, },
  140. {138, 83, 124, 69, 148, 93, 134, 79, },
  141. { 7, 172, 48, 213, 3, 168, 45, 210, },
  142. };
  143. #elif 1
  144. // tries to correct a gamma of 1.5
  145. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  146. { 0, 143, 18, 200, 2, 156, 25, 215, },
  147. { 78, 28, 125, 64, 89, 36, 138, 74, },
  148. { 10, 180, 3, 161, 16, 195, 8, 175, },
  149. {109, 51, 93, 38, 121, 60, 105, 47, },
  150. { 1, 152, 23, 210, 0, 147, 20, 205, },
  151. { 85, 33, 134, 71, 81, 30, 130, 67, },
  152. { 14, 190, 6, 171, 12, 185, 5, 166, },
  153. {117, 57, 101, 44, 113, 54, 97, 41, },
  154. };
  155. #elif 1
  156. // tries to correct a gamma of 2.0
  157. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  158. { 0, 124, 8, 193, 0, 140, 12, 213, },
  159. { 55, 14, 104, 42, 66, 19, 119, 52, },
  160. { 3, 168, 1, 145, 6, 187, 3, 162, },
  161. { 86, 31, 70, 21, 99, 39, 82, 28, },
  162. { 0, 134, 11, 206, 0, 129, 9, 200, },
  163. { 62, 17, 114, 48, 58, 16, 109, 45, },
  164. { 5, 181, 2, 157, 4, 175, 1, 151, },
  165. { 95, 36, 78, 26, 90, 34, 74, 24, },
  166. };
  167. #else
  168. // tries to correct a gamma of 2.5
  169. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  170. { 0, 107, 3, 187, 0, 125, 6, 212, },
  171. { 39, 7, 86, 28, 49, 11, 102, 36, },
  172. { 1, 158, 0, 131, 3, 180, 1, 151, },
  173. { 68, 19, 52, 12, 81, 25, 64, 17, },
  174. { 0, 119, 5, 203, 0, 113, 4, 195, },
  175. { 45, 9, 96, 33, 42, 8, 91, 30, },
  176. { 2, 172, 1, 144, 2, 165, 0, 137, },
  177. { 77, 23, 60, 15, 72, 21, 56, 14, },
  178. };
  179. #endif
  180. static av_always_inline void
  181. yuv2yuvX16_c_template(const int16_t *lumFilter, const int16_t **lumSrc,
  182. int lumFilterSize, const int16_t *chrFilter,
  183. const int16_t **chrUSrc, const int16_t **chrVSrc,
  184. int chrFilterSize, const int16_t **alpSrc,
  185. uint16_t *dest, uint16_t *uDest, uint16_t *vDest,
  186. uint16_t *aDest, int dstW, int chrDstW,
  187. int big_endian, int output_bits)
  188. {
  189. //FIXME Optimize (just quickly written not optimized..)
  190. int i;
  191. int shift = 11 + 16 - output_bits;
  192. #define output_pixel(pos, val) \
  193. if (big_endian) { \
  194. if (output_bits == 16) { \
  195. AV_WB16(pos, av_clip_uint16(val >> shift)); \
  196. } else { \
  197. AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  198. } \
  199. } else { \
  200. if (output_bits == 16) { \
  201. AV_WL16(pos, av_clip_uint16(val >> shift)); \
  202. } else { \
  203. AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  204. } \
  205. }
  206. for (i = 0; i < dstW; i++) {
  207. int val = 1 << (26-output_bits);
  208. int j;
  209. for (j = 0; j < lumFilterSize; j++)
  210. val += lumSrc[j][i] * lumFilter[j];
  211. output_pixel(&dest[i], val);
  212. }
  213. if (uDest) {
  214. for (i = 0; i < chrDstW; i++) {
  215. int u = 1 << (26-output_bits);
  216. int v = 1 << (26-output_bits);
  217. int j;
  218. for (j = 0; j < chrFilterSize; j++) {
  219. u += chrUSrc[j][i] * chrFilter[j];
  220. v += chrVSrc[j][i] * chrFilter[j];
  221. }
  222. output_pixel(&uDest[i], u);
  223. output_pixel(&vDest[i], v);
  224. }
  225. }
  226. if (CONFIG_SWSCALE_ALPHA && aDest) {
  227. for (i = 0; i < dstW; i++) {
  228. int val = 1 << (26-output_bits);
  229. int j;
  230. for (j = 0; j < lumFilterSize; j++)
  231. val += alpSrc[j][i] * lumFilter[j];
  232. output_pixel(&aDest[i], val);
  233. }
  234. }
  235. #undef output_pixel
  236. }
  237. #define yuv2NBPS(bits, BE_LE, is_be) \
  238. static void yuv2yuvX ## bits ## BE_LE ## _c(SwsContext *c, const int16_t *lumFilter, \
  239. const int16_t **lumSrc, int lumFilterSize, \
  240. const int16_t *chrFilter, const int16_t **chrUSrc, \
  241. const int16_t **chrVSrc, \
  242. int chrFilterSize, const int16_t **alpSrc, \
  243. uint8_t *_dest, uint8_t *_uDest, uint8_t *_vDest, \
  244. uint8_t *_aDest, int dstW, int chrDstW) \
  245. { \
  246. uint16_t *dest = (uint16_t *) _dest, *uDest = (uint16_t *) _uDest, \
  247. *vDest = (uint16_t *) _vDest, *aDest = (uint16_t *) _aDest; \
  248. yuv2yuvX16_c_template(lumFilter, lumSrc, lumFilterSize, \
  249. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  250. alpSrc, \
  251. dest, uDest, vDest, aDest, \
  252. dstW, chrDstW, is_be, bits); \
  253. }
  254. yuv2NBPS( 9, BE, 1);
  255. yuv2NBPS( 9, LE, 0);
  256. yuv2NBPS(10, BE, 1);
  257. yuv2NBPS(10, LE, 0);
  258. yuv2NBPS(16, BE, 1);
  259. yuv2NBPS(16, LE, 0);
  260. static inline void yuv2yuvX_c(SwsContext *c, const int16_t *lumFilter,
  261. const int16_t **lumSrc, int lumFilterSize,
  262. const int16_t *chrFilter, const int16_t **chrUSrc,
  263. const int16_t **chrVSrc,
  264. int chrFilterSize, const int16_t **alpSrc,
  265. uint8_t *dest, uint8_t *uDest, uint8_t *vDest,
  266. uint8_t *aDest, int dstW, int chrDstW)
  267. {
  268. //FIXME Optimize (just quickly written not optimized..)
  269. int i;
  270. for (i=0; i<dstW; i++) {
  271. int val=1<<18;
  272. int j;
  273. for (j=0; j<lumFilterSize; j++)
  274. val += lumSrc[j][i] * lumFilter[j];
  275. dest[i]= av_clip_uint8(val>>19);
  276. }
  277. if (uDest)
  278. for (i=0; i<chrDstW; i++) {
  279. int u=1<<18;
  280. int v=1<<18;
  281. int j;
  282. for (j=0; j<chrFilterSize; j++) {
  283. u += chrUSrc[j][i] * chrFilter[j];
  284. v += chrVSrc[j][i] * chrFilter[j];
  285. }
  286. uDest[i]= av_clip_uint8(u>>19);
  287. vDest[i]= av_clip_uint8(v>>19);
  288. }
  289. if (CONFIG_SWSCALE_ALPHA && aDest)
  290. for (i=0; i<dstW; i++) {
  291. int val=1<<18;
  292. int j;
  293. for (j=0; j<lumFilterSize; j++)
  294. val += alpSrc[j][i] * lumFilter[j];
  295. aDest[i]= av_clip_uint8(val>>19);
  296. }
  297. }
  298. static inline void yuv2nv12X_c(SwsContext *c, const int16_t *lumFilter,
  299. const int16_t **lumSrc, int lumFilterSize,
  300. const int16_t *chrFilter, const int16_t **chrUSrc,
  301. const int16_t **chrVSrc, int chrFilterSize,
  302. const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest,
  303. uint8_t *vDest, uint8_t *aDest,
  304. int dstW, int chrDstW)
  305. {
  306. enum PixelFormat dstFormat = c->dstFormat;
  307. //FIXME Optimize (just quickly written not optimized..)
  308. int i;
  309. for (i=0; i<dstW; i++) {
  310. int val=1<<18;
  311. int j;
  312. for (j=0; j<lumFilterSize; j++)
  313. val += lumSrc[j][i] * lumFilter[j];
  314. dest[i]= av_clip_uint8(val>>19);
  315. }
  316. if (!uDest)
  317. return;
  318. if (dstFormat == PIX_FMT_NV12)
  319. for (i=0; i<chrDstW; i++) {
  320. int u=1<<18;
  321. int v=1<<18;
  322. int j;
  323. for (j=0; j<chrFilterSize; j++) {
  324. u += chrUSrc[j][i] * chrFilter[j];
  325. v += chrVSrc[j][i] * chrFilter[j];
  326. }
  327. uDest[2*i]= av_clip_uint8(u>>19);
  328. uDest[2*i+1]= av_clip_uint8(v>>19);
  329. }
  330. else
  331. for (i=0; i<chrDstW; i++) {
  332. int u=1<<18;
  333. int v=1<<18;
  334. int j;
  335. for (j=0; j<chrFilterSize; j++) {
  336. u += chrUSrc[j][i] * chrFilter[j];
  337. v += chrVSrc[j][i] * chrFilter[j];
  338. }
  339. uDest[2*i]= av_clip_uint8(v>>19);
  340. uDest[2*i+1]= av_clip_uint8(u>>19);
  341. }
  342. }
  343. #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
  344. for (i=0; i<(dstW>>1); i++) {\
  345. int j;\
  346. int Y1 = 1<<18;\
  347. int Y2 = 1<<18;\
  348. int U = 1<<18;\
  349. int V = 1<<18;\
  350. int av_unused A1, A2;\
  351. type av_unused *r, *b, *g;\
  352. const int i2= 2*i;\
  353. \
  354. for (j=0; j<lumFilterSize; j++) {\
  355. Y1 += lumSrc[j][i2] * lumFilter[j];\
  356. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  357. }\
  358. for (j=0; j<chrFilterSize; j++) {\
  359. U += chrUSrc[j][i] * chrFilter[j];\
  360. V += chrVSrc[j][i] * chrFilter[j];\
  361. }\
  362. Y1>>=19;\
  363. Y2>>=19;\
  364. U >>=19;\
  365. V >>=19;\
  366. if (alpha) {\
  367. A1 = 1<<18;\
  368. A2 = 1<<18;\
  369. for (j=0; j<lumFilterSize; j++) {\
  370. A1 += alpSrc[j][i2 ] * lumFilter[j];\
  371. A2 += alpSrc[j][i2+1] * lumFilter[j];\
  372. }\
  373. A1>>=19;\
  374. A2>>=19;\
  375. }
  376. #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
  377. YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
  378. if ((Y1|Y2|U|V)&256) {\
  379. if (Y1>255) Y1=255; \
  380. else if (Y1<0)Y1=0; \
  381. if (Y2>255) Y2=255; \
  382. else if (Y2<0)Y2=0; \
  383. if (U>255) U=255; \
  384. else if (U<0) U=0; \
  385. if (V>255) V=255; \
  386. else if (V<0) V=0; \
  387. }\
  388. if (alpha && ((A1|A2)&256)) {\
  389. A1=av_clip_uint8(A1);\
  390. A2=av_clip_uint8(A2);\
  391. }
  392. #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
  393. for (i=0; i<dstW; i++) {\
  394. int j;\
  395. int Y = 0;\
  396. int U = -128<<19;\
  397. int V = -128<<19;\
  398. int av_unused A;\
  399. int R,G,B;\
  400. \
  401. for (j=0; j<lumFilterSize; j++) {\
  402. Y += lumSrc[j][i ] * lumFilter[j];\
  403. }\
  404. for (j=0; j<chrFilterSize; j++) {\
  405. U += chrUSrc[j][i] * chrFilter[j];\
  406. V += chrVSrc[j][i] * chrFilter[j];\
  407. }\
  408. Y >>=10;\
  409. U >>=10;\
  410. V >>=10;\
  411. if (alpha) {\
  412. A = rnd;\
  413. for (j=0; j<lumFilterSize; j++)\
  414. A += alpSrc[j][i ] * lumFilter[j];\
  415. A >>=19;\
  416. if (A&256)\
  417. A = av_clip_uint8(A);\
  418. }
  419. #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
  420. YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
  421. Y-= c->yuv2rgb_y_offset;\
  422. Y*= c->yuv2rgb_y_coeff;\
  423. Y+= rnd;\
  424. R= Y + V*c->yuv2rgb_v2r_coeff;\
  425. G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
  426. B= Y + U*c->yuv2rgb_u2b_coeff;\
  427. if ((R|G|B)&(0xC0000000)) {\
  428. if (R>=(256<<22)) R=(256<<22)-1; \
  429. else if (R<0)R=0; \
  430. if (G>=(256<<22)) G=(256<<22)-1; \
  431. else if (G<0)G=0; \
  432. if (B>=(256<<22)) B=(256<<22)-1; \
  433. else if (B<0)B=0; \
  434. }
  435. #define YSCALE_YUV_2_GRAY16_C \
  436. for (i=0; i<(dstW>>1); i++) {\
  437. int j;\
  438. int Y1 = 1<<18;\
  439. int Y2 = 1<<18;\
  440. int U = 1<<18;\
  441. int V = 1<<18;\
  442. \
  443. const int i2= 2*i;\
  444. \
  445. for (j=0; j<lumFilterSize; j++) {\
  446. Y1 += lumSrc[j][i2] * lumFilter[j];\
  447. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  448. }\
  449. Y1>>=11;\
  450. Y2>>=11;\
  451. if ((Y1|Y2|U|V)&65536) {\
  452. if (Y1>65535) Y1=65535; \
  453. else if (Y1<0)Y1=0; \
  454. if (Y2>65535) Y2=65535; \
  455. else if (Y2<0)Y2=0; \
  456. }
  457. #define YSCALE_YUV_2_RGBX_C(type,alpha) \
  458. YSCALE_YUV_2_PACKEDX_C(type,alpha) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
  459. r = (type *)c->table_rV[V]; \
  460. g = (type *)(c->table_gU[U] + c->table_gV[V]); \
  461. b = (type *)c->table_bU[U];
  462. #define YSCALE_YUV_2_PACKED2_C(type,alpha) \
  463. for (i=0; i<(dstW>>1); i++) { \
  464. const int i2= 2*i; \
  465. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
  466. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
  467. int U= (ubuf0[i]*uvalpha1+ubuf1[i]*uvalpha)>>19; \
  468. int V= (vbuf0[i]*uvalpha1+vbuf1[i]*uvalpha)>>19; \
  469. type av_unused *r, *b, *g; \
  470. int av_unused A1, A2; \
  471. if (alpha) {\
  472. A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
  473. A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
  474. }
  475. #define YSCALE_YUV_2_GRAY16_2_C \
  476. for (i=0; i<(dstW>>1); i++) { \
  477. const int i2= 2*i; \
  478. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
  479. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;
  480. #define YSCALE_YUV_2_RGB2_C(type,alpha) \
  481. YSCALE_YUV_2_PACKED2_C(type,alpha)\
  482. r = (type *)c->table_rV[V];\
  483. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  484. b = (type *)c->table_bU[U];
  485. #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
  486. for (i=0; i<(dstW>>1); i++) {\
  487. const int i2= 2*i;\
  488. int Y1= buf0[i2 ]>>7;\
  489. int Y2= buf0[i2+1]>>7;\
  490. int U= (ubuf1[i])>>7;\
  491. int V= (vbuf1[i])>>7;\
  492. type av_unused *r, *b, *g;\
  493. int av_unused A1, A2;\
  494. if (alpha) {\
  495. A1= abuf0[i2 ]>>7;\
  496. A2= abuf0[i2+1]>>7;\
  497. }
  498. #define YSCALE_YUV_2_GRAY16_1_C \
  499. for (i=0; i<(dstW>>1); i++) {\
  500. const int i2= 2*i;\
  501. int Y1= buf0[i2 ]<<1;\
  502. int Y2= buf0[i2+1]<<1;
  503. #define YSCALE_YUV_2_RGB1_C(type,alpha) \
  504. YSCALE_YUV_2_PACKED1_C(type,alpha)\
  505. r = (type *)c->table_rV[V];\
  506. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  507. b = (type *)c->table_bU[U];
  508. #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
  509. for (i=0; i<(dstW>>1); i++) {\
  510. const int i2= 2*i;\
  511. int Y1= buf0[i2 ]>>7;\
  512. int Y2= buf0[i2+1]>>7;\
  513. int U= (ubuf0[i] + ubuf1[i])>>8;\
  514. int V= (vbuf0[i] + vbuf1[i])>>8;\
  515. type av_unused *r, *b, *g;\
  516. int av_unused A1, A2;\
  517. if (alpha) {\
  518. A1= abuf0[i2 ]>>7;\
  519. A2= abuf0[i2+1]>>7;\
  520. }
  521. #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
  522. YSCALE_YUV_2_PACKED1B_C(type,alpha)\
  523. r = (type *)c->table_rV[V];\
  524. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  525. b = (type *)c->table_bU[U];
  526. #define YSCALE_YUV_2_MONO2_C \
  527. const uint8_t * const d128=dither_8x8_220[y&7];\
  528. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  529. for (i=0; i<dstW-7; i+=8) {\
  530. int acc;\
  531. acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
  532. acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
  533. acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
  534. acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
  535. acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
  536. acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
  537. acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
  538. acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
  539. ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
  540. dest++;\
  541. }
  542. #define YSCALE_YUV_2_MONOX_C \
  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 acc=0;\
  546. for (i=0; i<dstW-1; i+=2) {\
  547. int j;\
  548. int Y1=1<<18;\
  549. int Y2=1<<18;\
  550. \
  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)&256) {\
  558. if (Y1>255) Y1=255;\
  559. else if (Y1<0)Y1=0;\
  560. if (Y2>255) Y2=255;\
  561. else if (Y2<0)Y2=0;\
  562. }\
  563. acc+= acc + g[Y1+d128[(i+0)&7]];\
  564. acc+= acc + g[Y2+d128[(i+1)&7]];\
  565. if ((i&7)==6) {\
  566. ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
  567. dest++;\
  568. }\
  569. }
  570. #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
  571. switch(c->dstFormat) {\
  572. case PIX_FMT_RGB48BE:\
  573. case PIX_FMT_RGB48LE:\
  574. func(uint8_t,0)\
  575. ((uint8_t*)dest)[ 0]= r[Y1];\
  576. ((uint8_t*)dest)[ 1]= r[Y1];\
  577. ((uint8_t*)dest)[ 2]= g[Y1];\
  578. ((uint8_t*)dest)[ 3]= g[Y1];\
  579. ((uint8_t*)dest)[ 4]= b[Y1];\
  580. ((uint8_t*)dest)[ 5]= b[Y1];\
  581. ((uint8_t*)dest)[ 6]= r[Y2];\
  582. ((uint8_t*)dest)[ 7]= r[Y2];\
  583. ((uint8_t*)dest)[ 8]= g[Y2];\
  584. ((uint8_t*)dest)[ 9]= g[Y2];\
  585. ((uint8_t*)dest)[10]= b[Y2];\
  586. ((uint8_t*)dest)[11]= b[Y2];\
  587. dest+=12;\
  588. }\
  589. break;\
  590. case PIX_FMT_BGR48BE:\
  591. case PIX_FMT_BGR48LE:\
  592. func(uint8_t,0)\
  593. ((uint8_t*)dest)[ 0] = ((uint8_t*)dest)[ 1] = b[Y1];\
  594. ((uint8_t*)dest)[ 2] = ((uint8_t*)dest)[ 3] = g[Y1];\
  595. ((uint8_t*)dest)[ 4] = ((uint8_t*)dest)[ 5] = r[Y1];\
  596. ((uint8_t*)dest)[ 6] = ((uint8_t*)dest)[ 7] = b[Y2];\
  597. ((uint8_t*)dest)[ 8] = ((uint8_t*)dest)[ 9] = g[Y2];\
  598. ((uint8_t*)dest)[10] = ((uint8_t*)dest)[11] = r[Y2];\
  599. dest+=12;\
  600. }\
  601. break;\
  602. case PIX_FMT_RGBA:\
  603. case PIX_FMT_BGRA:\
  604. if (CONFIG_SMALL) {\
  605. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
  606. func(uint32_t,needAlpha)\
  607. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
  608. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
  609. }\
  610. } else {\
  611. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
  612. func(uint32_t,1)\
  613. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
  614. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
  615. }\
  616. } else {\
  617. func(uint32_t,0)\
  618. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  619. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  620. }\
  621. }\
  622. }\
  623. break;\
  624. case PIX_FMT_ARGB:\
  625. case PIX_FMT_ABGR:\
  626. if (CONFIG_SMALL) {\
  627. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
  628. func(uint32_t,needAlpha)\
  629. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
  630. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
  631. }\
  632. } else {\
  633. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
  634. func(uint32_t,1)\
  635. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
  636. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
  637. }\
  638. } else {\
  639. func(uint32_t,0)\
  640. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  641. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  642. }\
  643. }\
  644. } \
  645. break;\
  646. case PIX_FMT_RGB24:\
  647. func(uint8_t,0)\
  648. ((uint8_t*)dest)[0]= r[Y1];\
  649. ((uint8_t*)dest)[1]= g[Y1];\
  650. ((uint8_t*)dest)[2]= b[Y1];\
  651. ((uint8_t*)dest)[3]= r[Y2];\
  652. ((uint8_t*)dest)[4]= g[Y2];\
  653. ((uint8_t*)dest)[5]= b[Y2];\
  654. dest+=6;\
  655. }\
  656. break;\
  657. case PIX_FMT_BGR24:\
  658. func(uint8_t,0)\
  659. ((uint8_t*)dest)[0]= b[Y1];\
  660. ((uint8_t*)dest)[1]= g[Y1];\
  661. ((uint8_t*)dest)[2]= r[Y1];\
  662. ((uint8_t*)dest)[3]= b[Y2];\
  663. ((uint8_t*)dest)[4]= g[Y2];\
  664. ((uint8_t*)dest)[5]= r[Y2];\
  665. dest+=6;\
  666. }\
  667. break;\
  668. case PIX_FMT_RGB565BE:\
  669. case PIX_FMT_RGB565LE:\
  670. case PIX_FMT_BGR565BE:\
  671. case PIX_FMT_BGR565LE:\
  672. {\
  673. const int dr1= dither_2x2_8[y&1 ][0];\
  674. const int dg1= dither_2x2_4[y&1 ][0];\
  675. const int db1= dither_2x2_8[(y&1)^1][0];\
  676. const int dr2= dither_2x2_8[y&1 ][1];\
  677. const int dg2= dither_2x2_4[y&1 ][1];\
  678. const int db2= dither_2x2_8[(y&1)^1][1];\
  679. func(uint16_t,0)\
  680. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  681. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  682. }\
  683. }\
  684. break;\
  685. case PIX_FMT_RGB555BE:\
  686. case PIX_FMT_RGB555LE:\
  687. case PIX_FMT_BGR555BE:\
  688. case PIX_FMT_BGR555LE:\
  689. {\
  690. const int dr1= dither_2x2_8[y&1 ][0];\
  691. const int dg1= dither_2x2_8[y&1 ][1];\
  692. const int db1= dither_2x2_8[(y&1)^1][0];\
  693. const int dr2= dither_2x2_8[y&1 ][1];\
  694. const int dg2= dither_2x2_8[y&1 ][0];\
  695. const int db2= dither_2x2_8[(y&1)^1][1];\
  696. func(uint16_t,0)\
  697. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  698. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  699. }\
  700. }\
  701. break;\
  702. case PIX_FMT_RGB444BE:\
  703. case PIX_FMT_RGB444LE:\
  704. case PIX_FMT_BGR444BE:\
  705. case PIX_FMT_BGR444LE:\
  706. {\
  707. const int dr1= dither_4x4_16[y&3 ][0];\
  708. const int dg1= dither_4x4_16[y&3 ][1];\
  709. const int db1= dither_4x4_16[(y&3)^3][0];\
  710. const int dr2= dither_4x4_16[y&3 ][1];\
  711. const int dg2= dither_4x4_16[y&3 ][0];\
  712. const int db2= dither_4x4_16[(y&3)^3][1];\
  713. func(uint16_t,0)\
  714. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  715. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  716. }\
  717. }\
  718. break;\
  719. case PIX_FMT_RGB8:\
  720. case PIX_FMT_BGR8:\
  721. {\
  722. const uint8_t * const d64= dither_8x8_73[y&7];\
  723. const uint8_t * const d32= dither_8x8_32[y&7];\
  724. func(uint8_t,0)\
  725. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
  726. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
  727. }\
  728. }\
  729. break;\
  730. case PIX_FMT_RGB4:\
  731. case PIX_FMT_BGR4:\
  732. {\
  733. const uint8_t * const d64= dither_8x8_73 [y&7];\
  734. const uint8_t * const d128=dither_8x8_220[y&7];\
  735. func(uint8_t,0)\
  736. ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
  737. + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
  738. }\
  739. }\
  740. break;\
  741. case PIX_FMT_RGB4_BYTE:\
  742. case PIX_FMT_BGR4_BYTE:\
  743. {\
  744. const uint8_t * const d64= dither_8x8_73 [y&7];\
  745. const uint8_t * const d128=dither_8x8_220[y&7];\
  746. func(uint8_t,0)\
  747. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
  748. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
  749. }\
  750. }\
  751. break;\
  752. case PIX_FMT_MONOBLACK:\
  753. case PIX_FMT_MONOWHITE:\
  754. {\
  755. func_monoblack\
  756. }\
  757. break;\
  758. case PIX_FMT_YUYV422:\
  759. func2\
  760. ((uint8_t*)dest)[2*i2+0]= Y1;\
  761. ((uint8_t*)dest)[2*i2+1]= U;\
  762. ((uint8_t*)dest)[2*i2+2]= Y2;\
  763. ((uint8_t*)dest)[2*i2+3]= V;\
  764. } \
  765. break;\
  766. case PIX_FMT_UYVY422:\
  767. func2\
  768. ((uint8_t*)dest)[2*i2+0]= U;\
  769. ((uint8_t*)dest)[2*i2+1]= Y1;\
  770. ((uint8_t*)dest)[2*i2+2]= V;\
  771. ((uint8_t*)dest)[2*i2+3]= Y2;\
  772. } \
  773. break;\
  774. case PIX_FMT_GRAY16BE:\
  775. func_g16\
  776. ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
  777. ((uint8_t*)dest)[2*i2+1]= Y1;\
  778. ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
  779. ((uint8_t*)dest)[2*i2+3]= Y2;\
  780. } \
  781. break;\
  782. case PIX_FMT_GRAY16LE:\
  783. func_g16\
  784. ((uint8_t*)dest)[2*i2+0]= Y1;\
  785. ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
  786. ((uint8_t*)dest)[2*i2+2]= Y2;\
  787. ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
  788. } \
  789. break;\
  790. }
  791. static void yuv2packedX_c(SwsContext *c, const int16_t *lumFilter,
  792. const int16_t **lumSrc, int lumFilterSize,
  793. const int16_t *chrFilter, const int16_t **chrUSrc,
  794. const int16_t **chrVSrc, int chrFilterSize,
  795. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  796. {
  797. int i;
  798. YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGBX_C, YSCALE_YUV_2_PACKEDX_C(void,0), YSCALE_YUV_2_GRAY16_C, YSCALE_YUV_2_MONOX_C)
  799. }
  800. static inline void yuv2rgbX_c_full(SwsContext *c, const int16_t *lumFilter,
  801. const int16_t **lumSrc, int lumFilterSize,
  802. const int16_t *chrFilter, const int16_t **chrUSrc,
  803. const int16_t **chrVSrc, int chrFilterSize,
  804. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  805. {
  806. int i;
  807. int step= c->dstFormatBpp/8;
  808. int aidx= 3;
  809. switch(c->dstFormat) {
  810. case PIX_FMT_ARGB:
  811. dest++;
  812. aidx= 0;
  813. case PIX_FMT_RGB24:
  814. aidx--;
  815. case PIX_FMT_RGBA:
  816. if (CONFIG_SMALL) {
  817. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
  818. YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
  819. dest[aidx]= needAlpha ? A : 255;
  820. dest[0]= R>>22;
  821. dest[1]= G>>22;
  822. dest[2]= B>>22;
  823. dest+= step;
  824. }
  825. } else {
  826. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  827. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
  828. dest[aidx]= A;
  829. dest[0]= R>>22;
  830. dest[1]= G>>22;
  831. dest[2]= B>>22;
  832. dest+= step;
  833. }
  834. } else {
  835. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
  836. dest[aidx]= 255;
  837. dest[0]= R>>22;
  838. dest[1]= G>>22;
  839. dest[2]= B>>22;
  840. dest+= step;
  841. }
  842. }
  843. }
  844. break;
  845. case PIX_FMT_ABGR:
  846. dest++;
  847. aidx= 0;
  848. case PIX_FMT_BGR24:
  849. aidx--;
  850. case PIX_FMT_BGRA:
  851. if (CONFIG_SMALL) {
  852. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
  853. YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
  854. dest[aidx]= needAlpha ? A : 255;
  855. dest[0]= B>>22;
  856. dest[1]= G>>22;
  857. dest[2]= R>>22;
  858. dest+= step;
  859. }
  860. } else {
  861. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  862. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
  863. dest[aidx]= A;
  864. dest[0]= B>>22;
  865. dest[1]= G>>22;
  866. dest[2]= R>>22;
  867. dest+= step;
  868. }
  869. } else {
  870. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
  871. dest[aidx]= 255;
  872. dest[0]= B>>22;
  873. dest[1]= G>>22;
  874. dest[2]= R>>22;
  875. dest+= step;
  876. }
  877. }
  878. }
  879. break;
  880. default:
  881. assert(0);
  882. }
  883. }
  884. static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
  885. {
  886. int i;
  887. uint8_t *ptr = plane + stride*y;
  888. for (i=0; i<height; i++) {
  889. memset(ptr, val, width);
  890. ptr += stride;
  891. }
  892. }
  893. #define rgb48funcs(LE_BE, rfunc, compA, compB, compC) \
  894. static void compA ## compB ## compC ## 48 ## LE_BE ## ToY_c( \
  895. uint8_t *dst, const uint8_t *src, int width, \
  896. uint32_t *unused) \
  897. { \
  898. int i; \
  899. for (i = 0; i < width; i++) { \
  900. int compA = rfunc(&src[i*6+0]) >> 8; \
  901. int compB = rfunc(&src[i*6+2]) >> 8; \
  902. int compC = rfunc(&src[i*6+4]) >> 8; \
  903. \
  904. dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT; \
  905. } \
  906. } \
  907. \
  908. static void compA ## compB ## compC ## 48 ## LE_BE ## ToUV_c( \
  909. uint8_t *dstU, uint8_t *dstV, \
  910. const uint8_t *src1, const uint8_t *src2, \
  911. int width, uint32_t *unused) \
  912. { \
  913. int i; \
  914. assert(src1==src2); \
  915. for (i = 0; i < width; i++) { \
  916. int compA = rfunc(&src1[6*i + 0]) >> 8; \
  917. int compB = rfunc(&src1[6*i + 2]) >> 8; \
  918. int compC = rfunc(&src1[6*i + 4]) >> 8; \
  919. \
  920. dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT; \
  921. dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT; \
  922. } \
  923. } \
  924. \
  925. static void compA ## compB ## compC ## 48 ## LE_BE ## ToUV_half_c( \
  926. uint8_t *dstU, uint8_t *dstV, \
  927. const uint8_t *src1, const uint8_t *src2, \
  928. int width, uint32_t *unused) \
  929. { \
  930. int i; \
  931. assert(src1==src2); \
  932. for (i = 0; i < width; i++) { \
  933. int compA = (rfunc(&src1[12*i + 0]) >> 8) + (rfunc(&src1[12*i + 6]) >> 8); \
  934. int compB = (rfunc(&src1[12*i + 2]) >> 8) + (rfunc(&src1[12*i + 8]) >> 8); \
  935. int compC = (rfunc(&src1[12*i + 4]) >> 8) + (rfunc(&src1[12*i + 10]) >> 8); \
  936. \
  937. dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1); \
  938. dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1); \
  939. } \
  940. }
  941. rgb48funcs(LE, AV_RL16, r, g, b);
  942. rgb48funcs(BE, AV_RB16, r, g, b);
  943. rgb48funcs(LE, AV_RL16, b, g, r);
  944. rgb48funcs(BE, AV_RB16, b, g, r);
  945. #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
  946. static void name ## _c(uint8_t *dst, const uint8_t *src, \
  947. int width, uint32_t *unused)\
  948. {\
  949. int i;\
  950. for (i=0; i<width; i++) {\
  951. int b= (((const type*)src)[i]>>shb)&maskb;\
  952. int g= (((const type*)src)[i]>>shg)&maskg;\
  953. int r= (((const type*)src)[i]>>shr)&maskr;\
  954. \
  955. dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
  956. }\
  957. }
  958. BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
  959. BGR2Y(uint32_t,bgr321ToY,16,16, 0, 0xFF00, 0x00FF, 0xFF00, RY , GY<<8, BY , RGB2YUV_SHIFT+8)
  960. BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
  961. BGR2Y(uint32_t,rgb321ToY, 0,16,16, 0xFF00, 0x00FF, 0xFF00, RY , GY<<8, BY , RGB2YUV_SHIFT+8)
  962. BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY , RGB2YUV_SHIFT+8)
  963. BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY , RGB2YUV_SHIFT+7)
  964. BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
  965. BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
  966. static void abgrToA_c(uint8_t *dst, const uint8_t *src, int width, uint32_t *unused)
  967. {
  968. int i;
  969. for (i=0; i<width; i++) {
  970. dst[i]= src[4*i];
  971. }
  972. }
  973. static void rgbaToA_c(uint8_t *dst, const uint8_t *src, int width, uint32_t *unused)
  974. {
  975. int i;
  976. for (i=0; i<width; i++) {
  977. dst[i]= src[4*i+3];
  978. }
  979. }
  980. #define BGR2UV(type, name, shr, shg, shb, shp, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S) \
  981. static void name ## _c(uint8_t *dstU, uint8_t *dstV, \
  982. const uint8_t *src, const uint8_t *dummy, \
  983. int width, uint32_t *unused)\
  984. {\
  985. int i;\
  986. for (i=0; i<width; i++) {\
  987. int b= ((((const type*)src)[i]>>shp)&maskb)>>shb;\
  988. int g= ((((const type*)src)[i]>>shp)&maskg)>>shg;\
  989. int r= ((((const type*)src)[i]>>shp)&maskr)>>shr;\
  990. \
  991. dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
  992. dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
  993. }\
  994. }\
  995. static void name ## _half_c(uint8_t *dstU, uint8_t *dstV, \
  996. const uint8_t *src, const uint8_t *dummy, \
  997. int width, uint32_t *unused)\
  998. {\
  999. int i;\
  1000. for (i=0; i<width; i++) {\
  1001. int pix0= ((const type*)src)[2*i+0]>>shp;\
  1002. int pix1= ((const type*)src)[2*i+1]>>shp;\
  1003. int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
  1004. int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
  1005. int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
  1006. g&= maskg|(2*maskg);\
  1007. \
  1008. g>>=shg;\
  1009. \
  1010. dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
  1011. dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
  1012. }\
  1013. }
  1014. BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1015. BGR2UV(uint32_t,bgr321ToUV,16, 0, 0, 8, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1016. BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1017. BGR2UV(uint32_t,rgb321ToUV, 0, 0,16, 8, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1018. BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RU<<11, GU<<5, BU , RV<<11, GV<<5, BV , RGB2YUV_SHIFT+8)
  1019. BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RU<<10, GU<<5, BU , RV<<10, GV<<5, BV , RGB2YUV_SHIFT+7)
  1020. BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RU , GU<<5, BU<<11, RV , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
  1021. BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RU , GU<<5, BU<<10, RV , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
  1022. static void palToY_c(uint8_t *dst, const uint8_t *src, int width, uint32_t *pal)
  1023. {
  1024. int i;
  1025. for (i=0; i<width; i++) {
  1026. int d= src[i];
  1027. dst[i]= pal[d] & 0xFF;
  1028. }
  1029. }
  1030. static void palToUV_c(uint8_t *dstU, uint8_t *dstV,
  1031. const uint8_t *src1, const uint8_t *src2,
  1032. int width, uint32_t *pal)
  1033. {
  1034. int i;
  1035. assert(src1 == src2);
  1036. for (i=0; i<width; i++) {
  1037. int p= pal[src1[i]];
  1038. dstU[i]= p>>8;
  1039. dstV[i]= p>>16;
  1040. }
  1041. }
  1042. static void monowhite2Y_c(uint8_t *dst, const uint8_t *src,
  1043. int width, uint32_t *unused)
  1044. {
  1045. int i, j;
  1046. for (i=0; i<width/8; i++) {
  1047. int d= ~src[i];
  1048. for(j=0; j<8; j++)
  1049. dst[8*i+j]= ((d>>(7-j))&1)*255;
  1050. }
  1051. }
  1052. static void monoblack2Y_c(uint8_t *dst, const uint8_t *src,
  1053. int width, uint32_t *unused)
  1054. {
  1055. int i, j;
  1056. for (i=0; i<width/8; i++) {
  1057. int d= src[i];
  1058. for(j=0; j<8; j++)
  1059. dst[8*i+j]= ((d>>(7-j))&1)*255;
  1060. }
  1061. }
  1062. static void yuv2yuv1_c(SwsContext *c, const int16_t *lumSrc,
  1063. const int16_t *chrUSrc, const int16_t *chrVSrc,
  1064. const int16_t *alpSrc,
  1065. uint8_t *dest, uint8_t *uDest, uint8_t *vDest,
  1066. uint8_t *aDest, int dstW, int chrDstW)
  1067. {
  1068. int i;
  1069. for (i=0; i<dstW; i++) {
  1070. int val= (lumSrc[i]+64)>>7;
  1071. dest[i]= av_clip_uint8(val);
  1072. }
  1073. if (uDest)
  1074. for (i=0; i<chrDstW; i++) {
  1075. int u=(chrUSrc[i]+64)>>7;
  1076. int v=(chrVSrc[i]+64)>>7;
  1077. uDest[i]= av_clip_uint8(u);
  1078. vDest[i]= av_clip_uint8(v);
  1079. }
  1080. if (CONFIG_SWSCALE_ALPHA && aDest)
  1081. for (i=0; i<dstW; i++) {
  1082. int val= (alpSrc[i]+64)>>7;
  1083. aDest[i]= av_clip_uint8(val);
  1084. }
  1085. }
  1086. /**
  1087. * vertical bilinear scale YV12 to RGB
  1088. */
  1089. static void yuv2packed2_c(SwsContext *c, const uint16_t *buf0,
  1090. const uint16_t *buf1, const uint16_t *ubuf0,
  1091. const uint16_t *ubuf1, const uint16_t *vbuf0,
  1092. const uint16_t *vbuf1, const uint16_t *abuf0,
  1093. const uint16_t *abuf1, uint8_t *dest, int dstW,
  1094. int yalpha, int uvalpha, int y)
  1095. {
  1096. int yalpha1=4095- yalpha;
  1097. int uvalpha1=4095-uvalpha;
  1098. int i;
  1099. YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGB2_C, YSCALE_YUV_2_PACKED2_C(void,0), YSCALE_YUV_2_GRAY16_2_C, YSCALE_YUV_2_MONO2_C)
  1100. }
  1101. /**
  1102. * YV12 to RGB without scaling or interpolating
  1103. */
  1104. static void yuv2packed1_c(SwsContext *c, const uint16_t *buf0,
  1105. const uint16_t *ubuf0, const uint16_t *ubuf1,
  1106. const uint16_t *vbuf0, const uint16_t *vbuf1,
  1107. const uint16_t *abuf0, uint8_t *dest, int dstW,
  1108. int uvalpha, enum PixelFormat dstFormat,
  1109. int flags, int y)
  1110. {
  1111. const int yalpha1=0;
  1112. int i;
  1113. const uint16_t *buf1= buf0; //FIXME needed for RGB1/BGR1
  1114. const int yalpha= 4096; //FIXME ...
  1115. if (uvalpha < 2048) {
  1116. YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGB1_C, YSCALE_YUV_2_PACKED1_C(void,0), YSCALE_YUV_2_GRAY16_1_C, YSCALE_YUV_2_MONO2_C)
  1117. } else {
  1118. YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGB1B_C, YSCALE_YUV_2_PACKED1B_C(void,0), YSCALE_YUV_2_GRAY16_1_C, YSCALE_YUV_2_MONO2_C)
  1119. }
  1120. }
  1121. //FIXME yuy2* can read up to 7 samples too much
  1122. static void yuy2ToY_c(uint8_t *dst, const uint8_t *src, int width,
  1123. uint32_t *unused)
  1124. {
  1125. int i;
  1126. for (i=0; i<width; i++)
  1127. dst[i]= src[2*i];
  1128. }
  1129. static void yuy2ToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  1130. const uint8_t *src2, int width, uint32_t *unused)
  1131. {
  1132. int i;
  1133. for (i=0; i<width; i++) {
  1134. dstU[i]= src1[4*i + 1];
  1135. dstV[i]= src1[4*i + 3];
  1136. }
  1137. assert(src1 == src2);
  1138. }
  1139. static void LEToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  1140. const uint8_t *src2, int width, uint32_t *unused)
  1141. {
  1142. int i;
  1143. for (i=0; i<width; i++) {
  1144. dstU[i]= src1[2*i + 1];
  1145. dstV[i]= src2[2*i + 1];
  1146. }
  1147. }
  1148. /* This is almost identical to the previous, end exists only because
  1149. * yuy2ToY/UV)(dst, src+1, ...) would have 100% unaligned accesses. */
  1150. static void uyvyToY_c(uint8_t *dst, const uint8_t *src, int width,
  1151. uint32_t *unused)
  1152. {
  1153. int i;
  1154. for (i=0; i<width; i++)
  1155. dst[i]= src[2*i+1];
  1156. }
  1157. static void uyvyToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  1158. const uint8_t *src2, int width, uint32_t *unused)
  1159. {
  1160. int i;
  1161. for (i=0; i<width; i++) {
  1162. dstU[i]= src1[4*i + 0];
  1163. dstV[i]= src1[4*i + 2];
  1164. }
  1165. assert(src1 == src2);
  1166. }
  1167. static void BEToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  1168. const uint8_t *src2, int width, uint32_t *unused)
  1169. {
  1170. int i;
  1171. for (i=0; i<width; i++) {
  1172. dstU[i]= src1[2*i];
  1173. dstV[i]= src2[2*i];
  1174. }
  1175. }
  1176. static av_always_inline void nvXXtoUV_c(uint8_t *dst1, uint8_t *dst2,
  1177. const uint8_t *src, int width)
  1178. {
  1179. int i;
  1180. for (i = 0; i < width; i++) {
  1181. dst1[i] = src[2*i+0];
  1182. dst2[i] = src[2*i+1];
  1183. }
  1184. }
  1185. static void nv12ToUV_c(uint8_t *dstU, uint8_t *dstV,
  1186. const uint8_t *src1, const uint8_t *src2,
  1187. int width, uint32_t *unused)
  1188. {
  1189. nvXXtoUV_c(dstU, dstV, src1, width);
  1190. }
  1191. static void nv21ToUV_c(uint8_t *dstU, uint8_t *dstV,
  1192. const uint8_t *src1, const uint8_t *src2,
  1193. int width, uint32_t *unused)
  1194. {
  1195. nvXXtoUV_c(dstV, dstU, src1, width);
  1196. }
  1197. // FIXME Maybe dither instead.
  1198. #define YUV_NBPS(depth, endianness, rfunc) \
  1199. static void endianness ## depth ## ToUV_c(uint8_t *dstU, uint8_t *dstV, \
  1200. const uint8_t *_srcU, const uint8_t *_srcV, \
  1201. int width, uint32_t *unused) \
  1202. { \
  1203. int i; \
  1204. const uint16_t *srcU = (const uint16_t*)_srcU; \
  1205. const uint16_t *srcV = (const uint16_t*)_srcV; \
  1206. for (i = 0; i < width; i++) { \
  1207. dstU[i] = rfunc(&srcU[i])>>(depth-8); \
  1208. dstV[i] = rfunc(&srcV[i])>>(depth-8); \
  1209. } \
  1210. } \
  1211. \
  1212. static void endianness ## depth ## ToY_c(uint8_t *dstY, const uint8_t *_srcY, \
  1213. int width, uint32_t *unused) \
  1214. { \
  1215. int i; \
  1216. const uint16_t *srcY = (const uint16_t*)_srcY; \
  1217. for (i = 0; i < width; i++) \
  1218. dstY[i] = rfunc(&srcY[i])>>(depth-8); \
  1219. } \
  1220. YUV_NBPS( 9, LE, AV_RL16)
  1221. YUV_NBPS( 9, BE, AV_RB16)
  1222. YUV_NBPS(10, LE, AV_RL16)
  1223. YUV_NBPS(10, BE, AV_RB16)
  1224. static void bgr24ToY_c(uint8_t *dst, const uint8_t *src,
  1225. int width, uint32_t *unused)
  1226. {
  1227. int i;
  1228. for (i=0; i<width; i++) {
  1229. int b= src[i*3+0];
  1230. int g= src[i*3+1];
  1231. int r= src[i*3+2];
  1232. dst[i]= ((RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1233. }
  1234. }
  1235. static void bgr24ToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  1236. const uint8_t *src2, int width, uint32_t *unused)
  1237. {
  1238. int i;
  1239. for (i=0; i<width; i++) {
  1240. int b= src1[3*i + 0];
  1241. int g= src1[3*i + 1];
  1242. int r= src1[3*i + 2];
  1243. dstU[i]= (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT;
  1244. dstV[i]= (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT;
  1245. }
  1246. assert(src1 == src2);
  1247. }
  1248. static void bgr24ToUV_half_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  1249. const uint8_t *src2, int width, uint32_t *unused)
  1250. {
  1251. int i;
  1252. for (i=0; i<width; i++) {
  1253. int b= src1[6*i + 0] + src1[6*i + 3];
  1254. int g= src1[6*i + 1] + src1[6*i + 4];
  1255. int r= src1[6*i + 2] + src1[6*i + 5];
  1256. dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT))>>(RGB2YUV_SHIFT+1);
  1257. dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT))>>(RGB2YUV_SHIFT+1);
  1258. }
  1259. assert(src1 == src2);
  1260. }
  1261. static void rgb24ToY_c(uint8_t *dst, const uint8_t *src, int width,
  1262. uint32_t *unused)
  1263. {
  1264. int i;
  1265. for (i=0; i<width; i++) {
  1266. int r= src[i*3+0];
  1267. int g= src[i*3+1];
  1268. int b= src[i*3+2];
  1269. dst[i]= ((RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1270. }
  1271. }
  1272. static void rgb24ToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  1273. const uint8_t *src2, int width, uint32_t *unused)
  1274. {
  1275. int i;
  1276. assert(src1==src2);
  1277. for (i=0; i<width; i++) {
  1278. int r= src1[3*i + 0];
  1279. int g= src1[3*i + 1];
  1280. int b= src1[3*i + 2];
  1281. dstU[i]= (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT;
  1282. dstV[i]= (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT;
  1283. }
  1284. }
  1285. static void rgb24ToUV_half_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  1286. const uint8_t *src2, int width, uint32_t *unused)
  1287. {
  1288. int i;
  1289. assert(src1==src2);
  1290. for (i=0; i<width; i++) {
  1291. int r= src1[6*i + 0] + src1[6*i + 3];
  1292. int g= src1[6*i + 1] + src1[6*i + 4];
  1293. int b= src1[6*i + 2] + src1[6*i + 5];
  1294. dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT))>>(RGB2YUV_SHIFT+1);
  1295. dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT))>>(RGB2YUV_SHIFT+1);
  1296. }
  1297. }
  1298. // bilinear / bicubic scaling
  1299. static void hScale_c(int16_t *dst, int dstW, const uint8_t *src,
  1300. int srcW, int xInc,
  1301. const int16_t *filter, const int16_t *filterPos,
  1302. int filterSize)
  1303. {
  1304. int i;
  1305. for (i=0; i<dstW; i++) {
  1306. int j;
  1307. int srcPos= filterPos[i];
  1308. int val=0;
  1309. for (j=0; j<filterSize; j++) {
  1310. val += ((int)src[srcPos + j])*filter[filterSize*i + j];
  1311. }
  1312. //filter += hFilterSize;
  1313. dst[i] = FFMIN(val>>7, (1<<15)-1); // the cubic equation does overflow ...
  1314. //dst[i] = val>>7;
  1315. }
  1316. }
  1317. //FIXME all pal and rgb srcFormats could do this convertion as well
  1318. //FIXME all scalers more complex than bilinear could do half of this transform
  1319. static void chrRangeToJpeg_c(uint16_t *dstU, uint16_t *dstV, int width)
  1320. {
  1321. int i;
  1322. for (i = 0; i < width; i++) {
  1323. dstU[i] = (FFMIN(dstU[i],30775)*4663 - 9289992)>>12; //-264
  1324. dstV[i] = (FFMIN(dstV[i],30775)*4663 - 9289992)>>12; //-264
  1325. }
  1326. }
  1327. static void chrRangeFromJpeg_c(uint16_t *dstU, uint16_t *dstV, int width)
  1328. {
  1329. int i;
  1330. for (i = 0; i < width; i++) {
  1331. dstU[i] = (dstU[i]*1799 + 4081085)>>11; //1469
  1332. dstV[i] = (dstV[i]*1799 + 4081085)>>11; //1469
  1333. }
  1334. }
  1335. static void lumRangeToJpeg_c(uint16_t *dst, int width)
  1336. {
  1337. int i;
  1338. for (i = 0; i < width; i++)
  1339. dst[i] = (FFMIN(dst[i],30189)*19077 - 39057361)>>14;
  1340. }
  1341. static void lumRangeFromJpeg_c(uint16_t *dst, int width)
  1342. {
  1343. int i;
  1344. for (i = 0; i < width; i++)
  1345. dst[i] = (dst[i]*14071 + 33561947)>>14;
  1346. }
  1347. static void hyscale_fast_c(SwsContext *c, int16_t *dst, int dstWidth,
  1348. const uint8_t *src, int srcW, int xInc)
  1349. {
  1350. int i;
  1351. unsigned int xpos=0;
  1352. for (i=0;i<dstWidth;i++) {
  1353. register unsigned int xx=xpos>>16;
  1354. register unsigned int xalpha=(xpos&0xFFFF)>>9;
  1355. dst[i]= (src[xx]<<7) + (src[xx+1] - src[xx])*xalpha;
  1356. xpos+=xInc;
  1357. }
  1358. }
  1359. // *** horizontal scale Y line to temp buffer
  1360. static inline void hyscale(SwsContext *c, uint16_t *dst, int dstWidth,
  1361. const uint8_t *src, int srcW, int xInc,
  1362. const int16_t *hLumFilter,
  1363. const int16_t *hLumFilterPos, int hLumFilterSize,
  1364. uint8_t *formatConvBuffer,
  1365. uint32_t *pal, int isAlpha)
  1366. {
  1367. void (*toYV12)(uint8_t *, const uint8_t *, int, uint32_t *) = isAlpha ? c->alpToYV12 : c->lumToYV12;
  1368. void (*convertRange)(uint16_t *, int) = isAlpha ? NULL : c->lumConvertRange;
  1369. if (toYV12) {
  1370. toYV12(formatConvBuffer, src, srcW, pal);
  1371. src= formatConvBuffer;
  1372. }
  1373. if (!c->hyscale_fast) {
  1374. c->hScale(dst, dstWidth, src, srcW, xInc, hLumFilter, hLumFilterPos, hLumFilterSize);
  1375. } else { // fast bilinear upscale / crap downscale
  1376. c->hyscale_fast(c, dst, dstWidth, src, srcW, xInc);
  1377. }
  1378. if (convertRange)
  1379. convertRange(dst, dstWidth);
  1380. }
  1381. static void hcscale_fast_c(SwsContext *c, int16_t *dst1, int16_t *dst2,
  1382. int dstWidth, const uint8_t *src1,
  1383. const uint8_t *src2, int srcW, int xInc)
  1384. {
  1385. int i;
  1386. unsigned int xpos=0;
  1387. for (i=0;i<dstWidth;i++) {
  1388. register unsigned int xx=xpos>>16;
  1389. register unsigned int xalpha=(xpos&0xFFFF)>>9;
  1390. dst1[i]=(src1[xx]*(xalpha^127)+src1[xx+1]*xalpha);
  1391. dst2[i]=(src2[xx]*(xalpha^127)+src2[xx+1]*xalpha);
  1392. xpos+=xInc;
  1393. }
  1394. }
  1395. static inline void hcscale(SwsContext *c, uint16_t *dst1, uint16_t *dst2, int dstWidth,
  1396. const uint8_t *src1, const uint8_t *src2,
  1397. int srcW, int xInc, const int16_t *hChrFilter,
  1398. const int16_t *hChrFilterPos, int hChrFilterSize,
  1399. uint8_t *formatConvBuffer, uint32_t *pal)
  1400. {
  1401. if (c->chrToYV12) {
  1402. uint8_t *buf2 = formatConvBuffer + FFALIGN(srcW, 16);
  1403. c->chrToYV12(formatConvBuffer, buf2, src1, src2, srcW, pal);
  1404. src1= formatConvBuffer;
  1405. src2= buf2;
  1406. }
  1407. if (!c->hcscale_fast) {
  1408. c->hScale(dst1, dstWidth, src1, srcW, xInc, hChrFilter, hChrFilterPos, hChrFilterSize);
  1409. c->hScale(dst2, dstWidth, src2, srcW, xInc, hChrFilter, hChrFilterPos, hChrFilterSize);
  1410. } else { // fast bilinear upscale / crap downscale
  1411. c->hcscale_fast(c, dst1, dst2, dstWidth, src1, src2, srcW, xInc);
  1412. }
  1413. if (c->chrConvertRange)
  1414. c->chrConvertRange(dst1, dst2, dstWidth);
  1415. }
  1416. static av_always_inline void
  1417. find_c_packed_planar_out_funcs(SwsContext *c,
  1418. yuv2planar1_fn *yuv2yuv1, yuv2planarX_fn *yuv2yuvX,
  1419. yuv2packed1_fn *yuv2packed1, yuv2packed2_fn *yuv2packed2,
  1420. yuv2packedX_fn *yuv2packedX)
  1421. {
  1422. enum PixelFormat dstFormat = c->dstFormat;
  1423. if (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21) {
  1424. *yuv2yuvX = yuv2nv12X_c;
  1425. } else if (is16BPS(dstFormat)) {
  1426. *yuv2yuvX = isBE(dstFormat) ? yuv2yuvX16BE_c : yuv2yuvX16LE_c;
  1427. } else if (is9_OR_10BPS(dstFormat)) {
  1428. if (dstFormat == PIX_FMT_YUV420P9BE || dstFormat == PIX_FMT_YUV420P9LE) {
  1429. *yuv2yuvX = isBE(dstFormat) ? yuv2yuvX9BE_c : yuv2yuvX9LE_c;
  1430. } else {
  1431. *yuv2yuvX = isBE(dstFormat) ? yuv2yuvX10BE_c : yuv2yuvX10LE_c;
  1432. }
  1433. } else {
  1434. *yuv2yuv1 = yuv2yuv1_c;
  1435. *yuv2yuvX = yuv2yuvX_c;
  1436. }
  1437. if(c->flags & SWS_FULL_CHR_H_INT) {
  1438. *yuv2packedX = yuv2rgbX_c_full;
  1439. } else {
  1440. *yuv2packed1 = yuv2packed1_c;
  1441. *yuv2packed2 = yuv2packed2_c;
  1442. *yuv2packedX = yuv2packedX_c;
  1443. }
  1444. }
  1445. #define DEBUG_SWSCALE_BUFFERS 0
  1446. #define DEBUG_BUFFERS(...) if (DEBUG_SWSCALE_BUFFERS) av_log(c, AV_LOG_DEBUG, __VA_ARGS__)
  1447. static int swScale(SwsContext *c, const uint8_t* src[],
  1448. int srcStride[], int srcSliceY,
  1449. int srcSliceH, uint8_t* dst[], int dstStride[])
  1450. {
  1451. /* load a few things into local vars to make the code more readable? and faster */
  1452. const int srcW= c->srcW;
  1453. const int dstW= c->dstW;
  1454. const int dstH= c->dstH;
  1455. const int chrDstW= c->chrDstW;
  1456. const int chrSrcW= c->chrSrcW;
  1457. const int lumXInc= c->lumXInc;
  1458. const int chrXInc= c->chrXInc;
  1459. const enum PixelFormat dstFormat= c->dstFormat;
  1460. const int flags= c->flags;
  1461. int16_t *vLumFilterPos= c->vLumFilterPos;
  1462. int16_t *vChrFilterPos= c->vChrFilterPos;
  1463. int16_t *hLumFilterPos= c->hLumFilterPos;
  1464. int16_t *hChrFilterPos= c->hChrFilterPos;
  1465. int16_t *vLumFilter= c->vLumFilter;
  1466. int16_t *vChrFilter= c->vChrFilter;
  1467. int16_t *hLumFilter= c->hLumFilter;
  1468. int16_t *hChrFilter= c->hChrFilter;
  1469. int32_t *lumMmxFilter= c->lumMmxFilter;
  1470. int32_t *chrMmxFilter= c->chrMmxFilter;
  1471. int32_t av_unused *alpMmxFilter= c->alpMmxFilter;
  1472. const int vLumFilterSize= c->vLumFilterSize;
  1473. const int vChrFilterSize= c->vChrFilterSize;
  1474. const int hLumFilterSize= c->hLumFilterSize;
  1475. const int hChrFilterSize= c->hChrFilterSize;
  1476. int16_t **lumPixBuf= c->lumPixBuf;
  1477. int16_t **chrUPixBuf= c->chrUPixBuf;
  1478. int16_t **chrVPixBuf= c->chrVPixBuf;
  1479. int16_t **alpPixBuf= c->alpPixBuf;
  1480. const int vLumBufSize= c->vLumBufSize;
  1481. const int vChrBufSize= c->vChrBufSize;
  1482. uint8_t *formatConvBuffer= c->formatConvBuffer;
  1483. const int chrSrcSliceY= srcSliceY >> c->chrSrcVSubSample;
  1484. const int chrSrcSliceH= -((-srcSliceH) >> c->chrSrcVSubSample);
  1485. int lastDstY;
  1486. uint32_t *pal=c->pal_yuv;
  1487. yuv2planar1_fn yuv2yuv1 = c->yuv2yuv1;
  1488. yuv2planarX_fn yuv2yuvX = c->yuv2yuvX;
  1489. yuv2packed1_fn yuv2packed1 = c->yuv2packed1;
  1490. yuv2packed2_fn yuv2packed2 = c->yuv2packed2;
  1491. yuv2packedX_fn yuv2packedX = c->yuv2packedX;
  1492. /* vars which will change and which we need to store back in the context */
  1493. int dstY= c->dstY;
  1494. int lumBufIndex= c->lumBufIndex;
  1495. int chrBufIndex= c->chrBufIndex;
  1496. int lastInLumBuf= c->lastInLumBuf;
  1497. int lastInChrBuf= c->lastInChrBuf;
  1498. if (isPacked(c->srcFormat)) {
  1499. src[0]=
  1500. src[1]=
  1501. src[2]=
  1502. src[3]= src[0];
  1503. srcStride[0]=
  1504. srcStride[1]=
  1505. srcStride[2]=
  1506. srcStride[3]= srcStride[0];
  1507. }
  1508. srcStride[1]<<= c->vChrDrop;
  1509. srcStride[2]<<= c->vChrDrop;
  1510. DEBUG_BUFFERS("swScale() %p[%d] %p[%d] %p[%d] %p[%d] -> %p[%d] %p[%d] %p[%d] %p[%d]\n",
  1511. src[0], srcStride[0], src[1], srcStride[1], src[2], srcStride[2], src[3], srcStride[3],
  1512. dst[0], dstStride[0], dst[1], dstStride[1], dst[2], dstStride[2], dst[3], dstStride[3]);
  1513. DEBUG_BUFFERS("srcSliceY: %d srcSliceH: %d dstY: %d dstH: %d\n",
  1514. srcSliceY, srcSliceH, dstY, dstH);
  1515. DEBUG_BUFFERS("vLumFilterSize: %d vLumBufSize: %d vChrFilterSize: %d vChrBufSize: %d\n",
  1516. vLumFilterSize, vLumBufSize, vChrFilterSize, vChrBufSize);
  1517. if (dstStride[0]%8 !=0 || dstStride[1]%8 !=0 || dstStride[2]%8 !=0 || dstStride[3]%8 != 0) {
  1518. static int warnedAlready=0; //FIXME move this into the context perhaps
  1519. if (flags & SWS_PRINT_INFO && !warnedAlready) {
  1520. av_log(c, AV_LOG_WARNING, "Warning: dstStride is not aligned!\n"
  1521. " ->cannot do aligned memory accesses anymore\n");
  1522. warnedAlready=1;
  1523. }
  1524. }
  1525. /* Note the user might start scaling the picture in the middle so this
  1526. will not get executed. This is not really intended but works
  1527. currently, so people might do it. */
  1528. if (srcSliceY ==0) {
  1529. lumBufIndex=-1;
  1530. chrBufIndex=-1;
  1531. dstY=0;
  1532. lastInLumBuf= -1;
  1533. lastInChrBuf= -1;
  1534. }
  1535. lastDstY= dstY;
  1536. for (;dstY < dstH; dstY++) {
  1537. unsigned char *dest =dst[0]+dstStride[0]*dstY;
  1538. const int chrDstY= dstY>>c->chrDstVSubSample;
  1539. unsigned char *uDest=dst[1]+dstStride[1]*chrDstY;
  1540. unsigned char *vDest=dst[2]+dstStride[2]*chrDstY;
  1541. unsigned char *aDest=(CONFIG_SWSCALE_ALPHA && alpPixBuf) ? dst[3]+dstStride[3]*dstY : NULL;
  1542. const int firstLumSrcY= vLumFilterPos[dstY]; //First line needed as input
  1543. const int firstLumSrcY2= vLumFilterPos[FFMIN(dstY | ((1<<c->chrDstVSubSample) - 1), dstH-1)];
  1544. const int firstChrSrcY= vChrFilterPos[chrDstY]; //First line needed as input
  1545. int lastLumSrcY= firstLumSrcY + vLumFilterSize -1; // Last line needed as input
  1546. int lastLumSrcY2=firstLumSrcY2+ vLumFilterSize -1; // Last line needed as input
  1547. int lastChrSrcY= firstChrSrcY + vChrFilterSize -1; // Last line needed as input
  1548. int enough_lines;
  1549. //handle holes (FAST_BILINEAR & weird filters)
  1550. if (firstLumSrcY > lastInLumBuf) lastInLumBuf= firstLumSrcY-1;
  1551. if (firstChrSrcY > lastInChrBuf) lastInChrBuf= firstChrSrcY-1;
  1552. assert(firstLumSrcY >= lastInLumBuf - vLumBufSize + 1);
  1553. assert(firstChrSrcY >= lastInChrBuf - vChrBufSize + 1);
  1554. DEBUG_BUFFERS("dstY: %d\n", dstY);
  1555. DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n",
  1556. firstLumSrcY, lastLumSrcY, lastInLumBuf);
  1557. DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n",
  1558. firstChrSrcY, lastChrSrcY, lastInChrBuf);
  1559. // Do we have enough lines in this slice to output the dstY line
  1560. enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH && lastChrSrcY < -((-srcSliceY - srcSliceH)>>c->chrSrcVSubSample);
  1561. if (!enough_lines) {
  1562. lastLumSrcY = srcSliceY + srcSliceH - 1;
  1563. lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1;
  1564. DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n",
  1565. lastLumSrcY, lastChrSrcY);
  1566. }
  1567. //Do horizontal scaling
  1568. while(lastInLumBuf < lastLumSrcY) {
  1569. const uint8_t *src1= src[0]+(lastInLumBuf + 1 - srcSliceY)*srcStride[0];
  1570. const uint8_t *src2= src[3]+(lastInLumBuf + 1 - srcSliceY)*srcStride[3];
  1571. lumBufIndex++;
  1572. assert(lumBufIndex < 2*vLumBufSize);
  1573. assert(lastInLumBuf + 1 - srcSliceY < srcSliceH);
  1574. assert(lastInLumBuf + 1 - srcSliceY >= 0);
  1575. hyscale(c, lumPixBuf[ lumBufIndex ], dstW, src1, srcW, lumXInc,
  1576. hLumFilter, hLumFilterPos, hLumFilterSize,
  1577. formatConvBuffer,
  1578. pal, 0);
  1579. if (CONFIG_SWSCALE_ALPHA && alpPixBuf)
  1580. hyscale(c, alpPixBuf[ lumBufIndex ], dstW, src2, srcW,
  1581. lumXInc, hLumFilter, hLumFilterPos, hLumFilterSize,
  1582. formatConvBuffer,
  1583. pal, 1);
  1584. lastInLumBuf++;
  1585. DEBUG_BUFFERS("\t\tlumBufIndex %d: lastInLumBuf: %d\n",
  1586. lumBufIndex, lastInLumBuf);
  1587. }
  1588. while(lastInChrBuf < lastChrSrcY) {
  1589. const uint8_t *src1= src[1]+(lastInChrBuf + 1 - chrSrcSliceY)*srcStride[1];
  1590. const uint8_t *src2= src[2]+(lastInChrBuf + 1 - chrSrcSliceY)*srcStride[2];
  1591. chrBufIndex++;
  1592. assert(chrBufIndex < 2*vChrBufSize);
  1593. assert(lastInChrBuf + 1 - chrSrcSliceY < (chrSrcSliceH));
  1594. assert(lastInChrBuf + 1 - chrSrcSliceY >= 0);
  1595. //FIXME replace parameters through context struct (some at least)
  1596. if (c->needs_hcscale)
  1597. hcscale(c, chrUPixBuf[chrBufIndex], chrVPixBuf[chrBufIndex],
  1598. chrDstW, src1, src2, chrSrcW, chrXInc,
  1599. hChrFilter, hChrFilterPos, hChrFilterSize,
  1600. formatConvBuffer, pal);
  1601. lastInChrBuf++;
  1602. DEBUG_BUFFERS("\t\tchrBufIndex %d: lastInChrBuf: %d\n",
  1603. chrBufIndex, lastInChrBuf);
  1604. }
  1605. //wrap buf index around to stay inside the ring buffer
  1606. if (lumBufIndex >= vLumBufSize) lumBufIndex-= vLumBufSize;
  1607. if (chrBufIndex >= vChrBufSize) chrBufIndex-= vChrBufSize;
  1608. if (!enough_lines)
  1609. break; //we can't output a dstY line so let's try with the next slice
  1610. #if HAVE_MMX
  1611. updateMMXDitherTables(c, dstY, lumBufIndex, chrBufIndex, lastInLumBuf, lastInChrBuf);
  1612. #endif
  1613. if (dstY >= dstH-2) {
  1614. // hmm looks like we can't use MMX here without overwriting this array's tail
  1615. find_c_packed_planar_out_funcs(c, &yuv2yuv1, &yuv2yuvX,
  1616. &yuv2packed1, &yuv2packed2,
  1617. &yuv2packedX);
  1618. }
  1619. {
  1620. const int16_t **lumSrcPtr= (const int16_t **) lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
  1621. const int16_t **chrUSrcPtr= (const int16_t **) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  1622. const int16_t **chrVSrcPtr= (const int16_t **) chrVPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  1623. const int16_t **alpSrcPtr= (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? (const int16_t **) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
  1624. if (isPlanarYUV(dstFormat) || dstFormat==PIX_FMT_GRAY8) { //YV12 like
  1625. const int chrSkipMask= (1<<c->chrDstVSubSample)-1;
  1626. if ((dstY&chrSkipMask) || isGray(dstFormat)) uDest=vDest= NULL; //FIXME split functions in lumi / chromi
  1627. if (c->yuv2yuv1 && vLumFilterSize == 1 && vChrFilterSize == 1) { // unscaled YV12
  1628. const int16_t *lumBuf = lumSrcPtr[0];
  1629. const int16_t *chrUBuf= chrUSrcPtr[0];
  1630. const int16_t *chrVBuf= chrVSrcPtr[0];
  1631. const int16_t *alpBuf= (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? alpSrcPtr[0] : NULL;
  1632. yuv2yuv1(c, lumBuf, chrUBuf, chrVBuf, alpBuf, dest,
  1633. uDest, vDest, aDest, dstW, chrDstW);
  1634. } else { //General YV12
  1635. yuv2yuvX(c,
  1636. vLumFilter+dstY*vLumFilterSize , lumSrcPtr, vLumFilterSize,
  1637. vChrFilter+chrDstY*vChrFilterSize, chrUSrcPtr,
  1638. chrVSrcPtr, vChrFilterSize,
  1639. alpSrcPtr, dest, uDest, vDest, aDest, dstW, chrDstW);
  1640. }
  1641. } else {
  1642. assert(lumSrcPtr + vLumFilterSize - 1 < lumPixBuf + vLumBufSize*2);
  1643. assert(chrUSrcPtr + vChrFilterSize - 1 < chrUPixBuf + vChrBufSize*2);
  1644. if (c->yuv2packed1 && vLumFilterSize == 1 && vChrFilterSize == 2) { //unscaled RGB
  1645. int chrAlpha= vChrFilter[2*dstY+1];
  1646. yuv2packed1(c, *lumSrcPtr, *chrUSrcPtr, *(chrUSrcPtr+1),
  1647. *chrVSrcPtr, *(chrVSrcPtr+1),
  1648. alpPixBuf ? *alpSrcPtr : NULL,
  1649. dest, dstW, chrAlpha, dstFormat, flags, dstY);
  1650. } else if (c->yuv2packed2 && vLumFilterSize == 2 && vChrFilterSize == 2) { //bilinear upscale RGB
  1651. int lumAlpha= vLumFilter[2*dstY+1];
  1652. int chrAlpha= vChrFilter[2*dstY+1];
  1653. lumMmxFilter[2]=
  1654. lumMmxFilter[3]= vLumFilter[2*dstY ]*0x10001;
  1655. chrMmxFilter[2]=
  1656. chrMmxFilter[3]= vChrFilter[2*chrDstY]*0x10001;
  1657. yuv2packed2(c, *lumSrcPtr, *(lumSrcPtr+1), *chrUSrcPtr, *(chrUSrcPtr+1),
  1658. *chrVSrcPtr, *(chrVSrcPtr+1),
  1659. alpPixBuf ? *alpSrcPtr : NULL, alpPixBuf ? *(alpSrcPtr+1) : NULL,
  1660. dest, dstW, lumAlpha, chrAlpha, dstY);
  1661. } else { //general RGB
  1662. yuv2packedX(c,
  1663. vLumFilter+dstY*vLumFilterSize, lumSrcPtr, vLumFilterSize,
  1664. vChrFilter+dstY*vChrFilterSize, chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  1665. alpSrcPtr, dest, dstW, dstY);
  1666. }
  1667. }
  1668. }
  1669. }
  1670. if ((dstFormat == PIX_FMT_YUVA420P) && !alpPixBuf)
  1671. fillPlane(dst[3], dstStride[3], dstW, dstY-lastDstY, lastDstY, 255);
  1672. #if HAVE_MMX2
  1673. if (av_get_cpu_flags() & AV_CPU_FLAG_MMX2)
  1674. __asm__ volatile("sfence":::"memory");
  1675. #endif
  1676. emms_c();
  1677. /* store changed local vars back in the context */
  1678. c->dstY= dstY;
  1679. c->lumBufIndex= lumBufIndex;
  1680. c->chrBufIndex= chrBufIndex;
  1681. c->lastInLumBuf= lastInLumBuf;
  1682. c->lastInChrBuf= lastInChrBuf;
  1683. return dstY - lastDstY;
  1684. }
  1685. static void sws_init_swScale_c(SwsContext *c)
  1686. {
  1687. enum PixelFormat srcFormat = c->srcFormat;
  1688. find_c_packed_planar_out_funcs(c, &c->yuv2yuv1, &c->yuv2yuvX,
  1689. &c->yuv2packed1, &c->yuv2packed2,
  1690. &c->yuv2packedX);
  1691. c->hScale = hScale_c;
  1692. if (c->flags & SWS_FAST_BILINEAR) {
  1693. c->hyscale_fast = hyscale_fast_c;
  1694. c->hcscale_fast = hcscale_fast_c;
  1695. }
  1696. c->chrToYV12 = NULL;
  1697. switch(srcFormat) {
  1698. case PIX_FMT_YUYV422 : c->chrToYV12 = yuy2ToUV_c; break;
  1699. case PIX_FMT_UYVY422 : c->chrToYV12 = uyvyToUV_c; break;
  1700. case PIX_FMT_NV12 : c->chrToYV12 = nv12ToUV_c; break;
  1701. case PIX_FMT_NV21 : c->chrToYV12 = nv21ToUV_c; break;
  1702. case PIX_FMT_RGB8 :
  1703. case PIX_FMT_BGR8 :
  1704. case PIX_FMT_PAL8 :
  1705. case PIX_FMT_BGR4_BYTE:
  1706. case PIX_FMT_RGB4_BYTE: c->chrToYV12 = palToUV_c; break;
  1707. case PIX_FMT_YUV420P9BE: c->chrToYV12 = BE9ToUV_c; break;
  1708. case PIX_FMT_YUV420P9LE: c->chrToYV12 = LE9ToUV_c; break;
  1709. case PIX_FMT_YUV420P10BE: c->chrToYV12 = BE10ToUV_c; break;
  1710. case PIX_FMT_YUV420P10LE: c->chrToYV12 = LE10ToUV_c; break;
  1711. case PIX_FMT_YUV420P16BE:
  1712. case PIX_FMT_YUV422P16BE:
  1713. case PIX_FMT_YUV444P16BE: c->chrToYV12 = BEToUV_c; break;
  1714. case PIX_FMT_YUV420P16LE:
  1715. case PIX_FMT_YUV422P16LE:
  1716. case PIX_FMT_YUV444P16LE: c->chrToYV12 = LEToUV_c; break;
  1717. }
  1718. if (c->chrSrcHSubSample) {
  1719. switch(srcFormat) {
  1720. case PIX_FMT_RGB48BE: c->chrToYV12 = rgb48BEToUV_half_c; break;
  1721. case PIX_FMT_RGB48LE: c->chrToYV12 = rgb48LEToUV_half_c; break;
  1722. case PIX_FMT_BGR48BE: c->chrToYV12 = bgr48BEToUV_half_c; break;
  1723. case PIX_FMT_BGR48LE: c->chrToYV12 = bgr48LEToUV_half_c; break;
  1724. case PIX_FMT_RGB32 : c->chrToYV12 = bgr32ToUV_half_c; break;
  1725. case PIX_FMT_RGB32_1: c->chrToYV12 = bgr321ToUV_half_c; break;
  1726. case PIX_FMT_BGR24 : c->chrToYV12 = bgr24ToUV_half_c; break;
  1727. case PIX_FMT_BGR565 : c->chrToYV12 = bgr16ToUV_half_c; break;
  1728. case PIX_FMT_BGR555 : c->chrToYV12 = bgr15ToUV_half_c; break;
  1729. case PIX_FMT_BGR32 : c->chrToYV12 = rgb32ToUV_half_c; break;
  1730. case PIX_FMT_BGR32_1: c->chrToYV12 = rgb321ToUV_half_c; break;
  1731. case PIX_FMT_RGB24 : c->chrToYV12 = rgb24ToUV_half_c; break;
  1732. case PIX_FMT_RGB565 : c->chrToYV12 = rgb16ToUV_half_c; break;
  1733. case PIX_FMT_RGB555 : c->chrToYV12 = rgb15ToUV_half_c; break;
  1734. }
  1735. } else {
  1736. switch(srcFormat) {
  1737. case PIX_FMT_RGB48BE: c->chrToYV12 = rgb48BEToUV_c; break;
  1738. case PIX_FMT_RGB48LE: c->chrToYV12 = rgb48LEToUV_c; break;
  1739. case PIX_FMT_BGR48BE: c->chrToYV12 = bgr48BEToUV_c; break;
  1740. case PIX_FMT_BGR48LE: c->chrToYV12 = bgr48LEToUV_c; break;
  1741. case PIX_FMT_RGB32 : c->chrToYV12 = bgr32ToUV_c; break;
  1742. case PIX_FMT_RGB32_1: c->chrToYV12 = bgr321ToUV_c; break;
  1743. case PIX_FMT_BGR24 : c->chrToYV12 = bgr24ToUV_c; break;
  1744. case PIX_FMT_BGR565 : c->chrToYV12 = bgr16ToUV_c; break;
  1745. case PIX_FMT_BGR555 : c->chrToYV12 = bgr15ToUV_c; break;
  1746. case PIX_FMT_BGR32 : c->chrToYV12 = rgb32ToUV_c; break;
  1747. case PIX_FMT_BGR32_1: c->chrToYV12 = rgb321ToUV_c; break;
  1748. case PIX_FMT_RGB24 : c->chrToYV12 = rgb24ToUV_c; break;
  1749. case PIX_FMT_RGB565 : c->chrToYV12 = rgb16ToUV_c; break;
  1750. case PIX_FMT_RGB555 : c->chrToYV12 = rgb15ToUV_c; break;
  1751. }
  1752. }
  1753. c->lumToYV12 = NULL;
  1754. c->alpToYV12 = NULL;
  1755. switch (srcFormat) {
  1756. case PIX_FMT_YUV420P9BE: c->lumToYV12 = BE9ToY_c; break;
  1757. case PIX_FMT_YUV420P9LE: c->lumToYV12 = LE9ToY_c; break;
  1758. case PIX_FMT_YUV420P10BE: c->lumToYV12 = BE10ToY_c; break;
  1759. case PIX_FMT_YUV420P10LE: c->lumToYV12 = LE10ToY_c; break;
  1760. case PIX_FMT_YUYV422 :
  1761. case PIX_FMT_YUV420P16BE:
  1762. case PIX_FMT_YUV422P16BE:
  1763. case PIX_FMT_YUV444P16BE:
  1764. case PIX_FMT_Y400A :
  1765. case PIX_FMT_GRAY16BE : c->lumToYV12 = yuy2ToY_c; break;
  1766. case PIX_FMT_UYVY422 :
  1767. case PIX_FMT_YUV420P16LE:
  1768. case PIX_FMT_YUV422P16LE:
  1769. case PIX_FMT_YUV444P16LE:
  1770. case PIX_FMT_GRAY16LE : c->lumToYV12 = uyvyToY_c; break;
  1771. case PIX_FMT_BGR24 : c->lumToYV12 = bgr24ToY_c; break;
  1772. case PIX_FMT_BGR565 : c->lumToYV12 = bgr16ToY_c; break;
  1773. case PIX_FMT_BGR555 : c->lumToYV12 = bgr15ToY_c; break;
  1774. case PIX_FMT_RGB24 : c->lumToYV12 = rgb24ToY_c; break;
  1775. case PIX_FMT_RGB565 : c->lumToYV12 = rgb16ToY_c; break;
  1776. case PIX_FMT_RGB555 : c->lumToYV12 = rgb15ToY_c; break;
  1777. case PIX_FMT_RGB8 :
  1778. case PIX_FMT_BGR8 :
  1779. case PIX_FMT_PAL8 :
  1780. case PIX_FMT_BGR4_BYTE:
  1781. case PIX_FMT_RGB4_BYTE: c->lumToYV12 = palToY_c; break;
  1782. case PIX_FMT_MONOBLACK: c->lumToYV12 = monoblack2Y_c; break;
  1783. case PIX_FMT_MONOWHITE: c->lumToYV12 = monowhite2Y_c; break;
  1784. case PIX_FMT_RGB32 : c->lumToYV12 = bgr32ToY_c; break;
  1785. case PIX_FMT_RGB32_1: c->lumToYV12 = bgr321ToY_c; break;
  1786. case PIX_FMT_BGR32 : c->lumToYV12 = rgb32ToY_c; break;
  1787. case PIX_FMT_BGR32_1: c->lumToYV12 = rgb321ToY_c; break;
  1788. case PIX_FMT_RGB48BE: c->lumToYV12 = rgb48BEToY_c; break;
  1789. case PIX_FMT_RGB48LE: c->lumToYV12 = rgb48LEToY_c; break;
  1790. case PIX_FMT_BGR48BE: c->lumToYV12 = bgr48BEToY_c; break;
  1791. case PIX_FMT_BGR48LE: c->lumToYV12 = bgr48LEToY_c; break;
  1792. }
  1793. if (c->alpPixBuf) {
  1794. switch (srcFormat) {
  1795. case PIX_FMT_BGRA:
  1796. case PIX_FMT_RGBA: c->alpToYV12 = rgbaToA_c; break;
  1797. case PIX_FMT_ABGR:
  1798. case PIX_FMT_ARGB: c->alpToYV12 = abgrToA_c; break;
  1799. case PIX_FMT_Y400A: c->alpToYV12 = uyvyToY_c; break;
  1800. }
  1801. }
  1802. if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
  1803. if (c->srcRange) {
  1804. c->lumConvertRange = lumRangeFromJpeg_c;
  1805. c->chrConvertRange = chrRangeFromJpeg_c;
  1806. } else {
  1807. c->lumConvertRange = lumRangeToJpeg_c;
  1808. c->chrConvertRange = chrRangeToJpeg_c;
  1809. }
  1810. }
  1811. if (!(isGray(srcFormat) || isGray(c->dstFormat) ||
  1812. srcFormat == PIX_FMT_MONOBLACK || srcFormat == PIX_FMT_MONOWHITE))
  1813. c->needs_hcscale = 1;
  1814. }
  1815. SwsFunc ff_getSwsFunc(SwsContext *c)
  1816. {
  1817. sws_init_swScale_c(c);
  1818. if (HAVE_MMX)
  1819. ff_sws_init_swScale_mmx(c);
  1820. if (HAVE_ALTIVEC)
  1821. ff_sws_init_swScale_altivec(c);
  1822. return swScale;
  1823. }