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.

992 lines
39KB

  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 <inttypes.h>
  21. #include <string.h>
  22. #include <math.h>
  23. #include <stdio.h>
  24. #include "config.h"
  25. #include <assert.h>
  26. #include "swscale.h"
  27. #include "swscale_internal.h"
  28. #include "rgb2rgb.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "libavutil/cpu.h"
  31. #include "libavutil/avutil.h"
  32. #include "libavutil/mathematics.h"
  33. #include "libavutil/bswap.h"
  34. #include "libavutil/pixdesc.h"
  35. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_1)[8][8] = {
  36. { 0, 1, 0, 1, 0, 1, 0, 1,},
  37. { 1, 0, 1, 0, 1, 0, 1, 0,},
  38. { 0, 1, 0, 1, 0, 1, 0, 1,},
  39. { 1, 0, 1, 0, 1, 0, 1, 0,},
  40. { 0, 1, 0, 1, 0, 1, 0, 1,},
  41. { 1, 0, 1, 0, 1, 0, 1, 0,},
  42. { 0, 1, 0, 1, 0, 1, 0, 1,},
  43. { 1, 0, 1, 0, 1, 0, 1, 0,},
  44. };
  45. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_3)[8][8] = {
  46. { 1, 2, 1, 2, 1, 2, 1, 2,},
  47. { 3, 0, 3, 0, 3, 0, 3, 0,},
  48. { 1, 2, 1, 2, 1, 2, 1, 2,},
  49. { 3, 0, 3, 0, 3, 0, 3, 0,},
  50. { 1, 2, 1, 2, 1, 2, 1, 2,},
  51. { 3, 0, 3, 0, 3, 0, 3, 0,},
  52. { 1, 2, 1, 2, 1, 2, 1, 2,},
  53. { 3, 0, 3, 0, 3, 0, 3, 0,},
  54. };
  55. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_64)[8][8] = {
  56. { 18, 34, 30, 46, 17, 33, 29, 45,},
  57. { 50, 2, 62, 14, 49, 1, 61, 13,},
  58. { 26, 42, 22, 38, 25, 41, 21, 37,},
  59. { 58, 10, 54, 6, 57, 9, 53, 5,},
  60. { 16, 32, 28, 44, 19, 35, 31, 47,},
  61. { 48, 0, 60, 12, 51, 3, 63, 15,},
  62. { 24, 40, 20, 36, 27, 43, 23, 39,},
  63. { 56, 8, 52, 4, 59, 11, 55, 7,},
  64. };
  65. extern const uint8_t dither_8x8_128[8][8];
  66. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_256)[8][8] = {
  67. { 72, 136, 120, 184, 68, 132, 116, 180,},
  68. { 200, 8, 248, 56, 196, 4, 244, 52,},
  69. { 104, 168, 88, 152, 100, 164, 84, 148,},
  70. { 232, 40, 216, 24, 228, 36, 212, 20,},
  71. { 64, 128, 102, 176, 76, 140, 124, 188,},
  72. { 192, 0, 240, 48, 204, 12, 252, 60,},
  73. { 96, 160, 80, 144, 108, 172, 92, 156,},
  74. { 224, 32, 208, 16, 236, 44, 220, 28,},
  75. };
  76. #define RGB2YUV_SHIFT 15
  77. #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  78. #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  79. #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  80. #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  81. #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  82. #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  83. #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  84. #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  85. #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  86. static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
  87. {
  88. int i;
  89. uint8_t *ptr = plane + stride*y;
  90. for (i=0; i<height; i++) {
  91. memset(ptr, val, width);
  92. ptr += stride;
  93. }
  94. }
  95. static void copyPlane(const uint8_t *src, int srcStride,
  96. int srcSliceY, int srcSliceH, int width,
  97. uint8_t *dst, int dstStride)
  98. {
  99. dst += dstStride * srcSliceY;
  100. if (dstStride == srcStride && srcStride > 0) {
  101. memcpy(dst, src, srcSliceH * dstStride);
  102. } else {
  103. int i;
  104. for (i=0; i<srcSliceH; i++) {
  105. memcpy(dst, src, width);
  106. src += srcStride;
  107. dst += dstStride;
  108. }
  109. }
  110. }
  111. static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  112. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  113. {
  114. uint8_t *dst = dstParam[1] + dstStride[1]*srcSliceY/2;
  115. copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
  116. dstParam[0], dstStride[0]);
  117. if (c->dstFormat == PIX_FMT_NV12)
  118. interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
  119. else
  120. interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
  121. return srcSliceH;
  122. }
  123. static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  124. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  125. {
  126. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  127. yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
  128. return srcSliceH;
  129. }
  130. static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  131. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  132. {
  133. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  134. yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
  135. return srcSliceH;
  136. }
  137. static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  138. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  139. {
  140. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  141. yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
  142. return srcSliceH;
  143. }
  144. static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  145. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  146. {
  147. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  148. yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
  149. return srcSliceH;
  150. }
  151. static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  152. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  153. {
  154. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  155. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
  156. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
  157. yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  158. if (dstParam[3])
  159. fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  160. return srcSliceH;
  161. }
  162. static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  163. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  164. {
  165. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  166. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
  167. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
  168. yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  169. return srcSliceH;
  170. }
  171. static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  172. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  173. {
  174. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  175. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
  176. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
  177. uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  178. if (dstParam[3])
  179. fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  180. return srcSliceH;
  181. }
  182. static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  183. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  184. {
  185. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  186. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
  187. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
  188. uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  189. return srcSliceH;
  190. }
  191. static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
  192. {
  193. int i;
  194. for (i=0; i<num_pixels; i++)
  195. ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | (src[(i<<1)+1] << 24);
  196. }
  197. static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
  198. {
  199. int i;
  200. for (i=0; i<num_pixels; i++)
  201. ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | src[(i<<1)+1];
  202. }
  203. static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
  204. {
  205. int i;
  206. for (i=0; i<num_pixels; i++) {
  207. //FIXME slow?
  208. dst[0]= palette[src[i<<1]*4+0];
  209. dst[1]= palette[src[i<<1]*4+1];
  210. dst[2]= palette[src[i<<1]*4+2];
  211. dst+= 3;
  212. }
  213. }
  214. static int packed_16bpc_bswap(SwsContext *c, const uint8_t* src[],
  215. int srcStride[], int srcSliceY, int srcSliceH,
  216. uint8_t* dst[], int dstStride[])
  217. {
  218. int i, j;
  219. int srcstr = srcStride[0] >> 1;
  220. int dststr = dstStride[0] >> 1;
  221. uint16_t *dstPtr = (uint16_t *)dst[0];
  222. const uint16_t *srcPtr = (const uint16_t *)src[0];
  223. for (i = 0; i < srcSliceH; i++) {
  224. for (j = 0; j < srcstr; j++) {
  225. dstPtr[j] = av_bswap16(srcPtr[j]);
  226. }
  227. srcPtr += srcstr;
  228. dstPtr += dststr;
  229. }
  230. return srcSliceH;
  231. }
  232. static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  233. int srcSliceH, uint8_t* dst[], int dstStride[])
  234. {
  235. const enum PixelFormat srcFormat= c->srcFormat;
  236. const enum PixelFormat dstFormat= c->dstFormat;
  237. void (*conv)(const uint8_t *src, uint8_t *dst, int num_pixels,
  238. const uint8_t *palette)=NULL;
  239. int i;
  240. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  241. const uint8_t *srcPtr= src[0];
  242. if (srcFormat == PIX_FMT_Y400A) {
  243. switch (dstFormat) {
  244. case PIX_FMT_RGB32 : conv = gray8aToPacked32; break;
  245. case PIX_FMT_BGR32 : conv = gray8aToPacked32; break;
  246. case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
  247. case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
  248. case PIX_FMT_RGB24 : conv = gray8aToPacked24; break;
  249. case PIX_FMT_BGR24 : conv = gray8aToPacked24; break;
  250. }
  251. } else if (usePal(srcFormat)) {
  252. switch (dstFormat) {
  253. case PIX_FMT_RGB32 : conv = sws_convertPalette8ToPacked32; break;
  254. case PIX_FMT_BGR32 : conv = sws_convertPalette8ToPacked32; break;
  255. case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
  256. case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
  257. case PIX_FMT_RGB24 : conv = sws_convertPalette8ToPacked24; break;
  258. case PIX_FMT_BGR24 : conv = sws_convertPalette8ToPacked24; break;
  259. }
  260. }
  261. if (!conv)
  262. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  263. sws_format_name(srcFormat), sws_format_name(dstFormat));
  264. else {
  265. for (i=0; i<srcSliceH; i++) {
  266. conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
  267. srcPtr+= srcStride[0];
  268. dstPtr+= dstStride[0];
  269. }
  270. }
  271. return srcSliceH;
  272. }
  273. #define isRGBA32(x) ( \
  274. (x) == PIX_FMT_ARGB \
  275. || (x) == PIX_FMT_RGBA \
  276. || (x) == PIX_FMT_BGRA \
  277. || (x) == PIX_FMT_ABGR \
  278. )
  279. /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
  280. static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  281. int srcSliceH, uint8_t* dst[], int dstStride[])
  282. {
  283. const enum PixelFormat srcFormat= c->srcFormat;
  284. const enum PixelFormat dstFormat= c->dstFormat;
  285. const int srcBpp= (c->srcFormatBpp + 7) >> 3;
  286. const int dstBpp= (c->dstFormatBpp + 7) >> 3;
  287. const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
  288. const int dstId= c->dstFormatBpp >> 2;
  289. void (*conv)(const uint8_t *src, uint8_t *dst, int src_size)=NULL;
  290. #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
  291. if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
  292. if ( CONV_IS(ABGR, RGBA)
  293. || CONV_IS(ARGB, BGRA)
  294. || CONV_IS(BGRA, ARGB)
  295. || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
  296. else if (CONV_IS(ABGR, ARGB)
  297. || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
  298. else if (CONV_IS(ABGR, BGRA)
  299. || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
  300. else if (CONV_IS(BGRA, RGBA)
  301. || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
  302. else if (CONV_IS(BGRA, ABGR)
  303. || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
  304. } else
  305. /* BGR -> BGR */
  306. if ( (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
  307. || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
  308. switch(srcId | (dstId<<4)) {
  309. case 0x34: conv= rgb16to15; break;
  310. case 0x36: conv= rgb24to15; break;
  311. case 0x38: conv= rgb32to15; break;
  312. case 0x43: conv= rgb15to16; break;
  313. case 0x46: conv= rgb24to16; break;
  314. case 0x48: conv= rgb32to16; break;
  315. case 0x63: conv= rgb15to24; break;
  316. case 0x64: conv= rgb16to24; break;
  317. case 0x68: conv= rgb32to24; break;
  318. case 0x83: conv= rgb15to32; break;
  319. case 0x84: conv= rgb16to32; break;
  320. case 0x86: conv= rgb24to32; break;
  321. }
  322. } else if ( (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
  323. || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
  324. switch(srcId | (dstId<<4)) {
  325. case 0x33: conv= rgb15tobgr15; break;
  326. case 0x34: conv= rgb16tobgr15; break;
  327. case 0x36: conv= rgb24tobgr15; break;
  328. case 0x38: conv= rgb32tobgr15; break;
  329. case 0x43: conv= rgb15tobgr16; break;
  330. case 0x44: conv= rgb16tobgr16; break;
  331. case 0x46: conv= rgb24tobgr16; break;
  332. case 0x48: conv= rgb32tobgr16; break;
  333. case 0x63: conv= rgb15tobgr24; break;
  334. case 0x64: conv= rgb16tobgr24; break;
  335. case 0x66: conv= rgb24tobgr24; break;
  336. case 0x68: conv= rgb32tobgr24; break;
  337. case 0x83: conv= rgb15tobgr32; break;
  338. case 0x84: conv= rgb16tobgr32; break;
  339. case 0x86: conv= rgb24tobgr32; break;
  340. }
  341. }
  342. if (!conv) {
  343. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  344. sws_format_name(srcFormat), sws_format_name(dstFormat));
  345. } else {
  346. const uint8_t *srcPtr= src[0];
  347. uint8_t *dstPtr= dst[0];
  348. if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
  349. srcPtr += ALT32_CORR;
  350. if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
  351. dstPtr += ALT32_CORR;
  352. if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0 && !(srcStride[0] % srcBpp))
  353. conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
  354. else {
  355. int i;
  356. dstPtr += dstStride[0]*srcSliceY;
  357. for (i=0; i<srcSliceH; i++) {
  358. conv(srcPtr, dstPtr, c->srcW*srcBpp);
  359. srcPtr+= srcStride[0];
  360. dstPtr+= dstStride[0];
  361. }
  362. }
  363. }
  364. return srcSliceH;
  365. }
  366. static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  367. int srcSliceH, uint8_t* dst[], int dstStride[])
  368. {
  369. rgb24toyv12(
  370. src[0],
  371. dst[0]+ srcSliceY *dstStride[0],
  372. dst[1]+(srcSliceY>>1)*dstStride[1],
  373. dst[2]+(srcSliceY>>1)*dstStride[2],
  374. c->srcW, srcSliceH,
  375. dstStride[0], dstStride[1], srcStride[0]);
  376. if (dst[3])
  377. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  378. return srcSliceH;
  379. }
  380. static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  381. int srcSliceH, uint8_t* dst[], int dstStride[])
  382. {
  383. copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
  384. dst[0], dstStride[0]);
  385. planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
  386. srcSliceH >> 2, srcStride[1], dstStride[1]);
  387. planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
  388. srcSliceH >> 2, srcStride[2], dstStride[2]);
  389. if (dst[3])
  390. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  391. return srcSliceH;
  392. }
  393. /* unscaled copy like stuff (assumes nearly identical formats) */
  394. static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  395. int srcSliceH, uint8_t* dst[], int dstStride[])
  396. {
  397. if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
  398. memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
  399. else {
  400. int i;
  401. const uint8_t *srcPtr= src[0];
  402. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  403. int length=0;
  404. /* universal length finder */
  405. while(length+c->srcW <= FFABS(dstStride[0])
  406. && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
  407. assert(length!=0);
  408. for (i=0; i<srcSliceH; i++) {
  409. memcpy(dstPtr, srcPtr, length);
  410. srcPtr+= srcStride[0];
  411. dstPtr+= dstStride[0];
  412. }
  413. }
  414. return srcSliceH;
  415. }
  416. #define clip9(x) av_clip_uintp2(x, 9)
  417. #define clip10(x) av_clip_uintp2(x, 10)
  418. #define DITHER_COPY(dst, dstStride, wfunc, src, srcStride, rfunc, dithers, shift, clip) \
  419. for (i = 0; i < height; i++) { \
  420. const uint8_t *dither = dithers[i & 7]; \
  421. for (j = 0; j < length - 7; j += 8) { \
  422. wfunc(&dst[j + 0], clip((rfunc(&src[j + 0]) + dither[0]) >> shift)); \
  423. wfunc(&dst[j + 1], clip((rfunc(&src[j + 1]) + dither[1]) >> shift)); \
  424. wfunc(&dst[j + 2], clip((rfunc(&src[j + 2]) + dither[2]) >> shift)); \
  425. wfunc(&dst[j + 3], clip((rfunc(&src[j + 3]) + dither[3]) >> shift)); \
  426. wfunc(&dst[j + 4], clip((rfunc(&src[j + 4]) + dither[4]) >> shift)); \
  427. wfunc(&dst[j + 5], clip((rfunc(&src[j + 5]) + dither[5]) >> shift)); \
  428. wfunc(&dst[j + 6], clip((rfunc(&src[j + 6]) + dither[6]) >> shift)); \
  429. wfunc(&dst[j + 7], clip((rfunc(&src[j + 7]) + dither[7]) >> shift)); \
  430. } \
  431. for (; j < length; j++) \
  432. wfunc(&dst[j], (rfunc(&src[j]) + dither[j & 7]) >> shift); \
  433. dst += dstStride; \
  434. src += srcStride; \
  435. }
  436. static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  437. int srcSliceH, uint8_t* dst[], int dstStride[])
  438. {
  439. int plane, i, j;
  440. for (plane=0; plane<4; plane++) {
  441. int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
  442. int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
  443. int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
  444. const uint8_t *srcPtr= src[plane];
  445. uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
  446. if (!dst[plane]) continue;
  447. // ignore palette for GRAY8
  448. if (plane == 1 && !dst[2]) continue;
  449. if (!src[plane] || (plane == 1 && !src[2])) {
  450. if(is16BPS(c->dstFormat))
  451. length*=2;
  452. fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
  453. } else {
  454. if(is9_OR_10BPS(c->srcFormat)) {
  455. const int src_depth = av_pix_fmt_descriptors[c->srcFormat].comp[plane].depth_minus1+1;
  456. const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
  457. const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
  458. if (is16BPS(c->dstFormat)) {
  459. uint16_t *dstPtr2 = (uint16_t*)dstPtr;
  460. #define COPY9_OR_10TO16(rfunc, wfunc) \
  461. for (i = 0; i < height; i++) { \
  462. for (j = 0; j < length; j++) { \
  463. int srcpx = rfunc(&srcPtr2[j]); \
  464. wfunc(&dstPtr2[j], (srcpx<<(16-src_depth)) | (srcpx>>(2*src_depth-16))); \
  465. } \
  466. dstPtr2 += dstStride[plane]/2; \
  467. srcPtr2 += srcStride[plane]/2; \
  468. }
  469. if (isBE(c->dstFormat)) {
  470. if (isBE(c->srcFormat)) {
  471. COPY9_OR_10TO16(AV_RB16, AV_WB16);
  472. } else {
  473. COPY9_OR_10TO16(AV_RL16, AV_WB16);
  474. }
  475. } else {
  476. if (isBE(c->srcFormat)) {
  477. COPY9_OR_10TO16(AV_RB16, AV_WL16);
  478. } else {
  479. COPY9_OR_10TO16(AV_RL16, AV_WL16);
  480. }
  481. }
  482. } else if (is9_OR_10BPS(c->dstFormat)) {
  483. uint16_t *dstPtr2 = (uint16_t*)dstPtr;
  484. #define COPY9_OR_10TO9_OR_10(loop) \
  485. for (i = 0; i < height; i++) { \
  486. for (j = 0; j < length; j++) { \
  487. loop; \
  488. } \
  489. dstPtr2 += dstStride[plane]/2; \
  490. srcPtr2 += srcStride[plane]/2; \
  491. }
  492. #define COPY9_OR_10TO9_OR_10_2(rfunc, wfunc) \
  493. if (dst_depth > src_depth) { \
  494. COPY9_OR_10TO9_OR_10(int srcpx = rfunc(&srcPtr2[j]); \
  495. wfunc(&dstPtr2[j], (srcpx << 1) | (srcpx >> 9))); \
  496. } else if (dst_depth < src_depth) { \
  497. DITHER_COPY(dstPtr2, dstStride[plane]/2, wfunc, \
  498. srcPtr2, srcStride[plane]/2, rfunc, \
  499. dither_8x8_1, 1, clip9); \
  500. } else { \
  501. COPY9_OR_10TO9_OR_10(wfunc(&dstPtr2[j], rfunc(&srcPtr2[j]))); \
  502. }
  503. if (isBE(c->dstFormat)) {
  504. if (isBE(c->srcFormat)) {
  505. COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WB16);
  506. } else {
  507. COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WB16);
  508. }
  509. } else {
  510. if (isBE(c->srcFormat)) {
  511. COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WL16);
  512. } else {
  513. COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WL16);
  514. }
  515. }
  516. } else {
  517. #define W8(a, b) { *(a) = (b); }
  518. #define COPY9_OR_10TO8(rfunc) \
  519. if (src_depth == 9) { \
  520. DITHER_COPY(dstPtr, dstStride[plane], W8, \
  521. srcPtr2, srcStride[plane]/2, rfunc, \
  522. dither_8x8_1, 1, av_clip_uint8); \
  523. } else { \
  524. DITHER_COPY(dstPtr, dstStride[plane], W8, \
  525. srcPtr2, srcStride[plane]/2, rfunc, \
  526. dither_8x8_3, 2, av_clip_uint8); \
  527. }
  528. if (isBE(c->srcFormat)) {
  529. COPY9_OR_10TO8(AV_RB16);
  530. } else {
  531. COPY9_OR_10TO8(AV_RL16);
  532. }
  533. }
  534. } else if(is9_OR_10BPS(c->dstFormat)) {
  535. const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
  536. uint16_t *dstPtr2 = (uint16_t*)dstPtr;
  537. if (is16BPS(c->srcFormat)) {
  538. const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
  539. #define COPY16TO9_OR_10(rfunc, wfunc) \
  540. if (dst_depth == 9) { \
  541. DITHER_COPY(dstPtr2, dstStride[plane]/2, wfunc, \
  542. srcPtr2, srcStride[plane]/2, rfunc, \
  543. dither_8x8_128, 7, clip9); \
  544. } else { \
  545. DITHER_COPY(dstPtr2, dstStride[plane]/2, wfunc, \
  546. srcPtr2, srcStride[plane]/2, rfunc, \
  547. dither_8x8_64, 6, clip10); \
  548. }
  549. if (isBE(c->dstFormat)) {
  550. if (isBE(c->srcFormat)) {
  551. COPY16TO9_OR_10(AV_RB16, AV_WB16);
  552. } else {
  553. COPY16TO9_OR_10(AV_RL16, AV_WB16);
  554. }
  555. } else {
  556. if (isBE(c->srcFormat)) {
  557. COPY16TO9_OR_10(AV_RB16, AV_WL16);
  558. } else {
  559. COPY16TO9_OR_10(AV_RL16, AV_WL16);
  560. }
  561. }
  562. } else /* 8bit */ {
  563. #define COPY8TO9_OR_10(wfunc) \
  564. for (i = 0; i < height; i++) { \
  565. for (j = 0; j < length; j++) { \
  566. const int srcpx = srcPtr[j]; \
  567. wfunc(&dstPtr2[j], (srcpx<<(dst_depth-8)) | (srcpx >> (16-dst_depth))); \
  568. } \
  569. dstPtr2 += dstStride[plane]/2; \
  570. srcPtr += srcStride[plane]; \
  571. }
  572. if (isBE(c->dstFormat)) {
  573. COPY8TO9_OR_10(AV_WB16);
  574. } else {
  575. COPY8TO9_OR_10(AV_WL16);
  576. }
  577. }
  578. } else if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
  579. const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
  580. #define COPY16TO8(rfunc) \
  581. DITHER_COPY(dstPtr, dstStride[plane], W8, \
  582. srcPtr2, srcStride[plane]/2, rfunc, \
  583. dither_8x8_256, 8, av_clip_uint8);
  584. if (isBE(c->srcFormat)) {
  585. COPY16TO8(AV_RB16);
  586. } else {
  587. COPY16TO8(AV_RL16);
  588. }
  589. } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
  590. for (i=0; i<height; i++) {
  591. for (j=0; j<length; j++) {
  592. dstPtr[ j<<1 ] = srcPtr[j];
  593. dstPtr[(j<<1)+1] = srcPtr[j];
  594. }
  595. srcPtr+= srcStride[plane];
  596. dstPtr+= dstStride[plane];
  597. }
  598. } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
  599. && isBE(c->srcFormat) != isBE(c->dstFormat)) {
  600. for (i=0; i<height; i++) {
  601. for (j=0; j<length; j++)
  602. ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
  603. srcPtr+= srcStride[plane];
  604. dstPtr+= dstStride[plane];
  605. }
  606. } else if (dstStride[plane] == srcStride[plane] &&
  607. srcStride[plane] > 0 && srcStride[plane] == length) {
  608. memcpy(dst[plane] + dstStride[plane]*y, src[plane],
  609. height*dstStride[plane]);
  610. } else {
  611. if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
  612. length*=2;
  613. for (i=0; i<height; i++) {
  614. memcpy(dstPtr, srcPtr, length);
  615. srcPtr+= srcStride[plane];
  616. dstPtr+= dstStride[plane];
  617. }
  618. }
  619. }
  620. }
  621. return srcSliceH;
  622. }
  623. void ff_get_unscaled_swscale(SwsContext *c)
  624. {
  625. const enum PixelFormat srcFormat = c->srcFormat;
  626. const enum PixelFormat dstFormat = c->dstFormat;
  627. const int flags = c->flags;
  628. const int dstH = c->dstH;
  629. int needsDither;
  630. needsDither= isAnyRGB(dstFormat)
  631. && c->dstFormatBpp < 24
  632. && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
  633. /* yv12_to_nv12 */
  634. if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
  635. c->swScale= planarToNv12Wrapper;
  636. }
  637. /* yuv2bgr */
  638. if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
  639. && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
  640. c->swScale= ff_yuv2rgb_get_func_ptr(c);
  641. }
  642. if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
  643. c->swScale= yvu9ToYv12Wrapper;
  644. }
  645. /* bgr24toYV12 */
  646. if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
  647. c->swScale= bgr24ToYv12Wrapper;
  648. /* bswap 16 bits per component packed formats */
  649. if ((srcFormat == PIX_FMT_RGB48LE && dstFormat == PIX_FMT_RGB48BE) ||
  650. (srcFormat == PIX_FMT_RGB48BE && dstFormat == PIX_FMT_RGB48LE) ||
  651. (srcFormat == PIX_FMT_BGR48LE && dstFormat == PIX_FMT_BGR48BE) ||
  652. (srcFormat == PIX_FMT_BGR48BE && dstFormat == PIX_FMT_BGR48LE) ||
  653. (srcFormat == PIX_FMT_GRAY16LE && dstFormat == PIX_FMT_GRAY16BE) ||
  654. (srcFormat == PIX_FMT_GRAY16BE && dstFormat == PIX_FMT_GRAY16LE))
  655. c->swScale = packed_16bpc_bswap;
  656. /* RGB/BGR -> RGB/BGR (no dither needed forms) */
  657. if ( isAnyRGB(srcFormat)
  658. && isAnyRGB(dstFormat)
  659. && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
  660. && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
  661. && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
  662. && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
  663. && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
  664. && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
  665. && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
  666. && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
  667. && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
  668. && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
  669. && srcFormat != PIX_FMT_BGR48LE && dstFormat != PIX_FMT_BGR48LE
  670. && srcFormat != PIX_FMT_BGR48BE && dstFormat != PIX_FMT_BGR48BE
  671. && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
  672. c->swScale= rgbToRgbWrapper;
  673. if ((usePal(srcFormat) && (
  674. dstFormat == PIX_FMT_RGB32 ||
  675. dstFormat == PIX_FMT_RGB32_1 ||
  676. dstFormat == PIX_FMT_RGB24 ||
  677. dstFormat == PIX_FMT_BGR32 ||
  678. dstFormat == PIX_FMT_BGR32_1 ||
  679. dstFormat == PIX_FMT_BGR24)))
  680. c->swScale= palToRgbWrapper;
  681. if (srcFormat == PIX_FMT_YUV422P) {
  682. if (dstFormat == PIX_FMT_YUYV422)
  683. c->swScale= yuv422pToYuy2Wrapper;
  684. else if (dstFormat == PIX_FMT_UYVY422)
  685. c->swScale= yuv422pToUyvyWrapper;
  686. }
  687. /* LQ converters if -sws 0 or -sws 4*/
  688. if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
  689. /* yv12_to_yuy2 */
  690. if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
  691. if (dstFormat == PIX_FMT_YUYV422)
  692. c->swScale= planarToYuy2Wrapper;
  693. else if (dstFormat == PIX_FMT_UYVY422)
  694. c->swScale= planarToUyvyWrapper;
  695. }
  696. }
  697. if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  698. c->swScale= yuyvToYuv420Wrapper;
  699. if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  700. c->swScale= uyvyToYuv420Wrapper;
  701. if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
  702. c->swScale= yuyvToYuv422Wrapper;
  703. if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
  704. c->swScale= uyvyToYuv422Wrapper;
  705. /* simple copy */
  706. if ( srcFormat == dstFormat
  707. || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
  708. || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
  709. || (isPlanarYUV(srcFormat) && isGray(dstFormat))
  710. || (isPlanarYUV(dstFormat) && isGray(srcFormat))
  711. || (isGray(dstFormat) && isGray(srcFormat))
  712. || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
  713. && c->chrDstHSubSample == c->chrSrcHSubSample
  714. && c->chrDstVSubSample == c->chrSrcVSubSample
  715. && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
  716. && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
  717. {
  718. if (isPacked(c->srcFormat))
  719. c->swScale= packedCopyWrapper;
  720. else /* Planar YUV or gray */
  721. c->swScale= planarCopyWrapper;
  722. }
  723. if (ARCH_BFIN)
  724. ff_bfin_get_unscaled_swscale(c);
  725. if (HAVE_ALTIVEC)
  726. ff_swscale_get_unscaled_altivec(c);
  727. }
  728. static void reset_ptr(const uint8_t* src[], int format)
  729. {
  730. if(!isALPHA(format))
  731. src[3]=NULL;
  732. if (!isPlanar(format)) {
  733. src[3]=src[2]=NULL;
  734. if (!usePal(format))
  735. src[1]= NULL;
  736. }
  737. }
  738. static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
  739. const int linesizes[4])
  740. {
  741. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  742. int i;
  743. for (i = 0; i < 4; i++) {
  744. int plane = desc->comp[i].plane;
  745. if (!data[plane] || !linesizes[plane])
  746. return 0;
  747. }
  748. return 1;
  749. }
  750. /**
  751. * swscale wrapper, so we don't need to export the SwsContext.
  752. * Assumes planar YUV to be in YUV order instead of YVU.
  753. */
  754. int attribute_align_arg sws_scale(struct SwsContext *c, const uint8_t* const srcSlice[],
  755. const int srcStride[], int srcSliceY, int srcSliceH,
  756. uint8_t* const dst[], const int dstStride[])
  757. {
  758. int i;
  759. const uint8_t* src2[4]= {srcSlice[0], srcSlice[1], srcSlice[2], srcSlice[3]};
  760. uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
  761. // do not mess up sliceDir if we have a "trailing" 0-size slice
  762. if (srcSliceH == 0)
  763. return 0;
  764. if (!check_image_pointers(srcSlice, c->srcFormat, srcStride)) {
  765. av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
  766. return 0;
  767. }
  768. if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
  769. av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
  770. return 0;
  771. }
  772. if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
  773. av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
  774. return 0;
  775. }
  776. if (c->sliceDir == 0) {
  777. if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
  778. }
  779. if (usePal(c->srcFormat)) {
  780. for (i=0; i<256; i++) {
  781. int p, r, g, b,y,u,v;
  782. if(c->srcFormat == PIX_FMT_PAL8) {
  783. p=((const uint32_t*)(srcSlice[1]))[i];
  784. r= (p>>16)&0xFF;
  785. g= (p>> 8)&0xFF;
  786. b= p &0xFF;
  787. } else if(c->srcFormat == PIX_FMT_RGB8) {
  788. r= (i>>5 )*36;
  789. g= ((i>>2)&7)*36;
  790. b= (i&3 )*85;
  791. } else if(c->srcFormat == PIX_FMT_BGR8) {
  792. b= (i>>6 )*85;
  793. g= ((i>>3)&7)*36;
  794. r= (i&7 )*36;
  795. } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
  796. r= (i>>3 )*255;
  797. g= ((i>>1)&3)*85;
  798. b= (i&1 )*255;
  799. } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_Y400A) {
  800. r = g = b = i;
  801. } else {
  802. assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
  803. b= (i>>3 )*255;
  804. g= ((i>>1)&3)*85;
  805. r= (i&1 )*255;
  806. }
  807. y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  808. u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  809. v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  810. c->pal_yuv[i]= y + (u<<8) + (v<<16);
  811. switch(c->dstFormat) {
  812. case PIX_FMT_BGR32:
  813. #if !HAVE_BIGENDIAN
  814. case PIX_FMT_RGB24:
  815. #endif
  816. c->pal_rgb[i]= r + (g<<8) + (b<<16);
  817. break;
  818. case PIX_FMT_BGR32_1:
  819. #if HAVE_BIGENDIAN
  820. case PIX_FMT_BGR24:
  821. #endif
  822. c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
  823. break;
  824. case PIX_FMT_RGB32_1:
  825. #if HAVE_BIGENDIAN
  826. case PIX_FMT_RGB24:
  827. #endif
  828. c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
  829. break;
  830. case PIX_FMT_RGB32:
  831. #if !HAVE_BIGENDIAN
  832. case PIX_FMT_BGR24:
  833. #endif
  834. default:
  835. c->pal_rgb[i]= b + (g<<8) + (r<<16);
  836. }
  837. }
  838. }
  839. // copy strides, so they can safely be modified
  840. if (c->sliceDir == 1) {
  841. // slices go from top to bottom
  842. int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
  843. int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
  844. reset_ptr(src2, c->srcFormat);
  845. reset_ptr((const uint8_t**)dst2, c->dstFormat);
  846. /* reset slice direction at end of frame */
  847. if (srcSliceY + srcSliceH == c->srcH)
  848. c->sliceDir = 0;
  849. return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
  850. } else {
  851. // slices go from bottom to top => we flip the image internally
  852. int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
  853. int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
  854. src2[0] += (srcSliceH-1)*srcStride[0];
  855. if (!usePal(c->srcFormat))
  856. src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
  857. src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
  858. src2[3] += (srcSliceH-1)*srcStride[3];
  859. dst2[0] += ( c->dstH -1)*dstStride[0];
  860. dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
  861. dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
  862. dst2[3] += ( c->dstH -1)*dstStride[3];
  863. reset_ptr(src2, c->srcFormat);
  864. reset_ptr((const uint8_t**)dst2, c->dstFormat);
  865. /* reset slice direction at end of frame */
  866. if (!srcSliceY)
  867. c->sliceDir = 0;
  868. return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
  869. }
  870. }
  871. /* Convert the palette to the same packed 32-bit format as the palette */
  872. void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
  873. {
  874. int i;
  875. for (i=0; i<num_pixels; i++)
  876. ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
  877. }
  878. /* Palette format: ABCD -> dst format: ABC */
  879. void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
  880. {
  881. int i;
  882. for (i=0; i<num_pixels; i++) {
  883. //FIXME slow?
  884. dst[0]= palette[src[i]*4+0];
  885. dst[1]= palette[src[i]*4+1];
  886. dst[2]= palette[src[i]*4+2];
  887. dst+= 3;
  888. }
  889. }