output.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  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. yuv2rgb48_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)
  609. {
  610. int i;
  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. // 8bit: 12+15=27; 16-bit: 12+19=31
  627. Y1 >>= 14; // 10
  628. Y1 += 0x10000;
  629. Y2 >>= 14;
  630. Y2 += 0x10000;
  631. U >>= 14;
  632. V >>= 14;
  633. // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
  634. Y1 -= c->yuv2rgb_y_offset;
  635. Y2 -= c->yuv2rgb_y_offset;
  636. Y1 *= c->yuv2rgb_y_coeff;
  637. Y2 *= c->yuv2rgb_y_coeff;
  638. Y1 += 1 << 13; // 21
  639. Y2 += 1 << 13;
  640. // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
  641. R = V * c->yuv2rgb_v2r_coeff;
  642. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  643. B = U * c->yuv2rgb_u2b_coeff;
  644. // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
  645. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  646. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  647. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  648. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  649. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  650. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  651. dest += 6;
  652. }
  653. }
  654. static av_always_inline void
  655. yuv2rgb48_2_c_template(SwsContext *c, const int32_t *buf[2],
  656. const int32_t *ubuf[2], const int32_t *vbuf[2],
  657. const int32_t *abuf[2], uint16_t *dest, int dstW,
  658. int yalpha, int uvalpha, int y,
  659. enum AVPixelFormat target)
  660. {
  661. const int32_t *buf0 = buf[0], *buf1 = buf[1],
  662. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  663. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  664. int yalpha1 = 4096 - yalpha;
  665. int uvalpha1 = 4096 - uvalpha;
  666. int i;
  667. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  668. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 14;
  669. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 14;
  670. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha + (-128 << 23)) >> 14;
  671. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha + (-128 << 23)) >> 14;
  672. int R, G, B;
  673. Y1 -= c->yuv2rgb_y_offset;
  674. Y2 -= c->yuv2rgb_y_offset;
  675. Y1 *= c->yuv2rgb_y_coeff;
  676. Y2 *= c->yuv2rgb_y_coeff;
  677. Y1 += 1 << 13;
  678. Y2 += 1 << 13;
  679. R = V * c->yuv2rgb_v2r_coeff;
  680. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  681. B = U * c->yuv2rgb_u2b_coeff;
  682. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  683. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  684. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  685. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  686. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  687. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  688. dest += 6;
  689. }
  690. }
  691. static av_always_inline void
  692. yuv2rgb48_1_c_template(SwsContext *c, const int32_t *buf0,
  693. const int32_t *ubuf[2], const int32_t *vbuf[2],
  694. const int32_t *abuf0, uint16_t *dest, int dstW,
  695. int uvalpha, int y, enum AVPixelFormat target)
  696. {
  697. const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  698. int i;
  699. if (uvalpha < 2048) {
  700. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  701. int Y1 = (buf0[i * 2] ) >> 2;
  702. int Y2 = (buf0[i * 2 + 1]) >> 2;
  703. int U = (ubuf0[i] + (-128 << 11)) >> 2;
  704. int V = (vbuf0[i] + (-128 << 11)) >> 2;
  705. int R, G, B;
  706. Y1 -= c->yuv2rgb_y_offset;
  707. Y2 -= c->yuv2rgb_y_offset;
  708. Y1 *= c->yuv2rgb_y_coeff;
  709. Y2 *= c->yuv2rgb_y_coeff;
  710. Y1 += 1 << 13;
  711. Y2 += 1 << 13;
  712. R = V * c->yuv2rgb_v2r_coeff;
  713. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  714. B = U * c->yuv2rgb_u2b_coeff;
  715. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  716. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  717. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  718. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  719. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  720. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  721. dest += 6;
  722. }
  723. } else {
  724. const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  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] + ubuf1[i] + (-128 << 12)) >> 3;
  729. int V = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
  730. int R, G, B;
  731. Y1 -= c->yuv2rgb_y_offset;
  732. Y2 -= c->yuv2rgb_y_offset;
  733. Y1 *= c->yuv2rgb_y_coeff;
  734. Y2 *= c->yuv2rgb_y_coeff;
  735. Y1 += 1 << 13;
  736. Y2 += 1 << 13;
  737. R = V * c->yuv2rgb_v2r_coeff;
  738. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  739. B = U * c->yuv2rgb_u2b_coeff;
  740. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  741. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  742. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  743. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  744. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  745. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  746. dest += 6;
  747. }
  748. }
  749. }
  750. #undef output_pixel
  751. #undef r_b
  752. #undef b_r
  753. #define YUV2PACKED16WRAPPER(name, base, ext, fmt) \
  754. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  755. const int16_t **_lumSrc, int lumFilterSize, \
  756. const int16_t *chrFilter, const int16_t **_chrUSrc, \
  757. const int16_t **_chrVSrc, int chrFilterSize, \
  758. const int16_t **_alpSrc, uint8_t *_dest, int dstW, \
  759. int y) \
  760. { \
  761. const int32_t **lumSrc = (const int32_t **) _lumSrc, \
  762. **chrUSrc = (const int32_t **) _chrUSrc, \
  763. **chrVSrc = (const int32_t **) _chrVSrc, \
  764. **alpSrc = (const int32_t **) _alpSrc; \
  765. uint16_t *dest = (uint16_t *) _dest; \
  766. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  767. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  768. alpSrc, dest, dstW, y, fmt); \
  769. } \
  770. \
  771. static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
  772. const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
  773. const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
  774. int yalpha, int uvalpha, int y) \
  775. { \
  776. const int32_t **buf = (const int32_t **) _buf, \
  777. **ubuf = (const int32_t **) _ubuf, \
  778. **vbuf = (const int32_t **) _vbuf, \
  779. **abuf = (const int32_t **) _abuf; \
  780. uint16_t *dest = (uint16_t *) _dest; \
  781. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  782. dest, dstW, yalpha, uvalpha, y, fmt); \
  783. } \
  784. \
  785. static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
  786. const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
  787. const int16_t *_abuf0, uint8_t *_dest, int dstW, \
  788. int uvalpha, int y) \
  789. { \
  790. const int32_t *buf0 = (const int32_t *) _buf0, \
  791. **ubuf = (const int32_t **) _ubuf, \
  792. **vbuf = (const int32_t **) _vbuf, \
  793. *abuf0 = (const int32_t *) _abuf0; \
  794. uint16_t *dest = (uint16_t *) _dest; \
  795. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
  796. dstW, uvalpha, y, fmt); \
  797. }
  798. YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48be, AV_PIX_FMT_RGB48BE)
  799. YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48le, AV_PIX_FMT_RGB48LE)
  800. YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48be, AV_PIX_FMT_BGR48BE)
  801. YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48le, AV_PIX_FMT_BGR48LE)
  802. /*
  803. * Write out 2 RGB pixels in the target pixel format. This function takes a
  804. * R/G/B LUT as generated by ff_yuv2rgb_c_init_tables(), which takes care of
  805. * things like endianness conversion and shifting. The caller takes care of
  806. * setting the correct offset in these tables from the chroma (U/V) values.
  807. * This function then uses the luminance (Y1/Y2) values to write out the
  808. * correct RGB values into the destination buffer.
  809. */
  810. static av_always_inline void
  811. yuv2rgb_write(uint8_t *_dest, int i, int Y1, int Y2,
  812. unsigned A1, unsigned A2,
  813. const void *_r, const void *_g, const void *_b, int y,
  814. enum AVPixelFormat target, int hasAlpha)
  815. {
  816. if (target == AV_PIX_FMT_ARGB || target == AV_PIX_FMT_RGBA ||
  817. target == AV_PIX_FMT_ABGR || target == AV_PIX_FMT_BGRA) {
  818. uint32_t *dest = (uint32_t *) _dest;
  819. const uint32_t *r = (const uint32_t *) _r;
  820. const uint32_t *g = (const uint32_t *) _g;
  821. const uint32_t *b = (const uint32_t *) _b;
  822. #if CONFIG_SMALL
  823. int sh = hasAlpha ? ((target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24) : 0;
  824. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (hasAlpha ? A1 << sh : 0);
  825. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (hasAlpha ? A2 << sh : 0);
  826. #else
  827. if (hasAlpha) {
  828. int sh = (target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24;
  829. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
  830. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
  831. } else {
  832. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
  833. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
  834. }
  835. #endif
  836. } else if (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) {
  837. uint8_t *dest = (uint8_t *) _dest;
  838. const uint8_t *r = (const uint8_t *) _r;
  839. const uint8_t *g = (const uint8_t *) _g;
  840. const uint8_t *b = (const uint8_t *) _b;
  841. #define r_b ((target == AV_PIX_FMT_RGB24) ? r : b)
  842. #define b_r ((target == AV_PIX_FMT_RGB24) ? b : r)
  843. dest[i * 6 + 0] = r_b[Y1];
  844. dest[i * 6 + 1] = g[Y1];
  845. dest[i * 6 + 2] = b_r[Y1];
  846. dest[i * 6 + 3] = r_b[Y2];
  847. dest[i * 6 + 4] = g[Y2];
  848. dest[i * 6 + 5] = b_r[Y2];
  849. #undef r_b
  850. #undef b_r
  851. } else if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565 ||
  852. target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555 ||
  853. target == AV_PIX_FMT_RGB444 || target == AV_PIX_FMT_BGR444) {
  854. uint16_t *dest = (uint16_t *) _dest;
  855. const uint16_t *r = (const uint16_t *) _r;
  856. const uint16_t *g = (const uint16_t *) _g;
  857. const uint16_t *b = (const uint16_t *) _b;
  858. int dr1, dg1, db1, dr2, dg2, db2;
  859. if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565) {
  860. dr1 = dither_2x2_8[ y & 1 ][0];
  861. dg1 = dither_2x2_4[ y & 1 ][0];
  862. db1 = dither_2x2_8[(y & 1) ^ 1][0];
  863. dr2 = dither_2x2_8[ y & 1 ][1];
  864. dg2 = dither_2x2_4[ y & 1 ][1];
  865. db2 = dither_2x2_8[(y & 1) ^ 1][1];
  866. } else if (target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555) {
  867. dr1 = dither_2x2_8[ y & 1 ][0];
  868. dg1 = dither_2x2_8[ y & 1 ][1];
  869. db1 = dither_2x2_8[(y & 1) ^ 1][0];
  870. dr2 = dither_2x2_8[ y & 1 ][1];
  871. dg2 = dither_2x2_8[ y & 1 ][0];
  872. db2 = dither_2x2_8[(y & 1) ^ 1][1];
  873. } else {
  874. dr1 = dither_4x4_16[ y & 3 ][0];
  875. dg1 = dither_4x4_16[ y & 3 ][1];
  876. db1 = dither_4x4_16[(y & 3) ^ 3][0];
  877. dr2 = dither_4x4_16[ y & 3 ][1];
  878. dg2 = dither_4x4_16[ y & 3 ][0];
  879. db2 = dither_4x4_16[(y & 3) ^ 3][1];
  880. }
  881. dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
  882. dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
  883. } else /* 8/4-bit */ {
  884. uint8_t *dest = (uint8_t *) _dest;
  885. const uint8_t *r = (const uint8_t *) _r;
  886. const uint8_t *g = (const uint8_t *) _g;
  887. const uint8_t *b = (const uint8_t *) _b;
  888. int dr1, dg1, db1, dr2, dg2, db2;
  889. if (target == AV_PIX_FMT_RGB8 || target == AV_PIX_FMT_BGR8) {
  890. const uint8_t * const d64 = dither_8x8_73[y & 7];
  891. const uint8_t * const d32 = dither_8x8_32[y & 7];
  892. dr1 = dg1 = d32[(i * 2 + 0) & 7];
  893. db1 = d64[(i * 2 + 0) & 7];
  894. dr2 = dg2 = d32[(i * 2 + 1) & 7];
  895. db2 = d64[(i * 2 + 1) & 7];
  896. } else {
  897. const uint8_t * const d64 = dither_8x8_73 [y & 7];
  898. const uint8_t * const d128 = dither_8x8_220[y & 7];
  899. dr1 = db1 = d128[(i * 2 + 0) & 7];
  900. dg1 = d64[(i * 2 + 0) & 7];
  901. dr2 = db2 = d128[(i * 2 + 1) & 7];
  902. dg2 = d64[(i * 2 + 1) & 7];
  903. }
  904. if (target == AV_PIX_FMT_RGB4 || target == AV_PIX_FMT_BGR4) {
  905. dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
  906. ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
  907. } else {
  908. dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
  909. dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
  910. }
  911. }
  912. }
  913. static av_always_inline void
  914. yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
  915. const int16_t **lumSrc, int lumFilterSize,
  916. const int16_t *chrFilter, const int16_t **chrUSrc,
  917. const int16_t **chrVSrc, int chrFilterSize,
  918. const int16_t **alpSrc, uint8_t *dest, int dstW,
  919. int y, enum AVPixelFormat target, int hasAlpha)
  920. {
  921. int i;
  922. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  923. int j, A1, A2;
  924. int Y1 = 1 << 18;
  925. int Y2 = 1 << 18;
  926. int U = 1 << 18;
  927. int V = 1 << 18;
  928. const void *r, *g, *b;
  929. for (j = 0; j < lumFilterSize; j++) {
  930. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  931. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  932. }
  933. for (j = 0; j < chrFilterSize; j++) {
  934. U += chrUSrc[j][i] * chrFilter[j];
  935. V += chrVSrc[j][i] * chrFilter[j];
  936. }
  937. Y1 >>= 19;
  938. Y2 >>= 19;
  939. U >>= 19;
  940. V >>= 19;
  941. if (hasAlpha) {
  942. A1 = 1 << 18;
  943. A2 = 1 << 18;
  944. for (j = 0; j < lumFilterSize; j++) {
  945. A1 += alpSrc[j][i * 2 ] * lumFilter[j];
  946. A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
  947. }
  948. A1 >>= 19;
  949. A2 >>= 19;
  950. if ((A1 | A2) & 0x100) {
  951. A1 = av_clip_uint8(A1);
  952. A2 = av_clip_uint8(A2);
  953. }
  954. }
  955. r = c->table_rV[V + YUVRGB_TABLE_HEADROOM];
  956. g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]);
  957. b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  958. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  959. r, g, b, y, target, hasAlpha);
  960. }
  961. }
  962. static av_always_inline void
  963. yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
  964. const int16_t *ubuf[2], const int16_t *vbuf[2],
  965. const int16_t *abuf[2], uint8_t *dest, int dstW,
  966. int yalpha, int uvalpha, int y,
  967. enum AVPixelFormat target, int hasAlpha)
  968. {
  969. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  970. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  971. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
  972. *abuf0 = hasAlpha ? abuf[0] : NULL,
  973. *abuf1 = hasAlpha ? abuf[1] : NULL;
  974. int yalpha1 = 4096 - yalpha;
  975. int uvalpha1 = 4096 - uvalpha;
  976. int i;
  977. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  978. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
  979. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
  980. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
  981. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
  982. int A1, A2;
  983. const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
  984. *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
  985. *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  986. if (hasAlpha) {
  987. A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 19;
  988. A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
  989. A1 = av_clip_uint8(A1);
  990. A2 = av_clip_uint8(A2);
  991. }
  992. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  993. r, g, b, y, target, hasAlpha);
  994. }
  995. }
  996. static av_always_inline void
  997. yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
  998. const int16_t *ubuf[2], const int16_t *vbuf[2],
  999. const int16_t *abuf0, uint8_t *dest, int dstW,
  1000. int uvalpha, int y, enum AVPixelFormat target,
  1001. int hasAlpha)
  1002. {
  1003. const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  1004. int i;
  1005. if (uvalpha < 2048) {
  1006. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1007. int Y1 = (buf0[i * 2 ] + 64) >> 7;
  1008. int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
  1009. int U = (ubuf0[i] + 64) >> 7;
  1010. int V = (vbuf0[i] + 64) >> 7;
  1011. int A1, A2;
  1012. const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
  1013. *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
  1014. *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  1015. if (hasAlpha) {
  1016. A1 = abuf0[i * 2 ] * 255 + 16384 >> 15;
  1017. A2 = abuf0[i * 2 + 1] * 255 + 16384 >> 15;
  1018. A1 = av_clip_uint8(A1);
  1019. A2 = av_clip_uint8(A2);
  1020. }
  1021. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1022. r, g, b, y, target, hasAlpha);
  1023. }
  1024. } else {
  1025. const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  1026. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1027. int Y1 = (buf0[i * 2 ] + 64) >> 7;
  1028. int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
  1029. int U = (ubuf0[i] + ubuf1[i] + 128) >> 8;
  1030. int V = (vbuf0[i] + vbuf1[i] + 128) >> 8;
  1031. int A1, A2;
  1032. const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
  1033. *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
  1034. *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  1035. if (hasAlpha) {
  1036. A1 = (abuf0[i * 2 ] + 64) >> 7;
  1037. A2 = (abuf0[i * 2 + 1] + 64) >> 7;
  1038. A1 = av_clip_uint8(A1);
  1039. A2 = av_clip_uint8(A2);
  1040. }
  1041. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1042. r, g, b, y, target, hasAlpha);
  1043. }
  1044. }
  1045. }
  1046. #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
  1047. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  1048. const int16_t **lumSrc, int lumFilterSize, \
  1049. const int16_t *chrFilter, const int16_t **chrUSrc, \
  1050. const int16_t **chrVSrc, int chrFilterSize, \
  1051. const int16_t **alpSrc, uint8_t *dest, int dstW, \
  1052. int y) \
  1053. { \
  1054. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  1055. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  1056. alpSrc, dest, dstW, y, fmt, hasAlpha); \
  1057. }
  1058. #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
  1059. YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
  1060. static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
  1061. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  1062. const int16_t *abuf[2], uint8_t *dest, int dstW, \
  1063. int yalpha, int uvalpha, int y) \
  1064. { \
  1065. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  1066. dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
  1067. } \
  1068. \
  1069. static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
  1070. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  1071. const int16_t *abuf0, uint8_t *dest, int dstW, \
  1072. int uvalpha, int y) \
  1073. { \
  1074. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
  1075. dstW, uvalpha, y, fmt, hasAlpha); \
  1076. }
  1077. #if CONFIG_SMALL
  1078. YUV2RGBWRAPPER(yuv2rgb,, 32_1, AV_PIX_FMT_RGB32_1, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1079. YUV2RGBWRAPPER(yuv2rgb,, 32, AV_PIX_FMT_RGB32, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1080. #else
  1081. #if CONFIG_SWSCALE_ALPHA
  1082. YUV2RGBWRAPPER(yuv2rgb,, a32_1, AV_PIX_FMT_RGB32_1, 1)
  1083. YUV2RGBWRAPPER(yuv2rgb,, a32, AV_PIX_FMT_RGB32, 1)
  1084. #endif
  1085. YUV2RGBWRAPPER(yuv2rgb,, x32_1, AV_PIX_FMT_RGB32_1, 0)
  1086. YUV2RGBWRAPPER(yuv2rgb,, x32, AV_PIX_FMT_RGB32, 0)
  1087. #endif
  1088. YUV2RGBWRAPPER(yuv2, rgb, rgb24, AV_PIX_FMT_RGB24, 0)
  1089. YUV2RGBWRAPPER(yuv2, rgb, bgr24, AV_PIX_FMT_BGR24, 0)
  1090. YUV2RGBWRAPPER(yuv2rgb,, 16, AV_PIX_FMT_RGB565, 0)
  1091. YUV2RGBWRAPPER(yuv2rgb,, 15, AV_PIX_FMT_RGB555, 0)
  1092. YUV2RGBWRAPPER(yuv2rgb,, 12, AV_PIX_FMT_RGB444, 0)
  1093. YUV2RGBWRAPPER(yuv2rgb,, 8, AV_PIX_FMT_RGB8, 0)
  1094. YUV2RGBWRAPPER(yuv2rgb,, 4, AV_PIX_FMT_RGB4, 0)
  1095. YUV2RGBWRAPPER(yuv2rgb,, 4b, AV_PIX_FMT_RGB4_BYTE, 0)
  1096. static av_always_inline void
  1097. yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
  1098. const int16_t **lumSrc, int lumFilterSize,
  1099. const int16_t *chrFilter, const int16_t **chrUSrc,
  1100. const int16_t **chrVSrc, int chrFilterSize,
  1101. const int16_t **alpSrc, uint8_t *dest,
  1102. int dstW, int y, enum AVPixelFormat target, int hasAlpha)
  1103. {
  1104. int i;
  1105. int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
  1106. int err[4] = {0};
  1107. int isrgb8 = target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8;
  1108. for (i = 0; i < dstW; i++) {
  1109. int j;
  1110. int Y = 1<<9;
  1111. int U = (1<<9)-(128 << 19);
  1112. int V = (1<<9)-(128 << 19);
  1113. int R, G, B, A;
  1114. for (j = 0; j < lumFilterSize; j++) {
  1115. Y += lumSrc[j][i] * lumFilter[j];
  1116. }
  1117. for (j = 0; j < chrFilterSize; j++) {
  1118. U += chrUSrc[j][i] * chrFilter[j];
  1119. V += chrVSrc[j][i] * chrFilter[j];
  1120. }
  1121. Y >>= 10;
  1122. U >>= 10;
  1123. V >>= 10;
  1124. if (hasAlpha) {
  1125. A = 1 << 18;
  1126. for (j = 0; j < lumFilterSize; j++) {
  1127. A += alpSrc[j][i] * lumFilter[j];
  1128. }
  1129. A >>= 19;
  1130. if (A & 0x100)
  1131. A = av_clip_uint8(A);
  1132. }
  1133. Y -= c->yuv2rgb_y_offset;
  1134. Y *= c->yuv2rgb_y_coeff;
  1135. Y += 1 << 21;
  1136. R = Y + V*c->yuv2rgb_v2r_coeff;
  1137. G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
  1138. B = Y + U*c->yuv2rgb_u2b_coeff;
  1139. if ((R | G | B) & 0xC0000000) {
  1140. R = av_clip_uintp2(R, 30);
  1141. G = av_clip_uintp2(G, 30);
  1142. B = av_clip_uintp2(B, 30);
  1143. }
  1144. switch(target) {
  1145. case AV_PIX_FMT_ARGB:
  1146. dest[0] = hasAlpha ? A : 255;
  1147. dest[1] = R >> 22;
  1148. dest[2] = G >> 22;
  1149. dest[3] = B >> 22;
  1150. break;
  1151. case AV_PIX_FMT_RGB24:
  1152. dest[0] = R >> 22;
  1153. dest[1] = G >> 22;
  1154. dest[2] = B >> 22;
  1155. break;
  1156. case AV_PIX_FMT_RGBA:
  1157. dest[0] = R >> 22;
  1158. dest[1] = G >> 22;
  1159. dest[2] = B >> 22;
  1160. dest[3] = hasAlpha ? A : 255;
  1161. break;
  1162. case AV_PIX_FMT_ABGR:
  1163. dest[0] = hasAlpha ? A : 255;
  1164. dest[1] = B >> 22;
  1165. dest[2] = G >> 22;
  1166. dest[3] = R >> 22;
  1167. break;
  1168. case AV_PIX_FMT_BGR24:
  1169. dest[0] = B >> 22;
  1170. dest[1] = G >> 22;
  1171. dest[2] = R >> 22;
  1172. break;
  1173. case AV_PIX_FMT_BGRA:
  1174. dest[0] = B >> 22;
  1175. dest[1] = G >> 22;
  1176. dest[2] = R >> 22;
  1177. dest[3] = hasAlpha ? A : 255;
  1178. break;
  1179. case AV_PIX_FMT_BGR4_BYTE:
  1180. case AV_PIX_FMT_RGB4_BYTE:
  1181. case AV_PIX_FMT_BGR8:
  1182. case AV_PIX_FMT_RGB8:
  1183. {
  1184. int r,g,b;
  1185. R >>= 22;
  1186. G >>= 22;
  1187. B >>= 22;
  1188. 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;
  1189. 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;
  1190. 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;
  1191. c->dither_error[0][i] = err[0];
  1192. c->dither_error[1][i] = err[1];
  1193. c->dither_error[2][i] = err[2];
  1194. r = R >> (isrgb8 ? 5 : 7);
  1195. g = G >> (isrgb8 ? 5 : 6);
  1196. b = B >> (isrgb8 ? 6 : 7);
  1197. r = av_clip(r, 0, isrgb8 ? 7 : 1);
  1198. g = av_clip(g, 0, isrgb8 ? 7 : 3);
  1199. b = av_clip(b, 0, isrgb8 ? 3 : 1);
  1200. err[0] = R - r*(isrgb8 ? 36 : 255);
  1201. err[1] = G - g*(isrgb8 ? 36 : 85);
  1202. err[2] = B - b*(isrgb8 ? 85 : 255);
  1203. if(target == AV_PIX_FMT_BGR4_BYTE) {
  1204. dest[0] = r + 2*g + 8*b;
  1205. } else if(target == AV_PIX_FMT_RGB4_BYTE) {
  1206. dest[0] = b + 2*g + 8*r;
  1207. } else if(target == AV_PIX_FMT_BGR8) {
  1208. dest[0] = r + 8*g + 64*b;
  1209. } else if(target == AV_PIX_FMT_RGB8) {
  1210. dest[0] = b + 4*g + 32*r;
  1211. } else
  1212. av_assert2(0);
  1213. step = 1;
  1214. break;}
  1215. }
  1216. dest += step;
  1217. }
  1218. c->dither_error[0][i] = err[0];
  1219. c->dither_error[1][i] = err[1];
  1220. c->dither_error[2][i] = err[2];
  1221. }
  1222. #if CONFIG_SMALL
  1223. YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1224. YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1225. YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1226. YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1227. #else
  1228. #if CONFIG_SWSCALE_ALPHA
  1229. YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA, 1)
  1230. YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR, 1)
  1231. YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA, 1)
  1232. YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB, 1)
  1233. #endif
  1234. YUV2RGBWRAPPERX(yuv2, rgb_full, bgrx32_full, AV_PIX_FMT_BGRA, 0)
  1235. YUV2RGBWRAPPERX(yuv2, rgb_full, xbgr32_full, AV_PIX_FMT_ABGR, 0)
  1236. YUV2RGBWRAPPERX(yuv2, rgb_full, rgbx32_full, AV_PIX_FMT_RGBA, 0)
  1237. YUV2RGBWRAPPERX(yuv2, rgb_full, xrgb32_full, AV_PIX_FMT_ARGB, 0)
  1238. #endif
  1239. YUV2RGBWRAPPERX(yuv2, rgb_full, bgr24_full, AV_PIX_FMT_BGR24, 0)
  1240. YUV2RGBWRAPPERX(yuv2, rgb_full, rgb24_full, AV_PIX_FMT_RGB24, 0)
  1241. YUV2RGBWRAPPERX(yuv2, rgb_full, bgr4_byte_full, AV_PIX_FMT_BGR4_BYTE, 0)
  1242. YUV2RGBWRAPPERX(yuv2, rgb_full, rgb4_byte_full, AV_PIX_FMT_RGB4_BYTE, 0)
  1243. YUV2RGBWRAPPERX(yuv2, rgb_full, bgr8_full, AV_PIX_FMT_BGR8, 0)
  1244. YUV2RGBWRAPPERX(yuv2, rgb_full, rgb8_full, AV_PIX_FMT_RGB8, 0)
  1245. static void
  1246. yuv2gbrp_full_X_c(SwsContext *c, const int16_t *lumFilter,
  1247. const int16_t **lumSrc, int lumFilterSize,
  1248. const int16_t *chrFilter, const int16_t **chrUSrc,
  1249. const int16_t **chrVSrc, int chrFilterSize,
  1250. const int16_t **alpSrc, uint8_t **dest,
  1251. int dstW, int y)
  1252. {
  1253. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
  1254. int i;
  1255. int hasAlpha = 0;
  1256. uint16_t **dest16 = (uint16_t**)dest;
  1257. int SH = 22 + 7 - desc->comp[0].depth_minus1;
  1258. for (i = 0; i < dstW; i++) {
  1259. int j;
  1260. int Y = 1 << 9;
  1261. int U = (1 << 9) - (128 << 19);
  1262. int V = (1 << 9) - (128 << 19);
  1263. int R, G, B, A;
  1264. for (j = 0; j < lumFilterSize; j++)
  1265. Y += lumSrc[j][i] * lumFilter[j];
  1266. for (j = 0; j < chrFilterSize; j++) {
  1267. U += chrUSrc[j][i] * chrFilter[j];
  1268. V += chrVSrc[j][i] * chrFilter[j];
  1269. }
  1270. Y >>= 10;
  1271. U >>= 10;
  1272. V >>= 10;
  1273. if (hasAlpha) {
  1274. A = 1 << 18;
  1275. for (j = 0; j < lumFilterSize; j++)
  1276. A += alpSrc[j][i] * lumFilter[j];
  1277. A >>= 19;
  1278. if (A & 0x100)
  1279. A = av_clip_uint8(A);
  1280. }
  1281. Y -= c->yuv2rgb_y_offset;
  1282. Y *= c->yuv2rgb_y_coeff;
  1283. Y += 1 << 21;
  1284. R = Y + V * c->yuv2rgb_v2r_coeff;
  1285. G = Y + V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  1286. B = Y + U * c->yuv2rgb_u2b_coeff;
  1287. if ((R | G | B) & 0xC0000000) {
  1288. R = av_clip_uintp2(R, 30);
  1289. G = av_clip_uintp2(G, 30);
  1290. B = av_clip_uintp2(B, 30);
  1291. }
  1292. if (SH != 22) {
  1293. dest16[0][i] = G >> SH;
  1294. dest16[1][i] = B >> SH;
  1295. dest16[2][i] = R >> SH;
  1296. } else {
  1297. dest[0][i] = G >> 22;
  1298. dest[1][i] = B >> 22;
  1299. dest[2][i] = R >> 22;
  1300. }
  1301. }
  1302. if (SH != 22 && (!isBE(c->dstFormat)) != (!HAVE_BIGENDIAN)) {
  1303. for (i = 0; i < dstW; i++) {
  1304. dest16[0][i] = av_bswap16(dest16[0][i]);
  1305. dest16[1][i] = av_bswap16(dest16[1][i]);
  1306. dest16[2][i] = av_bswap16(dest16[2][i]);
  1307. }
  1308. }
  1309. }
  1310. av_cold void ff_sws_init_output_funcs(SwsContext *c,
  1311. yuv2planar1_fn *yuv2plane1,
  1312. yuv2planarX_fn *yuv2planeX,
  1313. yuv2interleavedX_fn *yuv2nv12cX,
  1314. yuv2packed1_fn *yuv2packed1,
  1315. yuv2packed2_fn *yuv2packed2,
  1316. yuv2packedX_fn *yuv2packedX,
  1317. yuv2anyX_fn *yuv2anyX)
  1318. {
  1319. enum AVPixelFormat dstFormat = c->dstFormat;
  1320. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
  1321. if (is16BPS(dstFormat)) {
  1322. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c : yuv2planeX_16LE_c;
  1323. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c : yuv2plane1_16LE_c;
  1324. } else if (is9_OR_10BPS(dstFormat)) {
  1325. if (desc->comp[0].depth_minus1 == 8) {
  1326. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c : yuv2planeX_9LE_c;
  1327. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c : yuv2plane1_9LE_c;
  1328. } else if (desc->comp[0].depth_minus1 == 9) {
  1329. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c : yuv2planeX_10LE_c;
  1330. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c : yuv2plane1_10LE_c;
  1331. } else if (desc->comp[0].depth_minus1 == 11) {
  1332. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_12BE_c : yuv2planeX_12LE_c;
  1333. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_12BE_c : yuv2plane1_12LE_c;
  1334. } else if (desc->comp[0].depth_minus1 == 13) {
  1335. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_14BE_c : yuv2planeX_14LE_c;
  1336. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_14BE_c : yuv2plane1_14LE_c;
  1337. } else
  1338. av_assert0(0);
  1339. } else {
  1340. *yuv2plane1 = yuv2plane1_8_c;
  1341. *yuv2planeX = yuv2planeX_8_c;
  1342. if (dstFormat == AV_PIX_FMT_NV12 || dstFormat == AV_PIX_FMT_NV21)
  1343. *yuv2nv12cX = yuv2nv12cX_c;
  1344. }
  1345. if(c->flags & SWS_FULL_CHR_H_INT) {
  1346. switch (dstFormat) {
  1347. case AV_PIX_FMT_RGBA:
  1348. #if CONFIG_SMALL
  1349. *yuv2packedX = yuv2rgba32_full_X_c;
  1350. #else
  1351. #if CONFIG_SWSCALE_ALPHA
  1352. if (c->alpPixBuf) {
  1353. *yuv2packedX = yuv2rgba32_full_X_c;
  1354. } else
  1355. #endif /* CONFIG_SWSCALE_ALPHA */
  1356. {
  1357. *yuv2packedX = yuv2rgbx32_full_X_c;
  1358. }
  1359. #endif /* !CONFIG_SMALL */
  1360. break;
  1361. case AV_PIX_FMT_ARGB:
  1362. #if CONFIG_SMALL
  1363. *yuv2packedX = yuv2argb32_full_X_c;
  1364. #else
  1365. #if CONFIG_SWSCALE_ALPHA
  1366. if (c->alpPixBuf) {
  1367. *yuv2packedX = yuv2argb32_full_X_c;
  1368. } else
  1369. #endif /* CONFIG_SWSCALE_ALPHA */
  1370. {
  1371. *yuv2packedX = yuv2xrgb32_full_X_c;
  1372. }
  1373. #endif /* !CONFIG_SMALL */
  1374. break;
  1375. case AV_PIX_FMT_BGRA:
  1376. #if CONFIG_SMALL
  1377. *yuv2packedX = yuv2bgra32_full_X_c;
  1378. #else
  1379. #if CONFIG_SWSCALE_ALPHA
  1380. if (c->alpPixBuf) {
  1381. *yuv2packedX = yuv2bgra32_full_X_c;
  1382. } else
  1383. #endif /* CONFIG_SWSCALE_ALPHA */
  1384. {
  1385. *yuv2packedX = yuv2bgrx32_full_X_c;
  1386. }
  1387. #endif /* !CONFIG_SMALL */
  1388. break;
  1389. case AV_PIX_FMT_ABGR:
  1390. #if CONFIG_SMALL
  1391. *yuv2packedX = yuv2abgr32_full_X_c;
  1392. #else
  1393. #if CONFIG_SWSCALE_ALPHA
  1394. if (c->alpPixBuf) {
  1395. *yuv2packedX = yuv2abgr32_full_X_c;
  1396. } else
  1397. #endif /* CONFIG_SWSCALE_ALPHA */
  1398. {
  1399. *yuv2packedX = yuv2xbgr32_full_X_c;
  1400. }
  1401. #endif /* !CONFIG_SMALL */
  1402. break;
  1403. case AV_PIX_FMT_RGB24:
  1404. *yuv2packedX = yuv2rgb24_full_X_c;
  1405. break;
  1406. case AV_PIX_FMT_BGR24:
  1407. *yuv2packedX = yuv2bgr24_full_X_c;
  1408. break;
  1409. case AV_PIX_FMT_BGR4_BYTE:
  1410. *yuv2packedX = yuv2bgr4_byte_full_X_c;
  1411. break;
  1412. case AV_PIX_FMT_RGB4_BYTE:
  1413. *yuv2packedX = yuv2rgb4_byte_full_X_c;
  1414. break;
  1415. case AV_PIX_FMT_BGR8:
  1416. *yuv2packedX = yuv2bgr8_full_X_c;
  1417. break;
  1418. case AV_PIX_FMT_RGB8:
  1419. *yuv2packedX = yuv2rgb8_full_X_c;
  1420. break;
  1421. case AV_PIX_FMT_GBRP:
  1422. case AV_PIX_FMT_GBRP9BE:
  1423. case AV_PIX_FMT_GBRP9LE:
  1424. case AV_PIX_FMT_GBRP10BE:
  1425. case AV_PIX_FMT_GBRP10LE:
  1426. case AV_PIX_FMT_GBRP12BE:
  1427. case AV_PIX_FMT_GBRP12LE:
  1428. case AV_PIX_FMT_GBRP14BE:
  1429. case AV_PIX_FMT_GBRP14LE:
  1430. case AV_PIX_FMT_GBRP16BE:
  1431. case AV_PIX_FMT_GBRP16LE:
  1432. *yuv2anyX = yuv2gbrp_full_X_c;
  1433. break;
  1434. }
  1435. if (!*yuv2packedX && !*yuv2anyX)
  1436. goto YUV_PACKED;
  1437. } else {
  1438. YUV_PACKED:
  1439. switch (dstFormat) {
  1440. case AV_PIX_FMT_RGB48LE:
  1441. *yuv2packed1 = yuv2rgb48le_1_c;
  1442. *yuv2packed2 = yuv2rgb48le_2_c;
  1443. *yuv2packedX = yuv2rgb48le_X_c;
  1444. break;
  1445. case AV_PIX_FMT_RGB48BE:
  1446. *yuv2packed1 = yuv2rgb48be_1_c;
  1447. *yuv2packed2 = yuv2rgb48be_2_c;
  1448. *yuv2packedX = yuv2rgb48be_X_c;
  1449. break;
  1450. case AV_PIX_FMT_BGR48LE:
  1451. *yuv2packed1 = yuv2bgr48le_1_c;
  1452. *yuv2packed2 = yuv2bgr48le_2_c;
  1453. *yuv2packedX = yuv2bgr48le_X_c;
  1454. break;
  1455. case AV_PIX_FMT_BGR48BE:
  1456. *yuv2packed1 = yuv2bgr48be_1_c;
  1457. *yuv2packed2 = yuv2bgr48be_2_c;
  1458. *yuv2packedX = yuv2bgr48be_X_c;
  1459. break;
  1460. case AV_PIX_FMT_RGB32:
  1461. case AV_PIX_FMT_BGR32:
  1462. #if CONFIG_SMALL
  1463. *yuv2packed1 = yuv2rgb32_1_c;
  1464. *yuv2packed2 = yuv2rgb32_2_c;
  1465. *yuv2packedX = yuv2rgb32_X_c;
  1466. #else
  1467. #if CONFIG_SWSCALE_ALPHA
  1468. if (c->alpPixBuf) {
  1469. *yuv2packed1 = yuv2rgba32_1_c;
  1470. *yuv2packed2 = yuv2rgba32_2_c;
  1471. *yuv2packedX = yuv2rgba32_X_c;
  1472. } else
  1473. #endif /* CONFIG_SWSCALE_ALPHA */
  1474. {
  1475. *yuv2packed1 = yuv2rgbx32_1_c;
  1476. *yuv2packed2 = yuv2rgbx32_2_c;
  1477. *yuv2packedX = yuv2rgbx32_X_c;
  1478. }
  1479. #endif /* !CONFIG_SMALL */
  1480. break;
  1481. case AV_PIX_FMT_RGB32_1:
  1482. case AV_PIX_FMT_BGR32_1:
  1483. #if CONFIG_SMALL
  1484. *yuv2packed1 = yuv2rgb32_1_1_c;
  1485. *yuv2packed2 = yuv2rgb32_1_2_c;
  1486. *yuv2packedX = yuv2rgb32_1_X_c;
  1487. #else
  1488. #if CONFIG_SWSCALE_ALPHA
  1489. if (c->alpPixBuf) {
  1490. *yuv2packed1 = yuv2rgba32_1_1_c;
  1491. *yuv2packed2 = yuv2rgba32_1_2_c;
  1492. *yuv2packedX = yuv2rgba32_1_X_c;
  1493. } else
  1494. #endif /* CONFIG_SWSCALE_ALPHA */
  1495. {
  1496. *yuv2packed1 = yuv2rgbx32_1_1_c;
  1497. *yuv2packed2 = yuv2rgbx32_1_2_c;
  1498. *yuv2packedX = yuv2rgbx32_1_X_c;
  1499. }
  1500. #endif /* !CONFIG_SMALL */
  1501. break;
  1502. case AV_PIX_FMT_RGB24:
  1503. *yuv2packed1 = yuv2rgb24_1_c;
  1504. *yuv2packed2 = yuv2rgb24_2_c;
  1505. *yuv2packedX = yuv2rgb24_X_c;
  1506. break;
  1507. case AV_PIX_FMT_BGR24:
  1508. *yuv2packed1 = yuv2bgr24_1_c;
  1509. *yuv2packed2 = yuv2bgr24_2_c;
  1510. *yuv2packedX = yuv2bgr24_X_c;
  1511. break;
  1512. case AV_PIX_FMT_RGB565LE:
  1513. case AV_PIX_FMT_RGB565BE:
  1514. case AV_PIX_FMT_BGR565LE:
  1515. case AV_PIX_FMT_BGR565BE:
  1516. *yuv2packed1 = yuv2rgb16_1_c;
  1517. *yuv2packed2 = yuv2rgb16_2_c;
  1518. *yuv2packedX = yuv2rgb16_X_c;
  1519. break;
  1520. case AV_PIX_FMT_RGB555LE:
  1521. case AV_PIX_FMT_RGB555BE:
  1522. case AV_PIX_FMT_BGR555LE:
  1523. case AV_PIX_FMT_BGR555BE:
  1524. *yuv2packed1 = yuv2rgb15_1_c;
  1525. *yuv2packed2 = yuv2rgb15_2_c;
  1526. *yuv2packedX = yuv2rgb15_X_c;
  1527. break;
  1528. case AV_PIX_FMT_RGB444LE:
  1529. case AV_PIX_FMT_RGB444BE:
  1530. case AV_PIX_FMT_BGR444LE:
  1531. case AV_PIX_FMT_BGR444BE:
  1532. *yuv2packed1 = yuv2rgb12_1_c;
  1533. *yuv2packed2 = yuv2rgb12_2_c;
  1534. *yuv2packedX = yuv2rgb12_X_c;
  1535. break;
  1536. case AV_PIX_FMT_RGB8:
  1537. case AV_PIX_FMT_BGR8:
  1538. *yuv2packed1 = yuv2rgb8_1_c;
  1539. *yuv2packed2 = yuv2rgb8_2_c;
  1540. *yuv2packedX = yuv2rgb8_X_c;
  1541. break;
  1542. case AV_PIX_FMT_RGB4:
  1543. case AV_PIX_FMT_BGR4:
  1544. *yuv2packed1 = yuv2rgb4_1_c;
  1545. *yuv2packed2 = yuv2rgb4_2_c;
  1546. *yuv2packedX = yuv2rgb4_X_c;
  1547. break;
  1548. case AV_PIX_FMT_RGB4_BYTE:
  1549. case AV_PIX_FMT_BGR4_BYTE:
  1550. *yuv2packed1 = yuv2rgb4b_1_c;
  1551. *yuv2packed2 = yuv2rgb4b_2_c;
  1552. *yuv2packedX = yuv2rgb4b_X_c;
  1553. break;
  1554. }
  1555. }
  1556. switch (dstFormat) {
  1557. case AV_PIX_FMT_MONOWHITE:
  1558. *yuv2packed1 = yuv2monowhite_1_c;
  1559. *yuv2packed2 = yuv2monowhite_2_c;
  1560. *yuv2packedX = yuv2monowhite_X_c;
  1561. break;
  1562. case AV_PIX_FMT_MONOBLACK:
  1563. *yuv2packed1 = yuv2monoblack_1_c;
  1564. *yuv2packed2 = yuv2monoblack_2_c;
  1565. *yuv2packedX = yuv2monoblack_X_c;
  1566. break;
  1567. case AV_PIX_FMT_YUYV422:
  1568. *yuv2packed1 = yuv2yuyv422_1_c;
  1569. *yuv2packed2 = yuv2yuyv422_2_c;
  1570. *yuv2packedX = yuv2yuyv422_X_c;
  1571. break;
  1572. case AV_PIX_FMT_UYVY422:
  1573. *yuv2packed1 = yuv2uyvy422_1_c;
  1574. *yuv2packed2 = yuv2uyvy422_2_c;
  1575. *yuv2packedX = yuv2uyvy422_X_c;
  1576. break;
  1577. }
  1578. }