vp9_pickmode.c 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975
  1. /*
  2. * Copyright (c) 2014 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 <limits.h>
  12. #include <math.h>
  13. #include <stdio.h>
  14. #include "./vp9_rtcd.h"
  15. #include "./vpx_dsp_rtcd.h"
  16. #include "vpx/vpx_codec.h"
  17. #include "vpx_dsp/vpx_dsp_common.h"
  18. #include "vpx_mem/vpx_mem.h"
  19. #include "vpx_ports/mem.h"
  20. #include "vp9/common/vp9_blockd.h"
  21. #include "vp9/common/vp9_common.h"
  22. #include "vp9/common/vp9_mvref_common.h"
  23. #include "vp9/common/vp9_pred_common.h"
  24. #include "vp9/common/vp9_reconinter.h"
  25. #include "vp9/common/vp9_reconintra.h"
  26. #include "vp9/common/vp9_scan.h"
  27. #include "vp9/encoder/vp9_cost.h"
  28. #include "vp9/encoder/vp9_encoder.h"
  29. #include "vp9/encoder/vp9_pickmode.h"
  30. #include "vp9/encoder/vp9_ratectrl.h"
  31. #include "vp9/encoder/vp9_rd.h"
  32. typedef struct {
  33. uint8_t *data;
  34. int stride;
  35. int in_use;
  36. } PRED_BUFFER;
  37. typedef struct {
  38. PRED_BUFFER *best_pred;
  39. PREDICTION_MODE best_mode;
  40. TX_SIZE best_tx_size;
  41. TX_SIZE best_intra_tx_size;
  42. MV_REFERENCE_FRAME best_ref_frame;
  43. MV_REFERENCE_FRAME best_second_ref_frame;
  44. uint8_t best_mode_skip_txfm;
  45. INTERP_FILTER best_pred_filter;
  46. } BEST_PICKMODE;
  47. static const int pos_shift_16x16[4][4] = {
  48. { 9, 10, 13, 14 }, { 11, 12, 15, 16 }, { 17, 18, 21, 22 }, { 19, 20, 23, 24 }
  49. };
  50. static int mv_refs_rt(VP9_COMP *cpi, const VP9_COMMON *cm, const MACROBLOCK *x,
  51. const MACROBLOCKD *xd, const TileInfo *const tile,
  52. MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
  53. int_mv *mv_ref_list, int_mv *base_mv, int mi_row,
  54. int mi_col, int use_base_mv) {
  55. const int *ref_sign_bias = cm->ref_frame_sign_bias;
  56. int i, refmv_count = 0;
  57. const POSITION *const mv_ref_search = mv_ref_blocks[mi->sb_type];
  58. int different_ref_found = 0;
  59. int context_counter = 0;
  60. int const_motion = 0;
  61. // Blank the reference vector list
  62. memset(mv_ref_list, 0, sizeof(*mv_ref_list) * MAX_MV_REF_CANDIDATES);
  63. // The nearest 2 blocks are treated differently
  64. // if the size < 8x8 we get the mv from the bmi substructure,
  65. // and we also need to keep a mode count.
  66. for (i = 0; i < 2; ++i) {
  67. const POSITION *const mv_ref = &mv_ref_search[i];
  68. if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
  69. const MODE_INFO *const candidate_mi =
  70. xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
  71. // Keep counts for entropy encoding.
  72. context_counter += mode_2_counter[candidate_mi->mode];
  73. different_ref_found = 1;
  74. if (candidate_mi->ref_frame[0] == ref_frame)
  75. ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 0, mv_ref->col, -1),
  76. refmv_count, mv_ref_list, Done);
  77. }
  78. }
  79. const_motion = 1;
  80. // Check the rest of the neighbors in much the same way
  81. // as before except we don't need to keep track of sub blocks or
  82. // mode counts.
  83. for (; i < MVREF_NEIGHBOURS && !refmv_count; ++i) {
  84. const POSITION *const mv_ref = &mv_ref_search[i];
  85. if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
  86. const MODE_INFO *const candidate_mi =
  87. xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
  88. different_ref_found = 1;
  89. if (candidate_mi->ref_frame[0] == ref_frame)
  90. ADD_MV_REF_LIST(candidate_mi->mv[0], refmv_count, mv_ref_list, Done);
  91. }
  92. }
  93. // Since we couldn't find 2 mvs from the same reference frame
  94. // go back through the neighbors and find motion vectors from
  95. // different reference frames.
  96. if (different_ref_found && !refmv_count) {
  97. for (i = 0; i < MVREF_NEIGHBOURS; ++i) {
  98. const POSITION *mv_ref = &mv_ref_search[i];
  99. if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
  100. const MODE_INFO *const candidate_mi =
  101. xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
  102. // If the candidate is INTRA we don't want to consider its mv.
  103. IF_DIFF_REF_FRAME_ADD_MV(candidate_mi, ref_frame, ref_sign_bias,
  104. refmv_count, mv_ref_list, Done);
  105. }
  106. }
  107. }
  108. if (use_base_mv &&
  109. !cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame &&
  110. ref_frame == LAST_FRAME) {
  111. // Get base layer mv.
  112. MV_REF *candidate =
  113. &cm->prev_frame
  114. ->mvs[(mi_col >> 1) + (mi_row >> 1) * (cm->mi_cols >> 1)];
  115. if (candidate->mv[0].as_int != INVALID_MV) {
  116. base_mv->as_mv.row = (candidate->mv[0].as_mv.row * 2);
  117. base_mv->as_mv.col = (candidate->mv[0].as_mv.col * 2);
  118. clamp_mv_ref(&base_mv->as_mv, xd);
  119. } else {
  120. base_mv->as_int = INVALID_MV;
  121. }
  122. }
  123. Done:
  124. x->mbmi_ext->mode_context[ref_frame] = counter_to_context[context_counter];
  125. // Clamp vectors
  126. for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i)
  127. clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
  128. return const_motion;
  129. }
  130. static int combined_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
  131. BLOCK_SIZE bsize, int mi_row, int mi_col,
  132. int_mv *tmp_mv, int *rate_mv,
  133. int64_t best_rd_sofar, int use_base_mv) {
  134. MACROBLOCKD *xd = &x->e_mbd;
  135. MODE_INFO *mi = xd->mi[0];
  136. struct buf_2d backup_yv12[MAX_MB_PLANE] = { { 0, 0 } };
  137. const int step_param = cpi->sf.mv.fullpel_search_step_param;
  138. const int sadpb = x->sadperbit16;
  139. MV mvp_full;
  140. const int ref = mi->ref_frame[0];
  141. const MV ref_mv = x->mbmi_ext->ref_mvs[ref][0].as_mv;
  142. MV center_mv;
  143. uint32_t dis;
  144. int rate_mode;
  145. const MvLimits tmp_mv_limits = x->mv_limits;
  146. int rv = 0;
  147. int cost_list[5];
  148. int search_subpel = 1;
  149. const YV12_BUFFER_CONFIG *scaled_ref_frame =
  150. vp9_get_scaled_ref_frame(cpi, ref);
  151. if (scaled_ref_frame) {
  152. int i;
  153. // Swap out the reference frame for a version that's been scaled to
  154. // match the resolution of the current frame, allowing the existing
  155. // motion search code to be used without additional modifications.
  156. for (i = 0; i < MAX_MB_PLANE; i++) backup_yv12[i] = xd->plane[i].pre[0];
  157. vp9_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL);
  158. }
  159. vp9_set_mv_search_range(&x->mv_limits, &ref_mv);
  160. // Limit motion vector for large lightning change.
  161. if (cpi->oxcf.speed > 5 && x->lowvar_highsumdiff) {
  162. x->mv_limits.col_min = VPXMAX(x->mv_limits.col_min, -10);
  163. x->mv_limits.row_min = VPXMAX(x->mv_limits.row_min, -10);
  164. x->mv_limits.col_max = VPXMIN(x->mv_limits.col_max, 10);
  165. x->mv_limits.row_max = VPXMIN(x->mv_limits.row_max, 10);
  166. }
  167. assert(x->mv_best_ref_index[ref] <= 2);
  168. if (x->mv_best_ref_index[ref] < 2)
  169. mvp_full = x->mbmi_ext->ref_mvs[ref][x->mv_best_ref_index[ref]].as_mv;
  170. else
  171. mvp_full = x->pred_mv[ref];
  172. mvp_full.col >>= 3;
  173. mvp_full.row >>= 3;
  174. if (!use_base_mv)
  175. center_mv = ref_mv;
  176. else
  177. center_mv = tmp_mv->as_mv;
  178. if (x->sb_use_mv_part) {
  179. tmp_mv->as_mv.row = x->sb_mvrow_part >> 3;
  180. tmp_mv->as_mv.col = x->sb_mvcol_part >> 3;
  181. } else {
  182. vp9_full_pixel_search(
  183. cpi, x, bsize, &mvp_full, step_param, cpi->sf.mv.search_method, sadpb,
  184. cond_cost_list(cpi, cost_list), &center_mv, &tmp_mv->as_mv, INT_MAX, 0);
  185. }
  186. x->mv_limits = tmp_mv_limits;
  187. // calculate the bit cost on motion vector
  188. mvp_full.row = tmp_mv->as_mv.row * 8;
  189. mvp_full.col = tmp_mv->as_mv.col * 8;
  190. *rate_mv = vp9_mv_bit_cost(&mvp_full, &ref_mv, x->nmvjointcost, x->mvcost,
  191. MV_COST_WEIGHT);
  192. rate_mode =
  193. cpi->inter_mode_cost[x->mbmi_ext->mode_context[ref]][INTER_OFFSET(NEWMV)];
  194. rv =
  195. !(RDCOST(x->rdmult, x->rddiv, (*rate_mv + rate_mode), 0) > best_rd_sofar);
  196. // For SVC on non-reference frame, avoid subpel for (0, 0) motion.
  197. if (cpi->use_svc && cpi->svc.non_reference_frame) {
  198. if (mvp_full.row == 0 && mvp_full.col == 0) search_subpel = 0;
  199. }
  200. if (rv && search_subpel) {
  201. SUBPEL_FORCE_STOP subpel_force_stop = cpi->sf.mv.subpel_force_stop;
  202. if (use_base_mv && cpi->sf.base_mv_aggressive) subpel_force_stop = HALF_PEL;
  203. if (cpi->sf.mv.enable_adaptive_subpel_force_stop) {
  204. const int mv_thresh = cpi->sf.mv.adapt_subpel_force_stop.mv_thresh;
  205. if (abs(tmp_mv->as_mv.row) >= mv_thresh ||
  206. abs(tmp_mv->as_mv.col) >= mv_thresh)
  207. subpel_force_stop = cpi->sf.mv.adapt_subpel_force_stop.force_stop_above;
  208. else
  209. subpel_force_stop = cpi->sf.mv.adapt_subpel_force_stop.force_stop_below;
  210. }
  211. cpi->find_fractional_mv_step(
  212. x, &tmp_mv->as_mv, &ref_mv, cpi->common.allow_high_precision_mv,
  213. x->errorperbit, &cpi->fn_ptr[bsize], subpel_force_stop,
  214. cpi->sf.mv.subpel_search_level, cond_cost_list(cpi, cost_list),
  215. x->nmvjointcost, x->mvcost, &dis, &x->pred_sse[ref], NULL, 0, 0,
  216. cpi->sf.use_accurate_subpel_search);
  217. *rate_mv = vp9_mv_bit_cost(&tmp_mv->as_mv, &ref_mv, x->nmvjointcost,
  218. x->mvcost, MV_COST_WEIGHT);
  219. }
  220. if (scaled_ref_frame) {
  221. int i;
  222. for (i = 0; i < MAX_MB_PLANE; i++) xd->plane[i].pre[0] = backup_yv12[i];
  223. }
  224. return rv;
  225. }
  226. static void block_variance(const uint8_t *src, int src_stride,
  227. const uint8_t *ref, int ref_stride, int w, int h,
  228. unsigned int *sse, int *sum, int block_size,
  229. #if CONFIG_VP9_HIGHBITDEPTH
  230. int use_highbitdepth, vpx_bit_depth_t bd,
  231. #endif
  232. uint32_t *sse8x8, int *sum8x8, uint32_t *var8x8) {
  233. int i, j, k = 0;
  234. *sse = 0;
  235. *sum = 0;
  236. for (i = 0; i < h; i += block_size) {
  237. for (j = 0; j < w; j += block_size) {
  238. #if CONFIG_VP9_HIGHBITDEPTH
  239. if (use_highbitdepth) {
  240. switch (bd) {
  241. case VPX_BITS_8:
  242. vpx_highbd_8_get8x8var(src + src_stride * i + j, src_stride,
  243. ref + ref_stride * i + j, ref_stride,
  244. &sse8x8[k], &sum8x8[k]);
  245. break;
  246. case VPX_BITS_10:
  247. vpx_highbd_10_get8x8var(src + src_stride * i + j, src_stride,
  248. ref + ref_stride * i + j, ref_stride,
  249. &sse8x8[k], &sum8x8[k]);
  250. break;
  251. case VPX_BITS_12:
  252. vpx_highbd_12_get8x8var(src + src_stride * i + j, src_stride,
  253. ref + ref_stride * i + j, ref_stride,
  254. &sse8x8[k], &sum8x8[k]);
  255. break;
  256. }
  257. } else {
  258. vpx_get8x8var(src + src_stride * i + j, src_stride,
  259. ref + ref_stride * i + j, ref_stride, &sse8x8[k],
  260. &sum8x8[k]);
  261. }
  262. #else
  263. vpx_get8x8var(src + src_stride * i + j, src_stride,
  264. ref + ref_stride * i + j, ref_stride, &sse8x8[k],
  265. &sum8x8[k]);
  266. #endif
  267. *sse += sse8x8[k];
  268. *sum += sum8x8[k];
  269. var8x8[k] = sse8x8[k] - (uint32_t)(((int64_t)sum8x8[k] * sum8x8[k]) >> 6);
  270. k++;
  271. }
  272. }
  273. }
  274. static void calculate_variance(int bw, int bh, TX_SIZE tx_size,
  275. unsigned int *sse_i, int *sum_i,
  276. unsigned int *var_o, unsigned int *sse_o,
  277. int *sum_o) {
  278. const BLOCK_SIZE unit_size = txsize_to_bsize[tx_size];
  279. const int nw = 1 << (bw - b_width_log2_lookup[unit_size]);
  280. const int nh = 1 << (bh - b_height_log2_lookup[unit_size]);
  281. int i, j, k = 0;
  282. for (i = 0; i < nh; i += 2) {
  283. for (j = 0; j < nw; j += 2) {
  284. sse_o[k] = sse_i[i * nw + j] + sse_i[i * nw + j + 1] +
  285. sse_i[(i + 1) * nw + j] + sse_i[(i + 1) * nw + j + 1];
  286. sum_o[k] = sum_i[i * nw + j] + sum_i[i * nw + j + 1] +
  287. sum_i[(i + 1) * nw + j] + sum_i[(i + 1) * nw + j + 1];
  288. var_o[k] = sse_o[k] - (uint32_t)(((int64_t)sum_o[k] * sum_o[k]) >>
  289. (b_width_log2_lookup[unit_size] +
  290. b_height_log2_lookup[unit_size] + 6));
  291. k++;
  292. }
  293. }
  294. }
  295. // Adjust the ac_thr according to speed, width, height and normalized sum
  296. static int ac_thr_factor(const int speed, const int width, const int height,
  297. const int norm_sum) {
  298. if (speed >= 8 && norm_sum < 5) {
  299. if (width <= 640 && height <= 480)
  300. return 4;
  301. else
  302. return 2;
  303. }
  304. return 1;
  305. }
  306. static TX_SIZE calculate_tx_size(VP9_COMP *const cpi, BLOCK_SIZE bsize,
  307. MACROBLOCKD *const xd, unsigned int var,
  308. unsigned int sse, int64_t ac_thr,
  309. unsigned int source_variance, int is_intra) {
  310. // TODO(marpan): Tune selection for intra-modes, screen content, etc.
  311. TX_SIZE tx_size;
  312. unsigned int var_thresh = is_intra ? (unsigned int)ac_thr : 1;
  313. int limit_tx = 1;
  314. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
  315. (source_variance == 0 || var < var_thresh))
  316. limit_tx = 0;
  317. if (cpi->common.tx_mode == TX_MODE_SELECT) {
  318. if (sse > (var << 2))
  319. tx_size = VPXMIN(max_txsize_lookup[bsize],
  320. tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
  321. else
  322. tx_size = TX_8X8;
  323. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && limit_tx &&
  324. cyclic_refresh_segment_id_boosted(xd->mi[0]->segment_id))
  325. tx_size = TX_8X8;
  326. else if (tx_size > TX_16X16 && limit_tx)
  327. tx_size = TX_16X16;
  328. // For screen-content force 4X4 tx_size over 8X8, for large variance.
  329. if (cpi->oxcf.content == VP9E_CONTENT_SCREEN && tx_size == TX_8X8 &&
  330. bsize <= BLOCK_16X16 && ((var >> 5) > (unsigned int)ac_thr))
  331. tx_size = TX_4X4;
  332. } else {
  333. tx_size = VPXMIN(max_txsize_lookup[bsize],
  334. tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
  335. }
  336. return tx_size;
  337. }
  338. static void compute_intra_yprediction(PREDICTION_MODE mode, BLOCK_SIZE bsize,
  339. MACROBLOCK *x, MACROBLOCKD *xd) {
  340. struct macroblockd_plane *const pd = &xd->plane[0];
  341. struct macroblock_plane *const p = &x->plane[0];
  342. uint8_t *const src_buf_base = p->src.buf;
  343. uint8_t *const dst_buf_base = pd->dst.buf;
  344. const int src_stride = p->src.stride;
  345. const int dst_stride = pd->dst.stride;
  346. // block and transform sizes, in number of 4x4 blocks log 2 ("*_b")
  347. // 4x4=0, 8x8=2, 16x16=4, 32x32=6, 64x64=8
  348. const TX_SIZE tx_size = max_txsize_lookup[bsize];
  349. const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
  350. const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
  351. int row, col;
  352. // If mb_to_right_edge is < 0 we are in a situation in which
  353. // the current block size extends into the UMV and we won't
  354. // visit the sub blocks that are wholly within the UMV.
  355. const int max_blocks_wide =
  356. num_4x4_w + (xd->mb_to_right_edge >= 0
  357. ? 0
  358. : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
  359. const int max_blocks_high =
  360. num_4x4_h + (xd->mb_to_bottom_edge >= 0
  361. ? 0
  362. : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
  363. // Keep track of the row and column of the blocks we use so that we know
  364. // if we are in the unrestricted motion border.
  365. for (row = 0; row < max_blocks_high; row += (1 << tx_size)) {
  366. // Skip visiting the sub blocks that are wholly within the UMV.
  367. for (col = 0; col < max_blocks_wide; col += (1 << tx_size)) {
  368. p->src.buf = &src_buf_base[4 * (row * (int64_t)src_stride + col)];
  369. pd->dst.buf = &dst_buf_base[4 * (row * (int64_t)dst_stride + col)];
  370. vp9_predict_intra_block(xd, b_width_log2_lookup[bsize], tx_size, mode,
  371. x->skip_encode ? p->src.buf : pd->dst.buf,
  372. x->skip_encode ? src_stride : dst_stride,
  373. pd->dst.buf, dst_stride, col, row, 0);
  374. }
  375. }
  376. p->src.buf = src_buf_base;
  377. pd->dst.buf = dst_buf_base;
  378. }
  379. static void model_rd_for_sb_y_large(VP9_COMP *cpi, BLOCK_SIZE bsize,
  380. MACROBLOCK *x, MACROBLOCKD *xd,
  381. int *out_rate_sum, int64_t *out_dist_sum,
  382. unsigned int *var_y, unsigned int *sse_y,
  383. int mi_row, int mi_col, int *early_term,
  384. int *flag_preduv_computed) {
  385. // Note our transform coeffs are 8 times an orthogonal transform.
  386. // Hence quantizer step is also 8 times. To get effective quantizer
  387. // we need to divide by 8 before sending to modeling function.
  388. unsigned int sse;
  389. int rate;
  390. int64_t dist;
  391. struct macroblock_plane *const p = &x->plane[0];
  392. struct macroblockd_plane *const pd = &xd->plane[0];
  393. const uint32_t dc_quant = pd->dequant[0];
  394. const uint32_t ac_quant = pd->dequant[1];
  395. int64_t dc_thr = dc_quant * dc_quant >> 6;
  396. int64_t ac_thr = ac_quant * ac_quant >> 6;
  397. unsigned int var;
  398. int sum;
  399. int skip_dc = 0;
  400. const int bw = b_width_log2_lookup[bsize];
  401. const int bh = b_height_log2_lookup[bsize];
  402. const int num8x8 = 1 << (bw + bh - 2);
  403. unsigned int sse8x8[64] = { 0 };
  404. int sum8x8[64] = { 0 };
  405. unsigned int var8x8[64] = { 0 };
  406. TX_SIZE tx_size;
  407. int i, k;
  408. #if CONFIG_VP9_HIGHBITDEPTH
  409. const vpx_bit_depth_t bd = cpi->common.bit_depth;
  410. #endif
  411. // Calculate variance for whole partition, and also save 8x8 blocks' variance
  412. // to be used in following transform skipping test.
  413. block_variance(p->src.buf, p->src.stride, pd->dst.buf, pd->dst.stride,
  414. 4 << bw, 4 << bh, &sse, &sum, 8,
  415. #if CONFIG_VP9_HIGHBITDEPTH
  416. cpi->common.use_highbitdepth, bd,
  417. #endif
  418. sse8x8, sum8x8, var8x8);
  419. var = sse - (unsigned int)(((int64_t)sum * sum) >> (bw + bh + 4));
  420. *var_y = var;
  421. *sse_y = sse;
  422. #if CONFIG_VP9_TEMPORAL_DENOISING
  423. if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi) &&
  424. cpi->oxcf.speed > 5)
  425. ac_thr = vp9_scale_acskip_thresh(ac_thr, cpi->denoiser.denoising_level,
  426. (abs(sum) >> (bw + bh)),
  427. cpi->svc.temporal_layer_id);
  428. else
  429. ac_thr *= ac_thr_factor(cpi->oxcf.speed, cpi->common.width,
  430. cpi->common.height, abs(sum) >> (bw + bh));
  431. #else
  432. ac_thr *= ac_thr_factor(cpi->oxcf.speed, cpi->common.width,
  433. cpi->common.height, abs(sum) >> (bw + bh));
  434. #endif
  435. tx_size = calculate_tx_size(cpi, bsize, xd, var, sse, ac_thr,
  436. x->source_variance, 0);
  437. // The code below for setting skip flag assumes tranform size of at least 8x8,
  438. // so force this lower limit on transform.
  439. if (tx_size < TX_8X8) tx_size = TX_8X8;
  440. xd->mi[0]->tx_size = tx_size;
  441. if (cpi->oxcf.content == VP9E_CONTENT_SCREEN && x->zero_temp_sad_source &&
  442. x->source_variance == 0)
  443. dc_thr = dc_thr << 1;
  444. // Evaluate if the partition block is a skippable block in Y plane.
  445. {
  446. unsigned int sse16x16[16] = { 0 };
  447. int sum16x16[16] = { 0 };
  448. unsigned int var16x16[16] = { 0 };
  449. const int num16x16 = num8x8 >> 2;
  450. unsigned int sse32x32[4] = { 0 };
  451. int sum32x32[4] = { 0 };
  452. unsigned int var32x32[4] = { 0 };
  453. const int num32x32 = num8x8 >> 4;
  454. int ac_test = 1;
  455. int dc_test = 1;
  456. const int num = (tx_size == TX_8X8)
  457. ? num8x8
  458. : ((tx_size == TX_16X16) ? num16x16 : num32x32);
  459. const unsigned int *sse_tx =
  460. (tx_size == TX_8X8) ? sse8x8
  461. : ((tx_size == TX_16X16) ? sse16x16 : sse32x32);
  462. const unsigned int *var_tx =
  463. (tx_size == TX_8X8) ? var8x8
  464. : ((tx_size == TX_16X16) ? var16x16 : var32x32);
  465. // Calculate variance if tx_size > TX_8X8
  466. if (tx_size >= TX_16X16)
  467. calculate_variance(bw, bh, TX_8X8, sse8x8, sum8x8, var16x16, sse16x16,
  468. sum16x16);
  469. if (tx_size == TX_32X32)
  470. calculate_variance(bw, bh, TX_16X16, sse16x16, sum16x16, var32x32,
  471. sse32x32, sum32x32);
  472. // Skipping test
  473. x->skip_txfm[0] = SKIP_TXFM_NONE;
  474. for (k = 0; k < num; k++)
  475. // Check if all ac coefficients can be quantized to zero.
  476. if (!(var_tx[k] < ac_thr || var == 0)) {
  477. ac_test = 0;
  478. break;
  479. }
  480. for (k = 0; k < num; k++)
  481. // Check if dc coefficient can be quantized to zero.
  482. if (!(sse_tx[k] - var_tx[k] < dc_thr || sse == var)) {
  483. dc_test = 0;
  484. break;
  485. }
  486. if (ac_test) {
  487. x->skip_txfm[0] = SKIP_TXFM_AC_ONLY;
  488. if (dc_test) x->skip_txfm[0] = SKIP_TXFM_AC_DC;
  489. } else if (dc_test) {
  490. skip_dc = 1;
  491. }
  492. }
  493. if (x->skip_txfm[0] == SKIP_TXFM_AC_DC) {
  494. int skip_uv[2] = { 0 };
  495. unsigned int var_uv[2];
  496. unsigned int sse_uv[2];
  497. *out_rate_sum = 0;
  498. *out_dist_sum = sse << 4;
  499. // Transform skipping test in UV planes.
  500. for (i = 1; i <= 2; i++) {
  501. struct macroblock_plane *const p = &x->plane[i];
  502. struct macroblockd_plane *const pd = &xd->plane[i];
  503. const TX_SIZE uv_tx_size = get_uv_tx_size(xd->mi[0], pd);
  504. const BLOCK_SIZE unit_size = txsize_to_bsize[uv_tx_size];
  505. const BLOCK_SIZE uv_bsize = get_plane_block_size(bsize, pd);
  506. const int uv_bw = b_width_log2_lookup[uv_bsize];
  507. const int uv_bh = b_height_log2_lookup[uv_bsize];
  508. const int sf = (uv_bw - b_width_log2_lookup[unit_size]) +
  509. (uv_bh - b_height_log2_lookup[unit_size]);
  510. const uint32_t uv_dc_thr = pd->dequant[0] * pd->dequant[0] >> (6 - sf);
  511. const uint32_t uv_ac_thr = pd->dequant[1] * pd->dequant[1] >> (6 - sf);
  512. int j = i - 1;
  513. vp9_build_inter_predictors_sbp(xd, mi_row, mi_col, bsize, i);
  514. flag_preduv_computed[i - 1] = 1;
  515. var_uv[j] = cpi->fn_ptr[uv_bsize].vf(
  516. p->src.buf, p->src.stride, pd->dst.buf, pd->dst.stride, &sse_uv[j]);
  517. if ((var_uv[j] < uv_ac_thr || var_uv[j] == 0) &&
  518. (sse_uv[j] - var_uv[j] < uv_dc_thr || sse_uv[j] == var_uv[j]))
  519. skip_uv[j] = 1;
  520. else
  521. break;
  522. }
  523. // If the transform in YUV planes are skippable, the mode search checks
  524. // fewer inter modes and doesn't check intra modes.
  525. if (skip_uv[0] & skip_uv[1]) {
  526. *early_term = 1;
  527. }
  528. return;
  529. }
  530. if (!skip_dc) {
  531. #if CONFIG_VP9_HIGHBITDEPTH
  532. vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
  533. dc_quant >> (xd->bd - 5), &rate, &dist);
  534. #else
  535. vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
  536. dc_quant >> 3, &rate, &dist);
  537. #endif // CONFIG_VP9_HIGHBITDEPTH
  538. }
  539. if (!skip_dc) {
  540. *out_rate_sum = rate >> 1;
  541. *out_dist_sum = dist << 3;
  542. } else {
  543. *out_rate_sum = 0;
  544. *out_dist_sum = (sse - var) << 4;
  545. }
  546. #if CONFIG_VP9_HIGHBITDEPTH
  547. vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bsize],
  548. ac_quant >> (xd->bd - 5), &rate, &dist);
  549. #else
  550. vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bsize], ac_quant >> 3,
  551. &rate, &dist);
  552. #endif // CONFIG_VP9_HIGHBITDEPTH
  553. *out_rate_sum += rate;
  554. *out_dist_sum += dist << 4;
  555. }
  556. static void model_rd_for_sb_y(VP9_COMP *cpi, BLOCK_SIZE bsize, MACROBLOCK *x,
  557. MACROBLOCKD *xd, int *out_rate_sum,
  558. int64_t *out_dist_sum, unsigned int *var_y,
  559. unsigned int *sse_y, int is_intra) {
  560. // Note our transform coeffs are 8 times an orthogonal transform.
  561. // Hence quantizer step is also 8 times. To get effective quantizer
  562. // we need to divide by 8 before sending to modeling function.
  563. unsigned int sse;
  564. int rate;
  565. int64_t dist;
  566. struct macroblock_plane *const p = &x->plane[0];
  567. struct macroblockd_plane *const pd = &xd->plane[0];
  568. const int64_t dc_thr = p->quant_thred[0] >> 6;
  569. const int64_t ac_thr = p->quant_thred[1] >> 6;
  570. const uint32_t dc_quant = pd->dequant[0];
  571. const uint32_t ac_quant = pd->dequant[1];
  572. unsigned int var = cpi->fn_ptr[bsize].vf(p->src.buf, p->src.stride,
  573. pd->dst.buf, pd->dst.stride, &sse);
  574. int skip_dc = 0;
  575. *var_y = var;
  576. *sse_y = sse;
  577. xd->mi[0]->tx_size = calculate_tx_size(cpi, bsize, xd, var, sse, ac_thr,
  578. x->source_variance, is_intra);
  579. // Evaluate if the partition block is a skippable block in Y plane.
  580. {
  581. const BLOCK_SIZE unit_size = txsize_to_bsize[xd->mi[0]->tx_size];
  582. const unsigned int num_blk_log2 =
  583. (b_width_log2_lookup[bsize] - b_width_log2_lookup[unit_size]) +
  584. (b_height_log2_lookup[bsize] - b_height_log2_lookup[unit_size]);
  585. const unsigned int sse_tx = sse >> num_blk_log2;
  586. const unsigned int var_tx = var >> num_blk_log2;
  587. x->skip_txfm[0] = SKIP_TXFM_NONE;
  588. // Check if all ac coefficients can be quantized to zero.
  589. if (var_tx < ac_thr || var == 0) {
  590. x->skip_txfm[0] = SKIP_TXFM_AC_ONLY;
  591. // Check if dc coefficient can be quantized to zero.
  592. if (sse_tx - var_tx < dc_thr || sse == var)
  593. x->skip_txfm[0] = SKIP_TXFM_AC_DC;
  594. } else {
  595. if (sse_tx - var_tx < dc_thr || sse == var) skip_dc = 1;
  596. }
  597. }
  598. if (x->skip_txfm[0] == SKIP_TXFM_AC_DC) {
  599. *out_rate_sum = 0;
  600. *out_dist_sum = sse << 4;
  601. return;
  602. }
  603. if (!skip_dc) {
  604. #if CONFIG_VP9_HIGHBITDEPTH
  605. vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
  606. dc_quant >> (xd->bd - 5), &rate, &dist);
  607. #else
  608. vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
  609. dc_quant >> 3, &rate, &dist);
  610. #endif // CONFIG_VP9_HIGHBITDEPTH
  611. }
  612. if (!skip_dc) {
  613. *out_rate_sum = rate >> 1;
  614. *out_dist_sum = dist << 3;
  615. } else {
  616. *out_rate_sum = 0;
  617. *out_dist_sum = (sse - var) << 4;
  618. }
  619. #if CONFIG_VP9_HIGHBITDEPTH
  620. vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bsize],
  621. ac_quant >> (xd->bd - 5), &rate, &dist);
  622. #else
  623. vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bsize], ac_quant >> 3,
  624. &rate, &dist);
  625. #endif // CONFIG_VP9_HIGHBITDEPTH
  626. *out_rate_sum += rate;
  627. *out_dist_sum += dist << 4;
  628. }
  629. static void block_yrd(VP9_COMP *cpi, MACROBLOCK *x, RD_COST *this_rdc,
  630. int *skippable, int64_t *sse, BLOCK_SIZE bsize,
  631. TX_SIZE tx_size, int rd_computed, int is_intra) {
  632. MACROBLOCKD *xd = &x->e_mbd;
  633. const struct macroblockd_plane *pd = &xd->plane[0];
  634. struct macroblock_plane *const p = &x->plane[0];
  635. const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
  636. const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
  637. const int step = 1 << (tx_size << 1);
  638. const int block_step = (1 << tx_size);
  639. int block = 0, r, c;
  640. const int max_blocks_wide =
  641. num_4x4_w + (xd->mb_to_right_edge >= 0 ? 0 : xd->mb_to_right_edge >> 5);
  642. const int max_blocks_high =
  643. num_4x4_h + (xd->mb_to_bottom_edge >= 0 ? 0 : xd->mb_to_bottom_edge >> 5);
  644. int eob_cost = 0;
  645. const int bw = 4 * num_4x4_w;
  646. const int bh = 4 * num_4x4_h;
  647. if (cpi->sf.use_simple_block_yrd && cpi->common.frame_type != KEY_FRAME &&
  648. (bsize < BLOCK_32X32 ||
  649. (cpi->use_svc &&
  650. (bsize < BLOCK_32X32 || cpi->svc.temporal_layer_id > 0)))) {
  651. unsigned int var_y, sse_y;
  652. (void)tx_size;
  653. if (!rd_computed)
  654. model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc->rate, &this_rdc->dist,
  655. &var_y, &sse_y, is_intra);
  656. *sse = INT_MAX;
  657. *skippable = 0;
  658. return;
  659. }
  660. (void)cpi;
  661. // The max tx_size passed in is TX_16X16.
  662. assert(tx_size != TX_32X32);
  663. #if CONFIG_VP9_HIGHBITDEPTH
  664. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  665. vpx_highbd_subtract_block(bh, bw, p->src_diff, bw, p->src.buf,
  666. p->src.stride, pd->dst.buf, pd->dst.stride,
  667. x->e_mbd.bd);
  668. } else {
  669. vpx_subtract_block(bh, bw, p->src_diff, bw, p->src.buf, p->src.stride,
  670. pd->dst.buf, pd->dst.stride);
  671. }
  672. #else
  673. vpx_subtract_block(bh, bw, p->src_diff, bw, p->src.buf, p->src.stride,
  674. pd->dst.buf, pd->dst.stride);
  675. #endif
  676. *skippable = 1;
  677. // Keep track of the row and column of the blocks we use so that we know
  678. // if we are in the unrestricted motion border.
  679. for (r = 0; r < max_blocks_high; r += block_step) {
  680. for (c = 0; c < num_4x4_w; c += block_step) {
  681. if (c < max_blocks_wide) {
  682. const scan_order *const scan_order = &vp9_default_scan_orders[tx_size];
  683. tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
  684. tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  685. tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  686. uint16_t *const eob = &p->eobs[block];
  687. const int diff_stride = bw;
  688. const int16_t *src_diff;
  689. src_diff = &p->src_diff[(r * diff_stride + c) << 2];
  690. switch (tx_size) {
  691. case TX_16X16:
  692. vpx_hadamard_16x16(src_diff, diff_stride, coeff);
  693. vp9_quantize_fp(coeff, 256, x->skip_block, p->round_fp, p->quant_fp,
  694. qcoeff, dqcoeff, pd->dequant, eob, scan_order->scan,
  695. scan_order->iscan);
  696. break;
  697. case TX_8X8:
  698. vpx_hadamard_8x8(src_diff, diff_stride, coeff);
  699. vp9_quantize_fp(coeff, 64, x->skip_block, p->round_fp, p->quant_fp,
  700. qcoeff, dqcoeff, pd->dequant, eob, scan_order->scan,
  701. scan_order->iscan);
  702. break;
  703. default:
  704. assert(tx_size == TX_4X4);
  705. x->fwd_txfm4x4(src_diff, coeff, diff_stride);
  706. vp9_quantize_fp(coeff, 16, x->skip_block, p->round_fp, p->quant_fp,
  707. qcoeff, dqcoeff, pd->dequant, eob, scan_order->scan,
  708. scan_order->iscan);
  709. break;
  710. }
  711. *skippable &= (*eob == 0);
  712. eob_cost += 1;
  713. }
  714. block += step;
  715. }
  716. }
  717. this_rdc->rate = 0;
  718. if (*sse < INT64_MAX) {
  719. *sse = (*sse << 6) >> 2;
  720. if (*skippable) {
  721. this_rdc->dist = *sse;
  722. return;
  723. }
  724. }
  725. block = 0;
  726. this_rdc->dist = 0;
  727. for (r = 0; r < max_blocks_high; r += block_step) {
  728. for (c = 0; c < num_4x4_w; c += block_step) {
  729. if (c < max_blocks_wide) {
  730. tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
  731. tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  732. tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  733. uint16_t *const eob = &p->eobs[block];
  734. if (*eob == 1)
  735. this_rdc->rate += (int)abs(qcoeff[0]);
  736. else if (*eob > 1)
  737. this_rdc->rate += vpx_satd(qcoeff, step << 4);
  738. this_rdc->dist += vp9_block_error_fp(coeff, dqcoeff, step << 4) >> 2;
  739. }
  740. block += step;
  741. }
  742. }
  743. // If skippable is set, rate gets clobbered later.
  744. this_rdc->rate <<= (2 + VP9_PROB_COST_SHIFT);
  745. this_rdc->rate += (eob_cost << VP9_PROB_COST_SHIFT);
  746. }
  747. static void model_rd_for_sb_uv(VP9_COMP *cpi, BLOCK_SIZE plane_bsize,
  748. MACROBLOCK *x, MACROBLOCKD *xd,
  749. RD_COST *this_rdc, unsigned int *var_y,
  750. unsigned int *sse_y, int start_plane,
  751. int stop_plane) {
  752. // Note our transform coeffs are 8 times an orthogonal transform.
  753. // Hence quantizer step is also 8 times. To get effective quantizer
  754. // we need to divide by 8 before sending to modeling function.
  755. unsigned int sse;
  756. int rate;
  757. int64_t dist;
  758. int i;
  759. #if CONFIG_VP9_HIGHBITDEPTH
  760. uint64_t tot_var = *var_y;
  761. uint64_t tot_sse = *sse_y;
  762. #else
  763. uint32_t tot_var = *var_y;
  764. uint32_t tot_sse = *sse_y;
  765. #endif
  766. this_rdc->rate = 0;
  767. this_rdc->dist = 0;
  768. for (i = start_plane; i <= stop_plane; ++i) {
  769. struct macroblock_plane *const p = &x->plane[i];
  770. struct macroblockd_plane *const pd = &xd->plane[i];
  771. const uint32_t dc_quant = pd->dequant[0];
  772. const uint32_t ac_quant = pd->dequant[1];
  773. const BLOCK_SIZE bs = plane_bsize;
  774. unsigned int var;
  775. if (!x->color_sensitivity[i - 1]) continue;
  776. var = cpi->fn_ptr[bs].vf(p->src.buf, p->src.stride, pd->dst.buf,
  777. pd->dst.stride, &sse);
  778. assert(sse >= var);
  779. tot_var += var;
  780. tot_sse += sse;
  781. #if CONFIG_VP9_HIGHBITDEPTH
  782. vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bs],
  783. dc_quant >> (xd->bd - 5), &rate, &dist);
  784. #else
  785. vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bs],
  786. dc_quant >> 3, &rate, &dist);
  787. #endif // CONFIG_VP9_HIGHBITDEPTH
  788. this_rdc->rate += rate >> 1;
  789. this_rdc->dist += dist << 3;
  790. #if CONFIG_VP9_HIGHBITDEPTH
  791. vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bs],
  792. ac_quant >> (xd->bd - 5), &rate, &dist);
  793. #else
  794. vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bs], ac_quant >> 3,
  795. &rate, &dist);
  796. #endif // CONFIG_VP9_HIGHBITDEPTH
  797. this_rdc->rate += rate;
  798. this_rdc->dist += dist << 4;
  799. }
  800. #if CONFIG_VP9_HIGHBITDEPTH
  801. *var_y = tot_var > UINT32_MAX ? UINT32_MAX : (uint32_t)tot_var;
  802. *sse_y = tot_sse > UINT32_MAX ? UINT32_MAX : (uint32_t)tot_sse;
  803. #else
  804. *var_y = tot_var;
  805. *sse_y = tot_sse;
  806. #endif
  807. }
  808. static int get_pred_buffer(PRED_BUFFER *p, int len) {
  809. int i;
  810. for (i = 0; i < len; i++) {
  811. if (!p[i].in_use) {
  812. p[i].in_use = 1;
  813. return i;
  814. }
  815. }
  816. return -1;
  817. }
  818. static void free_pred_buffer(PRED_BUFFER *p) {
  819. if (p != NULL) p->in_use = 0;
  820. }
  821. static void encode_breakout_test(
  822. VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int mi_row, int mi_col,
  823. MV_REFERENCE_FRAME ref_frame, PREDICTION_MODE this_mode, unsigned int var_y,
  824. unsigned int sse_y, struct buf_2d yv12_mb[][MAX_MB_PLANE], int *rate,
  825. int64_t *dist, int *flag_preduv_computed) {
  826. MACROBLOCKD *xd = &x->e_mbd;
  827. MODE_INFO *const mi = xd->mi[0];
  828. const BLOCK_SIZE uv_size = get_plane_block_size(bsize, &xd->plane[1]);
  829. unsigned int var = var_y, sse = sse_y;
  830. // Skipping threshold for ac.
  831. unsigned int thresh_ac;
  832. // Skipping threshold for dc.
  833. unsigned int thresh_dc;
  834. int motion_low = 1;
  835. if (cpi->use_svc && ref_frame == GOLDEN_FRAME) return;
  836. if (mi->mv[0].as_mv.row > 64 || mi->mv[0].as_mv.row < -64 ||
  837. mi->mv[0].as_mv.col > 64 || mi->mv[0].as_mv.col < -64)
  838. motion_low = 0;
  839. if (x->encode_breakout > 0 && motion_low == 1) {
  840. // Set a maximum for threshold to avoid big PSNR loss in low bit rate
  841. // case. Use extreme low threshold for static frames to limit
  842. // skipping.
  843. const unsigned int max_thresh = 36000;
  844. // The encode_breakout input
  845. const unsigned int min_thresh =
  846. VPXMIN(((unsigned int)x->encode_breakout << 4), max_thresh);
  847. #if CONFIG_VP9_HIGHBITDEPTH
  848. const int shift = (xd->bd << 1) - 16;
  849. #endif
  850. // Calculate threshold according to dequant value.
  851. thresh_ac = (xd->plane[0].dequant[1] * xd->plane[0].dequant[1]) >> 3;
  852. #if CONFIG_VP9_HIGHBITDEPTH
  853. if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) && shift > 0) {
  854. thresh_ac = ROUND_POWER_OF_TWO(thresh_ac, shift);
  855. }
  856. #endif // CONFIG_VP9_HIGHBITDEPTH
  857. thresh_ac = clamp(thresh_ac, min_thresh, max_thresh);
  858. // Adjust ac threshold according to partition size.
  859. thresh_ac >>=
  860. 8 - (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
  861. thresh_dc = (xd->plane[0].dequant[0] * xd->plane[0].dequant[0] >> 6);
  862. #if CONFIG_VP9_HIGHBITDEPTH
  863. if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) && shift > 0) {
  864. thresh_dc = ROUND_POWER_OF_TWO(thresh_dc, shift);
  865. }
  866. #endif // CONFIG_VP9_HIGHBITDEPTH
  867. } else {
  868. thresh_ac = 0;
  869. thresh_dc = 0;
  870. }
  871. // Y skipping condition checking for ac and dc.
  872. if (var <= thresh_ac && (sse - var) <= thresh_dc) {
  873. unsigned int sse_u, sse_v;
  874. unsigned int var_u, var_v;
  875. unsigned int thresh_ac_uv = thresh_ac;
  876. unsigned int thresh_dc_uv = thresh_dc;
  877. if (x->sb_is_skin) {
  878. thresh_ac_uv = 0;
  879. thresh_dc_uv = 0;
  880. }
  881. if (!flag_preduv_computed[0] || !flag_preduv_computed[1]) {
  882. xd->plane[1].pre[0] = yv12_mb[ref_frame][1];
  883. xd->plane[2].pre[0] = yv12_mb[ref_frame][2];
  884. vp9_build_inter_predictors_sbuv(xd, mi_row, mi_col, bsize);
  885. }
  886. var_u = cpi->fn_ptr[uv_size].vf(x->plane[1].src.buf, x->plane[1].src.stride,
  887. xd->plane[1].dst.buf,
  888. xd->plane[1].dst.stride, &sse_u);
  889. // U skipping condition checking
  890. if (((var_u << 2) <= thresh_ac_uv) && (sse_u - var_u <= thresh_dc_uv)) {
  891. var_v = cpi->fn_ptr[uv_size].vf(
  892. x->plane[2].src.buf, x->plane[2].src.stride, xd->plane[2].dst.buf,
  893. xd->plane[2].dst.stride, &sse_v);
  894. // V skipping condition checking
  895. if (((var_v << 2) <= thresh_ac_uv) && (sse_v - var_v <= thresh_dc_uv)) {
  896. x->skip = 1;
  897. // The cost of skip bit needs to be added.
  898. *rate = cpi->inter_mode_cost[x->mbmi_ext->mode_context[ref_frame]]
  899. [INTER_OFFSET(this_mode)];
  900. // More on this part of rate
  901. // rate += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
  902. // Scaling factor for SSE from spatial domain to frequency
  903. // domain is 16. Adjust distortion accordingly.
  904. // TODO(yunqingwang): In this function, only y-plane dist is
  905. // calculated.
  906. *dist = (sse << 4); // + ((sse_u + sse_v) << 4);
  907. // *disable_skip = 1;
  908. }
  909. }
  910. }
  911. }
  912. struct estimate_block_intra_args {
  913. VP9_COMP *cpi;
  914. MACROBLOCK *x;
  915. PREDICTION_MODE mode;
  916. int skippable;
  917. RD_COST *rdc;
  918. };
  919. static void estimate_block_intra(int plane, int block, int row, int col,
  920. BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
  921. void *arg) {
  922. struct estimate_block_intra_args *const args = arg;
  923. VP9_COMP *const cpi = args->cpi;
  924. MACROBLOCK *const x = args->x;
  925. MACROBLOCKD *const xd = &x->e_mbd;
  926. struct macroblock_plane *const p = &x->plane[plane];
  927. struct macroblockd_plane *const pd = &xd->plane[plane];
  928. const BLOCK_SIZE bsize_tx = txsize_to_bsize[tx_size];
  929. uint8_t *const src_buf_base = p->src.buf;
  930. uint8_t *const dst_buf_base = pd->dst.buf;
  931. const int src_stride = p->src.stride;
  932. const int dst_stride = pd->dst.stride;
  933. RD_COST this_rdc;
  934. (void)block;
  935. p->src.buf = &src_buf_base[4 * (row * (int64_t)src_stride + col)];
  936. pd->dst.buf = &dst_buf_base[4 * (row * (int64_t)dst_stride + col)];
  937. // Use source buffer as an approximation for the fully reconstructed buffer.
  938. vp9_predict_intra_block(xd, b_width_log2_lookup[plane_bsize], tx_size,
  939. args->mode, x->skip_encode ? p->src.buf : pd->dst.buf,
  940. x->skip_encode ? src_stride : dst_stride, pd->dst.buf,
  941. dst_stride, col, row, plane);
  942. if (plane == 0) {
  943. int64_t this_sse = INT64_MAX;
  944. block_yrd(cpi, x, &this_rdc, &args->skippable, &this_sse, bsize_tx,
  945. VPXMIN(tx_size, TX_16X16), 0, 1);
  946. } else {
  947. unsigned int var = 0;
  948. unsigned int sse = 0;
  949. model_rd_for_sb_uv(cpi, bsize_tx, x, xd, &this_rdc, &var, &sse, plane,
  950. plane);
  951. }
  952. p->src.buf = src_buf_base;
  953. pd->dst.buf = dst_buf_base;
  954. args->rdc->rate += this_rdc.rate;
  955. args->rdc->dist += this_rdc.dist;
  956. }
  957. static const THR_MODES mode_idx[MAX_REF_FRAMES][4] = {
  958. { THR_DC, THR_V_PRED, THR_H_PRED, THR_TM },
  959. { THR_NEARESTMV, THR_NEARMV, THR_ZEROMV, THR_NEWMV },
  960. { THR_NEARESTG, THR_NEARG, THR_ZEROG, THR_NEWG },
  961. { THR_NEARESTA, THR_NEARA, THR_ZEROA, THR_NEWA },
  962. };
  963. static const PREDICTION_MODE intra_mode_list[] = { DC_PRED, V_PRED, H_PRED,
  964. TM_PRED };
  965. static int mode_offset(const PREDICTION_MODE mode) {
  966. if (mode >= NEARESTMV) {
  967. return INTER_OFFSET(mode);
  968. } else {
  969. switch (mode) {
  970. case DC_PRED: return 0;
  971. case V_PRED: return 1;
  972. case H_PRED: return 2;
  973. case TM_PRED: return 3;
  974. default: return -1;
  975. }
  976. }
  977. }
  978. static INLINE int rd_less_than_thresh_row_mt(int64_t best_rd, int thresh,
  979. const int *const thresh_fact) {
  980. int is_rd_less_than_thresh;
  981. is_rd_less_than_thresh =
  982. best_rd < ((int64_t)thresh * (*thresh_fact) >> 5) || thresh == INT_MAX;
  983. return is_rd_less_than_thresh;
  984. }
  985. static INLINE void update_thresh_freq_fact_row_mt(
  986. VP9_COMP *cpi, TileDataEnc *tile_data, int source_variance,
  987. int thresh_freq_fact_idx, MV_REFERENCE_FRAME ref_frame,
  988. THR_MODES best_mode_idx, PREDICTION_MODE mode) {
  989. THR_MODES thr_mode_idx = mode_idx[ref_frame][mode_offset(mode)];
  990. int freq_fact_idx = thresh_freq_fact_idx + thr_mode_idx;
  991. int *freq_fact = &tile_data->row_base_thresh_freq_fact[freq_fact_idx];
  992. if (thr_mode_idx == best_mode_idx)
  993. *freq_fact -= (*freq_fact >> 4);
  994. else if (cpi->sf.limit_newmv_early_exit && mode == NEWMV &&
  995. ref_frame == LAST_FRAME && source_variance < 5) {
  996. *freq_fact = VPXMIN(*freq_fact + RD_THRESH_INC, 32);
  997. } else {
  998. *freq_fact = VPXMIN(*freq_fact + RD_THRESH_INC,
  999. cpi->sf.adaptive_rd_thresh * RD_THRESH_MAX_FACT);
  1000. }
  1001. }
  1002. static INLINE void update_thresh_freq_fact(
  1003. VP9_COMP *cpi, TileDataEnc *tile_data, int source_variance,
  1004. BLOCK_SIZE bsize, MV_REFERENCE_FRAME ref_frame, THR_MODES best_mode_idx,
  1005. PREDICTION_MODE mode) {
  1006. THR_MODES thr_mode_idx = mode_idx[ref_frame][mode_offset(mode)];
  1007. int *freq_fact = &tile_data->thresh_freq_fact[bsize][thr_mode_idx];
  1008. if (thr_mode_idx == best_mode_idx)
  1009. *freq_fact -= (*freq_fact >> 4);
  1010. else if (cpi->sf.limit_newmv_early_exit && mode == NEWMV &&
  1011. ref_frame == LAST_FRAME && source_variance < 5) {
  1012. *freq_fact = VPXMIN(*freq_fact + RD_THRESH_INC, 32);
  1013. } else {
  1014. *freq_fact = VPXMIN(*freq_fact + RD_THRESH_INC,
  1015. cpi->sf.adaptive_rd_thresh * RD_THRESH_MAX_FACT);
  1016. }
  1017. }
  1018. void vp9_pick_intra_mode(VP9_COMP *cpi, MACROBLOCK *x, RD_COST *rd_cost,
  1019. BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
  1020. MACROBLOCKD *const xd = &x->e_mbd;
  1021. MODE_INFO *const mi = xd->mi[0];
  1022. RD_COST this_rdc, best_rdc;
  1023. PREDICTION_MODE this_mode;
  1024. struct estimate_block_intra_args args = { cpi, x, DC_PRED, 1, 0 };
  1025. const TX_SIZE intra_tx_size =
  1026. VPXMIN(max_txsize_lookup[bsize],
  1027. tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
  1028. MODE_INFO *const mic = xd->mi[0];
  1029. int *bmode_costs;
  1030. const MODE_INFO *above_mi = xd->above_mi;
  1031. const MODE_INFO *left_mi = xd->left_mi;
  1032. const PREDICTION_MODE A = vp9_above_block_mode(mic, above_mi, 0);
  1033. const PREDICTION_MODE L = vp9_left_block_mode(mic, left_mi, 0);
  1034. bmode_costs = cpi->y_mode_costs[A][L];
  1035. (void)ctx;
  1036. vp9_rd_cost_reset(&best_rdc);
  1037. vp9_rd_cost_reset(&this_rdc);
  1038. mi->ref_frame[0] = INTRA_FRAME;
  1039. // Initialize interp_filter here so we do not have to check for inter block
  1040. // modes in get_pred_context_switchable_interp()
  1041. mi->interp_filter = SWITCHABLE_FILTERS;
  1042. mi->mv[0].as_int = INVALID_MV;
  1043. mi->uv_mode = DC_PRED;
  1044. memset(x->skip_txfm, 0, sizeof(x->skip_txfm));
  1045. // Change the limit of this loop to add other intra prediction
  1046. // mode tests.
  1047. for (this_mode = DC_PRED; this_mode <= H_PRED; ++this_mode) {
  1048. this_rdc.dist = this_rdc.rate = 0;
  1049. args.mode = this_mode;
  1050. args.skippable = 1;
  1051. args.rdc = &this_rdc;
  1052. mi->tx_size = intra_tx_size;
  1053. vp9_foreach_transformed_block_in_plane(xd, bsize, 0, estimate_block_intra,
  1054. &args);
  1055. if (args.skippable) {
  1056. x->skip_txfm[0] = SKIP_TXFM_AC_DC;
  1057. this_rdc.rate = vp9_cost_bit(vp9_get_skip_prob(&cpi->common, xd), 1);
  1058. } else {
  1059. x->skip_txfm[0] = SKIP_TXFM_NONE;
  1060. this_rdc.rate += vp9_cost_bit(vp9_get_skip_prob(&cpi->common, xd), 0);
  1061. }
  1062. this_rdc.rate += bmode_costs[this_mode];
  1063. this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist);
  1064. if (this_rdc.rdcost < best_rdc.rdcost) {
  1065. best_rdc = this_rdc;
  1066. mi->mode = this_mode;
  1067. }
  1068. }
  1069. *rd_cost = best_rdc;
  1070. }
  1071. static void init_ref_frame_cost(VP9_COMMON *const cm, MACROBLOCKD *const xd,
  1072. int ref_frame_cost[MAX_REF_FRAMES]) {
  1073. vpx_prob intra_inter_p = vp9_get_intra_inter_prob(cm, xd);
  1074. vpx_prob ref_single_p1 = vp9_get_pred_prob_single_ref_p1(cm, xd);
  1075. vpx_prob ref_single_p2 = vp9_get_pred_prob_single_ref_p2(cm, xd);
  1076. ref_frame_cost[INTRA_FRAME] = vp9_cost_bit(intra_inter_p, 0);
  1077. ref_frame_cost[LAST_FRAME] = ref_frame_cost[GOLDEN_FRAME] =
  1078. ref_frame_cost[ALTREF_FRAME] = vp9_cost_bit(intra_inter_p, 1);
  1079. ref_frame_cost[LAST_FRAME] += vp9_cost_bit(ref_single_p1, 0);
  1080. ref_frame_cost[GOLDEN_FRAME] += vp9_cost_bit(ref_single_p1, 1);
  1081. ref_frame_cost[ALTREF_FRAME] += vp9_cost_bit(ref_single_p1, 1);
  1082. ref_frame_cost[GOLDEN_FRAME] += vp9_cost_bit(ref_single_p2, 0);
  1083. ref_frame_cost[ALTREF_FRAME] += vp9_cost_bit(ref_single_p2, 1);
  1084. }
  1085. typedef struct {
  1086. MV_REFERENCE_FRAME ref_frame;
  1087. PREDICTION_MODE pred_mode;
  1088. } REF_MODE;
  1089. #define RT_INTER_MODES 12
  1090. static const REF_MODE ref_mode_set[RT_INTER_MODES] = {
  1091. { LAST_FRAME, ZEROMV }, { LAST_FRAME, NEARESTMV },
  1092. { GOLDEN_FRAME, ZEROMV }, { LAST_FRAME, NEARMV },
  1093. { LAST_FRAME, NEWMV }, { GOLDEN_FRAME, NEARESTMV },
  1094. { GOLDEN_FRAME, NEARMV }, { GOLDEN_FRAME, NEWMV },
  1095. { ALTREF_FRAME, ZEROMV }, { ALTREF_FRAME, NEARESTMV },
  1096. { ALTREF_FRAME, NEARMV }, { ALTREF_FRAME, NEWMV }
  1097. };
  1098. #define RT_INTER_MODES_SVC 8
  1099. static const REF_MODE ref_mode_set_svc[RT_INTER_MODES_SVC] = {
  1100. { LAST_FRAME, ZEROMV }, { LAST_FRAME, NEARESTMV },
  1101. { LAST_FRAME, NEARMV }, { GOLDEN_FRAME, ZEROMV },
  1102. { GOLDEN_FRAME, NEARESTMV }, { GOLDEN_FRAME, NEARMV },
  1103. { LAST_FRAME, NEWMV }, { GOLDEN_FRAME, NEWMV }
  1104. };
  1105. static INLINE void find_predictors(
  1106. VP9_COMP *cpi, MACROBLOCK *x, MV_REFERENCE_FRAME ref_frame,
  1107. int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES],
  1108. int const_motion[MAX_REF_FRAMES], int *ref_frame_skip_mask,
  1109. const int flag_list[4], TileDataEnc *tile_data, int mi_row, int mi_col,
  1110. struct buf_2d yv12_mb[4][MAX_MB_PLANE], BLOCK_SIZE bsize,
  1111. int force_skip_low_temp_var, int comp_pred_allowed) {
  1112. VP9_COMMON *const cm = &cpi->common;
  1113. MACROBLOCKD *const xd = &x->e_mbd;
  1114. const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
  1115. TileInfo *const tile_info = &tile_data->tile_info;
  1116. // TODO(jingning) placeholder for inter-frame non-RD mode decision.
  1117. x->pred_mv_sad[ref_frame] = INT_MAX;
  1118. frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
  1119. frame_mv[ZEROMV][ref_frame].as_int = 0;
  1120. // this needs various further optimizations. to be continued..
  1121. if ((cpi->ref_frame_flags & flag_list[ref_frame]) && (yv12 != NULL)) {
  1122. int_mv *const candidates = x->mbmi_ext->ref_mvs[ref_frame];
  1123. const struct scale_factors *const sf = &cm->frame_refs[ref_frame - 1].sf;
  1124. vp9_setup_pred_block(xd, yv12_mb[ref_frame], yv12, mi_row, mi_col, sf, sf);
  1125. if (cm->use_prev_frame_mvs || comp_pred_allowed) {
  1126. vp9_find_mv_refs(cm, xd, xd->mi[0], ref_frame, candidates, mi_row, mi_col,
  1127. x->mbmi_ext->mode_context);
  1128. } else {
  1129. const_motion[ref_frame] =
  1130. mv_refs_rt(cpi, cm, x, xd, tile_info, xd->mi[0], ref_frame,
  1131. candidates, &frame_mv[NEWMV][ref_frame], mi_row, mi_col,
  1132. (int)(cpi->svc.use_base_mv && cpi->svc.spatial_layer_id));
  1133. }
  1134. vp9_find_best_ref_mvs(xd, cm->allow_high_precision_mv, candidates,
  1135. &frame_mv[NEARESTMV][ref_frame],
  1136. &frame_mv[NEARMV][ref_frame]);
  1137. // Early exit for golden frame if force_skip_low_temp_var is set.
  1138. if (!vp9_is_scaled(sf) && bsize >= BLOCK_8X8 &&
  1139. !(force_skip_low_temp_var && ref_frame == GOLDEN_FRAME)) {
  1140. vp9_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride, ref_frame,
  1141. bsize);
  1142. }
  1143. } else {
  1144. *ref_frame_skip_mask |= (1 << ref_frame);
  1145. }
  1146. }
  1147. static void vp9_NEWMV_diff_bias(const NOISE_ESTIMATE *ne, MACROBLOCKD *xd,
  1148. PREDICTION_MODE this_mode, RD_COST *this_rdc,
  1149. BLOCK_SIZE bsize, int mv_row, int mv_col,
  1150. int is_last_frame, int lowvar_highsumdiff,
  1151. int is_skin) {
  1152. // Bias against MVs associated with NEWMV mode that are very different from
  1153. // top/left neighbors.
  1154. if (this_mode == NEWMV) {
  1155. int al_mv_average_row;
  1156. int al_mv_average_col;
  1157. int left_row, left_col;
  1158. int row_diff, col_diff;
  1159. int above_mv_valid = 0;
  1160. int left_mv_valid = 0;
  1161. int above_row = 0;
  1162. int above_col = 0;
  1163. if (xd->above_mi) {
  1164. above_mv_valid = xd->above_mi->mv[0].as_int != INVALID_MV;
  1165. above_row = xd->above_mi->mv[0].as_mv.row;
  1166. above_col = xd->above_mi->mv[0].as_mv.col;
  1167. }
  1168. if (xd->left_mi) {
  1169. left_mv_valid = xd->left_mi->mv[0].as_int != INVALID_MV;
  1170. left_row = xd->left_mi->mv[0].as_mv.row;
  1171. left_col = xd->left_mi->mv[0].as_mv.col;
  1172. }
  1173. if (above_mv_valid && left_mv_valid) {
  1174. al_mv_average_row = (above_row + left_row + 1) >> 1;
  1175. al_mv_average_col = (above_col + left_col + 1) >> 1;
  1176. } else if (above_mv_valid) {
  1177. al_mv_average_row = above_row;
  1178. al_mv_average_col = above_col;
  1179. } else if (left_mv_valid) {
  1180. al_mv_average_row = left_row;
  1181. al_mv_average_col = left_col;
  1182. } else {
  1183. al_mv_average_row = al_mv_average_col = 0;
  1184. }
  1185. row_diff = (al_mv_average_row - mv_row);
  1186. col_diff = (al_mv_average_col - mv_col);
  1187. if (row_diff > 48 || row_diff < -48 || col_diff > 48 || col_diff < -48) {
  1188. if (bsize > BLOCK_32X32)
  1189. this_rdc->rdcost = this_rdc->rdcost << 1;
  1190. else
  1191. this_rdc->rdcost = 3 * this_rdc->rdcost >> 1;
  1192. }
  1193. }
  1194. // If noise estimation is enabled, and estimated level is above threshold,
  1195. // add a bias to LAST reference with small motion, for large blocks.
  1196. if (ne->enabled && ne->level >= kMedium && bsize >= BLOCK_32X32 &&
  1197. is_last_frame && mv_row < 8 && mv_row > -8 && mv_col < 8 && mv_col > -8)
  1198. this_rdc->rdcost = 7 * (this_rdc->rdcost >> 3);
  1199. else if (lowvar_highsumdiff && !is_skin && bsize >= BLOCK_16X16 &&
  1200. is_last_frame && mv_row < 16 && mv_row > -16 && mv_col < 16 &&
  1201. mv_col > -16)
  1202. this_rdc->rdcost = 7 * (this_rdc->rdcost >> 3);
  1203. }
  1204. #if CONFIG_VP9_TEMPORAL_DENOISING
  1205. static void vp9_pickmode_ctx_den_update(
  1206. VP9_PICKMODE_CTX_DEN *ctx_den, int64_t zero_last_cost_orig,
  1207. int ref_frame_cost[MAX_REF_FRAMES],
  1208. int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES], int reuse_inter_pred,
  1209. BEST_PICKMODE *bp) {
  1210. ctx_den->zero_last_cost_orig = zero_last_cost_orig;
  1211. ctx_den->ref_frame_cost = ref_frame_cost;
  1212. ctx_den->frame_mv = frame_mv;
  1213. ctx_den->reuse_inter_pred = reuse_inter_pred;
  1214. ctx_den->best_tx_size = bp->best_tx_size;
  1215. ctx_den->best_mode = bp->best_mode;
  1216. ctx_den->best_ref_frame = bp->best_ref_frame;
  1217. ctx_den->best_pred_filter = bp->best_pred_filter;
  1218. ctx_den->best_mode_skip_txfm = bp->best_mode_skip_txfm;
  1219. }
  1220. static void recheck_zeromv_after_denoising(
  1221. VP9_COMP *cpi, MODE_INFO *const mi, MACROBLOCK *x, MACROBLOCKD *const xd,
  1222. VP9_DENOISER_DECISION decision, VP9_PICKMODE_CTX_DEN *ctx_den,
  1223. struct buf_2d yv12_mb[4][MAX_MB_PLANE], RD_COST *best_rdc, BLOCK_SIZE bsize,
  1224. int mi_row, int mi_col) {
  1225. // If INTRA or GOLDEN reference was selected, re-evaluate ZEROMV on
  1226. // denoised result. Only do this under noise conditions, and if rdcost of
  1227. // ZEROMV onoriginal source is not significantly higher than rdcost of best
  1228. // mode.
  1229. if (cpi->noise_estimate.enabled && cpi->noise_estimate.level > kLow &&
  1230. ctx_den->zero_last_cost_orig < (best_rdc->rdcost << 3) &&
  1231. ((ctx_den->best_ref_frame == INTRA_FRAME && decision >= FILTER_BLOCK) ||
  1232. (ctx_den->best_ref_frame == GOLDEN_FRAME &&
  1233. cpi->svc.number_spatial_layers == 1 &&
  1234. decision == FILTER_ZEROMV_BLOCK))) {
  1235. // Check if we should pick ZEROMV on denoised signal.
  1236. VP9_COMMON *const cm = &cpi->common;
  1237. int rate = 0;
  1238. int64_t dist = 0;
  1239. uint32_t var_y = UINT_MAX;
  1240. uint32_t sse_y = UINT_MAX;
  1241. RD_COST this_rdc;
  1242. mi->mode = ZEROMV;
  1243. mi->ref_frame[0] = LAST_FRAME;
  1244. mi->ref_frame[1] = NONE;
  1245. set_ref_ptrs(cm, xd, mi->ref_frame[0], NONE);
  1246. mi->mv[0].as_int = 0;
  1247. mi->interp_filter = EIGHTTAP;
  1248. if (cpi->sf.default_interp_filter == BILINEAR) mi->interp_filter = BILINEAR;
  1249. xd->plane[0].pre[0] = yv12_mb[LAST_FRAME][0];
  1250. vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
  1251. model_rd_for_sb_y(cpi, bsize, x, xd, &rate, &dist, &var_y, &sse_y, 0);
  1252. this_rdc.rate = rate + ctx_den->ref_frame_cost[LAST_FRAME] +
  1253. cpi->inter_mode_cost[x->mbmi_ext->mode_context[LAST_FRAME]]
  1254. [INTER_OFFSET(ZEROMV)];
  1255. this_rdc.dist = dist;
  1256. this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, rate, dist);
  1257. // Don't switch to ZEROMV if the rdcost for ZEROMV on denoised source
  1258. // is higher than best_ref mode (on original source).
  1259. if (this_rdc.rdcost > best_rdc->rdcost) {
  1260. this_rdc = *best_rdc;
  1261. mi->mode = ctx_den->best_mode;
  1262. mi->ref_frame[0] = ctx_den->best_ref_frame;
  1263. set_ref_ptrs(cm, xd, mi->ref_frame[0], NONE);
  1264. mi->interp_filter = ctx_den->best_pred_filter;
  1265. if (ctx_den->best_ref_frame == INTRA_FRAME) {
  1266. mi->mv[0].as_int = INVALID_MV;
  1267. mi->interp_filter = SWITCHABLE_FILTERS;
  1268. } else if (ctx_den->best_ref_frame == GOLDEN_FRAME) {
  1269. mi->mv[0].as_int =
  1270. ctx_den->frame_mv[ctx_den->best_mode][ctx_den->best_ref_frame]
  1271. .as_int;
  1272. if (ctx_den->reuse_inter_pred) {
  1273. xd->plane[0].pre[0] = yv12_mb[GOLDEN_FRAME][0];
  1274. vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
  1275. }
  1276. }
  1277. mi->tx_size = ctx_den->best_tx_size;
  1278. x->skip_txfm[0] = ctx_den->best_mode_skip_txfm;
  1279. } else {
  1280. ctx_den->best_ref_frame = LAST_FRAME;
  1281. *best_rdc = this_rdc;
  1282. }
  1283. }
  1284. }
  1285. #endif // CONFIG_VP9_TEMPORAL_DENOISING
  1286. static INLINE int get_force_skip_low_temp_var(uint8_t *variance_low, int mi_row,
  1287. int mi_col, BLOCK_SIZE bsize) {
  1288. const int i = (mi_row & 0x7) >> 1;
  1289. const int j = (mi_col & 0x7) >> 1;
  1290. int force_skip_low_temp_var = 0;
  1291. // Set force_skip_low_temp_var based on the block size and block offset.
  1292. if (bsize == BLOCK_64X64) {
  1293. force_skip_low_temp_var = variance_low[0];
  1294. } else if (bsize == BLOCK_64X32) {
  1295. if (!(mi_col & 0x7) && !(mi_row & 0x7)) {
  1296. force_skip_low_temp_var = variance_low[1];
  1297. } else if (!(mi_col & 0x7) && (mi_row & 0x7)) {
  1298. force_skip_low_temp_var = variance_low[2];
  1299. }
  1300. } else if (bsize == BLOCK_32X64) {
  1301. if (!(mi_col & 0x7) && !(mi_row & 0x7)) {
  1302. force_skip_low_temp_var = variance_low[3];
  1303. } else if ((mi_col & 0x7) && !(mi_row & 0x7)) {
  1304. force_skip_low_temp_var = variance_low[4];
  1305. }
  1306. } else if (bsize == BLOCK_32X32) {
  1307. if (!(mi_col & 0x7) && !(mi_row & 0x7)) {
  1308. force_skip_low_temp_var = variance_low[5];
  1309. } else if ((mi_col & 0x7) && !(mi_row & 0x7)) {
  1310. force_skip_low_temp_var = variance_low[6];
  1311. } else if (!(mi_col & 0x7) && (mi_row & 0x7)) {
  1312. force_skip_low_temp_var = variance_low[7];
  1313. } else if ((mi_col & 0x7) && (mi_row & 0x7)) {
  1314. force_skip_low_temp_var = variance_low[8];
  1315. }
  1316. } else if (bsize == BLOCK_16X16) {
  1317. force_skip_low_temp_var = variance_low[pos_shift_16x16[i][j]];
  1318. } else if (bsize == BLOCK_32X16) {
  1319. // The col shift index for the second 16x16 block.
  1320. const int j2 = ((mi_col + 2) & 0x7) >> 1;
  1321. // Only if each 16x16 block inside has low temporal variance.
  1322. force_skip_low_temp_var = variance_low[pos_shift_16x16[i][j]] &&
  1323. variance_low[pos_shift_16x16[i][j2]];
  1324. } else if (bsize == BLOCK_16X32) {
  1325. // The row shift index for the second 16x16 block.
  1326. const int i2 = ((mi_row + 2) & 0x7) >> 1;
  1327. force_skip_low_temp_var = variance_low[pos_shift_16x16[i][j]] &&
  1328. variance_low[pos_shift_16x16[i2][j]];
  1329. }
  1330. return force_skip_low_temp_var;
  1331. }
  1332. static void search_filter_ref(VP9_COMP *cpi, MACROBLOCK *x, RD_COST *this_rdc,
  1333. int mi_row, int mi_col, PRED_BUFFER *tmp,
  1334. BLOCK_SIZE bsize, int reuse_inter_pred,
  1335. PRED_BUFFER **this_mode_pred, unsigned int *var_y,
  1336. unsigned int *sse_y, int force_smooth_filter,
  1337. int *this_early_term, int *flag_preduv_computed,
  1338. int use_model_yrd_large) {
  1339. MACROBLOCKD *const xd = &x->e_mbd;
  1340. MODE_INFO *const mi = xd->mi[0];
  1341. struct macroblockd_plane *const pd = &xd->plane[0];
  1342. const int bw = num_4x4_blocks_wide_lookup[bsize] << 2;
  1343. int pf_rate[3] = { 0 };
  1344. int64_t pf_dist[3] = { 0 };
  1345. int curr_rate[3] = { 0 };
  1346. unsigned int pf_var[3] = { 0 };
  1347. unsigned int pf_sse[3] = { 0 };
  1348. TX_SIZE pf_tx_size[3] = { 0 };
  1349. int64_t best_cost = INT64_MAX;
  1350. INTERP_FILTER best_filter = SWITCHABLE, filter;
  1351. PRED_BUFFER *current_pred = *this_mode_pred;
  1352. uint8_t skip_txfm = SKIP_TXFM_NONE;
  1353. int best_early_term = 0;
  1354. int best_flag_preduv_computed[2] = { 0 };
  1355. INTERP_FILTER filter_start = force_smooth_filter ? EIGHTTAP_SMOOTH : EIGHTTAP;
  1356. for (filter = filter_start; filter <= EIGHTTAP_SMOOTH; ++filter) {
  1357. int64_t cost;
  1358. mi->interp_filter = filter;
  1359. vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
  1360. // For large partition blocks, extra testing is done.
  1361. if (use_model_yrd_large)
  1362. model_rd_for_sb_y_large(cpi, bsize, x, xd, &pf_rate[filter],
  1363. &pf_dist[filter], &pf_var[filter],
  1364. &pf_sse[filter], mi_row, mi_col, this_early_term,
  1365. flag_preduv_computed);
  1366. else
  1367. model_rd_for_sb_y(cpi, bsize, x, xd, &pf_rate[filter], &pf_dist[filter],
  1368. &pf_var[filter], &pf_sse[filter], 0);
  1369. curr_rate[filter] = pf_rate[filter];
  1370. pf_rate[filter] += vp9_get_switchable_rate(cpi, xd);
  1371. cost = RDCOST(x->rdmult, x->rddiv, pf_rate[filter], pf_dist[filter]);
  1372. pf_tx_size[filter] = mi->tx_size;
  1373. if (cost < best_cost) {
  1374. best_filter = filter;
  1375. best_cost = cost;
  1376. skip_txfm = x->skip_txfm[0];
  1377. best_early_term = *this_early_term;
  1378. best_flag_preduv_computed[0] = flag_preduv_computed[0];
  1379. best_flag_preduv_computed[1] = flag_preduv_computed[1];
  1380. if (reuse_inter_pred) {
  1381. if (*this_mode_pred != current_pred) {
  1382. free_pred_buffer(*this_mode_pred);
  1383. *this_mode_pred = current_pred;
  1384. }
  1385. current_pred = &tmp[get_pred_buffer(tmp, 3)];
  1386. pd->dst.buf = current_pred->data;
  1387. pd->dst.stride = bw;
  1388. }
  1389. }
  1390. }
  1391. if (reuse_inter_pred && *this_mode_pred != current_pred)
  1392. free_pred_buffer(current_pred);
  1393. mi->interp_filter = best_filter;
  1394. mi->tx_size = pf_tx_size[best_filter];
  1395. this_rdc->rate = curr_rate[best_filter];
  1396. this_rdc->dist = pf_dist[best_filter];
  1397. *var_y = pf_var[best_filter];
  1398. *sse_y = pf_sse[best_filter];
  1399. x->skip_txfm[0] = skip_txfm;
  1400. *this_early_term = best_early_term;
  1401. flag_preduv_computed[0] = best_flag_preduv_computed[0];
  1402. flag_preduv_computed[1] = best_flag_preduv_computed[1];
  1403. if (reuse_inter_pred) {
  1404. pd->dst.buf = (*this_mode_pred)->data;
  1405. pd->dst.stride = (*this_mode_pred)->stride;
  1406. }
  1407. }
  1408. static int search_new_mv(VP9_COMP *cpi, MACROBLOCK *x,
  1409. int_mv frame_mv[][MAX_REF_FRAMES],
  1410. MV_REFERENCE_FRAME ref_frame, int gf_temporal_ref,
  1411. BLOCK_SIZE bsize, int mi_row, int mi_col,
  1412. int best_pred_sad, int *rate_mv,
  1413. unsigned int best_sse_sofar, RD_COST *best_rdc) {
  1414. SVC *const svc = &cpi->svc;
  1415. MACROBLOCKD *const xd = &x->e_mbd;
  1416. MODE_INFO *const mi = xd->mi[0];
  1417. SPEED_FEATURES *const sf = &cpi->sf;
  1418. if (ref_frame > LAST_FRAME && gf_temporal_ref &&
  1419. cpi->oxcf.rc_mode == VPX_CBR) {
  1420. int tmp_sad;
  1421. uint32_t dis;
  1422. int cost_list[5] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX };
  1423. if (bsize < BLOCK_16X16) return -1;
  1424. tmp_sad = vp9_int_pro_motion_estimation(
  1425. cpi, x, bsize, mi_row, mi_col,
  1426. &x->mbmi_ext->ref_mvs[ref_frame][0].as_mv);
  1427. if (tmp_sad > x->pred_mv_sad[LAST_FRAME]) return -1;
  1428. if (tmp_sad + (num_pels_log2_lookup[bsize] << 4) > best_pred_sad) return -1;
  1429. frame_mv[NEWMV][ref_frame].as_int = mi->mv[0].as_int;
  1430. *rate_mv = vp9_mv_bit_cost(&frame_mv[NEWMV][ref_frame].as_mv,
  1431. &x->mbmi_ext->ref_mvs[ref_frame][0].as_mv,
  1432. x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
  1433. frame_mv[NEWMV][ref_frame].as_mv.row >>= 3;
  1434. frame_mv[NEWMV][ref_frame].as_mv.col >>= 3;
  1435. cpi->find_fractional_mv_step(
  1436. x, &frame_mv[NEWMV][ref_frame].as_mv,
  1437. &x->mbmi_ext->ref_mvs[ref_frame][0].as_mv,
  1438. cpi->common.allow_high_precision_mv, x->errorperbit,
  1439. &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop,
  1440. cpi->sf.mv.subpel_search_level, cond_cost_list(cpi, cost_list),
  1441. x->nmvjointcost, x->mvcost, &dis, &x->pred_sse[ref_frame], NULL, 0, 0,
  1442. cpi->sf.use_accurate_subpel_search);
  1443. } else if (svc->use_base_mv && svc->spatial_layer_id) {
  1444. if (frame_mv[NEWMV][ref_frame].as_int != INVALID_MV) {
  1445. const int pre_stride = xd->plane[0].pre[0].stride;
  1446. unsigned int base_mv_sse = UINT_MAX;
  1447. int scale = (cpi->rc.avg_frame_low_motion > 60) ? 2 : 4;
  1448. const uint8_t *const pre_buf =
  1449. xd->plane[0].pre[0].buf +
  1450. (frame_mv[NEWMV][ref_frame].as_mv.row >> 3) * pre_stride +
  1451. (frame_mv[NEWMV][ref_frame].as_mv.col >> 3);
  1452. cpi->fn_ptr[bsize].vf(x->plane[0].src.buf, x->plane[0].src.stride,
  1453. pre_buf, pre_stride, &base_mv_sse);
  1454. // Exit NEWMV search if base_mv is (0,0) && bsize < BLOCK_16x16,
  1455. // for SVC encoding.
  1456. if (cpi->use_svc && svc->use_base_mv && bsize < BLOCK_16X16 &&
  1457. frame_mv[NEWMV][ref_frame].as_mv.row == 0 &&
  1458. frame_mv[NEWMV][ref_frame].as_mv.col == 0)
  1459. return -1;
  1460. // Exit NEWMV search if base_mv_sse is large.
  1461. if (sf->base_mv_aggressive && base_mv_sse > (best_sse_sofar << scale))
  1462. return -1;
  1463. if (base_mv_sse < (best_sse_sofar << 1)) {
  1464. // Base layer mv is good.
  1465. // Exit NEWMV search if the base_mv is (0, 0) and sse is low, since
  1466. // (0, 0) mode is already tested.
  1467. unsigned int base_mv_sse_normalized =
  1468. base_mv_sse >>
  1469. (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
  1470. if (sf->base_mv_aggressive && base_mv_sse <= best_sse_sofar &&
  1471. base_mv_sse_normalized < 400 &&
  1472. frame_mv[NEWMV][ref_frame].as_mv.row == 0 &&
  1473. frame_mv[NEWMV][ref_frame].as_mv.col == 0)
  1474. return -1;
  1475. if (!combined_motion_search(cpi, x, bsize, mi_row, mi_col,
  1476. &frame_mv[NEWMV][ref_frame], rate_mv,
  1477. best_rdc->rdcost, 1)) {
  1478. return -1;
  1479. }
  1480. } else if (!combined_motion_search(cpi, x, bsize, mi_row, mi_col,
  1481. &frame_mv[NEWMV][ref_frame], rate_mv,
  1482. best_rdc->rdcost, 0)) {
  1483. return -1;
  1484. }
  1485. } else if (!combined_motion_search(cpi, x, bsize, mi_row, mi_col,
  1486. &frame_mv[NEWMV][ref_frame], rate_mv,
  1487. best_rdc->rdcost, 0)) {
  1488. return -1;
  1489. }
  1490. } else if (!combined_motion_search(cpi, x, bsize, mi_row, mi_col,
  1491. &frame_mv[NEWMV][ref_frame], rate_mv,
  1492. best_rdc->rdcost, 0)) {
  1493. return -1;
  1494. }
  1495. return 0;
  1496. }
  1497. static INLINE void init_best_pickmode(BEST_PICKMODE *bp) {
  1498. bp->best_mode = ZEROMV;
  1499. bp->best_ref_frame = LAST_FRAME;
  1500. bp->best_tx_size = TX_SIZES;
  1501. bp->best_intra_tx_size = TX_SIZES;
  1502. bp->best_pred_filter = EIGHTTAP;
  1503. bp->best_mode_skip_txfm = SKIP_TXFM_NONE;
  1504. bp->best_second_ref_frame = NONE;
  1505. bp->best_pred = NULL;
  1506. }
  1507. void vp9_pick_inter_mode(VP9_COMP *cpi, MACROBLOCK *x, TileDataEnc *tile_data,
  1508. int mi_row, int mi_col, RD_COST *rd_cost,
  1509. BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
  1510. VP9_COMMON *const cm = &cpi->common;
  1511. SPEED_FEATURES *const sf = &cpi->sf;
  1512. SVC *const svc = &cpi->svc;
  1513. MACROBLOCKD *const xd = &x->e_mbd;
  1514. MODE_INFO *const mi = xd->mi[0];
  1515. struct macroblockd_plane *const pd = &xd->plane[0];
  1516. BEST_PICKMODE best_pickmode;
  1517. MV_REFERENCE_FRAME ref_frame;
  1518. MV_REFERENCE_FRAME usable_ref_frame, second_ref_frame;
  1519. int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES];
  1520. uint8_t mode_checked[MB_MODE_COUNT][MAX_REF_FRAMES];
  1521. struct buf_2d yv12_mb[4][MAX_MB_PLANE];
  1522. static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
  1523. VP9_ALT_FLAG };
  1524. RD_COST this_rdc, best_rdc;
  1525. // var_y and sse_y are saved to be used in skipping checking
  1526. unsigned int var_y = UINT_MAX;
  1527. unsigned int sse_y = UINT_MAX;
  1528. const int intra_cost_penalty =
  1529. vp9_get_intra_cost_penalty(cpi, bsize, cm->base_qindex, cm->y_dc_delta_q);
  1530. int64_t inter_mode_thresh =
  1531. RDCOST(x->rdmult, x->rddiv, intra_cost_penalty, 0);
  1532. const int *const rd_threshes = cpi->rd.threshes[mi->segment_id][bsize];
  1533. const int sb_row = mi_row >> MI_BLOCK_SIZE_LOG2;
  1534. int thresh_freq_fact_idx = (sb_row * BLOCK_SIZES + bsize) * MAX_MODES;
  1535. const int *const rd_thresh_freq_fact =
  1536. (cpi->sf.adaptive_rd_thresh_row_mt)
  1537. ? &(tile_data->row_base_thresh_freq_fact[thresh_freq_fact_idx])
  1538. : tile_data->thresh_freq_fact[bsize];
  1539. #if CONFIG_VP9_TEMPORAL_DENOISING
  1540. const int denoise_recheck_zeromv = 1;
  1541. #endif
  1542. INTERP_FILTER filter_ref;
  1543. int pred_filter_search = cm->interp_filter == SWITCHABLE;
  1544. int const_motion[MAX_REF_FRAMES] = { 0 };
  1545. const int bh = num_4x4_blocks_high_lookup[bsize] << 2;
  1546. const int bw = num_4x4_blocks_wide_lookup[bsize] << 2;
  1547. // For speed 6, the result of interp filter is reused later in actual encoding
  1548. // process.
  1549. // tmp[3] points to dst buffer, and the other 3 point to allocated buffers.
  1550. PRED_BUFFER tmp[4];
  1551. DECLARE_ALIGNED(16, uint8_t, pred_buf[3 * 64 * 64]);
  1552. #if CONFIG_VP9_HIGHBITDEPTH
  1553. DECLARE_ALIGNED(16, uint16_t, pred_buf_16[3 * 64 * 64]);
  1554. #endif
  1555. struct buf_2d orig_dst = pd->dst;
  1556. PRED_BUFFER *this_mode_pred = NULL;
  1557. const int pixels_in_block = bh * bw;
  1558. int reuse_inter_pred = cpi->sf.reuse_inter_pred_sby && ctx->pred_pixel_ready;
  1559. int ref_frame_skip_mask = 0;
  1560. int idx;
  1561. int best_pred_sad = INT_MAX;
  1562. int best_early_term = 0;
  1563. int ref_frame_cost[MAX_REF_FRAMES];
  1564. int svc_force_zero_mode[3] = { 0 };
  1565. int perform_intra_pred = 1;
  1566. int use_golden_nonzeromv = 1;
  1567. int force_skip_low_temp_var = 0;
  1568. int skip_ref_find_pred[4] = { 0 };
  1569. unsigned int sse_zeromv_normalized = UINT_MAX;
  1570. unsigned int best_sse_sofar = UINT_MAX;
  1571. int gf_temporal_ref = 0;
  1572. int force_test_gf_zeromv = 0;
  1573. #if CONFIG_VP9_TEMPORAL_DENOISING
  1574. VP9_PICKMODE_CTX_DEN ctx_den;
  1575. int64_t zero_last_cost_orig = INT64_MAX;
  1576. int denoise_svc_pickmode = 1;
  1577. #endif
  1578. INTERP_FILTER filter_gf_svc = EIGHTTAP;
  1579. MV_REFERENCE_FRAME inter_layer_ref = GOLDEN_FRAME;
  1580. const struct segmentation *const seg = &cm->seg;
  1581. int comp_modes = 0;
  1582. int num_inter_modes = (cpi->use_svc) ? RT_INTER_MODES_SVC : RT_INTER_MODES;
  1583. int flag_svc_subpel = 0;
  1584. int svc_mv_col = 0;
  1585. int svc_mv_row = 0;
  1586. int no_scaling = 0;
  1587. int large_block = 0;
  1588. int use_model_yrd_large = 0;
  1589. unsigned int thresh_svc_skip_golden = 500;
  1590. unsigned int thresh_skip_golden = 500;
  1591. int force_smooth_filter = cpi->sf.force_smooth_interpol;
  1592. int scene_change_detected =
  1593. cpi->rc.high_source_sad ||
  1594. (cpi->use_svc && cpi->svc.high_source_sad_superframe);
  1595. init_best_pickmode(&best_pickmode);
  1596. x->encode_breakout = seg->enabled
  1597. ? cpi->segment_encode_breakout[mi->segment_id]
  1598. : cpi->encode_breakout;
  1599. x->source_variance = UINT_MAX;
  1600. if (cpi->sf.default_interp_filter == BILINEAR) {
  1601. best_pickmode.best_pred_filter = BILINEAR;
  1602. filter_gf_svc = BILINEAR;
  1603. }
  1604. if (cpi->use_svc && svc->spatial_layer_id > 0) {
  1605. int layer =
  1606. LAYER_IDS_TO_IDX(svc->spatial_layer_id - 1, svc->temporal_layer_id,
  1607. svc->number_temporal_layers);
  1608. LAYER_CONTEXT *const lc = &svc->layer_context[layer];
  1609. if (lc->scaling_factor_num == lc->scaling_factor_den) no_scaling = 1;
  1610. }
  1611. if (svc->spatial_layer_id > 0 &&
  1612. (svc->high_source_sad_superframe || no_scaling))
  1613. thresh_svc_skip_golden = 0;
  1614. // Lower the skip threshold if lower spatial layer is better quality relative
  1615. // to current layer.
  1616. else if (svc->spatial_layer_id > 0 && cm->base_qindex > 150 &&
  1617. cm->base_qindex > svc->lower_layer_qindex + 15)
  1618. thresh_svc_skip_golden = 100;
  1619. // Increase skip threshold if lower spatial layer is lower quality relative
  1620. // to current layer.
  1621. else if (svc->spatial_layer_id > 0 && cm->base_qindex < 140 &&
  1622. cm->base_qindex < svc->lower_layer_qindex - 20)
  1623. thresh_svc_skip_golden = 1000;
  1624. if (!cpi->use_svc ||
  1625. (svc->use_gf_temporal_ref_current_layer &&
  1626. !svc->layer_context[svc->temporal_layer_id].is_key_frame)) {
  1627. struct scale_factors *const sf_last = &cm->frame_refs[LAST_FRAME - 1].sf;
  1628. struct scale_factors *const sf_golden =
  1629. &cm->frame_refs[GOLDEN_FRAME - 1].sf;
  1630. gf_temporal_ref = 1;
  1631. // For temporal long term prediction, check that the golden reference
  1632. // is same scale as last reference, otherwise disable.
  1633. if ((sf_last->x_scale_fp != sf_golden->x_scale_fp) ||
  1634. (sf_last->y_scale_fp != sf_golden->y_scale_fp)) {
  1635. gf_temporal_ref = 0;
  1636. } else {
  1637. if (cpi->rc.avg_frame_low_motion > 70)
  1638. thresh_svc_skip_golden = 500;
  1639. else
  1640. thresh_svc_skip_golden = 0;
  1641. }
  1642. }
  1643. init_ref_frame_cost(cm, xd, ref_frame_cost);
  1644. memset(&mode_checked[0][0], 0, MB_MODE_COUNT * MAX_REF_FRAMES);
  1645. if (reuse_inter_pred) {
  1646. int i;
  1647. for (i = 0; i < 3; i++) {
  1648. #if CONFIG_VP9_HIGHBITDEPTH
  1649. if (cm->use_highbitdepth)
  1650. tmp[i].data = CONVERT_TO_BYTEPTR(&pred_buf_16[pixels_in_block * i]);
  1651. else
  1652. tmp[i].data = &pred_buf[pixels_in_block * i];
  1653. #else
  1654. tmp[i].data = &pred_buf[pixels_in_block * i];
  1655. #endif // CONFIG_VP9_HIGHBITDEPTH
  1656. tmp[i].stride = bw;
  1657. tmp[i].in_use = 0;
  1658. }
  1659. tmp[3].data = pd->dst.buf;
  1660. tmp[3].stride = pd->dst.stride;
  1661. tmp[3].in_use = 0;
  1662. }
  1663. x->skip_encode = cpi->sf.skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
  1664. x->skip = 0;
  1665. if (cpi->sf.cb_pred_filter_search) {
  1666. const int bsl = mi_width_log2_lookup[bsize];
  1667. pred_filter_search = cm->interp_filter == SWITCHABLE
  1668. ? (((mi_row + mi_col) >> bsl) +
  1669. get_chessboard_index(cm->current_video_frame)) &
  1670. 0x1
  1671. : 0;
  1672. }
  1673. // Instead of using vp9_get_pred_context_switchable_interp(xd) to assign
  1674. // filter_ref, we use a less strict condition on assigning filter_ref.
  1675. // This is to reduce the probabily of entering the flow of not assigning
  1676. // filter_ref and then skip filter search.
  1677. filter_ref = cm->interp_filter;
  1678. if (cpi->sf.default_interp_filter != BILINEAR) {
  1679. if (xd->above_mi && is_inter_block(xd->above_mi))
  1680. filter_ref = xd->above_mi->interp_filter;
  1681. else if (xd->left_mi && is_inter_block(xd->left_mi))
  1682. filter_ref = xd->left_mi->interp_filter;
  1683. }
  1684. // initialize mode decisions
  1685. vp9_rd_cost_reset(&best_rdc);
  1686. vp9_rd_cost_reset(rd_cost);
  1687. mi->sb_type = bsize;
  1688. mi->ref_frame[0] = NONE;
  1689. mi->ref_frame[1] = NONE;
  1690. mi->tx_size =
  1691. VPXMIN(max_txsize_lookup[bsize], tx_mode_to_biggest_tx_size[cm->tx_mode]);
  1692. if (sf->short_circuit_flat_blocks || sf->limit_newmv_early_exit) {
  1693. #if CONFIG_VP9_HIGHBITDEPTH
  1694. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
  1695. x->source_variance = vp9_high_get_sby_perpixel_variance(
  1696. cpi, &x->plane[0].src, bsize, xd->bd);
  1697. else
  1698. #endif // CONFIG_VP9_HIGHBITDEPTH
  1699. x->source_variance =
  1700. vp9_get_sby_perpixel_variance(cpi, &x->plane[0].src, bsize);
  1701. if (cpi->oxcf.content == VP9E_CONTENT_SCREEN &&
  1702. cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && mi->segment_id > 0 &&
  1703. x->zero_temp_sad_source && x->source_variance == 0) {
  1704. mi->segment_id = 0;
  1705. vp9_init_plane_quantizers(cpi, x);
  1706. }
  1707. }
  1708. #if CONFIG_VP9_TEMPORAL_DENOISING
  1709. if (cpi->oxcf.noise_sensitivity > 0) {
  1710. if (cpi->use_svc) denoise_svc_pickmode = vp9_denoise_svc_non_key(cpi);
  1711. if (cpi->denoiser.denoising_level > kDenLowLow && denoise_svc_pickmode)
  1712. vp9_denoiser_reset_frame_stats(ctx);
  1713. }
  1714. #endif
  1715. if (cpi->rc.frames_since_golden == 0 && gf_temporal_ref &&
  1716. !cpi->rc.alt_ref_gf_group && !cpi->rc.last_frame_is_src_altref) {
  1717. usable_ref_frame = LAST_FRAME;
  1718. } else {
  1719. usable_ref_frame = GOLDEN_FRAME;
  1720. }
  1721. if (cpi->oxcf.lag_in_frames > 0 && cpi->oxcf.rc_mode == VPX_VBR) {
  1722. if (cpi->rc.alt_ref_gf_group || cpi->rc.is_src_frame_alt_ref)
  1723. usable_ref_frame = ALTREF_FRAME;
  1724. if (cpi->rc.is_src_frame_alt_ref) {
  1725. skip_ref_find_pred[LAST_FRAME] = 1;
  1726. skip_ref_find_pred[GOLDEN_FRAME] = 1;
  1727. }
  1728. if (!cm->show_frame) {
  1729. if (cpi->rc.frames_since_key == 1) {
  1730. usable_ref_frame = LAST_FRAME;
  1731. skip_ref_find_pred[GOLDEN_FRAME] = 1;
  1732. skip_ref_find_pred[ALTREF_FRAME] = 1;
  1733. }
  1734. }
  1735. }
  1736. // For svc mode, on spatial_layer_id > 0: if the reference has different scale
  1737. // constrain the inter mode to only test zero motion.
  1738. if (cpi->use_svc && svc->force_zero_mode_spatial_ref &&
  1739. svc->spatial_layer_id > 0 && !gf_temporal_ref) {
  1740. if (cpi->ref_frame_flags & flag_list[LAST_FRAME]) {
  1741. struct scale_factors *const sf = &cm->frame_refs[LAST_FRAME - 1].sf;
  1742. if (vp9_is_scaled(sf)) {
  1743. svc_force_zero_mode[LAST_FRAME - 1] = 1;
  1744. inter_layer_ref = LAST_FRAME;
  1745. }
  1746. }
  1747. if (cpi->ref_frame_flags & flag_list[GOLDEN_FRAME]) {
  1748. struct scale_factors *const sf = &cm->frame_refs[GOLDEN_FRAME - 1].sf;
  1749. if (vp9_is_scaled(sf)) {
  1750. svc_force_zero_mode[GOLDEN_FRAME - 1] = 1;
  1751. inter_layer_ref = GOLDEN_FRAME;
  1752. }
  1753. }
  1754. }
  1755. if (cpi->sf.short_circuit_low_temp_var) {
  1756. force_skip_low_temp_var =
  1757. get_force_skip_low_temp_var(&x->variance_low[0], mi_row, mi_col, bsize);
  1758. // If force_skip_low_temp_var is set, and for short circuit mode = 1 and 3,
  1759. // skip golden reference.
  1760. if ((cpi->sf.short_circuit_low_temp_var == 1 ||
  1761. cpi->sf.short_circuit_low_temp_var == 3) &&
  1762. force_skip_low_temp_var) {
  1763. usable_ref_frame = LAST_FRAME;
  1764. }
  1765. }
  1766. if (sf->disable_golden_ref && (x->content_state_sb != kVeryHighSad ||
  1767. cpi->rc.avg_frame_low_motion < 60))
  1768. usable_ref_frame = LAST_FRAME;
  1769. if (!((cpi->ref_frame_flags & flag_list[GOLDEN_FRAME]) &&
  1770. !svc_force_zero_mode[GOLDEN_FRAME - 1] && !force_skip_low_temp_var))
  1771. use_golden_nonzeromv = 0;
  1772. if (cpi->oxcf.speed >= 8 && !cpi->use_svc &&
  1773. ((cpi->rc.frames_since_golden + 1) < x->last_sb_high_content ||
  1774. x->last_sb_high_content > 40 || cpi->rc.frames_since_golden > 120))
  1775. usable_ref_frame = LAST_FRAME;
  1776. // Compound prediction modes: (0,0) on LAST/GOLDEN and ARF.
  1777. if (cm->reference_mode == REFERENCE_MODE_SELECT &&
  1778. cpi->sf.use_compound_nonrd_pickmode && usable_ref_frame == ALTREF_FRAME)
  1779. comp_modes = 2;
  1780. // If the segment reference frame feature is enabled and it's set to GOLDEN
  1781. // reference, then make sure we don't skip checking GOLDEN, this is to
  1782. // prevent possibility of not picking any mode.
  1783. if (segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME) &&
  1784. get_segdata(seg, mi->segment_id, SEG_LVL_REF_FRAME) == GOLDEN_FRAME) {
  1785. usable_ref_frame = GOLDEN_FRAME;
  1786. skip_ref_find_pred[GOLDEN_FRAME] = 0;
  1787. thresh_svc_skip_golden = 0;
  1788. }
  1789. for (ref_frame = LAST_FRAME; ref_frame <= usable_ref_frame; ++ref_frame) {
  1790. // Skip find_predictor if the reference frame is not in the
  1791. // ref_frame_flags (i.e., not used as a reference for this frame).
  1792. skip_ref_find_pred[ref_frame] =
  1793. !(cpi->ref_frame_flags & flag_list[ref_frame]);
  1794. if (!skip_ref_find_pred[ref_frame]) {
  1795. find_predictors(cpi, x, ref_frame, frame_mv, const_motion,
  1796. &ref_frame_skip_mask, flag_list, tile_data, mi_row,
  1797. mi_col, yv12_mb, bsize, force_skip_low_temp_var,
  1798. comp_modes > 0);
  1799. }
  1800. }
  1801. if (cpi->use_svc || cpi->oxcf.speed <= 7 || bsize < BLOCK_32X32)
  1802. x->sb_use_mv_part = 0;
  1803. // Set the flag_svc_subpel to 1 for SVC if the lower spatial layer used
  1804. // an averaging filter for downsampling (phase = 8). If so, we will test
  1805. // a nonzero motion mode on the spatial reference.
  1806. // The nonzero motion is half pixel shifted to left and top (-4, -4).
  1807. if (cpi->use_svc && svc->spatial_layer_id > 0 &&
  1808. svc_force_zero_mode[inter_layer_ref - 1] &&
  1809. svc->downsample_filter_phase[svc->spatial_layer_id - 1] == 8 &&
  1810. !gf_temporal_ref) {
  1811. svc_mv_col = -4;
  1812. svc_mv_row = -4;
  1813. flag_svc_subpel = 1;
  1814. }
  1815. // For SVC with quality layers, when QP of lower layer is lower
  1816. // than current layer: force check of GF-ZEROMV before early exit
  1817. // due to skip flag.
  1818. if (svc->spatial_layer_id > 0 && no_scaling &&
  1819. (cpi->ref_frame_flags & flag_list[GOLDEN_FRAME]) &&
  1820. cm->base_qindex > svc->lower_layer_qindex + 10)
  1821. force_test_gf_zeromv = 1;
  1822. // For low motion content use x->sb_is_skin in addition to VeryHighSad
  1823. // for setting large_block.
  1824. large_block = (x->content_state_sb == kVeryHighSad ||
  1825. (x->sb_is_skin && cpi->rc.avg_frame_low_motion > 70) ||
  1826. cpi->oxcf.speed < 7)
  1827. ? bsize > BLOCK_32X32
  1828. : bsize >= BLOCK_32X32;
  1829. use_model_yrd_large =
  1830. cpi->oxcf.rc_mode == VPX_CBR && large_block &&
  1831. !cyclic_refresh_segment_id_boosted(xd->mi[0]->segment_id) &&
  1832. cm->base_qindex;
  1833. for (idx = 0; idx < num_inter_modes + comp_modes; ++idx) {
  1834. int rate_mv = 0;
  1835. int mode_rd_thresh;
  1836. int mode_index;
  1837. int i;
  1838. int64_t this_sse;
  1839. int is_skippable;
  1840. int this_early_term = 0;
  1841. int rd_computed = 0;
  1842. int flag_preduv_computed[2] = { 0 };
  1843. int inter_mv_mode = 0;
  1844. int skip_this_mv = 0;
  1845. int comp_pred = 0;
  1846. int force_mv_inter_layer = 0;
  1847. PREDICTION_MODE this_mode;
  1848. second_ref_frame = NONE;
  1849. if (idx < num_inter_modes) {
  1850. this_mode = ref_mode_set[idx].pred_mode;
  1851. ref_frame = ref_mode_set[idx].ref_frame;
  1852. if (cpi->use_svc) {
  1853. this_mode = ref_mode_set_svc[idx].pred_mode;
  1854. ref_frame = ref_mode_set_svc[idx].ref_frame;
  1855. }
  1856. } else {
  1857. // Add (0,0) compound modes.
  1858. this_mode = ZEROMV;
  1859. ref_frame = LAST_FRAME;
  1860. if (idx == num_inter_modes + comp_modes - 1) ref_frame = GOLDEN_FRAME;
  1861. second_ref_frame = ALTREF_FRAME;
  1862. comp_pred = 1;
  1863. }
  1864. if (ref_frame > usable_ref_frame) continue;
  1865. if (skip_ref_find_pred[ref_frame]) continue;
  1866. if (svc->previous_frame_is_intra_only) {
  1867. if (ref_frame != LAST_FRAME || frame_mv[this_mode][ref_frame].as_int != 0)
  1868. continue;
  1869. }
  1870. // If the segment reference frame feature is enabled then do nothing if the
  1871. // current ref frame is not allowed.
  1872. if (segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME) &&
  1873. get_segdata(seg, mi->segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame)
  1874. continue;
  1875. if (flag_svc_subpel && ref_frame == inter_layer_ref) {
  1876. force_mv_inter_layer = 1;
  1877. // Only test mode if NEARESTMV/NEARMV is (svc_mv_col, svc_mv_row),
  1878. // otherwise set NEWMV to (svc_mv_col, svc_mv_row).
  1879. if (this_mode == NEWMV) {
  1880. frame_mv[this_mode][ref_frame].as_mv.col = svc_mv_col;
  1881. frame_mv[this_mode][ref_frame].as_mv.row = svc_mv_row;
  1882. } else if (frame_mv[this_mode][ref_frame].as_mv.col != svc_mv_col ||
  1883. frame_mv[this_mode][ref_frame].as_mv.row != svc_mv_row) {
  1884. continue;
  1885. }
  1886. }
  1887. if (comp_pred) {
  1888. if (!cpi->allow_comp_inter_inter) continue;
  1889. // Skip compound inter modes if ARF is not available.
  1890. if (!(cpi->ref_frame_flags & flag_list[second_ref_frame])) continue;
  1891. // Do not allow compound prediction if the segment level reference frame
  1892. // feature is in use as in this case there can only be one reference.
  1893. if (segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME)) continue;
  1894. }
  1895. // For CBR mode: skip the golden reference search if sse of zeromv_last is
  1896. // below threshold.
  1897. if (ref_frame == GOLDEN_FRAME && cpi->oxcf.rc_mode == VPX_CBR &&
  1898. ((cpi->use_svc && sse_zeromv_normalized < thresh_svc_skip_golden) ||
  1899. (!cpi->use_svc && sse_zeromv_normalized < thresh_skip_golden)))
  1900. continue;
  1901. if (!(cpi->ref_frame_flags & flag_list[ref_frame])) continue;
  1902. // For screen content. If zero_temp_sad source is computed: skip
  1903. // non-zero motion check for stationary blocks. If the superblock is
  1904. // non-stationary then for flat blocks skip the zero last check (keep golden
  1905. // as it may be inter-layer reference). Otherwise (if zero_temp_sad_source
  1906. // is not computed) skip non-zero motion check for flat blocks.
  1907. // TODO(marpan): Compute zero_temp_sad_source per coding block.
  1908. if (cpi->oxcf.content == VP9E_CONTENT_SCREEN) {
  1909. if (cpi->compute_source_sad_onepass && cpi->sf.use_source_sad) {
  1910. if ((frame_mv[this_mode][ref_frame].as_int != 0 &&
  1911. x->zero_temp_sad_source) ||
  1912. (frame_mv[this_mode][ref_frame].as_int == 0 &&
  1913. x->source_variance == 0 && ref_frame == LAST_FRAME &&
  1914. !x->zero_temp_sad_source))
  1915. continue;
  1916. } else if (frame_mv[this_mode][ref_frame].as_int != 0 &&
  1917. x->source_variance == 0) {
  1918. continue;
  1919. }
  1920. }
  1921. if (!(cpi->sf.inter_mode_mask[bsize] & (1 << this_mode))) continue;
  1922. if (cpi->oxcf.lag_in_frames > 0 && cpi->oxcf.rc_mode == VPX_VBR) {
  1923. if (cpi->rc.is_src_frame_alt_ref &&
  1924. (ref_frame != ALTREF_FRAME ||
  1925. frame_mv[this_mode][ref_frame].as_int != 0))
  1926. continue;
  1927. if (!cm->show_frame && ref_frame == ALTREF_FRAME &&
  1928. frame_mv[this_mode][ref_frame].as_int != 0)
  1929. continue;
  1930. if (cpi->rc.alt_ref_gf_group && cm->show_frame &&
  1931. cpi->rc.frames_since_golden > (cpi->rc.baseline_gf_interval >> 1) &&
  1932. ref_frame == GOLDEN_FRAME &&
  1933. frame_mv[this_mode][ref_frame].as_int != 0)
  1934. continue;
  1935. if (cpi->rc.alt_ref_gf_group && cm->show_frame &&
  1936. cpi->rc.frames_since_golden > 0 &&
  1937. cpi->rc.frames_since_golden < (cpi->rc.baseline_gf_interval >> 1) &&
  1938. ref_frame == ALTREF_FRAME &&
  1939. frame_mv[this_mode][ref_frame].as_int != 0)
  1940. continue;
  1941. }
  1942. if (const_motion[ref_frame] && this_mode == NEARMV) continue;
  1943. // Skip non-zeromv mode search for golden frame if force_skip_low_temp_var
  1944. // is set. If nearestmv for golden frame is 0, zeromv mode will be skipped
  1945. // later.
  1946. if (!force_mv_inter_layer && force_skip_low_temp_var &&
  1947. ref_frame == GOLDEN_FRAME &&
  1948. frame_mv[this_mode][ref_frame].as_int != 0) {
  1949. continue;
  1950. }
  1951. if (x->content_state_sb != kVeryHighSad &&
  1952. (cpi->sf.short_circuit_low_temp_var >= 2 ||
  1953. (cpi->sf.short_circuit_low_temp_var == 1 && bsize == BLOCK_64X64)) &&
  1954. force_skip_low_temp_var && ref_frame == LAST_FRAME &&
  1955. this_mode == NEWMV) {
  1956. continue;
  1957. }
  1958. if (cpi->use_svc) {
  1959. if (!force_mv_inter_layer && svc_force_zero_mode[ref_frame - 1] &&
  1960. frame_mv[this_mode][ref_frame].as_int != 0)
  1961. continue;
  1962. }
  1963. // Disable this drop out case if the ref frame segment level feature is
  1964. // enabled for this segment. This is to prevent the possibility that we end
  1965. // up unable to pick any mode.
  1966. if (!segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME)) {
  1967. if (sf->reference_masking &&
  1968. !(frame_mv[this_mode][ref_frame].as_int == 0 &&
  1969. ref_frame == LAST_FRAME)) {
  1970. if (usable_ref_frame < ALTREF_FRAME) {
  1971. if (!force_skip_low_temp_var && usable_ref_frame > LAST_FRAME) {
  1972. i = (ref_frame == LAST_FRAME) ? GOLDEN_FRAME : LAST_FRAME;
  1973. if ((cpi->ref_frame_flags & flag_list[i]))
  1974. if (x->pred_mv_sad[ref_frame] > (x->pred_mv_sad[i] << 1))
  1975. ref_frame_skip_mask |= (1 << ref_frame);
  1976. }
  1977. } else if (!cpi->rc.is_src_frame_alt_ref &&
  1978. !(frame_mv[this_mode][ref_frame].as_int == 0 &&
  1979. ref_frame == ALTREF_FRAME)) {
  1980. int ref1 = (ref_frame == GOLDEN_FRAME) ? LAST_FRAME : GOLDEN_FRAME;
  1981. int ref2 = (ref_frame == ALTREF_FRAME) ? LAST_FRAME : ALTREF_FRAME;
  1982. if (((cpi->ref_frame_flags & flag_list[ref1]) &&
  1983. (x->pred_mv_sad[ref_frame] > (x->pred_mv_sad[ref1] << 1))) ||
  1984. ((cpi->ref_frame_flags & flag_list[ref2]) &&
  1985. (x->pred_mv_sad[ref_frame] > (x->pred_mv_sad[ref2] << 1))))
  1986. ref_frame_skip_mask |= (1 << ref_frame);
  1987. }
  1988. }
  1989. if (ref_frame_skip_mask & (1 << ref_frame)) continue;
  1990. }
  1991. // Select prediction reference frames.
  1992. for (i = 0; i < MAX_MB_PLANE; i++) {
  1993. xd->plane[i].pre[0] = yv12_mb[ref_frame][i];
  1994. if (comp_pred) xd->plane[i].pre[1] = yv12_mb[second_ref_frame][i];
  1995. }
  1996. mi->ref_frame[0] = ref_frame;
  1997. mi->ref_frame[1] = second_ref_frame;
  1998. set_ref_ptrs(cm, xd, ref_frame, second_ref_frame);
  1999. mode_index = mode_idx[ref_frame][INTER_OFFSET(this_mode)];
  2000. mode_rd_thresh = best_pickmode.best_mode_skip_txfm
  2001. ? rd_threshes[mode_index] << 1
  2002. : rd_threshes[mode_index];
  2003. // Increase mode_rd_thresh value for GOLDEN_FRAME for improved encoding
  2004. // speed with little/no subjective quality loss.
  2005. if (cpi->sf.bias_golden && ref_frame == GOLDEN_FRAME &&
  2006. cpi->rc.frames_since_golden > 4)
  2007. mode_rd_thresh = mode_rd_thresh << 3;
  2008. if ((cpi->sf.adaptive_rd_thresh_row_mt &&
  2009. rd_less_than_thresh_row_mt(best_rdc.rdcost, mode_rd_thresh,
  2010. &rd_thresh_freq_fact[mode_index])) ||
  2011. (!cpi->sf.adaptive_rd_thresh_row_mt &&
  2012. rd_less_than_thresh(best_rdc.rdcost, mode_rd_thresh,
  2013. &rd_thresh_freq_fact[mode_index])))
  2014. if (frame_mv[this_mode][ref_frame].as_int != 0) continue;
  2015. if (this_mode == NEWMV && !force_mv_inter_layer) {
  2016. if (search_new_mv(cpi, x, frame_mv, ref_frame, gf_temporal_ref, bsize,
  2017. mi_row, mi_col, best_pred_sad, &rate_mv, best_sse_sofar,
  2018. &best_rdc))
  2019. continue;
  2020. }
  2021. // TODO(jianj): Skipping the testing of (duplicate) non-zero motion vector
  2022. // causes some regression, leave it for duplicate zero-mv for now, until
  2023. // regression issue is resolved.
  2024. for (inter_mv_mode = NEARESTMV; inter_mv_mode <= NEWMV; inter_mv_mode++) {
  2025. if (inter_mv_mode == this_mode || comp_pred) continue;
  2026. if (mode_checked[inter_mv_mode][ref_frame] &&
  2027. frame_mv[this_mode][ref_frame].as_int ==
  2028. frame_mv[inter_mv_mode][ref_frame].as_int &&
  2029. frame_mv[inter_mv_mode][ref_frame].as_int == 0) {
  2030. skip_this_mv = 1;
  2031. break;
  2032. }
  2033. }
  2034. if (skip_this_mv) continue;
  2035. // If use_golden_nonzeromv is false, NEWMV mode is skipped for golden, no
  2036. // need to compute best_pred_sad which is only used to skip golden NEWMV.
  2037. if (use_golden_nonzeromv && this_mode == NEWMV && ref_frame == LAST_FRAME &&
  2038. frame_mv[NEWMV][LAST_FRAME].as_int != INVALID_MV) {
  2039. const int pre_stride = xd->plane[0].pre[0].stride;
  2040. const uint8_t *const pre_buf =
  2041. xd->plane[0].pre[0].buf +
  2042. (frame_mv[NEWMV][LAST_FRAME].as_mv.row >> 3) * pre_stride +
  2043. (frame_mv[NEWMV][LAST_FRAME].as_mv.col >> 3);
  2044. best_pred_sad = cpi->fn_ptr[bsize].sdf(
  2045. x->plane[0].src.buf, x->plane[0].src.stride, pre_buf, pre_stride);
  2046. x->pred_mv_sad[LAST_FRAME] = best_pred_sad;
  2047. }
  2048. if (this_mode != NEARESTMV && !comp_pred &&
  2049. frame_mv[this_mode][ref_frame].as_int ==
  2050. frame_mv[NEARESTMV][ref_frame].as_int)
  2051. continue;
  2052. mi->mode = this_mode;
  2053. mi->mv[0].as_int = frame_mv[this_mode][ref_frame].as_int;
  2054. mi->mv[1].as_int = 0;
  2055. // Search for the best prediction filter type, when the resulting
  2056. // motion vector is at sub-pixel accuracy level for luma component, i.e.,
  2057. // the last three bits are all zeros.
  2058. if (reuse_inter_pred) {
  2059. if (!this_mode_pred) {
  2060. this_mode_pred = &tmp[3];
  2061. } else {
  2062. this_mode_pred = &tmp[get_pred_buffer(tmp, 3)];
  2063. pd->dst.buf = this_mode_pred->data;
  2064. pd->dst.stride = bw;
  2065. }
  2066. }
  2067. if ((this_mode == NEWMV || filter_ref == SWITCHABLE) &&
  2068. pred_filter_search &&
  2069. (ref_frame == LAST_FRAME ||
  2070. (ref_frame == GOLDEN_FRAME && !force_mv_inter_layer &&
  2071. (cpi->use_svc || cpi->oxcf.rc_mode == VPX_VBR))) &&
  2072. (((mi->mv[0].as_mv.row | mi->mv[0].as_mv.col) & 0x07) != 0)) {
  2073. rd_computed = 1;
  2074. search_filter_ref(cpi, x, &this_rdc, mi_row, mi_col, tmp, bsize,
  2075. reuse_inter_pred, &this_mode_pred, &var_y, &sse_y,
  2076. force_smooth_filter, &this_early_term,
  2077. flag_preduv_computed, use_model_yrd_large);
  2078. } else {
  2079. mi->interp_filter = (filter_ref == SWITCHABLE) ? EIGHTTAP : filter_ref;
  2080. if (cpi->use_svc && ref_frame == GOLDEN_FRAME &&
  2081. svc_force_zero_mode[ref_frame - 1])
  2082. mi->interp_filter = filter_gf_svc;
  2083. vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
  2084. // For large partition blocks, extra testing is done.
  2085. if (use_model_yrd_large) {
  2086. rd_computed = 1;
  2087. model_rd_for_sb_y_large(cpi, bsize, x, xd, &this_rdc.rate,
  2088. &this_rdc.dist, &var_y, &sse_y, mi_row, mi_col,
  2089. &this_early_term, flag_preduv_computed);
  2090. } else {
  2091. rd_computed = 1;
  2092. model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc.rate, &this_rdc.dist,
  2093. &var_y, &sse_y, 0);
  2094. }
  2095. // Save normalized sse (between current and last frame) for (0, 0) motion.
  2096. if (ref_frame == LAST_FRAME &&
  2097. frame_mv[this_mode][ref_frame].as_int == 0) {
  2098. sse_zeromv_normalized =
  2099. sse_y >> (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
  2100. }
  2101. if (sse_y < best_sse_sofar) best_sse_sofar = sse_y;
  2102. }
  2103. if (!this_early_term) {
  2104. this_sse = (int64_t)sse_y;
  2105. block_yrd(cpi, x, &this_rdc, &is_skippable, &this_sse, bsize,
  2106. VPXMIN(mi->tx_size, TX_16X16), rd_computed, 0);
  2107. x->skip_txfm[0] = is_skippable;
  2108. if (is_skippable) {
  2109. this_rdc.rate = vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
  2110. } else {
  2111. if (RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist) <
  2112. RDCOST(x->rdmult, x->rddiv, 0, this_sse)) {
  2113. this_rdc.rate += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 0);
  2114. } else {
  2115. this_rdc.rate = vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
  2116. this_rdc.dist = this_sse;
  2117. x->skip_txfm[0] = SKIP_TXFM_AC_DC;
  2118. }
  2119. }
  2120. if (cm->interp_filter == SWITCHABLE) {
  2121. if ((mi->mv[0].as_mv.row | mi->mv[0].as_mv.col) & 0x07)
  2122. this_rdc.rate += vp9_get_switchable_rate(cpi, xd);
  2123. }
  2124. } else {
  2125. if (cm->interp_filter == SWITCHABLE) {
  2126. if ((mi->mv[0].as_mv.row | mi->mv[0].as_mv.col) & 0x07)
  2127. this_rdc.rate += vp9_get_switchable_rate(cpi, xd);
  2128. }
  2129. this_rdc.rate += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
  2130. }
  2131. if (!this_early_term &&
  2132. (x->color_sensitivity[0] || x->color_sensitivity[1])) {
  2133. RD_COST rdc_uv;
  2134. const BLOCK_SIZE uv_bsize = get_plane_block_size(bsize, &xd->plane[1]);
  2135. if (x->color_sensitivity[0] && !flag_preduv_computed[0]) {
  2136. vp9_build_inter_predictors_sbp(xd, mi_row, mi_col, bsize, 1);
  2137. flag_preduv_computed[0] = 1;
  2138. }
  2139. if (x->color_sensitivity[1] && !flag_preduv_computed[1]) {
  2140. vp9_build_inter_predictors_sbp(xd, mi_row, mi_col, bsize, 2);
  2141. flag_preduv_computed[1] = 1;
  2142. }
  2143. model_rd_for_sb_uv(cpi, uv_bsize, x, xd, &rdc_uv, &var_y, &sse_y, 1, 2);
  2144. this_rdc.rate += rdc_uv.rate;
  2145. this_rdc.dist += rdc_uv.dist;
  2146. }
  2147. this_rdc.rate += rate_mv;
  2148. this_rdc.rate += cpi->inter_mode_cost[x->mbmi_ext->mode_context[ref_frame]]
  2149. [INTER_OFFSET(this_mode)];
  2150. // TODO(marpan): Add costing for compound mode.
  2151. this_rdc.rate += ref_frame_cost[ref_frame];
  2152. this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist);
  2153. // Bias against NEWMV that is very different from its neighbors, and bias
  2154. // to small motion-lastref for noisy input.
  2155. if (cpi->oxcf.rc_mode == VPX_CBR && cpi->oxcf.speed >= 5 &&
  2156. cpi->oxcf.content != VP9E_CONTENT_SCREEN) {
  2157. vp9_NEWMV_diff_bias(&cpi->noise_estimate, xd, this_mode, &this_rdc, bsize,
  2158. frame_mv[this_mode][ref_frame].as_mv.row,
  2159. frame_mv[this_mode][ref_frame].as_mv.col,
  2160. ref_frame == LAST_FRAME, x->lowvar_highsumdiff,
  2161. x->sb_is_skin);
  2162. }
  2163. // Skipping checking: test to see if this block can be reconstructed by
  2164. // prediction only.
  2165. if (cpi->allow_encode_breakout && !xd->lossless && !scene_change_detected &&
  2166. !svc->high_num_blocks_with_motion) {
  2167. encode_breakout_test(cpi, x, bsize, mi_row, mi_col, ref_frame, this_mode,
  2168. var_y, sse_y, yv12_mb, &this_rdc.rate,
  2169. &this_rdc.dist, flag_preduv_computed);
  2170. if (x->skip) {
  2171. this_rdc.rate += rate_mv;
  2172. this_rdc.rdcost =
  2173. RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist);
  2174. }
  2175. }
  2176. // On spatially flat blocks for screne content: bias against zero-last
  2177. // if the sse_y is non-zero. Only on scene change or high motion frames.
  2178. if (cpi->oxcf.content == VP9E_CONTENT_SCREEN &&
  2179. (scene_change_detected || svc->high_num_blocks_with_motion) &&
  2180. ref_frame == LAST_FRAME && frame_mv[this_mode][ref_frame].as_int == 0 &&
  2181. svc->spatial_layer_id == 0 && x->source_variance == 0 && sse_y > 0) {
  2182. this_rdc.rdcost = this_rdc.rdcost << 2;
  2183. }
  2184. #if CONFIG_VP9_TEMPORAL_DENOISING
  2185. if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc_pickmode &&
  2186. cpi->denoiser.denoising_level > kDenLowLow) {
  2187. vp9_denoiser_update_frame_stats(mi, sse_y, this_mode, ctx);
  2188. // Keep track of zero_last cost.
  2189. if (ref_frame == LAST_FRAME && frame_mv[this_mode][ref_frame].as_int == 0)
  2190. zero_last_cost_orig = this_rdc.rdcost;
  2191. }
  2192. #else
  2193. (void)ctx;
  2194. #endif
  2195. mode_checked[this_mode][ref_frame] = 1;
  2196. if (this_rdc.rdcost < best_rdc.rdcost || x->skip) {
  2197. best_rdc = this_rdc;
  2198. best_early_term = this_early_term;
  2199. best_pickmode.best_mode = this_mode;
  2200. best_pickmode.best_pred_filter = mi->interp_filter;
  2201. best_pickmode.best_tx_size = mi->tx_size;
  2202. best_pickmode.best_ref_frame = ref_frame;
  2203. best_pickmode.best_mode_skip_txfm = x->skip_txfm[0];
  2204. best_pickmode.best_second_ref_frame = second_ref_frame;
  2205. if (reuse_inter_pred) {
  2206. free_pred_buffer(best_pickmode.best_pred);
  2207. best_pickmode.best_pred = this_mode_pred;
  2208. }
  2209. } else {
  2210. if (reuse_inter_pred) free_pred_buffer(this_mode_pred);
  2211. }
  2212. if (x->skip &&
  2213. (!force_test_gf_zeromv || mode_checked[ZEROMV][GOLDEN_FRAME]))
  2214. break;
  2215. // If early termination flag is 1 and at least 2 modes are checked,
  2216. // the mode search is terminated.
  2217. if (best_early_term && idx > 0 && !scene_change_detected &&
  2218. (!force_test_gf_zeromv || mode_checked[ZEROMV][GOLDEN_FRAME])) {
  2219. x->skip = 1;
  2220. break;
  2221. }
  2222. }
  2223. mi->mode = best_pickmode.best_mode;
  2224. mi->interp_filter = best_pickmode.best_pred_filter;
  2225. mi->tx_size = best_pickmode.best_tx_size;
  2226. mi->ref_frame[0] = best_pickmode.best_ref_frame;
  2227. mi->mv[0].as_int =
  2228. frame_mv[best_pickmode.best_mode][best_pickmode.best_ref_frame].as_int;
  2229. xd->mi[0]->bmi[0].as_mv[0].as_int = mi->mv[0].as_int;
  2230. x->skip_txfm[0] = best_pickmode.best_mode_skip_txfm;
  2231. mi->ref_frame[1] = best_pickmode.best_second_ref_frame;
  2232. // For spatial enhancemanent layer: perform intra prediction only if base
  2233. // layer is chosen as the reference. Always perform intra prediction if
  2234. // LAST is the only reference, or is_key_frame is set, or on base
  2235. // temporal layer.
  2236. if (svc->spatial_layer_id && !gf_temporal_ref) {
  2237. perform_intra_pred =
  2238. svc->temporal_layer_id == 0 ||
  2239. svc->layer_context[svc->temporal_layer_id].is_key_frame ||
  2240. !(cpi->ref_frame_flags & flag_list[GOLDEN_FRAME]) ||
  2241. (!svc->layer_context[svc->temporal_layer_id].is_key_frame &&
  2242. svc_force_zero_mode[best_pickmode.best_ref_frame - 1]);
  2243. inter_mode_thresh = (inter_mode_thresh << 1) + inter_mode_thresh;
  2244. }
  2245. if ((cpi->oxcf.lag_in_frames > 0 && cpi->oxcf.rc_mode == VPX_VBR &&
  2246. cpi->rc.is_src_frame_alt_ref) ||
  2247. svc->previous_frame_is_intra_only)
  2248. perform_intra_pred = 0;
  2249. // If the segment reference frame feature is enabled and set then
  2250. // skip the intra prediction.
  2251. if (segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME) &&
  2252. get_segdata(seg, mi->segment_id, SEG_LVL_REF_FRAME) > 0)
  2253. perform_intra_pred = 0;
  2254. // Perform intra prediction search, if the best SAD is above a certain
  2255. // threshold.
  2256. if (best_rdc.rdcost == INT64_MAX ||
  2257. (cpi->oxcf.content == VP9E_CONTENT_SCREEN && x->source_variance == 0) ||
  2258. (scene_change_detected && perform_intra_pred) ||
  2259. ((!force_skip_low_temp_var || bsize < BLOCK_32X32 ||
  2260. x->content_state_sb == kVeryHighSad) &&
  2261. perform_intra_pred && !x->skip && best_rdc.rdcost > inter_mode_thresh &&
  2262. bsize <= cpi->sf.max_intra_bsize && !x->skip_low_source_sad &&
  2263. !x->lowvar_highsumdiff)) {
  2264. struct estimate_block_intra_args args = { cpi, x, DC_PRED, 1, 0 };
  2265. int64_t this_sse = INT64_MAX;
  2266. int i;
  2267. PRED_BUFFER *const best_pred = best_pickmode.best_pred;
  2268. TX_SIZE intra_tx_size =
  2269. VPXMIN(max_txsize_lookup[bsize],
  2270. tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
  2271. if (reuse_inter_pred && best_pred != NULL) {
  2272. if (best_pred->data == orig_dst.buf) {
  2273. this_mode_pred = &tmp[get_pred_buffer(tmp, 3)];
  2274. #if CONFIG_VP9_HIGHBITDEPTH
  2275. if (cm->use_highbitdepth)
  2276. vpx_highbd_convolve_copy(
  2277. CONVERT_TO_SHORTPTR(best_pred->data), best_pred->stride,
  2278. CONVERT_TO_SHORTPTR(this_mode_pred->data), this_mode_pred->stride,
  2279. NULL, 0, 0, 0, 0, bw, bh, xd->bd);
  2280. else
  2281. vpx_convolve_copy(best_pred->data, best_pred->stride,
  2282. this_mode_pred->data, this_mode_pred->stride, NULL,
  2283. 0, 0, 0, 0, bw, bh);
  2284. #else
  2285. vpx_convolve_copy(best_pred->data, best_pred->stride,
  2286. this_mode_pred->data, this_mode_pred->stride, NULL, 0,
  2287. 0, 0, 0, bw, bh);
  2288. #endif // CONFIG_VP9_HIGHBITDEPTH
  2289. best_pickmode.best_pred = this_mode_pred;
  2290. }
  2291. }
  2292. pd->dst = orig_dst;
  2293. for (i = 0; i < 4; ++i) {
  2294. const PREDICTION_MODE this_mode = intra_mode_list[i];
  2295. THR_MODES mode_index = mode_idx[INTRA_FRAME][mode_offset(this_mode)];
  2296. int mode_rd_thresh = rd_threshes[mode_index];
  2297. // For spatially flat blocks, under short_circuit_flat_blocks flag:
  2298. // only check DC mode for stationary blocks, otherwise also check
  2299. // H and V mode.
  2300. if (sf->short_circuit_flat_blocks && x->source_variance == 0 &&
  2301. ((x->zero_temp_sad_source && this_mode != DC_PRED) || i > 2)) {
  2302. continue;
  2303. }
  2304. if (!((1 << this_mode) & cpi->sf.intra_y_mode_bsize_mask[bsize]))
  2305. continue;
  2306. if ((cpi->sf.adaptive_rd_thresh_row_mt &&
  2307. rd_less_than_thresh_row_mt(best_rdc.rdcost, mode_rd_thresh,
  2308. &rd_thresh_freq_fact[mode_index])) ||
  2309. (!cpi->sf.adaptive_rd_thresh_row_mt &&
  2310. rd_less_than_thresh(best_rdc.rdcost, mode_rd_thresh,
  2311. &rd_thresh_freq_fact[mode_index]))) {
  2312. // Avoid this early exit for screen on base layer, for scene
  2313. // changes or high motion frames.
  2314. if (cpi->oxcf.content != VP9E_CONTENT_SCREEN ||
  2315. svc->spatial_layer_id > 0 ||
  2316. (!scene_change_detected && !svc->high_num_blocks_with_motion))
  2317. continue;
  2318. }
  2319. mi->mode = this_mode;
  2320. mi->ref_frame[0] = INTRA_FRAME;
  2321. this_rdc.dist = this_rdc.rate = 0;
  2322. args.mode = this_mode;
  2323. args.skippable = 1;
  2324. args.rdc = &this_rdc;
  2325. mi->tx_size = intra_tx_size;
  2326. compute_intra_yprediction(this_mode, bsize, x, xd);
  2327. model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc.rate, &this_rdc.dist,
  2328. &var_y, &sse_y, 1);
  2329. block_yrd(cpi, x, &this_rdc, &args.skippable, &this_sse, bsize,
  2330. VPXMIN(mi->tx_size, TX_16X16), 1, 1);
  2331. // Check skip cost here since skippable is not set for for uv, this
  2332. // mirrors the behavior used by inter
  2333. if (args.skippable) {
  2334. x->skip_txfm[0] = SKIP_TXFM_AC_DC;
  2335. this_rdc.rate = vp9_cost_bit(vp9_get_skip_prob(&cpi->common, xd), 1);
  2336. } else {
  2337. x->skip_txfm[0] = SKIP_TXFM_NONE;
  2338. this_rdc.rate += vp9_cost_bit(vp9_get_skip_prob(&cpi->common, xd), 0);
  2339. }
  2340. // Inter and intra RD will mismatch in scale for non-screen content.
  2341. if (cpi->oxcf.content == VP9E_CONTENT_SCREEN) {
  2342. if (x->color_sensitivity[0])
  2343. vp9_foreach_transformed_block_in_plane(xd, bsize, 1,
  2344. estimate_block_intra, &args);
  2345. if (x->color_sensitivity[1])
  2346. vp9_foreach_transformed_block_in_plane(xd, bsize, 2,
  2347. estimate_block_intra, &args);
  2348. }
  2349. this_rdc.rate += cpi->mbmode_cost[this_mode];
  2350. this_rdc.rate += ref_frame_cost[INTRA_FRAME];
  2351. this_rdc.rate += intra_cost_penalty;
  2352. this_rdc.rdcost =
  2353. RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist);
  2354. if (this_rdc.rdcost < best_rdc.rdcost) {
  2355. best_rdc = this_rdc;
  2356. best_pickmode.best_mode = this_mode;
  2357. best_pickmode.best_intra_tx_size = mi->tx_size;
  2358. best_pickmode.best_ref_frame = INTRA_FRAME;
  2359. best_pickmode.best_second_ref_frame = NONE;
  2360. mi->uv_mode = this_mode;
  2361. mi->mv[0].as_int = INVALID_MV;
  2362. mi->mv[1].as_int = INVALID_MV;
  2363. best_pickmode.best_mode_skip_txfm = x->skip_txfm[0];
  2364. }
  2365. }
  2366. // Reset mb_mode_info to the best inter mode.
  2367. if (best_pickmode.best_ref_frame != INTRA_FRAME) {
  2368. mi->tx_size = best_pickmode.best_tx_size;
  2369. } else {
  2370. mi->tx_size = best_pickmode.best_intra_tx_size;
  2371. }
  2372. }
  2373. pd->dst = orig_dst;
  2374. mi->mode = best_pickmode.best_mode;
  2375. mi->ref_frame[0] = best_pickmode.best_ref_frame;
  2376. mi->ref_frame[1] = best_pickmode.best_second_ref_frame;
  2377. x->skip_txfm[0] = best_pickmode.best_mode_skip_txfm;
  2378. if (!is_inter_block(mi)) {
  2379. mi->interp_filter = SWITCHABLE_FILTERS;
  2380. }
  2381. if (reuse_inter_pred && best_pickmode.best_pred != NULL) {
  2382. PRED_BUFFER *const best_pred = best_pickmode.best_pred;
  2383. if (best_pred->data != orig_dst.buf && is_inter_mode(mi->mode)) {
  2384. #if CONFIG_VP9_HIGHBITDEPTH
  2385. if (cm->use_highbitdepth)
  2386. vpx_highbd_convolve_copy(
  2387. CONVERT_TO_SHORTPTR(best_pred->data), best_pred->stride,
  2388. CONVERT_TO_SHORTPTR(pd->dst.buf), pd->dst.stride, NULL, 0, 0, 0, 0,
  2389. bw, bh, xd->bd);
  2390. else
  2391. vpx_convolve_copy(best_pred->data, best_pred->stride, pd->dst.buf,
  2392. pd->dst.stride, NULL, 0, 0, 0, 0, bw, bh);
  2393. #else
  2394. vpx_convolve_copy(best_pred->data, best_pred->stride, pd->dst.buf,
  2395. pd->dst.stride, NULL, 0, 0, 0, 0, bw, bh);
  2396. #endif // CONFIG_VP9_HIGHBITDEPTH
  2397. }
  2398. }
  2399. #if CONFIG_VP9_TEMPORAL_DENOISING
  2400. if (cpi->oxcf.noise_sensitivity > 0 && cpi->resize_pending == 0 &&
  2401. denoise_svc_pickmode && cpi->denoiser.denoising_level > kDenLowLow &&
  2402. cpi->denoiser.reset == 0) {
  2403. VP9_DENOISER_DECISION decision = COPY_BLOCK;
  2404. ctx->sb_skip_denoising = 0;
  2405. // TODO(marpan): There is an issue with denoising when the
  2406. // superblock partitioning scheme is based on the pickmode.
  2407. // Remove this condition when the issue is resolved.
  2408. if (x->sb_pickmode_part) ctx->sb_skip_denoising = 1;
  2409. vp9_pickmode_ctx_den_update(&ctx_den, zero_last_cost_orig, ref_frame_cost,
  2410. frame_mv, reuse_inter_pred, &best_pickmode);
  2411. vp9_denoiser_denoise(cpi, x, mi_row, mi_col, bsize, ctx, &decision,
  2412. gf_temporal_ref);
  2413. if (denoise_recheck_zeromv)
  2414. recheck_zeromv_after_denoising(cpi, mi, x, xd, decision, &ctx_den,
  2415. yv12_mb, &best_rdc, bsize, mi_row, mi_col);
  2416. best_pickmode.best_ref_frame = ctx_den.best_ref_frame;
  2417. }
  2418. #endif
  2419. if (best_pickmode.best_ref_frame == ALTREF_FRAME ||
  2420. best_pickmode.best_second_ref_frame == ALTREF_FRAME)
  2421. x->arf_frame_usage++;
  2422. else if (best_pickmode.best_ref_frame != INTRA_FRAME)
  2423. x->lastgolden_frame_usage++;
  2424. if (cpi->sf.adaptive_rd_thresh) {
  2425. THR_MODES best_mode_idx =
  2426. mode_idx[best_pickmode.best_ref_frame][mode_offset(mi->mode)];
  2427. if (best_pickmode.best_ref_frame == INTRA_FRAME) {
  2428. // Only consider the modes that are included in the intra_mode_list.
  2429. int intra_modes = sizeof(intra_mode_list) / sizeof(PREDICTION_MODE);
  2430. int i;
  2431. // TODO(yunqingwang): Check intra mode mask and only update freq_fact
  2432. // for those valid modes.
  2433. for (i = 0; i < intra_modes; i++) {
  2434. if (cpi->sf.adaptive_rd_thresh_row_mt)
  2435. update_thresh_freq_fact_row_mt(cpi, tile_data, x->source_variance,
  2436. thresh_freq_fact_idx, INTRA_FRAME,
  2437. best_mode_idx, intra_mode_list[i]);
  2438. else
  2439. update_thresh_freq_fact(cpi, tile_data, x->source_variance, bsize,
  2440. INTRA_FRAME, best_mode_idx,
  2441. intra_mode_list[i]);
  2442. }
  2443. } else {
  2444. for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ++ref_frame) {
  2445. PREDICTION_MODE this_mode;
  2446. if (best_pickmode.best_ref_frame != ref_frame) continue;
  2447. for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
  2448. if (cpi->sf.adaptive_rd_thresh_row_mt)
  2449. update_thresh_freq_fact_row_mt(cpi, tile_data, x->source_variance,
  2450. thresh_freq_fact_idx, ref_frame,
  2451. best_mode_idx, this_mode);
  2452. else
  2453. update_thresh_freq_fact(cpi, tile_data, x->source_variance, bsize,
  2454. ref_frame, best_mode_idx, this_mode);
  2455. }
  2456. }
  2457. }
  2458. }
  2459. *rd_cost = best_rdc;
  2460. }
  2461. void vp9_pick_inter_mode_sub8x8(VP9_COMP *cpi, MACROBLOCK *x, int mi_row,
  2462. int mi_col, RD_COST *rd_cost, BLOCK_SIZE bsize,
  2463. PICK_MODE_CONTEXT *ctx) {
  2464. VP9_COMMON *const cm = &cpi->common;
  2465. SPEED_FEATURES *const sf = &cpi->sf;
  2466. MACROBLOCKD *const xd = &x->e_mbd;
  2467. MODE_INFO *const mi = xd->mi[0];
  2468. MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
  2469. const struct segmentation *const seg = &cm->seg;
  2470. MV_REFERENCE_FRAME ref_frame, second_ref_frame = NONE;
  2471. MV_REFERENCE_FRAME best_ref_frame = NONE;
  2472. unsigned char segment_id = mi->segment_id;
  2473. struct buf_2d yv12_mb[4][MAX_MB_PLANE];
  2474. static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
  2475. VP9_ALT_FLAG };
  2476. int64_t best_rd = INT64_MAX;
  2477. b_mode_info bsi[MAX_REF_FRAMES][4];
  2478. int ref_frame_skip_mask = 0;
  2479. const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
  2480. const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
  2481. int idx, idy;
  2482. x->skip_encode = sf->skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
  2483. ctx->pred_pixel_ready = 0;
  2484. for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ++ref_frame) {
  2485. const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
  2486. int_mv dummy_mv[2];
  2487. x->pred_mv_sad[ref_frame] = INT_MAX;
  2488. if ((cpi->ref_frame_flags & flag_list[ref_frame]) && (yv12 != NULL)) {
  2489. int_mv *const candidates = mbmi_ext->ref_mvs[ref_frame];
  2490. const struct scale_factors *const sf = &cm->frame_refs[ref_frame - 1].sf;
  2491. vp9_setup_pred_block(xd, yv12_mb[ref_frame], yv12, mi_row, mi_col, sf,
  2492. sf);
  2493. vp9_find_mv_refs(cm, xd, xd->mi[0], ref_frame, candidates, mi_row, mi_col,
  2494. mbmi_ext->mode_context);
  2495. vp9_find_best_ref_mvs(xd, cm->allow_high_precision_mv, candidates,
  2496. &dummy_mv[0], &dummy_mv[1]);
  2497. } else {
  2498. ref_frame_skip_mask |= (1 << ref_frame);
  2499. }
  2500. }
  2501. mi->sb_type = bsize;
  2502. mi->tx_size = TX_4X4;
  2503. mi->uv_mode = DC_PRED;
  2504. mi->ref_frame[0] = LAST_FRAME;
  2505. mi->ref_frame[1] = NONE;
  2506. mi->interp_filter =
  2507. cm->interp_filter == SWITCHABLE ? EIGHTTAP : cm->interp_filter;
  2508. for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ++ref_frame) {
  2509. int64_t this_rd = 0;
  2510. int plane;
  2511. if (ref_frame_skip_mask & (1 << ref_frame)) continue;
  2512. #if CONFIG_BETTER_HW_COMPATIBILITY
  2513. if ((bsize == BLOCK_8X4 || bsize == BLOCK_4X8) && ref_frame > INTRA_FRAME &&
  2514. vp9_is_scaled(&cm->frame_refs[ref_frame - 1].sf))
  2515. continue;
  2516. #endif
  2517. // TODO(jingning, agrange): Scaling reference frame not supported for
  2518. // sub8x8 blocks. Is this supported now?
  2519. if (ref_frame > INTRA_FRAME &&
  2520. vp9_is_scaled(&cm->frame_refs[ref_frame - 1].sf))
  2521. continue;
  2522. // If the segment reference frame feature is enabled....
  2523. // then do nothing if the current ref frame is not allowed..
  2524. if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) &&
  2525. get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame)
  2526. continue;
  2527. mi->ref_frame[0] = ref_frame;
  2528. x->skip = 0;
  2529. set_ref_ptrs(cm, xd, ref_frame, second_ref_frame);
  2530. // Select prediction reference frames.
  2531. for (plane = 0; plane < MAX_MB_PLANE; plane++)
  2532. xd->plane[plane].pre[0] = yv12_mb[ref_frame][plane];
  2533. for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
  2534. for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
  2535. int_mv b_mv[MB_MODE_COUNT];
  2536. int64_t b_best_rd = INT64_MAX;
  2537. const int i = idy * 2 + idx;
  2538. PREDICTION_MODE this_mode;
  2539. RD_COST this_rdc;
  2540. unsigned int var_y, sse_y;
  2541. struct macroblock_plane *p = &x->plane[0];
  2542. struct macroblockd_plane *pd = &xd->plane[0];
  2543. const struct buf_2d orig_src = p->src;
  2544. const struct buf_2d orig_dst = pd->dst;
  2545. struct buf_2d orig_pre[2];
  2546. memcpy(orig_pre, xd->plane[0].pre, sizeof(orig_pre));
  2547. // set buffer pointers for sub8x8 motion search.
  2548. p->src.buf =
  2549. &p->src.buf[vp9_raster_block_offset(BLOCK_8X8, i, p->src.stride)];
  2550. pd->dst.buf =
  2551. &pd->dst.buf[vp9_raster_block_offset(BLOCK_8X8, i, pd->dst.stride)];
  2552. pd->pre[0].buf =
  2553. &pd->pre[0]
  2554. .buf[vp9_raster_block_offset(BLOCK_8X8, i, pd->pre[0].stride)];
  2555. b_mv[ZEROMV].as_int = 0;
  2556. b_mv[NEWMV].as_int = INVALID_MV;
  2557. vp9_append_sub8x8_mvs_for_idx(cm, xd, i, 0, mi_row, mi_col,
  2558. &b_mv[NEARESTMV], &b_mv[NEARMV],
  2559. mbmi_ext->mode_context);
  2560. for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
  2561. int b_rate = 0;
  2562. xd->mi[0]->bmi[i].as_mv[0].as_int = b_mv[this_mode].as_int;
  2563. if (this_mode == NEWMV) {
  2564. const int step_param = cpi->sf.mv.fullpel_search_step_param;
  2565. MV mvp_full;
  2566. MV tmp_mv;
  2567. int cost_list[5];
  2568. const MvLimits tmp_mv_limits = x->mv_limits;
  2569. uint32_t dummy_dist;
  2570. if (i == 0) {
  2571. mvp_full.row = b_mv[NEARESTMV].as_mv.row >> 3;
  2572. mvp_full.col = b_mv[NEARESTMV].as_mv.col >> 3;
  2573. } else {
  2574. mvp_full.row = xd->mi[0]->bmi[0].as_mv[0].as_mv.row >> 3;
  2575. mvp_full.col = xd->mi[0]->bmi[0].as_mv[0].as_mv.col >> 3;
  2576. }
  2577. vp9_set_mv_search_range(&x->mv_limits,
  2578. &mbmi_ext->ref_mvs[ref_frame][0].as_mv);
  2579. vp9_full_pixel_search(
  2580. cpi, x, bsize, &mvp_full, step_param, cpi->sf.mv.search_method,
  2581. x->sadperbit4, cond_cost_list(cpi, cost_list),
  2582. &mbmi_ext->ref_mvs[ref_frame][0].as_mv, &tmp_mv, INT_MAX, 0);
  2583. x->mv_limits = tmp_mv_limits;
  2584. // calculate the bit cost on motion vector
  2585. mvp_full.row = tmp_mv.row * 8;
  2586. mvp_full.col = tmp_mv.col * 8;
  2587. b_rate += vp9_mv_bit_cost(
  2588. &mvp_full, &mbmi_ext->ref_mvs[ref_frame][0].as_mv,
  2589. x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
  2590. b_rate += cpi->inter_mode_cost[x->mbmi_ext->mode_context[ref_frame]]
  2591. [INTER_OFFSET(NEWMV)];
  2592. if (RDCOST(x->rdmult, x->rddiv, b_rate, 0) > b_best_rd) continue;
  2593. cpi->find_fractional_mv_step(
  2594. x, &tmp_mv, &mbmi_ext->ref_mvs[ref_frame][0].as_mv,
  2595. cpi->common.allow_high_precision_mv, x->errorperbit,
  2596. &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop,
  2597. cpi->sf.mv.subpel_search_level, cond_cost_list(cpi, cost_list),
  2598. x->nmvjointcost, x->mvcost, &dummy_dist,
  2599. &x->pred_sse[ref_frame], NULL, 0, 0,
  2600. cpi->sf.use_accurate_subpel_search);
  2601. xd->mi[0]->bmi[i].as_mv[0].as_mv = tmp_mv;
  2602. } else {
  2603. b_rate += cpi->inter_mode_cost[x->mbmi_ext->mode_context[ref_frame]]
  2604. [INTER_OFFSET(this_mode)];
  2605. }
  2606. #if CONFIG_VP9_HIGHBITDEPTH
  2607. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  2608. vp9_highbd_build_inter_predictor(
  2609. CONVERT_TO_SHORTPTR(pd->pre[0].buf), pd->pre[0].stride,
  2610. CONVERT_TO_SHORTPTR(pd->dst.buf), pd->dst.stride,
  2611. &xd->mi[0]->bmi[i].as_mv[0].as_mv, &xd->block_refs[0]->sf,
  2612. 4 * num_4x4_blocks_wide, 4 * num_4x4_blocks_high, 0,
  2613. vp9_filter_kernels[mi->interp_filter], MV_PRECISION_Q3,
  2614. mi_col * MI_SIZE + 4 * (i & 0x01),
  2615. mi_row * MI_SIZE + 4 * (i >> 1), xd->bd);
  2616. } else {
  2617. #endif
  2618. vp9_build_inter_predictor(
  2619. pd->pre[0].buf, pd->pre[0].stride, pd->dst.buf, pd->dst.stride,
  2620. &xd->mi[0]->bmi[i].as_mv[0].as_mv, &xd->block_refs[0]->sf,
  2621. 4 * num_4x4_blocks_wide, 4 * num_4x4_blocks_high, 0,
  2622. vp9_filter_kernels[mi->interp_filter], MV_PRECISION_Q3,
  2623. mi_col * MI_SIZE + 4 * (i & 0x01),
  2624. mi_row * MI_SIZE + 4 * (i >> 1));
  2625. #if CONFIG_VP9_HIGHBITDEPTH
  2626. }
  2627. #endif
  2628. model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc.rate, &this_rdc.dist,
  2629. &var_y, &sse_y, 0);
  2630. this_rdc.rate += b_rate;
  2631. this_rdc.rdcost =
  2632. RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist);
  2633. if (this_rdc.rdcost < b_best_rd) {
  2634. b_best_rd = this_rdc.rdcost;
  2635. bsi[ref_frame][i].as_mode = this_mode;
  2636. bsi[ref_frame][i].as_mv[0].as_mv = xd->mi[0]->bmi[i].as_mv[0].as_mv;
  2637. }
  2638. } // mode search
  2639. // restore source and prediction buffer pointers.
  2640. p->src = orig_src;
  2641. pd->pre[0] = orig_pre[0];
  2642. pd->dst = orig_dst;
  2643. this_rd += b_best_rd;
  2644. xd->mi[0]->bmi[i] = bsi[ref_frame][i];
  2645. if (num_4x4_blocks_wide > 1) xd->mi[0]->bmi[i + 1] = xd->mi[0]->bmi[i];
  2646. if (num_4x4_blocks_high > 1) xd->mi[0]->bmi[i + 2] = xd->mi[0]->bmi[i];
  2647. }
  2648. } // loop through sub8x8 blocks
  2649. if (this_rd < best_rd) {
  2650. best_rd = this_rd;
  2651. best_ref_frame = ref_frame;
  2652. }
  2653. } // reference frames
  2654. mi->tx_size = TX_4X4;
  2655. mi->ref_frame[0] = best_ref_frame;
  2656. for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
  2657. for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
  2658. const int block = idy * 2 + idx;
  2659. xd->mi[0]->bmi[block] = bsi[best_ref_frame][block];
  2660. if (num_4x4_blocks_wide > 1)
  2661. xd->mi[0]->bmi[block + 1] = bsi[best_ref_frame][block];
  2662. if (num_4x4_blocks_high > 1)
  2663. xd->mi[0]->bmi[block + 2] = bsi[best_ref_frame][block];
  2664. }
  2665. }
  2666. mi->mode = xd->mi[0]->bmi[3].as_mode;
  2667. ctx->mic = *(xd->mi[0]);
  2668. ctx->mbmi_ext = *x->mbmi_ext;
  2669. ctx->skip_txfm[0] = SKIP_TXFM_NONE;
  2670. ctx->skip = 0;
  2671. // Dummy assignment for speed -5. No effect in speed -6.
  2672. rd_cost->rdcost = best_rd;
  2673. }