swscale.c 44 KB

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