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.

762 lines
28KB

  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. #include <assert.h>
  21. #include <math.h>
  22. #include <stdint.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include "libavutil/avutil.h"
  26. #include "libavutil/bswap.h"
  27. #include "libavutil/cpu.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "config.h"
  32. #include "rgb2rgb.h"
  33. #include "swscale.h"
  34. #include "swscale_internal.h"
  35. #define RGB2YUV_SHIFT 15
  36. #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  37. #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  38. #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  39. #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  40. #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  41. #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  42. #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  43. #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  44. #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  45. #define input_pixel(pos) (isBE(origin) ? AV_RB16(pos) : AV_RL16(pos))
  46. #define r ((origin == PIX_FMT_BGR48BE || origin == PIX_FMT_BGR48LE) ? b_r : r_b)
  47. #define b ((origin == PIX_FMT_BGR48BE || origin == PIX_FMT_BGR48LE) ? r_b : b_r)
  48. static av_always_inline void
  49. rgb48ToY_c_template(uint16_t *dst, const uint16_t *src, int width,
  50. enum PixelFormat origin)
  51. {
  52. int i;
  53. for (i = 0; i < width; i++) {
  54. unsigned int r_b = input_pixel(&src[i*3+0]);
  55. unsigned int g = input_pixel(&src[i*3+1]);
  56. unsigned int b_r = input_pixel(&src[i*3+2]);
  57. dst[i] = (RY*r + GY*g + BY*b + (0x2001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  58. }
  59. }
  60. static av_always_inline void
  61. rgb48ToUV_c_template(uint16_t *dstU, uint16_t *dstV,
  62. const uint16_t *src1, const uint16_t *src2,
  63. int width, enum PixelFormat origin)
  64. {
  65. int i;
  66. assert(src1==src2);
  67. for (i = 0; i < width; i++) {
  68. int r_b = input_pixel(&src1[i*3+0]);
  69. int g = input_pixel(&src1[i*3+1]);
  70. int b_r = input_pixel(&src1[i*3+2]);
  71. dstU[i] = (RU*r + GU*g + BU*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  72. dstV[i] = (RV*r + GV*g + BV*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  73. }
  74. }
  75. static av_always_inline void
  76. rgb48ToUV_half_c_template(uint16_t *dstU, uint16_t *dstV,
  77. const uint16_t *src1, const uint16_t *src2,
  78. int width, enum PixelFormat origin)
  79. {
  80. int i;
  81. assert(src1==src2);
  82. for (i = 0; i < width; i++) {
  83. int r_b = (input_pixel(&src1[6 * i + 0]) + input_pixel(&src1[6 * i + 3]) + 1) >> 1;
  84. int g = (input_pixel(&src1[6 * i + 1]) + input_pixel(&src1[6 * i + 4]) + 1) >> 1;
  85. int b_r = (input_pixel(&src1[6 * i + 2]) + input_pixel(&src1[6 * i + 5]) + 1) >> 1;
  86. dstU[i]= (RU*r + GU*g + BU*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  87. dstV[i]= (RV*r + GV*g + BV*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  88. }
  89. }
  90. #undef r
  91. #undef b
  92. #undef input_pixel
  93. #define rgb48funcs(pattern, BE_LE, origin) \
  94. static void pattern ## 48 ## BE_LE ## ToY_c(uint8_t *_dst, const uint8_t *_src, \
  95. int width, uint32_t *unused) \
  96. { \
  97. const uint16_t *src = (const uint16_t *) _src; \
  98. uint16_t *dst = (uint16_t *) _dst; \
  99. rgb48ToY_c_template(dst, src, width, origin); \
  100. } \
  101. \
  102. static void pattern ## 48 ## BE_LE ## ToUV_c(uint8_t *_dstU, uint8_t *_dstV, \
  103. const uint8_t *_src1, const uint8_t *_src2, \
  104. int width, uint32_t *unused) \
  105. { \
  106. const uint16_t *src1 = (const uint16_t *) _src1, \
  107. *src2 = (const uint16_t *) _src2; \
  108. uint16_t *dstU = (uint16_t *) _dstU, *dstV = (uint16_t *) _dstV; \
  109. rgb48ToUV_c_template(dstU, dstV, src1, src2, width, origin); \
  110. } \
  111. \
  112. static void pattern ## 48 ## BE_LE ## ToUV_half_c(uint8_t *_dstU, uint8_t *_dstV, \
  113. const uint8_t *_src1, const uint8_t *_src2, \
  114. int width, uint32_t *unused) \
  115. { \
  116. const uint16_t *src1 = (const uint16_t *) _src1, \
  117. *src2 = (const uint16_t *) _src2; \
  118. uint16_t *dstU = (uint16_t *) _dstU, *dstV = (uint16_t *) _dstV; \
  119. rgb48ToUV_half_c_template(dstU, dstV, src1, src2, width, origin); \
  120. }
  121. rgb48funcs(rgb, LE, PIX_FMT_RGB48LE)
  122. rgb48funcs(rgb, BE, PIX_FMT_RGB48BE)
  123. rgb48funcs(bgr, LE, PIX_FMT_BGR48LE)
  124. rgb48funcs(bgr, BE, PIX_FMT_BGR48BE)
  125. #define input_pixel(i) ((origin == PIX_FMT_RGBA || origin == PIX_FMT_BGRA || \
  126. origin == PIX_FMT_ARGB || origin == PIX_FMT_ABGR) ? AV_RN32A(&src[(i)*4]) : \
  127. (isBE(origin) ? AV_RB16(&src[(i)*2]) : AV_RL16(&src[(i)*2])))
  128. static av_always_inline void
  129. rgb16_32ToY_c_template(uint8_t *dst, const uint8_t *src,
  130. int width, enum PixelFormat origin,
  131. int shr, int shg, int shb, int shp,
  132. int maskr, int maskg, int maskb,
  133. int rsh, int gsh, int bsh, int S)
  134. {
  135. const int ry = RY << rsh, gy = GY << gsh, by = BY << bsh;
  136. const unsigned rnd = 33u << (S - 1);
  137. int i;
  138. for (i = 0; i < width; i++) {
  139. int px = input_pixel(i) >> shp;
  140. int b = (px & maskb) >> shb;
  141. int g = (px & maskg) >> shg;
  142. int r = (px & maskr) >> shr;
  143. dst[i] = (ry * r + gy * g + by * b + rnd) >> S;
  144. }
  145. }
  146. static av_always_inline void
  147. rgb16_32ToUV_c_template(uint8_t *dstU, uint8_t *dstV,
  148. const uint8_t *src, int width,
  149. enum PixelFormat origin,
  150. int shr, int shg, int shb, int shp,
  151. int maskr, int maskg, int maskb,
  152. int rsh, int gsh, int bsh, int S)
  153. {
  154. const int ru = RU << rsh, gu = GU << gsh, bu = BU << bsh,
  155. rv = RV << rsh, gv = GV << gsh, bv = BV << bsh;
  156. const unsigned rnd = 257u << (S - 1);
  157. int i;
  158. for (i = 0; i < width; i++) {
  159. int px = input_pixel(i) >> shp;
  160. int b = (px & maskb) >> shb;
  161. int g = (px & maskg) >> shg;
  162. int r = (px & maskr) >> shr;
  163. dstU[i] = (ru * r + gu * g + bu * b + rnd) >> S;
  164. dstV[i] = (rv * r + gv * g + bv * b + rnd) >> S;
  165. }
  166. }
  167. static av_always_inline void
  168. rgb16_32ToUV_half_c_template(uint8_t *dstU, uint8_t *dstV,
  169. const uint8_t *src, int width,
  170. enum PixelFormat origin,
  171. int shr, int shg, int shb, int shp,
  172. int maskr, int maskg, int maskb,
  173. int rsh, int gsh, int bsh, int S)
  174. {
  175. const int ru = RU << rsh, gu = GU << gsh, bu = BU << bsh,
  176. rv = RV << rsh, gv = GV << gsh, bv = BV << bsh,
  177. maskgx = ~(maskr | maskb);
  178. const unsigned rnd = 257u << S;
  179. int i;
  180. maskr |= maskr << 1; maskb |= maskb << 1; maskg |= maskg << 1;
  181. for (i = 0; i < width; i++) {
  182. int px0 = input_pixel(2 * i + 0) >> shp;
  183. int px1 = input_pixel(2 * i + 1) >> shp;
  184. int b, r, g = (px0 & maskgx) + (px1 & maskgx);
  185. int rb = px0 + px1 - g;
  186. b = (rb & maskb) >> shb;
  187. if (shp || origin == PIX_FMT_BGR565LE || origin == PIX_FMT_BGR565BE ||
  188. origin == PIX_FMT_RGB565LE || origin == PIX_FMT_RGB565BE) {
  189. g >>= shg;
  190. } else {
  191. g = (g & maskg) >> shg;
  192. }
  193. r = (rb & maskr) >> shr;
  194. dstU[i] = (ru * r + gu * g + bu * b + rnd) >> (S + 1);
  195. dstV[i] = (rv * r + gv * g + bv * b + rnd) >> (S + 1);
  196. }
  197. }
  198. #undef input_pixel
  199. #define rgb16_32_wrapper(fmt, name, shr, shg, shb, shp, maskr, \
  200. maskg, maskb, rsh, gsh, bsh, S) \
  201. static void name ## ToY_c(uint8_t *dst, const uint8_t *src, \
  202. int width, uint32_t *unused) \
  203. { \
  204. rgb16_32ToY_c_template(dst, src, width, fmt, shr, shg, shb, shp, \
  205. maskr, maskg, maskb, rsh, gsh, bsh, S); \
  206. } \
  207. \
  208. static void name ## ToUV_c(uint8_t *dstU, uint8_t *dstV, \
  209. const uint8_t *src, const uint8_t *dummy, \
  210. int width, uint32_t *unused) \
  211. { \
  212. rgb16_32ToUV_c_template(dstU, dstV, src, width, fmt, shr, shg, shb, shp, \
  213. maskr, maskg, maskb, rsh, gsh, bsh, S); \
  214. } \
  215. \
  216. static void name ## ToUV_half_c(uint8_t *dstU, uint8_t *dstV, \
  217. const uint8_t *src, const uint8_t *dummy, \
  218. int width, uint32_t *unused) \
  219. { \
  220. rgb16_32ToUV_half_c_template(dstU, dstV, src, width, fmt, shr, shg, shb, shp, \
  221. maskr, maskg, maskb, rsh, gsh, bsh, S); \
  222. }
  223. rgb16_32_wrapper(PIX_FMT_BGR32, bgr32, 16, 0, 0, 0, 0xFF0000, 0xFF00, 0x00FF, 8, 0, 8, RGB2YUV_SHIFT+8)
  224. rgb16_32_wrapper(PIX_FMT_BGR32_1, bgr321, 16, 0, 0, 8, 0xFF0000, 0xFF00, 0x00FF, 8, 0, 8, RGB2YUV_SHIFT+8)
  225. rgb16_32_wrapper(PIX_FMT_RGB32, rgb32, 0, 0, 16, 0, 0x00FF, 0xFF00, 0xFF0000, 8, 0, 8, RGB2YUV_SHIFT+8)
  226. rgb16_32_wrapper(PIX_FMT_RGB32_1, rgb321, 0, 0, 16, 8, 0x00FF, 0xFF00, 0xFF0000, 8, 0, 8, RGB2YUV_SHIFT+8)
  227. rgb16_32_wrapper(PIX_FMT_BGR565LE, bgr16le, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, 11, 5, 0, RGB2YUV_SHIFT+8)
  228. rgb16_32_wrapper(PIX_FMT_BGR555LE, bgr15le, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, 10, 5, 0, RGB2YUV_SHIFT+7)
  229. rgb16_32_wrapper(PIX_FMT_BGR444LE, bgr12le, 0, 0, 0, 0, 0x000F, 0x00F0, 0x0F00, 8, 4, 0, RGB2YUV_SHIFT+4)
  230. rgb16_32_wrapper(PIX_FMT_RGB565LE, rgb16le, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, 0, 5, 11, RGB2YUV_SHIFT+8)
  231. rgb16_32_wrapper(PIX_FMT_RGB555LE, rgb15le, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, 0, 5, 10, RGB2YUV_SHIFT+7)
  232. rgb16_32_wrapper(PIX_FMT_RGB444LE, rgb12le, 0, 0, 0, 0, 0x0F00, 0x00F0, 0x000F, 0, 4, 8, RGB2YUV_SHIFT+4)
  233. rgb16_32_wrapper(PIX_FMT_BGR565BE, bgr16be, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, 11, 5, 0, RGB2YUV_SHIFT+8)
  234. rgb16_32_wrapper(PIX_FMT_BGR555BE, bgr15be, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, 10, 5, 0, RGB2YUV_SHIFT+7)
  235. rgb16_32_wrapper(PIX_FMT_BGR444BE, bgr12be, 0, 0, 0, 0, 0x000F, 0x00F0, 0x0F00, 8, 4, 0, RGB2YUV_SHIFT+4)
  236. rgb16_32_wrapper(PIX_FMT_RGB565BE, rgb16be, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, 0, 5, 11, RGB2YUV_SHIFT+8)
  237. rgb16_32_wrapper(PIX_FMT_RGB555BE, rgb15be, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, 0, 5, 10, RGB2YUV_SHIFT+7)
  238. rgb16_32_wrapper(PIX_FMT_RGB444BE, rgb12be, 0, 0, 0, 0, 0x0F00, 0x00F0, 0x000F, 0, 4, 8, RGB2YUV_SHIFT+4)
  239. static void abgrToA_c(uint8_t *dst, const uint8_t *src, int width, uint32_t *unused)
  240. {
  241. int i;
  242. for (i=0; i<width; i++) {
  243. dst[i]= src[4*i];
  244. }
  245. }
  246. static void rgbaToA_c(uint8_t *dst, const uint8_t *src, int width, uint32_t *unused)
  247. {
  248. int i;
  249. for (i=0; i<width; i++) {
  250. dst[i]= src[4*i+3];
  251. }
  252. }
  253. static void palToY_c(uint8_t *dst, const uint8_t *src, int width, uint32_t *pal)
  254. {
  255. int i;
  256. for (i=0; i<width; i++) {
  257. int d= src[i];
  258. dst[i]= pal[d] & 0xFF;
  259. }
  260. }
  261. static void palToUV_c(uint8_t *dstU, uint8_t *dstV,
  262. const uint8_t *src1, const uint8_t *src2,
  263. int width, uint32_t *pal)
  264. {
  265. int i;
  266. assert(src1 == src2);
  267. for (i=0; i<width; i++) {
  268. int p= pal[src1[i]];
  269. dstU[i]= p>>8;
  270. dstV[i]= p>>16;
  271. }
  272. }
  273. static void monowhite2Y_c(uint8_t *dst, const uint8_t *src,
  274. int width, uint32_t *unused)
  275. {
  276. int i, j;
  277. for (i=0; i<width/8; i++) {
  278. int d= ~src[i];
  279. for(j=0; j<8; j++)
  280. dst[8*i+j]= ((d>>(7-j))&1)*255;
  281. }
  282. }
  283. static void monoblack2Y_c(uint8_t *dst, const uint8_t *src,
  284. int width, uint32_t *unused)
  285. {
  286. int i, j;
  287. for (i=0; i<width/8; i++) {
  288. int d= src[i];
  289. for(j=0; j<8; j++)
  290. dst[8*i+j]= ((d>>(7-j))&1)*255;
  291. }
  292. }
  293. static void yuy2ToY_c(uint8_t *dst, const uint8_t *src, int width,
  294. uint32_t *unused)
  295. {
  296. int i;
  297. for (i=0; i<width; i++)
  298. dst[i]= src[2*i];
  299. }
  300. static void yuy2ToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  301. const uint8_t *src2, int width, uint32_t *unused)
  302. {
  303. int i;
  304. for (i=0; i<width; i++) {
  305. dstU[i]= src1[4*i + 1];
  306. dstV[i]= src1[4*i + 3];
  307. }
  308. assert(src1 == src2);
  309. }
  310. static void bswap16Y_c(uint8_t *_dst, const uint8_t *_src, int width, uint32_t *unused)
  311. {
  312. int i;
  313. const uint16_t *src = (const uint16_t *) _src;
  314. uint16_t *dst = (uint16_t *) _dst;
  315. for (i=0; i<width; i++) {
  316. dst[i] = av_bswap16(src[i]);
  317. }
  318. }
  319. static void bswap16UV_c(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *_src1,
  320. const uint8_t *_src2, int width, uint32_t *unused)
  321. {
  322. int i;
  323. const uint16_t *src1 = (const uint16_t *) _src1,
  324. *src2 = (const uint16_t *) _src2;
  325. uint16_t *dstU = (uint16_t *) _dstU, *dstV = (uint16_t *) _dstV;
  326. for (i=0; i<width; i++) {
  327. dstU[i] = av_bswap16(src1[i]);
  328. dstV[i] = av_bswap16(src2[i]);
  329. }
  330. }
  331. /* This is almost identical to the previous, end exists only because
  332. * yuy2ToY/UV)(dst, src+1, ...) would have 100% unaligned accesses. */
  333. static void uyvyToY_c(uint8_t *dst, const uint8_t *src, int width,
  334. uint32_t *unused)
  335. {
  336. int i;
  337. for (i=0; i<width; i++)
  338. dst[i]= src[2*i+1];
  339. }
  340. static void uyvyToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  341. const uint8_t *src2, int width, uint32_t *unused)
  342. {
  343. int i;
  344. for (i=0; i<width; i++) {
  345. dstU[i]= src1[4*i + 0];
  346. dstV[i]= src1[4*i + 2];
  347. }
  348. assert(src1 == src2);
  349. }
  350. static av_always_inline void nvXXtoUV_c(uint8_t *dst1, uint8_t *dst2,
  351. const uint8_t *src, int width)
  352. {
  353. int i;
  354. for (i = 0; i < width; i++) {
  355. dst1[i] = src[2*i+0];
  356. dst2[i] = src[2*i+1];
  357. }
  358. }
  359. static void nv12ToUV_c(uint8_t *dstU, uint8_t *dstV,
  360. const uint8_t *src1, const uint8_t *src2,
  361. int width, uint32_t *unused)
  362. {
  363. nvXXtoUV_c(dstU, dstV, src1, width);
  364. }
  365. static void nv21ToUV_c(uint8_t *dstU, uint8_t *dstV,
  366. const uint8_t *src1, const uint8_t *src2,
  367. int width, uint32_t *unused)
  368. {
  369. nvXXtoUV_c(dstV, dstU, src1, width);
  370. }
  371. #define input_pixel(pos) (isBE(origin) ? AV_RB16(pos) : AV_RL16(pos))
  372. static void bgr24ToY_c(uint8_t *dst, const uint8_t *src,
  373. int width, uint32_t *unused)
  374. {
  375. int i;
  376. for (i=0; i<width; i++) {
  377. int b= src[i*3+0];
  378. int g= src[i*3+1];
  379. int r= src[i*3+2];
  380. dst[i]= ((RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  381. }
  382. }
  383. static void bgr24ToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  384. const uint8_t *src2, int width, uint32_t *unused)
  385. {
  386. int i;
  387. for (i=0; i<width; i++) {
  388. int b= src1[3*i + 0];
  389. int g= src1[3*i + 1];
  390. int r= src1[3*i + 2];
  391. dstU[i]= (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT;
  392. dstV[i]= (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT;
  393. }
  394. assert(src1 == src2);
  395. }
  396. static void bgr24ToUV_half_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  397. const uint8_t *src2, int width, uint32_t *unused)
  398. {
  399. int i;
  400. for (i=0; i<width; i++) {
  401. int b= src1[6*i + 0] + src1[6*i + 3];
  402. int g= src1[6*i + 1] + src1[6*i + 4];
  403. int r= src1[6*i + 2] + src1[6*i + 5];
  404. dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT))>>(RGB2YUV_SHIFT+1);
  405. dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT))>>(RGB2YUV_SHIFT+1);
  406. }
  407. assert(src1 == src2);
  408. }
  409. static void rgb24ToY_c(uint8_t *dst, const uint8_t *src, int width,
  410. uint32_t *unused)
  411. {
  412. int i;
  413. for (i=0; i<width; i++) {
  414. int r= src[i*3+0];
  415. int g= src[i*3+1];
  416. int b= src[i*3+2];
  417. dst[i]= ((RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  418. }
  419. }
  420. static void rgb24ToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  421. const uint8_t *src2, int width, uint32_t *unused)
  422. {
  423. int i;
  424. assert(src1==src2);
  425. for (i=0; i<width; i++) {
  426. int r= src1[3*i + 0];
  427. int g= src1[3*i + 1];
  428. int b= src1[3*i + 2];
  429. dstU[i]= (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT;
  430. dstV[i]= (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT;
  431. }
  432. }
  433. static void rgb24ToUV_half_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  434. const uint8_t *src2, int width, uint32_t *unused)
  435. {
  436. int i;
  437. assert(src1==src2);
  438. for (i=0; i<width; i++) {
  439. int r= src1[6*i + 0] + src1[6*i + 3];
  440. int g= src1[6*i + 1] + src1[6*i + 4];
  441. int b= src1[6*i + 2] + src1[6*i + 5];
  442. dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT))>>(RGB2YUV_SHIFT+1);
  443. dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT))>>(RGB2YUV_SHIFT+1);
  444. }
  445. }
  446. static void planar_rgb_to_y(uint8_t *dst, const uint8_t *src[4], int width)
  447. {
  448. int i;
  449. for (i = 0; i < width; i++) {
  450. int g = src[0][i];
  451. int b = src[1][i];
  452. int r = src[2][i];
  453. dst[i] = ((RY * r + GY * g + BY * b + (33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  454. }
  455. }
  456. static void planar_rgb16le_to_y(uint8_t *_dst, const uint8_t *_src[4], int width)
  457. {
  458. int i;
  459. const uint16_t **src = (const uint16_t **) _src;
  460. uint16_t *dst = (uint16_t *) _dst;
  461. for (i = 0; i < width; i++) {
  462. int g = AV_RL16(src[0] + i);
  463. int b = AV_RL16(src[1] + i);
  464. int r = AV_RL16(src[2] + i);
  465. dst[i] = ((RY * r + GY * g + BY * b + (33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  466. }
  467. }
  468. static void planar_rgb16be_to_y(uint8_t *_dst, const uint8_t *_src[4], int width)
  469. {
  470. int i;
  471. const uint16_t **src = (const uint16_t **) _src;
  472. uint16_t *dst = (uint16_t *) _dst;
  473. for (i = 0; i < width; i++) {
  474. int g = AV_RB16(src[0] + i);
  475. int b = AV_RB16(src[1] + i);
  476. int r = AV_RB16(src[2] + i);
  477. dst[i] = ((RY * r + GY * g + BY * b + (33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  478. }
  479. }
  480. static void planar_rgb_to_uv(uint8_t *dstU, uint8_t *dstV, const uint8_t *src[4], int width)
  481. {
  482. int i;
  483. for (i = 0; i < width; i++) {
  484. int g = src[0][i];
  485. int b = src[1][i];
  486. int r = src[2][i];
  487. dstU[i] = (RU * r + GU * g + BU * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
  488. dstV[i] = (RV * r + GV * g + BV * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
  489. }
  490. }
  491. static void planar_rgb16le_to_uv(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *_src[4], int width)
  492. {
  493. int i;
  494. const uint16_t **src = (const uint16_t **) _src;
  495. uint16_t *dstU = (uint16_t *) _dstU;
  496. uint16_t *dstV = (uint16_t *) _dstV;
  497. for (i = 0; i < width; i++) {
  498. int g = AV_RL16(src[0] + i);
  499. int b = AV_RL16(src[1] + i);
  500. int r = AV_RL16(src[2] + i);
  501. dstU[i] = (RU * r + GU * g + BU * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
  502. dstV[i] = (RV * r + GV * g + BV * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
  503. }
  504. }
  505. static void planar_rgb16be_to_uv(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *_src[4], int width)
  506. {
  507. int i;
  508. const uint16_t **src = (const uint16_t **) _src;
  509. uint16_t *dstU = (uint16_t *) _dstU;
  510. uint16_t *dstV = (uint16_t *) _dstV;
  511. for (i = 0; i < width; i++) {
  512. int g = AV_RB16(src[0] + i);
  513. int b = AV_RB16(src[1] + i);
  514. int r = AV_RB16(src[2] + i);
  515. dstU[i] = (RU * r + GU * g + BU * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
  516. dstV[i] = (RV * r + GV * g + BV * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
  517. }
  518. }
  519. av_cold void ff_sws_init_input_funcs(SwsContext *c)
  520. {
  521. enum PixelFormat srcFormat = c->srcFormat;
  522. c->chrToYV12 = NULL;
  523. switch(srcFormat) {
  524. case PIX_FMT_YUYV422 : c->chrToYV12 = yuy2ToUV_c; break;
  525. case PIX_FMT_UYVY422 : c->chrToYV12 = uyvyToUV_c; break;
  526. case PIX_FMT_NV12 : c->chrToYV12 = nv12ToUV_c; break;
  527. case PIX_FMT_NV21 : c->chrToYV12 = nv21ToUV_c; break;
  528. case PIX_FMT_RGB8 :
  529. case PIX_FMT_BGR8 :
  530. case PIX_FMT_PAL8 :
  531. case PIX_FMT_BGR4_BYTE:
  532. case PIX_FMT_RGB4_BYTE: c->chrToYV12 = palToUV_c; break;
  533. case PIX_FMT_GBRP9LE:
  534. case PIX_FMT_GBRP10LE:
  535. case PIX_FMT_GBRP16LE: c->readChrPlanar = planar_rgb16le_to_uv; break;
  536. case PIX_FMT_GBRP9BE:
  537. case PIX_FMT_GBRP10BE:
  538. case PIX_FMT_GBRP16BE: c->readChrPlanar = planar_rgb16be_to_uv; break;
  539. case PIX_FMT_GBRP: c->readChrPlanar = planar_rgb_to_uv; break;
  540. #if HAVE_BIGENDIAN
  541. case PIX_FMT_YUV444P9LE:
  542. case PIX_FMT_YUV422P9LE:
  543. case PIX_FMT_YUV420P9LE:
  544. case PIX_FMT_YUV422P10LE:
  545. case PIX_FMT_YUV444P10LE:
  546. case PIX_FMT_YUV420P10LE:
  547. case PIX_FMT_YUV420P16LE:
  548. case PIX_FMT_YUV422P16LE:
  549. case PIX_FMT_YUV444P16LE: c->chrToYV12 = bswap16UV_c; break;
  550. #else
  551. case PIX_FMT_YUV444P9BE:
  552. case PIX_FMT_YUV422P9BE:
  553. case PIX_FMT_YUV420P9BE:
  554. case PIX_FMT_YUV444P10BE:
  555. case PIX_FMT_YUV422P10BE:
  556. case PIX_FMT_YUV420P10BE:
  557. case PIX_FMT_YUV420P16BE:
  558. case PIX_FMT_YUV422P16BE:
  559. case PIX_FMT_YUV444P16BE: c->chrToYV12 = bswap16UV_c; break;
  560. #endif
  561. }
  562. if (c->chrSrcHSubSample) {
  563. switch(srcFormat) {
  564. case PIX_FMT_RGB48BE : c->chrToYV12 = rgb48BEToUV_half_c; break;
  565. case PIX_FMT_RGB48LE : c->chrToYV12 = rgb48LEToUV_half_c; break;
  566. case PIX_FMT_BGR48BE : c->chrToYV12 = bgr48BEToUV_half_c; break;
  567. case PIX_FMT_BGR48LE : c->chrToYV12 = bgr48LEToUV_half_c; break;
  568. case PIX_FMT_RGB32 : c->chrToYV12 = bgr32ToUV_half_c; break;
  569. case PIX_FMT_RGB32_1 : c->chrToYV12 = bgr321ToUV_half_c; break;
  570. case PIX_FMT_BGR24 : c->chrToYV12 = bgr24ToUV_half_c; break;
  571. case PIX_FMT_BGR565LE: c->chrToYV12 = bgr16leToUV_half_c; break;
  572. case PIX_FMT_BGR565BE: c->chrToYV12 = bgr16beToUV_half_c; break;
  573. case PIX_FMT_BGR555LE: c->chrToYV12 = bgr15leToUV_half_c; break;
  574. case PIX_FMT_BGR555BE: c->chrToYV12 = bgr15beToUV_half_c; break;
  575. case PIX_FMT_BGR444LE: c->chrToYV12 = bgr12leToUV_half_c; break;
  576. case PIX_FMT_BGR444BE: c->chrToYV12 = bgr12beToUV_half_c; break;
  577. case PIX_FMT_BGR32 : c->chrToYV12 = rgb32ToUV_half_c; break;
  578. case PIX_FMT_BGR32_1 : c->chrToYV12 = rgb321ToUV_half_c; break;
  579. case PIX_FMT_RGB24 : c->chrToYV12 = rgb24ToUV_half_c; break;
  580. case PIX_FMT_RGB565LE: c->chrToYV12 = rgb16leToUV_half_c; break;
  581. case PIX_FMT_RGB565BE: c->chrToYV12 = rgb16beToUV_half_c; break;
  582. case PIX_FMT_RGB555LE: c->chrToYV12 = rgb15leToUV_half_c; break;
  583. case PIX_FMT_RGB555BE: c->chrToYV12 = rgb15beToUV_half_c; break;
  584. case PIX_FMT_RGB444LE: c->chrToYV12 = rgb12leToUV_half_c; break;
  585. case PIX_FMT_RGB444BE: c->chrToYV12 = rgb12beToUV_half_c; break;
  586. }
  587. } else {
  588. switch(srcFormat) {
  589. case PIX_FMT_RGB48BE : c->chrToYV12 = rgb48BEToUV_c; break;
  590. case PIX_FMT_RGB48LE : c->chrToYV12 = rgb48LEToUV_c; break;
  591. case PIX_FMT_BGR48BE : c->chrToYV12 = bgr48BEToUV_c; break;
  592. case PIX_FMT_BGR48LE : c->chrToYV12 = bgr48LEToUV_c; break;
  593. case PIX_FMT_RGB32 : c->chrToYV12 = bgr32ToUV_c; break;
  594. case PIX_FMT_RGB32_1 : c->chrToYV12 = bgr321ToUV_c; break;
  595. case PIX_FMT_BGR24 : c->chrToYV12 = bgr24ToUV_c; break;
  596. case PIX_FMT_BGR565LE: c->chrToYV12 = bgr16leToUV_c; break;
  597. case PIX_FMT_BGR565BE: c->chrToYV12 = bgr16beToUV_c; break;
  598. case PIX_FMT_BGR555LE: c->chrToYV12 = bgr15leToUV_c; break;
  599. case PIX_FMT_BGR555BE: c->chrToYV12 = bgr15beToUV_c; break;
  600. case PIX_FMT_BGR444LE: c->chrToYV12 = bgr12leToUV_c; break;
  601. case PIX_FMT_BGR444BE: c->chrToYV12 = bgr12beToUV_c; break;
  602. case PIX_FMT_BGR32 : c->chrToYV12 = rgb32ToUV_c; break;
  603. case PIX_FMT_BGR32_1 : c->chrToYV12 = rgb321ToUV_c; break;
  604. case PIX_FMT_RGB24 : c->chrToYV12 = rgb24ToUV_c; break;
  605. case PIX_FMT_RGB565LE: c->chrToYV12 = rgb16leToUV_c; break;
  606. case PIX_FMT_RGB565BE: c->chrToYV12 = rgb16beToUV_c; break;
  607. case PIX_FMT_RGB555LE: c->chrToYV12 = rgb15leToUV_c; break;
  608. case PIX_FMT_RGB555BE: c->chrToYV12 = rgb15beToUV_c; break;
  609. case PIX_FMT_RGB444LE: c->chrToYV12 = rgb12leToUV_c; break;
  610. case PIX_FMT_RGB444BE: c->chrToYV12 = rgb12beToUV_c; break;
  611. }
  612. }
  613. c->lumToYV12 = NULL;
  614. c->alpToYV12 = NULL;
  615. switch (srcFormat) {
  616. case PIX_FMT_GBRP9LE:
  617. case PIX_FMT_GBRP10LE:
  618. case PIX_FMT_GBRP16LE: c->readLumPlanar = planar_rgb16le_to_y; break;
  619. case PIX_FMT_GBRP9BE:
  620. case PIX_FMT_GBRP10BE:
  621. case PIX_FMT_GBRP16BE: c->readLumPlanar = planar_rgb16be_to_y; break;
  622. case PIX_FMT_GBRP: c->readLumPlanar = planar_rgb_to_y; break;
  623. #if HAVE_BIGENDIAN
  624. case PIX_FMT_YUV444P9LE:
  625. case PIX_FMT_YUV422P9LE:
  626. case PIX_FMT_YUV420P9LE:
  627. case PIX_FMT_YUV444P10LE:
  628. case PIX_FMT_YUV422P10LE:
  629. case PIX_FMT_YUV420P10LE:
  630. case PIX_FMT_YUV420P16LE:
  631. case PIX_FMT_YUV422P16LE:
  632. case PIX_FMT_YUV444P16LE:
  633. case PIX_FMT_GRAY16LE: c->lumToYV12 = bswap16Y_c; break;
  634. #else
  635. case PIX_FMT_YUV444P9BE:
  636. case PIX_FMT_YUV422P9BE:
  637. case PIX_FMT_YUV420P9BE:
  638. case PIX_FMT_YUV444P10BE:
  639. case PIX_FMT_YUV422P10BE:
  640. case PIX_FMT_YUV420P10BE:
  641. case PIX_FMT_YUV420P16BE:
  642. case PIX_FMT_YUV422P16BE:
  643. case PIX_FMT_YUV444P16BE:
  644. case PIX_FMT_GRAY16BE: c->lumToYV12 = bswap16Y_c; break;
  645. #endif
  646. case PIX_FMT_YUYV422 :
  647. case PIX_FMT_Y400A : c->lumToYV12 = yuy2ToY_c; break;
  648. case PIX_FMT_UYVY422 : c->lumToYV12 = uyvyToY_c; break;
  649. case PIX_FMT_BGR24 : c->lumToYV12 = bgr24ToY_c; break;
  650. case PIX_FMT_BGR565LE : c->lumToYV12 = bgr16leToY_c; break;
  651. case PIX_FMT_BGR565BE : c->lumToYV12 = bgr16beToY_c; break;
  652. case PIX_FMT_BGR555LE : c->lumToYV12 = bgr15leToY_c; break;
  653. case PIX_FMT_BGR555BE : c->lumToYV12 = bgr15beToY_c; break;
  654. case PIX_FMT_BGR444LE : c->lumToYV12 = bgr12leToY_c; break;
  655. case PIX_FMT_BGR444BE : c->lumToYV12 = bgr12beToY_c; break;
  656. case PIX_FMT_RGB24 : c->lumToYV12 = rgb24ToY_c; break;
  657. case PIX_FMT_RGB565LE : c->lumToYV12 = rgb16leToY_c; break;
  658. case PIX_FMT_RGB565BE : c->lumToYV12 = rgb16beToY_c; break;
  659. case PIX_FMT_RGB555LE : c->lumToYV12 = rgb15leToY_c; break;
  660. case PIX_FMT_RGB555BE : c->lumToYV12 = rgb15beToY_c; break;
  661. case PIX_FMT_RGB444LE : c->lumToYV12 = rgb12leToY_c; break;
  662. case PIX_FMT_RGB444BE : c->lumToYV12 = rgb12beToY_c; break;
  663. case PIX_FMT_RGB8 :
  664. case PIX_FMT_BGR8 :
  665. case PIX_FMT_PAL8 :
  666. case PIX_FMT_BGR4_BYTE:
  667. case PIX_FMT_RGB4_BYTE: c->lumToYV12 = palToY_c; break;
  668. case PIX_FMT_MONOBLACK: c->lumToYV12 = monoblack2Y_c; break;
  669. case PIX_FMT_MONOWHITE: c->lumToYV12 = monowhite2Y_c; break;
  670. case PIX_FMT_RGB32 : c->lumToYV12 = bgr32ToY_c; break;
  671. case PIX_FMT_RGB32_1: c->lumToYV12 = bgr321ToY_c; break;
  672. case PIX_FMT_BGR32 : c->lumToYV12 = rgb32ToY_c; break;
  673. case PIX_FMT_BGR32_1: c->lumToYV12 = rgb321ToY_c; break;
  674. case PIX_FMT_RGB48BE: c->lumToYV12 = rgb48BEToY_c; break;
  675. case PIX_FMT_RGB48LE: c->lumToYV12 = rgb48LEToY_c; break;
  676. case PIX_FMT_BGR48BE: c->lumToYV12 = bgr48BEToY_c; break;
  677. case PIX_FMT_BGR48LE: c->lumToYV12 = bgr48LEToY_c; break;
  678. }
  679. if (c->alpPixBuf) {
  680. switch (srcFormat) {
  681. case PIX_FMT_BGRA:
  682. case PIX_FMT_RGBA: c->alpToYV12 = rgbaToA_c; break;
  683. case PIX_FMT_ABGR:
  684. case PIX_FMT_ARGB: c->alpToYV12 = abgrToA_c; break;
  685. case PIX_FMT_Y400A: c->alpToYV12 = uyvyToY_c; break;
  686. }
  687. }
  688. }