swscale.c 103 KB

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