swscale.c 77 KB

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