vp9_rdopt.c 177 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763
  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 <assert.h>
  11. #include <math.h>
  12. #include "./vp9_rtcd.h"
  13. #include "./vpx_dsp_rtcd.h"
  14. #include "vpx_dsp/vpx_dsp_common.h"
  15. #include "vpx_mem/vpx_mem.h"
  16. #include "vpx_ports/mem.h"
  17. #include "vpx_ports/system_state.h"
  18. #include "vp9/common/vp9_common.h"
  19. #include "vp9/common/vp9_entropy.h"
  20. #include "vp9/common/vp9_entropymode.h"
  21. #include "vp9/common/vp9_idct.h"
  22. #include "vp9/common/vp9_mvref_common.h"
  23. #include "vp9/common/vp9_pred_common.h"
  24. #include "vp9/common/vp9_quant_common.h"
  25. #include "vp9/common/vp9_reconinter.h"
  26. #include "vp9/common/vp9_reconintra.h"
  27. #include "vp9/common/vp9_scan.h"
  28. #include "vp9/common/vp9_seg_common.h"
  29. #if !CONFIG_REALTIME_ONLY
  30. #include "vp9/encoder/vp9_aq_variance.h"
  31. #endif
  32. #include "vp9/encoder/vp9_cost.h"
  33. #include "vp9/encoder/vp9_encodemb.h"
  34. #include "vp9/encoder/vp9_encodemv.h"
  35. #include "vp9/encoder/vp9_encoder.h"
  36. #include "vp9/encoder/vp9_mcomp.h"
  37. #include "vp9/encoder/vp9_quantize.h"
  38. #include "vp9/encoder/vp9_ratectrl.h"
  39. #include "vp9/encoder/vp9_rd.h"
  40. #include "vp9/encoder/vp9_rdopt.h"
  41. #define LAST_FRAME_MODE_MASK \
  42. ((1 << GOLDEN_FRAME) | (1 << ALTREF_FRAME) | (1 << INTRA_FRAME))
  43. #define GOLDEN_FRAME_MODE_MASK \
  44. ((1 << LAST_FRAME) | (1 << ALTREF_FRAME) | (1 << INTRA_FRAME))
  45. #define ALT_REF_MODE_MASK \
  46. ((1 << LAST_FRAME) | (1 << GOLDEN_FRAME) | (1 << INTRA_FRAME))
  47. #define SECOND_REF_FRAME_MASK ((1 << ALTREF_FRAME) | 0x01)
  48. #define MIN_EARLY_TERM_INDEX 3
  49. #define NEW_MV_DISCOUNT_FACTOR 8
  50. typedef struct {
  51. PREDICTION_MODE mode;
  52. MV_REFERENCE_FRAME ref_frame[2];
  53. } MODE_DEFINITION;
  54. typedef struct {
  55. MV_REFERENCE_FRAME ref_frame[2];
  56. } REF_DEFINITION;
  57. struct rdcost_block_args {
  58. const VP9_COMP *cpi;
  59. MACROBLOCK *x;
  60. ENTROPY_CONTEXT t_above[16];
  61. ENTROPY_CONTEXT t_left[16];
  62. int this_rate;
  63. int64_t this_dist;
  64. int64_t this_sse;
  65. int64_t this_rd;
  66. int64_t best_rd;
  67. int exit_early;
  68. int use_fast_coef_costing;
  69. const scan_order *so;
  70. uint8_t skippable;
  71. struct buf_2d *this_recon;
  72. };
  73. #define LAST_NEW_MV_INDEX 6
  74. #if !CONFIG_REALTIME_ONLY
  75. static const MODE_DEFINITION vp9_mode_order[MAX_MODES] = {
  76. { NEARESTMV, { LAST_FRAME, NONE } },
  77. { NEARESTMV, { ALTREF_FRAME, NONE } },
  78. { NEARESTMV, { GOLDEN_FRAME, NONE } },
  79. { DC_PRED, { INTRA_FRAME, NONE } },
  80. { NEWMV, { LAST_FRAME, NONE } },
  81. { NEWMV, { ALTREF_FRAME, NONE } },
  82. { NEWMV, { GOLDEN_FRAME, NONE } },
  83. { NEARMV, { LAST_FRAME, NONE } },
  84. { NEARMV, { ALTREF_FRAME, NONE } },
  85. { NEARMV, { GOLDEN_FRAME, NONE } },
  86. { ZEROMV, { LAST_FRAME, NONE } },
  87. { ZEROMV, { GOLDEN_FRAME, NONE } },
  88. { ZEROMV, { ALTREF_FRAME, NONE } },
  89. { NEARESTMV, { LAST_FRAME, ALTREF_FRAME } },
  90. { NEARESTMV, { GOLDEN_FRAME, ALTREF_FRAME } },
  91. { TM_PRED, { INTRA_FRAME, NONE } },
  92. { NEARMV, { LAST_FRAME, ALTREF_FRAME } },
  93. { NEWMV, { LAST_FRAME, ALTREF_FRAME } },
  94. { NEARMV, { GOLDEN_FRAME, ALTREF_FRAME } },
  95. { NEWMV, { GOLDEN_FRAME, ALTREF_FRAME } },
  96. { ZEROMV, { LAST_FRAME, ALTREF_FRAME } },
  97. { ZEROMV, { GOLDEN_FRAME, ALTREF_FRAME } },
  98. { H_PRED, { INTRA_FRAME, NONE } },
  99. { V_PRED, { INTRA_FRAME, NONE } },
  100. { D135_PRED, { INTRA_FRAME, NONE } },
  101. { D207_PRED, { INTRA_FRAME, NONE } },
  102. { D153_PRED, { INTRA_FRAME, NONE } },
  103. { D63_PRED, { INTRA_FRAME, NONE } },
  104. { D117_PRED, { INTRA_FRAME, NONE } },
  105. { D45_PRED, { INTRA_FRAME, NONE } },
  106. };
  107. static const REF_DEFINITION vp9_ref_order[MAX_REFS] = {
  108. { { LAST_FRAME, NONE } }, { { GOLDEN_FRAME, NONE } },
  109. { { ALTREF_FRAME, NONE } }, { { LAST_FRAME, ALTREF_FRAME } },
  110. { { GOLDEN_FRAME, ALTREF_FRAME } }, { { INTRA_FRAME, NONE } },
  111. };
  112. #endif // !CONFIG_REALTIME_ONLY
  113. static void swap_block_ptr(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx, int m, int n,
  114. int min_plane, int max_plane) {
  115. int i;
  116. for (i = min_plane; i < max_plane; ++i) {
  117. struct macroblock_plane *const p = &x->plane[i];
  118. struct macroblockd_plane *const pd = &x->e_mbd.plane[i];
  119. p->coeff = ctx->coeff_pbuf[i][m];
  120. p->qcoeff = ctx->qcoeff_pbuf[i][m];
  121. pd->dqcoeff = ctx->dqcoeff_pbuf[i][m];
  122. p->eobs = ctx->eobs_pbuf[i][m];
  123. ctx->coeff_pbuf[i][m] = ctx->coeff_pbuf[i][n];
  124. ctx->qcoeff_pbuf[i][m] = ctx->qcoeff_pbuf[i][n];
  125. ctx->dqcoeff_pbuf[i][m] = ctx->dqcoeff_pbuf[i][n];
  126. ctx->eobs_pbuf[i][m] = ctx->eobs_pbuf[i][n];
  127. ctx->coeff_pbuf[i][n] = p->coeff;
  128. ctx->qcoeff_pbuf[i][n] = p->qcoeff;
  129. ctx->dqcoeff_pbuf[i][n] = pd->dqcoeff;
  130. ctx->eobs_pbuf[i][n] = p->eobs;
  131. }
  132. }
  133. #if !CONFIG_REALTIME_ONLY
  134. static void model_rd_for_sb(VP9_COMP *cpi, BLOCK_SIZE bsize, MACROBLOCK *x,
  135. MACROBLOCKD *xd, int *out_rate_sum,
  136. int64_t *out_dist_sum, int *skip_txfm_sb,
  137. int64_t *skip_sse_sb) {
  138. // Note our transform coeffs are 8 times an orthogonal transform.
  139. // Hence quantizer step is also 8 times. To get effective quantizer
  140. // we need to divide by 8 before sending to modeling function.
  141. int i;
  142. int64_t rate_sum = 0;
  143. int64_t dist_sum = 0;
  144. const int ref = xd->mi[0]->ref_frame[0];
  145. unsigned int sse;
  146. unsigned int var = 0;
  147. int64_t total_sse = 0;
  148. int skip_flag = 1;
  149. const int shift = 6;
  150. int64_t dist;
  151. const int dequant_shift =
  152. #if CONFIG_VP9_HIGHBITDEPTH
  153. (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? xd->bd - 5 :
  154. #endif // CONFIG_VP9_HIGHBITDEPTH
  155. 3;
  156. unsigned int qstep_vec[MAX_MB_PLANE];
  157. unsigned int nlog2_vec[MAX_MB_PLANE];
  158. unsigned int sum_sse_vec[MAX_MB_PLANE];
  159. int any_zero_sum_sse = 0;
  160. x->pred_sse[ref] = 0;
  161. for (i = 0; i < MAX_MB_PLANE; ++i) {
  162. struct macroblock_plane *const p = &x->plane[i];
  163. struct macroblockd_plane *const pd = &xd->plane[i];
  164. const BLOCK_SIZE bs = get_plane_block_size(bsize, pd);
  165. const TX_SIZE max_tx_size = max_txsize_lookup[bs];
  166. const BLOCK_SIZE unit_size = txsize_to_bsize[max_tx_size];
  167. const int64_t dc_thr = p->quant_thred[0] >> shift;
  168. const int64_t ac_thr = p->quant_thred[1] >> shift;
  169. unsigned int sum_sse = 0;
  170. // The low thresholds are used to measure if the prediction errors are
  171. // low enough so that we can skip the mode search.
  172. const int64_t low_dc_thr = VPXMIN(50, dc_thr >> 2);
  173. const int64_t low_ac_thr = VPXMIN(80, ac_thr >> 2);
  174. int bw = 1 << (b_width_log2_lookup[bs] - b_width_log2_lookup[unit_size]);
  175. int bh = 1 << (b_height_log2_lookup[bs] - b_width_log2_lookup[unit_size]);
  176. int idx, idy;
  177. int lw = b_width_log2_lookup[unit_size] + 2;
  178. int lh = b_height_log2_lookup[unit_size] + 2;
  179. for (idy = 0; idy < bh; ++idy) {
  180. for (idx = 0; idx < bw; ++idx) {
  181. uint8_t *src = p->src.buf + (idy * p->src.stride << lh) + (idx << lw);
  182. uint8_t *dst = pd->dst.buf + (idy * pd->dst.stride << lh) + (idx << lh);
  183. int block_idx = (idy << 1) + idx;
  184. int low_err_skip = 0;
  185. var = cpi->fn_ptr[unit_size].vf(src, p->src.stride, dst, pd->dst.stride,
  186. &sse);
  187. x->bsse[(i << 2) + block_idx] = sse;
  188. sum_sse += sse;
  189. x->skip_txfm[(i << 2) + block_idx] = SKIP_TXFM_NONE;
  190. if (!x->select_tx_size) {
  191. // Check if all ac coefficients can be quantized to zero.
  192. if (var < ac_thr || var == 0) {
  193. x->skip_txfm[(i << 2) + block_idx] = SKIP_TXFM_AC_ONLY;
  194. // Check if dc coefficient can be quantized to zero.
  195. if (sse - var < dc_thr || sse == var) {
  196. x->skip_txfm[(i << 2) + block_idx] = SKIP_TXFM_AC_DC;
  197. if (!sse || (var < low_ac_thr && sse - var < low_dc_thr))
  198. low_err_skip = 1;
  199. }
  200. }
  201. }
  202. if (skip_flag && !low_err_skip) skip_flag = 0;
  203. if (i == 0) x->pred_sse[ref] += sse;
  204. }
  205. }
  206. total_sse += sum_sse;
  207. sum_sse_vec[i] = sum_sse;
  208. any_zero_sum_sse = any_zero_sum_sse || (sum_sse == 0);
  209. qstep_vec[i] = pd->dequant[1] >> dequant_shift;
  210. nlog2_vec[i] = num_pels_log2_lookup[bs];
  211. }
  212. // Fast approximate the modelling function.
  213. if (cpi->sf.simple_model_rd_from_var) {
  214. for (i = 0; i < MAX_MB_PLANE; ++i) {
  215. int64_t rate;
  216. const int64_t square_error = sum_sse_vec[i];
  217. int quantizer = qstep_vec[i];
  218. if (quantizer < 120)
  219. rate = (square_error * (280 - quantizer)) >> (16 - VP9_PROB_COST_SHIFT);
  220. else
  221. rate = 0;
  222. dist = (square_error * quantizer) >> 8;
  223. rate_sum += rate;
  224. dist_sum += dist;
  225. }
  226. } else {
  227. if (any_zero_sum_sse) {
  228. for (i = 0; i < MAX_MB_PLANE; ++i) {
  229. int rate;
  230. vp9_model_rd_from_var_lapndz(sum_sse_vec[i], nlog2_vec[i], qstep_vec[i],
  231. &rate, &dist);
  232. rate_sum += rate;
  233. dist_sum += dist;
  234. }
  235. } else {
  236. vp9_model_rd_from_var_lapndz_vec(sum_sse_vec, nlog2_vec, qstep_vec,
  237. &rate_sum, &dist_sum);
  238. }
  239. }
  240. *skip_txfm_sb = skip_flag;
  241. *skip_sse_sb = total_sse << VP9_DIST_SCALE_LOG2;
  242. *out_rate_sum = (int)rate_sum;
  243. *out_dist_sum = dist_sum << VP9_DIST_SCALE_LOG2;
  244. }
  245. #endif // !CONFIG_REALTIME_ONLY
  246. #if CONFIG_VP9_HIGHBITDEPTH
  247. int64_t vp9_highbd_block_error_c(const tran_low_t *coeff,
  248. const tran_low_t *dqcoeff, intptr_t block_size,
  249. int64_t *ssz, int bd) {
  250. int i;
  251. int64_t error = 0, sqcoeff = 0;
  252. int shift = 2 * (bd - 8);
  253. int rounding = shift > 0 ? 1 << (shift - 1) : 0;
  254. for (i = 0; i < block_size; i++) {
  255. const int64_t diff = coeff[i] - dqcoeff[i];
  256. error += diff * diff;
  257. sqcoeff += (int64_t)coeff[i] * (int64_t)coeff[i];
  258. }
  259. assert(error >= 0 && sqcoeff >= 0);
  260. error = (error + rounding) >> shift;
  261. sqcoeff = (sqcoeff + rounding) >> shift;
  262. *ssz = sqcoeff;
  263. return error;
  264. }
  265. static int64_t vp9_highbd_block_error_dispatch(const tran_low_t *coeff,
  266. const tran_low_t *dqcoeff,
  267. intptr_t block_size,
  268. int64_t *ssz, int bd) {
  269. if (bd == 8) {
  270. return vp9_block_error(coeff, dqcoeff, block_size, ssz);
  271. } else {
  272. return vp9_highbd_block_error(coeff, dqcoeff, block_size, ssz, bd);
  273. }
  274. }
  275. #endif // CONFIG_VP9_HIGHBITDEPTH
  276. int64_t vp9_block_error_c(const tran_low_t *coeff, const tran_low_t *dqcoeff,
  277. intptr_t block_size, int64_t *ssz) {
  278. int i;
  279. int64_t error = 0, sqcoeff = 0;
  280. for (i = 0; i < block_size; i++) {
  281. const int diff = coeff[i] - dqcoeff[i];
  282. error += diff * diff;
  283. sqcoeff += coeff[i] * coeff[i];
  284. }
  285. *ssz = sqcoeff;
  286. return error;
  287. }
  288. int64_t vp9_block_error_fp_c(const tran_low_t *coeff, const tran_low_t *dqcoeff,
  289. int block_size) {
  290. int i;
  291. int64_t error = 0;
  292. for (i = 0; i < block_size; i++) {
  293. const int diff = coeff[i] - dqcoeff[i];
  294. error += diff * diff;
  295. }
  296. return error;
  297. }
  298. /* The trailing '0' is a terminator which is used inside cost_coeffs() to
  299. * decide whether to include cost of a trailing EOB node or not (i.e. we
  300. * can skip this if the last coefficient in this transform block, e.g. the
  301. * 16th coefficient in a 4x4 block or the 64th coefficient in a 8x8 block,
  302. * were non-zero). */
  303. static const int16_t band_counts[TX_SIZES][8] = {
  304. { 1, 2, 3, 4, 3, 16 - 13, 0 },
  305. { 1, 2, 3, 4, 11, 64 - 21, 0 },
  306. { 1, 2, 3, 4, 11, 256 - 21, 0 },
  307. { 1, 2, 3, 4, 11, 1024 - 21, 0 },
  308. };
  309. static int cost_coeffs(MACROBLOCK *x, int plane, int block, TX_SIZE tx_size,
  310. int pt, const int16_t *scan, const int16_t *nb,
  311. int use_fast_coef_costing) {
  312. MACROBLOCKD *const xd = &x->e_mbd;
  313. MODE_INFO *mi = xd->mi[0];
  314. const struct macroblock_plane *p = &x->plane[plane];
  315. const PLANE_TYPE type = get_plane_type(plane);
  316. const int16_t *band_count = &band_counts[tx_size][1];
  317. const int eob = p->eobs[block];
  318. const tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  319. unsigned int(*token_costs)[2][COEFF_CONTEXTS][ENTROPY_TOKENS] =
  320. x->token_costs[tx_size][type][is_inter_block(mi)];
  321. uint8_t token_cache[32 * 32];
  322. int cost;
  323. #if CONFIG_VP9_HIGHBITDEPTH
  324. const uint16_t *cat6_high_cost = vp9_get_high_cost_table(xd->bd);
  325. #else
  326. const uint16_t *cat6_high_cost = vp9_get_high_cost_table(8);
  327. #endif
  328. // Check for consistency of tx_size with mode info
  329. assert(type == PLANE_TYPE_Y
  330. ? mi->tx_size == tx_size
  331. : get_uv_tx_size(mi, &xd->plane[plane]) == tx_size);
  332. if (eob == 0) {
  333. // single eob token
  334. cost = token_costs[0][0][pt][EOB_TOKEN];
  335. } else {
  336. if (use_fast_coef_costing) {
  337. int band_left = *band_count++;
  338. int c;
  339. // dc token
  340. int v = qcoeff[0];
  341. int16_t prev_t;
  342. cost = vp9_get_token_cost(v, &prev_t, cat6_high_cost);
  343. cost += (*token_costs)[0][pt][prev_t];
  344. token_cache[0] = vp9_pt_energy_class[prev_t];
  345. ++token_costs;
  346. // ac tokens
  347. for (c = 1; c < eob; c++) {
  348. const int rc = scan[c];
  349. int16_t t;
  350. v = qcoeff[rc];
  351. cost += vp9_get_token_cost(v, &t, cat6_high_cost);
  352. cost += (*token_costs)[!prev_t][!prev_t][t];
  353. prev_t = t;
  354. if (!--band_left) {
  355. band_left = *band_count++;
  356. ++token_costs;
  357. }
  358. }
  359. // eob token
  360. if (band_left) cost += (*token_costs)[0][!prev_t][EOB_TOKEN];
  361. } else { // !use_fast_coef_costing
  362. int band_left = *band_count++;
  363. int c;
  364. // dc token
  365. int v = qcoeff[0];
  366. int16_t tok;
  367. unsigned int(*tok_cost_ptr)[COEFF_CONTEXTS][ENTROPY_TOKENS];
  368. cost = vp9_get_token_cost(v, &tok, cat6_high_cost);
  369. cost += (*token_costs)[0][pt][tok];
  370. token_cache[0] = vp9_pt_energy_class[tok];
  371. ++token_costs;
  372. tok_cost_ptr = &((*token_costs)[!tok]);
  373. // ac tokens
  374. for (c = 1; c < eob; c++) {
  375. const int rc = scan[c];
  376. v = qcoeff[rc];
  377. cost += vp9_get_token_cost(v, &tok, cat6_high_cost);
  378. pt = get_coef_context(nb, token_cache, c);
  379. cost += (*tok_cost_ptr)[pt][tok];
  380. token_cache[rc] = vp9_pt_energy_class[tok];
  381. if (!--band_left) {
  382. band_left = *band_count++;
  383. ++token_costs;
  384. }
  385. tok_cost_ptr = &((*token_costs)[!tok]);
  386. }
  387. // eob token
  388. if (band_left) {
  389. pt = get_coef_context(nb, token_cache, c);
  390. cost += (*token_costs)[0][pt][EOB_TOKEN];
  391. }
  392. }
  393. }
  394. return cost;
  395. }
  396. static INLINE int num_4x4_to_edge(int plane_4x4_dim, int mb_to_edge_dim,
  397. int subsampling_dim, int blk_dim) {
  398. return plane_4x4_dim + (mb_to_edge_dim >> (5 + subsampling_dim)) - blk_dim;
  399. }
  400. // Copy all visible 4x4s in the transform block.
  401. static void copy_block_visible(const MACROBLOCKD *xd,
  402. const struct macroblockd_plane *const pd,
  403. const uint8_t *src, const int src_stride,
  404. uint8_t *dst, const int dst_stride, int blk_row,
  405. int blk_col, const BLOCK_SIZE plane_bsize,
  406. const BLOCK_SIZE tx_bsize) {
  407. const int plane_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
  408. const int plane_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
  409. const int tx_4x4_w = num_4x4_blocks_wide_lookup[tx_bsize];
  410. const int tx_4x4_h = num_4x4_blocks_high_lookup[tx_bsize];
  411. int b4x4s_to_right_edge = num_4x4_to_edge(plane_4x4_w, xd->mb_to_right_edge,
  412. pd->subsampling_x, blk_col);
  413. int b4x4s_to_bottom_edge = num_4x4_to_edge(plane_4x4_h, xd->mb_to_bottom_edge,
  414. pd->subsampling_y, blk_row);
  415. const int is_highbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH;
  416. if (tx_bsize == BLOCK_4X4 ||
  417. (b4x4s_to_right_edge >= tx_4x4_w && b4x4s_to_bottom_edge >= tx_4x4_h)) {
  418. const int w = tx_4x4_w << 2;
  419. const int h = tx_4x4_h << 2;
  420. #if CONFIG_VP9_HIGHBITDEPTH
  421. if (is_highbd) {
  422. vpx_highbd_convolve_copy(CONVERT_TO_SHORTPTR(src), src_stride,
  423. CONVERT_TO_SHORTPTR(dst), dst_stride, NULL, 0, 0,
  424. 0, 0, w, h, xd->bd);
  425. } else {
  426. #endif
  427. vpx_convolve_copy(src, src_stride, dst, dst_stride, NULL, 0, 0, 0, 0, w,
  428. h);
  429. #if CONFIG_VP9_HIGHBITDEPTH
  430. }
  431. #endif
  432. } else {
  433. int r, c;
  434. int max_r = VPXMIN(b4x4s_to_bottom_edge, tx_4x4_h);
  435. int max_c = VPXMIN(b4x4s_to_right_edge, tx_4x4_w);
  436. // if we are in the unrestricted motion border.
  437. for (r = 0; r < max_r; ++r) {
  438. // Skip visiting the sub blocks that are wholly within the UMV.
  439. for (c = 0; c < max_c; ++c) {
  440. const uint8_t *src_ptr = src + r * src_stride * 4 + c * 4;
  441. uint8_t *dst_ptr = dst + r * dst_stride * 4 + c * 4;
  442. #if CONFIG_VP9_HIGHBITDEPTH
  443. if (is_highbd) {
  444. vpx_highbd_convolve_copy(CONVERT_TO_SHORTPTR(src_ptr), src_stride,
  445. CONVERT_TO_SHORTPTR(dst_ptr), dst_stride,
  446. NULL, 0, 0, 0, 0, 4, 4, xd->bd);
  447. } else {
  448. #endif
  449. vpx_convolve_copy(src_ptr, src_stride, dst_ptr, dst_stride, NULL, 0,
  450. 0, 0, 0, 4, 4);
  451. #if CONFIG_VP9_HIGHBITDEPTH
  452. }
  453. #endif
  454. }
  455. }
  456. }
  457. (void)is_highbd;
  458. }
  459. // Compute the pixel domain sum square error on all visible 4x4s in the
  460. // transform block.
  461. static unsigned pixel_sse(const VP9_COMP *const cpi, const MACROBLOCKD *xd,
  462. const struct macroblockd_plane *const pd,
  463. const uint8_t *src, const int src_stride,
  464. const uint8_t *dst, const int dst_stride, int blk_row,
  465. int blk_col, const BLOCK_SIZE plane_bsize,
  466. const BLOCK_SIZE tx_bsize) {
  467. unsigned int sse = 0;
  468. const int plane_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
  469. const int plane_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
  470. const int tx_4x4_w = num_4x4_blocks_wide_lookup[tx_bsize];
  471. const int tx_4x4_h = num_4x4_blocks_high_lookup[tx_bsize];
  472. int b4x4s_to_right_edge = num_4x4_to_edge(plane_4x4_w, xd->mb_to_right_edge,
  473. pd->subsampling_x, blk_col);
  474. int b4x4s_to_bottom_edge = num_4x4_to_edge(plane_4x4_h, xd->mb_to_bottom_edge,
  475. pd->subsampling_y, blk_row);
  476. if (tx_bsize == BLOCK_4X4 ||
  477. (b4x4s_to_right_edge >= tx_4x4_w && b4x4s_to_bottom_edge >= tx_4x4_h)) {
  478. cpi->fn_ptr[tx_bsize].vf(src, src_stride, dst, dst_stride, &sse);
  479. } else {
  480. const vpx_variance_fn_t vf_4x4 = cpi->fn_ptr[BLOCK_4X4].vf;
  481. int r, c;
  482. unsigned this_sse = 0;
  483. int max_r = VPXMIN(b4x4s_to_bottom_edge, tx_4x4_h);
  484. int max_c = VPXMIN(b4x4s_to_right_edge, tx_4x4_w);
  485. sse = 0;
  486. // if we are in the unrestricted motion border.
  487. for (r = 0; r < max_r; ++r) {
  488. // Skip visiting the sub blocks that are wholly within the UMV.
  489. for (c = 0; c < max_c; ++c) {
  490. vf_4x4(src + r * src_stride * 4 + c * 4, src_stride,
  491. dst + r * dst_stride * 4 + c * 4, dst_stride, &this_sse);
  492. sse += this_sse;
  493. }
  494. }
  495. }
  496. return sse;
  497. }
  498. // Compute the squares sum squares on all visible 4x4s in the transform block.
  499. static int64_t sum_squares_visible(const MACROBLOCKD *xd,
  500. const struct macroblockd_plane *const pd,
  501. const int16_t *diff, const int diff_stride,
  502. int blk_row, int blk_col,
  503. const BLOCK_SIZE plane_bsize,
  504. const BLOCK_SIZE tx_bsize) {
  505. int64_t sse;
  506. const int plane_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
  507. const int plane_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
  508. const int tx_4x4_w = num_4x4_blocks_wide_lookup[tx_bsize];
  509. const int tx_4x4_h = num_4x4_blocks_high_lookup[tx_bsize];
  510. int b4x4s_to_right_edge = num_4x4_to_edge(plane_4x4_w, xd->mb_to_right_edge,
  511. pd->subsampling_x, blk_col);
  512. int b4x4s_to_bottom_edge = num_4x4_to_edge(plane_4x4_h, xd->mb_to_bottom_edge,
  513. pd->subsampling_y, blk_row);
  514. if (tx_bsize == BLOCK_4X4 ||
  515. (b4x4s_to_right_edge >= tx_4x4_w && b4x4s_to_bottom_edge >= tx_4x4_h)) {
  516. assert(tx_4x4_w == tx_4x4_h);
  517. sse = (int64_t)vpx_sum_squares_2d_i16(diff, diff_stride, tx_4x4_w << 2);
  518. } else {
  519. int r, c;
  520. int max_r = VPXMIN(b4x4s_to_bottom_edge, tx_4x4_h);
  521. int max_c = VPXMIN(b4x4s_to_right_edge, tx_4x4_w);
  522. sse = 0;
  523. // if we are in the unrestricted motion border.
  524. for (r = 0; r < max_r; ++r) {
  525. // Skip visiting the sub blocks that are wholly within the UMV.
  526. for (c = 0; c < max_c; ++c) {
  527. sse += (int64_t)vpx_sum_squares_2d_i16(
  528. diff + r * diff_stride * 4 + c * 4, diff_stride, 4);
  529. }
  530. }
  531. }
  532. return sse;
  533. }
  534. static void dist_block(const VP9_COMP *cpi, MACROBLOCK *x, int plane,
  535. BLOCK_SIZE plane_bsize, int block, int blk_row,
  536. int blk_col, TX_SIZE tx_size, int64_t *out_dist,
  537. int64_t *out_sse, struct buf_2d *out_recon) {
  538. MACROBLOCKD *const xd = &x->e_mbd;
  539. const struct macroblock_plane *const p = &x->plane[plane];
  540. const struct macroblockd_plane *const pd = &xd->plane[plane];
  541. const int eob = p->eobs[block];
  542. if (!out_recon && x->block_tx_domain && eob) {
  543. const int ss_txfrm_size = tx_size << 1;
  544. int64_t this_sse;
  545. const int shift = tx_size == TX_32X32 ? 0 : 2;
  546. const tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
  547. const tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  548. #if CONFIG_VP9_HIGHBITDEPTH
  549. const int bd = (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? xd->bd : 8;
  550. *out_dist = vp9_highbd_block_error_dispatch(
  551. coeff, dqcoeff, 16 << ss_txfrm_size, &this_sse, bd) >>
  552. shift;
  553. #else
  554. *out_dist =
  555. vp9_block_error(coeff, dqcoeff, 16 << ss_txfrm_size, &this_sse) >>
  556. shift;
  557. #endif // CONFIG_VP9_HIGHBITDEPTH
  558. *out_sse = this_sse >> shift;
  559. if (x->skip_encode && !is_inter_block(xd->mi[0])) {
  560. // TODO(jingning): tune the model to better capture the distortion.
  561. const int64_t p =
  562. (pd->dequant[1] * pd->dequant[1] * (1 << ss_txfrm_size)) >>
  563. #if CONFIG_VP9_HIGHBITDEPTH
  564. (shift + 2 + (bd - 8) * 2);
  565. #else
  566. (shift + 2);
  567. #endif // CONFIG_VP9_HIGHBITDEPTH
  568. *out_dist += (p >> 4);
  569. *out_sse += p;
  570. }
  571. } else {
  572. const BLOCK_SIZE tx_bsize = txsize_to_bsize[tx_size];
  573. const int bs = 4 * num_4x4_blocks_wide_lookup[tx_bsize];
  574. const int src_stride = p->src.stride;
  575. const int dst_stride = pd->dst.stride;
  576. const int src_idx = 4 * (blk_row * src_stride + blk_col);
  577. const int dst_idx = 4 * (blk_row * dst_stride + blk_col);
  578. const uint8_t *src = &p->src.buf[src_idx];
  579. const uint8_t *dst = &pd->dst.buf[dst_idx];
  580. uint8_t *out_recon_ptr = 0;
  581. const tran_low_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  582. unsigned int tmp;
  583. tmp = pixel_sse(cpi, xd, pd, src, src_stride, dst, dst_stride, blk_row,
  584. blk_col, plane_bsize, tx_bsize);
  585. *out_sse = (int64_t)tmp * 16;
  586. if (out_recon) {
  587. const int out_recon_idx = 4 * (blk_row * out_recon->stride + blk_col);
  588. out_recon_ptr = &out_recon->buf[out_recon_idx];
  589. copy_block_visible(xd, pd, dst, dst_stride, out_recon_ptr,
  590. out_recon->stride, blk_row, blk_col, plane_bsize,
  591. tx_bsize);
  592. }
  593. if (eob) {
  594. #if CONFIG_VP9_HIGHBITDEPTH
  595. DECLARE_ALIGNED(16, uint16_t, recon16[1024]);
  596. uint8_t *recon = (uint8_t *)recon16;
  597. #else
  598. DECLARE_ALIGNED(16, uint8_t, recon[1024]);
  599. #endif // CONFIG_VP9_HIGHBITDEPTH
  600. #if CONFIG_VP9_HIGHBITDEPTH
  601. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  602. vpx_highbd_convolve_copy(CONVERT_TO_SHORTPTR(dst), dst_stride, recon16,
  603. 32, NULL, 0, 0, 0, 0, bs, bs, xd->bd);
  604. if (xd->lossless) {
  605. vp9_highbd_iwht4x4_add(dqcoeff, recon16, 32, eob, xd->bd);
  606. } else {
  607. switch (tx_size) {
  608. case TX_4X4:
  609. vp9_highbd_idct4x4_add(dqcoeff, recon16, 32, eob, xd->bd);
  610. break;
  611. case TX_8X8:
  612. vp9_highbd_idct8x8_add(dqcoeff, recon16, 32, eob, xd->bd);
  613. break;
  614. case TX_16X16:
  615. vp9_highbd_idct16x16_add(dqcoeff, recon16, 32, eob, xd->bd);
  616. break;
  617. default:
  618. assert(tx_size == TX_32X32);
  619. vp9_highbd_idct32x32_add(dqcoeff, recon16, 32, eob, xd->bd);
  620. break;
  621. }
  622. }
  623. recon = CONVERT_TO_BYTEPTR(recon16);
  624. } else {
  625. #endif // CONFIG_VP9_HIGHBITDEPTH
  626. vpx_convolve_copy(dst, dst_stride, recon, 32, NULL, 0, 0, 0, 0, bs, bs);
  627. switch (tx_size) {
  628. case TX_32X32: vp9_idct32x32_add(dqcoeff, recon, 32, eob); break;
  629. case TX_16X16: vp9_idct16x16_add(dqcoeff, recon, 32, eob); break;
  630. case TX_8X8: vp9_idct8x8_add(dqcoeff, recon, 32, eob); break;
  631. default:
  632. assert(tx_size == TX_4X4);
  633. // this is like vp9_short_idct4x4 but has a special case around
  634. // eob<=1, which is significant (not just an optimization) for
  635. // the lossless case.
  636. x->inv_txfm_add(dqcoeff, recon, 32, eob);
  637. break;
  638. }
  639. #if CONFIG_VP9_HIGHBITDEPTH
  640. }
  641. #endif // CONFIG_VP9_HIGHBITDEPTH
  642. tmp = pixel_sse(cpi, xd, pd, src, src_stride, recon, 32, blk_row, blk_col,
  643. plane_bsize, tx_bsize);
  644. if (out_recon) {
  645. copy_block_visible(xd, pd, recon, 32, out_recon_ptr, out_recon->stride,
  646. blk_row, blk_col, plane_bsize, tx_bsize);
  647. }
  648. }
  649. *out_dist = (int64_t)tmp * 16;
  650. }
  651. }
  652. static int rate_block(int plane, int block, TX_SIZE tx_size, int coeff_ctx,
  653. struct rdcost_block_args *args) {
  654. return cost_coeffs(args->x, plane, block, tx_size, coeff_ctx, args->so->scan,
  655. args->so->neighbors, args->use_fast_coef_costing);
  656. }
  657. static void block_rd_txfm(int plane, int block, int blk_row, int blk_col,
  658. BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) {
  659. struct rdcost_block_args *args = arg;
  660. MACROBLOCK *const x = args->x;
  661. MACROBLOCKD *const xd = &x->e_mbd;
  662. MODE_INFO *const mi = xd->mi[0];
  663. int64_t rd1, rd2, rd;
  664. int rate;
  665. int64_t dist;
  666. int64_t sse;
  667. const int coeff_ctx =
  668. combine_entropy_contexts(args->t_left[blk_row], args->t_above[blk_col]);
  669. struct buf_2d *recon = args->this_recon;
  670. const BLOCK_SIZE tx_bsize = txsize_to_bsize[tx_size];
  671. const struct macroblockd_plane *const pd = &xd->plane[plane];
  672. const int dst_stride = pd->dst.stride;
  673. const uint8_t *dst = &pd->dst.buf[4 * (blk_row * dst_stride + blk_col)];
  674. if (args->exit_early) return;
  675. if (!is_inter_block(mi)) {
  676. #if CONFIG_MISMATCH_DEBUG
  677. struct encode_b_args intra_arg = {
  678. x, x->block_qcoeff_opt, args->t_above, args->t_left, &mi->skip, 0, 0, 0
  679. };
  680. #else
  681. struct encode_b_args intra_arg = { x, x->block_qcoeff_opt, args->t_above,
  682. args->t_left, &mi->skip };
  683. #endif
  684. vp9_encode_block_intra(plane, block, blk_row, blk_col, plane_bsize, tx_size,
  685. &intra_arg);
  686. if (recon) {
  687. uint8_t *rec_ptr = &recon->buf[4 * (blk_row * recon->stride + blk_col)];
  688. copy_block_visible(xd, pd, dst, dst_stride, rec_ptr, recon->stride,
  689. blk_row, blk_col, plane_bsize, tx_bsize);
  690. }
  691. if (x->block_tx_domain) {
  692. dist_block(args->cpi, x, plane, plane_bsize, block, blk_row, blk_col,
  693. tx_size, &dist, &sse, /*recon =*/0);
  694. } else {
  695. const struct macroblock_plane *const p = &x->plane[plane];
  696. const int src_stride = p->src.stride;
  697. const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  698. const uint8_t *src = &p->src.buf[4 * (blk_row * src_stride + blk_col)];
  699. const int16_t *diff = &p->src_diff[4 * (blk_row * diff_stride + blk_col)];
  700. unsigned int tmp;
  701. sse = sum_squares_visible(xd, pd, diff, diff_stride, blk_row, blk_col,
  702. plane_bsize, tx_bsize);
  703. #if CONFIG_VP9_HIGHBITDEPTH
  704. if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) && (xd->bd > 8))
  705. sse = ROUND64_POWER_OF_TWO(sse, (xd->bd - 8) * 2);
  706. #endif // CONFIG_VP9_HIGHBITDEPTH
  707. sse = sse * 16;
  708. tmp = pixel_sse(args->cpi, xd, pd, src, src_stride, dst, dst_stride,
  709. blk_row, blk_col, plane_bsize, tx_bsize);
  710. dist = (int64_t)tmp * 16;
  711. }
  712. } else {
  713. int skip_txfm_flag = SKIP_TXFM_NONE;
  714. if (max_txsize_lookup[plane_bsize] == tx_size)
  715. skip_txfm_flag = x->skip_txfm[(plane << 2) + (block >> (tx_size << 1))];
  716. if (skip_txfm_flag == SKIP_TXFM_NONE ||
  717. (recon && skip_txfm_flag == SKIP_TXFM_AC_ONLY)) {
  718. // full forward transform and quantization
  719. vp9_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize, tx_size);
  720. if (x->block_qcoeff_opt)
  721. vp9_optimize_b(x, plane, block, tx_size, coeff_ctx);
  722. dist_block(args->cpi, x, plane, plane_bsize, block, blk_row, blk_col,
  723. tx_size, &dist, &sse, recon);
  724. } else if (skip_txfm_flag == SKIP_TXFM_AC_ONLY) {
  725. // compute DC coefficient
  726. tran_low_t *const coeff = BLOCK_OFFSET(x->plane[plane].coeff, block);
  727. tran_low_t *const dqcoeff = BLOCK_OFFSET(xd->plane[plane].dqcoeff, block);
  728. vp9_xform_quant_dc(x, plane, block, blk_row, blk_col, plane_bsize,
  729. tx_size);
  730. sse = x->bsse[(plane << 2) + (block >> (tx_size << 1))] << 4;
  731. dist = sse;
  732. if (x->plane[plane].eobs[block]) {
  733. const int64_t orig_sse = (int64_t)coeff[0] * coeff[0];
  734. const int64_t resd_sse = coeff[0] - dqcoeff[0];
  735. int64_t dc_correct = orig_sse - resd_sse * resd_sse;
  736. #if CONFIG_VP9_HIGHBITDEPTH
  737. dc_correct >>= ((xd->bd - 8) * 2);
  738. #endif
  739. if (tx_size != TX_32X32) dc_correct >>= 2;
  740. dist = VPXMAX(0, sse - dc_correct);
  741. }
  742. } else {
  743. // SKIP_TXFM_AC_DC
  744. // skip forward transform. Because this is handled here, the quantization
  745. // does not need to do it.
  746. x->plane[plane].eobs[block] = 0;
  747. sse = x->bsse[(plane << 2) + (block >> (tx_size << 1))] << 4;
  748. dist = sse;
  749. if (recon) {
  750. uint8_t *rec_ptr = &recon->buf[4 * (blk_row * recon->stride + blk_col)];
  751. copy_block_visible(xd, pd, dst, dst_stride, rec_ptr, recon->stride,
  752. blk_row, blk_col, plane_bsize, tx_bsize);
  753. }
  754. }
  755. }
  756. rd = RDCOST(x->rdmult, x->rddiv, 0, dist);
  757. if (args->this_rd + rd > args->best_rd) {
  758. args->exit_early = 1;
  759. return;
  760. }
  761. rate = rate_block(plane, block, tx_size, coeff_ctx, args);
  762. args->t_above[blk_col] = (x->plane[plane].eobs[block] > 0) ? 1 : 0;
  763. args->t_left[blk_row] = (x->plane[plane].eobs[block] > 0) ? 1 : 0;
  764. rd1 = RDCOST(x->rdmult, x->rddiv, rate, dist);
  765. rd2 = RDCOST(x->rdmult, x->rddiv, 0, sse);
  766. // TODO(jingning): temporarily enabled only for luma component
  767. rd = VPXMIN(rd1, rd2);
  768. if (plane == 0) {
  769. x->zcoeff_blk[tx_size][block] =
  770. !x->plane[plane].eobs[block] ||
  771. (x->sharpness == 0 && rd1 > rd2 && !xd->lossless);
  772. x->sum_y_eobs[tx_size] += x->plane[plane].eobs[block];
  773. }
  774. args->this_rate += rate;
  775. args->this_dist += dist;
  776. args->this_sse += sse;
  777. args->this_rd += rd;
  778. if (args->this_rd > args->best_rd) {
  779. args->exit_early = 1;
  780. return;
  781. }
  782. args->skippable &= !x->plane[plane].eobs[block];
  783. }
  784. static void txfm_rd_in_plane(const VP9_COMP *cpi, MACROBLOCK *x, int *rate,
  785. int64_t *distortion, int *skippable, int64_t *sse,
  786. int64_t ref_best_rd, int plane, BLOCK_SIZE bsize,
  787. TX_SIZE tx_size, int use_fast_coef_costing,
  788. struct buf_2d *recon) {
  789. MACROBLOCKD *const xd = &x->e_mbd;
  790. const struct macroblockd_plane *const pd = &xd->plane[plane];
  791. struct rdcost_block_args args;
  792. vp9_zero(args);
  793. args.cpi = cpi;
  794. args.x = x;
  795. args.best_rd = ref_best_rd;
  796. args.use_fast_coef_costing = use_fast_coef_costing;
  797. args.skippable = 1;
  798. args.this_recon = recon;
  799. if (plane == 0) xd->mi[0]->tx_size = tx_size;
  800. vp9_get_entropy_contexts(bsize, tx_size, pd, args.t_above, args.t_left);
  801. args.so = get_scan(xd, tx_size, get_plane_type(plane), 0);
  802. vp9_foreach_transformed_block_in_plane(xd, bsize, plane, block_rd_txfm,
  803. &args);
  804. if (args.exit_early) {
  805. *rate = INT_MAX;
  806. *distortion = INT64_MAX;
  807. *sse = INT64_MAX;
  808. *skippable = 0;
  809. } else {
  810. *distortion = args.this_dist;
  811. *rate = args.this_rate;
  812. *sse = args.this_sse;
  813. *skippable = args.skippable;
  814. }
  815. }
  816. static void choose_largest_tx_size(VP9_COMP *cpi, MACROBLOCK *x, int *rate,
  817. int64_t *distortion, int *skip, int64_t *sse,
  818. int64_t ref_best_rd, BLOCK_SIZE bs,
  819. struct buf_2d *recon) {
  820. const TX_SIZE max_tx_size = max_txsize_lookup[bs];
  821. VP9_COMMON *const cm = &cpi->common;
  822. const TX_SIZE largest_tx_size = tx_mode_to_biggest_tx_size[cm->tx_mode];
  823. MACROBLOCKD *const xd = &x->e_mbd;
  824. MODE_INFO *const mi = xd->mi[0];
  825. mi->tx_size = VPXMIN(max_tx_size, largest_tx_size);
  826. txfm_rd_in_plane(cpi, x, rate, distortion, skip, sse, ref_best_rd, 0, bs,
  827. mi->tx_size, cpi->sf.use_fast_coef_costing, recon);
  828. }
  829. static void choose_tx_size_from_rd(VP9_COMP *cpi, MACROBLOCK *x, int *rate,
  830. int64_t *distortion, int *skip,
  831. int64_t *psse, int64_t ref_best_rd,
  832. BLOCK_SIZE bs, struct buf_2d *recon) {
  833. const TX_SIZE max_tx_size = max_txsize_lookup[bs];
  834. VP9_COMMON *const cm = &cpi->common;
  835. MACROBLOCKD *const xd = &x->e_mbd;
  836. MODE_INFO *const mi = xd->mi[0];
  837. vpx_prob skip_prob = vp9_get_skip_prob(cm, xd);
  838. int r[TX_SIZES][2], s[TX_SIZES];
  839. int64_t d[TX_SIZES], sse[TX_SIZES];
  840. int64_t rd[TX_SIZES][2] = { { INT64_MAX, INT64_MAX },
  841. { INT64_MAX, INT64_MAX },
  842. { INT64_MAX, INT64_MAX },
  843. { INT64_MAX, INT64_MAX } };
  844. int n;
  845. int s0, s1;
  846. int64_t best_rd = ref_best_rd;
  847. TX_SIZE best_tx = max_tx_size;
  848. int start_tx, end_tx;
  849. const int tx_size_ctx = get_tx_size_context(xd);
  850. #if CONFIG_VP9_HIGHBITDEPTH
  851. DECLARE_ALIGNED(16, uint16_t, recon_buf16[TX_SIZES][64 * 64]);
  852. uint8_t *recon_buf[TX_SIZES];
  853. for (n = 0; n < TX_SIZES; ++n) {
  854. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  855. recon_buf[n] = CONVERT_TO_BYTEPTR(recon_buf16[n]);
  856. } else {
  857. recon_buf[n] = (uint8_t *)recon_buf16[n];
  858. }
  859. }
  860. #else
  861. DECLARE_ALIGNED(16, uint8_t, recon_buf[TX_SIZES][64 * 64]);
  862. #endif // CONFIG_VP9_HIGHBITDEPTH
  863. assert(skip_prob > 0);
  864. s0 = vp9_cost_bit(skip_prob, 0);
  865. s1 = vp9_cost_bit(skip_prob, 1);
  866. if (cm->tx_mode == TX_MODE_SELECT) {
  867. start_tx = max_tx_size;
  868. end_tx = VPXMAX(start_tx - cpi->sf.tx_size_search_depth, 0);
  869. if (bs > BLOCK_32X32) end_tx = VPXMIN(end_tx + 1, start_tx);
  870. } else {
  871. TX_SIZE chosen_tx_size =
  872. VPXMIN(max_tx_size, tx_mode_to_biggest_tx_size[cm->tx_mode]);
  873. start_tx = chosen_tx_size;
  874. end_tx = chosen_tx_size;
  875. }
  876. for (n = start_tx; n >= end_tx; n--) {
  877. const int r_tx_size = cpi->tx_size_cost[max_tx_size - 1][tx_size_ctx][n];
  878. if (recon) {
  879. struct buf_2d this_recon;
  880. this_recon.buf = recon_buf[n];
  881. this_recon.stride = recon->stride;
  882. txfm_rd_in_plane(cpi, x, &r[n][0], &d[n], &s[n], &sse[n], best_rd, 0, bs,
  883. n, cpi->sf.use_fast_coef_costing, &this_recon);
  884. } else {
  885. txfm_rd_in_plane(cpi, x, &r[n][0], &d[n], &s[n], &sse[n], best_rd, 0, bs,
  886. n, cpi->sf.use_fast_coef_costing, 0);
  887. }
  888. r[n][1] = r[n][0];
  889. if (r[n][0] < INT_MAX) {
  890. r[n][1] += r_tx_size;
  891. }
  892. if (d[n] == INT64_MAX || r[n][0] == INT_MAX) {
  893. rd[n][0] = rd[n][1] = INT64_MAX;
  894. } else if (s[n]) {
  895. if (is_inter_block(mi)) {
  896. rd[n][0] = rd[n][1] = RDCOST(x->rdmult, x->rddiv, s1, sse[n]);
  897. r[n][1] -= r_tx_size;
  898. } else {
  899. rd[n][0] = RDCOST(x->rdmult, x->rddiv, s1, sse[n]);
  900. rd[n][1] = RDCOST(x->rdmult, x->rddiv, s1 + r_tx_size, sse[n]);
  901. }
  902. } else {
  903. rd[n][0] = RDCOST(x->rdmult, x->rddiv, r[n][0] + s0, d[n]);
  904. rd[n][1] = RDCOST(x->rdmult, x->rddiv, r[n][1] + s0, d[n]);
  905. }
  906. if (is_inter_block(mi) && !xd->lossless && !s[n] && sse[n] != INT64_MAX) {
  907. rd[n][0] = VPXMIN(rd[n][0], RDCOST(x->rdmult, x->rddiv, s1, sse[n]));
  908. rd[n][1] = VPXMIN(rd[n][1], RDCOST(x->rdmult, x->rddiv, s1, sse[n]));
  909. }
  910. // Early termination in transform size search.
  911. if (cpi->sf.tx_size_search_breakout &&
  912. (rd[n][1] == INT64_MAX ||
  913. (n < (int)max_tx_size && rd[n][1] > rd[n + 1][1]) || s[n] == 1))
  914. break;
  915. if (rd[n][1] < best_rd) {
  916. best_tx = n;
  917. best_rd = rd[n][1];
  918. }
  919. }
  920. mi->tx_size = best_tx;
  921. *distortion = d[mi->tx_size];
  922. *rate = r[mi->tx_size][cm->tx_mode == TX_MODE_SELECT];
  923. *skip = s[mi->tx_size];
  924. *psse = sse[mi->tx_size];
  925. if (recon) {
  926. #if CONFIG_VP9_HIGHBITDEPTH
  927. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  928. memcpy(CONVERT_TO_SHORTPTR(recon->buf),
  929. CONVERT_TO_SHORTPTR(recon_buf[mi->tx_size]),
  930. 64 * 64 * sizeof(uint16_t));
  931. } else {
  932. #endif
  933. memcpy(recon->buf, recon_buf[mi->tx_size], 64 * 64);
  934. #if CONFIG_VP9_HIGHBITDEPTH
  935. }
  936. #endif
  937. }
  938. }
  939. static void super_block_yrd(VP9_COMP *cpi, MACROBLOCK *x, int *rate,
  940. int64_t *distortion, int *skip, int64_t *psse,
  941. BLOCK_SIZE bs, int64_t ref_best_rd,
  942. struct buf_2d *recon) {
  943. MACROBLOCKD *xd = &x->e_mbd;
  944. int64_t sse;
  945. int64_t *ret_sse = psse ? psse : &sse;
  946. assert(bs == xd->mi[0]->sb_type);
  947. if (cpi->sf.tx_size_search_method == USE_LARGESTALL || xd->lossless) {
  948. choose_largest_tx_size(cpi, x, rate, distortion, skip, ret_sse, ref_best_rd,
  949. bs, recon);
  950. } else {
  951. choose_tx_size_from_rd(cpi, x, rate, distortion, skip, ret_sse, ref_best_rd,
  952. bs, recon);
  953. }
  954. }
  955. static int conditional_skipintra(PREDICTION_MODE mode,
  956. PREDICTION_MODE best_intra_mode) {
  957. if (mode == D117_PRED && best_intra_mode != V_PRED &&
  958. best_intra_mode != D135_PRED)
  959. return 1;
  960. if (mode == D63_PRED && best_intra_mode != V_PRED &&
  961. best_intra_mode != D45_PRED)
  962. return 1;
  963. if (mode == D207_PRED && best_intra_mode != H_PRED &&
  964. best_intra_mode != D45_PRED)
  965. return 1;
  966. if (mode == D153_PRED && best_intra_mode != H_PRED &&
  967. best_intra_mode != D135_PRED)
  968. return 1;
  969. return 0;
  970. }
  971. static int64_t rd_pick_intra4x4block(VP9_COMP *cpi, MACROBLOCK *x, int row,
  972. int col, PREDICTION_MODE *best_mode,
  973. const int *bmode_costs, ENTROPY_CONTEXT *a,
  974. ENTROPY_CONTEXT *l, int *bestrate,
  975. int *bestratey, int64_t *bestdistortion,
  976. BLOCK_SIZE bsize, int64_t rd_thresh) {
  977. PREDICTION_MODE mode;
  978. MACROBLOCKD *const xd = &x->e_mbd;
  979. int64_t best_rd = rd_thresh;
  980. struct macroblock_plane *p = &x->plane[0];
  981. struct macroblockd_plane *pd = &xd->plane[0];
  982. const int src_stride = p->src.stride;
  983. const int dst_stride = pd->dst.stride;
  984. const uint8_t *src_init = &p->src.buf[row * 4 * src_stride + col * 4];
  985. uint8_t *dst_init = &pd->dst.buf[row * 4 * src_stride + col * 4];
  986. ENTROPY_CONTEXT ta[2], tempa[2];
  987. ENTROPY_CONTEXT tl[2], templ[2];
  988. const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
  989. const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
  990. int idx, idy;
  991. uint8_t best_dst[8 * 8];
  992. #if CONFIG_VP9_HIGHBITDEPTH
  993. uint16_t best_dst16[8 * 8];
  994. #endif
  995. memcpy(ta, a, num_4x4_blocks_wide * sizeof(a[0]));
  996. memcpy(tl, l, num_4x4_blocks_high * sizeof(l[0]));
  997. xd->mi[0]->tx_size = TX_4X4;
  998. #if CONFIG_VP9_HIGHBITDEPTH
  999. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  1000. for (mode = DC_PRED; mode <= TM_PRED; ++mode) {
  1001. int64_t this_rd;
  1002. int ratey = 0;
  1003. int64_t distortion = 0;
  1004. int rate = bmode_costs[mode];
  1005. if (!(cpi->sf.intra_y_mode_mask[TX_4X4] & (1 << mode))) continue;
  1006. // Only do the oblique modes if the best so far is
  1007. // one of the neighboring directional modes
  1008. if (cpi->sf.mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) {
  1009. if (conditional_skipintra(mode, *best_mode)) continue;
  1010. }
  1011. memcpy(tempa, ta, num_4x4_blocks_wide * sizeof(ta[0]));
  1012. memcpy(templ, tl, num_4x4_blocks_high * sizeof(tl[0]));
  1013. for (idy = 0; idy < num_4x4_blocks_high; ++idy) {
  1014. for (idx = 0; idx < num_4x4_blocks_wide; ++idx) {
  1015. const int block = (row + idy) * 2 + (col + idx);
  1016. const uint8_t *const src = &src_init[idx * 4 + idy * 4 * src_stride];
  1017. uint8_t *const dst = &dst_init[idx * 4 + idy * 4 * dst_stride];
  1018. uint16_t *const dst16 = CONVERT_TO_SHORTPTR(dst);
  1019. int16_t *const src_diff =
  1020. vp9_raster_block_offset_int16(BLOCK_8X8, block, p->src_diff);
  1021. tran_low_t *const coeff = BLOCK_OFFSET(x->plane[0].coeff, block);
  1022. xd->mi[0]->bmi[block].as_mode = mode;
  1023. vp9_predict_intra_block(xd, 1, TX_4X4, mode,
  1024. x->skip_encode ? src : dst,
  1025. x->skip_encode ? src_stride : dst_stride, dst,
  1026. dst_stride, col + idx, row + idy, 0);
  1027. vpx_highbd_subtract_block(4, 4, src_diff, 8, src, src_stride, dst,
  1028. dst_stride, xd->bd);
  1029. if (xd->lossless) {
  1030. const scan_order *so = &vp9_default_scan_orders[TX_4X4];
  1031. const int coeff_ctx =
  1032. combine_entropy_contexts(tempa[idx], templ[idy]);
  1033. vp9_highbd_fwht4x4(src_diff, coeff, 8);
  1034. vp9_regular_quantize_b_4x4(x, 0, block, so->scan, so->iscan);
  1035. ratey += cost_coeffs(x, 0, block, TX_4X4, coeff_ctx, so->scan,
  1036. so->neighbors, cpi->sf.use_fast_coef_costing);
  1037. tempa[idx] = templ[idy] = (x->plane[0].eobs[block] > 0 ? 1 : 0);
  1038. if (RDCOST(x->rdmult, x->rddiv, ratey, distortion) >= best_rd)
  1039. goto next_highbd;
  1040. vp9_highbd_iwht4x4_add(BLOCK_OFFSET(pd->dqcoeff, block), dst16,
  1041. dst_stride, p->eobs[block], xd->bd);
  1042. } else {
  1043. int64_t unused;
  1044. const TX_TYPE tx_type = get_tx_type_4x4(PLANE_TYPE_Y, xd, block);
  1045. const scan_order *so = &vp9_scan_orders[TX_4X4][tx_type];
  1046. const int coeff_ctx =
  1047. combine_entropy_contexts(tempa[idx], templ[idy]);
  1048. if (tx_type == DCT_DCT)
  1049. vpx_highbd_fdct4x4(src_diff, coeff, 8);
  1050. else
  1051. vp9_highbd_fht4x4(src_diff, coeff, 8, tx_type);
  1052. vp9_regular_quantize_b_4x4(x, 0, block, so->scan, so->iscan);
  1053. ratey += cost_coeffs(x, 0, block, TX_4X4, coeff_ctx, so->scan,
  1054. so->neighbors, cpi->sf.use_fast_coef_costing);
  1055. distortion += vp9_highbd_block_error_dispatch(
  1056. coeff, BLOCK_OFFSET(pd->dqcoeff, block), 16,
  1057. &unused, xd->bd) >>
  1058. 2;
  1059. tempa[idx] = templ[idy] = (x->plane[0].eobs[block] > 0 ? 1 : 0);
  1060. if (RDCOST(x->rdmult, x->rddiv, ratey, distortion) >= best_rd)
  1061. goto next_highbd;
  1062. vp9_highbd_iht4x4_add(tx_type, BLOCK_OFFSET(pd->dqcoeff, block),
  1063. dst16, dst_stride, p->eobs[block], xd->bd);
  1064. }
  1065. }
  1066. }
  1067. rate += ratey;
  1068. this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
  1069. if (this_rd < best_rd) {
  1070. *bestrate = rate;
  1071. *bestratey = ratey;
  1072. *bestdistortion = distortion;
  1073. best_rd = this_rd;
  1074. *best_mode = mode;
  1075. memcpy(a, tempa, num_4x4_blocks_wide * sizeof(tempa[0]));
  1076. memcpy(l, templ, num_4x4_blocks_high * sizeof(templ[0]));
  1077. for (idy = 0; idy < num_4x4_blocks_high * 4; ++idy) {
  1078. memcpy(best_dst16 + idy * 8,
  1079. CONVERT_TO_SHORTPTR(dst_init + idy * dst_stride),
  1080. num_4x4_blocks_wide * 4 * sizeof(uint16_t));
  1081. }
  1082. }
  1083. next_highbd : {}
  1084. }
  1085. if (best_rd >= rd_thresh || x->skip_encode) return best_rd;
  1086. for (idy = 0; idy < num_4x4_blocks_high * 4; ++idy) {
  1087. memcpy(CONVERT_TO_SHORTPTR(dst_init + idy * dst_stride),
  1088. best_dst16 + idy * 8, num_4x4_blocks_wide * 4 * sizeof(uint16_t));
  1089. }
  1090. return best_rd;
  1091. }
  1092. #endif // CONFIG_VP9_HIGHBITDEPTH
  1093. for (mode = DC_PRED; mode <= TM_PRED; ++mode) {
  1094. int64_t this_rd;
  1095. int ratey = 0;
  1096. int64_t distortion = 0;
  1097. int rate = bmode_costs[mode];
  1098. if (!(cpi->sf.intra_y_mode_mask[TX_4X4] & (1 << mode))) continue;
  1099. // Only do the oblique modes if the best so far is
  1100. // one of the neighboring directional modes
  1101. if (cpi->sf.mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) {
  1102. if (conditional_skipintra(mode, *best_mode)) continue;
  1103. }
  1104. memcpy(tempa, ta, num_4x4_blocks_wide * sizeof(ta[0]));
  1105. memcpy(templ, tl, num_4x4_blocks_high * sizeof(tl[0]));
  1106. for (idy = 0; idy < num_4x4_blocks_high; ++idy) {
  1107. for (idx = 0; idx < num_4x4_blocks_wide; ++idx) {
  1108. const int block = (row + idy) * 2 + (col + idx);
  1109. const uint8_t *const src = &src_init[idx * 4 + idy * 4 * src_stride];
  1110. uint8_t *const dst = &dst_init[idx * 4 + idy * 4 * dst_stride];
  1111. int16_t *const src_diff =
  1112. vp9_raster_block_offset_int16(BLOCK_8X8, block, p->src_diff);
  1113. tran_low_t *const coeff = BLOCK_OFFSET(x->plane[0].coeff, block);
  1114. xd->mi[0]->bmi[block].as_mode = mode;
  1115. vp9_predict_intra_block(xd, 1, TX_4X4, mode, x->skip_encode ? src : dst,
  1116. x->skip_encode ? src_stride : dst_stride, dst,
  1117. dst_stride, col + idx, row + idy, 0);
  1118. vpx_subtract_block(4, 4, src_diff, 8, src, src_stride, dst, dst_stride);
  1119. if (xd->lossless) {
  1120. const scan_order *so = &vp9_default_scan_orders[TX_4X4];
  1121. const int coeff_ctx =
  1122. combine_entropy_contexts(tempa[idx], templ[idy]);
  1123. vp9_fwht4x4(src_diff, coeff, 8);
  1124. vp9_regular_quantize_b_4x4(x, 0, block, so->scan, so->iscan);
  1125. ratey += cost_coeffs(x, 0, block, TX_4X4, coeff_ctx, so->scan,
  1126. so->neighbors, cpi->sf.use_fast_coef_costing);
  1127. tempa[idx] = templ[idy] = (x->plane[0].eobs[block] > 0) ? 1 : 0;
  1128. if (RDCOST(x->rdmult, x->rddiv, ratey, distortion) >= best_rd)
  1129. goto next;
  1130. vp9_iwht4x4_add(BLOCK_OFFSET(pd->dqcoeff, block), dst, dst_stride,
  1131. p->eobs[block]);
  1132. } else {
  1133. int64_t unused;
  1134. const TX_TYPE tx_type = get_tx_type_4x4(PLANE_TYPE_Y, xd, block);
  1135. const scan_order *so = &vp9_scan_orders[TX_4X4][tx_type];
  1136. const int coeff_ctx =
  1137. combine_entropy_contexts(tempa[idx], templ[idy]);
  1138. vp9_fht4x4(src_diff, coeff, 8, tx_type);
  1139. vp9_regular_quantize_b_4x4(x, 0, block, so->scan, so->iscan);
  1140. ratey += cost_coeffs(x, 0, block, TX_4X4, coeff_ctx, so->scan,
  1141. so->neighbors, cpi->sf.use_fast_coef_costing);
  1142. tempa[idx] = templ[idy] = (x->plane[0].eobs[block] > 0) ? 1 : 0;
  1143. distortion += vp9_block_error(coeff, BLOCK_OFFSET(pd->dqcoeff, block),
  1144. 16, &unused) >>
  1145. 2;
  1146. if (RDCOST(x->rdmult, x->rddiv, ratey, distortion) >= best_rd)
  1147. goto next;
  1148. vp9_iht4x4_add(tx_type, BLOCK_OFFSET(pd->dqcoeff, block), dst,
  1149. dst_stride, p->eobs[block]);
  1150. }
  1151. }
  1152. }
  1153. rate += ratey;
  1154. this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
  1155. if (this_rd < best_rd) {
  1156. *bestrate = rate;
  1157. *bestratey = ratey;
  1158. *bestdistortion = distortion;
  1159. best_rd = this_rd;
  1160. *best_mode = mode;
  1161. memcpy(a, tempa, num_4x4_blocks_wide * sizeof(tempa[0]));
  1162. memcpy(l, templ, num_4x4_blocks_high * sizeof(templ[0]));
  1163. for (idy = 0; idy < num_4x4_blocks_high * 4; ++idy)
  1164. memcpy(best_dst + idy * 8, dst_init + idy * dst_stride,
  1165. num_4x4_blocks_wide * 4);
  1166. }
  1167. next : {}
  1168. }
  1169. if (best_rd >= rd_thresh || x->skip_encode) return best_rd;
  1170. for (idy = 0; idy < num_4x4_blocks_high * 4; ++idy)
  1171. memcpy(dst_init + idy * dst_stride, best_dst + idy * 8,
  1172. num_4x4_blocks_wide * 4);
  1173. return best_rd;
  1174. }
  1175. static int64_t rd_pick_intra_sub_8x8_y_mode(VP9_COMP *cpi, MACROBLOCK *mb,
  1176. int *rate, int *rate_y,
  1177. int64_t *distortion,
  1178. int64_t best_rd) {
  1179. int i, j;
  1180. const MACROBLOCKD *const xd = &mb->e_mbd;
  1181. MODE_INFO *const mic = xd->mi[0];
  1182. const MODE_INFO *above_mi = xd->above_mi;
  1183. const MODE_INFO *left_mi = xd->left_mi;
  1184. const BLOCK_SIZE bsize = xd->mi[0]->sb_type;
  1185. const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
  1186. const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
  1187. int idx, idy;
  1188. int cost = 0;
  1189. int64_t total_distortion = 0;
  1190. int tot_rate_y = 0;
  1191. int64_t total_rd = 0;
  1192. const int *bmode_costs = cpi->mbmode_cost;
  1193. // Pick modes for each sub-block (of size 4x4, 4x8, or 8x4) in an 8x8 block.
  1194. for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
  1195. for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
  1196. PREDICTION_MODE best_mode = DC_PRED;
  1197. int r = INT_MAX, ry = INT_MAX;
  1198. int64_t d = INT64_MAX, this_rd = INT64_MAX;
  1199. i = idy * 2 + idx;
  1200. if (cpi->common.frame_type == KEY_FRAME) {
  1201. const PREDICTION_MODE A = vp9_above_block_mode(mic, above_mi, i);
  1202. const PREDICTION_MODE L = vp9_left_block_mode(mic, left_mi, i);
  1203. bmode_costs = cpi->y_mode_costs[A][L];
  1204. }
  1205. this_rd = rd_pick_intra4x4block(
  1206. cpi, mb, idy, idx, &best_mode, bmode_costs,
  1207. xd->plane[0].above_context + idx, xd->plane[0].left_context + idy, &r,
  1208. &ry, &d, bsize, best_rd - total_rd);
  1209. if (this_rd >= best_rd - total_rd) return INT64_MAX;
  1210. total_rd += this_rd;
  1211. cost += r;
  1212. total_distortion += d;
  1213. tot_rate_y += ry;
  1214. mic->bmi[i].as_mode = best_mode;
  1215. for (j = 1; j < num_4x4_blocks_high; ++j)
  1216. mic->bmi[i + j * 2].as_mode = best_mode;
  1217. for (j = 1; j < num_4x4_blocks_wide; ++j)
  1218. mic->bmi[i + j].as_mode = best_mode;
  1219. if (total_rd >= best_rd) return INT64_MAX;
  1220. }
  1221. }
  1222. *rate = cost;
  1223. *rate_y = tot_rate_y;
  1224. *distortion = total_distortion;
  1225. mic->mode = mic->bmi[3].as_mode;
  1226. return RDCOST(mb->rdmult, mb->rddiv, cost, total_distortion);
  1227. }
  1228. // This function is used only for intra_only frames
  1229. static int64_t rd_pick_intra_sby_mode(VP9_COMP *cpi, MACROBLOCK *x, int *rate,
  1230. int *rate_tokenonly, int64_t *distortion,
  1231. int *skippable, BLOCK_SIZE bsize,
  1232. int64_t best_rd) {
  1233. PREDICTION_MODE mode;
  1234. PREDICTION_MODE mode_selected = DC_PRED;
  1235. MACROBLOCKD *const xd = &x->e_mbd;
  1236. MODE_INFO *const mic = xd->mi[0];
  1237. int this_rate, this_rate_tokenonly, s;
  1238. int64_t this_distortion, this_rd;
  1239. TX_SIZE best_tx = TX_4X4;
  1240. int *bmode_costs;
  1241. const MODE_INFO *above_mi = xd->above_mi;
  1242. const MODE_INFO *left_mi = xd->left_mi;
  1243. const PREDICTION_MODE A = vp9_above_block_mode(mic, above_mi, 0);
  1244. const PREDICTION_MODE L = vp9_left_block_mode(mic, left_mi, 0);
  1245. bmode_costs = cpi->y_mode_costs[A][L];
  1246. memset(x->skip_txfm, SKIP_TXFM_NONE, sizeof(x->skip_txfm));
  1247. /* Y Search for intra prediction mode */
  1248. for (mode = DC_PRED; mode <= TM_PRED; mode++) {
  1249. if (cpi->sf.use_nonrd_pick_mode) {
  1250. // These speed features are turned on in hybrid non-RD and RD mode
  1251. // for key frame coding in the context of real-time setting.
  1252. if (conditional_skipintra(mode, mode_selected)) continue;
  1253. if (*skippable) break;
  1254. }
  1255. mic->mode = mode;
  1256. super_block_yrd(cpi, x, &this_rate_tokenonly, &this_distortion, &s, NULL,
  1257. bsize, best_rd, /*recon = */ 0);
  1258. if (this_rate_tokenonly == INT_MAX) continue;
  1259. this_rate = this_rate_tokenonly + bmode_costs[mode];
  1260. this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion);
  1261. if (this_rd < best_rd) {
  1262. mode_selected = mode;
  1263. best_rd = this_rd;
  1264. best_tx = mic->tx_size;
  1265. *rate = this_rate;
  1266. *rate_tokenonly = this_rate_tokenonly;
  1267. *distortion = this_distortion;
  1268. *skippable = s;
  1269. }
  1270. }
  1271. mic->mode = mode_selected;
  1272. mic->tx_size = best_tx;
  1273. return best_rd;
  1274. }
  1275. // Return value 0: early termination triggered, no valid rd cost available;
  1276. // 1: rd cost values are valid.
  1277. static int super_block_uvrd(const VP9_COMP *cpi, MACROBLOCK *x, int *rate,
  1278. int64_t *distortion, int *skippable, int64_t *sse,
  1279. BLOCK_SIZE bsize, int64_t ref_best_rd) {
  1280. MACROBLOCKD *const xd = &x->e_mbd;
  1281. MODE_INFO *const mi = xd->mi[0];
  1282. const TX_SIZE uv_tx_size = get_uv_tx_size(mi, &xd->plane[1]);
  1283. int plane;
  1284. int pnrate = 0, pnskip = 1;
  1285. int64_t pndist = 0, pnsse = 0;
  1286. int is_cost_valid = 1;
  1287. if (ref_best_rd < 0) is_cost_valid = 0;
  1288. if (is_inter_block(mi) && is_cost_valid) {
  1289. int plane;
  1290. for (plane = 1; plane < MAX_MB_PLANE; ++plane)
  1291. vp9_subtract_plane(x, bsize, plane);
  1292. }
  1293. *rate = 0;
  1294. *distortion = 0;
  1295. *sse = 0;
  1296. *skippable = 1;
  1297. for (plane = 1; plane < MAX_MB_PLANE; ++plane) {
  1298. txfm_rd_in_plane(cpi, x, &pnrate, &pndist, &pnskip, &pnsse, ref_best_rd,
  1299. plane, bsize, uv_tx_size, cpi->sf.use_fast_coef_costing,
  1300. /*recon = */ 0);
  1301. if (pnrate == INT_MAX) {
  1302. is_cost_valid = 0;
  1303. break;
  1304. }
  1305. *rate += pnrate;
  1306. *distortion += pndist;
  1307. *sse += pnsse;
  1308. *skippable &= pnskip;
  1309. }
  1310. if (!is_cost_valid) {
  1311. // reset cost value
  1312. *rate = INT_MAX;
  1313. *distortion = INT64_MAX;
  1314. *sse = INT64_MAX;
  1315. *skippable = 0;
  1316. }
  1317. return is_cost_valid;
  1318. }
  1319. static int64_t rd_pick_intra_sbuv_mode(VP9_COMP *cpi, MACROBLOCK *x,
  1320. PICK_MODE_CONTEXT *ctx, int *rate,
  1321. int *rate_tokenonly, int64_t *distortion,
  1322. int *skippable, BLOCK_SIZE bsize,
  1323. TX_SIZE max_tx_size) {
  1324. MACROBLOCKD *xd = &x->e_mbd;
  1325. PREDICTION_MODE mode;
  1326. PREDICTION_MODE mode_selected = DC_PRED;
  1327. int64_t best_rd = INT64_MAX, this_rd;
  1328. int this_rate_tokenonly, this_rate, s;
  1329. int64_t this_distortion, this_sse;
  1330. memset(x->skip_txfm, SKIP_TXFM_NONE, sizeof(x->skip_txfm));
  1331. for (mode = DC_PRED; mode <= TM_PRED; ++mode) {
  1332. if (!(cpi->sf.intra_uv_mode_mask[max_tx_size] & (1 << mode))) continue;
  1333. #if CONFIG_BETTER_HW_COMPATIBILITY && CONFIG_VP9_HIGHBITDEPTH
  1334. if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) &&
  1335. (xd->above_mi == NULL || xd->left_mi == NULL) && need_top_left[mode])
  1336. continue;
  1337. #endif // CONFIG_BETTER_HW_COMPATIBILITY && CONFIG_VP9_HIGHBITDEPTH
  1338. xd->mi[0]->uv_mode = mode;
  1339. if (!super_block_uvrd(cpi, x, &this_rate_tokenonly, &this_distortion, &s,
  1340. &this_sse, bsize, best_rd))
  1341. continue;
  1342. this_rate =
  1343. this_rate_tokenonly +
  1344. cpi->intra_uv_mode_cost[cpi->common.frame_type][xd->mi[0]->mode][mode];
  1345. this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion);
  1346. if (this_rd < best_rd) {
  1347. mode_selected = mode;
  1348. best_rd = this_rd;
  1349. *rate = this_rate;
  1350. *rate_tokenonly = this_rate_tokenonly;
  1351. *distortion = this_distortion;
  1352. *skippable = s;
  1353. if (!x->select_tx_size) swap_block_ptr(x, ctx, 2, 0, 1, MAX_MB_PLANE);
  1354. }
  1355. }
  1356. xd->mi[0]->uv_mode = mode_selected;
  1357. return best_rd;
  1358. }
  1359. #if !CONFIG_REALTIME_ONLY
  1360. static int64_t rd_sbuv_dcpred(const VP9_COMP *cpi, MACROBLOCK *x, int *rate,
  1361. int *rate_tokenonly, int64_t *distortion,
  1362. int *skippable, BLOCK_SIZE bsize) {
  1363. const VP9_COMMON *cm = &cpi->common;
  1364. int64_t unused;
  1365. x->e_mbd.mi[0]->uv_mode = DC_PRED;
  1366. memset(x->skip_txfm, SKIP_TXFM_NONE, sizeof(x->skip_txfm));
  1367. super_block_uvrd(cpi, x, rate_tokenonly, distortion, skippable, &unused,
  1368. bsize, INT64_MAX);
  1369. *rate =
  1370. *rate_tokenonly +
  1371. cpi->intra_uv_mode_cost[cm->frame_type][x->e_mbd.mi[0]->mode][DC_PRED];
  1372. return RDCOST(x->rdmult, x->rddiv, *rate, *distortion);
  1373. }
  1374. static void choose_intra_uv_mode(VP9_COMP *cpi, MACROBLOCK *const x,
  1375. PICK_MODE_CONTEXT *ctx, BLOCK_SIZE bsize,
  1376. TX_SIZE max_tx_size, int *rate_uv,
  1377. int *rate_uv_tokenonly, int64_t *dist_uv,
  1378. int *skip_uv, PREDICTION_MODE *mode_uv) {
  1379. // Use an estimated rd for uv_intra based on DC_PRED if the
  1380. // appropriate speed flag is set.
  1381. if (cpi->sf.use_uv_intra_rd_estimate) {
  1382. rd_sbuv_dcpred(cpi, x, rate_uv, rate_uv_tokenonly, dist_uv, skip_uv,
  1383. bsize < BLOCK_8X8 ? BLOCK_8X8 : bsize);
  1384. // Else do a proper rd search for each possible transform size that may
  1385. // be considered in the main rd loop.
  1386. } else {
  1387. rd_pick_intra_sbuv_mode(cpi, x, ctx, rate_uv, rate_uv_tokenonly, dist_uv,
  1388. skip_uv, bsize < BLOCK_8X8 ? BLOCK_8X8 : bsize,
  1389. max_tx_size);
  1390. }
  1391. *mode_uv = x->e_mbd.mi[0]->uv_mode;
  1392. }
  1393. static int cost_mv_ref(const VP9_COMP *cpi, PREDICTION_MODE mode,
  1394. int mode_context) {
  1395. assert(is_inter_mode(mode));
  1396. return cpi->inter_mode_cost[mode_context][INTER_OFFSET(mode)];
  1397. }
  1398. static int set_and_cost_bmi_mvs(VP9_COMP *cpi, MACROBLOCK *x, MACROBLOCKD *xd,
  1399. int i, PREDICTION_MODE mode, int_mv this_mv[2],
  1400. int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES],
  1401. int_mv seg_mvs[MAX_REF_FRAMES],
  1402. int_mv *best_ref_mv[2], const int *mvjcost,
  1403. int *mvcost[2]) {
  1404. MODE_INFO *const mi = xd->mi[0];
  1405. const MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
  1406. int thismvcost = 0;
  1407. int idx, idy;
  1408. const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[mi->sb_type];
  1409. const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[mi->sb_type];
  1410. const int is_compound = has_second_ref(mi);
  1411. switch (mode) {
  1412. case NEWMV:
  1413. this_mv[0].as_int = seg_mvs[mi->ref_frame[0]].as_int;
  1414. thismvcost += vp9_mv_bit_cost(&this_mv[0].as_mv, &best_ref_mv[0]->as_mv,
  1415. mvjcost, mvcost, MV_COST_WEIGHT_SUB);
  1416. if (is_compound) {
  1417. this_mv[1].as_int = seg_mvs[mi->ref_frame[1]].as_int;
  1418. thismvcost += vp9_mv_bit_cost(&this_mv[1].as_mv, &best_ref_mv[1]->as_mv,
  1419. mvjcost, mvcost, MV_COST_WEIGHT_SUB);
  1420. }
  1421. break;
  1422. case NEARMV:
  1423. case NEARESTMV:
  1424. this_mv[0].as_int = frame_mv[mode][mi->ref_frame[0]].as_int;
  1425. if (is_compound)
  1426. this_mv[1].as_int = frame_mv[mode][mi->ref_frame[1]].as_int;
  1427. break;
  1428. default:
  1429. assert(mode == ZEROMV);
  1430. this_mv[0].as_int = 0;
  1431. if (is_compound) this_mv[1].as_int = 0;
  1432. break;
  1433. }
  1434. mi->bmi[i].as_mv[0].as_int = this_mv[0].as_int;
  1435. if (is_compound) mi->bmi[i].as_mv[1].as_int = this_mv[1].as_int;
  1436. mi->bmi[i].as_mode = mode;
  1437. for (idy = 0; idy < num_4x4_blocks_high; ++idy)
  1438. for (idx = 0; idx < num_4x4_blocks_wide; ++idx)
  1439. memmove(&mi->bmi[i + idy * 2 + idx], &mi->bmi[i], sizeof(mi->bmi[i]));
  1440. return cost_mv_ref(cpi, mode, mbmi_ext->mode_context[mi->ref_frame[0]]) +
  1441. thismvcost;
  1442. }
  1443. static int64_t encode_inter_mb_segment(VP9_COMP *cpi, MACROBLOCK *x,
  1444. int64_t best_yrd, int i, int *labelyrate,
  1445. int64_t *distortion, int64_t *sse,
  1446. ENTROPY_CONTEXT *ta, ENTROPY_CONTEXT *tl,
  1447. int mi_row, int mi_col) {
  1448. int k;
  1449. MACROBLOCKD *xd = &x->e_mbd;
  1450. struct macroblockd_plane *const pd = &xd->plane[0];
  1451. struct macroblock_plane *const p = &x->plane[0];
  1452. MODE_INFO *const mi = xd->mi[0];
  1453. const BLOCK_SIZE plane_bsize = get_plane_block_size(mi->sb_type, pd);
  1454. const int width = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  1455. const int height = 4 * num_4x4_blocks_high_lookup[plane_bsize];
  1456. int idx, idy;
  1457. const uint8_t *const src =
  1458. &p->src.buf[vp9_raster_block_offset(BLOCK_8X8, i, p->src.stride)];
  1459. uint8_t *const dst =
  1460. &pd->dst.buf[vp9_raster_block_offset(BLOCK_8X8, i, pd->dst.stride)];
  1461. int64_t thisdistortion = 0, thissse = 0;
  1462. int thisrate = 0, ref;
  1463. const scan_order *so = &vp9_default_scan_orders[TX_4X4];
  1464. const int is_compound = has_second_ref(mi);
  1465. const InterpKernel *kernel = vp9_filter_kernels[mi->interp_filter];
  1466. for (ref = 0; ref < 1 + is_compound; ++ref) {
  1467. const int bw = b_width_log2_lookup[BLOCK_8X8];
  1468. const int h = 4 * (i >> bw);
  1469. const int w = 4 * (i & ((1 << bw) - 1));
  1470. const struct scale_factors *sf = &xd->block_refs[ref]->sf;
  1471. int y_stride = pd->pre[ref].stride;
  1472. uint8_t *pre = pd->pre[ref].buf + (h * pd->pre[ref].stride + w);
  1473. if (vp9_is_scaled(sf)) {
  1474. const int x_start = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x));
  1475. const int y_start = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y));
  1476. y_stride = xd->block_refs[ref]->buf->y_stride;
  1477. pre = xd->block_refs[ref]->buf->y_buffer;
  1478. pre += scaled_buffer_offset(x_start + w, y_start + h, y_stride, sf);
  1479. }
  1480. #if CONFIG_VP9_HIGHBITDEPTH
  1481. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  1482. vp9_highbd_build_inter_predictor(
  1483. CONVERT_TO_SHORTPTR(pre), y_stride, CONVERT_TO_SHORTPTR(dst),
  1484. pd->dst.stride, &mi->bmi[i].as_mv[ref].as_mv,
  1485. &xd->block_refs[ref]->sf, width, height, ref, kernel, MV_PRECISION_Q3,
  1486. mi_col * MI_SIZE + 4 * (i % 2), mi_row * MI_SIZE + 4 * (i / 2),
  1487. xd->bd);
  1488. } else {
  1489. vp9_build_inter_predictor(
  1490. pre, y_stride, dst, pd->dst.stride, &mi->bmi[i].as_mv[ref].as_mv,
  1491. &xd->block_refs[ref]->sf, width, height, ref, kernel, MV_PRECISION_Q3,
  1492. mi_col * MI_SIZE + 4 * (i % 2), mi_row * MI_SIZE + 4 * (i / 2));
  1493. }
  1494. #else
  1495. vp9_build_inter_predictor(
  1496. pre, y_stride, dst, pd->dst.stride, &mi->bmi[i].as_mv[ref].as_mv,
  1497. &xd->block_refs[ref]->sf, width, height, ref, kernel, MV_PRECISION_Q3,
  1498. mi_col * MI_SIZE + 4 * (i % 2), mi_row * MI_SIZE + 4 * (i / 2));
  1499. #endif // CONFIG_VP9_HIGHBITDEPTH
  1500. }
  1501. #if CONFIG_VP9_HIGHBITDEPTH
  1502. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  1503. vpx_highbd_subtract_block(
  1504. height, width, vp9_raster_block_offset_int16(BLOCK_8X8, i, p->src_diff),
  1505. 8, src, p->src.stride, dst, pd->dst.stride, xd->bd);
  1506. } else {
  1507. vpx_subtract_block(height, width,
  1508. vp9_raster_block_offset_int16(BLOCK_8X8, i, p->src_diff),
  1509. 8, src, p->src.stride, dst, pd->dst.stride);
  1510. }
  1511. #else
  1512. vpx_subtract_block(height, width,
  1513. vp9_raster_block_offset_int16(BLOCK_8X8, i, p->src_diff),
  1514. 8, src, p->src.stride, dst, pd->dst.stride);
  1515. #endif // CONFIG_VP9_HIGHBITDEPTH
  1516. k = i;
  1517. for (idy = 0; idy < height / 4; ++idy) {
  1518. for (idx = 0; idx < width / 4; ++idx) {
  1519. #if CONFIG_VP9_HIGHBITDEPTH
  1520. const int bd = (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? xd->bd : 8;
  1521. #endif
  1522. int64_t ssz, rd, rd1, rd2;
  1523. tran_low_t *coeff;
  1524. int coeff_ctx;
  1525. k += (idy * 2 + idx);
  1526. coeff_ctx = combine_entropy_contexts(ta[k & 1], tl[k >> 1]);
  1527. coeff = BLOCK_OFFSET(p->coeff, k);
  1528. x->fwd_txfm4x4(vp9_raster_block_offset_int16(BLOCK_8X8, k, p->src_diff),
  1529. coeff, 8);
  1530. vp9_regular_quantize_b_4x4(x, 0, k, so->scan, so->iscan);
  1531. #if CONFIG_VP9_HIGHBITDEPTH
  1532. thisdistortion += vp9_highbd_block_error_dispatch(
  1533. coeff, BLOCK_OFFSET(pd->dqcoeff, k), 16, &ssz, bd);
  1534. #else
  1535. thisdistortion +=
  1536. vp9_block_error(coeff, BLOCK_OFFSET(pd->dqcoeff, k), 16, &ssz);
  1537. #endif // CONFIG_VP9_HIGHBITDEPTH
  1538. thissse += ssz;
  1539. thisrate += cost_coeffs(x, 0, k, TX_4X4, coeff_ctx, so->scan,
  1540. so->neighbors, cpi->sf.use_fast_coef_costing);
  1541. ta[k & 1] = tl[k >> 1] = (x->plane[0].eobs[k] > 0) ? 1 : 0;
  1542. rd1 = RDCOST(x->rdmult, x->rddiv, thisrate, thisdistortion >> 2);
  1543. rd2 = RDCOST(x->rdmult, x->rddiv, 0, thissse >> 2);
  1544. rd = VPXMIN(rd1, rd2);
  1545. if (rd >= best_yrd) return INT64_MAX;
  1546. }
  1547. }
  1548. *distortion = thisdistortion >> 2;
  1549. *labelyrate = thisrate;
  1550. *sse = thissse >> 2;
  1551. return RDCOST(x->rdmult, x->rddiv, *labelyrate, *distortion);
  1552. }
  1553. #endif // !CONFIG_REALTIME_ONLY
  1554. typedef struct {
  1555. int eobs;
  1556. int brate;
  1557. int byrate;
  1558. int64_t bdist;
  1559. int64_t bsse;
  1560. int64_t brdcost;
  1561. int_mv mvs[2];
  1562. ENTROPY_CONTEXT ta[2];
  1563. ENTROPY_CONTEXT tl[2];
  1564. } SEG_RDSTAT;
  1565. typedef struct {
  1566. int_mv *ref_mv[2];
  1567. int_mv mvp;
  1568. int64_t segment_rd;
  1569. int r;
  1570. int64_t d;
  1571. int64_t sse;
  1572. int segment_yrate;
  1573. PREDICTION_MODE modes[4];
  1574. SEG_RDSTAT rdstat[4][INTER_MODES];
  1575. int mvthresh;
  1576. } BEST_SEG_INFO;
  1577. #if !CONFIG_REALTIME_ONLY
  1578. static INLINE int mv_check_bounds(const MvLimits *mv_limits, const MV *mv) {
  1579. return (mv->row >> 3) < mv_limits->row_min ||
  1580. (mv->row >> 3) > mv_limits->row_max ||
  1581. (mv->col >> 3) < mv_limits->col_min ||
  1582. (mv->col >> 3) > mv_limits->col_max;
  1583. }
  1584. static INLINE void mi_buf_shift(MACROBLOCK *x, int i) {
  1585. MODE_INFO *const mi = x->e_mbd.mi[0];
  1586. struct macroblock_plane *const p = &x->plane[0];
  1587. struct macroblockd_plane *const pd = &x->e_mbd.plane[0];
  1588. p->src.buf =
  1589. &p->src.buf[vp9_raster_block_offset(BLOCK_8X8, i, p->src.stride)];
  1590. assert(((intptr_t)pd->pre[0].buf & 0x7) == 0);
  1591. pd->pre[0].buf =
  1592. &pd->pre[0].buf[vp9_raster_block_offset(BLOCK_8X8, i, pd->pre[0].stride)];
  1593. if (has_second_ref(mi))
  1594. pd->pre[1].buf =
  1595. &pd->pre[1]
  1596. .buf[vp9_raster_block_offset(BLOCK_8X8, i, pd->pre[1].stride)];
  1597. }
  1598. static INLINE void mi_buf_restore(MACROBLOCK *x, struct buf_2d orig_src,
  1599. struct buf_2d orig_pre[2]) {
  1600. MODE_INFO *mi = x->e_mbd.mi[0];
  1601. x->plane[0].src = orig_src;
  1602. x->e_mbd.plane[0].pre[0] = orig_pre[0];
  1603. if (has_second_ref(mi)) x->e_mbd.plane[0].pre[1] = orig_pre[1];
  1604. }
  1605. static INLINE int mv_has_subpel(const MV *mv) {
  1606. return (mv->row & 0x0F) || (mv->col & 0x0F);
  1607. }
  1608. // Check if NEARESTMV/NEARMV/ZEROMV is the cheapest way encode zero motion.
  1609. // TODO(aconverse): Find out if this is still productive then clean up or remove
  1610. static int check_best_zero_mv(const VP9_COMP *cpi,
  1611. const uint8_t mode_context[MAX_REF_FRAMES],
  1612. int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES],
  1613. int this_mode,
  1614. const MV_REFERENCE_FRAME ref_frames[2]) {
  1615. if ((this_mode == NEARMV || this_mode == NEARESTMV || this_mode == ZEROMV) &&
  1616. frame_mv[this_mode][ref_frames[0]].as_int == 0 &&
  1617. (ref_frames[1] == NONE ||
  1618. frame_mv[this_mode][ref_frames[1]].as_int == 0)) {
  1619. int rfc = mode_context[ref_frames[0]];
  1620. int c1 = cost_mv_ref(cpi, NEARMV, rfc);
  1621. int c2 = cost_mv_ref(cpi, NEARESTMV, rfc);
  1622. int c3 = cost_mv_ref(cpi, ZEROMV, rfc);
  1623. if (this_mode == NEARMV) {
  1624. if (c1 > c3) return 0;
  1625. } else if (this_mode == NEARESTMV) {
  1626. if (c2 > c3) return 0;
  1627. } else {
  1628. assert(this_mode == ZEROMV);
  1629. if (ref_frames[1] == NONE) {
  1630. if ((c3 >= c2 && frame_mv[NEARESTMV][ref_frames[0]].as_int == 0) ||
  1631. (c3 >= c1 && frame_mv[NEARMV][ref_frames[0]].as_int == 0))
  1632. return 0;
  1633. } else {
  1634. if ((c3 >= c2 && frame_mv[NEARESTMV][ref_frames[0]].as_int == 0 &&
  1635. frame_mv[NEARESTMV][ref_frames[1]].as_int == 0) ||
  1636. (c3 >= c1 && frame_mv[NEARMV][ref_frames[0]].as_int == 0 &&
  1637. frame_mv[NEARMV][ref_frames[1]].as_int == 0))
  1638. return 0;
  1639. }
  1640. }
  1641. }
  1642. return 1;
  1643. }
  1644. static void joint_motion_search(VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
  1645. int_mv *frame_mv, int mi_row, int mi_col,
  1646. int_mv single_newmv[MAX_REF_FRAMES],
  1647. int *rate_mv) {
  1648. const VP9_COMMON *const cm = &cpi->common;
  1649. const int pw = 4 * num_4x4_blocks_wide_lookup[bsize];
  1650. const int ph = 4 * num_4x4_blocks_high_lookup[bsize];
  1651. MACROBLOCKD *xd = &x->e_mbd;
  1652. MODE_INFO *mi = xd->mi[0];
  1653. const int refs[2] = { mi->ref_frame[0],
  1654. mi->ref_frame[1] < 0 ? 0 : mi->ref_frame[1] };
  1655. int_mv ref_mv[2];
  1656. int ite, ref;
  1657. const InterpKernel *kernel = vp9_filter_kernels[mi->interp_filter];
  1658. struct scale_factors sf;
  1659. // Do joint motion search in compound mode to get more accurate mv.
  1660. struct buf_2d backup_yv12[2][MAX_MB_PLANE];
  1661. uint32_t last_besterr[2] = { UINT_MAX, UINT_MAX };
  1662. const YV12_BUFFER_CONFIG *const scaled_ref_frame[2] = {
  1663. vp9_get_scaled_ref_frame(cpi, mi->ref_frame[0]),
  1664. vp9_get_scaled_ref_frame(cpi, mi->ref_frame[1])
  1665. };
  1666. // Prediction buffer from second frame.
  1667. #if CONFIG_VP9_HIGHBITDEPTH
  1668. DECLARE_ALIGNED(16, uint16_t, second_pred_alloc_16[64 * 64]);
  1669. uint8_t *second_pred;
  1670. #else
  1671. DECLARE_ALIGNED(16, uint8_t, second_pred[64 * 64]);
  1672. #endif // CONFIG_VP9_HIGHBITDEPTH
  1673. for (ref = 0; ref < 2; ++ref) {
  1674. ref_mv[ref] = x->mbmi_ext->ref_mvs[refs[ref]][0];
  1675. if (scaled_ref_frame[ref]) {
  1676. int i;
  1677. // Swap out the reference frame for a version that's been scaled to
  1678. // match the resolution of the current frame, allowing the existing
  1679. // motion search code to be used without additional modifications.
  1680. for (i = 0; i < MAX_MB_PLANE; i++)
  1681. backup_yv12[ref][i] = xd->plane[i].pre[ref];
  1682. vp9_setup_pre_planes(xd, ref, scaled_ref_frame[ref], mi_row, mi_col,
  1683. NULL);
  1684. }
  1685. frame_mv[refs[ref]].as_int = single_newmv[refs[ref]].as_int;
  1686. }
  1687. // Since we have scaled the reference frames to match the size of the current
  1688. // frame we must use a unit scaling factor during mode selection.
  1689. #if CONFIG_VP9_HIGHBITDEPTH
  1690. vp9_setup_scale_factors_for_frame(&sf, cm->width, cm->height, cm->width,
  1691. cm->height, cm->use_highbitdepth);
  1692. #else
  1693. vp9_setup_scale_factors_for_frame(&sf, cm->width, cm->height, cm->width,
  1694. cm->height);
  1695. #endif // CONFIG_VP9_HIGHBITDEPTH
  1696. // Allow joint search multiple times iteratively for each reference frame
  1697. // and break out of the search loop if it couldn't find a better mv.
  1698. for (ite = 0; ite < 4; ite++) {
  1699. struct buf_2d ref_yv12[2];
  1700. uint32_t bestsme = UINT_MAX;
  1701. int sadpb = x->sadperbit16;
  1702. MV tmp_mv;
  1703. int search_range = 3;
  1704. const MvLimits tmp_mv_limits = x->mv_limits;
  1705. int id = ite % 2; // Even iterations search in the first reference frame,
  1706. // odd iterations search in the second. The predictor
  1707. // found for the 'other' reference frame is factored in.
  1708. // Initialized here because of compiler problem in Visual Studio.
  1709. ref_yv12[0] = xd->plane[0].pre[0];
  1710. ref_yv12[1] = xd->plane[0].pre[1];
  1711. // Get the prediction block from the 'other' reference frame.
  1712. #if CONFIG_VP9_HIGHBITDEPTH
  1713. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  1714. second_pred = CONVERT_TO_BYTEPTR(second_pred_alloc_16);
  1715. vp9_highbd_build_inter_predictor(
  1716. CONVERT_TO_SHORTPTR(ref_yv12[!id].buf), ref_yv12[!id].stride,
  1717. second_pred_alloc_16, pw, &frame_mv[refs[!id]].as_mv, &sf, pw, ph, 0,
  1718. kernel, MV_PRECISION_Q3, mi_col * MI_SIZE, mi_row * MI_SIZE, xd->bd);
  1719. } else {
  1720. second_pred = (uint8_t *)second_pred_alloc_16;
  1721. vp9_build_inter_predictor(ref_yv12[!id].buf, ref_yv12[!id].stride,
  1722. second_pred, pw, &frame_mv[refs[!id]].as_mv,
  1723. &sf, pw, ph, 0, kernel, MV_PRECISION_Q3,
  1724. mi_col * MI_SIZE, mi_row * MI_SIZE);
  1725. }
  1726. #else
  1727. vp9_build_inter_predictor(ref_yv12[!id].buf, ref_yv12[!id].stride,
  1728. second_pred, pw, &frame_mv[refs[!id]].as_mv, &sf,
  1729. pw, ph, 0, kernel, MV_PRECISION_Q3,
  1730. mi_col * MI_SIZE, mi_row * MI_SIZE);
  1731. #endif // CONFIG_VP9_HIGHBITDEPTH
  1732. // Do compound motion search on the current reference frame.
  1733. if (id) xd->plane[0].pre[0] = ref_yv12[id];
  1734. vp9_set_mv_search_range(&x->mv_limits, &ref_mv[id].as_mv);
  1735. // Use the mv result from the single mode as mv predictor.
  1736. tmp_mv = frame_mv[refs[id]].as_mv;
  1737. tmp_mv.col >>= 3;
  1738. tmp_mv.row >>= 3;
  1739. // Small-range full-pixel motion search.
  1740. bestsme = vp9_refining_search_8p_c(x, &tmp_mv, sadpb, search_range,
  1741. &cpi->fn_ptr[bsize], &ref_mv[id].as_mv,
  1742. second_pred);
  1743. if (bestsme < UINT_MAX)
  1744. bestsme = vp9_get_mvpred_av_var(x, &tmp_mv, &ref_mv[id].as_mv,
  1745. second_pred, &cpi->fn_ptr[bsize], 1);
  1746. x->mv_limits = tmp_mv_limits;
  1747. if (bestsme < UINT_MAX) {
  1748. uint32_t dis; /* TODO: use dis in distortion calculation later. */
  1749. uint32_t sse;
  1750. bestsme = cpi->find_fractional_mv_step(
  1751. x, &tmp_mv, &ref_mv[id].as_mv, cpi->common.allow_high_precision_mv,
  1752. x->errorperbit, &cpi->fn_ptr[bsize], 0,
  1753. cpi->sf.mv.subpel_search_level, NULL, x->nmvjointcost, x->mvcost,
  1754. &dis, &sse, second_pred, pw, ph, cpi->sf.use_accurate_subpel_search);
  1755. }
  1756. // Restore the pointer to the first (possibly scaled) prediction buffer.
  1757. if (id) xd->plane[0].pre[0] = ref_yv12[0];
  1758. if (bestsme < last_besterr[id]) {
  1759. frame_mv[refs[id]].as_mv = tmp_mv;
  1760. last_besterr[id] = bestsme;
  1761. } else {
  1762. break;
  1763. }
  1764. }
  1765. *rate_mv = 0;
  1766. for (ref = 0; ref < 2; ++ref) {
  1767. if (scaled_ref_frame[ref]) {
  1768. // Restore the prediction frame pointers to their unscaled versions.
  1769. int i;
  1770. for (i = 0; i < MAX_MB_PLANE; i++)
  1771. xd->plane[i].pre[ref] = backup_yv12[ref][i];
  1772. }
  1773. *rate_mv += vp9_mv_bit_cost(&frame_mv[refs[ref]].as_mv,
  1774. &x->mbmi_ext->ref_mvs[refs[ref]][0].as_mv,
  1775. x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
  1776. }
  1777. }
  1778. static int64_t rd_pick_best_sub8x8_mode(
  1779. VP9_COMP *cpi, MACROBLOCK *x, int_mv *best_ref_mv,
  1780. int_mv *second_best_ref_mv, int64_t best_rd, int *returntotrate,
  1781. int *returnyrate, int64_t *returndistortion, int *skippable, int64_t *psse,
  1782. int mvthresh, int_mv seg_mvs[4][MAX_REF_FRAMES], BEST_SEG_INFO *bsi_buf,
  1783. int filter_idx, int mi_row, int mi_col) {
  1784. int i;
  1785. BEST_SEG_INFO *bsi = bsi_buf + filter_idx;
  1786. MACROBLOCKD *xd = &x->e_mbd;
  1787. MODE_INFO *mi = xd->mi[0];
  1788. int mode_idx;
  1789. int k, br = 0, idx, idy;
  1790. int64_t bd = 0, block_sse = 0;
  1791. PREDICTION_MODE this_mode;
  1792. VP9_COMMON *cm = &cpi->common;
  1793. struct macroblock_plane *const p = &x->plane[0];
  1794. struct macroblockd_plane *const pd = &xd->plane[0];
  1795. const int label_count = 4;
  1796. int64_t this_segment_rd = 0;
  1797. int label_mv_thresh;
  1798. int segmentyrate = 0;
  1799. const BLOCK_SIZE bsize = mi->sb_type;
  1800. const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
  1801. const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
  1802. const int pw = num_4x4_blocks_wide << 2;
  1803. const int ph = num_4x4_blocks_high << 2;
  1804. ENTROPY_CONTEXT t_above[2], t_left[2];
  1805. int subpelmv = 1, have_ref = 0;
  1806. SPEED_FEATURES *const sf = &cpi->sf;
  1807. const int has_second_rf = has_second_ref(mi);
  1808. const int inter_mode_mask = sf->inter_mode_mask[bsize];
  1809. MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
  1810. vp9_zero(*bsi);
  1811. bsi->segment_rd = best_rd;
  1812. bsi->ref_mv[0] = best_ref_mv;
  1813. bsi->ref_mv[1] = second_best_ref_mv;
  1814. bsi->mvp.as_int = best_ref_mv->as_int;
  1815. bsi->mvthresh = mvthresh;
  1816. for (i = 0; i < 4; i++) bsi->modes[i] = ZEROMV;
  1817. memcpy(t_above, pd->above_context, sizeof(t_above));
  1818. memcpy(t_left, pd->left_context, sizeof(t_left));
  1819. // 64 makes this threshold really big effectively
  1820. // making it so that we very rarely check mvs on
  1821. // segments. setting this to 1 would make mv thresh
  1822. // roughly equal to what it is for macroblocks
  1823. label_mv_thresh = 1 * bsi->mvthresh / label_count;
  1824. // Segmentation method overheads
  1825. for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
  1826. for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
  1827. // TODO(jingning,rbultje): rewrite the rate-distortion optimization
  1828. // loop for 4x4/4x8/8x4 block coding. to be replaced with new rd loop
  1829. int_mv mode_mv[MB_MODE_COUNT][2];
  1830. int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES];
  1831. PREDICTION_MODE mode_selected = ZEROMV;
  1832. int64_t best_rd = INT64_MAX;
  1833. const int i = idy * 2 + idx;
  1834. int ref;
  1835. for (ref = 0; ref < 1 + has_second_rf; ++ref) {
  1836. const MV_REFERENCE_FRAME frame = mi->ref_frame[ref];
  1837. frame_mv[ZEROMV][frame].as_int = 0;
  1838. vp9_append_sub8x8_mvs_for_idx(
  1839. cm, xd, i, ref, mi_row, mi_col, &frame_mv[NEARESTMV][frame],
  1840. &frame_mv[NEARMV][frame], mbmi_ext->mode_context);
  1841. }
  1842. // search for the best motion vector on this segment
  1843. for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
  1844. const struct buf_2d orig_src = x->plane[0].src;
  1845. struct buf_2d orig_pre[2];
  1846. mode_idx = INTER_OFFSET(this_mode);
  1847. bsi->rdstat[i][mode_idx].brdcost = INT64_MAX;
  1848. if (!(inter_mode_mask & (1 << this_mode))) continue;
  1849. if (!check_best_zero_mv(cpi, mbmi_ext->mode_context, frame_mv,
  1850. this_mode, mi->ref_frame))
  1851. continue;
  1852. memcpy(orig_pre, pd->pre, sizeof(orig_pre));
  1853. memcpy(bsi->rdstat[i][mode_idx].ta, t_above,
  1854. sizeof(bsi->rdstat[i][mode_idx].ta));
  1855. memcpy(bsi->rdstat[i][mode_idx].tl, t_left,
  1856. sizeof(bsi->rdstat[i][mode_idx].tl));
  1857. // motion search for newmv (single predictor case only)
  1858. if (!has_second_rf && this_mode == NEWMV &&
  1859. seg_mvs[i][mi->ref_frame[0]].as_int == INVALID_MV) {
  1860. MV *const new_mv = &mode_mv[NEWMV][0].as_mv;
  1861. int step_param = 0;
  1862. uint32_t bestsme = UINT_MAX;
  1863. int sadpb = x->sadperbit4;
  1864. MV mvp_full;
  1865. int max_mv;
  1866. int cost_list[5];
  1867. const MvLimits tmp_mv_limits = x->mv_limits;
  1868. /* Is the best so far sufficiently good that we cant justify doing
  1869. * and new motion search. */
  1870. if (best_rd < label_mv_thresh) break;
  1871. if (cpi->oxcf.mode != BEST) {
  1872. // use previous block's result as next block's MV predictor.
  1873. if (i > 0) {
  1874. bsi->mvp.as_int = mi->bmi[i - 1].as_mv[0].as_int;
  1875. if (i == 2) bsi->mvp.as_int = mi->bmi[i - 2].as_mv[0].as_int;
  1876. }
  1877. }
  1878. if (i == 0)
  1879. max_mv = x->max_mv_context[mi->ref_frame[0]];
  1880. else
  1881. max_mv =
  1882. VPXMAX(abs(bsi->mvp.as_mv.row), abs(bsi->mvp.as_mv.col)) >> 3;
  1883. if (sf->mv.auto_mv_step_size && cm->show_frame) {
  1884. // Take wtd average of the step_params based on the last frame's
  1885. // max mv magnitude and the best ref mvs of the current block for
  1886. // the given reference.
  1887. step_param =
  1888. (vp9_init_search_range(max_mv) + cpi->mv_step_param) / 2;
  1889. } else {
  1890. step_param = cpi->mv_step_param;
  1891. }
  1892. mvp_full.row = bsi->mvp.as_mv.row >> 3;
  1893. mvp_full.col = bsi->mvp.as_mv.col >> 3;
  1894. if (sf->adaptive_motion_search) {
  1895. if (x->pred_mv[mi->ref_frame[0]].row != INT16_MAX &&
  1896. x->pred_mv[mi->ref_frame[0]].col != INT16_MAX) {
  1897. mvp_full.row = x->pred_mv[mi->ref_frame[0]].row >> 3;
  1898. mvp_full.col = x->pred_mv[mi->ref_frame[0]].col >> 3;
  1899. }
  1900. step_param = VPXMAX(step_param, 8);
  1901. }
  1902. // adjust src pointer for this block
  1903. mi_buf_shift(x, i);
  1904. vp9_set_mv_search_range(&x->mv_limits, &bsi->ref_mv[0]->as_mv);
  1905. bestsme = vp9_full_pixel_search(
  1906. cpi, x, bsize, &mvp_full, step_param, cpi->sf.mv.search_method,
  1907. sadpb,
  1908. sf->mv.subpel_search_method != SUBPEL_TREE ? cost_list : NULL,
  1909. &bsi->ref_mv[0]->as_mv, new_mv, INT_MAX, 1);
  1910. x->mv_limits = tmp_mv_limits;
  1911. if (bestsme < UINT_MAX) {
  1912. uint32_t distortion;
  1913. cpi->find_fractional_mv_step(
  1914. x, new_mv, &bsi->ref_mv[0]->as_mv, cm->allow_high_precision_mv,
  1915. x->errorperbit, &cpi->fn_ptr[bsize], sf->mv.subpel_force_stop,
  1916. sf->mv.subpel_search_level, cond_cost_list(cpi, cost_list),
  1917. x->nmvjointcost, x->mvcost, &distortion,
  1918. &x->pred_sse[mi->ref_frame[0]], NULL, pw, ph,
  1919. cpi->sf.use_accurate_subpel_search);
  1920. // save motion search result for use in compound prediction
  1921. seg_mvs[i][mi->ref_frame[0]].as_mv = *new_mv;
  1922. }
  1923. x->pred_mv[mi->ref_frame[0]] = *new_mv;
  1924. // restore src pointers
  1925. mi_buf_restore(x, orig_src, orig_pre);
  1926. }
  1927. if (has_second_rf) {
  1928. if (seg_mvs[i][mi->ref_frame[1]].as_int == INVALID_MV ||
  1929. seg_mvs[i][mi->ref_frame[0]].as_int == INVALID_MV)
  1930. continue;
  1931. }
  1932. if (has_second_rf && this_mode == NEWMV &&
  1933. mi->interp_filter == EIGHTTAP) {
  1934. // adjust src pointers
  1935. mi_buf_shift(x, i);
  1936. if (sf->comp_inter_joint_search_thresh <= bsize) {
  1937. int rate_mv;
  1938. joint_motion_search(cpi, x, bsize, frame_mv[this_mode], mi_row,
  1939. mi_col, seg_mvs[i], &rate_mv);
  1940. seg_mvs[i][mi->ref_frame[0]].as_int =
  1941. frame_mv[this_mode][mi->ref_frame[0]].as_int;
  1942. seg_mvs[i][mi->ref_frame[1]].as_int =
  1943. frame_mv[this_mode][mi->ref_frame[1]].as_int;
  1944. }
  1945. // restore src pointers
  1946. mi_buf_restore(x, orig_src, orig_pre);
  1947. }
  1948. bsi->rdstat[i][mode_idx].brate = set_and_cost_bmi_mvs(
  1949. cpi, x, xd, i, this_mode, mode_mv[this_mode], frame_mv, seg_mvs[i],
  1950. bsi->ref_mv, x->nmvjointcost, x->mvcost);
  1951. for (ref = 0; ref < 1 + has_second_rf; ++ref) {
  1952. bsi->rdstat[i][mode_idx].mvs[ref].as_int =
  1953. mode_mv[this_mode][ref].as_int;
  1954. if (num_4x4_blocks_wide > 1)
  1955. bsi->rdstat[i + 1][mode_idx].mvs[ref].as_int =
  1956. mode_mv[this_mode][ref].as_int;
  1957. if (num_4x4_blocks_high > 1)
  1958. bsi->rdstat[i + 2][mode_idx].mvs[ref].as_int =
  1959. mode_mv[this_mode][ref].as_int;
  1960. }
  1961. // Trap vectors that reach beyond the UMV borders
  1962. if (mv_check_bounds(&x->mv_limits, &mode_mv[this_mode][0].as_mv) ||
  1963. (has_second_rf &&
  1964. mv_check_bounds(&x->mv_limits, &mode_mv[this_mode][1].as_mv)))
  1965. continue;
  1966. if (filter_idx > 0) {
  1967. BEST_SEG_INFO *ref_bsi = bsi_buf;
  1968. subpelmv = 0;
  1969. have_ref = 1;
  1970. for (ref = 0; ref < 1 + has_second_rf; ++ref) {
  1971. subpelmv |= mv_has_subpel(&mode_mv[this_mode][ref].as_mv);
  1972. have_ref &= mode_mv[this_mode][ref].as_int ==
  1973. ref_bsi->rdstat[i][mode_idx].mvs[ref].as_int;
  1974. }
  1975. if (filter_idx > 1 && !subpelmv && !have_ref) {
  1976. ref_bsi = bsi_buf + 1;
  1977. have_ref = 1;
  1978. for (ref = 0; ref < 1 + has_second_rf; ++ref)
  1979. have_ref &= mode_mv[this_mode][ref].as_int ==
  1980. ref_bsi->rdstat[i][mode_idx].mvs[ref].as_int;
  1981. }
  1982. if (!subpelmv && have_ref &&
  1983. ref_bsi->rdstat[i][mode_idx].brdcost < INT64_MAX) {
  1984. memcpy(&bsi->rdstat[i][mode_idx], &ref_bsi->rdstat[i][mode_idx],
  1985. sizeof(SEG_RDSTAT));
  1986. if (num_4x4_blocks_wide > 1)
  1987. bsi->rdstat[i + 1][mode_idx].eobs =
  1988. ref_bsi->rdstat[i + 1][mode_idx].eobs;
  1989. if (num_4x4_blocks_high > 1)
  1990. bsi->rdstat[i + 2][mode_idx].eobs =
  1991. ref_bsi->rdstat[i + 2][mode_idx].eobs;
  1992. if (bsi->rdstat[i][mode_idx].brdcost < best_rd) {
  1993. mode_selected = this_mode;
  1994. best_rd = bsi->rdstat[i][mode_idx].brdcost;
  1995. }
  1996. continue;
  1997. }
  1998. }
  1999. bsi->rdstat[i][mode_idx].brdcost = encode_inter_mb_segment(
  2000. cpi, x, bsi->segment_rd - this_segment_rd, i,
  2001. &bsi->rdstat[i][mode_idx].byrate, &bsi->rdstat[i][mode_idx].bdist,
  2002. &bsi->rdstat[i][mode_idx].bsse, bsi->rdstat[i][mode_idx].ta,
  2003. bsi->rdstat[i][mode_idx].tl, mi_row, mi_col);
  2004. if (bsi->rdstat[i][mode_idx].brdcost < INT64_MAX) {
  2005. bsi->rdstat[i][mode_idx].brdcost +=
  2006. RDCOST(x->rdmult, x->rddiv, bsi->rdstat[i][mode_idx].brate, 0);
  2007. bsi->rdstat[i][mode_idx].brate += bsi->rdstat[i][mode_idx].byrate;
  2008. bsi->rdstat[i][mode_idx].eobs = p->eobs[i];
  2009. if (num_4x4_blocks_wide > 1)
  2010. bsi->rdstat[i + 1][mode_idx].eobs = p->eobs[i + 1];
  2011. if (num_4x4_blocks_high > 1)
  2012. bsi->rdstat[i + 2][mode_idx].eobs = p->eobs[i + 2];
  2013. }
  2014. if (bsi->rdstat[i][mode_idx].brdcost < best_rd) {
  2015. mode_selected = this_mode;
  2016. best_rd = bsi->rdstat[i][mode_idx].brdcost;
  2017. }
  2018. } /*for each 4x4 mode*/
  2019. if (best_rd == INT64_MAX) {
  2020. int iy, midx;
  2021. for (iy = i + 1; iy < 4; ++iy)
  2022. for (midx = 0; midx < INTER_MODES; ++midx)
  2023. bsi->rdstat[iy][midx].brdcost = INT64_MAX;
  2024. bsi->segment_rd = INT64_MAX;
  2025. return INT64_MAX;
  2026. }
  2027. mode_idx = INTER_OFFSET(mode_selected);
  2028. memcpy(t_above, bsi->rdstat[i][mode_idx].ta, sizeof(t_above));
  2029. memcpy(t_left, bsi->rdstat[i][mode_idx].tl, sizeof(t_left));
  2030. set_and_cost_bmi_mvs(cpi, x, xd, i, mode_selected, mode_mv[mode_selected],
  2031. frame_mv, seg_mvs[i], bsi->ref_mv, x->nmvjointcost,
  2032. x->mvcost);
  2033. br += bsi->rdstat[i][mode_idx].brate;
  2034. bd += bsi->rdstat[i][mode_idx].bdist;
  2035. block_sse += bsi->rdstat[i][mode_idx].bsse;
  2036. segmentyrate += bsi->rdstat[i][mode_idx].byrate;
  2037. this_segment_rd += bsi->rdstat[i][mode_idx].brdcost;
  2038. if (this_segment_rd > bsi->segment_rd) {
  2039. int iy, midx;
  2040. for (iy = i + 1; iy < 4; ++iy)
  2041. for (midx = 0; midx < INTER_MODES; ++midx)
  2042. bsi->rdstat[iy][midx].brdcost = INT64_MAX;
  2043. bsi->segment_rd = INT64_MAX;
  2044. return INT64_MAX;
  2045. }
  2046. }
  2047. } /* for each label */
  2048. bsi->r = br;
  2049. bsi->d = bd;
  2050. bsi->segment_yrate = segmentyrate;
  2051. bsi->segment_rd = this_segment_rd;
  2052. bsi->sse = block_sse;
  2053. // update the coding decisions
  2054. for (k = 0; k < 4; ++k) bsi->modes[k] = mi->bmi[k].as_mode;
  2055. if (bsi->segment_rd > best_rd) return INT64_MAX;
  2056. /* set it to the best */
  2057. for (i = 0; i < 4; i++) {
  2058. mode_idx = INTER_OFFSET(bsi->modes[i]);
  2059. mi->bmi[i].as_mv[0].as_int = bsi->rdstat[i][mode_idx].mvs[0].as_int;
  2060. if (has_second_ref(mi))
  2061. mi->bmi[i].as_mv[1].as_int = bsi->rdstat[i][mode_idx].mvs[1].as_int;
  2062. x->plane[0].eobs[i] = bsi->rdstat[i][mode_idx].eobs;
  2063. mi->bmi[i].as_mode = bsi->modes[i];
  2064. }
  2065. /*
  2066. * used to set mbmi->mv.as_int
  2067. */
  2068. *returntotrate = bsi->r;
  2069. *returndistortion = bsi->d;
  2070. *returnyrate = bsi->segment_yrate;
  2071. *skippable = vp9_is_skippable_in_plane(x, BLOCK_8X8, 0);
  2072. *psse = bsi->sse;
  2073. mi->mode = bsi->modes[3];
  2074. return bsi->segment_rd;
  2075. }
  2076. static void estimate_ref_frame_costs(const VP9_COMMON *cm,
  2077. const MACROBLOCKD *xd, int segment_id,
  2078. unsigned int *ref_costs_single,
  2079. unsigned int *ref_costs_comp,
  2080. vpx_prob *comp_mode_p) {
  2081. int seg_ref_active =
  2082. segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME);
  2083. if (seg_ref_active) {
  2084. memset(ref_costs_single, 0, MAX_REF_FRAMES * sizeof(*ref_costs_single));
  2085. memset(ref_costs_comp, 0, MAX_REF_FRAMES * sizeof(*ref_costs_comp));
  2086. *comp_mode_p = 128;
  2087. } else {
  2088. vpx_prob intra_inter_p = vp9_get_intra_inter_prob(cm, xd);
  2089. vpx_prob comp_inter_p = 128;
  2090. if (cm->reference_mode == REFERENCE_MODE_SELECT) {
  2091. comp_inter_p = vp9_get_reference_mode_prob(cm, xd);
  2092. *comp_mode_p = comp_inter_p;
  2093. } else {
  2094. *comp_mode_p = 128;
  2095. }
  2096. ref_costs_single[INTRA_FRAME] = vp9_cost_bit(intra_inter_p, 0);
  2097. if (cm->reference_mode != COMPOUND_REFERENCE) {
  2098. vpx_prob ref_single_p1 = vp9_get_pred_prob_single_ref_p1(cm, xd);
  2099. vpx_prob ref_single_p2 = vp9_get_pred_prob_single_ref_p2(cm, xd);
  2100. unsigned int base_cost = vp9_cost_bit(intra_inter_p, 1);
  2101. if (cm->reference_mode == REFERENCE_MODE_SELECT)
  2102. base_cost += vp9_cost_bit(comp_inter_p, 0);
  2103. ref_costs_single[LAST_FRAME] = ref_costs_single[GOLDEN_FRAME] =
  2104. ref_costs_single[ALTREF_FRAME] = base_cost;
  2105. ref_costs_single[LAST_FRAME] += vp9_cost_bit(ref_single_p1, 0);
  2106. ref_costs_single[GOLDEN_FRAME] += vp9_cost_bit(ref_single_p1, 1);
  2107. ref_costs_single[ALTREF_FRAME] += vp9_cost_bit(ref_single_p1, 1);
  2108. ref_costs_single[GOLDEN_FRAME] += vp9_cost_bit(ref_single_p2, 0);
  2109. ref_costs_single[ALTREF_FRAME] += vp9_cost_bit(ref_single_p2, 1);
  2110. } else {
  2111. ref_costs_single[LAST_FRAME] = 512;
  2112. ref_costs_single[GOLDEN_FRAME] = 512;
  2113. ref_costs_single[ALTREF_FRAME] = 512;
  2114. }
  2115. if (cm->reference_mode != SINGLE_REFERENCE) {
  2116. vpx_prob ref_comp_p = vp9_get_pred_prob_comp_ref_p(cm, xd);
  2117. unsigned int base_cost = vp9_cost_bit(intra_inter_p, 1);
  2118. if (cm->reference_mode == REFERENCE_MODE_SELECT)
  2119. base_cost += vp9_cost_bit(comp_inter_p, 1);
  2120. ref_costs_comp[LAST_FRAME] = base_cost + vp9_cost_bit(ref_comp_p, 0);
  2121. ref_costs_comp[GOLDEN_FRAME] = base_cost + vp9_cost_bit(ref_comp_p, 1);
  2122. } else {
  2123. ref_costs_comp[LAST_FRAME] = 512;
  2124. ref_costs_comp[GOLDEN_FRAME] = 512;
  2125. }
  2126. }
  2127. }
  2128. static void store_coding_context(
  2129. MACROBLOCK *x, PICK_MODE_CONTEXT *ctx, int mode_index,
  2130. int64_t comp_pred_diff[REFERENCE_MODES],
  2131. int64_t best_filter_diff[SWITCHABLE_FILTER_CONTEXTS], int skippable) {
  2132. MACROBLOCKD *const xd = &x->e_mbd;
  2133. // Take a snapshot of the coding context so it can be
  2134. // restored if we decide to encode this way
  2135. ctx->skip = x->skip;
  2136. ctx->skippable = skippable;
  2137. ctx->best_mode_index = mode_index;
  2138. ctx->mic = *xd->mi[0];
  2139. ctx->mbmi_ext = *x->mbmi_ext;
  2140. ctx->single_pred_diff = (int)comp_pred_diff[SINGLE_REFERENCE];
  2141. ctx->comp_pred_diff = (int)comp_pred_diff[COMPOUND_REFERENCE];
  2142. ctx->hybrid_pred_diff = (int)comp_pred_diff[REFERENCE_MODE_SELECT];
  2143. memcpy(ctx->best_filter_diff, best_filter_diff,
  2144. sizeof(*best_filter_diff) * SWITCHABLE_FILTER_CONTEXTS);
  2145. }
  2146. static void setup_buffer_inter(VP9_COMP *cpi, MACROBLOCK *x,
  2147. MV_REFERENCE_FRAME ref_frame,
  2148. BLOCK_SIZE block_size, int mi_row, int mi_col,
  2149. int_mv frame_nearest_mv[MAX_REF_FRAMES],
  2150. int_mv frame_near_mv[MAX_REF_FRAMES],
  2151. struct buf_2d yv12_mb[4][MAX_MB_PLANE]) {
  2152. const VP9_COMMON *cm = &cpi->common;
  2153. const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
  2154. MACROBLOCKD *const xd = &x->e_mbd;
  2155. MODE_INFO *const mi = xd->mi[0];
  2156. int_mv *const candidates = x->mbmi_ext->ref_mvs[ref_frame];
  2157. const struct scale_factors *const sf = &cm->frame_refs[ref_frame - 1].sf;
  2158. MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
  2159. assert(yv12 != NULL);
  2160. // TODO(jkoleszar): Is the UV buffer ever used here? If so, need to make this
  2161. // use the UV scaling factors.
  2162. vp9_setup_pred_block(xd, yv12_mb[ref_frame], yv12, mi_row, mi_col, sf, sf);
  2163. // Gets an initial list of candidate vectors from neighbours and orders them
  2164. vp9_find_mv_refs(cm, xd, mi, ref_frame, candidates, mi_row, mi_col,
  2165. mbmi_ext->mode_context);
  2166. // Candidate refinement carried out at encoder and decoder
  2167. vp9_find_best_ref_mvs(xd, cm->allow_high_precision_mv, candidates,
  2168. &frame_nearest_mv[ref_frame],
  2169. &frame_near_mv[ref_frame]);
  2170. // Further refinement that is encode side only to test the top few candidates
  2171. // in full and choose the best as the centre point for subsequent searches.
  2172. // The current implementation doesn't support scaling.
  2173. if (!vp9_is_scaled(sf) && block_size >= BLOCK_8X8)
  2174. vp9_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride, ref_frame,
  2175. block_size);
  2176. }
  2177. #if CONFIG_NON_GREEDY_MV
  2178. static int ref_frame_to_gf_rf_idx(int ref_frame) {
  2179. if (ref_frame == GOLDEN_FRAME) {
  2180. return 0;
  2181. }
  2182. if (ref_frame == LAST_FRAME) {
  2183. return 1;
  2184. }
  2185. if (ref_frame == ALTREF_FRAME) {
  2186. return 2;
  2187. }
  2188. assert(0);
  2189. return -1;
  2190. }
  2191. #endif
  2192. static void single_motion_search(VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
  2193. int mi_row, int mi_col, int_mv *tmp_mv,
  2194. int *rate_mv) {
  2195. MACROBLOCKD *xd = &x->e_mbd;
  2196. const VP9_COMMON *cm = &cpi->common;
  2197. MODE_INFO *mi = xd->mi[0];
  2198. struct buf_2d backup_yv12[MAX_MB_PLANE] = { { 0, 0 } };
  2199. int step_param;
  2200. MV mvp_full;
  2201. int ref = mi->ref_frame[0];
  2202. MV ref_mv = x->mbmi_ext->ref_mvs[ref][0].as_mv;
  2203. const MvLimits tmp_mv_limits = x->mv_limits;
  2204. int cost_list[5];
  2205. const int best_predmv_idx = x->mv_best_ref_index[ref];
  2206. const YV12_BUFFER_CONFIG *scaled_ref_frame =
  2207. vp9_get_scaled_ref_frame(cpi, ref);
  2208. const int pw = num_4x4_blocks_wide_lookup[bsize] << 2;
  2209. const int ph = num_4x4_blocks_high_lookup[bsize] << 2;
  2210. MV pred_mv[3];
  2211. #if CONFIG_NON_GREEDY_MV
  2212. double bestsme;
  2213. int_mv nb_full_mvs[NB_MVS_NUM];
  2214. const int nb_full_mv_num = NB_MVS_NUM;
  2215. int gf_group_idx = cpi->twopass.gf_group.index;
  2216. int gf_rf_idx = ref_frame_to_gf_rf_idx(ref);
  2217. BLOCK_SIZE square_bsize = get_square_block_size(bsize);
  2218. const int lambda = (pw * ph) / 4;
  2219. assert(pw * ph == lambda << 2);
  2220. vp9_prepare_nb_full_mvs(&cpi->tpl_stats[gf_group_idx], mi_row, mi_col,
  2221. gf_rf_idx, square_bsize, nb_full_mvs);
  2222. #else // CONFIG_NON_GREEDY_MV
  2223. int bestsme = INT_MAX;
  2224. int sadpb = x->sadperbit16;
  2225. #endif // CONFIG_NON_GREEDY_MV
  2226. pred_mv[0] = x->mbmi_ext->ref_mvs[ref][0].as_mv;
  2227. pred_mv[1] = x->mbmi_ext->ref_mvs[ref][1].as_mv;
  2228. pred_mv[2] = x->pred_mv[ref];
  2229. if (scaled_ref_frame) {
  2230. int i;
  2231. // Swap out the reference frame for a version that's been scaled to
  2232. // match the resolution of the current frame, allowing the existing
  2233. // motion search code to be used without additional modifications.
  2234. for (i = 0; i < MAX_MB_PLANE; i++) backup_yv12[i] = xd->plane[i].pre[0];
  2235. vp9_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL);
  2236. }
  2237. // Work out the size of the first step in the mv step search.
  2238. // 0 here is maximum length first step. 1 is VPXMAX >> 1 etc.
  2239. if (cpi->sf.mv.auto_mv_step_size && cm->show_frame) {
  2240. // Take wtd average of the step_params based on the last frame's
  2241. // max mv magnitude and that based on the best ref mvs of the current
  2242. // block for the given reference.
  2243. step_param =
  2244. (vp9_init_search_range(x->max_mv_context[ref]) + cpi->mv_step_param) /
  2245. 2;
  2246. } else {
  2247. step_param = cpi->mv_step_param;
  2248. }
  2249. if (cpi->sf.adaptive_motion_search && bsize < BLOCK_64X64) {
  2250. const int boffset =
  2251. 2 * (b_width_log2_lookup[BLOCK_64X64] -
  2252. VPXMIN(b_height_log2_lookup[bsize], b_width_log2_lookup[bsize]));
  2253. step_param = VPXMAX(step_param, boffset);
  2254. }
  2255. if (cpi->sf.adaptive_motion_search) {
  2256. int bwl = b_width_log2_lookup[bsize];
  2257. int bhl = b_height_log2_lookup[bsize];
  2258. int tlevel = x->pred_mv_sad[ref] >> (bwl + bhl + 4);
  2259. if (tlevel < 5) step_param += 2;
  2260. // prev_mv_sad is not setup for dynamically scaled frames.
  2261. if (cpi->oxcf.resize_mode != RESIZE_DYNAMIC) {
  2262. int i;
  2263. for (i = LAST_FRAME; i <= ALTREF_FRAME && cm->show_frame; ++i) {
  2264. if ((x->pred_mv_sad[ref] >> 3) > x->pred_mv_sad[i]) {
  2265. x->pred_mv[ref].row = INT16_MAX;
  2266. x->pred_mv[ref].col = INT16_MAX;
  2267. tmp_mv->as_int = INVALID_MV;
  2268. if (scaled_ref_frame) {
  2269. int i;
  2270. for (i = 0; i < MAX_MB_PLANE; ++i)
  2271. xd->plane[i].pre[0] = backup_yv12[i];
  2272. }
  2273. return;
  2274. }
  2275. }
  2276. }
  2277. }
  2278. // Note: MV limits are modified here. Always restore the original values
  2279. // after full-pixel motion search.
  2280. vp9_set_mv_search_range(&x->mv_limits, &ref_mv);
  2281. mvp_full = pred_mv[best_predmv_idx];
  2282. mvp_full.col >>= 3;
  2283. mvp_full.row >>= 3;
  2284. #if CONFIG_NON_GREEDY_MV
  2285. bestsme = vp9_full_pixel_diamond_new(cpi, x, &mvp_full, step_param, lambda, 1,
  2286. &cpi->fn_ptr[bsize], nb_full_mvs,
  2287. nb_full_mv_num, &tmp_mv->as_mv);
  2288. #else // CONFIG_NON_GREEDY_MV
  2289. bestsme = vp9_full_pixel_search(
  2290. cpi, x, bsize, &mvp_full, step_param, cpi->sf.mv.search_method, sadpb,
  2291. cond_cost_list(cpi, cost_list), &ref_mv, &tmp_mv->as_mv, INT_MAX, 1);
  2292. #endif // CONFIG_NON_GREEDY_MV
  2293. if (cpi->sf.enhanced_full_pixel_motion_search) {
  2294. int i;
  2295. for (i = 0; i < 3; ++i) {
  2296. #if CONFIG_NON_GREEDY_MV
  2297. double this_me;
  2298. #else // CONFIG_NON_GREEDY_MV
  2299. int this_me;
  2300. #endif // CONFIG_NON_GREEDY_MV
  2301. MV this_mv;
  2302. int diff_row;
  2303. int diff_col;
  2304. int step;
  2305. if (pred_mv[i].row == INT16_MAX || pred_mv[i].col == INT16_MAX) continue;
  2306. if (i == best_predmv_idx) continue;
  2307. diff_row = ((int)pred_mv[i].row -
  2308. pred_mv[i > 0 ? (i - 1) : best_predmv_idx].row) >>
  2309. 3;
  2310. diff_col = ((int)pred_mv[i].col -
  2311. pred_mv[i > 0 ? (i - 1) : best_predmv_idx].col) >>
  2312. 3;
  2313. if (diff_row == 0 && diff_col == 0) continue;
  2314. if (diff_row < 0) diff_row = -diff_row;
  2315. if (diff_col < 0) diff_col = -diff_col;
  2316. step = get_msb((diff_row + diff_col + 1) >> 1);
  2317. if (step <= 0) continue;
  2318. mvp_full = pred_mv[i];
  2319. mvp_full.col >>= 3;
  2320. mvp_full.row >>= 3;
  2321. #if CONFIG_NON_GREEDY_MV
  2322. this_me = vp9_full_pixel_diamond_new(
  2323. cpi, x, &mvp_full, VPXMAX(step_param, MAX_MVSEARCH_STEPS - step),
  2324. lambda, 1, &cpi->fn_ptr[bsize], nb_full_mvs, nb_full_mv_num,
  2325. &this_mv);
  2326. #else // CONFIG_NON_GREEDY_MV
  2327. this_me = vp9_full_pixel_search(
  2328. cpi, x, bsize, &mvp_full,
  2329. VPXMAX(step_param, MAX_MVSEARCH_STEPS - step),
  2330. cpi->sf.mv.search_method, sadpb, cond_cost_list(cpi, cost_list),
  2331. &ref_mv, &this_mv, INT_MAX, 1);
  2332. #endif // CONFIG_NON_GREEDY_MV
  2333. if (this_me < bestsme) {
  2334. tmp_mv->as_mv = this_mv;
  2335. bestsme = this_me;
  2336. }
  2337. }
  2338. }
  2339. x->mv_limits = tmp_mv_limits;
  2340. if (bestsme < INT_MAX) {
  2341. uint32_t dis; /* TODO: use dis in distortion calculation later. */
  2342. cpi->find_fractional_mv_step(
  2343. x, &tmp_mv->as_mv, &ref_mv, cm->allow_high_precision_mv, x->errorperbit,
  2344. &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop,
  2345. cpi->sf.mv.subpel_search_level, cond_cost_list(cpi, cost_list),
  2346. x->nmvjointcost, x->mvcost, &dis, &x->pred_sse[ref], NULL, pw, ph,
  2347. cpi->sf.use_accurate_subpel_search);
  2348. }
  2349. *rate_mv = vp9_mv_bit_cost(&tmp_mv->as_mv, &ref_mv, x->nmvjointcost,
  2350. x->mvcost, MV_COST_WEIGHT);
  2351. x->pred_mv[ref] = tmp_mv->as_mv;
  2352. if (scaled_ref_frame) {
  2353. int i;
  2354. for (i = 0; i < MAX_MB_PLANE; i++) xd->plane[i].pre[0] = backup_yv12[i];
  2355. }
  2356. }
  2357. static INLINE void restore_dst_buf(MACROBLOCKD *xd,
  2358. uint8_t *orig_dst[MAX_MB_PLANE],
  2359. int orig_dst_stride[MAX_MB_PLANE]) {
  2360. int i;
  2361. for (i = 0; i < MAX_MB_PLANE; i++) {
  2362. xd->plane[i].dst.buf = orig_dst[i];
  2363. xd->plane[i].dst.stride = orig_dst_stride[i];
  2364. }
  2365. }
  2366. // In some situations we want to discount tha pparent cost of a new motion
  2367. // vector. Where there is a subtle motion field and especially where there is
  2368. // low spatial complexity then it can be hard to cover the cost of a new motion
  2369. // vector in a single block, even if that motion vector reduces distortion.
  2370. // However, once established that vector may be usable through the nearest and
  2371. // near mv modes to reduce distortion in subsequent blocks and also improve
  2372. // visual quality.
  2373. static int discount_newmv_test(const VP9_COMP *cpi, int this_mode,
  2374. int_mv this_mv,
  2375. int_mv (*mode_mv)[MAX_REF_FRAMES], int ref_frame,
  2376. int mi_row, int mi_col, BLOCK_SIZE bsize) {
  2377. #if CONFIG_NON_GREEDY_MV
  2378. (void)mode_mv;
  2379. (void)this_mv;
  2380. if (this_mode == NEWMV && bsize >= BLOCK_8X8 && cpi->tpl_ready) {
  2381. const int gf_group_idx = cpi->twopass.gf_group.index;
  2382. const int gf_rf_idx = ref_frame_to_gf_rf_idx(ref_frame);
  2383. const TplDepFrame tpl_frame = cpi->tpl_stats[gf_group_idx];
  2384. const int tpl_block_mi_h = num_8x8_blocks_high_lookup[cpi->tpl_bsize];
  2385. const int tpl_block_mi_w = num_8x8_blocks_wide_lookup[cpi->tpl_bsize];
  2386. const int tpl_mi_row = mi_row - (mi_row % tpl_block_mi_h);
  2387. const int tpl_mi_col = mi_col - (mi_col % tpl_block_mi_w);
  2388. const int mv_mode =
  2389. tpl_frame
  2390. .mv_mode_arr[gf_rf_idx][tpl_mi_row * tpl_frame.stride + tpl_mi_col];
  2391. if (mv_mode == NEW_MV_MODE) {
  2392. int_mv tpl_new_mv = *get_pyramid_mv(&tpl_frame, gf_rf_idx, cpi->tpl_bsize,
  2393. tpl_mi_row, tpl_mi_col);
  2394. int row_diff = abs(tpl_new_mv.as_mv.row - this_mv.as_mv.row);
  2395. int col_diff = abs(tpl_new_mv.as_mv.col - this_mv.as_mv.col);
  2396. if (VPXMAX(row_diff, col_diff) <= 8) {
  2397. return 1;
  2398. } else {
  2399. return 0;
  2400. }
  2401. } else {
  2402. return 0;
  2403. }
  2404. } else {
  2405. return 0;
  2406. }
  2407. #else
  2408. (void)mi_row;
  2409. (void)mi_col;
  2410. (void)bsize;
  2411. return (!cpi->rc.is_src_frame_alt_ref && (this_mode == NEWMV) &&
  2412. (this_mv.as_int != 0) &&
  2413. ((mode_mv[NEARESTMV][ref_frame].as_int == 0) ||
  2414. (mode_mv[NEARESTMV][ref_frame].as_int == INVALID_MV)) &&
  2415. ((mode_mv[NEARMV][ref_frame].as_int == 0) ||
  2416. (mode_mv[NEARMV][ref_frame].as_int == INVALID_MV)));
  2417. #endif
  2418. }
  2419. static int64_t handle_inter_mode(
  2420. VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int *rate2,
  2421. int64_t *distortion, int *skippable, int *rate_y, int *rate_uv,
  2422. struct buf_2d *recon, int *disable_skip, int_mv (*mode_mv)[MAX_REF_FRAMES],
  2423. int mi_row, int mi_col, int_mv single_newmv[MAX_REF_FRAMES],
  2424. INTERP_FILTER (*single_filter)[MAX_REF_FRAMES],
  2425. int (*single_skippable)[MAX_REF_FRAMES], int64_t *psse,
  2426. const int64_t ref_best_rd, int64_t *mask_filter, int64_t filter_cache[]) {
  2427. VP9_COMMON *cm = &cpi->common;
  2428. MACROBLOCKD *xd = &x->e_mbd;
  2429. MODE_INFO *mi = xd->mi[0];
  2430. MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
  2431. const int is_comp_pred = has_second_ref(mi);
  2432. const int this_mode = mi->mode;
  2433. int_mv *frame_mv = mode_mv[this_mode];
  2434. int i;
  2435. int refs[2] = { mi->ref_frame[0],
  2436. (mi->ref_frame[1] < 0 ? 0 : mi->ref_frame[1]) };
  2437. int_mv cur_mv[2];
  2438. #if CONFIG_VP9_HIGHBITDEPTH
  2439. DECLARE_ALIGNED(16, uint16_t, tmp_buf16[MAX_MB_PLANE * 64 * 64]);
  2440. uint8_t *tmp_buf;
  2441. #else
  2442. DECLARE_ALIGNED(16, uint8_t, tmp_buf[MAX_MB_PLANE * 64 * 64]);
  2443. #endif // CONFIG_VP9_HIGHBITDEPTH
  2444. int pred_exists = 0;
  2445. int intpel_mv;
  2446. int64_t rd, tmp_rd, best_rd = INT64_MAX;
  2447. int best_needs_copy = 0;
  2448. uint8_t *orig_dst[MAX_MB_PLANE];
  2449. int orig_dst_stride[MAX_MB_PLANE];
  2450. int rs = 0;
  2451. INTERP_FILTER best_filter = SWITCHABLE;
  2452. uint8_t skip_txfm[MAX_MB_PLANE << 2] = { 0 };
  2453. int64_t bsse[MAX_MB_PLANE << 2] = { 0 };
  2454. int bsl = mi_width_log2_lookup[bsize];
  2455. int pred_filter_search =
  2456. cpi->sf.cb_pred_filter_search
  2457. ? (((mi_row + mi_col) >> bsl) +
  2458. get_chessboard_index(cm->current_video_frame)) &
  2459. 0x1
  2460. : 0;
  2461. int skip_txfm_sb = 0;
  2462. int64_t skip_sse_sb = INT64_MAX;
  2463. int64_t distortion_y = 0, distortion_uv = 0;
  2464. #if CONFIG_VP9_HIGHBITDEPTH
  2465. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  2466. tmp_buf = CONVERT_TO_BYTEPTR(tmp_buf16);
  2467. } else {
  2468. tmp_buf = (uint8_t *)tmp_buf16;
  2469. }
  2470. #endif // CONFIG_VP9_HIGHBITDEPTH
  2471. if (pred_filter_search) {
  2472. INTERP_FILTER af = SWITCHABLE, lf = SWITCHABLE;
  2473. if (xd->above_mi && is_inter_block(xd->above_mi))
  2474. af = xd->above_mi->interp_filter;
  2475. if (xd->left_mi && is_inter_block(xd->left_mi))
  2476. lf = xd->left_mi->interp_filter;
  2477. if ((this_mode != NEWMV) || (af == lf)) best_filter = af;
  2478. }
  2479. if (is_comp_pred) {
  2480. if (frame_mv[refs[0]].as_int == INVALID_MV ||
  2481. frame_mv[refs[1]].as_int == INVALID_MV)
  2482. return INT64_MAX;
  2483. if (cpi->sf.adaptive_mode_search) {
  2484. if (single_filter[this_mode][refs[0]] ==
  2485. single_filter[this_mode][refs[1]])
  2486. best_filter = single_filter[this_mode][refs[0]];
  2487. }
  2488. }
  2489. if (this_mode == NEWMV) {
  2490. int rate_mv;
  2491. if (is_comp_pred) {
  2492. // Initialize mv using single prediction mode result.
  2493. frame_mv[refs[0]].as_int = single_newmv[refs[0]].as_int;
  2494. frame_mv[refs[1]].as_int = single_newmv[refs[1]].as_int;
  2495. if (cpi->sf.comp_inter_joint_search_thresh <= bsize) {
  2496. joint_motion_search(cpi, x, bsize, frame_mv, mi_row, mi_col,
  2497. single_newmv, &rate_mv);
  2498. } else {
  2499. rate_mv = vp9_mv_bit_cost(&frame_mv[refs[0]].as_mv,
  2500. &x->mbmi_ext->ref_mvs[refs[0]][0].as_mv,
  2501. x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
  2502. rate_mv += vp9_mv_bit_cost(&frame_mv[refs[1]].as_mv,
  2503. &x->mbmi_ext->ref_mvs[refs[1]][0].as_mv,
  2504. x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
  2505. }
  2506. *rate2 += rate_mv;
  2507. } else {
  2508. int_mv tmp_mv;
  2509. single_motion_search(cpi, x, bsize, mi_row, mi_col, &tmp_mv, &rate_mv);
  2510. if (tmp_mv.as_int == INVALID_MV) return INT64_MAX;
  2511. frame_mv[refs[0]].as_int = xd->mi[0]->bmi[0].as_mv[0].as_int =
  2512. tmp_mv.as_int;
  2513. single_newmv[refs[0]].as_int = tmp_mv.as_int;
  2514. // Estimate the rate implications of a new mv but discount this
  2515. // under certain circumstances where we want to help initiate a weak
  2516. // motion field, where the distortion gain for a single block may not
  2517. // be enough to overcome the cost of a new mv.
  2518. if (discount_newmv_test(cpi, this_mode, tmp_mv, mode_mv, refs[0], mi_row,
  2519. mi_col, bsize)) {
  2520. *rate2 += VPXMAX((rate_mv / NEW_MV_DISCOUNT_FACTOR), 1);
  2521. } else {
  2522. *rate2 += rate_mv;
  2523. }
  2524. }
  2525. }
  2526. for (i = 0; i < is_comp_pred + 1; ++i) {
  2527. cur_mv[i] = frame_mv[refs[i]];
  2528. // Clip "next_nearest" so that it does not extend to far out of image
  2529. if (this_mode != NEWMV) clamp_mv2(&cur_mv[i].as_mv, xd);
  2530. if (mv_check_bounds(&x->mv_limits, &cur_mv[i].as_mv)) return INT64_MAX;
  2531. mi->mv[i].as_int = cur_mv[i].as_int;
  2532. }
  2533. // do first prediction into the destination buffer. Do the next
  2534. // prediction into a temporary buffer. Then keep track of which one
  2535. // of these currently holds the best predictor, and use the other
  2536. // one for future predictions. In the end, copy from tmp_buf to
  2537. // dst if necessary.
  2538. for (i = 0; i < MAX_MB_PLANE; i++) {
  2539. orig_dst[i] = xd->plane[i].dst.buf;
  2540. orig_dst_stride[i] = xd->plane[i].dst.stride;
  2541. }
  2542. // We don't include the cost of the second reference here, because there
  2543. // are only two options: Last/ARF or Golden/ARF; The second one is always
  2544. // known, which is ARF.
  2545. //
  2546. // Under some circumstances we discount the cost of new mv mode to encourage
  2547. // initiation of a motion field.
  2548. if (discount_newmv_test(cpi, this_mode, frame_mv[refs[0]], mode_mv, refs[0],
  2549. mi_row, mi_col, bsize)) {
  2550. *rate2 +=
  2551. VPXMIN(cost_mv_ref(cpi, this_mode, mbmi_ext->mode_context[refs[0]]),
  2552. cost_mv_ref(cpi, NEARESTMV, mbmi_ext->mode_context[refs[0]]));
  2553. } else {
  2554. *rate2 += cost_mv_ref(cpi, this_mode, mbmi_ext->mode_context[refs[0]]);
  2555. }
  2556. if (RDCOST(x->rdmult, x->rddiv, *rate2, 0) > ref_best_rd &&
  2557. mi->mode != NEARESTMV)
  2558. return INT64_MAX;
  2559. pred_exists = 0;
  2560. // Are all MVs integer pel for Y and UV
  2561. intpel_mv = !mv_has_subpel(&mi->mv[0].as_mv);
  2562. if (is_comp_pred) intpel_mv &= !mv_has_subpel(&mi->mv[1].as_mv);
  2563. // Search for best switchable filter by checking the variance of
  2564. // pred error irrespective of whether the filter will be used
  2565. for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i) filter_cache[i] = INT64_MAX;
  2566. if (cm->interp_filter != BILINEAR) {
  2567. if (x->source_variance < cpi->sf.disable_filter_search_var_thresh) {
  2568. best_filter = EIGHTTAP;
  2569. } else if (best_filter == SWITCHABLE) {
  2570. int newbest;
  2571. int tmp_rate_sum = 0;
  2572. int64_t tmp_dist_sum = 0;
  2573. for (i = 0; i < SWITCHABLE_FILTERS; ++i) {
  2574. int j;
  2575. int64_t rs_rd;
  2576. int tmp_skip_sb = 0;
  2577. int64_t tmp_skip_sse = INT64_MAX;
  2578. mi->interp_filter = i;
  2579. rs = vp9_get_switchable_rate(cpi, xd);
  2580. rs_rd = RDCOST(x->rdmult, x->rddiv, rs, 0);
  2581. if (i > 0 && intpel_mv) {
  2582. rd = RDCOST(x->rdmult, x->rddiv, tmp_rate_sum, tmp_dist_sum);
  2583. filter_cache[i] = rd;
  2584. filter_cache[SWITCHABLE_FILTERS] =
  2585. VPXMIN(filter_cache[SWITCHABLE_FILTERS], rd + rs_rd);
  2586. if (cm->interp_filter == SWITCHABLE) rd += rs_rd;
  2587. *mask_filter = VPXMAX(*mask_filter, rd);
  2588. } else {
  2589. int rate_sum = 0;
  2590. int64_t dist_sum = 0;
  2591. if (i > 0 && cpi->sf.adaptive_interp_filter_search &&
  2592. (cpi->sf.interp_filter_search_mask & (1 << i))) {
  2593. rate_sum = INT_MAX;
  2594. dist_sum = INT64_MAX;
  2595. continue;
  2596. }
  2597. if ((cm->interp_filter == SWITCHABLE && (!i || best_needs_copy)) ||
  2598. (cm->interp_filter != SWITCHABLE &&
  2599. (cm->interp_filter == mi->interp_filter ||
  2600. (i == 0 && intpel_mv)))) {
  2601. restore_dst_buf(xd, orig_dst, orig_dst_stride);
  2602. } else {
  2603. for (j = 0; j < MAX_MB_PLANE; j++) {
  2604. xd->plane[j].dst.buf = tmp_buf + j * 64 * 64;
  2605. xd->plane[j].dst.stride = 64;
  2606. }
  2607. }
  2608. vp9_build_inter_predictors_sb(xd, mi_row, mi_col, bsize);
  2609. model_rd_for_sb(cpi, bsize, x, xd, &rate_sum, &dist_sum, &tmp_skip_sb,
  2610. &tmp_skip_sse);
  2611. rd = RDCOST(x->rdmult, x->rddiv, rate_sum, dist_sum);
  2612. filter_cache[i] = rd;
  2613. filter_cache[SWITCHABLE_FILTERS] =
  2614. VPXMIN(filter_cache[SWITCHABLE_FILTERS], rd + rs_rd);
  2615. if (cm->interp_filter == SWITCHABLE) rd += rs_rd;
  2616. *mask_filter = VPXMAX(*mask_filter, rd);
  2617. if (i == 0 && intpel_mv) {
  2618. tmp_rate_sum = rate_sum;
  2619. tmp_dist_sum = dist_sum;
  2620. }
  2621. }
  2622. if (i == 0 && cpi->sf.use_rd_breakout && ref_best_rd < INT64_MAX) {
  2623. if (rd / 2 > ref_best_rd) {
  2624. restore_dst_buf(xd, orig_dst, orig_dst_stride);
  2625. return INT64_MAX;
  2626. }
  2627. }
  2628. newbest = i == 0 || rd < best_rd;
  2629. if (newbest) {
  2630. best_rd = rd;
  2631. best_filter = mi->interp_filter;
  2632. if (cm->interp_filter == SWITCHABLE && i && !intpel_mv)
  2633. best_needs_copy = !best_needs_copy;
  2634. }
  2635. if ((cm->interp_filter == SWITCHABLE && newbest) ||
  2636. (cm->interp_filter != SWITCHABLE &&
  2637. cm->interp_filter == mi->interp_filter)) {
  2638. pred_exists = 1;
  2639. tmp_rd = best_rd;
  2640. skip_txfm_sb = tmp_skip_sb;
  2641. skip_sse_sb = tmp_skip_sse;
  2642. memcpy(skip_txfm, x->skip_txfm, sizeof(skip_txfm));
  2643. memcpy(bsse, x->bsse, sizeof(bsse));
  2644. }
  2645. }
  2646. restore_dst_buf(xd, orig_dst, orig_dst_stride);
  2647. }
  2648. }
  2649. // Set the appropriate filter
  2650. mi->interp_filter =
  2651. cm->interp_filter != SWITCHABLE ? cm->interp_filter : best_filter;
  2652. rs = cm->interp_filter == SWITCHABLE ? vp9_get_switchable_rate(cpi, xd) : 0;
  2653. if (pred_exists) {
  2654. if (best_needs_copy) {
  2655. // again temporarily set the buffers to local memory to prevent a memcpy
  2656. for (i = 0; i < MAX_MB_PLANE; i++) {
  2657. xd->plane[i].dst.buf = tmp_buf + i * 64 * 64;
  2658. xd->plane[i].dst.stride = 64;
  2659. }
  2660. }
  2661. rd = tmp_rd + RDCOST(x->rdmult, x->rddiv, rs, 0);
  2662. } else {
  2663. int tmp_rate;
  2664. int64_t tmp_dist;
  2665. // Handles the special case when a filter that is not in the
  2666. // switchable list (ex. bilinear) is indicated at the frame level, or
  2667. // skip condition holds.
  2668. vp9_build_inter_predictors_sb(xd, mi_row, mi_col, bsize);
  2669. model_rd_for_sb(cpi, bsize, x, xd, &tmp_rate, &tmp_dist, &skip_txfm_sb,
  2670. &skip_sse_sb);
  2671. rd = RDCOST(x->rdmult, x->rddiv, rs + tmp_rate, tmp_dist);
  2672. memcpy(skip_txfm, x->skip_txfm, sizeof(skip_txfm));
  2673. memcpy(bsse, x->bsse, sizeof(bsse));
  2674. }
  2675. if (!is_comp_pred) single_filter[this_mode][refs[0]] = mi->interp_filter;
  2676. if (cpi->sf.adaptive_mode_search)
  2677. if (is_comp_pred)
  2678. if (single_skippable[this_mode][refs[0]] &&
  2679. single_skippable[this_mode][refs[1]])
  2680. memset(skip_txfm, SKIP_TXFM_AC_DC, sizeof(skip_txfm));
  2681. if (cpi->sf.use_rd_breakout && ref_best_rd < INT64_MAX) {
  2682. // if current pred_error modeled rd is substantially more than the best
  2683. // so far, do not bother doing full rd
  2684. if (rd / 2 > ref_best_rd) {
  2685. restore_dst_buf(xd, orig_dst, orig_dst_stride);
  2686. return INT64_MAX;
  2687. }
  2688. }
  2689. if (cm->interp_filter == SWITCHABLE) *rate2 += rs;
  2690. memcpy(x->skip_txfm, skip_txfm, sizeof(skip_txfm));
  2691. memcpy(x->bsse, bsse, sizeof(bsse));
  2692. if (!skip_txfm_sb || xd->lossless) {
  2693. int skippable_y, skippable_uv;
  2694. int64_t sseuv = INT64_MAX;
  2695. int64_t rdcosty = INT64_MAX;
  2696. // Y cost and distortion
  2697. vp9_subtract_plane(x, bsize, 0);
  2698. super_block_yrd(cpi, x, rate_y, &distortion_y, &skippable_y, psse, bsize,
  2699. ref_best_rd, recon);
  2700. if (*rate_y == INT_MAX) {
  2701. *rate2 = INT_MAX;
  2702. *distortion = INT64_MAX;
  2703. restore_dst_buf(xd, orig_dst, orig_dst_stride);
  2704. return INT64_MAX;
  2705. }
  2706. *rate2 += *rate_y;
  2707. *distortion += distortion_y;
  2708. rdcosty = RDCOST(x->rdmult, x->rddiv, *rate2, *distortion);
  2709. rdcosty = VPXMIN(rdcosty, RDCOST(x->rdmult, x->rddiv, 0, *psse));
  2710. if (!super_block_uvrd(cpi, x, rate_uv, &distortion_uv, &skippable_uv,
  2711. &sseuv, bsize, ref_best_rd - rdcosty)) {
  2712. *rate2 = INT_MAX;
  2713. *distortion = INT64_MAX;
  2714. restore_dst_buf(xd, orig_dst, orig_dst_stride);
  2715. return INT64_MAX;
  2716. }
  2717. *psse += sseuv;
  2718. *rate2 += *rate_uv;
  2719. *distortion += distortion_uv;
  2720. *skippable = skippable_y && skippable_uv;
  2721. } else {
  2722. x->skip = 1;
  2723. *disable_skip = 1;
  2724. // The cost of skip bit needs to be added.
  2725. *rate2 += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
  2726. *distortion = skip_sse_sb;
  2727. }
  2728. if (!is_comp_pred) single_skippable[this_mode][refs[0]] = *skippable;
  2729. restore_dst_buf(xd, orig_dst, orig_dst_stride);
  2730. return 0; // The rate-distortion cost will be re-calculated by caller.
  2731. }
  2732. #endif // !CONFIG_REALTIME_ONLY
  2733. void vp9_rd_pick_intra_mode_sb(VP9_COMP *cpi, MACROBLOCK *x, RD_COST *rd_cost,
  2734. BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx,
  2735. int64_t best_rd) {
  2736. VP9_COMMON *const cm = &cpi->common;
  2737. MACROBLOCKD *const xd = &x->e_mbd;
  2738. struct macroblockd_plane *const pd = xd->plane;
  2739. int rate_y = 0, rate_uv = 0, rate_y_tokenonly = 0, rate_uv_tokenonly = 0;
  2740. int y_skip = 0, uv_skip = 0;
  2741. int64_t dist_y = 0, dist_uv = 0;
  2742. TX_SIZE max_uv_tx_size;
  2743. x->skip_encode = 0;
  2744. ctx->skip = 0;
  2745. xd->mi[0]->ref_frame[0] = INTRA_FRAME;
  2746. xd->mi[0]->ref_frame[1] = NONE;
  2747. // Initialize interp_filter here so we do not have to check for inter block
  2748. // modes in get_pred_context_switchable_interp()
  2749. xd->mi[0]->interp_filter = SWITCHABLE_FILTERS;
  2750. if (bsize >= BLOCK_8X8) {
  2751. if (rd_pick_intra_sby_mode(cpi, x, &rate_y, &rate_y_tokenonly, &dist_y,
  2752. &y_skip, bsize, best_rd) >= best_rd) {
  2753. rd_cost->rate = INT_MAX;
  2754. return;
  2755. }
  2756. } else {
  2757. y_skip = 0;
  2758. if (rd_pick_intra_sub_8x8_y_mode(cpi, x, &rate_y, &rate_y_tokenonly,
  2759. &dist_y, best_rd) >= best_rd) {
  2760. rd_cost->rate = INT_MAX;
  2761. return;
  2762. }
  2763. }
  2764. max_uv_tx_size = uv_txsize_lookup[bsize][xd->mi[0]->tx_size]
  2765. [pd[1].subsampling_x][pd[1].subsampling_y];
  2766. rd_pick_intra_sbuv_mode(cpi, x, ctx, &rate_uv, &rate_uv_tokenonly, &dist_uv,
  2767. &uv_skip, VPXMAX(BLOCK_8X8, bsize), max_uv_tx_size);
  2768. if (y_skip && uv_skip) {
  2769. rd_cost->rate = rate_y + rate_uv - rate_y_tokenonly - rate_uv_tokenonly +
  2770. vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
  2771. rd_cost->dist = dist_y + dist_uv;
  2772. } else {
  2773. rd_cost->rate =
  2774. rate_y + rate_uv + vp9_cost_bit(vp9_get_skip_prob(cm, xd), 0);
  2775. rd_cost->dist = dist_y + dist_uv;
  2776. }
  2777. ctx->mic = *xd->mi[0];
  2778. ctx->mbmi_ext = *x->mbmi_ext;
  2779. rd_cost->rdcost = RDCOST(x->rdmult, x->rddiv, rd_cost->rate, rd_cost->dist);
  2780. }
  2781. #if !CONFIG_REALTIME_ONLY
  2782. // This function is designed to apply a bias or adjustment to an rd value based
  2783. // on the relative variance of the source and reconstruction.
  2784. #define LOW_VAR_THRESH 250
  2785. #define VAR_MULT 250
  2786. static unsigned int max_var_adjust[VP9E_CONTENT_INVALID] = { 16, 16, 250 };
  2787. static void rd_variance_adjustment(VP9_COMP *cpi, MACROBLOCK *x,
  2788. BLOCK_SIZE bsize, int64_t *this_rd,
  2789. struct buf_2d *recon,
  2790. MV_REFERENCE_FRAME ref_frame,
  2791. MV_REFERENCE_FRAME second_ref_frame,
  2792. PREDICTION_MODE this_mode) {
  2793. MACROBLOCKD *const xd = &x->e_mbd;
  2794. unsigned int rec_variance;
  2795. unsigned int src_variance;
  2796. unsigned int src_rec_min;
  2797. unsigned int var_diff = 0;
  2798. unsigned int var_factor = 0;
  2799. unsigned int adj_max;
  2800. unsigned int low_var_thresh = LOW_VAR_THRESH;
  2801. const int bw = num_8x8_blocks_wide_lookup[bsize];
  2802. const int bh = num_8x8_blocks_high_lookup[bsize];
  2803. vp9e_tune_content content_type = cpi->oxcf.content;
  2804. if (*this_rd == INT64_MAX) return;
  2805. #if CONFIG_VP9_HIGHBITDEPTH
  2806. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  2807. rec_variance = vp9_high_get_sby_variance(cpi, recon, bsize, xd->bd);
  2808. src_variance =
  2809. vp9_high_get_sby_variance(cpi, &x->plane[0].src, bsize, xd->bd);
  2810. } else {
  2811. rec_variance = vp9_get_sby_variance(cpi, recon, bsize);
  2812. src_variance = vp9_get_sby_variance(cpi, &x->plane[0].src, bsize);
  2813. }
  2814. #else
  2815. rec_variance = vp9_get_sby_variance(cpi, recon, bsize);
  2816. src_variance = vp9_get_sby_variance(cpi, &x->plane[0].src, bsize);
  2817. #endif // CONFIG_VP9_HIGHBITDEPTH
  2818. // Scale based on area in 8x8 blocks
  2819. rec_variance /= (bw * bh);
  2820. src_variance /= (bw * bh);
  2821. if (content_type == VP9E_CONTENT_FILM) {
  2822. if (cpi->oxcf.pass == 2) {
  2823. // Adjust low variance threshold based on estimated group noise enegry.
  2824. double noise_factor =
  2825. (double)cpi->twopass.gf_group.group_noise_energy / SECTION_NOISE_DEF;
  2826. low_var_thresh = (unsigned int)(low_var_thresh * noise_factor);
  2827. if (ref_frame == INTRA_FRAME) {
  2828. low_var_thresh *= 2;
  2829. if (this_mode == DC_PRED) low_var_thresh *= 5;
  2830. } else if (second_ref_frame > INTRA_FRAME) {
  2831. low_var_thresh *= 2;
  2832. }
  2833. }
  2834. } else {
  2835. low_var_thresh = LOW_VAR_THRESH / 2;
  2836. }
  2837. // Lower of source (raw per pixel value) and recon variance. Note that
  2838. // if the source per pixel is 0 then the recon value here will not be per
  2839. // pixel (see above) so will likely be much larger.
  2840. src_rec_min = VPXMIN(src_variance, rec_variance);
  2841. if (src_rec_min > low_var_thresh) return;
  2842. // We care more when the reconstruction has lower variance so give this case
  2843. // a stronger weighting.
  2844. var_diff = (src_variance > rec_variance) ? (src_variance - rec_variance) * 2
  2845. : (rec_variance - src_variance) / 2;
  2846. adj_max = max_var_adjust[content_type];
  2847. var_factor =
  2848. (unsigned int)((int64_t)VAR_MULT * var_diff) / VPXMAX(1, src_variance);
  2849. var_factor = VPXMIN(adj_max, var_factor);
  2850. if ((content_type == VP9E_CONTENT_FILM) &&
  2851. ((ref_frame == INTRA_FRAME) || (second_ref_frame > INTRA_FRAME))) {
  2852. var_factor *= 2;
  2853. }
  2854. *this_rd += (*this_rd * var_factor) / 100;
  2855. (void)xd;
  2856. }
  2857. #endif // !CONFIG_REALTIME_ONLY
  2858. // Do we have an internal image edge (e.g. formatting bars).
  2859. int vp9_internal_image_edge(VP9_COMP *cpi) {
  2860. return (cpi->oxcf.pass == 2) &&
  2861. ((cpi->twopass.this_frame_stats.inactive_zone_rows > 0) ||
  2862. (cpi->twopass.this_frame_stats.inactive_zone_cols > 0));
  2863. }
  2864. // Checks to see if a super block is on a horizontal image edge.
  2865. // In most cases this is the "real" edge unless there are formatting
  2866. // bars embedded in the stream.
  2867. int vp9_active_h_edge(VP9_COMP *cpi, int mi_row, int mi_step) {
  2868. int top_edge = 0;
  2869. int bottom_edge = cpi->common.mi_rows;
  2870. int is_active_h_edge = 0;
  2871. // For two pass account for any formatting bars detected.
  2872. if (cpi->oxcf.pass == 2) {
  2873. TWO_PASS *twopass = &cpi->twopass;
  2874. // The inactive region is specified in MBs not mi units.
  2875. // The image edge is in the following MB row.
  2876. top_edge += (int)(twopass->this_frame_stats.inactive_zone_rows * 2);
  2877. bottom_edge -= (int)(twopass->this_frame_stats.inactive_zone_rows * 2);
  2878. bottom_edge = VPXMAX(top_edge, bottom_edge);
  2879. }
  2880. if (((top_edge >= mi_row) && (top_edge < (mi_row + mi_step))) ||
  2881. ((bottom_edge >= mi_row) && (bottom_edge < (mi_row + mi_step)))) {
  2882. is_active_h_edge = 1;
  2883. }
  2884. return is_active_h_edge;
  2885. }
  2886. // Checks to see if a super block is on a vertical image edge.
  2887. // In most cases this is the "real" edge unless there are formatting
  2888. // bars embedded in the stream.
  2889. int vp9_active_v_edge(VP9_COMP *cpi, int mi_col, int mi_step) {
  2890. int left_edge = 0;
  2891. int right_edge = cpi->common.mi_cols;
  2892. int is_active_v_edge = 0;
  2893. // For two pass account for any formatting bars detected.
  2894. if (cpi->oxcf.pass == 2) {
  2895. TWO_PASS *twopass = &cpi->twopass;
  2896. // The inactive region is specified in MBs not mi units.
  2897. // The image edge is in the following MB row.
  2898. left_edge += (int)(twopass->this_frame_stats.inactive_zone_cols * 2);
  2899. right_edge -= (int)(twopass->this_frame_stats.inactive_zone_cols * 2);
  2900. right_edge = VPXMAX(left_edge, right_edge);
  2901. }
  2902. if (((left_edge >= mi_col) && (left_edge < (mi_col + mi_step))) ||
  2903. ((right_edge >= mi_col) && (right_edge < (mi_col + mi_step)))) {
  2904. is_active_v_edge = 1;
  2905. }
  2906. return is_active_v_edge;
  2907. }
  2908. // Checks to see if a super block is at the edge of the active image.
  2909. // In most cases this is the "real" edge unless there are formatting
  2910. // bars embedded in the stream.
  2911. int vp9_active_edge_sb(VP9_COMP *cpi, int mi_row, int mi_col) {
  2912. return vp9_active_h_edge(cpi, mi_row, MI_BLOCK_SIZE) ||
  2913. vp9_active_v_edge(cpi, mi_col, MI_BLOCK_SIZE);
  2914. }
  2915. #if !CONFIG_REALTIME_ONLY
  2916. void vp9_rd_pick_inter_mode_sb(VP9_COMP *cpi, TileDataEnc *tile_data,
  2917. MACROBLOCK *x, int mi_row, int mi_col,
  2918. RD_COST *rd_cost, BLOCK_SIZE bsize,
  2919. PICK_MODE_CONTEXT *ctx, int64_t best_rd_so_far) {
  2920. VP9_COMMON *const cm = &cpi->common;
  2921. TileInfo *const tile_info = &tile_data->tile_info;
  2922. RD_OPT *const rd_opt = &cpi->rd;
  2923. SPEED_FEATURES *const sf = &cpi->sf;
  2924. MACROBLOCKD *const xd = &x->e_mbd;
  2925. MODE_INFO *const mi = xd->mi[0];
  2926. MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
  2927. const struct segmentation *const seg = &cm->seg;
  2928. PREDICTION_MODE this_mode;
  2929. MV_REFERENCE_FRAME ref_frame, second_ref_frame;
  2930. unsigned char segment_id = mi->segment_id;
  2931. int comp_pred, i, k;
  2932. int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES];
  2933. struct buf_2d yv12_mb[4][MAX_MB_PLANE];
  2934. int_mv single_newmv[MAX_REF_FRAMES] = { { 0 } };
  2935. INTERP_FILTER single_inter_filter[MB_MODE_COUNT][MAX_REF_FRAMES];
  2936. int single_skippable[MB_MODE_COUNT][MAX_REF_FRAMES];
  2937. static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
  2938. VP9_ALT_FLAG };
  2939. int64_t best_rd = best_rd_so_far;
  2940. int64_t best_pred_diff[REFERENCE_MODES];
  2941. int64_t best_pred_rd[REFERENCE_MODES];
  2942. int64_t best_filter_rd[SWITCHABLE_FILTER_CONTEXTS];
  2943. int64_t best_filter_diff[SWITCHABLE_FILTER_CONTEXTS];
  2944. MODE_INFO best_mbmode;
  2945. int best_mode_skippable = 0;
  2946. int midx, best_mode_index = -1;
  2947. unsigned int ref_costs_single[MAX_REF_FRAMES], ref_costs_comp[MAX_REF_FRAMES];
  2948. vpx_prob comp_mode_p;
  2949. int64_t best_intra_rd = INT64_MAX;
  2950. unsigned int best_pred_sse = UINT_MAX;
  2951. PREDICTION_MODE best_intra_mode = DC_PRED;
  2952. int rate_uv_intra[TX_SIZES], rate_uv_tokenonly[TX_SIZES];
  2953. int64_t dist_uv[TX_SIZES];
  2954. int skip_uv[TX_SIZES];
  2955. PREDICTION_MODE mode_uv[TX_SIZES];
  2956. const int intra_cost_penalty =
  2957. vp9_get_intra_cost_penalty(cpi, bsize, cm->base_qindex, cm->y_dc_delta_q);
  2958. int best_skip2 = 0;
  2959. uint8_t ref_frame_skip_mask[2] = { 0, 1 };
  2960. uint16_t mode_skip_mask[MAX_REF_FRAMES] = { 0 };
  2961. int mode_skip_start = sf->mode_skip_start + 1;
  2962. const int *const rd_threshes = rd_opt->threshes[segment_id][bsize];
  2963. const int *const rd_thresh_freq_fact = tile_data->thresh_freq_fact[bsize];
  2964. int64_t mode_threshold[MAX_MODES];
  2965. int8_t *tile_mode_map = tile_data->mode_map[bsize];
  2966. int8_t mode_map[MAX_MODES]; // Maintain mode_map information locally to avoid
  2967. // lock mechanism involved with reads from
  2968. // tile_mode_map
  2969. const int mode_search_skip_flags = sf->mode_search_skip_flags;
  2970. const int is_rect_partition =
  2971. num_4x4_blocks_wide_lookup[bsize] != num_4x4_blocks_high_lookup[bsize];
  2972. int64_t mask_filter = 0;
  2973. int64_t filter_cache[SWITCHABLE_FILTER_CONTEXTS];
  2974. struct buf_2d *recon;
  2975. struct buf_2d recon_buf;
  2976. #if CONFIG_VP9_HIGHBITDEPTH
  2977. DECLARE_ALIGNED(16, uint16_t, recon16[64 * 64]);
  2978. recon_buf.buf = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH
  2979. ? CONVERT_TO_BYTEPTR(recon16)
  2980. : (uint8_t *)recon16;
  2981. #else
  2982. DECLARE_ALIGNED(16, uint8_t, recon8[64 * 64]);
  2983. recon_buf.buf = recon8;
  2984. #endif // CONFIG_VP9_HIGHBITDEPTH
  2985. recon_buf.stride = 64;
  2986. recon = cpi->oxcf.content == VP9E_CONTENT_FILM ? &recon_buf : 0;
  2987. vp9_zero(best_mbmode);
  2988. x->skip_encode = sf->skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
  2989. for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i) filter_cache[i] = INT64_MAX;
  2990. estimate_ref_frame_costs(cm, xd, segment_id, ref_costs_single, ref_costs_comp,
  2991. &comp_mode_p);
  2992. for (i = 0; i < REFERENCE_MODES; ++i) best_pred_rd[i] = INT64_MAX;
  2993. for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
  2994. best_filter_rd[i] = INT64_MAX;
  2995. for (i = 0; i < TX_SIZES; i++) rate_uv_intra[i] = INT_MAX;
  2996. for (i = 0; i < MAX_REF_FRAMES; ++i) x->pred_sse[i] = INT_MAX;
  2997. for (i = 0; i < MB_MODE_COUNT; ++i) {
  2998. for (k = 0; k < MAX_REF_FRAMES; ++k) {
  2999. single_inter_filter[i][k] = SWITCHABLE;
  3000. single_skippable[i][k] = 0;
  3001. }
  3002. }
  3003. rd_cost->rate = INT_MAX;
  3004. for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
  3005. x->pred_mv_sad[ref_frame] = INT_MAX;
  3006. if ((cpi->ref_frame_flags & flag_list[ref_frame]) &&
  3007. !(is_rect_partition && (ctx->skip_ref_frame_mask & (1 << ref_frame)))) {
  3008. assert(get_ref_frame_buffer(cpi, ref_frame) != NULL);
  3009. setup_buffer_inter(cpi, x, ref_frame, bsize, mi_row, mi_col,
  3010. frame_mv[NEARESTMV], frame_mv[NEARMV], yv12_mb);
  3011. }
  3012. frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
  3013. frame_mv[ZEROMV][ref_frame].as_int = 0;
  3014. }
  3015. for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
  3016. if (!(cpi->ref_frame_flags & flag_list[ref_frame])) {
  3017. // Skip checking missing references in both single and compound reference
  3018. // modes. Note that a mode will be skipped if both reference frames
  3019. // are masked out.
  3020. ref_frame_skip_mask[0] |= (1 << ref_frame);
  3021. ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
  3022. } else if (sf->reference_masking) {
  3023. for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
  3024. // Skip fixed mv modes for poor references
  3025. if ((x->pred_mv_sad[ref_frame] >> 2) > x->pred_mv_sad[i]) {
  3026. mode_skip_mask[ref_frame] |= INTER_NEAREST_NEAR_ZERO;
  3027. break;
  3028. }
  3029. }
  3030. }
  3031. // If the segment reference frame feature is enabled....
  3032. // then do nothing if the current ref frame is not allowed..
  3033. if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) &&
  3034. get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame) {
  3035. ref_frame_skip_mask[0] |= (1 << ref_frame);
  3036. ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
  3037. }
  3038. }
  3039. // Disable this drop out case if the ref frame
  3040. // segment level feature is enabled for this segment. This is to
  3041. // prevent the possibility that we end up unable to pick any mode.
  3042. if (!segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) {
  3043. // Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
  3044. // unless ARNR filtering is enabled in which case we want
  3045. // an unfiltered alternative. We allow near/nearest as well
  3046. // because they may result in zero-zero MVs but be cheaper.
  3047. if (cpi->rc.is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0)) {
  3048. ref_frame_skip_mask[0] = (1 << LAST_FRAME) | (1 << GOLDEN_FRAME);
  3049. ref_frame_skip_mask[1] = SECOND_REF_FRAME_MASK;
  3050. mode_skip_mask[ALTREF_FRAME] = ~INTER_NEAREST_NEAR_ZERO;
  3051. if (frame_mv[NEARMV][ALTREF_FRAME].as_int != 0)
  3052. mode_skip_mask[ALTREF_FRAME] |= (1 << NEARMV);
  3053. if (frame_mv[NEARESTMV][ALTREF_FRAME].as_int != 0)
  3054. mode_skip_mask[ALTREF_FRAME] |= (1 << NEARESTMV);
  3055. }
  3056. }
  3057. if (cpi->rc.is_src_frame_alt_ref) {
  3058. if (sf->alt_ref_search_fp) {
  3059. mode_skip_mask[ALTREF_FRAME] = 0;
  3060. ref_frame_skip_mask[0] = ~(1 << ALTREF_FRAME);
  3061. ref_frame_skip_mask[1] = SECOND_REF_FRAME_MASK;
  3062. }
  3063. }
  3064. if (sf->alt_ref_search_fp)
  3065. if (!cm->show_frame && x->pred_mv_sad[GOLDEN_FRAME] < INT_MAX)
  3066. if (x->pred_mv_sad[ALTREF_FRAME] > (x->pred_mv_sad[GOLDEN_FRAME] << 1))
  3067. mode_skip_mask[ALTREF_FRAME] |= INTER_ALL;
  3068. if (sf->adaptive_mode_search) {
  3069. if (cm->show_frame && !cpi->rc.is_src_frame_alt_ref &&
  3070. cpi->rc.frames_since_golden >= 3)
  3071. if (x->pred_mv_sad[GOLDEN_FRAME] > (x->pred_mv_sad[LAST_FRAME] << 1))
  3072. mode_skip_mask[GOLDEN_FRAME] |= INTER_ALL;
  3073. }
  3074. if (bsize > sf->max_intra_bsize) {
  3075. ref_frame_skip_mask[0] |= (1 << INTRA_FRAME);
  3076. ref_frame_skip_mask[1] |= (1 << INTRA_FRAME);
  3077. }
  3078. mode_skip_mask[INTRA_FRAME] |=
  3079. ~(sf->intra_y_mode_mask[max_txsize_lookup[bsize]]);
  3080. for (i = 0; i <= LAST_NEW_MV_INDEX; ++i) mode_threshold[i] = 0;
  3081. for (i = LAST_NEW_MV_INDEX + 1; i < MAX_MODES; ++i)
  3082. mode_threshold[i] = ((int64_t)rd_threshes[i] * rd_thresh_freq_fact[i]) >> 5;
  3083. midx = sf->schedule_mode_search ? mode_skip_start : 0;
  3084. while (midx > 4) {
  3085. uint8_t end_pos = 0;
  3086. for (i = 5; i < midx; ++i) {
  3087. if (mode_threshold[tile_mode_map[i - 1]] >
  3088. mode_threshold[tile_mode_map[i]]) {
  3089. uint8_t tmp = tile_mode_map[i];
  3090. tile_mode_map[i] = tile_mode_map[i - 1];
  3091. tile_mode_map[i - 1] = tmp;
  3092. end_pos = i;
  3093. }
  3094. }
  3095. midx = end_pos;
  3096. }
  3097. memcpy(mode_map, tile_mode_map, sizeof(mode_map));
  3098. for (midx = 0; midx < MAX_MODES; ++midx) {
  3099. int mode_index = mode_map[midx];
  3100. int mode_excluded = 0;
  3101. int64_t this_rd = INT64_MAX;
  3102. int disable_skip = 0;
  3103. int compmode_cost = 0;
  3104. int rate2 = 0, rate_y = 0, rate_uv = 0;
  3105. int64_t distortion2 = 0, distortion_y = 0, distortion_uv = 0;
  3106. int skippable = 0;
  3107. int this_skip2 = 0;
  3108. int64_t total_sse = INT64_MAX;
  3109. int early_term = 0;
  3110. this_mode = vp9_mode_order[mode_index].mode;
  3111. ref_frame = vp9_mode_order[mode_index].ref_frame[0];
  3112. second_ref_frame = vp9_mode_order[mode_index].ref_frame[1];
  3113. vp9_zero(x->sum_y_eobs);
  3114. if (is_rect_partition) {
  3115. if (ctx->skip_ref_frame_mask & (1 << ref_frame)) continue;
  3116. if (second_ref_frame > 0 &&
  3117. (ctx->skip_ref_frame_mask & (1 << second_ref_frame)))
  3118. continue;
  3119. }
  3120. // Look at the reference frame of the best mode so far and set the
  3121. // skip mask to look at a subset of the remaining modes.
  3122. if (midx == mode_skip_start && best_mode_index >= 0) {
  3123. switch (best_mbmode.ref_frame[0]) {
  3124. case INTRA_FRAME: break;
  3125. case LAST_FRAME: ref_frame_skip_mask[0] |= LAST_FRAME_MODE_MASK; break;
  3126. case GOLDEN_FRAME:
  3127. ref_frame_skip_mask[0] |= GOLDEN_FRAME_MODE_MASK;
  3128. break;
  3129. case ALTREF_FRAME: ref_frame_skip_mask[0] |= ALT_REF_MODE_MASK; break;
  3130. case NONE:
  3131. case MAX_REF_FRAMES: assert(0 && "Invalid Reference frame"); break;
  3132. }
  3133. }
  3134. if ((ref_frame_skip_mask[0] & (1 << ref_frame)) &&
  3135. (ref_frame_skip_mask[1] & (1 << VPXMAX(0, second_ref_frame))))
  3136. continue;
  3137. if (mode_skip_mask[ref_frame] & (1 << this_mode)) continue;
  3138. // Test best rd so far against threshold for trying this mode.
  3139. if (best_mode_skippable && sf->schedule_mode_search)
  3140. mode_threshold[mode_index] <<= 1;
  3141. if (best_rd < mode_threshold[mode_index]) continue;
  3142. // This is only used in motion vector unit test.
  3143. if (cpi->oxcf.motion_vector_unit_test && ref_frame == INTRA_FRAME) continue;
  3144. if (sf->motion_field_mode_search) {
  3145. const int mi_width = VPXMIN(num_8x8_blocks_wide_lookup[bsize],
  3146. tile_info->mi_col_end - mi_col);
  3147. const int mi_height = VPXMIN(num_8x8_blocks_high_lookup[bsize],
  3148. tile_info->mi_row_end - mi_row);
  3149. const int bsl = mi_width_log2_lookup[bsize];
  3150. int cb_partition_search_ctrl =
  3151. (((mi_row + mi_col) >> bsl) +
  3152. get_chessboard_index(cm->current_video_frame)) &
  3153. 0x1;
  3154. MODE_INFO *ref_mi;
  3155. int const_motion = 1;
  3156. int skip_ref_frame = !cb_partition_search_ctrl;
  3157. MV_REFERENCE_FRAME rf = NONE;
  3158. int_mv ref_mv;
  3159. ref_mv.as_int = INVALID_MV;
  3160. if ((mi_row - 1) >= tile_info->mi_row_start) {
  3161. ref_mv = xd->mi[-xd->mi_stride]->mv[0];
  3162. rf = xd->mi[-xd->mi_stride]->ref_frame[0];
  3163. for (i = 0; i < mi_width; ++i) {
  3164. ref_mi = xd->mi[-xd->mi_stride + i];
  3165. const_motion &= (ref_mv.as_int == ref_mi->mv[0].as_int) &&
  3166. (ref_frame == ref_mi->ref_frame[0]);
  3167. skip_ref_frame &= (rf == ref_mi->ref_frame[0]);
  3168. }
  3169. }
  3170. if ((mi_col - 1) >= tile_info->mi_col_start) {
  3171. if (ref_mv.as_int == INVALID_MV) ref_mv = xd->mi[-1]->mv[0];
  3172. if (rf == NONE) rf = xd->mi[-1]->ref_frame[0];
  3173. for (i = 0; i < mi_height; ++i) {
  3174. ref_mi = xd->mi[i * xd->mi_stride - 1];
  3175. const_motion &= (ref_mv.as_int == ref_mi->mv[0].as_int) &&
  3176. (ref_frame == ref_mi->ref_frame[0]);
  3177. skip_ref_frame &= (rf == ref_mi->ref_frame[0]);
  3178. }
  3179. }
  3180. if (skip_ref_frame && this_mode != NEARESTMV && this_mode != NEWMV)
  3181. if (rf > INTRA_FRAME)
  3182. if (ref_frame != rf) continue;
  3183. if (const_motion)
  3184. if (this_mode == NEARMV || this_mode == ZEROMV) continue;
  3185. }
  3186. comp_pred = second_ref_frame > INTRA_FRAME;
  3187. if (comp_pred) {
  3188. if (!cpi->allow_comp_inter_inter) continue;
  3189. if (cm->ref_frame_sign_bias[ref_frame] ==
  3190. cm->ref_frame_sign_bias[second_ref_frame])
  3191. continue;
  3192. // Skip compound inter modes if ARF is not available.
  3193. if (!(cpi->ref_frame_flags & flag_list[second_ref_frame])) continue;
  3194. // Do not allow compound prediction if the segment level reference frame
  3195. // feature is in use as in this case there can only be one reference.
  3196. if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) continue;
  3197. if ((mode_search_skip_flags & FLAG_SKIP_COMP_BESTINTRA) &&
  3198. best_mode_index >= 0 && best_mbmode.ref_frame[0] == INTRA_FRAME)
  3199. continue;
  3200. mode_excluded = cm->reference_mode == SINGLE_REFERENCE;
  3201. } else {
  3202. if (ref_frame != INTRA_FRAME)
  3203. mode_excluded = cm->reference_mode == COMPOUND_REFERENCE;
  3204. }
  3205. if (ref_frame == INTRA_FRAME) {
  3206. if (sf->adaptive_mode_search)
  3207. if ((x->source_variance << num_pels_log2_lookup[bsize]) > best_pred_sse)
  3208. continue;
  3209. if (this_mode != DC_PRED) {
  3210. // Disable intra modes other than DC_PRED for blocks with low variance
  3211. // Threshold for intra skipping based on source variance
  3212. // TODO(debargha): Specialize the threshold for super block sizes
  3213. const unsigned int skip_intra_var_thresh =
  3214. (cpi->oxcf.content == VP9E_CONTENT_FILM) ? 0 : 64;
  3215. if ((mode_search_skip_flags & FLAG_SKIP_INTRA_LOWVAR) &&
  3216. x->source_variance < skip_intra_var_thresh)
  3217. continue;
  3218. // Only search the oblique modes if the best so far is
  3219. // one of the neighboring directional modes
  3220. if ((mode_search_skip_flags & FLAG_SKIP_INTRA_BESTINTER) &&
  3221. (this_mode >= D45_PRED && this_mode <= TM_PRED)) {
  3222. if (best_mode_index >= 0 && best_mbmode.ref_frame[0] > INTRA_FRAME)
  3223. continue;
  3224. }
  3225. if (mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) {
  3226. if (conditional_skipintra(this_mode, best_intra_mode)) continue;
  3227. }
  3228. }
  3229. } else {
  3230. const MV_REFERENCE_FRAME ref_frames[2] = { ref_frame, second_ref_frame };
  3231. if (!check_best_zero_mv(cpi, mbmi_ext->mode_context, frame_mv, this_mode,
  3232. ref_frames))
  3233. continue;
  3234. }
  3235. mi->mode = this_mode;
  3236. mi->uv_mode = DC_PRED;
  3237. mi->ref_frame[0] = ref_frame;
  3238. mi->ref_frame[1] = second_ref_frame;
  3239. // Evaluate all sub-pel filters irrespective of whether we can use
  3240. // them for this frame.
  3241. mi->interp_filter =
  3242. cm->interp_filter == SWITCHABLE ? EIGHTTAP : cm->interp_filter;
  3243. mi->mv[0].as_int = mi->mv[1].as_int = 0;
  3244. x->skip = 0;
  3245. set_ref_ptrs(cm, xd, ref_frame, second_ref_frame);
  3246. // Select prediction reference frames.
  3247. for (i = 0; i < MAX_MB_PLANE; i++) {
  3248. xd->plane[i].pre[0] = yv12_mb[ref_frame][i];
  3249. if (comp_pred) xd->plane[i].pre[1] = yv12_mb[second_ref_frame][i];
  3250. }
  3251. if (ref_frame == INTRA_FRAME) {
  3252. TX_SIZE uv_tx;
  3253. struct macroblockd_plane *const pd = &xd->plane[1];
  3254. memset(x->skip_txfm, 0, sizeof(x->skip_txfm));
  3255. super_block_yrd(cpi, x, &rate_y, &distortion_y, &skippable, NULL, bsize,
  3256. best_rd, recon);
  3257. if (rate_y == INT_MAX) continue;
  3258. uv_tx = uv_txsize_lookup[bsize][mi->tx_size][pd->subsampling_x]
  3259. [pd->subsampling_y];
  3260. if (rate_uv_intra[uv_tx] == INT_MAX) {
  3261. choose_intra_uv_mode(cpi, x, ctx, bsize, uv_tx, &rate_uv_intra[uv_tx],
  3262. &rate_uv_tokenonly[uv_tx], &dist_uv[uv_tx],
  3263. &skip_uv[uv_tx], &mode_uv[uv_tx]);
  3264. }
  3265. rate_uv = rate_uv_tokenonly[uv_tx];
  3266. distortion_uv = dist_uv[uv_tx];
  3267. skippable = skippable && skip_uv[uv_tx];
  3268. mi->uv_mode = mode_uv[uv_tx];
  3269. rate2 = rate_y + cpi->mbmode_cost[mi->mode] + rate_uv_intra[uv_tx];
  3270. if (this_mode != DC_PRED && this_mode != TM_PRED)
  3271. rate2 += intra_cost_penalty;
  3272. distortion2 = distortion_y + distortion_uv;
  3273. } else {
  3274. this_rd = handle_inter_mode(
  3275. cpi, x, bsize, &rate2, &distortion2, &skippable, &rate_y, &rate_uv,
  3276. recon, &disable_skip, frame_mv, mi_row, mi_col, single_newmv,
  3277. single_inter_filter, single_skippable, &total_sse, best_rd,
  3278. &mask_filter, filter_cache);
  3279. if (this_rd == INT64_MAX) continue;
  3280. compmode_cost = vp9_cost_bit(comp_mode_p, comp_pred);
  3281. if (cm->reference_mode == REFERENCE_MODE_SELECT) rate2 += compmode_cost;
  3282. }
  3283. // Estimate the reference frame signaling cost and add it
  3284. // to the rolling cost variable.
  3285. if (comp_pred) {
  3286. rate2 += ref_costs_comp[ref_frame];
  3287. } else {
  3288. rate2 += ref_costs_single[ref_frame];
  3289. }
  3290. if (!disable_skip) {
  3291. const vpx_prob skip_prob = vp9_get_skip_prob(cm, xd);
  3292. const int skip_cost0 = vp9_cost_bit(skip_prob, 0);
  3293. const int skip_cost1 = vp9_cost_bit(skip_prob, 1);
  3294. if (skippable) {
  3295. // Back out the coefficient coding costs
  3296. rate2 -= (rate_y + rate_uv);
  3297. // Cost the skip mb case
  3298. rate2 += skip_cost1;
  3299. } else if (ref_frame != INTRA_FRAME && !xd->lossless &&
  3300. !cpi->oxcf.sharpness) {
  3301. if (RDCOST(x->rdmult, x->rddiv, rate_y + rate_uv + skip_cost0,
  3302. distortion2) <
  3303. RDCOST(x->rdmult, x->rddiv, skip_cost1, total_sse)) {
  3304. // Add in the cost of the no skip flag.
  3305. rate2 += skip_cost0;
  3306. } else {
  3307. // FIXME(rbultje) make this work for splitmv also
  3308. assert(total_sse >= 0);
  3309. rate2 += skip_cost1;
  3310. distortion2 = total_sse;
  3311. rate2 -= (rate_y + rate_uv);
  3312. this_skip2 = 1;
  3313. }
  3314. } else {
  3315. // Add in the cost of the no skip flag.
  3316. rate2 += skip_cost0;
  3317. }
  3318. // Calculate the final RD estimate for this mode.
  3319. this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
  3320. }
  3321. if (recon) {
  3322. // In film mode bias against DC pred and other intra if there is a
  3323. // significant difference between the variance of the sub blocks in the
  3324. // the source. Also apply some bias against compound modes which also
  3325. // tend to blur fine texture such as film grain over time.
  3326. //
  3327. // The sub block test here acts in the case where one or more sub
  3328. // blocks have high relatively variance but others relatively low
  3329. // variance. Here the high variance sub blocks may push the
  3330. // total variance for the current block size over the thresholds
  3331. // used in rd_variance_adjustment() below.
  3332. if (cpi->oxcf.content == VP9E_CONTENT_FILM) {
  3333. if (bsize >= BLOCK_16X16) {
  3334. int min_energy, max_energy;
  3335. vp9_get_sub_block_energy(cpi, x, mi_row, mi_col, bsize, &min_energy,
  3336. &max_energy);
  3337. if (max_energy > min_energy) {
  3338. if (ref_frame == INTRA_FRAME) {
  3339. if (this_mode == DC_PRED)
  3340. this_rd += (this_rd * (max_energy - min_energy));
  3341. else
  3342. this_rd += (this_rd * (max_energy - min_energy)) / 4;
  3343. } else if (second_ref_frame > INTRA_FRAME) {
  3344. this_rd += this_rd / 4;
  3345. }
  3346. }
  3347. }
  3348. }
  3349. // Apply an adjustment to the rd value based on the similarity of the
  3350. // source variance and reconstructed variance.
  3351. rd_variance_adjustment(cpi, x, bsize, &this_rd, recon, ref_frame,
  3352. second_ref_frame, this_mode);
  3353. }
  3354. if (ref_frame == INTRA_FRAME) {
  3355. // Keep record of best intra rd
  3356. if (this_rd < best_intra_rd) {
  3357. best_intra_rd = this_rd;
  3358. best_intra_mode = mi->mode;
  3359. }
  3360. }
  3361. if (!disable_skip && ref_frame == INTRA_FRAME) {
  3362. for (i = 0; i < REFERENCE_MODES; ++i)
  3363. best_pred_rd[i] = VPXMIN(best_pred_rd[i], this_rd);
  3364. for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
  3365. best_filter_rd[i] = VPXMIN(best_filter_rd[i], this_rd);
  3366. }
  3367. // Did this mode help.. i.e. is it the new best mode
  3368. if (this_rd < best_rd || x->skip) {
  3369. int max_plane = MAX_MB_PLANE;
  3370. if (!mode_excluded) {
  3371. // Note index of best mode so far
  3372. best_mode_index = mode_index;
  3373. if (ref_frame == INTRA_FRAME) {
  3374. /* required for left and above block mv */
  3375. mi->mv[0].as_int = 0;
  3376. max_plane = 1;
  3377. // Initialize interp_filter here so we do not have to check for
  3378. // inter block modes in get_pred_context_switchable_interp()
  3379. mi->interp_filter = SWITCHABLE_FILTERS;
  3380. } else {
  3381. best_pred_sse = x->pred_sse[ref_frame];
  3382. }
  3383. rd_cost->rate = rate2;
  3384. rd_cost->dist = distortion2;
  3385. rd_cost->rdcost = this_rd;
  3386. best_rd = this_rd;
  3387. best_mbmode = *mi;
  3388. best_skip2 = this_skip2;
  3389. best_mode_skippable = skippable;
  3390. if (!x->select_tx_size) swap_block_ptr(x, ctx, 1, 0, 0, max_plane);
  3391. memcpy(ctx->zcoeff_blk, x->zcoeff_blk[mi->tx_size],
  3392. sizeof(ctx->zcoeff_blk[0]) * ctx->num_4x4_blk);
  3393. ctx->sum_y_eobs = x->sum_y_eobs[mi->tx_size];
  3394. // TODO(debargha): enhance this test with a better distortion prediction
  3395. // based on qp, activity mask and history
  3396. if ((mode_search_skip_flags & FLAG_EARLY_TERMINATE) &&
  3397. (mode_index > MIN_EARLY_TERM_INDEX)) {
  3398. int qstep = xd->plane[0].dequant[1];
  3399. // TODO(debargha): Enhance this by specializing for each mode_index
  3400. int scale = 4;
  3401. #if CONFIG_VP9_HIGHBITDEPTH
  3402. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  3403. qstep >>= (xd->bd - 8);
  3404. }
  3405. #endif // CONFIG_VP9_HIGHBITDEPTH
  3406. if (x->source_variance < UINT_MAX) {
  3407. const int var_adjust = (x->source_variance < 16);
  3408. scale -= var_adjust;
  3409. }
  3410. if (ref_frame > INTRA_FRAME && distortion2 * scale < qstep * qstep) {
  3411. early_term = 1;
  3412. }
  3413. }
  3414. }
  3415. }
  3416. /* keep record of best compound/single-only prediction */
  3417. if (!disable_skip && ref_frame != INTRA_FRAME) {
  3418. int64_t single_rd, hybrid_rd, single_rate, hybrid_rate;
  3419. if (cm->reference_mode == REFERENCE_MODE_SELECT) {
  3420. single_rate = rate2 - compmode_cost;
  3421. hybrid_rate = rate2;
  3422. } else {
  3423. single_rate = rate2;
  3424. hybrid_rate = rate2 + compmode_cost;
  3425. }
  3426. single_rd = RDCOST(x->rdmult, x->rddiv, single_rate, distortion2);
  3427. hybrid_rd = RDCOST(x->rdmult, x->rddiv, hybrid_rate, distortion2);
  3428. if (!comp_pred) {
  3429. if (single_rd < best_pred_rd[SINGLE_REFERENCE])
  3430. best_pred_rd[SINGLE_REFERENCE] = single_rd;
  3431. } else {
  3432. if (single_rd < best_pred_rd[COMPOUND_REFERENCE])
  3433. best_pred_rd[COMPOUND_REFERENCE] = single_rd;
  3434. }
  3435. if (hybrid_rd < best_pred_rd[REFERENCE_MODE_SELECT])
  3436. best_pred_rd[REFERENCE_MODE_SELECT] = hybrid_rd;
  3437. /* keep record of best filter type */
  3438. if (!mode_excluded && cm->interp_filter != BILINEAR) {
  3439. int64_t ref =
  3440. filter_cache[cm->interp_filter == SWITCHABLE ? SWITCHABLE_FILTERS
  3441. : cm->interp_filter];
  3442. for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) {
  3443. int64_t adj_rd;
  3444. if (ref == INT64_MAX)
  3445. adj_rd = 0;
  3446. else if (filter_cache[i] == INT64_MAX)
  3447. // when early termination is triggered, the encoder does not have
  3448. // access to the rate-distortion cost. it only knows that the cost
  3449. // should be above the maximum valid value. hence it takes the known
  3450. // maximum plus an arbitrary constant as the rate-distortion cost.
  3451. adj_rd = mask_filter - ref + 10;
  3452. else
  3453. adj_rd = filter_cache[i] - ref;
  3454. adj_rd += this_rd;
  3455. best_filter_rd[i] = VPXMIN(best_filter_rd[i], adj_rd);
  3456. }
  3457. }
  3458. }
  3459. if (early_term) break;
  3460. if (x->skip && !comp_pred) break;
  3461. }
  3462. // The inter modes' rate costs are not calculated precisely in some cases.
  3463. // Therefore, sometimes, NEWMV is chosen instead of NEARESTMV, NEARMV, and
  3464. // ZEROMV. Here, checks are added for those cases, and the mode decisions
  3465. // are corrected.
  3466. if (best_mbmode.mode == NEWMV) {
  3467. const MV_REFERENCE_FRAME refs[2] = { best_mbmode.ref_frame[0],
  3468. best_mbmode.ref_frame[1] };
  3469. int comp_pred_mode = refs[1] > INTRA_FRAME;
  3470. if (frame_mv[NEARESTMV][refs[0]].as_int == best_mbmode.mv[0].as_int &&
  3471. ((comp_pred_mode &&
  3472. frame_mv[NEARESTMV][refs[1]].as_int == best_mbmode.mv[1].as_int) ||
  3473. !comp_pred_mode))
  3474. best_mbmode.mode = NEARESTMV;
  3475. else if (frame_mv[NEARMV][refs[0]].as_int == best_mbmode.mv[0].as_int &&
  3476. ((comp_pred_mode &&
  3477. frame_mv[NEARMV][refs[1]].as_int == best_mbmode.mv[1].as_int) ||
  3478. !comp_pred_mode))
  3479. best_mbmode.mode = NEARMV;
  3480. else if (best_mbmode.mv[0].as_int == 0 &&
  3481. ((comp_pred_mode && best_mbmode.mv[1].as_int == 0) ||
  3482. !comp_pred_mode))
  3483. best_mbmode.mode = ZEROMV;
  3484. }
  3485. if (best_mode_index < 0 || best_rd >= best_rd_so_far) {
  3486. // If adaptive interp filter is enabled, then the current leaf node of 8x8
  3487. // data is needed for sub8x8. Hence preserve the context.
  3488. #if CONFIG_CONSISTENT_RECODE
  3489. if (bsize == BLOCK_8X8) ctx->mic = *xd->mi[0];
  3490. #else
  3491. if (cpi->row_mt && bsize == BLOCK_8X8) ctx->mic = *xd->mi[0];
  3492. #endif
  3493. rd_cost->rate = INT_MAX;
  3494. rd_cost->rdcost = INT64_MAX;
  3495. return;
  3496. }
  3497. // If we used an estimate for the uv intra rd in the loop above...
  3498. if (sf->use_uv_intra_rd_estimate) {
  3499. // Do Intra UV best rd mode selection if best mode choice above was intra.
  3500. if (best_mbmode.ref_frame[0] == INTRA_FRAME) {
  3501. TX_SIZE uv_tx_size;
  3502. *mi = best_mbmode;
  3503. uv_tx_size = get_uv_tx_size(mi, &xd->plane[1]);
  3504. rd_pick_intra_sbuv_mode(cpi, x, ctx, &rate_uv_intra[uv_tx_size],
  3505. &rate_uv_tokenonly[uv_tx_size],
  3506. &dist_uv[uv_tx_size], &skip_uv[uv_tx_size],
  3507. bsize < BLOCK_8X8 ? BLOCK_8X8 : bsize,
  3508. uv_tx_size);
  3509. }
  3510. }
  3511. assert((cm->interp_filter == SWITCHABLE) ||
  3512. (cm->interp_filter == best_mbmode.interp_filter) ||
  3513. !is_inter_block(&best_mbmode));
  3514. if (!cpi->rc.is_src_frame_alt_ref)
  3515. vp9_update_rd_thresh_fact(tile_data->thresh_freq_fact,
  3516. sf->adaptive_rd_thresh, bsize, best_mode_index);
  3517. // macroblock modes
  3518. *mi = best_mbmode;
  3519. x->skip |= best_skip2;
  3520. for (i = 0; i < REFERENCE_MODES; ++i) {
  3521. if (best_pred_rd[i] == INT64_MAX)
  3522. best_pred_diff[i] = INT_MIN;
  3523. else
  3524. best_pred_diff[i] = best_rd - best_pred_rd[i];
  3525. }
  3526. if (!x->skip) {
  3527. for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) {
  3528. if (best_filter_rd[i] == INT64_MAX)
  3529. best_filter_diff[i] = 0;
  3530. else
  3531. best_filter_diff[i] = best_rd - best_filter_rd[i];
  3532. }
  3533. if (cm->interp_filter == SWITCHABLE)
  3534. assert(best_filter_diff[SWITCHABLE_FILTERS] == 0);
  3535. } else {
  3536. vp9_zero(best_filter_diff);
  3537. }
  3538. // TODO(yunqingwang): Moving this line in front of the above best_filter_diff
  3539. // updating code causes PSNR loss. Need to figure out the confliction.
  3540. x->skip |= best_mode_skippable;
  3541. if (!x->skip && !x->select_tx_size) {
  3542. int has_high_freq_coeff = 0;
  3543. int plane;
  3544. int max_plane = is_inter_block(xd->mi[0]) ? MAX_MB_PLANE : 1;
  3545. for (plane = 0; plane < max_plane; ++plane) {
  3546. x->plane[plane].eobs = ctx->eobs_pbuf[plane][1];
  3547. has_high_freq_coeff |= vp9_has_high_freq_in_plane(x, bsize, plane);
  3548. }
  3549. for (plane = max_plane; plane < MAX_MB_PLANE; ++plane) {
  3550. x->plane[plane].eobs = ctx->eobs_pbuf[plane][2];
  3551. has_high_freq_coeff |= vp9_has_high_freq_in_plane(x, bsize, plane);
  3552. }
  3553. best_mode_skippable |= !has_high_freq_coeff;
  3554. }
  3555. assert(best_mode_index >= 0);
  3556. store_coding_context(x, ctx, best_mode_index, best_pred_diff,
  3557. best_filter_diff, best_mode_skippable);
  3558. }
  3559. void vp9_rd_pick_inter_mode_sb_seg_skip(VP9_COMP *cpi, TileDataEnc *tile_data,
  3560. MACROBLOCK *x, RD_COST *rd_cost,
  3561. BLOCK_SIZE bsize,
  3562. PICK_MODE_CONTEXT *ctx,
  3563. int64_t best_rd_so_far) {
  3564. VP9_COMMON *const cm = &cpi->common;
  3565. MACROBLOCKD *const xd = &x->e_mbd;
  3566. MODE_INFO *const mi = xd->mi[0];
  3567. unsigned char segment_id = mi->segment_id;
  3568. const int comp_pred = 0;
  3569. int i;
  3570. int64_t best_pred_diff[REFERENCE_MODES];
  3571. int64_t best_filter_diff[SWITCHABLE_FILTER_CONTEXTS];
  3572. unsigned int ref_costs_single[MAX_REF_FRAMES], ref_costs_comp[MAX_REF_FRAMES];
  3573. vpx_prob comp_mode_p;
  3574. INTERP_FILTER best_filter = SWITCHABLE;
  3575. int64_t this_rd = INT64_MAX;
  3576. int rate2 = 0;
  3577. const int64_t distortion2 = 0;
  3578. x->skip_encode = cpi->sf.skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
  3579. estimate_ref_frame_costs(cm, xd, segment_id, ref_costs_single, ref_costs_comp,
  3580. &comp_mode_p);
  3581. for (i = 0; i < MAX_REF_FRAMES; ++i) x->pred_sse[i] = INT_MAX;
  3582. for (i = LAST_FRAME; i < MAX_REF_FRAMES; ++i) x->pred_mv_sad[i] = INT_MAX;
  3583. rd_cost->rate = INT_MAX;
  3584. assert(segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP));
  3585. mi->mode = ZEROMV;
  3586. mi->uv_mode = DC_PRED;
  3587. mi->ref_frame[0] = LAST_FRAME;
  3588. mi->ref_frame[1] = NONE;
  3589. mi->mv[0].as_int = 0;
  3590. x->skip = 1;
  3591. ctx->sum_y_eobs = 0;
  3592. if (cm->interp_filter != BILINEAR) {
  3593. best_filter = EIGHTTAP;
  3594. if (cm->interp_filter == SWITCHABLE &&
  3595. x->source_variance >= cpi->sf.disable_filter_search_var_thresh) {
  3596. int rs;
  3597. int best_rs = INT_MAX;
  3598. for (i = 0; i < SWITCHABLE_FILTERS; ++i) {
  3599. mi->interp_filter = i;
  3600. rs = vp9_get_switchable_rate(cpi, xd);
  3601. if (rs < best_rs) {
  3602. best_rs = rs;
  3603. best_filter = mi->interp_filter;
  3604. }
  3605. }
  3606. }
  3607. }
  3608. // Set the appropriate filter
  3609. if (cm->interp_filter == SWITCHABLE) {
  3610. mi->interp_filter = best_filter;
  3611. rate2 += vp9_get_switchable_rate(cpi, xd);
  3612. } else {
  3613. mi->interp_filter = cm->interp_filter;
  3614. }
  3615. if (cm->reference_mode == REFERENCE_MODE_SELECT)
  3616. rate2 += vp9_cost_bit(comp_mode_p, comp_pred);
  3617. // Estimate the reference frame signaling cost and add it
  3618. // to the rolling cost variable.
  3619. rate2 += ref_costs_single[LAST_FRAME];
  3620. this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
  3621. rd_cost->rate = rate2;
  3622. rd_cost->dist = distortion2;
  3623. rd_cost->rdcost = this_rd;
  3624. if (this_rd >= best_rd_so_far) {
  3625. rd_cost->rate = INT_MAX;
  3626. rd_cost->rdcost = INT64_MAX;
  3627. return;
  3628. }
  3629. assert((cm->interp_filter == SWITCHABLE) ||
  3630. (cm->interp_filter == mi->interp_filter));
  3631. vp9_update_rd_thresh_fact(tile_data->thresh_freq_fact,
  3632. cpi->sf.adaptive_rd_thresh, bsize, THR_ZEROMV);
  3633. vp9_zero(best_pred_diff);
  3634. vp9_zero(best_filter_diff);
  3635. if (!x->select_tx_size) swap_block_ptr(x, ctx, 1, 0, 0, MAX_MB_PLANE);
  3636. store_coding_context(x, ctx, THR_ZEROMV, best_pred_diff, best_filter_diff, 0);
  3637. }
  3638. void vp9_rd_pick_inter_mode_sub8x8(VP9_COMP *cpi, TileDataEnc *tile_data,
  3639. MACROBLOCK *x, int mi_row, int mi_col,
  3640. RD_COST *rd_cost, BLOCK_SIZE bsize,
  3641. PICK_MODE_CONTEXT *ctx,
  3642. int64_t best_rd_so_far) {
  3643. VP9_COMMON *const cm = &cpi->common;
  3644. RD_OPT *const rd_opt = &cpi->rd;
  3645. SPEED_FEATURES *const sf = &cpi->sf;
  3646. MACROBLOCKD *const xd = &x->e_mbd;
  3647. MODE_INFO *const mi = xd->mi[0];
  3648. const struct segmentation *const seg = &cm->seg;
  3649. MV_REFERENCE_FRAME ref_frame, second_ref_frame;
  3650. unsigned char segment_id = mi->segment_id;
  3651. int comp_pred, i;
  3652. int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES];
  3653. struct buf_2d yv12_mb[4][MAX_MB_PLANE];
  3654. static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
  3655. VP9_ALT_FLAG };
  3656. int64_t best_rd = best_rd_so_far;
  3657. int64_t best_yrd = best_rd_so_far; // FIXME(rbultje) more precise
  3658. int64_t best_pred_diff[REFERENCE_MODES];
  3659. int64_t best_pred_rd[REFERENCE_MODES];
  3660. int64_t best_filter_rd[SWITCHABLE_FILTER_CONTEXTS];
  3661. int64_t best_filter_diff[SWITCHABLE_FILTER_CONTEXTS];
  3662. MODE_INFO best_mbmode;
  3663. int ref_index, best_ref_index = 0;
  3664. unsigned int ref_costs_single[MAX_REF_FRAMES], ref_costs_comp[MAX_REF_FRAMES];
  3665. vpx_prob comp_mode_p;
  3666. INTERP_FILTER tmp_best_filter = SWITCHABLE;
  3667. int rate_uv_intra, rate_uv_tokenonly;
  3668. int64_t dist_uv;
  3669. int skip_uv;
  3670. PREDICTION_MODE mode_uv = DC_PRED;
  3671. const int intra_cost_penalty =
  3672. vp9_get_intra_cost_penalty(cpi, bsize, cm->base_qindex, cm->y_dc_delta_q);
  3673. int_mv seg_mvs[4][MAX_REF_FRAMES];
  3674. b_mode_info best_bmodes[4];
  3675. int best_skip2 = 0;
  3676. int ref_frame_skip_mask[2] = { 0 };
  3677. int64_t mask_filter = 0;
  3678. int64_t filter_cache[SWITCHABLE_FILTER_CONTEXTS];
  3679. int internal_active_edge =
  3680. vp9_active_edge_sb(cpi, mi_row, mi_col) && vp9_internal_image_edge(cpi);
  3681. const int *const rd_thresh_freq_fact = tile_data->thresh_freq_fact[bsize];
  3682. x->skip_encode = sf->skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
  3683. memset(x->zcoeff_blk[TX_4X4], 0, 4);
  3684. vp9_zero(best_mbmode);
  3685. for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i) filter_cache[i] = INT64_MAX;
  3686. for (i = 0; i < 4; i++) {
  3687. int j;
  3688. for (j = 0; j < MAX_REF_FRAMES; j++) seg_mvs[i][j].as_int = INVALID_MV;
  3689. }
  3690. estimate_ref_frame_costs(cm, xd, segment_id, ref_costs_single, ref_costs_comp,
  3691. &comp_mode_p);
  3692. for (i = 0; i < REFERENCE_MODES; ++i) best_pred_rd[i] = INT64_MAX;
  3693. for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
  3694. best_filter_rd[i] = INT64_MAX;
  3695. rate_uv_intra = INT_MAX;
  3696. rd_cost->rate = INT_MAX;
  3697. for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
  3698. if (cpi->ref_frame_flags & flag_list[ref_frame]) {
  3699. setup_buffer_inter(cpi, x, ref_frame, bsize, mi_row, mi_col,
  3700. frame_mv[NEARESTMV], frame_mv[NEARMV], yv12_mb);
  3701. } else {
  3702. ref_frame_skip_mask[0] |= (1 << ref_frame);
  3703. ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
  3704. }
  3705. frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
  3706. frame_mv[ZEROMV][ref_frame].as_int = 0;
  3707. }
  3708. for (ref_index = 0; ref_index < MAX_REFS; ++ref_index) {
  3709. int mode_excluded = 0;
  3710. int64_t this_rd = INT64_MAX;
  3711. int disable_skip = 0;
  3712. int compmode_cost = 0;
  3713. int rate2 = 0, rate_y = 0, rate_uv = 0;
  3714. int64_t distortion2 = 0, distortion_y = 0, distortion_uv = 0;
  3715. int skippable = 0;
  3716. int i;
  3717. int this_skip2 = 0;
  3718. int64_t total_sse = INT_MAX;
  3719. int early_term = 0;
  3720. struct buf_2d backup_yv12[2][MAX_MB_PLANE];
  3721. ref_frame = vp9_ref_order[ref_index].ref_frame[0];
  3722. second_ref_frame = vp9_ref_order[ref_index].ref_frame[1];
  3723. vp9_zero(x->sum_y_eobs);
  3724. #if CONFIG_BETTER_HW_COMPATIBILITY
  3725. // forbid 8X4 and 4X8 partitions if any reference frame is scaled.
  3726. if (bsize == BLOCK_8X4 || bsize == BLOCK_4X8) {
  3727. int ref_scaled = ref_frame > INTRA_FRAME &&
  3728. vp9_is_scaled(&cm->frame_refs[ref_frame - 1].sf);
  3729. if (second_ref_frame > INTRA_FRAME)
  3730. ref_scaled += vp9_is_scaled(&cm->frame_refs[second_ref_frame - 1].sf);
  3731. if (ref_scaled) continue;
  3732. }
  3733. #endif
  3734. // Look at the reference frame of the best mode so far and set the
  3735. // skip mask to look at a subset of the remaining modes.
  3736. if (ref_index > 2 && sf->mode_skip_start < MAX_MODES) {
  3737. if (ref_index == 3) {
  3738. switch (best_mbmode.ref_frame[0]) {
  3739. case INTRA_FRAME: break;
  3740. case LAST_FRAME:
  3741. ref_frame_skip_mask[0] |= (1 << GOLDEN_FRAME) | (1 << ALTREF_FRAME);
  3742. ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
  3743. break;
  3744. case GOLDEN_FRAME:
  3745. ref_frame_skip_mask[0] |= (1 << LAST_FRAME) | (1 << ALTREF_FRAME);
  3746. ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
  3747. break;
  3748. case ALTREF_FRAME:
  3749. ref_frame_skip_mask[0] |= (1 << GOLDEN_FRAME) | (1 << LAST_FRAME);
  3750. break;
  3751. case NONE:
  3752. case MAX_REF_FRAMES: assert(0 && "Invalid Reference frame"); break;
  3753. }
  3754. }
  3755. }
  3756. if ((ref_frame_skip_mask[0] & (1 << ref_frame)) &&
  3757. (ref_frame_skip_mask[1] & (1 << VPXMAX(0, second_ref_frame))))
  3758. continue;
  3759. // Test best rd so far against threshold for trying this mode.
  3760. if (!internal_active_edge &&
  3761. rd_less_than_thresh(best_rd,
  3762. rd_opt->threshes[segment_id][bsize][ref_index],
  3763. &rd_thresh_freq_fact[ref_index]))
  3764. continue;
  3765. // This is only used in motion vector unit test.
  3766. if (cpi->oxcf.motion_vector_unit_test && ref_frame == INTRA_FRAME) continue;
  3767. comp_pred = second_ref_frame > INTRA_FRAME;
  3768. if (comp_pred) {
  3769. if (!cpi->allow_comp_inter_inter) continue;
  3770. if (cm->ref_frame_sign_bias[ref_frame] ==
  3771. cm->ref_frame_sign_bias[second_ref_frame])
  3772. continue;
  3773. if (!(cpi->ref_frame_flags & flag_list[second_ref_frame])) continue;
  3774. // Do not allow compound prediction if the segment level reference frame
  3775. // feature is in use as in this case there can only be one reference.
  3776. if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) continue;
  3777. if ((sf->mode_search_skip_flags & FLAG_SKIP_COMP_BESTINTRA) &&
  3778. best_mbmode.ref_frame[0] == INTRA_FRAME)
  3779. continue;
  3780. }
  3781. if (comp_pred)
  3782. mode_excluded = cm->reference_mode == SINGLE_REFERENCE;
  3783. else if (ref_frame != INTRA_FRAME)
  3784. mode_excluded = cm->reference_mode == COMPOUND_REFERENCE;
  3785. // If the segment reference frame feature is enabled....
  3786. // then do nothing if the current ref frame is not allowed..
  3787. if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) &&
  3788. get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame) {
  3789. continue;
  3790. // Disable this drop out case if the ref frame
  3791. // segment level feature is enabled for this segment. This is to
  3792. // prevent the possibility that we end up unable to pick any mode.
  3793. } else if (!segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) {
  3794. // Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
  3795. // unless ARNR filtering is enabled in which case we want
  3796. // an unfiltered alternative. We allow near/nearest as well
  3797. // because they may result in zero-zero MVs but be cheaper.
  3798. if (cpi->rc.is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0))
  3799. continue;
  3800. }
  3801. mi->tx_size = TX_4X4;
  3802. mi->uv_mode = DC_PRED;
  3803. mi->ref_frame[0] = ref_frame;
  3804. mi->ref_frame[1] = second_ref_frame;
  3805. // Evaluate all sub-pel filters irrespective of whether we can use
  3806. // them for this frame.
  3807. mi->interp_filter =
  3808. cm->interp_filter == SWITCHABLE ? EIGHTTAP : cm->interp_filter;
  3809. x->skip = 0;
  3810. set_ref_ptrs(cm, xd, ref_frame, second_ref_frame);
  3811. // Select prediction reference frames.
  3812. for (i = 0; i < MAX_MB_PLANE; i++) {
  3813. xd->plane[i].pre[0] = yv12_mb[ref_frame][i];
  3814. if (comp_pred) xd->plane[i].pre[1] = yv12_mb[second_ref_frame][i];
  3815. }
  3816. if (ref_frame == INTRA_FRAME) {
  3817. int rate;
  3818. if (rd_pick_intra_sub_8x8_y_mode(cpi, x, &rate, &rate_y, &distortion_y,
  3819. best_rd) >= best_rd)
  3820. continue;
  3821. rate2 += rate;
  3822. rate2 += intra_cost_penalty;
  3823. distortion2 += distortion_y;
  3824. if (rate_uv_intra == INT_MAX) {
  3825. choose_intra_uv_mode(cpi, x, ctx, bsize, TX_4X4, &rate_uv_intra,
  3826. &rate_uv_tokenonly, &dist_uv, &skip_uv, &mode_uv);
  3827. }
  3828. rate2 += rate_uv_intra;
  3829. rate_uv = rate_uv_tokenonly;
  3830. distortion2 += dist_uv;
  3831. distortion_uv = dist_uv;
  3832. mi->uv_mode = mode_uv;
  3833. } else {
  3834. int rate;
  3835. int64_t distortion;
  3836. int64_t this_rd_thresh;
  3837. int64_t tmp_rd, tmp_best_rd = INT64_MAX, tmp_best_rdu = INT64_MAX;
  3838. int tmp_best_rate = INT_MAX, tmp_best_ratey = INT_MAX;
  3839. int64_t tmp_best_distortion = INT_MAX, tmp_best_sse, uv_sse;
  3840. int tmp_best_skippable = 0;
  3841. int switchable_filter_index;
  3842. int_mv *second_ref =
  3843. comp_pred ? &x->mbmi_ext->ref_mvs[second_ref_frame][0] : NULL;
  3844. b_mode_info tmp_best_bmodes[16];
  3845. MODE_INFO tmp_best_mbmode;
  3846. BEST_SEG_INFO bsi[SWITCHABLE_FILTERS];
  3847. int pred_exists = 0;
  3848. int uv_skippable;
  3849. YV12_BUFFER_CONFIG *scaled_ref_frame[2] = { NULL, NULL };
  3850. int ref;
  3851. for (ref = 0; ref < 2; ++ref) {
  3852. scaled_ref_frame[ref] =
  3853. mi->ref_frame[ref] > INTRA_FRAME
  3854. ? vp9_get_scaled_ref_frame(cpi, mi->ref_frame[ref])
  3855. : NULL;
  3856. if (scaled_ref_frame[ref]) {
  3857. int i;
  3858. // Swap out the reference frame for a version that's been scaled to
  3859. // match the resolution of the current frame, allowing the existing
  3860. // motion search code to be used without additional modifications.
  3861. for (i = 0; i < MAX_MB_PLANE; i++)
  3862. backup_yv12[ref][i] = xd->plane[i].pre[ref];
  3863. vp9_setup_pre_planes(xd, ref, scaled_ref_frame[ref], mi_row, mi_col,
  3864. NULL);
  3865. }
  3866. }
  3867. this_rd_thresh = (ref_frame == LAST_FRAME)
  3868. ? rd_opt->threshes[segment_id][bsize][THR_LAST]
  3869. : rd_opt->threshes[segment_id][bsize][THR_ALTR];
  3870. this_rd_thresh = (ref_frame == GOLDEN_FRAME)
  3871. ? rd_opt->threshes[segment_id][bsize][THR_GOLD]
  3872. : this_rd_thresh;
  3873. for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
  3874. filter_cache[i] = INT64_MAX;
  3875. if (cm->interp_filter != BILINEAR) {
  3876. tmp_best_filter = EIGHTTAP;
  3877. if (x->source_variance < sf->disable_filter_search_var_thresh) {
  3878. tmp_best_filter = EIGHTTAP;
  3879. } else if (sf->adaptive_pred_interp_filter == 1 &&
  3880. ctx->pred_interp_filter < SWITCHABLE) {
  3881. tmp_best_filter = ctx->pred_interp_filter;
  3882. } else if (sf->adaptive_pred_interp_filter == 2) {
  3883. tmp_best_filter = ctx->pred_interp_filter < SWITCHABLE
  3884. ? ctx->pred_interp_filter
  3885. : 0;
  3886. } else {
  3887. for (switchable_filter_index = 0;
  3888. switchable_filter_index < SWITCHABLE_FILTERS;
  3889. ++switchable_filter_index) {
  3890. int newbest, rs;
  3891. int64_t rs_rd;
  3892. MB_MODE_INFO_EXT *mbmi_ext = x->mbmi_ext;
  3893. mi->interp_filter = switchable_filter_index;
  3894. tmp_rd = rd_pick_best_sub8x8_mode(
  3895. cpi, x, &mbmi_ext->ref_mvs[ref_frame][0], second_ref, best_yrd,
  3896. &rate, &rate_y, &distortion, &skippable, &total_sse,
  3897. (int)this_rd_thresh, seg_mvs, bsi, switchable_filter_index,
  3898. mi_row, mi_col);
  3899. if (tmp_rd == INT64_MAX) continue;
  3900. rs = vp9_get_switchable_rate(cpi, xd);
  3901. rs_rd = RDCOST(x->rdmult, x->rddiv, rs, 0);
  3902. filter_cache[switchable_filter_index] = tmp_rd;
  3903. filter_cache[SWITCHABLE_FILTERS] =
  3904. VPXMIN(filter_cache[SWITCHABLE_FILTERS], tmp_rd + rs_rd);
  3905. if (cm->interp_filter == SWITCHABLE) tmp_rd += rs_rd;
  3906. mask_filter = VPXMAX(mask_filter, tmp_rd);
  3907. newbest = (tmp_rd < tmp_best_rd);
  3908. if (newbest) {
  3909. tmp_best_filter = mi->interp_filter;
  3910. tmp_best_rd = tmp_rd;
  3911. }
  3912. if ((newbest && cm->interp_filter == SWITCHABLE) ||
  3913. (mi->interp_filter == cm->interp_filter &&
  3914. cm->interp_filter != SWITCHABLE)) {
  3915. tmp_best_rdu = tmp_rd;
  3916. tmp_best_rate = rate;
  3917. tmp_best_ratey = rate_y;
  3918. tmp_best_distortion = distortion;
  3919. tmp_best_sse = total_sse;
  3920. tmp_best_skippable = skippable;
  3921. tmp_best_mbmode = *mi;
  3922. for (i = 0; i < 4; i++) {
  3923. tmp_best_bmodes[i] = xd->mi[0]->bmi[i];
  3924. x->zcoeff_blk[TX_4X4][i] = !x->plane[0].eobs[i];
  3925. x->sum_y_eobs[TX_4X4] += x->plane[0].eobs[i];
  3926. }
  3927. pred_exists = 1;
  3928. if (switchable_filter_index == 0 && sf->use_rd_breakout &&
  3929. best_rd < INT64_MAX) {
  3930. if (tmp_best_rdu / 2 > best_rd) {
  3931. // skip searching the other filters if the first is
  3932. // already substantially larger than the best so far
  3933. tmp_best_filter = mi->interp_filter;
  3934. tmp_best_rdu = INT64_MAX;
  3935. break;
  3936. }
  3937. }
  3938. }
  3939. } // switchable_filter_index loop
  3940. }
  3941. }
  3942. if (tmp_best_rdu == INT64_MAX && pred_exists) continue;
  3943. mi->interp_filter = (cm->interp_filter == SWITCHABLE ? tmp_best_filter
  3944. : cm->interp_filter);
  3945. if (!pred_exists) {
  3946. // Handles the special case when a filter that is not in the
  3947. // switchable list (bilinear, 6-tap) is indicated at the frame level
  3948. tmp_rd = rd_pick_best_sub8x8_mode(
  3949. cpi, x, &x->mbmi_ext->ref_mvs[ref_frame][0], second_ref, best_yrd,
  3950. &rate, &rate_y, &distortion, &skippable, &total_sse,
  3951. (int)this_rd_thresh, seg_mvs, bsi, 0, mi_row, mi_col);
  3952. if (tmp_rd == INT64_MAX) continue;
  3953. } else {
  3954. total_sse = tmp_best_sse;
  3955. rate = tmp_best_rate;
  3956. rate_y = tmp_best_ratey;
  3957. distortion = tmp_best_distortion;
  3958. skippable = tmp_best_skippable;
  3959. *mi = tmp_best_mbmode;
  3960. for (i = 0; i < 4; i++) xd->mi[0]->bmi[i] = tmp_best_bmodes[i];
  3961. }
  3962. rate2 += rate;
  3963. distortion2 += distortion;
  3964. if (cm->interp_filter == SWITCHABLE)
  3965. rate2 += vp9_get_switchable_rate(cpi, xd);
  3966. if (!mode_excluded)
  3967. mode_excluded = comp_pred ? cm->reference_mode == SINGLE_REFERENCE
  3968. : cm->reference_mode == COMPOUND_REFERENCE;
  3969. compmode_cost = vp9_cost_bit(comp_mode_p, comp_pred);
  3970. tmp_best_rdu =
  3971. best_rd - VPXMIN(RDCOST(x->rdmult, x->rddiv, rate2, distortion2),
  3972. RDCOST(x->rdmult, x->rddiv, 0, total_sse));
  3973. if (tmp_best_rdu > 0) {
  3974. // If even the 'Y' rd value of split is higher than best so far
  3975. // then dont bother looking at UV
  3976. vp9_build_inter_predictors_sbuv(&x->e_mbd, mi_row, mi_col, BLOCK_8X8);
  3977. memset(x->skip_txfm, SKIP_TXFM_NONE, sizeof(x->skip_txfm));
  3978. if (!super_block_uvrd(cpi, x, &rate_uv, &distortion_uv, &uv_skippable,
  3979. &uv_sse, BLOCK_8X8, tmp_best_rdu)) {
  3980. for (ref = 0; ref < 2; ++ref) {
  3981. if (scaled_ref_frame[ref]) {
  3982. int i;
  3983. for (i = 0; i < MAX_MB_PLANE; ++i)
  3984. xd->plane[i].pre[ref] = backup_yv12[ref][i];
  3985. }
  3986. }
  3987. continue;
  3988. }
  3989. rate2 += rate_uv;
  3990. distortion2 += distortion_uv;
  3991. skippable = skippable && uv_skippable;
  3992. total_sse += uv_sse;
  3993. }
  3994. for (ref = 0; ref < 2; ++ref) {
  3995. if (scaled_ref_frame[ref]) {
  3996. // Restore the prediction frame pointers to their unscaled versions.
  3997. int i;
  3998. for (i = 0; i < MAX_MB_PLANE; ++i)
  3999. xd->plane[i].pre[ref] = backup_yv12[ref][i];
  4000. }
  4001. }
  4002. }
  4003. if (cm->reference_mode == REFERENCE_MODE_SELECT) rate2 += compmode_cost;
  4004. // Estimate the reference frame signaling cost and add it
  4005. // to the rolling cost variable.
  4006. if (second_ref_frame > INTRA_FRAME) {
  4007. rate2 += ref_costs_comp[ref_frame];
  4008. } else {
  4009. rate2 += ref_costs_single[ref_frame];
  4010. }
  4011. if (!disable_skip) {
  4012. const vpx_prob skip_prob = vp9_get_skip_prob(cm, xd);
  4013. const int skip_cost0 = vp9_cost_bit(skip_prob, 0);
  4014. const int skip_cost1 = vp9_cost_bit(skip_prob, 1);
  4015. // Skip is never coded at the segment level for sub8x8 blocks and instead
  4016. // always coded in the bitstream at the mode info level.
  4017. if (ref_frame != INTRA_FRAME && !xd->lossless) {
  4018. if (RDCOST(x->rdmult, x->rddiv, rate_y + rate_uv + skip_cost0,
  4019. distortion2) <
  4020. RDCOST(x->rdmult, x->rddiv, skip_cost1, total_sse)) {
  4021. // Add in the cost of the no skip flag.
  4022. rate2 += skip_cost0;
  4023. } else {
  4024. // FIXME(rbultje) make this work for splitmv also
  4025. rate2 += skip_cost1;
  4026. distortion2 = total_sse;
  4027. assert(total_sse >= 0);
  4028. rate2 -= (rate_y + rate_uv);
  4029. rate_y = 0;
  4030. rate_uv = 0;
  4031. this_skip2 = 1;
  4032. }
  4033. } else {
  4034. // Add in the cost of the no skip flag.
  4035. rate2 += skip_cost0;
  4036. }
  4037. // Calculate the final RD estimate for this mode.
  4038. this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
  4039. }
  4040. if (!disable_skip && ref_frame == INTRA_FRAME) {
  4041. for (i = 0; i < REFERENCE_MODES; ++i)
  4042. best_pred_rd[i] = VPXMIN(best_pred_rd[i], this_rd);
  4043. for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
  4044. best_filter_rd[i] = VPXMIN(best_filter_rd[i], this_rd);
  4045. }
  4046. // Did this mode help.. i.e. is it the new best mode
  4047. if (this_rd < best_rd || x->skip) {
  4048. if (!mode_excluded) {
  4049. int max_plane = MAX_MB_PLANE;
  4050. // Note index of best mode so far
  4051. best_ref_index = ref_index;
  4052. if (ref_frame == INTRA_FRAME) {
  4053. /* required for left and above block mv */
  4054. mi->mv[0].as_int = 0;
  4055. max_plane = 1;
  4056. // Initialize interp_filter here so we do not have to check for
  4057. // inter block modes in get_pred_context_switchable_interp()
  4058. mi->interp_filter = SWITCHABLE_FILTERS;
  4059. }
  4060. rd_cost->rate = rate2;
  4061. rd_cost->dist = distortion2;
  4062. rd_cost->rdcost = this_rd;
  4063. best_rd = this_rd;
  4064. best_yrd =
  4065. best_rd - RDCOST(x->rdmult, x->rddiv, rate_uv, distortion_uv);
  4066. best_mbmode = *mi;
  4067. best_skip2 = this_skip2;
  4068. if (!x->select_tx_size) swap_block_ptr(x, ctx, 1, 0, 0, max_plane);
  4069. memcpy(ctx->zcoeff_blk, x->zcoeff_blk[TX_4X4],
  4070. sizeof(ctx->zcoeff_blk[0]) * ctx->num_4x4_blk);
  4071. ctx->sum_y_eobs = x->sum_y_eobs[TX_4X4];
  4072. for (i = 0; i < 4; i++) best_bmodes[i] = xd->mi[0]->bmi[i];
  4073. // TODO(debargha): enhance this test with a better distortion prediction
  4074. // based on qp, activity mask and history
  4075. if ((sf->mode_search_skip_flags & FLAG_EARLY_TERMINATE) &&
  4076. (ref_index > MIN_EARLY_TERM_INDEX)) {
  4077. int qstep = xd->plane[0].dequant[1];
  4078. // TODO(debargha): Enhance this by specializing for each mode_index
  4079. int scale = 4;
  4080. #if CONFIG_VP9_HIGHBITDEPTH
  4081. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  4082. qstep >>= (xd->bd - 8);
  4083. }
  4084. #endif // CONFIG_VP9_HIGHBITDEPTH
  4085. if (x->source_variance < UINT_MAX) {
  4086. const int var_adjust = (x->source_variance < 16);
  4087. scale -= var_adjust;
  4088. }
  4089. if (ref_frame > INTRA_FRAME && distortion2 * scale < qstep * qstep) {
  4090. early_term = 1;
  4091. }
  4092. }
  4093. }
  4094. }
  4095. /* keep record of best compound/single-only prediction */
  4096. if (!disable_skip && ref_frame != INTRA_FRAME) {
  4097. int64_t single_rd, hybrid_rd, single_rate, hybrid_rate;
  4098. if (cm->reference_mode == REFERENCE_MODE_SELECT) {
  4099. single_rate = rate2 - compmode_cost;
  4100. hybrid_rate = rate2;
  4101. } else {
  4102. single_rate = rate2;
  4103. hybrid_rate = rate2 + compmode_cost;
  4104. }
  4105. single_rd = RDCOST(x->rdmult, x->rddiv, single_rate, distortion2);
  4106. hybrid_rd = RDCOST(x->rdmult, x->rddiv, hybrid_rate, distortion2);
  4107. if (!comp_pred && single_rd < best_pred_rd[SINGLE_REFERENCE])
  4108. best_pred_rd[SINGLE_REFERENCE] = single_rd;
  4109. else if (comp_pred && single_rd < best_pred_rd[COMPOUND_REFERENCE])
  4110. best_pred_rd[COMPOUND_REFERENCE] = single_rd;
  4111. if (hybrid_rd < best_pred_rd[REFERENCE_MODE_SELECT])
  4112. best_pred_rd[REFERENCE_MODE_SELECT] = hybrid_rd;
  4113. }
  4114. /* keep record of best filter type */
  4115. if (!mode_excluded && !disable_skip && ref_frame != INTRA_FRAME &&
  4116. cm->interp_filter != BILINEAR) {
  4117. int64_t ref =
  4118. filter_cache[cm->interp_filter == SWITCHABLE ? SWITCHABLE_FILTERS
  4119. : cm->interp_filter];
  4120. int64_t adj_rd;
  4121. for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) {
  4122. if (ref == INT64_MAX)
  4123. adj_rd = 0;
  4124. else if (filter_cache[i] == INT64_MAX)
  4125. // when early termination is triggered, the encoder does not have
  4126. // access to the rate-distortion cost. it only knows that the cost
  4127. // should be above the maximum valid value. hence it takes the known
  4128. // maximum plus an arbitrary constant as the rate-distortion cost.
  4129. adj_rd = mask_filter - ref + 10;
  4130. else
  4131. adj_rd = filter_cache[i] - ref;
  4132. adj_rd += this_rd;
  4133. best_filter_rd[i] = VPXMIN(best_filter_rd[i], adj_rd);
  4134. }
  4135. }
  4136. if (early_term) break;
  4137. if (x->skip && !comp_pred) break;
  4138. }
  4139. if (best_rd >= best_rd_so_far) {
  4140. rd_cost->rate = INT_MAX;
  4141. rd_cost->rdcost = INT64_MAX;
  4142. return;
  4143. }
  4144. // If we used an estimate for the uv intra rd in the loop above...
  4145. if (sf->use_uv_intra_rd_estimate) {
  4146. // Do Intra UV best rd mode selection if best mode choice above was intra.
  4147. if (best_mbmode.ref_frame[0] == INTRA_FRAME) {
  4148. *mi = best_mbmode;
  4149. rd_pick_intra_sbuv_mode(cpi, x, ctx, &rate_uv_intra, &rate_uv_tokenonly,
  4150. &dist_uv, &skip_uv, BLOCK_8X8, TX_4X4);
  4151. }
  4152. }
  4153. if (best_rd == INT64_MAX) {
  4154. rd_cost->rate = INT_MAX;
  4155. rd_cost->dist = INT64_MAX;
  4156. rd_cost->rdcost = INT64_MAX;
  4157. return;
  4158. }
  4159. assert((cm->interp_filter == SWITCHABLE) ||
  4160. (cm->interp_filter == best_mbmode.interp_filter) ||
  4161. !is_inter_block(&best_mbmode));
  4162. vp9_update_rd_thresh_fact(tile_data->thresh_freq_fact, sf->adaptive_rd_thresh,
  4163. bsize, best_ref_index);
  4164. // macroblock modes
  4165. *mi = best_mbmode;
  4166. x->skip |= best_skip2;
  4167. if (!is_inter_block(&best_mbmode)) {
  4168. for (i = 0; i < 4; i++) xd->mi[0]->bmi[i].as_mode = best_bmodes[i].as_mode;
  4169. } else {
  4170. for (i = 0; i < 4; ++i)
  4171. memcpy(&xd->mi[0]->bmi[i], &best_bmodes[i], sizeof(b_mode_info));
  4172. mi->mv[0].as_int = xd->mi[0]->bmi[3].as_mv[0].as_int;
  4173. mi->mv[1].as_int = xd->mi[0]->bmi[3].as_mv[1].as_int;
  4174. }
  4175. for (i = 0; i < REFERENCE_MODES; ++i) {
  4176. if (best_pred_rd[i] == INT64_MAX)
  4177. best_pred_diff[i] = INT_MIN;
  4178. else
  4179. best_pred_diff[i] = best_rd - best_pred_rd[i];
  4180. }
  4181. if (!x->skip) {
  4182. for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) {
  4183. if (best_filter_rd[i] == INT64_MAX)
  4184. best_filter_diff[i] = 0;
  4185. else
  4186. best_filter_diff[i] = best_rd - best_filter_rd[i];
  4187. }
  4188. if (cm->interp_filter == SWITCHABLE)
  4189. assert(best_filter_diff[SWITCHABLE_FILTERS] == 0);
  4190. } else {
  4191. vp9_zero(best_filter_diff);
  4192. }
  4193. store_coding_context(x, ctx, best_ref_index, best_pred_diff, best_filter_diff,
  4194. 0);
  4195. }
  4196. #endif // !CONFIG_REALTIME_ONLY