swscale.c 44 KB

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