swscale.c 68 KB

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