vp9_encodeframe.c 165 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387
  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <limits.h>
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include "./vp9_rtcd.h"
  14. #include "./vpx_dsp_rtcd.h"
  15. #include "./vpx_config.h"
  16. #include "vpx_dsp/vpx_dsp_common.h"
  17. #include "vpx_ports/mem.h"
  18. #include "vpx_ports/vpx_timer.h"
  19. #include "vpx_ports/system_state.h"
  20. #include "vp9/common/vp9_common.h"
  21. #include "vp9/common/vp9_entropy.h"
  22. #include "vp9/common/vp9_entropymode.h"
  23. #include "vp9/common/vp9_idct.h"
  24. #include "vp9/common/vp9_mvref_common.h"
  25. #include "vp9/common/vp9_pred_common.h"
  26. #include "vp9/common/vp9_quant_common.h"
  27. #include "vp9/common/vp9_reconintra.h"
  28. #include "vp9/common/vp9_reconinter.h"
  29. #include "vp9/common/vp9_seg_common.h"
  30. #include "vp9/common/vp9_tile_common.h"
  31. #include "vp9/encoder/vp9_aq_360.h"
  32. #include "vp9/encoder/vp9_aq_complexity.h"
  33. #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
  34. #include "vp9/encoder/vp9_aq_variance.h"
  35. #include "vp9/encoder/vp9_encodeframe.h"
  36. #include "vp9/encoder/vp9_encodemb.h"
  37. #include "vp9/encoder/vp9_encodemv.h"
  38. #include "vp9/encoder/vp9_ethread.h"
  39. #include "vp9/encoder/vp9_extend.h"
  40. #include "vp9/encoder/vp9_pickmode.h"
  41. #include "vp9/encoder/vp9_rd.h"
  42. #include "vp9/encoder/vp9_rdopt.h"
  43. #include "vp9/encoder/vp9_segmentation.h"
  44. #include "vp9/encoder/vp9_tokenize.h"
  45. static void encode_superblock(VP9_COMP *cpi, ThreadData *td, TOKENEXTRA **t,
  46. int output_enabled, int mi_row, int mi_col,
  47. BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx);
  48. // This is used as a reference when computing the source variance for the
  49. // purpose of activity masking.
  50. // Eventually this should be replaced by custom no-reference routines,
  51. // which will be faster.
  52. static const uint8_t VP9_VAR_OFFS[64] = {
  53. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  54. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  55. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  56. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  57. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128
  58. };
  59. #if CONFIG_VP9_HIGHBITDEPTH
  60. static const uint16_t VP9_HIGH_VAR_OFFS_8[64] = {
  61. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  62. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  63. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  64. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  65. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128
  66. };
  67. static const uint16_t VP9_HIGH_VAR_OFFS_10[64] = {
  68. 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
  69. 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
  70. 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
  71. 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
  72. 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
  73. 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
  74. 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
  75. 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4
  76. };
  77. static const uint16_t VP9_HIGH_VAR_OFFS_12[64] = {
  78. 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  79. 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  80. 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  81. 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  82. 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  83. 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  84. 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  85. 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  86. 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  87. 128 * 16
  88. };
  89. #endif // CONFIG_VP9_HIGHBITDEPTH
  90. unsigned int vp9_get_sby_perpixel_variance(VP9_COMP *cpi,
  91. const struct buf_2d *ref,
  92. BLOCK_SIZE bs) {
  93. unsigned int sse;
  94. const unsigned int var =
  95. cpi->fn_ptr[bs].vf(ref->buf, ref->stride, VP9_VAR_OFFS, 0, &sse);
  96. return ROUND_POWER_OF_TWO(var, num_pels_log2_lookup[bs]);
  97. }
  98. #if CONFIG_VP9_HIGHBITDEPTH
  99. unsigned int vp9_high_get_sby_perpixel_variance(VP9_COMP *cpi,
  100. const struct buf_2d *ref,
  101. BLOCK_SIZE bs, int bd) {
  102. unsigned int var, sse;
  103. switch (bd) {
  104. case 10:
  105. var =
  106. cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
  107. CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_10), 0, &sse);
  108. break;
  109. case 12:
  110. var =
  111. cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
  112. CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_12), 0, &sse);
  113. break;
  114. case 8:
  115. default:
  116. var =
  117. cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
  118. CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_8), 0, &sse);
  119. break;
  120. }
  121. return ROUND64_POWER_OF_TWO((int64_t)var, num_pels_log2_lookup[bs]);
  122. }
  123. #endif // CONFIG_VP9_HIGHBITDEPTH
  124. static unsigned int get_sby_perpixel_diff_variance(VP9_COMP *cpi,
  125. const struct buf_2d *ref,
  126. int mi_row, int mi_col,
  127. BLOCK_SIZE bs) {
  128. unsigned int sse, var;
  129. uint8_t *last_y;
  130. const YV12_BUFFER_CONFIG *last = get_ref_frame_buffer(cpi, LAST_FRAME);
  131. assert(last != NULL);
  132. last_y =
  133. &last->y_buffer[mi_row * MI_SIZE * last->y_stride + mi_col * MI_SIZE];
  134. var = cpi->fn_ptr[bs].vf(ref->buf, ref->stride, last_y, last->y_stride, &sse);
  135. return ROUND_POWER_OF_TWO(var, num_pels_log2_lookup[bs]);
  136. }
  137. static BLOCK_SIZE get_rd_var_based_fixed_partition(VP9_COMP *cpi, MACROBLOCK *x,
  138. int mi_row, int mi_col) {
  139. unsigned int var = get_sby_perpixel_diff_variance(
  140. cpi, &x->plane[0].src, mi_row, mi_col, BLOCK_64X64);
  141. if (var < 8)
  142. return BLOCK_64X64;
  143. else if (var < 128)
  144. return BLOCK_32X32;
  145. else if (var < 2048)
  146. return BLOCK_16X16;
  147. else
  148. return BLOCK_8X8;
  149. }
  150. // Lighter version of set_offsets that only sets the mode info
  151. // pointers.
  152. static INLINE void set_mode_info_offsets(VP9_COMMON *const cm,
  153. MACROBLOCK *const x,
  154. MACROBLOCKD *const xd, int mi_row,
  155. int mi_col) {
  156. const int idx_str = xd->mi_stride * mi_row + mi_col;
  157. xd->mi = cm->mi_grid_visible + idx_str;
  158. xd->mi[0] = cm->mi + idx_str;
  159. x->mbmi_ext = x->mbmi_ext_base + (mi_row * cm->mi_cols + mi_col);
  160. }
  161. static void set_offsets(VP9_COMP *cpi, const TileInfo *const tile,
  162. MACROBLOCK *const x, int mi_row, int mi_col,
  163. BLOCK_SIZE bsize) {
  164. VP9_COMMON *const cm = &cpi->common;
  165. MACROBLOCKD *const xd = &x->e_mbd;
  166. MODE_INFO *mi;
  167. const int mi_width = num_8x8_blocks_wide_lookup[bsize];
  168. const int mi_height = num_8x8_blocks_high_lookup[bsize];
  169. const struct segmentation *const seg = &cm->seg;
  170. MvLimits *const mv_limits = &x->mv_limits;
  171. set_skip_context(xd, mi_row, mi_col);
  172. set_mode_info_offsets(cm, x, xd, mi_row, mi_col);
  173. mi = xd->mi[0];
  174. // Set up destination pointers.
  175. vp9_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
  176. // Set up limit values for MV components.
  177. // Mv beyond the range do not produce new/different prediction block.
  178. mv_limits->row_min = -(((mi_row + mi_height) * MI_SIZE) + VP9_INTERP_EXTEND);
  179. mv_limits->col_min = -(((mi_col + mi_width) * MI_SIZE) + VP9_INTERP_EXTEND);
  180. mv_limits->row_max = (cm->mi_rows - mi_row) * MI_SIZE + VP9_INTERP_EXTEND;
  181. mv_limits->col_max = (cm->mi_cols - mi_col) * MI_SIZE + VP9_INTERP_EXTEND;
  182. // Set up distance of MB to edge of frame in 1/8th pel units.
  183. assert(!(mi_col & (mi_width - 1)) && !(mi_row & (mi_height - 1)));
  184. set_mi_row_col(xd, tile, mi_row, mi_height, mi_col, mi_width, cm->mi_rows,
  185. cm->mi_cols);
  186. // Set up source buffers.
  187. vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col);
  188. // R/D setup.
  189. x->rddiv = cpi->rd.RDDIV;
  190. x->rdmult = cpi->rd.RDMULT;
  191. // Setup segment ID.
  192. if (seg->enabled) {
  193. if (cpi->oxcf.aq_mode != VARIANCE_AQ && cpi->oxcf.aq_mode != LOOKAHEAD_AQ &&
  194. cpi->oxcf.aq_mode != EQUATOR360_AQ) {
  195. const uint8_t *const map =
  196. seg->update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
  197. mi->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
  198. }
  199. vp9_init_plane_quantizers(cpi, x);
  200. x->encode_breakout = cpi->segment_encode_breakout[mi->segment_id];
  201. } else {
  202. mi->segment_id = 0;
  203. x->encode_breakout = cpi->encode_breakout;
  204. }
  205. // required by vp9_append_sub8x8_mvs_for_idx() and vp9_find_best_ref_mvs()
  206. xd->tile = *tile;
  207. }
  208. static void duplicate_mode_info_in_sb(VP9_COMMON *cm, MACROBLOCKD *xd,
  209. int mi_row, int mi_col,
  210. BLOCK_SIZE bsize) {
  211. const int block_width =
  212. VPXMIN(num_8x8_blocks_wide_lookup[bsize], cm->mi_cols - mi_col);
  213. const int block_height =
  214. VPXMIN(num_8x8_blocks_high_lookup[bsize], cm->mi_rows - mi_row);
  215. const int mi_stride = xd->mi_stride;
  216. MODE_INFO *const src_mi = xd->mi[0];
  217. int i, j;
  218. for (j = 0; j < block_height; ++j)
  219. for (i = 0; i < block_width; ++i) xd->mi[j * mi_stride + i] = src_mi;
  220. }
  221. static void set_block_size(VP9_COMP *const cpi, MACROBLOCK *const x,
  222. MACROBLOCKD *const xd, int mi_row, int mi_col,
  223. BLOCK_SIZE bsize) {
  224. if (cpi->common.mi_cols > mi_col && cpi->common.mi_rows > mi_row) {
  225. set_mode_info_offsets(&cpi->common, x, xd, mi_row, mi_col);
  226. xd->mi[0]->sb_type = bsize;
  227. }
  228. }
  229. typedef struct {
  230. int64_t sum_square_error;
  231. int64_t sum_error;
  232. int log2_count;
  233. int variance;
  234. } var;
  235. typedef struct {
  236. var none;
  237. var horz[2];
  238. var vert[2];
  239. } partition_variance;
  240. typedef struct {
  241. partition_variance part_variances;
  242. var split[4];
  243. } v4x4;
  244. typedef struct {
  245. partition_variance part_variances;
  246. v4x4 split[4];
  247. } v8x8;
  248. typedef struct {
  249. partition_variance part_variances;
  250. v8x8 split[4];
  251. } v16x16;
  252. typedef struct {
  253. partition_variance part_variances;
  254. v16x16 split[4];
  255. } v32x32;
  256. typedef struct {
  257. partition_variance part_variances;
  258. v32x32 split[4];
  259. } v64x64;
  260. typedef struct {
  261. partition_variance *part_variances;
  262. var *split[4];
  263. } variance_node;
  264. typedef enum {
  265. V16X16,
  266. V32X32,
  267. V64X64,
  268. } TREE_LEVEL;
  269. static void tree_to_node(void *data, BLOCK_SIZE bsize, variance_node *node) {
  270. int i;
  271. node->part_variances = NULL;
  272. switch (bsize) {
  273. case BLOCK_64X64: {
  274. v64x64 *vt = (v64x64 *)data;
  275. node->part_variances = &vt->part_variances;
  276. for (i = 0; i < 4; i++)
  277. node->split[i] = &vt->split[i].part_variances.none;
  278. break;
  279. }
  280. case BLOCK_32X32: {
  281. v32x32 *vt = (v32x32 *)data;
  282. node->part_variances = &vt->part_variances;
  283. for (i = 0; i < 4; i++)
  284. node->split[i] = &vt->split[i].part_variances.none;
  285. break;
  286. }
  287. case BLOCK_16X16: {
  288. v16x16 *vt = (v16x16 *)data;
  289. node->part_variances = &vt->part_variances;
  290. for (i = 0; i < 4; i++)
  291. node->split[i] = &vt->split[i].part_variances.none;
  292. break;
  293. }
  294. case BLOCK_8X8: {
  295. v8x8 *vt = (v8x8 *)data;
  296. node->part_variances = &vt->part_variances;
  297. for (i = 0; i < 4; i++)
  298. node->split[i] = &vt->split[i].part_variances.none;
  299. break;
  300. }
  301. case BLOCK_4X4: {
  302. v4x4 *vt = (v4x4 *)data;
  303. node->part_variances = &vt->part_variances;
  304. for (i = 0; i < 4; i++) node->split[i] = &vt->split[i];
  305. break;
  306. }
  307. default: {
  308. assert(0);
  309. break;
  310. }
  311. }
  312. }
  313. // Set variance values given sum square error, sum error, count.
  314. static void fill_variance(int64_t s2, int64_t s, int c, var *v) {
  315. v->sum_square_error = s2;
  316. v->sum_error = s;
  317. v->log2_count = c;
  318. }
  319. static void get_variance(var *v) {
  320. v->variance =
  321. (int)(256 * (v->sum_square_error -
  322. ((v->sum_error * v->sum_error) >> v->log2_count)) >>
  323. v->log2_count);
  324. }
  325. static void sum_2_variances(const var *a, const var *b, var *r) {
  326. assert(a->log2_count == b->log2_count);
  327. fill_variance(a->sum_square_error + b->sum_square_error,
  328. a->sum_error + b->sum_error, a->log2_count + 1, r);
  329. }
  330. static void fill_variance_tree(void *data, BLOCK_SIZE bsize) {
  331. variance_node node;
  332. memset(&node, 0, sizeof(node));
  333. tree_to_node(data, bsize, &node);
  334. sum_2_variances(node.split[0], node.split[1], &node.part_variances->horz[0]);
  335. sum_2_variances(node.split[2], node.split[3], &node.part_variances->horz[1]);
  336. sum_2_variances(node.split[0], node.split[2], &node.part_variances->vert[0]);
  337. sum_2_variances(node.split[1], node.split[3], &node.part_variances->vert[1]);
  338. sum_2_variances(&node.part_variances->vert[0], &node.part_variances->vert[1],
  339. &node.part_variances->none);
  340. }
  341. static int set_vt_partitioning(VP9_COMP *cpi, MACROBLOCK *const x,
  342. MACROBLOCKD *const xd, void *data,
  343. BLOCK_SIZE bsize, int mi_row, int mi_col,
  344. int64_t threshold, BLOCK_SIZE bsize_min,
  345. int force_split) {
  346. VP9_COMMON *const cm = &cpi->common;
  347. variance_node vt;
  348. const int block_width = num_8x8_blocks_wide_lookup[bsize];
  349. const int block_height = num_8x8_blocks_high_lookup[bsize];
  350. assert(block_height == block_width);
  351. tree_to_node(data, bsize, &vt);
  352. if (force_split == 1) return 0;
  353. // For bsize=bsize_min (16x16/8x8 for 8x8/4x4 downsampling), select if
  354. // variance is below threshold, otherwise split will be selected.
  355. // No check for vert/horiz split as too few samples for variance.
  356. if (bsize == bsize_min) {
  357. // Variance already computed to set the force_split.
  358. if (cm->frame_type == KEY_FRAME) get_variance(&vt.part_variances->none);
  359. if (mi_col + block_width / 2 < cm->mi_cols &&
  360. mi_row + block_height / 2 < cm->mi_rows &&
  361. vt.part_variances->none.variance < threshold) {
  362. set_block_size(cpi, x, xd, mi_row, mi_col, bsize);
  363. return 1;
  364. }
  365. return 0;
  366. } else if (bsize > bsize_min) {
  367. // Variance already computed to set the force_split.
  368. if (cm->frame_type == KEY_FRAME) get_variance(&vt.part_variances->none);
  369. // For key frame: take split for bsize above 32X32 or very high variance.
  370. if (cm->frame_type == KEY_FRAME &&
  371. (bsize > BLOCK_32X32 ||
  372. vt.part_variances->none.variance > (threshold << 4))) {
  373. return 0;
  374. }
  375. // If variance is low, take the bsize (no split).
  376. if (mi_col + block_width / 2 < cm->mi_cols &&
  377. mi_row + block_height / 2 < cm->mi_rows &&
  378. vt.part_variances->none.variance < threshold) {
  379. set_block_size(cpi, x, xd, mi_row, mi_col, bsize);
  380. return 1;
  381. }
  382. // Check vertical split.
  383. if (mi_row + block_height / 2 < cm->mi_rows) {
  384. BLOCK_SIZE subsize = get_subsize(bsize, PARTITION_VERT);
  385. get_variance(&vt.part_variances->vert[0]);
  386. get_variance(&vt.part_variances->vert[1]);
  387. if (vt.part_variances->vert[0].variance < threshold &&
  388. vt.part_variances->vert[1].variance < threshold &&
  389. get_plane_block_size(subsize, &xd->plane[1]) < BLOCK_INVALID) {
  390. set_block_size(cpi, x, xd, mi_row, mi_col, subsize);
  391. set_block_size(cpi, x, xd, mi_row, mi_col + block_width / 2, subsize);
  392. return 1;
  393. }
  394. }
  395. // Check horizontal split.
  396. if (mi_col + block_width / 2 < cm->mi_cols) {
  397. BLOCK_SIZE subsize = get_subsize(bsize, PARTITION_HORZ);
  398. get_variance(&vt.part_variances->horz[0]);
  399. get_variance(&vt.part_variances->horz[1]);
  400. if (vt.part_variances->horz[0].variance < threshold &&
  401. vt.part_variances->horz[1].variance < threshold &&
  402. get_plane_block_size(subsize, &xd->plane[1]) < BLOCK_INVALID) {
  403. set_block_size(cpi, x, xd, mi_row, mi_col, subsize);
  404. set_block_size(cpi, x, xd, mi_row + block_height / 2, mi_col, subsize);
  405. return 1;
  406. }
  407. }
  408. return 0;
  409. }
  410. return 0;
  411. }
  412. // Set the variance split thresholds for following the block sizes:
  413. // 0 - threshold_64x64, 1 - threshold_32x32, 2 - threshold_16x16,
  414. // 3 - vbp_threshold_8x8. vbp_threshold_8x8 (to split to 4x4 partition) is
  415. // currently only used on key frame.
  416. static void set_vbp_thresholds(VP9_COMP *cpi, int64_t thresholds[], int q) {
  417. VP9_COMMON *const cm = &cpi->common;
  418. const int is_key_frame = (cm->frame_type == KEY_FRAME);
  419. const int threshold_multiplier = is_key_frame ? 20 : 1;
  420. int64_t threshold_base =
  421. (int64_t)(threshold_multiplier * cpi->y_dequant[q][1]);
  422. if (is_key_frame) {
  423. thresholds[0] = threshold_base;
  424. thresholds[1] = threshold_base >> 2;
  425. thresholds[2] = threshold_base >> 2;
  426. thresholds[3] = threshold_base << 2;
  427. } else {
  428. // Increase base variance threshold based on estimated noise level.
  429. if (cpi->noise_estimate.enabled) {
  430. NOISE_LEVEL noise_level =
  431. vp9_noise_estimate_extract_level(&cpi->noise_estimate);
  432. if (noise_level == kHigh)
  433. threshold_base = 3 * threshold_base;
  434. else if (noise_level == kMedium)
  435. threshold_base = threshold_base << 1;
  436. else if (noise_level < kLow)
  437. threshold_base = (7 * threshold_base) >> 3;
  438. }
  439. thresholds[0] = threshold_base;
  440. thresholds[2] = threshold_base << cpi->oxcf.speed;
  441. if (cm->width <= 352 && cm->height <= 288) {
  442. thresholds[0] = threshold_base >> 3;
  443. thresholds[1] = threshold_base >> 1;
  444. thresholds[2] = threshold_base << 3;
  445. } else if (cm->width < 1280 && cm->height < 720) {
  446. thresholds[1] = (5 * threshold_base) >> 2;
  447. } else if (cm->width < 1920 && cm->height < 1080) {
  448. thresholds[1] = threshold_base << 1;
  449. } else {
  450. thresholds[1] = (5 * threshold_base) >> 1;
  451. }
  452. }
  453. }
  454. void vp9_set_variance_partition_thresholds(VP9_COMP *cpi, int q) {
  455. VP9_COMMON *const cm = &cpi->common;
  456. SPEED_FEATURES *const sf = &cpi->sf;
  457. const int is_key_frame = (cm->frame_type == KEY_FRAME);
  458. if (sf->partition_search_type != VAR_BASED_PARTITION &&
  459. sf->partition_search_type != REFERENCE_PARTITION) {
  460. return;
  461. } else {
  462. set_vbp_thresholds(cpi, cpi->vbp_thresholds, q);
  463. // The thresholds below are not changed locally.
  464. if (is_key_frame) {
  465. cpi->vbp_threshold_sad = 0;
  466. cpi->vbp_bsize_min = BLOCK_8X8;
  467. } else {
  468. if (cm->width <= 352 && cm->height <= 288)
  469. cpi->vbp_threshold_sad = 10;
  470. else
  471. cpi->vbp_threshold_sad = (cpi->y_dequant[q][1] << 1) > 1000
  472. ? (cpi->y_dequant[q][1] << 1)
  473. : 1000;
  474. cpi->vbp_bsize_min = BLOCK_16X16;
  475. }
  476. cpi->vbp_threshold_minmax = 15 + (q >> 3);
  477. }
  478. }
  479. // Compute the minmax over the 8x8 subblocks.
  480. static int compute_minmax_8x8(const uint8_t *s, int sp, const uint8_t *d,
  481. int dp, int x16_idx, int y16_idx,
  482. #if CONFIG_VP9_HIGHBITDEPTH
  483. int highbd_flag,
  484. #endif
  485. int pixels_wide, int pixels_high) {
  486. int k;
  487. int minmax_max = 0;
  488. int minmax_min = 255;
  489. // Loop over the 4 8x8 subblocks.
  490. for (k = 0; k < 4; k++) {
  491. int x8_idx = x16_idx + ((k & 1) << 3);
  492. int y8_idx = y16_idx + ((k >> 1) << 3);
  493. int min = 0;
  494. int max = 0;
  495. if (x8_idx < pixels_wide && y8_idx < pixels_high) {
  496. #if CONFIG_VP9_HIGHBITDEPTH
  497. if (highbd_flag & YV12_FLAG_HIGHBITDEPTH) {
  498. vpx_highbd_minmax_8x8(s + y8_idx * sp + x8_idx, sp,
  499. d + y8_idx * dp + x8_idx, dp, &min, &max);
  500. } else {
  501. vpx_minmax_8x8(s + y8_idx * sp + x8_idx, sp, d + y8_idx * dp + x8_idx,
  502. dp, &min, &max);
  503. }
  504. #else
  505. vpx_minmax_8x8(s + y8_idx * sp + x8_idx, sp, d + y8_idx * dp + x8_idx, dp,
  506. &min, &max);
  507. #endif
  508. if ((max - min) > minmax_max) minmax_max = (max - min);
  509. if ((max - min) < minmax_min) minmax_min = (max - min);
  510. }
  511. }
  512. return (minmax_max - minmax_min);
  513. }
  514. static void fill_variance_4x4avg(const uint8_t *s, int sp, const uint8_t *d,
  515. int dp, int x8_idx, int y8_idx, v8x8 *vst,
  516. #if CONFIG_VP9_HIGHBITDEPTH
  517. int highbd_flag,
  518. #endif
  519. int pixels_wide, int pixels_high,
  520. int is_key_frame) {
  521. int k;
  522. for (k = 0; k < 4; k++) {
  523. int x4_idx = x8_idx + ((k & 1) << 2);
  524. int y4_idx = y8_idx + ((k >> 1) << 2);
  525. unsigned int sse = 0;
  526. int sum = 0;
  527. if (x4_idx < pixels_wide && y4_idx < pixels_high) {
  528. int s_avg;
  529. int d_avg = 128;
  530. #if CONFIG_VP9_HIGHBITDEPTH
  531. if (highbd_flag & YV12_FLAG_HIGHBITDEPTH) {
  532. s_avg = vpx_highbd_avg_4x4(s + y4_idx * sp + x4_idx, sp);
  533. if (!is_key_frame)
  534. d_avg = vpx_highbd_avg_4x4(d + y4_idx * dp + x4_idx, dp);
  535. } else {
  536. s_avg = vpx_avg_4x4(s + y4_idx * sp + x4_idx, sp);
  537. if (!is_key_frame) d_avg = vpx_avg_4x4(d + y4_idx * dp + x4_idx, dp);
  538. }
  539. #else
  540. s_avg = vpx_avg_4x4(s + y4_idx * sp + x4_idx, sp);
  541. if (!is_key_frame) d_avg = vpx_avg_4x4(d + y4_idx * dp + x4_idx, dp);
  542. #endif
  543. sum = s_avg - d_avg;
  544. sse = sum * sum;
  545. }
  546. fill_variance(sse, sum, 0, &vst->split[k].part_variances.none);
  547. }
  548. }
  549. static void fill_variance_8x8avg(const uint8_t *s, int sp, const uint8_t *d,
  550. int dp, int x16_idx, int y16_idx, v16x16 *vst,
  551. #if CONFIG_VP9_HIGHBITDEPTH
  552. int highbd_flag,
  553. #endif
  554. int pixels_wide, int pixels_high,
  555. int is_key_frame) {
  556. int k;
  557. for (k = 0; k < 4; k++) {
  558. int x8_idx = x16_idx + ((k & 1) << 3);
  559. int y8_idx = y16_idx + ((k >> 1) << 3);
  560. unsigned int sse = 0;
  561. int sum = 0;
  562. if (x8_idx < pixels_wide && y8_idx < pixels_high) {
  563. int s_avg;
  564. int d_avg = 128;
  565. #if CONFIG_VP9_HIGHBITDEPTH
  566. if (highbd_flag & YV12_FLAG_HIGHBITDEPTH) {
  567. s_avg = vpx_highbd_avg_8x8(s + y8_idx * sp + x8_idx, sp);
  568. if (!is_key_frame)
  569. d_avg = vpx_highbd_avg_8x8(d + y8_idx * dp + x8_idx, dp);
  570. } else {
  571. s_avg = vpx_avg_8x8(s + y8_idx * sp + x8_idx, sp);
  572. if (!is_key_frame) d_avg = vpx_avg_8x8(d + y8_idx * dp + x8_idx, dp);
  573. }
  574. #else
  575. s_avg = vpx_avg_8x8(s + y8_idx * sp + x8_idx, sp);
  576. if (!is_key_frame) d_avg = vpx_avg_8x8(d + y8_idx * dp + x8_idx, dp);
  577. #endif
  578. sum = s_avg - d_avg;
  579. sse = sum * sum;
  580. }
  581. fill_variance(sse, sum, 0, &vst->split[k].part_variances.none);
  582. }
  583. }
  584. #if !CONFIG_VP9_HIGHBITDEPTH
  585. // Check if most of the superblock is skin content, and if so, force split to
  586. // 32x32, and set x->sb_is_skin for use in mode selection.
  587. static int skin_sb_split(VP9_COMP *cpi, MACROBLOCK *x, const int low_res,
  588. int mi_row, int mi_col, int *force_split) {
  589. VP9_COMMON *const cm = &cpi->common;
  590. // Avoid checking superblocks on/near boundary and avoid low resolutions.
  591. // Note superblock may still pick 64X64 if y_sad is very small
  592. // (i.e., y_sad < cpi->vbp_threshold_sad) below. For now leave this as is.
  593. if (!low_res && (mi_col >= 8 && mi_col + 8 < cm->mi_cols && mi_row >= 8 &&
  594. mi_row + 8 < cm->mi_rows)) {
  595. int num_16x16_skin = 0;
  596. int num_16x16_nonskin = 0;
  597. uint8_t *ysignal = x->plane[0].src.buf;
  598. uint8_t *usignal = x->plane[1].src.buf;
  599. uint8_t *vsignal = x->plane[2].src.buf;
  600. int sp = x->plane[0].src.stride;
  601. int spuv = x->plane[1].src.stride;
  602. const int block_index = mi_row * cm->mi_cols + mi_col;
  603. const int bw = num_8x8_blocks_wide_lookup[BLOCK_64X64];
  604. const int bh = num_8x8_blocks_high_lookup[BLOCK_64X64];
  605. const int xmis = VPXMIN(cm->mi_cols - mi_col, bw);
  606. const int ymis = VPXMIN(cm->mi_rows - mi_row, bh);
  607. // Loop through the 16x16 sub-blocks.
  608. int i, j;
  609. for (i = 0; i < ymis; i += 2) {
  610. for (j = 0; j < xmis; j += 2) {
  611. int bl_index = block_index + i * cm->mi_cols + j;
  612. int bl_index1 = bl_index + 1;
  613. int bl_index2 = bl_index + cm->mi_cols;
  614. int bl_index3 = bl_index2 + 1;
  615. int consec_zeromv =
  616. VPXMIN(cpi->consec_zero_mv[bl_index],
  617. VPXMIN(cpi->consec_zero_mv[bl_index1],
  618. VPXMIN(cpi->consec_zero_mv[bl_index2],
  619. cpi->consec_zero_mv[bl_index3])));
  620. int is_skin = vp9_compute_skin_block(
  621. ysignal, usignal, vsignal, sp, spuv, BLOCK_16X16, consec_zeromv, 0);
  622. num_16x16_skin += is_skin;
  623. num_16x16_nonskin += (1 - is_skin);
  624. if (num_16x16_nonskin > 3) {
  625. // Exit loop if at least 4 of the 16x16 blocks are not skin.
  626. i = ymis;
  627. break;
  628. }
  629. ysignal += 16;
  630. usignal += 8;
  631. vsignal += 8;
  632. }
  633. ysignal += (sp << 4) - 64;
  634. usignal += (spuv << 3) - 32;
  635. vsignal += (spuv << 3) - 32;
  636. }
  637. if (num_16x16_skin > 12) {
  638. *force_split = 1;
  639. return 1;
  640. }
  641. }
  642. return 0;
  643. }
  644. #endif
  645. static void set_low_temp_var_flag(VP9_COMP *cpi, MACROBLOCK *x, MACROBLOCKD *xd,
  646. v64x64 *vt, int64_t thresholds[],
  647. MV_REFERENCE_FRAME ref_frame_partition,
  648. int mi_col, int mi_row) {
  649. int i, j;
  650. VP9_COMMON *const cm = &cpi->common;
  651. const int mv_thr = cm->width > 640 ? 8 : 4;
  652. // Check temporal variance for bsize >= 16x16, if LAST_FRAME was selected and
  653. // int_pro mv is small. If the temporal variance is small set the flag
  654. // variance_low for the block. The variance threshold can be adjusted, the
  655. // higher the more aggressive.
  656. if (ref_frame_partition == LAST_FRAME &&
  657. (cpi->sf.short_circuit_low_temp_var == 1 ||
  658. (xd->mi[0]->mv[0].as_mv.col < mv_thr &&
  659. xd->mi[0]->mv[0].as_mv.col > -mv_thr &&
  660. xd->mi[0]->mv[0].as_mv.row < mv_thr &&
  661. xd->mi[0]->mv[0].as_mv.row > -mv_thr))) {
  662. if (xd->mi[0]->sb_type == BLOCK_64X64) {
  663. if ((vt->part_variances).none.variance < (thresholds[0] >> 1))
  664. x->variance_low[0] = 1;
  665. } else if (xd->mi[0]->sb_type == BLOCK_64X32) {
  666. for (i = 0; i < 2; i++) {
  667. if (vt->part_variances.horz[i].variance < (thresholds[0] >> 2))
  668. x->variance_low[i + 1] = 1;
  669. }
  670. } else if (xd->mi[0]->sb_type == BLOCK_32X64) {
  671. for (i = 0; i < 2; i++) {
  672. if (vt->part_variances.vert[i].variance < (thresholds[0] >> 2))
  673. x->variance_low[i + 3] = 1;
  674. }
  675. } else {
  676. for (i = 0; i < 4; i++) {
  677. const int idx[4][2] = { { 0, 0 }, { 0, 4 }, { 4, 0 }, { 4, 4 } };
  678. const int idx_str =
  679. cm->mi_stride * (mi_row + idx[i][0]) + mi_col + idx[i][1];
  680. MODE_INFO **this_mi = cm->mi_grid_visible + idx_str;
  681. if (cm->mi_cols <= mi_col + idx[i][1] ||
  682. cm->mi_rows <= mi_row + idx[i][0])
  683. continue;
  684. if ((*this_mi)->sb_type == BLOCK_32X32) {
  685. if (vt->split[i].part_variances.none.variance < (thresholds[1] >> 1))
  686. x->variance_low[i + 5] = 1;
  687. } else if (cpi->sf.short_circuit_low_temp_var == 2) {
  688. // For 32x16 and 16x32 blocks, the flag is set on each 16x16 block
  689. // inside.
  690. if ((*this_mi)->sb_type == BLOCK_16X16 ||
  691. (*this_mi)->sb_type == BLOCK_32X16 ||
  692. (*this_mi)->sb_type == BLOCK_16X32) {
  693. for (j = 0; j < 4; j++) {
  694. if (vt->split[i].split[j].part_variances.none.variance <
  695. (thresholds[2] >> 8))
  696. x->variance_low[(i << 2) + j + 9] = 1;
  697. }
  698. }
  699. }
  700. }
  701. }
  702. }
  703. }
  704. static void chroma_check(VP9_COMP *cpi, MACROBLOCK *x, int bsize,
  705. unsigned int y_sad, int is_key_frame) {
  706. int i;
  707. MACROBLOCKD *xd = &x->e_mbd;
  708. if (is_key_frame) return;
  709. for (i = 1; i <= 2; ++i) {
  710. unsigned int uv_sad = UINT_MAX;
  711. struct macroblock_plane *p = &x->plane[i];
  712. struct macroblockd_plane *pd = &xd->plane[i];
  713. const BLOCK_SIZE bs = get_plane_block_size(bsize, pd);
  714. if (bs != BLOCK_INVALID)
  715. uv_sad = cpi->fn_ptr[bs].sdf(p->src.buf, p->src.stride, pd->dst.buf,
  716. pd->dst.stride);
  717. // TODO(marpan): Investigate if we should lower this threshold if
  718. // superblock is detected as skin.
  719. x->color_sensitivity[i - 1] = uv_sad > (y_sad >> 2);
  720. }
  721. }
  722. // This function chooses partitioning based on the variance between source and
  723. // reconstructed last, where variance is computed for down-sampled inputs.
  724. static int choose_partitioning(VP9_COMP *cpi, const TileInfo *const tile,
  725. MACROBLOCK *x, int mi_row, int mi_col) {
  726. VP9_COMMON *const cm = &cpi->common;
  727. MACROBLOCKD *xd = &x->e_mbd;
  728. int i, j, k, m;
  729. v64x64 vt;
  730. v16x16 vt2[16];
  731. int force_split[21];
  732. int avg_32x32;
  733. int avg_16x16[4];
  734. uint8_t *s;
  735. const uint8_t *d;
  736. int sp;
  737. int dp;
  738. unsigned int y_sad = UINT_MAX;
  739. BLOCK_SIZE bsize = BLOCK_64X64;
  740. // Ref frame used in partitioning.
  741. MV_REFERENCE_FRAME ref_frame_partition = LAST_FRAME;
  742. int pixels_wide = 64, pixels_high = 64;
  743. int64_t thresholds[4] = { cpi->vbp_thresholds[0], cpi->vbp_thresholds[1],
  744. cpi->vbp_thresholds[2], cpi->vbp_thresholds[3] };
  745. // For the variance computation under SVC mode, we treat the frame as key if
  746. // the reference (base layer frame) is key frame (i.e., is_key_frame == 1).
  747. const int is_key_frame =
  748. (cm->frame_type == KEY_FRAME ||
  749. (is_one_pass_cbr_svc(cpi) &&
  750. cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame));
  751. // Always use 4x4 partition for key frame.
  752. const int use_4x4_partition = cm->frame_type == KEY_FRAME;
  753. const int low_res = (cm->width <= 352 && cm->height <= 288);
  754. int variance4x4downsample[16];
  755. int segment_id;
  756. set_offsets(cpi, tile, x, mi_row, mi_col, BLOCK_64X64);
  757. segment_id = xd->mi[0]->segment_id;
  758. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled) {
  759. if (cyclic_refresh_segment_id_boosted(segment_id)) {
  760. int q = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex);
  761. set_vbp_thresholds(cpi, thresholds, q);
  762. }
  763. }
  764. memset(x->variance_low, 0, sizeof(x->variance_low));
  765. if (xd->mb_to_right_edge < 0) pixels_wide += (xd->mb_to_right_edge >> 3);
  766. if (xd->mb_to_bottom_edge < 0) pixels_high += (xd->mb_to_bottom_edge >> 3);
  767. s = x->plane[0].src.buf;
  768. sp = x->plane[0].src.stride;
  769. // Index for force_split: 0 for 64x64, 1-4 for 32x32 blocks,
  770. // 5-20 for the 16x16 blocks.
  771. force_split[0] = 0;
  772. if (!is_key_frame) {
  773. // In the case of spatial/temporal scalable coding, the assumption here is
  774. // that the temporal reference frame will always be of type LAST_FRAME.
  775. // TODO(marpan): If that assumption is broken, we need to revisit this code.
  776. MODE_INFO *mi = xd->mi[0];
  777. const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, LAST_FRAME);
  778. const YV12_BUFFER_CONFIG *yv12_g = NULL;
  779. unsigned int y_sad_g, y_sad_thr;
  780. bsize = BLOCK_32X32 + (mi_col + 4 < cm->mi_cols) * 2 +
  781. (mi_row + 4 < cm->mi_rows);
  782. assert(yv12 != NULL);
  783. if (!(is_one_pass_cbr_svc(cpi) && cpi->svc.spatial_layer_id)) {
  784. // For now, GOLDEN will not be used for non-zero spatial layers, since
  785. // it may not be a temporal reference.
  786. yv12_g = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
  787. }
  788. if (yv12_g && yv12_g != yv12 && (cpi->ref_frame_flags & VP9_GOLD_FLAG)) {
  789. vp9_setup_pre_planes(xd, 0, yv12_g, mi_row, mi_col,
  790. &cm->frame_refs[GOLDEN_FRAME - 1].sf);
  791. y_sad_g = cpi->fn_ptr[bsize].sdf(
  792. x->plane[0].src.buf, x->plane[0].src.stride, xd->plane[0].pre[0].buf,
  793. xd->plane[0].pre[0].stride);
  794. } else {
  795. y_sad_g = UINT_MAX;
  796. }
  797. vp9_setup_pre_planes(xd, 0, yv12, mi_row, mi_col,
  798. &cm->frame_refs[LAST_FRAME - 1].sf);
  799. mi->ref_frame[0] = LAST_FRAME;
  800. mi->ref_frame[1] = NONE;
  801. mi->sb_type = BLOCK_64X64;
  802. mi->mv[0].as_int = 0;
  803. mi->interp_filter = BILINEAR;
  804. y_sad = vp9_int_pro_motion_estimation(cpi, x, bsize, mi_row, mi_col);
  805. // Pick ref frame for partitioning, bias last frame when y_sad_g and y_sad
  806. // are close if short_circuit_low_temp_var is on.
  807. y_sad_thr = cpi->sf.short_circuit_low_temp_var ? (y_sad * 7) >> 3 : y_sad;
  808. if (y_sad_g < y_sad_thr) {
  809. vp9_setup_pre_planes(xd, 0, yv12_g, mi_row, mi_col,
  810. &cm->frame_refs[GOLDEN_FRAME - 1].sf);
  811. mi->ref_frame[0] = GOLDEN_FRAME;
  812. mi->mv[0].as_int = 0;
  813. y_sad = y_sad_g;
  814. ref_frame_partition = GOLDEN_FRAME;
  815. } else {
  816. x->pred_mv[LAST_FRAME] = mi->mv[0].as_mv;
  817. ref_frame_partition = LAST_FRAME;
  818. }
  819. set_ref_ptrs(cm, xd, mi->ref_frame[0], mi->ref_frame[1]);
  820. vp9_build_inter_predictors_sb(xd, mi_row, mi_col, BLOCK_64X64);
  821. x->sb_is_skin = 0;
  822. #if !CONFIG_VP9_HIGHBITDEPTH
  823. if (cpi->use_skin_detection)
  824. x->sb_is_skin =
  825. skin_sb_split(cpi, x, low_res, mi_row, mi_col, force_split);
  826. #endif
  827. d = xd->plane[0].dst.buf;
  828. dp = xd->plane[0].dst.stride;
  829. // If the y_sad is very small, take 64x64 as partition and exit.
  830. // Don't check on boosted segment for now, as 64x64 is suppressed there.
  831. if (segment_id == CR_SEGMENT_ID_BASE && y_sad < cpi->vbp_threshold_sad) {
  832. const int block_width = num_8x8_blocks_wide_lookup[BLOCK_64X64];
  833. const int block_height = num_8x8_blocks_high_lookup[BLOCK_64X64];
  834. if (mi_col + block_width / 2 < cm->mi_cols &&
  835. mi_row + block_height / 2 < cm->mi_rows) {
  836. set_block_size(cpi, x, xd, mi_row, mi_col, BLOCK_64X64);
  837. chroma_check(cpi, x, bsize, y_sad, is_key_frame);
  838. return 0;
  839. }
  840. }
  841. } else {
  842. d = VP9_VAR_OFFS;
  843. dp = 0;
  844. #if CONFIG_VP9_HIGHBITDEPTH
  845. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  846. switch (xd->bd) {
  847. case 10: d = CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_10); break;
  848. case 12: d = CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_12); break;
  849. case 8:
  850. default: d = CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_8); break;
  851. }
  852. }
  853. #endif // CONFIG_VP9_HIGHBITDEPTH
  854. }
  855. // Fill in the entire tree of 8x8 (or 4x4 under some conditions) variances
  856. // for splits.
  857. for (i = 0; i < 4; i++) {
  858. const int x32_idx = ((i & 1) << 5);
  859. const int y32_idx = ((i >> 1) << 5);
  860. const int i2 = i << 2;
  861. force_split[i + 1] = 0;
  862. avg_16x16[i] = 0;
  863. for (j = 0; j < 4; j++) {
  864. const int x16_idx = x32_idx + ((j & 1) << 4);
  865. const int y16_idx = y32_idx + ((j >> 1) << 4);
  866. const int split_index = 5 + i2 + j;
  867. v16x16 *vst = &vt.split[i].split[j];
  868. force_split[split_index] = 0;
  869. variance4x4downsample[i2 + j] = 0;
  870. if (!is_key_frame) {
  871. fill_variance_8x8avg(s, sp, d, dp, x16_idx, y16_idx, vst,
  872. #if CONFIG_VP9_HIGHBITDEPTH
  873. xd->cur_buf->flags,
  874. #endif
  875. pixels_wide, pixels_high, is_key_frame);
  876. fill_variance_tree(&vt.split[i].split[j], BLOCK_16X16);
  877. get_variance(&vt.split[i].split[j].part_variances.none);
  878. avg_16x16[i] += vt.split[i].split[j].part_variances.none.variance;
  879. if (vt.split[i].split[j].part_variances.none.variance > thresholds[2]) {
  880. // 16X16 variance is above threshold for split, so force split to 8x8
  881. // for this 16x16 block (this also forces splits for upper levels).
  882. force_split[split_index] = 1;
  883. force_split[i + 1] = 1;
  884. force_split[0] = 1;
  885. } else if (cpi->oxcf.speed < 8 &&
  886. vt.split[i].split[j].part_variances.none.variance >
  887. thresholds[1] &&
  888. !cyclic_refresh_segment_id_boosted(segment_id)) {
  889. // We have some nominal amount of 16x16 variance (based on average),
  890. // compute the minmax over the 8x8 sub-blocks, and if above threshold,
  891. // force split to 8x8 block for this 16x16 block.
  892. int minmax = compute_minmax_8x8(s, sp, d, dp, x16_idx, y16_idx,
  893. #if CONFIG_VP9_HIGHBITDEPTH
  894. xd->cur_buf->flags,
  895. #endif
  896. pixels_wide, pixels_high);
  897. if (minmax > cpi->vbp_threshold_minmax) {
  898. force_split[split_index] = 1;
  899. force_split[i + 1] = 1;
  900. force_split[0] = 1;
  901. }
  902. }
  903. }
  904. if (is_key_frame || (low_res &&
  905. vt.split[i].split[j].part_variances.none.variance >
  906. (thresholds[1] << 1))) {
  907. force_split[split_index] = 0;
  908. // Go down to 4x4 down-sampling for variance.
  909. variance4x4downsample[i2 + j] = 1;
  910. for (k = 0; k < 4; k++) {
  911. int x8_idx = x16_idx + ((k & 1) << 3);
  912. int y8_idx = y16_idx + ((k >> 1) << 3);
  913. v8x8 *vst2 = is_key_frame ? &vst->split[k] : &vt2[i2 + j].split[k];
  914. fill_variance_4x4avg(s, sp, d, dp, x8_idx, y8_idx, vst2,
  915. #if CONFIG_VP9_HIGHBITDEPTH
  916. xd->cur_buf->flags,
  917. #endif
  918. pixels_wide, pixels_high, is_key_frame);
  919. }
  920. }
  921. }
  922. }
  923. // Fill the rest of the variance tree by summing split partition values.
  924. avg_32x32 = 0;
  925. for (i = 0; i < 4; i++) {
  926. const int i2 = i << 2;
  927. for (j = 0; j < 4; j++) {
  928. if (variance4x4downsample[i2 + j] == 1) {
  929. v16x16 *vtemp = (!is_key_frame) ? &vt2[i2 + j] : &vt.split[i].split[j];
  930. for (m = 0; m < 4; m++) fill_variance_tree(&vtemp->split[m], BLOCK_8X8);
  931. fill_variance_tree(vtemp, BLOCK_16X16);
  932. // If variance of this 16x16 block is above the threshold, force block
  933. // to split. This also forces a split on the upper levels.
  934. get_variance(&vtemp->part_variances.none);
  935. if (vtemp->part_variances.none.variance > thresholds[2]) {
  936. force_split[5 + i2 + j] = 1;
  937. force_split[i + 1] = 1;
  938. force_split[0] = 1;
  939. }
  940. }
  941. }
  942. fill_variance_tree(&vt.split[i], BLOCK_32X32);
  943. // If variance of this 32x32 block is above the threshold, or if its above
  944. // (some threshold of) the average variance over the sub-16x16 blocks, then
  945. // force this block to split. This also forces a split on the upper
  946. // (64x64) level.
  947. if (!force_split[i + 1]) {
  948. get_variance(&vt.split[i].part_variances.none);
  949. if (vt.split[i].part_variances.none.variance > thresholds[1] ||
  950. (!is_key_frame &&
  951. vt.split[i].part_variances.none.variance > (thresholds[1] >> 1) &&
  952. vt.split[i].part_variances.none.variance > (avg_16x16[i] >> 1))) {
  953. force_split[i + 1] = 1;
  954. force_split[0] = 1;
  955. }
  956. avg_32x32 += vt.split[i].part_variances.none.variance;
  957. }
  958. }
  959. if (!force_split[0]) {
  960. fill_variance_tree(&vt, BLOCK_64X64);
  961. get_variance(&vt.part_variances.none);
  962. // If variance of this 64x64 block is above (some threshold of) the average
  963. // variance over the sub-32x32 blocks, then force this block to split.
  964. if (!is_key_frame && vt.part_variances.none.variance > (5 * avg_32x32) >> 4)
  965. force_split[0] = 1;
  966. }
  967. // Now go through the entire structure, splitting every block size until
  968. // we get to one that's got a variance lower than our threshold.
  969. if (mi_col + 8 > cm->mi_cols || mi_row + 8 > cm->mi_rows ||
  970. !set_vt_partitioning(cpi, x, xd, &vt, BLOCK_64X64, mi_row, mi_col,
  971. thresholds[0], BLOCK_16X16, force_split[0])) {
  972. for (i = 0; i < 4; ++i) {
  973. const int x32_idx = ((i & 1) << 2);
  974. const int y32_idx = ((i >> 1) << 2);
  975. const int i2 = i << 2;
  976. if (!set_vt_partitioning(cpi, x, xd, &vt.split[i], BLOCK_32X32,
  977. (mi_row + y32_idx), (mi_col + x32_idx),
  978. thresholds[1], BLOCK_16X16,
  979. force_split[i + 1])) {
  980. for (j = 0; j < 4; ++j) {
  981. const int x16_idx = ((j & 1) << 1);
  982. const int y16_idx = ((j >> 1) << 1);
  983. // For inter frames: if variance4x4downsample[] == 1 for this 16x16
  984. // block, then the variance is based on 4x4 down-sampling, so use vt2
  985. // in set_vt_partioning(), otherwise use vt.
  986. v16x16 *vtemp = (!is_key_frame && variance4x4downsample[i2 + j] == 1)
  987. ? &vt2[i2 + j]
  988. : &vt.split[i].split[j];
  989. if (!set_vt_partitioning(
  990. cpi, x, xd, vtemp, BLOCK_16X16, mi_row + y32_idx + y16_idx,
  991. mi_col + x32_idx + x16_idx, thresholds[2], cpi->vbp_bsize_min,
  992. force_split[5 + i2 + j])) {
  993. for (k = 0; k < 4; ++k) {
  994. const int x8_idx = (k & 1);
  995. const int y8_idx = (k >> 1);
  996. if (use_4x4_partition) {
  997. if (!set_vt_partitioning(cpi, x, xd, &vtemp->split[k],
  998. BLOCK_8X8,
  999. mi_row + y32_idx + y16_idx + y8_idx,
  1000. mi_col + x32_idx + x16_idx + x8_idx,
  1001. thresholds[3], BLOCK_8X8, 0)) {
  1002. set_block_size(
  1003. cpi, x, xd, (mi_row + y32_idx + y16_idx + y8_idx),
  1004. (mi_col + x32_idx + x16_idx + x8_idx), BLOCK_4X4);
  1005. }
  1006. } else {
  1007. set_block_size(
  1008. cpi, x, xd, (mi_row + y32_idx + y16_idx + y8_idx),
  1009. (mi_col + x32_idx + x16_idx + x8_idx), BLOCK_8X8);
  1010. }
  1011. }
  1012. }
  1013. }
  1014. }
  1015. }
  1016. }
  1017. if (cpi->sf.short_circuit_low_temp_var) {
  1018. set_low_temp_var_flag(cpi, x, xd, &vt, thresholds, ref_frame_partition,
  1019. mi_col, mi_row);
  1020. }
  1021. chroma_check(cpi, x, bsize, y_sad, is_key_frame);
  1022. return 0;
  1023. }
  1024. static void update_state(VP9_COMP *cpi, ThreadData *td, PICK_MODE_CONTEXT *ctx,
  1025. int mi_row, int mi_col, BLOCK_SIZE bsize,
  1026. int output_enabled) {
  1027. int i, x_idx, y;
  1028. VP9_COMMON *const cm = &cpi->common;
  1029. RD_COUNTS *const rdc = &td->rd_counts;
  1030. MACROBLOCK *const x = &td->mb;
  1031. MACROBLOCKD *const xd = &x->e_mbd;
  1032. struct macroblock_plane *const p = x->plane;
  1033. struct macroblockd_plane *const pd = xd->plane;
  1034. MODE_INFO *mi = &ctx->mic;
  1035. MODE_INFO *const xdmi = xd->mi[0];
  1036. MODE_INFO *mi_addr = xd->mi[0];
  1037. const struct segmentation *const seg = &cm->seg;
  1038. const int bw = num_8x8_blocks_wide_lookup[mi->sb_type];
  1039. const int bh = num_8x8_blocks_high_lookup[mi->sb_type];
  1040. const int x_mis = VPXMIN(bw, cm->mi_cols - mi_col);
  1041. const int y_mis = VPXMIN(bh, cm->mi_rows - mi_row);
  1042. MV_REF *const frame_mvs = cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
  1043. int w, h;
  1044. const int mis = cm->mi_stride;
  1045. const int mi_width = num_8x8_blocks_wide_lookup[bsize];
  1046. const int mi_height = num_8x8_blocks_high_lookup[bsize];
  1047. int max_plane;
  1048. assert(mi->sb_type == bsize);
  1049. *mi_addr = *mi;
  1050. *x->mbmi_ext = ctx->mbmi_ext;
  1051. // If segmentation in use
  1052. if (seg->enabled) {
  1053. // For in frame complexity AQ copy the segment id from the segment map.
  1054. if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
  1055. const uint8_t *const map =
  1056. seg->update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
  1057. mi_addr->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
  1058. }
  1059. // Else for cyclic refresh mode update the segment map, set the segment id
  1060. // and then update the quantizer.
  1061. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
  1062. vp9_cyclic_refresh_update_segment(cpi, xd->mi[0], mi_row, mi_col, bsize,
  1063. ctx->rate, ctx->dist, x->skip, p);
  1064. }
  1065. }
  1066. max_plane = is_inter_block(xdmi) ? MAX_MB_PLANE : 1;
  1067. for (i = 0; i < max_plane; ++i) {
  1068. p[i].coeff = ctx->coeff_pbuf[i][1];
  1069. p[i].qcoeff = ctx->qcoeff_pbuf[i][1];
  1070. pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
  1071. p[i].eobs = ctx->eobs_pbuf[i][1];
  1072. }
  1073. for (i = max_plane; i < MAX_MB_PLANE; ++i) {
  1074. p[i].coeff = ctx->coeff_pbuf[i][2];
  1075. p[i].qcoeff = ctx->qcoeff_pbuf[i][2];
  1076. pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][2];
  1077. p[i].eobs = ctx->eobs_pbuf[i][2];
  1078. }
  1079. // Restore the coding context of the MB to that that was in place
  1080. // when the mode was picked for it
  1081. for (y = 0; y < mi_height; y++)
  1082. for (x_idx = 0; x_idx < mi_width; x_idx++)
  1083. if ((xd->mb_to_right_edge >> (3 + MI_SIZE_LOG2)) + mi_width > x_idx &&
  1084. (xd->mb_to_bottom_edge >> (3 + MI_SIZE_LOG2)) + mi_height > y) {
  1085. xd->mi[x_idx + y * mis] = mi_addr;
  1086. }
  1087. if (cpi->oxcf.aq_mode != NO_AQ) vp9_init_plane_quantizers(cpi, x);
  1088. if (is_inter_block(xdmi) && xdmi->sb_type < BLOCK_8X8) {
  1089. xdmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
  1090. xdmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
  1091. }
  1092. x->skip = ctx->skip;
  1093. memcpy(x->zcoeff_blk[xdmi->tx_size], ctx->zcoeff_blk,
  1094. sizeof(ctx->zcoeff_blk[0]) * ctx->num_4x4_blk);
  1095. if (!output_enabled) return;
  1096. #if CONFIG_INTERNAL_STATS
  1097. if (frame_is_intra_only(cm)) {
  1098. static const int kf_mode_index[] = {
  1099. THR_DC /*DC_PRED*/, THR_V_PRED /*V_PRED*/,
  1100. THR_H_PRED /*H_PRED*/, THR_D45_PRED /*D45_PRED*/,
  1101. THR_D135_PRED /*D135_PRED*/, THR_D117_PRED /*D117_PRED*/,
  1102. THR_D153_PRED /*D153_PRED*/, THR_D207_PRED /*D207_PRED*/,
  1103. THR_D63_PRED /*D63_PRED*/, THR_TM /*TM_PRED*/,
  1104. };
  1105. ++cpi->mode_chosen_counts[kf_mode_index[xdmi->mode]];
  1106. } else {
  1107. // Note how often each mode chosen as best
  1108. ++cpi->mode_chosen_counts[ctx->best_mode_index];
  1109. }
  1110. #endif
  1111. if (!frame_is_intra_only(cm)) {
  1112. if (is_inter_block(xdmi)) {
  1113. vp9_update_mv_count(td);
  1114. if (cm->interp_filter == SWITCHABLE) {
  1115. const int ctx = get_pred_context_switchable_interp(xd);
  1116. ++td->counts->switchable_interp[ctx][xdmi->interp_filter];
  1117. }
  1118. }
  1119. rdc->comp_pred_diff[SINGLE_REFERENCE] += ctx->single_pred_diff;
  1120. rdc->comp_pred_diff[COMPOUND_REFERENCE] += ctx->comp_pred_diff;
  1121. rdc->comp_pred_diff[REFERENCE_MODE_SELECT] += ctx->hybrid_pred_diff;
  1122. for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
  1123. rdc->filter_diff[i] += ctx->best_filter_diff[i];
  1124. }
  1125. for (h = 0; h < y_mis; ++h) {
  1126. MV_REF *const frame_mv = frame_mvs + h * cm->mi_cols;
  1127. for (w = 0; w < x_mis; ++w) {
  1128. MV_REF *const mv = frame_mv + w;
  1129. mv->ref_frame[0] = mi->ref_frame[0];
  1130. mv->ref_frame[1] = mi->ref_frame[1];
  1131. mv->mv[0].as_int = mi->mv[0].as_int;
  1132. mv->mv[1].as_int = mi->mv[1].as_int;
  1133. }
  1134. }
  1135. }
  1136. void vp9_setup_src_planes(MACROBLOCK *x, const YV12_BUFFER_CONFIG *src,
  1137. int mi_row, int mi_col) {
  1138. uint8_t *const buffers[3] = { src->y_buffer, src->u_buffer, src->v_buffer };
  1139. const int strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
  1140. int i;
  1141. // Set current frame pointer.
  1142. x->e_mbd.cur_buf = src;
  1143. for (i = 0; i < MAX_MB_PLANE; i++)
  1144. setup_pred_plane(&x->plane[i].src, buffers[i], strides[i], mi_row, mi_col,
  1145. NULL, x->e_mbd.plane[i].subsampling_x,
  1146. x->e_mbd.plane[i].subsampling_y);
  1147. }
  1148. static void set_mode_info_seg_skip(MACROBLOCK *x, TX_MODE tx_mode,
  1149. RD_COST *rd_cost, BLOCK_SIZE bsize) {
  1150. MACROBLOCKD *const xd = &x->e_mbd;
  1151. MODE_INFO *const mi = xd->mi[0];
  1152. INTERP_FILTER filter_ref;
  1153. filter_ref = get_pred_context_switchable_interp(xd);
  1154. if (filter_ref == SWITCHABLE_FILTERS) filter_ref = EIGHTTAP;
  1155. mi->sb_type = bsize;
  1156. mi->mode = ZEROMV;
  1157. mi->tx_size =
  1158. VPXMIN(max_txsize_lookup[bsize], tx_mode_to_biggest_tx_size[tx_mode]);
  1159. mi->skip = 1;
  1160. mi->uv_mode = DC_PRED;
  1161. mi->ref_frame[0] = LAST_FRAME;
  1162. mi->ref_frame[1] = NONE;
  1163. mi->mv[0].as_int = 0;
  1164. mi->interp_filter = filter_ref;
  1165. xd->mi[0]->bmi[0].as_mv[0].as_int = 0;
  1166. x->skip = 1;
  1167. vp9_rd_cost_init(rd_cost);
  1168. }
  1169. static int set_segment_rdmult(VP9_COMP *const cpi, MACROBLOCK *const x,
  1170. int8_t segment_id) {
  1171. int segment_qindex;
  1172. VP9_COMMON *const cm = &cpi->common;
  1173. vp9_init_plane_quantizers(cpi, x);
  1174. vpx_clear_system_state();
  1175. segment_qindex = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex);
  1176. return vp9_compute_rd_mult(cpi, segment_qindex + cm->y_dc_delta_q);
  1177. }
  1178. static void rd_pick_sb_modes(VP9_COMP *cpi, TileDataEnc *tile_data,
  1179. MACROBLOCK *const x, int mi_row, int mi_col,
  1180. RD_COST *rd_cost, BLOCK_SIZE bsize,
  1181. PICK_MODE_CONTEXT *ctx, int64_t best_rd) {
  1182. VP9_COMMON *const cm = &cpi->common;
  1183. TileInfo *const tile_info = &tile_data->tile_info;
  1184. MACROBLOCKD *const xd = &x->e_mbd;
  1185. MODE_INFO *mi;
  1186. struct macroblock_plane *const p = x->plane;
  1187. struct macroblockd_plane *const pd = xd->plane;
  1188. const AQ_MODE aq_mode = cpi->oxcf.aq_mode;
  1189. int i, orig_rdmult;
  1190. vpx_clear_system_state();
  1191. // Use the lower precision, but faster, 32x32 fdct for mode selection.
  1192. x->use_lp32x32fdct = 1;
  1193. set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
  1194. mi = xd->mi[0];
  1195. mi->sb_type = bsize;
  1196. for (i = 0; i < MAX_MB_PLANE; ++i) {
  1197. p[i].coeff = ctx->coeff_pbuf[i][0];
  1198. p[i].qcoeff = ctx->qcoeff_pbuf[i][0];
  1199. pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][0];
  1200. p[i].eobs = ctx->eobs_pbuf[i][0];
  1201. }
  1202. ctx->is_coded = 0;
  1203. ctx->skippable = 0;
  1204. ctx->pred_pixel_ready = 0;
  1205. x->skip_recode = 0;
  1206. // Set to zero to make sure we do not use the previous encoded frame stats
  1207. mi->skip = 0;
  1208. #if CONFIG_VP9_HIGHBITDEPTH
  1209. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  1210. x->source_variance = vp9_high_get_sby_perpixel_variance(
  1211. cpi, &x->plane[0].src, bsize, xd->bd);
  1212. } else {
  1213. x->source_variance =
  1214. vp9_get_sby_perpixel_variance(cpi, &x->plane[0].src, bsize);
  1215. }
  1216. #else
  1217. x->source_variance =
  1218. vp9_get_sby_perpixel_variance(cpi, &x->plane[0].src, bsize);
  1219. #endif // CONFIG_VP9_HIGHBITDEPTH
  1220. // Save rdmult before it might be changed, so it can be restored later.
  1221. orig_rdmult = x->rdmult;
  1222. if ((cpi->sf.tx_domain_thresh > 0.0) || (cpi->sf.quant_opt_thresh > 0.0)) {
  1223. double logvar = vp9_log_block_var(cpi, x, bsize);
  1224. // Check block complexity as part of descision on using pixel or transform
  1225. // domain distortion in rd tests.
  1226. x->block_tx_domain = cpi->sf.allow_txfm_domain_distortion &&
  1227. (logvar >= cpi->sf.tx_domain_thresh);
  1228. // Check block complexity as part of descision on using quantized
  1229. // coefficient optimisation inside the rd loop.
  1230. x->block_qcoeff_opt =
  1231. cpi->sf.allow_quant_coeff_opt && (logvar <= cpi->sf.quant_opt_thresh);
  1232. } else {
  1233. x->block_tx_domain = cpi->sf.allow_txfm_domain_distortion;
  1234. x->block_qcoeff_opt = cpi->sf.allow_quant_coeff_opt;
  1235. }
  1236. if (aq_mode == VARIANCE_AQ) {
  1237. const int energy =
  1238. bsize <= BLOCK_16X16 ? x->mb_energy : vp9_block_energy(cpi, x, bsize);
  1239. if (cm->frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame ||
  1240. cpi->force_update_segmentation ||
  1241. (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref)) {
  1242. mi->segment_id = vp9_vaq_segment_id(energy);
  1243. } else {
  1244. const uint8_t *const map =
  1245. cm->seg.update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
  1246. mi->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
  1247. }
  1248. x->rdmult = set_segment_rdmult(cpi, x, mi->segment_id);
  1249. } else if (aq_mode == LOOKAHEAD_AQ) {
  1250. const uint8_t *const map = cpi->segmentation_map;
  1251. // I do not change rdmult here consciously.
  1252. mi->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
  1253. } else if (aq_mode == EQUATOR360_AQ) {
  1254. if (cm->frame_type == KEY_FRAME || cpi->force_update_segmentation) {
  1255. mi->segment_id = vp9_360aq_segment_id(mi_row, cm->mi_rows);
  1256. } else {
  1257. const uint8_t *const map =
  1258. cm->seg.update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
  1259. mi->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
  1260. }
  1261. x->rdmult = set_segment_rdmult(cpi, x, mi->segment_id);
  1262. } else if (aq_mode == COMPLEXITY_AQ) {
  1263. x->rdmult = set_segment_rdmult(cpi, x, mi->segment_id);
  1264. } else if (aq_mode == CYCLIC_REFRESH_AQ) {
  1265. const uint8_t *const map =
  1266. cm->seg.update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
  1267. // If segment is boosted, use rdmult for that segment.
  1268. if (cyclic_refresh_segment_id_boosted(
  1269. get_segment_id(cm, map, bsize, mi_row, mi_col)))
  1270. x->rdmult = vp9_cyclic_refresh_get_rdmult(cpi->cyclic_refresh);
  1271. }
  1272. // Find best coding mode & reconstruct the MB so it is available
  1273. // as a predictor for MBs that follow in the SB
  1274. if (frame_is_intra_only(cm)) {
  1275. vp9_rd_pick_intra_mode_sb(cpi, x, rd_cost, bsize, ctx, best_rd);
  1276. } else {
  1277. if (bsize >= BLOCK_8X8) {
  1278. if (segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_SKIP))
  1279. vp9_rd_pick_inter_mode_sb_seg_skip(cpi, tile_data, x, rd_cost, bsize,
  1280. ctx, best_rd);
  1281. else
  1282. vp9_rd_pick_inter_mode_sb(cpi, tile_data, x, mi_row, mi_col, rd_cost,
  1283. bsize, ctx, best_rd);
  1284. } else {
  1285. vp9_rd_pick_inter_mode_sub8x8(cpi, tile_data, x, mi_row, mi_col, rd_cost,
  1286. bsize, ctx, best_rd);
  1287. }
  1288. }
  1289. // Examine the resulting rate and for AQ mode 2 make a segment choice.
  1290. if ((rd_cost->rate != INT_MAX) && (aq_mode == COMPLEXITY_AQ) &&
  1291. (bsize >= BLOCK_16X16) &&
  1292. (cm->frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame ||
  1293. (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref))) {
  1294. vp9_caq_select_segment(cpi, x, bsize, mi_row, mi_col, rd_cost->rate);
  1295. }
  1296. x->rdmult = orig_rdmult;
  1297. // TODO(jingning) The rate-distortion optimization flow needs to be
  1298. // refactored to provide proper exit/return handle.
  1299. if (rd_cost->rate == INT_MAX) rd_cost->rdcost = INT64_MAX;
  1300. ctx->rate = rd_cost->rate;
  1301. ctx->dist = rd_cost->dist;
  1302. }
  1303. static void update_stats(VP9_COMMON *cm, ThreadData *td) {
  1304. const MACROBLOCK *x = &td->mb;
  1305. const MACROBLOCKD *const xd = &x->e_mbd;
  1306. const MODE_INFO *const mi = xd->mi[0];
  1307. const MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
  1308. const BLOCK_SIZE bsize = mi->sb_type;
  1309. if (!frame_is_intra_only(cm)) {
  1310. FRAME_COUNTS *const counts = td->counts;
  1311. const int inter_block = is_inter_block(mi);
  1312. const int seg_ref_active =
  1313. segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_REF_FRAME);
  1314. if (!seg_ref_active) {
  1315. counts->intra_inter[get_intra_inter_context(xd)][inter_block]++;
  1316. // If the segment reference feature is enabled we have only a single
  1317. // reference frame allowed for the segment so exclude it from
  1318. // the reference frame counts used to work out probabilities.
  1319. if (inter_block) {
  1320. const MV_REFERENCE_FRAME ref0 = mi->ref_frame[0];
  1321. if (cm->reference_mode == REFERENCE_MODE_SELECT)
  1322. counts->comp_inter[vp9_get_reference_mode_context(cm, xd)]
  1323. [has_second_ref(mi)]++;
  1324. if (has_second_ref(mi)) {
  1325. counts->comp_ref[vp9_get_pred_context_comp_ref_p(cm, xd)]
  1326. [ref0 == GOLDEN_FRAME]++;
  1327. } else {
  1328. counts->single_ref[vp9_get_pred_context_single_ref_p1(xd)][0]
  1329. [ref0 != LAST_FRAME]++;
  1330. if (ref0 != LAST_FRAME)
  1331. counts->single_ref[vp9_get_pred_context_single_ref_p2(xd)][1]
  1332. [ref0 != GOLDEN_FRAME]++;
  1333. }
  1334. }
  1335. }
  1336. if (inter_block &&
  1337. !segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_SKIP)) {
  1338. const int mode_ctx = mbmi_ext->mode_context[mi->ref_frame[0]];
  1339. if (bsize >= BLOCK_8X8) {
  1340. const PREDICTION_MODE mode = mi->mode;
  1341. ++counts->inter_mode[mode_ctx][INTER_OFFSET(mode)];
  1342. } else {
  1343. const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
  1344. const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
  1345. int idx, idy;
  1346. for (idy = 0; idy < 2; idy += num_4x4_h) {
  1347. for (idx = 0; idx < 2; idx += num_4x4_w) {
  1348. const int j = idy * 2 + idx;
  1349. const PREDICTION_MODE b_mode = mi->bmi[j].as_mode;
  1350. ++counts->inter_mode[mode_ctx][INTER_OFFSET(b_mode)];
  1351. }
  1352. }
  1353. }
  1354. }
  1355. }
  1356. }
  1357. static void restore_context(MACROBLOCK *const x, int mi_row, int mi_col,
  1358. ENTROPY_CONTEXT a[16 * MAX_MB_PLANE],
  1359. ENTROPY_CONTEXT l[16 * MAX_MB_PLANE],
  1360. PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8],
  1361. BLOCK_SIZE bsize) {
  1362. MACROBLOCKD *const xd = &x->e_mbd;
  1363. int p;
  1364. const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
  1365. const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
  1366. int mi_width = num_8x8_blocks_wide_lookup[bsize];
  1367. int mi_height = num_8x8_blocks_high_lookup[bsize];
  1368. for (p = 0; p < MAX_MB_PLANE; p++) {
  1369. memcpy(xd->above_context[p] + ((mi_col * 2) >> xd->plane[p].subsampling_x),
  1370. a + num_4x4_blocks_wide * p,
  1371. (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
  1372. xd->plane[p].subsampling_x);
  1373. memcpy(xd->left_context[p] +
  1374. ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y),
  1375. l + num_4x4_blocks_high * p,
  1376. (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
  1377. xd->plane[p].subsampling_y);
  1378. }
  1379. memcpy(xd->above_seg_context + mi_col, sa,
  1380. sizeof(*xd->above_seg_context) * mi_width);
  1381. memcpy(xd->left_seg_context + (mi_row & MI_MASK), sl,
  1382. sizeof(xd->left_seg_context[0]) * mi_height);
  1383. }
  1384. static void save_context(MACROBLOCK *const x, int mi_row, int mi_col,
  1385. ENTROPY_CONTEXT a[16 * MAX_MB_PLANE],
  1386. ENTROPY_CONTEXT l[16 * MAX_MB_PLANE],
  1387. PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8],
  1388. BLOCK_SIZE bsize) {
  1389. const MACROBLOCKD *const xd = &x->e_mbd;
  1390. int p;
  1391. const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
  1392. const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
  1393. int mi_width = num_8x8_blocks_wide_lookup[bsize];
  1394. int mi_height = num_8x8_blocks_high_lookup[bsize];
  1395. // buffer the above/left context information of the block in search.
  1396. for (p = 0; p < MAX_MB_PLANE; ++p) {
  1397. memcpy(a + num_4x4_blocks_wide * p,
  1398. xd->above_context[p] + (mi_col * 2 >> xd->plane[p].subsampling_x),
  1399. (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
  1400. xd->plane[p].subsampling_x);
  1401. memcpy(l + num_4x4_blocks_high * p,
  1402. xd->left_context[p] +
  1403. ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y),
  1404. (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
  1405. xd->plane[p].subsampling_y);
  1406. }
  1407. memcpy(sa, xd->above_seg_context + mi_col,
  1408. sizeof(*xd->above_seg_context) * mi_width);
  1409. memcpy(sl, xd->left_seg_context + (mi_row & MI_MASK),
  1410. sizeof(xd->left_seg_context[0]) * mi_height);
  1411. }
  1412. static void encode_b(VP9_COMP *cpi, const TileInfo *const tile, ThreadData *td,
  1413. TOKENEXTRA **tp, int mi_row, int mi_col,
  1414. int output_enabled, BLOCK_SIZE bsize,
  1415. PICK_MODE_CONTEXT *ctx) {
  1416. MACROBLOCK *const x = &td->mb;
  1417. set_offsets(cpi, tile, x, mi_row, mi_col, bsize);
  1418. update_state(cpi, td, ctx, mi_row, mi_col, bsize, output_enabled);
  1419. encode_superblock(cpi, td, tp, output_enabled, mi_row, mi_col, bsize, ctx);
  1420. if (output_enabled) {
  1421. update_stats(&cpi->common, td);
  1422. (*tp)->token = EOSB_TOKEN;
  1423. (*tp)++;
  1424. }
  1425. }
  1426. static void encode_sb(VP9_COMP *cpi, ThreadData *td, const TileInfo *const tile,
  1427. TOKENEXTRA **tp, int mi_row, int mi_col,
  1428. int output_enabled, BLOCK_SIZE bsize, PC_TREE *pc_tree) {
  1429. VP9_COMMON *const cm = &cpi->common;
  1430. MACROBLOCK *const x = &td->mb;
  1431. MACROBLOCKD *const xd = &x->e_mbd;
  1432. const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
  1433. int ctx;
  1434. PARTITION_TYPE partition;
  1435. BLOCK_SIZE subsize = bsize;
  1436. if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
  1437. if (bsize >= BLOCK_8X8) {
  1438. ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
  1439. subsize = get_subsize(bsize, pc_tree->partitioning);
  1440. } else {
  1441. ctx = 0;
  1442. subsize = BLOCK_4X4;
  1443. }
  1444. partition = partition_lookup[bsl][subsize];
  1445. if (output_enabled && bsize != BLOCK_4X4)
  1446. td->counts->partition[ctx][partition]++;
  1447. switch (partition) {
  1448. case PARTITION_NONE:
  1449. encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
  1450. &pc_tree->none);
  1451. break;
  1452. case PARTITION_VERT:
  1453. encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
  1454. &pc_tree->vertical[0]);
  1455. if (mi_col + hbs < cm->mi_cols && bsize > BLOCK_8X8) {
  1456. encode_b(cpi, tile, td, tp, mi_row, mi_col + hbs, output_enabled,
  1457. subsize, &pc_tree->vertical[1]);
  1458. }
  1459. break;
  1460. case PARTITION_HORZ:
  1461. encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
  1462. &pc_tree->horizontal[0]);
  1463. if (mi_row + hbs < cm->mi_rows && bsize > BLOCK_8X8) {
  1464. encode_b(cpi, tile, td, tp, mi_row + hbs, mi_col, output_enabled,
  1465. subsize, &pc_tree->horizontal[1]);
  1466. }
  1467. break;
  1468. case PARTITION_SPLIT:
  1469. if (bsize == BLOCK_8X8) {
  1470. encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
  1471. pc_tree->leaf_split[0]);
  1472. } else {
  1473. encode_sb(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
  1474. pc_tree->split[0]);
  1475. encode_sb(cpi, td, tile, tp, mi_row, mi_col + hbs, output_enabled,
  1476. subsize, pc_tree->split[1]);
  1477. encode_sb(cpi, td, tile, tp, mi_row + hbs, mi_col, output_enabled,
  1478. subsize, pc_tree->split[2]);
  1479. encode_sb(cpi, td, tile, tp, mi_row + hbs, mi_col + hbs, output_enabled,
  1480. subsize, pc_tree->split[3]);
  1481. }
  1482. break;
  1483. default: assert(0 && "Invalid partition type."); break;
  1484. }
  1485. if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
  1486. update_partition_context(xd, mi_row, mi_col, subsize, bsize);
  1487. }
  1488. // Check to see if the given partition size is allowed for a specified number
  1489. // of 8x8 block rows and columns remaining in the image.
  1490. // If not then return the largest allowed partition size
  1491. static BLOCK_SIZE find_partition_size(BLOCK_SIZE bsize, int rows_left,
  1492. int cols_left, int *bh, int *bw) {
  1493. if (rows_left <= 0 || cols_left <= 0) {
  1494. return VPXMIN(bsize, BLOCK_8X8);
  1495. } else {
  1496. for (; bsize > 0; bsize -= 3) {
  1497. *bh = num_8x8_blocks_high_lookup[bsize];
  1498. *bw = num_8x8_blocks_wide_lookup[bsize];
  1499. if ((*bh <= rows_left) && (*bw <= cols_left)) {
  1500. break;
  1501. }
  1502. }
  1503. }
  1504. return bsize;
  1505. }
  1506. static void set_partial_b64x64_partition(MODE_INFO *mi, int mis, int bh_in,
  1507. int bw_in, int row8x8_remaining,
  1508. int col8x8_remaining, BLOCK_SIZE bsize,
  1509. MODE_INFO **mi_8x8) {
  1510. int bh = bh_in;
  1511. int r, c;
  1512. for (r = 0; r < MI_BLOCK_SIZE; r += bh) {
  1513. int bw = bw_in;
  1514. for (c = 0; c < MI_BLOCK_SIZE; c += bw) {
  1515. const int index = r * mis + c;
  1516. mi_8x8[index] = mi + index;
  1517. mi_8x8[index]->sb_type = find_partition_size(
  1518. bsize, row8x8_remaining - r, col8x8_remaining - c, &bh, &bw);
  1519. }
  1520. }
  1521. }
  1522. // This function attempts to set all mode info entries in a given SB64
  1523. // to the same block partition size.
  1524. // However, at the bottom and right borders of the image the requested size
  1525. // may not be allowed in which case this code attempts to choose the largest
  1526. // allowable partition.
  1527. static void set_fixed_partitioning(VP9_COMP *cpi, const TileInfo *const tile,
  1528. MODE_INFO **mi_8x8, int mi_row, int mi_col,
  1529. BLOCK_SIZE bsize) {
  1530. VP9_COMMON *const cm = &cpi->common;
  1531. const int mis = cm->mi_stride;
  1532. const int row8x8_remaining = tile->mi_row_end - mi_row;
  1533. const int col8x8_remaining = tile->mi_col_end - mi_col;
  1534. int block_row, block_col;
  1535. MODE_INFO *mi_upper_left = cm->mi + mi_row * mis + mi_col;
  1536. int bh = num_8x8_blocks_high_lookup[bsize];
  1537. int bw = num_8x8_blocks_wide_lookup[bsize];
  1538. assert((row8x8_remaining > 0) && (col8x8_remaining > 0));
  1539. // Apply the requested partition size to the SB64 if it is all "in image"
  1540. if ((col8x8_remaining >= MI_BLOCK_SIZE) &&
  1541. (row8x8_remaining >= MI_BLOCK_SIZE)) {
  1542. for (block_row = 0; block_row < MI_BLOCK_SIZE; block_row += bh) {
  1543. for (block_col = 0; block_col < MI_BLOCK_SIZE; block_col += bw) {
  1544. int index = block_row * mis + block_col;
  1545. mi_8x8[index] = mi_upper_left + index;
  1546. mi_8x8[index]->sb_type = bsize;
  1547. }
  1548. }
  1549. } else {
  1550. // Else this is a partial SB64.
  1551. set_partial_b64x64_partition(mi_upper_left, mis, bh, bw, row8x8_remaining,
  1552. col8x8_remaining, bsize, mi_8x8);
  1553. }
  1554. }
  1555. static const struct {
  1556. int row;
  1557. int col;
  1558. } coord_lookup[16] = {
  1559. // 32x32 index = 0
  1560. { 0, 0 },
  1561. { 0, 2 },
  1562. { 2, 0 },
  1563. { 2, 2 },
  1564. // 32x32 index = 1
  1565. { 0, 4 },
  1566. { 0, 6 },
  1567. { 2, 4 },
  1568. { 2, 6 },
  1569. // 32x32 index = 2
  1570. { 4, 0 },
  1571. { 4, 2 },
  1572. { 6, 0 },
  1573. { 6, 2 },
  1574. // 32x32 index = 3
  1575. { 4, 4 },
  1576. { 4, 6 },
  1577. { 6, 4 },
  1578. { 6, 6 },
  1579. };
  1580. static void set_source_var_based_partition(VP9_COMP *cpi,
  1581. const TileInfo *const tile,
  1582. MACROBLOCK *const x,
  1583. MODE_INFO **mi_8x8, int mi_row,
  1584. int mi_col) {
  1585. VP9_COMMON *const cm = &cpi->common;
  1586. const int mis = cm->mi_stride;
  1587. const int row8x8_remaining = tile->mi_row_end - mi_row;
  1588. const int col8x8_remaining = tile->mi_col_end - mi_col;
  1589. MODE_INFO *mi_upper_left = cm->mi + mi_row * mis + mi_col;
  1590. vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col);
  1591. assert((row8x8_remaining > 0) && (col8x8_remaining > 0));
  1592. // In-image SB64
  1593. if ((col8x8_remaining >= MI_BLOCK_SIZE) &&
  1594. (row8x8_remaining >= MI_BLOCK_SIZE)) {
  1595. int i, j;
  1596. int index;
  1597. diff d32[4];
  1598. const int offset = (mi_row >> 1) * cm->mb_cols + (mi_col >> 1);
  1599. int is_larger_better = 0;
  1600. int use32x32 = 0;
  1601. unsigned int thr = cpi->source_var_thresh;
  1602. memset(d32, 0, 4 * sizeof(diff));
  1603. for (i = 0; i < 4; i++) {
  1604. diff *d16[4];
  1605. for (j = 0; j < 4; j++) {
  1606. int b_mi_row = coord_lookup[i * 4 + j].row;
  1607. int b_mi_col = coord_lookup[i * 4 + j].col;
  1608. int boffset = b_mi_row / 2 * cm->mb_cols + b_mi_col / 2;
  1609. d16[j] = cpi->source_diff_var + offset + boffset;
  1610. index = b_mi_row * mis + b_mi_col;
  1611. mi_8x8[index] = mi_upper_left + index;
  1612. mi_8x8[index]->sb_type = BLOCK_16X16;
  1613. // TODO(yunqingwang): If d16[j].var is very large, use 8x8 partition
  1614. // size to further improve quality.
  1615. }
  1616. is_larger_better = (d16[0]->var < thr) && (d16[1]->var < thr) &&
  1617. (d16[2]->var < thr) && (d16[3]->var < thr);
  1618. // Use 32x32 partition
  1619. if (is_larger_better) {
  1620. use32x32 += 1;
  1621. for (j = 0; j < 4; j++) {
  1622. d32[i].sse += d16[j]->sse;
  1623. d32[i].sum += d16[j]->sum;
  1624. }
  1625. d32[i].var =
  1626. (unsigned int)(d32[i].sse -
  1627. (unsigned int)(((int64_t)d32[i].sum * d32[i].sum) >>
  1628. 10));
  1629. index = coord_lookup[i * 4].row * mis + coord_lookup[i * 4].col;
  1630. mi_8x8[index] = mi_upper_left + index;
  1631. mi_8x8[index]->sb_type = BLOCK_32X32;
  1632. }
  1633. }
  1634. if (use32x32 == 4) {
  1635. thr <<= 1;
  1636. is_larger_better = (d32[0].var < thr) && (d32[1].var < thr) &&
  1637. (d32[2].var < thr) && (d32[3].var < thr);
  1638. // Use 64x64 partition
  1639. if (is_larger_better) {
  1640. mi_8x8[0] = mi_upper_left;
  1641. mi_8x8[0]->sb_type = BLOCK_64X64;
  1642. }
  1643. }
  1644. } else { // partial in-image SB64
  1645. int bh = num_8x8_blocks_high_lookup[BLOCK_16X16];
  1646. int bw = num_8x8_blocks_wide_lookup[BLOCK_16X16];
  1647. set_partial_b64x64_partition(mi_upper_left, mis, bh, bw, row8x8_remaining,
  1648. col8x8_remaining, BLOCK_16X16, mi_8x8);
  1649. }
  1650. }
  1651. static void update_state_rt(VP9_COMP *cpi, ThreadData *td,
  1652. PICK_MODE_CONTEXT *ctx, int mi_row, int mi_col,
  1653. int bsize) {
  1654. VP9_COMMON *const cm = &cpi->common;
  1655. MACROBLOCK *const x = &td->mb;
  1656. MACROBLOCKD *const xd = &x->e_mbd;
  1657. MODE_INFO *const mi = xd->mi[0];
  1658. struct macroblock_plane *const p = x->plane;
  1659. const struct segmentation *const seg = &cm->seg;
  1660. const int bw = num_8x8_blocks_wide_lookup[mi->sb_type];
  1661. const int bh = num_8x8_blocks_high_lookup[mi->sb_type];
  1662. const int x_mis = VPXMIN(bw, cm->mi_cols - mi_col);
  1663. const int y_mis = VPXMIN(bh, cm->mi_rows - mi_row);
  1664. *(xd->mi[0]) = ctx->mic;
  1665. *(x->mbmi_ext) = ctx->mbmi_ext;
  1666. if (seg->enabled && cpi->oxcf.aq_mode != NO_AQ) {
  1667. // For in frame complexity AQ or variance AQ, copy segment_id from
  1668. // segmentation_map.
  1669. if (cpi->oxcf.aq_mode != CYCLIC_REFRESH_AQ) {
  1670. const uint8_t *const map =
  1671. seg->update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
  1672. mi->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
  1673. } else {
  1674. // Setting segmentation map for cyclic_refresh.
  1675. vp9_cyclic_refresh_update_segment(cpi, mi, mi_row, mi_col, bsize,
  1676. ctx->rate, ctx->dist, x->skip, p);
  1677. }
  1678. vp9_init_plane_quantizers(cpi, x);
  1679. }
  1680. if (is_inter_block(mi)) {
  1681. vp9_update_mv_count(td);
  1682. if (cm->interp_filter == SWITCHABLE) {
  1683. const int pred_ctx = get_pred_context_switchable_interp(xd);
  1684. ++td->counts->switchable_interp[pred_ctx][mi->interp_filter];
  1685. }
  1686. if (mi->sb_type < BLOCK_8X8) {
  1687. mi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
  1688. mi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
  1689. }
  1690. }
  1691. if (cm->use_prev_frame_mvs ||
  1692. (cpi->svc.use_base_mv && cpi->svc.number_spatial_layers > 1 &&
  1693. cpi->svc.spatial_layer_id != cpi->svc.number_spatial_layers - 1)) {
  1694. MV_REF *const frame_mvs =
  1695. cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
  1696. int w, h;
  1697. for (h = 0; h < y_mis; ++h) {
  1698. MV_REF *const frame_mv = frame_mvs + h * cm->mi_cols;
  1699. for (w = 0; w < x_mis; ++w) {
  1700. MV_REF *const mv = frame_mv + w;
  1701. mv->ref_frame[0] = mi->ref_frame[0];
  1702. mv->ref_frame[1] = mi->ref_frame[1];
  1703. mv->mv[0].as_int = mi->mv[0].as_int;
  1704. mv->mv[1].as_int = mi->mv[1].as_int;
  1705. }
  1706. }
  1707. }
  1708. x->skip = ctx->skip;
  1709. x->skip_txfm[0] = mi->segment_id ? 0 : ctx->skip_txfm[0];
  1710. }
  1711. static void encode_b_rt(VP9_COMP *cpi, ThreadData *td,
  1712. const TileInfo *const tile, TOKENEXTRA **tp, int mi_row,
  1713. int mi_col, int output_enabled, BLOCK_SIZE bsize,
  1714. PICK_MODE_CONTEXT *ctx) {
  1715. MACROBLOCK *const x = &td->mb;
  1716. set_offsets(cpi, tile, x, mi_row, mi_col, bsize);
  1717. update_state_rt(cpi, td, ctx, mi_row, mi_col, bsize);
  1718. encode_superblock(cpi, td, tp, output_enabled, mi_row, mi_col, bsize, ctx);
  1719. update_stats(&cpi->common, td);
  1720. (*tp)->token = EOSB_TOKEN;
  1721. (*tp)++;
  1722. }
  1723. static void encode_sb_rt(VP9_COMP *cpi, ThreadData *td,
  1724. const TileInfo *const tile, TOKENEXTRA **tp,
  1725. int mi_row, int mi_col, int output_enabled,
  1726. BLOCK_SIZE bsize, PC_TREE *pc_tree) {
  1727. VP9_COMMON *const cm = &cpi->common;
  1728. MACROBLOCK *const x = &td->mb;
  1729. MACROBLOCKD *const xd = &x->e_mbd;
  1730. const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
  1731. int ctx;
  1732. PARTITION_TYPE partition;
  1733. BLOCK_SIZE subsize;
  1734. if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
  1735. if (bsize >= BLOCK_8X8) {
  1736. const int idx_str = xd->mi_stride * mi_row + mi_col;
  1737. MODE_INFO **mi_8x8 = cm->mi_grid_visible + idx_str;
  1738. ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
  1739. subsize = mi_8x8[0]->sb_type;
  1740. } else {
  1741. ctx = 0;
  1742. subsize = BLOCK_4X4;
  1743. }
  1744. partition = partition_lookup[bsl][subsize];
  1745. if (output_enabled && bsize != BLOCK_4X4)
  1746. td->counts->partition[ctx][partition]++;
  1747. switch (partition) {
  1748. case PARTITION_NONE:
  1749. encode_b_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
  1750. &pc_tree->none);
  1751. break;
  1752. case PARTITION_VERT:
  1753. encode_b_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
  1754. &pc_tree->vertical[0]);
  1755. if (mi_col + hbs < cm->mi_cols && bsize > BLOCK_8X8) {
  1756. encode_b_rt(cpi, td, tile, tp, mi_row, mi_col + hbs, output_enabled,
  1757. subsize, &pc_tree->vertical[1]);
  1758. }
  1759. break;
  1760. case PARTITION_HORZ:
  1761. encode_b_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
  1762. &pc_tree->horizontal[0]);
  1763. if (mi_row + hbs < cm->mi_rows && bsize > BLOCK_8X8) {
  1764. encode_b_rt(cpi, td, tile, tp, mi_row + hbs, mi_col, output_enabled,
  1765. subsize, &pc_tree->horizontal[1]);
  1766. }
  1767. break;
  1768. case PARTITION_SPLIT:
  1769. subsize = get_subsize(bsize, PARTITION_SPLIT);
  1770. encode_sb_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
  1771. pc_tree->split[0]);
  1772. encode_sb_rt(cpi, td, tile, tp, mi_row, mi_col + hbs, output_enabled,
  1773. subsize, pc_tree->split[1]);
  1774. encode_sb_rt(cpi, td, tile, tp, mi_row + hbs, mi_col, output_enabled,
  1775. subsize, pc_tree->split[2]);
  1776. encode_sb_rt(cpi, td, tile, tp, mi_row + hbs, mi_col + hbs,
  1777. output_enabled, subsize, pc_tree->split[3]);
  1778. break;
  1779. default: assert(0 && "Invalid partition type."); break;
  1780. }
  1781. if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
  1782. update_partition_context(xd, mi_row, mi_col, subsize, bsize);
  1783. }
  1784. static void rd_use_partition(VP9_COMP *cpi, ThreadData *td,
  1785. TileDataEnc *tile_data, MODE_INFO **mi_8x8,
  1786. TOKENEXTRA **tp, int mi_row, int mi_col,
  1787. BLOCK_SIZE bsize, int *rate, int64_t *dist,
  1788. int do_recon, PC_TREE *pc_tree) {
  1789. VP9_COMMON *const cm = &cpi->common;
  1790. TileInfo *const tile_info = &tile_data->tile_info;
  1791. MACROBLOCK *const x = &td->mb;
  1792. MACROBLOCKD *const xd = &x->e_mbd;
  1793. const int mis = cm->mi_stride;
  1794. const int bsl = b_width_log2_lookup[bsize];
  1795. const int mi_step = num_4x4_blocks_wide_lookup[bsize] / 2;
  1796. const int bss = (1 << bsl) / 4;
  1797. int i, pl;
  1798. PARTITION_TYPE partition = PARTITION_NONE;
  1799. BLOCK_SIZE subsize;
  1800. ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
  1801. PARTITION_CONTEXT sl[8], sa[8];
  1802. RD_COST last_part_rdc, none_rdc, chosen_rdc;
  1803. BLOCK_SIZE sub_subsize = BLOCK_4X4;
  1804. int splits_below = 0;
  1805. BLOCK_SIZE bs_type = mi_8x8[0]->sb_type;
  1806. int do_partition_search = 1;
  1807. PICK_MODE_CONTEXT *ctx = &pc_tree->none;
  1808. if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
  1809. assert(num_4x4_blocks_wide_lookup[bsize] ==
  1810. num_4x4_blocks_high_lookup[bsize]);
  1811. vp9_rd_cost_reset(&last_part_rdc);
  1812. vp9_rd_cost_reset(&none_rdc);
  1813. vp9_rd_cost_reset(&chosen_rdc);
  1814. partition = partition_lookup[bsl][bs_type];
  1815. subsize = get_subsize(bsize, partition);
  1816. pc_tree->partitioning = partition;
  1817. save_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
  1818. if (bsize == BLOCK_16X16 && cpi->oxcf.aq_mode != NO_AQ) {
  1819. set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
  1820. x->mb_energy = vp9_block_energy(cpi, x, bsize);
  1821. }
  1822. if (do_partition_search &&
  1823. cpi->sf.partition_search_type == SEARCH_PARTITION &&
  1824. cpi->sf.adjust_partitioning_from_last_frame) {
  1825. // Check if any of the sub blocks are further split.
  1826. if (partition == PARTITION_SPLIT && subsize > BLOCK_8X8) {
  1827. sub_subsize = get_subsize(subsize, PARTITION_SPLIT);
  1828. splits_below = 1;
  1829. for (i = 0; i < 4; i++) {
  1830. int jj = i >> 1, ii = i & 0x01;
  1831. MODE_INFO *this_mi = mi_8x8[jj * bss * mis + ii * bss];
  1832. if (this_mi && this_mi->sb_type >= sub_subsize) {
  1833. splits_below = 0;
  1834. }
  1835. }
  1836. }
  1837. // If partition is not none try none unless each of the 4 splits are split
  1838. // even further..
  1839. if (partition != PARTITION_NONE && !splits_below &&
  1840. mi_row + (mi_step >> 1) < cm->mi_rows &&
  1841. mi_col + (mi_step >> 1) < cm->mi_cols) {
  1842. pc_tree->partitioning = PARTITION_NONE;
  1843. rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &none_rdc, bsize, ctx,
  1844. INT64_MAX);
  1845. pl = partition_plane_context(xd, mi_row, mi_col, bsize);
  1846. if (none_rdc.rate < INT_MAX) {
  1847. none_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
  1848. none_rdc.rdcost =
  1849. RDCOST(x->rdmult, x->rddiv, none_rdc.rate, none_rdc.dist);
  1850. }
  1851. restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
  1852. mi_8x8[0]->sb_type = bs_type;
  1853. pc_tree->partitioning = partition;
  1854. }
  1855. }
  1856. switch (partition) {
  1857. case PARTITION_NONE:
  1858. rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc, bsize,
  1859. ctx, INT64_MAX);
  1860. break;
  1861. case PARTITION_HORZ:
  1862. rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
  1863. subsize, &pc_tree->horizontal[0], INT64_MAX);
  1864. if (last_part_rdc.rate != INT_MAX && bsize >= BLOCK_8X8 &&
  1865. mi_row + (mi_step >> 1) < cm->mi_rows) {
  1866. RD_COST tmp_rdc;
  1867. PICK_MODE_CONTEXT *ctx = &pc_tree->horizontal[0];
  1868. vp9_rd_cost_init(&tmp_rdc);
  1869. update_state(cpi, td, ctx, mi_row, mi_col, subsize, 0);
  1870. encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize, ctx);
  1871. rd_pick_sb_modes(cpi, tile_data, x, mi_row + (mi_step >> 1), mi_col,
  1872. &tmp_rdc, subsize, &pc_tree->horizontal[1], INT64_MAX);
  1873. if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
  1874. vp9_rd_cost_reset(&last_part_rdc);
  1875. break;
  1876. }
  1877. last_part_rdc.rate += tmp_rdc.rate;
  1878. last_part_rdc.dist += tmp_rdc.dist;
  1879. last_part_rdc.rdcost += tmp_rdc.rdcost;
  1880. }
  1881. break;
  1882. case PARTITION_VERT:
  1883. rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
  1884. subsize, &pc_tree->vertical[0], INT64_MAX);
  1885. if (last_part_rdc.rate != INT_MAX && bsize >= BLOCK_8X8 &&
  1886. mi_col + (mi_step >> 1) < cm->mi_cols) {
  1887. RD_COST tmp_rdc;
  1888. PICK_MODE_CONTEXT *ctx = &pc_tree->vertical[0];
  1889. vp9_rd_cost_init(&tmp_rdc);
  1890. update_state(cpi, td, ctx, mi_row, mi_col, subsize, 0);
  1891. encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize, ctx);
  1892. rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + (mi_step >> 1),
  1893. &tmp_rdc, subsize,
  1894. &pc_tree->vertical[bsize > BLOCK_8X8], INT64_MAX);
  1895. if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
  1896. vp9_rd_cost_reset(&last_part_rdc);
  1897. break;
  1898. }
  1899. last_part_rdc.rate += tmp_rdc.rate;
  1900. last_part_rdc.dist += tmp_rdc.dist;
  1901. last_part_rdc.rdcost += tmp_rdc.rdcost;
  1902. }
  1903. break;
  1904. case PARTITION_SPLIT:
  1905. if (bsize == BLOCK_8X8) {
  1906. rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
  1907. subsize, pc_tree->leaf_split[0], INT64_MAX);
  1908. break;
  1909. }
  1910. last_part_rdc.rate = 0;
  1911. last_part_rdc.dist = 0;
  1912. last_part_rdc.rdcost = 0;
  1913. for (i = 0; i < 4; i++) {
  1914. int x_idx = (i & 1) * (mi_step >> 1);
  1915. int y_idx = (i >> 1) * (mi_step >> 1);
  1916. int jj = i >> 1, ii = i & 0x01;
  1917. RD_COST tmp_rdc;
  1918. if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols))
  1919. continue;
  1920. vp9_rd_cost_init(&tmp_rdc);
  1921. rd_use_partition(cpi, td, tile_data, mi_8x8 + jj * bss * mis + ii * bss,
  1922. tp, mi_row + y_idx, mi_col + x_idx, subsize,
  1923. &tmp_rdc.rate, &tmp_rdc.dist, i != 3,
  1924. pc_tree->split[i]);
  1925. if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
  1926. vp9_rd_cost_reset(&last_part_rdc);
  1927. break;
  1928. }
  1929. last_part_rdc.rate += tmp_rdc.rate;
  1930. last_part_rdc.dist += tmp_rdc.dist;
  1931. }
  1932. break;
  1933. default: assert(0); break;
  1934. }
  1935. pl = partition_plane_context(xd, mi_row, mi_col, bsize);
  1936. if (last_part_rdc.rate < INT_MAX) {
  1937. last_part_rdc.rate += cpi->partition_cost[pl][partition];
  1938. last_part_rdc.rdcost =
  1939. RDCOST(x->rdmult, x->rddiv, last_part_rdc.rate, last_part_rdc.dist);
  1940. }
  1941. if (do_partition_search && cpi->sf.adjust_partitioning_from_last_frame &&
  1942. cpi->sf.partition_search_type == SEARCH_PARTITION &&
  1943. partition != PARTITION_SPLIT && bsize > BLOCK_8X8 &&
  1944. (mi_row + mi_step < cm->mi_rows ||
  1945. mi_row + (mi_step >> 1) == cm->mi_rows) &&
  1946. (mi_col + mi_step < cm->mi_cols ||
  1947. mi_col + (mi_step >> 1) == cm->mi_cols)) {
  1948. BLOCK_SIZE split_subsize = get_subsize(bsize, PARTITION_SPLIT);
  1949. chosen_rdc.rate = 0;
  1950. chosen_rdc.dist = 0;
  1951. restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
  1952. pc_tree->partitioning = PARTITION_SPLIT;
  1953. // Split partition.
  1954. for (i = 0; i < 4; i++) {
  1955. int x_idx = (i & 1) * (mi_step >> 1);
  1956. int y_idx = (i >> 1) * (mi_step >> 1);
  1957. RD_COST tmp_rdc;
  1958. ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
  1959. PARTITION_CONTEXT sl[8], sa[8];
  1960. if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols))
  1961. continue;
  1962. save_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
  1963. pc_tree->split[i]->partitioning = PARTITION_NONE;
  1964. rd_pick_sb_modes(cpi, tile_data, x, mi_row + y_idx, mi_col + x_idx,
  1965. &tmp_rdc, split_subsize, &pc_tree->split[i]->none,
  1966. INT64_MAX);
  1967. restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
  1968. if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
  1969. vp9_rd_cost_reset(&chosen_rdc);
  1970. break;
  1971. }
  1972. chosen_rdc.rate += tmp_rdc.rate;
  1973. chosen_rdc.dist += tmp_rdc.dist;
  1974. if (i != 3)
  1975. encode_sb(cpi, td, tile_info, tp, mi_row + y_idx, mi_col + x_idx, 0,
  1976. split_subsize, pc_tree->split[i]);
  1977. pl = partition_plane_context(xd, mi_row + y_idx, mi_col + x_idx,
  1978. split_subsize);
  1979. chosen_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
  1980. }
  1981. pl = partition_plane_context(xd, mi_row, mi_col, bsize);
  1982. if (chosen_rdc.rate < INT_MAX) {
  1983. chosen_rdc.rate += cpi->partition_cost[pl][PARTITION_SPLIT];
  1984. chosen_rdc.rdcost =
  1985. RDCOST(x->rdmult, x->rddiv, chosen_rdc.rate, chosen_rdc.dist);
  1986. }
  1987. }
  1988. // If last_part is better set the partitioning to that.
  1989. if (last_part_rdc.rdcost < chosen_rdc.rdcost) {
  1990. mi_8x8[0]->sb_type = bsize;
  1991. if (bsize >= BLOCK_8X8) pc_tree->partitioning = partition;
  1992. chosen_rdc = last_part_rdc;
  1993. }
  1994. // If none was better set the partitioning to that.
  1995. if (none_rdc.rdcost < chosen_rdc.rdcost) {
  1996. if (bsize >= BLOCK_8X8) pc_tree->partitioning = PARTITION_NONE;
  1997. chosen_rdc = none_rdc;
  1998. }
  1999. restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
  2000. // We must have chosen a partitioning and encoding or we'll fail later on.
  2001. // No other opportunities for success.
  2002. if (bsize == BLOCK_64X64)
  2003. assert(chosen_rdc.rate < INT_MAX && chosen_rdc.dist < INT64_MAX);
  2004. if (do_recon) {
  2005. int output_enabled = (bsize == BLOCK_64X64);
  2006. encode_sb(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled, bsize,
  2007. pc_tree);
  2008. }
  2009. *rate = chosen_rdc.rate;
  2010. *dist = chosen_rdc.dist;
  2011. }
  2012. static const BLOCK_SIZE min_partition_size[BLOCK_SIZES] = {
  2013. BLOCK_4X4, BLOCK_4X4, BLOCK_4X4, BLOCK_4X4, BLOCK_4X4,
  2014. BLOCK_4X4, BLOCK_8X8, BLOCK_8X8, BLOCK_8X8, BLOCK_16X16,
  2015. BLOCK_16X16, BLOCK_16X16, BLOCK_16X16
  2016. };
  2017. static const BLOCK_SIZE max_partition_size[BLOCK_SIZES] = {
  2018. BLOCK_8X8, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16, BLOCK_32X32,
  2019. BLOCK_32X32, BLOCK_32X32, BLOCK_64X64, BLOCK_64X64, BLOCK_64X64,
  2020. BLOCK_64X64, BLOCK_64X64, BLOCK_64X64
  2021. };
  2022. // Look at all the mode_info entries for blocks that are part of this
  2023. // partition and find the min and max values for sb_type.
  2024. // At the moment this is designed to work on a 64x64 SB but could be
  2025. // adjusted to use a size parameter.
  2026. //
  2027. // The min and max are assumed to have been initialized prior to calling this
  2028. // function so repeat calls can accumulate a min and max of more than one sb64.
  2029. static void get_sb_partition_size_range(MACROBLOCKD *xd, MODE_INFO **mi_8x8,
  2030. BLOCK_SIZE *min_block_size,
  2031. BLOCK_SIZE *max_block_size,
  2032. int bs_hist[BLOCK_SIZES]) {
  2033. int sb_width_in_blocks = MI_BLOCK_SIZE;
  2034. int sb_height_in_blocks = MI_BLOCK_SIZE;
  2035. int i, j;
  2036. int index = 0;
  2037. // Check the sb_type for each block that belongs to this region.
  2038. for (i = 0; i < sb_height_in_blocks; ++i) {
  2039. for (j = 0; j < sb_width_in_blocks; ++j) {
  2040. MODE_INFO *mi = mi_8x8[index + j];
  2041. BLOCK_SIZE sb_type = mi ? mi->sb_type : 0;
  2042. bs_hist[sb_type]++;
  2043. *min_block_size = VPXMIN(*min_block_size, sb_type);
  2044. *max_block_size = VPXMAX(*max_block_size, sb_type);
  2045. }
  2046. index += xd->mi_stride;
  2047. }
  2048. }
  2049. // Next square block size less or equal than current block size.
  2050. static const BLOCK_SIZE next_square_size[BLOCK_SIZES] = {
  2051. BLOCK_4X4, BLOCK_4X4, BLOCK_4X4, BLOCK_8X8, BLOCK_8X8,
  2052. BLOCK_8X8, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16, BLOCK_32X32,
  2053. BLOCK_32X32, BLOCK_32X32, BLOCK_64X64
  2054. };
  2055. // Look at neighboring blocks and set a min and max partition size based on
  2056. // what they chose.
  2057. static void rd_auto_partition_range(VP9_COMP *cpi, const TileInfo *const tile,
  2058. MACROBLOCKD *const xd, int mi_row,
  2059. int mi_col, BLOCK_SIZE *min_block_size,
  2060. BLOCK_SIZE *max_block_size) {
  2061. VP9_COMMON *const cm = &cpi->common;
  2062. MODE_INFO **mi = xd->mi;
  2063. const int left_in_image = !!xd->left_mi;
  2064. const int above_in_image = !!xd->above_mi;
  2065. const int row8x8_remaining = tile->mi_row_end - mi_row;
  2066. const int col8x8_remaining = tile->mi_col_end - mi_col;
  2067. int bh, bw;
  2068. BLOCK_SIZE min_size = BLOCK_4X4;
  2069. BLOCK_SIZE max_size = BLOCK_64X64;
  2070. int bs_hist[BLOCK_SIZES] = { 0 };
  2071. // Trap case where we do not have a prediction.
  2072. if (left_in_image || above_in_image || cm->frame_type != KEY_FRAME) {
  2073. // Default "min to max" and "max to min"
  2074. min_size = BLOCK_64X64;
  2075. max_size = BLOCK_4X4;
  2076. // NOTE: each call to get_sb_partition_size_range() uses the previous
  2077. // passed in values for min and max as a starting point.
  2078. // Find the min and max partition used in previous frame at this location
  2079. if (cm->frame_type != KEY_FRAME) {
  2080. MODE_INFO **prev_mi =
  2081. &cm->prev_mi_grid_visible[mi_row * xd->mi_stride + mi_col];
  2082. get_sb_partition_size_range(xd, prev_mi, &min_size, &max_size, bs_hist);
  2083. }
  2084. // Find the min and max partition sizes used in the left SB64
  2085. if (left_in_image) {
  2086. MODE_INFO **left_sb64_mi = &mi[-MI_BLOCK_SIZE];
  2087. get_sb_partition_size_range(xd, left_sb64_mi, &min_size, &max_size,
  2088. bs_hist);
  2089. }
  2090. // Find the min and max partition sizes used in the above SB64.
  2091. if (above_in_image) {
  2092. MODE_INFO **above_sb64_mi = &mi[-xd->mi_stride * MI_BLOCK_SIZE];
  2093. get_sb_partition_size_range(xd, above_sb64_mi, &min_size, &max_size,
  2094. bs_hist);
  2095. }
  2096. // Adjust observed min and max for "relaxed" auto partition case.
  2097. if (cpi->sf.auto_min_max_partition_size == RELAXED_NEIGHBORING_MIN_MAX) {
  2098. min_size = min_partition_size[min_size];
  2099. max_size = max_partition_size[max_size];
  2100. }
  2101. }
  2102. // Check border cases where max and min from neighbors may not be legal.
  2103. max_size = find_partition_size(max_size, row8x8_remaining, col8x8_remaining,
  2104. &bh, &bw);
  2105. // Test for blocks at the edge of the active image.
  2106. // This may be the actual edge of the image or where there are formatting
  2107. // bars.
  2108. if (vp9_active_edge_sb(cpi, mi_row, mi_col)) {
  2109. min_size = BLOCK_4X4;
  2110. } else {
  2111. min_size =
  2112. VPXMIN(cpi->sf.rd_auto_partition_min_limit, VPXMIN(min_size, max_size));
  2113. }
  2114. // When use_square_partition_only is true, make sure at least one square
  2115. // partition is allowed by selecting the next smaller square size as
  2116. // *min_block_size.
  2117. if (cpi->sf.use_square_partition_only &&
  2118. next_square_size[max_size] < min_size) {
  2119. min_size = next_square_size[max_size];
  2120. }
  2121. *min_block_size = min_size;
  2122. *max_block_size = max_size;
  2123. }
  2124. // TODO(jingning) refactor functions setting partition search range
  2125. static void set_partition_range(VP9_COMMON *cm, MACROBLOCKD *xd, int mi_row,
  2126. int mi_col, BLOCK_SIZE bsize,
  2127. BLOCK_SIZE *min_bs, BLOCK_SIZE *max_bs) {
  2128. int mi_width = num_8x8_blocks_wide_lookup[bsize];
  2129. int mi_height = num_8x8_blocks_high_lookup[bsize];
  2130. int idx, idy;
  2131. MODE_INFO *mi;
  2132. const int idx_str = cm->mi_stride * mi_row + mi_col;
  2133. MODE_INFO **prev_mi = &cm->prev_mi_grid_visible[idx_str];
  2134. BLOCK_SIZE bs, min_size, max_size;
  2135. min_size = BLOCK_64X64;
  2136. max_size = BLOCK_4X4;
  2137. if (prev_mi) {
  2138. for (idy = 0; idy < mi_height; ++idy) {
  2139. for (idx = 0; idx < mi_width; ++idx) {
  2140. mi = prev_mi[idy * cm->mi_stride + idx];
  2141. bs = mi ? mi->sb_type : bsize;
  2142. min_size = VPXMIN(min_size, bs);
  2143. max_size = VPXMAX(max_size, bs);
  2144. }
  2145. }
  2146. }
  2147. if (xd->left_mi) {
  2148. for (idy = 0; idy < mi_height; ++idy) {
  2149. mi = xd->mi[idy * cm->mi_stride - 1];
  2150. bs = mi ? mi->sb_type : bsize;
  2151. min_size = VPXMIN(min_size, bs);
  2152. max_size = VPXMAX(max_size, bs);
  2153. }
  2154. }
  2155. if (xd->above_mi) {
  2156. for (idx = 0; idx < mi_width; ++idx) {
  2157. mi = xd->mi[idx - cm->mi_stride];
  2158. bs = mi ? mi->sb_type : bsize;
  2159. min_size = VPXMIN(min_size, bs);
  2160. max_size = VPXMAX(max_size, bs);
  2161. }
  2162. }
  2163. if (min_size == max_size) {
  2164. min_size = min_partition_size[min_size];
  2165. max_size = max_partition_size[max_size];
  2166. }
  2167. *min_bs = min_size;
  2168. *max_bs = max_size;
  2169. }
  2170. static INLINE void store_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
  2171. memcpy(ctx->pred_mv, x->pred_mv, sizeof(x->pred_mv));
  2172. }
  2173. static INLINE void load_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
  2174. memcpy(x->pred_mv, ctx->pred_mv, sizeof(x->pred_mv));
  2175. }
  2176. #if CONFIG_FP_MB_STATS
  2177. const int num_16x16_blocks_wide_lookup[BLOCK_SIZES] = { 1, 1, 1, 1, 1, 1, 1,
  2178. 1, 2, 2, 2, 4, 4 };
  2179. const int num_16x16_blocks_high_lookup[BLOCK_SIZES] = { 1, 1, 1, 1, 1, 1, 1,
  2180. 2, 1, 2, 4, 2, 4 };
  2181. const int qindex_skip_threshold_lookup[BLOCK_SIZES] = {
  2182. 0, 10, 10, 30, 40, 40, 60, 80, 80, 90, 100, 100, 120
  2183. };
  2184. const int qindex_split_threshold_lookup[BLOCK_SIZES] = {
  2185. 0, 3, 3, 7, 15, 15, 30, 40, 40, 60, 80, 80, 120
  2186. };
  2187. const int complexity_16x16_blocks_threshold[BLOCK_SIZES] = {
  2188. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 6
  2189. };
  2190. typedef enum {
  2191. MV_ZERO = 0,
  2192. MV_LEFT = 1,
  2193. MV_UP = 2,
  2194. MV_RIGHT = 3,
  2195. MV_DOWN = 4,
  2196. MV_INVALID
  2197. } MOTION_DIRECTION;
  2198. static INLINE MOTION_DIRECTION get_motion_direction_fp(uint8_t fp_byte) {
  2199. if (fp_byte & FPMB_MOTION_ZERO_MASK) {
  2200. return MV_ZERO;
  2201. } else if (fp_byte & FPMB_MOTION_LEFT_MASK) {
  2202. return MV_LEFT;
  2203. } else if (fp_byte & FPMB_MOTION_RIGHT_MASK) {
  2204. return MV_RIGHT;
  2205. } else if (fp_byte & FPMB_MOTION_UP_MASK) {
  2206. return MV_UP;
  2207. } else {
  2208. return MV_DOWN;
  2209. }
  2210. }
  2211. static INLINE int get_motion_inconsistency(MOTION_DIRECTION this_mv,
  2212. MOTION_DIRECTION that_mv) {
  2213. if (this_mv == that_mv) {
  2214. return 0;
  2215. } else {
  2216. return abs(this_mv - that_mv) == 2 ? 2 : 1;
  2217. }
  2218. }
  2219. #endif
  2220. // TODO(jingning,jimbankoski,rbultje): properly skip partition types that are
  2221. // unlikely to be selected depending on previous rate-distortion optimization
  2222. // results, for encoding speed-up.
  2223. static void rd_pick_partition(VP9_COMP *cpi, ThreadData *td,
  2224. TileDataEnc *tile_data, TOKENEXTRA **tp,
  2225. int mi_row, int mi_col, BLOCK_SIZE bsize,
  2226. RD_COST *rd_cost, int64_t best_rd,
  2227. PC_TREE *pc_tree) {
  2228. VP9_COMMON *const cm = &cpi->common;
  2229. TileInfo *const tile_info = &tile_data->tile_info;
  2230. MACROBLOCK *const x = &td->mb;
  2231. MACROBLOCKD *const xd = &x->e_mbd;
  2232. const int mi_step = num_8x8_blocks_wide_lookup[bsize] / 2;
  2233. ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
  2234. PARTITION_CONTEXT sl[8], sa[8];
  2235. TOKENEXTRA *tp_orig = *tp;
  2236. PICK_MODE_CONTEXT *ctx = &pc_tree->none;
  2237. int i;
  2238. const int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
  2239. BLOCK_SIZE subsize;
  2240. RD_COST this_rdc, sum_rdc, best_rdc;
  2241. int do_split = bsize >= BLOCK_8X8;
  2242. int do_rect = 1;
  2243. INTERP_FILTER pred_interp_filter;
  2244. // Override skipping rectangular partition operations for edge blocks
  2245. const int force_horz_split = (mi_row + mi_step >= cm->mi_rows);
  2246. const int force_vert_split = (mi_col + mi_step >= cm->mi_cols);
  2247. const int xss = x->e_mbd.plane[1].subsampling_x;
  2248. const int yss = x->e_mbd.plane[1].subsampling_y;
  2249. BLOCK_SIZE min_size = x->min_partition_size;
  2250. BLOCK_SIZE max_size = x->max_partition_size;
  2251. #if CONFIG_FP_MB_STATS
  2252. unsigned int src_diff_var = UINT_MAX;
  2253. int none_complexity = 0;
  2254. #endif
  2255. int partition_none_allowed = !force_horz_split && !force_vert_split;
  2256. int partition_horz_allowed =
  2257. !force_vert_split && yss <= xss && bsize >= BLOCK_8X8;
  2258. int partition_vert_allowed =
  2259. !force_horz_split && xss <= yss && bsize >= BLOCK_8X8;
  2260. int64_t dist_breakout_thr = cpi->sf.partition_search_breakout_dist_thr;
  2261. int rate_breakout_thr = cpi->sf.partition_search_breakout_rate_thr;
  2262. (void)*tp_orig;
  2263. assert(num_8x8_blocks_wide_lookup[bsize] ==
  2264. num_8x8_blocks_high_lookup[bsize]);
  2265. // Adjust dist breakout threshold according to the partition size.
  2266. dist_breakout_thr >>=
  2267. 8 - (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
  2268. rate_breakout_thr *= num_pels_log2_lookup[bsize];
  2269. vp9_rd_cost_init(&this_rdc);
  2270. vp9_rd_cost_init(&sum_rdc);
  2271. vp9_rd_cost_reset(&best_rdc);
  2272. best_rdc.rdcost = best_rd;
  2273. set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
  2274. if (bsize == BLOCK_16X16 && cpi->oxcf.aq_mode != NO_AQ &&
  2275. cpi->oxcf.aq_mode != LOOKAHEAD_AQ)
  2276. x->mb_energy = vp9_block_energy(cpi, x, bsize);
  2277. if (cpi->sf.cb_partition_search && bsize == BLOCK_16X16) {
  2278. int cb_partition_search_ctrl =
  2279. ((pc_tree->index == 0 || pc_tree->index == 3) +
  2280. get_chessboard_index(cm->current_video_frame)) &
  2281. 0x1;
  2282. if (cb_partition_search_ctrl && bsize > min_size && bsize < max_size)
  2283. set_partition_range(cm, xd, mi_row, mi_col, bsize, &min_size, &max_size);
  2284. }
  2285. // Determine partition types in search according to the speed features.
  2286. // The threshold set here has to be of square block size.
  2287. if (cpi->sf.auto_min_max_partition_size) {
  2288. partition_none_allowed &= (bsize <= max_size && bsize >= min_size);
  2289. partition_horz_allowed &=
  2290. ((bsize <= max_size && bsize > min_size) || force_horz_split);
  2291. partition_vert_allowed &=
  2292. ((bsize <= max_size && bsize > min_size) || force_vert_split);
  2293. do_split &= bsize > min_size;
  2294. }
  2295. if (cpi->sf.use_square_partition_only &&
  2296. bsize > cpi->sf.use_square_only_threshold) {
  2297. if (cpi->use_svc) {
  2298. if (!vp9_active_h_edge(cpi, mi_row, mi_step) || x->e_mbd.lossless)
  2299. partition_horz_allowed &= force_horz_split;
  2300. if (!vp9_active_v_edge(cpi, mi_row, mi_step) || x->e_mbd.lossless)
  2301. partition_vert_allowed &= force_vert_split;
  2302. } else {
  2303. partition_horz_allowed &= force_horz_split;
  2304. partition_vert_allowed &= force_vert_split;
  2305. }
  2306. }
  2307. save_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
  2308. #if CONFIG_FP_MB_STATS
  2309. if (cpi->use_fp_mb_stats) {
  2310. set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
  2311. src_diff_var = get_sby_perpixel_diff_variance(cpi, &x->plane[0].src, mi_row,
  2312. mi_col, bsize);
  2313. }
  2314. #endif
  2315. #if CONFIG_FP_MB_STATS
  2316. // Decide whether we shall split directly and skip searching NONE by using
  2317. // the first pass block statistics
  2318. if (cpi->use_fp_mb_stats && bsize >= BLOCK_32X32 && do_split &&
  2319. partition_none_allowed && src_diff_var > 4 &&
  2320. cm->base_qindex < qindex_split_threshold_lookup[bsize]) {
  2321. int mb_row = mi_row >> 1;
  2322. int mb_col = mi_col >> 1;
  2323. int mb_row_end =
  2324. VPXMIN(mb_row + num_16x16_blocks_high_lookup[bsize], cm->mb_rows);
  2325. int mb_col_end =
  2326. VPXMIN(mb_col + num_16x16_blocks_wide_lookup[bsize], cm->mb_cols);
  2327. int r, c;
  2328. // compute a complexity measure, basically measure inconsistency of motion
  2329. // vectors obtained from the first pass in the current block
  2330. for (r = mb_row; r < mb_row_end; r++) {
  2331. for (c = mb_col; c < mb_col_end; c++) {
  2332. const int mb_index = r * cm->mb_cols + c;
  2333. MOTION_DIRECTION this_mv;
  2334. MOTION_DIRECTION right_mv;
  2335. MOTION_DIRECTION bottom_mv;
  2336. this_mv =
  2337. get_motion_direction_fp(cpi->twopass.this_frame_mb_stats[mb_index]);
  2338. // to its right
  2339. if (c != mb_col_end - 1) {
  2340. right_mv = get_motion_direction_fp(
  2341. cpi->twopass.this_frame_mb_stats[mb_index + 1]);
  2342. none_complexity += get_motion_inconsistency(this_mv, right_mv);
  2343. }
  2344. // to its bottom
  2345. if (r != mb_row_end - 1) {
  2346. bottom_mv = get_motion_direction_fp(
  2347. cpi->twopass.this_frame_mb_stats[mb_index + cm->mb_cols]);
  2348. none_complexity += get_motion_inconsistency(this_mv, bottom_mv);
  2349. }
  2350. // do not count its left and top neighbors to avoid double counting
  2351. }
  2352. }
  2353. if (none_complexity > complexity_16x16_blocks_threshold[bsize]) {
  2354. partition_none_allowed = 0;
  2355. }
  2356. }
  2357. #endif
  2358. // PARTITION_NONE
  2359. if (partition_none_allowed) {
  2360. rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &this_rdc, bsize, ctx,
  2361. best_rdc.rdcost);
  2362. if (this_rdc.rate != INT_MAX) {
  2363. if (bsize >= BLOCK_8X8) {
  2364. this_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
  2365. this_rdc.rdcost =
  2366. RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist);
  2367. }
  2368. if (this_rdc.rdcost < best_rdc.rdcost) {
  2369. best_rdc = this_rdc;
  2370. if (bsize >= BLOCK_8X8) pc_tree->partitioning = PARTITION_NONE;
  2371. // If all y, u, v transform blocks in this partition are skippable, and
  2372. // the dist & rate are within the thresholds, the partition search is
  2373. // terminated for current branch of the partition search tree.
  2374. if (!x->e_mbd.lossless && ctx->skippable &&
  2375. ((best_rdc.dist < (dist_breakout_thr >> 2)) ||
  2376. (best_rdc.dist < dist_breakout_thr &&
  2377. best_rdc.rate < rate_breakout_thr))) {
  2378. do_split = 0;
  2379. do_rect = 0;
  2380. }
  2381. #if CONFIG_FP_MB_STATS
  2382. // Check if every 16x16 first pass block statistics has zero
  2383. // motion and the corresponding first pass residue is small enough.
  2384. // If that is the case, check the difference variance between the
  2385. // current frame and the last frame. If the variance is small enough,
  2386. // stop further splitting in RD optimization
  2387. if (cpi->use_fp_mb_stats && do_split != 0 &&
  2388. cm->base_qindex > qindex_skip_threshold_lookup[bsize]) {
  2389. int mb_row = mi_row >> 1;
  2390. int mb_col = mi_col >> 1;
  2391. int mb_row_end =
  2392. VPXMIN(mb_row + num_16x16_blocks_high_lookup[bsize], cm->mb_rows);
  2393. int mb_col_end =
  2394. VPXMIN(mb_col + num_16x16_blocks_wide_lookup[bsize], cm->mb_cols);
  2395. int r, c;
  2396. int skip = 1;
  2397. for (r = mb_row; r < mb_row_end; r++) {
  2398. for (c = mb_col; c < mb_col_end; c++) {
  2399. const int mb_index = r * cm->mb_cols + c;
  2400. if (!(cpi->twopass.this_frame_mb_stats[mb_index] &
  2401. FPMB_MOTION_ZERO_MASK) ||
  2402. !(cpi->twopass.this_frame_mb_stats[mb_index] &
  2403. FPMB_ERROR_SMALL_MASK)) {
  2404. skip = 0;
  2405. break;
  2406. }
  2407. }
  2408. if (skip == 0) {
  2409. break;
  2410. }
  2411. }
  2412. if (skip) {
  2413. if (src_diff_var == UINT_MAX) {
  2414. set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
  2415. src_diff_var = get_sby_perpixel_diff_variance(
  2416. cpi, &x->plane[0].src, mi_row, mi_col, bsize);
  2417. }
  2418. if (src_diff_var < 8) {
  2419. do_split = 0;
  2420. do_rect = 0;
  2421. }
  2422. }
  2423. }
  2424. #endif
  2425. }
  2426. }
  2427. restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
  2428. }
  2429. // store estimated motion vector
  2430. if (cpi->sf.adaptive_motion_search) store_pred_mv(x, ctx);
  2431. // If the interp_filter is marked as SWITCHABLE_FILTERS, it was for an
  2432. // intra block and used for context purposes.
  2433. if (ctx->mic.interp_filter == SWITCHABLE_FILTERS) {
  2434. pred_interp_filter = EIGHTTAP;
  2435. } else {
  2436. pred_interp_filter = ctx->mic.interp_filter;
  2437. }
  2438. // PARTITION_SPLIT
  2439. // TODO(jingning): use the motion vectors given by the above search as
  2440. // the starting point of motion search in the following partition type check.
  2441. if (do_split) {
  2442. subsize = get_subsize(bsize, PARTITION_SPLIT);
  2443. if (bsize == BLOCK_8X8) {
  2444. i = 4;
  2445. if (cpi->sf.adaptive_pred_interp_filter && partition_none_allowed)
  2446. pc_tree->leaf_split[0]->pred_interp_filter = pred_interp_filter;
  2447. rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
  2448. pc_tree->leaf_split[0], best_rdc.rdcost);
  2449. if (sum_rdc.rate == INT_MAX) sum_rdc.rdcost = INT64_MAX;
  2450. } else {
  2451. for (i = 0; i < 4 && sum_rdc.rdcost < best_rdc.rdcost; ++i) {
  2452. const int x_idx = (i & 1) * mi_step;
  2453. const int y_idx = (i >> 1) * mi_step;
  2454. if (mi_row + y_idx >= cm->mi_rows || mi_col + x_idx >= cm->mi_cols)
  2455. continue;
  2456. if (cpi->sf.adaptive_motion_search) load_pred_mv(x, ctx);
  2457. pc_tree->split[i]->index = i;
  2458. rd_pick_partition(cpi, td, tile_data, tp, mi_row + y_idx,
  2459. mi_col + x_idx, subsize, &this_rdc,
  2460. best_rdc.rdcost - sum_rdc.rdcost, pc_tree->split[i]);
  2461. if (this_rdc.rate == INT_MAX) {
  2462. sum_rdc.rdcost = INT64_MAX;
  2463. break;
  2464. } else {
  2465. sum_rdc.rate += this_rdc.rate;
  2466. sum_rdc.dist += this_rdc.dist;
  2467. sum_rdc.rdcost += this_rdc.rdcost;
  2468. }
  2469. }
  2470. }
  2471. if (sum_rdc.rdcost < best_rdc.rdcost && i == 4) {
  2472. sum_rdc.rate += cpi->partition_cost[pl][PARTITION_SPLIT];
  2473. sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
  2474. if (sum_rdc.rdcost < best_rdc.rdcost) {
  2475. best_rdc = sum_rdc;
  2476. pc_tree->partitioning = PARTITION_SPLIT;
  2477. // Rate and distortion based partition search termination clause.
  2478. if (!x->e_mbd.lossless && ((best_rdc.dist < (dist_breakout_thr >> 2)) ||
  2479. (best_rdc.dist < dist_breakout_thr &&
  2480. best_rdc.rate < rate_breakout_thr))) {
  2481. do_rect = 0;
  2482. }
  2483. }
  2484. } else {
  2485. // skip rectangular partition test when larger block size
  2486. // gives better rd cost
  2487. if ((cpi->sf.less_rectangular_check) &&
  2488. ((bsize > cpi->sf.use_square_only_threshold) ||
  2489. (best_rdc.dist < dist_breakout_thr)))
  2490. do_rect &= !partition_none_allowed;
  2491. }
  2492. restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
  2493. }
  2494. // PARTITION_HORZ
  2495. if (partition_horz_allowed &&
  2496. (do_rect || vp9_active_h_edge(cpi, mi_row, mi_step))) {
  2497. subsize = get_subsize(bsize, PARTITION_HORZ);
  2498. if (cpi->sf.adaptive_motion_search) load_pred_mv(x, ctx);
  2499. if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
  2500. partition_none_allowed)
  2501. pc_tree->horizontal[0].pred_interp_filter = pred_interp_filter;
  2502. rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
  2503. &pc_tree->horizontal[0], best_rdc.rdcost);
  2504. if (sum_rdc.rdcost < best_rdc.rdcost && mi_row + mi_step < cm->mi_rows &&
  2505. bsize > BLOCK_8X8) {
  2506. PICK_MODE_CONTEXT *ctx = &pc_tree->horizontal[0];
  2507. update_state(cpi, td, ctx, mi_row, mi_col, subsize, 0);
  2508. encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize, ctx);
  2509. if (cpi->sf.adaptive_motion_search) load_pred_mv(x, ctx);
  2510. if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
  2511. partition_none_allowed)
  2512. pc_tree->horizontal[1].pred_interp_filter = pred_interp_filter;
  2513. rd_pick_sb_modes(cpi, tile_data, x, mi_row + mi_step, mi_col, &this_rdc,
  2514. subsize, &pc_tree->horizontal[1],
  2515. best_rdc.rdcost - sum_rdc.rdcost);
  2516. if (this_rdc.rate == INT_MAX) {
  2517. sum_rdc.rdcost = INT64_MAX;
  2518. } else {
  2519. sum_rdc.rate += this_rdc.rate;
  2520. sum_rdc.dist += this_rdc.dist;
  2521. sum_rdc.rdcost += this_rdc.rdcost;
  2522. }
  2523. }
  2524. if (sum_rdc.rdcost < best_rdc.rdcost) {
  2525. sum_rdc.rate += cpi->partition_cost[pl][PARTITION_HORZ];
  2526. sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
  2527. if (sum_rdc.rdcost < best_rdc.rdcost) {
  2528. best_rdc = sum_rdc;
  2529. pc_tree->partitioning = PARTITION_HORZ;
  2530. if ((cpi->sf.less_rectangular_check) &&
  2531. (bsize > cpi->sf.use_square_only_threshold))
  2532. do_rect = 0;
  2533. }
  2534. }
  2535. restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
  2536. }
  2537. // PARTITION_VERT
  2538. if (partition_vert_allowed &&
  2539. (do_rect || vp9_active_v_edge(cpi, mi_col, mi_step))) {
  2540. subsize = get_subsize(bsize, PARTITION_VERT);
  2541. if (cpi->sf.adaptive_motion_search) load_pred_mv(x, ctx);
  2542. if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
  2543. partition_none_allowed)
  2544. pc_tree->vertical[0].pred_interp_filter = pred_interp_filter;
  2545. rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
  2546. &pc_tree->vertical[0], best_rdc.rdcost);
  2547. if (sum_rdc.rdcost < best_rdc.rdcost && mi_col + mi_step < cm->mi_cols &&
  2548. bsize > BLOCK_8X8) {
  2549. update_state(cpi, td, &pc_tree->vertical[0], mi_row, mi_col, subsize, 0);
  2550. encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize,
  2551. &pc_tree->vertical[0]);
  2552. if (cpi->sf.adaptive_motion_search) load_pred_mv(x, ctx);
  2553. if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
  2554. partition_none_allowed)
  2555. pc_tree->vertical[1].pred_interp_filter = pred_interp_filter;
  2556. rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + mi_step, &this_rdc,
  2557. subsize, &pc_tree->vertical[1],
  2558. best_rdc.rdcost - sum_rdc.rdcost);
  2559. if (this_rdc.rate == INT_MAX) {
  2560. sum_rdc.rdcost = INT64_MAX;
  2561. } else {
  2562. sum_rdc.rate += this_rdc.rate;
  2563. sum_rdc.dist += this_rdc.dist;
  2564. sum_rdc.rdcost += this_rdc.rdcost;
  2565. }
  2566. }
  2567. if (sum_rdc.rdcost < best_rdc.rdcost) {
  2568. sum_rdc.rate += cpi->partition_cost[pl][PARTITION_VERT];
  2569. sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
  2570. if (sum_rdc.rdcost < best_rdc.rdcost) {
  2571. best_rdc = sum_rdc;
  2572. pc_tree->partitioning = PARTITION_VERT;
  2573. }
  2574. }
  2575. restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
  2576. }
  2577. // TODO(jbb): This code added so that we avoid static analysis
  2578. // warning related to the fact that best_rd isn't used after this
  2579. // point. This code should be refactored so that the duplicate
  2580. // checks occur in some sub function and thus are used...
  2581. (void)best_rd;
  2582. *rd_cost = best_rdc;
  2583. if (best_rdc.rate < INT_MAX && best_rdc.dist < INT64_MAX &&
  2584. pc_tree->index != 3) {
  2585. int output_enabled = (bsize == BLOCK_64X64);
  2586. encode_sb(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled, bsize,
  2587. pc_tree);
  2588. }
  2589. if (bsize == BLOCK_64X64) {
  2590. assert(tp_orig < *tp);
  2591. assert(best_rdc.rate < INT_MAX);
  2592. assert(best_rdc.dist < INT64_MAX);
  2593. } else {
  2594. assert(tp_orig == *tp);
  2595. }
  2596. }
  2597. static void encode_rd_sb_row(VP9_COMP *cpi, ThreadData *td,
  2598. TileDataEnc *tile_data, int mi_row,
  2599. TOKENEXTRA **tp) {
  2600. VP9_COMMON *const cm = &cpi->common;
  2601. TileInfo *const tile_info = &tile_data->tile_info;
  2602. MACROBLOCK *const x = &td->mb;
  2603. MACROBLOCKD *const xd = &x->e_mbd;
  2604. SPEED_FEATURES *const sf = &cpi->sf;
  2605. const int mi_col_start = tile_info->mi_col_start;
  2606. const int mi_col_end = tile_info->mi_col_end;
  2607. int mi_col;
  2608. // Initialize the left context for the new SB row
  2609. memset(&xd->left_context, 0, sizeof(xd->left_context));
  2610. memset(xd->left_seg_context, 0, sizeof(xd->left_seg_context));
  2611. // Code each SB in the row
  2612. for (mi_col = mi_col_start; mi_col < mi_col_end; mi_col += MI_BLOCK_SIZE) {
  2613. const struct segmentation *const seg = &cm->seg;
  2614. int dummy_rate;
  2615. int64_t dummy_dist;
  2616. RD_COST dummy_rdc;
  2617. int i;
  2618. int seg_skip = 0;
  2619. const int idx_str = cm->mi_stride * mi_row + mi_col;
  2620. MODE_INFO **mi = cm->mi_grid_visible + idx_str;
  2621. if (sf->adaptive_pred_interp_filter) {
  2622. for (i = 0; i < 64; ++i) td->leaf_tree[i].pred_interp_filter = SWITCHABLE;
  2623. for (i = 0; i < 64; ++i) {
  2624. td->pc_tree[i].vertical[0].pred_interp_filter = SWITCHABLE;
  2625. td->pc_tree[i].vertical[1].pred_interp_filter = SWITCHABLE;
  2626. td->pc_tree[i].horizontal[0].pred_interp_filter = SWITCHABLE;
  2627. td->pc_tree[i].horizontal[1].pred_interp_filter = SWITCHABLE;
  2628. }
  2629. }
  2630. vp9_zero(x->pred_mv);
  2631. td->pc_root->index = 0;
  2632. if (seg->enabled) {
  2633. const uint8_t *const map =
  2634. seg->update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
  2635. int segment_id = get_segment_id(cm, map, BLOCK_64X64, mi_row, mi_col);
  2636. seg_skip = segfeature_active(seg, segment_id, SEG_LVL_SKIP);
  2637. }
  2638. x->source_variance = UINT_MAX;
  2639. if (sf->partition_search_type == FIXED_PARTITION || seg_skip) {
  2640. const BLOCK_SIZE bsize =
  2641. seg_skip ? BLOCK_64X64 : sf->always_this_block_size;
  2642. set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
  2643. set_fixed_partitioning(cpi, tile_info, mi, mi_row, mi_col, bsize);
  2644. rd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col, BLOCK_64X64,
  2645. &dummy_rate, &dummy_dist, 1, td->pc_root);
  2646. } else if (cpi->partition_search_skippable_frame) {
  2647. BLOCK_SIZE bsize;
  2648. set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
  2649. bsize = get_rd_var_based_fixed_partition(cpi, x, mi_row, mi_col);
  2650. set_fixed_partitioning(cpi, tile_info, mi, mi_row, mi_col, bsize);
  2651. rd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col, BLOCK_64X64,
  2652. &dummy_rate, &dummy_dist, 1, td->pc_root);
  2653. } else if (sf->partition_search_type == VAR_BASED_PARTITION &&
  2654. cm->frame_type != KEY_FRAME) {
  2655. choose_partitioning(cpi, tile_info, x, mi_row, mi_col);
  2656. rd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col, BLOCK_64X64,
  2657. &dummy_rate, &dummy_dist, 1, td->pc_root);
  2658. } else {
  2659. // If required set upper and lower partition size limits
  2660. if (sf->auto_min_max_partition_size) {
  2661. set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
  2662. rd_auto_partition_range(cpi, tile_info, xd, mi_row, mi_col,
  2663. &x->min_partition_size, &x->max_partition_size);
  2664. }
  2665. rd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, BLOCK_64X64,
  2666. &dummy_rdc, INT64_MAX, td->pc_root);
  2667. }
  2668. }
  2669. }
  2670. static void init_encode_frame_mb_context(VP9_COMP *cpi) {
  2671. MACROBLOCK *const x = &cpi->td.mb;
  2672. VP9_COMMON *const cm = &cpi->common;
  2673. MACROBLOCKD *const xd = &x->e_mbd;
  2674. const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
  2675. // Copy data over into macro block data structures.
  2676. vp9_setup_src_planes(x, cpi->Source, 0, 0);
  2677. vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
  2678. // Note: this memset assumes above_context[0], [1] and [2]
  2679. // are allocated as part of the same buffer.
  2680. memset(xd->above_context[0], 0,
  2681. sizeof(*xd->above_context[0]) * 2 * aligned_mi_cols * MAX_MB_PLANE);
  2682. memset(xd->above_seg_context, 0,
  2683. sizeof(*xd->above_seg_context) * aligned_mi_cols);
  2684. }
  2685. static int check_dual_ref_flags(VP9_COMP *cpi) {
  2686. const int ref_flags = cpi->ref_frame_flags;
  2687. if (segfeature_active(&cpi->common.seg, 1, SEG_LVL_REF_FRAME)) {
  2688. return 0;
  2689. } else {
  2690. return (!!(ref_flags & VP9_GOLD_FLAG) + !!(ref_flags & VP9_LAST_FLAG) +
  2691. !!(ref_flags & VP9_ALT_FLAG)) >= 2;
  2692. }
  2693. }
  2694. static void reset_skip_tx_size(VP9_COMMON *cm, TX_SIZE max_tx_size) {
  2695. int mi_row, mi_col;
  2696. const int mis = cm->mi_stride;
  2697. MODE_INFO **mi_ptr = cm->mi_grid_visible;
  2698. for (mi_row = 0; mi_row < cm->mi_rows; ++mi_row, mi_ptr += mis) {
  2699. for (mi_col = 0; mi_col < cm->mi_cols; ++mi_col) {
  2700. if (mi_ptr[mi_col]->tx_size > max_tx_size)
  2701. mi_ptr[mi_col]->tx_size = max_tx_size;
  2702. }
  2703. }
  2704. }
  2705. static MV_REFERENCE_FRAME get_frame_type(const VP9_COMP *cpi) {
  2706. if (frame_is_intra_only(&cpi->common))
  2707. return INTRA_FRAME;
  2708. else if (cpi->rc.is_src_frame_alt_ref && cpi->refresh_golden_frame)
  2709. return ALTREF_FRAME;
  2710. else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)
  2711. return GOLDEN_FRAME;
  2712. else
  2713. return LAST_FRAME;
  2714. }
  2715. static TX_MODE select_tx_mode(const VP9_COMP *cpi, MACROBLOCKD *const xd) {
  2716. if (xd->lossless) return ONLY_4X4;
  2717. if (cpi->common.frame_type == KEY_FRAME && cpi->sf.use_nonrd_pick_mode)
  2718. return ALLOW_16X16;
  2719. if (cpi->sf.tx_size_search_method == USE_LARGESTALL)
  2720. return ALLOW_32X32;
  2721. else if (cpi->sf.tx_size_search_method == USE_FULL_RD ||
  2722. cpi->sf.tx_size_search_method == USE_TX_8X8)
  2723. return TX_MODE_SELECT;
  2724. else
  2725. return cpi->common.tx_mode;
  2726. }
  2727. static void hybrid_intra_mode_search(VP9_COMP *cpi, MACROBLOCK *const x,
  2728. RD_COST *rd_cost, BLOCK_SIZE bsize,
  2729. PICK_MODE_CONTEXT *ctx) {
  2730. if (bsize < BLOCK_16X16)
  2731. vp9_rd_pick_intra_mode_sb(cpi, x, rd_cost, bsize, ctx, INT64_MAX);
  2732. else
  2733. vp9_pick_intra_mode(cpi, x, rd_cost, bsize, ctx);
  2734. }
  2735. static void nonrd_pick_sb_modes(VP9_COMP *cpi, TileDataEnc *tile_data,
  2736. MACROBLOCK *const x, int mi_row, int mi_col,
  2737. RD_COST *rd_cost, BLOCK_SIZE bsize,
  2738. PICK_MODE_CONTEXT *ctx) {
  2739. VP9_COMMON *const cm = &cpi->common;
  2740. TileInfo *const tile_info = &tile_data->tile_info;
  2741. MACROBLOCKD *const xd = &x->e_mbd;
  2742. MODE_INFO *mi;
  2743. ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
  2744. BLOCK_SIZE bs = VPXMAX(bsize, BLOCK_8X8); // processing unit block size
  2745. const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bs];
  2746. const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bs];
  2747. int plane;
  2748. set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
  2749. mi = xd->mi[0];
  2750. mi->sb_type = bsize;
  2751. for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
  2752. struct macroblockd_plane *pd = &xd->plane[plane];
  2753. memcpy(a + num_4x4_blocks_wide * plane, pd->above_context,
  2754. (sizeof(a[0]) * num_4x4_blocks_wide) >> pd->subsampling_x);
  2755. memcpy(l + num_4x4_blocks_high * plane, pd->left_context,
  2756. (sizeof(l[0]) * num_4x4_blocks_high) >> pd->subsampling_y);
  2757. }
  2758. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled)
  2759. if (cyclic_refresh_segment_id_boosted(mi->segment_id))
  2760. x->rdmult = vp9_cyclic_refresh_get_rdmult(cpi->cyclic_refresh);
  2761. if (cm->frame_type == KEY_FRAME)
  2762. hybrid_intra_mode_search(cpi, x, rd_cost, bsize, ctx);
  2763. else if (segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_SKIP))
  2764. set_mode_info_seg_skip(x, cm->tx_mode, rd_cost, bsize);
  2765. else if (bsize >= BLOCK_8X8)
  2766. vp9_pick_inter_mode(cpi, x, tile_data, mi_row, mi_col, rd_cost, bsize, ctx);
  2767. else
  2768. vp9_pick_inter_mode_sub8x8(cpi, x, mi_row, mi_col, rd_cost, bsize, ctx);
  2769. duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, bsize);
  2770. for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
  2771. struct macroblockd_plane *pd = &xd->plane[plane];
  2772. memcpy(pd->above_context, a + num_4x4_blocks_wide * plane,
  2773. (sizeof(a[0]) * num_4x4_blocks_wide) >> pd->subsampling_x);
  2774. memcpy(pd->left_context, l + num_4x4_blocks_high * plane,
  2775. (sizeof(l[0]) * num_4x4_blocks_high) >> pd->subsampling_y);
  2776. }
  2777. if (rd_cost->rate == INT_MAX) vp9_rd_cost_reset(rd_cost);
  2778. ctx->rate = rd_cost->rate;
  2779. ctx->dist = rd_cost->dist;
  2780. }
  2781. static void fill_mode_info_sb(VP9_COMMON *cm, MACROBLOCK *x, int mi_row,
  2782. int mi_col, BLOCK_SIZE bsize, PC_TREE *pc_tree) {
  2783. MACROBLOCKD *xd = &x->e_mbd;
  2784. int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
  2785. PARTITION_TYPE partition = pc_tree->partitioning;
  2786. BLOCK_SIZE subsize = get_subsize(bsize, partition);
  2787. assert(bsize >= BLOCK_8X8);
  2788. if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
  2789. switch (partition) {
  2790. case PARTITION_NONE:
  2791. set_mode_info_offsets(cm, x, xd, mi_row, mi_col);
  2792. *(xd->mi[0]) = pc_tree->none.mic;
  2793. *(x->mbmi_ext) = pc_tree->none.mbmi_ext;
  2794. duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, bsize);
  2795. break;
  2796. case PARTITION_VERT:
  2797. set_mode_info_offsets(cm, x, xd, mi_row, mi_col);
  2798. *(xd->mi[0]) = pc_tree->vertical[0].mic;
  2799. *(x->mbmi_ext) = pc_tree->vertical[0].mbmi_ext;
  2800. duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, subsize);
  2801. if (mi_col + hbs < cm->mi_cols) {
  2802. set_mode_info_offsets(cm, x, xd, mi_row, mi_col + hbs);
  2803. *(xd->mi[0]) = pc_tree->vertical[1].mic;
  2804. *(x->mbmi_ext) = pc_tree->vertical[1].mbmi_ext;
  2805. duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col + hbs, subsize);
  2806. }
  2807. break;
  2808. case PARTITION_HORZ:
  2809. set_mode_info_offsets(cm, x, xd, mi_row, mi_col);
  2810. *(xd->mi[0]) = pc_tree->horizontal[0].mic;
  2811. *(x->mbmi_ext) = pc_tree->horizontal[0].mbmi_ext;
  2812. duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, subsize);
  2813. if (mi_row + hbs < cm->mi_rows) {
  2814. set_mode_info_offsets(cm, x, xd, mi_row + hbs, mi_col);
  2815. *(xd->mi[0]) = pc_tree->horizontal[1].mic;
  2816. *(x->mbmi_ext) = pc_tree->horizontal[1].mbmi_ext;
  2817. duplicate_mode_info_in_sb(cm, xd, mi_row + hbs, mi_col, subsize);
  2818. }
  2819. break;
  2820. case PARTITION_SPLIT: {
  2821. fill_mode_info_sb(cm, x, mi_row, mi_col, subsize, pc_tree->split[0]);
  2822. fill_mode_info_sb(cm, x, mi_row, mi_col + hbs, subsize,
  2823. pc_tree->split[1]);
  2824. fill_mode_info_sb(cm, x, mi_row + hbs, mi_col, subsize,
  2825. pc_tree->split[2]);
  2826. fill_mode_info_sb(cm, x, mi_row + hbs, mi_col + hbs, subsize,
  2827. pc_tree->split[3]);
  2828. break;
  2829. }
  2830. default: break;
  2831. }
  2832. }
  2833. // Reset the prediction pixel ready flag recursively.
  2834. static void pred_pixel_ready_reset(PC_TREE *pc_tree, BLOCK_SIZE bsize) {
  2835. pc_tree->none.pred_pixel_ready = 0;
  2836. pc_tree->horizontal[0].pred_pixel_ready = 0;
  2837. pc_tree->horizontal[1].pred_pixel_ready = 0;
  2838. pc_tree->vertical[0].pred_pixel_ready = 0;
  2839. pc_tree->vertical[1].pred_pixel_ready = 0;
  2840. if (bsize > BLOCK_8X8) {
  2841. BLOCK_SIZE subsize = get_subsize(bsize, PARTITION_SPLIT);
  2842. int i;
  2843. for (i = 0; i < 4; ++i) pred_pixel_ready_reset(pc_tree->split[i], subsize);
  2844. }
  2845. }
  2846. static void nonrd_pick_partition(VP9_COMP *cpi, ThreadData *td,
  2847. TileDataEnc *tile_data, TOKENEXTRA **tp,
  2848. int mi_row, int mi_col, BLOCK_SIZE bsize,
  2849. RD_COST *rd_cost, int do_recon,
  2850. int64_t best_rd, PC_TREE *pc_tree) {
  2851. const SPEED_FEATURES *const sf = &cpi->sf;
  2852. VP9_COMMON *const cm = &cpi->common;
  2853. TileInfo *const tile_info = &tile_data->tile_info;
  2854. MACROBLOCK *const x = &td->mb;
  2855. MACROBLOCKD *const xd = &x->e_mbd;
  2856. const int ms = num_8x8_blocks_wide_lookup[bsize] / 2;
  2857. TOKENEXTRA *tp_orig = *tp;
  2858. PICK_MODE_CONTEXT *ctx = &pc_tree->none;
  2859. int i;
  2860. BLOCK_SIZE subsize = bsize;
  2861. RD_COST this_rdc, sum_rdc, best_rdc;
  2862. int do_split = bsize >= BLOCK_8X8;
  2863. int do_rect = 1;
  2864. // Override skipping rectangular partition operations for edge blocks
  2865. const int force_horz_split = (mi_row + ms >= cm->mi_rows);
  2866. const int force_vert_split = (mi_col + ms >= cm->mi_cols);
  2867. const int xss = x->e_mbd.plane[1].subsampling_x;
  2868. const int yss = x->e_mbd.plane[1].subsampling_y;
  2869. int partition_none_allowed = !force_horz_split && !force_vert_split;
  2870. int partition_horz_allowed =
  2871. !force_vert_split && yss <= xss && bsize >= BLOCK_8X8;
  2872. int partition_vert_allowed =
  2873. !force_horz_split && xss <= yss && bsize >= BLOCK_8X8;
  2874. (void)*tp_orig;
  2875. assert(num_8x8_blocks_wide_lookup[bsize] ==
  2876. num_8x8_blocks_high_lookup[bsize]);
  2877. vp9_rd_cost_init(&sum_rdc);
  2878. vp9_rd_cost_reset(&best_rdc);
  2879. best_rdc.rdcost = best_rd;
  2880. // Determine partition types in search according to the speed features.
  2881. // The threshold set here has to be of square block size.
  2882. if (sf->auto_min_max_partition_size) {
  2883. partition_none_allowed &=
  2884. (bsize <= x->max_partition_size && bsize >= x->min_partition_size);
  2885. partition_horz_allowed &=
  2886. ((bsize <= x->max_partition_size && bsize > x->min_partition_size) ||
  2887. force_horz_split);
  2888. partition_vert_allowed &=
  2889. ((bsize <= x->max_partition_size && bsize > x->min_partition_size) ||
  2890. force_vert_split);
  2891. do_split &= bsize > x->min_partition_size;
  2892. }
  2893. if (sf->use_square_partition_only) {
  2894. partition_horz_allowed &= force_horz_split;
  2895. partition_vert_allowed &= force_vert_split;
  2896. }
  2897. ctx->pred_pixel_ready =
  2898. !(partition_vert_allowed || partition_horz_allowed || do_split);
  2899. // PARTITION_NONE
  2900. if (partition_none_allowed) {
  2901. nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &this_rdc, bsize,
  2902. ctx);
  2903. ctx->mic = *xd->mi[0];
  2904. ctx->mbmi_ext = *x->mbmi_ext;
  2905. ctx->skip_txfm[0] = x->skip_txfm[0];
  2906. ctx->skip = x->skip;
  2907. if (this_rdc.rate != INT_MAX) {
  2908. int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
  2909. this_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
  2910. this_rdc.rdcost =
  2911. RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist);
  2912. if (this_rdc.rdcost < best_rdc.rdcost) {
  2913. int64_t dist_breakout_thr = sf->partition_search_breakout_dist_thr;
  2914. int64_t rate_breakout_thr = sf->partition_search_breakout_rate_thr;
  2915. dist_breakout_thr >>=
  2916. 8 - (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
  2917. rate_breakout_thr *= num_pels_log2_lookup[bsize];
  2918. best_rdc = this_rdc;
  2919. if (bsize >= BLOCK_8X8) pc_tree->partitioning = PARTITION_NONE;
  2920. if (!x->e_mbd.lossless && this_rdc.rate < rate_breakout_thr &&
  2921. this_rdc.dist < dist_breakout_thr) {
  2922. do_split = 0;
  2923. do_rect = 0;
  2924. }
  2925. }
  2926. }
  2927. }
  2928. // store estimated motion vector
  2929. store_pred_mv(x, ctx);
  2930. // PARTITION_SPLIT
  2931. if (do_split) {
  2932. int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
  2933. sum_rdc.rate += cpi->partition_cost[pl][PARTITION_SPLIT];
  2934. sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
  2935. subsize = get_subsize(bsize, PARTITION_SPLIT);
  2936. for (i = 0; i < 4 && sum_rdc.rdcost < best_rdc.rdcost; ++i) {
  2937. const int x_idx = (i & 1) * ms;
  2938. const int y_idx = (i >> 1) * ms;
  2939. if (mi_row + y_idx >= cm->mi_rows || mi_col + x_idx >= cm->mi_cols)
  2940. continue;
  2941. load_pred_mv(x, ctx);
  2942. nonrd_pick_partition(cpi, td, tile_data, tp, mi_row + y_idx,
  2943. mi_col + x_idx, subsize, &this_rdc, 0,
  2944. best_rdc.rdcost - sum_rdc.rdcost, pc_tree->split[i]);
  2945. if (this_rdc.rate == INT_MAX) {
  2946. vp9_rd_cost_reset(&sum_rdc);
  2947. } else {
  2948. sum_rdc.rate += this_rdc.rate;
  2949. sum_rdc.dist += this_rdc.dist;
  2950. sum_rdc.rdcost += this_rdc.rdcost;
  2951. }
  2952. }
  2953. if (sum_rdc.rdcost < best_rdc.rdcost) {
  2954. best_rdc = sum_rdc;
  2955. pc_tree->partitioning = PARTITION_SPLIT;
  2956. } else {
  2957. // skip rectangular partition test when larger block size
  2958. // gives better rd cost
  2959. if (sf->less_rectangular_check) do_rect &= !partition_none_allowed;
  2960. }
  2961. }
  2962. // PARTITION_HORZ
  2963. if (partition_horz_allowed && do_rect) {
  2964. subsize = get_subsize(bsize, PARTITION_HORZ);
  2965. if (sf->adaptive_motion_search) load_pred_mv(x, ctx);
  2966. pc_tree->horizontal[0].pred_pixel_ready = 1;
  2967. nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
  2968. &pc_tree->horizontal[0]);
  2969. pc_tree->horizontal[0].mic = *xd->mi[0];
  2970. pc_tree->horizontal[0].mbmi_ext = *x->mbmi_ext;
  2971. pc_tree->horizontal[0].skip_txfm[0] = x->skip_txfm[0];
  2972. pc_tree->horizontal[0].skip = x->skip;
  2973. if (sum_rdc.rdcost < best_rdc.rdcost && mi_row + ms < cm->mi_rows) {
  2974. load_pred_mv(x, ctx);
  2975. pc_tree->horizontal[1].pred_pixel_ready = 1;
  2976. nonrd_pick_sb_modes(cpi, tile_data, x, mi_row + ms, mi_col, &this_rdc,
  2977. subsize, &pc_tree->horizontal[1]);
  2978. pc_tree->horizontal[1].mic = *xd->mi[0];
  2979. pc_tree->horizontal[1].mbmi_ext = *x->mbmi_ext;
  2980. pc_tree->horizontal[1].skip_txfm[0] = x->skip_txfm[0];
  2981. pc_tree->horizontal[1].skip = x->skip;
  2982. if (this_rdc.rate == INT_MAX) {
  2983. vp9_rd_cost_reset(&sum_rdc);
  2984. } else {
  2985. int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
  2986. this_rdc.rate += cpi->partition_cost[pl][PARTITION_HORZ];
  2987. sum_rdc.rate += this_rdc.rate;
  2988. sum_rdc.dist += this_rdc.dist;
  2989. sum_rdc.rdcost =
  2990. RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
  2991. }
  2992. }
  2993. if (sum_rdc.rdcost < best_rdc.rdcost) {
  2994. best_rdc = sum_rdc;
  2995. pc_tree->partitioning = PARTITION_HORZ;
  2996. } else {
  2997. pred_pixel_ready_reset(pc_tree, bsize);
  2998. }
  2999. }
  3000. // PARTITION_VERT
  3001. if (partition_vert_allowed && do_rect) {
  3002. subsize = get_subsize(bsize, PARTITION_VERT);
  3003. if (sf->adaptive_motion_search) load_pred_mv(x, ctx);
  3004. pc_tree->vertical[0].pred_pixel_ready = 1;
  3005. nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
  3006. &pc_tree->vertical[0]);
  3007. pc_tree->vertical[0].mic = *xd->mi[0];
  3008. pc_tree->vertical[0].mbmi_ext = *x->mbmi_ext;
  3009. pc_tree->vertical[0].skip_txfm[0] = x->skip_txfm[0];
  3010. pc_tree->vertical[0].skip = x->skip;
  3011. if (sum_rdc.rdcost < best_rdc.rdcost && mi_col + ms < cm->mi_cols) {
  3012. load_pred_mv(x, ctx);
  3013. pc_tree->vertical[1].pred_pixel_ready = 1;
  3014. nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + ms, &this_rdc,
  3015. subsize, &pc_tree->vertical[1]);
  3016. pc_tree->vertical[1].mic = *xd->mi[0];
  3017. pc_tree->vertical[1].mbmi_ext = *x->mbmi_ext;
  3018. pc_tree->vertical[1].skip_txfm[0] = x->skip_txfm[0];
  3019. pc_tree->vertical[1].skip = x->skip;
  3020. if (this_rdc.rate == INT_MAX) {
  3021. vp9_rd_cost_reset(&sum_rdc);
  3022. } else {
  3023. int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
  3024. sum_rdc.rate += cpi->partition_cost[pl][PARTITION_VERT];
  3025. sum_rdc.rate += this_rdc.rate;
  3026. sum_rdc.dist += this_rdc.dist;
  3027. sum_rdc.rdcost =
  3028. RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
  3029. }
  3030. }
  3031. if (sum_rdc.rdcost < best_rdc.rdcost) {
  3032. best_rdc = sum_rdc;
  3033. pc_tree->partitioning = PARTITION_VERT;
  3034. } else {
  3035. pred_pixel_ready_reset(pc_tree, bsize);
  3036. }
  3037. }
  3038. *rd_cost = best_rdc;
  3039. if (best_rdc.rate == INT_MAX) {
  3040. vp9_rd_cost_reset(rd_cost);
  3041. return;
  3042. }
  3043. // update mode info array
  3044. fill_mode_info_sb(cm, x, mi_row, mi_col, bsize, pc_tree);
  3045. if (best_rdc.rate < INT_MAX && best_rdc.dist < INT64_MAX && do_recon) {
  3046. int output_enabled = (bsize == BLOCK_64X64);
  3047. encode_sb_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled, bsize,
  3048. pc_tree);
  3049. }
  3050. if (bsize == BLOCK_64X64 && do_recon) {
  3051. assert(tp_orig < *tp);
  3052. assert(best_rdc.rate < INT_MAX);
  3053. assert(best_rdc.dist < INT64_MAX);
  3054. } else {
  3055. assert(tp_orig == *tp);
  3056. }
  3057. }
  3058. static void nonrd_select_partition(VP9_COMP *cpi, ThreadData *td,
  3059. TileDataEnc *tile_data, MODE_INFO **mi,
  3060. TOKENEXTRA **tp, int mi_row, int mi_col,
  3061. BLOCK_SIZE bsize, int output_enabled,
  3062. RD_COST *rd_cost, PC_TREE *pc_tree) {
  3063. VP9_COMMON *const cm = &cpi->common;
  3064. TileInfo *const tile_info = &tile_data->tile_info;
  3065. MACROBLOCK *const x = &td->mb;
  3066. MACROBLOCKD *const xd = &x->e_mbd;
  3067. const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
  3068. const int mis = cm->mi_stride;
  3069. PARTITION_TYPE partition;
  3070. BLOCK_SIZE subsize;
  3071. RD_COST this_rdc;
  3072. vp9_rd_cost_reset(&this_rdc);
  3073. if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
  3074. subsize = (bsize >= BLOCK_8X8) ? mi[0]->sb_type : BLOCK_4X4;
  3075. partition = partition_lookup[bsl][subsize];
  3076. if (bsize == BLOCK_32X32 && subsize == BLOCK_32X32) {
  3077. x->max_partition_size = BLOCK_32X32;
  3078. x->min_partition_size = BLOCK_16X16;
  3079. nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, bsize, rd_cost,
  3080. 0, INT64_MAX, pc_tree);
  3081. } else if (bsize == BLOCK_32X32 && partition != PARTITION_NONE &&
  3082. subsize >= BLOCK_16X16) {
  3083. x->max_partition_size = BLOCK_32X32;
  3084. x->min_partition_size = BLOCK_8X8;
  3085. nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, bsize, rd_cost,
  3086. 0, INT64_MAX, pc_tree);
  3087. } else if (bsize == BLOCK_16X16 && partition != PARTITION_NONE) {
  3088. x->max_partition_size = BLOCK_16X16;
  3089. x->min_partition_size = BLOCK_8X8;
  3090. nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, bsize, rd_cost,
  3091. 0, INT64_MAX, pc_tree);
  3092. } else {
  3093. switch (partition) {
  3094. case PARTITION_NONE:
  3095. pc_tree->none.pred_pixel_ready = 1;
  3096. nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, rd_cost, subsize,
  3097. &pc_tree->none);
  3098. pc_tree->none.mic = *xd->mi[0];
  3099. pc_tree->none.mbmi_ext = *x->mbmi_ext;
  3100. pc_tree->none.skip_txfm[0] = x->skip_txfm[0];
  3101. pc_tree->none.skip = x->skip;
  3102. break;
  3103. case PARTITION_VERT:
  3104. pc_tree->vertical[0].pred_pixel_ready = 1;
  3105. nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, rd_cost, subsize,
  3106. &pc_tree->vertical[0]);
  3107. pc_tree->vertical[0].mic = *xd->mi[0];
  3108. pc_tree->vertical[0].mbmi_ext = *x->mbmi_ext;
  3109. pc_tree->vertical[0].skip_txfm[0] = x->skip_txfm[0];
  3110. pc_tree->vertical[0].skip = x->skip;
  3111. if (mi_col + hbs < cm->mi_cols) {
  3112. pc_tree->vertical[1].pred_pixel_ready = 1;
  3113. nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + hbs,
  3114. &this_rdc, subsize, &pc_tree->vertical[1]);
  3115. pc_tree->vertical[1].mic = *xd->mi[0];
  3116. pc_tree->vertical[1].mbmi_ext = *x->mbmi_ext;
  3117. pc_tree->vertical[1].skip_txfm[0] = x->skip_txfm[0];
  3118. pc_tree->vertical[1].skip = x->skip;
  3119. if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
  3120. rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
  3121. rd_cost->rate += this_rdc.rate;
  3122. rd_cost->dist += this_rdc.dist;
  3123. }
  3124. }
  3125. break;
  3126. case PARTITION_HORZ:
  3127. pc_tree->horizontal[0].pred_pixel_ready = 1;
  3128. nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, rd_cost, subsize,
  3129. &pc_tree->horizontal[0]);
  3130. pc_tree->horizontal[0].mic = *xd->mi[0];
  3131. pc_tree->horizontal[0].mbmi_ext = *x->mbmi_ext;
  3132. pc_tree->horizontal[0].skip_txfm[0] = x->skip_txfm[0];
  3133. pc_tree->horizontal[0].skip = x->skip;
  3134. if (mi_row + hbs < cm->mi_rows) {
  3135. pc_tree->horizontal[1].pred_pixel_ready = 1;
  3136. nonrd_pick_sb_modes(cpi, tile_data, x, mi_row + hbs, mi_col,
  3137. &this_rdc, subsize, &pc_tree->horizontal[1]);
  3138. pc_tree->horizontal[1].mic = *xd->mi[0];
  3139. pc_tree->horizontal[1].mbmi_ext = *x->mbmi_ext;
  3140. pc_tree->horizontal[1].skip_txfm[0] = x->skip_txfm[0];
  3141. pc_tree->horizontal[1].skip = x->skip;
  3142. if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
  3143. rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
  3144. rd_cost->rate += this_rdc.rate;
  3145. rd_cost->dist += this_rdc.dist;
  3146. }
  3147. }
  3148. break;
  3149. case PARTITION_SPLIT:
  3150. subsize = get_subsize(bsize, PARTITION_SPLIT);
  3151. nonrd_select_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
  3152. subsize, output_enabled, rd_cost,
  3153. pc_tree->split[0]);
  3154. nonrd_select_partition(cpi, td, tile_data, mi + hbs, tp, mi_row,
  3155. mi_col + hbs, subsize, output_enabled, &this_rdc,
  3156. pc_tree->split[1]);
  3157. if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
  3158. rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
  3159. rd_cost->rate += this_rdc.rate;
  3160. rd_cost->dist += this_rdc.dist;
  3161. }
  3162. nonrd_select_partition(cpi, td, tile_data, mi + hbs * mis, tp,
  3163. mi_row + hbs, mi_col, subsize, output_enabled,
  3164. &this_rdc, pc_tree->split[2]);
  3165. if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
  3166. rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
  3167. rd_cost->rate += this_rdc.rate;
  3168. rd_cost->dist += this_rdc.dist;
  3169. }
  3170. nonrd_select_partition(cpi, td, tile_data, mi + hbs * mis + hbs, tp,
  3171. mi_row + hbs, mi_col + hbs, subsize,
  3172. output_enabled, &this_rdc, pc_tree->split[3]);
  3173. if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
  3174. rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
  3175. rd_cost->rate += this_rdc.rate;
  3176. rd_cost->dist += this_rdc.dist;
  3177. }
  3178. break;
  3179. default: assert(0 && "Invalid partition type."); break;
  3180. }
  3181. }
  3182. if (bsize == BLOCK_64X64 && output_enabled)
  3183. encode_sb_rt(cpi, td, tile_info, tp, mi_row, mi_col, 1, bsize, pc_tree);
  3184. }
  3185. static void nonrd_use_partition(VP9_COMP *cpi, ThreadData *td,
  3186. TileDataEnc *tile_data, MODE_INFO **mi,
  3187. TOKENEXTRA **tp, int mi_row, int mi_col,
  3188. BLOCK_SIZE bsize, int output_enabled,
  3189. RD_COST *dummy_cost, PC_TREE *pc_tree) {
  3190. VP9_COMMON *const cm = &cpi->common;
  3191. TileInfo *tile_info = &tile_data->tile_info;
  3192. MACROBLOCK *const x = &td->mb;
  3193. MACROBLOCKD *const xd = &x->e_mbd;
  3194. const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
  3195. const int mis = cm->mi_stride;
  3196. PARTITION_TYPE partition;
  3197. BLOCK_SIZE subsize;
  3198. if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
  3199. subsize = (bsize >= BLOCK_8X8) ? mi[0]->sb_type : BLOCK_4X4;
  3200. partition = partition_lookup[bsl][subsize];
  3201. if (output_enabled && bsize != BLOCK_4X4) {
  3202. int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
  3203. td->counts->partition[ctx][partition]++;
  3204. }
  3205. switch (partition) {
  3206. case PARTITION_NONE:
  3207. pc_tree->none.pred_pixel_ready = 1;
  3208. nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
  3209. subsize, &pc_tree->none);
  3210. pc_tree->none.mic = *xd->mi[0];
  3211. pc_tree->none.mbmi_ext = *x->mbmi_ext;
  3212. pc_tree->none.skip_txfm[0] = x->skip_txfm[0];
  3213. pc_tree->none.skip = x->skip;
  3214. encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
  3215. subsize, &pc_tree->none);
  3216. break;
  3217. case PARTITION_VERT:
  3218. pc_tree->vertical[0].pred_pixel_ready = 1;
  3219. nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
  3220. subsize, &pc_tree->vertical[0]);
  3221. pc_tree->vertical[0].mic = *xd->mi[0];
  3222. pc_tree->vertical[0].mbmi_ext = *x->mbmi_ext;
  3223. pc_tree->vertical[0].skip_txfm[0] = x->skip_txfm[0];
  3224. pc_tree->vertical[0].skip = x->skip;
  3225. encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
  3226. subsize, &pc_tree->vertical[0]);
  3227. if (mi_col + hbs < cm->mi_cols && bsize > BLOCK_8X8) {
  3228. pc_tree->vertical[1].pred_pixel_ready = 1;
  3229. nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + hbs, dummy_cost,
  3230. subsize, &pc_tree->vertical[1]);
  3231. pc_tree->vertical[1].mic = *xd->mi[0];
  3232. pc_tree->vertical[1].mbmi_ext = *x->mbmi_ext;
  3233. pc_tree->vertical[1].skip_txfm[0] = x->skip_txfm[0];
  3234. pc_tree->vertical[1].skip = x->skip;
  3235. encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col + hbs,
  3236. output_enabled, subsize, &pc_tree->vertical[1]);
  3237. }
  3238. break;
  3239. case PARTITION_HORZ:
  3240. pc_tree->horizontal[0].pred_pixel_ready = 1;
  3241. nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
  3242. subsize, &pc_tree->horizontal[0]);
  3243. pc_tree->horizontal[0].mic = *xd->mi[0];
  3244. pc_tree->horizontal[0].mbmi_ext = *x->mbmi_ext;
  3245. pc_tree->horizontal[0].skip_txfm[0] = x->skip_txfm[0];
  3246. pc_tree->horizontal[0].skip = x->skip;
  3247. encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
  3248. subsize, &pc_tree->horizontal[0]);
  3249. if (mi_row + hbs < cm->mi_rows && bsize > BLOCK_8X8) {
  3250. pc_tree->horizontal[1].pred_pixel_ready = 1;
  3251. nonrd_pick_sb_modes(cpi, tile_data, x, mi_row + hbs, mi_col, dummy_cost,
  3252. subsize, &pc_tree->horizontal[1]);
  3253. pc_tree->horizontal[1].mic = *xd->mi[0];
  3254. pc_tree->horizontal[1].mbmi_ext = *x->mbmi_ext;
  3255. pc_tree->horizontal[1].skip_txfm[0] = x->skip_txfm[0];
  3256. pc_tree->horizontal[1].skip = x->skip;
  3257. encode_b_rt(cpi, td, tile_info, tp, mi_row + hbs, mi_col,
  3258. output_enabled, subsize, &pc_tree->horizontal[1]);
  3259. }
  3260. break;
  3261. case PARTITION_SPLIT:
  3262. subsize = get_subsize(bsize, PARTITION_SPLIT);
  3263. if (bsize == BLOCK_8X8) {
  3264. nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
  3265. subsize, pc_tree->leaf_split[0]);
  3266. encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
  3267. subsize, pc_tree->leaf_split[0]);
  3268. } else {
  3269. nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col, subsize,
  3270. output_enabled, dummy_cost, pc_tree->split[0]);
  3271. nonrd_use_partition(cpi, td, tile_data, mi + hbs, tp, mi_row,
  3272. mi_col + hbs, subsize, output_enabled, dummy_cost,
  3273. pc_tree->split[1]);
  3274. nonrd_use_partition(cpi, td, tile_data, mi + hbs * mis, tp,
  3275. mi_row + hbs, mi_col, subsize, output_enabled,
  3276. dummy_cost, pc_tree->split[2]);
  3277. nonrd_use_partition(cpi, td, tile_data, mi + hbs * mis + hbs, tp,
  3278. mi_row + hbs, mi_col + hbs, subsize, output_enabled,
  3279. dummy_cost, pc_tree->split[3]);
  3280. }
  3281. break;
  3282. default: assert(0 && "Invalid partition type."); break;
  3283. }
  3284. if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
  3285. update_partition_context(xd, mi_row, mi_col, subsize, bsize);
  3286. }
  3287. static void encode_nonrd_sb_row(VP9_COMP *cpi, ThreadData *td,
  3288. TileDataEnc *tile_data, int mi_row,
  3289. TOKENEXTRA **tp) {
  3290. SPEED_FEATURES *const sf = &cpi->sf;
  3291. VP9_COMMON *const cm = &cpi->common;
  3292. TileInfo *const tile_info = &tile_data->tile_info;
  3293. MACROBLOCK *const x = &td->mb;
  3294. MACROBLOCKD *const xd = &x->e_mbd;
  3295. const int mi_col_start = tile_info->mi_col_start;
  3296. const int mi_col_end = tile_info->mi_col_end;
  3297. int mi_col;
  3298. // Initialize the left context for the new SB row
  3299. memset(&xd->left_context, 0, sizeof(xd->left_context));
  3300. memset(xd->left_seg_context, 0, sizeof(xd->left_seg_context));
  3301. // Code each SB in the row
  3302. for (mi_col = mi_col_start; mi_col < mi_col_end; mi_col += MI_BLOCK_SIZE) {
  3303. const struct segmentation *const seg = &cm->seg;
  3304. RD_COST dummy_rdc;
  3305. const int idx_str = cm->mi_stride * mi_row + mi_col;
  3306. MODE_INFO **mi = cm->mi_grid_visible + idx_str;
  3307. PARTITION_SEARCH_TYPE partition_search_type = sf->partition_search_type;
  3308. BLOCK_SIZE bsize = BLOCK_64X64;
  3309. int seg_skip = 0;
  3310. x->source_variance = UINT_MAX;
  3311. vp9_zero(x->pred_mv);
  3312. vp9_rd_cost_init(&dummy_rdc);
  3313. x->color_sensitivity[0] = 0;
  3314. x->color_sensitivity[1] = 0;
  3315. x->sb_is_skin = 0;
  3316. if (seg->enabled) {
  3317. const uint8_t *const map =
  3318. seg->update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
  3319. int segment_id = get_segment_id(cm, map, BLOCK_64X64, mi_row, mi_col);
  3320. seg_skip = segfeature_active(seg, segment_id, SEG_LVL_SKIP);
  3321. if (seg_skip) {
  3322. partition_search_type = FIXED_PARTITION;
  3323. }
  3324. }
  3325. // Set the partition type of the 64X64 block
  3326. switch (partition_search_type) {
  3327. case VAR_BASED_PARTITION:
  3328. // TODO(jingning, marpan): The mode decision and encoding process
  3329. // support both intra and inter sub8x8 block coding for RTC mode.
  3330. // Tune the thresholds accordingly to use sub8x8 block coding for
  3331. // coding performance improvement.
  3332. choose_partitioning(cpi, tile_info, x, mi_row, mi_col);
  3333. nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
  3334. BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
  3335. break;
  3336. case SOURCE_VAR_BASED_PARTITION:
  3337. set_source_var_based_partition(cpi, tile_info, x, mi, mi_row, mi_col);
  3338. nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
  3339. BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
  3340. break;
  3341. case FIXED_PARTITION:
  3342. if (!seg_skip) bsize = sf->always_this_block_size;
  3343. set_fixed_partitioning(cpi, tile_info, mi, mi_row, mi_col, bsize);
  3344. nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
  3345. BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
  3346. break;
  3347. case REFERENCE_PARTITION:
  3348. set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
  3349. // Use nonrd_pick_partition on scene-cut for VBR, or on qp-segment
  3350. // if cyclic_refresh is enabled.
  3351. // nonrd_pick_partition does not support 4x4 partition, so avoid it
  3352. // on key frame for now.
  3353. if ((cpi->oxcf.rc_mode == VPX_VBR && cpi->rc.high_source_sad &&
  3354. cm->frame_type != KEY_FRAME) ||
  3355. (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
  3356. xd->mi[0]->segment_id)) {
  3357. // Use lower max_partition_size for low resoultions.
  3358. if (cm->width <= 352 && cm->height <= 288)
  3359. x->max_partition_size = BLOCK_32X32;
  3360. else
  3361. x->max_partition_size = BLOCK_64X64;
  3362. x->min_partition_size = BLOCK_8X8;
  3363. nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col,
  3364. BLOCK_64X64, &dummy_rdc, 1, INT64_MAX,
  3365. td->pc_root);
  3366. } else {
  3367. choose_partitioning(cpi, tile_info, x, mi_row, mi_col);
  3368. // TODO(marpan): Seems like nonrd_select_partition does not support
  3369. // 4x4 partition. Since 4x4 is used on key frame, use this switch
  3370. // for now.
  3371. if (cm->frame_type == KEY_FRAME)
  3372. nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
  3373. BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
  3374. else
  3375. nonrd_select_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
  3376. BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
  3377. }
  3378. break;
  3379. default: assert(0); break;
  3380. }
  3381. }
  3382. }
  3383. // end RTC play code
  3384. static int set_var_thresh_from_histogram(VP9_COMP *cpi) {
  3385. const SPEED_FEATURES *const sf = &cpi->sf;
  3386. const VP9_COMMON *const cm = &cpi->common;
  3387. const uint8_t *src = cpi->Source->y_buffer;
  3388. const uint8_t *last_src = cpi->Last_Source->y_buffer;
  3389. const int src_stride = cpi->Source->y_stride;
  3390. const int last_stride = cpi->Last_Source->y_stride;
  3391. // Pick cutoff threshold
  3392. const int cutoff = (VPXMIN(cm->width, cm->height) >= 720)
  3393. ? (cm->MBs * VAR_HIST_LARGE_CUT_OFF / 100)
  3394. : (cm->MBs * VAR_HIST_SMALL_CUT_OFF / 100);
  3395. DECLARE_ALIGNED(16, int, hist[VAR_HIST_BINS]);
  3396. diff *var16 = cpi->source_diff_var;
  3397. int sum = 0;
  3398. int i, j;
  3399. memset(hist, 0, VAR_HIST_BINS * sizeof(hist[0]));
  3400. for (i = 0; i < cm->mb_rows; i++) {
  3401. for (j = 0; j < cm->mb_cols; j++) {
  3402. #if CONFIG_VP9_HIGHBITDEPTH
  3403. if (cm->use_highbitdepth) {
  3404. switch (cm->bit_depth) {
  3405. case VPX_BITS_8:
  3406. vpx_highbd_8_get16x16var(src, src_stride, last_src, last_stride,
  3407. &var16->sse, &var16->sum);
  3408. break;
  3409. case VPX_BITS_10:
  3410. vpx_highbd_10_get16x16var(src, src_stride, last_src, last_stride,
  3411. &var16->sse, &var16->sum);
  3412. break;
  3413. case VPX_BITS_12:
  3414. vpx_highbd_12_get16x16var(src, src_stride, last_src, last_stride,
  3415. &var16->sse, &var16->sum);
  3416. break;
  3417. default:
  3418. assert(0 &&
  3419. "cm->bit_depth should be VPX_BITS_8, VPX_BITS_10"
  3420. " or VPX_BITS_12");
  3421. return -1;
  3422. }
  3423. } else {
  3424. vpx_get16x16var(src, src_stride, last_src, last_stride, &var16->sse,
  3425. &var16->sum);
  3426. }
  3427. #else
  3428. vpx_get16x16var(src, src_stride, last_src, last_stride, &var16->sse,
  3429. &var16->sum);
  3430. #endif // CONFIG_VP9_HIGHBITDEPTH
  3431. var16->var = var16->sse - (((uint32_t)var16->sum * var16->sum) >> 8);
  3432. if (var16->var >= VAR_HIST_MAX_BG_VAR)
  3433. hist[VAR_HIST_BINS - 1]++;
  3434. else
  3435. hist[var16->var / VAR_HIST_FACTOR]++;
  3436. src += 16;
  3437. last_src += 16;
  3438. var16++;
  3439. }
  3440. src = src - cm->mb_cols * 16 + 16 * src_stride;
  3441. last_src = last_src - cm->mb_cols * 16 + 16 * last_stride;
  3442. }
  3443. cpi->source_var_thresh = 0;
  3444. if (hist[VAR_HIST_BINS - 1] < cutoff) {
  3445. for (i = 0; i < VAR_HIST_BINS - 1; i++) {
  3446. sum += hist[i];
  3447. if (sum > cutoff) {
  3448. cpi->source_var_thresh = (i + 1) * VAR_HIST_FACTOR;
  3449. return 0;
  3450. }
  3451. }
  3452. }
  3453. return sf->search_type_check_frequency;
  3454. }
  3455. static void source_var_based_partition_search_method(VP9_COMP *cpi) {
  3456. VP9_COMMON *const cm = &cpi->common;
  3457. SPEED_FEATURES *const sf = &cpi->sf;
  3458. if (cm->frame_type == KEY_FRAME) {
  3459. // For key frame, use SEARCH_PARTITION.
  3460. sf->partition_search_type = SEARCH_PARTITION;
  3461. } else if (cm->intra_only) {
  3462. sf->partition_search_type = FIXED_PARTITION;
  3463. } else {
  3464. if (cm->last_width != cm->width || cm->last_height != cm->height) {
  3465. if (cpi->source_diff_var) vpx_free(cpi->source_diff_var);
  3466. CHECK_MEM_ERROR(cm, cpi->source_diff_var,
  3467. vpx_calloc(cm->MBs, sizeof(diff)));
  3468. }
  3469. if (!cpi->frames_till_next_var_check)
  3470. cpi->frames_till_next_var_check = set_var_thresh_from_histogram(cpi);
  3471. if (cpi->frames_till_next_var_check > 0) {
  3472. sf->partition_search_type = FIXED_PARTITION;
  3473. cpi->frames_till_next_var_check--;
  3474. }
  3475. }
  3476. }
  3477. static int get_skip_encode_frame(const VP9_COMMON *cm, ThreadData *const td) {
  3478. unsigned int intra_count = 0, inter_count = 0;
  3479. int j;
  3480. for (j = 0; j < INTRA_INTER_CONTEXTS; ++j) {
  3481. intra_count += td->counts->intra_inter[j][0];
  3482. inter_count += td->counts->intra_inter[j][1];
  3483. }
  3484. return (intra_count << 2) < inter_count && cm->frame_type != KEY_FRAME &&
  3485. cm->show_frame;
  3486. }
  3487. void vp9_init_tile_data(VP9_COMP *cpi) {
  3488. VP9_COMMON *const cm = &cpi->common;
  3489. const int tile_cols = 1 << cm->log2_tile_cols;
  3490. const int tile_rows = 1 << cm->log2_tile_rows;
  3491. int tile_col, tile_row;
  3492. TOKENEXTRA *pre_tok = cpi->tile_tok[0][0];
  3493. int tile_tok = 0;
  3494. if (cpi->tile_data == NULL || cpi->allocated_tiles < tile_cols * tile_rows) {
  3495. if (cpi->tile_data != NULL) vpx_free(cpi->tile_data);
  3496. CHECK_MEM_ERROR(cm, cpi->tile_data, vpx_malloc(tile_cols * tile_rows *
  3497. sizeof(*cpi->tile_data)));
  3498. cpi->allocated_tiles = tile_cols * tile_rows;
  3499. for (tile_row = 0; tile_row < tile_rows; ++tile_row)
  3500. for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
  3501. TileDataEnc *tile_data =
  3502. &cpi->tile_data[tile_row * tile_cols + tile_col];
  3503. int i, j;
  3504. for (i = 0; i < BLOCK_SIZES; ++i) {
  3505. for (j = 0; j < MAX_MODES; ++j) {
  3506. tile_data->thresh_freq_fact[i][j] = 32;
  3507. tile_data->mode_map[i][j] = j;
  3508. }
  3509. }
  3510. }
  3511. }
  3512. for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
  3513. for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
  3514. TileInfo *tile_info =
  3515. &cpi->tile_data[tile_row * tile_cols + tile_col].tile_info;
  3516. vp9_tile_init(tile_info, cm, tile_row, tile_col);
  3517. cpi->tile_tok[tile_row][tile_col] = pre_tok + tile_tok;
  3518. pre_tok = cpi->tile_tok[tile_row][tile_col];
  3519. tile_tok = allocated_tokens(*tile_info);
  3520. }
  3521. }
  3522. }
  3523. void vp9_encode_tile(VP9_COMP *cpi, ThreadData *td, int tile_row,
  3524. int tile_col) {
  3525. VP9_COMMON *const cm = &cpi->common;
  3526. const int tile_cols = 1 << cm->log2_tile_cols;
  3527. TileDataEnc *this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col];
  3528. const TileInfo *const tile_info = &this_tile->tile_info;
  3529. TOKENEXTRA *tok = cpi->tile_tok[tile_row][tile_col];
  3530. const int mi_row_start = tile_info->mi_row_start;
  3531. const int mi_row_end = tile_info->mi_row_end;
  3532. int mi_row;
  3533. // Set up pointers to per thread motion search counters.
  3534. td->mb.m_search_count_ptr = &td->rd_counts.m_search_count;
  3535. td->mb.ex_search_count_ptr = &td->rd_counts.ex_search_count;
  3536. for (mi_row = mi_row_start; mi_row < mi_row_end; mi_row += MI_BLOCK_SIZE) {
  3537. if (cpi->sf.use_nonrd_pick_mode)
  3538. encode_nonrd_sb_row(cpi, td, this_tile, mi_row, &tok);
  3539. else
  3540. encode_rd_sb_row(cpi, td, this_tile, mi_row, &tok);
  3541. }
  3542. cpi->tok_count[tile_row][tile_col] =
  3543. (unsigned int)(tok - cpi->tile_tok[tile_row][tile_col]);
  3544. assert(tok - cpi->tile_tok[tile_row][tile_col] <=
  3545. allocated_tokens(*tile_info));
  3546. }
  3547. static void encode_tiles(VP9_COMP *cpi) {
  3548. VP9_COMMON *const cm = &cpi->common;
  3549. const int tile_cols = 1 << cm->log2_tile_cols;
  3550. const int tile_rows = 1 << cm->log2_tile_rows;
  3551. int tile_col, tile_row;
  3552. vp9_init_tile_data(cpi);
  3553. for (tile_row = 0; tile_row < tile_rows; ++tile_row)
  3554. for (tile_col = 0; tile_col < tile_cols; ++tile_col)
  3555. vp9_encode_tile(cpi, &cpi->td, tile_row, tile_col);
  3556. }
  3557. #if CONFIG_FP_MB_STATS
  3558. static int input_fpmb_stats(FIRSTPASS_MB_STATS *firstpass_mb_stats,
  3559. VP9_COMMON *cm, uint8_t **this_frame_mb_stats) {
  3560. uint8_t *mb_stats_in = firstpass_mb_stats->mb_stats_start +
  3561. cm->current_video_frame * cm->MBs * sizeof(uint8_t);
  3562. if (mb_stats_in > firstpass_mb_stats->mb_stats_end) return EOF;
  3563. *this_frame_mb_stats = mb_stats_in;
  3564. return 1;
  3565. }
  3566. #endif
  3567. static void encode_frame_internal(VP9_COMP *cpi) {
  3568. SPEED_FEATURES *const sf = &cpi->sf;
  3569. ThreadData *const td = &cpi->td;
  3570. MACROBLOCK *const x = &td->mb;
  3571. VP9_COMMON *const cm = &cpi->common;
  3572. MACROBLOCKD *const xd = &x->e_mbd;
  3573. xd->mi = cm->mi_grid_visible;
  3574. xd->mi[0] = cm->mi;
  3575. vp9_zero(*td->counts);
  3576. vp9_zero(cpi->td.rd_counts);
  3577. xd->lossless = cm->base_qindex == 0 && cm->y_dc_delta_q == 0 &&
  3578. cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
  3579. #if CONFIG_VP9_HIGHBITDEPTH
  3580. if (cm->use_highbitdepth)
  3581. x->fwd_txm4x4 = xd->lossless ? vp9_highbd_fwht4x4 : vpx_highbd_fdct4x4;
  3582. else
  3583. x->fwd_txm4x4 = xd->lossless ? vp9_fwht4x4 : vpx_fdct4x4;
  3584. x->highbd_itxm_add =
  3585. xd->lossless ? vp9_highbd_iwht4x4_add : vp9_highbd_idct4x4_add;
  3586. #else
  3587. x->fwd_txm4x4 = xd->lossless ? vp9_fwht4x4 : vpx_fdct4x4;
  3588. #endif // CONFIG_VP9_HIGHBITDEPTH
  3589. x->itxm_add = xd->lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
  3590. if (xd->lossless) x->optimize = 0;
  3591. cm->tx_mode = select_tx_mode(cpi, xd);
  3592. vp9_frame_init_quantizer(cpi);
  3593. vp9_initialize_rd_consts(cpi);
  3594. vp9_initialize_me_consts(cpi, x, cm->base_qindex);
  3595. init_encode_frame_mb_context(cpi);
  3596. cm->use_prev_frame_mvs =
  3597. !cm->error_resilient_mode && cm->width == cm->last_width &&
  3598. cm->height == cm->last_height && !cm->intra_only && cm->last_show_frame;
  3599. // Special case: set prev_mi to NULL when the previous mode info
  3600. // context cannot be used.
  3601. cm->prev_mi =
  3602. cm->use_prev_frame_mvs ? cm->prev_mip + cm->mi_stride + 1 : NULL;
  3603. x->quant_fp = cpi->sf.use_quant_fp;
  3604. vp9_zero(x->skip_txfm);
  3605. if (sf->use_nonrd_pick_mode) {
  3606. // Initialize internal buffer pointers for rtc coding, where non-RD
  3607. // mode decision is used and hence no buffer pointer swap needed.
  3608. int i;
  3609. struct macroblock_plane *const p = x->plane;
  3610. struct macroblockd_plane *const pd = xd->plane;
  3611. PICK_MODE_CONTEXT *ctx = &cpi->td.pc_root->none;
  3612. for (i = 0; i < MAX_MB_PLANE; ++i) {
  3613. p[i].coeff = ctx->coeff_pbuf[i][0];
  3614. p[i].qcoeff = ctx->qcoeff_pbuf[i][0];
  3615. pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][0];
  3616. p[i].eobs = ctx->eobs_pbuf[i][0];
  3617. }
  3618. vp9_zero(x->zcoeff_blk);
  3619. if (cm->frame_type != KEY_FRAME && cpi->rc.frames_since_golden == 0 &&
  3620. !cpi->use_svc)
  3621. cpi->ref_frame_flags &= (~VP9_GOLD_FLAG);
  3622. if (sf->partition_search_type == SOURCE_VAR_BASED_PARTITION)
  3623. source_var_based_partition_search_method(cpi);
  3624. }
  3625. {
  3626. struct vpx_usec_timer emr_timer;
  3627. vpx_usec_timer_start(&emr_timer);
  3628. #if CONFIG_FP_MB_STATS
  3629. if (cpi->use_fp_mb_stats) {
  3630. input_fpmb_stats(&cpi->twopass.firstpass_mb_stats, cm,
  3631. &cpi->twopass.this_frame_mb_stats);
  3632. }
  3633. #endif
  3634. // If allowed, encoding tiles in parallel with one thread handling one tile.
  3635. if (VPXMIN(cpi->oxcf.max_threads, 1 << cm->log2_tile_cols) > 1)
  3636. vp9_encode_tiles_mt(cpi);
  3637. else
  3638. encode_tiles(cpi);
  3639. vpx_usec_timer_mark(&emr_timer);
  3640. cpi->time_encode_sb_row += vpx_usec_timer_elapsed(&emr_timer);
  3641. }
  3642. sf->skip_encode_frame =
  3643. sf->skip_encode_sb ? get_skip_encode_frame(cm, td) : 0;
  3644. #if 0
  3645. // Keep record of the total distortion this time around for future use
  3646. cpi->last_frame_distortion = cpi->frame_distortion;
  3647. #endif
  3648. }
  3649. static INTERP_FILTER get_interp_filter(
  3650. const int64_t threshes[SWITCHABLE_FILTER_CONTEXTS], int is_alt_ref) {
  3651. if (!is_alt_ref && threshes[EIGHTTAP_SMOOTH] > threshes[EIGHTTAP] &&
  3652. threshes[EIGHTTAP_SMOOTH] > threshes[EIGHTTAP_SHARP] &&
  3653. threshes[EIGHTTAP_SMOOTH] > threshes[SWITCHABLE - 1]) {
  3654. return EIGHTTAP_SMOOTH;
  3655. } else if (threshes[EIGHTTAP_SHARP] > threshes[EIGHTTAP] &&
  3656. threshes[EIGHTTAP_SHARP] > threshes[SWITCHABLE - 1]) {
  3657. return EIGHTTAP_SHARP;
  3658. } else if (threshes[EIGHTTAP] > threshes[SWITCHABLE - 1]) {
  3659. return EIGHTTAP;
  3660. } else {
  3661. return SWITCHABLE;
  3662. }
  3663. }
  3664. static int compute_frame_aq_offset(struct VP9_COMP *cpi) {
  3665. VP9_COMMON *const cm = &cpi->common;
  3666. MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
  3667. struct segmentation *const seg = &cm->seg;
  3668. int mi_row, mi_col;
  3669. int sum_delta = 0;
  3670. int map_index = 0;
  3671. int qdelta_index;
  3672. int segment_id;
  3673. for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
  3674. MODE_INFO **mi_8x8 = mi_8x8_ptr;
  3675. for (mi_col = 0; mi_col < cm->mi_cols; mi_col++, mi_8x8++) {
  3676. segment_id = mi_8x8[0]->segment_id;
  3677. qdelta_index = get_segdata(seg, segment_id, SEG_LVL_ALT_Q);
  3678. sum_delta += qdelta_index;
  3679. map_index++;
  3680. }
  3681. mi_8x8_ptr += cm->mi_stride;
  3682. }
  3683. return sum_delta / (cm->mi_rows * cm->mi_cols);
  3684. }
  3685. void vp9_encode_frame(VP9_COMP *cpi) {
  3686. VP9_COMMON *const cm = &cpi->common;
  3687. // In the longer term the encoder should be generalized to match the
  3688. // decoder such that we allow compound where one of the 3 buffers has a
  3689. // different sign bias and that buffer is then the fixed ref. However, this
  3690. // requires further work in the rd loop. For now the only supported encoder
  3691. // side behavior is where the ALT ref buffer has opposite sign bias to
  3692. // the other two.
  3693. if (!frame_is_intra_only(cm)) {
  3694. if ((cm->ref_frame_sign_bias[ALTREF_FRAME] ==
  3695. cm->ref_frame_sign_bias[GOLDEN_FRAME]) ||
  3696. (cm->ref_frame_sign_bias[ALTREF_FRAME] ==
  3697. cm->ref_frame_sign_bias[LAST_FRAME])) {
  3698. cpi->allow_comp_inter_inter = 0;
  3699. } else {
  3700. cpi->allow_comp_inter_inter = 1;
  3701. cm->comp_fixed_ref = ALTREF_FRAME;
  3702. cm->comp_var_ref[0] = LAST_FRAME;
  3703. cm->comp_var_ref[1] = GOLDEN_FRAME;
  3704. }
  3705. }
  3706. if (cpi->sf.frame_parameter_update) {
  3707. int i;
  3708. RD_OPT *const rd_opt = &cpi->rd;
  3709. FRAME_COUNTS *counts = cpi->td.counts;
  3710. RD_COUNTS *const rdc = &cpi->td.rd_counts;
  3711. // This code does a single RD pass over the whole frame assuming
  3712. // either compound, single or hybrid prediction as per whatever has
  3713. // worked best for that type of frame in the past.
  3714. // It also predicts whether another coding mode would have worked
  3715. // better than this coding mode. If that is the case, it remembers
  3716. // that for subsequent frames.
  3717. // It also does the same analysis for transform size selection.
  3718. const MV_REFERENCE_FRAME frame_type = get_frame_type(cpi);
  3719. int64_t *const mode_thrs = rd_opt->prediction_type_threshes[frame_type];
  3720. int64_t *const filter_thrs = rd_opt->filter_threshes[frame_type];
  3721. const int is_alt_ref = frame_type == ALTREF_FRAME;
  3722. /* prediction (compound, single or hybrid) mode selection */
  3723. if (is_alt_ref || !cpi->allow_comp_inter_inter)
  3724. cm->reference_mode = SINGLE_REFERENCE;
  3725. else if (mode_thrs[COMPOUND_REFERENCE] > mode_thrs[SINGLE_REFERENCE] &&
  3726. mode_thrs[COMPOUND_REFERENCE] > mode_thrs[REFERENCE_MODE_SELECT] &&
  3727. check_dual_ref_flags(cpi) && cpi->static_mb_pct == 100)
  3728. cm->reference_mode = COMPOUND_REFERENCE;
  3729. else if (mode_thrs[SINGLE_REFERENCE] > mode_thrs[REFERENCE_MODE_SELECT])
  3730. cm->reference_mode = SINGLE_REFERENCE;
  3731. else
  3732. cm->reference_mode = REFERENCE_MODE_SELECT;
  3733. if (cm->interp_filter == SWITCHABLE)
  3734. cm->interp_filter = get_interp_filter(filter_thrs, is_alt_ref);
  3735. encode_frame_internal(cpi);
  3736. for (i = 0; i < REFERENCE_MODES; ++i)
  3737. mode_thrs[i] = (mode_thrs[i] + rdc->comp_pred_diff[i] / cm->MBs) / 2;
  3738. for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
  3739. filter_thrs[i] = (filter_thrs[i] + rdc->filter_diff[i] / cm->MBs) / 2;
  3740. if (cm->reference_mode == REFERENCE_MODE_SELECT) {
  3741. int single_count_zero = 0;
  3742. int comp_count_zero = 0;
  3743. for (i = 0; i < COMP_INTER_CONTEXTS; i++) {
  3744. single_count_zero += counts->comp_inter[i][0];
  3745. comp_count_zero += counts->comp_inter[i][1];
  3746. }
  3747. if (comp_count_zero == 0) {
  3748. cm->reference_mode = SINGLE_REFERENCE;
  3749. vp9_zero(counts->comp_inter);
  3750. } else if (single_count_zero == 0) {
  3751. cm->reference_mode = COMPOUND_REFERENCE;
  3752. vp9_zero(counts->comp_inter);
  3753. }
  3754. }
  3755. if (cm->tx_mode == TX_MODE_SELECT) {
  3756. int count4x4 = 0;
  3757. int count8x8_lp = 0, count8x8_8x8p = 0;
  3758. int count16x16_16x16p = 0, count16x16_lp = 0;
  3759. int count32x32 = 0;
  3760. for (i = 0; i < TX_SIZE_CONTEXTS; ++i) {
  3761. count4x4 += counts->tx.p32x32[i][TX_4X4];
  3762. count4x4 += counts->tx.p16x16[i][TX_4X4];
  3763. count4x4 += counts->tx.p8x8[i][TX_4X4];
  3764. count8x8_lp += counts->tx.p32x32[i][TX_8X8];
  3765. count8x8_lp += counts->tx.p16x16[i][TX_8X8];
  3766. count8x8_8x8p += counts->tx.p8x8[i][TX_8X8];
  3767. count16x16_16x16p += counts->tx.p16x16[i][TX_16X16];
  3768. count16x16_lp += counts->tx.p32x32[i][TX_16X16];
  3769. count32x32 += counts->tx.p32x32[i][TX_32X32];
  3770. }
  3771. if (count4x4 == 0 && count16x16_lp == 0 && count16x16_16x16p == 0 &&
  3772. count32x32 == 0) {
  3773. cm->tx_mode = ALLOW_8X8;
  3774. reset_skip_tx_size(cm, TX_8X8);
  3775. } else if (count8x8_8x8p == 0 && count16x16_16x16p == 0 &&
  3776. count8x8_lp == 0 && count16x16_lp == 0 && count32x32 == 0) {
  3777. cm->tx_mode = ONLY_4X4;
  3778. reset_skip_tx_size(cm, TX_4X4);
  3779. } else if (count8x8_lp == 0 && count16x16_lp == 0 && count4x4 == 0) {
  3780. cm->tx_mode = ALLOW_32X32;
  3781. } else if (count32x32 == 0 && count8x8_lp == 0 && count4x4 == 0) {
  3782. cm->tx_mode = ALLOW_16X16;
  3783. reset_skip_tx_size(cm, TX_16X16);
  3784. }
  3785. }
  3786. } else {
  3787. cm->reference_mode = SINGLE_REFERENCE;
  3788. encode_frame_internal(cpi);
  3789. }
  3790. // If segmented AQ is enabled compute the average AQ weighting.
  3791. if (cm->seg.enabled && (cpi->oxcf.aq_mode != NO_AQ) &&
  3792. (cm->seg.update_map || cm->seg.update_data)) {
  3793. cm->seg.aq_av_offset = compute_frame_aq_offset(cpi);
  3794. }
  3795. }
  3796. static void sum_intra_stats(FRAME_COUNTS *counts, const MODE_INFO *mi) {
  3797. const PREDICTION_MODE y_mode = mi->mode;
  3798. const PREDICTION_MODE uv_mode = mi->uv_mode;
  3799. const BLOCK_SIZE bsize = mi->sb_type;
  3800. if (bsize < BLOCK_8X8) {
  3801. int idx, idy;
  3802. const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
  3803. const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
  3804. for (idy = 0; idy < 2; idy += num_4x4_h)
  3805. for (idx = 0; idx < 2; idx += num_4x4_w)
  3806. ++counts->y_mode[0][mi->bmi[idy * 2 + idx].as_mode];
  3807. } else {
  3808. ++counts->y_mode[size_group_lookup[bsize]][y_mode];
  3809. }
  3810. ++counts->uv_mode[y_mode][uv_mode];
  3811. }
  3812. static void update_zeromv_cnt(VP9_COMP *const cpi, const MODE_INFO *const mi,
  3813. int mi_row, int mi_col, BLOCK_SIZE bsize) {
  3814. const VP9_COMMON *const cm = &cpi->common;
  3815. MV mv = mi->mv[0].as_mv;
  3816. const int bw = num_8x8_blocks_wide_lookup[bsize];
  3817. const int bh = num_8x8_blocks_high_lookup[bsize];
  3818. const int xmis = VPXMIN(cm->mi_cols - mi_col, bw);
  3819. const int ymis = VPXMIN(cm->mi_rows - mi_row, bh);
  3820. const int block_index = mi_row * cm->mi_cols + mi_col;
  3821. int x, y;
  3822. for (y = 0; y < ymis; y++)
  3823. for (x = 0; x < xmis; x++) {
  3824. int map_offset = block_index + y * cm->mi_cols + x;
  3825. if (is_inter_block(mi) && mi->segment_id <= CR_SEGMENT_ID_BOOST2) {
  3826. if (abs(mv.row) < 8 && abs(mv.col) < 8) {
  3827. if (cpi->consec_zero_mv[map_offset] < 255)
  3828. cpi->consec_zero_mv[map_offset]++;
  3829. } else {
  3830. cpi->consec_zero_mv[map_offset] = 0;
  3831. }
  3832. }
  3833. }
  3834. }
  3835. static void encode_superblock(VP9_COMP *cpi, ThreadData *td, TOKENEXTRA **t,
  3836. int output_enabled, int mi_row, int mi_col,
  3837. BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
  3838. VP9_COMMON *const cm = &cpi->common;
  3839. MACROBLOCK *const x = &td->mb;
  3840. MACROBLOCKD *const xd = &x->e_mbd;
  3841. MODE_INFO *mi = xd->mi[0];
  3842. const int seg_skip =
  3843. segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_SKIP);
  3844. x->skip_recode = !x->select_tx_size && mi->sb_type >= BLOCK_8X8 &&
  3845. cpi->oxcf.aq_mode != COMPLEXITY_AQ &&
  3846. cpi->oxcf.aq_mode != CYCLIC_REFRESH_AQ &&
  3847. cpi->sf.allow_skip_recode;
  3848. if (!x->skip_recode && !cpi->sf.use_nonrd_pick_mode)
  3849. memset(x->skip_txfm, 0, sizeof(x->skip_txfm));
  3850. x->skip_optimize = ctx->is_coded;
  3851. ctx->is_coded = 1;
  3852. x->use_lp32x32fdct = cpi->sf.use_lp32x32fdct;
  3853. x->skip_encode = (!output_enabled && cpi->sf.skip_encode_frame &&
  3854. x->q_index < QIDX_SKIP_THRESH);
  3855. if (x->skip_encode) return;
  3856. if (!is_inter_block(mi)) {
  3857. int plane;
  3858. #if CONFIG_BETTER_HW_COMPATIBILITY && CONFIG_VP9_HIGHBITDEPTH
  3859. if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) &&
  3860. (xd->above_mi == NULL || xd->left_mi == NULL) &&
  3861. need_top_left[mi->uv_mode])
  3862. assert(0);
  3863. #endif // CONFIG_BETTER_HW_COMPATIBILITY && CONFIG_VP9_HIGHBITDEPTH
  3864. mi->skip = 1;
  3865. for (plane = 0; plane < MAX_MB_PLANE; ++plane)
  3866. vp9_encode_intra_block_plane(x, VPXMAX(bsize, BLOCK_8X8), plane, 1);
  3867. if (output_enabled) sum_intra_stats(td->counts, mi);
  3868. vp9_tokenize_sb(cpi, td, t, !output_enabled, seg_skip,
  3869. VPXMAX(bsize, BLOCK_8X8));
  3870. } else {
  3871. int ref;
  3872. const int is_compound = has_second_ref(mi);
  3873. set_ref_ptrs(cm, xd, mi->ref_frame[0], mi->ref_frame[1]);
  3874. for (ref = 0; ref < 1 + is_compound; ++ref) {
  3875. YV12_BUFFER_CONFIG *cfg = get_ref_frame_buffer(cpi, mi->ref_frame[ref]);
  3876. assert(cfg != NULL);
  3877. vp9_setup_pre_planes(xd, ref, cfg, mi_row, mi_col,
  3878. &xd->block_refs[ref]->sf);
  3879. }
  3880. if (!(cpi->sf.reuse_inter_pred_sby && ctx->pred_pixel_ready) || seg_skip)
  3881. vp9_build_inter_predictors_sby(xd, mi_row, mi_col,
  3882. VPXMAX(bsize, BLOCK_8X8));
  3883. vp9_build_inter_predictors_sbuv(xd, mi_row, mi_col,
  3884. VPXMAX(bsize, BLOCK_8X8));
  3885. vp9_encode_sb(x, VPXMAX(bsize, BLOCK_8X8));
  3886. vp9_tokenize_sb(cpi, td, t, !output_enabled, seg_skip,
  3887. VPXMAX(bsize, BLOCK_8X8));
  3888. }
  3889. if (seg_skip) {
  3890. assert(mi->skip);
  3891. }
  3892. if (output_enabled) {
  3893. if (cm->tx_mode == TX_MODE_SELECT && mi->sb_type >= BLOCK_8X8 &&
  3894. !(is_inter_block(mi) && mi->skip)) {
  3895. ++get_tx_counts(max_txsize_lookup[bsize], get_tx_size_context(xd),
  3896. &td->counts->tx)[mi->tx_size];
  3897. } else {
  3898. // The new intra coding scheme requires no change of transform size
  3899. if (is_inter_block(mi)) {
  3900. mi->tx_size = VPXMIN(tx_mode_to_biggest_tx_size[cm->tx_mode],
  3901. max_txsize_lookup[bsize]);
  3902. } else {
  3903. mi->tx_size = (bsize >= BLOCK_8X8) ? mi->tx_size : TX_4X4;
  3904. }
  3905. }
  3906. ++td->counts->tx.tx_totals[mi->tx_size];
  3907. ++td->counts->tx.tx_totals[get_uv_tx_size(mi, &xd->plane[1])];
  3908. if (cm->seg.enabled && cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
  3909. vp9_cyclic_refresh_update_sb_postencode(cpi, mi, mi_row, mi_col, bsize);
  3910. if (cpi->oxcf.pass == 0 && cpi->svc.temporal_layer_id == 0)
  3911. update_zeromv_cnt(cpi, mi, mi_row, mi_col, bsize);
  3912. }
  3913. }