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.

2164 lines
77KB

  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/x86_cpu.h"
  60. #include "libavutil/avutil.h"
  61. #include "libavutil/mathematics.h"
  62. #include "libavutil/bswap.h"
  63. #include "libavutil/pixdesc.h"
  64. #undef MOVNTQ
  65. #undef PAVGB
  66. //#undef HAVE_MMX2
  67. //#define HAVE_AMD3DNOW
  68. //#undef HAVE_MMX
  69. //#undef ARCH_X86
  70. #define DITHER1XBPP
  71. #define isPacked(x) ( \
  72. (x)==PIX_FMT_PAL8 \
  73. || (x)==PIX_FMT_YUYV422 \
  74. || (x)==PIX_FMT_UYVY422 \
  75. || (x)==PIX_FMT_Y400A \
  76. || isAnyRGB(x) \
  77. )
  78. #define RGB2YUV_SHIFT 15
  79. #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  80. #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  81. #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  82. #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  83. #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  84. #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  85. #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  86. #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  87. #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  88. static const double rgb2yuv_table[8][9]={
  89. {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
  90. {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
  91. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  92. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  93. {0.59 , 0.11 , 0.30 , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
  94. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  95. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  96. {0.701 , 0.087 , 0.212 , -0.384, 0.5, -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
  97. };
  98. /*
  99. NOTES
  100. Special versions: fast Y 1:1 scaling (no interpolation in y direction)
  101. TODO
  102. more intelligent misalignment avoidance for the horizontal scaler
  103. write special vertical cubic upscale version
  104. optimize C code (YV12 / minmax)
  105. add support for packed pixel YUV input & output
  106. add support for Y8 output
  107. optimize BGR24 & BGR32
  108. add BGR4 output support
  109. write special BGR->BGR scaler
  110. */
  111. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
  112. { 1, 3, 1, 3, 1, 3, 1, 3, },
  113. { 2, 0, 2, 0, 2, 0, 2, 0, },
  114. };
  115. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
  116. { 6, 2, 6, 2, 6, 2, 6, 2, },
  117. { 0, 4, 0, 4, 0, 4, 0, 4, },
  118. };
  119. DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[4][8]={
  120. { 8, 4, 11, 7, 8, 4, 11, 7, },
  121. { 2, 14, 1, 13, 2, 14, 1, 13, },
  122. { 10, 6, 9, 5, 10, 6, 9, 5, },
  123. { 0, 12, 3, 15, 0, 12, 3, 15, },
  124. };
  125. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
  126. { 17, 9, 23, 15, 16, 8, 22, 14, },
  127. { 5, 29, 3, 27, 4, 28, 2, 26, },
  128. { 21, 13, 19, 11, 20, 12, 18, 10, },
  129. { 0, 24, 6, 30, 1, 25, 7, 31, },
  130. { 16, 8, 22, 14, 17, 9, 23, 15, },
  131. { 4, 28, 2, 26, 5, 29, 3, 27, },
  132. { 20, 12, 18, 10, 21, 13, 19, 11, },
  133. { 1, 25, 7, 31, 0, 24, 6, 30, },
  134. };
  135. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
  136. { 0, 55, 14, 68, 3, 58, 17, 72, },
  137. { 37, 18, 50, 32, 40, 22, 54, 35, },
  138. { 9, 64, 5, 59, 13, 67, 8, 63, },
  139. { 46, 27, 41, 23, 49, 31, 44, 26, },
  140. { 2, 57, 16, 71, 1, 56, 15, 70, },
  141. { 39, 21, 52, 34, 38, 19, 51, 33, },
  142. { 11, 66, 7, 62, 10, 65, 6, 60, },
  143. { 48, 30, 43, 25, 47, 29, 42, 24, },
  144. };
  145. #if 1
  146. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  147. {117, 62, 158, 103, 113, 58, 155, 100, },
  148. { 34, 199, 21, 186, 31, 196, 17, 182, },
  149. {144, 89, 131, 76, 141, 86, 127, 72, },
  150. { 0, 165, 41, 206, 10, 175, 52, 217, },
  151. {110, 55, 151, 96, 120, 65, 162, 107, },
  152. { 28, 193, 14, 179, 38, 203, 24, 189, },
  153. {138, 83, 124, 69, 148, 93, 134, 79, },
  154. { 7, 172, 48, 213, 3, 168, 45, 210, },
  155. };
  156. #elif 1
  157. // tries to correct a gamma of 1.5
  158. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  159. { 0, 143, 18, 200, 2, 156, 25, 215, },
  160. { 78, 28, 125, 64, 89, 36, 138, 74, },
  161. { 10, 180, 3, 161, 16, 195, 8, 175, },
  162. {109, 51, 93, 38, 121, 60, 105, 47, },
  163. { 1, 152, 23, 210, 0, 147, 20, 205, },
  164. { 85, 33, 134, 71, 81, 30, 130, 67, },
  165. { 14, 190, 6, 171, 12, 185, 5, 166, },
  166. {117, 57, 101, 44, 113, 54, 97, 41, },
  167. };
  168. #elif 1
  169. // tries to correct a gamma of 2.0
  170. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  171. { 0, 124, 8, 193, 0, 140, 12, 213, },
  172. { 55, 14, 104, 42, 66, 19, 119, 52, },
  173. { 3, 168, 1, 145, 6, 187, 3, 162, },
  174. { 86, 31, 70, 21, 99, 39, 82, 28, },
  175. { 0, 134, 11, 206, 0, 129, 9, 200, },
  176. { 62, 17, 114, 48, 58, 16, 109, 45, },
  177. { 5, 181, 2, 157, 4, 175, 1, 151, },
  178. { 95, 36, 78, 26, 90, 34, 74, 24, },
  179. };
  180. #else
  181. // tries to correct a gamma of 2.5
  182. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  183. { 0, 107, 3, 187, 0, 125, 6, 212, },
  184. { 39, 7, 86, 28, 49, 11, 102, 36, },
  185. { 1, 158, 0, 131, 3, 180, 1, 151, },
  186. { 68, 19, 52, 12, 81, 25, 64, 17, },
  187. { 0, 119, 5, 203, 0, 113, 4, 195, },
  188. { 45, 9, 96, 33, 42, 8, 91, 30, },
  189. { 2, 172, 1, 144, 2, 165, 0, 137, },
  190. { 77, 23, 60, 15, 72, 21, 56, 14, },
  191. };
  192. #endif
  193. static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  194. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  195. const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
  196. int dstW, int chrDstW, int big_endian, int output_bits)
  197. {
  198. //FIXME Optimize (just quickly written not optimized..)
  199. int i;
  200. int shift = 11 + 16 - output_bits;
  201. for (i = 0; i < dstW; i++) {
  202. int val = 1 << 10;
  203. int j;
  204. for (j = 0; j < lumFilterSize; j++)
  205. val += lumSrc[j][i] * lumFilter[j];
  206. if (big_endian) {
  207. AV_WB16(&dest[i], av_clip_uint16(val >> shift));
  208. } else {
  209. AV_WL16(&dest[i], av_clip_uint16(val >> shift));
  210. }
  211. }
  212. if (uDest) {
  213. for (i = 0; i < chrDstW; i++) {
  214. int u = 1 << 10;
  215. int v = 1 << 10;
  216. int j;
  217. for (j = 0; j < chrFilterSize; j++) {
  218. u += chrSrc[j][i ] * chrFilter[j];
  219. v += chrSrc[j][i + VOFW] * chrFilter[j];
  220. }
  221. if (big_endian) {
  222. AV_WB16(&uDest[i], av_clip_uint16(u >> shift));
  223. AV_WB16(&vDest[i], av_clip_uint16(v >> shift));
  224. } else {
  225. AV_WL16(&uDest[i], av_clip_uint16(u >> shift));
  226. AV_WL16(&vDest[i], av_clip_uint16(v >> shift));
  227. }
  228. }
  229. }
  230. if (CONFIG_SWSCALE_ALPHA && aDest) {
  231. for (i = 0; i < dstW; i++) {
  232. int val = 1 << 10;
  233. int j;
  234. for (j = 0; j < lumFilterSize; j++)
  235. val += alpSrc[j][i] * lumFilter[j];
  236. if (big_endian) {
  237. AV_WB16(&aDest[i], av_clip_uint16(val >> shift));
  238. } else {
  239. AV_WL16(&aDest[i], av_clip_uint16(val >> shift));
  240. }
  241. }
  242. }
  243. }
  244. static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  245. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  246. const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
  247. enum PixelFormat dstFormat)
  248. {
  249. #define conv16(bits) \
  250. if (isBE(dstFormat)) { \
  251. yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize, \
  252. chrFilter, chrSrc, chrFilterSize, \
  253. alpSrc, \
  254. dest, uDest, vDest, aDest, \
  255. dstW, chrDstW, 1, bits); \
  256. } else { \
  257. yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize, \
  258. chrFilter, chrSrc, chrFilterSize, \
  259. alpSrc, \
  260. dest, uDest, vDest, aDest, \
  261. dstW, chrDstW, 0, bits); \
  262. }
  263. if (is16BPS(dstFormat)) {
  264. conv16(16);
  265. } else if (av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1 == 8) {
  266. conv16(9);
  267. } else {
  268. conv16(10);
  269. }
  270. #undef conv16
  271. }
  272. static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  273. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  274. const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
  275. {
  276. //FIXME Optimize (just quickly written not optimized..)
  277. int i;
  278. for (i=0; i<dstW; i++) {
  279. int val=1<<18;
  280. int j;
  281. for (j=0; j<lumFilterSize; j++)
  282. val += lumSrc[j][i] * lumFilter[j];
  283. dest[i]= av_clip_uint8(val>>19);
  284. }
  285. if (uDest)
  286. for (i=0; i<chrDstW; i++) {
  287. int u=1<<18;
  288. int v=1<<18;
  289. int j;
  290. for (j=0; j<chrFilterSize; j++) {
  291. u += chrSrc[j][i] * chrFilter[j];
  292. v += chrSrc[j][i + VOFW] * chrFilter[j];
  293. }
  294. uDest[i]= av_clip_uint8(u>>19);
  295. vDest[i]= av_clip_uint8(v>>19);
  296. }
  297. if (CONFIG_SWSCALE_ALPHA && aDest)
  298. for (i=0; i<dstW; i++) {
  299. int val=1<<18;
  300. int j;
  301. for (j=0; j<lumFilterSize; j++)
  302. val += alpSrc[j][i] * lumFilter[j];
  303. aDest[i]= av_clip_uint8(val>>19);
  304. }
  305. }
  306. static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  307. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  308. uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
  309. {
  310. //FIXME Optimize (just quickly written not optimized..)
  311. int i;
  312. for (i=0; i<dstW; i++) {
  313. int val=1<<18;
  314. int j;
  315. for (j=0; j<lumFilterSize; j++)
  316. val += lumSrc[j][i] * lumFilter[j];
  317. dest[i]= av_clip_uint8(val>>19);
  318. }
  319. if (!uDest)
  320. return;
  321. if (dstFormat == PIX_FMT_NV12)
  322. for (i=0; i<chrDstW; i++) {
  323. int u=1<<18;
  324. int v=1<<18;
  325. int j;
  326. for (j=0; j<chrFilterSize; j++) {
  327. u += chrSrc[j][i] * chrFilter[j];
  328. v += chrSrc[j][i + VOFW] * chrFilter[j];
  329. }
  330. uDest[2*i]= av_clip_uint8(u>>19);
  331. uDest[2*i+1]= av_clip_uint8(v>>19);
  332. }
  333. else
  334. for (i=0; i<chrDstW; i++) {
  335. int u=1<<18;
  336. int v=1<<18;
  337. int j;
  338. for (j=0; j<chrFilterSize; j++) {
  339. u += chrSrc[j][i] * chrFilter[j];
  340. v += chrSrc[j][i + VOFW] * chrFilter[j];
  341. }
  342. uDest[2*i]= av_clip_uint8(v>>19);
  343. uDest[2*i+1]= av_clip_uint8(u>>19);
  344. }
  345. }
  346. #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
  347. for (i=0; i<(dstW>>1); i++) {\
  348. int j;\
  349. int Y1 = 1<<18;\
  350. int Y2 = 1<<18;\
  351. int U = 1<<18;\
  352. int V = 1<<18;\
  353. int av_unused A1, A2;\
  354. type av_unused *r, *b, *g;\
  355. const int i2= 2*i;\
  356. \
  357. for (j=0; j<lumFilterSize; j++) {\
  358. Y1 += lumSrc[j][i2] * lumFilter[j];\
  359. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  360. }\
  361. for (j=0; j<chrFilterSize; j++) {\
  362. U += chrSrc[j][i] * chrFilter[j];\
  363. V += chrSrc[j][i+VOFW] * chrFilter[j];\
  364. }\
  365. Y1>>=19;\
  366. Y2>>=19;\
  367. U >>=19;\
  368. V >>=19;\
  369. if (alpha) {\
  370. A1 = 1<<18;\
  371. A2 = 1<<18;\
  372. for (j=0; j<lumFilterSize; j++) {\
  373. A1 += alpSrc[j][i2 ] * lumFilter[j];\
  374. A2 += alpSrc[j][i2+1] * lumFilter[j];\
  375. }\
  376. A1>>=19;\
  377. A2>>=19;\
  378. }
  379. #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
  380. YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
  381. if ((Y1|Y2|U|V)&256) {\
  382. if (Y1>255) Y1=255; \
  383. else if (Y1<0)Y1=0; \
  384. if (Y2>255) Y2=255; \
  385. else if (Y2<0)Y2=0; \
  386. if (U>255) U=255; \
  387. else if (U<0) U=0; \
  388. if (V>255) V=255; \
  389. else if (V<0) V=0; \
  390. }\
  391. if (alpha && ((A1|A2)&256)) {\
  392. A1=av_clip_uint8(A1);\
  393. A2=av_clip_uint8(A2);\
  394. }
  395. #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
  396. for (i=0; i<dstW; i++) {\
  397. int j;\
  398. int Y = 0;\
  399. int U = -128<<19;\
  400. int V = -128<<19;\
  401. int av_unused A;\
  402. int R,G,B;\
  403. \
  404. for (j=0; j<lumFilterSize; j++) {\
  405. Y += lumSrc[j][i ] * lumFilter[j];\
  406. }\
  407. for (j=0; j<chrFilterSize; j++) {\
  408. U += chrSrc[j][i ] * chrFilter[j];\
  409. V += chrSrc[j][i+VOFW] * chrFilter[j];\
  410. }\
  411. Y >>=10;\
  412. U >>=10;\
  413. V >>=10;\
  414. if (alpha) {\
  415. A = rnd;\
  416. for (j=0; j<lumFilterSize; j++)\
  417. A += alpSrc[j][i ] * lumFilter[j];\
  418. A >>=19;\
  419. if (A&256)\
  420. A = av_clip_uint8(A);\
  421. }
  422. #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
  423. YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
  424. Y-= c->yuv2rgb_y_offset;\
  425. Y*= c->yuv2rgb_y_coeff;\
  426. Y+= rnd;\
  427. R= Y + V*c->yuv2rgb_v2r_coeff;\
  428. G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
  429. B= Y + U*c->yuv2rgb_u2b_coeff;\
  430. if ((R|G|B)&(0xC0000000)) {\
  431. if (R>=(256<<22)) R=(256<<22)-1; \
  432. else if (R<0)R=0; \
  433. if (G>=(256<<22)) G=(256<<22)-1; \
  434. else if (G<0)G=0; \
  435. if (B>=(256<<22)) B=(256<<22)-1; \
  436. else if (B<0)B=0; \
  437. }
  438. #define YSCALE_YUV_2_GRAY16_C \
  439. for (i=0; i<(dstW>>1); i++) {\
  440. int j;\
  441. int Y1 = 1<<18;\
  442. int Y2 = 1<<18;\
  443. int U = 1<<18;\
  444. int V = 1<<18;\
  445. \
  446. const int i2= 2*i;\
  447. \
  448. for (j=0; j<lumFilterSize; j++) {\
  449. Y1 += lumSrc[j][i2] * lumFilter[j];\
  450. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  451. }\
  452. Y1>>=11;\
  453. Y2>>=11;\
  454. if ((Y1|Y2|U|V)&65536) {\
  455. if (Y1>65535) Y1=65535; \
  456. else if (Y1<0)Y1=0; \
  457. if (Y2>65535) Y2=65535; \
  458. else if (Y2<0)Y2=0; \
  459. }
  460. #define YSCALE_YUV_2_RGBX_C(type,alpha) \
  461. YSCALE_YUV_2_PACKEDX_C(type,alpha) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
  462. r = (type *)c->table_rV[V]; \
  463. g = (type *)(c->table_gU[U] + c->table_gV[V]); \
  464. b = (type *)c->table_bU[U];
  465. #define YSCALE_YUV_2_PACKED2_C(type,alpha) \
  466. for (i=0; i<(dstW>>1); i++) { \
  467. const int i2= 2*i; \
  468. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
  469. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
  470. int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
  471. int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
  472. type av_unused *r, *b, *g; \
  473. int av_unused A1, A2; \
  474. if (alpha) {\
  475. A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
  476. A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
  477. }
  478. #define YSCALE_YUV_2_GRAY16_2_C \
  479. for (i=0; i<(dstW>>1); i++) { \
  480. const int i2= 2*i; \
  481. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
  482. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;
  483. #define YSCALE_YUV_2_RGB2_C(type,alpha) \
  484. YSCALE_YUV_2_PACKED2_C(type,alpha)\
  485. r = (type *)c->table_rV[V];\
  486. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  487. b = (type *)c->table_bU[U];
  488. #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
  489. for (i=0; i<(dstW>>1); i++) {\
  490. const int i2= 2*i;\
  491. int Y1= buf0[i2 ]>>7;\
  492. int Y2= buf0[i2+1]>>7;\
  493. int U= (uvbuf1[i ])>>7;\
  494. int V= (uvbuf1[i+VOFW])>>7;\
  495. type av_unused *r, *b, *g;\
  496. int av_unused A1, A2;\
  497. if (alpha) {\
  498. A1= abuf0[i2 ]>>7;\
  499. A2= abuf0[i2+1]>>7;\
  500. }
  501. #define YSCALE_YUV_2_GRAY16_1_C \
  502. for (i=0; i<(dstW>>1); i++) {\
  503. const int i2= 2*i;\
  504. int Y1= buf0[i2 ]<<1;\
  505. int Y2= buf0[i2+1]<<1;
  506. #define YSCALE_YUV_2_RGB1_C(type,alpha) \
  507. YSCALE_YUV_2_PACKED1_C(type,alpha)\
  508. r = (type *)c->table_rV[V];\
  509. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  510. b = (type *)c->table_bU[U];
  511. #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
  512. for (i=0; i<(dstW>>1); i++) {\
  513. const int i2= 2*i;\
  514. int Y1= buf0[i2 ]>>7;\
  515. int Y2= buf0[i2+1]>>7;\
  516. int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
  517. int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
  518. type av_unused *r, *b, *g;\
  519. int av_unused A1, A2;\
  520. if (alpha) {\
  521. A1= abuf0[i2 ]>>7;\
  522. A2= abuf0[i2+1]>>7;\
  523. }
  524. #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
  525. YSCALE_YUV_2_PACKED1B_C(type,alpha)\
  526. r = (type *)c->table_rV[V];\
  527. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  528. b = (type *)c->table_bU[U];
  529. #define YSCALE_YUV_2_MONO2_C \
  530. const uint8_t * const d128=dither_8x8_220[y&7];\
  531. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  532. for (i=0; i<dstW-7; i+=8) {\
  533. int acc;\
  534. acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
  535. acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
  536. acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
  537. acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
  538. acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
  539. acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
  540. acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
  541. acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
  542. ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
  543. dest++;\
  544. }
  545. #define YSCALE_YUV_2_MONOX_C \
  546. const uint8_t * const d128=dither_8x8_220[y&7];\
  547. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  548. int acc=0;\
  549. for (i=0; i<dstW-1; i+=2) {\
  550. int j;\
  551. int Y1=1<<18;\
  552. int Y2=1<<18;\
  553. \
  554. for (j=0; j<lumFilterSize; j++) {\
  555. Y1 += lumSrc[j][i] * lumFilter[j];\
  556. Y2 += lumSrc[j][i+1] * lumFilter[j];\
  557. }\
  558. Y1>>=19;\
  559. Y2>>=19;\
  560. if ((Y1|Y2)&256) {\
  561. if (Y1>255) Y1=255;\
  562. else if (Y1<0)Y1=0;\
  563. if (Y2>255) Y2=255;\
  564. else if (Y2<0)Y2=0;\
  565. }\
  566. acc+= acc + g[Y1+d128[(i+0)&7]];\
  567. acc+= acc + g[Y2+d128[(i+1)&7]];\
  568. if ((i&7)==6) {\
  569. ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
  570. dest++;\
  571. }\
  572. }
  573. #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
  574. switch(c->dstFormat) {\
  575. case PIX_FMT_RGB48BE:\
  576. case PIX_FMT_RGB48LE:\
  577. func(uint8_t,0)\
  578. ((uint8_t*)dest)[ 0]= r[Y1];\
  579. ((uint8_t*)dest)[ 1]= r[Y1];\
  580. ((uint8_t*)dest)[ 2]= g[Y1];\
  581. ((uint8_t*)dest)[ 3]= g[Y1];\
  582. ((uint8_t*)dest)[ 4]= b[Y1];\
  583. ((uint8_t*)dest)[ 5]= b[Y1];\
  584. ((uint8_t*)dest)[ 6]= r[Y2];\
  585. ((uint8_t*)dest)[ 7]= r[Y2];\
  586. ((uint8_t*)dest)[ 8]= g[Y2];\
  587. ((uint8_t*)dest)[ 9]= g[Y2];\
  588. ((uint8_t*)dest)[10]= b[Y2];\
  589. ((uint8_t*)dest)[11]= b[Y2];\
  590. dest+=12;\
  591. }\
  592. break;\
  593. case PIX_FMT_BGR48BE:\
  594. case PIX_FMT_BGR48LE:\
  595. func(uint8_t,0)\
  596. ((uint8_t*)dest)[ 0] = ((uint8_t*)dest)[ 1] = b[Y1];\
  597. ((uint8_t*)dest)[ 2] = ((uint8_t*)dest)[ 3] = g[Y1];\
  598. ((uint8_t*)dest)[ 4] = ((uint8_t*)dest)[ 5] = r[Y1];\
  599. ((uint8_t*)dest)[ 6] = ((uint8_t*)dest)[ 7] = b[Y2];\
  600. ((uint8_t*)dest)[ 8] = ((uint8_t*)dest)[ 9] = g[Y2];\
  601. ((uint8_t*)dest)[10] = ((uint8_t*)dest)[11] = r[Y2];\
  602. dest+=12;\
  603. }\
  604. break;\
  605. case PIX_FMT_RGBA:\
  606. case PIX_FMT_BGRA:\
  607. if (CONFIG_SMALL) {\
  608. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
  609. func(uint32_t,needAlpha)\
  610. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
  611. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
  612. }\
  613. } else {\
  614. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
  615. func(uint32_t,1)\
  616. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
  617. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
  618. }\
  619. } else {\
  620. func(uint32_t,0)\
  621. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  622. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  623. }\
  624. }\
  625. }\
  626. break;\
  627. case PIX_FMT_ARGB:\
  628. case PIX_FMT_ABGR:\
  629. if (CONFIG_SMALL) {\
  630. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
  631. func(uint32_t,needAlpha)\
  632. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
  633. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
  634. }\
  635. } else {\
  636. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
  637. func(uint32_t,1)\
  638. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
  639. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
  640. }\
  641. } else {\
  642. func(uint32_t,0)\
  643. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  644. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  645. }\
  646. }\
  647. } \
  648. break;\
  649. case PIX_FMT_RGB24:\
  650. func(uint8_t,0)\
  651. ((uint8_t*)dest)[0]= r[Y1];\
  652. ((uint8_t*)dest)[1]= g[Y1];\
  653. ((uint8_t*)dest)[2]= b[Y1];\
  654. ((uint8_t*)dest)[3]= r[Y2];\
  655. ((uint8_t*)dest)[4]= g[Y2];\
  656. ((uint8_t*)dest)[5]= b[Y2];\
  657. dest+=6;\
  658. }\
  659. break;\
  660. case PIX_FMT_BGR24:\
  661. func(uint8_t,0)\
  662. ((uint8_t*)dest)[0]= b[Y1];\
  663. ((uint8_t*)dest)[1]= g[Y1];\
  664. ((uint8_t*)dest)[2]= r[Y1];\
  665. ((uint8_t*)dest)[3]= b[Y2];\
  666. ((uint8_t*)dest)[4]= g[Y2];\
  667. ((uint8_t*)dest)[5]= r[Y2];\
  668. dest+=6;\
  669. }\
  670. break;\
  671. case PIX_FMT_RGB565BE:\
  672. case PIX_FMT_RGB565LE:\
  673. case PIX_FMT_BGR565BE:\
  674. case PIX_FMT_BGR565LE:\
  675. {\
  676. const int dr1= dither_2x2_8[y&1 ][0];\
  677. const int dg1= dither_2x2_4[y&1 ][0];\
  678. const int db1= dither_2x2_8[(y&1)^1][0];\
  679. const int dr2= dither_2x2_8[y&1 ][1];\
  680. const int dg2= dither_2x2_4[y&1 ][1];\
  681. const int db2= dither_2x2_8[(y&1)^1][1];\
  682. func(uint16_t,0)\
  683. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  684. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  685. }\
  686. }\
  687. break;\
  688. case PIX_FMT_RGB555BE:\
  689. case PIX_FMT_RGB555LE:\
  690. case PIX_FMT_BGR555BE:\
  691. case PIX_FMT_BGR555LE:\
  692. {\
  693. const int dr1= dither_2x2_8[y&1 ][0];\
  694. const int dg1= dither_2x2_8[y&1 ][1];\
  695. const int db1= dither_2x2_8[(y&1)^1][0];\
  696. const int dr2= dither_2x2_8[y&1 ][1];\
  697. const int dg2= dither_2x2_8[y&1 ][0];\
  698. const int db2= dither_2x2_8[(y&1)^1][1];\
  699. func(uint16_t,0)\
  700. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  701. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  702. }\
  703. }\
  704. break;\
  705. case PIX_FMT_RGB444BE:\
  706. case PIX_FMT_RGB444LE:\
  707. case PIX_FMT_BGR444BE:\
  708. case PIX_FMT_BGR444LE:\
  709. {\
  710. const int dr1= dither_4x4_16[y&3 ][0];\
  711. const int dg1= dither_4x4_16[y&3 ][1];\
  712. const int db1= dither_4x4_16[(y&3)^3][0];\
  713. const int dr2= dither_4x4_16[y&3 ][1];\
  714. const int dg2= dither_4x4_16[y&3 ][0];\
  715. const int db2= dither_4x4_16[(y&3)^3][1];\
  716. func(uint16_t,0)\
  717. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  718. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  719. }\
  720. }\
  721. break;\
  722. case PIX_FMT_RGB8:\
  723. case PIX_FMT_BGR8:\
  724. {\
  725. const uint8_t * const d64= dither_8x8_73[y&7];\
  726. const uint8_t * const d32= dither_8x8_32[y&7];\
  727. func(uint8_t,0)\
  728. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
  729. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
  730. }\
  731. }\
  732. break;\
  733. case PIX_FMT_RGB4:\
  734. case PIX_FMT_BGR4:\
  735. {\
  736. const uint8_t * const d64= dither_8x8_73 [y&7];\
  737. const uint8_t * const d128=dither_8x8_220[y&7];\
  738. func(uint8_t,0)\
  739. ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
  740. + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
  741. }\
  742. }\
  743. break;\
  744. case PIX_FMT_RGB4_BYTE:\
  745. case PIX_FMT_BGR4_BYTE:\
  746. {\
  747. const uint8_t * const d64= dither_8x8_73 [y&7];\
  748. const uint8_t * const d128=dither_8x8_220[y&7];\
  749. func(uint8_t,0)\
  750. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
  751. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
  752. }\
  753. }\
  754. break;\
  755. case PIX_FMT_MONOBLACK:\
  756. case PIX_FMT_MONOWHITE:\
  757. {\
  758. func_monoblack\
  759. }\
  760. break;\
  761. case PIX_FMT_YUYV422:\
  762. func2\
  763. ((uint8_t*)dest)[2*i2+0]= Y1;\
  764. ((uint8_t*)dest)[2*i2+1]= U;\
  765. ((uint8_t*)dest)[2*i2+2]= Y2;\
  766. ((uint8_t*)dest)[2*i2+3]= V;\
  767. } \
  768. break;\
  769. case PIX_FMT_UYVY422:\
  770. func2\
  771. ((uint8_t*)dest)[2*i2+0]= U;\
  772. ((uint8_t*)dest)[2*i2+1]= Y1;\
  773. ((uint8_t*)dest)[2*i2+2]= V;\
  774. ((uint8_t*)dest)[2*i2+3]= Y2;\
  775. } \
  776. break;\
  777. case PIX_FMT_GRAY16BE:\
  778. func_g16\
  779. ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
  780. ((uint8_t*)dest)[2*i2+1]= Y1;\
  781. ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
  782. ((uint8_t*)dest)[2*i2+3]= Y2;\
  783. } \
  784. break;\
  785. case PIX_FMT_GRAY16LE:\
  786. func_g16\
  787. ((uint8_t*)dest)[2*i2+0]= Y1;\
  788. ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
  789. ((uint8_t*)dest)[2*i2+2]= Y2;\
  790. ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
  791. } \
  792. break;\
  793. }
  794. static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  795. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  796. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  797. {
  798. int i;
  799. 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)
  800. }
  801. static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  802. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  803. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  804. {
  805. int i;
  806. int step= c->dstFormatBpp/8;
  807. int aidx= 3;
  808. switch(c->dstFormat) {
  809. case PIX_FMT_ARGB:
  810. dest++;
  811. aidx= 0;
  812. case PIX_FMT_RGB24:
  813. aidx--;
  814. case PIX_FMT_RGBA:
  815. if (CONFIG_SMALL) {
  816. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
  817. YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
  818. dest[aidx]= needAlpha ? A : 255;
  819. dest[0]= R>>22;
  820. dest[1]= G>>22;
  821. dest[2]= B>>22;
  822. dest+= step;
  823. }
  824. } else {
  825. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  826. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
  827. dest[aidx]= A;
  828. dest[0]= R>>22;
  829. dest[1]= G>>22;
  830. dest[2]= B>>22;
  831. dest+= step;
  832. }
  833. } else {
  834. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
  835. dest[aidx]= 255;
  836. dest[0]= R>>22;
  837. dest[1]= G>>22;
  838. dest[2]= B>>22;
  839. dest+= step;
  840. }
  841. }
  842. }
  843. break;
  844. case PIX_FMT_ABGR:
  845. dest++;
  846. aidx= 0;
  847. case PIX_FMT_BGR24:
  848. aidx--;
  849. case PIX_FMT_BGRA:
  850. if (CONFIG_SMALL) {
  851. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
  852. YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
  853. dest[aidx]= needAlpha ? A : 255;
  854. dest[0]= B>>22;
  855. dest[1]= G>>22;
  856. dest[2]= R>>22;
  857. dest+= step;
  858. }
  859. } else {
  860. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  861. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
  862. dest[aidx]= A;
  863. dest[0]= B>>22;
  864. dest[1]= G>>22;
  865. dest[2]= R>>22;
  866. dest+= step;
  867. }
  868. } else {
  869. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
  870. dest[aidx]= 255;
  871. dest[0]= B>>22;
  872. dest[1]= G>>22;
  873. dest[2]= R>>22;
  874. dest+= step;
  875. }
  876. }
  877. }
  878. break;
  879. default:
  880. assert(0);
  881. }
  882. }
  883. static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
  884. {
  885. int i;
  886. uint8_t *ptr = plane + stride*y;
  887. for (i=0; i<height; i++) {
  888. memset(ptr, val, width);
  889. ptr += stride;
  890. }
  891. }
  892. static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, long width,
  893. uint32_t *unused)
  894. {
  895. int i;
  896. for (i = 0; i < width; i++) {
  897. int r = src[i*6+0];
  898. int g = src[i*6+2];
  899. int b = src[i*6+4];
  900. dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  901. }
  902. }
  903. static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
  904. const uint8_t *src1, const uint8_t *src2,
  905. long width, uint32_t *unused)
  906. {
  907. int i;
  908. assert(src1==src2);
  909. for (i = 0; i < width; i++) {
  910. int r = src1[6*i + 0];
  911. int g = src1[6*i + 2];
  912. int b = src1[6*i + 4];
  913. dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  914. dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  915. }
  916. }
  917. static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
  918. const uint8_t *src1, const uint8_t *src2,
  919. long width, uint32_t *unused)
  920. {
  921. int i;
  922. assert(src1==src2);
  923. for (i = 0; i < width; i++) {
  924. int r= src1[12*i + 0] + src1[12*i + 6];
  925. int g= src1[12*i + 2] + src1[12*i + 8];
  926. int b= src1[12*i + 4] + src1[12*i + 10];
  927. dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  928. dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  929. }
  930. }
  931. static inline void bgr48ToY(uint8_t *dst, const uint8_t *src, long width,
  932. uint32_t *unused)
  933. {
  934. int i;
  935. for (i = 0; i < width; i++) {
  936. int b = src[i*6+0];
  937. int g = src[i*6+2];
  938. int r = src[i*6+4];
  939. dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  940. }
  941. }
  942. static inline void bgr48ToUV(uint8_t *dstU, uint8_t *dstV,
  943. const uint8_t *src1, const uint8_t *src2,
  944. long width, uint32_t *unused)
  945. {
  946. int i;
  947. for (i = 0; i < width; i++) {
  948. int b = src1[6*i + 0];
  949. int g = src1[6*i + 2];
  950. int r = src1[6*i + 4];
  951. dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  952. dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  953. }
  954. }
  955. static inline void bgr48ToUV_half(uint8_t *dstU, uint8_t *dstV,
  956. const uint8_t *src1, const uint8_t *src2,
  957. long width, uint32_t *unused)
  958. {
  959. int i;
  960. for (i = 0; i < width; i++) {
  961. int b= src1[12*i + 0] + src1[12*i + 6];
  962. int g= src1[12*i + 2] + src1[12*i + 8];
  963. int r= src1[12*i + 4] + src1[12*i + 10];
  964. dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  965. dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  966. }
  967. }
  968. #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
  969. static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
  970. {\
  971. int i;\
  972. for (i=0; i<width; i++) {\
  973. int b= (((const type*)src)[i]>>shb)&maskb;\
  974. int g= (((const type*)src)[i]>>shg)&maskg;\
  975. int r= (((const type*)src)[i]>>shr)&maskr;\
  976. \
  977. dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
  978. }\
  979. }
  980. BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
  981. BGR2Y(uint32_t,bgr321ToY,16,16, 0, 0xFF00, 0x00FF, 0xFF00, RY , GY<<8, BY , RGB2YUV_SHIFT+8)
  982. BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
  983. BGR2Y(uint32_t,rgb321ToY, 0,16,16, 0xFF00, 0x00FF, 0xFF00, RY , GY<<8, BY , RGB2YUV_SHIFT+8)
  984. BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY , RGB2YUV_SHIFT+8)
  985. BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY , RGB2YUV_SHIFT+7)
  986. BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
  987. BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
  988. static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  989. {
  990. int i;
  991. for (i=0; i<width; i++) {
  992. dst[i]= src[4*i];
  993. }
  994. }
  995. #define BGR2UV(type, name, shr, shg, shb, shp, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S) \
  996. static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
  997. {\
  998. int i;\
  999. for (i=0; i<width; i++) {\
  1000. int b= ((((const type*)src)[i]>>shp)&maskb)>>shb;\
  1001. int g= ((((const type*)src)[i]>>shp)&maskg)>>shg;\
  1002. int r= ((((const type*)src)[i]>>shp)&maskr)>>shr;\
  1003. \
  1004. dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
  1005. dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
  1006. }\
  1007. }\
  1008. static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
  1009. {\
  1010. int i;\
  1011. for (i=0; i<width; i++) {\
  1012. int pix0= ((const type*)src)[2*i+0]>>shp;\
  1013. int pix1= ((const type*)src)[2*i+1]>>shp;\
  1014. int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
  1015. int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
  1016. int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
  1017. g&= maskg|(2*maskg);\
  1018. \
  1019. g>>=shg;\
  1020. \
  1021. dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
  1022. dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
  1023. }\
  1024. }
  1025. BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1026. BGR2UV(uint32_t,bgr321ToUV,16, 0, 0, 8, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1027. BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1028. BGR2UV(uint32_t,rgb321ToUV, 0, 0,16, 8, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1029. BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RU<<11, GU<<5, BU , RV<<11, GV<<5, BV , RGB2YUV_SHIFT+8)
  1030. BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RU<<10, GU<<5, BU , RV<<10, GV<<5, BV , RGB2YUV_SHIFT+7)
  1031. BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RU , GU<<5, BU<<11, RV , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
  1032. BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RU , GU<<5, BU<<10, RV , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
  1033. static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
  1034. {
  1035. int i;
  1036. for (i=0; i<width; i++) {
  1037. int d= src[i];
  1038. dst[i]= pal[d] & 0xFF;
  1039. }
  1040. }
  1041. static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
  1042. const uint8_t *src1, const uint8_t *src2,
  1043. long width, uint32_t *pal)
  1044. {
  1045. int i;
  1046. assert(src1 == src2);
  1047. for (i=0; i<width; i++) {
  1048. int p= pal[src1[i]];
  1049. dstU[i]= p>>8;
  1050. dstV[i]= p>>16;
  1051. }
  1052. }
  1053. static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  1054. {
  1055. int i, j;
  1056. for (i=0; i<width/8; i++) {
  1057. int d= ~src[i];
  1058. for(j=0; j<8; j++)
  1059. dst[8*i+j]= ((d>>(7-j))&1)*255;
  1060. }
  1061. }
  1062. static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  1063. {
  1064. int i, j;
  1065. for (i=0; i<width/8; i++) {
  1066. int d= src[i];
  1067. for(j=0; j<8; j++)
  1068. dst[8*i+j]= ((d>>(7-j))&1)*255;
  1069. }
  1070. }
  1071. //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
  1072. //Plain C versions
  1073. #if CONFIG_RUNTIME_CPUDETECT
  1074. # define COMPILE_C 1
  1075. # if ARCH_X86
  1076. # define COMPILE_MMX HAVE_MMX
  1077. # define COMPILE_MMX2 HAVE_MMX2
  1078. # define COMPILE_3DNOW HAVE_AMD3DNOW
  1079. # elif ARCH_PPC
  1080. # define COMPILE_ALTIVEC HAVE_ALTIVEC
  1081. # endif
  1082. #else /* CONFIG_RUNTIME_CPUDETECT */
  1083. # if ARCH_X86
  1084. # if HAVE_MMX2
  1085. # define COMPILE_MMX2 1
  1086. # elif HAVE_AMD3DNOW
  1087. # define COMPILE_3DNOW 1
  1088. # elif HAVE_MMX
  1089. # define COMPILE_MMX 1
  1090. # else
  1091. # define COMPILE_C 1
  1092. # endif
  1093. # elif ARCH_PPC && HAVE_ALTIVEC
  1094. # define COMPILE_ALTIVEC 1
  1095. # else
  1096. # define COMPILE_C 1
  1097. # endif
  1098. #endif
  1099. #ifndef COMPILE_C
  1100. # define COMPILE_C 0
  1101. #endif
  1102. #ifndef COMPILE_MMX
  1103. # define COMPILE_MMX 0
  1104. #endif
  1105. #ifndef COMPILE_MMX2
  1106. # define COMPILE_MMX2 0
  1107. #endif
  1108. #ifndef COMPILE_3DNOW
  1109. # define COMPILE_3DNOW 0
  1110. #endif
  1111. #ifndef COMPILE_ALTIVEC
  1112. # define COMPILE_ALTIVEC 0
  1113. #endif
  1114. #define COMPILE_TEMPLATE_MMX 0
  1115. #define COMPILE_TEMPLATE_MMX2 0
  1116. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1117. #define COMPILE_TEMPLATE_ALTIVEC 0
  1118. #include "swscale_template.c"
  1119. #if COMPILE_ALTIVEC
  1120. #undef RENAME
  1121. #undef COMPILE_TEMPLATE_ALTIVEC
  1122. #define COMPILE_TEMPLATE_ALTIVEC 1
  1123. #define RENAME(a) a ## _altivec
  1124. #include "ppc/swscale_template.c"
  1125. #endif
  1126. #if ARCH_X86
  1127. //MMX versions
  1128. #if COMPILE_MMX
  1129. #undef RENAME
  1130. #undef COMPILE_TEMPLATE_MMX
  1131. #undef COMPILE_TEMPLATE_MMX2
  1132. #undef COMPILE_TEMPLATE_AMD3DNOW
  1133. #define COMPILE_TEMPLATE_MMX 1
  1134. #define COMPILE_TEMPLATE_MMX2 0
  1135. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1136. #define RENAME(a) a ## _MMX
  1137. #include "x86/swscale_template.c"
  1138. #endif
  1139. //MMX2 versions
  1140. #if COMPILE_MMX2
  1141. #undef RENAME
  1142. #undef COMPILE_TEMPLATE_MMX
  1143. #undef COMPILE_TEMPLATE_MMX2
  1144. #undef COMPILE_TEMPLATE_AMD3DNOW
  1145. #define COMPILE_TEMPLATE_MMX 1
  1146. #define COMPILE_TEMPLATE_MMX2 1
  1147. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1148. #define RENAME(a) a ## _MMX2
  1149. #include "x86/swscale_template.c"
  1150. #endif
  1151. //3DNOW versions
  1152. #if COMPILE_3DNOW
  1153. #undef RENAME
  1154. #undef COMPILE_TEMPLATE_MMX
  1155. #undef COMPILE_TEMPLATE_MMX2
  1156. #undef COMPILE_TEMPLATE_AMD3DNOW
  1157. #define COMPILE_TEMPLATE_MMX 1
  1158. #define COMPILE_TEMPLATE_MMX2 0
  1159. #define COMPILE_TEMPLATE_AMD3DNOW 1
  1160. #define RENAME(a) a ## _3DNow
  1161. #include "x86/swscale_template.c"
  1162. #endif
  1163. #endif //ARCH_X86
  1164. SwsFunc ff_getSwsFunc(SwsContext *c)
  1165. {
  1166. sws_init_swScale_c(c);
  1167. #if CONFIG_RUNTIME_CPUDETECT
  1168. #if ARCH_X86
  1169. // ordered per speed fastest first
  1170. if (c->flags & SWS_CPU_CAPS_MMX2) {
  1171. sws_init_swScale_MMX2(c);
  1172. return swScale_MMX2;
  1173. } else if (c->flags & SWS_CPU_CAPS_3DNOW) {
  1174. sws_init_swScale_3DNow(c);
  1175. return swScale_3DNow;
  1176. } else if (c->flags & SWS_CPU_CAPS_MMX) {
  1177. sws_init_swScale_MMX(c);
  1178. return swScale_MMX;
  1179. }
  1180. #else
  1181. #if COMPILE_ALTIVEC
  1182. if (c->flags & SWS_CPU_CAPS_ALTIVEC) {
  1183. sws_init_swScale_altivec(c);
  1184. return swScale_altivec;
  1185. }
  1186. #endif
  1187. #endif /* ARCH_X86 */
  1188. #else //CONFIG_RUNTIME_CPUDETECT
  1189. #if COMPILE_TEMPLATE_MMX2
  1190. sws_init_swScale_MMX2(c);
  1191. return swScale_MMX2;
  1192. #elif COMPILE_TEMPLATE_AMD3DNOW
  1193. sws_init_swScale_3DNow(c);
  1194. return swScale_3DNow;
  1195. #elif COMPILE_TEMPLATE_MMX
  1196. sws_init_swScale_MMX(c);
  1197. return swScale_MMX;
  1198. #elif COMPILE_TEMPLATE_ALTIVEC
  1199. sws_init_swScale_altivec(c);
  1200. return swScale_altivec;
  1201. #endif
  1202. #endif //!CONFIG_RUNTIME_CPUDETECT
  1203. return swScale_c;
  1204. }
  1205. static void copyPlane(const uint8_t *src, int srcStride,
  1206. int srcSliceY, int srcSliceH, int width,
  1207. uint8_t *dst, int dstStride)
  1208. {
  1209. dst += dstStride * srcSliceY;
  1210. if (dstStride == srcStride && srcStride > 0) {
  1211. memcpy(dst, src, srcSliceH * dstStride);
  1212. } else {
  1213. int i;
  1214. for (i=0; i<srcSliceH; i++) {
  1215. memcpy(dst, src, width);
  1216. src += srcStride;
  1217. dst += dstStride;
  1218. }
  1219. }
  1220. }
  1221. static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1222. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1223. {
  1224. uint8_t *dst = dstParam[1] + dstStride[1]*srcSliceY/2;
  1225. copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
  1226. dstParam[0], dstStride[0]);
  1227. if (c->dstFormat == PIX_FMT_NV12)
  1228. interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
  1229. else
  1230. interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
  1231. return srcSliceH;
  1232. }
  1233. static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1234. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1235. {
  1236. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1237. yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
  1238. return srcSliceH;
  1239. }
  1240. static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1241. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1242. {
  1243. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1244. yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
  1245. return srcSliceH;
  1246. }
  1247. static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1248. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1249. {
  1250. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1251. yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
  1252. return srcSliceH;
  1253. }
  1254. static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1255. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1256. {
  1257. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1258. yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
  1259. return srcSliceH;
  1260. }
  1261. static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1262. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1263. {
  1264. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1265. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
  1266. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
  1267. yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1268. if (dstParam[3])
  1269. fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1270. return srcSliceH;
  1271. }
  1272. static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1273. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1274. {
  1275. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1276. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
  1277. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
  1278. yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1279. return srcSliceH;
  1280. }
  1281. static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1282. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1283. {
  1284. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1285. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
  1286. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
  1287. uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1288. if (dstParam[3])
  1289. fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1290. return srcSliceH;
  1291. }
  1292. static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1293. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1294. {
  1295. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1296. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
  1297. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
  1298. uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1299. return srcSliceH;
  1300. }
  1301. static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1302. {
  1303. long i;
  1304. for (i=0; i<num_pixels; i++)
  1305. ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | (src[(i<<1)+1] << 24);
  1306. }
  1307. static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1308. {
  1309. long i;
  1310. for (i=0; i<num_pixels; i++)
  1311. ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | src[(i<<1)+1];
  1312. }
  1313. static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1314. {
  1315. long i;
  1316. for (i=0; i<num_pixels; i++) {
  1317. //FIXME slow?
  1318. dst[0]= palette[src[i<<1]*4+0];
  1319. dst[1]= palette[src[i<<1]*4+1];
  1320. dst[2]= palette[src[i<<1]*4+2];
  1321. dst+= 3;
  1322. }
  1323. }
  1324. static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1325. int srcSliceH, uint8_t* dst[], int dstStride[])
  1326. {
  1327. const enum PixelFormat srcFormat= c->srcFormat;
  1328. const enum PixelFormat dstFormat= c->dstFormat;
  1329. void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
  1330. const uint8_t *palette)=NULL;
  1331. int i;
  1332. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1333. const uint8_t *srcPtr= src[0];
  1334. if (srcFormat == PIX_FMT_Y400A) {
  1335. switch (dstFormat) {
  1336. case PIX_FMT_RGB32 : conv = gray8aToPacked32; break;
  1337. case PIX_FMT_BGR32 : conv = gray8aToPacked32; break;
  1338. case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
  1339. case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
  1340. case PIX_FMT_RGB24 : conv = gray8aToPacked24; break;
  1341. case PIX_FMT_BGR24 : conv = gray8aToPacked24; break;
  1342. }
  1343. } else if (usePal(srcFormat)) {
  1344. switch (dstFormat) {
  1345. case PIX_FMT_RGB32 : conv = sws_convertPalette8ToPacked32; break;
  1346. case PIX_FMT_BGR32 : conv = sws_convertPalette8ToPacked32; break;
  1347. case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
  1348. case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
  1349. case PIX_FMT_RGB24 : conv = sws_convertPalette8ToPacked24; break;
  1350. case PIX_FMT_BGR24 : conv = sws_convertPalette8ToPacked24; break;
  1351. }
  1352. }
  1353. if (!conv)
  1354. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1355. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1356. else {
  1357. for (i=0; i<srcSliceH; i++) {
  1358. conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
  1359. srcPtr+= srcStride[0];
  1360. dstPtr+= dstStride[0];
  1361. }
  1362. }
  1363. return srcSliceH;
  1364. }
  1365. #define isRGBA32(x) ( \
  1366. (x) == PIX_FMT_ARGB \
  1367. || (x) == PIX_FMT_RGBA \
  1368. || (x) == PIX_FMT_BGRA \
  1369. || (x) == PIX_FMT_ABGR \
  1370. )
  1371. /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
  1372. static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1373. int srcSliceH, uint8_t* dst[], int dstStride[])
  1374. {
  1375. const enum PixelFormat srcFormat= c->srcFormat;
  1376. const enum PixelFormat dstFormat= c->dstFormat;
  1377. const int srcBpp= (c->srcFormatBpp + 7) >> 3;
  1378. const int dstBpp= (c->dstFormatBpp + 7) >> 3;
  1379. const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
  1380. const int dstId= c->dstFormatBpp >> 2;
  1381. void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
  1382. #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
  1383. if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
  1384. if ( CONV_IS(ABGR, RGBA)
  1385. || CONV_IS(ARGB, BGRA)
  1386. || CONV_IS(BGRA, ARGB)
  1387. || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
  1388. else if (CONV_IS(ABGR, ARGB)
  1389. || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
  1390. else if (CONV_IS(ABGR, BGRA)
  1391. || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
  1392. else if (CONV_IS(BGRA, RGBA)
  1393. || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
  1394. else if (CONV_IS(BGRA, ABGR)
  1395. || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
  1396. } else
  1397. /* BGR -> BGR */
  1398. if ( (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
  1399. || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
  1400. switch(srcId | (dstId<<4)) {
  1401. case 0x34: conv= rgb16to15; break;
  1402. case 0x36: conv= rgb24to15; break;
  1403. case 0x38: conv= rgb32to15; break;
  1404. case 0x43: conv= rgb15to16; break;
  1405. case 0x46: conv= rgb24to16; break;
  1406. case 0x48: conv= rgb32to16; break;
  1407. case 0x63: conv= rgb15to24; break;
  1408. case 0x64: conv= rgb16to24; break;
  1409. case 0x68: conv= rgb32to24; break;
  1410. case 0x83: conv= rgb15to32; break;
  1411. case 0x84: conv= rgb16to32; break;
  1412. case 0x86: conv= rgb24to32; break;
  1413. }
  1414. } else if ( (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
  1415. || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
  1416. switch(srcId | (dstId<<4)) {
  1417. case 0x33: conv= rgb15tobgr15; break;
  1418. case 0x34: conv= rgb16tobgr15; break;
  1419. case 0x36: conv= rgb24tobgr15; break;
  1420. case 0x38: conv= rgb32tobgr15; break;
  1421. case 0x43: conv= rgb15tobgr16; break;
  1422. case 0x44: conv= rgb16tobgr16; break;
  1423. case 0x46: conv= rgb24tobgr16; break;
  1424. case 0x48: conv= rgb32tobgr16; break;
  1425. case 0x63: conv= rgb15tobgr24; break;
  1426. case 0x64: conv= rgb16tobgr24; break;
  1427. case 0x66: conv= rgb24tobgr24; break;
  1428. case 0x68: conv= rgb32tobgr24; break;
  1429. case 0x83: conv= rgb15tobgr32; break;
  1430. case 0x84: conv= rgb16tobgr32; break;
  1431. case 0x86: conv= rgb24tobgr32; break;
  1432. }
  1433. }
  1434. if (!conv) {
  1435. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1436. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1437. } else {
  1438. const uint8_t *srcPtr= src[0];
  1439. uint8_t *dstPtr= dst[0];
  1440. if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
  1441. srcPtr += ALT32_CORR;
  1442. if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
  1443. dstPtr += ALT32_CORR;
  1444. if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
  1445. conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
  1446. else {
  1447. int i;
  1448. dstPtr += dstStride[0]*srcSliceY;
  1449. for (i=0; i<srcSliceH; i++) {
  1450. conv(srcPtr, dstPtr, c->srcW*srcBpp);
  1451. srcPtr+= srcStride[0];
  1452. dstPtr+= dstStride[0];
  1453. }
  1454. }
  1455. }
  1456. return srcSliceH;
  1457. }
  1458. static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1459. int srcSliceH, uint8_t* dst[], int dstStride[])
  1460. {
  1461. rgb24toyv12(
  1462. src[0],
  1463. dst[0]+ srcSliceY *dstStride[0],
  1464. dst[1]+(srcSliceY>>1)*dstStride[1],
  1465. dst[2]+(srcSliceY>>1)*dstStride[2],
  1466. c->srcW, srcSliceH,
  1467. dstStride[0], dstStride[1], srcStride[0]);
  1468. if (dst[3])
  1469. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1470. return srcSliceH;
  1471. }
  1472. static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1473. int srcSliceH, uint8_t* dst[], int dstStride[])
  1474. {
  1475. copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
  1476. dst[0], dstStride[0]);
  1477. planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
  1478. srcSliceH >> 2, srcStride[1], dstStride[1]);
  1479. planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
  1480. srcSliceH >> 2, srcStride[2], dstStride[2]);
  1481. if (dst[3])
  1482. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1483. return srcSliceH;
  1484. }
  1485. /* unscaled copy like stuff (assumes nearly identical formats) */
  1486. static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1487. int srcSliceH, uint8_t* dst[], int dstStride[])
  1488. {
  1489. if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
  1490. memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
  1491. else {
  1492. int i;
  1493. const uint8_t *srcPtr= src[0];
  1494. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1495. int length=0;
  1496. /* universal length finder */
  1497. while(length+c->srcW <= FFABS(dstStride[0])
  1498. && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
  1499. assert(length!=0);
  1500. for (i=0; i<srcSliceH; i++) {
  1501. memcpy(dstPtr, srcPtr, length);
  1502. srcPtr+= srcStride[0];
  1503. dstPtr+= dstStride[0];
  1504. }
  1505. }
  1506. return srcSliceH;
  1507. }
  1508. static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1509. int srcSliceH, uint8_t* dst[], int dstStride[])
  1510. {
  1511. int plane, i, j;
  1512. for (plane=0; plane<4; plane++) {
  1513. int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
  1514. int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
  1515. int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
  1516. const uint8_t *srcPtr= src[plane];
  1517. uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
  1518. if (!dst[plane]) continue;
  1519. // ignore palette for GRAY8
  1520. if (plane == 1 && !dst[2]) continue;
  1521. if (!src[plane] || (plane == 1 && !src[2])) {
  1522. if(is16BPS(c->dstFormat))
  1523. length*=2;
  1524. fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
  1525. } else {
  1526. if(is9_OR_10BPS(c->srcFormat)) {
  1527. const int src_depth = av_pix_fmt_descriptors[c->srcFormat].comp[plane].depth_minus1+1;
  1528. const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
  1529. const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
  1530. if (is16BPS(c->dstFormat)) {
  1531. uint16_t *dstPtr2 = (uint16_t*)dstPtr;
  1532. #define COPY9_OR_10TO16(rfunc, wfunc) \
  1533. for (i = 0; i < height; i++) { \
  1534. for (j = 0; j < length; j++) { \
  1535. int srcpx = rfunc(&srcPtr2[j]); \
  1536. wfunc(&dstPtr2[j], (srcpx<<(16-src_depth)) | (srcpx>>(2*src_depth-16))); \
  1537. } \
  1538. dstPtr2 += dstStride[plane]/2; \
  1539. srcPtr2 += srcStride[plane]/2; \
  1540. }
  1541. if (isBE(c->dstFormat)) {
  1542. if (isBE(c->srcFormat)) {
  1543. COPY9_OR_10TO16(AV_RB16, AV_WB16);
  1544. } else {
  1545. COPY9_OR_10TO16(AV_RL16, AV_WB16);
  1546. }
  1547. } else {
  1548. if (isBE(c->srcFormat)) {
  1549. COPY9_OR_10TO16(AV_RB16, AV_WL16);
  1550. } else {
  1551. COPY9_OR_10TO16(AV_RL16, AV_WL16);
  1552. }
  1553. }
  1554. } else if (is9_OR_10BPS(c->dstFormat)) {
  1555. uint16_t *dstPtr2 = (uint16_t*)dstPtr;
  1556. #define COPY9_OR_10TO9_OR_10(loop) \
  1557. for (i = 0; i < height; i++) { \
  1558. for (j = 0; j < length; j++) { \
  1559. loop; \
  1560. } \
  1561. dstPtr2 += dstStride[plane]/2; \
  1562. srcPtr2 += srcStride[plane]/2; \
  1563. }
  1564. #define COPY9_OR_10TO9_OR_10_2(rfunc, wfunc) \
  1565. if (dst_depth > src_depth) { \
  1566. COPY9_OR_10TO9_OR_10(int srcpx = rfunc(&srcPtr2[j]); \
  1567. wfunc(&dstPtr2[j], (srcpx << 1) | (srcpx >> 9))); \
  1568. } else if (dst_depth < src_depth) { \
  1569. COPY9_OR_10TO9_OR_10(wfunc(&dstPtr2[j], rfunc(&srcPtr2[j]) >> 1)); \
  1570. } else { \
  1571. COPY9_OR_10TO9_OR_10(wfunc(&dstPtr2[j], rfunc(&srcPtr2[j]))); \
  1572. }
  1573. if (isBE(c->dstFormat)) {
  1574. if (isBE(c->srcFormat)) {
  1575. COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WB16);
  1576. } else {
  1577. COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WB16);
  1578. }
  1579. } else {
  1580. if (isBE(c->srcFormat)) {
  1581. COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WL16);
  1582. } else {
  1583. COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WL16);
  1584. }
  1585. }
  1586. } else {
  1587. // FIXME Maybe dither instead.
  1588. #define COPY9_OR_10TO8(rfunc) \
  1589. for (i = 0; i < height; i++) { \
  1590. for (j = 0; j < length; j++) { \
  1591. dstPtr[j] = rfunc(&srcPtr2[j])>>(src_depth-8); \
  1592. } \
  1593. dstPtr += dstStride[plane]; \
  1594. srcPtr2 += srcStride[plane]/2; \
  1595. }
  1596. if (isBE(c->srcFormat)) {
  1597. COPY9_OR_10TO8(AV_RB16);
  1598. } else {
  1599. COPY9_OR_10TO8(AV_RL16);
  1600. }
  1601. }
  1602. } else if(is9_OR_10BPS(c->dstFormat)) {
  1603. const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
  1604. uint16_t *dstPtr2 = (uint16_t*)dstPtr;
  1605. if (is16BPS(c->srcFormat)) {
  1606. const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
  1607. #define COPY16TO9_OR_10(rfunc, wfunc) \
  1608. for (i = 0; i < height; i++) { \
  1609. for (j = 0; j < length; j++) { \
  1610. wfunc(&dstPtr2[j], rfunc(&srcPtr2[j])>>(16-dst_depth)); \
  1611. } \
  1612. dstPtr2 += dstStride[plane]/2; \
  1613. srcPtr2 += srcStride[plane]/2; \
  1614. }
  1615. if (isBE(c->dstFormat)) {
  1616. if (isBE(c->srcFormat)) {
  1617. COPY16TO9_OR_10(AV_RB16, AV_WB16);
  1618. } else {
  1619. COPY16TO9_OR_10(AV_RL16, AV_WB16);
  1620. }
  1621. } else {
  1622. if (isBE(c->srcFormat)) {
  1623. COPY16TO9_OR_10(AV_RB16, AV_WL16);
  1624. } else {
  1625. COPY16TO9_OR_10(AV_RL16, AV_WL16);
  1626. }
  1627. }
  1628. } else /* 8bit */ {
  1629. #define COPY8TO9_OR_10(wfunc) \
  1630. for (i = 0; i < height; i++) { \
  1631. for (j = 0; j < length; j++) { \
  1632. const int srcpx = srcPtr[j]; \
  1633. wfunc(&dstPtr2[j], (srcpx<<(dst_depth-8)) | (srcpx >> (16-dst_depth))); \
  1634. } \
  1635. dstPtr2 += dstStride[plane]/2; \
  1636. srcPtr += srcStride[plane]; \
  1637. }
  1638. if (isBE(c->dstFormat)) {
  1639. COPY8TO9_OR_10(AV_WB16);
  1640. } else {
  1641. COPY8TO9_OR_10(AV_WL16);
  1642. }
  1643. }
  1644. } else if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
  1645. if (!isBE(c->srcFormat)) srcPtr++;
  1646. for (i=0; i<height; i++) {
  1647. for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
  1648. srcPtr+= srcStride[plane];
  1649. dstPtr+= dstStride[plane];
  1650. }
  1651. } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
  1652. for (i=0; i<height; i++) {
  1653. for (j=0; j<length; j++) {
  1654. dstPtr[ j<<1 ] = srcPtr[j];
  1655. dstPtr[(j<<1)+1] = srcPtr[j];
  1656. }
  1657. srcPtr+= srcStride[plane];
  1658. dstPtr+= dstStride[plane];
  1659. }
  1660. } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
  1661. && isBE(c->srcFormat) != isBE(c->dstFormat)) {
  1662. for (i=0; i<height; i++) {
  1663. for (j=0; j<length; j++)
  1664. ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
  1665. srcPtr+= srcStride[plane];
  1666. dstPtr+= dstStride[plane];
  1667. }
  1668. } else if (dstStride[plane] == srcStride[plane] &&
  1669. srcStride[plane] > 0 && srcStride[plane] == length) {
  1670. memcpy(dst[plane] + dstStride[plane]*y, src[plane],
  1671. height*dstStride[plane]);
  1672. } else {
  1673. if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
  1674. length*=2;
  1675. for (i=0; i<height; i++) {
  1676. memcpy(dstPtr, srcPtr, length);
  1677. srcPtr+= srcStride[plane];
  1678. dstPtr+= dstStride[plane];
  1679. }
  1680. }
  1681. }
  1682. }
  1683. return srcSliceH;
  1684. }
  1685. int ff_hardcodedcpuflags(void)
  1686. {
  1687. int flags = 0;
  1688. #if COMPILE_TEMPLATE_MMX2
  1689. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
  1690. #elif COMPILE_TEMPLATE_AMD3DNOW
  1691. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
  1692. #elif COMPILE_TEMPLATE_MMX
  1693. flags |= SWS_CPU_CAPS_MMX;
  1694. #elif COMPILE_TEMPLATE_ALTIVEC
  1695. flags |= SWS_CPU_CAPS_ALTIVEC;
  1696. #elif ARCH_BFIN
  1697. flags |= SWS_CPU_CAPS_BFIN;
  1698. #endif
  1699. return flags;
  1700. }
  1701. void ff_get_unscaled_swscale(SwsContext *c)
  1702. {
  1703. const enum PixelFormat srcFormat = c->srcFormat;
  1704. const enum PixelFormat dstFormat = c->dstFormat;
  1705. const int flags = c->flags;
  1706. const int dstH = c->dstH;
  1707. int needsDither;
  1708. needsDither= isAnyRGB(dstFormat)
  1709. && c->dstFormatBpp < 24
  1710. && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
  1711. /* yv12_to_nv12 */
  1712. if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
  1713. c->swScale= planarToNv12Wrapper;
  1714. }
  1715. /* yuv2bgr */
  1716. if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
  1717. && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
  1718. c->swScale= ff_yuv2rgb_get_func_ptr(c);
  1719. }
  1720. if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
  1721. c->swScale= yvu9ToYv12Wrapper;
  1722. }
  1723. /* bgr24toYV12 */
  1724. if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
  1725. c->swScale= bgr24ToYv12Wrapper;
  1726. /* RGB/BGR -> RGB/BGR (no dither needed forms) */
  1727. if ( isAnyRGB(srcFormat)
  1728. && isAnyRGB(dstFormat)
  1729. && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
  1730. && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
  1731. && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
  1732. && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
  1733. && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
  1734. && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
  1735. && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
  1736. && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
  1737. && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
  1738. && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
  1739. && srcFormat != PIX_FMT_BGR48LE && dstFormat != PIX_FMT_BGR48LE
  1740. && srcFormat != PIX_FMT_BGR48BE && dstFormat != PIX_FMT_BGR48BE
  1741. && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
  1742. c->swScale= rgbToRgbWrapper;
  1743. if ((usePal(srcFormat) && (
  1744. dstFormat == PIX_FMT_RGB32 ||
  1745. dstFormat == PIX_FMT_RGB32_1 ||
  1746. dstFormat == PIX_FMT_RGB24 ||
  1747. dstFormat == PIX_FMT_BGR32 ||
  1748. dstFormat == PIX_FMT_BGR32_1 ||
  1749. dstFormat == PIX_FMT_BGR24)))
  1750. c->swScale= palToRgbWrapper;
  1751. if (srcFormat == PIX_FMT_YUV422P) {
  1752. if (dstFormat == PIX_FMT_YUYV422)
  1753. c->swScale= yuv422pToYuy2Wrapper;
  1754. else if (dstFormat == PIX_FMT_UYVY422)
  1755. c->swScale= yuv422pToUyvyWrapper;
  1756. }
  1757. /* LQ converters if -sws 0 or -sws 4*/
  1758. if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
  1759. /* yv12_to_yuy2 */
  1760. if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
  1761. if (dstFormat == PIX_FMT_YUYV422)
  1762. c->swScale= planarToYuy2Wrapper;
  1763. else if (dstFormat == PIX_FMT_UYVY422)
  1764. c->swScale= planarToUyvyWrapper;
  1765. }
  1766. }
  1767. if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  1768. c->swScale= yuyvToYuv420Wrapper;
  1769. if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  1770. c->swScale= uyvyToYuv420Wrapper;
  1771. if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
  1772. c->swScale= yuyvToYuv422Wrapper;
  1773. if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
  1774. c->swScale= uyvyToYuv422Wrapper;
  1775. #if COMPILE_ALTIVEC
  1776. if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
  1777. !(c->flags & SWS_BITEXACT) &&
  1778. srcFormat == PIX_FMT_YUV420P) {
  1779. // unscaled YV12 -> packed YUV, we want speed
  1780. if (dstFormat == PIX_FMT_YUYV422)
  1781. c->swScale= yv12toyuy2_unscaled_altivec;
  1782. else if (dstFormat == PIX_FMT_UYVY422)
  1783. c->swScale= yv12touyvy_unscaled_altivec;
  1784. }
  1785. #endif
  1786. /* simple copy */
  1787. if ( srcFormat == dstFormat
  1788. || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
  1789. || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
  1790. || (isPlanarYUV(srcFormat) && isGray(dstFormat))
  1791. || (isPlanarYUV(dstFormat) && isGray(srcFormat))
  1792. || (isGray(dstFormat) && isGray(srcFormat))
  1793. || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
  1794. && c->chrDstHSubSample == c->chrSrcHSubSample
  1795. && c->chrDstVSubSample == c->chrSrcVSubSample
  1796. && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
  1797. && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
  1798. {
  1799. if (isPacked(c->srcFormat))
  1800. c->swScale= packedCopyWrapper;
  1801. else /* Planar YUV or gray */
  1802. c->swScale= planarCopyWrapper;
  1803. }
  1804. #if ARCH_BFIN
  1805. if (flags & SWS_CPU_CAPS_BFIN)
  1806. ff_bfin_get_unscaled_swscale (c);
  1807. #endif
  1808. }
  1809. static void reset_ptr(const uint8_t* src[], int format)
  1810. {
  1811. if(!isALPHA(format))
  1812. src[3]=NULL;
  1813. if(!isPlanarYUV(format)) {
  1814. src[3]=src[2]=NULL;
  1815. if (!usePal(format))
  1816. src[1]= NULL;
  1817. }
  1818. }
  1819. static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
  1820. const int linesizes[4])
  1821. {
  1822. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  1823. int i;
  1824. for (i = 0; i < 4; i++) {
  1825. int plane = desc->comp[i].plane;
  1826. if (!data[plane] || !linesizes[plane])
  1827. return 0;
  1828. }
  1829. return 1;
  1830. }
  1831. /**
  1832. * swscale wrapper, so we don't need to export the SwsContext.
  1833. * Assumes planar YUV to be in YUV order instead of YVU.
  1834. */
  1835. int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
  1836. int srcSliceH, uint8_t* const dst[], const int dstStride[])
  1837. {
  1838. int i;
  1839. const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
  1840. uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
  1841. // do not mess up sliceDir if we have a "trailing" 0-size slice
  1842. if (srcSliceH == 0)
  1843. return 0;
  1844. if (!check_image_pointers(src, c->srcFormat, srcStride)) {
  1845. av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
  1846. return 0;
  1847. }
  1848. if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
  1849. av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
  1850. return 0;
  1851. }
  1852. if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
  1853. av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
  1854. return 0;
  1855. }
  1856. if (c->sliceDir == 0) {
  1857. if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
  1858. }
  1859. if (usePal(c->srcFormat)) {
  1860. for (i=0; i<256; i++) {
  1861. int p, r, g, b,y,u,v;
  1862. if(c->srcFormat == PIX_FMT_PAL8) {
  1863. p=((const uint32_t*)(src[1]))[i];
  1864. r= (p>>16)&0xFF;
  1865. g= (p>> 8)&0xFF;
  1866. b= p &0xFF;
  1867. } else if(c->srcFormat == PIX_FMT_RGB8) {
  1868. r= (i>>5 )*36;
  1869. g= ((i>>2)&7)*36;
  1870. b= (i&3 )*85;
  1871. } else if(c->srcFormat == PIX_FMT_BGR8) {
  1872. b= (i>>6 )*85;
  1873. g= ((i>>3)&7)*36;
  1874. r= (i&7 )*36;
  1875. } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
  1876. r= (i>>3 )*255;
  1877. g= ((i>>1)&3)*85;
  1878. b= (i&1 )*255;
  1879. } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_Y400A) {
  1880. r = g = b = i;
  1881. } else {
  1882. assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
  1883. b= (i>>3 )*255;
  1884. g= ((i>>1)&3)*85;
  1885. r= (i&1 )*255;
  1886. }
  1887. y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1888. u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1889. v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1890. c->pal_yuv[i]= y + (u<<8) + (v<<16);
  1891. switch(c->dstFormat) {
  1892. case PIX_FMT_BGR32:
  1893. #if !HAVE_BIGENDIAN
  1894. case PIX_FMT_RGB24:
  1895. #endif
  1896. c->pal_rgb[i]= r + (g<<8) + (b<<16);
  1897. break;
  1898. case PIX_FMT_BGR32_1:
  1899. #if HAVE_BIGENDIAN
  1900. case PIX_FMT_BGR24:
  1901. #endif
  1902. c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
  1903. break;
  1904. case PIX_FMT_RGB32_1:
  1905. #if HAVE_BIGENDIAN
  1906. case PIX_FMT_RGB24:
  1907. #endif
  1908. c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
  1909. break;
  1910. case PIX_FMT_RGB32:
  1911. #if !HAVE_BIGENDIAN
  1912. case PIX_FMT_BGR24:
  1913. #endif
  1914. default:
  1915. c->pal_rgb[i]= b + (g<<8) + (r<<16);
  1916. }
  1917. }
  1918. }
  1919. // copy strides, so they can safely be modified
  1920. if (c->sliceDir == 1) {
  1921. // slices go from top to bottom
  1922. int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
  1923. int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
  1924. reset_ptr(src2, c->srcFormat);
  1925. reset_ptr((const uint8_t**)dst2, c->dstFormat);
  1926. /* reset slice direction at end of frame */
  1927. if (srcSliceY + srcSliceH == c->srcH)
  1928. c->sliceDir = 0;
  1929. return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
  1930. } else {
  1931. // slices go from bottom to top => we flip the image internally
  1932. int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
  1933. int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
  1934. src2[0] += (srcSliceH-1)*srcStride[0];
  1935. if (!usePal(c->srcFormat))
  1936. src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
  1937. src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
  1938. src2[3] += (srcSliceH-1)*srcStride[3];
  1939. dst2[0] += ( c->dstH -1)*dstStride[0];
  1940. dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
  1941. dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
  1942. dst2[3] += ( c->dstH -1)*dstStride[3];
  1943. reset_ptr(src2, c->srcFormat);
  1944. reset_ptr((const uint8_t**)dst2, c->dstFormat);
  1945. /* reset slice direction at end of frame */
  1946. if (!srcSliceY)
  1947. c->sliceDir = 0;
  1948. return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
  1949. }
  1950. }
  1951. /* Convert the palette to the same packed 32-bit format as the palette */
  1952. void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1953. {
  1954. long i;
  1955. for (i=0; i<num_pixels; i++)
  1956. ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
  1957. }
  1958. /* Palette format: ABCD -> dst format: ABC */
  1959. void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1960. {
  1961. long i;
  1962. for (i=0; i<num_pixels; i++) {
  1963. //FIXME slow?
  1964. dst[0]= palette[src[i]*4+0];
  1965. dst[1]= palette[src[i]*4+1];
  1966. dst[2]= palette[src[i]*4+2];
  1967. dst+= 3;
  1968. }
  1969. }