2
0

vp9_encoder.c 177 KB

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