swscale.c 46 KB

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