utils.c 101 KB

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