utils.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430
  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
  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 "config.h"
  21. #define _DEFAULT_SOURCE
  22. #define _SVID_SOURCE // needed for MAP_ANONYMOUS
  23. #define _DARWIN_C_SOURCE // needed for MAP_ANON
  24. #include <inttypes.h>
  25. #include <math.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #if HAVE_MMAP
  29. #include <sys/mman.h>
  30. #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
  31. #define MAP_ANONYMOUS MAP_ANON
  32. #endif
  33. #endif
  34. #if HAVE_VIRTUALALLOC
  35. #define WIN32_LEAN_AND_MEAN
  36. #include <windows.h>
  37. #endif
  38. #include "libavutil/attributes.h"
  39. #include "libavutil/avassert.h"
  40. #include "libavutil/avutil.h"
  41. #include "libavutil/bswap.h"
  42. #include "libavutil/cpu.h"
  43. #include "libavutil/imgutils.h"
  44. #include "libavutil/intreadwrite.h"
  45. #include "libavutil/libm.h"
  46. #include "libavutil/mathematics.h"
  47. #include "libavutil/opt.h"
  48. #include "libavutil/pixdesc.h"
  49. #include "libavutil/aarch64/cpu.h"
  50. #include "libavutil/ppc/cpu.h"
  51. #include "libavutil/x86/asm.h"
  52. #include "libavutil/x86/cpu.h"
  53. // We have to implement deprecated functions until they are removed, this is the
  54. // simplest way to prevent warnings
  55. #undef attribute_deprecated
  56. #define attribute_deprecated
  57. #include "rgb2rgb.h"
  58. #include "swscale.h"
  59. #include "swscale_internal.h"
  60. #if !FF_API_SWS_VECTOR
  61. static SwsVector *sws_getIdentityVec(void);
  62. static void sws_addVec(SwsVector *a, SwsVector *b);
  63. static void sws_shiftVec(SwsVector *a, int shift);
  64. static void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level);
  65. #endif
  66. static void handle_formats(SwsContext *c);
  67. unsigned swscale_version(void)
  68. {
  69. av_assert0(LIBSWSCALE_VERSION_MICRO >= 100);
  70. return LIBSWSCALE_VERSION_INT;
  71. }
  72. const char *swscale_configuration(void)
  73. {
  74. return FFMPEG_CONFIGURATION;
  75. }
  76. const char *swscale_license(void)
  77. {
  78. #define LICENSE_PREFIX "libswscale license: "
  79. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  80. }
  81. typedef struct FormatEntry {
  82. uint8_t is_supported_in :1;
  83. uint8_t is_supported_out :1;
  84. uint8_t is_supported_endianness :1;
  85. } FormatEntry;
  86. static const FormatEntry format_entries[AV_PIX_FMT_NB] = {
  87. [AV_PIX_FMT_YUV420P] = { 1, 1 },
  88. [AV_PIX_FMT_YUYV422] = { 1, 1 },
  89. [AV_PIX_FMT_RGB24] = { 1, 1 },
  90. [AV_PIX_FMT_BGR24] = { 1, 1 },
  91. [AV_PIX_FMT_YUV422P] = { 1, 1 },
  92. [AV_PIX_FMT_YUV444P] = { 1, 1 },
  93. [AV_PIX_FMT_YUV410P] = { 1, 1 },
  94. [AV_PIX_FMT_YUV411P] = { 1, 1 },
  95. [AV_PIX_FMT_GRAY8] = { 1, 1 },
  96. [AV_PIX_FMT_MONOWHITE] = { 1, 1 },
  97. [AV_PIX_FMT_MONOBLACK] = { 1, 1 },
  98. [AV_PIX_FMT_PAL8] = { 1, 0 },
  99. [AV_PIX_FMT_YUVJ420P] = { 1, 1 },
  100. [AV_PIX_FMT_YUVJ411P] = { 1, 1 },
  101. [AV_PIX_FMT_YUVJ422P] = { 1, 1 },
  102. [AV_PIX_FMT_YUVJ444P] = { 1, 1 },
  103. [AV_PIX_FMT_YVYU422] = { 1, 1 },
  104. [AV_PIX_FMT_UYVY422] = { 1, 1 },
  105. [AV_PIX_FMT_UYYVYY411] = { 0, 0 },
  106. [AV_PIX_FMT_BGR8] = { 1, 1 },
  107. [AV_PIX_FMT_BGR4] = { 0, 1 },
  108. [AV_PIX_FMT_BGR4_BYTE] = { 1, 1 },
  109. [AV_PIX_FMT_RGB8] = { 1, 1 },
  110. [AV_PIX_FMT_RGB4] = { 0, 1 },
  111. [AV_PIX_FMT_RGB4_BYTE] = { 1, 1 },
  112. [AV_PIX_FMT_NV12] = { 1, 1 },
  113. [AV_PIX_FMT_NV21] = { 1, 1 },
  114. [AV_PIX_FMT_ARGB] = { 1, 1 },
  115. [AV_PIX_FMT_RGBA] = { 1, 1 },
  116. [AV_PIX_FMT_ABGR] = { 1, 1 },
  117. [AV_PIX_FMT_BGRA] = { 1, 1 },
  118. [AV_PIX_FMT_0RGB] = { 1, 1 },
  119. [AV_PIX_FMT_RGB0] = { 1, 1 },
  120. [AV_PIX_FMT_0BGR] = { 1, 1 },
  121. [AV_PIX_FMT_BGR0] = { 1, 1 },
  122. [AV_PIX_FMT_GRAY9BE] = { 1, 1 },
  123. [AV_PIX_FMT_GRAY9LE] = { 1, 1 },
  124. [AV_PIX_FMT_GRAY10BE] = { 1, 1 },
  125. [AV_PIX_FMT_GRAY10LE] = { 1, 1 },
  126. [AV_PIX_FMT_GRAY12BE] = { 1, 1 },
  127. [AV_PIX_FMT_GRAY12LE] = { 1, 1 },
  128. [AV_PIX_FMT_GRAY14BE] = { 1, 1 },
  129. [AV_PIX_FMT_GRAY14LE] = { 1, 1 },
  130. [AV_PIX_FMT_GRAY16BE] = { 1, 1 },
  131. [AV_PIX_FMT_GRAY16LE] = { 1, 1 },
  132. [AV_PIX_FMT_YUV440P] = { 1, 1 },
  133. [AV_PIX_FMT_YUVJ440P] = { 1, 1 },
  134. [AV_PIX_FMT_YUV440P10LE] = { 1, 1 },
  135. [AV_PIX_FMT_YUV440P10BE] = { 1, 1 },
  136. [AV_PIX_FMT_YUV440P12LE] = { 1, 1 },
  137. [AV_PIX_FMT_YUV440P12BE] = { 1, 1 },
  138. [AV_PIX_FMT_YUVA420P] = { 1, 1 },
  139. [AV_PIX_FMT_YUVA422P] = { 1, 1 },
  140. [AV_PIX_FMT_YUVA444P] = { 1, 1 },
  141. [AV_PIX_FMT_YUVA420P9BE] = { 1, 1 },
  142. [AV_PIX_FMT_YUVA420P9LE] = { 1, 1 },
  143. [AV_PIX_FMT_YUVA422P9BE] = { 1, 1 },
  144. [AV_PIX_FMT_YUVA422P9LE] = { 1, 1 },
  145. [AV_PIX_FMT_YUVA444P9BE] = { 1, 1 },
  146. [AV_PIX_FMT_YUVA444P9LE] = { 1, 1 },
  147. [AV_PIX_FMT_YUVA420P10BE]= { 1, 1 },
  148. [AV_PIX_FMT_YUVA420P10LE]= { 1, 1 },
  149. [AV_PIX_FMT_YUVA422P10BE]= { 1, 1 },
  150. [AV_PIX_FMT_YUVA422P10LE]= { 1, 1 },
  151. [AV_PIX_FMT_YUVA444P10BE]= { 1, 1 },
  152. [AV_PIX_FMT_YUVA444P10LE]= { 1, 1 },
  153. [AV_PIX_FMT_YUVA420P16BE]= { 1, 1 },
  154. [AV_PIX_FMT_YUVA420P16LE]= { 1, 1 },
  155. [AV_PIX_FMT_YUVA422P16BE]= { 1, 1 },
  156. [AV_PIX_FMT_YUVA422P16LE]= { 1, 1 },
  157. [AV_PIX_FMT_YUVA444P16BE]= { 1, 1 },
  158. [AV_PIX_FMT_YUVA444P16LE]= { 1, 1 },
  159. [AV_PIX_FMT_RGB48BE] = { 1, 1 },
  160. [AV_PIX_FMT_RGB48LE] = { 1, 1 },
  161. [AV_PIX_FMT_RGBA64BE] = { 1, 1, 1 },
  162. [AV_PIX_FMT_RGBA64LE] = { 1, 1, 1 },
  163. [AV_PIX_FMT_RGB565BE] = { 1, 1 },
  164. [AV_PIX_FMT_RGB565LE] = { 1, 1 },
  165. [AV_PIX_FMT_RGB555BE] = { 1, 1 },
  166. [AV_PIX_FMT_RGB555LE] = { 1, 1 },
  167. [AV_PIX_FMT_BGR565BE] = { 1, 1 },
  168. [AV_PIX_FMT_BGR565LE] = { 1, 1 },
  169. [AV_PIX_FMT_BGR555BE] = { 1, 1 },
  170. [AV_PIX_FMT_BGR555LE] = { 1, 1 },
  171. [AV_PIX_FMT_YUV420P16LE] = { 1, 1 },
  172. [AV_PIX_FMT_YUV420P16BE] = { 1, 1 },
  173. [AV_PIX_FMT_YUV422P16LE] = { 1, 1 },
  174. [AV_PIX_FMT_YUV422P16BE] = { 1, 1 },
  175. [AV_PIX_FMT_YUV444P16LE] = { 1, 1 },
  176. [AV_PIX_FMT_YUV444P16BE] = { 1, 1 },
  177. [AV_PIX_FMT_RGB444LE] = { 1, 1 },
  178. [AV_PIX_FMT_RGB444BE] = { 1, 1 },
  179. [AV_PIX_FMT_BGR444LE] = { 1, 1 },
  180. [AV_PIX_FMT_BGR444BE] = { 1, 1 },
  181. [AV_PIX_FMT_YA8] = { 1, 1 },
  182. [AV_PIX_FMT_YA16BE] = { 1, 1 },
  183. [AV_PIX_FMT_YA16LE] = { 1, 1 },
  184. [AV_PIX_FMT_BGR48BE] = { 1, 1 },
  185. [AV_PIX_FMT_BGR48LE] = { 1, 1 },
  186. [AV_PIX_FMT_BGRA64BE] = { 1, 1, 1 },
  187. [AV_PIX_FMT_BGRA64LE] = { 1, 1, 1 },
  188. [AV_PIX_FMT_YUV420P9BE] = { 1, 1 },
  189. [AV_PIX_FMT_YUV420P9LE] = { 1, 1 },
  190. [AV_PIX_FMT_YUV420P10BE] = { 1, 1 },
  191. [AV_PIX_FMT_YUV420P10LE] = { 1, 1 },
  192. [AV_PIX_FMT_YUV420P12BE] = { 1, 1 },
  193. [AV_PIX_FMT_YUV420P12LE] = { 1, 1 },
  194. [AV_PIX_FMT_YUV420P14BE] = { 1, 1 },
  195. [AV_PIX_FMT_YUV420P14LE] = { 1, 1 },
  196. [AV_PIX_FMT_YUV422P9BE] = { 1, 1 },
  197. [AV_PIX_FMT_YUV422P9LE] = { 1, 1 },
  198. [AV_PIX_FMT_YUV422P10BE] = { 1, 1 },
  199. [AV_PIX_FMT_YUV422P10LE] = { 1, 1 },
  200. [AV_PIX_FMT_YUV422P12BE] = { 1, 1 },
  201. [AV_PIX_FMT_YUV422P12LE] = { 1, 1 },
  202. [AV_PIX_FMT_YUV422P14BE] = { 1, 1 },
  203. [AV_PIX_FMT_YUV422P14LE] = { 1, 1 },
  204. [AV_PIX_FMT_YUV444P9BE] = { 1, 1 },
  205. [AV_PIX_FMT_YUV444P9LE] = { 1, 1 },
  206. [AV_PIX_FMT_YUV444P10BE] = { 1, 1 },
  207. [AV_PIX_FMT_YUV444P10LE] = { 1, 1 },
  208. [AV_PIX_FMT_YUV444P12BE] = { 1, 1 },
  209. [AV_PIX_FMT_YUV444P12LE] = { 1, 1 },
  210. [AV_PIX_FMT_YUV444P14BE] = { 1, 1 },
  211. [AV_PIX_FMT_YUV444P14LE] = { 1, 1 },
  212. [AV_PIX_FMT_GBRP] = { 1, 1 },
  213. [AV_PIX_FMT_GBRP9LE] = { 1, 1 },
  214. [AV_PIX_FMT_GBRP9BE] = { 1, 1 },
  215. [AV_PIX_FMT_GBRP10LE] = { 1, 1 },
  216. [AV_PIX_FMT_GBRP10BE] = { 1, 1 },
  217. [AV_PIX_FMT_GBRAP10LE] = { 1, 1 },
  218. [AV_PIX_FMT_GBRAP10BE] = { 1, 1 },
  219. [AV_PIX_FMT_GBRP12LE] = { 1, 1 },
  220. [AV_PIX_FMT_GBRP12BE] = { 1, 1 },
  221. [AV_PIX_FMT_GBRAP12LE] = { 1, 1 },
  222. [AV_PIX_FMT_GBRAP12BE] = { 1, 1 },
  223. [AV_PIX_FMT_GBRP14LE] = { 1, 1 },
  224. [AV_PIX_FMT_GBRP14BE] = { 1, 1 },
  225. [AV_PIX_FMT_GBRP16LE] = { 1, 1 },
  226. [AV_PIX_FMT_GBRP16BE] = { 1, 1 },
  227. [AV_PIX_FMT_GBRAP] = { 1, 1 },
  228. [AV_PIX_FMT_GBRAP16LE] = { 1, 1 },
  229. [AV_PIX_FMT_GBRAP16BE] = { 1, 1 },
  230. [AV_PIX_FMT_BAYER_BGGR8] = { 1, 0 },
  231. [AV_PIX_FMT_BAYER_RGGB8] = { 1, 0 },
  232. [AV_PIX_FMT_BAYER_GBRG8] = { 1, 0 },
  233. [AV_PIX_FMT_BAYER_GRBG8] = { 1, 0 },
  234. [AV_PIX_FMT_BAYER_BGGR16LE] = { 1, 0 },
  235. [AV_PIX_FMT_BAYER_BGGR16BE] = { 1, 0 },
  236. [AV_PIX_FMT_BAYER_RGGB16LE] = { 1, 0 },
  237. [AV_PIX_FMT_BAYER_RGGB16BE] = { 1, 0 },
  238. [AV_PIX_FMT_BAYER_GBRG16LE] = { 1, 0 },
  239. [AV_PIX_FMT_BAYER_GBRG16BE] = { 1, 0 },
  240. [AV_PIX_FMT_BAYER_GRBG16LE] = { 1, 0 },
  241. [AV_PIX_FMT_BAYER_GRBG16BE] = { 1, 0 },
  242. [AV_PIX_FMT_XYZ12BE] = { 1, 1, 1 },
  243. [AV_PIX_FMT_XYZ12LE] = { 1, 1, 1 },
  244. [AV_PIX_FMT_AYUV64LE] = { 1, 1},
  245. [AV_PIX_FMT_P010LE] = { 1, 1 },
  246. [AV_PIX_FMT_P010BE] = { 1, 1 },
  247. [AV_PIX_FMT_P016LE] = { 1, 1 },
  248. [AV_PIX_FMT_P016BE] = { 1, 1 },
  249. [AV_PIX_FMT_GRAYF32LE] = { 1, 1 },
  250. [AV_PIX_FMT_GRAYF32BE] = { 1, 1 },
  251. [AV_PIX_FMT_YUVA422P12BE] = { 1, 1 },
  252. [AV_PIX_FMT_YUVA422P12LE] = { 1, 1 },
  253. [AV_PIX_FMT_YUVA444P12BE] = { 1, 1 },
  254. [AV_PIX_FMT_YUVA444P12LE] = { 1, 1 },
  255. [AV_PIX_FMT_NV24] = { 1, 1 },
  256. [AV_PIX_FMT_NV42] = { 1, 1 },
  257. };
  258. int sws_isSupportedInput(enum AVPixelFormat pix_fmt)
  259. {
  260. return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
  261. format_entries[pix_fmt].is_supported_in : 0;
  262. }
  263. int sws_isSupportedOutput(enum AVPixelFormat pix_fmt)
  264. {
  265. return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
  266. format_entries[pix_fmt].is_supported_out : 0;
  267. }
  268. int sws_isSupportedEndiannessConversion(enum AVPixelFormat pix_fmt)
  269. {
  270. return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
  271. format_entries[pix_fmt].is_supported_endianness : 0;
  272. }
  273. static double getSplineCoeff(double a, double b, double c, double d,
  274. double dist)
  275. {
  276. if (dist <= 1.0)
  277. return ((d * dist + c) * dist + b) * dist + a;
  278. else
  279. return getSplineCoeff(0.0,
  280. b + 2.0 * c + 3.0 * d,
  281. c + 3.0 * d,
  282. -b - 3.0 * c - 6.0 * d,
  283. dist - 1.0);
  284. }
  285. static av_cold int get_local_pos(SwsContext *s, int chr_subsample, int pos, int dir)
  286. {
  287. if (pos == -1 || pos <= -513) {
  288. pos = (128 << chr_subsample) - 128;
  289. }
  290. pos += 128; // relative to ideal left edge
  291. return pos >> chr_subsample;
  292. }
  293. typedef struct {
  294. int flag; ///< flag associated to the algorithm
  295. const char *description; ///< human-readable description
  296. int size_factor; ///< size factor used when initing the filters
  297. } ScaleAlgorithm;
  298. static const ScaleAlgorithm scale_algorithms[] = {
  299. { SWS_AREA, "area averaging", 1 /* downscale only, for upscale it is bilinear */ },
  300. { SWS_BICUBIC, "bicubic", 4 },
  301. { SWS_BICUBLIN, "luma bicubic / chroma bilinear", -1 },
  302. { SWS_BILINEAR, "bilinear", 2 },
  303. { SWS_FAST_BILINEAR, "fast bilinear", -1 },
  304. { SWS_GAUSS, "Gaussian", 8 /* infinite ;) */ },
  305. { SWS_LANCZOS, "Lanczos", -1 /* custom */ },
  306. { SWS_POINT, "nearest neighbor / point", -1 },
  307. { SWS_SINC, "sinc", 20 /* infinite ;) */ },
  308. { SWS_SPLINE, "bicubic spline", 20 /* infinite :)*/ },
  309. { SWS_X, "experimental", 8 },
  310. };
  311. static av_cold int initFilter(int16_t **outFilter, int32_t **filterPos,
  312. int *outFilterSize, int xInc, int srcW,
  313. int dstW, int filterAlign, int one,
  314. int flags, int cpu_flags,
  315. SwsVector *srcFilter, SwsVector *dstFilter,
  316. double param[2], int srcPos, int dstPos)
  317. {
  318. int i;
  319. int filterSize;
  320. int filter2Size;
  321. int minFilterSize;
  322. int64_t *filter = NULL;
  323. int64_t *filter2 = NULL;
  324. const int64_t fone = 1LL << (54 - FFMIN(av_log2(srcW/dstW), 8));
  325. int ret = -1;
  326. emms_c(); // FIXME should not be required but IS (even for non-MMX versions)
  327. // NOTE: the +3 is for the MMX(+1) / SSE(+3) scaler which reads over the end
  328. FF_ALLOC_ARRAY_OR_GOTO(NULL, *filterPos, (dstW + 3), sizeof(**filterPos), fail);
  329. if (FFABS(xInc - 0x10000) < 10 && srcPos == dstPos) { // unscaled
  330. int i;
  331. filterSize = 1;
  332. FF_ALLOCZ_ARRAY_OR_GOTO(NULL, filter,
  333. dstW, sizeof(*filter) * filterSize, fail);
  334. for (i = 0; i < dstW; i++) {
  335. filter[i * filterSize] = fone;
  336. (*filterPos)[i] = i;
  337. }
  338. } else if (flags & SWS_POINT) { // lame looking point sampling mode
  339. int i;
  340. int64_t xDstInSrc;
  341. filterSize = 1;
  342. FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
  343. dstW, sizeof(*filter) * filterSize, fail);
  344. xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
  345. for (i = 0; i < dstW; i++) {
  346. int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
  347. (*filterPos)[i] = xx;
  348. filter[i] = fone;
  349. xDstInSrc += xInc;
  350. }
  351. } else if ((xInc <= (1 << 16) && (flags & SWS_AREA)) ||
  352. (flags & SWS_FAST_BILINEAR)) { // bilinear upscale
  353. int i;
  354. int64_t xDstInSrc;
  355. filterSize = 2;
  356. FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
  357. dstW, sizeof(*filter) * filterSize, fail);
  358. xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
  359. for (i = 0; i < dstW; i++) {
  360. int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
  361. int j;
  362. (*filterPos)[i] = xx;
  363. // bilinear upscale / linear interpolate / area averaging
  364. for (j = 0; j < filterSize; j++) {
  365. int64_t coeff = fone - FFABS((int64_t)xx * (1 << 16) - xDstInSrc) * (fone >> 16);
  366. if (coeff < 0)
  367. coeff = 0;
  368. filter[i * filterSize + j] = coeff;
  369. xx++;
  370. }
  371. xDstInSrc += xInc;
  372. }
  373. } else {
  374. int64_t xDstInSrc;
  375. int sizeFactor = -1;
  376. for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) {
  377. if (flags & scale_algorithms[i].flag && scale_algorithms[i].size_factor > 0) {
  378. sizeFactor = scale_algorithms[i].size_factor;
  379. break;
  380. }
  381. }
  382. if (flags & SWS_LANCZOS)
  383. sizeFactor = param[0] != SWS_PARAM_DEFAULT ? ceil(2 * param[0]) : 6;
  384. av_assert0(sizeFactor > 0);
  385. if (xInc <= 1 << 16)
  386. filterSize = 1 + sizeFactor; // upscale
  387. else
  388. filterSize = 1 + (sizeFactor * srcW + dstW - 1) / dstW;
  389. filterSize = FFMIN(filterSize, srcW - 2);
  390. filterSize = FFMAX(filterSize, 1);
  391. FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
  392. dstW, sizeof(*filter) * filterSize, fail);
  393. xDstInSrc = ((dstPos*(int64_t)xInc)>>7) - ((srcPos*0x10000LL)>>7);
  394. for (i = 0; i < dstW; i++) {
  395. int xx = (xDstInSrc - (filterSize - 2) * (1LL<<16)) / (1 << 17);
  396. int j;
  397. (*filterPos)[i] = xx;
  398. for (j = 0; j < filterSize; j++) {
  399. int64_t d = (FFABS(((int64_t)xx * (1 << 17)) - xDstInSrc)) << 13;
  400. double floatd;
  401. int64_t coeff;
  402. if (xInc > 1 << 16)
  403. d = d * dstW / srcW;
  404. floatd = d * (1.0 / (1 << 30));
  405. if (flags & SWS_BICUBIC) {
  406. int64_t B = (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1 << 24);
  407. int64_t C = (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1 << 24);
  408. if (d >= 1LL << 31) {
  409. coeff = 0.0;
  410. } else {
  411. int64_t dd = (d * d) >> 30;
  412. int64_t ddd = (dd * d) >> 30;
  413. if (d < 1LL << 30)
  414. coeff = (12 * (1 << 24) - 9 * B - 6 * C) * ddd +
  415. (-18 * (1 << 24) + 12 * B + 6 * C) * dd +
  416. (6 * (1 << 24) - 2 * B) * (1 << 30);
  417. else
  418. coeff = (-B - 6 * C) * ddd +
  419. (6 * B + 30 * C) * dd +
  420. (-12 * B - 48 * C) * d +
  421. (8 * B + 24 * C) * (1 << 30);
  422. }
  423. coeff /= (1LL<<54)/fone;
  424. } else if (flags & SWS_X) {
  425. double A = param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
  426. double c;
  427. if (floatd < 1.0)
  428. c = cos(floatd * M_PI);
  429. else
  430. c = -1.0;
  431. if (c < 0.0)
  432. c = -pow(-c, A);
  433. else
  434. c = pow(c, A);
  435. coeff = (c * 0.5 + 0.5) * fone;
  436. } else if (flags & SWS_AREA) {
  437. int64_t d2 = d - (1 << 29);
  438. if (d2 * xInc < -(1LL << (29 + 16)))
  439. coeff = 1.0 * (1LL << (30 + 16));
  440. else if (d2 * xInc < (1LL << (29 + 16)))
  441. coeff = -d2 * xInc + (1LL << (29 + 16));
  442. else
  443. coeff = 0.0;
  444. coeff *= fone >> (30 + 16);
  445. } else if (flags & SWS_GAUSS) {
  446. double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
  447. coeff = exp2(-p * floatd * floatd) * fone;
  448. } else if (flags & SWS_SINC) {
  449. coeff = (d ? sin(floatd * M_PI) / (floatd * M_PI) : 1.0) * fone;
  450. } else if (flags & SWS_LANCZOS) {
  451. double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
  452. coeff = (d ? sin(floatd * M_PI) * sin(floatd * M_PI / p) /
  453. (floatd * floatd * M_PI * M_PI / p) : 1.0) * fone;
  454. if (floatd > p)
  455. coeff = 0;
  456. } else if (flags & SWS_BILINEAR) {
  457. coeff = (1 << 30) - d;
  458. if (coeff < 0)
  459. coeff = 0;
  460. coeff *= fone >> 30;
  461. } else if (flags & SWS_SPLINE) {
  462. double p = -2.196152422706632;
  463. coeff = getSplineCoeff(1.0, 0.0, p, -p - 1.0, floatd) * fone;
  464. } else {
  465. av_assert0(0);
  466. }
  467. filter[i * filterSize + j] = coeff;
  468. xx++;
  469. }
  470. xDstInSrc += 2 * xInc;
  471. }
  472. }
  473. /* apply src & dst Filter to filter -> filter2
  474. * av_free(filter);
  475. */
  476. av_assert0(filterSize > 0);
  477. filter2Size = filterSize;
  478. if (srcFilter)
  479. filter2Size += srcFilter->length - 1;
  480. if (dstFilter)
  481. filter2Size += dstFilter->length - 1;
  482. av_assert0(filter2Size > 0);
  483. FF_ALLOCZ_ARRAY_OR_GOTO(NULL, filter2, dstW, filter2Size * sizeof(*filter2), fail);
  484. for (i = 0; i < dstW; i++) {
  485. int j, k;
  486. if (srcFilter) {
  487. for (k = 0; k < srcFilter->length; k++) {
  488. for (j = 0; j < filterSize; j++)
  489. filter2[i * filter2Size + k + j] +=
  490. srcFilter->coeff[k] * filter[i * filterSize + j];
  491. }
  492. } else {
  493. for (j = 0; j < filterSize; j++)
  494. filter2[i * filter2Size + j] = filter[i * filterSize + j];
  495. }
  496. // FIXME dstFilter
  497. (*filterPos)[i] += (filterSize - 1) / 2 - (filter2Size - 1) / 2;
  498. }
  499. av_freep(&filter);
  500. /* try to reduce the filter-size (step1 find size and shift left) */
  501. // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
  502. minFilterSize = 0;
  503. for (i = dstW - 1; i >= 0; i--) {
  504. int min = filter2Size;
  505. int j;
  506. int64_t cutOff = 0.0;
  507. /* get rid of near zero elements on the left by shifting left */
  508. for (j = 0; j < filter2Size; j++) {
  509. int k;
  510. cutOff += FFABS(filter2[i * filter2Size]);
  511. if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
  512. break;
  513. /* preserve monotonicity because the core can't handle the
  514. * filter otherwise */
  515. if (i < dstW - 1 && (*filterPos)[i] >= (*filterPos)[i + 1])
  516. break;
  517. // move filter coefficients left
  518. for (k = 1; k < filter2Size; k++)
  519. filter2[i * filter2Size + k - 1] = filter2[i * filter2Size + k];
  520. filter2[i * filter2Size + k - 1] = 0;
  521. (*filterPos)[i]++;
  522. }
  523. cutOff = 0;
  524. /* count near zeros on the right */
  525. for (j = filter2Size - 1; j > 0; j--) {
  526. cutOff += FFABS(filter2[i * filter2Size + j]);
  527. if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
  528. break;
  529. min--;
  530. }
  531. if (min > minFilterSize)
  532. minFilterSize = min;
  533. }
  534. if (PPC_ALTIVEC(cpu_flags)) {
  535. // we can handle the special case 4, so we don't want to go the full 8
  536. if (minFilterSize < 5)
  537. filterAlign = 4;
  538. /* We really don't want to waste our time doing useless computation, so
  539. * fall back on the scalar C code for very small filters.
  540. * Vectorizing is worth it only if you have a decent-sized vector. */
  541. if (minFilterSize < 3)
  542. filterAlign = 1;
  543. }
  544. if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) {
  545. // special case for unscaled vertical filtering
  546. if (minFilterSize == 1 && filterAlign == 2)
  547. filterAlign = 1;
  548. }
  549. av_assert0(minFilterSize > 0);
  550. filterSize = (minFilterSize + (filterAlign - 1)) & (~(filterAlign - 1));
  551. av_assert0(filterSize > 0);
  552. filter = av_malloc_array(dstW, filterSize * sizeof(*filter));
  553. if (!filter)
  554. goto fail;
  555. if (filterSize >= MAX_FILTER_SIZE * 16 /
  556. ((flags & SWS_ACCURATE_RND) ? APCK_SIZE : 16)) {
  557. ret = RETCODE_USE_CASCADE;
  558. goto fail;
  559. }
  560. *outFilterSize = filterSize;
  561. if (flags & SWS_PRINT_INFO)
  562. av_log(NULL, AV_LOG_VERBOSE,
  563. "SwScaler: reducing / aligning filtersize %d -> %d\n",
  564. filter2Size, filterSize);
  565. /* try to reduce the filter-size (step2 reduce it) */
  566. for (i = 0; i < dstW; i++) {
  567. int j;
  568. for (j = 0; j < filterSize; j++) {
  569. if (j >= filter2Size)
  570. filter[i * filterSize + j] = 0;
  571. else
  572. filter[i * filterSize + j] = filter2[i * filter2Size + j];
  573. if ((flags & SWS_BITEXACT) && j >= minFilterSize)
  574. filter[i * filterSize + j] = 0;
  575. }
  576. }
  577. // FIXME try to align filterPos if possible
  578. // fix borders
  579. for (i = 0; i < dstW; i++) {
  580. int j;
  581. if ((*filterPos)[i] < 0) {
  582. // move filter coefficients left to compensate for filterPos
  583. for (j = 1; j < filterSize; j++) {
  584. int left = FFMAX(j + (*filterPos)[i], 0);
  585. filter[i * filterSize + left] += filter[i * filterSize + j];
  586. filter[i * filterSize + j] = 0;
  587. }
  588. (*filterPos)[i]= 0;
  589. }
  590. if ((*filterPos)[i] + filterSize > srcW) {
  591. int shift = (*filterPos)[i] + FFMIN(filterSize - srcW, 0);
  592. int64_t acc = 0;
  593. for (j = filterSize - 1; j >= 0; j--) {
  594. if ((*filterPos)[i] + j >= srcW) {
  595. acc += filter[i * filterSize + j];
  596. filter[i * filterSize + j] = 0;
  597. }
  598. }
  599. for (j = filterSize - 1; j >= 0; j--) {
  600. if (j < shift) {
  601. filter[i * filterSize + j] = 0;
  602. } else {
  603. filter[i * filterSize + j] = filter[i * filterSize + j - shift];
  604. }
  605. }
  606. (*filterPos)[i]-= shift;
  607. filter[i * filterSize + srcW - 1 - (*filterPos)[i]] += acc;
  608. }
  609. av_assert0((*filterPos)[i] >= 0);
  610. av_assert0((*filterPos)[i] < srcW);
  611. if ((*filterPos)[i] + filterSize > srcW) {
  612. for (j = 0; j < filterSize; j++) {
  613. av_assert0((*filterPos)[i] + j < srcW || !filter[i * filterSize + j]);
  614. }
  615. }
  616. }
  617. // Note the +1 is for the MMX scaler which reads over the end
  618. /* align at 16 for AltiVec (needed by hScale_altivec_real) */
  619. FF_ALLOCZ_ARRAY_OR_GOTO(NULL, *outFilter,
  620. (dstW + 3), *outFilterSize * sizeof(int16_t), fail);
  621. /* normalize & store in outFilter */
  622. for (i = 0; i < dstW; i++) {
  623. int j;
  624. int64_t error = 0;
  625. int64_t sum = 0;
  626. for (j = 0; j < filterSize; j++) {
  627. sum += filter[i * filterSize + j];
  628. }
  629. sum = (sum + one / 2) / one;
  630. if (!sum) {
  631. av_log(NULL, AV_LOG_WARNING, "SwScaler: zero vector in scaling\n");
  632. sum = 1;
  633. }
  634. for (j = 0; j < *outFilterSize; j++) {
  635. int64_t v = filter[i * filterSize + j] + error;
  636. int intV = ROUNDED_DIV(v, sum);
  637. (*outFilter)[i * (*outFilterSize) + j] = intV;
  638. error = v - intV * sum;
  639. }
  640. }
  641. (*filterPos)[dstW + 0] =
  642. (*filterPos)[dstW + 1] =
  643. (*filterPos)[dstW + 2] = (*filterPos)[dstW - 1]; /* the MMX/SSE scaler will
  644. * read over the end */
  645. for (i = 0; i < *outFilterSize; i++) {
  646. int k = (dstW - 1) * (*outFilterSize) + i;
  647. (*outFilter)[k + 1 * (*outFilterSize)] =
  648. (*outFilter)[k + 2 * (*outFilterSize)] =
  649. (*outFilter)[k + 3 * (*outFilterSize)] = (*outFilter)[k];
  650. }
  651. ret = 0;
  652. fail:
  653. if(ret < 0)
  654. av_log(NULL, ret == RETCODE_USE_CASCADE ? AV_LOG_DEBUG : AV_LOG_ERROR, "sws: initFilter failed\n");
  655. av_free(filter);
  656. av_free(filter2);
  657. return ret;
  658. }
  659. static void fill_rgb2yuv_table(SwsContext *c, const int table[4], int dstRange)
  660. {
  661. int64_t W, V, Z, Cy, Cu, Cv;
  662. int64_t vr = table[0];
  663. int64_t ub = table[1];
  664. int64_t ug = -table[2];
  665. int64_t vg = -table[3];
  666. int64_t ONE = 65536;
  667. int64_t cy = ONE;
  668. uint8_t *p = (uint8_t*)c->input_rgb2yuv_table;
  669. int i;
  670. static const int8_t map[] = {
  671. BY_IDX, GY_IDX, -1 , BY_IDX, BY_IDX, GY_IDX, -1 , BY_IDX,
  672. RY_IDX, -1 , GY_IDX, RY_IDX, RY_IDX, -1 , GY_IDX, RY_IDX,
  673. RY_IDX, GY_IDX, -1 , RY_IDX, RY_IDX, GY_IDX, -1 , RY_IDX,
  674. BY_IDX, -1 , GY_IDX, BY_IDX, BY_IDX, -1 , GY_IDX, BY_IDX,
  675. BU_IDX, GU_IDX, -1 , BU_IDX, BU_IDX, GU_IDX, -1 , BU_IDX,
  676. RU_IDX, -1 , GU_IDX, RU_IDX, RU_IDX, -1 , GU_IDX, RU_IDX,
  677. RU_IDX, GU_IDX, -1 , RU_IDX, RU_IDX, GU_IDX, -1 , RU_IDX,
  678. BU_IDX, -1 , GU_IDX, BU_IDX, BU_IDX, -1 , GU_IDX, BU_IDX,
  679. BV_IDX, GV_IDX, -1 , BV_IDX, BV_IDX, GV_IDX, -1 , BV_IDX,
  680. RV_IDX, -1 , GV_IDX, RV_IDX, RV_IDX, -1 , GV_IDX, RV_IDX,
  681. RV_IDX, GV_IDX, -1 , RV_IDX, RV_IDX, GV_IDX, -1 , RV_IDX,
  682. BV_IDX, -1 , GV_IDX, BV_IDX, BV_IDX, -1 , GV_IDX, BV_IDX,
  683. RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX,
  684. BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX,
  685. GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 ,
  686. -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX,
  687. RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX,
  688. BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX,
  689. GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 ,
  690. -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX,
  691. RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX,
  692. BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX,
  693. GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 ,
  694. -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, //23
  695. -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //24
  696. -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //25
  697. -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //26
  698. -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //27
  699. -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //28
  700. -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //29
  701. -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //30
  702. -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //31
  703. BY_IDX, GY_IDX, RY_IDX, -1 , -1 , -1 , -1 , -1 , //32
  704. BU_IDX, GU_IDX, RU_IDX, -1 , -1 , -1 , -1 , -1 , //33
  705. BV_IDX, GV_IDX, RV_IDX, -1 , -1 , -1 , -1 , -1 , //34
  706. };
  707. dstRange = 0; //FIXME range = 1 is handled elsewhere
  708. if (!dstRange) {
  709. cy = cy * 255 / 219;
  710. } else {
  711. vr = vr * 224 / 255;
  712. ub = ub * 224 / 255;
  713. ug = ug * 224 / 255;
  714. vg = vg * 224 / 255;
  715. }
  716. W = ROUNDED_DIV(ONE*ONE*ug, ub);
  717. V = ROUNDED_DIV(ONE*ONE*vg, vr);
  718. Z = ONE*ONE-W-V;
  719. Cy = ROUNDED_DIV(cy*Z, ONE);
  720. Cu = ROUNDED_DIV(ub*Z, ONE);
  721. Cv = ROUNDED_DIV(vr*Z, ONE);
  722. c->input_rgb2yuv_table[RY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V , Cy);
  723. c->input_rgb2yuv_table[GY_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cy);
  724. c->input_rgb2yuv_table[BY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W , Cy);
  725. c->input_rgb2yuv_table[RU_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V , Cu);
  726. c->input_rgb2yuv_table[GU_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cu);
  727. c->input_rgb2yuv_table[BU_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(Z+W) , Cu);
  728. c->input_rgb2yuv_table[RV_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(V+Z) , Cv);
  729. c->input_rgb2yuv_table[GV_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cv);
  730. c->input_rgb2yuv_table[BV_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W , Cv);
  731. if(/*!dstRange && */!memcmp(table, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], sizeof(ff_yuv2rgb_coeffs[SWS_CS_DEFAULT]))) {
  732. c->input_rgb2yuv_table[BY_IDX] = ((int)(0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
  733. c->input_rgb2yuv_table[BV_IDX] = (-(int)(0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
  734. c->input_rgb2yuv_table[BU_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
  735. c->input_rgb2yuv_table[GY_IDX] = ((int)(0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
  736. c->input_rgb2yuv_table[GV_IDX] = (-(int)(0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
  737. c->input_rgb2yuv_table[GU_IDX] = (-(int)(0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
  738. c->input_rgb2yuv_table[RY_IDX] = ((int)(0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
  739. c->input_rgb2yuv_table[RV_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
  740. c->input_rgb2yuv_table[RU_IDX] = (-(int)(0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
  741. }
  742. for(i=0; i<FF_ARRAY_ELEMS(map); i++)
  743. AV_WL16(p + 16*4 + 2*i, map[i] >= 0 ? c->input_rgb2yuv_table[map[i]] : 0);
  744. }
  745. static void fill_xyztables(struct SwsContext *c)
  746. {
  747. int i;
  748. double xyzgamma = XYZ_GAMMA;
  749. double rgbgamma = 1.0 / RGB_GAMMA;
  750. double xyzgammainv = 1.0 / XYZ_GAMMA;
  751. double rgbgammainv = RGB_GAMMA;
  752. static const int16_t xyz2rgb_matrix[3][4] = {
  753. {13270, -6295, -2041},
  754. {-3969, 7682, 170},
  755. { 228, -835, 4329} };
  756. static const int16_t rgb2xyz_matrix[3][4] = {
  757. {1689, 1464, 739},
  758. { 871, 2929, 296},
  759. { 79, 488, 3891} };
  760. static int16_t xyzgamma_tab[4096], rgbgamma_tab[4096], xyzgammainv_tab[4096], rgbgammainv_tab[4096];
  761. memcpy(c->xyz2rgb_matrix, xyz2rgb_matrix, sizeof(c->xyz2rgb_matrix));
  762. memcpy(c->rgb2xyz_matrix, rgb2xyz_matrix, sizeof(c->rgb2xyz_matrix));
  763. c->xyzgamma = xyzgamma_tab;
  764. c->rgbgamma = rgbgamma_tab;
  765. c->xyzgammainv = xyzgammainv_tab;
  766. c->rgbgammainv = rgbgammainv_tab;
  767. if (rgbgamma_tab[4095])
  768. return;
  769. /* set gamma vectors */
  770. for (i = 0; i < 4096; i++) {
  771. xyzgamma_tab[i] = lrint(pow(i / 4095.0, xyzgamma) * 4095.0);
  772. rgbgamma_tab[i] = lrint(pow(i / 4095.0, rgbgamma) * 4095.0);
  773. xyzgammainv_tab[i] = lrint(pow(i / 4095.0, xyzgammainv) * 4095.0);
  774. rgbgammainv_tab[i] = lrint(pow(i / 4095.0, rgbgammainv) * 4095.0);
  775. }
  776. }
  777. int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4],
  778. int srcRange, const int table[4], int dstRange,
  779. int brightness, int contrast, int saturation)
  780. {
  781. const AVPixFmtDescriptor *desc_dst;
  782. const AVPixFmtDescriptor *desc_src;
  783. int need_reinit = 0;
  784. handle_formats(c);
  785. desc_dst = av_pix_fmt_desc_get(c->dstFormat);
  786. desc_src = av_pix_fmt_desc_get(c->srcFormat);
  787. if(!isYUV(c->dstFormat) && !isGray(c->dstFormat))
  788. dstRange = 0;
  789. if(!isYUV(c->srcFormat) && !isGray(c->srcFormat))
  790. srcRange = 0;
  791. if (c->srcRange != srcRange ||
  792. c->dstRange != dstRange ||
  793. c->brightness != brightness ||
  794. c->contrast != contrast ||
  795. c->saturation != saturation ||
  796. memcmp(c->srcColorspaceTable, inv_table, sizeof(int) * 4) ||
  797. memcmp(c->dstColorspaceTable, table, sizeof(int) * 4)
  798. )
  799. need_reinit = 1;
  800. memmove(c->srcColorspaceTable, inv_table, sizeof(int) * 4);
  801. memmove(c->dstColorspaceTable, table, sizeof(int) * 4);
  802. c->brightness = brightness;
  803. c->contrast = contrast;
  804. c->saturation = saturation;
  805. c->srcRange = srcRange;
  806. c->dstRange = dstRange;
  807. //The srcBpc check is possibly wrong but we seem to lack a definitive reference to test this
  808. //and what we have in ticket 2939 looks better with this check
  809. if (need_reinit && (c->srcBpc == 8 || !isYUV(c->srcFormat)))
  810. ff_sws_init_range_convert(c);
  811. c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
  812. c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
  813. if (c->cascaded_context[c->cascaded_mainindex])
  814. return sws_setColorspaceDetails(c->cascaded_context[c->cascaded_mainindex],inv_table, srcRange,table, dstRange, brightness, contrast, saturation);
  815. if (!need_reinit)
  816. return 0;
  817. if ((isYUV(c->dstFormat) || isGray(c->dstFormat)) && (isYUV(c->srcFormat) || isGray(c->srcFormat))) {
  818. if (!c->cascaded_context[0] &&
  819. memcmp(c->dstColorspaceTable, c->srcColorspaceTable, sizeof(int) * 4) &&
  820. c->srcW && c->srcH && c->dstW && c->dstH) {
  821. enum AVPixelFormat tmp_format;
  822. int tmp_width, tmp_height;
  823. int srcW = c->srcW;
  824. int srcH = c->srcH;
  825. int dstW = c->dstW;
  826. int dstH = c->dstH;
  827. int ret;
  828. av_log(c, AV_LOG_VERBOSE, "YUV color matrix differs for YUV->YUV, using intermediate RGB to convert\n");
  829. if (isNBPS(c->dstFormat) || is16BPS(c->dstFormat)) {
  830. if (isALPHA(c->srcFormat) && isALPHA(c->dstFormat)) {
  831. tmp_format = AV_PIX_FMT_BGRA64;
  832. } else {
  833. tmp_format = AV_PIX_FMT_BGR48;
  834. }
  835. } else {
  836. if (isALPHA(c->srcFormat) && isALPHA(c->dstFormat)) {
  837. tmp_format = AV_PIX_FMT_BGRA;
  838. } else {
  839. tmp_format = AV_PIX_FMT_BGR24;
  840. }
  841. }
  842. if (srcW*srcH > dstW*dstH) {
  843. tmp_width = dstW;
  844. tmp_height = dstH;
  845. } else {
  846. tmp_width = srcW;
  847. tmp_height = srcH;
  848. }
  849. ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
  850. tmp_width, tmp_height, tmp_format, 64);
  851. if (ret < 0)
  852. return ret;
  853. c->cascaded_context[0] = sws_alloc_set_opts(srcW, srcH, c->srcFormat,
  854. tmp_width, tmp_height, tmp_format,
  855. c->flags, c->param);
  856. if (!c->cascaded_context[0])
  857. return -1;
  858. c->cascaded_context[0]->alphablend = c->alphablend;
  859. ret = sws_init_context(c->cascaded_context[0], NULL , NULL);
  860. if (ret < 0)
  861. return ret;
  862. //we set both src and dst depending on that the RGB side will be ignored
  863. sws_setColorspaceDetails(c->cascaded_context[0], inv_table,
  864. srcRange, table, dstRange,
  865. brightness, contrast, saturation);
  866. c->cascaded_context[1] = sws_getContext(tmp_width, tmp_height, tmp_format,
  867. dstW, dstH, c->dstFormat,
  868. c->flags, NULL, NULL, c->param);
  869. if (!c->cascaded_context[1])
  870. return -1;
  871. sws_setColorspaceDetails(c->cascaded_context[1], inv_table,
  872. srcRange, table, dstRange,
  873. 0, 1 << 16, 1 << 16);
  874. return 0;
  875. }
  876. return -1;
  877. }
  878. if (!isYUV(c->dstFormat) && !isGray(c->dstFormat)) {
  879. ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness,
  880. contrast, saturation);
  881. // FIXME factorize
  882. if (ARCH_PPC)
  883. ff_yuv2rgb_init_tables_ppc(c, inv_table, brightness,
  884. contrast, saturation);
  885. }
  886. fill_rgb2yuv_table(c, table, dstRange);
  887. return 0;
  888. }
  889. int sws_getColorspaceDetails(struct SwsContext *c, int **inv_table,
  890. int *srcRange, int **table, int *dstRange,
  891. int *brightness, int *contrast, int *saturation)
  892. {
  893. if (!c )
  894. return -1;
  895. *inv_table = c->srcColorspaceTable;
  896. *table = c->dstColorspaceTable;
  897. *srcRange = c->srcRange;
  898. *dstRange = c->dstRange;
  899. *brightness = c->brightness;
  900. *contrast = c->contrast;
  901. *saturation = c->saturation;
  902. return 0;
  903. }
  904. static int handle_jpeg(enum AVPixelFormat *format)
  905. {
  906. switch (*format) {
  907. case AV_PIX_FMT_YUVJ420P:
  908. *format = AV_PIX_FMT_YUV420P;
  909. return 1;
  910. case AV_PIX_FMT_YUVJ411P:
  911. *format = AV_PIX_FMT_YUV411P;
  912. return 1;
  913. case AV_PIX_FMT_YUVJ422P:
  914. *format = AV_PIX_FMT_YUV422P;
  915. return 1;
  916. case AV_PIX_FMT_YUVJ444P:
  917. *format = AV_PIX_FMT_YUV444P;
  918. return 1;
  919. case AV_PIX_FMT_YUVJ440P:
  920. *format = AV_PIX_FMT_YUV440P;
  921. return 1;
  922. case AV_PIX_FMT_GRAY8:
  923. case AV_PIX_FMT_YA8:
  924. case AV_PIX_FMT_GRAY9LE:
  925. case AV_PIX_FMT_GRAY9BE:
  926. case AV_PIX_FMT_GRAY10LE:
  927. case AV_PIX_FMT_GRAY10BE:
  928. case AV_PIX_FMT_GRAY12LE:
  929. case AV_PIX_FMT_GRAY12BE:
  930. case AV_PIX_FMT_GRAY14LE:
  931. case AV_PIX_FMT_GRAY14BE:
  932. case AV_PIX_FMT_GRAY16LE:
  933. case AV_PIX_FMT_GRAY16BE:
  934. case AV_PIX_FMT_YA16BE:
  935. case AV_PIX_FMT_YA16LE:
  936. return 1;
  937. default:
  938. return 0;
  939. }
  940. }
  941. static int handle_0alpha(enum AVPixelFormat *format)
  942. {
  943. switch (*format) {
  944. case AV_PIX_FMT_0BGR : *format = AV_PIX_FMT_ABGR ; return 1;
  945. case AV_PIX_FMT_BGR0 : *format = AV_PIX_FMT_BGRA ; return 4;
  946. case AV_PIX_FMT_0RGB : *format = AV_PIX_FMT_ARGB ; return 1;
  947. case AV_PIX_FMT_RGB0 : *format = AV_PIX_FMT_RGBA ; return 4;
  948. default: return 0;
  949. }
  950. }
  951. static int handle_xyz(enum AVPixelFormat *format)
  952. {
  953. switch (*format) {
  954. case AV_PIX_FMT_XYZ12BE : *format = AV_PIX_FMT_RGB48BE; return 1;
  955. case AV_PIX_FMT_XYZ12LE : *format = AV_PIX_FMT_RGB48LE; return 1;
  956. default: return 0;
  957. }
  958. }
  959. static void handle_formats(SwsContext *c)
  960. {
  961. c->src0Alpha |= handle_0alpha(&c->srcFormat);
  962. c->dst0Alpha |= handle_0alpha(&c->dstFormat);
  963. c->srcXYZ |= handle_xyz(&c->srcFormat);
  964. c->dstXYZ |= handle_xyz(&c->dstFormat);
  965. if (c->srcXYZ || c->dstXYZ)
  966. fill_xyztables(c);
  967. }
  968. SwsContext *sws_alloc_context(void)
  969. {
  970. SwsContext *c = av_mallocz(sizeof(SwsContext));
  971. av_assert0(offsetof(SwsContext, redDither) + DITHER32_INT == offsetof(SwsContext, dither32));
  972. if (c) {
  973. c->av_class = &ff_sws_context_class;
  974. av_opt_set_defaults(c);
  975. }
  976. return c;
  977. }
  978. static uint16_t * alloc_gamma_tbl(double e)
  979. {
  980. int i = 0;
  981. uint16_t * tbl;
  982. tbl = (uint16_t*)av_malloc(sizeof(uint16_t) * 1 << 16);
  983. if (!tbl)
  984. return NULL;
  985. for (i = 0; i < 65536; ++i) {
  986. tbl[i] = pow(i / 65535.0, e) * 65535.0;
  987. }
  988. return tbl;
  989. }
  990. static enum AVPixelFormat alphaless_fmt(enum AVPixelFormat fmt)
  991. {
  992. switch(fmt) {
  993. case AV_PIX_FMT_ARGB: return AV_PIX_FMT_RGB24;
  994. case AV_PIX_FMT_RGBA: return AV_PIX_FMT_RGB24;
  995. case AV_PIX_FMT_ABGR: return AV_PIX_FMT_BGR24;
  996. case AV_PIX_FMT_BGRA: return AV_PIX_FMT_BGR24;
  997. case AV_PIX_FMT_YA8: return AV_PIX_FMT_GRAY8;
  998. case AV_PIX_FMT_YUVA420P: return AV_PIX_FMT_YUV420P;
  999. case AV_PIX_FMT_YUVA422P: return AV_PIX_FMT_YUV422P;
  1000. case AV_PIX_FMT_YUVA444P: return AV_PIX_FMT_YUV444P;
  1001. case AV_PIX_FMT_GBRAP: return AV_PIX_FMT_GBRP;
  1002. case AV_PIX_FMT_GBRAP10LE: return AV_PIX_FMT_GBRP10;
  1003. case AV_PIX_FMT_GBRAP10BE: return AV_PIX_FMT_GBRP10;
  1004. case AV_PIX_FMT_GBRAP12LE: return AV_PIX_FMT_GBRP12;
  1005. case AV_PIX_FMT_GBRAP12BE: return AV_PIX_FMT_GBRP12;
  1006. case AV_PIX_FMT_GBRAP16LE: return AV_PIX_FMT_GBRP16;
  1007. case AV_PIX_FMT_GBRAP16BE: return AV_PIX_FMT_GBRP16;
  1008. case AV_PIX_FMT_RGBA64LE: return AV_PIX_FMT_RGB48;
  1009. case AV_PIX_FMT_RGBA64BE: return AV_PIX_FMT_RGB48;
  1010. case AV_PIX_FMT_BGRA64LE: return AV_PIX_FMT_BGR48;
  1011. case AV_PIX_FMT_BGRA64BE: return AV_PIX_FMT_BGR48;
  1012. case AV_PIX_FMT_YA16BE: return AV_PIX_FMT_GRAY16;
  1013. case AV_PIX_FMT_YA16LE: return AV_PIX_FMT_GRAY16;
  1014. case AV_PIX_FMT_YUVA420P9BE: return AV_PIX_FMT_YUV420P9;
  1015. case AV_PIX_FMT_YUVA422P9BE: return AV_PIX_FMT_YUV422P9;
  1016. case AV_PIX_FMT_YUVA444P9BE: return AV_PIX_FMT_YUV444P9;
  1017. case AV_PIX_FMT_YUVA420P9LE: return AV_PIX_FMT_YUV420P9;
  1018. case AV_PIX_FMT_YUVA422P9LE: return AV_PIX_FMT_YUV422P9;
  1019. case AV_PIX_FMT_YUVA444P9LE: return AV_PIX_FMT_YUV444P9;
  1020. case AV_PIX_FMT_YUVA420P10BE: return AV_PIX_FMT_YUV420P10;
  1021. case AV_PIX_FMT_YUVA422P10BE: return AV_PIX_FMT_YUV422P10;
  1022. case AV_PIX_FMT_YUVA444P10BE: return AV_PIX_FMT_YUV444P10;
  1023. case AV_PIX_FMT_YUVA420P10LE: return AV_PIX_FMT_YUV420P10;
  1024. case AV_PIX_FMT_YUVA422P10LE: return AV_PIX_FMT_YUV422P10;
  1025. case AV_PIX_FMT_YUVA444P10LE: return AV_PIX_FMT_YUV444P10;
  1026. case AV_PIX_FMT_YUVA420P16BE: return AV_PIX_FMT_YUV420P16;
  1027. case AV_PIX_FMT_YUVA422P16BE: return AV_PIX_FMT_YUV422P16;
  1028. case AV_PIX_FMT_YUVA444P16BE: return AV_PIX_FMT_YUV444P16;
  1029. case AV_PIX_FMT_YUVA420P16LE: return AV_PIX_FMT_YUV420P16;
  1030. case AV_PIX_FMT_YUVA422P16LE: return AV_PIX_FMT_YUV422P16;
  1031. case AV_PIX_FMT_YUVA444P16LE: return AV_PIX_FMT_YUV444P16;
  1032. // case AV_PIX_FMT_AYUV64LE:
  1033. // case AV_PIX_FMT_AYUV64BE:
  1034. // case AV_PIX_FMT_PAL8:
  1035. default: return AV_PIX_FMT_NONE;
  1036. }
  1037. }
  1038. av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter,
  1039. SwsFilter *dstFilter)
  1040. {
  1041. int i;
  1042. int usesVFilter, usesHFilter;
  1043. int unscaled;
  1044. SwsFilter dummyFilter = { NULL, NULL, NULL, NULL };
  1045. int srcW = c->srcW;
  1046. int srcH = c->srcH;
  1047. int dstW = c->dstW;
  1048. int dstH = c->dstH;
  1049. int dst_stride = FFALIGN(dstW * sizeof(int16_t) + 66, 16);
  1050. int flags, cpu_flags;
  1051. enum AVPixelFormat srcFormat = c->srcFormat;
  1052. enum AVPixelFormat dstFormat = c->dstFormat;
  1053. const AVPixFmtDescriptor *desc_src;
  1054. const AVPixFmtDescriptor *desc_dst;
  1055. int ret = 0;
  1056. enum AVPixelFormat tmpFmt;
  1057. static const float float_mult = 1.0f / 255.0f;
  1058. cpu_flags = av_get_cpu_flags();
  1059. flags = c->flags;
  1060. emms_c();
  1061. if (!rgb15to16)
  1062. ff_sws_rgb2rgb_init();
  1063. unscaled = (srcW == dstW && srcH == dstH);
  1064. c->srcRange |= handle_jpeg(&c->srcFormat);
  1065. c->dstRange |= handle_jpeg(&c->dstFormat);
  1066. if(srcFormat!=c->srcFormat || dstFormat!=c->dstFormat)
  1067. av_log(c, AV_LOG_WARNING, "deprecated pixel format used, make sure you did set range correctly\n");
  1068. if (!c->contrast && !c->saturation && !c->dstFormatBpp)
  1069. sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], c->srcRange,
  1070. ff_yuv2rgb_coeffs[SWS_CS_DEFAULT],
  1071. c->dstRange, 0, 1 << 16, 1 << 16);
  1072. handle_formats(c);
  1073. srcFormat = c->srcFormat;
  1074. dstFormat = c->dstFormat;
  1075. desc_src = av_pix_fmt_desc_get(srcFormat);
  1076. desc_dst = av_pix_fmt_desc_get(dstFormat);
  1077. // If the source has no alpha then disable alpha blendaway
  1078. if (c->src0Alpha)
  1079. c->alphablend = SWS_ALPHA_BLEND_NONE;
  1080. if (!(unscaled && sws_isSupportedEndiannessConversion(srcFormat) &&
  1081. av_pix_fmt_swap_endianness(srcFormat) == dstFormat)) {
  1082. if (!sws_isSupportedInput(srcFormat)) {
  1083. av_log(c, AV_LOG_ERROR, "%s is not supported as input pixel format\n",
  1084. av_get_pix_fmt_name(srcFormat));
  1085. return AVERROR(EINVAL);
  1086. }
  1087. if (!sws_isSupportedOutput(dstFormat)) {
  1088. av_log(c, AV_LOG_ERROR, "%s is not supported as output pixel format\n",
  1089. av_get_pix_fmt_name(dstFormat));
  1090. return AVERROR(EINVAL);
  1091. }
  1092. }
  1093. av_assert2(desc_src && desc_dst);
  1094. i = flags & (SWS_POINT |
  1095. SWS_AREA |
  1096. SWS_BILINEAR |
  1097. SWS_FAST_BILINEAR |
  1098. SWS_BICUBIC |
  1099. SWS_X |
  1100. SWS_GAUSS |
  1101. SWS_LANCZOS |
  1102. SWS_SINC |
  1103. SWS_SPLINE |
  1104. SWS_BICUBLIN);
  1105. /* provide a default scaler if not set by caller */
  1106. if (!i) {
  1107. if (dstW < srcW && dstH < srcH)
  1108. flags |= SWS_BICUBIC;
  1109. else if (dstW > srcW && dstH > srcH)
  1110. flags |= SWS_BICUBIC;
  1111. else
  1112. flags |= SWS_BICUBIC;
  1113. c->flags = flags;
  1114. } else if (i & (i - 1)) {
  1115. av_log(c, AV_LOG_ERROR,
  1116. "Exactly one scaler algorithm must be chosen, got %X\n", i);
  1117. return AVERROR(EINVAL);
  1118. }
  1119. /* sanity check */
  1120. if (srcW < 1 || srcH < 1 || dstW < 1 || dstH < 1) {
  1121. /* FIXME check if these are enough and try to lower them after
  1122. * fixing the relevant parts of the code */
  1123. av_log(c, AV_LOG_ERROR, "%dx%d -> %dx%d is invalid scaling dimension\n",
  1124. srcW, srcH, dstW, dstH);
  1125. return AVERROR(EINVAL);
  1126. }
  1127. if (flags & SWS_FAST_BILINEAR) {
  1128. if (srcW < 8 || dstW < 8) {
  1129. flags ^= SWS_FAST_BILINEAR | SWS_BILINEAR;
  1130. c->flags = flags;
  1131. }
  1132. }
  1133. if (!dstFilter)
  1134. dstFilter = &dummyFilter;
  1135. if (!srcFilter)
  1136. srcFilter = &dummyFilter;
  1137. c->lumXInc = (((int64_t)srcW << 16) + (dstW >> 1)) / dstW;
  1138. c->lumYInc = (((int64_t)srcH << 16) + (dstH >> 1)) / dstH;
  1139. c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
  1140. c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
  1141. c->vRounder = 4 * 0x0001000100010001ULL;
  1142. usesVFilter = (srcFilter->lumV && srcFilter->lumV->length > 1) ||
  1143. (srcFilter->chrV && srcFilter->chrV->length > 1) ||
  1144. (dstFilter->lumV && dstFilter->lumV->length > 1) ||
  1145. (dstFilter->chrV && dstFilter->chrV->length > 1);
  1146. usesHFilter = (srcFilter->lumH && srcFilter->lumH->length > 1) ||
  1147. (srcFilter->chrH && srcFilter->chrH->length > 1) ||
  1148. (dstFilter->lumH && dstFilter->lumH->length > 1) ||
  1149. (dstFilter->chrH && dstFilter->chrH->length > 1);
  1150. av_pix_fmt_get_chroma_sub_sample(srcFormat, &c->chrSrcHSubSample, &c->chrSrcVSubSample);
  1151. av_pix_fmt_get_chroma_sub_sample(dstFormat, &c->chrDstHSubSample, &c->chrDstVSubSample);
  1152. if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) {
  1153. if (dstW&1) {
  1154. av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to odd output size\n");
  1155. flags |= SWS_FULL_CHR_H_INT;
  1156. c->flags = flags;
  1157. }
  1158. if ( c->chrSrcHSubSample == 0
  1159. && c->chrSrcVSubSample == 0
  1160. && c->dither != SWS_DITHER_BAYER //SWS_FULL_CHR_H_INT is currently not supported with SWS_DITHER_BAYER
  1161. && !(c->flags & SWS_FAST_BILINEAR)
  1162. ) {
  1163. av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to input having non subsampled chroma\n");
  1164. flags |= SWS_FULL_CHR_H_INT;
  1165. c->flags = flags;
  1166. }
  1167. }
  1168. if (c->dither == SWS_DITHER_AUTO) {
  1169. if (flags & SWS_ERROR_DIFFUSION)
  1170. c->dither = SWS_DITHER_ED;
  1171. }
  1172. if(dstFormat == AV_PIX_FMT_BGR4_BYTE ||
  1173. dstFormat == AV_PIX_FMT_RGB4_BYTE ||
  1174. dstFormat == AV_PIX_FMT_BGR8 ||
  1175. dstFormat == AV_PIX_FMT_RGB8) {
  1176. if (c->dither == SWS_DITHER_AUTO)
  1177. c->dither = (flags & SWS_FULL_CHR_H_INT) ? SWS_DITHER_ED : SWS_DITHER_BAYER;
  1178. if (!(flags & SWS_FULL_CHR_H_INT)) {
  1179. if (c->dither == SWS_DITHER_ED || c->dither == SWS_DITHER_A_DITHER || c->dither == SWS_DITHER_X_DITHER) {
  1180. av_log(c, AV_LOG_DEBUG,
  1181. "Desired dithering only supported in full chroma interpolation for destination format '%s'\n",
  1182. av_get_pix_fmt_name(dstFormat));
  1183. flags |= SWS_FULL_CHR_H_INT;
  1184. c->flags = flags;
  1185. }
  1186. }
  1187. if (flags & SWS_FULL_CHR_H_INT) {
  1188. if (c->dither == SWS_DITHER_BAYER) {
  1189. av_log(c, AV_LOG_DEBUG,
  1190. "Ordered dither is not supported in full chroma interpolation for destination format '%s'\n",
  1191. av_get_pix_fmt_name(dstFormat));
  1192. c->dither = SWS_DITHER_ED;
  1193. }
  1194. }
  1195. }
  1196. if (isPlanarRGB(dstFormat)) {
  1197. if (!(flags & SWS_FULL_CHR_H_INT)) {
  1198. av_log(c, AV_LOG_DEBUG,
  1199. "%s output is not supported with half chroma resolution, switching to full\n",
  1200. av_get_pix_fmt_name(dstFormat));
  1201. flags |= SWS_FULL_CHR_H_INT;
  1202. c->flags = flags;
  1203. }
  1204. }
  1205. /* reuse chroma for 2 pixels RGB/BGR unless user wants full
  1206. * chroma interpolation */
  1207. if (flags & SWS_FULL_CHR_H_INT &&
  1208. isAnyRGB(dstFormat) &&
  1209. !isPlanarRGB(dstFormat) &&
  1210. dstFormat != AV_PIX_FMT_RGBA64LE &&
  1211. dstFormat != AV_PIX_FMT_RGBA64BE &&
  1212. dstFormat != AV_PIX_FMT_BGRA64LE &&
  1213. dstFormat != AV_PIX_FMT_BGRA64BE &&
  1214. dstFormat != AV_PIX_FMT_RGB48LE &&
  1215. dstFormat != AV_PIX_FMT_RGB48BE &&
  1216. dstFormat != AV_PIX_FMT_BGR48LE &&
  1217. dstFormat != AV_PIX_FMT_BGR48BE &&
  1218. dstFormat != AV_PIX_FMT_RGBA &&
  1219. dstFormat != AV_PIX_FMT_ARGB &&
  1220. dstFormat != AV_PIX_FMT_BGRA &&
  1221. dstFormat != AV_PIX_FMT_ABGR &&
  1222. dstFormat != AV_PIX_FMT_RGB24 &&
  1223. dstFormat != AV_PIX_FMT_BGR24 &&
  1224. dstFormat != AV_PIX_FMT_BGR4_BYTE &&
  1225. dstFormat != AV_PIX_FMT_RGB4_BYTE &&
  1226. dstFormat != AV_PIX_FMT_BGR8 &&
  1227. dstFormat != AV_PIX_FMT_RGB8
  1228. ) {
  1229. av_log(c, AV_LOG_WARNING,
  1230. "full chroma interpolation for destination format '%s' not yet implemented\n",
  1231. av_get_pix_fmt_name(dstFormat));
  1232. flags &= ~SWS_FULL_CHR_H_INT;
  1233. c->flags = flags;
  1234. }
  1235. if (isAnyRGB(dstFormat) && !(flags & SWS_FULL_CHR_H_INT))
  1236. c->chrDstHSubSample = 1;
  1237. // drop some chroma lines if the user wants it
  1238. c->vChrDrop = (flags & SWS_SRC_V_CHR_DROP_MASK) >>
  1239. SWS_SRC_V_CHR_DROP_SHIFT;
  1240. c->chrSrcVSubSample += c->vChrDrop;
  1241. /* drop every other pixel for chroma calculation unless user
  1242. * wants full chroma */
  1243. if (isAnyRGB(srcFormat) && !(flags & SWS_FULL_CHR_H_INP) &&
  1244. srcFormat != AV_PIX_FMT_RGB8 && srcFormat != AV_PIX_FMT_BGR8 &&
  1245. srcFormat != AV_PIX_FMT_RGB4 && srcFormat != AV_PIX_FMT_BGR4 &&
  1246. srcFormat != AV_PIX_FMT_RGB4_BYTE && srcFormat != AV_PIX_FMT_BGR4_BYTE &&
  1247. srcFormat != AV_PIX_FMT_GBRP9BE && srcFormat != AV_PIX_FMT_GBRP9LE &&
  1248. srcFormat != AV_PIX_FMT_GBRP10BE && srcFormat != AV_PIX_FMT_GBRP10LE &&
  1249. srcFormat != AV_PIX_FMT_GBRAP10BE && srcFormat != AV_PIX_FMT_GBRAP10LE &&
  1250. srcFormat != AV_PIX_FMT_GBRP12BE && srcFormat != AV_PIX_FMT_GBRP12LE &&
  1251. srcFormat != AV_PIX_FMT_GBRAP12BE && srcFormat != AV_PIX_FMT_GBRAP12LE &&
  1252. srcFormat != AV_PIX_FMT_GBRP14BE && srcFormat != AV_PIX_FMT_GBRP14LE &&
  1253. srcFormat != AV_PIX_FMT_GBRP16BE && srcFormat != AV_PIX_FMT_GBRP16LE &&
  1254. srcFormat != AV_PIX_FMT_GBRAP16BE && srcFormat != AV_PIX_FMT_GBRAP16LE &&
  1255. ((dstW >> c->chrDstHSubSample) <= (srcW >> 1) ||
  1256. (flags & SWS_FAST_BILINEAR)))
  1257. c->chrSrcHSubSample = 1;
  1258. // Note the AV_CEIL_RSHIFT is so that we always round toward +inf.
  1259. c->chrSrcW = AV_CEIL_RSHIFT(srcW, c->chrSrcHSubSample);
  1260. c->chrSrcH = AV_CEIL_RSHIFT(srcH, c->chrSrcVSubSample);
  1261. c->chrDstW = AV_CEIL_RSHIFT(dstW, c->chrDstHSubSample);
  1262. c->chrDstH = AV_CEIL_RSHIFT(dstH, c->chrDstVSubSample);
  1263. FF_ALLOCZ_OR_GOTO(c, c->formatConvBuffer, FFALIGN(srcW*2+78, 16) * 2, fail);
  1264. c->srcBpc = desc_src->comp[0].depth;
  1265. if (c->srcBpc < 8)
  1266. c->srcBpc = 8;
  1267. c->dstBpc = desc_dst->comp[0].depth;
  1268. if (c->dstBpc < 8)
  1269. c->dstBpc = 8;
  1270. if (isAnyRGB(srcFormat) || srcFormat == AV_PIX_FMT_PAL8)
  1271. c->srcBpc = 16;
  1272. if (c->dstBpc == 16)
  1273. dst_stride <<= 1;
  1274. if (INLINE_MMXEXT(cpu_flags) && c->srcBpc == 8 && c->dstBpc <= 14) {
  1275. c->canMMXEXTBeUsed = dstW >= srcW && (dstW & 31) == 0 &&
  1276. c->chrDstW >= c->chrSrcW &&
  1277. (srcW & 15) == 0;
  1278. if (!c->canMMXEXTBeUsed && dstW >= srcW && c->chrDstW >= c->chrSrcW && (srcW & 15) == 0
  1279. && (flags & SWS_FAST_BILINEAR)) {
  1280. if (flags & SWS_PRINT_INFO)
  1281. av_log(c, AV_LOG_INFO,
  1282. "output width is not a multiple of 32 -> no MMXEXT scaler\n");
  1283. }
  1284. if (usesHFilter || isNBPS(c->srcFormat) || is16BPS(c->srcFormat) || isAnyRGB(c->srcFormat))
  1285. c->canMMXEXTBeUsed = 0;
  1286. } else
  1287. c->canMMXEXTBeUsed = 0;
  1288. c->chrXInc = (((int64_t)c->chrSrcW << 16) + (c->chrDstW >> 1)) / c->chrDstW;
  1289. c->chrYInc = (((int64_t)c->chrSrcH << 16) + (c->chrDstH >> 1)) / c->chrDstH;
  1290. /* Match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src
  1291. * to pixel n-2 of dst, but only for the FAST_BILINEAR mode otherwise do
  1292. * correct scaling.
  1293. * n-2 is the last chrominance sample available.
  1294. * This is not perfect, but no one should notice the difference, the more
  1295. * correct variant would be like the vertical one, but that would require
  1296. * some special code for the first and last pixel */
  1297. if (flags & SWS_FAST_BILINEAR) {
  1298. if (c->canMMXEXTBeUsed) {
  1299. c->lumXInc += 20;
  1300. c->chrXInc += 20;
  1301. }
  1302. // we don't use the x86 asm scaler if MMX is available
  1303. else if (INLINE_MMX(cpu_flags) && c->dstBpc <= 14) {
  1304. c->lumXInc = ((int64_t)(srcW - 2) << 16) / (dstW - 2) - 20;
  1305. c->chrXInc = ((int64_t)(c->chrSrcW - 2) << 16) / (c->chrDstW - 2) - 20;
  1306. }
  1307. }
  1308. // hardcoded for now
  1309. c->gamma_value = 2.2;
  1310. tmpFmt = AV_PIX_FMT_RGBA64LE;
  1311. if (!unscaled && c->gamma_flag && (srcFormat != tmpFmt || dstFormat != tmpFmt)) {
  1312. SwsContext *c2;
  1313. c->cascaded_context[0] = NULL;
  1314. ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
  1315. srcW, srcH, tmpFmt, 64);
  1316. if (ret < 0)
  1317. return ret;
  1318. c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
  1319. srcW, srcH, tmpFmt,
  1320. flags, NULL, NULL, c->param);
  1321. if (!c->cascaded_context[0]) {
  1322. return -1;
  1323. }
  1324. c->cascaded_context[1] = sws_getContext(srcW, srcH, tmpFmt,
  1325. dstW, dstH, tmpFmt,
  1326. flags, srcFilter, dstFilter, c->param);
  1327. if (!c->cascaded_context[1])
  1328. return -1;
  1329. c2 = c->cascaded_context[1];
  1330. c2->is_internal_gamma = 1;
  1331. c2->gamma = alloc_gamma_tbl( c->gamma_value);
  1332. c2->inv_gamma = alloc_gamma_tbl(1.f/c->gamma_value);
  1333. if (!c2->gamma || !c2->inv_gamma)
  1334. return AVERROR(ENOMEM);
  1335. // is_internal_flag is set after creating the context
  1336. // to properly create the gamma convert FilterDescriptor
  1337. // we have to re-initialize it
  1338. ff_free_filters(c2);
  1339. if (ff_init_filters(c2) < 0) {
  1340. sws_freeContext(c2);
  1341. return -1;
  1342. }
  1343. c->cascaded_context[2] = NULL;
  1344. if (dstFormat != tmpFmt) {
  1345. ret = av_image_alloc(c->cascaded1_tmp, c->cascaded1_tmpStride,
  1346. dstW, dstH, tmpFmt, 64);
  1347. if (ret < 0)
  1348. return ret;
  1349. c->cascaded_context[2] = sws_getContext(dstW, dstH, tmpFmt,
  1350. dstW, dstH, dstFormat,
  1351. flags, NULL, NULL, c->param);
  1352. if (!c->cascaded_context[2])
  1353. return -1;
  1354. }
  1355. return 0;
  1356. }
  1357. if (isBayer(srcFormat)) {
  1358. if (!unscaled ||
  1359. (dstFormat != AV_PIX_FMT_RGB24 && dstFormat != AV_PIX_FMT_YUV420P)) {
  1360. enum AVPixelFormat tmpFormat = AV_PIX_FMT_RGB24;
  1361. ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
  1362. srcW, srcH, tmpFormat, 64);
  1363. if (ret < 0)
  1364. return ret;
  1365. c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
  1366. srcW, srcH, tmpFormat,
  1367. flags, srcFilter, NULL, c->param);
  1368. if (!c->cascaded_context[0])
  1369. return -1;
  1370. c->cascaded_context[1] = sws_getContext(srcW, srcH, tmpFormat,
  1371. dstW, dstH, dstFormat,
  1372. flags, NULL, dstFilter, c->param);
  1373. if (!c->cascaded_context[1])
  1374. return -1;
  1375. return 0;
  1376. }
  1377. }
  1378. if (unscaled && c->srcBpc == 8 && dstFormat == AV_PIX_FMT_GRAYF32){
  1379. for (i = 0; i < 256; ++i){
  1380. c->uint2float_lut[i] = (float)i * float_mult;
  1381. }
  1382. }
  1383. // float will be converted to uint16_t
  1384. if ((srcFormat == AV_PIX_FMT_GRAYF32BE || srcFormat == AV_PIX_FMT_GRAYF32LE) &&
  1385. (!unscaled || unscaled && dstFormat != srcFormat && (srcFormat != AV_PIX_FMT_GRAYF32 ||
  1386. dstFormat != AV_PIX_FMT_GRAY8))){
  1387. c->srcBpc = 16;
  1388. }
  1389. if (CONFIG_SWSCALE_ALPHA && isALPHA(srcFormat) && !isALPHA(dstFormat)) {
  1390. enum AVPixelFormat tmpFormat = alphaless_fmt(srcFormat);
  1391. if (tmpFormat != AV_PIX_FMT_NONE && c->alphablend != SWS_ALPHA_BLEND_NONE)
  1392. if (!unscaled ||
  1393. dstFormat != tmpFormat ||
  1394. usesHFilter || usesVFilter ||
  1395. c->srcRange != c->dstRange
  1396. ) {
  1397. c->cascaded_mainindex = 1;
  1398. ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
  1399. srcW, srcH, tmpFormat, 64);
  1400. if (ret < 0)
  1401. return ret;
  1402. c->cascaded_context[0] = sws_alloc_set_opts(srcW, srcH, srcFormat,
  1403. srcW, srcH, tmpFormat,
  1404. flags, c->param);
  1405. if (!c->cascaded_context[0])
  1406. return -1;
  1407. c->cascaded_context[0]->alphablend = c->alphablend;
  1408. ret = sws_init_context(c->cascaded_context[0], NULL , NULL);
  1409. if (ret < 0)
  1410. return ret;
  1411. c->cascaded_context[1] = sws_alloc_set_opts(srcW, srcH, tmpFormat,
  1412. dstW, dstH, dstFormat,
  1413. flags, c->param);
  1414. if (!c->cascaded_context[1])
  1415. return -1;
  1416. c->cascaded_context[1]->srcRange = c->srcRange;
  1417. c->cascaded_context[1]->dstRange = c->dstRange;
  1418. ret = sws_init_context(c->cascaded_context[1], srcFilter , dstFilter);
  1419. if (ret < 0)
  1420. return ret;
  1421. return 0;
  1422. }
  1423. }
  1424. #if HAVE_MMAP && HAVE_MPROTECT && defined(MAP_ANONYMOUS)
  1425. #define USE_MMAP 1
  1426. #else
  1427. #define USE_MMAP 0
  1428. #endif
  1429. /* precalculate horizontal scaler filter coefficients */
  1430. {
  1431. #if HAVE_MMXEXT_INLINE
  1432. // can't downscale !!!
  1433. if (c->canMMXEXTBeUsed && (flags & SWS_FAST_BILINEAR)) {
  1434. c->lumMmxextFilterCodeSize = ff_init_hscaler_mmxext(dstW, c->lumXInc, NULL,
  1435. NULL, NULL, 8);
  1436. c->chrMmxextFilterCodeSize = ff_init_hscaler_mmxext(c->chrDstW, c->chrXInc,
  1437. NULL, NULL, NULL, 4);
  1438. #if USE_MMAP
  1439. c->lumMmxextFilterCode = mmap(NULL, c->lumMmxextFilterCodeSize,
  1440. PROT_READ | PROT_WRITE,
  1441. MAP_PRIVATE | MAP_ANONYMOUS,
  1442. -1, 0);
  1443. c->chrMmxextFilterCode = mmap(NULL, c->chrMmxextFilterCodeSize,
  1444. PROT_READ | PROT_WRITE,
  1445. MAP_PRIVATE | MAP_ANONYMOUS,
  1446. -1, 0);
  1447. #elif HAVE_VIRTUALALLOC
  1448. c->lumMmxextFilterCode = VirtualAlloc(NULL,
  1449. c->lumMmxextFilterCodeSize,
  1450. MEM_COMMIT,
  1451. PAGE_EXECUTE_READWRITE);
  1452. c->chrMmxextFilterCode = VirtualAlloc(NULL,
  1453. c->chrMmxextFilterCodeSize,
  1454. MEM_COMMIT,
  1455. PAGE_EXECUTE_READWRITE);
  1456. #else
  1457. c->lumMmxextFilterCode = av_malloc(c->lumMmxextFilterCodeSize);
  1458. c->chrMmxextFilterCode = av_malloc(c->chrMmxextFilterCodeSize);
  1459. #endif
  1460. #ifdef MAP_ANONYMOUS
  1461. if (c->lumMmxextFilterCode == MAP_FAILED || c->chrMmxextFilterCode == MAP_FAILED)
  1462. #else
  1463. if (!c->lumMmxextFilterCode || !c->chrMmxextFilterCode)
  1464. #endif
  1465. {
  1466. av_log(c, AV_LOG_ERROR, "Failed to allocate MMX2FilterCode\n");
  1467. return AVERROR(ENOMEM);
  1468. }
  1469. FF_ALLOCZ_OR_GOTO(c, c->hLumFilter, (dstW / 8 + 8) * sizeof(int16_t), fail);
  1470. FF_ALLOCZ_OR_GOTO(c, c->hChrFilter, (c->chrDstW / 4 + 8) * sizeof(int16_t), fail);
  1471. FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW / 2 / 8 + 8) * sizeof(int32_t), fail);
  1472. FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW / 2 / 4 + 8) * sizeof(int32_t), fail);
  1473. ff_init_hscaler_mmxext( dstW, c->lumXInc, c->lumMmxextFilterCode,
  1474. c->hLumFilter, (uint32_t*)c->hLumFilterPos, 8);
  1475. ff_init_hscaler_mmxext(c->chrDstW, c->chrXInc, c->chrMmxextFilterCode,
  1476. c->hChrFilter, (uint32_t*)c->hChrFilterPos, 4);
  1477. #if USE_MMAP
  1478. if ( mprotect(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1
  1479. || mprotect(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1) {
  1480. av_log(c, AV_LOG_ERROR, "mprotect failed, cannot use fast bilinear scaler\n");
  1481. goto fail;
  1482. }
  1483. #endif
  1484. } else
  1485. #endif /* HAVE_MMXEXT_INLINE */
  1486. {
  1487. const int filterAlign = X86_MMX(cpu_flags) ? 4 :
  1488. PPC_ALTIVEC(cpu_flags) ? 8 :
  1489. have_neon(cpu_flags) ? 8 : 1;
  1490. if ((ret = initFilter(&c->hLumFilter, &c->hLumFilterPos,
  1491. &c->hLumFilterSize, c->lumXInc,
  1492. srcW, dstW, filterAlign, 1 << 14,
  1493. (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
  1494. cpu_flags, srcFilter->lumH, dstFilter->lumH,
  1495. c->param,
  1496. get_local_pos(c, 0, 0, 0),
  1497. get_local_pos(c, 0, 0, 0))) < 0)
  1498. goto fail;
  1499. if ((ret = initFilter(&c->hChrFilter, &c->hChrFilterPos,
  1500. &c->hChrFilterSize, c->chrXInc,
  1501. c->chrSrcW, c->chrDstW, filterAlign, 1 << 14,
  1502. (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
  1503. cpu_flags, srcFilter->chrH, dstFilter->chrH,
  1504. c->param,
  1505. get_local_pos(c, c->chrSrcHSubSample, c->src_h_chr_pos, 0),
  1506. get_local_pos(c, c->chrDstHSubSample, c->dst_h_chr_pos, 0))) < 0)
  1507. goto fail;
  1508. }
  1509. } // initialize horizontal stuff
  1510. /* precalculate vertical scaler filter coefficients */
  1511. {
  1512. const int filterAlign = X86_MMX(cpu_flags) ? 2 :
  1513. PPC_ALTIVEC(cpu_flags) ? 8 :
  1514. have_neon(cpu_flags) ? 2 : 1;
  1515. if ((ret = initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize,
  1516. c->lumYInc, srcH, dstH, filterAlign, (1 << 12),
  1517. (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
  1518. cpu_flags, srcFilter->lumV, dstFilter->lumV,
  1519. c->param,
  1520. get_local_pos(c, 0, 0, 1),
  1521. get_local_pos(c, 0, 0, 1))) < 0)
  1522. goto fail;
  1523. if ((ret = initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize,
  1524. c->chrYInc, c->chrSrcH, c->chrDstH,
  1525. filterAlign, (1 << 12),
  1526. (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
  1527. cpu_flags, srcFilter->chrV, dstFilter->chrV,
  1528. c->param,
  1529. get_local_pos(c, c->chrSrcVSubSample, c->src_v_chr_pos, 1),
  1530. get_local_pos(c, c->chrDstVSubSample, c->dst_v_chr_pos, 1))) < 0)
  1531. goto fail;
  1532. #if HAVE_ALTIVEC
  1533. FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof(vector signed short) * c->vLumFilterSize * c->dstH, fail);
  1534. FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof(vector signed short) * c->vChrFilterSize * c->chrDstH, fail);
  1535. for (i = 0; i < c->vLumFilterSize * c->dstH; i++) {
  1536. int j;
  1537. short *p = (short *)&c->vYCoeffsBank[i];
  1538. for (j = 0; j < 8; j++)
  1539. p[j] = c->vLumFilter[i];
  1540. }
  1541. for (i = 0; i < c->vChrFilterSize * c->chrDstH; i++) {
  1542. int j;
  1543. short *p = (short *)&c->vCCoeffsBank[i];
  1544. for (j = 0; j < 8; j++)
  1545. p[j] = c->vChrFilter[i];
  1546. }
  1547. #endif
  1548. }
  1549. for (i = 0; i < 4; i++)
  1550. FF_ALLOCZ_OR_GOTO(c, c->dither_error[i], (c->dstW+2) * sizeof(int), fail);
  1551. c->needAlpha = (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat)) ? 1 : 0;
  1552. // 64 / c->scalingBpp is the same as 16 / sizeof(scaling_intermediate)
  1553. c->uv_off = (dst_stride>>1) + 64 / (c->dstBpc &~ 7);
  1554. c->uv_offx2 = dst_stride + 16;
  1555. av_assert0(c->chrDstH <= dstH);
  1556. if (flags & SWS_PRINT_INFO) {
  1557. const char *scaler = NULL, *cpucaps;
  1558. for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) {
  1559. if (flags & scale_algorithms[i].flag) {
  1560. scaler = scale_algorithms[i].description;
  1561. break;
  1562. }
  1563. }
  1564. if (!scaler)
  1565. scaler = "ehh flags invalid?!";
  1566. av_log(c, AV_LOG_INFO, "%s scaler, from %s to %s%s ",
  1567. scaler,
  1568. av_get_pix_fmt_name(srcFormat),
  1569. #ifdef DITHER1XBPP
  1570. dstFormat == AV_PIX_FMT_BGR555 || dstFormat == AV_PIX_FMT_BGR565 ||
  1571. dstFormat == AV_PIX_FMT_RGB444BE || dstFormat == AV_PIX_FMT_RGB444LE ||
  1572. dstFormat == AV_PIX_FMT_BGR444BE || dstFormat == AV_PIX_FMT_BGR444LE ?
  1573. "dithered " : "",
  1574. #else
  1575. "",
  1576. #endif
  1577. av_get_pix_fmt_name(dstFormat));
  1578. if (INLINE_MMXEXT(cpu_flags))
  1579. cpucaps = "MMXEXT";
  1580. else if (INLINE_AMD3DNOW(cpu_flags))
  1581. cpucaps = "3DNOW";
  1582. else if (INLINE_MMX(cpu_flags))
  1583. cpucaps = "MMX";
  1584. else if (PPC_ALTIVEC(cpu_flags))
  1585. cpucaps = "AltiVec";
  1586. else
  1587. cpucaps = "C";
  1588. av_log(c, AV_LOG_INFO, "using %s\n", cpucaps);
  1589. av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
  1590. av_log(c, AV_LOG_DEBUG,
  1591. "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  1592. c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
  1593. av_log(c, AV_LOG_DEBUG,
  1594. "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  1595. c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH,
  1596. c->chrXInc, c->chrYInc);
  1597. }
  1598. /* alpha blend special case, note this has been split via cascaded contexts if its scaled */
  1599. if (unscaled && !usesHFilter && !usesVFilter &&
  1600. c->alphablend != SWS_ALPHA_BLEND_NONE &&
  1601. isALPHA(srcFormat) &&
  1602. (c->srcRange == c->dstRange || isAnyRGB(dstFormat)) &&
  1603. alphaless_fmt(srcFormat) == dstFormat
  1604. ) {
  1605. c->swscale = ff_sws_alphablendaway;
  1606. if (flags & SWS_PRINT_INFO)
  1607. av_log(c, AV_LOG_INFO,
  1608. "using alpha blendaway %s -> %s special converter\n",
  1609. av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
  1610. return 0;
  1611. }
  1612. /* unscaled special cases */
  1613. if (unscaled && !usesHFilter && !usesVFilter &&
  1614. (c->srcRange == c->dstRange || isAnyRGB(dstFormat) ||
  1615. isFloat(srcFormat) || isFloat(dstFormat))){
  1616. ff_get_unscaled_swscale(c);
  1617. if (c->swscale) {
  1618. if (flags & SWS_PRINT_INFO)
  1619. av_log(c, AV_LOG_INFO,
  1620. "using unscaled %s -> %s special converter\n",
  1621. av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
  1622. return 0;
  1623. }
  1624. }
  1625. c->swscale = ff_getSwsFunc(c);
  1626. return ff_init_filters(c);
  1627. fail: // FIXME replace things by appropriate error codes
  1628. if (ret == RETCODE_USE_CASCADE) {
  1629. int tmpW = sqrt(srcW * (int64_t)dstW);
  1630. int tmpH = sqrt(srcH * (int64_t)dstH);
  1631. enum AVPixelFormat tmpFormat = AV_PIX_FMT_YUV420P;
  1632. if (isALPHA(srcFormat))
  1633. tmpFormat = AV_PIX_FMT_YUVA420P;
  1634. if (srcW*(int64_t)srcH <= 4LL*dstW*dstH)
  1635. return AVERROR(EINVAL);
  1636. ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
  1637. tmpW, tmpH, tmpFormat, 64);
  1638. if (ret < 0)
  1639. return ret;
  1640. c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
  1641. tmpW, tmpH, tmpFormat,
  1642. flags, srcFilter, NULL, c->param);
  1643. if (!c->cascaded_context[0])
  1644. return -1;
  1645. c->cascaded_context[1] = sws_getContext(tmpW, tmpH, tmpFormat,
  1646. dstW, dstH, dstFormat,
  1647. flags, NULL, dstFilter, c->param);
  1648. if (!c->cascaded_context[1])
  1649. return -1;
  1650. return 0;
  1651. }
  1652. return -1;
  1653. }
  1654. SwsContext *sws_alloc_set_opts(int srcW, int srcH, enum AVPixelFormat srcFormat,
  1655. int dstW, int dstH, enum AVPixelFormat dstFormat,
  1656. int flags, const double *param)
  1657. {
  1658. SwsContext *c;
  1659. if (!(c = sws_alloc_context()))
  1660. return NULL;
  1661. c->flags = flags;
  1662. c->srcW = srcW;
  1663. c->srcH = srcH;
  1664. c->dstW = dstW;
  1665. c->dstH = dstH;
  1666. c->srcFormat = srcFormat;
  1667. c->dstFormat = dstFormat;
  1668. if (param) {
  1669. c->param[0] = param[0];
  1670. c->param[1] = param[1];
  1671. }
  1672. return c;
  1673. }
  1674. SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,
  1675. int dstW, int dstH, enum AVPixelFormat dstFormat,
  1676. int flags, SwsFilter *srcFilter,
  1677. SwsFilter *dstFilter, const double *param)
  1678. {
  1679. SwsContext *c;
  1680. c = sws_alloc_set_opts(srcW, srcH, srcFormat,
  1681. dstW, dstH, dstFormat,
  1682. flags, param);
  1683. if (!c)
  1684. return NULL;
  1685. if (sws_init_context(c, srcFilter, dstFilter) < 0) {
  1686. sws_freeContext(c);
  1687. return NULL;
  1688. }
  1689. return c;
  1690. }
  1691. static int isnan_vec(SwsVector *a)
  1692. {
  1693. int i;
  1694. for (i=0; i<a->length; i++)
  1695. if (isnan(a->coeff[i]))
  1696. return 1;
  1697. return 0;
  1698. }
  1699. static void makenan_vec(SwsVector *a)
  1700. {
  1701. int i;
  1702. for (i=0; i<a->length; i++)
  1703. a->coeff[i] = NAN;
  1704. }
  1705. SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
  1706. float lumaSharpen, float chromaSharpen,
  1707. float chromaHShift, float chromaVShift,
  1708. int verbose)
  1709. {
  1710. SwsFilter *filter = av_malloc(sizeof(SwsFilter));
  1711. if (!filter)
  1712. return NULL;
  1713. if (lumaGBlur != 0.0) {
  1714. filter->lumH = sws_getGaussianVec(lumaGBlur, 3.0);
  1715. filter->lumV = sws_getGaussianVec(lumaGBlur, 3.0);
  1716. } else {
  1717. filter->lumH = sws_getIdentityVec();
  1718. filter->lumV = sws_getIdentityVec();
  1719. }
  1720. if (chromaGBlur != 0.0) {
  1721. filter->chrH = sws_getGaussianVec(chromaGBlur, 3.0);
  1722. filter->chrV = sws_getGaussianVec(chromaGBlur, 3.0);
  1723. } else {
  1724. filter->chrH = sws_getIdentityVec();
  1725. filter->chrV = sws_getIdentityVec();
  1726. }
  1727. if (!filter->lumH || !filter->lumV || !filter->chrH || !filter->chrV)
  1728. goto fail;
  1729. if (chromaSharpen != 0.0) {
  1730. SwsVector *id = sws_getIdentityVec();
  1731. if (!id)
  1732. goto fail;
  1733. sws_scaleVec(filter->chrH, -chromaSharpen);
  1734. sws_scaleVec(filter->chrV, -chromaSharpen);
  1735. sws_addVec(filter->chrH, id);
  1736. sws_addVec(filter->chrV, id);
  1737. sws_freeVec(id);
  1738. }
  1739. if (lumaSharpen != 0.0) {
  1740. SwsVector *id = sws_getIdentityVec();
  1741. if (!id)
  1742. goto fail;
  1743. sws_scaleVec(filter->lumH, -lumaSharpen);
  1744. sws_scaleVec(filter->lumV, -lumaSharpen);
  1745. sws_addVec(filter->lumH, id);
  1746. sws_addVec(filter->lumV, id);
  1747. sws_freeVec(id);
  1748. }
  1749. if (chromaHShift != 0.0)
  1750. sws_shiftVec(filter->chrH, (int)(chromaHShift + 0.5));
  1751. if (chromaVShift != 0.0)
  1752. sws_shiftVec(filter->chrV, (int)(chromaVShift + 0.5));
  1753. sws_normalizeVec(filter->chrH, 1.0);
  1754. sws_normalizeVec(filter->chrV, 1.0);
  1755. sws_normalizeVec(filter->lumH, 1.0);
  1756. sws_normalizeVec(filter->lumV, 1.0);
  1757. if (isnan_vec(filter->chrH) ||
  1758. isnan_vec(filter->chrV) ||
  1759. isnan_vec(filter->lumH) ||
  1760. isnan_vec(filter->lumV))
  1761. goto fail;
  1762. if (verbose)
  1763. sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
  1764. if (verbose)
  1765. sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
  1766. return filter;
  1767. fail:
  1768. sws_freeVec(filter->lumH);
  1769. sws_freeVec(filter->lumV);
  1770. sws_freeVec(filter->chrH);
  1771. sws_freeVec(filter->chrV);
  1772. av_freep(&filter);
  1773. return NULL;
  1774. }
  1775. SwsVector *sws_allocVec(int length)
  1776. {
  1777. SwsVector *vec;
  1778. if(length <= 0 || length > INT_MAX/ sizeof(double))
  1779. return NULL;
  1780. vec = av_malloc(sizeof(SwsVector));
  1781. if (!vec)
  1782. return NULL;
  1783. vec->length = length;
  1784. vec->coeff = av_malloc(sizeof(double) * length);
  1785. if (!vec->coeff)
  1786. av_freep(&vec);
  1787. return vec;
  1788. }
  1789. SwsVector *sws_getGaussianVec(double variance, double quality)
  1790. {
  1791. const int length = (int)(variance * quality + 0.5) | 1;
  1792. int i;
  1793. double middle = (length - 1) * 0.5;
  1794. SwsVector *vec;
  1795. if(variance < 0 || quality < 0)
  1796. return NULL;
  1797. vec = sws_allocVec(length);
  1798. if (!vec)
  1799. return NULL;
  1800. for (i = 0; i < length; i++) {
  1801. double dist = i - middle;
  1802. vec->coeff[i] = exp(-dist * dist / (2 * variance * variance)) /
  1803. sqrt(2 * variance * M_PI);
  1804. }
  1805. sws_normalizeVec(vec, 1.0);
  1806. return vec;
  1807. }
  1808. /**
  1809. * Allocate and return a vector with length coefficients, all
  1810. * with the same value c.
  1811. */
  1812. #if !FF_API_SWS_VECTOR
  1813. static
  1814. #endif
  1815. SwsVector *sws_getConstVec(double c, int length)
  1816. {
  1817. int i;
  1818. SwsVector *vec = sws_allocVec(length);
  1819. if (!vec)
  1820. return NULL;
  1821. for (i = 0; i < length; i++)
  1822. vec->coeff[i] = c;
  1823. return vec;
  1824. }
  1825. /**
  1826. * Allocate and return a vector with just one coefficient, with
  1827. * value 1.0.
  1828. */
  1829. #if !FF_API_SWS_VECTOR
  1830. static
  1831. #endif
  1832. SwsVector *sws_getIdentityVec(void)
  1833. {
  1834. return sws_getConstVec(1.0, 1);
  1835. }
  1836. static double sws_dcVec(SwsVector *a)
  1837. {
  1838. int i;
  1839. double sum = 0;
  1840. for (i = 0; i < a->length; i++)
  1841. sum += a->coeff[i];
  1842. return sum;
  1843. }
  1844. void sws_scaleVec(SwsVector *a, double scalar)
  1845. {
  1846. int i;
  1847. for (i = 0; i < a->length; i++)
  1848. a->coeff[i] *= scalar;
  1849. }
  1850. void sws_normalizeVec(SwsVector *a, double height)
  1851. {
  1852. sws_scaleVec(a, height / sws_dcVec(a));
  1853. }
  1854. #if FF_API_SWS_VECTOR
  1855. static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
  1856. {
  1857. int length = a->length + b->length - 1;
  1858. int i, j;
  1859. SwsVector *vec = sws_getConstVec(0.0, length);
  1860. if (!vec)
  1861. return NULL;
  1862. for (i = 0; i < a->length; i++) {
  1863. for (j = 0; j < b->length; j++) {
  1864. vec->coeff[i + j] += a->coeff[i] * b->coeff[j];
  1865. }
  1866. }
  1867. return vec;
  1868. }
  1869. #endif
  1870. static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
  1871. {
  1872. int length = FFMAX(a->length, b->length);
  1873. int i;
  1874. SwsVector *vec = sws_getConstVec(0.0, length);
  1875. if (!vec)
  1876. return NULL;
  1877. for (i = 0; i < a->length; i++)
  1878. vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
  1879. for (i = 0; i < b->length; i++)
  1880. vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] += b->coeff[i];
  1881. return vec;
  1882. }
  1883. #if FF_API_SWS_VECTOR
  1884. static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
  1885. {
  1886. int length = FFMAX(a->length, b->length);
  1887. int i;
  1888. SwsVector *vec = sws_getConstVec(0.0, length);
  1889. if (!vec)
  1890. return NULL;
  1891. for (i = 0; i < a->length; i++)
  1892. vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
  1893. for (i = 0; i < b->length; i++)
  1894. vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] -= b->coeff[i];
  1895. return vec;
  1896. }
  1897. #endif
  1898. /* shift left / or right if "shift" is negative */
  1899. static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
  1900. {
  1901. int length = a->length + FFABS(shift) * 2;
  1902. int i;
  1903. SwsVector *vec = sws_getConstVec(0.0, length);
  1904. if (!vec)
  1905. return NULL;
  1906. for (i = 0; i < a->length; i++) {
  1907. vec->coeff[i + (length - 1) / 2 -
  1908. (a->length - 1) / 2 - shift] = a->coeff[i];
  1909. }
  1910. return vec;
  1911. }
  1912. #if !FF_API_SWS_VECTOR
  1913. static
  1914. #endif
  1915. void sws_shiftVec(SwsVector *a, int shift)
  1916. {
  1917. SwsVector *shifted = sws_getShiftedVec(a, shift);
  1918. if (!shifted) {
  1919. makenan_vec(a);
  1920. return;
  1921. }
  1922. av_free(a->coeff);
  1923. a->coeff = shifted->coeff;
  1924. a->length = shifted->length;
  1925. av_free(shifted);
  1926. }
  1927. #if !FF_API_SWS_VECTOR
  1928. static
  1929. #endif
  1930. void sws_addVec(SwsVector *a, SwsVector *b)
  1931. {
  1932. SwsVector *sum = sws_sumVec(a, b);
  1933. if (!sum) {
  1934. makenan_vec(a);
  1935. return;
  1936. }
  1937. av_free(a->coeff);
  1938. a->coeff = sum->coeff;
  1939. a->length = sum->length;
  1940. av_free(sum);
  1941. }
  1942. #if FF_API_SWS_VECTOR
  1943. void sws_subVec(SwsVector *a, SwsVector *b)
  1944. {
  1945. SwsVector *diff = sws_diffVec(a, b);
  1946. if (!diff) {
  1947. makenan_vec(a);
  1948. return;
  1949. }
  1950. av_free(a->coeff);
  1951. a->coeff = diff->coeff;
  1952. a->length = diff->length;
  1953. av_free(diff);
  1954. }
  1955. void sws_convVec(SwsVector *a, SwsVector *b)
  1956. {
  1957. SwsVector *conv = sws_getConvVec(a, b);
  1958. if (!conv) {
  1959. makenan_vec(a);
  1960. return;
  1961. }
  1962. av_free(a->coeff);
  1963. a->coeff = conv->coeff;
  1964. a->length = conv->length;
  1965. av_free(conv);
  1966. }
  1967. SwsVector *sws_cloneVec(SwsVector *a)
  1968. {
  1969. SwsVector *vec = sws_allocVec(a->length);
  1970. if (!vec)
  1971. return NULL;
  1972. memcpy(vec->coeff, a->coeff, a->length * sizeof(*a->coeff));
  1973. return vec;
  1974. }
  1975. #endif
  1976. /**
  1977. * Print with av_log() a textual representation of the vector a
  1978. * if log_level <= av_log_level.
  1979. */
  1980. #if !FF_API_SWS_VECTOR
  1981. static
  1982. #endif
  1983. void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
  1984. {
  1985. int i;
  1986. double max = 0;
  1987. double min = 0;
  1988. double range;
  1989. for (i = 0; i < a->length; i++)
  1990. if (a->coeff[i] > max)
  1991. max = a->coeff[i];
  1992. for (i = 0; i < a->length; i++)
  1993. if (a->coeff[i] < min)
  1994. min = a->coeff[i];
  1995. range = max - min;
  1996. for (i = 0; i < a->length; i++) {
  1997. int x = (int)((a->coeff[i] - min) * 60.0 / range + 0.5);
  1998. av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
  1999. for (; x > 0; x--)
  2000. av_log(log_ctx, log_level, " ");
  2001. av_log(log_ctx, log_level, "|\n");
  2002. }
  2003. }
  2004. void sws_freeVec(SwsVector *a)
  2005. {
  2006. if (!a)
  2007. return;
  2008. av_freep(&a->coeff);
  2009. a->length = 0;
  2010. av_free(a);
  2011. }
  2012. void sws_freeFilter(SwsFilter *filter)
  2013. {
  2014. if (!filter)
  2015. return;
  2016. sws_freeVec(filter->lumH);
  2017. sws_freeVec(filter->lumV);
  2018. sws_freeVec(filter->chrH);
  2019. sws_freeVec(filter->chrV);
  2020. av_free(filter);
  2021. }
  2022. void sws_freeContext(SwsContext *c)
  2023. {
  2024. int i;
  2025. if (!c)
  2026. return;
  2027. for (i = 0; i < 4; i++)
  2028. av_freep(&c->dither_error[i]);
  2029. av_freep(&c->vLumFilter);
  2030. av_freep(&c->vChrFilter);
  2031. av_freep(&c->hLumFilter);
  2032. av_freep(&c->hChrFilter);
  2033. #if HAVE_ALTIVEC
  2034. av_freep(&c->vYCoeffsBank);
  2035. av_freep(&c->vCCoeffsBank);
  2036. #endif
  2037. av_freep(&c->vLumFilterPos);
  2038. av_freep(&c->vChrFilterPos);
  2039. av_freep(&c->hLumFilterPos);
  2040. av_freep(&c->hChrFilterPos);
  2041. #if HAVE_MMX_INLINE
  2042. #if USE_MMAP
  2043. if (c->lumMmxextFilterCode)
  2044. munmap(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize);
  2045. if (c->chrMmxextFilterCode)
  2046. munmap(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize);
  2047. #elif HAVE_VIRTUALALLOC
  2048. if (c->lumMmxextFilterCode)
  2049. VirtualFree(c->lumMmxextFilterCode, 0, MEM_RELEASE);
  2050. if (c->chrMmxextFilterCode)
  2051. VirtualFree(c->chrMmxextFilterCode, 0, MEM_RELEASE);
  2052. #else
  2053. av_free(c->lumMmxextFilterCode);
  2054. av_free(c->chrMmxextFilterCode);
  2055. #endif
  2056. c->lumMmxextFilterCode = NULL;
  2057. c->chrMmxextFilterCode = NULL;
  2058. #endif /* HAVE_MMX_INLINE */
  2059. av_freep(&c->yuvTable);
  2060. av_freep(&c->formatConvBuffer);
  2061. sws_freeContext(c->cascaded_context[0]);
  2062. sws_freeContext(c->cascaded_context[1]);
  2063. sws_freeContext(c->cascaded_context[2]);
  2064. memset(c->cascaded_context, 0, sizeof(c->cascaded_context));
  2065. av_freep(&c->cascaded_tmp[0]);
  2066. av_freep(&c->cascaded1_tmp[0]);
  2067. av_freep(&c->gamma);
  2068. av_freep(&c->inv_gamma);
  2069. ff_free_filters(c);
  2070. av_free(c);
  2071. }
  2072. struct SwsContext *sws_getCachedContext(struct SwsContext *context, int srcW,
  2073. int srcH, enum AVPixelFormat srcFormat,
  2074. int dstW, int dstH,
  2075. enum AVPixelFormat dstFormat, int flags,
  2076. SwsFilter *srcFilter,
  2077. SwsFilter *dstFilter,
  2078. const double *param)
  2079. {
  2080. static const double default_param[2] = { SWS_PARAM_DEFAULT,
  2081. SWS_PARAM_DEFAULT };
  2082. int64_t src_h_chr_pos = -513, dst_h_chr_pos = -513,
  2083. src_v_chr_pos = -513, dst_v_chr_pos = -513;
  2084. if (!param)
  2085. param = default_param;
  2086. if (context &&
  2087. (context->srcW != srcW ||
  2088. context->srcH != srcH ||
  2089. context->srcFormat != srcFormat ||
  2090. context->dstW != dstW ||
  2091. context->dstH != dstH ||
  2092. context->dstFormat != dstFormat ||
  2093. context->flags != flags ||
  2094. context->param[0] != param[0] ||
  2095. context->param[1] != param[1])) {
  2096. av_opt_get_int(context, "src_h_chr_pos", 0, &src_h_chr_pos);
  2097. av_opt_get_int(context, "src_v_chr_pos", 0, &src_v_chr_pos);
  2098. av_opt_get_int(context, "dst_h_chr_pos", 0, &dst_h_chr_pos);
  2099. av_opt_get_int(context, "dst_v_chr_pos", 0, &dst_v_chr_pos);
  2100. sws_freeContext(context);
  2101. context = NULL;
  2102. }
  2103. if (!context) {
  2104. if (!(context = sws_alloc_context()))
  2105. return NULL;
  2106. context->srcW = srcW;
  2107. context->srcH = srcH;
  2108. context->srcFormat = srcFormat;
  2109. context->dstW = dstW;
  2110. context->dstH = dstH;
  2111. context->dstFormat = dstFormat;
  2112. context->flags = flags;
  2113. context->param[0] = param[0];
  2114. context->param[1] = param[1];
  2115. av_opt_set_int(context, "src_h_chr_pos", src_h_chr_pos, 0);
  2116. av_opt_set_int(context, "src_v_chr_pos", src_v_chr_pos, 0);
  2117. av_opt_set_int(context, "dst_h_chr_pos", dst_h_chr_pos, 0);
  2118. av_opt_set_int(context, "dst_v_chr_pos", dst_v_chr_pos, 0);
  2119. if (sws_init_context(context, srcFilter, dstFilter) < 0) {
  2120. sws_freeContext(context);
  2121. return NULL;
  2122. }
  2123. }
  2124. return context;
  2125. }