output.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129
  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 <assert.h>
  21. #include <math.h>
  22. #include <stdint.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include "libavutil/attributes.h"
  26. #include "libavutil/avutil.h"
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/bswap.h"
  29. #include "libavutil/cpu.h"
  30. #include "libavutil/intreadwrite.h"
  31. #include "libavutil/mathematics.h"
  32. #include "libavutil/pixdesc.h"
  33. #include "config.h"
  34. #include "rgb2rgb.h"
  35. #include "swscale.h"
  36. #include "swscale_internal.h"
  37. DECLARE_ALIGNED(8, const uint8_t, dither_2x2_4)[][8]={
  38. { 1, 3, 1, 3, 1, 3, 1, 3, },
  39. { 2, 0, 2, 0, 2, 0, 2, 0, },
  40. { 1, 3, 1, 3, 1, 3, 1, 3, },
  41. };
  42. DECLARE_ALIGNED(8, const uint8_t, dither_2x2_8)[][8]={
  43. { 6, 2, 6, 2, 6, 2, 6, 2, },
  44. { 0, 4, 0, 4, 0, 4, 0, 4, },
  45. { 6, 2, 6, 2, 6, 2, 6, 2, },
  46. };
  47. DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[][8]={
  48. { 8, 4, 11, 7, 8, 4, 11, 7, },
  49. { 2, 14, 1, 13, 2, 14, 1, 13, },
  50. { 10, 6, 9, 5, 10, 6, 9, 5, },
  51. { 0, 12, 3, 15, 0, 12, 3, 15, },
  52. { 8, 4, 11, 7, 8, 4, 11, 7, },
  53. };
  54. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[][8]={
  55. { 17, 9, 23, 15, 16, 8, 22, 14, },
  56. { 5, 29, 3, 27, 4, 28, 2, 26, },
  57. { 21, 13, 19, 11, 20, 12, 18, 10, },
  58. { 0, 24, 6, 30, 1, 25, 7, 31, },
  59. { 16, 8, 22, 14, 17, 9, 23, 15, },
  60. { 4, 28, 2, 26, 5, 29, 3, 27, },
  61. { 20, 12, 18, 10, 21, 13, 19, 11, },
  62. { 1, 25, 7, 31, 0, 24, 6, 30, },
  63. { 17, 9, 23, 15, 16, 8, 22, 14, },
  64. };
  65. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[][8]={
  66. { 0, 55, 14, 68, 3, 58, 17, 72, },
  67. { 37, 18, 50, 32, 40, 22, 54, 35, },
  68. { 9, 64, 5, 59, 13, 67, 8, 63, },
  69. { 46, 27, 41, 23, 49, 31, 44, 26, },
  70. { 2, 57, 16, 71, 1, 56, 15, 70, },
  71. { 39, 21, 52, 34, 38, 19, 51, 33, },
  72. { 11, 66, 7, 62, 10, 65, 6, 60, },
  73. { 48, 30, 43, 25, 47, 29, 42, 24, },
  74. { 0, 55, 14, 68, 3, 58, 17, 72, },
  75. };
  76. #if 1
  77. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[][8]={
  78. {117, 62, 158, 103, 113, 58, 155, 100, },
  79. { 34, 199, 21, 186, 31, 196, 17, 182, },
  80. {144, 89, 131, 76, 141, 86, 127, 72, },
  81. { 0, 165, 41, 206, 10, 175, 52, 217, },
  82. {110, 55, 151, 96, 120, 65, 162, 107, },
  83. { 28, 193, 14, 179, 38, 203, 24, 189, },
  84. {138, 83, 124, 69, 148, 93, 134, 79, },
  85. { 7, 172, 48, 213, 3, 168, 45, 210, },
  86. {117, 62, 158, 103, 113, 58, 155, 100, },
  87. };
  88. #elif 1
  89. // tries to correct a gamma of 1.5
  90. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[][8]={
  91. { 0, 143, 18, 200, 2, 156, 25, 215, },
  92. { 78, 28, 125, 64, 89, 36, 138, 74, },
  93. { 10, 180, 3, 161, 16, 195, 8, 175, },
  94. {109, 51, 93, 38, 121, 60, 105, 47, },
  95. { 1, 152, 23, 210, 0, 147, 20, 205, },
  96. { 85, 33, 134, 71, 81, 30, 130, 67, },
  97. { 14, 190, 6, 171, 12, 185, 5, 166, },
  98. {117, 57, 101, 44, 113, 54, 97, 41, },
  99. { 0, 143, 18, 200, 2, 156, 25, 215, },
  100. };
  101. #elif 1
  102. // tries to correct a gamma of 2.0
  103. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[][8]={
  104. { 0, 124, 8, 193, 0, 140, 12, 213, },
  105. { 55, 14, 104, 42, 66, 19, 119, 52, },
  106. { 3, 168, 1, 145, 6, 187, 3, 162, },
  107. { 86, 31, 70, 21, 99, 39, 82, 28, },
  108. { 0, 134, 11, 206, 0, 129, 9, 200, },
  109. { 62, 17, 114, 48, 58, 16, 109, 45, },
  110. { 5, 181, 2, 157, 4, 175, 1, 151, },
  111. { 95, 36, 78, 26, 90, 34, 74, 24, },
  112. { 0, 124, 8, 193, 0, 140, 12, 213, },
  113. };
  114. #else
  115. // tries to correct a gamma of 2.5
  116. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[][8]={
  117. { 0, 107, 3, 187, 0, 125, 6, 212, },
  118. { 39, 7, 86, 28, 49, 11, 102, 36, },
  119. { 1, 158, 0, 131, 3, 180, 1, 151, },
  120. { 68, 19, 52, 12, 81, 25, 64, 17, },
  121. { 0, 119, 5, 203, 0, 113, 4, 195, },
  122. { 45, 9, 96, 33, 42, 8, 91, 30, },
  123. { 2, 172, 1, 144, 2, 165, 0, 137, },
  124. { 77, 23, 60, 15, 72, 21, 56, 14, },
  125. { 0, 107, 3, 187, 0, 125, 6, 212, },
  126. };
  127. #endif
  128. #define output_pixel(pos, val, bias, signedness) \
  129. if (big_endian) { \
  130. AV_WB16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
  131. } else { \
  132. AV_WL16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
  133. }
  134. static av_always_inline void
  135. yuv2plane1_16_c_template(const int32_t *src, uint16_t *dest, int dstW,
  136. int big_endian, int output_bits)
  137. {
  138. int i;
  139. int shift = 3;
  140. av_assert0(output_bits == 16);
  141. for (i = 0; i < dstW; i++) {
  142. int val = src[i] + (1 << (shift - 1));
  143. output_pixel(&dest[i], val, 0, uint);
  144. }
  145. }
  146. static av_always_inline void
  147. yuv2planeX_16_c_template(const int16_t *filter, int filterSize,
  148. const int32_t **src, uint16_t *dest, int dstW,
  149. int big_endian, int output_bits)
  150. {
  151. int i;
  152. int shift = 15;
  153. av_assert0(output_bits == 16);
  154. for (i = 0; i < dstW; i++) {
  155. int val = 1 << (shift - 1);
  156. int j;
  157. /* range of val is [0,0x7FFFFFFF], so 31 bits, but with lanczos/spline
  158. * filters (or anything with negative coeffs, the range can be slightly
  159. * wider in both directions. To account for this overflow, we subtract
  160. * a constant so it always fits in the signed range (assuming a
  161. * reasonable filterSize), and re-add that at the end. */
  162. val -= 0x40000000;
  163. for (j = 0; j < filterSize; j++)
  164. val += src[j][i] * (unsigned)filter[j];
  165. output_pixel(&dest[i], val, 0x8000, int);
  166. }
  167. }
  168. #undef output_pixel
  169. #define output_pixel(pos, val) \
  170. if (big_endian) { \
  171. AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  172. } else { \
  173. AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  174. }
  175. static av_always_inline void
  176. yuv2plane1_10_c_template(const int16_t *src, uint16_t *dest, int dstW,
  177. int big_endian, int output_bits)
  178. {
  179. int i;
  180. int shift = 15 - output_bits;
  181. for (i = 0; i < dstW; i++) {
  182. int val = src[i] + (1 << (shift - 1));
  183. output_pixel(&dest[i], val);
  184. }
  185. }
  186. static av_always_inline void
  187. yuv2planeX_10_c_template(const int16_t *filter, int filterSize,
  188. const int16_t **src, uint16_t *dest, int dstW,
  189. int big_endian, int output_bits)
  190. {
  191. int i;
  192. int shift = 11 + 16 - output_bits;
  193. for (i = 0; i < dstW; i++) {
  194. int val = 1 << (shift - 1);
  195. int j;
  196. for (j = 0; j < filterSize; j++)
  197. val += src[j][i] * filter[j];
  198. output_pixel(&dest[i], val);
  199. }
  200. }
  201. #undef output_pixel
  202. #define yuv2NBPS(bits, BE_LE, is_be, template_size, typeX_t) \
  203. static void yuv2plane1_ ## bits ## BE_LE ## _c(const int16_t *src, \
  204. uint8_t *dest, int dstW, \
  205. const uint8_t *dither, int offset)\
  206. { \
  207. yuv2plane1_ ## template_size ## _c_template((const typeX_t *) src, \
  208. (uint16_t *) dest, dstW, is_be, bits); \
  209. }\
  210. static void yuv2planeX_ ## bits ## BE_LE ## _c(const int16_t *filter, int filterSize, \
  211. const int16_t **src, uint8_t *dest, int dstW, \
  212. const uint8_t *dither, int offset)\
  213. { \
  214. yuv2planeX_## template_size ## _c_template(filter, \
  215. filterSize, (const typeX_t **) src, \
  216. (uint16_t *) dest, dstW, is_be, bits); \
  217. }
  218. yuv2NBPS( 9, BE, 1, 10, int16_t)
  219. yuv2NBPS( 9, LE, 0, 10, int16_t)
  220. yuv2NBPS(10, BE, 1, 10, int16_t)
  221. yuv2NBPS(10, LE, 0, 10, int16_t)
  222. yuv2NBPS(12, BE, 1, 10, int16_t)
  223. yuv2NBPS(12, LE, 0, 10, int16_t)
  224. yuv2NBPS(14, BE, 1, 10, int16_t)
  225. yuv2NBPS(14, LE, 0, 10, int16_t)
  226. yuv2NBPS(16, BE, 1, 16, int32_t)
  227. yuv2NBPS(16, LE, 0, 16, int32_t)
  228. static void yuv2planeX_8_c(const int16_t *filter, int filterSize,
  229. const int16_t **src, uint8_t *dest, int dstW,
  230. const uint8_t *dither, int offset)
  231. {
  232. int i;
  233. for (i=0; i<dstW; i++) {
  234. int val = dither[(i + offset) & 7] << 12;
  235. int j;
  236. for (j=0; j<filterSize; j++)
  237. val += src[j][i] * filter[j];
  238. dest[i]= av_clip_uint8(val>>19);
  239. }
  240. }
  241. static void yuv2plane1_8_c(const int16_t *src, uint8_t *dest, int dstW,
  242. const uint8_t *dither, int offset)
  243. {
  244. int i;
  245. for (i=0; i<dstW; i++) {
  246. int val = (src[i] + dither[(i + offset) & 7]) >> 7;
  247. dest[i]= av_clip_uint8(val);
  248. }
  249. }
  250. static void yuv2nv12cX_c(SwsContext *c, const int16_t *chrFilter, int chrFilterSize,
  251. const int16_t **chrUSrc, const int16_t **chrVSrc,
  252. uint8_t *dest, int chrDstW)
  253. {
  254. enum AVPixelFormat dstFormat = c->dstFormat;
  255. const uint8_t *chrDither = c->chrDither8;
  256. int i;
  257. if (dstFormat == AV_PIX_FMT_NV12)
  258. for (i=0; i<chrDstW; i++) {
  259. int u = chrDither[i & 7] << 12;
  260. int v = chrDither[(i + 3) & 7] << 12;
  261. int j;
  262. for (j=0; j<chrFilterSize; j++) {
  263. u += chrUSrc[j][i] * chrFilter[j];
  264. v += chrVSrc[j][i] * chrFilter[j];
  265. }
  266. dest[2*i]= av_clip_uint8(u>>19);
  267. dest[2*i+1]= av_clip_uint8(v>>19);
  268. }
  269. else
  270. for (i=0; i<chrDstW; i++) {
  271. int u = chrDither[i & 7] << 12;
  272. int v = chrDither[(i + 3) & 7] << 12;
  273. int j;
  274. for (j=0; j<chrFilterSize; j++) {
  275. u += chrUSrc[j][i] * chrFilter[j];
  276. v += chrVSrc[j][i] * chrFilter[j];
  277. }
  278. dest[2*i]= av_clip_uint8(v>>19);
  279. dest[2*i+1]= av_clip_uint8(u>>19);
  280. }
  281. }
  282. #define accumulate_bit(acc, val) \
  283. acc <<= 1; \
  284. acc |= (val) >= (128 + 110)
  285. #define output_pixel(pos, acc) \
  286. if (target == AV_PIX_FMT_MONOBLACK) { \
  287. pos = acc; \
  288. } else { \
  289. pos = ~acc; \
  290. }
  291. static av_always_inline void
  292. yuv2mono_X_c_template(SwsContext *c, const int16_t *lumFilter,
  293. const int16_t **lumSrc, int lumFilterSize,
  294. const int16_t *chrFilter, const int16_t **chrUSrc,
  295. const int16_t **chrVSrc, int chrFilterSize,
  296. const int16_t **alpSrc, uint8_t *dest, int dstW,
  297. int y, enum AVPixelFormat target)
  298. {
  299. const uint8_t * const d128=dither_8x8_220[y&7];
  300. int i;
  301. unsigned acc = 0;
  302. int err = 0;
  303. for (i = 0; i < dstW; i += 2) {
  304. int j;
  305. int Y1 = 1 << 18;
  306. int Y2 = 1 << 18;
  307. for (j = 0; j < lumFilterSize; j++) {
  308. Y1 += lumSrc[j][i] * lumFilter[j];
  309. Y2 += lumSrc[j][i+1] * lumFilter[j];
  310. }
  311. Y1 >>= 19;
  312. Y2 >>= 19;
  313. if ((Y1 | Y2) & 0x100) {
  314. Y1 = av_clip_uint8(Y1);
  315. Y2 = av_clip_uint8(Y2);
  316. }
  317. if (c->flags & SWS_ERROR_DIFFUSION) {
  318. 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;
  319. c->dither_error[0][i] = err;
  320. acc = 2*acc + (Y1 >= 128);
  321. Y1 -= 220*(acc&1);
  322. 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);
  323. c->dither_error[0][i+1] = Y1;
  324. acc = 2*acc + (err >= 128);
  325. err -= 220*(acc&1);
  326. } else {
  327. accumulate_bit(acc, Y1 + d128[(i + 0) & 7]);
  328. accumulate_bit(acc, Y2 + d128[(i + 1) & 7]);
  329. }
  330. if ((i & 7) == 6) {
  331. output_pixel(*dest++, acc);
  332. }
  333. }
  334. c->dither_error[0][i] = err;
  335. if (i & 6) {
  336. output_pixel(*dest, acc);
  337. }
  338. }
  339. static av_always_inline void
  340. yuv2mono_2_c_template(SwsContext *c, const int16_t *buf[2],
  341. const int16_t *ubuf[2], const int16_t *vbuf[2],
  342. const int16_t *abuf[2], uint8_t *dest, int dstW,
  343. int yalpha, int uvalpha, int y,
  344. enum AVPixelFormat target)
  345. {
  346. const int16_t *buf0 = buf[0], *buf1 = buf[1];
  347. const uint8_t * const d128 = dither_8x8_220[y & 7];
  348. int yalpha1 = 4096 - yalpha;
  349. int i;
  350. if (c->flags & SWS_ERROR_DIFFUSION) {
  351. int err = 0;
  352. int acc = 0;
  353. for (i = 0; i < dstW; i +=2) {
  354. int Y;
  355. Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
  356. Y += (7*err + 1*c->dither_error[0][i] + 5*c->dither_error[0][i+1] + 3*c->dither_error[0][i+2] + 8 - 256)>>4;
  357. c->dither_error[0][i] = err;
  358. acc = 2*acc + (Y >= 128);
  359. Y -= 220*(acc&1);
  360. err = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
  361. err += (7*Y + 1*c->dither_error[0][i+1] + 5*c->dither_error[0][i+2] + 3*c->dither_error[0][i+3] + 8 - 256)>>4;
  362. c->dither_error[0][i+1] = Y;
  363. acc = 2*acc + (err >= 128);
  364. err -= 220*(acc&1);
  365. if ((i & 7) == 6)
  366. output_pixel(*dest++, acc);
  367. }
  368. c->dither_error[0][i] = err;
  369. } else {
  370. for (i = 0; i < dstW; i += 8) {
  371. int Y, acc = 0;
  372. Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
  373. accumulate_bit(acc, Y + d128[0]);
  374. Y = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
  375. accumulate_bit(acc, Y + d128[1]);
  376. Y = (buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19;
  377. accumulate_bit(acc, Y + d128[2]);
  378. Y = (buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19;
  379. accumulate_bit(acc, Y + d128[3]);
  380. Y = (buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19;
  381. accumulate_bit(acc, Y + d128[4]);
  382. Y = (buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19;
  383. accumulate_bit(acc, Y + d128[5]);
  384. Y = (buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19;
  385. accumulate_bit(acc, Y + d128[6]);
  386. Y = (buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19;
  387. accumulate_bit(acc, Y + d128[7]);
  388. output_pixel(*dest++, acc);
  389. }
  390. }
  391. }
  392. static av_always_inline void
  393. yuv2mono_1_c_template(SwsContext *c, const int16_t *buf0,
  394. const int16_t *ubuf[2], const int16_t *vbuf[2],
  395. const int16_t *abuf0, uint8_t *dest, int dstW,
  396. int uvalpha, int y, enum AVPixelFormat target)
  397. {
  398. const uint8_t * const d128 = dither_8x8_220[y & 7];
  399. int i;
  400. if (c->flags & SWS_ERROR_DIFFUSION) {
  401. int err = 0;
  402. int acc = 0;
  403. for (i = 0; i < dstW; i +=2) {
  404. int Y;
  405. Y = ((buf0[i + 0] + 64) >> 7);
  406. Y += (7*err + 1*c->dither_error[0][i] + 5*c->dither_error[0][i+1] + 3*c->dither_error[0][i+2] + 8 - 256)>>4;
  407. c->dither_error[0][i] = err;
  408. acc = 2*acc + (Y >= 128);
  409. Y -= 220*(acc&1);
  410. err = ((buf0[i + 1] + 64) >> 7);
  411. err += (7*Y + 1*c->dither_error[0][i+1] + 5*c->dither_error[0][i+2] + 3*c->dither_error[0][i+3] + 8 - 256)>>4;
  412. c->dither_error[0][i+1] = Y;
  413. acc = 2*acc + (err >= 128);
  414. err -= 220*(acc&1);
  415. if ((i & 7) == 6)
  416. output_pixel(*dest++, acc);
  417. }
  418. c->dither_error[0][i] = err;
  419. } else {
  420. for (i = 0; i < dstW; i += 8) {
  421. int acc = 0;
  422. accumulate_bit(acc, ((buf0[i + 0] + 64) >> 7) + d128[0]);
  423. accumulate_bit(acc, ((buf0[i + 1] + 64) >> 7) + d128[1]);
  424. accumulate_bit(acc, ((buf0[i + 2] + 64) >> 7) + d128[2]);
  425. accumulate_bit(acc, ((buf0[i + 3] + 64) >> 7) + d128[3]);
  426. accumulate_bit(acc, ((buf0[i + 4] + 64) >> 7) + d128[4]);
  427. accumulate_bit(acc, ((buf0[i + 5] + 64) >> 7) + d128[5]);
  428. accumulate_bit(acc, ((buf0[i + 6] + 64) >> 7) + d128[6]);
  429. accumulate_bit(acc, ((buf0[i + 7] + 64) >> 7) + d128[7]);
  430. output_pixel(*dest++, acc);
  431. }
  432. }
  433. }
  434. #undef output_pixel
  435. #undef accumulate_bit
  436. #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
  437. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  438. const int16_t **lumSrc, int lumFilterSize, \
  439. const int16_t *chrFilter, const int16_t **chrUSrc, \
  440. const int16_t **chrVSrc, int chrFilterSize, \
  441. const int16_t **alpSrc, uint8_t *dest, int dstW, \
  442. int y) \
  443. { \
  444. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  445. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  446. alpSrc, dest, dstW, y, fmt); \
  447. } \
  448. \
  449. static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
  450. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  451. const int16_t *abuf[2], uint8_t *dest, int dstW, \
  452. int yalpha, int uvalpha, int y) \
  453. { \
  454. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  455. dest, dstW, yalpha, uvalpha, y, fmt); \
  456. } \
  457. \
  458. static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
  459. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  460. const int16_t *abuf0, uint8_t *dest, int dstW, \
  461. int uvalpha, int y) \
  462. { \
  463. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
  464. abuf0, dest, dstW, uvalpha, \
  465. y, fmt); \
  466. }
  467. YUV2PACKEDWRAPPER(yuv2mono,, white, AV_PIX_FMT_MONOWHITE)
  468. YUV2PACKEDWRAPPER(yuv2mono,, black, AV_PIX_FMT_MONOBLACK)
  469. #define output_pixels(pos, Y1, U, Y2, V) \
  470. if (target == AV_PIX_FMT_YUYV422) { \
  471. dest[pos + 0] = Y1; \
  472. dest[pos + 1] = U; \
  473. dest[pos + 2] = Y2; \
  474. dest[pos + 3] = V; \
  475. } else { \
  476. dest[pos + 0] = U; \
  477. dest[pos + 1] = Y1; \
  478. dest[pos + 2] = V; \
  479. dest[pos + 3] = Y2; \
  480. }
  481. static av_always_inline void
  482. yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
  483. const int16_t **lumSrc, int lumFilterSize,
  484. const int16_t *chrFilter, const int16_t **chrUSrc,
  485. const int16_t **chrVSrc, int chrFilterSize,
  486. const int16_t **alpSrc, uint8_t *dest, int dstW,
  487. int y, enum AVPixelFormat target)
  488. {
  489. int i;
  490. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  491. int j;
  492. int Y1 = 1 << 18;
  493. int Y2 = 1 << 18;
  494. int U = 1 << 18;
  495. int V = 1 << 18;
  496. for (j = 0; j < lumFilterSize; j++) {
  497. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  498. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  499. }
  500. for (j = 0; j < chrFilterSize; j++) {
  501. U += chrUSrc[j][i] * chrFilter[j];
  502. V += chrVSrc[j][i] * chrFilter[j];
  503. }
  504. Y1 >>= 19;
  505. Y2 >>= 19;
  506. U >>= 19;
  507. V >>= 19;
  508. if ((Y1 | Y2 | U | V) & 0x100) {
  509. Y1 = av_clip_uint8(Y1);
  510. Y2 = av_clip_uint8(Y2);
  511. U = av_clip_uint8(U);
  512. V = av_clip_uint8(V);
  513. }
  514. output_pixels(4*i, Y1, U, Y2, V);
  515. }
  516. }
  517. static av_always_inline void
  518. yuv2422_2_c_template(SwsContext *c, const int16_t *buf[2],
  519. const int16_t *ubuf[2], const int16_t *vbuf[2],
  520. const int16_t *abuf[2], uint8_t *dest, int dstW,
  521. int yalpha, int uvalpha, int y,
  522. enum AVPixelFormat target)
  523. {
  524. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  525. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  526. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  527. int yalpha1 = 4096 - yalpha;
  528. int uvalpha1 = 4096 - uvalpha;
  529. int i;
  530. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  531. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
  532. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
  533. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
  534. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
  535. if ((Y1 | Y2 | U | V) & 0x100) {
  536. Y1 = av_clip_uint8(Y1);
  537. Y2 = av_clip_uint8(Y2);
  538. U = av_clip_uint8(U);
  539. V = av_clip_uint8(V);
  540. }
  541. output_pixels(i * 4, Y1, U, Y2, V);
  542. }
  543. }
  544. static av_always_inline void
  545. yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
  546. const int16_t *ubuf[2], const int16_t *vbuf[2],
  547. const int16_t *abuf0, uint8_t *dest, int dstW,
  548. int uvalpha, int y, enum AVPixelFormat target)
  549. {
  550. const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  551. int i;
  552. if (uvalpha < 2048) {
  553. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  554. int Y1 = (buf0[i * 2 ]+64) >> 7;
  555. int Y2 = (buf0[i * 2 + 1]+64) >> 7;
  556. int U = (ubuf0[i] +64) >> 7;
  557. int V = (vbuf0[i] +64) >> 7;
  558. if ((Y1 | Y2 | U | V) & 0x100) {
  559. Y1 = av_clip_uint8(Y1);
  560. Y2 = av_clip_uint8(Y2);
  561. U = av_clip_uint8(U);
  562. V = av_clip_uint8(V);
  563. }
  564. Y1 = av_clip_uint8(Y1);
  565. Y2 = av_clip_uint8(Y2);
  566. U = av_clip_uint8(U);
  567. V = av_clip_uint8(V);
  568. output_pixels(i * 4, Y1, U, Y2, V);
  569. }
  570. } else {
  571. const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  572. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  573. int Y1 = (buf0[i * 2 ] + 64) >> 7;
  574. int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
  575. int U = (ubuf0[i] + ubuf1[i]+128) >> 8;
  576. int V = (vbuf0[i] + vbuf1[i]+128) >> 8;
  577. if ((Y1 | Y2 | U | V) & 0x100) {
  578. Y1 = av_clip_uint8(Y1);
  579. Y2 = av_clip_uint8(Y2);
  580. U = av_clip_uint8(U);
  581. V = av_clip_uint8(V);
  582. }
  583. Y1 = av_clip_uint8(Y1);
  584. Y2 = av_clip_uint8(Y2);
  585. U = av_clip_uint8(U);
  586. V = av_clip_uint8(V);
  587. output_pixels(i * 4, Y1, U, Y2, V);
  588. }
  589. }
  590. }
  591. #undef output_pixels
  592. YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, AV_PIX_FMT_YUYV422)
  593. YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, AV_PIX_FMT_UYVY422)
  594. #define R_B ((target == AV_PIX_FMT_RGB48LE || target == AV_PIX_FMT_RGB48BE) ? R : B)
  595. #define B_R ((target == AV_PIX_FMT_RGB48LE || target == AV_PIX_FMT_RGB48BE) ? B : R)
  596. #define output_pixel(pos, val) \
  597. if (isBE(target)) { \
  598. AV_WB16(pos, val); \
  599. } else { \
  600. AV_WL16(pos, val); \
  601. }
  602. static av_always_inline void
  603. yuv2rgba64_X_c_template(SwsContext *c, const int16_t *lumFilter,
  604. const int32_t **lumSrc, int lumFilterSize,
  605. const int16_t *chrFilter, const int32_t **chrUSrc,
  606. const int32_t **chrVSrc, int chrFilterSize,
  607. const int32_t **alpSrc, uint16_t *dest, int dstW,
  608. int y, enum AVPixelFormat target, int hasAlpha)
  609. {
  610. int i;
  611. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  612. int j, A1 = 0, A2 = 0;
  613. int Y1 = -0x40000000;
  614. int Y2 = -0x40000000;
  615. int U = -128 << 23; // 19
  616. int V = -128 << 23;
  617. int R, G, B;
  618. for (j = 0; j < lumFilterSize; j++) {
  619. Y1 += lumSrc[j][i * 2] * (unsigned)lumFilter[j];
  620. Y2 += lumSrc[j][i * 2 + 1] * (unsigned)lumFilter[j];
  621. }
  622. for (j = 0; j < chrFilterSize; j++) {;
  623. U += chrUSrc[j][i] * (unsigned)chrFilter[j];
  624. V += chrVSrc[j][i] * (unsigned)chrFilter[j];
  625. }
  626. if (hasAlpha) {
  627. A1 = -0x40000000;
  628. A2 = -0x40000000;
  629. for (j = 0; j < lumFilterSize; j++) {
  630. A1 += alpSrc[j][i * 2] * (unsigned)lumFilter[j];
  631. A2 += alpSrc[j][i * 2 + 1] * (unsigned)lumFilter[j];
  632. }
  633. A1 >>= 1;
  634. A1 += 0x20002000;
  635. A2 >>= 1;
  636. A2 += 0x20002000;
  637. }
  638. // 8bit: 12+15=27; 16-bit: 12+19=31
  639. Y1 >>= 14; // 10
  640. Y1 += 0x10000;
  641. Y2 >>= 14;
  642. Y2 += 0x10000;
  643. U >>= 14;
  644. V >>= 14;
  645. // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
  646. Y1 -= c->yuv2rgb_y_offset;
  647. Y2 -= c->yuv2rgb_y_offset;
  648. Y1 *= c->yuv2rgb_y_coeff;
  649. Y2 *= c->yuv2rgb_y_coeff;
  650. Y1 += 1 << 13; // 21
  651. Y2 += 1 << 13;
  652. // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
  653. R = V * c->yuv2rgb_v2r_coeff;
  654. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  655. B = U * c->yuv2rgb_u2b_coeff;
  656. // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
  657. output_pixel(&dest[0], av_clip_uintp2(B_R + Y1, 30) >> 14);
  658. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  659. output_pixel(&dest[2], av_clip_uintp2(R_B + Y1, 30) >> 14);
  660. output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
  661. output_pixel(&dest[4], av_clip_uintp2(B_R + Y2, 30) >> 14);
  662. output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14);
  663. output_pixel(&dest[6], av_clip_uintp2(R_B + Y2, 30) >> 14);
  664. output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
  665. dest += 8;
  666. }
  667. }
  668. static av_always_inline void
  669. yuv2rgba64_2_c_template(SwsContext *c, const int32_t *buf[2],
  670. const int32_t *ubuf[2], const int32_t *vbuf[2],
  671. const int32_t *abuf[2], uint16_t *dest, int dstW,
  672. int yalpha, int uvalpha, int y,
  673. enum AVPixelFormat target, int hasAlpha)
  674. {
  675. const int32_t *buf0 = buf[0], *buf1 = buf[1],
  676. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  677. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
  678. *abuf0 = hasAlpha ? abuf[0] : NULL,
  679. *abuf1 = hasAlpha ? abuf[1] : NULL;
  680. int yalpha1 = 4096 - yalpha;
  681. int uvalpha1 = 4096 - uvalpha;
  682. int i;
  683. int A1 = 0, A2 = 0; // Init to avoid compiler warnings
  684. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  685. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 14;
  686. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 14;
  687. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha + (-128 << 23)) >> 14;
  688. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha + (-128 << 23)) >> 14;
  689. int R, G, B;
  690. Y1 -= c->yuv2rgb_y_offset;
  691. Y2 -= c->yuv2rgb_y_offset;
  692. Y1 *= c->yuv2rgb_y_coeff;
  693. Y2 *= c->yuv2rgb_y_coeff;
  694. Y1 += 1 << 13;
  695. Y2 += 1 << 13;
  696. R = V * c->yuv2rgb_v2r_coeff;
  697. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  698. B = U * c->yuv2rgb_u2b_coeff;
  699. if (hasAlpha) {
  700. A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 1;
  701. A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 1;
  702. A1 += 1 << 13;
  703. A2 += 1 << 13;
  704. }
  705. output_pixel(&dest[0], av_clip_uintp2(B_R + Y1, 30) >> 14);
  706. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  707. output_pixel(&dest[2], av_clip_uintp2(R_B + Y1, 30) >> 14);
  708. output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
  709. output_pixel(&dest[4], av_clip_uintp2(B_R + Y2, 30) >> 14);
  710. output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14);
  711. output_pixel(&dest[6], av_clip_uintp2(R_B + Y2, 30) >> 14);
  712. output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
  713. dest += 8;
  714. }
  715. }
  716. static av_always_inline void
  717. yuv2rgba64_1_c_template(SwsContext *c, const int32_t *buf0,
  718. const int32_t *ubuf[2], const int32_t *vbuf[2],
  719. const int32_t *abuf0, uint16_t *dest, int dstW,
  720. int uvalpha, int y, enum AVPixelFormat target, int hasAlpha)
  721. {
  722. const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  723. int i;
  724. if (uvalpha < 2048) {
  725. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  726. int Y1 = (buf0[i * 2] ) >> 2;
  727. int Y2 = (buf0[i * 2 + 1]) >> 2;
  728. int U = (ubuf0[i] + (-128 << 11)) >> 2;
  729. int V = (vbuf0[i] + (-128 << 11)) >> 2;
  730. int R, G, B;
  731. int A1, A2;
  732. Y1 -= c->yuv2rgb_y_offset;
  733. Y2 -= c->yuv2rgb_y_offset;
  734. Y1 *= c->yuv2rgb_y_coeff;
  735. Y2 *= c->yuv2rgb_y_coeff;
  736. Y1 += 1 << 13;
  737. Y2 += 1 << 13;
  738. if (hasAlpha) {
  739. A1 = abuf0[i * 2 ] << 11;
  740. A2 = abuf0[i * 2 + 1] << 11;
  741. A1 += 1 << 13;
  742. A2 += 1 << 13;
  743. }
  744. R = V * c->yuv2rgb_v2r_coeff;
  745. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  746. B = U * c->yuv2rgb_u2b_coeff;
  747. output_pixel(&dest[0], av_clip_uintp2(B_R + Y1, 30) >> 14);
  748. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  749. output_pixel(&dest[2], av_clip_uintp2(R_B + Y1, 30) >> 14);
  750. output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
  751. output_pixel(&dest[4], av_clip_uintp2(B_R + Y2, 30) >> 14);
  752. output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14);
  753. output_pixel(&dest[6], av_clip_uintp2(R_B + Y2, 30) >> 14);
  754. output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
  755. dest += 8;
  756. }
  757. } else {
  758. const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  759. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  760. int Y1 = (buf0[i * 2] ) >> 2;
  761. int Y2 = (buf0[i * 2 + 1]) >> 2;
  762. int U = (ubuf0[i] + ubuf1[i] + (-128 << 12)) >> 3;
  763. int V = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
  764. int R, G, B;
  765. int A1, A2;
  766. Y1 -= c->yuv2rgb_y_offset;
  767. Y2 -= c->yuv2rgb_y_offset;
  768. Y1 *= c->yuv2rgb_y_coeff;
  769. Y2 *= c->yuv2rgb_y_coeff;
  770. Y1 += 1 << 13;
  771. Y2 += 1 << 13;
  772. if (hasAlpha) {
  773. A1 = abuf0[i * 2 ] << 11;
  774. A2 = abuf0[i * 2 + 1] << 11;
  775. A1 += 1 << 13;
  776. A2 += 1 << 13;
  777. }
  778. R = V * c->yuv2rgb_v2r_coeff;
  779. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  780. B = U * c->yuv2rgb_u2b_coeff;
  781. output_pixel(&dest[0], av_clip_uintp2(B_R + Y1, 30) >> 14);
  782. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  783. output_pixel(&dest[2], av_clip_uintp2(R_B + Y1, 30) >> 14);
  784. output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
  785. output_pixel(&dest[4], av_clip_uintp2(B_R + Y2, 30) >> 14);
  786. output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14);
  787. output_pixel(&dest[6], av_clip_uintp2(R_B + Y2, 30) >> 14);
  788. output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
  789. dest += 8;
  790. }
  791. }
  792. }
  793. static av_always_inline void
  794. yuv2rgb48_X_c_template(SwsContext *c, const int16_t *lumFilter,
  795. const int32_t **lumSrc, int lumFilterSize,
  796. const int16_t *chrFilter, const int32_t **chrUSrc,
  797. const int32_t **chrVSrc, int chrFilterSize,
  798. const int32_t **alpSrc, uint16_t *dest, int dstW,
  799. int y, enum AVPixelFormat target, int hasAlpha)
  800. {
  801. int i;
  802. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  803. int j;
  804. int Y1 = -0x40000000;
  805. int Y2 = -0x40000000;
  806. int U = -128 << 23; // 19
  807. int V = -128 << 23;
  808. int R, G, B;
  809. for (j = 0; j < lumFilterSize; j++) {
  810. Y1 += lumSrc[j][i * 2] * (unsigned)lumFilter[j];
  811. Y2 += lumSrc[j][i * 2 + 1] * (unsigned)lumFilter[j];
  812. }
  813. for (j = 0; j < chrFilterSize; j++) {;
  814. U += chrUSrc[j][i] * (unsigned)chrFilter[j];
  815. V += chrVSrc[j][i] * (unsigned)chrFilter[j];
  816. }
  817. // 8bit: 12+15=27; 16-bit: 12+19=31
  818. Y1 >>= 14; // 10
  819. Y1 += 0x10000;
  820. Y2 >>= 14;
  821. Y2 += 0x10000;
  822. U >>= 14;
  823. V >>= 14;
  824. // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
  825. Y1 -= c->yuv2rgb_y_offset;
  826. Y2 -= c->yuv2rgb_y_offset;
  827. Y1 *= c->yuv2rgb_y_coeff;
  828. Y2 *= c->yuv2rgb_y_coeff;
  829. Y1 += 1 << 13; // 21
  830. Y2 += 1 << 13;
  831. // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
  832. R = V * c->yuv2rgb_v2r_coeff;
  833. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  834. B = U * c->yuv2rgb_u2b_coeff;
  835. // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
  836. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  837. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  838. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  839. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  840. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  841. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  842. dest += 6;
  843. }
  844. }
  845. static av_always_inline void
  846. yuv2rgb48_2_c_template(SwsContext *c, const int32_t *buf[2],
  847. const int32_t *ubuf[2], const int32_t *vbuf[2],
  848. const int32_t *abuf[2], uint16_t *dest, int dstW,
  849. int yalpha, int uvalpha, int y,
  850. enum AVPixelFormat target, int hasAlpha)
  851. {
  852. const int32_t *buf0 = buf[0], *buf1 = buf[1],
  853. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  854. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  855. int yalpha1 = 4096 - yalpha;
  856. int uvalpha1 = 4096 - uvalpha;
  857. int i;
  858. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  859. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 14;
  860. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 14;
  861. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha + (-128 << 23)) >> 14;
  862. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha + (-128 << 23)) >> 14;
  863. int R, G, B;
  864. Y1 -= c->yuv2rgb_y_offset;
  865. Y2 -= c->yuv2rgb_y_offset;
  866. Y1 *= c->yuv2rgb_y_coeff;
  867. Y2 *= c->yuv2rgb_y_coeff;
  868. Y1 += 1 << 13;
  869. Y2 += 1 << 13;
  870. R = V * c->yuv2rgb_v2r_coeff;
  871. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  872. B = U * c->yuv2rgb_u2b_coeff;
  873. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  874. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  875. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  876. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  877. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  878. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  879. dest += 6;
  880. }
  881. }
  882. static av_always_inline void
  883. yuv2rgb48_1_c_template(SwsContext *c, const int32_t *buf0,
  884. const int32_t *ubuf[2], const int32_t *vbuf[2],
  885. const int32_t *abuf0, uint16_t *dest, int dstW,
  886. int uvalpha, int y, enum AVPixelFormat target, int hasAlpha)
  887. {
  888. const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  889. int i;
  890. if (uvalpha < 2048) {
  891. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  892. int Y1 = (buf0[i * 2] ) >> 2;
  893. int Y2 = (buf0[i * 2 + 1]) >> 2;
  894. int U = (ubuf0[i] + (-128 << 11)) >> 2;
  895. int V = (vbuf0[i] + (-128 << 11)) >> 2;
  896. int R, G, B;
  897. Y1 -= c->yuv2rgb_y_offset;
  898. Y2 -= c->yuv2rgb_y_offset;
  899. Y1 *= c->yuv2rgb_y_coeff;
  900. Y2 *= c->yuv2rgb_y_coeff;
  901. Y1 += 1 << 13;
  902. Y2 += 1 << 13;
  903. R = V * c->yuv2rgb_v2r_coeff;
  904. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  905. B = U * c->yuv2rgb_u2b_coeff;
  906. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  907. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  908. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  909. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  910. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  911. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  912. dest += 6;
  913. }
  914. } else {
  915. const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  916. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  917. int Y1 = (buf0[i * 2] ) >> 2;
  918. int Y2 = (buf0[i * 2 + 1]) >> 2;
  919. int U = (ubuf0[i] + ubuf1[i] + (-128 << 12)) >> 3;
  920. int V = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
  921. int R, G, B;
  922. Y1 -= c->yuv2rgb_y_offset;
  923. Y2 -= c->yuv2rgb_y_offset;
  924. Y1 *= c->yuv2rgb_y_coeff;
  925. Y2 *= c->yuv2rgb_y_coeff;
  926. Y1 += 1 << 13;
  927. Y2 += 1 << 13;
  928. R = V * c->yuv2rgb_v2r_coeff;
  929. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  930. B = U * c->yuv2rgb_u2b_coeff;
  931. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  932. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  933. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  934. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  935. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  936. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  937. dest += 6;
  938. }
  939. }
  940. }
  941. #undef output_pixel
  942. #undef r_b
  943. #undef b_r
  944. #define YUV2PACKED16WRAPPER(name, base, ext, fmt, hasAlpha) \
  945. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  946. const int16_t **_lumSrc, int lumFilterSize, \
  947. const int16_t *chrFilter, const int16_t **_chrUSrc, \
  948. const int16_t **_chrVSrc, int chrFilterSize, \
  949. const int16_t **_alpSrc, uint8_t *_dest, int dstW, \
  950. int y) \
  951. { \
  952. const int32_t **lumSrc = (const int32_t **) _lumSrc, \
  953. **chrUSrc = (const int32_t **) _chrUSrc, \
  954. **chrVSrc = (const int32_t **) _chrVSrc, \
  955. **alpSrc = (const int32_t **) _alpSrc; \
  956. uint16_t *dest = (uint16_t *) _dest; \
  957. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  958. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  959. alpSrc, dest, dstW, y, fmt, hasAlpha); \
  960. } \
  961. \
  962. static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
  963. const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
  964. const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
  965. int yalpha, int uvalpha, int y) \
  966. { \
  967. const int32_t **buf = (const int32_t **) _buf, \
  968. **ubuf = (const int32_t **) _ubuf, \
  969. **vbuf = (const int32_t **) _vbuf, \
  970. **abuf = (const int32_t **) _abuf; \
  971. uint16_t *dest = (uint16_t *) _dest; \
  972. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  973. dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
  974. } \
  975. \
  976. static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
  977. const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
  978. const int16_t *_abuf0, uint8_t *_dest, int dstW, \
  979. int uvalpha, int y) \
  980. { \
  981. const int32_t *buf0 = (const int32_t *) _buf0, \
  982. **ubuf = (const int32_t **) _ubuf, \
  983. **vbuf = (const int32_t **) _vbuf, \
  984. *abuf0 = (const int32_t *) _abuf0; \
  985. uint16_t *dest = (uint16_t *) _dest; \
  986. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
  987. dstW, uvalpha, y, fmt, hasAlpha); \
  988. }
  989. YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48be, AV_PIX_FMT_RGB48BE, 0)
  990. YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48le, AV_PIX_FMT_RGB48LE, 0)
  991. YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48be, AV_PIX_FMT_BGR48BE, 0)
  992. YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48le, AV_PIX_FMT_BGR48LE, 0)
  993. YUV2PACKED16WRAPPER(yuv2, rgba64, rgba64be, AV_PIX_FMT_RGBA64BE, 1)
  994. YUV2PACKED16WRAPPER(yuv2, rgba64, rgba64le, AV_PIX_FMT_RGBA64LE, 1)
  995. YUV2PACKED16WRAPPER(yuv2, rgba64, rgbx64be, AV_PIX_FMT_RGBA64BE, 0)
  996. YUV2PACKED16WRAPPER(yuv2, rgba64, rgbx64le, AV_PIX_FMT_RGBA64LE, 0)
  997. /*
  998. * Write out 2 RGB pixels in the target pixel format. This function takes a
  999. * R/G/B LUT as generated by ff_yuv2rgb_c_init_tables(), which takes care of
  1000. * things like endianness conversion and shifting. The caller takes care of
  1001. * setting the correct offset in these tables from the chroma (U/V) values.
  1002. * This function then uses the luminance (Y1/Y2) values to write out the
  1003. * correct RGB values into the destination buffer.
  1004. */
  1005. static av_always_inline void
  1006. yuv2rgb_write(uint8_t *_dest, int i, int Y1, int Y2,
  1007. unsigned A1, unsigned A2,
  1008. const void *_r, const void *_g, const void *_b, int y,
  1009. enum AVPixelFormat target, int hasAlpha)
  1010. {
  1011. if (target == AV_PIX_FMT_ARGB || target == AV_PIX_FMT_RGBA ||
  1012. target == AV_PIX_FMT_ABGR || target == AV_PIX_FMT_BGRA) {
  1013. uint32_t *dest = (uint32_t *) _dest;
  1014. const uint32_t *r = (const uint32_t *) _r;
  1015. const uint32_t *g = (const uint32_t *) _g;
  1016. const uint32_t *b = (const uint32_t *) _b;
  1017. #if CONFIG_SMALL
  1018. int sh = hasAlpha ? ((target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24) : 0;
  1019. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (hasAlpha ? A1 << sh : 0);
  1020. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (hasAlpha ? A2 << sh : 0);
  1021. #else
  1022. if (hasAlpha) {
  1023. int sh = (target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24;
  1024. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
  1025. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
  1026. } else {
  1027. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
  1028. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
  1029. }
  1030. #endif
  1031. } else if (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) {
  1032. uint8_t *dest = (uint8_t *) _dest;
  1033. const uint8_t *r = (const uint8_t *) _r;
  1034. const uint8_t *g = (const uint8_t *) _g;
  1035. const uint8_t *b = (const uint8_t *) _b;
  1036. #define r_b ((target == AV_PIX_FMT_RGB24) ? r : b)
  1037. #define b_r ((target == AV_PIX_FMT_RGB24) ? b : r)
  1038. dest[i * 6 + 0] = r_b[Y1];
  1039. dest[i * 6 + 1] = g[Y1];
  1040. dest[i * 6 + 2] = b_r[Y1];
  1041. dest[i * 6 + 3] = r_b[Y2];
  1042. dest[i * 6 + 4] = g[Y2];
  1043. dest[i * 6 + 5] = b_r[Y2];
  1044. #undef r_b
  1045. #undef b_r
  1046. } else if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565 ||
  1047. target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555 ||
  1048. target == AV_PIX_FMT_RGB444 || target == AV_PIX_FMT_BGR444) {
  1049. uint16_t *dest = (uint16_t *) _dest;
  1050. const uint16_t *r = (const uint16_t *) _r;
  1051. const uint16_t *g = (const uint16_t *) _g;
  1052. const uint16_t *b = (const uint16_t *) _b;
  1053. int dr1, dg1, db1, dr2, dg2, db2;
  1054. if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565) {
  1055. dr1 = dither_2x2_8[ y & 1 ][0];
  1056. dg1 = dither_2x2_4[ y & 1 ][0];
  1057. db1 = dither_2x2_8[(y & 1) ^ 1][0];
  1058. dr2 = dither_2x2_8[ y & 1 ][1];
  1059. dg2 = dither_2x2_4[ y & 1 ][1];
  1060. db2 = dither_2x2_8[(y & 1) ^ 1][1];
  1061. } else if (target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555) {
  1062. dr1 = dither_2x2_8[ y & 1 ][0];
  1063. dg1 = dither_2x2_8[ y & 1 ][1];
  1064. db1 = dither_2x2_8[(y & 1) ^ 1][0];
  1065. dr2 = dither_2x2_8[ y & 1 ][1];
  1066. dg2 = dither_2x2_8[ y & 1 ][0];
  1067. db2 = dither_2x2_8[(y & 1) ^ 1][1];
  1068. } else {
  1069. dr1 = dither_4x4_16[ y & 3 ][0];
  1070. dg1 = dither_4x4_16[ y & 3 ][1];
  1071. db1 = dither_4x4_16[(y & 3) ^ 3][0];
  1072. dr2 = dither_4x4_16[ y & 3 ][1];
  1073. dg2 = dither_4x4_16[ y & 3 ][0];
  1074. db2 = dither_4x4_16[(y & 3) ^ 3][1];
  1075. }
  1076. dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
  1077. dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
  1078. } else /* 8/4-bit */ {
  1079. uint8_t *dest = (uint8_t *) _dest;
  1080. const uint8_t *r = (const uint8_t *) _r;
  1081. const uint8_t *g = (const uint8_t *) _g;
  1082. const uint8_t *b = (const uint8_t *) _b;
  1083. int dr1, dg1, db1, dr2, dg2, db2;
  1084. if (target == AV_PIX_FMT_RGB8 || target == AV_PIX_FMT_BGR8) {
  1085. const uint8_t * const d64 = dither_8x8_73[y & 7];
  1086. const uint8_t * const d32 = dither_8x8_32[y & 7];
  1087. dr1 = dg1 = d32[(i * 2 + 0) & 7];
  1088. db1 = d64[(i * 2 + 0) & 7];
  1089. dr2 = dg2 = d32[(i * 2 + 1) & 7];
  1090. db2 = d64[(i * 2 + 1) & 7];
  1091. } else {
  1092. const uint8_t * const d64 = dither_8x8_73 [y & 7];
  1093. const uint8_t * const d128 = dither_8x8_220[y & 7];
  1094. dr1 = db1 = d128[(i * 2 + 0) & 7];
  1095. dg1 = d64[(i * 2 + 0) & 7];
  1096. dr2 = db2 = d128[(i * 2 + 1) & 7];
  1097. dg2 = d64[(i * 2 + 1) & 7];
  1098. }
  1099. if (target == AV_PIX_FMT_RGB4 || target == AV_PIX_FMT_BGR4) {
  1100. dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
  1101. ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
  1102. } else {
  1103. dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
  1104. dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
  1105. }
  1106. }
  1107. }
  1108. static av_always_inline void
  1109. yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
  1110. const int16_t **lumSrc, int lumFilterSize,
  1111. const int16_t *chrFilter, const int16_t **chrUSrc,
  1112. const int16_t **chrVSrc, int chrFilterSize,
  1113. const int16_t **alpSrc, uint8_t *dest, int dstW,
  1114. int y, enum AVPixelFormat target, int hasAlpha)
  1115. {
  1116. int i;
  1117. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1118. int j, A1, A2;
  1119. int Y1 = 1 << 18;
  1120. int Y2 = 1 << 18;
  1121. int U = 1 << 18;
  1122. int V = 1 << 18;
  1123. const void *r, *g, *b;
  1124. for (j = 0; j < lumFilterSize; j++) {
  1125. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  1126. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  1127. }
  1128. for (j = 0; j < chrFilterSize; j++) {
  1129. U += chrUSrc[j][i] * chrFilter[j];
  1130. V += chrVSrc[j][i] * chrFilter[j];
  1131. }
  1132. Y1 >>= 19;
  1133. Y2 >>= 19;
  1134. U >>= 19;
  1135. V >>= 19;
  1136. if (hasAlpha) {
  1137. A1 = 1 << 18;
  1138. A2 = 1 << 18;
  1139. for (j = 0; j < lumFilterSize; j++) {
  1140. A1 += alpSrc[j][i * 2 ] * lumFilter[j];
  1141. A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
  1142. }
  1143. A1 >>= 19;
  1144. A2 >>= 19;
  1145. if ((A1 | A2) & 0x100) {
  1146. A1 = av_clip_uint8(A1);
  1147. A2 = av_clip_uint8(A2);
  1148. }
  1149. }
  1150. r = c->table_rV[V + YUVRGB_TABLE_HEADROOM];
  1151. g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]);
  1152. b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  1153. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1154. r, g, b, y, target, hasAlpha);
  1155. }
  1156. }
  1157. static av_always_inline void
  1158. yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
  1159. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1160. const int16_t *abuf[2], uint8_t *dest, int dstW,
  1161. int yalpha, int uvalpha, int y,
  1162. enum AVPixelFormat target, int hasAlpha)
  1163. {
  1164. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  1165. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  1166. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
  1167. *abuf0 = hasAlpha ? abuf[0] : NULL,
  1168. *abuf1 = hasAlpha ? abuf[1] : NULL;
  1169. int yalpha1 = 4096 - yalpha;
  1170. int uvalpha1 = 4096 - uvalpha;
  1171. int i;
  1172. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1173. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
  1174. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
  1175. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
  1176. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
  1177. int A1, A2;
  1178. const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
  1179. *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
  1180. *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  1181. if (hasAlpha) {
  1182. A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 19;
  1183. A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
  1184. A1 = av_clip_uint8(A1);
  1185. A2 = av_clip_uint8(A2);
  1186. }
  1187. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1188. r, g, b, y, target, hasAlpha);
  1189. }
  1190. }
  1191. static av_always_inline void
  1192. yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
  1193. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1194. const int16_t *abuf0, uint8_t *dest, int dstW,
  1195. int uvalpha, int y, enum AVPixelFormat target,
  1196. int hasAlpha)
  1197. {
  1198. const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  1199. int i;
  1200. if (uvalpha < 2048) {
  1201. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1202. int Y1 = (buf0[i * 2 ] + 64) >> 7;
  1203. int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
  1204. int U = (ubuf0[i] + 64) >> 7;
  1205. int V = (vbuf0[i] + 64) >> 7;
  1206. int A1, A2;
  1207. const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
  1208. *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
  1209. *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  1210. if (hasAlpha) {
  1211. A1 = abuf0[i * 2 ] * 255 + 16384 >> 15;
  1212. A2 = abuf0[i * 2 + 1] * 255 + 16384 >> 15;
  1213. A1 = av_clip_uint8(A1);
  1214. A2 = av_clip_uint8(A2);
  1215. }
  1216. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1217. r, g, b, y, target, hasAlpha);
  1218. }
  1219. } else {
  1220. const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  1221. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1222. int Y1 = (buf0[i * 2 ] + 64) >> 7;
  1223. int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
  1224. int U = (ubuf0[i] + ubuf1[i] + 128) >> 8;
  1225. int V = (vbuf0[i] + vbuf1[i] + 128) >> 8;
  1226. int A1, A2;
  1227. const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
  1228. *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
  1229. *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  1230. if (hasAlpha) {
  1231. A1 = (abuf0[i * 2 ] + 64) >> 7;
  1232. A2 = (abuf0[i * 2 + 1] + 64) >> 7;
  1233. A1 = av_clip_uint8(A1);
  1234. A2 = av_clip_uint8(A2);
  1235. }
  1236. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1237. r, g, b, y, target, hasAlpha);
  1238. }
  1239. }
  1240. }
  1241. #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
  1242. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  1243. const int16_t **lumSrc, int lumFilterSize, \
  1244. const int16_t *chrFilter, const int16_t **chrUSrc, \
  1245. const int16_t **chrVSrc, int chrFilterSize, \
  1246. const int16_t **alpSrc, uint8_t *dest, int dstW, \
  1247. int y) \
  1248. { \
  1249. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  1250. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  1251. alpSrc, dest, dstW, y, fmt, hasAlpha); \
  1252. }
  1253. #define YUV2RGBWRAPPERX2(name, base, ext, fmt, hasAlpha) \
  1254. YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
  1255. static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
  1256. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  1257. const int16_t *abuf[2], uint8_t *dest, int dstW, \
  1258. int yalpha, int uvalpha, int y) \
  1259. { \
  1260. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  1261. dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
  1262. }
  1263. #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
  1264. YUV2RGBWRAPPERX2(name, base, ext, fmt, hasAlpha) \
  1265. static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
  1266. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  1267. const int16_t *abuf0, uint8_t *dest, int dstW, \
  1268. int uvalpha, int y) \
  1269. { \
  1270. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
  1271. dstW, uvalpha, y, fmt, hasAlpha); \
  1272. }
  1273. #if CONFIG_SMALL
  1274. YUV2RGBWRAPPER(yuv2rgb,, 32_1, AV_PIX_FMT_RGB32_1, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1275. YUV2RGBWRAPPER(yuv2rgb,, 32, AV_PIX_FMT_RGB32, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1276. #else
  1277. #if CONFIG_SWSCALE_ALPHA
  1278. YUV2RGBWRAPPER(yuv2rgb,, a32_1, AV_PIX_FMT_RGB32_1, 1)
  1279. YUV2RGBWRAPPER(yuv2rgb,, a32, AV_PIX_FMT_RGB32, 1)
  1280. #endif
  1281. YUV2RGBWRAPPER(yuv2rgb,, x32_1, AV_PIX_FMT_RGB32_1, 0)
  1282. YUV2RGBWRAPPER(yuv2rgb,, x32, AV_PIX_FMT_RGB32, 0)
  1283. #endif
  1284. YUV2RGBWRAPPER(yuv2, rgb, rgb24, AV_PIX_FMT_RGB24, 0)
  1285. YUV2RGBWRAPPER(yuv2, rgb, bgr24, AV_PIX_FMT_BGR24, 0)
  1286. YUV2RGBWRAPPER(yuv2rgb,, 16, AV_PIX_FMT_RGB565, 0)
  1287. YUV2RGBWRAPPER(yuv2rgb,, 15, AV_PIX_FMT_RGB555, 0)
  1288. YUV2RGBWRAPPER(yuv2rgb,, 12, AV_PIX_FMT_RGB444, 0)
  1289. YUV2RGBWRAPPER(yuv2rgb,, 8, AV_PIX_FMT_RGB8, 0)
  1290. YUV2RGBWRAPPER(yuv2rgb,, 4, AV_PIX_FMT_RGB4, 0)
  1291. YUV2RGBWRAPPER(yuv2rgb,, 4b, AV_PIX_FMT_RGB4_BYTE, 0)
  1292. static av_always_inline void yuv2rgb_write_full(SwsContext *c,
  1293. uint8_t *dest, int i, int Y, int A, int U, int V,
  1294. int y, enum AVPixelFormat target, int hasAlpha, int err[4])
  1295. {
  1296. int R, G, B;
  1297. int isrgb8 = target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8;
  1298. Y -= c->yuv2rgb_y_offset;
  1299. Y *= c->yuv2rgb_y_coeff;
  1300. Y += 1 << 21;
  1301. R = Y + V*c->yuv2rgb_v2r_coeff;
  1302. G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
  1303. B = Y + U*c->yuv2rgb_u2b_coeff;
  1304. if ((R | G | B) & 0xC0000000) {
  1305. R = av_clip_uintp2(R, 30);
  1306. G = av_clip_uintp2(G, 30);
  1307. B = av_clip_uintp2(B, 30);
  1308. }
  1309. switch(target) {
  1310. case AV_PIX_FMT_ARGB:
  1311. dest[0] = hasAlpha ? A : 255;
  1312. dest[1] = R >> 22;
  1313. dest[2] = G >> 22;
  1314. dest[3] = B >> 22;
  1315. break;
  1316. case AV_PIX_FMT_RGB24:
  1317. dest[0] = R >> 22;
  1318. dest[1] = G >> 22;
  1319. dest[2] = B >> 22;
  1320. break;
  1321. case AV_PIX_FMT_RGBA:
  1322. dest[0] = R >> 22;
  1323. dest[1] = G >> 22;
  1324. dest[2] = B >> 22;
  1325. dest[3] = hasAlpha ? A : 255;
  1326. break;
  1327. case AV_PIX_FMT_ABGR:
  1328. dest[0] = hasAlpha ? A : 255;
  1329. dest[1] = B >> 22;
  1330. dest[2] = G >> 22;
  1331. dest[3] = R >> 22;
  1332. break;
  1333. case AV_PIX_FMT_BGR24:
  1334. dest[0] = B >> 22;
  1335. dest[1] = G >> 22;
  1336. dest[2] = R >> 22;
  1337. break;
  1338. case AV_PIX_FMT_BGRA:
  1339. dest[0] = B >> 22;
  1340. dest[1] = G >> 22;
  1341. dest[2] = R >> 22;
  1342. dest[3] = hasAlpha ? A : 255;
  1343. break;
  1344. case AV_PIX_FMT_BGR4_BYTE:
  1345. case AV_PIX_FMT_RGB4_BYTE:
  1346. case AV_PIX_FMT_BGR8:
  1347. case AV_PIX_FMT_RGB8:
  1348. {
  1349. int r,g,b;
  1350. R >>= 22;
  1351. G >>= 22;
  1352. B >>= 22;
  1353. 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;
  1354. 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;
  1355. 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;
  1356. c->dither_error[0][i] = err[0];
  1357. c->dither_error[1][i] = err[1];
  1358. c->dither_error[2][i] = err[2];
  1359. r = R >> (isrgb8 ? 5 : 7);
  1360. g = G >> (isrgb8 ? 5 : 6);
  1361. b = B >> (isrgb8 ? 6 : 7);
  1362. r = av_clip(r, 0, isrgb8 ? 7 : 1);
  1363. g = av_clip(g, 0, isrgb8 ? 7 : 3);
  1364. b = av_clip(b, 0, isrgb8 ? 3 : 1);
  1365. err[0] = R - r*(isrgb8 ? 36 : 255);
  1366. err[1] = G - g*(isrgb8 ? 36 : 85);
  1367. err[2] = B - b*(isrgb8 ? 85 : 255);
  1368. if(target == AV_PIX_FMT_BGR4_BYTE) {
  1369. dest[0] = r + 2*g + 8*b;
  1370. } else if(target == AV_PIX_FMT_RGB4_BYTE) {
  1371. dest[0] = b + 2*g + 8*r;
  1372. } else if(target == AV_PIX_FMT_BGR8) {
  1373. dest[0] = r + 8*g + 64*b;
  1374. } else if(target == AV_PIX_FMT_RGB8) {
  1375. dest[0] = b + 4*g + 32*r;
  1376. } else
  1377. av_assert2(0);
  1378. break;}
  1379. }
  1380. }
  1381. static av_always_inline void
  1382. yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
  1383. const int16_t **lumSrc, int lumFilterSize,
  1384. const int16_t *chrFilter, const int16_t **chrUSrc,
  1385. const int16_t **chrVSrc, int chrFilterSize,
  1386. const int16_t **alpSrc, uint8_t *dest,
  1387. int dstW, int y, enum AVPixelFormat target, int hasAlpha)
  1388. {
  1389. int i;
  1390. int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
  1391. int err[4] = {0};
  1392. int A = 0; //init to silence warning
  1393. if( target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
  1394. || target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8)
  1395. step = 1;
  1396. for (i = 0; i < dstW; i++) {
  1397. int j;
  1398. int Y = 1<<9;
  1399. int U = (1<<9)-(128 << 19);
  1400. int V = (1<<9)-(128 << 19);
  1401. for (j = 0; j < lumFilterSize; j++) {
  1402. Y += lumSrc[j][i] * lumFilter[j];
  1403. }
  1404. for (j = 0; j < chrFilterSize; j++) {
  1405. U += chrUSrc[j][i] * chrFilter[j];
  1406. V += chrVSrc[j][i] * chrFilter[j];
  1407. }
  1408. Y >>= 10;
  1409. U >>= 10;
  1410. V >>= 10;
  1411. if (hasAlpha) {
  1412. A = 1 << 18;
  1413. for (j = 0; j < lumFilterSize; j++) {
  1414. A += alpSrc[j][i] * lumFilter[j];
  1415. }
  1416. A >>= 19;
  1417. if (A & 0x100)
  1418. A = av_clip_uint8(A);
  1419. }
  1420. yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
  1421. dest += step;
  1422. }
  1423. c->dither_error[0][i] = err[0];
  1424. c->dither_error[1][i] = err[1];
  1425. c->dither_error[2][i] = err[2];
  1426. }
  1427. static av_always_inline void
  1428. yuv2rgb_full_2_c_template(SwsContext *c, const int16_t *buf[2],
  1429. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1430. const int16_t *abuf[2], uint8_t *dest, int dstW,
  1431. int yalpha, int uvalpha, int y,
  1432. enum AVPixelFormat target, int hasAlpha)
  1433. {
  1434. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  1435. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  1436. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
  1437. *abuf0 = hasAlpha ? abuf[0] : NULL,
  1438. *abuf1 = hasAlpha ? abuf[1] : NULL;
  1439. int yalpha1 = 4096 - yalpha;
  1440. int uvalpha1 = 4096 - uvalpha;
  1441. int i;
  1442. int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
  1443. int err[4] = {0};
  1444. int A = 0; // init to silcene warning
  1445. if( target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
  1446. || target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8)
  1447. step = 1;
  1448. for (i = 0; i < dstW; i++) {
  1449. int Y = ( buf0[i] * yalpha1 + buf1[i] * yalpha ) >> 10; //FIXME rounding
  1450. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha-(128 << 19)) >> 10;
  1451. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha-(128 << 19)) >> 10;
  1452. if (hasAlpha) {
  1453. A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha + (1<<18)) >> 19;
  1454. if (A & 0x100)
  1455. A = av_clip_uint8(A);
  1456. }
  1457. yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
  1458. dest += step;
  1459. }
  1460. c->dither_error[0][i] = err[0];
  1461. c->dither_error[1][i] = err[1];
  1462. c->dither_error[2][i] = err[2];
  1463. }
  1464. static av_always_inline void
  1465. yuv2rgb_full_1_c_template(SwsContext *c, const int16_t *buf0,
  1466. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1467. const int16_t *abuf0, uint8_t *dest, int dstW,
  1468. int uvalpha, int y, enum AVPixelFormat target,
  1469. int hasAlpha)
  1470. {
  1471. const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  1472. int i;
  1473. int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
  1474. int err[4] = {0};
  1475. if( target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
  1476. || target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8)
  1477. step = 1;
  1478. if (uvalpha < 2048) {
  1479. int A = 0; //init to silence warning
  1480. for (i = 0; i < dstW; i++) {
  1481. int Y = buf0[i] << 2;
  1482. int U = (ubuf0[i] - (128<<7)) << 2;
  1483. int V = (vbuf0[i] - (128<<7)) << 2;
  1484. if (hasAlpha) {
  1485. A = (abuf0[i] + 64) >> 7;
  1486. if (A & 0x100)
  1487. A = av_clip_uint8(A);
  1488. }
  1489. yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
  1490. dest += step;
  1491. }
  1492. } else {
  1493. const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  1494. int A = 0; //init to silence warning
  1495. for (i = 0; i < dstW; i++) {
  1496. int Y = buf0[i] << 2;
  1497. int U = (ubuf0[i] + ubuf1[i] - (128<<8)) << 1;
  1498. int V = (vbuf0[i] + vbuf1[i] - (128<<8)) << 1;
  1499. if (hasAlpha) {
  1500. A = (abuf0[i] + 64) >> 7;
  1501. if (A & 0x100)
  1502. A = av_clip_uint8(A);
  1503. }
  1504. yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
  1505. dest += step;
  1506. }
  1507. }
  1508. c->dither_error[0][i] = err[0];
  1509. c->dither_error[1][i] = err[1];
  1510. c->dither_error[2][i] = err[2];
  1511. }
  1512. #if CONFIG_SMALL
  1513. YUV2RGBWRAPPER(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1514. YUV2RGBWRAPPER(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1515. YUV2RGBWRAPPER(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1516. YUV2RGBWRAPPER(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1517. #else
  1518. #if CONFIG_SWSCALE_ALPHA
  1519. YUV2RGBWRAPPER(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA, 1)
  1520. YUV2RGBWRAPPER(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR, 1)
  1521. YUV2RGBWRAPPER(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA, 1)
  1522. YUV2RGBWRAPPER(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB, 1)
  1523. #endif
  1524. YUV2RGBWRAPPER(yuv2, rgb_full, bgrx32_full, AV_PIX_FMT_BGRA, 0)
  1525. YUV2RGBWRAPPER(yuv2, rgb_full, xbgr32_full, AV_PIX_FMT_ABGR, 0)
  1526. YUV2RGBWRAPPER(yuv2, rgb_full, rgbx32_full, AV_PIX_FMT_RGBA, 0)
  1527. YUV2RGBWRAPPER(yuv2, rgb_full, xrgb32_full, AV_PIX_FMT_ARGB, 0)
  1528. #endif
  1529. YUV2RGBWRAPPER(yuv2, rgb_full, bgr24_full, AV_PIX_FMT_BGR24, 0)
  1530. YUV2RGBWRAPPER(yuv2, rgb_full, rgb24_full, AV_PIX_FMT_RGB24, 0)
  1531. YUV2RGBWRAPPER(yuv2, rgb_full, bgr4_byte_full, AV_PIX_FMT_BGR4_BYTE, 0)
  1532. YUV2RGBWRAPPER(yuv2, rgb_full, rgb4_byte_full, AV_PIX_FMT_RGB4_BYTE, 0)
  1533. YUV2RGBWRAPPER(yuv2, rgb_full, bgr8_full, AV_PIX_FMT_BGR8, 0)
  1534. YUV2RGBWRAPPER(yuv2, rgb_full, rgb8_full, AV_PIX_FMT_RGB8, 0)
  1535. static void
  1536. yuv2gbrp_full_X_c(SwsContext *c, const int16_t *lumFilter,
  1537. const int16_t **lumSrc, int lumFilterSize,
  1538. const int16_t *chrFilter, const int16_t **chrUSrc,
  1539. const int16_t **chrVSrc, int chrFilterSize,
  1540. const int16_t **alpSrc, uint8_t **dest,
  1541. int dstW, int y)
  1542. {
  1543. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
  1544. int i;
  1545. int hasAlpha = (desc->flags & PIX_FMT_ALPHA) && alpSrc;
  1546. uint16_t **dest16 = (uint16_t**)dest;
  1547. int SH = 22 + 7 - desc->comp[0].depth_minus1;
  1548. int A = 0; // init to silence warning
  1549. for (i = 0; i < dstW; i++) {
  1550. int j;
  1551. int Y = 1 << 9;
  1552. int U = (1 << 9) - (128 << 19);
  1553. int V = (1 << 9) - (128 << 19);
  1554. int R, G, B;
  1555. for (j = 0; j < lumFilterSize; j++)
  1556. Y += lumSrc[j][i] * lumFilter[j];
  1557. for (j = 0; j < chrFilterSize; j++) {
  1558. U += chrUSrc[j][i] * chrFilter[j];
  1559. V += chrVSrc[j][i] * chrFilter[j];
  1560. }
  1561. Y >>= 10;
  1562. U >>= 10;
  1563. V >>= 10;
  1564. if (hasAlpha) {
  1565. A = 1 << 18;
  1566. for (j = 0; j < lumFilterSize; j++)
  1567. A += alpSrc[j][i] * lumFilter[j];
  1568. A >>= 19;
  1569. if (A & 0x100)
  1570. A = av_clip_uint8(A);
  1571. }
  1572. Y -= c->yuv2rgb_y_offset;
  1573. Y *= c->yuv2rgb_y_coeff;
  1574. Y += 1 << 21;
  1575. R = Y + V * c->yuv2rgb_v2r_coeff;
  1576. G = Y + V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  1577. B = Y + U * c->yuv2rgb_u2b_coeff;
  1578. if ((R | G | B) & 0xC0000000) {
  1579. R = av_clip_uintp2(R, 30);
  1580. G = av_clip_uintp2(G, 30);
  1581. B = av_clip_uintp2(B, 30);
  1582. }
  1583. if (SH != 22) {
  1584. dest16[0][i] = G >> SH;
  1585. dest16[1][i] = B >> SH;
  1586. dest16[2][i] = R >> SH;
  1587. if (hasAlpha)
  1588. dest16[3][i] = A;
  1589. } else {
  1590. dest[0][i] = G >> 22;
  1591. dest[1][i] = B >> 22;
  1592. dest[2][i] = R >> 22;
  1593. if (hasAlpha)
  1594. dest[3][i] = A;
  1595. }
  1596. }
  1597. if (SH != 22 && (!isBE(c->dstFormat)) != (!HAVE_BIGENDIAN)) {
  1598. for (i = 0; i < dstW; i++) {
  1599. dest16[0][i] = av_bswap16(dest16[0][i]);
  1600. dest16[1][i] = av_bswap16(dest16[1][i]);
  1601. dest16[2][i] = av_bswap16(dest16[2][i]);
  1602. if (hasAlpha)
  1603. dest16[3][i] = av_bswap16(dest16[3][i]);
  1604. }
  1605. }
  1606. }
  1607. av_cold void ff_sws_init_output_funcs(SwsContext *c,
  1608. yuv2planar1_fn *yuv2plane1,
  1609. yuv2planarX_fn *yuv2planeX,
  1610. yuv2interleavedX_fn *yuv2nv12cX,
  1611. yuv2packed1_fn *yuv2packed1,
  1612. yuv2packed2_fn *yuv2packed2,
  1613. yuv2packedX_fn *yuv2packedX,
  1614. yuv2anyX_fn *yuv2anyX)
  1615. {
  1616. enum AVPixelFormat dstFormat = c->dstFormat;
  1617. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
  1618. if (is16BPS(dstFormat)) {
  1619. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c : yuv2planeX_16LE_c;
  1620. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c : yuv2plane1_16LE_c;
  1621. } else if (is9_OR_10BPS(dstFormat)) {
  1622. if (desc->comp[0].depth_minus1 == 8) {
  1623. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c : yuv2planeX_9LE_c;
  1624. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c : yuv2plane1_9LE_c;
  1625. } else if (desc->comp[0].depth_minus1 == 9) {
  1626. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c : yuv2planeX_10LE_c;
  1627. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c : yuv2plane1_10LE_c;
  1628. } else if (desc->comp[0].depth_minus1 == 11) {
  1629. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_12BE_c : yuv2planeX_12LE_c;
  1630. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_12BE_c : yuv2plane1_12LE_c;
  1631. } else if (desc->comp[0].depth_minus1 == 13) {
  1632. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_14BE_c : yuv2planeX_14LE_c;
  1633. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_14BE_c : yuv2plane1_14LE_c;
  1634. } else
  1635. av_assert0(0);
  1636. } else {
  1637. *yuv2plane1 = yuv2plane1_8_c;
  1638. *yuv2planeX = yuv2planeX_8_c;
  1639. if (dstFormat == AV_PIX_FMT_NV12 || dstFormat == AV_PIX_FMT_NV21)
  1640. *yuv2nv12cX = yuv2nv12cX_c;
  1641. }
  1642. if(c->flags & SWS_FULL_CHR_H_INT) {
  1643. switch (dstFormat) {
  1644. case AV_PIX_FMT_RGBA:
  1645. #if CONFIG_SMALL
  1646. *yuv2packedX = yuv2rgba32_full_X_c;
  1647. *yuv2packed2 = yuv2rgba32_full_2_c;
  1648. *yuv2packed1 = yuv2rgba32_full_1_c;
  1649. #else
  1650. #if CONFIG_SWSCALE_ALPHA
  1651. if (c->alpPixBuf) {
  1652. *yuv2packedX = yuv2rgba32_full_X_c;
  1653. *yuv2packed2 = yuv2rgba32_full_2_c;
  1654. *yuv2packed1 = yuv2rgba32_full_1_c;
  1655. } else
  1656. #endif /* CONFIG_SWSCALE_ALPHA */
  1657. {
  1658. *yuv2packedX = yuv2rgbx32_full_X_c;
  1659. *yuv2packed2 = yuv2rgbx32_full_2_c;
  1660. *yuv2packed1 = yuv2rgbx32_full_1_c;
  1661. }
  1662. #endif /* !CONFIG_SMALL */
  1663. break;
  1664. case AV_PIX_FMT_ARGB:
  1665. #if CONFIG_SMALL
  1666. *yuv2packedX = yuv2argb32_full_X_c;
  1667. *yuv2packed2 = yuv2argb32_full_2_c;
  1668. *yuv2packed1 = yuv2argb32_full_1_c;
  1669. #else
  1670. #if CONFIG_SWSCALE_ALPHA
  1671. if (c->alpPixBuf) {
  1672. *yuv2packedX = yuv2argb32_full_X_c;
  1673. *yuv2packed2 = yuv2argb32_full_2_c;
  1674. *yuv2packed1 = yuv2argb32_full_1_c;
  1675. } else
  1676. #endif /* CONFIG_SWSCALE_ALPHA */
  1677. {
  1678. *yuv2packedX = yuv2xrgb32_full_X_c;
  1679. *yuv2packed2 = yuv2xrgb32_full_2_c;
  1680. *yuv2packed1 = yuv2xrgb32_full_1_c;
  1681. }
  1682. #endif /* !CONFIG_SMALL */
  1683. break;
  1684. case AV_PIX_FMT_BGRA:
  1685. #if CONFIG_SMALL
  1686. *yuv2packedX = yuv2bgra32_full_X_c;
  1687. *yuv2packed2 = yuv2bgra32_full_2_c;
  1688. *yuv2packed1 = yuv2bgra32_full_1_c;
  1689. #else
  1690. #if CONFIG_SWSCALE_ALPHA
  1691. if (c->alpPixBuf) {
  1692. *yuv2packedX = yuv2bgra32_full_X_c;
  1693. *yuv2packed2 = yuv2bgra32_full_2_c;
  1694. *yuv2packed1 = yuv2bgra32_full_1_c;
  1695. } else
  1696. #endif /* CONFIG_SWSCALE_ALPHA */
  1697. {
  1698. *yuv2packedX = yuv2bgrx32_full_X_c;
  1699. *yuv2packed2 = yuv2bgrx32_full_2_c;
  1700. *yuv2packed1 = yuv2bgrx32_full_1_c;
  1701. }
  1702. #endif /* !CONFIG_SMALL */
  1703. break;
  1704. case AV_PIX_FMT_ABGR:
  1705. #if CONFIG_SMALL
  1706. *yuv2packedX = yuv2abgr32_full_X_c;
  1707. *yuv2packed2 = yuv2abgr32_full_2_c;
  1708. *yuv2packed1 = yuv2abgr32_full_1_c;
  1709. #else
  1710. #if CONFIG_SWSCALE_ALPHA
  1711. if (c->alpPixBuf) {
  1712. *yuv2packedX = yuv2abgr32_full_X_c;
  1713. *yuv2packed2 = yuv2abgr32_full_2_c;
  1714. *yuv2packed1 = yuv2abgr32_full_1_c;
  1715. } else
  1716. #endif /* CONFIG_SWSCALE_ALPHA */
  1717. {
  1718. *yuv2packedX = yuv2xbgr32_full_X_c;
  1719. *yuv2packed2 = yuv2xbgr32_full_2_c;
  1720. *yuv2packed1 = yuv2xbgr32_full_1_c;
  1721. }
  1722. #endif /* !CONFIG_SMALL */
  1723. break;
  1724. case AV_PIX_FMT_RGB24:
  1725. *yuv2packedX = yuv2rgb24_full_X_c;
  1726. *yuv2packed2 = yuv2rgb24_full_2_c;
  1727. *yuv2packed1 = yuv2rgb24_full_1_c;
  1728. break;
  1729. case AV_PIX_FMT_BGR24:
  1730. *yuv2packedX = yuv2bgr24_full_X_c;
  1731. *yuv2packed2 = yuv2bgr24_full_2_c;
  1732. *yuv2packed1 = yuv2bgr24_full_1_c;
  1733. break;
  1734. case AV_PIX_FMT_BGR4_BYTE:
  1735. *yuv2packedX = yuv2bgr4_byte_full_X_c;
  1736. *yuv2packed2 = yuv2bgr4_byte_full_2_c;
  1737. *yuv2packed1 = yuv2bgr4_byte_full_1_c;
  1738. break;
  1739. case AV_PIX_FMT_RGB4_BYTE:
  1740. *yuv2packedX = yuv2rgb4_byte_full_X_c;
  1741. *yuv2packed2 = yuv2rgb4_byte_full_2_c;
  1742. *yuv2packed1 = yuv2rgb4_byte_full_1_c;
  1743. break;
  1744. case AV_PIX_FMT_BGR8:
  1745. *yuv2packedX = yuv2bgr8_full_X_c;
  1746. *yuv2packed2 = yuv2bgr8_full_2_c;
  1747. *yuv2packed1 = yuv2bgr8_full_1_c;
  1748. break;
  1749. case AV_PIX_FMT_RGB8:
  1750. *yuv2packedX = yuv2rgb8_full_X_c;
  1751. *yuv2packed2 = yuv2rgb8_full_2_c;
  1752. *yuv2packed1 = yuv2rgb8_full_1_c;
  1753. break;
  1754. case AV_PIX_FMT_GBRP:
  1755. case AV_PIX_FMT_GBRP9BE:
  1756. case AV_PIX_FMT_GBRP9LE:
  1757. case AV_PIX_FMT_GBRP10BE:
  1758. case AV_PIX_FMT_GBRP10LE:
  1759. case AV_PIX_FMT_GBRP12BE:
  1760. case AV_PIX_FMT_GBRP12LE:
  1761. case AV_PIX_FMT_GBRP14BE:
  1762. case AV_PIX_FMT_GBRP14LE:
  1763. case AV_PIX_FMT_GBRP16BE:
  1764. case AV_PIX_FMT_GBRP16LE:
  1765. case AV_PIX_FMT_GBRAP:
  1766. *yuv2anyX = yuv2gbrp_full_X_c;
  1767. break;
  1768. }
  1769. if (!*yuv2packedX && !*yuv2anyX)
  1770. goto YUV_PACKED;
  1771. } else {
  1772. YUV_PACKED:
  1773. switch (dstFormat) {
  1774. case AV_PIX_FMT_RGBA64LE:
  1775. #if CONFIG_SWSCALE_ALPHA
  1776. if (c->alpPixBuf) {
  1777. *yuv2packed1 = yuv2rgba64le_1_c;
  1778. *yuv2packed2 = yuv2rgba64le_2_c;
  1779. *yuv2packedX = yuv2rgba64le_X_c;
  1780. } else
  1781. #endif /* CONFIG_SWSCALE_ALPHA */
  1782. {
  1783. *yuv2packed1 = yuv2rgbx64le_1_c;
  1784. *yuv2packed2 = yuv2rgbx64le_2_c;
  1785. *yuv2packedX = yuv2rgbx64le_X_c;
  1786. }
  1787. break;
  1788. case AV_PIX_FMT_RGBA64BE:
  1789. #if CONFIG_SWSCALE_ALPHA
  1790. if (c->alpPixBuf) {
  1791. *yuv2packed1 = yuv2rgba64be_1_c;
  1792. *yuv2packed2 = yuv2rgba64be_2_c;
  1793. *yuv2packedX = yuv2rgba64be_X_c;
  1794. } else
  1795. #endif /* CONFIG_SWSCALE_ALPHA */
  1796. {
  1797. *yuv2packed1 = yuv2rgbx64be_1_c;
  1798. *yuv2packed2 = yuv2rgbx64be_2_c;
  1799. *yuv2packedX = yuv2rgbx64be_X_c;
  1800. }
  1801. break;
  1802. case AV_PIX_FMT_RGB48LE:
  1803. *yuv2packed1 = yuv2rgb48le_1_c;
  1804. *yuv2packed2 = yuv2rgb48le_2_c;
  1805. *yuv2packedX = yuv2rgb48le_X_c;
  1806. break;
  1807. case AV_PIX_FMT_RGB48BE:
  1808. *yuv2packed1 = yuv2rgb48be_1_c;
  1809. *yuv2packed2 = yuv2rgb48be_2_c;
  1810. *yuv2packedX = yuv2rgb48be_X_c;
  1811. break;
  1812. case AV_PIX_FMT_BGR48LE:
  1813. *yuv2packed1 = yuv2bgr48le_1_c;
  1814. *yuv2packed2 = yuv2bgr48le_2_c;
  1815. *yuv2packedX = yuv2bgr48le_X_c;
  1816. break;
  1817. case AV_PIX_FMT_BGR48BE:
  1818. *yuv2packed1 = yuv2bgr48be_1_c;
  1819. *yuv2packed2 = yuv2bgr48be_2_c;
  1820. *yuv2packedX = yuv2bgr48be_X_c;
  1821. break;
  1822. case AV_PIX_FMT_RGB32:
  1823. case AV_PIX_FMT_BGR32:
  1824. #if CONFIG_SMALL
  1825. *yuv2packed1 = yuv2rgb32_1_c;
  1826. *yuv2packed2 = yuv2rgb32_2_c;
  1827. *yuv2packedX = yuv2rgb32_X_c;
  1828. #else
  1829. #if CONFIG_SWSCALE_ALPHA
  1830. if (c->alpPixBuf) {
  1831. *yuv2packed1 = yuv2rgba32_1_c;
  1832. *yuv2packed2 = yuv2rgba32_2_c;
  1833. *yuv2packedX = yuv2rgba32_X_c;
  1834. } else
  1835. #endif /* CONFIG_SWSCALE_ALPHA */
  1836. {
  1837. *yuv2packed1 = yuv2rgbx32_1_c;
  1838. *yuv2packed2 = yuv2rgbx32_2_c;
  1839. *yuv2packedX = yuv2rgbx32_X_c;
  1840. }
  1841. #endif /* !CONFIG_SMALL */
  1842. break;
  1843. case AV_PIX_FMT_RGB32_1:
  1844. case AV_PIX_FMT_BGR32_1:
  1845. #if CONFIG_SMALL
  1846. *yuv2packed1 = yuv2rgb32_1_1_c;
  1847. *yuv2packed2 = yuv2rgb32_1_2_c;
  1848. *yuv2packedX = yuv2rgb32_1_X_c;
  1849. #else
  1850. #if CONFIG_SWSCALE_ALPHA
  1851. if (c->alpPixBuf) {
  1852. *yuv2packed1 = yuv2rgba32_1_1_c;
  1853. *yuv2packed2 = yuv2rgba32_1_2_c;
  1854. *yuv2packedX = yuv2rgba32_1_X_c;
  1855. } else
  1856. #endif /* CONFIG_SWSCALE_ALPHA */
  1857. {
  1858. *yuv2packed1 = yuv2rgbx32_1_1_c;
  1859. *yuv2packed2 = yuv2rgbx32_1_2_c;
  1860. *yuv2packedX = yuv2rgbx32_1_X_c;
  1861. }
  1862. #endif /* !CONFIG_SMALL */
  1863. break;
  1864. case AV_PIX_FMT_RGB24:
  1865. *yuv2packed1 = yuv2rgb24_1_c;
  1866. *yuv2packed2 = yuv2rgb24_2_c;
  1867. *yuv2packedX = yuv2rgb24_X_c;
  1868. break;
  1869. case AV_PIX_FMT_BGR24:
  1870. *yuv2packed1 = yuv2bgr24_1_c;
  1871. *yuv2packed2 = yuv2bgr24_2_c;
  1872. *yuv2packedX = yuv2bgr24_X_c;
  1873. break;
  1874. case AV_PIX_FMT_RGB565LE:
  1875. case AV_PIX_FMT_RGB565BE:
  1876. case AV_PIX_FMT_BGR565LE:
  1877. case AV_PIX_FMT_BGR565BE:
  1878. *yuv2packed1 = yuv2rgb16_1_c;
  1879. *yuv2packed2 = yuv2rgb16_2_c;
  1880. *yuv2packedX = yuv2rgb16_X_c;
  1881. break;
  1882. case AV_PIX_FMT_RGB555LE:
  1883. case AV_PIX_FMT_RGB555BE:
  1884. case AV_PIX_FMT_BGR555LE:
  1885. case AV_PIX_FMT_BGR555BE:
  1886. *yuv2packed1 = yuv2rgb15_1_c;
  1887. *yuv2packed2 = yuv2rgb15_2_c;
  1888. *yuv2packedX = yuv2rgb15_X_c;
  1889. break;
  1890. case AV_PIX_FMT_RGB444LE:
  1891. case AV_PIX_FMT_RGB444BE:
  1892. case AV_PIX_FMT_BGR444LE:
  1893. case AV_PIX_FMT_BGR444BE:
  1894. *yuv2packed1 = yuv2rgb12_1_c;
  1895. *yuv2packed2 = yuv2rgb12_2_c;
  1896. *yuv2packedX = yuv2rgb12_X_c;
  1897. break;
  1898. case AV_PIX_FMT_RGB8:
  1899. case AV_PIX_FMT_BGR8:
  1900. *yuv2packed1 = yuv2rgb8_1_c;
  1901. *yuv2packed2 = yuv2rgb8_2_c;
  1902. *yuv2packedX = yuv2rgb8_X_c;
  1903. break;
  1904. case AV_PIX_FMT_RGB4:
  1905. case AV_PIX_FMT_BGR4:
  1906. *yuv2packed1 = yuv2rgb4_1_c;
  1907. *yuv2packed2 = yuv2rgb4_2_c;
  1908. *yuv2packedX = yuv2rgb4_X_c;
  1909. break;
  1910. case AV_PIX_FMT_RGB4_BYTE:
  1911. case AV_PIX_FMT_BGR4_BYTE:
  1912. *yuv2packed1 = yuv2rgb4b_1_c;
  1913. *yuv2packed2 = yuv2rgb4b_2_c;
  1914. *yuv2packedX = yuv2rgb4b_X_c;
  1915. break;
  1916. }
  1917. }
  1918. switch (dstFormat) {
  1919. case AV_PIX_FMT_MONOWHITE:
  1920. *yuv2packed1 = yuv2monowhite_1_c;
  1921. *yuv2packed2 = yuv2monowhite_2_c;
  1922. *yuv2packedX = yuv2monowhite_X_c;
  1923. break;
  1924. case AV_PIX_FMT_MONOBLACK:
  1925. *yuv2packed1 = yuv2monoblack_1_c;
  1926. *yuv2packed2 = yuv2monoblack_2_c;
  1927. *yuv2packedX = yuv2monoblack_X_c;
  1928. break;
  1929. case AV_PIX_FMT_YUYV422:
  1930. *yuv2packed1 = yuv2yuyv422_1_c;
  1931. *yuv2packed2 = yuv2yuyv422_2_c;
  1932. *yuv2packedX = yuv2yuyv422_X_c;
  1933. break;
  1934. case AV_PIX_FMT_UYVY422:
  1935. *yuv2packed1 = yuv2uyvy422_1_c;
  1936. *yuv2packed2 = yuv2uyvy422_2_c;
  1937. *yuv2packedX = yuv2uyvy422_X_c;
  1938. break;
  1939. }
  1940. }