output.c 106 KB

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