vf_v360.c 149 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688
  1. /*
  2. * Copyright (c) 2019 Eugene Lyapustin
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * 360 video conversion filter.
  23. * Principle of operation:
  24. *
  25. * (for each pixel in output frame)
  26. * 1) Calculate OpenGL-like coordinates (x, y, z) for pixel position (i, j)
  27. * 2) Apply 360 operations (rotation, mirror) to (x, y, z)
  28. * 3) Calculate pixel position (u, v) in input frame
  29. * 4) Calculate interpolation window and weight for each pixel
  30. *
  31. * (for each frame)
  32. * 5) Remap input frame to output frame using precalculated data
  33. */
  34. #include <math.h>
  35. #include "libavutil/avassert.h"
  36. #include "libavutil/imgutils.h"
  37. #include "libavutil/pixdesc.h"
  38. #include "libavutil/opt.h"
  39. #include "avfilter.h"
  40. #include "formats.h"
  41. #include "internal.h"
  42. #include "video.h"
  43. #include "v360.h"
  44. typedef struct ThreadData {
  45. AVFrame *in;
  46. AVFrame *out;
  47. } ThreadData;
  48. #define OFFSET(x) offsetof(V360Context, x)
  49. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  50. #define TFLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  51. static const AVOption v360_options[] = {
  52. { "input", "set input projection", OFFSET(in), AV_OPT_TYPE_INT, {.i64=EQUIRECTANGULAR}, 0, NB_PROJECTIONS-1, FLAGS, "in" },
  53. { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "in" },
  54. { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "in" },
  55. { "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "in" },
  56. { "c6x1", "cubemap 6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "in" },
  57. { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "in" },
  58. { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "in" },
  59. { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "in" },
  60. {"rectilinear", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "in" },
  61. { "gnomonic", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "in" },
  62. { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
  63. { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
  64. { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "in" },
  65. { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "in" },
  66. { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "in" },
  67. { "ball", "ball", 0, AV_OPT_TYPE_CONST, {.i64=BALL}, 0, 0, FLAGS, "in" },
  68. { "hammer", "hammer", 0, AV_OPT_TYPE_CONST, {.i64=HAMMER}, 0, 0, FLAGS, "in" },
  69. {"sinusoidal", "sinusoidal", 0, AV_OPT_TYPE_CONST, {.i64=SINUSOIDAL}, 0, 0, FLAGS, "in" },
  70. { "fisheye", "fisheye", 0, AV_OPT_TYPE_CONST, {.i64=FISHEYE}, 0, 0, FLAGS, "in" },
  71. { "pannini", "pannini", 0, AV_OPT_TYPE_CONST, {.i64=PANNINI}, 0, 0, FLAGS, "in" },
  72. {"cylindrical", "cylindrical", 0, AV_OPT_TYPE_CONST, {.i64=CYLINDRICAL}, 0, 0, FLAGS, "in" },
  73. {"tetrahedron", "tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=TETRAHEDRON}, 0, 0, FLAGS, "in" },
  74. {"barrelsplit", "barrel split facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL_SPLIT}, 0, 0, FLAGS, "in" },
  75. { "tsp", "truncated square pyramid", 0, AV_OPT_TYPE_CONST, {.i64=TSPYRAMID}, 0, 0, FLAGS, "in" },
  76. { "hequirect", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "in" },
  77. { "he", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "in" },
  78. { "equisolid", "equisolid", 0, AV_OPT_TYPE_CONST, {.i64=EQUISOLID}, 0, 0, FLAGS, "in" },
  79. { "og", "orthographic", 0, AV_OPT_TYPE_CONST, {.i64=ORTHOGRAPHIC}, 0, 0, FLAGS, "in" },
  80. {"octahedron", "octahedron", 0, AV_OPT_TYPE_CONST, {.i64=OCTAHEDRON}, 0, 0, FLAGS, "in" },
  81. { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" },
  82. { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
  83. { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
  84. { "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "out" },
  85. { "c6x1", "cubemap 6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "out" },
  86. { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "out" },
  87. { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "out" },
  88. { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
  89. {"rectilinear", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
  90. { "gnomonic", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
  91. { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
  92. { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
  93. { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "out" },
  94. { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "out" },
  95. { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "out" },
  96. { "ball", "ball", 0, AV_OPT_TYPE_CONST, {.i64=BALL}, 0, 0, FLAGS, "out" },
  97. { "hammer", "hammer", 0, AV_OPT_TYPE_CONST, {.i64=HAMMER}, 0, 0, FLAGS, "out" },
  98. {"sinusoidal", "sinusoidal", 0, AV_OPT_TYPE_CONST, {.i64=SINUSOIDAL}, 0, 0, FLAGS, "out" },
  99. { "fisheye", "fisheye", 0, AV_OPT_TYPE_CONST, {.i64=FISHEYE}, 0, 0, FLAGS, "out" },
  100. { "pannini", "pannini", 0, AV_OPT_TYPE_CONST, {.i64=PANNINI}, 0, 0, FLAGS, "out" },
  101. {"cylindrical", "cylindrical", 0, AV_OPT_TYPE_CONST, {.i64=CYLINDRICAL}, 0, 0, FLAGS, "out" },
  102. {"perspective", "perspective", 0, AV_OPT_TYPE_CONST, {.i64=PERSPECTIVE}, 0, 0, FLAGS, "out" },
  103. {"tetrahedron", "tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=TETRAHEDRON}, 0, 0, FLAGS, "out" },
  104. {"barrelsplit", "barrel split facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL_SPLIT}, 0, 0, FLAGS, "out" },
  105. { "tsp", "truncated square pyramid", 0, AV_OPT_TYPE_CONST, {.i64=TSPYRAMID}, 0, 0, FLAGS, "out" },
  106. { "hequirect", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "out" },
  107. { "he", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "out" },
  108. { "equisolid", "equisolid", 0, AV_OPT_TYPE_CONST, {.i64=EQUISOLID}, 0, 0, FLAGS, "out" },
  109. { "og", "orthographic", 0, AV_OPT_TYPE_CONST, {.i64=ORTHOGRAPHIC}, 0, 0, FLAGS, "out" },
  110. {"octahedron", "octahedron", 0, AV_OPT_TYPE_CONST, {.i64=OCTAHEDRON}, 0, 0, FLAGS, "out" },
  111. { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" },
  112. { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
  113. { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
  114. { "line", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
  115. { "linear", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
  116. { "lagrange9", "lagrange9 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LAGRANGE9}, 0, 0, FLAGS, "interp" },
  117. { "cube", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
  118. { "cubic", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
  119. { "lanc", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
  120. { "lanczos", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
  121. { "sp16", "spline16 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=SPLINE16}, 0, 0, FLAGS, "interp" },
  122. { "spline16", "spline16 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=SPLINE16}, 0, 0, FLAGS, "interp" },
  123. { "gauss", "gaussian interpolation", 0, AV_OPT_TYPE_CONST, {.i64=GAUSSIAN}, 0, 0, FLAGS, "interp" },
  124. { "gaussian", "gaussian interpolation", 0, AV_OPT_TYPE_CONST, {.i64=GAUSSIAN}, 0, 0, FLAGS, "interp" },
  125. { "w", "output width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "w"},
  126. { "h", "output height", OFFSET(height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "h"},
  127. { "in_stereo", "input stereo format", OFFSET(in_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
  128. {"out_stereo", "output stereo format", OFFSET(out_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
  129. { "2d", "2d mono", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_2D}, 0, 0, FLAGS, "stereo" },
  130. { "sbs", "side by side", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_SBS}, 0, 0, FLAGS, "stereo" },
  131. { "tb", "top bottom", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_TB}, 0, 0, FLAGS, "stereo" },
  132. { "in_forder", "input cubemap face order", OFFSET(in_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "in_forder"},
  133. {"out_forder", "output cubemap face order", OFFSET(out_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "out_forder"},
  134. { "in_frot", "input cubemap face rotation", OFFSET(in_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "in_frot"},
  135. { "out_frot", "output cubemap face rotation",OFFSET(out_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "out_frot"},
  136. { "in_pad", "percent input cubemap pads", OFFSET(in_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 0.1,TFLAGS, "in_pad"},
  137. { "out_pad", "percent output cubemap pads", OFFSET(out_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 0.1,TFLAGS, "out_pad"},
  138. { "fin_pad", "fixed input cubemap pads", OFFSET(fin_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100,TFLAGS, "fin_pad"},
  139. { "fout_pad", "fixed output cubemap pads", OFFSET(fout_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100,TFLAGS, "fout_pad"},
  140. { "yaw", "yaw rotation", OFFSET(yaw), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "yaw"},
  141. { "pitch", "pitch rotation", OFFSET(pitch), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "pitch"},
  142. { "roll", "roll rotation", OFFSET(roll), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "roll"},
  143. { "rorder", "rotation order", OFFSET(rorder), AV_OPT_TYPE_STRING, {.str="ypr"}, 0, 0,TFLAGS, "rorder"},
  144. { "h_fov", "output horizontal field of view",OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f,TFLAGS, "h_fov"},
  145. { "v_fov", "output vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f,TFLAGS, "v_fov"},
  146. { "d_fov", "output diagonal field of view", OFFSET(d_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "d_fov"},
  147. { "h_flip", "flip out video horizontally", OFFSET(h_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "h_flip"},
  148. { "v_flip", "flip out video vertically", OFFSET(v_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "v_flip"},
  149. { "d_flip", "flip out video indepth", OFFSET(d_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "d_flip"},
  150. { "ih_flip", "flip in video horizontally", OFFSET(ih_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "ih_flip"},
  151. { "iv_flip", "flip in video vertically", OFFSET(iv_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "iv_flip"},
  152. { "in_trans", "transpose video input", OFFSET(in_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "in_transpose"},
  153. { "out_trans", "transpose video output", OFFSET(out_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "out_transpose"},
  154. { "ih_fov", "input horizontal field of view",OFFSET(ih_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f,TFLAGS, "ih_fov"},
  155. { "iv_fov", "input vertical field of view", OFFSET(iv_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f,TFLAGS, "iv_fov"},
  156. { "id_fov", "input diagonal field of view", OFFSET(id_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "id_fov"},
  157. {"alpha_mask", "build mask in alpha plane", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "alpha"},
  158. { NULL }
  159. };
  160. AVFILTER_DEFINE_CLASS(v360);
  161. static int query_formats(AVFilterContext *ctx)
  162. {
  163. V360Context *s = ctx->priv;
  164. static const enum AVPixelFormat pix_fmts[] = {
  165. // YUVA444
  166. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
  167. AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
  168. AV_PIX_FMT_YUVA444P16,
  169. // YUVA422
  170. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
  171. AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
  172. AV_PIX_FMT_YUVA422P16,
  173. // YUVA420
  174. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
  175. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
  176. // YUVJ
  177. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  178. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  179. AV_PIX_FMT_YUVJ411P,
  180. // YUV444
  181. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P9,
  182. AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
  183. AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
  184. // YUV440
  185. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV440P10,
  186. AV_PIX_FMT_YUV440P12,
  187. // YUV422
  188. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P9,
  189. AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
  190. AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16,
  191. // YUV420
  192. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P9,
  193. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
  194. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16,
  195. // YUV411
  196. AV_PIX_FMT_YUV411P,
  197. // YUV410
  198. AV_PIX_FMT_YUV410P,
  199. // GBR
  200. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9,
  201. AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
  202. AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  203. // GBRA
  204. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
  205. AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  206. // GRAY
  207. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
  208. AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
  209. AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  210. AV_PIX_FMT_NONE
  211. };
  212. static const enum AVPixelFormat alpha_pix_fmts[] = {
  213. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
  214. AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
  215. AV_PIX_FMT_YUVA444P16,
  216. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
  217. AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
  218. AV_PIX_FMT_YUVA422P16,
  219. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
  220. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
  221. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
  222. AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  223. AV_PIX_FMT_NONE
  224. };
  225. AVFilterFormats *fmts_list = ff_make_format_list(s->alpha ? alpha_pix_fmts : pix_fmts);
  226. if (!fmts_list)
  227. return AVERROR(ENOMEM);
  228. return ff_set_common_formats(ctx, fmts_list);
  229. }
  230. #define DEFINE_REMAP1_LINE(bits, div) \
  231. static void remap1_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *const src, \
  232. ptrdiff_t in_linesize, \
  233. const int16_t *const u, const int16_t *const v, \
  234. const int16_t *const ker) \
  235. { \
  236. const uint##bits##_t *const s = (const uint##bits##_t *const)src; \
  237. uint##bits##_t *d = (uint##bits##_t *)dst; \
  238. \
  239. in_linesize /= div; \
  240. \
  241. for (int x = 0; x < width; x++) \
  242. d[x] = s[v[x] * in_linesize + u[x]]; \
  243. }
  244. DEFINE_REMAP1_LINE( 8, 1)
  245. DEFINE_REMAP1_LINE(16, 2)
  246. /**
  247. * Generate remapping function with a given window size and pixel depth.
  248. *
  249. * @param ws size of interpolation window
  250. * @param bits number of bits per pixel
  251. */
  252. #define DEFINE_REMAP(ws, bits) \
  253. static int remap##ws##_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
  254. { \
  255. ThreadData *td = arg; \
  256. const V360Context *s = ctx->priv; \
  257. const SliceXYRemap *r = &s->slice_remap[jobnr]; \
  258. const AVFrame *in = td->in; \
  259. AVFrame *out = td->out; \
  260. \
  261. for (int stereo = 0; stereo < 1 + s->out_stereo > STEREO_2D; stereo++) { \
  262. for (int plane = 0; plane < s->nb_planes; plane++) { \
  263. const unsigned map = s->map[plane]; \
  264. const int in_linesize = in->linesize[plane]; \
  265. const int out_linesize = out->linesize[plane]; \
  266. const int uv_linesize = s->uv_linesize[plane]; \
  267. const int in_offset_w = stereo ? s->in_offset_w[plane] : 0; \
  268. const int in_offset_h = stereo ? s->in_offset_h[plane] : 0; \
  269. const int out_offset_w = stereo ? s->out_offset_w[plane] : 0; \
  270. const int out_offset_h = stereo ? s->out_offset_h[plane] : 0; \
  271. const uint8_t *const src = in->data[plane] + \
  272. in_offset_h * in_linesize + in_offset_w * (bits >> 3); \
  273. uint8_t *dst = out->data[plane] + out_offset_h * out_linesize + out_offset_w * (bits >> 3); \
  274. const uint8_t *mask = plane == 3 ? r->mask : NULL; \
  275. const int width = s->pr_width[plane]; \
  276. const int height = s->pr_height[plane]; \
  277. \
  278. const int slice_start = (height * jobnr ) / nb_jobs; \
  279. const int slice_end = (height * (jobnr + 1)) / nb_jobs; \
  280. \
  281. for (int y = slice_start; y < slice_end && !mask; y++) { \
  282. const int16_t *const u = r->u[map] + (y - slice_start) * uv_linesize * ws * ws; \
  283. const int16_t *const v = r->v[map] + (y - slice_start) * uv_linesize * ws * ws; \
  284. const int16_t *const ker = r->ker[map] + (y - slice_start) * uv_linesize * ws * ws; \
  285. \
  286. s->remap_line(dst + y * out_linesize, width, src, in_linesize, u, v, ker); \
  287. } \
  288. \
  289. for (int y = slice_start; y < slice_end && mask; y++) { \
  290. memcpy(dst + y * out_linesize, mask + \
  291. (y - slice_start) * width * (bits >> 3), width * (bits >> 3)); \
  292. } \
  293. } \
  294. } \
  295. \
  296. return 0; \
  297. }
  298. DEFINE_REMAP(1, 8)
  299. DEFINE_REMAP(2, 8)
  300. DEFINE_REMAP(3, 8)
  301. DEFINE_REMAP(4, 8)
  302. DEFINE_REMAP(1, 16)
  303. DEFINE_REMAP(2, 16)
  304. DEFINE_REMAP(3, 16)
  305. DEFINE_REMAP(4, 16)
  306. #define DEFINE_REMAP_LINE(ws, bits, div) \
  307. static void remap##ws##_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *const src, \
  308. ptrdiff_t in_linesize, \
  309. const int16_t *const u, const int16_t *const v, \
  310. const int16_t *const ker) \
  311. { \
  312. const uint##bits##_t *const s = (const uint##bits##_t *const)src; \
  313. uint##bits##_t *d = (uint##bits##_t *)dst; \
  314. \
  315. in_linesize /= div; \
  316. \
  317. for (int x = 0; x < width; x++) { \
  318. const int16_t *const uu = u + x * ws * ws; \
  319. const int16_t *const vv = v + x * ws * ws; \
  320. const int16_t *const kker = ker + x * ws * ws; \
  321. int tmp = 0; \
  322. \
  323. for (int i = 0; i < ws; i++) { \
  324. const int iws = i * ws; \
  325. for (int j = 0; j < ws; j++) { \
  326. tmp += kker[iws + j] * s[vv[iws + j] * in_linesize + uu[iws + j]]; \
  327. } \
  328. } \
  329. \
  330. d[x] = av_clip_uint##bits(tmp >> 14); \
  331. } \
  332. }
  333. DEFINE_REMAP_LINE(2, 8, 1)
  334. DEFINE_REMAP_LINE(3, 8, 1)
  335. DEFINE_REMAP_LINE(4, 8, 1)
  336. DEFINE_REMAP_LINE(2, 16, 2)
  337. DEFINE_REMAP_LINE(3, 16, 2)
  338. DEFINE_REMAP_LINE(4, 16, 2)
  339. void ff_v360_init(V360Context *s, int depth)
  340. {
  341. switch (s->interp) {
  342. case NEAREST:
  343. s->remap_line = depth <= 8 ? remap1_8bit_line_c : remap1_16bit_line_c;
  344. break;
  345. case BILINEAR:
  346. s->remap_line = depth <= 8 ? remap2_8bit_line_c : remap2_16bit_line_c;
  347. break;
  348. case LAGRANGE9:
  349. s->remap_line = depth <= 8 ? remap3_8bit_line_c : remap3_16bit_line_c;
  350. break;
  351. case BICUBIC:
  352. case LANCZOS:
  353. case SPLINE16:
  354. case GAUSSIAN:
  355. s->remap_line = depth <= 8 ? remap4_8bit_line_c : remap4_16bit_line_c;
  356. break;
  357. }
  358. if (ARCH_X86)
  359. ff_v360_init_x86(s, depth);
  360. }
  361. /**
  362. * Save nearest pixel coordinates for remapping.
  363. *
  364. * @param du horizontal relative coordinate
  365. * @param dv vertical relative coordinate
  366. * @param rmap calculated 4x4 window
  367. * @param u u remap data
  368. * @param v v remap data
  369. * @param ker ker remap data
  370. */
  371. static void nearest_kernel(float du, float dv, const XYRemap *rmap,
  372. int16_t *u, int16_t *v, int16_t *ker)
  373. {
  374. const int i = lrintf(dv) + 1;
  375. const int j = lrintf(du) + 1;
  376. u[0] = rmap->u[i][j];
  377. v[0] = rmap->v[i][j];
  378. }
  379. /**
  380. * Calculate kernel for bilinear interpolation.
  381. *
  382. * @param du horizontal relative coordinate
  383. * @param dv vertical relative coordinate
  384. * @param rmap calculated 4x4 window
  385. * @param u u remap data
  386. * @param v v remap data
  387. * @param ker ker remap data
  388. */
  389. static void bilinear_kernel(float du, float dv, const XYRemap *rmap,
  390. int16_t *u, int16_t *v, int16_t *ker)
  391. {
  392. for (int i = 0; i < 2; i++) {
  393. for (int j = 0; j < 2; j++) {
  394. u[i * 2 + j] = rmap->u[i + 1][j + 1];
  395. v[i * 2 + j] = rmap->v[i + 1][j + 1];
  396. }
  397. }
  398. ker[0] = lrintf((1.f - du) * (1.f - dv) * 16385.f);
  399. ker[1] = lrintf( du * (1.f - dv) * 16385.f);
  400. ker[2] = lrintf((1.f - du) * dv * 16385.f);
  401. ker[3] = lrintf( du * dv * 16385.f);
  402. }
  403. /**
  404. * Calculate 1-dimensional lagrange coefficients.
  405. *
  406. * @param t relative coordinate
  407. * @param coeffs coefficients
  408. */
  409. static inline void calculate_lagrange_coeffs(float t, float *coeffs)
  410. {
  411. coeffs[0] = (t - 1.f) * (t - 2.f) * 0.5f;
  412. coeffs[1] = -t * (t - 2.f);
  413. coeffs[2] = t * (t - 1.f) * 0.5f;
  414. }
  415. /**
  416. * Calculate kernel for lagrange interpolation.
  417. *
  418. * @param du horizontal relative coordinate
  419. * @param dv vertical relative coordinate
  420. * @param rmap calculated 4x4 window
  421. * @param u u remap data
  422. * @param v v remap data
  423. * @param ker ker remap data
  424. */
  425. static void lagrange_kernel(float du, float dv, const XYRemap *rmap,
  426. int16_t *u, int16_t *v, int16_t *ker)
  427. {
  428. float du_coeffs[3];
  429. float dv_coeffs[3];
  430. calculate_lagrange_coeffs(du, du_coeffs);
  431. calculate_lagrange_coeffs(dv, dv_coeffs);
  432. for (int i = 0; i < 3; i++) {
  433. for (int j = 0; j < 3; j++) {
  434. u[i * 3 + j] = rmap->u[i + 1][j + 1];
  435. v[i * 3 + j] = rmap->v[i + 1][j + 1];
  436. ker[i * 3 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
  437. }
  438. }
  439. }
  440. /**
  441. * Calculate 1-dimensional cubic coefficients.
  442. *
  443. * @param t relative coordinate
  444. * @param coeffs coefficients
  445. */
  446. static inline void calculate_bicubic_coeffs(float t, float *coeffs)
  447. {
  448. const float tt = t * t;
  449. const float ttt = t * t * t;
  450. coeffs[0] = - t / 3.f + tt / 2.f - ttt / 6.f;
  451. coeffs[1] = 1.f - t / 2.f - tt + ttt / 2.f;
  452. coeffs[2] = t + tt / 2.f - ttt / 2.f;
  453. coeffs[3] = - t / 6.f + ttt / 6.f;
  454. }
  455. /**
  456. * Calculate kernel for bicubic interpolation.
  457. *
  458. * @param du horizontal relative coordinate
  459. * @param dv vertical relative coordinate
  460. * @param rmap calculated 4x4 window
  461. * @param u u remap data
  462. * @param v v remap data
  463. * @param ker ker remap data
  464. */
  465. static void bicubic_kernel(float du, float dv, const XYRemap *rmap,
  466. int16_t *u, int16_t *v, int16_t *ker)
  467. {
  468. float du_coeffs[4];
  469. float dv_coeffs[4];
  470. calculate_bicubic_coeffs(du, du_coeffs);
  471. calculate_bicubic_coeffs(dv, dv_coeffs);
  472. for (int i = 0; i < 4; i++) {
  473. for (int j = 0; j < 4; j++) {
  474. u[i * 4 + j] = rmap->u[i][j];
  475. v[i * 4 + j] = rmap->v[i][j];
  476. ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
  477. }
  478. }
  479. }
  480. /**
  481. * Calculate 1-dimensional lanczos coefficients.
  482. *
  483. * @param t relative coordinate
  484. * @param coeffs coefficients
  485. */
  486. static inline void calculate_lanczos_coeffs(float t, float *coeffs)
  487. {
  488. float sum = 0.f;
  489. for (int i = 0; i < 4; i++) {
  490. const float x = M_PI * (t - i + 1);
  491. if (x == 0.f) {
  492. coeffs[i] = 1.f;
  493. } else {
  494. coeffs[i] = sinf(x) * sinf(x / 2.f) / (x * x / 2.f);
  495. }
  496. sum += coeffs[i];
  497. }
  498. for (int i = 0; i < 4; i++) {
  499. coeffs[i] /= sum;
  500. }
  501. }
  502. /**
  503. * Calculate kernel for lanczos interpolation.
  504. *
  505. * @param du horizontal relative coordinate
  506. * @param dv vertical relative coordinate
  507. * @param rmap calculated 4x4 window
  508. * @param u u remap data
  509. * @param v v remap data
  510. * @param ker ker remap data
  511. */
  512. static void lanczos_kernel(float du, float dv, const XYRemap *rmap,
  513. int16_t *u, int16_t *v, int16_t *ker)
  514. {
  515. float du_coeffs[4];
  516. float dv_coeffs[4];
  517. calculate_lanczos_coeffs(du, du_coeffs);
  518. calculate_lanczos_coeffs(dv, dv_coeffs);
  519. for (int i = 0; i < 4; i++) {
  520. for (int j = 0; j < 4; j++) {
  521. u[i * 4 + j] = rmap->u[i][j];
  522. v[i * 4 + j] = rmap->v[i][j];
  523. ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
  524. }
  525. }
  526. }
  527. /**
  528. * Calculate 1-dimensional spline16 coefficients.
  529. *
  530. * @param t relative coordinate
  531. * @param coeffs coefficients
  532. */
  533. static void calculate_spline16_coeffs(float t, float *coeffs)
  534. {
  535. coeffs[0] = ((-1.f / 3.f * t + 0.8f) * t - 7.f / 15.f) * t;
  536. coeffs[1] = ((t - 9.f / 5.f) * t - 0.2f) * t + 1.f;
  537. coeffs[2] = ((6.f / 5.f - t) * t + 0.8f) * t;
  538. coeffs[3] = ((1.f / 3.f * t - 0.2f) * t - 2.f / 15.f) * t;
  539. }
  540. /**
  541. * Calculate kernel for spline16 interpolation.
  542. *
  543. * @param du horizontal relative coordinate
  544. * @param dv vertical relative coordinate
  545. * @param rmap calculated 4x4 window
  546. * @param u u remap data
  547. * @param v v remap data
  548. * @param ker ker remap data
  549. */
  550. static void spline16_kernel(float du, float dv, const XYRemap *rmap,
  551. int16_t *u, int16_t *v, int16_t *ker)
  552. {
  553. float du_coeffs[4];
  554. float dv_coeffs[4];
  555. calculate_spline16_coeffs(du, du_coeffs);
  556. calculate_spline16_coeffs(dv, dv_coeffs);
  557. for (int i = 0; i < 4; i++) {
  558. for (int j = 0; j < 4; j++) {
  559. u[i * 4 + j] = rmap->u[i][j];
  560. v[i * 4 + j] = rmap->v[i][j];
  561. ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
  562. }
  563. }
  564. }
  565. /**
  566. * Calculate 1-dimensional gaussian coefficients.
  567. *
  568. * @param t relative coordinate
  569. * @param coeffs coefficients
  570. */
  571. static void calculate_gaussian_coeffs(float t, float *coeffs)
  572. {
  573. float sum = 0.f;
  574. for (int i = 0; i < 4; i++) {
  575. const float x = t - (i - 1);
  576. if (x == 0.f) {
  577. coeffs[i] = 1.f;
  578. } else {
  579. coeffs[i] = expf(-2.f * x * x) * expf(-x * x / 2.f);
  580. }
  581. sum += coeffs[i];
  582. }
  583. for (int i = 0; i < 4; i++) {
  584. coeffs[i] /= sum;
  585. }
  586. }
  587. /**
  588. * Calculate kernel for gaussian interpolation.
  589. *
  590. * @param du horizontal relative coordinate
  591. * @param dv vertical relative coordinate
  592. * @param rmap calculated 4x4 window
  593. * @param u u remap data
  594. * @param v v remap data
  595. * @param ker ker remap data
  596. */
  597. static void gaussian_kernel(float du, float dv, const XYRemap *rmap,
  598. int16_t *u, int16_t *v, int16_t *ker)
  599. {
  600. float du_coeffs[4];
  601. float dv_coeffs[4];
  602. calculate_gaussian_coeffs(du, du_coeffs);
  603. calculate_gaussian_coeffs(dv, dv_coeffs);
  604. for (int i = 0; i < 4; i++) {
  605. for (int j = 0; j < 4; j++) {
  606. u[i * 4 + j] = rmap->u[i][j];
  607. v[i * 4 + j] = rmap->v[i][j];
  608. ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
  609. }
  610. }
  611. }
  612. /**
  613. * Modulo operation with only positive remainders.
  614. *
  615. * @param a dividend
  616. * @param b divisor
  617. *
  618. * @return positive remainder of (a / b)
  619. */
  620. static inline int mod(int a, int b)
  621. {
  622. const int res = a % b;
  623. if (res < 0) {
  624. return res + b;
  625. } else {
  626. return res;
  627. }
  628. }
  629. /**
  630. * Reflect y operation.
  631. *
  632. * @param y input vertical position
  633. * @param h input height
  634. */
  635. static inline int reflecty(int y, int h)
  636. {
  637. if (y < 0) {
  638. return -y;
  639. } else if (y >= h) {
  640. return 2 * h - 1 - y;
  641. }
  642. return y;
  643. }
  644. /**
  645. * Reflect x operation for equirect.
  646. *
  647. * @param x input horizontal position
  648. * @param y input vertical position
  649. * @param w input width
  650. * @param h input height
  651. */
  652. static inline int ereflectx(int x, int y, int w, int h)
  653. {
  654. if (y < 0 || y >= h)
  655. x += w / 2;
  656. return mod(x, w);
  657. }
  658. /**
  659. * Reflect x operation.
  660. *
  661. * @param x input horizontal position
  662. * @param y input vertical position
  663. * @param w input width
  664. * @param h input height
  665. */
  666. static inline int reflectx(int x, int y, int w, int h)
  667. {
  668. if (y < 0 || y >= h)
  669. return w - 1 - x;
  670. return mod(x, w);
  671. }
  672. /**
  673. * Convert char to corresponding direction.
  674. * Used for cubemap options.
  675. */
  676. static int get_direction(char c)
  677. {
  678. switch (c) {
  679. case 'r':
  680. return RIGHT;
  681. case 'l':
  682. return LEFT;
  683. case 'u':
  684. return UP;
  685. case 'd':
  686. return DOWN;
  687. case 'f':
  688. return FRONT;
  689. case 'b':
  690. return BACK;
  691. default:
  692. return -1;
  693. }
  694. }
  695. /**
  696. * Convert char to corresponding rotation angle.
  697. * Used for cubemap options.
  698. */
  699. static int get_rotation(char c)
  700. {
  701. switch (c) {
  702. case '0':
  703. return ROT_0;
  704. case '1':
  705. return ROT_90;
  706. case '2':
  707. return ROT_180;
  708. case '3':
  709. return ROT_270;
  710. default:
  711. return -1;
  712. }
  713. }
  714. /**
  715. * Convert char to corresponding rotation order.
  716. */
  717. static int get_rorder(char c)
  718. {
  719. switch (c) {
  720. case 'Y':
  721. case 'y':
  722. return YAW;
  723. case 'P':
  724. case 'p':
  725. return PITCH;
  726. case 'R':
  727. case 'r':
  728. return ROLL;
  729. default:
  730. return -1;
  731. }
  732. }
  733. /**
  734. * Prepare data for processing cubemap input format.
  735. *
  736. * @param ctx filter context
  737. *
  738. * @return error code
  739. */
  740. static int prepare_cube_in(AVFilterContext *ctx)
  741. {
  742. V360Context *s = ctx->priv;
  743. for (int face = 0; face < NB_FACES; face++) {
  744. const char c = s->in_forder[face];
  745. int direction;
  746. if (c == '\0') {
  747. av_log(ctx, AV_LOG_ERROR,
  748. "Incomplete in_forder option. Direction for all 6 faces should be specified.\n");
  749. return AVERROR(EINVAL);
  750. }
  751. direction = get_direction(c);
  752. if (direction == -1) {
  753. av_log(ctx, AV_LOG_ERROR,
  754. "Incorrect direction symbol '%c' in in_forder option.\n", c);
  755. return AVERROR(EINVAL);
  756. }
  757. s->in_cubemap_face_order[direction] = face;
  758. }
  759. for (int face = 0; face < NB_FACES; face++) {
  760. const char c = s->in_frot[face];
  761. int rotation;
  762. if (c == '\0') {
  763. av_log(ctx, AV_LOG_ERROR,
  764. "Incomplete in_frot option. Rotation for all 6 faces should be specified.\n");
  765. return AVERROR(EINVAL);
  766. }
  767. rotation = get_rotation(c);
  768. if (rotation == -1) {
  769. av_log(ctx, AV_LOG_ERROR,
  770. "Incorrect rotation symbol '%c' in in_frot option.\n", c);
  771. return AVERROR(EINVAL);
  772. }
  773. s->in_cubemap_face_rotation[face] = rotation;
  774. }
  775. return 0;
  776. }
  777. /**
  778. * Prepare data for processing cubemap output format.
  779. *
  780. * @param ctx filter context
  781. *
  782. * @return error code
  783. */
  784. static int prepare_cube_out(AVFilterContext *ctx)
  785. {
  786. V360Context *s = ctx->priv;
  787. for (int face = 0; face < NB_FACES; face++) {
  788. const char c = s->out_forder[face];
  789. int direction;
  790. if (c == '\0') {
  791. av_log(ctx, AV_LOG_ERROR,
  792. "Incomplete out_forder option. Direction for all 6 faces should be specified.\n");
  793. return AVERROR(EINVAL);
  794. }
  795. direction = get_direction(c);
  796. if (direction == -1) {
  797. av_log(ctx, AV_LOG_ERROR,
  798. "Incorrect direction symbol '%c' in out_forder option.\n", c);
  799. return AVERROR(EINVAL);
  800. }
  801. s->out_cubemap_direction_order[face] = direction;
  802. }
  803. for (int face = 0; face < NB_FACES; face++) {
  804. const char c = s->out_frot[face];
  805. int rotation;
  806. if (c == '\0') {
  807. av_log(ctx, AV_LOG_ERROR,
  808. "Incomplete out_frot option. Rotation for all 6 faces should be specified.\n");
  809. return AVERROR(EINVAL);
  810. }
  811. rotation = get_rotation(c);
  812. if (rotation == -1) {
  813. av_log(ctx, AV_LOG_ERROR,
  814. "Incorrect rotation symbol '%c' in out_frot option.\n", c);
  815. return AVERROR(EINVAL);
  816. }
  817. s->out_cubemap_face_rotation[face] = rotation;
  818. }
  819. return 0;
  820. }
  821. static inline void rotate_cube_face(float *uf, float *vf, int rotation)
  822. {
  823. float tmp;
  824. switch (rotation) {
  825. case ROT_0:
  826. break;
  827. case ROT_90:
  828. tmp = *uf;
  829. *uf = -*vf;
  830. *vf = tmp;
  831. break;
  832. case ROT_180:
  833. *uf = -*uf;
  834. *vf = -*vf;
  835. break;
  836. case ROT_270:
  837. tmp = -*uf;
  838. *uf = *vf;
  839. *vf = tmp;
  840. break;
  841. default:
  842. av_assert0(0);
  843. }
  844. }
  845. static inline void rotate_cube_face_inverse(float *uf, float *vf, int rotation)
  846. {
  847. float tmp;
  848. switch (rotation) {
  849. case ROT_0:
  850. break;
  851. case ROT_90:
  852. tmp = -*uf;
  853. *uf = *vf;
  854. *vf = tmp;
  855. break;
  856. case ROT_180:
  857. *uf = -*uf;
  858. *vf = -*vf;
  859. break;
  860. case ROT_270:
  861. tmp = *uf;
  862. *uf = -*vf;
  863. *vf = tmp;
  864. break;
  865. default:
  866. av_assert0(0);
  867. }
  868. }
  869. /**
  870. * Normalize vector.
  871. *
  872. * @param vec vector
  873. */
  874. static void normalize_vector(float *vec)
  875. {
  876. const float norm = sqrtf(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
  877. vec[0] /= norm;
  878. vec[1] /= norm;
  879. vec[2] /= norm;
  880. }
  881. /**
  882. * Calculate 3D coordinates on sphere for corresponding cubemap position.
  883. * Common operation for every cubemap.
  884. *
  885. * @param s filter private context
  886. * @param uf horizontal cubemap coordinate [0, 1)
  887. * @param vf vertical cubemap coordinate [0, 1)
  888. * @param face face of cubemap
  889. * @param vec coordinates on sphere
  890. * @param scalew scale for uf
  891. * @param scaleh scale for vf
  892. */
  893. static void cube_to_xyz(const V360Context *s,
  894. float uf, float vf, int face,
  895. float *vec, float scalew, float scaleh)
  896. {
  897. const int direction = s->out_cubemap_direction_order[face];
  898. float l_x, l_y, l_z;
  899. uf /= scalew;
  900. vf /= scaleh;
  901. rotate_cube_face_inverse(&uf, &vf, s->out_cubemap_face_rotation[face]);
  902. switch (direction) {
  903. case RIGHT:
  904. l_x = 1.f;
  905. l_y = vf;
  906. l_z = -uf;
  907. break;
  908. case LEFT:
  909. l_x = -1.f;
  910. l_y = vf;
  911. l_z = uf;
  912. break;
  913. case UP:
  914. l_x = uf;
  915. l_y = -1.f;
  916. l_z = vf;
  917. break;
  918. case DOWN:
  919. l_x = uf;
  920. l_y = 1.f;
  921. l_z = -vf;
  922. break;
  923. case FRONT:
  924. l_x = uf;
  925. l_y = vf;
  926. l_z = 1.f;
  927. break;
  928. case BACK:
  929. l_x = -uf;
  930. l_y = vf;
  931. l_z = -1.f;
  932. break;
  933. default:
  934. av_assert0(0);
  935. }
  936. vec[0] = l_x;
  937. vec[1] = l_y;
  938. vec[2] = l_z;
  939. normalize_vector(vec);
  940. }
  941. /**
  942. * Calculate cubemap position for corresponding 3D coordinates on sphere.
  943. * Common operation for every cubemap.
  944. *
  945. * @param s filter private context
  946. * @param vec coordinated on sphere
  947. * @param uf horizontal cubemap coordinate [0, 1)
  948. * @param vf vertical cubemap coordinate [0, 1)
  949. * @param direction direction of view
  950. */
  951. static void xyz_to_cube(const V360Context *s,
  952. const float *vec,
  953. float *uf, float *vf, int *direction)
  954. {
  955. const float phi = atan2f(vec[0], vec[2]);
  956. const float theta = asinf(vec[1]);
  957. float phi_norm, theta_threshold;
  958. int face;
  959. if (phi >= -M_PI_4 && phi < M_PI_4) {
  960. *direction = FRONT;
  961. phi_norm = phi;
  962. } else if (phi >= -(M_PI_2 + M_PI_4) && phi < -M_PI_4) {
  963. *direction = LEFT;
  964. phi_norm = phi + M_PI_2;
  965. } else if (phi >= M_PI_4 && phi < M_PI_2 + M_PI_4) {
  966. *direction = RIGHT;
  967. phi_norm = phi - M_PI_2;
  968. } else {
  969. *direction = BACK;
  970. phi_norm = phi + ((phi > 0.f) ? -M_PI : M_PI);
  971. }
  972. theta_threshold = atanf(cosf(phi_norm));
  973. if (theta > theta_threshold) {
  974. *direction = DOWN;
  975. } else if (theta < -theta_threshold) {
  976. *direction = UP;
  977. }
  978. switch (*direction) {
  979. case RIGHT:
  980. *uf = -vec[2] / vec[0];
  981. *vf = vec[1] / vec[0];
  982. break;
  983. case LEFT:
  984. *uf = -vec[2] / vec[0];
  985. *vf = -vec[1] / vec[0];
  986. break;
  987. case UP:
  988. *uf = -vec[0] / vec[1];
  989. *vf = -vec[2] / vec[1];
  990. break;
  991. case DOWN:
  992. *uf = vec[0] / vec[1];
  993. *vf = -vec[2] / vec[1];
  994. break;
  995. case FRONT:
  996. *uf = vec[0] / vec[2];
  997. *vf = vec[1] / vec[2];
  998. break;
  999. case BACK:
  1000. *uf = vec[0] / vec[2];
  1001. *vf = -vec[1] / vec[2];
  1002. break;
  1003. default:
  1004. av_assert0(0);
  1005. }
  1006. face = s->in_cubemap_face_order[*direction];
  1007. rotate_cube_face(uf, vf, s->in_cubemap_face_rotation[face]);
  1008. }
  1009. /**
  1010. * Find position on another cube face in case of overflow/underflow.
  1011. * Used for calculation of interpolation window.
  1012. *
  1013. * @param s filter private context
  1014. * @param uf horizontal cubemap coordinate
  1015. * @param vf vertical cubemap coordinate
  1016. * @param direction direction of view
  1017. * @param new_uf new horizontal cubemap coordinate
  1018. * @param new_vf new vertical cubemap coordinate
  1019. * @param face face position on cubemap
  1020. */
  1021. static void process_cube_coordinates(const V360Context *s,
  1022. float uf, float vf, int direction,
  1023. float *new_uf, float *new_vf, int *face)
  1024. {
  1025. /*
  1026. * Cubemap orientation
  1027. *
  1028. * width
  1029. * <------->
  1030. * +-------+
  1031. * | | U
  1032. * | up | h ------->
  1033. * +-------+-------+-------+-------+ ^ e |
  1034. * | | | | | | i V |
  1035. * | left | front | right | back | | g |
  1036. * +-------+-------+-------+-------+ v h v
  1037. * | | t
  1038. * | down |
  1039. * +-------+
  1040. */
  1041. *face = s->in_cubemap_face_order[direction];
  1042. rotate_cube_face_inverse(&uf, &vf, s->in_cubemap_face_rotation[*face]);
  1043. if ((uf < -1.f || uf >= 1.f) && (vf < -1.f || vf >= 1.f)) {
  1044. // There are no pixels to use in this case
  1045. *new_uf = uf;
  1046. *new_vf = vf;
  1047. } else if (uf < -1.f) {
  1048. uf += 2.f;
  1049. switch (direction) {
  1050. case RIGHT:
  1051. direction = FRONT;
  1052. *new_uf = uf;
  1053. *new_vf = vf;
  1054. break;
  1055. case LEFT:
  1056. direction = BACK;
  1057. *new_uf = uf;
  1058. *new_vf = vf;
  1059. break;
  1060. case UP:
  1061. direction = LEFT;
  1062. *new_uf = vf;
  1063. *new_vf = -uf;
  1064. break;
  1065. case DOWN:
  1066. direction = LEFT;
  1067. *new_uf = -vf;
  1068. *new_vf = uf;
  1069. break;
  1070. case FRONT:
  1071. direction = LEFT;
  1072. *new_uf = uf;
  1073. *new_vf = vf;
  1074. break;
  1075. case BACK:
  1076. direction = RIGHT;
  1077. *new_uf = uf;
  1078. *new_vf = vf;
  1079. break;
  1080. default:
  1081. av_assert0(0);
  1082. }
  1083. } else if (uf >= 1.f) {
  1084. uf -= 2.f;
  1085. switch (direction) {
  1086. case RIGHT:
  1087. direction = BACK;
  1088. *new_uf = uf;
  1089. *new_vf = vf;
  1090. break;
  1091. case LEFT:
  1092. direction = FRONT;
  1093. *new_uf = uf;
  1094. *new_vf = vf;
  1095. break;
  1096. case UP:
  1097. direction = RIGHT;
  1098. *new_uf = -vf;
  1099. *new_vf = uf;
  1100. break;
  1101. case DOWN:
  1102. direction = RIGHT;
  1103. *new_uf = vf;
  1104. *new_vf = -uf;
  1105. break;
  1106. case FRONT:
  1107. direction = RIGHT;
  1108. *new_uf = uf;
  1109. *new_vf = vf;
  1110. break;
  1111. case BACK:
  1112. direction = LEFT;
  1113. *new_uf = uf;
  1114. *new_vf = vf;
  1115. break;
  1116. default:
  1117. av_assert0(0);
  1118. }
  1119. } else if (vf < -1.f) {
  1120. vf += 2.f;
  1121. switch (direction) {
  1122. case RIGHT:
  1123. direction = UP;
  1124. *new_uf = vf;
  1125. *new_vf = -uf;
  1126. break;
  1127. case LEFT:
  1128. direction = UP;
  1129. *new_uf = -vf;
  1130. *new_vf = uf;
  1131. break;
  1132. case UP:
  1133. direction = BACK;
  1134. *new_uf = -uf;
  1135. *new_vf = -vf;
  1136. break;
  1137. case DOWN:
  1138. direction = FRONT;
  1139. *new_uf = uf;
  1140. *new_vf = vf;
  1141. break;
  1142. case FRONT:
  1143. direction = UP;
  1144. *new_uf = uf;
  1145. *new_vf = vf;
  1146. break;
  1147. case BACK:
  1148. direction = UP;
  1149. *new_uf = -uf;
  1150. *new_vf = -vf;
  1151. break;
  1152. default:
  1153. av_assert0(0);
  1154. }
  1155. } else if (vf >= 1.f) {
  1156. vf -= 2.f;
  1157. switch (direction) {
  1158. case RIGHT:
  1159. direction = DOWN;
  1160. *new_uf = -vf;
  1161. *new_vf = uf;
  1162. break;
  1163. case LEFT:
  1164. direction = DOWN;
  1165. *new_uf = vf;
  1166. *new_vf = -uf;
  1167. break;
  1168. case UP:
  1169. direction = FRONT;
  1170. *new_uf = uf;
  1171. *new_vf = vf;
  1172. break;
  1173. case DOWN:
  1174. direction = BACK;
  1175. *new_uf = -uf;
  1176. *new_vf = -vf;
  1177. break;
  1178. case FRONT:
  1179. direction = DOWN;
  1180. *new_uf = uf;
  1181. *new_vf = vf;
  1182. break;
  1183. case BACK:
  1184. direction = DOWN;
  1185. *new_uf = -uf;
  1186. *new_vf = -vf;
  1187. break;
  1188. default:
  1189. av_assert0(0);
  1190. }
  1191. } else {
  1192. // Inside cube face
  1193. *new_uf = uf;
  1194. *new_vf = vf;
  1195. }
  1196. *face = s->in_cubemap_face_order[direction];
  1197. rotate_cube_face(new_uf, new_vf, s->in_cubemap_face_rotation[*face]);
  1198. }
  1199. /**
  1200. * Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format.
  1201. *
  1202. * @param s filter private context
  1203. * @param i horizontal position on frame [0, width)
  1204. * @param j vertical position on frame [0, height)
  1205. * @param width frame width
  1206. * @param height frame height
  1207. * @param vec coordinates on sphere
  1208. */
  1209. static int cube3x2_to_xyz(const V360Context *s,
  1210. int i, int j, int width, int height,
  1211. float *vec)
  1212. {
  1213. const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width / 3.f) : 1.f - s->out_pad;
  1214. const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 2.f) : 1.f - s->out_pad;
  1215. const float ew = width / 3.f;
  1216. const float eh = height / 2.f;
  1217. const int u_face = floorf(i / ew);
  1218. const int v_face = floorf(j / eh);
  1219. const int face = u_face + 3 * v_face;
  1220. const int u_shift = ceilf(ew * u_face);
  1221. const int v_shift = ceilf(eh * v_face);
  1222. const int ewi = ceilf(ew * (u_face + 1)) - u_shift;
  1223. const int ehi = ceilf(eh * (v_face + 1)) - v_shift;
  1224. const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
  1225. const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
  1226. cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
  1227. return 1;
  1228. }
  1229. /**
  1230. * Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere.
  1231. *
  1232. * @param s filter private context
  1233. * @param vec coordinates on sphere
  1234. * @param width frame width
  1235. * @param height frame height
  1236. * @param us horizontal coordinates for interpolation window
  1237. * @param vs vertical coordinates for interpolation window
  1238. * @param du horizontal relative coordinate
  1239. * @param dv vertical relative coordinate
  1240. */
  1241. static int xyz_to_cube3x2(const V360Context *s,
  1242. const float *vec, int width, int height,
  1243. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1244. {
  1245. const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width / 3.f) : 1.f - s->in_pad;
  1246. const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 2.f) : 1.f - s->in_pad;
  1247. const float ew = width / 3.f;
  1248. const float eh = height / 2.f;
  1249. float uf, vf;
  1250. int ui, vi;
  1251. int ewi, ehi;
  1252. int direction, face;
  1253. int u_face, v_face;
  1254. xyz_to_cube(s, vec, &uf, &vf, &direction);
  1255. uf *= scalew;
  1256. vf *= scaleh;
  1257. face = s->in_cubemap_face_order[direction];
  1258. u_face = face % 3;
  1259. v_face = face / 3;
  1260. ewi = ceilf(ew * (u_face + 1)) - ceilf(ew * u_face);
  1261. ehi = ceilf(eh * (v_face + 1)) - ceilf(eh * v_face);
  1262. uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
  1263. vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
  1264. ui = floorf(uf);
  1265. vi = floorf(vf);
  1266. *du = uf - ui;
  1267. *dv = vf - vi;
  1268. for (int i = 0; i < 4; i++) {
  1269. for (int j = 0; j < 4; j++) {
  1270. int new_ui = ui + j - 1;
  1271. int new_vi = vi + i - 1;
  1272. int u_shift, v_shift;
  1273. int new_ewi, new_ehi;
  1274. if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
  1275. face = s->in_cubemap_face_order[direction];
  1276. u_face = face % 3;
  1277. v_face = face / 3;
  1278. u_shift = ceilf(ew * u_face);
  1279. v_shift = ceilf(eh * v_face);
  1280. } else {
  1281. uf = 2.f * new_ui / ewi - 1.f;
  1282. vf = 2.f * new_vi / ehi - 1.f;
  1283. uf /= scalew;
  1284. vf /= scaleh;
  1285. process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
  1286. uf *= scalew;
  1287. vf *= scaleh;
  1288. u_face = face % 3;
  1289. v_face = face / 3;
  1290. u_shift = ceilf(ew * u_face);
  1291. v_shift = ceilf(eh * v_face);
  1292. new_ewi = ceilf(ew * (u_face + 1)) - u_shift;
  1293. new_ehi = ceilf(eh * (v_face + 1)) - v_shift;
  1294. new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
  1295. new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
  1296. }
  1297. us[i][j] = u_shift + new_ui;
  1298. vs[i][j] = v_shift + new_vi;
  1299. }
  1300. }
  1301. return 1;
  1302. }
  1303. /**
  1304. * Calculate 3D coordinates on sphere for corresponding frame position in cubemap1x6 format.
  1305. *
  1306. * @param s filter private context
  1307. * @param i horizontal position on frame [0, width)
  1308. * @param j vertical position on frame [0, height)
  1309. * @param width frame width
  1310. * @param height frame height
  1311. * @param vec coordinates on sphere
  1312. */
  1313. static int cube1x6_to_xyz(const V360Context *s,
  1314. int i, int j, int width, int height,
  1315. float *vec)
  1316. {
  1317. const float scalew = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / width : 1.f - s->out_pad;
  1318. const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 6.f) : 1.f - s->out_pad;
  1319. const float ew = width;
  1320. const float eh = height / 6.f;
  1321. const int face = floorf(j / eh);
  1322. const int v_shift = ceilf(eh * face);
  1323. const int ehi = ceilf(eh * (face + 1)) - v_shift;
  1324. const float uf = 2.f * (i + 0.5f) / ew - 1.f;
  1325. const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
  1326. cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
  1327. return 1;
  1328. }
  1329. /**
  1330. * Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format.
  1331. *
  1332. * @param s filter private context
  1333. * @param i horizontal position on frame [0, width)
  1334. * @param j vertical position on frame [0, height)
  1335. * @param width frame width
  1336. * @param height frame height
  1337. * @param vec coordinates on sphere
  1338. */
  1339. static int cube6x1_to_xyz(const V360Context *s,
  1340. int i, int j, int width, int height,
  1341. float *vec)
  1342. {
  1343. const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width / 6.f) : 1.f - s->out_pad;
  1344. const float scaleh = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / height : 1.f - s->out_pad;
  1345. const float ew = width / 6.f;
  1346. const float eh = height;
  1347. const int face = floorf(i / ew);
  1348. const int u_shift = ceilf(ew * face);
  1349. const int ewi = ceilf(ew * (face + 1)) - u_shift;
  1350. const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
  1351. const float vf = 2.f * (j + 0.5f) / eh - 1.f;
  1352. cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
  1353. return 1;
  1354. }
  1355. /**
  1356. * Calculate frame position in cubemap1x6 format for corresponding 3D coordinates on sphere.
  1357. *
  1358. * @param s filter private context
  1359. * @param vec coordinates on sphere
  1360. * @param width frame width
  1361. * @param height frame height
  1362. * @param us horizontal coordinates for interpolation window
  1363. * @param vs vertical coordinates for interpolation window
  1364. * @param du horizontal relative coordinate
  1365. * @param dv vertical relative coordinate
  1366. */
  1367. static int xyz_to_cube1x6(const V360Context *s,
  1368. const float *vec, int width, int height,
  1369. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1370. {
  1371. const float scalew = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / width : 1.f - s->in_pad;
  1372. const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 6.f) : 1.f - s->in_pad;
  1373. const float eh = height / 6.f;
  1374. const int ewi = width;
  1375. float uf, vf;
  1376. int ui, vi;
  1377. int ehi;
  1378. int direction, face;
  1379. xyz_to_cube(s, vec, &uf, &vf, &direction);
  1380. uf *= scalew;
  1381. vf *= scaleh;
  1382. face = s->in_cubemap_face_order[direction];
  1383. ehi = ceilf(eh * (face + 1)) - ceilf(eh * face);
  1384. uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
  1385. vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
  1386. ui = floorf(uf);
  1387. vi = floorf(vf);
  1388. *du = uf - ui;
  1389. *dv = vf - vi;
  1390. for (int i = 0; i < 4; i++) {
  1391. for (int j = 0; j < 4; j++) {
  1392. int new_ui = ui + j - 1;
  1393. int new_vi = vi + i - 1;
  1394. int v_shift;
  1395. int new_ehi;
  1396. if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
  1397. face = s->in_cubemap_face_order[direction];
  1398. v_shift = ceilf(eh * face);
  1399. } else {
  1400. uf = 2.f * new_ui / ewi - 1.f;
  1401. vf = 2.f * new_vi / ehi - 1.f;
  1402. uf /= scalew;
  1403. vf /= scaleh;
  1404. process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
  1405. uf *= scalew;
  1406. vf *= scaleh;
  1407. v_shift = ceilf(eh * face);
  1408. new_ehi = ceilf(eh * (face + 1)) - v_shift;
  1409. new_ui = av_clip(lrintf(0.5f * ewi * (uf + 1.f)), 0, ewi - 1);
  1410. new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
  1411. }
  1412. us[i][j] = new_ui;
  1413. vs[i][j] = v_shift + new_vi;
  1414. }
  1415. }
  1416. return 1;
  1417. }
  1418. /**
  1419. * Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere.
  1420. *
  1421. * @param s filter private context
  1422. * @param vec coordinates on sphere
  1423. * @param width frame width
  1424. * @param height frame height
  1425. * @param us horizontal coordinates for interpolation window
  1426. * @param vs vertical coordinates for interpolation window
  1427. * @param du horizontal relative coordinate
  1428. * @param dv vertical relative coordinate
  1429. */
  1430. static int xyz_to_cube6x1(const V360Context *s,
  1431. const float *vec, int width, int height,
  1432. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1433. {
  1434. const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width / 6.f) : 1.f - s->in_pad;
  1435. const float scaleh = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / height : 1.f - s->in_pad;
  1436. const float ew = width / 6.f;
  1437. const int ehi = height;
  1438. float uf, vf;
  1439. int ui, vi;
  1440. int ewi;
  1441. int direction, face;
  1442. xyz_to_cube(s, vec, &uf, &vf, &direction);
  1443. uf *= scalew;
  1444. vf *= scaleh;
  1445. face = s->in_cubemap_face_order[direction];
  1446. ewi = ceilf(ew * (face + 1)) - ceilf(ew * face);
  1447. uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
  1448. vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
  1449. ui = floorf(uf);
  1450. vi = floorf(vf);
  1451. *du = uf - ui;
  1452. *dv = vf - vi;
  1453. for (int i = 0; i < 4; i++) {
  1454. for (int j = 0; j < 4; j++) {
  1455. int new_ui = ui + j - 1;
  1456. int new_vi = vi + i - 1;
  1457. int u_shift;
  1458. int new_ewi;
  1459. if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
  1460. face = s->in_cubemap_face_order[direction];
  1461. u_shift = ceilf(ew * face);
  1462. } else {
  1463. uf = 2.f * new_ui / ewi - 1.f;
  1464. vf = 2.f * new_vi / ehi - 1.f;
  1465. uf /= scalew;
  1466. vf /= scaleh;
  1467. process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
  1468. uf *= scalew;
  1469. vf *= scaleh;
  1470. u_shift = ceilf(ew * face);
  1471. new_ewi = ceilf(ew * (face + 1)) - u_shift;
  1472. new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
  1473. new_vi = av_clip(lrintf(0.5f * ehi * (vf + 1.f)), 0, ehi - 1);
  1474. }
  1475. us[i][j] = u_shift + new_ui;
  1476. vs[i][j] = new_vi;
  1477. }
  1478. }
  1479. return 1;
  1480. }
  1481. /**
  1482. * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
  1483. *
  1484. * @param s filter private context
  1485. * @param i horizontal position on frame [0, width)
  1486. * @param j vertical position on frame [0, height)
  1487. * @param width frame width
  1488. * @param height frame height
  1489. * @param vec coordinates on sphere
  1490. */
  1491. static int equirect_to_xyz(const V360Context *s,
  1492. int i, int j, int width, int height,
  1493. float *vec)
  1494. {
  1495. const float phi = ((2.f * i + 0.5f) / width - 1.f) * M_PI;
  1496. const float theta = ((2.f * j + 0.5f) / height - 1.f) * M_PI_2;
  1497. const float sin_phi = sinf(phi);
  1498. const float cos_phi = cosf(phi);
  1499. const float sin_theta = sinf(theta);
  1500. const float cos_theta = cosf(theta);
  1501. vec[0] = cos_theta * sin_phi;
  1502. vec[1] = sin_theta;
  1503. vec[2] = cos_theta * cos_phi;
  1504. return 1;
  1505. }
  1506. /**
  1507. * Calculate 3D coordinates on sphere for corresponding frame position in half equirectangular format.
  1508. *
  1509. * @param s filter private context
  1510. * @param i horizontal position on frame [0, width)
  1511. * @param j vertical position on frame [0, height)
  1512. * @param width frame width
  1513. * @param height frame height
  1514. * @param vec coordinates on sphere
  1515. */
  1516. static int hequirect_to_xyz(const V360Context *s,
  1517. int i, int j, int width, int height,
  1518. float *vec)
  1519. {
  1520. const float phi = ((2.f * i + 0.5f) / width - 1.f) * M_PI_2;
  1521. const float theta = ((2.f * j + 0.5f) / height - 1.f) * M_PI_2;
  1522. const float sin_phi = sinf(phi);
  1523. const float cos_phi = cosf(phi);
  1524. const float sin_theta = sinf(theta);
  1525. const float cos_theta = cosf(theta);
  1526. vec[0] = cos_theta * sin_phi;
  1527. vec[1] = sin_theta;
  1528. vec[2] = cos_theta * cos_phi;
  1529. return 1;
  1530. }
  1531. /**
  1532. * Prepare data for processing stereographic output format.
  1533. *
  1534. * @param ctx filter context
  1535. *
  1536. * @return error code
  1537. */
  1538. static int prepare_stereographic_out(AVFilterContext *ctx)
  1539. {
  1540. V360Context *s = ctx->priv;
  1541. s->flat_range[0] = tanf(FFMIN(s->h_fov, 359.f) * M_PI / 720.f);
  1542. s->flat_range[1] = tanf(FFMIN(s->v_fov, 359.f) * M_PI / 720.f);
  1543. return 0;
  1544. }
  1545. /**
  1546. * Calculate 3D coordinates on sphere for corresponding frame position in stereographic format.
  1547. *
  1548. * @param s filter private context
  1549. * @param i horizontal position on frame [0, width)
  1550. * @param j vertical position on frame [0, height)
  1551. * @param width frame width
  1552. * @param height frame height
  1553. * @param vec coordinates on sphere
  1554. */
  1555. static int stereographic_to_xyz(const V360Context *s,
  1556. int i, int j, int width, int height,
  1557. float *vec)
  1558. {
  1559. const float x = ((2.f * i + 1.f) / width - 1.f) * s->flat_range[0];
  1560. const float y = ((2.f * j + 1.f) / height - 1.f) * s->flat_range[1];
  1561. const float r = hypotf(x, y);
  1562. const float theta = atanf(r) * 2.f;
  1563. const float sin_theta = sinf(theta);
  1564. vec[0] = x / r * sin_theta;
  1565. vec[1] = y / r * sin_theta;
  1566. vec[2] = cosf(theta);
  1567. normalize_vector(vec);
  1568. return 1;
  1569. }
  1570. /**
  1571. * Prepare data for processing stereographic input format.
  1572. *
  1573. * @param ctx filter context
  1574. *
  1575. * @return error code
  1576. */
  1577. static int prepare_stereographic_in(AVFilterContext *ctx)
  1578. {
  1579. V360Context *s = ctx->priv;
  1580. s->iflat_range[0] = tanf(FFMIN(s->ih_fov, 359.f) * M_PI / 720.f);
  1581. s->iflat_range[1] = tanf(FFMIN(s->iv_fov, 359.f) * M_PI / 720.f);
  1582. return 0;
  1583. }
  1584. /**
  1585. * Calculate frame position in stereographic format for corresponding 3D coordinates on sphere.
  1586. *
  1587. * @param s filter private context
  1588. * @param vec coordinates on sphere
  1589. * @param width frame width
  1590. * @param height frame height
  1591. * @param us horizontal coordinates for interpolation window
  1592. * @param vs vertical coordinates for interpolation window
  1593. * @param du horizontal relative coordinate
  1594. * @param dv vertical relative coordinate
  1595. */
  1596. static int xyz_to_stereographic(const V360Context *s,
  1597. const float *vec, int width, int height,
  1598. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1599. {
  1600. const float theta = acosf(vec[2]);
  1601. const float r = tanf(theta * 0.5f);
  1602. const float c = r / hypotf(vec[0], vec[1]);
  1603. const float x = vec[0] * c / s->iflat_range[0];
  1604. const float y = vec[1] * c / s->iflat_range[1];
  1605. const float uf = (x + 1.f) * width / 2.f;
  1606. const float vf = (y + 1.f) * height / 2.f;
  1607. const int ui = floorf(uf);
  1608. const int vi = floorf(vf);
  1609. const int visible = isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
  1610. *du = visible ? uf - ui : 0.f;
  1611. *dv = visible ? vf - vi : 0.f;
  1612. for (int i = 0; i < 4; i++) {
  1613. for (int j = 0; j < 4; j++) {
  1614. us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
  1615. vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
  1616. }
  1617. }
  1618. return visible;
  1619. }
  1620. /**
  1621. * Prepare data for processing equisolid output format.
  1622. *
  1623. * @param ctx filter context
  1624. *
  1625. * @return error code
  1626. */
  1627. static int prepare_equisolid_out(AVFilterContext *ctx)
  1628. {
  1629. V360Context *s = ctx->priv;
  1630. s->flat_range[0] = sinf(s->h_fov * M_PI / 720.f);
  1631. s->flat_range[1] = sinf(s->v_fov * M_PI / 720.f);
  1632. return 0;
  1633. }
  1634. /**
  1635. * Calculate 3D coordinates on sphere for corresponding frame position in equisolid format.
  1636. *
  1637. * @param s filter private context
  1638. * @param i horizontal position on frame [0, width)
  1639. * @param j vertical position on frame [0, height)
  1640. * @param width frame width
  1641. * @param height frame height
  1642. * @param vec coordinates on sphere
  1643. */
  1644. static int equisolid_to_xyz(const V360Context *s,
  1645. int i, int j, int width, int height,
  1646. float *vec)
  1647. {
  1648. const float x = ((2.f * i + 1.f) / width - 1.f) * s->flat_range[0];
  1649. const float y = ((2.f * j + 1.f) / height - 1.f) * s->flat_range[1];
  1650. const float r = hypotf(x, y);
  1651. const float theta = asinf(r) * 2.f;
  1652. const float sin_theta = sinf(theta);
  1653. vec[0] = x / r * sin_theta;
  1654. vec[1] = y / r * sin_theta;
  1655. vec[2] = cosf(theta);
  1656. normalize_vector(vec);
  1657. return 1;
  1658. }
  1659. /**
  1660. * Prepare data for processing equisolid input format.
  1661. *
  1662. * @param ctx filter context
  1663. *
  1664. * @return error code
  1665. */
  1666. static int prepare_equisolid_in(AVFilterContext *ctx)
  1667. {
  1668. V360Context *s = ctx->priv;
  1669. s->iflat_range[0] = sinf(FFMIN(s->ih_fov, 359.f) * M_PI / 720.f);
  1670. s->iflat_range[1] = sinf(FFMIN(s->iv_fov, 359.f) * M_PI / 720.f);
  1671. return 0;
  1672. }
  1673. /**
  1674. * Calculate frame position in equisolid format for corresponding 3D coordinates on sphere.
  1675. *
  1676. * @param s filter private context
  1677. * @param vec coordinates on sphere
  1678. * @param width frame width
  1679. * @param height frame height
  1680. * @param us horizontal coordinates for interpolation window
  1681. * @param vs vertical coordinates for interpolation window
  1682. * @param du horizontal relative coordinate
  1683. * @param dv vertical relative coordinate
  1684. */
  1685. static int xyz_to_equisolid(const V360Context *s,
  1686. const float *vec, int width, int height,
  1687. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1688. {
  1689. const float theta = acosf(vec[2]);
  1690. const float r = sinf(theta * 0.5f);
  1691. const float c = r / hypotf(vec[0], vec[1]);
  1692. const float x = vec[0] * c / s->iflat_range[0];
  1693. const float y = vec[1] * c / s->iflat_range[1];
  1694. const float uf = (x + 1.f) * width / 2.f;
  1695. const float vf = (y + 1.f) * height / 2.f;
  1696. const int ui = floorf(uf);
  1697. const int vi = floorf(vf);
  1698. const int visible = isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
  1699. *du = visible ? uf - ui : 0.f;
  1700. *dv = visible ? vf - vi : 0.f;
  1701. for (int i = 0; i < 4; i++) {
  1702. for (int j = 0; j < 4; j++) {
  1703. us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
  1704. vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
  1705. }
  1706. }
  1707. return visible;
  1708. }
  1709. /**
  1710. * Prepare data for processing orthographic output format.
  1711. *
  1712. * @param ctx filter context
  1713. *
  1714. * @return error code
  1715. */
  1716. static int prepare_orthographic_out(AVFilterContext *ctx)
  1717. {
  1718. V360Context *s = ctx->priv;
  1719. s->flat_range[0] = sinf(FFMIN(s->h_fov, 180.f) * M_PI / 360.f);
  1720. s->flat_range[1] = sinf(FFMIN(s->v_fov, 180.f) * M_PI / 360.f);
  1721. return 0;
  1722. }
  1723. /**
  1724. * Calculate 3D coordinates on sphere for corresponding frame position in orthographic format.
  1725. *
  1726. * @param s filter private context
  1727. * @param i horizontal position on frame [0, width)
  1728. * @param j vertical position on frame [0, height)
  1729. * @param width frame width
  1730. * @param height frame height
  1731. * @param vec coordinates on sphere
  1732. */
  1733. static int orthographic_to_xyz(const V360Context *s,
  1734. int i, int j, int width, int height,
  1735. float *vec)
  1736. {
  1737. const float x = ((2.f * i + 1.f) / width - 1.f) * s->flat_range[0];
  1738. const float y = ((2.f * j + 1.f) / height - 1.f) * s->flat_range[1];
  1739. const float r = hypotf(x, y);
  1740. const float theta = asinf(r);
  1741. vec[0] = x;
  1742. vec[1] = y;
  1743. vec[2] = cosf(theta);
  1744. normalize_vector(vec);
  1745. return 1;
  1746. }
  1747. /**
  1748. * Prepare data for processing orthographic input format.
  1749. *
  1750. * @param ctx filter context
  1751. *
  1752. * @return error code
  1753. */
  1754. static int prepare_orthographic_in(AVFilterContext *ctx)
  1755. {
  1756. V360Context *s = ctx->priv;
  1757. s->iflat_range[0] = sinf(FFMIN(s->ih_fov, 180.f) * M_PI / 360.f);
  1758. s->iflat_range[1] = sinf(FFMIN(s->iv_fov, 180.f) * M_PI / 360.f);
  1759. return 0;
  1760. }
  1761. /**
  1762. * Calculate frame position in orthographic format for corresponding 3D coordinates on sphere.
  1763. *
  1764. * @param s filter private context
  1765. * @param vec coordinates on sphere
  1766. * @param width frame width
  1767. * @param height frame height
  1768. * @param us horizontal coordinates for interpolation window
  1769. * @param vs vertical coordinates for interpolation window
  1770. * @param du horizontal relative coordinate
  1771. * @param dv vertical relative coordinate
  1772. */
  1773. static int xyz_to_orthographic(const V360Context *s,
  1774. const float *vec, int width, int height,
  1775. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1776. {
  1777. const float theta = acosf(vec[2]);
  1778. const float r = sinf(theta);
  1779. const float c = r / hypotf(vec[0], vec[1]);
  1780. const float x = vec[0] * c / s->iflat_range[0];
  1781. const float y = vec[1] * c / s->iflat_range[1];
  1782. const float uf = (x + 1.f) * width / 2.f;
  1783. const float vf = (y + 1.f) * height / 2.f;
  1784. const int ui = floorf(uf);
  1785. const int vi = floorf(vf);
  1786. const int visible = vec[2] >= 0.f && isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
  1787. *du = visible ? uf - ui : 0.f;
  1788. *dv = visible ? vf - vi : 0.f;
  1789. for (int i = 0; i < 4; i++) {
  1790. for (int j = 0; j < 4; j++) {
  1791. us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
  1792. vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
  1793. }
  1794. }
  1795. return visible;
  1796. }
  1797. /**
  1798. * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
  1799. *
  1800. * @param s filter private context
  1801. * @param vec coordinates on sphere
  1802. * @param width frame width
  1803. * @param height frame height
  1804. * @param us horizontal coordinates for interpolation window
  1805. * @param vs vertical coordinates for interpolation window
  1806. * @param du horizontal relative coordinate
  1807. * @param dv vertical relative coordinate
  1808. */
  1809. static int xyz_to_equirect(const V360Context *s,
  1810. const float *vec, int width, int height,
  1811. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1812. {
  1813. const float phi = atan2f(vec[0], vec[2]);
  1814. const float theta = asinf(vec[1]);
  1815. const float uf = (phi / M_PI + 1.f) * width / 2.f;
  1816. const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
  1817. const int ui = floorf(uf);
  1818. const int vi = floorf(vf);
  1819. *du = uf - ui;
  1820. *dv = vf - vi;
  1821. for (int i = 0; i < 4; i++) {
  1822. for (int j = 0; j < 4; j++) {
  1823. us[i][j] = ereflectx(ui + j - 1, vi + i - 1, width, height);
  1824. vs[i][j] = reflecty(vi + i - 1, height);
  1825. }
  1826. }
  1827. return 1;
  1828. }
  1829. /**
  1830. * Calculate frame position in half equirectangular format for corresponding 3D coordinates on sphere.
  1831. *
  1832. * @param s filter private context
  1833. * @param vec coordinates on sphere
  1834. * @param width frame width
  1835. * @param height frame height
  1836. * @param us horizontal coordinates for interpolation window
  1837. * @param vs vertical coordinates for interpolation window
  1838. * @param du horizontal relative coordinate
  1839. * @param dv vertical relative coordinate
  1840. */
  1841. static int xyz_to_hequirect(const V360Context *s,
  1842. const float *vec, int width, int height,
  1843. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1844. {
  1845. const float phi = atan2f(vec[0], vec[2]);
  1846. const float theta = asinf(vec[1]);
  1847. const float uf = (phi / M_PI_2 + 1.f) * width / 2.f;
  1848. const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
  1849. const int ui = floorf(uf);
  1850. const int vi = floorf(vf);
  1851. const int visible = phi >= -M_PI_2 && phi <= M_PI_2;
  1852. *du = uf - ui;
  1853. *dv = vf - vi;
  1854. for (int i = 0; i < 4; i++) {
  1855. for (int j = 0; j < 4; j++) {
  1856. us[i][j] = av_clip(ui + j - 1, 0, width - 1);
  1857. vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
  1858. }
  1859. }
  1860. return visible;
  1861. }
  1862. /**
  1863. * Prepare data for processing flat input format.
  1864. *
  1865. * @param ctx filter context
  1866. *
  1867. * @return error code
  1868. */
  1869. static int prepare_flat_in(AVFilterContext *ctx)
  1870. {
  1871. V360Context *s = ctx->priv;
  1872. s->iflat_range[0] = tanf(0.5f * s->ih_fov * M_PI / 180.f);
  1873. s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
  1874. return 0;
  1875. }
  1876. /**
  1877. * Calculate frame position in flat format for corresponding 3D coordinates on sphere.
  1878. *
  1879. * @param s filter private context
  1880. * @param vec coordinates on sphere
  1881. * @param width frame width
  1882. * @param height frame height
  1883. * @param us horizontal coordinates for interpolation window
  1884. * @param vs vertical coordinates for interpolation window
  1885. * @param du horizontal relative coordinate
  1886. * @param dv vertical relative coordinate
  1887. */
  1888. static int xyz_to_flat(const V360Context *s,
  1889. const float *vec, int width, int height,
  1890. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1891. {
  1892. const float theta = acosf(vec[2]);
  1893. const float r = tanf(theta);
  1894. const float rr = fabsf(r) < 1e+6f ? r : hypotf(width, height);
  1895. const float zf = vec[2];
  1896. const float h = hypotf(vec[0], vec[1]);
  1897. const float c = h <= 1e-6f ? 1.f : rr / h;
  1898. float uf = vec[0] * c / s->iflat_range[0];
  1899. float vf = vec[1] * c / s->iflat_range[1];
  1900. int visible, ui, vi;
  1901. uf = zf >= 0.f ? (uf + 1.f) * width / 2.f : 0.f;
  1902. vf = zf >= 0.f ? (vf + 1.f) * height / 2.f : 0.f;
  1903. ui = floorf(uf);
  1904. vi = floorf(vf);
  1905. visible = vi >= 0 && vi < height && ui >= 0 && ui < width && zf >= 0.f;
  1906. *du = uf - ui;
  1907. *dv = vf - vi;
  1908. for (int i = 0; i < 4; i++) {
  1909. for (int j = 0; j < 4; j++) {
  1910. us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
  1911. vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
  1912. }
  1913. }
  1914. return visible;
  1915. }
  1916. /**
  1917. * Calculate frame position in mercator format for corresponding 3D coordinates on sphere.
  1918. *
  1919. * @param s filter private context
  1920. * @param vec coordinates on sphere
  1921. * @param width frame width
  1922. * @param height frame height
  1923. * @param us horizontal coordinates for interpolation window
  1924. * @param vs vertical coordinates for interpolation window
  1925. * @param du horizontal relative coordinate
  1926. * @param dv vertical relative coordinate
  1927. */
  1928. static int xyz_to_mercator(const V360Context *s,
  1929. const float *vec, int width, int height,
  1930. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1931. {
  1932. const float phi = atan2f(vec[0], vec[2]);
  1933. const float theta = vec[1];
  1934. const float uf = (phi / M_PI + 1.f) * width / 2.f;
  1935. const float vf = (av_clipf(logf((1.f + theta) / (1.f - theta)) / (2.f * M_PI), -1.f, 1.f) + 1.f) * height / 2.f;
  1936. const int ui = floorf(uf);
  1937. const int vi = floorf(vf);
  1938. *du = uf - ui;
  1939. *dv = vf - vi;
  1940. for (int i = 0; i < 4; i++) {
  1941. for (int j = 0; j < 4; j++) {
  1942. us[i][j] = av_clip(ui + j - 1, 0, width - 1);
  1943. vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
  1944. }
  1945. }
  1946. return 1;
  1947. }
  1948. /**
  1949. * Calculate 3D coordinates on sphere for corresponding frame position in mercator format.
  1950. *
  1951. * @param s filter private context
  1952. * @param i horizontal position on frame [0, width)
  1953. * @param j vertical position on frame [0, height)
  1954. * @param width frame width
  1955. * @param height frame height
  1956. * @param vec coordinates on sphere
  1957. */
  1958. static int mercator_to_xyz(const V360Context *s,
  1959. int i, int j, int width, int height,
  1960. float *vec)
  1961. {
  1962. const float phi = ((2.f * i + 1.f) / width - 1.f) * M_PI + M_PI_2;
  1963. const float y = ((2.f * j + 1.f) / height - 1.f) * M_PI;
  1964. const float div = expf(2.f * y) + 1.f;
  1965. const float sin_phi = sinf(phi);
  1966. const float cos_phi = cosf(phi);
  1967. const float sin_theta = 2.f * expf(y) / div;
  1968. const float cos_theta = (expf(2.f * y) - 1.f) / div;
  1969. vec[0] = -sin_theta * cos_phi;
  1970. vec[1] = cos_theta;
  1971. vec[2] = sin_theta * sin_phi;
  1972. return 1;
  1973. }
  1974. /**
  1975. * Calculate frame position in ball format for corresponding 3D coordinates on sphere.
  1976. *
  1977. * @param s filter private context
  1978. * @param vec coordinates on sphere
  1979. * @param width frame width
  1980. * @param height frame height
  1981. * @param us horizontal coordinates for interpolation window
  1982. * @param vs vertical coordinates for interpolation window
  1983. * @param du horizontal relative coordinate
  1984. * @param dv vertical relative coordinate
  1985. */
  1986. static int xyz_to_ball(const V360Context *s,
  1987. const float *vec, int width, int height,
  1988. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1989. {
  1990. const float l = hypotf(vec[0], vec[1]);
  1991. const float r = sqrtf(1.f - vec[2]) / M_SQRT2;
  1992. const float uf = (1.f + r * vec[0] / (l > 0.f ? l : 1.f)) * width * 0.5f;
  1993. const float vf = (1.f + r * vec[1] / (l > 0.f ? l : 1.f)) * height * 0.5f;
  1994. const int ui = floorf(uf);
  1995. const int vi = floorf(vf);
  1996. *du = uf - ui;
  1997. *dv = vf - vi;
  1998. for (int i = 0; i < 4; i++) {
  1999. for (int j = 0; j < 4; j++) {
  2000. us[i][j] = av_clip(ui + j - 1, 0, width - 1);
  2001. vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
  2002. }
  2003. }
  2004. return 1;
  2005. }
  2006. /**
  2007. * Calculate 3D coordinates on sphere for corresponding frame position in ball format.
  2008. *
  2009. * @param s filter private context
  2010. * @param i horizontal position on frame [0, width)
  2011. * @param j vertical position on frame [0, height)
  2012. * @param width frame width
  2013. * @param height frame height
  2014. * @param vec coordinates on sphere
  2015. */
  2016. static int ball_to_xyz(const V360Context *s,
  2017. int i, int j, int width, int height,
  2018. float *vec)
  2019. {
  2020. const float x = (2.f * i + 1.f) / width - 1.f;
  2021. const float y = (2.f * j + 1.f) / height - 1.f;
  2022. const float l = hypotf(x, y);
  2023. if (l <= 1.f) {
  2024. const float z = 2.f * l * sqrtf(1.f - l * l);
  2025. vec[0] = z * x / (l > 0.f ? l : 1.f);
  2026. vec[1] = z * y / (l > 0.f ? l : 1.f);
  2027. vec[2] = 1.f - 2.f * l * l;
  2028. } else {
  2029. vec[0] = 0.f;
  2030. vec[1] = 1.f;
  2031. vec[2] = 0.f;
  2032. return 0;
  2033. }
  2034. return 1;
  2035. }
  2036. /**
  2037. * Calculate 3D coordinates on sphere for corresponding frame position in hammer format.
  2038. *
  2039. * @param s filter private context
  2040. * @param i horizontal position on frame [0, width)
  2041. * @param j vertical position on frame [0, height)
  2042. * @param width frame width
  2043. * @param height frame height
  2044. * @param vec coordinates on sphere
  2045. */
  2046. static int hammer_to_xyz(const V360Context *s,
  2047. int i, int j, int width, int height,
  2048. float *vec)
  2049. {
  2050. const float x = ((2.f * i + 1.f) / width - 1.f);
  2051. const float y = ((2.f * j + 1.f) / height - 1.f);
  2052. const float xx = x * x;
  2053. const float yy = y * y;
  2054. const float z = sqrtf(1.f - xx * 0.5f - yy * 0.5f);
  2055. const float a = M_SQRT2 * x * z;
  2056. const float b = 2.f * z * z - 1.f;
  2057. const float aa = a * a;
  2058. const float bb = b * b;
  2059. const float w = sqrtf(1.f - 2.f * yy * z * z);
  2060. vec[0] = w * 2.f * a * b / (aa + bb);
  2061. vec[1] = M_SQRT2 * y * z;
  2062. vec[2] = w * (bb - aa) / (aa + bb);
  2063. normalize_vector(vec);
  2064. return 1;
  2065. }
  2066. /**
  2067. * Calculate frame position in hammer format for corresponding 3D coordinates on sphere.
  2068. *
  2069. * @param s filter private context
  2070. * @param vec coordinates on sphere
  2071. * @param width frame width
  2072. * @param height frame height
  2073. * @param us horizontal coordinates for interpolation window
  2074. * @param vs vertical coordinates for interpolation window
  2075. * @param du horizontal relative coordinate
  2076. * @param dv vertical relative coordinate
  2077. */
  2078. static int xyz_to_hammer(const V360Context *s,
  2079. const float *vec, int width, int height,
  2080. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2081. {
  2082. const float theta = atan2f(vec[0], vec[2]);
  2083. const float z = sqrtf(1.f + sqrtf(1.f - vec[1] * vec[1]) * cosf(theta * 0.5f));
  2084. const float x = sqrtf(1.f - vec[1] * vec[1]) * sinf(theta * 0.5f) / z;
  2085. const float y = vec[1] / z;
  2086. const float uf = (x + 1.f) * width / 2.f;
  2087. const float vf = (y + 1.f) * height / 2.f;
  2088. const int ui = floorf(uf);
  2089. const int vi = floorf(vf);
  2090. *du = uf - ui;
  2091. *dv = vf - vi;
  2092. for (int i = 0; i < 4; i++) {
  2093. for (int j = 0; j < 4; j++) {
  2094. us[i][j] = av_clip(ui + j - 1, 0, width - 1);
  2095. vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
  2096. }
  2097. }
  2098. return 1;
  2099. }
  2100. /**
  2101. * Calculate 3D coordinates on sphere for corresponding frame position in sinusoidal format.
  2102. *
  2103. * @param s filter private context
  2104. * @param i horizontal position on frame [0, width)
  2105. * @param j vertical position on frame [0, height)
  2106. * @param width frame width
  2107. * @param height frame height
  2108. * @param vec coordinates on sphere
  2109. */
  2110. static int sinusoidal_to_xyz(const V360Context *s,
  2111. int i, int j, int width, int height,
  2112. float *vec)
  2113. {
  2114. const float theta = ((2.f * j + 1.f) / height - 1.f) * M_PI_2;
  2115. const float phi = ((2.f * i + 1.f) / width - 1.f) * M_PI / cosf(theta);
  2116. const float sin_phi = sinf(phi);
  2117. const float cos_phi = cosf(phi);
  2118. const float sin_theta = sinf(theta);
  2119. const float cos_theta = cosf(theta);
  2120. vec[0] = cos_theta * sin_phi;
  2121. vec[1] = sin_theta;
  2122. vec[2] = cos_theta * cos_phi;
  2123. normalize_vector(vec);
  2124. return 1;
  2125. }
  2126. /**
  2127. * Calculate frame position in sinusoidal format for corresponding 3D coordinates on sphere.
  2128. *
  2129. * @param s filter private context
  2130. * @param vec coordinates on sphere
  2131. * @param width frame width
  2132. * @param height frame height
  2133. * @param us horizontal coordinates for interpolation window
  2134. * @param vs vertical coordinates for interpolation window
  2135. * @param du horizontal relative coordinate
  2136. * @param dv vertical relative coordinate
  2137. */
  2138. static int xyz_to_sinusoidal(const V360Context *s,
  2139. const float *vec, int width, int height,
  2140. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2141. {
  2142. const float theta = asinf(vec[1]);
  2143. const float phi = atan2f(vec[0], vec[2]) * cosf(theta);
  2144. const float uf = (phi / M_PI + 1.f) * width / 2.f;
  2145. const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
  2146. const int ui = floorf(uf);
  2147. const int vi = floorf(vf);
  2148. *du = uf - ui;
  2149. *dv = vf - vi;
  2150. for (int i = 0; i < 4; i++) {
  2151. for (int j = 0; j < 4; j++) {
  2152. us[i][j] = av_clip(ui + j - 1, 0, width - 1);
  2153. vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
  2154. }
  2155. }
  2156. return 1;
  2157. }
  2158. /**
  2159. * Prepare data for processing equi-angular cubemap input format.
  2160. *
  2161. * @param ctx filter context
  2162. *
  2163. * @return error code
  2164. */
  2165. static int prepare_eac_in(AVFilterContext *ctx)
  2166. {
  2167. V360Context *s = ctx->priv;
  2168. s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
  2169. s->in_cubemap_face_order[LEFT] = TOP_LEFT;
  2170. s->in_cubemap_face_order[UP] = BOTTOM_RIGHT;
  2171. s->in_cubemap_face_order[DOWN] = BOTTOM_LEFT;
  2172. s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
  2173. s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
  2174. s->in_cubemap_face_rotation[TOP_LEFT] = ROT_0;
  2175. s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
  2176. s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
  2177. s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
  2178. s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
  2179. s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
  2180. return 0;
  2181. }
  2182. /**
  2183. * Prepare data for processing equi-angular cubemap output format.
  2184. *
  2185. * @param ctx filter context
  2186. *
  2187. * @return error code
  2188. */
  2189. static int prepare_eac_out(AVFilterContext *ctx)
  2190. {
  2191. V360Context *s = ctx->priv;
  2192. s->out_cubemap_direction_order[TOP_LEFT] = LEFT;
  2193. s->out_cubemap_direction_order[TOP_MIDDLE] = FRONT;
  2194. s->out_cubemap_direction_order[TOP_RIGHT] = RIGHT;
  2195. s->out_cubemap_direction_order[BOTTOM_LEFT] = DOWN;
  2196. s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
  2197. s->out_cubemap_direction_order[BOTTOM_RIGHT] = UP;
  2198. s->out_cubemap_face_rotation[TOP_LEFT] = ROT_0;
  2199. s->out_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
  2200. s->out_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
  2201. s->out_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
  2202. s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
  2203. s->out_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
  2204. return 0;
  2205. }
  2206. /**
  2207. * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
  2208. *
  2209. * @param s filter private context
  2210. * @param i horizontal position on frame [0, width)
  2211. * @param j vertical position on frame [0, height)
  2212. * @param width frame width
  2213. * @param height frame height
  2214. * @param vec coordinates on sphere
  2215. */
  2216. static int eac_to_xyz(const V360Context *s,
  2217. int i, int j, int width, int height,
  2218. float *vec)
  2219. {
  2220. const float pixel_pad = 2;
  2221. const float u_pad = pixel_pad / width;
  2222. const float v_pad = pixel_pad / height;
  2223. int u_face, v_face, face;
  2224. float l_x, l_y, l_z;
  2225. float uf = (i + 0.5f) / width;
  2226. float vf = (j + 0.5f) / height;
  2227. // EAC has 2-pixel padding on faces except between faces on the same row
  2228. // Padding pixels seems not to be stretched with tangent as regular pixels
  2229. // Formulas below approximate original padding as close as I could get experimentally
  2230. // Horizontal padding
  2231. uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
  2232. if (uf < 0.f) {
  2233. u_face = 0;
  2234. uf -= 0.5f;
  2235. } else if (uf >= 3.f) {
  2236. u_face = 2;
  2237. uf -= 2.5f;
  2238. } else {
  2239. u_face = floorf(uf);
  2240. uf = fmodf(uf, 1.f) - 0.5f;
  2241. }
  2242. // Vertical padding
  2243. v_face = floorf(vf * 2.f);
  2244. vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
  2245. if (uf >= -0.5f && uf < 0.5f) {
  2246. uf = tanf(M_PI_2 * uf);
  2247. } else {
  2248. uf = 2.f * uf;
  2249. }
  2250. if (vf >= -0.5f && vf < 0.5f) {
  2251. vf = tanf(M_PI_2 * vf);
  2252. } else {
  2253. vf = 2.f * vf;
  2254. }
  2255. face = u_face + 3 * v_face;
  2256. switch (face) {
  2257. case TOP_LEFT:
  2258. l_x = -1.f;
  2259. l_y = vf;
  2260. l_z = uf;
  2261. break;
  2262. case TOP_MIDDLE:
  2263. l_x = uf;
  2264. l_y = vf;
  2265. l_z = 1.f;
  2266. break;
  2267. case TOP_RIGHT:
  2268. l_x = 1.f;
  2269. l_y = vf;
  2270. l_z = -uf;
  2271. break;
  2272. case BOTTOM_LEFT:
  2273. l_x = -vf;
  2274. l_y = 1.f;
  2275. l_z = -uf;
  2276. break;
  2277. case BOTTOM_MIDDLE:
  2278. l_x = -vf;
  2279. l_y = -uf;
  2280. l_z = -1.f;
  2281. break;
  2282. case BOTTOM_RIGHT:
  2283. l_x = -vf;
  2284. l_y = -1.f;
  2285. l_z = uf;
  2286. break;
  2287. default:
  2288. av_assert0(0);
  2289. }
  2290. vec[0] = l_x;
  2291. vec[1] = l_y;
  2292. vec[2] = l_z;
  2293. normalize_vector(vec);
  2294. return 1;
  2295. }
  2296. /**
  2297. * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
  2298. *
  2299. * @param s filter private context
  2300. * @param vec coordinates on sphere
  2301. * @param width frame width
  2302. * @param height frame height
  2303. * @param us horizontal coordinates for interpolation window
  2304. * @param vs vertical coordinates for interpolation window
  2305. * @param du horizontal relative coordinate
  2306. * @param dv vertical relative coordinate
  2307. */
  2308. static int xyz_to_eac(const V360Context *s,
  2309. const float *vec, int width, int height,
  2310. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2311. {
  2312. const float pixel_pad = 2;
  2313. const float u_pad = pixel_pad / width;
  2314. const float v_pad = pixel_pad / height;
  2315. float uf, vf;
  2316. int ui, vi;
  2317. int direction, face;
  2318. int u_face, v_face;
  2319. xyz_to_cube(s, vec, &uf, &vf, &direction);
  2320. face = s->in_cubemap_face_order[direction];
  2321. u_face = face % 3;
  2322. v_face = face / 3;
  2323. uf = M_2_PI * atanf(uf) + 0.5f;
  2324. vf = M_2_PI * atanf(vf) + 0.5f;
  2325. // These formulas are inversed from eac_to_xyz ones
  2326. uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
  2327. vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
  2328. uf *= width;
  2329. vf *= height;
  2330. uf -= 0.5f;
  2331. vf -= 0.5f;
  2332. ui = floorf(uf);
  2333. vi = floorf(vf);
  2334. *du = uf - ui;
  2335. *dv = vf - vi;
  2336. for (int i = 0; i < 4; i++) {
  2337. for (int j = 0; j < 4; j++) {
  2338. us[i][j] = av_clip(ui + j - 1, 0, width - 1);
  2339. vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
  2340. }
  2341. }
  2342. return 1;
  2343. }
  2344. /**
  2345. * Prepare data for processing flat output format.
  2346. *
  2347. * @param ctx filter context
  2348. *
  2349. * @return error code
  2350. */
  2351. static int prepare_flat_out(AVFilterContext *ctx)
  2352. {
  2353. V360Context *s = ctx->priv;
  2354. s->flat_range[0] = tanf(0.5f * s->h_fov * M_PI / 180.f);
  2355. s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
  2356. return 0;
  2357. }
  2358. /**
  2359. * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
  2360. *
  2361. * @param s filter private context
  2362. * @param i horizontal position on frame [0, width)
  2363. * @param j vertical position on frame [0, height)
  2364. * @param width frame width
  2365. * @param height frame height
  2366. * @param vec coordinates on sphere
  2367. */
  2368. static int flat_to_xyz(const V360Context *s,
  2369. int i, int j, int width, int height,
  2370. float *vec)
  2371. {
  2372. const float l_x = s->flat_range[0] * ((2.f * i + 0.5f) / width - 1.f);
  2373. const float l_y = s->flat_range[1] * ((2.f * j + 0.5f) / height - 1.f);
  2374. vec[0] = l_x;
  2375. vec[1] = l_y;
  2376. vec[2] = 1.f;
  2377. normalize_vector(vec);
  2378. return 1;
  2379. }
  2380. /**
  2381. * Prepare data for processing fisheye output format.
  2382. *
  2383. * @param ctx filter context
  2384. *
  2385. * @return error code
  2386. */
  2387. static int prepare_fisheye_out(AVFilterContext *ctx)
  2388. {
  2389. V360Context *s = ctx->priv;
  2390. s->flat_range[0] = s->h_fov / 180.f;
  2391. s->flat_range[1] = s->v_fov / 180.f;
  2392. return 0;
  2393. }
  2394. /**
  2395. * Calculate 3D coordinates on sphere for corresponding frame position in fisheye format.
  2396. *
  2397. * @param s filter private context
  2398. * @param i horizontal position on frame [0, width)
  2399. * @param j vertical position on frame [0, height)
  2400. * @param width frame width
  2401. * @param height frame height
  2402. * @param vec coordinates on sphere
  2403. */
  2404. static int fisheye_to_xyz(const V360Context *s,
  2405. int i, int j, int width, int height,
  2406. float *vec)
  2407. {
  2408. const float uf = s->flat_range[0] * ((2.f * i) / width - 1.f);
  2409. const float vf = s->flat_range[1] * ((2.f * j + 1.f) / height - 1.f);
  2410. const float phi = atan2f(vf, uf);
  2411. const float theta = M_PI_2 * (1.f - hypotf(uf, vf));
  2412. const float sin_phi = sinf(phi);
  2413. const float cos_phi = cosf(phi);
  2414. const float sin_theta = sinf(theta);
  2415. const float cos_theta = cosf(theta);
  2416. vec[0] = cos_theta * cos_phi;
  2417. vec[1] = cos_theta * sin_phi;
  2418. vec[2] = sin_theta;
  2419. normalize_vector(vec);
  2420. return 1;
  2421. }
  2422. /**
  2423. * Prepare data for processing fisheye input format.
  2424. *
  2425. * @param ctx filter context
  2426. *
  2427. * @return error code
  2428. */
  2429. static int prepare_fisheye_in(AVFilterContext *ctx)
  2430. {
  2431. V360Context *s = ctx->priv;
  2432. s->iflat_range[0] = s->ih_fov / 180.f;
  2433. s->iflat_range[1] = s->iv_fov / 180.f;
  2434. return 0;
  2435. }
  2436. /**
  2437. * Calculate frame position in fisheye format for corresponding 3D coordinates on sphere.
  2438. *
  2439. * @param s filter private context
  2440. * @param vec coordinates on sphere
  2441. * @param width frame width
  2442. * @param height frame height
  2443. * @param us horizontal coordinates for interpolation window
  2444. * @param vs vertical coordinates for interpolation window
  2445. * @param du horizontal relative coordinate
  2446. * @param dv vertical relative coordinate
  2447. */
  2448. static int xyz_to_fisheye(const V360Context *s,
  2449. const float *vec, int width, int height,
  2450. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2451. {
  2452. const float h = hypotf(vec[0], vec[1]);
  2453. const float lh = h > 0.f ? h : 1.f;
  2454. const float phi = atan2f(h, vec[2]) / M_PI;
  2455. float uf = vec[0] / lh * phi / s->iflat_range[0];
  2456. float vf = vec[1] / lh * phi / s->iflat_range[1];
  2457. const int visible = hypotf(uf, vf) <= 0.5f;
  2458. int ui, vi;
  2459. uf = (uf + 0.5f) * width;
  2460. vf = (vf + 0.5f) * height;
  2461. ui = floorf(uf);
  2462. vi = floorf(vf);
  2463. *du = visible ? uf - ui : 0.f;
  2464. *dv = visible ? vf - vi : 0.f;
  2465. for (int i = 0; i < 4; i++) {
  2466. for (int j = 0; j < 4; j++) {
  2467. us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
  2468. vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
  2469. }
  2470. }
  2471. return visible;
  2472. }
  2473. /**
  2474. * Calculate 3D coordinates on sphere for corresponding frame position in pannini format.
  2475. *
  2476. * @param s filter private context
  2477. * @param i horizontal position on frame [0, width)
  2478. * @param j vertical position on frame [0, height)
  2479. * @param width frame width
  2480. * @param height frame height
  2481. * @param vec coordinates on sphere
  2482. */
  2483. static int pannini_to_xyz(const V360Context *s,
  2484. int i, int j, int width, int height,
  2485. float *vec)
  2486. {
  2487. const float uf = ((2.f * i + 1.f) / width - 1.f);
  2488. const float vf = ((2.f * j + 1.f) / height - 1.f);
  2489. const float d = s->h_fov;
  2490. const float k = uf * uf / ((d + 1.f) * (d + 1.f));
  2491. const float dscr = k * k * d * d - (k + 1.f) * (k * d * d - 1.f);
  2492. const float clon = (-k * d + sqrtf(dscr)) / (k + 1.f);
  2493. const float S = (d + 1.f) / (d + clon);
  2494. const float lon = atan2f(uf, S * clon);
  2495. const float lat = atan2f(vf, S);
  2496. vec[0] = sinf(lon) * cosf(lat);
  2497. vec[1] = sinf(lat);
  2498. vec[2] = cosf(lon) * cosf(lat);
  2499. normalize_vector(vec);
  2500. return 1;
  2501. }
  2502. /**
  2503. * Calculate frame position in pannini format for corresponding 3D coordinates on sphere.
  2504. *
  2505. * @param s filter private context
  2506. * @param vec coordinates on sphere
  2507. * @param width frame width
  2508. * @param height frame height
  2509. * @param us horizontal coordinates for interpolation window
  2510. * @param vs vertical coordinates for interpolation window
  2511. * @param du horizontal relative coordinate
  2512. * @param dv vertical relative coordinate
  2513. */
  2514. static int xyz_to_pannini(const V360Context *s,
  2515. const float *vec, int width, int height,
  2516. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2517. {
  2518. const float phi = atan2f(vec[0], vec[2]);
  2519. const float theta = asinf(vec[1]);
  2520. const float d = s->ih_fov;
  2521. const float S = (d + 1.f) / (d + cosf(phi));
  2522. const float x = S * sinf(phi);
  2523. const float y = S * tanf(theta);
  2524. const float uf = (x + 1.f) * width / 2.f;
  2525. const float vf = (y + 1.f) * height / 2.f;
  2526. const int ui = floorf(uf);
  2527. const int vi = floorf(vf);
  2528. const int visible = vi >= 0 && vi < height && ui >= 0 && ui < width && vec[2] >= 0.f;
  2529. *du = uf - ui;
  2530. *dv = vf - vi;
  2531. for (int i = 0; i < 4; i++) {
  2532. for (int j = 0; j < 4; j++) {
  2533. us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
  2534. vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
  2535. }
  2536. }
  2537. return visible;
  2538. }
  2539. /**
  2540. * Prepare data for processing cylindrical output format.
  2541. *
  2542. * @param ctx filter context
  2543. *
  2544. * @return error code
  2545. */
  2546. static int prepare_cylindrical_out(AVFilterContext *ctx)
  2547. {
  2548. V360Context *s = ctx->priv;
  2549. s->flat_range[0] = M_PI * s->h_fov / 360.f;
  2550. s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
  2551. return 0;
  2552. }
  2553. /**
  2554. * Calculate 3D coordinates on sphere for corresponding frame position in cylindrical format.
  2555. *
  2556. * @param s filter private context
  2557. * @param i horizontal position on frame [0, width)
  2558. * @param j vertical position on frame [0, height)
  2559. * @param width frame width
  2560. * @param height frame height
  2561. * @param vec coordinates on sphere
  2562. */
  2563. static int cylindrical_to_xyz(const V360Context *s,
  2564. int i, int j, int width, int height,
  2565. float *vec)
  2566. {
  2567. const float uf = s->flat_range[0] * ((2.f * i + 1.f) / width - 1.f);
  2568. const float vf = s->flat_range[1] * ((2.f * j + 1.f) / height - 1.f);
  2569. const float phi = uf;
  2570. const float theta = atanf(vf);
  2571. const float sin_phi = sinf(phi);
  2572. const float cos_phi = cosf(phi);
  2573. const float sin_theta = sinf(theta);
  2574. const float cos_theta = cosf(theta);
  2575. vec[0] = cos_theta * sin_phi;
  2576. vec[1] = sin_theta;
  2577. vec[2] = cos_theta * cos_phi;
  2578. normalize_vector(vec);
  2579. return 1;
  2580. }
  2581. /**
  2582. * Prepare data for processing cylindrical input format.
  2583. *
  2584. * @param ctx filter context
  2585. *
  2586. * @return error code
  2587. */
  2588. static int prepare_cylindrical_in(AVFilterContext *ctx)
  2589. {
  2590. V360Context *s = ctx->priv;
  2591. s->iflat_range[0] = M_PI * s->ih_fov / 360.f;
  2592. s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
  2593. return 0;
  2594. }
  2595. /**
  2596. * Calculate frame position in cylindrical format for corresponding 3D coordinates on sphere.
  2597. *
  2598. * @param s filter private context
  2599. * @param vec coordinates on sphere
  2600. * @param width frame width
  2601. * @param height frame height
  2602. * @param us horizontal coordinates for interpolation window
  2603. * @param vs vertical coordinates for interpolation window
  2604. * @param du horizontal relative coordinate
  2605. * @param dv vertical relative coordinate
  2606. */
  2607. static int xyz_to_cylindrical(const V360Context *s,
  2608. const float *vec, int width, int height,
  2609. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2610. {
  2611. const float phi = atan2f(vec[0], vec[2]) / s->iflat_range[0];
  2612. const float theta = asinf(vec[1]);
  2613. const float uf = (phi + 1.f) * (width - 1) / 2.f;
  2614. const float vf = (tanf(theta) / s->iflat_range[1] + 1.f) * height / 2.f;
  2615. const int ui = floorf(uf);
  2616. const int vi = floorf(vf);
  2617. const int visible = vi >= 0 && vi < height && ui >= 0 && ui < width &&
  2618. theta <= M_PI * s->iv_fov / 180.f &&
  2619. theta >= -M_PI * s->iv_fov / 180.f;
  2620. *du = uf - ui;
  2621. *dv = vf - vi;
  2622. for (int i = 0; i < 4; i++) {
  2623. for (int j = 0; j < 4; j++) {
  2624. us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
  2625. vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
  2626. }
  2627. }
  2628. return visible;
  2629. }
  2630. /**
  2631. * Calculate 3D coordinates on sphere for corresponding frame position in perspective format.
  2632. *
  2633. * @param s filter private context
  2634. * @param i horizontal position on frame [0, width)
  2635. * @param j vertical position on frame [0, height)
  2636. * @param width frame width
  2637. * @param height frame height
  2638. * @param vec coordinates on sphere
  2639. */
  2640. static int perspective_to_xyz(const V360Context *s,
  2641. int i, int j, int width, int height,
  2642. float *vec)
  2643. {
  2644. const float uf = ((2.f * i + 1.f) / width - 1.f);
  2645. const float vf = ((2.f * j + 1.f) / height - 1.f);
  2646. const float rh = hypotf(uf, vf);
  2647. const float sinzz = 1.f - rh * rh;
  2648. const float h = 1.f + s->v_fov;
  2649. const float sinz = (h - sqrtf(sinzz)) / (h / rh + rh / h);
  2650. const float sinz2 = sinz * sinz;
  2651. if (sinz2 <= 1.f) {
  2652. const float cosz = sqrtf(1.f - sinz2);
  2653. const float theta = asinf(cosz);
  2654. const float phi = atan2f(uf, vf);
  2655. const float sin_phi = sinf(phi);
  2656. const float cos_phi = cosf(phi);
  2657. const float sin_theta = sinf(theta);
  2658. const float cos_theta = cosf(theta);
  2659. vec[0] = cos_theta * sin_phi;
  2660. vec[1] = sin_theta;
  2661. vec[2] = cos_theta * cos_phi;
  2662. } else {
  2663. vec[0] = 0.f;
  2664. vec[1] = 1.f;
  2665. vec[2] = 0.f;
  2666. return 0;
  2667. }
  2668. normalize_vector(vec);
  2669. return 1;
  2670. }
  2671. /**
  2672. * Calculate 3D coordinates on sphere for corresponding frame position in tetrahedron format.
  2673. *
  2674. * @param s filter private context
  2675. * @param i horizontal position on frame [0, width)
  2676. * @param j vertical position on frame [0, height)
  2677. * @param width frame width
  2678. * @param height frame height
  2679. * @param vec coordinates on sphere
  2680. */
  2681. static int tetrahedron_to_xyz(const V360Context *s,
  2682. int i, int j, int width, int height,
  2683. float *vec)
  2684. {
  2685. const float uf = (float)i / width;
  2686. const float vf = (float)j / height;
  2687. vec[0] = uf < 0.5f ? uf * 4.f - 1.f : 3.f - uf * 4.f;
  2688. vec[1] = 1.f - vf * 2.f;
  2689. vec[2] = 2.f * fabsf(1.f - fabsf(1.f - uf * 2.f + vf)) - 1.f;
  2690. normalize_vector(vec);
  2691. return 1;
  2692. }
  2693. /**
  2694. * Calculate frame position in tetrahedron format for corresponding 3D coordinates on sphere.
  2695. *
  2696. * @param s filter private context
  2697. * @param vec coordinates on sphere
  2698. * @param width frame width
  2699. * @param height frame height
  2700. * @param us horizontal coordinates for interpolation window
  2701. * @param vs vertical coordinates for interpolation window
  2702. * @param du horizontal relative coordinate
  2703. * @param dv vertical relative coordinate
  2704. */
  2705. static int xyz_to_tetrahedron(const V360Context *s,
  2706. const float *vec, int width, int height,
  2707. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2708. {
  2709. const float d0 = vec[0] * 1.f + vec[1] * 1.f + vec[2] *-1.f;
  2710. const float d1 = vec[0] *-1.f + vec[1] *-1.f + vec[2] *-1.f;
  2711. const float d2 = vec[0] * 1.f + vec[1] *-1.f + vec[2] * 1.f;
  2712. const float d3 = vec[0] *-1.f + vec[1] * 1.f + vec[2] * 1.f;
  2713. const float d = FFMAX(d0, FFMAX3(d1, d2, d3));
  2714. float uf, vf, x, y, z;
  2715. int ui, vi;
  2716. x = vec[0] / d;
  2717. y = vec[1] / d;
  2718. z = -vec[2] / d;
  2719. vf = 0.5f - y * 0.5f;
  2720. if ((x + y >= 0.f && y + z >= 0.f && -z - x <= 0.f) ||
  2721. (x + y <= 0.f && -y + z >= 0.f && z - x >= 0.f)) {
  2722. uf = 0.25f * x + 0.25f;
  2723. } else {
  2724. uf = 0.75f - 0.25f * x;
  2725. }
  2726. uf *= width;
  2727. vf *= height;
  2728. ui = floorf(uf);
  2729. vi = floorf(vf);
  2730. *du = uf - ui;
  2731. *dv = vf - vi;
  2732. for (int i = 0; i < 4; i++) {
  2733. for (int j = 0; j < 4; j++) {
  2734. us[i][j] = reflectx(ui + j - 1, vi + i - 1, width, height);
  2735. vs[i][j] = reflecty(vi + i - 1, height);
  2736. }
  2737. }
  2738. return 1;
  2739. }
  2740. /**
  2741. * Calculate 3D coordinates on sphere for corresponding frame position in dual fisheye format.
  2742. *
  2743. * @param s filter private context
  2744. * @param i horizontal position on frame [0, width)
  2745. * @param j vertical position on frame [0, height)
  2746. * @param width frame width
  2747. * @param height frame height
  2748. * @param vec coordinates on sphere
  2749. */
  2750. static int dfisheye_to_xyz(const V360Context *s,
  2751. int i, int j, int width, int height,
  2752. float *vec)
  2753. {
  2754. const float ew = width / 2.f;
  2755. const float eh = height;
  2756. const int ei = i >= ew ? i - ew : i;
  2757. const float m = i >= ew ? 1.f : -1.f;
  2758. const float uf = s->flat_range[0] * ((2.f * ei) / ew - 1.f);
  2759. const float vf = s->flat_range[1] * ((2.f * j + 1.f) / eh - 1.f);
  2760. const float h = hypotf(uf, vf);
  2761. const float lh = h > 0.f ? h : 1.f;
  2762. const float theta = m * M_PI_2 * (1.f - h);
  2763. const float sin_theta = sinf(theta);
  2764. const float cos_theta = cosf(theta);
  2765. vec[0] = cos_theta * m * uf / lh;
  2766. vec[1] = cos_theta * vf / lh;
  2767. vec[2] = sin_theta;
  2768. normalize_vector(vec);
  2769. return 1;
  2770. }
  2771. /**
  2772. * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
  2773. *
  2774. * @param s filter private context
  2775. * @param vec coordinates on sphere
  2776. * @param width frame width
  2777. * @param height frame height
  2778. * @param us horizontal coordinates for interpolation window
  2779. * @param vs vertical coordinates for interpolation window
  2780. * @param du horizontal relative coordinate
  2781. * @param dv vertical relative coordinate
  2782. */
  2783. static int xyz_to_dfisheye(const V360Context *s,
  2784. const float *vec, int width, int height,
  2785. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2786. {
  2787. const float ew = width / 2.f;
  2788. const float eh = height;
  2789. const float h = hypotf(vec[0], vec[1]);
  2790. const float lh = h > 0.f ? h : 1.f;
  2791. const float theta = acosf(fabsf(vec[2])) / M_PI;
  2792. float uf = (theta * (vec[0] / lh) / s->iflat_range[0] + 0.5f) * ew;
  2793. float vf = (theta * (vec[1] / lh) / s->iflat_range[1] + 0.5f) * eh;
  2794. int ui, vi;
  2795. int u_shift;
  2796. if (vec[2] >= 0.f) {
  2797. u_shift = ceilf(ew);
  2798. } else {
  2799. u_shift = 0;
  2800. uf = ew - uf;
  2801. }
  2802. ui = floorf(uf);
  2803. vi = floorf(vf);
  2804. *du = uf - ui;
  2805. *dv = vf - vi;
  2806. for (int i = 0; i < 4; i++) {
  2807. for (int j = 0; j < 4; j++) {
  2808. us[i][j] = av_clip(u_shift + ui + j - 1, 0, width - 1);
  2809. vs[i][j] = av_clip( vi + i - 1, 0, height - 1);
  2810. }
  2811. }
  2812. return 1;
  2813. }
  2814. /**
  2815. * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
  2816. *
  2817. * @param s filter private context
  2818. * @param i horizontal position on frame [0, width)
  2819. * @param j vertical position on frame [0, height)
  2820. * @param width frame width
  2821. * @param height frame height
  2822. * @param vec coordinates on sphere
  2823. */
  2824. static int barrel_to_xyz(const V360Context *s,
  2825. int i, int j, int width, int height,
  2826. float *vec)
  2827. {
  2828. const float scale = 0.99f;
  2829. float l_x, l_y, l_z;
  2830. if (i < 4 * width / 5) {
  2831. const float theta_range = M_PI_4;
  2832. const int ew = 4 * width / 5;
  2833. const int eh = height;
  2834. const float phi = ((2.f * i) / ew - 1.f) * M_PI / scale;
  2835. const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
  2836. const float sin_phi = sinf(phi);
  2837. const float cos_phi = cosf(phi);
  2838. const float sin_theta = sinf(theta);
  2839. const float cos_theta = cosf(theta);
  2840. l_x = cos_theta * sin_phi;
  2841. l_y = sin_theta;
  2842. l_z = cos_theta * cos_phi;
  2843. } else {
  2844. const int ew = width / 5;
  2845. const int eh = height / 2;
  2846. float uf, vf;
  2847. if (j < eh) { // UP
  2848. uf = 2.f * (i - 4 * ew) / ew - 1.f;
  2849. vf = 2.f * (j ) / eh - 1.f;
  2850. uf /= scale;
  2851. vf /= scale;
  2852. l_x = uf;
  2853. l_y = -1.f;
  2854. l_z = vf;
  2855. } else { // DOWN
  2856. uf = 2.f * (i - 4 * ew) / ew - 1.f;
  2857. vf = 2.f * (j - eh) / eh - 1.f;
  2858. uf /= scale;
  2859. vf /= scale;
  2860. l_x = uf;
  2861. l_y = 1.f;
  2862. l_z = -vf;
  2863. }
  2864. }
  2865. vec[0] = l_x;
  2866. vec[1] = l_y;
  2867. vec[2] = l_z;
  2868. normalize_vector(vec);
  2869. return 1;
  2870. }
  2871. /**
  2872. * Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
  2873. *
  2874. * @param s filter private context
  2875. * @param vec coordinates on sphere
  2876. * @param width frame width
  2877. * @param height frame height
  2878. * @param us horizontal coordinates for interpolation window
  2879. * @param vs vertical coordinates for interpolation window
  2880. * @param du horizontal relative coordinate
  2881. * @param dv vertical relative coordinate
  2882. */
  2883. static int xyz_to_barrel(const V360Context *s,
  2884. const float *vec, int width, int height,
  2885. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2886. {
  2887. const float scale = 0.99f;
  2888. const float phi = atan2f(vec[0], vec[2]);
  2889. const float theta = asinf(vec[1]);
  2890. const float theta_range = M_PI_4;
  2891. int ew, eh;
  2892. int u_shift, v_shift;
  2893. float uf, vf;
  2894. int ui, vi;
  2895. if (theta > -theta_range && theta < theta_range) {
  2896. ew = 4 * width / 5;
  2897. eh = height;
  2898. u_shift = 0;
  2899. v_shift = 0;
  2900. uf = (phi / M_PI * scale + 1.f) * ew / 2.f;
  2901. vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
  2902. } else {
  2903. ew = width / 5;
  2904. eh = height / 2;
  2905. u_shift = 4 * ew;
  2906. if (theta < 0.f) { // UP
  2907. uf = -vec[0] / vec[1];
  2908. vf = -vec[2] / vec[1];
  2909. v_shift = 0;
  2910. } else { // DOWN
  2911. uf = vec[0] / vec[1];
  2912. vf = -vec[2] / vec[1];
  2913. v_shift = eh;
  2914. }
  2915. uf = 0.5f * ew * (uf * scale + 1.f);
  2916. vf = 0.5f * eh * (vf * scale + 1.f);
  2917. }
  2918. ui = floorf(uf);
  2919. vi = floorf(vf);
  2920. *du = uf - ui;
  2921. *dv = vf - vi;
  2922. for (int i = 0; i < 4; i++) {
  2923. for (int j = 0; j < 4; j++) {
  2924. us[i][j] = u_shift + av_clip(ui + j - 1, 0, ew - 1);
  2925. vs[i][j] = v_shift + av_clip(vi + i - 1, 0, eh - 1);
  2926. }
  2927. }
  2928. return 1;
  2929. }
  2930. /**
  2931. * Calculate frame position in barrel split facebook's format for corresponding 3D coordinates on sphere.
  2932. *
  2933. * @param s filter private context
  2934. * @param vec coordinates on sphere
  2935. * @param width frame width
  2936. * @param height frame height
  2937. * @param us horizontal coordinates for interpolation window
  2938. * @param vs vertical coordinates for interpolation window
  2939. * @param du horizontal relative coordinate
  2940. * @param dv vertical relative coordinate
  2941. */
  2942. static int xyz_to_barrelsplit(const V360Context *s,
  2943. const float *vec, int width, int height,
  2944. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2945. {
  2946. const float phi = atan2f(vec[0], vec[2]);
  2947. const float theta = asinf(vec[1]);
  2948. const float theta_range = M_PI_4;
  2949. int ew, eh;
  2950. int u_shift, v_shift;
  2951. float uf, vf;
  2952. int ui, vi;
  2953. if (theta >= -theta_range && theta <= theta_range) {
  2954. const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width * 2.f / 3.f) : 1.f - s->in_pad;
  2955. const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 2.f) : 1.f - s->in_pad;
  2956. ew = width / 3 * 2;
  2957. eh = height / 2;
  2958. u_shift = 0;
  2959. v_shift = phi >= M_PI_2 || phi < -M_PI_2 ? eh : 0;
  2960. uf = fmodf(phi, M_PI_2) / M_PI_2;
  2961. vf = theta / M_PI_4;
  2962. if (v_shift)
  2963. uf = uf >= 0.f ? fmodf(uf - 1.f, 1.f) : fmodf(uf + 1.f, 1.f);
  2964. uf = (uf * scalew + 1.f) * width / 3.f;
  2965. vf = (vf * scaleh + 1.f) * height / 4.f;
  2966. } else {
  2967. const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width / 3.f) : 1.f - s->in_pad;
  2968. const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 4.f) : 1.f - s->in_pad;
  2969. int v_offset = 0;
  2970. ew = width / 3;
  2971. eh = height / 4;
  2972. u_shift = 2 * ew;
  2973. if (theta <= 0.f && theta >= -M_PI_2 &&
  2974. phi <= M_PI_2 && phi >= -M_PI_2) {
  2975. uf = -vec[0] / vec[1];
  2976. vf = -vec[2] / vec[1];
  2977. v_shift = 0;
  2978. v_offset = -eh;
  2979. } else if (theta >= 0.f && theta <= M_PI_2 &&
  2980. phi <= M_PI_2 && phi >= -M_PI_2) {
  2981. uf = vec[0] / vec[1];
  2982. vf = -vec[2] / vec[1];
  2983. v_shift = height * 0.25f;
  2984. } else if (theta <= 0.f && theta >= -M_PI_2) {
  2985. uf = vec[0] / vec[1];
  2986. vf = vec[2] / vec[1];
  2987. v_shift = height * 0.5f;
  2988. v_offset = -eh;
  2989. } else {
  2990. uf = -vec[0] / vec[1];
  2991. vf = vec[2] / vec[1];
  2992. v_shift = height * 0.75f;
  2993. }
  2994. uf = 0.5f * width / 3.f * (uf * scalew + 1.f);
  2995. vf = height * 0.25f * (vf * scaleh + 1.f) + v_offset;
  2996. }
  2997. ui = floorf(uf);
  2998. vi = floorf(vf);
  2999. *du = uf - ui;
  3000. *dv = vf - vi;
  3001. for (int i = 0; i < 4; i++) {
  3002. for (int j = 0; j < 4; j++) {
  3003. us[i][j] = u_shift + av_clip(ui + j - 1, 0, ew - 1);
  3004. vs[i][j] = v_shift + av_clip(vi + i - 1, 0, eh - 1);
  3005. }
  3006. }
  3007. return 1;
  3008. }
  3009. /**
  3010. * Calculate 3D coordinates on sphere for corresponding frame position in barrel split facebook's format.
  3011. *
  3012. * @param s filter private context
  3013. * @param i horizontal position on frame [0, width)
  3014. * @param j vertical position on frame [0, height)
  3015. * @param width frame width
  3016. * @param height frame height
  3017. * @param vec coordinates on sphere
  3018. */
  3019. static int barrelsplit_to_xyz(const V360Context *s,
  3020. int i, int j, int width, int height,
  3021. float *vec)
  3022. {
  3023. const float x = (i + 0.5f) / width;
  3024. const float y = (j + 0.5f) / height;
  3025. float l_x, l_y, l_z;
  3026. if (x < 2.f / 3.f) {
  3027. const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width * 2.f / 3.f) : 1.f - s->out_pad;
  3028. const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 2.f) : 1.f - s->out_pad;
  3029. const float back = floorf(y * 2.f);
  3030. const float phi = ((3.f / 2.f * x - 0.5f) / scalew - back) * M_PI;
  3031. const float theta = (y - 0.25f - 0.5f * back) / scaleh * M_PI;
  3032. const float sin_phi = sinf(phi);
  3033. const float cos_phi = cosf(phi);
  3034. const float sin_theta = sinf(theta);
  3035. const float cos_theta = cosf(theta);
  3036. l_x = cos_theta * sin_phi;
  3037. l_y = sin_theta;
  3038. l_z = cos_theta * cos_phi;
  3039. } else {
  3040. const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width / 3.f) : 1.f - s->out_pad;
  3041. const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 4.f) : 1.f - s->out_pad;
  3042. const int face = floorf(y * 4.f);
  3043. float uf, vf;
  3044. uf = x * 3.f - 2.f;
  3045. switch (face) {
  3046. case 0:
  3047. vf = y * 2.f;
  3048. uf = 1.f - uf;
  3049. vf = 0.5f - vf;
  3050. l_x = (0.5f - uf) / scalew;
  3051. l_y = -0.5f;
  3052. l_z = (0.5f - vf) / scaleh;
  3053. break;
  3054. case 1:
  3055. vf = y * 2.f;
  3056. uf = 1.f - uf;
  3057. vf = 1.f - (vf - 0.5f);
  3058. l_x = (0.5f - uf) / scalew;
  3059. l_y = 0.5f;
  3060. l_z = (-0.5f + vf) / scaleh;
  3061. break;
  3062. case 2:
  3063. vf = y * 2.f - 0.5f;
  3064. vf = 1.f - (1.f - vf);
  3065. l_x = (0.5f - uf) / scalew;
  3066. l_y = -0.5f;
  3067. l_z = (0.5f - vf) / scaleh;
  3068. break;
  3069. case 3:
  3070. vf = y * 2.f - 1.5f;
  3071. l_x = (0.5f - uf) / scalew;
  3072. l_y = 0.5f;
  3073. l_z = (-0.5f + vf) / scaleh;
  3074. break;
  3075. }
  3076. }
  3077. vec[0] = l_x;
  3078. vec[1] = l_y;
  3079. vec[2] = l_z;
  3080. normalize_vector(vec);
  3081. return 1;
  3082. }
  3083. /**
  3084. * Calculate 3D coordinates on sphere for corresponding frame position in tspyramid format.
  3085. *
  3086. * @param s filter private context
  3087. * @param i horizontal position on frame [0, width)
  3088. * @param j vertical position on frame [0, height)
  3089. * @param width frame width
  3090. * @param height frame height
  3091. * @param vec coordinates on sphere
  3092. */
  3093. static int tspyramid_to_xyz(const V360Context *s,
  3094. int i, int j, int width, int height,
  3095. float *vec)
  3096. {
  3097. const float x = (i + 0.5f) / width;
  3098. const float y = (j + 0.5f) / height;
  3099. if (x < 0.5f) {
  3100. vec[0] = x * 4.f - 1.f;
  3101. vec[1] = (y * 2.f - 1.f);
  3102. vec[2] = 1.f;
  3103. } else if (x >= 0.6875f && x < 0.8125f &&
  3104. y >= 0.375f && y < 0.625f) {
  3105. vec[0] = -(x - 0.6875f) * 16.f + 1.f;
  3106. vec[1] = (y - 0.375f) * 8.f - 1.f;
  3107. vec[2] = -1.f;
  3108. } else if (0.5f <= x && x < 0.6875f &&
  3109. ((0.f <= y && y < 0.375f && y >= 2.f * (x - 0.5f)) ||
  3110. (0.375f <= y && y < 0.625f) ||
  3111. (0.625f <= y && y < 1.f && y <= 2.f * (1.f - x)))) {
  3112. vec[0] = 1.f;
  3113. vec[1] = 2.f * (y - 2.f * x + 1.f) / (3.f - 4.f * x) - 1.f;
  3114. vec[2] = -2.f * (x - 0.5f) / 0.1875f + 1.f;
  3115. } else if (0.8125f <= x && x < 1.f &&
  3116. ((0.f <= y && y < 0.375f && x >= (1.f - y / 2.f)) ||
  3117. (0.375f <= y && y < 0.625f) ||
  3118. (0.625f <= y && y < 1.f && y <= (2.f * x - 1.f)))) {
  3119. vec[0] = -1.f;
  3120. vec[1] = 2.f * (y + 2.f * x - 2.f) / (4.f * x - 3.f) - 1.f;
  3121. vec[2] = 2.f * (x - 0.8125f) / 0.1875f - 1.f;
  3122. } else if (0.f <= y && y < 0.375f &&
  3123. ((0.5f <= x && x < 0.8125f && y < 2.f * (x - 0.5f)) ||
  3124. (0.6875f <= x && x < 0.8125f) ||
  3125. (0.8125f <= x && x < 1.f && x < (1.f - y / 2.f)))) {
  3126. vec[0] = 2.f * (1.f - x - 0.5f * y) / (0.5f - y) - 1.f;
  3127. vec[1] = -1.f;
  3128. vec[2] = 2.f * (0.375f - y) / 0.375f - 1.f;
  3129. } else {
  3130. vec[0] = 2.f * (0.5f - x + 0.5f * y) / (y - 0.5f) - 1.f;
  3131. vec[1] = 1.f;
  3132. vec[2] = -2.f * (1.f - y) / 0.375f + 1.f;
  3133. }
  3134. normalize_vector(vec);
  3135. return 1;
  3136. }
  3137. /**
  3138. * Calculate frame position in tspyramid format for corresponding 3D coordinates on sphere.
  3139. *
  3140. * @param s filter private context
  3141. * @param vec coordinates on sphere
  3142. * @param width frame width
  3143. * @param height frame height
  3144. * @param us horizontal coordinates for interpolation window
  3145. * @param vs vertical coordinates for interpolation window
  3146. * @param du horizontal relative coordinate
  3147. * @param dv vertical relative coordinate
  3148. */
  3149. static int xyz_to_tspyramid(const V360Context *s,
  3150. const float *vec, int width, int height,
  3151. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  3152. {
  3153. float uf, vf;
  3154. int ui, vi;
  3155. int face;
  3156. xyz_to_cube(s, vec, &uf, &vf, &face);
  3157. uf = (uf + 1.f) * 0.5f;
  3158. vf = (vf + 1.f) * 0.5f;
  3159. switch (face) {
  3160. case UP:
  3161. uf = 0.1875f * vf - 0.375f * uf * vf - 0.125f * uf + 0.8125f;
  3162. vf = 0.375f - 0.375f * vf;
  3163. break;
  3164. case FRONT:
  3165. uf = 0.5f * uf;
  3166. break;
  3167. case DOWN:
  3168. uf = 1.f - 0.1875f * vf - 0.5f * uf + 0.375f * uf * vf;
  3169. vf = 1.f - 0.375f * vf;
  3170. break;
  3171. case LEFT:
  3172. vf = 0.25f * vf + 0.75f * uf * vf - 0.375f * uf + 0.375f;
  3173. uf = 0.1875f * uf + 0.8125f;
  3174. break;
  3175. case RIGHT:
  3176. vf = 0.375f * uf - 0.75f * uf * vf + vf;
  3177. uf = 0.1875f * uf + 0.5f;
  3178. break;
  3179. case BACK:
  3180. uf = 0.125f * uf + 0.6875f;
  3181. vf = 0.25f * vf + 0.375f;
  3182. break;
  3183. }
  3184. uf *= width;
  3185. vf *= height;
  3186. ui = floorf(uf);
  3187. vi = floorf(vf);
  3188. *du = uf - ui;
  3189. *dv = vf - vi;
  3190. for (int i = 0; i < 4; i++) {
  3191. for (int j = 0; j < 4; j++) {
  3192. us[i][j] = reflectx(ui + j - 1, vi + i - 1, width, height);
  3193. vs[i][j] = reflecty(vi + i - 1, height);
  3194. }
  3195. }
  3196. return 1;
  3197. }
  3198. /**
  3199. * Calculate 3D coordinates on sphere for corresponding frame position in octahedron format.
  3200. *
  3201. * @param s filter private context
  3202. * @param i horizontal position on frame [0, width)
  3203. * @param j vertical position on frame [0, height)
  3204. * @param width frame width
  3205. * @param height frame height
  3206. * @param vec coordinates on sphere
  3207. */
  3208. static int octahedron_to_xyz(const V360Context *s,
  3209. int i, int j, int width, int height,
  3210. float *vec)
  3211. {
  3212. const float x = ((i + 0.5f) / width) * 2.f - 1.f;
  3213. const float y = ((j + 0.5f) / height) * 2.f - 1.f;
  3214. const float ax = fabsf(x);
  3215. const float ay = fabsf(y);
  3216. vec[2] = 1.f - (ax + ay);
  3217. if (ax + ay > 1.f) {
  3218. vec[0] = (1.f - ay) * FFSIGN(x);
  3219. vec[1] = (1.f - ax) * FFSIGN(y);
  3220. } else {
  3221. vec[0] = x;
  3222. vec[1] = y;
  3223. }
  3224. normalize_vector(vec);
  3225. return 1;
  3226. }
  3227. /**
  3228. * Calculate frame position in octahedron format for corresponding 3D coordinates on sphere.
  3229. *
  3230. * @param s filter private context
  3231. * @param vec coordinates on sphere
  3232. * @param width frame width
  3233. * @param height frame height
  3234. * @param us horizontal coordinates for interpolation window
  3235. * @param vs vertical coordinates for interpolation window
  3236. * @param du horizontal relative coordinate
  3237. * @param dv vertical relative coordinate
  3238. */
  3239. static int xyz_to_octahedron(const V360Context *s,
  3240. const float *vec, int width, int height,
  3241. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  3242. {
  3243. float uf, vf, zf;
  3244. int ui, vi;
  3245. float div = fabsf(vec[0]) + fabsf(vec[1]) + fabsf(vec[2]);
  3246. uf = vec[0] / div;
  3247. vf = vec[1] / div;
  3248. zf = vec[2];
  3249. if (zf < 0.f) {
  3250. zf = vf;
  3251. vf = (1.f - fabsf(uf)) * FFSIGN(zf);
  3252. uf = (1.f - fabsf(zf)) * FFSIGN(uf);
  3253. }
  3254. uf = uf * 0.5f + 0.5f;
  3255. vf = vf * 0.5f + 0.5f;
  3256. uf *= width;
  3257. vf *= height;
  3258. ui = floorf(uf);
  3259. vi = floorf(vf);
  3260. *du = uf - ui;
  3261. *dv = vf - vi;
  3262. for (int i = 0; i < 4; i++) {
  3263. for (int j = 0; j < 4; j++) {
  3264. us[i][j] = av_clip(ui + j - 1, 0, width - 1);
  3265. vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
  3266. }
  3267. }
  3268. return 1;
  3269. }
  3270. static void multiply_matrix(float c[3][3], const float a[3][3], const float b[3][3])
  3271. {
  3272. for (int i = 0; i < 3; i++) {
  3273. for (int j = 0; j < 3; j++) {
  3274. float sum = 0.f;
  3275. for (int k = 0; k < 3; k++)
  3276. sum += a[i][k] * b[k][j];
  3277. c[i][j] = sum;
  3278. }
  3279. }
  3280. }
  3281. /**
  3282. * Calculate rotation matrix for yaw/pitch/roll angles.
  3283. */
  3284. static inline void calculate_rotation_matrix(float yaw, float pitch, float roll,
  3285. float rot_mat[3][3],
  3286. const int rotation_order[3])
  3287. {
  3288. const float yaw_rad = yaw * M_PI / 180.f;
  3289. const float pitch_rad = pitch * M_PI / 180.f;
  3290. const float roll_rad = roll * M_PI / 180.f;
  3291. const float sin_yaw = sinf(yaw_rad);
  3292. const float cos_yaw = cosf(yaw_rad);
  3293. const float sin_pitch = sinf(pitch_rad);
  3294. const float cos_pitch = cosf(pitch_rad);
  3295. const float sin_roll = sinf(roll_rad);
  3296. const float cos_roll = cosf(roll_rad);
  3297. float m[3][3][3];
  3298. float temp[3][3];
  3299. m[0][0][0] = cos_yaw; m[0][0][1] = 0; m[0][0][2] = sin_yaw;
  3300. m[0][1][0] = 0; m[0][1][1] = 1; m[0][1][2] = 0;
  3301. m[0][2][0] = -sin_yaw; m[0][2][1] = 0; m[0][2][2] = cos_yaw;
  3302. m[1][0][0] = 1; m[1][0][1] = 0; m[1][0][2] = 0;
  3303. m[1][1][0] = 0; m[1][1][1] = cos_pitch; m[1][1][2] = -sin_pitch;
  3304. m[1][2][0] = 0; m[1][2][1] = sin_pitch; m[1][2][2] = cos_pitch;
  3305. m[2][0][0] = cos_roll; m[2][0][1] = -sin_roll; m[2][0][2] = 0;
  3306. m[2][1][0] = sin_roll; m[2][1][1] = cos_roll; m[2][1][2] = 0;
  3307. m[2][2][0] = 0; m[2][2][1] = 0; m[2][2][2] = 1;
  3308. multiply_matrix(temp, m[rotation_order[0]], m[rotation_order[1]]);
  3309. multiply_matrix(rot_mat, temp, m[rotation_order[2]]);
  3310. }
  3311. /**
  3312. * Rotate vector with given rotation matrix.
  3313. *
  3314. * @param rot_mat rotation matrix
  3315. * @param vec vector
  3316. */
  3317. static inline void rotate(const float rot_mat[3][3],
  3318. float *vec)
  3319. {
  3320. const float x_tmp = vec[0] * rot_mat[0][0] + vec[1] * rot_mat[0][1] + vec[2] * rot_mat[0][2];
  3321. const float y_tmp = vec[0] * rot_mat[1][0] + vec[1] * rot_mat[1][1] + vec[2] * rot_mat[1][2];
  3322. const float z_tmp = vec[0] * rot_mat[2][0] + vec[1] * rot_mat[2][1] + vec[2] * rot_mat[2][2];
  3323. vec[0] = x_tmp;
  3324. vec[1] = y_tmp;
  3325. vec[2] = z_tmp;
  3326. }
  3327. static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
  3328. float *modifier)
  3329. {
  3330. modifier[0] = h_flip ? -1.f : 1.f;
  3331. modifier[1] = v_flip ? -1.f : 1.f;
  3332. modifier[2] = d_flip ? -1.f : 1.f;
  3333. }
  3334. static inline void mirror(const float *modifier, float *vec)
  3335. {
  3336. vec[0] *= modifier[0];
  3337. vec[1] *= modifier[1];
  3338. vec[2] *= modifier[2];
  3339. }
  3340. static inline void input_flip(int16_t u[4][4], int16_t v[4][4], int w, int h, int hflip, int vflip)
  3341. {
  3342. if (hflip) {
  3343. for (int i = 0; i < 4; i++) {
  3344. for (int j = 0; j < 4; j++)
  3345. u[i][j] = w - 1 - u[i][j];
  3346. }
  3347. }
  3348. if (vflip) {
  3349. for (int i = 0; i < 4; i++) {
  3350. for (int j = 0; j < 4; j++)
  3351. v[i][j] = h - 1 - v[i][j];
  3352. }
  3353. }
  3354. }
  3355. static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int sizeof_mask, int p)
  3356. {
  3357. const int pr_height = s->pr_height[p];
  3358. for (int n = 0; n < s->nb_threads; n++) {
  3359. SliceXYRemap *r = &s->slice_remap[n];
  3360. const int slice_start = (pr_height * n ) / s->nb_threads;
  3361. const int slice_end = (pr_height * (n + 1)) / s->nb_threads;
  3362. const int height = slice_end - slice_start;
  3363. if (!r->u[p])
  3364. r->u[p] = av_calloc(s->uv_linesize[p] * height, sizeof_uv);
  3365. if (!r->v[p])
  3366. r->v[p] = av_calloc(s->uv_linesize[p] * height, sizeof_uv);
  3367. if (!r->u[p] || !r->v[p])
  3368. return AVERROR(ENOMEM);
  3369. if (sizeof_ker) {
  3370. if (!r->ker[p])
  3371. r->ker[p] = av_calloc(s->uv_linesize[p] * height, sizeof_ker);
  3372. if (!r->ker[p])
  3373. return AVERROR(ENOMEM);
  3374. }
  3375. if (sizeof_mask && !p) {
  3376. if (!r->mask)
  3377. r->mask = av_calloc(s->pr_width[p] * height, sizeof_mask);
  3378. if (!r->mask)
  3379. return AVERROR(ENOMEM);
  3380. }
  3381. }
  3382. return 0;
  3383. }
  3384. static void fov_from_dfov(int format, float d_fov, float w, float h, float *h_fov, float *v_fov)
  3385. {
  3386. switch (format) {
  3387. case ORTHOGRAPHIC:
  3388. {
  3389. const float d = 0.5f * hypotf(w, h);
  3390. const float l = sinf(d_fov * M_PI / 360.f) / d;
  3391. *h_fov = asinf(w * 0.5 * l) * 360.f / M_PI;
  3392. *v_fov = asinf(h * 0.5 * l) * 360.f / M_PI;
  3393. if (d_fov > 180.f) {
  3394. *h_fov = 180.f - *h_fov;
  3395. *v_fov = 180.f - *v_fov;
  3396. }
  3397. }
  3398. break;
  3399. case EQUISOLID:
  3400. {
  3401. const float d = 0.5f * hypotf(w, h);
  3402. const float l = d / (sinf(d_fov * M_PI / 720.f));
  3403. *h_fov = 2.f * asinf(w * 0.5f / l) * 360.f / M_PI;
  3404. *v_fov = 2.f * asinf(h * 0.5f / l) * 360.f / M_PI;
  3405. }
  3406. break;
  3407. case STEREOGRAPHIC:
  3408. {
  3409. const float d = 0.5f * hypotf(w, h);
  3410. const float l = d / (tanf(d_fov * M_PI / 720.f));
  3411. *h_fov = 2.f * atan2f(w * 0.5f, l) * 360.f / M_PI;
  3412. *v_fov = 2.f * atan2f(h * 0.5f, l) * 360.f / M_PI;
  3413. }
  3414. break;
  3415. case DUAL_FISHEYE:
  3416. {
  3417. const float d = 0.5f * hypotf(w * 0.5f, h);
  3418. *h_fov = d / w * 2.f * d_fov;
  3419. *v_fov = d / h * d_fov;
  3420. }
  3421. break;
  3422. case FISHEYE:
  3423. {
  3424. const float d = 0.5f * hypotf(w, h);
  3425. *h_fov = d / w * d_fov;
  3426. *v_fov = d / h * d_fov;
  3427. }
  3428. break;
  3429. case FLAT:
  3430. default:
  3431. {
  3432. const float da = tanf(0.5f * FFMIN(d_fov, 359.f) * M_PI / 180.f);
  3433. const float d = hypotf(w, h);
  3434. *h_fov = atan2f(da * w, d) * 360.f / M_PI;
  3435. *v_fov = atan2f(da * h, d) * 360.f / M_PI;
  3436. if (*h_fov < 0.f)
  3437. *h_fov += 360.f;
  3438. if (*v_fov < 0.f)
  3439. *v_fov += 360.f;
  3440. }
  3441. break;
  3442. }
  3443. }
  3444. static void set_dimensions(int *outw, int *outh, int w, int h, const AVPixFmtDescriptor *desc)
  3445. {
  3446. outw[1] = outw[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
  3447. outw[0] = outw[3] = w;
  3448. outh[1] = outh[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
  3449. outh[0] = outh[3] = h;
  3450. }
  3451. // Calculate remap data
  3452. static av_always_inline int v360_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  3453. {
  3454. V360Context *s = ctx->priv;
  3455. SliceXYRemap *r = &s->slice_remap[jobnr];
  3456. for (int p = 0; p < s->nb_allocated; p++) {
  3457. const int max_value = s->max_value;
  3458. const int width = s->pr_width[p];
  3459. const int uv_linesize = s->uv_linesize[p];
  3460. const int height = s->pr_height[p];
  3461. const int in_width = s->inplanewidth[p];
  3462. const int in_height = s->inplaneheight[p];
  3463. const int slice_start = (height * jobnr ) / nb_jobs;
  3464. const int slice_end = (height * (jobnr + 1)) / nb_jobs;
  3465. const int elements = s->elements;
  3466. float du, dv;
  3467. float vec[3];
  3468. XYRemap rmap;
  3469. for (int j = slice_start; j < slice_end; j++) {
  3470. for (int i = 0; i < width; i++) {
  3471. int16_t *u = r->u[p] + ((j - slice_start) * uv_linesize + i) * elements;
  3472. int16_t *v = r->v[p] + ((j - slice_start) * uv_linesize + i) * elements;
  3473. int16_t *ker = r->ker[p] + ((j - slice_start) * uv_linesize + i) * elements;
  3474. uint8_t *mask8 = p ? NULL : r->mask + ((j - slice_start) * s->pr_width[0] + i);
  3475. uint16_t *mask16 = p ? NULL : (uint16_t *)r->mask + ((j - slice_start) * s->pr_width[0] + i);
  3476. int in_mask, out_mask;
  3477. if (s->out_transpose)
  3478. out_mask = s->out_transform(s, j, i, height, width, vec);
  3479. else
  3480. out_mask = s->out_transform(s, i, j, width, height, vec);
  3481. av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
  3482. rotate(s->rot_mat, vec);
  3483. av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
  3484. normalize_vector(vec);
  3485. mirror(s->output_mirror_modifier, vec);
  3486. if (s->in_transpose)
  3487. in_mask = s->in_transform(s, vec, in_height, in_width, rmap.v, rmap.u, &du, &dv);
  3488. else
  3489. in_mask = s->in_transform(s, vec, in_width, in_height, rmap.u, rmap.v, &du, &dv);
  3490. input_flip(rmap.u, rmap.v, in_width, in_height, s->ih_flip, s->iv_flip);
  3491. av_assert1(!isnan(du) && !isnan(dv));
  3492. s->calculate_kernel(du, dv, &rmap, u, v, ker);
  3493. if (!p && r->mask) {
  3494. if (s->mask_size == 1) {
  3495. mask8[0] = 255 * (out_mask & in_mask);
  3496. } else {
  3497. mask16[0] = max_value * (out_mask & in_mask);
  3498. }
  3499. }
  3500. }
  3501. }
  3502. }
  3503. return 0;
  3504. }
  3505. static int config_output(AVFilterLink *outlink)
  3506. {
  3507. AVFilterContext *ctx = outlink->src;
  3508. AVFilterLink *inlink = ctx->inputs[0];
  3509. V360Context *s = ctx->priv;
  3510. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  3511. const int depth = desc->comp[0].depth;
  3512. const int sizeof_mask = s->mask_size = (depth + 7) >> 3;
  3513. int sizeof_uv;
  3514. int sizeof_ker;
  3515. int err;
  3516. int h, w;
  3517. int in_offset_h, in_offset_w;
  3518. int out_offset_h, out_offset_w;
  3519. float hf, wf;
  3520. int (*prepare_out)(AVFilterContext *ctx);
  3521. int have_alpha;
  3522. s->max_value = (1 << depth) - 1;
  3523. switch (s->interp) {
  3524. case NEAREST:
  3525. s->calculate_kernel = nearest_kernel;
  3526. s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
  3527. s->elements = 1;
  3528. sizeof_uv = sizeof(int16_t) * s->elements;
  3529. sizeof_ker = 0;
  3530. break;
  3531. case BILINEAR:
  3532. s->calculate_kernel = bilinear_kernel;
  3533. s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
  3534. s->elements = 2 * 2;
  3535. sizeof_uv = sizeof(int16_t) * s->elements;
  3536. sizeof_ker = sizeof(int16_t) * s->elements;
  3537. break;
  3538. case LAGRANGE9:
  3539. s->calculate_kernel = lagrange_kernel;
  3540. s->remap_slice = depth <= 8 ? remap3_8bit_slice : remap3_16bit_slice;
  3541. s->elements = 3 * 3;
  3542. sizeof_uv = sizeof(int16_t) * s->elements;
  3543. sizeof_ker = sizeof(int16_t) * s->elements;
  3544. break;
  3545. case BICUBIC:
  3546. s->calculate_kernel = bicubic_kernel;
  3547. s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
  3548. s->elements = 4 * 4;
  3549. sizeof_uv = sizeof(int16_t) * s->elements;
  3550. sizeof_ker = sizeof(int16_t) * s->elements;
  3551. break;
  3552. case LANCZOS:
  3553. s->calculate_kernel = lanczos_kernel;
  3554. s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
  3555. s->elements = 4 * 4;
  3556. sizeof_uv = sizeof(int16_t) * s->elements;
  3557. sizeof_ker = sizeof(int16_t) * s->elements;
  3558. break;
  3559. case SPLINE16:
  3560. s->calculate_kernel = spline16_kernel;
  3561. s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
  3562. s->elements = 4 * 4;
  3563. sizeof_uv = sizeof(int16_t) * s->elements;
  3564. sizeof_ker = sizeof(int16_t) * s->elements;
  3565. break;
  3566. case GAUSSIAN:
  3567. s->calculate_kernel = gaussian_kernel;
  3568. s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
  3569. s->elements = 4 * 4;
  3570. sizeof_uv = sizeof(int16_t) * s->elements;
  3571. sizeof_ker = sizeof(int16_t) * s->elements;
  3572. break;
  3573. default:
  3574. av_assert0(0);
  3575. }
  3576. ff_v360_init(s, depth);
  3577. for (int order = 0; order < NB_RORDERS; order++) {
  3578. const char c = s->rorder[order];
  3579. int rorder;
  3580. if (c == '\0') {
  3581. av_log(ctx, AV_LOG_WARNING,
  3582. "Incomplete rorder option. Direction for all 3 rotation orders should be specified. Switching to default rorder.\n");
  3583. s->rotation_order[0] = YAW;
  3584. s->rotation_order[1] = PITCH;
  3585. s->rotation_order[2] = ROLL;
  3586. break;
  3587. }
  3588. rorder = get_rorder(c);
  3589. if (rorder == -1) {
  3590. av_log(ctx, AV_LOG_WARNING,
  3591. "Incorrect rotation order symbol '%c' in rorder option. Switching to default rorder.\n", c);
  3592. s->rotation_order[0] = YAW;
  3593. s->rotation_order[1] = PITCH;
  3594. s->rotation_order[2] = ROLL;
  3595. break;
  3596. }
  3597. s->rotation_order[order] = rorder;
  3598. }
  3599. switch (s->in_stereo) {
  3600. case STEREO_2D:
  3601. w = inlink->w;
  3602. h = inlink->h;
  3603. in_offset_w = in_offset_h = 0;
  3604. break;
  3605. case STEREO_SBS:
  3606. w = inlink->w / 2;
  3607. h = inlink->h;
  3608. in_offset_w = w;
  3609. in_offset_h = 0;
  3610. break;
  3611. case STEREO_TB:
  3612. w = inlink->w;
  3613. h = inlink->h / 2;
  3614. in_offset_w = 0;
  3615. in_offset_h = h;
  3616. break;
  3617. default:
  3618. av_assert0(0);
  3619. }
  3620. set_dimensions(s->inplanewidth, s->inplaneheight, w, h, desc);
  3621. set_dimensions(s->in_offset_w, s->in_offset_h, in_offset_w, in_offset_h, desc);
  3622. s->in_width = s->inplanewidth[0];
  3623. s->in_height = s->inplaneheight[0];
  3624. if (s->id_fov > 0.f)
  3625. fov_from_dfov(s->in, s->id_fov, w, h, &s->ih_fov, &s->iv_fov);
  3626. if (s->in_transpose)
  3627. FFSWAP(int, s->in_width, s->in_height);
  3628. switch (s->in) {
  3629. case EQUIRECTANGULAR:
  3630. s->in_transform = xyz_to_equirect;
  3631. err = 0;
  3632. wf = w;
  3633. hf = h;
  3634. break;
  3635. case CUBEMAP_3_2:
  3636. s->in_transform = xyz_to_cube3x2;
  3637. err = prepare_cube_in(ctx);
  3638. wf = w / 3.f * 4.f;
  3639. hf = h;
  3640. break;
  3641. case CUBEMAP_1_6:
  3642. s->in_transform = xyz_to_cube1x6;
  3643. err = prepare_cube_in(ctx);
  3644. wf = w * 4.f;
  3645. hf = h / 3.f;
  3646. break;
  3647. case CUBEMAP_6_1:
  3648. s->in_transform = xyz_to_cube6x1;
  3649. err = prepare_cube_in(ctx);
  3650. wf = w / 3.f * 2.f;
  3651. hf = h * 2.f;
  3652. break;
  3653. case EQUIANGULAR:
  3654. s->in_transform = xyz_to_eac;
  3655. err = prepare_eac_in(ctx);
  3656. wf = w;
  3657. hf = h / 9.f * 8.f;
  3658. break;
  3659. case FLAT:
  3660. s->in_transform = xyz_to_flat;
  3661. err = prepare_flat_in(ctx);
  3662. wf = w;
  3663. hf = h;
  3664. break;
  3665. case PERSPECTIVE:
  3666. av_log(ctx, AV_LOG_ERROR, "Supplied format is not accepted as input.\n");
  3667. return AVERROR(EINVAL);
  3668. case DUAL_FISHEYE:
  3669. s->in_transform = xyz_to_dfisheye;
  3670. err = prepare_fisheye_in(ctx);
  3671. wf = w;
  3672. hf = h;
  3673. break;
  3674. case BARREL:
  3675. s->in_transform = xyz_to_barrel;
  3676. err = 0;
  3677. wf = w / 5.f * 4.f;
  3678. hf = h;
  3679. break;
  3680. case STEREOGRAPHIC:
  3681. s->in_transform = xyz_to_stereographic;
  3682. err = prepare_stereographic_in(ctx);
  3683. wf = w;
  3684. hf = h / 2.f;
  3685. break;
  3686. case MERCATOR:
  3687. s->in_transform = xyz_to_mercator;
  3688. err = 0;
  3689. wf = w;
  3690. hf = h / 2.f;
  3691. break;
  3692. case BALL:
  3693. s->in_transform = xyz_to_ball;
  3694. err = 0;
  3695. wf = w;
  3696. hf = h / 2.f;
  3697. break;
  3698. case HAMMER:
  3699. s->in_transform = xyz_to_hammer;
  3700. err = 0;
  3701. wf = w;
  3702. hf = h;
  3703. break;
  3704. case SINUSOIDAL:
  3705. s->in_transform = xyz_to_sinusoidal;
  3706. err = 0;
  3707. wf = w;
  3708. hf = h;
  3709. break;
  3710. case FISHEYE:
  3711. s->in_transform = xyz_to_fisheye;
  3712. err = prepare_fisheye_in(ctx);
  3713. wf = w * 2;
  3714. hf = h;
  3715. break;
  3716. case PANNINI:
  3717. s->in_transform = xyz_to_pannini;
  3718. err = 0;
  3719. wf = w;
  3720. hf = h;
  3721. break;
  3722. case CYLINDRICAL:
  3723. s->in_transform = xyz_to_cylindrical;
  3724. err = prepare_cylindrical_in(ctx);
  3725. wf = w;
  3726. hf = h * 2.f;
  3727. break;
  3728. case TETRAHEDRON:
  3729. s->in_transform = xyz_to_tetrahedron;
  3730. err = 0;
  3731. wf = w;
  3732. hf = h;
  3733. break;
  3734. case BARREL_SPLIT:
  3735. s->in_transform = xyz_to_barrelsplit;
  3736. err = 0;
  3737. wf = w * 4.f / 3.f;
  3738. hf = h;
  3739. break;
  3740. case TSPYRAMID:
  3741. s->in_transform = xyz_to_tspyramid;
  3742. err = 0;
  3743. wf = w;
  3744. hf = h;
  3745. break;
  3746. case HEQUIRECTANGULAR:
  3747. s->in_transform = xyz_to_hequirect;
  3748. err = 0;
  3749. wf = w * 2.f;
  3750. hf = h;
  3751. break;
  3752. case EQUISOLID:
  3753. s->in_transform = xyz_to_equisolid;
  3754. err = prepare_equisolid_in(ctx);
  3755. wf = w;
  3756. hf = h / 2.f;
  3757. break;
  3758. case ORTHOGRAPHIC:
  3759. s->in_transform = xyz_to_orthographic;
  3760. err = prepare_orthographic_in(ctx);
  3761. wf = w;
  3762. hf = h / 2.f;
  3763. break;
  3764. case OCTAHEDRON:
  3765. s->in_transform = xyz_to_octahedron;
  3766. err = 0;
  3767. wf = w;
  3768. hf = h / 2.f;
  3769. break;
  3770. default:
  3771. av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
  3772. return AVERROR_BUG;
  3773. }
  3774. if (err != 0) {
  3775. return err;
  3776. }
  3777. switch (s->out) {
  3778. case EQUIRECTANGULAR:
  3779. s->out_transform = equirect_to_xyz;
  3780. prepare_out = NULL;
  3781. w = lrintf(wf);
  3782. h = lrintf(hf);
  3783. break;
  3784. case CUBEMAP_3_2:
  3785. s->out_transform = cube3x2_to_xyz;
  3786. prepare_out = prepare_cube_out;
  3787. w = lrintf(wf / 4.f * 3.f);
  3788. h = lrintf(hf);
  3789. break;
  3790. case CUBEMAP_1_6:
  3791. s->out_transform = cube1x6_to_xyz;
  3792. prepare_out = prepare_cube_out;
  3793. w = lrintf(wf / 4.f);
  3794. h = lrintf(hf * 3.f);
  3795. break;
  3796. case CUBEMAP_6_1:
  3797. s->out_transform = cube6x1_to_xyz;
  3798. prepare_out = prepare_cube_out;
  3799. w = lrintf(wf / 2.f * 3.f);
  3800. h = lrintf(hf / 2.f);
  3801. break;
  3802. case EQUIANGULAR:
  3803. s->out_transform = eac_to_xyz;
  3804. prepare_out = prepare_eac_out;
  3805. w = lrintf(wf);
  3806. h = lrintf(hf / 8.f * 9.f);
  3807. break;
  3808. case FLAT:
  3809. s->out_transform = flat_to_xyz;
  3810. prepare_out = prepare_flat_out;
  3811. w = lrintf(wf);
  3812. h = lrintf(hf);
  3813. break;
  3814. case DUAL_FISHEYE:
  3815. s->out_transform = dfisheye_to_xyz;
  3816. prepare_out = prepare_fisheye_out;
  3817. w = lrintf(wf);
  3818. h = lrintf(hf);
  3819. break;
  3820. case BARREL:
  3821. s->out_transform = barrel_to_xyz;
  3822. prepare_out = NULL;
  3823. w = lrintf(wf / 4.f * 5.f);
  3824. h = lrintf(hf);
  3825. break;
  3826. case STEREOGRAPHIC:
  3827. s->out_transform = stereographic_to_xyz;
  3828. prepare_out = prepare_stereographic_out;
  3829. w = lrintf(wf);
  3830. h = lrintf(hf * 2.f);
  3831. break;
  3832. case MERCATOR:
  3833. s->out_transform = mercator_to_xyz;
  3834. prepare_out = NULL;
  3835. w = lrintf(wf);
  3836. h = lrintf(hf * 2.f);
  3837. break;
  3838. case BALL:
  3839. s->out_transform = ball_to_xyz;
  3840. prepare_out = NULL;
  3841. w = lrintf(wf);
  3842. h = lrintf(hf * 2.f);
  3843. break;
  3844. case HAMMER:
  3845. s->out_transform = hammer_to_xyz;
  3846. prepare_out = NULL;
  3847. w = lrintf(wf);
  3848. h = lrintf(hf);
  3849. break;
  3850. case SINUSOIDAL:
  3851. s->out_transform = sinusoidal_to_xyz;
  3852. prepare_out = NULL;
  3853. w = lrintf(wf);
  3854. h = lrintf(hf);
  3855. break;
  3856. case FISHEYE:
  3857. s->out_transform = fisheye_to_xyz;
  3858. prepare_out = prepare_fisheye_out;
  3859. w = lrintf(wf * 0.5f);
  3860. h = lrintf(hf);
  3861. break;
  3862. case PANNINI:
  3863. s->out_transform = pannini_to_xyz;
  3864. prepare_out = NULL;
  3865. w = lrintf(wf);
  3866. h = lrintf(hf);
  3867. break;
  3868. case CYLINDRICAL:
  3869. s->out_transform = cylindrical_to_xyz;
  3870. prepare_out = prepare_cylindrical_out;
  3871. w = lrintf(wf);
  3872. h = lrintf(hf * 0.5f);
  3873. break;
  3874. case PERSPECTIVE:
  3875. s->out_transform = perspective_to_xyz;
  3876. prepare_out = NULL;
  3877. w = lrintf(wf / 2.f);
  3878. h = lrintf(hf);
  3879. break;
  3880. case TETRAHEDRON:
  3881. s->out_transform = tetrahedron_to_xyz;
  3882. prepare_out = NULL;
  3883. w = lrintf(wf);
  3884. h = lrintf(hf);
  3885. break;
  3886. case BARREL_SPLIT:
  3887. s->out_transform = barrelsplit_to_xyz;
  3888. prepare_out = NULL;
  3889. w = lrintf(wf / 4.f * 3.f);
  3890. h = lrintf(hf);
  3891. break;
  3892. case TSPYRAMID:
  3893. s->out_transform = tspyramid_to_xyz;
  3894. prepare_out = NULL;
  3895. w = lrintf(wf);
  3896. h = lrintf(hf);
  3897. break;
  3898. case HEQUIRECTANGULAR:
  3899. s->out_transform = hequirect_to_xyz;
  3900. prepare_out = NULL;
  3901. w = lrintf(wf / 2.f);
  3902. h = lrintf(hf);
  3903. break;
  3904. case EQUISOLID:
  3905. s->out_transform = equisolid_to_xyz;
  3906. prepare_out = prepare_equisolid_out;
  3907. w = lrintf(wf);
  3908. h = lrintf(hf * 2.f);
  3909. break;
  3910. case ORTHOGRAPHIC:
  3911. s->out_transform = orthographic_to_xyz;
  3912. prepare_out = prepare_orthographic_out;
  3913. w = lrintf(wf);
  3914. h = lrintf(hf * 2.f);
  3915. break;
  3916. case OCTAHEDRON:
  3917. s->out_transform = octahedron_to_xyz;
  3918. prepare_out = NULL;
  3919. w = lrintf(wf);
  3920. h = lrintf(hf * 2.f);
  3921. break;
  3922. default:
  3923. av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
  3924. return AVERROR_BUG;
  3925. }
  3926. // Override resolution with user values if specified
  3927. if (s->width > 0 && s->height <= 0 && s->h_fov > 0.f && s->v_fov > 0.f &&
  3928. s->out == FLAT && s->d_fov == 0.f) {
  3929. w = s->width;
  3930. h = w / tanf(s->h_fov * M_PI / 360.f) * tanf(s->v_fov * M_PI / 360.f);
  3931. } else if (s->width <= 0 && s->height > 0 && s->h_fov > 0.f && s->v_fov > 0.f &&
  3932. s->out == FLAT && s->d_fov == 0.f) {
  3933. h = s->height;
  3934. w = h / tanf(s->v_fov * M_PI / 360.f) * tanf(s->h_fov * M_PI / 360.f);
  3935. } else if (s->width > 0 && s->height > 0) {
  3936. w = s->width;
  3937. h = s->height;
  3938. } else if (s->width > 0 || s->height > 0) {
  3939. av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
  3940. return AVERROR(EINVAL);
  3941. } else {
  3942. if (s->out_transpose)
  3943. FFSWAP(int, w, h);
  3944. if (s->in_transpose)
  3945. FFSWAP(int, w, h);
  3946. }
  3947. s->width = w;
  3948. s->height = h;
  3949. if (s->d_fov > 0.f)
  3950. fov_from_dfov(s->out, s->d_fov, w, h, &s->h_fov, &s->v_fov);
  3951. if (prepare_out) {
  3952. err = prepare_out(ctx);
  3953. if (err != 0)
  3954. return err;
  3955. }
  3956. set_dimensions(s->pr_width, s->pr_height, w, h, desc);
  3957. switch (s->out_stereo) {
  3958. case STEREO_2D:
  3959. out_offset_w = out_offset_h = 0;
  3960. break;
  3961. case STEREO_SBS:
  3962. out_offset_w = w;
  3963. out_offset_h = 0;
  3964. w *= 2;
  3965. break;
  3966. case STEREO_TB:
  3967. out_offset_w = 0;
  3968. out_offset_h = h;
  3969. h *= 2;
  3970. break;
  3971. default:
  3972. av_assert0(0);
  3973. }
  3974. set_dimensions(s->out_offset_w, s->out_offset_h, out_offset_w, out_offset_h, desc);
  3975. set_dimensions(s->planewidth, s->planeheight, w, h, desc);
  3976. for (int i = 0; i < 4; i++)
  3977. s->uv_linesize[i] = FFALIGN(s->pr_width[i], 8);
  3978. outlink->h = h;
  3979. outlink->w = w;
  3980. s->nb_threads = FFMIN(outlink->h, ff_filter_get_nb_threads(ctx));
  3981. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  3982. have_alpha = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
  3983. if (desc->log2_chroma_h == desc->log2_chroma_w && desc->log2_chroma_h == 0) {
  3984. s->nb_allocated = 1;
  3985. s->map[0] = s->map[1] = s->map[2] = s->map[3] = 0;
  3986. } else {
  3987. s->nb_allocated = 2;
  3988. s->map[0] = s->map[3] = 0;
  3989. s->map[1] = s->map[2] = 1;
  3990. }
  3991. if (!s->slice_remap)
  3992. s->slice_remap = av_calloc(s->nb_threads, sizeof(*s->slice_remap));
  3993. if (!s->slice_remap)
  3994. return AVERROR(ENOMEM);
  3995. for (int i = 0; i < s->nb_allocated; i++) {
  3996. err = allocate_plane(s, sizeof_uv, sizeof_ker, sizeof_mask * have_alpha * s->alpha, i);
  3997. if (err < 0)
  3998. return err;
  3999. }
  4000. calculate_rotation_matrix(s->yaw, s->pitch, s->roll, s->rot_mat, s->rotation_order);
  4001. set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, s->output_mirror_modifier);
  4002. ctx->internal->execute(ctx, v360_slice, NULL, NULL, s->nb_threads);
  4003. return 0;
  4004. }
  4005. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  4006. {
  4007. AVFilterContext *ctx = inlink->dst;
  4008. AVFilterLink *outlink = ctx->outputs[0];
  4009. V360Context *s = ctx->priv;
  4010. AVFrame *out;
  4011. ThreadData td;
  4012. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  4013. if (!out) {
  4014. av_frame_free(&in);
  4015. return AVERROR(ENOMEM);
  4016. }
  4017. av_frame_copy_props(out, in);
  4018. td.in = in;
  4019. td.out = out;
  4020. ctx->internal->execute(ctx, s->remap_slice, &td, NULL, s->nb_threads);
  4021. av_frame_free(&in);
  4022. return ff_filter_frame(outlink, out);
  4023. }
  4024. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  4025. char *res, int res_len, int flags)
  4026. {
  4027. int ret;
  4028. ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
  4029. if (ret < 0)
  4030. return ret;
  4031. return config_output(ctx->outputs[0]);
  4032. }
  4033. static av_cold void uninit(AVFilterContext *ctx)
  4034. {
  4035. V360Context *s = ctx->priv;
  4036. for (int n = 0; n < s->nb_threads && s->slice_remap; n++) {
  4037. SliceXYRemap *r = &s->slice_remap[n];
  4038. for (int p = 0; p < s->nb_allocated; p++) {
  4039. av_freep(&r->u[p]);
  4040. av_freep(&r->v[p]);
  4041. av_freep(&r->ker[p]);
  4042. }
  4043. av_freep(&r->mask);
  4044. }
  4045. av_freep(&s->slice_remap);
  4046. }
  4047. static const AVFilterPad inputs[] = {
  4048. {
  4049. .name = "default",
  4050. .type = AVMEDIA_TYPE_VIDEO,
  4051. .filter_frame = filter_frame,
  4052. },
  4053. { NULL }
  4054. };
  4055. static const AVFilterPad outputs[] = {
  4056. {
  4057. .name = "default",
  4058. .type = AVMEDIA_TYPE_VIDEO,
  4059. .config_props = config_output,
  4060. },
  4061. { NULL }
  4062. };
  4063. AVFilter ff_vf_v360 = {
  4064. .name = "v360",
  4065. .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
  4066. .priv_size = sizeof(V360Context),
  4067. .uninit = uninit,
  4068. .query_formats = query_formats,
  4069. .inputs = inputs,
  4070. .outputs = outputs,
  4071. .priv_class = &v360_class,
  4072. .flags = AVFILTER_FLAG_SLICE_THREADS,
  4073. .process_command = process_command,
  4074. };