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.

2034 lines
71KB

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