swscale.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976
  1. /*
  2. * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /*
  21. supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR32_1, BGR24, BGR16, BGR15, RGB32, RGB32_1, RGB24, Y8/Y800, YVU9/IF09, PAL8
  22. supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
  23. {BGR,RGB}{1,4,8,15,16} support dithering
  24. unscaled special converters (YV12=I420=IYUV, Y800=Y8)
  25. YV12 -> {BGR,RGB}{1,4,8,12,15,16,24,32}
  26. x -> x
  27. YUV9 -> YV12
  28. YUV9/YV12 -> Y800
  29. Y800 -> YUV9/YV12
  30. BGR24 -> BGR32 & RGB24 -> RGB32
  31. BGR32 -> BGR24 & RGB32 -> RGB24
  32. BGR15 -> BGR16
  33. */
  34. /*
  35. tested special converters (most are tested actually, but I did not write it down ...)
  36. YV12 -> BGR12/BGR16
  37. YV12 -> YV12
  38. BGR15 -> BGR16
  39. BGR16 -> BGR16
  40. YVU9 -> YV12
  41. untested special converters
  42. YV12/I420 -> BGR15/BGR24/BGR32 (it is the yuv2rgb stuff, so it should be OK)
  43. YV12/I420 -> YV12/I420
  44. YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
  45. BGR24 -> BGR32 & RGB24 -> RGB32
  46. BGR32 -> BGR24 & RGB32 -> RGB24
  47. BGR24 -> YV12
  48. */
  49. #include <inttypes.h>
  50. #include <string.h>
  51. #include <math.h>
  52. #include <stdio.h>
  53. #include "config.h"
  54. #include <assert.h>
  55. #include "swscale.h"
  56. #include "swscale_internal.h"
  57. #include "rgb2rgb.h"
  58. #include "libavutil/intreadwrite.h"
  59. #include "libavutil/x86_cpu.h"
  60. #include "libavutil/avutil.h"
  61. #include "libavutil/mathematics.h"
  62. #include "libavutil/bswap.h"
  63. #include "libavutil/pixdesc.h"
  64. #undef MOVNTQ
  65. #undef PAVGB
  66. //#undef HAVE_MMX2
  67. //#define HAVE_AMD3DNOW
  68. //#undef HAVE_MMX
  69. //#undef ARCH_X86
  70. #define DITHER1XBPP
  71. #define isPacked(x) ( \
  72. (x)==PIX_FMT_PAL8 \
  73. || (x)==PIX_FMT_YUYV422 \
  74. || (x)==PIX_FMT_UYVY422 \
  75. || (x)==PIX_FMT_Y400A \
  76. || isAnyRGB(x) \
  77. )
  78. #define RGB2YUV_SHIFT 15
  79. #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  80. #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  81. #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  82. #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  83. #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  84. #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  85. #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  86. #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  87. #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  88. static const double rgb2yuv_table[8][9]={
  89. {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
  90. {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
  91. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  92. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  93. {0.59 , 0.11 , 0.30 , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
  94. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  95. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  96. {0.701 , 0.087 , 0.212 , -0.384, 0.5, -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
  97. };
  98. /*
  99. NOTES
  100. Special versions: fast Y 1:1 scaling (no interpolation in y direction)
  101. TODO
  102. more intelligent misalignment avoidance for the horizontal scaler
  103. write special vertical cubic upscale version
  104. optimize C code (YV12 / minmax)
  105. add support for packed pixel YUV input & output
  106. add support for Y8 output
  107. optimize BGR24 & BGR32
  108. add BGR4 output support
  109. write special BGR->BGR scaler
  110. */
  111. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
  112. { 1, 3, 1, 3, 1, 3, 1, 3, },
  113. { 2, 0, 2, 0, 2, 0, 2, 0, },
  114. };
  115. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
  116. { 6, 2, 6, 2, 6, 2, 6, 2, },
  117. { 0, 4, 0, 4, 0, 4, 0, 4, },
  118. };
  119. DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[4][8]={
  120. { 8, 4, 11, 7, 8, 4, 11, 7, },
  121. { 2, 14, 1, 13, 2, 14, 1, 13, },
  122. { 10, 6, 9, 5, 10, 6, 9, 5, },
  123. { 0, 12, 3, 15, 0, 12, 3, 15, },
  124. };
  125. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
  126. { 17, 9, 23, 15, 16, 8, 22, 14, },
  127. { 5, 29, 3, 27, 4, 28, 2, 26, },
  128. { 21, 13, 19, 11, 20, 12, 18, 10, },
  129. { 0, 24, 6, 30, 1, 25, 7, 31, },
  130. { 16, 8, 22, 14, 17, 9, 23, 15, },
  131. { 4, 28, 2, 26, 5, 29, 3, 27, },
  132. { 20, 12, 18, 10, 21, 13, 19, 11, },
  133. { 1, 25, 7, 31, 0, 24, 6, 30, },
  134. };
  135. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
  136. { 0, 55, 14, 68, 3, 58, 17, 72, },
  137. { 37, 18, 50, 32, 40, 22, 54, 35, },
  138. { 9, 64, 5, 59, 13, 67, 8, 63, },
  139. { 46, 27, 41, 23, 49, 31, 44, 26, },
  140. { 2, 57, 16, 71, 1, 56, 15, 70, },
  141. { 39, 21, 52, 34, 38, 19, 51, 33, },
  142. { 11, 66, 7, 62, 10, 65, 6, 60, },
  143. { 48, 30, 43, 25, 47, 29, 42, 24, },
  144. };
  145. #if 1
  146. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  147. {117, 62, 158, 103, 113, 58, 155, 100, },
  148. { 34, 199, 21, 186, 31, 196, 17, 182, },
  149. {144, 89, 131, 76, 141, 86, 127, 72, },
  150. { 0, 165, 41, 206, 10, 175, 52, 217, },
  151. {110, 55, 151, 96, 120, 65, 162, 107, },
  152. { 28, 193, 14, 179, 38, 203, 24, 189, },
  153. {138, 83, 124, 69, 148, 93, 134, 79, },
  154. { 7, 172, 48, 213, 3, 168, 45, 210, },
  155. };
  156. #elif 1
  157. // tries to correct a gamma of 1.5
  158. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  159. { 0, 143, 18, 200, 2, 156, 25, 215, },
  160. { 78, 28, 125, 64, 89, 36, 138, 74, },
  161. { 10, 180, 3, 161, 16, 195, 8, 175, },
  162. {109, 51, 93, 38, 121, 60, 105, 47, },
  163. { 1, 152, 23, 210, 0, 147, 20, 205, },
  164. { 85, 33, 134, 71, 81, 30, 130, 67, },
  165. { 14, 190, 6, 171, 12, 185, 5, 166, },
  166. {117, 57, 101, 44, 113, 54, 97, 41, },
  167. };
  168. #elif 1
  169. // tries to correct a gamma of 2.0
  170. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  171. { 0, 124, 8, 193, 0, 140, 12, 213, },
  172. { 55, 14, 104, 42, 66, 19, 119, 52, },
  173. { 3, 168, 1, 145, 6, 187, 3, 162, },
  174. { 86, 31, 70, 21, 99, 39, 82, 28, },
  175. { 0, 134, 11, 206, 0, 129, 9, 200, },
  176. { 62, 17, 114, 48, 58, 16, 109, 45, },
  177. { 5, 181, 2, 157, 4, 175, 1, 151, },
  178. { 95, 36, 78, 26, 90, 34, 74, 24, },
  179. };
  180. #else
  181. // tries to correct a gamma of 2.5
  182. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  183. { 0, 107, 3, 187, 0, 125, 6, 212, },
  184. { 39, 7, 86, 28, 49, 11, 102, 36, },
  185. { 1, 158, 0, 131, 3, 180, 1, 151, },
  186. { 68, 19, 52, 12, 81, 25, 64, 17, },
  187. { 0, 119, 5, 203, 0, 113, 4, 195, },
  188. { 45, 9, 96, 33, 42, 8, 91, 30, },
  189. { 2, 172, 1, 144, 2, 165, 0, 137, },
  190. { 77, 23, 60, 15, 72, 21, 56, 14, },
  191. };
  192. #endif
  193. static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  194. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  195. const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
  196. int dstW, int chrDstW, int big_endian)
  197. {
  198. //FIXME Optimize (just quickly written not optimized..)
  199. int i;
  200. for (i = 0; i < dstW; i++) {
  201. int val = 1 << 10;
  202. int j;
  203. for (j = 0; j < lumFilterSize; j++)
  204. val += lumSrc[j][i] * lumFilter[j];
  205. if (big_endian) {
  206. AV_WB16(&dest[i], av_clip_uint16(val >> 11));
  207. } else {
  208. AV_WL16(&dest[i], av_clip_uint16(val >> 11));
  209. }
  210. }
  211. if (uDest) {
  212. for (i = 0; i < chrDstW; i++) {
  213. int u = 1 << 10;
  214. int v = 1 << 10;
  215. int j;
  216. for (j = 0; j < chrFilterSize; j++) {
  217. u += chrSrc[j][i ] * chrFilter[j];
  218. v += chrSrc[j][i + VOFW] * chrFilter[j];
  219. }
  220. if (big_endian) {
  221. AV_WB16(&uDest[i], av_clip_uint16(u >> 11));
  222. AV_WB16(&vDest[i], av_clip_uint16(v >> 11));
  223. } else {
  224. AV_WL16(&uDest[i], av_clip_uint16(u >> 11));
  225. AV_WL16(&vDest[i], av_clip_uint16(v >> 11));
  226. }
  227. }
  228. }
  229. if (CONFIG_SWSCALE_ALPHA && aDest) {
  230. for (i = 0; i < dstW; i++) {
  231. int val = 1 << 10;
  232. int j;
  233. for (j = 0; j < lumFilterSize; j++)
  234. val += alpSrc[j][i] * lumFilter[j];
  235. if (big_endian) {
  236. AV_WB16(&aDest[i], av_clip_uint16(val >> 11));
  237. } else {
  238. AV_WL16(&aDest[i], av_clip_uint16(val >> 11));
  239. }
  240. }
  241. }
  242. }
  243. static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  244. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  245. const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
  246. enum PixelFormat dstFormat)
  247. {
  248. if (isBE(dstFormat)) {
  249. yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
  250. chrFilter, chrSrc, chrFilterSize,
  251. alpSrc,
  252. dest, uDest, vDest, aDest,
  253. dstW, chrDstW, 1);
  254. } else {
  255. yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
  256. chrFilter, chrSrc, chrFilterSize,
  257. alpSrc,
  258. dest, uDest, vDest, aDest,
  259. dstW, chrDstW, 0);
  260. }
  261. }
  262. static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  263. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  264. const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
  265. {
  266. //FIXME Optimize (just quickly written not optimized..)
  267. int i;
  268. for (i=0; i<dstW; i++) {
  269. int val=1<<18;
  270. int j;
  271. for (j=0; j<lumFilterSize; j++)
  272. val += lumSrc[j][i] * lumFilter[j];
  273. dest[i]= av_clip_uint8(val>>19);
  274. }
  275. if (uDest)
  276. for (i=0; i<chrDstW; i++) {
  277. int u=1<<18;
  278. int v=1<<18;
  279. int j;
  280. for (j=0; j<chrFilterSize; j++) {
  281. u += chrSrc[j][i] * chrFilter[j];
  282. v += chrSrc[j][i + VOFW] * chrFilter[j];
  283. }
  284. uDest[i]= av_clip_uint8(u>>19);
  285. vDest[i]= av_clip_uint8(v>>19);
  286. }
  287. if (CONFIG_SWSCALE_ALPHA && aDest)
  288. for (i=0; i<dstW; i++) {
  289. int val=1<<18;
  290. int j;
  291. for (j=0; j<lumFilterSize; j++)
  292. val += alpSrc[j][i] * lumFilter[j];
  293. aDest[i]= av_clip_uint8(val>>19);
  294. }
  295. }
  296. static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  297. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  298. uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
  299. {
  300. //FIXME Optimize (just quickly written not optimized..)
  301. int i;
  302. for (i=0; i<dstW; i++) {
  303. int val=1<<18;
  304. int j;
  305. for (j=0; j<lumFilterSize; j++)
  306. val += lumSrc[j][i] * lumFilter[j];
  307. dest[i]= av_clip_uint8(val>>19);
  308. }
  309. if (!uDest)
  310. return;
  311. if (dstFormat == PIX_FMT_NV12)
  312. for (i=0; i<chrDstW; i++) {
  313. int u=1<<18;
  314. int v=1<<18;
  315. int j;
  316. for (j=0; j<chrFilterSize; j++) {
  317. u += chrSrc[j][i] * chrFilter[j];
  318. v += chrSrc[j][i + VOFW] * chrFilter[j];
  319. }
  320. uDest[2*i]= av_clip_uint8(u>>19);
  321. uDest[2*i+1]= av_clip_uint8(v>>19);
  322. }
  323. else
  324. for (i=0; i<chrDstW; i++) {
  325. int u=1<<18;
  326. int v=1<<18;
  327. int j;
  328. for (j=0; j<chrFilterSize; j++) {
  329. u += chrSrc[j][i] * chrFilter[j];
  330. v += chrSrc[j][i + VOFW] * chrFilter[j];
  331. }
  332. uDest[2*i]= av_clip_uint8(v>>19);
  333. uDest[2*i+1]= av_clip_uint8(u>>19);
  334. }
  335. }
  336. #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
  337. for (i=0; i<(dstW>>1); i++) {\
  338. int j;\
  339. int Y1 = 1<<18;\
  340. int Y2 = 1<<18;\
  341. int U = 1<<18;\
  342. int V = 1<<18;\
  343. int av_unused A1, A2;\
  344. type av_unused *r, *b, *g;\
  345. const int i2= 2*i;\
  346. \
  347. for (j=0; j<lumFilterSize; j++) {\
  348. Y1 += lumSrc[j][i2] * lumFilter[j];\
  349. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  350. }\
  351. for (j=0; j<chrFilterSize; j++) {\
  352. U += chrSrc[j][i] * chrFilter[j];\
  353. V += chrSrc[j][i+VOFW] * chrFilter[j];\
  354. }\
  355. Y1>>=19;\
  356. Y2>>=19;\
  357. U >>=19;\
  358. V >>=19;\
  359. if (alpha) {\
  360. A1 = 1<<18;\
  361. A2 = 1<<18;\
  362. for (j=0; j<lumFilterSize; j++) {\
  363. A1 += alpSrc[j][i2 ] * lumFilter[j];\
  364. A2 += alpSrc[j][i2+1] * lumFilter[j];\
  365. }\
  366. A1>>=19;\
  367. A2>>=19;\
  368. }
  369. #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
  370. YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
  371. if ((Y1|Y2|U|V)&256) {\
  372. if (Y1>255) Y1=255; \
  373. else if (Y1<0)Y1=0; \
  374. if (Y2>255) Y2=255; \
  375. else if (Y2<0)Y2=0; \
  376. if (U>255) U=255; \
  377. else if (U<0) U=0; \
  378. if (V>255) V=255; \
  379. else if (V<0) V=0; \
  380. }\
  381. if (alpha && ((A1|A2)&256)) {\
  382. A1=av_clip_uint8(A1);\
  383. A2=av_clip_uint8(A2);\
  384. }
  385. #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
  386. for (i=0; i<dstW; i++) {\
  387. int j;\
  388. int Y = 0;\
  389. int U = -128<<19;\
  390. int V = -128<<19;\
  391. int av_unused A;\
  392. int R,G,B;\
  393. \
  394. for (j=0; j<lumFilterSize; j++) {\
  395. Y += lumSrc[j][i ] * lumFilter[j];\
  396. }\
  397. for (j=0; j<chrFilterSize; j++) {\
  398. U += chrSrc[j][i ] * chrFilter[j];\
  399. V += chrSrc[j][i+VOFW] * chrFilter[j];\
  400. }\
  401. Y >>=10;\
  402. U >>=10;\
  403. V >>=10;\
  404. if (alpha) {\
  405. A = rnd;\
  406. for (j=0; j<lumFilterSize; j++)\
  407. A += alpSrc[j][i ] * lumFilter[j];\
  408. A >>=19;\
  409. if (A&256)\
  410. A = av_clip_uint8(A);\
  411. }
  412. #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
  413. YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
  414. Y-= c->yuv2rgb_y_offset;\
  415. Y*= c->yuv2rgb_y_coeff;\
  416. Y+= rnd;\
  417. R= Y + V*c->yuv2rgb_v2r_coeff;\
  418. G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
  419. B= Y + U*c->yuv2rgb_u2b_coeff;\
  420. if ((R|G|B)&(0xC0000000)) {\
  421. if (R>=(256<<22)) R=(256<<22)-1; \
  422. else if (R<0)R=0; \
  423. if (G>=(256<<22)) G=(256<<22)-1; \
  424. else if (G<0)G=0; \
  425. if (B>=(256<<22)) B=(256<<22)-1; \
  426. else if (B<0)B=0; \
  427. }
  428. #define YSCALE_YUV_2_GRAY16_C \
  429. for (i=0; i<(dstW>>1); i++) {\
  430. int j;\
  431. int Y1 = 1<<18;\
  432. int Y2 = 1<<18;\
  433. int U = 1<<18;\
  434. int V = 1<<18;\
  435. \
  436. const int i2= 2*i;\
  437. \
  438. for (j=0; j<lumFilterSize; j++) {\
  439. Y1 += lumSrc[j][i2] * lumFilter[j];\
  440. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  441. }\
  442. Y1>>=11;\
  443. Y2>>=11;\
  444. if ((Y1|Y2|U|V)&65536) {\
  445. if (Y1>65535) Y1=65535; \
  446. else if (Y1<0)Y1=0; \
  447. if (Y2>65535) Y2=65535; \
  448. else if (Y2<0)Y2=0; \
  449. }
  450. #define YSCALE_YUV_2_RGBX_C(type,alpha) \
  451. YSCALE_YUV_2_PACKEDX_C(type,alpha) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
  452. r = (type *)c->table_rV[V]; \
  453. g = (type *)(c->table_gU[U] + c->table_gV[V]); \
  454. b = (type *)c->table_bU[U];
  455. #define YSCALE_YUV_2_PACKED2_C(type,alpha) \
  456. for (i=0; i<(dstW>>1); i++) { \
  457. const int i2= 2*i; \
  458. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
  459. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
  460. int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
  461. int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
  462. type av_unused *r, *b, *g; \
  463. int av_unused A1, A2; \
  464. if (alpha) {\
  465. A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
  466. A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
  467. }
  468. #define YSCALE_YUV_2_GRAY16_2_C \
  469. for (i=0; i<(dstW>>1); i++) { \
  470. const int i2= 2*i; \
  471. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
  472. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;
  473. #define YSCALE_YUV_2_RGB2_C(type,alpha) \
  474. YSCALE_YUV_2_PACKED2_C(type,alpha)\
  475. r = (type *)c->table_rV[V];\
  476. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  477. b = (type *)c->table_bU[U];
  478. #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
  479. for (i=0; i<(dstW>>1); i++) {\
  480. const int i2= 2*i;\
  481. int Y1= buf0[i2 ]>>7;\
  482. int Y2= buf0[i2+1]>>7;\
  483. int U= (uvbuf1[i ])>>7;\
  484. int V= (uvbuf1[i+VOFW])>>7;\
  485. type av_unused *r, *b, *g;\
  486. int av_unused A1, A2;\
  487. if (alpha) {\
  488. A1= abuf0[i2 ]>>7;\
  489. A2= abuf0[i2+1]>>7;\
  490. }
  491. #define YSCALE_YUV_2_GRAY16_1_C \
  492. for (i=0; i<(dstW>>1); i++) {\
  493. const int i2= 2*i;\
  494. int Y1= buf0[i2 ]<<1;\
  495. int Y2= buf0[i2+1]<<1;
  496. #define YSCALE_YUV_2_RGB1_C(type,alpha) \
  497. YSCALE_YUV_2_PACKED1_C(type,alpha)\
  498. r = (type *)c->table_rV[V];\
  499. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  500. b = (type *)c->table_bU[U];
  501. #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
  502. for (i=0; i<(dstW>>1); i++) {\
  503. const int i2= 2*i;\
  504. int Y1= buf0[i2 ]>>7;\
  505. int Y2= buf0[i2+1]>>7;\
  506. int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
  507. int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
  508. type av_unused *r, *b, *g;\
  509. int av_unused A1, A2;\
  510. if (alpha) {\
  511. A1= abuf0[i2 ]>>7;\
  512. A2= abuf0[i2+1]>>7;\
  513. }
  514. #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
  515. YSCALE_YUV_2_PACKED1B_C(type,alpha)\
  516. r = (type *)c->table_rV[V];\
  517. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  518. b = (type *)c->table_bU[U];
  519. #define YSCALE_YUV_2_MONO2_C \
  520. const uint8_t * const d128=dither_8x8_220[y&7];\
  521. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  522. for (i=0; i<dstW-7; i+=8) {\
  523. int acc;\
  524. acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
  525. acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
  526. acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
  527. acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
  528. acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
  529. acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
  530. acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
  531. acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
  532. ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
  533. dest++;\
  534. }
  535. #define YSCALE_YUV_2_MONOX_C \
  536. const uint8_t * const d128=dither_8x8_220[y&7];\
  537. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  538. int acc=0;\
  539. for (i=0; i<dstW-1; i+=2) {\
  540. int j;\
  541. int Y1=1<<18;\
  542. int Y2=1<<18;\
  543. \
  544. for (j=0; j<lumFilterSize; j++) {\
  545. Y1 += lumSrc[j][i] * lumFilter[j];\
  546. Y2 += lumSrc[j][i+1] * lumFilter[j];\
  547. }\
  548. Y1>>=19;\
  549. Y2>>=19;\
  550. if ((Y1|Y2)&256) {\
  551. if (Y1>255) Y1=255;\
  552. else if (Y1<0)Y1=0;\
  553. if (Y2>255) Y2=255;\
  554. else if (Y2<0)Y2=0;\
  555. }\
  556. acc+= acc + g[Y1+d128[(i+0)&7]];\
  557. acc+= acc + g[Y2+d128[(i+1)&7]];\
  558. if ((i&7)==6) {\
  559. ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
  560. dest++;\
  561. }\
  562. }
  563. #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
  564. switch(c->dstFormat) {\
  565. case PIX_FMT_RGB48BE:\
  566. case PIX_FMT_RGB48LE:\
  567. func(uint8_t,0)\
  568. ((uint8_t*)dest)[ 0]= r[Y1];\
  569. ((uint8_t*)dest)[ 1]= r[Y1];\
  570. ((uint8_t*)dest)[ 2]= g[Y1];\
  571. ((uint8_t*)dest)[ 3]= g[Y1];\
  572. ((uint8_t*)dest)[ 4]= b[Y1];\
  573. ((uint8_t*)dest)[ 5]= b[Y1];\
  574. ((uint8_t*)dest)[ 6]= r[Y2];\
  575. ((uint8_t*)dest)[ 7]= r[Y2];\
  576. ((uint8_t*)dest)[ 8]= g[Y2];\
  577. ((uint8_t*)dest)[ 9]= g[Y2];\
  578. ((uint8_t*)dest)[10]= b[Y2];\
  579. ((uint8_t*)dest)[11]= b[Y2];\
  580. dest+=12;\
  581. }\
  582. break;\
  583. case PIX_FMT_RGBA:\
  584. case PIX_FMT_BGRA:\
  585. if (CONFIG_SMALL) {\
  586. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
  587. func(uint32_t,needAlpha)\
  588. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
  589. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
  590. }\
  591. } else {\
  592. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
  593. func(uint32_t,1)\
  594. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
  595. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
  596. }\
  597. } else {\
  598. func(uint32_t,0)\
  599. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  600. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  601. }\
  602. }\
  603. }\
  604. break;\
  605. case PIX_FMT_ARGB:\
  606. case PIX_FMT_ABGR:\
  607. if (CONFIG_SMALL) {\
  608. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
  609. func(uint32_t,needAlpha)\
  610. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
  611. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
  612. }\
  613. } else {\
  614. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
  615. func(uint32_t,1)\
  616. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
  617. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
  618. }\
  619. } else {\
  620. func(uint32_t,0)\
  621. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  622. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  623. }\
  624. }\
  625. } \
  626. break;\
  627. case PIX_FMT_RGB24:\
  628. func(uint8_t,0)\
  629. ((uint8_t*)dest)[0]= r[Y1];\
  630. ((uint8_t*)dest)[1]= g[Y1];\
  631. ((uint8_t*)dest)[2]= b[Y1];\
  632. ((uint8_t*)dest)[3]= r[Y2];\
  633. ((uint8_t*)dest)[4]= g[Y2];\
  634. ((uint8_t*)dest)[5]= b[Y2];\
  635. dest+=6;\
  636. }\
  637. break;\
  638. case PIX_FMT_BGR24:\
  639. func(uint8_t,0)\
  640. ((uint8_t*)dest)[0]= b[Y1];\
  641. ((uint8_t*)dest)[1]= g[Y1];\
  642. ((uint8_t*)dest)[2]= r[Y1];\
  643. ((uint8_t*)dest)[3]= b[Y2];\
  644. ((uint8_t*)dest)[4]= g[Y2];\
  645. ((uint8_t*)dest)[5]= r[Y2];\
  646. dest+=6;\
  647. }\
  648. break;\
  649. case PIX_FMT_RGB565BE:\
  650. case PIX_FMT_RGB565LE:\
  651. case PIX_FMT_BGR565BE:\
  652. case PIX_FMT_BGR565LE:\
  653. {\
  654. const int dr1= dither_2x2_8[y&1 ][0];\
  655. const int dg1= dither_2x2_4[y&1 ][0];\
  656. const int db1= dither_2x2_8[(y&1)^1][0];\
  657. const int dr2= dither_2x2_8[y&1 ][1];\
  658. const int dg2= dither_2x2_4[y&1 ][1];\
  659. const int db2= dither_2x2_8[(y&1)^1][1];\
  660. func(uint16_t,0)\
  661. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  662. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  663. }\
  664. }\
  665. break;\
  666. case PIX_FMT_RGB555BE:\
  667. case PIX_FMT_RGB555LE:\
  668. case PIX_FMT_BGR555BE:\
  669. case PIX_FMT_BGR555LE:\
  670. {\
  671. const int dr1= dither_2x2_8[y&1 ][0];\
  672. const int dg1= dither_2x2_8[y&1 ][1];\
  673. const int db1= dither_2x2_8[(y&1)^1][0];\
  674. const int dr2= dither_2x2_8[y&1 ][1];\
  675. const int dg2= dither_2x2_8[y&1 ][0];\
  676. const int db2= dither_2x2_8[(y&1)^1][1];\
  677. func(uint16_t,0)\
  678. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  679. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  680. }\
  681. }\
  682. break;\
  683. case PIX_FMT_RGB444BE:\
  684. case PIX_FMT_RGB444LE:\
  685. case PIX_FMT_BGR444BE:\
  686. case PIX_FMT_BGR444LE:\
  687. {\
  688. const int dr1= dither_4x4_16[y&3 ][0];\
  689. const int dg1= dither_4x4_16[y&3 ][1];\
  690. const int db1= dither_4x4_16[(y&3)^3][0];\
  691. const int dr2= dither_4x4_16[y&3 ][1];\
  692. const int dg2= dither_4x4_16[y&3 ][0];\
  693. const int db2= dither_4x4_16[(y&3)^3][1];\
  694. func(uint16_t,0)\
  695. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  696. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  697. }\
  698. }\
  699. break;\
  700. case PIX_FMT_RGB8:\
  701. case PIX_FMT_BGR8:\
  702. {\
  703. const uint8_t * const d64= dither_8x8_73[y&7];\
  704. const uint8_t * const d32= dither_8x8_32[y&7];\
  705. func(uint8_t,0)\
  706. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
  707. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
  708. }\
  709. }\
  710. break;\
  711. case PIX_FMT_RGB4:\
  712. case PIX_FMT_BGR4:\
  713. {\
  714. const uint8_t * const d64= dither_8x8_73 [y&7];\
  715. const uint8_t * const d128=dither_8x8_220[y&7];\
  716. func(uint8_t,0)\
  717. ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
  718. + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
  719. }\
  720. }\
  721. break;\
  722. case PIX_FMT_RGB4_BYTE:\
  723. case PIX_FMT_BGR4_BYTE:\
  724. {\
  725. const uint8_t * const d64= dither_8x8_73 [y&7];\
  726. const uint8_t * const d128=dither_8x8_220[y&7];\
  727. func(uint8_t,0)\
  728. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
  729. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
  730. }\
  731. }\
  732. break;\
  733. case PIX_FMT_MONOBLACK:\
  734. case PIX_FMT_MONOWHITE:\
  735. {\
  736. func_monoblack\
  737. }\
  738. break;\
  739. case PIX_FMT_YUYV422:\
  740. func2\
  741. ((uint8_t*)dest)[2*i2+0]= Y1;\
  742. ((uint8_t*)dest)[2*i2+1]= U;\
  743. ((uint8_t*)dest)[2*i2+2]= Y2;\
  744. ((uint8_t*)dest)[2*i2+3]= V;\
  745. } \
  746. break;\
  747. case PIX_FMT_UYVY422:\
  748. func2\
  749. ((uint8_t*)dest)[2*i2+0]= U;\
  750. ((uint8_t*)dest)[2*i2+1]= Y1;\
  751. ((uint8_t*)dest)[2*i2+2]= V;\
  752. ((uint8_t*)dest)[2*i2+3]= Y2;\
  753. } \
  754. break;\
  755. case PIX_FMT_GRAY16BE:\
  756. func_g16\
  757. ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
  758. ((uint8_t*)dest)[2*i2+1]= Y1;\
  759. ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
  760. ((uint8_t*)dest)[2*i2+3]= Y2;\
  761. } \
  762. break;\
  763. case PIX_FMT_GRAY16LE:\
  764. func_g16\
  765. ((uint8_t*)dest)[2*i2+0]= Y1;\
  766. ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
  767. ((uint8_t*)dest)[2*i2+2]= Y2;\
  768. ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
  769. } \
  770. break;\
  771. }
  772. static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  773. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  774. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  775. {
  776. int i;
  777. YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGBX_C, YSCALE_YUV_2_PACKEDX_C(void,0), YSCALE_YUV_2_GRAY16_C, YSCALE_YUV_2_MONOX_C)
  778. }
  779. static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  780. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  781. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  782. {
  783. int i;
  784. int step= c->dstFormatBpp/8;
  785. int aidx= 3;
  786. switch(c->dstFormat) {
  787. case PIX_FMT_ARGB:
  788. dest++;
  789. aidx= 0;
  790. case PIX_FMT_RGB24:
  791. aidx--;
  792. case PIX_FMT_RGBA:
  793. if (CONFIG_SMALL) {
  794. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
  795. YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
  796. dest[aidx]= needAlpha ? A : 255;
  797. dest[0]= R>>22;
  798. dest[1]= G>>22;
  799. dest[2]= B>>22;
  800. dest+= step;
  801. }
  802. } else {
  803. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  804. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
  805. dest[aidx]= A;
  806. dest[0]= R>>22;
  807. dest[1]= G>>22;
  808. dest[2]= B>>22;
  809. dest+= step;
  810. }
  811. } else {
  812. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
  813. dest[aidx]= 255;
  814. dest[0]= R>>22;
  815. dest[1]= G>>22;
  816. dest[2]= B>>22;
  817. dest+= step;
  818. }
  819. }
  820. }
  821. break;
  822. case PIX_FMT_ABGR:
  823. dest++;
  824. aidx= 0;
  825. case PIX_FMT_BGR24:
  826. aidx--;
  827. case PIX_FMT_BGRA:
  828. if (CONFIG_SMALL) {
  829. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
  830. YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
  831. dest[aidx]= needAlpha ? A : 255;
  832. dest[0]= B>>22;
  833. dest[1]= G>>22;
  834. dest[2]= R>>22;
  835. dest+= step;
  836. }
  837. } else {
  838. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  839. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
  840. dest[aidx]= A;
  841. dest[0]= B>>22;
  842. dest[1]= G>>22;
  843. dest[2]= R>>22;
  844. dest+= step;
  845. }
  846. } else {
  847. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
  848. dest[aidx]= 255;
  849. dest[0]= B>>22;
  850. dest[1]= G>>22;
  851. dest[2]= R>>22;
  852. dest+= step;
  853. }
  854. }
  855. }
  856. break;
  857. default:
  858. assert(0);
  859. }
  860. }
  861. static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
  862. {
  863. int i;
  864. uint8_t *ptr = plane + stride*y;
  865. for (i=0; i<height; i++) {
  866. memset(ptr, val, width);
  867. ptr += stride;
  868. }
  869. }
  870. static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, long width,
  871. uint32_t *unused)
  872. {
  873. int i;
  874. for (i = 0; i < width; i++) {
  875. int r = src[i*6+0];
  876. int g = src[i*6+2];
  877. int b = src[i*6+4];
  878. dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  879. }
  880. }
  881. static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
  882. const uint8_t *src1, const uint8_t *src2,
  883. long width, uint32_t *unused)
  884. {
  885. int i;
  886. assert(src1==src2);
  887. for (i = 0; i < width; i++) {
  888. int r = src1[6*i + 0];
  889. int g = src1[6*i + 2];
  890. int b = src1[6*i + 4];
  891. dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  892. dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  893. }
  894. }
  895. static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
  896. const uint8_t *src1, const uint8_t *src2,
  897. long width, uint32_t *unused)
  898. {
  899. int i;
  900. assert(src1==src2);
  901. for (i = 0; i < width; i++) {
  902. int r= src1[12*i + 0] + src1[12*i + 6];
  903. int g= src1[12*i + 2] + src1[12*i + 8];
  904. int b= src1[12*i + 4] + src1[12*i + 10];
  905. dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  906. dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  907. }
  908. }
  909. #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
  910. static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
  911. {\
  912. int i;\
  913. for (i=0; i<width; i++) {\
  914. int b= (((const type*)src)[i]>>shb)&maskb;\
  915. int g= (((const type*)src)[i]>>shg)&maskg;\
  916. int r= (((const type*)src)[i]>>shr)&maskr;\
  917. \
  918. dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
  919. }\
  920. }
  921. BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
  922. BGR2Y(uint32_t,bgr321ToY,16,16, 0, 0xFF00, 0x00FF, 0xFF00, RY , GY<<8, BY , RGB2YUV_SHIFT+8)
  923. BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
  924. BGR2Y(uint32_t,rgb321ToY, 0,16,16, 0xFF00, 0x00FF, 0xFF00, RY , GY<<8, BY , RGB2YUV_SHIFT+8)
  925. BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY , RGB2YUV_SHIFT+8)
  926. BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY , RGB2YUV_SHIFT+7)
  927. BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
  928. BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
  929. static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  930. {
  931. int i;
  932. for (i=0; i<width; i++) {
  933. dst[i]= src[4*i];
  934. }
  935. }
  936. #define BGR2UV(type, name, shr, shg, shb, shp, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S) \
  937. static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
  938. {\
  939. int i;\
  940. for (i=0; i<width; i++) {\
  941. int b= ((((const type*)src)[i]>>shp)&maskb)>>shb;\
  942. int g= ((((const type*)src)[i]>>shp)&maskg)>>shg;\
  943. int r= ((((const type*)src)[i]>>shp)&maskr)>>shr;\
  944. \
  945. dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
  946. dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
  947. }\
  948. }\
  949. static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
  950. {\
  951. int i;\
  952. for (i=0; i<width; i++) {\
  953. int pix0= ((const type*)src)[2*i+0]>>shp;\
  954. int pix1= ((const type*)src)[2*i+1]>>shp;\
  955. int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
  956. int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
  957. int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
  958. g&= maskg|(2*maskg);\
  959. \
  960. g>>=shg;\
  961. \
  962. dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
  963. dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
  964. }\
  965. }
  966. BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  967. BGR2UV(uint32_t,bgr321ToUV,16, 0, 0, 8, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  968. BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  969. BGR2UV(uint32_t,rgb321ToUV, 0, 0,16, 8, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  970. BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RU<<11, GU<<5, BU , RV<<11, GV<<5, BV , RGB2YUV_SHIFT+8)
  971. BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RU<<10, GU<<5, BU , RV<<10, GV<<5, BV , RGB2YUV_SHIFT+7)
  972. BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RU , GU<<5, BU<<11, RV , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
  973. BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RU , GU<<5, BU<<10, RV , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
  974. static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
  975. {
  976. int i;
  977. for (i=0; i<width; i++) {
  978. int d= src[i];
  979. dst[i]= pal[d] & 0xFF;
  980. }
  981. }
  982. static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
  983. const uint8_t *src1, const uint8_t *src2,
  984. long width, uint32_t *pal)
  985. {
  986. int i;
  987. assert(src1 == src2);
  988. for (i=0; i<width; i++) {
  989. int p= pal[src1[i]];
  990. dstU[i]= p>>8;
  991. dstV[i]= p>>16;
  992. }
  993. }
  994. static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  995. {
  996. int i, j;
  997. for (i=0; i<width/8; i++) {
  998. int d= ~src[i];
  999. for(j=0; j<8; j++)
  1000. dst[8*i+j]= ((d>>(7-j))&1)*255;
  1001. }
  1002. }
  1003. static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  1004. {
  1005. int i, j;
  1006. for (i=0; i<width/8; i++) {
  1007. int d= src[i];
  1008. for(j=0; j<8; j++)
  1009. dst[8*i+j]= ((d>>(7-j))&1)*255;
  1010. }
  1011. }
  1012. //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
  1013. //Plain C versions
  1014. #if CONFIG_RUNTIME_CPUDETECT
  1015. # define COMPILE_C 1
  1016. # if ARCH_X86
  1017. # define COMPILE_MMX HAVE_MMX
  1018. # define COMPILE_MMX2 HAVE_MMX2
  1019. # define COMPILE_3DNOW HAVE_AMD3DNOW
  1020. # elif ARCH_PPC
  1021. # define COMPILE_ALTIVEC HAVE_ALTIVEC
  1022. # endif
  1023. #else /* CONFIG_RUNTIME_CPUDETECT */
  1024. # if ARCH_X86
  1025. # if HAVE_MMX2
  1026. # define COMPILE_MMX2 1
  1027. # elif HAVE_AMD3DNOW
  1028. # define COMPILE_3DNOW 1
  1029. # elif HAVE_MMX
  1030. # define COMPILE_MMX 1
  1031. # else
  1032. # define COMPILE_C 1
  1033. # endif
  1034. # elif ARCH_PPC && HAVE_ALTIVEC
  1035. # define COMPILE_ALTIVEC 1
  1036. # else
  1037. # define COMPILE_C 1
  1038. # endif
  1039. #endif
  1040. #ifndef COMPILE_C
  1041. # define COMPILE_C 0
  1042. #endif
  1043. #ifndef COMPILE_MMX
  1044. # define COMPILE_MMX 0
  1045. #endif
  1046. #ifndef COMPILE_MMX2
  1047. # define COMPILE_MMX2 0
  1048. #endif
  1049. #ifndef COMPILE_3DNOW
  1050. # define COMPILE_3DNOW 0
  1051. #endif
  1052. #ifndef COMPILE_ALTIVEC
  1053. # define COMPILE_ALTIVEC 0
  1054. #endif
  1055. #define COMPILE_TEMPLATE_MMX 0
  1056. #define COMPILE_TEMPLATE_MMX2 0
  1057. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1058. #define COMPILE_TEMPLATE_ALTIVEC 0
  1059. #include "swscale_template.c"
  1060. #if COMPILE_ALTIVEC
  1061. #undef RENAME
  1062. #undef COMPILE_TEMPLATE_ALTIVEC
  1063. #define COMPILE_TEMPLATE_ALTIVEC 1
  1064. #define RENAME(a) a ## _altivec
  1065. #include "ppc/swscale_template.c"
  1066. #endif
  1067. #if ARCH_X86
  1068. //MMX versions
  1069. #if COMPILE_MMX
  1070. #undef RENAME
  1071. #undef COMPILE_TEMPLATE_MMX
  1072. #undef COMPILE_TEMPLATE_MMX2
  1073. #undef COMPILE_TEMPLATE_AMD3DNOW
  1074. #define COMPILE_TEMPLATE_MMX 1
  1075. #define COMPILE_TEMPLATE_MMX2 0
  1076. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1077. #define RENAME(a) a ## _MMX
  1078. #include "x86/swscale_template.c"
  1079. #endif
  1080. //MMX2 versions
  1081. #if COMPILE_MMX2
  1082. #undef RENAME
  1083. #undef COMPILE_TEMPLATE_MMX
  1084. #undef COMPILE_TEMPLATE_MMX2
  1085. #undef COMPILE_TEMPLATE_AMD3DNOW
  1086. #define COMPILE_TEMPLATE_MMX 1
  1087. #define COMPILE_TEMPLATE_MMX2 1
  1088. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1089. #define RENAME(a) a ## _MMX2
  1090. #include "x86/swscale_template.c"
  1091. #endif
  1092. //3DNOW versions
  1093. #if COMPILE_3DNOW
  1094. #undef RENAME
  1095. #undef COMPILE_TEMPLATE_MMX
  1096. #undef COMPILE_TEMPLATE_MMX2
  1097. #undef COMPILE_TEMPLATE_AMD3DNOW
  1098. #define COMPILE_TEMPLATE_MMX 1
  1099. #define COMPILE_TEMPLATE_MMX2 0
  1100. #define COMPILE_TEMPLATE_AMD3DNOW 1
  1101. #define RENAME(a) a ## _3DNow
  1102. #include "x86/swscale_template.c"
  1103. #endif
  1104. #endif //ARCH_X86
  1105. SwsFunc ff_getSwsFunc(SwsContext *c)
  1106. {
  1107. sws_init_swScale_c(c);
  1108. #if CONFIG_RUNTIME_CPUDETECT
  1109. #if ARCH_X86
  1110. // ordered per speed fastest first
  1111. if (c->flags & SWS_CPU_CAPS_MMX2) {
  1112. sws_init_swScale_MMX2(c);
  1113. return swScale_MMX2;
  1114. } else if (c->flags & SWS_CPU_CAPS_3DNOW) {
  1115. sws_init_swScale_3DNow(c);
  1116. return swScale_3DNow;
  1117. } else if (c->flags & SWS_CPU_CAPS_MMX) {
  1118. sws_init_swScale_MMX(c);
  1119. return swScale_MMX;
  1120. }
  1121. #else
  1122. #if COMPILE_ALTIVEC
  1123. if (c->flags & SWS_CPU_CAPS_ALTIVEC) {
  1124. sws_init_swScale_altivec(c);
  1125. return swScale_altivec;
  1126. }
  1127. #endif
  1128. #endif /* ARCH_X86 */
  1129. #else //CONFIG_RUNTIME_CPUDETECT
  1130. #if COMPILE_TEMPLATE_MMX2
  1131. sws_init_swScale_MMX2(c);
  1132. return swScale_MMX2;
  1133. #elif COMPILE_TEMPLATE_AMD3DNOW
  1134. sws_init_swScale_3DNow(c);
  1135. return swScale_3DNow;
  1136. #elif COMPILE_TEMPLATE_MMX
  1137. sws_init_swScale_MMX(c);
  1138. return swScale_MMX;
  1139. #elif COMPILE_TEMPLATE_ALTIVEC
  1140. sws_init_swScale_altivec(c);
  1141. return swScale_altivec;
  1142. #endif
  1143. #endif //!CONFIG_RUNTIME_CPUDETECT
  1144. return swScale_c;
  1145. }
  1146. static void copyPlane(const uint8_t *src, int srcStride,
  1147. int srcSliceY, int srcSliceH, int width,
  1148. uint8_t *dst, int dstStride)
  1149. {
  1150. dst += dstStride * srcSliceY;
  1151. if (dstStride == srcStride && srcStride > 0) {
  1152. memcpy(dst, src, srcSliceH * dstStride);
  1153. } else {
  1154. int i;
  1155. for (i=0; i<srcSliceH; i++) {
  1156. memcpy(dst, src, width);
  1157. src += srcStride;
  1158. dst += dstStride;
  1159. }
  1160. }
  1161. }
  1162. static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1163. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1164. {
  1165. uint8_t *dst = dstParam[1] + dstStride[1]*srcSliceY/2;
  1166. copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
  1167. dstParam[0], dstStride[0]);
  1168. if (c->dstFormat == PIX_FMT_NV12)
  1169. interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
  1170. else
  1171. interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
  1172. return srcSliceH;
  1173. }
  1174. static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1175. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1176. {
  1177. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1178. yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
  1179. return srcSliceH;
  1180. }
  1181. static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1182. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1183. {
  1184. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1185. yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
  1186. return srcSliceH;
  1187. }
  1188. static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1189. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1190. {
  1191. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1192. yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
  1193. return srcSliceH;
  1194. }
  1195. static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1196. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1197. {
  1198. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1199. yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
  1200. return srcSliceH;
  1201. }
  1202. static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1203. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1204. {
  1205. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1206. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
  1207. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
  1208. yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1209. if (dstParam[3])
  1210. fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1211. return srcSliceH;
  1212. }
  1213. static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1214. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1215. {
  1216. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1217. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
  1218. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
  1219. yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1220. return srcSliceH;
  1221. }
  1222. static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1223. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1224. {
  1225. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1226. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
  1227. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
  1228. uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1229. if (dstParam[3])
  1230. fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1231. return srcSliceH;
  1232. }
  1233. static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1234. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1235. {
  1236. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1237. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
  1238. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
  1239. uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1240. return srcSliceH;
  1241. }
  1242. static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1243. {
  1244. long i;
  1245. for (i=0; i<num_pixels; i++)
  1246. ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | (src[(i<<1)+1] << 24);
  1247. }
  1248. static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1249. {
  1250. long i;
  1251. for (i=0; i<num_pixels; i++)
  1252. ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | src[(i<<1)+1];
  1253. }
  1254. static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1255. {
  1256. long i;
  1257. for (i=0; i<num_pixels; i++) {
  1258. //FIXME slow?
  1259. dst[0]= palette[src[i<<1]*4+0];
  1260. dst[1]= palette[src[i<<1]*4+1];
  1261. dst[2]= palette[src[i<<1]*4+2];
  1262. dst+= 3;
  1263. }
  1264. }
  1265. static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1266. int srcSliceH, uint8_t* dst[], int dstStride[])
  1267. {
  1268. const enum PixelFormat srcFormat= c->srcFormat;
  1269. const enum PixelFormat dstFormat= c->dstFormat;
  1270. void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
  1271. const uint8_t *palette)=NULL;
  1272. int i;
  1273. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1274. const uint8_t *srcPtr= src[0];
  1275. if (srcFormat == PIX_FMT_Y400A) {
  1276. switch (dstFormat) {
  1277. case PIX_FMT_RGB32 : conv = gray8aToPacked32; break;
  1278. case PIX_FMT_BGR32 : conv = gray8aToPacked32; break;
  1279. case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
  1280. case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
  1281. case PIX_FMT_RGB24 : conv = gray8aToPacked24; break;
  1282. case PIX_FMT_BGR24 : conv = gray8aToPacked24; break;
  1283. }
  1284. } else if (usePal(srcFormat)) {
  1285. switch (dstFormat) {
  1286. case PIX_FMT_RGB32 : conv = sws_convertPalette8ToPacked32; break;
  1287. case PIX_FMT_BGR32 : conv = sws_convertPalette8ToPacked32; break;
  1288. case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
  1289. case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
  1290. case PIX_FMT_RGB24 : conv = sws_convertPalette8ToPacked24; break;
  1291. case PIX_FMT_BGR24 : conv = sws_convertPalette8ToPacked24; break;
  1292. }
  1293. }
  1294. if (!conv)
  1295. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1296. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1297. else {
  1298. for (i=0; i<srcSliceH; i++) {
  1299. conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
  1300. srcPtr+= srcStride[0];
  1301. dstPtr+= dstStride[0];
  1302. }
  1303. }
  1304. return srcSliceH;
  1305. }
  1306. #define isRGBA32(x) ( \
  1307. (x) == PIX_FMT_ARGB \
  1308. || (x) == PIX_FMT_RGBA \
  1309. || (x) == PIX_FMT_BGRA \
  1310. || (x) == PIX_FMT_ABGR \
  1311. )
  1312. /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
  1313. static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1314. int srcSliceH, uint8_t* dst[], int dstStride[])
  1315. {
  1316. const enum PixelFormat srcFormat= c->srcFormat;
  1317. const enum PixelFormat dstFormat= c->dstFormat;
  1318. const int srcBpp= (c->srcFormatBpp + 7) >> 3;
  1319. const int dstBpp= (c->dstFormatBpp + 7) >> 3;
  1320. const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
  1321. const int dstId= c->dstFormatBpp >> 2;
  1322. void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
  1323. #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
  1324. if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
  1325. if ( CONV_IS(ABGR, RGBA)
  1326. || CONV_IS(ARGB, BGRA)
  1327. || CONV_IS(BGRA, ARGB)
  1328. || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
  1329. else if (CONV_IS(ABGR, ARGB)
  1330. || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
  1331. else if (CONV_IS(ABGR, BGRA)
  1332. || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
  1333. else if (CONV_IS(BGRA, RGBA)
  1334. || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
  1335. else if (CONV_IS(BGRA, ABGR)
  1336. || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
  1337. } else
  1338. /* BGR -> BGR */
  1339. if ( (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
  1340. || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
  1341. switch(srcId | (dstId<<4)) {
  1342. case 0x34: conv= rgb16to15; break;
  1343. case 0x36: conv= rgb24to15; break;
  1344. case 0x38: conv= rgb32to15; break;
  1345. case 0x43: conv= rgb15to16; break;
  1346. case 0x46: conv= rgb24to16; break;
  1347. case 0x48: conv= rgb32to16; break;
  1348. case 0x63: conv= rgb15to24; break;
  1349. case 0x64: conv= rgb16to24; break;
  1350. case 0x68: conv= rgb32to24; break;
  1351. case 0x83: conv= rgb15to32; break;
  1352. case 0x84: conv= rgb16to32; break;
  1353. case 0x86: conv= rgb24to32; break;
  1354. }
  1355. } else if ( (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
  1356. || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
  1357. switch(srcId | (dstId<<4)) {
  1358. case 0x33: conv= rgb15tobgr15; break;
  1359. case 0x34: conv= rgb16tobgr15; break;
  1360. case 0x36: conv= rgb24tobgr15; break;
  1361. case 0x38: conv= rgb32tobgr15; break;
  1362. case 0x43: conv= rgb15tobgr16; break;
  1363. case 0x44: conv= rgb16tobgr16; break;
  1364. case 0x46: conv= rgb24tobgr16; break;
  1365. case 0x48: conv= rgb32tobgr16; break;
  1366. case 0x63: conv= rgb15tobgr24; break;
  1367. case 0x64: conv= rgb16tobgr24; break;
  1368. case 0x66: conv= rgb24tobgr24; break;
  1369. case 0x68: conv= rgb32tobgr24; break;
  1370. case 0x83: conv= rgb15tobgr32; break;
  1371. case 0x84: conv= rgb16tobgr32; break;
  1372. case 0x86: conv= rgb24tobgr32; break;
  1373. }
  1374. }
  1375. if (!conv) {
  1376. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1377. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1378. } else {
  1379. const uint8_t *srcPtr= src[0];
  1380. uint8_t *dstPtr= dst[0];
  1381. if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
  1382. srcPtr += ALT32_CORR;
  1383. if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
  1384. dstPtr += ALT32_CORR;
  1385. if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
  1386. conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
  1387. else {
  1388. int i;
  1389. dstPtr += dstStride[0]*srcSliceY;
  1390. for (i=0; i<srcSliceH; i++) {
  1391. conv(srcPtr, dstPtr, c->srcW*srcBpp);
  1392. srcPtr+= srcStride[0];
  1393. dstPtr+= dstStride[0];
  1394. }
  1395. }
  1396. }
  1397. return srcSliceH;
  1398. }
  1399. static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1400. int srcSliceH, uint8_t* dst[], int dstStride[])
  1401. {
  1402. rgb24toyv12(
  1403. src[0],
  1404. dst[0]+ srcSliceY *dstStride[0],
  1405. dst[1]+(srcSliceY>>1)*dstStride[1],
  1406. dst[2]+(srcSliceY>>1)*dstStride[2],
  1407. c->srcW, srcSliceH,
  1408. dstStride[0], dstStride[1], srcStride[0]);
  1409. if (dst[3])
  1410. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1411. return srcSliceH;
  1412. }
  1413. static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1414. int srcSliceH, uint8_t* dst[], int dstStride[])
  1415. {
  1416. copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
  1417. dst[0], dstStride[0]);
  1418. planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
  1419. srcSliceH >> 2, srcStride[1], dstStride[1]);
  1420. planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
  1421. srcSliceH >> 2, srcStride[2], dstStride[2]);
  1422. if (dst[3])
  1423. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1424. return srcSliceH;
  1425. }
  1426. /* unscaled copy like stuff (assumes nearly identical formats) */
  1427. static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1428. int srcSliceH, uint8_t* dst[], int dstStride[])
  1429. {
  1430. if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
  1431. memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
  1432. else {
  1433. int i;
  1434. const uint8_t *srcPtr= src[0];
  1435. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1436. int length=0;
  1437. /* universal length finder */
  1438. while(length+c->srcW <= FFABS(dstStride[0])
  1439. && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
  1440. assert(length!=0);
  1441. for (i=0; i<srcSliceH; i++) {
  1442. memcpy(dstPtr, srcPtr, length);
  1443. srcPtr+= srcStride[0];
  1444. dstPtr+= dstStride[0];
  1445. }
  1446. }
  1447. return srcSliceH;
  1448. }
  1449. static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1450. int srcSliceH, uint8_t* dst[], int dstStride[])
  1451. {
  1452. int plane, i, j;
  1453. for (plane=0; plane<4; plane++) {
  1454. int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
  1455. int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
  1456. int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
  1457. const uint8_t *srcPtr= src[plane];
  1458. uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
  1459. if (!dst[plane]) continue;
  1460. // ignore palette for GRAY8
  1461. if (plane == 1 && !dst[2]) continue;
  1462. if (!src[plane] || (plane == 1 && !src[2])) {
  1463. if(is16BPS(c->dstFormat))
  1464. length*=2;
  1465. fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
  1466. } else {
  1467. if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
  1468. if (!isBE(c->srcFormat)) srcPtr++;
  1469. for (i=0; i<height; i++) {
  1470. for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
  1471. srcPtr+= srcStride[plane];
  1472. dstPtr+= dstStride[plane];
  1473. }
  1474. } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
  1475. for (i=0; i<height; i++) {
  1476. for (j=0; j<length; j++) {
  1477. dstPtr[ j<<1 ] = srcPtr[j];
  1478. dstPtr[(j<<1)+1] = srcPtr[j];
  1479. }
  1480. srcPtr+= srcStride[plane];
  1481. dstPtr+= dstStride[plane];
  1482. }
  1483. } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
  1484. && isBE(c->srcFormat) != isBE(c->dstFormat)) {
  1485. for (i=0; i<height; i++) {
  1486. for (j=0; j<length; j++)
  1487. ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
  1488. srcPtr+= srcStride[plane];
  1489. dstPtr+= dstStride[plane];
  1490. }
  1491. } else if (dstStride[plane] == srcStride[plane] &&
  1492. srcStride[plane] > 0 && srcStride[plane] == length) {
  1493. memcpy(dst[plane] + dstStride[plane]*y, src[plane],
  1494. height*dstStride[plane]);
  1495. } else {
  1496. if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
  1497. length*=2;
  1498. for (i=0; i<height; i++) {
  1499. memcpy(dstPtr, srcPtr, length);
  1500. srcPtr+= srcStride[plane];
  1501. dstPtr+= dstStride[plane];
  1502. }
  1503. }
  1504. }
  1505. }
  1506. return srcSliceH;
  1507. }
  1508. int ff_hardcodedcpuflags(void)
  1509. {
  1510. int flags = 0;
  1511. #if COMPILE_TEMPLATE_MMX2
  1512. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
  1513. #elif COMPILE_TEMPLATE_AMD3DNOW
  1514. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
  1515. #elif COMPILE_TEMPLATE_MMX
  1516. flags |= SWS_CPU_CAPS_MMX;
  1517. #elif COMPILE_TEMPLATE_ALTIVEC
  1518. flags |= SWS_CPU_CAPS_ALTIVEC;
  1519. #elif ARCH_BFIN
  1520. flags |= SWS_CPU_CAPS_BFIN;
  1521. #endif
  1522. return flags;
  1523. }
  1524. void ff_get_unscaled_swscale(SwsContext *c)
  1525. {
  1526. const enum PixelFormat srcFormat = c->srcFormat;
  1527. const enum PixelFormat dstFormat = c->dstFormat;
  1528. const int flags = c->flags;
  1529. const int dstH = c->dstH;
  1530. int needsDither;
  1531. needsDither= isAnyRGB(dstFormat)
  1532. && c->dstFormatBpp < 24
  1533. && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
  1534. /* yv12_to_nv12 */
  1535. if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
  1536. c->swScale= planarToNv12Wrapper;
  1537. }
  1538. /* yuv2bgr */
  1539. if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
  1540. && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
  1541. c->swScale= ff_yuv2rgb_get_func_ptr(c);
  1542. }
  1543. if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
  1544. c->swScale= yvu9ToYv12Wrapper;
  1545. }
  1546. /* bgr24toYV12 */
  1547. if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
  1548. c->swScale= bgr24ToYv12Wrapper;
  1549. /* RGB/BGR -> RGB/BGR (no dither needed forms) */
  1550. if ( isAnyRGB(srcFormat)
  1551. && isAnyRGB(dstFormat)
  1552. && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
  1553. && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
  1554. && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
  1555. && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
  1556. && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
  1557. && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
  1558. && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
  1559. && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
  1560. && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
  1561. && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
  1562. && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
  1563. c->swScale= rgbToRgbWrapper;
  1564. if ((usePal(srcFormat) && (
  1565. dstFormat == PIX_FMT_RGB32 ||
  1566. dstFormat == PIX_FMT_RGB32_1 ||
  1567. dstFormat == PIX_FMT_RGB24 ||
  1568. dstFormat == PIX_FMT_BGR32 ||
  1569. dstFormat == PIX_FMT_BGR32_1 ||
  1570. dstFormat == PIX_FMT_BGR24)))
  1571. c->swScale= palToRgbWrapper;
  1572. if (srcFormat == PIX_FMT_YUV422P) {
  1573. if (dstFormat == PIX_FMT_YUYV422)
  1574. c->swScale= yuv422pToYuy2Wrapper;
  1575. else if (dstFormat == PIX_FMT_UYVY422)
  1576. c->swScale= yuv422pToUyvyWrapper;
  1577. }
  1578. /* LQ converters if -sws 0 or -sws 4*/
  1579. if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
  1580. /* yv12_to_yuy2 */
  1581. if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
  1582. if (dstFormat == PIX_FMT_YUYV422)
  1583. c->swScale= planarToYuy2Wrapper;
  1584. else if (dstFormat == PIX_FMT_UYVY422)
  1585. c->swScale= planarToUyvyWrapper;
  1586. }
  1587. }
  1588. if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  1589. c->swScale= yuyvToYuv420Wrapper;
  1590. if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  1591. c->swScale= uyvyToYuv420Wrapper;
  1592. if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
  1593. c->swScale= yuyvToYuv422Wrapper;
  1594. if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
  1595. c->swScale= uyvyToYuv422Wrapper;
  1596. #if COMPILE_ALTIVEC
  1597. if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
  1598. !(c->flags & SWS_BITEXACT) &&
  1599. srcFormat == PIX_FMT_YUV420P) {
  1600. // unscaled YV12 -> packed YUV, we want speed
  1601. if (dstFormat == PIX_FMT_YUYV422)
  1602. c->swScale= yv12toyuy2_unscaled_altivec;
  1603. else if (dstFormat == PIX_FMT_UYVY422)
  1604. c->swScale= yv12touyvy_unscaled_altivec;
  1605. }
  1606. #endif
  1607. /* simple copy */
  1608. if ( srcFormat == dstFormat
  1609. || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
  1610. || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
  1611. || (isPlanarYUV(srcFormat) && isGray(dstFormat))
  1612. || (isPlanarYUV(dstFormat) && isGray(srcFormat))
  1613. || (isGray(dstFormat) && isGray(srcFormat))
  1614. || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
  1615. && c->chrDstHSubSample == c->chrSrcHSubSample
  1616. && c->chrDstVSubSample == c->chrSrcVSubSample
  1617. && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
  1618. && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
  1619. {
  1620. if (isPacked(c->srcFormat))
  1621. c->swScale= packedCopyWrapper;
  1622. else /* Planar YUV or gray */
  1623. c->swScale= planarCopyWrapper;
  1624. }
  1625. #if ARCH_BFIN
  1626. if (flags & SWS_CPU_CAPS_BFIN)
  1627. ff_bfin_get_unscaled_swscale (c);
  1628. #endif
  1629. }
  1630. static void reset_ptr(const uint8_t* src[], int format)
  1631. {
  1632. if(!isALPHA(format))
  1633. src[3]=NULL;
  1634. if(!isPlanarYUV(format)) {
  1635. src[3]=src[2]=NULL;
  1636. if (!usePal(format))
  1637. src[1]= NULL;
  1638. }
  1639. }
  1640. static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
  1641. const int linesizes[4])
  1642. {
  1643. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  1644. int i;
  1645. for (i = 0; i < 4; i++) {
  1646. int plane = desc->comp[i].plane;
  1647. if (!data[plane] || !linesizes[plane])
  1648. return 0;
  1649. }
  1650. return 1;
  1651. }
  1652. /**
  1653. * swscale wrapper, so we don't need to export the SwsContext.
  1654. * Assumes planar YUV to be in YUV order instead of YVU.
  1655. */
  1656. int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
  1657. int srcSliceH, uint8_t* const dst[], const int dstStride[])
  1658. {
  1659. int i;
  1660. const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
  1661. uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
  1662. // do not mess up sliceDir if we have a "trailing" 0-size slice
  1663. if (srcSliceH == 0)
  1664. return 0;
  1665. if (!check_image_pointers(src, c->srcFormat, srcStride)) {
  1666. av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
  1667. return 0;
  1668. }
  1669. if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
  1670. av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
  1671. return 0;
  1672. }
  1673. if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
  1674. av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
  1675. return 0;
  1676. }
  1677. if (c->sliceDir == 0) {
  1678. if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
  1679. }
  1680. if (usePal(c->srcFormat)) {
  1681. for (i=0; i<256; i++) {
  1682. int p, r, g, b,y,u,v;
  1683. if(c->srcFormat == PIX_FMT_PAL8) {
  1684. p=((const uint32_t*)(src[1]))[i];
  1685. r= (p>>16)&0xFF;
  1686. g= (p>> 8)&0xFF;
  1687. b= p &0xFF;
  1688. } else if(c->srcFormat == PIX_FMT_RGB8) {
  1689. r= (i>>5 )*36;
  1690. g= ((i>>2)&7)*36;
  1691. b= (i&3 )*85;
  1692. } else if(c->srcFormat == PIX_FMT_BGR8) {
  1693. b= (i>>6 )*85;
  1694. g= ((i>>3)&7)*36;
  1695. r= (i&7 )*36;
  1696. } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
  1697. r= (i>>3 )*255;
  1698. g= ((i>>1)&3)*85;
  1699. b= (i&1 )*255;
  1700. } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_Y400A) {
  1701. r = g = b = i;
  1702. } else {
  1703. assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
  1704. b= (i>>3 )*255;
  1705. g= ((i>>1)&3)*85;
  1706. r= (i&1 )*255;
  1707. }
  1708. y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1709. u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1710. v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1711. c->pal_yuv[i]= y + (u<<8) + (v<<16);
  1712. switch(c->dstFormat) {
  1713. case PIX_FMT_BGR32:
  1714. #if !HAVE_BIGENDIAN
  1715. case PIX_FMT_RGB24:
  1716. #endif
  1717. c->pal_rgb[i]= r + (g<<8) + (b<<16);
  1718. break;
  1719. case PIX_FMT_BGR32_1:
  1720. #if HAVE_BIGENDIAN
  1721. case PIX_FMT_BGR24:
  1722. #endif
  1723. c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
  1724. break;
  1725. case PIX_FMT_RGB32_1:
  1726. #if HAVE_BIGENDIAN
  1727. case PIX_FMT_RGB24:
  1728. #endif
  1729. c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
  1730. break;
  1731. case PIX_FMT_RGB32:
  1732. #if !HAVE_BIGENDIAN
  1733. case PIX_FMT_BGR24:
  1734. #endif
  1735. default:
  1736. c->pal_rgb[i]= b + (g<<8) + (r<<16);
  1737. }
  1738. }
  1739. }
  1740. // copy strides, so they can safely be modified
  1741. if (c->sliceDir == 1) {
  1742. // slices go from top to bottom
  1743. int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
  1744. int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
  1745. reset_ptr(src2, c->srcFormat);
  1746. reset_ptr((const uint8_t**)dst2, c->dstFormat);
  1747. /* reset slice direction at end of frame */
  1748. if (srcSliceY + srcSliceH == c->srcH)
  1749. c->sliceDir = 0;
  1750. return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
  1751. } else {
  1752. // slices go from bottom to top => we flip the image internally
  1753. int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
  1754. int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
  1755. src2[0] += (srcSliceH-1)*srcStride[0];
  1756. if (!usePal(c->srcFormat))
  1757. src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
  1758. src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
  1759. src2[3] += (srcSliceH-1)*srcStride[3];
  1760. dst2[0] += ( c->dstH -1)*dstStride[0];
  1761. dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
  1762. dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
  1763. dst2[3] += ( c->dstH -1)*dstStride[3];
  1764. reset_ptr(src2, c->srcFormat);
  1765. reset_ptr((const uint8_t**)dst2, c->dstFormat);
  1766. /* reset slice direction at end of frame */
  1767. if (!srcSliceY)
  1768. c->sliceDir = 0;
  1769. return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
  1770. }
  1771. }
  1772. /* Convert the palette to the same packed 32-bit format as the palette */
  1773. void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1774. {
  1775. long i;
  1776. for (i=0; i<num_pixels; i++)
  1777. ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
  1778. }
  1779. /* Palette format: ABCD -> dst format: ABC */
  1780. void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1781. {
  1782. long i;
  1783. for (i=0; i<num_pixels; i++) {
  1784. //FIXME slow?
  1785. dst[0]= palette[src[i]*4+0];
  1786. dst[1]= palette[src[i]*4+1];
  1787. dst[2]= palette[src[i]*4+2];
  1788. dst+= 3;
  1789. }
  1790. }