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.

2081 lines
75KB

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