rdopt.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394
  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <assert.h>
  11. #include <stdio.h>
  12. #include <math.h>
  13. #include <limits.h>
  14. #include <assert.h>
  15. #include "vpx_config.h"
  16. #include "vp8_rtcd.h"
  17. #include "./vpx_dsp_rtcd.h"
  18. #include "encodeframe.h"
  19. #include "tokenize.h"
  20. #include "treewriter.h"
  21. #include "onyx_int.h"
  22. #include "modecosts.h"
  23. #include "encodeintra.h"
  24. #include "pickinter.h"
  25. #include "vp8/common/common.h"
  26. #include "vp8/common/entropymode.h"
  27. #include "vp8/common/reconinter.h"
  28. #include "vp8/common/reconintra.h"
  29. #include "vp8/common/reconintra4x4.h"
  30. #include "vp8/common/findnearmv.h"
  31. #include "vp8/common/quant_common.h"
  32. #include "encodemb.h"
  33. #include "vp8/encoder/quantize.h"
  34. #include "vpx_dsp/variance.h"
  35. #include "vpx_ports/system_state.h"
  36. #include "mcomp.h"
  37. #include "rdopt.h"
  38. #include "vpx_mem/vpx_mem.h"
  39. #include "vp8/common/systemdependent.h"
  40. #if CONFIG_TEMPORAL_DENOISING
  41. #include "denoising.h"
  42. #endif
  43. extern void vp8_update_zbin_extra(VP8_COMP *cpi, MACROBLOCK *x);
  44. #define MAXF(a, b) (((a) > (b)) ? (a) : (b))
  45. typedef struct rate_distortion_struct {
  46. int rate2;
  47. int rate_y;
  48. int rate_uv;
  49. int distortion2;
  50. int distortion_uv;
  51. } RATE_DISTORTION;
  52. typedef struct best_mode_struct {
  53. int yrd;
  54. int rd;
  55. int intra_rd;
  56. MB_MODE_INFO mbmode;
  57. union b_mode_info bmodes[16];
  58. PARTITION_INFO partition;
  59. } BEST_MODE;
  60. static const int auto_speed_thresh[17] = { 1000, 200, 150, 130, 150, 125,
  61. 120, 115, 115, 115, 115, 115,
  62. 115, 115, 115, 115, 105 };
  63. const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES] = {
  64. ZEROMV, DC_PRED,
  65. NEARESTMV, NEARMV,
  66. ZEROMV, NEARESTMV,
  67. ZEROMV, NEARESTMV,
  68. NEARMV, NEARMV,
  69. V_PRED, H_PRED, TM_PRED,
  70. NEWMV, NEWMV, NEWMV,
  71. SPLITMV, SPLITMV, SPLITMV,
  72. B_PRED,
  73. };
  74. /* This table determines the search order in reference frame priority order,
  75. * which may not necessarily match INTRA,LAST,GOLDEN,ARF
  76. */
  77. const int vp8_ref_frame_order[MAX_MODES] = {
  78. 1, 0,
  79. 1, 1,
  80. 2, 2,
  81. 3, 3,
  82. 2, 3,
  83. 0, 0, 0,
  84. 1, 2, 3,
  85. 1, 2, 3,
  86. 0,
  87. };
  88. static void fill_token_costs(
  89. int c[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS],
  90. const vp8_prob p[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS]
  91. [ENTROPY_NODES]) {
  92. int i, j, k;
  93. for (i = 0; i < BLOCK_TYPES; ++i) {
  94. for (j = 0; j < COEF_BANDS; ++j) {
  95. for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
  96. /* check for pt=0 and band > 1 if block type 0
  97. * and 0 if blocktype 1
  98. */
  99. if (k == 0 && j > (i == 0)) {
  100. vp8_cost_tokens2(c[i][j][k], p[i][j][k], vp8_coef_tree, 2);
  101. } else {
  102. vp8_cost_tokens(c[i][j][k], p[i][j][k], vp8_coef_tree);
  103. }
  104. }
  105. }
  106. }
  107. }
  108. static const int rd_iifactor[32] = { 4, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0,
  109. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  110. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  111. /* values are now correlated to quantizer */
  112. static const int sad_per_bit16lut[QINDEX_RANGE] = {
  113. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3,
  114. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  115. 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6,
  116. 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  117. 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9,
  118. 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11,
  119. 11, 11, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14
  120. };
  121. static const int sad_per_bit4lut[QINDEX_RANGE] = {
  122. 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  123. 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6,
  124. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  125. 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
  126. 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12,
  127. 12, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16,
  128. 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20,
  129. };
  130. void vp8cx_initialize_me_consts(VP8_COMP *cpi, int QIndex) {
  131. cpi->mb.sadperbit16 = sad_per_bit16lut[QIndex];
  132. cpi->mb.sadperbit4 = sad_per_bit4lut[QIndex];
  133. }
  134. void vp8_initialize_rd_consts(VP8_COMP *cpi, MACROBLOCK *x, int Qvalue) {
  135. int q;
  136. int i;
  137. double capped_q = (Qvalue < 160) ? (double)Qvalue : 160.0;
  138. double rdconst = 2.80;
  139. vpx_clear_system_state();
  140. /* Further tests required to see if optimum is different
  141. * for key frames, golden frames and arf frames.
  142. */
  143. cpi->RDMULT = (int)(rdconst * (capped_q * capped_q));
  144. /* Extend rate multiplier along side quantizer zbin increases */
  145. if (cpi->mb.zbin_over_quant > 0) {
  146. double oq_factor;
  147. double modq;
  148. /* Experimental code using the same basic equation as used for Q above
  149. * The units of cpi->mb.zbin_over_quant are 1/128 of Q bin size
  150. */
  151. oq_factor = 1.0 + ((double)0.0015625 * cpi->mb.zbin_over_quant);
  152. modq = (int)((double)capped_q * oq_factor);
  153. cpi->RDMULT = (int)(rdconst * (modq * modq));
  154. }
  155. if (cpi->pass == 2 && (cpi->common.frame_type != KEY_FRAME)) {
  156. if (cpi->twopass.next_iiratio > 31) {
  157. cpi->RDMULT += (cpi->RDMULT * rd_iifactor[31]) >> 4;
  158. } else {
  159. cpi->RDMULT +=
  160. (cpi->RDMULT * rd_iifactor[cpi->twopass.next_iiratio]) >> 4;
  161. }
  162. }
  163. cpi->mb.errorperbit = (cpi->RDMULT / 110);
  164. cpi->mb.errorperbit += (cpi->mb.errorperbit == 0);
  165. vp8_set_speed_features(cpi);
  166. for (i = 0; i < MAX_MODES; ++i) {
  167. x->mode_test_hit_counts[i] = 0;
  168. }
  169. q = (int)pow(Qvalue, 1.25);
  170. if (q < 8) q = 8;
  171. if (cpi->RDMULT > 1000) {
  172. cpi->RDDIV = 1;
  173. cpi->RDMULT /= 100;
  174. for (i = 0; i < MAX_MODES; ++i) {
  175. if (cpi->sf.thresh_mult[i] < INT_MAX) {
  176. x->rd_threshes[i] = cpi->sf.thresh_mult[i] * q / 100;
  177. } else {
  178. x->rd_threshes[i] = INT_MAX;
  179. }
  180. cpi->rd_baseline_thresh[i] = x->rd_threshes[i];
  181. }
  182. } else {
  183. cpi->RDDIV = 100;
  184. for (i = 0; i < MAX_MODES; ++i) {
  185. if (cpi->sf.thresh_mult[i] < (INT_MAX / q)) {
  186. x->rd_threshes[i] = cpi->sf.thresh_mult[i] * q;
  187. } else {
  188. x->rd_threshes[i] = INT_MAX;
  189. }
  190. cpi->rd_baseline_thresh[i] = x->rd_threshes[i];
  191. }
  192. }
  193. {
  194. /* build token cost array for the type of frame we have now */
  195. FRAME_CONTEXT *l = &cpi->lfc_n;
  196. if (cpi->common.refresh_alt_ref_frame) {
  197. l = &cpi->lfc_a;
  198. } else if (cpi->common.refresh_golden_frame) {
  199. l = &cpi->lfc_g;
  200. }
  201. fill_token_costs(cpi->mb.token_costs,
  202. (const vp8_prob(*)[8][3][11])l->coef_probs);
  203. /*
  204. fill_token_costs(
  205. cpi->mb.token_costs,
  206. (const vp8_prob( *)[8][3][11]) cpi->common.fc.coef_probs);
  207. */
  208. /* TODO make these mode costs depend on last,alt or gold too. (jbb) */
  209. vp8_init_mode_costs(cpi);
  210. }
  211. }
  212. void vp8_auto_select_speed(VP8_COMP *cpi) {
  213. int milliseconds_for_compress = (int)(1000000 / cpi->framerate);
  214. milliseconds_for_compress =
  215. milliseconds_for_compress * (16 - cpi->oxcf.cpu_used) / 16;
  216. #if 0
  217. if (0)
  218. {
  219. FILE *f;
  220. f = fopen("speed.stt", "a");
  221. fprintf(f, " %8ld %10ld %10ld %10ld\n",
  222. cpi->common.current_video_frame, cpi->Speed, milliseconds_for_compress, cpi->avg_pick_mode_time);
  223. fclose(f);
  224. }
  225. #endif
  226. if (cpi->avg_pick_mode_time < milliseconds_for_compress &&
  227. (cpi->avg_encode_time - cpi->avg_pick_mode_time) <
  228. milliseconds_for_compress) {
  229. if (cpi->avg_pick_mode_time == 0) {
  230. cpi->Speed = 4;
  231. } else {
  232. if (milliseconds_for_compress * 100 < cpi->avg_encode_time * 95) {
  233. cpi->Speed += 2;
  234. cpi->avg_pick_mode_time = 0;
  235. cpi->avg_encode_time = 0;
  236. if (cpi->Speed > 16) {
  237. cpi->Speed = 16;
  238. }
  239. }
  240. if (milliseconds_for_compress * 100 >
  241. cpi->avg_encode_time * auto_speed_thresh[cpi->Speed]) {
  242. cpi->Speed -= 1;
  243. cpi->avg_pick_mode_time = 0;
  244. cpi->avg_encode_time = 0;
  245. /* In real-time mode, cpi->speed is in [4, 16]. */
  246. if (cpi->Speed < 4) {
  247. cpi->Speed = 4;
  248. }
  249. }
  250. }
  251. } else {
  252. cpi->Speed += 4;
  253. if (cpi->Speed > 16) cpi->Speed = 16;
  254. cpi->avg_pick_mode_time = 0;
  255. cpi->avg_encode_time = 0;
  256. }
  257. }
  258. int vp8_block_error_c(short *coeff, short *dqcoeff) {
  259. int i;
  260. int error = 0;
  261. for (i = 0; i < 16; ++i) {
  262. int this_diff = coeff[i] - dqcoeff[i];
  263. error += this_diff * this_diff;
  264. }
  265. return error;
  266. }
  267. int vp8_mbblock_error_c(MACROBLOCK *mb, int dc) {
  268. BLOCK *be;
  269. BLOCKD *bd;
  270. int i, j;
  271. int berror, error = 0;
  272. for (i = 0; i < 16; ++i) {
  273. be = &mb->block[i];
  274. bd = &mb->e_mbd.block[i];
  275. berror = 0;
  276. for (j = dc; j < 16; ++j) {
  277. int this_diff = be->coeff[j] - bd->dqcoeff[j];
  278. berror += this_diff * this_diff;
  279. }
  280. error += berror;
  281. }
  282. return error;
  283. }
  284. int vp8_mbuverror_c(MACROBLOCK *mb) {
  285. BLOCK *be;
  286. BLOCKD *bd;
  287. int i;
  288. int error = 0;
  289. for (i = 16; i < 24; ++i) {
  290. be = &mb->block[i];
  291. bd = &mb->e_mbd.block[i];
  292. error += vp8_block_error_c(be->coeff, bd->dqcoeff);
  293. }
  294. return error;
  295. }
  296. int VP8_UVSSE(MACROBLOCK *x) {
  297. unsigned char *uptr, *vptr;
  298. unsigned char *upred_ptr = (*(x->block[16].base_src) + x->block[16].src);
  299. unsigned char *vpred_ptr = (*(x->block[20].base_src) + x->block[20].src);
  300. int uv_stride = x->block[16].src_stride;
  301. unsigned int sse1 = 0;
  302. unsigned int sse2 = 0;
  303. int mv_row = x->e_mbd.mode_info_context->mbmi.mv.as_mv.row;
  304. int mv_col = x->e_mbd.mode_info_context->mbmi.mv.as_mv.col;
  305. int offset;
  306. int pre_stride = x->e_mbd.pre.uv_stride;
  307. if (mv_row < 0) {
  308. mv_row -= 1;
  309. } else {
  310. mv_row += 1;
  311. }
  312. if (mv_col < 0) {
  313. mv_col -= 1;
  314. } else {
  315. mv_col += 1;
  316. }
  317. mv_row /= 2;
  318. mv_col /= 2;
  319. offset = (mv_row >> 3) * pre_stride + (mv_col >> 3);
  320. uptr = x->e_mbd.pre.u_buffer + offset;
  321. vptr = x->e_mbd.pre.v_buffer + offset;
  322. if ((mv_row | mv_col) & 7) {
  323. vpx_sub_pixel_variance8x8(uptr, pre_stride, mv_col & 7, mv_row & 7,
  324. upred_ptr, uv_stride, &sse2);
  325. vpx_sub_pixel_variance8x8(vptr, pre_stride, mv_col & 7, mv_row & 7,
  326. vpred_ptr, uv_stride, &sse1);
  327. sse2 += sse1;
  328. } else {
  329. vpx_variance8x8(uptr, pre_stride, upred_ptr, uv_stride, &sse2);
  330. vpx_variance8x8(vptr, pre_stride, vpred_ptr, uv_stride, &sse1);
  331. sse2 += sse1;
  332. }
  333. return sse2;
  334. }
  335. static int cost_coeffs(MACROBLOCK *mb, BLOCKD *b, int type, ENTROPY_CONTEXT *a,
  336. ENTROPY_CONTEXT *l) {
  337. int c = !type; /* start at coef 0, unless Y with Y2 */
  338. int eob = (int)(*b->eob);
  339. int pt; /* surrounding block/prev coef predictor */
  340. int cost = 0;
  341. short *qcoeff_ptr = b->qcoeff;
  342. VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
  343. assert(eob <= 16);
  344. for (; c < eob; ++c) {
  345. const int v = qcoeff_ptr[vp8_default_zig_zag1d[c]];
  346. const int t = vp8_dct_value_tokens_ptr[v].Token;
  347. cost += mb->token_costs[type][vp8_coef_bands[c]][pt][t];
  348. cost += vp8_dct_value_cost_ptr[v];
  349. pt = vp8_prev_token_class[t];
  350. }
  351. if (c < 16) {
  352. cost += mb->token_costs[type][vp8_coef_bands[c]][pt][DCT_EOB_TOKEN];
  353. }
  354. pt = (c != !type); /* is eob first coefficient; */
  355. *a = *l = pt;
  356. return cost;
  357. }
  358. static int vp8_rdcost_mby(MACROBLOCK *mb) {
  359. int cost = 0;
  360. int b;
  361. MACROBLOCKD *x = &mb->e_mbd;
  362. ENTROPY_CONTEXT_PLANES t_above, t_left;
  363. ENTROPY_CONTEXT *ta;
  364. ENTROPY_CONTEXT *tl;
  365. memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
  366. memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
  367. ta = (ENTROPY_CONTEXT *)&t_above;
  368. tl = (ENTROPY_CONTEXT *)&t_left;
  369. for (b = 0; b < 16; ++b) {
  370. cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_Y_NO_DC,
  371. ta + vp8_block2above[b], tl + vp8_block2left[b]);
  372. }
  373. cost += cost_coeffs(mb, x->block + 24, PLANE_TYPE_Y2,
  374. ta + vp8_block2above[24], tl + vp8_block2left[24]);
  375. return cost;
  376. }
  377. static void macro_block_yrd(MACROBLOCK *mb, int *Rate, int *Distortion) {
  378. int b;
  379. MACROBLOCKD *const x = &mb->e_mbd;
  380. BLOCK *const mb_y2 = mb->block + 24;
  381. BLOCKD *const x_y2 = x->block + 24;
  382. short *Y2DCPtr = mb_y2->src_diff;
  383. BLOCK *beptr;
  384. int d;
  385. vp8_subtract_mby(mb->src_diff, *(mb->block[0].base_src),
  386. mb->block[0].src_stride, mb->e_mbd.predictor, 16);
  387. /* Fdct and building the 2nd order block */
  388. for (beptr = mb->block; beptr < mb->block + 16; beptr += 2) {
  389. mb->short_fdct8x4(beptr->src_diff, beptr->coeff, 32);
  390. *Y2DCPtr++ = beptr->coeff[0];
  391. *Y2DCPtr++ = beptr->coeff[16];
  392. }
  393. /* 2nd order fdct */
  394. mb->short_walsh4x4(mb_y2->src_diff, mb_y2->coeff, 8);
  395. /* Quantization */
  396. for (b = 0; b < 16; ++b) {
  397. mb->quantize_b(&mb->block[b], &mb->e_mbd.block[b]);
  398. }
  399. /* DC predication and Quantization of 2nd Order block */
  400. mb->quantize_b(mb_y2, x_y2);
  401. /* Distortion */
  402. d = vp8_mbblock_error(mb, 1) << 2;
  403. d += vp8_block_error(mb_y2->coeff, x_y2->dqcoeff);
  404. *Distortion = (d >> 4);
  405. /* rate */
  406. *Rate = vp8_rdcost_mby(mb);
  407. }
  408. static void copy_predictor(unsigned char *dst, const unsigned char *predictor) {
  409. const unsigned int *p = (const unsigned int *)predictor;
  410. unsigned int *d = (unsigned int *)dst;
  411. d[0] = p[0];
  412. d[4] = p[4];
  413. d[8] = p[8];
  414. d[12] = p[12];
  415. }
  416. static int rd_pick_intra4x4block(MACROBLOCK *x, BLOCK *be, BLOCKD *b,
  417. B_PREDICTION_MODE *best_mode,
  418. const int *bmode_costs, ENTROPY_CONTEXT *a,
  419. ENTROPY_CONTEXT *l,
  420. int *bestrate, int *bestratey,
  421. int *bestdistortion) {
  422. B_PREDICTION_MODE mode;
  423. int best_rd = INT_MAX;
  424. int rate = 0;
  425. int distortion;
  426. ENTROPY_CONTEXT ta = *a, tempa = *a;
  427. ENTROPY_CONTEXT tl = *l, templ = *l;
  428. /*
  429. * The predictor buffer is a 2d buffer with a stride of 16. Create
  430. * a temp buffer that meets the stride requirements, but we are only
  431. * interested in the left 4x4 block
  432. * */
  433. DECLARE_ALIGNED(16, unsigned char, best_predictor[16 * 4]);
  434. DECLARE_ALIGNED(16, short, best_dqcoeff[16]);
  435. int dst_stride = x->e_mbd.dst.y_stride;
  436. unsigned char *dst = x->e_mbd.dst.y_buffer + b->offset;
  437. unsigned char *Above = dst - dst_stride;
  438. unsigned char *yleft = dst - 1;
  439. unsigned char top_left = Above[-1];
  440. for (mode = B_DC_PRED; mode <= B_HU_PRED; ++mode) {
  441. int this_rd;
  442. int ratey;
  443. rate = bmode_costs[mode];
  444. vp8_intra4x4_predict(Above, yleft, dst_stride, mode, b->predictor, 16,
  445. top_left);
  446. vp8_subtract_b(be, b, 16);
  447. x->short_fdct4x4(be->src_diff, be->coeff, 32);
  448. x->quantize_b(be, b);
  449. tempa = ta;
  450. templ = tl;
  451. ratey = cost_coeffs(x, b, PLANE_TYPE_Y_WITH_DC, &tempa, &templ);
  452. rate += ratey;
  453. distortion = vp8_block_error(be->coeff, b->dqcoeff) >> 2;
  454. this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
  455. if (this_rd < best_rd) {
  456. *bestrate = rate;
  457. *bestratey = ratey;
  458. *bestdistortion = distortion;
  459. best_rd = this_rd;
  460. *best_mode = mode;
  461. *a = tempa;
  462. *l = templ;
  463. copy_predictor(best_predictor, b->predictor);
  464. memcpy(best_dqcoeff, b->dqcoeff, 32);
  465. }
  466. }
  467. b->bmi.as_mode = *best_mode;
  468. vp8_short_idct4x4llm(best_dqcoeff, best_predictor, 16, dst, dst_stride);
  469. return best_rd;
  470. }
  471. static int rd_pick_intra4x4mby_modes(MACROBLOCK *mb, int *Rate, int *rate_y,
  472. int *Distortion, int best_rd) {
  473. MACROBLOCKD *const xd = &mb->e_mbd;
  474. int i;
  475. int cost = mb->mbmode_cost[xd->frame_type][B_PRED];
  476. int distortion = 0;
  477. int tot_rate_y = 0;
  478. int64_t total_rd = 0;
  479. ENTROPY_CONTEXT_PLANES t_above, t_left;
  480. ENTROPY_CONTEXT *ta;
  481. ENTROPY_CONTEXT *tl;
  482. const int *bmode_costs;
  483. memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
  484. memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
  485. ta = (ENTROPY_CONTEXT *)&t_above;
  486. tl = (ENTROPY_CONTEXT *)&t_left;
  487. intra_prediction_down_copy(xd, xd->dst.y_buffer - xd->dst.y_stride + 16);
  488. bmode_costs = mb->inter_bmode_costs;
  489. for (i = 0; i < 16; ++i) {
  490. MODE_INFO *const mic = xd->mode_info_context;
  491. const int mis = xd->mode_info_stride;
  492. B_PREDICTION_MODE best_mode = B_MODE_COUNT;
  493. int r = 0, ry = 0, d = 0;
  494. if (mb->e_mbd.frame_type == KEY_FRAME) {
  495. const B_PREDICTION_MODE A = above_block_mode(mic, i, mis);
  496. const B_PREDICTION_MODE L = left_block_mode(mic, i);
  497. bmode_costs = mb->bmode_costs[A][L];
  498. }
  499. total_rd += rd_pick_intra4x4block(
  500. mb, mb->block + i, xd->block + i, &best_mode, bmode_costs,
  501. ta + vp8_block2above[i], tl + vp8_block2left[i], &r, &ry, &d);
  502. cost += r;
  503. distortion += d;
  504. tot_rate_y += ry;
  505. assert(best_mode != B_MODE_COUNT);
  506. mic->bmi[i].as_mode = best_mode;
  507. if (total_rd >= (int64_t)best_rd) break;
  508. }
  509. if (total_rd >= (int64_t)best_rd) return INT_MAX;
  510. *Rate = cost;
  511. *rate_y = tot_rate_y;
  512. *Distortion = distortion;
  513. return RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
  514. }
  515. static int rd_pick_intra16x16mby_mode(MACROBLOCK *x, int *Rate, int *rate_y,
  516. int *Distortion) {
  517. MB_PREDICTION_MODE mode;
  518. MB_PREDICTION_MODE mode_selected = MB_MODE_COUNT;
  519. int rate, ratey;
  520. int distortion;
  521. int best_rd = INT_MAX;
  522. int this_rd;
  523. MACROBLOCKD *xd = &x->e_mbd;
  524. /* Y Search for 16x16 intra prediction mode */
  525. for (mode = DC_PRED; mode <= TM_PRED; ++mode) {
  526. xd->mode_info_context->mbmi.mode = mode;
  527. vp8_build_intra_predictors_mby_s(xd, xd->dst.y_buffer - xd->dst.y_stride,
  528. xd->dst.y_buffer - 1, xd->dst.y_stride,
  529. xd->predictor, 16);
  530. macro_block_yrd(x, &ratey, &distortion);
  531. rate = ratey +
  532. x->mbmode_cost[xd->frame_type][xd->mode_info_context->mbmi.mode];
  533. this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
  534. if (this_rd < best_rd) {
  535. mode_selected = mode;
  536. best_rd = this_rd;
  537. *Rate = rate;
  538. *rate_y = ratey;
  539. *Distortion = distortion;
  540. }
  541. }
  542. assert(mode_selected != MB_MODE_COUNT);
  543. xd->mode_info_context->mbmi.mode = mode_selected;
  544. return best_rd;
  545. }
  546. static int rd_cost_mbuv(MACROBLOCK *mb) {
  547. int b;
  548. int cost = 0;
  549. MACROBLOCKD *x = &mb->e_mbd;
  550. ENTROPY_CONTEXT_PLANES t_above, t_left;
  551. ENTROPY_CONTEXT *ta;
  552. ENTROPY_CONTEXT *tl;
  553. memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
  554. memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
  555. ta = (ENTROPY_CONTEXT *)&t_above;
  556. tl = (ENTROPY_CONTEXT *)&t_left;
  557. for (b = 16; b < 24; ++b) {
  558. cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_UV,
  559. ta + vp8_block2above[b], tl + vp8_block2left[b]);
  560. }
  561. return cost;
  562. }
  563. static int rd_inter16x16_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate,
  564. int *distortion, int fullpixel) {
  565. (void)cpi;
  566. (void)fullpixel;
  567. vp8_build_inter16x16_predictors_mbuv(&x->e_mbd);
  568. vp8_subtract_mbuv(x->src_diff, x->src.u_buffer, x->src.v_buffer,
  569. x->src.uv_stride, &x->e_mbd.predictor[256],
  570. &x->e_mbd.predictor[320], 8);
  571. vp8_transform_mbuv(x);
  572. vp8_quantize_mbuv(x);
  573. *rate = rd_cost_mbuv(x);
  574. *distortion = vp8_mbuverror(x) / 4;
  575. return RDCOST(x->rdmult, x->rddiv, *rate, *distortion);
  576. }
  577. static int rd_inter4x4_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate,
  578. int *distortion, int fullpixel) {
  579. (void)cpi;
  580. (void)fullpixel;
  581. vp8_build_inter4x4_predictors_mbuv(&x->e_mbd);
  582. vp8_subtract_mbuv(x->src_diff, x->src.u_buffer, x->src.v_buffer,
  583. x->src.uv_stride, &x->e_mbd.predictor[256],
  584. &x->e_mbd.predictor[320], 8);
  585. vp8_transform_mbuv(x);
  586. vp8_quantize_mbuv(x);
  587. *rate = rd_cost_mbuv(x);
  588. *distortion = vp8_mbuverror(x) / 4;
  589. return RDCOST(x->rdmult, x->rddiv, *rate, *distortion);
  590. }
  591. static void rd_pick_intra_mbuv_mode(MACROBLOCK *x, int *rate,
  592. int *rate_tokenonly, int *distortion) {
  593. MB_PREDICTION_MODE mode;
  594. MB_PREDICTION_MODE mode_selected = MB_MODE_COUNT;
  595. int best_rd = INT_MAX;
  596. int d = 0, r = 0;
  597. int rate_to;
  598. MACROBLOCKD *xd = &x->e_mbd;
  599. for (mode = DC_PRED; mode <= TM_PRED; ++mode) {
  600. int this_rate;
  601. int this_distortion;
  602. int this_rd;
  603. xd->mode_info_context->mbmi.uv_mode = mode;
  604. vp8_build_intra_predictors_mbuv_s(
  605. xd, xd->dst.u_buffer - xd->dst.uv_stride,
  606. xd->dst.v_buffer - xd->dst.uv_stride, xd->dst.u_buffer - 1,
  607. xd->dst.v_buffer - 1, xd->dst.uv_stride, &xd->predictor[256],
  608. &xd->predictor[320], 8);
  609. vp8_subtract_mbuv(x->src_diff, x->src.u_buffer, x->src.v_buffer,
  610. x->src.uv_stride, &xd->predictor[256],
  611. &xd->predictor[320], 8);
  612. vp8_transform_mbuv(x);
  613. vp8_quantize_mbuv(x);
  614. rate_to = rd_cost_mbuv(x);
  615. this_rate =
  616. rate_to + x->intra_uv_mode_cost[xd->frame_type]
  617. [xd->mode_info_context->mbmi.uv_mode];
  618. this_distortion = vp8_mbuverror(x) / 4;
  619. this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion);
  620. if (this_rd < best_rd) {
  621. best_rd = this_rd;
  622. d = this_distortion;
  623. r = this_rate;
  624. *rate_tokenonly = rate_to;
  625. mode_selected = mode;
  626. }
  627. }
  628. *rate = r;
  629. *distortion = d;
  630. assert(mode_selected != MB_MODE_COUNT);
  631. xd->mode_info_context->mbmi.uv_mode = mode_selected;
  632. }
  633. int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4]) {
  634. vp8_prob p[VP8_MVREFS - 1];
  635. assert(NEARESTMV <= m && m <= SPLITMV);
  636. vp8_mv_ref_probs(p, near_mv_ref_ct);
  637. return vp8_cost_token(vp8_mv_ref_tree, p,
  638. vp8_mv_ref_encoding_array + (m - NEARESTMV));
  639. }
  640. void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, int_mv *mv) {
  641. x->e_mbd.mode_info_context->mbmi.mode = mb;
  642. x->e_mbd.mode_info_context->mbmi.mv.as_int = mv->as_int;
  643. }
  644. static int labels2mode(MACROBLOCK *x, int const *labelings, int which_label,
  645. B_PREDICTION_MODE this_mode, int_mv *this_mv,
  646. int_mv *best_ref_mv, int *mvcost[2]) {
  647. MACROBLOCKD *const xd = &x->e_mbd;
  648. MODE_INFO *const mic = xd->mode_info_context;
  649. const int mis = xd->mode_info_stride;
  650. int cost = 0;
  651. int thismvcost = 0;
  652. /* We have to be careful retrieving previously-encoded motion vectors.
  653. Ones from this macroblock have to be pulled from the BLOCKD array
  654. as they have not yet made it to the bmi array in our MB_MODE_INFO. */
  655. int i = 0;
  656. do {
  657. BLOCKD *const d = xd->block + i;
  658. const int row = i >> 2, col = i & 3;
  659. B_PREDICTION_MODE m;
  660. if (labelings[i] != which_label) continue;
  661. if (col && labelings[i] == labelings[i - 1]) {
  662. m = LEFT4X4;
  663. } else if (row && labelings[i] == labelings[i - 4]) {
  664. m = ABOVE4X4;
  665. } else {
  666. /* the only time we should do costing for new motion vector
  667. * or mode is when we are on a new label (jbb May 08, 2007)
  668. */
  669. switch (m = this_mode) {
  670. case NEW4X4:
  671. thismvcost = vp8_mv_bit_cost(this_mv, best_ref_mv, mvcost, 102);
  672. break;
  673. case LEFT4X4:
  674. this_mv->as_int = col ? d[-1].bmi.mv.as_int : left_block_mv(mic, i);
  675. break;
  676. case ABOVE4X4:
  677. this_mv->as_int =
  678. row ? d[-4].bmi.mv.as_int : above_block_mv(mic, i, mis);
  679. break;
  680. case ZERO4X4: this_mv->as_int = 0; break;
  681. default: break;
  682. }
  683. if (m == ABOVE4X4) { /* replace above with left if same */
  684. int_mv left_mv;
  685. left_mv.as_int = col ? d[-1].bmi.mv.as_int : left_block_mv(mic, i);
  686. if (left_mv.as_int == this_mv->as_int) m = LEFT4X4;
  687. }
  688. cost = x->inter_bmode_costs[m];
  689. }
  690. d->bmi.mv.as_int = this_mv->as_int;
  691. x->partition_info->bmi[i].mode = m;
  692. x->partition_info->bmi[i].mv.as_int = this_mv->as_int;
  693. } while (++i < 16);
  694. cost += thismvcost;
  695. return cost;
  696. }
  697. static int rdcost_mbsegment_y(MACROBLOCK *mb, const int *labels,
  698. int which_label, ENTROPY_CONTEXT *ta,
  699. ENTROPY_CONTEXT *tl) {
  700. int cost = 0;
  701. int b;
  702. MACROBLOCKD *x = &mb->e_mbd;
  703. for (b = 0; b < 16; ++b) {
  704. if (labels[b] == which_label) {
  705. cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_Y_WITH_DC,
  706. ta + vp8_block2above[b], tl + vp8_block2left[b]);
  707. }
  708. }
  709. return cost;
  710. }
  711. static unsigned int vp8_encode_inter_mb_segment(MACROBLOCK *x,
  712. int const *labels,
  713. int which_label) {
  714. int i;
  715. unsigned int distortion = 0;
  716. int pre_stride = x->e_mbd.pre.y_stride;
  717. unsigned char *base_pre = x->e_mbd.pre.y_buffer;
  718. for (i = 0; i < 16; ++i) {
  719. if (labels[i] == which_label) {
  720. BLOCKD *bd = &x->e_mbd.block[i];
  721. BLOCK *be = &x->block[i];
  722. vp8_build_inter_predictors_b(bd, 16, base_pre, pre_stride,
  723. x->e_mbd.subpixel_predict);
  724. vp8_subtract_b(be, bd, 16);
  725. x->short_fdct4x4(be->src_diff, be->coeff, 32);
  726. x->quantize_b(be, bd);
  727. distortion += vp8_block_error(be->coeff, bd->dqcoeff);
  728. }
  729. }
  730. return distortion;
  731. }
  732. static const unsigned int segmentation_to_sseshift[4] = { 3, 3, 2, 0 };
  733. typedef struct {
  734. int_mv *ref_mv;
  735. int_mv mvp;
  736. int segment_rd;
  737. int segment_num;
  738. int r;
  739. int d;
  740. int segment_yrate;
  741. B_PREDICTION_MODE modes[16];
  742. int_mv mvs[16];
  743. unsigned char eobs[16];
  744. int mvthresh;
  745. int *mdcounts;
  746. int_mv sv_mvp[4]; /* save 4 mvp from 8x8 */
  747. int sv_istep[2]; /* save 2 initial step_param for 16x8/8x16 */
  748. } BEST_SEG_INFO;
  749. static void rd_check_segment(VP8_COMP *cpi, MACROBLOCK *x, BEST_SEG_INFO *bsi,
  750. unsigned int segmentation) {
  751. int i;
  752. int const *labels;
  753. int br = 0;
  754. int bd = 0;
  755. B_PREDICTION_MODE this_mode;
  756. int label_count;
  757. int this_segment_rd = 0;
  758. int label_mv_thresh;
  759. int rate = 0;
  760. int sbr = 0;
  761. int sbd = 0;
  762. int segmentyrate = 0;
  763. vp8_variance_fn_ptr_t *v_fn_ptr;
  764. ENTROPY_CONTEXT_PLANES t_above, t_left;
  765. ENTROPY_CONTEXT_PLANES t_above_b, t_left_b;
  766. memcpy(&t_above, x->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
  767. memcpy(&t_left, x->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
  768. vp8_zero(t_above_b);
  769. vp8_zero(t_left_b);
  770. br = 0;
  771. bd = 0;
  772. v_fn_ptr = &cpi->fn_ptr[segmentation];
  773. labels = vp8_mbsplits[segmentation];
  774. label_count = vp8_mbsplit_count[segmentation];
  775. /* 64 makes this threshold really big effectively making it so that we
  776. * very rarely check mvs on segments. setting this to 1 would make mv
  777. * thresh roughly equal to what it is for macroblocks
  778. */
  779. label_mv_thresh = 1 * bsi->mvthresh / label_count;
  780. /* Segmentation method overheads */
  781. rate = vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs,
  782. vp8_mbsplit_encodings + segmentation);
  783. rate += vp8_cost_mv_ref(SPLITMV, bsi->mdcounts);
  784. this_segment_rd += RDCOST(x->rdmult, x->rddiv, rate, 0);
  785. br += rate;
  786. for (i = 0; i < label_count; ++i) {
  787. int_mv mode_mv[B_MODE_COUNT] = { { 0 }, { 0 } };
  788. int best_label_rd = INT_MAX;
  789. B_PREDICTION_MODE mode_selected = ZERO4X4;
  790. int bestlabelyrate = 0;
  791. /* search for the best motion vector on this segment */
  792. for (this_mode = LEFT4X4; this_mode <= NEW4X4; ++this_mode) {
  793. int this_rd;
  794. int distortion;
  795. int labelyrate;
  796. ENTROPY_CONTEXT_PLANES t_above_s, t_left_s;
  797. ENTROPY_CONTEXT *ta_s;
  798. ENTROPY_CONTEXT *tl_s;
  799. memcpy(&t_above_s, &t_above, sizeof(ENTROPY_CONTEXT_PLANES));
  800. memcpy(&t_left_s, &t_left, sizeof(ENTROPY_CONTEXT_PLANES));
  801. ta_s = (ENTROPY_CONTEXT *)&t_above_s;
  802. tl_s = (ENTROPY_CONTEXT *)&t_left_s;
  803. if (this_mode == NEW4X4) {
  804. int sseshift;
  805. int num00;
  806. int step_param = 0;
  807. int further_steps;
  808. int n;
  809. int thissme;
  810. int bestsme = INT_MAX;
  811. int_mv temp_mv;
  812. BLOCK *c;
  813. BLOCKD *e;
  814. /* Is the best so far sufficiently good that we cant justify
  815. * doing a new motion search.
  816. */
  817. if (best_label_rd < label_mv_thresh) break;
  818. if (cpi->compressor_speed) {
  819. if (segmentation == BLOCK_8X16 || segmentation == BLOCK_16X8) {
  820. bsi->mvp.as_int = bsi->sv_mvp[i].as_int;
  821. if (i == 1 && segmentation == BLOCK_16X8) {
  822. bsi->mvp.as_int = bsi->sv_mvp[2].as_int;
  823. }
  824. step_param = bsi->sv_istep[i];
  825. }
  826. /* use previous block's result as next block's MV
  827. * predictor.
  828. */
  829. if (segmentation == BLOCK_4X4 && i > 0) {
  830. bsi->mvp.as_int = x->e_mbd.block[i - 1].bmi.mv.as_int;
  831. if (i == 4 || i == 8 || i == 12) {
  832. bsi->mvp.as_int = x->e_mbd.block[i - 4].bmi.mv.as_int;
  833. }
  834. step_param = 2;
  835. }
  836. }
  837. further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
  838. {
  839. int sadpb = x->sadperbit4;
  840. int_mv mvp_full;
  841. mvp_full.as_mv.row = bsi->mvp.as_mv.row >> 3;
  842. mvp_full.as_mv.col = bsi->mvp.as_mv.col >> 3;
  843. /* find first label */
  844. n = vp8_mbsplit_offset[segmentation][i];
  845. c = &x->block[n];
  846. e = &x->e_mbd.block[n];
  847. {
  848. bestsme = cpi->diamond_search_sad(
  849. x, c, e, &mvp_full, &mode_mv[NEW4X4], step_param, sadpb, &num00,
  850. v_fn_ptr, x->mvcost, bsi->ref_mv);
  851. n = num00;
  852. num00 = 0;
  853. while (n < further_steps) {
  854. n++;
  855. if (num00) {
  856. num00--;
  857. } else {
  858. thissme = cpi->diamond_search_sad(
  859. x, c, e, &mvp_full, &temp_mv, step_param + n, sadpb, &num00,
  860. v_fn_ptr, x->mvcost, bsi->ref_mv);
  861. if (thissme < bestsme) {
  862. bestsme = thissme;
  863. mode_mv[NEW4X4].as_int = temp_mv.as_int;
  864. }
  865. }
  866. }
  867. }
  868. sseshift = segmentation_to_sseshift[segmentation];
  869. /* Should we do a full search (best quality only) */
  870. if ((cpi->compressor_speed == 0) && (bestsme >> sseshift) > 4000) {
  871. /* Check if mvp_full is within the range. */
  872. vp8_clamp_mv(&mvp_full, x->mv_col_min, x->mv_col_max, x->mv_row_min,
  873. x->mv_row_max);
  874. thissme = cpi->full_search_sad(x, c, e, &mvp_full, sadpb, 16,
  875. v_fn_ptr, x->mvcost, bsi->ref_mv);
  876. if (thissme < bestsme) {
  877. bestsme = thissme;
  878. mode_mv[NEW4X4].as_int = e->bmi.mv.as_int;
  879. } else {
  880. /* The full search result is actually worse so
  881. * re-instate the previous best vector
  882. */
  883. e->bmi.mv.as_int = mode_mv[NEW4X4].as_int;
  884. }
  885. }
  886. }
  887. if (bestsme < INT_MAX) {
  888. int disto;
  889. unsigned int sse;
  890. cpi->find_fractional_mv_step(x, c, e, &mode_mv[NEW4X4], bsi->ref_mv,
  891. x->errorperbit, v_fn_ptr, x->mvcost,
  892. &disto, &sse);
  893. }
  894. } /* NEW4X4 */
  895. rate = labels2mode(x, labels, i, this_mode, &mode_mv[this_mode],
  896. bsi->ref_mv, x->mvcost);
  897. /* Trap vectors that reach beyond the UMV borders */
  898. if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) ||
  899. ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) ||
  900. ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) ||
  901. ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max)) {
  902. continue;
  903. }
  904. distortion = vp8_encode_inter_mb_segment(x, labels, i) / 4;
  905. labelyrate = rdcost_mbsegment_y(x, labels, i, ta_s, tl_s);
  906. rate += labelyrate;
  907. this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
  908. if (this_rd < best_label_rd) {
  909. sbr = rate;
  910. sbd = distortion;
  911. bestlabelyrate = labelyrate;
  912. mode_selected = this_mode;
  913. best_label_rd = this_rd;
  914. memcpy(&t_above_b, &t_above_s, sizeof(ENTROPY_CONTEXT_PLANES));
  915. memcpy(&t_left_b, &t_left_s, sizeof(ENTROPY_CONTEXT_PLANES));
  916. }
  917. } /*for each 4x4 mode*/
  918. memcpy(&t_above, &t_above_b, sizeof(ENTROPY_CONTEXT_PLANES));
  919. memcpy(&t_left, &t_left_b, sizeof(ENTROPY_CONTEXT_PLANES));
  920. labels2mode(x, labels, i, mode_selected, &mode_mv[mode_selected],
  921. bsi->ref_mv, x->mvcost);
  922. br += sbr;
  923. bd += sbd;
  924. segmentyrate += bestlabelyrate;
  925. this_segment_rd += best_label_rd;
  926. if (this_segment_rd >= bsi->segment_rd) break;
  927. } /* for each label */
  928. if (this_segment_rd < bsi->segment_rd) {
  929. bsi->r = br;
  930. bsi->d = bd;
  931. bsi->segment_yrate = segmentyrate;
  932. bsi->segment_rd = this_segment_rd;
  933. bsi->segment_num = segmentation;
  934. /* store everything needed to come back to this!! */
  935. for (i = 0; i < 16; ++i) {
  936. bsi->mvs[i].as_mv = x->partition_info->bmi[i].mv.as_mv;
  937. bsi->modes[i] = x->partition_info->bmi[i].mode;
  938. bsi->eobs[i] = x->e_mbd.eobs[i];
  939. }
  940. }
  941. }
  942. static void vp8_cal_step_param(int sr, int *sp) {
  943. int step = 0;
  944. if (sr > MAX_FIRST_STEP) {
  945. sr = MAX_FIRST_STEP;
  946. } else if (sr < 1) {
  947. sr = 1;
  948. }
  949. while (sr >>= 1) step++;
  950. *sp = MAX_MVSEARCH_STEPS - 1 - step;
  951. }
  952. static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x,
  953. int_mv *best_ref_mv, int best_rd,
  954. int *mdcounts, int *returntotrate,
  955. int *returnyrate,
  956. int *returndistortion,
  957. int mvthresh) {
  958. int i;
  959. BEST_SEG_INFO bsi;
  960. memset(&bsi, 0, sizeof(bsi));
  961. bsi.segment_rd = best_rd;
  962. bsi.ref_mv = best_ref_mv;
  963. bsi.mvp.as_int = best_ref_mv->as_int;
  964. bsi.mvthresh = mvthresh;
  965. bsi.mdcounts = mdcounts;
  966. for (i = 0; i < 16; ++i) {
  967. bsi.modes[i] = ZERO4X4;
  968. }
  969. if (cpi->compressor_speed == 0) {
  970. /* for now, we will keep the original segmentation order
  971. when in best quality mode */
  972. rd_check_segment(cpi, x, &bsi, BLOCK_16X8);
  973. rd_check_segment(cpi, x, &bsi, BLOCK_8X16);
  974. rd_check_segment(cpi, x, &bsi, BLOCK_8X8);
  975. rd_check_segment(cpi, x, &bsi, BLOCK_4X4);
  976. } else {
  977. int sr;
  978. rd_check_segment(cpi, x, &bsi, BLOCK_8X8);
  979. if (bsi.segment_rd < best_rd) {
  980. int col_min = ((best_ref_mv->as_mv.col + 7) >> 3) - MAX_FULL_PEL_VAL;
  981. int row_min = ((best_ref_mv->as_mv.row + 7) >> 3) - MAX_FULL_PEL_VAL;
  982. int col_max = (best_ref_mv->as_mv.col >> 3) + MAX_FULL_PEL_VAL;
  983. int row_max = (best_ref_mv->as_mv.row >> 3) + MAX_FULL_PEL_VAL;
  984. int tmp_col_min = x->mv_col_min;
  985. int tmp_col_max = x->mv_col_max;
  986. int tmp_row_min = x->mv_row_min;
  987. int tmp_row_max = x->mv_row_max;
  988. /* Get intersection of UMV window and valid MV window to reduce # of
  989. * checks in diamond search. */
  990. if (x->mv_col_min < col_min) x->mv_col_min = col_min;
  991. if (x->mv_col_max > col_max) x->mv_col_max = col_max;
  992. if (x->mv_row_min < row_min) x->mv_row_min = row_min;
  993. if (x->mv_row_max > row_max) x->mv_row_max = row_max;
  994. /* Get 8x8 result */
  995. bsi.sv_mvp[0].as_int = bsi.mvs[0].as_int;
  996. bsi.sv_mvp[1].as_int = bsi.mvs[2].as_int;
  997. bsi.sv_mvp[2].as_int = bsi.mvs[8].as_int;
  998. bsi.sv_mvp[3].as_int = bsi.mvs[10].as_int;
  999. /* Use 8x8 result as 16x8/8x16's predictor MV. Adjust search range
  1000. * according to the closeness of 2 MV. */
  1001. /* block 8X16 */
  1002. {
  1003. sr =
  1004. MAXF((abs(bsi.sv_mvp[0].as_mv.row - bsi.sv_mvp[2].as_mv.row)) >> 3,
  1005. (abs(bsi.sv_mvp[0].as_mv.col - bsi.sv_mvp[2].as_mv.col)) >> 3);
  1006. vp8_cal_step_param(sr, &bsi.sv_istep[0]);
  1007. sr =
  1008. MAXF((abs(bsi.sv_mvp[1].as_mv.row - bsi.sv_mvp[3].as_mv.row)) >> 3,
  1009. (abs(bsi.sv_mvp[1].as_mv.col - bsi.sv_mvp[3].as_mv.col)) >> 3);
  1010. vp8_cal_step_param(sr, &bsi.sv_istep[1]);
  1011. rd_check_segment(cpi, x, &bsi, BLOCK_8X16);
  1012. }
  1013. /* block 16X8 */
  1014. {
  1015. sr =
  1016. MAXF((abs(bsi.sv_mvp[0].as_mv.row - bsi.sv_mvp[1].as_mv.row)) >> 3,
  1017. (abs(bsi.sv_mvp[0].as_mv.col - bsi.sv_mvp[1].as_mv.col)) >> 3);
  1018. vp8_cal_step_param(sr, &bsi.sv_istep[0]);
  1019. sr =
  1020. MAXF((abs(bsi.sv_mvp[2].as_mv.row - bsi.sv_mvp[3].as_mv.row)) >> 3,
  1021. (abs(bsi.sv_mvp[2].as_mv.col - bsi.sv_mvp[3].as_mv.col)) >> 3);
  1022. vp8_cal_step_param(sr, &bsi.sv_istep[1]);
  1023. rd_check_segment(cpi, x, &bsi, BLOCK_16X8);
  1024. }
  1025. /* If 8x8 is better than 16x8/8x16, then do 4x4 search */
  1026. /* Not skip 4x4 if speed=0 (good quality) */
  1027. if (cpi->sf.no_skip_block4x4_search || bsi.segment_num == BLOCK_8X8)
  1028. /* || (sv_segment_rd8x8-bsi.segment_rd) < sv_segment_rd8x8>>5) */
  1029. {
  1030. bsi.mvp.as_int = bsi.sv_mvp[0].as_int;
  1031. rd_check_segment(cpi, x, &bsi, BLOCK_4X4);
  1032. }
  1033. /* restore UMV window */
  1034. x->mv_col_min = tmp_col_min;
  1035. x->mv_col_max = tmp_col_max;
  1036. x->mv_row_min = tmp_row_min;
  1037. x->mv_row_max = tmp_row_max;
  1038. }
  1039. }
  1040. /* set it to the best */
  1041. for (i = 0; i < 16; ++i) {
  1042. BLOCKD *bd = &x->e_mbd.block[i];
  1043. bd->bmi.mv.as_int = bsi.mvs[i].as_int;
  1044. *bd->eob = bsi.eobs[i];
  1045. }
  1046. *returntotrate = bsi.r;
  1047. *returndistortion = bsi.d;
  1048. *returnyrate = bsi.segment_yrate;
  1049. /* save partitions */
  1050. x->e_mbd.mode_info_context->mbmi.partitioning = bsi.segment_num;
  1051. x->partition_info->count = vp8_mbsplit_count[bsi.segment_num];
  1052. for (i = 0; i < x->partition_info->count; ++i) {
  1053. int j;
  1054. j = vp8_mbsplit_offset[bsi.segment_num][i];
  1055. x->partition_info->bmi[i].mode = bsi.modes[j];
  1056. x->partition_info->bmi[i].mv.as_mv = bsi.mvs[j].as_mv;
  1057. }
  1058. /*
  1059. * used to set x->e_mbd.mode_info_context->mbmi.mv.as_int
  1060. */
  1061. x->partition_info->bmi[15].mv.as_int = bsi.mvs[15].as_int;
  1062. return bsi.segment_rd;
  1063. }
  1064. /* The improved MV prediction */
  1065. void vp8_mv_pred(VP8_COMP *cpi, MACROBLOCKD *xd, const MODE_INFO *here,
  1066. int_mv *mvp, int refframe, int *ref_frame_sign_bias, int *sr,
  1067. int near_sadidx[]) {
  1068. const MODE_INFO *above = here - xd->mode_info_stride;
  1069. const MODE_INFO *left = here - 1;
  1070. const MODE_INFO *aboveleft = above - 1;
  1071. int_mv near_mvs[8];
  1072. int near_ref[8];
  1073. int_mv mv;
  1074. int vcnt = 0;
  1075. int find = 0;
  1076. int mb_offset;
  1077. int mvx[8];
  1078. int mvy[8];
  1079. int i;
  1080. mv.as_int = 0;
  1081. if (here->mbmi.ref_frame != INTRA_FRAME) {
  1082. near_mvs[0].as_int = near_mvs[1].as_int = near_mvs[2].as_int =
  1083. near_mvs[3].as_int = near_mvs[4].as_int = near_mvs[5].as_int =
  1084. near_mvs[6].as_int = near_mvs[7].as_int = 0;
  1085. near_ref[0] = near_ref[1] = near_ref[2] = near_ref[3] = near_ref[4] =
  1086. near_ref[5] = near_ref[6] = near_ref[7] = 0;
  1087. /* read in 3 nearby block's MVs from current frame as prediction
  1088. * candidates.
  1089. */
  1090. if (above->mbmi.ref_frame != INTRA_FRAME) {
  1091. near_mvs[vcnt].as_int = above->mbmi.mv.as_int;
  1092. mv_bias(ref_frame_sign_bias[above->mbmi.ref_frame], refframe,
  1093. &near_mvs[vcnt], ref_frame_sign_bias);
  1094. near_ref[vcnt] = above->mbmi.ref_frame;
  1095. }
  1096. vcnt++;
  1097. if (left->mbmi.ref_frame != INTRA_FRAME) {
  1098. near_mvs[vcnt].as_int = left->mbmi.mv.as_int;
  1099. mv_bias(ref_frame_sign_bias[left->mbmi.ref_frame], refframe,
  1100. &near_mvs[vcnt], ref_frame_sign_bias);
  1101. near_ref[vcnt] = left->mbmi.ref_frame;
  1102. }
  1103. vcnt++;
  1104. if (aboveleft->mbmi.ref_frame != INTRA_FRAME) {
  1105. near_mvs[vcnt].as_int = aboveleft->mbmi.mv.as_int;
  1106. mv_bias(ref_frame_sign_bias[aboveleft->mbmi.ref_frame], refframe,
  1107. &near_mvs[vcnt], ref_frame_sign_bias);
  1108. near_ref[vcnt] = aboveleft->mbmi.ref_frame;
  1109. }
  1110. vcnt++;
  1111. /* read in 5 nearby block's MVs from last frame. */
  1112. if (cpi->common.last_frame_type != KEY_FRAME) {
  1113. mb_offset = (-xd->mb_to_top_edge / 128 + 1) * (xd->mode_info_stride + 1) +
  1114. (-xd->mb_to_left_edge / 128 + 1);
  1115. /* current in last frame */
  1116. if (cpi->lf_ref_frame[mb_offset] != INTRA_FRAME) {
  1117. near_mvs[vcnt].as_int = cpi->lfmv[mb_offset].as_int;
  1118. mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset], refframe,
  1119. &near_mvs[vcnt], ref_frame_sign_bias);
  1120. near_ref[vcnt] = cpi->lf_ref_frame[mb_offset];
  1121. }
  1122. vcnt++;
  1123. /* above in last frame */
  1124. if (cpi->lf_ref_frame[mb_offset - xd->mode_info_stride - 1] !=
  1125. INTRA_FRAME) {
  1126. near_mvs[vcnt].as_int =
  1127. cpi->lfmv[mb_offset - xd->mode_info_stride - 1].as_int;
  1128. mv_bias(
  1129. cpi->lf_ref_frame_sign_bias[mb_offset - xd->mode_info_stride - 1],
  1130. refframe, &near_mvs[vcnt], ref_frame_sign_bias);
  1131. near_ref[vcnt] =
  1132. cpi->lf_ref_frame[mb_offset - xd->mode_info_stride - 1];
  1133. }
  1134. vcnt++;
  1135. /* left in last frame */
  1136. if (cpi->lf_ref_frame[mb_offset - 1] != INTRA_FRAME) {
  1137. near_mvs[vcnt].as_int = cpi->lfmv[mb_offset - 1].as_int;
  1138. mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset - 1], refframe,
  1139. &near_mvs[vcnt], ref_frame_sign_bias);
  1140. near_ref[vcnt] = cpi->lf_ref_frame[mb_offset - 1];
  1141. }
  1142. vcnt++;
  1143. /* right in last frame */
  1144. if (cpi->lf_ref_frame[mb_offset + 1] != INTRA_FRAME) {
  1145. near_mvs[vcnt].as_int = cpi->lfmv[mb_offset + 1].as_int;
  1146. mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset + 1], refframe,
  1147. &near_mvs[vcnt], ref_frame_sign_bias);
  1148. near_ref[vcnt] = cpi->lf_ref_frame[mb_offset + 1];
  1149. }
  1150. vcnt++;
  1151. /* below in last frame */
  1152. if (cpi->lf_ref_frame[mb_offset + xd->mode_info_stride + 1] !=
  1153. INTRA_FRAME) {
  1154. near_mvs[vcnt].as_int =
  1155. cpi->lfmv[mb_offset + xd->mode_info_stride + 1].as_int;
  1156. mv_bias(
  1157. cpi->lf_ref_frame_sign_bias[mb_offset + xd->mode_info_stride + 1],
  1158. refframe, &near_mvs[vcnt], ref_frame_sign_bias);
  1159. near_ref[vcnt] =
  1160. cpi->lf_ref_frame[mb_offset + xd->mode_info_stride + 1];
  1161. }
  1162. vcnt++;
  1163. }
  1164. for (i = 0; i < vcnt; ++i) {
  1165. if (near_ref[near_sadidx[i]] != INTRA_FRAME) {
  1166. if (here->mbmi.ref_frame == near_ref[near_sadidx[i]]) {
  1167. mv.as_int = near_mvs[near_sadidx[i]].as_int;
  1168. find = 1;
  1169. if (i < 3) {
  1170. *sr = 3;
  1171. } else {
  1172. *sr = 2;
  1173. }
  1174. break;
  1175. }
  1176. }
  1177. }
  1178. if (!find) {
  1179. for (i = 0; i < vcnt; ++i) {
  1180. mvx[i] = near_mvs[i].as_mv.row;
  1181. mvy[i] = near_mvs[i].as_mv.col;
  1182. }
  1183. insertsortmv(mvx, vcnt);
  1184. insertsortmv(mvy, vcnt);
  1185. mv.as_mv.row = mvx[vcnt / 2];
  1186. mv.as_mv.col = mvy[vcnt / 2];
  1187. /* sr is set to 0 to allow calling function to decide the search
  1188. * range.
  1189. */
  1190. *sr = 0;
  1191. }
  1192. }
  1193. /* Set up return values */
  1194. mvp->as_int = mv.as_int;
  1195. vp8_clamp_mv2(mvp, xd);
  1196. }
  1197. void vp8_cal_sad(VP8_COMP *cpi, MACROBLOCKD *xd, MACROBLOCK *x,
  1198. int recon_yoffset, int near_sadidx[]) {
  1199. /* near_sad indexes:
  1200. * 0-cf above, 1-cf left, 2-cf aboveleft,
  1201. * 3-lf current, 4-lf above, 5-lf left, 6-lf right, 7-lf below
  1202. */
  1203. int near_sad[8] = { 0 };
  1204. BLOCK *b = &x->block[0];
  1205. unsigned char *src_y_ptr = *(b->base_src);
  1206. /* calculate sad for current frame 3 nearby MBs. */
  1207. if (xd->mb_to_top_edge == 0 && xd->mb_to_left_edge == 0) {
  1208. near_sad[0] = near_sad[1] = near_sad[2] = INT_MAX;
  1209. } else if (xd->mb_to_top_edge ==
  1210. 0) { /* only has left MB for sad calculation. */
  1211. near_sad[0] = near_sad[2] = INT_MAX;
  1212. near_sad[1] = cpi->fn_ptr[BLOCK_16X16].sdf(
  1213. src_y_ptr, b->src_stride, xd->dst.y_buffer - 16, xd->dst.y_stride);
  1214. } else if (xd->mb_to_left_edge ==
  1215. 0) { /* only has left MB for sad calculation. */
  1216. near_sad[1] = near_sad[2] = INT_MAX;
  1217. near_sad[0] = cpi->fn_ptr[BLOCK_16X16].sdf(
  1218. src_y_ptr, b->src_stride, xd->dst.y_buffer - xd->dst.y_stride * 16,
  1219. xd->dst.y_stride);
  1220. } else {
  1221. near_sad[0] = cpi->fn_ptr[BLOCK_16X16].sdf(
  1222. src_y_ptr, b->src_stride, xd->dst.y_buffer - xd->dst.y_stride * 16,
  1223. xd->dst.y_stride);
  1224. near_sad[1] = cpi->fn_ptr[BLOCK_16X16].sdf(
  1225. src_y_ptr, b->src_stride, xd->dst.y_buffer - 16, xd->dst.y_stride);
  1226. near_sad[2] = cpi->fn_ptr[BLOCK_16X16].sdf(
  1227. src_y_ptr, b->src_stride, xd->dst.y_buffer - xd->dst.y_stride * 16 - 16,
  1228. xd->dst.y_stride);
  1229. }
  1230. if (cpi->common.last_frame_type != KEY_FRAME) {
  1231. /* calculate sad for last frame 5 nearby MBs. */
  1232. unsigned char *pre_y_buffer =
  1233. cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_buffer + recon_yoffset;
  1234. int pre_y_stride = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_stride;
  1235. if (xd->mb_to_top_edge == 0) near_sad[4] = INT_MAX;
  1236. if (xd->mb_to_left_edge == 0) near_sad[5] = INT_MAX;
  1237. if (xd->mb_to_right_edge == 0) near_sad[6] = INT_MAX;
  1238. if (xd->mb_to_bottom_edge == 0) near_sad[7] = INT_MAX;
  1239. if (near_sad[4] != INT_MAX) {
  1240. near_sad[4] = cpi->fn_ptr[BLOCK_16X16].sdf(
  1241. src_y_ptr, b->src_stride, pre_y_buffer - pre_y_stride * 16,
  1242. pre_y_stride);
  1243. }
  1244. if (near_sad[5] != INT_MAX) {
  1245. near_sad[5] = cpi->fn_ptr[BLOCK_16X16].sdf(
  1246. src_y_ptr, b->src_stride, pre_y_buffer - 16, pre_y_stride);
  1247. }
  1248. near_sad[3] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride,
  1249. pre_y_buffer, pre_y_stride);
  1250. if (near_sad[6] != INT_MAX) {
  1251. near_sad[6] = cpi->fn_ptr[BLOCK_16X16].sdf(
  1252. src_y_ptr, b->src_stride, pre_y_buffer + 16, pre_y_stride);
  1253. }
  1254. if (near_sad[7] != INT_MAX) {
  1255. near_sad[7] = cpi->fn_ptr[BLOCK_16X16].sdf(
  1256. src_y_ptr, b->src_stride, pre_y_buffer + pre_y_stride * 16,
  1257. pre_y_stride);
  1258. }
  1259. }
  1260. if (cpi->common.last_frame_type != KEY_FRAME) {
  1261. insertsortsad(near_sad, near_sadidx, 8);
  1262. } else {
  1263. insertsortsad(near_sad, near_sadidx, 3);
  1264. }
  1265. }
  1266. static void rd_update_mvcount(MACROBLOCK *x, int_mv *best_ref_mv) {
  1267. if (x->e_mbd.mode_info_context->mbmi.mode == SPLITMV) {
  1268. int i;
  1269. for (i = 0; i < x->partition_info->count; ++i) {
  1270. if (x->partition_info->bmi[i].mode == NEW4X4) {
  1271. x->MVcount[0][mv_max + ((x->partition_info->bmi[i].mv.as_mv.row -
  1272. best_ref_mv->as_mv.row) >>
  1273. 1)]++;
  1274. x->MVcount[1][mv_max + ((x->partition_info->bmi[i].mv.as_mv.col -
  1275. best_ref_mv->as_mv.col) >>
  1276. 1)]++;
  1277. }
  1278. }
  1279. } else if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV) {
  1280. x->MVcount[0][mv_max + ((x->e_mbd.mode_info_context->mbmi.mv.as_mv.row -
  1281. best_ref_mv->as_mv.row) >>
  1282. 1)]++;
  1283. x->MVcount[1][mv_max + ((x->e_mbd.mode_info_context->mbmi.mv.as_mv.col -
  1284. best_ref_mv->as_mv.col) >>
  1285. 1)]++;
  1286. }
  1287. }
  1288. static int evaluate_inter_mode_rd(int mdcounts[4], RATE_DISTORTION *rd,
  1289. int *disable_skip, VP8_COMP *cpi,
  1290. MACROBLOCK *x) {
  1291. MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
  1292. BLOCK *b = &x->block[0];
  1293. MACROBLOCKD *xd = &x->e_mbd;
  1294. int distortion;
  1295. vp8_build_inter16x16_predictors_mby(&x->e_mbd, x->e_mbd.predictor, 16);
  1296. if (cpi->active_map_enabled && x->active_ptr[0] == 0) {
  1297. x->skip = 1;
  1298. } else if (x->encode_breakout) {
  1299. unsigned int sse;
  1300. unsigned int var;
  1301. unsigned int threshold =
  1302. (xd->block[0].dequant[1] * xd->block[0].dequant[1] >> 4);
  1303. if (threshold < x->encode_breakout) threshold = x->encode_breakout;
  1304. var = vpx_variance16x16(*(b->base_src), b->src_stride, x->e_mbd.predictor,
  1305. 16, &sse);
  1306. if (sse < threshold) {
  1307. unsigned int q2dc = xd->block[24].dequant[0];
  1308. /* If theres is no codeable 2nd order dc
  1309. or a very small uniform pixel change change */
  1310. if ((sse - var<q2dc * q2dc>> 4) || (sse / 2 > var && sse - var < 64)) {
  1311. /* Check u and v to make sure skip is ok */
  1312. unsigned int sse2 = VP8_UVSSE(x);
  1313. if (sse2 * 2 < threshold) {
  1314. x->skip = 1;
  1315. rd->distortion2 = sse + sse2;
  1316. rd->rate2 = 500;
  1317. /* for best_yrd calculation */
  1318. rd->rate_uv = 0;
  1319. rd->distortion_uv = sse2;
  1320. *disable_skip = 1;
  1321. return RDCOST(x->rdmult, x->rddiv, rd->rate2, rd->distortion2);
  1322. }
  1323. }
  1324. }
  1325. }
  1326. /* Add in the Mv/mode cost */
  1327. rd->rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
  1328. /* Y cost and distortion */
  1329. macro_block_yrd(x, &rd->rate_y, &distortion);
  1330. rd->rate2 += rd->rate_y;
  1331. rd->distortion2 += distortion;
  1332. /* UV cost and distortion */
  1333. rd_inter16x16_uv(cpi, x, &rd->rate_uv, &rd->distortion_uv,
  1334. cpi->common.full_pixel);
  1335. rd->rate2 += rd->rate_uv;
  1336. rd->distortion2 += rd->distortion_uv;
  1337. return INT_MAX;
  1338. }
  1339. static int calculate_final_rd_costs(int this_rd, RATE_DISTORTION *rd,
  1340. int *other_cost, int disable_skip,
  1341. int uv_intra_tteob, int intra_rd_penalty,
  1342. VP8_COMP *cpi, MACROBLOCK *x) {
  1343. MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
  1344. /* Where skip is allowable add in the default per mb cost for the no
  1345. * skip case. where we then decide to skip we have to delete this and
  1346. * replace it with the cost of signalling a skip
  1347. */
  1348. if (cpi->common.mb_no_coeff_skip) {
  1349. *other_cost += vp8_cost_bit(cpi->prob_skip_false, 0);
  1350. rd->rate2 += *other_cost;
  1351. }
  1352. /* Estimate the reference frame signaling cost and add it
  1353. * to the rolling cost variable.
  1354. */
  1355. rd->rate2 += x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
  1356. if (!disable_skip) {
  1357. /* Test for the condition where skip block will be activated
  1358. * because there are no non zero coefficients and make any
  1359. * necessary adjustment for rate
  1360. */
  1361. if (cpi->common.mb_no_coeff_skip) {
  1362. int i;
  1363. int tteob;
  1364. int has_y2_block = (this_mode != SPLITMV && this_mode != B_PRED);
  1365. tteob = 0;
  1366. if (has_y2_block) tteob += x->e_mbd.eobs[24];
  1367. for (i = 0; i < 16; ++i) tteob += (x->e_mbd.eobs[i] > has_y2_block);
  1368. if (x->e_mbd.mode_info_context->mbmi.ref_frame) {
  1369. for (i = 16; i < 24; ++i) tteob += x->e_mbd.eobs[i];
  1370. } else {
  1371. tteob += uv_intra_tteob;
  1372. }
  1373. if (tteob == 0) {
  1374. rd->rate2 -= (rd->rate_y + rd->rate_uv);
  1375. /* for best_yrd calculation */
  1376. rd->rate_uv = 0;
  1377. /* Back out no skip flag costing and add in skip flag costing */
  1378. if (cpi->prob_skip_false) {
  1379. int prob_skip_cost;
  1380. prob_skip_cost = vp8_cost_bit(cpi->prob_skip_false, 1);
  1381. prob_skip_cost -= (int)vp8_cost_bit(cpi->prob_skip_false, 0);
  1382. rd->rate2 += prob_skip_cost;
  1383. *other_cost += prob_skip_cost;
  1384. }
  1385. }
  1386. }
  1387. /* Calculate the final RD estimate for this mode */
  1388. this_rd = RDCOST(x->rdmult, x->rddiv, rd->rate2, rd->distortion2);
  1389. if (this_rd < INT_MAX &&
  1390. x->e_mbd.mode_info_context->mbmi.ref_frame == INTRA_FRAME) {
  1391. this_rd += intra_rd_penalty;
  1392. }
  1393. }
  1394. return this_rd;
  1395. }
  1396. static void update_best_mode(BEST_MODE *best_mode, int this_rd,
  1397. RATE_DISTORTION *rd, int other_cost,
  1398. MACROBLOCK *x) {
  1399. MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
  1400. other_cost += x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
  1401. /* Calculate the final y RD estimate for this mode */
  1402. best_mode->yrd =
  1403. RDCOST(x->rdmult, x->rddiv, (rd->rate2 - rd->rate_uv - other_cost),
  1404. (rd->distortion2 - rd->distortion_uv));
  1405. best_mode->rd = this_rd;
  1406. memcpy(&best_mode->mbmode, &x->e_mbd.mode_info_context->mbmi,
  1407. sizeof(MB_MODE_INFO));
  1408. memcpy(&best_mode->partition, x->partition_info, sizeof(PARTITION_INFO));
  1409. if ((this_mode == B_PRED) || (this_mode == SPLITMV)) {
  1410. int i;
  1411. for (i = 0; i < 16; ++i) {
  1412. best_mode->bmodes[i] = x->e_mbd.block[i].bmi;
  1413. }
  1414. }
  1415. }
  1416. void vp8_rd_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset,
  1417. int recon_uvoffset, int *returnrate,
  1418. int *returndistortion, int *returnintra, int mb_row,
  1419. int mb_col) {
  1420. BLOCK *b = &x->block[0];
  1421. BLOCKD *d = &x->e_mbd.block[0];
  1422. MACROBLOCKD *xd = &x->e_mbd;
  1423. int_mv best_ref_mv_sb[2];
  1424. int_mv mode_mv_sb[2][MB_MODE_COUNT];
  1425. int_mv best_ref_mv;
  1426. int_mv *mode_mv;
  1427. MB_PREDICTION_MODE this_mode;
  1428. int num00;
  1429. int best_mode_index = 0;
  1430. BEST_MODE best_mode;
  1431. int i;
  1432. int mode_index;
  1433. int mdcounts[4];
  1434. int rate;
  1435. RATE_DISTORTION rd;
  1436. int uv_intra_rate, uv_intra_distortion, uv_intra_rate_tokenonly;
  1437. int uv_intra_tteob = 0;
  1438. int uv_intra_done = 0;
  1439. MB_PREDICTION_MODE uv_intra_mode = 0;
  1440. int_mv mvp;
  1441. int near_sadidx[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
  1442. int saddone = 0;
  1443. /* search range got from mv_pred(). It uses step_param levels. (0-7) */
  1444. int sr = 0;
  1445. unsigned char *plane[4][3] = { { 0, 0 } };
  1446. int ref_frame_map[4];
  1447. int sign_bias = 0;
  1448. int intra_rd_penalty =
  1449. 10 * vp8_dc_quant(cpi->common.base_qindex, cpi->common.y1dc_delta_q);
  1450. #if CONFIG_TEMPORAL_DENOISING
  1451. unsigned int zero_mv_sse = UINT_MAX, best_sse = UINT_MAX,
  1452. best_rd_sse = UINT_MAX;
  1453. #endif
  1454. // _uv variables are not set consistantly before calling update_best_mode.
  1455. rd.rate_uv = 0;
  1456. rd.distortion_uv = 0;
  1457. mode_mv = mode_mv_sb[sign_bias];
  1458. best_ref_mv.as_int = 0;
  1459. best_mode.rd = INT_MAX;
  1460. best_mode.yrd = INT_MAX;
  1461. best_mode.intra_rd = INT_MAX;
  1462. memset(mode_mv_sb, 0, sizeof(mode_mv_sb));
  1463. memset(&best_mode.mbmode, 0, sizeof(best_mode.mbmode));
  1464. memset(&best_mode.bmodes, 0, sizeof(best_mode.bmodes));
  1465. /* Setup search priorities */
  1466. get_reference_search_order(cpi, ref_frame_map);
  1467. /* Check to see if there is at least 1 valid reference frame that we need
  1468. * to calculate near_mvs.
  1469. */
  1470. if (ref_frame_map[1] > 0) {
  1471. sign_bias = vp8_find_near_mvs_bias(
  1472. &x->e_mbd, x->e_mbd.mode_info_context, mode_mv_sb, best_ref_mv_sb,
  1473. mdcounts, ref_frame_map[1], cpi->common.ref_frame_sign_bias);
  1474. mode_mv = mode_mv_sb[sign_bias];
  1475. best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
  1476. }
  1477. get_predictor_pointers(cpi, plane, recon_yoffset, recon_uvoffset);
  1478. *returnintra = INT_MAX;
  1479. /* Count of the number of MBs tested so far this frame */
  1480. x->mbs_tested_so_far++;
  1481. x->skip = 0;
  1482. for (mode_index = 0; mode_index < MAX_MODES; ++mode_index) {
  1483. int this_rd = INT_MAX;
  1484. int disable_skip = 0;
  1485. int other_cost = 0;
  1486. int this_ref_frame = ref_frame_map[vp8_ref_frame_order[mode_index]];
  1487. /* Test best rd so far against threshold for trying this mode. */
  1488. if (best_mode.rd <= x->rd_threshes[mode_index]) continue;
  1489. if (this_ref_frame < 0) continue;
  1490. /* These variables hold are rolling total cost and distortion for
  1491. * this mode
  1492. */
  1493. rd.rate2 = 0;
  1494. rd.distortion2 = 0;
  1495. this_mode = vp8_mode_order[mode_index];
  1496. x->e_mbd.mode_info_context->mbmi.mode = this_mode;
  1497. x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
  1498. /* Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
  1499. * unless ARNR filtering is enabled in which case we want
  1500. * an unfiltered alternative
  1501. */
  1502. if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0)) {
  1503. if (this_mode != ZEROMV ||
  1504. x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME) {
  1505. continue;
  1506. }
  1507. }
  1508. /* everything but intra */
  1509. if (x->e_mbd.mode_info_context->mbmi.ref_frame) {
  1510. assert(plane[this_ref_frame][0] != NULL &&
  1511. plane[this_ref_frame][1] != NULL &&
  1512. plane[this_ref_frame][2] != NULL);
  1513. x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
  1514. x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
  1515. x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
  1516. if (sign_bias != cpi->common.ref_frame_sign_bias[this_ref_frame]) {
  1517. sign_bias = cpi->common.ref_frame_sign_bias[this_ref_frame];
  1518. mode_mv = mode_mv_sb[sign_bias];
  1519. best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
  1520. }
  1521. }
  1522. /* Check to see if the testing frequency for this mode is at its
  1523. * max If so then prevent it from being tested and increase the
  1524. * threshold for its testing
  1525. */
  1526. if (x->mode_test_hit_counts[mode_index] &&
  1527. (cpi->mode_check_freq[mode_index] > 1)) {
  1528. if (x->mbs_tested_so_far <= cpi->mode_check_freq[mode_index] *
  1529. x->mode_test_hit_counts[mode_index]) {
  1530. /* Increase the threshold for coding this mode to make it
  1531. * less likely to be chosen
  1532. */
  1533. x->rd_thresh_mult[mode_index] += 4;
  1534. if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT) {
  1535. x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
  1536. }
  1537. x->rd_threshes[mode_index] =
  1538. (cpi->rd_baseline_thresh[mode_index] >> 7) *
  1539. x->rd_thresh_mult[mode_index];
  1540. continue;
  1541. }
  1542. }
  1543. /* We have now reached the point where we are going to test the
  1544. * current mode so increment the counter for the number of times
  1545. * it has been tested
  1546. */
  1547. x->mode_test_hit_counts[mode_index]++;
  1548. /* Experimental code. Special case for gf and arf zeromv modes.
  1549. * Increase zbin size to supress noise
  1550. */
  1551. if (x->zbin_mode_boost_enabled) {
  1552. if (this_ref_frame == INTRA_FRAME) {
  1553. x->zbin_mode_boost = 0;
  1554. } else {
  1555. if (vp8_mode_order[mode_index] == ZEROMV) {
  1556. if (this_ref_frame != LAST_FRAME) {
  1557. x->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST;
  1558. } else {
  1559. x->zbin_mode_boost = LF_ZEROMV_ZBIN_BOOST;
  1560. }
  1561. } else if (vp8_mode_order[mode_index] == SPLITMV) {
  1562. x->zbin_mode_boost = 0;
  1563. } else {
  1564. x->zbin_mode_boost = MV_ZBIN_BOOST;
  1565. }
  1566. }
  1567. vp8_update_zbin_extra(cpi, x);
  1568. }
  1569. if (!uv_intra_done && this_ref_frame == INTRA_FRAME) {
  1570. rd_pick_intra_mbuv_mode(x, &uv_intra_rate, &uv_intra_rate_tokenonly,
  1571. &uv_intra_distortion);
  1572. uv_intra_mode = x->e_mbd.mode_info_context->mbmi.uv_mode;
  1573. /*
  1574. * Total of the eobs is used later to further adjust rate2. Since uv
  1575. * block's intra eobs will be overwritten when we check inter modes,
  1576. * we need to save uv_intra_tteob here.
  1577. */
  1578. for (i = 16; i < 24; ++i) uv_intra_tteob += x->e_mbd.eobs[i];
  1579. uv_intra_done = 1;
  1580. }
  1581. switch (this_mode) {
  1582. case B_PRED: {
  1583. int tmp_rd;
  1584. /* Note the rate value returned here includes the cost of
  1585. * coding the BPRED mode: x->mbmode_cost[x->e_mbd.frame_type][BPRED]
  1586. */
  1587. int distortion;
  1588. tmp_rd = rd_pick_intra4x4mby_modes(x, &rate, &rd.rate_y, &distortion,
  1589. best_mode.yrd);
  1590. rd.rate2 += rate;
  1591. rd.distortion2 += distortion;
  1592. if (tmp_rd < best_mode.yrd) {
  1593. assert(uv_intra_done);
  1594. rd.rate2 += uv_intra_rate;
  1595. rd.rate_uv = uv_intra_rate_tokenonly;
  1596. rd.distortion2 += uv_intra_distortion;
  1597. rd.distortion_uv = uv_intra_distortion;
  1598. } else {
  1599. this_rd = INT_MAX;
  1600. disable_skip = 1;
  1601. }
  1602. break;
  1603. }
  1604. case SPLITMV: {
  1605. int tmp_rd;
  1606. int this_rd_thresh;
  1607. int distortion;
  1608. this_rd_thresh = (vp8_ref_frame_order[mode_index] == 1)
  1609. ? x->rd_threshes[THR_NEW1]
  1610. : x->rd_threshes[THR_NEW3];
  1611. this_rd_thresh = (vp8_ref_frame_order[mode_index] == 2)
  1612. ? x->rd_threshes[THR_NEW2]
  1613. : this_rd_thresh;
  1614. tmp_rd = vp8_rd_pick_best_mbsegmentation(
  1615. cpi, x, &best_ref_mv, best_mode.yrd, mdcounts, &rate, &rd.rate_y,
  1616. &distortion, this_rd_thresh);
  1617. rd.rate2 += rate;
  1618. rd.distortion2 += distortion;
  1619. /* If even the 'Y' rd value of split is higher than best so far
  1620. * then dont bother looking at UV
  1621. */
  1622. if (tmp_rd < best_mode.yrd) {
  1623. /* Now work out UV cost and add it in */
  1624. rd_inter4x4_uv(cpi, x, &rd.rate_uv, &rd.distortion_uv,
  1625. cpi->common.full_pixel);
  1626. rd.rate2 += rd.rate_uv;
  1627. rd.distortion2 += rd.distortion_uv;
  1628. } else {
  1629. this_rd = INT_MAX;
  1630. disable_skip = 1;
  1631. }
  1632. break;
  1633. }
  1634. case DC_PRED:
  1635. case V_PRED:
  1636. case H_PRED:
  1637. case TM_PRED: {
  1638. int distortion;
  1639. x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
  1640. vp8_build_intra_predictors_mby_s(
  1641. xd, xd->dst.y_buffer - xd->dst.y_stride, xd->dst.y_buffer - 1,
  1642. xd->dst.y_stride, xd->predictor, 16);
  1643. macro_block_yrd(x, &rd.rate_y, &distortion);
  1644. rd.rate2 += rd.rate_y;
  1645. rd.distortion2 += distortion;
  1646. rd.rate2 += x->mbmode_cost[x->e_mbd.frame_type]
  1647. [x->e_mbd.mode_info_context->mbmi.mode];
  1648. assert(uv_intra_done);
  1649. rd.rate2 += uv_intra_rate;
  1650. rd.rate_uv = uv_intra_rate_tokenonly;
  1651. rd.distortion2 += uv_intra_distortion;
  1652. rd.distortion_uv = uv_intra_distortion;
  1653. break;
  1654. }
  1655. case NEWMV: {
  1656. int thissme;
  1657. int bestsme = INT_MAX;
  1658. int step_param = cpi->sf.first_step;
  1659. int further_steps;
  1660. int n;
  1661. /* If last step (1-away) of n-step search doesn't pick the center point
  1662. as the best match, we will do a final 1-away diamond refining search
  1663. */
  1664. int do_refine = 1;
  1665. int sadpb = x->sadperbit16;
  1666. int_mv mvp_full;
  1667. int col_min = ((best_ref_mv.as_mv.col + 7) >> 3) - MAX_FULL_PEL_VAL;
  1668. int row_min = ((best_ref_mv.as_mv.row + 7) >> 3) - MAX_FULL_PEL_VAL;
  1669. int col_max = (best_ref_mv.as_mv.col >> 3) + MAX_FULL_PEL_VAL;
  1670. int row_max = (best_ref_mv.as_mv.row >> 3) + MAX_FULL_PEL_VAL;
  1671. int tmp_col_min = x->mv_col_min;
  1672. int tmp_col_max = x->mv_col_max;
  1673. int tmp_row_min = x->mv_row_min;
  1674. int tmp_row_max = x->mv_row_max;
  1675. if (!saddone) {
  1676. vp8_cal_sad(cpi, xd, x, recon_yoffset, &near_sadidx[0]);
  1677. saddone = 1;
  1678. }
  1679. vp8_mv_pred(cpi, &x->e_mbd, x->e_mbd.mode_info_context, &mvp,
  1680. x->e_mbd.mode_info_context->mbmi.ref_frame,
  1681. cpi->common.ref_frame_sign_bias, &sr, &near_sadidx[0]);
  1682. mvp_full.as_mv.col = mvp.as_mv.col >> 3;
  1683. mvp_full.as_mv.row = mvp.as_mv.row >> 3;
  1684. /* Get intersection of UMV window and valid MV window to
  1685. * reduce # of checks in diamond search.
  1686. */
  1687. if (x->mv_col_min < col_min) x->mv_col_min = col_min;
  1688. if (x->mv_col_max > col_max) x->mv_col_max = col_max;
  1689. if (x->mv_row_min < row_min) x->mv_row_min = row_min;
  1690. if (x->mv_row_max > row_max) x->mv_row_max = row_max;
  1691. /* adjust search range according to sr from mv prediction */
  1692. if (sr > step_param) step_param = sr;
  1693. /* Initial step/diamond search */
  1694. {
  1695. bestsme = cpi->diamond_search_sad(
  1696. x, b, d, &mvp_full, &d->bmi.mv, step_param, sadpb, &num00,
  1697. &cpi->fn_ptr[BLOCK_16X16], x->mvcost, &best_ref_mv);
  1698. mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
  1699. /* Further step/diamond searches as necessary */
  1700. further_steps = (cpi->sf.max_step_search_steps - 1) - step_param;
  1701. n = num00;
  1702. num00 = 0;
  1703. /* If there won't be more n-step search, check to see if refining
  1704. * search is needed. */
  1705. if (n > further_steps) do_refine = 0;
  1706. while (n < further_steps) {
  1707. n++;
  1708. if (num00) {
  1709. num00--;
  1710. } else {
  1711. thissme = cpi->diamond_search_sad(
  1712. x, b, d, &mvp_full, &d->bmi.mv, step_param + n, sadpb, &num00,
  1713. &cpi->fn_ptr[BLOCK_16X16], x->mvcost, &best_ref_mv);
  1714. /* check to see if refining search is needed. */
  1715. if (num00 > (further_steps - n)) do_refine = 0;
  1716. if (thissme < bestsme) {
  1717. bestsme = thissme;
  1718. mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
  1719. } else {
  1720. d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
  1721. }
  1722. }
  1723. }
  1724. }
  1725. /* final 1-away diamond refining search */
  1726. if (do_refine == 1) {
  1727. int search_range;
  1728. search_range = 8;
  1729. thissme = cpi->refining_search_sad(
  1730. x, b, d, &d->bmi.mv, sadpb, search_range,
  1731. &cpi->fn_ptr[BLOCK_16X16], x->mvcost, &best_ref_mv);
  1732. if (thissme < bestsme) {
  1733. bestsme = thissme;
  1734. mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
  1735. } else {
  1736. d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
  1737. }
  1738. }
  1739. x->mv_col_min = tmp_col_min;
  1740. x->mv_col_max = tmp_col_max;
  1741. x->mv_row_min = tmp_row_min;
  1742. x->mv_row_max = tmp_row_max;
  1743. if (bestsme < INT_MAX) {
  1744. int dis; /* TODO: use dis in distortion calculation later. */
  1745. unsigned int sse;
  1746. cpi->find_fractional_mv_step(
  1747. x, b, d, &d->bmi.mv, &best_ref_mv, x->errorperbit,
  1748. &cpi->fn_ptr[BLOCK_16X16], x->mvcost, &dis, &sse);
  1749. }
  1750. mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
  1751. /* Add the new motion vector cost to our rolling cost variable */
  1752. rd.rate2 +=
  1753. vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv, x->mvcost, 96);
  1754. }
  1755. // fall through
  1756. case NEARESTMV:
  1757. case NEARMV:
  1758. /* Clip "next_nearest" so that it does not extend to far out
  1759. * of image
  1760. */
  1761. vp8_clamp_mv2(&mode_mv[this_mode], xd);
  1762. /* Do not bother proceeding if the vector (from newmv, nearest
  1763. * or near) is 0,0 as this should then be coded using the zeromv
  1764. * mode.
  1765. */
  1766. if (((this_mode == NEARMV) || (this_mode == NEARESTMV)) &&
  1767. (mode_mv[this_mode].as_int == 0)) {
  1768. continue;
  1769. }
  1770. // fall through
  1771. case ZEROMV:
  1772. /* Trap vectors that reach beyond the UMV borders
  1773. * Note that ALL New MV, Nearest MV Near MV and Zero MV code
  1774. * drops through to this point because of the lack of break
  1775. * statements in the previous two cases.
  1776. */
  1777. if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) ||
  1778. ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) ||
  1779. ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) ||
  1780. ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max)) {
  1781. continue;
  1782. }
  1783. vp8_set_mbmode_and_mvs(x, this_mode, &mode_mv[this_mode]);
  1784. this_rd = evaluate_inter_mode_rd(mdcounts, &rd, &disable_skip, cpi, x);
  1785. break;
  1786. default: break;
  1787. }
  1788. this_rd =
  1789. calculate_final_rd_costs(this_rd, &rd, &other_cost, disable_skip,
  1790. uv_intra_tteob, intra_rd_penalty, cpi, x);
  1791. /* Keep record of best intra distortion */
  1792. if ((x->e_mbd.mode_info_context->mbmi.ref_frame == INTRA_FRAME) &&
  1793. (this_rd < best_mode.intra_rd)) {
  1794. best_mode.intra_rd = this_rd;
  1795. *returnintra = rd.distortion2;
  1796. }
  1797. #if CONFIG_TEMPORAL_DENOISING
  1798. if (cpi->oxcf.noise_sensitivity) {
  1799. unsigned int sse;
  1800. vp8_get_inter_mbpred_error(x, &cpi->fn_ptr[BLOCK_16X16], &sse,
  1801. mode_mv[this_mode]);
  1802. if (sse < best_rd_sse) best_rd_sse = sse;
  1803. /* Store for later use by denoiser. */
  1804. if (this_mode == ZEROMV && sse < zero_mv_sse) {
  1805. zero_mv_sse = sse;
  1806. x->best_zeromv_reference_frame =
  1807. x->e_mbd.mode_info_context->mbmi.ref_frame;
  1808. }
  1809. /* Store the best NEWMV in x for later use in the denoiser. */
  1810. if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV && sse < best_sse) {
  1811. best_sse = sse;
  1812. vp8_get_inter_mbpred_error(x, &cpi->fn_ptr[BLOCK_16X16], &best_sse,
  1813. mode_mv[this_mode]);
  1814. x->best_sse_inter_mode = NEWMV;
  1815. x->best_sse_mv = x->e_mbd.mode_info_context->mbmi.mv;
  1816. x->need_to_clamp_best_mvs =
  1817. x->e_mbd.mode_info_context->mbmi.need_to_clamp_mvs;
  1818. x->best_reference_frame = x->e_mbd.mode_info_context->mbmi.ref_frame;
  1819. }
  1820. }
  1821. #endif
  1822. /* Did this mode help.. i.i is it the new best mode */
  1823. if (this_rd < best_mode.rd || x->skip) {
  1824. /* Note index of best mode so far */
  1825. best_mode_index = mode_index;
  1826. *returnrate = rd.rate2;
  1827. *returndistortion = rd.distortion2;
  1828. if (this_mode <= B_PRED) {
  1829. x->e_mbd.mode_info_context->mbmi.uv_mode = uv_intra_mode;
  1830. /* required for left and above block mv */
  1831. x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
  1832. }
  1833. update_best_mode(&best_mode, this_rd, &rd, other_cost, x);
  1834. /* Testing this mode gave rise to an improvement in best error
  1835. * score. Lower threshold a bit for next time
  1836. */
  1837. x->rd_thresh_mult[mode_index] =
  1838. (x->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2))
  1839. ? x->rd_thresh_mult[mode_index] - 2
  1840. : MIN_THRESHMULT;
  1841. }
  1842. /* If the mode did not help improve the best error case then raise
  1843. * the threshold for testing that mode next time around.
  1844. */
  1845. else {
  1846. x->rd_thresh_mult[mode_index] += 4;
  1847. if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT) {
  1848. x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
  1849. }
  1850. }
  1851. x->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) *
  1852. x->rd_thresh_mult[mode_index];
  1853. if (x->skip) break;
  1854. }
  1855. /* Reduce the activation RD thresholds for the best choice mode */
  1856. if ((cpi->rd_baseline_thresh[best_mode_index] > 0) &&
  1857. (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2))) {
  1858. int best_adjustment = (x->rd_thresh_mult[best_mode_index] >> 2);
  1859. x->rd_thresh_mult[best_mode_index] =
  1860. (x->rd_thresh_mult[best_mode_index] >=
  1861. (MIN_THRESHMULT + best_adjustment))
  1862. ? x->rd_thresh_mult[best_mode_index] - best_adjustment
  1863. : MIN_THRESHMULT;
  1864. x->rd_threshes[best_mode_index] =
  1865. (cpi->rd_baseline_thresh[best_mode_index] >> 7) *
  1866. x->rd_thresh_mult[best_mode_index];
  1867. }
  1868. #if CONFIG_TEMPORAL_DENOISING
  1869. if (cpi->oxcf.noise_sensitivity) {
  1870. int block_index = mb_row * cpi->common.mb_cols + mb_col;
  1871. if (x->best_sse_inter_mode == DC_PRED) {
  1872. /* No best MV found. */
  1873. x->best_sse_inter_mode = best_mode.mbmode.mode;
  1874. x->best_sse_mv = best_mode.mbmode.mv;
  1875. x->need_to_clamp_best_mvs = best_mode.mbmode.need_to_clamp_mvs;
  1876. x->best_reference_frame = best_mode.mbmode.ref_frame;
  1877. best_sse = best_rd_sse;
  1878. }
  1879. vp8_denoiser_denoise_mb(&cpi->denoiser, x, best_sse, zero_mv_sse,
  1880. recon_yoffset, recon_uvoffset, &cpi->common.lf_info,
  1881. mb_row, mb_col, block_index, 0);
  1882. /* Reevaluate ZEROMV after denoising. */
  1883. if (best_mode.mbmode.ref_frame == INTRA_FRAME &&
  1884. x->best_zeromv_reference_frame != INTRA_FRAME) {
  1885. int this_rd = INT_MAX;
  1886. int disable_skip = 0;
  1887. int other_cost = 0;
  1888. int this_ref_frame = x->best_zeromv_reference_frame;
  1889. rd.rate2 =
  1890. x->ref_frame_cost[this_ref_frame] + vp8_cost_mv_ref(ZEROMV, mdcounts);
  1891. rd.distortion2 = 0;
  1892. /* set up the proper prediction buffers for the frame */
  1893. x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
  1894. x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
  1895. x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
  1896. x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
  1897. x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
  1898. x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
  1899. x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
  1900. this_rd = evaluate_inter_mode_rd(mdcounts, &rd, &disable_skip, cpi, x);
  1901. this_rd =
  1902. calculate_final_rd_costs(this_rd, &rd, &other_cost, disable_skip,
  1903. uv_intra_tteob, intra_rd_penalty, cpi, x);
  1904. if (this_rd < best_mode.rd || x->skip) {
  1905. *returnrate = rd.rate2;
  1906. *returndistortion = rd.distortion2;
  1907. update_best_mode(&best_mode, this_rd, &rd, other_cost, x);
  1908. }
  1909. }
  1910. }
  1911. #endif
  1912. if (cpi->is_src_frame_alt_ref &&
  1913. (best_mode.mbmode.mode != ZEROMV ||
  1914. best_mode.mbmode.ref_frame != ALTREF_FRAME)) {
  1915. x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
  1916. x->e_mbd.mode_info_context->mbmi.ref_frame = ALTREF_FRAME;
  1917. x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
  1918. x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
  1919. x->e_mbd.mode_info_context->mbmi.mb_skip_coeff =
  1920. (cpi->common.mb_no_coeff_skip);
  1921. x->e_mbd.mode_info_context->mbmi.partitioning = 0;
  1922. return;
  1923. }
  1924. /* macroblock modes */
  1925. memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mode.mbmode,
  1926. sizeof(MB_MODE_INFO));
  1927. if (best_mode.mbmode.mode == B_PRED) {
  1928. for (i = 0; i < 16; ++i) {
  1929. xd->mode_info_context->bmi[i].as_mode = best_mode.bmodes[i].as_mode;
  1930. }
  1931. }
  1932. if (best_mode.mbmode.mode == SPLITMV) {
  1933. for (i = 0; i < 16; ++i) {
  1934. xd->mode_info_context->bmi[i].mv.as_int = best_mode.bmodes[i].mv.as_int;
  1935. }
  1936. memcpy(x->partition_info, &best_mode.partition, sizeof(PARTITION_INFO));
  1937. x->e_mbd.mode_info_context->mbmi.mv.as_int =
  1938. x->partition_info->bmi[15].mv.as_int;
  1939. }
  1940. if (sign_bias !=
  1941. cpi->common.ref_frame_sign_bias[xd->mode_info_context->mbmi.ref_frame]) {
  1942. best_ref_mv.as_int = best_ref_mv_sb[!sign_bias].as_int;
  1943. }
  1944. rd_update_mvcount(x, &best_ref_mv);
  1945. }
  1946. void vp8_rd_pick_intra_mode(MACROBLOCK *x, int *rate) {
  1947. int error4x4, error16x16;
  1948. int rate4x4, rate16x16 = 0, rateuv;
  1949. int dist4x4, dist16x16, distuv;
  1950. int rate_;
  1951. int rate4x4_tokenonly = 0;
  1952. int rate16x16_tokenonly = 0;
  1953. int rateuv_tokenonly = 0;
  1954. x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
  1955. rd_pick_intra_mbuv_mode(x, &rateuv, &rateuv_tokenonly, &distuv);
  1956. rate_ = rateuv;
  1957. error16x16 = rd_pick_intra16x16mby_mode(x, &rate16x16, &rate16x16_tokenonly,
  1958. &dist16x16);
  1959. error4x4 = rd_pick_intra4x4mby_modes(x, &rate4x4, &rate4x4_tokenonly,
  1960. &dist4x4, error16x16);
  1961. if (error4x4 < error16x16) {
  1962. x->e_mbd.mode_info_context->mbmi.mode = B_PRED;
  1963. rate_ += rate4x4;
  1964. } else {
  1965. rate_ += rate16x16;
  1966. }
  1967. *rate = rate_;
  1968. }