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.

2457 lines
90KB

  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 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 void yuv2yuv1_c(SwsContext *c, const int16_t *lumSrc,
  299. const int16_t *chrUSrc, const int16_t *chrVSrc,
  300. const int16_t *alpSrc,
  301. uint8_t *dest, uint8_t *uDest, uint8_t *vDest,
  302. uint8_t *aDest, int dstW, int chrDstW)
  303. {
  304. int i;
  305. for (i=0; i<dstW; i++) {
  306. int val= (lumSrc[i]+64)>>7;
  307. dest[i]= av_clip_uint8(val);
  308. }
  309. if (uDest)
  310. for (i=0; i<chrDstW; i++) {
  311. int u=(chrUSrc[i]+64)>>7;
  312. int v=(chrVSrc[i]+64)>>7;
  313. uDest[i]= av_clip_uint8(u);
  314. vDest[i]= av_clip_uint8(v);
  315. }
  316. if (CONFIG_SWSCALE_ALPHA && aDest)
  317. for (i=0; i<dstW; i++) {
  318. int val= (alpSrc[i]+64)>>7;
  319. aDest[i]= av_clip_uint8(val);
  320. }
  321. }
  322. static void yuv2nv12X_c(SwsContext *c, const int16_t *lumFilter,
  323. const int16_t **lumSrc, int lumFilterSize,
  324. const int16_t *chrFilter, const int16_t **chrUSrc,
  325. const int16_t **chrVSrc, int chrFilterSize,
  326. const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest,
  327. uint8_t *vDest, uint8_t *aDest,
  328. int dstW, int chrDstW)
  329. {
  330. enum PixelFormat dstFormat = c->dstFormat;
  331. //FIXME Optimize (just quickly written not optimized..)
  332. int i;
  333. for (i=0; i<dstW; i++) {
  334. int val=1<<18;
  335. int j;
  336. for (j=0; j<lumFilterSize; j++)
  337. val += lumSrc[j][i] * lumFilter[j];
  338. dest[i]= av_clip_uint8(val>>19);
  339. }
  340. if (!uDest)
  341. return;
  342. if (dstFormat == PIX_FMT_NV12)
  343. for (i=0; i<chrDstW; i++) {
  344. int u=1<<18;
  345. int v=1<<18;
  346. int j;
  347. for (j=0; j<chrFilterSize; j++) {
  348. u += chrUSrc[j][i] * chrFilter[j];
  349. v += chrVSrc[j][i] * chrFilter[j];
  350. }
  351. uDest[2*i]= av_clip_uint8(u>>19);
  352. uDest[2*i+1]= av_clip_uint8(v>>19);
  353. }
  354. else
  355. for (i=0; i<chrDstW; i++) {
  356. int u=1<<18;
  357. int v=1<<18;
  358. int j;
  359. for (j=0; j<chrFilterSize; j++) {
  360. u += chrUSrc[j][i] * chrFilter[j];
  361. v += chrVSrc[j][i] * chrFilter[j];
  362. }
  363. uDest[2*i]= av_clip_uint8(v>>19);
  364. uDest[2*i+1]= av_clip_uint8(u>>19);
  365. }
  366. }
  367. #define output_pixel(pos, val) \
  368. if (target == PIX_FMT_GRAY16BE) { \
  369. AV_WB16(pos, val); \
  370. } else { \
  371. AV_WL16(pos, val); \
  372. }
  373. static av_always_inline void
  374. yuv2gray16_X_c_template(SwsContext *c, const int16_t *lumFilter,
  375. const int16_t **lumSrc, int lumFilterSize,
  376. const int16_t *chrFilter, const int16_t **chrUSrc,
  377. const int16_t **chrVSrc, int chrFilterSize,
  378. const int16_t **alpSrc, uint8_t *dest, int dstW,
  379. int y, enum PixelFormat target)
  380. {
  381. int i;
  382. for (i = 0; i < (dstW >> 1); i++) {
  383. int j;
  384. int Y1 = 1 << 18;
  385. int Y2 = 1 << 18;
  386. const int i2 = 2 * i;
  387. for (j = 0; j < lumFilterSize; j++) {
  388. Y1 += lumSrc[j][i2] * lumFilter[j];
  389. Y2 += lumSrc[j][i2+1] * lumFilter[j];
  390. }
  391. Y1 >>= 11;
  392. Y2 >>= 11;
  393. if ((Y1 | Y2) & 0x10000) {
  394. Y1 = av_clip_uint16(Y1);
  395. Y2 = av_clip_uint16(Y2);
  396. }
  397. output_pixel(&dest[2 * i2 + 0], Y1);
  398. output_pixel(&dest[2 * i2 + 2], Y2);
  399. }
  400. }
  401. static av_always_inline void
  402. yuv2gray16_2_c_template(SwsContext *c, const uint16_t *buf0,
  403. const uint16_t *buf1, const uint16_t *ubuf0,
  404. const uint16_t *ubuf1, const uint16_t *vbuf0,
  405. const uint16_t *vbuf1, const uint16_t *abuf0,
  406. const uint16_t *abuf1, uint8_t *dest, int dstW,
  407. int yalpha, int uvalpha, int y,
  408. enum PixelFormat target)
  409. {
  410. int yalpha1 = 4095 - yalpha; \
  411. int i;
  412. for (i = 0; i < (dstW >> 1); i++) {
  413. const int i2 = 2 * i;
  414. int Y1 = (buf0[i2 ] * yalpha1 + buf1[i2 ] * yalpha) >> 11;
  415. int Y2 = (buf0[i2+1] * yalpha1 + buf1[i2+1] * yalpha) >> 11;
  416. output_pixel(&dest[2 * i2 + 0], Y1);
  417. output_pixel(&dest[2 * i2 + 2], Y2);
  418. }
  419. }
  420. static av_always_inline void
  421. yuv2gray16_1_c_template(SwsContext *c, const uint16_t *buf0,
  422. const uint16_t *ubuf0, const uint16_t *ubuf1,
  423. const uint16_t *vbuf0, const uint16_t *vbuf1,
  424. const uint16_t *abuf0, uint8_t *dest, int dstW,
  425. int uvalpha, enum PixelFormat dstFormat,
  426. int flags, int y, enum PixelFormat target)
  427. {
  428. int i;
  429. for (i = 0; i < (dstW >> 1); i++) {
  430. const int i2 = 2 * i;
  431. int Y1 = buf0[i2 ] << 1;
  432. int Y2 = buf0[i2+1] << 1;
  433. output_pixel(&dest[2 * i2 + 0], Y1);
  434. output_pixel(&dest[2 * i2 + 2], Y2);
  435. }
  436. }
  437. #undef output_pixel
  438. #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
  439. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  440. const int16_t **lumSrc, int lumFilterSize, \
  441. const int16_t *chrFilter, const int16_t **chrUSrc, \
  442. const int16_t **chrVSrc, int chrFilterSize, \
  443. const int16_t **alpSrc, uint8_t *dest, int dstW, \
  444. int y) \
  445. { \
  446. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  447. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  448. alpSrc, dest, dstW, y, fmt); \
  449. } \
  450. \
  451. static void name ## ext ## _2_c(SwsContext *c, const uint16_t *buf0, \
  452. const uint16_t *buf1, const uint16_t *ubuf0, \
  453. const uint16_t *ubuf1, const uint16_t *vbuf0, \
  454. const uint16_t *vbuf1, const uint16_t *abuf0, \
  455. const uint16_t *abuf1, uint8_t *dest, int dstW, \
  456. int yalpha, int uvalpha, int y) \
  457. { \
  458. name ## base ## _2_c_template(c, buf0, buf1, ubuf0, ubuf1, \
  459. vbuf0, vbuf1, abuf0, abuf1, \
  460. dest, dstW, yalpha, uvalpha, y, fmt); \
  461. } \
  462. \
  463. static void name ## ext ## _1_c(SwsContext *c, const uint16_t *buf0, \
  464. const uint16_t *ubuf0, const uint16_t *ubuf1, \
  465. const uint16_t *vbuf0, const uint16_t *vbuf1, \
  466. const uint16_t *abuf0, uint8_t *dest, int dstW, \
  467. int uvalpha, enum PixelFormat dstFormat, \
  468. int flags, int y) \
  469. { \
  470. name ## base ## _1_c_template(c, buf0, ubuf0, ubuf1, vbuf0, \
  471. vbuf1, abuf0, dest, dstW, uvalpha, \
  472. dstFormat, flags, y, fmt); \
  473. }
  474. YUV2PACKEDWRAPPER(yuv2gray16,, LE, PIX_FMT_GRAY16LE);
  475. YUV2PACKEDWRAPPER(yuv2gray16,, BE, PIX_FMT_GRAY16BE);
  476. #define output_pixel(pos, acc) \
  477. if (target == PIX_FMT_MONOBLACK) { \
  478. pos = acc; \
  479. } else { \
  480. pos = ~acc; \
  481. }
  482. static av_always_inline void
  483. yuv2mono_X_c_template(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, enum PixelFormat target)
  489. {
  490. const uint8_t * const d128=dither_8x8_220[y&7];
  491. uint8_t *g = c->table_gU[128] + c->table_gV[128];
  492. int i;
  493. int acc = 0;
  494. for (i = 0; i < dstW - 1; i += 2) {
  495. int j;
  496. int Y1 = 1 << 18;
  497. int Y2 = 1 << 18;
  498. for (j = 0; j < lumFilterSize; j++) {
  499. Y1 += lumSrc[j][i] * lumFilter[j];
  500. Y2 += lumSrc[j][i+1] * lumFilter[j];
  501. }
  502. Y1 >>= 19;
  503. Y2 >>= 19;
  504. if ((Y1 | Y2) & 0x100) {
  505. Y1 = av_clip_uint8(Y1);
  506. Y2 = av_clip_uint8(Y2);
  507. }
  508. acc += acc + g[Y1 + d128[(i + 0) & 7]];
  509. acc += acc + g[Y2 + d128[(i + 1) & 7]];
  510. if ((i & 7) == 6) {
  511. output_pixel(*dest++, acc);
  512. }
  513. }
  514. }
  515. static av_always_inline void
  516. yuv2mono_2_c_template(SwsContext *c, const uint16_t *buf0,
  517. const uint16_t *buf1, const uint16_t *ubuf0,
  518. const uint16_t *ubuf1, const uint16_t *vbuf0,
  519. const uint16_t *vbuf1, const uint16_t *abuf0,
  520. const uint16_t *abuf1, uint8_t *dest, int dstW,
  521. int yalpha, int uvalpha, int y,
  522. enum PixelFormat target)
  523. {
  524. const uint8_t * const d128 = dither_8x8_220[y & 7];
  525. uint8_t *g = c->table_gU[128] + c->table_gV[128];
  526. int yalpha1 = 4095 - yalpha;
  527. int i;
  528. for (i = 0; i < dstW - 7; i += 8) {
  529. int acc = g[((buf0[i ] * yalpha1 + buf1[i ] * yalpha) >> 19) + d128[0]];
  530. acc += acc + g[((buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19) + d128[1]];
  531. acc += acc + g[((buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19) + d128[2]];
  532. acc += acc + g[((buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19) + d128[3]];
  533. acc += acc + g[((buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19) + d128[4]];
  534. acc += acc + g[((buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19) + d128[5]];
  535. acc += acc + g[((buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19) + d128[6]];
  536. acc += acc + g[((buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19) + d128[7]];
  537. output_pixel(*dest++, acc);
  538. }
  539. }
  540. static av_always_inline void
  541. yuv2mono_1_c_template(SwsContext *c, const uint16_t *buf0,
  542. const uint16_t *ubuf0, const uint16_t *ubuf1,
  543. const uint16_t *vbuf0, const uint16_t *vbuf1,
  544. const uint16_t *abuf0, uint8_t *dest, int dstW,
  545. int uvalpha, enum PixelFormat dstFormat,
  546. int flags, int y, enum PixelFormat target)
  547. {
  548. const uint8_t * const d128 = dither_8x8_220[y & 7];
  549. uint8_t *g = c->table_gU[128] + c->table_gV[128];
  550. int i;
  551. for (i = 0; i < dstW - 7; i += 8) {
  552. int acc = g[(buf0[i ] >> 7) + d128[0]];
  553. acc += acc + g[(buf0[i + 1] >> 7) + d128[1]];
  554. acc += acc + g[(buf0[i + 2] >> 7) + d128[2]];
  555. acc += acc + g[(buf0[i + 3] >> 7) + d128[3]];
  556. acc += acc + g[(buf0[i + 4] >> 7) + d128[4]];
  557. acc += acc + g[(buf0[i + 5] >> 7) + d128[5]];
  558. acc += acc + g[(buf0[i + 6] >> 7) + d128[6]];
  559. acc += acc + g[(buf0[i + 7] >> 7) + d128[7]];
  560. output_pixel(*dest++, acc);
  561. }
  562. }
  563. #undef output_pixel
  564. YUV2PACKEDWRAPPER(yuv2mono,, white, PIX_FMT_MONOWHITE);
  565. YUV2PACKEDWRAPPER(yuv2mono,, black, PIX_FMT_MONOBLACK);
  566. #define output_pixels(pos, Y1, U, Y2, V) \
  567. if (target == PIX_FMT_YUYV422) { \
  568. dest[pos + 0] = Y1; \
  569. dest[pos + 1] = U; \
  570. dest[pos + 2] = Y2; \
  571. dest[pos + 3] = V; \
  572. } else { \
  573. dest[pos + 0] = U; \
  574. dest[pos + 1] = Y1; \
  575. dest[pos + 2] = V; \
  576. dest[pos + 3] = Y2; \
  577. }
  578. static av_always_inline void
  579. yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
  580. const int16_t **lumSrc, int lumFilterSize,
  581. const int16_t *chrFilter, const int16_t **chrUSrc,
  582. const int16_t **chrVSrc, int chrFilterSize,
  583. const int16_t **alpSrc, uint8_t *dest, int dstW,
  584. int y, enum PixelFormat target)
  585. {
  586. int i;
  587. for (i = 0; i < (dstW >> 1); i++) {
  588. int j;
  589. int Y1 = 1 << 18;
  590. int Y2 = 1 << 18;
  591. int U = 1 << 18;
  592. int V = 1 << 18;
  593. for (j = 0; j < lumFilterSize; j++) {
  594. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  595. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  596. }
  597. for (j = 0; j < chrFilterSize; j++) {
  598. U += chrUSrc[j][i] * chrFilter[j];
  599. V += chrVSrc[j][i] * chrFilter[j];
  600. }
  601. Y1 >>= 19;
  602. Y2 >>= 19;
  603. U >>= 19;
  604. V >>= 19;
  605. if ((Y1 | Y2 | U | V) & 0x100) {
  606. Y1 = av_clip_uint8(Y1);
  607. Y2 = av_clip_uint8(Y2);
  608. U = av_clip_uint8(U);
  609. V = av_clip_uint8(V);
  610. }
  611. output_pixels(4*i, Y1, U, Y2, V);
  612. }
  613. }
  614. static av_always_inline void
  615. yuv2422_2_c_template(SwsContext *c, const uint16_t *buf0,
  616. const uint16_t *buf1, const uint16_t *ubuf0,
  617. const uint16_t *ubuf1, const uint16_t *vbuf0,
  618. const uint16_t *vbuf1, const uint16_t *abuf0,
  619. const uint16_t *abuf1, uint8_t *dest, int dstW,
  620. int yalpha, int uvalpha, int y,
  621. enum PixelFormat target)
  622. {
  623. int yalpha1 = 4095 - yalpha;
  624. int uvalpha1 = 4095 - uvalpha;
  625. int i;
  626. for (i = 0; i < (dstW >> 1); i++) {
  627. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
  628. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
  629. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
  630. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
  631. output_pixels(i * 4, Y1, U, Y2, V);
  632. }
  633. }
  634. static av_always_inline void
  635. yuv2422_1_c_template(SwsContext *c, const uint16_t *buf0,
  636. const uint16_t *ubuf0, const uint16_t *ubuf1,
  637. const uint16_t *vbuf0, const uint16_t *vbuf1,
  638. const uint16_t *abuf0, uint8_t *dest, int dstW,
  639. int uvalpha, enum PixelFormat dstFormat,
  640. int flags, int y, enum PixelFormat target)
  641. {
  642. int i;
  643. if (uvalpha < 2048) {
  644. for (i = 0; i < (dstW >> 1); i++) {
  645. int Y1 = buf0[i * 2] >> 7;
  646. int Y2 = buf0[i * 2 + 1] >> 7;
  647. int U = ubuf1[i] >> 7;
  648. int V = vbuf1[i] >> 7;
  649. output_pixels(i * 4, Y1, U, Y2, V);
  650. }
  651. } else {
  652. for (i = 0; i < (dstW >> 1); i++) {
  653. int Y1 = buf0[i * 2] >> 7;
  654. int Y2 = buf0[i * 2 + 1] >> 7;
  655. int U = (ubuf0[i] + ubuf1[i]) >> 8;
  656. int V = (vbuf0[i] + vbuf1[i]) >> 8;
  657. output_pixels(i * 4, Y1, U, Y2, V);
  658. }
  659. }
  660. }
  661. #undef output_pixels
  662. YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, PIX_FMT_YUYV422);
  663. YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, PIX_FMT_UYVY422);
  664. #define r_b ((target == PIX_FMT_RGB48LE || target == PIX_FMT_RGB48BE) ? r : b)
  665. #define b_r ((target == PIX_FMT_RGB48LE || target == PIX_FMT_RGB48BE) ? b : r)
  666. static av_always_inline void
  667. yuv2rgb48_X_c_template(SwsContext *c, const int16_t *lumFilter,
  668. const int16_t **lumSrc, int lumFilterSize,
  669. const int16_t *chrFilter, const int16_t **chrUSrc,
  670. const int16_t **chrVSrc, int chrFilterSize,
  671. const int16_t **alpSrc, uint8_t *dest, int dstW,
  672. int y, enum PixelFormat target)
  673. {
  674. int i;
  675. for (i = 0; i < (dstW >> 1); i++) {
  676. int j;
  677. int Y1 = 1 << 18;
  678. int Y2 = 1 << 18;
  679. int U = 1 << 18;
  680. int V = 1 << 18;
  681. const uint8_t *r, *g, *b;
  682. for (j = 0; j < lumFilterSize; j++) {
  683. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  684. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  685. }
  686. for (j = 0; j < chrFilterSize; j++) {
  687. U += chrUSrc[j][i] * chrFilter[j];
  688. V += chrVSrc[j][i] * chrFilter[j];
  689. }
  690. Y1 >>= 19;
  691. Y2 >>= 19;
  692. U >>= 19;
  693. V >>= 19;
  694. if ((Y1 | Y2 | U | V) & 0x100) {
  695. Y1 = av_clip_uint8(Y1);
  696. Y2 = av_clip_uint8(Y2);
  697. U = av_clip_uint8(U);
  698. V = av_clip_uint8(V);
  699. }
  700. /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/
  701. r = (const uint8_t *) c->table_rV[V];
  702. g = (const uint8_t *)(c->table_gU[U] + c->table_gV[V]);
  703. b = (const uint8_t *) c->table_bU[U];
  704. dest[ 0] = dest[ 1] = r_b[Y1];
  705. dest[ 2] = dest[ 3] = g[Y1];
  706. dest[ 4] = dest[ 5] = b_r[Y1];
  707. dest[ 6] = dest[ 7] = r_b[Y2];
  708. dest[ 8] = dest[ 9] = g[Y2];
  709. dest[10] = dest[11] = b_r[Y2];
  710. dest += 12;
  711. }
  712. }
  713. static av_always_inline void
  714. yuv2rgb48_2_c_template(SwsContext *c, const uint16_t *buf0,
  715. const uint16_t *buf1, const uint16_t *ubuf0,
  716. const uint16_t *ubuf1, const uint16_t *vbuf0,
  717. const uint16_t *vbuf1, const uint16_t *abuf0,
  718. const uint16_t *abuf1, uint8_t *dest, int dstW,
  719. int yalpha, int uvalpha, int y,
  720. enum PixelFormat target)
  721. {
  722. int yalpha1 = 4095 - yalpha;
  723. int uvalpha1 = 4095 - uvalpha;
  724. int i;
  725. for (i = 0; i < (dstW >> 1); i++) {
  726. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
  727. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
  728. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
  729. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
  730. const uint8_t *r = (const uint8_t *) c->table_rV[V],
  731. *g = (const uint8_t *)(c->table_gU[U] + c->table_gV[V]),
  732. *b = (const uint8_t *) c->table_bU[U];
  733. dest[ 0] = dest[ 1] = r_b[Y1];
  734. dest[ 2] = dest[ 3] = g[Y1];
  735. dest[ 4] = dest[ 5] = b_r[Y1];
  736. dest[ 6] = dest[ 7] = r_b[Y2];
  737. dest[ 8] = dest[ 9] = g[Y2];
  738. dest[10] = dest[11] = b_r[Y2];
  739. dest += 12;
  740. }
  741. }
  742. static av_always_inline void
  743. yuv2rgb48_1_c_template(SwsContext *c, const uint16_t *buf0,
  744. const uint16_t *ubuf0, const uint16_t *ubuf1,
  745. const uint16_t *vbuf0, const uint16_t *vbuf1,
  746. const uint16_t *abuf0, uint8_t *dest, int dstW,
  747. int uvalpha, enum PixelFormat dstFormat,
  748. int flags, int y, enum PixelFormat target)
  749. {
  750. int i;
  751. if (uvalpha < 2048) {
  752. for (i = 0; i < (dstW >> 1); i++) {
  753. int Y1 = buf0[i * 2] >> 7;
  754. int Y2 = buf0[i * 2 + 1] >> 7;
  755. int U = ubuf1[i] >> 7;
  756. int V = vbuf1[i] >> 7;
  757. const uint8_t *r = (const uint8_t *) c->table_rV[V],
  758. *g = (const uint8_t *)(c->table_gU[U] + c->table_gV[V]),
  759. *b = (const uint8_t *) c->table_bU[U];
  760. dest[ 0] = dest[ 1] = r_b[Y1];
  761. dest[ 2] = dest[ 3] = g[Y1];
  762. dest[ 4] = dest[ 5] = b_r[Y1];
  763. dest[ 6] = dest[ 7] = r_b[Y2];
  764. dest[ 8] = dest[ 9] = g[Y2];
  765. dest[10] = dest[11] = b_r[Y2];
  766. dest += 12;
  767. }
  768. } else {
  769. for (i = 0; i < (dstW >> 1); i++) {
  770. int Y1 = buf0[i * 2] >> 7;
  771. int Y2 = buf0[i * 2 + 1] >> 7;
  772. int U = (ubuf0[i] + ubuf1[i]) >> 8;
  773. int V = (vbuf0[i] + vbuf1[i]) >> 8;
  774. const uint8_t *r = (const uint8_t *) c->table_rV[V],
  775. *g = (const uint8_t *)(c->table_gU[U] + c->table_gV[V]),
  776. *b = (const uint8_t *) c->table_bU[U];
  777. dest[ 0] = dest[ 1] = r_b[Y1];
  778. dest[ 2] = dest[ 3] = g[Y1];
  779. dest[ 4] = dest[ 5] = b_r[Y1];
  780. dest[ 6] = dest[ 7] = r_b[Y2];
  781. dest[ 8] = dest[ 9] = g[Y2];
  782. dest[10] = dest[11] = b_r[Y2];
  783. dest += 12;
  784. }
  785. }
  786. }
  787. #undef r_b
  788. #undef b_r
  789. YUV2PACKEDWRAPPER(yuv2, rgb48, rgb48be, PIX_FMT_RGB48BE);
  790. //YUV2PACKEDWRAPPER(yuv2, rgb48, rgb48le, PIX_FMT_RGB48LE);
  791. YUV2PACKEDWRAPPER(yuv2, rgb48, bgr48be, PIX_FMT_BGR48BE);
  792. //YUV2PACKEDWRAPPER(yuv2, rgb48, bgr48le, PIX_FMT_BGR48LE);
  793. #define YSCALE_YUV_2_RGBX_C(type,alpha) \
  794. for (i=0; i<(dstW>>1); i++) {\
  795. int j;\
  796. int Y1 = 1<<18;\
  797. int Y2 = 1<<18;\
  798. int U = 1<<18;\
  799. int V = 1<<18;\
  800. int av_unused A1, A2;\
  801. type av_unused *r, *b, *g;\
  802. const int i2= 2*i;\
  803. \
  804. for (j=0; j<lumFilterSize; j++) {\
  805. Y1 += lumSrc[j][i2] * lumFilter[j];\
  806. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  807. }\
  808. for (j=0; j<chrFilterSize; j++) {\
  809. U += chrUSrc[j][i] * chrFilter[j];\
  810. V += chrVSrc[j][i] * chrFilter[j];\
  811. }\
  812. Y1>>=19;\
  813. Y2>>=19;\
  814. U >>=19;\
  815. V >>=19;\
  816. if ((Y1|Y2|U|V)&0x100) {\
  817. Y1 = av_clip_uint8(Y1); \
  818. Y2 = av_clip_uint8(Y2); \
  819. U = av_clip_uint8(U); \
  820. V = av_clip_uint8(V); \
  821. }\
  822. if (alpha) {\
  823. A1 = 1<<18;\
  824. A2 = 1<<18;\
  825. for (j=0; j<lumFilterSize; j++) {\
  826. A1 += alpSrc[j][i2 ] * lumFilter[j];\
  827. A2 += alpSrc[j][i2+1] * lumFilter[j];\
  828. }\
  829. A1>>=19;\
  830. A2>>=19;\
  831. if ((A1|A2)&0x100) {\
  832. A1 = av_clip_uint8(A1); \
  833. A2 = av_clip_uint8(A2); \
  834. }\
  835. }\
  836. /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
  837. r = (type *)c->table_rV[V]; \
  838. g = (type *)(c->table_gU[U] + c->table_gV[V]); \
  839. b = (type *)c->table_bU[U];
  840. #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
  841. for (i=0; i<dstW; i++) {\
  842. int j;\
  843. int Y = 0;\
  844. int U = -128<<19;\
  845. int V = -128<<19;\
  846. int av_unused A;\
  847. int R,G,B;\
  848. \
  849. for (j=0; j<lumFilterSize; j++) {\
  850. Y += lumSrc[j][i ] * lumFilter[j];\
  851. }\
  852. for (j=0; j<chrFilterSize; j++) {\
  853. U += chrUSrc[j][i] * chrFilter[j];\
  854. V += chrVSrc[j][i] * chrFilter[j];\
  855. }\
  856. Y >>=10;\
  857. U >>=10;\
  858. V >>=10;\
  859. if (alpha) {\
  860. A = rnd;\
  861. for (j=0; j<lumFilterSize; j++)\
  862. A += alpSrc[j][i ] * lumFilter[j];\
  863. A >>=19;\
  864. if (A&0x100)\
  865. A = av_clip_uint8(A);\
  866. }\
  867. Y-= c->yuv2rgb_y_offset;\
  868. Y*= c->yuv2rgb_y_coeff;\
  869. Y+= rnd;\
  870. R= Y + V*c->yuv2rgb_v2r_coeff;\
  871. G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
  872. B= Y + U*c->yuv2rgb_u2b_coeff;\
  873. if ((R|G|B)&(0xC0000000)) {\
  874. R = av_clip_uintp2(R, 30); \
  875. G = av_clip_uintp2(G, 30); \
  876. B = av_clip_uintp2(B, 30); \
  877. }
  878. #define YSCALE_YUV_2_RGB2_C(type,alpha) \
  879. for (i=0; i<(dstW>>1); i++) { \
  880. const int i2= 2*i; \
  881. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
  882. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
  883. int U= (ubuf0[i]*uvalpha1+ubuf1[i]*uvalpha)>>19; \
  884. int V= (vbuf0[i]*uvalpha1+vbuf1[i]*uvalpha)>>19; \
  885. type av_unused *r, *b, *g; \
  886. int av_unused A1, A2; \
  887. if (alpha) {\
  888. A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
  889. A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
  890. }\
  891. r = (type *)c->table_rV[V];\
  892. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  893. b = (type *)c->table_bU[U];
  894. #define YSCALE_YUV_2_RGB1_C(type,alpha) \
  895. for (i=0; i<(dstW>>1); i++) {\
  896. const int i2= 2*i;\
  897. int Y1= buf0[i2 ]>>7;\
  898. int Y2= buf0[i2+1]>>7;\
  899. int U= (ubuf1[i])>>7;\
  900. int V= (vbuf1[i])>>7;\
  901. type av_unused *r, *b, *g;\
  902. int av_unused A1, A2;\
  903. if (alpha) {\
  904. A1= abuf0[i2 ]>>7;\
  905. A2= abuf0[i2+1]>>7;\
  906. }\
  907. r = (type *)c->table_rV[V];\
  908. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  909. b = (type *)c->table_bU[U];
  910. #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
  911. for (i=0; i<(dstW>>1); i++) {\
  912. const int i2= 2*i;\
  913. int Y1= buf0[i2 ]>>7;\
  914. int Y2= buf0[i2+1]>>7;\
  915. int U= (ubuf0[i] + ubuf1[i])>>8;\
  916. int V= (vbuf0[i] + vbuf1[i])>>8;\
  917. type av_unused *r, *b, *g;\
  918. int av_unused A1, A2;\
  919. if (alpha) {\
  920. A1= abuf0[i2 ]>>7;\
  921. A2= abuf0[i2+1]>>7;\
  922. }\
  923. r = (type *)c->table_rV[V];\
  924. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  925. b = (type *)c->table_bU[U];
  926. #define YSCALE_YUV_2_ANYRGB_C(func)\
  927. switch(c->dstFormat) {\
  928. case PIX_FMT_RGBA:\
  929. case PIX_FMT_BGRA:\
  930. if (CONFIG_SMALL) {\
  931. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
  932. func(uint32_t,needAlpha)\
  933. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
  934. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
  935. }\
  936. } else {\
  937. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
  938. func(uint32_t,1)\
  939. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
  940. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
  941. }\
  942. } else {\
  943. func(uint32_t,0)\
  944. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  945. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  946. }\
  947. }\
  948. }\
  949. break;\
  950. case PIX_FMT_ARGB:\
  951. case PIX_FMT_ABGR:\
  952. if (CONFIG_SMALL) {\
  953. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
  954. func(uint32_t,needAlpha)\
  955. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
  956. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
  957. }\
  958. } else {\
  959. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
  960. func(uint32_t,1)\
  961. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
  962. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
  963. }\
  964. } else {\
  965. func(uint32_t,0)\
  966. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  967. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  968. }\
  969. }\
  970. } \
  971. break;\
  972. case PIX_FMT_RGB24:\
  973. func(uint8_t,0)\
  974. ((uint8_t*)dest)[0]= r[Y1];\
  975. ((uint8_t*)dest)[1]= g[Y1];\
  976. ((uint8_t*)dest)[2]= b[Y1];\
  977. ((uint8_t*)dest)[3]= r[Y2];\
  978. ((uint8_t*)dest)[4]= g[Y2];\
  979. ((uint8_t*)dest)[5]= b[Y2];\
  980. dest+=6;\
  981. }\
  982. break;\
  983. case PIX_FMT_BGR24:\
  984. func(uint8_t,0)\
  985. ((uint8_t*)dest)[0]= b[Y1];\
  986. ((uint8_t*)dest)[1]= g[Y1];\
  987. ((uint8_t*)dest)[2]= r[Y1];\
  988. ((uint8_t*)dest)[3]= b[Y2];\
  989. ((uint8_t*)dest)[4]= g[Y2];\
  990. ((uint8_t*)dest)[5]= r[Y2];\
  991. dest+=6;\
  992. }\
  993. break;\
  994. case PIX_FMT_RGB565:\
  995. case PIX_FMT_BGR565:\
  996. {\
  997. const int dr1= dither_2x2_8[y&1 ][0];\
  998. const int dg1= dither_2x2_4[y&1 ][0];\
  999. const int db1= dither_2x2_8[(y&1)^1][0];\
  1000. const int dr2= dither_2x2_8[y&1 ][1];\
  1001. const int dg2= dither_2x2_4[y&1 ][1];\
  1002. const int db2= dither_2x2_8[(y&1)^1][1];\
  1003. func(uint16_t,0)\
  1004. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  1005. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  1006. }\
  1007. }\
  1008. break;\
  1009. case PIX_FMT_RGB555:\
  1010. case PIX_FMT_BGR555:\
  1011. {\
  1012. const int dr1= dither_2x2_8[y&1 ][0];\
  1013. const int dg1= dither_2x2_8[y&1 ][1];\
  1014. const int db1= dither_2x2_8[(y&1)^1][0];\
  1015. const int dr2= dither_2x2_8[y&1 ][1];\
  1016. const int dg2= dither_2x2_8[y&1 ][0];\
  1017. const int db2= dither_2x2_8[(y&1)^1][1];\
  1018. func(uint16_t,0)\
  1019. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  1020. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  1021. }\
  1022. }\
  1023. break;\
  1024. case PIX_FMT_RGB444:\
  1025. case PIX_FMT_BGR444:\
  1026. {\
  1027. const int dr1= dither_4x4_16[y&3 ][0];\
  1028. const int dg1= dither_4x4_16[y&3 ][1];\
  1029. const int db1= dither_4x4_16[(y&3)^3][0];\
  1030. const int dr2= dither_4x4_16[y&3 ][1];\
  1031. const int dg2= dither_4x4_16[y&3 ][0];\
  1032. const int db2= dither_4x4_16[(y&3)^3][1];\
  1033. func(uint16_t,0)\
  1034. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  1035. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  1036. }\
  1037. }\
  1038. break;\
  1039. case PIX_FMT_RGB8:\
  1040. case PIX_FMT_BGR8:\
  1041. {\
  1042. const uint8_t * const d64= dither_8x8_73[y&7];\
  1043. const uint8_t * const d32= dither_8x8_32[y&7];\
  1044. func(uint8_t,0)\
  1045. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
  1046. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
  1047. }\
  1048. }\
  1049. break;\
  1050. case PIX_FMT_RGB4:\
  1051. case PIX_FMT_BGR4:\
  1052. {\
  1053. const uint8_t * const d64= dither_8x8_73 [y&7];\
  1054. const uint8_t * const d128=dither_8x8_220[y&7];\
  1055. func(uint8_t,0)\
  1056. ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
  1057. + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
  1058. }\
  1059. }\
  1060. break;\
  1061. case PIX_FMT_RGB4_BYTE:\
  1062. case PIX_FMT_BGR4_BYTE:\
  1063. {\
  1064. const uint8_t * const d64= dither_8x8_73 [y&7];\
  1065. const uint8_t * const d128=dither_8x8_220[y&7];\
  1066. func(uint8_t,0)\
  1067. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
  1068. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
  1069. }\
  1070. }\
  1071. break;\
  1072. }
  1073. static void yuv2packedX_c(SwsContext *c, const int16_t *lumFilter,
  1074. const int16_t **lumSrc, int lumFilterSize,
  1075. const int16_t *chrFilter, const int16_t **chrUSrc,
  1076. const int16_t **chrVSrc, int chrFilterSize,
  1077. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  1078. {
  1079. int i;
  1080. YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGBX_C)
  1081. }
  1082. static void yuv2rgbX_c_full(SwsContext *c, const int16_t *lumFilter,
  1083. const int16_t **lumSrc, int lumFilterSize,
  1084. const int16_t *chrFilter, const int16_t **chrUSrc,
  1085. const int16_t **chrVSrc, int chrFilterSize,
  1086. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  1087. {
  1088. int i;
  1089. int step= c->dstFormatBpp/8;
  1090. int aidx= 3;
  1091. switch(c->dstFormat) {
  1092. case PIX_FMT_ARGB:
  1093. dest++;
  1094. aidx= 0;
  1095. case PIX_FMT_RGB24:
  1096. aidx--;
  1097. case PIX_FMT_RGBA:
  1098. if (CONFIG_SMALL) {
  1099. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
  1100. YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
  1101. dest[aidx]= needAlpha ? A : 255;
  1102. dest[0]= R>>22;
  1103. dest[1]= G>>22;
  1104. dest[2]= B>>22;
  1105. dest+= step;
  1106. }
  1107. } else {
  1108. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  1109. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
  1110. dest[aidx]= A;
  1111. dest[0]= R>>22;
  1112. dest[1]= G>>22;
  1113. dest[2]= B>>22;
  1114. dest+= step;
  1115. }
  1116. } else {
  1117. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
  1118. dest[aidx]= 255;
  1119. dest[0]= R>>22;
  1120. dest[1]= G>>22;
  1121. dest[2]= B>>22;
  1122. dest+= step;
  1123. }
  1124. }
  1125. }
  1126. break;
  1127. case PIX_FMT_ABGR:
  1128. dest++;
  1129. aidx= 0;
  1130. case PIX_FMT_BGR24:
  1131. aidx--;
  1132. case PIX_FMT_BGRA:
  1133. if (CONFIG_SMALL) {
  1134. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
  1135. YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
  1136. dest[aidx]= needAlpha ? A : 255;
  1137. dest[0]= B>>22;
  1138. dest[1]= G>>22;
  1139. dest[2]= R>>22;
  1140. dest+= step;
  1141. }
  1142. } else {
  1143. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  1144. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
  1145. dest[aidx]= A;
  1146. dest[0]= B>>22;
  1147. dest[1]= G>>22;
  1148. dest[2]= R>>22;
  1149. dest+= step;
  1150. }
  1151. } else {
  1152. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
  1153. dest[aidx]= 255;
  1154. dest[0]= B>>22;
  1155. dest[1]= G>>22;
  1156. dest[2]= R>>22;
  1157. dest+= step;
  1158. }
  1159. }
  1160. }
  1161. break;
  1162. default:
  1163. assert(0);
  1164. }
  1165. }
  1166. /**
  1167. * vertical bilinear scale YV12 to RGB
  1168. */
  1169. static void yuv2packed2_c(SwsContext *c, const uint16_t *buf0,
  1170. const uint16_t *buf1, const uint16_t *ubuf0,
  1171. const uint16_t *ubuf1, const uint16_t *vbuf0,
  1172. const uint16_t *vbuf1, const uint16_t *abuf0,
  1173. const uint16_t *abuf1, uint8_t *dest, int dstW,
  1174. int yalpha, int uvalpha, int y)
  1175. {
  1176. int yalpha1=4095- yalpha;
  1177. int uvalpha1=4095-uvalpha;
  1178. int i;
  1179. YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGB2_C)
  1180. }
  1181. /**
  1182. * YV12 to RGB without scaling or interpolating
  1183. */
  1184. static void yuv2packed1_c(SwsContext *c, const uint16_t *buf0,
  1185. const uint16_t *ubuf0, const uint16_t *ubuf1,
  1186. const uint16_t *vbuf0, const uint16_t *vbuf1,
  1187. const uint16_t *abuf0, uint8_t *dest, int dstW,
  1188. int uvalpha, enum PixelFormat dstFormat,
  1189. int flags, int y)
  1190. {
  1191. int i;
  1192. if (uvalpha < 2048) {
  1193. YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGB1_C)
  1194. } else {
  1195. YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGB1B_C)
  1196. }
  1197. }
  1198. static av_always_inline void fillPlane(uint8_t* plane, int stride,
  1199. int width, int height,
  1200. int y, uint8_t val)
  1201. {
  1202. int i;
  1203. uint8_t *ptr = plane + stride*y;
  1204. for (i=0; i<height; i++) {
  1205. memset(ptr, val, width);
  1206. ptr += stride;
  1207. }
  1208. }
  1209. #define input_pixel(pos) (isBE(origin) ? AV_RB16(pos) : AV_RL16(pos))
  1210. #define r ((origin == PIX_FMT_BGR48BE || origin == PIX_FMT_BGR48LE) ? b_r : r_b)
  1211. #define b ((origin == PIX_FMT_BGR48BE || origin == PIX_FMT_BGR48LE) ? r_b : b_r)
  1212. static av_always_inline void
  1213. rgb48ToY_c_template(uint8_t *dst, const uint8_t *src, int width,
  1214. enum PixelFormat origin)
  1215. {
  1216. int i;
  1217. for (i = 0; i < width; i++) {
  1218. int r_b = input_pixel(&src[i*6+0]) >> 8;
  1219. int g = input_pixel(&src[i*6+2]) >> 8;
  1220. int b_r = input_pixel(&src[i*6+4]) >> 8;
  1221. dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1222. }
  1223. }
  1224. static av_always_inline void
  1225. rgb48ToUV_c_template(uint8_t *dstU, uint8_t *dstV,
  1226. const uint8_t *src1, const uint8_t *src2,
  1227. int width, enum PixelFormat origin)
  1228. {
  1229. int i;
  1230. assert(src1==src2);
  1231. for (i = 0; i < width; i++) {
  1232. int r_b = input_pixel(&src1[i*6+0]) >> 8;
  1233. int g = input_pixel(&src1[i*6+2]) >> 8;
  1234. int b_r = input_pixel(&src1[i*6+4]) >> 8;
  1235. dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1236. dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1237. }
  1238. }
  1239. static av_always_inline void
  1240. rgb48ToUV_half_c_template(uint8_t *dstU, uint8_t *dstV,
  1241. const uint8_t *src1, const uint8_t *src2,
  1242. int width, enum PixelFormat origin)
  1243. {
  1244. int i;
  1245. assert(src1==src2);
  1246. for (i = 0; i < width; i++) {
  1247. int r_b = (input_pixel(&src1[12*i + 0]) >> 8) + (input_pixel(&src1[12*i + 6]) >> 8);
  1248. int g = (input_pixel(&src1[12*i + 2]) >> 8) + (input_pixel(&src1[12*i + 8]) >> 8);
  1249. int b_r = (input_pixel(&src1[12*i + 4]) >> 8) + (input_pixel(&src1[12*i + 10]) >> 8);
  1250. dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  1251. dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  1252. }
  1253. }
  1254. #undef r
  1255. #undef b
  1256. #undef input_pixel
  1257. #define rgb48funcs(pattern, BE_LE, origin) \
  1258. static void pattern ## 48 ## BE_LE ## ToY_c(uint8_t *dst, const uint8_t *src, \
  1259. int width, uint32_t *unused) \
  1260. { \
  1261. rgb48ToY_c_template(dst, src, width, origin); \
  1262. } \
  1263. \
  1264. static void pattern ## 48 ## BE_LE ## ToUV_c(uint8_t *dstU, uint8_t *dstV, \
  1265. const uint8_t *src1, const uint8_t *src2, \
  1266. int width, uint32_t *unused) \
  1267. { \
  1268. rgb48ToUV_c_template(dstU, dstV, src1, src2, width, origin); \
  1269. } \
  1270. \
  1271. static void pattern ## 48 ## BE_LE ## ToUV_half_c(uint8_t *dstU, uint8_t *dstV, \
  1272. const uint8_t *src1, const uint8_t *src2, \
  1273. int width, uint32_t *unused) \
  1274. { \
  1275. rgb48ToUV_half_c_template(dstU, dstV, src1, src2, width, origin); \
  1276. }
  1277. rgb48funcs(rgb, LE, PIX_FMT_RGB48LE);
  1278. rgb48funcs(rgb, BE, PIX_FMT_RGB48BE);
  1279. rgb48funcs(bgr, LE, PIX_FMT_BGR48LE);
  1280. rgb48funcs(bgr, BE, PIX_FMT_BGR48BE);
  1281. #define input_pixel(i) ((origin == PIX_FMT_RGBA || origin == PIX_FMT_BGRA || \
  1282. origin == PIX_FMT_ARGB || origin == PIX_FMT_ABGR) ? AV_RN32A(&src[(i)*4]) : \
  1283. (isBE(origin) ? AV_RB16(&src[(i)*2]) : AV_RL16(&src[(i)*2])))
  1284. static av_always_inline void
  1285. rgb16_32ToY_c_template(uint8_t *dst, const uint8_t *src,
  1286. int width, enum PixelFormat origin,
  1287. int shr, int shg, int shb, int shp,
  1288. int maskr, int maskg, int maskb,
  1289. int rsh, int gsh, int bsh, int S)
  1290. {
  1291. const int ry = RY << rsh, gy = GY << gsh, by = BY << bsh,
  1292. rnd = 33 << (S - 1);
  1293. int i;
  1294. for (i = 0; i < width; i++) {
  1295. int px = input_pixel(i) >> shp;
  1296. int b = (px & maskb) >> shb;
  1297. int g = (px & maskg) >> shg;
  1298. int r = (px & maskr) >> shr;
  1299. dst[i] = (ry * r + gy * g + by * b + rnd) >> S;
  1300. }
  1301. }
  1302. static av_always_inline void
  1303. rgb16_32ToUV_c_template(uint8_t *dstU, uint8_t *dstV,
  1304. const uint8_t *src, int width,
  1305. enum PixelFormat origin,
  1306. int shr, int shg, int shb, int shp,
  1307. int maskr, int maskg, int maskb,
  1308. int rsh, int gsh, int bsh, int S)
  1309. {
  1310. const int ru = RU << rsh, gu = GU << gsh, bu = BU << bsh,
  1311. rv = RV << rsh, gv = GV << gsh, bv = BV << bsh,
  1312. rnd = 257 << (S - 1);
  1313. int i;
  1314. for (i = 0; i < width; i++) {
  1315. int px = input_pixel(i) >> shp;
  1316. int b = (px & maskb) >> shb;
  1317. int g = (px & maskg) >> shg;
  1318. int r = (px & maskr) >> shr;
  1319. dstU[i] = (ru * r + gu * g + bu * b + rnd) >> S;
  1320. dstV[i] = (rv * r + gv * g + bv * b + rnd) >> S;
  1321. }
  1322. }
  1323. static av_always_inline void
  1324. rgb16_32ToUV_half_c_template(uint8_t *dstU, uint8_t *dstV,
  1325. const uint8_t *src, int width,
  1326. enum PixelFormat origin,
  1327. int shr, int shg, int shb, int shp,
  1328. int maskr, int maskg, int maskb,
  1329. int rsh, int gsh, int bsh, int S)
  1330. {
  1331. const int ru = RU << rsh, gu = GU << gsh, bu = BU << bsh,
  1332. rv = RV << rsh, gv = GV << gsh, bv = BV << bsh,
  1333. rnd = 257 << S, maskgx = ~(maskr | maskb);
  1334. int i;
  1335. maskr |= maskr << 1; maskb |= maskb << 1; maskg |= maskg << 1;
  1336. for (i = 0; i < width; i++) {
  1337. int px0 = input_pixel(2 * i + 0) >> shp;
  1338. int px1 = input_pixel(2 * i + 1) >> shp;
  1339. int b, r, g = (px0 & maskgx) + (px1 & maskgx);
  1340. int rb = px0 + px1 - g;
  1341. b = (rb & maskb) >> shb;
  1342. if (shp || origin == PIX_FMT_BGR565LE || origin == PIX_FMT_BGR565BE ||
  1343. origin == PIX_FMT_RGB565LE || origin == PIX_FMT_RGB565BE) {
  1344. g >>= shg;
  1345. } else {
  1346. g = (g & maskg) >> shg;
  1347. }
  1348. r = (rb & maskr) >> shr;
  1349. dstU[i] = (ru * r + gu * g + bu * b + rnd) >> (S + 1);
  1350. dstV[i] = (rv * r + gv * g + bv * b + rnd) >> (S + 1);
  1351. }
  1352. }
  1353. #undef input_pixel
  1354. #define rgb16_32_wrapper(fmt, name, shr, shg, shb, shp, maskr, \
  1355. maskg, maskb, rsh, gsh, bsh, S) \
  1356. static void name ## ToY_c(uint8_t *dst, const uint8_t *src, \
  1357. int width, uint32_t *unused) \
  1358. { \
  1359. rgb16_32ToY_c_template(dst, src, width, fmt, shr, shg, shb, shp, \
  1360. maskr, maskg, maskb, rsh, gsh, bsh, S); \
  1361. } \
  1362. \
  1363. static void name ## ToUV_c(uint8_t *dstU, uint8_t *dstV, \
  1364. const uint8_t *src, const uint8_t *dummy, \
  1365. int width, uint32_t *unused) \
  1366. { \
  1367. rgb16_32ToUV_c_template(dstU, dstV, src, width, fmt, shr, shg, shb, shp, \
  1368. maskr, maskg, maskb, rsh, gsh, bsh, S); \
  1369. } \
  1370. \
  1371. static void name ## ToUV_half_c(uint8_t *dstU, uint8_t *dstV, \
  1372. const uint8_t *src, const uint8_t *dummy, \
  1373. int width, uint32_t *unused) \
  1374. { \
  1375. rgb16_32ToUV_half_c_template(dstU, dstV, src, width, fmt, shr, shg, shb, shp, \
  1376. maskr, maskg, maskb, rsh, gsh, bsh, S); \
  1377. }
  1378. rgb16_32_wrapper(PIX_FMT_BGR32, bgr32, 16, 0, 0, 0, 0xFF0000, 0xFF00, 0x00FF, 8, 0, 8, RGB2YUV_SHIFT+8);
  1379. rgb16_32_wrapper(PIX_FMT_BGR32_1, bgr321, 16, 0, 0, 8, 0xFF0000, 0xFF00, 0x00FF, 8, 0, 8, RGB2YUV_SHIFT+8);
  1380. rgb16_32_wrapper(PIX_FMT_RGB32, rgb32, 0, 0, 16, 0, 0x00FF, 0xFF00, 0xFF0000, 8, 0, 8, RGB2YUV_SHIFT+8);
  1381. rgb16_32_wrapper(PIX_FMT_RGB32_1, rgb321, 0, 0, 16, 8, 0x00FF, 0xFF00, 0xFF0000, 8, 0, 8, RGB2YUV_SHIFT+8);
  1382. rgb16_32_wrapper(PIX_FMT_BGR565LE, bgr16le, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, 11, 5, 0, RGB2YUV_SHIFT+8);
  1383. rgb16_32_wrapper(PIX_FMT_BGR555LE, bgr15le, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, 10, 5, 0, RGB2YUV_SHIFT+7);
  1384. rgb16_32_wrapper(PIX_FMT_RGB565LE, rgb16le, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, 0, 5, 11, RGB2YUV_SHIFT+8);
  1385. rgb16_32_wrapper(PIX_FMT_RGB555LE, rgb15le, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, 0, 5, 10, RGB2YUV_SHIFT+7);
  1386. rgb16_32_wrapper(PIX_FMT_BGR565BE, bgr16be, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, 11, 5, 0, RGB2YUV_SHIFT+8);
  1387. rgb16_32_wrapper(PIX_FMT_BGR555BE, bgr15be, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, 10, 5, 0, RGB2YUV_SHIFT+7);
  1388. rgb16_32_wrapper(PIX_FMT_RGB565BE, rgb16be, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, 0, 5, 11, RGB2YUV_SHIFT+8);
  1389. rgb16_32_wrapper(PIX_FMT_RGB555BE, rgb15be, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, 0, 5, 10, RGB2YUV_SHIFT+7);
  1390. static void abgrToA_c(uint8_t *dst, const uint8_t *src, int width, uint32_t *unused)
  1391. {
  1392. int i;
  1393. for (i=0; i<width; i++) {
  1394. dst[i]= src[4*i];
  1395. }
  1396. }
  1397. static void rgbaToA_c(uint8_t *dst, const uint8_t *src, int width, uint32_t *unused)
  1398. {
  1399. int i;
  1400. for (i=0; i<width; i++) {
  1401. dst[i]= src[4*i+3];
  1402. }
  1403. }
  1404. static void palToY_c(uint8_t *dst, const uint8_t *src, int width, uint32_t *pal)
  1405. {
  1406. int i;
  1407. for (i=0; i<width; i++) {
  1408. int d= src[i];
  1409. dst[i]= pal[d] & 0xFF;
  1410. }
  1411. }
  1412. static void palToUV_c(uint8_t *dstU, uint8_t *dstV,
  1413. const uint8_t *src1, const uint8_t *src2,
  1414. int width, uint32_t *pal)
  1415. {
  1416. int i;
  1417. assert(src1 == src2);
  1418. for (i=0; i<width; i++) {
  1419. int p= pal[src1[i]];
  1420. dstU[i]= p>>8;
  1421. dstV[i]= p>>16;
  1422. }
  1423. }
  1424. static void monowhite2Y_c(uint8_t *dst, const uint8_t *src,
  1425. int width, uint32_t *unused)
  1426. {
  1427. int i, j;
  1428. for (i=0; i<width/8; i++) {
  1429. int d= ~src[i];
  1430. for(j=0; j<8; j++)
  1431. dst[8*i+j]= ((d>>(7-j))&1)*255;
  1432. }
  1433. }
  1434. static void monoblack2Y_c(uint8_t *dst, const uint8_t *src,
  1435. int width, uint32_t *unused)
  1436. {
  1437. int i, j;
  1438. for (i=0; i<width/8; i++) {
  1439. int d= src[i];
  1440. for(j=0; j<8; j++)
  1441. dst[8*i+j]= ((d>>(7-j))&1)*255;
  1442. }
  1443. }
  1444. //FIXME yuy2* can read up to 7 samples too much
  1445. static void yuy2ToY_c(uint8_t *dst, const uint8_t *src, int width,
  1446. uint32_t *unused)
  1447. {
  1448. int i;
  1449. for (i=0; i<width; i++)
  1450. dst[i]= src[2*i];
  1451. }
  1452. static void yuy2ToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  1453. const uint8_t *src2, int width, uint32_t *unused)
  1454. {
  1455. int i;
  1456. for (i=0; i<width; i++) {
  1457. dstU[i]= src1[4*i + 1];
  1458. dstV[i]= src1[4*i + 3];
  1459. }
  1460. assert(src1 == src2);
  1461. }
  1462. static void LEToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  1463. const uint8_t *src2, int width, uint32_t *unused)
  1464. {
  1465. int i;
  1466. for (i=0; i<width; i++) {
  1467. dstU[i]= src1[2*i + 1];
  1468. dstV[i]= src2[2*i + 1];
  1469. }
  1470. }
  1471. /* This is almost identical to the previous, end exists only because
  1472. * yuy2ToY/UV)(dst, src+1, ...) would have 100% unaligned accesses. */
  1473. static void uyvyToY_c(uint8_t *dst, const uint8_t *src, int width,
  1474. uint32_t *unused)
  1475. {
  1476. int i;
  1477. for (i=0; i<width; i++)
  1478. dst[i]= src[2*i+1];
  1479. }
  1480. static void uyvyToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  1481. const uint8_t *src2, int width, uint32_t *unused)
  1482. {
  1483. int i;
  1484. for (i=0; i<width; i++) {
  1485. dstU[i]= src1[4*i + 0];
  1486. dstV[i]= src1[4*i + 2];
  1487. }
  1488. assert(src1 == src2);
  1489. }
  1490. static void BEToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  1491. const uint8_t *src2, int width, uint32_t *unused)
  1492. {
  1493. int i;
  1494. for (i=0; i<width; i++) {
  1495. dstU[i]= src1[2*i];
  1496. dstV[i]= src2[2*i];
  1497. }
  1498. }
  1499. static av_always_inline void nvXXtoUV_c(uint8_t *dst1, uint8_t *dst2,
  1500. const uint8_t *src, int width)
  1501. {
  1502. int i;
  1503. for (i = 0; i < width; i++) {
  1504. dst1[i] = src[2*i+0];
  1505. dst2[i] = src[2*i+1];
  1506. }
  1507. }
  1508. static void nv12ToUV_c(uint8_t *dstU, uint8_t *dstV,
  1509. const uint8_t *src1, const uint8_t *src2,
  1510. int width, uint32_t *unused)
  1511. {
  1512. nvXXtoUV_c(dstU, dstV, src1, width);
  1513. }
  1514. static void nv21ToUV_c(uint8_t *dstU, uint8_t *dstV,
  1515. const uint8_t *src1, const uint8_t *src2,
  1516. int width, uint32_t *unused)
  1517. {
  1518. nvXXtoUV_c(dstV, dstU, src1, width);
  1519. }
  1520. #define input_pixel(pos) (isBE(origin) ? AV_RB16(pos) : AV_RL16(pos))
  1521. // FIXME Maybe dither instead.
  1522. static av_always_inline void
  1523. yuv9_OR_10ToUV_c_template(uint8_t *dstU, uint8_t *dstV,
  1524. const uint8_t *_srcU, const uint8_t *_srcV,
  1525. int width, enum PixelFormat origin, int depth)
  1526. {
  1527. int i;
  1528. const uint16_t *srcU = (const uint16_t *) _srcU;
  1529. const uint16_t *srcV = (const uint16_t *) _srcV;
  1530. for (i = 0; i < width; i++) {
  1531. dstU[i] = input_pixel(&srcU[i]) >> (depth - 8);
  1532. dstV[i] = input_pixel(&srcV[i]) >> (depth - 8);
  1533. }
  1534. }
  1535. static av_always_inline void
  1536. yuv9_or_10ToY_c_template(uint8_t *dstY, const uint8_t *_srcY,
  1537. int width, enum PixelFormat origin, int depth)
  1538. {
  1539. int i;
  1540. const uint16_t *srcY = (const uint16_t*)_srcY;
  1541. for (i = 0; i < width; i++)
  1542. dstY[i] = input_pixel(&srcY[i]) >> (depth - 8);
  1543. }
  1544. #undef input_pixel
  1545. #define YUV_NBPS(depth, BE_LE, origin) \
  1546. static void BE_LE ## depth ## ToUV_c(uint8_t *dstU, uint8_t *dstV, \
  1547. const uint8_t *srcU, const uint8_t *srcV, \
  1548. int width, uint32_t *unused) \
  1549. { \
  1550. yuv9_OR_10ToUV_c_template(dstU, dstV, srcU, srcV, width, origin, depth); \
  1551. } \
  1552. static void BE_LE ## depth ## ToY_c(uint8_t *dstY, const uint8_t *srcY, \
  1553. int width, uint32_t *unused) \
  1554. { \
  1555. yuv9_or_10ToY_c_template(dstY, srcY, width, origin, depth); \
  1556. }
  1557. YUV_NBPS( 9, LE, PIX_FMT_YUV420P9LE);
  1558. YUV_NBPS( 9, BE, PIX_FMT_YUV420P9BE);
  1559. YUV_NBPS(10, LE, PIX_FMT_YUV420P10LE);
  1560. YUV_NBPS(10, BE, PIX_FMT_YUV420P10BE);
  1561. static void bgr24ToY_c(uint8_t *dst, const uint8_t *src,
  1562. int width, uint32_t *unused)
  1563. {
  1564. int i;
  1565. for (i=0; i<width; i++) {
  1566. int b= src[i*3+0];
  1567. int g= src[i*3+1];
  1568. int r= src[i*3+2];
  1569. dst[i]= ((RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1570. }
  1571. }
  1572. static void bgr24ToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  1573. const uint8_t *src2, int width, uint32_t *unused)
  1574. {
  1575. int i;
  1576. for (i=0; i<width; i++) {
  1577. int b= src1[3*i + 0];
  1578. int g= src1[3*i + 1];
  1579. int r= src1[3*i + 2];
  1580. dstU[i]= (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT;
  1581. dstV[i]= (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT;
  1582. }
  1583. assert(src1 == src2);
  1584. }
  1585. static void bgr24ToUV_half_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  1586. const uint8_t *src2, int width, uint32_t *unused)
  1587. {
  1588. int i;
  1589. for (i=0; i<width; i++) {
  1590. int b= src1[6*i + 0] + src1[6*i + 3];
  1591. int g= src1[6*i + 1] + src1[6*i + 4];
  1592. int r= src1[6*i + 2] + src1[6*i + 5];
  1593. dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT))>>(RGB2YUV_SHIFT+1);
  1594. dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT))>>(RGB2YUV_SHIFT+1);
  1595. }
  1596. assert(src1 == src2);
  1597. }
  1598. static void rgb24ToY_c(uint8_t *dst, const uint8_t *src, int width,
  1599. uint32_t *unused)
  1600. {
  1601. int i;
  1602. for (i=0; i<width; i++) {
  1603. int r= src[i*3+0];
  1604. int g= src[i*3+1];
  1605. int b= src[i*3+2];
  1606. dst[i]= ((RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1607. }
  1608. }
  1609. static void rgb24ToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  1610. const uint8_t *src2, int width, uint32_t *unused)
  1611. {
  1612. int i;
  1613. assert(src1==src2);
  1614. for (i=0; i<width; i++) {
  1615. int r= src1[3*i + 0];
  1616. int g= src1[3*i + 1];
  1617. int b= src1[3*i + 2];
  1618. dstU[i]= (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT;
  1619. dstV[i]= (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT;
  1620. }
  1621. }
  1622. static void rgb24ToUV_half_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  1623. const uint8_t *src2, int width, uint32_t *unused)
  1624. {
  1625. int i;
  1626. assert(src1==src2);
  1627. for (i=0; i<width; i++) {
  1628. int r= src1[6*i + 0] + src1[6*i + 3];
  1629. int g= src1[6*i + 1] + src1[6*i + 4];
  1630. int b= src1[6*i + 2] + src1[6*i + 5];
  1631. dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT))>>(RGB2YUV_SHIFT+1);
  1632. dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT))>>(RGB2YUV_SHIFT+1);
  1633. }
  1634. }
  1635. // bilinear / bicubic scaling
  1636. static void hScale_c(int16_t *dst, int dstW, const uint8_t *src,
  1637. int srcW, int xInc,
  1638. const int16_t *filter, const int16_t *filterPos,
  1639. int filterSize)
  1640. {
  1641. int i;
  1642. for (i=0; i<dstW; i++) {
  1643. int j;
  1644. int srcPos= filterPos[i];
  1645. int val=0;
  1646. for (j=0; j<filterSize; j++) {
  1647. val += ((int)src[srcPos + j])*filter[filterSize*i + j];
  1648. }
  1649. //filter += hFilterSize;
  1650. dst[i] = FFMIN(val>>7, (1<<15)-1); // the cubic equation does overflow ...
  1651. //dst[i] = val>>7;
  1652. }
  1653. }
  1654. //FIXME all pal and rgb srcFormats could do this convertion as well
  1655. //FIXME all scalers more complex than bilinear could do half of this transform
  1656. static void chrRangeToJpeg_c(int16_t *dstU, int16_t *dstV, int width)
  1657. {
  1658. int i;
  1659. for (i = 0; i < width; i++) {
  1660. dstU[i] = (FFMIN(dstU[i],30775)*4663 - 9289992)>>12; //-264
  1661. dstV[i] = (FFMIN(dstV[i],30775)*4663 - 9289992)>>12; //-264
  1662. }
  1663. }
  1664. static void chrRangeFromJpeg_c(int16_t *dstU, int16_t *dstV, int width)
  1665. {
  1666. int i;
  1667. for (i = 0; i < width; i++) {
  1668. dstU[i] = (dstU[i]*1799 + 4081085)>>11; //1469
  1669. dstV[i] = (dstV[i]*1799 + 4081085)>>11; //1469
  1670. }
  1671. }
  1672. static void lumRangeToJpeg_c(int16_t *dst, int width)
  1673. {
  1674. int i;
  1675. for (i = 0; i < width; i++)
  1676. dst[i] = (FFMIN(dst[i],30189)*19077 - 39057361)>>14;
  1677. }
  1678. static void lumRangeFromJpeg_c(int16_t *dst, int width)
  1679. {
  1680. int i;
  1681. for (i = 0; i < width; i++)
  1682. dst[i] = (dst[i]*14071 + 33561947)>>14;
  1683. }
  1684. static void hyscale_fast_c(SwsContext *c, int16_t *dst, int dstWidth,
  1685. const uint8_t *src, int srcW, int xInc)
  1686. {
  1687. int i;
  1688. unsigned int xpos=0;
  1689. for (i=0;i<dstWidth;i++) {
  1690. register unsigned int xx=xpos>>16;
  1691. register unsigned int xalpha=(xpos&0xFFFF)>>9;
  1692. dst[i]= (src[xx]<<7) + (src[xx+1] - src[xx])*xalpha;
  1693. xpos+=xInc;
  1694. }
  1695. }
  1696. // *** horizontal scale Y line to temp buffer
  1697. static av_always_inline void hyscale(SwsContext *c, uint16_t *dst, int dstWidth,
  1698. const uint8_t *src, int srcW, int xInc,
  1699. const int16_t *hLumFilter,
  1700. const int16_t *hLumFilterPos, int hLumFilterSize,
  1701. uint8_t *formatConvBuffer,
  1702. uint32_t *pal, int isAlpha)
  1703. {
  1704. void (*toYV12)(uint8_t *, const uint8_t *, int, uint32_t *) = isAlpha ? c->alpToYV12 : c->lumToYV12;
  1705. void (*convertRange)(int16_t *, int) = isAlpha ? NULL : c->lumConvertRange;
  1706. if (toYV12) {
  1707. toYV12(formatConvBuffer, src, srcW, pal);
  1708. src= formatConvBuffer;
  1709. }
  1710. if (!c->hyscale_fast) {
  1711. c->hScale(dst, dstWidth, src, srcW, xInc, hLumFilter, hLumFilterPos, hLumFilterSize);
  1712. } else { // fast bilinear upscale / crap downscale
  1713. c->hyscale_fast(c, dst, dstWidth, src, srcW, xInc);
  1714. }
  1715. if (convertRange)
  1716. convertRange(dst, dstWidth);
  1717. }
  1718. static void hcscale_fast_c(SwsContext *c, int16_t *dst1, int16_t *dst2,
  1719. int dstWidth, const uint8_t *src1,
  1720. const uint8_t *src2, int srcW, int xInc)
  1721. {
  1722. int i;
  1723. unsigned int xpos=0;
  1724. for (i=0;i<dstWidth;i++) {
  1725. register unsigned int xx=xpos>>16;
  1726. register unsigned int xalpha=(xpos&0xFFFF)>>9;
  1727. dst1[i]=(src1[xx]*(xalpha^127)+src1[xx+1]*xalpha);
  1728. dst2[i]=(src2[xx]*(xalpha^127)+src2[xx+1]*xalpha);
  1729. xpos+=xInc;
  1730. }
  1731. }
  1732. static av_always_inline void hcscale(SwsContext *c, uint16_t *dst1, uint16_t *dst2, int dstWidth,
  1733. const uint8_t *src1, const uint8_t *src2,
  1734. int srcW, int xInc, const int16_t *hChrFilter,
  1735. const int16_t *hChrFilterPos, int hChrFilterSize,
  1736. uint8_t *formatConvBuffer, uint32_t *pal)
  1737. {
  1738. if (c->chrToYV12) {
  1739. uint8_t *buf2 = formatConvBuffer + FFALIGN(srcW, 16);
  1740. c->chrToYV12(formatConvBuffer, buf2, src1, src2, srcW, pal);
  1741. src1= formatConvBuffer;
  1742. src2= buf2;
  1743. }
  1744. if (!c->hcscale_fast) {
  1745. c->hScale(dst1, dstWidth, src1, srcW, xInc, hChrFilter, hChrFilterPos, hChrFilterSize);
  1746. c->hScale(dst2, dstWidth, src2, srcW, xInc, hChrFilter, hChrFilterPos, hChrFilterSize);
  1747. } else { // fast bilinear upscale / crap downscale
  1748. c->hcscale_fast(c, dst1, dst2, dstWidth, src1, src2, srcW, xInc);
  1749. }
  1750. if (c->chrConvertRange)
  1751. c->chrConvertRange(dst1, dst2, dstWidth);
  1752. }
  1753. static av_always_inline void
  1754. find_c_packed_planar_out_funcs(SwsContext *c,
  1755. yuv2planar1_fn *yuv2yuv1, yuv2planarX_fn *yuv2yuvX,
  1756. yuv2packed1_fn *yuv2packed1, yuv2packed2_fn *yuv2packed2,
  1757. yuv2packedX_fn *yuv2packedX)
  1758. {
  1759. enum PixelFormat dstFormat = c->dstFormat;
  1760. if (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21) {
  1761. *yuv2yuvX = yuv2nv12X_c;
  1762. } else if (is16BPS(dstFormat)) {
  1763. *yuv2yuvX = isBE(dstFormat) ? yuv2yuvX16BE_c : yuv2yuvX16LE_c;
  1764. } else if (is9_OR_10BPS(dstFormat)) {
  1765. if (av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1 == 8) {
  1766. *yuv2yuvX = isBE(dstFormat) ? yuv2yuvX9BE_c : yuv2yuvX9LE_c;
  1767. } else {
  1768. *yuv2yuvX = isBE(dstFormat) ? yuv2yuvX10BE_c : yuv2yuvX10LE_c;
  1769. }
  1770. } else {
  1771. *yuv2yuv1 = yuv2yuv1_c;
  1772. *yuv2yuvX = yuv2yuvX_c;
  1773. }
  1774. if(c->flags & SWS_FULL_CHR_H_INT) {
  1775. *yuv2packedX = yuv2rgbX_c_full;
  1776. } else {
  1777. switch (dstFormat) {
  1778. case PIX_FMT_GRAY16BE:
  1779. *yuv2packed1 = yuv2gray16BE_1_c;
  1780. *yuv2packed2 = yuv2gray16BE_2_c;
  1781. *yuv2packedX = yuv2gray16BE_X_c;
  1782. break;
  1783. case PIX_FMT_GRAY16LE:
  1784. *yuv2packed1 = yuv2gray16LE_1_c;
  1785. *yuv2packed2 = yuv2gray16LE_2_c;
  1786. *yuv2packedX = yuv2gray16LE_X_c;
  1787. break;
  1788. case PIX_FMT_MONOWHITE:
  1789. *yuv2packed1 = yuv2monowhite_1_c;
  1790. *yuv2packed2 = yuv2monowhite_2_c;
  1791. *yuv2packedX = yuv2monowhite_X_c;
  1792. break;
  1793. case PIX_FMT_MONOBLACK:
  1794. *yuv2packed1 = yuv2monoblack_1_c;
  1795. *yuv2packed2 = yuv2monoblack_2_c;
  1796. *yuv2packedX = yuv2monoblack_X_c;
  1797. break;
  1798. case PIX_FMT_YUYV422:
  1799. *yuv2packed1 = yuv2yuyv422_1_c;
  1800. *yuv2packed2 = yuv2yuyv422_2_c;
  1801. *yuv2packedX = yuv2yuyv422_X_c;
  1802. break;
  1803. case PIX_FMT_UYVY422:
  1804. *yuv2packed1 = yuv2uyvy422_1_c;
  1805. *yuv2packed2 = yuv2uyvy422_2_c;
  1806. *yuv2packedX = yuv2uyvy422_X_c;
  1807. break;
  1808. case PIX_FMT_RGB48LE:
  1809. //*yuv2packed1 = yuv2rgb48le_1_c;
  1810. //*yuv2packed2 = yuv2rgb48le_2_c;
  1811. //*yuv2packedX = yuv2rgb48le_X_c;
  1812. //break;
  1813. case PIX_FMT_RGB48BE:
  1814. *yuv2packed1 = yuv2rgb48be_1_c;
  1815. *yuv2packed2 = yuv2rgb48be_2_c;
  1816. *yuv2packedX = yuv2rgb48be_X_c;
  1817. break;
  1818. case PIX_FMT_BGR48LE:
  1819. //*yuv2packed1 = yuv2bgr48le_1_c;
  1820. //*yuv2packed2 = yuv2bgr48le_2_c;
  1821. //*yuv2packedX = yuv2bgr48le_X_c;
  1822. //break;
  1823. case PIX_FMT_BGR48BE:
  1824. *yuv2packed1 = yuv2bgr48be_1_c;
  1825. *yuv2packed2 = yuv2bgr48be_2_c;
  1826. *yuv2packedX = yuv2bgr48be_X_c;
  1827. break;
  1828. default:
  1829. *yuv2packed1 = yuv2packed1_c;
  1830. *yuv2packed2 = yuv2packed2_c;
  1831. *yuv2packedX = yuv2packedX_c;
  1832. break;
  1833. }
  1834. }
  1835. }
  1836. #define DEBUG_SWSCALE_BUFFERS 0
  1837. #define DEBUG_BUFFERS(...) if (DEBUG_SWSCALE_BUFFERS) av_log(c, AV_LOG_DEBUG, __VA_ARGS__)
  1838. static int swScale(SwsContext *c, const uint8_t* src[],
  1839. int srcStride[], int srcSliceY,
  1840. int srcSliceH, uint8_t* dst[], int dstStride[])
  1841. {
  1842. /* load a few things into local vars to make the code more readable? and faster */
  1843. const int srcW= c->srcW;
  1844. const int dstW= c->dstW;
  1845. const int dstH= c->dstH;
  1846. const int chrDstW= c->chrDstW;
  1847. const int chrSrcW= c->chrSrcW;
  1848. const int lumXInc= c->lumXInc;
  1849. const int chrXInc= c->chrXInc;
  1850. const enum PixelFormat dstFormat= c->dstFormat;
  1851. const int flags= c->flags;
  1852. int16_t *vLumFilterPos= c->vLumFilterPos;
  1853. int16_t *vChrFilterPos= c->vChrFilterPos;
  1854. int16_t *hLumFilterPos= c->hLumFilterPos;
  1855. int16_t *hChrFilterPos= c->hChrFilterPos;
  1856. int16_t *vLumFilter= c->vLumFilter;
  1857. int16_t *vChrFilter= c->vChrFilter;
  1858. int16_t *hLumFilter= c->hLumFilter;
  1859. int16_t *hChrFilter= c->hChrFilter;
  1860. int32_t *lumMmxFilter= c->lumMmxFilter;
  1861. int32_t *chrMmxFilter= c->chrMmxFilter;
  1862. int32_t av_unused *alpMmxFilter= c->alpMmxFilter;
  1863. const int vLumFilterSize= c->vLumFilterSize;
  1864. const int vChrFilterSize= c->vChrFilterSize;
  1865. const int hLumFilterSize= c->hLumFilterSize;
  1866. const int hChrFilterSize= c->hChrFilterSize;
  1867. int16_t **lumPixBuf= c->lumPixBuf;
  1868. int16_t **chrUPixBuf= c->chrUPixBuf;
  1869. int16_t **chrVPixBuf= c->chrVPixBuf;
  1870. int16_t **alpPixBuf= c->alpPixBuf;
  1871. const int vLumBufSize= c->vLumBufSize;
  1872. const int vChrBufSize= c->vChrBufSize;
  1873. uint8_t *formatConvBuffer= c->formatConvBuffer;
  1874. const int chrSrcSliceY= srcSliceY >> c->chrSrcVSubSample;
  1875. const int chrSrcSliceH= -((-srcSliceH) >> c->chrSrcVSubSample);
  1876. int lastDstY;
  1877. uint32_t *pal=c->pal_yuv;
  1878. yuv2planar1_fn yuv2yuv1 = c->yuv2yuv1;
  1879. yuv2planarX_fn yuv2yuvX = c->yuv2yuvX;
  1880. yuv2packed1_fn yuv2packed1 = c->yuv2packed1;
  1881. yuv2packed2_fn yuv2packed2 = c->yuv2packed2;
  1882. yuv2packedX_fn yuv2packedX = c->yuv2packedX;
  1883. /* vars which will change and which we need to store back in the context */
  1884. int dstY= c->dstY;
  1885. int lumBufIndex= c->lumBufIndex;
  1886. int chrBufIndex= c->chrBufIndex;
  1887. int lastInLumBuf= c->lastInLumBuf;
  1888. int lastInChrBuf= c->lastInChrBuf;
  1889. if (isPacked(c->srcFormat)) {
  1890. src[0]=
  1891. src[1]=
  1892. src[2]=
  1893. src[3]= src[0];
  1894. srcStride[0]=
  1895. srcStride[1]=
  1896. srcStride[2]=
  1897. srcStride[3]= srcStride[0];
  1898. }
  1899. srcStride[1]<<= c->vChrDrop;
  1900. srcStride[2]<<= c->vChrDrop;
  1901. DEBUG_BUFFERS("swScale() %p[%d] %p[%d] %p[%d] %p[%d] -> %p[%d] %p[%d] %p[%d] %p[%d]\n",
  1902. src[0], srcStride[0], src[1], srcStride[1], src[2], srcStride[2], src[3], srcStride[3],
  1903. dst[0], dstStride[0], dst[1], dstStride[1], dst[2], dstStride[2], dst[3], dstStride[3]);
  1904. DEBUG_BUFFERS("srcSliceY: %d srcSliceH: %d dstY: %d dstH: %d\n",
  1905. srcSliceY, srcSliceH, dstY, dstH);
  1906. DEBUG_BUFFERS("vLumFilterSize: %d vLumBufSize: %d vChrFilterSize: %d vChrBufSize: %d\n",
  1907. vLumFilterSize, vLumBufSize, vChrFilterSize, vChrBufSize);
  1908. if (dstStride[0]%8 !=0 || dstStride[1]%8 !=0 || dstStride[2]%8 !=0 || dstStride[3]%8 != 0) {
  1909. static int warnedAlready=0; //FIXME move this into the context perhaps
  1910. if (flags & SWS_PRINT_INFO && !warnedAlready) {
  1911. av_log(c, AV_LOG_WARNING, "Warning: dstStride is not aligned!\n"
  1912. " ->cannot do aligned memory accesses anymore\n");
  1913. warnedAlready=1;
  1914. }
  1915. }
  1916. /* Note the user might start scaling the picture in the middle so this
  1917. will not get executed. This is not really intended but works
  1918. currently, so people might do it. */
  1919. if (srcSliceY ==0) {
  1920. lumBufIndex=-1;
  1921. chrBufIndex=-1;
  1922. dstY=0;
  1923. lastInLumBuf= -1;
  1924. lastInChrBuf= -1;
  1925. }
  1926. lastDstY= dstY;
  1927. for (;dstY < dstH; dstY++) {
  1928. unsigned char *dest =dst[0]+dstStride[0]*dstY;
  1929. const int chrDstY= dstY>>c->chrDstVSubSample;
  1930. unsigned char *uDest=dst[1]+dstStride[1]*chrDstY;
  1931. unsigned char *vDest=dst[2]+dstStride[2]*chrDstY;
  1932. unsigned char *aDest=(CONFIG_SWSCALE_ALPHA && alpPixBuf) ? dst[3]+dstStride[3]*dstY : NULL;
  1933. const int firstLumSrcY= vLumFilterPos[dstY]; //First line needed as input
  1934. const int firstLumSrcY2= vLumFilterPos[FFMIN(dstY | ((1<<c->chrDstVSubSample) - 1), dstH-1)];
  1935. const int firstChrSrcY= vChrFilterPos[chrDstY]; //First line needed as input
  1936. int lastLumSrcY= firstLumSrcY + vLumFilterSize -1; // Last line needed as input
  1937. int lastLumSrcY2=firstLumSrcY2+ vLumFilterSize -1; // Last line needed as input
  1938. int lastChrSrcY= firstChrSrcY + vChrFilterSize -1; // Last line needed as input
  1939. int enough_lines;
  1940. //handle holes (FAST_BILINEAR & weird filters)
  1941. if (firstLumSrcY > lastInLumBuf) lastInLumBuf= firstLumSrcY-1;
  1942. if (firstChrSrcY > lastInChrBuf) lastInChrBuf= firstChrSrcY-1;
  1943. assert(firstLumSrcY >= lastInLumBuf - vLumBufSize + 1);
  1944. assert(firstChrSrcY >= lastInChrBuf - vChrBufSize + 1);
  1945. DEBUG_BUFFERS("dstY: %d\n", dstY);
  1946. DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n",
  1947. firstLumSrcY, lastLumSrcY, lastInLumBuf);
  1948. DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n",
  1949. firstChrSrcY, lastChrSrcY, lastInChrBuf);
  1950. // Do we have enough lines in this slice to output the dstY line
  1951. enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH && lastChrSrcY < -((-srcSliceY - srcSliceH)>>c->chrSrcVSubSample);
  1952. if (!enough_lines) {
  1953. lastLumSrcY = srcSliceY + srcSliceH - 1;
  1954. lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1;
  1955. DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n",
  1956. lastLumSrcY, lastChrSrcY);
  1957. }
  1958. //Do horizontal scaling
  1959. while(lastInLumBuf < lastLumSrcY) {
  1960. const uint8_t *src1= src[0]+(lastInLumBuf + 1 - srcSliceY)*srcStride[0];
  1961. const uint8_t *src2= src[3]+(lastInLumBuf + 1 - srcSliceY)*srcStride[3];
  1962. lumBufIndex++;
  1963. assert(lumBufIndex < 2*vLumBufSize);
  1964. assert(lastInLumBuf + 1 - srcSliceY < srcSliceH);
  1965. assert(lastInLumBuf + 1 - srcSliceY >= 0);
  1966. hyscale(c, lumPixBuf[ lumBufIndex ], dstW, src1, srcW, lumXInc,
  1967. hLumFilter, hLumFilterPos, hLumFilterSize,
  1968. formatConvBuffer,
  1969. pal, 0);
  1970. if (CONFIG_SWSCALE_ALPHA && alpPixBuf)
  1971. hyscale(c, alpPixBuf[ lumBufIndex ], dstW, src2, srcW,
  1972. lumXInc, hLumFilter, hLumFilterPos, hLumFilterSize,
  1973. formatConvBuffer,
  1974. pal, 1);
  1975. lastInLumBuf++;
  1976. DEBUG_BUFFERS("\t\tlumBufIndex %d: lastInLumBuf: %d\n",
  1977. lumBufIndex, lastInLumBuf);
  1978. }
  1979. while(lastInChrBuf < lastChrSrcY) {
  1980. const uint8_t *src1= src[1]+(lastInChrBuf + 1 - chrSrcSliceY)*srcStride[1];
  1981. const uint8_t *src2= src[2]+(lastInChrBuf + 1 - chrSrcSliceY)*srcStride[2];
  1982. chrBufIndex++;
  1983. assert(chrBufIndex < 2*vChrBufSize);
  1984. assert(lastInChrBuf + 1 - chrSrcSliceY < (chrSrcSliceH));
  1985. assert(lastInChrBuf + 1 - chrSrcSliceY >= 0);
  1986. //FIXME replace parameters through context struct (some at least)
  1987. if (c->needs_hcscale)
  1988. hcscale(c, chrUPixBuf[chrBufIndex], chrVPixBuf[chrBufIndex],
  1989. chrDstW, src1, src2, chrSrcW, chrXInc,
  1990. hChrFilter, hChrFilterPos, hChrFilterSize,
  1991. formatConvBuffer, pal);
  1992. lastInChrBuf++;
  1993. DEBUG_BUFFERS("\t\tchrBufIndex %d: lastInChrBuf: %d\n",
  1994. chrBufIndex, lastInChrBuf);
  1995. }
  1996. //wrap buf index around to stay inside the ring buffer
  1997. if (lumBufIndex >= vLumBufSize) lumBufIndex-= vLumBufSize;
  1998. if (chrBufIndex >= vChrBufSize) chrBufIndex-= vChrBufSize;
  1999. if (!enough_lines)
  2000. break; //we can't output a dstY line so let's try with the next slice
  2001. #if HAVE_MMX
  2002. updateMMXDitherTables(c, dstY, lumBufIndex, chrBufIndex, lastInLumBuf, lastInChrBuf);
  2003. #endif
  2004. if (dstY >= dstH-2) {
  2005. // hmm looks like we can't use MMX here without overwriting this array's tail
  2006. find_c_packed_planar_out_funcs(c, &yuv2yuv1, &yuv2yuvX,
  2007. &yuv2packed1, &yuv2packed2,
  2008. &yuv2packedX);
  2009. }
  2010. {
  2011. const int16_t **lumSrcPtr= (const int16_t **) lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
  2012. const int16_t **chrUSrcPtr= (const int16_t **) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  2013. const int16_t **chrVSrcPtr= (const int16_t **) chrVPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  2014. const int16_t **alpSrcPtr= (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? (const int16_t **) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
  2015. if (isPlanarYUV(dstFormat) || dstFormat==PIX_FMT_GRAY8) { //YV12 like
  2016. const int chrSkipMask= (1<<c->chrDstVSubSample)-1;
  2017. if ((dstY&chrSkipMask) || isGray(dstFormat)) uDest=vDest= NULL; //FIXME split functions in lumi / chromi
  2018. if (c->yuv2yuv1 && vLumFilterSize == 1 && vChrFilterSize == 1) { // unscaled YV12
  2019. const int16_t *lumBuf = lumSrcPtr[0];
  2020. const int16_t *chrUBuf= chrUSrcPtr[0];
  2021. const int16_t *chrVBuf= chrVSrcPtr[0];
  2022. const int16_t *alpBuf= (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? alpSrcPtr[0] : NULL;
  2023. yuv2yuv1(c, lumBuf, chrUBuf, chrVBuf, alpBuf, dest,
  2024. uDest, vDest, aDest, dstW, chrDstW);
  2025. } else { //General YV12
  2026. yuv2yuvX(c,
  2027. vLumFilter+dstY*vLumFilterSize , lumSrcPtr, vLumFilterSize,
  2028. vChrFilter+chrDstY*vChrFilterSize, chrUSrcPtr,
  2029. chrVSrcPtr, vChrFilterSize,
  2030. alpSrcPtr, dest, uDest, vDest, aDest, dstW, chrDstW);
  2031. }
  2032. } else {
  2033. assert(lumSrcPtr + vLumFilterSize - 1 < lumPixBuf + vLumBufSize*2);
  2034. assert(chrUSrcPtr + vChrFilterSize - 1 < chrUPixBuf + vChrBufSize*2);
  2035. if (c->yuv2packed1 && vLumFilterSize == 1 && vChrFilterSize == 2) { //unscaled RGB
  2036. int chrAlpha= vChrFilter[2*dstY+1];
  2037. yuv2packed1(c, *lumSrcPtr, *chrUSrcPtr, *(chrUSrcPtr+1),
  2038. *chrVSrcPtr, *(chrVSrcPtr+1),
  2039. alpPixBuf ? *alpSrcPtr : NULL,
  2040. dest, dstW, chrAlpha, dstFormat, flags, dstY);
  2041. } else if (c->yuv2packed2 && vLumFilterSize == 2 && vChrFilterSize == 2) { //bilinear upscale RGB
  2042. int lumAlpha= vLumFilter[2*dstY+1];
  2043. int chrAlpha= vChrFilter[2*dstY+1];
  2044. lumMmxFilter[2]=
  2045. lumMmxFilter[3]= vLumFilter[2*dstY ]*0x10001;
  2046. chrMmxFilter[2]=
  2047. chrMmxFilter[3]= vChrFilter[2*chrDstY]*0x10001;
  2048. yuv2packed2(c, *lumSrcPtr, *(lumSrcPtr+1), *chrUSrcPtr, *(chrUSrcPtr+1),
  2049. *chrVSrcPtr, *(chrVSrcPtr+1),
  2050. alpPixBuf ? *alpSrcPtr : NULL, alpPixBuf ? *(alpSrcPtr+1) : NULL,
  2051. dest, dstW, lumAlpha, chrAlpha, dstY);
  2052. } else { //general RGB
  2053. yuv2packedX(c,
  2054. vLumFilter+dstY*vLumFilterSize, lumSrcPtr, vLumFilterSize,
  2055. vChrFilter+dstY*vChrFilterSize, chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  2056. alpSrcPtr, dest, dstW, dstY);
  2057. }
  2058. }
  2059. }
  2060. }
  2061. if ((dstFormat == PIX_FMT_YUVA420P) && !alpPixBuf)
  2062. fillPlane(dst[3], dstStride[3], dstW, dstY-lastDstY, lastDstY, 255);
  2063. #if HAVE_MMX2
  2064. if (av_get_cpu_flags() & AV_CPU_FLAG_MMX2)
  2065. __asm__ volatile("sfence":::"memory");
  2066. #endif
  2067. emms_c();
  2068. /* store changed local vars back in the context */
  2069. c->dstY= dstY;
  2070. c->lumBufIndex= lumBufIndex;
  2071. c->chrBufIndex= chrBufIndex;
  2072. c->lastInLumBuf= lastInLumBuf;
  2073. c->lastInChrBuf= lastInChrBuf;
  2074. return dstY - lastDstY;
  2075. }
  2076. static av_cold void sws_init_swScale_c(SwsContext *c)
  2077. {
  2078. enum PixelFormat srcFormat = c->srcFormat;
  2079. find_c_packed_planar_out_funcs(c, &c->yuv2yuv1, &c->yuv2yuvX,
  2080. &c->yuv2packed1, &c->yuv2packed2,
  2081. &c->yuv2packedX);
  2082. c->hScale = hScale_c;
  2083. if (c->flags & SWS_FAST_BILINEAR) {
  2084. c->hyscale_fast = hyscale_fast_c;
  2085. c->hcscale_fast = hcscale_fast_c;
  2086. }
  2087. c->chrToYV12 = NULL;
  2088. switch(srcFormat) {
  2089. case PIX_FMT_YUYV422 : c->chrToYV12 = yuy2ToUV_c; break;
  2090. case PIX_FMT_UYVY422 : c->chrToYV12 = uyvyToUV_c; break;
  2091. case PIX_FMT_NV12 : c->chrToYV12 = nv12ToUV_c; break;
  2092. case PIX_FMT_NV21 : c->chrToYV12 = nv21ToUV_c; break;
  2093. case PIX_FMT_RGB8 :
  2094. case PIX_FMT_BGR8 :
  2095. case PIX_FMT_PAL8 :
  2096. case PIX_FMT_BGR4_BYTE:
  2097. case PIX_FMT_RGB4_BYTE: c->chrToYV12 = palToUV_c; break;
  2098. case PIX_FMT_YUV444P9BE:
  2099. case PIX_FMT_YUV420P9BE: c->chrToYV12 = BE9ToUV_c; break;
  2100. case PIX_FMT_YUV444P9LE:
  2101. case PIX_FMT_YUV420P9LE: c->chrToYV12 = LE9ToUV_c; break;
  2102. case PIX_FMT_YUV444P10BE:
  2103. case PIX_FMT_YUV422P10BE:
  2104. case PIX_FMT_YUV420P10BE: c->chrToYV12 = BE10ToUV_c; break;
  2105. case PIX_FMT_YUV422P10LE:
  2106. case PIX_FMT_YUV444P10LE:
  2107. case PIX_FMT_YUV420P10LE: c->chrToYV12 = LE10ToUV_c; break;
  2108. case PIX_FMT_YUV420P16BE:
  2109. case PIX_FMT_YUV422P16BE:
  2110. case PIX_FMT_YUV444P16BE: c->chrToYV12 = BEToUV_c; break;
  2111. case PIX_FMT_YUV420P16LE:
  2112. case PIX_FMT_YUV422P16LE:
  2113. case PIX_FMT_YUV444P16LE: c->chrToYV12 = LEToUV_c; break;
  2114. }
  2115. if (c->chrSrcHSubSample) {
  2116. switch(srcFormat) {
  2117. case PIX_FMT_RGB48BE : c->chrToYV12 = rgb48BEToUV_half_c; break;
  2118. case PIX_FMT_RGB48LE : c->chrToYV12 = rgb48LEToUV_half_c; break;
  2119. case PIX_FMT_BGR48BE : c->chrToYV12 = bgr48BEToUV_half_c; break;
  2120. case PIX_FMT_BGR48LE : c->chrToYV12 = bgr48LEToUV_half_c; break;
  2121. case PIX_FMT_RGB32 : c->chrToYV12 = bgr32ToUV_half_c; break;
  2122. case PIX_FMT_RGB32_1 : c->chrToYV12 = bgr321ToUV_half_c; break;
  2123. case PIX_FMT_BGR24 : c->chrToYV12 = bgr24ToUV_half_c; break;
  2124. case PIX_FMT_BGR565LE: c->chrToYV12 = bgr16leToUV_half_c; break;
  2125. case PIX_FMT_BGR565BE: c->chrToYV12 = bgr16beToUV_half_c; break;
  2126. case PIX_FMT_BGR555LE: c->chrToYV12 = bgr15leToUV_half_c; break;
  2127. case PIX_FMT_BGR555BE: c->chrToYV12 = bgr15beToUV_half_c; break;
  2128. case PIX_FMT_BGR32 : c->chrToYV12 = rgb32ToUV_half_c; break;
  2129. case PIX_FMT_BGR32_1 : c->chrToYV12 = rgb321ToUV_half_c; break;
  2130. case PIX_FMT_RGB24 : c->chrToYV12 = rgb24ToUV_half_c; break;
  2131. case PIX_FMT_RGB565LE: c->chrToYV12 = rgb16leToUV_half_c; break;
  2132. case PIX_FMT_RGB565BE: c->chrToYV12 = rgb16beToUV_half_c; break;
  2133. case PIX_FMT_RGB555LE: c->chrToYV12 = rgb15leToUV_half_c; break;
  2134. case PIX_FMT_RGB555BE: c->chrToYV12 = rgb15beToUV_half_c; break;
  2135. }
  2136. } else {
  2137. switch(srcFormat) {
  2138. case PIX_FMT_RGB48BE : c->chrToYV12 = rgb48BEToUV_c; break;
  2139. case PIX_FMT_RGB48LE : c->chrToYV12 = rgb48LEToUV_c; break;
  2140. case PIX_FMT_BGR48BE : c->chrToYV12 = bgr48BEToUV_c; break;
  2141. case PIX_FMT_BGR48LE : c->chrToYV12 = bgr48LEToUV_c; break;
  2142. case PIX_FMT_RGB32 : c->chrToYV12 = bgr32ToUV_c; break;
  2143. case PIX_FMT_RGB32_1 : c->chrToYV12 = bgr321ToUV_c; break;
  2144. case PIX_FMT_BGR24 : c->chrToYV12 = bgr24ToUV_c; break;
  2145. case PIX_FMT_BGR565LE: c->chrToYV12 = bgr16leToUV_c; break;
  2146. case PIX_FMT_BGR565BE: c->chrToYV12 = bgr16beToUV_c; break;
  2147. case PIX_FMT_BGR555LE: c->chrToYV12 = bgr15leToUV_c; break;
  2148. case PIX_FMT_BGR555BE: c->chrToYV12 = bgr15beToUV_c; break;
  2149. case PIX_FMT_BGR32 : c->chrToYV12 = rgb32ToUV_c; break;
  2150. case PIX_FMT_BGR32_1 : c->chrToYV12 = rgb321ToUV_c; break;
  2151. case PIX_FMT_RGB24 : c->chrToYV12 = rgb24ToUV_c; break;
  2152. case PIX_FMT_RGB565LE: c->chrToYV12 = rgb16leToUV_c; break;
  2153. case PIX_FMT_RGB565BE: c->chrToYV12 = rgb16beToUV_c; break;
  2154. case PIX_FMT_RGB555LE: c->chrToYV12 = rgb15leToUV_c; break;
  2155. case PIX_FMT_RGB555BE: c->chrToYV12 = rgb15beToUV_c; break;
  2156. }
  2157. }
  2158. c->lumToYV12 = NULL;
  2159. c->alpToYV12 = NULL;
  2160. switch (srcFormat) {
  2161. case PIX_FMT_YUV444P9BE:
  2162. case PIX_FMT_YUV420P9BE: c->lumToYV12 = BE9ToY_c; break;
  2163. case PIX_FMT_YUV444P9LE:
  2164. case PIX_FMT_YUV420P9LE: c->lumToYV12 = LE9ToY_c; break;
  2165. case PIX_FMT_YUV444P10BE:
  2166. case PIX_FMT_YUV422P10BE:
  2167. case PIX_FMT_YUV420P10BE: c->lumToYV12 = BE10ToY_c; break;
  2168. case PIX_FMT_YUV444P10LE:
  2169. case PIX_FMT_YUV422P10LE:
  2170. case PIX_FMT_YUV420P10LE: c->lumToYV12 = LE10ToY_c; break;
  2171. case PIX_FMT_YUYV422 :
  2172. case PIX_FMT_YUV420P16BE:
  2173. case PIX_FMT_YUV422P16BE:
  2174. case PIX_FMT_YUV444P16BE:
  2175. case PIX_FMT_Y400A :
  2176. case PIX_FMT_GRAY16BE : c->lumToYV12 = yuy2ToY_c; break;
  2177. case PIX_FMT_UYVY422 :
  2178. case PIX_FMT_YUV420P16LE:
  2179. case PIX_FMT_YUV422P16LE:
  2180. case PIX_FMT_YUV444P16LE:
  2181. case PIX_FMT_GRAY16LE : c->lumToYV12 = uyvyToY_c; break;
  2182. case PIX_FMT_BGR24 : c->lumToYV12 = bgr24ToY_c; break;
  2183. case PIX_FMT_BGR565LE : c->lumToYV12 = bgr16leToY_c; break;
  2184. case PIX_FMT_BGR565BE : c->lumToYV12 = bgr16beToY_c; break;
  2185. case PIX_FMT_BGR555LE : c->lumToYV12 = bgr15leToY_c; break;
  2186. case PIX_FMT_BGR555BE : c->lumToYV12 = bgr15beToY_c; break;
  2187. case PIX_FMT_RGB24 : c->lumToYV12 = rgb24ToY_c; break;
  2188. case PIX_FMT_RGB565LE : c->lumToYV12 = rgb16leToY_c; break;
  2189. case PIX_FMT_RGB565BE : c->lumToYV12 = rgb16beToY_c; break;
  2190. case PIX_FMT_RGB555LE : c->lumToYV12 = rgb15leToY_c; break;
  2191. case PIX_FMT_RGB555BE : c->lumToYV12 = rgb15beToY_c; break;
  2192. case PIX_FMT_RGB8 :
  2193. case PIX_FMT_BGR8 :
  2194. case PIX_FMT_PAL8 :
  2195. case PIX_FMT_BGR4_BYTE:
  2196. case PIX_FMT_RGB4_BYTE: c->lumToYV12 = palToY_c; break;
  2197. case PIX_FMT_MONOBLACK: c->lumToYV12 = monoblack2Y_c; break;
  2198. case PIX_FMT_MONOWHITE: c->lumToYV12 = monowhite2Y_c; break;
  2199. case PIX_FMT_RGB32 : c->lumToYV12 = bgr32ToY_c; break;
  2200. case PIX_FMT_RGB32_1: c->lumToYV12 = bgr321ToY_c; break;
  2201. case PIX_FMT_BGR32 : c->lumToYV12 = rgb32ToY_c; break;
  2202. case PIX_FMT_BGR32_1: c->lumToYV12 = rgb321ToY_c; break;
  2203. case PIX_FMT_RGB48BE: c->lumToYV12 = rgb48BEToY_c; break;
  2204. case PIX_FMT_RGB48LE: c->lumToYV12 = rgb48LEToY_c; break;
  2205. case PIX_FMT_BGR48BE: c->lumToYV12 = bgr48BEToY_c; break;
  2206. case PIX_FMT_BGR48LE: c->lumToYV12 = bgr48LEToY_c; break;
  2207. }
  2208. if (c->alpPixBuf) {
  2209. switch (srcFormat) {
  2210. case PIX_FMT_BGRA:
  2211. case PIX_FMT_RGBA: c->alpToYV12 = rgbaToA_c; break;
  2212. case PIX_FMT_ABGR:
  2213. case PIX_FMT_ARGB: c->alpToYV12 = abgrToA_c; break;
  2214. case PIX_FMT_Y400A: c->alpToYV12 = uyvyToY_c; break;
  2215. }
  2216. }
  2217. if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
  2218. if (c->srcRange) {
  2219. c->lumConvertRange = lumRangeFromJpeg_c;
  2220. c->chrConvertRange = chrRangeFromJpeg_c;
  2221. } else {
  2222. c->lumConvertRange = lumRangeToJpeg_c;
  2223. c->chrConvertRange = chrRangeToJpeg_c;
  2224. }
  2225. }
  2226. if (!(isGray(srcFormat) || isGray(c->dstFormat) ||
  2227. srcFormat == PIX_FMT_MONOBLACK || srcFormat == PIX_FMT_MONOWHITE))
  2228. c->needs_hcscale = 1;
  2229. }
  2230. SwsFunc ff_getSwsFunc(SwsContext *c)
  2231. {
  2232. sws_init_swScale_c(c);
  2233. if (HAVE_MMX)
  2234. ff_sws_init_swScale_mmx(c);
  2235. if (HAVE_ALTIVEC)
  2236. ff_sws_init_swScale_altivec(c);
  2237. return swScale;
  2238. }