swscale.c 102 KB

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