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.

994 lines
35KB

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