swscale.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950
  1. /*
  2. * Copyright (C) 2001-2003 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 modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (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
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * 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. * the C code (not assembly, mmx, ...) of this file can be used
  21. * under the LGPL license too
  22. */
  23. /*
  24. supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR32_1, BGR24, BGR16, BGR15, RGB32, RGB32_1, RGB24, Y8/Y800, YVU9/IF09, PAL8
  25. supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
  26. {BGR,RGB}{1,4,8,15,16} support dithering
  27. unscaled special converters (YV12=I420=IYUV, Y800=Y8)
  28. YV12 -> {BGR,RGB}{1,4,8,12,15,16,24,32}
  29. x -> x
  30. YUV9 -> YV12
  31. YUV9/YV12 -> Y800
  32. Y800 -> YUV9/YV12
  33. BGR24 -> BGR32 & RGB24 -> RGB32
  34. BGR32 -> BGR24 & RGB32 -> RGB24
  35. BGR15 -> BGR16
  36. */
  37. /*
  38. tested special converters (most are tested actually, but I did not write it down ...)
  39. YV12 -> BGR16
  40. YV12 -> YV12
  41. BGR15 -> BGR16
  42. BGR16 -> BGR16
  43. YVU9 -> YV12
  44. untested special converters
  45. YV12/I420 -> BGR15/BGR24/BGR32 (it is the yuv2rgb stuff, so it should be OK)
  46. YV12/I420 -> YV12/I420
  47. YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
  48. BGR24 -> BGR32 & RGB24 -> RGB32
  49. BGR32 -> BGR24 & RGB32 -> RGB24
  50. BGR24 -> YV12
  51. */
  52. #include <inttypes.h>
  53. #include <string.h>
  54. #include <math.h>
  55. #include <stdio.h>
  56. #include "config.h"
  57. #include <assert.h>
  58. #include "swscale.h"
  59. #include "swscale_internal.h"
  60. #include "rgb2rgb.h"
  61. #include "libavutil/intreadwrite.h"
  62. #include "libavutil/x86_cpu.h"
  63. #include "libavutil/avutil.h"
  64. #include "libavutil/bswap.h"
  65. #include "libavutil/pixdesc.h"
  66. #undef MOVNTQ
  67. #undef PAVGB
  68. //#undef HAVE_MMX2
  69. //#define HAVE_AMD3DNOW
  70. //#undef HAVE_MMX
  71. //#undef ARCH_X86
  72. #define DITHER1XBPP
  73. #define FAST_BGR2YV12 // use 7 bit coefficients instead of 15 bit
  74. #ifdef M_PI
  75. #define PI M_PI
  76. #else
  77. #define PI 3.14159265358979323846
  78. #endif
  79. #define isPacked(x) ( \
  80. (x)==PIX_FMT_PAL8 \
  81. || (x)==PIX_FMT_YUYV422 \
  82. || (x)==PIX_FMT_UYVY422 \
  83. || isAnyRGB(x) \
  84. )
  85. #define RGB2YUV_SHIFT 15
  86. #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  87. #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  88. #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  89. #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  90. #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  91. #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  92. #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  93. #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  94. #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  95. static const double rgb2yuv_table[8][9]={
  96. {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
  97. {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
  98. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
  99. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
  100. {0.59 , 0.11 , 0.30 , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
  101. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
  102. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //SMPTE 170M
  103. {0.701 , 0.087 , 0.212 , -0.384, 0.5 -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
  104. };
  105. /*
  106. NOTES
  107. Special versions: fast Y 1:1 scaling (no interpolation in y direction)
  108. TODO
  109. more intelligent misalignment avoidance for the horizontal scaler
  110. write special vertical cubic upscale version
  111. optimize C code (YV12 / minmax)
  112. add support for packed pixel YUV input & output
  113. add support for Y8 output
  114. optimize BGR24 & BGR32
  115. add BGR4 output support
  116. write special BGR->BGR scaler
  117. */
  118. #if ARCH_X86 && CONFIG_GPL
  119. DECLARE_ASM_CONST(8, uint64_t, bF8)= 0xF8F8F8F8F8F8F8F8LL;
  120. DECLARE_ASM_CONST(8, uint64_t, bFC)= 0xFCFCFCFCFCFCFCFCLL;
  121. DECLARE_ASM_CONST(8, uint64_t, w10)= 0x0010001000100010LL;
  122. DECLARE_ASM_CONST(8, uint64_t, w02)= 0x0002000200020002LL;
  123. DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
  124. DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
  125. DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
  126. DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
  127. const DECLARE_ALIGNED(8, uint64_t, ff_dither4)[2] = {
  128. 0x0103010301030103LL,
  129. 0x0200020002000200LL,};
  130. const DECLARE_ALIGNED(8, uint64_t, ff_dither8)[2] = {
  131. 0x0602060206020602LL,
  132. 0x0004000400040004LL,};
  133. DECLARE_ASM_CONST(8, uint64_t, b16Mask)= 0x001F001F001F001FLL;
  134. DECLARE_ASM_CONST(8, uint64_t, g16Mask)= 0x07E007E007E007E0LL;
  135. DECLARE_ASM_CONST(8, uint64_t, r16Mask)= 0xF800F800F800F800LL;
  136. DECLARE_ASM_CONST(8, uint64_t, b15Mask)= 0x001F001F001F001FLL;
  137. DECLARE_ASM_CONST(8, uint64_t, g15Mask)= 0x03E003E003E003E0LL;
  138. DECLARE_ASM_CONST(8, uint64_t, r15Mask)= 0x7C007C007C007C00LL;
  139. DECLARE_ALIGNED(8, const uint64_t, ff_M24A) = 0x00FF0000FF0000FFLL;
  140. DECLARE_ALIGNED(8, const uint64_t, ff_M24B) = 0xFF0000FF0000FF00LL;
  141. DECLARE_ALIGNED(8, const uint64_t, ff_M24C) = 0x0000FF0000FF0000LL;
  142. #ifdef FAST_BGR2YV12
  143. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000000210041000DULL;
  144. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000FFEEFFDC0038ULL;
  145. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00000038FFD2FFF8ULL;
  146. #else
  147. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000020E540830C8BULL;
  148. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000ED0FDAC23831ULL;
  149. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00003831D0E6F6EAULL;
  150. #endif /* FAST_BGR2YV12 */
  151. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset) = 0x1010101010101010ULL;
  152. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
  153. DECLARE_ALIGNED(8, const uint64_t, ff_w1111) = 0x0001000100010001ULL;
  154. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
  155. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
  156. DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
  157. DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
  158. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
  159. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV)[2][4] = {
  160. {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
  161. {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
  162. };
  163. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
  164. #endif /* ARCH_X86 && CONFIG_GPL */
  165. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
  166. { 1, 3, 1, 3, 1, 3, 1, 3, },
  167. { 2, 0, 2, 0, 2, 0, 2, 0, },
  168. };
  169. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
  170. { 6, 2, 6, 2, 6, 2, 6, 2, },
  171. { 0, 4, 0, 4, 0, 4, 0, 4, },
  172. };
  173. DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[4][8]={
  174. { 8, 4, 11, 7, 8, 4, 11, 7, },
  175. { 2, 14, 1, 13, 2, 14, 1, 13, },
  176. { 10, 6, 9, 5, 10, 6, 9, 5, },
  177. { 0, 12, 3, 15, 0, 12, 3, 15, },
  178. };
  179. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
  180. { 17, 9, 23, 15, 16, 8, 22, 14, },
  181. { 5, 29, 3, 27, 4, 28, 2, 26, },
  182. { 21, 13, 19, 11, 20, 12, 18, 10, },
  183. { 0, 24, 6, 30, 1, 25, 7, 31, },
  184. { 16, 8, 22, 14, 17, 9, 23, 15, },
  185. { 4, 28, 2, 26, 5, 29, 3, 27, },
  186. { 20, 12, 18, 10, 21, 13, 19, 11, },
  187. { 1, 25, 7, 31, 0, 24, 6, 30, },
  188. };
  189. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
  190. { 0, 55, 14, 68, 3, 58, 17, 72, },
  191. { 37, 18, 50, 32, 40, 22, 54, 35, },
  192. { 9, 64, 5, 59, 13, 67, 8, 63, },
  193. { 46, 27, 41, 23, 49, 31, 44, 26, },
  194. { 2, 57, 16, 71, 1, 56, 15, 70, },
  195. { 39, 21, 52, 34, 38, 19, 51, 33, },
  196. { 11, 66, 7, 62, 10, 65, 6, 60, },
  197. { 48, 30, 43, 25, 47, 29, 42, 24, },
  198. };
  199. #if 1
  200. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  201. {117, 62, 158, 103, 113, 58, 155, 100, },
  202. { 34, 199, 21, 186, 31, 196, 17, 182, },
  203. {144, 89, 131, 76, 141, 86, 127, 72, },
  204. { 0, 165, 41, 206, 10, 175, 52, 217, },
  205. {110, 55, 151, 96, 120, 65, 162, 107, },
  206. { 28, 193, 14, 179, 38, 203, 24, 189, },
  207. {138, 83, 124, 69, 148, 93, 134, 79, },
  208. { 7, 172, 48, 213, 3, 168, 45, 210, },
  209. };
  210. #elif 1
  211. // tries to correct a gamma of 1.5
  212. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  213. { 0, 143, 18, 200, 2, 156, 25, 215, },
  214. { 78, 28, 125, 64, 89, 36, 138, 74, },
  215. { 10, 180, 3, 161, 16, 195, 8, 175, },
  216. {109, 51, 93, 38, 121, 60, 105, 47, },
  217. { 1, 152, 23, 210, 0, 147, 20, 205, },
  218. { 85, 33, 134, 71, 81, 30, 130, 67, },
  219. { 14, 190, 6, 171, 12, 185, 5, 166, },
  220. {117, 57, 101, 44, 113, 54, 97, 41, },
  221. };
  222. #elif 1
  223. // tries to correct a gamma of 2.0
  224. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  225. { 0, 124, 8, 193, 0, 140, 12, 213, },
  226. { 55, 14, 104, 42, 66, 19, 119, 52, },
  227. { 3, 168, 1, 145, 6, 187, 3, 162, },
  228. { 86, 31, 70, 21, 99, 39, 82, 28, },
  229. { 0, 134, 11, 206, 0, 129, 9, 200, },
  230. { 62, 17, 114, 48, 58, 16, 109, 45, },
  231. { 5, 181, 2, 157, 4, 175, 1, 151, },
  232. { 95, 36, 78, 26, 90, 34, 74, 24, },
  233. };
  234. #else
  235. // tries to correct a gamma of 2.5
  236. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  237. { 0, 107, 3, 187, 0, 125, 6, 212, },
  238. { 39, 7, 86, 28, 49, 11, 102, 36, },
  239. { 1, 158, 0, 131, 3, 180, 1, 151, },
  240. { 68, 19, 52, 12, 81, 25, 64, 17, },
  241. { 0, 119, 5, 203, 0, 113, 4, 195, },
  242. { 45, 9, 96, 33, 42, 8, 91, 30, },
  243. { 2, 172, 1, 144, 2, 165, 0, 137, },
  244. { 77, 23, 60, 15, 72, 21, 56, 14, },
  245. };
  246. #endif
  247. static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  248. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  249. const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
  250. int dstW, int chrDstW, int big_endian)
  251. {
  252. //FIXME Optimize (just quickly written not optimized..)
  253. int i;
  254. for (i = 0; i < dstW; i++) {
  255. int val = 1 << 10;
  256. int j;
  257. for (j = 0; j < lumFilterSize; j++)
  258. val += lumSrc[j][i] * lumFilter[j];
  259. if (big_endian) {
  260. AV_WB16(&dest[i], av_clip_uint16(val >> 11));
  261. } else {
  262. AV_WL16(&dest[i], av_clip_uint16(val >> 11));
  263. }
  264. }
  265. if (uDest) {
  266. for (i = 0; i < chrDstW; i++) {
  267. int u = 1 << 10;
  268. int v = 1 << 10;
  269. int j;
  270. for (j = 0; j < chrFilterSize; j++) {
  271. u += chrSrc[j][i ] * chrFilter[j];
  272. v += chrSrc[j][i + VOFW] * chrFilter[j];
  273. }
  274. if (big_endian) {
  275. AV_WB16(&uDest[i], av_clip_uint16(u >> 11));
  276. AV_WB16(&vDest[i], av_clip_uint16(v >> 11));
  277. } else {
  278. AV_WL16(&uDest[i], av_clip_uint16(u >> 11));
  279. AV_WL16(&vDest[i], av_clip_uint16(v >> 11));
  280. }
  281. }
  282. }
  283. if (CONFIG_SWSCALE_ALPHA && aDest) {
  284. for (i = 0; i < dstW; i++) {
  285. int val = 1 << 10;
  286. int j;
  287. for (j = 0; j < lumFilterSize; j++)
  288. val += alpSrc[j][i] * lumFilter[j];
  289. if (big_endian) {
  290. AV_WB16(&aDest[i], av_clip_uint16(val >> 11));
  291. } else {
  292. AV_WL16(&aDest[i], av_clip_uint16(val >> 11));
  293. }
  294. }
  295. }
  296. }
  297. static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  298. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  299. const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
  300. enum PixelFormat dstFormat)
  301. {
  302. if (isBE(dstFormat)) {
  303. yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
  304. chrFilter, chrSrc, chrFilterSize,
  305. alpSrc,
  306. dest, uDest, vDest, aDest,
  307. dstW, chrDstW, 1);
  308. } else {
  309. yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
  310. chrFilter, chrSrc, chrFilterSize,
  311. alpSrc,
  312. dest, uDest, vDest, aDest,
  313. dstW, chrDstW, 0);
  314. }
  315. }
  316. static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  317. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  318. const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
  319. {
  320. //FIXME Optimize (just quickly written not optimized..)
  321. int i;
  322. for (i=0; i<dstW; i++) {
  323. int val=1<<18;
  324. int j;
  325. for (j=0; j<lumFilterSize; j++)
  326. val += lumSrc[j][i] * lumFilter[j];
  327. dest[i]= av_clip_uint8(val>>19);
  328. }
  329. if (uDest)
  330. for (i=0; i<chrDstW; i++) {
  331. int u=1<<18;
  332. int v=1<<18;
  333. int j;
  334. for (j=0; j<chrFilterSize; j++) {
  335. u += chrSrc[j][i] * chrFilter[j];
  336. v += chrSrc[j][i + VOFW] * chrFilter[j];
  337. }
  338. uDest[i]= av_clip_uint8(u>>19);
  339. vDest[i]= av_clip_uint8(v>>19);
  340. }
  341. if (CONFIG_SWSCALE_ALPHA && aDest)
  342. for (i=0; i<dstW; i++) {
  343. int val=1<<18;
  344. int j;
  345. for (j=0; j<lumFilterSize; j++)
  346. val += alpSrc[j][i] * lumFilter[j];
  347. aDest[i]= av_clip_uint8(val>>19);
  348. }
  349. }
  350. static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  351. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  352. uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
  353. {
  354. //FIXME Optimize (just quickly written not optimized..)
  355. int i;
  356. for (i=0; i<dstW; i++) {
  357. int val=1<<18;
  358. int j;
  359. for (j=0; j<lumFilterSize; j++)
  360. val += lumSrc[j][i] * lumFilter[j];
  361. dest[i]= av_clip_uint8(val>>19);
  362. }
  363. if (!uDest)
  364. return;
  365. if (dstFormat == PIX_FMT_NV12)
  366. for (i=0; i<chrDstW; i++) {
  367. int u=1<<18;
  368. int v=1<<18;
  369. int j;
  370. for (j=0; j<chrFilterSize; j++) {
  371. u += chrSrc[j][i] * chrFilter[j];
  372. v += chrSrc[j][i + VOFW] * chrFilter[j];
  373. }
  374. uDest[2*i]= av_clip_uint8(u>>19);
  375. uDest[2*i+1]= av_clip_uint8(v>>19);
  376. }
  377. else
  378. for (i=0; i<chrDstW; i++) {
  379. int u=1<<18;
  380. int v=1<<18;
  381. int j;
  382. for (j=0; j<chrFilterSize; j++) {
  383. u += chrSrc[j][i] * chrFilter[j];
  384. v += chrSrc[j][i + VOFW] * chrFilter[j];
  385. }
  386. uDest[2*i]= av_clip_uint8(v>>19);
  387. uDest[2*i+1]= av_clip_uint8(u>>19);
  388. }
  389. }
  390. #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
  391. for (i=0; i<(dstW>>1); i++) {\
  392. int j;\
  393. int Y1 = 1<<18;\
  394. int Y2 = 1<<18;\
  395. int U = 1<<18;\
  396. int V = 1<<18;\
  397. int av_unused A1, A2;\
  398. type av_unused *r, *b, *g;\
  399. const int i2= 2*i;\
  400. \
  401. for (j=0; j<lumFilterSize; j++) {\
  402. Y1 += lumSrc[j][i2] * lumFilter[j];\
  403. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  404. }\
  405. for (j=0; j<chrFilterSize; j++) {\
  406. U += chrSrc[j][i] * chrFilter[j];\
  407. V += chrSrc[j][i+VOFW] * chrFilter[j];\
  408. }\
  409. Y1>>=19;\
  410. Y2>>=19;\
  411. U >>=19;\
  412. V >>=19;\
  413. if (alpha) {\
  414. A1 = 1<<18;\
  415. A2 = 1<<18;\
  416. for (j=0; j<lumFilterSize; j++) {\
  417. A1 += alpSrc[j][i2 ] * lumFilter[j];\
  418. A2 += alpSrc[j][i2+1] * lumFilter[j];\
  419. }\
  420. A1>>=19;\
  421. A2>>=19;\
  422. }
  423. #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
  424. YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
  425. if ((Y1|Y2|U|V)&256) {\
  426. if (Y1>255) Y1=255; \
  427. else if (Y1<0)Y1=0; \
  428. if (Y2>255) Y2=255; \
  429. else if (Y2<0)Y2=0; \
  430. if (U>255) U=255; \
  431. else if (U<0) U=0; \
  432. if (V>255) V=255; \
  433. else if (V<0) V=0; \
  434. }\
  435. if (alpha && ((A1|A2)&256)) {\
  436. A1=av_clip_uint8(A1);\
  437. A2=av_clip_uint8(A2);\
  438. }
  439. #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
  440. for (i=0; i<dstW; i++) {\
  441. int j;\
  442. int Y = 0;\
  443. int U = -128<<19;\
  444. int V = -128<<19;\
  445. int av_unused A;\
  446. int R,G,B;\
  447. \
  448. for (j=0; j<lumFilterSize; j++) {\
  449. Y += lumSrc[j][i ] * lumFilter[j];\
  450. }\
  451. for (j=0; j<chrFilterSize; j++) {\
  452. U += chrSrc[j][i ] * chrFilter[j];\
  453. V += chrSrc[j][i+VOFW] * chrFilter[j];\
  454. }\
  455. Y >>=10;\
  456. U >>=10;\
  457. V >>=10;\
  458. if (alpha) {\
  459. A = rnd;\
  460. for (j=0; j<lumFilterSize; j++)\
  461. A += alpSrc[j][i ] * lumFilter[j];\
  462. A >>=19;\
  463. if (A&256)\
  464. A = av_clip_uint8(A);\
  465. }
  466. #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
  467. YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
  468. Y-= c->yuv2rgb_y_offset;\
  469. Y*= c->yuv2rgb_y_coeff;\
  470. Y+= rnd;\
  471. R= Y + V*c->yuv2rgb_v2r_coeff;\
  472. G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
  473. B= Y + U*c->yuv2rgb_u2b_coeff;\
  474. if ((R|G|B)&(0xC0000000)) {\
  475. if (R>=(256<<22)) R=(256<<22)-1; \
  476. else if (R<0)R=0; \
  477. if (G>=(256<<22)) G=(256<<22)-1; \
  478. else if (G<0)G=0; \
  479. if (B>=(256<<22)) B=(256<<22)-1; \
  480. else if (B<0)B=0; \
  481. }
  482. #define YSCALE_YUV_2_GRAY16_C \
  483. for (i=0; i<(dstW>>1); i++) {\
  484. int j;\
  485. int Y1 = 1<<18;\
  486. int Y2 = 1<<18;\
  487. int U = 1<<18;\
  488. int V = 1<<18;\
  489. \
  490. const int i2= 2*i;\
  491. \
  492. for (j=0; j<lumFilterSize; j++) {\
  493. Y1 += lumSrc[j][i2] * lumFilter[j];\
  494. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  495. }\
  496. Y1>>=11;\
  497. Y2>>=11;\
  498. if ((Y1|Y2|U|V)&65536) {\
  499. if (Y1>65535) Y1=65535; \
  500. else if (Y1<0)Y1=0; \
  501. if (Y2>65535) Y2=65535; \
  502. else if (Y2<0)Y2=0; \
  503. }
  504. #define YSCALE_YUV_2_RGBX_C(type,alpha) \
  505. YSCALE_YUV_2_PACKEDX_C(type,alpha) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
  506. r = (type *)c->table_rV[V]; \
  507. g = (type *)(c->table_gU[U] + c->table_gV[V]); \
  508. b = (type *)c->table_bU[U];
  509. #define YSCALE_YUV_2_PACKED2_C(type,alpha) \
  510. for (i=0; i<(dstW>>1); i++) { \
  511. const int i2= 2*i; \
  512. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
  513. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
  514. int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
  515. int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
  516. type av_unused *r, *b, *g; \
  517. int av_unused A1, A2; \
  518. if (alpha) {\
  519. A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
  520. A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
  521. }
  522. #define YSCALE_YUV_2_GRAY16_2_C \
  523. for (i=0; i<(dstW>>1); i++) { \
  524. const int i2= 2*i; \
  525. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
  526. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;
  527. #define YSCALE_YUV_2_RGB2_C(type,alpha) \
  528. YSCALE_YUV_2_PACKED2_C(type,alpha)\
  529. r = (type *)c->table_rV[V];\
  530. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  531. b = (type *)c->table_bU[U];
  532. #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
  533. for (i=0; i<(dstW>>1); i++) {\
  534. const int i2= 2*i;\
  535. int Y1= buf0[i2 ]>>7;\
  536. int Y2= buf0[i2+1]>>7;\
  537. int U= (uvbuf1[i ])>>7;\
  538. int V= (uvbuf1[i+VOFW])>>7;\
  539. type av_unused *r, *b, *g;\
  540. int av_unused A1, A2;\
  541. if (alpha) {\
  542. A1= abuf0[i2 ]>>7;\
  543. A2= abuf0[i2+1]>>7;\
  544. }
  545. #define YSCALE_YUV_2_GRAY16_1_C \
  546. for (i=0; i<(dstW>>1); i++) {\
  547. const int i2= 2*i;\
  548. int Y1= buf0[i2 ]<<1;\
  549. int Y2= buf0[i2+1]<<1;
  550. #define YSCALE_YUV_2_RGB1_C(type,alpha) \
  551. YSCALE_YUV_2_PACKED1_C(type,alpha)\
  552. r = (type *)c->table_rV[V];\
  553. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  554. b = (type *)c->table_bU[U];
  555. #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
  556. for (i=0; i<(dstW>>1); i++) {\
  557. const int i2= 2*i;\
  558. int Y1= buf0[i2 ]>>7;\
  559. int Y2= buf0[i2+1]>>7;\
  560. int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
  561. int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
  562. type av_unused *r, *b, *g;\
  563. int av_unused A1, A2;\
  564. if (alpha) {\
  565. A1= abuf0[i2 ]>>7;\
  566. A2= abuf0[i2+1]>>7;\
  567. }
  568. #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
  569. YSCALE_YUV_2_PACKED1B_C(type,alpha)\
  570. r = (type *)c->table_rV[V];\
  571. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  572. b = (type *)c->table_bU[U];
  573. #define YSCALE_YUV_2_MONO2_C \
  574. const uint8_t * const d128=dither_8x8_220[y&7];\
  575. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  576. for (i=0; i<dstW-7; i+=8) {\
  577. int acc;\
  578. acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
  579. acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
  580. acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
  581. acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
  582. acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
  583. acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
  584. acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
  585. acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
  586. ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
  587. dest++;\
  588. }
  589. #define YSCALE_YUV_2_MONOX_C \
  590. const uint8_t * const d128=dither_8x8_220[y&7];\
  591. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  592. int acc=0;\
  593. for (i=0; i<dstW-1; i+=2) {\
  594. int j;\
  595. int Y1=1<<18;\
  596. int Y2=1<<18;\
  597. \
  598. for (j=0; j<lumFilterSize; j++) {\
  599. Y1 += lumSrc[j][i] * lumFilter[j];\
  600. Y2 += lumSrc[j][i+1] * lumFilter[j];\
  601. }\
  602. Y1>>=19;\
  603. Y2>>=19;\
  604. if ((Y1|Y2)&256) {\
  605. if (Y1>255) Y1=255;\
  606. else if (Y1<0)Y1=0;\
  607. if (Y2>255) Y2=255;\
  608. else if (Y2<0)Y2=0;\
  609. }\
  610. acc+= acc + g[Y1+d128[(i+0)&7]];\
  611. acc+= acc + g[Y2+d128[(i+1)&7]];\
  612. if ((i&7)==6) {\
  613. ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
  614. dest++;\
  615. }\
  616. }
  617. #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
  618. switch(c->dstFormat) {\
  619. case PIX_FMT_RGB48BE:\
  620. case PIX_FMT_RGB48LE:\
  621. func(uint8_t,0)\
  622. ((uint8_t*)dest)[ 0]= r[Y1];\
  623. ((uint8_t*)dest)[ 1]= r[Y1];\
  624. ((uint8_t*)dest)[ 2]= g[Y1];\
  625. ((uint8_t*)dest)[ 3]= g[Y1];\
  626. ((uint8_t*)dest)[ 4]= b[Y1];\
  627. ((uint8_t*)dest)[ 5]= b[Y1];\
  628. ((uint8_t*)dest)[ 6]= r[Y2];\
  629. ((uint8_t*)dest)[ 7]= r[Y2];\
  630. ((uint8_t*)dest)[ 8]= g[Y2];\
  631. ((uint8_t*)dest)[ 9]= g[Y2];\
  632. ((uint8_t*)dest)[10]= b[Y2];\
  633. ((uint8_t*)dest)[11]= b[Y2];\
  634. dest+=12;\
  635. }\
  636. break;\
  637. case PIX_FMT_RGBA:\
  638. case PIX_FMT_BGRA:\
  639. if (CONFIG_SMALL) {\
  640. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
  641. func(uint32_t,needAlpha)\
  642. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
  643. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
  644. }\
  645. } else {\
  646. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
  647. func(uint32_t,1)\
  648. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
  649. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
  650. }\
  651. } else {\
  652. func(uint32_t,0)\
  653. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  654. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  655. }\
  656. }\
  657. }\
  658. break;\
  659. case PIX_FMT_ARGB:\
  660. case PIX_FMT_ABGR:\
  661. if (CONFIG_SMALL) {\
  662. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
  663. func(uint32_t,needAlpha)\
  664. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
  665. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
  666. }\
  667. } else {\
  668. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
  669. func(uint32_t,1)\
  670. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
  671. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
  672. }\
  673. } else {\
  674. func(uint32_t,0)\
  675. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  676. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  677. }\
  678. }\
  679. } \
  680. break;\
  681. case PIX_FMT_RGB24:\
  682. func(uint8_t,0)\
  683. ((uint8_t*)dest)[0]= r[Y1];\
  684. ((uint8_t*)dest)[1]= g[Y1];\
  685. ((uint8_t*)dest)[2]= b[Y1];\
  686. ((uint8_t*)dest)[3]= r[Y2];\
  687. ((uint8_t*)dest)[4]= g[Y2];\
  688. ((uint8_t*)dest)[5]= b[Y2];\
  689. dest+=6;\
  690. }\
  691. break;\
  692. case PIX_FMT_BGR24:\
  693. func(uint8_t,0)\
  694. ((uint8_t*)dest)[0]= b[Y1];\
  695. ((uint8_t*)dest)[1]= g[Y1];\
  696. ((uint8_t*)dest)[2]= r[Y1];\
  697. ((uint8_t*)dest)[3]= b[Y2];\
  698. ((uint8_t*)dest)[4]= g[Y2];\
  699. ((uint8_t*)dest)[5]= r[Y2];\
  700. dest+=6;\
  701. }\
  702. break;\
  703. case PIX_FMT_RGB565BE:\
  704. case PIX_FMT_RGB565LE:\
  705. case PIX_FMT_BGR565BE:\
  706. case PIX_FMT_BGR565LE:\
  707. {\
  708. const int dr1= dither_2x2_8[y&1 ][0];\
  709. const int dg1= dither_2x2_4[y&1 ][0];\
  710. const int db1= dither_2x2_8[(y&1)^1][0];\
  711. const int dr2= dither_2x2_8[y&1 ][1];\
  712. const int dg2= dither_2x2_4[y&1 ][1];\
  713. const int db2= dither_2x2_8[(y&1)^1][1];\
  714. func(uint16_t,0)\
  715. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  716. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  717. }\
  718. }\
  719. break;\
  720. case PIX_FMT_RGB555BE:\
  721. case PIX_FMT_RGB555LE:\
  722. case PIX_FMT_BGR555BE:\
  723. case PIX_FMT_BGR555LE:\
  724. {\
  725. const int dr1= dither_2x2_8[y&1 ][0];\
  726. const int dg1= dither_2x2_8[y&1 ][1];\
  727. const int db1= dither_2x2_8[(y&1)^1][0];\
  728. const int dr2= dither_2x2_8[y&1 ][1];\
  729. const int dg2= dither_2x2_8[y&1 ][0];\
  730. const int db2= dither_2x2_8[(y&1)^1][1];\
  731. func(uint16_t,0)\
  732. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  733. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  734. }\
  735. }\
  736. break;\
  737. case PIX_FMT_RGB8:\
  738. case PIX_FMT_BGR8:\
  739. {\
  740. const uint8_t * const d64= dither_8x8_73[y&7];\
  741. const uint8_t * const d32= dither_8x8_32[y&7];\
  742. func(uint8_t,0)\
  743. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
  744. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
  745. }\
  746. }\
  747. break;\
  748. case PIX_FMT_RGB4:\
  749. case PIX_FMT_BGR4:\
  750. {\
  751. const uint8_t * const d64= dither_8x8_73 [y&7];\
  752. const uint8_t * const d128=dither_8x8_220[y&7];\
  753. func(uint8_t,0)\
  754. ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
  755. + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
  756. }\
  757. }\
  758. break;\
  759. case PIX_FMT_RGB4_BYTE:\
  760. case PIX_FMT_BGR4_BYTE:\
  761. {\
  762. const uint8_t * const d64= dither_8x8_73 [y&7];\
  763. const uint8_t * const d128=dither_8x8_220[y&7];\
  764. func(uint8_t,0)\
  765. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
  766. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
  767. }\
  768. }\
  769. break;\
  770. case PIX_FMT_MONOBLACK:\
  771. case PIX_FMT_MONOWHITE:\
  772. {\
  773. func_monoblack\
  774. }\
  775. break;\
  776. case PIX_FMT_YUYV422:\
  777. func2\
  778. ((uint8_t*)dest)[2*i2+0]= Y1;\
  779. ((uint8_t*)dest)[2*i2+1]= U;\
  780. ((uint8_t*)dest)[2*i2+2]= Y2;\
  781. ((uint8_t*)dest)[2*i2+3]= V;\
  782. } \
  783. break;\
  784. case PIX_FMT_UYVY422:\
  785. func2\
  786. ((uint8_t*)dest)[2*i2+0]= U;\
  787. ((uint8_t*)dest)[2*i2+1]= Y1;\
  788. ((uint8_t*)dest)[2*i2+2]= V;\
  789. ((uint8_t*)dest)[2*i2+3]= Y2;\
  790. } \
  791. break;\
  792. case PIX_FMT_GRAY16BE:\
  793. func_g16\
  794. ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
  795. ((uint8_t*)dest)[2*i2+1]= Y1;\
  796. ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
  797. ((uint8_t*)dest)[2*i2+3]= Y2;\
  798. } \
  799. break;\
  800. case PIX_FMT_GRAY16LE:\
  801. func_g16\
  802. ((uint8_t*)dest)[2*i2+0]= Y1;\
  803. ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
  804. ((uint8_t*)dest)[2*i2+2]= Y2;\
  805. ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
  806. } \
  807. break;\
  808. }
  809. static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  810. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  811. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  812. {
  813. int i;
  814. YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGBX_C, YSCALE_YUV_2_PACKEDX_C(void,0), YSCALE_YUV_2_GRAY16_C, YSCALE_YUV_2_MONOX_C)
  815. }
  816. static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  817. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  818. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  819. {
  820. int i;
  821. int step= c->dstFormatBpp/8;
  822. int aidx= 3;
  823. switch(c->dstFormat) {
  824. case PIX_FMT_ARGB:
  825. dest++;
  826. aidx= 0;
  827. case PIX_FMT_RGB24:
  828. aidx--;
  829. case PIX_FMT_RGBA:
  830. if (CONFIG_SMALL) {
  831. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
  832. YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
  833. dest[aidx]= needAlpha ? A : 255;
  834. dest[0]= R>>22;
  835. dest[1]= G>>22;
  836. dest[2]= B>>22;
  837. dest+= step;
  838. }
  839. } else {
  840. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  841. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
  842. dest[aidx]= A;
  843. dest[0]= R>>22;
  844. dest[1]= G>>22;
  845. dest[2]= B>>22;
  846. dest+= step;
  847. }
  848. } else {
  849. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
  850. dest[aidx]= 255;
  851. dest[0]= R>>22;
  852. dest[1]= G>>22;
  853. dest[2]= B>>22;
  854. dest+= step;
  855. }
  856. }
  857. }
  858. break;
  859. case PIX_FMT_ABGR:
  860. dest++;
  861. aidx= 0;
  862. case PIX_FMT_BGR24:
  863. aidx--;
  864. case PIX_FMT_BGRA:
  865. if (CONFIG_SMALL) {
  866. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
  867. YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
  868. dest[aidx]= needAlpha ? A : 255;
  869. dest[0]= B>>22;
  870. dest[1]= G>>22;
  871. dest[2]= R>>22;
  872. dest+= step;
  873. }
  874. } else {
  875. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  876. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
  877. dest[aidx]= A;
  878. dest[0]= B>>22;
  879. dest[1]= G>>22;
  880. dest[2]= R>>22;
  881. dest+= step;
  882. }
  883. } else {
  884. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
  885. dest[aidx]= 255;
  886. dest[0]= B>>22;
  887. dest[1]= G>>22;
  888. dest[2]= R>>22;
  889. dest+= step;
  890. }
  891. }
  892. }
  893. break;
  894. default:
  895. assert(0);
  896. }
  897. }
  898. static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
  899. {
  900. int i;
  901. uint8_t *ptr = plane + stride*y;
  902. for (i=0; i<height; i++) {
  903. memset(ptr, val, width);
  904. ptr += stride;
  905. }
  906. }
  907. static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, int width,
  908. uint32_t *unused)
  909. {
  910. int i;
  911. for (i = 0; i < width; i++) {
  912. int r = src[i*6+0];
  913. int g = src[i*6+2];
  914. int b = src[i*6+4];
  915. dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  916. }
  917. }
  918. static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
  919. const uint8_t *src1, const uint8_t *src2,
  920. int width, uint32_t *unused)
  921. {
  922. int i;
  923. assert(src1==src2);
  924. for (i = 0; i < width; i++) {
  925. int r = src1[6*i + 0];
  926. int g = src1[6*i + 2];
  927. int b = src1[6*i + 4];
  928. dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  929. dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  930. }
  931. }
  932. static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
  933. const uint8_t *src1, const uint8_t *src2,
  934. int width, uint32_t *unused)
  935. {
  936. int i;
  937. assert(src1==src2);
  938. for (i = 0; i < width; i++) {
  939. int r= src1[12*i + 0] + src1[12*i + 6];
  940. int g= src1[12*i + 2] + src1[12*i + 8];
  941. int b= src1[12*i + 4] + src1[12*i + 10];
  942. dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  943. dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  944. }
  945. }
  946. #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
  947. static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
  948. {\
  949. int i;\
  950. for (i=0; i<width; i++) {\
  951. int b= (((const type*)src)[i]>>shb)&maskb;\
  952. int g= (((const type*)src)[i]>>shg)&maskg;\
  953. int r= (((const type*)src)[i]>>shr)&maskr;\
  954. \
  955. dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
  956. }\
  957. }
  958. BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
  959. BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
  960. BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY , RGB2YUV_SHIFT+8)
  961. BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY , RGB2YUV_SHIFT+7)
  962. BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
  963. BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
  964. static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  965. {
  966. int i;
  967. for (i=0; i<width; i++) {
  968. dst[i]= src[4*i];
  969. }
  970. }
  971. #define BGR2UV(type, name, shr, shg, shb, maska, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S)\
  972. static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
  973. {\
  974. int i;\
  975. for (i=0; i<width; i++) {\
  976. int b= (((const type*)src)[i]&maskb)>>shb;\
  977. int g= (((const type*)src)[i]&maskg)>>shg;\
  978. int r= (((const type*)src)[i]&maskr)>>shr;\
  979. \
  980. dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
  981. dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
  982. }\
  983. }\
  984. static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
  985. {\
  986. int i;\
  987. for (i=0; i<width; i++) {\
  988. int pix0= ((const type*)src)[2*i+0];\
  989. int pix1= ((const type*)src)[2*i+1];\
  990. int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
  991. int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
  992. int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
  993. g&= maskg|(2*maskg);\
  994. \
  995. g>>=shg;\
  996. \
  997. dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
  998. dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
  999. }\
  1000. }
  1001. BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0xFF000000, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1002. BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0xFF000000, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1003. BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RU<<11, GU<<5, BU , RV<<11, GV<<5, BV , RGB2YUV_SHIFT+8)
  1004. BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RU<<10, GU<<5, BU , RV<<10, GV<<5, BV , RGB2YUV_SHIFT+7)
  1005. BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RU , GU<<5, BU<<11, RV , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
  1006. BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RU , GU<<5, BU<<10, RV , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
  1007. static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
  1008. {
  1009. int i;
  1010. for (i=0; i<width; i++) {
  1011. int d= src[i];
  1012. dst[i]= pal[d] & 0xFF;
  1013. }
  1014. }
  1015. static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
  1016. const uint8_t *src1, const uint8_t *src2,
  1017. long width, uint32_t *pal)
  1018. {
  1019. int i;
  1020. assert(src1 == src2);
  1021. for (i=0; i<width; i++) {
  1022. int p= pal[src1[i]];
  1023. dstU[i]= p>>8;
  1024. dstV[i]= p>>16;
  1025. }
  1026. }
  1027. static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  1028. {
  1029. int i, j;
  1030. for (i=0; i<width/8; i++) {
  1031. int d= ~src[i];
  1032. for(j=0; j<8; j++)
  1033. dst[8*i+j]= ((d>>(7-j))&1)*255;
  1034. }
  1035. }
  1036. static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  1037. {
  1038. int i, j;
  1039. for (i=0; i<width/8; i++) {
  1040. int d= src[i];
  1041. for(j=0; j<8; j++)
  1042. dst[8*i+j]= ((d>>(7-j))&1)*255;
  1043. }
  1044. }
  1045. //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
  1046. //Plain C versions
  1047. #if ((!HAVE_MMX || !CONFIG_GPL) && !HAVE_ALTIVEC) || CONFIG_RUNTIME_CPUDETECT
  1048. #define COMPILE_C
  1049. #endif
  1050. #if ARCH_PPC
  1051. #if HAVE_ALTIVEC || CONFIG_RUNTIME_CPUDETECT
  1052. #define COMPILE_ALTIVEC
  1053. #endif
  1054. #endif //ARCH_PPC
  1055. #if ARCH_X86
  1056. #if ((HAVE_MMX && !HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
  1057. #define COMPILE_MMX
  1058. #endif
  1059. #if (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
  1060. #define COMPILE_MMX2
  1061. #endif
  1062. #if ((HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
  1063. #define COMPILE_3DNOW
  1064. #endif
  1065. #endif //ARCH_X86
  1066. #define COMPILE_TEMPLATE_MMX 0
  1067. #define COMPILE_TEMPLATE_MMX2 0
  1068. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1069. #define COMPILE_TEMPLATE_ALTIVEC 0
  1070. #ifdef COMPILE_C
  1071. #define RENAME(a) a ## _C
  1072. #include "swscale_template.c"
  1073. #endif
  1074. #ifdef COMPILE_ALTIVEC
  1075. #undef RENAME
  1076. #undef COMPILE_TEMPLATE_ALTIVEC
  1077. #define COMPILE_TEMPLATE_ALTIVEC 1
  1078. #define RENAME(a) a ## _altivec
  1079. #include "swscale_template.c"
  1080. #endif
  1081. #if ARCH_X86
  1082. //MMX versions
  1083. #ifdef COMPILE_MMX
  1084. #undef RENAME
  1085. #undef COMPILE_TEMPLATE_MMX
  1086. #undef COMPILE_TEMPLATE_MMX2
  1087. #undef COMPILE_TEMPLATE_AMD3DNOW
  1088. #define COMPILE_TEMPLATE_MMX 1
  1089. #define COMPILE_TEMPLATE_MMX2 0
  1090. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1091. #define RENAME(a) a ## _MMX
  1092. #include "swscale_template.c"
  1093. #endif
  1094. //MMX2 versions
  1095. #ifdef COMPILE_MMX2
  1096. #undef RENAME
  1097. #undef COMPILE_TEMPLATE_MMX
  1098. #undef COMPILE_TEMPLATE_MMX2
  1099. #undef COMPILE_TEMPLATE_AMD3DNOW
  1100. #define COMPILE_TEMPLATE_MMX 1
  1101. #define COMPILE_TEMPLATE_MMX2 1
  1102. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1103. #define RENAME(a) a ## _MMX2
  1104. #include "swscale_template.c"
  1105. #endif
  1106. //3DNOW versions
  1107. #ifdef COMPILE_3DNOW
  1108. #undef RENAME
  1109. #undef COMPILE_TEMPLATE_MMX
  1110. #undef COMPILE_TEMPLATE_MMX2
  1111. #undef COMPILE_TEMPLATE_AMD3DNOW
  1112. #define COMPILE_TEMPLATE_MMX 1
  1113. #define COMPILE_TEMPLATE_MMX2 0
  1114. #define COMPILE_TEMPLATE_AMD3DNOW 1
  1115. #define RENAME(a) a ## _3DNow
  1116. #include "swscale_template.c"
  1117. #endif
  1118. #endif //ARCH_X86
  1119. SwsFunc ff_getSwsFunc(SwsContext *c)
  1120. {
  1121. #if CONFIG_RUNTIME_CPUDETECT
  1122. int flags = c->flags;
  1123. #if ARCH_X86 && CONFIG_GPL
  1124. // ordered per speed fastest first
  1125. if (flags & SWS_CPU_CAPS_MMX2) {
  1126. sws_init_swScale_MMX2(c);
  1127. return swScale_MMX2;
  1128. } else if (flags & SWS_CPU_CAPS_3DNOW) {
  1129. sws_init_swScale_3DNow(c);
  1130. return swScale_3DNow;
  1131. } else if (flags & SWS_CPU_CAPS_MMX) {
  1132. sws_init_swScale_MMX(c);
  1133. return swScale_MMX;
  1134. } else {
  1135. sws_init_swScale_C(c);
  1136. return swScale_C;
  1137. }
  1138. #else
  1139. #if ARCH_PPC
  1140. if (flags & SWS_CPU_CAPS_ALTIVEC) {
  1141. sws_init_swScale_altivec(c);
  1142. return swScale_altivec;
  1143. } else {
  1144. sws_init_swScale_C(c);
  1145. return swScale_C;
  1146. }
  1147. #endif
  1148. sws_init_swScale_C(c);
  1149. return swScale_C;
  1150. #endif /* ARCH_X86 && CONFIG_GPL */
  1151. #else //CONFIG_RUNTIME_CPUDETECT
  1152. #if COMPILE_TEMPLATE_MMX2
  1153. sws_init_swScale_MMX2(c);
  1154. return swScale_MMX2;
  1155. #elif COMPILE_TEMPLATE_AMD3DNOW
  1156. sws_init_swScale_3DNow(c);
  1157. return swScale_3DNow;
  1158. #elif COMPILE_TEMPLATE_MMX
  1159. sws_init_swScale_MMX(c);
  1160. return swScale_MMX;
  1161. #elif COMPILE_TEMPLATE_ALTIVEC
  1162. sws_init_swScale_altivec(c);
  1163. return swScale_altivec;
  1164. #else
  1165. sws_init_swScale_C(c);
  1166. return swScale_C;
  1167. #endif
  1168. #endif //!CONFIG_RUNTIME_CPUDETECT
  1169. }
  1170. static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1171. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1172. {
  1173. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1174. /* Copy Y plane */
  1175. if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
  1176. memcpy(dst, src[0], srcSliceH*dstStride[0]);
  1177. else {
  1178. int i;
  1179. const uint8_t *srcPtr= src[0];
  1180. uint8_t *dstPtr= dst;
  1181. for (i=0; i<srcSliceH; i++) {
  1182. memcpy(dstPtr, srcPtr, c->srcW);
  1183. srcPtr+= srcStride[0];
  1184. dstPtr+= dstStride[0];
  1185. }
  1186. }
  1187. dst = dstParam[1] + dstStride[1]*srcSliceY/2;
  1188. if (c->dstFormat == PIX_FMT_NV12)
  1189. interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
  1190. else
  1191. interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
  1192. return srcSliceH;
  1193. }
  1194. static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1195. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1196. {
  1197. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1198. yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
  1199. return srcSliceH;
  1200. }
  1201. static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1202. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1203. {
  1204. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1205. yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
  1206. return srcSliceH;
  1207. }
  1208. static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1209. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1210. {
  1211. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1212. yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
  1213. return srcSliceH;
  1214. }
  1215. static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1216. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1217. {
  1218. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1219. yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
  1220. return srcSliceH;
  1221. }
  1222. static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1223. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1224. {
  1225. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1226. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
  1227. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
  1228. yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1229. if (dstParam[3])
  1230. fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1231. return srcSliceH;
  1232. }
  1233. static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1234. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1235. {
  1236. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1237. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
  1238. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
  1239. yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1240. return srcSliceH;
  1241. }
  1242. static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1243. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1244. {
  1245. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1246. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
  1247. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
  1248. uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1249. if (dstParam[3])
  1250. fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1251. return srcSliceH;
  1252. }
  1253. static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1254. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1255. {
  1256. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1257. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
  1258. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
  1259. uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1260. return srcSliceH;
  1261. }
  1262. static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1263. int srcSliceH, uint8_t* dst[], int dstStride[])
  1264. {
  1265. const enum PixelFormat srcFormat= c->srcFormat;
  1266. const enum PixelFormat dstFormat= c->dstFormat;
  1267. void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
  1268. const uint8_t *palette)=NULL;
  1269. int i;
  1270. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1271. const uint8_t *srcPtr= src[0];
  1272. if (usePal(srcFormat)) {
  1273. switch (dstFormat) {
  1274. case PIX_FMT_RGB32 : conv = palette8topacked32; break;
  1275. case PIX_FMT_BGR32 : conv = palette8topacked32; break;
  1276. case PIX_FMT_BGR32_1: conv = palette8topacked32; break;
  1277. case PIX_FMT_RGB32_1: conv = palette8topacked32; break;
  1278. case PIX_FMT_RGB24 : conv = palette8topacked24; break;
  1279. case PIX_FMT_BGR24 : conv = palette8topacked24; break;
  1280. }
  1281. }
  1282. if (!conv)
  1283. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1284. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1285. else {
  1286. for (i=0; i<srcSliceH; i++) {
  1287. conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
  1288. srcPtr+= srcStride[0];
  1289. dstPtr+= dstStride[0];
  1290. }
  1291. }
  1292. return srcSliceH;
  1293. }
  1294. #define isRGBA32(x) ( \
  1295. (x) == PIX_FMT_ARGB \
  1296. || (x) == PIX_FMT_RGBA \
  1297. || (x) == PIX_FMT_BGRA \
  1298. || (x) == PIX_FMT_ABGR \
  1299. )
  1300. /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
  1301. static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1302. int srcSliceH, uint8_t* dst[], int dstStride[])
  1303. {
  1304. const enum PixelFormat srcFormat= c->srcFormat;
  1305. const enum PixelFormat dstFormat= c->dstFormat;
  1306. const int srcBpp= (c->srcFormatBpp + 7) >> 3;
  1307. const int dstBpp= (c->dstFormatBpp + 7) >> 3;
  1308. const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
  1309. const int dstId= c->dstFormatBpp >> 2;
  1310. void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
  1311. #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
  1312. if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
  1313. if ( CONV_IS(ABGR, RGBA)
  1314. || CONV_IS(ARGB, BGRA)
  1315. || CONV_IS(BGRA, ARGB)
  1316. || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
  1317. else if (CONV_IS(ABGR, ARGB)
  1318. || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
  1319. else if (CONV_IS(ABGR, BGRA)
  1320. || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
  1321. else if (CONV_IS(BGRA, RGBA)
  1322. || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
  1323. else if (CONV_IS(BGRA, ABGR)
  1324. || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
  1325. } else
  1326. /* BGR -> BGR */
  1327. if ( (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
  1328. || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
  1329. switch(srcId | (dstId<<4)) {
  1330. case 0x34: conv= rgb16to15; break;
  1331. case 0x36: conv= rgb24to15; break;
  1332. case 0x38: conv= rgb32to15; break;
  1333. case 0x43: conv= rgb15to16; break;
  1334. case 0x46: conv= rgb24to16; break;
  1335. case 0x48: conv= rgb32to16; break;
  1336. case 0x63: conv= rgb15to24; break;
  1337. case 0x64: conv= rgb16to24; break;
  1338. case 0x68: conv= rgb32to24; break;
  1339. case 0x83: conv= rgb15to32; break;
  1340. case 0x84: conv= rgb16to32; break;
  1341. case 0x86: conv= rgb24to32; break;
  1342. }
  1343. } else if ( (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
  1344. || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
  1345. switch(srcId | (dstId<<4)) {
  1346. case 0x33: conv= rgb15tobgr15; break;
  1347. case 0x34: conv= rgb16tobgr15; break;
  1348. case 0x36: conv= rgb24tobgr15; break;
  1349. case 0x38: conv= rgb32tobgr15; break;
  1350. case 0x43: conv= rgb15tobgr16; break;
  1351. case 0x44: conv= rgb16tobgr16; break;
  1352. case 0x46: conv= rgb24tobgr16; break;
  1353. case 0x48: conv= rgb32tobgr16; break;
  1354. case 0x63: conv= rgb15tobgr24; break;
  1355. case 0x64: conv= rgb16tobgr24; break;
  1356. case 0x66: conv= rgb24tobgr24; break;
  1357. case 0x68: conv= rgb32tobgr24; break;
  1358. case 0x83: conv= rgb15tobgr32; break;
  1359. case 0x84: conv= rgb16tobgr32; break;
  1360. case 0x86: conv= rgb24tobgr32; break;
  1361. }
  1362. }
  1363. if (!conv) {
  1364. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1365. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1366. } else {
  1367. const uint8_t *srcPtr= src[0];
  1368. uint8_t *dstPtr= dst[0];
  1369. if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
  1370. srcPtr += ALT32_CORR;
  1371. if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
  1372. dstPtr += ALT32_CORR;
  1373. if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
  1374. conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
  1375. else {
  1376. int i;
  1377. dstPtr += dstStride[0]*srcSliceY;
  1378. for (i=0; i<srcSliceH; i++) {
  1379. conv(srcPtr, dstPtr, c->srcW*srcBpp);
  1380. srcPtr+= srcStride[0];
  1381. dstPtr+= dstStride[0];
  1382. }
  1383. }
  1384. }
  1385. return srcSliceH;
  1386. }
  1387. static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1388. int srcSliceH, uint8_t* dst[], int dstStride[])
  1389. {
  1390. rgb24toyv12(
  1391. src[0],
  1392. dst[0]+ srcSliceY *dstStride[0],
  1393. dst[1]+(srcSliceY>>1)*dstStride[1],
  1394. dst[2]+(srcSliceY>>1)*dstStride[2],
  1395. c->srcW, srcSliceH,
  1396. dstStride[0], dstStride[1], srcStride[0]);
  1397. if (dst[3])
  1398. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1399. return srcSliceH;
  1400. }
  1401. static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1402. int srcSliceH, uint8_t* dst[], int dstStride[])
  1403. {
  1404. int i;
  1405. /* copy Y */
  1406. if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
  1407. memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
  1408. else {
  1409. const uint8_t *srcPtr= src[0];
  1410. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1411. for (i=0; i<srcSliceH; i++) {
  1412. memcpy(dstPtr, srcPtr, c->srcW);
  1413. srcPtr+= srcStride[0];
  1414. dstPtr+= dstStride[0];
  1415. }
  1416. }
  1417. if (c->dstFormat==PIX_FMT_YUV420P || c->dstFormat==PIX_FMT_YUVA420P) {
  1418. planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
  1419. srcSliceH >> 2, srcStride[1], dstStride[1]);
  1420. planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
  1421. srcSliceH >> 2, srcStride[2], dstStride[2]);
  1422. } else {
  1423. planar2x(src[1], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
  1424. srcSliceH >> 2, srcStride[1], dstStride[2]);
  1425. planar2x(src[2], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
  1426. srcSliceH >> 2, srcStride[2], dstStride[1]);
  1427. }
  1428. if (dst[3])
  1429. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1430. return srcSliceH;
  1431. }
  1432. /* unscaled copy like stuff (assumes nearly identical formats) */
  1433. static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1434. int srcSliceH, uint8_t* dst[], int dstStride[])
  1435. {
  1436. if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
  1437. memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
  1438. else {
  1439. int i;
  1440. const uint8_t *srcPtr= src[0];
  1441. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1442. int length=0;
  1443. /* universal length finder */
  1444. while(length+c->srcW <= FFABS(dstStride[0])
  1445. && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
  1446. assert(length!=0);
  1447. for (i=0; i<srcSliceH; i++) {
  1448. memcpy(dstPtr, srcPtr, length);
  1449. srcPtr+= srcStride[0];
  1450. dstPtr+= dstStride[0];
  1451. }
  1452. }
  1453. return srcSliceH;
  1454. }
  1455. static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1456. int srcSliceH, uint8_t* dst[], int dstStride[])
  1457. {
  1458. int plane, i, j;
  1459. for (plane=0; plane<4; plane++) {
  1460. int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
  1461. int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
  1462. int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
  1463. const uint8_t *srcPtr= src[plane];
  1464. uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
  1465. if (!dst[plane]) continue;
  1466. // ignore palette for GRAY8
  1467. if (plane == 1 && !dst[2]) continue;
  1468. if (!src[plane] || (plane == 1 && !src[2])) {
  1469. if(is16BPS(c->dstFormat))
  1470. length*=2;
  1471. fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
  1472. } else {
  1473. if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
  1474. if (!isBE(c->srcFormat)) srcPtr++;
  1475. for (i=0; i<height; i++) {
  1476. for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
  1477. srcPtr+= srcStride[plane];
  1478. dstPtr+= dstStride[plane];
  1479. }
  1480. } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
  1481. for (i=0; i<height; i++) {
  1482. for (j=0; j<length; j++) {
  1483. dstPtr[ j<<1 ] = srcPtr[j];
  1484. dstPtr[(j<<1)+1] = srcPtr[j];
  1485. }
  1486. srcPtr+= srcStride[plane];
  1487. dstPtr+= dstStride[plane];
  1488. }
  1489. } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
  1490. && isBE(c->srcFormat) != isBE(c->dstFormat)) {
  1491. for (i=0; i<height; i++) {
  1492. for (j=0; j<length; j++)
  1493. ((uint16_t*)dstPtr)[j] = bswap_16(((const uint16_t*)srcPtr)[j]);
  1494. srcPtr+= srcStride[plane];
  1495. dstPtr+= dstStride[plane];
  1496. }
  1497. } else if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
  1498. memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
  1499. else {
  1500. if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
  1501. length*=2;
  1502. for (i=0; i<height; i++) {
  1503. memcpy(dstPtr, srcPtr, length);
  1504. srcPtr+= srcStride[plane];
  1505. dstPtr+= dstStride[plane];
  1506. }
  1507. }
  1508. }
  1509. }
  1510. return srcSliceH;
  1511. }
  1512. int ff_hardcodedcpuflags(void)
  1513. {
  1514. int flags = 0;
  1515. #if COMPILE_TEMPLATE_MMX2
  1516. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
  1517. #elif COMPILE_TEMPLATE_AMD3DNOW
  1518. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
  1519. #elif COMPILE_TEMPLATE_MMX
  1520. flags |= SWS_CPU_CAPS_MMX;
  1521. #elif COMPILE_TEMPLATE_ALTIVEC
  1522. flags |= SWS_CPU_CAPS_ALTIVEC;
  1523. #elif ARCH_BFIN
  1524. flags |= SWS_CPU_CAPS_BFIN;
  1525. #endif
  1526. return flags;
  1527. }
  1528. void ff_get_unscaled_swscale(SwsContext *c)
  1529. {
  1530. const enum PixelFormat srcFormat = c->srcFormat;
  1531. const enum PixelFormat dstFormat = c->dstFormat;
  1532. const int flags = c->flags;
  1533. const int dstH = c->dstH;
  1534. int needsDither;
  1535. needsDither= isAnyRGB(dstFormat)
  1536. && c->dstFormatBpp < 24
  1537. && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
  1538. /* yv12_to_nv12 */
  1539. if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
  1540. c->swScale= planarToNv12Wrapper;
  1541. }
  1542. /* yuv2bgr */
  1543. if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
  1544. && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
  1545. c->swScale= ff_yuv2rgb_get_func_ptr(c);
  1546. }
  1547. if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
  1548. c->swScale= yvu9ToYv12Wrapper;
  1549. }
  1550. /* bgr24toYV12 */
  1551. if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
  1552. c->swScale= bgr24ToYv12Wrapper;
  1553. /* RGB/BGR -> RGB/BGR (no dither needed forms) */
  1554. if ( isAnyRGB(srcFormat)
  1555. && isAnyRGB(dstFormat)
  1556. && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
  1557. && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
  1558. && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
  1559. && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
  1560. && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
  1561. && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
  1562. && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
  1563. && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
  1564. && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
  1565. && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
  1566. && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
  1567. c->swScale= rgbToRgbWrapper;
  1568. if ((usePal(srcFormat) && (
  1569. dstFormat == PIX_FMT_RGB32 ||
  1570. dstFormat == PIX_FMT_RGB32_1 ||
  1571. dstFormat == PIX_FMT_RGB24 ||
  1572. dstFormat == PIX_FMT_BGR32 ||
  1573. dstFormat == PIX_FMT_BGR32_1 ||
  1574. dstFormat == PIX_FMT_BGR24)))
  1575. c->swScale= palToRgbWrapper;
  1576. if (srcFormat == PIX_FMT_YUV422P) {
  1577. if (dstFormat == PIX_FMT_YUYV422)
  1578. c->swScale= yuv422pToYuy2Wrapper;
  1579. else if (dstFormat == PIX_FMT_UYVY422)
  1580. c->swScale= yuv422pToUyvyWrapper;
  1581. }
  1582. /* LQ converters if -sws 0 or -sws 4*/
  1583. if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
  1584. /* yv12_to_yuy2 */
  1585. if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
  1586. if (dstFormat == PIX_FMT_YUYV422)
  1587. c->swScale= planarToYuy2Wrapper;
  1588. else if (dstFormat == PIX_FMT_UYVY422)
  1589. c->swScale= planarToUyvyWrapper;
  1590. }
  1591. }
  1592. if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  1593. c->swScale= yuyvToYuv420Wrapper;
  1594. if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  1595. c->swScale= uyvyToYuv420Wrapper;
  1596. if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
  1597. c->swScale= yuyvToYuv422Wrapper;
  1598. if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
  1599. c->swScale= uyvyToYuv422Wrapper;
  1600. #ifdef COMPILE_ALTIVEC
  1601. if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
  1602. !(c->flags & SWS_BITEXACT) &&
  1603. srcFormat == PIX_FMT_YUV420P) {
  1604. // unscaled YV12 -> packed YUV, we want speed
  1605. if (dstFormat == PIX_FMT_YUYV422)
  1606. c->swScale= yv12toyuy2_unscaled_altivec;
  1607. else if (dstFormat == PIX_FMT_UYVY422)
  1608. c->swScale= yv12touyvy_unscaled_altivec;
  1609. }
  1610. #endif
  1611. /* simple copy */
  1612. if ( srcFormat == dstFormat
  1613. || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
  1614. || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
  1615. || (isPlanarYUV(srcFormat) && isGray(dstFormat))
  1616. || (isPlanarYUV(dstFormat) && isGray(srcFormat))
  1617. || (isGray(dstFormat) && isGray(srcFormat))
  1618. || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
  1619. && c->chrDstHSubSample == c->chrSrcHSubSample
  1620. && c->chrDstVSubSample == c->chrSrcVSubSample
  1621. && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
  1622. && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
  1623. {
  1624. if (isPacked(c->srcFormat))
  1625. c->swScale= packedCopyWrapper;
  1626. else /* Planar YUV or gray */
  1627. c->swScale= planarCopyWrapper;
  1628. }
  1629. #if ARCH_BFIN
  1630. if (flags & SWS_CPU_CAPS_BFIN)
  1631. ff_bfin_get_unscaled_swscale (c);
  1632. #endif
  1633. }
  1634. static void reset_ptr(const uint8_t* src[], int format)
  1635. {
  1636. if(!isALPHA(format))
  1637. src[3]=NULL;
  1638. if(!isPlanarYUV(format)) {
  1639. src[3]=src[2]=NULL;
  1640. if (!usePal(format))
  1641. src[1]= NULL;
  1642. }
  1643. }
  1644. /**
  1645. * swscale wrapper, so we don't need to export the SwsContext.
  1646. * Assumes planar YUV to be in YUV order instead of YVU.
  1647. */
  1648. int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
  1649. int srcSliceH, uint8_t* const dst[], const int dstStride[])
  1650. {
  1651. int i;
  1652. const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
  1653. uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
  1654. // do not mess up sliceDir if we have a "trailing" 0-size slice
  1655. if (srcSliceH == 0)
  1656. return 0;
  1657. if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
  1658. av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
  1659. return 0;
  1660. }
  1661. if (c->sliceDir == 0) {
  1662. if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
  1663. }
  1664. if (usePal(c->srcFormat)) {
  1665. for (i=0; i<256; i++) {
  1666. int p, r, g, b,y,u,v;
  1667. if(c->srcFormat == PIX_FMT_PAL8) {
  1668. p=((const uint32_t*)(src[1]))[i];
  1669. r= (p>>16)&0xFF;
  1670. g= (p>> 8)&0xFF;
  1671. b= p &0xFF;
  1672. } else if(c->srcFormat == PIX_FMT_RGB8) {
  1673. r= (i>>5 )*36;
  1674. g= ((i>>2)&7)*36;
  1675. b= (i&3 )*85;
  1676. } else if(c->srcFormat == PIX_FMT_BGR8) {
  1677. b= (i>>6 )*85;
  1678. g= ((i>>3)&7)*36;
  1679. r= (i&7 )*36;
  1680. } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
  1681. r= (i>>3 )*255;
  1682. g= ((i>>1)&3)*85;
  1683. b= (i&1 )*255;
  1684. } else if(c->srcFormat == PIX_FMT_GRAY8) {
  1685. r = g = b = i;
  1686. } else {
  1687. assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
  1688. b= (i>>3 )*255;
  1689. g= ((i>>1)&3)*85;
  1690. r= (i&1 )*255;
  1691. }
  1692. y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1693. u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1694. v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1695. c->pal_yuv[i]= y + (u<<8) + (v<<16);
  1696. switch(c->dstFormat) {
  1697. case PIX_FMT_BGR32:
  1698. #if !HAVE_BIGENDIAN
  1699. case PIX_FMT_RGB24:
  1700. #endif
  1701. c->pal_rgb[i]= r + (g<<8) + (b<<16);
  1702. break;
  1703. case PIX_FMT_BGR32_1:
  1704. #if HAVE_BIGENDIAN
  1705. case PIX_FMT_BGR24:
  1706. #endif
  1707. c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
  1708. break;
  1709. case PIX_FMT_RGB32_1:
  1710. #if HAVE_BIGENDIAN
  1711. case PIX_FMT_RGB24:
  1712. #endif
  1713. c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
  1714. break;
  1715. case PIX_FMT_RGB32:
  1716. #if !HAVE_BIGENDIAN
  1717. case PIX_FMT_BGR24:
  1718. #endif
  1719. default:
  1720. c->pal_rgb[i]= b + (g<<8) + (r<<16);
  1721. }
  1722. }
  1723. }
  1724. // copy strides, so they can safely be modified
  1725. if (c->sliceDir == 1) {
  1726. // slices go from top to bottom
  1727. int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
  1728. int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
  1729. reset_ptr(src2, c->srcFormat);
  1730. reset_ptr((const uint8_t**)dst2, c->dstFormat);
  1731. /* reset slice direction at end of frame */
  1732. if (srcSliceY + srcSliceH == c->srcH)
  1733. c->sliceDir = 0;
  1734. return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
  1735. } else {
  1736. // slices go from bottom to top => we flip the image internally
  1737. int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
  1738. int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
  1739. src2[0] += (srcSliceH-1)*srcStride[0];
  1740. if (!usePal(c->srcFormat))
  1741. src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
  1742. src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
  1743. src2[3] += (srcSliceH-1)*srcStride[3];
  1744. dst2[0] += ( c->dstH -1)*dstStride[0];
  1745. dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
  1746. dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
  1747. dst2[3] += ( c->dstH -1)*dstStride[3];
  1748. reset_ptr(src2, c->srcFormat);
  1749. reset_ptr((const uint8_t**)dst2, c->dstFormat);
  1750. /* reset slice direction at end of frame */
  1751. if (!srcSliceY)
  1752. c->sliceDir = 0;
  1753. return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
  1754. }
  1755. }
  1756. #if LIBSWSCALE_VERSION_MAJOR < 1
  1757. int sws_scale_ordered(SwsContext *c, const uint8_t* const src[], int srcStride[], int srcSliceY,
  1758. int srcSliceH, uint8_t* dst[], int dstStride[])
  1759. {
  1760. return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
  1761. }
  1762. #endif