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.

962 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 && !(srcStride[0] % srcBpp))
  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 clip9(x) av_clip_uintp2(x, 9)
  399. #define clip10(x) av_clip_uintp2(x, 10)
  400. #define DITHER_COPY(dst, dstStride, wfunc, src, srcStride, rfunc, dithers, shift, clip) \
  401. for (i = 0; i < height; i++) { \
  402. const uint8_t *dither = dithers[i & 7]; \
  403. for (j = 0; j < length - 7; j += 8) { \
  404. wfunc(&dst[j + 0], clip((rfunc(&src[j + 0]) + dither[0]) >> shift)); \
  405. wfunc(&dst[j + 1], clip((rfunc(&src[j + 1]) + dither[1]) >> shift)); \
  406. wfunc(&dst[j + 2], clip((rfunc(&src[j + 2]) + dither[2]) >> shift)); \
  407. wfunc(&dst[j + 3], clip((rfunc(&src[j + 3]) + dither[3]) >> shift)); \
  408. wfunc(&dst[j + 4], clip((rfunc(&src[j + 4]) + dither[4]) >> shift)); \
  409. wfunc(&dst[j + 5], clip((rfunc(&src[j + 5]) + dither[5]) >> shift)); \
  410. wfunc(&dst[j + 6], clip((rfunc(&src[j + 6]) + dither[6]) >> shift)); \
  411. wfunc(&dst[j + 7], clip((rfunc(&src[j + 7]) + dither[7]) >> shift)); \
  412. } \
  413. for (; j < length; j++) \
  414. wfunc(&dst[j], (rfunc(&src[j]) + dither[j & 7]) >> shift); \
  415. dst += dstStride; \
  416. src += srcStride; \
  417. }
  418. static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  419. int srcSliceH, uint8_t* dst[], int dstStride[])
  420. {
  421. int plane, i, j;
  422. for (plane=0; plane<4; plane++) {
  423. int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
  424. int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
  425. int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
  426. const uint8_t *srcPtr= src[plane];
  427. uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
  428. if (!dst[plane]) continue;
  429. // ignore palette for GRAY8
  430. if (plane == 1 && !dst[2]) continue;
  431. if (!src[plane] || (plane == 1 && !src[2])) {
  432. if(is16BPS(c->dstFormat))
  433. length*=2;
  434. fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
  435. } else {
  436. if(is9_OR_10BPS(c->srcFormat)) {
  437. const int src_depth = av_pix_fmt_descriptors[c->srcFormat].comp[plane].depth_minus1+1;
  438. const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
  439. const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
  440. if (is16BPS(c->dstFormat)) {
  441. uint16_t *dstPtr2 = (uint16_t*)dstPtr;
  442. #define COPY9_OR_10TO16(rfunc, wfunc) \
  443. for (i = 0; i < height; i++) { \
  444. for (j = 0; j < length; j++) { \
  445. int srcpx = rfunc(&srcPtr2[j]); \
  446. wfunc(&dstPtr2[j], (srcpx<<(16-src_depth)) | (srcpx>>(2*src_depth-16))); \
  447. } \
  448. dstPtr2 += dstStride[plane]/2; \
  449. srcPtr2 += srcStride[plane]/2; \
  450. }
  451. if (isBE(c->dstFormat)) {
  452. if (isBE(c->srcFormat)) {
  453. COPY9_OR_10TO16(AV_RB16, AV_WB16);
  454. } else {
  455. COPY9_OR_10TO16(AV_RL16, AV_WB16);
  456. }
  457. } else {
  458. if (isBE(c->srcFormat)) {
  459. COPY9_OR_10TO16(AV_RB16, AV_WL16);
  460. } else {
  461. COPY9_OR_10TO16(AV_RL16, AV_WL16);
  462. }
  463. }
  464. } else if (is9_OR_10BPS(c->dstFormat)) {
  465. uint16_t *dstPtr2 = (uint16_t*)dstPtr;
  466. #define COPY9_OR_10TO9_OR_10(loop) \
  467. for (i = 0; i < height; i++) { \
  468. for (j = 0; j < length; j++) { \
  469. loop; \
  470. } \
  471. dstPtr2 += dstStride[plane]/2; \
  472. srcPtr2 += srcStride[plane]/2; \
  473. }
  474. #define COPY9_OR_10TO9_OR_10_2(rfunc, wfunc) \
  475. if (dst_depth > src_depth) { \
  476. COPY9_OR_10TO9_OR_10(int srcpx = rfunc(&srcPtr2[j]); \
  477. wfunc(&dstPtr2[j], (srcpx << 1) | (srcpx >> 9))); \
  478. } else if (dst_depth < src_depth) { \
  479. DITHER_COPY(dstPtr2, dstStride[plane]/2, wfunc, \
  480. srcPtr2, srcStride[plane]/2, rfunc, \
  481. dither_8x8_1, 1, clip9); \
  482. } else { \
  483. COPY9_OR_10TO9_OR_10(wfunc(&dstPtr2[j], rfunc(&srcPtr2[j]))); \
  484. }
  485. if (isBE(c->dstFormat)) {
  486. if (isBE(c->srcFormat)) {
  487. COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WB16);
  488. } else {
  489. COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WB16);
  490. }
  491. } else {
  492. if (isBE(c->srcFormat)) {
  493. COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WL16);
  494. } else {
  495. COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WL16);
  496. }
  497. }
  498. } else {
  499. #define W8(a, b) { *(a) = (b); }
  500. #define COPY9_OR_10TO8(rfunc) \
  501. if (src_depth == 9) { \
  502. DITHER_COPY(dstPtr, dstStride[plane], W8, \
  503. srcPtr2, srcStride[plane]/2, rfunc, \
  504. dither_8x8_1, 1, av_clip_uint8); \
  505. } else { \
  506. DITHER_COPY(dstPtr, dstStride[plane], W8, \
  507. srcPtr2, srcStride[plane]/2, rfunc, \
  508. dither_8x8_3, 2, av_clip_uint8); \
  509. }
  510. if (isBE(c->srcFormat)) {
  511. COPY9_OR_10TO8(AV_RB16);
  512. } else {
  513. COPY9_OR_10TO8(AV_RL16);
  514. }
  515. }
  516. } else if(is9_OR_10BPS(c->dstFormat)) {
  517. const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
  518. uint16_t *dstPtr2 = (uint16_t*)dstPtr;
  519. if (is16BPS(c->srcFormat)) {
  520. const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
  521. #define COPY16TO9_OR_10(rfunc, wfunc) \
  522. if (dst_depth == 9) { \
  523. DITHER_COPY(dstPtr2, dstStride[plane]/2, wfunc, \
  524. srcPtr2, srcStride[plane]/2, rfunc, \
  525. dither_8x8_128, 7, clip9); \
  526. } else { \
  527. DITHER_COPY(dstPtr2, dstStride[plane]/2, wfunc, \
  528. srcPtr2, srcStride[plane]/2, rfunc, \
  529. dither_8x8_64, 6, clip10); \
  530. }
  531. if (isBE(c->dstFormat)) {
  532. if (isBE(c->srcFormat)) {
  533. COPY16TO9_OR_10(AV_RB16, AV_WB16);
  534. } else {
  535. COPY16TO9_OR_10(AV_RL16, AV_WB16);
  536. }
  537. } else {
  538. if (isBE(c->srcFormat)) {
  539. COPY16TO9_OR_10(AV_RB16, AV_WL16);
  540. } else {
  541. COPY16TO9_OR_10(AV_RL16, AV_WL16);
  542. }
  543. }
  544. } else /* 8bit */ {
  545. #define COPY8TO9_OR_10(wfunc) \
  546. for (i = 0; i < height; i++) { \
  547. for (j = 0; j < length; j++) { \
  548. const int srcpx = srcPtr[j]; \
  549. wfunc(&dstPtr2[j], (srcpx<<(dst_depth-8)) | (srcpx >> (16-dst_depth))); \
  550. } \
  551. dstPtr2 += dstStride[plane]/2; \
  552. srcPtr += srcStride[plane]; \
  553. }
  554. if (isBE(c->dstFormat)) {
  555. COPY8TO9_OR_10(AV_WB16);
  556. } else {
  557. COPY8TO9_OR_10(AV_WL16);
  558. }
  559. }
  560. } else if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
  561. const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
  562. #define COPY16TO8(rfunc) \
  563. DITHER_COPY(dstPtr, dstStride[plane], W8, \
  564. srcPtr2, srcStride[plane]/2, rfunc, \
  565. dither_8x8_256, 8, av_clip_uint8);
  566. if (isBE(c->srcFormat)) {
  567. COPY16TO8(AV_RB16);
  568. } else {
  569. COPY16TO8(AV_RL16);
  570. }
  571. } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
  572. for (i=0; i<height; i++) {
  573. for (j=0; j<length; j++) {
  574. dstPtr[ j<<1 ] = srcPtr[j];
  575. dstPtr[(j<<1)+1] = srcPtr[j];
  576. }
  577. srcPtr+= srcStride[plane];
  578. dstPtr+= dstStride[plane];
  579. }
  580. } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
  581. && isBE(c->srcFormat) != isBE(c->dstFormat)) {
  582. for (i=0; i<height; i++) {
  583. for (j=0; j<length; j++)
  584. ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
  585. srcPtr+= srcStride[plane];
  586. dstPtr+= dstStride[plane];
  587. }
  588. } else if (dstStride[plane] == srcStride[plane] &&
  589. srcStride[plane] > 0 && srcStride[plane] == length) {
  590. memcpy(dst[plane] + dstStride[plane]*y, src[plane],
  591. height*dstStride[plane]);
  592. } else {
  593. if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
  594. length*=2;
  595. for (i=0; i<height; i++) {
  596. memcpy(dstPtr, srcPtr, length);
  597. srcPtr+= srcStride[plane];
  598. dstPtr+= dstStride[plane];
  599. }
  600. }
  601. }
  602. }
  603. return srcSliceH;
  604. }
  605. void ff_get_unscaled_swscale(SwsContext *c)
  606. {
  607. const enum PixelFormat srcFormat = c->srcFormat;
  608. const enum PixelFormat dstFormat = c->dstFormat;
  609. const int flags = c->flags;
  610. const int dstH = c->dstH;
  611. int needsDither;
  612. needsDither= isAnyRGB(dstFormat)
  613. && c->dstFormatBpp < 24
  614. && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
  615. /* yv12_to_nv12 */
  616. if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
  617. c->swScale= planarToNv12Wrapper;
  618. }
  619. /* yuv2bgr */
  620. if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
  621. && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
  622. c->swScale= ff_yuv2rgb_get_func_ptr(c);
  623. }
  624. if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
  625. c->swScale= yvu9ToYv12Wrapper;
  626. }
  627. /* bgr24toYV12 */
  628. if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
  629. c->swScale= bgr24ToYv12Wrapper;
  630. /* RGB/BGR -> RGB/BGR (no dither needed forms) */
  631. if ( isAnyRGB(srcFormat)
  632. && isAnyRGB(dstFormat)
  633. && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
  634. && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
  635. && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
  636. && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
  637. && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
  638. && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
  639. && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
  640. && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
  641. && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
  642. && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
  643. && srcFormat != PIX_FMT_BGR48LE && dstFormat != PIX_FMT_BGR48LE
  644. && srcFormat != PIX_FMT_BGR48BE && dstFormat != PIX_FMT_BGR48BE
  645. && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
  646. c->swScale= rgbToRgbWrapper;
  647. if ((usePal(srcFormat) && (
  648. dstFormat == PIX_FMT_RGB32 ||
  649. dstFormat == PIX_FMT_RGB32_1 ||
  650. dstFormat == PIX_FMT_RGB24 ||
  651. dstFormat == PIX_FMT_BGR32 ||
  652. dstFormat == PIX_FMT_BGR32_1 ||
  653. dstFormat == PIX_FMT_BGR24)))
  654. c->swScale= palToRgbWrapper;
  655. if (srcFormat == PIX_FMT_YUV422P) {
  656. if (dstFormat == PIX_FMT_YUYV422)
  657. c->swScale= yuv422pToYuy2Wrapper;
  658. else if (dstFormat == PIX_FMT_UYVY422)
  659. c->swScale= yuv422pToUyvyWrapper;
  660. }
  661. /* LQ converters if -sws 0 or -sws 4*/
  662. if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
  663. /* yv12_to_yuy2 */
  664. if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
  665. if (dstFormat == PIX_FMT_YUYV422)
  666. c->swScale= planarToYuy2Wrapper;
  667. else if (dstFormat == PIX_FMT_UYVY422)
  668. c->swScale= planarToUyvyWrapper;
  669. }
  670. }
  671. if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  672. c->swScale= yuyvToYuv420Wrapper;
  673. if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  674. c->swScale= uyvyToYuv420Wrapper;
  675. if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
  676. c->swScale= yuyvToYuv422Wrapper;
  677. if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
  678. c->swScale= uyvyToYuv422Wrapper;
  679. /* simple copy */
  680. if ( srcFormat == dstFormat
  681. || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
  682. || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
  683. || (isPlanarYUV(srcFormat) && isGray(dstFormat))
  684. || (isPlanarYUV(dstFormat) && isGray(srcFormat))
  685. || (isGray(dstFormat) && isGray(srcFormat))
  686. || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
  687. && c->chrDstHSubSample == c->chrSrcHSubSample
  688. && c->chrDstVSubSample == c->chrSrcVSubSample
  689. && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
  690. && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
  691. {
  692. if (isPacked(c->srcFormat))
  693. c->swScale= packedCopyWrapper;
  694. else /* Planar YUV or gray */
  695. c->swScale= planarCopyWrapper;
  696. }
  697. if (ARCH_BFIN)
  698. ff_bfin_get_unscaled_swscale(c);
  699. if (HAVE_ALTIVEC)
  700. ff_swscale_get_unscaled_altivec(c);
  701. }
  702. static void reset_ptr(const uint8_t* src[], int format)
  703. {
  704. if(!isALPHA(format))
  705. src[3]=NULL;
  706. if(!isPlanarYUV(format)) {
  707. src[3]=src[2]=NULL;
  708. if (!usePal(format))
  709. src[1]= NULL;
  710. }
  711. }
  712. static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
  713. const int linesizes[4])
  714. {
  715. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  716. int i;
  717. for (i = 0; i < 4; i++) {
  718. int plane = desc->comp[i].plane;
  719. if (!data[plane] || !linesizes[plane])
  720. return 0;
  721. }
  722. return 1;
  723. }
  724. /**
  725. * swscale wrapper, so we don't need to export the SwsContext.
  726. * Assumes planar YUV to be in YUV order instead of YVU.
  727. */
  728. int sws_scale(struct SwsContext *c, const uint8_t* const srcSlice[],
  729. const int srcStride[], int srcSliceY, int srcSliceH,
  730. uint8_t* const dst[], const int dstStride[])
  731. {
  732. int i;
  733. const uint8_t* src2[4]= {srcSlice[0], srcSlice[1], srcSlice[2], srcSlice[3]};
  734. uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
  735. // do not mess up sliceDir if we have a "trailing" 0-size slice
  736. if (srcSliceH == 0)
  737. return 0;
  738. if (!check_image_pointers(srcSlice, c->srcFormat, srcStride)) {
  739. av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
  740. return 0;
  741. }
  742. if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
  743. av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
  744. return 0;
  745. }
  746. if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
  747. av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
  748. return 0;
  749. }
  750. if (c->sliceDir == 0) {
  751. if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
  752. }
  753. if (usePal(c->srcFormat)) {
  754. for (i=0; i<256; i++) {
  755. int p, r, g, b,y,u,v;
  756. if(c->srcFormat == PIX_FMT_PAL8) {
  757. p=((const uint32_t*)(srcSlice[1]))[i];
  758. r= (p>>16)&0xFF;
  759. g= (p>> 8)&0xFF;
  760. b= p &0xFF;
  761. } else if(c->srcFormat == PIX_FMT_RGB8) {
  762. r= (i>>5 )*36;
  763. g= ((i>>2)&7)*36;
  764. b= (i&3 )*85;
  765. } else if(c->srcFormat == PIX_FMT_BGR8) {
  766. b= (i>>6 )*85;
  767. g= ((i>>3)&7)*36;
  768. r= (i&7 )*36;
  769. } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
  770. r= (i>>3 )*255;
  771. g= ((i>>1)&3)*85;
  772. b= (i&1 )*255;
  773. } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_Y400A) {
  774. r = g = b = i;
  775. } else {
  776. assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
  777. b= (i>>3 )*255;
  778. g= ((i>>1)&3)*85;
  779. r= (i&1 )*255;
  780. }
  781. y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  782. u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  783. v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  784. c->pal_yuv[i]= y + (u<<8) + (v<<16);
  785. switch(c->dstFormat) {
  786. case PIX_FMT_BGR32:
  787. #if !HAVE_BIGENDIAN
  788. case PIX_FMT_RGB24:
  789. #endif
  790. c->pal_rgb[i]= r + (g<<8) + (b<<16);
  791. break;
  792. case PIX_FMT_BGR32_1:
  793. #if HAVE_BIGENDIAN
  794. case PIX_FMT_BGR24:
  795. #endif
  796. c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
  797. break;
  798. case PIX_FMT_RGB32_1:
  799. #if HAVE_BIGENDIAN
  800. case PIX_FMT_RGB24:
  801. #endif
  802. c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
  803. break;
  804. case PIX_FMT_RGB32:
  805. #if !HAVE_BIGENDIAN
  806. case PIX_FMT_BGR24:
  807. #endif
  808. default:
  809. c->pal_rgb[i]= b + (g<<8) + (r<<16);
  810. }
  811. }
  812. }
  813. // copy strides, so they can safely be modified
  814. if (c->sliceDir == 1) {
  815. // slices go from top to bottom
  816. int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
  817. int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
  818. reset_ptr(src2, c->srcFormat);
  819. reset_ptr((const uint8_t**)dst2, c->dstFormat);
  820. /* reset slice direction at end of frame */
  821. if (srcSliceY + srcSliceH == c->srcH)
  822. c->sliceDir = 0;
  823. return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
  824. } else {
  825. // slices go from bottom to top => we flip the image internally
  826. int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
  827. int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
  828. src2[0] += (srcSliceH-1)*srcStride[0];
  829. if (!usePal(c->srcFormat))
  830. src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
  831. src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
  832. src2[3] += (srcSliceH-1)*srcStride[3];
  833. dst2[0] += ( c->dstH -1)*dstStride[0];
  834. dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
  835. dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
  836. dst2[3] += ( c->dstH -1)*dstStride[3];
  837. reset_ptr(src2, c->srcFormat);
  838. reset_ptr((const uint8_t**)dst2, c->dstFormat);
  839. /* reset slice direction at end of frame */
  840. if (!srcSliceY)
  841. c->sliceDir = 0;
  842. return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
  843. }
  844. }
  845. /* Convert the palette to the same packed 32-bit format as the palette */
  846. void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
  847. {
  848. int i;
  849. for (i=0; i<num_pixels; i++)
  850. ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
  851. }
  852. /* Palette format: ABCD -> dst format: ABC */
  853. void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
  854. {
  855. int i;
  856. for (i=0; i<num_pixels; i++) {
  857. //FIXME slow?
  858. dst[0]= palette[src[i]*4+0];
  859. dst[1]= palette[src[i]*4+1];
  860. dst[2]= palette[src[i]*4+2];
  861. dst+= 3;
  862. }
  863. }