utils.c 99 KB

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