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.

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