output.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135
  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, 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, 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, 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, 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, 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, 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, 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, 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, dither_8x8_220)[][8]={
  116. { 0, 107, 3, 187, 0, 125, 6, 212, },
  117. { 39, 7, 86, 28, 49, 11, 102, 36, },
  118. { 1, 158, 0, 131, 3, 180, 1, 151, },
  119. { 68, 19, 52, 12, 81, 25, 64, 17, },
  120. { 0, 119, 5, 203, 0, 113, 4, 195, },
  121. { 45, 9, 96, 33, 42, 8, 91, 30, },
  122. { 2, 172, 1, 144, 2, 165, 0, 137, },
  123. { 77, 23, 60, 15, 72, 21, 56, 14, },
  124. { 0, 107, 3, 187, 0, 125, 6, 212, },
  125. };
  126. #endif
  127. #define output_pixel(pos, val, bias, signedness) \
  128. if (big_endian) { \
  129. AV_WB16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
  130. } else { \
  131. AV_WL16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
  132. }
  133. static av_always_inline void
  134. yuv2plane1_16_c_template(const int32_t *src, uint16_t *dest, int dstW,
  135. int big_endian, int output_bits)
  136. {
  137. int i;
  138. int shift = 3;
  139. av_assert0(output_bits == 16);
  140. for (i = 0; i < dstW; i++) {
  141. int val = src[i] + (1 << (shift - 1));
  142. output_pixel(&dest[i], val, 0, uint);
  143. }
  144. }
  145. static av_always_inline void
  146. yuv2planeX_16_c_template(const int16_t *filter, int filterSize,
  147. const int32_t **src, uint16_t *dest, int dstW,
  148. int big_endian, int output_bits)
  149. {
  150. int i;
  151. int shift = 15;
  152. av_assert0(output_bits == 16);
  153. for (i = 0; i < dstW; i++) {
  154. int val = 1 << (shift - 1);
  155. int j;
  156. /* range of val is [0,0x7FFFFFFF], so 31 bits, but with lanczos/spline
  157. * filters (or anything with negative coeffs, the range can be slightly
  158. * wider in both directions. To account for this overflow, we subtract
  159. * a constant so it always fits in the signed range (assuming a
  160. * reasonable filterSize), and re-add that at the end. */
  161. val -= 0x40000000;
  162. for (j = 0; j < filterSize; j++)
  163. val += src[j][i] * (unsigned)filter[j];
  164. output_pixel(&dest[i], val, 0x8000, int);
  165. }
  166. }
  167. #undef output_pixel
  168. #define output_pixel(pos, val) \
  169. if (big_endian) { \
  170. AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  171. } else { \
  172. AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  173. }
  174. static av_always_inline void
  175. yuv2plane1_10_c_template(const int16_t *src, uint16_t *dest, int dstW,
  176. int big_endian, int output_bits)
  177. {
  178. int i;
  179. int shift = 15 - output_bits;
  180. for (i = 0; i < dstW; i++) {
  181. int val = src[i] + (1 << (shift - 1));
  182. output_pixel(&dest[i], val);
  183. }
  184. }
  185. static av_always_inline void
  186. yuv2planeX_10_c_template(const int16_t *filter, int filterSize,
  187. const int16_t **src, uint16_t *dest, int dstW,
  188. int big_endian, int output_bits)
  189. {
  190. int i;
  191. int shift = 11 + 16 - output_bits;
  192. for (i = 0; i < dstW; i++) {
  193. int val = 1 << (shift - 1);
  194. int j;
  195. for (j = 0; j < filterSize; j++)
  196. val += src[j][i] * filter[j];
  197. output_pixel(&dest[i], val);
  198. }
  199. }
  200. #undef output_pixel
  201. #define yuv2NBPS(bits, BE_LE, is_be, template_size, typeX_t) \
  202. static void yuv2plane1_ ## bits ## BE_LE ## _c(const int16_t *src, \
  203. uint8_t *dest, int dstW, \
  204. const uint8_t *dither, int offset)\
  205. { \
  206. yuv2plane1_ ## template_size ## _c_template((const typeX_t *) src, \
  207. (uint16_t *) dest, dstW, is_be, bits); \
  208. }\
  209. static void yuv2planeX_ ## bits ## BE_LE ## _c(const int16_t *filter, int filterSize, \
  210. const int16_t **src, uint8_t *dest, int dstW, \
  211. const uint8_t *dither, int offset)\
  212. { \
  213. yuv2planeX_## template_size ## _c_template(filter, \
  214. filterSize, (const typeX_t **) src, \
  215. (uint16_t *) dest, dstW, is_be, bits); \
  216. }
  217. yuv2NBPS( 9, BE, 1, 10, int16_t)
  218. yuv2NBPS( 9, LE, 0, 10, int16_t)
  219. yuv2NBPS(10, BE, 1, 10, int16_t)
  220. yuv2NBPS(10, LE, 0, 10, int16_t)
  221. yuv2NBPS(12, BE, 1, 10, int16_t)
  222. yuv2NBPS(12, LE, 0, 10, int16_t)
  223. yuv2NBPS(14, BE, 1, 10, int16_t)
  224. yuv2NBPS(14, LE, 0, 10, int16_t)
  225. yuv2NBPS(16, BE, 1, 16, int32_t)
  226. yuv2NBPS(16, LE, 0, 16, int32_t)
  227. static void yuv2planeX_8_c(const int16_t *filter, int filterSize,
  228. const int16_t **src, uint8_t *dest, int dstW,
  229. const uint8_t *dither, int offset)
  230. {
  231. int i;
  232. for (i=0; i<dstW; i++) {
  233. int val = dither[(i + offset) & 7] << 12;
  234. int j;
  235. for (j=0; j<filterSize; j++)
  236. val += src[j][i] * filter[j];
  237. dest[i]= av_clip_uint8(val>>19);
  238. }
  239. }
  240. static void yuv2plane1_8_c(const int16_t *src, uint8_t *dest, int dstW,
  241. const uint8_t *dither, int offset)
  242. {
  243. int i;
  244. for (i=0; i<dstW; i++) {
  245. int val = (src[i] + dither[(i + offset) & 7]) >> 7;
  246. dest[i]= av_clip_uint8(val);
  247. }
  248. }
  249. static void yuv2nv12cX_c(SwsContext *c, const int16_t *chrFilter, int chrFilterSize,
  250. const int16_t **chrUSrc, const int16_t **chrVSrc,
  251. uint8_t *dest, int chrDstW)
  252. {
  253. enum AVPixelFormat dstFormat = c->dstFormat;
  254. const uint8_t *chrDither = c->chrDither8;
  255. int i;
  256. if (dstFormat == AV_PIX_FMT_NV12)
  257. for (i=0; i<chrDstW; i++) {
  258. int u = chrDither[i & 7] << 12;
  259. int v = chrDither[(i + 3) & 7] << 12;
  260. int j;
  261. for (j=0; j<chrFilterSize; j++) {
  262. u += chrUSrc[j][i] * chrFilter[j];
  263. v += chrVSrc[j][i] * chrFilter[j];
  264. }
  265. dest[2*i]= av_clip_uint8(u>>19);
  266. dest[2*i+1]= av_clip_uint8(v>>19);
  267. }
  268. else
  269. for (i=0; i<chrDstW; i++) {
  270. int u = chrDither[i & 7] << 12;
  271. int v = chrDither[(i + 3) & 7] << 12;
  272. int j;
  273. for (j=0; j<chrFilterSize; j++) {
  274. u += chrUSrc[j][i] * chrFilter[j];
  275. v += chrVSrc[j][i] * chrFilter[j];
  276. }
  277. dest[2*i]= av_clip_uint8(v>>19);
  278. dest[2*i+1]= av_clip_uint8(u>>19);
  279. }
  280. }
  281. #define accumulate_bit(acc, val) \
  282. acc <<= 1; \
  283. acc |= (val) >= (128 + 110)
  284. #define output_pixel(pos, acc) \
  285. if (target == AV_PIX_FMT_MONOBLACK) { \
  286. pos = acc; \
  287. } else { \
  288. pos = ~acc; \
  289. }
  290. static av_always_inline void
  291. yuv2mono_X_c_template(SwsContext *c, const int16_t *lumFilter,
  292. const int16_t **lumSrc, int lumFilterSize,
  293. const int16_t *chrFilter, const int16_t **chrUSrc,
  294. const int16_t **chrVSrc, int chrFilterSize,
  295. const int16_t **alpSrc, uint8_t *dest, int dstW,
  296. int y, enum AVPixelFormat target)
  297. {
  298. const uint8_t * const d128=dither_8x8_220[y&7];
  299. int i;
  300. unsigned acc = 0;
  301. int err = 0;
  302. for (i = 0; i < dstW; i += 2) {
  303. int j;
  304. int Y1 = 1 << 18;
  305. int Y2 = 1 << 18;
  306. for (j = 0; j < lumFilterSize; j++) {
  307. Y1 += lumSrc[j][i] * lumFilter[j];
  308. Y2 += lumSrc[j][i+1] * lumFilter[j];
  309. }
  310. Y1 >>= 19;
  311. Y2 >>= 19;
  312. if ((Y1 | Y2) & 0x100) {
  313. Y1 = av_clip_uint8(Y1);
  314. Y2 = av_clip_uint8(Y2);
  315. }
  316. if (c->flags & SWS_ERROR_DIFFUSION) {
  317. Y1 += (7*err + 1*c->dither_error[0][i] + 5*c->dither_error[0][i+1] + 3*c->dither_error[0][i+2] + 8 - 256)>>4;
  318. c->dither_error[0][i] = err;
  319. acc = 2*acc + (Y1 >= 128);
  320. Y1 -= 220*(acc&1);
  321. err = Y2 + ((7*Y1 + 1*c->dither_error[0][i+1] + 5*c->dither_error[0][i+2] + 3*c->dither_error[0][i+3] + 8 - 256)>>4);
  322. c->dither_error[0][i+1] = Y1;
  323. acc = 2*acc + (err >= 128);
  324. err -= 220*(acc&1);
  325. } else {
  326. accumulate_bit(acc, Y1 + d128[(i + 0) & 7]);
  327. accumulate_bit(acc, Y2 + d128[(i + 1) & 7]);
  328. }
  329. if ((i & 7) == 6) {
  330. output_pixel(*dest++, acc);
  331. }
  332. }
  333. c->dither_error[0][i] = err;
  334. if (i & 6) {
  335. output_pixel(*dest, acc);
  336. }
  337. }
  338. static av_always_inline void
  339. yuv2mono_2_c_template(SwsContext *c, const int16_t *buf[2],
  340. const int16_t *ubuf[2], const int16_t *vbuf[2],
  341. const int16_t *abuf[2], uint8_t *dest, int dstW,
  342. int yalpha, int uvalpha, int y,
  343. enum AVPixelFormat target)
  344. {
  345. const int16_t *buf0 = buf[0], *buf1 = buf[1];
  346. const uint8_t * const d128 = dither_8x8_220[y & 7];
  347. int yalpha1 = 4096 - yalpha;
  348. int i;
  349. if (c->flags & SWS_ERROR_DIFFUSION) {
  350. int err = 0;
  351. int acc = 0;
  352. for (i = 0; i < dstW; i +=2) {
  353. int Y;
  354. Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
  355. 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;
  356. c->dither_error[0][i] = err;
  357. acc = 2*acc + (Y >= 128);
  358. Y -= 220*(acc&1);
  359. err = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
  360. 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;
  361. c->dither_error[0][i+1] = Y;
  362. acc = 2*acc + (err >= 128);
  363. err -= 220*(acc&1);
  364. if ((i & 7) == 6)
  365. output_pixel(*dest++, acc);
  366. }
  367. c->dither_error[0][i] = err;
  368. } else {
  369. for (i = 0; i < dstW; i += 8) {
  370. int Y, acc = 0;
  371. Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
  372. accumulate_bit(acc, Y + d128[0]);
  373. Y = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
  374. accumulate_bit(acc, Y + d128[1]);
  375. Y = (buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19;
  376. accumulate_bit(acc, Y + d128[2]);
  377. Y = (buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19;
  378. accumulate_bit(acc, Y + d128[3]);
  379. Y = (buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19;
  380. accumulate_bit(acc, Y + d128[4]);
  381. Y = (buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19;
  382. accumulate_bit(acc, Y + d128[5]);
  383. Y = (buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19;
  384. accumulate_bit(acc, Y + d128[6]);
  385. Y = (buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19;
  386. accumulate_bit(acc, Y + d128[7]);
  387. output_pixel(*dest++, acc);
  388. }
  389. }
  390. }
  391. static av_always_inline void
  392. yuv2mono_1_c_template(SwsContext *c, const int16_t *buf0,
  393. const int16_t *ubuf[2], const int16_t *vbuf[2],
  394. const int16_t *abuf0, uint8_t *dest, int dstW,
  395. int uvalpha, int y, enum AVPixelFormat target)
  396. {
  397. const uint8_t * const d128 = dither_8x8_220[y & 7];
  398. int i;
  399. if (c->flags & SWS_ERROR_DIFFUSION) {
  400. int err = 0;
  401. int acc = 0;
  402. for (i = 0; i < dstW; i +=2) {
  403. int Y;
  404. Y = ((buf0[i + 0] + 64) >> 7);
  405. 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;
  406. c->dither_error[0][i] = err;
  407. acc = 2*acc + (Y >= 128);
  408. Y -= 220*(acc&1);
  409. err = ((buf0[i + 1] + 64) >> 7);
  410. 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;
  411. c->dither_error[0][i+1] = Y;
  412. acc = 2*acc + (err >= 128);
  413. err -= 220*(acc&1);
  414. if ((i & 7) == 6)
  415. output_pixel(*dest++, acc);
  416. }
  417. c->dither_error[0][i] = err;
  418. } else {
  419. for (i = 0; i < dstW; i += 8) {
  420. int acc = 0;
  421. accumulate_bit(acc, ((buf0[i + 0] + 64) >> 7) + d128[0]);
  422. accumulate_bit(acc, ((buf0[i + 1] + 64) >> 7) + d128[1]);
  423. accumulate_bit(acc, ((buf0[i + 2] + 64) >> 7) + d128[2]);
  424. accumulate_bit(acc, ((buf0[i + 3] + 64) >> 7) + d128[3]);
  425. accumulate_bit(acc, ((buf0[i + 4] + 64) >> 7) + d128[4]);
  426. accumulate_bit(acc, ((buf0[i + 5] + 64) >> 7) + d128[5]);
  427. accumulate_bit(acc, ((buf0[i + 6] + 64) >> 7) + d128[6]);
  428. accumulate_bit(acc, ((buf0[i + 7] + 64) >> 7) + d128[7]);
  429. output_pixel(*dest++, acc);
  430. }
  431. }
  432. }
  433. #undef output_pixel
  434. #undef accumulate_bit
  435. #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
  436. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  437. const int16_t **lumSrc, int lumFilterSize, \
  438. const int16_t *chrFilter, const int16_t **chrUSrc, \
  439. const int16_t **chrVSrc, int chrFilterSize, \
  440. const int16_t **alpSrc, uint8_t *dest, int dstW, \
  441. int y) \
  442. { \
  443. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  444. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  445. alpSrc, dest, dstW, y, fmt); \
  446. } \
  447. \
  448. static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
  449. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  450. const int16_t *abuf[2], uint8_t *dest, int dstW, \
  451. int yalpha, int uvalpha, int y) \
  452. { \
  453. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  454. dest, dstW, yalpha, uvalpha, y, fmt); \
  455. } \
  456. \
  457. static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
  458. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  459. const int16_t *abuf0, uint8_t *dest, int dstW, \
  460. int uvalpha, int y) \
  461. { \
  462. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
  463. abuf0, dest, dstW, uvalpha, \
  464. y, fmt); \
  465. }
  466. YUV2PACKEDWRAPPER(yuv2mono,, white, AV_PIX_FMT_MONOWHITE)
  467. YUV2PACKEDWRAPPER(yuv2mono,, black, AV_PIX_FMT_MONOBLACK)
  468. #define output_pixels(pos, Y1, U, Y2, V) \
  469. if (target == AV_PIX_FMT_YUYV422) { \
  470. dest[pos + 0] = Y1; \
  471. dest[pos + 1] = U; \
  472. dest[pos + 2] = Y2; \
  473. dest[pos + 3] = V; \
  474. } else { \
  475. dest[pos + 0] = U; \
  476. dest[pos + 1] = Y1; \
  477. dest[pos + 2] = V; \
  478. dest[pos + 3] = Y2; \
  479. }
  480. static av_always_inline void
  481. yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
  482. const int16_t **lumSrc, int lumFilterSize,
  483. const int16_t *chrFilter, const int16_t **chrUSrc,
  484. const int16_t **chrVSrc, int chrFilterSize,
  485. const int16_t **alpSrc, uint8_t *dest, int dstW,
  486. int y, enum AVPixelFormat target)
  487. {
  488. int i;
  489. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  490. int j;
  491. int Y1 = 1 << 18;
  492. int Y2 = 1 << 18;
  493. int U = 1 << 18;
  494. int V = 1 << 18;
  495. for (j = 0; j < lumFilterSize; j++) {
  496. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  497. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  498. }
  499. for (j = 0; j < chrFilterSize; j++) {
  500. U += chrUSrc[j][i] * chrFilter[j];
  501. V += chrVSrc[j][i] * chrFilter[j];
  502. }
  503. Y1 >>= 19;
  504. Y2 >>= 19;
  505. U >>= 19;
  506. V >>= 19;
  507. if ((Y1 | Y2 | U | V) & 0x100) {
  508. Y1 = av_clip_uint8(Y1);
  509. Y2 = av_clip_uint8(Y2);
  510. U = av_clip_uint8(U);
  511. V = av_clip_uint8(V);
  512. }
  513. output_pixels(4*i, Y1, U, Y2, V);
  514. }
  515. }
  516. static av_always_inline void
  517. yuv2422_2_c_template(SwsContext *c, const int16_t *buf[2],
  518. const int16_t *ubuf[2], const int16_t *vbuf[2],
  519. const int16_t *abuf[2], uint8_t *dest, int dstW,
  520. int yalpha, int uvalpha, int y,
  521. enum AVPixelFormat target)
  522. {
  523. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  524. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  525. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  526. int yalpha1 = 4096 - yalpha;
  527. int uvalpha1 = 4096 - uvalpha;
  528. int i;
  529. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  530. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
  531. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
  532. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
  533. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
  534. if ((Y1 | Y2 | U | V) & 0x100) {
  535. Y1 = av_clip_uint8(Y1);
  536. Y2 = av_clip_uint8(Y2);
  537. U = av_clip_uint8(U);
  538. V = av_clip_uint8(V);
  539. }
  540. output_pixels(i * 4, Y1, U, Y2, V);
  541. }
  542. }
  543. static av_always_inline void
  544. yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
  545. const int16_t *ubuf[2], const int16_t *vbuf[2],
  546. const int16_t *abuf0, uint8_t *dest, int dstW,
  547. int uvalpha, int y, enum AVPixelFormat target)
  548. {
  549. const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  550. int i;
  551. if (uvalpha < 2048) {
  552. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  553. int Y1 = (buf0[i * 2 ]+64) >> 7;
  554. int Y2 = (buf0[i * 2 + 1]+64) >> 7;
  555. int U = (ubuf0[i] +64) >> 7;
  556. int V = (vbuf0[i] +64) >> 7;
  557. if ((Y1 | Y2 | U | V) & 0x100) {
  558. Y1 = av_clip_uint8(Y1);
  559. Y2 = av_clip_uint8(Y2);
  560. U = av_clip_uint8(U);
  561. V = av_clip_uint8(V);
  562. }
  563. Y1 = av_clip_uint8(Y1);
  564. Y2 = av_clip_uint8(Y2);
  565. U = av_clip_uint8(U);
  566. V = av_clip_uint8(V);
  567. output_pixels(i * 4, Y1, U, Y2, V);
  568. }
  569. } else {
  570. const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  571. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  572. int Y1 = (buf0[i * 2 ] + 64) >> 7;
  573. int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
  574. int U = (ubuf0[i] + ubuf1[i]+128) >> 8;
  575. int V = (vbuf0[i] + vbuf1[i]+128) >> 8;
  576. if ((Y1 | Y2 | U | V) & 0x100) {
  577. Y1 = av_clip_uint8(Y1);
  578. Y2 = av_clip_uint8(Y2);
  579. U = av_clip_uint8(U);
  580. V = av_clip_uint8(V);
  581. }
  582. Y1 = av_clip_uint8(Y1);
  583. Y2 = av_clip_uint8(Y2);
  584. U = av_clip_uint8(U);
  585. V = av_clip_uint8(V);
  586. output_pixels(i * 4, Y1, U, Y2, V);
  587. }
  588. }
  589. }
  590. #undef output_pixels
  591. YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, AV_PIX_FMT_YUYV422)
  592. YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, AV_PIX_FMT_UYVY422)
  593. #define R_B ((target == AV_PIX_FMT_RGB48LE || target == AV_PIX_FMT_RGB48BE) ? R : B)
  594. #define B_R ((target == AV_PIX_FMT_RGB48LE || target == AV_PIX_FMT_RGB48BE) ? B : R)
  595. #define output_pixel(pos, val) \
  596. if (isBE(target)) { \
  597. AV_WB16(pos, val); \
  598. } else { \
  599. AV_WL16(pos, val); \
  600. }
  601. static av_always_inline void
  602. yuv2rgba64_X_c_template(SwsContext *c, const int16_t *lumFilter,
  603. const int32_t **lumSrc, int lumFilterSize,
  604. const int16_t *chrFilter, const int32_t **chrUSrc,
  605. const int32_t **chrVSrc, int chrFilterSize,
  606. const int32_t **alpSrc, uint16_t *dest, int dstW,
  607. int y, enum AVPixelFormat target, int hasAlpha)
  608. {
  609. int i;
  610. int A1 = 0xffff<<14, A2 = 0xffff<<14;
  611. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  612. int j;
  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 = 0xffff<<14, A2 = 0xffff<<14;
  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. int A1 = 0xffff<<14, A2= 0xffff<<14;
  725. if (uvalpha < 2048) {
  726. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  727. int Y1 = (buf0[i * 2] ) >> 2;
  728. int Y2 = (buf0[i * 2 + 1]) >> 2;
  729. int U = (ubuf0[i] + (-128 << 11)) >> 2;
  730. int V = (vbuf0[i] + (-128 << 11)) >> 2;
  731. int R, G, B;
  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. int A1 = 0xffff<<14, A2 = 0xffff<<14;
  760. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  761. int Y1 = (buf0[i * 2] ) >> 2;
  762. int Y2 = (buf0[i * 2 + 1]) >> 2;
  763. int U = (ubuf0[i] + ubuf1[i] + (-128 << 12)) >> 3;
  764. int V = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
  765. int R, G, B;
  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. av_assert2((((r[Y1] + g[Y1] + b[Y1]) >> sh) & 0xFF) == 0);
  1025. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
  1026. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
  1027. } else {
  1028. #if defined(ASSERT_LEVEL) && ASSERT_LEVEL > 1
  1029. int sh = (target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24;
  1030. av_assert2((((r[Y1] + g[Y1] + b[Y1]) >> sh) & 0xFF) == 0xFF);
  1031. #endif
  1032. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
  1033. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
  1034. }
  1035. #endif
  1036. } else if (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) {
  1037. uint8_t *dest = (uint8_t *) _dest;
  1038. const uint8_t *r = (const uint8_t *) _r;
  1039. const uint8_t *g = (const uint8_t *) _g;
  1040. const uint8_t *b = (const uint8_t *) _b;
  1041. #define r_b ((target == AV_PIX_FMT_RGB24) ? r : b)
  1042. #define b_r ((target == AV_PIX_FMT_RGB24) ? b : r)
  1043. dest[i * 6 + 0] = r_b[Y1];
  1044. dest[i * 6 + 1] = g[Y1];
  1045. dest[i * 6 + 2] = b_r[Y1];
  1046. dest[i * 6 + 3] = r_b[Y2];
  1047. dest[i * 6 + 4] = g[Y2];
  1048. dest[i * 6 + 5] = b_r[Y2];
  1049. #undef r_b
  1050. #undef b_r
  1051. } else if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565 ||
  1052. target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555 ||
  1053. target == AV_PIX_FMT_RGB444 || target == AV_PIX_FMT_BGR444) {
  1054. uint16_t *dest = (uint16_t *) _dest;
  1055. const uint16_t *r = (const uint16_t *) _r;
  1056. const uint16_t *g = (const uint16_t *) _g;
  1057. const uint16_t *b = (const uint16_t *) _b;
  1058. int dr1, dg1, db1, dr2, dg2, db2;
  1059. if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565) {
  1060. dr1 = dither_2x2_8[ y & 1 ][0];
  1061. dg1 = dither_2x2_4[ y & 1 ][0];
  1062. db1 = dither_2x2_8[(y & 1) ^ 1][0];
  1063. dr2 = dither_2x2_8[ y & 1 ][1];
  1064. dg2 = dither_2x2_4[ y & 1 ][1];
  1065. db2 = dither_2x2_8[(y & 1) ^ 1][1];
  1066. } else if (target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555) {
  1067. dr1 = dither_2x2_8[ y & 1 ][0];
  1068. dg1 = dither_2x2_8[ y & 1 ][1];
  1069. db1 = dither_2x2_8[(y & 1) ^ 1][0];
  1070. dr2 = dither_2x2_8[ y & 1 ][1];
  1071. dg2 = dither_2x2_8[ y & 1 ][0];
  1072. db2 = dither_2x2_8[(y & 1) ^ 1][1];
  1073. } else {
  1074. dr1 = dither_4x4_16[ y & 3 ][0];
  1075. dg1 = dither_4x4_16[ y & 3 ][1];
  1076. db1 = dither_4x4_16[(y & 3) ^ 3][0];
  1077. dr2 = dither_4x4_16[ y & 3 ][1];
  1078. dg2 = dither_4x4_16[ y & 3 ][0];
  1079. db2 = dither_4x4_16[(y & 3) ^ 3][1];
  1080. }
  1081. dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
  1082. dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
  1083. } else /* 8/4-bit */ {
  1084. uint8_t *dest = (uint8_t *) _dest;
  1085. const uint8_t *r = (const uint8_t *) _r;
  1086. const uint8_t *g = (const uint8_t *) _g;
  1087. const uint8_t *b = (const uint8_t *) _b;
  1088. int dr1, dg1, db1, dr2, dg2, db2;
  1089. if (target == AV_PIX_FMT_RGB8 || target == AV_PIX_FMT_BGR8) {
  1090. const uint8_t * const d64 = dither_8x8_73[y & 7];
  1091. const uint8_t * const d32 = dither_8x8_32[y & 7];
  1092. dr1 = dg1 = d32[(i * 2 + 0) & 7];
  1093. db1 = d64[(i * 2 + 0) & 7];
  1094. dr2 = dg2 = d32[(i * 2 + 1) & 7];
  1095. db2 = d64[(i * 2 + 1) & 7];
  1096. } else {
  1097. const uint8_t * const d64 = dither_8x8_73 [y & 7];
  1098. const uint8_t * const d128 = dither_8x8_220[y & 7];
  1099. dr1 = db1 = d128[(i * 2 + 0) & 7];
  1100. dg1 = d64[(i * 2 + 0) & 7];
  1101. dr2 = db2 = d128[(i * 2 + 1) & 7];
  1102. dg2 = d64[(i * 2 + 1) & 7];
  1103. }
  1104. if (target == AV_PIX_FMT_RGB4 || target == AV_PIX_FMT_BGR4) {
  1105. dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
  1106. ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
  1107. } else {
  1108. dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
  1109. dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
  1110. }
  1111. }
  1112. }
  1113. static av_always_inline void
  1114. yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
  1115. const int16_t **lumSrc, int lumFilterSize,
  1116. const int16_t *chrFilter, const int16_t **chrUSrc,
  1117. const int16_t **chrVSrc, int chrFilterSize,
  1118. const int16_t **alpSrc, uint8_t *dest, int dstW,
  1119. int y, enum AVPixelFormat target, int hasAlpha)
  1120. {
  1121. int i;
  1122. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1123. int j, A1, A2;
  1124. int Y1 = 1 << 18;
  1125. int Y2 = 1 << 18;
  1126. int U = 1 << 18;
  1127. int V = 1 << 18;
  1128. const void *r, *g, *b;
  1129. for (j = 0; j < lumFilterSize; j++) {
  1130. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  1131. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  1132. }
  1133. for (j = 0; j < chrFilterSize; j++) {
  1134. U += chrUSrc[j][i] * chrFilter[j];
  1135. V += chrVSrc[j][i] * chrFilter[j];
  1136. }
  1137. Y1 >>= 19;
  1138. Y2 >>= 19;
  1139. U >>= 19;
  1140. V >>= 19;
  1141. if (hasAlpha) {
  1142. A1 = 1 << 18;
  1143. A2 = 1 << 18;
  1144. for (j = 0; j < lumFilterSize; j++) {
  1145. A1 += alpSrc[j][i * 2 ] * lumFilter[j];
  1146. A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
  1147. }
  1148. A1 >>= 19;
  1149. A2 >>= 19;
  1150. if ((A1 | A2) & 0x100) {
  1151. A1 = av_clip_uint8(A1);
  1152. A2 = av_clip_uint8(A2);
  1153. }
  1154. }
  1155. r = c->table_rV[V + YUVRGB_TABLE_HEADROOM];
  1156. g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]);
  1157. b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  1158. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1159. r, g, b, y, target, hasAlpha);
  1160. }
  1161. }
  1162. static av_always_inline void
  1163. yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
  1164. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1165. const int16_t *abuf[2], uint8_t *dest, int dstW,
  1166. int yalpha, int uvalpha, int y,
  1167. enum AVPixelFormat target, int hasAlpha)
  1168. {
  1169. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  1170. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  1171. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
  1172. *abuf0 = hasAlpha ? abuf[0] : NULL,
  1173. *abuf1 = hasAlpha ? abuf[1] : NULL;
  1174. int yalpha1 = 4096 - yalpha;
  1175. int uvalpha1 = 4096 - uvalpha;
  1176. int i;
  1177. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1178. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
  1179. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
  1180. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
  1181. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
  1182. int A1, A2;
  1183. const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
  1184. *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
  1185. *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  1186. if (hasAlpha) {
  1187. A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 19;
  1188. A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
  1189. A1 = av_clip_uint8(A1);
  1190. A2 = av_clip_uint8(A2);
  1191. }
  1192. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1193. r, g, b, y, target, hasAlpha);
  1194. }
  1195. }
  1196. static av_always_inline void
  1197. yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
  1198. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1199. const int16_t *abuf0, uint8_t *dest, int dstW,
  1200. int uvalpha, int y, enum AVPixelFormat target,
  1201. int hasAlpha)
  1202. {
  1203. const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  1204. int i;
  1205. if (uvalpha < 2048) {
  1206. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1207. int Y1 = (buf0[i * 2 ] + 64) >> 7;
  1208. int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
  1209. int U = (ubuf0[i] + 64) >> 7;
  1210. int V = (vbuf0[i] + 64) >> 7;
  1211. int A1, A2;
  1212. const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
  1213. *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
  1214. *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  1215. if (hasAlpha) {
  1216. A1 = abuf0[i * 2 ] * 255 + 16384 >> 15;
  1217. A2 = abuf0[i * 2 + 1] * 255 + 16384 >> 15;
  1218. A1 = av_clip_uint8(A1);
  1219. A2 = av_clip_uint8(A2);
  1220. }
  1221. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1222. r, g, b, y, target, hasAlpha);
  1223. }
  1224. } else {
  1225. const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  1226. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1227. int Y1 = (buf0[i * 2 ] + 64) >> 7;
  1228. int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
  1229. int U = (ubuf0[i] + ubuf1[i] + 128) >> 8;
  1230. int V = (vbuf0[i] + vbuf1[i] + 128) >> 8;
  1231. int A1, A2;
  1232. const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
  1233. *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
  1234. *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  1235. if (hasAlpha) {
  1236. A1 = (abuf0[i * 2 ] + 64) >> 7;
  1237. A2 = (abuf0[i * 2 + 1] + 64) >> 7;
  1238. A1 = av_clip_uint8(A1);
  1239. A2 = av_clip_uint8(A2);
  1240. }
  1241. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1242. r, g, b, y, target, hasAlpha);
  1243. }
  1244. }
  1245. }
  1246. #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
  1247. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  1248. const int16_t **lumSrc, int lumFilterSize, \
  1249. const int16_t *chrFilter, const int16_t **chrUSrc, \
  1250. const int16_t **chrVSrc, int chrFilterSize, \
  1251. const int16_t **alpSrc, uint8_t *dest, int dstW, \
  1252. int y) \
  1253. { \
  1254. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  1255. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  1256. alpSrc, dest, dstW, y, fmt, hasAlpha); \
  1257. }
  1258. #define YUV2RGBWRAPPERX2(name, base, ext, fmt, hasAlpha) \
  1259. YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
  1260. static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
  1261. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  1262. const int16_t *abuf[2], uint8_t *dest, int dstW, \
  1263. int yalpha, int uvalpha, int y) \
  1264. { \
  1265. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  1266. dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
  1267. }
  1268. #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
  1269. YUV2RGBWRAPPERX2(name, base, ext, fmt, hasAlpha) \
  1270. static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
  1271. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  1272. const int16_t *abuf0, uint8_t *dest, int dstW, \
  1273. int uvalpha, int y) \
  1274. { \
  1275. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
  1276. dstW, uvalpha, y, fmt, hasAlpha); \
  1277. }
  1278. #if CONFIG_SMALL
  1279. YUV2RGBWRAPPER(yuv2rgb,, 32_1, AV_PIX_FMT_RGB32_1, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1280. YUV2RGBWRAPPER(yuv2rgb,, 32, AV_PIX_FMT_RGB32, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1281. #else
  1282. #if CONFIG_SWSCALE_ALPHA
  1283. YUV2RGBWRAPPER(yuv2rgb,, a32_1, AV_PIX_FMT_RGB32_1, 1)
  1284. YUV2RGBWRAPPER(yuv2rgb,, a32, AV_PIX_FMT_RGB32, 1)
  1285. #endif
  1286. YUV2RGBWRAPPER(yuv2rgb,, x32_1, AV_PIX_FMT_RGB32_1, 0)
  1287. YUV2RGBWRAPPER(yuv2rgb,, x32, AV_PIX_FMT_RGB32, 0)
  1288. #endif
  1289. YUV2RGBWRAPPER(yuv2, rgb, rgb24, AV_PIX_FMT_RGB24, 0)
  1290. YUV2RGBWRAPPER(yuv2, rgb, bgr24, AV_PIX_FMT_BGR24, 0)
  1291. YUV2RGBWRAPPER(yuv2rgb,, 16, AV_PIX_FMT_RGB565, 0)
  1292. YUV2RGBWRAPPER(yuv2rgb,, 15, AV_PIX_FMT_RGB555, 0)
  1293. YUV2RGBWRAPPER(yuv2rgb,, 12, AV_PIX_FMT_RGB444, 0)
  1294. YUV2RGBWRAPPER(yuv2rgb,, 8, AV_PIX_FMT_RGB8, 0)
  1295. YUV2RGBWRAPPER(yuv2rgb,, 4, AV_PIX_FMT_RGB4, 0)
  1296. YUV2RGBWRAPPER(yuv2rgb,, 4b, AV_PIX_FMT_RGB4_BYTE, 0)
  1297. static av_always_inline void yuv2rgb_write_full(SwsContext *c,
  1298. uint8_t *dest, int i, int Y, int A, int U, int V,
  1299. int y, enum AVPixelFormat target, int hasAlpha, int err[4])
  1300. {
  1301. int R, G, B;
  1302. int isrgb8 = target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8;
  1303. Y -= c->yuv2rgb_y_offset;
  1304. Y *= c->yuv2rgb_y_coeff;
  1305. Y += 1 << 21;
  1306. R = Y + V*c->yuv2rgb_v2r_coeff;
  1307. G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
  1308. B = Y + U*c->yuv2rgb_u2b_coeff;
  1309. if ((R | G | B) & 0xC0000000) {
  1310. R = av_clip_uintp2(R, 30);
  1311. G = av_clip_uintp2(G, 30);
  1312. B = av_clip_uintp2(B, 30);
  1313. }
  1314. switch(target) {
  1315. case AV_PIX_FMT_ARGB:
  1316. dest[0] = hasAlpha ? A : 255;
  1317. dest[1] = R >> 22;
  1318. dest[2] = G >> 22;
  1319. dest[3] = B >> 22;
  1320. break;
  1321. case AV_PIX_FMT_RGB24:
  1322. dest[0] = R >> 22;
  1323. dest[1] = G >> 22;
  1324. dest[2] = B >> 22;
  1325. break;
  1326. case AV_PIX_FMT_RGBA:
  1327. dest[0] = R >> 22;
  1328. dest[1] = G >> 22;
  1329. dest[2] = B >> 22;
  1330. dest[3] = hasAlpha ? A : 255;
  1331. break;
  1332. case AV_PIX_FMT_ABGR:
  1333. dest[0] = hasAlpha ? A : 255;
  1334. dest[1] = B >> 22;
  1335. dest[2] = G >> 22;
  1336. dest[3] = R >> 22;
  1337. break;
  1338. case AV_PIX_FMT_BGR24:
  1339. dest[0] = B >> 22;
  1340. dest[1] = G >> 22;
  1341. dest[2] = R >> 22;
  1342. break;
  1343. case AV_PIX_FMT_BGRA:
  1344. dest[0] = B >> 22;
  1345. dest[1] = G >> 22;
  1346. dest[2] = R >> 22;
  1347. dest[3] = hasAlpha ? A : 255;
  1348. break;
  1349. case AV_PIX_FMT_BGR4_BYTE:
  1350. case AV_PIX_FMT_RGB4_BYTE:
  1351. case AV_PIX_FMT_BGR8:
  1352. case AV_PIX_FMT_RGB8:
  1353. {
  1354. int r,g,b;
  1355. R >>= 22;
  1356. G >>= 22;
  1357. B >>= 22;
  1358. 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;
  1359. 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;
  1360. 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;
  1361. c->dither_error[0][i] = err[0];
  1362. c->dither_error[1][i] = err[1];
  1363. c->dither_error[2][i] = err[2];
  1364. r = R >> (isrgb8 ? 5 : 7);
  1365. g = G >> (isrgb8 ? 5 : 6);
  1366. b = B >> (isrgb8 ? 6 : 7);
  1367. r = av_clip(r, 0, isrgb8 ? 7 : 1);
  1368. g = av_clip(g, 0, isrgb8 ? 7 : 3);
  1369. b = av_clip(b, 0, isrgb8 ? 3 : 1);
  1370. err[0] = R - r*(isrgb8 ? 36 : 255);
  1371. err[1] = G - g*(isrgb8 ? 36 : 85);
  1372. err[2] = B - b*(isrgb8 ? 85 : 255);
  1373. if(target == AV_PIX_FMT_BGR4_BYTE) {
  1374. dest[0] = r + 2*g + 8*b;
  1375. } else if(target == AV_PIX_FMT_RGB4_BYTE) {
  1376. dest[0] = b + 2*g + 8*r;
  1377. } else if(target == AV_PIX_FMT_BGR8) {
  1378. dest[0] = r + 8*g + 64*b;
  1379. } else if(target == AV_PIX_FMT_RGB8) {
  1380. dest[0] = b + 4*g + 32*r;
  1381. } else
  1382. av_assert2(0);
  1383. break;}
  1384. }
  1385. }
  1386. static av_always_inline void
  1387. yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
  1388. const int16_t **lumSrc, int lumFilterSize,
  1389. const int16_t *chrFilter, const int16_t **chrUSrc,
  1390. const int16_t **chrVSrc, int chrFilterSize,
  1391. const int16_t **alpSrc, uint8_t *dest,
  1392. int dstW, int y, enum AVPixelFormat target, int hasAlpha)
  1393. {
  1394. int i;
  1395. int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
  1396. int err[4] = {0};
  1397. int A = 0; //init to silence warning
  1398. if( target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
  1399. || target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8)
  1400. step = 1;
  1401. for (i = 0; i < dstW; i++) {
  1402. int j;
  1403. int Y = 1<<9;
  1404. int U = (1<<9)-(128 << 19);
  1405. int V = (1<<9)-(128 << 19);
  1406. for (j = 0; j < lumFilterSize; j++) {
  1407. Y += lumSrc[j][i] * lumFilter[j];
  1408. }
  1409. for (j = 0; j < chrFilterSize; j++) {
  1410. U += chrUSrc[j][i] * chrFilter[j];
  1411. V += chrVSrc[j][i] * chrFilter[j];
  1412. }
  1413. Y >>= 10;
  1414. U >>= 10;
  1415. V >>= 10;
  1416. if (hasAlpha) {
  1417. A = 1 << 18;
  1418. for (j = 0; j < lumFilterSize; j++) {
  1419. A += alpSrc[j][i] * lumFilter[j];
  1420. }
  1421. A >>= 19;
  1422. if (A & 0x100)
  1423. A = av_clip_uint8(A);
  1424. }
  1425. yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
  1426. dest += step;
  1427. }
  1428. c->dither_error[0][i] = err[0];
  1429. c->dither_error[1][i] = err[1];
  1430. c->dither_error[2][i] = err[2];
  1431. }
  1432. static av_always_inline void
  1433. yuv2rgb_full_2_c_template(SwsContext *c, const int16_t *buf[2],
  1434. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1435. const int16_t *abuf[2], uint8_t *dest, int dstW,
  1436. int yalpha, int uvalpha, int y,
  1437. enum AVPixelFormat target, int hasAlpha)
  1438. {
  1439. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  1440. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  1441. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
  1442. *abuf0 = hasAlpha ? abuf[0] : NULL,
  1443. *abuf1 = hasAlpha ? abuf[1] : NULL;
  1444. int yalpha1 = 4096 - yalpha;
  1445. int uvalpha1 = 4096 - uvalpha;
  1446. int i;
  1447. int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
  1448. int err[4] = {0};
  1449. int A = 0; // init to silcene warning
  1450. if( target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
  1451. || target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8)
  1452. step = 1;
  1453. for (i = 0; i < dstW; i++) {
  1454. int Y = ( buf0[i] * yalpha1 + buf1[i] * yalpha ) >> 10; //FIXME rounding
  1455. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha-(128 << 19)) >> 10;
  1456. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha-(128 << 19)) >> 10;
  1457. if (hasAlpha) {
  1458. A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha + (1<<18)) >> 19;
  1459. if (A & 0x100)
  1460. A = av_clip_uint8(A);
  1461. }
  1462. yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
  1463. dest += step;
  1464. }
  1465. c->dither_error[0][i] = err[0];
  1466. c->dither_error[1][i] = err[1];
  1467. c->dither_error[2][i] = err[2];
  1468. }
  1469. static av_always_inline void
  1470. yuv2rgb_full_1_c_template(SwsContext *c, const int16_t *buf0,
  1471. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1472. const int16_t *abuf0, uint8_t *dest, int dstW,
  1473. int uvalpha, int y, enum AVPixelFormat target,
  1474. int hasAlpha)
  1475. {
  1476. const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  1477. int i;
  1478. int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
  1479. int err[4] = {0};
  1480. if( target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
  1481. || target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8)
  1482. step = 1;
  1483. if (uvalpha < 2048) {
  1484. int A = 0; //init to silence warning
  1485. for (i = 0; i < dstW; i++) {
  1486. int Y = buf0[i] << 2;
  1487. int U = (ubuf0[i] - (128<<7)) << 2;
  1488. int V = (vbuf0[i] - (128<<7)) << 2;
  1489. if (hasAlpha) {
  1490. A = (abuf0[i] + 64) >> 7;
  1491. if (A & 0x100)
  1492. A = av_clip_uint8(A);
  1493. }
  1494. yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
  1495. dest += step;
  1496. }
  1497. } else {
  1498. const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  1499. int A = 0; //init to silence warning
  1500. for (i = 0; i < dstW; i++) {
  1501. int Y = buf0[i] << 2;
  1502. int U = (ubuf0[i] + ubuf1[i] - (128<<8)) << 1;
  1503. int V = (vbuf0[i] + vbuf1[i] - (128<<8)) << 1;
  1504. if (hasAlpha) {
  1505. A = (abuf0[i] + 64) >> 7;
  1506. if (A & 0x100)
  1507. A = av_clip_uint8(A);
  1508. }
  1509. yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
  1510. dest += step;
  1511. }
  1512. }
  1513. c->dither_error[0][i] = err[0];
  1514. c->dither_error[1][i] = err[1];
  1515. c->dither_error[2][i] = err[2];
  1516. }
  1517. #if CONFIG_SMALL
  1518. YUV2RGBWRAPPER(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1519. YUV2RGBWRAPPER(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1520. YUV2RGBWRAPPER(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1521. YUV2RGBWRAPPER(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1522. #else
  1523. #if CONFIG_SWSCALE_ALPHA
  1524. YUV2RGBWRAPPER(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA, 1)
  1525. YUV2RGBWRAPPER(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR, 1)
  1526. YUV2RGBWRAPPER(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA, 1)
  1527. YUV2RGBWRAPPER(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB, 1)
  1528. #endif
  1529. YUV2RGBWRAPPER(yuv2, rgb_full, bgrx32_full, AV_PIX_FMT_BGRA, 0)
  1530. YUV2RGBWRAPPER(yuv2, rgb_full, xbgr32_full, AV_PIX_FMT_ABGR, 0)
  1531. YUV2RGBWRAPPER(yuv2, rgb_full, rgbx32_full, AV_PIX_FMT_RGBA, 0)
  1532. YUV2RGBWRAPPER(yuv2, rgb_full, xrgb32_full, AV_PIX_FMT_ARGB, 0)
  1533. #endif
  1534. YUV2RGBWRAPPER(yuv2, rgb_full, bgr24_full, AV_PIX_FMT_BGR24, 0)
  1535. YUV2RGBWRAPPER(yuv2, rgb_full, rgb24_full, AV_PIX_FMT_RGB24, 0)
  1536. YUV2RGBWRAPPER(yuv2, rgb_full, bgr4_byte_full, AV_PIX_FMT_BGR4_BYTE, 0)
  1537. YUV2RGBWRAPPER(yuv2, rgb_full, rgb4_byte_full, AV_PIX_FMT_RGB4_BYTE, 0)
  1538. YUV2RGBWRAPPER(yuv2, rgb_full, bgr8_full, AV_PIX_FMT_BGR8, 0)
  1539. YUV2RGBWRAPPER(yuv2, rgb_full, rgb8_full, AV_PIX_FMT_RGB8, 0)
  1540. static void
  1541. yuv2gbrp_full_X_c(SwsContext *c, const int16_t *lumFilter,
  1542. const int16_t **lumSrc, int lumFilterSize,
  1543. const int16_t *chrFilter, const int16_t **chrUSrc,
  1544. const int16_t **chrVSrc, int chrFilterSize,
  1545. const int16_t **alpSrc, uint8_t **dest,
  1546. int dstW, int y)
  1547. {
  1548. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
  1549. int i;
  1550. int hasAlpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) && alpSrc;
  1551. uint16_t **dest16 = (uint16_t**)dest;
  1552. int SH = 22 + 7 - desc->comp[0].depth_minus1;
  1553. int A = 0; // init to silence warning
  1554. for (i = 0; i < dstW; i++) {
  1555. int j;
  1556. int Y = 1 << 9;
  1557. int U = (1 << 9) - (128 << 19);
  1558. int V = (1 << 9) - (128 << 19);
  1559. int R, G, B;
  1560. for (j = 0; j < lumFilterSize; j++)
  1561. Y += lumSrc[j][i] * lumFilter[j];
  1562. for (j = 0; j < chrFilterSize; j++) {
  1563. U += chrUSrc[j][i] * chrFilter[j];
  1564. V += chrVSrc[j][i] * chrFilter[j];
  1565. }
  1566. Y >>= 10;
  1567. U >>= 10;
  1568. V >>= 10;
  1569. if (hasAlpha) {
  1570. A = 1 << 18;
  1571. for (j = 0; j < lumFilterSize; j++)
  1572. A += alpSrc[j][i] * lumFilter[j];
  1573. A >>= 19;
  1574. if (A & 0x100)
  1575. A = av_clip_uint8(A);
  1576. }
  1577. Y -= c->yuv2rgb_y_offset;
  1578. Y *= c->yuv2rgb_y_coeff;
  1579. Y += 1 << 21;
  1580. R = Y + V * c->yuv2rgb_v2r_coeff;
  1581. G = Y + V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  1582. B = Y + U * c->yuv2rgb_u2b_coeff;
  1583. if ((R | G | B) & 0xC0000000) {
  1584. R = av_clip_uintp2(R, 30);
  1585. G = av_clip_uintp2(G, 30);
  1586. B = av_clip_uintp2(B, 30);
  1587. }
  1588. if (SH != 22) {
  1589. dest16[0][i] = G >> SH;
  1590. dest16[1][i] = B >> SH;
  1591. dest16[2][i] = R >> SH;
  1592. if (hasAlpha)
  1593. dest16[3][i] = A;
  1594. } else {
  1595. dest[0][i] = G >> 22;
  1596. dest[1][i] = B >> 22;
  1597. dest[2][i] = R >> 22;
  1598. if (hasAlpha)
  1599. dest[3][i] = A;
  1600. }
  1601. }
  1602. if (SH != 22 && (!isBE(c->dstFormat)) != (!HAVE_BIGENDIAN)) {
  1603. for (i = 0; i < dstW; i++) {
  1604. dest16[0][i] = av_bswap16(dest16[0][i]);
  1605. dest16[1][i] = av_bswap16(dest16[1][i]);
  1606. dest16[2][i] = av_bswap16(dest16[2][i]);
  1607. if (hasAlpha)
  1608. dest16[3][i] = av_bswap16(dest16[3][i]);
  1609. }
  1610. }
  1611. }
  1612. av_cold void ff_sws_init_output_funcs(SwsContext *c,
  1613. yuv2planar1_fn *yuv2plane1,
  1614. yuv2planarX_fn *yuv2planeX,
  1615. yuv2interleavedX_fn *yuv2nv12cX,
  1616. yuv2packed1_fn *yuv2packed1,
  1617. yuv2packed2_fn *yuv2packed2,
  1618. yuv2packedX_fn *yuv2packedX,
  1619. yuv2anyX_fn *yuv2anyX)
  1620. {
  1621. enum AVPixelFormat dstFormat = c->dstFormat;
  1622. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
  1623. if (is16BPS(dstFormat)) {
  1624. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c : yuv2planeX_16LE_c;
  1625. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c : yuv2plane1_16LE_c;
  1626. } else if (is9_OR_10BPS(dstFormat)) {
  1627. if (desc->comp[0].depth_minus1 == 8) {
  1628. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c : yuv2planeX_9LE_c;
  1629. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c : yuv2plane1_9LE_c;
  1630. } else if (desc->comp[0].depth_minus1 == 9) {
  1631. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c : yuv2planeX_10LE_c;
  1632. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c : yuv2plane1_10LE_c;
  1633. } else if (desc->comp[0].depth_minus1 == 11) {
  1634. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_12BE_c : yuv2planeX_12LE_c;
  1635. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_12BE_c : yuv2plane1_12LE_c;
  1636. } else if (desc->comp[0].depth_minus1 == 13) {
  1637. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_14BE_c : yuv2planeX_14LE_c;
  1638. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_14BE_c : yuv2plane1_14LE_c;
  1639. } else
  1640. av_assert0(0);
  1641. } else {
  1642. *yuv2plane1 = yuv2plane1_8_c;
  1643. *yuv2planeX = yuv2planeX_8_c;
  1644. if (dstFormat == AV_PIX_FMT_NV12 || dstFormat == AV_PIX_FMT_NV21)
  1645. *yuv2nv12cX = yuv2nv12cX_c;
  1646. }
  1647. if(c->flags & SWS_FULL_CHR_H_INT) {
  1648. switch (dstFormat) {
  1649. case AV_PIX_FMT_RGBA:
  1650. #if CONFIG_SMALL
  1651. *yuv2packedX = yuv2rgba32_full_X_c;
  1652. *yuv2packed2 = yuv2rgba32_full_2_c;
  1653. *yuv2packed1 = yuv2rgba32_full_1_c;
  1654. #else
  1655. #if CONFIG_SWSCALE_ALPHA
  1656. if (c->alpPixBuf) {
  1657. *yuv2packedX = yuv2rgba32_full_X_c;
  1658. *yuv2packed2 = yuv2rgba32_full_2_c;
  1659. *yuv2packed1 = yuv2rgba32_full_1_c;
  1660. } else
  1661. #endif /* CONFIG_SWSCALE_ALPHA */
  1662. {
  1663. *yuv2packedX = yuv2rgbx32_full_X_c;
  1664. *yuv2packed2 = yuv2rgbx32_full_2_c;
  1665. *yuv2packed1 = yuv2rgbx32_full_1_c;
  1666. }
  1667. #endif /* !CONFIG_SMALL */
  1668. break;
  1669. case AV_PIX_FMT_ARGB:
  1670. #if CONFIG_SMALL
  1671. *yuv2packedX = yuv2argb32_full_X_c;
  1672. *yuv2packed2 = yuv2argb32_full_2_c;
  1673. *yuv2packed1 = yuv2argb32_full_1_c;
  1674. #else
  1675. #if CONFIG_SWSCALE_ALPHA
  1676. if (c->alpPixBuf) {
  1677. *yuv2packedX = yuv2argb32_full_X_c;
  1678. *yuv2packed2 = yuv2argb32_full_2_c;
  1679. *yuv2packed1 = yuv2argb32_full_1_c;
  1680. } else
  1681. #endif /* CONFIG_SWSCALE_ALPHA */
  1682. {
  1683. *yuv2packedX = yuv2xrgb32_full_X_c;
  1684. *yuv2packed2 = yuv2xrgb32_full_2_c;
  1685. *yuv2packed1 = yuv2xrgb32_full_1_c;
  1686. }
  1687. #endif /* !CONFIG_SMALL */
  1688. break;
  1689. case AV_PIX_FMT_BGRA:
  1690. #if CONFIG_SMALL
  1691. *yuv2packedX = yuv2bgra32_full_X_c;
  1692. *yuv2packed2 = yuv2bgra32_full_2_c;
  1693. *yuv2packed1 = yuv2bgra32_full_1_c;
  1694. #else
  1695. #if CONFIG_SWSCALE_ALPHA
  1696. if (c->alpPixBuf) {
  1697. *yuv2packedX = yuv2bgra32_full_X_c;
  1698. *yuv2packed2 = yuv2bgra32_full_2_c;
  1699. *yuv2packed1 = yuv2bgra32_full_1_c;
  1700. } else
  1701. #endif /* CONFIG_SWSCALE_ALPHA */
  1702. {
  1703. *yuv2packedX = yuv2bgrx32_full_X_c;
  1704. *yuv2packed2 = yuv2bgrx32_full_2_c;
  1705. *yuv2packed1 = yuv2bgrx32_full_1_c;
  1706. }
  1707. #endif /* !CONFIG_SMALL */
  1708. break;
  1709. case AV_PIX_FMT_ABGR:
  1710. #if CONFIG_SMALL
  1711. *yuv2packedX = yuv2abgr32_full_X_c;
  1712. *yuv2packed2 = yuv2abgr32_full_2_c;
  1713. *yuv2packed1 = yuv2abgr32_full_1_c;
  1714. #else
  1715. #if CONFIG_SWSCALE_ALPHA
  1716. if (c->alpPixBuf) {
  1717. *yuv2packedX = yuv2abgr32_full_X_c;
  1718. *yuv2packed2 = yuv2abgr32_full_2_c;
  1719. *yuv2packed1 = yuv2abgr32_full_1_c;
  1720. } else
  1721. #endif /* CONFIG_SWSCALE_ALPHA */
  1722. {
  1723. *yuv2packedX = yuv2xbgr32_full_X_c;
  1724. *yuv2packed2 = yuv2xbgr32_full_2_c;
  1725. *yuv2packed1 = yuv2xbgr32_full_1_c;
  1726. }
  1727. #endif /* !CONFIG_SMALL */
  1728. break;
  1729. case AV_PIX_FMT_RGB24:
  1730. *yuv2packedX = yuv2rgb24_full_X_c;
  1731. *yuv2packed2 = yuv2rgb24_full_2_c;
  1732. *yuv2packed1 = yuv2rgb24_full_1_c;
  1733. break;
  1734. case AV_PIX_FMT_BGR24:
  1735. *yuv2packedX = yuv2bgr24_full_X_c;
  1736. *yuv2packed2 = yuv2bgr24_full_2_c;
  1737. *yuv2packed1 = yuv2bgr24_full_1_c;
  1738. break;
  1739. case AV_PIX_FMT_BGR4_BYTE:
  1740. *yuv2packedX = yuv2bgr4_byte_full_X_c;
  1741. *yuv2packed2 = yuv2bgr4_byte_full_2_c;
  1742. *yuv2packed1 = yuv2bgr4_byte_full_1_c;
  1743. break;
  1744. case AV_PIX_FMT_RGB4_BYTE:
  1745. *yuv2packedX = yuv2rgb4_byte_full_X_c;
  1746. *yuv2packed2 = yuv2rgb4_byte_full_2_c;
  1747. *yuv2packed1 = yuv2rgb4_byte_full_1_c;
  1748. break;
  1749. case AV_PIX_FMT_BGR8:
  1750. *yuv2packedX = yuv2bgr8_full_X_c;
  1751. *yuv2packed2 = yuv2bgr8_full_2_c;
  1752. *yuv2packed1 = yuv2bgr8_full_1_c;
  1753. break;
  1754. case AV_PIX_FMT_RGB8:
  1755. *yuv2packedX = yuv2rgb8_full_X_c;
  1756. *yuv2packed2 = yuv2rgb8_full_2_c;
  1757. *yuv2packed1 = yuv2rgb8_full_1_c;
  1758. break;
  1759. case AV_PIX_FMT_GBRP:
  1760. case AV_PIX_FMT_GBRP9BE:
  1761. case AV_PIX_FMT_GBRP9LE:
  1762. case AV_PIX_FMT_GBRP10BE:
  1763. case AV_PIX_FMT_GBRP10LE:
  1764. case AV_PIX_FMT_GBRP12BE:
  1765. case AV_PIX_FMT_GBRP12LE:
  1766. case AV_PIX_FMT_GBRP14BE:
  1767. case AV_PIX_FMT_GBRP14LE:
  1768. case AV_PIX_FMT_GBRP16BE:
  1769. case AV_PIX_FMT_GBRP16LE:
  1770. case AV_PIX_FMT_GBRAP:
  1771. *yuv2anyX = yuv2gbrp_full_X_c;
  1772. break;
  1773. }
  1774. if (!*yuv2packedX && !*yuv2anyX)
  1775. goto YUV_PACKED;
  1776. } else {
  1777. YUV_PACKED:
  1778. switch (dstFormat) {
  1779. case AV_PIX_FMT_RGBA64LE:
  1780. #if CONFIG_SWSCALE_ALPHA
  1781. if (c->alpPixBuf) {
  1782. *yuv2packed1 = yuv2rgba64le_1_c;
  1783. *yuv2packed2 = yuv2rgba64le_2_c;
  1784. *yuv2packedX = yuv2rgba64le_X_c;
  1785. } else
  1786. #endif /* CONFIG_SWSCALE_ALPHA */
  1787. {
  1788. *yuv2packed1 = yuv2rgbx64le_1_c;
  1789. *yuv2packed2 = yuv2rgbx64le_2_c;
  1790. *yuv2packedX = yuv2rgbx64le_X_c;
  1791. }
  1792. break;
  1793. case AV_PIX_FMT_RGBA64BE:
  1794. #if CONFIG_SWSCALE_ALPHA
  1795. if (c->alpPixBuf) {
  1796. *yuv2packed1 = yuv2rgba64be_1_c;
  1797. *yuv2packed2 = yuv2rgba64be_2_c;
  1798. *yuv2packedX = yuv2rgba64be_X_c;
  1799. } else
  1800. #endif /* CONFIG_SWSCALE_ALPHA */
  1801. {
  1802. *yuv2packed1 = yuv2rgbx64be_1_c;
  1803. *yuv2packed2 = yuv2rgbx64be_2_c;
  1804. *yuv2packedX = yuv2rgbx64be_X_c;
  1805. }
  1806. break;
  1807. case AV_PIX_FMT_RGB48LE:
  1808. *yuv2packed1 = yuv2rgb48le_1_c;
  1809. *yuv2packed2 = yuv2rgb48le_2_c;
  1810. *yuv2packedX = yuv2rgb48le_X_c;
  1811. break;
  1812. case AV_PIX_FMT_RGB48BE:
  1813. *yuv2packed1 = yuv2rgb48be_1_c;
  1814. *yuv2packed2 = yuv2rgb48be_2_c;
  1815. *yuv2packedX = yuv2rgb48be_X_c;
  1816. break;
  1817. case AV_PIX_FMT_BGR48LE:
  1818. *yuv2packed1 = yuv2bgr48le_1_c;
  1819. *yuv2packed2 = yuv2bgr48le_2_c;
  1820. *yuv2packedX = yuv2bgr48le_X_c;
  1821. break;
  1822. case AV_PIX_FMT_BGR48BE:
  1823. *yuv2packed1 = yuv2bgr48be_1_c;
  1824. *yuv2packed2 = yuv2bgr48be_2_c;
  1825. *yuv2packedX = yuv2bgr48be_X_c;
  1826. break;
  1827. case AV_PIX_FMT_RGB32:
  1828. case AV_PIX_FMT_BGR32:
  1829. #if CONFIG_SMALL
  1830. *yuv2packed1 = yuv2rgb32_1_c;
  1831. *yuv2packed2 = yuv2rgb32_2_c;
  1832. *yuv2packedX = yuv2rgb32_X_c;
  1833. #else
  1834. #if CONFIG_SWSCALE_ALPHA
  1835. if (c->alpPixBuf) {
  1836. *yuv2packed1 = yuv2rgba32_1_c;
  1837. *yuv2packed2 = yuv2rgba32_2_c;
  1838. *yuv2packedX = yuv2rgba32_X_c;
  1839. } else
  1840. #endif /* CONFIG_SWSCALE_ALPHA */
  1841. {
  1842. *yuv2packed1 = yuv2rgbx32_1_c;
  1843. *yuv2packed2 = yuv2rgbx32_2_c;
  1844. *yuv2packedX = yuv2rgbx32_X_c;
  1845. }
  1846. #endif /* !CONFIG_SMALL */
  1847. break;
  1848. case AV_PIX_FMT_RGB32_1:
  1849. case AV_PIX_FMT_BGR32_1:
  1850. #if CONFIG_SMALL
  1851. *yuv2packed1 = yuv2rgb32_1_1_c;
  1852. *yuv2packed2 = yuv2rgb32_1_2_c;
  1853. *yuv2packedX = yuv2rgb32_1_X_c;
  1854. #else
  1855. #if CONFIG_SWSCALE_ALPHA
  1856. if (c->alpPixBuf) {
  1857. *yuv2packed1 = yuv2rgba32_1_1_c;
  1858. *yuv2packed2 = yuv2rgba32_1_2_c;
  1859. *yuv2packedX = yuv2rgba32_1_X_c;
  1860. } else
  1861. #endif /* CONFIG_SWSCALE_ALPHA */
  1862. {
  1863. *yuv2packed1 = yuv2rgbx32_1_1_c;
  1864. *yuv2packed2 = yuv2rgbx32_1_2_c;
  1865. *yuv2packedX = yuv2rgbx32_1_X_c;
  1866. }
  1867. #endif /* !CONFIG_SMALL */
  1868. break;
  1869. case AV_PIX_FMT_RGB24:
  1870. *yuv2packed1 = yuv2rgb24_1_c;
  1871. *yuv2packed2 = yuv2rgb24_2_c;
  1872. *yuv2packedX = yuv2rgb24_X_c;
  1873. break;
  1874. case AV_PIX_FMT_BGR24:
  1875. *yuv2packed1 = yuv2bgr24_1_c;
  1876. *yuv2packed2 = yuv2bgr24_2_c;
  1877. *yuv2packedX = yuv2bgr24_X_c;
  1878. break;
  1879. case AV_PIX_FMT_RGB565LE:
  1880. case AV_PIX_FMT_RGB565BE:
  1881. case AV_PIX_FMT_BGR565LE:
  1882. case AV_PIX_FMT_BGR565BE:
  1883. *yuv2packed1 = yuv2rgb16_1_c;
  1884. *yuv2packed2 = yuv2rgb16_2_c;
  1885. *yuv2packedX = yuv2rgb16_X_c;
  1886. break;
  1887. case AV_PIX_FMT_RGB555LE:
  1888. case AV_PIX_FMT_RGB555BE:
  1889. case AV_PIX_FMT_BGR555LE:
  1890. case AV_PIX_FMT_BGR555BE:
  1891. *yuv2packed1 = yuv2rgb15_1_c;
  1892. *yuv2packed2 = yuv2rgb15_2_c;
  1893. *yuv2packedX = yuv2rgb15_X_c;
  1894. break;
  1895. case AV_PIX_FMT_RGB444LE:
  1896. case AV_PIX_FMT_RGB444BE:
  1897. case AV_PIX_FMT_BGR444LE:
  1898. case AV_PIX_FMT_BGR444BE:
  1899. *yuv2packed1 = yuv2rgb12_1_c;
  1900. *yuv2packed2 = yuv2rgb12_2_c;
  1901. *yuv2packedX = yuv2rgb12_X_c;
  1902. break;
  1903. case AV_PIX_FMT_RGB8:
  1904. case AV_PIX_FMT_BGR8:
  1905. *yuv2packed1 = yuv2rgb8_1_c;
  1906. *yuv2packed2 = yuv2rgb8_2_c;
  1907. *yuv2packedX = yuv2rgb8_X_c;
  1908. break;
  1909. case AV_PIX_FMT_RGB4:
  1910. case AV_PIX_FMT_BGR4:
  1911. *yuv2packed1 = yuv2rgb4_1_c;
  1912. *yuv2packed2 = yuv2rgb4_2_c;
  1913. *yuv2packedX = yuv2rgb4_X_c;
  1914. break;
  1915. case AV_PIX_FMT_RGB4_BYTE:
  1916. case AV_PIX_FMT_BGR4_BYTE:
  1917. *yuv2packed1 = yuv2rgb4b_1_c;
  1918. *yuv2packed2 = yuv2rgb4b_2_c;
  1919. *yuv2packedX = yuv2rgb4b_X_c;
  1920. break;
  1921. }
  1922. }
  1923. switch (dstFormat) {
  1924. case AV_PIX_FMT_MONOWHITE:
  1925. *yuv2packed1 = yuv2monowhite_1_c;
  1926. *yuv2packed2 = yuv2monowhite_2_c;
  1927. *yuv2packedX = yuv2monowhite_X_c;
  1928. break;
  1929. case AV_PIX_FMT_MONOBLACK:
  1930. *yuv2packed1 = yuv2monoblack_1_c;
  1931. *yuv2packed2 = yuv2monoblack_2_c;
  1932. *yuv2packedX = yuv2monoblack_X_c;
  1933. break;
  1934. case AV_PIX_FMT_YUYV422:
  1935. *yuv2packed1 = yuv2yuyv422_1_c;
  1936. *yuv2packed2 = yuv2yuyv422_2_c;
  1937. *yuv2packedX = yuv2yuyv422_X_c;
  1938. break;
  1939. case AV_PIX_FMT_UYVY422:
  1940. *yuv2packed1 = yuv2uyvy422_1_c;
  1941. *yuv2packed2 = yuv2uyvy422_2_c;
  1942. *yuv2packedX = yuv2uyvy422_X_c;
  1943. break;
  1944. }
  1945. }