vp9_encoder.c 275 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824
  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <limits.h>
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include "./vp9_rtcd.h"
  15. #include "./vpx_config.h"
  16. #include "./vpx_dsp_rtcd.h"
  17. #include "./vpx_scale_rtcd.h"
  18. #include "vpx_dsp/psnr.h"
  19. #include "vpx_dsp/vpx_dsp_common.h"
  20. #include "vpx_dsp/vpx_filter.h"
  21. #if CONFIG_INTERNAL_STATS
  22. #include "vpx_dsp/ssim.h"
  23. #endif
  24. #include "vpx_ports/mem.h"
  25. #include "vpx_ports/system_state.h"
  26. #include "vpx_ports/vpx_timer.h"
  27. #if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
  28. #include "vpx_util/vpx_debug_util.h"
  29. #endif // CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
  30. #include "vp9/common/vp9_alloccommon.h"
  31. #include "vp9/common/vp9_filter.h"
  32. #include "vp9/common/vp9_idct.h"
  33. #if CONFIG_NON_GREEDY_MV
  34. #include "vp9/common/vp9_mvref_common.h"
  35. #endif
  36. #if CONFIG_VP9_POSTPROC
  37. #include "vp9/common/vp9_postproc.h"
  38. #endif
  39. #include "vp9/common/vp9_reconinter.h"
  40. #include "vp9/common/vp9_reconintra.h"
  41. #include "vp9/common/vp9_tile_common.h"
  42. #include "vp9/common/vp9_scan.h"
  43. #if !CONFIG_REALTIME_ONLY
  44. #include "vp9/encoder/vp9_alt_ref_aq.h"
  45. #include "vp9/encoder/vp9_aq_360.h"
  46. #include "vp9/encoder/vp9_aq_complexity.h"
  47. #endif
  48. #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
  49. #if !CONFIG_REALTIME_ONLY
  50. #include "vp9/encoder/vp9_aq_variance.h"
  51. #endif
  52. #include "vp9/encoder/vp9_bitstream.h"
  53. #if CONFIG_INTERNAL_STATS
  54. #include "vp9/encoder/vp9_blockiness.h"
  55. #endif
  56. #include "vp9/encoder/vp9_context_tree.h"
  57. #include "vp9/encoder/vp9_encodeframe.h"
  58. #include "vp9/encoder/vp9_encodemb.h"
  59. #include "vp9/encoder/vp9_encodemv.h"
  60. #include "vp9/encoder/vp9_encoder.h"
  61. #include "vp9/encoder/vp9_ethread.h"
  62. #include "vp9/encoder/vp9_extend.h"
  63. #include "vp9/encoder/vp9_firstpass.h"
  64. #include "vp9/encoder/vp9_mbgraph.h"
  65. #if CONFIG_NON_GREEDY_MV
  66. #include "vp9/encoder/vp9_mcomp.h"
  67. #endif
  68. #include "vp9/encoder/vp9_multi_thread.h"
  69. #include "vp9/encoder/vp9_noise_estimate.h"
  70. #include "vp9/encoder/vp9_picklpf.h"
  71. #include "vp9/encoder/vp9_ratectrl.h"
  72. #include "vp9/encoder/vp9_rd.h"
  73. #include "vp9/encoder/vp9_resize.h"
  74. #include "vp9/encoder/vp9_segmentation.h"
  75. #include "vp9/encoder/vp9_skin_detection.h"
  76. #include "vp9/encoder/vp9_speed_features.h"
  77. #include "vp9/encoder/vp9_svc_layercontext.h"
  78. #include "vp9/encoder/vp9_temporal_filter.h"
  79. #define AM_SEGMENT_ID_INACTIVE 7
  80. #define AM_SEGMENT_ID_ACTIVE 0
  81. // Whether to use high precision mv for altref computation.
  82. #define ALTREF_HIGH_PRECISION_MV 1
  83. // Q threshold for high precision mv. Choose a very high value for now so that
  84. // HIGH_PRECISION is always chosen.
  85. #define HIGH_PRECISION_MV_QTHRESH 200
  86. #define FRAME_SIZE_FACTOR 128 // empirical params for context model threshold
  87. #define FRAME_RATE_FACTOR 8
  88. #ifdef OUTPUT_YUV_DENOISED
  89. FILE *yuv_denoised_file = NULL;
  90. #endif
  91. #ifdef OUTPUT_YUV_SKINMAP
  92. static FILE *yuv_skinmap_file = NULL;
  93. #endif
  94. #ifdef OUTPUT_YUV_REC
  95. FILE *yuv_rec_file;
  96. #endif
  97. #ifdef OUTPUT_YUV_SVC_SRC
  98. FILE *yuv_svc_src[3] = { NULL, NULL, NULL };
  99. #endif
  100. #if 0
  101. FILE *framepsnr;
  102. FILE *kf_list;
  103. FILE *keyfile;
  104. #endif
  105. #ifdef ENABLE_KF_DENOISE
  106. // Test condition for spatial denoise of source.
  107. static int is_spatial_denoise_enabled(VP9_COMP *cpi) {
  108. VP9_COMMON *const cm = &cpi->common;
  109. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  110. return (oxcf->pass != 1) && !is_lossless_requested(&cpi->oxcf) &&
  111. frame_is_intra_only(cm);
  112. }
  113. #endif
  114. #if CONFIG_VP9_HIGHBITDEPTH
  115. void highbd_wht_fwd_txfm(int16_t *src_diff, int bw, tran_low_t *coeff,
  116. TX_SIZE tx_size);
  117. #endif
  118. void wht_fwd_txfm(int16_t *src_diff, int bw, tran_low_t *coeff,
  119. TX_SIZE tx_size);
  120. #if !CONFIG_REALTIME_ONLY
  121. // compute adaptive threshold for skip recoding
  122. static int compute_context_model_thresh(const VP9_COMP *const cpi) {
  123. const VP9_COMMON *const cm = &cpi->common;
  124. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  125. const int frame_size = (cm->width * cm->height) >> 10;
  126. const int bitrate = (int)(oxcf->target_bandwidth >> 10);
  127. const int qindex_factor = cm->base_qindex + (MAXQ >> 1);
  128. // This equation makes the threshold adaptive to frame size.
  129. // Coding gain obtained by recoding comes from alternate frames of large
  130. // content change. We skip recoding if the difference of previous and current
  131. // frame context probability model is less than a certain threshold.
  132. // The first component is the most critical part to guarantee adaptivity.
  133. // Other parameters are estimated based on normal setting of hd resolution
  134. // parameters. e.g frame_size = 1920x1080, bitrate = 8000, qindex_factor < 50
  135. const int thresh =
  136. ((FRAME_SIZE_FACTOR * frame_size - FRAME_RATE_FACTOR * bitrate) *
  137. qindex_factor) >>
  138. 9;
  139. return thresh;
  140. }
  141. // compute the total cost difference between current
  142. // and previous frame context prob model.
  143. static int compute_context_model_diff(const VP9_COMMON *const cm) {
  144. const FRAME_CONTEXT *const pre_fc =
  145. &cm->frame_contexts[cm->frame_context_idx];
  146. const FRAME_CONTEXT *const cur_fc = cm->fc;
  147. const FRAME_COUNTS *counts = &cm->counts;
  148. vpx_prob pre_last_prob, cur_last_prob;
  149. int diff = 0;
  150. int i, j, k, l, m, n;
  151. // y_mode_prob
  152. for (i = 0; i < BLOCK_SIZE_GROUPS; ++i) {
  153. for (j = 0; j < INTRA_MODES - 1; ++j) {
  154. diff += (int)counts->y_mode[i][j] *
  155. (pre_fc->y_mode_prob[i][j] - cur_fc->y_mode_prob[i][j]);
  156. }
  157. pre_last_prob = MAX_PROB - pre_fc->y_mode_prob[i][INTRA_MODES - 2];
  158. cur_last_prob = MAX_PROB - cur_fc->y_mode_prob[i][INTRA_MODES - 2];
  159. diff += (int)counts->y_mode[i][INTRA_MODES - 1] *
  160. (pre_last_prob - cur_last_prob);
  161. }
  162. // uv_mode_prob
  163. for (i = 0; i < INTRA_MODES; ++i) {
  164. for (j = 0; j < INTRA_MODES - 1; ++j) {
  165. diff += (int)counts->uv_mode[i][j] *
  166. (pre_fc->uv_mode_prob[i][j] - cur_fc->uv_mode_prob[i][j]);
  167. }
  168. pre_last_prob = MAX_PROB - pre_fc->uv_mode_prob[i][INTRA_MODES - 2];
  169. cur_last_prob = MAX_PROB - cur_fc->uv_mode_prob[i][INTRA_MODES - 2];
  170. diff += (int)counts->uv_mode[i][INTRA_MODES - 1] *
  171. (pre_last_prob - cur_last_prob);
  172. }
  173. // partition_prob
  174. for (i = 0; i < PARTITION_CONTEXTS; ++i) {
  175. for (j = 0; j < PARTITION_TYPES - 1; ++j) {
  176. diff += (int)counts->partition[i][j] *
  177. (pre_fc->partition_prob[i][j] - cur_fc->partition_prob[i][j]);
  178. }
  179. pre_last_prob = MAX_PROB - pre_fc->partition_prob[i][PARTITION_TYPES - 2];
  180. cur_last_prob = MAX_PROB - cur_fc->partition_prob[i][PARTITION_TYPES - 2];
  181. diff += (int)counts->partition[i][PARTITION_TYPES - 1] *
  182. (pre_last_prob - cur_last_prob);
  183. }
  184. // coef_probs
  185. for (i = 0; i < TX_SIZES; ++i) {
  186. for (j = 0; j < PLANE_TYPES; ++j) {
  187. for (k = 0; k < REF_TYPES; ++k) {
  188. for (l = 0; l < COEF_BANDS; ++l) {
  189. for (m = 0; m < BAND_COEFF_CONTEXTS(l); ++m) {
  190. for (n = 0; n < UNCONSTRAINED_NODES; ++n) {
  191. diff += (int)counts->coef[i][j][k][l][m][n] *
  192. (pre_fc->coef_probs[i][j][k][l][m][n] -
  193. cur_fc->coef_probs[i][j][k][l][m][n]);
  194. }
  195. pre_last_prob =
  196. MAX_PROB -
  197. pre_fc->coef_probs[i][j][k][l][m][UNCONSTRAINED_NODES - 1];
  198. cur_last_prob =
  199. MAX_PROB -
  200. cur_fc->coef_probs[i][j][k][l][m][UNCONSTRAINED_NODES - 1];
  201. diff += (int)counts->coef[i][j][k][l][m][UNCONSTRAINED_NODES] *
  202. (pre_last_prob - cur_last_prob);
  203. }
  204. }
  205. }
  206. }
  207. }
  208. // switchable_interp_prob
  209. for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i) {
  210. for (j = 0; j < SWITCHABLE_FILTERS - 1; ++j) {
  211. diff += (int)counts->switchable_interp[i][j] *
  212. (pre_fc->switchable_interp_prob[i][j] -
  213. cur_fc->switchable_interp_prob[i][j]);
  214. }
  215. pre_last_prob =
  216. MAX_PROB - pre_fc->switchable_interp_prob[i][SWITCHABLE_FILTERS - 2];
  217. cur_last_prob =
  218. MAX_PROB - cur_fc->switchable_interp_prob[i][SWITCHABLE_FILTERS - 2];
  219. diff += (int)counts->switchable_interp[i][SWITCHABLE_FILTERS - 1] *
  220. (pre_last_prob - cur_last_prob);
  221. }
  222. // inter_mode_probs
  223. for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
  224. for (j = 0; j < INTER_MODES - 1; ++j) {
  225. diff += (int)counts->inter_mode[i][j] *
  226. (pre_fc->inter_mode_probs[i][j] - cur_fc->inter_mode_probs[i][j]);
  227. }
  228. pre_last_prob = MAX_PROB - pre_fc->inter_mode_probs[i][INTER_MODES - 2];
  229. cur_last_prob = MAX_PROB - cur_fc->inter_mode_probs[i][INTER_MODES - 2];
  230. diff += (int)counts->inter_mode[i][INTER_MODES - 1] *
  231. (pre_last_prob - cur_last_prob);
  232. }
  233. // intra_inter_prob
  234. for (i = 0; i < INTRA_INTER_CONTEXTS; ++i) {
  235. diff += (int)counts->intra_inter[i][0] *
  236. (pre_fc->intra_inter_prob[i] - cur_fc->intra_inter_prob[i]);
  237. pre_last_prob = MAX_PROB - pre_fc->intra_inter_prob[i];
  238. cur_last_prob = MAX_PROB - cur_fc->intra_inter_prob[i];
  239. diff += (int)counts->intra_inter[i][1] * (pre_last_prob - cur_last_prob);
  240. }
  241. // comp_inter_prob
  242. for (i = 0; i < COMP_INTER_CONTEXTS; ++i) {
  243. diff += (int)counts->comp_inter[i][0] *
  244. (pre_fc->comp_inter_prob[i] - cur_fc->comp_inter_prob[i]);
  245. pre_last_prob = MAX_PROB - pre_fc->comp_inter_prob[i];
  246. cur_last_prob = MAX_PROB - cur_fc->comp_inter_prob[i];
  247. diff += (int)counts->comp_inter[i][1] * (pre_last_prob - cur_last_prob);
  248. }
  249. // single_ref_prob
  250. for (i = 0; i < REF_CONTEXTS; ++i) {
  251. for (j = 0; j < 2; ++j) {
  252. diff += (int)counts->single_ref[i][j][0] *
  253. (pre_fc->single_ref_prob[i][j] - cur_fc->single_ref_prob[i][j]);
  254. pre_last_prob = MAX_PROB - pre_fc->single_ref_prob[i][j];
  255. cur_last_prob = MAX_PROB - cur_fc->single_ref_prob[i][j];
  256. diff +=
  257. (int)counts->single_ref[i][j][1] * (pre_last_prob - cur_last_prob);
  258. }
  259. }
  260. // comp_ref_prob
  261. for (i = 0; i < REF_CONTEXTS; ++i) {
  262. diff += (int)counts->comp_ref[i][0] *
  263. (pre_fc->comp_ref_prob[i] - cur_fc->comp_ref_prob[i]);
  264. pre_last_prob = MAX_PROB - pre_fc->comp_ref_prob[i];
  265. cur_last_prob = MAX_PROB - cur_fc->comp_ref_prob[i];
  266. diff += (int)counts->comp_ref[i][1] * (pre_last_prob - cur_last_prob);
  267. }
  268. // tx_probs
  269. for (i = 0; i < TX_SIZE_CONTEXTS; ++i) {
  270. // p32x32
  271. for (j = 0; j < TX_SIZES - 1; ++j) {
  272. diff += (int)counts->tx.p32x32[i][j] *
  273. (pre_fc->tx_probs.p32x32[i][j] - cur_fc->tx_probs.p32x32[i][j]);
  274. }
  275. pre_last_prob = MAX_PROB - pre_fc->tx_probs.p32x32[i][TX_SIZES - 2];
  276. cur_last_prob = MAX_PROB - cur_fc->tx_probs.p32x32[i][TX_SIZES - 2];
  277. diff += (int)counts->tx.p32x32[i][TX_SIZES - 1] *
  278. (pre_last_prob - cur_last_prob);
  279. // p16x16
  280. for (j = 0; j < TX_SIZES - 2; ++j) {
  281. diff += (int)counts->tx.p16x16[i][j] *
  282. (pre_fc->tx_probs.p16x16[i][j] - cur_fc->tx_probs.p16x16[i][j]);
  283. }
  284. pre_last_prob = MAX_PROB - pre_fc->tx_probs.p16x16[i][TX_SIZES - 3];
  285. cur_last_prob = MAX_PROB - cur_fc->tx_probs.p16x16[i][TX_SIZES - 3];
  286. diff += (int)counts->tx.p16x16[i][TX_SIZES - 2] *
  287. (pre_last_prob - cur_last_prob);
  288. // p8x8
  289. for (j = 0; j < TX_SIZES - 3; ++j) {
  290. diff += (int)counts->tx.p8x8[i][j] *
  291. (pre_fc->tx_probs.p8x8[i][j] - cur_fc->tx_probs.p8x8[i][j]);
  292. }
  293. pre_last_prob = MAX_PROB - pre_fc->tx_probs.p8x8[i][TX_SIZES - 4];
  294. cur_last_prob = MAX_PROB - cur_fc->tx_probs.p8x8[i][TX_SIZES - 4];
  295. diff +=
  296. (int)counts->tx.p8x8[i][TX_SIZES - 3] * (pre_last_prob - cur_last_prob);
  297. }
  298. // skip_probs
  299. for (i = 0; i < SKIP_CONTEXTS; ++i) {
  300. diff += (int)counts->skip[i][0] *
  301. (pre_fc->skip_probs[i] - cur_fc->skip_probs[i]);
  302. pre_last_prob = MAX_PROB - pre_fc->skip_probs[i];
  303. cur_last_prob = MAX_PROB - cur_fc->skip_probs[i];
  304. diff += (int)counts->skip[i][1] * (pre_last_prob - cur_last_prob);
  305. }
  306. // mv
  307. for (i = 0; i < MV_JOINTS - 1; ++i) {
  308. diff += (int)counts->mv.joints[i] *
  309. (pre_fc->nmvc.joints[i] - cur_fc->nmvc.joints[i]);
  310. }
  311. pre_last_prob = MAX_PROB - pre_fc->nmvc.joints[MV_JOINTS - 2];
  312. cur_last_prob = MAX_PROB - cur_fc->nmvc.joints[MV_JOINTS - 2];
  313. diff +=
  314. (int)counts->mv.joints[MV_JOINTS - 1] * (pre_last_prob - cur_last_prob);
  315. for (i = 0; i < 2; ++i) {
  316. const nmv_component_counts *nmv_count = &counts->mv.comps[i];
  317. const nmv_component *pre_nmv_prob = &pre_fc->nmvc.comps[i];
  318. const nmv_component *cur_nmv_prob = &cur_fc->nmvc.comps[i];
  319. // sign
  320. diff += (int)nmv_count->sign[0] * (pre_nmv_prob->sign - cur_nmv_prob->sign);
  321. pre_last_prob = MAX_PROB - pre_nmv_prob->sign;
  322. cur_last_prob = MAX_PROB - cur_nmv_prob->sign;
  323. diff += (int)nmv_count->sign[1] * (pre_last_prob - cur_last_prob);
  324. // classes
  325. for (j = 0; j < MV_CLASSES - 1; ++j) {
  326. diff += (int)nmv_count->classes[j] *
  327. (pre_nmv_prob->classes[j] - cur_nmv_prob->classes[j]);
  328. }
  329. pre_last_prob = MAX_PROB - pre_nmv_prob->classes[MV_CLASSES - 2];
  330. cur_last_prob = MAX_PROB - cur_nmv_prob->classes[MV_CLASSES - 2];
  331. diff += (int)nmv_count->classes[MV_CLASSES - 1] *
  332. (pre_last_prob - cur_last_prob);
  333. // class0
  334. for (j = 0; j < CLASS0_SIZE - 1; ++j) {
  335. diff += (int)nmv_count->class0[j] *
  336. (pre_nmv_prob->class0[j] - cur_nmv_prob->class0[j]);
  337. }
  338. pre_last_prob = MAX_PROB - pre_nmv_prob->class0[CLASS0_SIZE - 2];
  339. cur_last_prob = MAX_PROB - cur_nmv_prob->class0[CLASS0_SIZE - 2];
  340. diff += (int)nmv_count->class0[CLASS0_SIZE - 1] *
  341. (pre_last_prob - cur_last_prob);
  342. // bits
  343. for (j = 0; j < MV_OFFSET_BITS; ++j) {
  344. diff += (int)nmv_count->bits[j][0] *
  345. (pre_nmv_prob->bits[j] - cur_nmv_prob->bits[j]);
  346. pre_last_prob = MAX_PROB - pre_nmv_prob->bits[j];
  347. cur_last_prob = MAX_PROB - cur_nmv_prob->bits[j];
  348. diff += (int)nmv_count->bits[j][1] * (pre_last_prob - cur_last_prob);
  349. }
  350. // class0_fp
  351. for (j = 0; j < CLASS0_SIZE; ++j) {
  352. for (k = 0; k < MV_FP_SIZE - 1; ++k) {
  353. diff += (int)nmv_count->class0_fp[j][k] *
  354. (pre_nmv_prob->class0_fp[j][k] - cur_nmv_prob->class0_fp[j][k]);
  355. }
  356. pre_last_prob = MAX_PROB - pre_nmv_prob->class0_fp[j][MV_FP_SIZE - 2];
  357. cur_last_prob = MAX_PROB - cur_nmv_prob->class0_fp[j][MV_FP_SIZE - 2];
  358. diff += (int)nmv_count->class0_fp[j][MV_FP_SIZE - 1] *
  359. (pre_last_prob - cur_last_prob);
  360. }
  361. // fp
  362. for (j = 0; j < MV_FP_SIZE - 1; ++j) {
  363. diff +=
  364. (int)nmv_count->fp[j] * (pre_nmv_prob->fp[j] - cur_nmv_prob->fp[j]);
  365. }
  366. pre_last_prob = MAX_PROB - pre_nmv_prob->fp[MV_FP_SIZE - 2];
  367. cur_last_prob = MAX_PROB - cur_nmv_prob->fp[MV_FP_SIZE - 2];
  368. diff +=
  369. (int)nmv_count->fp[MV_FP_SIZE - 1] * (pre_last_prob - cur_last_prob);
  370. // class0_hp
  371. diff += (int)nmv_count->class0_hp[0] *
  372. (pre_nmv_prob->class0_hp - cur_nmv_prob->class0_hp);
  373. pre_last_prob = MAX_PROB - pre_nmv_prob->class0_hp;
  374. cur_last_prob = MAX_PROB - cur_nmv_prob->class0_hp;
  375. diff += (int)nmv_count->class0_hp[1] * (pre_last_prob - cur_last_prob);
  376. // hp
  377. diff += (int)nmv_count->hp[0] * (pre_nmv_prob->hp - cur_nmv_prob->hp);
  378. pre_last_prob = MAX_PROB - pre_nmv_prob->hp;
  379. cur_last_prob = MAX_PROB - cur_nmv_prob->hp;
  380. diff += (int)nmv_count->hp[1] * (pre_last_prob - cur_last_prob);
  381. }
  382. return -diff;
  383. }
  384. #endif // !CONFIG_REALTIME_ONLY
  385. // Test for whether to calculate metrics for the frame.
  386. static int is_psnr_calc_enabled(VP9_COMP *cpi) {
  387. VP9_COMMON *const cm = &cpi->common;
  388. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  389. return cpi->b_calculate_psnr && (oxcf->pass != 1) && cm->show_frame;
  390. }
  391. /* clang-format off */
  392. const Vp9LevelSpec vp9_level_defs[VP9_LEVELS] = {
  393. // sample rate size breadth bitrate cpb
  394. { LEVEL_1, 829440, 36864, 512, 200, 400, 2, 1, 4, 8 },
  395. { LEVEL_1_1, 2764800, 73728, 768, 800, 1000, 2, 1, 4, 8 },
  396. { LEVEL_2, 4608000, 122880, 960, 1800, 1500, 2, 1, 4, 8 },
  397. { LEVEL_2_1, 9216000, 245760, 1344, 3600, 2800, 2, 2, 4, 8 },
  398. { LEVEL_3, 20736000, 552960, 2048, 7200, 6000, 2, 4, 4, 8 },
  399. { LEVEL_3_1, 36864000, 983040, 2752, 12000, 10000, 2, 4, 4, 8 },
  400. { LEVEL_4, 83558400, 2228224, 4160, 18000, 16000, 4, 4, 4, 8 },
  401. { LEVEL_4_1, 160432128, 2228224, 4160, 30000, 18000, 4, 4, 5, 6 },
  402. { LEVEL_5, 311951360, 8912896, 8384, 60000, 36000, 6, 8, 6, 4 },
  403. { LEVEL_5_1, 588251136, 8912896, 8384, 120000, 46000, 8, 8, 10, 4 },
  404. // TODO(huisu): update max_cpb_size for level 5_2 ~ 6_2 when
  405. // they are finalized (currently tentative).
  406. { LEVEL_5_2, 1176502272, 8912896, 8384, 180000, 90000, 8, 8, 10, 4 },
  407. { LEVEL_6, 1176502272, 35651584, 16832, 180000, 90000, 8, 16, 10, 4 },
  408. { LEVEL_6_1, 2353004544u, 35651584, 16832, 240000, 180000, 8, 16, 10, 4 },
  409. { LEVEL_6_2, 4706009088u, 35651584, 16832, 480000, 360000, 8, 16, 10, 4 },
  410. };
  411. /* clang-format on */
  412. static const char *level_fail_messages[TARGET_LEVEL_FAIL_IDS] = {
  413. "The average bit-rate is too high.",
  414. "The picture size is too large.",
  415. "The picture width/height is too large.",
  416. "The luma sample rate is too large.",
  417. "The CPB size is too large.",
  418. "The compression ratio is too small",
  419. "Too many column tiles are used.",
  420. "The alt-ref distance is too small.",
  421. "Too many reference buffers are used."
  422. };
  423. static INLINE void Scale2Ratio(VPX_SCALING mode, int *hr, int *hs) {
  424. switch (mode) {
  425. case NORMAL:
  426. *hr = 1;
  427. *hs = 1;
  428. break;
  429. case FOURFIVE:
  430. *hr = 4;
  431. *hs = 5;
  432. break;
  433. case THREEFIVE:
  434. *hr = 3;
  435. *hs = 5;
  436. break;
  437. default:
  438. assert(mode == ONETWO);
  439. *hr = 1;
  440. *hs = 2;
  441. break;
  442. }
  443. }
  444. // Mark all inactive blocks as active. Other segmentation features may be set
  445. // so memset cannot be used, instead only inactive blocks should be reset.
  446. static void suppress_active_map(VP9_COMP *cpi) {
  447. unsigned char *const seg_map = cpi->segmentation_map;
  448. if (cpi->active_map.enabled || cpi->active_map.update) {
  449. const int rows = cpi->common.mi_rows;
  450. const int cols = cpi->common.mi_cols;
  451. int i;
  452. for (i = 0; i < rows * cols; ++i)
  453. if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
  454. seg_map[i] = AM_SEGMENT_ID_ACTIVE;
  455. }
  456. }
  457. static void apply_active_map(VP9_COMP *cpi) {
  458. struct segmentation *const seg = &cpi->common.seg;
  459. unsigned char *const seg_map = cpi->segmentation_map;
  460. const unsigned char *const active_map = cpi->active_map.map;
  461. int i;
  462. assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
  463. if (frame_is_intra_only(&cpi->common)) {
  464. cpi->active_map.enabled = 0;
  465. cpi->active_map.update = 1;
  466. }
  467. if (cpi->active_map.update) {
  468. if (cpi->active_map.enabled) {
  469. for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
  470. if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
  471. vp9_enable_segmentation(seg);
  472. vp9_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
  473. vp9_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
  474. // Setting the data to -MAX_LOOP_FILTER will result in the computed loop
  475. // filter level being zero regardless of the value of seg->abs_delta.
  476. vp9_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF,
  477. -MAX_LOOP_FILTER);
  478. } else {
  479. vp9_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
  480. vp9_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
  481. if (seg->enabled) {
  482. seg->update_data = 1;
  483. seg->update_map = 1;
  484. }
  485. }
  486. cpi->active_map.update = 0;
  487. }
  488. }
  489. static void apply_roi_map(VP9_COMP *cpi) {
  490. VP9_COMMON *cm = &cpi->common;
  491. struct segmentation *const seg = &cm->seg;
  492. vpx_roi_map_t *roi = &cpi->roi;
  493. const int *delta_q = roi->delta_q;
  494. const int *delta_lf = roi->delta_lf;
  495. const int *skip = roi->skip;
  496. int ref_frame[8];
  497. int internal_delta_q[MAX_SEGMENTS];
  498. int i;
  499. static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
  500. VP9_ALT_FLAG };
  501. // TODO(jianj): Investigate why ROI not working in speed < 5 or in non
  502. // realtime mode.
  503. if (cpi->oxcf.mode != REALTIME || cpi->oxcf.speed < 5) return;
  504. if (!roi->enabled) return;
  505. memcpy(&ref_frame, roi->ref_frame, sizeof(ref_frame));
  506. vp9_enable_segmentation(seg);
  507. vp9_clearall_segfeatures(seg);
  508. // Select delta coding method;
  509. seg->abs_delta = SEGMENT_DELTADATA;
  510. memcpy(cpi->segmentation_map, roi->roi_map, (cm->mi_rows * cm->mi_cols));
  511. for (i = 0; i < MAX_SEGMENTS; ++i) {
  512. // Translate the external delta q values to internal values.
  513. internal_delta_q[i] = vp9_quantizer_to_qindex(abs(delta_q[i]));
  514. if (delta_q[i] < 0) internal_delta_q[i] = -internal_delta_q[i];
  515. vp9_disable_segfeature(seg, i, SEG_LVL_ALT_Q);
  516. vp9_disable_segfeature(seg, i, SEG_LVL_ALT_LF);
  517. if (internal_delta_q[i] != 0) {
  518. vp9_enable_segfeature(seg, i, SEG_LVL_ALT_Q);
  519. vp9_set_segdata(seg, i, SEG_LVL_ALT_Q, internal_delta_q[i]);
  520. }
  521. if (delta_lf[i] != 0) {
  522. vp9_enable_segfeature(seg, i, SEG_LVL_ALT_LF);
  523. vp9_set_segdata(seg, i, SEG_LVL_ALT_LF, delta_lf[i]);
  524. }
  525. if (skip[i] != 0) {
  526. vp9_enable_segfeature(seg, i, SEG_LVL_SKIP);
  527. vp9_set_segdata(seg, i, SEG_LVL_SKIP, skip[i]);
  528. }
  529. if (ref_frame[i] >= 0) {
  530. int valid_ref = 1;
  531. // ALTREF is not used as reference for nonrd_pickmode with 0 lag.
  532. if (ref_frame[i] == ALTREF_FRAME && cpi->sf.use_nonrd_pick_mode)
  533. valid_ref = 0;
  534. // If GOLDEN is selected, make sure it's set as reference.
  535. if (ref_frame[i] == GOLDEN_FRAME &&
  536. !(cpi->ref_frame_flags & flag_list[ref_frame[i]])) {
  537. valid_ref = 0;
  538. }
  539. // GOLDEN was updated in previous encoded frame, so GOLDEN and LAST are
  540. // same reference.
  541. if (ref_frame[i] == GOLDEN_FRAME && cpi->rc.frames_since_golden == 0)
  542. ref_frame[i] = LAST_FRAME;
  543. if (valid_ref) {
  544. vp9_enable_segfeature(seg, i, SEG_LVL_REF_FRAME);
  545. vp9_set_segdata(seg, i, SEG_LVL_REF_FRAME, ref_frame[i]);
  546. }
  547. }
  548. }
  549. roi->enabled = 1;
  550. }
  551. static void init_level_info(Vp9LevelInfo *level_info) {
  552. Vp9LevelStats *const level_stats = &level_info->level_stats;
  553. Vp9LevelSpec *const level_spec = &level_info->level_spec;
  554. memset(level_stats, 0, sizeof(*level_stats));
  555. memset(level_spec, 0, sizeof(*level_spec));
  556. level_spec->level = LEVEL_UNKNOWN;
  557. level_spec->min_altref_distance = INT_MAX;
  558. }
  559. static int check_seg_range(int seg_data[8], int range) {
  560. return !(abs(seg_data[0]) > range || abs(seg_data[1]) > range ||
  561. abs(seg_data[2]) > range || abs(seg_data[3]) > range ||
  562. abs(seg_data[4]) > range || abs(seg_data[5]) > range ||
  563. abs(seg_data[6]) > range || abs(seg_data[7]) > range);
  564. }
  565. VP9_LEVEL vp9_get_level(const Vp9LevelSpec *const level_spec) {
  566. int i;
  567. const Vp9LevelSpec *this_level;
  568. vpx_clear_system_state();
  569. for (i = 0; i < VP9_LEVELS; ++i) {
  570. this_level = &vp9_level_defs[i];
  571. if ((double)level_spec->max_luma_sample_rate >
  572. (double)this_level->max_luma_sample_rate *
  573. (1 + SAMPLE_RATE_GRACE_P) ||
  574. level_spec->max_luma_picture_size > this_level->max_luma_picture_size ||
  575. level_spec->max_luma_picture_breadth >
  576. this_level->max_luma_picture_breadth ||
  577. level_spec->average_bitrate > this_level->average_bitrate ||
  578. level_spec->max_cpb_size > this_level->max_cpb_size ||
  579. level_spec->compression_ratio < this_level->compression_ratio ||
  580. level_spec->max_col_tiles > this_level->max_col_tiles ||
  581. level_spec->min_altref_distance < this_level->min_altref_distance ||
  582. level_spec->max_ref_frame_buffers > this_level->max_ref_frame_buffers)
  583. continue;
  584. break;
  585. }
  586. return (i == VP9_LEVELS) ? LEVEL_UNKNOWN : vp9_level_defs[i].level;
  587. }
  588. int vp9_set_roi_map(VP9_COMP *cpi, unsigned char *map, unsigned int rows,
  589. unsigned int cols, int delta_q[8], int delta_lf[8],
  590. int skip[8], int ref_frame[8]) {
  591. VP9_COMMON *cm = &cpi->common;
  592. vpx_roi_map_t *roi = &cpi->roi;
  593. const int range = 63;
  594. const int ref_frame_range = 3; // Alt-ref
  595. const int skip_range = 1;
  596. const int frame_rows = cpi->common.mi_rows;
  597. const int frame_cols = cpi->common.mi_cols;
  598. // Check number of rows and columns match
  599. if (frame_rows != (int)rows || frame_cols != (int)cols) {
  600. return -1;
  601. }
  602. if (!check_seg_range(delta_q, range) || !check_seg_range(delta_lf, range) ||
  603. !check_seg_range(ref_frame, ref_frame_range) ||
  604. !check_seg_range(skip, skip_range))
  605. return -1;
  606. // Also disable segmentation if no deltas are specified.
  607. if (!map ||
  608. (!(delta_q[0] | delta_q[1] | delta_q[2] | delta_q[3] | delta_q[4] |
  609. delta_q[5] | delta_q[6] | delta_q[7] | delta_lf[0] | delta_lf[1] |
  610. delta_lf[2] | delta_lf[3] | delta_lf[4] | delta_lf[5] | delta_lf[6] |
  611. delta_lf[7] | skip[0] | skip[1] | skip[2] | skip[3] | skip[4] |
  612. skip[5] | skip[6] | skip[7]) &&
  613. (ref_frame[0] == -1 && ref_frame[1] == -1 && ref_frame[2] == -1 &&
  614. ref_frame[3] == -1 && ref_frame[4] == -1 && ref_frame[5] == -1 &&
  615. ref_frame[6] == -1 && ref_frame[7] == -1))) {
  616. vp9_disable_segmentation(&cm->seg);
  617. cpi->roi.enabled = 0;
  618. return 0;
  619. }
  620. if (roi->roi_map) {
  621. vpx_free(roi->roi_map);
  622. roi->roi_map = NULL;
  623. }
  624. CHECK_MEM_ERROR(cm, roi->roi_map, vpx_malloc(rows * cols));
  625. // Copy to ROI sturcture in the compressor.
  626. memcpy(roi->roi_map, map, rows * cols);
  627. memcpy(&roi->delta_q, delta_q, MAX_SEGMENTS * sizeof(delta_q[0]));
  628. memcpy(&roi->delta_lf, delta_lf, MAX_SEGMENTS * sizeof(delta_lf[0]));
  629. memcpy(&roi->skip, skip, MAX_SEGMENTS * sizeof(skip[0]));
  630. memcpy(&roi->ref_frame, ref_frame, MAX_SEGMENTS * sizeof(ref_frame[0]));
  631. roi->enabled = 1;
  632. roi->rows = rows;
  633. roi->cols = cols;
  634. return 0;
  635. }
  636. int vp9_set_active_map(VP9_COMP *cpi, unsigned char *new_map_16x16, int rows,
  637. int cols) {
  638. if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
  639. unsigned char *const active_map_8x8 = cpi->active_map.map;
  640. const int mi_rows = cpi->common.mi_rows;
  641. const int mi_cols = cpi->common.mi_cols;
  642. cpi->active_map.update = 1;
  643. if (new_map_16x16) {
  644. int r, c;
  645. for (r = 0; r < mi_rows; ++r) {
  646. for (c = 0; c < mi_cols; ++c) {
  647. active_map_8x8[r * mi_cols + c] =
  648. new_map_16x16[(r >> 1) * cols + (c >> 1)]
  649. ? AM_SEGMENT_ID_ACTIVE
  650. : AM_SEGMENT_ID_INACTIVE;
  651. }
  652. }
  653. cpi->active_map.enabled = 1;
  654. } else {
  655. cpi->active_map.enabled = 0;
  656. }
  657. return 0;
  658. } else {
  659. return -1;
  660. }
  661. }
  662. int vp9_get_active_map(VP9_COMP *cpi, unsigned char *new_map_16x16, int rows,
  663. int cols) {
  664. if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
  665. new_map_16x16) {
  666. unsigned char *const seg_map_8x8 = cpi->segmentation_map;
  667. const int mi_rows = cpi->common.mi_rows;
  668. const int mi_cols = cpi->common.mi_cols;
  669. memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
  670. if (cpi->active_map.enabled) {
  671. int r, c;
  672. for (r = 0; r < mi_rows; ++r) {
  673. for (c = 0; c < mi_cols; ++c) {
  674. // Cyclic refresh segments are considered active despite not having
  675. // AM_SEGMENT_ID_ACTIVE
  676. new_map_16x16[(r >> 1) * cols + (c >> 1)] |=
  677. seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
  678. }
  679. }
  680. }
  681. return 0;
  682. } else {
  683. return -1;
  684. }
  685. }
  686. void vp9_set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
  687. MACROBLOCK *const mb = &cpi->td.mb;
  688. cpi->common.allow_high_precision_mv = allow_high_precision_mv;
  689. if (cpi->common.allow_high_precision_mv) {
  690. mb->mvcost = mb->nmvcost_hp;
  691. mb->mvsadcost = mb->nmvsadcost_hp;
  692. } else {
  693. mb->mvcost = mb->nmvcost;
  694. mb->mvsadcost = mb->nmvsadcost;
  695. }
  696. }
  697. static void setup_frame(VP9_COMP *cpi) {
  698. VP9_COMMON *const cm = &cpi->common;
  699. // Set up entropy context depending on frame type. The decoder mandates
  700. // the use of the default context, index 0, for keyframes and inter
  701. // frames where the error_resilient_mode or intra_only flag is set. For
  702. // other inter-frames the encoder currently uses only two contexts;
  703. // context 1 for ALTREF frames and context 0 for the others.
  704. if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
  705. vp9_setup_past_independence(cm);
  706. } else {
  707. if (!cpi->use_svc) cm->frame_context_idx = cpi->refresh_alt_ref_frame;
  708. }
  709. // TODO(jingning): Overwrite the frame_context_idx index in multi-layer ARF
  710. // case. Need some further investigation on if we could apply this to single
  711. // layer ARF case as well.
  712. if (cpi->multi_layer_arf && !cpi->use_svc) {
  713. GF_GROUP *const gf_group = &cpi->twopass.gf_group;
  714. cm->frame_context_idx = clamp(gf_group->layer_depth[gf_group->index] - 1, 0,
  715. FRAME_CONTEXTS - 1);
  716. }
  717. if (cm->frame_type == KEY_FRAME) {
  718. cpi->refresh_golden_frame = 1;
  719. cpi->refresh_alt_ref_frame = 1;
  720. vp9_zero(cpi->interp_filter_selected);
  721. } else {
  722. *cm->fc = cm->frame_contexts[cm->frame_context_idx];
  723. vp9_zero(cpi->interp_filter_selected[0]);
  724. }
  725. }
  726. static void vp9_enc_setup_mi(VP9_COMMON *cm) {
  727. int i;
  728. cm->mi = cm->mip + cm->mi_stride + 1;
  729. memset(cm->mip, 0, cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mip));
  730. cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
  731. // Clear top border row
  732. memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
  733. // Clear left border column
  734. for (i = 1; i < cm->mi_rows + 1; ++i)
  735. memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
  736. cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
  737. cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
  738. memset(cm->mi_grid_base, 0,
  739. cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mi_grid_base));
  740. }
  741. static int vp9_enc_alloc_mi(VP9_COMMON *cm, int mi_size) {
  742. cm->mip = vpx_calloc(mi_size, sizeof(*cm->mip));
  743. if (!cm->mip) return 1;
  744. cm->prev_mip = vpx_calloc(mi_size, sizeof(*cm->prev_mip));
  745. if (!cm->prev_mip) return 1;
  746. cm->mi_alloc_size = mi_size;
  747. cm->mi_grid_base = (MODE_INFO **)vpx_calloc(mi_size, sizeof(MODE_INFO *));
  748. if (!cm->mi_grid_base) return 1;
  749. cm->prev_mi_grid_base =
  750. (MODE_INFO **)vpx_calloc(mi_size, sizeof(MODE_INFO *));
  751. if (!cm->prev_mi_grid_base) return 1;
  752. return 0;
  753. }
  754. static void vp9_enc_free_mi(VP9_COMMON *cm) {
  755. vpx_free(cm->mip);
  756. cm->mip = NULL;
  757. vpx_free(cm->prev_mip);
  758. cm->prev_mip = NULL;
  759. vpx_free(cm->mi_grid_base);
  760. cm->mi_grid_base = NULL;
  761. vpx_free(cm->prev_mi_grid_base);
  762. cm->prev_mi_grid_base = NULL;
  763. cm->mi_alloc_size = 0;
  764. }
  765. static void vp9_swap_mi_and_prev_mi(VP9_COMMON *cm) {
  766. // Current mip will be the prev_mip for the next frame.
  767. MODE_INFO **temp_base = cm->prev_mi_grid_base;
  768. MODE_INFO *temp = cm->prev_mip;
  769. // Skip update prev_mi frame in show_existing_frame mode.
  770. if (cm->show_existing_frame) return;
  771. cm->prev_mip = cm->mip;
  772. cm->mip = temp;
  773. // Update the upper left visible macroblock ptrs.
  774. cm->mi = cm->mip + cm->mi_stride + 1;
  775. cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
  776. cm->prev_mi_grid_base = cm->mi_grid_base;
  777. cm->mi_grid_base = temp_base;
  778. cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
  779. cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
  780. }
  781. void vp9_initialize_enc(void) {
  782. static volatile int init_done = 0;
  783. if (!init_done) {
  784. vp9_rtcd();
  785. vpx_dsp_rtcd();
  786. vpx_scale_rtcd();
  787. vp9_init_intra_predictors();
  788. vp9_init_me_luts();
  789. vp9_rc_init_minq_luts();
  790. vp9_entropy_mv_init();
  791. #if !CONFIG_REALTIME_ONLY
  792. vp9_temporal_filter_init();
  793. #endif
  794. init_done = 1;
  795. }
  796. }
  797. static void dealloc_compressor_data(VP9_COMP *cpi) {
  798. VP9_COMMON *const cm = &cpi->common;
  799. int i;
  800. vpx_free(cpi->mbmi_ext_base);
  801. cpi->mbmi_ext_base = NULL;
  802. vpx_free(cpi->tile_data);
  803. cpi->tile_data = NULL;
  804. vpx_free(cpi->segmentation_map);
  805. cpi->segmentation_map = NULL;
  806. vpx_free(cpi->coding_context.last_frame_seg_map_copy);
  807. cpi->coding_context.last_frame_seg_map_copy = NULL;
  808. vpx_free(cpi->nmvcosts[0]);
  809. vpx_free(cpi->nmvcosts[1]);
  810. cpi->nmvcosts[0] = NULL;
  811. cpi->nmvcosts[1] = NULL;
  812. vpx_free(cpi->nmvcosts_hp[0]);
  813. vpx_free(cpi->nmvcosts_hp[1]);
  814. cpi->nmvcosts_hp[0] = NULL;
  815. cpi->nmvcosts_hp[1] = NULL;
  816. vpx_free(cpi->nmvsadcosts[0]);
  817. vpx_free(cpi->nmvsadcosts[1]);
  818. cpi->nmvsadcosts[0] = NULL;
  819. cpi->nmvsadcosts[1] = NULL;
  820. vpx_free(cpi->nmvsadcosts_hp[0]);
  821. vpx_free(cpi->nmvsadcosts_hp[1]);
  822. cpi->nmvsadcosts_hp[0] = NULL;
  823. cpi->nmvsadcosts_hp[1] = NULL;
  824. vpx_free(cpi->skin_map);
  825. cpi->skin_map = NULL;
  826. vpx_free(cpi->prev_partition);
  827. cpi->prev_partition = NULL;
  828. vpx_free(cpi->svc.prev_partition_svc);
  829. cpi->svc.prev_partition_svc = NULL;
  830. vpx_free(cpi->prev_segment_id);
  831. cpi->prev_segment_id = NULL;
  832. vpx_free(cpi->prev_variance_low);
  833. cpi->prev_variance_low = NULL;
  834. vpx_free(cpi->copied_frame_cnt);
  835. cpi->copied_frame_cnt = NULL;
  836. vpx_free(cpi->content_state_sb_fd);
  837. cpi->content_state_sb_fd = NULL;
  838. vpx_free(cpi->count_arf_frame_usage);
  839. cpi->count_arf_frame_usage = NULL;
  840. vpx_free(cpi->count_lastgolden_frame_usage);
  841. cpi->count_lastgolden_frame_usage = NULL;
  842. vp9_cyclic_refresh_free(cpi->cyclic_refresh);
  843. cpi->cyclic_refresh = NULL;
  844. vpx_free(cpi->active_map.map);
  845. cpi->active_map.map = NULL;
  846. vpx_free(cpi->roi.roi_map);
  847. cpi->roi.roi_map = NULL;
  848. vpx_free(cpi->consec_zero_mv);
  849. cpi->consec_zero_mv = NULL;
  850. vpx_free(cpi->mb_wiener_variance);
  851. cpi->mb_wiener_variance = NULL;
  852. vpx_free(cpi->mi_ssim_rdmult_scaling_factors);
  853. cpi->mi_ssim_rdmult_scaling_factors = NULL;
  854. vp9_free_ref_frame_buffers(cm->buffer_pool);
  855. #if CONFIG_VP9_POSTPROC
  856. vp9_free_postproc_buffers(cm);
  857. #endif
  858. vp9_free_context_buffers(cm);
  859. vpx_free_frame_buffer(&cpi->last_frame_uf);
  860. vpx_free_frame_buffer(&cpi->scaled_source);
  861. vpx_free_frame_buffer(&cpi->scaled_last_source);
  862. vpx_free_frame_buffer(&cpi->alt_ref_buffer);
  863. #ifdef ENABLE_KF_DENOISE
  864. vpx_free_frame_buffer(&cpi->raw_unscaled_source);
  865. vpx_free_frame_buffer(&cpi->raw_scaled_source);
  866. #endif
  867. vp9_lookahead_destroy(cpi->lookahead);
  868. vpx_free(cpi->tile_tok[0][0]);
  869. cpi->tile_tok[0][0] = 0;
  870. vpx_free(cpi->tplist[0][0]);
  871. cpi->tplist[0][0] = NULL;
  872. vp9_free_pc_tree(&cpi->td);
  873. for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
  874. LAYER_CONTEXT *const lc = &cpi->svc.layer_context[i];
  875. vpx_free(lc->rc_twopass_stats_in.buf);
  876. lc->rc_twopass_stats_in.buf = NULL;
  877. lc->rc_twopass_stats_in.sz = 0;
  878. }
  879. if (cpi->source_diff_var != NULL) {
  880. vpx_free(cpi->source_diff_var);
  881. cpi->source_diff_var = NULL;
  882. }
  883. for (i = 0; i < MAX_LAG_BUFFERS; ++i) {
  884. vpx_free_frame_buffer(&cpi->svc.scaled_frames[i]);
  885. }
  886. memset(&cpi->svc.scaled_frames[0], 0,
  887. MAX_LAG_BUFFERS * sizeof(cpi->svc.scaled_frames[0]));
  888. vpx_free_frame_buffer(&cpi->svc.scaled_temp);
  889. memset(&cpi->svc.scaled_temp, 0, sizeof(cpi->svc.scaled_temp));
  890. vpx_free_frame_buffer(&cpi->svc.empty_frame.img);
  891. memset(&cpi->svc.empty_frame, 0, sizeof(cpi->svc.empty_frame));
  892. vp9_free_svc_cyclic_refresh(cpi);
  893. }
  894. static void save_coding_context(VP9_COMP *cpi) {
  895. CODING_CONTEXT *const cc = &cpi->coding_context;
  896. VP9_COMMON *cm = &cpi->common;
  897. // Stores a snapshot of key state variables which can subsequently be
  898. // restored with a call to vp9_restore_coding_context. These functions are
  899. // intended for use in a re-code loop in vp9_compress_frame where the
  900. // quantizer value is adjusted between loop iterations.
  901. vp9_copy(cc->nmvjointcost, cpi->td.mb.nmvjointcost);
  902. memcpy(cc->nmvcosts[0], cpi->nmvcosts[0],
  903. MV_VALS * sizeof(*cpi->nmvcosts[0]));
  904. memcpy(cc->nmvcosts[1], cpi->nmvcosts[1],
  905. MV_VALS * sizeof(*cpi->nmvcosts[1]));
  906. memcpy(cc->nmvcosts_hp[0], cpi->nmvcosts_hp[0],
  907. MV_VALS * sizeof(*cpi->nmvcosts_hp[0]));
  908. memcpy(cc->nmvcosts_hp[1], cpi->nmvcosts_hp[1],
  909. MV_VALS * sizeof(*cpi->nmvcosts_hp[1]));
  910. vp9_copy(cc->segment_pred_probs, cm->seg.pred_probs);
  911. memcpy(cpi->coding_context.last_frame_seg_map_copy, cm->last_frame_seg_map,
  912. (cm->mi_rows * cm->mi_cols));
  913. vp9_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
  914. vp9_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
  915. cc->fc = *cm->fc;
  916. }
  917. static void restore_coding_context(VP9_COMP *cpi) {
  918. CODING_CONTEXT *const cc = &cpi->coding_context;
  919. VP9_COMMON *cm = &cpi->common;
  920. // Restore key state variables to the snapshot state stored in the
  921. // previous call to vp9_save_coding_context.
  922. vp9_copy(cpi->td.mb.nmvjointcost, cc->nmvjointcost);
  923. memcpy(cpi->nmvcosts[0], cc->nmvcosts[0], MV_VALS * sizeof(*cc->nmvcosts[0]));
  924. memcpy(cpi->nmvcosts[1], cc->nmvcosts[1], MV_VALS * sizeof(*cc->nmvcosts[1]));
  925. memcpy(cpi->nmvcosts_hp[0], cc->nmvcosts_hp[0],
  926. MV_VALS * sizeof(*cc->nmvcosts_hp[0]));
  927. memcpy(cpi->nmvcosts_hp[1], cc->nmvcosts_hp[1],
  928. MV_VALS * sizeof(*cc->nmvcosts_hp[1]));
  929. vp9_copy(cm->seg.pred_probs, cc->segment_pred_probs);
  930. memcpy(cm->last_frame_seg_map, cpi->coding_context.last_frame_seg_map_copy,
  931. (cm->mi_rows * cm->mi_cols));
  932. vp9_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
  933. vp9_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
  934. *cm->fc = cc->fc;
  935. }
  936. #if !CONFIG_REALTIME_ONLY
  937. static void configure_static_seg_features(VP9_COMP *cpi) {
  938. VP9_COMMON *const cm = &cpi->common;
  939. const RATE_CONTROL *const rc = &cpi->rc;
  940. struct segmentation *const seg = &cm->seg;
  941. int high_q = (int)(rc->avg_q > 48.0);
  942. int qi_delta;
  943. // Disable and clear down for KF
  944. if (cm->frame_type == KEY_FRAME) {
  945. // Clear down the global segmentation map
  946. memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
  947. seg->update_map = 0;
  948. seg->update_data = 0;
  949. cpi->static_mb_pct = 0;
  950. // Disable segmentation
  951. vp9_disable_segmentation(seg);
  952. // Clear down the segment features.
  953. vp9_clearall_segfeatures(seg);
  954. } else if (cpi->refresh_alt_ref_frame) {
  955. // If this is an alt ref frame
  956. // Clear down the global segmentation map
  957. memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
  958. seg->update_map = 0;
  959. seg->update_data = 0;
  960. cpi->static_mb_pct = 0;
  961. // Disable segmentation and individual segment features by default
  962. vp9_disable_segmentation(seg);
  963. vp9_clearall_segfeatures(seg);
  964. // Scan frames from current to arf frame.
  965. // This function re-enables segmentation if appropriate.
  966. vp9_update_mbgraph_stats(cpi);
  967. // If segmentation was enabled set those features needed for the
  968. // arf itself.
  969. if (seg->enabled) {
  970. seg->update_map = 1;
  971. seg->update_data = 1;
  972. qi_delta =
  973. vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875, cm->bit_depth);
  974. vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
  975. vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
  976. vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
  977. vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
  978. // Where relevant assume segment data is delta data
  979. seg->abs_delta = SEGMENT_DELTADATA;
  980. }
  981. } else if (seg->enabled) {
  982. // All other frames if segmentation has been enabled
  983. // First normal frame in a valid gf or alt ref group
  984. if (rc->frames_since_golden == 0) {
  985. // Set up segment features for normal frames in an arf group
  986. if (rc->source_alt_ref_active) {
  987. seg->update_map = 0;
  988. seg->update_data = 1;
  989. seg->abs_delta = SEGMENT_DELTADATA;
  990. qi_delta =
  991. vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125, cm->bit_depth);
  992. vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
  993. vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
  994. vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
  995. vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
  996. // Segment coding disabled for compred testing
  997. if (high_q || (cpi->static_mb_pct == 100)) {
  998. vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
  999. vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
  1000. vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
  1001. }
  1002. } else {
  1003. // Disable segmentation and clear down features if alt ref
  1004. // is not active for this group
  1005. vp9_disable_segmentation(seg);
  1006. memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
  1007. seg->update_map = 0;
  1008. seg->update_data = 0;
  1009. vp9_clearall_segfeatures(seg);
  1010. }
  1011. } else if (rc->is_src_frame_alt_ref) {
  1012. // Special case where we are coding over the top of a previous
  1013. // alt ref frame.
  1014. // Segment coding disabled for compred testing
  1015. // Enable ref frame features for segment 0 as well
  1016. vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
  1017. vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
  1018. // All mbs should use ALTREF_FRAME
  1019. vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
  1020. vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
  1021. vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
  1022. vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
  1023. // Skip all MBs if high Q (0,0 mv and skip coeffs)
  1024. if (high_q) {
  1025. vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
  1026. vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
  1027. }
  1028. // Enable data update
  1029. seg->update_data = 1;
  1030. } else {
  1031. // All other frames.
  1032. // No updates.. leave things as they are.
  1033. seg->update_map = 0;
  1034. seg->update_data = 0;
  1035. }
  1036. }
  1037. }
  1038. #endif // !CONFIG_REALTIME_ONLY
  1039. static void update_reference_segmentation_map(VP9_COMP *cpi) {
  1040. VP9_COMMON *const cm = &cpi->common;
  1041. MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
  1042. uint8_t *cache_ptr = cm->last_frame_seg_map;
  1043. int row, col;
  1044. for (row = 0; row < cm->mi_rows; row++) {
  1045. MODE_INFO **mi_8x8 = mi_8x8_ptr;
  1046. uint8_t *cache = cache_ptr;
  1047. for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
  1048. cache[0] = mi_8x8[0]->segment_id;
  1049. mi_8x8_ptr += cm->mi_stride;
  1050. cache_ptr += cm->mi_cols;
  1051. }
  1052. }
  1053. static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
  1054. VP9_COMMON *cm = &cpi->common;
  1055. const VP9EncoderConfig *oxcf = &cpi->oxcf;
  1056. if (!cpi->lookahead)
  1057. cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height,
  1058. cm->subsampling_x, cm->subsampling_y,
  1059. #if CONFIG_VP9_HIGHBITDEPTH
  1060. cm->use_highbitdepth,
  1061. #endif
  1062. oxcf->lag_in_frames);
  1063. if (!cpi->lookahead)
  1064. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  1065. "Failed to allocate lag buffers");
  1066. // TODO(agrange) Check if ARF is enabled and skip allocation if not.
  1067. if (vpx_realloc_frame_buffer(&cpi->alt_ref_buffer, oxcf->width, oxcf->height,
  1068. cm->subsampling_x, cm->subsampling_y,
  1069. #if CONFIG_VP9_HIGHBITDEPTH
  1070. cm->use_highbitdepth,
  1071. #endif
  1072. VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
  1073. NULL, NULL, NULL))
  1074. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  1075. "Failed to allocate altref buffer");
  1076. }
  1077. static void alloc_util_frame_buffers(VP9_COMP *cpi) {
  1078. VP9_COMMON *const cm = &cpi->common;
  1079. if (vpx_realloc_frame_buffer(&cpi->last_frame_uf, cm->width, cm->height,
  1080. cm->subsampling_x, cm->subsampling_y,
  1081. #if CONFIG_VP9_HIGHBITDEPTH
  1082. cm->use_highbitdepth,
  1083. #endif
  1084. VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
  1085. NULL, NULL, NULL))
  1086. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  1087. "Failed to allocate last frame buffer");
  1088. if (vpx_realloc_frame_buffer(&cpi->scaled_source, cm->width, cm->height,
  1089. cm->subsampling_x, cm->subsampling_y,
  1090. #if CONFIG_VP9_HIGHBITDEPTH
  1091. cm->use_highbitdepth,
  1092. #endif
  1093. VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
  1094. NULL, NULL, NULL))
  1095. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  1096. "Failed to allocate scaled source buffer");
  1097. // For 1 pass cbr: allocate scaled_frame that may be used as an intermediate
  1098. // buffer for a 2 stage down-sampling: two stages of 1:2 down-sampling for a
  1099. // target of 1/4x1/4. number_spatial_layers must be greater than 2.
  1100. if (is_one_pass_cbr_svc(cpi) && !cpi->svc.scaled_temp_is_alloc &&
  1101. cpi->svc.number_spatial_layers > 2) {
  1102. cpi->svc.scaled_temp_is_alloc = 1;
  1103. if (vpx_realloc_frame_buffer(
  1104. &cpi->svc.scaled_temp, cm->width >> 1, cm->height >> 1,
  1105. cm->subsampling_x, cm->subsampling_y,
  1106. #if CONFIG_VP9_HIGHBITDEPTH
  1107. cm->use_highbitdepth,
  1108. #endif
  1109. VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
  1110. vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
  1111. "Failed to allocate scaled_frame for svc ");
  1112. }
  1113. if (vpx_realloc_frame_buffer(&cpi->scaled_last_source, cm->width, cm->height,
  1114. cm->subsampling_x, cm->subsampling_y,
  1115. #if CONFIG_VP9_HIGHBITDEPTH
  1116. cm->use_highbitdepth,
  1117. #endif
  1118. VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
  1119. NULL, NULL, NULL))
  1120. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  1121. "Failed to allocate scaled last source buffer");
  1122. #ifdef ENABLE_KF_DENOISE
  1123. if (vpx_realloc_frame_buffer(&cpi->raw_unscaled_source, cm->width, cm->height,
  1124. cm->subsampling_x, cm->subsampling_y,
  1125. #if CONFIG_VP9_HIGHBITDEPTH
  1126. cm->use_highbitdepth,
  1127. #endif
  1128. VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
  1129. NULL, NULL, NULL))
  1130. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  1131. "Failed to allocate unscaled raw source frame buffer");
  1132. if (vpx_realloc_frame_buffer(&cpi->raw_scaled_source, cm->width, cm->height,
  1133. cm->subsampling_x, cm->subsampling_y,
  1134. #if CONFIG_VP9_HIGHBITDEPTH
  1135. cm->use_highbitdepth,
  1136. #endif
  1137. VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
  1138. NULL, NULL, NULL))
  1139. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  1140. "Failed to allocate scaled raw source frame buffer");
  1141. #endif
  1142. }
  1143. static int alloc_context_buffers_ext(VP9_COMP *cpi) {
  1144. VP9_COMMON *cm = &cpi->common;
  1145. int mi_size = cm->mi_cols * cm->mi_rows;
  1146. cpi->mbmi_ext_base = vpx_calloc(mi_size, sizeof(*cpi->mbmi_ext_base));
  1147. if (!cpi->mbmi_ext_base) return 1;
  1148. return 0;
  1149. }
  1150. static void alloc_compressor_data(VP9_COMP *cpi) {
  1151. VP9_COMMON *cm = &cpi->common;
  1152. int sb_rows;
  1153. vp9_alloc_context_buffers(cm, cm->width, cm->height);
  1154. alloc_context_buffers_ext(cpi);
  1155. vpx_free(cpi->tile_tok[0][0]);
  1156. {
  1157. unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
  1158. CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
  1159. vpx_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
  1160. }
  1161. sb_rows = mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2;
  1162. vpx_free(cpi->tplist[0][0]);
  1163. CHECK_MEM_ERROR(
  1164. cm, cpi->tplist[0][0],
  1165. vpx_calloc(sb_rows * 4 * (1 << 6), sizeof(*cpi->tplist[0][0])));
  1166. vp9_setup_pc_tree(&cpi->common, &cpi->td);
  1167. }
  1168. void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
  1169. cpi->framerate = framerate < 0.1 ? 30 : framerate;
  1170. vp9_rc_update_framerate(cpi);
  1171. }
  1172. static void set_tile_limits(VP9_COMP *cpi) {
  1173. VP9_COMMON *const cm = &cpi->common;
  1174. int min_log2_tile_cols, max_log2_tile_cols;
  1175. vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
  1176. cm->log2_tile_cols =
  1177. clamp(cpi->oxcf.tile_columns, min_log2_tile_cols, max_log2_tile_cols);
  1178. cm->log2_tile_rows = cpi->oxcf.tile_rows;
  1179. if (cpi->oxcf.target_level == LEVEL_AUTO) {
  1180. const int level_tile_cols =
  1181. log_tile_cols_from_picsize_level(cpi->common.width, cpi->common.height);
  1182. if (cm->log2_tile_cols > level_tile_cols) {
  1183. cm->log2_tile_cols = VPXMAX(level_tile_cols, min_log2_tile_cols);
  1184. }
  1185. }
  1186. }
  1187. static void update_frame_size(VP9_COMP *cpi) {
  1188. VP9_COMMON *const cm = &cpi->common;
  1189. MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
  1190. vp9_set_mb_mi(cm, cm->width, cm->height);
  1191. vp9_init_context_buffers(cm);
  1192. vp9_init_macroblockd(cm, xd, NULL);
  1193. cpi->td.mb.mbmi_ext_base = cpi->mbmi_ext_base;
  1194. memset(cpi->mbmi_ext_base, 0,
  1195. cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
  1196. set_tile_limits(cpi);
  1197. }
  1198. static void init_buffer_indices(VP9_COMP *cpi) {
  1199. int ref_frame;
  1200. for (ref_frame = 0; ref_frame < REF_FRAMES; ++ref_frame)
  1201. cpi->ref_fb_idx[ref_frame] = ref_frame;
  1202. cpi->lst_fb_idx = cpi->ref_fb_idx[LAST_FRAME - 1];
  1203. cpi->gld_fb_idx = cpi->ref_fb_idx[GOLDEN_FRAME - 1];
  1204. cpi->alt_fb_idx = cpi->ref_fb_idx[ALTREF_FRAME - 1];
  1205. }
  1206. static void init_level_constraint(LevelConstraint *lc) {
  1207. lc->level_index = -1;
  1208. lc->max_cpb_size = INT_MAX;
  1209. lc->max_frame_size = INT_MAX;
  1210. lc->rc_config_updated = 0;
  1211. lc->fail_flag = 0;
  1212. }
  1213. static void set_level_constraint(LevelConstraint *ls, int8_t level_index) {
  1214. vpx_clear_system_state();
  1215. ls->level_index = level_index;
  1216. if (level_index >= 0) {
  1217. ls->max_cpb_size = vp9_level_defs[level_index].max_cpb_size * (double)1000;
  1218. }
  1219. }
  1220. static void init_config(struct VP9_COMP *cpi, VP9EncoderConfig *oxcf) {
  1221. VP9_COMMON *const cm = &cpi->common;
  1222. cpi->oxcf = *oxcf;
  1223. cpi->framerate = oxcf->init_framerate;
  1224. cm->profile = oxcf->profile;
  1225. cm->bit_depth = oxcf->bit_depth;
  1226. #if CONFIG_VP9_HIGHBITDEPTH
  1227. cm->use_highbitdepth = oxcf->use_highbitdepth;
  1228. #endif
  1229. cm->color_space = oxcf->color_space;
  1230. cm->color_range = oxcf->color_range;
  1231. cpi->target_level = oxcf->target_level;
  1232. cpi->keep_level_stats = oxcf->target_level != LEVEL_MAX;
  1233. set_level_constraint(&cpi->level_constraint,
  1234. get_level_index(cpi->target_level));
  1235. cm->width = oxcf->width;
  1236. cm->height = oxcf->height;
  1237. alloc_compressor_data(cpi);
  1238. cpi->svc.temporal_layering_mode = oxcf->temporal_layering_mode;
  1239. // Single thread case: use counts in common.
  1240. cpi->td.counts = &cm->counts;
  1241. // Spatial scalability.
  1242. cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
  1243. // Temporal scalability.
  1244. cpi->svc.number_temporal_layers = oxcf->ts_number_layers;
  1245. if ((cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ||
  1246. ((cpi->svc.number_temporal_layers > 1 ||
  1247. cpi->svc.number_spatial_layers > 1) &&
  1248. cpi->oxcf.pass != 1)) {
  1249. vp9_init_layer_context(cpi);
  1250. }
  1251. // change includes all joint functionality
  1252. vp9_change_config(cpi, oxcf);
  1253. cpi->static_mb_pct = 0;
  1254. cpi->ref_frame_flags = 0;
  1255. init_buffer_indices(cpi);
  1256. vp9_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
  1257. }
  1258. static void set_rc_buffer_sizes(RATE_CONTROL *rc,
  1259. const VP9EncoderConfig *oxcf) {
  1260. const int64_t bandwidth = oxcf->target_bandwidth;
  1261. const int64_t starting = oxcf->starting_buffer_level_ms;
  1262. const int64_t optimal = oxcf->optimal_buffer_level_ms;
  1263. const int64_t maximum = oxcf->maximum_buffer_size_ms;
  1264. rc->starting_buffer_level = starting * bandwidth / 1000;
  1265. rc->optimal_buffer_level =
  1266. (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
  1267. rc->maximum_buffer_size =
  1268. (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
  1269. }
  1270. #if CONFIG_VP9_HIGHBITDEPTH
  1271. #define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF) \
  1272. cpi->fn_ptr[BT].sdf = SDF; \
  1273. cpi->fn_ptr[BT].sdaf = SDAF; \
  1274. cpi->fn_ptr[BT].vf = VF; \
  1275. cpi->fn_ptr[BT].svf = SVF; \
  1276. cpi->fn_ptr[BT].svaf = SVAF; \
  1277. cpi->fn_ptr[BT].sdx4df = SDX4DF;
  1278. #define MAKE_BFP_SAD_WRAPPER(fnname) \
  1279. static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
  1280. int source_stride, \
  1281. const uint8_t *ref_ptr, int ref_stride) { \
  1282. return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
  1283. } \
  1284. static unsigned int fnname##_bits10( \
  1285. const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
  1286. int ref_stride) { \
  1287. return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
  1288. } \
  1289. static unsigned int fnname##_bits12( \
  1290. const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
  1291. int ref_stride) { \
  1292. return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
  1293. }
  1294. #define MAKE_BFP_SADAVG_WRAPPER(fnname) \
  1295. static unsigned int fnname##_bits8( \
  1296. const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
  1297. int ref_stride, const uint8_t *second_pred) { \
  1298. return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
  1299. } \
  1300. static unsigned int fnname##_bits10( \
  1301. const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
  1302. int ref_stride, const uint8_t *second_pred) { \
  1303. return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
  1304. 2; \
  1305. } \
  1306. static unsigned int fnname##_bits12( \
  1307. const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
  1308. int ref_stride, const uint8_t *second_pred) { \
  1309. return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
  1310. 4; \
  1311. }
  1312. #define MAKE_BFP_SAD4D_WRAPPER(fnname) \
  1313. static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
  1314. const uint8_t *const ref_ptr[], int ref_stride, \
  1315. unsigned int *sad_array) { \
  1316. fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
  1317. } \
  1318. static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
  1319. const uint8_t *const ref_ptr[], int ref_stride, \
  1320. unsigned int *sad_array) { \
  1321. int i; \
  1322. fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
  1323. for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
  1324. } \
  1325. static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
  1326. const uint8_t *const ref_ptr[], int ref_stride, \
  1327. unsigned int *sad_array) { \
  1328. int i; \
  1329. fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
  1330. for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
  1331. }
  1332. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x16)
  1333. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x16_avg)
  1334. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x16x4d)
  1335. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x32)
  1336. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x32_avg)
  1337. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x32x4d)
  1338. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad64x32)
  1339. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad64x32_avg)
  1340. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad64x32x4d)
  1341. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x64)
  1342. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x64_avg)
  1343. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x64x4d)
  1344. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x32)
  1345. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x32_avg)
  1346. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x32x4d)
  1347. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad64x64)
  1348. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad64x64_avg)
  1349. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad64x64x4d)
  1350. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x16)
  1351. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x16_avg)
  1352. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x16x4d)
  1353. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x8)
  1354. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x8_avg)
  1355. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x8x4d)
  1356. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x16)
  1357. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x16_avg)
  1358. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x16x4d)
  1359. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x8)
  1360. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x8_avg)
  1361. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x8x4d)
  1362. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x4)
  1363. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x4_avg)
  1364. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x4x4d)
  1365. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad4x8)
  1366. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad4x8_avg)
  1367. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad4x8x4d)
  1368. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad4x4)
  1369. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad4x4_avg)
  1370. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad4x4x4d)
  1371. static void highbd_set_var_fns(VP9_COMP *const cpi) {
  1372. VP9_COMMON *const cm = &cpi->common;
  1373. if (cm->use_highbitdepth) {
  1374. switch (cm->bit_depth) {
  1375. case VPX_BITS_8:
  1376. HIGHBD_BFP(BLOCK_32X16, vpx_highbd_sad32x16_bits8,
  1377. vpx_highbd_sad32x16_avg_bits8, vpx_highbd_8_variance32x16,
  1378. vpx_highbd_8_sub_pixel_variance32x16,
  1379. vpx_highbd_8_sub_pixel_avg_variance32x16,
  1380. vpx_highbd_sad32x16x4d_bits8)
  1381. HIGHBD_BFP(BLOCK_16X32, vpx_highbd_sad16x32_bits8,
  1382. vpx_highbd_sad16x32_avg_bits8, vpx_highbd_8_variance16x32,
  1383. vpx_highbd_8_sub_pixel_variance16x32,
  1384. vpx_highbd_8_sub_pixel_avg_variance16x32,
  1385. vpx_highbd_sad16x32x4d_bits8)
  1386. HIGHBD_BFP(BLOCK_64X32, vpx_highbd_sad64x32_bits8,
  1387. vpx_highbd_sad64x32_avg_bits8, vpx_highbd_8_variance64x32,
  1388. vpx_highbd_8_sub_pixel_variance64x32,
  1389. vpx_highbd_8_sub_pixel_avg_variance64x32,
  1390. vpx_highbd_sad64x32x4d_bits8)
  1391. HIGHBD_BFP(BLOCK_32X64, vpx_highbd_sad32x64_bits8,
  1392. vpx_highbd_sad32x64_avg_bits8, vpx_highbd_8_variance32x64,
  1393. vpx_highbd_8_sub_pixel_variance32x64,
  1394. vpx_highbd_8_sub_pixel_avg_variance32x64,
  1395. vpx_highbd_sad32x64x4d_bits8)
  1396. HIGHBD_BFP(BLOCK_32X32, vpx_highbd_sad32x32_bits8,
  1397. vpx_highbd_sad32x32_avg_bits8, vpx_highbd_8_variance32x32,
  1398. vpx_highbd_8_sub_pixel_variance32x32,
  1399. vpx_highbd_8_sub_pixel_avg_variance32x32,
  1400. vpx_highbd_sad32x32x4d_bits8)
  1401. HIGHBD_BFP(BLOCK_64X64, vpx_highbd_sad64x64_bits8,
  1402. vpx_highbd_sad64x64_avg_bits8, vpx_highbd_8_variance64x64,
  1403. vpx_highbd_8_sub_pixel_variance64x64,
  1404. vpx_highbd_8_sub_pixel_avg_variance64x64,
  1405. vpx_highbd_sad64x64x4d_bits8)
  1406. HIGHBD_BFP(BLOCK_16X16, vpx_highbd_sad16x16_bits8,
  1407. vpx_highbd_sad16x16_avg_bits8, vpx_highbd_8_variance16x16,
  1408. vpx_highbd_8_sub_pixel_variance16x16,
  1409. vpx_highbd_8_sub_pixel_avg_variance16x16,
  1410. vpx_highbd_sad16x16x4d_bits8)
  1411. HIGHBD_BFP(BLOCK_16X8, vpx_highbd_sad16x8_bits8,
  1412. vpx_highbd_sad16x8_avg_bits8, vpx_highbd_8_variance16x8,
  1413. vpx_highbd_8_sub_pixel_variance16x8,
  1414. vpx_highbd_8_sub_pixel_avg_variance16x8,
  1415. vpx_highbd_sad16x8x4d_bits8)
  1416. HIGHBD_BFP(BLOCK_8X16, vpx_highbd_sad8x16_bits8,
  1417. vpx_highbd_sad8x16_avg_bits8, vpx_highbd_8_variance8x16,
  1418. vpx_highbd_8_sub_pixel_variance8x16,
  1419. vpx_highbd_8_sub_pixel_avg_variance8x16,
  1420. vpx_highbd_sad8x16x4d_bits8)
  1421. HIGHBD_BFP(
  1422. BLOCK_8X8, vpx_highbd_sad8x8_bits8, vpx_highbd_sad8x8_avg_bits8,
  1423. vpx_highbd_8_variance8x8, vpx_highbd_8_sub_pixel_variance8x8,
  1424. vpx_highbd_8_sub_pixel_avg_variance8x8, vpx_highbd_sad8x8x4d_bits8)
  1425. HIGHBD_BFP(
  1426. BLOCK_8X4, vpx_highbd_sad8x4_bits8, vpx_highbd_sad8x4_avg_bits8,
  1427. vpx_highbd_8_variance8x4, vpx_highbd_8_sub_pixel_variance8x4,
  1428. vpx_highbd_8_sub_pixel_avg_variance8x4, vpx_highbd_sad8x4x4d_bits8)
  1429. HIGHBD_BFP(
  1430. BLOCK_4X8, vpx_highbd_sad4x8_bits8, vpx_highbd_sad4x8_avg_bits8,
  1431. vpx_highbd_8_variance4x8, vpx_highbd_8_sub_pixel_variance4x8,
  1432. vpx_highbd_8_sub_pixel_avg_variance4x8, vpx_highbd_sad4x8x4d_bits8)
  1433. HIGHBD_BFP(
  1434. BLOCK_4X4, vpx_highbd_sad4x4_bits8, vpx_highbd_sad4x4_avg_bits8,
  1435. vpx_highbd_8_variance4x4, vpx_highbd_8_sub_pixel_variance4x4,
  1436. vpx_highbd_8_sub_pixel_avg_variance4x4, vpx_highbd_sad4x4x4d_bits8)
  1437. break;
  1438. case VPX_BITS_10:
  1439. HIGHBD_BFP(BLOCK_32X16, vpx_highbd_sad32x16_bits10,
  1440. vpx_highbd_sad32x16_avg_bits10, vpx_highbd_10_variance32x16,
  1441. vpx_highbd_10_sub_pixel_variance32x16,
  1442. vpx_highbd_10_sub_pixel_avg_variance32x16,
  1443. vpx_highbd_sad32x16x4d_bits10)
  1444. HIGHBD_BFP(BLOCK_16X32, vpx_highbd_sad16x32_bits10,
  1445. vpx_highbd_sad16x32_avg_bits10, vpx_highbd_10_variance16x32,
  1446. vpx_highbd_10_sub_pixel_variance16x32,
  1447. vpx_highbd_10_sub_pixel_avg_variance16x32,
  1448. vpx_highbd_sad16x32x4d_bits10)
  1449. HIGHBD_BFP(BLOCK_64X32, vpx_highbd_sad64x32_bits10,
  1450. vpx_highbd_sad64x32_avg_bits10, vpx_highbd_10_variance64x32,
  1451. vpx_highbd_10_sub_pixel_variance64x32,
  1452. vpx_highbd_10_sub_pixel_avg_variance64x32,
  1453. vpx_highbd_sad64x32x4d_bits10)
  1454. HIGHBD_BFP(BLOCK_32X64, vpx_highbd_sad32x64_bits10,
  1455. vpx_highbd_sad32x64_avg_bits10, vpx_highbd_10_variance32x64,
  1456. vpx_highbd_10_sub_pixel_variance32x64,
  1457. vpx_highbd_10_sub_pixel_avg_variance32x64,
  1458. vpx_highbd_sad32x64x4d_bits10)
  1459. HIGHBD_BFP(BLOCK_32X32, vpx_highbd_sad32x32_bits10,
  1460. vpx_highbd_sad32x32_avg_bits10, vpx_highbd_10_variance32x32,
  1461. vpx_highbd_10_sub_pixel_variance32x32,
  1462. vpx_highbd_10_sub_pixel_avg_variance32x32,
  1463. vpx_highbd_sad32x32x4d_bits10)
  1464. HIGHBD_BFP(BLOCK_64X64, vpx_highbd_sad64x64_bits10,
  1465. vpx_highbd_sad64x64_avg_bits10, vpx_highbd_10_variance64x64,
  1466. vpx_highbd_10_sub_pixel_variance64x64,
  1467. vpx_highbd_10_sub_pixel_avg_variance64x64,
  1468. vpx_highbd_sad64x64x4d_bits10)
  1469. HIGHBD_BFP(BLOCK_16X16, vpx_highbd_sad16x16_bits10,
  1470. vpx_highbd_sad16x16_avg_bits10, vpx_highbd_10_variance16x16,
  1471. vpx_highbd_10_sub_pixel_variance16x16,
  1472. vpx_highbd_10_sub_pixel_avg_variance16x16,
  1473. vpx_highbd_sad16x16x4d_bits10)
  1474. HIGHBD_BFP(BLOCK_16X8, vpx_highbd_sad16x8_bits10,
  1475. vpx_highbd_sad16x8_avg_bits10, vpx_highbd_10_variance16x8,
  1476. vpx_highbd_10_sub_pixel_variance16x8,
  1477. vpx_highbd_10_sub_pixel_avg_variance16x8,
  1478. vpx_highbd_sad16x8x4d_bits10)
  1479. HIGHBD_BFP(BLOCK_8X16, vpx_highbd_sad8x16_bits10,
  1480. vpx_highbd_sad8x16_avg_bits10, vpx_highbd_10_variance8x16,
  1481. vpx_highbd_10_sub_pixel_variance8x16,
  1482. vpx_highbd_10_sub_pixel_avg_variance8x16,
  1483. vpx_highbd_sad8x16x4d_bits10)
  1484. HIGHBD_BFP(BLOCK_8X8, vpx_highbd_sad8x8_bits10,
  1485. vpx_highbd_sad8x8_avg_bits10, vpx_highbd_10_variance8x8,
  1486. vpx_highbd_10_sub_pixel_variance8x8,
  1487. vpx_highbd_10_sub_pixel_avg_variance8x8,
  1488. vpx_highbd_sad8x8x4d_bits10)
  1489. HIGHBD_BFP(BLOCK_8X4, vpx_highbd_sad8x4_bits10,
  1490. vpx_highbd_sad8x4_avg_bits10, vpx_highbd_10_variance8x4,
  1491. vpx_highbd_10_sub_pixel_variance8x4,
  1492. vpx_highbd_10_sub_pixel_avg_variance8x4,
  1493. vpx_highbd_sad8x4x4d_bits10)
  1494. HIGHBD_BFP(BLOCK_4X8, vpx_highbd_sad4x8_bits10,
  1495. vpx_highbd_sad4x8_avg_bits10, vpx_highbd_10_variance4x8,
  1496. vpx_highbd_10_sub_pixel_variance4x8,
  1497. vpx_highbd_10_sub_pixel_avg_variance4x8,
  1498. vpx_highbd_sad4x8x4d_bits10)
  1499. HIGHBD_BFP(BLOCK_4X4, vpx_highbd_sad4x4_bits10,
  1500. vpx_highbd_sad4x4_avg_bits10, vpx_highbd_10_variance4x4,
  1501. vpx_highbd_10_sub_pixel_variance4x4,
  1502. vpx_highbd_10_sub_pixel_avg_variance4x4,
  1503. vpx_highbd_sad4x4x4d_bits10)
  1504. break;
  1505. default:
  1506. assert(cm->bit_depth == VPX_BITS_12);
  1507. HIGHBD_BFP(BLOCK_32X16, vpx_highbd_sad32x16_bits12,
  1508. vpx_highbd_sad32x16_avg_bits12, vpx_highbd_12_variance32x16,
  1509. vpx_highbd_12_sub_pixel_variance32x16,
  1510. vpx_highbd_12_sub_pixel_avg_variance32x16,
  1511. vpx_highbd_sad32x16x4d_bits12)
  1512. HIGHBD_BFP(BLOCK_16X32, vpx_highbd_sad16x32_bits12,
  1513. vpx_highbd_sad16x32_avg_bits12, vpx_highbd_12_variance16x32,
  1514. vpx_highbd_12_sub_pixel_variance16x32,
  1515. vpx_highbd_12_sub_pixel_avg_variance16x32,
  1516. vpx_highbd_sad16x32x4d_bits12)
  1517. HIGHBD_BFP(BLOCK_64X32, vpx_highbd_sad64x32_bits12,
  1518. vpx_highbd_sad64x32_avg_bits12, vpx_highbd_12_variance64x32,
  1519. vpx_highbd_12_sub_pixel_variance64x32,
  1520. vpx_highbd_12_sub_pixel_avg_variance64x32,
  1521. vpx_highbd_sad64x32x4d_bits12)
  1522. HIGHBD_BFP(BLOCK_32X64, vpx_highbd_sad32x64_bits12,
  1523. vpx_highbd_sad32x64_avg_bits12, vpx_highbd_12_variance32x64,
  1524. vpx_highbd_12_sub_pixel_variance32x64,
  1525. vpx_highbd_12_sub_pixel_avg_variance32x64,
  1526. vpx_highbd_sad32x64x4d_bits12)
  1527. HIGHBD_BFP(BLOCK_32X32, vpx_highbd_sad32x32_bits12,
  1528. vpx_highbd_sad32x32_avg_bits12, vpx_highbd_12_variance32x32,
  1529. vpx_highbd_12_sub_pixel_variance32x32,
  1530. vpx_highbd_12_sub_pixel_avg_variance32x32,
  1531. vpx_highbd_sad32x32x4d_bits12)
  1532. HIGHBD_BFP(BLOCK_64X64, vpx_highbd_sad64x64_bits12,
  1533. vpx_highbd_sad64x64_avg_bits12, vpx_highbd_12_variance64x64,
  1534. vpx_highbd_12_sub_pixel_variance64x64,
  1535. vpx_highbd_12_sub_pixel_avg_variance64x64,
  1536. vpx_highbd_sad64x64x4d_bits12)
  1537. HIGHBD_BFP(BLOCK_16X16, vpx_highbd_sad16x16_bits12,
  1538. vpx_highbd_sad16x16_avg_bits12, vpx_highbd_12_variance16x16,
  1539. vpx_highbd_12_sub_pixel_variance16x16,
  1540. vpx_highbd_12_sub_pixel_avg_variance16x16,
  1541. vpx_highbd_sad16x16x4d_bits12)
  1542. HIGHBD_BFP(BLOCK_16X8, vpx_highbd_sad16x8_bits12,
  1543. vpx_highbd_sad16x8_avg_bits12, vpx_highbd_12_variance16x8,
  1544. vpx_highbd_12_sub_pixel_variance16x8,
  1545. vpx_highbd_12_sub_pixel_avg_variance16x8,
  1546. vpx_highbd_sad16x8x4d_bits12)
  1547. HIGHBD_BFP(BLOCK_8X16, vpx_highbd_sad8x16_bits12,
  1548. vpx_highbd_sad8x16_avg_bits12, vpx_highbd_12_variance8x16,
  1549. vpx_highbd_12_sub_pixel_variance8x16,
  1550. vpx_highbd_12_sub_pixel_avg_variance8x16,
  1551. vpx_highbd_sad8x16x4d_bits12)
  1552. HIGHBD_BFP(BLOCK_8X8, vpx_highbd_sad8x8_bits12,
  1553. vpx_highbd_sad8x8_avg_bits12, vpx_highbd_12_variance8x8,
  1554. vpx_highbd_12_sub_pixel_variance8x8,
  1555. vpx_highbd_12_sub_pixel_avg_variance8x8,
  1556. vpx_highbd_sad8x8x4d_bits12)
  1557. HIGHBD_BFP(BLOCK_8X4, vpx_highbd_sad8x4_bits12,
  1558. vpx_highbd_sad8x4_avg_bits12, vpx_highbd_12_variance8x4,
  1559. vpx_highbd_12_sub_pixel_variance8x4,
  1560. vpx_highbd_12_sub_pixel_avg_variance8x4,
  1561. vpx_highbd_sad8x4x4d_bits12)
  1562. HIGHBD_BFP(BLOCK_4X8, vpx_highbd_sad4x8_bits12,
  1563. vpx_highbd_sad4x8_avg_bits12, vpx_highbd_12_variance4x8,
  1564. vpx_highbd_12_sub_pixel_variance4x8,
  1565. vpx_highbd_12_sub_pixel_avg_variance4x8,
  1566. vpx_highbd_sad4x8x4d_bits12)
  1567. HIGHBD_BFP(BLOCK_4X4, vpx_highbd_sad4x4_bits12,
  1568. vpx_highbd_sad4x4_avg_bits12, vpx_highbd_12_variance4x4,
  1569. vpx_highbd_12_sub_pixel_variance4x4,
  1570. vpx_highbd_12_sub_pixel_avg_variance4x4,
  1571. vpx_highbd_sad4x4x4d_bits12)
  1572. break;
  1573. }
  1574. }
  1575. }
  1576. #endif // CONFIG_VP9_HIGHBITDEPTH
  1577. static void realloc_segmentation_maps(VP9_COMP *cpi) {
  1578. VP9_COMMON *const cm = &cpi->common;
  1579. // Create the encoder segmentation map and set all entries to 0
  1580. vpx_free(cpi->segmentation_map);
  1581. CHECK_MEM_ERROR(cm, cpi->segmentation_map,
  1582. vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
  1583. // Create a map used for cyclic background refresh.
  1584. if (cpi->cyclic_refresh) vp9_cyclic_refresh_free(cpi->cyclic_refresh);
  1585. CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
  1586. vp9_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
  1587. // Create a map used to mark inactive areas.
  1588. vpx_free(cpi->active_map.map);
  1589. CHECK_MEM_ERROR(cm, cpi->active_map.map,
  1590. vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
  1591. // And a place holder structure is the coding context
  1592. // for use if we want to save and restore it
  1593. vpx_free(cpi->coding_context.last_frame_seg_map_copy);
  1594. CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy,
  1595. vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
  1596. }
  1597. static void alloc_copy_partition_data(VP9_COMP *cpi) {
  1598. VP9_COMMON *const cm = &cpi->common;
  1599. if (cpi->prev_partition == NULL) {
  1600. CHECK_MEM_ERROR(cm, cpi->prev_partition,
  1601. (BLOCK_SIZE *)vpx_calloc(cm->mi_stride * cm->mi_rows,
  1602. sizeof(*cpi->prev_partition)));
  1603. }
  1604. if (cpi->prev_segment_id == NULL) {
  1605. CHECK_MEM_ERROR(
  1606. cm, cpi->prev_segment_id,
  1607. (int8_t *)vpx_calloc((cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1),
  1608. sizeof(*cpi->prev_segment_id)));
  1609. }
  1610. if (cpi->prev_variance_low == NULL) {
  1611. CHECK_MEM_ERROR(cm, cpi->prev_variance_low,
  1612. (uint8_t *)vpx_calloc(
  1613. (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1) * 25,
  1614. sizeof(*cpi->prev_variance_low)));
  1615. }
  1616. if (cpi->copied_frame_cnt == NULL) {
  1617. CHECK_MEM_ERROR(
  1618. cm, cpi->copied_frame_cnt,
  1619. (uint8_t *)vpx_calloc((cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1),
  1620. sizeof(*cpi->copied_frame_cnt)));
  1621. }
  1622. }
  1623. static void free_copy_partition_data(VP9_COMP *cpi) {
  1624. vpx_free(cpi->prev_partition);
  1625. cpi->prev_partition = NULL;
  1626. vpx_free(cpi->prev_segment_id);
  1627. cpi->prev_segment_id = NULL;
  1628. vpx_free(cpi->prev_variance_low);
  1629. cpi->prev_variance_low = NULL;
  1630. vpx_free(cpi->copied_frame_cnt);
  1631. cpi->copied_frame_cnt = NULL;
  1632. }
  1633. void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
  1634. VP9_COMMON *const cm = &cpi->common;
  1635. RATE_CONTROL *const rc = &cpi->rc;
  1636. int last_w = cpi->oxcf.width;
  1637. int last_h = cpi->oxcf.height;
  1638. vp9_init_quantizer(cpi);
  1639. if (cm->profile != oxcf->profile) cm->profile = oxcf->profile;
  1640. cm->bit_depth = oxcf->bit_depth;
  1641. cm->color_space = oxcf->color_space;
  1642. cm->color_range = oxcf->color_range;
  1643. cpi->target_level = oxcf->target_level;
  1644. cpi->keep_level_stats = oxcf->target_level != LEVEL_MAX;
  1645. set_level_constraint(&cpi->level_constraint,
  1646. get_level_index(cpi->target_level));
  1647. if (cm->profile <= PROFILE_1)
  1648. assert(cm->bit_depth == VPX_BITS_8);
  1649. else
  1650. assert(cm->bit_depth > VPX_BITS_8);
  1651. cpi->oxcf = *oxcf;
  1652. #if CONFIG_VP9_HIGHBITDEPTH
  1653. cpi->td.mb.e_mbd.bd = (int)cm->bit_depth;
  1654. #endif // CONFIG_VP9_HIGHBITDEPTH
  1655. if ((oxcf->pass == 0) && (oxcf->rc_mode == VPX_Q)) {
  1656. rc->baseline_gf_interval = FIXED_GF_INTERVAL;
  1657. } else {
  1658. rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
  1659. }
  1660. cpi->refresh_golden_frame = 0;
  1661. cpi->refresh_last_frame = 1;
  1662. cm->refresh_frame_context = 1;
  1663. cm->reset_frame_context = 0;
  1664. vp9_reset_segment_features(&cm->seg);
  1665. vp9_set_high_precision_mv(cpi, 0);
  1666. {
  1667. int i;
  1668. for (i = 0; i < MAX_SEGMENTS; i++)
  1669. cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
  1670. }
  1671. cpi->encode_breakout = cpi->oxcf.encode_breakout;
  1672. set_rc_buffer_sizes(rc, &cpi->oxcf);
  1673. // Under a configuration change, where maximum_buffer_size may change,
  1674. // keep buffer level clipped to the maximum allowed buffer size.
  1675. rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size);
  1676. rc->buffer_level = VPXMIN(rc->buffer_level, rc->maximum_buffer_size);
  1677. // Set up frame rate and related parameters rate control values.
  1678. vp9_new_framerate(cpi, cpi->framerate);
  1679. // Set absolute upper and lower quality limits
  1680. rc->worst_quality = cpi->oxcf.worst_allowed_q;
  1681. rc->best_quality = cpi->oxcf.best_allowed_q;
  1682. cm->interp_filter = cpi->sf.default_interp_filter;
  1683. if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
  1684. cm->render_width = cpi->oxcf.render_width;
  1685. cm->render_height = cpi->oxcf.render_height;
  1686. } else {
  1687. cm->render_width = cpi->oxcf.width;
  1688. cm->render_height = cpi->oxcf.height;
  1689. }
  1690. if (last_w != cpi->oxcf.width || last_h != cpi->oxcf.height) {
  1691. cm->width = cpi->oxcf.width;
  1692. cm->height = cpi->oxcf.height;
  1693. cpi->external_resize = 1;
  1694. }
  1695. if (cpi->initial_width) {
  1696. int new_mi_size = 0;
  1697. vp9_set_mb_mi(cm, cm->width, cm->height);
  1698. new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
  1699. if (cm->mi_alloc_size < new_mi_size) {
  1700. vp9_free_context_buffers(cm);
  1701. vp9_free_pc_tree(&cpi->td);
  1702. vpx_free(cpi->mbmi_ext_base);
  1703. alloc_compressor_data(cpi);
  1704. realloc_segmentation_maps(cpi);
  1705. cpi->initial_width = cpi->initial_height = 0;
  1706. cpi->external_resize = 0;
  1707. } else if (cm->mi_alloc_size == new_mi_size &&
  1708. (cpi->oxcf.width > last_w || cpi->oxcf.height > last_h)) {
  1709. vp9_alloc_loop_filter(cm);
  1710. }
  1711. }
  1712. if (cm->current_video_frame == 0 || last_w != cpi->oxcf.width ||
  1713. last_h != cpi->oxcf.height)
  1714. update_frame_size(cpi);
  1715. if (last_w != cpi->oxcf.width || last_h != cpi->oxcf.height) {
  1716. vpx_free(cpi->consec_zero_mv);
  1717. CHECK_MEM_ERROR(
  1718. cm, cpi->consec_zero_mv,
  1719. vpx_calloc(cm->mi_rows * cm->mi_cols, sizeof(*cpi->consec_zero_mv)));
  1720. vpx_free(cpi->skin_map);
  1721. CHECK_MEM_ERROR(
  1722. cm, cpi->skin_map,
  1723. vpx_calloc(cm->mi_rows * cm->mi_cols, sizeof(cpi->skin_map[0])));
  1724. free_copy_partition_data(cpi);
  1725. alloc_copy_partition_data(cpi);
  1726. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
  1727. vp9_cyclic_refresh_reset_resize(cpi);
  1728. rc->rc_1_frame = 0;
  1729. rc->rc_2_frame = 0;
  1730. }
  1731. if ((cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ||
  1732. ((cpi->svc.number_temporal_layers > 1 ||
  1733. cpi->svc.number_spatial_layers > 1) &&
  1734. cpi->oxcf.pass != 1)) {
  1735. vp9_update_layer_context_change_config(cpi,
  1736. (int)cpi->oxcf.target_bandwidth);
  1737. }
  1738. // Check for resetting the rc flags (rc_1_frame, rc_2_frame) if the
  1739. // configuration change has a large change in avg_frame_bandwidth.
  1740. // For SVC check for resetting based on spatial layer average bandwidth.
  1741. // Also reset buffer level to optimal level.
  1742. if (cm->current_video_frame > 0) {
  1743. if (cpi->use_svc) {
  1744. vp9_svc_check_reset_layer_rc_flag(cpi);
  1745. } else {
  1746. if (rc->avg_frame_bandwidth > (3 * rc->last_avg_frame_bandwidth >> 1) ||
  1747. rc->avg_frame_bandwidth < (rc->last_avg_frame_bandwidth >> 1)) {
  1748. rc->rc_1_frame = 0;
  1749. rc->rc_2_frame = 0;
  1750. rc->bits_off_target = rc->optimal_buffer_level;
  1751. rc->buffer_level = rc->optimal_buffer_level;
  1752. }
  1753. }
  1754. }
  1755. cpi->alt_ref_source = NULL;
  1756. rc->is_src_frame_alt_ref = 0;
  1757. #if 0
  1758. // Experimental RD Code
  1759. cpi->frame_distortion = 0;
  1760. cpi->last_frame_distortion = 0;
  1761. #endif
  1762. set_tile_limits(cpi);
  1763. cpi->ext_refresh_frame_flags_pending = 0;
  1764. cpi->ext_refresh_frame_context_pending = 0;
  1765. #if CONFIG_VP9_HIGHBITDEPTH
  1766. highbd_set_var_fns(cpi);
  1767. #endif
  1768. vp9_set_row_mt(cpi);
  1769. }
  1770. #ifndef M_LOG2_E
  1771. #define M_LOG2_E 0.693147180559945309417
  1772. #endif
  1773. #define log2f(x) (log(x) / (float)M_LOG2_E)
  1774. /***********************************************************************
  1775. * Read before modifying 'cal_nmvjointsadcost' or 'cal_nmvsadcosts' *
  1776. ***********************************************************************
  1777. * The following 2 functions ('cal_nmvjointsadcost' and *
  1778. * 'cal_nmvsadcosts') are used to calculate cost lookup tables *
  1779. * used by 'vp9_diamond_search_sad'. The C implementation of the *
  1780. * function is generic, but the AVX intrinsics optimised version *
  1781. * relies on the following properties of the computed tables: *
  1782. * For cal_nmvjointsadcost: *
  1783. * - mvjointsadcost[1] == mvjointsadcost[2] == mvjointsadcost[3] *
  1784. * For cal_nmvsadcosts: *
  1785. * - For all i: mvsadcost[0][i] == mvsadcost[1][i] *
  1786. * (Equal costs for both components) *
  1787. * - For all i: mvsadcost[0][i] == mvsadcost[0][-i] *
  1788. * (Cost function is even) *
  1789. * If these do not hold, then the AVX optimised version of the *
  1790. * 'vp9_diamond_search_sad' function cannot be used as it is, in which *
  1791. * case you can revert to using the C function instead. *
  1792. ***********************************************************************/
  1793. static void cal_nmvjointsadcost(int *mvjointsadcost) {
  1794. /*********************************************************************
  1795. * Warning: Read the comments above before modifying this function *
  1796. *********************************************************************/
  1797. mvjointsadcost[0] = 600;
  1798. mvjointsadcost[1] = 300;
  1799. mvjointsadcost[2] = 300;
  1800. mvjointsadcost[3] = 300;
  1801. }
  1802. static void cal_nmvsadcosts(int *mvsadcost[2]) {
  1803. /*********************************************************************
  1804. * Warning: Read the comments above before modifying this function *
  1805. *********************************************************************/
  1806. int i = 1;
  1807. mvsadcost[0][0] = 0;
  1808. mvsadcost[1][0] = 0;
  1809. do {
  1810. double z = 256 * (2 * (log2f(8 * i) + .6));
  1811. mvsadcost[0][i] = (int)z;
  1812. mvsadcost[1][i] = (int)z;
  1813. mvsadcost[0][-i] = (int)z;
  1814. mvsadcost[1][-i] = (int)z;
  1815. } while (++i <= MV_MAX);
  1816. }
  1817. static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
  1818. int i = 1;
  1819. mvsadcost[0][0] = 0;
  1820. mvsadcost[1][0] = 0;
  1821. do {
  1822. double z = 256 * (2 * (log2f(8 * i) + .6));
  1823. mvsadcost[0][i] = (int)z;
  1824. mvsadcost[1][i] = (int)z;
  1825. mvsadcost[0][-i] = (int)z;
  1826. mvsadcost[1][-i] = (int)z;
  1827. } while (++i <= MV_MAX);
  1828. }
  1829. VP9_COMP *vp9_create_compressor(VP9EncoderConfig *oxcf,
  1830. BufferPool *const pool) {
  1831. unsigned int i;
  1832. VP9_COMP *volatile const cpi = vpx_memalign(32, sizeof(VP9_COMP));
  1833. VP9_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
  1834. if (!cm) return NULL;
  1835. vp9_zero(*cpi);
  1836. if (setjmp(cm->error.jmp)) {
  1837. cm->error.setjmp = 0;
  1838. vp9_remove_compressor(cpi);
  1839. return 0;
  1840. }
  1841. cm->error.setjmp = 1;
  1842. cm->alloc_mi = vp9_enc_alloc_mi;
  1843. cm->free_mi = vp9_enc_free_mi;
  1844. cm->setup_mi = vp9_enc_setup_mi;
  1845. CHECK_MEM_ERROR(cm, cm->fc, (FRAME_CONTEXT *)vpx_calloc(1, sizeof(*cm->fc)));
  1846. CHECK_MEM_ERROR(
  1847. cm, cm->frame_contexts,
  1848. (FRAME_CONTEXT *)vpx_calloc(FRAME_CONTEXTS, sizeof(*cm->frame_contexts)));
  1849. cpi->use_svc = 0;
  1850. cpi->resize_state = ORIG;
  1851. cpi->external_resize = 0;
  1852. cpi->resize_avg_qp = 0;
  1853. cpi->resize_buffer_underflow = 0;
  1854. cpi->use_skin_detection = 0;
  1855. cpi->common.buffer_pool = pool;
  1856. cpi->force_update_segmentation = 0;
  1857. init_config(cpi, oxcf);
  1858. vp9_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
  1859. cm->current_video_frame = 0;
  1860. cpi->partition_search_skippable_frame = 0;
  1861. cpi->tile_data = NULL;
  1862. realloc_segmentation_maps(cpi);
  1863. CHECK_MEM_ERROR(
  1864. cm, cpi->skin_map,
  1865. vpx_calloc(cm->mi_rows * cm->mi_cols, sizeof(cpi->skin_map[0])));
  1866. #if !CONFIG_REALTIME_ONLY
  1867. CHECK_MEM_ERROR(cm, cpi->alt_ref_aq, vp9_alt_ref_aq_create());
  1868. #endif
  1869. CHECK_MEM_ERROR(
  1870. cm, cpi->consec_zero_mv,
  1871. vpx_calloc(cm->mi_rows * cm->mi_cols, sizeof(*cpi->consec_zero_mv)));
  1872. CHECK_MEM_ERROR(cm, cpi->nmvcosts[0],
  1873. vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[0])));
  1874. CHECK_MEM_ERROR(cm, cpi->nmvcosts[1],
  1875. vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[1])));
  1876. CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[0],
  1877. vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[0])));
  1878. CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[1],
  1879. vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[1])));
  1880. CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[0],
  1881. vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[0])));
  1882. CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[1],
  1883. vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[1])));
  1884. CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[0],
  1885. vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[0])));
  1886. CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[1],
  1887. vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[1])));
  1888. for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
  1889. i++) {
  1890. CHECK_MEM_ERROR(
  1891. cm, cpi->mbgraph_stats[i].mb_stats,
  1892. vpx_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
  1893. }
  1894. #if CONFIG_FP_MB_STATS
  1895. cpi->use_fp_mb_stats = 0;
  1896. if (cpi->use_fp_mb_stats) {
  1897. // a place holder used to store the first pass mb stats in the first pass
  1898. CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
  1899. vpx_calloc(cm->MBs * sizeof(uint8_t), 1));
  1900. } else {
  1901. cpi->twopass.frame_mb_stats_buf = NULL;
  1902. }
  1903. #endif
  1904. cpi->refresh_alt_ref_frame = 0;
  1905. cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
  1906. init_level_info(&cpi->level_info);
  1907. init_level_constraint(&cpi->level_constraint);
  1908. #if CONFIG_INTERNAL_STATS
  1909. cpi->b_calculate_blockiness = 1;
  1910. cpi->b_calculate_consistency = 1;
  1911. cpi->total_inconsistency = 0;
  1912. cpi->psnr.worst = 100.0;
  1913. cpi->worst_ssim = 100.0;
  1914. cpi->count = 0;
  1915. cpi->bytes = 0;
  1916. if (cpi->b_calculate_psnr) {
  1917. cpi->total_sq_error = 0;
  1918. cpi->total_samples = 0;
  1919. cpi->totalp_sq_error = 0;
  1920. cpi->totalp_samples = 0;
  1921. cpi->tot_recode_hits = 0;
  1922. cpi->summed_quality = 0;
  1923. cpi->summed_weights = 0;
  1924. cpi->summedp_quality = 0;
  1925. cpi->summedp_weights = 0;
  1926. }
  1927. cpi->fastssim.worst = 100.0;
  1928. cpi->psnrhvs.worst = 100.0;
  1929. if (cpi->b_calculate_blockiness) {
  1930. cpi->total_blockiness = 0;
  1931. cpi->worst_blockiness = 0.0;
  1932. }
  1933. if (cpi->b_calculate_consistency) {
  1934. CHECK_MEM_ERROR(cm, cpi->ssim_vars,
  1935. vpx_calloc(cpi->common.mi_rows * cpi->common.mi_cols,
  1936. sizeof(*cpi->ssim_vars) * 4));
  1937. cpi->worst_consistency = 100.0;
  1938. } else {
  1939. cpi->ssim_vars = NULL;
  1940. }
  1941. #endif
  1942. cpi->first_time_stamp_ever = INT64_MAX;
  1943. /*********************************************************************
  1944. * Warning: Read the comments around 'cal_nmvjointsadcost' and *
  1945. * 'cal_nmvsadcosts' before modifying how these tables are computed. *
  1946. *********************************************************************/
  1947. cal_nmvjointsadcost(cpi->td.mb.nmvjointsadcost);
  1948. cpi->td.mb.nmvcost[0] = &cpi->nmvcosts[0][MV_MAX];
  1949. cpi->td.mb.nmvcost[1] = &cpi->nmvcosts[1][MV_MAX];
  1950. cpi->td.mb.nmvsadcost[0] = &cpi->nmvsadcosts[0][MV_MAX];
  1951. cpi->td.mb.nmvsadcost[1] = &cpi->nmvsadcosts[1][MV_MAX];
  1952. cal_nmvsadcosts(cpi->td.mb.nmvsadcost);
  1953. cpi->td.mb.nmvcost_hp[0] = &cpi->nmvcosts_hp[0][MV_MAX];
  1954. cpi->td.mb.nmvcost_hp[1] = &cpi->nmvcosts_hp[1][MV_MAX];
  1955. cpi->td.mb.nmvsadcost_hp[0] = &cpi->nmvsadcosts_hp[0][MV_MAX];
  1956. cpi->td.mb.nmvsadcost_hp[1] = &cpi->nmvsadcosts_hp[1][MV_MAX];
  1957. cal_nmvsadcosts_hp(cpi->td.mb.nmvsadcost_hp);
  1958. #if CONFIG_VP9_TEMPORAL_DENOISING
  1959. #ifdef OUTPUT_YUV_DENOISED
  1960. yuv_denoised_file = fopen("denoised.yuv", "ab");
  1961. #endif
  1962. #endif
  1963. #ifdef OUTPUT_YUV_SKINMAP
  1964. yuv_skinmap_file = fopen("skinmap.yuv", "wb");
  1965. #endif
  1966. #ifdef OUTPUT_YUV_REC
  1967. yuv_rec_file = fopen("rec.yuv", "wb");
  1968. #endif
  1969. #ifdef OUTPUT_YUV_SVC_SRC
  1970. yuv_svc_src[0] = fopen("svc_src_0.yuv", "wb");
  1971. yuv_svc_src[1] = fopen("svc_src_1.yuv", "wb");
  1972. yuv_svc_src[2] = fopen("svc_src_2.yuv", "wb");
  1973. #endif
  1974. #if 0
  1975. framepsnr = fopen("framepsnr.stt", "a");
  1976. kf_list = fopen("kf_list.stt", "w");
  1977. #endif
  1978. cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
  1979. #if !CONFIG_REALTIME_ONLY
  1980. if (oxcf->pass == 1) {
  1981. vp9_init_first_pass(cpi);
  1982. } else if (oxcf->pass == 2) {
  1983. const size_t packet_sz = sizeof(FIRSTPASS_STATS);
  1984. const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
  1985. if (cpi->svc.number_spatial_layers > 1 ||
  1986. cpi->svc.number_temporal_layers > 1) {
  1987. FIRSTPASS_STATS *const stats = oxcf->two_pass_stats_in.buf;
  1988. FIRSTPASS_STATS *stats_copy[VPX_SS_MAX_LAYERS] = { 0 };
  1989. int i;
  1990. for (i = 0; i < oxcf->ss_number_layers; ++i) {
  1991. FIRSTPASS_STATS *const last_packet_for_layer =
  1992. &stats[packets - oxcf->ss_number_layers + i];
  1993. const int layer_id = (int)last_packet_for_layer->spatial_layer_id;
  1994. const int packets_in_layer = (int)last_packet_for_layer->count + 1;
  1995. if (layer_id >= 0 && layer_id < oxcf->ss_number_layers) {
  1996. LAYER_CONTEXT *const lc = &cpi->svc.layer_context[layer_id];
  1997. vpx_free(lc->rc_twopass_stats_in.buf);
  1998. lc->rc_twopass_stats_in.sz = packets_in_layer * packet_sz;
  1999. CHECK_MEM_ERROR(cm, lc->rc_twopass_stats_in.buf,
  2000. vpx_malloc(lc->rc_twopass_stats_in.sz));
  2001. lc->twopass.stats_in_start = lc->rc_twopass_stats_in.buf;
  2002. lc->twopass.stats_in = lc->twopass.stats_in_start;
  2003. lc->twopass.stats_in_end =
  2004. lc->twopass.stats_in_start + packets_in_layer - 1;
  2005. stats_copy[layer_id] = lc->rc_twopass_stats_in.buf;
  2006. }
  2007. }
  2008. for (i = 0; i < packets; ++i) {
  2009. const int layer_id = (int)stats[i].spatial_layer_id;
  2010. if (layer_id >= 0 && layer_id < oxcf->ss_number_layers &&
  2011. stats_copy[layer_id] != NULL) {
  2012. *stats_copy[layer_id] = stats[i];
  2013. ++stats_copy[layer_id];
  2014. }
  2015. }
  2016. vp9_init_second_pass_spatial_svc(cpi);
  2017. } else {
  2018. #if CONFIG_FP_MB_STATS
  2019. if (cpi->use_fp_mb_stats) {
  2020. const size_t psz = cpi->common.MBs * sizeof(uint8_t);
  2021. const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
  2022. cpi->twopass.firstpass_mb_stats.mb_stats_start =
  2023. oxcf->firstpass_mb_stats_in.buf;
  2024. cpi->twopass.firstpass_mb_stats.mb_stats_end =
  2025. cpi->twopass.firstpass_mb_stats.mb_stats_start +
  2026. (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
  2027. }
  2028. #endif
  2029. cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
  2030. cpi->twopass.stats_in = cpi->twopass.stats_in_start;
  2031. cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
  2032. vp9_init_second_pass(cpi);
  2033. }
  2034. }
  2035. #endif // !CONFIG_REALTIME_ONLY
  2036. cpi->mb_wiener_var_cols = 0;
  2037. cpi->mb_wiener_var_rows = 0;
  2038. cpi->mb_wiener_variance = NULL;
  2039. vp9_set_speed_features_framesize_independent(cpi, oxcf->speed);
  2040. vp9_set_speed_features_framesize_dependent(cpi, oxcf->speed);
  2041. {
  2042. const int bsize = BLOCK_16X16;
  2043. const int w = num_8x8_blocks_wide_lookup[bsize];
  2044. const int h = num_8x8_blocks_high_lookup[bsize];
  2045. const int num_cols = (cm->mi_cols + w - 1) / w;
  2046. const int num_rows = (cm->mi_rows + h - 1) / h;
  2047. CHECK_MEM_ERROR(cm, cpi->mi_ssim_rdmult_scaling_factors,
  2048. vpx_calloc(num_rows * num_cols,
  2049. sizeof(*cpi->mi_ssim_rdmult_scaling_factors)));
  2050. }
  2051. cpi->kmeans_data_arr_alloc = 0;
  2052. #if CONFIG_NON_GREEDY_MV
  2053. cpi->feature_score_loc_alloc = 0;
  2054. cpi->tpl_ready = 0;
  2055. #endif // CONFIG_NON_GREEDY_MV
  2056. for (i = 0; i < MAX_ARF_GOP_SIZE; ++i) cpi->tpl_stats[i].tpl_stats_ptr = NULL;
  2057. // Allocate memory to store variances for a frame.
  2058. CHECK_MEM_ERROR(cm, cpi->source_diff_var, vpx_calloc(cm->MBs, sizeof(diff)));
  2059. cpi->source_var_thresh = 0;
  2060. cpi->frames_till_next_var_check = 0;
  2061. #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF) \
  2062. cpi->fn_ptr[BT].sdf = SDF; \
  2063. cpi->fn_ptr[BT].sdaf = SDAF; \
  2064. cpi->fn_ptr[BT].vf = VF; \
  2065. cpi->fn_ptr[BT].svf = SVF; \
  2066. cpi->fn_ptr[BT].svaf = SVAF; \
  2067. cpi->fn_ptr[BT].sdx4df = SDX4DF;
  2068. BFP(BLOCK_32X16, vpx_sad32x16, vpx_sad32x16_avg, vpx_variance32x16,
  2069. vpx_sub_pixel_variance32x16, vpx_sub_pixel_avg_variance32x16,
  2070. vpx_sad32x16x4d)
  2071. BFP(BLOCK_16X32, vpx_sad16x32, vpx_sad16x32_avg, vpx_variance16x32,
  2072. vpx_sub_pixel_variance16x32, vpx_sub_pixel_avg_variance16x32,
  2073. vpx_sad16x32x4d)
  2074. BFP(BLOCK_64X32, vpx_sad64x32, vpx_sad64x32_avg, vpx_variance64x32,
  2075. vpx_sub_pixel_variance64x32, vpx_sub_pixel_avg_variance64x32,
  2076. vpx_sad64x32x4d)
  2077. BFP(BLOCK_32X64, vpx_sad32x64, vpx_sad32x64_avg, vpx_variance32x64,
  2078. vpx_sub_pixel_variance32x64, vpx_sub_pixel_avg_variance32x64,
  2079. vpx_sad32x64x4d)
  2080. BFP(BLOCK_32X32, vpx_sad32x32, vpx_sad32x32_avg, vpx_variance32x32,
  2081. vpx_sub_pixel_variance32x32, vpx_sub_pixel_avg_variance32x32,
  2082. vpx_sad32x32x4d)
  2083. BFP(BLOCK_64X64, vpx_sad64x64, vpx_sad64x64_avg, vpx_variance64x64,
  2084. vpx_sub_pixel_variance64x64, vpx_sub_pixel_avg_variance64x64,
  2085. vpx_sad64x64x4d)
  2086. BFP(BLOCK_16X16, vpx_sad16x16, vpx_sad16x16_avg, vpx_variance16x16,
  2087. vpx_sub_pixel_variance16x16, vpx_sub_pixel_avg_variance16x16,
  2088. vpx_sad16x16x4d)
  2089. BFP(BLOCK_16X8, vpx_sad16x8, vpx_sad16x8_avg, vpx_variance16x8,
  2090. vpx_sub_pixel_variance16x8, vpx_sub_pixel_avg_variance16x8,
  2091. vpx_sad16x8x4d)
  2092. BFP(BLOCK_8X16, vpx_sad8x16, vpx_sad8x16_avg, vpx_variance8x16,
  2093. vpx_sub_pixel_variance8x16, vpx_sub_pixel_avg_variance8x16,
  2094. vpx_sad8x16x4d)
  2095. BFP(BLOCK_8X8, vpx_sad8x8, vpx_sad8x8_avg, vpx_variance8x8,
  2096. vpx_sub_pixel_variance8x8, vpx_sub_pixel_avg_variance8x8, vpx_sad8x8x4d)
  2097. BFP(BLOCK_8X4, vpx_sad8x4, vpx_sad8x4_avg, vpx_variance8x4,
  2098. vpx_sub_pixel_variance8x4, vpx_sub_pixel_avg_variance8x4, vpx_sad8x4x4d)
  2099. BFP(BLOCK_4X8, vpx_sad4x8, vpx_sad4x8_avg, vpx_variance4x8,
  2100. vpx_sub_pixel_variance4x8, vpx_sub_pixel_avg_variance4x8, vpx_sad4x8x4d)
  2101. BFP(BLOCK_4X4, vpx_sad4x4, vpx_sad4x4_avg, vpx_variance4x4,
  2102. vpx_sub_pixel_variance4x4, vpx_sub_pixel_avg_variance4x4, vpx_sad4x4x4d)
  2103. #if CONFIG_VP9_HIGHBITDEPTH
  2104. highbd_set_var_fns(cpi);
  2105. #endif
  2106. /* vp9_init_quantizer() is first called here. Add check in
  2107. * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
  2108. * called later when needed. This will avoid unnecessary calls of
  2109. * vp9_init_quantizer() for every frame.
  2110. */
  2111. vp9_init_quantizer(cpi);
  2112. vp9_loop_filter_init(cm);
  2113. // Set up the unit scaling factor used during motion search.
  2114. #if CONFIG_VP9_HIGHBITDEPTH
  2115. vp9_setup_scale_factors_for_frame(&cpi->me_sf, cm->width, cm->height,
  2116. cm->width, cm->height,
  2117. cm->use_highbitdepth);
  2118. #else
  2119. vp9_setup_scale_factors_for_frame(&cpi->me_sf, cm->width, cm->height,
  2120. cm->width, cm->height);
  2121. #endif // CONFIG_VP9_HIGHBITDEPTH
  2122. cpi->td.mb.me_sf = &cpi->me_sf;
  2123. cm->error.setjmp = 0;
  2124. return cpi;
  2125. }
  2126. #if CONFIG_INTERNAL_STATS
  2127. #define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
  2128. #define SNPRINT2(H, T, V) \
  2129. snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
  2130. #endif // CONFIG_INTERNAL_STATS
  2131. void vp9_remove_compressor(VP9_COMP *cpi) {
  2132. VP9_COMMON *cm;
  2133. unsigned int i, frame;
  2134. int t;
  2135. if (!cpi) return;
  2136. #if CONFIG_INTERNAL_STATS
  2137. vpx_free(cpi->ssim_vars);
  2138. #endif
  2139. cm = &cpi->common;
  2140. if (cm->current_video_frame > 0) {
  2141. #if CONFIG_INTERNAL_STATS
  2142. vpx_clear_system_state();
  2143. if (cpi->oxcf.pass != 1) {
  2144. char headings[512] = { 0 };
  2145. char results[512] = { 0 };
  2146. FILE *f = fopen("opsnr.stt", "a");
  2147. double time_encoded =
  2148. (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
  2149. 10000000.000;
  2150. double total_encode_time =
  2151. (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
  2152. const double dr =
  2153. (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
  2154. const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
  2155. const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
  2156. const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
  2157. if (cpi->b_calculate_psnr) {
  2158. const double total_psnr = vpx_sse_to_psnr(
  2159. (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
  2160. const double totalp_psnr = vpx_sse_to_psnr(
  2161. (double)cpi->totalp_samples, peak, (double)cpi->totalp_sq_error);
  2162. const double total_ssim =
  2163. 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
  2164. const double totalp_ssim =
  2165. 100 * pow(cpi->summedp_quality / cpi->summedp_weights, 8.0);
  2166. snprintf(headings, sizeof(headings),
  2167. "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
  2168. "VPXSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
  2169. "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
  2170. "AVPsnrY\tAPsnrCb\tAPsnrCr");
  2171. snprintf(results, sizeof(results),
  2172. "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
  2173. "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
  2174. "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
  2175. "%7.3f\t%7.3f\t%7.3f",
  2176. dr, cpi->psnr.stat[ALL] / cpi->count, total_psnr,
  2177. cpi->psnrp.stat[ALL] / cpi->count, totalp_psnr, total_ssim,
  2178. totalp_ssim, cpi->fastssim.stat[ALL] / cpi->count,
  2179. cpi->psnrhvs.stat[ALL] / cpi->count, cpi->psnr.worst,
  2180. cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
  2181. cpi->psnr.stat[Y] / cpi->count, cpi->psnr.stat[U] / cpi->count,
  2182. cpi->psnr.stat[V] / cpi->count);
  2183. if (cpi->b_calculate_blockiness) {
  2184. SNPRINT(headings, "\t Block\tWstBlck");
  2185. SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
  2186. SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
  2187. }
  2188. if (cpi->b_calculate_consistency) {
  2189. double consistency =
  2190. vpx_sse_to_psnr((double)cpi->totalp_samples, peak,
  2191. (double)cpi->total_inconsistency);
  2192. SNPRINT(headings, "\tConsist\tWstCons");
  2193. SNPRINT2(results, "\t%7.3f", consistency);
  2194. SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
  2195. }
  2196. fprintf(f, "%s\t Time\tRcErr\tAbsErr\n", headings);
  2197. fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
  2198. rate_err, fabs(rate_err));
  2199. }
  2200. fclose(f);
  2201. }
  2202. #endif
  2203. #if 0
  2204. {
  2205. printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
  2206. printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
  2207. printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
  2208. cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
  2209. cpi->time_compress_data / 1000,
  2210. (cpi->time_receive_data + cpi->time_compress_data) / 1000);
  2211. }
  2212. #endif
  2213. }
  2214. #if CONFIG_VP9_TEMPORAL_DENOISING
  2215. vp9_denoiser_free(&(cpi->denoiser));
  2216. #endif
  2217. if (cpi->kmeans_data_arr_alloc) {
  2218. #if CONFIG_MULTITHREAD
  2219. pthread_mutex_destroy(&cpi->kmeans_mutex);
  2220. #endif
  2221. vpx_free(cpi->kmeans_data_arr);
  2222. }
  2223. #if CONFIG_NON_GREEDY_MV
  2224. vpx_free(cpi->feature_score_loc_arr);
  2225. vpx_free(cpi->feature_score_loc_sort);
  2226. vpx_free(cpi->feature_score_loc_heap);
  2227. vpx_free(cpi->select_mv_arr);
  2228. #endif
  2229. for (frame = 0; frame < MAX_ARF_GOP_SIZE; ++frame) {
  2230. #if CONFIG_NON_GREEDY_MV
  2231. int rf_idx;
  2232. for (rf_idx = 0; rf_idx < 3; ++rf_idx) {
  2233. int sqr_bsize;
  2234. for (sqr_bsize = 0; sqr_bsize < SQUARE_BLOCK_SIZES; ++sqr_bsize) {
  2235. vpx_free(cpi->tpl_stats[frame].pyramid_mv_arr[rf_idx][sqr_bsize]);
  2236. }
  2237. vpx_free(cpi->tpl_stats[frame].mv_mode_arr[rf_idx]);
  2238. vpx_free(cpi->tpl_stats[frame].rd_diff_arr[rf_idx]);
  2239. }
  2240. #endif
  2241. vpx_free(cpi->tpl_stats[frame].tpl_stats_ptr);
  2242. cpi->tpl_stats[frame].is_valid = 0;
  2243. }
  2244. for (t = 0; t < cpi->num_workers; ++t) {
  2245. VPxWorker *const worker = &cpi->workers[t];
  2246. EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
  2247. // Deallocate allocated threads.
  2248. vpx_get_worker_interface()->end(worker);
  2249. // Deallocate allocated thread data.
  2250. if (t < cpi->num_workers - 1) {
  2251. vpx_free(thread_data->td->counts);
  2252. vp9_free_pc_tree(thread_data->td);
  2253. vpx_free(thread_data->td);
  2254. }
  2255. }
  2256. vpx_free(cpi->tile_thr_data);
  2257. vpx_free(cpi->workers);
  2258. vp9_row_mt_mem_dealloc(cpi);
  2259. if (cpi->num_workers > 1) {
  2260. vp9_loop_filter_dealloc(&cpi->lf_row_sync);
  2261. vp9_bitstream_encode_tiles_buffer_dealloc(cpi);
  2262. }
  2263. #if !CONFIG_REALTIME_ONLY
  2264. vp9_alt_ref_aq_destroy(cpi->alt_ref_aq);
  2265. #endif
  2266. dealloc_compressor_data(cpi);
  2267. for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
  2268. ++i) {
  2269. vpx_free(cpi->mbgraph_stats[i].mb_stats);
  2270. }
  2271. #if CONFIG_FP_MB_STATS
  2272. if (cpi->use_fp_mb_stats) {
  2273. vpx_free(cpi->twopass.frame_mb_stats_buf);
  2274. cpi->twopass.frame_mb_stats_buf = NULL;
  2275. }
  2276. #endif
  2277. vp9_remove_common(cm);
  2278. vp9_free_ref_frame_buffers(cm->buffer_pool);
  2279. #if CONFIG_VP9_POSTPROC
  2280. vp9_free_postproc_buffers(cm);
  2281. #endif
  2282. vpx_free(cpi);
  2283. #if CONFIG_VP9_TEMPORAL_DENOISING
  2284. #ifdef OUTPUT_YUV_DENOISED
  2285. fclose(yuv_denoised_file);
  2286. #endif
  2287. #endif
  2288. #ifdef OUTPUT_YUV_SKINMAP
  2289. fclose(yuv_skinmap_file);
  2290. #endif
  2291. #ifdef OUTPUT_YUV_REC
  2292. fclose(yuv_rec_file);
  2293. #endif
  2294. #ifdef OUTPUT_YUV_SVC_SRC
  2295. fclose(yuv_svc_src[0]);
  2296. fclose(yuv_svc_src[1]);
  2297. fclose(yuv_svc_src[2]);
  2298. #endif
  2299. #if 0
  2300. if (keyfile)
  2301. fclose(keyfile);
  2302. if (framepsnr)
  2303. fclose(framepsnr);
  2304. if (kf_list)
  2305. fclose(kf_list);
  2306. #endif
  2307. }
  2308. static void generate_psnr_packet(VP9_COMP *cpi) {
  2309. struct vpx_codec_cx_pkt pkt;
  2310. int i;
  2311. PSNR_STATS psnr;
  2312. #if CONFIG_VP9_HIGHBITDEPTH
  2313. vpx_calc_highbd_psnr(cpi->raw_source_frame, cpi->common.frame_to_show, &psnr,
  2314. cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
  2315. #else
  2316. vpx_calc_psnr(cpi->raw_source_frame, cpi->common.frame_to_show, &psnr);
  2317. #endif
  2318. for (i = 0; i < 4; ++i) {
  2319. pkt.data.psnr.samples[i] = psnr.samples[i];
  2320. pkt.data.psnr.sse[i] = psnr.sse[i];
  2321. pkt.data.psnr.psnr[i] = psnr.psnr[i];
  2322. }
  2323. pkt.kind = VPX_CODEC_PSNR_PKT;
  2324. if (cpi->use_svc)
  2325. cpi->svc
  2326. .layer_context[cpi->svc.spatial_layer_id *
  2327. cpi->svc.number_temporal_layers]
  2328. .psnr_pkt = pkt.data.psnr;
  2329. else
  2330. vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
  2331. }
  2332. int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags) {
  2333. if (ref_frame_flags > 7) return -1;
  2334. cpi->ref_frame_flags = ref_frame_flags;
  2335. return 0;
  2336. }
  2337. void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags) {
  2338. cpi->ext_refresh_golden_frame = (ref_frame_flags & VP9_GOLD_FLAG) != 0;
  2339. cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & VP9_ALT_FLAG) != 0;
  2340. cpi->ext_refresh_last_frame = (ref_frame_flags & VP9_LAST_FLAG) != 0;
  2341. cpi->ext_refresh_frame_flags_pending = 1;
  2342. }
  2343. static YV12_BUFFER_CONFIG *get_vp9_ref_frame_buffer(
  2344. VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag) {
  2345. MV_REFERENCE_FRAME ref_frame = NONE;
  2346. if (ref_frame_flag == VP9_LAST_FLAG)
  2347. ref_frame = LAST_FRAME;
  2348. else if (ref_frame_flag == VP9_GOLD_FLAG)
  2349. ref_frame = GOLDEN_FRAME;
  2350. else if (ref_frame_flag == VP9_ALT_FLAG)
  2351. ref_frame = ALTREF_FRAME;
  2352. return ref_frame == NONE ? NULL : get_ref_frame_buffer(cpi, ref_frame);
  2353. }
  2354. int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
  2355. YV12_BUFFER_CONFIG *sd) {
  2356. YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
  2357. if (cfg) {
  2358. vpx_yv12_copy_frame(cfg, sd);
  2359. return 0;
  2360. } else {
  2361. return -1;
  2362. }
  2363. }
  2364. int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
  2365. YV12_BUFFER_CONFIG *sd) {
  2366. YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
  2367. if (cfg) {
  2368. vpx_yv12_copy_frame(sd, cfg);
  2369. return 0;
  2370. } else {
  2371. return -1;
  2372. }
  2373. }
  2374. int vp9_update_entropy(VP9_COMP *cpi, int update) {
  2375. cpi->ext_refresh_frame_context = update;
  2376. cpi->ext_refresh_frame_context_pending = 1;
  2377. return 0;
  2378. }
  2379. #ifdef OUTPUT_YUV_REC
  2380. void vp9_write_yuv_rec_frame(VP9_COMMON *cm) {
  2381. YV12_BUFFER_CONFIG *s = cm->frame_to_show;
  2382. uint8_t *src = s->y_buffer;
  2383. int h = cm->height;
  2384. #if CONFIG_VP9_HIGHBITDEPTH
  2385. if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
  2386. uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
  2387. do {
  2388. fwrite(src16, s->y_width, 2, yuv_rec_file);
  2389. src16 += s->y_stride;
  2390. } while (--h);
  2391. src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
  2392. h = s->uv_height;
  2393. do {
  2394. fwrite(src16, s->uv_width, 2, yuv_rec_file);
  2395. src16 += s->uv_stride;
  2396. } while (--h);
  2397. src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
  2398. h = s->uv_height;
  2399. do {
  2400. fwrite(src16, s->uv_width, 2, yuv_rec_file);
  2401. src16 += s->uv_stride;
  2402. } while (--h);
  2403. fflush(yuv_rec_file);
  2404. return;
  2405. }
  2406. #endif // CONFIG_VP9_HIGHBITDEPTH
  2407. do {
  2408. fwrite(src, s->y_width, 1, yuv_rec_file);
  2409. src += s->y_stride;
  2410. } while (--h);
  2411. src = s->u_buffer;
  2412. h = s->uv_height;
  2413. do {
  2414. fwrite(src, s->uv_width, 1, yuv_rec_file);
  2415. src += s->uv_stride;
  2416. } while (--h);
  2417. src = s->v_buffer;
  2418. h = s->uv_height;
  2419. do {
  2420. fwrite(src, s->uv_width, 1, yuv_rec_file);
  2421. src += s->uv_stride;
  2422. } while (--h);
  2423. fflush(yuv_rec_file);
  2424. }
  2425. #endif
  2426. #if CONFIG_VP9_HIGHBITDEPTH
  2427. static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
  2428. YV12_BUFFER_CONFIG *dst,
  2429. int bd) {
  2430. #else
  2431. static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
  2432. YV12_BUFFER_CONFIG *dst) {
  2433. #endif // CONFIG_VP9_HIGHBITDEPTH
  2434. // TODO(dkovalev): replace YV12_BUFFER_CONFIG with vpx_image_t
  2435. int i;
  2436. const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
  2437. src->v_buffer };
  2438. const int src_strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
  2439. const int src_widths[3] = { src->y_crop_width, src->uv_crop_width,
  2440. src->uv_crop_width };
  2441. const int src_heights[3] = { src->y_crop_height, src->uv_crop_height,
  2442. src->uv_crop_height };
  2443. uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
  2444. const int dst_strides[3] = { dst->y_stride, dst->uv_stride, dst->uv_stride };
  2445. const int dst_widths[3] = { dst->y_crop_width, dst->uv_crop_width,
  2446. dst->uv_crop_width };
  2447. const int dst_heights[3] = { dst->y_crop_height, dst->uv_crop_height,
  2448. dst->uv_crop_height };
  2449. for (i = 0; i < MAX_MB_PLANE; ++i) {
  2450. #if CONFIG_VP9_HIGHBITDEPTH
  2451. if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
  2452. vp9_highbd_resize_plane(srcs[i], src_heights[i], src_widths[i],
  2453. src_strides[i], dsts[i], dst_heights[i],
  2454. dst_widths[i], dst_strides[i], bd);
  2455. } else {
  2456. vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
  2457. dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
  2458. }
  2459. #else
  2460. vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
  2461. dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
  2462. #endif // CONFIG_VP9_HIGHBITDEPTH
  2463. }
  2464. vpx_extend_frame_borders(dst);
  2465. }
  2466. #if CONFIG_VP9_HIGHBITDEPTH
  2467. static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
  2468. YV12_BUFFER_CONFIG *dst, int bd,
  2469. INTERP_FILTER filter_type,
  2470. int phase_scaler) {
  2471. const int src_w = src->y_crop_width;
  2472. const int src_h = src->y_crop_height;
  2473. const int dst_w = dst->y_crop_width;
  2474. const int dst_h = dst->y_crop_height;
  2475. const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
  2476. src->v_buffer };
  2477. const int src_strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
  2478. uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
  2479. const int dst_strides[3] = { dst->y_stride, dst->uv_stride, dst->uv_stride };
  2480. const InterpKernel *const kernel = vp9_filter_kernels[filter_type];
  2481. int x, y, i;
  2482. for (i = 0; i < MAX_MB_PLANE; ++i) {
  2483. const int factor = (i == 0 || i == 3 ? 1 : 2);
  2484. const int src_stride = src_strides[i];
  2485. const int dst_stride = dst_strides[i];
  2486. for (y = 0; y < dst_h; y += 16) {
  2487. const int y_q4 = y * (16 / factor) * src_h / dst_h + phase_scaler;
  2488. for (x = 0; x < dst_w; x += 16) {
  2489. const int x_q4 = x * (16 / factor) * src_w / dst_w + phase_scaler;
  2490. const uint8_t *src_ptr = srcs[i] +
  2491. (y / factor) * src_h / dst_h * src_stride +
  2492. (x / factor) * src_w / dst_w;
  2493. uint8_t *dst_ptr = dsts[i] + (y / factor) * dst_stride + (x / factor);
  2494. if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
  2495. vpx_highbd_convolve8(CONVERT_TO_SHORTPTR(src_ptr), src_stride,
  2496. CONVERT_TO_SHORTPTR(dst_ptr), dst_stride, kernel,
  2497. x_q4 & 0xf, 16 * src_w / dst_w, y_q4 & 0xf,
  2498. 16 * src_h / dst_h, 16 / factor, 16 / factor,
  2499. bd);
  2500. } else {
  2501. vpx_scaled_2d(src_ptr, src_stride, dst_ptr, dst_stride, kernel,
  2502. x_q4 & 0xf, 16 * src_w / dst_w, y_q4 & 0xf,
  2503. 16 * src_h / dst_h, 16 / factor, 16 / factor);
  2504. }
  2505. }
  2506. }
  2507. }
  2508. vpx_extend_frame_borders(dst);
  2509. }
  2510. #endif // CONFIG_VP9_HIGHBITDEPTH
  2511. #if !CONFIG_REALTIME_ONLY
  2512. static int scale_down(VP9_COMP *cpi, int q) {
  2513. RATE_CONTROL *const rc = &cpi->rc;
  2514. GF_GROUP *const gf_group = &cpi->twopass.gf_group;
  2515. int scale = 0;
  2516. assert(frame_is_kf_gf_arf(cpi));
  2517. if (rc->frame_size_selector == UNSCALED &&
  2518. q >= rc->rf_level_maxq[gf_group->rf_level[gf_group->index]]) {
  2519. const int max_size_thresh =
  2520. (int)(rate_thresh_mult[SCALE_STEP1] *
  2521. VPXMAX(rc->this_frame_target, rc->avg_frame_bandwidth));
  2522. scale = rc->projected_frame_size > max_size_thresh ? 1 : 0;
  2523. }
  2524. return scale;
  2525. }
  2526. static int big_rate_miss_high_threshold(VP9_COMP *cpi) {
  2527. const RATE_CONTROL *const rc = &cpi->rc;
  2528. int big_miss_high;
  2529. if (frame_is_kf_gf_arf(cpi))
  2530. big_miss_high = rc->this_frame_target * 3 / 2;
  2531. else
  2532. big_miss_high = rc->this_frame_target * 2;
  2533. return big_miss_high;
  2534. }
  2535. static int big_rate_miss(VP9_COMP *cpi) {
  2536. const RATE_CONTROL *const rc = &cpi->rc;
  2537. int big_miss_high;
  2538. int big_miss_low;
  2539. // Ignore for overlay frames
  2540. if (rc->is_src_frame_alt_ref) {
  2541. return 0;
  2542. } else {
  2543. big_miss_low = (rc->this_frame_target / 2);
  2544. big_miss_high = big_rate_miss_high_threshold(cpi);
  2545. return (rc->projected_frame_size > big_miss_high) ||
  2546. (rc->projected_frame_size < big_miss_low);
  2547. }
  2548. }
  2549. // test in two pass for the first
  2550. static int two_pass_first_group_inter(VP9_COMP *cpi) {
  2551. if (cpi->oxcf.pass == 2) {
  2552. TWO_PASS *const twopass = &cpi->twopass;
  2553. GF_GROUP *const gf_group = &twopass->gf_group;
  2554. const int gfg_index = gf_group->index;
  2555. if (gfg_index == 0) return gf_group->update_type[gfg_index] == LF_UPDATE;
  2556. return gf_group->update_type[gfg_index - 1] != LF_UPDATE &&
  2557. gf_group->update_type[gfg_index] == LF_UPDATE;
  2558. } else {
  2559. return 0;
  2560. }
  2561. }
  2562. // Function to test for conditions that indicate we should loop
  2563. // back and recode a frame.
  2564. static int recode_loop_test(VP9_COMP *cpi, int high_limit, int low_limit, int q,
  2565. int maxq, int minq) {
  2566. const RATE_CONTROL *const rc = &cpi->rc;
  2567. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  2568. const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
  2569. int force_recode = 0;
  2570. if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
  2571. big_rate_miss(cpi) || (cpi->sf.recode_loop == ALLOW_RECODE) ||
  2572. (two_pass_first_group_inter(cpi) &&
  2573. (cpi->sf.recode_loop == ALLOW_RECODE_FIRST)) ||
  2574. (frame_is_kfgfarf && (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF))) {
  2575. if (frame_is_kfgfarf && (oxcf->resize_mode == RESIZE_DYNAMIC) &&
  2576. scale_down(cpi, q)) {
  2577. // Code this group at a lower resolution.
  2578. cpi->resize_pending = 1;
  2579. return 1;
  2580. }
  2581. // Force recode for extreme overshoot.
  2582. if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
  2583. (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF &&
  2584. rc->projected_frame_size >= big_rate_miss_high_threshold(cpi))) {
  2585. return 1;
  2586. }
  2587. // TODO(agrange) high_limit could be greater than the scale-down threshold.
  2588. if ((rc->projected_frame_size > high_limit && q < maxq) ||
  2589. (rc->projected_frame_size < low_limit && q > minq)) {
  2590. force_recode = 1;
  2591. } else if (cpi->oxcf.rc_mode == VPX_CQ) {
  2592. // Deal with frame undershoot and whether or not we are
  2593. // below the automatically set cq level.
  2594. if (q > oxcf->cq_level &&
  2595. rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
  2596. force_recode = 1;
  2597. }
  2598. }
  2599. }
  2600. return force_recode;
  2601. }
  2602. #endif // !CONFIG_REALTIME_ONLY
  2603. static void update_ref_frames(VP9_COMP *cpi) {
  2604. VP9_COMMON *const cm = &cpi->common;
  2605. BufferPool *const pool = cm->buffer_pool;
  2606. GF_GROUP *const gf_group = &cpi->twopass.gf_group;
  2607. if (cpi->rc.show_arf_as_gld) {
  2608. int tmp = cpi->alt_fb_idx;
  2609. cpi->alt_fb_idx = cpi->gld_fb_idx;
  2610. cpi->gld_fb_idx = tmp;
  2611. } else if (cm->show_existing_frame) {
  2612. // Pop ARF.
  2613. cpi->lst_fb_idx = cpi->alt_fb_idx;
  2614. cpi->alt_fb_idx =
  2615. stack_pop(gf_group->arf_index_stack, gf_group->stack_size);
  2616. --gf_group->stack_size;
  2617. }
  2618. // At this point the new frame has been encoded.
  2619. // If any buffer copy / swapping is signaled it should be done here.
  2620. if (cm->frame_type == KEY_FRAME) {
  2621. ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
  2622. cm->new_fb_idx);
  2623. ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
  2624. cm->new_fb_idx);
  2625. } else if (vp9_preserve_existing_gf(cpi)) {
  2626. // We have decided to preserve the previously existing golden frame as our
  2627. // new ARF frame. However, in the short term in function
  2628. // vp9_get_refresh_mask() we left it in the GF slot and, if
  2629. // we're updating the GF with the current decoded frame, we save it to the
  2630. // ARF slot instead.
  2631. // We now have to update the ARF with the current frame and swap gld_fb_idx
  2632. // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
  2633. // slot and, if we're updating the GF, the current frame becomes the new GF.
  2634. int tmp;
  2635. ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
  2636. cm->new_fb_idx);
  2637. tmp = cpi->alt_fb_idx;
  2638. cpi->alt_fb_idx = cpi->gld_fb_idx;
  2639. cpi->gld_fb_idx = tmp;
  2640. } else { /* For non key/golden frames */
  2641. if (cpi->refresh_alt_ref_frame) {
  2642. int arf_idx = gf_group->top_arf_idx;
  2643. // Push new ARF into stack.
  2644. stack_push(gf_group->arf_index_stack, cpi->alt_fb_idx,
  2645. gf_group->stack_size);
  2646. ++gf_group->stack_size;
  2647. assert(arf_idx < REF_FRAMES);
  2648. ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
  2649. memcpy(cpi->interp_filter_selected[ALTREF_FRAME],
  2650. cpi->interp_filter_selected[0],
  2651. sizeof(cpi->interp_filter_selected[0]));
  2652. cpi->alt_fb_idx = arf_idx;
  2653. }
  2654. if (cpi->refresh_golden_frame) {
  2655. ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
  2656. cm->new_fb_idx);
  2657. if (!cpi->rc.is_src_frame_alt_ref)
  2658. memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
  2659. cpi->interp_filter_selected[0],
  2660. sizeof(cpi->interp_filter_selected[0]));
  2661. else
  2662. memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
  2663. cpi->interp_filter_selected[ALTREF_FRAME],
  2664. sizeof(cpi->interp_filter_selected[ALTREF_FRAME]));
  2665. }
  2666. }
  2667. if (cpi->refresh_last_frame) {
  2668. ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->lst_fb_idx],
  2669. cm->new_fb_idx);
  2670. if (!cpi->rc.is_src_frame_alt_ref)
  2671. memcpy(cpi->interp_filter_selected[LAST_FRAME],
  2672. cpi->interp_filter_selected[0],
  2673. sizeof(cpi->interp_filter_selected[0]));
  2674. }
  2675. if (gf_group->update_type[gf_group->index] == MID_OVERLAY_UPDATE) {
  2676. cpi->alt_fb_idx =
  2677. stack_pop(gf_group->arf_index_stack, gf_group->stack_size);
  2678. --gf_group->stack_size;
  2679. }
  2680. }
  2681. void vp9_update_reference_frames(VP9_COMP *cpi) {
  2682. update_ref_frames(cpi);
  2683. #if CONFIG_VP9_TEMPORAL_DENOISING
  2684. vp9_denoiser_update_ref_frame(cpi);
  2685. #endif
  2686. if (is_one_pass_cbr_svc(cpi)) vp9_svc_update_ref_frame(cpi);
  2687. }
  2688. static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) {
  2689. MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
  2690. struct loopfilter *lf = &cm->lf;
  2691. int is_reference_frame =
  2692. (cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
  2693. cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame);
  2694. if (cpi->use_svc &&
  2695. cpi->svc.temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS)
  2696. is_reference_frame = !cpi->svc.non_reference_frame;
  2697. // Skip loop filter in show_existing_frame mode.
  2698. if (cm->show_existing_frame) {
  2699. lf->filter_level = 0;
  2700. return;
  2701. }
  2702. if (xd->lossless) {
  2703. lf->filter_level = 0;
  2704. lf->last_filt_level = 0;
  2705. } else {
  2706. struct vpx_usec_timer timer;
  2707. vpx_clear_system_state();
  2708. vpx_usec_timer_start(&timer);
  2709. if (!cpi->rc.is_src_frame_alt_ref) {
  2710. if ((cpi->common.frame_type == KEY_FRAME) &&
  2711. (!cpi->rc.this_key_frame_forced)) {
  2712. lf->last_filt_level = 0;
  2713. }
  2714. vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.lpf_pick);
  2715. lf->last_filt_level = lf->filter_level;
  2716. } else {
  2717. lf->filter_level = 0;
  2718. }
  2719. vpx_usec_timer_mark(&timer);
  2720. cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
  2721. }
  2722. if (lf->filter_level > 0 && is_reference_frame) {
  2723. vp9_build_mask_frame(cm, lf->filter_level, 0);
  2724. if (cpi->num_workers > 1)
  2725. vp9_loop_filter_frame_mt(cm->frame_to_show, cm, xd->plane,
  2726. lf->filter_level, 0, 0, cpi->workers,
  2727. cpi->num_workers, &cpi->lf_row_sync);
  2728. else
  2729. vp9_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
  2730. }
  2731. vpx_extend_frame_inner_borders(cm->frame_to_show);
  2732. }
  2733. static INLINE void alloc_frame_mvs(VP9_COMMON *const cm, int buffer_idx) {
  2734. RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
  2735. if (new_fb_ptr->mvs == NULL || new_fb_ptr->mi_rows < cm->mi_rows ||
  2736. new_fb_ptr->mi_cols < cm->mi_cols) {
  2737. vpx_free(new_fb_ptr->mvs);
  2738. CHECK_MEM_ERROR(cm, new_fb_ptr->mvs,
  2739. (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols,
  2740. sizeof(*new_fb_ptr->mvs)));
  2741. new_fb_ptr->mi_rows = cm->mi_rows;
  2742. new_fb_ptr->mi_cols = cm->mi_cols;
  2743. }
  2744. }
  2745. void vp9_scale_references(VP9_COMP *cpi) {
  2746. VP9_COMMON *cm = &cpi->common;
  2747. MV_REFERENCE_FRAME ref_frame;
  2748. const VP9_REFFRAME ref_mask[3] = { VP9_LAST_FLAG, VP9_GOLD_FLAG,
  2749. VP9_ALT_FLAG };
  2750. for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
  2751. // Need to convert from VP9_REFFRAME to index into ref_mask (subtract 1).
  2752. if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
  2753. BufferPool *const pool = cm->buffer_pool;
  2754. const YV12_BUFFER_CONFIG *const ref =
  2755. get_ref_frame_buffer(cpi, ref_frame);
  2756. if (ref == NULL) {
  2757. cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
  2758. continue;
  2759. }
  2760. #if CONFIG_VP9_HIGHBITDEPTH
  2761. if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
  2762. RefCntBuffer *new_fb_ptr = NULL;
  2763. int force_scaling = 0;
  2764. int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
  2765. if (new_fb == INVALID_IDX) {
  2766. new_fb = get_free_fb(cm);
  2767. force_scaling = 1;
  2768. }
  2769. if (new_fb == INVALID_IDX) return;
  2770. new_fb_ptr = &pool->frame_bufs[new_fb];
  2771. if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
  2772. new_fb_ptr->buf.y_crop_height != cm->height) {
  2773. if (vpx_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
  2774. cm->subsampling_x, cm->subsampling_y,
  2775. cm->use_highbitdepth,
  2776. VP9_ENC_BORDER_IN_PIXELS,
  2777. cm->byte_alignment, NULL, NULL, NULL))
  2778. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  2779. "Failed to allocate frame buffer");
  2780. scale_and_extend_frame(ref, &new_fb_ptr->buf, (int)cm->bit_depth,
  2781. EIGHTTAP, 0);
  2782. cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
  2783. alloc_frame_mvs(cm, new_fb);
  2784. }
  2785. #else
  2786. if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
  2787. RefCntBuffer *new_fb_ptr = NULL;
  2788. int force_scaling = 0;
  2789. int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
  2790. if (new_fb == INVALID_IDX) {
  2791. new_fb = get_free_fb(cm);
  2792. force_scaling = 1;
  2793. }
  2794. if (new_fb == INVALID_IDX) return;
  2795. new_fb_ptr = &pool->frame_bufs[new_fb];
  2796. if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
  2797. new_fb_ptr->buf.y_crop_height != cm->height) {
  2798. if (vpx_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
  2799. cm->subsampling_x, cm->subsampling_y,
  2800. VP9_ENC_BORDER_IN_PIXELS,
  2801. cm->byte_alignment, NULL, NULL, NULL))
  2802. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  2803. "Failed to allocate frame buffer");
  2804. vp9_scale_and_extend_frame(ref, &new_fb_ptr->buf, EIGHTTAP, 0);
  2805. cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
  2806. alloc_frame_mvs(cm, new_fb);
  2807. }
  2808. #endif // CONFIG_VP9_HIGHBITDEPTH
  2809. } else {
  2810. int buf_idx;
  2811. RefCntBuffer *buf = NULL;
  2812. if (cpi->oxcf.pass == 0 && !cpi->use_svc) {
  2813. // Check for release of scaled reference.
  2814. buf_idx = cpi->scaled_ref_idx[ref_frame - 1];
  2815. if (buf_idx != INVALID_IDX) {
  2816. buf = &pool->frame_bufs[buf_idx];
  2817. --buf->ref_count;
  2818. cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
  2819. }
  2820. }
  2821. buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
  2822. buf = &pool->frame_bufs[buf_idx];
  2823. buf->buf.y_crop_width = ref->y_crop_width;
  2824. buf->buf.y_crop_height = ref->y_crop_height;
  2825. cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
  2826. ++buf->ref_count;
  2827. }
  2828. } else {
  2829. if (cpi->oxcf.pass != 0 || cpi->use_svc)
  2830. cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
  2831. }
  2832. }
  2833. }
  2834. static void release_scaled_references(VP9_COMP *cpi) {
  2835. VP9_COMMON *cm = &cpi->common;
  2836. int i;
  2837. if (cpi->oxcf.pass == 0 && !cpi->use_svc) {
  2838. // Only release scaled references under certain conditions:
  2839. // if reference will be updated, or if scaled reference has same resolution.
  2840. int refresh[3];
  2841. refresh[0] = (cpi->refresh_last_frame) ? 1 : 0;
  2842. refresh[1] = (cpi->refresh_golden_frame) ? 1 : 0;
  2843. refresh[2] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
  2844. for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
  2845. const int idx = cpi->scaled_ref_idx[i - 1];
  2846. if (idx != INVALID_IDX) {
  2847. RefCntBuffer *const buf = &cm->buffer_pool->frame_bufs[idx];
  2848. const YV12_BUFFER_CONFIG *const ref = get_ref_frame_buffer(cpi, i);
  2849. if (refresh[i - 1] || (buf->buf.y_crop_width == ref->y_crop_width &&
  2850. buf->buf.y_crop_height == ref->y_crop_height)) {
  2851. --buf->ref_count;
  2852. cpi->scaled_ref_idx[i - 1] = INVALID_IDX;
  2853. }
  2854. }
  2855. }
  2856. } else {
  2857. for (i = 0; i < REFS_PER_FRAME; ++i) {
  2858. const int idx = cpi->scaled_ref_idx[i];
  2859. if (idx != INVALID_IDX) {
  2860. RefCntBuffer *const buf = &cm->buffer_pool->frame_bufs[idx];
  2861. --buf->ref_count;
  2862. cpi->scaled_ref_idx[i] = INVALID_IDX;
  2863. }
  2864. }
  2865. }
  2866. }
  2867. static void full_to_model_count(unsigned int *model_count,
  2868. unsigned int *full_count) {
  2869. int n;
  2870. model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
  2871. model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
  2872. model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
  2873. for (n = THREE_TOKEN; n < EOB_TOKEN; ++n)
  2874. model_count[TWO_TOKEN] += full_count[n];
  2875. model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
  2876. }
  2877. static void full_to_model_counts(vp9_coeff_count_model *model_count,
  2878. vp9_coeff_count *full_count) {
  2879. int i, j, k, l;
  2880. for (i = 0; i < PLANE_TYPES; ++i)
  2881. for (j = 0; j < REF_TYPES; ++j)
  2882. for (k = 0; k < COEF_BANDS; ++k)
  2883. for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
  2884. full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
  2885. }
  2886. #if 0 && CONFIG_INTERNAL_STATS
  2887. static void output_frame_level_debug_stats(VP9_COMP *cpi) {
  2888. VP9_COMMON *const cm = &cpi->common;
  2889. FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
  2890. int64_t recon_err;
  2891. vpx_clear_system_state();
  2892. #if CONFIG_VP9_HIGHBITDEPTH
  2893. if (cm->use_highbitdepth) {
  2894. recon_err = vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
  2895. } else {
  2896. recon_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
  2897. }
  2898. #else
  2899. recon_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
  2900. #endif // CONFIG_VP9_HIGHBITDEPTH
  2901. if (cpi->twopass.total_left_stats.coded_error != 0.0) {
  2902. double dc_quant_devisor;
  2903. #if CONFIG_VP9_HIGHBITDEPTH
  2904. switch (cm->bit_depth) {
  2905. case VPX_BITS_8:
  2906. dc_quant_devisor = 4.0;
  2907. break;
  2908. case VPX_BITS_10:
  2909. dc_quant_devisor = 16.0;
  2910. break;
  2911. default:
  2912. assert(cm->bit_depth == VPX_BITS_12);
  2913. dc_quant_devisor = 64.0;
  2914. break;
  2915. }
  2916. #else
  2917. dc_quant_devisor = 4.0;
  2918. #endif
  2919. if (!cm->current_video_frame) {
  2920. fprintf(f, "frame, width, height, last ts, last end ts, "
  2921. "source_alt_ref_pending, source_alt_ref_active, "
  2922. "this_frame_target, projected_frame_size, "
  2923. "projected_frame_size / MBs, "
  2924. "projected_frame_size - this_frame_target, "
  2925. "vbr_bits_off_target, vbr_bits_off_target_fast, "
  2926. "twopass.extend_minq, twopass.extend_minq_fast, "
  2927. "total_target_vs_actual, "
  2928. "starting_buffer_level - bits_off_target, "
  2929. "total_actual_bits, base_qindex, q for base_qindex, "
  2930. "dc quant, q for active_worst_quality, avg_q, q for oxcf.cq_level, "
  2931. "refresh_last_frame, refresh_golden_frame, refresh_alt_ref_frame, "
  2932. "frame_type, gfu_boost, "
  2933. "twopass.bits_left, "
  2934. "twopass.total_left_stats.coded_error, "
  2935. "twopass.bits_left / (1 + twopass.total_left_stats.coded_error), "
  2936. "tot_recode_hits, recon_err, kf_boost, "
  2937. "twopass.kf_zeromotion_pct, twopass.fr_content_type, "
  2938. "filter_level, seg.aq_av_offset\n");
  2939. }
  2940. fprintf(f, "%10u, %d, %d, %10"PRId64", %10"PRId64", %d, %d, %10d, %10d, "
  2941. "%10d, %10d, %10"PRId64", %10"PRId64", %5d, %5d, %10"PRId64", "
  2942. "%10"PRId64", %10"PRId64", %10d, %7.2lf, %7.2lf, %7.2lf, %7.2lf, "
  2943. "%7.2lf, %6d, %6d, %5d, %5d, %5d, %10"PRId64", %10.3lf, %10lf, %8u, "
  2944. "%10"PRId64", %10d, %10d, %10d, %10d, %10d\n",
  2945. cpi->common.current_video_frame,
  2946. cm->width, cm->height,
  2947. cpi->last_time_stamp_seen,
  2948. cpi->last_end_time_stamp_seen,
  2949. cpi->rc.source_alt_ref_pending,
  2950. cpi->rc.source_alt_ref_active,
  2951. cpi->rc.this_frame_target,
  2952. cpi->rc.projected_frame_size,
  2953. cpi->rc.projected_frame_size / cpi->common.MBs,
  2954. (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
  2955. cpi->rc.vbr_bits_off_target,
  2956. cpi->rc.vbr_bits_off_target_fast,
  2957. cpi->twopass.extend_minq,
  2958. cpi->twopass.extend_minq_fast,
  2959. cpi->rc.total_target_vs_actual,
  2960. (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
  2961. cpi->rc.total_actual_bits, cm->base_qindex,
  2962. vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth),
  2963. (double)vp9_dc_quant(cm->base_qindex, 0, cm->bit_depth) /
  2964. dc_quant_devisor,
  2965. vp9_convert_qindex_to_q(cpi->twopass.active_worst_quality,
  2966. cm->bit_depth),
  2967. cpi->rc.avg_q,
  2968. vp9_convert_qindex_to_q(cpi->oxcf.cq_level, cm->bit_depth),
  2969. cpi->refresh_last_frame, cpi->refresh_golden_frame,
  2970. cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
  2971. cpi->twopass.bits_left,
  2972. cpi->twopass.total_left_stats.coded_error,
  2973. cpi->twopass.bits_left /
  2974. (1 + cpi->twopass.total_left_stats.coded_error),
  2975. cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
  2976. cpi->twopass.kf_zeromotion_pct,
  2977. cpi->twopass.fr_content_type,
  2978. cm->lf.filter_level,
  2979. cm->seg.aq_av_offset);
  2980. }
  2981. fclose(f);
  2982. if (0) {
  2983. FILE *const fmodes = fopen("Modes.stt", "a");
  2984. int i;
  2985. fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
  2986. cm->frame_type, cpi->refresh_golden_frame,
  2987. cpi->refresh_alt_ref_frame);
  2988. for (i = 0; i < MAX_MODES; ++i)
  2989. fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
  2990. fprintf(fmodes, "\n");
  2991. fclose(fmodes);
  2992. }
  2993. }
  2994. #endif
  2995. static void set_mv_search_params(VP9_COMP *cpi) {
  2996. const VP9_COMMON *const cm = &cpi->common;
  2997. const unsigned int max_mv_def = VPXMIN(cm->width, cm->height);
  2998. // Default based on max resolution.
  2999. cpi->mv_step_param = vp9_init_search_range(max_mv_def);
  3000. if (cpi->sf.mv.auto_mv_step_size) {
  3001. if (frame_is_intra_only(cm)) {
  3002. // Initialize max_mv_magnitude for use in the first INTER frame
  3003. // after a key/intra-only frame.
  3004. cpi->max_mv_magnitude = max_mv_def;
  3005. } else {
  3006. if (cm->show_frame) {
  3007. // Allow mv_steps to correspond to twice the max mv magnitude found
  3008. // in the previous frame, capped by the default max_mv_magnitude based
  3009. // on resolution.
  3010. cpi->mv_step_param = vp9_init_search_range(
  3011. VPXMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
  3012. }
  3013. cpi->max_mv_magnitude = 0;
  3014. }
  3015. }
  3016. }
  3017. static void set_size_independent_vars(VP9_COMP *cpi) {
  3018. vp9_set_speed_features_framesize_independent(cpi, cpi->oxcf.speed);
  3019. vp9_set_rd_speed_thresholds(cpi);
  3020. vp9_set_rd_speed_thresholds_sub8x8(cpi);
  3021. cpi->common.interp_filter = cpi->sf.default_interp_filter;
  3022. }
  3023. static void set_size_dependent_vars(VP9_COMP *cpi, int *q, int *bottom_index,
  3024. int *top_index) {
  3025. VP9_COMMON *const cm = &cpi->common;
  3026. // Setup variables that depend on the dimensions of the frame.
  3027. vp9_set_speed_features_framesize_dependent(cpi, cpi->oxcf.speed);
  3028. // Decide q and q bounds.
  3029. *q = vp9_rc_pick_q_and_bounds(cpi, bottom_index, top_index);
  3030. if (cpi->oxcf.rc_mode == VPX_CBR && cpi->rc.force_max_q) {
  3031. *q = cpi->rc.worst_quality;
  3032. cpi->rc.force_max_q = 0;
  3033. }
  3034. if (!frame_is_intra_only(cm)) {
  3035. vp9_set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH);
  3036. }
  3037. #if !CONFIG_REALTIME_ONLY
  3038. // Configure experimental use of segmentation for enhanced coding of
  3039. // static regions if indicated.
  3040. // Only allowed in the second pass of a two pass encode, as it requires
  3041. // lagged coding, and if the relevant speed feature flag is set.
  3042. if (cpi->oxcf.pass == 2 && cpi->sf.static_segmentation)
  3043. configure_static_seg_features(cpi);
  3044. #endif // !CONFIG_REALTIME_ONLY
  3045. #if CONFIG_VP9_POSTPROC && !(CONFIG_VP9_TEMPORAL_DENOISING)
  3046. if (cpi->oxcf.noise_sensitivity > 0) {
  3047. int l = 0;
  3048. switch (cpi->oxcf.noise_sensitivity) {
  3049. case 1: l = 20; break;
  3050. case 2: l = 40; break;
  3051. case 3: l = 60; break;
  3052. case 4:
  3053. case 5: l = 100; break;
  3054. case 6: l = 150; break;
  3055. }
  3056. if (!cpi->common.postproc_state.limits) {
  3057. cpi->common.postproc_state.limits =
  3058. vpx_calloc(cpi->un_scaled_source->y_width,
  3059. sizeof(*cpi->common.postproc_state.limits));
  3060. }
  3061. vp9_denoise(cpi->Source, cpi->Source, l, cpi->common.postproc_state.limits);
  3062. }
  3063. #endif // CONFIG_VP9_POSTPROC
  3064. }
  3065. #if CONFIG_VP9_TEMPORAL_DENOISING
  3066. static void setup_denoiser_buffer(VP9_COMP *cpi) {
  3067. VP9_COMMON *const cm = &cpi->common;
  3068. if (cpi->oxcf.noise_sensitivity > 0 &&
  3069. !cpi->denoiser.frame_buffer_initialized) {
  3070. if (vp9_denoiser_alloc(cm, &cpi->svc, &cpi->denoiser, cpi->use_svc,
  3071. cpi->oxcf.noise_sensitivity, cm->width, cm->height,
  3072. cm->subsampling_x, cm->subsampling_y,
  3073. #if CONFIG_VP9_HIGHBITDEPTH
  3074. cm->use_highbitdepth,
  3075. #endif
  3076. VP9_ENC_BORDER_IN_PIXELS))
  3077. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  3078. "Failed to allocate denoiser");
  3079. }
  3080. }
  3081. #endif
  3082. static void init_motion_estimation(VP9_COMP *cpi) {
  3083. int y_stride = cpi->scaled_source.y_stride;
  3084. if (cpi->sf.mv.search_method == NSTEP) {
  3085. vp9_init3smotion_compensation(&cpi->ss_cfg, y_stride);
  3086. } else if (cpi->sf.mv.search_method == DIAMOND) {
  3087. vp9_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
  3088. }
  3089. }
  3090. static void set_frame_size(VP9_COMP *cpi) {
  3091. int ref_frame;
  3092. VP9_COMMON *const cm = &cpi->common;
  3093. VP9EncoderConfig *const oxcf = &cpi->oxcf;
  3094. MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
  3095. #if !CONFIG_REALTIME_ONLY
  3096. if (oxcf->pass == 2 && oxcf->rc_mode == VPX_VBR &&
  3097. ((oxcf->resize_mode == RESIZE_FIXED && cm->current_video_frame == 0) ||
  3098. (oxcf->resize_mode == RESIZE_DYNAMIC && cpi->resize_pending))) {
  3099. calculate_coded_size(cpi, &oxcf->scaled_frame_width,
  3100. &oxcf->scaled_frame_height);
  3101. // There has been a change in frame size.
  3102. vp9_set_size_literal(cpi, oxcf->scaled_frame_width,
  3103. oxcf->scaled_frame_height);
  3104. }
  3105. #endif // !CONFIG_REALTIME_ONLY
  3106. if (oxcf->pass == 0 && oxcf->rc_mode == VPX_CBR && !cpi->use_svc &&
  3107. oxcf->resize_mode == RESIZE_DYNAMIC && cpi->resize_pending != 0) {
  3108. oxcf->scaled_frame_width =
  3109. (oxcf->width * cpi->resize_scale_num) / cpi->resize_scale_den;
  3110. oxcf->scaled_frame_height =
  3111. (oxcf->height * cpi->resize_scale_num) / cpi->resize_scale_den;
  3112. // There has been a change in frame size.
  3113. vp9_set_size_literal(cpi, oxcf->scaled_frame_width,
  3114. oxcf->scaled_frame_height);
  3115. // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
  3116. set_mv_search_params(cpi);
  3117. vp9_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
  3118. #if CONFIG_VP9_TEMPORAL_DENOISING
  3119. // Reset the denoiser on the resized frame.
  3120. if (cpi->oxcf.noise_sensitivity > 0) {
  3121. vp9_denoiser_free(&(cpi->denoiser));
  3122. setup_denoiser_buffer(cpi);
  3123. // Dynamic resize is only triggered for non-SVC, so we can force
  3124. // golden frame update here as temporary fix to denoiser.
  3125. cpi->refresh_golden_frame = 1;
  3126. }
  3127. #endif
  3128. }
  3129. if ((oxcf->pass == 2) && !cpi->use_svc) {
  3130. vp9_set_target_rate(cpi);
  3131. }
  3132. alloc_frame_mvs(cm, cm->new_fb_idx);
  3133. // Reset the frame pointers to the current frame size.
  3134. if (vpx_realloc_frame_buffer(get_frame_new_buffer(cm), cm->width, cm->height,
  3135. cm->subsampling_x, cm->subsampling_y,
  3136. #if CONFIG_VP9_HIGHBITDEPTH
  3137. cm->use_highbitdepth,
  3138. #endif
  3139. VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
  3140. NULL, NULL, NULL))
  3141. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  3142. "Failed to allocate frame buffer");
  3143. alloc_util_frame_buffers(cpi);
  3144. init_motion_estimation(cpi);
  3145. for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
  3146. RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - 1];
  3147. const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
  3148. ref_buf->idx = buf_idx;
  3149. if (buf_idx != INVALID_IDX) {
  3150. YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
  3151. ref_buf->buf = buf;
  3152. #if CONFIG_VP9_HIGHBITDEPTH
  3153. vp9_setup_scale_factors_for_frame(
  3154. &ref_buf->sf, buf->y_crop_width, buf->y_crop_height, cm->width,
  3155. cm->height, (buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0);
  3156. #else
  3157. vp9_setup_scale_factors_for_frame(&ref_buf->sf, buf->y_crop_width,
  3158. buf->y_crop_height, cm->width,
  3159. cm->height);
  3160. #endif // CONFIG_VP9_HIGHBITDEPTH
  3161. if (vp9_is_scaled(&ref_buf->sf)) vpx_extend_frame_borders(buf);
  3162. } else {
  3163. ref_buf->buf = NULL;
  3164. }
  3165. }
  3166. set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
  3167. }
  3168. #if CONFIG_CONSISTENT_RECODE
  3169. static void save_encode_params(VP9_COMP *cpi) {
  3170. VP9_COMMON *const cm = &cpi->common;
  3171. const int tile_cols = 1 << cm->log2_tile_cols;
  3172. const int tile_rows = 1 << cm->log2_tile_rows;
  3173. int tile_col, tile_row;
  3174. int i, j;
  3175. RD_OPT *rd_opt = &cpi->rd;
  3176. for (i = 0; i < MAX_REF_FRAMES; i++) {
  3177. for (j = 0; j < REFERENCE_MODES; j++)
  3178. rd_opt->prediction_type_threshes_prev[i][j] =
  3179. rd_opt->prediction_type_threshes[i][j];
  3180. for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; j++)
  3181. rd_opt->filter_threshes_prev[i][j] = rd_opt->filter_threshes[i][j];
  3182. }
  3183. if (cpi->tile_data != NULL) {
  3184. for (tile_row = 0; tile_row < tile_rows; ++tile_row)
  3185. for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
  3186. TileDataEnc *tile_data =
  3187. &cpi->tile_data[tile_row * tile_cols + tile_col];
  3188. for (i = 0; i < BLOCK_SIZES; ++i) {
  3189. for (j = 0; j < MAX_MODES; ++j) {
  3190. tile_data->thresh_freq_fact_prev[i][j] =
  3191. tile_data->thresh_freq_fact[i][j];
  3192. }
  3193. }
  3194. }
  3195. }
  3196. }
  3197. #endif
  3198. static INLINE void set_raw_source_frame(VP9_COMP *cpi) {
  3199. #ifdef ENABLE_KF_DENOISE
  3200. if (is_spatial_denoise_enabled(cpi)) {
  3201. cpi->raw_source_frame = vp9_scale_if_required(
  3202. cm, &cpi->raw_unscaled_source, &cpi->raw_scaled_source,
  3203. (oxcf->pass == 0), EIGHTTAP, 0);
  3204. } else {
  3205. cpi->raw_source_frame = cpi->Source;
  3206. }
  3207. #else
  3208. cpi->raw_source_frame = cpi->Source;
  3209. #endif
  3210. }
  3211. static int encode_without_recode_loop(VP9_COMP *cpi, size_t *size,
  3212. uint8_t *dest) {
  3213. VP9_COMMON *const cm = &cpi->common;
  3214. SVC *const svc = &cpi->svc;
  3215. int q = 0, bottom_index = 0, top_index = 0;
  3216. int no_drop_scene_change = 0;
  3217. const INTERP_FILTER filter_scaler =
  3218. (is_one_pass_cbr_svc(cpi))
  3219. ? svc->downsample_filter_type[svc->spatial_layer_id]
  3220. : EIGHTTAP;
  3221. const int phase_scaler =
  3222. (is_one_pass_cbr_svc(cpi))
  3223. ? svc->downsample_filter_phase[svc->spatial_layer_id]
  3224. : 0;
  3225. if (cm->show_existing_frame) {
  3226. cpi->rc.this_frame_target = 0;
  3227. if (is_psnr_calc_enabled(cpi)) set_raw_source_frame(cpi);
  3228. return 1;
  3229. }
  3230. svc->time_stamp_prev[svc->spatial_layer_id] = svc->time_stamp_superframe;
  3231. // Flag to check if its valid to compute the source sad (used for
  3232. // scene detection and for superblock content state in CBR mode).
  3233. // The flag may get reset below based on SVC or resizing state.
  3234. cpi->compute_source_sad_onepass = cpi->oxcf.mode == REALTIME;
  3235. vpx_clear_system_state();
  3236. set_frame_size(cpi);
  3237. if (is_one_pass_cbr_svc(cpi) &&
  3238. cpi->un_scaled_source->y_width == cm->width << 2 &&
  3239. cpi->un_scaled_source->y_height == cm->height << 2 &&
  3240. svc->scaled_temp.y_width == cm->width << 1 &&
  3241. svc->scaled_temp.y_height == cm->height << 1) {
  3242. // For svc, if it is a 1/4x1/4 downscaling, do a two-stage scaling to take
  3243. // advantage of the 1:2 optimized scaler. In the process, the 1/2x1/2
  3244. // result will be saved in scaled_temp and might be used later.
  3245. const INTERP_FILTER filter_scaler2 = svc->downsample_filter_type[1];
  3246. const int phase_scaler2 = svc->downsample_filter_phase[1];
  3247. cpi->Source = vp9_svc_twostage_scale(
  3248. cm, cpi->un_scaled_source, &cpi->scaled_source, &svc->scaled_temp,
  3249. filter_scaler, phase_scaler, filter_scaler2, phase_scaler2);
  3250. svc->scaled_one_half = 1;
  3251. } else if (is_one_pass_cbr_svc(cpi) &&
  3252. cpi->un_scaled_source->y_width == cm->width << 1 &&
  3253. cpi->un_scaled_source->y_height == cm->height << 1 &&
  3254. svc->scaled_one_half) {
  3255. // If the spatial layer is 1/2x1/2 and the scaling is already done in the
  3256. // two-stage scaling, use the result directly.
  3257. cpi->Source = &svc->scaled_temp;
  3258. svc->scaled_one_half = 0;
  3259. } else {
  3260. cpi->Source = vp9_scale_if_required(
  3261. cm, cpi->un_scaled_source, &cpi->scaled_source, (cpi->oxcf.pass == 0),
  3262. filter_scaler, phase_scaler);
  3263. }
  3264. #ifdef OUTPUT_YUV_SVC_SRC
  3265. // Write out at most 3 spatial layers.
  3266. if (is_one_pass_cbr_svc(cpi) && svc->spatial_layer_id < 3) {
  3267. vpx_write_yuv_frame(yuv_svc_src[svc->spatial_layer_id], cpi->Source);
  3268. }
  3269. #endif
  3270. // Unfiltered raw source used in metrics calculation if the source
  3271. // has been filtered.
  3272. if (is_psnr_calc_enabled(cpi)) {
  3273. #ifdef ENABLE_KF_DENOISE
  3274. if (is_spatial_denoise_enabled(cpi)) {
  3275. cpi->raw_source_frame = vp9_scale_if_required(
  3276. cm, &cpi->raw_unscaled_source, &cpi->raw_scaled_source,
  3277. (cpi->oxcf.pass == 0), EIGHTTAP, phase_scaler);
  3278. } else {
  3279. cpi->raw_source_frame = cpi->Source;
  3280. }
  3281. #else
  3282. cpi->raw_source_frame = cpi->Source;
  3283. #endif
  3284. }
  3285. if ((cpi->use_svc &&
  3286. (svc->spatial_layer_id < svc->number_spatial_layers - 1 ||
  3287. svc->temporal_layer_id < svc->number_temporal_layers - 1 ||
  3288. svc->current_superframe < 1)) ||
  3289. cpi->resize_pending || cpi->resize_state || cpi->external_resize ||
  3290. cpi->resize_state != ORIG) {
  3291. cpi->compute_source_sad_onepass = 0;
  3292. if (cpi->content_state_sb_fd != NULL)
  3293. memset(cpi->content_state_sb_fd, 0,
  3294. (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1) *
  3295. sizeof(*cpi->content_state_sb_fd));
  3296. }
  3297. // Avoid scaling last_source unless its needed.
  3298. // Last source is needed if avg_source_sad() is used, or if
  3299. // partition_search_type == SOURCE_VAR_BASED_PARTITION, or if noise
  3300. // estimation is enabled.
  3301. if (cpi->unscaled_last_source != NULL &&
  3302. (cpi->oxcf.content == VP9E_CONTENT_SCREEN ||
  3303. (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_VBR &&
  3304. cpi->oxcf.mode == REALTIME && cpi->oxcf.speed >= 5) ||
  3305. cpi->sf.partition_search_type == SOURCE_VAR_BASED_PARTITION ||
  3306. (cpi->noise_estimate.enabled && !cpi->oxcf.noise_sensitivity) ||
  3307. cpi->compute_source_sad_onepass))
  3308. cpi->Last_Source = vp9_scale_if_required(
  3309. cm, cpi->unscaled_last_source, &cpi->scaled_last_source,
  3310. (cpi->oxcf.pass == 0), EIGHTTAP, 0);
  3311. if (cpi->Last_Source == NULL ||
  3312. cpi->Last_Source->y_width != cpi->Source->y_width ||
  3313. cpi->Last_Source->y_height != cpi->Source->y_height)
  3314. cpi->compute_source_sad_onepass = 0;
  3315. if (frame_is_intra_only(cm) || cpi->resize_pending != 0) {
  3316. memset(cpi->consec_zero_mv, 0,
  3317. cm->mi_rows * cm->mi_cols * sizeof(*cpi->consec_zero_mv));
  3318. }
  3319. #if CONFIG_VP9_TEMPORAL_DENOISING
  3320. if (cpi->oxcf.noise_sensitivity > 0 && cpi->use_svc)
  3321. vp9_denoiser_reset_on_first_frame(cpi);
  3322. #endif
  3323. // Scene detection is always used for VBR mode or screen-content case.
  3324. // For other cases (e.g., CBR mode) use it for 5 <= speed < 8 for now
  3325. // (need to check encoding time cost for doing this for speed 8).
  3326. cpi->rc.high_source_sad = 0;
  3327. cpi->rc.hybrid_intra_scene_change = 0;
  3328. cpi->rc.re_encode_maxq_scene_change = 0;
  3329. if (cm->show_frame && cpi->oxcf.mode == REALTIME &&
  3330. (cpi->oxcf.rc_mode == VPX_VBR ||
  3331. cpi->oxcf.content == VP9E_CONTENT_SCREEN ||
  3332. (cpi->oxcf.speed >= 5 && cpi->oxcf.speed < 8)))
  3333. vp9_scene_detection_onepass(cpi);
  3334. if (svc->spatial_layer_id == svc->first_spatial_layer_to_encode) {
  3335. svc->high_source_sad_superframe = cpi->rc.high_source_sad;
  3336. svc->high_num_blocks_with_motion = cpi->rc.high_num_blocks_with_motion;
  3337. // On scene change reset temporal layer pattern to TL0.
  3338. // Note that if the base/lower spatial layers are skipped: instead of
  3339. // inserting base layer here, we force max-q for the next superframe
  3340. // with lower spatial layers: this is done in vp9_encodedframe_overshoot()
  3341. // when max-q is decided for the current layer.
  3342. // Only do this reset for bypass/flexible mode.
  3343. if (svc->high_source_sad_superframe && svc->temporal_layer_id > 0 &&
  3344. svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
  3345. // rc->high_source_sad will get reset so copy it to restore it.
  3346. int tmp_high_source_sad = cpi->rc.high_source_sad;
  3347. vp9_svc_reset_temporal_layers(cpi, cm->frame_type == KEY_FRAME);
  3348. cpi->rc.high_source_sad = tmp_high_source_sad;
  3349. }
  3350. }
  3351. vp9_update_noise_estimate(cpi);
  3352. // For 1 pass CBR, check if we are dropping this frame.
  3353. // Never drop on key frame, if base layer is key for svc,
  3354. // on scene change, or if superframe has layer sync.
  3355. if ((cpi->rc.high_source_sad || svc->high_source_sad_superframe) &&
  3356. !(cpi->rc.use_post_encode_drop && svc->last_layer_dropped[0]))
  3357. no_drop_scene_change = 1;
  3358. if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR &&
  3359. !frame_is_intra_only(cm) && !no_drop_scene_change &&
  3360. !svc->superframe_has_layer_sync &&
  3361. (!cpi->use_svc ||
  3362. !svc->layer_context[svc->temporal_layer_id].is_key_frame)) {
  3363. if (vp9_rc_drop_frame(cpi)) return 0;
  3364. }
  3365. // For 1 pass CBR SVC, only ZEROMV is allowed for spatial reference frame
  3366. // when svc->force_zero_mode_spatial_ref = 1. Under those conditions we can
  3367. // avoid this frame-level upsampling (for non intra_only frames).
  3368. if (frame_is_intra_only(cm) == 0 &&
  3369. !(is_one_pass_cbr_svc(cpi) && svc->force_zero_mode_spatial_ref)) {
  3370. vp9_scale_references(cpi);
  3371. }
  3372. set_size_independent_vars(cpi);
  3373. set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
  3374. // search method and step parameter might be changed in speed settings.
  3375. init_motion_estimation(cpi);
  3376. if (cpi->sf.copy_partition_flag) alloc_copy_partition_data(cpi);
  3377. if (cpi->sf.svc_use_lowres_part &&
  3378. svc->spatial_layer_id == svc->number_spatial_layers - 2) {
  3379. if (svc->prev_partition_svc == NULL) {
  3380. CHECK_MEM_ERROR(
  3381. cm, svc->prev_partition_svc,
  3382. (BLOCK_SIZE *)vpx_calloc(cm->mi_stride * cm->mi_rows,
  3383. sizeof(*svc->prev_partition_svc)));
  3384. }
  3385. }
  3386. // TODO(jianj): Look into issue of skin detection with high bitdepth.
  3387. if (cm->bit_depth == 8 && cpi->oxcf.speed >= 5 && cpi->oxcf.pass == 0 &&
  3388. cpi->oxcf.rc_mode == VPX_CBR &&
  3389. cpi->oxcf.content != VP9E_CONTENT_SCREEN &&
  3390. cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
  3391. cpi->use_skin_detection = 1;
  3392. }
  3393. // Enable post encode frame dropping for CBR on non key frame, when
  3394. // ext_use_post_encode_drop is specified by user.
  3395. cpi->rc.use_post_encode_drop = cpi->rc.ext_use_post_encode_drop &&
  3396. cpi->oxcf.rc_mode == VPX_CBR &&
  3397. cm->frame_type != KEY_FRAME;
  3398. vp9_set_quantizer(cm, q);
  3399. vp9_set_variance_partition_thresholds(cpi, q, 0);
  3400. setup_frame(cpi);
  3401. suppress_active_map(cpi);
  3402. if (cpi->use_svc) {
  3403. // On non-zero spatial layer, check for disabling inter-layer
  3404. // prediction.
  3405. if (svc->spatial_layer_id > 0) vp9_svc_constrain_inter_layer_pred(cpi);
  3406. vp9_svc_assert_constraints_pattern(cpi);
  3407. }
  3408. if (cpi->rc.last_post_encode_dropped_scene_change) {
  3409. cpi->rc.high_source_sad = 1;
  3410. svc->high_source_sad_superframe = 1;
  3411. // For now disable use_source_sad since Last_Source will not be the previous
  3412. // encoded but the dropped one.
  3413. cpi->sf.use_source_sad = 0;
  3414. cpi->rc.last_post_encode_dropped_scene_change = 0;
  3415. }
  3416. // Check if this high_source_sad (scene/slide change) frame should be
  3417. // encoded at high/max QP, and if so, set the q and adjust some rate
  3418. // control parameters.
  3419. if (cpi->sf.overshoot_detection_cbr_rt == FAST_DETECTION_MAXQ &&
  3420. (cpi->rc.high_source_sad ||
  3421. (cpi->use_svc && svc->high_source_sad_superframe))) {
  3422. if (vp9_encodedframe_overshoot(cpi, -1, &q)) {
  3423. vp9_set_quantizer(cm, q);
  3424. vp9_set_variance_partition_thresholds(cpi, q, 0);
  3425. }
  3426. }
  3427. #if !CONFIG_REALTIME_ONLY
  3428. // Variance adaptive and in frame q adjustment experiments are mutually
  3429. // exclusive.
  3430. if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
  3431. vp9_vaq_frame_setup(cpi);
  3432. } else if (cpi->oxcf.aq_mode == EQUATOR360_AQ) {
  3433. vp9_360aq_frame_setup(cpi);
  3434. } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
  3435. vp9_setup_in_frame_q_adj(cpi);
  3436. } else if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ) {
  3437. // it may be pretty bad for rate-control,
  3438. // and I should handle it somehow
  3439. vp9_alt_ref_aq_setup_map(cpi->alt_ref_aq, cpi);
  3440. } else {
  3441. #endif
  3442. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
  3443. vp9_cyclic_refresh_setup(cpi);
  3444. } else if (cpi->roi.enabled && !frame_is_intra_only(cm)) {
  3445. apply_roi_map(cpi);
  3446. }
  3447. #if !CONFIG_REALTIME_ONLY
  3448. }
  3449. #endif
  3450. apply_active_map(cpi);
  3451. vp9_encode_frame(cpi);
  3452. // Check if we should re-encode this frame at high Q because of high
  3453. // overshoot based on the encoded frame size. Only for frames where
  3454. // high temporal-source SAD is detected.
  3455. // For SVC: all spatial layers are checked for re-encoding.
  3456. if (cpi->sf.overshoot_detection_cbr_rt == RE_ENCODE_MAXQ &&
  3457. (cpi->rc.high_source_sad ||
  3458. (cpi->use_svc && svc->high_source_sad_superframe))) {
  3459. int frame_size = 0;
  3460. // Get an estimate of the encoded frame size.
  3461. save_coding_context(cpi);
  3462. vp9_pack_bitstream(cpi, dest, size);
  3463. restore_coding_context(cpi);
  3464. frame_size = (int)(*size) << 3;
  3465. // Check if encoded frame will overshoot too much, and if so, set the q and
  3466. // adjust some rate control parameters, and return to re-encode the frame.
  3467. if (vp9_encodedframe_overshoot(cpi, frame_size, &q)) {
  3468. vpx_clear_system_state();
  3469. vp9_set_quantizer(cm, q);
  3470. vp9_set_variance_partition_thresholds(cpi, q, 0);
  3471. suppress_active_map(cpi);
  3472. // Turn-off cyclic refresh for re-encoded frame.
  3473. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
  3474. CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
  3475. unsigned char *const seg_map = cpi->segmentation_map;
  3476. memset(seg_map, 0, cm->mi_rows * cm->mi_cols);
  3477. memset(cr->last_coded_q_map, MAXQ,
  3478. cm->mi_rows * cm->mi_cols * sizeof(*cr->last_coded_q_map));
  3479. cr->sb_index = 0;
  3480. vp9_disable_segmentation(&cm->seg);
  3481. }
  3482. apply_active_map(cpi);
  3483. vp9_encode_frame(cpi);
  3484. }
  3485. }
  3486. // Update some stats from cyclic refresh, and check for golden frame update.
  3487. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
  3488. !frame_is_intra_only(cm))
  3489. vp9_cyclic_refresh_postencode(cpi);
  3490. // Update the skip mb flag probabilities based on the distribution
  3491. // seen in the last encoder iteration.
  3492. // update_base_skip_probs(cpi);
  3493. vpx_clear_system_state();
  3494. return 1;
  3495. }
  3496. #if !CONFIG_REALTIME_ONLY
  3497. #define MAX_QSTEP_ADJ 4
  3498. static int get_qstep_adj(int rate_excess, int rate_limit) {
  3499. int qstep =
  3500. rate_limit ? ((rate_excess + rate_limit / 2) / rate_limit) : INT_MAX;
  3501. return VPXMIN(qstep, MAX_QSTEP_ADJ);
  3502. }
  3503. static void encode_with_recode_loop(VP9_COMP *cpi, size_t *size,
  3504. uint8_t *dest) {
  3505. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  3506. VP9_COMMON *const cm = &cpi->common;
  3507. RATE_CONTROL *const rc = &cpi->rc;
  3508. int bottom_index, top_index;
  3509. int loop_count = 0;
  3510. int loop_at_this_size = 0;
  3511. int loop = 0;
  3512. int overshoot_seen = 0;
  3513. int undershoot_seen = 0;
  3514. int frame_over_shoot_limit;
  3515. int frame_under_shoot_limit;
  3516. int q = 0, q_low = 0, q_high = 0;
  3517. int enable_acl;
  3518. #ifdef AGGRESSIVE_VBR
  3519. int qrange_adj = 1;
  3520. #endif
  3521. if (cm->show_existing_frame) {
  3522. rc->this_frame_target = 0;
  3523. if (is_psnr_calc_enabled(cpi)) set_raw_source_frame(cpi);
  3524. return;
  3525. }
  3526. set_size_independent_vars(cpi);
  3527. enable_acl = cpi->sf.allow_acl ? (cm->frame_type == KEY_FRAME) ||
  3528. (cpi->twopass.gf_group.index == 1)
  3529. : 0;
  3530. do {
  3531. vpx_clear_system_state();
  3532. set_frame_size(cpi);
  3533. if (loop_count == 0 || cpi->resize_pending != 0) {
  3534. set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
  3535. #ifdef AGGRESSIVE_VBR
  3536. if (two_pass_first_group_inter(cpi)) {
  3537. // Adjustment limits for min and max q
  3538. qrange_adj = VPXMAX(1, (top_index - bottom_index) / 2);
  3539. bottom_index =
  3540. VPXMAX(bottom_index - qrange_adj / 2, oxcf->best_allowed_q);
  3541. top_index = VPXMIN(oxcf->worst_allowed_q, top_index + qrange_adj / 2);
  3542. }
  3543. #endif
  3544. // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
  3545. set_mv_search_params(cpi);
  3546. // Reset the loop state for new frame size.
  3547. overshoot_seen = 0;
  3548. undershoot_seen = 0;
  3549. // Reconfiguration for change in frame size has concluded.
  3550. cpi->resize_pending = 0;
  3551. q_low = bottom_index;
  3552. q_high = top_index;
  3553. loop_at_this_size = 0;
  3554. }
  3555. // Decide frame size bounds first time through.
  3556. if (loop_count == 0) {
  3557. vp9_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
  3558. &frame_under_shoot_limit,
  3559. &frame_over_shoot_limit);
  3560. }
  3561. cpi->Source =
  3562. vp9_scale_if_required(cm, cpi->un_scaled_source, &cpi->scaled_source,
  3563. (oxcf->pass == 0), EIGHTTAP, 0);
  3564. // Unfiltered raw source used in metrics calculation if the source
  3565. // has been filtered.
  3566. if (is_psnr_calc_enabled(cpi)) {
  3567. #ifdef ENABLE_KF_DENOISE
  3568. if (is_spatial_denoise_enabled(cpi)) {
  3569. cpi->raw_source_frame = vp9_scale_if_required(
  3570. cm, &cpi->raw_unscaled_source, &cpi->raw_scaled_source,
  3571. (oxcf->pass == 0), EIGHTTAP, 0);
  3572. } else {
  3573. cpi->raw_source_frame = cpi->Source;
  3574. }
  3575. #else
  3576. cpi->raw_source_frame = cpi->Source;
  3577. #endif
  3578. }
  3579. if (cpi->unscaled_last_source != NULL)
  3580. cpi->Last_Source = vp9_scale_if_required(cm, cpi->unscaled_last_source,
  3581. &cpi->scaled_last_source,
  3582. (oxcf->pass == 0), EIGHTTAP, 0);
  3583. if (frame_is_intra_only(cm) == 0) {
  3584. if (loop_count > 0) {
  3585. release_scaled_references(cpi);
  3586. }
  3587. vp9_scale_references(cpi);
  3588. }
  3589. vp9_set_quantizer(cm, q);
  3590. if (loop_count == 0) setup_frame(cpi);
  3591. // Variance adaptive and in frame q adjustment experiments are mutually
  3592. // exclusive.
  3593. if (oxcf->aq_mode == VARIANCE_AQ) {
  3594. vp9_vaq_frame_setup(cpi);
  3595. } else if (oxcf->aq_mode == EQUATOR360_AQ) {
  3596. vp9_360aq_frame_setup(cpi);
  3597. } else if (oxcf->aq_mode == COMPLEXITY_AQ) {
  3598. vp9_setup_in_frame_q_adj(cpi);
  3599. } else if (oxcf->aq_mode == LOOKAHEAD_AQ) {
  3600. vp9_alt_ref_aq_setup_map(cpi->alt_ref_aq, cpi);
  3601. } else if (oxcf->aq_mode == PSNR_AQ) {
  3602. vp9_psnr_aq_mode_setup(&cm->seg);
  3603. }
  3604. vp9_encode_frame(cpi);
  3605. // Update the skip mb flag probabilities based on the distribution
  3606. // seen in the last encoder iteration.
  3607. // update_base_skip_probs(cpi);
  3608. vpx_clear_system_state();
  3609. // Dummy pack of the bitstream using up to date stats to get an
  3610. // accurate estimate of output frame size to determine if we need
  3611. // to recode.
  3612. if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
  3613. save_coding_context(cpi);
  3614. if (!cpi->sf.use_nonrd_pick_mode) vp9_pack_bitstream(cpi, dest, size);
  3615. rc->projected_frame_size = (int)(*size) << 3;
  3616. if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
  3617. }
  3618. if (oxcf->rc_mode == VPX_Q) {
  3619. loop = 0;
  3620. } else {
  3621. if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
  3622. (rc->projected_frame_size < rc->max_frame_bandwidth)) {
  3623. int last_q = q;
  3624. int64_t kf_err;
  3625. int64_t high_err_target = cpi->ambient_err;
  3626. int64_t low_err_target = cpi->ambient_err >> 1;
  3627. #if CONFIG_VP9_HIGHBITDEPTH
  3628. if (cm->use_highbitdepth) {
  3629. kf_err = vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
  3630. } else {
  3631. kf_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
  3632. }
  3633. #else
  3634. kf_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
  3635. #endif // CONFIG_VP9_HIGHBITDEPTH
  3636. // Prevent possible divide by zero error below for perfect KF
  3637. kf_err += !kf_err;
  3638. // The key frame is not good enough or we can afford
  3639. // to make it better without undue risk of popping.
  3640. if ((kf_err > high_err_target &&
  3641. rc->projected_frame_size <= frame_over_shoot_limit) ||
  3642. (kf_err > low_err_target &&
  3643. rc->projected_frame_size <= frame_under_shoot_limit)) {
  3644. // Lower q_high
  3645. q_high = q > q_low ? q - 1 : q_low;
  3646. // Adjust Q
  3647. q = (int)((q * high_err_target) / kf_err);
  3648. q = VPXMIN(q, (q_high + q_low) >> 1);
  3649. } else if (kf_err < low_err_target &&
  3650. rc->projected_frame_size >= frame_under_shoot_limit) {
  3651. // The key frame is much better than the previous frame
  3652. // Raise q_low
  3653. q_low = q < q_high ? q + 1 : q_high;
  3654. // Adjust Q
  3655. q = (int)((q * low_err_target) / kf_err);
  3656. q = VPXMIN(q, (q_high + q_low + 1) >> 1);
  3657. }
  3658. // Clamp Q to upper and lower limits:
  3659. q = clamp(q, q_low, q_high);
  3660. loop = q != last_q;
  3661. } else if (recode_loop_test(cpi, frame_over_shoot_limit,
  3662. frame_under_shoot_limit, q,
  3663. VPXMAX(q_high, top_index), bottom_index)) {
  3664. // Is the projected frame size out of range and are we allowed
  3665. // to attempt to recode.
  3666. int last_q = q;
  3667. int retries = 0;
  3668. int qstep;
  3669. if (cpi->resize_pending == 1) {
  3670. // Change in frame size so go back around the recode loop.
  3671. cpi->rc.frame_size_selector =
  3672. SCALE_STEP1 - cpi->rc.frame_size_selector;
  3673. cpi->rc.next_frame_size_selector = cpi->rc.frame_size_selector;
  3674. #if CONFIG_INTERNAL_STATS
  3675. ++cpi->tot_recode_hits;
  3676. #endif
  3677. ++loop_count;
  3678. loop = 1;
  3679. continue;
  3680. }
  3681. // Frame size out of permitted range:
  3682. // Update correction factor & compute new Q to try...
  3683. // Frame is too large
  3684. if (rc->projected_frame_size > rc->this_frame_target) {
  3685. // Special case if the projected size is > the max allowed.
  3686. if ((q == q_high) &&
  3687. ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
  3688. (!rc->is_src_frame_alt_ref &&
  3689. (rc->projected_frame_size >=
  3690. big_rate_miss_high_threshold(cpi))))) {
  3691. int max_rate = VPXMAX(1, VPXMIN(rc->max_frame_bandwidth,
  3692. big_rate_miss_high_threshold(cpi)));
  3693. double q_val_high;
  3694. q_val_high = vp9_convert_qindex_to_q(q_high, cm->bit_depth);
  3695. q_val_high =
  3696. q_val_high * ((double)rc->projected_frame_size / max_rate);
  3697. q_high = vp9_convert_q_to_qindex(q_val_high, cm->bit_depth);
  3698. q_high = clamp(q_high, rc->best_quality, rc->worst_quality);
  3699. }
  3700. // Raise Qlow as to at least the current value
  3701. qstep =
  3702. get_qstep_adj(rc->projected_frame_size, rc->this_frame_target);
  3703. q_low = VPXMIN(q + qstep, q_high);
  3704. if (undershoot_seen || loop_at_this_size > 1) {
  3705. // Update rate_correction_factor unless
  3706. vp9_rc_update_rate_correction_factors(cpi);
  3707. q = (q_high + q_low + 1) / 2;
  3708. } else {
  3709. // Update rate_correction_factor unless
  3710. vp9_rc_update_rate_correction_factors(cpi);
  3711. q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
  3712. VPXMAX(q_high, top_index));
  3713. while (q < q_low && retries < 10) {
  3714. vp9_rc_update_rate_correction_factors(cpi);
  3715. q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
  3716. VPXMAX(q_high, top_index));
  3717. retries++;
  3718. }
  3719. }
  3720. overshoot_seen = 1;
  3721. } else {
  3722. // Frame is too small
  3723. qstep =
  3724. get_qstep_adj(rc->this_frame_target, rc->projected_frame_size);
  3725. q_high = VPXMAX(q - qstep, q_low);
  3726. if (overshoot_seen || loop_at_this_size > 1) {
  3727. vp9_rc_update_rate_correction_factors(cpi);
  3728. q = (q_high + q_low) / 2;
  3729. } else {
  3730. vp9_rc_update_rate_correction_factors(cpi);
  3731. q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
  3732. VPXMIN(q_low, bottom_index), top_index);
  3733. // Special case reset for qlow for constrained quality.
  3734. // This should only trigger where there is very substantial
  3735. // undershoot on a frame and the auto cq level is above
  3736. // the user passsed in value.
  3737. if (oxcf->rc_mode == VPX_CQ && q < q_low) {
  3738. q_low = q;
  3739. }
  3740. while (q > q_high && retries < 10) {
  3741. vp9_rc_update_rate_correction_factors(cpi);
  3742. q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
  3743. VPXMIN(q_low, bottom_index), top_index);
  3744. retries++;
  3745. }
  3746. }
  3747. undershoot_seen = 1;
  3748. }
  3749. // Clamp Q to upper and lower limits:
  3750. q = clamp(q, q_low, q_high);
  3751. loop = (q != last_q);
  3752. } else {
  3753. loop = 0;
  3754. }
  3755. }
  3756. // Special case for overlay frame.
  3757. if (rc->is_src_frame_alt_ref &&
  3758. rc->projected_frame_size < rc->max_frame_bandwidth)
  3759. loop = 0;
  3760. if (loop) {
  3761. ++loop_count;
  3762. ++loop_at_this_size;
  3763. #if CONFIG_INTERNAL_STATS
  3764. ++cpi->tot_recode_hits;
  3765. #endif
  3766. }
  3767. if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF)
  3768. if (loop || !enable_acl) restore_coding_context(cpi);
  3769. } while (loop);
  3770. #ifdef AGGRESSIVE_VBR
  3771. if (two_pass_first_group_inter(cpi)) {
  3772. cpi->twopass.active_worst_quality =
  3773. VPXMIN(q + qrange_adj, oxcf->worst_allowed_q);
  3774. } else if (!frame_is_kf_gf_arf(cpi)) {
  3775. #else
  3776. if (!frame_is_kf_gf_arf(cpi)) {
  3777. #endif
  3778. // Have we been forced to adapt Q outside the expected range by an extreme
  3779. // rate miss. If so adjust the active maxQ for the subsequent frames.
  3780. if (!rc->is_src_frame_alt_ref && (q > cpi->twopass.active_worst_quality)) {
  3781. cpi->twopass.active_worst_quality = q;
  3782. } else if (oxcf->vbr_corpus_complexity && q == q_low &&
  3783. rc->projected_frame_size < rc->this_frame_target) {
  3784. cpi->twopass.active_worst_quality =
  3785. VPXMAX(q, cpi->twopass.active_worst_quality - 1);
  3786. }
  3787. }
  3788. if (enable_acl) {
  3789. // Skip recoding, if model diff is below threshold
  3790. const int thresh = compute_context_model_thresh(cpi);
  3791. const int diff = compute_context_model_diff(cm);
  3792. if (diff < thresh) {
  3793. vpx_clear_system_state();
  3794. restore_coding_context(cpi);
  3795. return;
  3796. }
  3797. vp9_encode_frame(cpi);
  3798. vpx_clear_system_state();
  3799. restore_coding_context(cpi);
  3800. }
  3801. }
  3802. #endif // !CONFIG_REALTIME_ONLY
  3803. static int get_ref_frame_flags(const VP9_COMP *cpi) {
  3804. const int *const map = cpi->common.ref_frame_map;
  3805. const int gold_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idx];
  3806. const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idx];
  3807. const int gold_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
  3808. int flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
  3809. if (gold_is_last) flags &= ~VP9_GOLD_FLAG;
  3810. if (cpi->rc.frames_till_gf_update_due == INT_MAX &&
  3811. (cpi->svc.number_temporal_layers == 1 &&
  3812. cpi->svc.number_spatial_layers == 1))
  3813. flags &= ~VP9_GOLD_FLAG;
  3814. if (alt_is_last) flags &= ~VP9_ALT_FLAG;
  3815. if (gold_is_alt) flags &= ~VP9_ALT_FLAG;
  3816. return flags;
  3817. }
  3818. static void set_ext_overrides(VP9_COMP *cpi) {
  3819. // Overrides the defaults with the externally supplied values with
  3820. // vp9_update_reference() and vp9_update_entropy() calls
  3821. // Note: The overrides are valid only for the next frame passed
  3822. // to encode_frame_to_data_rate() function
  3823. if (cpi->ext_refresh_frame_context_pending) {
  3824. cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
  3825. cpi->ext_refresh_frame_context_pending = 0;
  3826. }
  3827. if (cpi->ext_refresh_frame_flags_pending) {
  3828. cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
  3829. cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
  3830. cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
  3831. }
  3832. }
  3833. YV12_BUFFER_CONFIG *vp9_svc_twostage_scale(
  3834. VP9_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled,
  3835. YV12_BUFFER_CONFIG *scaled_temp, INTERP_FILTER filter_type,
  3836. int phase_scaler, INTERP_FILTER filter_type2, int phase_scaler2) {
  3837. if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
  3838. cm->mi_rows * MI_SIZE != unscaled->y_height) {
  3839. #if CONFIG_VP9_HIGHBITDEPTH
  3840. if (cm->bit_depth == VPX_BITS_8) {
  3841. vp9_scale_and_extend_frame(unscaled, scaled_temp, filter_type2,
  3842. phase_scaler2);
  3843. vp9_scale_and_extend_frame(scaled_temp, scaled, filter_type,
  3844. phase_scaler);
  3845. } else {
  3846. scale_and_extend_frame(unscaled, scaled_temp, (int)cm->bit_depth,
  3847. filter_type2, phase_scaler2);
  3848. scale_and_extend_frame(scaled_temp, scaled, (int)cm->bit_depth,
  3849. filter_type, phase_scaler);
  3850. }
  3851. #else
  3852. vp9_scale_and_extend_frame(unscaled, scaled_temp, filter_type2,
  3853. phase_scaler2);
  3854. vp9_scale_and_extend_frame(scaled_temp, scaled, filter_type, phase_scaler);
  3855. #endif // CONFIG_VP9_HIGHBITDEPTH
  3856. return scaled;
  3857. } else {
  3858. return unscaled;
  3859. }
  3860. }
  3861. YV12_BUFFER_CONFIG *vp9_scale_if_required(
  3862. VP9_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled,
  3863. int use_normative_scaler, INTERP_FILTER filter_type, int phase_scaler) {
  3864. if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
  3865. cm->mi_rows * MI_SIZE != unscaled->y_height) {
  3866. #if CONFIG_VP9_HIGHBITDEPTH
  3867. if (use_normative_scaler && unscaled->y_width <= (scaled->y_width << 1) &&
  3868. unscaled->y_height <= (scaled->y_height << 1))
  3869. if (cm->bit_depth == VPX_BITS_8)
  3870. vp9_scale_and_extend_frame(unscaled, scaled, filter_type, phase_scaler);
  3871. else
  3872. scale_and_extend_frame(unscaled, scaled, (int)cm->bit_depth,
  3873. filter_type, phase_scaler);
  3874. else
  3875. scale_and_extend_frame_nonnormative(unscaled, scaled, (int)cm->bit_depth);
  3876. #else
  3877. if (use_normative_scaler && unscaled->y_width <= (scaled->y_width << 1) &&
  3878. unscaled->y_height <= (scaled->y_height << 1))
  3879. vp9_scale_and_extend_frame(unscaled, scaled, filter_type, phase_scaler);
  3880. else
  3881. scale_and_extend_frame_nonnormative(unscaled, scaled);
  3882. #endif // CONFIG_VP9_HIGHBITDEPTH
  3883. return scaled;
  3884. } else {
  3885. return unscaled;
  3886. }
  3887. }
  3888. static void set_ref_sign_bias(VP9_COMP *cpi) {
  3889. VP9_COMMON *const cm = &cpi->common;
  3890. RefCntBuffer *const ref_buffer = get_ref_cnt_buffer(cm, cm->new_fb_idx);
  3891. const int cur_frame_index = ref_buffer->frame_index;
  3892. MV_REFERENCE_FRAME ref_frame;
  3893. for (ref_frame = LAST_FRAME; ref_frame < MAX_REF_FRAMES; ++ref_frame) {
  3894. const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
  3895. const RefCntBuffer *const ref_cnt_buf =
  3896. get_ref_cnt_buffer(&cpi->common, buf_idx);
  3897. if (ref_cnt_buf) {
  3898. cm->ref_frame_sign_bias[ref_frame] =
  3899. cur_frame_index < ref_cnt_buf->frame_index;
  3900. }
  3901. }
  3902. }
  3903. static int setup_interp_filter_search_mask(VP9_COMP *cpi) {
  3904. INTERP_FILTER ifilter;
  3905. int ref_total[MAX_REF_FRAMES] = { 0 };
  3906. MV_REFERENCE_FRAME ref;
  3907. int mask = 0;
  3908. if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
  3909. return mask;
  3910. for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
  3911. for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter)
  3912. ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
  3913. for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter) {
  3914. if ((ref_total[LAST_FRAME] &&
  3915. cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
  3916. (ref_total[GOLDEN_FRAME] == 0 ||
  3917. cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50 <
  3918. ref_total[GOLDEN_FRAME]) &&
  3919. (ref_total[ALTREF_FRAME] == 0 ||
  3920. cpi->interp_filter_selected[ALTREF_FRAME][ifilter] * 50 <
  3921. ref_total[ALTREF_FRAME]))
  3922. mask |= 1 << ifilter;
  3923. }
  3924. return mask;
  3925. }
  3926. #ifdef ENABLE_KF_DENOISE
  3927. // Baseline Kernal weights for denoise
  3928. static uint8_t dn_kernal_3[9] = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
  3929. static uint8_t dn_kernal_5[25] = { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 4,
  3930. 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1 };
  3931. static INLINE void add_denoise_point(int centre_val, int data_val, int thresh,
  3932. uint8_t point_weight, int *sum_val,
  3933. int *sum_weight) {
  3934. if (abs(centre_val - data_val) <= thresh) {
  3935. *sum_weight += point_weight;
  3936. *sum_val += (int)data_val * (int)point_weight;
  3937. }
  3938. }
  3939. static void spatial_denoise_point(uint8_t *src_ptr, const int stride,
  3940. const int strength) {
  3941. int sum_weight = 0;
  3942. int sum_val = 0;
  3943. int thresh = strength;
  3944. int kernal_size = 5;
  3945. int half_k_size = 2;
  3946. int i, j;
  3947. int max_diff = 0;
  3948. uint8_t *tmp_ptr;
  3949. uint8_t *kernal_ptr;
  3950. // Find the maximum deviation from the source point in the locale.
  3951. tmp_ptr = src_ptr - (stride * (half_k_size + 1)) - (half_k_size + 1);
  3952. for (i = 0; i < kernal_size + 2; ++i) {
  3953. for (j = 0; j < kernal_size + 2; ++j) {
  3954. max_diff = VPXMAX(max_diff, abs((int)*src_ptr - (int)tmp_ptr[j]));
  3955. }
  3956. tmp_ptr += stride;
  3957. }
  3958. // Select the kernal size.
  3959. if (max_diff > (strength + (strength >> 1))) {
  3960. kernal_size = 3;
  3961. half_k_size = 1;
  3962. thresh = thresh >> 1;
  3963. }
  3964. kernal_ptr = (kernal_size == 3) ? dn_kernal_3 : dn_kernal_5;
  3965. // Apply the kernal
  3966. tmp_ptr = src_ptr - (stride * half_k_size) - half_k_size;
  3967. for (i = 0; i < kernal_size; ++i) {
  3968. for (j = 0; j < kernal_size; ++j) {
  3969. add_denoise_point((int)*src_ptr, (int)tmp_ptr[j], thresh, *kernal_ptr,
  3970. &sum_val, &sum_weight);
  3971. ++kernal_ptr;
  3972. }
  3973. tmp_ptr += stride;
  3974. }
  3975. // Update the source value with the new filtered value
  3976. *src_ptr = (uint8_t)((sum_val + (sum_weight >> 1)) / sum_weight);
  3977. }
  3978. #if CONFIG_VP9_HIGHBITDEPTH
  3979. static void highbd_spatial_denoise_point(uint16_t *src_ptr, const int stride,
  3980. const int strength) {
  3981. int sum_weight = 0;
  3982. int sum_val = 0;
  3983. int thresh = strength;
  3984. int kernal_size = 5;
  3985. int half_k_size = 2;
  3986. int i, j;
  3987. int max_diff = 0;
  3988. uint16_t *tmp_ptr;
  3989. uint8_t *kernal_ptr;
  3990. // Find the maximum deviation from the source point in the locale.
  3991. tmp_ptr = src_ptr - (stride * (half_k_size + 1)) - (half_k_size + 1);
  3992. for (i = 0; i < kernal_size + 2; ++i) {
  3993. for (j = 0; j < kernal_size + 2; ++j) {
  3994. max_diff = VPXMAX(max_diff, abs((int)src_ptr - (int)tmp_ptr[j]));
  3995. }
  3996. tmp_ptr += stride;
  3997. }
  3998. // Select the kernal size.
  3999. if (max_diff > (strength + (strength >> 1))) {
  4000. kernal_size = 3;
  4001. half_k_size = 1;
  4002. thresh = thresh >> 1;
  4003. }
  4004. kernal_ptr = (kernal_size == 3) ? dn_kernal_3 : dn_kernal_5;
  4005. // Apply the kernal
  4006. tmp_ptr = src_ptr - (stride * half_k_size) - half_k_size;
  4007. for (i = 0; i < kernal_size; ++i) {
  4008. for (j = 0; j < kernal_size; ++j) {
  4009. add_denoise_point((int)*src_ptr, (int)tmp_ptr[j], thresh, *kernal_ptr,
  4010. &sum_val, &sum_weight);
  4011. ++kernal_ptr;
  4012. }
  4013. tmp_ptr += stride;
  4014. }
  4015. // Update the source value with the new filtered value
  4016. *src_ptr = (uint16_t)((sum_val + (sum_weight >> 1)) / sum_weight);
  4017. }
  4018. #endif // CONFIG_VP9_HIGHBITDEPTH
  4019. // Apply thresholded spatial noise supression to a given buffer.
  4020. static void spatial_denoise_buffer(VP9_COMP *cpi, uint8_t *buffer,
  4021. const int stride, const int width,
  4022. const int height, const int strength) {
  4023. VP9_COMMON *const cm = &cpi->common;
  4024. uint8_t *src_ptr = buffer;
  4025. int row;
  4026. int col;
  4027. for (row = 0; row < height; ++row) {
  4028. for (col = 0; col < width; ++col) {
  4029. #if CONFIG_VP9_HIGHBITDEPTH
  4030. if (cm->use_highbitdepth)
  4031. highbd_spatial_denoise_point(CONVERT_TO_SHORTPTR(&src_ptr[col]), stride,
  4032. strength);
  4033. else
  4034. spatial_denoise_point(&src_ptr[col], stride, strength);
  4035. #else
  4036. spatial_denoise_point(&src_ptr[col], stride, strength);
  4037. #endif // CONFIG_VP9_HIGHBITDEPTH
  4038. }
  4039. src_ptr += stride;
  4040. }
  4041. }
  4042. // Apply thresholded spatial noise supression to source.
  4043. static void spatial_denoise_frame(VP9_COMP *cpi) {
  4044. YV12_BUFFER_CONFIG *src = cpi->Source;
  4045. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  4046. TWO_PASS *const twopass = &cpi->twopass;
  4047. VP9_COMMON *const cm = &cpi->common;
  4048. // Base the filter strength on the current active max Q.
  4049. const int q = (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality,
  4050. cm->bit_depth));
  4051. int strength =
  4052. VPXMAX(oxcf->arnr_strength >> 2, VPXMIN(oxcf->arnr_strength, (q >> 4)));
  4053. // Denoise each of Y,U and V buffers.
  4054. spatial_denoise_buffer(cpi, src->y_buffer, src->y_stride, src->y_width,
  4055. src->y_height, strength);
  4056. strength += (strength >> 1);
  4057. spatial_denoise_buffer(cpi, src->u_buffer, src->uv_stride, src->uv_width,
  4058. src->uv_height, strength << 1);
  4059. spatial_denoise_buffer(cpi, src->v_buffer, src->uv_stride, src->uv_width,
  4060. src->uv_height, strength << 1);
  4061. }
  4062. #endif // ENABLE_KF_DENOISE
  4063. #if !CONFIG_REALTIME_ONLY
  4064. static void vp9_try_disable_lookahead_aq(VP9_COMP *cpi, size_t *size,
  4065. uint8_t *dest) {
  4066. if (cpi->common.seg.enabled)
  4067. if (ALT_REF_AQ_PROTECT_GAIN) {
  4068. size_t nsize = *size;
  4069. int overhead;
  4070. // TODO(yuryg): optimize this, as
  4071. // we don't really need to repack
  4072. save_coding_context(cpi);
  4073. vp9_disable_segmentation(&cpi->common.seg);
  4074. vp9_pack_bitstream(cpi, dest, &nsize);
  4075. restore_coding_context(cpi);
  4076. overhead = (int)*size - (int)nsize;
  4077. if (vp9_alt_ref_aq_disable_if(cpi->alt_ref_aq, overhead, (int)*size))
  4078. vp9_encode_frame(cpi);
  4079. else
  4080. vp9_enable_segmentation(&cpi->common.seg);
  4081. }
  4082. }
  4083. #endif
  4084. static void set_frame_index(VP9_COMP *cpi, VP9_COMMON *cm) {
  4085. RefCntBuffer *const ref_buffer = get_ref_cnt_buffer(cm, cm->new_fb_idx);
  4086. if (ref_buffer) {
  4087. const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
  4088. ref_buffer->frame_index =
  4089. cm->current_video_frame + gf_group->arf_src_offset[gf_group->index];
  4090. }
  4091. }
  4092. // Implementation and modifications of C. Yeo, H. L. Tan, and Y. H. Tan, "On
  4093. // rate distortion optimization using SSIM," Circuits and Systems for Video
  4094. // Technology, IEEE Transactions on, vol. 23, no. 7, pp. 1170-1181, 2013.
  4095. // SSIM_VAR_SCALE defines the strength of the bias towards SSIM in RDO.
  4096. // Some sample values are:
  4097. // (for midres test set)
  4098. // SSIM_VAR_SCALE avg_psnr ssim ms_ssim
  4099. // 8.0 9.421 -5.537 -6.898
  4100. // 16.0 4.703 -5.378 -6.238
  4101. // 32.0 1.929 -4.308 -4.807
  4102. #define SSIM_VAR_SCALE 16.0
  4103. static void set_mb_ssim_rdmult_scaling(VP9_COMP *cpi) {
  4104. VP9_COMMON *cm = &cpi->common;
  4105. ThreadData *td = &cpi->td;
  4106. MACROBLOCK *x = &td->mb;
  4107. MACROBLOCKD *xd = &x->e_mbd;
  4108. uint8_t *y_buffer = cpi->Source->y_buffer;
  4109. const int y_stride = cpi->Source->y_stride;
  4110. const int block_size = BLOCK_16X16;
  4111. const int num_8x8_w = num_8x8_blocks_wide_lookup[block_size];
  4112. const int num_8x8_h = num_8x8_blocks_high_lookup[block_size];
  4113. const int num_cols = (cm->mi_cols + num_8x8_w - 1) / num_8x8_w;
  4114. const int num_rows = (cm->mi_rows + num_8x8_h - 1) / num_8x8_h;
  4115. double log_sum = 0.0;
  4116. int row, col;
  4117. #if CONFIG_VP9_HIGHBITDEPTH
  4118. double c2;
  4119. if (xd->bd == 10) {
  4120. c2 = 941.8761; // (.03*1023)^2
  4121. } else if (xd->bd == 12) {
  4122. c2 = 15092.1225; // (.03*4095)^2
  4123. } else {
  4124. c2 = 58.5225; // (.03*255)^2
  4125. }
  4126. #else
  4127. const double c2 = 58.5225; // (.03*255)^2
  4128. #endif
  4129. // Loop through each 64x64 block.
  4130. for (row = 0; row < num_rows; ++row) {
  4131. for (col = 0; col < num_cols; ++col) {
  4132. int mi_row, mi_col;
  4133. double var = 0.0, num_of_var = 0.0;
  4134. const int index = row * num_cols + col;
  4135. for (mi_row = row * num_8x8_h;
  4136. mi_row < cm->mi_rows && mi_row < (row + 1) * num_8x8_h; ++mi_row) {
  4137. for (mi_col = col * num_8x8_w;
  4138. mi_col < cm->mi_cols && mi_col < (col + 1) * num_8x8_w; ++mi_col) {
  4139. struct buf_2d buf;
  4140. const int row_offset_y = mi_row << 3;
  4141. const int col_offset_y = mi_col << 3;
  4142. buf.buf = y_buffer + row_offset_y * y_stride + col_offset_y;
  4143. buf.stride = y_stride;
  4144. // In order to make SSIM_VAR_SCALE in a same scale for both 8 bit
  4145. // and high bit videos, the variance needs to be divided by 2.0 or
  4146. // 64.0 separately.
  4147. #if CONFIG_VP9_HIGHBITDEPTH
  4148. if (cpi->Source->flags & YV12_FLAG_HIGHBITDEPTH)
  4149. var +=
  4150. vp9_high_get_sby_variance(cpi, &buf, BLOCK_8X8, xd->bd) / 2.0;
  4151. else
  4152. #endif
  4153. var += vp9_get_sby_variance(cpi, &buf, BLOCK_8X8) / 64.0;
  4154. num_of_var += 1.0;
  4155. }
  4156. }
  4157. var = var / num_of_var / SSIM_VAR_SCALE;
  4158. var = 2.0 * var + c2;
  4159. cpi->mi_ssim_rdmult_scaling_factors[index] = var;
  4160. log_sum += log(var);
  4161. }
  4162. }
  4163. log_sum = exp(log_sum / (double)(num_rows * num_cols));
  4164. for (row = 0; row < num_rows; ++row) {
  4165. for (col = 0; col < num_cols; ++col) {
  4166. const int index = row * num_cols + col;
  4167. cpi->mi_ssim_rdmult_scaling_factors[index] /= log_sum;
  4168. }
  4169. }
  4170. (void)xd;
  4171. }
  4172. // Process the wiener variance in 16x16 block basis.
  4173. static int qsort_comp(const void *elem1, const void *elem2) {
  4174. int a = *((const int *)elem1);
  4175. int b = *((const int *)elem2);
  4176. if (a > b) return 1;
  4177. if (a < b) return -1;
  4178. return 0;
  4179. }
  4180. static void init_mb_wiener_var_buffer(VP9_COMP *cpi) {
  4181. VP9_COMMON *cm = &cpi->common;
  4182. if (cpi->mb_wiener_variance && cpi->mb_wiener_var_rows >= cm->mb_rows &&
  4183. cpi->mb_wiener_var_cols >= cm->mb_cols)
  4184. return;
  4185. vpx_free(cpi->mb_wiener_variance);
  4186. cpi->mb_wiener_variance = NULL;
  4187. CHECK_MEM_ERROR(
  4188. cm, cpi->mb_wiener_variance,
  4189. vpx_calloc(cm->mb_rows * cm->mb_cols, sizeof(*cpi->mb_wiener_variance)));
  4190. cpi->mb_wiener_var_rows = cm->mb_rows;
  4191. cpi->mb_wiener_var_cols = cm->mb_cols;
  4192. }
  4193. static void set_mb_wiener_variance(VP9_COMP *cpi) {
  4194. VP9_COMMON *cm = &cpi->common;
  4195. uint8_t *buffer = cpi->Source->y_buffer;
  4196. int buf_stride = cpi->Source->y_stride;
  4197. #if CONFIG_VP9_HIGHBITDEPTH
  4198. ThreadData *td = &cpi->td;
  4199. MACROBLOCK *x = &td->mb;
  4200. MACROBLOCKD *xd = &x->e_mbd;
  4201. DECLARE_ALIGNED(16, uint16_t, zero_pred16[32 * 32]);
  4202. DECLARE_ALIGNED(16, uint8_t, zero_pred8[32 * 32]);
  4203. uint8_t *zero_pred;
  4204. #else
  4205. DECLARE_ALIGNED(16, uint8_t, zero_pred[32 * 32]);
  4206. #endif
  4207. DECLARE_ALIGNED(16, int16_t, src_diff[32 * 32]);
  4208. DECLARE_ALIGNED(16, tran_low_t, coeff[32 * 32]);
  4209. int mb_row, mb_col, count = 0;
  4210. // Hard coded operating block size
  4211. const int block_size = 16;
  4212. const int coeff_count = block_size * block_size;
  4213. const TX_SIZE tx_size = TX_16X16;
  4214. #if CONFIG_VP9_HIGHBITDEPTH
  4215. xd->cur_buf = cpi->Source;
  4216. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  4217. zero_pred = CONVERT_TO_BYTEPTR(zero_pred16);
  4218. memset(zero_pred16, 0, sizeof(*zero_pred16) * coeff_count);
  4219. } else {
  4220. zero_pred = zero_pred8;
  4221. memset(zero_pred8, 0, sizeof(*zero_pred8) * coeff_count);
  4222. }
  4223. #else
  4224. memset(zero_pred, 0, sizeof(*zero_pred) * coeff_count);
  4225. #endif
  4226. cpi->norm_wiener_variance = 0;
  4227. for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) {
  4228. for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
  4229. int idx;
  4230. int16_t median_val = 0;
  4231. uint8_t *mb_buffer =
  4232. buffer + mb_row * block_size * buf_stride + mb_col * block_size;
  4233. int64_t wiener_variance = 0;
  4234. #if CONFIG_VP9_HIGHBITDEPTH
  4235. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  4236. vpx_highbd_subtract_block(block_size, block_size, src_diff, block_size,
  4237. mb_buffer, buf_stride, zero_pred, block_size,
  4238. xd->bd);
  4239. highbd_wht_fwd_txfm(src_diff, block_size, coeff, tx_size);
  4240. } else {
  4241. vpx_subtract_block(block_size, block_size, src_diff, block_size,
  4242. mb_buffer, buf_stride, zero_pred, block_size);
  4243. wht_fwd_txfm(src_diff, block_size, coeff, tx_size);
  4244. }
  4245. #else
  4246. vpx_subtract_block(block_size, block_size, src_diff, block_size,
  4247. mb_buffer, buf_stride, zero_pred, block_size);
  4248. wht_fwd_txfm(src_diff, block_size, coeff, tx_size);
  4249. #endif // CONFIG_VP9_HIGHBITDEPTH
  4250. coeff[0] = 0;
  4251. for (idx = 1; idx < coeff_count; ++idx) coeff[idx] = abs(coeff[idx]);
  4252. qsort(coeff, coeff_count - 1, sizeof(*coeff), qsort_comp);
  4253. // Noise level estimation
  4254. median_val = coeff[coeff_count / 2];
  4255. // Wiener filter
  4256. for (idx = 1; idx < coeff_count; ++idx) {
  4257. int64_t sqr_coeff = (int64_t)coeff[idx] * coeff[idx];
  4258. int64_t tmp_coeff = (int64_t)coeff[idx];
  4259. if (median_val) {
  4260. tmp_coeff = (sqr_coeff * coeff[idx]) /
  4261. (sqr_coeff + (int64_t)median_val * median_val);
  4262. }
  4263. wiener_variance += tmp_coeff * tmp_coeff;
  4264. }
  4265. cpi->mb_wiener_variance[mb_row * cm->mb_cols + mb_col] =
  4266. wiener_variance / coeff_count;
  4267. cpi->norm_wiener_variance +=
  4268. cpi->mb_wiener_variance[mb_row * cm->mb_cols + mb_col];
  4269. ++count;
  4270. }
  4271. }
  4272. if (count) cpi->norm_wiener_variance /= count;
  4273. cpi->norm_wiener_variance = VPXMAX(1, cpi->norm_wiener_variance);
  4274. }
  4275. static void encode_frame_to_data_rate(VP9_COMP *cpi, size_t *size,
  4276. uint8_t *dest,
  4277. unsigned int *frame_flags) {
  4278. VP9_COMMON *const cm = &cpi->common;
  4279. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  4280. struct segmentation *const seg = &cm->seg;
  4281. TX_SIZE t;
  4282. // SVC: skip encoding of enhancement layer if the layer target bandwidth = 0.
  4283. // If in constrained layer drop mode (svc.framedrop_mode != LAYER_DROP) and
  4284. // base spatial layer was dropped, no need to set svc.skip_enhancement_layer,
  4285. // as whole superframe will be dropped.
  4286. if (cpi->use_svc && cpi->svc.spatial_layer_id > 0 &&
  4287. cpi->oxcf.target_bandwidth == 0 &&
  4288. !(cpi->svc.framedrop_mode != LAYER_DROP &&
  4289. cpi->svc.drop_spatial_layer[0])) {
  4290. cpi->svc.skip_enhancement_layer = 1;
  4291. vp9_rc_postencode_update_drop_frame(cpi);
  4292. cpi->ext_refresh_frame_flags_pending = 0;
  4293. cpi->last_frame_dropped = 1;
  4294. cpi->svc.last_layer_dropped[cpi->svc.spatial_layer_id] = 1;
  4295. cpi->svc.drop_spatial_layer[cpi->svc.spatial_layer_id] = 1;
  4296. if (cpi->svc.framedrop_mode == LAYER_DROP ||
  4297. cpi->svc.drop_spatial_layer[0] == 0) {
  4298. // For the case of constrained drop mode where the base is dropped
  4299. // (drop_spatial_layer[0] == 1), which means full superframe dropped,
  4300. // we don't increment the svc frame counters. In particular temporal
  4301. // layer counter (which is incremented in vp9_inc_frame_in_layer())
  4302. // won't be incremented, so on a dropped frame we try the same
  4303. // temporal_layer_id on next incoming frame. This is to avoid an
  4304. // issue with temporal alignement with full superframe dropping.
  4305. vp9_inc_frame_in_layer(cpi);
  4306. }
  4307. return;
  4308. }
  4309. set_ext_overrides(cpi);
  4310. vpx_clear_system_state();
  4311. #ifdef ENABLE_KF_DENOISE
  4312. // Spatial denoise of key frame.
  4313. if (is_spatial_denoise_enabled(cpi)) spatial_denoise_frame(cpi);
  4314. #endif
  4315. if (cm->show_existing_frame == 0) {
  4316. // Update frame index
  4317. set_frame_index(cpi, cm);
  4318. // Set the arf sign bias for this frame.
  4319. set_ref_sign_bias(cpi);
  4320. }
  4321. // Set default state for segment based loop filter update flags.
  4322. cm->lf.mode_ref_delta_update = 0;
  4323. if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
  4324. cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
  4325. // Set various flags etc to special state if it is a key frame.
  4326. if (frame_is_intra_only(cm)) {
  4327. // Reset the loop filter deltas and segmentation map.
  4328. vp9_reset_segment_features(&cm->seg);
  4329. // If segmentation is enabled force a map update for key frames.
  4330. if (seg->enabled) {
  4331. seg->update_map = 1;
  4332. seg->update_data = 1;
  4333. }
  4334. // The alternate reference frame cannot be active for a key frame.
  4335. cpi->rc.source_alt_ref_active = 0;
  4336. cm->error_resilient_mode = oxcf->error_resilient_mode;
  4337. cm->frame_parallel_decoding_mode = oxcf->frame_parallel_decoding_mode;
  4338. // By default, encoder assumes decoder can use prev_mi.
  4339. if (cm->error_resilient_mode) {
  4340. cm->frame_parallel_decoding_mode = 1;
  4341. cm->reset_frame_context = 0;
  4342. cm->refresh_frame_context = 0;
  4343. } else if (cm->intra_only) {
  4344. // Only reset the current context.
  4345. cm->reset_frame_context = 2;
  4346. }
  4347. }
  4348. if (oxcf->tuning == VP8_TUNE_SSIM) set_mb_ssim_rdmult_scaling(cpi);
  4349. if (oxcf->aq_mode == PERCEPTUAL_AQ) {
  4350. init_mb_wiener_var_buffer(cpi);
  4351. set_mb_wiener_variance(cpi);
  4352. }
  4353. vpx_clear_system_state();
  4354. #if CONFIG_INTERNAL_STATS
  4355. memset(cpi->mode_chosen_counts, 0,
  4356. MAX_MODES * sizeof(*cpi->mode_chosen_counts));
  4357. #endif
  4358. #if CONFIG_CONSISTENT_RECODE
  4359. // Backup to ensure consistency between recodes
  4360. save_encode_params(cpi);
  4361. #endif
  4362. if (cpi->sf.recode_loop == DISALLOW_RECODE) {
  4363. if (!encode_without_recode_loop(cpi, size, dest)) return;
  4364. } else {
  4365. #if !CONFIG_REALTIME_ONLY
  4366. encode_with_recode_loop(cpi, size, dest);
  4367. #endif
  4368. }
  4369. // TODO(jingning): When using show existing frame mode, we assume that the
  4370. // current ARF will be directly used as the final reconstructed frame. This is
  4371. // an encoder control scheme. One could in principle explore other
  4372. // possibilities to arrange the reference frame buffer and their coding order.
  4373. if (cm->show_existing_frame) {
  4374. ref_cnt_fb(cm->buffer_pool->frame_bufs, &cm->new_fb_idx,
  4375. cm->ref_frame_map[cpi->alt_fb_idx]);
  4376. }
  4377. #if !CONFIG_REALTIME_ONLY
  4378. // Disable segmentation if it decrease rate/distortion ratio
  4379. if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ)
  4380. vp9_try_disable_lookahead_aq(cpi, size, dest);
  4381. #endif
  4382. #if CONFIG_VP9_TEMPORAL_DENOISING
  4383. #ifdef OUTPUT_YUV_DENOISED
  4384. if (oxcf->noise_sensitivity > 0 && denoise_svc(cpi)) {
  4385. vpx_write_yuv_frame(yuv_denoised_file,
  4386. &cpi->denoiser.running_avg_y[INTRA_FRAME]);
  4387. }
  4388. #endif
  4389. #endif
  4390. #ifdef OUTPUT_YUV_SKINMAP
  4391. if (cpi->common.current_video_frame > 1) {
  4392. vp9_output_skin_map(cpi, yuv_skinmap_file);
  4393. }
  4394. #endif
  4395. // Special case code to reduce pulsing when key frames are forced at a
  4396. // fixed interval. Note the reconstruction error if it is the frame before
  4397. // the force key frame
  4398. if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
  4399. #if CONFIG_VP9_HIGHBITDEPTH
  4400. if (cm->use_highbitdepth) {
  4401. cpi->ambient_err =
  4402. vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
  4403. } else {
  4404. cpi->ambient_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
  4405. }
  4406. #else
  4407. cpi->ambient_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
  4408. #endif // CONFIG_VP9_HIGHBITDEPTH
  4409. }
  4410. // If the encoder forced a KEY_FRAME decision
  4411. if (cm->frame_type == KEY_FRAME) cpi->refresh_last_frame = 1;
  4412. cm->frame_to_show = get_frame_new_buffer(cm);
  4413. cm->frame_to_show->color_space = cm->color_space;
  4414. cm->frame_to_show->color_range = cm->color_range;
  4415. cm->frame_to_show->render_width = cm->render_width;
  4416. cm->frame_to_show->render_height = cm->render_height;
  4417. // Pick the loop filter level for the frame.
  4418. loopfilter_frame(cpi, cm);
  4419. if (cpi->rc.use_post_encode_drop) save_coding_context(cpi);
  4420. // build the bitstream
  4421. vp9_pack_bitstream(cpi, dest, size);
  4422. if (cpi->rc.use_post_encode_drop && cm->base_qindex < cpi->rc.worst_quality &&
  4423. cpi->svc.spatial_layer_id == 0 && post_encode_drop_cbr(cpi, size)) {
  4424. restore_coding_context(cpi);
  4425. return;
  4426. }
  4427. cpi->last_frame_dropped = 0;
  4428. cpi->svc.last_layer_dropped[cpi->svc.spatial_layer_id] = 0;
  4429. if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)
  4430. cpi->svc.num_encoded_top_layer++;
  4431. // Keep track of the frame buffer index updated/refreshed for the
  4432. // current encoded TL0 superframe.
  4433. if (cpi->svc.temporal_layer_id == 0) {
  4434. if (cpi->refresh_last_frame)
  4435. cpi->svc.fb_idx_upd_tl0[cpi->svc.spatial_layer_id] = cpi->lst_fb_idx;
  4436. else if (cpi->refresh_golden_frame)
  4437. cpi->svc.fb_idx_upd_tl0[cpi->svc.spatial_layer_id] = cpi->gld_fb_idx;
  4438. else if (cpi->refresh_alt_ref_frame)
  4439. cpi->svc.fb_idx_upd_tl0[cpi->svc.spatial_layer_id] = cpi->alt_fb_idx;
  4440. }
  4441. if (cm->seg.update_map) update_reference_segmentation_map(cpi);
  4442. if (frame_is_intra_only(cm) == 0) {
  4443. release_scaled_references(cpi);
  4444. }
  4445. vp9_update_reference_frames(cpi);
  4446. if (!cm->show_existing_frame) {
  4447. for (t = TX_4X4; t <= TX_32X32; ++t) {
  4448. full_to_model_counts(cpi->td.counts->coef[t],
  4449. cpi->td.rd_counts.coef_counts[t]);
  4450. }
  4451. if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
  4452. if (!frame_is_intra_only(cm)) {
  4453. vp9_adapt_mode_probs(cm);
  4454. vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
  4455. }
  4456. vp9_adapt_coef_probs(cm);
  4457. }
  4458. }
  4459. cpi->ext_refresh_frame_flags_pending = 0;
  4460. if (cpi->refresh_golden_frame == 1)
  4461. cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
  4462. else
  4463. cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
  4464. if (cpi->refresh_alt_ref_frame == 1)
  4465. cpi->frame_flags |= FRAMEFLAGS_ALTREF;
  4466. else
  4467. cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
  4468. cpi->ref_frame_flags = get_ref_frame_flags(cpi);
  4469. cm->last_frame_type = cm->frame_type;
  4470. vp9_rc_postencode_update(cpi, *size);
  4471. *size = VPXMAX(1, *size);
  4472. #if 0
  4473. output_frame_level_debug_stats(cpi);
  4474. #endif
  4475. if (cm->frame_type == KEY_FRAME) {
  4476. // Tell the caller that the frame was coded as a key frame
  4477. *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
  4478. } else {
  4479. *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
  4480. }
  4481. // Clear the one shot update flags for segmentation map and mode/ref loop
  4482. // filter deltas.
  4483. cm->seg.update_map = 0;
  4484. cm->seg.update_data = 0;
  4485. cm->lf.mode_ref_delta_update = 0;
  4486. // keep track of the last coded dimensions
  4487. cm->last_width = cm->width;
  4488. cm->last_height = cm->height;
  4489. // reset to normal state now that we are done.
  4490. if (!cm->show_existing_frame) {
  4491. cm->last_show_frame = cm->show_frame;
  4492. cm->prev_frame = cm->cur_frame;
  4493. }
  4494. if (cm->show_frame) {
  4495. vp9_swap_mi_and_prev_mi(cm);
  4496. // Don't increment frame counters if this was an altref buffer
  4497. // update not a real frame
  4498. ++cm->current_video_frame;
  4499. if (cpi->use_svc) vp9_inc_frame_in_layer(cpi);
  4500. }
  4501. if (cpi->use_svc) {
  4502. cpi->svc
  4503. .layer_context[cpi->svc.spatial_layer_id *
  4504. cpi->svc.number_temporal_layers +
  4505. cpi->svc.temporal_layer_id]
  4506. .last_frame_type = cm->frame_type;
  4507. // Reset layer_sync back to 0 for next frame.
  4508. cpi->svc.spatial_layer_sync[cpi->svc.spatial_layer_id] = 0;
  4509. }
  4510. cpi->force_update_segmentation = 0;
  4511. #if !CONFIG_REALTIME_ONLY
  4512. if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ)
  4513. vp9_alt_ref_aq_unset_all(cpi->alt_ref_aq, cpi);
  4514. #endif
  4515. cpi->svc.previous_frame_is_intra_only = cm->intra_only;
  4516. cpi->svc.set_intra_only_frame = 0;
  4517. }
  4518. static void SvcEncode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
  4519. unsigned int *frame_flags) {
  4520. vp9_rc_get_svc_params(cpi);
  4521. encode_frame_to_data_rate(cpi, size, dest, frame_flags);
  4522. }
  4523. static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
  4524. unsigned int *frame_flags) {
  4525. if (cpi->oxcf.rc_mode == VPX_CBR) {
  4526. vp9_rc_get_one_pass_cbr_params(cpi);
  4527. } else {
  4528. vp9_rc_get_one_pass_vbr_params(cpi);
  4529. }
  4530. encode_frame_to_data_rate(cpi, size, dest, frame_flags);
  4531. }
  4532. #if !CONFIG_REALTIME_ONLY
  4533. static void Pass2Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
  4534. unsigned int *frame_flags) {
  4535. cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
  4536. #if CONFIG_MISMATCH_DEBUG
  4537. mismatch_move_frame_idx_w();
  4538. #endif
  4539. encode_frame_to_data_rate(cpi, size, dest, frame_flags);
  4540. vp9_twopass_postencode_update(cpi);
  4541. }
  4542. #endif // !CONFIG_REALTIME_ONLY
  4543. static void init_ref_frame_bufs(VP9_COMMON *cm) {
  4544. int i;
  4545. BufferPool *const pool = cm->buffer_pool;
  4546. cm->new_fb_idx = INVALID_IDX;
  4547. for (i = 0; i < REF_FRAMES; ++i) {
  4548. cm->ref_frame_map[i] = INVALID_IDX;
  4549. }
  4550. for (i = 0; i < FRAME_BUFFERS; ++i) {
  4551. pool->frame_bufs[i].ref_count = 0;
  4552. }
  4553. }
  4554. static void check_initial_width(VP9_COMP *cpi,
  4555. #if CONFIG_VP9_HIGHBITDEPTH
  4556. int use_highbitdepth,
  4557. #endif
  4558. int subsampling_x, int subsampling_y) {
  4559. VP9_COMMON *const cm = &cpi->common;
  4560. if (!cpi->initial_width ||
  4561. #if CONFIG_VP9_HIGHBITDEPTH
  4562. cm->use_highbitdepth != use_highbitdepth ||
  4563. #endif
  4564. cm->subsampling_x != subsampling_x ||
  4565. cm->subsampling_y != subsampling_y) {
  4566. cm->subsampling_x = subsampling_x;
  4567. cm->subsampling_y = subsampling_y;
  4568. #if CONFIG_VP9_HIGHBITDEPTH
  4569. cm->use_highbitdepth = use_highbitdepth;
  4570. #endif
  4571. alloc_raw_frame_buffers(cpi);
  4572. init_ref_frame_bufs(cm);
  4573. alloc_util_frame_buffers(cpi);
  4574. init_motion_estimation(cpi); // TODO(agrange) This can be removed.
  4575. cpi->initial_width = cm->width;
  4576. cpi->initial_height = cm->height;
  4577. cpi->initial_mbs = cm->MBs;
  4578. }
  4579. }
  4580. int vp9_receive_raw_frame(VP9_COMP *cpi, vpx_enc_frame_flags_t frame_flags,
  4581. YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
  4582. int64_t end_time) {
  4583. VP9_COMMON *const cm = &cpi->common;
  4584. struct vpx_usec_timer timer;
  4585. int res = 0;
  4586. const int subsampling_x = sd->subsampling_x;
  4587. const int subsampling_y = sd->subsampling_y;
  4588. #if CONFIG_VP9_HIGHBITDEPTH
  4589. const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
  4590. #endif
  4591. #if CONFIG_VP9_HIGHBITDEPTH
  4592. check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
  4593. #else
  4594. check_initial_width(cpi, subsampling_x, subsampling_y);
  4595. #endif // CONFIG_VP9_HIGHBITDEPTH
  4596. #if CONFIG_VP9_HIGHBITDEPTH
  4597. // Disable denoiser for high bitdepth since vp9_denoiser_filter only works for
  4598. // 8 bits.
  4599. if (cm->bit_depth > 8) cpi->oxcf.noise_sensitivity = 0;
  4600. #endif
  4601. #if CONFIG_VP9_TEMPORAL_DENOISING
  4602. setup_denoiser_buffer(cpi);
  4603. #endif
  4604. vpx_usec_timer_start(&timer);
  4605. if (vp9_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
  4606. #if CONFIG_VP9_HIGHBITDEPTH
  4607. use_highbitdepth,
  4608. #endif // CONFIG_VP9_HIGHBITDEPTH
  4609. frame_flags))
  4610. res = -1;
  4611. vpx_usec_timer_mark(&timer);
  4612. cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
  4613. if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
  4614. (subsampling_x != 1 || subsampling_y != 1)) {
  4615. vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
  4616. "Non-4:2:0 color format requires profile 1 or 3");
  4617. res = -1;
  4618. }
  4619. if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
  4620. (subsampling_x == 1 && subsampling_y == 1)) {
  4621. vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
  4622. "4:2:0 color format requires profile 0 or 2");
  4623. res = -1;
  4624. }
  4625. return res;
  4626. }
  4627. static int frame_is_reference(const VP9_COMP *cpi) {
  4628. const VP9_COMMON *cm = &cpi->common;
  4629. return cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
  4630. cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame ||
  4631. cm->refresh_frame_context || cm->lf.mode_ref_delta_update ||
  4632. cm->seg.update_map || cm->seg.update_data;
  4633. }
  4634. static void adjust_frame_rate(VP9_COMP *cpi,
  4635. const struct lookahead_entry *source) {
  4636. int64_t this_duration;
  4637. int step = 0;
  4638. if (source->ts_start == cpi->first_time_stamp_ever) {
  4639. this_duration = source->ts_end - source->ts_start;
  4640. step = 1;
  4641. } else {
  4642. int64_t last_duration =
  4643. cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
  4644. this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
  4645. // do a step update if the duration changes by 10%
  4646. if (last_duration)
  4647. step = (int)((this_duration - last_duration) * 10 / last_duration);
  4648. }
  4649. if (this_duration) {
  4650. if (step) {
  4651. vp9_new_framerate(cpi, 10000000.0 / this_duration);
  4652. } else {
  4653. // Average this frame's rate into the last second's average
  4654. // frame rate. If we haven't seen 1 second yet, then average
  4655. // over the whole interval seen.
  4656. const double interval = VPXMIN(
  4657. (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
  4658. double avg_duration = 10000000.0 / cpi->framerate;
  4659. avg_duration *= (interval - avg_duration + this_duration);
  4660. avg_duration /= interval;
  4661. vp9_new_framerate(cpi, 10000000.0 / avg_duration);
  4662. }
  4663. }
  4664. cpi->last_time_stamp_seen = source->ts_start;
  4665. cpi->last_end_time_stamp_seen = source->ts_end;
  4666. }
  4667. // Returns 0 if this is not an alt ref else the offset of the source frame
  4668. // used as the arf midpoint.
  4669. static int get_arf_src_index(VP9_COMP *cpi) {
  4670. RATE_CONTROL *const rc = &cpi->rc;
  4671. int arf_src_index = 0;
  4672. if (is_altref_enabled(cpi)) {
  4673. if (cpi->oxcf.pass == 2) {
  4674. const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
  4675. if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
  4676. arf_src_index = gf_group->arf_src_offset[gf_group->index];
  4677. }
  4678. } else if (rc->source_alt_ref_pending) {
  4679. arf_src_index = rc->frames_till_gf_update_due;
  4680. }
  4681. }
  4682. return arf_src_index;
  4683. }
  4684. static void check_src_altref(VP9_COMP *cpi,
  4685. const struct lookahead_entry *source) {
  4686. RATE_CONTROL *const rc = &cpi->rc;
  4687. if (cpi->oxcf.pass == 2) {
  4688. const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
  4689. rc->is_src_frame_alt_ref =
  4690. (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
  4691. } else {
  4692. rc->is_src_frame_alt_ref =
  4693. cpi->alt_ref_source && (source == cpi->alt_ref_source);
  4694. }
  4695. if (rc->is_src_frame_alt_ref) {
  4696. // Current frame is an ARF overlay frame.
  4697. cpi->alt_ref_source = NULL;
  4698. // Don't refresh the last buffer for an ARF overlay frame. It will
  4699. // become the GF so preserve last as an alternative prediction option.
  4700. cpi->refresh_last_frame = 0;
  4701. }
  4702. }
  4703. #if CONFIG_INTERNAL_STATS
  4704. static void adjust_image_stat(double y, double u, double v, double all,
  4705. ImageStat *s) {
  4706. s->stat[Y] += y;
  4707. s->stat[U] += u;
  4708. s->stat[V] += v;
  4709. s->stat[ALL] += all;
  4710. s->worst = VPXMIN(s->worst, all);
  4711. }
  4712. #endif // CONFIG_INTERNAL_STATS
  4713. // Adjust the maximum allowable frame size for the target level.
  4714. static void level_rc_framerate(VP9_COMP *cpi, int arf_src_index) {
  4715. RATE_CONTROL *const rc = &cpi->rc;
  4716. LevelConstraint *const ls = &cpi->level_constraint;
  4717. VP9_COMMON *const cm = &cpi->common;
  4718. const double max_cpb_size = ls->max_cpb_size;
  4719. vpx_clear_system_state();
  4720. rc->max_frame_bandwidth = VPXMIN(rc->max_frame_bandwidth, ls->max_frame_size);
  4721. if (frame_is_intra_only(cm)) {
  4722. rc->max_frame_bandwidth =
  4723. VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.5));
  4724. } else if (arf_src_index > 0) {
  4725. rc->max_frame_bandwidth =
  4726. VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.4));
  4727. } else {
  4728. rc->max_frame_bandwidth =
  4729. VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.2));
  4730. }
  4731. }
  4732. static void update_level_info(VP9_COMP *cpi, size_t *size, int arf_src_index) {
  4733. VP9_COMMON *const cm = &cpi->common;
  4734. Vp9LevelInfo *const level_info = &cpi->level_info;
  4735. Vp9LevelSpec *const level_spec = &level_info->level_spec;
  4736. Vp9LevelStats *const level_stats = &level_info->level_stats;
  4737. int i, idx;
  4738. uint64_t luma_samples, dur_end;
  4739. const uint32_t luma_pic_size = cm->width * cm->height;
  4740. const uint32_t luma_pic_breadth = VPXMAX(cm->width, cm->height);
  4741. LevelConstraint *const level_constraint = &cpi->level_constraint;
  4742. const int8_t level_index = level_constraint->level_index;
  4743. double cpb_data_size;
  4744. vpx_clear_system_state();
  4745. // update level_stats
  4746. level_stats->total_compressed_size += *size;
  4747. if (cm->show_frame) {
  4748. level_stats->total_uncompressed_size +=
  4749. luma_pic_size +
  4750. 2 * (luma_pic_size >> (cm->subsampling_x + cm->subsampling_y));
  4751. level_stats->time_encoded =
  4752. (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
  4753. (double)TICKS_PER_SEC;
  4754. }
  4755. if (arf_src_index > 0) {
  4756. if (!level_stats->seen_first_altref) {
  4757. level_stats->seen_first_altref = 1;
  4758. } else if (level_stats->frames_since_last_altref <
  4759. level_spec->min_altref_distance) {
  4760. level_spec->min_altref_distance = level_stats->frames_since_last_altref;
  4761. }
  4762. level_stats->frames_since_last_altref = 0;
  4763. } else {
  4764. ++level_stats->frames_since_last_altref;
  4765. }
  4766. if (level_stats->frame_window_buffer.len < FRAME_WINDOW_SIZE - 1) {
  4767. idx = (level_stats->frame_window_buffer.start +
  4768. level_stats->frame_window_buffer.len++) %
  4769. FRAME_WINDOW_SIZE;
  4770. } else {
  4771. idx = level_stats->frame_window_buffer.start;
  4772. level_stats->frame_window_buffer.start = (idx + 1) % FRAME_WINDOW_SIZE;
  4773. }
  4774. level_stats->frame_window_buffer.buf[idx].ts = cpi->last_time_stamp_seen;
  4775. level_stats->frame_window_buffer.buf[idx].size = (uint32_t)(*size);
  4776. level_stats->frame_window_buffer.buf[idx].luma_samples = luma_pic_size;
  4777. if (cm->frame_type == KEY_FRAME) {
  4778. level_stats->ref_refresh_map = 0;
  4779. } else {
  4780. int count = 0;
  4781. level_stats->ref_refresh_map |= vp9_get_refresh_mask(cpi);
  4782. // Also need to consider the case where the encoder refers to a buffer
  4783. // that has been implicitly refreshed after encoding a keyframe.
  4784. if (!cm->intra_only) {
  4785. level_stats->ref_refresh_map |= (1 << cpi->lst_fb_idx);
  4786. level_stats->ref_refresh_map |= (1 << cpi->gld_fb_idx);
  4787. level_stats->ref_refresh_map |= (1 << cpi->alt_fb_idx);
  4788. }
  4789. for (i = 0; i < REF_FRAMES; ++i) {
  4790. count += (level_stats->ref_refresh_map >> i) & 1;
  4791. }
  4792. if (count > level_spec->max_ref_frame_buffers) {
  4793. level_spec->max_ref_frame_buffers = count;
  4794. }
  4795. }
  4796. // update average_bitrate
  4797. level_spec->average_bitrate = (double)level_stats->total_compressed_size /
  4798. 125.0 / level_stats->time_encoded;
  4799. // update max_luma_sample_rate
  4800. luma_samples = 0;
  4801. for (i = 0; i < level_stats->frame_window_buffer.len; ++i) {
  4802. idx = (level_stats->frame_window_buffer.start +
  4803. level_stats->frame_window_buffer.len - 1 - i) %
  4804. FRAME_WINDOW_SIZE;
  4805. if (i == 0) {
  4806. dur_end = level_stats->frame_window_buffer.buf[idx].ts;
  4807. }
  4808. if (dur_end - level_stats->frame_window_buffer.buf[idx].ts >=
  4809. TICKS_PER_SEC) {
  4810. break;
  4811. }
  4812. luma_samples += level_stats->frame_window_buffer.buf[idx].luma_samples;
  4813. }
  4814. if (luma_samples > level_spec->max_luma_sample_rate) {
  4815. level_spec->max_luma_sample_rate = luma_samples;
  4816. }
  4817. // update max_cpb_size
  4818. cpb_data_size = 0;
  4819. for (i = 0; i < CPB_WINDOW_SIZE; ++i) {
  4820. if (i >= level_stats->frame_window_buffer.len) break;
  4821. idx = (level_stats->frame_window_buffer.start +
  4822. level_stats->frame_window_buffer.len - 1 - i) %
  4823. FRAME_WINDOW_SIZE;
  4824. cpb_data_size += level_stats->frame_window_buffer.buf[idx].size;
  4825. }
  4826. cpb_data_size = cpb_data_size / 125.0;
  4827. if (cpb_data_size > level_spec->max_cpb_size) {
  4828. level_spec->max_cpb_size = cpb_data_size;
  4829. }
  4830. // update max_luma_picture_size
  4831. if (luma_pic_size > level_spec->max_luma_picture_size) {
  4832. level_spec->max_luma_picture_size = luma_pic_size;
  4833. }
  4834. // update max_luma_picture_breadth
  4835. if (luma_pic_breadth > level_spec->max_luma_picture_breadth) {
  4836. level_spec->max_luma_picture_breadth = luma_pic_breadth;
  4837. }
  4838. // update compression_ratio
  4839. level_spec->compression_ratio = (double)level_stats->total_uncompressed_size *
  4840. cm->bit_depth /
  4841. level_stats->total_compressed_size / 8.0;
  4842. // update max_col_tiles
  4843. if (level_spec->max_col_tiles < (1 << cm->log2_tile_cols)) {
  4844. level_spec->max_col_tiles = (1 << cm->log2_tile_cols);
  4845. }
  4846. if (level_index >= 0 && level_constraint->fail_flag == 0) {
  4847. if (level_spec->max_luma_picture_size >
  4848. vp9_level_defs[level_index].max_luma_picture_size) {
  4849. level_constraint->fail_flag |= (1 << LUMA_PIC_SIZE_TOO_LARGE);
  4850. vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
  4851. "Failed to encode to the target level %d. %s",
  4852. vp9_level_defs[level_index].level,
  4853. level_fail_messages[LUMA_PIC_SIZE_TOO_LARGE]);
  4854. }
  4855. if (level_spec->max_luma_picture_breadth >
  4856. vp9_level_defs[level_index].max_luma_picture_breadth) {
  4857. level_constraint->fail_flag |= (1 << LUMA_PIC_BREADTH_TOO_LARGE);
  4858. vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
  4859. "Failed to encode to the target level %d. %s",
  4860. vp9_level_defs[level_index].level,
  4861. level_fail_messages[LUMA_PIC_BREADTH_TOO_LARGE]);
  4862. }
  4863. if ((double)level_spec->max_luma_sample_rate >
  4864. (double)vp9_level_defs[level_index].max_luma_sample_rate *
  4865. (1 + SAMPLE_RATE_GRACE_P)) {
  4866. level_constraint->fail_flag |= (1 << LUMA_SAMPLE_RATE_TOO_LARGE);
  4867. vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
  4868. "Failed to encode to the target level %d. %s",
  4869. vp9_level_defs[level_index].level,
  4870. level_fail_messages[LUMA_SAMPLE_RATE_TOO_LARGE]);
  4871. }
  4872. if (level_spec->max_col_tiles > vp9_level_defs[level_index].max_col_tiles) {
  4873. level_constraint->fail_flag |= (1 << TOO_MANY_COLUMN_TILE);
  4874. vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
  4875. "Failed to encode to the target level %d. %s",
  4876. vp9_level_defs[level_index].level,
  4877. level_fail_messages[TOO_MANY_COLUMN_TILE]);
  4878. }
  4879. if (level_spec->min_altref_distance <
  4880. vp9_level_defs[level_index].min_altref_distance) {
  4881. level_constraint->fail_flag |= (1 << ALTREF_DIST_TOO_SMALL);
  4882. vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
  4883. "Failed to encode to the target level %d. %s",
  4884. vp9_level_defs[level_index].level,
  4885. level_fail_messages[ALTREF_DIST_TOO_SMALL]);
  4886. }
  4887. if (level_spec->max_ref_frame_buffers >
  4888. vp9_level_defs[level_index].max_ref_frame_buffers) {
  4889. level_constraint->fail_flag |= (1 << TOO_MANY_REF_BUFFER);
  4890. vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
  4891. "Failed to encode to the target level %d. %s",
  4892. vp9_level_defs[level_index].level,
  4893. level_fail_messages[TOO_MANY_REF_BUFFER]);
  4894. }
  4895. if (level_spec->max_cpb_size > vp9_level_defs[level_index].max_cpb_size) {
  4896. level_constraint->fail_flag |= (1 << CPB_TOO_LARGE);
  4897. vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
  4898. "Failed to encode to the target level %d. %s",
  4899. vp9_level_defs[level_index].level,
  4900. level_fail_messages[CPB_TOO_LARGE]);
  4901. }
  4902. // Set an upper bound for the next frame size. It will be used in
  4903. // level_rc_framerate() before encoding the next frame.
  4904. cpb_data_size = 0;
  4905. for (i = 0; i < CPB_WINDOW_SIZE - 1; ++i) {
  4906. if (i >= level_stats->frame_window_buffer.len) break;
  4907. idx = (level_stats->frame_window_buffer.start +
  4908. level_stats->frame_window_buffer.len - 1 - i) %
  4909. FRAME_WINDOW_SIZE;
  4910. cpb_data_size += level_stats->frame_window_buffer.buf[idx].size;
  4911. }
  4912. cpb_data_size = cpb_data_size / 125.0;
  4913. level_constraint->max_frame_size =
  4914. (int)((vp9_level_defs[level_index].max_cpb_size - cpb_data_size) *
  4915. 1000.0);
  4916. if (level_stats->frame_window_buffer.len < CPB_WINDOW_SIZE - 1)
  4917. level_constraint->max_frame_size >>= 1;
  4918. }
  4919. }
  4920. typedef struct GF_PICTURE {
  4921. YV12_BUFFER_CONFIG *frame;
  4922. int ref_frame[3];
  4923. FRAME_UPDATE_TYPE update_type;
  4924. } GF_PICTURE;
  4925. static void init_gop_frames(VP9_COMP *cpi, GF_PICTURE *gf_picture,
  4926. const GF_GROUP *gf_group, int *tpl_group_frames) {
  4927. VP9_COMMON *cm = &cpi->common;
  4928. int frame_idx = 0;
  4929. int i;
  4930. int gld_index = -1;
  4931. int alt_index = -1;
  4932. int lst_index = -1;
  4933. int arf_index_stack[MAX_ARF_LAYERS];
  4934. int arf_stack_size = 0;
  4935. int extend_frame_count = 0;
  4936. int pframe_qindex = cpi->tpl_stats[2].base_qindex;
  4937. int frame_gop_offset = 0;
  4938. RefCntBuffer *frame_bufs = cm->buffer_pool->frame_bufs;
  4939. int8_t recon_frame_index[REFS_PER_FRAME + MAX_ARF_LAYERS];
  4940. memset(recon_frame_index, -1, sizeof(recon_frame_index));
  4941. stack_init(arf_index_stack, MAX_ARF_LAYERS);
  4942. // TODO(jingning): To be used later for gf frame type parsing.
  4943. (void)gf_group;
  4944. for (i = 0; i < FRAME_BUFFERS; ++i) {
  4945. if (frame_bufs[i].ref_count == 0) {
  4946. alloc_frame_mvs(cm, i);
  4947. if (vpx_realloc_frame_buffer(&frame_bufs[i].buf, cm->width, cm->height,
  4948. cm->subsampling_x, cm->subsampling_y,
  4949. #if CONFIG_VP9_HIGHBITDEPTH
  4950. cm->use_highbitdepth,
  4951. #endif
  4952. VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
  4953. NULL, NULL, NULL))
  4954. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  4955. "Failed to allocate frame buffer");
  4956. recon_frame_index[frame_idx] = i;
  4957. ++frame_idx;
  4958. if (frame_idx >= REFS_PER_FRAME + cpi->oxcf.enable_auto_arf) break;
  4959. }
  4960. }
  4961. for (i = 0; i < REFS_PER_FRAME + 1; ++i) {
  4962. assert(recon_frame_index[i] >= 0);
  4963. cpi->tpl_recon_frames[i] = &frame_bufs[recon_frame_index[i]].buf;
  4964. }
  4965. *tpl_group_frames = 0;
  4966. // Initialize Golden reference frame.
  4967. gf_picture[0].frame = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
  4968. for (i = 0; i < 3; ++i) gf_picture[0].ref_frame[i] = -1;
  4969. gf_picture[0].update_type = gf_group->update_type[0];
  4970. gld_index = 0;
  4971. ++*tpl_group_frames;
  4972. // Initialize base layer ARF frame
  4973. gf_picture[1].frame = cpi->Source;
  4974. gf_picture[1].ref_frame[0] = gld_index;
  4975. gf_picture[1].ref_frame[1] = lst_index;
  4976. gf_picture[1].ref_frame[2] = alt_index;
  4977. gf_picture[1].update_type = gf_group->update_type[1];
  4978. alt_index = 1;
  4979. ++*tpl_group_frames;
  4980. // Initialize P frames
  4981. for (frame_idx = 2; frame_idx < MAX_ARF_GOP_SIZE; ++frame_idx) {
  4982. struct lookahead_entry *buf;
  4983. frame_gop_offset = gf_group->frame_gop_index[frame_idx];
  4984. buf = vp9_lookahead_peek(cpi->lookahead, frame_gop_offset - 1);
  4985. if (buf == NULL) break;
  4986. gf_picture[frame_idx].frame = &buf->img;
  4987. gf_picture[frame_idx].ref_frame[0] = gld_index;
  4988. gf_picture[frame_idx].ref_frame[1] = lst_index;
  4989. gf_picture[frame_idx].ref_frame[2] = alt_index;
  4990. gf_picture[frame_idx].update_type = gf_group->update_type[frame_idx];
  4991. switch (gf_group->update_type[frame_idx]) {
  4992. case ARF_UPDATE:
  4993. stack_push(arf_index_stack, alt_index, arf_stack_size);
  4994. ++arf_stack_size;
  4995. alt_index = frame_idx;
  4996. break;
  4997. case LF_UPDATE: lst_index = frame_idx; break;
  4998. case OVERLAY_UPDATE:
  4999. gld_index = frame_idx;
  5000. alt_index = stack_pop(arf_index_stack, arf_stack_size);
  5001. --arf_stack_size;
  5002. break;
  5003. case USE_BUF_FRAME:
  5004. lst_index = alt_index;
  5005. alt_index = stack_pop(arf_index_stack, arf_stack_size);
  5006. --arf_stack_size;
  5007. break;
  5008. default: break;
  5009. }
  5010. ++*tpl_group_frames;
  5011. // The length of group of pictures is baseline_gf_interval, plus the
  5012. // beginning golden frame from last GOP, plus the last overlay frame in
  5013. // the same GOP.
  5014. if (frame_idx == gf_group->gf_group_size) break;
  5015. }
  5016. alt_index = -1;
  5017. ++frame_idx;
  5018. ++frame_gop_offset;
  5019. // Extend two frames outside the current gf group.
  5020. for (; frame_idx < MAX_LAG_BUFFERS && extend_frame_count < 2; ++frame_idx) {
  5021. struct lookahead_entry *buf =
  5022. vp9_lookahead_peek(cpi->lookahead, frame_gop_offset - 1);
  5023. if (buf == NULL) break;
  5024. cpi->tpl_stats[frame_idx].base_qindex = pframe_qindex;
  5025. gf_picture[frame_idx].frame = &buf->img;
  5026. gf_picture[frame_idx].ref_frame[0] = gld_index;
  5027. gf_picture[frame_idx].ref_frame[1] = lst_index;
  5028. gf_picture[frame_idx].ref_frame[2] = alt_index;
  5029. gf_picture[frame_idx].update_type = LF_UPDATE;
  5030. lst_index = frame_idx;
  5031. ++*tpl_group_frames;
  5032. ++extend_frame_count;
  5033. ++frame_gop_offset;
  5034. }
  5035. }
  5036. static void init_tpl_stats(VP9_COMP *cpi) {
  5037. int frame_idx;
  5038. for (frame_idx = 0; frame_idx < MAX_ARF_GOP_SIZE; ++frame_idx) {
  5039. TplDepFrame *tpl_frame = &cpi->tpl_stats[frame_idx];
  5040. memset(tpl_frame->tpl_stats_ptr, 0,
  5041. tpl_frame->height * tpl_frame->width *
  5042. sizeof(*tpl_frame->tpl_stats_ptr));
  5043. tpl_frame->is_valid = 0;
  5044. }
  5045. }
  5046. #if CONFIG_NON_GREEDY_MV
  5047. static uint32_t motion_compensated_prediction(
  5048. VP9_COMP *cpi, ThreadData *td, int frame_idx, uint8_t *cur_frame_buf,
  5049. uint8_t *ref_frame_buf, int stride, BLOCK_SIZE bsize, int mi_row,
  5050. int mi_col, MV *mv, int rf_idx) {
  5051. #else // CONFIG_NON_GREEDY_MV
  5052. static uint32_t motion_compensated_prediction(VP9_COMP *cpi, ThreadData *td,
  5053. int frame_idx,
  5054. uint8_t *cur_frame_buf,
  5055. uint8_t *ref_frame_buf,
  5056. int stride, BLOCK_SIZE bsize,
  5057. int mi_row, int mi_col, MV *mv) {
  5058. #endif // CONFIG_NON_GREEDY_MV
  5059. MACROBLOCK *const x = &td->mb;
  5060. MACROBLOCKD *const xd = &x->e_mbd;
  5061. MV_SPEED_FEATURES *const mv_sf = &cpi->sf.mv;
  5062. const SEARCH_METHODS search_method = NSTEP;
  5063. int step_param;
  5064. int sadpb = x->sadperbit16;
  5065. uint32_t bestsme = UINT_MAX;
  5066. uint32_t distortion;
  5067. uint32_t sse;
  5068. int cost_list[5];
  5069. const MvLimits tmp_mv_limits = x->mv_limits;
  5070. #if CONFIG_NON_GREEDY_MV
  5071. // lambda is used to adjust the importance of motion vector consitency.
  5072. // TODO(angiebird): Figure out lambda's proper value.
  5073. const int lambda = cpi->tpl_stats[frame_idx].lambda;
  5074. int_mv nb_full_mvs[NB_MVS_NUM];
  5075. #endif
  5076. MV best_ref_mv1 = { 0, 0 };
  5077. MV best_ref_mv1_full; /* full-pixel value of best_ref_mv1 */
  5078. best_ref_mv1_full.col = best_ref_mv1.col >> 3;
  5079. best_ref_mv1_full.row = best_ref_mv1.row >> 3;
  5080. // Setup frame pointers
  5081. x->plane[0].src.buf = cur_frame_buf;
  5082. x->plane[0].src.stride = stride;
  5083. xd->plane[0].pre[0].buf = ref_frame_buf;
  5084. xd->plane[0].pre[0].stride = stride;
  5085. step_param = mv_sf->reduce_first_step_size;
  5086. step_param = VPXMIN(step_param, MAX_MVSEARCH_STEPS - 2);
  5087. vp9_set_mv_search_range(&x->mv_limits, &best_ref_mv1);
  5088. #if CONFIG_NON_GREEDY_MV
  5089. (void)search_method;
  5090. (void)sadpb;
  5091. vp9_prepare_nb_full_mvs(&cpi->tpl_stats[frame_idx], mi_row, mi_col, rf_idx,
  5092. bsize, nb_full_mvs);
  5093. vp9_full_pixel_diamond_new(cpi, x, &best_ref_mv1_full, step_param, lambda, 1,
  5094. &cpi->fn_ptr[bsize], nb_full_mvs, NB_MVS_NUM, mv);
  5095. #else
  5096. (void)frame_idx;
  5097. (void)mi_row;
  5098. (void)mi_col;
  5099. vp9_full_pixel_search(cpi, x, bsize, &best_ref_mv1_full, step_param,
  5100. search_method, sadpb, cond_cost_list(cpi, cost_list),
  5101. &best_ref_mv1, mv, 0, 0);
  5102. #endif
  5103. /* restore UMV window */
  5104. x->mv_limits = tmp_mv_limits;
  5105. // TODO(yunqing): may use higher tap interp filter than 2 taps.
  5106. // Ignore mv costing by sending NULL pointer instead of cost array
  5107. bestsme = cpi->find_fractional_mv_step(
  5108. x, mv, &best_ref_mv1, cpi->common.allow_high_precision_mv, x->errorperbit,
  5109. &cpi->fn_ptr[bsize], 0, mv_sf->subpel_search_level,
  5110. cond_cost_list(cpi, cost_list), NULL, NULL, &distortion, &sse, NULL, 0, 0,
  5111. USE_2_TAPS);
  5112. return bestsme;
  5113. }
  5114. static int get_overlap_area(int grid_pos_row, int grid_pos_col, int ref_pos_row,
  5115. int ref_pos_col, int block, BLOCK_SIZE bsize) {
  5116. int width = 0, height = 0;
  5117. int bw = 4 << b_width_log2_lookup[bsize];
  5118. int bh = 4 << b_height_log2_lookup[bsize];
  5119. switch (block) {
  5120. case 0:
  5121. width = grid_pos_col + bw - ref_pos_col;
  5122. height = grid_pos_row + bh - ref_pos_row;
  5123. break;
  5124. case 1:
  5125. width = ref_pos_col + bw - grid_pos_col;
  5126. height = grid_pos_row + bh - ref_pos_row;
  5127. break;
  5128. case 2:
  5129. width = grid_pos_col + bw - ref_pos_col;
  5130. height = ref_pos_row + bh - grid_pos_row;
  5131. break;
  5132. case 3:
  5133. width = ref_pos_col + bw - grid_pos_col;
  5134. height = ref_pos_row + bh - grid_pos_row;
  5135. break;
  5136. default: assert(0);
  5137. }
  5138. return width * height;
  5139. }
  5140. static int round_floor(int ref_pos, int bsize_pix) {
  5141. int round;
  5142. if (ref_pos < 0)
  5143. round = -(1 + (-ref_pos - 1) / bsize_pix);
  5144. else
  5145. round = ref_pos / bsize_pix;
  5146. return round;
  5147. }
  5148. static void tpl_model_store(TplDepStats *tpl_stats, int mi_row, int mi_col,
  5149. BLOCK_SIZE bsize, int stride) {
  5150. const int mi_height = num_8x8_blocks_high_lookup[bsize];
  5151. const int mi_width = num_8x8_blocks_wide_lookup[bsize];
  5152. const TplDepStats *src_stats = &tpl_stats[mi_row * stride + mi_col];
  5153. int idx, idy;
  5154. for (idy = 0; idy < mi_height; ++idy) {
  5155. for (idx = 0; idx < mi_width; ++idx) {
  5156. TplDepStats *tpl_ptr = &tpl_stats[(mi_row + idy) * stride + mi_col + idx];
  5157. const int64_t mc_flow = tpl_ptr->mc_flow;
  5158. const int64_t mc_ref_cost = tpl_ptr->mc_ref_cost;
  5159. *tpl_ptr = *src_stats;
  5160. tpl_ptr->mc_flow = mc_flow;
  5161. tpl_ptr->mc_ref_cost = mc_ref_cost;
  5162. tpl_ptr->mc_dep_cost = tpl_ptr->intra_cost + tpl_ptr->mc_flow;
  5163. }
  5164. }
  5165. }
  5166. static void tpl_model_update_b(TplDepFrame *tpl_frame, TplDepStats *tpl_stats,
  5167. int mi_row, int mi_col, const BLOCK_SIZE bsize) {
  5168. TplDepFrame *ref_tpl_frame = &tpl_frame[tpl_stats->ref_frame_index];
  5169. TplDepStats *ref_stats = ref_tpl_frame->tpl_stats_ptr;
  5170. MV mv = tpl_stats->mv.as_mv;
  5171. int mv_row = mv.row >> 3;
  5172. int mv_col = mv.col >> 3;
  5173. int ref_pos_row = mi_row * MI_SIZE + mv_row;
  5174. int ref_pos_col = mi_col * MI_SIZE + mv_col;
  5175. const int bw = 4 << b_width_log2_lookup[bsize];
  5176. const int bh = 4 << b_height_log2_lookup[bsize];
  5177. const int mi_height = num_8x8_blocks_high_lookup[bsize];
  5178. const int mi_width = num_8x8_blocks_wide_lookup[bsize];
  5179. const int pix_num = bw * bh;
  5180. // top-left on grid block location in pixel
  5181. int grid_pos_row_base = round_floor(ref_pos_row, bh) * bh;
  5182. int grid_pos_col_base = round_floor(ref_pos_col, bw) * bw;
  5183. int block;
  5184. for (block = 0; block < 4; ++block) {
  5185. int grid_pos_row = grid_pos_row_base + bh * (block >> 1);
  5186. int grid_pos_col = grid_pos_col_base + bw * (block & 0x01);
  5187. if (grid_pos_row >= 0 && grid_pos_row < ref_tpl_frame->mi_rows * MI_SIZE &&
  5188. grid_pos_col >= 0 && grid_pos_col < ref_tpl_frame->mi_cols * MI_SIZE) {
  5189. int overlap_area = get_overlap_area(
  5190. grid_pos_row, grid_pos_col, ref_pos_row, ref_pos_col, block, bsize);
  5191. int ref_mi_row = round_floor(grid_pos_row, bh) * mi_height;
  5192. int ref_mi_col = round_floor(grid_pos_col, bw) * mi_width;
  5193. int64_t mc_flow = tpl_stats->mc_dep_cost -
  5194. (tpl_stats->mc_dep_cost * tpl_stats->inter_cost) /
  5195. tpl_stats->intra_cost;
  5196. int idx, idy;
  5197. for (idy = 0; idy < mi_height; ++idy) {
  5198. for (idx = 0; idx < mi_width; ++idx) {
  5199. TplDepStats *des_stats =
  5200. &ref_stats[(ref_mi_row + idy) * ref_tpl_frame->stride +
  5201. (ref_mi_col + idx)];
  5202. des_stats->mc_flow += (mc_flow * overlap_area) / pix_num;
  5203. des_stats->mc_ref_cost +=
  5204. ((tpl_stats->intra_cost - tpl_stats->inter_cost) * overlap_area) /
  5205. pix_num;
  5206. assert(overlap_area >= 0);
  5207. }
  5208. }
  5209. }
  5210. }
  5211. }
  5212. static void tpl_model_update(TplDepFrame *tpl_frame, TplDepStats *tpl_stats,
  5213. int mi_row, int mi_col, const BLOCK_SIZE bsize) {
  5214. int idx, idy;
  5215. const int mi_height = num_8x8_blocks_high_lookup[bsize];
  5216. const int mi_width = num_8x8_blocks_wide_lookup[bsize];
  5217. for (idy = 0; idy < mi_height; ++idy) {
  5218. for (idx = 0; idx < mi_width; ++idx) {
  5219. TplDepStats *tpl_ptr =
  5220. &tpl_stats[(mi_row + idy) * tpl_frame->stride + (mi_col + idx)];
  5221. tpl_model_update_b(tpl_frame, tpl_ptr, mi_row + idy, mi_col + idx,
  5222. BLOCK_8X8);
  5223. }
  5224. }
  5225. }
  5226. static void get_quantize_error(MACROBLOCK *x, int plane, tran_low_t *coeff,
  5227. tran_low_t *qcoeff, tran_low_t *dqcoeff,
  5228. TX_SIZE tx_size, int64_t *recon_error,
  5229. int64_t *sse) {
  5230. MACROBLOCKD *const xd = &x->e_mbd;
  5231. const struct macroblock_plane *const p = &x->plane[plane];
  5232. const struct macroblockd_plane *const pd = &xd->plane[plane];
  5233. const scan_order *const scan_order = &vp9_default_scan_orders[tx_size];
  5234. uint16_t eob;
  5235. int pix_num = 1 << num_pels_log2_lookup[txsize_to_bsize[tx_size]];
  5236. const int shift = tx_size == TX_32X32 ? 0 : 2;
  5237. #if CONFIG_VP9_HIGHBITDEPTH
  5238. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  5239. vp9_highbd_quantize_fp_32x32(coeff, pix_num, x->skip_block, p->round_fp,
  5240. p->quant_fp, qcoeff, dqcoeff, pd->dequant,
  5241. &eob, scan_order->scan, scan_order->iscan);
  5242. } else {
  5243. vp9_quantize_fp_32x32(coeff, pix_num, x->skip_block, p->round_fp,
  5244. p->quant_fp, qcoeff, dqcoeff, pd->dequant, &eob,
  5245. scan_order->scan, scan_order->iscan);
  5246. }
  5247. #else
  5248. vp9_quantize_fp_32x32(coeff, pix_num, x->skip_block, p->round_fp, p->quant_fp,
  5249. qcoeff, dqcoeff, pd->dequant, &eob, scan_order->scan,
  5250. scan_order->iscan);
  5251. #endif // CONFIG_VP9_HIGHBITDEPTH
  5252. *recon_error = vp9_block_error(coeff, dqcoeff, pix_num, sse) >> shift;
  5253. *recon_error = VPXMAX(*recon_error, 1);
  5254. *sse = (*sse) >> shift;
  5255. *sse = VPXMAX(*sse, 1);
  5256. }
  5257. #if CONFIG_VP9_HIGHBITDEPTH
  5258. void highbd_wht_fwd_txfm(int16_t *src_diff, int bw, tran_low_t *coeff,
  5259. TX_SIZE tx_size) {
  5260. // TODO(sdeng): Implement SIMD based high bit-depth Hadamard transforms.
  5261. switch (tx_size) {
  5262. case TX_8X8: vpx_highbd_hadamard_8x8(src_diff, bw, coeff); break;
  5263. case TX_16X16: vpx_highbd_hadamard_16x16(src_diff, bw, coeff); break;
  5264. case TX_32X32: vpx_highbd_hadamard_32x32(src_diff, bw, coeff); break;
  5265. default: assert(0);
  5266. }
  5267. }
  5268. #endif // CONFIG_VP9_HIGHBITDEPTH
  5269. void wht_fwd_txfm(int16_t *src_diff, int bw, tran_low_t *coeff,
  5270. TX_SIZE tx_size) {
  5271. switch (tx_size) {
  5272. case TX_8X8: vpx_hadamard_8x8(src_diff, bw, coeff); break;
  5273. case TX_16X16: vpx_hadamard_16x16(src_diff, bw, coeff); break;
  5274. case TX_32X32: vpx_hadamard_32x32(src_diff, bw, coeff); break;
  5275. default: assert(0);
  5276. }
  5277. }
  5278. static void set_mv_limits(const VP9_COMMON *cm, MACROBLOCK *x, int mi_row,
  5279. int mi_col) {
  5280. x->mv_limits.row_min = -((mi_row * MI_SIZE) + (17 - 2 * VP9_INTERP_EXTEND));
  5281. x->mv_limits.row_max =
  5282. (cm->mi_rows - 1 - mi_row) * MI_SIZE + (17 - 2 * VP9_INTERP_EXTEND);
  5283. x->mv_limits.col_min = -((mi_col * MI_SIZE) + (17 - 2 * VP9_INTERP_EXTEND));
  5284. x->mv_limits.col_max =
  5285. ((cm->mi_cols - 1 - mi_col) * MI_SIZE) + (17 - 2 * VP9_INTERP_EXTEND);
  5286. }
  5287. static void mode_estimation(VP9_COMP *cpi, MACROBLOCK *x, MACROBLOCKD *xd,
  5288. struct scale_factors *sf, GF_PICTURE *gf_picture,
  5289. int frame_idx, TplDepFrame *tpl_frame,
  5290. int16_t *src_diff, tran_low_t *coeff,
  5291. tran_low_t *qcoeff, tran_low_t *dqcoeff, int mi_row,
  5292. int mi_col, BLOCK_SIZE bsize, TX_SIZE tx_size,
  5293. YV12_BUFFER_CONFIG *ref_frame[], uint8_t *predictor,
  5294. int64_t *recon_error, int64_t *sse) {
  5295. VP9_COMMON *cm = &cpi->common;
  5296. ThreadData *td = &cpi->td;
  5297. const int bw = 4 << b_width_log2_lookup[bsize];
  5298. const int bh = 4 << b_height_log2_lookup[bsize];
  5299. const int pix_num = bw * bh;
  5300. int best_rf_idx = -1;
  5301. int_mv best_mv;
  5302. int64_t best_inter_cost = INT64_MAX;
  5303. int64_t inter_cost;
  5304. int rf_idx;
  5305. const InterpKernel *const kernel = vp9_filter_kernels[EIGHTTAP];
  5306. int64_t best_intra_cost = INT64_MAX;
  5307. int64_t intra_cost;
  5308. PREDICTION_MODE mode;
  5309. int mb_y_offset = mi_row * MI_SIZE * xd->cur_buf->y_stride + mi_col * MI_SIZE;
  5310. MODE_INFO mi_above, mi_left;
  5311. const int mi_height = num_8x8_blocks_high_lookup[bsize];
  5312. const int mi_width = num_8x8_blocks_wide_lookup[bsize];
  5313. TplDepStats *tpl_stats =
  5314. &tpl_frame->tpl_stats_ptr[mi_row * tpl_frame->stride + mi_col];
  5315. xd->mb_to_top_edge = -((mi_row * MI_SIZE) * 8);
  5316. xd->mb_to_bottom_edge = ((cm->mi_rows - 1 - mi_row) * MI_SIZE) * 8;
  5317. xd->mb_to_left_edge = -((mi_col * MI_SIZE) * 8);
  5318. xd->mb_to_right_edge = ((cm->mi_cols - 1 - mi_col) * MI_SIZE) * 8;
  5319. xd->above_mi = (mi_row > 0) ? &mi_above : NULL;
  5320. xd->left_mi = (mi_col > 0) ? &mi_left : NULL;
  5321. // Intra prediction search
  5322. for (mode = DC_PRED; mode <= TM_PRED; ++mode) {
  5323. uint8_t *src, *dst;
  5324. int src_stride, dst_stride;
  5325. src = xd->cur_buf->y_buffer + mb_y_offset;
  5326. src_stride = xd->cur_buf->y_stride;
  5327. dst = &predictor[0];
  5328. dst_stride = bw;
  5329. xd->mi[0]->sb_type = bsize;
  5330. xd->mi[0]->ref_frame[0] = INTRA_FRAME;
  5331. vp9_predict_intra_block(xd, b_width_log2_lookup[bsize], tx_size, mode, src,
  5332. src_stride, dst, dst_stride, 0, 0, 0);
  5333. #if CONFIG_VP9_HIGHBITDEPTH
  5334. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  5335. vpx_highbd_subtract_block(bh, bw, src_diff, bw, src, src_stride, dst,
  5336. dst_stride, xd->bd);
  5337. highbd_wht_fwd_txfm(src_diff, bw, coeff, tx_size);
  5338. intra_cost = vpx_highbd_satd(coeff, pix_num);
  5339. } else {
  5340. vpx_subtract_block(bh, bw, src_diff, bw, src, src_stride, dst,
  5341. dst_stride);
  5342. wht_fwd_txfm(src_diff, bw, coeff, tx_size);
  5343. intra_cost = vpx_satd(coeff, pix_num);
  5344. }
  5345. #else
  5346. vpx_subtract_block(bh, bw, src_diff, bw, src, src_stride, dst, dst_stride);
  5347. wht_fwd_txfm(src_diff, bw, coeff, tx_size);
  5348. intra_cost = vpx_satd(coeff, pix_num);
  5349. #endif // CONFIG_VP9_HIGHBITDEPTH
  5350. if (intra_cost < best_intra_cost) best_intra_cost = intra_cost;
  5351. }
  5352. // Motion compensated prediction
  5353. best_mv.as_int = 0;
  5354. set_mv_limits(cm, x, mi_row, mi_col);
  5355. for (rf_idx = 0; rf_idx < 3; ++rf_idx) {
  5356. int_mv mv;
  5357. if (ref_frame[rf_idx] == NULL) continue;
  5358. #if CONFIG_NON_GREEDY_MV
  5359. (void)td;
  5360. mv.as_int =
  5361. get_pyramid_mv(tpl_frame, rf_idx, bsize, mi_row, mi_col)->as_int;
  5362. #else
  5363. motion_compensated_prediction(
  5364. cpi, td, frame_idx, xd->cur_buf->y_buffer + mb_y_offset,
  5365. ref_frame[rf_idx]->y_buffer + mb_y_offset, xd->cur_buf->y_stride, bsize,
  5366. mi_row, mi_col, &mv.as_mv);
  5367. #endif
  5368. #if CONFIG_VP9_HIGHBITDEPTH
  5369. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  5370. vp9_highbd_build_inter_predictor(
  5371. CONVERT_TO_SHORTPTR(ref_frame[rf_idx]->y_buffer + mb_y_offset),
  5372. ref_frame[rf_idx]->y_stride, CONVERT_TO_SHORTPTR(&predictor[0]), bw,
  5373. &mv.as_mv, sf, bw, bh, 0, kernel, MV_PRECISION_Q3, mi_col * MI_SIZE,
  5374. mi_row * MI_SIZE, xd->bd);
  5375. vpx_highbd_subtract_block(
  5376. bh, bw, src_diff, bw, xd->cur_buf->y_buffer + mb_y_offset,
  5377. xd->cur_buf->y_stride, &predictor[0], bw, xd->bd);
  5378. highbd_wht_fwd_txfm(src_diff, bw, coeff, tx_size);
  5379. inter_cost = vpx_highbd_satd(coeff, pix_num);
  5380. } else {
  5381. vp9_build_inter_predictor(
  5382. ref_frame[rf_idx]->y_buffer + mb_y_offset,
  5383. ref_frame[rf_idx]->y_stride, &predictor[0], bw, &mv.as_mv, sf, bw, bh,
  5384. 0, kernel, MV_PRECISION_Q3, mi_col * MI_SIZE, mi_row * MI_SIZE);
  5385. vpx_subtract_block(bh, bw, src_diff, bw,
  5386. xd->cur_buf->y_buffer + mb_y_offset,
  5387. xd->cur_buf->y_stride, &predictor[0], bw);
  5388. wht_fwd_txfm(src_diff, bw, coeff, tx_size);
  5389. inter_cost = vpx_satd(coeff, pix_num);
  5390. }
  5391. #else
  5392. vp9_build_inter_predictor(ref_frame[rf_idx]->y_buffer + mb_y_offset,
  5393. ref_frame[rf_idx]->y_stride, &predictor[0], bw,
  5394. &mv.as_mv, sf, bw, bh, 0, kernel, MV_PRECISION_Q3,
  5395. mi_col * MI_SIZE, mi_row * MI_SIZE);
  5396. vpx_subtract_block(bh, bw, src_diff, bw,
  5397. xd->cur_buf->y_buffer + mb_y_offset,
  5398. xd->cur_buf->y_stride, &predictor[0], bw);
  5399. wht_fwd_txfm(src_diff, bw, coeff, tx_size);
  5400. inter_cost = vpx_satd(coeff, pix_num);
  5401. #endif
  5402. if (inter_cost < best_inter_cost) {
  5403. best_rf_idx = rf_idx;
  5404. best_inter_cost = inter_cost;
  5405. best_mv.as_int = mv.as_int;
  5406. get_quantize_error(x, 0, coeff, qcoeff, dqcoeff, tx_size, recon_error,
  5407. sse);
  5408. }
  5409. }
  5410. best_intra_cost = VPXMAX(best_intra_cost, 1);
  5411. best_inter_cost = VPXMIN(best_intra_cost, best_inter_cost);
  5412. tpl_stats->inter_cost = VPXMAX(
  5413. 1, (best_inter_cost << TPL_DEP_COST_SCALE_LOG2) / (mi_height * mi_width));
  5414. tpl_stats->intra_cost = VPXMAX(
  5415. 1, (best_intra_cost << TPL_DEP_COST_SCALE_LOG2) / (mi_height * mi_width));
  5416. tpl_stats->ref_frame_index = gf_picture[frame_idx].ref_frame[best_rf_idx];
  5417. tpl_stats->mv.as_int = best_mv.as_int;
  5418. }
  5419. #if CONFIG_NON_GREEDY_MV
  5420. static int get_block_src_pred_buf(MACROBLOCKD *xd, GF_PICTURE *gf_picture,
  5421. int frame_idx, int rf_idx, int mi_row,
  5422. int mi_col, struct buf_2d *src,
  5423. struct buf_2d *pre) {
  5424. const int mb_y_offset =
  5425. mi_row * MI_SIZE * xd->cur_buf->y_stride + mi_col * MI_SIZE;
  5426. YV12_BUFFER_CONFIG *ref_frame = NULL;
  5427. int ref_frame_idx = gf_picture[frame_idx].ref_frame[rf_idx];
  5428. if (ref_frame_idx != -1) {
  5429. ref_frame = gf_picture[ref_frame_idx].frame;
  5430. src->buf = xd->cur_buf->y_buffer + mb_y_offset;
  5431. src->stride = xd->cur_buf->y_stride;
  5432. pre->buf = ref_frame->y_buffer + mb_y_offset;
  5433. pre->stride = ref_frame->y_stride;
  5434. assert(src->stride == pre->stride);
  5435. return 1;
  5436. } else {
  5437. printf("invalid ref_frame_idx");
  5438. assert(ref_frame_idx != -1);
  5439. return 0;
  5440. }
  5441. }
  5442. #define kMvPreCheckLines 5
  5443. #define kMvPreCheckSize 15
  5444. #define MV_REF_POS_NUM 3
  5445. POSITION mv_ref_pos[MV_REF_POS_NUM] = {
  5446. { -1, 0 },
  5447. { 0, -1 },
  5448. { -1, -1 },
  5449. };
  5450. static int_mv *get_select_mv(VP9_COMP *cpi, TplDepFrame *tpl_frame, int mi_row,
  5451. int mi_col) {
  5452. return &cpi->select_mv_arr[mi_row * tpl_frame->stride + mi_col];
  5453. }
  5454. static int_mv find_ref_mv(int mv_mode, VP9_COMP *cpi, TplDepFrame *tpl_frame,
  5455. BLOCK_SIZE bsize, int mi_row, int mi_col) {
  5456. int i;
  5457. const int mi_height = num_8x8_blocks_high_lookup[bsize];
  5458. const int mi_width = num_8x8_blocks_wide_lookup[bsize];
  5459. int_mv nearest_mv, near_mv, invalid_mv;
  5460. nearest_mv.as_int = INVALID_MV;
  5461. near_mv.as_int = INVALID_MV;
  5462. invalid_mv.as_int = INVALID_MV;
  5463. for (i = 0; i < MV_REF_POS_NUM; ++i) {
  5464. int nb_row = mi_row + mv_ref_pos[i].row * mi_height;
  5465. int nb_col = mi_col + mv_ref_pos[i].col * mi_width;
  5466. assert(mv_ref_pos[i].row <= 0);
  5467. assert(mv_ref_pos[i].col <= 0);
  5468. if (nb_row >= 0 && nb_col >= 0) {
  5469. if (nearest_mv.as_int == INVALID_MV) {
  5470. nearest_mv = *get_select_mv(cpi, tpl_frame, nb_row, nb_col);
  5471. } else {
  5472. int_mv mv = *get_select_mv(cpi, tpl_frame, nb_row, nb_col);
  5473. if (mv.as_int == nearest_mv.as_int) {
  5474. continue;
  5475. } else {
  5476. near_mv = mv;
  5477. break;
  5478. }
  5479. }
  5480. }
  5481. }
  5482. if (nearest_mv.as_int == INVALID_MV) {
  5483. nearest_mv.as_mv.row = 0;
  5484. nearest_mv.as_mv.col = 0;
  5485. }
  5486. if (near_mv.as_int == INVALID_MV) {
  5487. near_mv.as_mv.row = 0;
  5488. near_mv.as_mv.col = 0;
  5489. }
  5490. if (mv_mode == NEAREST_MV_MODE) {
  5491. return nearest_mv;
  5492. }
  5493. if (mv_mode == NEAR_MV_MODE) {
  5494. return near_mv;
  5495. }
  5496. assert(0);
  5497. return invalid_mv;
  5498. }
  5499. static int_mv get_mv_from_mv_mode(int mv_mode, VP9_COMP *cpi,
  5500. TplDepFrame *tpl_frame, int rf_idx,
  5501. BLOCK_SIZE bsize, int mi_row, int mi_col) {
  5502. int_mv mv;
  5503. switch (mv_mode) {
  5504. case ZERO_MV_MODE:
  5505. mv.as_mv.row = 0;
  5506. mv.as_mv.col = 0;
  5507. break;
  5508. case NEW_MV_MODE:
  5509. mv = *get_pyramid_mv(tpl_frame, rf_idx, bsize, mi_row, mi_col);
  5510. break;
  5511. case NEAREST_MV_MODE:
  5512. mv = find_ref_mv(mv_mode, cpi, tpl_frame, bsize, mi_row, mi_col);
  5513. break;
  5514. case NEAR_MV_MODE:
  5515. mv = find_ref_mv(mv_mode, cpi, tpl_frame, bsize, mi_row, mi_col);
  5516. break;
  5517. default:
  5518. mv.as_int = INVALID_MV;
  5519. assert(0);
  5520. break;
  5521. }
  5522. return mv;
  5523. }
  5524. static double get_mv_dist(int mv_mode, VP9_COMP *cpi, MACROBLOCKD *xd,
  5525. GF_PICTURE *gf_picture, int frame_idx,
  5526. TplDepFrame *tpl_frame, int rf_idx, BLOCK_SIZE bsize,
  5527. int mi_row, int mi_col, int_mv *mv) {
  5528. uint32_t sse;
  5529. struct buf_2d src;
  5530. struct buf_2d pre;
  5531. MV full_mv;
  5532. *mv = get_mv_from_mv_mode(mv_mode, cpi, tpl_frame, rf_idx, bsize, mi_row,
  5533. mi_col);
  5534. full_mv = get_full_mv(&mv->as_mv);
  5535. if (get_block_src_pred_buf(xd, gf_picture, frame_idx, rf_idx, mi_row, mi_col,
  5536. &src, &pre)) {
  5537. // TODO(angiebird): Consider subpixel when computing the sse.
  5538. cpi->fn_ptr[bsize].vf(src.buf, src.stride, get_buf_from_mv(&pre, &full_mv),
  5539. pre.stride, &sse);
  5540. return (double)(sse << VP9_DIST_SCALE_LOG2);
  5541. } else {
  5542. assert(0);
  5543. return 0;
  5544. }
  5545. }
  5546. static int get_mv_mode_cost(int mv_mode) {
  5547. // TODO(angiebird): The probabilities are roughly inferred from
  5548. // default_inter_mode_probs. Check if there is a better way to set the
  5549. // probabilities.
  5550. const int zero_mv_prob = 16;
  5551. const int new_mv_prob = 24 * 1;
  5552. const int ref_mv_prob = 256 - zero_mv_prob - new_mv_prob;
  5553. assert(zero_mv_prob + new_mv_prob + ref_mv_prob == 256);
  5554. switch (mv_mode) {
  5555. case ZERO_MV_MODE: return vp9_prob_cost[zero_mv_prob]; break;
  5556. case NEW_MV_MODE: return vp9_prob_cost[new_mv_prob]; break;
  5557. case NEAREST_MV_MODE: return vp9_prob_cost[ref_mv_prob]; break;
  5558. case NEAR_MV_MODE: return vp9_prob_cost[ref_mv_prob]; break;
  5559. default: assert(0); return -1;
  5560. }
  5561. }
  5562. static INLINE double get_mv_diff_cost(MV *new_mv, MV *ref_mv) {
  5563. double mv_diff_cost = log2(1 + abs(new_mv->row - ref_mv->row)) +
  5564. log2(1 + abs(new_mv->col - ref_mv->col));
  5565. mv_diff_cost *= (1 << VP9_PROB_COST_SHIFT);
  5566. return mv_diff_cost;
  5567. }
  5568. static double get_mv_cost(int mv_mode, VP9_COMP *cpi, TplDepFrame *tpl_frame,
  5569. int rf_idx, BLOCK_SIZE bsize, int mi_row,
  5570. int mi_col) {
  5571. double mv_cost = get_mv_mode_cost(mv_mode);
  5572. if (mv_mode == NEW_MV_MODE) {
  5573. MV new_mv = get_mv_from_mv_mode(mv_mode, cpi, tpl_frame, rf_idx, bsize,
  5574. mi_row, mi_col)
  5575. .as_mv;
  5576. MV nearest_mv = get_mv_from_mv_mode(NEAREST_MV_MODE, cpi, tpl_frame, rf_idx,
  5577. bsize, mi_row, mi_col)
  5578. .as_mv;
  5579. MV near_mv = get_mv_from_mv_mode(NEAR_MV_MODE, cpi, tpl_frame, rf_idx,
  5580. bsize, mi_row, mi_col)
  5581. .as_mv;
  5582. double nearest_cost = get_mv_diff_cost(&new_mv, &nearest_mv);
  5583. double near_cost = get_mv_diff_cost(&new_mv, &near_mv);
  5584. mv_cost += nearest_cost < near_cost ? nearest_cost : near_cost;
  5585. }
  5586. return mv_cost;
  5587. }
  5588. static double eval_mv_mode(int mv_mode, VP9_COMP *cpi, MACROBLOCK *x,
  5589. GF_PICTURE *gf_picture, int frame_idx,
  5590. TplDepFrame *tpl_frame, int rf_idx, BLOCK_SIZE bsize,
  5591. int mi_row, int mi_col, int_mv *mv) {
  5592. MACROBLOCKD *xd = &x->e_mbd;
  5593. double mv_dist = get_mv_dist(mv_mode, cpi, xd, gf_picture, frame_idx,
  5594. tpl_frame, rf_idx, bsize, mi_row, mi_col, mv);
  5595. double mv_cost =
  5596. get_mv_cost(mv_mode, cpi, tpl_frame, rf_idx, bsize, mi_row, mi_col);
  5597. double mult = 180;
  5598. return mv_cost + mult * log2f(1 + mv_dist);
  5599. }
  5600. static int find_best_ref_mv_mode(VP9_COMP *cpi, MACROBLOCK *x,
  5601. GF_PICTURE *gf_picture, int frame_idx,
  5602. TplDepFrame *tpl_frame, int rf_idx,
  5603. BLOCK_SIZE bsize, int mi_row, int mi_col,
  5604. double *rd, int_mv *mv) {
  5605. int best_mv_mode = ZERO_MV_MODE;
  5606. int update = 0;
  5607. int mv_mode;
  5608. *rd = 0;
  5609. for (mv_mode = 0; mv_mode < MAX_MV_MODE; ++mv_mode) {
  5610. double this_rd;
  5611. int_mv this_mv;
  5612. if (mv_mode == NEW_MV_MODE) {
  5613. continue;
  5614. }
  5615. this_rd = eval_mv_mode(mv_mode, cpi, x, gf_picture, frame_idx, tpl_frame,
  5616. rf_idx, bsize, mi_row, mi_col, &this_mv);
  5617. if (update == 0) {
  5618. *rd = this_rd;
  5619. *mv = this_mv;
  5620. best_mv_mode = mv_mode;
  5621. update = 1;
  5622. } else {
  5623. if (this_rd < *rd) {
  5624. *rd = this_rd;
  5625. *mv = this_mv;
  5626. best_mv_mode = mv_mode;
  5627. }
  5628. }
  5629. }
  5630. return best_mv_mode;
  5631. }
  5632. static void predict_mv_mode(VP9_COMP *cpi, MACROBLOCK *x,
  5633. GF_PICTURE *gf_picture, int frame_idx,
  5634. TplDepFrame *tpl_frame, int rf_idx,
  5635. BLOCK_SIZE bsize, int mi_row, int mi_col) {
  5636. const int mi_height = num_8x8_blocks_high_lookup[bsize];
  5637. const int mi_width = num_8x8_blocks_wide_lookup[bsize];
  5638. int tmp_mv_mode_arr[kMvPreCheckSize];
  5639. int *mv_mode_arr = tpl_frame->mv_mode_arr[rf_idx];
  5640. double *rd_diff_arr = tpl_frame->rd_diff_arr[rf_idx];
  5641. int_mv *select_mv_arr = cpi->select_mv_arr;
  5642. int_mv tmp_select_mv_arr[kMvPreCheckSize];
  5643. int stride = tpl_frame->stride;
  5644. double new_mv_rd = 0;
  5645. double no_new_mv_rd = 0;
  5646. double this_new_mv_rd = 0;
  5647. double this_no_new_mv_rd = 0;
  5648. int idx;
  5649. int tmp_idx;
  5650. assert(kMvPreCheckSize == (kMvPreCheckLines * (kMvPreCheckLines + 1)) >> 1);
  5651. // no new mv
  5652. // diagnal scan order
  5653. tmp_idx = 0;
  5654. for (idx = 0; idx < kMvPreCheckLines; ++idx) {
  5655. int r;
  5656. for (r = 0; r <= idx; ++r) {
  5657. int c = idx - r;
  5658. int nb_row = mi_row + r * mi_height;
  5659. int nb_col = mi_col + c * mi_width;
  5660. if (nb_row < tpl_frame->mi_rows && nb_col < tpl_frame->mi_cols) {
  5661. double this_rd;
  5662. int_mv *mv = &select_mv_arr[nb_row * stride + nb_col];
  5663. mv_mode_arr[nb_row * stride + nb_col] =
  5664. find_best_ref_mv_mode(cpi, x, gf_picture, frame_idx, tpl_frame,
  5665. rf_idx, bsize, nb_row, nb_col, &this_rd, mv);
  5666. if (r == 0 && c == 0) {
  5667. this_no_new_mv_rd = this_rd;
  5668. }
  5669. no_new_mv_rd += this_rd;
  5670. tmp_mv_mode_arr[tmp_idx] = mv_mode_arr[nb_row * stride + nb_col];
  5671. tmp_select_mv_arr[tmp_idx] = select_mv_arr[nb_row * stride + nb_col];
  5672. ++tmp_idx;
  5673. }
  5674. }
  5675. }
  5676. // new mv
  5677. mv_mode_arr[mi_row * stride + mi_col] = NEW_MV_MODE;
  5678. this_new_mv_rd = eval_mv_mode(NEW_MV_MODE, cpi, x, gf_picture, frame_idx,
  5679. tpl_frame, rf_idx, bsize, mi_row, mi_col,
  5680. &select_mv_arr[mi_row * stride + mi_col]);
  5681. new_mv_rd = this_new_mv_rd;
  5682. // We start from idx = 1 because idx = 0 is evaluated as NEW_MV_MODE
  5683. // beforehand.
  5684. for (idx = 1; idx < kMvPreCheckLines; ++idx) {
  5685. int r;
  5686. for (r = 0; r <= idx; ++r) {
  5687. int c = idx - r;
  5688. int nb_row = mi_row + r * mi_height;
  5689. int nb_col = mi_col + c * mi_width;
  5690. if (nb_row < tpl_frame->mi_rows && nb_col < tpl_frame->mi_cols) {
  5691. double this_rd;
  5692. int_mv *mv = &select_mv_arr[nb_row * stride + nb_col];
  5693. mv_mode_arr[nb_row * stride + nb_col] =
  5694. find_best_ref_mv_mode(cpi, x, gf_picture, frame_idx, tpl_frame,
  5695. rf_idx, bsize, nb_row, nb_col, &this_rd, mv);
  5696. new_mv_rd += this_rd;
  5697. }
  5698. }
  5699. }
  5700. // update best_mv_mode
  5701. tmp_idx = 0;
  5702. if (no_new_mv_rd < new_mv_rd) {
  5703. for (idx = 0; idx < kMvPreCheckLines; ++idx) {
  5704. int r;
  5705. for (r = 0; r <= idx; ++r) {
  5706. int c = idx - r;
  5707. int nb_row = mi_row + r * mi_height;
  5708. int nb_col = mi_col + c * mi_width;
  5709. if (nb_row < tpl_frame->mi_rows && nb_col < tpl_frame->mi_cols) {
  5710. mv_mode_arr[nb_row * stride + nb_col] = tmp_mv_mode_arr[tmp_idx];
  5711. select_mv_arr[nb_row * stride + nb_col] = tmp_select_mv_arr[tmp_idx];
  5712. ++tmp_idx;
  5713. }
  5714. }
  5715. }
  5716. rd_diff_arr[mi_row * stride + mi_col] = 0;
  5717. } else {
  5718. rd_diff_arr[mi_row * stride + mi_col] =
  5719. (no_new_mv_rd - this_no_new_mv_rd) - (new_mv_rd - this_new_mv_rd);
  5720. }
  5721. }
  5722. static void predict_mv_mode_arr(VP9_COMP *cpi, MACROBLOCK *x,
  5723. GF_PICTURE *gf_picture, int frame_idx,
  5724. TplDepFrame *tpl_frame, int rf_idx,
  5725. BLOCK_SIZE bsize) {
  5726. const int mi_height = num_8x8_blocks_high_lookup[bsize];
  5727. const int mi_width = num_8x8_blocks_wide_lookup[bsize];
  5728. const int unit_rows = tpl_frame->mi_rows / mi_height;
  5729. const int unit_cols = tpl_frame->mi_cols / mi_width;
  5730. const int max_diagonal_lines = unit_rows + unit_cols - 1;
  5731. int idx;
  5732. for (idx = 0; idx < max_diagonal_lines; ++idx) {
  5733. int r;
  5734. for (r = VPXMAX(idx - unit_cols + 1, 0); r <= VPXMIN(idx, unit_rows - 1);
  5735. ++r) {
  5736. int c = idx - r;
  5737. int mi_row = r * mi_height;
  5738. int mi_col = c * mi_width;
  5739. assert(c >= 0 && c < unit_cols);
  5740. assert(mi_row >= 0 && mi_row < tpl_frame->mi_rows);
  5741. assert(mi_col >= 0 && mi_col < tpl_frame->mi_cols);
  5742. predict_mv_mode(cpi, x, gf_picture, frame_idx, tpl_frame, rf_idx, bsize,
  5743. mi_row, mi_col);
  5744. }
  5745. }
  5746. }
  5747. static double get_feature_score(uint8_t *buf, ptrdiff_t stride, int rows,
  5748. int cols) {
  5749. double IxIx = 0;
  5750. double IxIy = 0;
  5751. double IyIy = 0;
  5752. double score;
  5753. int r, c;
  5754. vpx_clear_system_state();
  5755. for (r = 0; r + 1 < rows; ++r) {
  5756. for (c = 0; c + 1 < cols; ++c) {
  5757. int diff_x = buf[r * stride + c] - buf[r * stride + c + 1];
  5758. int diff_y = buf[r * stride + c] - buf[(r + 1) * stride + c];
  5759. IxIx += diff_x * diff_x;
  5760. IxIy += diff_x * diff_y;
  5761. IyIy += diff_y * diff_y;
  5762. }
  5763. }
  5764. IxIx /= (rows - 1) * (cols - 1);
  5765. IxIy /= (rows - 1) * (cols - 1);
  5766. IyIy /= (rows - 1) * (cols - 1);
  5767. score = (IxIx * IyIy - IxIy * IxIy + 0.0001) / (IxIx + IyIy + 0.0001);
  5768. return score;
  5769. }
  5770. static int compare_feature_score(const void *a, const void *b) {
  5771. const FEATURE_SCORE_LOC *aa = *(FEATURE_SCORE_LOC *const *)a;
  5772. const FEATURE_SCORE_LOC *bb = *(FEATURE_SCORE_LOC *const *)b;
  5773. if (aa->feature_score < bb->feature_score) {
  5774. return 1;
  5775. } else if (aa->feature_score > bb->feature_score) {
  5776. return -1;
  5777. } else {
  5778. return 0;
  5779. }
  5780. }
  5781. static void do_motion_search(VP9_COMP *cpi, ThreadData *td, int frame_idx,
  5782. YV12_BUFFER_CONFIG **ref_frame, BLOCK_SIZE bsize,
  5783. int mi_row, int mi_col) {
  5784. VP9_COMMON *cm = &cpi->common;
  5785. MACROBLOCK *x = &td->mb;
  5786. MACROBLOCKD *xd = &x->e_mbd;
  5787. TplDepFrame *tpl_frame = &cpi->tpl_stats[frame_idx];
  5788. TplDepStats *tpl_stats =
  5789. &tpl_frame->tpl_stats_ptr[mi_row * tpl_frame->stride + mi_col];
  5790. const int mb_y_offset =
  5791. mi_row * MI_SIZE * xd->cur_buf->y_stride + mi_col * MI_SIZE;
  5792. int rf_idx;
  5793. set_mv_limits(cm, x, mi_row, mi_col);
  5794. for (rf_idx = 0; rf_idx < 3; ++rf_idx) {
  5795. int_mv *mv = get_pyramid_mv(tpl_frame, rf_idx, bsize, mi_row, mi_col);
  5796. if (ref_frame[rf_idx] == NULL) {
  5797. tpl_stats->ready[rf_idx] = 0;
  5798. continue;
  5799. } else {
  5800. tpl_stats->ready[rf_idx] = 1;
  5801. }
  5802. motion_compensated_prediction(
  5803. cpi, td, frame_idx, xd->cur_buf->y_buffer + mb_y_offset,
  5804. ref_frame[rf_idx]->y_buffer + mb_y_offset, xd->cur_buf->y_stride, bsize,
  5805. mi_row, mi_col, &mv->as_mv, rf_idx);
  5806. }
  5807. }
  5808. #define CHANGE_MV_SEARCH_ORDER 1
  5809. #define USE_PQSORT 1
  5810. #if CHANGE_MV_SEARCH_ORDER
  5811. #if USE_PQSORT
  5812. static void max_heap_pop(FEATURE_SCORE_LOC **heap, int *size,
  5813. FEATURE_SCORE_LOC **output) {
  5814. if (*size > 0) {
  5815. *output = heap[0];
  5816. --*size;
  5817. if (*size > 0) {
  5818. int p, l, r;
  5819. heap[0] = heap[*size];
  5820. p = 0;
  5821. l = 2 * p + 1;
  5822. r = 2 * p + 2;
  5823. while (l < *size) {
  5824. FEATURE_SCORE_LOC *tmp;
  5825. int c = l;
  5826. if (r < *size && heap[r]->feature_score > heap[l]->feature_score) {
  5827. c = r;
  5828. }
  5829. if (heap[p]->feature_score >= heap[c]->feature_score) {
  5830. break;
  5831. }
  5832. tmp = heap[p];
  5833. heap[p] = heap[c];
  5834. heap[c] = tmp;
  5835. p = c;
  5836. l = 2 * p + 1;
  5837. r = 2 * p + 2;
  5838. }
  5839. }
  5840. } else {
  5841. assert(0);
  5842. }
  5843. }
  5844. static void max_heap_push(FEATURE_SCORE_LOC **heap, int *size,
  5845. FEATURE_SCORE_LOC *input) {
  5846. int c, p;
  5847. FEATURE_SCORE_LOC *tmp;
  5848. input->visited = 1;
  5849. heap[*size] = input;
  5850. ++*size;
  5851. c = *size - 1;
  5852. p = c >> 1;
  5853. while (c > 0 && heap[c]->feature_score > heap[p]->feature_score) {
  5854. tmp = heap[p];
  5855. heap[p] = heap[c];
  5856. heap[c] = tmp;
  5857. c = p;
  5858. p >>= 1;
  5859. }
  5860. }
  5861. static void add_nb_blocks_to_heap(VP9_COMP *cpi, const TplDepFrame *tpl_frame,
  5862. BLOCK_SIZE bsize, int mi_row, int mi_col,
  5863. int *heap_size) {
  5864. const int mi_unit = num_8x8_blocks_wide_lookup[bsize];
  5865. const int dirs[NB_MVS_NUM][2] = { { -1, 0 }, { 0, -1 }, { 1, 0 }, { 0, 1 } };
  5866. int i;
  5867. for (i = 0; i < NB_MVS_NUM; ++i) {
  5868. int r = dirs[i][0] * mi_unit;
  5869. int c = dirs[i][1] * mi_unit;
  5870. if (mi_row + r >= 0 && mi_row + r < tpl_frame->mi_rows && mi_col + c >= 0 &&
  5871. mi_col + c < tpl_frame->mi_cols) {
  5872. FEATURE_SCORE_LOC *fs_loc =
  5873. &cpi->feature_score_loc_arr[(mi_row + r) * tpl_frame->stride +
  5874. (mi_col + c)];
  5875. if (fs_loc->visited == 0) {
  5876. max_heap_push(cpi->feature_score_loc_heap, heap_size, fs_loc);
  5877. }
  5878. }
  5879. }
  5880. }
  5881. #endif // USE_PQSORT
  5882. #endif // CHANGE_MV_SEARCH_ORDER
  5883. static void build_motion_field(VP9_COMP *cpi, MACROBLOCKD *xd, int frame_idx,
  5884. YV12_BUFFER_CONFIG *ref_frame[3],
  5885. BLOCK_SIZE bsize) {
  5886. VP9_COMMON *cm = &cpi->common;
  5887. ThreadData *td = &cpi->td;
  5888. TplDepFrame *tpl_frame = &cpi->tpl_stats[frame_idx];
  5889. const int mi_height = num_8x8_blocks_high_lookup[bsize];
  5890. const int mi_width = num_8x8_blocks_wide_lookup[bsize];
  5891. const int pw = num_4x4_blocks_wide_lookup[bsize] << 2;
  5892. const int ph = num_4x4_blocks_high_lookup[bsize] << 2;
  5893. int fs_loc_sort_size;
  5894. int fs_loc_heap_size;
  5895. int mi_row, mi_col;
  5896. tpl_frame->lambda = (pw * ph) >> 2;
  5897. assert(pw * ph == tpl_frame->lambda << 2);
  5898. fs_loc_sort_size = 0;
  5899. for (mi_row = 0; mi_row < cm->mi_rows; mi_row += mi_height) {
  5900. for (mi_col = 0; mi_col < cm->mi_cols; mi_col += mi_width) {
  5901. const int mb_y_offset =
  5902. mi_row * MI_SIZE * xd->cur_buf->y_stride + mi_col * MI_SIZE;
  5903. const int bw = 4 << b_width_log2_lookup[bsize];
  5904. const int bh = 4 << b_height_log2_lookup[bsize];
  5905. TplDepStats *tpl_stats =
  5906. &tpl_frame->tpl_stats_ptr[mi_row * tpl_frame->stride + mi_col];
  5907. FEATURE_SCORE_LOC *fs_loc =
  5908. &cpi->feature_score_loc_arr[mi_row * tpl_frame->stride + mi_col];
  5909. tpl_stats->feature_score = get_feature_score(
  5910. xd->cur_buf->y_buffer + mb_y_offset, xd->cur_buf->y_stride, bw, bh);
  5911. fs_loc->visited = 0;
  5912. fs_loc->feature_score = tpl_stats->feature_score;
  5913. fs_loc->mi_row = mi_row;
  5914. fs_loc->mi_col = mi_col;
  5915. cpi->feature_score_loc_sort[fs_loc_sort_size] = fs_loc;
  5916. ++fs_loc_sort_size;
  5917. }
  5918. }
  5919. qsort(cpi->feature_score_loc_sort, fs_loc_sort_size,
  5920. sizeof(*cpi->feature_score_loc_sort), compare_feature_score);
  5921. for (mi_row = 0; mi_row < cm->mi_rows; mi_row += mi_height) {
  5922. for (mi_col = 0; mi_col < cm->mi_cols; mi_col += mi_width) {
  5923. int rf_idx;
  5924. for (rf_idx = 0; rf_idx < 3; ++rf_idx) {
  5925. TplDepStats *tpl_stats =
  5926. &tpl_frame->tpl_stats_ptr[mi_row * tpl_frame->stride + mi_col];
  5927. tpl_stats->ready[rf_idx] = 0;
  5928. }
  5929. }
  5930. }
  5931. #if CHANGE_MV_SEARCH_ORDER
  5932. #if !USE_PQSORT
  5933. for (i = 0; i < fs_loc_sort_size; ++i) {
  5934. FEATURE_SCORE_LOC *fs_loc = cpi->feature_score_loc_sort[i];
  5935. do_motion_search(cpi, td, frame_idx, ref_frame, bsize, fs_loc->mi_row,
  5936. fs_loc->mi_col);
  5937. }
  5938. #else // !USE_PQSORT
  5939. fs_loc_heap_size = 0;
  5940. max_heap_push(cpi->feature_score_loc_heap, &fs_loc_heap_size,
  5941. cpi->feature_score_loc_sort[0]);
  5942. while (fs_loc_heap_size > 0) {
  5943. FEATURE_SCORE_LOC *fs_loc;
  5944. max_heap_pop(cpi->feature_score_loc_heap, &fs_loc_heap_size, &fs_loc);
  5945. do_motion_search(cpi, td, frame_idx, ref_frame, bsize, fs_loc->mi_row,
  5946. fs_loc->mi_col);
  5947. add_nb_blocks_to_heap(cpi, tpl_frame, bsize, fs_loc->mi_row, fs_loc->mi_col,
  5948. &fs_loc_heap_size);
  5949. }
  5950. #endif // !USE_PQSORT
  5951. #else // CHANGE_MV_SEARCH_ORDER
  5952. for (mi_row = 0; mi_row < cm->mi_rows; mi_row += mi_height) {
  5953. for (mi_col = 0; mi_col < cm->mi_cols; mi_col += mi_width) {
  5954. do_motion_search(cpi, td, frame_idx, ref_frame, bsize, mi_row, mi_col);
  5955. }
  5956. }
  5957. #endif // CHANGE_MV_SEARCH_ORDER
  5958. }
  5959. #endif // CONFIG_NON_GREEDY_MV
  5960. static void mc_flow_dispenser(VP9_COMP *cpi, GF_PICTURE *gf_picture,
  5961. int frame_idx, BLOCK_SIZE bsize) {
  5962. TplDepFrame *tpl_frame = &cpi->tpl_stats[frame_idx];
  5963. YV12_BUFFER_CONFIG *this_frame = gf_picture[frame_idx].frame;
  5964. YV12_BUFFER_CONFIG *ref_frame[3] = { NULL, NULL, NULL };
  5965. VP9_COMMON *cm = &cpi->common;
  5966. struct scale_factors sf;
  5967. int rdmult, idx;
  5968. ThreadData *td = &cpi->td;
  5969. MACROBLOCK *x = &td->mb;
  5970. MACROBLOCKD *xd = &x->e_mbd;
  5971. int mi_row, mi_col;
  5972. #if CONFIG_VP9_HIGHBITDEPTH
  5973. DECLARE_ALIGNED(16, uint16_t, predictor16[32 * 32 * 3]);
  5974. DECLARE_ALIGNED(16, uint8_t, predictor8[32 * 32 * 3]);
  5975. uint8_t *predictor;
  5976. #else
  5977. DECLARE_ALIGNED(16, uint8_t, predictor[32 * 32 * 3]);
  5978. #endif
  5979. DECLARE_ALIGNED(16, int16_t, src_diff[32 * 32]);
  5980. DECLARE_ALIGNED(16, tran_low_t, coeff[32 * 32]);
  5981. DECLARE_ALIGNED(16, tran_low_t, qcoeff[32 * 32]);
  5982. DECLARE_ALIGNED(16, tran_low_t, dqcoeff[32 * 32]);
  5983. const TX_SIZE tx_size = max_txsize_lookup[bsize];
  5984. const int mi_height = num_8x8_blocks_high_lookup[bsize];
  5985. const int mi_width = num_8x8_blocks_wide_lookup[bsize];
  5986. int64_t recon_error, sse;
  5987. #if CONFIG_NON_GREEDY_MV
  5988. int square_block_idx;
  5989. int rf_idx;
  5990. #endif
  5991. // Setup scaling factor
  5992. #if CONFIG_VP9_HIGHBITDEPTH
  5993. vp9_setup_scale_factors_for_frame(
  5994. &sf, this_frame->y_crop_width, this_frame->y_crop_height,
  5995. this_frame->y_crop_width, this_frame->y_crop_height,
  5996. cpi->common.use_highbitdepth);
  5997. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
  5998. predictor = CONVERT_TO_BYTEPTR(predictor16);
  5999. else
  6000. predictor = predictor8;
  6001. #else
  6002. vp9_setup_scale_factors_for_frame(
  6003. &sf, this_frame->y_crop_width, this_frame->y_crop_height,
  6004. this_frame->y_crop_width, this_frame->y_crop_height);
  6005. #endif // CONFIG_VP9_HIGHBITDEPTH
  6006. // Prepare reference frame pointers. If any reference frame slot is
  6007. // unavailable, the pointer will be set to Null.
  6008. for (idx = 0; idx < 3; ++idx) {
  6009. int rf_idx = gf_picture[frame_idx].ref_frame[idx];
  6010. if (rf_idx != -1) ref_frame[idx] = gf_picture[rf_idx].frame;
  6011. }
  6012. xd->mi = cm->mi_grid_visible;
  6013. xd->mi[0] = cm->mi;
  6014. xd->cur_buf = this_frame;
  6015. // Get rd multiplier set up.
  6016. rdmult = vp9_compute_rd_mult_based_on_qindex(cpi, tpl_frame->base_qindex);
  6017. set_error_per_bit(&cpi->td.mb, rdmult);
  6018. vp9_initialize_me_consts(cpi, &cpi->td.mb, tpl_frame->base_qindex);
  6019. tpl_frame->is_valid = 1;
  6020. cm->base_qindex = tpl_frame->base_qindex;
  6021. vp9_frame_init_quantizer(cpi);
  6022. #if CONFIG_NON_GREEDY_MV
  6023. for (square_block_idx = 0; square_block_idx < SQUARE_BLOCK_SIZES;
  6024. ++square_block_idx) {
  6025. BLOCK_SIZE square_bsize = square_block_idx_to_bsize(square_block_idx);
  6026. build_motion_field(cpi, xd, frame_idx, ref_frame, square_bsize);
  6027. }
  6028. for (rf_idx = 0; rf_idx < 3; ++rf_idx) {
  6029. int ref_frame_idx = gf_picture[frame_idx].ref_frame[rf_idx];
  6030. if (ref_frame_idx != -1) {
  6031. predict_mv_mode_arr(cpi, x, gf_picture, frame_idx, tpl_frame, rf_idx,
  6032. bsize);
  6033. }
  6034. }
  6035. #endif
  6036. for (mi_row = 0; mi_row < cm->mi_rows; mi_row += mi_height) {
  6037. for (mi_col = 0; mi_col < cm->mi_cols; mi_col += mi_width) {
  6038. mode_estimation(cpi, x, xd, &sf, gf_picture, frame_idx, tpl_frame,
  6039. src_diff, coeff, qcoeff, dqcoeff, mi_row, mi_col, bsize,
  6040. tx_size, ref_frame, predictor, &recon_error, &sse);
  6041. // Motion flow dependency dispenser.
  6042. tpl_model_store(tpl_frame->tpl_stats_ptr, mi_row, mi_col, bsize,
  6043. tpl_frame->stride);
  6044. tpl_model_update(cpi->tpl_stats, tpl_frame->tpl_stats_ptr, mi_row, mi_col,
  6045. bsize);
  6046. }
  6047. }
  6048. }
  6049. #if CONFIG_NON_GREEDY_MV
  6050. #define DUMP_TPL_STATS 0
  6051. #if DUMP_TPL_STATS
  6052. static void dump_buf(uint8_t *buf, int stride, int row, int col, int h, int w) {
  6053. int i, j;
  6054. printf("%d %d\n", h, w);
  6055. for (i = 0; i < h; ++i) {
  6056. for (j = 0; j < w; ++j) {
  6057. printf("%d ", buf[(row + i) * stride + col + j]);
  6058. }
  6059. }
  6060. printf("\n");
  6061. }
  6062. static void dump_frame_buf(const YV12_BUFFER_CONFIG *frame_buf) {
  6063. dump_buf(frame_buf->y_buffer, frame_buf->y_stride, 0, 0, frame_buf->y_height,
  6064. frame_buf->y_width);
  6065. dump_buf(frame_buf->u_buffer, frame_buf->uv_stride, 0, 0,
  6066. frame_buf->uv_height, frame_buf->uv_width);
  6067. dump_buf(frame_buf->v_buffer, frame_buf->uv_stride, 0, 0,
  6068. frame_buf->uv_height, frame_buf->uv_width);
  6069. }
  6070. static void dump_tpl_stats(const VP9_COMP *cpi, int tpl_group_frames,
  6071. const GF_GROUP *gf_group,
  6072. const GF_PICTURE *gf_picture, BLOCK_SIZE bsize) {
  6073. int frame_idx;
  6074. const VP9_COMMON *cm = &cpi->common;
  6075. int rf_idx;
  6076. for (frame_idx = 1; frame_idx < tpl_group_frames; ++frame_idx) {
  6077. for (rf_idx = 0; rf_idx < 3; ++rf_idx) {
  6078. const TplDepFrame *tpl_frame = &cpi->tpl_stats[frame_idx];
  6079. int mi_row, mi_col;
  6080. int ref_frame_idx;
  6081. const int mi_height = num_8x8_blocks_high_lookup[bsize];
  6082. const int mi_width = num_8x8_blocks_wide_lookup[bsize];
  6083. ref_frame_idx = gf_picture[frame_idx].ref_frame[rf_idx];
  6084. if (ref_frame_idx != -1) {
  6085. YV12_BUFFER_CONFIG *ref_frame_buf = gf_picture[ref_frame_idx].frame;
  6086. const int gf_frame_offset = gf_group->frame_gop_index[frame_idx];
  6087. const int ref_gf_frame_offset =
  6088. gf_group->frame_gop_index[ref_frame_idx];
  6089. printf("=\n");
  6090. printf(
  6091. "frame_idx %d mi_rows %d mi_cols %d bsize %d ref_frame_idx %d "
  6092. "rf_idx %d gf_frame_offset %d ref_gf_frame_offset %d\n",
  6093. frame_idx, cm->mi_rows, cm->mi_cols, mi_width * MI_SIZE,
  6094. ref_frame_idx, rf_idx, gf_frame_offset, ref_gf_frame_offset);
  6095. for (mi_row = 0; mi_row < cm->mi_rows; ++mi_row) {
  6096. for (mi_col = 0; mi_col < cm->mi_cols; ++mi_col) {
  6097. if ((mi_row % mi_height) == 0 && (mi_col % mi_width) == 0) {
  6098. int_mv mv =
  6099. *get_pyramid_mv(tpl_frame, rf_idx, bsize, mi_row, mi_col);
  6100. printf("%d %d %d %d\n", mi_row, mi_col, mv.as_mv.row,
  6101. mv.as_mv.col);
  6102. }
  6103. }
  6104. }
  6105. for (mi_row = 0; mi_row < cm->mi_rows; ++mi_row) {
  6106. for (mi_col = 0; mi_col < cm->mi_cols; ++mi_col) {
  6107. if ((mi_row % mi_height) == 0 && (mi_col % mi_width) == 0) {
  6108. const TplDepStats *tpl_ptr =
  6109. &tpl_frame
  6110. ->tpl_stats_ptr[mi_row * tpl_frame->stride + mi_col];
  6111. printf("%f ", tpl_ptr->feature_score);
  6112. }
  6113. }
  6114. }
  6115. printf("\n");
  6116. for (mi_row = 0; mi_row < cm->mi_rows; mi_row += mi_height) {
  6117. for (mi_col = 0; mi_col < cm->mi_cols; mi_col += mi_width) {
  6118. const int mv_mode =
  6119. tpl_frame
  6120. ->mv_mode_arr[rf_idx][mi_row * tpl_frame->stride + mi_col];
  6121. printf("%d ", mv_mode);
  6122. }
  6123. }
  6124. printf("\n");
  6125. dump_frame_buf(gf_picture[frame_idx].frame);
  6126. dump_frame_buf(ref_frame_buf);
  6127. }
  6128. }
  6129. }
  6130. }
  6131. #endif // DUMP_TPL_STATS
  6132. #endif // CONFIG_NON_GREEDY_MV
  6133. static void init_tpl_buffer(VP9_COMP *cpi) {
  6134. VP9_COMMON *cm = &cpi->common;
  6135. int frame;
  6136. const int mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
  6137. const int mi_rows = mi_cols_aligned_to_sb(cm->mi_rows);
  6138. #if CONFIG_NON_GREEDY_MV
  6139. int sqr_bsize;
  6140. int rf_idx;
  6141. // TODO(angiebird): This probably needs further modifications to support
  6142. // frame scaling later on.
  6143. if (cpi->feature_score_loc_alloc == 0) {
  6144. // The smallest block size of motion field is 4x4, but the mi_unit is 8x8,
  6145. // therefore the number of units is "mi_rows * mi_cols * 4" here.
  6146. CHECK_MEM_ERROR(
  6147. cm, cpi->feature_score_loc_arr,
  6148. vpx_calloc(mi_rows * mi_cols * 4, sizeof(*cpi->feature_score_loc_arr)));
  6149. CHECK_MEM_ERROR(cm, cpi->feature_score_loc_sort,
  6150. vpx_calloc(mi_rows * mi_cols * 4,
  6151. sizeof(*cpi->feature_score_loc_sort)));
  6152. CHECK_MEM_ERROR(cm, cpi->feature_score_loc_heap,
  6153. vpx_calloc(mi_rows * mi_cols * 4,
  6154. sizeof(*cpi->feature_score_loc_heap)));
  6155. cpi->feature_score_loc_alloc = 1;
  6156. }
  6157. vpx_free(cpi->select_mv_arr);
  6158. CHECK_MEM_ERROR(
  6159. cm, cpi->select_mv_arr,
  6160. vpx_calloc(mi_rows * mi_cols * 4, sizeof(*cpi->select_mv_arr)));
  6161. #endif
  6162. // TODO(jingning): Reduce the actual memory use for tpl model build up.
  6163. for (frame = 0; frame < MAX_ARF_GOP_SIZE; ++frame) {
  6164. if (cpi->tpl_stats[frame].width >= mi_cols &&
  6165. cpi->tpl_stats[frame].height >= mi_rows &&
  6166. cpi->tpl_stats[frame].tpl_stats_ptr)
  6167. continue;
  6168. #if CONFIG_NON_GREEDY_MV
  6169. for (rf_idx = 0; rf_idx < 3; ++rf_idx) {
  6170. for (sqr_bsize = 0; sqr_bsize < SQUARE_BLOCK_SIZES; ++sqr_bsize) {
  6171. vpx_free(cpi->tpl_stats[frame].pyramid_mv_arr[rf_idx][sqr_bsize]);
  6172. CHECK_MEM_ERROR(
  6173. cm, cpi->tpl_stats[frame].pyramid_mv_arr[rf_idx][sqr_bsize],
  6174. vpx_calloc(
  6175. mi_rows * mi_cols * 4,
  6176. sizeof(
  6177. *cpi->tpl_stats[frame].pyramid_mv_arr[rf_idx][sqr_bsize])));
  6178. }
  6179. vpx_free(cpi->tpl_stats[frame].mv_mode_arr[rf_idx]);
  6180. CHECK_MEM_ERROR(
  6181. cm, cpi->tpl_stats[frame].mv_mode_arr[rf_idx],
  6182. vpx_calloc(mi_rows * mi_cols * 4,
  6183. sizeof(*cpi->tpl_stats[frame].mv_mode_arr[rf_idx])));
  6184. vpx_free(cpi->tpl_stats[frame].rd_diff_arr[rf_idx]);
  6185. CHECK_MEM_ERROR(
  6186. cm, cpi->tpl_stats[frame].rd_diff_arr[rf_idx],
  6187. vpx_calloc(mi_rows * mi_cols * 4,
  6188. sizeof(*cpi->tpl_stats[frame].rd_diff_arr[rf_idx])));
  6189. }
  6190. #endif
  6191. vpx_free(cpi->tpl_stats[frame].tpl_stats_ptr);
  6192. CHECK_MEM_ERROR(cm, cpi->tpl_stats[frame].tpl_stats_ptr,
  6193. vpx_calloc(mi_rows * mi_cols,
  6194. sizeof(*cpi->tpl_stats[frame].tpl_stats_ptr)));
  6195. cpi->tpl_stats[frame].is_valid = 0;
  6196. cpi->tpl_stats[frame].width = mi_cols;
  6197. cpi->tpl_stats[frame].height = mi_rows;
  6198. cpi->tpl_stats[frame].stride = mi_cols;
  6199. cpi->tpl_stats[frame].mi_rows = cm->mi_rows;
  6200. cpi->tpl_stats[frame].mi_cols = cm->mi_cols;
  6201. }
  6202. for (frame = 0; frame < REF_FRAMES; ++frame) {
  6203. cpi->enc_frame_buf[frame].mem_valid = 0;
  6204. cpi->enc_frame_buf[frame].released = 1;
  6205. }
  6206. }
  6207. static void setup_tpl_stats(VP9_COMP *cpi) {
  6208. GF_PICTURE gf_picture[MAX_ARF_GOP_SIZE];
  6209. const GF_GROUP *gf_group = &cpi->twopass.gf_group;
  6210. int tpl_group_frames = 0;
  6211. int frame_idx;
  6212. cpi->tpl_bsize = BLOCK_32X32;
  6213. init_gop_frames(cpi, gf_picture, gf_group, &tpl_group_frames);
  6214. init_tpl_stats(cpi);
  6215. // Backward propagation from tpl_group_frames to 1.
  6216. for (frame_idx = tpl_group_frames - 1; frame_idx > 0; --frame_idx) {
  6217. if (gf_picture[frame_idx].update_type == USE_BUF_FRAME) continue;
  6218. mc_flow_dispenser(cpi, gf_picture, frame_idx, cpi->tpl_bsize);
  6219. }
  6220. #if CONFIG_NON_GREEDY_MV
  6221. cpi->tpl_ready = 1;
  6222. #if DUMP_TPL_STATS
  6223. dump_tpl_stats(cpi, tpl_group_frames, gf_group, gf_picture, cpi->tpl_bsize);
  6224. #endif // DUMP_TPL_STATS
  6225. #endif // CONFIG_NON_GREEDY_MV
  6226. }
  6227. int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
  6228. size_t *size, uint8_t *dest, int64_t *time_stamp,
  6229. int64_t *time_end, int flush) {
  6230. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  6231. VP9_COMMON *const cm = &cpi->common;
  6232. BufferPool *const pool = cm->buffer_pool;
  6233. RATE_CONTROL *const rc = &cpi->rc;
  6234. struct vpx_usec_timer cmptimer;
  6235. YV12_BUFFER_CONFIG *force_src_buffer = NULL;
  6236. struct lookahead_entry *last_source = NULL;
  6237. struct lookahead_entry *source = NULL;
  6238. int arf_src_index;
  6239. const int gf_group_index = cpi->twopass.gf_group.index;
  6240. int i;
  6241. if (is_one_pass_cbr_svc(cpi)) {
  6242. vp9_one_pass_cbr_svc_start_layer(cpi);
  6243. }
  6244. vpx_usec_timer_start(&cmptimer);
  6245. vp9_set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
  6246. // Is multi-arf enabled.
  6247. // Note that at the moment multi_arf is only configured for 2 pass VBR and
  6248. // will not work properly with svc.
  6249. // Enable the Jingning's new "multi_layer_arf" code if "enable_auto_arf"
  6250. // is greater than or equal to 2.
  6251. if ((oxcf->pass == 2) && !cpi->use_svc && (cpi->oxcf.enable_auto_arf >= 2))
  6252. cpi->multi_layer_arf = 1;
  6253. else
  6254. cpi->multi_layer_arf = 0;
  6255. // Normal defaults
  6256. cm->reset_frame_context = 0;
  6257. cm->refresh_frame_context = 1;
  6258. if (!is_one_pass_cbr_svc(cpi)) {
  6259. cpi->refresh_last_frame = 1;
  6260. cpi->refresh_golden_frame = 0;
  6261. cpi->refresh_alt_ref_frame = 0;
  6262. }
  6263. // Should we encode an arf frame.
  6264. arf_src_index = get_arf_src_index(cpi);
  6265. if (arf_src_index) {
  6266. for (i = 0; i <= arf_src_index; ++i) {
  6267. struct lookahead_entry *e = vp9_lookahead_peek(cpi->lookahead, i);
  6268. // Avoid creating an alt-ref if there's a forced keyframe pending.
  6269. if (e == NULL) {
  6270. break;
  6271. } else if (e->flags == VPX_EFLAG_FORCE_KF) {
  6272. arf_src_index = 0;
  6273. flush = 1;
  6274. break;
  6275. }
  6276. }
  6277. }
  6278. // Clear arf index stack before group of pictures processing starts.
  6279. if (gf_group_index == 1) {
  6280. stack_init(cpi->twopass.gf_group.arf_index_stack, MAX_LAG_BUFFERS * 2);
  6281. cpi->twopass.gf_group.stack_size = 0;
  6282. }
  6283. if (arf_src_index) {
  6284. assert(arf_src_index <= rc->frames_to_key);
  6285. if ((source = vp9_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
  6286. cpi->alt_ref_source = source;
  6287. #if !CONFIG_REALTIME_ONLY
  6288. if ((oxcf->mode != REALTIME) && (oxcf->arnr_max_frames > 0) &&
  6289. (oxcf->arnr_strength > 0)) {
  6290. int bitrate = cpi->rc.avg_frame_bandwidth / 40;
  6291. int not_low_bitrate = bitrate > ALT_REF_AQ_LOW_BITRATE_BOUNDARY;
  6292. int not_last_frame = (cpi->lookahead->sz - arf_src_index > 1);
  6293. not_last_frame |= ALT_REF_AQ_APPLY_TO_LAST_FRAME;
  6294. // Produce the filtered ARF frame.
  6295. vp9_temporal_filter(cpi, arf_src_index);
  6296. vpx_extend_frame_borders(&cpi->alt_ref_buffer);
  6297. // for small bitrates segmentation overhead usually
  6298. // eats all bitrate gain from enabling delta quantizers
  6299. if (cpi->oxcf.alt_ref_aq != 0 && not_low_bitrate && not_last_frame)
  6300. vp9_alt_ref_aq_setup_mode(cpi->alt_ref_aq, cpi);
  6301. force_src_buffer = &cpi->alt_ref_buffer;
  6302. }
  6303. #endif
  6304. cm->show_frame = 0;
  6305. cm->intra_only = 0;
  6306. cpi->refresh_alt_ref_frame = 1;
  6307. cpi->refresh_golden_frame = 0;
  6308. cpi->refresh_last_frame = 0;
  6309. rc->is_src_frame_alt_ref = 0;
  6310. rc->source_alt_ref_pending = 0;
  6311. } else {
  6312. rc->source_alt_ref_pending = 0;
  6313. }
  6314. }
  6315. if (!source) {
  6316. // Get last frame source.
  6317. if (cm->current_video_frame > 0) {
  6318. if ((last_source = vp9_lookahead_peek(cpi->lookahead, -1)) == NULL)
  6319. return -1;
  6320. }
  6321. // Read in the source frame.
  6322. if (cpi->use_svc || cpi->svc.set_intra_only_frame)
  6323. source = vp9_svc_lookahead_pop(cpi, cpi->lookahead, flush);
  6324. else
  6325. source = vp9_lookahead_pop(cpi->lookahead, flush);
  6326. if (source != NULL) {
  6327. cm->show_frame = 1;
  6328. cm->intra_only = 0;
  6329. // if the flags indicate intra frame, but if the current picture is for
  6330. // non-zero spatial layer, it should not be an intra picture.
  6331. if ((source->flags & VPX_EFLAG_FORCE_KF) && cpi->use_svc &&
  6332. cpi->svc.spatial_layer_id > 0) {
  6333. source->flags &= ~(unsigned int)(VPX_EFLAG_FORCE_KF);
  6334. }
  6335. // Check to see if the frame should be encoded as an arf overlay.
  6336. check_src_altref(cpi, source);
  6337. }
  6338. }
  6339. if (source) {
  6340. cpi->un_scaled_source = cpi->Source =
  6341. force_src_buffer ? force_src_buffer : &source->img;
  6342. #ifdef ENABLE_KF_DENOISE
  6343. // Copy of raw source for metrics calculation.
  6344. if (is_psnr_calc_enabled(cpi))
  6345. vp9_copy_and_extend_frame(cpi->Source, &cpi->raw_unscaled_source);
  6346. #endif
  6347. cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
  6348. *time_stamp = source->ts_start;
  6349. *time_end = source->ts_end;
  6350. *frame_flags = (source->flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
  6351. } else {
  6352. *size = 0;
  6353. #if !CONFIG_REALTIME_ONLY
  6354. if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
  6355. vp9_end_first_pass(cpi); /* get last stats packet */
  6356. cpi->twopass.first_pass_done = 1;
  6357. }
  6358. #endif // !CONFIG_REALTIME_ONLY
  6359. return -1;
  6360. }
  6361. if (source->ts_start < cpi->first_time_stamp_ever) {
  6362. cpi->first_time_stamp_ever = source->ts_start;
  6363. cpi->last_end_time_stamp_seen = source->ts_start;
  6364. }
  6365. // Clear down mmx registers
  6366. vpx_clear_system_state();
  6367. // adjust frame rates based on timestamps given
  6368. if (cm->show_frame) {
  6369. if (cpi->use_svc && cpi->svc.use_set_ref_frame_config &&
  6370. cpi->svc.duration[cpi->svc.spatial_layer_id] > 0)
  6371. vp9_svc_adjust_frame_rate(cpi);
  6372. else
  6373. adjust_frame_rate(cpi, source);
  6374. }
  6375. if (is_one_pass_cbr_svc(cpi)) {
  6376. vp9_update_temporal_layer_framerate(cpi);
  6377. vp9_restore_layer_context(cpi);
  6378. }
  6379. // Find a free buffer for the new frame, releasing the reference previously
  6380. // held.
  6381. if (cm->new_fb_idx != INVALID_IDX) {
  6382. --pool->frame_bufs[cm->new_fb_idx].ref_count;
  6383. }
  6384. cm->new_fb_idx = get_free_fb(cm);
  6385. if (cm->new_fb_idx == INVALID_IDX) return -1;
  6386. cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
  6387. // Start with a 0 size frame.
  6388. *size = 0;
  6389. cpi->frame_flags = *frame_flags;
  6390. #if !CONFIG_REALTIME_ONLY
  6391. if ((oxcf->pass == 2) && !cpi->use_svc) {
  6392. vp9_rc_get_second_pass_params(cpi);
  6393. } else if (oxcf->pass == 1) {
  6394. set_frame_size(cpi);
  6395. }
  6396. #endif // !CONFIG_REALTIME_ONLY
  6397. if (oxcf->pass != 1 && cpi->level_constraint.level_index >= 0 &&
  6398. cpi->level_constraint.fail_flag == 0)
  6399. level_rc_framerate(cpi, arf_src_index);
  6400. if (cpi->oxcf.pass != 0 || cpi->use_svc || frame_is_intra_only(cm) == 1) {
  6401. for (i = 0; i < REFS_PER_FRAME; ++i) cpi->scaled_ref_idx[i] = INVALID_IDX;
  6402. }
  6403. if (cpi->kmeans_data_arr_alloc == 0) {
  6404. const int mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
  6405. const int mi_rows = mi_cols_aligned_to_sb(cm->mi_rows);
  6406. #if CONFIG_MULTITHREAD
  6407. pthread_mutex_init(&cpi->kmeans_mutex, NULL);
  6408. #endif
  6409. CHECK_MEM_ERROR(
  6410. cm, cpi->kmeans_data_arr,
  6411. vpx_calloc(mi_rows * mi_cols, sizeof(*cpi->kmeans_data_arr)));
  6412. cpi->kmeans_data_stride = mi_cols;
  6413. cpi->kmeans_data_arr_alloc = 1;
  6414. }
  6415. if (gf_group_index == 1 &&
  6416. cpi->twopass.gf_group.update_type[gf_group_index] == ARF_UPDATE &&
  6417. cpi->sf.enable_tpl_model) {
  6418. init_tpl_buffer(cpi);
  6419. vp9_estimate_qp_gop(cpi);
  6420. setup_tpl_stats(cpi);
  6421. }
  6422. #if CONFIG_BITSTREAM_DEBUG
  6423. assert(cpi->oxcf.max_threads == 0 &&
  6424. "bitstream debug tool does not support multithreading");
  6425. bitstream_queue_record_write();
  6426. #endif
  6427. #if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
  6428. bitstream_queue_set_frame_write(cm->current_video_frame * 2 + cm->show_frame);
  6429. #endif
  6430. cpi->td.mb.fp_src_pred = 0;
  6431. #if CONFIG_REALTIME_ONLY
  6432. if (cpi->use_svc) {
  6433. SvcEncode(cpi, size, dest, frame_flags);
  6434. } else {
  6435. // One pass encode
  6436. Pass0Encode(cpi, size, dest, frame_flags);
  6437. }
  6438. #else // !CONFIG_REALTIME_ONLY
  6439. if (oxcf->pass == 1 && !cpi->use_svc) {
  6440. const int lossless = is_lossless_requested(oxcf);
  6441. #if CONFIG_VP9_HIGHBITDEPTH
  6442. if (cpi->oxcf.use_highbitdepth)
  6443. cpi->td.mb.fwd_txfm4x4 =
  6444. lossless ? vp9_highbd_fwht4x4 : vpx_highbd_fdct4x4;
  6445. else
  6446. cpi->td.mb.fwd_txfm4x4 = lossless ? vp9_fwht4x4 : vpx_fdct4x4;
  6447. cpi->td.mb.highbd_inv_txfm_add =
  6448. lossless ? vp9_highbd_iwht4x4_add : vp9_highbd_idct4x4_add;
  6449. #else
  6450. cpi->td.mb.fwd_txfm4x4 = lossless ? vp9_fwht4x4 : vpx_fdct4x4;
  6451. #endif // CONFIG_VP9_HIGHBITDEPTH
  6452. cpi->td.mb.inv_txfm_add = lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
  6453. vp9_first_pass(cpi, source);
  6454. } else if (oxcf->pass == 2 && !cpi->use_svc) {
  6455. Pass2Encode(cpi, size, dest, frame_flags);
  6456. } else if (cpi->use_svc) {
  6457. SvcEncode(cpi, size, dest, frame_flags);
  6458. } else {
  6459. // One pass encode
  6460. Pass0Encode(cpi, size, dest, frame_flags);
  6461. }
  6462. #endif // CONFIG_REALTIME_ONLY
  6463. if (cm->show_frame) cm->cur_show_frame_fb_idx = cm->new_fb_idx;
  6464. if (cm->refresh_frame_context)
  6465. cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
  6466. // No frame encoded, or frame was dropped, release scaled references.
  6467. if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
  6468. release_scaled_references(cpi);
  6469. }
  6470. if (*size > 0) {
  6471. cpi->droppable = !frame_is_reference(cpi);
  6472. }
  6473. // Save layer specific state.
  6474. if (is_one_pass_cbr_svc(cpi) || ((cpi->svc.number_temporal_layers > 1 ||
  6475. cpi->svc.number_spatial_layers > 1) &&
  6476. oxcf->pass == 2)) {
  6477. vp9_save_layer_context(cpi);
  6478. }
  6479. vpx_usec_timer_mark(&cmptimer);
  6480. cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
  6481. // Should we calculate metrics for the frame.
  6482. if (is_psnr_calc_enabled(cpi)) generate_psnr_packet(cpi);
  6483. if (cpi->keep_level_stats && oxcf->pass != 1)
  6484. update_level_info(cpi, size, arf_src_index);
  6485. #if CONFIG_INTERNAL_STATS
  6486. if (oxcf->pass != 1) {
  6487. double samples = 0.0;
  6488. cpi->bytes += (int)(*size);
  6489. if (cm->show_frame) {
  6490. uint32_t bit_depth = 8;
  6491. uint32_t in_bit_depth = 8;
  6492. cpi->count++;
  6493. #if CONFIG_VP9_HIGHBITDEPTH
  6494. if (cm->use_highbitdepth) {
  6495. in_bit_depth = cpi->oxcf.input_bit_depth;
  6496. bit_depth = cm->bit_depth;
  6497. }
  6498. #endif
  6499. if (cpi->b_calculate_psnr) {
  6500. YV12_BUFFER_CONFIG *orig = cpi->raw_source_frame;
  6501. YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
  6502. YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
  6503. PSNR_STATS psnr;
  6504. #if CONFIG_VP9_HIGHBITDEPTH
  6505. vpx_calc_highbd_psnr(orig, recon, &psnr, cpi->td.mb.e_mbd.bd,
  6506. in_bit_depth);
  6507. #else
  6508. vpx_calc_psnr(orig, recon, &psnr);
  6509. #endif // CONFIG_VP9_HIGHBITDEPTH
  6510. adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3],
  6511. psnr.psnr[0], &cpi->psnr);
  6512. cpi->total_sq_error += psnr.sse[0];
  6513. cpi->total_samples += psnr.samples[0];
  6514. samples = psnr.samples[0];
  6515. {
  6516. PSNR_STATS psnr2;
  6517. double frame_ssim2 = 0, weight = 0;
  6518. #if CONFIG_VP9_POSTPROC
  6519. if (vpx_alloc_frame_buffer(
  6520. pp, recon->y_crop_width, recon->y_crop_height,
  6521. cm->subsampling_x, cm->subsampling_y,
  6522. #if CONFIG_VP9_HIGHBITDEPTH
  6523. cm->use_highbitdepth,
  6524. #endif
  6525. VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment) < 0) {
  6526. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  6527. "Failed to allocate post processing buffer");
  6528. }
  6529. {
  6530. vp9_ppflags_t ppflags;
  6531. ppflags.post_proc_flag = VP9D_DEBLOCK;
  6532. ppflags.deblocking_level = 0; // not used in vp9_post_proc_frame()
  6533. ppflags.noise_level = 0; // not used in vp9_post_proc_frame()
  6534. vp9_post_proc_frame(cm, pp, &ppflags,
  6535. cpi->un_scaled_source->y_width);
  6536. }
  6537. #endif
  6538. vpx_clear_system_state();
  6539. #if CONFIG_VP9_HIGHBITDEPTH
  6540. vpx_calc_highbd_psnr(orig, pp, &psnr2, cpi->td.mb.e_mbd.bd,
  6541. cpi->oxcf.input_bit_depth);
  6542. #else
  6543. vpx_calc_psnr(orig, pp, &psnr2);
  6544. #endif // CONFIG_VP9_HIGHBITDEPTH
  6545. cpi->totalp_sq_error += psnr2.sse[0];
  6546. cpi->totalp_samples += psnr2.samples[0];
  6547. adjust_image_stat(psnr2.psnr[1], psnr2.psnr[2], psnr2.psnr[3],
  6548. psnr2.psnr[0], &cpi->psnrp);
  6549. #if CONFIG_VP9_HIGHBITDEPTH
  6550. if (cm->use_highbitdepth) {
  6551. frame_ssim2 = vpx_highbd_calc_ssim(orig, recon, &weight, bit_depth,
  6552. in_bit_depth);
  6553. } else {
  6554. frame_ssim2 = vpx_calc_ssim(orig, recon, &weight);
  6555. }
  6556. #else
  6557. frame_ssim2 = vpx_calc_ssim(orig, recon, &weight);
  6558. #endif // CONFIG_VP9_HIGHBITDEPTH
  6559. cpi->worst_ssim = VPXMIN(cpi->worst_ssim, frame_ssim2);
  6560. cpi->summed_quality += frame_ssim2 * weight;
  6561. cpi->summed_weights += weight;
  6562. #if CONFIG_VP9_HIGHBITDEPTH
  6563. if (cm->use_highbitdepth) {
  6564. frame_ssim2 = vpx_highbd_calc_ssim(orig, pp, &weight, bit_depth,
  6565. in_bit_depth);
  6566. } else {
  6567. frame_ssim2 = vpx_calc_ssim(orig, pp, &weight);
  6568. }
  6569. #else
  6570. frame_ssim2 = vpx_calc_ssim(orig, pp, &weight);
  6571. #endif // CONFIG_VP9_HIGHBITDEPTH
  6572. cpi->summedp_quality += frame_ssim2 * weight;
  6573. cpi->summedp_weights += weight;
  6574. #if 0
  6575. if (cm->show_frame) {
  6576. FILE *f = fopen("q_used.stt", "a");
  6577. fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
  6578. cpi->common.current_video_frame, psnr2.psnr[1],
  6579. psnr2.psnr[2], psnr2.psnr[3], psnr2.psnr[0], frame_ssim2);
  6580. fclose(f);
  6581. }
  6582. #endif
  6583. }
  6584. }
  6585. if (cpi->b_calculate_blockiness) {
  6586. #if CONFIG_VP9_HIGHBITDEPTH
  6587. if (!cm->use_highbitdepth)
  6588. #endif
  6589. {
  6590. double frame_blockiness = vp9_get_blockiness(
  6591. cpi->Source->y_buffer, cpi->Source->y_stride,
  6592. cm->frame_to_show->y_buffer, cm->frame_to_show->y_stride,
  6593. cpi->Source->y_width, cpi->Source->y_height);
  6594. cpi->worst_blockiness =
  6595. VPXMAX(cpi->worst_blockiness, frame_blockiness);
  6596. cpi->total_blockiness += frame_blockiness;
  6597. }
  6598. }
  6599. if (cpi->b_calculate_consistency) {
  6600. #if CONFIG_VP9_HIGHBITDEPTH
  6601. if (!cm->use_highbitdepth)
  6602. #endif
  6603. {
  6604. double this_inconsistency = vpx_get_ssim_metrics(
  6605. cpi->Source->y_buffer, cpi->Source->y_stride,
  6606. cm->frame_to_show->y_buffer, cm->frame_to_show->y_stride,
  6607. cpi->Source->y_width, cpi->Source->y_height, cpi->ssim_vars,
  6608. &cpi->metrics, 1);
  6609. const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
  6610. double consistency =
  6611. vpx_sse_to_psnr(samples, peak, (double)cpi->total_inconsistency);
  6612. if (consistency > 0.0)
  6613. cpi->worst_consistency =
  6614. VPXMIN(cpi->worst_consistency, consistency);
  6615. cpi->total_inconsistency += this_inconsistency;
  6616. }
  6617. }
  6618. {
  6619. double y, u, v, frame_all;
  6620. frame_all = vpx_calc_fastssim(cpi->Source, cm->frame_to_show, &y, &u,
  6621. &v, bit_depth, in_bit_depth);
  6622. adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
  6623. }
  6624. {
  6625. double y, u, v, frame_all;
  6626. frame_all = vpx_psnrhvs(cpi->Source, cm->frame_to_show, &y, &u, &v,
  6627. bit_depth, in_bit_depth);
  6628. adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
  6629. }
  6630. }
  6631. }
  6632. #endif
  6633. if (is_one_pass_cbr_svc(cpi)) {
  6634. if (cm->show_frame) {
  6635. ++cpi->svc.spatial_layer_to_encode;
  6636. if (cpi->svc.spatial_layer_to_encode >= cpi->svc.number_spatial_layers)
  6637. cpi->svc.spatial_layer_to_encode = 0;
  6638. }
  6639. }
  6640. vpx_clear_system_state();
  6641. return 0;
  6642. }
  6643. int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
  6644. vp9_ppflags_t *flags) {
  6645. VP9_COMMON *cm = &cpi->common;
  6646. #if !CONFIG_VP9_POSTPROC
  6647. (void)flags;
  6648. #endif
  6649. if (!cm->show_frame) {
  6650. return -1;
  6651. } else {
  6652. int ret;
  6653. #if CONFIG_VP9_POSTPROC
  6654. ret = vp9_post_proc_frame(cm, dest, flags, cpi->un_scaled_source->y_width);
  6655. #else
  6656. if (cm->frame_to_show) {
  6657. *dest = *cm->frame_to_show;
  6658. dest->y_width = cm->width;
  6659. dest->y_height = cm->height;
  6660. dest->uv_width = cm->width >> cm->subsampling_x;
  6661. dest->uv_height = cm->height >> cm->subsampling_y;
  6662. ret = 0;
  6663. } else {
  6664. ret = -1;
  6665. }
  6666. #endif // !CONFIG_VP9_POSTPROC
  6667. vpx_clear_system_state();
  6668. return ret;
  6669. }
  6670. }
  6671. int vp9_set_internal_size(VP9_COMP *cpi, VPX_SCALING horiz_mode,
  6672. VPX_SCALING vert_mode) {
  6673. VP9_COMMON *cm = &cpi->common;
  6674. int hr = 0, hs = 0, vr = 0, vs = 0;
  6675. if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
  6676. Scale2Ratio(horiz_mode, &hr, &hs);
  6677. Scale2Ratio(vert_mode, &vr, &vs);
  6678. // always go to the next whole number
  6679. cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
  6680. cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
  6681. if (cm->current_video_frame) {
  6682. assert(cm->width <= cpi->initial_width);
  6683. assert(cm->height <= cpi->initial_height);
  6684. }
  6685. update_frame_size(cpi);
  6686. return 0;
  6687. }
  6688. int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
  6689. unsigned int height) {
  6690. VP9_COMMON *cm = &cpi->common;
  6691. #if CONFIG_VP9_HIGHBITDEPTH
  6692. check_initial_width(cpi, cm->use_highbitdepth, 1, 1);
  6693. #else
  6694. check_initial_width(cpi, 1, 1);
  6695. #endif // CONFIG_VP9_HIGHBITDEPTH
  6696. #if CONFIG_VP9_TEMPORAL_DENOISING
  6697. setup_denoiser_buffer(cpi);
  6698. #endif
  6699. if (width) {
  6700. cm->width = width;
  6701. if (cm->width > cpi->initial_width) {
  6702. cm->width = cpi->initial_width;
  6703. printf("Warning: Desired width too large, changed to %d\n", cm->width);
  6704. }
  6705. }
  6706. if (height) {
  6707. cm->height = height;
  6708. if (cm->height > cpi->initial_height) {
  6709. cm->height = cpi->initial_height;
  6710. printf("Warning: Desired height too large, changed to %d\n", cm->height);
  6711. }
  6712. }
  6713. assert(cm->width <= cpi->initial_width);
  6714. assert(cm->height <= cpi->initial_height);
  6715. update_frame_size(cpi);
  6716. return 0;
  6717. }
  6718. void vp9_set_svc(VP9_COMP *cpi, int use_svc) {
  6719. cpi->use_svc = use_svc;
  6720. return;
  6721. }
  6722. int vp9_get_quantizer(VP9_COMP *cpi) { return cpi->common.base_qindex; }
  6723. void vp9_apply_encoding_flags(VP9_COMP *cpi, vpx_enc_frame_flags_t flags) {
  6724. if (flags &
  6725. (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF)) {
  6726. int ref = 7;
  6727. if (flags & VP8_EFLAG_NO_REF_LAST) ref ^= VP9_LAST_FLAG;
  6728. if (flags & VP8_EFLAG_NO_REF_GF) ref ^= VP9_GOLD_FLAG;
  6729. if (flags & VP8_EFLAG_NO_REF_ARF) ref ^= VP9_ALT_FLAG;
  6730. vp9_use_as_reference(cpi, ref);
  6731. }
  6732. if (flags &
  6733. (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
  6734. VP8_EFLAG_FORCE_GF | VP8_EFLAG_FORCE_ARF)) {
  6735. int upd = 7;
  6736. if (flags & VP8_EFLAG_NO_UPD_LAST) upd ^= VP9_LAST_FLAG;
  6737. if (flags & VP8_EFLAG_NO_UPD_GF) upd ^= VP9_GOLD_FLAG;
  6738. if (flags & VP8_EFLAG_NO_UPD_ARF) upd ^= VP9_ALT_FLAG;
  6739. vp9_update_reference(cpi, upd);
  6740. }
  6741. if (flags & VP8_EFLAG_NO_UPD_ENTROPY) {
  6742. vp9_update_entropy(cpi, 0);
  6743. }
  6744. }
  6745. void vp9_set_row_mt(VP9_COMP *cpi) {
  6746. // Enable row based multi-threading for supported modes of encoding
  6747. cpi->row_mt = 0;
  6748. if (((cpi->oxcf.mode == GOOD || cpi->oxcf.mode == BEST) &&
  6749. cpi->oxcf.speed < 5 && cpi->oxcf.pass == 1) &&
  6750. cpi->oxcf.row_mt && !cpi->use_svc)
  6751. cpi->row_mt = 1;
  6752. if (cpi->oxcf.mode == GOOD && cpi->oxcf.speed < 5 &&
  6753. (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) && cpi->oxcf.row_mt &&
  6754. !cpi->use_svc)
  6755. cpi->row_mt = 1;
  6756. // In realtime mode, enable row based multi-threading for all the speed levels
  6757. // where non-rd path is used.
  6758. if (cpi->oxcf.mode == REALTIME && cpi->oxcf.speed >= 5 && cpi->oxcf.row_mt) {
  6759. cpi->row_mt = 1;
  6760. }
  6761. if (cpi->row_mt)
  6762. cpi->row_mt_bit_exact = 1;
  6763. else
  6764. cpi->row_mt_bit_exact = 0;
  6765. }