vp9_encoder.c 199 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632
  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 <math.h>
  11. #include <stdio.h>
  12. #include <limits.h>
  13. #include "./vp9_rtcd.h"
  14. #include "./vpx_config.h"
  15. #include "./vpx_dsp_rtcd.h"
  16. #include "./vpx_scale_rtcd.h"
  17. #include "vpx_dsp/psnr.h"
  18. #include "vpx_dsp/vpx_dsp_common.h"
  19. #include "vpx_dsp/vpx_filter.h"
  20. #if CONFIG_INTERNAL_STATS
  21. #include "vpx_dsp/ssim.h"
  22. #endif
  23. #include "vpx_ports/mem.h"
  24. #include "vpx_ports/system_state.h"
  25. #include "vpx_ports/vpx_timer.h"
  26. #include "vp9/common/vp9_alloccommon.h"
  27. #include "vp9/common/vp9_filter.h"
  28. #include "vp9/common/vp9_idct.h"
  29. #if CONFIG_VP9_POSTPROC
  30. #include "vp9/common/vp9_postproc.h"
  31. #endif
  32. #include "vp9/common/vp9_reconinter.h"
  33. #include "vp9/common/vp9_reconintra.h"
  34. #include "vp9/common/vp9_tile_common.h"
  35. #include "vp9/encoder/vp9_alt_ref_aq.h"
  36. #include "vp9/encoder/vp9_aq_360.h"
  37. #include "vp9/encoder/vp9_aq_complexity.h"
  38. #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
  39. #include "vp9/encoder/vp9_aq_variance.h"
  40. #include "vp9/encoder/vp9_bitstream.h"
  41. #include "vp9/encoder/vp9_context_tree.h"
  42. #include "vp9/encoder/vp9_encodeframe.h"
  43. #include "vp9/encoder/vp9_encodemv.h"
  44. #include "vp9/encoder/vp9_encoder.h"
  45. #include "vp9/encoder/vp9_extend.h"
  46. #include "vp9/encoder/vp9_ethread.h"
  47. #include "vp9/encoder/vp9_firstpass.h"
  48. #include "vp9/encoder/vp9_mbgraph.h"
  49. #include "vp9/encoder/vp9_multi_thread.h"
  50. #include "vp9/encoder/vp9_noise_estimate.h"
  51. #include "vp9/encoder/vp9_picklpf.h"
  52. #include "vp9/encoder/vp9_ratectrl.h"
  53. #include "vp9/encoder/vp9_rd.h"
  54. #include "vp9/encoder/vp9_resize.h"
  55. #include "vp9/encoder/vp9_segmentation.h"
  56. #include "vp9/encoder/vp9_skin_detection.h"
  57. #include "vp9/encoder/vp9_speed_features.h"
  58. #include "vp9/encoder/vp9_svc_layercontext.h"
  59. #include "vp9/encoder/vp9_temporal_filter.h"
  60. #define AM_SEGMENT_ID_INACTIVE 7
  61. #define AM_SEGMENT_ID_ACTIVE 0
  62. #define ALTREF_HIGH_PRECISION_MV 1 // Whether to use high precision mv
  63. // for altref computation.
  64. #define HIGH_PRECISION_MV_QTHRESH 200 // Q threshold for high precision
  65. // mv. Choose a very high value for
  66. // now so that HIGH_PRECISION is always
  67. // chosen.
  68. #define FRAME_SIZE_FACTOR 128 // empirical params for context model threshold
  69. #define FRAME_RATE_FACTOR 8
  70. #ifdef OUTPUT_YUV_DENOISED
  71. FILE *yuv_denoised_file = NULL;
  72. #endif
  73. #ifdef OUTPUT_YUV_SKINMAP
  74. static FILE *yuv_skinmap_file = NULL;
  75. #endif
  76. #ifdef OUTPUT_YUV_REC
  77. FILE *yuv_rec_file;
  78. #endif
  79. #if 0
  80. FILE *framepsnr;
  81. FILE *kf_list;
  82. FILE *keyfile;
  83. #endif
  84. #ifdef ENABLE_KF_DENOISE
  85. // Test condition for spatial denoise of source.
  86. static int is_spatial_denoise_enabled(VP9_COMP *cpi) {
  87. VP9_COMMON *const cm = &cpi->common;
  88. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  89. return (oxcf->pass != 1) && !is_lossless_requested(&cpi->oxcf) &&
  90. frame_is_intra_only(cm);
  91. }
  92. #endif
  93. // compute adaptive threshold for skip recoding
  94. static int compute_context_model_thresh(const VP9_COMP *const cpi) {
  95. const VP9_COMMON *const cm = &cpi->common;
  96. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  97. const int frame_size = (cm->width * cm->height) >> 10;
  98. const int bitrate = (int)(oxcf->target_bandwidth >> 10);
  99. const int qindex_factor = cm->base_qindex + (MAXQ >> 1);
  100. // This equation makes the threshold adaptive to frame size.
  101. // Coding gain obtained by recoding comes from alternate frames of large
  102. // content change. We skip recoding if the difference of previous and current
  103. // frame context probability model is less than a certain threshold.
  104. // The first component is the most critical part to guarantee adaptivity.
  105. // Other parameters are estimated based on normal setting of hd resolution
  106. // parameters. e.g frame_size = 1920x1080, bitrate = 8000, qindex_factor < 50
  107. const int thresh =
  108. ((FRAME_SIZE_FACTOR * frame_size - FRAME_RATE_FACTOR * bitrate) *
  109. qindex_factor) >>
  110. 9;
  111. return thresh;
  112. }
  113. // compute the total cost difference between current
  114. // and previous frame context prob model.
  115. static int compute_context_model_diff(const VP9_COMMON *const cm) {
  116. const FRAME_CONTEXT *const pre_fc =
  117. &cm->frame_contexts[cm->frame_context_idx];
  118. const FRAME_CONTEXT *const cur_fc = cm->fc;
  119. const FRAME_COUNTS *counts = &cm->counts;
  120. vpx_prob pre_last_prob, cur_last_prob;
  121. int diff = 0;
  122. int i, j, k, l, m, n;
  123. // y_mode_prob
  124. for (i = 0; i < BLOCK_SIZE_GROUPS; ++i) {
  125. for (j = 0; j < INTRA_MODES - 1; ++j) {
  126. diff += (int)counts->y_mode[i][j] *
  127. (pre_fc->y_mode_prob[i][j] - cur_fc->y_mode_prob[i][j]);
  128. }
  129. pre_last_prob = MAX_PROB - pre_fc->y_mode_prob[i][INTRA_MODES - 2];
  130. cur_last_prob = MAX_PROB - cur_fc->y_mode_prob[i][INTRA_MODES - 2];
  131. diff += (int)counts->y_mode[i][INTRA_MODES - 1] *
  132. (pre_last_prob - cur_last_prob);
  133. }
  134. // uv_mode_prob
  135. for (i = 0; i < INTRA_MODES; ++i) {
  136. for (j = 0; j < INTRA_MODES - 1; ++j) {
  137. diff += (int)counts->uv_mode[i][j] *
  138. (pre_fc->uv_mode_prob[i][j] - cur_fc->uv_mode_prob[i][j]);
  139. }
  140. pre_last_prob = MAX_PROB - pre_fc->uv_mode_prob[i][INTRA_MODES - 2];
  141. cur_last_prob = MAX_PROB - cur_fc->uv_mode_prob[i][INTRA_MODES - 2];
  142. diff += (int)counts->uv_mode[i][INTRA_MODES - 1] *
  143. (pre_last_prob - cur_last_prob);
  144. }
  145. // partition_prob
  146. for (i = 0; i < PARTITION_CONTEXTS; ++i) {
  147. for (j = 0; j < PARTITION_TYPES - 1; ++j) {
  148. diff += (int)counts->partition[i][j] *
  149. (pre_fc->partition_prob[i][j] - cur_fc->partition_prob[i][j]);
  150. }
  151. pre_last_prob = MAX_PROB - pre_fc->partition_prob[i][PARTITION_TYPES - 2];
  152. cur_last_prob = MAX_PROB - cur_fc->partition_prob[i][PARTITION_TYPES - 2];
  153. diff += (int)counts->partition[i][PARTITION_TYPES - 1] *
  154. (pre_last_prob - cur_last_prob);
  155. }
  156. // coef_probs
  157. for (i = 0; i < TX_SIZES; ++i) {
  158. for (j = 0; j < PLANE_TYPES; ++j) {
  159. for (k = 0; k < REF_TYPES; ++k) {
  160. for (l = 0; l < COEF_BANDS; ++l) {
  161. for (m = 0; m < BAND_COEFF_CONTEXTS(l); ++m) {
  162. for (n = 0; n < UNCONSTRAINED_NODES; ++n) {
  163. diff += (int)counts->coef[i][j][k][l][m][n] *
  164. (pre_fc->coef_probs[i][j][k][l][m][n] -
  165. cur_fc->coef_probs[i][j][k][l][m][n]);
  166. }
  167. pre_last_prob =
  168. MAX_PROB -
  169. pre_fc->coef_probs[i][j][k][l][m][UNCONSTRAINED_NODES - 1];
  170. cur_last_prob =
  171. MAX_PROB -
  172. cur_fc->coef_probs[i][j][k][l][m][UNCONSTRAINED_NODES - 1];
  173. diff += (int)counts->coef[i][j][k][l][m][UNCONSTRAINED_NODES] *
  174. (pre_last_prob - cur_last_prob);
  175. }
  176. }
  177. }
  178. }
  179. }
  180. // switchable_interp_prob
  181. for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i) {
  182. for (j = 0; j < SWITCHABLE_FILTERS - 1; ++j) {
  183. diff += (int)counts->switchable_interp[i][j] *
  184. (pre_fc->switchable_interp_prob[i][j] -
  185. cur_fc->switchable_interp_prob[i][j]);
  186. }
  187. pre_last_prob =
  188. MAX_PROB - pre_fc->switchable_interp_prob[i][SWITCHABLE_FILTERS - 2];
  189. cur_last_prob =
  190. MAX_PROB - cur_fc->switchable_interp_prob[i][SWITCHABLE_FILTERS - 2];
  191. diff += (int)counts->switchable_interp[i][SWITCHABLE_FILTERS - 1] *
  192. (pre_last_prob - cur_last_prob);
  193. }
  194. // inter_mode_probs
  195. for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
  196. for (j = 0; j < INTER_MODES - 1; ++j) {
  197. diff += (int)counts->inter_mode[i][j] *
  198. (pre_fc->inter_mode_probs[i][j] - cur_fc->inter_mode_probs[i][j]);
  199. }
  200. pre_last_prob = MAX_PROB - pre_fc->inter_mode_probs[i][INTER_MODES - 2];
  201. cur_last_prob = MAX_PROB - cur_fc->inter_mode_probs[i][INTER_MODES - 2];
  202. diff += (int)counts->inter_mode[i][INTER_MODES - 1] *
  203. (pre_last_prob - cur_last_prob);
  204. }
  205. // intra_inter_prob
  206. for (i = 0; i < INTRA_INTER_CONTEXTS; ++i) {
  207. diff += (int)counts->intra_inter[i][0] *
  208. (pre_fc->intra_inter_prob[i] - cur_fc->intra_inter_prob[i]);
  209. pre_last_prob = MAX_PROB - pre_fc->intra_inter_prob[i];
  210. cur_last_prob = MAX_PROB - cur_fc->intra_inter_prob[i];
  211. diff += (int)counts->intra_inter[i][1] * (pre_last_prob - cur_last_prob);
  212. }
  213. // comp_inter_prob
  214. for (i = 0; i < COMP_INTER_CONTEXTS; ++i) {
  215. diff += (int)counts->comp_inter[i][0] *
  216. (pre_fc->comp_inter_prob[i] - cur_fc->comp_inter_prob[i]);
  217. pre_last_prob = MAX_PROB - pre_fc->comp_inter_prob[i];
  218. cur_last_prob = MAX_PROB - cur_fc->comp_inter_prob[i];
  219. diff += (int)counts->comp_inter[i][1] * (pre_last_prob - cur_last_prob);
  220. }
  221. // single_ref_prob
  222. for (i = 0; i < REF_CONTEXTS; ++i) {
  223. for (j = 0; j < 2; ++j) {
  224. diff += (int)counts->single_ref[i][j][0] *
  225. (pre_fc->single_ref_prob[i][j] - cur_fc->single_ref_prob[i][j]);
  226. pre_last_prob = MAX_PROB - pre_fc->single_ref_prob[i][j];
  227. cur_last_prob = MAX_PROB - cur_fc->single_ref_prob[i][j];
  228. diff +=
  229. (int)counts->single_ref[i][j][1] * (pre_last_prob - cur_last_prob);
  230. }
  231. }
  232. // comp_ref_prob
  233. for (i = 0; i < REF_CONTEXTS; ++i) {
  234. diff += (int)counts->comp_ref[i][0] *
  235. (pre_fc->comp_ref_prob[i] - cur_fc->comp_ref_prob[i]);
  236. pre_last_prob = MAX_PROB - pre_fc->comp_ref_prob[i];
  237. cur_last_prob = MAX_PROB - cur_fc->comp_ref_prob[i];
  238. diff += (int)counts->comp_ref[i][1] * (pre_last_prob - cur_last_prob);
  239. }
  240. // tx_probs
  241. for (i = 0; i < TX_SIZE_CONTEXTS; ++i) {
  242. // p32x32
  243. for (j = 0; j < TX_SIZES - 1; ++j) {
  244. diff += (int)counts->tx.p32x32[i][j] *
  245. (pre_fc->tx_probs.p32x32[i][j] - cur_fc->tx_probs.p32x32[i][j]);
  246. }
  247. pre_last_prob = MAX_PROB - pre_fc->tx_probs.p32x32[i][TX_SIZES - 2];
  248. cur_last_prob = MAX_PROB - cur_fc->tx_probs.p32x32[i][TX_SIZES - 2];
  249. diff += (int)counts->tx.p32x32[i][TX_SIZES - 1] *
  250. (pre_last_prob - cur_last_prob);
  251. // p16x16
  252. for (j = 0; j < TX_SIZES - 2; ++j) {
  253. diff += (int)counts->tx.p16x16[i][j] *
  254. (pre_fc->tx_probs.p16x16[i][j] - cur_fc->tx_probs.p16x16[i][j]);
  255. }
  256. pre_last_prob = MAX_PROB - pre_fc->tx_probs.p16x16[i][TX_SIZES - 3];
  257. cur_last_prob = MAX_PROB - cur_fc->tx_probs.p16x16[i][TX_SIZES - 3];
  258. diff += (int)counts->tx.p16x16[i][TX_SIZES - 2] *
  259. (pre_last_prob - cur_last_prob);
  260. // p8x8
  261. for (j = 0; j < TX_SIZES - 3; ++j) {
  262. diff += (int)counts->tx.p8x8[i][j] *
  263. (pre_fc->tx_probs.p8x8[i][j] - cur_fc->tx_probs.p8x8[i][j]);
  264. }
  265. pre_last_prob = MAX_PROB - pre_fc->tx_probs.p8x8[i][TX_SIZES - 4];
  266. cur_last_prob = MAX_PROB - cur_fc->tx_probs.p8x8[i][TX_SIZES - 4];
  267. diff +=
  268. (int)counts->tx.p8x8[i][TX_SIZES - 3] * (pre_last_prob - cur_last_prob);
  269. }
  270. // skip_probs
  271. for (i = 0; i < SKIP_CONTEXTS; ++i) {
  272. diff += (int)counts->skip[i][0] *
  273. (pre_fc->skip_probs[i] - cur_fc->skip_probs[i]);
  274. pre_last_prob = MAX_PROB - pre_fc->skip_probs[i];
  275. cur_last_prob = MAX_PROB - cur_fc->skip_probs[i];
  276. diff += (int)counts->skip[i][1] * (pre_last_prob - cur_last_prob);
  277. }
  278. // mv
  279. for (i = 0; i < MV_JOINTS - 1; ++i) {
  280. diff += (int)counts->mv.joints[i] *
  281. (pre_fc->nmvc.joints[i] - cur_fc->nmvc.joints[i]);
  282. }
  283. pre_last_prob = MAX_PROB - pre_fc->nmvc.joints[MV_JOINTS - 2];
  284. cur_last_prob = MAX_PROB - cur_fc->nmvc.joints[MV_JOINTS - 2];
  285. diff +=
  286. (int)counts->mv.joints[MV_JOINTS - 1] * (pre_last_prob - cur_last_prob);
  287. for (i = 0; i < 2; ++i) {
  288. const nmv_component_counts *nmv_count = &counts->mv.comps[i];
  289. const nmv_component *pre_nmv_prob = &pre_fc->nmvc.comps[i];
  290. const nmv_component *cur_nmv_prob = &cur_fc->nmvc.comps[i];
  291. // sign
  292. diff += (int)nmv_count->sign[0] * (pre_nmv_prob->sign - cur_nmv_prob->sign);
  293. pre_last_prob = MAX_PROB - pre_nmv_prob->sign;
  294. cur_last_prob = MAX_PROB - cur_nmv_prob->sign;
  295. diff += (int)nmv_count->sign[1] * (pre_last_prob - cur_last_prob);
  296. // classes
  297. for (j = 0; j < MV_CLASSES - 1; ++j) {
  298. diff += (int)nmv_count->classes[j] *
  299. (pre_nmv_prob->classes[j] - cur_nmv_prob->classes[j]);
  300. }
  301. pre_last_prob = MAX_PROB - pre_nmv_prob->classes[MV_CLASSES - 2];
  302. cur_last_prob = MAX_PROB - cur_nmv_prob->classes[MV_CLASSES - 2];
  303. diff += (int)nmv_count->classes[MV_CLASSES - 1] *
  304. (pre_last_prob - cur_last_prob);
  305. // class0
  306. for (j = 0; j < CLASS0_SIZE - 1; ++j) {
  307. diff += (int)nmv_count->class0[j] *
  308. (pre_nmv_prob->class0[j] - cur_nmv_prob->class0[j]);
  309. }
  310. pre_last_prob = MAX_PROB - pre_nmv_prob->class0[CLASS0_SIZE - 2];
  311. cur_last_prob = MAX_PROB - cur_nmv_prob->class0[CLASS0_SIZE - 2];
  312. diff += (int)nmv_count->class0[CLASS0_SIZE - 1] *
  313. (pre_last_prob - cur_last_prob);
  314. // bits
  315. for (j = 0; j < MV_OFFSET_BITS; ++j) {
  316. diff += (int)nmv_count->bits[j][0] *
  317. (pre_nmv_prob->bits[j] - cur_nmv_prob->bits[j]);
  318. pre_last_prob = MAX_PROB - pre_nmv_prob->bits[j];
  319. cur_last_prob = MAX_PROB - cur_nmv_prob->bits[j];
  320. diff += (int)nmv_count->bits[j][1] * (pre_last_prob - cur_last_prob);
  321. }
  322. // class0_fp
  323. for (j = 0; j < CLASS0_SIZE; ++j) {
  324. for (k = 0; k < MV_FP_SIZE - 1; ++k) {
  325. diff += (int)nmv_count->class0_fp[j][k] *
  326. (pre_nmv_prob->class0_fp[j][k] - cur_nmv_prob->class0_fp[j][k]);
  327. }
  328. pre_last_prob = MAX_PROB - pre_nmv_prob->class0_fp[j][MV_FP_SIZE - 2];
  329. cur_last_prob = MAX_PROB - cur_nmv_prob->class0_fp[j][MV_FP_SIZE - 2];
  330. diff += (int)nmv_count->class0_fp[j][MV_FP_SIZE - 1] *
  331. (pre_last_prob - cur_last_prob);
  332. }
  333. // fp
  334. for (j = 0; j < MV_FP_SIZE - 1; ++j) {
  335. diff +=
  336. (int)nmv_count->fp[j] * (pre_nmv_prob->fp[j] - cur_nmv_prob->fp[j]);
  337. }
  338. pre_last_prob = MAX_PROB - pre_nmv_prob->fp[MV_FP_SIZE - 2];
  339. cur_last_prob = MAX_PROB - cur_nmv_prob->fp[MV_FP_SIZE - 2];
  340. diff +=
  341. (int)nmv_count->fp[MV_FP_SIZE - 1] * (pre_last_prob - cur_last_prob);
  342. // class0_hp
  343. diff += (int)nmv_count->class0_hp[0] *
  344. (pre_nmv_prob->class0_hp - cur_nmv_prob->class0_hp);
  345. pre_last_prob = MAX_PROB - pre_nmv_prob->class0_hp;
  346. cur_last_prob = MAX_PROB - cur_nmv_prob->class0_hp;
  347. diff += (int)nmv_count->class0_hp[1] * (pre_last_prob - cur_last_prob);
  348. // hp
  349. diff += (int)nmv_count->hp[0] * (pre_nmv_prob->hp - cur_nmv_prob->hp);
  350. pre_last_prob = MAX_PROB - pre_nmv_prob->hp;
  351. cur_last_prob = MAX_PROB - cur_nmv_prob->hp;
  352. diff += (int)nmv_count->hp[1] * (pre_last_prob - cur_last_prob);
  353. }
  354. return -diff;
  355. }
  356. // Test for whether to calculate metrics for the frame.
  357. static int is_psnr_calc_enabled(VP9_COMP *cpi) {
  358. VP9_COMMON *const cm = &cpi->common;
  359. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  360. return cpi->b_calculate_psnr && (oxcf->pass != 1) && cm->show_frame;
  361. }
  362. /* clang-format off */
  363. const Vp9LevelSpec vp9_level_defs[VP9_LEVELS] = {
  364. { LEVEL_1, 829440, 36864, 200, 400, 2, 1, 4, 8 },
  365. { LEVEL_1_1, 2764800, 73728, 800, 1000, 2, 1, 4, 8 },
  366. { LEVEL_2, 4608000, 122880, 1800, 1500, 2, 1, 4, 8 },
  367. { LEVEL_2_1, 9216000, 245760, 3600, 2800, 2, 2, 4, 8 },
  368. { LEVEL_3, 20736000, 552960, 7200, 6000, 2, 4, 4, 8 },
  369. { LEVEL_3_1, 36864000, 983040, 12000, 10000, 2, 4, 4, 8 },
  370. { LEVEL_4, 83558400, 2228224, 18000, 16000, 4, 4, 4, 8 },
  371. { LEVEL_4_1, 160432128, 2228224, 30000, 18000, 4, 4, 5, 6 },
  372. { LEVEL_5, 311951360, 8912896, 60000, 36000, 6, 8, 6, 4 },
  373. { LEVEL_5_1, 588251136, 8912896, 120000, 46000, 8, 8, 10, 4 },
  374. // TODO(huisu): update max_cpb_size for level 5_2 ~ 6_2 when
  375. // they are finalized (currently tentative).
  376. { LEVEL_5_2, 1176502272, 8912896, 180000, 90000, 8, 8, 10, 4 },
  377. { LEVEL_6, 1176502272, 35651584, 180000, 90000, 8, 16, 10, 4 },
  378. { LEVEL_6_1, 2353004544u, 35651584, 240000, 180000, 8, 16, 10, 4 },
  379. { LEVEL_6_2, 4706009088u, 35651584, 480000, 360000, 8, 16, 10, 4 },
  380. };
  381. /* clang-format on */
  382. static const char *level_fail_messages[TARGET_LEVEL_FAIL_IDS] =
  383. { "The average bit-rate is too high.",
  384. "The picture size is too large.",
  385. "The luma sample rate is too large.",
  386. "The CPB size is too large.",
  387. "The compression ratio is too small",
  388. "Too many column tiles are used.",
  389. "The alt-ref distance is too small.",
  390. "Too many reference buffers are used." };
  391. static INLINE void Scale2Ratio(VPX_SCALING mode, int *hr, int *hs) {
  392. switch (mode) {
  393. case NORMAL:
  394. *hr = 1;
  395. *hs = 1;
  396. break;
  397. case FOURFIVE:
  398. *hr = 4;
  399. *hs = 5;
  400. break;
  401. case THREEFIVE:
  402. *hr = 3;
  403. *hs = 5;
  404. break;
  405. case ONETWO:
  406. *hr = 1;
  407. *hs = 2;
  408. break;
  409. default:
  410. *hr = 1;
  411. *hs = 1;
  412. assert(0);
  413. break;
  414. }
  415. }
  416. // Mark all inactive blocks as active. Other segmentation features may be set
  417. // so memset cannot be used, instead only inactive blocks should be reset.
  418. static void suppress_active_map(VP9_COMP *cpi) {
  419. unsigned char *const seg_map = cpi->segmentation_map;
  420. if (cpi->active_map.enabled || cpi->active_map.update) {
  421. const int rows = cpi->common.mi_rows;
  422. const int cols = cpi->common.mi_cols;
  423. int i;
  424. for (i = 0; i < rows * cols; ++i)
  425. if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
  426. seg_map[i] = AM_SEGMENT_ID_ACTIVE;
  427. }
  428. }
  429. static void apply_active_map(VP9_COMP *cpi) {
  430. struct segmentation *const seg = &cpi->common.seg;
  431. unsigned char *const seg_map = cpi->segmentation_map;
  432. const unsigned char *const active_map = cpi->active_map.map;
  433. int i;
  434. assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
  435. if (frame_is_intra_only(&cpi->common)) {
  436. cpi->active_map.enabled = 0;
  437. cpi->active_map.update = 1;
  438. }
  439. if (cpi->active_map.update) {
  440. if (cpi->active_map.enabled) {
  441. for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
  442. if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
  443. vp9_enable_segmentation(seg);
  444. vp9_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
  445. vp9_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
  446. // Setting the data to -MAX_LOOP_FILTER will result in the computed loop
  447. // filter level being zero regardless of the value of seg->abs_delta.
  448. vp9_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF,
  449. -MAX_LOOP_FILTER);
  450. } else {
  451. vp9_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
  452. vp9_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
  453. if (seg->enabled) {
  454. seg->update_data = 1;
  455. seg->update_map = 1;
  456. }
  457. }
  458. cpi->active_map.update = 0;
  459. }
  460. }
  461. static void init_level_info(Vp9LevelInfo *level_info) {
  462. Vp9LevelStats *const level_stats = &level_info->level_stats;
  463. Vp9LevelSpec *const level_spec = &level_info->level_spec;
  464. memset(level_stats, 0, sizeof(*level_stats));
  465. memset(level_spec, 0, sizeof(*level_spec));
  466. level_spec->level = LEVEL_UNKNOWN;
  467. level_spec->min_altref_distance = INT_MAX;
  468. }
  469. VP9_LEVEL vp9_get_level(const Vp9LevelSpec *const level_spec) {
  470. int i;
  471. const Vp9LevelSpec *this_level;
  472. vpx_clear_system_state();
  473. for (i = 0; i < VP9_LEVELS; ++i) {
  474. this_level = &vp9_level_defs[i];
  475. if ((double)level_spec->max_luma_sample_rate >
  476. (double)this_level->max_luma_sample_rate *
  477. (1 + SAMPLE_RATE_GRACE_P) ||
  478. level_spec->max_luma_picture_size > this_level->max_luma_picture_size ||
  479. level_spec->average_bitrate > this_level->average_bitrate ||
  480. level_spec->max_cpb_size > this_level->max_cpb_size ||
  481. level_spec->compression_ratio < this_level->compression_ratio ||
  482. level_spec->max_col_tiles > this_level->max_col_tiles ||
  483. level_spec->min_altref_distance < this_level->min_altref_distance ||
  484. level_spec->max_ref_frame_buffers > this_level->max_ref_frame_buffers)
  485. continue;
  486. break;
  487. }
  488. return (i == VP9_LEVELS) ? LEVEL_UNKNOWN : vp9_level_defs[i].level;
  489. }
  490. int vp9_set_active_map(VP9_COMP *cpi, unsigned char *new_map_16x16, int rows,
  491. int cols) {
  492. if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
  493. unsigned char *const active_map_8x8 = cpi->active_map.map;
  494. const int mi_rows = cpi->common.mi_rows;
  495. const int mi_cols = cpi->common.mi_cols;
  496. cpi->active_map.update = 1;
  497. if (new_map_16x16) {
  498. int r, c;
  499. for (r = 0; r < mi_rows; ++r) {
  500. for (c = 0; c < mi_cols; ++c) {
  501. active_map_8x8[r * mi_cols + c] =
  502. new_map_16x16[(r >> 1) * cols + (c >> 1)]
  503. ? AM_SEGMENT_ID_ACTIVE
  504. : AM_SEGMENT_ID_INACTIVE;
  505. }
  506. }
  507. cpi->active_map.enabled = 1;
  508. } else {
  509. cpi->active_map.enabled = 0;
  510. }
  511. return 0;
  512. } else {
  513. return -1;
  514. }
  515. }
  516. int vp9_get_active_map(VP9_COMP *cpi, unsigned char *new_map_16x16, int rows,
  517. int cols) {
  518. if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
  519. new_map_16x16) {
  520. unsigned char *const seg_map_8x8 = cpi->segmentation_map;
  521. const int mi_rows = cpi->common.mi_rows;
  522. const int mi_cols = cpi->common.mi_cols;
  523. memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
  524. if (cpi->active_map.enabled) {
  525. int r, c;
  526. for (r = 0; r < mi_rows; ++r) {
  527. for (c = 0; c < mi_cols; ++c) {
  528. // Cyclic refresh segments are considered active despite not having
  529. // AM_SEGMENT_ID_ACTIVE
  530. new_map_16x16[(r >> 1) * cols + (c >> 1)] |=
  531. seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
  532. }
  533. }
  534. }
  535. return 0;
  536. } else {
  537. return -1;
  538. }
  539. }
  540. void vp9_set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
  541. MACROBLOCK *const mb = &cpi->td.mb;
  542. cpi->common.allow_high_precision_mv = allow_high_precision_mv;
  543. if (cpi->common.allow_high_precision_mv) {
  544. mb->mvcost = mb->nmvcost_hp;
  545. mb->mvsadcost = mb->nmvsadcost_hp;
  546. } else {
  547. mb->mvcost = mb->nmvcost;
  548. mb->mvsadcost = mb->nmvsadcost;
  549. }
  550. }
  551. static void setup_frame(VP9_COMP *cpi) {
  552. VP9_COMMON *const cm = &cpi->common;
  553. // Set up entropy context depending on frame type. The decoder mandates
  554. // the use of the default context, index 0, for keyframes and inter
  555. // frames where the error_resilient_mode or intra_only flag is set. For
  556. // other inter-frames the encoder currently uses only two contexts;
  557. // context 1 for ALTREF frames and context 0 for the others.
  558. if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
  559. vp9_setup_past_independence(cm);
  560. } else {
  561. if (!cpi->use_svc) cm->frame_context_idx = cpi->refresh_alt_ref_frame;
  562. }
  563. if (cm->frame_type == KEY_FRAME) {
  564. if (!is_two_pass_svc(cpi)) cpi->refresh_golden_frame = 1;
  565. cpi->refresh_alt_ref_frame = 1;
  566. vp9_zero(cpi->interp_filter_selected);
  567. } else {
  568. *cm->fc = cm->frame_contexts[cm->frame_context_idx];
  569. vp9_zero(cpi->interp_filter_selected[0]);
  570. }
  571. }
  572. static void vp9_enc_setup_mi(VP9_COMMON *cm) {
  573. int i;
  574. cm->mi = cm->mip + cm->mi_stride + 1;
  575. memset(cm->mip, 0, cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mip));
  576. cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
  577. // Clear top border row
  578. memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
  579. // Clear left border column
  580. for (i = 1; i < cm->mi_rows + 1; ++i)
  581. memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
  582. cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
  583. cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
  584. memset(cm->mi_grid_base, 0,
  585. cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mi_grid_base));
  586. }
  587. static int vp9_enc_alloc_mi(VP9_COMMON *cm, int mi_size) {
  588. cm->mip = vpx_calloc(mi_size, sizeof(*cm->mip));
  589. if (!cm->mip) return 1;
  590. cm->prev_mip = vpx_calloc(mi_size, sizeof(*cm->prev_mip));
  591. if (!cm->prev_mip) return 1;
  592. cm->mi_alloc_size = mi_size;
  593. cm->mi_grid_base = (MODE_INFO **)vpx_calloc(mi_size, sizeof(MODE_INFO *));
  594. if (!cm->mi_grid_base) return 1;
  595. cm->prev_mi_grid_base =
  596. (MODE_INFO **)vpx_calloc(mi_size, sizeof(MODE_INFO *));
  597. if (!cm->prev_mi_grid_base) return 1;
  598. return 0;
  599. }
  600. static void vp9_enc_free_mi(VP9_COMMON *cm) {
  601. vpx_free(cm->mip);
  602. cm->mip = NULL;
  603. vpx_free(cm->prev_mip);
  604. cm->prev_mip = NULL;
  605. vpx_free(cm->mi_grid_base);
  606. cm->mi_grid_base = NULL;
  607. vpx_free(cm->prev_mi_grid_base);
  608. cm->prev_mi_grid_base = NULL;
  609. }
  610. static void vp9_swap_mi_and_prev_mi(VP9_COMMON *cm) {
  611. // Current mip will be the prev_mip for the next frame.
  612. MODE_INFO **temp_base = cm->prev_mi_grid_base;
  613. MODE_INFO *temp = cm->prev_mip;
  614. cm->prev_mip = cm->mip;
  615. cm->mip = temp;
  616. // Update the upper left visible macroblock ptrs.
  617. cm->mi = cm->mip + cm->mi_stride + 1;
  618. cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
  619. cm->prev_mi_grid_base = cm->mi_grid_base;
  620. cm->mi_grid_base = temp_base;
  621. cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
  622. cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
  623. }
  624. void vp9_initialize_enc(void) {
  625. static volatile int init_done = 0;
  626. if (!init_done) {
  627. vp9_rtcd();
  628. vpx_dsp_rtcd();
  629. vpx_scale_rtcd();
  630. vp9_init_intra_predictors();
  631. vp9_init_me_luts();
  632. vp9_rc_init_minq_luts();
  633. vp9_entropy_mv_init();
  634. #if !CONFIG_REALTIME_ONLY
  635. vp9_temporal_filter_init();
  636. #endif
  637. init_done = 1;
  638. }
  639. }
  640. static void dealloc_compressor_data(VP9_COMP *cpi) {
  641. VP9_COMMON *const cm = &cpi->common;
  642. int i;
  643. vpx_free(cpi->mbmi_ext_base);
  644. cpi->mbmi_ext_base = NULL;
  645. vpx_free(cpi->tile_data);
  646. cpi->tile_data = NULL;
  647. vpx_free(cpi->segmentation_map);
  648. cpi->segmentation_map = NULL;
  649. vpx_free(cpi->coding_context.last_frame_seg_map_copy);
  650. cpi->coding_context.last_frame_seg_map_copy = NULL;
  651. vpx_free(cpi->nmvcosts[0]);
  652. vpx_free(cpi->nmvcosts[1]);
  653. cpi->nmvcosts[0] = NULL;
  654. cpi->nmvcosts[1] = NULL;
  655. vpx_free(cpi->nmvcosts_hp[0]);
  656. vpx_free(cpi->nmvcosts_hp[1]);
  657. cpi->nmvcosts_hp[0] = NULL;
  658. cpi->nmvcosts_hp[1] = NULL;
  659. vpx_free(cpi->nmvsadcosts[0]);
  660. vpx_free(cpi->nmvsadcosts[1]);
  661. cpi->nmvsadcosts[0] = NULL;
  662. cpi->nmvsadcosts[1] = NULL;
  663. vpx_free(cpi->nmvsadcosts_hp[0]);
  664. vpx_free(cpi->nmvsadcosts_hp[1]);
  665. cpi->nmvsadcosts_hp[0] = NULL;
  666. cpi->nmvsadcosts_hp[1] = NULL;
  667. vpx_free(cpi->skin_map);
  668. cpi->skin_map = NULL;
  669. vpx_free(cpi->prev_partition);
  670. cpi->prev_partition = NULL;
  671. vpx_free(cpi->prev_segment_id);
  672. cpi->prev_segment_id = NULL;
  673. vpx_free(cpi->prev_variance_low);
  674. cpi->prev_variance_low = NULL;
  675. vpx_free(cpi->copied_frame_cnt);
  676. cpi->copied_frame_cnt = NULL;
  677. vpx_free(cpi->content_state_sb_fd);
  678. cpi->content_state_sb_fd = NULL;
  679. vp9_cyclic_refresh_free(cpi->cyclic_refresh);
  680. cpi->cyclic_refresh = NULL;
  681. vpx_free(cpi->active_map.map);
  682. cpi->active_map.map = NULL;
  683. vpx_free(cpi->consec_zero_mv);
  684. cpi->consec_zero_mv = NULL;
  685. vp9_free_ref_frame_buffers(cm->buffer_pool);
  686. #if CONFIG_VP9_POSTPROC
  687. vp9_free_postproc_buffers(cm);
  688. #endif
  689. vp9_free_context_buffers(cm);
  690. vpx_free_frame_buffer(&cpi->last_frame_uf);
  691. vpx_free_frame_buffer(&cpi->scaled_source);
  692. vpx_free_frame_buffer(&cpi->scaled_last_source);
  693. vpx_free_frame_buffer(&cpi->alt_ref_buffer);
  694. #ifdef ENABLE_KF_DENOISE
  695. vpx_free_frame_buffer(&cpi->raw_unscaled_source);
  696. vpx_free_frame_buffer(&cpi->raw_scaled_source);
  697. #endif
  698. vp9_lookahead_destroy(cpi->lookahead);
  699. vpx_free(cpi->tile_tok[0][0]);
  700. cpi->tile_tok[0][0] = 0;
  701. vpx_free(cpi->tplist[0][0]);
  702. cpi->tplist[0][0] = NULL;
  703. vp9_free_pc_tree(&cpi->td);
  704. for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
  705. LAYER_CONTEXT *const lc = &cpi->svc.layer_context[i];
  706. vpx_free(lc->rc_twopass_stats_in.buf);
  707. lc->rc_twopass_stats_in.buf = NULL;
  708. lc->rc_twopass_stats_in.sz = 0;
  709. }
  710. if (cpi->source_diff_var != NULL) {
  711. vpx_free(cpi->source_diff_var);
  712. cpi->source_diff_var = NULL;
  713. }
  714. for (i = 0; i < MAX_LAG_BUFFERS; ++i) {
  715. vpx_free_frame_buffer(&cpi->svc.scaled_frames[i]);
  716. }
  717. memset(&cpi->svc.scaled_frames[0], 0,
  718. MAX_LAG_BUFFERS * sizeof(cpi->svc.scaled_frames[0]));
  719. vpx_free_frame_buffer(&cpi->svc.scaled_temp);
  720. memset(&cpi->svc.scaled_temp, 0, sizeof(cpi->svc.scaled_temp));
  721. vpx_free_frame_buffer(&cpi->svc.empty_frame.img);
  722. memset(&cpi->svc.empty_frame, 0, sizeof(cpi->svc.empty_frame));
  723. vp9_free_svc_cyclic_refresh(cpi);
  724. }
  725. static void save_coding_context(VP9_COMP *cpi) {
  726. CODING_CONTEXT *const cc = &cpi->coding_context;
  727. VP9_COMMON *cm = &cpi->common;
  728. // Stores a snapshot of key state variables which can subsequently be
  729. // restored with a call to vp9_restore_coding_context. These functions are
  730. // intended for use in a re-code loop in vp9_compress_frame where the
  731. // quantizer value is adjusted between loop iterations.
  732. vp9_copy(cc->nmvjointcost, cpi->td.mb.nmvjointcost);
  733. memcpy(cc->nmvcosts[0], cpi->nmvcosts[0],
  734. MV_VALS * sizeof(*cpi->nmvcosts[0]));
  735. memcpy(cc->nmvcosts[1], cpi->nmvcosts[1],
  736. MV_VALS * sizeof(*cpi->nmvcosts[1]));
  737. memcpy(cc->nmvcosts_hp[0], cpi->nmvcosts_hp[0],
  738. MV_VALS * sizeof(*cpi->nmvcosts_hp[0]));
  739. memcpy(cc->nmvcosts_hp[1], cpi->nmvcosts_hp[1],
  740. MV_VALS * sizeof(*cpi->nmvcosts_hp[1]));
  741. vp9_copy(cc->segment_pred_probs, cm->seg.pred_probs);
  742. memcpy(cpi->coding_context.last_frame_seg_map_copy, cm->last_frame_seg_map,
  743. (cm->mi_rows * cm->mi_cols));
  744. vp9_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
  745. vp9_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
  746. cc->fc = *cm->fc;
  747. }
  748. static void restore_coding_context(VP9_COMP *cpi) {
  749. CODING_CONTEXT *const cc = &cpi->coding_context;
  750. VP9_COMMON *cm = &cpi->common;
  751. // Restore key state variables to the snapshot state stored in the
  752. // previous call to vp9_save_coding_context.
  753. vp9_copy(cpi->td.mb.nmvjointcost, cc->nmvjointcost);
  754. memcpy(cpi->nmvcosts[0], cc->nmvcosts[0], MV_VALS * sizeof(*cc->nmvcosts[0]));
  755. memcpy(cpi->nmvcosts[1], cc->nmvcosts[1], MV_VALS * sizeof(*cc->nmvcosts[1]));
  756. memcpy(cpi->nmvcosts_hp[0], cc->nmvcosts_hp[0],
  757. MV_VALS * sizeof(*cc->nmvcosts_hp[0]));
  758. memcpy(cpi->nmvcosts_hp[1], cc->nmvcosts_hp[1],
  759. MV_VALS * sizeof(*cc->nmvcosts_hp[1]));
  760. vp9_copy(cm->seg.pred_probs, cc->segment_pred_probs);
  761. memcpy(cm->last_frame_seg_map, cpi->coding_context.last_frame_seg_map_copy,
  762. (cm->mi_rows * cm->mi_cols));
  763. vp9_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
  764. vp9_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
  765. *cm->fc = cc->fc;
  766. }
  767. #if !CONFIG_REALTIME_ONLY
  768. static void configure_static_seg_features(VP9_COMP *cpi) {
  769. VP9_COMMON *const cm = &cpi->common;
  770. const RATE_CONTROL *const rc = &cpi->rc;
  771. struct segmentation *const seg = &cm->seg;
  772. int high_q = (int)(rc->avg_q > 48.0);
  773. int qi_delta;
  774. // Disable and clear down for KF
  775. if (cm->frame_type == KEY_FRAME) {
  776. // Clear down the global segmentation map
  777. memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
  778. seg->update_map = 0;
  779. seg->update_data = 0;
  780. cpi->static_mb_pct = 0;
  781. // Disable segmentation
  782. vp9_disable_segmentation(seg);
  783. // Clear down the segment features.
  784. vp9_clearall_segfeatures(seg);
  785. } else if (cpi->refresh_alt_ref_frame) {
  786. // If this is an alt ref frame
  787. // Clear down the global segmentation map
  788. memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
  789. seg->update_map = 0;
  790. seg->update_data = 0;
  791. cpi->static_mb_pct = 0;
  792. // Disable segmentation and individual segment features by default
  793. vp9_disable_segmentation(seg);
  794. vp9_clearall_segfeatures(seg);
  795. // Scan frames from current to arf frame.
  796. // This function re-enables segmentation if appropriate.
  797. vp9_update_mbgraph_stats(cpi);
  798. // If segmentation was enabled set those features needed for the
  799. // arf itself.
  800. if (seg->enabled) {
  801. seg->update_map = 1;
  802. seg->update_data = 1;
  803. qi_delta =
  804. vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875, cm->bit_depth);
  805. vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
  806. vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
  807. vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
  808. vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
  809. // Where relevant assume segment data is delta data
  810. seg->abs_delta = SEGMENT_DELTADATA;
  811. }
  812. } else if (seg->enabled) {
  813. // All other frames if segmentation has been enabled
  814. // First normal frame in a valid gf or alt ref group
  815. if (rc->frames_since_golden == 0) {
  816. // Set up segment features for normal frames in an arf group
  817. if (rc->source_alt_ref_active) {
  818. seg->update_map = 0;
  819. seg->update_data = 1;
  820. seg->abs_delta = SEGMENT_DELTADATA;
  821. qi_delta =
  822. vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125, cm->bit_depth);
  823. vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
  824. vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
  825. vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
  826. vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
  827. // Segment coding disabled for compred testing
  828. if (high_q || (cpi->static_mb_pct == 100)) {
  829. vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
  830. vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
  831. vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
  832. }
  833. } else {
  834. // Disable segmentation and clear down features if alt ref
  835. // is not active for this group
  836. vp9_disable_segmentation(seg);
  837. memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
  838. seg->update_map = 0;
  839. seg->update_data = 0;
  840. vp9_clearall_segfeatures(seg);
  841. }
  842. } else if (rc->is_src_frame_alt_ref) {
  843. // Special case where we are coding over the top of a previous
  844. // alt ref frame.
  845. // Segment coding disabled for compred testing
  846. // Enable ref frame features for segment 0 as well
  847. vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
  848. vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
  849. // All mbs should use ALTREF_FRAME
  850. vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
  851. vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
  852. vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
  853. vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
  854. // Skip all MBs if high Q (0,0 mv and skip coeffs)
  855. if (high_q) {
  856. vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
  857. vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
  858. }
  859. // Enable data update
  860. seg->update_data = 1;
  861. } else {
  862. // All other frames.
  863. // No updates.. leave things as they are.
  864. seg->update_map = 0;
  865. seg->update_data = 0;
  866. }
  867. }
  868. }
  869. #endif // !CONFIG_REALTIME_ONLY
  870. static void update_reference_segmentation_map(VP9_COMP *cpi) {
  871. VP9_COMMON *const cm = &cpi->common;
  872. MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
  873. uint8_t *cache_ptr = cm->last_frame_seg_map;
  874. int row, col;
  875. for (row = 0; row < cm->mi_rows; row++) {
  876. MODE_INFO **mi_8x8 = mi_8x8_ptr;
  877. uint8_t *cache = cache_ptr;
  878. for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
  879. cache[0] = mi_8x8[0]->segment_id;
  880. mi_8x8_ptr += cm->mi_stride;
  881. cache_ptr += cm->mi_cols;
  882. }
  883. }
  884. static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
  885. VP9_COMMON *cm = &cpi->common;
  886. const VP9EncoderConfig *oxcf = &cpi->oxcf;
  887. if (!cpi->lookahead)
  888. cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height,
  889. cm->subsampling_x, cm->subsampling_y,
  890. #if CONFIG_VP9_HIGHBITDEPTH
  891. cm->use_highbitdepth,
  892. #endif
  893. oxcf->lag_in_frames);
  894. if (!cpi->lookahead)
  895. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  896. "Failed to allocate lag buffers");
  897. // TODO(agrange) Check if ARF is enabled and skip allocation if not.
  898. if (vpx_realloc_frame_buffer(&cpi->alt_ref_buffer, oxcf->width, oxcf->height,
  899. cm->subsampling_x, cm->subsampling_y,
  900. #if CONFIG_VP9_HIGHBITDEPTH
  901. cm->use_highbitdepth,
  902. #endif
  903. VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
  904. NULL, NULL, NULL))
  905. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  906. "Failed to allocate altref buffer");
  907. }
  908. static void alloc_util_frame_buffers(VP9_COMP *cpi) {
  909. VP9_COMMON *const cm = &cpi->common;
  910. if (vpx_realloc_frame_buffer(&cpi->last_frame_uf, cm->width, cm->height,
  911. cm->subsampling_x, cm->subsampling_y,
  912. #if CONFIG_VP9_HIGHBITDEPTH
  913. cm->use_highbitdepth,
  914. #endif
  915. VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
  916. NULL, NULL, NULL))
  917. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  918. "Failed to allocate last frame buffer");
  919. if (vpx_realloc_frame_buffer(&cpi->scaled_source, cm->width, cm->height,
  920. cm->subsampling_x, cm->subsampling_y,
  921. #if CONFIG_VP9_HIGHBITDEPTH
  922. cm->use_highbitdepth,
  923. #endif
  924. VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
  925. NULL, NULL, NULL))
  926. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  927. "Failed to allocate scaled source buffer");
  928. // For 1 pass cbr: allocate scaled_frame that may be used as an intermediate
  929. // buffer for a 2 stage down-sampling: two stages of 1:2 down-sampling for a
  930. // target of 1/4x1/4.
  931. if (is_one_pass_cbr_svc(cpi) && !cpi->svc.scaled_temp_is_alloc) {
  932. cpi->svc.scaled_temp_is_alloc = 1;
  933. if (vpx_realloc_frame_buffer(
  934. &cpi->svc.scaled_temp, cm->width >> 1, cm->height >> 1,
  935. cm->subsampling_x, cm->subsampling_y,
  936. #if CONFIG_VP9_HIGHBITDEPTH
  937. cm->use_highbitdepth,
  938. #endif
  939. VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
  940. vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
  941. "Failed to allocate scaled_frame for svc ");
  942. }
  943. if (vpx_realloc_frame_buffer(&cpi->scaled_last_source, cm->width, cm->height,
  944. cm->subsampling_x, cm->subsampling_y,
  945. #if CONFIG_VP9_HIGHBITDEPTH
  946. cm->use_highbitdepth,
  947. #endif
  948. VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
  949. NULL, NULL, NULL))
  950. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  951. "Failed to allocate scaled last source buffer");
  952. #ifdef ENABLE_KF_DENOISE
  953. if (vpx_realloc_frame_buffer(&cpi->raw_unscaled_source, cm->width, cm->height,
  954. cm->subsampling_x, cm->subsampling_y,
  955. #if CONFIG_VP9_HIGHBITDEPTH
  956. cm->use_highbitdepth,
  957. #endif
  958. VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
  959. NULL, NULL, NULL))
  960. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  961. "Failed to allocate unscaled raw source frame buffer");
  962. if (vpx_realloc_frame_buffer(&cpi->raw_scaled_source, cm->width, cm->height,
  963. cm->subsampling_x, cm->subsampling_y,
  964. #if CONFIG_VP9_HIGHBITDEPTH
  965. cm->use_highbitdepth,
  966. #endif
  967. VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
  968. NULL, NULL, NULL))
  969. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  970. "Failed to allocate scaled raw source frame buffer");
  971. #endif
  972. }
  973. static int alloc_context_buffers_ext(VP9_COMP *cpi) {
  974. VP9_COMMON *cm = &cpi->common;
  975. int mi_size = cm->mi_cols * cm->mi_rows;
  976. cpi->mbmi_ext_base = vpx_calloc(mi_size, sizeof(*cpi->mbmi_ext_base));
  977. if (!cpi->mbmi_ext_base) return 1;
  978. return 0;
  979. }
  980. static void alloc_compressor_data(VP9_COMP *cpi) {
  981. VP9_COMMON *cm = &cpi->common;
  982. int sb_rows;
  983. vp9_alloc_context_buffers(cm, cm->width, cm->height);
  984. alloc_context_buffers_ext(cpi);
  985. vpx_free(cpi->tile_tok[0][0]);
  986. {
  987. unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
  988. CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
  989. vpx_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
  990. }
  991. sb_rows = mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2;
  992. vpx_free(cpi->tplist[0][0]);
  993. CHECK_MEM_ERROR(
  994. cm, cpi->tplist[0][0],
  995. vpx_calloc(sb_rows * 4 * (1 << 6), sizeof(*cpi->tplist[0][0])));
  996. vp9_setup_pc_tree(&cpi->common, &cpi->td);
  997. }
  998. void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
  999. cpi->framerate = framerate < 0.1 ? 30 : framerate;
  1000. vp9_rc_update_framerate(cpi);
  1001. }
  1002. static void set_tile_limits(VP9_COMP *cpi) {
  1003. VP9_COMMON *const cm = &cpi->common;
  1004. int min_log2_tile_cols, max_log2_tile_cols;
  1005. vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
  1006. if (is_two_pass_svc(cpi) && (cpi->svc.encode_empty_frame_state == ENCODING ||
  1007. cpi->svc.number_spatial_layers > 1)) {
  1008. cm->log2_tile_cols = 0;
  1009. cm->log2_tile_rows = 0;
  1010. } else {
  1011. cm->log2_tile_cols =
  1012. clamp(cpi->oxcf.tile_columns, min_log2_tile_cols, max_log2_tile_cols);
  1013. cm->log2_tile_rows = cpi->oxcf.tile_rows;
  1014. }
  1015. if (cpi->oxcf.target_level == LEVEL_AUTO) {
  1016. const uint32_t pic_size = cpi->common.width * cpi->common.height;
  1017. const int level_tile_cols = log_tile_cols_from_picsize_level(pic_size);
  1018. if (cm->log2_tile_cols > level_tile_cols) {
  1019. cm->log2_tile_cols = VPXMAX(level_tile_cols, min_log2_tile_cols);
  1020. }
  1021. }
  1022. }
  1023. static void update_frame_size(VP9_COMP *cpi) {
  1024. VP9_COMMON *const cm = &cpi->common;
  1025. MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
  1026. vp9_set_mb_mi(cm, cm->width, cm->height);
  1027. vp9_init_context_buffers(cm);
  1028. vp9_init_macroblockd(cm, xd, NULL);
  1029. cpi->td.mb.mbmi_ext_base = cpi->mbmi_ext_base;
  1030. memset(cpi->mbmi_ext_base, 0,
  1031. cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
  1032. set_tile_limits(cpi);
  1033. if (is_two_pass_svc(cpi)) {
  1034. if (vpx_realloc_frame_buffer(&cpi->alt_ref_buffer, cm->width, cm->height,
  1035. cm->subsampling_x, cm->subsampling_y,
  1036. #if CONFIG_VP9_HIGHBITDEPTH
  1037. cm->use_highbitdepth,
  1038. #endif
  1039. VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
  1040. NULL, NULL, NULL))
  1041. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  1042. "Failed to reallocate alt_ref_buffer");
  1043. }
  1044. }
  1045. static void init_buffer_indices(VP9_COMP *cpi) {
  1046. cpi->lst_fb_idx = 0;
  1047. cpi->gld_fb_idx = 1;
  1048. cpi->alt_fb_idx = 2;
  1049. }
  1050. static void init_level_constraint(LevelConstraint *lc) {
  1051. lc->level_index = -1;
  1052. lc->max_cpb_size = INT_MAX;
  1053. lc->max_frame_size = INT_MAX;
  1054. lc->rc_config_updated = 0;
  1055. lc->fail_flag = 0;
  1056. }
  1057. static void set_level_constraint(LevelConstraint *ls, int8_t level_index) {
  1058. vpx_clear_system_state();
  1059. ls->level_index = level_index;
  1060. if (level_index >= 0) {
  1061. ls->max_cpb_size = vp9_level_defs[level_index].max_cpb_size * (double)1000;
  1062. }
  1063. }
  1064. static void init_config(struct VP9_COMP *cpi, VP9EncoderConfig *oxcf) {
  1065. VP9_COMMON *const cm = &cpi->common;
  1066. cpi->oxcf = *oxcf;
  1067. cpi->framerate = oxcf->init_framerate;
  1068. cm->profile = oxcf->profile;
  1069. cm->bit_depth = oxcf->bit_depth;
  1070. #if CONFIG_VP9_HIGHBITDEPTH
  1071. cm->use_highbitdepth = oxcf->use_highbitdepth;
  1072. #endif
  1073. cm->color_space = oxcf->color_space;
  1074. cm->color_range = oxcf->color_range;
  1075. cpi->target_level = oxcf->target_level;
  1076. cpi->keep_level_stats = oxcf->target_level != LEVEL_MAX;
  1077. set_level_constraint(&cpi->level_constraint,
  1078. get_level_index(cpi->target_level));
  1079. cm->width = oxcf->width;
  1080. cm->height = oxcf->height;
  1081. alloc_compressor_data(cpi);
  1082. cpi->svc.temporal_layering_mode = oxcf->temporal_layering_mode;
  1083. // Single thread case: use counts in common.
  1084. cpi->td.counts = &cm->counts;
  1085. // Spatial scalability.
  1086. cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
  1087. // Temporal scalability.
  1088. cpi->svc.number_temporal_layers = oxcf->ts_number_layers;
  1089. if ((cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ||
  1090. ((cpi->svc.number_temporal_layers > 1 ||
  1091. cpi->svc.number_spatial_layers > 1) &&
  1092. cpi->oxcf.pass != 1)) {
  1093. vp9_init_layer_context(cpi);
  1094. }
  1095. // change includes all joint functionality
  1096. vp9_change_config(cpi, oxcf);
  1097. cpi->static_mb_pct = 0;
  1098. cpi->ref_frame_flags = 0;
  1099. init_buffer_indices(cpi);
  1100. vp9_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
  1101. }
  1102. static void set_rc_buffer_sizes(RATE_CONTROL *rc,
  1103. const VP9EncoderConfig *oxcf) {
  1104. const int64_t bandwidth = oxcf->target_bandwidth;
  1105. const int64_t starting = oxcf->starting_buffer_level_ms;
  1106. const int64_t optimal = oxcf->optimal_buffer_level_ms;
  1107. const int64_t maximum = oxcf->maximum_buffer_size_ms;
  1108. rc->starting_buffer_level = starting * bandwidth / 1000;
  1109. rc->optimal_buffer_level =
  1110. (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
  1111. rc->maximum_buffer_size =
  1112. (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
  1113. }
  1114. #if CONFIG_VP9_HIGHBITDEPTH
  1115. #define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF) \
  1116. cpi->fn_ptr[BT].sdf = SDF; \
  1117. cpi->fn_ptr[BT].sdaf = SDAF; \
  1118. cpi->fn_ptr[BT].vf = VF; \
  1119. cpi->fn_ptr[BT].svf = SVF; \
  1120. cpi->fn_ptr[BT].svaf = SVAF; \
  1121. cpi->fn_ptr[BT].sdx4df = SDX4DF;
  1122. #define MAKE_BFP_SAD_WRAPPER(fnname) \
  1123. static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
  1124. int source_stride, \
  1125. const uint8_t *ref_ptr, int ref_stride) { \
  1126. return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
  1127. } \
  1128. static unsigned int fnname##_bits10( \
  1129. const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
  1130. int ref_stride) { \
  1131. return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
  1132. } \
  1133. static unsigned int fnname##_bits12( \
  1134. const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
  1135. int ref_stride) { \
  1136. return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
  1137. }
  1138. #define MAKE_BFP_SADAVG_WRAPPER(fnname) \
  1139. static unsigned int fnname##_bits8( \
  1140. const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
  1141. int ref_stride, const uint8_t *second_pred) { \
  1142. return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
  1143. } \
  1144. static unsigned int fnname##_bits10( \
  1145. const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
  1146. int ref_stride, const uint8_t *second_pred) { \
  1147. return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
  1148. 2; \
  1149. } \
  1150. static unsigned int fnname##_bits12( \
  1151. const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
  1152. int ref_stride, const uint8_t *second_pred) { \
  1153. return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
  1154. 4; \
  1155. }
  1156. #define MAKE_BFP_SAD4D_WRAPPER(fnname) \
  1157. static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
  1158. const uint8_t *const ref_ptr[], int ref_stride, \
  1159. unsigned int *sad_array) { \
  1160. fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
  1161. } \
  1162. static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
  1163. const uint8_t *const ref_ptr[], int ref_stride, \
  1164. unsigned int *sad_array) { \
  1165. int i; \
  1166. fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
  1167. for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
  1168. } \
  1169. static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
  1170. const uint8_t *const ref_ptr[], int ref_stride, \
  1171. unsigned int *sad_array) { \
  1172. int i; \
  1173. fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
  1174. for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
  1175. }
  1176. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x16)
  1177. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x16_avg)
  1178. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x16x4d)
  1179. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x32)
  1180. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x32_avg)
  1181. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x32x4d)
  1182. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad64x32)
  1183. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad64x32_avg)
  1184. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad64x32x4d)
  1185. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x64)
  1186. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x64_avg)
  1187. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x64x4d)
  1188. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x32)
  1189. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x32_avg)
  1190. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x32x4d)
  1191. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad64x64)
  1192. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad64x64_avg)
  1193. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad64x64x4d)
  1194. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x16)
  1195. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x16_avg)
  1196. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x16x4d)
  1197. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x8)
  1198. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x8_avg)
  1199. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x8x4d)
  1200. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x16)
  1201. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x16_avg)
  1202. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x16x4d)
  1203. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x8)
  1204. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x8_avg)
  1205. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x8x4d)
  1206. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x4)
  1207. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x4_avg)
  1208. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x4x4d)
  1209. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad4x8)
  1210. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad4x8_avg)
  1211. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad4x8x4d)
  1212. MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad4x4)
  1213. MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad4x4_avg)
  1214. MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad4x4x4d)
  1215. static void highbd_set_var_fns(VP9_COMP *const cpi) {
  1216. VP9_COMMON *const cm = &cpi->common;
  1217. if (cm->use_highbitdepth) {
  1218. switch (cm->bit_depth) {
  1219. case VPX_BITS_8:
  1220. HIGHBD_BFP(BLOCK_32X16, vpx_highbd_sad32x16_bits8,
  1221. vpx_highbd_sad32x16_avg_bits8, vpx_highbd_8_variance32x16,
  1222. vpx_highbd_8_sub_pixel_variance32x16,
  1223. vpx_highbd_8_sub_pixel_avg_variance32x16,
  1224. vpx_highbd_sad32x16x4d_bits8)
  1225. HIGHBD_BFP(BLOCK_16X32, vpx_highbd_sad16x32_bits8,
  1226. vpx_highbd_sad16x32_avg_bits8, vpx_highbd_8_variance16x32,
  1227. vpx_highbd_8_sub_pixel_variance16x32,
  1228. vpx_highbd_8_sub_pixel_avg_variance16x32,
  1229. vpx_highbd_sad16x32x4d_bits8)
  1230. HIGHBD_BFP(BLOCK_64X32, vpx_highbd_sad64x32_bits8,
  1231. vpx_highbd_sad64x32_avg_bits8, vpx_highbd_8_variance64x32,
  1232. vpx_highbd_8_sub_pixel_variance64x32,
  1233. vpx_highbd_8_sub_pixel_avg_variance64x32,
  1234. vpx_highbd_sad64x32x4d_bits8)
  1235. HIGHBD_BFP(BLOCK_32X64, vpx_highbd_sad32x64_bits8,
  1236. vpx_highbd_sad32x64_avg_bits8, vpx_highbd_8_variance32x64,
  1237. vpx_highbd_8_sub_pixel_variance32x64,
  1238. vpx_highbd_8_sub_pixel_avg_variance32x64,
  1239. vpx_highbd_sad32x64x4d_bits8)
  1240. HIGHBD_BFP(BLOCK_32X32, vpx_highbd_sad32x32_bits8,
  1241. vpx_highbd_sad32x32_avg_bits8, vpx_highbd_8_variance32x32,
  1242. vpx_highbd_8_sub_pixel_variance32x32,
  1243. vpx_highbd_8_sub_pixel_avg_variance32x32,
  1244. vpx_highbd_sad32x32x4d_bits8)
  1245. HIGHBD_BFP(BLOCK_64X64, vpx_highbd_sad64x64_bits8,
  1246. vpx_highbd_sad64x64_avg_bits8, vpx_highbd_8_variance64x64,
  1247. vpx_highbd_8_sub_pixel_variance64x64,
  1248. vpx_highbd_8_sub_pixel_avg_variance64x64,
  1249. vpx_highbd_sad64x64x4d_bits8)
  1250. HIGHBD_BFP(BLOCK_16X16, vpx_highbd_sad16x16_bits8,
  1251. vpx_highbd_sad16x16_avg_bits8, vpx_highbd_8_variance16x16,
  1252. vpx_highbd_8_sub_pixel_variance16x16,
  1253. vpx_highbd_8_sub_pixel_avg_variance16x16,
  1254. vpx_highbd_sad16x16x4d_bits8)
  1255. HIGHBD_BFP(BLOCK_16X8, vpx_highbd_sad16x8_bits8,
  1256. vpx_highbd_sad16x8_avg_bits8, vpx_highbd_8_variance16x8,
  1257. vpx_highbd_8_sub_pixel_variance16x8,
  1258. vpx_highbd_8_sub_pixel_avg_variance16x8,
  1259. vpx_highbd_sad16x8x4d_bits8)
  1260. HIGHBD_BFP(BLOCK_8X16, vpx_highbd_sad8x16_bits8,
  1261. vpx_highbd_sad8x16_avg_bits8, vpx_highbd_8_variance8x16,
  1262. vpx_highbd_8_sub_pixel_variance8x16,
  1263. vpx_highbd_8_sub_pixel_avg_variance8x16,
  1264. vpx_highbd_sad8x16x4d_bits8)
  1265. HIGHBD_BFP(
  1266. BLOCK_8X8, vpx_highbd_sad8x8_bits8, vpx_highbd_sad8x8_avg_bits8,
  1267. vpx_highbd_8_variance8x8, vpx_highbd_8_sub_pixel_variance8x8,
  1268. vpx_highbd_8_sub_pixel_avg_variance8x8, vpx_highbd_sad8x8x4d_bits8)
  1269. HIGHBD_BFP(
  1270. BLOCK_8X4, vpx_highbd_sad8x4_bits8, vpx_highbd_sad8x4_avg_bits8,
  1271. vpx_highbd_8_variance8x4, vpx_highbd_8_sub_pixel_variance8x4,
  1272. vpx_highbd_8_sub_pixel_avg_variance8x4, vpx_highbd_sad8x4x4d_bits8)
  1273. HIGHBD_BFP(
  1274. BLOCK_4X8, vpx_highbd_sad4x8_bits8, vpx_highbd_sad4x8_avg_bits8,
  1275. vpx_highbd_8_variance4x8, vpx_highbd_8_sub_pixel_variance4x8,
  1276. vpx_highbd_8_sub_pixel_avg_variance4x8, vpx_highbd_sad4x8x4d_bits8)
  1277. HIGHBD_BFP(
  1278. BLOCK_4X4, vpx_highbd_sad4x4_bits8, vpx_highbd_sad4x4_avg_bits8,
  1279. vpx_highbd_8_variance4x4, vpx_highbd_8_sub_pixel_variance4x4,
  1280. vpx_highbd_8_sub_pixel_avg_variance4x4, vpx_highbd_sad4x4x4d_bits8)
  1281. break;
  1282. case VPX_BITS_10:
  1283. HIGHBD_BFP(BLOCK_32X16, vpx_highbd_sad32x16_bits10,
  1284. vpx_highbd_sad32x16_avg_bits10, vpx_highbd_10_variance32x16,
  1285. vpx_highbd_10_sub_pixel_variance32x16,
  1286. vpx_highbd_10_sub_pixel_avg_variance32x16,
  1287. vpx_highbd_sad32x16x4d_bits10)
  1288. HIGHBD_BFP(BLOCK_16X32, vpx_highbd_sad16x32_bits10,
  1289. vpx_highbd_sad16x32_avg_bits10, vpx_highbd_10_variance16x32,
  1290. vpx_highbd_10_sub_pixel_variance16x32,
  1291. vpx_highbd_10_sub_pixel_avg_variance16x32,
  1292. vpx_highbd_sad16x32x4d_bits10)
  1293. HIGHBD_BFP(BLOCK_64X32, vpx_highbd_sad64x32_bits10,
  1294. vpx_highbd_sad64x32_avg_bits10, vpx_highbd_10_variance64x32,
  1295. vpx_highbd_10_sub_pixel_variance64x32,
  1296. vpx_highbd_10_sub_pixel_avg_variance64x32,
  1297. vpx_highbd_sad64x32x4d_bits10)
  1298. HIGHBD_BFP(BLOCK_32X64, vpx_highbd_sad32x64_bits10,
  1299. vpx_highbd_sad32x64_avg_bits10, vpx_highbd_10_variance32x64,
  1300. vpx_highbd_10_sub_pixel_variance32x64,
  1301. vpx_highbd_10_sub_pixel_avg_variance32x64,
  1302. vpx_highbd_sad32x64x4d_bits10)
  1303. HIGHBD_BFP(BLOCK_32X32, vpx_highbd_sad32x32_bits10,
  1304. vpx_highbd_sad32x32_avg_bits10, vpx_highbd_10_variance32x32,
  1305. vpx_highbd_10_sub_pixel_variance32x32,
  1306. vpx_highbd_10_sub_pixel_avg_variance32x32,
  1307. vpx_highbd_sad32x32x4d_bits10)
  1308. HIGHBD_BFP(BLOCK_64X64, vpx_highbd_sad64x64_bits10,
  1309. vpx_highbd_sad64x64_avg_bits10, vpx_highbd_10_variance64x64,
  1310. vpx_highbd_10_sub_pixel_variance64x64,
  1311. vpx_highbd_10_sub_pixel_avg_variance64x64,
  1312. vpx_highbd_sad64x64x4d_bits10)
  1313. HIGHBD_BFP(BLOCK_16X16, vpx_highbd_sad16x16_bits10,
  1314. vpx_highbd_sad16x16_avg_bits10, vpx_highbd_10_variance16x16,
  1315. vpx_highbd_10_sub_pixel_variance16x16,
  1316. vpx_highbd_10_sub_pixel_avg_variance16x16,
  1317. vpx_highbd_sad16x16x4d_bits10)
  1318. HIGHBD_BFP(BLOCK_16X8, vpx_highbd_sad16x8_bits10,
  1319. vpx_highbd_sad16x8_avg_bits10, vpx_highbd_10_variance16x8,
  1320. vpx_highbd_10_sub_pixel_variance16x8,
  1321. vpx_highbd_10_sub_pixel_avg_variance16x8,
  1322. vpx_highbd_sad16x8x4d_bits10)
  1323. HIGHBD_BFP(BLOCK_8X16, vpx_highbd_sad8x16_bits10,
  1324. vpx_highbd_sad8x16_avg_bits10, vpx_highbd_10_variance8x16,
  1325. vpx_highbd_10_sub_pixel_variance8x16,
  1326. vpx_highbd_10_sub_pixel_avg_variance8x16,
  1327. vpx_highbd_sad8x16x4d_bits10)
  1328. HIGHBD_BFP(BLOCK_8X8, vpx_highbd_sad8x8_bits10,
  1329. vpx_highbd_sad8x8_avg_bits10, vpx_highbd_10_variance8x8,
  1330. vpx_highbd_10_sub_pixel_variance8x8,
  1331. vpx_highbd_10_sub_pixel_avg_variance8x8,
  1332. vpx_highbd_sad8x8x4d_bits10)
  1333. HIGHBD_BFP(BLOCK_8X4, vpx_highbd_sad8x4_bits10,
  1334. vpx_highbd_sad8x4_avg_bits10, vpx_highbd_10_variance8x4,
  1335. vpx_highbd_10_sub_pixel_variance8x4,
  1336. vpx_highbd_10_sub_pixel_avg_variance8x4,
  1337. vpx_highbd_sad8x4x4d_bits10)
  1338. HIGHBD_BFP(BLOCK_4X8, vpx_highbd_sad4x8_bits10,
  1339. vpx_highbd_sad4x8_avg_bits10, vpx_highbd_10_variance4x8,
  1340. vpx_highbd_10_sub_pixel_variance4x8,
  1341. vpx_highbd_10_sub_pixel_avg_variance4x8,
  1342. vpx_highbd_sad4x8x4d_bits10)
  1343. HIGHBD_BFP(BLOCK_4X4, vpx_highbd_sad4x4_bits10,
  1344. vpx_highbd_sad4x4_avg_bits10, vpx_highbd_10_variance4x4,
  1345. vpx_highbd_10_sub_pixel_variance4x4,
  1346. vpx_highbd_10_sub_pixel_avg_variance4x4,
  1347. vpx_highbd_sad4x4x4d_bits10)
  1348. break;
  1349. case VPX_BITS_12:
  1350. HIGHBD_BFP(BLOCK_32X16, vpx_highbd_sad32x16_bits12,
  1351. vpx_highbd_sad32x16_avg_bits12, vpx_highbd_12_variance32x16,
  1352. vpx_highbd_12_sub_pixel_variance32x16,
  1353. vpx_highbd_12_sub_pixel_avg_variance32x16,
  1354. vpx_highbd_sad32x16x4d_bits12)
  1355. HIGHBD_BFP(BLOCK_16X32, vpx_highbd_sad16x32_bits12,
  1356. vpx_highbd_sad16x32_avg_bits12, vpx_highbd_12_variance16x32,
  1357. vpx_highbd_12_sub_pixel_variance16x32,
  1358. vpx_highbd_12_sub_pixel_avg_variance16x32,
  1359. vpx_highbd_sad16x32x4d_bits12)
  1360. HIGHBD_BFP(BLOCK_64X32, vpx_highbd_sad64x32_bits12,
  1361. vpx_highbd_sad64x32_avg_bits12, vpx_highbd_12_variance64x32,
  1362. vpx_highbd_12_sub_pixel_variance64x32,
  1363. vpx_highbd_12_sub_pixel_avg_variance64x32,
  1364. vpx_highbd_sad64x32x4d_bits12)
  1365. HIGHBD_BFP(BLOCK_32X64, vpx_highbd_sad32x64_bits12,
  1366. vpx_highbd_sad32x64_avg_bits12, vpx_highbd_12_variance32x64,
  1367. vpx_highbd_12_sub_pixel_variance32x64,
  1368. vpx_highbd_12_sub_pixel_avg_variance32x64,
  1369. vpx_highbd_sad32x64x4d_bits12)
  1370. HIGHBD_BFP(BLOCK_32X32, vpx_highbd_sad32x32_bits12,
  1371. vpx_highbd_sad32x32_avg_bits12, vpx_highbd_12_variance32x32,
  1372. vpx_highbd_12_sub_pixel_variance32x32,
  1373. vpx_highbd_12_sub_pixel_avg_variance32x32,
  1374. vpx_highbd_sad32x32x4d_bits12)
  1375. HIGHBD_BFP(BLOCK_64X64, vpx_highbd_sad64x64_bits12,
  1376. vpx_highbd_sad64x64_avg_bits12, vpx_highbd_12_variance64x64,
  1377. vpx_highbd_12_sub_pixel_variance64x64,
  1378. vpx_highbd_12_sub_pixel_avg_variance64x64,
  1379. vpx_highbd_sad64x64x4d_bits12)
  1380. HIGHBD_BFP(BLOCK_16X16, vpx_highbd_sad16x16_bits12,
  1381. vpx_highbd_sad16x16_avg_bits12, vpx_highbd_12_variance16x16,
  1382. vpx_highbd_12_sub_pixel_variance16x16,
  1383. vpx_highbd_12_sub_pixel_avg_variance16x16,
  1384. vpx_highbd_sad16x16x4d_bits12)
  1385. HIGHBD_BFP(BLOCK_16X8, vpx_highbd_sad16x8_bits12,
  1386. vpx_highbd_sad16x8_avg_bits12, vpx_highbd_12_variance16x8,
  1387. vpx_highbd_12_sub_pixel_variance16x8,
  1388. vpx_highbd_12_sub_pixel_avg_variance16x8,
  1389. vpx_highbd_sad16x8x4d_bits12)
  1390. HIGHBD_BFP(BLOCK_8X16, vpx_highbd_sad8x16_bits12,
  1391. vpx_highbd_sad8x16_avg_bits12, vpx_highbd_12_variance8x16,
  1392. vpx_highbd_12_sub_pixel_variance8x16,
  1393. vpx_highbd_12_sub_pixel_avg_variance8x16,
  1394. vpx_highbd_sad8x16x4d_bits12)
  1395. HIGHBD_BFP(BLOCK_8X8, vpx_highbd_sad8x8_bits12,
  1396. vpx_highbd_sad8x8_avg_bits12, vpx_highbd_12_variance8x8,
  1397. vpx_highbd_12_sub_pixel_variance8x8,
  1398. vpx_highbd_12_sub_pixel_avg_variance8x8,
  1399. vpx_highbd_sad8x8x4d_bits12)
  1400. HIGHBD_BFP(BLOCK_8X4, vpx_highbd_sad8x4_bits12,
  1401. vpx_highbd_sad8x4_avg_bits12, vpx_highbd_12_variance8x4,
  1402. vpx_highbd_12_sub_pixel_variance8x4,
  1403. vpx_highbd_12_sub_pixel_avg_variance8x4,
  1404. vpx_highbd_sad8x4x4d_bits12)
  1405. HIGHBD_BFP(BLOCK_4X8, vpx_highbd_sad4x8_bits12,
  1406. vpx_highbd_sad4x8_avg_bits12, vpx_highbd_12_variance4x8,
  1407. vpx_highbd_12_sub_pixel_variance4x8,
  1408. vpx_highbd_12_sub_pixel_avg_variance4x8,
  1409. vpx_highbd_sad4x8x4d_bits12)
  1410. HIGHBD_BFP(BLOCK_4X4, vpx_highbd_sad4x4_bits12,
  1411. vpx_highbd_sad4x4_avg_bits12, vpx_highbd_12_variance4x4,
  1412. vpx_highbd_12_sub_pixel_variance4x4,
  1413. vpx_highbd_12_sub_pixel_avg_variance4x4,
  1414. vpx_highbd_sad4x4x4d_bits12)
  1415. break;
  1416. default:
  1417. assert(0 &&
  1418. "cm->bit_depth should be VPX_BITS_8, "
  1419. "VPX_BITS_10 or VPX_BITS_12");
  1420. }
  1421. }
  1422. }
  1423. #endif // CONFIG_VP9_HIGHBITDEPTH
  1424. static void realloc_segmentation_maps(VP9_COMP *cpi) {
  1425. VP9_COMMON *const cm = &cpi->common;
  1426. // Create the encoder segmentation map and set all entries to 0
  1427. vpx_free(cpi->segmentation_map);
  1428. CHECK_MEM_ERROR(cm, cpi->segmentation_map,
  1429. vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
  1430. // Create a map used for cyclic background refresh.
  1431. if (cpi->cyclic_refresh) vp9_cyclic_refresh_free(cpi->cyclic_refresh);
  1432. CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
  1433. vp9_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
  1434. // Create a map used to mark inactive areas.
  1435. vpx_free(cpi->active_map.map);
  1436. CHECK_MEM_ERROR(cm, cpi->active_map.map,
  1437. vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
  1438. // And a place holder structure is the coding context
  1439. // for use if we want to save and restore it
  1440. vpx_free(cpi->coding_context.last_frame_seg_map_copy);
  1441. CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy,
  1442. vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
  1443. }
  1444. static void alloc_copy_partition_data(VP9_COMP *cpi) {
  1445. VP9_COMMON *const cm = &cpi->common;
  1446. if (cpi->prev_partition == NULL) {
  1447. CHECK_MEM_ERROR(cm, cpi->prev_partition,
  1448. (BLOCK_SIZE *)vpx_calloc(cm->mi_stride * cm->mi_rows,
  1449. sizeof(*cpi->prev_partition)));
  1450. }
  1451. if (cpi->prev_segment_id == NULL) {
  1452. CHECK_MEM_ERROR(
  1453. cm, cpi->prev_segment_id,
  1454. (int8_t *)vpx_calloc((cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1),
  1455. sizeof(*cpi->prev_segment_id)));
  1456. }
  1457. if (cpi->prev_variance_low == NULL) {
  1458. CHECK_MEM_ERROR(cm, cpi->prev_variance_low,
  1459. (uint8_t *)vpx_calloc(
  1460. (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1) * 25,
  1461. sizeof(*cpi->prev_variance_low)));
  1462. }
  1463. if (cpi->copied_frame_cnt == NULL) {
  1464. CHECK_MEM_ERROR(
  1465. cm, cpi->copied_frame_cnt,
  1466. (uint8_t *)vpx_calloc((cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1),
  1467. sizeof(*cpi->copied_frame_cnt)));
  1468. }
  1469. }
  1470. void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
  1471. VP9_COMMON *const cm = &cpi->common;
  1472. RATE_CONTROL *const rc = &cpi->rc;
  1473. int last_w = cpi->oxcf.width;
  1474. int last_h = cpi->oxcf.height;
  1475. if (cm->profile != oxcf->profile) cm->profile = oxcf->profile;
  1476. cm->bit_depth = oxcf->bit_depth;
  1477. cm->color_space = oxcf->color_space;
  1478. cm->color_range = oxcf->color_range;
  1479. cpi->target_level = oxcf->target_level;
  1480. cpi->keep_level_stats = oxcf->target_level != LEVEL_MAX;
  1481. set_level_constraint(&cpi->level_constraint,
  1482. get_level_index(cpi->target_level));
  1483. if (cm->profile <= PROFILE_1)
  1484. assert(cm->bit_depth == VPX_BITS_8);
  1485. else
  1486. assert(cm->bit_depth > VPX_BITS_8);
  1487. cpi->oxcf = *oxcf;
  1488. #if CONFIG_VP9_HIGHBITDEPTH
  1489. cpi->td.mb.e_mbd.bd = (int)cm->bit_depth;
  1490. #endif // CONFIG_VP9_HIGHBITDEPTH
  1491. if ((oxcf->pass == 0) && (oxcf->rc_mode == VPX_Q)) {
  1492. rc->baseline_gf_interval = FIXED_GF_INTERVAL;
  1493. } else {
  1494. rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
  1495. }
  1496. cpi->refresh_golden_frame = 0;
  1497. cpi->refresh_last_frame = 1;
  1498. cm->refresh_frame_context = 1;
  1499. cm->reset_frame_context = 0;
  1500. vp9_reset_segment_features(&cm->seg);
  1501. vp9_set_high_precision_mv(cpi, 0);
  1502. {
  1503. int i;
  1504. for (i = 0; i < MAX_SEGMENTS; i++)
  1505. cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
  1506. }
  1507. cpi->encode_breakout = cpi->oxcf.encode_breakout;
  1508. set_rc_buffer_sizes(rc, &cpi->oxcf);
  1509. // Under a configuration change, where maximum_buffer_size may change,
  1510. // keep buffer level clipped to the maximum allowed buffer size.
  1511. rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size);
  1512. rc->buffer_level = VPXMIN(rc->buffer_level, rc->maximum_buffer_size);
  1513. // Set up frame rate and related parameters rate control values.
  1514. vp9_new_framerate(cpi, cpi->framerate);
  1515. // Set absolute upper and lower quality limits
  1516. rc->worst_quality = cpi->oxcf.worst_allowed_q;
  1517. rc->best_quality = cpi->oxcf.best_allowed_q;
  1518. cm->interp_filter = cpi->sf.default_interp_filter;
  1519. if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
  1520. cm->render_width = cpi->oxcf.render_width;
  1521. cm->render_height = cpi->oxcf.render_height;
  1522. } else {
  1523. cm->render_width = cpi->oxcf.width;
  1524. cm->render_height = cpi->oxcf.height;
  1525. }
  1526. if (last_w != cpi->oxcf.width || last_h != cpi->oxcf.height) {
  1527. cm->width = cpi->oxcf.width;
  1528. cm->height = cpi->oxcf.height;
  1529. cpi->external_resize = 1;
  1530. }
  1531. if (cpi->initial_width) {
  1532. int new_mi_size = 0;
  1533. vp9_set_mb_mi(cm, cm->width, cm->height);
  1534. new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
  1535. if (cm->mi_alloc_size < new_mi_size) {
  1536. vp9_free_context_buffers(cm);
  1537. alloc_compressor_data(cpi);
  1538. realloc_segmentation_maps(cpi);
  1539. cpi->initial_width = cpi->initial_height = 0;
  1540. cpi->external_resize = 0;
  1541. } else if (cm->mi_alloc_size == new_mi_size &&
  1542. (cpi->oxcf.width > last_w || cpi->oxcf.height > last_h)) {
  1543. vp9_alloc_loop_filter(cm);
  1544. }
  1545. }
  1546. if (cm->current_video_frame == 0 || last_w != cpi->oxcf.width ||
  1547. last_h != cpi->oxcf.height)
  1548. update_frame_size(cpi);
  1549. if (last_w != cpi->oxcf.width || last_h != cpi->oxcf.height) {
  1550. memset(cpi->consec_zero_mv, 0,
  1551. cm->mi_rows * cm->mi_cols * sizeof(*cpi->consec_zero_mv));
  1552. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
  1553. vp9_cyclic_refresh_reset_resize(cpi);
  1554. }
  1555. if ((cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ||
  1556. ((cpi->svc.number_temporal_layers > 1 ||
  1557. cpi->svc.number_spatial_layers > 1) &&
  1558. cpi->oxcf.pass != 1)) {
  1559. vp9_update_layer_context_change_config(cpi,
  1560. (int)cpi->oxcf.target_bandwidth);
  1561. }
  1562. cpi->alt_ref_source = NULL;
  1563. rc->is_src_frame_alt_ref = 0;
  1564. #if 0
  1565. // Experimental RD Code
  1566. cpi->frame_distortion = 0;
  1567. cpi->last_frame_distortion = 0;
  1568. #endif
  1569. set_tile_limits(cpi);
  1570. cpi->ext_refresh_frame_flags_pending = 0;
  1571. cpi->ext_refresh_frame_context_pending = 0;
  1572. #if CONFIG_VP9_HIGHBITDEPTH
  1573. highbd_set_var_fns(cpi);
  1574. #endif
  1575. vp9_set_row_mt(cpi);
  1576. }
  1577. #ifndef M_LOG2_E
  1578. #define M_LOG2_E 0.693147180559945309417
  1579. #endif
  1580. #define log2f(x) (log(x) / (float)M_LOG2_E)
  1581. /***********************************************************************
  1582. * Read before modifying 'cal_nmvjointsadcost' or 'cal_nmvsadcosts' *
  1583. ***********************************************************************
  1584. * The following 2 functions ('cal_nmvjointsadcost' and *
  1585. * 'cal_nmvsadcosts') are used to calculate cost lookup tables *
  1586. * used by 'vp9_diamond_search_sad'. The C implementation of the *
  1587. * function is generic, but the AVX intrinsics optimised version *
  1588. * relies on the following properties of the computed tables: *
  1589. * For cal_nmvjointsadcost: *
  1590. * - mvjointsadcost[1] == mvjointsadcost[2] == mvjointsadcost[3] *
  1591. * For cal_nmvsadcosts: *
  1592. * - For all i: mvsadcost[0][i] == mvsadcost[1][i] *
  1593. * (Equal costs for both components) *
  1594. * - For all i: mvsadcost[0][i] == mvsadcost[0][-i] *
  1595. * (Cost function is even) *
  1596. * If these do not hold, then the AVX optimised version of the *
  1597. * 'vp9_diamond_search_sad' function cannot be used as it is, in which *
  1598. * case you can revert to using the C function instead. *
  1599. ***********************************************************************/
  1600. static void cal_nmvjointsadcost(int *mvjointsadcost) {
  1601. /*********************************************************************
  1602. * Warning: Read the comments above before modifying this function *
  1603. *********************************************************************/
  1604. mvjointsadcost[0] = 600;
  1605. mvjointsadcost[1] = 300;
  1606. mvjointsadcost[2] = 300;
  1607. mvjointsadcost[3] = 300;
  1608. }
  1609. static void cal_nmvsadcosts(int *mvsadcost[2]) {
  1610. /*********************************************************************
  1611. * Warning: Read the comments above before modifying this function *
  1612. *********************************************************************/
  1613. int i = 1;
  1614. mvsadcost[0][0] = 0;
  1615. mvsadcost[1][0] = 0;
  1616. do {
  1617. double z = 256 * (2 * (log2f(8 * i) + .6));
  1618. mvsadcost[0][i] = (int)z;
  1619. mvsadcost[1][i] = (int)z;
  1620. mvsadcost[0][-i] = (int)z;
  1621. mvsadcost[1][-i] = (int)z;
  1622. } while (++i <= MV_MAX);
  1623. }
  1624. static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
  1625. int i = 1;
  1626. mvsadcost[0][0] = 0;
  1627. mvsadcost[1][0] = 0;
  1628. do {
  1629. double z = 256 * (2 * (log2f(8 * i) + .6));
  1630. mvsadcost[0][i] = (int)z;
  1631. mvsadcost[1][i] = (int)z;
  1632. mvsadcost[0][-i] = (int)z;
  1633. mvsadcost[1][-i] = (int)z;
  1634. } while (++i <= MV_MAX);
  1635. }
  1636. VP9_COMP *vp9_create_compressor(VP9EncoderConfig *oxcf,
  1637. BufferPool *const pool) {
  1638. unsigned int i;
  1639. VP9_COMP *volatile const cpi = vpx_memalign(32, sizeof(VP9_COMP));
  1640. VP9_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
  1641. if (!cm) return NULL;
  1642. vp9_zero(*cpi);
  1643. if (setjmp(cm->error.jmp)) {
  1644. cm->error.setjmp = 0;
  1645. vp9_remove_compressor(cpi);
  1646. return 0;
  1647. }
  1648. cm->error.setjmp = 1;
  1649. cm->alloc_mi = vp9_enc_alloc_mi;
  1650. cm->free_mi = vp9_enc_free_mi;
  1651. cm->setup_mi = vp9_enc_setup_mi;
  1652. CHECK_MEM_ERROR(cm, cm->fc, (FRAME_CONTEXT *)vpx_calloc(1, sizeof(*cm->fc)));
  1653. CHECK_MEM_ERROR(
  1654. cm, cm->frame_contexts,
  1655. (FRAME_CONTEXT *)vpx_calloc(FRAME_CONTEXTS, sizeof(*cm->frame_contexts)));
  1656. cpi->use_svc = 0;
  1657. cpi->resize_state = ORIG;
  1658. cpi->external_resize = 0;
  1659. cpi->resize_avg_qp = 0;
  1660. cpi->resize_buffer_underflow = 0;
  1661. cpi->use_skin_detection = 0;
  1662. cpi->common.buffer_pool = pool;
  1663. cpi->force_update_segmentation = 0;
  1664. init_config(cpi, oxcf);
  1665. vp9_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
  1666. cm->current_video_frame = 0;
  1667. cpi->partition_search_skippable_frame = 0;
  1668. cpi->tile_data = NULL;
  1669. realloc_segmentation_maps(cpi);
  1670. CHECK_MEM_ERROR(cm, cpi->skin_map, vpx_calloc(cm->mi_rows * cm->mi_cols,
  1671. sizeof(cpi->skin_map[0])));
  1672. CHECK_MEM_ERROR(cm, cpi->alt_ref_aq, vp9_alt_ref_aq_create());
  1673. CHECK_MEM_ERROR(
  1674. cm, cpi->consec_zero_mv,
  1675. vpx_calloc(cm->mi_rows * cm->mi_cols, sizeof(*cpi->consec_zero_mv)));
  1676. CHECK_MEM_ERROR(cm, cpi->nmvcosts[0],
  1677. vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[0])));
  1678. CHECK_MEM_ERROR(cm, cpi->nmvcosts[1],
  1679. vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[1])));
  1680. CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[0],
  1681. vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[0])));
  1682. CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[1],
  1683. vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[1])));
  1684. CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[0],
  1685. vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[0])));
  1686. CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[1],
  1687. vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[1])));
  1688. CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[0],
  1689. vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[0])));
  1690. CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[1],
  1691. vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[1])));
  1692. for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
  1693. i++) {
  1694. CHECK_MEM_ERROR(
  1695. cm, cpi->mbgraph_stats[i].mb_stats,
  1696. vpx_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
  1697. }
  1698. #if CONFIG_FP_MB_STATS
  1699. cpi->use_fp_mb_stats = 0;
  1700. if (cpi->use_fp_mb_stats) {
  1701. // a place holder used to store the first pass mb stats in the first pass
  1702. CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
  1703. vpx_calloc(cm->MBs * sizeof(uint8_t), 1));
  1704. } else {
  1705. cpi->twopass.frame_mb_stats_buf = NULL;
  1706. }
  1707. #endif
  1708. cpi->refresh_alt_ref_frame = 0;
  1709. cpi->multi_arf_last_grp_enabled = 0;
  1710. cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
  1711. init_level_info(&cpi->level_info);
  1712. init_level_constraint(&cpi->level_constraint);
  1713. #if CONFIG_INTERNAL_STATS
  1714. cpi->b_calculate_blockiness = 1;
  1715. cpi->b_calculate_consistency = 1;
  1716. cpi->total_inconsistency = 0;
  1717. cpi->psnr.worst = 100.0;
  1718. cpi->worst_ssim = 100.0;
  1719. cpi->count = 0;
  1720. cpi->bytes = 0;
  1721. if (cpi->b_calculate_psnr) {
  1722. cpi->total_sq_error = 0;
  1723. cpi->total_samples = 0;
  1724. cpi->totalp_sq_error = 0;
  1725. cpi->totalp_samples = 0;
  1726. cpi->tot_recode_hits = 0;
  1727. cpi->summed_quality = 0;
  1728. cpi->summed_weights = 0;
  1729. cpi->summedp_quality = 0;
  1730. cpi->summedp_weights = 0;
  1731. }
  1732. cpi->fastssim.worst = 100.0;
  1733. cpi->psnrhvs.worst = 100.0;
  1734. if (cpi->b_calculate_blockiness) {
  1735. cpi->total_blockiness = 0;
  1736. cpi->worst_blockiness = 0.0;
  1737. }
  1738. if (cpi->b_calculate_consistency) {
  1739. CHECK_MEM_ERROR(cm, cpi->ssim_vars,
  1740. vpx_malloc(sizeof(*cpi->ssim_vars) * 4 *
  1741. cpi->common.mi_rows * cpi->common.mi_cols));
  1742. cpi->worst_consistency = 100.0;
  1743. }
  1744. #endif
  1745. cpi->first_time_stamp_ever = INT64_MAX;
  1746. /*********************************************************************
  1747. * Warning: Read the comments around 'cal_nmvjointsadcost' and *
  1748. * 'cal_nmvsadcosts' before modifying how these tables are computed. *
  1749. *********************************************************************/
  1750. cal_nmvjointsadcost(cpi->td.mb.nmvjointsadcost);
  1751. cpi->td.mb.nmvcost[0] = &cpi->nmvcosts[0][MV_MAX];
  1752. cpi->td.mb.nmvcost[1] = &cpi->nmvcosts[1][MV_MAX];
  1753. cpi->td.mb.nmvsadcost[0] = &cpi->nmvsadcosts[0][MV_MAX];
  1754. cpi->td.mb.nmvsadcost[1] = &cpi->nmvsadcosts[1][MV_MAX];
  1755. cal_nmvsadcosts(cpi->td.mb.nmvsadcost);
  1756. cpi->td.mb.nmvcost_hp[0] = &cpi->nmvcosts_hp[0][MV_MAX];
  1757. cpi->td.mb.nmvcost_hp[1] = &cpi->nmvcosts_hp[1][MV_MAX];
  1758. cpi->td.mb.nmvsadcost_hp[0] = &cpi->nmvsadcosts_hp[0][MV_MAX];
  1759. cpi->td.mb.nmvsadcost_hp[1] = &cpi->nmvsadcosts_hp[1][MV_MAX];
  1760. cal_nmvsadcosts_hp(cpi->td.mb.nmvsadcost_hp);
  1761. #if CONFIG_VP9_TEMPORAL_DENOISING
  1762. #ifdef OUTPUT_YUV_DENOISED
  1763. yuv_denoised_file = fopen("denoised.yuv", "ab");
  1764. #endif
  1765. #endif
  1766. #ifdef OUTPUT_YUV_SKINMAP
  1767. yuv_skinmap_file = fopen("skinmap.yuv", "wb");
  1768. #endif
  1769. #ifdef OUTPUT_YUV_REC
  1770. yuv_rec_file = fopen("rec.yuv", "wb");
  1771. #endif
  1772. #if 0
  1773. framepsnr = fopen("framepsnr.stt", "a");
  1774. kf_list = fopen("kf_list.stt", "w");
  1775. #endif
  1776. cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
  1777. #if !CONFIG_REALTIME_ONLY
  1778. if (oxcf->pass == 1) {
  1779. vp9_init_first_pass(cpi);
  1780. } else if (oxcf->pass == 2) {
  1781. const size_t packet_sz = sizeof(FIRSTPASS_STATS);
  1782. const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
  1783. if (cpi->svc.number_spatial_layers > 1 ||
  1784. cpi->svc.number_temporal_layers > 1) {
  1785. FIRSTPASS_STATS *const stats = oxcf->two_pass_stats_in.buf;
  1786. FIRSTPASS_STATS *stats_copy[VPX_SS_MAX_LAYERS] = { 0 };
  1787. int i;
  1788. for (i = 0; i < oxcf->ss_number_layers; ++i) {
  1789. FIRSTPASS_STATS *const last_packet_for_layer =
  1790. &stats[packets - oxcf->ss_number_layers + i];
  1791. const int layer_id = (int)last_packet_for_layer->spatial_layer_id;
  1792. const int packets_in_layer = (int)last_packet_for_layer->count + 1;
  1793. if (layer_id >= 0 && layer_id < oxcf->ss_number_layers) {
  1794. LAYER_CONTEXT *const lc = &cpi->svc.layer_context[layer_id];
  1795. vpx_free(lc->rc_twopass_stats_in.buf);
  1796. lc->rc_twopass_stats_in.sz = packets_in_layer * packet_sz;
  1797. CHECK_MEM_ERROR(cm, lc->rc_twopass_stats_in.buf,
  1798. vpx_malloc(lc->rc_twopass_stats_in.sz));
  1799. lc->twopass.stats_in_start = lc->rc_twopass_stats_in.buf;
  1800. lc->twopass.stats_in = lc->twopass.stats_in_start;
  1801. lc->twopass.stats_in_end =
  1802. lc->twopass.stats_in_start + packets_in_layer - 1;
  1803. stats_copy[layer_id] = lc->rc_twopass_stats_in.buf;
  1804. }
  1805. }
  1806. for (i = 0; i < packets; ++i) {
  1807. const int layer_id = (int)stats[i].spatial_layer_id;
  1808. if (layer_id >= 0 && layer_id < oxcf->ss_number_layers &&
  1809. stats_copy[layer_id] != NULL) {
  1810. *stats_copy[layer_id] = stats[i];
  1811. ++stats_copy[layer_id];
  1812. }
  1813. }
  1814. vp9_init_second_pass_spatial_svc(cpi);
  1815. } else {
  1816. #if CONFIG_FP_MB_STATS
  1817. if (cpi->use_fp_mb_stats) {
  1818. const size_t psz = cpi->common.MBs * sizeof(uint8_t);
  1819. const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
  1820. cpi->twopass.firstpass_mb_stats.mb_stats_start =
  1821. oxcf->firstpass_mb_stats_in.buf;
  1822. cpi->twopass.firstpass_mb_stats.mb_stats_end =
  1823. cpi->twopass.firstpass_mb_stats.mb_stats_start +
  1824. (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
  1825. }
  1826. #endif
  1827. cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
  1828. cpi->twopass.stats_in = cpi->twopass.stats_in_start;
  1829. cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
  1830. vp9_init_second_pass(cpi);
  1831. }
  1832. }
  1833. #endif // !CONFIG_REALTIME_ONLY
  1834. vp9_set_speed_features_framesize_independent(cpi);
  1835. vp9_set_speed_features_framesize_dependent(cpi);
  1836. // Allocate memory to store variances for a frame.
  1837. CHECK_MEM_ERROR(cm, cpi->source_diff_var, vpx_calloc(cm->MBs, sizeof(diff)));
  1838. cpi->source_var_thresh = 0;
  1839. cpi->frames_till_next_var_check = 0;
  1840. #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF) \
  1841. cpi->fn_ptr[BT].sdf = SDF; \
  1842. cpi->fn_ptr[BT].sdaf = SDAF; \
  1843. cpi->fn_ptr[BT].vf = VF; \
  1844. cpi->fn_ptr[BT].svf = SVF; \
  1845. cpi->fn_ptr[BT].svaf = SVAF; \
  1846. cpi->fn_ptr[BT].sdx4df = SDX4DF;
  1847. BFP(BLOCK_32X16, vpx_sad32x16, vpx_sad32x16_avg, vpx_variance32x16,
  1848. vpx_sub_pixel_variance32x16, vpx_sub_pixel_avg_variance32x16,
  1849. vpx_sad32x16x4d)
  1850. BFP(BLOCK_16X32, vpx_sad16x32, vpx_sad16x32_avg, vpx_variance16x32,
  1851. vpx_sub_pixel_variance16x32, vpx_sub_pixel_avg_variance16x32,
  1852. vpx_sad16x32x4d)
  1853. BFP(BLOCK_64X32, vpx_sad64x32, vpx_sad64x32_avg, vpx_variance64x32,
  1854. vpx_sub_pixel_variance64x32, vpx_sub_pixel_avg_variance64x32,
  1855. vpx_sad64x32x4d)
  1856. BFP(BLOCK_32X64, vpx_sad32x64, vpx_sad32x64_avg, vpx_variance32x64,
  1857. vpx_sub_pixel_variance32x64, vpx_sub_pixel_avg_variance32x64,
  1858. vpx_sad32x64x4d)
  1859. BFP(BLOCK_32X32, vpx_sad32x32, vpx_sad32x32_avg, vpx_variance32x32,
  1860. vpx_sub_pixel_variance32x32, vpx_sub_pixel_avg_variance32x32,
  1861. vpx_sad32x32x4d)
  1862. BFP(BLOCK_64X64, vpx_sad64x64, vpx_sad64x64_avg, vpx_variance64x64,
  1863. vpx_sub_pixel_variance64x64, vpx_sub_pixel_avg_variance64x64,
  1864. vpx_sad64x64x4d)
  1865. BFP(BLOCK_16X16, vpx_sad16x16, vpx_sad16x16_avg, vpx_variance16x16,
  1866. vpx_sub_pixel_variance16x16, vpx_sub_pixel_avg_variance16x16,
  1867. vpx_sad16x16x4d)
  1868. BFP(BLOCK_16X8, vpx_sad16x8, vpx_sad16x8_avg, vpx_variance16x8,
  1869. vpx_sub_pixel_variance16x8, vpx_sub_pixel_avg_variance16x8,
  1870. vpx_sad16x8x4d)
  1871. BFP(BLOCK_8X16, vpx_sad8x16, vpx_sad8x16_avg, vpx_variance8x16,
  1872. vpx_sub_pixel_variance8x16, vpx_sub_pixel_avg_variance8x16,
  1873. vpx_sad8x16x4d)
  1874. BFP(BLOCK_8X8, vpx_sad8x8, vpx_sad8x8_avg, vpx_variance8x8,
  1875. vpx_sub_pixel_variance8x8, vpx_sub_pixel_avg_variance8x8, vpx_sad8x8x4d)
  1876. BFP(BLOCK_8X4, vpx_sad8x4, vpx_sad8x4_avg, vpx_variance8x4,
  1877. vpx_sub_pixel_variance8x4, vpx_sub_pixel_avg_variance8x4, vpx_sad8x4x4d)
  1878. BFP(BLOCK_4X8, vpx_sad4x8, vpx_sad4x8_avg, vpx_variance4x8,
  1879. vpx_sub_pixel_variance4x8, vpx_sub_pixel_avg_variance4x8, vpx_sad4x8x4d)
  1880. BFP(BLOCK_4X4, vpx_sad4x4, vpx_sad4x4_avg, vpx_variance4x4,
  1881. vpx_sub_pixel_variance4x4, vpx_sub_pixel_avg_variance4x4, vpx_sad4x4x4d)
  1882. #if CONFIG_VP9_HIGHBITDEPTH
  1883. highbd_set_var_fns(cpi);
  1884. #endif
  1885. /* vp9_init_quantizer() is first called here. Add check in
  1886. * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
  1887. * called later when needed. This will avoid unnecessary calls of
  1888. * vp9_init_quantizer() for every frame.
  1889. */
  1890. vp9_init_quantizer(cpi);
  1891. vp9_loop_filter_init(cm);
  1892. cm->error.setjmp = 0;
  1893. return cpi;
  1894. }
  1895. #if CONFIG_INTERNAL_STATS
  1896. #define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
  1897. #define SNPRINT2(H, T, V) \
  1898. snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
  1899. #endif // CONFIG_INTERNAL_STATS
  1900. void vp9_remove_compressor(VP9_COMP *cpi) {
  1901. VP9_COMMON *cm;
  1902. unsigned int i;
  1903. int t;
  1904. if (!cpi) return;
  1905. cm = &cpi->common;
  1906. if (cm->current_video_frame > 0) {
  1907. #if CONFIG_INTERNAL_STATS
  1908. vpx_clear_system_state();
  1909. if (cpi->oxcf.pass != 1) {
  1910. char headings[512] = { 0 };
  1911. char results[512] = { 0 };
  1912. FILE *f = fopen("opsnr.stt", "a");
  1913. double time_encoded =
  1914. (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
  1915. 10000000.000;
  1916. double total_encode_time =
  1917. (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
  1918. const double dr =
  1919. (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
  1920. const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
  1921. const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
  1922. const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
  1923. if (cpi->b_calculate_psnr) {
  1924. const double total_psnr = vpx_sse_to_psnr(
  1925. (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
  1926. const double totalp_psnr = vpx_sse_to_psnr(
  1927. (double)cpi->totalp_samples, peak, (double)cpi->totalp_sq_error);
  1928. const double total_ssim =
  1929. 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
  1930. const double totalp_ssim =
  1931. 100 * pow(cpi->summedp_quality / cpi->summedp_weights, 8.0);
  1932. snprintf(headings, sizeof(headings),
  1933. "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
  1934. "VPXSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
  1935. "WstPsnr\tWstSsim\tWstFast\tWstHVS");
  1936. snprintf(results, sizeof(results),
  1937. "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
  1938. "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
  1939. "%7.3f\t%7.3f\t%7.3f\t%7.3f",
  1940. dr, cpi->psnr.stat[ALL] / cpi->count, total_psnr,
  1941. cpi->psnrp.stat[ALL] / cpi->count, totalp_psnr, total_ssim,
  1942. totalp_ssim, cpi->fastssim.stat[ALL] / cpi->count,
  1943. cpi->psnrhvs.stat[ALL] / cpi->count, cpi->psnr.worst,
  1944. cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst);
  1945. if (cpi->b_calculate_blockiness) {
  1946. SNPRINT(headings, "\t Block\tWstBlck");
  1947. SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
  1948. SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
  1949. }
  1950. if (cpi->b_calculate_consistency) {
  1951. double consistency =
  1952. vpx_sse_to_psnr((double)cpi->totalp_samples, peak,
  1953. (double)cpi->total_inconsistency);
  1954. SNPRINT(headings, "\tConsist\tWstCons");
  1955. SNPRINT2(results, "\t%7.3f", consistency);
  1956. SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
  1957. }
  1958. fprintf(f, "%s\t Time\tRcErr\tAbsErr\n", headings);
  1959. fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
  1960. rate_err, fabs(rate_err));
  1961. }
  1962. fclose(f);
  1963. }
  1964. #endif
  1965. #if 0
  1966. {
  1967. printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
  1968. printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
  1969. printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
  1970. cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
  1971. cpi->time_compress_data / 1000,
  1972. (cpi->time_receive_data + cpi->time_compress_data) / 1000);
  1973. }
  1974. #endif
  1975. }
  1976. #if CONFIG_VP9_TEMPORAL_DENOISING
  1977. vp9_denoiser_free(&(cpi->denoiser));
  1978. #endif
  1979. for (t = 0; t < cpi->num_workers; ++t) {
  1980. VPxWorker *const worker = &cpi->workers[t];
  1981. EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
  1982. // Deallocate allocated threads.
  1983. vpx_get_worker_interface()->end(worker);
  1984. // Deallocate allocated thread data.
  1985. if (t < cpi->num_workers - 1) {
  1986. vpx_free(thread_data->td->counts);
  1987. vp9_free_pc_tree(thread_data->td);
  1988. vpx_free(thread_data->td);
  1989. }
  1990. }
  1991. vpx_free(cpi->tile_thr_data);
  1992. vpx_free(cpi->workers);
  1993. vp9_row_mt_mem_dealloc(cpi);
  1994. if (cpi->num_workers > 1) {
  1995. vp9_loop_filter_dealloc(&cpi->lf_row_sync);
  1996. vp9_bitstream_encode_tiles_buffer_dealloc(cpi);
  1997. }
  1998. vp9_alt_ref_aq_destroy(cpi->alt_ref_aq);
  1999. dealloc_compressor_data(cpi);
  2000. for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
  2001. ++i) {
  2002. vpx_free(cpi->mbgraph_stats[i].mb_stats);
  2003. }
  2004. #if CONFIG_FP_MB_STATS
  2005. if (cpi->use_fp_mb_stats) {
  2006. vpx_free(cpi->twopass.frame_mb_stats_buf);
  2007. cpi->twopass.frame_mb_stats_buf = NULL;
  2008. }
  2009. #endif
  2010. vp9_remove_common(cm);
  2011. vp9_free_ref_frame_buffers(cm->buffer_pool);
  2012. #if CONFIG_VP9_POSTPROC
  2013. vp9_free_postproc_buffers(cm);
  2014. #endif
  2015. vpx_free(cpi);
  2016. #if CONFIG_VP9_TEMPORAL_DENOISING
  2017. #ifdef OUTPUT_YUV_DENOISED
  2018. fclose(yuv_denoised_file);
  2019. #endif
  2020. #endif
  2021. #ifdef OUTPUT_YUV_SKINMAP
  2022. fclose(yuv_skinmap_file);
  2023. #endif
  2024. #ifdef OUTPUT_YUV_REC
  2025. fclose(yuv_rec_file);
  2026. #endif
  2027. #if 0
  2028. if (keyfile)
  2029. fclose(keyfile);
  2030. if (framepsnr)
  2031. fclose(framepsnr);
  2032. if (kf_list)
  2033. fclose(kf_list);
  2034. #endif
  2035. }
  2036. static void generate_psnr_packet(VP9_COMP *cpi) {
  2037. struct vpx_codec_cx_pkt pkt;
  2038. int i;
  2039. PSNR_STATS psnr;
  2040. #if CONFIG_VP9_HIGHBITDEPTH
  2041. vpx_calc_highbd_psnr(cpi->raw_source_frame, cpi->common.frame_to_show, &psnr,
  2042. cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
  2043. #else
  2044. vpx_calc_psnr(cpi->raw_source_frame, cpi->common.frame_to_show, &psnr);
  2045. #endif
  2046. for (i = 0; i < 4; ++i) {
  2047. pkt.data.psnr.samples[i] = psnr.samples[i];
  2048. pkt.data.psnr.sse[i] = psnr.sse[i];
  2049. pkt.data.psnr.psnr[i] = psnr.psnr[i];
  2050. }
  2051. pkt.kind = VPX_CODEC_PSNR_PKT;
  2052. if (cpi->use_svc)
  2053. cpi->svc
  2054. .layer_context[cpi->svc.spatial_layer_id *
  2055. cpi->svc.number_temporal_layers]
  2056. .psnr_pkt = pkt.data.psnr;
  2057. else
  2058. vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
  2059. }
  2060. int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags) {
  2061. if (ref_frame_flags > 7) return -1;
  2062. cpi->ref_frame_flags = ref_frame_flags;
  2063. return 0;
  2064. }
  2065. void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags) {
  2066. cpi->ext_refresh_golden_frame = (ref_frame_flags & VP9_GOLD_FLAG) != 0;
  2067. cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & VP9_ALT_FLAG) != 0;
  2068. cpi->ext_refresh_last_frame = (ref_frame_flags & VP9_LAST_FLAG) != 0;
  2069. cpi->ext_refresh_frame_flags_pending = 1;
  2070. }
  2071. static YV12_BUFFER_CONFIG *get_vp9_ref_frame_buffer(
  2072. VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag) {
  2073. MV_REFERENCE_FRAME ref_frame = NONE;
  2074. if (ref_frame_flag == VP9_LAST_FLAG)
  2075. ref_frame = LAST_FRAME;
  2076. else if (ref_frame_flag == VP9_GOLD_FLAG)
  2077. ref_frame = GOLDEN_FRAME;
  2078. else if (ref_frame_flag == VP9_ALT_FLAG)
  2079. ref_frame = ALTREF_FRAME;
  2080. return ref_frame == NONE ? NULL : get_ref_frame_buffer(cpi, ref_frame);
  2081. }
  2082. int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
  2083. YV12_BUFFER_CONFIG *sd) {
  2084. YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
  2085. if (cfg) {
  2086. vpx_yv12_copy_frame(cfg, sd);
  2087. return 0;
  2088. } else {
  2089. return -1;
  2090. }
  2091. }
  2092. int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
  2093. YV12_BUFFER_CONFIG *sd) {
  2094. YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
  2095. if (cfg) {
  2096. vpx_yv12_copy_frame(sd, cfg);
  2097. return 0;
  2098. } else {
  2099. return -1;
  2100. }
  2101. }
  2102. int vp9_update_entropy(VP9_COMP *cpi, int update) {
  2103. cpi->ext_refresh_frame_context = update;
  2104. cpi->ext_refresh_frame_context_pending = 1;
  2105. return 0;
  2106. }
  2107. #ifdef OUTPUT_YUV_REC
  2108. void vp9_write_yuv_rec_frame(VP9_COMMON *cm) {
  2109. YV12_BUFFER_CONFIG *s = cm->frame_to_show;
  2110. uint8_t *src = s->y_buffer;
  2111. int h = cm->height;
  2112. #if CONFIG_VP9_HIGHBITDEPTH
  2113. if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
  2114. uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
  2115. do {
  2116. fwrite(src16, s->y_width, 2, yuv_rec_file);
  2117. src16 += s->y_stride;
  2118. } while (--h);
  2119. src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
  2120. h = s->uv_height;
  2121. do {
  2122. fwrite(src16, s->uv_width, 2, yuv_rec_file);
  2123. src16 += s->uv_stride;
  2124. } while (--h);
  2125. src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
  2126. h = s->uv_height;
  2127. do {
  2128. fwrite(src16, s->uv_width, 2, yuv_rec_file);
  2129. src16 += s->uv_stride;
  2130. } while (--h);
  2131. fflush(yuv_rec_file);
  2132. return;
  2133. }
  2134. #endif // CONFIG_VP9_HIGHBITDEPTH
  2135. do {
  2136. fwrite(src, s->y_width, 1, yuv_rec_file);
  2137. src += s->y_stride;
  2138. } while (--h);
  2139. src = s->u_buffer;
  2140. h = s->uv_height;
  2141. do {
  2142. fwrite(src, s->uv_width, 1, yuv_rec_file);
  2143. src += s->uv_stride;
  2144. } while (--h);
  2145. src = s->v_buffer;
  2146. h = s->uv_height;
  2147. do {
  2148. fwrite(src, s->uv_width, 1, yuv_rec_file);
  2149. src += s->uv_stride;
  2150. } while (--h);
  2151. fflush(yuv_rec_file);
  2152. }
  2153. #endif
  2154. #if CONFIG_VP9_HIGHBITDEPTH
  2155. static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
  2156. YV12_BUFFER_CONFIG *dst,
  2157. int bd) {
  2158. #else
  2159. static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
  2160. YV12_BUFFER_CONFIG *dst) {
  2161. #endif // CONFIG_VP9_HIGHBITDEPTH
  2162. // TODO(dkovalev): replace YV12_BUFFER_CONFIG with vpx_image_t
  2163. int i;
  2164. const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
  2165. src->v_buffer };
  2166. const int src_strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
  2167. const int src_widths[3] = { src->y_crop_width, src->uv_crop_width,
  2168. src->uv_crop_width };
  2169. const int src_heights[3] = { src->y_crop_height, src->uv_crop_height,
  2170. src->uv_crop_height };
  2171. uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
  2172. const int dst_strides[3] = { dst->y_stride, dst->uv_stride, dst->uv_stride };
  2173. const int dst_widths[3] = { dst->y_crop_width, dst->uv_crop_width,
  2174. dst->uv_crop_width };
  2175. const int dst_heights[3] = { dst->y_crop_height, dst->uv_crop_height,
  2176. dst->uv_crop_height };
  2177. for (i = 0; i < MAX_MB_PLANE; ++i) {
  2178. #if CONFIG_VP9_HIGHBITDEPTH
  2179. if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
  2180. vp9_highbd_resize_plane(srcs[i], src_heights[i], src_widths[i],
  2181. src_strides[i], dsts[i], dst_heights[i],
  2182. dst_widths[i], dst_strides[i], bd);
  2183. } else {
  2184. vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
  2185. dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
  2186. }
  2187. #else
  2188. vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
  2189. dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
  2190. #endif // CONFIG_VP9_HIGHBITDEPTH
  2191. }
  2192. vpx_extend_frame_borders(dst);
  2193. }
  2194. #if CONFIG_VP9_HIGHBITDEPTH
  2195. static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
  2196. YV12_BUFFER_CONFIG *dst, int bd,
  2197. INTERP_FILTER filter_type,
  2198. int phase_scaler) {
  2199. const int src_w = src->y_crop_width;
  2200. const int src_h = src->y_crop_height;
  2201. const int dst_w = dst->y_crop_width;
  2202. const int dst_h = dst->y_crop_height;
  2203. const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
  2204. src->v_buffer };
  2205. const int src_strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
  2206. uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
  2207. const int dst_strides[3] = { dst->y_stride, dst->uv_stride, dst->uv_stride };
  2208. const InterpKernel *const kernel = vp9_filter_kernels[filter_type];
  2209. int x, y, i;
  2210. for (i = 0; i < MAX_MB_PLANE; ++i) {
  2211. const int factor = (i == 0 || i == 3 ? 1 : 2);
  2212. const int src_stride = src_strides[i];
  2213. const int dst_stride = dst_strides[i];
  2214. for (y = 0; y < dst_h; y += 16) {
  2215. const int y_q4 = y * (16 / factor) * src_h / dst_h + phase_scaler;
  2216. for (x = 0; x < dst_w; x += 16) {
  2217. const int x_q4 = x * (16 / factor) * src_w / dst_w + phase_scaler;
  2218. const uint8_t *src_ptr = srcs[i] +
  2219. (y / factor) * src_h / dst_h * src_stride +
  2220. (x / factor) * src_w / dst_w;
  2221. uint8_t *dst_ptr = dsts[i] + (y / factor) * dst_stride + (x / factor);
  2222. if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
  2223. vpx_highbd_convolve8(CONVERT_TO_SHORTPTR(src_ptr), src_stride,
  2224. CONVERT_TO_SHORTPTR(dst_ptr), dst_stride, kernel,
  2225. x_q4 & 0xf, 16 * src_w / dst_w, y_q4 & 0xf,
  2226. 16 * src_h / dst_h, 16 / factor, 16 / factor,
  2227. bd);
  2228. } else {
  2229. vpx_scaled_2d(src_ptr, src_stride, dst_ptr, dst_stride, kernel,
  2230. x_q4 & 0xf, 16 * src_w / dst_w, y_q4 & 0xf,
  2231. 16 * src_h / dst_h, 16 / factor, 16 / factor);
  2232. }
  2233. }
  2234. }
  2235. }
  2236. vpx_extend_frame_borders(dst);
  2237. }
  2238. #endif // CONFIG_VP9_HIGHBITDEPTH
  2239. static int scale_down(VP9_COMP *cpi, int q) {
  2240. RATE_CONTROL *const rc = &cpi->rc;
  2241. GF_GROUP *const gf_group = &cpi->twopass.gf_group;
  2242. int scale = 0;
  2243. assert(frame_is_kf_gf_arf(cpi));
  2244. if (rc->frame_size_selector == UNSCALED &&
  2245. q >= rc->rf_level_maxq[gf_group->rf_level[gf_group->index]]) {
  2246. const int max_size_thresh =
  2247. (int)(rate_thresh_mult[SCALE_STEP1] *
  2248. VPXMAX(rc->this_frame_target, rc->avg_frame_bandwidth));
  2249. scale = rc->projected_frame_size > max_size_thresh ? 1 : 0;
  2250. }
  2251. return scale;
  2252. }
  2253. static int big_rate_miss_high_threshold(VP9_COMP *cpi) {
  2254. const RATE_CONTROL *const rc = &cpi->rc;
  2255. int big_miss_high;
  2256. if (frame_is_kf_gf_arf(cpi))
  2257. big_miss_high = rc->this_frame_target * 3 / 2;
  2258. else
  2259. big_miss_high = rc->this_frame_target * 2;
  2260. return big_miss_high;
  2261. }
  2262. static int big_rate_miss(VP9_COMP *cpi) {
  2263. const RATE_CONTROL *const rc = &cpi->rc;
  2264. int big_miss_high;
  2265. int big_miss_low;
  2266. // Ignore for overlay frames
  2267. if (rc->is_src_frame_alt_ref) {
  2268. return 0;
  2269. } else {
  2270. big_miss_low = (rc->this_frame_target / 2);
  2271. big_miss_high = big_rate_miss_high_threshold(cpi);
  2272. return (rc->projected_frame_size > big_miss_high) ||
  2273. (rc->projected_frame_size < big_miss_low);
  2274. }
  2275. }
  2276. // test in two pass for the first
  2277. static int two_pass_first_group_inter(VP9_COMP *cpi) {
  2278. TWO_PASS *const twopass = &cpi->twopass;
  2279. GF_GROUP *const gf_group = &twopass->gf_group;
  2280. if ((cpi->oxcf.pass == 2) &&
  2281. (gf_group->index == gf_group->first_inter_index)) {
  2282. return 1;
  2283. } else {
  2284. return 0;
  2285. }
  2286. }
  2287. // Function to test for conditions that indicate we should loop
  2288. // back and recode a frame.
  2289. static int recode_loop_test(VP9_COMP *cpi, int high_limit, int low_limit, int q,
  2290. int maxq, int minq) {
  2291. const RATE_CONTROL *const rc = &cpi->rc;
  2292. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  2293. const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
  2294. int force_recode = 0;
  2295. if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
  2296. big_rate_miss(cpi) || (cpi->sf.recode_loop == ALLOW_RECODE) ||
  2297. (two_pass_first_group_inter(cpi) &&
  2298. (cpi->sf.recode_loop == ALLOW_RECODE_FIRST)) ||
  2299. (frame_is_kfgfarf && (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF))) {
  2300. if (frame_is_kfgfarf && (oxcf->resize_mode == RESIZE_DYNAMIC) &&
  2301. scale_down(cpi, q)) {
  2302. // Code this group at a lower resolution.
  2303. cpi->resize_pending = 1;
  2304. return 1;
  2305. }
  2306. // Force recode for extreme overshoot.
  2307. if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
  2308. (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF &&
  2309. rc->projected_frame_size >= big_rate_miss_high_threshold(cpi))) {
  2310. return 1;
  2311. }
  2312. // TODO(agrange) high_limit could be greater than the scale-down threshold.
  2313. if ((rc->projected_frame_size > high_limit && q < maxq) ||
  2314. (rc->projected_frame_size < low_limit && q > minq)) {
  2315. force_recode = 1;
  2316. } else if (cpi->oxcf.rc_mode == VPX_CQ) {
  2317. // Deal with frame undershoot and whether or not we are
  2318. // below the automatically set cq level.
  2319. if (q > oxcf->cq_level &&
  2320. rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
  2321. force_recode = 1;
  2322. }
  2323. }
  2324. }
  2325. return force_recode;
  2326. }
  2327. void vp9_update_reference_frames(VP9_COMP *cpi) {
  2328. VP9_COMMON *const cm = &cpi->common;
  2329. BufferPool *const pool = cm->buffer_pool;
  2330. // At this point the new frame has been encoded.
  2331. // If any buffer copy / swapping is signaled it should be done here.
  2332. if (cm->frame_type == KEY_FRAME) {
  2333. ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
  2334. cm->new_fb_idx);
  2335. ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
  2336. cm->new_fb_idx);
  2337. } else if (vp9_preserve_existing_gf(cpi)) {
  2338. // We have decided to preserve the previously existing golden frame as our
  2339. // new ARF frame. However, in the short term in function
  2340. // vp9_get_refresh_mask() we left it in the GF slot and, if
  2341. // we're updating the GF with the current decoded frame, we save it to the
  2342. // ARF slot instead.
  2343. // We now have to update the ARF with the current frame and swap gld_fb_idx
  2344. // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
  2345. // slot and, if we're updating the GF, the current frame becomes the new GF.
  2346. int tmp;
  2347. ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
  2348. cm->new_fb_idx);
  2349. tmp = cpi->alt_fb_idx;
  2350. cpi->alt_fb_idx = cpi->gld_fb_idx;
  2351. cpi->gld_fb_idx = tmp;
  2352. if (is_two_pass_svc(cpi)) {
  2353. cpi->svc.layer_context[0].gold_ref_idx = cpi->gld_fb_idx;
  2354. cpi->svc.layer_context[0].alt_ref_idx = cpi->alt_fb_idx;
  2355. }
  2356. } else { /* For non key/golden frames */
  2357. if (cpi->refresh_alt_ref_frame) {
  2358. int arf_idx = cpi->alt_fb_idx;
  2359. if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
  2360. const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
  2361. arf_idx = gf_group->arf_update_idx[gf_group->index];
  2362. }
  2363. ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
  2364. memcpy(cpi->interp_filter_selected[ALTREF_FRAME],
  2365. cpi->interp_filter_selected[0],
  2366. sizeof(cpi->interp_filter_selected[0]));
  2367. }
  2368. if (cpi->refresh_golden_frame) {
  2369. ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
  2370. cm->new_fb_idx);
  2371. if (!cpi->rc.is_src_frame_alt_ref)
  2372. memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
  2373. cpi->interp_filter_selected[0],
  2374. sizeof(cpi->interp_filter_selected[0]));
  2375. else
  2376. memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
  2377. cpi->interp_filter_selected[ALTREF_FRAME],
  2378. sizeof(cpi->interp_filter_selected[ALTREF_FRAME]));
  2379. }
  2380. }
  2381. if (cpi->refresh_last_frame) {
  2382. ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->lst_fb_idx],
  2383. cm->new_fb_idx);
  2384. if (!cpi->rc.is_src_frame_alt_ref)
  2385. memcpy(cpi->interp_filter_selected[LAST_FRAME],
  2386. cpi->interp_filter_selected[0],
  2387. sizeof(cpi->interp_filter_selected[0]));
  2388. }
  2389. #if CONFIG_VP9_TEMPORAL_DENOISING
  2390. if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi) &&
  2391. cpi->denoiser.denoising_level > kDenLowLow) {
  2392. int svc_base_is_key = 0;
  2393. if (cpi->use_svc) {
  2394. int realloc_fail = 0;
  2395. int layer = LAYER_IDS_TO_IDX(cpi->svc.spatial_layer_id,
  2396. cpi->svc.temporal_layer_id,
  2397. cpi->svc.number_temporal_layers);
  2398. LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
  2399. svc_base_is_key = lc->is_key_frame;
  2400. // Check if we need to allocate extra buffers in the denoiser for
  2401. // refreshed frames.
  2402. realloc_fail = vp9_denoiser_realloc_svc(
  2403. cm, &cpi->denoiser, cpi->refresh_alt_ref_frame,
  2404. cpi->refresh_golden_frame, cpi->refresh_last_frame, cpi->alt_fb_idx,
  2405. cpi->gld_fb_idx, cpi->lst_fb_idx);
  2406. if (realloc_fail)
  2407. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  2408. "Failed to re-allocate denoiser for SVC");
  2409. }
  2410. vp9_denoiser_update_frame_info(
  2411. &cpi->denoiser, *cpi->Source, cpi->common.frame_type,
  2412. cpi->refresh_alt_ref_frame, cpi->refresh_golden_frame,
  2413. cpi->refresh_last_frame, cpi->alt_fb_idx, cpi->gld_fb_idx,
  2414. cpi->lst_fb_idx, cpi->resize_pending, svc_base_is_key);
  2415. }
  2416. #endif
  2417. if (is_one_pass_cbr_svc(cpi)) {
  2418. // Keep track of frame index for each reference frame.
  2419. SVC *const svc = &cpi->svc;
  2420. if (cm->frame_type == KEY_FRAME) {
  2421. svc->ref_frame_index[cpi->lst_fb_idx] = svc->current_superframe;
  2422. svc->ref_frame_index[cpi->gld_fb_idx] = svc->current_superframe;
  2423. svc->ref_frame_index[cpi->alt_fb_idx] = svc->current_superframe;
  2424. } else {
  2425. if (cpi->refresh_last_frame)
  2426. svc->ref_frame_index[cpi->lst_fb_idx] = svc->current_superframe;
  2427. if (cpi->refresh_golden_frame)
  2428. svc->ref_frame_index[cpi->gld_fb_idx] = svc->current_superframe;
  2429. if (cpi->refresh_alt_ref_frame)
  2430. svc->ref_frame_index[cpi->alt_fb_idx] = svc->current_superframe;
  2431. }
  2432. }
  2433. }
  2434. static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) {
  2435. MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
  2436. struct loopfilter *lf = &cm->lf;
  2437. const int is_reference_frame =
  2438. (cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
  2439. cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame);
  2440. if (xd->lossless) {
  2441. lf->filter_level = 0;
  2442. lf->last_filt_level = 0;
  2443. } else {
  2444. struct vpx_usec_timer timer;
  2445. vpx_clear_system_state();
  2446. vpx_usec_timer_start(&timer);
  2447. if (!cpi->rc.is_src_frame_alt_ref) {
  2448. if ((cpi->common.frame_type == KEY_FRAME) &&
  2449. (!cpi->rc.this_key_frame_forced)) {
  2450. lf->last_filt_level = 0;
  2451. }
  2452. vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.lpf_pick);
  2453. lf->last_filt_level = lf->filter_level;
  2454. } else {
  2455. lf->filter_level = 0;
  2456. }
  2457. vpx_usec_timer_mark(&timer);
  2458. cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
  2459. }
  2460. if (lf->filter_level > 0 && is_reference_frame) {
  2461. vp9_build_mask_frame(cm, lf->filter_level, 0);
  2462. if (cpi->num_workers > 1)
  2463. vp9_loop_filter_frame_mt(cm->frame_to_show, cm, xd->plane,
  2464. lf->filter_level, 0, 0, cpi->workers,
  2465. cpi->num_workers, &cpi->lf_row_sync);
  2466. else
  2467. vp9_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
  2468. }
  2469. vpx_extend_frame_inner_borders(cm->frame_to_show);
  2470. }
  2471. static INLINE void alloc_frame_mvs(VP9_COMMON *const cm, int buffer_idx) {
  2472. RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
  2473. if (new_fb_ptr->mvs == NULL || new_fb_ptr->mi_rows < cm->mi_rows ||
  2474. new_fb_ptr->mi_cols < cm->mi_cols) {
  2475. vpx_free(new_fb_ptr->mvs);
  2476. CHECK_MEM_ERROR(cm, new_fb_ptr->mvs,
  2477. (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols,
  2478. sizeof(*new_fb_ptr->mvs)));
  2479. new_fb_ptr->mi_rows = cm->mi_rows;
  2480. new_fb_ptr->mi_cols = cm->mi_cols;
  2481. }
  2482. }
  2483. void vp9_scale_references(VP9_COMP *cpi) {
  2484. VP9_COMMON *cm = &cpi->common;
  2485. MV_REFERENCE_FRAME ref_frame;
  2486. const VP9_REFFRAME ref_mask[3] = { VP9_LAST_FLAG, VP9_GOLD_FLAG,
  2487. VP9_ALT_FLAG };
  2488. for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
  2489. // Need to convert from VP9_REFFRAME to index into ref_mask (subtract 1).
  2490. if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
  2491. BufferPool *const pool = cm->buffer_pool;
  2492. const YV12_BUFFER_CONFIG *const ref =
  2493. get_ref_frame_buffer(cpi, ref_frame);
  2494. if (ref == NULL) {
  2495. cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
  2496. continue;
  2497. }
  2498. #if CONFIG_VP9_HIGHBITDEPTH
  2499. if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
  2500. RefCntBuffer *new_fb_ptr = NULL;
  2501. int force_scaling = 0;
  2502. int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
  2503. if (new_fb == INVALID_IDX) {
  2504. new_fb = get_free_fb(cm);
  2505. force_scaling = 1;
  2506. }
  2507. if (new_fb == INVALID_IDX) return;
  2508. new_fb_ptr = &pool->frame_bufs[new_fb];
  2509. if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
  2510. new_fb_ptr->buf.y_crop_height != cm->height) {
  2511. if (vpx_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
  2512. cm->subsampling_x, cm->subsampling_y,
  2513. cm->use_highbitdepth,
  2514. VP9_ENC_BORDER_IN_PIXELS,
  2515. cm->byte_alignment, NULL, NULL, NULL))
  2516. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  2517. "Failed to allocate frame buffer");
  2518. scale_and_extend_frame(ref, &new_fb_ptr->buf, (int)cm->bit_depth,
  2519. EIGHTTAP, 0);
  2520. cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
  2521. alloc_frame_mvs(cm, new_fb);
  2522. }
  2523. #else
  2524. if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
  2525. RefCntBuffer *new_fb_ptr = NULL;
  2526. int force_scaling = 0;
  2527. int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
  2528. if (new_fb == INVALID_IDX) {
  2529. new_fb = get_free_fb(cm);
  2530. force_scaling = 1;
  2531. }
  2532. if (new_fb == INVALID_IDX) return;
  2533. new_fb_ptr = &pool->frame_bufs[new_fb];
  2534. if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
  2535. new_fb_ptr->buf.y_crop_height != cm->height) {
  2536. if (vpx_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
  2537. cm->subsampling_x, cm->subsampling_y,
  2538. VP9_ENC_BORDER_IN_PIXELS,
  2539. cm->byte_alignment, NULL, NULL, NULL))
  2540. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  2541. "Failed to allocate frame buffer");
  2542. vp9_scale_and_extend_frame(ref, &new_fb_ptr->buf, EIGHTTAP, 0);
  2543. cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
  2544. alloc_frame_mvs(cm, new_fb);
  2545. }
  2546. #endif // CONFIG_VP9_HIGHBITDEPTH
  2547. } else {
  2548. int buf_idx;
  2549. RefCntBuffer *buf = NULL;
  2550. if (cpi->oxcf.pass == 0 && !cpi->use_svc) {
  2551. // Check for release of scaled reference.
  2552. buf_idx = cpi->scaled_ref_idx[ref_frame - 1];
  2553. buf = (buf_idx != INVALID_IDX) ? &pool->frame_bufs[buf_idx] : NULL;
  2554. if (buf != NULL) {
  2555. --buf->ref_count;
  2556. cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
  2557. }
  2558. }
  2559. buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
  2560. buf = &pool->frame_bufs[buf_idx];
  2561. buf->buf.y_crop_width = ref->y_crop_width;
  2562. buf->buf.y_crop_height = ref->y_crop_height;
  2563. cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
  2564. ++buf->ref_count;
  2565. }
  2566. } else {
  2567. if (cpi->oxcf.pass != 0 || cpi->use_svc)
  2568. cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
  2569. }
  2570. }
  2571. }
  2572. static void release_scaled_references(VP9_COMP *cpi) {
  2573. VP9_COMMON *cm = &cpi->common;
  2574. int i;
  2575. if (cpi->oxcf.pass == 0 && !cpi->use_svc) {
  2576. // Only release scaled references under certain conditions:
  2577. // if reference will be updated, or if scaled reference has same resolution.
  2578. int refresh[3];
  2579. refresh[0] = (cpi->refresh_last_frame) ? 1 : 0;
  2580. refresh[1] = (cpi->refresh_golden_frame) ? 1 : 0;
  2581. refresh[2] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
  2582. for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
  2583. const int idx = cpi->scaled_ref_idx[i - 1];
  2584. RefCntBuffer *const buf =
  2585. idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
  2586. const YV12_BUFFER_CONFIG *const ref = get_ref_frame_buffer(cpi, i);
  2587. if (buf != NULL &&
  2588. (refresh[i - 1] || (buf->buf.y_crop_width == ref->y_crop_width &&
  2589. buf->buf.y_crop_height == ref->y_crop_height))) {
  2590. --buf->ref_count;
  2591. cpi->scaled_ref_idx[i - 1] = INVALID_IDX;
  2592. }
  2593. }
  2594. } else {
  2595. for (i = 0; i < MAX_REF_FRAMES; ++i) {
  2596. const int idx = cpi->scaled_ref_idx[i];
  2597. RefCntBuffer *const buf =
  2598. idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
  2599. if (buf != NULL) {
  2600. --buf->ref_count;
  2601. cpi->scaled_ref_idx[i] = INVALID_IDX;
  2602. }
  2603. }
  2604. }
  2605. }
  2606. static void full_to_model_count(unsigned int *model_count,
  2607. unsigned int *full_count) {
  2608. int n;
  2609. model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
  2610. model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
  2611. model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
  2612. for (n = THREE_TOKEN; n < EOB_TOKEN; ++n)
  2613. model_count[TWO_TOKEN] += full_count[n];
  2614. model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
  2615. }
  2616. static void full_to_model_counts(vp9_coeff_count_model *model_count,
  2617. vp9_coeff_count *full_count) {
  2618. int i, j, k, l;
  2619. for (i = 0; i < PLANE_TYPES; ++i)
  2620. for (j = 0; j < REF_TYPES; ++j)
  2621. for (k = 0; k < COEF_BANDS; ++k)
  2622. for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
  2623. full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
  2624. }
  2625. #if 0 && CONFIG_INTERNAL_STATS
  2626. static void output_frame_level_debug_stats(VP9_COMP *cpi) {
  2627. VP9_COMMON *const cm = &cpi->common;
  2628. FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
  2629. int64_t recon_err;
  2630. vpx_clear_system_state();
  2631. #if CONFIG_VP9_HIGHBITDEPTH
  2632. if (cm->use_highbitdepth) {
  2633. recon_err = vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
  2634. } else {
  2635. recon_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
  2636. }
  2637. #else
  2638. recon_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
  2639. #endif // CONFIG_VP9_HIGHBITDEPTH
  2640. if (cpi->twopass.total_left_stats.coded_error != 0.0) {
  2641. double dc_quant_devisor;
  2642. #if CONFIG_VP9_HIGHBITDEPTH
  2643. switch (cm->bit_depth) {
  2644. case VPX_BITS_8:
  2645. dc_quant_devisor = 4.0;
  2646. break;
  2647. case VPX_BITS_10:
  2648. dc_quant_devisor = 16.0;
  2649. break;
  2650. case VPX_BITS_12:
  2651. dc_quant_devisor = 64.0;
  2652. break;
  2653. default:
  2654. assert(0 && "bit_depth must be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12");
  2655. break;
  2656. }
  2657. #else
  2658. dc_quant_devisor = 4.0;
  2659. #endif
  2660. if (!cm->current_video_frame) {
  2661. fprintf(f, "frame, width, height, last ts, last end ts, "
  2662. "source_alt_ref_pending, source_alt_ref_active, "
  2663. "this_frame_target, projected_frame_size, "
  2664. "projected_frame_size / MBs, "
  2665. "projected_frame_size - this_frame_target, "
  2666. "vbr_bits_off_target, vbr_bits_off_target_fast, "
  2667. "twopass.extend_minq, twopass.extend_minq_fast, "
  2668. "total_target_vs_actual, "
  2669. "starting_buffer_level - bits_off_target, "
  2670. "total_actual_bits, base_qindex, q for base_qindex, "
  2671. "dc quant, q for active_worst_quality, avg_q, q for oxcf.cq_level, "
  2672. "refresh_last_frame, refresh_golden_frame, refresh_alt_ref_frame, "
  2673. "frame_type, gfu_boost, "
  2674. "twopass.bits_left, "
  2675. "twopass.total_left_stats.coded_error, "
  2676. "twopass.bits_left / (1 + twopass.total_left_stats.coded_error), "
  2677. "tot_recode_hits, recon_err, kf_boost, "
  2678. "twopass.kf_zeromotion_pct, twopass.fr_content_type, "
  2679. "filter_level, seg.aq_av_offset\n");
  2680. }
  2681. fprintf(f, "%10u, %d, %d, %10"PRId64", %10"PRId64", %d, %d, %10d, %10d, "
  2682. "%10d, %10d, %10"PRId64", %10"PRId64", %5d, %5d, %10"PRId64", "
  2683. "%10"PRId64", %10"PRId64", %10d, %7.2lf, %7.2lf, %7.2lf, %7.2lf, "
  2684. "%7.2lf, %6d, %6d, %5d, %5d, %5d, %10"PRId64", %10.3lf, %10lf, %8u, "
  2685. "%10"PRId64", %10d, %10d, %10d, %10d, %10d\n",
  2686. cpi->common.current_video_frame,
  2687. cm->width, cm->height,
  2688. cpi->last_time_stamp_seen,
  2689. cpi->last_end_time_stamp_seen,
  2690. cpi->rc.source_alt_ref_pending,
  2691. cpi->rc.source_alt_ref_active,
  2692. cpi->rc.this_frame_target,
  2693. cpi->rc.projected_frame_size,
  2694. cpi->rc.projected_frame_size / cpi->common.MBs,
  2695. (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
  2696. cpi->rc.vbr_bits_off_target,
  2697. cpi->rc.vbr_bits_off_target_fast,
  2698. cpi->twopass.extend_minq,
  2699. cpi->twopass.extend_minq_fast,
  2700. cpi->rc.total_target_vs_actual,
  2701. (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
  2702. cpi->rc.total_actual_bits, cm->base_qindex,
  2703. vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth),
  2704. (double)vp9_dc_quant(cm->base_qindex, 0, cm->bit_depth) /
  2705. dc_quant_devisor,
  2706. vp9_convert_qindex_to_q(cpi->twopass.active_worst_quality,
  2707. cm->bit_depth),
  2708. cpi->rc.avg_q,
  2709. vp9_convert_qindex_to_q(cpi->oxcf.cq_level, cm->bit_depth),
  2710. cpi->refresh_last_frame, cpi->refresh_golden_frame,
  2711. cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
  2712. cpi->twopass.bits_left,
  2713. cpi->twopass.total_left_stats.coded_error,
  2714. cpi->twopass.bits_left /
  2715. (1 + cpi->twopass.total_left_stats.coded_error),
  2716. cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
  2717. cpi->twopass.kf_zeromotion_pct,
  2718. cpi->twopass.fr_content_type,
  2719. cm->lf.filter_level,
  2720. cm->seg.aq_av_offset);
  2721. }
  2722. fclose(f);
  2723. if (0) {
  2724. FILE *const fmodes = fopen("Modes.stt", "a");
  2725. int i;
  2726. fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
  2727. cm->frame_type, cpi->refresh_golden_frame,
  2728. cpi->refresh_alt_ref_frame);
  2729. for (i = 0; i < MAX_MODES; ++i)
  2730. fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
  2731. fprintf(fmodes, "\n");
  2732. fclose(fmodes);
  2733. }
  2734. }
  2735. #endif
  2736. static void set_mv_search_params(VP9_COMP *cpi) {
  2737. const VP9_COMMON *const cm = &cpi->common;
  2738. const unsigned int max_mv_def = VPXMIN(cm->width, cm->height);
  2739. // Default based on max resolution.
  2740. cpi->mv_step_param = vp9_init_search_range(max_mv_def);
  2741. if (cpi->sf.mv.auto_mv_step_size) {
  2742. if (frame_is_intra_only(cm)) {
  2743. // Initialize max_mv_magnitude for use in the first INTER frame
  2744. // after a key/intra-only frame.
  2745. cpi->max_mv_magnitude = max_mv_def;
  2746. } else {
  2747. if (cm->show_frame) {
  2748. // Allow mv_steps to correspond to twice the max mv magnitude found
  2749. // in the previous frame, capped by the default max_mv_magnitude based
  2750. // on resolution.
  2751. cpi->mv_step_param = vp9_init_search_range(
  2752. VPXMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
  2753. }
  2754. cpi->max_mv_magnitude = 0;
  2755. }
  2756. }
  2757. }
  2758. static void set_size_independent_vars(VP9_COMP *cpi) {
  2759. vp9_set_speed_features_framesize_independent(cpi);
  2760. vp9_set_rd_speed_thresholds(cpi);
  2761. vp9_set_rd_speed_thresholds_sub8x8(cpi);
  2762. cpi->common.interp_filter = cpi->sf.default_interp_filter;
  2763. }
  2764. static void set_size_dependent_vars(VP9_COMP *cpi, int *q, int *bottom_index,
  2765. int *top_index) {
  2766. VP9_COMMON *const cm = &cpi->common;
  2767. // Setup variables that depend on the dimensions of the frame.
  2768. vp9_set_speed_features_framesize_dependent(cpi);
  2769. // Decide q and q bounds.
  2770. *q = vp9_rc_pick_q_and_bounds(cpi, bottom_index, top_index);
  2771. if (!frame_is_intra_only(cm)) {
  2772. vp9_set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH);
  2773. }
  2774. #if !CONFIG_REALTIME_ONLY
  2775. // Configure experimental use of segmentation for enhanced coding of
  2776. // static regions if indicated.
  2777. // Only allowed in the second pass of a two pass encode, as it requires
  2778. // lagged coding, and if the relevant speed feature flag is set.
  2779. if (cpi->oxcf.pass == 2 && cpi->sf.static_segmentation)
  2780. configure_static_seg_features(cpi);
  2781. #endif // !CONFIG_REALTIME_ONLY
  2782. #if CONFIG_VP9_POSTPROC && !(CONFIG_VP9_TEMPORAL_DENOISING)
  2783. if (cpi->oxcf.noise_sensitivity > 0) {
  2784. int l = 0;
  2785. switch (cpi->oxcf.noise_sensitivity) {
  2786. case 1: l = 20; break;
  2787. case 2: l = 40; break;
  2788. case 3: l = 60; break;
  2789. case 4:
  2790. case 5: l = 100; break;
  2791. case 6: l = 150; break;
  2792. }
  2793. if (!cpi->common.postproc_state.limits) {
  2794. cpi->common.postproc_state.limits =
  2795. vpx_calloc(cpi->un_scaled_source->y_width,
  2796. sizeof(*cpi->common.postproc_state.limits));
  2797. }
  2798. vp9_denoise(cpi->Source, cpi->Source, l, cpi->common.postproc_state.limits);
  2799. }
  2800. #endif // CONFIG_VP9_POSTPROC
  2801. }
  2802. #if CONFIG_VP9_TEMPORAL_DENOISING
  2803. static void setup_denoiser_buffer(VP9_COMP *cpi) {
  2804. VP9_COMMON *const cm = &cpi->common;
  2805. if (cpi->oxcf.noise_sensitivity > 0 &&
  2806. !cpi->denoiser.frame_buffer_initialized) {
  2807. if (vp9_denoiser_alloc(cm, cpi->use_svc, &cpi->denoiser, cm->width,
  2808. cm->height, cm->subsampling_x, cm->subsampling_y,
  2809. #if CONFIG_VP9_HIGHBITDEPTH
  2810. cm->use_highbitdepth,
  2811. #endif
  2812. VP9_ENC_BORDER_IN_PIXELS))
  2813. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  2814. "Failed to allocate denoiser");
  2815. }
  2816. }
  2817. #endif
  2818. static void init_motion_estimation(VP9_COMP *cpi) {
  2819. int y_stride = cpi->scaled_source.y_stride;
  2820. if (cpi->sf.mv.search_method == NSTEP) {
  2821. vp9_init3smotion_compensation(&cpi->ss_cfg, y_stride);
  2822. } else if (cpi->sf.mv.search_method == DIAMOND) {
  2823. vp9_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
  2824. }
  2825. }
  2826. static void set_frame_size(VP9_COMP *cpi) {
  2827. int ref_frame;
  2828. VP9_COMMON *const cm = &cpi->common;
  2829. VP9EncoderConfig *const oxcf = &cpi->oxcf;
  2830. MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
  2831. #if !CONFIG_REALTIME_ONLY
  2832. if (oxcf->pass == 2 && oxcf->rc_mode == VPX_VBR &&
  2833. ((oxcf->resize_mode == RESIZE_FIXED && cm->current_video_frame == 0) ||
  2834. (oxcf->resize_mode == RESIZE_DYNAMIC && cpi->resize_pending))) {
  2835. calculate_coded_size(cpi, &oxcf->scaled_frame_width,
  2836. &oxcf->scaled_frame_height);
  2837. // There has been a change in frame size.
  2838. vp9_set_size_literal(cpi, oxcf->scaled_frame_width,
  2839. oxcf->scaled_frame_height);
  2840. }
  2841. #endif // !CONFIG_REALTIME_ONLY
  2842. if (oxcf->pass == 0 && oxcf->rc_mode == VPX_CBR && !cpi->use_svc &&
  2843. oxcf->resize_mode == RESIZE_DYNAMIC && cpi->resize_pending != 0) {
  2844. oxcf->scaled_frame_width =
  2845. (oxcf->width * cpi->resize_scale_num) / cpi->resize_scale_den;
  2846. oxcf->scaled_frame_height =
  2847. (oxcf->height * cpi->resize_scale_num) / cpi->resize_scale_den;
  2848. // There has been a change in frame size.
  2849. vp9_set_size_literal(cpi, oxcf->scaled_frame_width,
  2850. oxcf->scaled_frame_height);
  2851. // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
  2852. set_mv_search_params(cpi);
  2853. vp9_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
  2854. #if CONFIG_VP9_TEMPORAL_DENOISING
  2855. // Reset the denoiser on the resized frame.
  2856. if (cpi->oxcf.noise_sensitivity > 0) {
  2857. vp9_denoiser_free(&(cpi->denoiser));
  2858. setup_denoiser_buffer(cpi);
  2859. // Dynamic resize is only triggered for non-SVC, so we can force
  2860. // golden frame update here as temporary fix to denoiser.
  2861. cpi->refresh_golden_frame = 1;
  2862. }
  2863. #endif
  2864. }
  2865. if ((oxcf->pass == 2) &&
  2866. (!cpi->use_svc || (is_two_pass_svc(cpi) &&
  2867. cpi->svc.encode_empty_frame_state != ENCODING))) {
  2868. vp9_set_target_rate(cpi);
  2869. }
  2870. alloc_frame_mvs(cm, cm->new_fb_idx);
  2871. // Reset the frame pointers to the current frame size.
  2872. if (vpx_realloc_frame_buffer(get_frame_new_buffer(cm), cm->width, cm->height,
  2873. cm->subsampling_x, cm->subsampling_y,
  2874. #if CONFIG_VP9_HIGHBITDEPTH
  2875. cm->use_highbitdepth,
  2876. #endif
  2877. VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
  2878. NULL, NULL, NULL))
  2879. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  2880. "Failed to allocate frame buffer");
  2881. alloc_util_frame_buffers(cpi);
  2882. init_motion_estimation(cpi);
  2883. for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
  2884. RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - 1];
  2885. const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
  2886. ref_buf->idx = buf_idx;
  2887. if (buf_idx != INVALID_IDX) {
  2888. YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
  2889. ref_buf->buf = buf;
  2890. #if CONFIG_VP9_HIGHBITDEPTH
  2891. vp9_setup_scale_factors_for_frame(
  2892. &ref_buf->sf, buf->y_crop_width, buf->y_crop_height, cm->width,
  2893. cm->height, (buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0);
  2894. #else
  2895. vp9_setup_scale_factors_for_frame(&ref_buf->sf, buf->y_crop_width,
  2896. buf->y_crop_height, cm->width,
  2897. cm->height);
  2898. #endif // CONFIG_VP9_HIGHBITDEPTH
  2899. if (vp9_is_scaled(&ref_buf->sf)) vpx_extend_frame_borders(buf);
  2900. } else {
  2901. ref_buf->buf = NULL;
  2902. }
  2903. }
  2904. set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
  2905. }
  2906. static void encode_without_recode_loop(VP9_COMP *cpi, size_t *size,
  2907. uint8_t *dest) {
  2908. VP9_COMMON *const cm = &cpi->common;
  2909. int q = 0, bottom_index = 0, top_index = 0; // Dummy variables.
  2910. const INTERP_FILTER filter_scaler =
  2911. (is_one_pass_cbr_svc(cpi))
  2912. ? cpi->svc.downsample_filter_type[cpi->svc.spatial_layer_id]
  2913. : EIGHTTAP;
  2914. const int phase_scaler =
  2915. (is_one_pass_cbr_svc(cpi))
  2916. ? cpi->svc.downsample_filter_phase[cpi->svc.spatial_layer_id]
  2917. : 0;
  2918. // Flag to check if its valid to compute the source sad (used for
  2919. // scene detection and for superblock content state in CBR mode).
  2920. // The flag may get reset below based on SVC or resizing state.
  2921. cpi->compute_source_sad_onepass =
  2922. cpi->oxcf.mode == REALTIME && cpi->oxcf.speed >= 5 && cm->show_frame;
  2923. vpx_clear_system_state();
  2924. set_frame_size(cpi);
  2925. if (is_one_pass_cbr_svc(cpi) &&
  2926. cpi->un_scaled_source->y_width == cm->width << 2 &&
  2927. cpi->un_scaled_source->y_height == cm->height << 2 &&
  2928. cpi->svc.scaled_temp.y_width == cm->width << 1 &&
  2929. cpi->svc.scaled_temp.y_height == cm->height << 1) {
  2930. // For svc, if it is a 1/4x1/4 downscaling, do a two-stage scaling to take
  2931. // advantage of the 1:2 optimized scaler. In the process, the 1/2x1/2
  2932. // result will be saved in scaled_temp and might be used later.
  2933. const INTERP_FILTER filter_scaler2 = cpi->svc.downsample_filter_type[1];
  2934. const int phase_scaler2 = cpi->svc.downsample_filter_phase[1];
  2935. cpi->Source = vp9_svc_twostage_scale(
  2936. cm, cpi->un_scaled_source, &cpi->scaled_source, &cpi->svc.scaled_temp,
  2937. filter_scaler, phase_scaler, filter_scaler2, phase_scaler2);
  2938. cpi->svc.scaled_one_half = 1;
  2939. } else if (is_one_pass_cbr_svc(cpi) &&
  2940. cpi->un_scaled_source->y_width == cm->width << 1 &&
  2941. cpi->un_scaled_source->y_height == cm->height << 1 &&
  2942. cpi->svc.scaled_one_half) {
  2943. // If the spatial layer is 1/2x1/2 and the scaling is already done in the
  2944. // two-stage scaling, use the result directly.
  2945. cpi->Source = &cpi->svc.scaled_temp;
  2946. cpi->svc.scaled_one_half = 0;
  2947. } else {
  2948. cpi->Source = vp9_scale_if_required(
  2949. cm, cpi->un_scaled_source, &cpi->scaled_source, (cpi->oxcf.pass == 0),
  2950. filter_scaler, phase_scaler);
  2951. }
  2952. // Unfiltered raw source used in metrics calculation if the source
  2953. // has been filtered.
  2954. if (is_psnr_calc_enabled(cpi)) {
  2955. #ifdef ENABLE_KF_DENOISE
  2956. if (is_spatial_denoise_enabled(cpi)) {
  2957. cpi->raw_source_frame = vp9_scale_if_required(
  2958. cm, &cpi->raw_unscaled_source, &cpi->raw_scaled_source,
  2959. (cpi->oxcf.pass == 0), EIGHTTAP, phase_scaler);
  2960. } else {
  2961. cpi->raw_source_frame = cpi->Source;
  2962. }
  2963. #else
  2964. cpi->raw_source_frame = cpi->Source;
  2965. #endif
  2966. }
  2967. if ((cpi->use_svc &&
  2968. (cpi->svc.spatial_layer_id < cpi->svc.number_spatial_layers - 1 ||
  2969. cpi->svc.temporal_layer_id < cpi->svc.number_temporal_layers - 1 ||
  2970. cpi->svc.current_superframe < 1)) ||
  2971. cpi->resize_pending || cpi->resize_state || cpi->external_resize ||
  2972. cpi->resize_state != ORIG) {
  2973. cpi->compute_source_sad_onepass = 0;
  2974. if (cpi->content_state_sb_fd != NULL)
  2975. memset(cpi->content_state_sb_fd, 0,
  2976. (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1) *
  2977. sizeof(*cpi->content_state_sb_fd));
  2978. }
  2979. // Avoid scaling last_source unless its needed.
  2980. // Last source is needed if avg_source_sad() is used, or if
  2981. // partition_search_type == SOURCE_VAR_BASED_PARTITION, or if noise
  2982. // estimation is enabled.
  2983. if (cpi->unscaled_last_source != NULL &&
  2984. (cpi->oxcf.content == VP9E_CONTENT_SCREEN ||
  2985. (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_VBR &&
  2986. cpi->oxcf.mode == REALTIME && cpi->oxcf.speed >= 5) ||
  2987. cpi->sf.partition_search_type == SOURCE_VAR_BASED_PARTITION ||
  2988. (cpi->noise_estimate.enabled && !cpi->oxcf.noise_sensitivity) ||
  2989. cpi->compute_source_sad_onepass))
  2990. cpi->Last_Source = vp9_scale_if_required(
  2991. cm, cpi->unscaled_last_source, &cpi->scaled_last_source,
  2992. (cpi->oxcf.pass == 0), EIGHTTAP, 0);
  2993. if (cpi->Last_Source == NULL ||
  2994. cpi->Last_Source->y_width != cpi->Source->y_width ||
  2995. cpi->Last_Source->y_height != cpi->Source->y_height)
  2996. cpi->compute_source_sad_onepass = 0;
  2997. if (cm->frame_type == KEY_FRAME || cpi->resize_pending != 0) {
  2998. memset(cpi->consec_zero_mv, 0,
  2999. cm->mi_rows * cm->mi_cols * sizeof(*cpi->consec_zero_mv));
  3000. }
  3001. vp9_update_noise_estimate(cpi);
  3002. // Scene detection is always used for VBR mode or screen-content case.
  3003. // For other cases (e.g., CBR mode) use it for 5 <= speed < 8 for now
  3004. // (need to check encoding time cost for doing this for speed 8).
  3005. if (cpi->compute_source_sad_onepass &&
  3006. (cpi->oxcf.rc_mode == VPX_VBR ||
  3007. cpi->oxcf.content == VP9E_CONTENT_SCREEN ||
  3008. (cpi->oxcf.speed >= 5 && cpi->oxcf.speed < 8)))
  3009. vp9_scene_detection_onepass(cpi);
  3010. // For 1 pass CBR SVC, only ZEROMV is allowed for spatial reference frame
  3011. // when svc->force_zero_mode_spatial_ref = 1. Under those conditions we can
  3012. // avoid this frame-level upsampling (for non intra_only frames).
  3013. if (frame_is_intra_only(cm) == 0 &&
  3014. !(is_one_pass_cbr_svc(cpi) && cpi->svc.force_zero_mode_spatial_ref)) {
  3015. vp9_scale_references(cpi);
  3016. }
  3017. set_size_independent_vars(cpi);
  3018. set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
  3019. if (cpi->sf.copy_partition_flag) alloc_copy_partition_data(cpi);
  3020. if (cpi->oxcf.speed >= 5 && cpi->oxcf.pass == 0 &&
  3021. cpi->oxcf.rc_mode == VPX_CBR &&
  3022. cpi->oxcf.content != VP9E_CONTENT_SCREEN &&
  3023. cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
  3024. cpi->use_skin_detection = 1;
  3025. }
  3026. vp9_set_quantizer(cm, q);
  3027. vp9_set_variance_partition_thresholds(cpi, q, 0);
  3028. setup_frame(cpi);
  3029. suppress_active_map(cpi);
  3030. // Variance adaptive and in frame q adjustment experiments are mutually
  3031. // exclusive.
  3032. if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
  3033. vp9_vaq_frame_setup(cpi);
  3034. } else if (cpi->oxcf.aq_mode == EQUATOR360_AQ) {
  3035. vp9_360aq_frame_setup(cpi);
  3036. } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
  3037. vp9_setup_in_frame_q_adj(cpi);
  3038. } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
  3039. vp9_cyclic_refresh_setup(cpi);
  3040. } else if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ) {
  3041. // it may be pretty bad for rate-control,
  3042. // and I should handle it somehow
  3043. vp9_alt_ref_aq_setup_map(cpi->alt_ref_aq, cpi);
  3044. }
  3045. apply_active_map(cpi);
  3046. vp9_encode_frame(cpi);
  3047. // Check if we should drop this frame because of high overshoot.
  3048. // Only for frames where high temporal-source SAD is detected.
  3049. if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR &&
  3050. cpi->resize_state == ORIG && cm->frame_type != KEY_FRAME &&
  3051. cpi->oxcf.content == VP9E_CONTENT_SCREEN &&
  3052. cpi->rc.high_source_sad == 1) {
  3053. int frame_size = 0;
  3054. // Get an estimate of the encoded frame size.
  3055. save_coding_context(cpi);
  3056. vp9_pack_bitstream(cpi, dest, size);
  3057. restore_coding_context(cpi);
  3058. frame_size = (int)(*size) << 3;
  3059. // Check if encoded frame will overshoot too much, and if so, set the q and
  3060. // adjust some rate control parameters, and return to re-encode the frame.
  3061. if (vp9_encodedframe_overshoot(cpi, frame_size, &q)) {
  3062. vpx_clear_system_state();
  3063. vp9_set_quantizer(cm, q);
  3064. vp9_set_variance_partition_thresholds(cpi, q, 0);
  3065. suppress_active_map(cpi);
  3066. // Turn-off cyclic refresh for re-encoded frame.
  3067. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
  3068. unsigned char *const seg_map = cpi->segmentation_map;
  3069. memset(seg_map, 0, cm->mi_rows * cm->mi_cols);
  3070. vp9_disable_segmentation(&cm->seg);
  3071. }
  3072. apply_active_map(cpi);
  3073. vp9_encode_frame(cpi);
  3074. }
  3075. }
  3076. // Update some stats from cyclic refresh, and check for golden frame update.
  3077. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
  3078. cm->frame_type != KEY_FRAME)
  3079. vp9_cyclic_refresh_postencode(cpi);
  3080. // Update the skip mb flag probabilities based on the distribution
  3081. // seen in the last encoder iteration.
  3082. // update_base_skip_probs(cpi);
  3083. vpx_clear_system_state();
  3084. }
  3085. #define MAX_QSTEP_ADJ 4
  3086. static int get_qstep_adj(int rate_excess, int rate_limit) {
  3087. int qstep =
  3088. rate_limit ? ((rate_excess + rate_limit / 2) / rate_limit) : INT_MAX;
  3089. return VPXMIN(qstep, MAX_QSTEP_ADJ);
  3090. }
  3091. static void encode_with_recode_loop(VP9_COMP *cpi, size_t *size,
  3092. uint8_t *dest) {
  3093. VP9_COMMON *const cm = &cpi->common;
  3094. RATE_CONTROL *const rc = &cpi->rc;
  3095. int bottom_index, top_index;
  3096. int loop_count = 0;
  3097. int loop_at_this_size = 0;
  3098. int loop = 0;
  3099. int overshoot_seen = 0;
  3100. int undershoot_seen = 0;
  3101. int frame_over_shoot_limit;
  3102. int frame_under_shoot_limit;
  3103. int q = 0, q_low = 0, q_high = 0;
  3104. int enable_acl;
  3105. #ifdef AGGRESSIVE_VBR
  3106. int qrange_adj = 1;
  3107. #endif
  3108. set_size_independent_vars(cpi);
  3109. enable_acl = cpi->sf.allow_acl
  3110. ? (cm->frame_type == KEY_FRAME) || (cm->show_frame == 0)
  3111. : 0;
  3112. do {
  3113. vpx_clear_system_state();
  3114. set_frame_size(cpi);
  3115. if (loop_count == 0 || cpi->resize_pending != 0) {
  3116. set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
  3117. #ifdef AGGRESSIVE_VBR
  3118. if (two_pass_first_group_inter(cpi)) {
  3119. // Adjustment limits for min and max q
  3120. qrange_adj = VPXMAX(1, (top_index - bottom_index) / 2);
  3121. bottom_index =
  3122. VPXMAX(bottom_index - qrange_adj / 2, cpi->oxcf.best_allowed_q);
  3123. top_index =
  3124. VPXMIN(cpi->oxcf.worst_allowed_q, top_index + qrange_adj / 2);
  3125. }
  3126. #endif
  3127. // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
  3128. set_mv_search_params(cpi);
  3129. // Reset the loop state for new frame size.
  3130. overshoot_seen = 0;
  3131. undershoot_seen = 0;
  3132. // Reconfiguration for change in frame size has concluded.
  3133. cpi->resize_pending = 0;
  3134. q_low = bottom_index;
  3135. q_high = top_index;
  3136. loop_at_this_size = 0;
  3137. }
  3138. // Decide frame size bounds first time through.
  3139. if (loop_count == 0) {
  3140. vp9_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
  3141. &frame_under_shoot_limit,
  3142. &frame_over_shoot_limit);
  3143. }
  3144. cpi->Source =
  3145. vp9_scale_if_required(cm, cpi->un_scaled_source, &cpi->scaled_source,
  3146. (cpi->oxcf.pass == 0), EIGHTTAP, 0);
  3147. // Unfiltered raw source used in metrics calculation if the source
  3148. // has been filtered.
  3149. if (is_psnr_calc_enabled(cpi)) {
  3150. #ifdef ENABLE_KF_DENOISE
  3151. if (is_spatial_denoise_enabled(cpi)) {
  3152. cpi->raw_source_frame = vp9_scale_if_required(
  3153. cm, &cpi->raw_unscaled_source, &cpi->raw_scaled_source,
  3154. (cpi->oxcf.pass == 0), EIGHTTAP, 0);
  3155. } else {
  3156. cpi->raw_source_frame = cpi->Source;
  3157. }
  3158. #else
  3159. cpi->raw_source_frame = cpi->Source;
  3160. #endif
  3161. }
  3162. if (cpi->unscaled_last_source != NULL)
  3163. cpi->Last_Source = vp9_scale_if_required(
  3164. cm, cpi->unscaled_last_source, &cpi->scaled_last_source,
  3165. (cpi->oxcf.pass == 0), EIGHTTAP, 0);
  3166. if (frame_is_intra_only(cm) == 0) {
  3167. if (loop_count > 0) {
  3168. release_scaled_references(cpi);
  3169. }
  3170. vp9_scale_references(cpi);
  3171. }
  3172. vp9_set_quantizer(cm, q);
  3173. if (loop_count == 0) setup_frame(cpi);
  3174. // Variance adaptive and in frame q adjustment experiments are mutually
  3175. // exclusive.
  3176. if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
  3177. vp9_vaq_frame_setup(cpi);
  3178. } else if (cpi->oxcf.aq_mode == EQUATOR360_AQ) {
  3179. vp9_360aq_frame_setup(cpi);
  3180. } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
  3181. vp9_setup_in_frame_q_adj(cpi);
  3182. } else if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ) {
  3183. vp9_alt_ref_aq_setup_map(cpi->alt_ref_aq, cpi);
  3184. }
  3185. vp9_encode_frame(cpi);
  3186. // Update the skip mb flag probabilities based on the distribution
  3187. // seen in the last encoder iteration.
  3188. // update_base_skip_probs(cpi);
  3189. vpx_clear_system_state();
  3190. // Dummy pack of the bitstream using up to date stats to get an
  3191. // accurate estimate of output frame size to determine if we need
  3192. // to recode.
  3193. if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
  3194. save_coding_context(cpi);
  3195. if (!cpi->sf.use_nonrd_pick_mode) vp9_pack_bitstream(cpi, dest, size);
  3196. rc->projected_frame_size = (int)(*size) << 3;
  3197. if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
  3198. }
  3199. if (cpi->oxcf.rc_mode == VPX_Q) {
  3200. loop = 0;
  3201. } else {
  3202. if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
  3203. (rc->projected_frame_size < rc->max_frame_bandwidth)) {
  3204. int last_q = q;
  3205. int64_t kf_err;
  3206. int64_t high_err_target = cpi->ambient_err;
  3207. int64_t low_err_target = cpi->ambient_err >> 1;
  3208. #if CONFIG_VP9_HIGHBITDEPTH
  3209. if (cm->use_highbitdepth) {
  3210. kf_err = vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
  3211. } else {
  3212. kf_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
  3213. }
  3214. #else
  3215. kf_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
  3216. #endif // CONFIG_VP9_HIGHBITDEPTH
  3217. // Prevent possible divide by zero error below for perfect KF
  3218. kf_err += !kf_err;
  3219. // The key frame is not good enough or we can afford
  3220. // to make it better without undue risk of popping.
  3221. if ((kf_err > high_err_target &&
  3222. rc->projected_frame_size <= frame_over_shoot_limit) ||
  3223. (kf_err > low_err_target &&
  3224. rc->projected_frame_size <= frame_under_shoot_limit)) {
  3225. // Lower q_high
  3226. q_high = q > q_low ? q - 1 : q_low;
  3227. // Adjust Q
  3228. q = (int)((q * high_err_target) / kf_err);
  3229. q = VPXMIN(q, (q_high + q_low) >> 1);
  3230. } else if (kf_err < low_err_target &&
  3231. rc->projected_frame_size >= frame_under_shoot_limit) {
  3232. // The key frame is much better than the previous frame
  3233. // Raise q_low
  3234. q_low = q < q_high ? q + 1 : q_high;
  3235. // Adjust Q
  3236. q = (int)((q * low_err_target) / kf_err);
  3237. q = VPXMIN(q, (q_high + q_low + 1) >> 1);
  3238. }
  3239. // Clamp Q to upper and lower limits:
  3240. q = clamp(q, q_low, q_high);
  3241. loop = q != last_q;
  3242. } else if (recode_loop_test(cpi, frame_over_shoot_limit,
  3243. frame_under_shoot_limit, q,
  3244. VPXMAX(q_high, top_index), bottom_index)) {
  3245. // Is the projected frame size out of range and are we allowed
  3246. // to attempt to recode.
  3247. int last_q = q;
  3248. int retries = 0;
  3249. int qstep;
  3250. if (cpi->resize_pending == 1) {
  3251. // Change in frame size so go back around the recode loop.
  3252. cpi->rc.frame_size_selector =
  3253. SCALE_STEP1 - cpi->rc.frame_size_selector;
  3254. cpi->rc.next_frame_size_selector = cpi->rc.frame_size_selector;
  3255. #if CONFIG_INTERNAL_STATS
  3256. ++cpi->tot_recode_hits;
  3257. #endif
  3258. ++loop_count;
  3259. loop = 1;
  3260. continue;
  3261. }
  3262. // Frame size out of permitted range:
  3263. // Update correction factor & compute new Q to try...
  3264. // Frame is too large
  3265. if (rc->projected_frame_size > rc->this_frame_target) {
  3266. // Special case if the projected size is > the max allowed.
  3267. if ((q == q_high) &&
  3268. ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
  3269. (rc->projected_frame_size >=
  3270. big_rate_miss_high_threshold(cpi)))) {
  3271. int max_rate = VPXMAX(1, VPXMIN(rc->max_frame_bandwidth,
  3272. big_rate_miss_high_threshold(cpi)));
  3273. double q_val_high;
  3274. q_val_high = vp9_convert_qindex_to_q(q_high, cm->bit_depth);
  3275. q_val_high =
  3276. q_val_high * ((double)rc->projected_frame_size / max_rate);
  3277. q_high = vp9_convert_q_to_qindex(q_val_high, cm->bit_depth);
  3278. q_high = clamp(q_high, rc->best_quality, rc->worst_quality);
  3279. }
  3280. // Raise Qlow as to at least the current value
  3281. qstep =
  3282. get_qstep_adj(rc->projected_frame_size, rc->this_frame_target);
  3283. q_low = VPXMIN(q + qstep, q_high);
  3284. if (undershoot_seen || loop_at_this_size > 1) {
  3285. // Update rate_correction_factor unless
  3286. vp9_rc_update_rate_correction_factors(cpi);
  3287. q = (q_high + q_low + 1) / 2;
  3288. } else {
  3289. // Update rate_correction_factor unless
  3290. vp9_rc_update_rate_correction_factors(cpi);
  3291. q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
  3292. VPXMAX(q_high, top_index));
  3293. while (q < q_low && retries < 10) {
  3294. vp9_rc_update_rate_correction_factors(cpi);
  3295. q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
  3296. VPXMAX(q_high, top_index));
  3297. retries++;
  3298. }
  3299. }
  3300. overshoot_seen = 1;
  3301. } else {
  3302. // Frame is too small
  3303. qstep =
  3304. get_qstep_adj(rc->this_frame_target, rc->projected_frame_size);
  3305. q_high = VPXMAX(q - qstep, q_low);
  3306. if (overshoot_seen || loop_at_this_size > 1) {
  3307. vp9_rc_update_rate_correction_factors(cpi);
  3308. q = (q_high + q_low) / 2;
  3309. } else {
  3310. vp9_rc_update_rate_correction_factors(cpi);
  3311. q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
  3312. VPXMIN(q_low, bottom_index), top_index);
  3313. // Special case reset for qlow for constrained quality.
  3314. // This should only trigger where there is very substantial
  3315. // undershoot on a frame and the auto cq level is above
  3316. // the user passsed in value.
  3317. if (cpi->oxcf.rc_mode == VPX_CQ && q < q_low) {
  3318. q_low = q;
  3319. }
  3320. while (q > q_high && retries < 10) {
  3321. vp9_rc_update_rate_correction_factors(cpi);
  3322. q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
  3323. VPXMIN(q_low, bottom_index), top_index);
  3324. retries++;
  3325. }
  3326. }
  3327. undershoot_seen = 1;
  3328. }
  3329. // Clamp Q to upper and lower limits:
  3330. q = clamp(q, q_low, q_high);
  3331. loop = (q != last_q);
  3332. } else {
  3333. loop = 0;
  3334. }
  3335. }
  3336. // Special case for overlay frame.
  3337. if (rc->is_src_frame_alt_ref &&
  3338. rc->projected_frame_size < rc->max_frame_bandwidth)
  3339. loop = 0;
  3340. if (loop) {
  3341. ++loop_count;
  3342. ++loop_at_this_size;
  3343. #if CONFIG_INTERNAL_STATS
  3344. ++cpi->tot_recode_hits;
  3345. #endif
  3346. }
  3347. if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF)
  3348. if (loop || !enable_acl) restore_coding_context(cpi);
  3349. } while (loop);
  3350. #ifdef AGGRESSIVE_VBR
  3351. if (two_pass_first_group_inter(cpi)) {
  3352. cpi->twopass.active_worst_quality =
  3353. VPXMIN(q + qrange_adj, cpi->oxcf.worst_allowed_q);
  3354. } else if (!frame_is_kf_gf_arf(cpi)) {
  3355. #else
  3356. if (!frame_is_kf_gf_arf(cpi)) {
  3357. #endif
  3358. // Have we been forced to adapt Q outside the expected range by an extreme
  3359. // rate miss. If so adjust the active maxQ for the subsequent frames.
  3360. if (q > cpi->twopass.active_worst_quality) {
  3361. cpi->twopass.active_worst_quality = q;
  3362. }
  3363. }
  3364. if (enable_acl) {
  3365. // Skip recoding, if model diff is below threshold
  3366. const int thresh = compute_context_model_thresh(cpi);
  3367. const int diff = compute_context_model_diff(cm);
  3368. if (diff < thresh) {
  3369. vpx_clear_system_state();
  3370. restore_coding_context(cpi);
  3371. return;
  3372. }
  3373. vp9_encode_frame(cpi);
  3374. vpx_clear_system_state();
  3375. restore_coding_context(cpi);
  3376. vp9_pack_bitstream(cpi, dest, size);
  3377. vp9_encode_frame(cpi);
  3378. vpx_clear_system_state();
  3379. restore_coding_context(cpi);
  3380. }
  3381. }
  3382. static int get_ref_frame_flags(const VP9_COMP *cpi) {
  3383. const int *const map = cpi->common.ref_frame_map;
  3384. const int gold_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idx];
  3385. const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idx];
  3386. const int gold_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
  3387. int flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
  3388. if (gold_is_last) flags &= ~VP9_GOLD_FLAG;
  3389. if (cpi->rc.frames_till_gf_update_due == INT_MAX &&
  3390. (cpi->svc.number_temporal_layers == 1 &&
  3391. cpi->svc.number_spatial_layers == 1))
  3392. flags &= ~VP9_GOLD_FLAG;
  3393. if (alt_is_last) flags &= ~VP9_ALT_FLAG;
  3394. if (gold_is_alt) flags &= ~VP9_ALT_FLAG;
  3395. return flags;
  3396. }
  3397. static void set_ext_overrides(VP9_COMP *cpi) {
  3398. // Overrides the defaults with the externally supplied values with
  3399. // vp9_update_reference() and vp9_update_entropy() calls
  3400. // Note: The overrides are valid only for the next frame passed
  3401. // to encode_frame_to_data_rate() function
  3402. if (cpi->ext_refresh_frame_context_pending) {
  3403. cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
  3404. cpi->ext_refresh_frame_context_pending = 0;
  3405. }
  3406. if (cpi->ext_refresh_frame_flags_pending) {
  3407. cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
  3408. cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
  3409. cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
  3410. }
  3411. }
  3412. YV12_BUFFER_CONFIG *vp9_svc_twostage_scale(
  3413. VP9_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled,
  3414. YV12_BUFFER_CONFIG *scaled_temp, INTERP_FILTER filter_type,
  3415. int phase_scaler, INTERP_FILTER filter_type2, int phase_scaler2) {
  3416. if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
  3417. cm->mi_rows * MI_SIZE != unscaled->y_height) {
  3418. #if CONFIG_VP9_HIGHBITDEPTH
  3419. if (cm->bit_depth == VPX_BITS_8) {
  3420. vp9_scale_and_extend_frame(unscaled, scaled_temp, filter_type2,
  3421. phase_scaler2);
  3422. vp9_scale_and_extend_frame(scaled_temp, scaled, filter_type,
  3423. phase_scaler);
  3424. } else {
  3425. scale_and_extend_frame(unscaled, scaled_temp, (int)cm->bit_depth,
  3426. filter_type2, phase_scaler2);
  3427. scale_and_extend_frame(scaled_temp, scaled, (int)cm->bit_depth,
  3428. filter_type, phase_scaler);
  3429. }
  3430. #else
  3431. vp9_scale_and_extend_frame(unscaled, scaled_temp, filter_type2,
  3432. phase_scaler2);
  3433. vp9_scale_and_extend_frame(scaled_temp, scaled, filter_type, phase_scaler);
  3434. #endif // CONFIG_VP9_HIGHBITDEPTH
  3435. return scaled;
  3436. } else {
  3437. return unscaled;
  3438. }
  3439. }
  3440. YV12_BUFFER_CONFIG *vp9_scale_if_required(
  3441. VP9_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled,
  3442. int use_normative_scaler, INTERP_FILTER filter_type, int phase_scaler) {
  3443. if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
  3444. cm->mi_rows * MI_SIZE != unscaled->y_height) {
  3445. #if CONFIG_VP9_HIGHBITDEPTH
  3446. if (use_normative_scaler && unscaled->y_width <= (scaled->y_width << 1) &&
  3447. unscaled->y_height <= (scaled->y_height << 1))
  3448. if (cm->bit_depth == VPX_BITS_8)
  3449. vp9_scale_and_extend_frame(unscaled, scaled, filter_type, phase_scaler);
  3450. else
  3451. scale_and_extend_frame(unscaled, scaled, (int)cm->bit_depth,
  3452. filter_type, phase_scaler);
  3453. else
  3454. scale_and_extend_frame_nonnormative(unscaled, scaled, (int)cm->bit_depth);
  3455. #else
  3456. if (use_normative_scaler && unscaled->y_width <= (scaled->y_width << 1) &&
  3457. unscaled->y_height <= (scaled->y_height << 1))
  3458. vp9_scale_and_extend_frame(unscaled, scaled, filter_type, phase_scaler);
  3459. else
  3460. scale_and_extend_frame_nonnormative(unscaled, scaled);
  3461. #endif // CONFIG_VP9_HIGHBITDEPTH
  3462. return scaled;
  3463. } else {
  3464. return unscaled;
  3465. }
  3466. }
  3467. static void set_arf_sign_bias(VP9_COMP *cpi) {
  3468. VP9_COMMON *const cm = &cpi->common;
  3469. int arf_sign_bias;
  3470. if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
  3471. const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
  3472. arf_sign_bias = cpi->rc.source_alt_ref_active &&
  3473. (!cpi->refresh_alt_ref_frame ||
  3474. (gf_group->rf_level[gf_group->index] == GF_ARF_LOW));
  3475. } else {
  3476. arf_sign_bias =
  3477. (cpi->rc.source_alt_ref_active && !cpi->refresh_alt_ref_frame);
  3478. }
  3479. cm->ref_frame_sign_bias[ALTREF_FRAME] = arf_sign_bias;
  3480. }
  3481. static int setup_interp_filter_search_mask(VP9_COMP *cpi) {
  3482. INTERP_FILTER ifilter;
  3483. int ref_total[MAX_REF_FRAMES] = { 0 };
  3484. MV_REFERENCE_FRAME ref;
  3485. int mask = 0;
  3486. if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
  3487. return mask;
  3488. for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
  3489. for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter)
  3490. ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
  3491. for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter) {
  3492. if ((ref_total[LAST_FRAME] &&
  3493. cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
  3494. (ref_total[GOLDEN_FRAME] == 0 ||
  3495. cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50 <
  3496. ref_total[GOLDEN_FRAME]) &&
  3497. (ref_total[ALTREF_FRAME] == 0 ||
  3498. cpi->interp_filter_selected[ALTREF_FRAME][ifilter] * 50 <
  3499. ref_total[ALTREF_FRAME]))
  3500. mask |= 1 << ifilter;
  3501. }
  3502. return mask;
  3503. }
  3504. #ifdef ENABLE_KF_DENOISE
  3505. // Baseline Kernal weights for denoise
  3506. static uint8_t dn_kernal_3[9] = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
  3507. static uint8_t dn_kernal_5[25] = { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 4,
  3508. 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1 };
  3509. static INLINE void add_denoise_point(int centre_val, int data_val, int thresh,
  3510. uint8_t point_weight, int *sum_val,
  3511. int *sum_weight) {
  3512. if (abs(centre_val - data_val) <= thresh) {
  3513. *sum_weight += point_weight;
  3514. *sum_val += (int)data_val * (int)point_weight;
  3515. }
  3516. }
  3517. static void spatial_denoise_point(uint8_t *src_ptr, const int stride,
  3518. const int strength) {
  3519. int sum_weight = 0;
  3520. int sum_val = 0;
  3521. int thresh = strength;
  3522. int kernal_size = 5;
  3523. int half_k_size = 2;
  3524. int i, j;
  3525. int max_diff = 0;
  3526. uint8_t *tmp_ptr;
  3527. uint8_t *kernal_ptr;
  3528. // Find the maximum deviation from the source point in the locale.
  3529. tmp_ptr = src_ptr - (stride * (half_k_size + 1)) - (half_k_size + 1);
  3530. for (i = 0; i < kernal_size + 2; ++i) {
  3531. for (j = 0; j < kernal_size + 2; ++j) {
  3532. max_diff = VPXMAX(max_diff, abs((int)*src_ptr - (int)tmp_ptr[j]));
  3533. }
  3534. tmp_ptr += stride;
  3535. }
  3536. // Select the kernal size.
  3537. if (max_diff > (strength + (strength >> 1))) {
  3538. kernal_size = 3;
  3539. half_k_size = 1;
  3540. thresh = thresh >> 1;
  3541. }
  3542. kernal_ptr = (kernal_size == 3) ? dn_kernal_3 : dn_kernal_5;
  3543. // Apply the kernal
  3544. tmp_ptr = src_ptr - (stride * half_k_size) - half_k_size;
  3545. for (i = 0; i < kernal_size; ++i) {
  3546. for (j = 0; j < kernal_size; ++j) {
  3547. add_denoise_point((int)*src_ptr, (int)tmp_ptr[j], thresh, *kernal_ptr,
  3548. &sum_val, &sum_weight);
  3549. ++kernal_ptr;
  3550. }
  3551. tmp_ptr += stride;
  3552. }
  3553. // Update the source value with the new filtered value
  3554. *src_ptr = (uint8_t)((sum_val + (sum_weight >> 1)) / sum_weight);
  3555. }
  3556. #if CONFIG_VP9_HIGHBITDEPTH
  3557. static void highbd_spatial_denoise_point(uint16_t *src_ptr, const int stride,
  3558. const int strength) {
  3559. int sum_weight = 0;
  3560. int sum_val = 0;
  3561. int thresh = strength;
  3562. int kernal_size = 5;
  3563. int half_k_size = 2;
  3564. int i, j;
  3565. int max_diff = 0;
  3566. uint16_t *tmp_ptr;
  3567. uint8_t *kernal_ptr;
  3568. // Find the maximum deviation from the source point in the locale.
  3569. tmp_ptr = src_ptr - (stride * (half_k_size + 1)) - (half_k_size + 1);
  3570. for (i = 0; i < kernal_size + 2; ++i) {
  3571. for (j = 0; j < kernal_size + 2; ++j) {
  3572. max_diff = VPXMAX(max_diff, abs((int)src_ptr - (int)tmp_ptr[j]));
  3573. }
  3574. tmp_ptr += stride;
  3575. }
  3576. // Select the kernal size.
  3577. if (max_diff > (strength + (strength >> 1))) {
  3578. kernal_size = 3;
  3579. half_k_size = 1;
  3580. thresh = thresh >> 1;
  3581. }
  3582. kernal_ptr = (kernal_size == 3) ? dn_kernal_3 : dn_kernal_5;
  3583. // Apply the kernal
  3584. tmp_ptr = src_ptr - (stride * half_k_size) - half_k_size;
  3585. for (i = 0; i < kernal_size; ++i) {
  3586. for (j = 0; j < kernal_size; ++j) {
  3587. add_denoise_point((int)*src_ptr, (int)tmp_ptr[j], thresh, *kernal_ptr,
  3588. &sum_val, &sum_weight);
  3589. ++kernal_ptr;
  3590. }
  3591. tmp_ptr += stride;
  3592. }
  3593. // Update the source value with the new filtered value
  3594. *src_ptr = (uint16_t)((sum_val + (sum_weight >> 1)) / sum_weight);
  3595. }
  3596. #endif // CONFIG_VP9_HIGHBITDEPTH
  3597. // Apply thresholded spatial noise supression to a given buffer.
  3598. static void spatial_denoise_buffer(VP9_COMP *cpi, uint8_t *buffer,
  3599. const int stride, const int width,
  3600. const int height, const int strength) {
  3601. VP9_COMMON *const cm = &cpi->common;
  3602. uint8_t *src_ptr = buffer;
  3603. int row;
  3604. int col;
  3605. for (row = 0; row < height; ++row) {
  3606. for (col = 0; col < width; ++col) {
  3607. #if CONFIG_VP9_HIGHBITDEPTH
  3608. if (cm->use_highbitdepth)
  3609. highbd_spatial_denoise_point(CONVERT_TO_SHORTPTR(&src_ptr[col]), stride,
  3610. strength);
  3611. else
  3612. spatial_denoise_point(&src_ptr[col], stride, strength);
  3613. #else
  3614. spatial_denoise_point(&src_ptr[col], stride, strength);
  3615. #endif // CONFIG_VP9_HIGHBITDEPTH
  3616. }
  3617. src_ptr += stride;
  3618. }
  3619. }
  3620. // Apply thresholded spatial noise supression to source.
  3621. static void spatial_denoise_frame(VP9_COMP *cpi) {
  3622. YV12_BUFFER_CONFIG *src = cpi->Source;
  3623. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  3624. TWO_PASS *const twopass = &cpi->twopass;
  3625. VP9_COMMON *const cm = &cpi->common;
  3626. // Base the filter strength on the current active max Q.
  3627. const int q = (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality,
  3628. cm->bit_depth));
  3629. int strength =
  3630. VPXMAX(oxcf->arnr_strength >> 2, VPXMIN(oxcf->arnr_strength, (q >> 4)));
  3631. // Denoise each of Y,U and V buffers.
  3632. spatial_denoise_buffer(cpi, src->y_buffer, src->y_stride, src->y_width,
  3633. src->y_height, strength);
  3634. strength += (strength >> 1);
  3635. spatial_denoise_buffer(cpi, src->u_buffer, src->uv_stride, src->uv_width,
  3636. src->uv_height, strength << 1);
  3637. spatial_denoise_buffer(cpi, src->v_buffer, src->uv_stride, src->uv_width,
  3638. src->uv_height, strength << 1);
  3639. }
  3640. #endif // ENABLE_KF_DENOISE
  3641. static void vp9_try_disable_lookahead_aq(VP9_COMP *cpi, size_t *size,
  3642. uint8_t *dest) {
  3643. if (cpi->common.seg.enabled)
  3644. if (ALT_REF_AQ_PROTECT_GAIN) {
  3645. size_t nsize = *size;
  3646. int overhead;
  3647. // TODO(yuryg): optimize this, as
  3648. // we don't really need to repack
  3649. save_coding_context(cpi);
  3650. vp9_disable_segmentation(&cpi->common.seg);
  3651. vp9_pack_bitstream(cpi, dest, &nsize);
  3652. restore_coding_context(cpi);
  3653. overhead = (int)*size - (int)nsize;
  3654. if (vp9_alt_ref_aq_disable_if(cpi->alt_ref_aq, overhead, (int)*size))
  3655. vp9_encode_frame(cpi);
  3656. else
  3657. vp9_enable_segmentation(&cpi->common.seg);
  3658. }
  3659. }
  3660. static void encode_frame_to_data_rate(VP9_COMP *cpi, size_t *size,
  3661. uint8_t *dest,
  3662. unsigned int *frame_flags) {
  3663. VP9_COMMON *const cm = &cpi->common;
  3664. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  3665. struct segmentation *const seg = &cm->seg;
  3666. TX_SIZE t;
  3667. set_ext_overrides(cpi);
  3668. vpx_clear_system_state();
  3669. #ifdef ENABLE_KF_DENOISE
  3670. // Spatial denoise of key frame.
  3671. if (is_spatial_denoise_enabled(cpi)) spatial_denoise_frame(cpi);
  3672. #endif
  3673. // Set the arf sign bias for this frame.
  3674. set_arf_sign_bias(cpi);
  3675. // Set default state for segment based loop filter update flags.
  3676. cm->lf.mode_ref_delta_update = 0;
  3677. if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
  3678. cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
  3679. // Set various flags etc to special state if it is a key frame.
  3680. if (frame_is_intra_only(cm)) {
  3681. // Reset the loop filter deltas and segmentation map.
  3682. vp9_reset_segment_features(&cm->seg);
  3683. // If segmentation is enabled force a map update for key frames.
  3684. if (seg->enabled) {
  3685. seg->update_map = 1;
  3686. seg->update_data = 1;
  3687. }
  3688. // The alternate reference frame cannot be active for a key frame.
  3689. cpi->rc.source_alt_ref_active = 0;
  3690. cm->error_resilient_mode = oxcf->error_resilient_mode;
  3691. cm->frame_parallel_decoding_mode = oxcf->frame_parallel_decoding_mode;
  3692. // By default, encoder assumes decoder can use prev_mi.
  3693. if (cm->error_resilient_mode) {
  3694. cm->frame_parallel_decoding_mode = 1;
  3695. cm->reset_frame_context = 0;
  3696. cm->refresh_frame_context = 0;
  3697. } else if (cm->intra_only) {
  3698. // Only reset the current context.
  3699. cm->reset_frame_context = 2;
  3700. }
  3701. }
  3702. if (is_two_pass_svc(cpi) && cm->error_resilient_mode == 0) {
  3703. // Use context 0 for intra only empty frame, but the last frame context
  3704. // for other empty frames.
  3705. if (cpi->svc.encode_empty_frame_state == ENCODING) {
  3706. if (cpi->svc.encode_intra_empty_frame != 0)
  3707. cm->frame_context_idx = 0;
  3708. else
  3709. cm->frame_context_idx = FRAME_CONTEXTS - 1;
  3710. } else {
  3711. cm->frame_context_idx =
  3712. cpi->svc.spatial_layer_id * cpi->svc.number_temporal_layers +
  3713. cpi->svc.temporal_layer_id;
  3714. }
  3715. cm->frame_parallel_decoding_mode = oxcf->frame_parallel_decoding_mode;
  3716. // The probs will be updated based on the frame type of its previous
  3717. // frame if frame_parallel_decoding_mode is 0. The type may vary for
  3718. // the frame after a key frame in base layer since we may drop enhancement
  3719. // layers. So set frame_parallel_decoding_mode to 1 in this case.
  3720. if (cm->frame_parallel_decoding_mode == 0) {
  3721. if (cpi->svc.number_temporal_layers == 1) {
  3722. if (cpi->svc.spatial_layer_id == 0 &&
  3723. cpi->svc.layer_context[0].last_frame_type == KEY_FRAME)
  3724. cm->frame_parallel_decoding_mode = 1;
  3725. } else if (cpi->svc.spatial_layer_id == 0) {
  3726. // Find the 2nd frame in temporal base layer and 1st frame in temporal
  3727. // enhancement layers from the key frame.
  3728. int i;
  3729. for (i = 0; i < cpi->svc.number_temporal_layers; ++i) {
  3730. if (cpi->svc.layer_context[0].frames_from_key_frame == 1 << i) {
  3731. cm->frame_parallel_decoding_mode = 1;
  3732. break;
  3733. }
  3734. }
  3735. }
  3736. }
  3737. }
  3738. // For 1 pass CBR, check if we are dropping this frame.
  3739. // For spatial layers, for now only check for frame-dropping on first spatial
  3740. // layer, and if decision is to drop, we drop whole super-frame.
  3741. if (oxcf->pass == 0 && oxcf->rc_mode == VPX_CBR &&
  3742. cm->frame_type != KEY_FRAME) {
  3743. if (vp9_rc_drop_frame(cpi) ||
  3744. (is_one_pass_cbr_svc(cpi) && cpi->svc.rc_drop_superframe == 1)) {
  3745. vp9_rc_postencode_update_drop_frame(cpi);
  3746. ++cm->current_video_frame;
  3747. cpi->ext_refresh_frame_flags_pending = 0;
  3748. cpi->svc.rc_drop_superframe = 1;
  3749. cpi->last_frame_dropped = 1;
  3750. // TODO(marpan): Advancing the svc counters on dropped frames can break
  3751. // the referencing scheme for the fixed svc patterns defined in
  3752. // vp9_one_pass_cbr_svc_start_layer(). Look into fixing this issue, but
  3753. // for now, don't advance the svc frame counters on dropped frame.
  3754. // if (cpi->use_svc)
  3755. // vp9_inc_frame_in_layer(cpi);
  3756. return;
  3757. }
  3758. }
  3759. vpx_clear_system_state();
  3760. #if CONFIG_INTERNAL_STATS
  3761. memset(cpi->mode_chosen_counts, 0,
  3762. MAX_MODES * sizeof(*cpi->mode_chosen_counts));
  3763. #endif
  3764. if (cpi->sf.recode_loop == DISALLOW_RECODE) {
  3765. encode_without_recode_loop(cpi, size, dest);
  3766. } else {
  3767. encode_with_recode_loop(cpi, size, dest);
  3768. }
  3769. cpi->last_frame_dropped = 0;
  3770. // Disable segmentation if it decrease rate/distortion ratio
  3771. if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ)
  3772. vp9_try_disable_lookahead_aq(cpi, size, dest);
  3773. #if CONFIG_VP9_TEMPORAL_DENOISING
  3774. #ifdef OUTPUT_YUV_DENOISED
  3775. if (oxcf->noise_sensitivity > 0 && denoise_svc(cpi)) {
  3776. vpx_write_yuv_frame(yuv_denoised_file,
  3777. &cpi->denoiser.running_avg_y[INTRA_FRAME]);
  3778. }
  3779. #endif
  3780. #endif
  3781. #ifdef OUTPUT_YUV_SKINMAP
  3782. if (cpi->common.current_video_frame > 1) {
  3783. vp9_output_skin_map(cpi, yuv_skinmap_file);
  3784. }
  3785. #endif
  3786. // Special case code to reduce pulsing when key frames are forced at a
  3787. // fixed interval. Note the reconstruction error if it is the frame before
  3788. // the force key frame
  3789. if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
  3790. #if CONFIG_VP9_HIGHBITDEPTH
  3791. if (cm->use_highbitdepth) {
  3792. cpi->ambient_err =
  3793. vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
  3794. } else {
  3795. cpi->ambient_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
  3796. }
  3797. #else
  3798. cpi->ambient_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
  3799. #endif // CONFIG_VP9_HIGHBITDEPTH
  3800. }
  3801. // If the encoder forced a KEY_FRAME decision
  3802. if (cm->frame_type == KEY_FRAME) cpi->refresh_last_frame = 1;
  3803. cm->frame_to_show = get_frame_new_buffer(cm);
  3804. cm->frame_to_show->color_space = cm->color_space;
  3805. cm->frame_to_show->color_range = cm->color_range;
  3806. cm->frame_to_show->render_width = cm->render_width;
  3807. cm->frame_to_show->render_height = cm->render_height;
  3808. // Pick the loop filter level for the frame.
  3809. loopfilter_frame(cpi, cm);
  3810. // build the bitstream
  3811. vp9_pack_bitstream(cpi, dest, size);
  3812. if (cm->seg.update_map) update_reference_segmentation_map(cpi);
  3813. if (frame_is_intra_only(cm) == 0) {
  3814. release_scaled_references(cpi);
  3815. }
  3816. vp9_update_reference_frames(cpi);
  3817. for (t = TX_4X4; t <= TX_32X32; t++)
  3818. full_to_model_counts(cpi->td.counts->coef[t],
  3819. cpi->td.rd_counts.coef_counts[t]);
  3820. if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode)
  3821. vp9_adapt_coef_probs(cm);
  3822. if (!frame_is_intra_only(cm)) {
  3823. if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
  3824. vp9_adapt_mode_probs(cm);
  3825. vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
  3826. }
  3827. }
  3828. cpi->ext_refresh_frame_flags_pending = 0;
  3829. if (cpi->refresh_golden_frame == 1)
  3830. cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
  3831. else
  3832. cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
  3833. if (cpi->refresh_alt_ref_frame == 1)
  3834. cpi->frame_flags |= FRAMEFLAGS_ALTREF;
  3835. else
  3836. cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
  3837. cpi->ref_frame_flags = get_ref_frame_flags(cpi);
  3838. cm->last_frame_type = cm->frame_type;
  3839. if (!(is_two_pass_svc(cpi) && cpi->svc.encode_empty_frame_state == ENCODING))
  3840. vp9_rc_postencode_update(cpi, *size);
  3841. #if 0
  3842. output_frame_level_debug_stats(cpi);
  3843. #endif
  3844. if (cm->frame_type == KEY_FRAME) {
  3845. // Tell the caller that the frame was coded as a key frame
  3846. *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
  3847. } else {
  3848. *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
  3849. }
  3850. // Clear the one shot update flags for segmentation map and mode/ref loop
  3851. // filter deltas.
  3852. cm->seg.update_map = 0;
  3853. cm->seg.update_data = 0;
  3854. cm->lf.mode_ref_delta_update = 0;
  3855. // keep track of the last coded dimensions
  3856. cm->last_width = cm->width;
  3857. cm->last_height = cm->height;
  3858. // reset to normal state now that we are done.
  3859. if (!cm->show_existing_frame) cm->last_show_frame = cm->show_frame;
  3860. if (cm->show_frame) {
  3861. vp9_swap_mi_and_prev_mi(cm);
  3862. // Don't increment frame counters if this was an altref buffer
  3863. // update not a real frame
  3864. ++cm->current_video_frame;
  3865. if (cpi->use_svc) vp9_inc_frame_in_layer(cpi);
  3866. }
  3867. cm->prev_frame = cm->cur_frame;
  3868. if (cpi->use_svc)
  3869. cpi->svc
  3870. .layer_context[cpi->svc.spatial_layer_id *
  3871. cpi->svc.number_temporal_layers +
  3872. cpi->svc.temporal_layer_id]
  3873. .last_frame_type = cm->frame_type;
  3874. cpi->force_update_segmentation = 0;
  3875. if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ)
  3876. vp9_alt_ref_aq_unset_all(cpi->alt_ref_aq, cpi);
  3877. }
  3878. static void SvcEncode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
  3879. unsigned int *frame_flags) {
  3880. vp9_rc_get_svc_params(cpi);
  3881. encode_frame_to_data_rate(cpi, size, dest, frame_flags);
  3882. }
  3883. static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
  3884. unsigned int *frame_flags) {
  3885. if (cpi->oxcf.rc_mode == VPX_CBR) {
  3886. vp9_rc_get_one_pass_cbr_params(cpi);
  3887. } else {
  3888. vp9_rc_get_one_pass_vbr_params(cpi);
  3889. }
  3890. encode_frame_to_data_rate(cpi, size, dest, frame_flags);
  3891. }
  3892. #if !CONFIG_REALTIME_ONLY
  3893. static void Pass2Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
  3894. unsigned int *frame_flags) {
  3895. cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
  3896. encode_frame_to_data_rate(cpi, size, dest, frame_flags);
  3897. if (!(is_two_pass_svc(cpi) && cpi->svc.encode_empty_frame_state == ENCODING))
  3898. vp9_twopass_postencode_update(cpi);
  3899. }
  3900. #endif // !CONFIG_REALTIME_ONLY
  3901. static void init_ref_frame_bufs(VP9_COMMON *cm) {
  3902. int i;
  3903. BufferPool *const pool = cm->buffer_pool;
  3904. cm->new_fb_idx = INVALID_IDX;
  3905. for (i = 0; i < REF_FRAMES; ++i) {
  3906. cm->ref_frame_map[i] = INVALID_IDX;
  3907. pool->frame_bufs[i].ref_count = 0;
  3908. }
  3909. }
  3910. static void check_initial_width(VP9_COMP *cpi,
  3911. #if CONFIG_VP9_HIGHBITDEPTH
  3912. int use_highbitdepth,
  3913. #endif
  3914. int subsampling_x, int subsampling_y) {
  3915. VP9_COMMON *const cm = &cpi->common;
  3916. if (!cpi->initial_width ||
  3917. #if CONFIG_VP9_HIGHBITDEPTH
  3918. cm->use_highbitdepth != use_highbitdepth ||
  3919. #endif
  3920. cm->subsampling_x != subsampling_x ||
  3921. cm->subsampling_y != subsampling_y) {
  3922. cm->subsampling_x = subsampling_x;
  3923. cm->subsampling_y = subsampling_y;
  3924. #if CONFIG_VP9_HIGHBITDEPTH
  3925. cm->use_highbitdepth = use_highbitdepth;
  3926. #endif
  3927. alloc_raw_frame_buffers(cpi);
  3928. init_ref_frame_bufs(cm);
  3929. alloc_util_frame_buffers(cpi);
  3930. init_motion_estimation(cpi); // TODO(agrange) This can be removed.
  3931. cpi->initial_width = cm->width;
  3932. cpi->initial_height = cm->height;
  3933. cpi->initial_mbs = cm->MBs;
  3934. }
  3935. }
  3936. int vp9_receive_raw_frame(VP9_COMP *cpi, vpx_enc_frame_flags_t frame_flags,
  3937. YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
  3938. int64_t end_time) {
  3939. VP9_COMMON *const cm = &cpi->common;
  3940. struct vpx_usec_timer timer;
  3941. int res = 0;
  3942. const int subsampling_x = sd->subsampling_x;
  3943. const int subsampling_y = sd->subsampling_y;
  3944. #if CONFIG_VP9_HIGHBITDEPTH
  3945. const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
  3946. #endif
  3947. #if CONFIG_VP9_HIGHBITDEPTH
  3948. check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
  3949. #else
  3950. check_initial_width(cpi, subsampling_x, subsampling_y);
  3951. #endif // CONFIG_VP9_HIGHBITDEPTH
  3952. #if CONFIG_VP9_TEMPORAL_DENOISING
  3953. setup_denoiser_buffer(cpi);
  3954. #endif
  3955. vpx_usec_timer_start(&timer);
  3956. if (vp9_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
  3957. #if CONFIG_VP9_HIGHBITDEPTH
  3958. use_highbitdepth,
  3959. #endif // CONFIG_VP9_HIGHBITDEPTH
  3960. frame_flags))
  3961. res = -1;
  3962. vpx_usec_timer_mark(&timer);
  3963. cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
  3964. if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
  3965. (subsampling_x != 1 || subsampling_y != 1)) {
  3966. vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
  3967. "Non-4:2:0 color format requires profile 1 or 3");
  3968. res = -1;
  3969. }
  3970. if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
  3971. (subsampling_x == 1 && subsampling_y == 1)) {
  3972. vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
  3973. "4:2:0 color format requires profile 0 or 2");
  3974. res = -1;
  3975. }
  3976. return res;
  3977. }
  3978. static int frame_is_reference(const VP9_COMP *cpi) {
  3979. const VP9_COMMON *cm = &cpi->common;
  3980. return cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
  3981. cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame ||
  3982. cm->refresh_frame_context || cm->lf.mode_ref_delta_update ||
  3983. cm->seg.update_map || cm->seg.update_data;
  3984. }
  3985. static void adjust_frame_rate(VP9_COMP *cpi,
  3986. const struct lookahead_entry *source) {
  3987. int64_t this_duration;
  3988. int step = 0;
  3989. if (source->ts_start == cpi->first_time_stamp_ever) {
  3990. this_duration = source->ts_end - source->ts_start;
  3991. step = 1;
  3992. } else {
  3993. int64_t last_duration =
  3994. cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
  3995. this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
  3996. // do a step update if the duration changes by 10%
  3997. if (last_duration)
  3998. step = (int)((this_duration - last_duration) * 10 / last_duration);
  3999. }
  4000. if (this_duration) {
  4001. if (step) {
  4002. vp9_new_framerate(cpi, 10000000.0 / this_duration);
  4003. } else {
  4004. // Average this frame's rate into the last second's average
  4005. // frame rate. If we haven't seen 1 second yet, then average
  4006. // over the whole interval seen.
  4007. const double interval = VPXMIN(
  4008. (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
  4009. double avg_duration = 10000000.0 / cpi->framerate;
  4010. avg_duration *= (interval - avg_duration + this_duration);
  4011. avg_duration /= interval;
  4012. vp9_new_framerate(cpi, 10000000.0 / avg_duration);
  4013. }
  4014. }
  4015. cpi->last_time_stamp_seen = source->ts_start;
  4016. cpi->last_end_time_stamp_seen = source->ts_end;
  4017. }
  4018. // Returns 0 if this is not an alt ref else the offset of the source frame
  4019. // used as the arf midpoint.
  4020. static int get_arf_src_index(VP9_COMP *cpi) {
  4021. RATE_CONTROL *const rc = &cpi->rc;
  4022. int arf_src_index = 0;
  4023. if (is_altref_enabled(cpi)) {
  4024. if (cpi->oxcf.pass == 2) {
  4025. const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
  4026. if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
  4027. arf_src_index = gf_group->arf_src_offset[gf_group->index];
  4028. }
  4029. } else if (rc->source_alt_ref_pending) {
  4030. arf_src_index = rc->frames_till_gf_update_due;
  4031. }
  4032. }
  4033. return arf_src_index;
  4034. }
  4035. static void check_src_altref(VP9_COMP *cpi,
  4036. const struct lookahead_entry *source) {
  4037. RATE_CONTROL *const rc = &cpi->rc;
  4038. if (cpi->oxcf.pass == 2) {
  4039. const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
  4040. rc->is_src_frame_alt_ref =
  4041. (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
  4042. } else {
  4043. rc->is_src_frame_alt_ref =
  4044. cpi->alt_ref_source && (source == cpi->alt_ref_source);
  4045. }
  4046. if (rc->is_src_frame_alt_ref) {
  4047. // Current frame is an ARF overlay frame.
  4048. cpi->alt_ref_source = NULL;
  4049. // Don't refresh the last buffer for an ARF overlay frame. It will
  4050. // become the GF so preserve last as an alternative prediction option.
  4051. cpi->refresh_last_frame = 0;
  4052. }
  4053. }
  4054. #if CONFIG_INTERNAL_STATS
  4055. extern double vp9_get_blockiness(const uint8_t *img1, int img1_pitch,
  4056. const uint8_t *img2, int img2_pitch, int width,
  4057. int height);
  4058. static void adjust_image_stat(double y, double u, double v, double all,
  4059. ImageStat *s) {
  4060. s->stat[Y] += y;
  4061. s->stat[U] += u;
  4062. s->stat[V] += v;
  4063. s->stat[ALL] += all;
  4064. s->worst = VPXMIN(s->worst, all);
  4065. }
  4066. #endif // CONFIG_INTERNAL_STATS
  4067. // Adjust the maximum allowable frame size for the target level.
  4068. static void level_rc_framerate(VP9_COMP *cpi, int arf_src_index) {
  4069. RATE_CONTROL *const rc = &cpi->rc;
  4070. LevelConstraint *const ls = &cpi->level_constraint;
  4071. VP9_COMMON *const cm = &cpi->common;
  4072. const double max_cpb_size = ls->max_cpb_size;
  4073. vpx_clear_system_state();
  4074. rc->max_frame_bandwidth = VPXMIN(rc->max_frame_bandwidth, ls->max_frame_size);
  4075. if (frame_is_intra_only(cm)) {
  4076. rc->max_frame_bandwidth =
  4077. VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.5));
  4078. } else if (arf_src_index > 0) {
  4079. rc->max_frame_bandwidth =
  4080. VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.4));
  4081. } else {
  4082. rc->max_frame_bandwidth =
  4083. VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.2));
  4084. }
  4085. }
  4086. static void update_level_info(VP9_COMP *cpi, size_t *size, int arf_src_index) {
  4087. VP9_COMMON *const cm = &cpi->common;
  4088. Vp9LevelInfo *const level_info = &cpi->level_info;
  4089. Vp9LevelSpec *const level_spec = &level_info->level_spec;
  4090. Vp9LevelStats *const level_stats = &level_info->level_stats;
  4091. int i, idx;
  4092. uint64_t luma_samples, dur_end;
  4093. const uint32_t luma_pic_size = cm->width * cm->height;
  4094. LevelConstraint *const level_constraint = &cpi->level_constraint;
  4095. const int8_t level_index = level_constraint->level_index;
  4096. double cpb_data_size;
  4097. vpx_clear_system_state();
  4098. // update level_stats
  4099. level_stats->total_compressed_size += *size;
  4100. if (cm->show_frame) {
  4101. level_stats->total_uncompressed_size +=
  4102. luma_pic_size +
  4103. 2 * (luma_pic_size >> (cm->subsampling_x + cm->subsampling_y));
  4104. level_stats->time_encoded =
  4105. (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
  4106. (double)TICKS_PER_SEC;
  4107. }
  4108. if (arf_src_index > 0) {
  4109. if (!level_stats->seen_first_altref) {
  4110. level_stats->seen_first_altref = 1;
  4111. } else if (level_stats->frames_since_last_altref <
  4112. level_spec->min_altref_distance) {
  4113. level_spec->min_altref_distance = level_stats->frames_since_last_altref;
  4114. }
  4115. level_stats->frames_since_last_altref = 0;
  4116. } else {
  4117. ++level_stats->frames_since_last_altref;
  4118. }
  4119. if (level_stats->frame_window_buffer.len < FRAME_WINDOW_SIZE - 1) {
  4120. idx = (level_stats->frame_window_buffer.start +
  4121. level_stats->frame_window_buffer.len++) %
  4122. FRAME_WINDOW_SIZE;
  4123. } else {
  4124. idx = level_stats->frame_window_buffer.start;
  4125. level_stats->frame_window_buffer.start = (idx + 1) % FRAME_WINDOW_SIZE;
  4126. }
  4127. level_stats->frame_window_buffer.buf[idx].ts = cpi->last_time_stamp_seen;
  4128. level_stats->frame_window_buffer.buf[idx].size = (uint32_t)(*size);
  4129. level_stats->frame_window_buffer.buf[idx].luma_samples = luma_pic_size;
  4130. if (cm->frame_type == KEY_FRAME) {
  4131. level_stats->ref_refresh_map = 0;
  4132. } else {
  4133. int count = 0;
  4134. level_stats->ref_refresh_map |= vp9_get_refresh_mask(cpi);
  4135. // Also need to consider the case where the encoder refers to a buffer
  4136. // that has been implicitly refreshed after encoding a keyframe.
  4137. if (!cm->intra_only) {
  4138. level_stats->ref_refresh_map |= (1 << cpi->lst_fb_idx);
  4139. level_stats->ref_refresh_map |= (1 << cpi->gld_fb_idx);
  4140. level_stats->ref_refresh_map |= (1 << cpi->alt_fb_idx);
  4141. }
  4142. for (i = 0; i < REF_FRAMES; ++i) {
  4143. count += (level_stats->ref_refresh_map >> i) & 1;
  4144. }
  4145. if (count > level_spec->max_ref_frame_buffers) {
  4146. level_spec->max_ref_frame_buffers = count;
  4147. }
  4148. }
  4149. // update average_bitrate
  4150. level_spec->average_bitrate = (double)level_stats->total_compressed_size /
  4151. 125.0 / level_stats->time_encoded;
  4152. // update max_luma_sample_rate
  4153. luma_samples = 0;
  4154. for (i = 0; i < level_stats->frame_window_buffer.len; ++i) {
  4155. idx = (level_stats->frame_window_buffer.start +
  4156. level_stats->frame_window_buffer.len - 1 - i) %
  4157. FRAME_WINDOW_SIZE;
  4158. if (i == 0) {
  4159. dur_end = level_stats->frame_window_buffer.buf[idx].ts;
  4160. }
  4161. if (dur_end - level_stats->frame_window_buffer.buf[idx].ts >=
  4162. TICKS_PER_SEC) {
  4163. break;
  4164. }
  4165. luma_samples += level_stats->frame_window_buffer.buf[idx].luma_samples;
  4166. }
  4167. if (luma_samples > level_spec->max_luma_sample_rate) {
  4168. level_spec->max_luma_sample_rate = luma_samples;
  4169. }
  4170. // update max_cpb_size
  4171. cpb_data_size = 0;
  4172. for (i = 0; i < CPB_WINDOW_SIZE; ++i) {
  4173. if (i >= level_stats->frame_window_buffer.len) break;
  4174. idx = (level_stats->frame_window_buffer.start +
  4175. level_stats->frame_window_buffer.len - 1 - i) %
  4176. FRAME_WINDOW_SIZE;
  4177. cpb_data_size += level_stats->frame_window_buffer.buf[idx].size;
  4178. }
  4179. cpb_data_size = cpb_data_size / 125.0;
  4180. if (cpb_data_size > level_spec->max_cpb_size) {
  4181. level_spec->max_cpb_size = cpb_data_size;
  4182. }
  4183. // update max_luma_picture_size
  4184. if (luma_pic_size > level_spec->max_luma_picture_size) {
  4185. level_spec->max_luma_picture_size = luma_pic_size;
  4186. }
  4187. // update compression_ratio
  4188. level_spec->compression_ratio = (double)level_stats->total_uncompressed_size *
  4189. cm->bit_depth /
  4190. level_stats->total_compressed_size / 8.0;
  4191. // update max_col_tiles
  4192. if (level_spec->max_col_tiles < (1 << cm->log2_tile_cols)) {
  4193. level_spec->max_col_tiles = (1 << cm->log2_tile_cols);
  4194. }
  4195. if (level_index >= 0 && level_constraint->fail_flag == 0) {
  4196. if (level_spec->max_luma_picture_size >
  4197. vp9_level_defs[level_index].max_luma_picture_size) {
  4198. level_constraint->fail_flag |= (1 << LUMA_PIC_SIZE_TOO_LARGE);
  4199. vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
  4200. "Failed to encode to the target level %d. %s",
  4201. vp9_level_defs[level_index].level,
  4202. level_fail_messages[LUMA_PIC_SIZE_TOO_LARGE]);
  4203. }
  4204. if ((double)level_spec->max_luma_sample_rate >
  4205. (double)vp9_level_defs[level_index].max_luma_sample_rate *
  4206. (1 + SAMPLE_RATE_GRACE_P)) {
  4207. level_constraint->fail_flag |= (1 << LUMA_SAMPLE_RATE_TOO_LARGE);
  4208. vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
  4209. "Failed to encode to the target level %d. %s",
  4210. vp9_level_defs[level_index].level,
  4211. level_fail_messages[LUMA_SAMPLE_RATE_TOO_LARGE]);
  4212. }
  4213. if (level_spec->max_col_tiles > vp9_level_defs[level_index].max_col_tiles) {
  4214. level_constraint->fail_flag |= (1 << TOO_MANY_COLUMN_TILE);
  4215. vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
  4216. "Failed to encode to the target level %d. %s",
  4217. vp9_level_defs[level_index].level,
  4218. level_fail_messages[TOO_MANY_COLUMN_TILE]);
  4219. }
  4220. if (level_spec->min_altref_distance <
  4221. vp9_level_defs[level_index].min_altref_distance) {
  4222. level_constraint->fail_flag |= (1 << ALTREF_DIST_TOO_SMALL);
  4223. vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
  4224. "Failed to encode to the target level %d. %s",
  4225. vp9_level_defs[level_index].level,
  4226. level_fail_messages[ALTREF_DIST_TOO_SMALL]);
  4227. }
  4228. if (level_spec->max_ref_frame_buffers >
  4229. vp9_level_defs[level_index].max_ref_frame_buffers) {
  4230. level_constraint->fail_flag |= (1 << TOO_MANY_REF_BUFFER);
  4231. vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
  4232. "Failed to encode to the target level %d. %s",
  4233. vp9_level_defs[level_index].level,
  4234. level_fail_messages[TOO_MANY_REF_BUFFER]);
  4235. }
  4236. if (level_spec->max_cpb_size > vp9_level_defs[level_index].max_cpb_size) {
  4237. level_constraint->fail_flag |= (1 << CPB_TOO_LARGE);
  4238. vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
  4239. "Failed to encode to the target level %d. %s",
  4240. vp9_level_defs[level_index].level,
  4241. level_fail_messages[CPB_TOO_LARGE]);
  4242. }
  4243. // Set an upper bound for the next frame size. It will be used in
  4244. // level_rc_framerate() before encoding the next frame.
  4245. cpb_data_size = 0;
  4246. for (i = 0; i < CPB_WINDOW_SIZE - 1; ++i) {
  4247. if (i >= level_stats->frame_window_buffer.len) break;
  4248. idx = (level_stats->frame_window_buffer.start +
  4249. level_stats->frame_window_buffer.len - 1 - i) %
  4250. FRAME_WINDOW_SIZE;
  4251. cpb_data_size += level_stats->frame_window_buffer.buf[idx].size;
  4252. }
  4253. cpb_data_size = cpb_data_size / 125.0;
  4254. level_constraint->max_frame_size =
  4255. (int)((vp9_level_defs[level_index].max_cpb_size - cpb_data_size) *
  4256. 1000.0);
  4257. if (level_stats->frame_window_buffer.len < CPB_WINDOW_SIZE - 1)
  4258. level_constraint->max_frame_size >>= 1;
  4259. }
  4260. }
  4261. int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
  4262. size_t *size, uint8_t *dest, int64_t *time_stamp,
  4263. int64_t *time_end, int flush) {
  4264. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  4265. VP9_COMMON *const cm = &cpi->common;
  4266. BufferPool *const pool = cm->buffer_pool;
  4267. RATE_CONTROL *const rc = &cpi->rc;
  4268. struct vpx_usec_timer cmptimer;
  4269. YV12_BUFFER_CONFIG *force_src_buffer = NULL;
  4270. struct lookahead_entry *last_source = NULL;
  4271. struct lookahead_entry *source = NULL;
  4272. int arf_src_index;
  4273. int i;
  4274. if (is_two_pass_svc(cpi)) {
  4275. #if CONFIG_SPATIAL_SVC
  4276. vp9_svc_start_frame(cpi);
  4277. // Use a small empty frame instead of a real frame
  4278. if (cpi->svc.encode_empty_frame_state == ENCODING)
  4279. source = &cpi->svc.empty_frame;
  4280. #endif
  4281. if (oxcf->pass == 2) vp9_restore_layer_context(cpi);
  4282. } else if (is_one_pass_cbr_svc(cpi)) {
  4283. vp9_one_pass_cbr_svc_start_layer(cpi);
  4284. }
  4285. vpx_usec_timer_start(&cmptimer);
  4286. vp9_set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
  4287. // Is multi-arf enabled.
  4288. // Note that at the moment multi_arf is only configured for 2 pass VBR and
  4289. // will not work properly with svc.
  4290. if ((oxcf->pass == 2) && !cpi->use_svc && (cpi->oxcf.enable_auto_arf > 1))
  4291. cpi->multi_arf_allowed = 1;
  4292. else
  4293. cpi->multi_arf_allowed = 0;
  4294. // Normal defaults
  4295. cm->reset_frame_context = 0;
  4296. cm->refresh_frame_context = 1;
  4297. if (!is_one_pass_cbr_svc(cpi)) {
  4298. cpi->refresh_last_frame = 1;
  4299. cpi->refresh_golden_frame = 0;
  4300. cpi->refresh_alt_ref_frame = 0;
  4301. }
  4302. // Should we encode an arf frame.
  4303. arf_src_index = get_arf_src_index(cpi);
  4304. // Skip alt frame if we encode the empty frame
  4305. if (is_two_pass_svc(cpi) && source != NULL) arf_src_index = 0;
  4306. if (arf_src_index) {
  4307. for (i = 0; i <= arf_src_index; ++i) {
  4308. struct lookahead_entry *e = vp9_lookahead_peek(cpi->lookahead, i);
  4309. // Avoid creating an alt-ref if there's a forced keyframe pending.
  4310. if (e == NULL) {
  4311. break;
  4312. } else if (e->flags == VPX_EFLAG_FORCE_KF) {
  4313. arf_src_index = 0;
  4314. flush = 1;
  4315. break;
  4316. }
  4317. }
  4318. }
  4319. if (arf_src_index) {
  4320. assert(arf_src_index <= rc->frames_to_key);
  4321. if ((source = vp9_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
  4322. cpi->alt_ref_source = source;
  4323. #if CONFIG_SPATIAL_SVC
  4324. if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0) {
  4325. int i;
  4326. // Reference a hidden frame from a lower layer
  4327. for (i = cpi->svc.spatial_layer_id - 1; i >= 0; --i) {
  4328. if (oxcf->ss_enable_auto_arf[i]) {
  4329. cpi->gld_fb_idx = cpi->svc.layer_context[i].alt_ref_idx;
  4330. break;
  4331. }
  4332. }
  4333. }
  4334. cpi->svc.layer_context[cpi->svc.spatial_layer_id].has_alt_frame = 1;
  4335. #endif
  4336. #if !CONFIG_REALTIME_ONLY
  4337. if ((oxcf->mode != REALTIME) && (oxcf->arnr_max_frames > 0) &&
  4338. (oxcf->arnr_strength > 0)) {
  4339. int bitrate = cpi->rc.avg_frame_bandwidth / 40;
  4340. int not_low_bitrate = bitrate > ALT_REF_AQ_LOW_BITRATE_BOUNDARY;
  4341. int not_last_frame = (cpi->lookahead->sz - arf_src_index > 1);
  4342. not_last_frame |= ALT_REF_AQ_APPLY_TO_LAST_FRAME;
  4343. // Produce the filtered ARF frame.
  4344. vp9_temporal_filter(cpi, arf_src_index);
  4345. vpx_extend_frame_borders(&cpi->alt_ref_buffer);
  4346. // for small bitrates segmentation overhead usually
  4347. // eats all bitrate gain from enabling delta quantizers
  4348. if (cpi->oxcf.alt_ref_aq != 0 && not_low_bitrate && not_last_frame)
  4349. vp9_alt_ref_aq_setup_mode(cpi->alt_ref_aq, cpi);
  4350. force_src_buffer = &cpi->alt_ref_buffer;
  4351. }
  4352. #endif
  4353. cm->show_frame = 0;
  4354. cm->intra_only = 0;
  4355. cpi->refresh_alt_ref_frame = 1;
  4356. cpi->refresh_golden_frame = 0;
  4357. cpi->refresh_last_frame = 0;
  4358. rc->is_src_frame_alt_ref = 0;
  4359. rc->source_alt_ref_pending = 0;
  4360. } else {
  4361. rc->source_alt_ref_pending = 0;
  4362. }
  4363. }
  4364. if (!source) {
  4365. // Get last frame source.
  4366. if (cm->current_video_frame > 0) {
  4367. if ((last_source = vp9_lookahead_peek(cpi->lookahead, -1)) == NULL)
  4368. return -1;
  4369. }
  4370. // Read in the source frame.
  4371. if (cpi->use_svc)
  4372. source = vp9_svc_lookahead_pop(cpi, cpi->lookahead, flush);
  4373. else
  4374. source = vp9_lookahead_pop(cpi->lookahead, flush);
  4375. if (source != NULL) {
  4376. cm->show_frame = 1;
  4377. cm->intra_only = 0;
  4378. // if the flags indicate intra frame, but if the current picture is for
  4379. // non-zero spatial layer, it should not be an intra picture.
  4380. // TODO(Won Kap): this needs to change if per-layer intra frame is
  4381. // allowed.
  4382. if ((source->flags & VPX_EFLAG_FORCE_KF) &&
  4383. cpi->svc.spatial_layer_id > cpi->svc.first_spatial_layer_to_encode) {
  4384. source->flags &= ~(unsigned int)(VPX_EFLAG_FORCE_KF);
  4385. }
  4386. // Check to see if the frame should be encoded as an arf overlay.
  4387. check_src_altref(cpi, source);
  4388. }
  4389. }
  4390. if (source) {
  4391. cpi->un_scaled_source = cpi->Source =
  4392. force_src_buffer ? force_src_buffer : &source->img;
  4393. #ifdef ENABLE_KF_DENOISE
  4394. // Copy of raw source for metrics calculation.
  4395. if (is_psnr_calc_enabled(cpi))
  4396. vp9_copy_and_extend_frame(cpi->Source, &cpi->raw_unscaled_source);
  4397. #endif
  4398. cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
  4399. *time_stamp = source->ts_start;
  4400. *time_end = source->ts_end;
  4401. *frame_flags = (source->flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
  4402. } else {
  4403. *size = 0;
  4404. #if !CONFIG_REALTIME_ONLY
  4405. if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
  4406. vp9_end_first_pass(cpi); /* get last stats packet */
  4407. cpi->twopass.first_pass_done = 1;
  4408. }
  4409. #endif // !CONFIG_REALTIME_ONLY
  4410. return -1;
  4411. }
  4412. if (source->ts_start < cpi->first_time_stamp_ever) {
  4413. cpi->first_time_stamp_ever = source->ts_start;
  4414. cpi->last_end_time_stamp_seen = source->ts_start;
  4415. }
  4416. // Clear down mmx registers
  4417. vpx_clear_system_state();
  4418. // adjust frame rates based on timestamps given
  4419. if (cm->show_frame) {
  4420. adjust_frame_rate(cpi, source);
  4421. }
  4422. if (is_one_pass_cbr_svc(cpi)) {
  4423. vp9_update_temporal_layer_framerate(cpi);
  4424. vp9_restore_layer_context(cpi);
  4425. }
  4426. // Find a free buffer for the new frame, releasing the reference previously
  4427. // held.
  4428. if (cm->new_fb_idx != INVALID_IDX) {
  4429. --pool->frame_bufs[cm->new_fb_idx].ref_count;
  4430. }
  4431. cm->new_fb_idx = get_free_fb(cm);
  4432. if (cm->new_fb_idx == INVALID_IDX) return -1;
  4433. cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
  4434. if (!cpi->use_svc && cpi->multi_arf_allowed) {
  4435. if (cm->frame_type == KEY_FRAME) {
  4436. init_buffer_indices(cpi);
  4437. } else if (oxcf->pass == 2) {
  4438. const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
  4439. cpi->alt_fb_idx = gf_group->arf_ref_idx[gf_group->index];
  4440. }
  4441. }
  4442. // Start with a 0 size frame.
  4443. *size = 0;
  4444. cpi->frame_flags = *frame_flags;
  4445. #if !CONFIG_REALTIME_ONLY
  4446. if ((oxcf->pass == 2) &&
  4447. (!cpi->use_svc || (is_two_pass_svc(cpi) &&
  4448. cpi->svc.encode_empty_frame_state != ENCODING))) {
  4449. vp9_rc_get_second_pass_params(cpi);
  4450. } else if (oxcf->pass == 1) {
  4451. set_frame_size(cpi);
  4452. }
  4453. #endif // !CONFIG_REALTIME_ONLY
  4454. if (oxcf->pass != 1 && cpi->level_constraint.level_index >= 0 &&
  4455. cpi->level_constraint.fail_flag == 0)
  4456. level_rc_framerate(cpi, arf_src_index);
  4457. if (cpi->oxcf.pass != 0 || cpi->use_svc || frame_is_intra_only(cm) == 1) {
  4458. for (i = 0; i < MAX_REF_FRAMES; ++i) cpi->scaled_ref_idx[i] = INVALID_IDX;
  4459. }
  4460. cpi->td.mb.fp_src_pred = 0;
  4461. #if CONFIG_REALTIME_ONLY
  4462. if (cpi->use_svc) {
  4463. SvcEncode(cpi, size, dest, frame_flags);
  4464. } else {
  4465. // One pass encode
  4466. Pass0Encode(cpi, size, dest, frame_flags);
  4467. }
  4468. #else // !CONFIG_REALTIME_ONLY
  4469. if (oxcf->pass == 1 && (!cpi->use_svc || is_two_pass_svc(cpi))) {
  4470. const int lossless = is_lossless_requested(oxcf);
  4471. #if CONFIG_VP9_HIGHBITDEPTH
  4472. if (cpi->oxcf.use_highbitdepth)
  4473. cpi->td.mb.fwd_txfm4x4 =
  4474. lossless ? vp9_highbd_fwht4x4 : vpx_highbd_fdct4x4;
  4475. else
  4476. cpi->td.mb.fwd_txfm4x4 = lossless ? vp9_fwht4x4 : vpx_fdct4x4;
  4477. cpi->td.mb.highbd_inv_txfm_add =
  4478. lossless ? vp9_highbd_iwht4x4_add : vp9_highbd_idct4x4_add;
  4479. #else
  4480. cpi->td.mb.fwd_txfm4x4 = lossless ? vp9_fwht4x4 : vpx_fdct4x4;
  4481. #endif // CONFIG_VP9_HIGHBITDEPTH
  4482. cpi->td.mb.inv_txfm_add = lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
  4483. vp9_first_pass(cpi, source);
  4484. } else if (oxcf->pass == 2 && (!cpi->use_svc || is_two_pass_svc(cpi))) {
  4485. Pass2Encode(cpi, size, dest, frame_flags);
  4486. } else if (cpi->use_svc) {
  4487. SvcEncode(cpi, size, dest, frame_flags);
  4488. } else {
  4489. // One pass encode
  4490. Pass0Encode(cpi, size, dest, frame_flags);
  4491. }
  4492. #endif // CONFIG_REALTIME_ONLY
  4493. if (cm->refresh_frame_context)
  4494. cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
  4495. // No frame encoded, or frame was dropped, release scaled references.
  4496. if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
  4497. release_scaled_references(cpi);
  4498. }
  4499. if (*size > 0) {
  4500. cpi->droppable = !frame_is_reference(cpi);
  4501. }
  4502. // Save layer specific state.
  4503. if (is_one_pass_cbr_svc(cpi) || ((cpi->svc.number_temporal_layers > 1 ||
  4504. cpi->svc.number_spatial_layers > 1) &&
  4505. oxcf->pass == 2)) {
  4506. vp9_save_layer_context(cpi);
  4507. }
  4508. vpx_usec_timer_mark(&cmptimer);
  4509. cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
  4510. // Should we calculate metrics for the frame.
  4511. if (is_psnr_calc_enabled(cpi)) generate_psnr_packet(cpi);
  4512. if (cpi->keep_level_stats && oxcf->pass != 1)
  4513. update_level_info(cpi, size, arf_src_index);
  4514. #if CONFIG_INTERNAL_STATS
  4515. if (oxcf->pass != 1) {
  4516. double samples = 0.0;
  4517. cpi->bytes += (int)(*size);
  4518. if (cm->show_frame) {
  4519. uint32_t bit_depth = 8;
  4520. uint32_t in_bit_depth = 8;
  4521. cpi->count++;
  4522. #if CONFIG_VP9_HIGHBITDEPTH
  4523. if (cm->use_highbitdepth) {
  4524. in_bit_depth = cpi->oxcf.input_bit_depth;
  4525. bit_depth = cm->bit_depth;
  4526. }
  4527. #endif
  4528. if (cpi->b_calculate_psnr) {
  4529. YV12_BUFFER_CONFIG *orig = cpi->raw_source_frame;
  4530. YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
  4531. YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
  4532. PSNR_STATS psnr;
  4533. #if CONFIG_VP9_HIGHBITDEPTH
  4534. vpx_calc_highbd_psnr(orig, recon, &psnr, cpi->td.mb.e_mbd.bd,
  4535. in_bit_depth);
  4536. #else
  4537. vpx_calc_psnr(orig, recon, &psnr);
  4538. #endif // CONFIG_VP9_HIGHBITDEPTH
  4539. adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3],
  4540. psnr.psnr[0], &cpi->psnr);
  4541. cpi->total_sq_error += psnr.sse[0];
  4542. cpi->total_samples += psnr.samples[0];
  4543. samples = psnr.samples[0];
  4544. {
  4545. PSNR_STATS psnr2;
  4546. double frame_ssim2 = 0, weight = 0;
  4547. #if CONFIG_VP9_POSTPROC
  4548. if (vpx_alloc_frame_buffer(
  4549. pp, recon->y_crop_width, recon->y_crop_height,
  4550. cm->subsampling_x, cm->subsampling_y,
  4551. #if CONFIG_VP9_HIGHBITDEPTH
  4552. cm->use_highbitdepth,
  4553. #endif
  4554. VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment) < 0) {
  4555. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  4556. "Failed to allocate post processing buffer");
  4557. }
  4558. {
  4559. vp9_ppflags_t ppflags;
  4560. ppflags.post_proc_flag = VP9D_DEBLOCK;
  4561. ppflags.deblocking_level = 0; // not used in vp9_post_proc_frame()
  4562. ppflags.noise_level = 0; // not used in vp9_post_proc_frame()
  4563. vp9_post_proc_frame(cm, pp, &ppflags);
  4564. }
  4565. #endif
  4566. vpx_clear_system_state();
  4567. #if CONFIG_VP9_HIGHBITDEPTH
  4568. vpx_calc_highbd_psnr(orig, pp, &psnr2, cpi->td.mb.e_mbd.bd,
  4569. cpi->oxcf.input_bit_depth);
  4570. #else
  4571. vpx_calc_psnr(orig, pp, &psnr2);
  4572. #endif // CONFIG_VP9_HIGHBITDEPTH
  4573. cpi->totalp_sq_error += psnr2.sse[0];
  4574. cpi->totalp_samples += psnr2.samples[0];
  4575. adjust_image_stat(psnr2.psnr[1], psnr2.psnr[2], psnr2.psnr[3],
  4576. psnr2.psnr[0], &cpi->psnrp);
  4577. #if CONFIG_VP9_HIGHBITDEPTH
  4578. if (cm->use_highbitdepth) {
  4579. frame_ssim2 = vpx_highbd_calc_ssim(orig, recon, &weight, bit_depth,
  4580. in_bit_depth);
  4581. } else {
  4582. frame_ssim2 = vpx_calc_ssim(orig, recon, &weight);
  4583. }
  4584. #else
  4585. frame_ssim2 = vpx_calc_ssim(orig, recon, &weight);
  4586. #endif // CONFIG_VP9_HIGHBITDEPTH
  4587. cpi->worst_ssim = VPXMIN(cpi->worst_ssim, frame_ssim2);
  4588. cpi->summed_quality += frame_ssim2 * weight;
  4589. cpi->summed_weights += weight;
  4590. #if CONFIG_VP9_HIGHBITDEPTH
  4591. if (cm->use_highbitdepth) {
  4592. frame_ssim2 = vpx_highbd_calc_ssim(orig, pp, &weight, bit_depth,
  4593. in_bit_depth);
  4594. } else {
  4595. frame_ssim2 = vpx_calc_ssim(orig, pp, &weight);
  4596. }
  4597. #else
  4598. frame_ssim2 = vpx_calc_ssim(orig, pp, &weight);
  4599. #endif // CONFIG_VP9_HIGHBITDEPTH
  4600. cpi->summedp_quality += frame_ssim2 * weight;
  4601. cpi->summedp_weights += weight;
  4602. #if 0
  4603. {
  4604. FILE *f = fopen("q_used.stt", "a");
  4605. fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
  4606. cpi->common.current_video_frame, y2, u2, v2,
  4607. frame_psnr2, frame_ssim2);
  4608. fclose(f);
  4609. }
  4610. #endif
  4611. }
  4612. }
  4613. if (cpi->b_calculate_blockiness) {
  4614. #if CONFIG_VP9_HIGHBITDEPTH
  4615. if (!cm->use_highbitdepth)
  4616. #endif
  4617. {
  4618. double frame_blockiness = vp9_get_blockiness(
  4619. cpi->Source->y_buffer, cpi->Source->y_stride,
  4620. cm->frame_to_show->y_buffer, cm->frame_to_show->y_stride,
  4621. cpi->Source->y_width, cpi->Source->y_height);
  4622. cpi->worst_blockiness =
  4623. VPXMAX(cpi->worst_blockiness, frame_blockiness);
  4624. cpi->total_blockiness += frame_blockiness;
  4625. }
  4626. }
  4627. if (cpi->b_calculate_consistency) {
  4628. #if CONFIG_VP9_HIGHBITDEPTH
  4629. if (!cm->use_highbitdepth)
  4630. #endif
  4631. {
  4632. double this_inconsistency = vpx_get_ssim_metrics(
  4633. cpi->Source->y_buffer, cpi->Source->y_stride,
  4634. cm->frame_to_show->y_buffer, cm->frame_to_show->y_stride,
  4635. cpi->Source->y_width, cpi->Source->y_height, cpi->ssim_vars,
  4636. &cpi->metrics, 1);
  4637. const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
  4638. double consistency =
  4639. vpx_sse_to_psnr(samples, peak, (double)cpi->total_inconsistency);
  4640. if (consistency > 0.0)
  4641. cpi->worst_consistency =
  4642. VPXMIN(cpi->worst_consistency, consistency);
  4643. cpi->total_inconsistency += this_inconsistency;
  4644. }
  4645. }
  4646. {
  4647. double y, u, v, frame_all;
  4648. frame_all = vpx_calc_fastssim(cpi->Source, cm->frame_to_show, &y, &u,
  4649. &v, bit_depth, in_bit_depth);
  4650. adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
  4651. }
  4652. {
  4653. double y, u, v, frame_all;
  4654. frame_all = vpx_psnrhvs(cpi->Source, cm->frame_to_show, &y, &u, &v,
  4655. bit_depth, in_bit_depth);
  4656. adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
  4657. }
  4658. }
  4659. }
  4660. #endif
  4661. if (is_two_pass_svc(cpi)) {
  4662. if (cpi->svc.encode_empty_frame_state == ENCODING) {
  4663. cpi->svc.encode_empty_frame_state = ENCODED;
  4664. cpi->svc.encode_intra_empty_frame = 0;
  4665. }
  4666. if (cm->show_frame) {
  4667. ++cpi->svc.spatial_layer_to_encode;
  4668. if (cpi->svc.spatial_layer_to_encode >= cpi->svc.number_spatial_layers)
  4669. cpi->svc.spatial_layer_to_encode = 0;
  4670. // May need the empty frame after an visible frame.
  4671. cpi->svc.encode_empty_frame_state = NEED_TO_ENCODE;
  4672. }
  4673. } else if (is_one_pass_cbr_svc(cpi)) {
  4674. if (cm->show_frame) {
  4675. ++cpi->svc.spatial_layer_to_encode;
  4676. if (cpi->svc.spatial_layer_to_encode >= cpi->svc.number_spatial_layers)
  4677. cpi->svc.spatial_layer_to_encode = 0;
  4678. }
  4679. }
  4680. vpx_clear_system_state();
  4681. return 0;
  4682. }
  4683. int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
  4684. vp9_ppflags_t *flags) {
  4685. VP9_COMMON *cm = &cpi->common;
  4686. #if !CONFIG_VP9_POSTPROC
  4687. (void)flags;
  4688. #endif
  4689. if (!cm->show_frame) {
  4690. return -1;
  4691. } else {
  4692. int ret;
  4693. #if CONFIG_VP9_POSTPROC
  4694. ret = vp9_post_proc_frame(cm, dest, flags);
  4695. #else
  4696. if (cm->frame_to_show) {
  4697. *dest = *cm->frame_to_show;
  4698. dest->y_width = cm->width;
  4699. dest->y_height = cm->height;
  4700. dest->uv_width = cm->width >> cm->subsampling_x;
  4701. dest->uv_height = cm->height >> cm->subsampling_y;
  4702. ret = 0;
  4703. } else {
  4704. ret = -1;
  4705. }
  4706. #endif // !CONFIG_VP9_POSTPROC
  4707. vpx_clear_system_state();
  4708. return ret;
  4709. }
  4710. }
  4711. int vp9_set_internal_size(VP9_COMP *cpi, VPX_SCALING horiz_mode,
  4712. VPX_SCALING vert_mode) {
  4713. VP9_COMMON *cm = &cpi->common;
  4714. int hr = 0, hs = 0, vr = 0, vs = 0;
  4715. if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
  4716. Scale2Ratio(horiz_mode, &hr, &hs);
  4717. Scale2Ratio(vert_mode, &vr, &vs);
  4718. // always go to the next whole number
  4719. cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
  4720. cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
  4721. if (cm->current_video_frame) {
  4722. assert(cm->width <= cpi->initial_width);
  4723. assert(cm->height <= cpi->initial_height);
  4724. }
  4725. update_frame_size(cpi);
  4726. return 0;
  4727. }
  4728. int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
  4729. unsigned int height) {
  4730. VP9_COMMON *cm = &cpi->common;
  4731. #if CONFIG_VP9_HIGHBITDEPTH
  4732. check_initial_width(cpi, cm->use_highbitdepth, 1, 1);
  4733. #else
  4734. check_initial_width(cpi, 1, 1);
  4735. #endif // CONFIG_VP9_HIGHBITDEPTH
  4736. #if CONFIG_VP9_TEMPORAL_DENOISING
  4737. setup_denoiser_buffer(cpi);
  4738. #endif
  4739. if (width) {
  4740. cm->width = width;
  4741. if (cm->width > cpi->initial_width) {
  4742. cm->width = cpi->initial_width;
  4743. printf("Warning: Desired width too large, changed to %d\n", cm->width);
  4744. }
  4745. }
  4746. if (height) {
  4747. cm->height = height;
  4748. if (cm->height > cpi->initial_height) {
  4749. cm->height = cpi->initial_height;
  4750. printf("Warning: Desired height too large, changed to %d\n", cm->height);
  4751. }
  4752. }
  4753. assert(cm->width <= cpi->initial_width);
  4754. assert(cm->height <= cpi->initial_height);
  4755. update_frame_size(cpi);
  4756. return 0;
  4757. }
  4758. void vp9_set_svc(VP9_COMP *cpi, int use_svc) {
  4759. cpi->use_svc = use_svc;
  4760. return;
  4761. }
  4762. int vp9_get_quantizer(VP9_COMP *cpi) { return cpi->common.base_qindex; }
  4763. void vp9_apply_encoding_flags(VP9_COMP *cpi, vpx_enc_frame_flags_t flags) {
  4764. if (flags &
  4765. (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF)) {
  4766. int ref = 7;
  4767. if (flags & VP8_EFLAG_NO_REF_LAST) ref ^= VP9_LAST_FLAG;
  4768. if (flags & VP8_EFLAG_NO_REF_GF) ref ^= VP9_GOLD_FLAG;
  4769. if (flags & VP8_EFLAG_NO_REF_ARF) ref ^= VP9_ALT_FLAG;
  4770. vp9_use_as_reference(cpi, ref);
  4771. }
  4772. if (flags &
  4773. (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
  4774. VP8_EFLAG_FORCE_GF | VP8_EFLAG_FORCE_ARF)) {
  4775. int upd = 7;
  4776. if (flags & VP8_EFLAG_NO_UPD_LAST) upd ^= VP9_LAST_FLAG;
  4777. if (flags & VP8_EFLAG_NO_UPD_GF) upd ^= VP9_GOLD_FLAG;
  4778. if (flags & VP8_EFLAG_NO_UPD_ARF) upd ^= VP9_ALT_FLAG;
  4779. vp9_update_reference(cpi, upd);
  4780. }
  4781. if (flags & VP8_EFLAG_NO_UPD_ENTROPY) {
  4782. vp9_update_entropy(cpi, 0);
  4783. }
  4784. }
  4785. void vp9_set_row_mt(VP9_COMP *cpi) {
  4786. // Enable row based multi-threading for supported modes of encoding
  4787. cpi->row_mt = 0;
  4788. if (((cpi->oxcf.mode == GOOD || cpi->oxcf.mode == BEST) &&
  4789. cpi->oxcf.speed < 5 && cpi->oxcf.pass == 1) &&
  4790. cpi->oxcf.row_mt && !cpi->use_svc)
  4791. cpi->row_mt = 1;
  4792. if (cpi->oxcf.mode == GOOD && cpi->oxcf.speed < 5 &&
  4793. (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) && cpi->oxcf.row_mt &&
  4794. !cpi->use_svc)
  4795. cpi->row_mt = 1;
  4796. // In realtime mode, enable row based multi-threading for all the speed levels
  4797. // where non-rd path is used.
  4798. if (cpi->oxcf.mode == REALTIME && cpi->oxcf.speed >= 5 && cpi->oxcf.row_mt) {
  4799. cpi->row_mt = 1;
  4800. }
  4801. if (cpi->row_mt)
  4802. cpi->row_mt_bit_exact = 1;
  4803. else
  4804. cpi->row_mt_bit_exact = 0;
  4805. }