swscale.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  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(SwsInternal *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->opts.src_format);
  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->opts.src_format) || c->opts.src_format==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(SwsInternal *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->opts.src_format);
  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->opts.src_format) || c->opts.src_format==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(SwsInternal *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(SwsInternal *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. uint32_t _coeff, int64_t _offset)
  145. {
  146. uint16_t coeff = _coeff;
  147. int32_t offset = _offset;
  148. int i;
  149. for (i = 0; i < width; i++) {
  150. int U = (dstU[i] * coeff + offset) >> 14;
  151. int V = (dstV[i] * coeff + offset) >> 14;
  152. dstU[i] = FFMIN(U, (1 << 15) - 1);
  153. dstV[i] = FFMIN(V, (1 << 15) - 1);
  154. }
  155. }
  156. static void chrRangeFromJpeg_c(int16_t *dstU, int16_t *dstV, int width,
  157. uint32_t _coeff, int64_t _offset)
  158. {
  159. uint16_t coeff = _coeff;
  160. int32_t offset = _offset;
  161. int i;
  162. for (i = 0; i < width; i++) {
  163. dstU[i] = (dstU[i] * coeff + offset) >> 14;
  164. dstV[i] = (dstV[i] * coeff + offset) >> 14;
  165. }
  166. }
  167. static void lumRangeToJpeg_c(int16_t *dst, int width,
  168. uint32_t _coeff, int64_t _offset)
  169. {
  170. uint16_t coeff = _coeff;
  171. int32_t offset = _offset;
  172. int i;
  173. for (i = 0; i < width; i++) {
  174. int Y = (dst[i] * coeff + offset) >> 14;
  175. dst[i] = FFMIN(Y, (1 << 15) - 1);
  176. }
  177. }
  178. static void lumRangeFromJpeg_c(int16_t *dst, int width,
  179. uint32_t _coeff, int64_t _offset)
  180. {
  181. uint16_t coeff = _coeff;
  182. int32_t offset = _offset;
  183. int i;
  184. for (i = 0; i < width; i++)
  185. dst[i] = (dst[i] * coeff + offset) >> 14;
  186. }
  187. static void chrRangeToJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width,
  188. uint32_t coeff, int64_t offset)
  189. {
  190. int i;
  191. int32_t *dstU = (int32_t *) _dstU;
  192. int32_t *dstV = (int32_t *) _dstV;
  193. for (i = 0; i < width; i++) {
  194. int U = ((int64_t) dstU[i] * coeff + offset) >> 18;
  195. int V = ((int64_t) dstV[i] * coeff + offset) >> 18;
  196. dstU[i] = FFMIN(U, (1 << 19) - 1);
  197. dstV[i] = FFMIN(V, (1 << 19) - 1);
  198. }
  199. }
  200. static void chrRangeFromJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width,
  201. uint32_t coeff, int64_t offset)
  202. {
  203. int i;
  204. int32_t *dstU = (int32_t *) _dstU;
  205. int32_t *dstV = (int32_t *) _dstV;
  206. for (i = 0; i < width; i++) {
  207. dstU[i] = ((int64_t) dstU[i] * coeff + offset) >> 18;
  208. dstV[i] = ((int64_t) dstV[i] * coeff + offset) >> 18;
  209. }
  210. }
  211. static void lumRangeToJpeg16_c(int16_t *_dst, int width,
  212. uint32_t coeff, int64_t offset)
  213. {
  214. int i;
  215. int32_t *dst = (int32_t *) _dst;
  216. for (i = 0; i < width; i++) {
  217. int Y = ((int64_t) dst[i] * coeff + offset) >> 18;
  218. dst[i] = FFMIN(Y, (1 << 19) - 1);
  219. }
  220. }
  221. static void lumRangeFromJpeg16_c(int16_t *_dst, int width,
  222. uint32_t coeff, int64_t offset)
  223. {
  224. int i;
  225. int32_t *dst = (int32_t *) _dst;
  226. for (i = 0; i < width; i++)
  227. dst[i] = ((int64_t) dst[i] * coeff + offset) >> 18;
  228. }
  229. #define DEBUG_SWSCALE_BUFFERS 0
  230. #define DEBUG_BUFFERS(...) \
  231. if (DEBUG_SWSCALE_BUFFERS) \
  232. av_log(c, AV_LOG_DEBUG, __VA_ARGS__)
  233. int ff_swscale(SwsInternal *c, const uint8_t *const src[], const int srcStride[],
  234. int srcSliceY, int srcSliceH, uint8_t *const dst[],
  235. const int dstStride[], int dstSliceY, int dstSliceH)
  236. {
  237. const int scale_dst = dstSliceY > 0 || dstSliceH < c->opts.dst_h;
  238. /* load a few things into local vars to make the code more readable?
  239. * and faster */
  240. const int dstW = c->opts.dst_w;
  241. int dstH = c->opts.dst_h;
  242. const enum AVPixelFormat dstFormat = c->opts.dst_format;
  243. const int flags = c->opts.flags;
  244. int32_t *vLumFilterPos = c->vLumFilterPos;
  245. int32_t *vChrFilterPos = c->vChrFilterPos;
  246. const int vLumFilterSize = c->vLumFilterSize;
  247. const int vChrFilterSize = c->vChrFilterSize;
  248. yuv2planar1_fn yuv2plane1 = c->yuv2plane1;
  249. yuv2planarX_fn yuv2planeX = c->yuv2planeX;
  250. yuv2interleavedX_fn yuv2nv12cX = c->yuv2nv12cX;
  251. yuv2packed1_fn yuv2packed1 = c->yuv2packed1;
  252. yuv2packed2_fn yuv2packed2 = c->yuv2packed2;
  253. yuv2packedX_fn yuv2packedX = c->yuv2packedX;
  254. yuv2anyX_fn yuv2anyX = c->yuv2anyX;
  255. const int chrSrcSliceY = srcSliceY >> c->chrSrcVSubSample;
  256. const int chrSrcSliceH = AV_CEIL_RSHIFT(srcSliceH, c->chrSrcVSubSample);
  257. int should_dither = isNBPS(c->opts.src_format) ||
  258. is16BPS(c->opts.src_format);
  259. int lastDstY;
  260. /* vars which will change and which we need to store back in the context */
  261. int dstY = c->dstY;
  262. int lastInLumBuf = c->lastInLumBuf;
  263. int lastInChrBuf = c->lastInChrBuf;
  264. int lumStart = 0;
  265. int lumEnd = c->descIndex[0];
  266. int chrStart = lumEnd;
  267. int chrEnd = c->descIndex[1];
  268. int vStart = chrEnd;
  269. int vEnd = c->numDesc;
  270. SwsSlice *src_slice = &c->slice[lumStart];
  271. SwsSlice *hout_slice = &c->slice[c->numSlice-2];
  272. SwsSlice *vout_slice = &c->slice[c->numSlice-1];
  273. SwsFilterDescriptor *desc = c->desc;
  274. int needAlpha = c->needAlpha;
  275. int hasLumHoles = 1;
  276. int hasChrHoles = 1;
  277. const uint8_t *src2[4];
  278. int srcStride2[4];
  279. if (isPacked(c->opts.src_format)) {
  280. src2[0] =
  281. src2[1] =
  282. src2[2] =
  283. src2[3] = src[0];
  284. srcStride2[0] =
  285. srcStride2[1] =
  286. srcStride2[2] =
  287. srcStride2[3] = srcStride[0];
  288. } else {
  289. memcpy(src2, src, sizeof(src2));
  290. memcpy(srcStride2, srcStride, sizeof(srcStride2));
  291. }
  292. srcStride2[1] *= 1 << c->vChrDrop;
  293. srcStride2[2] *= 1 << c->vChrDrop;
  294. DEBUG_BUFFERS("swscale() %p[%d] %p[%d] %p[%d] %p[%d] -> %p[%d] %p[%d] %p[%d] %p[%d]\n",
  295. src2[0], srcStride2[0], src2[1], srcStride2[1],
  296. src2[2], srcStride2[2], src2[3], srcStride2[3],
  297. dst[0], dstStride[0], dst[1], dstStride[1],
  298. dst[2], dstStride[2], dst[3], dstStride[3]);
  299. DEBUG_BUFFERS("srcSliceY: %d srcSliceH: %d dstY: %d dstH: %d\n",
  300. srcSliceY, srcSliceH, dstY, dstH);
  301. DEBUG_BUFFERS("vLumFilterSize: %d vChrFilterSize: %d\n",
  302. vLumFilterSize, vChrFilterSize);
  303. if (dstStride[0]&15 || dstStride[1]&15 ||
  304. dstStride[2]&15 || dstStride[3]&15) {
  305. SwsInternal *const ctx = c->parent ? sws_internal(c->parent) : c;
  306. if (flags & SWS_PRINT_INFO &&
  307. !atomic_exchange_explicit(&ctx->stride_unaligned_warned, 1, memory_order_relaxed)) {
  308. av_log(c, AV_LOG_WARNING,
  309. "Warning: dstStride is not aligned!\n"
  310. " ->cannot do aligned memory accesses anymore\n");
  311. }
  312. }
  313. #if ARCH_X86
  314. if ( (uintptr_t) dst[0]&15 || (uintptr_t) dst[1]&15 || (uintptr_t) dst[2]&15
  315. || (uintptr_t)src2[0]&15 || (uintptr_t)src2[1]&15 || (uintptr_t)src2[2]&15
  316. || dstStride[0]&15 || dstStride[1]&15 || dstStride[2]&15 || dstStride[3]&15
  317. || srcStride2[0]&15 || srcStride2[1]&15 || srcStride2[2]&15 || srcStride2[3]&15
  318. ) {
  319. SwsInternal *const ctx = c->parent ? sws_internal(c->parent) : c;
  320. int cpu_flags = av_get_cpu_flags();
  321. if (flags & SWS_PRINT_INFO && HAVE_MMXEXT && (cpu_flags & AV_CPU_FLAG_SSE2) &&
  322. !atomic_exchange_explicit(&ctx->stride_unaligned_warned,1, memory_order_relaxed)) {
  323. av_log(c, AV_LOG_WARNING, "Warning: data is not aligned! This can lead to a speed loss\n");
  324. }
  325. }
  326. #endif
  327. if (scale_dst) {
  328. dstY = dstSliceY;
  329. dstH = dstY + dstSliceH;
  330. lastInLumBuf = -1;
  331. lastInChrBuf = -1;
  332. } else if (srcSliceY == 0) {
  333. /* Note the user might start scaling the picture in the middle so this
  334. * will not get executed. This is not really intended but works
  335. * currently, so people might do it. */
  336. dstY = 0;
  337. lastInLumBuf = -1;
  338. lastInChrBuf = -1;
  339. }
  340. if (!should_dither) {
  341. c->chrDither8 = c->lumDither8 = sws_pb_64;
  342. }
  343. lastDstY = dstY;
  344. ff_init_vscale_pfn(c, yuv2plane1, yuv2planeX, yuv2nv12cX,
  345. yuv2packed1, yuv2packed2, yuv2packedX, yuv2anyX, c->use_mmx_vfilter);
  346. ff_init_slice_from_src(src_slice, (uint8_t**)src2, srcStride2, c->opts.src_w,
  347. srcSliceY, srcSliceH, chrSrcSliceY, chrSrcSliceH, 1);
  348. ff_init_slice_from_src(vout_slice, (uint8_t**)dst, dstStride, c->opts.dst_w,
  349. dstY, dstSliceH, dstY >> c->chrDstVSubSample,
  350. AV_CEIL_RSHIFT(dstSliceH, c->chrDstVSubSample), scale_dst);
  351. if (srcSliceY == 0) {
  352. hout_slice->plane[0].sliceY = lastInLumBuf + 1;
  353. hout_slice->plane[1].sliceY = lastInChrBuf + 1;
  354. hout_slice->plane[2].sliceY = lastInChrBuf + 1;
  355. hout_slice->plane[3].sliceY = lastInLumBuf + 1;
  356. hout_slice->plane[0].sliceH =
  357. hout_slice->plane[1].sliceH =
  358. hout_slice->plane[2].sliceH =
  359. hout_slice->plane[3].sliceH = 0;
  360. hout_slice->width = dstW;
  361. }
  362. for (; dstY < dstH; dstY++) {
  363. const int chrDstY = dstY >> c->chrDstVSubSample;
  364. int use_mmx_vfilter= c->use_mmx_vfilter;
  365. // First line needed as input
  366. const int firstLumSrcY = FFMAX(1 - vLumFilterSize, vLumFilterPos[dstY]);
  367. const int firstLumSrcY2 = FFMAX(1 - vLumFilterSize, vLumFilterPos[FFMIN(dstY | ((1 << c->chrDstVSubSample) - 1), c->opts.dst_h - 1)]);
  368. // First line needed as input
  369. const int firstChrSrcY = FFMAX(1 - vChrFilterSize, vChrFilterPos[chrDstY]);
  370. // Last line needed as input
  371. int lastLumSrcY = FFMIN(c->opts.src_h, firstLumSrcY + vLumFilterSize) - 1;
  372. int lastLumSrcY2 = FFMIN(c->opts.src_h, firstLumSrcY2 + vLumFilterSize) - 1;
  373. int lastChrSrcY = FFMIN(c->chrSrcH, firstChrSrcY + vChrFilterSize) - 1;
  374. int enough_lines;
  375. int i;
  376. int posY, cPosY, firstPosY, lastPosY, firstCPosY, lastCPosY;
  377. // handle holes (FAST_BILINEAR & weird filters)
  378. if (firstLumSrcY > lastInLumBuf) {
  379. hasLumHoles = lastInLumBuf != firstLumSrcY - 1;
  380. if (hasLumHoles) {
  381. hout_slice->plane[0].sliceY = firstLumSrcY;
  382. hout_slice->plane[3].sliceY = firstLumSrcY;
  383. hout_slice->plane[0].sliceH =
  384. hout_slice->plane[3].sliceH = 0;
  385. }
  386. lastInLumBuf = firstLumSrcY - 1;
  387. }
  388. if (firstChrSrcY > lastInChrBuf) {
  389. hasChrHoles = lastInChrBuf != firstChrSrcY - 1;
  390. if (hasChrHoles) {
  391. hout_slice->plane[1].sliceY = firstChrSrcY;
  392. hout_slice->plane[2].sliceY = firstChrSrcY;
  393. hout_slice->plane[1].sliceH =
  394. hout_slice->plane[2].sliceH = 0;
  395. }
  396. lastInChrBuf = firstChrSrcY - 1;
  397. }
  398. DEBUG_BUFFERS("dstY: %d\n", dstY);
  399. DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n",
  400. firstLumSrcY, lastLumSrcY, lastInLumBuf);
  401. DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n",
  402. firstChrSrcY, lastChrSrcY, lastInChrBuf);
  403. // Do we have enough lines in this slice to output the dstY line
  404. enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH &&
  405. lastChrSrcY < AV_CEIL_RSHIFT(srcSliceY + srcSliceH, c->chrSrcVSubSample);
  406. if (!enough_lines) {
  407. lastLumSrcY = srcSliceY + srcSliceH - 1;
  408. lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1;
  409. DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n",
  410. lastLumSrcY, lastChrSrcY);
  411. }
  412. av_assert0((lastLumSrcY - firstLumSrcY + 1) <= hout_slice->plane[0].available_lines);
  413. av_assert0((lastChrSrcY - firstChrSrcY + 1) <= hout_slice->plane[1].available_lines);
  414. posY = hout_slice->plane[0].sliceY + hout_slice->plane[0].sliceH;
  415. if (posY <= lastLumSrcY && !hasLumHoles) {
  416. firstPosY = FFMAX(firstLumSrcY, posY);
  417. lastPosY = FFMIN(firstLumSrcY + hout_slice->plane[0].available_lines - 1, srcSliceY + srcSliceH - 1);
  418. } else {
  419. firstPosY = posY;
  420. lastPosY = lastLumSrcY;
  421. }
  422. cPosY = hout_slice->plane[1].sliceY + hout_slice->plane[1].sliceH;
  423. if (cPosY <= lastChrSrcY && !hasChrHoles) {
  424. firstCPosY = FFMAX(firstChrSrcY, cPosY);
  425. lastCPosY = FFMIN(firstChrSrcY + hout_slice->plane[1].available_lines - 1, AV_CEIL_RSHIFT(srcSliceY + srcSliceH, c->chrSrcVSubSample) - 1);
  426. } else {
  427. firstCPosY = cPosY;
  428. lastCPosY = lastChrSrcY;
  429. }
  430. ff_rotate_slice(hout_slice, lastPosY, lastCPosY);
  431. if (posY < lastLumSrcY + 1) {
  432. for (i = lumStart; i < lumEnd; ++i)
  433. desc[i].process(c, &desc[i], firstPosY, lastPosY - firstPosY + 1);
  434. }
  435. lastInLumBuf = lastLumSrcY;
  436. if (cPosY < lastChrSrcY + 1) {
  437. for (i = chrStart; i < chrEnd; ++i)
  438. desc[i].process(c, &desc[i], firstCPosY, lastCPosY - firstCPosY + 1);
  439. }
  440. lastInChrBuf = lastChrSrcY;
  441. if (!enough_lines)
  442. break; // we can't output a dstY line so let's try with the next slice
  443. #if HAVE_MMX_INLINE
  444. ff_updateMMXDitherTables(c, dstY);
  445. c->dstW_mmx = c->opts.dst_w;
  446. #endif
  447. if (should_dither) {
  448. c->chrDither8 = ff_dither_8x8_128[chrDstY & 7];
  449. c->lumDither8 = ff_dither_8x8_128[dstY & 7];
  450. }
  451. if (dstY >= c->opts.dst_h - 2) {
  452. /* hmm looks like we can't use MMX here without overwriting
  453. * this array's tail */
  454. ff_sws_init_output_funcs(c, &yuv2plane1, &yuv2planeX, &yuv2nv12cX,
  455. &yuv2packed1, &yuv2packed2, &yuv2packedX, &yuv2anyX);
  456. use_mmx_vfilter= 0;
  457. ff_init_vscale_pfn(c, yuv2plane1, yuv2planeX, yuv2nv12cX,
  458. yuv2packed1, yuv2packed2, yuv2packedX, yuv2anyX, use_mmx_vfilter);
  459. }
  460. for (i = vStart; i < vEnd; ++i)
  461. desc[i].process(c, &desc[i], dstY, 1);
  462. }
  463. if (isPlanar(dstFormat) && isALPHA(dstFormat) && !needAlpha) {
  464. int offset = lastDstY - dstSliceY;
  465. int length = dstW;
  466. int height = dstY - lastDstY;
  467. if (is16BPS(dstFormat) || isNBPS(dstFormat)) {
  468. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
  469. fillPlane16(dst[3], dstStride[3], length, height, offset,
  470. 1, desc->comp[3].depth,
  471. isBE(dstFormat));
  472. } else if (is32BPS(dstFormat)) {
  473. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
  474. fillPlane32(dst[3], dstStride[3], length, height, offset,
  475. 1, desc->comp[3].depth,
  476. isBE(dstFormat), desc->flags & AV_PIX_FMT_FLAG_FLOAT);
  477. } else
  478. fillPlane(dst[3], dstStride[3], length, height, offset, 255);
  479. }
  480. #if HAVE_MMXEXT_INLINE
  481. if (av_get_cpu_flags() & AV_CPU_FLAG_MMXEXT)
  482. __asm__ volatile ("sfence" ::: "memory");
  483. #endif
  484. emms_c();
  485. /* store changed local vars back in the context */
  486. c->dstY = dstY;
  487. c->lastInLumBuf = lastInLumBuf;
  488. c->lastInChrBuf = lastInChrBuf;
  489. return dstY - lastDstY;
  490. }
  491. /*
  492. * Solve for coeff and offset:
  493. * dst = ((src << src_shift) * coeff + offset) >> (mult_shift + src_shift)
  494. *
  495. * If SwsInternal->dstBpc is > 14, coeff is uint16_t and offset is int32_t,
  496. * otherwise (SwsInternal->dstBpc is <= 14) coeff is uint32_t and offset is
  497. * int64_t.
  498. */
  499. static void solve_range_convert(uint16_t src_min, uint16_t src_max,
  500. uint16_t dst_min, uint16_t dst_max,
  501. int src_bits, int src_shift, int mult_shift,
  502. uint32_t *coeff, int64_t *offset)
  503. {
  504. uint16_t src_range = src_max - src_min;
  505. uint16_t dst_range = dst_max - dst_min;
  506. int total_shift = mult_shift + src_shift;
  507. *coeff = AV_CEIL_RSHIFT(((uint64_t) dst_range << total_shift) / src_range, src_shift);
  508. *offset = ((int64_t) dst_max << total_shift) -
  509. ((int64_t) src_max << src_shift) * *coeff;
  510. }
  511. static void init_range_convert_constants(SwsInternal *c)
  512. {
  513. const int bit_depth = c->dstBpc ? c->dstBpc : 8;
  514. const int src_bits = bit_depth <= 14 ? 15 : 19;
  515. const int src_shift = src_bits - bit_depth;
  516. const int mult_shift = bit_depth <= 14 ? 14 : 18;
  517. const uint16_t mpeg_min = 16U << (bit_depth - 8);
  518. const uint16_t mpeg_max_lum = 235U << (bit_depth - 8);
  519. const uint16_t mpeg_max_chr = 240U << (bit_depth - 8);
  520. const uint16_t jpeg_max = (1U << bit_depth) - 1;
  521. uint16_t src_min, src_max_lum, src_max_chr;
  522. uint16_t dst_min, dst_max_lum, dst_max_chr;
  523. if (c->opts.src_range) {
  524. src_min = 0;
  525. src_max_lum = jpeg_max;
  526. src_max_chr = jpeg_max;
  527. dst_min = mpeg_min;
  528. dst_max_lum = mpeg_max_lum;
  529. dst_max_chr = mpeg_max_chr;
  530. } else {
  531. src_min = mpeg_min;
  532. src_max_lum = mpeg_max_lum;
  533. src_max_chr = mpeg_max_chr;
  534. dst_min = 0;
  535. dst_max_lum = jpeg_max;
  536. dst_max_chr = jpeg_max;
  537. }
  538. solve_range_convert(src_min, src_max_lum, dst_min, dst_max_lum,
  539. src_bits, src_shift, mult_shift,
  540. &c->lumConvertRange_coeff, &c->lumConvertRange_offset);
  541. solve_range_convert(src_min, src_max_chr, dst_min, dst_max_chr,
  542. src_bits, src_shift, mult_shift,
  543. &c->chrConvertRange_coeff, &c->chrConvertRange_offset);
  544. }
  545. av_cold void ff_sws_init_range_convert(SwsInternal *c)
  546. {
  547. c->lumConvertRange = NULL;
  548. c->chrConvertRange = NULL;
  549. if (c->opts.src_range != c->opts.dst_range && !isAnyRGB(c->opts.dst_format)) {
  550. init_range_convert_constants(c);
  551. if (c->dstBpc <= 14) {
  552. if (c->opts.src_range) {
  553. c->lumConvertRange = lumRangeFromJpeg_c;
  554. c->chrConvertRange = chrRangeFromJpeg_c;
  555. } else {
  556. c->lumConvertRange = lumRangeToJpeg_c;
  557. c->chrConvertRange = chrRangeToJpeg_c;
  558. }
  559. } else {
  560. if (c->opts.src_range) {
  561. c->lumConvertRange = lumRangeFromJpeg16_c;
  562. c->chrConvertRange = chrRangeFromJpeg16_c;
  563. } else {
  564. c->lumConvertRange = lumRangeToJpeg16_c;
  565. c->chrConvertRange = chrRangeToJpeg16_c;
  566. }
  567. }
  568. #if ARCH_AARCH64
  569. ff_sws_init_range_convert_aarch64(c);
  570. #elif ARCH_LOONGARCH64
  571. ff_sws_init_range_convert_loongarch(c);
  572. #elif ARCH_RISCV
  573. ff_sws_init_range_convert_riscv(c);
  574. #elif ARCH_X86
  575. ff_sws_init_range_convert_x86(c);
  576. #endif
  577. }
  578. }
  579. static av_cold void sws_init_swscale(SwsInternal *c)
  580. {
  581. enum AVPixelFormat srcFormat = c->opts.src_format;
  582. ff_sws_init_output_funcs(c, &c->yuv2plane1, &c->yuv2planeX,
  583. &c->yuv2nv12cX, &c->yuv2packed1,
  584. &c->yuv2packed2, &c->yuv2packedX, &c->yuv2anyX);
  585. ff_sws_init_input_funcs(c, &c->lumToYV12, &c->alpToYV12, &c->chrToYV12,
  586. &c->readLumPlanar, &c->readAlpPlanar, &c->readChrPlanar);
  587. if (c->srcBpc == 8) {
  588. if (c->dstBpc <= 14) {
  589. c->hyScale = c->hcScale = hScale8To15_c;
  590. if (c->opts.flags & SWS_FAST_BILINEAR) {
  591. c->hyscale_fast = ff_hyscale_fast_c;
  592. c->hcscale_fast = ff_hcscale_fast_c;
  593. }
  594. } else {
  595. c->hyScale = c->hcScale = hScale8To19_c;
  596. }
  597. } else {
  598. c->hyScale = c->hcScale = c->dstBpc > 14 ? hScale16To19_c
  599. : hScale16To15_c;
  600. }
  601. ff_sws_init_range_convert(c);
  602. if (!(isGray(srcFormat) || isGray(c->opts.dst_format) ||
  603. srcFormat == AV_PIX_FMT_MONOBLACK || srcFormat == AV_PIX_FMT_MONOWHITE))
  604. c->needs_hcscale = 1;
  605. }
  606. void ff_sws_init_scale(SwsInternal *c)
  607. {
  608. sws_init_swscale(c);
  609. #if ARCH_PPC
  610. ff_sws_init_swscale_ppc(c);
  611. #elif ARCH_X86
  612. ff_sws_init_swscale_x86(c);
  613. #elif ARCH_AARCH64
  614. ff_sws_init_swscale_aarch64(c);
  615. #elif ARCH_ARM
  616. ff_sws_init_swscale_arm(c);
  617. #elif ARCH_LOONGARCH64
  618. ff_sws_init_swscale_loongarch(c);
  619. #elif ARCH_RISCV
  620. ff_sws_init_swscale_riscv(c);
  621. #endif
  622. }
  623. static void reset_ptr(const uint8_t *src[], enum AVPixelFormat format)
  624. {
  625. if (!isALPHA(format))
  626. src[3] = NULL;
  627. if (!isPlanar(format)) {
  628. src[3] = src[2] = NULL;
  629. if (!usePal(format))
  630. src[1] = NULL;
  631. }
  632. }
  633. static int check_image_pointers(const uint8_t * const data[4], enum AVPixelFormat pix_fmt,
  634. const int linesizes[4])
  635. {
  636. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  637. int i;
  638. av_assert2(desc);
  639. for (i = 0; i < 4; i++) {
  640. int plane = desc->comp[i].plane;
  641. if (!data[plane] || !linesizes[plane])
  642. return 0;
  643. }
  644. return 1;
  645. }
  646. void ff_xyz12Torgb48(const SwsInternal *c, uint8_t *dst, int dst_stride,
  647. const uint8_t *src, int src_stride, int w, int h)
  648. {
  649. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->opts.src_format);
  650. for (int yp = 0; yp < h; yp++) {
  651. const uint16_t *src16 = (const uint16_t *) src;
  652. uint16_t *dst16 = (uint16_t *) dst;
  653. for (int xp = 0; xp < 3 * w; xp += 3) {
  654. int x, y, z, r, g, b;
  655. if (desc->flags & AV_PIX_FMT_FLAG_BE) {
  656. x = AV_RB16(src16 + xp + 0);
  657. y = AV_RB16(src16 + xp + 1);
  658. z = AV_RB16(src16 + xp + 2);
  659. } else {
  660. x = AV_RL16(src16 + xp + 0);
  661. y = AV_RL16(src16 + xp + 1);
  662. z = AV_RL16(src16 + xp + 2);
  663. }
  664. x = c->xyzgamma[x >> 4];
  665. y = c->xyzgamma[y >> 4];
  666. z = c->xyzgamma[z >> 4];
  667. // convert from XYZlinear to sRGBlinear
  668. r = c->xyz2rgb_matrix[0][0] * x +
  669. c->xyz2rgb_matrix[0][1] * y +
  670. c->xyz2rgb_matrix[0][2] * z >> 12;
  671. g = c->xyz2rgb_matrix[1][0] * x +
  672. c->xyz2rgb_matrix[1][1] * y +
  673. c->xyz2rgb_matrix[1][2] * z >> 12;
  674. b = c->xyz2rgb_matrix[2][0] * x +
  675. c->xyz2rgb_matrix[2][1] * y +
  676. c->xyz2rgb_matrix[2][2] * z >> 12;
  677. // limit values to 16-bit depth
  678. r = av_clip_uint16(r);
  679. g = av_clip_uint16(g);
  680. b = av_clip_uint16(b);
  681. // convert from sRGBlinear to RGB and scale from 12bit to 16bit
  682. if (desc->flags & AV_PIX_FMT_FLAG_BE) {
  683. AV_WB16(dst16 + xp + 0, c->rgbgamma[r] << 4);
  684. AV_WB16(dst16 + xp + 1, c->rgbgamma[g] << 4);
  685. AV_WB16(dst16 + xp + 2, c->rgbgamma[b] << 4);
  686. } else {
  687. AV_WL16(dst16 + xp + 0, c->rgbgamma[r] << 4);
  688. AV_WL16(dst16 + xp + 1, c->rgbgamma[g] << 4);
  689. AV_WL16(dst16 + xp + 2, c->rgbgamma[b] << 4);
  690. }
  691. }
  692. src += src_stride;
  693. dst += dst_stride;
  694. }
  695. }
  696. void ff_rgb48Toxyz12(const SwsInternal *c, uint8_t *dst, int dst_stride,
  697. const uint8_t *src, int src_stride, int w, int h)
  698. {
  699. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->opts.dst_format);
  700. for (int yp = 0; yp < h; yp++) {
  701. uint16_t *src16 = (uint16_t *) src;
  702. uint16_t *dst16 = (uint16_t *) dst;
  703. for (int xp = 0; xp < 3 * w; xp += 3) {
  704. int x, y, z, r, g, b;
  705. if (desc->flags & AV_PIX_FMT_FLAG_BE) {
  706. r = AV_RB16(src16 + xp + 0);
  707. g = AV_RB16(src16 + xp + 1);
  708. b = AV_RB16(src16 + xp + 2);
  709. } else {
  710. r = AV_RL16(src16 + xp + 0);
  711. g = AV_RL16(src16 + xp + 1);
  712. b = AV_RL16(src16 + xp + 2);
  713. }
  714. r = c->rgbgammainv[r>>4];
  715. g = c->rgbgammainv[g>>4];
  716. b = c->rgbgammainv[b>>4];
  717. // convert from sRGBlinear to XYZlinear
  718. x = c->rgb2xyz_matrix[0][0] * r +
  719. c->rgb2xyz_matrix[0][1] * g +
  720. c->rgb2xyz_matrix[0][2] * b >> 12;
  721. y = c->rgb2xyz_matrix[1][0] * r +
  722. c->rgb2xyz_matrix[1][1] * g +
  723. c->rgb2xyz_matrix[1][2] * b >> 12;
  724. z = c->rgb2xyz_matrix[2][0] * r +
  725. c->rgb2xyz_matrix[2][1] * g +
  726. c->rgb2xyz_matrix[2][2] * b >> 12;
  727. // limit values to 16-bit depth
  728. x = av_clip_uint16(x);
  729. y = av_clip_uint16(y);
  730. z = av_clip_uint16(z);
  731. // convert from XYZlinear to X'Y'Z' and scale from 12bit to 16bit
  732. if (desc->flags & AV_PIX_FMT_FLAG_BE) {
  733. AV_WB16(dst16 + xp + 0, c->xyzgammainv[x] << 4);
  734. AV_WB16(dst16 + xp + 1, c->xyzgammainv[y] << 4);
  735. AV_WB16(dst16 + xp + 2, c->xyzgammainv[z] << 4);
  736. } else {
  737. AV_WL16(dst16 + xp + 0, c->xyzgammainv[x] << 4);
  738. AV_WL16(dst16 + xp + 1, c->xyzgammainv[y] << 4);
  739. AV_WL16(dst16 + xp + 2, c->xyzgammainv[z] << 4);
  740. }
  741. }
  742. src += src_stride;
  743. dst += dst_stride;
  744. }
  745. }
  746. void ff_update_palette(SwsInternal *c, const uint32_t *pal)
  747. {
  748. for (int i = 0; i < 256; i++) {
  749. int r, g, b, y, u, v, a = 0xff;
  750. if (c->opts.src_format == AV_PIX_FMT_PAL8) {
  751. uint32_t p = pal[i];
  752. a = (p >> 24) & 0xFF;
  753. r = (p >> 16) & 0xFF;
  754. g = (p >> 8) & 0xFF;
  755. b = p & 0xFF;
  756. } else if (c->opts.src_format == AV_PIX_FMT_RGB8) {
  757. r = ( i >> 5 ) * 36;
  758. g = ((i >> 2) & 7) * 36;
  759. b = ( i & 3) * 85;
  760. } else if (c->opts.src_format == AV_PIX_FMT_BGR8) {
  761. b = ( i >> 6 ) * 85;
  762. g = ((i >> 3) & 7) * 36;
  763. r = ( i & 7) * 36;
  764. } else if (c->opts.src_format == AV_PIX_FMT_RGB4_BYTE) {
  765. r = ( i >> 3 ) * 255;
  766. g = ((i >> 1) & 3) * 85;
  767. b = ( i & 1) * 255;
  768. } else if (c->opts.src_format == AV_PIX_FMT_GRAY8 || c->opts.src_format == AV_PIX_FMT_GRAY8A) {
  769. r = g = b = i;
  770. } else {
  771. av_assert1(c->opts.src_format == AV_PIX_FMT_BGR4_BYTE);
  772. b = ( i >> 3 ) * 255;
  773. g = ((i >> 1) & 3) * 85;
  774. r = ( i & 1) * 255;
  775. }
  776. #define RGB2YUV_SHIFT 15
  777. #define BY ( (int) (0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  778. #define BV (-(int) (0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  779. #define BU ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  780. #define GY ( (int) (0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  781. #define GV (-(int) (0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  782. #define GU (-(int) (0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  783. #define RY ( (int) (0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  784. #define RV ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  785. #define RU (-(int) (0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  786. y = av_clip_uint8((RY * r + GY * g + BY * b + ( 33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  787. u = av_clip_uint8((RU * r + GU * g + BU * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  788. v = av_clip_uint8((RV * r + GV * g + BV * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  789. c->pal_yuv[i]= y + (u<<8) + (v<<16) + ((unsigned)a<<24);
  790. switch (c->opts.dst_format) {
  791. case AV_PIX_FMT_BGR32:
  792. #if !HAVE_BIGENDIAN
  793. case AV_PIX_FMT_RGB24:
  794. #endif
  795. c->pal_rgb[i]= r + (g<<8) + (b<<16) + ((unsigned)a<<24);
  796. break;
  797. case AV_PIX_FMT_BGR32_1:
  798. #if HAVE_BIGENDIAN
  799. case AV_PIX_FMT_BGR24:
  800. #endif
  801. c->pal_rgb[i]= a + (r<<8) + (g<<16) + ((unsigned)b<<24);
  802. break;
  803. case AV_PIX_FMT_RGB32_1:
  804. #if HAVE_BIGENDIAN
  805. case AV_PIX_FMT_RGB24:
  806. #endif
  807. c->pal_rgb[i]= a + (b<<8) + (g<<16) + ((unsigned)r<<24);
  808. break;
  809. case AV_PIX_FMT_GBRP:
  810. case AV_PIX_FMT_GBRAP:
  811. #if HAVE_BIGENDIAN
  812. c->pal_rgb[i]= a + (r<<8) + (b<<16) + ((unsigned)g<<24);
  813. #else
  814. c->pal_rgb[i]= g + (b<<8) + (r<<16) + ((unsigned)a<<24);
  815. #endif
  816. break;
  817. case AV_PIX_FMT_RGB32:
  818. #if !HAVE_BIGENDIAN
  819. case AV_PIX_FMT_BGR24:
  820. #endif
  821. default:
  822. c->pal_rgb[i]= b + (g<<8) + (r<<16) + ((unsigned)a<<24);
  823. }
  824. }
  825. }
  826. static int scale_internal(SwsContext *sws,
  827. const uint8_t * const srcSlice[], const int srcStride[],
  828. int srcSliceY, int srcSliceH,
  829. uint8_t *const dstSlice[], const int dstStride[],
  830. int dstSliceY, int dstSliceH);
  831. static int scale_gamma(SwsInternal *c,
  832. const uint8_t * const srcSlice[], const int srcStride[],
  833. int srcSliceY, int srcSliceH,
  834. uint8_t * const dstSlice[], const int dstStride[],
  835. int dstSliceY, int dstSliceH)
  836. {
  837. int ret = scale_internal(c->cascaded_context[0],
  838. srcSlice, srcStride, srcSliceY, srcSliceH,
  839. c->cascaded_tmp[0], c->cascaded_tmpStride[0], 0, c->opts.src_h);
  840. if (ret < 0)
  841. return ret;
  842. if (c->cascaded_context[2])
  843. ret = scale_internal(c->cascaded_context[1], (const uint8_t * const *)c->cascaded_tmp[0],
  844. c->cascaded_tmpStride[0], srcSliceY, srcSliceH,
  845. c->cascaded_tmp[1], c->cascaded_tmpStride[1], 0, c->opts.dst_h);
  846. else
  847. ret = scale_internal(c->cascaded_context[1], (const uint8_t * const *)c->cascaded_tmp[0],
  848. c->cascaded_tmpStride[0], srcSliceY, srcSliceH,
  849. dstSlice, dstStride, dstSliceY, dstSliceH);
  850. if (ret < 0)
  851. return ret;
  852. if (c->cascaded_context[2]) {
  853. const int dstY1 = sws_internal(c->cascaded_context[1])->dstY;
  854. ret = scale_internal(c->cascaded_context[2], (const uint8_t * const *)c->cascaded_tmp[1],
  855. c->cascaded_tmpStride[1], dstY1 - ret, dstY1,
  856. dstSlice, dstStride, dstSliceY, dstSliceH);
  857. }
  858. return ret;
  859. }
  860. static int scale_cascaded(SwsInternal *c,
  861. const uint8_t * const srcSlice[], const int srcStride[],
  862. int srcSliceY, int srcSliceH,
  863. uint8_t * const dstSlice[], const int dstStride[],
  864. int dstSliceY, int dstSliceH)
  865. {
  866. const int dstH0 = c->cascaded_context[0]->dst_h;
  867. int ret = scale_internal(c->cascaded_context[0],
  868. srcSlice, srcStride, srcSliceY, srcSliceH,
  869. c->cascaded_tmp[0], c->cascaded_tmpStride[0],
  870. 0, dstH0);
  871. if (ret < 0)
  872. return ret;
  873. ret = scale_internal(c->cascaded_context[1],
  874. (const uint8_t * const * )c->cascaded_tmp[0], c->cascaded_tmpStride[0],
  875. 0, dstH0, dstSlice, dstStride, dstSliceY, dstSliceH);
  876. return ret;
  877. }
  878. static int scale_internal(SwsContext *sws,
  879. const uint8_t * const srcSlice[], const int srcStride[],
  880. int srcSliceY, int srcSliceH,
  881. uint8_t *const dstSlice[], const int dstStride[],
  882. int dstSliceY, int dstSliceH)
  883. {
  884. SwsInternal *c = sws_internal(sws);
  885. const int scale_dst = dstSliceY > 0 || dstSliceH < sws->dst_h;
  886. const int frame_start = scale_dst || !c->sliceDir;
  887. int i, ret;
  888. const uint8_t *src2[4];
  889. uint8_t *dst2[4];
  890. int macro_height_src = isBayer(sws->src_format) ? 2 : (1 << c->chrSrcVSubSample);
  891. int macro_height_dst = isBayer(sws->dst_format) ? 2 : (1 << c->chrDstVSubSample);
  892. // copy strides, so they can safely be modified
  893. int srcStride2[4];
  894. int dstStride2[4];
  895. int srcSliceY_internal = srcSliceY;
  896. if (!srcStride || !dstStride || !dstSlice || !srcSlice) {
  897. av_log(c, AV_LOG_ERROR, "One of the input parameters to sws_scale() is NULL, please check the calling code\n");
  898. return AVERROR(EINVAL);
  899. }
  900. if ((srcSliceY & (macro_height_src - 1)) ||
  901. ((srcSliceH & (macro_height_src - 1)) && srcSliceY + srcSliceH != sws->src_h) ||
  902. srcSliceY + srcSliceH > sws->src_h ||
  903. (isBayer(sws->src_format) && srcSliceH <= 1)) {
  904. av_log(c, AV_LOG_ERROR, "Slice parameters %d, %d are invalid\n", srcSliceY, srcSliceH);
  905. return AVERROR(EINVAL);
  906. }
  907. if ((dstSliceY & (macro_height_dst - 1)) ||
  908. ((dstSliceH & (macro_height_dst - 1)) && dstSliceY + dstSliceH != sws->dst_h) ||
  909. dstSliceY + dstSliceH > sws->dst_h) {
  910. av_log(c, AV_LOG_ERROR, "Slice parameters %d, %d are invalid\n", dstSliceY, dstSliceH);
  911. return AVERROR(EINVAL);
  912. }
  913. if (!check_image_pointers(srcSlice, sws->src_format, srcStride)) {
  914. av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
  915. return AVERROR(EINVAL);
  916. }
  917. if (!check_image_pointers((const uint8_t* const*)dstSlice, sws->dst_format, dstStride)) {
  918. av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
  919. return AVERROR(EINVAL);
  920. }
  921. // do not mess up sliceDir if we have a "trailing" 0-size slice
  922. if (srcSliceH == 0)
  923. return 0;
  924. if (sws->gamma_flag && c->cascaded_context[0])
  925. return scale_gamma(c, srcSlice, srcStride, srcSliceY, srcSliceH,
  926. dstSlice, dstStride, dstSliceY, dstSliceH);
  927. if (c->cascaded_context[0] && srcSliceY == 0 && srcSliceH == c->cascaded_context[0]->src_h)
  928. return scale_cascaded(c, srcSlice, srcStride, srcSliceY, srcSliceH,
  929. dstSlice, dstStride, dstSliceY, dstSliceH);
  930. if (!srcSliceY && (sws->flags & SWS_BITEXACT) && sws->dither == SWS_DITHER_ED && c->dither_error[0])
  931. for (i = 0; i < 4; i++)
  932. memset(c->dither_error[i], 0, sizeof(c->dither_error[0][0]) * (sws->dst_w+2));
  933. if (usePal(sws->src_format))
  934. ff_update_palette(c, (const uint32_t *)srcSlice[1]);
  935. memcpy(src2, srcSlice, sizeof(src2));
  936. memcpy(dst2, dstSlice, sizeof(dst2));
  937. memcpy(srcStride2, srcStride, sizeof(srcStride2));
  938. memcpy(dstStride2, dstStride, sizeof(dstStride2));
  939. if (frame_start && !scale_dst) {
  940. if (srcSliceY != 0 && srcSliceY + srcSliceH != sws->src_h) {
  941. av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
  942. return AVERROR(EINVAL);
  943. }
  944. c->sliceDir = (srcSliceY == 0) ? 1 : -1;
  945. } else if (scale_dst)
  946. c->sliceDir = 1;
  947. if (c->src0Alpha && !c->dst0Alpha && isALPHA(sws->dst_format)) {
  948. uint8_t *base;
  949. int x,y;
  950. av_fast_malloc(&c->rgb0_scratch, &c->rgb0_scratch_allocated,
  951. FFABS(srcStride[0]) * srcSliceH + 32);
  952. if (!c->rgb0_scratch)
  953. return AVERROR(ENOMEM);
  954. base = srcStride[0] < 0 ? c->rgb0_scratch - srcStride[0] * (srcSliceH-1) :
  955. c->rgb0_scratch;
  956. for (y=0; y<srcSliceH; y++){
  957. memcpy(base + srcStride[0]*y, src2[0] + srcStride[0]*y, 4*sws->src_w);
  958. for (x=c->src0Alpha-1; x<4*sws->src_w; x+=4) {
  959. base[ srcStride[0]*y + x] = 0xFF;
  960. }
  961. }
  962. src2[0] = base;
  963. }
  964. if (c->srcXYZ && !(c->dstXYZ && sws->src_w==sws->dst_w && sws->src_h==sws->dst_h)) {
  965. uint8_t *base;
  966. av_fast_malloc(&c->xyz_scratch, &c->xyz_scratch_allocated,
  967. FFABS(srcStride[0]) * srcSliceH + 32);
  968. if (!c->xyz_scratch)
  969. return AVERROR(ENOMEM);
  970. base = srcStride[0] < 0 ? c->xyz_scratch - srcStride[0] * (srcSliceH-1) :
  971. c->xyz_scratch;
  972. ff_xyz12Torgb48(c, base, srcStride[0], src2[0], srcStride[0], sws->src_w, srcSliceH);
  973. src2[0] = base;
  974. }
  975. if (c->sliceDir != 1) {
  976. // slices go from bottom to top => we flip the image internally
  977. for (i=0; i<4; i++) {
  978. srcStride2[i] *= -1;
  979. dstStride2[i] *= -1;
  980. }
  981. src2[0] += (srcSliceH - 1) * srcStride[0];
  982. if (!usePal(sws->src_format))
  983. src2[1] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[1];
  984. src2[2] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[2];
  985. src2[3] += (srcSliceH - 1) * srcStride[3];
  986. dst2[0] += ( sws->dst_h - 1) * dstStride[0];
  987. dst2[1] += ((sws->dst_h >> c->chrDstVSubSample) - 1) * dstStride[1];
  988. dst2[2] += ((sws->dst_h >> c->chrDstVSubSample) - 1) * dstStride[2];
  989. dst2[3] += ( sws->dst_h - 1) * dstStride[3];
  990. srcSliceY_internal = sws->src_h-srcSliceY-srcSliceH;
  991. }
  992. reset_ptr(src2, sws->src_format);
  993. reset_ptr((void*)dst2, sws->dst_format);
  994. if (c->convert_unscaled) {
  995. int offset = srcSliceY_internal;
  996. int slice_h = srcSliceH;
  997. // for dst slice scaling, offset the pointers to match the unscaled API
  998. if (scale_dst) {
  999. av_assert0(offset == 0);
  1000. for (i = 0; i < 4 && src2[i]; i++) {
  1001. if (!src2[i] || (i > 0 && usePal(sws->src_format)))
  1002. break;
  1003. src2[i] += (dstSliceY >> ((i == 1 || i == 2) ? c->chrSrcVSubSample : 0)) * srcStride2[i];
  1004. }
  1005. for (i = 0; i < 4 && dst2[i]; i++) {
  1006. if (!dst2[i] || (i > 0 && usePal(sws->dst_format)))
  1007. break;
  1008. dst2[i] -= (dstSliceY >> ((i == 1 || i == 2) ? c->chrDstVSubSample : 0)) * dstStride2[i];
  1009. }
  1010. offset = dstSliceY;
  1011. slice_h = dstSliceH;
  1012. }
  1013. ret = c->convert_unscaled(c, src2, srcStride2, offset, slice_h,
  1014. dst2, dstStride2);
  1015. if (scale_dst)
  1016. dst2[0] += dstSliceY * dstStride2[0];
  1017. } else {
  1018. ret = ff_swscale(c, src2, srcStride2, srcSliceY_internal, srcSliceH,
  1019. dst2, dstStride2, dstSliceY, dstSliceH);
  1020. }
  1021. if (c->dstXYZ && !(c->srcXYZ && sws->src_w==sws->dst_w && sws->src_h==sws->dst_h)) {
  1022. uint8_t *dst;
  1023. if (scale_dst) {
  1024. dst = dst2[0];
  1025. } else {
  1026. int dstY = c->dstY ? c->dstY : srcSliceY + srcSliceH;
  1027. av_assert0(dstY >= ret);
  1028. av_assert0(ret >= 0);
  1029. av_assert0(sws->dst_h >= dstY);
  1030. dst = dst2[0] + (dstY - ret) * dstStride2[0];
  1031. }
  1032. /* replace on the same data */
  1033. ff_rgb48Toxyz12(c, dst, dstStride2[0], dst, dstStride2[0], sws->dst_w, ret);
  1034. }
  1035. /* reset slice direction at end of frame */
  1036. if ((srcSliceY_internal + srcSliceH == sws->src_h) || scale_dst)
  1037. c->sliceDir = 0;
  1038. return ret;
  1039. }
  1040. void sws_frame_end(SwsContext *sws)
  1041. {
  1042. SwsInternal *c = sws_internal(sws);
  1043. av_frame_unref(c->frame_src);
  1044. av_frame_unref(c->frame_dst);
  1045. c->src_ranges.nb_ranges = 0;
  1046. }
  1047. int sws_frame_start(SwsContext *sws, AVFrame *dst, const AVFrame *src)
  1048. {
  1049. SwsInternal *c = sws_internal(sws);
  1050. int ret, allocated = 0;
  1051. ret = av_frame_ref(c->frame_src, src);
  1052. if (ret < 0)
  1053. return ret;
  1054. if (!dst->buf[0]) {
  1055. dst->width = sws->dst_w;
  1056. dst->height = sws->dst_h;
  1057. dst->format = sws->dst_format;
  1058. ret = av_frame_get_buffer(dst, 0);
  1059. if (ret < 0)
  1060. return ret;
  1061. allocated = 1;
  1062. }
  1063. ret = av_frame_ref(c->frame_dst, dst);
  1064. if (ret < 0) {
  1065. if (allocated)
  1066. av_frame_unref(dst);
  1067. return ret;
  1068. }
  1069. return 0;
  1070. }
  1071. int sws_send_slice(SwsContext *sws, unsigned int slice_start,
  1072. unsigned int slice_height)
  1073. {
  1074. SwsInternal *c = sws_internal(sws);
  1075. int ret;
  1076. ret = ff_range_add(&c->src_ranges, slice_start, slice_height);
  1077. if (ret < 0)
  1078. return ret;
  1079. return 0;
  1080. }
  1081. unsigned int sws_receive_slice_alignment(const SwsContext *sws)
  1082. {
  1083. SwsInternal *c = sws_internal(sws);
  1084. if (c->slice_ctx)
  1085. return sws_internal(c->slice_ctx[0])->dst_slice_align;
  1086. return c->dst_slice_align;
  1087. }
  1088. int sws_receive_slice(SwsContext *sws, unsigned int slice_start,
  1089. unsigned int slice_height)
  1090. {
  1091. SwsInternal *c = sws_internal(sws);
  1092. unsigned int align = sws_receive_slice_alignment(sws);
  1093. uint8_t *dst[4];
  1094. /* wait until complete input has been received */
  1095. if (!(c->src_ranges.nb_ranges == 1 &&
  1096. c->src_ranges.ranges[0].start == 0 &&
  1097. c->src_ranges.ranges[0].len == sws->src_h))
  1098. return AVERROR(EAGAIN);
  1099. if ((slice_start > 0 || slice_height < sws->dst_h) &&
  1100. (slice_start % align || slice_height % align)) {
  1101. av_log(c, AV_LOG_ERROR,
  1102. "Incorrectly aligned output: %u/%u not multiples of %u\n",
  1103. slice_start, slice_height, align);
  1104. return AVERROR(EINVAL);
  1105. }
  1106. if (c->slicethread) {
  1107. int nb_jobs = c->nb_slice_ctx;
  1108. int ret = 0;
  1109. if (c->slice_ctx[0]->dither == SWS_DITHER_ED)
  1110. nb_jobs = 1;
  1111. c->dst_slice_start = slice_start;
  1112. c->dst_slice_height = slice_height;
  1113. avpriv_slicethread_execute(c->slicethread, nb_jobs, 0);
  1114. for (int i = 0; i < c->nb_slice_ctx; i++) {
  1115. if (c->slice_err[i] < 0) {
  1116. ret = c->slice_err[i];
  1117. break;
  1118. }
  1119. }
  1120. memset(c->slice_err, 0, c->nb_slice_ctx * sizeof(*c->slice_err));
  1121. return ret;
  1122. }
  1123. for (int i = 0; i < FF_ARRAY_ELEMS(dst); i++) {
  1124. ptrdiff_t offset = c->frame_dst->linesize[i] * (ptrdiff_t)(slice_start >> c->chrDstVSubSample);
  1125. dst[i] = FF_PTR_ADD(c->frame_dst->data[i], offset);
  1126. }
  1127. return scale_internal(sws, (const uint8_t * const *)c->frame_src->data,
  1128. c->frame_src->linesize, 0, sws->src_h,
  1129. dst, c->frame_dst->linesize, slice_start, slice_height);
  1130. }
  1131. static void get_frame_pointers(const AVFrame *frame, uint8_t *data[4],
  1132. int linesize[4], int field)
  1133. {
  1134. for (int i = 0; i < 4; i++) {
  1135. data[i] = frame->data[i];
  1136. linesize[i] = frame->linesize[i];
  1137. }
  1138. if (!(frame->flags & AV_FRAME_FLAG_INTERLACED)) {
  1139. av_assert1(!field);
  1140. return;
  1141. }
  1142. if (field == FIELD_BOTTOM) {
  1143. /* Odd rows, offset by one line */
  1144. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  1145. for (int i = 0; i < 4; i++) {
  1146. data[i] += linesize[i];
  1147. if (desc->flags & AV_PIX_FMT_FLAG_PAL)
  1148. break;
  1149. }
  1150. }
  1151. /* Take only every second line */
  1152. for (int i = 0; i < 4; i++)
  1153. linesize[i] <<= 1;
  1154. }
  1155. /* Subset of av_frame_ref() that only references (video) data buffers */
  1156. static int frame_ref(AVFrame *dst, const AVFrame *src)
  1157. {
  1158. /* ref the buffers */
  1159. for (int i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
  1160. if (!src->buf[i])
  1161. continue;
  1162. dst->buf[i] = av_buffer_ref(src->buf[i]);
  1163. if (!dst->buf[i])
  1164. return AVERROR(ENOMEM);
  1165. }
  1166. memcpy(dst->data, src->data, sizeof(src->data));
  1167. memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
  1168. return 0;
  1169. }
  1170. int sws_scale_frame(SwsContext *sws, AVFrame *dst, const AVFrame *src)
  1171. {
  1172. int ret;
  1173. SwsInternal *c = sws_internal(sws);
  1174. if (!src || !dst)
  1175. return AVERROR(EINVAL);
  1176. if (c->frame_src) {
  1177. /* Context has been initialized with explicit values, fall back to
  1178. * legacy API */
  1179. ret = sws_frame_start(sws, dst, src);
  1180. if (ret < 0)
  1181. return ret;
  1182. ret = sws_send_slice(sws, 0, src->height);
  1183. if (ret >= 0)
  1184. ret = sws_receive_slice(sws, 0, dst->height);
  1185. sws_frame_end(sws);
  1186. return ret;
  1187. }
  1188. ret = sws_frame_setup(sws, dst, src);
  1189. if (ret < 0)
  1190. return ret;
  1191. if (!src->data[0])
  1192. return 0;
  1193. if (c->graph[FIELD_TOP]->noop &&
  1194. (!c->graph[FIELD_BOTTOM] || c->graph[FIELD_BOTTOM]->noop) &&
  1195. src->buf[0] && !dst->buf[0] && !dst->data[0])
  1196. {
  1197. /* Lightweight refcopy */
  1198. ret = frame_ref(dst, src);
  1199. if (ret < 0)
  1200. return ret;
  1201. } else {
  1202. if (!dst->data[0]) {
  1203. ret = av_frame_get_buffer(dst, 0);
  1204. if (ret < 0)
  1205. return ret;
  1206. }
  1207. for (int field = 0; field < 2; field++) {
  1208. SwsGraph *graph = c->graph[field];
  1209. uint8_t *dst_data[4], *src_data[4];
  1210. int dst_linesize[4], src_linesize[4];
  1211. get_frame_pointers(dst, dst_data, dst_linesize, field);
  1212. get_frame_pointers(src, src_data, src_linesize, field);
  1213. ff_sws_graph_run(graph, dst_data, dst_linesize,
  1214. (const uint8_t **) src_data, src_linesize);
  1215. if (!graph->dst.interlaced)
  1216. break;
  1217. }
  1218. }
  1219. return 0;
  1220. }
  1221. static int validate_params(SwsContext *ctx)
  1222. {
  1223. #define VALIDATE(field, min, max) \
  1224. if (ctx->field < min || ctx->field > max) { \
  1225. av_log(ctx, AV_LOG_ERROR, "'%s' (%d) out of range [%d, %d]\n", \
  1226. #field, (int) ctx->field, min, max); \
  1227. return AVERROR(EINVAL); \
  1228. }
  1229. VALIDATE(threads, 0, SWS_MAX_THREADS);
  1230. VALIDATE(dither, 0, SWS_DITHER_NB - 1)
  1231. VALIDATE(alpha_blend, 0, SWS_ALPHA_BLEND_NB - 1)
  1232. return 0;
  1233. }
  1234. int sws_frame_setup(SwsContext *ctx, const AVFrame *dst, const AVFrame *src)
  1235. {
  1236. SwsInternal *s = sws_internal(ctx);
  1237. const char *err_msg;
  1238. int ret;
  1239. if (!src || !dst)
  1240. return AVERROR(EINVAL);
  1241. if ((ret = validate_params(ctx)) < 0)
  1242. return ret;
  1243. for (int field = 0; field < 2; field++) {
  1244. SwsFormat src_fmt = ff_fmt_from_frame(src, field);
  1245. SwsFormat dst_fmt = ff_fmt_from_frame(dst, field);
  1246. int src_ok, dst_ok;
  1247. if ((src->flags ^ dst->flags) & AV_FRAME_FLAG_INTERLACED) {
  1248. err_msg = "Cannot convert interlaced to progressive frames or vice versa.\n";
  1249. ret = AVERROR(EINVAL);
  1250. goto fail;
  1251. }
  1252. src_ok = ff_test_fmt(&src_fmt, 0);
  1253. dst_ok = ff_test_fmt(&dst_fmt, 1);
  1254. if ((!src_ok || !dst_ok) && !ff_props_equal(&src_fmt, &dst_fmt)) {
  1255. err_msg = src_ok ? "Unsupported output" : "Unsupported input";
  1256. ret = AVERROR(ENOTSUP);
  1257. goto fail;
  1258. }
  1259. ret = ff_sws_graph_reinit(ctx, &dst_fmt, &src_fmt, field, &s->graph[field]);
  1260. if (ret < 0) {
  1261. err_msg = "Failed initializing scaling graph";
  1262. goto fail;
  1263. }
  1264. if (s->graph[field]->incomplete && ctx->flags & SWS_STRICT) {
  1265. err_msg = "Incomplete scaling graph";
  1266. ret = AVERROR(EINVAL);
  1267. goto fail;
  1268. }
  1269. if (!src_fmt.interlaced) {
  1270. ff_sws_graph_free(&s->graph[FIELD_BOTTOM]);
  1271. break;
  1272. }
  1273. continue;
  1274. fail:
  1275. av_log(ctx, AV_LOG_ERROR, "%s (%s): fmt:%s csp:%s prim:%s trc:%s ->"
  1276. " fmt:%s csp:%s prim:%s trc:%s\n",
  1277. err_msg, av_err2str(ret),
  1278. av_get_pix_fmt_name(src_fmt.format), av_color_space_name(src_fmt.csp),
  1279. av_color_primaries_name(src_fmt.color.prim), av_color_transfer_name(src_fmt.color.trc),
  1280. av_get_pix_fmt_name(dst_fmt.format), av_color_space_name(dst_fmt.csp),
  1281. av_color_primaries_name(dst_fmt.color.prim), av_color_transfer_name(dst_fmt.color.trc));
  1282. for (int i = 0; i < FF_ARRAY_ELEMS(s->graph); i++)
  1283. ff_sws_graph_free(&s->graph[i]);
  1284. return ret;
  1285. }
  1286. return 0;
  1287. }
  1288. /**
  1289. * swscale wrapper, so we don't need to export the SwsContext.
  1290. * Assumes planar YUV to be in YUV order instead of YVU.
  1291. */
  1292. int attribute_align_arg sws_scale(SwsContext *sws,
  1293. const uint8_t * const srcSlice[],
  1294. const int srcStride[], int srcSliceY,
  1295. int srcSliceH, uint8_t *const dst[],
  1296. const int dstStride[])
  1297. {
  1298. SwsInternal *c = sws_internal(sws);
  1299. if (c->nb_slice_ctx) {
  1300. sws = c->slice_ctx[0];
  1301. c = sws_internal(sws);
  1302. }
  1303. return scale_internal(sws, srcSlice, srcStride, srcSliceY, srcSliceH,
  1304. dst, dstStride, 0, sws->dst_h);
  1305. }
  1306. void ff_sws_slice_worker(void *priv, int jobnr, int threadnr,
  1307. int nb_jobs, int nb_threads)
  1308. {
  1309. SwsInternal *parent = priv;
  1310. SwsContext *sws = parent->slice_ctx[threadnr];
  1311. SwsInternal *c = sws_internal(sws);
  1312. const int slice_height = FFALIGN(FFMAX((parent->dst_slice_height + nb_jobs - 1) / nb_jobs, 1),
  1313. c->dst_slice_align);
  1314. const int slice_start = jobnr * slice_height;
  1315. const int slice_end = FFMIN((jobnr + 1) * slice_height, parent->dst_slice_height);
  1316. int err = 0;
  1317. if (slice_end > slice_start) {
  1318. uint8_t *dst[4] = { NULL };
  1319. for (int i = 0; i < FF_ARRAY_ELEMS(dst) && parent->frame_dst->data[i]; i++) {
  1320. const int vshift = (i == 1 || i == 2) ? c->chrDstVSubSample : 0;
  1321. const ptrdiff_t offset = parent->frame_dst->linesize[i] *
  1322. (ptrdiff_t)((slice_start + parent->dst_slice_start) >> vshift);
  1323. dst[i] = parent->frame_dst->data[i] + offset;
  1324. }
  1325. err = scale_internal(sws, (const uint8_t * const *)parent->frame_src->data,
  1326. parent->frame_src->linesize, 0, sws->src_h,
  1327. dst, parent->frame_dst->linesize,
  1328. parent->dst_slice_start + slice_start, slice_end - slice_start);
  1329. }
  1330. parent->slice_err[threadnr] = err;
  1331. }