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.

1004 lines
36KB

  1. /*
  2. * Copyright (C) 2001-2011 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 <math.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/avutil.h"
  26. #include "libavutil/bswap.h"
  27. #include "libavutil/cpu.h"
  28. #include "libavutil/imgutils.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "libavutil/mathematics.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "config.h"
  33. #include "rgb2rgb.h"
  34. #include "swscale_internal.h"
  35. #include "swscale.h"
  36. DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_128)[9][8] = {
  37. { 36, 68, 60, 92, 34, 66, 58, 90, },
  38. { 100, 4, 124, 28, 98, 2, 122, 26, },
  39. { 52, 84, 44, 76, 50, 82, 42, 74, },
  40. { 116, 20, 108, 12, 114, 18, 106, 10, },
  41. { 32, 64, 56, 88, 38, 70, 62, 94, },
  42. { 96, 0, 120, 24, 102, 6, 126, 30, },
  43. { 48, 80, 40, 72, 54, 86, 46, 78, },
  44. { 112, 16, 104, 8, 118, 22, 110, 14, },
  45. { 36, 68, 60, 92, 34, 66, 58, 90, },
  46. };
  47. DECLARE_ALIGNED(8, static const uint8_t, sws_pb_64)[8] = {
  48. 64, 64, 64, 64, 64, 64, 64, 64
  49. };
  50. static av_always_inline void fillPlane(uint8_t *plane, int stride, int width,
  51. int height, int y, uint8_t val)
  52. {
  53. int i;
  54. uint8_t *ptr = plane + stride * y;
  55. for (i = 0; i < height; i++) {
  56. memset(ptr, val, width);
  57. ptr += stride;
  58. }
  59. }
  60. static void hScale16To19_c(SwsContext *c, int16_t *_dst, int dstW,
  61. const uint8_t *_src, const int16_t *filter,
  62. const int32_t *filterPos, int filterSize)
  63. {
  64. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
  65. int i;
  66. int32_t *dst = (int32_t *) _dst;
  67. const uint16_t *src = (const uint16_t *) _src;
  68. int bits = desc->comp[0].depth - 1;
  69. int sh = bits - 4;
  70. if((isAnyRGB(c->srcFormat) || c->srcFormat==AV_PIX_FMT_PAL8) && desc->comp[0].depth<16)
  71. sh= 9;
  72. for (i = 0; i < dstW; i++) {
  73. int j;
  74. int srcPos = filterPos[i];
  75. int val = 0;
  76. for (j = 0; j < filterSize; j++) {
  77. val += src[srcPos + j] * filter[filterSize * i + j];
  78. }
  79. // filter=14 bit, input=16 bit, output=30 bit, >> 11 makes 19 bit
  80. dst[i] = FFMIN(val >> sh, (1 << 19) - 1);
  81. }
  82. }
  83. static void hScale16To15_c(SwsContext *c, int16_t *dst, int dstW,
  84. const uint8_t *_src, const int16_t *filter,
  85. const int32_t *filterPos, int filterSize)
  86. {
  87. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
  88. int i;
  89. const uint16_t *src = (const uint16_t *) _src;
  90. int sh = desc->comp[0].depth - 1;
  91. if(sh<15)
  92. sh= isAnyRGB(c->srcFormat) || c->srcFormat==AV_PIX_FMT_PAL8 ? 13 : (desc->comp[0].depth - 1);
  93. for (i = 0; i < dstW; i++) {
  94. int j;
  95. int srcPos = filterPos[i];
  96. int val = 0;
  97. for (j = 0; j < filterSize; j++) {
  98. val += src[srcPos + j] * filter[filterSize * i + j];
  99. }
  100. // filter=14 bit, input=16 bit, output=30 bit, >> 15 makes 15 bit
  101. dst[i] = FFMIN(val >> sh, (1 << 15) - 1);
  102. }
  103. }
  104. // bilinear / bicubic scaling
  105. static void hScale8To15_c(SwsContext *c, int16_t *dst, int dstW,
  106. const uint8_t *src, const int16_t *filter,
  107. const int32_t *filterPos, int filterSize)
  108. {
  109. int i;
  110. for (i = 0; i < dstW; i++) {
  111. int j;
  112. int srcPos = filterPos[i];
  113. int val = 0;
  114. for (j = 0; j < filterSize; j++) {
  115. val += ((int)src[srcPos + j]) * filter[filterSize * i + j];
  116. }
  117. dst[i] = FFMIN(val >> 7, (1 << 15) - 1); // the cubic equation does overflow ...
  118. }
  119. }
  120. static void hScale8To19_c(SwsContext *c, int16_t *_dst, int dstW,
  121. const uint8_t *src, const int16_t *filter,
  122. const int32_t *filterPos, int filterSize)
  123. {
  124. int i;
  125. int32_t *dst = (int32_t *) _dst;
  126. for (i = 0; i < dstW; i++) {
  127. int j;
  128. int srcPos = filterPos[i];
  129. int val = 0;
  130. for (j = 0; j < filterSize; j++) {
  131. val += ((int)src[srcPos + j]) * filter[filterSize * i + j];
  132. }
  133. dst[i] = FFMIN(val >> 3, (1 << 19) - 1); // the cubic equation does overflow ...
  134. }
  135. }
  136. // FIXME all pal and rgb srcFormats could do this conversion as well
  137. // FIXME all scalers more complex than bilinear could do half of this transform
  138. static void chrRangeToJpeg_c(int16_t *dstU, int16_t *dstV, int width)
  139. {
  140. int i;
  141. for (i = 0; i < width; i++) {
  142. dstU[i] = (FFMIN(dstU[i], 30775) * 4663 - 9289992) >> 12; // -264
  143. dstV[i] = (FFMIN(dstV[i], 30775) * 4663 - 9289992) >> 12; // -264
  144. }
  145. }
  146. static void chrRangeFromJpeg_c(int16_t *dstU, int16_t *dstV, int width)
  147. {
  148. int i;
  149. for (i = 0; i < width; i++) {
  150. dstU[i] = (dstU[i] * 1799 + 4081085) >> 11; // 1469
  151. dstV[i] = (dstV[i] * 1799 + 4081085) >> 11; // 1469
  152. }
  153. }
  154. static void lumRangeToJpeg_c(int16_t *dst, int width)
  155. {
  156. int i;
  157. for (i = 0; i < width; i++)
  158. dst[i] = (FFMIN(dst[i], 30189) * 19077 - 39057361) >> 14;
  159. }
  160. static void lumRangeFromJpeg_c(int16_t *dst, int width)
  161. {
  162. int i;
  163. for (i = 0; i < width; i++)
  164. dst[i] = (dst[i] * 14071 + 33561947) >> 14;
  165. }
  166. static void chrRangeToJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
  167. {
  168. int i;
  169. int32_t *dstU = (int32_t *) _dstU;
  170. int32_t *dstV = (int32_t *) _dstV;
  171. for (i = 0; i < width; i++) {
  172. dstU[i] = (FFMIN(dstU[i], 30775 << 4) * 4663 - (9289992 << 4)) >> 12; // -264
  173. dstV[i] = (FFMIN(dstV[i], 30775 << 4) * 4663 - (9289992 << 4)) >> 12; // -264
  174. }
  175. }
  176. static void chrRangeFromJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
  177. {
  178. int i;
  179. int32_t *dstU = (int32_t *) _dstU;
  180. int32_t *dstV = (int32_t *) _dstV;
  181. for (i = 0; i < width; i++) {
  182. dstU[i] = (dstU[i] * 1799 + (4081085 << 4)) >> 11; // 1469
  183. dstV[i] = (dstV[i] * 1799 + (4081085 << 4)) >> 11; // 1469
  184. }
  185. }
  186. static void lumRangeToJpeg16_c(int16_t *_dst, int width)
  187. {
  188. int i;
  189. int32_t *dst = (int32_t *) _dst;
  190. for (i = 0; i < width; i++) {
  191. dst[i] = ((int)(FFMIN(dst[i], 30189 << 4) * 4769U - (39057361 << 2))) >> 12;
  192. }
  193. }
  194. static void lumRangeFromJpeg16_c(int16_t *_dst, int width)
  195. {
  196. int i;
  197. int32_t *dst = (int32_t *) _dst;
  198. for (i = 0; i < width; i++)
  199. dst[i] = (dst[i]*(14071/4) + (33561947<<4)/4)>>12;
  200. }
  201. #define DEBUG_SWSCALE_BUFFERS 0
  202. #define DEBUG_BUFFERS(...) \
  203. if (DEBUG_SWSCALE_BUFFERS) \
  204. av_log(c, AV_LOG_DEBUG, __VA_ARGS__)
  205. static int swscale(SwsContext *c, const uint8_t *src[],
  206. int srcStride[], int srcSliceY,
  207. int srcSliceH, uint8_t *dst[], int dstStride[])
  208. {
  209. /* load a few things into local vars to make the code more readable?
  210. * and faster */
  211. const int dstW = c->dstW;
  212. const int dstH = c->dstH;
  213. const enum AVPixelFormat dstFormat = c->dstFormat;
  214. const int flags = c->flags;
  215. int32_t *vLumFilterPos = c->vLumFilterPos;
  216. int32_t *vChrFilterPos = c->vChrFilterPos;
  217. const int vLumFilterSize = c->vLumFilterSize;
  218. const int vChrFilterSize = c->vChrFilterSize;
  219. yuv2planar1_fn yuv2plane1 = c->yuv2plane1;
  220. yuv2planarX_fn yuv2planeX = c->yuv2planeX;
  221. yuv2interleavedX_fn yuv2nv12cX = c->yuv2nv12cX;
  222. yuv2packed1_fn yuv2packed1 = c->yuv2packed1;
  223. yuv2packed2_fn yuv2packed2 = c->yuv2packed2;
  224. yuv2packedX_fn yuv2packedX = c->yuv2packedX;
  225. yuv2anyX_fn yuv2anyX = c->yuv2anyX;
  226. const int chrSrcSliceY = srcSliceY >> c->chrSrcVSubSample;
  227. const int chrSrcSliceH = AV_CEIL_RSHIFT(srcSliceH, c->chrSrcVSubSample);
  228. int should_dither = is9_OR_10BPS(c->srcFormat) ||
  229. is16BPS(c->srcFormat);
  230. int lastDstY;
  231. /* vars which will change and which we need to store back in the context */
  232. int dstY = c->dstY;
  233. int lumBufIndex = c->lumBufIndex;
  234. int chrBufIndex = c->chrBufIndex;
  235. int lastInLumBuf = c->lastInLumBuf;
  236. int lastInChrBuf = c->lastInChrBuf;
  237. int lumStart = 0;
  238. int lumEnd = c->descIndex[0];
  239. int chrStart = lumEnd;
  240. int chrEnd = c->descIndex[1];
  241. int vStart = chrEnd;
  242. int vEnd = c->numDesc;
  243. SwsSlice *src_slice = &c->slice[lumStart];
  244. SwsSlice *hout_slice = &c->slice[c->numSlice-2];
  245. SwsSlice *vout_slice = &c->slice[c->numSlice-1];
  246. SwsFilterDescriptor *desc = c->desc;
  247. int needAlpha = c->needAlpha;
  248. int hasLumHoles = 1;
  249. int hasChrHoles = 1;
  250. if (isPacked(c->srcFormat)) {
  251. src[0] =
  252. src[1] =
  253. src[2] =
  254. src[3] = src[0];
  255. srcStride[0] =
  256. srcStride[1] =
  257. srcStride[2] =
  258. srcStride[3] = srcStride[0];
  259. }
  260. srcStride[1] <<= c->vChrDrop;
  261. srcStride[2] <<= c->vChrDrop;
  262. DEBUG_BUFFERS("swscale() %p[%d] %p[%d] %p[%d] %p[%d] -> %p[%d] %p[%d] %p[%d] %p[%d]\n",
  263. src[0], srcStride[0], src[1], srcStride[1],
  264. src[2], srcStride[2], src[3], srcStride[3],
  265. dst[0], dstStride[0], dst[1], dstStride[1],
  266. dst[2], dstStride[2], dst[3], dstStride[3]);
  267. DEBUG_BUFFERS("srcSliceY: %d srcSliceH: %d dstY: %d dstH: %d\n",
  268. srcSliceY, srcSliceH, dstY, dstH);
  269. DEBUG_BUFFERS("vLumFilterSize: %d vChrFilterSize: %d\n",
  270. vLumFilterSize, vChrFilterSize);
  271. if (dstStride[0]&15 || dstStride[1]&15 ||
  272. dstStride[2]&15 || dstStride[3]&15) {
  273. static int warnedAlready = 0; // FIXME maybe move this into the context
  274. if (flags & SWS_PRINT_INFO && !warnedAlready) {
  275. av_log(c, AV_LOG_WARNING,
  276. "Warning: dstStride is not aligned!\n"
  277. " ->cannot do aligned memory accesses anymore\n");
  278. warnedAlready = 1;
  279. }
  280. }
  281. if ( (uintptr_t)dst[0]&15 || (uintptr_t)dst[1]&15 || (uintptr_t)dst[2]&15
  282. || (uintptr_t)src[0]&15 || (uintptr_t)src[1]&15 || (uintptr_t)src[2]&15
  283. || dstStride[0]&15 || dstStride[1]&15 || dstStride[2]&15 || dstStride[3]&15
  284. || srcStride[0]&15 || srcStride[1]&15 || srcStride[2]&15 || srcStride[3]&15
  285. ) {
  286. static int warnedAlready=0;
  287. int cpu_flags = av_get_cpu_flags();
  288. if (HAVE_MMXEXT && (cpu_flags & AV_CPU_FLAG_SSE2) && !warnedAlready){
  289. av_log(c, AV_LOG_WARNING, "Warning: data is not aligned! This can lead to a speedloss\n");
  290. warnedAlready=1;
  291. }
  292. }
  293. /* Note the user might start scaling the picture in the middle so this
  294. * will not get executed. This is not really intended but works
  295. * currently, so people might do it. */
  296. if (srcSliceY == 0) {
  297. lumBufIndex = -1;
  298. chrBufIndex = -1;
  299. dstY = 0;
  300. lastInLumBuf = -1;
  301. lastInChrBuf = -1;
  302. }
  303. if (!should_dither) {
  304. c->chrDither8 = c->lumDither8 = sws_pb_64;
  305. }
  306. lastDstY = dstY;
  307. ff_init_vscale_pfn(c, yuv2plane1, yuv2planeX, yuv2nv12cX,
  308. yuv2packed1, yuv2packed2, yuv2packedX, yuv2anyX, c->use_mmx_vfilter);
  309. ff_init_slice_from_src(src_slice, (uint8_t**)src, srcStride, c->srcW,
  310. srcSliceY, srcSliceH, chrSrcSliceY, chrSrcSliceH, 1);
  311. ff_init_slice_from_src(vout_slice, (uint8_t**)dst, dstStride, c->dstW,
  312. dstY, dstH, dstY >> c->chrDstVSubSample,
  313. AV_CEIL_RSHIFT(dstH, c->chrDstVSubSample), 0);
  314. if (srcSliceY == 0) {
  315. hout_slice->plane[0].sliceY = lastInLumBuf + 1;
  316. hout_slice->plane[1].sliceY = lastInChrBuf + 1;
  317. hout_slice->plane[2].sliceY = lastInChrBuf + 1;
  318. hout_slice->plane[3].sliceY = lastInLumBuf + 1;
  319. hout_slice->plane[0].sliceH =
  320. hout_slice->plane[1].sliceH =
  321. hout_slice->plane[2].sliceH =
  322. hout_slice->plane[3].sliceH = 0;
  323. hout_slice->width = dstW;
  324. }
  325. for (; dstY < dstH; dstY++) {
  326. const int chrDstY = dstY >> c->chrDstVSubSample;
  327. int use_mmx_vfilter= c->use_mmx_vfilter;
  328. // First line needed as input
  329. const int firstLumSrcY = FFMAX(1 - vLumFilterSize, vLumFilterPos[dstY]);
  330. const int firstLumSrcY2 = FFMAX(1 - vLumFilterSize, vLumFilterPos[FFMIN(dstY | ((1 << c->chrDstVSubSample) - 1), dstH - 1)]);
  331. // First line needed as input
  332. const int firstChrSrcY = FFMAX(1 - vChrFilterSize, vChrFilterPos[chrDstY]);
  333. // Last line needed as input
  334. int lastLumSrcY = FFMIN(c->srcH, firstLumSrcY + vLumFilterSize) - 1;
  335. int lastLumSrcY2 = FFMIN(c->srcH, firstLumSrcY2 + vLumFilterSize) - 1;
  336. int lastChrSrcY = FFMIN(c->chrSrcH, firstChrSrcY + vChrFilterSize) - 1;
  337. int enough_lines;
  338. int i;
  339. int posY, cPosY, firstPosY, lastPosY, firstCPosY, lastCPosY;
  340. // handle holes (FAST_BILINEAR & weird filters)
  341. if (firstLumSrcY > lastInLumBuf) {
  342. hasLumHoles = lastInLumBuf != firstLumSrcY - 1;
  343. if (hasLumHoles) {
  344. hout_slice->plane[0].sliceY = firstLumSrcY;
  345. hout_slice->plane[3].sliceY = firstLumSrcY;
  346. hout_slice->plane[0].sliceH =
  347. hout_slice->plane[3].sliceH = 0;
  348. }
  349. lastInLumBuf = firstLumSrcY - 1;
  350. }
  351. if (firstChrSrcY > lastInChrBuf) {
  352. hasChrHoles = lastInChrBuf != firstChrSrcY - 1;
  353. if (hasChrHoles) {
  354. hout_slice->plane[1].sliceY = firstChrSrcY;
  355. hout_slice->plane[2].sliceY = firstChrSrcY;
  356. hout_slice->plane[1].sliceH =
  357. hout_slice->plane[2].sliceH = 0;
  358. }
  359. lastInChrBuf = firstChrSrcY - 1;
  360. }
  361. DEBUG_BUFFERS("dstY: %d\n", dstY);
  362. DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n",
  363. firstLumSrcY, lastLumSrcY, lastInLumBuf);
  364. DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n",
  365. firstChrSrcY, lastChrSrcY, lastInChrBuf);
  366. // Do we have enough lines in this slice to output the dstY line
  367. enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH &&
  368. lastChrSrcY < AV_CEIL_RSHIFT(srcSliceY + srcSliceH, c->chrSrcVSubSample);
  369. if (!enough_lines) {
  370. lastLumSrcY = srcSliceY + srcSliceH - 1;
  371. lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1;
  372. DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n",
  373. lastLumSrcY, lastChrSrcY);
  374. }
  375. av_assert0((lastLumSrcY - firstLumSrcY + 1) <= hout_slice->plane[0].available_lines);
  376. av_assert0((lastChrSrcY - firstChrSrcY + 1) <= hout_slice->plane[1].available_lines);
  377. posY = hout_slice->plane[0].sliceY + hout_slice->plane[0].sliceH;
  378. if (posY <= lastLumSrcY && !hasLumHoles) {
  379. firstPosY = FFMAX(firstLumSrcY, posY);
  380. lastPosY = FFMIN(firstLumSrcY + hout_slice->plane[0].available_lines - 1, srcSliceY + srcSliceH - 1);
  381. } else {
  382. firstPosY = lastInLumBuf + 1;
  383. lastPosY = lastLumSrcY;
  384. }
  385. cPosY = hout_slice->plane[1].sliceY + hout_slice->plane[1].sliceH;
  386. if (cPosY <= lastChrSrcY && !hasChrHoles) {
  387. firstCPosY = FFMAX(firstChrSrcY, cPosY);
  388. lastCPosY = FFMIN(firstChrSrcY + hout_slice->plane[1].available_lines - 1, AV_CEIL_RSHIFT(srcSliceY + srcSliceH, c->chrSrcVSubSample) - 1);
  389. } else {
  390. firstCPosY = lastInChrBuf + 1;
  391. lastCPosY = lastChrSrcY;
  392. }
  393. ff_rotate_slice(hout_slice, lastPosY, lastCPosY);
  394. if (posY < lastLumSrcY + 1) {
  395. for (i = lumStart; i < lumEnd; ++i)
  396. desc[i].process(c, &desc[i], firstPosY, lastPosY - firstPosY + 1);
  397. }
  398. lumBufIndex += lastLumSrcY - lastInLumBuf;
  399. lastInLumBuf = lastLumSrcY;
  400. if (cPosY < lastChrSrcY + 1) {
  401. for (i = chrStart; i < chrEnd; ++i)
  402. desc[i].process(c, &desc[i], firstCPosY, lastCPosY - firstCPosY + 1);
  403. }
  404. chrBufIndex += lastChrSrcY - lastInChrBuf;
  405. lastInChrBuf = lastChrSrcY;
  406. // wrap buf index around to stay inside the ring buffer
  407. if (lumBufIndex >= vLumFilterSize)
  408. lumBufIndex -= vLumFilterSize;
  409. if (chrBufIndex >= vChrFilterSize)
  410. chrBufIndex -= vChrFilterSize;
  411. if (!enough_lines)
  412. break; // we can't output a dstY line so let's try with the next slice
  413. #if HAVE_MMX_INLINE
  414. ff_updateMMXDitherTables(c, dstY, lumBufIndex, chrBufIndex,
  415. lastInLumBuf, lastInChrBuf);
  416. #endif
  417. if (should_dither) {
  418. c->chrDither8 = ff_dither_8x8_128[chrDstY & 7];
  419. c->lumDither8 = ff_dither_8x8_128[dstY & 7];
  420. }
  421. if (dstY >= dstH - 2) {
  422. /* hmm looks like we can't use MMX here without overwriting
  423. * this array's tail */
  424. ff_sws_init_output_funcs(c, &yuv2plane1, &yuv2planeX, &yuv2nv12cX,
  425. &yuv2packed1, &yuv2packed2, &yuv2packedX, &yuv2anyX);
  426. use_mmx_vfilter= 0;
  427. ff_init_vscale_pfn(c, yuv2plane1, yuv2planeX, yuv2nv12cX,
  428. yuv2packed1, yuv2packed2, yuv2packedX, yuv2anyX, use_mmx_vfilter);
  429. }
  430. {
  431. for (i = vStart; i < vEnd; ++i)
  432. desc[i].process(c, &desc[i], dstY, 1);
  433. }
  434. }
  435. if (isPlanar(dstFormat) && isALPHA(dstFormat) && !needAlpha) {
  436. int length = dstW;
  437. int height = dstY - lastDstY;
  438. if (is16BPS(dstFormat) || isNBPS(dstFormat)) {
  439. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
  440. fillPlane16(dst[3], dstStride[3], length, height, lastDstY,
  441. 1, desc->comp[3].depth,
  442. isBE(dstFormat));
  443. } else
  444. fillPlane(dst[3], dstStride[3], length, height, lastDstY, 255);
  445. }
  446. #if HAVE_MMXEXT_INLINE
  447. if (av_get_cpu_flags() & AV_CPU_FLAG_MMXEXT)
  448. __asm__ volatile ("sfence" ::: "memory");
  449. #endif
  450. emms_c();
  451. /* store changed local vars back in the context */
  452. c->dstY = dstY;
  453. c->lumBufIndex = lumBufIndex;
  454. c->chrBufIndex = chrBufIndex;
  455. c->lastInLumBuf = lastInLumBuf;
  456. c->lastInChrBuf = lastInChrBuf;
  457. return dstY - lastDstY;
  458. }
  459. av_cold void ff_sws_init_range_convert(SwsContext *c)
  460. {
  461. c->lumConvertRange = NULL;
  462. c->chrConvertRange = NULL;
  463. if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
  464. if (c->dstBpc <= 14) {
  465. if (c->srcRange) {
  466. c->lumConvertRange = lumRangeFromJpeg_c;
  467. c->chrConvertRange = chrRangeFromJpeg_c;
  468. } else {
  469. c->lumConvertRange = lumRangeToJpeg_c;
  470. c->chrConvertRange = chrRangeToJpeg_c;
  471. }
  472. } else {
  473. if (c->srcRange) {
  474. c->lumConvertRange = lumRangeFromJpeg16_c;
  475. c->chrConvertRange = chrRangeFromJpeg16_c;
  476. } else {
  477. c->lumConvertRange = lumRangeToJpeg16_c;
  478. c->chrConvertRange = chrRangeToJpeg16_c;
  479. }
  480. }
  481. }
  482. }
  483. static av_cold void sws_init_swscale(SwsContext *c)
  484. {
  485. enum AVPixelFormat srcFormat = c->srcFormat;
  486. ff_sws_init_output_funcs(c, &c->yuv2plane1, &c->yuv2planeX,
  487. &c->yuv2nv12cX, &c->yuv2packed1,
  488. &c->yuv2packed2, &c->yuv2packedX, &c->yuv2anyX);
  489. ff_sws_init_input_funcs(c);
  490. if (HAVE_VSX && (!HAVE_BIGENDIAN)) {
  491. ff_sws_init_input_funcs_vsx(c);
  492. }
  493. if (c->srcBpc == 8) {
  494. if (c->dstBpc <= 14) {
  495. c->hyScale = c->hcScale = hScale8To15_c;
  496. if (c->flags & SWS_FAST_BILINEAR) {
  497. c->hyscale_fast = ff_hyscale_fast_c;
  498. c->hcscale_fast = ff_hcscale_fast_c;
  499. }
  500. } else {
  501. c->hyScale = c->hcScale = hScale8To19_c;
  502. }
  503. } else {
  504. c->hyScale = c->hcScale = c->dstBpc > 14 ? hScale16To19_c
  505. : hScale16To15_c;
  506. }
  507. ff_sws_init_range_convert(c);
  508. if (!(isGray(srcFormat) || isGray(c->dstFormat) ||
  509. srcFormat == AV_PIX_FMT_MONOBLACK || srcFormat == AV_PIX_FMT_MONOWHITE))
  510. c->needs_hcscale = 1;
  511. }
  512. SwsFunc ff_getSwsFunc(SwsContext *c)
  513. {
  514. sws_init_swscale(c);
  515. if (ARCH_PPC)
  516. ff_sws_init_swscale_ppc(c);
  517. if (ARCH_X86)
  518. ff_sws_init_swscale_x86(c);
  519. if (ARCH_AARCH64)
  520. ff_sws_init_swscale_aarch64(c);
  521. if (ARCH_ARM)
  522. ff_sws_init_swscale_arm(c);
  523. return swscale;
  524. }
  525. static void reset_ptr(const uint8_t *src[], enum AVPixelFormat format)
  526. {
  527. if (!isALPHA(format))
  528. src[3] = NULL;
  529. if (!isPlanar(format)) {
  530. src[3] = src[2] = NULL;
  531. if (!usePal(format))
  532. src[1] = NULL;
  533. }
  534. }
  535. static int check_image_pointers(const uint8_t * const data[4], enum AVPixelFormat pix_fmt,
  536. const int linesizes[4])
  537. {
  538. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  539. int i;
  540. av_assert2(desc);
  541. for (i = 0; i < 4; i++) {
  542. int plane = desc->comp[i].plane;
  543. if (!data[plane] || !linesizes[plane])
  544. return 0;
  545. }
  546. return 1;
  547. }
  548. static void xyz12Torgb48(struct SwsContext *c, uint16_t *dst,
  549. const uint16_t *src, int stride, int h)
  550. {
  551. int xp,yp;
  552. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
  553. for (yp=0; yp<h; yp++) {
  554. for (xp=0; xp+2<stride; xp+=3) {
  555. int x, y, z, r, g, b;
  556. if (desc->flags & AV_PIX_FMT_FLAG_BE) {
  557. x = AV_RB16(src + xp + 0);
  558. y = AV_RB16(src + xp + 1);
  559. z = AV_RB16(src + xp + 2);
  560. } else {
  561. x = AV_RL16(src + xp + 0);
  562. y = AV_RL16(src + xp + 1);
  563. z = AV_RL16(src + xp + 2);
  564. }
  565. x = c->xyzgamma[x>>4];
  566. y = c->xyzgamma[y>>4];
  567. z = c->xyzgamma[z>>4];
  568. // convert from XYZlinear to sRGBlinear
  569. r = c->xyz2rgb_matrix[0][0] * x +
  570. c->xyz2rgb_matrix[0][1] * y +
  571. c->xyz2rgb_matrix[0][2] * z >> 12;
  572. g = c->xyz2rgb_matrix[1][0] * x +
  573. c->xyz2rgb_matrix[1][1] * y +
  574. c->xyz2rgb_matrix[1][2] * z >> 12;
  575. b = c->xyz2rgb_matrix[2][0] * x +
  576. c->xyz2rgb_matrix[2][1] * y +
  577. c->xyz2rgb_matrix[2][2] * z >> 12;
  578. // limit values to 12-bit depth
  579. r = av_clip_uintp2(r, 12);
  580. g = av_clip_uintp2(g, 12);
  581. b = av_clip_uintp2(b, 12);
  582. // convert from sRGBlinear to RGB and scale from 12bit to 16bit
  583. if (desc->flags & AV_PIX_FMT_FLAG_BE) {
  584. AV_WB16(dst + xp + 0, c->rgbgamma[r] << 4);
  585. AV_WB16(dst + xp + 1, c->rgbgamma[g] << 4);
  586. AV_WB16(dst + xp + 2, c->rgbgamma[b] << 4);
  587. } else {
  588. AV_WL16(dst + xp + 0, c->rgbgamma[r] << 4);
  589. AV_WL16(dst + xp + 1, c->rgbgamma[g] << 4);
  590. AV_WL16(dst + xp + 2, c->rgbgamma[b] << 4);
  591. }
  592. }
  593. src += stride;
  594. dst += stride;
  595. }
  596. }
  597. static void rgb48Toxyz12(struct SwsContext *c, uint16_t *dst,
  598. const uint16_t *src, int stride, int h)
  599. {
  600. int xp,yp;
  601. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
  602. for (yp=0; yp<h; yp++) {
  603. for (xp=0; xp+2<stride; xp+=3) {
  604. int x, y, z, r, g, b;
  605. if (desc->flags & AV_PIX_FMT_FLAG_BE) {
  606. r = AV_RB16(src + xp + 0);
  607. g = AV_RB16(src + xp + 1);
  608. b = AV_RB16(src + xp + 2);
  609. } else {
  610. r = AV_RL16(src + xp + 0);
  611. g = AV_RL16(src + xp + 1);
  612. b = AV_RL16(src + xp + 2);
  613. }
  614. r = c->rgbgammainv[r>>4];
  615. g = c->rgbgammainv[g>>4];
  616. b = c->rgbgammainv[b>>4];
  617. // convert from sRGBlinear to XYZlinear
  618. x = c->rgb2xyz_matrix[0][0] * r +
  619. c->rgb2xyz_matrix[0][1] * g +
  620. c->rgb2xyz_matrix[0][2] * b >> 12;
  621. y = c->rgb2xyz_matrix[1][0] * r +
  622. c->rgb2xyz_matrix[1][1] * g +
  623. c->rgb2xyz_matrix[1][2] * b >> 12;
  624. z = c->rgb2xyz_matrix[2][0] * r +
  625. c->rgb2xyz_matrix[2][1] * g +
  626. c->rgb2xyz_matrix[2][2] * b >> 12;
  627. // limit values to 12-bit depth
  628. x = av_clip_uintp2(x, 12);
  629. y = av_clip_uintp2(y, 12);
  630. z = av_clip_uintp2(z, 12);
  631. // convert from XYZlinear to X'Y'Z' and scale from 12bit to 16bit
  632. if (desc->flags & AV_PIX_FMT_FLAG_BE) {
  633. AV_WB16(dst + xp + 0, c->xyzgammainv[x] << 4);
  634. AV_WB16(dst + xp + 1, c->xyzgammainv[y] << 4);
  635. AV_WB16(dst + xp + 2, c->xyzgammainv[z] << 4);
  636. } else {
  637. AV_WL16(dst + xp + 0, c->xyzgammainv[x] << 4);
  638. AV_WL16(dst + xp + 1, c->xyzgammainv[y] << 4);
  639. AV_WL16(dst + xp + 2, c->xyzgammainv[z] << 4);
  640. }
  641. }
  642. src += stride;
  643. dst += stride;
  644. }
  645. }
  646. /**
  647. * swscale wrapper, so we don't need to export the SwsContext.
  648. * Assumes planar YUV to be in YUV order instead of YVU.
  649. */
  650. int attribute_align_arg sws_scale(struct SwsContext *c,
  651. const uint8_t * const srcSlice[],
  652. const int srcStride[], int srcSliceY,
  653. int srcSliceH, uint8_t *const dst[],
  654. const int dstStride[])
  655. {
  656. int i, ret;
  657. const uint8_t *src2[4];
  658. uint8_t *dst2[4];
  659. uint8_t *rgb0_tmp = NULL;
  660. int macro_height = isBayer(c->srcFormat) ? 2 : (1 << c->chrSrcVSubSample);
  661. if (!srcStride || !dstStride || !dst || !srcSlice) {
  662. av_log(c, AV_LOG_ERROR, "One of the input parameters to sws_scale() is NULL, please check the calling code\n");
  663. return 0;
  664. }
  665. if ((srcSliceY & (macro_height-1)) ||
  666. ((srcSliceH& (macro_height-1)) && srcSliceY + srcSliceH != c->srcH) ||
  667. srcSliceY + srcSliceH > c->srcH) {
  668. av_log(c, AV_LOG_ERROR, "Slice parameters %d, %d are invalid\n", srcSliceY, srcSliceH);
  669. return AVERROR(EINVAL);
  670. }
  671. if (c->gamma_flag && c->cascaded_context[0]) {
  672. ret = sws_scale(c->cascaded_context[0],
  673. srcSlice, srcStride, srcSliceY, srcSliceH,
  674. c->cascaded_tmp, c->cascaded_tmpStride);
  675. if (ret < 0)
  676. return ret;
  677. if (c->cascaded_context[2])
  678. ret = sws_scale(c->cascaded_context[1], (const uint8_t * const *)c->cascaded_tmp, c->cascaded_tmpStride, srcSliceY, srcSliceH, c->cascaded1_tmp, c->cascaded1_tmpStride);
  679. else
  680. ret = sws_scale(c->cascaded_context[1], (const uint8_t * const *)c->cascaded_tmp, c->cascaded_tmpStride, srcSliceY, srcSliceH, dst, dstStride);
  681. if (ret < 0)
  682. return ret;
  683. if (c->cascaded_context[2]) {
  684. ret = sws_scale(c->cascaded_context[2],
  685. (const uint8_t * const *)c->cascaded1_tmp, c->cascaded1_tmpStride, c->cascaded_context[1]->dstY - ret, c->cascaded_context[1]->dstY,
  686. dst, dstStride);
  687. }
  688. return ret;
  689. }
  690. if (c->cascaded_context[0] && srcSliceY == 0 && srcSliceH == c->cascaded_context[0]->srcH) {
  691. ret = sws_scale(c->cascaded_context[0],
  692. srcSlice, srcStride, srcSliceY, srcSliceH,
  693. c->cascaded_tmp, c->cascaded_tmpStride);
  694. if (ret < 0)
  695. return ret;
  696. ret = sws_scale(c->cascaded_context[1],
  697. (const uint8_t * const * )c->cascaded_tmp, c->cascaded_tmpStride, 0, c->cascaded_context[0]->dstH,
  698. dst, dstStride);
  699. return ret;
  700. }
  701. memcpy(src2, srcSlice, sizeof(src2));
  702. memcpy(dst2, dst, sizeof(dst2));
  703. // do not mess up sliceDir if we have a "trailing" 0-size slice
  704. if (srcSliceH == 0)
  705. return 0;
  706. if (!check_image_pointers(srcSlice, c->srcFormat, srcStride)) {
  707. av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
  708. return 0;
  709. }
  710. if (!check_image_pointers((const uint8_t* const*)dst, c->dstFormat, dstStride)) {
  711. av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
  712. return 0;
  713. }
  714. if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
  715. av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
  716. return 0;
  717. }
  718. if (c->sliceDir == 0) {
  719. if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
  720. }
  721. if (usePal(c->srcFormat)) {
  722. for (i = 0; i < 256; i++) {
  723. int r, g, b, y, u, v, a = 0xff;
  724. if (c->srcFormat == AV_PIX_FMT_PAL8) {
  725. uint32_t p = ((const uint32_t *)(srcSlice[1]))[i];
  726. a = (p >> 24) & 0xFF;
  727. r = (p >> 16) & 0xFF;
  728. g = (p >> 8) & 0xFF;
  729. b = p & 0xFF;
  730. } else if (c->srcFormat == AV_PIX_FMT_RGB8) {
  731. r = ( i >> 5 ) * 36;
  732. g = ((i >> 2) & 7) * 36;
  733. b = ( i & 3) * 85;
  734. } else if (c->srcFormat == AV_PIX_FMT_BGR8) {
  735. b = ( i >> 6 ) * 85;
  736. g = ((i >> 3) & 7) * 36;
  737. r = ( i & 7) * 36;
  738. } else if (c->srcFormat == AV_PIX_FMT_RGB4_BYTE) {
  739. r = ( i >> 3 ) * 255;
  740. g = ((i >> 1) & 3) * 85;
  741. b = ( i & 1) * 255;
  742. } else if (c->srcFormat == AV_PIX_FMT_GRAY8 || c->srcFormat == AV_PIX_FMT_GRAY8A) {
  743. r = g = b = i;
  744. } else {
  745. av_assert1(c->srcFormat == AV_PIX_FMT_BGR4_BYTE);
  746. b = ( i >> 3 ) * 255;
  747. g = ((i >> 1) & 3) * 85;
  748. r = ( i & 1) * 255;
  749. }
  750. #define RGB2YUV_SHIFT 15
  751. #define BY ( (int) (0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  752. #define BV (-(int) (0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  753. #define BU ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  754. #define GY ( (int) (0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  755. #define GV (-(int) (0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  756. #define GU (-(int) (0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  757. #define RY ( (int) (0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  758. #define RV ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  759. #define RU (-(int) (0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  760. y = av_clip_uint8((RY * r + GY * g + BY * b + ( 33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  761. u = av_clip_uint8((RU * r + GU * g + BU * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  762. v = av_clip_uint8((RV * r + GV * g + BV * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  763. c->pal_yuv[i]= y + (u<<8) + (v<<16) + ((unsigned)a<<24);
  764. switch (c->dstFormat) {
  765. case AV_PIX_FMT_BGR32:
  766. #if !HAVE_BIGENDIAN
  767. case AV_PIX_FMT_RGB24:
  768. #endif
  769. c->pal_rgb[i]= r + (g<<8) + (b<<16) + ((unsigned)a<<24);
  770. break;
  771. case AV_PIX_FMT_BGR32_1:
  772. #if HAVE_BIGENDIAN
  773. case AV_PIX_FMT_BGR24:
  774. #endif
  775. c->pal_rgb[i]= a + (r<<8) + (g<<16) + ((unsigned)b<<24);
  776. break;
  777. case AV_PIX_FMT_RGB32_1:
  778. #if HAVE_BIGENDIAN
  779. case AV_PIX_FMT_RGB24:
  780. #endif
  781. c->pal_rgb[i]= a + (b<<8) + (g<<16) + ((unsigned)r<<24);
  782. break;
  783. case AV_PIX_FMT_RGB32:
  784. #if !HAVE_BIGENDIAN
  785. case AV_PIX_FMT_BGR24:
  786. #endif
  787. default:
  788. c->pal_rgb[i]= b + (g<<8) + (r<<16) + ((unsigned)a<<24);
  789. }
  790. }
  791. }
  792. if (c->src0Alpha && !c->dst0Alpha && isALPHA(c->dstFormat)) {
  793. uint8_t *base;
  794. int x,y;
  795. rgb0_tmp = av_malloc(FFABS(srcStride[0]) * srcSliceH + 32);
  796. if (!rgb0_tmp)
  797. return AVERROR(ENOMEM);
  798. base = srcStride[0] < 0 ? rgb0_tmp - srcStride[0] * (srcSliceH-1) : rgb0_tmp;
  799. for (y=0; y<srcSliceH; y++){
  800. memcpy(base + srcStride[0]*y, src2[0] + srcStride[0]*y, 4*c->srcW);
  801. for (x=c->src0Alpha-1; x<4*c->srcW; x+=4) {
  802. base[ srcStride[0]*y + x] = 0xFF;
  803. }
  804. }
  805. src2[0] = base;
  806. }
  807. if (c->srcXYZ && !(c->dstXYZ && c->srcW==c->dstW && c->srcH==c->dstH)) {
  808. uint8_t *base;
  809. rgb0_tmp = av_malloc(FFABS(srcStride[0]) * srcSliceH + 32);
  810. if (!rgb0_tmp)
  811. return AVERROR(ENOMEM);
  812. base = srcStride[0] < 0 ? rgb0_tmp - srcStride[0] * (srcSliceH-1) : rgb0_tmp;
  813. xyz12Torgb48(c, (uint16_t*)base, (const uint16_t*)src2[0], srcStride[0]/2, srcSliceH);
  814. src2[0] = base;
  815. }
  816. if (!srcSliceY && (c->flags & SWS_BITEXACT) && c->dither == SWS_DITHER_ED && c->dither_error[0])
  817. for (i = 0; i < 4; i++)
  818. memset(c->dither_error[i], 0, sizeof(c->dither_error[0][0]) * (c->dstW+2));
  819. // copy strides, so they can safely be modified
  820. if (c->sliceDir == 1) {
  821. // slices go from top to bottom
  822. int srcStride2[4] = { srcStride[0], srcStride[1], srcStride[2],
  823. srcStride[3] };
  824. int dstStride2[4] = { dstStride[0], dstStride[1], dstStride[2],
  825. dstStride[3] };
  826. reset_ptr(src2, c->srcFormat);
  827. reset_ptr((void*)dst2, c->dstFormat);
  828. /* reset slice direction at end of frame */
  829. if (srcSliceY + srcSliceH == c->srcH)
  830. c->sliceDir = 0;
  831. ret = c->swscale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2,
  832. dstStride2);
  833. } else {
  834. // slices go from bottom to top => we flip the image internally
  835. int srcStride2[4] = { -srcStride[0], -srcStride[1], -srcStride[2],
  836. -srcStride[3] };
  837. int dstStride2[4] = { -dstStride[0], -dstStride[1], -dstStride[2],
  838. -dstStride[3] };
  839. src2[0] += (srcSliceH - 1) * srcStride[0];
  840. if (!usePal(c->srcFormat))
  841. src2[1] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[1];
  842. src2[2] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[2];
  843. src2[3] += (srcSliceH - 1) * srcStride[3];
  844. dst2[0] += ( c->dstH - 1) * dstStride[0];
  845. dst2[1] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[1];
  846. dst2[2] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[2];
  847. dst2[3] += ( c->dstH - 1) * dstStride[3];
  848. reset_ptr(src2, c->srcFormat);
  849. reset_ptr((void*)dst2, c->dstFormat);
  850. /* reset slice direction at end of frame */
  851. if (!srcSliceY)
  852. c->sliceDir = 0;
  853. ret = c->swscale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH,
  854. srcSliceH, dst2, dstStride2);
  855. }
  856. if (c->dstXYZ && !(c->srcXYZ && c->srcW==c->dstW && c->srcH==c->dstH)) {
  857. /* replace on the same data */
  858. rgb48Toxyz12(c, (uint16_t*)dst2[0], (const uint16_t*)dst2[0], dstStride[0]/2, ret);
  859. }
  860. av_free(rgb0_tmp);
  861. return ret;
  862. }