swscale.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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. #endif
  521. }
  522. static void reset_ptr(const uint8_t *src[], enum AVPixelFormat format)
  523. {
  524. if (!isALPHA(format))
  525. src[3] = NULL;
  526. if (!isPlanar(format)) {
  527. src[3] = src[2] = NULL;
  528. if (!usePal(format))
  529. src[1] = NULL;
  530. }
  531. }
  532. static int check_image_pointers(const uint8_t * const data[4], enum AVPixelFormat pix_fmt,
  533. const int linesizes[4])
  534. {
  535. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  536. int i;
  537. av_assert2(desc);
  538. for (i = 0; i < 4; i++) {
  539. int plane = desc->comp[i].plane;
  540. if (!data[plane] || !linesizes[plane])
  541. return 0;
  542. }
  543. return 1;
  544. }
  545. static void xyz12Torgb48(struct SwsContext *c, uint16_t *dst,
  546. const uint16_t *src, int stride, int h)
  547. {
  548. int xp,yp;
  549. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
  550. for (yp=0; yp<h; yp++) {
  551. for (xp=0; xp+2<stride; xp+=3) {
  552. int x, y, z, r, g, b;
  553. if (desc->flags & AV_PIX_FMT_FLAG_BE) {
  554. x = AV_RB16(src + xp + 0);
  555. y = AV_RB16(src + xp + 1);
  556. z = AV_RB16(src + xp + 2);
  557. } else {
  558. x = AV_RL16(src + xp + 0);
  559. y = AV_RL16(src + xp + 1);
  560. z = AV_RL16(src + xp + 2);
  561. }
  562. x = c->xyzgamma[x>>4];
  563. y = c->xyzgamma[y>>4];
  564. z = c->xyzgamma[z>>4];
  565. // convert from XYZlinear to sRGBlinear
  566. r = c->xyz2rgb_matrix[0][0] * x +
  567. c->xyz2rgb_matrix[0][1] * y +
  568. c->xyz2rgb_matrix[0][2] * z >> 12;
  569. g = c->xyz2rgb_matrix[1][0] * x +
  570. c->xyz2rgb_matrix[1][1] * y +
  571. c->xyz2rgb_matrix[1][2] * z >> 12;
  572. b = c->xyz2rgb_matrix[2][0] * x +
  573. c->xyz2rgb_matrix[2][1] * y +
  574. c->xyz2rgb_matrix[2][2] * z >> 12;
  575. // limit values to 12-bit depth
  576. r = av_clip_uintp2(r, 12);
  577. g = av_clip_uintp2(g, 12);
  578. b = av_clip_uintp2(b, 12);
  579. // convert from sRGBlinear to RGB and scale from 12bit to 16bit
  580. if (desc->flags & AV_PIX_FMT_FLAG_BE) {
  581. AV_WB16(dst + xp + 0, c->rgbgamma[r] << 4);
  582. AV_WB16(dst + xp + 1, c->rgbgamma[g] << 4);
  583. AV_WB16(dst + xp + 2, c->rgbgamma[b] << 4);
  584. } else {
  585. AV_WL16(dst + xp + 0, c->rgbgamma[r] << 4);
  586. AV_WL16(dst + xp + 1, c->rgbgamma[g] << 4);
  587. AV_WL16(dst + xp + 2, c->rgbgamma[b] << 4);
  588. }
  589. }
  590. src += stride;
  591. dst += stride;
  592. }
  593. }
  594. static void rgb48Toxyz12(struct SwsContext *c, uint16_t *dst,
  595. const uint16_t *src, int stride, int h)
  596. {
  597. int xp,yp;
  598. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
  599. for (yp=0; yp<h; yp++) {
  600. for (xp=0; xp+2<stride; xp+=3) {
  601. int x, y, z, r, g, b;
  602. if (desc->flags & AV_PIX_FMT_FLAG_BE) {
  603. r = AV_RB16(src + xp + 0);
  604. g = AV_RB16(src + xp + 1);
  605. b = AV_RB16(src + xp + 2);
  606. } else {
  607. r = AV_RL16(src + xp + 0);
  608. g = AV_RL16(src + xp + 1);
  609. b = AV_RL16(src + xp + 2);
  610. }
  611. r = c->rgbgammainv[r>>4];
  612. g = c->rgbgammainv[g>>4];
  613. b = c->rgbgammainv[b>>4];
  614. // convert from sRGBlinear to XYZlinear
  615. x = c->rgb2xyz_matrix[0][0] * r +
  616. c->rgb2xyz_matrix[0][1] * g +
  617. c->rgb2xyz_matrix[0][2] * b >> 12;
  618. y = c->rgb2xyz_matrix[1][0] * r +
  619. c->rgb2xyz_matrix[1][1] * g +
  620. c->rgb2xyz_matrix[1][2] * b >> 12;
  621. z = c->rgb2xyz_matrix[2][0] * r +
  622. c->rgb2xyz_matrix[2][1] * g +
  623. c->rgb2xyz_matrix[2][2] * b >> 12;
  624. // limit values to 12-bit depth
  625. x = av_clip_uintp2(x, 12);
  626. y = av_clip_uintp2(y, 12);
  627. z = av_clip_uintp2(z, 12);
  628. // convert from XYZlinear to X'Y'Z' and scale from 12bit to 16bit
  629. if (desc->flags & AV_PIX_FMT_FLAG_BE) {
  630. AV_WB16(dst + xp + 0, c->xyzgammainv[x] << 4);
  631. AV_WB16(dst + xp + 1, c->xyzgammainv[y] << 4);
  632. AV_WB16(dst + xp + 2, c->xyzgammainv[z] << 4);
  633. } else {
  634. AV_WL16(dst + xp + 0, c->xyzgammainv[x] << 4);
  635. AV_WL16(dst + xp + 1, c->xyzgammainv[y] << 4);
  636. AV_WL16(dst + xp + 2, c->xyzgammainv[z] << 4);
  637. }
  638. }
  639. src += stride;
  640. dst += stride;
  641. }
  642. }
  643. static void update_palette(SwsContext *c, const uint32_t *pal)
  644. {
  645. for (int i = 0; i < 256; i++) {
  646. int r, g, b, y, u, v, a = 0xff;
  647. if (c->srcFormat == AV_PIX_FMT_PAL8) {
  648. uint32_t p = pal[i];
  649. a = (p >> 24) & 0xFF;
  650. r = (p >> 16) & 0xFF;
  651. g = (p >> 8) & 0xFF;
  652. b = p & 0xFF;
  653. } else if (c->srcFormat == AV_PIX_FMT_RGB8) {
  654. r = ( i >> 5 ) * 36;
  655. g = ((i >> 2) & 7) * 36;
  656. b = ( i & 3) * 85;
  657. } else if (c->srcFormat == AV_PIX_FMT_BGR8) {
  658. b = ( i >> 6 ) * 85;
  659. g = ((i >> 3) & 7) * 36;
  660. r = ( i & 7) * 36;
  661. } else if (c->srcFormat == AV_PIX_FMT_RGB4_BYTE) {
  662. r = ( i >> 3 ) * 255;
  663. g = ((i >> 1) & 3) * 85;
  664. b = ( i & 1) * 255;
  665. } else if (c->srcFormat == AV_PIX_FMT_GRAY8 || c->srcFormat == AV_PIX_FMT_GRAY8A) {
  666. r = g = b = i;
  667. } else {
  668. av_assert1(c->srcFormat == AV_PIX_FMT_BGR4_BYTE);
  669. b = ( i >> 3 ) * 255;
  670. g = ((i >> 1) & 3) * 85;
  671. r = ( i & 1) * 255;
  672. }
  673. #define RGB2YUV_SHIFT 15
  674. #define BY ( (int) (0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  675. #define BV (-(int) (0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  676. #define BU ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  677. #define GY ( (int) (0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  678. #define GV (-(int) (0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  679. #define GU (-(int) (0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  680. #define RY ( (int) (0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  681. #define RV ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  682. #define RU (-(int) (0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  683. y = av_clip_uint8((RY * r + GY * g + BY * b + ( 33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  684. u = av_clip_uint8((RU * r + GU * g + BU * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  685. v = av_clip_uint8((RV * r + GV * g + BV * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  686. c->pal_yuv[i]= y + (u<<8) + (v<<16) + ((unsigned)a<<24);
  687. switch (c->dstFormat) {
  688. case AV_PIX_FMT_BGR32:
  689. #if !HAVE_BIGENDIAN
  690. case AV_PIX_FMT_RGB24:
  691. #endif
  692. c->pal_rgb[i]= r + (g<<8) + (b<<16) + ((unsigned)a<<24);
  693. break;
  694. case AV_PIX_FMT_BGR32_1:
  695. #if HAVE_BIGENDIAN
  696. case AV_PIX_FMT_BGR24:
  697. #endif
  698. c->pal_rgb[i]= a + (r<<8) + (g<<16) + ((unsigned)b<<24);
  699. break;
  700. case AV_PIX_FMT_RGB32_1:
  701. #if HAVE_BIGENDIAN
  702. case AV_PIX_FMT_RGB24:
  703. #endif
  704. c->pal_rgb[i]= a + (b<<8) + (g<<16) + ((unsigned)r<<24);
  705. break;
  706. case AV_PIX_FMT_RGB32:
  707. #if !HAVE_BIGENDIAN
  708. case AV_PIX_FMT_BGR24:
  709. #endif
  710. default:
  711. c->pal_rgb[i]= b + (g<<8) + (r<<16) + ((unsigned)a<<24);
  712. }
  713. }
  714. }
  715. static int scale_internal(SwsContext *c,
  716. const uint8_t * const srcSlice[], const int srcStride[],
  717. int srcSliceY, int srcSliceH,
  718. uint8_t *const dstSlice[], const int dstStride[],
  719. int dstSliceY, int dstSliceH);
  720. static int scale_gamma(SwsContext *c,
  721. const uint8_t * const srcSlice[], const int srcStride[],
  722. int srcSliceY, int srcSliceH,
  723. uint8_t * const dstSlice[], const int dstStride[],
  724. int dstSliceY, int dstSliceH)
  725. {
  726. int ret = scale_internal(c->cascaded_context[0],
  727. srcSlice, srcStride, srcSliceY, srcSliceH,
  728. c->cascaded_tmp, c->cascaded_tmpStride, 0, c->srcH);
  729. if (ret < 0)
  730. return ret;
  731. if (c->cascaded_context[2])
  732. ret = scale_internal(c->cascaded_context[1], (const uint8_t * const *)c->cascaded_tmp,
  733. c->cascaded_tmpStride, srcSliceY, srcSliceH,
  734. c->cascaded1_tmp, c->cascaded1_tmpStride, 0, c->dstH);
  735. else
  736. ret = scale_internal(c->cascaded_context[1], (const uint8_t * const *)c->cascaded_tmp,
  737. c->cascaded_tmpStride, srcSliceY, srcSliceH,
  738. dstSlice, dstStride, dstSliceY, dstSliceH);
  739. if (ret < 0)
  740. return ret;
  741. if (c->cascaded_context[2]) {
  742. ret = scale_internal(c->cascaded_context[2], (const uint8_t * const *)c->cascaded1_tmp,
  743. c->cascaded1_tmpStride, c->cascaded_context[1]->dstY - ret,
  744. c->cascaded_context[1]->dstY,
  745. dstSlice, dstStride, dstSliceY, dstSliceH);
  746. }
  747. return ret;
  748. }
  749. static int scale_cascaded(SwsContext *c,
  750. const uint8_t * const srcSlice[], const int srcStride[],
  751. int srcSliceY, int srcSliceH,
  752. uint8_t * const dstSlice[], const int dstStride[],
  753. int dstSliceY, int dstSliceH)
  754. {
  755. int ret = scale_internal(c->cascaded_context[0],
  756. srcSlice, srcStride, srcSliceY, srcSliceH,
  757. c->cascaded_tmp, c->cascaded_tmpStride,
  758. 0, c->cascaded_context[0]->dstH);
  759. if (ret < 0)
  760. return ret;
  761. ret = scale_internal(c->cascaded_context[1],
  762. (const uint8_t * const * )c->cascaded_tmp, c->cascaded_tmpStride,
  763. 0, c->cascaded_context[0]->dstH,
  764. dstSlice, dstStride, dstSliceY, dstSliceH);
  765. return ret;
  766. }
  767. static int scale_internal(SwsContext *c,
  768. const uint8_t * const srcSlice[], const int srcStride[],
  769. int srcSliceY, int srcSliceH,
  770. uint8_t *const dstSlice[], const int dstStride[],
  771. int dstSliceY, int dstSliceH)
  772. {
  773. const int scale_dst = dstSliceY > 0 || dstSliceH < c->dstH;
  774. const int frame_start = scale_dst || !c->sliceDir;
  775. int i, ret;
  776. const uint8_t *src2[4];
  777. uint8_t *dst2[4];
  778. int macro_height_src = isBayer(c->srcFormat) ? 2 : (1 << c->chrSrcVSubSample);
  779. int macro_height_dst = isBayer(c->dstFormat) ? 2 : (1 << c->chrDstVSubSample);
  780. // copy strides, so they can safely be modified
  781. int srcStride2[4];
  782. int dstStride2[4];
  783. int srcSliceY_internal = srcSliceY;
  784. if (!srcStride || !dstStride || !dstSlice || !srcSlice) {
  785. av_log(c, AV_LOG_ERROR, "One of the input parameters to sws_scale() is NULL, please check the calling code\n");
  786. return AVERROR(EINVAL);
  787. }
  788. if ((srcSliceY & (macro_height_src - 1)) ||
  789. ((srcSliceH & (macro_height_src - 1)) && srcSliceY + srcSliceH != c->srcH) ||
  790. srcSliceY + srcSliceH > c->srcH ||
  791. (isBayer(c->srcFormat) && srcSliceH <= 1)) {
  792. av_log(c, AV_LOG_ERROR, "Slice parameters %d, %d are invalid\n", srcSliceY, srcSliceH);
  793. return AVERROR(EINVAL);
  794. }
  795. if ((dstSliceY & (macro_height_dst - 1)) ||
  796. ((dstSliceH & (macro_height_dst - 1)) && dstSliceY + dstSliceH != c->dstH) ||
  797. dstSliceY + dstSliceH > c->dstH) {
  798. av_log(c, AV_LOG_ERROR, "Slice parameters %d, %d are invalid\n", dstSliceY, dstSliceH);
  799. return AVERROR(EINVAL);
  800. }
  801. if (!check_image_pointers(srcSlice, c->srcFormat, srcStride)) {
  802. av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
  803. return AVERROR(EINVAL);
  804. }
  805. if (!check_image_pointers((const uint8_t* const*)dstSlice, c->dstFormat, dstStride)) {
  806. av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
  807. return AVERROR(EINVAL);
  808. }
  809. // do not mess up sliceDir if we have a "trailing" 0-size slice
  810. if (srcSliceH == 0)
  811. return 0;
  812. if (c->gamma_flag && c->cascaded_context[0])
  813. return scale_gamma(c, srcSlice, srcStride, srcSliceY, srcSliceH,
  814. dstSlice, dstStride, dstSliceY, dstSliceH);
  815. if (c->cascaded_context[0] && srcSliceY == 0 && srcSliceH == c->cascaded_context[0]->srcH)
  816. return scale_cascaded(c, srcSlice, srcStride, srcSliceY, srcSliceH,
  817. dstSlice, dstStride, dstSliceY, dstSliceH);
  818. if (!srcSliceY && (c->flags & SWS_BITEXACT) && c->dither == SWS_DITHER_ED && c->dither_error[0])
  819. for (i = 0; i < 4; i++)
  820. memset(c->dither_error[i], 0, sizeof(c->dither_error[0][0]) * (c->dstW+2));
  821. if (usePal(c->srcFormat))
  822. update_palette(c, (const uint32_t *)srcSlice[1]);
  823. memcpy(src2, srcSlice, sizeof(src2));
  824. memcpy(dst2, dstSlice, sizeof(dst2));
  825. memcpy(srcStride2, srcStride, sizeof(srcStride2));
  826. memcpy(dstStride2, dstStride, sizeof(dstStride2));
  827. if (frame_start && !scale_dst) {
  828. if (srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
  829. av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
  830. return AVERROR(EINVAL);
  831. }
  832. c->sliceDir = (srcSliceY == 0) ? 1 : -1;
  833. } else if (scale_dst)
  834. c->sliceDir = 1;
  835. if (c->src0Alpha && !c->dst0Alpha && isALPHA(c->dstFormat)) {
  836. uint8_t *base;
  837. int x,y;
  838. av_fast_malloc(&c->rgb0_scratch, &c->rgb0_scratch_allocated,
  839. FFABS(srcStride[0]) * srcSliceH + 32);
  840. if (!c->rgb0_scratch)
  841. return AVERROR(ENOMEM);
  842. base = srcStride[0] < 0 ? c->rgb0_scratch - srcStride[0] * (srcSliceH-1) :
  843. c->rgb0_scratch;
  844. for (y=0; y<srcSliceH; y++){
  845. memcpy(base + srcStride[0]*y, src2[0] + srcStride[0]*y, 4*c->srcW);
  846. for (x=c->src0Alpha-1; x<4*c->srcW; x+=4) {
  847. base[ srcStride[0]*y + x] = 0xFF;
  848. }
  849. }
  850. src2[0] = base;
  851. }
  852. if (c->srcXYZ && !(c->dstXYZ && c->srcW==c->dstW && c->srcH==c->dstH)) {
  853. uint8_t *base;
  854. av_fast_malloc(&c->xyz_scratch, &c->xyz_scratch_allocated,
  855. FFABS(srcStride[0]) * srcSliceH + 32);
  856. if (!c->xyz_scratch)
  857. return AVERROR(ENOMEM);
  858. base = srcStride[0] < 0 ? c->xyz_scratch - srcStride[0] * (srcSliceH-1) :
  859. c->xyz_scratch;
  860. xyz12Torgb48(c, (uint16_t*)base, (const uint16_t*)src2[0], srcStride[0]/2, srcSliceH);
  861. src2[0] = base;
  862. }
  863. if (c->sliceDir != 1) {
  864. // slices go from bottom to top => we flip the image internally
  865. for (i=0; i<4; i++) {
  866. srcStride2[i] *= -1;
  867. dstStride2[i] *= -1;
  868. }
  869. src2[0] += (srcSliceH - 1) * srcStride[0];
  870. if (!usePal(c->srcFormat))
  871. src2[1] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[1];
  872. src2[2] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[2];
  873. src2[3] += (srcSliceH - 1) * srcStride[3];
  874. dst2[0] += ( c->dstH - 1) * dstStride[0];
  875. dst2[1] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[1];
  876. dst2[2] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[2];
  877. dst2[3] += ( c->dstH - 1) * dstStride[3];
  878. srcSliceY_internal = c->srcH-srcSliceY-srcSliceH;
  879. }
  880. reset_ptr(src2, c->srcFormat);
  881. reset_ptr((void*)dst2, c->dstFormat);
  882. if (c->convert_unscaled) {
  883. int offset = srcSliceY_internal;
  884. int slice_h = srcSliceH;
  885. // for dst slice scaling, offset the pointers to match the unscaled API
  886. if (scale_dst) {
  887. av_assert0(offset == 0);
  888. for (i = 0; i < 4 && src2[i]; i++) {
  889. if (!src2[i] || (i > 0 && usePal(c->srcFormat)))
  890. break;
  891. src2[i] += (dstSliceY >> ((i == 1 || i == 2) ? c->chrSrcVSubSample : 0)) * srcStride2[i];
  892. }
  893. for (i = 0; i < 4 && dst2[i]; i++) {
  894. if (!dst2[i] || (i > 0 && usePal(c->dstFormat)))
  895. break;
  896. dst2[i] -= (dstSliceY >> ((i == 1 || i == 2) ? c->chrDstVSubSample : 0)) * dstStride2[i];
  897. }
  898. offset = dstSliceY;
  899. slice_h = dstSliceH;
  900. }
  901. ret = c->convert_unscaled(c, src2, srcStride2, offset, slice_h,
  902. dst2, dstStride2);
  903. if (scale_dst)
  904. dst2[0] += dstSliceY * dstStride2[0];
  905. } else {
  906. ret = swscale(c, src2, srcStride2, srcSliceY_internal, srcSliceH,
  907. dst2, dstStride2, dstSliceY, dstSliceH);
  908. }
  909. if (c->dstXYZ && !(c->srcXYZ && c->srcW==c->dstW && c->srcH==c->dstH)) {
  910. uint16_t *dst16;
  911. if (scale_dst) {
  912. dst16 = (uint16_t *)dst2[0];
  913. } else {
  914. int dstY = c->dstY ? c->dstY : srcSliceY + srcSliceH;
  915. av_assert0(dstY >= ret);
  916. av_assert0(ret >= 0);
  917. av_assert0(c->dstH >= dstY);
  918. dst16 = (uint16_t*)(dst2[0] + (dstY - ret) * dstStride2[0]);
  919. }
  920. /* replace on the same data */
  921. rgb48Toxyz12(c, dst16, dst16, dstStride2[0]/2, ret);
  922. }
  923. /* reset slice direction at end of frame */
  924. if ((srcSliceY_internal + srcSliceH == c->srcH) || scale_dst)
  925. c->sliceDir = 0;
  926. return ret;
  927. }
  928. void sws_frame_end(struct SwsContext *c)
  929. {
  930. av_frame_unref(c->frame_src);
  931. av_frame_unref(c->frame_dst);
  932. c->src_ranges.nb_ranges = 0;
  933. }
  934. int sws_frame_start(struct SwsContext *c, AVFrame *dst, const AVFrame *src)
  935. {
  936. int ret, allocated = 0;
  937. ret = av_frame_ref(c->frame_src, src);
  938. if (ret < 0)
  939. return ret;
  940. if (!dst->buf[0]) {
  941. dst->width = c->dstW;
  942. dst->height = c->dstH;
  943. dst->format = c->dstFormat;
  944. ret = av_frame_get_buffer(dst, 0);
  945. if (ret < 0)
  946. return ret;
  947. allocated = 1;
  948. }
  949. ret = av_frame_ref(c->frame_dst, dst);
  950. if (ret < 0) {
  951. if (allocated)
  952. av_frame_unref(dst);
  953. return ret;
  954. }
  955. return 0;
  956. }
  957. int sws_send_slice(struct SwsContext *c, unsigned int slice_start,
  958. unsigned int slice_height)
  959. {
  960. int ret;
  961. ret = ff_range_add(&c->src_ranges, slice_start, slice_height);
  962. if (ret < 0)
  963. return ret;
  964. return 0;
  965. }
  966. unsigned int sws_receive_slice_alignment(const struct SwsContext *c)
  967. {
  968. if (c->slice_ctx)
  969. return c->slice_ctx[0]->dst_slice_align;
  970. return c->dst_slice_align;
  971. }
  972. int sws_receive_slice(struct SwsContext *c, unsigned int slice_start,
  973. unsigned int slice_height)
  974. {
  975. unsigned int align = sws_receive_slice_alignment(c);
  976. uint8_t *dst[4];
  977. /* wait until complete input has been received */
  978. if (!(c->src_ranges.nb_ranges == 1 &&
  979. c->src_ranges.ranges[0].start == 0 &&
  980. c->src_ranges.ranges[0].len == c->srcH))
  981. return AVERROR(EAGAIN);
  982. if ((slice_start > 0 || slice_height < c->dstH) &&
  983. (slice_start % align || slice_height % align)) {
  984. av_log(c, AV_LOG_ERROR,
  985. "Incorrectly aligned output: %u/%u not multiples of %u\n",
  986. slice_start, slice_height, align);
  987. return AVERROR(EINVAL);
  988. }
  989. if (c->slicethread) {
  990. int nb_jobs = c->slice_ctx[0]->dither == SWS_DITHER_ED ? 1 : c->nb_slice_ctx;
  991. int ret = 0;
  992. c->dst_slice_start = slice_start;
  993. c->dst_slice_height = slice_height;
  994. avpriv_slicethread_execute(c->slicethread, nb_jobs, 0);
  995. for (int i = 0; i < c->nb_slice_ctx; i++) {
  996. if (c->slice_err[i] < 0) {
  997. ret = c->slice_err[i];
  998. break;
  999. }
  1000. }
  1001. memset(c->slice_err, 0, c->nb_slice_ctx * sizeof(*c->slice_err));
  1002. return ret;
  1003. }
  1004. for (int i = 0; i < FF_ARRAY_ELEMS(dst); i++) {
  1005. ptrdiff_t offset = c->frame_dst->linesize[i] * (ptrdiff_t)(slice_start >> c->chrDstVSubSample);
  1006. dst[i] = FF_PTR_ADD(c->frame_dst->data[i], offset);
  1007. }
  1008. return scale_internal(c, (const uint8_t * const *)c->frame_src->data,
  1009. c->frame_src->linesize, 0, c->srcH,
  1010. dst, c->frame_dst->linesize, slice_start, slice_height);
  1011. }
  1012. int sws_scale_frame(struct SwsContext *c, AVFrame *dst, const AVFrame *src)
  1013. {
  1014. int ret;
  1015. ret = sws_frame_start(c, dst, src);
  1016. if (ret < 0)
  1017. return ret;
  1018. ret = sws_send_slice(c, 0, src->height);
  1019. if (ret >= 0)
  1020. ret = sws_receive_slice(c, 0, dst->height);
  1021. sws_frame_end(c);
  1022. return ret;
  1023. }
  1024. /**
  1025. * swscale wrapper, so we don't need to export the SwsContext.
  1026. * Assumes planar YUV to be in YUV order instead of YVU.
  1027. */
  1028. int attribute_align_arg sws_scale(struct SwsContext *c,
  1029. const uint8_t * const srcSlice[],
  1030. const int srcStride[], int srcSliceY,
  1031. int srcSliceH, uint8_t *const dst[],
  1032. const int dstStride[])
  1033. {
  1034. if (c->nb_slice_ctx)
  1035. c = c->slice_ctx[0];
  1036. return scale_internal(c, srcSlice, srcStride, srcSliceY, srcSliceH,
  1037. dst, dstStride, 0, c->dstH);
  1038. }
  1039. void ff_sws_slice_worker(void *priv, int jobnr, int threadnr,
  1040. int nb_jobs, int nb_threads)
  1041. {
  1042. SwsContext *parent = priv;
  1043. SwsContext *c = parent->slice_ctx[threadnr];
  1044. const int slice_height = FFALIGN(FFMAX((parent->dst_slice_height + nb_jobs - 1) / nb_jobs, 1),
  1045. c->dst_slice_align);
  1046. const int slice_start = jobnr * slice_height;
  1047. const int slice_end = FFMIN((jobnr + 1) * slice_height, parent->dst_slice_height);
  1048. int err = 0;
  1049. if (slice_end > slice_start) {
  1050. uint8_t *dst[4] = { NULL };
  1051. for (int i = 0; i < FF_ARRAY_ELEMS(dst) && parent->frame_dst->data[i]; i++) {
  1052. const int vshift = (i == 1 || i == 2) ? c->chrDstVSubSample : 0;
  1053. const ptrdiff_t offset = parent->frame_dst->linesize[i] *
  1054. (ptrdiff_t)((slice_start + parent->dst_slice_start) >> vshift);
  1055. dst[i] = parent->frame_dst->data[i] + offset;
  1056. }
  1057. err = scale_internal(c, (const uint8_t * const *)parent->frame_src->data,
  1058. parent->frame_src->linesize, 0, c->srcH,
  1059. dst, parent->frame_dst->linesize,
  1060. parent->dst_slice_start + slice_start, slice_end - slice_start);
  1061. }
  1062. parent->slice_err[threadnr] = err;
  1063. }