utils.c 94 KB

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