utils.c 94 KB

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