output.c 92 KB

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