Real-time WebGL video manipulation | by Szabolcs Damján
文章推薦指數: 80 %
This is part III of the “Manipulating video in a browser” series. In the previous articles, we experimented with the new “Insertable Streams for ... GetunlimitedaccessOpeninappHomeNotificationsListsStoriesWritePublishedinDoclerEngineeringReal-timeWebGLvideomanipulationlive!ThisispartIIIofthe“Manipulatingvideoinabrowser”series.Inthepreviousarticles,weexperimentedwiththenew“InsertableStreamsforMediaStreamTrack”APItomodifythewebcam’svideostreambeforesendingitfurther—totheWebRTCmodule,forexample.ThefirstattemptusedJavaScripttoprocessthevideoframes:https://medium.com/docler-engineering/manipulating-video-in-a-browser-5b37f8149d9bThesecondoneusedWebAssemblytodothesameoperation:https://medium.com/docler-engineering/video-manipulation-with-webassembly-3477a0c8524dOurexampleisasimplegreenscreeneffectwheretheapplicationswapsthegreenbackgroundtoanicesummerscene.originalscenemodifiedsceneTheresultsweresomewhatdisappointing:alowerperformancePC,oranaveragemobiledevice,wasnotabletodeliverasolid30frame/secin720p(HD)!Raisethebet!Thereisahiddenworkhorseinnearlyeverycomputer,regardlessifit’sasmartphoneorahigh-endPC:thegraphicsprocessora.k.a.thefamousGPUNow,I’llshowhowtogettheGPUtoprocessvideoframesinabrowserenvironment.WebGLtotherescueInmodernbrowsers,ourkeytoGPUprocessingistheWebGLAPI.Let’sdustoffourpreviousexampleandswaptheframetransformerfunctionforWebGL’sone!GPUprocessingisalittlebitdifferentthanourpreviousexamples.WewillneedtoprepareanduploadspecialprogramstotheGPU.Withthehelpofthevertexandfragmentshaderprograms,wecantransformourvideoframes.EachframewillbeuploadedtotheGPUasatexture,thenthemodifiedframewillbepassedfurthertothevideostream.processflowImustmentionthatthisarticlewon’texplainallthedetailsofWebGL,butitwilldemonstrateaWebGLbasedimplementationofacertaintechnicalproblem.Ihaveaddedsomelinkstolearningmaterialintheappendixifyouarewishtodigdeeperintothetopic.PrepareforWebGLmagicTouseWebGL,youwillneedthefollowingcomponents:AHTMLcanvaselementItisnotnecessarytoattachthiselementtotheDOM,itcansimplyexistinmemory.ThiswillbetherunningcontextofyourWebGLprograms,andwillholdtheresultingimageaftereveryprocessinground.ThevertexshaderSinceourimageisasimple,staticrectangleinthe3DspaceprovidedbyWebGL,thiscomponentwillnotdotoomuch,onlycoordinatesystemtransformations.ThefragmentshaderThispieceofprogramwilldotheimagemanipulationbyrunningontheGPUdirectly.WebGLinitializationCheckoutthedetailsintheupcomingsections.WebGLinitializationBeforegettingtheGPUtodowhatwewant,weneedtoinitializetheWebGLsubsystem.Theinitializationcodeismostlygeneric,theonlyexceptionistheinterestingwayweneedtouploadtheYUVcolorformatvideoframes.ThefullinitializationscriptcanbefoundintheAppendix.Apartfromthebasicinitializationprocess,wealsoneedtouploadthebackgroundimagetotheGPUasanormalRGBAtexture.However,thevideoframescomingfromthecameraareintheYUVcolorformat,whichisnotdirectly“understandable”fortheGPU.WewillneedtodelegatethecolorspaceconversiontotheGPUaswell,anduploadtheYUVencodedframedatadirectlyasatexture.AgenericRGBAtextureisnotexactlysuitableforthispurpose,becausetheY,UandVchannelsarepackedinseparatesectioninthedatabuffer,andeachcolorfulpixelshouldgetdatafromeachofthethreesections.Weneedasimplearrayrepresentationofthedata,andthemostappropriatetextureformatforthispurposeisapuremonochromeformat,whichiscalledLUMINANCEonlytexture.DataarrangementinthedifferenttextureformatsNowthatwehaveboththebackgroundimageandthevideoframesastextures,it’stimetoexaminehowtheGPUwillprocessthisdatatocreatethedesiredvideoeffect.FragmentshaderWekepttheimplementationoftheshaderasclosetotheoriginalJavaScriptsolutionaspossibletokeepitunderstandable.ThemostinterestingpartistheYUVtoRGBAconversion.ThefollowingimageillustrateshowashaderprogramprocessestheYUVformatdata.WecanseethatinspiteoftheRGBAformat,technically3differentimagescontainalltheinformation,andeachpixelreceivesluminanceandcolorinformationfromallthreesub-images.BecausetheUandVchannels’heightandwidthresolutionsarehalved,theevenandoddrowswillcontinuouslyappearinthebufferwhenitissampledwiththefullresolution.Wehavetolivewiththissmalldrawback,becausewehavetrickedtheWebGLsubsystemto“think”weprovidedamonochrometexturewiththefullresolution,butinrealitywesentafullresolutionmonochromechannel(Y)andtwohalvedresolutioncolorchannels(UandV)inthesametexturebuffer.Intheshaderprogram,thefirstfourdefinitionsareusedtosearchfortheYUVchanneldata.Therestshouldbeprettystraightforward,sinceit’sreallyanalogoustotheJavaScriptversion.VideostreamtransformerWearenowatthelaststeptogetaworkingtest.Weneedtomodifythetransformerfunctionfromourpreviousversionssothattheapplicationuploadstheframesastextures,thengetstheresultsbackbeforecreatingtheoutgoingvideoframe.FirstattemptWearegoingtoreadtherenderedpixelsasaframe-bufferwiththe“readPixels”function.Theapplicationworks,buttheperformanceisfarundertheexpectedlevel.EvenonahighperformancePC,theframeprocessingtimeisaround10ms.Afterinspectingtheperformancemetrics,wequicklyfindthatthe“readPixels”functioneatsuptheresources!Wecandobetter!SecondattemptBycarefullycheckingtheVideoFrameconstructor’sdocumentation,wefindthatitcanacceptcanvaselementsaswell,soalsotrytheversionbelow.Thefollowingchangesareneeded:🎉Hurrah!🎉Thespeedhasincreasedalot!Now,theframesareprocessedin1msonthesamePC.Excellent!!!BenchmarksTocomparethedifferenttechnologies,Ihavemeasuredtheframeprocessingtimeondifferentdeviceswithalltheoptionsdiscussedinmyseriesofarticles.AllofthedevicesrunChromev97.TheresolutionwassettoHD(1280*720)FrameprocessingtimeAt30frame/sec,theavailabletimebetweenframesis33ms.Inthespreadsheetabove,theredvaluesindicatesthecaseswhentheframeratedroppedduetoextendedprocessingtime.Itsobvious,thattheGPUprocessingisfarmoreperformantthananyothersolution,andiscapableofreal-timeimageprocessingonslower(mobile)devicesaswell.AnotherinterestingresultistheperformanceofWebAssembly,whichseemstoberelativelyfasteronslowerdevicescomparedtoJavaScript.AppendixUsefullinksSomeofmyfavoriteWebGLresources:https://webgl2fundamentals.org/https://thebookofshaders.com/WebGLinitializationscriptYoumayfindthedetailsinteresting…MorefromDoclerEngineeringGetinsightsfromDoclerEngineeringteamsonhowwearesolvingdifferentchallengesinlivestreaming,softwarearchitecture,infrastructureandtechnicalmanagement.ReadmorefromDoclerEngineeringRecommendedfromMediumfloridaelderlycareorganiz//platform.twitter.com/widgets.js fromTwitterhttps://twitter.com/preferredpcareJustinShermanReactCodingInterviewChallenge23AbhinavGautamReact-RouterQuirksJohannesBauminBetterProgrammingHowToCheckifaPropertyExistsonaJavaScriptObjectfloridaelderlycareorganizThepositive#businessimpactof#mindfulnessatworkhttps://t.co/GiUQ8oE9GJby@MindfulnessNCLGabrielDinizBuildingaJavascriptScrollingCarouselAaronHedquistX-RayGlowShaderEbonyHargroObjectOrientedProgrammingw/JavaScript|“this”,“new”&constructorsAboutHelpTermsPrivacyGettheMediumappGetstartedSzabolcsDamján21FollowersFollowMorefromMediumJanHalamainSuperfaceReliableIPGeolocationwithSuperfaceDavidLueckeinTheFeathersFlightpathWhat’snextforFeathersin2022?michaelsorenseninITNEXTHowtoavoidNPMsupplychainattacks.MiroslavBodečekinDappetizerIntroducingDappetizer:anIndexingFrameworkforTezosHelpStatusWritersBlogCareersPrivacyTermsAboutKnowable
延伸文章資訊
- 1动画纹理- Web API 接口参考 - MDN
第一步是创建 <video> 将用于检索视频帧的元素: ... 检查这两个事件可确保有可用数据,并且可以安全地开始将视频上传到WebGL 纹理。在上面的代码中, ...
- 2Adding Video to Unity WebGL - SIMMER.io
How do I get video to work for Unity WebGL? This is one of my number one questions and I finally ...
- 3HTML5 Video WebGL Test - krpano.com
This is a very simple test-case / example for using a HTML5 video as WebGL texture. This example ...
- 4Animating textures in WebGL - Web APIs - MDN
In this demonstration, we build upon the previous example by replacing our static textures with t...
- 5Real-time WebGL video manipulation | by Szabolcs Damján
This is part III of the “Manipulating video in a browser” series. In the previous articles, we ex...