output.c 110 KB

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