vp9_rdopt.c 157 KB

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