swscale_template.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /*
  2. * Copyright (C) 2001-2003 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. static inline void yuv2yuvX_c(SwsContext *c, const int16_t *lumFilter,
  21. const int16_t **lumSrc, int lumFilterSize,
  22. const int16_t *chrFilter, const int16_t **chrUSrc,
  23. const int16_t **chrVSrc,
  24. int chrFilterSize, const int16_t **alpSrc,
  25. uint8_t *dest, uint8_t *uDest, uint8_t *vDest,
  26. uint8_t *aDest, int dstW, int chrDstW, const uint8_t *lumDither, const uint8_t *chrDither)
  27. {
  28. yuv2yuvXinC(lumFilter, lumSrc, lumFilterSize,
  29. chrFilter, chrUSrc, chrVSrc, chrFilterSize,
  30. alpSrc, dest, uDest, vDest, aDest, dstW, chrDstW, lumDither, chrDither);
  31. }
  32. static inline void yuv2nv12X_c(SwsContext *c, const int16_t *lumFilter,
  33. const int16_t **lumSrc, int lumFilterSize,
  34. const int16_t *chrFilter, const int16_t **chrUSrc,
  35. const int16_t **chrVSrc,
  36. int chrFilterSize, uint8_t *dest, uint8_t *uDest,
  37. int dstW, int chrDstW, enum PixelFormat dstFormat, const uint8_t *dither, const uint8_t *chrDither)
  38. {
  39. yuv2nv12XinC(lumFilter, lumSrc, lumFilterSize,
  40. chrFilter, chrUSrc, chrVSrc, chrFilterSize,
  41. dest, uDest, dstW, chrDstW, dstFormat, dither, chrDither);
  42. }
  43. static inline void yuv2yuv1_c(SwsContext *c, const int16_t *lumSrc,
  44. const int16_t *chrUSrc, const int16_t *chrVSrc,
  45. const int16_t *alpSrc,
  46. uint8_t *dest, uint8_t *uDest, uint8_t *vDest,
  47. uint8_t *aDest, int dstW, int chrDstW, const uint8_t *lumDither, const uint8_t *chrDither)
  48. {
  49. int i;
  50. for (i=0; i<dstW; i++) {
  51. int val= (lumSrc[i]+lumDither[i&7])>>7;
  52. dest[i]= av_clip_uint8(val);
  53. }
  54. if (uDest)
  55. for (i=0; i<chrDstW; i++) {
  56. int u=(chrUSrc[i]+chrDither[i&7])>>7;
  57. int v=(chrVSrc[i]+chrDither[(i+3)&7])>>7;
  58. uDest[i]= av_clip_uint8(u);
  59. vDest[i]= av_clip_uint8(v);
  60. }
  61. if (CONFIG_SWSCALE_ALPHA && aDest)
  62. for (i=0; i<dstW; i++) {
  63. int val= (alpSrc[i]+lumDither[i&7])>>7;
  64. aDest[i]= av_clip_uint8(val);
  65. }
  66. }
  67. /**
  68. * vertical scale YV12 to RGB
  69. */
  70. static inline void yuv2packedX_c(SwsContext *c, const int16_t *lumFilter,
  71. const int16_t **lumSrc, int lumFilterSize,
  72. const int16_t *chrFilter, const int16_t **chrUSrc,
  73. const int16_t **chrVSrc,
  74. int chrFilterSize, const int16_t **alpSrc,
  75. uint8_t *dest, int dstW, int dstY)
  76. {
  77. yuv2packedXinC(c, lumFilter, lumSrc, lumFilterSize,
  78. chrFilter, chrUSrc, chrVSrc, chrFilterSize,
  79. alpSrc, dest, dstW, dstY);
  80. }
  81. /**
  82. * vertical bilinear scale YV12 to RGB
  83. */
  84. static inline void yuv2packed2_c(SwsContext *c, const uint16_t *buf0,
  85. const uint16_t *buf1, const uint16_t *ubuf0,
  86. const uint16_t *ubuf1, const uint16_t *vbuf0,
  87. const uint16_t *vbuf1, const uint16_t *abuf0,
  88. const uint16_t *abuf1, uint8_t *dest, int dstW,
  89. int yalpha, int uvalpha, int y)
  90. {
  91. int yalpha1=4095- yalpha;
  92. int uvalpha1=4095-uvalpha;
  93. int i;
  94. YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGB2_C, YSCALE_YUV_2_PACKED2_C(void,0), YSCALE_YUV_2_GRAY16_2_C, YSCALE_YUV_2_MONO2_C)
  95. }
  96. /**
  97. * YV12 to RGB without scaling or interpolating
  98. */
  99. static inline void yuv2packed1_c(SwsContext *c, const uint16_t *buf0,
  100. const uint16_t *ubuf0, const uint16_t *ubuf1,
  101. const uint16_t *vbuf0, const uint16_t *vbuf1,
  102. const uint16_t *abuf0, uint8_t *dest, int dstW,
  103. int uvalpha, enum PixelFormat dstFormat,
  104. int flags, int y)
  105. {
  106. const int yalpha1=0;
  107. int i;
  108. const uint16_t *buf1= buf0; //FIXME needed for RGB1/BGR1
  109. const int yalpha= 4096; //FIXME ...
  110. if (uvalpha < 2048) {
  111. YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGB1_C, YSCALE_YUV_2_PACKED1_C(void,0), YSCALE_YUV_2_GRAY16_1_C, YSCALE_YUV_2_MONO2_C)
  112. } else {
  113. YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGB1B_C, YSCALE_YUV_2_PACKED1B_C(void,0), YSCALE_YUV_2_GRAY16_1_C, YSCALE_YUV_2_MONO2_C)
  114. }
  115. }
  116. //FIXME yuy2* can read up to 7 samples too much
  117. static inline void yuy2ToY_c(uint8_t *dst, const uint8_t *src, int width,
  118. uint32_t *unused)
  119. {
  120. int i;
  121. for (i=0; i<width; i++)
  122. dst[i]= src[2*i];
  123. }
  124. static inline void yuy2ToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  125. const uint8_t *src2, int width, uint32_t *unused)
  126. {
  127. int i;
  128. for (i=0; i<width; i++) {
  129. dstU[i]= src1[4*i + 1];
  130. dstV[i]= src1[4*i + 3];
  131. }
  132. assert(src1 == src2);
  133. }
  134. static inline void LEToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  135. const uint8_t *src2, int width, uint32_t *unused)
  136. {
  137. int i;
  138. // FIXME I don't think this code is right for YUV444/422, since then h is not subsampled so
  139. // we need to skip each second pixel. Same for BEToUV.
  140. for (i=0; i<width; i++) {
  141. dstU[i]= src1[2*i + 1];
  142. dstV[i]= src2[2*i + 1];
  143. }
  144. }
  145. /* This is almost identical to the previous, end exists only because
  146. * yuy2ToY/UV)(dst, src+1, ...) would have 100% unaligned accesses. */
  147. static inline void uyvyToY_c(uint8_t *dst, const uint8_t *src, int width,
  148. uint32_t *unused)
  149. {
  150. int i;
  151. for (i=0; i<width; i++)
  152. dst[i]= src[2*i+1];
  153. }
  154. static inline void uyvyToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  155. const uint8_t *src2, int width, uint32_t *unused)
  156. {
  157. int i;
  158. for (i=0; i<width; i++) {
  159. dstU[i]= src1[4*i + 0];
  160. dstV[i]= src1[4*i + 2];
  161. }
  162. assert(src1 == src2);
  163. }
  164. static inline void BEToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  165. const uint8_t *src2, int width, uint32_t *unused)
  166. {
  167. int i;
  168. for (i=0; i<width; i++) {
  169. dstU[i]= src1[2*i];
  170. dstV[i]= src2[2*i];
  171. }
  172. }
  173. static inline void nvXXtoUV_c(uint8_t *dst1, uint8_t *dst2,
  174. const uint8_t *src, int width)
  175. {
  176. int i;
  177. for (i = 0; i < width; i++) {
  178. dst1[i] = src[2*i+0];
  179. dst2[i] = src[2*i+1];
  180. }
  181. }
  182. // FIXME Maybe dither instead.
  183. #ifndef YUV_NBPS
  184. #define YUV_NBPS(depth, endianness, rfunc) \
  185. static inline void endianness ## depth ## ToUV_c(uint8_t *dstU, uint8_t *dstV, \
  186. const uint16_t *srcU, const uint16_t *srcV, \
  187. int width, uint32_t *unused) \
  188. { \
  189. int i; \
  190. for (i = 0; i < width; i++) { \
  191. dstU[i] = rfunc(&srcU[i])>>(depth-8); \
  192. dstV[i] = rfunc(&srcV[i])>>(depth-8); \
  193. } \
  194. } \
  195. \
  196. static inline void endianness ## depth ## ToY_c(uint8_t *dstY, const uint16_t *srcY, int width, uint32_t *unused) \
  197. { \
  198. int i; \
  199. for (i = 0; i < width; i++) \
  200. dstY[i] = rfunc(&srcY[i])>>(depth-8); \
  201. } \
  202. YUV_NBPS( 9, LE, AV_RL16)
  203. YUV_NBPS( 9, BE, AV_RB16)
  204. YUV_NBPS(10, LE, AV_RL16)
  205. YUV_NBPS(10, BE, AV_RB16)
  206. #endif // YUV_NBPS
  207. static inline void nv12ToUV_c(uint8_t *dstU, uint8_t *dstV,
  208. const uint8_t *src1, const uint8_t *src2,
  209. int width, uint32_t *unused)
  210. {
  211. nvXXtoUV_c(dstU, dstV, src1, width);
  212. }
  213. static inline void nv21ToUV_c(uint8_t *dstU, uint8_t *dstV,
  214. const uint8_t *src1, const uint8_t *src2,
  215. int width, uint32_t *unused)
  216. {
  217. nvXXtoUV_c(dstV, dstU, src1, width);
  218. }
  219. // bilinear / bicubic scaling
  220. static inline void hScale_c(int16_t *dst, int dstW, const uint8_t *src,
  221. int srcW, int xInc,
  222. const int16_t *filter, const int16_t *filterPos,
  223. int filterSize)
  224. {
  225. int i;
  226. for (i=0; i<dstW; i++) {
  227. int j;
  228. int srcPos= filterPos[i];
  229. int val=0;
  230. for (j=0; j<filterSize; j++) {
  231. val += ((int)src[srcPos + j])*filter[filterSize*i + j];
  232. }
  233. //filter += hFilterSize;
  234. dst[i] = FFMIN(val>>7, (1<<15)-1); // the cubic equation does overflow ...
  235. //dst[i] = val>>7;
  236. }
  237. }
  238. #define DEBUG_SWSCALE_BUFFERS 0
  239. #define DEBUG_BUFFERS(...) if (DEBUG_SWSCALE_BUFFERS) av_log(c, AV_LOG_DEBUG, __VA_ARGS__)
  240. #if HAVE_MMX
  241. static void updateMMXDitherTables(SwsContext *c, int dstY, int lumBufIndex, int chrBufIndex,
  242. int lastInLumBuf, int lastInChrBuf);
  243. #endif
  244. static int swScale_c(SwsContext *c, const uint8_t* src[], int srcStride[],
  245. int srcSliceY, int srcSliceH, uint8_t* dst[], int dstStride[])
  246. {
  247. /* load a few things into local vars to make the code more readable? and faster */
  248. const int srcW= c->srcW;
  249. const int dstW= c->dstW;
  250. const int dstH= c->dstH;
  251. const int chrDstW= c->chrDstW;
  252. const int chrSrcW= c->chrSrcW;
  253. const int lumXInc= c->lumXInc;
  254. const int chrXInc= c->chrXInc;
  255. const enum PixelFormat dstFormat= c->dstFormat;
  256. const int flags= c->flags;
  257. int16_t *vLumFilterPos= c->vLumFilterPos;
  258. int16_t *vChrFilterPos= c->vChrFilterPos;
  259. int16_t *hLumFilterPos= c->hLumFilterPos;
  260. int16_t *hChrFilterPos= c->hChrFilterPos;
  261. int16_t *vLumFilter= c->vLumFilter;
  262. int16_t *vChrFilter= c->vChrFilter;
  263. int16_t *hLumFilter= c->hLumFilter;
  264. int16_t *hChrFilter= c->hChrFilter;
  265. int32_t *lumMmxFilter= c->lumMmxFilter;
  266. int32_t *chrMmxFilter= c->chrMmxFilter;
  267. int32_t av_unused *alpMmxFilter= c->alpMmxFilter;
  268. const int vLumFilterSize= c->vLumFilterSize;
  269. const int vChrFilterSize= c->vChrFilterSize;
  270. const int hLumFilterSize= c->hLumFilterSize;
  271. const int hChrFilterSize= c->hChrFilterSize;
  272. int16_t **lumPixBuf= c->lumPixBuf;
  273. int16_t **chrUPixBuf= c->chrUPixBuf;
  274. int16_t **chrVPixBuf= c->chrVPixBuf;
  275. int16_t **alpPixBuf= c->alpPixBuf;
  276. const int vLumBufSize= c->vLumBufSize;
  277. const int vChrBufSize= c->vChrBufSize;
  278. uint8_t *formatConvBuffer= c->formatConvBuffer;
  279. const int chrSrcSliceY= srcSliceY >> c->chrSrcVSubSample;
  280. const int chrSrcSliceH= -((-srcSliceH) >> c->chrSrcVSubSample);
  281. int lastDstY;
  282. uint32_t *pal=c->pal_yuv;
  283. /* vars which will change and which we need to store back in the context */
  284. int dstY= c->dstY;
  285. int lumBufIndex= c->lumBufIndex;
  286. int chrBufIndex= c->chrBufIndex;
  287. int lastInLumBuf= c->lastInLumBuf;
  288. int lastInChrBuf= c->lastInChrBuf;
  289. if (isPacked(c->srcFormat)) {
  290. src[0]=
  291. src[1]=
  292. src[2]=
  293. src[3]= src[0];
  294. srcStride[0]=
  295. srcStride[1]=
  296. srcStride[2]=
  297. srcStride[3]= srcStride[0];
  298. }
  299. srcStride[1]<<= c->vChrDrop;
  300. srcStride[2]<<= c->vChrDrop;
  301. DEBUG_BUFFERS("swScale() %p[%d] %p[%d] %p[%d] %p[%d] -> %p[%d] %p[%d] %p[%d] %p[%d]\n",
  302. src[0], srcStride[0], src[1], srcStride[1], src[2], srcStride[2], src[3], srcStride[3],
  303. dst[0], dstStride[0], dst[1], dstStride[1], dst[2], dstStride[2], dst[3], dstStride[3]);
  304. DEBUG_BUFFERS("srcSliceY: %d srcSliceH: %d dstY: %d dstH: %d\n",
  305. srcSliceY, srcSliceH, dstY, dstH);
  306. DEBUG_BUFFERS("vLumFilterSize: %d vLumBufSize: %d vChrFilterSize: %d vChrBufSize: %d\n",
  307. vLumFilterSize, vLumBufSize, vChrFilterSize, vChrBufSize);
  308. if (dstStride[0]%8 !=0 || dstStride[1]%8 !=0 || dstStride[2]%8 !=0 || dstStride[3]%8 != 0) {
  309. static int warnedAlready=0; //FIXME move this into the context perhaps
  310. if (flags & SWS_PRINT_INFO && !warnedAlready) {
  311. av_log(c, AV_LOG_WARNING, "Warning: dstStride is not aligned!\n"
  312. " ->cannot do aligned memory accesses anymore\n");
  313. warnedAlready=1;
  314. }
  315. }
  316. /* Note the user might start scaling the picture in the middle so this
  317. will not get executed. This is not really intended but works
  318. currently, so people might do it. */
  319. if (srcSliceY ==0) {
  320. lumBufIndex=-1;
  321. chrBufIndex=-1;
  322. dstY=0;
  323. lastInLumBuf= -1;
  324. lastInChrBuf= -1;
  325. }
  326. lastDstY= dstY;
  327. for (;dstY < dstH; dstY++) {
  328. unsigned char *dest =dst[0]+dstStride[0]*dstY;
  329. const int chrDstY= dstY>>c->chrDstVSubSample;
  330. unsigned char *uDest=dst[1]+dstStride[1]*chrDstY;
  331. unsigned char *vDest=dst[2]+dstStride[2]*chrDstY;
  332. unsigned char *aDest=(CONFIG_SWSCALE_ALPHA && alpPixBuf) ? dst[3]+dstStride[3]*dstY : NULL;
  333. const uint8_t *lumDither= isNBPS(c->srcFormat) || is16BPS(c->srcFormat) ? dithers[7][dstY &7] : flat64;
  334. const uint8_t *chrDither= isNBPS(c->srcFormat) || is16BPS(c->srcFormat) ? dithers[7][chrDstY&7] : flat64;
  335. const int firstLumSrcY= vLumFilterPos[dstY]; //First line needed as input
  336. const int firstLumSrcY2= vLumFilterPos[FFMIN(dstY | ((1<<c->chrDstVSubSample) - 1), dstH-1)];
  337. const int firstChrSrcY= vChrFilterPos[chrDstY]; //First line needed as input
  338. int lastLumSrcY= firstLumSrcY + vLumFilterSize -1; // Last line needed as input
  339. int lastLumSrcY2=firstLumSrcY2+ vLumFilterSize -1; // Last line needed as input
  340. int lastChrSrcY= firstChrSrcY + vChrFilterSize -1; // Last line needed as input
  341. int enough_lines;
  342. //handle holes (FAST_BILINEAR & weird filters)
  343. if (firstLumSrcY > lastInLumBuf) lastInLumBuf= firstLumSrcY-1;
  344. if (firstChrSrcY > lastInChrBuf) lastInChrBuf= firstChrSrcY-1;
  345. assert(firstLumSrcY >= lastInLumBuf - vLumBufSize + 1);
  346. assert(firstChrSrcY >= lastInChrBuf - vChrBufSize + 1);
  347. DEBUG_BUFFERS("dstY: %d\n", dstY);
  348. DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n",
  349. firstLumSrcY, lastLumSrcY, lastInLumBuf);
  350. DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n",
  351. firstChrSrcY, lastChrSrcY, lastInChrBuf);
  352. // Do we have enough lines in this slice to output the dstY line
  353. enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH && lastChrSrcY < -((-srcSliceY - srcSliceH)>>c->chrSrcVSubSample);
  354. if (!enough_lines) {
  355. lastLumSrcY = srcSliceY + srcSliceH - 1;
  356. lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1;
  357. DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n",
  358. lastLumSrcY, lastChrSrcY);
  359. }
  360. //Do horizontal scaling
  361. while(lastInLumBuf < lastLumSrcY) {
  362. const uint8_t *src1= src[0]+(lastInLumBuf + 1 - srcSliceY)*srcStride[0];
  363. const uint8_t *src2= src[3]+(lastInLumBuf + 1 - srcSliceY)*srcStride[3];
  364. lumBufIndex++;
  365. assert(lumBufIndex < 2*vLumBufSize);
  366. assert(lastInLumBuf + 1 - srcSliceY < srcSliceH);
  367. assert(lastInLumBuf + 1 - srcSliceY >= 0);
  368. hyscale_c(c, lumPixBuf[ lumBufIndex ], dstW, src1, srcW, lumXInc,
  369. hLumFilter, hLumFilterPos, hLumFilterSize,
  370. formatConvBuffer,
  371. pal, 0);
  372. if (CONFIG_SWSCALE_ALPHA && alpPixBuf)
  373. hyscale_c(c, alpPixBuf[ lumBufIndex ], dstW, src2, srcW,
  374. lumXInc, hLumFilter, hLumFilterPos, hLumFilterSize,
  375. formatConvBuffer,
  376. pal, 1);
  377. lastInLumBuf++;
  378. DEBUG_BUFFERS("\t\tlumBufIndex %d: lastInLumBuf: %d\n",
  379. lumBufIndex, lastInLumBuf);
  380. }
  381. while(lastInChrBuf < lastChrSrcY) {
  382. const uint8_t *src1= src[1]+(lastInChrBuf + 1 - chrSrcSliceY)*srcStride[1];
  383. const uint8_t *src2= src[2]+(lastInChrBuf + 1 - chrSrcSliceY)*srcStride[2];
  384. chrBufIndex++;
  385. assert(chrBufIndex < 2*vChrBufSize);
  386. assert(lastInChrBuf + 1 - chrSrcSliceY < (chrSrcSliceH));
  387. assert(lastInChrBuf + 1 - chrSrcSliceY >= 0);
  388. //FIXME replace parameters through context struct (some at least)
  389. if (c->needs_hcscale)
  390. hcscale_c(c, chrUPixBuf[chrBufIndex], chrVPixBuf[chrBufIndex],
  391. chrDstW, src1, src2, chrSrcW, chrXInc,
  392. hChrFilter, hChrFilterPos, hChrFilterSize,
  393. formatConvBuffer, pal);
  394. lastInChrBuf++;
  395. DEBUG_BUFFERS("\t\tchrBufIndex %d: lastInChrBuf: %d\n",
  396. chrBufIndex, lastInChrBuf);
  397. }
  398. //wrap buf index around to stay inside the ring buffer
  399. if (lumBufIndex >= vLumBufSize) lumBufIndex-= vLumBufSize;
  400. if (chrBufIndex >= vChrBufSize) chrBufIndex-= vChrBufSize;
  401. if (!enough_lines)
  402. break; //we can't output a dstY line so let's try with the next slice
  403. #if HAVE_MMX
  404. updateMMXDitherTables(c, dstY, lumBufIndex, chrBufIndex, lastInLumBuf, lastInChrBuf);
  405. #endif
  406. if (dstY < dstH-2) {
  407. const int16_t **lumSrcPtr= (const int16_t **) lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
  408. const int16_t **chrUSrcPtr= (const int16_t **) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  409. const int16_t **chrVSrcPtr= (const int16_t **) chrVPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  410. const int16_t **alpSrcPtr= (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? (const int16_t **) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
  411. if (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21) {
  412. const int chrSkipMask= (1<<c->chrDstVSubSample)-1;
  413. if (dstY&chrSkipMask) uDest= NULL; //FIXME split functions in lumi / chromi
  414. c->yuv2nv12X(c,
  415. vLumFilter+dstY*vLumFilterSize , lumSrcPtr, vLumFilterSize,
  416. vChrFilter+chrDstY*vChrFilterSize, chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  417. dest, uDest, dstW, chrDstW, dstFormat, lumDither, chrDither);
  418. } else if (isPlanarYUV(dstFormat) || dstFormat==PIX_FMT_GRAY8) { //YV12 like
  419. const int chrSkipMask= (1<<c->chrDstVSubSample)-1;
  420. if ((dstY&chrSkipMask) || isGray(dstFormat)) uDest=vDest= NULL; //FIXME split functions in lumi / chromi
  421. if (is16BPS(dstFormat) || isNBPS(dstFormat)) {
  422. yuv2yuvX16inC(vLumFilter+dstY*vLumFilterSize , lumSrcPtr, vLumFilterSize,
  423. vChrFilter+chrDstY*vChrFilterSize, chrUSrcPtr,
  424. chrVSrcPtr, vChrFilterSize,
  425. alpSrcPtr, (uint16_t *) dest, (uint16_t *) uDest,
  426. (uint16_t *) vDest, (uint16_t *) aDest, dstW, chrDstW,
  427. dstFormat);
  428. } else if (vLumFilterSize == 1 && vChrFilterSize == 1) { // unscaled YV12
  429. const int16_t *lumBuf = lumSrcPtr[0];
  430. const int16_t *chrUBuf= chrUSrcPtr[0];
  431. const int16_t *chrVBuf= chrVSrcPtr[0];
  432. const int16_t *alpBuf= (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? alpSrcPtr[0] : NULL;
  433. c->yuv2yuv1(c, lumBuf, chrUBuf, chrVBuf, alpBuf, dest,
  434. uDest, vDest, aDest, dstW, chrDstW, lumDither, chrDither);
  435. } else { //General YV12
  436. c->yuv2yuvX(c,
  437. vLumFilter+dstY*vLumFilterSize , lumSrcPtr, vLumFilterSize,
  438. vChrFilter+chrDstY*vChrFilterSize, chrUSrcPtr,
  439. chrVSrcPtr, vChrFilterSize,
  440. alpSrcPtr, dest, uDest, vDest, aDest, dstW, chrDstW, lumDither, chrDither);
  441. }
  442. } else {
  443. assert(lumSrcPtr + vLumFilterSize - 1 < lumPixBuf + vLumBufSize*2);
  444. assert(chrUSrcPtr + vChrFilterSize - 1 < chrUPixBuf + vChrBufSize*2);
  445. if (vLumFilterSize == 1 && vChrFilterSize == 2) { //unscaled RGB
  446. int chrAlpha= vChrFilter[2*dstY+1];
  447. if(flags & SWS_FULL_CHR_H_INT) {
  448. yuv2rgbXinC_full(c, //FIXME write a packed1_full function
  449. vLumFilter+dstY*vLumFilterSize, lumSrcPtr, vLumFilterSize,
  450. vChrFilter+dstY*vChrFilterSize, chrUSrcPtr,
  451. chrVSrcPtr, vChrFilterSize,
  452. alpSrcPtr, dest, dstW, dstY);
  453. } else {
  454. c->yuv2packed1(c, *lumSrcPtr, *chrUSrcPtr, *(chrUSrcPtr+1),
  455. *chrVSrcPtr, *(chrVSrcPtr+1),
  456. alpPixBuf ? *alpSrcPtr : NULL,
  457. dest, dstW, chrAlpha, dstFormat, flags, dstY);
  458. }
  459. } else if (vLumFilterSize == 2 && vChrFilterSize == 2) { //bilinear upscale RGB
  460. int lumAlpha= vLumFilter[2*dstY+1];
  461. int chrAlpha= vChrFilter[2*dstY+1];
  462. lumMmxFilter[2]=
  463. lumMmxFilter[3]= vLumFilter[2*dstY ]*0x10001;
  464. chrMmxFilter[2]=
  465. chrMmxFilter[3]= vChrFilter[2*chrDstY]*0x10001;
  466. if(flags & SWS_FULL_CHR_H_INT) {
  467. yuv2rgbXinC_full(c, //FIXME write a packed2_full function
  468. vLumFilter+dstY*vLumFilterSize, lumSrcPtr, vLumFilterSize,
  469. vChrFilter+dstY*vChrFilterSize, chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  470. alpSrcPtr, dest, dstW, dstY);
  471. } else {
  472. c->yuv2packed2(c, *lumSrcPtr, *(lumSrcPtr+1), *chrUSrcPtr, *(chrUSrcPtr+1),
  473. *chrVSrcPtr, *(chrVSrcPtr+1),
  474. alpPixBuf ? *alpSrcPtr : NULL, alpPixBuf ? *(alpSrcPtr+1) : NULL,
  475. dest, dstW, lumAlpha, chrAlpha, dstY);
  476. }
  477. } else { //general RGB
  478. if(flags & SWS_FULL_CHR_H_INT) {
  479. yuv2rgbXinC_full(c,
  480. vLumFilter+dstY*vLumFilterSize, lumSrcPtr, vLumFilterSize,
  481. vChrFilter+dstY*vChrFilterSize, chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  482. alpSrcPtr, dest, dstW, dstY);
  483. } else {
  484. c->yuv2packedX(c,
  485. vLumFilter+dstY*vLumFilterSize, lumSrcPtr, vLumFilterSize,
  486. vChrFilter+dstY*vChrFilterSize, chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  487. alpSrcPtr, dest, dstW, dstY);
  488. }
  489. }
  490. }
  491. } else { // hmm looks like we can't use MMX here without overwriting this array's tail
  492. const int16_t **lumSrcPtr= (const int16_t **)lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
  493. const int16_t **chrUSrcPtr= (const int16_t **)chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  494. const int16_t **chrVSrcPtr= (const int16_t **)chrVPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  495. const int16_t **alpSrcPtr= (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? (const int16_t **)alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
  496. if (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21) {
  497. const int chrSkipMask= (1<<c->chrDstVSubSample)-1;
  498. if (dstY&chrSkipMask) uDest= NULL; //FIXME split functions in lumi / chromi
  499. yuv2nv12XinC(
  500. vLumFilter+dstY*vLumFilterSize , lumSrcPtr, vLumFilterSize,
  501. vChrFilter+chrDstY*vChrFilterSize, chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  502. dest, uDest, dstW, chrDstW, dstFormat, lumDither, chrDither);
  503. } else if (isPlanarYUV(dstFormat) || dstFormat==PIX_FMT_GRAY8) { //YV12
  504. const int chrSkipMask= (1<<c->chrDstVSubSample)-1;
  505. if ((dstY&chrSkipMask) || isGray(dstFormat)) uDest=vDest= NULL; //FIXME split functions in lumi / chromi
  506. if (is16BPS(dstFormat) || isNBPS(dstFormat)) {
  507. yuv2yuvX16inC(
  508. vLumFilter+dstY*vLumFilterSize , lumSrcPtr, vLumFilterSize,
  509. vChrFilter+chrDstY*vChrFilterSize, chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  510. alpSrcPtr, (uint16_t *) dest, (uint16_t *) uDest, (uint16_t *) vDest, (uint16_t *) aDest, dstW, chrDstW,
  511. dstFormat);
  512. } else {
  513. yuv2yuvXinC(
  514. vLumFilter+dstY*vLumFilterSize , lumSrcPtr, vLumFilterSize,
  515. vChrFilter+chrDstY*vChrFilterSize, chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  516. alpSrcPtr, dest, uDest, vDest, aDest, dstW, chrDstW, lumDither, chrDither);
  517. }
  518. } else {
  519. assert(lumSrcPtr + vLumFilterSize - 1 < lumPixBuf + vLumBufSize*2);
  520. assert(chrUSrcPtr + vChrFilterSize - 1 < chrUPixBuf + vChrBufSize*2);
  521. if(flags & SWS_FULL_CHR_H_INT) {
  522. yuv2rgbXinC_full(c,
  523. vLumFilter+dstY*vLumFilterSize, lumSrcPtr, vLumFilterSize,
  524. vChrFilter+dstY*vChrFilterSize, chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  525. alpSrcPtr, dest, dstW, dstY);
  526. } else {
  527. yuv2packedXinC(c,
  528. vLumFilter+dstY*vLumFilterSize, lumSrcPtr, vLumFilterSize,
  529. vChrFilter+dstY*vChrFilterSize, chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  530. alpSrcPtr, dest, dstW, dstY);
  531. }
  532. }
  533. }
  534. }
  535. if ((dstFormat == PIX_FMT_YUVA420P) && !alpPixBuf)
  536. fillPlane(dst[3], dstStride[3], dstW, dstY-lastDstY, lastDstY, 255);
  537. #if HAVE_MMX2
  538. if (av_get_cpu_flags() & AV_CPU_FLAG_MMX2)
  539. __asm__ volatile("sfence":::"memory");
  540. #endif
  541. emms_c();
  542. /* store changed local vars back in the context */
  543. c->dstY= dstY;
  544. c->lumBufIndex= lumBufIndex;
  545. c->chrBufIndex= chrBufIndex;
  546. c->lastInLumBuf= lastInLumBuf;
  547. c->lastInChrBuf= lastInChrBuf;
  548. return dstY - lastDstY;
  549. }
  550. static void sws_init_swScale_c(SwsContext *c)
  551. {
  552. enum PixelFormat srcFormat = c->srcFormat;
  553. c->yuv2nv12X = yuv2nv12X_c;
  554. c->yuv2yuv1 = yuv2yuv1_c;
  555. c->yuv2yuvX = yuv2yuvX_c;
  556. c->yuv2packed1 = yuv2packed1_c;
  557. c->yuv2packed2 = yuv2packed2_c;
  558. c->yuv2packedX = yuv2packedX_c;
  559. c->hScale = hScale_c;
  560. if (c->flags & SWS_FAST_BILINEAR)
  561. {
  562. c->hyscale_fast = hyscale_fast_c;
  563. c->hcscale_fast = hcscale_fast_c;
  564. }
  565. c->chrToYV12 = NULL;
  566. switch(srcFormat) {
  567. case PIX_FMT_YUYV422 : c->chrToYV12 = yuy2ToUV_c; break;
  568. case PIX_FMT_UYVY422 : c->chrToYV12 = uyvyToUV_c; break;
  569. case PIX_FMT_NV12 : c->chrToYV12 = nv12ToUV_c; break;
  570. case PIX_FMT_NV21 : c->chrToYV12 = nv21ToUV_c; break;
  571. case PIX_FMT_RGB8 :
  572. case PIX_FMT_BGR8 :
  573. case PIX_FMT_PAL8 :
  574. case PIX_FMT_BGR4_BYTE:
  575. case PIX_FMT_RGB4_BYTE: c->chrToYV12 = palToUV; break;
  576. case PIX_FMT_GRAY16BE :
  577. case PIX_FMT_YUV420P9BE:
  578. case PIX_FMT_YUV422P10BE:
  579. case PIX_FMT_YUV420P10BE:
  580. case PIX_FMT_YUV420P16BE:
  581. case PIX_FMT_YUV422P16BE:
  582. case PIX_FMT_YUV444P16BE: c->hScale16= HAVE_BIGENDIAN ? hScale16_c : hScale16X_c; break;
  583. case PIX_FMT_GRAY16LE :
  584. case PIX_FMT_YUV420P9LE:
  585. case PIX_FMT_YUV422P10LE:
  586. case PIX_FMT_YUV420P10LE:
  587. case PIX_FMT_YUV420P16LE:
  588. case PIX_FMT_YUV422P16LE:
  589. case PIX_FMT_YUV444P16LE: c->hScale16= HAVE_BIGENDIAN ? hScale16X_c : hScale16_c; break;
  590. }
  591. if (c->chrSrcHSubSample) {
  592. switch(srcFormat) {
  593. case PIX_FMT_RGB48BE: c->chrToYV12 = rgb48BEToUV_half; break;
  594. case PIX_FMT_RGB48LE: c->chrToYV12 = rgb48LEToUV_half; break;
  595. case PIX_FMT_BGR48BE: c->chrToYV12 = bgr48BEToUV_half; break;
  596. case PIX_FMT_BGR48LE: c->chrToYV12 = bgr48LEToUV_half; break;
  597. case PIX_FMT_RGB32 : c->chrToYV12 = bgr32ToUV_half; break;
  598. case PIX_FMT_RGB32_1: c->chrToYV12 = bgr321ToUV_half; break;
  599. case PIX_FMT_BGR24 : c->chrToYV12 = bgr24ToUV_half_c; break;
  600. case PIX_FMT_BGR565 : c->chrToYV12 = bgr16ToUV_half; break;
  601. case PIX_FMT_BGR555 : c->chrToYV12 = bgr15ToUV_half; break;
  602. case PIX_FMT_BGR32 : c->chrToYV12 = rgb32ToUV_half; break;
  603. case PIX_FMT_BGR32_1: c->chrToYV12 = rgb321ToUV_half; break;
  604. case PIX_FMT_RGB24 : c->chrToYV12 = rgb24ToUV_half_c; break;
  605. case PIX_FMT_RGB565 : c->chrToYV12 = rgb16ToUV_half; break;
  606. case PIX_FMT_RGB555 : c->chrToYV12 = rgb15ToUV_half; break;
  607. }
  608. } else {
  609. switch(srcFormat) {
  610. case PIX_FMT_RGB48BE: c->chrToYV12 = rgb48BEToUV; break;
  611. case PIX_FMT_RGB48LE: c->chrToYV12 = rgb48LEToUV; break;
  612. case PIX_FMT_BGR48BE: c->chrToYV12 = bgr48BEToUV; break;
  613. case PIX_FMT_BGR48LE: c->chrToYV12 = bgr48LEToUV; break;
  614. case PIX_FMT_RGB32 : c->chrToYV12 = bgr32ToUV; break;
  615. case PIX_FMT_RGB32_1: c->chrToYV12 = bgr321ToUV; break;
  616. case PIX_FMT_BGR24 : c->chrToYV12 = bgr24ToUV_c; break;
  617. case PIX_FMT_BGR565 : c->chrToYV12 = bgr16ToUV; break;
  618. case PIX_FMT_BGR555 : c->chrToYV12 = bgr15ToUV; break;
  619. case PIX_FMT_BGR32 : c->chrToYV12 = rgb32ToUV; break;
  620. case PIX_FMT_BGR32_1: c->chrToYV12 = rgb321ToUV; break;
  621. case PIX_FMT_RGB24 : c->chrToYV12 = rgb24ToUV_c; break;
  622. case PIX_FMT_RGB565 : c->chrToYV12 = rgb16ToUV; break;
  623. case PIX_FMT_RGB555 : c->chrToYV12 = rgb15ToUV; break;
  624. }
  625. }
  626. c->lumToYV12 = NULL;
  627. c->alpToYV12 = NULL;
  628. switch (srcFormat) {
  629. case PIX_FMT_YUYV422 :
  630. case PIX_FMT_GRAY8A :
  631. c->lumToYV12 = yuy2ToY_c; break;
  632. case PIX_FMT_UYVY422 :
  633. c->lumToYV12 = uyvyToY_c; break;
  634. case PIX_FMT_BGR24 : c->lumToYV12 = bgr24ToY_c; break;
  635. case PIX_FMT_BGR565 : c->lumToYV12 = bgr16ToY; break;
  636. case PIX_FMT_BGR555 : c->lumToYV12 = bgr15ToY; break;
  637. case PIX_FMT_RGB24 : c->lumToYV12 = rgb24ToY_c; break;
  638. case PIX_FMT_RGB565 : c->lumToYV12 = rgb16ToY; break;
  639. case PIX_FMT_RGB555 : c->lumToYV12 = rgb15ToY; break;
  640. case PIX_FMT_RGB8 :
  641. case PIX_FMT_BGR8 :
  642. case PIX_FMT_PAL8 :
  643. case PIX_FMT_BGR4_BYTE:
  644. case PIX_FMT_RGB4_BYTE: c->lumToYV12 = palToY; break;
  645. case PIX_FMT_MONOBLACK: c->lumToYV12 = monoblack2Y; break;
  646. case PIX_FMT_MONOWHITE: c->lumToYV12 = monowhite2Y; break;
  647. case PIX_FMT_RGB32 : c->lumToYV12 = bgr32ToY; break;
  648. case PIX_FMT_RGB32_1: c->lumToYV12 = bgr321ToY; break;
  649. case PIX_FMT_BGR32 : c->lumToYV12 = rgb32ToY; break;
  650. case PIX_FMT_BGR32_1: c->lumToYV12 = rgb321ToY; break;
  651. case PIX_FMT_RGB48BE: c->lumToYV12 = rgb48BEToY; break;
  652. case PIX_FMT_RGB48LE: c->lumToYV12 = rgb48LEToY; break;
  653. case PIX_FMT_BGR48BE: c->lumToYV12 = bgr48BEToY; break;
  654. case PIX_FMT_BGR48LE: c->lumToYV12 = bgr48LEToY; break;
  655. }
  656. if (c->alpPixBuf) {
  657. switch (srcFormat) {
  658. case PIX_FMT_RGB32 :
  659. case PIX_FMT_RGB32_1:
  660. case PIX_FMT_BGR32 :
  661. case PIX_FMT_BGR32_1: c->alpToYV12 = abgrToA; break;
  662. case PIX_FMT_GRAY8A : c->alpToYV12 = yuy2ToY_c; break;
  663. case PIX_FMT_PAL8 : c->alpToYV12 = palToA; break;
  664. }
  665. }
  666. if(isAnyRGB(c->srcFormat) || c->srcFormat == PIX_FMT_PAL8)
  667. c->hScale16= hScale16_c;
  668. switch (srcFormat) {
  669. case PIX_FMT_GRAY8A :
  670. c->alpSrcOffset = 1;
  671. break;
  672. case PIX_FMT_RGB32 :
  673. case PIX_FMT_BGR32 :
  674. c->alpSrcOffset = 3;
  675. break;
  676. }
  677. if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
  678. if (c->srcRange) {
  679. c->lumConvertRange = lumRangeFromJpeg_c;
  680. c->chrConvertRange = chrRangeFromJpeg_c;
  681. } else {
  682. c->lumConvertRange = lumRangeToJpeg_c;
  683. c->chrConvertRange = chrRangeToJpeg_c;
  684. }
  685. }
  686. if (!(isGray(srcFormat) || isGray(c->dstFormat) ||
  687. srcFormat == PIX_FMT_MONOBLACK || srcFormat == PIX_FMT_MONOWHITE))
  688. c->needs_hcscale = 1;
  689. }