2
0

vp9_rdopt.c 163 KB

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