swscale.c 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948
  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/cpu.h"
  61. #include "libavutil/avutil.h"
  62. #include "libavutil/mathematics.h"
  63. #include "libavutil/bswap.h"
  64. #include "libavutil/pixdesc.h"
  65. #define RGB2YUV_SHIFT 15
  66. #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  67. #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  68. #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  69. #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  70. #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  71. #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  72. #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  73. #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  74. #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  75. /*
  76. NOTES
  77. Special versions: fast Y 1:1 scaling (no interpolation in y direction)
  78. TODO
  79. more intelligent misalignment avoidance for the horizontal scaler
  80. write special vertical cubic upscale version
  81. optimize C code (YV12 / minmax)
  82. add support for packed pixel YUV input & output
  83. add support for Y8 output
  84. optimize BGR24 & BGR32
  85. add BGR4 output support
  86. write special BGR->BGR scaler
  87. */
  88. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
  89. { 1, 3, 1, 3, 1, 3, 1, 3, },
  90. { 2, 0, 2, 0, 2, 0, 2, 0, },
  91. };
  92. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
  93. { 6, 2, 6, 2, 6, 2, 6, 2, },
  94. { 0, 4, 0, 4, 0, 4, 0, 4, },
  95. };
  96. DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[4][8]={
  97. { 8, 4, 11, 7, 8, 4, 11, 7, },
  98. { 2, 14, 1, 13, 2, 14, 1, 13, },
  99. { 10, 6, 9, 5, 10, 6, 9, 5, },
  100. { 0, 12, 3, 15, 0, 12, 3, 15, },
  101. };
  102. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
  103. { 17, 9, 23, 15, 16, 8, 22, 14, },
  104. { 5, 29, 3, 27, 4, 28, 2, 26, },
  105. { 21, 13, 19, 11, 20, 12, 18, 10, },
  106. { 0, 24, 6, 30, 1, 25, 7, 31, },
  107. { 16, 8, 22, 14, 17, 9, 23, 15, },
  108. { 4, 28, 2, 26, 5, 29, 3, 27, },
  109. { 20, 12, 18, 10, 21, 13, 19, 11, },
  110. { 1, 25, 7, 31, 0, 24, 6, 30, },
  111. };
  112. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
  113. { 0, 55, 14, 68, 3, 58, 17, 72, },
  114. { 37, 18, 50, 32, 40, 22, 54, 35, },
  115. { 9, 64, 5, 59, 13, 67, 8, 63, },
  116. { 46, 27, 41, 23, 49, 31, 44, 26, },
  117. { 2, 57, 16, 71, 1, 56, 15, 70, },
  118. { 39, 21, 52, 34, 38, 19, 51, 33, },
  119. { 11, 66, 7, 62, 10, 65, 6, 60, },
  120. { 48, 30, 43, 25, 47, 29, 42, 24, },
  121. };
  122. #if 1
  123. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  124. {117, 62, 158, 103, 113, 58, 155, 100, },
  125. { 34, 199, 21, 186, 31, 196, 17, 182, },
  126. {144, 89, 131, 76, 141, 86, 127, 72, },
  127. { 0, 165, 41, 206, 10, 175, 52, 217, },
  128. {110, 55, 151, 96, 120, 65, 162, 107, },
  129. { 28, 193, 14, 179, 38, 203, 24, 189, },
  130. {138, 83, 124, 69, 148, 93, 134, 79, },
  131. { 7, 172, 48, 213, 3, 168, 45, 210, },
  132. };
  133. #elif 1
  134. // tries to correct a gamma of 1.5
  135. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  136. { 0, 143, 18, 200, 2, 156, 25, 215, },
  137. { 78, 28, 125, 64, 89, 36, 138, 74, },
  138. { 10, 180, 3, 161, 16, 195, 8, 175, },
  139. {109, 51, 93, 38, 121, 60, 105, 47, },
  140. { 1, 152, 23, 210, 0, 147, 20, 205, },
  141. { 85, 33, 134, 71, 81, 30, 130, 67, },
  142. { 14, 190, 6, 171, 12, 185, 5, 166, },
  143. {117, 57, 101, 44, 113, 54, 97, 41, },
  144. };
  145. #elif 1
  146. // tries to correct a gamma of 2.0
  147. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  148. { 0, 124, 8, 193, 0, 140, 12, 213, },
  149. { 55, 14, 104, 42, 66, 19, 119, 52, },
  150. { 3, 168, 1, 145, 6, 187, 3, 162, },
  151. { 86, 31, 70, 21, 99, 39, 82, 28, },
  152. { 0, 134, 11, 206, 0, 129, 9, 200, },
  153. { 62, 17, 114, 48, 58, 16, 109, 45, },
  154. { 5, 181, 2, 157, 4, 175, 1, 151, },
  155. { 95, 36, 78, 26, 90, 34, 74, 24, },
  156. };
  157. #else
  158. // tries to correct a gamma of 2.5
  159. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  160. { 0, 107, 3, 187, 0, 125, 6, 212, },
  161. { 39, 7, 86, 28, 49, 11, 102, 36, },
  162. { 1, 158, 0, 131, 3, 180, 1, 151, },
  163. { 68, 19, 52, 12, 81, 25, 64, 17, },
  164. { 0, 119, 5, 203, 0, 113, 4, 195, },
  165. { 45, 9, 96, 33, 42, 8, 91, 30, },
  166. { 2, 172, 1, 144, 2, 165, 0, 137, },
  167. { 77, 23, 60, 15, 72, 21, 56, 14, },
  168. };
  169. #endif
  170. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_128)[8][8] = {
  171. { 36, 68, 60, 92, 34, 66, 58, 90,},
  172. { 100, 4,124, 28, 98, 2,122, 26,},
  173. { 52, 84, 44, 76, 50, 82, 42, 74,},
  174. { 116, 20,108, 12,114, 18,106, 10,},
  175. { 32, 64, 56, 88, 38, 70, 62, 94,},
  176. { 96, 0,120, 24,102, 6,126, 30,},
  177. { 48, 80, 40, 72, 54, 86, 46, 78,},
  178. { 112, 16,104, 8,118, 22,110, 14,},
  179. };
  180. DECLARE_ALIGNED(8, const uint8_t, ff_sws_pb_64)[8] =
  181. { 64, 64, 64, 64, 64, 64, 64, 64 };
  182. DECLARE_ALIGNED(8, const uint8_t, dithers)[8][8][8]={
  183. {
  184. { 0, 1, 0, 1, 0, 1, 0, 1,},
  185. { 1, 0, 1, 0, 1, 0, 1, 0,},
  186. { 0, 1, 0, 1, 0, 1, 0, 1,},
  187. { 1, 0, 1, 0, 1, 0, 1, 0,},
  188. { 0, 1, 0, 1, 0, 1, 0, 1,},
  189. { 1, 0, 1, 0, 1, 0, 1, 0,},
  190. { 0, 1, 0, 1, 0, 1, 0, 1,},
  191. { 1, 0, 1, 0, 1, 0, 1, 0,},
  192. },{
  193. { 1, 2, 1, 2, 1, 2, 1, 2,},
  194. { 3, 0, 3, 0, 3, 0, 3, 0,},
  195. { 1, 2, 1, 2, 1, 2, 1, 2,},
  196. { 3, 0, 3, 0, 3, 0, 3, 0,},
  197. { 1, 2, 1, 2, 1, 2, 1, 2,},
  198. { 3, 0, 3, 0, 3, 0, 3, 0,},
  199. { 1, 2, 1, 2, 1, 2, 1, 2,},
  200. { 3, 0, 3, 0, 3, 0, 3, 0,},
  201. },{
  202. { 2, 4, 3, 5, 2, 4, 3, 5,},
  203. { 6, 0, 7, 1, 6, 0, 7, 1,},
  204. { 3, 5, 2, 4, 3, 5, 2, 4,},
  205. { 7, 1, 6, 0, 7, 1, 6, 0,},
  206. { 2, 4, 3, 5, 2, 4, 3, 5,},
  207. { 6, 0, 7, 1, 6, 0, 7, 1,},
  208. { 3, 5, 2, 4, 3, 5, 2, 4,},
  209. { 7, 1, 6, 0, 7, 1, 6, 0,},
  210. },{
  211. { 4, 8, 7, 11, 4, 8, 7, 11,},
  212. { 12, 0, 15, 3, 12, 0, 15, 3,},
  213. { 6, 10, 5, 9, 6, 10, 5, 9,},
  214. { 14, 2, 13, 1, 14, 2, 13, 1,},
  215. { 4, 8, 7, 11, 4, 8, 7, 11,},
  216. { 12, 0, 15, 3, 12, 0, 15, 3,},
  217. { 6, 10, 5, 9, 6, 10, 5, 9,},
  218. { 14, 2, 13, 1, 14, 2, 13, 1,},
  219. },{
  220. { 9, 17, 15, 23, 8, 16, 14, 22,},
  221. { 25, 1, 31, 7, 24, 0, 30, 6,},
  222. { 13, 21, 11, 19, 12, 20, 10, 18,},
  223. { 29, 5, 27, 3, 28, 4, 26, 2,},
  224. { 8, 16, 14, 22, 9, 17, 15, 23,},
  225. { 24, 0, 30, 6, 25, 1, 31, 7,},
  226. { 12, 20, 10, 18, 13, 21, 11, 19,},
  227. { 28, 4, 26, 2, 29, 5, 27, 3,},
  228. },{
  229. { 18, 34, 30, 46, 17, 33, 29, 45,},
  230. { 50, 2, 62, 14, 49, 1, 61, 13,},
  231. { 26, 42, 22, 38, 25, 41, 21, 37,},
  232. { 58, 10, 54, 6, 57, 9, 53, 5,},
  233. { 16, 32, 28, 44, 19, 35, 31, 47,},
  234. { 48, 0, 60, 12, 51, 3, 63, 15,},
  235. { 24, 40, 20, 36, 27, 43, 23, 39,},
  236. { 56, 8, 52, 4, 59, 11, 55, 7,},
  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. { 36, 68, 60, 92, 34, 66, 58, 90,},
  248. { 100, 4,124, 28, 98, 2,122, 26,},
  249. { 52, 84, 44, 76, 50, 82, 42, 74,},
  250. { 116, 20,108, 12,114, 18,106, 10,},
  251. { 32, 64, 56, 88, 38, 70, 62, 94,},
  252. { 96, 0,120, 24,102, 6,126, 30,},
  253. { 48, 80, 40, 72, 54, 86, 46, 78,},
  254. { 112, 16,104, 8,118, 22,110, 14,},
  255. }};
  256. static const uint8_t flat64[8]={64,64,64,64,64,64,64,64};
  257. const uint16_t dither_scale[15][16]={
  258. { 2, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,},
  259. { 2, 3, 7, 7, 13, 13, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,},
  260. { 3, 3, 4, 15, 15, 29, 57, 57, 57, 113, 113, 113, 113, 113, 113, 113,},
  261. { 3, 4, 4, 5, 31, 31, 61, 121, 241, 241, 241, 241, 481, 481, 481, 481,},
  262. { 3, 4, 5, 5, 6, 63, 63, 125, 249, 497, 993, 993, 993, 993, 993, 1985,},
  263. { 3, 5, 6, 6, 6, 7, 127, 127, 253, 505, 1009, 2017, 4033, 4033, 4033, 4033,},
  264. { 3, 5, 6, 7, 7, 7, 8, 255, 255, 509, 1017, 2033, 4065, 8129,16257,16257,},
  265. { 3, 5, 6, 8, 8, 8, 8, 9, 511, 511, 1021, 2041, 4081, 8161,16321,32641,},
  266. { 3, 5, 7, 8, 9, 9, 9, 9, 10, 1023, 1023, 2045, 4089, 8177,16353,32705,},
  267. { 3, 5, 7, 8, 10, 10, 10, 10, 10, 11, 2047, 2047, 4093, 8185,16369,32737,},
  268. { 3, 5, 7, 8, 10, 11, 11, 11, 11, 11, 12, 4095, 4095, 8189,16377,32753,},
  269. { 3, 5, 7, 9, 10, 12, 12, 12, 12, 12, 12, 13, 8191, 8191,16381,32761,},
  270. { 3, 5, 7, 9, 10, 12, 13, 13, 13, 13, 13, 13, 14,16383,16383,32765,},
  271. { 3, 5, 7, 9, 10, 12, 14, 14, 14, 14, 14, 14, 14, 15,32767,32767,},
  272. { 3, 5, 7, 9, 11, 12, 14, 15, 15, 15, 15, 15, 15, 15, 16,65535,},
  273. };
  274. static av_always_inline void
  275. yuv2yuvX16_c_template(const int16_t *lumFilter, const int32_t **lumSrc,
  276. int lumFilterSize, const int16_t *chrFilter,
  277. const int32_t **chrUSrc, const int32_t **chrVSrc,
  278. int chrFilterSize, const int32_t **alpSrc,
  279. uint16_t *dest[4], int dstW, int chrDstW,
  280. int big_endian, int output_bits)
  281. {
  282. //FIXME Optimize (just quickly written not optimized..)
  283. int i;
  284. int dword= output_bits == 16;
  285. uint16_t *yDest = dest[0], *uDest = dest[1], *vDest = dest[2],
  286. *aDest = CONFIG_SWSCALE_ALPHA ? dest[3] : NULL;
  287. int shift = 11 + 4*dword + 16 - output_bits - 1;
  288. #define output_pixel(pos, val) \
  289. if (big_endian) { \
  290. if (output_bits == 16) { \
  291. AV_WB16(pos, av_clip_uint16(val >> shift)); \
  292. } else { \
  293. AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  294. } \
  295. } else { \
  296. if (output_bits == 16) { \
  297. AV_WL16(pos, av_clip_uint16(val >> shift)); \
  298. } else { \
  299. AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  300. } \
  301. }
  302. for (i = 0; i < dstW; i++) {
  303. int val = 1 << (26-output_bits + 4*dword - 1);
  304. int j;
  305. for (j = 0; j < lumFilterSize; j++)
  306. val += ((dword ? lumSrc[j][i] : ((int16_t**)lumSrc)[j][i]) * lumFilter[j])>>1;
  307. output_pixel(&yDest[i], val);
  308. }
  309. if (uDest) {
  310. for (i = 0; i < chrDstW; i++) {
  311. int u = 1 << (26-output_bits + 4*dword - 1);
  312. int v = 1 << (26-output_bits + 4*dword - 1);
  313. int j;
  314. for (j = 0; j < chrFilterSize; j++) {
  315. u += ((dword ? chrUSrc[j][i] : ((int16_t**)chrUSrc)[j][i]) * chrFilter[j]) >> 1;
  316. v += ((dword ? chrVSrc[j][i] : ((int16_t**)chrVSrc)[j][i]) * chrFilter[j]) >> 1;
  317. }
  318. output_pixel(&uDest[i], u);
  319. output_pixel(&vDest[i], v);
  320. }
  321. }
  322. if (CONFIG_SWSCALE_ALPHA && aDest) {
  323. for (i = 0; i < dstW; i++) {
  324. int val = 1 << (26-output_bits + 4*dword - 1);
  325. int j;
  326. for (j = 0; j < lumFilterSize; j++)
  327. val += ((dword ? alpSrc[j][i] : ((int16_t**)alpSrc)[j][i]) * lumFilter[j]) >> 1;
  328. output_pixel(&aDest[i], val);
  329. }
  330. }
  331. #undef output_pixel
  332. }
  333. #define yuv2NBPS(bits, BE_LE, is_be) \
  334. static void yuv2yuvX ## bits ## BE_LE ## _c(SwsContext *c, const int16_t *lumFilter, \
  335. const int16_t **_lumSrc, int lumFilterSize, \
  336. const int16_t *chrFilter, const int16_t **_chrUSrc, \
  337. const int16_t **_chrVSrc, \
  338. int chrFilterSize, const int16_t **_alpSrc, \
  339. uint8_t *_dest[4], int dstW, int chrDstW) \
  340. { \
  341. const int32_t **lumSrc = (const int32_t **) _lumSrc, \
  342. **chrUSrc = (const int32_t **) _chrUSrc, \
  343. **chrVSrc = (const int32_t **) _chrVSrc, \
  344. **alpSrc = (const int32_t **) _alpSrc; \
  345. yuv2yuvX16_c_template(lumFilter, lumSrc, lumFilterSize, \
  346. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  347. alpSrc, (uint16_t **) _dest, \
  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 void yuv2yuvX_c(SwsContext *c, const int16_t *lumFilter,
  357. const int16_t **lumSrc, int lumFilterSize,
  358. const int16_t *chrFilter, const int16_t **chrUSrc,
  359. const int16_t **chrVSrc,
  360. int chrFilterSize, const int16_t **alpSrc,
  361. uint8_t *dest[4], int dstW, int chrDstW)
  362. {
  363. uint8_t *yDest = dest[0], *uDest = dest[1], *vDest = dest[2],
  364. *aDest = CONFIG_SWSCALE_ALPHA ? dest[3] : NULL;
  365. int i;
  366. const uint8_t *lumDither = c->lumDither8, *chrDither = c->chrDither8;
  367. //FIXME Optimize (just quickly written not optimized..)
  368. for (i=0; i<dstW; i++) {
  369. int val = lumDither[i & 7] << 12;
  370. int j;
  371. for (j=0; j<lumFilterSize; j++)
  372. val += lumSrc[j][i] * lumFilter[j];
  373. yDest[i]= av_clip_uint8(val>>19);
  374. }
  375. if (uDest)
  376. for (i=0; i<chrDstW; i++) {
  377. int u = chrDither[i & 7] << 12;
  378. int v = chrDither[(i + 3) & 7] << 12;
  379. int j;
  380. for (j=0; j<chrFilterSize; j++) {
  381. u += chrUSrc[j][i] * chrFilter[j];
  382. v += chrVSrc[j][i] * chrFilter[j];
  383. }
  384. uDest[i]= av_clip_uint8(u>>19);
  385. vDest[i]= av_clip_uint8(v>>19);
  386. }
  387. if (CONFIG_SWSCALE_ALPHA && aDest)
  388. for (i=0; i<dstW; i++) {
  389. int val = lumDither[i & 7] << 12;
  390. int j;
  391. for (j=0; j<lumFilterSize; j++)
  392. val += alpSrc[j][i] * lumFilter[j];
  393. aDest[i]= av_clip_uint8(val>>19);
  394. }
  395. }
  396. static void yuv2yuv1_c(SwsContext *c, const int16_t *lumSrc,
  397. const int16_t *chrUSrc, const int16_t *chrVSrc,
  398. const int16_t *alpSrc,
  399. uint8_t *dest[4], int dstW, int chrDstW)
  400. {
  401. uint8_t *yDest = dest[0], *uDest = dest[1], *vDest = dest[2],
  402. *aDest = CONFIG_SWSCALE_ALPHA ? dest[3] : NULL;
  403. int i;
  404. const uint8_t *lumDither = c->lumDither8, *chrDither = c->chrDither8;
  405. for (i=0; i<dstW; i++) {
  406. int val = (lumSrc[i]+ lumDither[i & 7]) >> 7;
  407. yDest[i]= av_clip_uint8(val);
  408. }
  409. if (uDest)
  410. for (i=0; i<chrDstW; i++) {
  411. int u = (chrUSrc[i] + chrDither[i & 7]) >> 7;
  412. int v = (chrVSrc[i] + chrDither[(i + 3) & 7]) >> 7;
  413. uDest[i]= av_clip_uint8(u);
  414. vDest[i]= av_clip_uint8(v);
  415. }
  416. if (CONFIG_SWSCALE_ALPHA && aDest)
  417. for (i=0; i<dstW; i++) {
  418. int val = (alpSrc[i] + lumDither[i & 7]) >> 7;
  419. aDest[i]= av_clip_uint8(val);
  420. }
  421. }
  422. static void yuv2nv12X_c(SwsContext *c, const int16_t *lumFilter,
  423. const int16_t **lumSrc, int lumFilterSize,
  424. const int16_t *chrFilter, const int16_t **chrUSrc,
  425. const int16_t **chrVSrc, int chrFilterSize,
  426. const int16_t **alpSrc, uint8_t *dest[4],
  427. int dstW, int chrDstW)
  428. {
  429. uint8_t *yDest = dest[0], *uDest = dest[1];
  430. enum PixelFormat dstFormat = c->dstFormat;
  431. const uint8_t *lumDither = c->lumDither8, *chrDither = c->chrDither8;
  432. //FIXME Optimize (just quickly written not optimized..)
  433. int i;
  434. for (i=0; i<dstW; i++) {
  435. int val = lumDither[i & 7] << 12;
  436. int j;
  437. for (j=0; j<lumFilterSize; j++)
  438. val += lumSrc[j][i] * lumFilter[j];
  439. yDest[i]= av_clip_uint8(val>>19);
  440. }
  441. if (!uDest)
  442. return;
  443. if (dstFormat == PIX_FMT_NV12)
  444. for (i=0; i<chrDstW; i++) {
  445. int u = chrDither[i & 7] << 12;
  446. int v = chrDither[(i + 3) & 7] << 12;
  447. int j;
  448. for (j=0; j<chrFilterSize; j++) {
  449. u += chrUSrc[j][i] * chrFilter[j];
  450. v += chrVSrc[j][i] * chrFilter[j];
  451. }
  452. uDest[2*i]= av_clip_uint8(u>>19);
  453. uDest[2*i+1]= av_clip_uint8(v>>19);
  454. }
  455. else
  456. for (i=0; i<chrDstW; i++) {
  457. int u = chrDither[i & 7] << 12;
  458. int v = chrDither[(i + 3) & 7] << 12;
  459. int j;
  460. for (j=0; j<chrFilterSize; j++) {
  461. u += chrUSrc[j][i] * chrFilter[j];
  462. v += chrVSrc[j][i] * chrFilter[j];
  463. }
  464. uDest[2*i]= av_clip_uint8(v>>19);
  465. uDest[2*i+1]= av_clip_uint8(u>>19);
  466. }
  467. }
  468. #define output_pixel(pos, val) \
  469. if (target == PIX_FMT_GRAY16BE) { \
  470. AV_WB16(pos, val); \
  471. } else { \
  472. AV_WL16(pos, val); \
  473. }
  474. static av_always_inline void
  475. yuv2gray16_X_c_template(SwsContext *c, const int16_t *lumFilter,
  476. const int32_t **lumSrc, int lumFilterSize,
  477. const int16_t *chrFilter, const int32_t **chrUSrc,
  478. const int32_t **chrVSrc, int chrFilterSize,
  479. const int32_t **alpSrc, uint16_t *dest, int dstW,
  480. int y, enum PixelFormat target)
  481. {
  482. int i;
  483. for (i = 0; i < (dstW >> 1); i++) {
  484. int j;
  485. int Y1 = 1 << 14;
  486. int Y2 = 1 << 14;
  487. for (j = 0; j < lumFilterSize; j++) {
  488. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  489. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  490. }
  491. Y1 >>= 15;
  492. Y2 >>= 15;
  493. if ((Y1 | Y2) & 0x10000) {
  494. Y1 = av_clip_uint16(Y1);
  495. Y2 = av_clip_uint16(Y2);
  496. }
  497. output_pixel(&dest[i * 2 + 0], Y1);
  498. output_pixel(&dest[i * 2 + 1], Y2);
  499. }
  500. }
  501. static av_always_inline void
  502. yuv2gray16_2_c_template(SwsContext *c, const int32_t *buf[2],
  503. const int32_t *ubuf[2], const int32_t *vbuf[2],
  504. const int32_t *abuf[2], uint16_t *dest, int dstW,
  505. int yalpha, int uvalpha, int y,
  506. enum PixelFormat target)
  507. {
  508. int yalpha1 = 4095 - yalpha;
  509. int i;
  510. const int32_t *buf0 = buf[0], *buf1 = buf[1];
  511. for (i = 0; i < (dstW >> 1); i++) {
  512. int Y1 = (buf0[i * 2 ] * yalpha1 + buf1[i * 2 ] * yalpha) >> 15;
  513. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 15;
  514. output_pixel(&dest[i * 2 + 0], Y1);
  515. output_pixel(&dest[i * 2 + 1], Y2);
  516. }
  517. }
  518. static av_always_inline void
  519. yuv2gray16_1_c_template(SwsContext *c, const int32_t *buf0,
  520. const int32_t *ubuf[2], const int32_t *vbuf[2],
  521. const int32_t *abuf0, uint16_t *dest, int dstW,
  522. int uvalpha, int y, enum PixelFormat target)
  523. {
  524. int i;
  525. for (i = 0; i < (dstW >> 1); i++) {
  526. int Y1 = (buf0[i * 2 ]+4)>>3;
  527. int Y2 = (buf0[i * 2 + 1]+4)>>3;
  528. output_pixel(&dest[i * 2 + 0], Y1);
  529. output_pixel(&dest[i * 2 + 1], Y2);
  530. }
  531. }
  532. #undef output_pixel
  533. #define YUV2PACKED16WRAPPER(name, base, ext, fmt) \
  534. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  535. const int16_t **_lumSrc, int lumFilterSize, \
  536. const int16_t *chrFilter, const int16_t **_chrUSrc, \
  537. const int16_t **_chrVSrc, int chrFilterSize, \
  538. const int16_t **_alpSrc, uint8_t *_dest, int dstW, \
  539. int y) \
  540. { \
  541. const int32_t **lumSrc = (const int32_t **) _lumSrc, \
  542. **chrUSrc = (const int32_t **) _chrUSrc, \
  543. **chrVSrc = (const int32_t **) _chrVSrc, \
  544. **alpSrc = (const int32_t **) _alpSrc; \
  545. uint16_t *dest = (uint16_t *) _dest; \
  546. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  547. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  548. alpSrc, dest, dstW, y, fmt); \
  549. } \
  550. \
  551. static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
  552. const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
  553. const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
  554. int yalpha, int uvalpha, int y) \
  555. { \
  556. const int32_t **buf = (const int32_t **) _buf, \
  557. **ubuf = (const int32_t **) _ubuf, \
  558. **vbuf = (const int32_t **) _vbuf, \
  559. **abuf = (const int32_t **) _abuf; \
  560. uint16_t *dest = (uint16_t *) _dest; \
  561. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  562. dest, dstW, yalpha, uvalpha, y, fmt); \
  563. } \
  564. \
  565. static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
  566. const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
  567. const int16_t *_abuf0, uint8_t *_dest, int dstW, \
  568. int uvalpha, int y) \
  569. { \
  570. const int32_t *buf0 = (const int32_t *) _buf0, \
  571. **ubuf = (const int32_t **) _ubuf, \
  572. **vbuf = (const int32_t **) _vbuf, \
  573. *abuf0 = (const int32_t *) _abuf0; \
  574. uint16_t *dest = (uint16_t *) _dest; \
  575. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
  576. dstW, uvalpha, y, fmt); \
  577. }
  578. YUV2PACKED16WRAPPER(yuv2gray16,, LE, PIX_FMT_GRAY16LE);
  579. YUV2PACKED16WRAPPER(yuv2gray16,, BE, PIX_FMT_GRAY16BE);
  580. #define output_pixel(pos, acc) \
  581. if (target == PIX_FMT_MONOBLACK) { \
  582. pos = acc; \
  583. } else { \
  584. pos = ~acc; \
  585. }
  586. static av_always_inline void
  587. yuv2mono_X_c_template(SwsContext *c, const int16_t *lumFilter,
  588. const int16_t **lumSrc, int lumFilterSize,
  589. const int16_t *chrFilter, const int16_t **chrUSrc,
  590. const int16_t **chrVSrc, int chrFilterSize,
  591. const int16_t **alpSrc, uint8_t *dest, int dstW,
  592. int y, enum PixelFormat target)
  593. {
  594. const uint8_t * const d128=dither_8x8_220[y&7];
  595. uint8_t *g = c->table_gU[128] + c->table_gV[128];
  596. int i;
  597. int acc = 0;
  598. for (i = 0; i < dstW - 1; i += 2) {
  599. int j;
  600. int Y1 = 1 << 18;
  601. int Y2 = 1 << 18;
  602. for (j = 0; j < lumFilterSize; j++) {
  603. Y1 += lumSrc[j][i] * lumFilter[j];
  604. Y2 += lumSrc[j][i+1] * lumFilter[j];
  605. }
  606. Y1 >>= 19;
  607. Y2 >>= 19;
  608. if ((Y1 | Y2) & 0x100) {
  609. Y1 = av_clip_uint8(Y1);
  610. Y2 = av_clip_uint8(Y2);
  611. }
  612. acc += acc + g[Y1 + d128[(i + 0) & 7]];
  613. acc += acc + g[Y2 + d128[(i + 1) & 7]];
  614. if ((i & 7) == 6) {
  615. output_pixel(*dest++, acc);
  616. }
  617. }
  618. }
  619. static av_always_inline void
  620. yuv2mono_2_c_template(SwsContext *c, const int16_t *buf[2],
  621. const int16_t *ubuf[2], const int16_t *vbuf[2],
  622. const int16_t *abuf[2], uint8_t *dest, int dstW,
  623. int yalpha, int uvalpha, int y,
  624. enum PixelFormat target)
  625. {
  626. const int16_t *buf0 = buf[0], *buf1 = buf[1];
  627. const uint8_t * const d128 = dither_8x8_220[y & 7];
  628. uint8_t *g = c->table_gU[128] + c->table_gV[128];
  629. int yalpha1 = 4095 - yalpha;
  630. int i;
  631. for (i = 0; i < dstW - 7; i += 8) {
  632. int acc = g[((buf0[i ] * yalpha1 + buf1[i ] * yalpha) >> 19) + d128[0]];
  633. acc += acc + g[((buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19) + d128[1]];
  634. acc += acc + g[((buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19) + d128[2]];
  635. acc += acc + g[((buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19) + d128[3]];
  636. acc += acc + g[((buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19) + d128[4]];
  637. acc += acc + g[((buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19) + d128[5]];
  638. acc += acc + g[((buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19) + d128[6]];
  639. acc += acc + g[((buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19) + d128[7]];
  640. output_pixel(*dest++, acc);
  641. }
  642. }
  643. static av_always_inline void
  644. yuv2mono_1_c_template(SwsContext *c, const int16_t *buf0,
  645. const int16_t *ubuf[2], const int16_t *vbuf[2],
  646. const int16_t *abuf0, uint8_t *dest, int dstW,
  647. int uvalpha, int y, enum PixelFormat target)
  648. {
  649. const uint8_t * const d128 = dither_8x8_220[y & 7];
  650. uint8_t *g = c->table_gU[128] + c->table_gV[128];
  651. int i;
  652. for (i = 0; i < dstW - 7; i += 8) {
  653. int acc = g[(buf0[i ] >> 7) + d128[0]];
  654. acc += acc + g[(buf0[i + 1] >> 7) + d128[1]];
  655. acc += acc + g[(buf0[i + 2] >> 7) + d128[2]];
  656. acc += acc + g[(buf0[i + 3] >> 7) + d128[3]];
  657. acc += acc + g[(buf0[i + 4] >> 7) + d128[4]];
  658. acc += acc + g[(buf0[i + 5] >> 7) + d128[5]];
  659. acc += acc + g[(buf0[i + 6] >> 7) + d128[6]];
  660. acc += acc + g[(buf0[i + 7] >> 7) + d128[7]];
  661. output_pixel(*dest++, acc);
  662. }
  663. }
  664. #undef output_pixel
  665. #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
  666. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  667. const int16_t **lumSrc, int lumFilterSize, \
  668. const int16_t *chrFilter, const int16_t **chrUSrc, \
  669. const int16_t **chrVSrc, int chrFilterSize, \
  670. const int16_t **alpSrc, uint8_t *dest, int dstW, \
  671. int y) \
  672. { \
  673. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  674. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  675. alpSrc, dest, dstW, y, fmt); \
  676. } \
  677. \
  678. static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
  679. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  680. const int16_t *abuf[2], uint8_t *dest, int dstW, \
  681. int yalpha, int uvalpha, int y) \
  682. { \
  683. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  684. dest, dstW, yalpha, uvalpha, y, fmt); \
  685. } \
  686. \
  687. static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
  688. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  689. const int16_t *abuf0, uint8_t *dest, int dstW, \
  690. int uvalpha, int y) \
  691. { \
  692. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
  693. abuf0, dest, dstW, uvalpha, \
  694. y, fmt); \
  695. }
  696. YUV2PACKEDWRAPPER(yuv2mono,, white, PIX_FMT_MONOWHITE);
  697. YUV2PACKEDWRAPPER(yuv2mono,, black, PIX_FMT_MONOBLACK);
  698. #define output_pixels(pos, Y1, U, Y2, V) \
  699. if (target == PIX_FMT_YUYV422) { \
  700. dest[pos + 0] = Y1; \
  701. dest[pos + 1] = U; \
  702. dest[pos + 2] = Y2; \
  703. dest[pos + 3] = V; \
  704. } else { \
  705. dest[pos + 0] = U; \
  706. dest[pos + 1] = Y1; \
  707. dest[pos + 2] = V; \
  708. dest[pos + 3] = Y2; \
  709. }
  710. static av_always_inline void
  711. yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
  712. const int16_t **lumSrc, int lumFilterSize,
  713. const int16_t *chrFilter, const int16_t **chrUSrc,
  714. const int16_t **chrVSrc, int chrFilterSize,
  715. const int16_t **alpSrc, uint8_t *dest, int dstW,
  716. int y, enum PixelFormat target)
  717. {
  718. int i;
  719. for (i = 0; i < (dstW >> 1); i++) {
  720. int j;
  721. int Y1 = 1 << 18;
  722. int Y2 = 1 << 18;
  723. int U = 1 << 18;
  724. int V = 1 << 18;
  725. for (j = 0; j < lumFilterSize; j++) {
  726. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  727. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  728. }
  729. for (j = 0; j < chrFilterSize; j++) {
  730. U += chrUSrc[j][i] * chrFilter[j];
  731. V += chrVSrc[j][i] * chrFilter[j];
  732. }
  733. Y1 >>= 19;
  734. Y2 >>= 19;
  735. U >>= 19;
  736. V >>= 19;
  737. if ((Y1 | Y2 | U | V) & 0x100) {
  738. Y1 = av_clip_uint8(Y1);
  739. Y2 = av_clip_uint8(Y2);
  740. U = av_clip_uint8(U);
  741. V = av_clip_uint8(V);
  742. }
  743. output_pixels(4*i, Y1, U, Y2, V);
  744. }
  745. }
  746. static av_always_inline void
  747. yuv2422_2_c_template(SwsContext *c, const int16_t *buf[2],
  748. const int16_t *ubuf[2], const int16_t *vbuf[2],
  749. const int16_t *abuf[2], uint8_t *dest, int dstW,
  750. int yalpha, int uvalpha, int y,
  751. enum PixelFormat target)
  752. {
  753. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  754. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  755. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  756. int yalpha1 = 4095 - yalpha;
  757. int uvalpha1 = 4095 - uvalpha;
  758. int i;
  759. for (i = 0; i < (dstW >> 1); i++) {
  760. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
  761. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
  762. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
  763. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
  764. output_pixels(i * 4, Y1, U, Y2, V);
  765. }
  766. }
  767. static av_always_inline void
  768. yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
  769. const int16_t *ubuf[2], const int16_t *vbuf[2],
  770. const int16_t *abuf0, uint8_t *dest, int dstW,
  771. int uvalpha, int y, enum PixelFormat target)
  772. {
  773. const int16_t *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  774. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  775. int i;
  776. if (uvalpha < 2048) {
  777. for (i = 0; i < (dstW >> 1); i++) {
  778. int Y1 = buf0[i * 2] >> 7;
  779. int Y2 = buf0[i * 2 + 1] >> 7;
  780. int U = ubuf1[i] >> 7;
  781. int V = vbuf1[i] >> 7;
  782. output_pixels(i * 4, Y1, U, Y2, V);
  783. }
  784. } else {
  785. for (i = 0; i < (dstW >> 1); i++) {
  786. int Y1 = buf0[i * 2] >> 7;
  787. int Y2 = buf0[i * 2 + 1] >> 7;
  788. int U = (ubuf0[i] + ubuf1[i]) >> 8;
  789. int V = (vbuf0[i] + vbuf1[i]) >> 8;
  790. output_pixels(i * 4, Y1, U, Y2, V);
  791. }
  792. }
  793. }
  794. #undef output_pixels
  795. YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, PIX_FMT_YUYV422);
  796. YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, PIX_FMT_UYVY422);
  797. #define R_B ((target == PIX_FMT_RGB48LE || target == PIX_FMT_RGB48BE) ? R : B)
  798. #define B_R ((target == PIX_FMT_RGB48LE || target == PIX_FMT_RGB48BE) ? B : R)
  799. #define output_pixel(pos, val) \
  800. if (isBE(target)) { \
  801. AV_WB16(pos, val); \
  802. } else { \
  803. AV_WL16(pos, val); \
  804. }
  805. static av_always_inline void
  806. yuv2rgb48_X_c_template(SwsContext *c, const int16_t *lumFilter,
  807. const int32_t **lumSrc, int lumFilterSize,
  808. const int16_t *chrFilter, const int32_t **chrUSrc,
  809. const int32_t **chrVSrc, int chrFilterSize,
  810. const int32_t **alpSrc, uint16_t *dest, int dstW,
  811. int y, enum PixelFormat target)
  812. {
  813. int i;
  814. for (i = 0; i < (dstW >> 1); i++) {
  815. int j;
  816. int Y1 = 0;
  817. int Y2 = 0;
  818. int U = -128 << 23; // 19
  819. int V = -128 << 23;
  820. int R, G, B;
  821. for (j = 0; j < lumFilterSize; j++) {
  822. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  823. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  824. }
  825. for (j = 0; j < chrFilterSize; j++) {
  826. U += chrUSrc[j][i] * chrFilter[j];
  827. V += chrVSrc[j][i] * chrFilter[j];
  828. }
  829. // 8bit: 12+15=27; 16-bit: 12+19=31
  830. Y1 >>= 14; // 10
  831. Y2 >>= 14;
  832. U >>= 14;
  833. V >>= 14;
  834. // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
  835. Y1 -= c->yuv2rgb_y_offset;
  836. Y2 -= c->yuv2rgb_y_offset;
  837. Y1 *= c->yuv2rgb_y_coeff;
  838. Y2 *= c->yuv2rgb_y_coeff;
  839. Y1 += 1 << 13; // 21
  840. Y2 += 1 << 13;
  841. // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
  842. R = V * c->yuv2rgb_v2r_coeff;
  843. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  844. B = U * c->yuv2rgb_u2b_coeff;
  845. // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
  846. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  847. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  848. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  849. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  850. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  851. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  852. dest += 6;
  853. }
  854. }
  855. static av_always_inline void
  856. yuv2rgb48_2_c_template(SwsContext *c, const int32_t *buf[2],
  857. const int32_t *ubuf[2], const int32_t *vbuf[2],
  858. const int32_t *abuf[2], uint16_t *dest, int dstW,
  859. int yalpha, int uvalpha, int y,
  860. enum PixelFormat target)
  861. {
  862. const int32_t *buf0 = buf[0], *buf1 = buf[1],
  863. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  864. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  865. int yalpha1 = 4095 - yalpha;
  866. int uvalpha1 = 4095 - uvalpha;
  867. int i;
  868. for (i = 0; i < (dstW >> 1); i++) {
  869. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 14;
  870. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 14;
  871. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha + (-128 << 23)) >> 14;
  872. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha + (-128 << 23)) >> 14;
  873. int R, G, B;
  874. Y1 -= c->yuv2rgb_y_offset;
  875. Y2 -= c->yuv2rgb_y_offset;
  876. Y1 *= c->yuv2rgb_y_coeff;
  877. Y2 *= c->yuv2rgb_y_coeff;
  878. Y1 += 1 << 13;
  879. Y2 += 1 << 13;
  880. R = V * c->yuv2rgb_v2r_coeff;
  881. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  882. B = U * c->yuv2rgb_u2b_coeff;
  883. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  884. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  885. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  886. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  887. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  888. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  889. dest += 6;
  890. }
  891. }
  892. static av_always_inline void
  893. yuv2rgb48_1_c_template(SwsContext *c, const int32_t *buf0,
  894. const int32_t *ubuf[2], const int32_t *vbuf[2],
  895. const int32_t *abuf0, uint16_t *dest, int dstW,
  896. int uvalpha, int y, enum PixelFormat target)
  897. {
  898. const int32_t *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  899. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  900. int i;
  901. if (uvalpha < 2048) {
  902. for (i = 0; i < (dstW >> 1); i++) {
  903. int Y1 = (buf0[i * 2] ) >> 2;
  904. int Y2 = (buf0[i * 2 + 1]) >> 2;
  905. int U = (ubuf0[i] + (-128 << 11)) >> 2;
  906. int V = (vbuf0[i] + (-128 << 11)) >> 2;
  907. int R, G, B;
  908. Y1 -= c->yuv2rgb_y_offset;
  909. Y2 -= c->yuv2rgb_y_offset;
  910. Y1 *= c->yuv2rgb_y_coeff;
  911. Y2 *= c->yuv2rgb_y_coeff;
  912. Y1 += 1 << 13;
  913. Y2 += 1 << 13;
  914. R = V * c->yuv2rgb_v2r_coeff;
  915. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  916. B = U * c->yuv2rgb_u2b_coeff;
  917. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  918. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  919. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  920. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  921. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  922. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  923. dest += 6;
  924. }
  925. } else {
  926. for (i = 0; i < (dstW >> 1); i++) {
  927. int Y1 = (buf0[i * 2] ) >> 2;
  928. int Y2 = (buf0[i * 2 + 1]) >> 2;
  929. int U = (ubuf0[i] + ubuf1[i] + (-128 << 12)) >> 3;
  930. int V = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
  931. int R, G, B;
  932. Y1 -= c->yuv2rgb_y_offset;
  933. Y2 -= c->yuv2rgb_y_offset;
  934. Y1 *= c->yuv2rgb_y_coeff;
  935. Y2 *= c->yuv2rgb_y_coeff;
  936. Y1 += 1 << 13;
  937. Y2 += 1 << 13;
  938. R = V * c->yuv2rgb_v2r_coeff;
  939. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  940. B = U * c->yuv2rgb_u2b_coeff;
  941. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  942. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  943. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  944. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  945. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  946. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  947. dest += 6;
  948. }
  949. }
  950. }
  951. #undef output_pixel
  952. #undef r_b
  953. #undef b_r
  954. YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48be, PIX_FMT_RGB48BE);
  955. YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48le, PIX_FMT_RGB48LE);
  956. YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48be, PIX_FMT_BGR48BE);
  957. YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48le, PIX_FMT_BGR48LE);
  958. static av_always_inline void
  959. yuv2rgb_write(uint8_t *_dest, int i, int Y1, int Y2,
  960. int U, int V, int A1, int A2,
  961. const void *_r, const void *_g, const void *_b, int y,
  962. enum PixelFormat target, int hasAlpha)
  963. {
  964. if (target == PIX_FMT_ARGB || target == PIX_FMT_RGBA ||
  965. target == PIX_FMT_ABGR || target == PIX_FMT_BGRA) {
  966. uint32_t *dest = (uint32_t *) _dest;
  967. const uint32_t *r = (const uint32_t *) _r;
  968. const uint32_t *g = (const uint32_t *) _g;
  969. const uint32_t *b = (const uint32_t *) _b;
  970. #if CONFIG_SMALL
  971. int sh = hasAlpha ? ((target == PIX_FMT_RGB32_1 || target == PIX_FMT_BGR32_1) ? 0 : 24) : 0;
  972. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (hasAlpha ? A1 << sh : 0);
  973. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (hasAlpha ? A2 << sh : 0);
  974. #else
  975. if (hasAlpha) {
  976. int sh = (target == PIX_FMT_RGB32_1 || target == PIX_FMT_BGR32_1) ? 0 : 24;
  977. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
  978. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
  979. } else {
  980. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
  981. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
  982. }
  983. #endif
  984. } else if (target == PIX_FMT_RGB24 || target == PIX_FMT_BGR24) {
  985. uint8_t *dest = (uint8_t *) _dest;
  986. const uint8_t *r = (const uint8_t *) _r;
  987. const uint8_t *g = (const uint8_t *) _g;
  988. const uint8_t *b = (const uint8_t *) _b;
  989. #define r_b ((target == PIX_FMT_RGB24) ? r : b)
  990. #define b_r ((target == PIX_FMT_RGB24) ? b : r)
  991. dest[i * 6 + 0] = r_b[Y1];
  992. dest[i * 6 + 1] = g[Y1];
  993. dest[i * 6 + 2] = b_r[Y1];
  994. dest[i * 6 + 3] = r_b[Y2];
  995. dest[i * 6 + 4] = g[Y2];
  996. dest[i * 6 + 5] = b_r[Y2];
  997. #undef r_b
  998. #undef b_r
  999. } else if (target == PIX_FMT_RGB565 || target == PIX_FMT_BGR565 ||
  1000. target == PIX_FMT_RGB555 || target == PIX_FMT_BGR555 ||
  1001. target == PIX_FMT_RGB444 || target == PIX_FMT_BGR444) {
  1002. uint16_t *dest = (uint16_t *) _dest;
  1003. const uint16_t *r = (const uint16_t *) _r;
  1004. const uint16_t *g = (const uint16_t *) _g;
  1005. const uint16_t *b = (const uint16_t *) _b;
  1006. int dr1, dg1, db1, dr2, dg2, db2;
  1007. if (target == PIX_FMT_RGB565 || target == PIX_FMT_BGR565) {
  1008. dr1 = dither_2x2_8[ y & 1 ][0];
  1009. dg1 = dither_2x2_4[ y & 1 ][0];
  1010. db1 = dither_2x2_8[(y & 1) ^ 1][0];
  1011. dr2 = dither_2x2_8[ y & 1 ][1];
  1012. dg2 = dither_2x2_4[ y & 1 ][1];
  1013. db2 = dither_2x2_8[(y & 1) ^ 1][1];
  1014. } else if (target == PIX_FMT_RGB555 || target == PIX_FMT_BGR555) {
  1015. dr1 = dither_2x2_8[ y & 1 ][0];
  1016. dg1 = dither_2x2_8[ y & 1 ][1];
  1017. db1 = dither_2x2_8[(y & 1) ^ 1][0];
  1018. dr2 = dither_2x2_8[ y & 1 ][1];
  1019. dg2 = dither_2x2_8[ y & 1 ][0];
  1020. db2 = dither_2x2_8[(y & 1) ^ 1][1];
  1021. } else {
  1022. dr1 = dither_4x4_16[ y & 3 ][0];
  1023. dg1 = dither_4x4_16[ y & 3 ][1];
  1024. db1 = dither_4x4_16[(y & 3) ^ 3][0];
  1025. dr2 = dither_4x4_16[ y & 3 ][1];
  1026. dg2 = dither_4x4_16[ y & 3 ][0];
  1027. db2 = dither_4x4_16[(y & 3) ^ 3][1];
  1028. }
  1029. dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
  1030. dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
  1031. } else /* 8/4-bit */ {
  1032. uint8_t *dest = (uint8_t *) _dest;
  1033. const uint8_t *r = (const uint8_t *) _r;
  1034. const uint8_t *g = (const uint8_t *) _g;
  1035. const uint8_t *b = (const uint8_t *) _b;
  1036. int dr1, dg1, db1, dr2, dg2, db2;
  1037. if (target == PIX_FMT_RGB8 || target == PIX_FMT_BGR8) {
  1038. const uint8_t * const d64 = dither_8x8_73[y & 7];
  1039. const uint8_t * const d32 = dither_8x8_32[y & 7];
  1040. dr1 = dg1 = d32[(i * 2 + 0) & 7];
  1041. db1 = d64[(i * 2 + 0) & 7];
  1042. dr2 = dg2 = d32[(i * 2 + 1) & 7];
  1043. db2 = d64[(i * 2 + 1) & 7];
  1044. } else {
  1045. const uint8_t * const d64 = dither_8x8_73 [y & 7];
  1046. const uint8_t * const d128 = dither_8x8_220[y & 7];
  1047. dr1 = db1 = d128[(i * 2 + 0) & 7];
  1048. dg1 = d64[(i * 2 + 0) & 7];
  1049. dr2 = db2 = d128[(i * 2 + 1) & 7];
  1050. dg2 = d64[(i * 2 + 1) & 7];
  1051. }
  1052. if (target == PIX_FMT_RGB4 || target == PIX_FMT_BGR4) {
  1053. dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
  1054. ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
  1055. } else {
  1056. dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
  1057. dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
  1058. }
  1059. }
  1060. }
  1061. static av_always_inline void
  1062. yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
  1063. const int16_t **lumSrc, int lumFilterSize,
  1064. const int16_t *chrFilter, const int16_t **chrUSrc,
  1065. const int16_t **chrVSrc, int chrFilterSize,
  1066. const int16_t **alpSrc, uint8_t *dest, int dstW,
  1067. int y, enum PixelFormat target, int hasAlpha)
  1068. {
  1069. int i;
  1070. for (i = 0; i < (dstW >> 1); i++) {
  1071. int j;
  1072. int Y1 = 1 << 18;
  1073. int Y2 = 1 << 18;
  1074. int U = 1 << 18;
  1075. int V = 1 << 18;
  1076. int av_unused A1, A2;
  1077. const void *r, *g, *b;
  1078. for (j = 0; j < lumFilterSize; j++) {
  1079. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  1080. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  1081. }
  1082. for (j = 0; j < chrFilterSize; j++) {
  1083. U += chrUSrc[j][i] * chrFilter[j];
  1084. V += chrVSrc[j][i] * chrFilter[j];
  1085. }
  1086. Y1 >>= 19;
  1087. Y2 >>= 19;
  1088. U >>= 19;
  1089. V >>= 19;
  1090. if ((Y1 | Y2 | U | V) & 0x100) {
  1091. Y1 = av_clip_uint8(Y1);
  1092. Y2 = av_clip_uint8(Y2);
  1093. U = av_clip_uint8(U);
  1094. V = av_clip_uint8(V);
  1095. }
  1096. if (hasAlpha) {
  1097. A1 = 1 << 18;
  1098. A2 = 1 << 18;
  1099. for (j = 0; j < lumFilterSize; j++) {
  1100. A1 += alpSrc[j][i * 2 ] * lumFilter[j];
  1101. A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
  1102. }
  1103. A1 >>= 19;
  1104. A2 >>= 19;
  1105. if ((A1 | A2) & 0x100) {
  1106. A1 = av_clip_uint8(A1);
  1107. A2 = av_clip_uint8(A2);
  1108. }
  1109. }
  1110. /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/
  1111. r = c->table_rV[V];
  1112. g = (c->table_gU[U] + c->table_gV[V]);
  1113. b = c->table_bU[U];
  1114. yuv2rgb_write(dest, i, Y1, Y2, U, V, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1115. r, g, b, y, target, hasAlpha);
  1116. }
  1117. }
  1118. static av_always_inline void
  1119. yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
  1120. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1121. const int16_t *abuf[2], uint8_t *dest, int dstW,
  1122. int yalpha, int uvalpha, int y,
  1123. enum PixelFormat target, int hasAlpha)
  1124. {
  1125. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  1126. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  1127. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
  1128. *abuf0 = hasAlpha ? abuf[0] : NULL,
  1129. *abuf1 = hasAlpha ? abuf[1] : NULL;
  1130. int yalpha1 = 4095 - yalpha;
  1131. int uvalpha1 = 4095 - uvalpha;
  1132. int i;
  1133. for (i = 0; i < (dstW >> 1); i++) {
  1134. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
  1135. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
  1136. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
  1137. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
  1138. int A1, A2;
  1139. const void *r = c->table_rV[V],
  1140. *g = (c->table_gU[U] + c->table_gV[V]),
  1141. *b = c->table_bU[U];
  1142. if (hasAlpha) {
  1143. A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 19;
  1144. A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
  1145. }
  1146. yuv2rgb_write(dest, i, Y1, Y2, U, V, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1147. r, g, b, y, target, hasAlpha);
  1148. }
  1149. }
  1150. static av_always_inline void
  1151. yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
  1152. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1153. const int16_t *abuf0, uint8_t *dest, int dstW,
  1154. int uvalpha, int y, enum PixelFormat target,
  1155. int hasAlpha)
  1156. {
  1157. const int16_t *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  1158. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  1159. int i;
  1160. if (uvalpha < 2048) {
  1161. for (i = 0; i < (dstW >> 1); i++) {
  1162. int Y1 = buf0[i * 2] >> 7;
  1163. int Y2 = buf0[i * 2 + 1] >> 7;
  1164. int U = ubuf1[i] >> 7;
  1165. int V = vbuf1[i] >> 7;
  1166. int A1, A2;
  1167. const void *r = c->table_rV[V],
  1168. *g = (c->table_gU[U] + c->table_gV[V]),
  1169. *b = c->table_bU[U];
  1170. if (hasAlpha) {
  1171. A1 = abuf0[i * 2 ] >> 7;
  1172. A2 = abuf0[i * 2 + 1] >> 7;
  1173. }
  1174. yuv2rgb_write(dest, i, Y1, Y2, U, V, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1175. r, g, b, y, target, hasAlpha);
  1176. }
  1177. } else {
  1178. for (i = 0; i < (dstW >> 1); i++) {
  1179. int Y1 = buf0[i * 2] >> 7;
  1180. int Y2 = buf0[i * 2 + 1] >> 7;
  1181. int U = (ubuf0[i] + ubuf1[i]) >> 8;
  1182. int V = (vbuf0[i] + vbuf1[i]) >> 8;
  1183. int A1, A2;
  1184. const void *r = c->table_rV[V],
  1185. *g = (c->table_gU[U] + c->table_gV[V]),
  1186. *b = c->table_bU[U];
  1187. if (hasAlpha) {
  1188. A1 = abuf0[i * 2 ] >> 7;
  1189. A2 = abuf0[i * 2 + 1] >> 7;
  1190. }
  1191. yuv2rgb_write(dest, i, Y1, Y2, U, V, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1192. r, g, b, y, target, hasAlpha);
  1193. }
  1194. }
  1195. }
  1196. #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
  1197. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  1198. const int16_t **lumSrc, int lumFilterSize, \
  1199. const int16_t *chrFilter, const int16_t **chrUSrc, \
  1200. const int16_t **chrVSrc, int chrFilterSize, \
  1201. const int16_t **alpSrc, uint8_t *dest, int dstW, \
  1202. int y) \
  1203. { \
  1204. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  1205. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  1206. alpSrc, dest, dstW, y, fmt, hasAlpha); \
  1207. }
  1208. #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
  1209. YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
  1210. static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
  1211. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  1212. const int16_t *abuf[2], uint8_t *dest, int dstW, \
  1213. int yalpha, int uvalpha, int y) \
  1214. { \
  1215. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  1216. dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
  1217. } \
  1218. \
  1219. static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
  1220. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  1221. const int16_t *abuf0, uint8_t *dest, int dstW, \
  1222. int uvalpha, int y) \
  1223. { \
  1224. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
  1225. dstW, uvalpha, y, fmt, hasAlpha); \
  1226. }
  1227. #if CONFIG_SMALL
  1228. YUV2RGBWRAPPER(yuv2rgb,, 32_1, PIX_FMT_RGB32_1, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1229. YUV2RGBWRAPPER(yuv2rgb,, 32, PIX_FMT_RGB32, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1230. #else
  1231. #if CONFIG_SWSCALE_ALPHA
  1232. YUV2RGBWRAPPER(yuv2rgb,, a32_1, PIX_FMT_RGB32_1, 1);
  1233. YUV2RGBWRAPPER(yuv2rgb,, a32, PIX_FMT_RGB32, 1);
  1234. #endif
  1235. YUV2RGBWRAPPER(yuv2rgb,, x32_1, PIX_FMT_RGB32_1, 0);
  1236. YUV2RGBWRAPPER(yuv2rgb,, x32, PIX_FMT_RGB32, 0);
  1237. #endif
  1238. YUV2RGBWRAPPER(yuv2, rgb, rgb24, PIX_FMT_RGB24, 0);
  1239. YUV2RGBWRAPPER(yuv2, rgb, bgr24, PIX_FMT_BGR24, 0);
  1240. YUV2RGBWRAPPER(yuv2rgb,, 16, PIX_FMT_RGB565, 0);
  1241. YUV2RGBWRAPPER(yuv2rgb,, 15, PIX_FMT_RGB555, 0);
  1242. YUV2RGBWRAPPER(yuv2rgb,, 12, PIX_FMT_RGB444, 0);
  1243. YUV2RGBWRAPPER(yuv2rgb,, 8, PIX_FMT_RGB8, 0);
  1244. YUV2RGBWRAPPER(yuv2rgb,, 4, PIX_FMT_RGB4, 0);
  1245. YUV2RGBWRAPPER(yuv2rgb,, 4b, PIX_FMT_RGB4_BYTE, 0);
  1246. static av_always_inline void
  1247. yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
  1248. const int16_t **lumSrc, int lumFilterSize,
  1249. const int16_t *chrFilter, const int16_t **chrUSrc,
  1250. const int16_t **chrVSrc, int chrFilterSize,
  1251. const int16_t **alpSrc, uint8_t *dest,
  1252. int dstW, int y, enum PixelFormat target, int hasAlpha)
  1253. {
  1254. int i;
  1255. int step = (target == PIX_FMT_RGB24 || target == PIX_FMT_BGR24) ? 3 : 4;
  1256. for (i = 0; i < dstW; i++) {
  1257. int j;
  1258. int Y = 1<<9;
  1259. int U = (1<<9)-(128 << 19);
  1260. int V = (1<<9)-(128 << 19);
  1261. int av_unused A;
  1262. int R, G, B;
  1263. for (j = 0; j < lumFilterSize; j++) {
  1264. Y += lumSrc[j][i] * lumFilter[j];
  1265. }
  1266. for (j = 0; j < chrFilterSize; j++) {
  1267. U += chrUSrc[j][i] * chrFilter[j];
  1268. V += chrVSrc[j][i] * chrFilter[j];
  1269. }
  1270. Y >>= 10;
  1271. U >>= 10;
  1272. V >>= 10;
  1273. if (hasAlpha) {
  1274. A = 1 << 18;
  1275. for (j = 0; j < lumFilterSize; j++) {
  1276. A += alpSrc[j][i] * lumFilter[j];
  1277. }
  1278. A >>= 19;
  1279. if (A & 0x100)
  1280. A = av_clip_uint8(A);
  1281. }
  1282. Y -= c->yuv2rgb_y_offset;
  1283. Y *= c->yuv2rgb_y_coeff;
  1284. Y += 1 << 21;
  1285. R = Y + V*c->yuv2rgb_v2r_coeff;
  1286. G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
  1287. B = Y + U*c->yuv2rgb_u2b_coeff;
  1288. if ((R | G | B) & 0xC0000000) {
  1289. R = av_clip_uintp2(R, 30);
  1290. G = av_clip_uintp2(G, 30);
  1291. B = av_clip_uintp2(B, 30);
  1292. }
  1293. switch(target) {
  1294. case PIX_FMT_ARGB:
  1295. dest[0] = hasAlpha ? A : 255;
  1296. dest[1] = R >> 22;
  1297. dest[2] = G >> 22;
  1298. dest[3] = B >> 22;
  1299. break;
  1300. case PIX_FMT_RGB24:
  1301. dest[0] = R >> 22;
  1302. dest[1] = G >> 22;
  1303. dest[2] = B >> 22;
  1304. break;
  1305. case PIX_FMT_RGBA:
  1306. dest[0] = R >> 22;
  1307. dest[1] = G >> 22;
  1308. dest[2] = B >> 22;
  1309. dest[3] = hasAlpha ? A : 255;
  1310. break;
  1311. case PIX_FMT_ABGR:
  1312. dest[0] = hasAlpha ? A : 255;
  1313. dest[1] = B >> 22;
  1314. dest[2] = G >> 22;
  1315. dest[3] = R >> 22;
  1316. break;
  1317. case PIX_FMT_BGR24:
  1318. dest[0] = B >> 22;
  1319. dest[1] = G >> 22;
  1320. dest[2] = R >> 22;
  1321. break;
  1322. case PIX_FMT_BGRA:
  1323. dest[0] = B >> 22;
  1324. dest[1] = G >> 22;
  1325. dest[2] = R >> 22;
  1326. dest[3] = hasAlpha ? A : 255;
  1327. break;
  1328. }
  1329. dest += step;
  1330. }
  1331. }
  1332. #if CONFIG_SMALL
  1333. YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, PIX_FMT_BGRA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1334. YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, PIX_FMT_ABGR, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1335. YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, PIX_FMT_RGBA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1336. YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, PIX_FMT_ARGB, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1337. #else
  1338. #if CONFIG_SWSCALE_ALPHA
  1339. YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, PIX_FMT_BGRA, 1);
  1340. YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, PIX_FMT_ABGR, 1);
  1341. YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, PIX_FMT_RGBA, 1);
  1342. YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, PIX_FMT_ARGB, 1);
  1343. #endif
  1344. YUV2RGBWRAPPERX(yuv2, rgb_full, bgrx32_full, PIX_FMT_BGRA, 0);
  1345. YUV2RGBWRAPPERX(yuv2, rgb_full, xbgr32_full, PIX_FMT_ABGR, 0);
  1346. YUV2RGBWRAPPERX(yuv2, rgb_full, rgbx32_full, PIX_FMT_RGBA, 0);
  1347. YUV2RGBWRAPPERX(yuv2, rgb_full, xrgb32_full, PIX_FMT_ARGB, 0);
  1348. #endif
  1349. YUV2RGBWRAPPERX(yuv2, rgb_full, bgr24_full, PIX_FMT_BGR24, 0);
  1350. YUV2RGBWRAPPERX(yuv2, rgb_full, rgb24_full, PIX_FMT_RGB24, 0);
  1351. static av_always_inline void fillPlane(uint8_t* plane, int stride,
  1352. int width, int height,
  1353. int y, uint8_t val)
  1354. {
  1355. int i;
  1356. uint8_t *ptr = plane + stride*y;
  1357. for (i=0; i<height; i++) {
  1358. memset(ptr, val, width);
  1359. ptr += stride;
  1360. }
  1361. }
  1362. #define input_pixel(pos) (isBE(origin) ? AV_RB16(pos) : AV_RL16(pos))
  1363. #define r ((origin == PIX_FMT_BGR48BE || origin == PIX_FMT_BGR48LE) ? b_r : r_b)
  1364. #define b ((origin == PIX_FMT_BGR48BE || origin == PIX_FMT_BGR48LE) ? r_b : b_r)
  1365. static av_always_inline void
  1366. rgb48ToY_c_template(uint16_t *dst, const uint16_t *src, int width,
  1367. enum PixelFormat origin)
  1368. {
  1369. int i;
  1370. for (i = 0; i < width; i++) {
  1371. unsigned int r_b = input_pixel(&src[i*3+0]);
  1372. unsigned int g = input_pixel(&src[i*3+1]);
  1373. unsigned int b_r = input_pixel(&src[i*3+2]);
  1374. dst[i] = (RY*r + GY*g + BY*b + (0x2001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1375. }
  1376. }
  1377. static av_always_inline void
  1378. rgb48ToUV_c_template(uint16_t *dstU, uint16_t *dstV,
  1379. const uint16_t *src1, const uint16_t *src2,
  1380. int width, enum PixelFormat origin)
  1381. {
  1382. int i;
  1383. assert(src1==src2);
  1384. for (i = 0; i < width; i++) {
  1385. int r_b = input_pixel(&src1[i*3+0]);
  1386. int g = input_pixel(&src1[i*3+1]);
  1387. int b_r = input_pixel(&src1[i*3+2]);
  1388. dstU[i] = (RU*r + GU*g + BU*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1389. dstV[i] = (RV*r + GV*g + BV*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1390. }
  1391. }
  1392. static av_always_inline void
  1393. rgb48ToUV_half_c_template(uint16_t *dstU, uint16_t *dstV,
  1394. const uint16_t *src1, const uint16_t *src2,
  1395. int width, enum PixelFormat origin)
  1396. {
  1397. int i;
  1398. assert(src1==src2);
  1399. for (i = 0; i < width; i++) {
  1400. int r_b = (input_pixel(&src1[6 * i + 0]) + input_pixel(&src1[6 * i + 3]) + 1) >> 1;
  1401. int g = (input_pixel(&src1[6 * i + 1]) + input_pixel(&src1[6 * i + 4]) + 1) >> 1;
  1402. int b_r = (input_pixel(&src1[6 * i + 2]) + input_pixel(&src1[6 * i + 5]) + 1) >> 1;
  1403. dstU[i]= (RU*r + GU*g + BU*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1404. dstV[i]= (RV*r + GV*g + BV*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1405. }
  1406. }
  1407. #undef r
  1408. #undef b
  1409. #undef input_pixel
  1410. #define rgb48funcs(pattern, BE_LE, origin) \
  1411. static void pattern ## 48 ## BE_LE ## ToY_c(uint8_t *_dst, const uint8_t *_src, \
  1412. int width, uint32_t *unused) \
  1413. { \
  1414. const uint16_t *src = (const uint16_t *) _src; \
  1415. uint16_t *dst = (uint16_t *) _dst; \
  1416. rgb48ToY_c_template(dst, src, width, origin); \
  1417. } \
  1418. \
  1419. static void pattern ## 48 ## BE_LE ## ToUV_c(uint8_t *_dstU, uint8_t *_dstV, \
  1420. const uint8_t *_src1, const uint8_t *_src2, \
  1421. int width, uint32_t *unused) \
  1422. { \
  1423. const uint16_t *src1 = (const uint16_t *) _src1, \
  1424. *src2 = (const uint16_t *) _src2; \
  1425. uint16_t *dstU = (uint16_t *) _dstU, *dstV = (uint16_t *) _dstV; \
  1426. rgb48ToUV_c_template(dstU, dstV, src1, src2, width, origin); \
  1427. } \
  1428. \
  1429. static void pattern ## 48 ## BE_LE ## ToUV_half_c(uint8_t *_dstU, uint8_t *_dstV, \
  1430. const uint8_t *_src1, const uint8_t *_src2, \
  1431. int width, uint32_t *unused) \
  1432. { \
  1433. const uint16_t *src1 = (const uint16_t *) _src1, \
  1434. *src2 = (const uint16_t *) _src2; \
  1435. uint16_t *dstU = (uint16_t *) _dstU, *dstV = (uint16_t *) _dstV; \
  1436. rgb48ToUV_half_c_template(dstU, dstV, src1, src2, width, origin); \
  1437. }
  1438. rgb48funcs(rgb, LE, PIX_FMT_RGB48LE);
  1439. rgb48funcs(rgb, BE, PIX_FMT_RGB48BE);
  1440. rgb48funcs(bgr, LE, PIX_FMT_BGR48LE);
  1441. rgb48funcs(bgr, BE, PIX_FMT_BGR48BE);
  1442. #define input_pixel(i) ((origin == PIX_FMT_RGBA || origin == PIX_FMT_BGRA || \
  1443. origin == PIX_FMT_ARGB || origin == PIX_FMT_ABGR) ? AV_RN32A(&src[(i)*4]) : \
  1444. (isBE(origin) ? AV_RB16(&src[(i)*2]) : AV_RL16(&src[(i)*2])))
  1445. static av_always_inline void
  1446. rgb16_32ToY_c_template(int16_t *dst, const uint8_t *src,
  1447. int width, enum PixelFormat origin,
  1448. int shr, int shg, int shb, int shp,
  1449. int maskr, int maskg, int maskb,
  1450. int rsh, int gsh, int bsh, int S)
  1451. {
  1452. const int ry = RY << rsh, gy = GY << gsh, by = BY << bsh,
  1453. rnd = (32<<((S)-1)) + (1<<(S-7));
  1454. int i;
  1455. for (i = 0; i < width; i++) {
  1456. int px = input_pixel(i) >> shp;
  1457. int b = (px & maskb) >> shb;
  1458. int g = (px & maskg) >> shg;
  1459. int r = (px & maskr) >> shr;
  1460. dst[i] = (ry * r + gy * g + by * b + rnd) >> ((S)-6);
  1461. }
  1462. }
  1463. static av_always_inline void
  1464. rgb16_32ToUV_c_template(int16_t *dstU, int16_t *dstV,
  1465. const uint8_t *src, int width,
  1466. enum PixelFormat origin,
  1467. int shr, int shg, int shb, int shp,
  1468. int maskr, int maskg, int maskb,
  1469. int rsh, int gsh, int bsh, int S)
  1470. {
  1471. const int ru = RU << rsh, gu = GU << gsh, bu = BU << bsh,
  1472. rv = RV << rsh, gv = GV << gsh, bv = BV << bsh,
  1473. rnd = (256<<((S)-1)) + (1<<(S-7));
  1474. int i;
  1475. for (i = 0; i < width; i++) {
  1476. int px = input_pixel(i) >> shp;
  1477. int b = (px & maskb) >> shb;
  1478. int g = (px & maskg) >> shg;
  1479. int r = (px & maskr) >> shr;
  1480. dstU[i] = (ru * r + gu * g + bu * b + rnd) >> ((S)-6);
  1481. dstV[i] = (rv * r + gv * g + bv * b + rnd) >> ((S)-6);
  1482. }
  1483. }
  1484. static av_always_inline void
  1485. rgb16_32ToUV_half_c_template(int16_t *dstU, int16_t *dstV,
  1486. const uint8_t *src, int width,
  1487. enum PixelFormat origin,
  1488. int shr, int shg, int shb, int shp,
  1489. int maskr, int maskg, int maskb,
  1490. int rsh, int gsh, int bsh, int S)
  1491. {
  1492. const int ru = RU << rsh, gu = GU << gsh, bu = BU << bsh,
  1493. rv = RV << rsh, gv = GV << gsh, bv = BV << bsh,
  1494. rnd = (256U<<(S)) + (1<<(S-6)), maskgx = ~(maskr | maskb);
  1495. int i;
  1496. maskr |= maskr << 1; maskb |= maskb << 1; maskg |= maskg << 1;
  1497. for (i = 0; i < width; i++) {
  1498. int px0 = input_pixel(2 * i + 0) >> shp;
  1499. int px1 = input_pixel(2 * i + 1) >> shp;
  1500. int b, r, g = (px0 & maskgx) + (px1 & maskgx);
  1501. int rb = px0 + px1 - g;
  1502. b = (rb & maskb) >> shb;
  1503. if (shp || origin == PIX_FMT_BGR565LE || origin == PIX_FMT_BGR565BE ||
  1504. origin == PIX_FMT_RGB565LE || origin == PIX_FMT_RGB565BE) {
  1505. g >>= shg;
  1506. } else {
  1507. g = (g & maskg) >> shg;
  1508. }
  1509. r = (rb & maskr) >> shr;
  1510. dstU[i] = (ru * r + gu * g + bu * b + (unsigned)rnd) >> ((S)-6+1);
  1511. dstV[i] = (rv * r + gv * g + bv * b + (unsigned)rnd) >> ((S)-6+1);
  1512. }
  1513. }
  1514. #undef input_pixel
  1515. #define rgb16_32_wrapper(fmt, name, shr, shg, shb, shp, maskr, \
  1516. maskg, maskb, rsh, gsh, bsh, S) \
  1517. static void name ## ToY_c(uint8_t *dst, const uint8_t *src, \
  1518. int width, uint32_t *unused) \
  1519. { \
  1520. rgb16_32ToY_c_template(dst, src, width, fmt, shr, shg, shb, shp, \
  1521. maskr, maskg, maskb, rsh, gsh, bsh, S); \
  1522. } \
  1523. \
  1524. static void name ## ToUV_c(uint8_t *dstU, uint8_t *dstV, \
  1525. const uint8_t *src, const uint8_t *dummy, \
  1526. int width, uint32_t *unused) \
  1527. { \
  1528. rgb16_32ToUV_c_template(dstU, dstV, src, width, fmt, shr, shg, shb, shp, \
  1529. maskr, maskg, maskb, rsh, gsh, bsh, S); \
  1530. } \
  1531. \
  1532. static void name ## ToUV_half_c(uint8_t *dstU, uint8_t *dstV, \
  1533. const uint8_t *src, const uint8_t *dummy, \
  1534. int width, uint32_t *unused) \
  1535. { \
  1536. rgb16_32ToUV_half_c_template(dstU, dstV, src, width, fmt, shr, shg, shb, shp, \
  1537. maskr, maskg, maskb, rsh, gsh, bsh, S); \
  1538. }
  1539. rgb16_32_wrapper(PIX_FMT_BGR32, bgr32, 16, 0, 0, 0, 0xFF0000, 0xFF00, 0x00FF, 8, 0, 8, RGB2YUV_SHIFT+8);
  1540. rgb16_32_wrapper(PIX_FMT_BGR32_1, bgr321, 16, 0, 0, 8, 0xFF0000, 0xFF00, 0x00FF, 8, 0, 8, RGB2YUV_SHIFT+8);
  1541. rgb16_32_wrapper(PIX_FMT_RGB32, rgb32, 0, 0, 16, 0, 0x00FF, 0xFF00, 0xFF0000, 8, 0, 8, RGB2YUV_SHIFT+8);
  1542. rgb16_32_wrapper(PIX_FMT_RGB32_1, rgb321, 0, 0, 16, 8, 0x00FF, 0xFF00, 0xFF0000, 8, 0, 8, RGB2YUV_SHIFT+8);
  1543. rgb16_32_wrapper(PIX_FMT_BGR565LE, bgr16le, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, 11, 5, 0, RGB2YUV_SHIFT+8);
  1544. rgb16_32_wrapper(PIX_FMT_BGR555LE, bgr15le, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, 10, 5, 0, RGB2YUV_SHIFT+7);
  1545. rgb16_32_wrapper(PIX_FMT_RGB565LE, rgb16le, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, 0, 5, 11, RGB2YUV_SHIFT+8);
  1546. rgb16_32_wrapper(PIX_FMT_RGB555LE, rgb15le, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, 0, 5, 10, RGB2YUV_SHIFT+7);
  1547. rgb16_32_wrapper(PIX_FMT_BGR565BE, bgr16be, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, 11, 5, 0, RGB2YUV_SHIFT+8);
  1548. rgb16_32_wrapper(PIX_FMT_BGR555BE, bgr15be, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, 10, 5, 0, RGB2YUV_SHIFT+7);
  1549. rgb16_32_wrapper(PIX_FMT_RGB565BE, rgb16be, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, 0, 5, 11, RGB2YUV_SHIFT+8);
  1550. rgb16_32_wrapper(PIX_FMT_RGB555BE, rgb15be, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, 0, 5, 10, RGB2YUV_SHIFT+7);
  1551. static void abgrToA_c(int16_t *dst, const uint8_t *src, int width, uint32_t *unused)
  1552. {
  1553. int i;
  1554. for (i=0; i<width; i++) {
  1555. dst[i]= src[4*i]<<6;
  1556. }
  1557. }
  1558. static void rgbaToA_c(int16_t *dst, const uint8_t *src, int width, uint32_t *unused)
  1559. {
  1560. int i;
  1561. for (i=0; i<width; i++) {
  1562. dst[i]= src[4*i+3]<<6;
  1563. }
  1564. }
  1565. static void palToA_c(int16_t *dst, const uint8_t *src, int width, uint32_t *pal)
  1566. {
  1567. int i;
  1568. for (i=0; i<width; i++) {
  1569. int d= src[i];
  1570. dst[i]= (pal[d] >> 24)<<6;
  1571. }
  1572. }
  1573. static void palToY_c(int16_t *dst, const uint8_t *src, long width, uint32_t *pal)
  1574. {
  1575. int i;
  1576. for (i=0; i<width; i++) {
  1577. int d= src[i];
  1578. dst[i]= (pal[d] & 0xFF)<<6;
  1579. }
  1580. }
  1581. static void palToUV_c(uint16_t *dstU, int16_t *dstV,
  1582. const uint8_t *src1, const uint8_t *src2,
  1583. int width, uint32_t *pal)
  1584. {
  1585. int i;
  1586. assert(src1 == src2);
  1587. for (i=0; i<width; i++) {
  1588. int p= pal[src1[i]];
  1589. dstU[i]= (uint8_t)(p>> 8)<<6;
  1590. dstV[i]= (uint8_t)(p>>16)<<6;
  1591. }
  1592. }
  1593. static void monowhite2Y_c(int16_t *dst, const uint8_t *src, int width, uint32_t *unused)
  1594. {
  1595. int i, j;
  1596. for (i=0; i<width/8; i++) {
  1597. int d= ~src[i];
  1598. for(j=0; j<8; j++)
  1599. dst[8*i+j]= ((d>>(7-j))&1)*16383;
  1600. }
  1601. if(width&7){
  1602. int d= ~src[i];
  1603. for(j=0; j<(width&7); j++)
  1604. dst[8*i+j]= ((d>>(7-j))&1)*16383;
  1605. }
  1606. }
  1607. static void monoblack2Y_c(int16_t *dst, const uint8_t *src, int width, uint32_t *unused)
  1608. {
  1609. int i, j;
  1610. for (i=0; i<width/8; i++) {
  1611. int d= src[i];
  1612. for(j=0; j<8; j++)
  1613. dst[8*i+j]= ((d>>(7-j))&1)*16383;
  1614. }
  1615. if(width&7){
  1616. int d= src[i];
  1617. for(j=0; j<(width&7); j++)
  1618. dst[8*i+j]= ((d>>(7-j))&1)*16383;
  1619. }
  1620. }
  1621. //FIXME yuy2* can read up to 7 samples too much
  1622. static void yuy2ToY_c(uint8_t *dst, const uint8_t *src, int width,
  1623. uint32_t *unused)
  1624. {
  1625. int i;
  1626. for (i=0; i<width; i++)
  1627. dst[i]= src[2*i];
  1628. }
  1629. static void yuy2ToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  1630. const uint8_t *src2, int width, uint32_t *unused)
  1631. {
  1632. int i;
  1633. for (i=0; i<width; i++) {
  1634. dstU[i]= src1[4*i + 1];
  1635. dstV[i]= src1[4*i + 3];
  1636. }
  1637. assert(src1 == src2);
  1638. }
  1639. static void bswap16Y_c(uint8_t *_dst, const uint8_t *_src, int width, uint32_t *unused)
  1640. {
  1641. int i;
  1642. const uint16_t *src = (const uint16_t *) _src;
  1643. uint16_t *dst = (uint16_t *) _dst;
  1644. for (i=0; i<width; i++) {
  1645. dst[i] = av_bswap16(src[i]);
  1646. }
  1647. }
  1648. static void bswap16UV_c(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *_src1,
  1649. const uint8_t *_src2, int width, uint32_t *unused)
  1650. {
  1651. int i;
  1652. const uint16_t *src1 = (const uint16_t *) _src1,
  1653. *src2 = (const uint16_t *) _src2;
  1654. uint16_t *dstU = (uint16_t *) _dstU, *dstV = (uint16_t *) _dstV;
  1655. for (i=0; i<width; i++) {
  1656. dstU[i] = av_bswap16(src1[i]);
  1657. dstV[i] = av_bswap16(src2[i]);
  1658. }
  1659. }
  1660. /* This is almost identical to the previous, end exists only because
  1661. * yuy2ToY/UV)(dst, src+1, ...) would have 100% unaligned accesses. */
  1662. static void uyvyToY_c(uint8_t *dst, const uint8_t *src, int width,
  1663. uint32_t *unused)
  1664. {
  1665. int i;
  1666. for (i=0; i<width; i++)
  1667. dst[i]= src[2*i+1];
  1668. }
  1669. static void uyvyToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
  1670. const uint8_t *src2, int width, uint32_t *unused)
  1671. {
  1672. int i;
  1673. for (i=0; i<width; i++) {
  1674. dstU[i]= src1[4*i + 0];
  1675. dstV[i]= src1[4*i + 2];
  1676. }
  1677. assert(src1 == src2);
  1678. }
  1679. static av_always_inline void nvXXtoUV_c(uint8_t *dst1, uint8_t *dst2,
  1680. const uint8_t *src, int width)
  1681. {
  1682. int i;
  1683. for (i = 0; i < width; i++) {
  1684. dst1[i] = src[2*i+0];
  1685. dst2[i] = src[2*i+1];
  1686. }
  1687. }
  1688. static void nv12ToUV_c(uint8_t *dstU, uint8_t *dstV,
  1689. const uint8_t *src1, const uint8_t *src2,
  1690. int width, uint32_t *unused)
  1691. {
  1692. nvXXtoUV_c(dstU, dstV, src1, width);
  1693. }
  1694. static void nv21ToUV_c(uint8_t *dstU, uint8_t *dstV,
  1695. const uint8_t *src1, const uint8_t *src2,
  1696. int width, uint32_t *unused)
  1697. {
  1698. nvXXtoUV_c(dstV, dstU, src1, width);
  1699. }
  1700. #define input_pixel(pos) (isBE(origin) ? AV_RB16(pos) : AV_RL16(pos))
  1701. static void bgr24ToY_c(int16_t *dst, const uint8_t *src,
  1702. int width, uint32_t *unused)
  1703. {
  1704. int i;
  1705. for (i=0; i<width; i++) {
  1706. int b= src[i*3+0];
  1707. int g= src[i*3+1];
  1708. int r= src[i*3+2];
  1709. dst[i]= ((RY*r + GY*g + BY*b + (32<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6));
  1710. }
  1711. }
  1712. static void bgr24ToUV_c(int16_t *dstU, int16_t *dstV, const uint8_t *src1,
  1713. const uint8_t *src2, int width, uint32_t *unused)
  1714. {
  1715. int i;
  1716. for (i=0; i<width; i++) {
  1717. int b= src1[3*i + 0];
  1718. int g= src1[3*i + 1];
  1719. int r= src1[3*i + 2];
  1720. dstU[i]= (RU*r + GU*g + BU*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
  1721. dstV[i]= (RV*r + GV*g + BV*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
  1722. }
  1723. assert(src1 == src2);
  1724. }
  1725. static void bgr24ToUV_half_c(int16_t *dstU, int16_t *dstV, const uint8_t *src1,
  1726. const uint8_t *src2, int width, uint32_t *unused)
  1727. {
  1728. int i;
  1729. for (i=0; i<width; i++) {
  1730. int b= src1[6*i + 0] + src1[6*i + 3];
  1731. int g= src1[6*i + 1] + src1[6*i + 4];
  1732. int r= src1[6*i + 2] + src1[6*i + 5];
  1733. dstU[i]= (RU*r + GU*g + BU*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
  1734. dstV[i]= (RV*r + GV*g + BV*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
  1735. }
  1736. assert(src1 == src2);
  1737. }
  1738. static void rgb24ToY_c(int16_t *dst, const uint8_t *src, int width,
  1739. uint32_t *unused)
  1740. {
  1741. int i;
  1742. for (i=0; i<width; i++) {
  1743. int r= src[i*3+0];
  1744. int g= src[i*3+1];
  1745. int b= src[i*3+2];
  1746. dst[i]= ((RY*r + GY*g + BY*b + (32<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6));
  1747. }
  1748. }
  1749. static void rgb24ToUV_c(int16_t *dstU, int16_t *dstV, const uint8_t *src1,
  1750. const uint8_t *src2, int width, uint32_t *unused)
  1751. {
  1752. int i;
  1753. assert(src1==src2);
  1754. for (i=0; i<width; i++) {
  1755. int r= src1[3*i + 0];
  1756. int g= src1[3*i + 1];
  1757. int b= src1[3*i + 2];
  1758. dstU[i]= (RU*r + GU*g + BU*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
  1759. dstV[i]= (RV*r + GV*g + BV*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
  1760. }
  1761. }
  1762. static void rgb24ToUV_half_c(int16_t *dstU, int16_t *dstV, const uint8_t *src1,
  1763. const uint8_t *src2, int width, uint32_t *unused)
  1764. {
  1765. int i;
  1766. assert(src1==src2);
  1767. for (i=0; i<width; i++) {
  1768. int r= src1[6*i + 0] + src1[6*i + 3];
  1769. int g= src1[6*i + 1] + src1[6*i + 4];
  1770. int b= src1[6*i + 2] + src1[6*i + 5];
  1771. dstU[i]= (RU*r + GU*g + BU*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
  1772. dstV[i]= (RV*r + GV*g + BV*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
  1773. }
  1774. }
  1775. static void hScale16_c(SwsContext *c, int16_t *_dst, int dstW, const uint8_t *_src,
  1776. const int16_t *filter,
  1777. const int16_t *filterPos, int filterSize)
  1778. {
  1779. int i;
  1780. int32_t *dst = (int32_t *) _dst;
  1781. const uint16_t *src = (const uint16_t *) _src;
  1782. int bits = av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1;
  1783. int sh = (bits <= 7) ? 11 : (bits - 4);
  1784. if((isAnyRGB(c->srcFormat) || c->srcFormat==PIX_FMT_PAL8) && av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1<15)
  1785. sh= 9;
  1786. for (i = 0; i < dstW; i++) {
  1787. int j;
  1788. int srcPos = filterPos[i];
  1789. int val = 0;
  1790. for (j = 0; j < filterSize; j++) {
  1791. val += src[srcPos + j] * filter[filterSize * i + j];
  1792. }
  1793. // filter=14 bit, input=16 bit, output=30 bit, >> 11 makes 19 bit
  1794. dst[i] = FFMIN(val >> sh, (1 << 19) - 1);
  1795. }
  1796. }
  1797. // bilinear / bicubic scaling
  1798. static void hScale_c(SwsContext *c, int16_t *dst, int dstW, const uint8_t *src,
  1799. const int16_t *filter, const int16_t *filterPos,
  1800. int filterSize)
  1801. {
  1802. int i;
  1803. for (i=0; i<dstW; i++) {
  1804. int j;
  1805. int srcPos= filterPos[i];
  1806. int val=0;
  1807. for (j=0; j<filterSize; j++) {
  1808. val += ((int)src[srcPos + j])*filter[filterSize*i + j];
  1809. }
  1810. //filter += hFilterSize;
  1811. dst[i] = FFMIN(val>>7, (1<<15)-1); // the cubic equation does overflow ...
  1812. //dst[i] = val>>7;
  1813. }
  1814. }
  1815. static inline void hScale16N_c(int16_t *dst, int dstW, const uint16_t *src, int srcW, int xInc,
  1816. const int16_t *filter, const int16_t *filterPos, long filterSize, int shift)
  1817. {
  1818. int i, j;
  1819. for (i=0; i<dstW; i++) {
  1820. int srcPos= filterPos[i];
  1821. int val=0;
  1822. for (j=0; j<filterSize; j++) {
  1823. val += ((int)src[srcPos + j])*filter[filterSize*i + j];
  1824. }
  1825. dst[i] = FFMIN(val>>shift, (1<<15)-1); // the cubic equation does overflow ...
  1826. }
  1827. }
  1828. static inline void hScale16NX_c(int16_t *dst, int dstW, const uint16_t *src, int srcW, int xInc,
  1829. const int16_t *filter, const int16_t *filterPos, long filterSize, int shift)
  1830. {
  1831. int i, j;
  1832. for (i=0; i<dstW; i++) {
  1833. int srcPos= filterPos[i];
  1834. int val=0;
  1835. for (j=0; j<filterSize; j++) {
  1836. val += ((int)av_bswap16(src[srcPos + j]))*filter[filterSize*i + j];
  1837. }
  1838. dst[i] = FFMIN(val>>shift, (1<<15)-1); // the cubic equation does overflow ...
  1839. }
  1840. }
  1841. //FIXME all pal and rgb srcFormats could do this convertion as well
  1842. //FIXME all scalers more complex than bilinear could do half of this transform
  1843. static void chrRangeToJpeg_c(int16_t *dstU, int16_t *dstV, int width)
  1844. {
  1845. int i;
  1846. for (i = 0; i < width; i++) {
  1847. dstU[i] = (FFMIN(dstU[i],30775)*4663 - 9289992)>>12; //-264
  1848. dstV[i] = (FFMIN(dstV[i],30775)*4663 - 9289992)>>12; //-264
  1849. }
  1850. }
  1851. static void chrRangeFromJpeg_c(int16_t *dstU, int16_t *dstV, int width)
  1852. {
  1853. int i;
  1854. for (i = 0; i < width; i++) {
  1855. dstU[i] = (dstU[i]*1799 + 4081085)>>11; //1469
  1856. dstV[i] = (dstV[i]*1799 + 4081085)>>11; //1469
  1857. }
  1858. }
  1859. static void lumRangeToJpeg_c(int16_t *dst, int width)
  1860. {
  1861. int i;
  1862. for (i = 0; i < width; i++)
  1863. dst[i] = (FFMIN(dst[i],30189)*19077 - 39057361)>>14;
  1864. }
  1865. static void lumRangeFromJpeg_c(int16_t *dst, int width)
  1866. {
  1867. int i;
  1868. for (i = 0; i < width; i++)
  1869. dst[i] = (dst[i]*14071 + 33561947)>>14;
  1870. }
  1871. static void chrRangeToJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
  1872. {
  1873. int i;
  1874. int32_t *dstU = (int32_t *) _dstU;
  1875. int32_t *dstV = (int32_t *) _dstV;
  1876. for (i = 0; i < width; i++) {
  1877. dstU[i] = (FFMIN(dstU[i],30775<<4)*4663 - (9289992<<4))>>12; //-264
  1878. dstV[i] = (FFMIN(dstV[i],30775<<4)*4663 - (9289992<<4))>>12; //-264
  1879. }
  1880. }
  1881. static void chrRangeFromJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
  1882. {
  1883. int i;
  1884. int32_t *dstU = (int32_t *) _dstU;
  1885. int32_t *dstV = (int32_t *) _dstV;
  1886. for (i = 0; i < width; i++) {
  1887. dstU[i] = (dstU[i]*1799 + (4081085<<4))>>11; //1469
  1888. dstV[i] = (dstV[i]*1799 + (4081085<<4))>>11; //1469
  1889. }
  1890. }
  1891. static void lumRangeToJpeg16_c(int16_t *_dst, int width)
  1892. {
  1893. int i;
  1894. int32_t *dst = (int32_t *) _dst;
  1895. for (i = 0; i < width; i++)
  1896. dst[i] = (FFMIN(dst[i],30189<<4)*4769 - (39057361<<2))>>12;
  1897. }
  1898. static void lumRangeFromJpeg16_c(int16_t *_dst, int width)
  1899. {
  1900. int i;
  1901. int32_t *dst = (int32_t *) _dst;
  1902. for (i = 0; i < width; i++)
  1903. dst[i] = (dst[i]*(14071/4) + (33561947<<4)/4)>>12;
  1904. }
  1905. static void hyscale_fast_c(SwsContext *c, int16_t *dst, int dstWidth,
  1906. const uint8_t *src, int srcW, int xInc)
  1907. {
  1908. int i;
  1909. unsigned int xpos=0;
  1910. for (i=0;i<dstWidth;i++) {
  1911. register unsigned int xx=xpos>>16;
  1912. register unsigned int xalpha=(xpos&0xFFFF)>>9;
  1913. dst[i]= (src[xx]<<7) + (src[xx+1] - src[xx])*xalpha;
  1914. xpos+=xInc;
  1915. }
  1916. for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--)
  1917. dst[i] = src[srcW-1]*128;
  1918. }
  1919. static void scale8To16Rv_c(uint16_t *_dst, const uint8_t *src, int len)
  1920. {
  1921. int i;
  1922. uint8_t *dst = (uint8_t *) _dst;
  1923. for (i = len - 1; i >= 0; i--) {
  1924. dst[i * 2] = dst[i * 2 + 1] = src[i];
  1925. }
  1926. }
  1927. static void scale19To15Fw_c(int16_t *dst, const int32_t *src, int len)
  1928. {
  1929. int i;
  1930. for (i = 0; i < len; i++) {
  1931. dst[i] = src[i] >> 4;
  1932. }
  1933. }
  1934. // *** horizontal scale Y line to temp buffer
  1935. static av_always_inline void hyscale(SwsContext *c, int16_t *dst, int dstWidth,
  1936. const uint8_t *src, int srcW, int xInc,
  1937. const int16_t *hLumFilter,
  1938. const int16_t *hLumFilterPos, int hLumFilterSize,
  1939. uint8_t *formatConvBuffer,
  1940. uint32_t *pal, int isAlpha)
  1941. {
  1942. void (*toYV12)(uint8_t *, const uint8_t *, int, uint32_t *) = isAlpha ? c->alpToYV12 : c->lumToYV12;
  1943. void (*convertRange)(int16_t *, int) = isAlpha ? NULL : c->lumConvertRange;
  1944. if (toYV12) {
  1945. toYV12(formatConvBuffer, src, srcW, pal);
  1946. src= formatConvBuffer;
  1947. }
  1948. if (av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1 < 8 && c->scalingBpp == 16 && !isAnyRGB(c->srcFormat)) {
  1949. c->scale8To16Rv((uint16_t *) formatConvBuffer, src, srcW);
  1950. src = formatConvBuffer;
  1951. }
  1952. if (c->hScale16) {
  1953. int shift= isAnyRGB(c->srcFormat) || c->srcFormat==PIX_FMT_PAL8 ? 13 : av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1;
  1954. c->hScale16(dst, dstWidth, (const uint16_t*)src, srcW, xInc, hLumFilter, hLumFilterPos, hLumFilterSize, shift);
  1955. } else if (!c->hyscale_fast) {
  1956. c->hScale(c, dst, dstWidth, src, hLumFilter, hLumFilterPos, hLumFilterSize);
  1957. } else { // fast bilinear upscale / crap downscale
  1958. c->hyscale_fast(c, dst, dstWidth, src, srcW, xInc);
  1959. }
  1960. if (convertRange)
  1961. convertRange(dst, dstWidth);
  1962. if (av_pix_fmt_descriptors[c->dstFormat].comp[0].depth_minus1 < 15 && c->scalingBpp == 16) {
  1963. c->scale19To15Fw(dst, (int32_t *) dst, dstWidth);
  1964. }
  1965. }
  1966. static void hcscale_fast_c(SwsContext *c, int16_t *dst1, int16_t *dst2,
  1967. int dstWidth, const uint8_t *src1,
  1968. const uint8_t *src2, int srcW, int xInc)
  1969. {
  1970. int i;
  1971. unsigned int xpos=0;
  1972. for (i=0;i<dstWidth;i++) {
  1973. register unsigned int xx=xpos>>16;
  1974. register unsigned int xalpha=(xpos&0xFFFF)>>9;
  1975. dst1[i]=(src1[xx]*(xalpha^127)+src1[xx+1]*xalpha);
  1976. dst2[i]=(src2[xx]*(xalpha^127)+src2[xx+1]*xalpha);
  1977. xpos+=xInc;
  1978. }
  1979. for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--) {
  1980. dst1[i] = src1[srcW-1]*128;
  1981. dst2[i] = src2[srcW-1]*128;
  1982. }
  1983. }
  1984. static av_always_inline void hcscale(SwsContext *c, int16_t *dst1, int16_t *dst2, int dstWidth,
  1985. const uint8_t *src1, const uint8_t *src2,
  1986. int srcW, int xInc, const int16_t *hChrFilter,
  1987. const int16_t *hChrFilterPos, int hChrFilterSize,
  1988. uint8_t *formatConvBuffer, uint32_t *pal)
  1989. {
  1990. if (c->chrToYV12) {
  1991. uint8_t *buf2 = formatConvBuffer + FFALIGN(srcW*2+78, 16);
  1992. c->chrToYV12(formatConvBuffer, buf2, src1, src2, srcW, pal);
  1993. src1= formatConvBuffer;
  1994. src2= buf2;
  1995. }
  1996. if (av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1 < 8 && c->scalingBpp == 16 && !isAnyRGB(c->srcFormat)) {
  1997. uint8_t *buf2 = (formatConvBuffer + FFALIGN(srcW * 2+78, 16));
  1998. c->scale8To16Rv((uint16_t *) formatConvBuffer, src1, srcW);
  1999. c->scale8To16Rv((uint16_t *) buf2, src2, srcW);
  2000. src1 = formatConvBuffer;
  2001. src2 = buf2;
  2002. }
  2003. if (c->hScale16) {
  2004. int shift= isAnyRGB(c->srcFormat) || c->srcFormat==PIX_FMT_PAL8 ? 13 : av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1;
  2005. c->hScale16(dst1, dstWidth, (const uint16_t*)src1, srcW, xInc, hChrFilter, hChrFilterPos, hChrFilterSize, shift);
  2006. c->hScale16(dst2, dstWidth, (const uint16_t*)src2, srcW, xInc, hChrFilter, hChrFilterPos, hChrFilterSize, shift);
  2007. } else if (!c->hcscale_fast) {
  2008. c->hScale(c, dst1, dstWidth, src1, hChrFilter, hChrFilterPos, hChrFilterSize);
  2009. c->hScale(c, dst2, dstWidth, src2, hChrFilter, hChrFilterPos, hChrFilterSize);
  2010. } else { // fast bilinear upscale / crap downscale
  2011. c->hcscale_fast(c, dst1, dst2, dstWidth, src1, src2, srcW, xInc);
  2012. }
  2013. if (c->chrConvertRange)
  2014. c->chrConvertRange(dst1, dst2, dstWidth);
  2015. if (av_pix_fmt_descriptors[c->dstFormat].comp[0].depth_minus1 < 15 && c->scalingBpp == 16) {
  2016. c->scale19To15Fw(dst1, (int32_t *) dst1, dstWidth);
  2017. c->scale19To15Fw(dst2, (int32_t *) dst2, dstWidth);
  2018. }
  2019. }
  2020. static av_always_inline void
  2021. find_c_packed_planar_out_funcs(SwsContext *c,
  2022. yuv2planar1_fn *yuv2yuv1, yuv2planarX_fn *yuv2yuvX,
  2023. yuv2packed1_fn *yuv2packed1, yuv2packed2_fn *yuv2packed2,
  2024. yuv2packedX_fn *yuv2packedX)
  2025. {
  2026. enum PixelFormat dstFormat = c->dstFormat;
  2027. if (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21) {
  2028. *yuv2yuvX = yuv2nv12X_c;
  2029. } else if (is16BPS(dstFormat)) {
  2030. *yuv2yuvX = isBE(dstFormat) ? yuv2yuvX16BE_c : yuv2yuvX16LE_c;
  2031. } else if (is9_OR_10BPS(dstFormat)) {
  2032. if (av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1 == 8) {
  2033. *yuv2yuvX = isBE(dstFormat) ? yuv2yuvX9BE_c : yuv2yuvX9LE_c;
  2034. } else {
  2035. *yuv2yuvX = isBE(dstFormat) ? yuv2yuvX10BE_c : yuv2yuvX10LE_c;
  2036. }
  2037. } else {
  2038. *yuv2yuv1 = yuv2yuv1_c;
  2039. *yuv2yuvX = yuv2yuvX_c;
  2040. }
  2041. if(c->flags & SWS_FULL_CHR_H_INT) {
  2042. switch (dstFormat) {
  2043. case PIX_FMT_RGBA:
  2044. #if CONFIG_SMALL
  2045. *yuv2packedX = yuv2rgba32_full_X_c;
  2046. #else
  2047. #if CONFIG_SWSCALE_ALPHA
  2048. if (c->alpPixBuf) {
  2049. *yuv2packedX = yuv2rgba32_full_X_c;
  2050. } else
  2051. #endif /* CONFIG_SWSCALE_ALPHA */
  2052. {
  2053. *yuv2packedX = yuv2rgbx32_full_X_c;
  2054. }
  2055. #endif /* !CONFIG_SMALL */
  2056. break;
  2057. case PIX_FMT_ARGB:
  2058. #if CONFIG_SMALL
  2059. *yuv2packedX = yuv2argb32_full_X_c;
  2060. #else
  2061. #if CONFIG_SWSCALE_ALPHA
  2062. if (c->alpPixBuf) {
  2063. *yuv2packedX = yuv2argb32_full_X_c;
  2064. } else
  2065. #endif /* CONFIG_SWSCALE_ALPHA */
  2066. {
  2067. *yuv2packedX = yuv2xrgb32_full_X_c;
  2068. }
  2069. #endif /* !CONFIG_SMALL */
  2070. break;
  2071. case PIX_FMT_BGRA:
  2072. #if CONFIG_SMALL
  2073. *yuv2packedX = yuv2bgra32_full_X_c;
  2074. #else
  2075. #if CONFIG_SWSCALE_ALPHA
  2076. if (c->alpPixBuf) {
  2077. *yuv2packedX = yuv2bgra32_full_X_c;
  2078. } else
  2079. #endif /* CONFIG_SWSCALE_ALPHA */
  2080. {
  2081. *yuv2packedX = yuv2bgrx32_full_X_c;
  2082. }
  2083. #endif /* !CONFIG_SMALL */
  2084. break;
  2085. case PIX_FMT_ABGR:
  2086. #if CONFIG_SMALL
  2087. *yuv2packedX = yuv2abgr32_full_X_c;
  2088. #else
  2089. #if CONFIG_SWSCALE_ALPHA
  2090. if (c->alpPixBuf) {
  2091. *yuv2packedX = yuv2abgr32_full_X_c;
  2092. } else
  2093. #endif /* CONFIG_SWSCALE_ALPHA */
  2094. {
  2095. *yuv2packedX = yuv2xbgr32_full_X_c;
  2096. }
  2097. #endif /* !CONFIG_SMALL */
  2098. break;
  2099. case PIX_FMT_RGB24:
  2100. *yuv2packedX = yuv2rgb24_full_X_c;
  2101. break;
  2102. case PIX_FMT_BGR24:
  2103. *yuv2packedX = yuv2bgr24_full_X_c;
  2104. break;
  2105. }
  2106. if(!*yuv2packedX)
  2107. goto YUV_PACKED;
  2108. } else {
  2109. YUV_PACKED:
  2110. switch (dstFormat) {
  2111. case PIX_FMT_GRAY16BE:
  2112. *yuv2packed1 = yuv2gray16BE_1_c;
  2113. *yuv2packed2 = yuv2gray16BE_2_c;
  2114. *yuv2packedX = yuv2gray16BE_X_c;
  2115. break;
  2116. case PIX_FMT_GRAY16LE:
  2117. *yuv2packed1 = yuv2gray16LE_1_c;
  2118. *yuv2packed2 = yuv2gray16LE_2_c;
  2119. *yuv2packedX = yuv2gray16LE_X_c;
  2120. break;
  2121. case PIX_FMT_MONOWHITE:
  2122. *yuv2packed1 = yuv2monowhite_1_c;
  2123. *yuv2packed2 = yuv2monowhite_2_c;
  2124. *yuv2packedX = yuv2monowhite_X_c;
  2125. break;
  2126. case PIX_FMT_MONOBLACK:
  2127. *yuv2packed1 = yuv2monoblack_1_c;
  2128. *yuv2packed2 = yuv2monoblack_2_c;
  2129. *yuv2packedX = yuv2monoblack_X_c;
  2130. break;
  2131. case PIX_FMT_YUYV422:
  2132. *yuv2packed1 = yuv2yuyv422_1_c;
  2133. *yuv2packed2 = yuv2yuyv422_2_c;
  2134. *yuv2packedX = yuv2yuyv422_X_c;
  2135. break;
  2136. case PIX_FMT_UYVY422:
  2137. *yuv2packed1 = yuv2uyvy422_1_c;
  2138. *yuv2packed2 = yuv2uyvy422_2_c;
  2139. *yuv2packedX = yuv2uyvy422_X_c;
  2140. break;
  2141. case PIX_FMT_RGB48LE:
  2142. *yuv2packed1 = yuv2rgb48le_1_c;
  2143. *yuv2packed2 = yuv2rgb48le_2_c;
  2144. *yuv2packedX = yuv2rgb48le_X_c;
  2145. break;
  2146. case PIX_FMT_RGB48BE:
  2147. *yuv2packed1 = yuv2rgb48be_1_c;
  2148. *yuv2packed2 = yuv2rgb48be_2_c;
  2149. *yuv2packedX = yuv2rgb48be_X_c;
  2150. break;
  2151. case PIX_FMT_BGR48LE:
  2152. *yuv2packed1 = yuv2bgr48le_1_c;
  2153. *yuv2packed2 = yuv2bgr48le_2_c;
  2154. *yuv2packedX = yuv2bgr48le_X_c;
  2155. break;
  2156. case PIX_FMT_BGR48BE:
  2157. *yuv2packed1 = yuv2bgr48be_1_c;
  2158. *yuv2packed2 = yuv2bgr48be_2_c;
  2159. *yuv2packedX = yuv2bgr48be_X_c;
  2160. break;
  2161. case PIX_FMT_RGB32:
  2162. case PIX_FMT_BGR32:
  2163. #if CONFIG_SMALL
  2164. *yuv2packed1 = yuv2rgb32_1_c;
  2165. *yuv2packed2 = yuv2rgb32_2_c;
  2166. *yuv2packedX = yuv2rgb32_X_c;
  2167. #else
  2168. #if CONFIG_SWSCALE_ALPHA
  2169. if (c->alpPixBuf) {
  2170. *yuv2packed1 = yuv2rgba32_1_c;
  2171. *yuv2packed2 = yuv2rgba32_2_c;
  2172. *yuv2packedX = yuv2rgba32_X_c;
  2173. } else
  2174. #endif /* CONFIG_SWSCALE_ALPHA */
  2175. {
  2176. *yuv2packed1 = yuv2rgbx32_1_c;
  2177. *yuv2packed2 = yuv2rgbx32_2_c;
  2178. *yuv2packedX = yuv2rgbx32_X_c;
  2179. }
  2180. #endif /* !CONFIG_SMALL */
  2181. break;
  2182. case PIX_FMT_RGB32_1:
  2183. case PIX_FMT_BGR32_1:
  2184. #if CONFIG_SMALL
  2185. *yuv2packed1 = yuv2rgb32_1_1_c;
  2186. *yuv2packed2 = yuv2rgb32_1_2_c;
  2187. *yuv2packedX = yuv2rgb32_1_X_c;
  2188. #else
  2189. #if CONFIG_SWSCALE_ALPHA
  2190. if (c->alpPixBuf) {
  2191. *yuv2packed1 = yuv2rgba32_1_1_c;
  2192. *yuv2packed2 = yuv2rgba32_1_2_c;
  2193. *yuv2packedX = yuv2rgba32_1_X_c;
  2194. } else
  2195. #endif /* CONFIG_SWSCALE_ALPHA */
  2196. {
  2197. *yuv2packed1 = yuv2rgbx32_1_1_c;
  2198. *yuv2packed2 = yuv2rgbx32_1_2_c;
  2199. *yuv2packedX = yuv2rgbx32_1_X_c;
  2200. }
  2201. #endif /* !CONFIG_SMALL */
  2202. break;
  2203. case PIX_FMT_RGB24:
  2204. *yuv2packed1 = yuv2rgb24_1_c;
  2205. *yuv2packed2 = yuv2rgb24_2_c;
  2206. *yuv2packedX = yuv2rgb24_X_c;
  2207. break;
  2208. case PIX_FMT_BGR24:
  2209. *yuv2packed1 = yuv2bgr24_1_c;
  2210. *yuv2packed2 = yuv2bgr24_2_c;
  2211. *yuv2packedX = yuv2bgr24_X_c;
  2212. break;
  2213. case PIX_FMT_RGB565LE:
  2214. case PIX_FMT_RGB565BE:
  2215. case PIX_FMT_BGR565LE:
  2216. case PIX_FMT_BGR565BE:
  2217. *yuv2packed1 = yuv2rgb16_1_c;
  2218. *yuv2packed2 = yuv2rgb16_2_c;
  2219. *yuv2packedX = yuv2rgb16_X_c;
  2220. break;
  2221. case PIX_FMT_RGB555LE:
  2222. case PIX_FMT_RGB555BE:
  2223. case PIX_FMT_BGR555LE:
  2224. case PIX_FMT_BGR555BE:
  2225. *yuv2packed1 = yuv2rgb15_1_c;
  2226. *yuv2packed2 = yuv2rgb15_2_c;
  2227. *yuv2packedX = yuv2rgb15_X_c;
  2228. break;
  2229. case PIX_FMT_RGB444LE:
  2230. case PIX_FMT_RGB444BE:
  2231. case PIX_FMT_BGR444LE:
  2232. case PIX_FMT_BGR444BE:
  2233. *yuv2packed1 = yuv2rgb12_1_c;
  2234. *yuv2packed2 = yuv2rgb12_2_c;
  2235. *yuv2packedX = yuv2rgb12_X_c;
  2236. break;
  2237. case PIX_FMT_RGB8:
  2238. case PIX_FMT_BGR8:
  2239. *yuv2packed1 = yuv2rgb8_1_c;
  2240. *yuv2packed2 = yuv2rgb8_2_c;
  2241. *yuv2packedX = yuv2rgb8_X_c;
  2242. break;
  2243. case PIX_FMT_RGB4:
  2244. case PIX_FMT_BGR4:
  2245. *yuv2packed1 = yuv2rgb4_1_c;
  2246. *yuv2packed2 = yuv2rgb4_2_c;
  2247. *yuv2packedX = yuv2rgb4_X_c;
  2248. break;
  2249. case PIX_FMT_RGB4_BYTE:
  2250. case PIX_FMT_BGR4_BYTE:
  2251. *yuv2packed1 = yuv2rgb4b_1_c;
  2252. *yuv2packed2 = yuv2rgb4b_2_c;
  2253. *yuv2packedX = yuv2rgb4b_X_c;
  2254. break;
  2255. }
  2256. }
  2257. }
  2258. #define DEBUG_SWSCALE_BUFFERS 0
  2259. #define DEBUG_BUFFERS(...) if (DEBUG_SWSCALE_BUFFERS) av_log(c, AV_LOG_DEBUG, __VA_ARGS__)
  2260. static int swScale(SwsContext *c, const uint8_t* src[],
  2261. int srcStride[], int srcSliceY,
  2262. int srcSliceH, uint8_t* dst[], int dstStride[])
  2263. {
  2264. /* load a few things into local vars to make the code more readable? and faster */
  2265. const int srcW= c->srcW;
  2266. const int dstW= c->dstW;
  2267. const int dstH= c->dstH;
  2268. const int chrDstW= c->chrDstW;
  2269. const int chrSrcW= c->chrSrcW;
  2270. const int lumXInc= c->lumXInc;
  2271. const int chrXInc= c->chrXInc;
  2272. const enum PixelFormat dstFormat= c->dstFormat;
  2273. const int flags= c->flags;
  2274. int16_t *vLumFilterPos= c->vLumFilterPos;
  2275. int16_t *vChrFilterPos= c->vChrFilterPos;
  2276. int16_t *hLumFilterPos= c->hLumFilterPos;
  2277. int16_t *hChrFilterPos= c->hChrFilterPos;
  2278. int16_t *vLumFilter= c->vLumFilter;
  2279. int16_t *vChrFilter= c->vChrFilter;
  2280. int16_t *hLumFilter= c->hLumFilter;
  2281. int16_t *hChrFilter= c->hChrFilter;
  2282. int32_t *lumMmxFilter= c->lumMmxFilter;
  2283. int32_t *chrMmxFilter= c->chrMmxFilter;
  2284. int32_t av_unused *alpMmxFilter= c->alpMmxFilter;
  2285. const int vLumFilterSize= c->vLumFilterSize;
  2286. const int vChrFilterSize= c->vChrFilterSize;
  2287. const int hLumFilterSize= c->hLumFilterSize;
  2288. const int hChrFilterSize= c->hChrFilterSize;
  2289. int16_t **lumPixBuf= c->lumPixBuf;
  2290. int16_t **chrUPixBuf= c->chrUPixBuf;
  2291. int16_t **chrVPixBuf= c->chrVPixBuf;
  2292. int16_t **alpPixBuf= c->alpPixBuf;
  2293. const int vLumBufSize= c->vLumBufSize;
  2294. const int vChrBufSize= c->vChrBufSize;
  2295. uint8_t *formatConvBuffer= c->formatConvBuffer;
  2296. const int chrSrcSliceY= srcSliceY >> c->chrSrcVSubSample;
  2297. const int chrSrcSliceH= -((-srcSliceH) >> c->chrSrcVSubSample);
  2298. int lastDstY;
  2299. uint32_t *pal=c->pal_yuv;
  2300. int should_dither= isNBPS(c->srcFormat) || is16BPS(c->srcFormat);
  2301. yuv2planar1_fn yuv2yuv1 = c->yuv2yuv1;
  2302. yuv2planarX_fn yuv2yuvX = c->yuv2yuvX;
  2303. yuv2packed1_fn yuv2packed1 = c->yuv2packed1;
  2304. yuv2packed2_fn yuv2packed2 = c->yuv2packed2;
  2305. yuv2packedX_fn yuv2packedX = c->yuv2packedX;
  2306. /* vars which will change and which we need to store back in the context */
  2307. int dstY= c->dstY;
  2308. int lumBufIndex= c->lumBufIndex;
  2309. int chrBufIndex= c->chrBufIndex;
  2310. int lastInLumBuf= c->lastInLumBuf;
  2311. int lastInChrBuf= c->lastInChrBuf;
  2312. if (isPacked(c->srcFormat)) {
  2313. src[0]=
  2314. src[1]=
  2315. src[2]=
  2316. src[3]= src[0];
  2317. srcStride[0]=
  2318. srcStride[1]=
  2319. srcStride[2]=
  2320. srcStride[3]= srcStride[0];
  2321. }
  2322. srcStride[1]<<= c->vChrDrop;
  2323. srcStride[2]<<= c->vChrDrop;
  2324. DEBUG_BUFFERS("swScale() %p[%d] %p[%d] %p[%d] %p[%d] -> %p[%d] %p[%d] %p[%d] %p[%d]\n",
  2325. src[0], srcStride[0], src[1], srcStride[1], src[2], srcStride[2], src[3], srcStride[3],
  2326. dst[0], dstStride[0], dst[1], dstStride[1], dst[2], dstStride[2], dst[3], dstStride[3]);
  2327. DEBUG_BUFFERS("srcSliceY: %d srcSliceH: %d dstY: %d dstH: %d\n",
  2328. srcSliceY, srcSliceH, dstY, dstH);
  2329. DEBUG_BUFFERS("vLumFilterSize: %d vLumBufSize: %d vChrFilterSize: %d vChrBufSize: %d\n",
  2330. vLumFilterSize, vLumBufSize, vChrFilterSize, vChrBufSize);
  2331. if (dstStride[0]%8 !=0 || dstStride[1]%8 !=0 || dstStride[2]%8 !=0 || dstStride[3]%8 != 0) {
  2332. static int warnedAlready=0; //FIXME move this into the context perhaps
  2333. if (flags & SWS_PRINT_INFO && !warnedAlready) {
  2334. av_log(c, AV_LOG_WARNING, "Warning: dstStride is not aligned!\n"
  2335. " ->cannot do aligned memory accesses anymore\n");
  2336. warnedAlready=1;
  2337. }
  2338. }
  2339. /* Note the user might start scaling the picture in the middle so this
  2340. will not get executed. This is not really intended but works
  2341. currently, so people might do it. */
  2342. if (srcSliceY ==0) {
  2343. lumBufIndex=-1;
  2344. chrBufIndex=-1;
  2345. dstY=0;
  2346. lastInLumBuf= -1;
  2347. lastInChrBuf= -1;
  2348. }
  2349. if (!should_dither) {
  2350. c->chrDither8 = c->lumDither8 = ff_sws_pb_64;
  2351. }
  2352. lastDstY= dstY;
  2353. for (;dstY < dstH; dstY++) {
  2354. const int chrDstY= dstY>>c->chrDstVSubSample;
  2355. uint8_t *dest[4] = {
  2356. dst[0] + dstStride[0] * dstY,
  2357. dst[1] + dstStride[1] * chrDstY,
  2358. dst[2] + dstStride[2] * chrDstY,
  2359. (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? dst[3] + dstStride[3] * dstY : NULL,
  2360. };
  2361. const int firstLumSrcY= vLumFilterPos[dstY]; //First line needed as input
  2362. const int firstLumSrcY2= vLumFilterPos[FFMIN(dstY | ((1<<c->chrDstVSubSample) - 1), dstH-1)];
  2363. const int firstChrSrcY= vChrFilterPos[chrDstY]; //First line needed as input
  2364. int lastLumSrcY= firstLumSrcY + vLumFilterSize -1; // Last line needed as input
  2365. int lastLumSrcY2=firstLumSrcY2+ vLumFilterSize -1; // Last line needed as input
  2366. int lastChrSrcY= firstChrSrcY + vChrFilterSize -1; // Last line needed as input
  2367. int enough_lines;
  2368. //handle holes (FAST_BILINEAR & weird filters)
  2369. if (firstLumSrcY > lastInLumBuf) lastInLumBuf= firstLumSrcY-1;
  2370. if (firstChrSrcY > lastInChrBuf) lastInChrBuf= firstChrSrcY-1;
  2371. assert(firstLumSrcY >= lastInLumBuf - vLumBufSize + 1);
  2372. assert(firstChrSrcY >= lastInChrBuf - vChrBufSize + 1);
  2373. DEBUG_BUFFERS("dstY: %d\n", dstY);
  2374. DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n",
  2375. firstLumSrcY, lastLumSrcY, lastInLumBuf);
  2376. DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n",
  2377. firstChrSrcY, lastChrSrcY, lastInChrBuf);
  2378. // Do we have enough lines in this slice to output the dstY line
  2379. enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH && lastChrSrcY < -((-srcSliceY - srcSliceH)>>c->chrSrcVSubSample);
  2380. if (!enough_lines) {
  2381. lastLumSrcY = srcSliceY + srcSliceH - 1;
  2382. lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1;
  2383. DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n",
  2384. lastLumSrcY, lastChrSrcY);
  2385. }
  2386. //Do horizontal scaling
  2387. while(lastInLumBuf < lastLumSrcY) {
  2388. const uint8_t *src1= src[0]+(lastInLumBuf + 1 - srcSliceY)*srcStride[0];
  2389. const uint8_t *src2= src[3]+(lastInLumBuf + 1 - srcSliceY)*srcStride[3];
  2390. lumBufIndex++;
  2391. assert(lumBufIndex < 2*vLumBufSize);
  2392. assert(lastInLumBuf + 1 - srcSliceY < srcSliceH);
  2393. assert(lastInLumBuf + 1 - srcSliceY >= 0);
  2394. hyscale(c, lumPixBuf[ lumBufIndex ], dstW, src1, srcW, lumXInc,
  2395. hLumFilter, hLumFilterPos, hLumFilterSize,
  2396. formatConvBuffer,
  2397. pal, 0);
  2398. if (CONFIG_SWSCALE_ALPHA && alpPixBuf)
  2399. hyscale(c, alpPixBuf[ lumBufIndex ], dstW, src2, srcW,
  2400. lumXInc, hLumFilter, hLumFilterPos, hLumFilterSize,
  2401. formatConvBuffer,
  2402. pal, 1);
  2403. lastInLumBuf++;
  2404. DEBUG_BUFFERS("\t\tlumBufIndex %d: lastInLumBuf: %d\n",
  2405. lumBufIndex, lastInLumBuf);
  2406. }
  2407. while(lastInChrBuf < lastChrSrcY) {
  2408. const uint8_t *src1= src[1]+(lastInChrBuf + 1 - chrSrcSliceY)*srcStride[1];
  2409. const uint8_t *src2= src[2]+(lastInChrBuf + 1 - chrSrcSliceY)*srcStride[2];
  2410. chrBufIndex++;
  2411. assert(chrBufIndex < 2*vChrBufSize);
  2412. assert(lastInChrBuf + 1 - chrSrcSliceY < (chrSrcSliceH));
  2413. assert(lastInChrBuf + 1 - chrSrcSliceY >= 0);
  2414. //FIXME replace parameters through context struct (some at least)
  2415. if (c->needs_hcscale)
  2416. hcscale(c, chrUPixBuf[chrBufIndex], chrVPixBuf[chrBufIndex],
  2417. chrDstW, src1, src2, chrSrcW, chrXInc,
  2418. hChrFilter, hChrFilterPos, hChrFilterSize,
  2419. formatConvBuffer, pal);
  2420. lastInChrBuf++;
  2421. DEBUG_BUFFERS("\t\tchrBufIndex %d: lastInChrBuf: %d\n",
  2422. chrBufIndex, lastInChrBuf);
  2423. }
  2424. //wrap buf index around to stay inside the ring buffer
  2425. if (lumBufIndex >= vLumBufSize) lumBufIndex-= vLumBufSize;
  2426. if (chrBufIndex >= vChrBufSize) chrBufIndex-= vChrBufSize;
  2427. if (!enough_lines)
  2428. break; //we can't output a dstY line so let's try with the next slice
  2429. #if HAVE_MMX
  2430. updateMMXDitherTables(c, dstY, lumBufIndex, chrBufIndex, lastInLumBuf, lastInChrBuf);
  2431. #endif
  2432. if (should_dither) {
  2433. c->chrDither8 = dither_8x8_128[chrDstY & 7];
  2434. c->lumDither8 = dither_8x8_128[dstY & 7];
  2435. }
  2436. if (dstY >= dstH-2) {
  2437. // hmm looks like we can't use MMX here without overwriting this array's tail
  2438. find_c_packed_planar_out_funcs(c, &yuv2yuv1, &yuv2yuvX,
  2439. &yuv2packed1, &yuv2packed2,
  2440. &yuv2packedX);
  2441. }
  2442. {
  2443. const int16_t **lumSrcPtr= (const int16_t **) lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
  2444. const int16_t **chrUSrcPtr= (const int16_t **) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  2445. const int16_t **chrVSrcPtr= (const int16_t **) chrVPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  2446. const int16_t **alpSrcPtr= (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? (const int16_t **) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
  2447. if (isPlanarYUV(dstFormat) || dstFormat==PIX_FMT_GRAY8) { //YV12 like
  2448. const int chrSkipMask= (1<<c->chrDstVSubSample)-1;
  2449. if ((dstY&chrSkipMask) || isGray(dstFormat))
  2450. dest[1] = dest[2] = NULL; //FIXME split functions in lumi / chromi
  2451. if (c->yuv2yuv1 && vLumFilterSize == 1 && vChrFilterSize == 1) { // unscaled YV12
  2452. const int16_t *alpBuf= (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? alpSrcPtr[0] : NULL;
  2453. yuv2yuv1(c, lumSrcPtr[0], chrUSrcPtr[0], chrVSrcPtr[0], alpBuf,
  2454. dest, dstW, chrDstW);
  2455. } else { //General YV12
  2456. yuv2yuvX(c, vLumFilter + dstY * vLumFilterSize,
  2457. lumSrcPtr, vLumFilterSize,
  2458. vChrFilter + chrDstY * vChrFilterSize,
  2459. chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  2460. alpSrcPtr, dest, dstW, chrDstW);
  2461. }
  2462. } else {
  2463. assert(lumSrcPtr + vLumFilterSize - 1 < lumPixBuf + vLumBufSize*2);
  2464. assert(chrUSrcPtr + vChrFilterSize - 1 < chrUPixBuf + vChrBufSize*2);
  2465. if (c->yuv2packed1 && vLumFilterSize == 1 && vChrFilterSize == 2) { //unscaled RGB
  2466. int chrAlpha = vChrFilter[2 * dstY + 1];
  2467. yuv2packed1(c, *lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
  2468. alpPixBuf ? *alpSrcPtr : NULL,
  2469. dest[0], dstW, chrAlpha, dstY);
  2470. } else if (c->yuv2packed2 && vLumFilterSize == 2 && vChrFilterSize == 2) { //bilinear upscale RGB
  2471. int lumAlpha = vLumFilter[2 * dstY + 1];
  2472. int chrAlpha = vChrFilter[2 * dstY + 1];
  2473. lumMmxFilter[2] =
  2474. lumMmxFilter[3] = vLumFilter[2 * dstY ] * 0x10001;
  2475. chrMmxFilter[2] =
  2476. chrMmxFilter[3] = vChrFilter[2 * chrDstY] * 0x10001;
  2477. yuv2packed2(c, lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
  2478. alpPixBuf ? alpSrcPtr : NULL,
  2479. dest[0], dstW, lumAlpha, chrAlpha, dstY);
  2480. } else { //general RGB
  2481. yuv2packedX(c, vLumFilter + dstY * vLumFilterSize,
  2482. lumSrcPtr, vLumFilterSize,
  2483. vChrFilter + dstY * vChrFilterSize,
  2484. chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  2485. alpSrcPtr, dest[0], dstW, dstY);
  2486. }
  2487. }
  2488. }
  2489. }
  2490. if ((dstFormat == PIX_FMT_YUVA420P) && !alpPixBuf)
  2491. fillPlane(dst[3], dstStride[3], dstW, dstY-lastDstY, lastDstY, 255);
  2492. #if HAVE_MMX2
  2493. if (av_get_cpu_flags() & AV_CPU_FLAG_MMX2)
  2494. __asm__ volatile("sfence":::"memory");
  2495. #endif
  2496. emms_c();
  2497. /* store changed local vars back in the context */
  2498. c->dstY= dstY;
  2499. c->lumBufIndex= lumBufIndex;
  2500. c->chrBufIndex= chrBufIndex;
  2501. c->lastInLumBuf= lastInLumBuf;
  2502. c->lastInChrBuf= lastInChrBuf;
  2503. return dstY - lastDstY;
  2504. }
  2505. static av_cold void sws_init_swScale_c(SwsContext *c)
  2506. {
  2507. enum PixelFormat srcFormat = c->srcFormat;
  2508. find_c_packed_planar_out_funcs(c, &c->yuv2yuv1, &c->yuv2yuvX,
  2509. &c->yuv2packed1, &c->yuv2packed2,
  2510. &c->yuv2packedX);
  2511. c->chrToYV12 = NULL;
  2512. switch(srcFormat) {
  2513. case PIX_FMT_YUYV422 : c->chrToYV12 = yuy2ToUV_c; break;
  2514. case PIX_FMT_UYVY422 : c->chrToYV12 = uyvyToUV_c; break;
  2515. case PIX_FMT_NV12 : c->chrToYV12 = nv12ToUV_c; break;
  2516. case PIX_FMT_NV21 : c->chrToYV12 = nv21ToUV_c; break;
  2517. case PIX_FMT_RGB8 :
  2518. case PIX_FMT_BGR8 :
  2519. case PIX_FMT_PAL8 :
  2520. case PIX_FMT_BGR4_BYTE:
  2521. case PIX_FMT_RGB4_BYTE: c->chrToYV12 = palToUV_c; break;
  2522. case PIX_FMT_YUV444P9BE:
  2523. case PIX_FMT_YUV420P9BE:
  2524. case PIX_FMT_YUV444P10BE:
  2525. case PIX_FMT_YUV422P10BE:
  2526. case PIX_FMT_YUV420P10BE: c->hScale16= HAVE_BIGENDIAN ? hScale16N_c : hScale16NX_c; break;
  2527. case PIX_FMT_YUV444P9LE:
  2528. case PIX_FMT_YUV420P9LE:
  2529. case PIX_FMT_YUV422P10LE:
  2530. case PIX_FMT_YUV420P10LE:
  2531. case PIX_FMT_YUV444P10LE: c->hScale16= HAVE_BIGENDIAN ? hScale16NX_c : hScale16N_c; break;
  2532. #if HAVE_BIGENDIAN
  2533. case PIX_FMT_YUV420P16LE:
  2534. case PIX_FMT_YUV422P16LE:
  2535. case PIX_FMT_YUV444P16LE: c->chrToYV12 = bswap16UV_c; break;
  2536. #else
  2537. case PIX_FMT_YUV420P16BE:
  2538. case PIX_FMT_YUV422P16BE:
  2539. case PIX_FMT_YUV444P16BE: c->chrToYV12 = bswap16UV_c; break;
  2540. #endif
  2541. }
  2542. if (c->chrSrcHSubSample) {
  2543. switch(srcFormat) {
  2544. case PIX_FMT_RGB48BE : c->chrToYV12 = rgb48BEToUV_half_c; break;
  2545. case PIX_FMT_RGB48LE : c->chrToYV12 = rgb48LEToUV_half_c; break;
  2546. case PIX_FMT_BGR48BE : c->chrToYV12 = bgr48BEToUV_half_c; break;
  2547. case PIX_FMT_BGR48LE : c->chrToYV12 = bgr48LEToUV_half_c; break;
  2548. case PIX_FMT_RGB32 : c->chrToYV12 = bgr32ToUV_half_c; break;
  2549. case PIX_FMT_RGB32_1 : c->chrToYV12 = bgr321ToUV_half_c; break;
  2550. case PIX_FMT_BGR24 : c->chrToYV12 = bgr24ToUV_half_c; break;
  2551. case PIX_FMT_BGR565LE: c->chrToYV12 = bgr16leToUV_half_c; break;
  2552. case PIX_FMT_BGR565BE: c->chrToYV12 = bgr16beToUV_half_c; break;
  2553. case PIX_FMT_BGR555LE: c->chrToYV12 = bgr15leToUV_half_c; break;
  2554. case PIX_FMT_BGR555BE: c->chrToYV12 = bgr15beToUV_half_c; break;
  2555. case PIX_FMT_BGR32 : c->chrToYV12 = rgb32ToUV_half_c; break;
  2556. case PIX_FMT_BGR32_1 : c->chrToYV12 = rgb321ToUV_half_c; break;
  2557. case PIX_FMT_RGB24 : c->chrToYV12 = rgb24ToUV_half_c; break;
  2558. case PIX_FMT_RGB565LE: c->chrToYV12 = rgb16leToUV_half_c; break;
  2559. case PIX_FMT_RGB565BE: c->chrToYV12 = rgb16beToUV_half_c; break;
  2560. case PIX_FMT_RGB555LE: c->chrToYV12 = rgb15leToUV_half_c; break;
  2561. case PIX_FMT_RGB555BE: c->chrToYV12 = rgb15beToUV_half_c; break;
  2562. }
  2563. } else {
  2564. switch(srcFormat) {
  2565. case PIX_FMT_RGB48BE : c->chrToYV12 = rgb48BEToUV_c; break;
  2566. case PIX_FMT_RGB48LE : c->chrToYV12 = rgb48LEToUV_c; break;
  2567. case PIX_FMT_BGR48BE : c->chrToYV12 = bgr48BEToUV_c; break;
  2568. case PIX_FMT_BGR48LE : c->chrToYV12 = bgr48LEToUV_c; break;
  2569. case PIX_FMT_RGB32 : c->chrToYV12 = bgr32ToUV_c; break;
  2570. case PIX_FMT_RGB32_1 : c->chrToYV12 = bgr321ToUV_c; break;
  2571. case PIX_FMT_BGR24 : c->chrToYV12 = bgr24ToUV_c; break;
  2572. case PIX_FMT_BGR565LE: c->chrToYV12 = bgr16leToUV_c; break;
  2573. case PIX_FMT_BGR565BE: c->chrToYV12 = bgr16beToUV_c; break;
  2574. case PIX_FMT_BGR555LE: c->chrToYV12 = bgr15leToUV_c; break;
  2575. case PIX_FMT_BGR555BE: c->chrToYV12 = bgr15beToUV_c; break;
  2576. case PIX_FMT_BGR32 : c->chrToYV12 = rgb32ToUV_c; break;
  2577. case PIX_FMT_BGR32_1 : c->chrToYV12 = rgb321ToUV_c; break;
  2578. case PIX_FMT_RGB24 : c->chrToYV12 = rgb24ToUV_c; break;
  2579. case PIX_FMT_RGB565LE: c->chrToYV12 = rgb16leToUV_c; break;
  2580. case PIX_FMT_RGB565BE: c->chrToYV12 = rgb16beToUV_c; break;
  2581. case PIX_FMT_RGB555LE: c->chrToYV12 = rgb15leToUV_c; break;
  2582. case PIX_FMT_RGB555BE: c->chrToYV12 = rgb15beToUV_c; break;
  2583. }
  2584. }
  2585. c->lumToYV12 = NULL;
  2586. c->alpToYV12 = NULL;
  2587. switch (srcFormat) {
  2588. #if HAVE_BIGENDIAN
  2589. case PIX_FMT_YUV420P16LE:
  2590. case PIX_FMT_YUV422P16LE:
  2591. case PIX_FMT_YUV444P16LE:
  2592. case PIX_FMT_GRAY16LE: c->lumToYV12 = bswap16Y_c; break;
  2593. #else
  2594. case PIX_FMT_YUV420P16BE:
  2595. case PIX_FMT_YUV422P16BE:
  2596. case PIX_FMT_YUV444P16BE:
  2597. case PIX_FMT_GRAY16BE: c->lumToYV12 = bswap16Y_c; break;
  2598. #endif
  2599. case PIX_FMT_YUYV422 :
  2600. case PIX_FMT_Y400A : c->lumToYV12 = yuy2ToY_c; break;
  2601. case PIX_FMT_UYVY422 : c->lumToYV12 = uyvyToY_c; break;
  2602. case PIX_FMT_BGR24 : c->lumToYV12 = bgr24ToY_c; break;
  2603. case PIX_FMT_BGR565LE : c->lumToYV12 = bgr16leToY_c; break;
  2604. case PIX_FMT_BGR565BE : c->lumToYV12 = bgr16beToY_c; break;
  2605. case PIX_FMT_BGR555LE : c->lumToYV12 = bgr15leToY_c; break;
  2606. case PIX_FMT_BGR555BE : c->lumToYV12 = bgr15beToY_c; break;
  2607. case PIX_FMT_RGB24 : c->lumToYV12 = rgb24ToY_c; break;
  2608. case PIX_FMT_RGB565LE : c->lumToYV12 = rgb16leToY_c; break;
  2609. case PIX_FMT_RGB565BE : c->lumToYV12 = rgb16beToY_c; break;
  2610. case PIX_FMT_RGB555LE : c->lumToYV12 = rgb15leToY_c; break;
  2611. case PIX_FMT_RGB555BE : c->lumToYV12 = rgb15beToY_c; break;
  2612. case PIX_FMT_RGB8 :
  2613. case PIX_FMT_BGR8 :
  2614. case PIX_FMT_PAL8 :
  2615. case PIX_FMT_BGR4_BYTE:
  2616. case PIX_FMT_RGB4_BYTE: c->lumToYV12 = palToY_c; break;
  2617. case PIX_FMT_MONOBLACK: c->lumToYV12 = monoblack2Y_c; break;
  2618. case PIX_FMT_MONOWHITE: c->lumToYV12 = monowhite2Y_c; break;
  2619. case PIX_FMT_RGB32 : c->lumToYV12 = bgr32ToY_c; break;
  2620. case PIX_FMT_RGB32_1: c->lumToYV12 = bgr321ToY_c; break;
  2621. case PIX_FMT_BGR32 : c->lumToYV12 = rgb32ToY_c; break;
  2622. case PIX_FMT_BGR32_1: c->lumToYV12 = rgb321ToY_c; break;
  2623. case PIX_FMT_RGB48BE: c->lumToYV12 = rgb48BEToY_c; break;
  2624. case PIX_FMT_RGB48LE: c->lumToYV12 = rgb48LEToY_c; break;
  2625. case PIX_FMT_BGR48BE: c->lumToYV12 = bgr48BEToY_c; break;
  2626. case PIX_FMT_BGR48LE: c->lumToYV12 = bgr48LEToY_c; break;
  2627. }
  2628. if (c->alpPixBuf) {
  2629. switch (srcFormat) {
  2630. case PIX_FMT_BGRA:
  2631. case PIX_FMT_RGBA: c->alpToYV12 = rgbaToA_c; break;
  2632. case PIX_FMT_ABGR:
  2633. case PIX_FMT_ARGB: c->alpToYV12 = abgrToA_c; break;
  2634. case PIX_FMT_Y400A: c->alpToYV12 = uyvyToY_c; break;
  2635. case PIX_FMT_PAL8 : c->alpToYV12 = palToA_c; break;
  2636. }
  2637. }
  2638. if((isAnyRGB(c->srcFormat) && av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1<15)
  2639. || c->srcFormat == PIX_FMT_PAL8)
  2640. c->hScale16= hScale16N_c;
  2641. if (c->scalingBpp == 8) {
  2642. c->hScale = hScale_c;
  2643. if (c->flags & SWS_FAST_BILINEAR) {
  2644. c->hyscale_fast = hyscale_fast_c;
  2645. c->hcscale_fast = hcscale_fast_c;
  2646. }
  2647. if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
  2648. if (c->srcRange) {
  2649. c->lumConvertRange = lumRangeFromJpeg_c;
  2650. c->chrConvertRange = chrRangeFromJpeg_c;
  2651. } else {
  2652. c->lumConvertRange = lumRangeToJpeg_c;
  2653. c->chrConvertRange = chrRangeToJpeg_c;
  2654. }
  2655. }
  2656. } else {
  2657. if(c->hScale16 == hScale16NX_c && !isAnyRGB(c->srcFormat)){
  2658. c->chrToYV12 = bswap16UV_c;
  2659. c->lumToYV12 = bswap16Y_c;
  2660. }
  2661. c->hScale16 = NULL;
  2662. c->hScale = hScale16_c;
  2663. c->scale19To15Fw = scale19To15Fw_c;
  2664. c->scale8To16Rv = scale8To16Rv_c;
  2665. if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
  2666. if (c->srcRange) {
  2667. c->lumConvertRange = lumRangeFromJpeg16_c;
  2668. c->chrConvertRange = chrRangeFromJpeg16_c;
  2669. } else {
  2670. c->lumConvertRange = lumRangeToJpeg16_c;
  2671. c->chrConvertRange = chrRangeToJpeg16_c;
  2672. }
  2673. }
  2674. }
  2675. if (!(isGray(srcFormat) || isGray(c->dstFormat) ||
  2676. srcFormat == PIX_FMT_MONOBLACK || srcFormat == PIX_FMT_MONOWHITE))
  2677. c->needs_hcscale = 1;
  2678. }
  2679. SwsFunc ff_getSwsFunc(SwsContext *c)
  2680. {
  2681. sws_init_swScale_c(c);
  2682. if (HAVE_MMX)
  2683. ff_sws_init_swScale_mmx(c);
  2684. if (HAVE_ALTIVEC)
  2685. ff_sws_init_swScale_altivec(c);
  2686. return swScale;
  2687. }