swscale.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  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 <stdint.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/bswap.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/cpu.h"
  27. #include "libavutil/emms.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/mem.h"
  30. #include "libavutil/mem_internal.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "config.h"
  33. #include "swscale_internal.h"
  34. #include "swscale.h"
  35. DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_128)[9][8] = {
  36. { 36, 68, 60, 92, 34, 66, 58, 90, },
  37. { 100, 4, 124, 28, 98, 2, 122, 26, },
  38. { 52, 84, 44, 76, 50, 82, 42, 74, },
  39. { 116, 20, 108, 12, 114, 18, 106, 10, },
  40. { 32, 64, 56, 88, 38, 70, 62, 94, },
  41. { 96, 0, 120, 24, 102, 6, 126, 30, },
  42. { 48, 80, 40, 72, 54, 86, 46, 78, },
  43. { 112, 16, 104, 8, 118, 22, 110, 14, },
  44. { 36, 68, 60, 92, 34, 66, 58, 90, },
  45. };
  46. DECLARE_ALIGNED(8, static const uint8_t, sws_pb_64)[8] = {
  47. 64, 64, 64, 64, 64, 64, 64, 64
  48. };
  49. static av_always_inline void fillPlane(uint8_t *plane, int stride, int width,
  50. int height, int y, uint8_t val)
  51. {
  52. int i;
  53. uint8_t *ptr = plane + stride * y;
  54. for (i = 0; i < height; i++) {
  55. memset(ptr, val, width);
  56. ptr += stride;
  57. }
  58. }
  59. static void hScale16To19_c(SwsContext *c, int16_t *_dst, int dstW,
  60. const uint8_t *_src, const int16_t *filter,
  61. const int32_t *filterPos, int filterSize)
  62. {
  63. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
  64. int i;
  65. int32_t *dst = (int32_t *) _dst;
  66. const uint16_t *src = (const uint16_t *) _src;
  67. int bits = desc->comp[0].depth - 1;
  68. int sh = bits - 4;
  69. if ((isAnyRGB(c->srcFormat) || c->srcFormat==AV_PIX_FMT_PAL8) && desc->comp[0].depth<16) {
  70. sh = 9;
  71. } else if (desc->flags & AV_PIX_FMT_FLAG_FLOAT) { /* float input are process like uint 16bpc */
  72. sh = 16 - 1 - 4;
  73. }
  74. for (i = 0; i < dstW; i++) {
  75. int j;
  76. int srcPos = filterPos[i];
  77. int val = 0;
  78. for (j = 0; j < filterSize; j++) {
  79. val += src[srcPos + j] * filter[filterSize * i + j];
  80. }
  81. // filter=14 bit, input=16 bit, output=30 bit, >> 11 makes 19 bit
  82. dst[i] = FFMIN(val >> sh, (1 << 19) - 1);
  83. }
  84. }
  85. static void hScale16To15_c(SwsContext *c, int16_t *dst, int dstW,
  86. const uint8_t *_src, const int16_t *filter,
  87. const int32_t *filterPos, int filterSize)
  88. {
  89. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
  90. int i;
  91. const uint16_t *src = (const uint16_t *) _src;
  92. int sh = desc->comp[0].depth - 1;
  93. if (sh<15) {
  94. sh = isAnyRGB(c->srcFormat) || c->srcFormat==AV_PIX_FMT_PAL8 ? 13 : (desc->comp[0].depth - 1);
  95. } else if (desc->flags & AV_PIX_FMT_FLAG_FLOAT) { /* float input are process like uint 16bpc */
  96. sh = 16 - 1;
  97. }
  98. for (i = 0; i < dstW; i++) {
  99. int j;
  100. int srcPos = filterPos[i];
  101. int val = 0;
  102. for (j = 0; j < filterSize; j++) {
  103. val += src[srcPos + j] * filter[filterSize * i + j];
  104. }
  105. // filter=14 bit, input=16 bit, output=30 bit, >> 15 makes 15 bit
  106. dst[i] = FFMIN(val >> sh, (1 << 15) - 1);
  107. }
  108. }
  109. // bilinear / bicubic scaling
  110. static void hScale8To15_c(SwsContext *c, int16_t *dst, int dstW,
  111. const uint8_t *src, const int16_t *filter,
  112. const int32_t *filterPos, int filterSize)
  113. {
  114. int i;
  115. for (i = 0; i < dstW; i++) {
  116. int j;
  117. int srcPos = filterPos[i];
  118. int val = 0;
  119. for (j = 0; j < filterSize; j++) {
  120. val += ((int)src[srcPos + j]) * filter[filterSize * i + j];
  121. }
  122. dst[i] = FFMIN(val >> 7, (1 << 15) - 1); // the cubic equation does overflow ...
  123. }
  124. }
  125. static void hScale8To19_c(SwsContext *c, int16_t *_dst, int dstW,
  126. const uint8_t *src, const int16_t *filter,
  127. const int32_t *filterPos, int filterSize)
  128. {
  129. int i;
  130. int32_t *dst = (int32_t *) _dst;
  131. for (i = 0; i < dstW; i++) {
  132. int j;
  133. int srcPos = filterPos[i];
  134. int val = 0;
  135. for (j = 0; j < filterSize; j++) {
  136. val += ((int)src[srcPos + j]) * filter[filterSize * i + j];
  137. }
  138. dst[i] = FFMIN(val >> 3, (1 << 19) - 1); // the cubic equation does overflow ...
  139. }
  140. }
  141. // FIXME all pal and rgb srcFormats could do this conversion as well
  142. // FIXME all scalers more complex than bilinear could do half of this transform
  143. static void chrRangeToJpeg_c(int16_t *dstU, int16_t *dstV, int width)
  144. {
  145. int i;
  146. for (i = 0; i < width; i++) {
  147. dstU[i] = (FFMIN(dstU[i], 30775) * 4663 - 9289992) >> 12; // -264
  148. dstV[i] = (FFMIN(dstV[i], 30775) * 4663 - 9289992) >> 12; // -264
  149. }
  150. }
  151. static void chrRangeFromJpeg_c(int16_t *dstU, int16_t *dstV, int width)
  152. {
  153. int i;
  154. for (i = 0; i < width; i++) {
  155. dstU[i] = (dstU[i] * 1799 + 4081085) >> 11; // 1469
  156. dstV[i] = (dstV[i] * 1799 + 4081085) >> 11; // 1469
  157. }
  158. }
  159. static void lumRangeToJpeg_c(int16_t *dst, int width)
  160. {
  161. int i;
  162. for (i = 0; i < width; i++)
  163. dst[i] = (FFMIN(dst[i], 30189) * 19077 - 39057361) >> 14;
  164. }
  165. static void lumRangeFromJpeg_c(int16_t *dst, int width)
  166. {
  167. int i;
  168. for (i = 0; i < width; i++)
  169. dst[i] = (dst[i] * 14071 + 33561947) >> 14;
  170. }
  171. static void chrRangeToJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
  172. {
  173. int i;
  174. int32_t *dstU = (int32_t *) _dstU;
  175. int32_t *dstV = (int32_t *) _dstV;
  176. for (i = 0; i < width; i++) {
  177. dstU[i] = (FFMIN(dstU[i], 30775 << 4) * 4663 - (9289992 << 4)) >> 12; // -264
  178. dstV[i] = (FFMIN(dstV[i], 30775 << 4) * 4663 - (9289992 << 4)) >> 12; // -264
  179. }
  180. }
  181. static void chrRangeFromJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
  182. {
  183. int i;
  184. int32_t *dstU = (int32_t *) _dstU;
  185. int32_t *dstV = (int32_t *) _dstV;
  186. for (i = 0; i < width; i++) {
  187. dstU[i] = (dstU[i] * 1799 + (4081085 << 4)) >> 11; // 1469
  188. dstV[i] = (dstV[i] * 1799 + (4081085 << 4)) >> 11; // 1469
  189. }
  190. }
  191. static void lumRangeToJpeg16_c(int16_t *_dst, int width)
  192. {
  193. int i;
  194. int32_t *dst = (int32_t *) _dst;
  195. for (i = 0; i < width; i++) {
  196. dst[i] = ((int)(FFMIN(dst[i], 30189 << 4) * 4769U - (39057361 << 2))) >> 12;
  197. }
  198. }
  199. static void lumRangeFromJpeg16_c(int16_t *_dst, int width)
  200. {
  201. int i;
  202. int32_t *dst = (int32_t *) _dst;
  203. for (i = 0; i < width; i++)
  204. dst[i] = (dst[i]*(14071/4) + (33561947<<4)/4)>>12;
  205. }
  206. #define DEBUG_SWSCALE_BUFFERS 0
  207. #define DEBUG_BUFFERS(...) \
  208. if (DEBUG_SWSCALE_BUFFERS) \
  209. av_log(c, AV_LOG_DEBUG, __VA_ARGS__)
  210. static int swscale(SwsContext *c, const uint8_t *src[],
  211. int srcStride[], int srcSliceY, int srcSliceH,
  212. uint8_t *dst[], int dstStride[],
  213. int dstSliceY, int dstSliceH)
  214. {
  215. const int scale_dst = dstSliceY > 0 || dstSliceH < c->dstH;
  216. /* load a few things into local vars to make the code more readable?
  217. * and faster */
  218. const int dstW = c->dstW;
  219. 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. SwsContext *const ctx = c->parent ? c->parent : c;
  277. if (flags & SWS_PRINT_INFO &&
  278. !atomic_exchange_explicit(&ctx->stride_unaligned_warned, 1, memory_order_relaxed)) {
  279. av_log(c, AV_LOG_WARNING,
  280. "Warning: dstStride is not aligned!\n"
  281. " ->cannot do aligned memory accesses anymore\n");
  282. }
  283. }
  284. #if ARCH_X86
  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. SwsContext *const ctx = c->parent ? c->parent : c;
  291. int cpu_flags = av_get_cpu_flags();
  292. if (flags & SWS_PRINT_INFO && HAVE_MMXEXT && (cpu_flags & AV_CPU_FLAG_SSE2) &&
  293. !atomic_exchange_explicit(&ctx->stride_unaligned_warned,1, memory_order_relaxed)) {
  294. av_log(c, AV_LOG_WARNING, "Warning: data is not aligned! This can lead to a speed loss\n");
  295. }
  296. }
  297. #endif
  298. if (scale_dst) {
  299. dstY = dstSliceY;
  300. dstH = dstY + dstSliceH;
  301. lastInLumBuf = -1;
  302. lastInChrBuf = -1;
  303. } else if (srcSliceY == 0) {
  304. /* Note the user might start scaling the picture in the middle so this
  305. * will not get executed. This is not really intended but works
  306. * currently, so people might do it. */
  307. dstY = 0;
  308. lastInLumBuf = -1;
  309. lastInChrBuf = -1;
  310. }
  311. if (!should_dither) {
  312. c->chrDither8 = c->lumDither8 = sws_pb_64;
  313. }
  314. lastDstY = dstY;
  315. ff_init_vscale_pfn(c, yuv2plane1, yuv2planeX, yuv2nv12cX,
  316. yuv2packed1, yuv2packed2, yuv2packedX, yuv2anyX, c->use_mmx_vfilter);
  317. ff_init_slice_from_src(src_slice, (uint8_t**)src, srcStride, c->srcW,
  318. srcSliceY, srcSliceH, chrSrcSliceY, chrSrcSliceH, 1);
  319. ff_init_slice_from_src(vout_slice, (uint8_t**)dst, dstStride, c->dstW,
  320. dstY, dstSliceH, dstY >> c->chrDstVSubSample,
  321. AV_CEIL_RSHIFT(dstSliceH, c->chrDstVSubSample), scale_dst);
  322. if (srcSliceY == 0) {
  323. hout_slice->plane[0].sliceY = lastInLumBuf + 1;
  324. hout_slice->plane[1].sliceY = lastInChrBuf + 1;
  325. hout_slice->plane[2].sliceY = lastInChrBuf + 1;
  326. hout_slice->plane[3].sliceY = lastInLumBuf + 1;
  327. hout_slice->plane[0].sliceH =
  328. hout_slice->plane[1].sliceH =
  329. hout_slice->plane[2].sliceH =
  330. hout_slice->plane[3].sliceH = 0;
  331. hout_slice->width = dstW;
  332. }
  333. for (; dstY < dstH; dstY++) {
  334. const int chrDstY = dstY >> c->chrDstVSubSample;
  335. int use_mmx_vfilter= c->use_mmx_vfilter;
  336. // First line needed as input
  337. const int firstLumSrcY = FFMAX(1 - vLumFilterSize, vLumFilterPos[dstY]);
  338. const int firstLumSrcY2 = FFMAX(1 - vLumFilterSize, vLumFilterPos[FFMIN(dstY | ((1 << c->chrDstVSubSample) - 1), c->dstH - 1)]);
  339. // First line needed as input
  340. const int firstChrSrcY = FFMAX(1 - vChrFilterSize, vChrFilterPos[chrDstY]);
  341. // Last line needed as input
  342. int lastLumSrcY = FFMIN(c->srcH, firstLumSrcY + vLumFilterSize) - 1;
  343. int lastLumSrcY2 = FFMIN(c->srcH, firstLumSrcY2 + vLumFilterSize) - 1;
  344. int lastChrSrcY = FFMIN(c->chrSrcH, firstChrSrcY + vChrFilterSize) - 1;
  345. int enough_lines;
  346. int i;
  347. int posY, cPosY, firstPosY, lastPosY, firstCPosY, lastCPosY;
  348. // handle holes (FAST_BILINEAR & weird filters)
  349. if (firstLumSrcY > lastInLumBuf) {
  350. hasLumHoles = lastInLumBuf != firstLumSrcY - 1;
  351. if (hasLumHoles) {
  352. hout_slice->plane[0].sliceY = firstLumSrcY;
  353. hout_slice->plane[3].sliceY = firstLumSrcY;
  354. hout_slice->plane[0].sliceH =
  355. hout_slice->plane[3].sliceH = 0;
  356. }
  357. lastInLumBuf = firstLumSrcY - 1;
  358. }
  359. if (firstChrSrcY > lastInChrBuf) {
  360. hasChrHoles = lastInChrBuf != firstChrSrcY - 1;
  361. if (hasChrHoles) {
  362. hout_slice->plane[1].sliceY = firstChrSrcY;
  363. hout_slice->plane[2].sliceY = firstChrSrcY;
  364. hout_slice->plane[1].sliceH =
  365. hout_slice->plane[2].sliceH = 0;
  366. }
  367. lastInChrBuf = firstChrSrcY - 1;
  368. }
  369. DEBUG_BUFFERS("dstY: %d\n", dstY);
  370. DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n",
  371. firstLumSrcY, lastLumSrcY, lastInLumBuf);
  372. DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n",
  373. firstChrSrcY, lastChrSrcY, lastInChrBuf);
  374. // Do we have enough lines in this slice to output the dstY line
  375. enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH &&
  376. lastChrSrcY < AV_CEIL_RSHIFT(srcSliceY + srcSliceH, c->chrSrcVSubSample);
  377. if (!enough_lines) {
  378. lastLumSrcY = srcSliceY + srcSliceH - 1;
  379. lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1;
  380. DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n",
  381. lastLumSrcY, lastChrSrcY);
  382. }
  383. av_assert0((lastLumSrcY - firstLumSrcY + 1) <= hout_slice->plane[0].available_lines);
  384. av_assert0((lastChrSrcY - firstChrSrcY + 1) <= hout_slice->plane[1].available_lines);
  385. posY = hout_slice->plane[0].sliceY + hout_slice->plane[0].sliceH;
  386. if (posY <= lastLumSrcY && !hasLumHoles) {
  387. firstPosY = FFMAX(firstLumSrcY, posY);
  388. lastPosY = FFMIN(firstLumSrcY + hout_slice->plane[0].available_lines - 1, srcSliceY + srcSliceH - 1);
  389. } else {
  390. firstPosY = posY;
  391. lastPosY = lastLumSrcY;
  392. }
  393. cPosY = hout_slice->plane[1].sliceY + hout_slice->plane[1].sliceH;
  394. if (cPosY <= lastChrSrcY && !hasChrHoles) {
  395. firstCPosY = FFMAX(firstChrSrcY, cPosY);
  396. lastCPosY = FFMIN(firstChrSrcY + hout_slice->plane[1].available_lines - 1, AV_CEIL_RSHIFT(srcSliceY + srcSliceH, c->chrSrcVSubSample) - 1);
  397. } else {
  398. firstCPosY = cPosY;
  399. lastCPosY = lastChrSrcY;
  400. }
  401. ff_rotate_slice(hout_slice, lastPosY, lastCPosY);
  402. if (posY < lastLumSrcY + 1) {
  403. for (i = lumStart; i < lumEnd; ++i)
  404. desc[i].process(c, &desc[i], firstPosY, lastPosY - firstPosY + 1);
  405. }
  406. lastInLumBuf = lastLumSrcY;
  407. if (cPosY < lastChrSrcY + 1) {
  408. for (i = chrStart; i < chrEnd; ++i)
  409. desc[i].process(c, &desc[i], firstCPosY, lastCPosY - firstCPosY + 1);
  410. }
  411. lastInChrBuf = lastChrSrcY;
  412. if (!enough_lines)
  413. break; // we can't output a dstY line so let's try with the next slice
  414. #if HAVE_MMX_INLINE
  415. ff_updateMMXDitherTables(c, dstY);
  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 >= c->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. for (i = vStart; i < vEnd; ++i)
  431. desc[i].process(c, &desc[i], dstY, 1);
  432. }
  433. if (isPlanar(dstFormat) && isALPHA(dstFormat) && !needAlpha) {
  434. int offset = lastDstY - dstSliceY;
  435. int length = dstW;
  436. int height = dstY - lastDstY;
  437. if (is16BPS(dstFormat) || isNBPS(dstFormat)) {
  438. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
  439. fillPlane16(dst[3], dstStride[3], length, height, offset,
  440. 1, desc->comp[3].depth,
  441. isBE(dstFormat));
  442. } else if (is32BPS(dstFormat)) {
  443. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
  444. fillPlane32(dst[3], dstStride[3], length, height, offset,
  445. 1, desc->comp[3].depth,
  446. isBE(dstFormat), desc->flags & AV_PIX_FMT_FLAG_FLOAT);
  447. } else
  448. fillPlane(dst[3], dstStride[3], length, height, offset, 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->lastInLumBuf = lastInLumBuf;
  458. c->lastInChrBuf = lastInChrBuf;
  459. return dstY - lastDstY;
  460. }
  461. av_cold void ff_sws_init_range_convert(SwsContext *c)
  462. {
  463. c->lumConvertRange = NULL;
  464. c->chrConvertRange = NULL;
  465. if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
  466. if (c->dstBpc <= 14) {
  467. if (c->srcRange) {
  468. c->lumConvertRange = lumRangeFromJpeg_c;
  469. c->chrConvertRange = chrRangeFromJpeg_c;
  470. } else {
  471. c->lumConvertRange = lumRangeToJpeg_c;
  472. c->chrConvertRange = chrRangeToJpeg_c;
  473. }
  474. } else {
  475. if (c->srcRange) {
  476. c->lumConvertRange = lumRangeFromJpeg16_c;
  477. c->chrConvertRange = chrRangeFromJpeg16_c;
  478. } else {
  479. c->lumConvertRange = lumRangeToJpeg16_c;
  480. c->chrConvertRange = chrRangeToJpeg16_c;
  481. }
  482. }
  483. }
  484. }
  485. static av_cold void sws_init_swscale(SwsContext *c)
  486. {
  487. enum AVPixelFormat srcFormat = c->srcFormat;
  488. ff_sws_init_output_funcs(c, &c->yuv2plane1, &c->yuv2planeX,
  489. &c->yuv2nv12cX, &c->yuv2packed1,
  490. &c->yuv2packed2, &c->yuv2packedX, &c->yuv2anyX);
  491. ff_sws_init_input_funcs(c);
  492. if (c->srcBpc == 8) {
  493. if (c->dstBpc <= 14) {
  494. c->hyScale = c->hcScale = hScale8To15_c;
  495. if (c->flags & SWS_FAST_BILINEAR) {
  496. c->hyscale_fast = ff_hyscale_fast_c;
  497. c->hcscale_fast = ff_hcscale_fast_c;
  498. }
  499. } else {
  500. c->hyScale = c->hcScale = hScale8To19_c;
  501. }
  502. } else {
  503. c->hyScale = c->hcScale = c->dstBpc > 14 ? hScale16To19_c
  504. : hScale16To15_c;
  505. }
  506. ff_sws_init_range_convert(c);
  507. if (!(isGray(srcFormat) || isGray(c->dstFormat) ||
  508. srcFormat == AV_PIX_FMT_MONOBLACK || srcFormat == AV_PIX_FMT_MONOWHITE))
  509. c->needs_hcscale = 1;
  510. }
  511. void ff_sws_init_scale(SwsContext *c)
  512. {
  513. sws_init_swscale(c);
  514. #if ARCH_PPC
  515. ff_sws_init_swscale_ppc(c);
  516. #elif ARCH_X86
  517. ff_sws_init_swscale_x86(c);
  518. #elif ARCH_AARCH64
  519. ff_sws_init_swscale_aarch64(c);
  520. #elif ARCH_ARM
  521. ff_sws_init_swscale_arm(c);
  522. #elif ARCH_LOONGARCH64
  523. ff_sws_init_swscale_loongarch(c);
  524. #elif ARCH_RISCV
  525. ff_sws_init_swscale_riscv(c);
  526. #endif
  527. }
  528. static void reset_ptr(const uint8_t *src[], enum AVPixelFormat format)
  529. {
  530. if (!isALPHA(format))
  531. src[3] = NULL;
  532. if (!isPlanar(format)) {
  533. src[3] = src[2] = NULL;
  534. if (!usePal(format))
  535. src[1] = NULL;
  536. }
  537. }
  538. static int check_image_pointers(const uint8_t * const data[4], enum AVPixelFormat pix_fmt,
  539. const int linesizes[4])
  540. {
  541. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  542. int i;
  543. av_assert2(desc);
  544. for (i = 0; i < 4; i++) {
  545. int plane = desc->comp[i].plane;
  546. if (!data[plane] || !linesizes[plane])
  547. return 0;
  548. }
  549. return 1;
  550. }
  551. static void xyz12Torgb48(struct SwsContext *c, uint16_t *dst,
  552. const uint16_t *src, int stride, int h)
  553. {
  554. int xp,yp;
  555. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
  556. for (yp=0; yp<h; yp++) {
  557. for (xp=0; xp+2<stride; xp+=3) {
  558. int x, y, z, r, g, b;
  559. if (desc->flags & AV_PIX_FMT_FLAG_BE) {
  560. x = AV_RB16(src + xp + 0);
  561. y = AV_RB16(src + xp + 1);
  562. z = AV_RB16(src + xp + 2);
  563. } else {
  564. x = AV_RL16(src + xp + 0);
  565. y = AV_RL16(src + xp + 1);
  566. z = AV_RL16(src + xp + 2);
  567. }
  568. x = c->xyzgamma[x>>4];
  569. y = c->xyzgamma[y>>4];
  570. z = c->xyzgamma[z>>4];
  571. // convert from XYZlinear to sRGBlinear
  572. r = c->xyz2rgb_matrix[0][0] * x +
  573. c->xyz2rgb_matrix[0][1] * y +
  574. c->xyz2rgb_matrix[0][2] * z >> 12;
  575. g = c->xyz2rgb_matrix[1][0] * x +
  576. c->xyz2rgb_matrix[1][1] * y +
  577. c->xyz2rgb_matrix[1][2] * z >> 12;
  578. b = c->xyz2rgb_matrix[2][0] * x +
  579. c->xyz2rgb_matrix[2][1] * y +
  580. c->xyz2rgb_matrix[2][2] * z >> 12;
  581. // limit values to 12-bit depth
  582. r = av_clip_uintp2(r, 12);
  583. g = av_clip_uintp2(g, 12);
  584. b = av_clip_uintp2(b, 12);
  585. // convert from sRGBlinear to RGB and scale from 12bit to 16bit
  586. if (desc->flags & AV_PIX_FMT_FLAG_BE) {
  587. AV_WB16(dst + xp + 0, c->rgbgamma[r] << 4);
  588. AV_WB16(dst + xp + 1, c->rgbgamma[g] << 4);
  589. AV_WB16(dst + xp + 2, c->rgbgamma[b] << 4);
  590. } else {
  591. AV_WL16(dst + xp + 0, c->rgbgamma[r] << 4);
  592. AV_WL16(dst + xp + 1, c->rgbgamma[g] << 4);
  593. AV_WL16(dst + xp + 2, c->rgbgamma[b] << 4);
  594. }
  595. }
  596. src += stride;
  597. dst += stride;
  598. }
  599. }
  600. static void rgb48Toxyz12(struct SwsContext *c, uint16_t *dst,
  601. const uint16_t *src, int stride, int h)
  602. {
  603. int xp,yp;
  604. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
  605. for (yp=0; yp<h; yp++) {
  606. for (xp=0; xp+2<stride; xp+=3) {
  607. int x, y, z, r, g, b;
  608. if (desc->flags & AV_PIX_FMT_FLAG_BE) {
  609. r = AV_RB16(src + xp + 0);
  610. g = AV_RB16(src + xp + 1);
  611. b = AV_RB16(src + xp + 2);
  612. } else {
  613. r = AV_RL16(src + xp + 0);
  614. g = AV_RL16(src + xp + 1);
  615. b = AV_RL16(src + xp + 2);
  616. }
  617. r = c->rgbgammainv[r>>4];
  618. g = c->rgbgammainv[g>>4];
  619. b = c->rgbgammainv[b>>4];
  620. // convert from sRGBlinear to XYZlinear
  621. x = c->rgb2xyz_matrix[0][0] * r +
  622. c->rgb2xyz_matrix[0][1] * g +
  623. c->rgb2xyz_matrix[0][2] * b >> 12;
  624. y = c->rgb2xyz_matrix[1][0] * r +
  625. c->rgb2xyz_matrix[1][1] * g +
  626. c->rgb2xyz_matrix[1][2] * b >> 12;
  627. z = c->rgb2xyz_matrix[2][0] * r +
  628. c->rgb2xyz_matrix[2][1] * g +
  629. c->rgb2xyz_matrix[2][2] * b >> 12;
  630. // limit values to 12-bit depth
  631. x = av_clip_uintp2(x, 12);
  632. y = av_clip_uintp2(y, 12);
  633. z = av_clip_uintp2(z, 12);
  634. // convert from XYZlinear to X'Y'Z' and scale from 12bit to 16bit
  635. if (desc->flags & AV_PIX_FMT_FLAG_BE) {
  636. AV_WB16(dst + xp + 0, c->xyzgammainv[x] << 4);
  637. AV_WB16(dst + xp + 1, c->xyzgammainv[y] << 4);
  638. AV_WB16(dst + xp + 2, c->xyzgammainv[z] << 4);
  639. } else {
  640. AV_WL16(dst + xp + 0, c->xyzgammainv[x] << 4);
  641. AV_WL16(dst + xp + 1, c->xyzgammainv[y] << 4);
  642. AV_WL16(dst + xp + 2, c->xyzgammainv[z] << 4);
  643. }
  644. }
  645. src += stride;
  646. dst += stride;
  647. }
  648. }
  649. static void update_palette(SwsContext *c, const uint32_t *pal)
  650. {
  651. for (int i = 0; i < 256; i++) {
  652. int r, g, b, y, u, v, a = 0xff;
  653. if (c->srcFormat == AV_PIX_FMT_PAL8) {
  654. uint32_t p = pal[i];
  655. a = (p >> 24) & 0xFF;
  656. r = (p >> 16) & 0xFF;
  657. g = (p >> 8) & 0xFF;
  658. b = p & 0xFF;
  659. } else if (c->srcFormat == AV_PIX_FMT_RGB8) {
  660. r = ( i >> 5 ) * 36;
  661. g = ((i >> 2) & 7) * 36;
  662. b = ( i & 3) * 85;
  663. } else if (c->srcFormat == AV_PIX_FMT_BGR8) {
  664. b = ( i >> 6 ) * 85;
  665. g = ((i >> 3) & 7) * 36;
  666. r = ( i & 7) * 36;
  667. } else if (c->srcFormat == AV_PIX_FMT_RGB4_BYTE) {
  668. r = ( i >> 3 ) * 255;
  669. g = ((i >> 1) & 3) * 85;
  670. b = ( i & 1) * 255;
  671. } else if (c->srcFormat == AV_PIX_FMT_GRAY8 || c->srcFormat == AV_PIX_FMT_GRAY8A) {
  672. r = g = b = i;
  673. } else {
  674. av_assert1(c->srcFormat == AV_PIX_FMT_BGR4_BYTE);
  675. b = ( i >> 3 ) * 255;
  676. g = ((i >> 1) & 3) * 85;
  677. r = ( i & 1) * 255;
  678. }
  679. #define RGB2YUV_SHIFT 15
  680. #define BY ( (int) (0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  681. #define BV (-(int) (0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  682. #define BU ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  683. #define GY ( (int) (0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  684. #define GV (-(int) (0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  685. #define GU (-(int) (0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  686. #define RY ( (int) (0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  687. #define RV ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  688. #define RU (-(int) (0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  689. y = av_clip_uint8((RY * r + GY * g + BY * b + ( 33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  690. u = av_clip_uint8((RU * r + GU * g + BU * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  691. v = av_clip_uint8((RV * r + GV * g + BV * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  692. c->pal_yuv[i]= y + (u<<8) + (v<<16) + ((unsigned)a<<24);
  693. switch (c->dstFormat) {
  694. case AV_PIX_FMT_BGR32:
  695. #if !HAVE_BIGENDIAN
  696. case AV_PIX_FMT_RGB24:
  697. #endif
  698. c->pal_rgb[i]= r + (g<<8) + (b<<16) + ((unsigned)a<<24);
  699. break;
  700. case AV_PIX_FMT_BGR32_1:
  701. #if HAVE_BIGENDIAN
  702. case AV_PIX_FMT_BGR24:
  703. #endif
  704. c->pal_rgb[i]= a + (r<<8) + (g<<16) + ((unsigned)b<<24);
  705. break;
  706. case AV_PIX_FMT_RGB32_1:
  707. #if HAVE_BIGENDIAN
  708. case AV_PIX_FMT_RGB24:
  709. #endif
  710. c->pal_rgb[i]= a + (b<<8) + (g<<16) + ((unsigned)r<<24);
  711. break;
  712. case AV_PIX_FMT_RGB32:
  713. #if !HAVE_BIGENDIAN
  714. case AV_PIX_FMT_BGR24:
  715. #endif
  716. default:
  717. c->pal_rgb[i]= b + (g<<8) + (r<<16) + ((unsigned)a<<24);
  718. }
  719. }
  720. }
  721. static int scale_internal(SwsContext *c,
  722. const uint8_t * const srcSlice[], const int srcStride[],
  723. int srcSliceY, int srcSliceH,
  724. uint8_t *const dstSlice[], const int dstStride[],
  725. int dstSliceY, int dstSliceH);
  726. static int scale_gamma(SwsContext *c,
  727. const uint8_t * const srcSlice[], const int srcStride[],
  728. int srcSliceY, int srcSliceH,
  729. uint8_t * const dstSlice[], const int dstStride[],
  730. int dstSliceY, int dstSliceH)
  731. {
  732. int ret = scale_internal(c->cascaded_context[0],
  733. srcSlice, srcStride, srcSliceY, srcSliceH,
  734. c->cascaded_tmp, c->cascaded_tmpStride, 0, c->srcH);
  735. if (ret < 0)
  736. return ret;
  737. if (c->cascaded_context[2])
  738. ret = scale_internal(c->cascaded_context[1], (const uint8_t * const *)c->cascaded_tmp,
  739. c->cascaded_tmpStride, srcSliceY, srcSliceH,
  740. c->cascaded1_tmp, c->cascaded1_tmpStride, 0, c->dstH);
  741. else
  742. ret = scale_internal(c->cascaded_context[1], (const uint8_t * const *)c->cascaded_tmp,
  743. c->cascaded_tmpStride, srcSliceY, srcSliceH,
  744. dstSlice, dstStride, dstSliceY, dstSliceH);
  745. if (ret < 0)
  746. return ret;
  747. if (c->cascaded_context[2]) {
  748. ret = scale_internal(c->cascaded_context[2], (const uint8_t * const *)c->cascaded1_tmp,
  749. c->cascaded1_tmpStride, c->cascaded_context[1]->dstY - ret,
  750. c->cascaded_context[1]->dstY,
  751. dstSlice, dstStride, dstSliceY, dstSliceH);
  752. }
  753. return ret;
  754. }
  755. static int scale_cascaded(SwsContext *c,
  756. const uint8_t * const srcSlice[], const int srcStride[],
  757. int srcSliceY, int srcSliceH,
  758. uint8_t * const dstSlice[], const int dstStride[],
  759. int dstSliceY, int dstSliceH)
  760. {
  761. int ret = scale_internal(c->cascaded_context[0],
  762. srcSlice, srcStride, srcSliceY, srcSliceH,
  763. c->cascaded_tmp, c->cascaded_tmpStride,
  764. 0, c->cascaded_context[0]->dstH);
  765. if (ret < 0)
  766. return ret;
  767. ret = scale_internal(c->cascaded_context[1],
  768. (const uint8_t * const * )c->cascaded_tmp, c->cascaded_tmpStride,
  769. 0, c->cascaded_context[0]->dstH,
  770. dstSlice, dstStride, dstSliceY, dstSliceH);
  771. return ret;
  772. }
  773. static int scale_internal(SwsContext *c,
  774. const uint8_t * const srcSlice[], const int srcStride[],
  775. int srcSliceY, int srcSliceH,
  776. uint8_t *const dstSlice[], const int dstStride[],
  777. int dstSliceY, int dstSliceH)
  778. {
  779. const int scale_dst = dstSliceY > 0 || dstSliceH < c->dstH;
  780. const int frame_start = scale_dst || !c->sliceDir;
  781. int i, ret;
  782. const uint8_t *src2[4];
  783. uint8_t *dst2[4];
  784. int macro_height_src = isBayer(c->srcFormat) ? 2 : (1 << c->chrSrcVSubSample);
  785. int macro_height_dst = isBayer(c->dstFormat) ? 2 : (1 << c->chrDstVSubSample);
  786. // copy strides, so they can safely be modified
  787. int srcStride2[4];
  788. int dstStride2[4];
  789. int srcSliceY_internal = srcSliceY;
  790. if (!srcStride || !dstStride || !dstSlice || !srcSlice) {
  791. av_log(c, AV_LOG_ERROR, "One of the input parameters to sws_scale() is NULL, please check the calling code\n");
  792. return AVERROR(EINVAL);
  793. }
  794. if ((srcSliceY & (macro_height_src - 1)) ||
  795. ((srcSliceH & (macro_height_src - 1)) && srcSliceY + srcSliceH != c->srcH) ||
  796. srcSliceY + srcSliceH > c->srcH ||
  797. (isBayer(c->srcFormat) && srcSliceH <= 1)) {
  798. av_log(c, AV_LOG_ERROR, "Slice parameters %d, %d are invalid\n", srcSliceY, srcSliceH);
  799. return AVERROR(EINVAL);
  800. }
  801. if ((dstSliceY & (macro_height_dst - 1)) ||
  802. ((dstSliceH & (macro_height_dst - 1)) && dstSliceY + dstSliceH != c->dstH) ||
  803. dstSliceY + dstSliceH > c->dstH) {
  804. av_log(c, AV_LOG_ERROR, "Slice parameters %d, %d are invalid\n", dstSliceY, dstSliceH);
  805. return AVERROR(EINVAL);
  806. }
  807. if (!check_image_pointers(srcSlice, c->srcFormat, srcStride)) {
  808. av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
  809. return AVERROR(EINVAL);
  810. }
  811. if (!check_image_pointers((const uint8_t* const*)dstSlice, c->dstFormat, dstStride)) {
  812. av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
  813. return AVERROR(EINVAL);
  814. }
  815. // do not mess up sliceDir if we have a "trailing" 0-size slice
  816. if (srcSliceH == 0)
  817. return 0;
  818. if (c->gamma_flag && c->cascaded_context[0])
  819. return scale_gamma(c, srcSlice, srcStride, srcSliceY, srcSliceH,
  820. dstSlice, dstStride, dstSliceY, dstSliceH);
  821. if (c->cascaded_context[0] && srcSliceY == 0 && srcSliceH == c->cascaded_context[0]->srcH)
  822. return scale_cascaded(c, srcSlice, srcStride, srcSliceY, srcSliceH,
  823. dstSlice, dstStride, dstSliceY, dstSliceH);
  824. if (!srcSliceY && (c->flags & SWS_BITEXACT) && c->dither == SWS_DITHER_ED && c->dither_error[0])
  825. for (i = 0; i < 4; i++)
  826. memset(c->dither_error[i], 0, sizeof(c->dither_error[0][0]) * (c->dstW+2));
  827. if (usePal(c->srcFormat))
  828. update_palette(c, (const uint32_t *)srcSlice[1]);
  829. memcpy(src2, srcSlice, sizeof(src2));
  830. memcpy(dst2, dstSlice, sizeof(dst2));
  831. memcpy(srcStride2, srcStride, sizeof(srcStride2));
  832. memcpy(dstStride2, dstStride, sizeof(dstStride2));
  833. if (frame_start && !scale_dst) {
  834. if (srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
  835. av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
  836. return AVERROR(EINVAL);
  837. }
  838. c->sliceDir = (srcSliceY == 0) ? 1 : -1;
  839. } else if (scale_dst)
  840. c->sliceDir = 1;
  841. if (c->src0Alpha && !c->dst0Alpha && isALPHA(c->dstFormat)) {
  842. uint8_t *base;
  843. int x,y;
  844. av_fast_malloc(&c->rgb0_scratch, &c->rgb0_scratch_allocated,
  845. FFABS(srcStride[0]) * srcSliceH + 32);
  846. if (!c->rgb0_scratch)
  847. return AVERROR(ENOMEM);
  848. base = srcStride[0] < 0 ? c->rgb0_scratch - srcStride[0] * (srcSliceH-1) :
  849. c->rgb0_scratch;
  850. for (y=0; y<srcSliceH; y++){
  851. memcpy(base + srcStride[0]*y, src2[0] + srcStride[0]*y, 4*c->srcW);
  852. for (x=c->src0Alpha-1; x<4*c->srcW; x+=4) {
  853. base[ srcStride[0]*y + x] = 0xFF;
  854. }
  855. }
  856. src2[0] = base;
  857. }
  858. if (c->srcXYZ && !(c->dstXYZ && c->srcW==c->dstW && c->srcH==c->dstH)) {
  859. uint8_t *base;
  860. av_fast_malloc(&c->xyz_scratch, &c->xyz_scratch_allocated,
  861. FFABS(srcStride[0]) * srcSliceH + 32);
  862. if (!c->xyz_scratch)
  863. return AVERROR(ENOMEM);
  864. base = srcStride[0] < 0 ? c->xyz_scratch - srcStride[0] * (srcSliceH-1) :
  865. c->xyz_scratch;
  866. xyz12Torgb48(c, (uint16_t*)base, (const uint16_t*)src2[0], srcStride[0]/2, srcSliceH);
  867. src2[0] = base;
  868. }
  869. if (c->sliceDir != 1) {
  870. // slices go from bottom to top => we flip the image internally
  871. for (i=0; i<4; i++) {
  872. srcStride2[i] *= -1;
  873. dstStride2[i] *= -1;
  874. }
  875. src2[0] += (srcSliceH - 1) * srcStride[0];
  876. if (!usePal(c->srcFormat))
  877. src2[1] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[1];
  878. src2[2] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[2];
  879. src2[3] += (srcSliceH - 1) * srcStride[3];
  880. dst2[0] += ( c->dstH - 1) * dstStride[0];
  881. dst2[1] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[1];
  882. dst2[2] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[2];
  883. dst2[3] += ( c->dstH - 1) * dstStride[3];
  884. srcSliceY_internal = c->srcH-srcSliceY-srcSliceH;
  885. }
  886. reset_ptr(src2, c->srcFormat);
  887. reset_ptr((void*)dst2, c->dstFormat);
  888. if (c->convert_unscaled) {
  889. int offset = srcSliceY_internal;
  890. int slice_h = srcSliceH;
  891. // for dst slice scaling, offset the pointers to match the unscaled API
  892. if (scale_dst) {
  893. av_assert0(offset == 0);
  894. for (i = 0; i < 4 && src2[i]; i++) {
  895. if (!src2[i] || (i > 0 && usePal(c->srcFormat)))
  896. break;
  897. src2[i] += (dstSliceY >> ((i == 1 || i == 2) ? c->chrSrcVSubSample : 0)) * srcStride2[i];
  898. }
  899. for (i = 0; i < 4 && dst2[i]; i++) {
  900. if (!dst2[i] || (i > 0 && usePal(c->dstFormat)))
  901. break;
  902. dst2[i] -= (dstSliceY >> ((i == 1 || i == 2) ? c->chrDstVSubSample : 0)) * dstStride2[i];
  903. }
  904. offset = dstSliceY;
  905. slice_h = dstSliceH;
  906. }
  907. ret = c->convert_unscaled(c, src2, srcStride2, offset, slice_h,
  908. dst2, dstStride2);
  909. if (scale_dst)
  910. dst2[0] += dstSliceY * dstStride2[0];
  911. } else {
  912. ret = swscale(c, src2, srcStride2, srcSliceY_internal, srcSliceH,
  913. dst2, dstStride2, dstSliceY, dstSliceH);
  914. }
  915. if (c->dstXYZ && !(c->srcXYZ && c->srcW==c->dstW && c->srcH==c->dstH)) {
  916. uint16_t *dst16;
  917. if (scale_dst) {
  918. dst16 = (uint16_t *)dst2[0];
  919. } else {
  920. int dstY = c->dstY ? c->dstY : srcSliceY + srcSliceH;
  921. av_assert0(dstY >= ret);
  922. av_assert0(ret >= 0);
  923. av_assert0(c->dstH >= dstY);
  924. dst16 = (uint16_t*)(dst2[0] + (dstY - ret) * dstStride2[0]);
  925. }
  926. /* replace on the same data */
  927. rgb48Toxyz12(c, dst16, dst16, dstStride2[0]/2, ret);
  928. }
  929. /* reset slice direction at end of frame */
  930. if ((srcSliceY_internal + srcSliceH == c->srcH) || scale_dst)
  931. c->sliceDir = 0;
  932. return ret;
  933. }
  934. void sws_frame_end(struct SwsContext *c)
  935. {
  936. av_frame_unref(c->frame_src);
  937. av_frame_unref(c->frame_dst);
  938. c->src_ranges.nb_ranges = 0;
  939. }
  940. int sws_frame_start(struct SwsContext *c, AVFrame *dst, const AVFrame *src)
  941. {
  942. int ret, allocated = 0;
  943. ret = av_frame_ref(c->frame_src, src);
  944. if (ret < 0)
  945. return ret;
  946. if (!dst->buf[0]) {
  947. dst->width = c->dstW;
  948. dst->height = c->dstH;
  949. dst->format = c->dstFormat;
  950. ret = av_frame_get_buffer(dst, 0);
  951. if (ret < 0)
  952. return ret;
  953. allocated = 1;
  954. }
  955. ret = av_frame_ref(c->frame_dst, dst);
  956. if (ret < 0) {
  957. if (allocated)
  958. av_frame_unref(dst);
  959. return ret;
  960. }
  961. return 0;
  962. }
  963. int sws_send_slice(struct SwsContext *c, unsigned int slice_start,
  964. unsigned int slice_height)
  965. {
  966. int ret;
  967. ret = ff_range_add(&c->src_ranges, slice_start, slice_height);
  968. if (ret < 0)
  969. return ret;
  970. return 0;
  971. }
  972. unsigned int sws_receive_slice_alignment(const struct SwsContext *c)
  973. {
  974. if (c->slice_ctx)
  975. return c->slice_ctx[0]->dst_slice_align;
  976. return c->dst_slice_align;
  977. }
  978. int sws_receive_slice(struct SwsContext *c, unsigned int slice_start,
  979. unsigned int slice_height)
  980. {
  981. unsigned int align = sws_receive_slice_alignment(c);
  982. uint8_t *dst[4];
  983. /* wait until complete input has been received */
  984. if (!(c->src_ranges.nb_ranges == 1 &&
  985. c->src_ranges.ranges[0].start == 0 &&
  986. c->src_ranges.ranges[0].len == c->srcH))
  987. return AVERROR(EAGAIN);
  988. if ((slice_start > 0 || slice_height < c->dstH) &&
  989. (slice_start % align || slice_height % align)) {
  990. av_log(c, AV_LOG_ERROR,
  991. "Incorrectly aligned output: %u/%u not multiples of %u\n",
  992. slice_start, slice_height, align);
  993. return AVERROR(EINVAL);
  994. }
  995. if (c->slicethread) {
  996. int nb_jobs = c->slice_ctx[0]->dither == SWS_DITHER_ED ? 1 : c->nb_slice_ctx;
  997. int ret = 0;
  998. c->dst_slice_start = slice_start;
  999. c->dst_slice_height = slice_height;
  1000. avpriv_slicethread_execute(c->slicethread, nb_jobs, 0);
  1001. for (int i = 0; i < c->nb_slice_ctx; i++) {
  1002. if (c->slice_err[i] < 0) {
  1003. ret = c->slice_err[i];
  1004. break;
  1005. }
  1006. }
  1007. memset(c->slice_err, 0, c->nb_slice_ctx * sizeof(*c->slice_err));
  1008. return ret;
  1009. }
  1010. for (int i = 0; i < FF_ARRAY_ELEMS(dst); i++) {
  1011. ptrdiff_t offset = c->frame_dst->linesize[i] * (slice_start >> c->chrDstVSubSample);
  1012. dst[i] = FF_PTR_ADD(c->frame_dst->data[i], offset);
  1013. }
  1014. return scale_internal(c, (const uint8_t * const *)c->frame_src->data,
  1015. c->frame_src->linesize, 0, c->srcH,
  1016. dst, c->frame_dst->linesize, slice_start, slice_height);
  1017. }
  1018. int sws_scale_frame(struct SwsContext *c, AVFrame *dst, const AVFrame *src)
  1019. {
  1020. int ret;
  1021. ret = sws_frame_start(c, dst, src);
  1022. if (ret < 0)
  1023. return ret;
  1024. ret = sws_send_slice(c, 0, src->height);
  1025. if (ret >= 0)
  1026. ret = sws_receive_slice(c, 0, dst->height);
  1027. sws_frame_end(c);
  1028. return ret;
  1029. }
  1030. /**
  1031. * swscale wrapper, so we don't need to export the SwsContext.
  1032. * Assumes planar YUV to be in YUV order instead of YVU.
  1033. */
  1034. int attribute_align_arg sws_scale(struct SwsContext *c,
  1035. const uint8_t * const srcSlice[],
  1036. const int srcStride[], int srcSliceY,
  1037. int srcSliceH, uint8_t *const dst[],
  1038. const int dstStride[])
  1039. {
  1040. if (c->nb_slice_ctx)
  1041. c = c->slice_ctx[0];
  1042. return scale_internal(c, srcSlice, srcStride, srcSliceY, srcSliceH,
  1043. dst, dstStride, 0, c->dstH);
  1044. }
  1045. void ff_sws_slice_worker(void *priv, int jobnr, int threadnr,
  1046. int nb_jobs, int nb_threads)
  1047. {
  1048. SwsContext *parent = priv;
  1049. SwsContext *c = parent->slice_ctx[threadnr];
  1050. const int slice_height = FFALIGN(FFMAX((parent->dst_slice_height + nb_jobs - 1) / nb_jobs, 1),
  1051. c->dst_slice_align);
  1052. const int slice_start = jobnr * slice_height;
  1053. const int slice_end = FFMIN((jobnr + 1) * slice_height, parent->dst_slice_height);
  1054. int err = 0;
  1055. if (slice_end > slice_start) {
  1056. uint8_t *dst[4] = { NULL };
  1057. for (int i = 0; i < FF_ARRAY_ELEMS(dst) && parent->frame_dst->data[i]; i++) {
  1058. const int vshift = (i == 1 || i == 2) ? c->chrDstVSubSample : 0;
  1059. const ptrdiff_t offset = parent->frame_dst->linesize[i] *
  1060. ((slice_start + parent->dst_slice_start) >> vshift);
  1061. dst[i] = parent->frame_dst->data[i] + offset;
  1062. }
  1063. err = scale_internal(c, (const uint8_t * const *)parent->frame_src->data,
  1064. parent->frame_src->linesize, 0, c->srcH,
  1065. dst, parent->frame_dst->linesize,
  1066. parent->dst_slice_start + slice_start, slice_end - slice_start);
  1067. }
  1068. parent->slice_err[threadnr] = err;
  1069. }