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.

960 lines
38KB

  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 palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  215. int srcSliceH, uint8_t* dst[], int dstStride[])
  216. {
  217. const enum PixelFormat srcFormat= c->srcFormat;
  218. const enum PixelFormat dstFormat= c->dstFormat;
  219. void (*conv)(const uint8_t *src, uint8_t *dst, int num_pixels,
  220. const uint8_t *palette)=NULL;
  221. int i;
  222. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  223. const uint8_t *srcPtr= src[0];
  224. if (srcFormat == PIX_FMT_Y400A) {
  225. switch (dstFormat) {
  226. case PIX_FMT_RGB32 : conv = gray8aToPacked32; break;
  227. case PIX_FMT_BGR32 : conv = gray8aToPacked32; break;
  228. case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
  229. case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
  230. case PIX_FMT_RGB24 : conv = gray8aToPacked24; break;
  231. case PIX_FMT_BGR24 : conv = gray8aToPacked24; break;
  232. }
  233. } else if (usePal(srcFormat)) {
  234. switch (dstFormat) {
  235. case PIX_FMT_RGB32 : conv = sws_convertPalette8ToPacked32; break;
  236. case PIX_FMT_BGR32 : conv = sws_convertPalette8ToPacked32; break;
  237. case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
  238. case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
  239. case PIX_FMT_RGB24 : conv = sws_convertPalette8ToPacked24; break;
  240. case PIX_FMT_BGR24 : conv = sws_convertPalette8ToPacked24; break;
  241. }
  242. }
  243. if (!conv)
  244. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  245. sws_format_name(srcFormat), sws_format_name(dstFormat));
  246. else {
  247. for (i=0; i<srcSliceH; i++) {
  248. conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
  249. srcPtr+= srcStride[0];
  250. dstPtr+= dstStride[0];
  251. }
  252. }
  253. return srcSliceH;
  254. }
  255. #define isRGBA32(x) ( \
  256. (x) == PIX_FMT_ARGB \
  257. || (x) == PIX_FMT_RGBA \
  258. || (x) == PIX_FMT_BGRA \
  259. || (x) == PIX_FMT_ABGR \
  260. )
  261. /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
  262. static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  263. int srcSliceH, uint8_t* dst[], int dstStride[])
  264. {
  265. const enum PixelFormat srcFormat= c->srcFormat;
  266. const enum PixelFormat dstFormat= c->dstFormat;
  267. const int srcBpp= (c->srcFormatBpp + 7) >> 3;
  268. const int dstBpp= (c->dstFormatBpp + 7) >> 3;
  269. const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
  270. const int dstId= c->dstFormatBpp >> 2;
  271. void (*conv)(const uint8_t *src, uint8_t *dst, int src_size)=NULL;
  272. #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
  273. if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
  274. if ( CONV_IS(ABGR, RGBA)
  275. || CONV_IS(ARGB, BGRA)
  276. || CONV_IS(BGRA, ARGB)
  277. || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
  278. else if (CONV_IS(ABGR, ARGB)
  279. || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
  280. else if (CONV_IS(ABGR, BGRA)
  281. || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
  282. else if (CONV_IS(BGRA, RGBA)
  283. || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
  284. else if (CONV_IS(BGRA, ABGR)
  285. || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
  286. } else
  287. /* BGR -> BGR */
  288. if ( (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
  289. || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
  290. switch(srcId | (dstId<<4)) {
  291. case 0x34: conv= rgb16to15; break;
  292. case 0x36: conv= rgb24to15; break;
  293. case 0x38: conv= rgb32to15; break;
  294. case 0x43: conv= rgb15to16; break;
  295. case 0x46: conv= rgb24to16; break;
  296. case 0x48: conv= rgb32to16; break;
  297. case 0x63: conv= rgb15to24; break;
  298. case 0x64: conv= rgb16to24; break;
  299. case 0x68: conv= rgb32to24; break;
  300. case 0x83: conv= rgb15to32; break;
  301. case 0x84: conv= rgb16to32; break;
  302. case 0x86: conv= rgb24to32; break;
  303. }
  304. } else if ( (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
  305. || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
  306. switch(srcId | (dstId<<4)) {
  307. case 0x33: conv= rgb15tobgr15; break;
  308. case 0x34: conv= rgb16tobgr15; break;
  309. case 0x36: conv= rgb24tobgr15; break;
  310. case 0x38: conv= rgb32tobgr15; break;
  311. case 0x43: conv= rgb15tobgr16; break;
  312. case 0x44: conv= rgb16tobgr16; break;
  313. case 0x46: conv= rgb24tobgr16; break;
  314. case 0x48: conv= rgb32tobgr16; break;
  315. case 0x63: conv= rgb15tobgr24; break;
  316. case 0x64: conv= rgb16tobgr24; break;
  317. case 0x66: conv= rgb24tobgr24; break;
  318. case 0x68: conv= rgb32tobgr24; break;
  319. case 0x83: conv= rgb15tobgr32; break;
  320. case 0x84: conv= rgb16tobgr32; break;
  321. case 0x86: conv= rgb24tobgr32; break;
  322. }
  323. }
  324. if (!conv) {
  325. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  326. sws_format_name(srcFormat), sws_format_name(dstFormat));
  327. } else {
  328. const uint8_t *srcPtr= src[0];
  329. uint8_t *dstPtr= dst[0];
  330. if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
  331. srcPtr += ALT32_CORR;
  332. if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
  333. dstPtr += ALT32_CORR;
  334. if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
  335. conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
  336. else {
  337. int i;
  338. dstPtr += dstStride[0]*srcSliceY;
  339. for (i=0; i<srcSliceH; i++) {
  340. conv(srcPtr, dstPtr, c->srcW*srcBpp);
  341. srcPtr+= srcStride[0];
  342. dstPtr+= dstStride[0];
  343. }
  344. }
  345. }
  346. return srcSliceH;
  347. }
  348. static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  349. int srcSliceH, uint8_t* dst[], int dstStride[])
  350. {
  351. rgb24toyv12(
  352. src[0],
  353. dst[0]+ srcSliceY *dstStride[0],
  354. dst[1]+(srcSliceY>>1)*dstStride[1],
  355. dst[2]+(srcSliceY>>1)*dstStride[2],
  356. c->srcW, srcSliceH,
  357. dstStride[0], dstStride[1], srcStride[0]);
  358. if (dst[3])
  359. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  360. return srcSliceH;
  361. }
  362. static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  363. int srcSliceH, uint8_t* dst[], int dstStride[])
  364. {
  365. copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
  366. dst[0], dstStride[0]);
  367. planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
  368. srcSliceH >> 2, srcStride[1], dstStride[1]);
  369. planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
  370. srcSliceH >> 2, srcStride[2], dstStride[2]);
  371. if (dst[3])
  372. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  373. return srcSliceH;
  374. }
  375. /* unscaled copy like stuff (assumes nearly identical formats) */
  376. static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  377. int srcSliceH, uint8_t* dst[], int dstStride[])
  378. {
  379. if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
  380. memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
  381. else {
  382. int i;
  383. const uint8_t *srcPtr= src[0];
  384. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  385. int length=0;
  386. /* universal length finder */
  387. while(length+c->srcW <= FFABS(dstStride[0])
  388. && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
  389. assert(length!=0);
  390. for (i=0; i<srcSliceH; i++) {
  391. memcpy(dstPtr, srcPtr, length);
  392. srcPtr+= srcStride[0];
  393. dstPtr+= dstStride[0];
  394. }
  395. }
  396. return srcSliceH;
  397. }
  398. #define DITHER_COPY(dst, dstStride, wfunc, src, srcStride, rfunc, dithers, shift) \
  399. for (i = 0; i < height; i++) { \
  400. const uint8_t *dither = dithers[i & 7]; \
  401. for (j = 0; j < length - 7; j += 8) { \
  402. wfunc(&dst[j + 0], (rfunc(&src[j + 0]) + dither[0]) >> shift); \
  403. wfunc(&dst[j + 1], (rfunc(&src[j + 1]) + dither[1]) >> shift); \
  404. wfunc(&dst[j + 2], (rfunc(&src[j + 2]) + dither[2]) >> shift); \
  405. wfunc(&dst[j + 3], (rfunc(&src[j + 3]) + dither[3]) >> shift); \
  406. wfunc(&dst[j + 4], (rfunc(&src[j + 4]) + dither[4]) >> shift); \
  407. wfunc(&dst[j + 5], (rfunc(&src[j + 5]) + dither[5]) >> shift); \
  408. wfunc(&dst[j + 6], (rfunc(&src[j + 6]) + dither[6]) >> shift); \
  409. wfunc(&dst[j + 7], (rfunc(&src[j + 7]) + dither[7]) >> shift); \
  410. } \
  411. for (; j < length; j++) \
  412. wfunc(&dst[j], (rfunc(&src[j]) + dither[j & 7]) >> shift); \
  413. dst += dstStride; \
  414. src += srcStride; \
  415. }
  416. static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  417. int srcSliceH, uint8_t* dst[], int dstStride[])
  418. {
  419. int plane, i, j;
  420. for (plane=0; plane<4; plane++) {
  421. int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
  422. int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
  423. int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
  424. const uint8_t *srcPtr= src[plane];
  425. uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
  426. if (!dst[plane]) continue;
  427. // ignore palette for GRAY8
  428. if (plane == 1 && !dst[2]) continue;
  429. if (!src[plane] || (plane == 1 && !src[2])) {
  430. if(is16BPS(c->dstFormat))
  431. length*=2;
  432. fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
  433. } else {
  434. if(is9_OR_10BPS(c->srcFormat)) {
  435. const int src_depth = av_pix_fmt_descriptors[c->srcFormat].comp[plane].depth_minus1+1;
  436. const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
  437. const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
  438. if (is16BPS(c->dstFormat)) {
  439. uint16_t *dstPtr2 = (uint16_t*)dstPtr;
  440. #define COPY9_OR_10TO16(rfunc, wfunc) \
  441. for (i = 0; i < height; i++) { \
  442. for (j = 0; j < length; j++) { \
  443. int srcpx = rfunc(&srcPtr2[j]); \
  444. wfunc(&dstPtr2[j], (srcpx<<(16-src_depth)) | (srcpx>>(2*src_depth-16))); \
  445. } \
  446. dstPtr2 += dstStride[plane]/2; \
  447. srcPtr2 += srcStride[plane]/2; \
  448. }
  449. if (isBE(c->dstFormat)) {
  450. if (isBE(c->srcFormat)) {
  451. COPY9_OR_10TO16(AV_RB16, AV_WB16);
  452. } else {
  453. COPY9_OR_10TO16(AV_RL16, AV_WB16);
  454. }
  455. } else {
  456. if (isBE(c->srcFormat)) {
  457. COPY9_OR_10TO16(AV_RB16, AV_WL16);
  458. } else {
  459. COPY9_OR_10TO16(AV_RL16, AV_WL16);
  460. }
  461. }
  462. } else if (is9_OR_10BPS(c->dstFormat)) {
  463. uint16_t *dstPtr2 = (uint16_t*)dstPtr;
  464. #define COPY9_OR_10TO9_OR_10(loop) \
  465. for (i = 0; i < height; i++) { \
  466. for (j = 0; j < length; j++) { \
  467. loop; \
  468. } \
  469. dstPtr2 += dstStride[plane]/2; \
  470. srcPtr2 += srcStride[plane]/2; \
  471. }
  472. #define COPY9_OR_10TO9_OR_10_2(rfunc, wfunc) \
  473. if (dst_depth > src_depth) { \
  474. COPY9_OR_10TO9_OR_10(int srcpx = rfunc(&srcPtr2[j]); \
  475. wfunc(&dstPtr2[j], (srcpx << 1) | (srcpx >> 9))); \
  476. } else if (dst_depth < src_depth) { \
  477. DITHER_COPY(dstPtr2, dstStride[plane]/2, wfunc, \
  478. srcPtr2, srcStride[plane]/2, rfunc, \
  479. dither_8x8_1, 1); \
  480. } else { \
  481. COPY9_OR_10TO9_OR_10(wfunc(&dstPtr2[j], rfunc(&srcPtr2[j]))); \
  482. }
  483. if (isBE(c->dstFormat)) {
  484. if (isBE(c->srcFormat)) {
  485. COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WB16);
  486. } else {
  487. COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WB16);
  488. }
  489. } else {
  490. if (isBE(c->srcFormat)) {
  491. COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WL16);
  492. } else {
  493. COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WL16);
  494. }
  495. }
  496. } else {
  497. #define W8(a, b) { *(a) = (b); }
  498. #define COPY9_OR_10TO8(rfunc) \
  499. if (src_depth == 9) { \
  500. DITHER_COPY(dstPtr, dstStride[plane], W8, \
  501. srcPtr2, srcStride[plane]/2, rfunc, \
  502. dither_8x8_1, 1); \
  503. } else { \
  504. DITHER_COPY(dstPtr, dstStride[plane], W8, \
  505. srcPtr2, srcStride[plane]/2, rfunc, \
  506. dither_8x8_3, 2); \
  507. }
  508. if (isBE(c->srcFormat)) {
  509. COPY9_OR_10TO8(AV_RB16);
  510. } else {
  511. COPY9_OR_10TO8(AV_RL16);
  512. }
  513. }
  514. } else if(is9_OR_10BPS(c->dstFormat)) {
  515. const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
  516. uint16_t *dstPtr2 = (uint16_t*)dstPtr;
  517. if (is16BPS(c->srcFormat)) {
  518. const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
  519. #define COPY16TO9_OR_10(rfunc, wfunc) \
  520. if (dst_depth == 9) { \
  521. DITHER_COPY(dstPtr2, dstStride[plane]/2, wfunc, \
  522. srcPtr2, srcStride[plane]/2, rfunc, \
  523. dither_8x8_128, 7); \
  524. } else { \
  525. DITHER_COPY(dstPtr2, dstStride[plane]/2, wfunc, \
  526. srcPtr2, srcStride[plane]/2, rfunc, \
  527. dither_8x8_64, 6); \
  528. }
  529. if (isBE(c->dstFormat)) {
  530. if (isBE(c->srcFormat)) {
  531. COPY16TO9_OR_10(AV_RB16, AV_WB16);
  532. } else {
  533. COPY16TO9_OR_10(AV_RL16, AV_WB16);
  534. }
  535. } else {
  536. if (isBE(c->srcFormat)) {
  537. COPY16TO9_OR_10(AV_RB16, AV_WL16);
  538. } else {
  539. COPY16TO9_OR_10(AV_RL16, AV_WL16);
  540. }
  541. }
  542. } else /* 8bit */ {
  543. #define COPY8TO9_OR_10(wfunc) \
  544. for (i = 0; i < height; i++) { \
  545. for (j = 0; j < length; j++) { \
  546. const int srcpx = srcPtr[j]; \
  547. wfunc(&dstPtr2[j], (srcpx<<(dst_depth-8)) | (srcpx >> (16-dst_depth))); \
  548. } \
  549. dstPtr2 += dstStride[plane]/2; \
  550. srcPtr += srcStride[plane]; \
  551. }
  552. if (isBE(c->dstFormat)) {
  553. COPY8TO9_OR_10(AV_WB16);
  554. } else {
  555. COPY8TO9_OR_10(AV_WL16);
  556. }
  557. }
  558. } else if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
  559. const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
  560. #define COPY16TO8(rfunc) \
  561. DITHER_COPY(dstPtr, dstStride[plane], W8, \
  562. srcPtr2, srcStride[plane]/2, rfunc, \
  563. dither_8x8_256, 8);
  564. if (isBE(c->srcFormat)) {
  565. COPY16TO8(AV_RB16);
  566. } else {
  567. COPY16TO8(AV_RL16);
  568. }
  569. } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
  570. for (i=0; i<height; i++) {
  571. for (j=0; j<length; j++) {
  572. dstPtr[ j<<1 ] = srcPtr[j];
  573. dstPtr[(j<<1)+1] = srcPtr[j];
  574. }
  575. srcPtr+= srcStride[plane];
  576. dstPtr+= dstStride[plane];
  577. }
  578. } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
  579. && isBE(c->srcFormat) != isBE(c->dstFormat)) {
  580. for (i=0; i<height; i++) {
  581. for (j=0; j<length; j++)
  582. ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
  583. srcPtr+= srcStride[plane];
  584. dstPtr+= dstStride[plane];
  585. }
  586. } else if (dstStride[plane] == srcStride[plane] &&
  587. srcStride[plane] > 0 && srcStride[plane] == length) {
  588. memcpy(dst[plane] + dstStride[plane]*y, src[plane],
  589. height*dstStride[plane]);
  590. } else {
  591. if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
  592. length*=2;
  593. for (i=0; i<height; i++) {
  594. memcpy(dstPtr, srcPtr, length);
  595. srcPtr+= srcStride[plane];
  596. dstPtr+= dstStride[plane];
  597. }
  598. }
  599. }
  600. }
  601. return srcSliceH;
  602. }
  603. void ff_get_unscaled_swscale(SwsContext *c)
  604. {
  605. const enum PixelFormat srcFormat = c->srcFormat;
  606. const enum PixelFormat dstFormat = c->dstFormat;
  607. const int flags = c->flags;
  608. const int dstH = c->dstH;
  609. int needsDither;
  610. needsDither= isAnyRGB(dstFormat)
  611. && c->dstFormatBpp < 24
  612. && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
  613. /* yv12_to_nv12 */
  614. if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
  615. c->swScale= planarToNv12Wrapper;
  616. }
  617. /* yuv2bgr */
  618. if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
  619. && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
  620. c->swScale= ff_yuv2rgb_get_func_ptr(c);
  621. }
  622. if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
  623. c->swScale= yvu9ToYv12Wrapper;
  624. }
  625. /* bgr24toYV12 */
  626. if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
  627. c->swScale= bgr24ToYv12Wrapper;
  628. /* RGB/BGR -> RGB/BGR (no dither needed forms) */
  629. if ( isAnyRGB(srcFormat)
  630. && isAnyRGB(dstFormat)
  631. && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
  632. && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
  633. && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
  634. && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
  635. && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
  636. && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
  637. && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
  638. && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
  639. && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
  640. && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
  641. && srcFormat != PIX_FMT_BGR48LE && dstFormat != PIX_FMT_BGR48LE
  642. && srcFormat != PIX_FMT_BGR48BE && dstFormat != PIX_FMT_BGR48BE
  643. && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
  644. c->swScale= rgbToRgbWrapper;
  645. if ((usePal(srcFormat) && (
  646. dstFormat == PIX_FMT_RGB32 ||
  647. dstFormat == PIX_FMT_RGB32_1 ||
  648. dstFormat == PIX_FMT_RGB24 ||
  649. dstFormat == PIX_FMT_BGR32 ||
  650. dstFormat == PIX_FMT_BGR32_1 ||
  651. dstFormat == PIX_FMT_BGR24)))
  652. c->swScale= palToRgbWrapper;
  653. if (srcFormat == PIX_FMT_YUV422P) {
  654. if (dstFormat == PIX_FMT_YUYV422)
  655. c->swScale= yuv422pToYuy2Wrapper;
  656. else if (dstFormat == PIX_FMT_UYVY422)
  657. c->swScale= yuv422pToUyvyWrapper;
  658. }
  659. /* LQ converters if -sws 0 or -sws 4*/
  660. if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
  661. /* yv12_to_yuy2 */
  662. if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
  663. if (dstFormat == PIX_FMT_YUYV422)
  664. c->swScale= planarToYuy2Wrapper;
  665. else if (dstFormat == PIX_FMT_UYVY422)
  666. c->swScale= planarToUyvyWrapper;
  667. }
  668. }
  669. if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  670. c->swScale= yuyvToYuv420Wrapper;
  671. if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  672. c->swScale= uyvyToYuv420Wrapper;
  673. if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
  674. c->swScale= yuyvToYuv422Wrapper;
  675. if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
  676. c->swScale= uyvyToYuv422Wrapper;
  677. /* simple copy */
  678. if ( srcFormat == dstFormat
  679. || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
  680. || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
  681. || (isPlanarYUV(srcFormat) && isGray(dstFormat))
  682. || (isPlanarYUV(dstFormat) && isGray(srcFormat))
  683. || (isGray(dstFormat) && isGray(srcFormat))
  684. || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
  685. && c->chrDstHSubSample == c->chrSrcHSubSample
  686. && c->chrDstVSubSample == c->chrSrcVSubSample
  687. && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
  688. && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
  689. {
  690. if (isPacked(c->srcFormat))
  691. c->swScale= packedCopyWrapper;
  692. else /* Planar YUV or gray */
  693. c->swScale= planarCopyWrapper;
  694. }
  695. if (ARCH_BFIN)
  696. ff_bfin_get_unscaled_swscale(c);
  697. if (HAVE_ALTIVEC)
  698. ff_swscale_get_unscaled_altivec(c);
  699. }
  700. static void reset_ptr(const uint8_t* src[], int format)
  701. {
  702. if(!isALPHA(format))
  703. src[3]=NULL;
  704. if(!isPlanarYUV(format)) {
  705. src[3]=src[2]=NULL;
  706. if (!usePal(format))
  707. src[1]= NULL;
  708. }
  709. }
  710. static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
  711. const int linesizes[4])
  712. {
  713. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  714. int i;
  715. for (i = 0; i < 4; i++) {
  716. int plane = desc->comp[i].plane;
  717. if (!data[plane] || !linesizes[plane])
  718. return 0;
  719. }
  720. return 1;
  721. }
  722. /**
  723. * swscale wrapper, so we don't need to export the SwsContext.
  724. * Assumes planar YUV to be in YUV order instead of YVU.
  725. */
  726. int sws_scale(struct SwsContext *c, const uint8_t* const srcSlice[],
  727. const int srcStride[], int srcSliceY, int srcSliceH,
  728. uint8_t* const dst[], const int dstStride[])
  729. {
  730. int i;
  731. const uint8_t* src2[4]= {srcSlice[0], srcSlice[1], srcSlice[2], srcSlice[3]};
  732. uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
  733. // do not mess up sliceDir if we have a "trailing" 0-size slice
  734. if (srcSliceH == 0)
  735. return 0;
  736. if (!check_image_pointers(srcSlice, c->srcFormat, srcStride)) {
  737. av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
  738. return 0;
  739. }
  740. if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
  741. av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
  742. return 0;
  743. }
  744. if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
  745. av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
  746. return 0;
  747. }
  748. if (c->sliceDir == 0) {
  749. if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
  750. }
  751. if (usePal(c->srcFormat)) {
  752. for (i=0; i<256; i++) {
  753. int p, r, g, b,y,u,v;
  754. if(c->srcFormat == PIX_FMT_PAL8) {
  755. p=((const uint32_t*)(srcSlice[1]))[i];
  756. r= (p>>16)&0xFF;
  757. g= (p>> 8)&0xFF;
  758. b= p &0xFF;
  759. } else if(c->srcFormat == PIX_FMT_RGB8) {
  760. r= (i>>5 )*36;
  761. g= ((i>>2)&7)*36;
  762. b= (i&3 )*85;
  763. } else if(c->srcFormat == PIX_FMT_BGR8) {
  764. b= (i>>6 )*85;
  765. g= ((i>>3)&7)*36;
  766. r= (i&7 )*36;
  767. } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
  768. r= (i>>3 )*255;
  769. g= ((i>>1)&3)*85;
  770. b= (i&1 )*255;
  771. } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_Y400A) {
  772. r = g = b = i;
  773. } else {
  774. assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
  775. b= (i>>3 )*255;
  776. g= ((i>>1)&3)*85;
  777. r= (i&1 )*255;
  778. }
  779. y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  780. u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  781. v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  782. c->pal_yuv[i]= y + (u<<8) + (v<<16);
  783. switch(c->dstFormat) {
  784. case PIX_FMT_BGR32:
  785. #if !HAVE_BIGENDIAN
  786. case PIX_FMT_RGB24:
  787. #endif
  788. c->pal_rgb[i]= r + (g<<8) + (b<<16);
  789. break;
  790. case PIX_FMT_BGR32_1:
  791. #if HAVE_BIGENDIAN
  792. case PIX_FMT_BGR24:
  793. #endif
  794. c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
  795. break;
  796. case PIX_FMT_RGB32_1:
  797. #if HAVE_BIGENDIAN
  798. case PIX_FMT_RGB24:
  799. #endif
  800. c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
  801. break;
  802. case PIX_FMT_RGB32:
  803. #if !HAVE_BIGENDIAN
  804. case PIX_FMT_BGR24:
  805. #endif
  806. default:
  807. c->pal_rgb[i]= b + (g<<8) + (r<<16);
  808. }
  809. }
  810. }
  811. // copy strides, so they can safely be modified
  812. if (c->sliceDir == 1) {
  813. // slices go from top to bottom
  814. int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
  815. int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
  816. reset_ptr(src2, c->srcFormat);
  817. reset_ptr((const uint8_t**)dst2, c->dstFormat);
  818. /* reset slice direction at end of frame */
  819. if (srcSliceY + srcSliceH == c->srcH)
  820. c->sliceDir = 0;
  821. return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
  822. } else {
  823. // slices go from bottom to top => we flip the image internally
  824. int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
  825. int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
  826. src2[0] += (srcSliceH-1)*srcStride[0];
  827. if (!usePal(c->srcFormat))
  828. src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
  829. src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
  830. src2[3] += (srcSliceH-1)*srcStride[3];
  831. dst2[0] += ( c->dstH -1)*dstStride[0];
  832. dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
  833. dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
  834. dst2[3] += ( c->dstH -1)*dstStride[3];
  835. reset_ptr(src2, c->srcFormat);
  836. reset_ptr((const uint8_t**)dst2, c->dstFormat);
  837. /* reset slice direction at end of frame */
  838. if (!srcSliceY)
  839. c->sliceDir = 0;
  840. return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
  841. }
  842. }
  843. /* Convert the palette to the same packed 32-bit format as the palette */
  844. void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
  845. {
  846. int i;
  847. for (i=0; i<num_pixels; i++)
  848. ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
  849. }
  850. /* Palette format: ABCD -> dst format: ABC */
  851. void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
  852. {
  853. int i;
  854. for (i=0; i<num_pixels; i++) {
  855. //FIXME slow?
  856. dst[0]= palette[src[i]*4+0];
  857. dst[1]= palette[src[i]*4+1];
  858. dst[2]= palette[src[i]*4+2];
  859. dst+= 3;
  860. }
  861. }