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.

2102 lines
76KB

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