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.

2091 lines
78KB

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