output.c 95 KB

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