output.c 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087
  1. /*
  2. * Copyright (C) 2001-2012 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <math.h>
  21. #include <stdint.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "libavutil/attributes.h"
  25. #include "libavutil/avutil.h"
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/bswap.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/mem_internal.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "config.h"
  33. #include "rgb2rgb.h"
  34. #include "swscale.h"
  35. #include "swscale_internal.h"
  36. DECLARE_ALIGNED(8, const uint8_t, ff_dither_2x2_4)[][8] = {
  37. { 1, 3, 1, 3, 1, 3, 1, 3, },
  38. { 2, 0, 2, 0, 2, 0, 2, 0, },
  39. { 1, 3, 1, 3, 1, 3, 1, 3, },
  40. };
  41. DECLARE_ALIGNED(8, const uint8_t, ff_dither_2x2_8)[][8] = {
  42. { 6, 2, 6, 2, 6, 2, 6, 2, },
  43. { 0, 4, 0, 4, 0, 4, 0, 4, },
  44. { 6, 2, 6, 2, 6, 2, 6, 2, },
  45. };
  46. DECLARE_ALIGNED(8, const uint8_t, ff_dither_4x4_16)[][8] = {
  47. { 8, 4, 11, 7, 8, 4, 11, 7, },
  48. { 2, 14, 1, 13, 2, 14, 1, 13, },
  49. { 10, 6, 9, 5, 10, 6, 9, 5, },
  50. { 0, 12, 3, 15, 0, 12, 3, 15, },
  51. { 8, 4, 11, 7, 8, 4, 11, 7, },
  52. };
  53. DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_32)[][8] = {
  54. { 17, 9, 23, 15, 16, 8, 22, 14, },
  55. { 5, 29, 3, 27, 4, 28, 2, 26, },
  56. { 21, 13, 19, 11, 20, 12, 18, 10, },
  57. { 0, 24, 6, 30, 1, 25, 7, 31, },
  58. { 16, 8, 22, 14, 17, 9, 23, 15, },
  59. { 4, 28, 2, 26, 5, 29, 3, 27, },
  60. { 20, 12, 18, 10, 21, 13, 19, 11, },
  61. { 1, 25, 7, 31, 0, 24, 6, 30, },
  62. { 17, 9, 23, 15, 16, 8, 22, 14, },
  63. };
  64. DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_73)[][8] = {
  65. { 0, 55, 14, 68, 3, 58, 17, 72, },
  66. { 37, 18, 50, 32, 40, 22, 54, 35, },
  67. { 9, 64, 5, 59, 13, 67, 8, 63, },
  68. { 46, 27, 41, 23, 49, 31, 44, 26, },
  69. { 2, 57, 16, 71, 1, 56, 15, 70, },
  70. { 39, 21, 52, 34, 38, 19, 51, 33, },
  71. { 11, 66, 7, 62, 10, 65, 6, 60, },
  72. { 48, 30, 43, 25, 47, 29, 42, 24, },
  73. { 0, 55, 14, 68, 3, 58, 17, 72, },
  74. };
  75. #if 1
  76. DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[][8] = {
  77. {117, 62, 158, 103, 113, 58, 155, 100, },
  78. { 34, 199, 21, 186, 31, 196, 17, 182, },
  79. {144, 89, 131, 76, 141, 86, 127, 72, },
  80. { 0, 165, 41, 206, 10, 175, 52, 217, },
  81. {110, 55, 151, 96, 120, 65, 162, 107, },
  82. { 28, 193, 14, 179, 38, 203, 24, 189, },
  83. {138, 83, 124, 69, 148, 93, 134, 79, },
  84. { 7, 172, 48, 213, 3, 168, 45, 210, },
  85. {117, 62, 158, 103, 113, 58, 155, 100, },
  86. };
  87. #elif 1
  88. // tries to correct a gamma of 1.5
  89. DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[][8] = {
  90. { 0, 143, 18, 200, 2, 156, 25, 215, },
  91. { 78, 28, 125, 64, 89, 36, 138, 74, },
  92. { 10, 180, 3, 161, 16, 195, 8, 175, },
  93. {109, 51, 93, 38, 121, 60, 105, 47, },
  94. { 1, 152, 23, 210, 0, 147, 20, 205, },
  95. { 85, 33, 134, 71, 81, 30, 130, 67, },
  96. { 14, 190, 6, 171, 12, 185, 5, 166, },
  97. {117, 57, 101, 44, 113, 54, 97, 41, },
  98. { 0, 143, 18, 200, 2, 156, 25, 215, },
  99. };
  100. #elif 1
  101. // tries to correct a gamma of 2.0
  102. DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[][8] = {
  103. { 0, 124, 8, 193, 0, 140, 12, 213, },
  104. { 55, 14, 104, 42, 66, 19, 119, 52, },
  105. { 3, 168, 1, 145, 6, 187, 3, 162, },
  106. { 86, 31, 70, 21, 99, 39, 82, 28, },
  107. { 0, 134, 11, 206, 0, 129, 9, 200, },
  108. { 62, 17, 114, 48, 58, 16, 109, 45, },
  109. { 5, 181, 2, 157, 4, 175, 1, 151, },
  110. { 95, 36, 78, 26, 90, 34, 74, 24, },
  111. { 0, 124, 8, 193, 0, 140, 12, 213, },
  112. };
  113. #else
  114. // tries to correct a gamma of 2.5
  115. DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[][8] = {
  116. { 0, 107, 3, 187, 0, 125, 6, 212, },
  117. { 39, 7, 86, 28, 49, 11, 102, 36, },
  118. { 1, 158, 0, 131, 3, 180, 1, 151, },
  119. { 68, 19, 52, 12, 81, 25, 64, 17, },
  120. { 0, 119, 5, 203, 0, 113, 4, 195, },
  121. { 45, 9, 96, 33, 42, 8, 91, 30, },
  122. { 2, 172, 1, 144, 2, 165, 0, 137, },
  123. { 77, 23, 60, 15, 72, 21, 56, 14, },
  124. { 0, 107, 3, 187, 0, 125, 6, 212, },
  125. };
  126. #endif
  127. #define output_pixel(pos, val, bias, signedness) \
  128. if (big_endian) { \
  129. AV_WB16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
  130. } else { \
  131. AV_WL16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
  132. }
  133. static av_always_inline void
  134. yuv2plane1_16_c_template(const int32_t *src, uint16_t *dest, int dstW,
  135. int big_endian, int output_bits)
  136. {
  137. int i;
  138. int shift = 3;
  139. av_assert0(output_bits == 16);
  140. for (i = 0; i < dstW; i++) {
  141. int val = src[i] + (1 << (shift - 1));
  142. output_pixel(&dest[i], val, 0, uint);
  143. }
  144. }
  145. static av_always_inline void
  146. yuv2planeX_16_c_template(const int16_t *filter, int filterSize,
  147. const int32_t **src, uint16_t *dest, int dstW,
  148. int big_endian, int output_bits)
  149. {
  150. int i;
  151. int shift = 15;
  152. av_assert0(output_bits == 16);
  153. for (i = 0; i < dstW; i++) {
  154. int val = 1 << (shift - 1);
  155. int j;
  156. /* range of val is [0,0x7FFFFFFF], so 31 bits, but with lanczos/spline
  157. * filters (or anything with negative coeffs, the range can be slightly
  158. * wider in both directions. To account for this overflow, we subtract
  159. * a constant so it always fits in the signed range (assuming a
  160. * reasonable filterSize), and re-add that at the end. */
  161. val -= 0x40000000;
  162. for (j = 0; j < filterSize; j++)
  163. val += src[j][i] * (unsigned)filter[j];
  164. output_pixel(&dest[i], val, 0x8000, int);
  165. }
  166. }
  167. static av_always_inline void
  168. yuv2nv12cX_16_c_template(int big_endian, const uint8_t *chrDither,
  169. const int16_t *chrFilter, int chrFilterSize,
  170. const int16_t **chrUSrc, const int16_t **chrVSrc,
  171. uint8_t *dest8, int chrDstW, int output_bits)
  172. {
  173. uint16_t *dest = (uint16_t*)dest8;
  174. const int32_t **uSrc = (const int32_t **)chrUSrc;
  175. const int32_t **vSrc = (const int32_t **)chrVSrc;
  176. int shift = 15;
  177. int i, j;
  178. av_assert0(output_bits == 16);
  179. for (i = 0; i < chrDstW; i++) {
  180. int u = 1 << (shift - 1);
  181. int v = 1 << (shift - 1);
  182. /* See yuv2planeX_16_c_template for details. */
  183. u -= 0x40000000;
  184. v -= 0x40000000;
  185. for (j = 0; j < chrFilterSize; j++) {
  186. u += uSrc[j][i] * (unsigned)chrFilter[j];
  187. v += vSrc[j][i] * (unsigned)chrFilter[j];
  188. }
  189. output_pixel(&dest[2*i] , u, 0x8000, int);
  190. output_pixel(&dest[2*i+1], v, 0x8000, int);
  191. }
  192. }
  193. static av_always_inline void
  194. yuv2plane1_float_c_template(const int32_t *src, float *dest, int dstW)
  195. {
  196. static const int big_endian = HAVE_BIGENDIAN;
  197. static const int shift = 3;
  198. static const float float_mult = 1.0f / 65535.0f;
  199. int i, val;
  200. uint16_t val_uint;
  201. for (i = 0; i < dstW; ++i){
  202. val = src[i] + (1 << (shift - 1));
  203. output_pixel(&val_uint, val, 0, uint);
  204. dest[i] = float_mult * (float)val_uint;
  205. }
  206. }
  207. static av_always_inline void
  208. yuv2plane1_float_bswap_c_template(const int32_t *src, uint32_t *dest, int dstW)
  209. {
  210. static const int big_endian = HAVE_BIGENDIAN;
  211. static const int shift = 3;
  212. static const float float_mult = 1.0f / 65535.0f;
  213. int i, val;
  214. uint16_t val_uint;
  215. for (i = 0; i < dstW; ++i){
  216. val = src[i] + (1 << (shift - 1));
  217. output_pixel(&val_uint, val, 0, uint);
  218. dest[i] = av_bswap32(av_float2int(float_mult * (float)val_uint));
  219. }
  220. }
  221. static av_always_inline void
  222. yuv2planeX_float_c_template(const int16_t *filter, int filterSize, const int32_t **src,
  223. float *dest, int dstW)
  224. {
  225. static const int big_endian = HAVE_BIGENDIAN;
  226. static const int shift = 15;
  227. static const float float_mult = 1.0f / 65535.0f;
  228. int i, j, val;
  229. uint16_t val_uint;
  230. for (i = 0; i < dstW; ++i){
  231. val = (1 << (shift - 1)) - 0x40000000;
  232. for (j = 0; j < filterSize; ++j){
  233. val += src[j][i] * (unsigned)filter[j];
  234. }
  235. output_pixel(&val_uint, val, 0x8000, int);
  236. dest[i] = float_mult * (float)val_uint;
  237. }
  238. }
  239. static av_always_inline void
  240. yuv2planeX_float_bswap_c_template(const int16_t *filter, int filterSize, const int32_t **src,
  241. uint32_t *dest, int dstW)
  242. {
  243. static const int big_endian = HAVE_BIGENDIAN;
  244. static const int shift = 15;
  245. static const float float_mult = 1.0f / 65535.0f;
  246. int i, j, val;
  247. uint16_t val_uint;
  248. for (i = 0; i < dstW; ++i){
  249. val = (1 << (shift - 1)) - 0x40000000;
  250. for (j = 0; j < filterSize; ++j){
  251. val += src[j][i] * (unsigned)filter[j];
  252. }
  253. output_pixel(&val_uint, val, 0x8000, int);
  254. dest[i] = av_bswap32(av_float2int(float_mult * (float)val_uint));
  255. }
  256. }
  257. #define yuv2plane1_float(template, dest_type, BE_LE) \
  258. static void yuv2plane1_float ## BE_LE ## _c(const int16_t *src, uint8_t *dest, int dstW, \
  259. const uint8_t *dither, int offset) \
  260. { \
  261. template((const int32_t *)src, (dest_type *)dest, dstW); \
  262. }
  263. #define yuv2planeX_float(template, dest_type, BE_LE) \
  264. static void yuv2planeX_float ## BE_LE ## _c(const int16_t *filter, int filterSize, \
  265. const int16_t **src, uint8_t *dest, int dstW, \
  266. const uint8_t *dither, int offset) \
  267. { \
  268. template(filter, filterSize, (const int32_t **)src, (dest_type *)dest, dstW); \
  269. }
  270. #if HAVE_BIGENDIAN
  271. yuv2plane1_float(yuv2plane1_float_c_template, float, BE)
  272. yuv2plane1_float(yuv2plane1_float_bswap_c_template, uint32_t, LE)
  273. yuv2planeX_float(yuv2planeX_float_c_template, float, BE)
  274. yuv2planeX_float(yuv2planeX_float_bswap_c_template, uint32_t, LE)
  275. #else
  276. yuv2plane1_float(yuv2plane1_float_c_template, float, LE)
  277. yuv2plane1_float(yuv2plane1_float_bswap_c_template, uint32_t, BE)
  278. yuv2planeX_float(yuv2planeX_float_c_template, float, LE)
  279. yuv2planeX_float(yuv2planeX_float_bswap_c_template, uint32_t, BE)
  280. #endif
  281. #undef output_pixel
  282. #define output_pixel(pos, val) \
  283. if (big_endian) { \
  284. AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  285. } else { \
  286. AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  287. }
  288. static av_always_inline void
  289. yuv2plane1_10_c_template(const int16_t *src, uint16_t *dest, int dstW,
  290. int big_endian, int output_bits)
  291. {
  292. int i;
  293. int shift = 15 - output_bits;
  294. for (i = 0; i < dstW; i++) {
  295. int val = src[i] + (1 << (shift - 1));
  296. output_pixel(&dest[i], val);
  297. }
  298. }
  299. static av_always_inline void
  300. yuv2planeX_10_c_template(const int16_t *filter, int filterSize,
  301. const int16_t **src, uint16_t *dest, int dstW,
  302. int big_endian, int output_bits)
  303. {
  304. int i;
  305. int shift = 11 + 16 - output_bits;
  306. for (i = 0; i < dstW; i++) {
  307. int val = 1 << (shift - 1);
  308. int j;
  309. for (j = 0; j < filterSize; j++)
  310. val += src[j][i] * filter[j];
  311. output_pixel(&dest[i], val);
  312. }
  313. }
  314. #undef output_pixel
  315. #define yuv2NBPS(bits, BE_LE, is_be, template_size, typeX_t) \
  316. static void yuv2plane1_ ## bits ## BE_LE ## _c(const int16_t *src, \
  317. uint8_t *dest, int dstW, \
  318. const uint8_t *dither, int offset)\
  319. { \
  320. yuv2plane1_ ## template_size ## _c_template((const typeX_t *) src, \
  321. (uint16_t *) dest, dstW, is_be, bits); \
  322. }\
  323. static void yuv2planeX_ ## bits ## BE_LE ## _c(const int16_t *filter, int filterSize, \
  324. const int16_t **src, uint8_t *dest, int dstW, \
  325. const uint8_t *dither, int offset)\
  326. { \
  327. yuv2planeX_## template_size ## _c_template(filter, \
  328. filterSize, (const typeX_t **) src, \
  329. (uint16_t *) dest, dstW, is_be, bits); \
  330. }
  331. yuv2NBPS( 9, BE, 1, 10, int16_t)
  332. yuv2NBPS( 9, LE, 0, 10, int16_t)
  333. yuv2NBPS(10, BE, 1, 10, int16_t)
  334. yuv2NBPS(10, LE, 0, 10, int16_t)
  335. yuv2NBPS(12, BE, 1, 10, int16_t)
  336. yuv2NBPS(12, LE, 0, 10, int16_t)
  337. yuv2NBPS(14, BE, 1, 10, int16_t)
  338. yuv2NBPS(14, LE, 0, 10, int16_t)
  339. yuv2NBPS(16, BE, 1, 16, int32_t)
  340. yuv2NBPS(16, LE, 0, 16, int32_t)
  341. static void yuv2nv12cX_16LE_c(enum AVPixelFormat dstFormat, const uint8_t *chrDither,
  342. const int16_t *chrFilter, int chrFilterSize,
  343. const int16_t **chrUSrc, const int16_t **chrVSrc,
  344. uint8_t *dest8, int chrDstW)
  345. {
  346. yuv2nv12cX_16_c_template(0, chrDither, chrFilter, chrFilterSize, chrUSrc, chrVSrc, dest8, chrDstW, 16);
  347. }
  348. static void yuv2nv12cX_16BE_c(enum AVPixelFormat dstFormat, const uint8_t *chrDither,
  349. const int16_t *chrFilter, int chrFilterSize,
  350. const int16_t **chrUSrc, const int16_t **chrVSrc,
  351. uint8_t *dest8, int chrDstW)
  352. {
  353. yuv2nv12cX_16_c_template(1, chrDither, chrFilter, chrFilterSize, chrUSrc, chrVSrc, dest8, chrDstW, 16);
  354. }
  355. static void yuv2planeX_8_c(const int16_t *filter, int filterSize,
  356. const int16_t **src, uint8_t *dest, int dstW,
  357. const uint8_t *dither, int offset)
  358. {
  359. int i;
  360. for (i=0; i<dstW; i++) {
  361. int val = dither[(i + offset) & 7] << 12;
  362. int j;
  363. for (j=0; j<filterSize; j++)
  364. val += src[j][i] * filter[j];
  365. dest[i]= av_clip_uint8(val>>19);
  366. }
  367. }
  368. static void yuv2plane1_8_c(const int16_t *src, uint8_t *dest, int dstW,
  369. const uint8_t *dither, int offset)
  370. {
  371. int i;
  372. for (i=0; i<dstW; i++) {
  373. int val = (src[i] + dither[(i + offset) & 7]) >> 7;
  374. dest[i]= av_clip_uint8(val);
  375. }
  376. }
  377. static void yuv2nv12cX_c(enum AVPixelFormat dstFormat, const uint8_t *chrDither,
  378. const int16_t *chrFilter, int chrFilterSize,
  379. const int16_t **chrUSrc, const int16_t **chrVSrc,
  380. uint8_t *dest, int chrDstW)
  381. {
  382. int i;
  383. if (!isSwappedChroma(dstFormat))
  384. for (i=0; i<chrDstW; i++) {
  385. int u = chrDither[i & 7] << 12;
  386. int v = chrDither[(i + 3) & 7] << 12;
  387. int j;
  388. for (j=0; j<chrFilterSize; j++) {
  389. u += chrUSrc[j][i] * chrFilter[j];
  390. v += chrVSrc[j][i] * chrFilter[j];
  391. }
  392. dest[2*i]= av_clip_uint8(u>>19);
  393. dest[2*i+1]= av_clip_uint8(v>>19);
  394. }
  395. else
  396. for (i=0; i<chrDstW; i++) {
  397. int u = chrDither[i & 7] << 12;
  398. int v = chrDither[(i + 3) & 7] << 12;
  399. int j;
  400. for (j=0; j<chrFilterSize; j++) {
  401. u += chrUSrc[j][i] * chrFilter[j];
  402. v += chrVSrc[j][i] * chrFilter[j];
  403. }
  404. dest[2*i]= av_clip_uint8(v>>19);
  405. dest[2*i+1]= av_clip_uint8(u>>19);
  406. }
  407. }
  408. #define output_pixel(pos, val) \
  409. if (big_endian) { \
  410. AV_WB16(pos, av_clip_uintp2(val >> shift, 10) << 6); \
  411. } else { \
  412. AV_WL16(pos, av_clip_uintp2(val >> shift, 10) << 6); \
  413. }
  414. static void yuv2p010l1_c(const int16_t *src,
  415. uint16_t *dest, int dstW,
  416. int big_endian)
  417. {
  418. int i;
  419. int shift = 5;
  420. for (i = 0; i < dstW; i++) {
  421. int val = src[i] + (1 << (shift - 1));
  422. output_pixel(&dest[i], val);
  423. }
  424. }
  425. static void yuv2p010lX_c(const int16_t *filter, int filterSize,
  426. const int16_t **src, uint16_t *dest, int dstW,
  427. int big_endian)
  428. {
  429. int i, j;
  430. int shift = 17;
  431. for (i = 0; i < dstW; i++) {
  432. int val = 1 << (shift - 1);
  433. for (j = 0; j < filterSize; j++)
  434. val += src[j][i] * filter[j];
  435. output_pixel(&dest[i], val);
  436. }
  437. }
  438. static void yuv2p010cX_c(int big_endian, const uint8_t *chrDither,
  439. const int16_t *chrFilter, int chrFilterSize,
  440. const int16_t **chrUSrc, const int16_t **chrVSrc,
  441. uint8_t *dest8, int chrDstW)
  442. {
  443. uint16_t *dest = (uint16_t*)dest8;
  444. int shift = 17;
  445. int i, j;
  446. for (i = 0; i < chrDstW; i++) {
  447. int u = 1 << (shift - 1);
  448. int v = 1 << (shift - 1);
  449. for (j = 0; j < chrFilterSize; j++) {
  450. u += chrUSrc[j][i] * chrFilter[j];
  451. v += chrVSrc[j][i] * chrFilter[j];
  452. }
  453. output_pixel(&dest[2*i] , u);
  454. output_pixel(&dest[2*i+1], v);
  455. }
  456. }
  457. static void yuv2p010l1_LE_c(const int16_t *src,
  458. uint8_t *dest, int dstW,
  459. const uint8_t *dither, int offset)
  460. {
  461. yuv2p010l1_c(src, (uint16_t*)dest, dstW, 0);
  462. }
  463. static void yuv2p010l1_BE_c(const int16_t *src,
  464. uint8_t *dest, int dstW,
  465. const uint8_t *dither, int offset)
  466. {
  467. yuv2p010l1_c(src, (uint16_t*)dest, dstW, 1);
  468. }
  469. static void yuv2p010lX_LE_c(const int16_t *filter, int filterSize,
  470. const int16_t **src, uint8_t *dest, int dstW,
  471. const uint8_t *dither, int offset)
  472. {
  473. yuv2p010lX_c(filter, filterSize, src, (uint16_t*)dest, dstW, 0);
  474. }
  475. static void yuv2p010lX_BE_c(const int16_t *filter, int filterSize,
  476. const int16_t **src, uint8_t *dest, int dstW,
  477. const uint8_t *dither, int offset)
  478. {
  479. yuv2p010lX_c(filter, filterSize, src, (uint16_t*)dest, dstW, 1);
  480. }
  481. static void yuv2p010cX_LE_c(enum AVPixelFormat dstFormat, const uint8_t *chrDither,
  482. const int16_t *chrFilter, int chrFilterSize,
  483. const int16_t **chrUSrc, const int16_t **chrVSrc,
  484. uint8_t *dest8, int chrDstW)
  485. {
  486. yuv2p010cX_c(0, chrDither, chrFilter, chrFilterSize, chrUSrc, chrVSrc, dest8, chrDstW);
  487. }
  488. static void yuv2p010cX_BE_c(enum AVPixelFormat dstFormat, const uint8_t *chrDither,
  489. const int16_t *chrFilter, int chrFilterSize,
  490. const int16_t **chrUSrc, const int16_t **chrVSrc,
  491. uint8_t *dest8, int chrDstW)
  492. {
  493. yuv2p010cX_c(1, chrDither, chrFilter, chrFilterSize, chrUSrc, chrVSrc, dest8, chrDstW);
  494. }
  495. #undef output_pixel
  496. #define accumulate_bit(acc, val) \
  497. acc <<= 1; \
  498. acc |= (val) >= 234
  499. #define output_pixel(pos, acc) \
  500. if (target == AV_PIX_FMT_MONOBLACK) { \
  501. pos = acc; \
  502. } else { \
  503. pos = ~acc; \
  504. }
  505. static av_always_inline void
  506. yuv2mono_X_c_template(SwsContext *c, const int16_t *lumFilter,
  507. const int16_t **lumSrc, int lumFilterSize,
  508. const int16_t *chrFilter, const int16_t **chrUSrc,
  509. const int16_t **chrVSrc, int chrFilterSize,
  510. const int16_t **alpSrc, uint8_t *dest, int dstW,
  511. int y, enum AVPixelFormat target)
  512. {
  513. const uint8_t * const d128 = ff_dither_8x8_220[y&7];
  514. int i;
  515. unsigned acc = 0;
  516. int err = 0;
  517. for (i = 0; i < dstW; i += 2) {
  518. int j;
  519. int Y1 = 1 << 18;
  520. int Y2 = 1 << 18;
  521. for (j = 0; j < lumFilterSize; j++) {
  522. Y1 += lumSrc[j][i] * lumFilter[j];
  523. Y2 += lumSrc[j][i+1] * lumFilter[j];
  524. }
  525. Y1 >>= 19;
  526. Y2 >>= 19;
  527. if ((Y1 | Y2) & 0x100) {
  528. Y1 = av_clip_uint8(Y1);
  529. Y2 = av_clip_uint8(Y2);
  530. }
  531. if (c->dither == SWS_DITHER_ED) {
  532. Y1 += (7*err + 1*c->dither_error[0][i] + 5*c->dither_error[0][i+1] + 3*c->dither_error[0][i+2] + 8 - 256)>>4;
  533. c->dither_error[0][i] = err;
  534. acc = 2*acc + (Y1 >= 128);
  535. Y1 -= 220*(acc&1);
  536. err = Y2 + ((7*Y1 + 1*c->dither_error[0][i+1] + 5*c->dither_error[0][i+2] + 3*c->dither_error[0][i+3] + 8 - 256)>>4);
  537. c->dither_error[0][i+1] = Y1;
  538. acc = 2*acc + (err >= 128);
  539. err -= 220*(acc&1);
  540. } else {
  541. accumulate_bit(acc, Y1 + d128[(i + 0) & 7]);
  542. accumulate_bit(acc, Y2 + d128[(i + 1) & 7]);
  543. }
  544. if ((i & 7) == 6) {
  545. output_pixel(*dest++, acc);
  546. }
  547. }
  548. c->dither_error[0][i] = err;
  549. if (i & 6) {
  550. output_pixel(*dest, acc);
  551. }
  552. }
  553. static av_always_inline void
  554. yuv2mono_2_c_template(SwsContext *c, const int16_t *buf[2],
  555. const int16_t *ubuf[2], const int16_t *vbuf[2],
  556. const int16_t *abuf[2], uint8_t *dest, int dstW,
  557. int yalpha, int uvalpha, int y,
  558. enum AVPixelFormat target)
  559. {
  560. const int16_t *buf0 = buf[0], *buf1 = buf[1];
  561. const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
  562. int yalpha1 = 4096 - yalpha;
  563. int i;
  564. av_assert2(yalpha <= 4096U);
  565. if (c->dither == SWS_DITHER_ED) {
  566. int err = 0;
  567. int acc = 0;
  568. for (i = 0; i < dstW; i +=2) {
  569. int Y;
  570. Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
  571. Y += (7*err + 1*c->dither_error[0][i] + 5*c->dither_error[0][i+1] + 3*c->dither_error[0][i+2] + 8 - 256)>>4;
  572. c->dither_error[0][i] = err;
  573. acc = 2*acc + (Y >= 128);
  574. Y -= 220*(acc&1);
  575. err = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
  576. err += (7*Y + 1*c->dither_error[0][i+1] + 5*c->dither_error[0][i+2] + 3*c->dither_error[0][i+3] + 8 - 256)>>4;
  577. c->dither_error[0][i+1] = Y;
  578. acc = 2*acc + (err >= 128);
  579. err -= 220*(acc&1);
  580. if ((i & 7) == 6)
  581. output_pixel(*dest++, acc);
  582. }
  583. c->dither_error[0][i] = err;
  584. } else {
  585. for (i = 0; i < dstW; i += 8) {
  586. int Y, acc = 0;
  587. Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
  588. accumulate_bit(acc, Y + d128[0]);
  589. Y = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
  590. accumulate_bit(acc, Y + d128[1]);
  591. Y = (buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19;
  592. accumulate_bit(acc, Y + d128[2]);
  593. Y = (buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19;
  594. accumulate_bit(acc, Y + d128[3]);
  595. Y = (buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19;
  596. accumulate_bit(acc, Y + d128[4]);
  597. Y = (buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19;
  598. accumulate_bit(acc, Y + d128[5]);
  599. Y = (buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19;
  600. accumulate_bit(acc, Y + d128[6]);
  601. Y = (buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19;
  602. accumulate_bit(acc, Y + d128[7]);
  603. output_pixel(*dest++, acc);
  604. }
  605. }
  606. }
  607. static av_always_inline void
  608. yuv2mono_1_c_template(SwsContext *c, const int16_t *buf0,
  609. const int16_t *ubuf[2], const int16_t *vbuf[2],
  610. const int16_t *abuf0, uint8_t *dest, int dstW,
  611. int uvalpha, int y, enum AVPixelFormat target)
  612. {
  613. const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
  614. int i;
  615. if (c->dither == SWS_DITHER_ED) {
  616. int err = 0;
  617. int acc = 0;
  618. for (i = 0; i < dstW; i +=2) {
  619. int Y;
  620. Y = ((buf0[i + 0] + 64) >> 7);
  621. Y += (7*err + 1*c->dither_error[0][i] + 5*c->dither_error[0][i+1] + 3*c->dither_error[0][i+2] + 8 - 256)>>4;
  622. c->dither_error[0][i] = err;
  623. acc = 2*acc + (Y >= 128);
  624. Y -= 220*(acc&1);
  625. err = ((buf0[i + 1] + 64) >> 7);
  626. err += (7*Y + 1*c->dither_error[0][i+1] + 5*c->dither_error[0][i+2] + 3*c->dither_error[0][i+3] + 8 - 256)>>4;
  627. c->dither_error[0][i+1] = Y;
  628. acc = 2*acc + (err >= 128);
  629. err -= 220*(acc&1);
  630. if ((i & 7) == 6)
  631. output_pixel(*dest++, acc);
  632. }
  633. c->dither_error[0][i] = err;
  634. } else {
  635. for (i = 0; i < dstW; i += 8) {
  636. int acc = 0;
  637. accumulate_bit(acc, ((buf0[i + 0] + 64) >> 7) + d128[0]);
  638. accumulate_bit(acc, ((buf0[i + 1] + 64) >> 7) + d128[1]);
  639. accumulate_bit(acc, ((buf0[i + 2] + 64) >> 7) + d128[2]);
  640. accumulate_bit(acc, ((buf0[i + 3] + 64) >> 7) + d128[3]);
  641. accumulate_bit(acc, ((buf0[i + 4] + 64) >> 7) + d128[4]);
  642. accumulate_bit(acc, ((buf0[i + 5] + 64) >> 7) + d128[5]);
  643. accumulate_bit(acc, ((buf0[i + 6] + 64) >> 7) + d128[6]);
  644. accumulate_bit(acc, ((buf0[i + 7] + 64) >> 7) + d128[7]);
  645. output_pixel(*dest++, acc);
  646. }
  647. }
  648. }
  649. #undef output_pixel
  650. #undef accumulate_bit
  651. #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
  652. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  653. const int16_t **lumSrc, int lumFilterSize, \
  654. const int16_t *chrFilter, const int16_t **chrUSrc, \
  655. const int16_t **chrVSrc, int chrFilterSize, \
  656. const int16_t **alpSrc, uint8_t *dest, int dstW, \
  657. int y) \
  658. { \
  659. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  660. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  661. alpSrc, dest, dstW, y, fmt); \
  662. } \
  663. \
  664. static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
  665. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  666. const int16_t *abuf[2], uint8_t *dest, int dstW, \
  667. int yalpha, int uvalpha, int y) \
  668. { \
  669. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  670. dest, dstW, yalpha, uvalpha, y, fmt); \
  671. } \
  672. \
  673. static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
  674. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  675. const int16_t *abuf0, uint8_t *dest, int dstW, \
  676. int uvalpha, int y) \
  677. { \
  678. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
  679. abuf0, dest, dstW, uvalpha, \
  680. y, fmt); \
  681. }
  682. YUV2PACKEDWRAPPER(yuv2mono,, white, AV_PIX_FMT_MONOWHITE)
  683. YUV2PACKEDWRAPPER(yuv2mono,, black, AV_PIX_FMT_MONOBLACK)
  684. #define output_pixels(pos, Y1, U, Y2, V) \
  685. if (target == AV_PIX_FMT_YUYV422) { \
  686. dest[pos + 0] = Y1; \
  687. dest[pos + 1] = U; \
  688. dest[pos + 2] = Y2; \
  689. dest[pos + 3] = V; \
  690. } else if (target == AV_PIX_FMT_YVYU422) { \
  691. dest[pos + 0] = Y1; \
  692. dest[pos + 1] = V; \
  693. dest[pos + 2] = Y2; \
  694. dest[pos + 3] = U; \
  695. } else { /* AV_PIX_FMT_UYVY422 */ \
  696. dest[pos + 0] = U; \
  697. dest[pos + 1] = Y1; \
  698. dest[pos + 2] = V; \
  699. dest[pos + 3] = Y2; \
  700. }
  701. static av_always_inline void
  702. yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
  703. const int16_t **lumSrc, int lumFilterSize,
  704. const int16_t *chrFilter, const int16_t **chrUSrc,
  705. const int16_t **chrVSrc, int chrFilterSize,
  706. const int16_t **alpSrc, uint8_t *dest, int dstW,
  707. int y, enum AVPixelFormat target)
  708. {
  709. int i;
  710. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  711. int j;
  712. int Y1 = 1 << 18;
  713. int Y2 = 1 << 18;
  714. int U = 1 << 18;
  715. int V = 1 << 18;
  716. for (j = 0; j < lumFilterSize; j++) {
  717. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  718. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  719. }
  720. for (j = 0; j < chrFilterSize; j++) {
  721. U += chrUSrc[j][i] * chrFilter[j];
  722. V += chrVSrc[j][i] * chrFilter[j];
  723. }
  724. Y1 >>= 19;
  725. Y2 >>= 19;
  726. U >>= 19;
  727. V >>= 19;
  728. if ((Y1 | Y2 | U | V) & 0x100) {
  729. Y1 = av_clip_uint8(Y1);
  730. Y2 = av_clip_uint8(Y2);
  731. U = av_clip_uint8(U);
  732. V = av_clip_uint8(V);
  733. }
  734. output_pixels(4*i, Y1, U, Y2, V);
  735. }
  736. }
  737. static av_always_inline void
  738. yuv2422_2_c_template(SwsContext *c, const int16_t *buf[2],
  739. const int16_t *ubuf[2], const int16_t *vbuf[2],
  740. const int16_t *abuf[2], uint8_t *dest, int dstW,
  741. int yalpha, int uvalpha, int y,
  742. enum AVPixelFormat target)
  743. {
  744. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  745. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  746. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  747. int yalpha1 = 4096 - yalpha;
  748. int uvalpha1 = 4096 - uvalpha;
  749. int i;
  750. av_assert2(yalpha <= 4096U);
  751. av_assert2(uvalpha <= 4096U);
  752. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  753. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
  754. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
  755. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
  756. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
  757. if ((Y1 | Y2 | U | V) & 0x100) {
  758. Y1 = av_clip_uint8(Y1);
  759. Y2 = av_clip_uint8(Y2);
  760. U = av_clip_uint8(U);
  761. V = av_clip_uint8(V);
  762. }
  763. output_pixels(i * 4, Y1, U, Y2, V);
  764. }
  765. }
  766. static av_always_inline void
  767. yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
  768. const int16_t *ubuf[2], const int16_t *vbuf[2],
  769. const int16_t *abuf0, uint8_t *dest, int dstW,
  770. int uvalpha, int y, enum AVPixelFormat target)
  771. {
  772. const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  773. int i;
  774. if (uvalpha < 2048) {
  775. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  776. int Y1 = (buf0[i * 2 ]+64) >> 7;
  777. int Y2 = (buf0[i * 2 + 1]+64) >> 7;
  778. int U = (ubuf0[i] +64) >> 7;
  779. int V = (vbuf0[i] +64) >> 7;
  780. if ((Y1 | Y2 | U | V) & 0x100) {
  781. Y1 = av_clip_uint8(Y1);
  782. Y2 = av_clip_uint8(Y2);
  783. U = av_clip_uint8(U);
  784. V = av_clip_uint8(V);
  785. }
  786. output_pixels(i * 4, Y1, U, Y2, V);
  787. }
  788. } else {
  789. const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  790. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  791. int Y1 = (buf0[i * 2 ] + 64) >> 7;
  792. int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
  793. int U = (ubuf0[i] + ubuf1[i]+128) >> 8;
  794. int V = (vbuf0[i] + vbuf1[i]+128) >> 8;
  795. if ((Y1 | Y2 | U | V) & 0x100) {
  796. Y1 = av_clip_uint8(Y1);
  797. Y2 = av_clip_uint8(Y2);
  798. U = av_clip_uint8(U);
  799. V = av_clip_uint8(V);
  800. }
  801. output_pixels(i * 4, Y1, U, Y2, V);
  802. }
  803. }
  804. }
  805. #undef output_pixels
  806. YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, AV_PIX_FMT_YUYV422)
  807. YUV2PACKEDWRAPPER(yuv2, 422, yvyu422, AV_PIX_FMT_YVYU422)
  808. YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, AV_PIX_FMT_UYVY422)
  809. #define R_B ((target == AV_PIX_FMT_RGB48LE || target == AV_PIX_FMT_RGB48BE || target == AV_PIX_FMT_RGBA64LE || target == AV_PIX_FMT_RGBA64BE) ? R : B)
  810. #define B_R ((target == AV_PIX_FMT_RGB48LE || target == AV_PIX_FMT_RGB48BE || target == AV_PIX_FMT_RGBA64LE || target == AV_PIX_FMT_RGBA64BE) ? B : R)
  811. #define output_pixel(pos, val) \
  812. if (isBE(target)) { \
  813. AV_WB16(pos, val); \
  814. } else { \
  815. AV_WL16(pos, val); \
  816. }
  817. static av_always_inline void
  818. yuv2ya16_X_c_template(SwsContext *c, const int16_t *lumFilter,
  819. const int32_t **lumSrc, int lumFilterSize,
  820. const int16_t *chrFilter, const int32_t **unused_chrUSrc,
  821. const int32_t **unused_chrVSrc, int unused_chrFilterSize,
  822. const int32_t **alpSrc, uint16_t *dest, int dstW,
  823. int y, enum AVPixelFormat target, int unused_hasAlpha, int unused_eightbytes)
  824. {
  825. int hasAlpha = !!alpSrc;
  826. int i;
  827. for (i = 0; i < dstW; i++) {
  828. int j;
  829. int Y = -0x40000000;
  830. int A = 0xffff;
  831. for (j = 0; j < lumFilterSize; j++)
  832. Y += lumSrc[j][i] * lumFilter[j];
  833. Y >>= 15;
  834. Y += (1<<3) + 0x8000;
  835. Y = av_clip_uint16(Y);
  836. if (hasAlpha) {
  837. A = -0x40000000 + (1<<14);
  838. for (j = 0; j < lumFilterSize; j++)
  839. A += alpSrc[j][i] * lumFilter[j];
  840. A >>= 15;
  841. A += 0x8000;
  842. A = av_clip_uint16(A);
  843. }
  844. output_pixel(&dest[2 * i ], Y);
  845. output_pixel(&dest[2 * i + 1], A);
  846. }
  847. }
  848. static av_always_inline void
  849. yuv2ya16_2_c_template(SwsContext *c, const int32_t *buf[2],
  850. const int32_t *unused_ubuf[2], const int32_t *unused_vbuf[2],
  851. const int32_t *abuf[2], uint16_t *dest, int dstW,
  852. int yalpha, int unused_uvalpha, int y,
  853. enum AVPixelFormat target, int unused_hasAlpha, int unused_eightbytes)
  854. {
  855. int hasAlpha = abuf && abuf[0] && abuf[1];
  856. const int32_t *buf0 = buf[0], *buf1 = buf[1],
  857. *abuf0 = hasAlpha ? abuf[0] : NULL,
  858. *abuf1 = hasAlpha ? abuf[1] : NULL;
  859. int yalpha1 = 4096 - yalpha;
  860. int i;
  861. av_assert2(yalpha <= 4096U);
  862. for (i = 0; i < dstW; i++) {
  863. int Y = (buf0[i] * yalpha1 + buf1[i] * yalpha) >> 15;
  864. int A;
  865. Y = av_clip_uint16(Y);
  866. if (hasAlpha) {
  867. A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha) >> 15;
  868. A = av_clip_uint16(A);
  869. }
  870. output_pixel(&dest[2 * i ], Y);
  871. output_pixel(&dest[2 * i + 1], hasAlpha ? A : 65535);
  872. }
  873. }
  874. static av_always_inline void
  875. yuv2ya16_1_c_template(SwsContext *c, const int32_t *buf0,
  876. const int32_t *unused_ubuf[2], const int32_t *unused_vbuf[2],
  877. const int32_t *abuf0, uint16_t *dest, int dstW,
  878. int unused_uvalpha, int y, enum AVPixelFormat target, int unused_hasAlpha, int unused_eightbytes)
  879. {
  880. int hasAlpha = !!abuf0;
  881. int i;
  882. for (i = 0; i < dstW; i++) {
  883. int Y = buf0[i] >> 3;/* 19 - 16 */
  884. int A;
  885. Y = av_clip_uint16(Y);
  886. if (hasAlpha) {
  887. A = abuf0[i] >> 3;
  888. if (A & 0x100)
  889. A = av_clip_uint16(A);
  890. }
  891. output_pixel(&dest[2 * i ], Y);
  892. output_pixel(&dest[2 * i + 1], hasAlpha ? A : 65535);
  893. }
  894. }
  895. static av_always_inline void
  896. yuv2rgba64_X_c_template(SwsContext *c, const int16_t *lumFilter,
  897. const int32_t **lumSrc, int lumFilterSize,
  898. const int16_t *chrFilter, const int32_t **chrUSrc,
  899. const int32_t **chrVSrc, int chrFilterSize,
  900. const int32_t **alpSrc, uint16_t *dest, int dstW,
  901. int y, enum AVPixelFormat target, int hasAlpha, int eightbytes)
  902. {
  903. int i;
  904. int A1 = 0xffff<<14, A2 = 0xffff<<14;
  905. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  906. int j;
  907. unsigned Y1 = -0x40000000;
  908. unsigned Y2 = -0x40000000;
  909. int U = -(128 << 23); // 19
  910. int V = -(128 << 23);
  911. int R, G, B;
  912. for (j = 0; j < lumFilterSize; j++) {
  913. Y1 += lumSrc[j][i * 2] * (unsigned)lumFilter[j];
  914. Y2 += lumSrc[j][i * 2 + 1] * (unsigned)lumFilter[j];
  915. }
  916. for (j = 0; j < chrFilterSize; j++) {;
  917. U += chrUSrc[j][i] * (unsigned)chrFilter[j];
  918. V += chrVSrc[j][i] * (unsigned)chrFilter[j];
  919. }
  920. if (hasAlpha) {
  921. A1 = -0x40000000;
  922. A2 = -0x40000000;
  923. for (j = 0; j < lumFilterSize; j++) {
  924. A1 += alpSrc[j][i * 2] * (unsigned)lumFilter[j];
  925. A2 += alpSrc[j][i * 2 + 1] * (unsigned)lumFilter[j];
  926. }
  927. A1 >>= 1;
  928. A1 += 0x20002000;
  929. A2 >>= 1;
  930. A2 += 0x20002000;
  931. }
  932. // 8 bits: 12+15=27; 16 bits: 12+19=31
  933. Y1 = (int)Y1 >> 14; // 10
  934. Y1 += 0x10000;
  935. Y2 = (int)Y2 >> 14;
  936. Y2 += 0x10000;
  937. U >>= 14;
  938. V >>= 14;
  939. // 8 bits: 27 -> 17 bits, 16 bits: 31 - 14 = 17 bits
  940. Y1 -= c->yuv2rgb_y_offset;
  941. Y2 -= c->yuv2rgb_y_offset;
  942. Y1 *= c->yuv2rgb_y_coeff;
  943. Y2 *= c->yuv2rgb_y_coeff;
  944. Y1 += (1 << 13) - (1 << 29); // 21
  945. Y2 += (1 << 13) - (1 << 29);
  946. // 8 bits: 17 + 13 bits = 30 bits, 16 bits: 17 + 13 bits = 30 bits
  947. R = V * c->yuv2rgb_v2r_coeff;
  948. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  949. B = U * c->yuv2rgb_u2b_coeff;
  950. // 8 bits: 30 - 22 = 8 bits, 16 bits: 30 bits - 14 = 16 bits
  951. output_pixel(&dest[0], av_clip_uintp2(((int)(R_B + Y1) >> 14) + (1<<15), 16));
  952. output_pixel(&dest[1], av_clip_uintp2(((int)( G + Y1) >> 14) + (1<<15), 16));
  953. output_pixel(&dest[2], av_clip_uintp2(((int)(B_R + Y1) >> 14) + (1<<15), 16));
  954. if (eightbytes) {
  955. output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
  956. output_pixel(&dest[4], av_clip_uintp2(((int)(R_B + Y2) >> 14) + (1<<15), 16));
  957. output_pixel(&dest[5], av_clip_uintp2(((int)( G + Y2) >> 14) + (1<<15), 16));
  958. output_pixel(&dest[6], av_clip_uintp2(((int)(B_R + Y2) >> 14) + (1<<15), 16));
  959. output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
  960. dest += 8;
  961. } else {
  962. output_pixel(&dest[3], av_clip_uintp2(((int)(R_B + Y2) >> 14) + (1<<15), 16));
  963. output_pixel(&dest[4], av_clip_uintp2(((int)( G + Y2) >> 14) + (1<<15), 16));
  964. output_pixel(&dest[5], av_clip_uintp2(((int)(B_R + Y2) >> 14) + (1<<15), 16));
  965. dest += 6;
  966. }
  967. }
  968. }
  969. static av_always_inline void
  970. yuv2rgba64_2_c_template(SwsContext *c, const int32_t *buf[2],
  971. const int32_t *ubuf[2], const int32_t *vbuf[2],
  972. const int32_t *abuf[2], uint16_t *dest, int dstW,
  973. int yalpha, int uvalpha, int y,
  974. enum AVPixelFormat target, int hasAlpha, int eightbytes)
  975. {
  976. const int32_t *buf0 = buf[0], *buf1 = buf[1],
  977. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  978. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
  979. *abuf0 = hasAlpha ? abuf[0] : NULL,
  980. *abuf1 = hasAlpha ? abuf[1] : NULL;
  981. int yalpha1 = 4096 - yalpha;
  982. int uvalpha1 = 4096 - uvalpha;
  983. int i;
  984. int A1 = 0xffff<<14, A2 = 0xffff<<14;
  985. av_assert2(yalpha <= 4096U);
  986. av_assert2(uvalpha <= 4096U);
  987. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  988. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 14;
  989. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 14;
  990. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha - (128 << 23)) >> 14;
  991. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha - (128 << 23)) >> 14;
  992. int R, G, B;
  993. Y1 -= c->yuv2rgb_y_offset;
  994. Y2 -= c->yuv2rgb_y_offset;
  995. Y1 *= c->yuv2rgb_y_coeff;
  996. Y2 *= c->yuv2rgb_y_coeff;
  997. Y1 += (1 << 13) - (1 << 29);
  998. Y2 += (1 << 13) - (1 << 29);
  999. R = V * c->yuv2rgb_v2r_coeff;
  1000. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  1001. B = U * c->yuv2rgb_u2b_coeff;
  1002. if (hasAlpha) {
  1003. A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 1;
  1004. A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 1;
  1005. A1 += 1 << 13;
  1006. A2 += 1 << 13;
  1007. }
  1008. output_pixel(&dest[0], av_clip_uintp2(((R_B + Y1) >> 14) + (1<<15), 16));
  1009. output_pixel(&dest[1], av_clip_uintp2((( G + Y1) >> 14) + (1<<15), 16));
  1010. output_pixel(&dest[2], av_clip_uintp2(((B_R + Y1) >> 14) + (1<<15), 16));
  1011. if (eightbytes) {
  1012. output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
  1013. output_pixel(&dest[4], av_clip_uintp2(((R_B + Y2) >> 14) + (1<<15), 16));
  1014. output_pixel(&dest[5], av_clip_uintp2((( G + Y2) >> 14) + (1<<15), 16));
  1015. output_pixel(&dest[6], av_clip_uintp2(((B_R + Y2) >> 14) + (1<<15), 16));
  1016. output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
  1017. dest += 8;
  1018. } else {
  1019. output_pixel(&dest[3], av_clip_uintp2(((R_B + Y2) >> 14) + (1<<15), 16));
  1020. output_pixel(&dest[4], av_clip_uintp2((( G + Y2) >> 14) + (1<<15), 16));
  1021. output_pixel(&dest[5], av_clip_uintp2(((B_R + Y2) >> 14) + (1<<15), 16));
  1022. dest += 6;
  1023. }
  1024. }
  1025. }
  1026. static av_always_inline void
  1027. yuv2rgba64_1_c_template(SwsContext *c, const int32_t *buf0,
  1028. const int32_t *ubuf[2], const int32_t *vbuf[2],
  1029. const int32_t *abuf0, uint16_t *dest, int dstW,
  1030. int uvalpha, int y, enum AVPixelFormat target, int hasAlpha, int eightbytes)
  1031. {
  1032. const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  1033. int i;
  1034. int A1 = 0xffff<<14, A2= 0xffff<<14;
  1035. if (uvalpha < 2048) {
  1036. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1037. SUINT Y1 = (buf0[i * 2] ) >> 2;
  1038. SUINT Y2 = (buf0[i * 2 + 1]) >> 2;
  1039. int U = (ubuf0[i] - (128 << 11)) >> 2;
  1040. int V = (vbuf0[i] - (128 << 11)) >> 2;
  1041. int R, G, B;
  1042. Y1 -= c->yuv2rgb_y_offset;
  1043. Y2 -= c->yuv2rgb_y_offset;
  1044. Y1 *= c->yuv2rgb_y_coeff;
  1045. Y2 *= c->yuv2rgb_y_coeff;
  1046. Y1 += (1 << 13) - (1 << 29);
  1047. Y2 += (1 << 13) - (1 << 29);
  1048. if (hasAlpha) {
  1049. A1 = abuf0[i * 2 ] * (1 << 11);
  1050. A2 = abuf0[i * 2 + 1] * (1 << 11);
  1051. A1 += 1 << 13;
  1052. A2 += 1 << 13;
  1053. }
  1054. R = V * c->yuv2rgb_v2r_coeff;
  1055. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  1056. B = U * c->yuv2rgb_u2b_coeff;
  1057. output_pixel(&dest[0], av_clip_uintp2(((int)(R_B + Y1) >> 14) + (1<<15), 16));
  1058. output_pixel(&dest[1], av_clip_uintp2(((int)( G + Y1) >> 14) + (1<<15), 16));
  1059. output_pixel(&dest[2], av_clip_uintp2(((int)(B_R + Y1) >> 14) + (1<<15), 16));
  1060. if (eightbytes) {
  1061. output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
  1062. output_pixel(&dest[4], av_clip_uintp2(((int)(R_B + Y2) >> 14) + (1<<15), 16));
  1063. output_pixel(&dest[5], av_clip_uintp2(((int)( G + Y2) >> 14) + (1<<15), 16));
  1064. output_pixel(&dest[6], av_clip_uintp2(((int)(B_R + Y2) >> 14) + (1<<15), 16));
  1065. output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
  1066. dest += 8;
  1067. } else {
  1068. output_pixel(&dest[3], av_clip_uintp2(((int)(R_B + Y2) >> 14) + (1<<15), 16));
  1069. output_pixel(&dest[4], av_clip_uintp2(((int)( G + Y2) >> 14) + (1<<15), 16));
  1070. output_pixel(&dest[5], av_clip_uintp2(((int)(B_R + Y2) >> 14) + (1<<15), 16));
  1071. dest += 6;
  1072. }
  1073. }
  1074. } else {
  1075. const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  1076. int A1 = 0xffff<<14, A2 = 0xffff<<14;
  1077. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1078. SUINT Y1 = (buf0[i * 2] ) >> 2;
  1079. SUINT Y2 = (buf0[i * 2 + 1]) >> 2;
  1080. int U = (ubuf0[i] + ubuf1[i] - (128 << 12)) >> 3;
  1081. int V = (vbuf0[i] + vbuf1[i] - (128 << 12)) >> 3;
  1082. int R, G, B;
  1083. Y1 -= c->yuv2rgb_y_offset;
  1084. Y2 -= c->yuv2rgb_y_offset;
  1085. Y1 *= c->yuv2rgb_y_coeff;
  1086. Y2 *= c->yuv2rgb_y_coeff;
  1087. Y1 += (1 << 13) - (1 << 29);
  1088. Y2 += (1 << 13) - (1 << 29);
  1089. if (hasAlpha) {
  1090. A1 = abuf0[i * 2 ] * (1 << 11);
  1091. A2 = abuf0[i * 2 + 1] * (1 << 11);
  1092. A1 += 1 << 13;
  1093. A2 += 1 << 13;
  1094. }
  1095. R = V * c->yuv2rgb_v2r_coeff;
  1096. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  1097. B = U * c->yuv2rgb_u2b_coeff;
  1098. output_pixel(&dest[0], av_clip_uintp2(((int)(R_B + Y1) >> 14) + (1<<15), 16));
  1099. output_pixel(&dest[1], av_clip_uintp2(((int)( G + Y1) >> 14) + (1<<15), 16));
  1100. output_pixel(&dest[2], av_clip_uintp2(((int)(B_R + Y1) >> 14) + (1<<15), 16));
  1101. if (eightbytes) {
  1102. output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
  1103. output_pixel(&dest[4], av_clip_uintp2(((int)(R_B + Y2) >> 14) + (1<<15), 16));
  1104. output_pixel(&dest[5], av_clip_uintp2(((int)( G + Y2) >> 14) + (1<<15), 16));
  1105. output_pixel(&dest[6], av_clip_uintp2(((int)(B_R + Y2) >> 14) + (1<<15), 16));
  1106. output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
  1107. dest += 8;
  1108. } else {
  1109. output_pixel(&dest[3], av_clip_uintp2(((int)(R_B + Y2) >> 14) + (1<<15), 16));
  1110. output_pixel(&dest[4], av_clip_uintp2(((int)( G + Y2) >> 14) + (1<<15), 16));
  1111. output_pixel(&dest[5], av_clip_uintp2(((int)(B_R + Y2) >> 14) + (1<<15), 16));
  1112. dest += 6;
  1113. }
  1114. }
  1115. }
  1116. }
  1117. static av_always_inline void
  1118. yuv2rgba64_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
  1119. const int32_t **lumSrc, int lumFilterSize,
  1120. const int16_t *chrFilter, const int32_t **chrUSrc,
  1121. const int32_t **chrVSrc, int chrFilterSize,
  1122. const int32_t **alpSrc, uint16_t *dest, int dstW,
  1123. int y, enum AVPixelFormat target, int hasAlpha, int eightbytes)
  1124. {
  1125. int i;
  1126. int A = 0xffff<<14;
  1127. for (i = 0; i < dstW; i++) {
  1128. int j;
  1129. int Y = -0x40000000;
  1130. int U = -(128 << 23); // 19
  1131. int V = -(128 << 23);
  1132. int R, G, B;
  1133. for (j = 0; j < lumFilterSize; j++) {
  1134. Y += lumSrc[j][i] * (unsigned)lumFilter[j];
  1135. }
  1136. for (j = 0; j < chrFilterSize; j++) {;
  1137. U += chrUSrc[j][i] * (unsigned)chrFilter[j];
  1138. V += chrVSrc[j][i] * (unsigned)chrFilter[j];
  1139. }
  1140. if (hasAlpha) {
  1141. A = -0x40000000;
  1142. for (j = 0; j < lumFilterSize; j++) {
  1143. A += alpSrc[j][i] * (unsigned)lumFilter[j];
  1144. }
  1145. A >>= 1;
  1146. A += 0x20002000;
  1147. }
  1148. // 8bit: 12+15=27; 16-bit: 12+19=31
  1149. Y >>= 14; // 10
  1150. Y += 0x10000;
  1151. U >>= 14;
  1152. V >>= 14;
  1153. // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
  1154. Y -= c->yuv2rgb_y_offset;
  1155. Y *= c->yuv2rgb_y_coeff;
  1156. Y += (1 << 13) - (1<<29); // 21
  1157. // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
  1158. R = V * c->yuv2rgb_v2r_coeff;
  1159. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  1160. B = U * c->yuv2rgb_u2b_coeff;
  1161. // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
  1162. output_pixel(&dest[0], av_clip_uintp2(((R_B + Y)>>14) + (1<<15), 16));
  1163. output_pixel(&dest[1], av_clip_uintp2((( G + Y)>>14) + (1<<15), 16));
  1164. output_pixel(&dest[2], av_clip_uintp2(((B_R + Y)>>14) + (1<<15), 16));
  1165. if (eightbytes) {
  1166. output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14);
  1167. dest += 4;
  1168. } else {
  1169. dest += 3;
  1170. }
  1171. }
  1172. }
  1173. static av_always_inline void
  1174. yuv2rgba64_full_2_c_template(SwsContext *c, const int32_t *buf[2],
  1175. const int32_t *ubuf[2], const int32_t *vbuf[2],
  1176. const int32_t *abuf[2], uint16_t *dest, int dstW,
  1177. int yalpha, int uvalpha, int y,
  1178. enum AVPixelFormat target, int hasAlpha, int eightbytes)
  1179. {
  1180. const int32_t *buf0 = buf[0], *buf1 = buf[1],
  1181. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  1182. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
  1183. *abuf0 = hasAlpha ? abuf[0] : NULL,
  1184. *abuf1 = hasAlpha ? abuf[1] : NULL;
  1185. int yalpha1 = 4096 - yalpha;
  1186. int uvalpha1 = 4096 - uvalpha;
  1187. int i;
  1188. int A = 0xffff<<14;
  1189. av_assert2(yalpha <= 4096U);
  1190. av_assert2(uvalpha <= 4096U);
  1191. for (i = 0; i < dstW; i++) {
  1192. int Y = (buf0[i] * yalpha1 + buf1[i] * yalpha) >> 14;
  1193. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha - (128 << 23)) >> 14;
  1194. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha - (128 << 23)) >> 14;
  1195. int R, G, B;
  1196. Y -= c->yuv2rgb_y_offset;
  1197. Y *= c->yuv2rgb_y_coeff;
  1198. Y += (1 << 13) - (1 << 29);
  1199. R = V * c->yuv2rgb_v2r_coeff;
  1200. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  1201. B = U * c->yuv2rgb_u2b_coeff;
  1202. if (hasAlpha) {
  1203. A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha) >> 1;
  1204. A += 1 << 13;
  1205. }
  1206. output_pixel(&dest[0], av_clip_uintp2(((R_B + Y) >> 14) + (1<<15), 16));
  1207. output_pixel(&dest[1], av_clip_uintp2((( G + Y) >> 14) + (1<<15), 16));
  1208. output_pixel(&dest[2], av_clip_uintp2(((B_R + Y) >> 14) + (1<<15), 16));
  1209. if (eightbytes) {
  1210. output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14);
  1211. dest += 4;
  1212. } else {
  1213. dest += 3;
  1214. }
  1215. }
  1216. }
  1217. static av_always_inline void
  1218. yuv2rgba64_full_1_c_template(SwsContext *c, const int32_t *buf0,
  1219. const int32_t *ubuf[2], const int32_t *vbuf[2],
  1220. const int32_t *abuf0, uint16_t *dest, int dstW,
  1221. int uvalpha, int y, enum AVPixelFormat target, int hasAlpha, int eightbytes)
  1222. {
  1223. const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  1224. int i;
  1225. int A = 0xffff<<14;
  1226. if (uvalpha < 2048) {
  1227. for (i = 0; i < dstW; i++) {
  1228. SUINT Y = (buf0[i]) >> 2;
  1229. int U = (ubuf0[i] - (128 << 11)) >> 2;
  1230. int V = (vbuf0[i] - (128 << 11)) >> 2;
  1231. int R, G, B;
  1232. Y -= c->yuv2rgb_y_offset;
  1233. Y *= c->yuv2rgb_y_coeff;
  1234. Y += (1 << 13) - (1 << 29);
  1235. if (hasAlpha) {
  1236. A = abuf0[i] * (1 << 11);
  1237. A += 1 << 13;
  1238. }
  1239. R = V * c->yuv2rgb_v2r_coeff;
  1240. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  1241. B = U * c->yuv2rgb_u2b_coeff;
  1242. output_pixel(&dest[0], av_clip_uintp2(((int)(R_B + Y) >> 14) + (1<<15), 16));
  1243. output_pixel(&dest[1], av_clip_uintp2(((int)( G + Y) >> 14) + (1<<15), 16));
  1244. output_pixel(&dest[2], av_clip_uintp2(((int)(B_R + Y) >> 14) + (1<<15), 16));
  1245. if (eightbytes) {
  1246. output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14);
  1247. dest += 4;
  1248. } else {
  1249. dest += 3;
  1250. }
  1251. }
  1252. } else {
  1253. const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  1254. int A = 0xffff<<14;
  1255. for (i = 0; i < dstW; i++) {
  1256. SUINT Y = (buf0[i] ) >> 2;
  1257. int U = (ubuf0[i] + ubuf1[i] - (128 << 12)) >> 3;
  1258. int V = (vbuf0[i] + vbuf1[i] - (128 << 12)) >> 3;
  1259. int R, G, B;
  1260. Y -= c->yuv2rgb_y_offset;
  1261. Y *= c->yuv2rgb_y_coeff;
  1262. Y += (1 << 13) - (1 << 29);
  1263. if (hasAlpha) {
  1264. A = abuf0[i] * (1 << 11);
  1265. A += 1 << 13;
  1266. }
  1267. R = V * c->yuv2rgb_v2r_coeff;
  1268. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  1269. B = U * c->yuv2rgb_u2b_coeff;
  1270. output_pixel(&dest[0], av_clip_uintp2(((int)(R_B + Y) >> 14) + (1<<15), 16));
  1271. output_pixel(&dest[1], av_clip_uintp2(((int)( G + Y) >> 14) + (1<<15), 16));
  1272. output_pixel(&dest[2], av_clip_uintp2(((int)(B_R + Y) >> 14) + (1<<15), 16));
  1273. if (eightbytes) {
  1274. output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14);
  1275. dest += 4;
  1276. } else {
  1277. dest += 3;
  1278. }
  1279. }
  1280. }
  1281. }
  1282. #undef output_pixel
  1283. #undef r_b
  1284. #undef b_r
  1285. #define YUV2PACKED16WRAPPER(name, base, ext, fmt, hasAlpha, eightbytes) \
  1286. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  1287. const int16_t **_lumSrc, int lumFilterSize, \
  1288. const int16_t *chrFilter, const int16_t **_chrUSrc, \
  1289. const int16_t **_chrVSrc, int chrFilterSize, \
  1290. const int16_t **_alpSrc, uint8_t *_dest, int dstW, \
  1291. int y) \
  1292. { \
  1293. const int32_t **lumSrc = (const int32_t **) _lumSrc, \
  1294. **chrUSrc = (const int32_t **) _chrUSrc, \
  1295. **chrVSrc = (const int32_t **) _chrVSrc, \
  1296. **alpSrc = (const int32_t **) _alpSrc; \
  1297. uint16_t *dest = (uint16_t *) _dest; \
  1298. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  1299. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  1300. alpSrc, dest, dstW, y, fmt, hasAlpha, eightbytes); \
  1301. } \
  1302. \
  1303. static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
  1304. const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
  1305. const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
  1306. int yalpha, int uvalpha, int y) \
  1307. { \
  1308. const int32_t **buf = (const int32_t **) _buf, \
  1309. **ubuf = (const int32_t **) _ubuf, \
  1310. **vbuf = (const int32_t **) _vbuf, \
  1311. **abuf = (const int32_t **) _abuf; \
  1312. uint16_t *dest = (uint16_t *) _dest; \
  1313. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  1314. dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha, eightbytes); \
  1315. } \
  1316. \
  1317. static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
  1318. const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
  1319. const int16_t *_abuf0, uint8_t *_dest, int dstW, \
  1320. int uvalpha, int y) \
  1321. { \
  1322. const int32_t *buf0 = (const int32_t *) _buf0, \
  1323. **ubuf = (const int32_t **) _ubuf, \
  1324. **vbuf = (const int32_t **) _vbuf, \
  1325. *abuf0 = (const int32_t *) _abuf0; \
  1326. uint16_t *dest = (uint16_t *) _dest; \
  1327. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
  1328. dstW, uvalpha, y, fmt, hasAlpha, eightbytes); \
  1329. }
  1330. YUV2PACKED16WRAPPER(yuv2, rgba64, rgb48be, AV_PIX_FMT_RGB48BE, 0, 0)
  1331. YUV2PACKED16WRAPPER(yuv2, rgba64, rgb48le, AV_PIX_FMT_RGB48LE, 0, 0)
  1332. YUV2PACKED16WRAPPER(yuv2, rgba64, bgr48be, AV_PIX_FMT_BGR48BE, 0, 0)
  1333. YUV2PACKED16WRAPPER(yuv2, rgba64, bgr48le, AV_PIX_FMT_BGR48LE, 0, 0)
  1334. YUV2PACKED16WRAPPER(yuv2, rgba64, rgba64be, AV_PIX_FMT_RGBA64BE, 1, 1)
  1335. YUV2PACKED16WRAPPER(yuv2, rgba64, rgba64le, AV_PIX_FMT_RGBA64LE, 1, 1)
  1336. YUV2PACKED16WRAPPER(yuv2, rgba64, rgbx64be, AV_PIX_FMT_RGBA64BE, 0, 1)
  1337. YUV2PACKED16WRAPPER(yuv2, rgba64, rgbx64le, AV_PIX_FMT_RGBA64LE, 0, 1)
  1338. YUV2PACKED16WRAPPER(yuv2, rgba64, bgra64be, AV_PIX_FMT_BGRA64BE, 1, 1)
  1339. YUV2PACKED16WRAPPER(yuv2, rgba64, bgra64le, AV_PIX_FMT_BGRA64LE, 1, 1)
  1340. YUV2PACKED16WRAPPER(yuv2, rgba64, bgrx64be, AV_PIX_FMT_BGRA64BE, 0, 1)
  1341. YUV2PACKED16WRAPPER(yuv2, rgba64, bgrx64le, AV_PIX_FMT_BGRA64LE, 0, 1)
  1342. YUV2PACKED16WRAPPER(yuv2, ya16, ya16be, AV_PIX_FMT_YA16BE, 1, 0)
  1343. YUV2PACKED16WRAPPER(yuv2, ya16, ya16le, AV_PIX_FMT_YA16LE, 1, 0)
  1344. YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgb48be_full, AV_PIX_FMT_RGB48BE, 0, 0)
  1345. YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgb48le_full, AV_PIX_FMT_RGB48LE, 0, 0)
  1346. YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgr48be_full, AV_PIX_FMT_BGR48BE, 0, 0)
  1347. YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgr48le_full, AV_PIX_FMT_BGR48LE, 0, 0)
  1348. YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgba64be_full, AV_PIX_FMT_RGBA64BE, 1, 1)
  1349. YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgba64le_full, AV_PIX_FMT_RGBA64LE, 1, 1)
  1350. YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgbx64be_full, AV_PIX_FMT_RGBA64BE, 0, 1)
  1351. YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgbx64le_full, AV_PIX_FMT_RGBA64LE, 0, 1)
  1352. YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgra64be_full, AV_PIX_FMT_BGRA64BE, 1, 1)
  1353. YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgra64le_full, AV_PIX_FMT_BGRA64LE, 1, 1)
  1354. YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgrx64be_full, AV_PIX_FMT_BGRA64BE, 0, 1)
  1355. YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgrx64le_full, AV_PIX_FMT_BGRA64LE, 0, 1)
  1356. /*
  1357. * Write out 2 RGB pixels in the target pixel format. This function takes a
  1358. * R/G/B LUT as generated by ff_yuv2rgb_c_init_tables(), which takes care of
  1359. * things like endianness conversion and shifting. The caller takes care of
  1360. * setting the correct offset in these tables from the chroma (U/V) values.
  1361. * This function then uses the luminance (Y1/Y2) values to write out the
  1362. * correct RGB values into the destination buffer.
  1363. */
  1364. static av_always_inline void
  1365. yuv2rgb_write(uint8_t *_dest, int i, int Y1, int Y2,
  1366. unsigned A1, unsigned A2,
  1367. const void *_r, const void *_g, const void *_b, int y,
  1368. enum AVPixelFormat target, int hasAlpha)
  1369. {
  1370. if (target == AV_PIX_FMT_ARGB || target == AV_PIX_FMT_RGBA ||
  1371. target == AV_PIX_FMT_ABGR || target == AV_PIX_FMT_BGRA) {
  1372. uint32_t *dest = (uint32_t *) _dest;
  1373. const uint32_t *r = (const uint32_t *) _r;
  1374. const uint32_t *g = (const uint32_t *) _g;
  1375. const uint32_t *b = (const uint32_t *) _b;
  1376. #if CONFIG_SMALL
  1377. int sh = hasAlpha ? ((target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24) : 0;
  1378. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (hasAlpha ? A1 << sh : 0);
  1379. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (hasAlpha ? A2 << sh : 0);
  1380. #else
  1381. if (hasAlpha) {
  1382. int sh = (target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24;
  1383. av_assert2((((r[Y1] + g[Y1] + b[Y1]) >> sh) & 0xFF) == 0);
  1384. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
  1385. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
  1386. } else {
  1387. #if defined(ASSERT_LEVEL) && ASSERT_LEVEL > 1
  1388. int sh = (target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24;
  1389. av_assert2((((r[Y1] + g[Y1] + b[Y1]) >> sh) & 0xFF) == 0xFF);
  1390. #endif
  1391. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
  1392. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
  1393. }
  1394. #endif
  1395. } else if (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) {
  1396. uint8_t *dest = (uint8_t *) _dest;
  1397. const uint8_t *r = (const uint8_t *) _r;
  1398. const uint8_t *g = (const uint8_t *) _g;
  1399. const uint8_t *b = (const uint8_t *) _b;
  1400. #define r_b ((target == AV_PIX_FMT_RGB24) ? r : b)
  1401. #define b_r ((target == AV_PIX_FMT_RGB24) ? b : r)
  1402. dest[i * 6 + 0] = r_b[Y1];
  1403. dest[i * 6 + 1] = g[Y1];
  1404. dest[i * 6 + 2] = b_r[Y1];
  1405. dest[i * 6 + 3] = r_b[Y2];
  1406. dest[i * 6 + 4] = g[Y2];
  1407. dest[i * 6 + 5] = b_r[Y2];
  1408. #undef r_b
  1409. #undef b_r
  1410. } else if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565 ||
  1411. target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555 ||
  1412. target == AV_PIX_FMT_RGB444 || target == AV_PIX_FMT_BGR444) {
  1413. uint16_t *dest = (uint16_t *) _dest;
  1414. const uint16_t *r = (const uint16_t *) _r;
  1415. const uint16_t *g = (const uint16_t *) _g;
  1416. const uint16_t *b = (const uint16_t *) _b;
  1417. int dr1, dg1, db1, dr2, dg2, db2;
  1418. if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565) {
  1419. dr1 = ff_dither_2x2_8[ y & 1 ][0];
  1420. dg1 = ff_dither_2x2_4[ y & 1 ][0];
  1421. db1 = ff_dither_2x2_8[(y & 1) ^ 1][0];
  1422. dr2 = ff_dither_2x2_8[ y & 1 ][1];
  1423. dg2 = ff_dither_2x2_4[ y & 1 ][1];
  1424. db2 = ff_dither_2x2_8[(y & 1) ^ 1][1];
  1425. } else if (target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555) {
  1426. dr1 = ff_dither_2x2_8[ y & 1 ][0];
  1427. dg1 = ff_dither_2x2_8[ y & 1 ][1];
  1428. db1 = ff_dither_2x2_8[(y & 1) ^ 1][0];
  1429. dr2 = ff_dither_2x2_8[ y & 1 ][1];
  1430. dg2 = ff_dither_2x2_8[ y & 1 ][0];
  1431. db2 = ff_dither_2x2_8[(y & 1) ^ 1][1];
  1432. } else {
  1433. dr1 = ff_dither_4x4_16[ y & 3 ][0];
  1434. dg1 = ff_dither_4x4_16[ y & 3 ][1];
  1435. db1 = ff_dither_4x4_16[(y & 3) ^ 3][0];
  1436. dr2 = ff_dither_4x4_16[ y & 3 ][1];
  1437. dg2 = ff_dither_4x4_16[ y & 3 ][0];
  1438. db2 = ff_dither_4x4_16[(y & 3) ^ 3][1];
  1439. }
  1440. dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
  1441. dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
  1442. } else if (target == AV_PIX_FMT_X2RGB10 || target == AV_PIX_FMT_X2BGR10) {
  1443. uint32_t *dest = (uint32_t *) _dest;
  1444. const uint32_t *r = (const uint32_t *) _r;
  1445. const uint32_t *g = (const uint32_t *) _g;
  1446. const uint32_t *b = (const uint32_t *) _b;
  1447. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
  1448. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
  1449. } else /* 8/4 bits */ {
  1450. uint8_t *dest = (uint8_t *) _dest;
  1451. const uint8_t *r = (const uint8_t *) _r;
  1452. const uint8_t *g = (const uint8_t *) _g;
  1453. const uint8_t *b = (const uint8_t *) _b;
  1454. int dr1, dg1, db1, dr2, dg2, db2;
  1455. if (target == AV_PIX_FMT_RGB8 || target == AV_PIX_FMT_BGR8) {
  1456. const uint8_t * const d64 = ff_dither_8x8_73[y & 7];
  1457. const uint8_t * const d32 = ff_dither_8x8_32[y & 7];
  1458. dr1 = dg1 = d32[(i * 2 + 0) & 7];
  1459. db1 = d64[(i * 2 + 0) & 7];
  1460. dr2 = dg2 = d32[(i * 2 + 1) & 7];
  1461. db2 = d64[(i * 2 + 1) & 7];
  1462. } else {
  1463. const uint8_t * const d64 = ff_dither_8x8_73 [y & 7];
  1464. const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
  1465. dr1 = db1 = d128[(i * 2 + 0) & 7];
  1466. dg1 = d64[(i * 2 + 0) & 7];
  1467. dr2 = db2 = d128[(i * 2 + 1) & 7];
  1468. dg2 = d64[(i * 2 + 1) & 7];
  1469. }
  1470. if (target == AV_PIX_FMT_RGB4 || target == AV_PIX_FMT_BGR4) {
  1471. dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
  1472. ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
  1473. } else {
  1474. dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
  1475. dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
  1476. }
  1477. }
  1478. }
  1479. static av_always_inline void
  1480. yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
  1481. const int16_t **lumSrc, int lumFilterSize,
  1482. const int16_t *chrFilter, const int16_t **chrUSrc,
  1483. const int16_t **chrVSrc, int chrFilterSize,
  1484. const int16_t **alpSrc, uint8_t *dest, int dstW,
  1485. int y, enum AVPixelFormat target, int hasAlpha)
  1486. {
  1487. int i;
  1488. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1489. int j, A1, A2;
  1490. int Y1 = 1 << 18;
  1491. int Y2 = 1 << 18;
  1492. int U = 1 << 18;
  1493. int V = 1 << 18;
  1494. const void *r, *g, *b;
  1495. for (j = 0; j < lumFilterSize; j++) {
  1496. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  1497. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  1498. }
  1499. for (j = 0; j < chrFilterSize; j++) {
  1500. U += chrUSrc[j][i] * chrFilter[j];
  1501. V += chrVSrc[j][i] * chrFilter[j];
  1502. }
  1503. Y1 >>= 19;
  1504. Y2 >>= 19;
  1505. U >>= 19;
  1506. V >>= 19;
  1507. if (hasAlpha) {
  1508. A1 = 1 << 18;
  1509. A2 = 1 << 18;
  1510. for (j = 0; j < lumFilterSize; j++) {
  1511. A1 += alpSrc[j][i * 2 ] * lumFilter[j];
  1512. A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
  1513. }
  1514. A1 >>= 19;
  1515. A2 >>= 19;
  1516. if ((A1 | A2) & 0x100) {
  1517. A1 = av_clip_uint8(A1);
  1518. A2 = av_clip_uint8(A2);
  1519. }
  1520. }
  1521. r = c->table_rV[V + YUVRGB_TABLE_HEADROOM];
  1522. g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]);
  1523. b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  1524. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1525. r, g, b, y, target, hasAlpha);
  1526. }
  1527. }
  1528. static av_always_inline void
  1529. yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
  1530. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1531. const int16_t *abuf[2], uint8_t *dest, int dstW,
  1532. int yalpha, int uvalpha, int y,
  1533. enum AVPixelFormat target, int hasAlpha)
  1534. {
  1535. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  1536. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  1537. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
  1538. *abuf0 = hasAlpha ? abuf[0] : NULL,
  1539. *abuf1 = hasAlpha ? abuf[1] : NULL;
  1540. int yalpha1 = 4096 - yalpha;
  1541. int uvalpha1 = 4096 - uvalpha;
  1542. int i;
  1543. av_assert2(yalpha <= 4096U);
  1544. av_assert2(uvalpha <= 4096U);
  1545. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1546. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
  1547. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
  1548. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
  1549. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
  1550. int A1, A2;
  1551. const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
  1552. *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
  1553. *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  1554. if (hasAlpha) {
  1555. A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 19;
  1556. A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
  1557. A1 = av_clip_uint8(A1);
  1558. A2 = av_clip_uint8(A2);
  1559. }
  1560. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1561. r, g, b, y, target, hasAlpha);
  1562. }
  1563. }
  1564. static av_always_inline void
  1565. yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
  1566. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1567. const int16_t *abuf0, uint8_t *dest, int dstW,
  1568. int uvalpha, int y, enum AVPixelFormat target,
  1569. int hasAlpha)
  1570. {
  1571. const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  1572. int i;
  1573. if (uvalpha < 2048) {
  1574. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1575. int Y1 = (buf0[i * 2 ] + 64) >> 7;
  1576. int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
  1577. int U = (ubuf0[i] + 64) >> 7;
  1578. int V = (vbuf0[i] + 64) >> 7;
  1579. int A1, A2;
  1580. const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
  1581. *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
  1582. *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  1583. if (hasAlpha) {
  1584. A1 = abuf0[i * 2 ] * 255 + 16384 >> 15;
  1585. A2 = abuf0[i * 2 + 1] * 255 + 16384 >> 15;
  1586. A1 = av_clip_uint8(A1);
  1587. A2 = av_clip_uint8(A2);
  1588. }
  1589. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1590. r, g, b, y, target, hasAlpha);
  1591. }
  1592. } else {
  1593. const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  1594. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1595. int Y1 = (buf0[i * 2 ] + 64) >> 7;
  1596. int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
  1597. int U = (ubuf0[i] + ubuf1[i] + 128) >> 8;
  1598. int V = (vbuf0[i] + vbuf1[i] + 128) >> 8;
  1599. int A1, A2;
  1600. const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
  1601. *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
  1602. *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  1603. if (hasAlpha) {
  1604. A1 = (abuf0[i * 2 ] + 64) >> 7;
  1605. A2 = (abuf0[i * 2 + 1] + 64) >> 7;
  1606. A1 = av_clip_uint8(A1);
  1607. A2 = av_clip_uint8(A2);
  1608. }
  1609. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1610. r, g, b, y, target, hasAlpha);
  1611. }
  1612. }
  1613. }
  1614. #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
  1615. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  1616. const int16_t **lumSrc, int lumFilterSize, \
  1617. const int16_t *chrFilter, const int16_t **chrUSrc, \
  1618. const int16_t **chrVSrc, int chrFilterSize, \
  1619. const int16_t **alpSrc, uint8_t *dest, int dstW, \
  1620. int y) \
  1621. { \
  1622. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  1623. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  1624. alpSrc, dest, dstW, y, fmt, hasAlpha); \
  1625. }
  1626. #define YUV2RGBWRAPPERX2(name, base, ext, fmt, hasAlpha) \
  1627. YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
  1628. static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
  1629. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  1630. const int16_t *abuf[2], uint8_t *dest, int dstW, \
  1631. int yalpha, int uvalpha, int y) \
  1632. { \
  1633. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  1634. dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
  1635. }
  1636. #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
  1637. YUV2RGBWRAPPERX2(name, base, ext, fmt, hasAlpha) \
  1638. static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
  1639. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  1640. const int16_t *abuf0, uint8_t *dest, int dstW, \
  1641. int uvalpha, int y) \
  1642. { \
  1643. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
  1644. dstW, uvalpha, y, fmt, hasAlpha); \
  1645. }
  1646. #if CONFIG_SMALL
  1647. YUV2RGBWRAPPER(yuv2rgb,, 32_1, AV_PIX_FMT_RGB32_1, CONFIG_SWSCALE_ALPHA && c->needAlpha)
  1648. YUV2RGBWRAPPER(yuv2rgb,, 32, AV_PIX_FMT_RGB32, CONFIG_SWSCALE_ALPHA && c->needAlpha)
  1649. #else
  1650. #if CONFIG_SWSCALE_ALPHA
  1651. YUV2RGBWRAPPER(yuv2rgb,, a32_1, AV_PIX_FMT_RGB32_1, 1)
  1652. YUV2RGBWRAPPER(yuv2rgb,, a32, AV_PIX_FMT_RGB32, 1)
  1653. #endif
  1654. YUV2RGBWRAPPER(yuv2rgb,, x32_1, AV_PIX_FMT_RGB32_1, 0)
  1655. YUV2RGBWRAPPER(yuv2rgb,, x32, AV_PIX_FMT_RGB32, 0)
  1656. #endif
  1657. YUV2RGBWRAPPER(yuv2, rgb, rgb24, AV_PIX_FMT_RGB24, 0)
  1658. YUV2RGBWRAPPER(yuv2, rgb, bgr24, AV_PIX_FMT_BGR24, 0)
  1659. YUV2RGBWRAPPER(yuv2rgb,, 16, AV_PIX_FMT_RGB565, 0)
  1660. YUV2RGBWRAPPER(yuv2rgb,, 15, AV_PIX_FMT_RGB555, 0)
  1661. YUV2RGBWRAPPER(yuv2rgb,, 12, AV_PIX_FMT_RGB444, 0)
  1662. YUV2RGBWRAPPER(yuv2rgb,, 8, AV_PIX_FMT_RGB8, 0)
  1663. YUV2RGBWRAPPER(yuv2rgb,, 4, AV_PIX_FMT_RGB4, 0)
  1664. YUV2RGBWRAPPER(yuv2rgb,, 4b, AV_PIX_FMT_RGB4_BYTE, 0)
  1665. YUV2RGBWRAPPER(yuv2, rgb, x2rgb10, AV_PIX_FMT_X2RGB10, 0)
  1666. YUV2RGBWRAPPER(yuv2, rgb, x2bgr10, AV_PIX_FMT_X2BGR10, 0)
  1667. static av_always_inline void yuv2rgb_write_full(SwsContext *c,
  1668. uint8_t *dest, int i, int Y, int A, int U, int V,
  1669. int y, enum AVPixelFormat target, int hasAlpha, int err[4])
  1670. {
  1671. int R, G, B;
  1672. int isrgb8 = target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8;
  1673. Y -= c->yuv2rgb_y_offset;
  1674. Y *= c->yuv2rgb_y_coeff;
  1675. Y += 1 << 21;
  1676. R = (unsigned)Y + V*(unsigned)c->yuv2rgb_v2r_coeff;
  1677. G = (unsigned)Y + V*(unsigned)c->yuv2rgb_v2g_coeff + U*(unsigned)c->yuv2rgb_u2g_coeff;
  1678. B = (unsigned)Y + U*(unsigned)c->yuv2rgb_u2b_coeff;
  1679. if ((R | G | B) & 0xC0000000) {
  1680. R = av_clip_uintp2(R, 30);
  1681. G = av_clip_uintp2(G, 30);
  1682. B = av_clip_uintp2(B, 30);
  1683. }
  1684. switch(target) {
  1685. case AV_PIX_FMT_ARGB:
  1686. dest[0] = hasAlpha ? A : 255;
  1687. dest[1] = R >> 22;
  1688. dest[2] = G >> 22;
  1689. dest[3] = B >> 22;
  1690. break;
  1691. case AV_PIX_FMT_RGB24:
  1692. dest[0] = R >> 22;
  1693. dest[1] = G >> 22;
  1694. dest[2] = B >> 22;
  1695. break;
  1696. case AV_PIX_FMT_RGBA:
  1697. dest[0] = R >> 22;
  1698. dest[1] = G >> 22;
  1699. dest[2] = B >> 22;
  1700. dest[3] = hasAlpha ? A : 255;
  1701. break;
  1702. case AV_PIX_FMT_ABGR:
  1703. dest[0] = hasAlpha ? A : 255;
  1704. dest[1] = B >> 22;
  1705. dest[2] = G >> 22;
  1706. dest[3] = R >> 22;
  1707. break;
  1708. case AV_PIX_FMT_BGR24:
  1709. dest[0] = B >> 22;
  1710. dest[1] = G >> 22;
  1711. dest[2] = R >> 22;
  1712. break;
  1713. case AV_PIX_FMT_BGRA:
  1714. dest[0] = B >> 22;
  1715. dest[1] = G >> 22;
  1716. dest[2] = R >> 22;
  1717. dest[3] = hasAlpha ? A : 255;
  1718. break;
  1719. case AV_PIX_FMT_BGR4_BYTE:
  1720. case AV_PIX_FMT_RGB4_BYTE:
  1721. case AV_PIX_FMT_BGR8:
  1722. case AV_PIX_FMT_RGB8:
  1723. {
  1724. int r,g,b;
  1725. switch (c->dither) {
  1726. case SWS_DITHER_NONE:
  1727. if (isrgb8) {
  1728. r = av_clip_uintp2(R >> 27, 3);
  1729. g = av_clip_uintp2(G >> 27, 3);
  1730. b = av_clip_uintp2(B >> 28, 2);
  1731. } else {
  1732. r = av_clip_uintp2(R >> 29, 1);
  1733. g = av_clip_uintp2(G >> 28, 2);
  1734. b = av_clip_uintp2(B >> 29, 1);
  1735. }
  1736. break;
  1737. default:
  1738. case SWS_DITHER_AUTO:
  1739. case SWS_DITHER_ED:
  1740. R >>= 22;
  1741. G >>= 22;
  1742. B >>= 22;
  1743. R += (7*err[0] + 1*c->dither_error[0][i] + 5*c->dither_error[0][i+1] + 3*c->dither_error[0][i+2])>>4;
  1744. G += (7*err[1] + 1*c->dither_error[1][i] + 5*c->dither_error[1][i+1] + 3*c->dither_error[1][i+2])>>4;
  1745. B += (7*err[2] + 1*c->dither_error[2][i] + 5*c->dither_error[2][i+1] + 3*c->dither_error[2][i+2])>>4;
  1746. c->dither_error[0][i] = err[0];
  1747. c->dither_error[1][i] = err[1];
  1748. c->dither_error[2][i] = err[2];
  1749. r = R >> (isrgb8 ? 5 : 7);
  1750. g = G >> (isrgb8 ? 5 : 6);
  1751. b = B >> (isrgb8 ? 6 : 7);
  1752. r = av_clip(r, 0, isrgb8 ? 7 : 1);
  1753. g = av_clip(g, 0, isrgb8 ? 7 : 3);
  1754. b = av_clip(b, 0, isrgb8 ? 3 : 1);
  1755. err[0] = R - r*(isrgb8 ? 36 : 255);
  1756. err[1] = G - g*(isrgb8 ? 36 : 85);
  1757. err[2] = B - b*(isrgb8 ? 85 : 255);
  1758. break;
  1759. case SWS_DITHER_A_DITHER:
  1760. if (isrgb8) {
  1761. /* see http://pippin.gimp.org/a_dither/ for details/origin */
  1762. #define A_DITHER(u,v) (((((u)+((v)*236))*119)&0xff))
  1763. r = (((R >> 19) + A_DITHER(i,y) -96)>>8);
  1764. g = (((G >> 19) + A_DITHER(i + 17,y) - 96)>>8);
  1765. b = (((B >> 20) + A_DITHER(i + 17*2,y) -96)>>8);
  1766. r = av_clip_uintp2(r, 3);
  1767. g = av_clip_uintp2(g, 3);
  1768. b = av_clip_uintp2(b, 2);
  1769. } else {
  1770. r = (((R >> 21) + A_DITHER(i,y)-256)>>8);
  1771. g = (((G >> 19) + A_DITHER(i + 17,y)-256)>>8);
  1772. b = (((B >> 21) + A_DITHER(i + 17*2,y)-256)>>8);
  1773. r = av_clip_uintp2(r, 1);
  1774. g = av_clip_uintp2(g, 2);
  1775. b = av_clip_uintp2(b, 1);
  1776. }
  1777. break;
  1778. case SWS_DITHER_X_DITHER:
  1779. if (isrgb8) {
  1780. /* see http://pippin.gimp.org/a_dither/ for details/origin */
  1781. #define X_DITHER(u,v) (((((u)^((v)*237))*181)&0x1ff)/2)
  1782. r = (((R >> 19) + X_DITHER(i,y) - 96)>>8);
  1783. g = (((G >> 19) + X_DITHER(i + 17,y) - 96)>>8);
  1784. b = (((B >> 20) + X_DITHER(i + 17*2,y) - 96)>>8);
  1785. r = av_clip_uintp2(r, 3);
  1786. g = av_clip_uintp2(g, 3);
  1787. b = av_clip_uintp2(b, 2);
  1788. } else {
  1789. r = (((R >> 21) + X_DITHER(i,y)-256)>>8);
  1790. g = (((G >> 19) + X_DITHER(i + 17,y)-256)>>8);
  1791. b = (((B >> 21) + X_DITHER(i + 17*2,y)-256)>>8);
  1792. r = av_clip_uintp2(r, 1);
  1793. g = av_clip_uintp2(g, 2);
  1794. b = av_clip_uintp2(b, 1);
  1795. }
  1796. break;
  1797. }
  1798. if(target == AV_PIX_FMT_BGR4_BYTE) {
  1799. dest[0] = r + 2*g + 8*b;
  1800. } else if(target == AV_PIX_FMT_RGB4_BYTE) {
  1801. dest[0] = b + 2*g + 8*r;
  1802. } else if(target == AV_PIX_FMT_BGR8) {
  1803. dest[0] = r + 8*g + 64*b;
  1804. } else if(target == AV_PIX_FMT_RGB8) {
  1805. dest[0] = b + 4*g + 32*r;
  1806. } else
  1807. av_assert2(0);
  1808. break;}
  1809. }
  1810. }
  1811. static av_always_inline void
  1812. yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
  1813. const int16_t **lumSrc, int lumFilterSize,
  1814. const int16_t *chrFilter, const int16_t **chrUSrc,
  1815. const int16_t **chrVSrc, int chrFilterSize,
  1816. const int16_t **alpSrc, uint8_t *dest,
  1817. int dstW, int y, enum AVPixelFormat target, int hasAlpha)
  1818. {
  1819. int i;
  1820. int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
  1821. int err[4] = {0};
  1822. int A = 0; //init to silence warning
  1823. if( target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
  1824. || target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8)
  1825. step = 1;
  1826. for (i = 0; i < dstW; i++) {
  1827. int j;
  1828. int Y = 1<<9;
  1829. int U = (1<<9)-(128 << 19);
  1830. int V = (1<<9)-(128 << 19);
  1831. for (j = 0; j < lumFilterSize; j++) {
  1832. Y += lumSrc[j][i] * lumFilter[j];
  1833. }
  1834. for (j = 0; j < chrFilterSize; j++) {
  1835. U += chrUSrc[j][i] * chrFilter[j];
  1836. V += chrVSrc[j][i] * chrFilter[j];
  1837. }
  1838. Y >>= 10;
  1839. U >>= 10;
  1840. V >>= 10;
  1841. if (hasAlpha) {
  1842. A = 1 << 18;
  1843. for (j = 0; j < lumFilterSize; j++) {
  1844. A += alpSrc[j][i] * lumFilter[j];
  1845. }
  1846. A >>= 19;
  1847. if (A & 0x100)
  1848. A = av_clip_uint8(A);
  1849. }
  1850. yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
  1851. dest += step;
  1852. }
  1853. c->dither_error[0][i] = err[0];
  1854. c->dither_error[1][i] = err[1];
  1855. c->dither_error[2][i] = err[2];
  1856. }
  1857. static av_always_inline void
  1858. yuv2rgb_full_2_c_template(SwsContext *c, const int16_t *buf[2],
  1859. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1860. const int16_t *abuf[2], uint8_t *dest, int dstW,
  1861. int yalpha, int uvalpha, int y,
  1862. enum AVPixelFormat target, int hasAlpha)
  1863. {
  1864. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  1865. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  1866. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
  1867. *abuf0 = hasAlpha ? abuf[0] : NULL,
  1868. *abuf1 = hasAlpha ? abuf[1] : NULL;
  1869. int yalpha1 = 4096 - yalpha;
  1870. int uvalpha1 = 4096 - uvalpha;
  1871. int i;
  1872. int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
  1873. int err[4] = {0};
  1874. int A = 0; // init to silcene warning
  1875. av_assert2(yalpha <= 4096U);
  1876. av_assert2(uvalpha <= 4096U);
  1877. if( target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
  1878. || target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8)
  1879. step = 1;
  1880. for (i = 0; i < dstW; i++) {
  1881. int Y = ( buf0[i] * yalpha1 + buf1[i] * yalpha ) >> 10; //FIXME rounding
  1882. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha-(128 << 19)) >> 10;
  1883. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha-(128 << 19)) >> 10;
  1884. if (hasAlpha) {
  1885. A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha + (1<<18)) >> 19;
  1886. if (A & 0x100)
  1887. A = av_clip_uint8(A);
  1888. }
  1889. yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
  1890. dest += step;
  1891. }
  1892. c->dither_error[0][i] = err[0];
  1893. c->dither_error[1][i] = err[1];
  1894. c->dither_error[2][i] = err[2];
  1895. }
  1896. static av_always_inline void
  1897. yuv2rgb_full_1_c_template(SwsContext *c, const int16_t *buf0,
  1898. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1899. const int16_t *abuf0, uint8_t *dest, int dstW,
  1900. int uvalpha, int y, enum AVPixelFormat target,
  1901. int hasAlpha)
  1902. {
  1903. const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  1904. int i;
  1905. int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
  1906. int err[4] = {0};
  1907. if( target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
  1908. || target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8)
  1909. step = 1;
  1910. if (uvalpha < 2048) {
  1911. int A = 0; //init to silence warning
  1912. for (i = 0; i < dstW; i++) {
  1913. int Y = buf0[i] * 4;
  1914. int U = (ubuf0[i] - (128<<7)) * 4;
  1915. int V = (vbuf0[i] - (128<<7)) * 4;
  1916. if (hasAlpha) {
  1917. A = (abuf0[i] + 64) >> 7;
  1918. if (A & 0x100)
  1919. A = av_clip_uint8(A);
  1920. }
  1921. yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
  1922. dest += step;
  1923. }
  1924. } else {
  1925. const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  1926. int A = 0; //init to silence warning
  1927. for (i = 0; i < dstW; i++) {
  1928. int Y = buf0[i] * 4;
  1929. int U = (ubuf0[i] + ubuf1[i] - (128<<8)) * 2;
  1930. int V = (vbuf0[i] + vbuf1[i] - (128<<8)) * 2;
  1931. if (hasAlpha) {
  1932. A = (abuf0[i] + 64) >> 7;
  1933. if (A & 0x100)
  1934. A = av_clip_uint8(A);
  1935. }
  1936. yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
  1937. dest += step;
  1938. }
  1939. }
  1940. c->dither_error[0][i] = err[0];
  1941. c->dither_error[1][i] = err[1];
  1942. c->dither_error[2][i] = err[2];
  1943. }
  1944. #if CONFIG_SMALL
  1945. YUV2RGBWRAPPER(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA, CONFIG_SWSCALE_ALPHA && c->needAlpha)
  1946. YUV2RGBWRAPPER(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR, CONFIG_SWSCALE_ALPHA && c->needAlpha)
  1947. YUV2RGBWRAPPER(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA, CONFIG_SWSCALE_ALPHA && c->needAlpha)
  1948. YUV2RGBWRAPPER(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB, CONFIG_SWSCALE_ALPHA && c->needAlpha)
  1949. #else
  1950. #if CONFIG_SWSCALE_ALPHA
  1951. YUV2RGBWRAPPER(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA, 1)
  1952. YUV2RGBWRAPPER(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR, 1)
  1953. YUV2RGBWRAPPER(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA, 1)
  1954. YUV2RGBWRAPPER(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB, 1)
  1955. #endif
  1956. YUV2RGBWRAPPER(yuv2, rgb_full, bgrx32_full, AV_PIX_FMT_BGRA, 0)
  1957. YUV2RGBWRAPPER(yuv2, rgb_full, xbgr32_full, AV_PIX_FMT_ABGR, 0)
  1958. YUV2RGBWRAPPER(yuv2, rgb_full, rgbx32_full, AV_PIX_FMT_RGBA, 0)
  1959. YUV2RGBWRAPPER(yuv2, rgb_full, xrgb32_full, AV_PIX_FMT_ARGB, 0)
  1960. #endif
  1961. YUV2RGBWRAPPER(yuv2, rgb_full, bgr24_full, AV_PIX_FMT_BGR24, 0)
  1962. YUV2RGBWRAPPER(yuv2, rgb_full, rgb24_full, AV_PIX_FMT_RGB24, 0)
  1963. YUV2RGBWRAPPER(yuv2, rgb_full, bgr4_byte_full, AV_PIX_FMT_BGR4_BYTE, 0)
  1964. YUV2RGBWRAPPER(yuv2, rgb_full, rgb4_byte_full, AV_PIX_FMT_RGB4_BYTE, 0)
  1965. YUV2RGBWRAPPER(yuv2, rgb_full, bgr8_full, AV_PIX_FMT_BGR8, 0)
  1966. YUV2RGBWRAPPER(yuv2, rgb_full, rgb8_full, AV_PIX_FMT_RGB8, 0)
  1967. static void
  1968. yuv2gbrp_full_X_c(SwsContext *c, const int16_t *lumFilter,
  1969. const int16_t **lumSrc, int lumFilterSize,
  1970. const int16_t *chrFilter, const int16_t **chrUSrc,
  1971. const int16_t **chrVSrc, int chrFilterSize,
  1972. const int16_t **alpSrc, uint8_t **dest,
  1973. int dstW, int y)
  1974. {
  1975. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
  1976. int i;
  1977. int hasAlpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) && alpSrc;
  1978. uint16_t **dest16 = (uint16_t**)dest;
  1979. int SH = 22 + 8 - desc->comp[0].depth;
  1980. int A = 0; // init to silence warning
  1981. for (i = 0; i < dstW; i++) {
  1982. int j;
  1983. int Y = 1 << 9;
  1984. int U = (1 << 9) - (128 << 19);
  1985. int V = (1 << 9) - (128 << 19);
  1986. int R, G, B;
  1987. for (j = 0; j < lumFilterSize; j++)
  1988. Y += lumSrc[j][i] * lumFilter[j];
  1989. for (j = 0; j < chrFilterSize; j++) {
  1990. U += chrUSrc[j][i] * chrFilter[j];
  1991. V += chrVSrc[j][i] * chrFilter[j];
  1992. }
  1993. Y >>= 10;
  1994. U >>= 10;
  1995. V >>= 10;
  1996. if (hasAlpha) {
  1997. A = 1 << 18;
  1998. for (j = 0; j < lumFilterSize; j++)
  1999. A += alpSrc[j][i] * lumFilter[j];
  2000. if (A & 0xF8000000)
  2001. A = av_clip_uintp2(A, 27);
  2002. }
  2003. Y -= c->yuv2rgb_y_offset;
  2004. Y *= c->yuv2rgb_y_coeff;
  2005. Y += 1 << (SH-1);
  2006. R = Y + V * c->yuv2rgb_v2r_coeff;
  2007. G = Y + V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  2008. B = Y + U * c->yuv2rgb_u2b_coeff;
  2009. if ((R | G | B) & 0xC0000000) {
  2010. R = av_clip_uintp2(R, 30);
  2011. G = av_clip_uintp2(G, 30);
  2012. B = av_clip_uintp2(B, 30);
  2013. }
  2014. if (SH != 22) {
  2015. dest16[0][i] = G >> SH;
  2016. dest16[1][i] = B >> SH;
  2017. dest16[2][i] = R >> SH;
  2018. if (hasAlpha)
  2019. dest16[3][i] = A >> (SH - 3);
  2020. } else {
  2021. dest[0][i] = G >> 22;
  2022. dest[1][i] = B >> 22;
  2023. dest[2][i] = R >> 22;
  2024. if (hasAlpha)
  2025. dest[3][i] = A >> 19;
  2026. }
  2027. }
  2028. if (SH != 22 && (!isBE(c->dstFormat)) != (!HAVE_BIGENDIAN)) {
  2029. for (i = 0; i < dstW; i++) {
  2030. dest16[0][i] = av_bswap16(dest16[0][i]);
  2031. dest16[1][i] = av_bswap16(dest16[1][i]);
  2032. dest16[2][i] = av_bswap16(dest16[2][i]);
  2033. if (hasAlpha)
  2034. dest16[3][i] = av_bswap16(dest16[3][i]);
  2035. }
  2036. }
  2037. }
  2038. static void
  2039. yuv2gbrp16_full_X_c(SwsContext *c, const int16_t *lumFilter,
  2040. const int16_t **lumSrcx, int lumFilterSize,
  2041. const int16_t *chrFilter, const int16_t **chrUSrcx,
  2042. const int16_t **chrVSrcx, int chrFilterSize,
  2043. const int16_t **alpSrcx, uint8_t **dest,
  2044. int dstW, int y)
  2045. {
  2046. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
  2047. int i;
  2048. int hasAlpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) && alpSrcx;
  2049. uint16_t **dest16 = (uint16_t**)dest;
  2050. const int32_t **lumSrc = (const int32_t**)lumSrcx;
  2051. const int32_t **chrUSrc = (const int32_t**)chrUSrcx;
  2052. const int32_t **chrVSrc = (const int32_t**)chrVSrcx;
  2053. const int32_t **alpSrc = (const int32_t**)alpSrcx;
  2054. for (i = 0; i < dstW; i++) {
  2055. int j;
  2056. int Y = -0x40000000;
  2057. int U = -(128 << 23);
  2058. int V = -(128 << 23);
  2059. int R, G, B, A;
  2060. for (j = 0; j < lumFilterSize; j++)
  2061. Y += lumSrc[j][i] * (unsigned)lumFilter[j];
  2062. for (j = 0; j < chrFilterSize; j++) {
  2063. U += chrUSrc[j][i] * (unsigned)chrFilter[j];
  2064. V += chrVSrc[j][i] * (unsigned)chrFilter[j];
  2065. }
  2066. Y >>= 14;
  2067. Y += 0x10000;
  2068. U >>= 14;
  2069. V >>= 14;
  2070. if (hasAlpha) {
  2071. A = -0x40000000;
  2072. for (j = 0; j < lumFilterSize; j++)
  2073. A += alpSrc[j][i] * (unsigned)lumFilter[j];
  2074. A >>= 1;
  2075. A += 0x20002000;
  2076. }
  2077. Y -= c->yuv2rgb_y_offset;
  2078. Y *= c->yuv2rgb_y_coeff;
  2079. Y += (1 << 13) - (1 << 29);
  2080. R = V * c->yuv2rgb_v2r_coeff;
  2081. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  2082. B = U * c->yuv2rgb_u2b_coeff;
  2083. dest16[2][i] = av_clip_uintp2(((Y + R) >> 14) + (1<<15), 16);
  2084. dest16[0][i] = av_clip_uintp2(((Y + G) >> 14) + (1<<15), 16);
  2085. dest16[1][i] = av_clip_uintp2(((Y + B) >> 14) + (1<<15), 16);
  2086. if (hasAlpha)
  2087. dest16[3][i] = av_clip_uintp2(A, 30) >> 14;
  2088. }
  2089. if ((!isBE(c->dstFormat)) != (!HAVE_BIGENDIAN)) {
  2090. for (i = 0; i < dstW; i++) {
  2091. dest16[0][i] = av_bswap16(dest16[0][i]);
  2092. dest16[1][i] = av_bswap16(dest16[1][i]);
  2093. dest16[2][i] = av_bswap16(dest16[2][i]);
  2094. if (hasAlpha)
  2095. dest16[3][i] = av_bswap16(dest16[3][i]);
  2096. }
  2097. }
  2098. }
  2099. static void
  2100. yuv2gbrpf32_full_X_c(SwsContext *c, const int16_t *lumFilter,
  2101. const int16_t **lumSrcx, int lumFilterSize,
  2102. const int16_t *chrFilter, const int16_t **chrUSrcx,
  2103. const int16_t **chrVSrcx, int chrFilterSize,
  2104. const int16_t **alpSrcx, uint8_t **dest,
  2105. int dstW, int y)
  2106. {
  2107. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
  2108. int i;
  2109. int hasAlpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) && alpSrcx;
  2110. uint32_t **dest32 = (uint32_t**)dest;
  2111. const int32_t **lumSrc = (const int32_t**)lumSrcx;
  2112. const int32_t **chrUSrc = (const int32_t**)chrUSrcx;
  2113. const int32_t **chrVSrc = (const int32_t**)chrVSrcx;
  2114. const int32_t **alpSrc = (const int32_t**)alpSrcx;
  2115. static const float float_mult = 1.0f / 65535.0f;
  2116. for (i = 0; i < dstW; i++) {
  2117. int j;
  2118. int Y = -0x40000000;
  2119. int U = -(128 << 23);
  2120. int V = -(128 << 23);
  2121. int R, G, B, A;
  2122. for (j = 0; j < lumFilterSize; j++)
  2123. Y += lumSrc[j][i] * (unsigned)lumFilter[j];
  2124. for (j = 0; j < chrFilterSize; j++) {
  2125. U += chrUSrc[j][i] * (unsigned)chrFilter[j];
  2126. V += chrVSrc[j][i] * (unsigned)chrFilter[j];
  2127. }
  2128. Y >>= 14;
  2129. Y += 0x10000;
  2130. U >>= 14;
  2131. V >>= 14;
  2132. if (hasAlpha) {
  2133. A = -0x40000000;
  2134. for (j = 0; j < lumFilterSize; j++)
  2135. A += alpSrc[j][i] * (unsigned)lumFilter[j];
  2136. A >>= 1;
  2137. A += 0x20002000;
  2138. }
  2139. Y -= c->yuv2rgb_y_offset;
  2140. Y *= c->yuv2rgb_y_coeff;
  2141. Y += (1 << 13) - (1 << 29);
  2142. R = V * c->yuv2rgb_v2r_coeff;
  2143. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  2144. B = U * c->yuv2rgb_u2b_coeff;
  2145. R = av_clip_uintp2(((Y + R) >> 14) + (1<<15), 16);
  2146. G = av_clip_uintp2(((Y + G) >> 14) + (1<<15), 16);
  2147. B = av_clip_uintp2(((Y + B) >> 14) + (1<<15), 16);
  2148. dest32[0][i] = av_float2int(float_mult * (float)G);
  2149. dest32[1][i] = av_float2int(float_mult * (float)B);
  2150. dest32[2][i] = av_float2int(float_mult * (float)R);
  2151. if (hasAlpha)
  2152. dest32[3][i] = av_float2int(float_mult * (float)(av_clip_uintp2(A, 30) >> 14));
  2153. }
  2154. if ((!isBE(c->dstFormat)) != (!HAVE_BIGENDIAN)) {
  2155. for (i = 0; i < dstW; i++) {
  2156. dest32[0][i] = av_bswap32(dest32[0][i]);
  2157. dest32[1][i] = av_bswap32(dest32[1][i]);
  2158. dest32[2][i] = av_bswap32(dest32[2][i]);
  2159. if (hasAlpha)
  2160. dest32[3][i] = av_bswap32(dest32[3][i]);
  2161. }
  2162. }
  2163. }
  2164. static void
  2165. yuv2ya8_1_c(SwsContext *c, const int16_t *buf0,
  2166. const int16_t *ubuf[2], const int16_t *vbuf[2],
  2167. const int16_t *abuf0, uint8_t *dest, int dstW,
  2168. int uvalpha, int y)
  2169. {
  2170. int hasAlpha = !!abuf0;
  2171. int i;
  2172. for (i = 0; i < dstW; i++) {
  2173. int Y = (buf0[i] + 64) >> 7;
  2174. int A;
  2175. Y = av_clip_uint8(Y);
  2176. if (hasAlpha) {
  2177. A = (abuf0[i] + 64) >> 7;
  2178. if (A & 0x100)
  2179. A = av_clip_uint8(A);
  2180. }
  2181. dest[i * 2 ] = Y;
  2182. dest[i * 2 + 1] = hasAlpha ? A : 255;
  2183. }
  2184. }
  2185. static void
  2186. yuv2ya8_2_c(SwsContext *c, const int16_t *buf[2],
  2187. const int16_t *ubuf[2], const int16_t *vbuf[2],
  2188. const int16_t *abuf[2], uint8_t *dest, int dstW,
  2189. int yalpha, int uvalpha, int y)
  2190. {
  2191. int hasAlpha = abuf && abuf[0] && abuf[1];
  2192. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  2193. *abuf0 = hasAlpha ? abuf[0] : NULL,
  2194. *abuf1 = hasAlpha ? abuf[1] : NULL;
  2195. int yalpha1 = 4096 - yalpha;
  2196. int i;
  2197. av_assert2(yalpha <= 4096U);
  2198. for (i = 0; i < dstW; i++) {
  2199. int Y = (buf0[i] * yalpha1 + buf1[i] * yalpha) >> 19;
  2200. int A;
  2201. Y = av_clip_uint8(Y);
  2202. if (hasAlpha) {
  2203. A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha) >> 19;
  2204. A = av_clip_uint8(A);
  2205. }
  2206. dest[i * 2 ] = Y;
  2207. dest[i * 2 + 1] = hasAlpha ? A : 255;
  2208. }
  2209. }
  2210. static void
  2211. yuv2ya8_X_c(SwsContext *c, const int16_t *lumFilter,
  2212. const int16_t **lumSrc, int lumFilterSize,
  2213. const int16_t *chrFilter, const int16_t **chrUSrc,
  2214. const int16_t **chrVSrc, int chrFilterSize,
  2215. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  2216. {
  2217. int hasAlpha = !!alpSrc;
  2218. int i;
  2219. for (i = 0; i < dstW; i++) {
  2220. int j;
  2221. int Y = 1 << 18, A = 1 << 18;
  2222. for (j = 0; j < lumFilterSize; j++)
  2223. Y += lumSrc[j][i] * lumFilter[j];
  2224. Y >>= 19;
  2225. if (Y & 0x100)
  2226. Y = av_clip_uint8(Y);
  2227. if (hasAlpha) {
  2228. for (j = 0; j < lumFilterSize; j++)
  2229. A += alpSrc[j][i] * lumFilter[j];
  2230. A >>= 19;
  2231. if (A & 0x100)
  2232. A = av_clip_uint8(A);
  2233. }
  2234. dest[2 * i ] = Y;
  2235. dest[2 * i + 1] = hasAlpha ? A : 255;
  2236. }
  2237. }
  2238. static void
  2239. yuv2ayuv64le_X_c(SwsContext *c, const int16_t *lumFilter,
  2240. const int16_t **_lumSrc, int lumFilterSize,
  2241. const int16_t *chrFilter, const int16_t **_chrUSrc,
  2242. const int16_t **_chrVSrc, int chrFilterSize,
  2243. const int16_t **_alpSrc, uint8_t *dest, int dstW, int y)
  2244. {
  2245. const int32_t **lumSrc = (const int32_t **) _lumSrc,
  2246. **chrUSrc = (const int32_t **) _chrUSrc,
  2247. **chrVSrc = (const int32_t **) _chrVSrc,
  2248. **alpSrc = (const int32_t **) _alpSrc;
  2249. int hasAlpha = !!alpSrc;
  2250. int i;
  2251. for (i = 0; i < dstW; i++) {
  2252. int Y = 1 << 14, U = 1 << 14;
  2253. int V = 1 << 14, A = 1 << 14;
  2254. int j;
  2255. Y -= 0x40000000;
  2256. U -= 0x40000000;
  2257. V -= 0x40000000;
  2258. A -= 0x40000000;
  2259. for (j = 0; j < lumFilterSize; j++)
  2260. Y += lumSrc[j][i] * (unsigned)lumFilter[j];
  2261. for (j = 0; j < chrFilterSize; j++)
  2262. U += chrUSrc[j][i] * (unsigned)chrFilter[j];
  2263. for (j = 0; j < chrFilterSize; j++)
  2264. V += chrVSrc[j][i] * (unsigned)chrFilter[j];
  2265. if (hasAlpha)
  2266. for (j = 0; j < lumFilterSize; j++)
  2267. A += alpSrc[j][i] * (unsigned)lumFilter[j];
  2268. Y = 0x8000 + av_clip_int16(Y >> 15);
  2269. U = 0x8000 + av_clip_int16(U >> 15);
  2270. V = 0x8000 + av_clip_int16(V >> 15);
  2271. A = 0x8000 + av_clip_int16(A >> 15);
  2272. AV_WL16(dest + 8 * i, hasAlpha ? A : 65535);
  2273. AV_WL16(dest + 8 * i + 2, Y);
  2274. AV_WL16(dest + 8 * i + 4, U);
  2275. AV_WL16(dest + 8 * i + 6, V);
  2276. }
  2277. }
  2278. av_cold void ff_sws_init_output_funcs(SwsContext *c,
  2279. yuv2planar1_fn *yuv2plane1,
  2280. yuv2planarX_fn *yuv2planeX,
  2281. yuv2interleavedX_fn *yuv2nv12cX,
  2282. yuv2packed1_fn *yuv2packed1,
  2283. yuv2packed2_fn *yuv2packed2,
  2284. yuv2packedX_fn *yuv2packedX,
  2285. yuv2anyX_fn *yuv2anyX)
  2286. {
  2287. enum AVPixelFormat dstFormat = c->dstFormat;
  2288. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
  2289. if (isSemiPlanarYUV(dstFormat) && isDataInHighBits(dstFormat)) {
  2290. av_assert0(desc->comp[0].depth == 10);
  2291. *yuv2plane1 = isBE(dstFormat) ? yuv2p010l1_BE_c : yuv2p010l1_LE_c;
  2292. *yuv2planeX = isBE(dstFormat) ? yuv2p010lX_BE_c : yuv2p010lX_LE_c;
  2293. *yuv2nv12cX = isBE(dstFormat) ? yuv2p010cX_BE_c : yuv2p010cX_LE_c;
  2294. } else if (is16BPS(dstFormat)) {
  2295. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c : yuv2planeX_16LE_c;
  2296. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c : yuv2plane1_16LE_c;
  2297. if (isSemiPlanarYUV(dstFormat)) {
  2298. *yuv2nv12cX = isBE(dstFormat) ? yuv2nv12cX_16BE_c : yuv2nv12cX_16LE_c;
  2299. }
  2300. } else if (isNBPS(dstFormat)) {
  2301. if (desc->comp[0].depth == 9) {
  2302. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c : yuv2planeX_9LE_c;
  2303. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c : yuv2plane1_9LE_c;
  2304. } else if (desc->comp[0].depth == 10) {
  2305. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c : yuv2planeX_10LE_c;
  2306. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c : yuv2plane1_10LE_c;
  2307. } else if (desc->comp[0].depth == 12) {
  2308. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_12BE_c : yuv2planeX_12LE_c;
  2309. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_12BE_c : yuv2plane1_12LE_c;
  2310. } else if (desc->comp[0].depth == 14) {
  2311. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_14BE_c : yuv2planeX_14LE_c;
  2312. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_14BE_c : yuv2plane1_14LE_c;
  2313. } else
  2314. av_assert0(0);
  2315. } else if (dstFormat == AV_PIX_FMT_GRAYF32BE) {
  2316. *yuv2planeX = yuv2planeX_floatBE_c;
  2317. *yuv2plane1 = yuv2plane1_floatBE_c;
  2318. } else if (dstFormat == AV_PIX_FMT_GRAYF32LE) {
  2319. *yuv2planeX = yuv2planeX_floatLE_c;
  2320. *yuv2plane1 = yuv2plane1_floatLE_c;
  2321. } else {
  2322. *yuv2plane1 = yuv2plane1_8_c;
  2323. *yuv2planeX = yuv2planeX_8_c;
  2324. if (isSemiPlanarYUV(dstFormat))
  2325. *yuv2nv12cX = yuv2nv12cX_c;
  2326. }
  2327. if(c->flags & SWS_FULL_CHR_H_INT) {
  2328. switch (dstFormat) {
  2329. case AV_PIX_FMT_RGBA:
  2330. #if CONFIG_SMALL
  2331. *yuv2packedX = yuv2rgba32_full_X_c;
  2332. *yuv2packed2 = yuv2rgba32_full_2_c;
  2333. *yuv2packed1 = yuv2rgba32_full_1_c;
  2334. #else
  2335. #if CONFIG_SWSCALE_ALPHA
  2336. if (c->needAlpha) {
  2337. *yuv2packedX = yuv2rgba32_full_X_c;
  2338. *yuv2packed2 = yuv2rgba32_full_2_c;
  2339. *yuv2packed1 = yuv2rgba32_full_1_c;
  2340. } else
  2341. #endif /* CONFIG_SWSCALE_ALPHA */
  2342. {
  2343. *yuv2packedX = yuv2rgbx32_full_X_c;
  2344. *yuv2packed2 = yuv2rgbx32_full_2_c;
  2345. *yuv2packed1 = yuv2rgbx32_full_1_c;
  2346. }
  2347. #endif /* !CONFIG_SMALL */
  2348. break;
  2349. case AV_PIX_FMT_ARGB:
  2350. #if CONFIG_SMALL
  2351. *yuv2packedX = yuv2argb32_full_X_c;
  2352. *yuv2packed2 = yuv2argb32_full_2_c;
  2353. *yuv2packed1 = yuv2argb32_full_1_c;
  2354. #else
  2355. #if CONFIG_SWSCALE_ALPHA
  2356. if (c->needAlpha) {
  2357. *yuv2packedX = yuv2argb32_full_X_c;
  2358. *yuv2packed2 = yuv2argb32_full_2_c;
  2359. *yuv2packed1 = yuv2argb32_full_1_c;
  2360. } else
  2361. #endif /* CONFIG_SWSCALE_ALPHA */
  2362. {
  2363. *yuv2packedX = yuv2xrgb32_full_X_c;
  2364. *yuv2packed2 = yuv2xrgb32_full_2_c;
  2365. *yuv2packed1 = yuv2xrgb32_full_1_c;
  2366. }
  2367. #endif /* !CONFIG_SMALL */
  2368. break;
  2369. case AV_PIX_FMT_BGRA:
  2370. #if CONFIG_SMALL
  2371. *yuv2packedX = yuv2bgra32_full_X_c;
  2372. *yuv2packed2 = yuv2bgra32_full_2_c;
  2373. *yuv2packed1 = yuv2bgra32_full_1_c;
  2374. #else
  2375. #if CONFIG_SWSCALE_ALPHA
  2376. if (c->needAlpha) {
  2377. *yuv2packedX = yuv2bgra32_full_X_c;
  2378. *yuv2packed2 = yuv2bgra32_full_2_c;
  2379. *yuv2packed1 = yuv2bgra32_full_1_c;
  2380. } else
  2381. #endif /* CONFIG_SWSCALE_ALPHA */
  2382. {
  2383. *yuv2packedX = yuv2bgrx32_full_X_c;
  2384. *yuv2packed2 = yuv2bgrx32_full_2_c;
  2385. *yuv2packed1 = yuv2bgrx32_full_1_c;
  2386. }
  2387. #endif /* !CONFIG_SMALL */
  2388. break;
  2389. case AV_PIX_FMT_ABGR:
  2390. #if CONFIG_SMALL
  2391. *yuv2packedX = yuv2abgr32_full_X_c;
  2392. *yuv2packed2 = yuv2abgr32_full_2_c;
  2393. *yuv2packed1 = yuv2abgr32_full_1_c;
  2394. #else
  2395. #if CONFIG_SWSCALE_ALPHA
  2396. if (c->needAlpha) {
  2397. *yuv2packedX = yuv2abgr32_full_X_c;
  2398. *yuv2packed2 = yuv2abgr32_full_2_c;
  2399. *yuv2packed1 = yuv2abgr32_full_1_c;
  2400. } else
  2401. #endif /* CONFIG_SWSCALE_ALPHA */
  2402. {
  2403. *yuv2packedX = yuv2xbgr32_full_X_c;
  2404. *yuv2packed2 = yuv2xbgr32_full_2_c;
  2405. *yuv2packed1 = yuv2xbgr32_full_1_c;
  2406. }
  2407. #endif /* !CONFIG_SMALL */
  2408. break;
  2409. case AV_PIX_FMT_RGBA64LE:
  2410. #if CONFIG_SWSCALE_ALPHA
  2411. if (c->needAlpha) {
  2412. *yuv2packedX = yuv2rgba64le_full_X_c;
  2413. *yuv2packed2 = yuv2rgba64le_full_2_c;
  2414. *yuv2packed1 = yuv2rgba64le_full_1_c;
  2415. } else
  2416. #endif /* CONFIG_SWSCALE_ALPHA */
  2417. {
  2418. *yuv2packedX = yuv2rgbx64le_full_X_c;
  2419. *yuv2packed2 = yuv2rgbx64le_full_2_c;
  2420. *yuv2packed1 = yuv2rgbx64le_full_1_c;
  2421. }
  2422. break;
  2423. case AV_PIX_FMT_RGBA64BE:
  2424. #if CONFIG_SWSCALE_ALPHA
  2425. if (c->needAlpha) {
  2426. *yuv2packedX = yuv2rgba64be_full_X_c;
  2427. *yuv2packed2 = yuv2rgba64be_full_2_c;
  2428. *yuv2packed1 = yuv2rgba64be_full_1_c;
  2429. } else
  2430. #endif /* CONFIG_SWSCALE_ALPHA */
  2431. {
  2432. *yuv2packedX = yuv2rgbx64be_full_X_c;
  2433. *yuv2packed2 = yuv2rgbx64be_full_2_c;
  2434. *yuv2packed1 = yuv2rgbx64be_full_1_c;
  2435. }
  2436. break;
  2437. case AV_PIX_FMT_BGRA64LE:
  2438. #if CONFIG_SWSCALE_ALPHA
  2439. if (c->needAlpha) {
  2440. *yuv2packedX = yuv2bgra64le_full_X_c;
  2441. *yuv2packed2 = yuv2bgra64le_full_2_c;
  2442. *yuv2packed1 = yuv2bgra64le_full_1_c;
  2443. } else
  2444. #endif /* CONFIG_SWSCALE_ALPHA */
  2445. {
  2446. *yuv2packedX = yuv2bgrx64le_full_X_c;
  2447. *yuv2packed2 = yuv2bgrx64le_full_2_c;
  2448. *yuv2packed1 = yuv2bgrx64le_full_1_c;
  2449. }
  2450. break;
  2451. case AV_PIX_FMT_BGRA64BE:
  2452. #if CONFIG_SWSCALE_ALPHA
  2453. if (c->needAlpha) {
  2454. *yuv2packedX = yuv2bgra64be_full_X_c;
  2455. *yuv2packed2 = yuv2bgra64be_full_2_c;
  2456. *yuv2packed1 = yuv2bgra64be_full_1_c;
  2457. } else
  2458. #endif /* CONFIG_SWSCALE_ALPHA */
  2459. {
  2460. *yuv2packedX = yuv2bgrx64be_full_X_c;
  2461. *yuv2packed2 = yuv2bgrx64be_full_2_c;
  2462. *yuv2packed1 = yuv2bgrx64be_full_1_c;
  2463. }
  2464. break;
  2465. case AV_PIX_FMT_RGB24:
  2466. *yuv2packedX = yuv2rgb24_full_X_c;
  2467. *yuv2packed2 = yuv2rgb24_full_2_c;
  2468. *yuv2packed1 = yuv2rgb24_full_1_c;
  2469. break;
  2470. case AV_PIX_FMT_BGR24:
  2471. *yuv2packedX = yuv2bgr24_full_X_c;
  2472. *yuv2packed2 = yuv2bgr24_full_2_c;
  2473. *yuv2packed1 = yuv2bgr24_full_1_c;
  2474. break;
  2475. case AV_PIX_FMT_RGB48LE:
  2476. *yuv2packedX = yuv2rgb48le_full_X_c;
  2477. *yuv2packed2 = yuv2rgb48le_full_2_c;
  2478. *yuv2packed1 = yuv2rgb48le_full_1_c;
  2479. break;
  2480. case AV_PIX_FMT_BGR48LE:
  2481. *yuv2packedX = yuv2bgr48le_full_X_c;
  2482. *yuv2packed2 = yuv2bgr48le_full_2_c;
  2483. *yuv2packed1 = yuv2bgr48le_full_1_c;
  2484. break;
  2485. case AV_PIX_FMT_RGB48BE:
  2486. *yuv2packedX = yuv2rgb48be_full_X_c;
  2487. *yuv2packed2 = yuv2rgb48be_full_2_c;
  2488. *yuv2packed1 = yuv2rgb48be_full_1_c;
  2489. break;
  2490. case AV_PIX_FMT_BGR48BE:
  2491. *yuv2packedX = yuv2bgr48be_full_X_c;
  2492. *yuv2packed2 = yuv2bgr48be_full_2_c;
  2493. *yuv2packed1 = yuv2bgr48be_full_1_c;
  2494. break;
  2495. case AV_PIX_FMT_BGR4_BYTE:
  2496. *yuv2packedX = yuv2bgr4_byte_full_X_c;
  2497. *yuv2packed2 = yuv2bgr4_byte_full_2_c;
  2498. *yuv2packed1 = yuv2bgr4_byte_full_1_c;
  2499. break;
  2500. case AV_PIX_FMT_RGB4_BYTE:
  2501. *yuv2packedX = yuv2rgb4_byte_full_X_c;
  2502. *yuv2packed2 = yuv2rgb4_byte_full_2_c;
  2503. *yuv2packed1 = yuv2rgb4_byte_full_1_c;
  2504. break;
  2505. case AV_PIX_FMT_BGR8:
  2506. *yuv2packedX = yuv2bgr8_full_X_c;
  2507. *yuv2packed2 = yuv2bgr8_full_2_c;
  2508. *yuv2packed1 = yuv2bgr8_full_1_c;
  2509. break;
  2510. case AV_PIX_FMT_RGB8:
  2511. *yuv2packedX = yuv2rgb8_full_X_c;
  2512. *yuv2packed2 = yuv2rgb8_full_2_c;
  2513. *yuv2packed1 = yuv2rgb8_full_1_c;
  2514. break;
  2515. case AV_PIX_FMT_GBRP:
  2516. case AV_PIX_FMT_GBRP9BE:
  2517. case AV_PIX_FMT_GBRP9LE:
  2518. case AV_PIX_FMT_GBRP10BE:
  2519. case AV_PIX_FMT_GBRP10LE:
  2520. case AV_PIX_FMT_GBRP12BE:
  2521. case AV_PIX_FMT_GBRP12LE:
  2522. case AV_PIX_FMT_GBRP14BE:
  2523. case AV_PIX_FMT_GBRP14LE:
  2524. case AV_PIX_FMT_GBRAP:
  2525. case AV_PIX_FMT_GBRAP10BE:
  2526. case AV_PIX_FMT_GBRAP10LE:
  2527. case AV_PIX_FMT_GBRAP12BE:
  2528. case AV_PIX_FMT_GBRAP12LE:
  2529. *yuv2anyX = yuv2gbrp_full_X_c;
  2530. break;
  2531. case AV_PIX_FMT_GBRP16BE:
  2532. case AV_PIX_FMT_GBRP16LE:
  2533. case AV_PIX_FMT_GBRAP16BE:
  2534. case AV_PIX_FMT_GBRAP16LE:
  2535. *yuv2anyX = yuv2gbrp16_full_X_c;
  2536. break;
  2537. case AV_PIX_FMT_GBRPF32BE:
  2538. case AV_PIX_FMT_GBRPF32LE:
  2539. case AV_PIX_FMT_GBRAPF32BE:
  2540. case AV_PIX_FMT_GBRAPF32LE:
  2541. *yuv2anyX = yuv2gbrpf32_full_X_c;
  2542. break;
  2543. }
  2544. if (!*yuv2packedX && !*yuv2anyX)
  2545. goto YUV_PACKED;
  2546. } else {
  2547. YUV_PACKED:
  2548. switch (dstFormat) {
  2549. case AV_PIX_FMT_RGBA64LE:
  2550. #if CONFIG_SWSCALE_ALPHA
  2551. if (c->needAlpha) {
  2552. *yuv2packed1 = yuv2rgba64le_1_c;
  2553. *yuv2packed2 = yuv2rgba64le_2_c;
  2554. *yuv2packedX = yuv2rgba64le_X_c;
  2555. } else
  2556. #endif /* CONFIG_SWSCALE_ALPHA */
  2557. {
  2558. *yuv2packed1 = yuv2rgbx64le_1_c;
  2559. *yuv2packed2 = yuv2rgbx64le_2_c;
  2560. *yuv2packedX = yuv2rgbx64le_X_c;
  2561. }
  2562. break;
  2563. case AV_PIX_FMT_RGBA64BE:
  2564. #if CONFIG_SWSCALE_ALPHA
  2565. if (c->needAlpha) {
  2566. *yuv2packed1 = yuv2rgba64be_1_c;
  2567. *yuv2packed2 = yuv2rgba64be_2_c;
  2568. *yuv2packedX = yuv2rgba64be_X_c;
  2569. } else
  2570. #endif /* CONFIG_SWSCALE_ALPHA */
  2571. {
  2572. *yuv2packed1 = yuv2rgbx64be_1_c;
  2573. *yuv2packed2 = yuv2rgbx64be_2_c;
  2574. *yuv2packedX = yuv2rgbx64be_X_c;
  2575. }
  2576. break;
  2577. case AV_PIX_FMT_BGRA64LE:
  2578. #if CONFIG_SWSCALE_ALPHA
  2579. if (c->needAlpha) {
  2580. *yuv2packed1 = yuv2bgra64le_1_c;
  2581. *yuv2packed2 = yuv2bgra64le_2_c;
  2582. *yuv2packedX = yuv2bgra64le_X_c;
  2583. } else
  2584. #endif /* CONFIG_SWSCALE_ALPHA */
  2585. {
  2586. *yuv2packed1 = yuv2bgrx64le_1_c;
  2587. *yuv2packed2 = yuv2bgrx64le_2_c;
  2588. *yuv2packedX = yuv2bgrx64le_X_c;
  2589. }
  2590. break;
  2591. case AV_PIX_FMT_BGRA64BE:
  2592. #if CONFIG_SWSCALE_ALPHA
  2593. if (c->needAlpha) {
  2594. *yuv2packed1 = yuv2bgra64be_1_c;
  2595. *yuv2packed2 = yuv2bgra64be_2_c;
  2596. *yuv2packedX = yuv2bgra64be_X_c;
  2597. } else
  2598. #endif /* CONFIG_SWSCALE_ALPHA */
  2599. {
  2600. *yuv2packed1 = yuv2bgrx64be_1_c;
  2601. *yuv2packed2 = yuv2bgrx64be_2_c;
  2602. *yuv2packedX = yuv2bgrx64be_X_c;
  2603. }
  2604. break;
  2605. case AV_PIX_FMT_RGB48LE:
  2606. *yuv2packed1 = yuv2rgb48le_1_c;
  2607. *yuv2packed2 = yuv2rgb48le_2_c;
  2608. *yuv2packedX = yuv2rgb48le_X_c;
  2609. break;
  2610. case AV_PIX_FMT_RGB48BE:
  2611. *yuv2packed1 = yuv2rgb48be_1_c;
  2612. *yuv2packed2 = yuv2rgb48be_2_c;
  2613. *yuv2packedX = yuv2rgb48be_X_c;
  2614. break;
  2615. case AV_PIX_FMT_BGR48LE:
  2616. *yuv2packed1 = yuv2bgr48le_1_c;
  2617. *yuv2packed2 = yuv2bgr48le_2_c;
  2618. *yuv2packedX = yuv2bgr48le_X_c;
  2619. break;
  2620. case AV_PIX_FMT_BGR48BE:
  2621. *yuv2packed1 = yuv2bgr48be_1_c;
  2622. *yuv2packed2 = yuv2bgr48be_2_c;
  2623. *yuv2packedX = yuv2bgr48be_X_c;
  2624. break;
  2625. case AV_PIX_FMT_RGB32:
  2626. case AV_PIX_FMT_BGR32:
  2627. #if CONFIG_SMALL
  2628. *yuv2packed1 = yuv2rgb32_1_c;
  2629. *yuv2packed2 = yuv2rgb32_2_c;
  2630. *yuv2packedX = yuv2rgb32_X_c;
  2631. #else
  2632. #if CONFIG_SWSCALE_ALPHA
  2633. if (c->needAlpha) {
  2634. *yuv2packed1 = yuv2rgba32_1_c;
  2635. *yuv2packed2 = yuv2rgba32_2_c;
  2636. *yuv2packedX = yuv2rgba32_X_c;
  2637. } else
  2638. #endif /* CONFIG_SWSCALE_ALPHA */
  2639. {
  2640. *yuv2packed1 = yuv2rgbx32_1_c;
  2641. *yuv2packed2 = yuv2rgbx32_2_c;
  2642. *yuv2packedX = yuv2rgbx32_X_c;
  2643. }
  2644. #endif /* !CONFIG_SMALL */
  2645. break;
  2646. case AV_PIX_FMT_RGB32_1:
  2647. case AV_PIX_FMT_BGR32_1:
  2648. #if CONFIG_SMALL
  2649. *yuv2packed1 = yuv2rgb32_1_1_c;
  2650. *yuv2packed2 = yuv2rgb32_1_2_c;
  2651. *yuv2packedX = yuv2rgb32_1_X_c;
  2652. #else
  2653. #if CONFIG_SWSCALE_ALPHA
  2654. if (c->needAlpha) {
  2655. *yuv2packed1 = yuv2rgba32_1_1_c;
  2656. *yuv2packed2 = yuv2rgba32_1_2_c;
  2657. *yuv2packedX = yuv2rgba32_1_X_c;
  2658. } else
  2659. #endif /* CONFIG_SWSCALE_ALPHA */
  2660. {
  2661. *yuv2packed1 = yuv2rgbx32_1_1_c;
  2662. *yuv2packed2 = yuv2rgbx32_1_2_c;
  2663. *yuv2packedX = yuv2rgbx32_1_X_c;
  2664. }
  2665. #endif /* !CONFIG_SMALL */
  2666. break;
  2667. case AV_PIX_FMT_RGB24:
  2668. *yuv2packed1 = yuv2rgb24_1_c;
  2669. *yuv2packed2 = yuv2rgb24_2_c;
  2670. *yuv2packedX = yuv2rgb24_X_c;
  2671. break;
  2672. case AV_PIX_FMT_BGR24:
  2673. *yuv2packed1 = yuv2bgr24_1_c;
  2674. *yuv2packed2 = yuv2bgr24_2_c;
  2675. *yuv2packedX = yuv2bgr24_X_c;
  2676. break;
  2677. case AV_PIX_FMT_RGB565LE:
  2678. case AV_PIX_FMT_RGB565BE:
  2679. case AV_PIX_FMT_BGR565LE:
  2680. case AV_PIX_FMT_BGR565BE:
  2681. *yuv2packed1 = yuv2rgb16_1_c;
  2682. *yuv2packed2 = yuv2rgb16_2_c;
  2683. *yuv2packedX = yuv2rgb16_X_c;
  2684. break;
  2685. case AV_PIX_FMT_RGB555LE:
  2686. case AV_PIX_FMT_RGB555BE:
  2687. case AV_PIX_FMT_BGR555LE:
  2688. case AV_PIX_FMT_BGR555BE:
  2689. *yuv2packed1 = yuv2rgb15_1_c;
  2690. *yuv2packed2 = yuv2rgb15_2_c;
  2691. *yuv2packedX = yuv2rgb15_X_c;
  2692. break;
  2693. case AV_PIX_FMT_RGB444LE:
  2694. case AV_PIX_FMT_RGB444BE:
  2695. case AV_PIX_FMT_BGR444LE:
  2696. case AV_PIX_FMT_BGR444BE:
  2697. *yuv2packed1 = yuv2rgb12_1_c;
  2698. *yuv2packed2 = yuv2rgb12_2_c;
  2699. *yuv2packedX = yuv2rgb12_X_c;
  2700. break;
  2701. case AV_PIX_FMT_RGB8:
  2702. case AV_PIX_FMT_BGR8:
  2703. *yuv2packed1 = yuv2rgb8_1_c;
  2704. *yuv2packed2 = yuv2rgb8_2_c;
  2705. *yuv2packedX = yuv2rgb8_X_c;
  2706. break;
  2707. case AV_PIX_FMT_RGB4:
  2708. case AV_PIX_FMT_BGR4:
  2709. *yuv2packed1 = yuv2rgb4_1_c;
  2710. *yuv2packed2 = yuv2rgb4_2_c;
  2711. *yuv2packedX = yuv2rgb4_X_c;
  2712. break;
  2713. case AV_PIX_FMT_RGB4_BYTE:
  2714. case AV_PIX_FMT_BGR4_BYTE:
  2715. *yuv2packed1 = yuv2rgb4b_1_c;
  2716. *yuv2packed2 = yuv2rgb4b_2_c;
  2717. *yuv2packedX = yuv2rgb4b_X_c;
  2718. break;
  2719. case AV_PIX_FMT_X2RGB10LE:
  2720. case AV_PIX_FMT_X2RGB10BE:
  2721. *yuv2packed1 = yuv2x2rgb10_1_c;
  2722. *yuv2packed2 = yuv2x2rgb10_2_c;
  2723. *yuv2packedX = yuv2x2rgb10_X_c;
  2724. break;
  2725. case AV_PIX_FMT_X2BGR10LE:
  2726. case AV_PIX_FMT_X2BGR10BE:
  2727. *yuv2packed1 = yuv2x2bgr10_1_c;
  2728. *yuv2packed2 = yuv2x2bgr10_2_c;
  2729. *yuv2packedX = yuv2x2bgr10_X_c;
  2730. break;
  2731. }
  2732. }
  2733. switch (dstFormat) {
  2734. case AV_PIX_FMT_MONOWHITE:
  2735. *yuv2packed1 = yuv2monowhite_1_c;
  2736. *yuv2packed2 = yuv2monowhite_2_c;
  2737. *yuv2packedX = yuv2monowhite_X_c;
  2738. break;
  2739. case AV_PIX_FMT_MONOBLACK:
  2740. *yuv2packed1 = yuv2monoblack_1_c;
  2741. *yuv2packed2 = yuv2monoblack_2_c;
  2742. *yuv2packedX = yuv2monoblack_X_c;
  2743. break;
  2744. case AV_PIX_FMT_YUYV422:
  2745. *yuv2packed1 = yuv2yuyv422_1_c;
  2746. *yuv2packed2 = yuv2yuyv422_2_c;
  2747. *yuv2packedX = yuv2yuyv422_X_c;
  2748. break;
  2749. case AV_PIX_FMT_YVYU422:
  2750. *yuv2packed1 = yuv2yvyu422_1_c;
  2751. *yuv2packed2 = yuv2yvyu422_2_c;
  2752. *yuv2packedX = yuv2yvyu422_X_c;
  2753. break;
  2754. case AV_PIX_FMT_UYVY422:
  2755. *yuv2packed1 = yuv2uyvy422_1_c;
  2756. *yuv2packed2 = yuv2uyvy422_2_c;
  2757. *yuv2packedX = yuv2uyvy422_X_c;
  2758. break;
  2759. case AV_PIX_FMT_YA8:
  2760. *yuv2packed1 = yuv2ya8_1_c;
  2761. *yuv2packed2 = yuv2ya8_2_c;
  2762. *yuv2packedX = yuv2ya8_X_c;
  2763. break;
  2764. case AV_PIX_FMT_YA16LE:
  2765. *yuv2packed1 = yuv2ya16le_1_c;
  2766. *yuv2packed2 = yuv2ya16le_2_c;
  2767. *yuv2packedX = yuv2ya16le_X_c;
  2768. break;
  2769. case AV_PIX_FMT_YA16BE:
  2770. *yuv2packed1 = yuv2ya16be_1_c;
  2771. *yuv2packed2 = yuv2ya16be_2_c;
  2772. *yuv2packedX = yuv2ya16be_X_c;
  2773. break;
  2774. case AV_PIX_FMT_AYUV64LE:
  2775. *yuv2packedX = yuv2ayuv64le_X_c;
  2776. break;
  2777. }
  2778. }