vp9_firstpass.c 131 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511
  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 <limits.h>
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include "./vpx_dsp_rtcd.h"
  14. #include "./vpx_scale_rtcd.h"
  15. #include "vpx_dsp/vpx_dsp_common.h"
  16. #include "vpx_mem/vpx_mem.h"
  17. #include "vpx_ports/mem.h"
  18. #include "vpx_ports/system_state.h"
  19. #include "vpx_scale/vpx_scale.h"
  20. #include "vpx_scale/yv12config.h"
  21. #include "vp9/common/vp9_entropymv.h"
  22. #include "vp9/common/vp9_quant_common.h"
  23. #include "vp9/common/vp9_reconinter.h" // vp9_setup_dst_planes()
  24. #include "vp9/encoder/vp9_aq_variance.h"
  25. #include "vp9/encoder/vp9_block.h"
  26. #include "vp9/encoder/vp9_encodeframe.h"
  27. #include "vp9/encoder/vp9_encodemb.h"
  28. #include "vp9/encoder/vp9_encodemv.h"
  29. #include "vp9/encoder/vp9_encoder.h"
  30. #include "vp9/encoder/vp9_ethread.h"
  31. #include "vp9/encoder/vp9_extend.h"
  32. #include "vp9/encoder/vp9_firstpass.h"
  33. #include "vp9/encoder/vp9_mcomp.h"
  34. #include "vp9/encoder/vp9_quantize.h"
  35. #include "vp9/encoder/vp9_rd.h"
  36. #include "vpx_dsp/variance.h"
  37. #define OUTPUT_FPF 0
  38. #define ARF_STATS_OUTPUT 0
  39. #define COMPLEXITY_STATS_OUTPUT 0
  40. #define FIRST_PASS_Q 10.0
  41. #define NORMAL_BOOST 100
  42. #define MIN_ARF_GF_BOOST 250
  43. #define MIN_DECAY_FACTOR 0.01
  44. #define NEW_MV_MODE_PENALTY 32
  45. #define DARK_THRESH 64
  46. #define LOW_I_THRESH 24000
  47. #define NCOUNT_INTRA_THRESH 8192
  48. #define NCOUNT_INTRA_FACTOR 3
  49. #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x)-0.000001 : (x) + 0.000001)
  50. #if ARF_STATS_OUTPUT
  51. unsigned int arf_count = 0;
  52. #endif
  53. // Resets the first pass file to the given position using a relative seek from
  54. // the current position.
  55. static void reset_fpf_position(TWO_PASS *p, const FIRSTPASS_STATS *position) {
  56. p->stats_in = position;
  57. }
  58. // Read frame stats at an offset from the current position.
  59. static const FIRSTPASS_STATS *read_frame_stats(const TWO_PASS *p, int offset) {
  60. if ((offset >= 0 && p->stats_in + offset >= p->stats_in_end) ||
  61. (offset < 0 && p->stats_in + offset < p->stats_in_start)) {
  62. return NULL;
  63. }
  64. return &p->stats_in[offset];
  65. }
  66. static int input_stats(TWO_PASS *p, FIRSTPASS_STATS *fps) {
  67. if (p->stats_in >= p->stats_in_end) return EOF;
  68. *fps = *p->stats_in;
  69. ++p->stats_in;
  70. return 1;
  71. }
  72. static void output_stats(FIRSTPASS_STATS *stats,
  73. struct vpx_codec_pkt_list *pktlist) {
  74. struct vpx_codec_cx_pkt pkt;
  75. pkt.kind = VPX_CODEC_STATS_PKT;
  76. pkt.data.twopass_stats.buf = stats;
  77. pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
  78. vpx_codec_pkt_list_add(pktlist, &pkt);
  79. // TEMP debug code
  80. #if OUTPUT_FPF
  81. {
  82. FILE *fpfile;
  83. fpfile = fopen("firstpass.stt", "a");
  84. fprintf(fpfile,
  85. "%12.0lf %12.4lf %12.2lf %12.2lf %12.2lf %12.0lf %12.4lf %12.4lf"
  86. "%12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf"
  87. "%12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.0lf %12.4lf %12.0lf"
  88. "%12.4lf"
  89. "\n",
  90. stats->frame, stats->weight, stats->intra_error, stats->coded_error,
  91. stats->sr_coded_error, stats->frame_noise_energy, stats->pcnt_inter,
  92. stats->pcnt_motion, stats->pcnt_second_ref, stats->pcnt_neutral,
  93. stats->pcnt_intra_low, stats->pcnt_intra_high,
  94. stats->intra_skip_pct, stats->intra_smooth_pct,
  95. stats->inactive_zone_rows, stats->inactive_zone_cols, stats->MVr,
  96. stats->mvr_abs, stats->MVc, stats->mvc_abs, stats->MVrv,
  97. stats->MVcv, stats->mv_in_out_count, stats->count, stats->duration);
  98. fclose(fpfile);
  99. }
  100. #endif
  101. }
  102. #if CONFIG_FP_MB_STATS
  103. static void output_fpmb_stats(uint8_t *this_frame_mb_stats, VP9_COMMON *cm,
  104. struct vpx_codec_pkt_list *pktlist) {
  105. struct vpx_codec_cx_pkt pkt;
  106. pkt.kind = VPX_CODEC_FPMB_STATS_PKT;
  107. pkt.data.firstpass_mb_stats.buf = this_frame_mb_stats;
  108. pkt.data.firstpass_mb_stats.sz = cm->initial_mbs * sizeof(uint8_t);
  109. vpx_codec_pkt_list_add(pktlist, &pkt);
  110. }
  111. #endif
  112. static void zero_stats(FIRSTPASS_STATS *section) {
  113. section->frame = 0.0;
  114. section->weight = 0.0;
  115. section->intra_error = 0.0;
  116. section->coded_error = 0.0;
  117. section->sr_coded_error = 0.0;
  118. section->frame_noise_energy = 0.0;
  119. section->pcnt_inter = 0.0;
  120. section->pcnt_motion = 0.0;
  121. section->pcnt_second_ref = 0.0;
  122. section->pcnt_neutral = 0.0;
  123. section->intra_skip_pct = 0.0;
  124. section->intra_smooth_pct = 0.0;
  125. section->pcnt_intra_low = 0.0;
  126. section->pcnt_intra_high = 0.0;
  127. section->inactive_zone_rows = 0.0;
  128. section->inactive_zone_cols = 0.0;
  129. section->MVr = 0.0;
  130. section->mvr_abs = 0.0;
  131. section->MVc = 0.0;
  132. section->mvc_abs = 0.0;
  133. section->MVrv = 0.0;
  134. section->MVcv = 0.0;
  135. section->mv_in_out_count = 0.0;
  136. section->count = 0.0;
  137. section->duration = 1.0;
  138. section->spatial_layer_id = 0;
  139. }
  140. static void accumulate_stats(FIRSTPASS_STATS *section,
  141. const FIRSTPASS_STATS *frame) {
  142. section->frame += frame->frame;
  143. section->weight += frame->weight;
  144. section->spatial_layer_id = frame->spatial_layer_id;
  145. section->intra_error += frame->intra_error;
  146. section->coded_error += frame->coded_error;
  147. section->sr_coded_error += frame->sr_coded_error;
  148. section->frame_noise_energy += frame->frame_noise_energy;
  149. section->pcnt_inter += frame->pcnt_inter;
  150. section->pcnt_motion += frame->pcnt_motion;
  151. section->pcnt_second_ref += frame->pcnt_second_ref;
  152. section->pcnt_neutral += frame->pcnt_neutral;
  153. section->intra_skip_pct += frame->intra_skip_pct;
  154. section->intra_smooth_pct += frame->intra_smooth_pct;
  155. section->pcnt_intra_low += frame->pcnt_intra_low;
  156. section->pcnt_intra_high += frame->pcnt_intra_high;
  157. section->inactive_zone_rows += frame->inactive_zone_rows;
  158. section->inactive_zone_cols += frame->inactive_zone_cols;
  159. section->MVr += frame->MVr;
  160. section->mvr_abs += frame->mvr_abs;
  161. section->MVc += frame->MVc;
  162. section->mvc_abs += frame->mvc_abs;
  163. section->MVrv += frame->MVrv;
  164. section->MVcv += frame->MVcv;
  165. section->mv_in_out_count += frame->mv_in_out_count;
  166. section->count += frame->count;
  167. section->duration += frame->duration;
  168. }
  169. static void subtract_stats(FIRSTPASS_STATS *section,
  170. const FIRSTPASS_STATS *frame) {
  171. section->frame -= frame->frame;
  172. section->weight -= frame->weight;
  173. section->intra_error -= frame->intra_error;
  174. section->coded_error -= frame->coded_error;
  175. section->sr_coded_error -= frame->sr_coded_error;
  176. section->frame_noise_energy -= frame->frame_noise_energy;
  177. section->pcnt_inter -= frame->pcnt_inter;
  178. section->pcnt_motion -= frame->pcnt_motion;
  179. section->pcnt_second_ref -= frame->pcnt_second_ref;
  180. section->pcnt_neutral -= frame->pcnt_neutral;
  181. section->intra_skip_pct -= frame->intra_skip_pct;
  182. section->intra_smooth_pct -= frame->intra_smooth_pct;
  183. section->pcnt_intra_low -= frame->pcnt_intra_low;
  184. section->pcnt_intra_high -= frame->pcnt_intra_high;
  185. section->inactive_zone_rows -= frame->inactive_zone_rows;
  186. section->inactive_zone_cols -= frame->inactive_zone_cols;
  187. section->MVr -= frame->MVr;
  188. section->mvr_abs -= frame->mvr_abs;
  189. section->MVc -= frame->MVc;
  190. section->mvc_abs -= frame->mvc_abs;
  191. section->MVrv -= frame->MVrv;
  192. section->MVcv -= frame->MVcv;
  193. section->mv_in_out_count -= frame->mv_in_out_count;
  194. section->count -= frame->count;
  195. section->duration -= frame->duration;
  196. }
  197. // Calculate an active area of the image that discounts formatting
  198. // bars and partially discounts other 0 energy areas.
  199. #define MIN_ACTIVE_AREA 0.5
  200. #define MAX_ACTIVE_AREA 1.0
  201. static double calculate_active_area(const VP9_COMP *cpi,
  202. const FIRSTPASS_STATS *this_frame) {
  203. double active_pct;
  204. active_pct =
  205. 1.0 -
  206. ((this_frame->intra_skip_pct / 2) +
  207. ((this_frame->inactive_zone_rows * 2) / (double)cpi->common.mb_rows));
  208. return fclamp(active_pct, MIN_ACTIVE_AREA, MAX_ACTIVE_AREA);
  209. }
  210. // Get the average weighted error for the clip (or corpus)
  211. static double get_distribution_av_err(VP9_COMP *cpi, TWO_PASS *const twopass) {
  212. const double av_weight =
  213. twopass->total_stats.weight / twopass->total_stats.count;
  214. if (cpi->oxcf.vbr_corpus_complexity)
  215. return av_weight * twopass->mean_mod_score;
  216. else
  217. return (twopass->total_stats.coded_error * av_weight) /
  218. twopass->total_stats.count;
  219. }
  220. #define ACT_AREA_CORRECTION 0.5
  221. // Calculate a modified Error used in distributing bits between easier and
  222. // harder frames.
  223. static double calculate_mod_frame_score(const VP9_COMP *cpi,
  224. const VP9EncoderConfig *oxcf,
  225. const FIRSTPASS_STATS *this_frame,
  226. const double av_err) {
  227. double modified_score =
  228. av_err * pow(this_frame->coded_error * this_frame->weight /
  229. DOUBLE_DIVIDE_CHECK(av_err),
  230. oxcf->two_pass_vbrbias / 100.0);
  231. // Correction for active area. Frames with a reduced active area
  232. // (eg due to formatting bars) have a higher error per mb for the
  233. // remaining active MBs. The correction here assumes that coding
  234. // 0.5N blocks of complexity 2X is a little easier than coding N
  235. // blocks of complexity X.
  236. modified_score *=
  237. pow(calculate_active_area(cpi, this_frame), ACT_AREA_CORRECTION);
  238. return modified_score;
  239. }
  240. static double calculate_norm_frame_score(const VP9_COMP *cpi,
  241. const TWO_PASS *twopass,
  242. const VP9EncoderConfig *oxcf,
  243. const FIRSTPASS_STATS *this_frame,
  244. const double av_err) {
  245. double modified_score =
  246. av_err * pow(this_frame->coded_error * this_frame->weight /
  247. DOUBLE_DIVIDE_CHECK(av_err),
  248. oxcf->two_pass_vbrbias / 100.0);
  249. const double min_score = (double)(oxcf->two_pass_vbrmin_section) / 100.0;
  250. const double max_score = (double)(oxcf->two_pass_vbrmax_section) / 100.0;
  251. // Correction for active area. Frames with a reduced active area
  252. // (eg due to formatting bars) have a higher error per mb for the
  253. // remaining active MBs. The correction here assumes that coding
  254. // 0.5N blocks of complexity 2X is a little easier than coding N
  255. // blocks of complexity X.
  256. modified_score *=
  257. pow(calculate_active_area(cpi, this_frame), ACT_AREA_CORRECTION);
  258. // Normalize to a midpoint score.
  259. modified_score /= DOUBLE_DIVIDE_CHECK(twopass->mean_mod_score);
  260. return fclamp(modified_score, min_score, max_score);
  261. }
  262. // This function returns the maximum target rate per frame.
  263. static int frame_max_bits(const RATE_CONTROL *rc,
  264. const VP9EncoderConfig *oxcf) {
  265. int64_t max_bits = ((int64_t)rc->avg_frame_bandwidth *
  266. (int64_t)oxcf->two_pass_vbrmax_section) /
  267. 100;
  268. if (max_bits < 0)
  269. max_bits = 0;
  270. else if (max_bits > rc->max_frame_bandwidth)
  271. max_bits = rc->max_frame_bandwidth;
  272. return (int)max_bits;
  273. }
  274. void vp9_init_first_pass(VP9_COMP *cpi) {
  275. zero_stats(&cpi->twopass.total_stats);
  276. }
  277. void vp9_end_first_pass(VP9_COMP *cpi) {
  278. output_stats(&cpi->twopass.total_stats, cpi->output_pkt_list);
  279. vpx_free(cpi->twopass.fp_mb_float_stats);
  280. cpi->twopass.fp_mb_float_stats = NULL;
  281. }
  282. static vpx_variance_fn_t get_block_variance_fn(BLOCK_SIZE bsize) {
  283. switch (bsize) {
  284. case BLOCK_8X8: return vpx_mse8x8;
  285. case BLOCK_16X8: return vpx_mse16x8;
  286. case BLOCK_8X16: return vpx_mse8x16;
  287. default: return vpx_mse16x16;
  288. }
  289. }
  290. static unsigned int get_prediction_error(BLOCK_SIZE bsize,
  291. const struct buf_2d *src,
  292. const struct buf_2d *ref) {
  293. unsigned int sse;
  294. const vpx_variance_fn_t fn = get_block_variance_fn(bsize);
  295. fn(src->buf, src->stride, ref->buf, ref->stride, &sse);
  296. return sse;
  297. }
  298. #if CONFIG_VP9_HIGHBITDEPTH
  299. static vpx_variance_fn_t highbd_get_block_variance_fn(BLOCK_SIZE bsize,
  300. int bd) {
  301. switch (bd) {
  302. default:
  303. switch (bsize) {
  304. case BLOCK_8X8: return vpx_highbd_8_mse8x8;
  305. case BLOCK_16X8: return vpx_highbd_8_mse16x8;
  306. case BLOCK_8X16: return vpx_highbd_8_mse8x16;
  307. default: return vpx_highbd_8_mse16x16;
  308. }
  309. break;
  310. case 10:
  311. switch (bsize) {
  312. case BLOCK_8X8: return vpx_highbd_10_mse8x8;
  313. case BLOCK_16X8: return vpx_highbd_10_mse16x8;
  314. case BLOCK_8X16: return vpx_highbd_10_mse8x16;
  315. default: return vpx_highbd_10_mse16x16;
  316. }
  317. break;
  318. case 12:
  319. switch (bsize) {
  320. case BLOCK_8X8: return vpx_highbd_12_mse8x8;
  321. case BLOCK_16X8: return vpx_highbd_12_mse16x8;
  322. case BLOCK_8X16: return vpx_highbd_12_mse8x16;
  323. default: return vpx_highbd_12_mse16x16;
  324. }
  325. break;
  326. }
  327. }
  328. static unsigned int highbd_get_prediction_error(BLOCK_SIZE bsize,
  329. const struct buf_2d *src,
  330. const struct buf_2d *ref,
  331. int bd) {
  332. unsigned int sse;
  333. const vpx_variance_fn_t fn = highbd_get_block_variance_fn(bsize, bd);
  334. fn(src->buf, src->stride, ref->buf, ref->stride, &sse);
  335. return sse;
  336. }
  337. #endif // CONFIG_VP9_HIGHBITDEPTH
  338. // Refine the motion search range according to the frame dimension
  339. // for first pass test.
  340. static int get_search_range(const VP9_COMP *cpi) {
  341. int sr = 0;
  342. const int dim = VPXMIN(cpi->initial_width, cpi->initial_height);
  343. while ((dim << sr) < MAX_FULL_PEL_VAL) ++sr;
  344. return sr;
  345. }
  346. static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
  347. const MV *ref_mv, MV *best_mv,
  348. int *best_motion_err) {
  349. MACROBLOCKD *const xd = &x->e_mbd;
  350. MV tmp_mv = { 0, 0 };
  351. MV ref_mv_full = { ref_mv->row >> 3, ref_mv->col >> 3 };
  352. int num00, tmp_err, n;
  353. const BLOCK_SIZE bsize = xd->mi[0]->sb_type;
  354. vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[bsize];
  355. const int new_mv_mode_penalty = NEW_MV_MODE_PENALTY;
  356. int step_param = 3;
  357. int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
  358. const int sr = get_search_range(cpi);
  359. step_param += sr;
  360. further_steps -= sr;
  361. // Override the default variance function to use MSE.
  362. v_fn_ptr.vf = get_block_variance_fn(bsize);
  363. #if CONFIG_VP9_HIGHBITDEPTH
  364. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  365. v_fn_ptr.vf = highbd_get_block_variance_fn(bsize, xd->bd);
  366. }
  367. #endif // CONFIG_VP9_HIGHBITDEPTH
  368. // Center the initial step/diamond search on best mv.
  369. tmp_err = cpi->diamond_search_sad(x, &cpi->ss_cfg, &ref_mv_full, &tmp_mv,
  370. step_param, x->sadperbit16, &num00,
  371. &v_fn_ptr, ref_mv);
  372. if (tmp_err < INT_MAX)
  373. tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1);
  374. if (tmp_err < INT_MAX - new_mv_mode_penalty) tmp_err += new_mv_mode_penalty;
  375. if (tmp_err < *best_motion_err) {
  376. *best_motion_err = tmp_err;
  377. *best_mv = tmp_mv;
  378. }
  379. // Carry out further step/diamond searches as necessary.
  380. n = num00;
  381. num00 = 0;
  382. while (n < further_steps) {
  383. ++n;
  384. if (num00) {
  385. --num00;
  386. } else {
  387. tmp_err = cpi->diamond_search_sad(x, &cpi->ss_cfg, &ref_mv_full, &tmp_mv,
  388. step_param + n, x->sadperbit16, &num00,
  389. &v_fn_ptr, ref_mv);
  390. if (tmp_err < INT_MAX)
  391. tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1);
  392. if (tmp_err < INT_MAX - new_mv_mode_penalty)
  393. tmp_err += new_mv_mode_penalty;
  394. if (tmp_err < *best_motion_err) {
  395. *best_motion_err = tmp_err;
  396. *best_mv = tmp_mv;
  397. }
  398. }
  399. }
  400. }
  401. static BLOCK_SIZE get_bsize(const VP9_COMMON *cm, int mb_row, int mb_col) {
  402. if (2 * mb_col + 1 < cm->mi_cols) {
  403. return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_16X16 : BLOCK_16X8;
  404. } else {
  405. return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_8X16 : BLOCK_8X8;
  406. }
  407. }
  408. static int find_fp_qindex(vpx_bit_depth_t bit_depth) {
  409. int i;
  410. for (i = 0; i < QINDEX_RANGE; ++i)
  411. if (vp9_convert_qindex_to_q(i, bit_depth) >= FIRST_PASS_Q) break;
  412. if (i == QINDEX_RANGE) i--;
  413. return i;
  414. }
  415. static void set_first_pass_params(VP9_COMP *cpi) {
  416. VP9_COMMON *const cm = &cpi->common;
  417. if (!cpi->refresh_alt_ref_frame &&
  418. (cm->current_video_frame == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY))) {
  419. cm->frame_type = KEY_FRAME;
  420. } else {
  421. cm->frame_type = INTER_FRAME;
  422. }
  423. // Do not use periodic key frames.
  424. cpi->rc.frames_to_key = INT_MAX;
  425. }
  426. // Scale an sse threshold to account for 8/10/12 bit.
  427. static int scale_sse_threshold(VP9_COMMON *cm, int thresh) {
  428. int ret_val = thresh;
  429. #if CONFIG_VP9_HIGHBITDEPTH
  430. if (cm->use_highbitdepth) {
  431. switch (cm->bit_depth) {
  432. case VPX_BITS_8: ret_val = thresh; break;
  433. case VPX_BITS_10: ret_val = thresh << 4; break;
  434. default:
  435. assert(cm->bit_depth == VPX_BITS_12);
  436. ret_val = thresh << 8;
  437. break;
  438. }
  439. }
  440. #else
  441. (void)cm;
  442. #endif // CONFIG_VP9_HIGHBITDEPTH
  443. return ret_val;
  444. }
  445. // This threshold is used to track blocks where to all intents and purposes
  446. // the intra prediction error 0. Though the metric we test against
  447. // is technically a sse we are mainly interested in blocks where all the pixels
  448. // in the 8 bit domain have an error of <= 1 (where error = sse) so a
  449. // linear scaling for 10 and 12 bit gives similar results.
  450. #define UL_INTRA_THRESH 50
  451. static int get_ul_intra_threshold(VP9_COMMON *cm) {
  452. int ret_val = UL_INTRA_THRESH;
  453. #if CONFIG_VP9_HIGHBITDEPTH
  454. if (cm->use_highbitdepth) {
  455. switch (cm->bit_depth) {
  456. case VPX_BITS_8: ret_val = UL_INTRA_THRESH; break;
  457. case VPX_BITS_10: ret_val = UL_INTRA_THRESH << 2; break;
  458. default:
  459. assert(cm->bit_depth == VPX_BITS_12);
  460. ret_val = UL_INTRA_THRESH << 4;
  461. break;
  462. }
  463. }
  464. #else
  465. (void)cm;
  466. #endif // CONFIG_VP9_HIGHBITDEPTH
  467. return ret_val;
  468. }
  469. #define SMOOTH_INTRA_THRESH 4000
  470. static int get_smooth_intra_threshold(VP9_COMMON *cm) {
  471. int ret_val = SMOOTH_INTRA_THRESH;
  472. #if CONFIG_VP9_HIGHBITDEPTH
  473. if (cm->use_highbitdepth) {
  474. switch (cm->bit_depth) {
  475. case VPX_BITS_8: ret_val = SMOOTH_INTRA_THRESH; break;
  476. case VPX_BITS_10: ret_val = SMOOTH_INTRA_THRESH << 4; break;
  477. default:
  478. assert(cm->bit_depth == VPX_BITS_12);
  479. ret_val = SMOOTH_INTRA_THRESH << 8;
  480. break;
  481. }
  482. }
  483. #else
  484. (void)cm;
  485. #endif // CONFIG_VP9_HIGHBITDEPTH
  486. return ret_val;
  487. }
  488. #define FP_DN_THRESH 8
  489. #define FP_MAX_DN_THRESH 24
  490. #define KERNEL_SIZE 3
  491. // Baseline Kernal weights for first pass noise metric
  492. static uint8_t fp_dn_kernal_3[KERNEL_SIZE * KERNEL_SIZE] = { 1, 2, 1, 2, 4,
  493. 2, 1, 2, 1 };
  494. // Estimate noise at a single point based on the impace of a spatial kernal
  495. // on the point value
  496. static int fp_estimate_point_noise(uint8_t *src_ptr, const int stride) {
  497. int sum_weight = 0;
  498. int sum_val = 0;
  499. int i, j;
  500. int max_diff = 0;
  501. int diff;
  502. int dn_diff;
  503. uint8_t *tmp_ptr;
  504. uint8_t *kernal_ptr;
  505. uint8_t dn_val;
  506. uint8_t centre_val = *src_ptr;
  507. kernal_ptr = fp_dn_kernal_3;
  508. // Apply the kernal
  509. tmp_ptr = src_ptr - stride - 1;
  510. for (i = 0; i < KERNEL_SIZE; ++i) {
  511. for (j = 0; j < KERNEL_SIZE; ++j) {
  512. diff = abs((int)centre_val - (int)tmp_ptr[j]);
  513. max_diff = VPXMAX(max_diff, diff);
  514. if (diff <= FP_DN_THRESH) {
  515. sum_weight += *kernal_ptr;
  516. sum_val += (int)tmp_ptr[j] * (int)*kernal_ptr;
  517. }
  518. ++kernal_ptr;
  519. }
  520. tmp_ptr += stride;
  521. }
  522. if (max_diff < FP_MAX_DN_THRESH)
  523. // Update the source value with the new filtered value
  524. dn_val = (sum_val + (sum_weight >> 1)) / sum_weight;
  525. else
  526. dn_val = *src_ptr;
  527. // return the noise energy as the square of the difference between the
  528. // denoised and raw value.
  529. dn_diff = (int)*src_ptr - (int)dn_val;
  530. return dn_diff * dn_diff;
  531. }
  532. #if CONFIG_VP9_HIGHBITDEPTH
  533. static int fp_highbd_estimate_point_noise(uint8_t *src_ptr, const int stride) {
  534. int sum_weight = 0;
  535. int sum_val = 0;
  536. int i, j;
  537. int max_diff = 0;
  538. int diff;
  539. int dn_diff;
  540. uint8_t *tmp_ptr;
  541. uint16_t *tmp_ptr16;
  542. uint8_t *kernal_ptr;
  543. uint16_t dn_val;
  544. uint16_t centre_val = *CONVERT_TO_SHORTPTR(src_ptr);
  545. kernal_ptr = fp_dn_kernal_3;
  546. // Apply the kernal
  547. tmp_ptr = src_ptr - stride - 1;
  548. for (i = 0; i < KERNEL_SIZE; ++i) {
  549. tmp_ptr16 = CONVERT_TO_SHORTPTR(tmp_ptr);
  550. for (j = 0; j < KERNEL_SIZE; ++j) {
  551. diff = abs((int)centre_val - (int)tmp_ptr16[j]);
  552. max_diff = VPXMAX(max_diff, diff);
  553. if (diff <= FP_DN_THRESH) {
  554. sum_weight += *kernal_ptr;
  555. sum_val += (int)tmp_ptr16[j] * (int)*kernal_ptr;
  556. }
  557. ++kernal_ptr;
  558. }
  559. tmp_ptr += stride;
  560. }
  561. if (max_diff < FP_MAX_DN_THRESH)
  562. // Update the source value with the new filtered value
  563. dn_val = (sum_val + (sum_weight >> 1)) / sum_weight;
  564. else
  565. dn_val = *CONVERT_TO_SHORTPTR(src_ptr);
  566. // return the noise energy as the square of the difference between the
  567. // denoised and raw value.
  568. dn_diff = (int)(*CONVERT_TO_SHORTPTR(src_ptr)) - (int)dn_val;
  569. return dn_diff * dn_diff;
  570. }
  571. #endif
  572. // Estimate noise for a block.
  573. static int fp_estimate_block_noise(MACROBLOCK *x, BLOCK_SIZE bsize) {
  574. #if CONFIG_VP9_HIGHBITDEPTH
  575. MACROBLOCKD *xd = &x->e_mbd;
  576. #endif
  577. uint8_t *src_ptr = &x->plane[0].src.buf[0];
  578. const int width = num_4x4_blocks_wide_lookup[bsize] * 4;
  579. const int height = num_4x4_blocks_high_lookup[bsize] * 4;
  580. int w, h;
  581. int stride = x->plane[0].src.stride;
  582. int block_noise = 0;
  583. // Sampled points to reduce cost overhead.
  584. for (h = 0; h < height; h += 2) {
  585. for (w = 0; w < width; w += 2) {
  586. #if CONFIG_VP9_HIGHBITDEPTH
  587. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
  588. block_noise += fp_highbd_estimate_point_noise(src_ptr, stride);
  589. else
  590. block_noise += fp_estimate_point_noise(src_ptr, stride);
  591. #else
  592. block_noise += fp_estimate_point_noise(src_ptr, stride);
  593. #endif
  594. ++src_ptr;
  595. }
  596. src_ptr += (stride - width);
  597. }
  598. return block_noise << 2; // Scale << 2 to account for sampling.
  599. }
  600. // This function is called to test the functionality of row based
  601. // multi-threading in unit tests for bit-exactness
  602. static void accumulate_floating_point_stats(VP9_COMP *cpi,
  603. TileDataEnc *first_tile_col) {
  604. VP9_COMMON *const cm = &cpi->common;
  605. int mb_row, mb_col;
  606. first_tile_col->fp_data.intra_factor = 0;
  607. first_tile_col->fp_data.brightness_factor = 0;
  608. first_tile_col->fp_data.neutral_count = 0;
  609. for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) {
  610. for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
  611. const int mb_index = mb_row * cm->mb_cols + mb_col;
  612. first_tile_col->fp_data.intra_factor +=
  613. cpi->twopass.fp_mb_float_stats[mb_index].frame_mb_intra_factor;
  614. first_tile_col->fp_data.brightness_factor +=
  615. cpi->twopass.fp_mb_float_stats[mb_index].frame_mb_brightness_factor;
  616. first_tile_col->fp_data.neutral_count +=
  617. cpi->twopass.fp_mb_float_stats[mb_index].frame_mb_neutral_count;
  618. }
  619. }
  620. }
  621. static void first_pass_stat_calc(VP9_COMP *cpi, FIRSTPASS_STATS *fps,
  622. FIRSTPASS_DATA *fp_acc_data) {
  623. VP9_COMMON *const cm = &cpi->common;
  624. // The minimum error here insures some bit allocation to frames even
  625. // in static regions. The allocation per MB declines for larger formats
  626. // where the typical "real" energy per MB also falls.
  627. // Initial estimate here uses sqrt(mbs) to define the min_err, where the
  628. // number of mbs is proportional to the image area.
  629. const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE) ? cpi->initial_mbs
  630. : cpi->common.MBs;
  631. const double min_err = 200 * sqrt(num_mbs);
  632. // Clamp the image start to rows/2. This number of rows is discarded top
  633. // and bottom as dead data so rows / 2 means the frame is blank.
  634. if ((fp_acc_data->image_data_start_row > cm->mb_rows / 2) ||
  635. (fp_acc_data->image_data_start_row == INVALID_ROW)) {
  636. fp_acc_data->image_data_start_row = cm->mb_rows / 2;
  637. }
  638. // Exclude any image dead zone
  639. if (fp_acc_data->image_data_start_row > 0) {
  640. fp_acc_data->intra_skip_count =
  641. VPXMAX(0, fp_acc_data->intra_skip_count -
  642. (fp_acc_data->image_data_start_row * cm->mb_cols * 2));
  643. }
  644. fp_acc_data->intra_factor = fp_acc_data->intra_factor / (double)num_mbs;
  645. fp_acc_data->brightness_factor =
  646. fp_acc_data->brightness_factor / (double)num_mbs;
  647. fps->weight = fp_acc_data->intra_factor * fp_acc_data->brightness_factor;
  648. fps->frame = cm->current_video_frame;
  649. fps->spatial_layer_id = cpi->svc.spatial_layer_id;
  650. fps->coded_error =
  651. ((double)(fp_acc_data->coded_error >> 8) + min_err) / num_mbs;
  652. fps->sr_coded_error =
  653. ((double)(fp_acc_data->sr_coded_error >> 8) + min_err) / num_mbs;
  654. fps->intra_error =
  655. ((double)(fp_acc_data->intra_error >> 8) + min_err) / num_mbs;
  656. fps->frame_noise_energy =
  657. (double)(fp_acc_data->frame_noise_energy) / (double)num_mbs;
  658. fps->count = 1.0;
  659. fps->pcnt_inter = (double)(fp_acc_data->intercount) / num_mbs;
  660. fps->pcnt_second_ref = (double)(fp_acc_data->second_ref_count) / num_mbs;
  661. fps->pcnt_neutral = (double)(fp_acc_data->neutral_count) / num_mbs;
  662. fps->pcnt_intra_low = (double)(fp_acc_data->intra_count_low) / num_mbs;
  663. fps->pcnt_intra_high = (double)(fp_acc_data->intra_count_high) / num_mbs;
  664. fps->intra_skip_pct = (double)(fp_acc_data->intra_skip_count) / num_mbs;
  665. fps->intra_smooth_pct = (double)(fp_acc_data->intra_smooth_count) / num_mbs;
  666. fps->inactive_zone_rows = (double)(fp_acc_data->image_data_start_row);
  667. // Currently set to 0 as most issues relate to letter boxing.
  668. fps->inactive_zone_cols = (double)0;
  669. if (fp_acc_data->mvcount > 0) {
  670. fps->MVr = (double)(fp_acc_data->sum_mvr) / fp_acc_data->mvcount;
  671. fps->mvr_abs = (double)(fp_acc_data->sum_mvr_abs) / fp_acc_data->mvcount;
  672. fps->MVc = (double)(fp_acc_data->sum_mvc) / fp_acc_data->mvcount;
  673. fps->mvc_abs = (double)(fp_acc_data->sum_mvc_abs) / fp_acc_data->mvcount;
  674. fps->MVrv = ((double)(fp_acc_data->sum_mvrs) -
  675. ((double)(fp_acc_data->sum_mvr) * (fp_acc_data->sum_mvr) /
  676. fp_acc_data->mvcount)) /
  677. fp_acc_data->mvcount;
  678. fps->MVcv = ((double)(fp_acc_data->sum_mvcs) -
  679. ((double)(fp_acc_data->sum_mvc) * (fp_acc_data->sum_mvc) /
  680. fp_acc_data->mvcount)) /
  681. fp_acc_data->mvcount;
  682. fps->mv_in_out_count =
  683. (double)(fp_acc_data->sum_in_vectors) / (fp_acc_data->mvcount * 2);
  684. fps->pcnt_motion = (double)(fp_acc_data->mvcount) / num_mbs;
  685. } else {
  686. fps->MVr = 0.0;
  687. fps->mvr_abs = 0.0;
  688. fps->MVc = 0.0;
  689. fps->mvc_abs = 0.0;
  690. fps->MVrv = 0.0;
  691. fps->MVcv = 0.0;
  692. fps->mv_in_out_count = 0.0;
  693. fps->pcnt_motion = 0.0;
  694. }
  695. }
  696. static void accumulate_fp_mb_row_stat(TileDataEnc *this_tile,
  697. FIRSTPASS_DATA *fp_acc_data) {
  698. this_tile->fp_data.intra_factor += fp_acc_data->intra_factor;
  699. this_tile->fp_data.brightness_factor += fp_acc_data->brightness_factor;
  700. this_tile->fp_data.coded_error += fp_acc_data->coded_error;
  701. this_tile->fp_data.sr_coded_error += fp_acc_data->sr_coded_error;
  702. this_tile->fp_data.frame_noise_energy += fp_acc_data->frame_noise_energy;
  703. this_tile->fp_data.intra_error += fp_acc_data->intra_error;
  704. this_tile->fp_data.intercount += fp_acc_data->intercount;
  705. this_tile->fp_data.second_ref_count += fp_acc_data->second_ref_count;
  706. this_tile->fp_data.neutral_count += fp_acc_data->neutral_count;
  707. this_tile->fp_data.intra_count_low += fp_acc_data->intra_count_low;
  708. this_tile->fp_data.intra_count_high += fp_acc_data->intra_count_high;
  709. this_tile->fp_data.intra_skip_count += fp_acc_data->intra_skip_count;
  710. this_tile->fp_data.mvcount += fp_acc_data->mvcount;
  711. this_tile->fp_data.sum_mvr += fp_acc_data->sum_mvr;
  712. this_tile->fp_data.sum_mvr_abs += fp_acc_data->sum_mvr_abs;
  713. this_tile->fp_data.sum_mvc += fp_acc_data->sum_mvc;
  714. this_tile->fp_data.sum_mvc_abs += fp_acc_data->sum_mvc_abs;
  715. this_tile->fp_data.sum_mvrs += fp_acc_data->sum_mvrs;
  716. this_tile->fp_data.sum_mvcs += fp_acc_data->sum_mvcs;
  717. this_tile->fp_data.sum_in_vectors += fp_acc_data->sum_in_vectors;
  718. this_tile->fp_data.intra_smooth_count += fp_acc_data->intra_smooth_count;
  719. this_tile->fp_data.image_data_start_row =
  720. VPXMIN(this_tile->fp_data.image_data_start_row,
  721. fp_acc_data->image_data_start_row) == INVALID_ROW
  722. ? VPXMAX(this_tile->fp_data.image_data_start_row,
  723. fp_acc_data->image_data_start_row)
  724. : VPXMIN(this_tile->fp_data.image_data_start_row,
  725. fp_acc_data->image_data_start_row);
  726. }
  727. #define NZ_MOTION_PENALTY 128
  728. #define INTRA_MODE_PENALTY 1024
  729. void vp9_first_pass_encode_tile_mb_row(VP9_COMP *cpi, ThreadData *td,
  730. FIRSTPASS_DATA *fp_acc_data,
  731. TileDataEnc *tile_data, MV *best_ref_mv,
  732. int mb_row) {
  733. int mb_col;
  734. MACROBLOCK *const x = &td->mb;
  735. VP9_COMMON *const cm = &cpi->common;
  736. MACROBLOCKD *const xd = &x->e_mbd;
  737. TileInfo tile = tile_data->tile_info;
  738. const int mb_col_start = ROUND_POWER_OF_TWO(tile.mi_col_start, 1);
  739. const int mb_col_end = ROUND_POWER_OF_TWO(tile.mi_col_end, 1);
  740. struct macroblock_plane *const p = x->plane;
  741. struct macroblockd_plane *const pd = xd->plane;
  742. const PICK_MODE_CONTEXT *ctx = &td->pc_root->none;
  743. int i, c;
  744. int num_mb_cols = get_num_cols(tile_data->tile_info, 1);
  745. int recon_yoffset, recon_uvoffset;
  746. const int intrapenalty = INTRA_MODE_PENALTY;
  747. const MV zero_mv = { 0, 0 };
  748. int recon_y_stride, recon_uv_stride, uv_mb_height;
  749. YV12_BUFFER_CONFIG *const lst_yv12 = get_ref_frame_buffer(cpi, LAST_FRAME);
  750. YV12_BUFFER_CONFIG *gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
  751. YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm);
  752. const YV12_BUFFER_CONFIG *first_ref_buf = lst_yv12;
  753. MODE_INFO mi_above, mi_left;
  754. double mb_intra_factor;
  755. double mb_brightness_factor;
  756. double mb_neutral_count;
  757. int scaled_low_intra_thresh = scale_sse_threshold(cm, LOW_I_THRESH);
  758. // First pass code requires valid last and new frame buffers.
  759. assert(new_yv12 != NULL);
  760. assert(frame_is_intra_only(cm) || (lst_yv12 != NULL));
  761. xd->mi = cm->mi_grid_visible + xd->mi_stride * (mb_row << 1) + mb_col_start;
  762. xd->mi[0] = cm->mi + xd->mi_stride * (mb_row << 1) + mb_col_start;
  763. for (i = 0; i < MAX_MB_PLANE; ++i) {
  764. p[i].coeff = ctx->coeff_pbuf[i][1];
  765. p[i].qcoeff = ctx->qcoeff_pbuf[i][1];
  766. pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
  767. p[i].eobs = ctx->eobs_pbuf[i][1];
  768. }
  769. recon_y_stride = new_yv12->y_stride;
  770. recon_uv_stride = new_yv12->uv_stride;
  771. uv_mb_height = 16 >> (new_yv12->y_height > new_yv12->uv_height);
  772. // Reset above block coeffs.
  773. recon_yoffset = (mb_row * recon_y_stride * 16) + mb_col_start * 16;
  774. recon_uvoffset =
  775. (mb_row * recon_uv_stride * uv_mb_height) + mb_col_start * uv_mb_height;
  776. // Set up limit values for motion vectors to prevent them extending
  777. // outside the UMV borders.
  778. x->mv_limits.row_min = -((mb_row * 16) + BORDER_MV_PIXELS_B16);
  779. x->mv_limits.row_max =
  780. ((cm->mb_rows - 1 - mb_row) * 16) + BORDER_MV_PIXELS_B16;
  781. for (mb_col = mb_col_start, c = 0; mb_col < mb_col_end; ++mb_col, c++) {
  782. int this_error;
  783. int this_intra_error;
  784. const int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
  785. const BLOCK_SIZE bsize = get_bsize(cm, mb_row, mb_col);
  786. double log_intra;
  787. int level_sample;
  788. const int mb_index = mb_row * cm->mb_cols + mb_col;
  789. #if CONFIG_FP_MB_STATS
  790. const int mb_index = mb_row * cm->mb_cols + mb_col;
  791. #endif
  792. (*(cpi->row_mt_sync_read_ptr))(&tile_data->row_mt_sync, mb_row, c);
  793. // Adjust to the next column of MBs.
  794. x->plane[0].src.buf = cpi->Source->y_buffer +
  795. mb_row * 16 * x->plane[0].src.stride + mb_col * 16;
  796. x->plane[1].src.buf = cpi->Source->u_buffer +
  797. mb_row * uv_mb_height * x->plane[1].src.stride +
  798. mb_col * uv_mb_height;
  799. x->plane[2].src.buf = cpi->Source->v_buffer +
  800. mb_row * uv_mb_height * x->plane[1].src.stride +
  801. mb_col * uv_mb_height;
  802. vpx_clear_system_state();
  803. xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset;
  804. xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset;
  805. xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset;
  806. xd->mi[0]->sb_type = bsize;
  807. xd->mi[0]->ref_frame[0] = INTRA_FRAME;
  808. set_mi_row_col(xd, &tile, mb_row << 1, num_8x8_blocks_high_lookup[bsize],
  809. mb_col << 1, num_8x8_blocks_wide_lookup[bsize], cm->mi_rows,
  810. cm->mi_cols);
  811. // Are edges available for intra prediction?
  812. // Since the firstpass does not populate the mi_grid_visible,
  813. // above_mi/left_mi must be overwritten with a nonzero value when edges
  814. // are available. Required by vp9_predict_intra_block().
  815. xd->above_mi = (mb_row != 0) ? &mi_above : NULL;
  816. xd->left_mi = ((mb_col << 1) > tile.mi_col_start) ? &mi_left : NULL;
  817. // Do intra 16x16 prediction.
  818. x->skip_encode = 0;
  819. x->fp_src_pred = 0;
  820. // Do intra prediction based on source pixels for tile boundaries
  821. if (mb_col == mb_col_start && mb_col != 0) {
  822. xd->left_mi = &mi_left;
  823. x->fp_src_pred = 1;
  824. }
  825. xd->mi[0]->mode = DC_PRED;
  826. xd->mi[0]->tx_size =
  827. use_dc_pred ? (bsize >= BLOCK_16X16 ? TX_16X16 : TX_8X8) : TX_4X4;
  828. // Fix - zero the 16x16 block first. This ensures correct this_error for
  829. // block sizes smaller than 16x16.
  830. vp9_zero_array(x->plane[0].src_diff, 256);
  831. vp9_encode_intra_block_plane(x, bsize, 0, 0);
  832. this_error = vpx_get_mb_ss(x->plane[0].src_diff);
  833. this_intra_error = this_error;
  834. // Keep a record of blocks that have very low intra error residual
  835. // (i.e. are in effect completely flat and untextured in the intra
  836. // domain). In natural videos this is uncommon, but it is much more
  837. // common in animations, graphics and screen content, so may be used
  838. // as a signal to detect these types of content.
  839. if (this_error < get_ul_intra_threshold(cm)) {
  840. ++(fp_acc_data->intra_skip_count);
  841. } else if ((mb_col > 0) &&
  842. (fp_acc_data->image_data_start_row == INVALID_ROW)) {
  843. fp_acc_data->image_data_start_row = mb_row;
  844. }
  845. // Blocks that are mainly smooth in the intra domain.
  846. // Some special accounting for CQ but also these are better for testing
  847. // noise levels.
  848. if (this_error < get_smooth_intra_threshold(cm)) {
  849. ++(fp_acc_data->intra_smooth_count);
  850. }
  851. // Special case noise measurement for first frame.
  852. if (cm->current_video_frame == 0) {
  853. if (this_intra_error < scale_sse_threshold(cm, LOW_I_THRESH)) {
  854. fp_acc_data->frame_noise_energy += fp_estimate_block_noise(x, bsize);
  855. } else {
  856. fp_acc_data->frame_noise_energy += (int64_t)SECTION_NOISE_DEF;
  857. }
  858. }
  859. #if CONFIG_VP9_HIGHBITDEPTH
  860. if (cm->use_highbitdepth) {
  861. switch (cm->bit_depth) {
  862. case VPX_BITS_8: break;
  863. case VPX_BITS_10: this_error >>= 4; break;
  864. default:
  865. assert(cm->bit_depth == VPX_BITS_12);
  866. this_error >>= 8;
  867. break;
  868. }
  869. }
  870. #endif // CONFIG_VP9_HIGHBITDEPTH
  871. vpx_clear_system_state();
  872. log_intra = log(this_error + 1.0);
  873. if (log_intra < 10.0) {
  874. mb_intra_factor = 1.0 + ((10.0 - log_intra) * 0.05);
  875. fp_acc_data->intra_factor += mb_intra_factor;
  876. if (cpi->row_mt_bit_exact)
  877. cpi->twopass.fp_mb_float_stats[mb_index].frame_mb_intra_factor =
  878. mb_intra_factor;
  879. } else {
  880. fp_acc_data->intra_factor += 1.0;
  881. if (cpi->row_mt_bit_exact)
  882. cpi->twopass.fp_mb_float_stats[mb_index].frame_mb_intra_factor = 1.0;
  883. }
  884. #if CONFIG_VP9_HIGHBITDEPTH
  885. if (cm->use_highbitdepth)
  886. level_sample = CONVERT_TO_SHORTPTR(x->plane[0].src.buf)[0];
  887. else
  888. level_sample = x->plane[0].src.buf[0];
  889. #else
  890. level_sample = x->plane[0].src.buf[0];
  891. #endif
  892. if ((level_sample < DARK_THRESH) && (log_intra < 9.0)) {
  893. mb_brightness_factor = 1.0 + (0.01 * (DARK_THRESH - level_sample));
  894. fp_acc_data->brightness_factor += mb_brightness_factor;
  895. if (cpi->row_mt_bit_exact)
  896. cpi->twopass.fp_mb_float_stats[mb_index].frame_mb_brightness_factor =
  897. mb_brightness_factor;
  898. } else {
  899. fp_acc_data->brightness_factor += 1.0;
  900. if (cpi->row_mt_bit_exact)
  901. cpi->twopass.fp_mb_float_stats[mb_index].frame_mb_brightness_factor =
  902. 1.0;
  903. }
  904. // Intrapenalty below deals with situations where the intra and inter
  905. // error scores are very low (e.g. a plain black frame).
  906. // We do not have special cases in first pass for 0,0 and nearest etc so
  907. // all inter modes carry an overhead cost estimate for the mv.
  908. // When the error score is very low this causes us to pick all or lots of
  909. // INTRA modes and throw lots of key frames.
  910. // This penalty adds a cost matching that of a 0,0 mv to the intra case.
  911. this_error += intrapenalty;
  912. // Accumulate the intra error.
  913. fp_acc_data->intra_error += (int64_t)this_error;
  914. #if CONFIG_FP_MB_STATS
  915. if (cpi->use_fp_mb_stats) {
  916. // initialization
  917. cpi->twopass.frame_mb_stats_buf[mb_index] = 0;
  918. }
  919. #endif
  920. // Set up limit values for motion vectors to prevent them extending
  921. // outside the UMV borders.
  922. x->mv_limits.col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16);
  923. x->mv_limits.col_max =
  924. ((cm->mb_cols - 1 - mb_col) * 16) + BORDER_MV_PIXELS_B16;
  925. // Other than for the first frame do a motion search.
  926. if (cm->current_video_frame > 0) {
  927. int tmp_err, motion_error, this_motion_error, raw_motion_error;
  928. // Assume 0,0 motion with no mv overhead.
  929. MV mv = { 0, 0 }, tmp_mv = { 0, 0 };
  930. struct buf_2d unscaled_last_source_buf_2d;
  931. vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[bsize];
  932. xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset;
  933. #if CONFIG_VP9_HIGHBITDEPTH
  934. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  935. motion_error = highbd_get_prediction_error(
  936. bsize, &x->plane[0].src, &xd->plane[0].pre[0], xd->bd);
  937. this_motion_error = highbd_get_prediction_error(
  938. bsize, &x->plane[0].src, &xd->plane[0].pre[0], 8);
  939. } else {
  940. motion_error =
  941. get_prediction_error(bsize, &x->plane[0].src, &xd->plane[0].pre[0]);
  942. this_motion_error = motion_error;
  943. }
  944. #else
  945. motion_error =
  946. get_prediction_error(bsize, &x->plane[0].src, &xd->plane[0].pre[0]);
  947. this_motion_error = motion_error;
  948. #endif // CONFIG_VP9_HIGHBITDEPTH
  949. // Compute the motion error of the 0,0 motion using the last source
  950. // frame as the reference. Skip the further motion search on
  951. // reconstructed frame if this error is very small.
  952. unscaled_last_source_buf_2d.buf =
  953. cpi->unscaled_last_source->y_buffer + recon_yoffset;
  954. unscaled_last_source_buf_2d.stride = cpi->unscaled_last_source->y_stride;
  955. #if CONFIG_VP9_HIGHBITDEPTH
  956. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  957. raw_motion_error = highbd_get_prediction_error(
  958. bsize, &x->plane[0].src, &unscaled_last_source_buf_2d, xd->bd);
  959. } else {
  960. raw_motion_error = get_prediction_error(bsize, &x->plane[0].src,
  961. &unscaled_last_source_buf_2d);
  962. }
  963. #else
  964. raw_motion_error = get_prediction_error(bsize, &x->plane[0].src,
  965. &unscaled_last_source_buf_2d);
  966. #endif // CONFIG_VP9_HIGHBITDEPTH
  967. if (raw_motion_error > NZ_MOTION_PENALTY) {
  968. // Test last reference frame using the previous best mv as the
  969. // starting point (best reference) for the search.
  970. first_pass_motion_search(cpi, x, best_ref_mv, &mv, &motion_error);
  971. v_fn_ptr.vf = get_block_variance_fn(bsize);
  972. #if CONFIG_VP9_HIGHBITDEPTH
  973. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  974. v_fn_ptr.vf = highbd_get_block_variance_fn(bsize, 8);
  975. }
  976. #endif // CONFIG_VP9_HIGHBITDEPTH
  977. this_motion_error =
  978. vp9_get_mvpred_var(x, &mv, best_ref_mv, &v_fn_ptr, 0);
  979. // If the current best reference mv is not centered on 0,0 then do a
  980. // 0,0 based search as well.
  981. if (!is_zero_mv(best_ref_mv)) {
  982. tmp_err = INT_MAX;
  983. first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv, &tmp_err);
  984. if (tmp_err < motion_error) {
  985. motion_error = tmp_err;
  986. mv = tmp_mv;
  987. this_motion_error =
  988. vp9_get_mvpred_var(x, &tmp_mv, &zero_mv, &v_fn_ptr, 0);
  989. }
  990. }
  991. // Search in an older reference frame.
  992. if ((cm->current_video_frame > 1) && gld_yv12 != NULL) {
  993. // Assume 0,0 motion with no mv overhead.
  994. int gf_motion_error;
  995. xd->plane[0].pre[0].buf = gld_yv12->y_buffer + recon_yoffset;
  996. #if CONFIG_VP9_HIGHBITDEPTH
  997. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  998. gf_motion_error = highbd_get_prediction_error(
  999. bsize, &x->plane[0].src, &xd->plane[0].pre[0], xd->bd);
  1000. } else {
  1001. gf_motion_error = get_prediction_error(bsize, &x->plane[0].src,
  1002. &xd->plane[0].pre[0]);
  1003. }
  1004. #else
  1005. gf_motion_error = get_prediction_error(bsize, &x->plane[0].src,
  1006. &xd->plane[0].pre[0]);
  1007. #endif // CONFIG_VP9_HIGHBITDEPTH
  1008. first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv, &gf_motion_error);
  1009. if (gf_motion_error < motion_error && gf_motion_error < this_error)
  1010. ++(fp_acc_data->second_ref_count);
  1011. // Reset to last frame as reference buffer.
  1012. xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset;
  1013. xd->plane[1].pre[0].buf = first_ref_buf->u_buffer + recon_uvoffset;
  1014. xd->plane[2].pre[0].buf = first_ref_buf->v_buffer + recon_uvoffset;
  1015. // In accumulating a score for the older reference frame take the
  1016. // best of the motion predicted score and the intra coded error
  1017. // (just as will be done for) accumulation of "coded_error" for
  1018. // the last frame.
  1019. if (gf_motion_error < this_error)
  1020. fp_acc_data->sr_coded_error += gf_motion_error;
  1021. else
  1022. fp_acc_data->sr_coded_error += this_error;
  1023. } else {
  1024. fp_acc_data->sr_coded_error += motion_error;
  1025. }
  1026. } else {
  1027. fp_acc_data->sr_coded_error += motion_error;
  1028. }
  1029. // Start by assuming that intra mode is best.
  1030. best_ref_mv->row = 0;
  1031. best_ref_mv->col = 0;
  1032. #if CONFIG_FP_MB_STATS
  1033. if (cpi->use_fp_mb_stats) {
  1034. // intra prediction statistics
  1035. cpi->twopass.frame_mb_stats_buf[mb_index] = 0;
  1036. cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_DCINTRA_MASK;
  1037. cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_MOTION_ZERO_MASK;
  1038. if (this_error > FPMB_ERROR_LARGE_TH) {
  1039. cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_LARGE_MASK;
  1040. } else if (this_error < FPMB_ERROR_SMALL_TH) {
  1041. cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_SMALL_MASK;
  1042. }
  1043. }
  1044. #endif
  1045. if (motion_error <= this_error) {
  1046. vpx_clear_system_state();
  1047. // Keep a count of cases where the inter and intra were very close
  1048. // and very low. This helps with scene cut detection for example in
  1049. // cropped clips with black bars at the sides or top and bottom.
  1050. if (((this_error - intrapenalty) * 9 <= motion_error * 10) &&
  1051. (this_error < (2 * intrapenalty))) {
  1052. fp_acc_data->neutral_count += 1.0;
  1053. if (cpi->row_mt_bit_exact)
  1054. cpi->twopass.fp_mb_float_stats[mb_index].frame_mb_neutral_count =
  1055. 1.0;
  1056. // Also track cases where the intra is not much worse than the inter
  1057. // and use this in limiting the GF/arf group length.
  1058. } else if ((this_error > NCOUNT_INTRA_THRESH) &&
  1059. (this_error < (NCOUNT_INTRA_FACTOR * motion_error))) {
  1060. mb_neutral_count =
  1061. (double)motion_error / DOUBLE_DIVIDE_CHECK((double)this_error);
  1062. fp_acc_data->neutral_count += mb_neutral_count;
  1063. if (cpi->row_mt_bit_exact)
  1064. cpi->twopass.fp_mb_float_stats[mb_index].frame_mb_neutral_count =
  1065. mb_neutral_count;
  1066. }
  1067. mv.row *= 8;
  1068. mv.col *= 8;
  1069. this_error = motion_error;
  1070. xd->mi[0]->mode = NEWMV;
  1071. xd->mi[0]->mv[0].as_mv = mv;
  1072. xd->mi[0]->tx_size = TX_4X4;
  1073. xd->mi[0]->ref_frame[0] = LAST_FRAME;
  1074. xd->mi[0]->ref_frame[1] = NONE;
  1075. vp9_build_inter_predictors_sby(xd, mb_row << 1, mb_col << 1, bsize);
  1076. vp9_encode_sby_pass1(x, bsize);
  1077. fp_acc_data->sum_mvr += mv.row;
  1078. fp_acc_data->sum_mvr_abs += abs(mv.row);
  1079. fp_acc_data->sum_mvc += mv.col;
  1080. fp_acc_data->sum_mvc_abs += abs(mv.col);
  1081. fp_acc_data->sum_mvrs += mv.row * mv.row;
  1082. fp_acc_data->sum_mvcs += mv.col * mv.col;
  1083. ++(fp_acc_data->intercount);
  1084. *best_ref_mv = mv;
  1085. #if CONFIG_FP_MB_STATS
  1086. if (cpi->use_fp_mb_stats) {
  1087. // inter prediction statistics
  1088. cpi->twopass.frame_mb_stats_buf[mb_index] = 0;
  1089. cpi->twopass.frame_mb_stats_buf[mb_index] &= ~FPMB_DCINTRA_MASK;
  1090. cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_MOTION_ZERO_MASK;
  1091. if (this_error > FPMB_ERROR_LARGE_TH) {
  1092. cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_LARGE_MASK;
  1093. } else if (this_error < FPMB_ERROR_SMALL_TH) {
  1094. cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_SMALL_MASK;
  1095. }
  1096. }
  1097. #endif
  1098. if (!is_zero_mv(&mv)) {
  1099. ++(fp_acc_data->mvcount);
  1100. #if CONFIG_FP_MB_STATS
  1101. if (cpi->use_fp_mb_stats) {
  1102. cpi->twopass.frame_mb_stats_buf[mb_index] &= ~FPMB_MOTION_ZERO_MASK;
  1103. // check estimated motion direction
  1104. if (mv.as_mv.col > 0 && mv.as_mv.col >= abs(mv.as_mv.row)) {
  1105. // right direction
  1106. cpi->twopass.frame_mb_stats_buf[mb_index] |=
  1107. FPMB_MOTION_RIGHT_MASK;
  1108. } else if (mv.as_mv.row < 0 &&
  1109. abs(mv.as_mv.row) >= abs(mv.as_mv.col)) {
  1110. // up direction
  1111. cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_MOTION_UP_MASK;
  1112. } else if (mv.as_mv.col < 0 &&
  1113. abs(mv.as_mv.col) >= abs(mv.as_mv.row)) {
  1114. // left direction
  1115. cpi->twopass.frame_mb_stats_buf[mb_index] |=
  1116. FPMB_MOTION_LEFT_MASK;
  1117. } else {
  1118. // down direction
  1119. cpi->twopass.frame_mb_stats_buf[mb_index] |=
  1120. FPMB_MOTION_DOWN_MASK;
  1121. }
  1122. }
  1123. #endif
  1124. // Does the row vector point inwards or outwards?
  1125. if (mb_row < cm->mb_rows / 2) {
  1126. if (mv.row > 0)
  1127. --(fp_acc_data->sum_in_vectors);
  1128. else if (mv.row < 0)
  1129. ++(fp_acc_data->sum_in_vectors);
  1130. } else if (mb_row > cm->mb_rows / 2) {
  1131. if (mv.row > 0)
  1132. ++(fp_acc_data->sum_in_vectors);
  1133. else if (mv.row < 0)
  1134. --(fp_acc_data->sum_in_vectors);
  1135. }
  1136. // Does the col vector point inwards or outwards?
  1137. if (mb_col < cm->mb_cols / 2) {
  1138. if (mv.col > 0)
  1139. --(fp_acc_data->sum_in_vectors);
  1140. else if (mv.col < 0)
  1141. ++(fp_acc_data->sum_in_vectors);
  1142. } else if (mb_col > cm->mb_cols / 2) {
  1143. if (mv.col > 0)
  1144. ++(fp_acc_data->sum_in_vectors);
  1145. else if (mv.col < 0)
  1146. --(fp_acc_data->sum_in_vectors);
  1147. }
  1148. }
  1149. if (this_intra_error < scaled_low_intra_thresh) {
  1150. fp_acc_data->frame_noise_energy += fp_estimate_block_noise(x, bsize);
  1151. } else {
  1152. fp_acc_data->frame_noise_energy += (int64_t)SECTION_NOISE_DEF;
  1153. }
  1154. } else { // Intra < inter error
  1155. if (this_intra_error < scaled_low_intra_thresh) {
  1156. fp_acc_data->frame_noise_energy += fp_estimate_block_noise(x, bsize);
  1157. if (this_motion_error < scaled_low_intra_thresh) {
  1158. fp_acc_data->intra_count_low += 1.0;
  1159. } else {
  1160. fp_acc_data->intra_count_high += 1.0;
  1161. }
  1162. } else {
  1163. fp_acc_data->frame_noise_energy += (int64_t)SECTION_NOISE_DEF;
  1164. fp_acc_data->intra_count_high += 1.0;
  1165. }
  1166. }
  1167. } else {
  1168. fp_acc_data->sr_coded_error += (int64_t)this_error;
  1169. }
  1170. fp_acc_data->coded_error += (int64_t)this_error;
  1171. recon_yoffset += 16;
  1172. recon_uvoffset += uv_mb_height;
  1173. // Accumulate row level stats to the corresponding tile stats
  1174. if (cpi->row_mt && mb_col == mb_col_end - 1)
  1175. accumulate_fp_mb_row_stat(tile_data, fp_acc_data);
  1176. (*(cpi->row_mt_sync_write_ptr))(&tile_data->row_mt_sync, mb_row, c,
  1177. num_mb_cols);
  1178. }
  1179. vpx_clear_system_state();
  1180. }
  1181. static void first_pass_encode(VP9_COMP *cpi, FIRSTPASS_DATA *fp_acc_data) {
  1182. VP9_COMMON *const cm = &cpi->common;
  1183. int mb_row;
  1184. TileDataEnc tile_data;
  1185. TileInfo *tile = &tile_data.tile_info;
  1186. MV zero_mv = { 0, 0 };
  1187. MV best_ref_mv;
  1188. // Tiling is ignored in the first pass.
  1189. vp9_tile_init(tile, cm, 0, 0);
  1190. for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) {
  1191. best_ref_mv = zero_mv;
  1192. vp9_first_pass_encode_tile_mb_row(cpi, &cpi->td, fp_acc_data, &tile_data,
  1193. &best_ref_mv, mb_row);
  1194. }
  1195. }
  1196. void vp9_first_pass(VP9_COMP *cpi, const struct lookahead_entry *source) {
  1197. MACROBLOCK *const x = &cpi->td.mb;
  1198. VP9_COMMON *const cm = &cpi->common;
  1199. MACROBLOCKD *const xd = &x->e_mbd;
  1200. TWO_PASS *twopass = &cpi->twopass;
  1201. YV12_BUFFER_CONFIG *const lst_yv12 = get_ref_frame_buffer(cpi, LAST_FRAME);
  1202. YV12_BUFFER_CONFIG *gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
  1203. YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm);
  1204. const YV12_BUFFER_CONFIG *first_ref_buf = lst_yv12;
  1205. BufferPool *const pool = cm->buffer_pool;
  1206. FIRSTPASS_DATA fp_temp_data;
  1207. FIRSTPASS_DATA *fp_acc_data = &fp_temp_data;
  1208. vpx_clear_system_state();
  1209. vp9_zero(fp_temp_data);
  1210. fp_acc_data->image_data_start_row = INVALID_ROW;
  1211. // First pass code requires valid last and new frame buffers.
  1212. assert(new_yv12 != NULL);
  1213. assert(frame_is_intra_only(cm) || (lst_yv12 != NULL));
  1214. #if CONFIG_FP_MB_STATS
  1215. if (cpi->use_fp_mb_stats) {
  1216. vp9_zero_array(cpi->twopass.frame_mb_stats_buf, cm->initial_mbs);
  1217. }
  1218. #endif
  1219. set_first_pass_params(cpi);
  1220. vp9_set_quantizer(cm, find_fp_qindex(cm->bit_depth));
  1221. vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
  1222. vp9_setup_src_planes(x, cpi->Source, 0, 0);
  1223. vp9_setup_dst_planes(xd->plane, new_yv12, 0, 0);
  1224. if (!frame_is_intra_only(cm)) {
  1225. vp9_setup_pre_planes(xd, 0, first_ref_buf, 0, 0, NULL);
  1226. }
  1227. xd->mi = cm->mi_grid_visible;
  1228. xd->mi[0] = cm->mi;
  1229. vp9_frame_init_quantizer(cpi);
  1230. x->skip_recode = 0;
  1231. vp9_init_mv_probs(cm);
  1232. vp9_initialize_rd_consts(cpi);
  1233. cm->log2_tile_rows = 0;
  1234. if (cpi->row_mt_bit_exact && cpi->twopass.fp_mb_float_stats == NULL)
  1235. CHECK_MEM_ERROR(
  1236. cm, cpi->twopass.fp_mb_float_stats,
  1237. vpx_calloc(cm->MBs * sizeof(*cpi->twopass.fp_mb_float_stats), 1));
  1238. {
  1239. FIRSTPASS_STATS fps;
  1240. TileDataEnc *first_tile_col;
  1241. if (!cpi->row_mt) {
  1242. cm->log2_tile_cols = 0;
  1243. cpi->row_mt_sync_read_ptr = vp9_row_mt_sync_read_dummy;
  1244. cpi->row_mt_sync_write_ptr = vp9_row_mt_sync_write_dummy;
  1245. first_pass_encode(cpi, fp_acc_data);
  1246. first_pass_stat_calc(cpi, &fps, fp_acc_data);
  1247. } else {
  1248. cpi->row_mt_sync_read_ptr = vp9_row_mt_sync_read;
  1249. cpi->row_mt_sync_write_ptr = vp9_row_mt_sync_write;
  1250. if (cpi->row_mt_bit_exact) {
  1251. cm->log2_tile_cols = 0;
  1252. vp9_zero_array(cpi->twopass.fp_mb_float_stats, cm->MBs);
  1253. }
  1254. vp9_encode_fp_row_mt(cpi);
  1255. first_tile_col = &cpi->tile_data[0];
  1256. if (cpi->row_mt_bit_exact)
  1257. accumulate_floating_point_stats(cpi, first_tile_col);
  1258. first_pass_stat_calc(cpi, &fps, &(first_tile_col->fp_data));
  1259. }
  1260. // Dont allow a value of 0 for duration.
  1261. // (Section duration is also defaulted to minimum of 1.0).
  1262. fps.duration = VPXMAX(1.0, (double)(source->ts_end - source->ts_start));
  1263. // Don't want to do output stats with a stack variable!
  1264. twopass->this_frame_stats = fps;
  1265. output_stats(&twopass->this_frame_stats, cpi->output_pkt_list);
  1266. accumulate_stats(&twopass->total_stats, &fps);
  1267. #if CONFIG_FP_MB_STATS
  1268. if (cpi->use_fp_mb_stats) {
  1269. output_fpmb_stats(twopass->frame_mb_stats_buf, cm, cpi->output_pkt_list);
  1270. }
  1271. #endif
  1272. }
  1273. // Copy the previous Last Frame back into gf and and arf buffers if
  1274. // the prediction is good enough... but also don't allow it to lag too far.
  1275. if ((twopass->sr_update_lag > 3) ||
  1276. ((cm->current_video_frame > 0) &&
  1277. (twopass->this_frame_stats.pcnt_inter > 0.20) &&
  1278. ((twopass->this_frame_stats.intra_error /
  1279. DOUBLE_DIVIDE_CHECK(twopass->this_frame_stats.coded_error)) > 2.0))) {
  1280. if (gld_yv12 != NULL) {
  1281. ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
  1282. cm->ref_frame_map[cpi->lst_fb_idx]);
  1283. }
  1284. twopass->sr_update_lag = 1;
  1285. } else {
  1286. ++twopass->sr_update_lag;
  1287. }
  1288. vpx_extend_frame_borders(new_yv12);
  1289. // The frame we just compressed now becomes the last frame.
  1290. ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->lst_fb_idx],
  1291. cm->new_fb_idx);
  1292. // Special case for the first frame. Copy into the GF buffer as a second
  1293. // reference.
  1294. if (cm->current_video_frame == 0 && cpi->gld_fb_idx != INVALID_IDX) {
  1295. ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
  1296. cm->ref_frame_map[cpi->lst_fb_idx]);
  1297. }
  1298. // Use this to see what the first pass reconstruction looks like.
  1299. if (0) {
  1300. char filename[512];
  1301. FILE *recon_file;
  1302. snprintf(filename, sizeof(filename), "enc%04d.yuv",
  1303. (int)cm->current_video_frame);
  1304. if (cm->current_video_frame == 0)
  1305. recon_file = fopen(filename, "wb");
  1306. else
  1307. recon_file = fopen(filename, "ab");
  1308. (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file);
  1309. fclose(recon_file);
  1310. }
  1311. ++cm->current_video_frame;
  1312. if (cpi->use_svc) vp9_inc_frame_in_layer(cpi);
  1313. }
  1314. static const double q_pow_term[(QINDEX_RANGE >> 5) + 1] = { 0.65, 0.70, 0.75,
  1315. 0.85, 0.90, 0.90,
  1316. 0.90, 1.00, 1.25 };
  1317. static double calc_correction_factor(double err_per_mb, double err_divisor,
  1318. int q) {
  1319. const double error_term = err_per_mb / DOUBLE_DIVIDE_CHECK(err_divisor);
  1320. const int index = q >> 5;
  1321. double power_term;
  1322. assert((index >= 0) && (index < (QINDEX_RANGE >> 5)));
  1323. // Adjustment based on quantizer to the power term.
  1324. power_term =
  1325. q_pow_term[index] +
  1326. (((q_pow_term[index + 1] - q_pow_term[index]) * (q % 32)) / 32.0);
  1327. // Calculate correction factor.
  1328. if (power_term < 1.0) assert(error_term >= 0.0);
  1329. return fclamp(pow(error_term, power_term), 0.05, 5.0);
  1330. }
  1331. static double wq_err_divisor(VP9_COMP *cpi) {
  1332. const VP9_COMMON *const cm = &cpi->common;
  1333. unsigned int screen_area = (cm->width * cm->height);
  1334. // Use a different error per mb factor for calculating boost for
  1335. // different formats.
  1336. if (screen_area <= 640 * 360) {
  1337. return 115.0;
  1338. } else if (screen_area < 1280 * 720) {
  1339. return 125.0;
  1340. } else if (screen_area <= 1920 * 1080) {
  1341. return 130.0;
  1342. } else if (screen_area < 3840 * 2160) {
  1343. return 150.0;
  1344. }
  1345. // Fall through to here only for 4K and above.
  1346. return 200.0;
  1347. }
  1348. #define NOISE_FACTOR_MIN 0.9
  1349. #define NOISE_FACTOR_MAX 1.1
  1350. static int get_twopass_worst_quality(VP9_COMP *cpi, const double section_err,
  1351. double inactive_zone, double section_noise,
  1352. int section_target_bandwidth) {
  1353. const RATE_CONTROL *const rc = &cpi->rc;
  1354. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  1355. TWO_PASS *const twopass = &cpi->twopass;
  1356. double last_group_rate_err;
  1357. // Clamp the target rate to VBR min / max limts.
  1358. const int target_rate =
  1359. vp9_rc_clamp_pframe_target_size(cpi, section_target_bandwidth);
  1360. double noise_factor = pow((section_noise / SECTION_NOISE_DEF), 0.5);
  1361. noise_factor = fclamp(noise_factor, NOISE_FACTOR_MIN, NOISE_FACTOR_MAX);
  1362. inactive_zone = fclamp(inactive_zone, 0.0, 1.0);
  1363. // TODO(jimbankoski): remove #if here or below when this has been
  1364. // well tested.
  1365. #if CONFIG_ALWAYS_ADJUST_BPM
  1366. // based on recent history adjust expectations of bits per macroblock.
  1367. last_group_rate_err =
  1368. (double)twopass->rolling_arf_group_actual_bits /
  1369. DOUBLE_DIVIDE_CHECK((double)twopass->rolling_arf_group_target_bits);
  1370. last_group_rate_err = VPXMAX(0.25, VPXMIN(4.0, last_group_rate_err));
  1371. twopass->bpm_factor *= (3.0 + last_group_rate_err) / 4.0;
  1372. twopass->bpm_factor = VPXMAX(0.25, VPXMIN(4.0, twopass->bpm_factor));
  1373. #endif
  1374. if (target_rate <= 0) {
  1375. return rc->worst_quality; // Highest value allowed
  1376. } else {
  1377. const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
  1378. ? cpi->initial_mbs
  1379. : cpi->common.MBs;
  1380. const double active_pct = VPXMAX(0.01, 1.0 - inactive_zone);
  1381. const int active_mbs = (int)VPXMAX(1, (double)num_mbs * active_pct);
  1382. const double av_err_per_mb = section_err / active_pct;
  1383. const double speed_term = 1.0 + 0.04 * oxcf->speed;
  1384. const int target_norm_bits_per_mb =
  1385. (int)(((uint64_t)target_rate << BPER_MB_NORMBITS) / active_mbs);
  1386. int q;
  1387. // TODO(jimbankoski): remove #if here or above when this has been
  1388. // well tested.
  1389. #if !CONFIG_ALWAYS_ADJUST_BPM
  1390. // based on recent history adjust expectations of bits per macroblock.
  1391. last_group_rate_err =
  1392. (double)twopass->rolling_arf_group_actual_bits /
  1393. DOUBLE_DIVIDE_CHECK((double)twopass->rolling_arf_group_target_bits);
  1394. last_group_rate_err = VPXMAX(0.25, VPXMIN(4.0, last_group_rate_err));
  1395. twopass->bpm_factor *= (3.0 + last_group_rate_err) / 4.0;
  1396. twopass->bpm_factor = VPXMAX(0.25, VPXMIN(4.0, twopass->bpm_factor));
  1397. #endif
  1398. // Try and pick a max Q that will be high enough to encode the
  1399. // content at the given rate.
  1400. for (q = rc->best_quality; q < rc->worst_quality; ++q) {
  1401. const double factor =
  1402. calc_correction_factor(av_err_per_mb, wq_err_divisor(cpi), q);
  1403. const int bits_per_mb = vp9_rc_bits_per_mb(
  1404. INTER_FRAME, q,
  1405. factor * speed_term * cpi->twopass.bpm_factor * noise_factor,
  1406. cpi->common.bit_depth);
  1407. if (bits_per_mb <= target_norm_bits_per_mb) break;
  1408. }
  1409. // Restriction on active max q for constrained quality mode.
  1410. if (cpi->oxcf.rc_mode == VPX_CQ) q = VPXMAX(q, oxcf->cq_level);
  1411. return q;
  1412. }
  1413. }
  1414. static void setup_rf_level_maxq(VP9_COMP *cpi) {
  1415. int i;
  1416. RATE_CONTROL *const rc = &cpi->rc;
  1417. for (i = INTER_NORMAL; i < RATE_FACTOR_LEVELS; ++i) {
  1418. int qdelta = vp9_frame_type_qdelta(cpi, i, rc->worst_quality);
  1419. rc->rf_level_maxq[i] = VPXMAX(rc->worst_quality + qdelta, rc->best_quality);
  1420. }
  1421. }
  1422. static void init_subsampling(VP9_COMP *cpi) {
  1423. const VP9_COMMON *const cm = &cpi->common;
  1424. RATE_CONTROL *const rc = &cpi->rc;
  1425. const int w = cm->width;
  1426. const int h = cm->height;
  1427. int i;
  1428. for (i = 0; i < FRAME_SCALE_STEPS; ++i) {
  1429. // Note: Frames with odd-sized dimensions may result from this scaling.
  1430. rc->frame_width[i] = (w * 16) / frame_scale_factor[i];
  1431. rc->frame_height[i] = (h * 16) / frame_scale_factor[i];
  1432. }
  1433. setup_rf_level_maxq(cpi);
  1434. }
  1435. void calculate_coded_size(VP9_COMP *cpi, int *scaled_frame_width,
  1436. int *scaled_frame_height) {
  1437. RATE_CONTROL *const rc = &cpi->rc;
  1438. *scaled_frame_width = rc->frame_width[rc->frame_size_selector];
  1439. *scaled_frame_height = rc->frame_height[rc->frame_size_selector];
  1440. }
  1441. void vp9_init_second_pass(VP9_COMP *cpi) {
  1442. VP9EncoderConfig *const oxcf = &cpi->oxcf;
  1443. RATE_CONTROL *const rc = &cpi->rc;
  1444. TWO_PASS *const twopass = &cpi->twopass;
  1445. double frame_rate;
  1446. FIRSTPASS_STATS *stats;
  1447. zero_stats(&twopass->total_stats);
  1448. zero_stats(&twopass->total_left_stats);
  1449. if (!twopass->stats_in_end) return;
  1450. stats = &twopass->total_stats;
  1451. *stats = *twopass->stats_in_end;
  1452. twopass->total_left_stats = *stats;
  1453. // Scan the first pass file and calculate a modified score for each
  1454. // frame that is used to distribute bits. The modified score is assumed
  1455. // to provide a linear basis for bit allocation. I.e a frame A with a score
  1456. // that is double that of frame B will be allocated 2x as many bits.
  1457. {
  1458. double modified_score_total = 0.0;
  1459. const FIRSTPASS_STATS *s = twopass->stats_in;
  1460. double av_err;
  1461. if (oxcf->vbr_corpus_complexity) {
  1462. twopass->mean_mod_score = (double)oxcf->vbr_corpus_complexity / 10.0;
  1463. av_err = get_distribution_av_err(cpi, twopass);
  1464. } else {
  1465. av_err = get_distribution_av_err(cpi, twopass);
  1466. // The first scan is unclamped and gives a raw average.
  1467. while (s < twopass->stats_in_end) {
  1468. modified_score_total += calculate_mod_frame_score(cpi, oxcf, s, av_err);
  1469. ++s;
  1470. }
  1471. // The average error from this first scan is used to define the midpoint
  1472. // error for the rate distribution function.
  1473. twopass->mean_mod_score =
  1474. modified_score_total / DOUBLE_DIVIDE_CHECK(stats->count);
  1475. }
  1476. // Second scan using clamps based on the previous cycle average.
  1477. // This may modify the total and average somewhat but we dont bother with
  1478. // further itterations.
  1479. modified_score_total = 0.0;
  1480. s = twopass->stats_in;
  1481. while (s < twopass->stats_in_end) {
  1482. modified_score_total +=
  1483. calculate_norm_frame_score(cpi, twopass, oxcf, s, av_err);
  1484. ++s;
  1485. }
  1486. twopass->normalized_score_left = modified_score_total;
  1487. // If using Corpus wide VBR mode then update the clip target bandwidth to
  1488. // reflect how the clip compares to the rest of the corpus.
  1489. if (oxcf->vbr_corpus_complexity) {
  1490. oxcf->target_bandwidth =
  1491. (int64_t)((double)oxcf->target_bandwidth *
  1492. (twopass->normalized_score_left / stats->count));
  1493. }
  1494. #if COMPLEXITY_STATS_OUTPUT
  1495. {
  1496. FILE *compstats;
  1497. compstats = fopen("complexity_stats.stt", "a");
  1498. fprintf(compstats, "%10.3lf\n",
  1499. twopass->normalized_score_left / stats->count);
  1500. fclose(compstats);
  1501. }
  1502. #endif
  1503. }
  1504. frame_rate = 10000000.0 * stats->count / stats->duration;
  1505. // Each frame can have a different duration, as the frame rate in the source
  1506. // isn't guaranteed to be constant. The frame rate prior to the first frame
  1507. // encoded in the second pass is a guess. However, the sum duration is not.
  1508. // It is calculated based on the actual durations of all frames from the
  1509. // first pass.
  1510. vp9_new_framerate(cpi, frame_rate);
  1511. twopass->bits_left =
  1512. (int64_t)(stats->duration * oxcf->target_bandwidth / 10000000.0);
  1513. // This variable monitors how far behind the second ref update is lagging.
  1514. twopass->sr_update_lag = 1;
  1515. // Reset the vbr bits off target counters
  1516. rc->vbr_bits_off_target = 0;
  1517. rc->vbr_bits_off_target_fast = 0;
  1518. rc->rate_error_estimate = 0;
  1519. // Static sequence monitor variables.
  1520. twopass->kf_zeromotion_pct = 100;
  1521. twopass->last_kfgroup_zeromotion_pct = 100;
  1522. // Initialize bits per macro_block estimate correction factor.
  1523. twopass->bpm_factor = 1.0;
  1524. // Initialize actual and target bits counters for ARF groups so that
  1525. // at the start we have a neutral bpm adjustment.
  1526. twopass->rolling_arf_group_target_bits = 1;
  1527. twopass->rolling_arf_group_actual_bits = 1;
  1528. if (oxcf->resize_mode != RESIZE_NONE) {
  1529. init_subsampling(cpi);
  1530. }
  1531. // Initialize the arnr strangth adjustment to 0
  1532. twopass->arnr_strength_adjustment = 0;
  1533. }
  1534. #define SR_DIFF_PART 0.0015
  1535. #define INTRA_PART 0.005
  1536. #define DEFAULT_DECAY_LIMIT 0.75
  1537. #define LOW_SR_DIFF_TRHESH 0.1
  1538. #define SR_DIFF_MAX 128.0
  1539. #define LOW_CODED_ERR_PER_MB 10.0
  1540. #define NCOUNT_FRAME_II_THRESH 6.0
  1541. static double get_sr_decay_rate(const VP9_COMP *cpi,
  1542. const FIRSTPASS_STATS *frame) {
  1543. double sr_diff = (frame->sr_coded_error - frame->coded_error);
  1544. double sr_decay = 1.0;
  1545. double modified_pct_inter;
  1546. double modified_pcnt_intra;
  1547. const double motion_amplitude_part =
  1548. frame->pcnt_motion * ((frame->mvc_abs + frame->mvr_abs) /
  1549. (cpi->initial_height + cpi->initial_width));
  1550. modified_pct_inter = frame->pcnt_inter;
  1551. if ((frame->coded_error > LOW_CODED_ERR_PER_MB) &&
  1552. ((frame->intra_error / DOUBLE_DIVIDE_CHECK(frame->coded_error)) <
  1553. (double)NCOUNT_FRAME_II_THRESH)) {
  1554. modified_pct_inter =
  1555. frame->pcnt_inter + frame->pcnt_intra_low - frame->pcnt_neutral;
  1556. }
  1557. modified_pcnt_intra = 100 * (1.0 - modified_pct_inter);
  1558. if ((sr_diff > LOW_SR_DIFF_TRHESH)) {
  1559. sr_diff = VPXMIN(sr_diff, SR_DIFF_MAX);
  1560. sr_decay = 1.0 - (SR_DIFF_PART * sr_diff) - motion_amplitude_part -
  1561. (INTRA_PART * modified_pcnt_intra);
  1562. }
  1563. return VPXMAX(sr_decay, DEFAULT_DECAY_LIMIT);
  1564. }
  1565. // This function gives an estimate of how badly we believe the prediction
  1566. // quality is decaying from frame to frame.
  1567. static double get_zero_motion_factor(const VP9_COMP *cpi,
  1568. const FIRSTPASS_STATS *frame) {
  1569. const double zero_motion_pct = frame->pcnt_inter - frame->pcnt_motion;
  1570. double sr_decay = get_sr_decay_rate(cpi, frame);
  1571. return VPXMIN(sr_decay, zero_motion_pct);
  1572. }
  1573. #define ZM_POWER_FACTOR 0.75
  1574. static double get_prediction_decay_rate(const VP9_COMP *cpi,
  1575. const FIRSTPASS_STATS *next_frame) {
  1576. const double sr_decay_rate = get_sr_decay_rate(cpi, next_frame);
  1577. const double zero_motion_factor =
  1578. (0.95 * pow((next_frame->pcnt_inter - next_frame->pcnt_motion),
  1579. ZM_POWER_FACTOR));
  1580. return VPXMAX(zero_motion_factor,
  1581. (sr_decay_rate + ((1.0 - sr_decay_rate) * zero_motion_factor)));
  1582. }
  1583. // Function to test for a condition where a complex transition is followed
  1584. // by a static section. For example in slide shows where there is a fade
  1585. // between slides. This is to help with more optimal kf and gf positioning.
  1586. static int detect_transition_to_still(VP9_COMP *cpi, int frame_interval,
  1587. int still_interval,
  1588. double loop_decay_rate,
  1589. double last_decay_rate) {
  1590. TWO_PASS *const twopass = &cpi->twopass;
  1591. RATE_CONTROL *const rc = &cpi->rc;
  1592. // Break clause to detect very still sections after motion
  1593. // For example a static image after a fade or other transition
  1594. // instead of a clean scene cut.
  1595. if (frame_interval > rc->min_gf_interval && loop_decay_rate >= 0.999 &&
  1596. last_decay_rate < 0.9) {
  1597. int j;
  1598. // Look ahead a few frames to see if static condition persists...
  1599. for (j = 0; j < still_interval; ++j) {
  1600. const FIRSTPASS_STATS *stats = &twopass->stats_in[j];
  1601. if (stats >= twopass->stats_in_end) break;
  1602. if (stats->pcnt_inter - stats->pcnt_motion < 0.999) break;
  1603. }
  1604. // Only if it does do we signal a transition to still.
  1605. return j == still_interval;
  1606. }
  1607. return 0;
  1608. }
  1609. // This function detects a flash through the high relative pcnt_second_ref
  1610. // score in the frame following a flash frame. The offset passed in should
  1611. // reflect this.
  1612. static int detect_flash(const TWO_PASS *twopass, int offset) {
  1613. const FIRSTPASS_STATS *const next_frame = read_frame_stats(twopass, offset);
  1614. // What we are looking for here is a situation where there is a
  1615. // brief break in prediction (such as a flash) but subsequent frames
  1616. // are reasonably well predicted by an earlier (pre flash) frame.
  1617. // The recovery after a flash is indicated by a high pcnt_second_ref
  1618. // useage or a second ref coded error notabley lower than the last
  1619. // frame coded error.
  1620. return next_frame != NULL &&
  1621. ((next_frame->sr_coded_error < next_frame->coded_error) ||
  1622. ((next_frame->pcnt_second_ref > next_frame->pcnt_inter) &&
  1623. (next_frame->pcnt_second_ref >= 0.5)));
  1624. }
  1625. // Update the motion related elements to the GF arf boost calculation.
  1626. static void accumulate_frame_motion_stats(const FIRSTPASS_STATS *stats,
  1627. double *mv_in_out,
  1628. double *mv_in_out_accumulator,
  1629. double *abs_mv_in_out_accumulator,
  1630. double *mv_ratio_accumulator) {
  1631. const double pct = stats->pcnt_motion;
  1632. // Accumulate Motion In/Out of frame stats.
  1633. *mv_in_out = stats->mv_in_out_count * pct;
  1634. *mv_in_out_accumulator += *mv_in_out;
  1635. *abs_mv_in_out_accumulator += fabs(*mv_in_out);
  1636. // Accumulate a measure of how uniform (or conversely how random) the motion
  1637. // field is (a ratio of abs(mv) / mv).
  1638. if (pct > 0.05) {
  1639. const double mvr_ratio =
  1640. fabs(stats->mvr_abs) / DOUBLE_DIVIDE_CHECK(fabs(stats->MVr));
  1641. const double mvc_ratio =
  1642. fabs(stats->mvc_abs) / DOUBLE_DIVIDE_CHECK(fabs(stats->MVc));
  1643. *mv_ratio_accumulator +=
  1644. pct * (mvr_ratio < stats->mvr_abs ? mvr_ratio : stats->mvr_abs);
  1645. *mv_ratio_accumulator +=
  1646. pct * (mvc_ratio < stats->mvc_abs ? mvc_ratio : stats->mvc_abs);
  1647. }
  1648. }
  1649. #define BASELINE_ERR_PER_MB 12500.0
  1650. #define GF_MAX_BOOST 96.0
  1651. static double calc_frame_boost(VP9_COMP *cpi, const FIRSTPASS_STATS *this_frame,
  1652. double this_frame_mv_in_out) {
  1653. double frame_boost;
  1654. const double lq = vp9_convert_qindex_to_q(
  1655. cpi->rc.avg_frame_qindex[INTER_FRAME], cpi->common.bit_depth);
  1656. const double boost_q_correction = VPXMIN((0.5 + (lq * 0.015)), 1.5);
  1657. const double active_area = calculate_active_area(cpi, this_frame);
  1658. // Underlying boost factor is based on inter error ratio.
  1659. frame_boost = (BASELINE_ERR_PER_MB * active_area) /
  1660. DOUBLE_DIVIDE_CHECK(this_frame->coded_error);
  1661. // Small adjustment for cases where there is a zoom out
  1662. if (this_frame_mv_in_out > 0.0)
  1663. frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
  1664. // Q correction and scalling
  1665. frame_boost = frame_boost * boost_q_correction;
  1666. return VPXMIN(frame_boost, GF_MAX_BOOST * boost_q_correction);
  1667. }
  1668. static double kf_err_per_mb(VP9_COMP *cpi) {
  1669. const VP9_COMMON *const cm = &cpi->common;
  1670. unsigned int screen_area = (cm->width * cm->height);
  1671. // Use a different error per mb factor for calculating boost for
  1672. // different formats.
  1673. if (screen_area < 1280 * 720) {
  1674. return 2000.0;
  1675. } else if (screen_area < 1920 * 1080) {
  1676. return 500.0;
  1677. }
  1678. return 250.0;
  1679. }
  1680. static double calc_kf_frame_boost(VP9_COMP *cpi,
  1681. const FIRSTPASS_STATS *this_frame,
  1682. double *sr_accumulator,
  1683. double this_frame_mv_in_out,
  1684. double max_boost) {
  1685. double frame_boost;
  1686. const double lq = vp9_convert_qindex_to_q(
  1687. cpi->rc.avg_frame_qindex[INTER_FRAME], cpi->common.bit_depth);
  1688. const double boost_q_correction = VPXMIN((0.50 + (lq * 0.015)), 2.00);
  1689. const double active_area = calculate_active_area(cpi, this_frame);
  1690. // Underlying boost factor is based on inter error ratio.
  1691. frame_boost = (kf_err_per_mb(cpi) * active_area) /
  1692. DOUBLE_DIVIDE_CHECK(this_frame->coded_error + *sr_accumulator);
  1693. // Update the accumulator for second ref error difference.
  1694. // This is intended to give an indication of how much the coded error is
  1695. // increasing over time.
  1696. *sr_accumulator += (this_frame->sr_coded_error - this_frame->coded_error);
  1697. *sr_accumulator = VPXMAX(0.0, *sr_accumulator);
  1698. // Small adjustment for cases where there is a zoom out
  1699. if (this_frame_mv_in_out > 0.0)
  1700. frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
  1701. // Q correction and scaling
  1702. // The 40.0 value here is an experimentally derived baseline minimum.
  1703. // This value is in line with the minimum per frame boost in the alt_ref
  1704. // boost calculation.
  1705. frame_boost = ((frame_boost + 40.0) * boost_q_correction);
  1706. return VPXMIN(frame_boost, max_boost * boost_q_correction);
  1707. }
  1708. static int calc_arf_boost(VP9_COMP *cpi, int f_frames, int b_frames) {
  1709. TWO_PASS *const twopass = &cpi->twopass;
  1710. int i;
  1711. double boost_score = 0.0;
  1712. double mv_ratio_accumulator = 0.0;
  1713. double decay_accumulator = 1.0;
  1714. double this_frame_mv_in_out = 0.0;
  1715. double mv_in_out_accumulator = 0.0;
  1716. double abs_mv_in_out_accumulator = 0.0;
  1717. int arf_boost;
  1718. int flash_detected = 0;
  1719. // Search forward from the proposed arf/next gf position.
  1720. for (i = 0; i < f_frames; ++i) {
  1721. const FIRSTPASS_STATS *this_frame = read_frame_stats(twopass, i);
  1722. if (this_frame == NULL) break;
  1723. // Update the motion related elements to the boost calculation.
  1724. accumulate_frame_motion_stats(
  1725. this_frame, &this_frame_mv_in_out, &mv_in_out_accumulator,
  1726. &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
  1727. // We want to discount the flash frame itself and the recovery
  1728. // frame that follows as both will have poor scores.
  1729. flash_detected = detect_flash(twopass, i) || detect_flash(twopass, i + 1);
  1730. // Accumulate the effect of prediction quality decay.
  1731. if (!flash_detected) {
  1732. decay_accumulator *= get_prediction_decay_rate(cpi, this_frame);
  1733. decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
  1734. ? MIN_DECAY_FACTOR
  1735. : decay_accumulator;
  1736. }
  1737. boost_score += decay_accumulator *
  1738. calc_frame_boost(cpi, this_frame, this_frame_mv_in_out);
  1739. }
  1740. arf_boost = (int)boost_score;
  1741. // Reset for backward looking loop.
  1742. boost_score = 0.0;
  1743. mv_ratio_accumulator = 0.0;
  1744. decay_accumulator = 1.0;
  1745. this_frame_mv_in_out = 0.0;
  1746. mv_in_out_accumulator = 0.0;
  1747. abs_mv_in_out_accumulator = 0.0;
  1748. // Search backward towards last gf position.
  1749. for (i = -1; i >= -b_frames; --i) {
  1750. const FIRSTPASS_STATS *this_frame = read_frame_stats(twopass, i);
  1751. if (this_frame == NULL) break;
  1752. // Update the motion related elements to the boost calculation.
  1753. accumulate_frame_motion_stats(
  1754. this_frame, &this_frame_mv_in_out, &mv_in_out_accumulator,
  1755. &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
  1756. // We want to discount the the flash frame itself and the recovery
  1757. // frame that follows as both will have poor scores.
  1758. flash_detected = detect_flash(twopass, i) || detect_flash(twopass, i + 1);
  1759. // Cumulative effect of prediction quality decay.
  1760. if (!flash_detected) {
  1761. decay_accumulator *= get_prediction_decay_rate(cpi, this_frame);
  1762. decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
  1763. ? MIN_DECAY_FACTOR
  1764. : decay_accumulator;
  1765. }
  1766. boost_score += decay_accumulator *
  1767. calc_frame_boost(cpi, this_frame, this_frame_mv_in_out);
  1768. }
  1769. arf_boost += (int)boost_score;
  1770. if (arf_boost < ((b_frames + f_frames) * 40))
  1771. arf_boost = ((b_frames + f_frames) * 40);
  1772. arf_boost = VPXMAX(arf_boost, MIN_ARF_GF_BOOST);
  1773. return arf_boost;
  1774. }
  1775. // Calculate a section intra ratio used in setting max loop filter.
  1776. static int calculate_section_intra_ratio(const FIRSTPASS_STATS *begin,
  1777. const FIRSTPASS_STATS *end,
  1778. int section_length) {
  1779. const FIRSTPASS_STATS *s = begin;
  1780. double intra_error = 0.0;
  1781. double coded_error = 0.0;
  1782. int i = 0;
  1783. while (s < end && i < section_length) {
  1784. intra_error += s->intra_error;
  1785. coded_error += s->coded_error;
  1786. ++s;
  1787. ++i;
  1788. }
  1789. return (int)(intra_error / DOUBLE_DIVIDE_CHECK(coded_error));
  1790. }
  1791. // Calculate the total bits to allocate in this GF/ARF group.
  1792. static int64_t calculate_total_gf_group_bits(VP9_COMP *cpi,
  1793. double gf_group_err) {
  1794. VP9_COMMON *const cm = &cpi->common;
  1795. const RATE_CONTROL *const rc = &cpi->rc;
  1796. const TWO_PASS *const twopass = &cpi->twopass;
  1797. const int max_bits = frame_max_bits(rc, &cpi->oxcf);
  1798. int64_t total_group_bits;
  1799. const int is_key_frame = frame_is_intra_only(cm);
  1800. const int arf_active_or_kf = is_key_frame || rc->source_alt_ref_active;
  1801. int gop_frames =
  1802. rc->baseline_gf_interval + rc->source_alt_ref_pending - arf_active_or_kf;
  1803. // Calculate the bits to be allocated to the group as a whole.
  1804. if ((twopass->kf_group_bits > 0) && (twopass->kf_group_error_left > 0.0)) {
  1805. total_group_bits = (int64_t)(twopass->kf_group_bits *
  1806. (gf_group_err / twopass->kf_group_error_left));
  1807. } else {
  1808. total_group_bits = 0;
  1809. }
  1810. // Clamp odd edge cases.
  1811. total_group_bits = (total_group_bits < 0)
  1812. ? 0
  1813. : (total_group_bits > twopass->kf_group_bits)
  1814. ? twopass->kf_group_bits
  1815. : total_group_bits;
  1816. // Clip based on user supplied data rate variability limit.
  1817. if (total_group_bits > (int64_t)max_bits * gop_frames)
  1818. total_group_bits = (int64_t)max_bits * gop_frames;
  1819. return total_group_bits;
  1820. }
  1821. // Calculate the number bits extra to assign to boosted frames in a group.
  1822. static int calculate_boost_bits(int frame_count, int boost,
  1823. int64_t total_group_bits) {
  1824. int allocation_chunks;
  1825. // return 0 for invalid inputs (could arise e.g. through rounding errors)
  1826. if (!boost || (total_group_bits <= 0) || (frame_count < 0)) return 0;
  1827. allocation_chunks = (frame_count * NORMAL_BOOST) + boost;
  1828. // Prevent overflow.
  1829. if (boost > 1023) {
  1830. int divisor = boost >> 10;
  1831. boost /= divisor;
  1832. allocation_chunks /= divisor;
  1833. }
  1834. // Calculate the number of extra bits for use in the boosted frame or frames.
  1835. return VPXMAX((int)(((int64_t)boost * total_group_bits) / allocation_chunks),
  1836. 0);
  1837. }
  1838. // Used in corpus vbr: Calculates the total normalized group complexity score
  1839. // for a given number of frames starting at the current position in the stats
  1840. // file.
  1841. static double calculate_group_score(VP9_COMP *cpi, double av_score,
  1842. int frame_count) {
  1843. VP9EncoderConfig *const oxcf = &cpi->oxcf;
  1844. TWO_PASS *const twopass = &cpi->twopass;
  1845. const FIRSTPASS_STATS *s = twopass->stats_in;
  1846. double score_total = 0.0;
  1847. int i = 0;
  1848. // We dont ever want to return a 0 score here.
  1849. if (frame_count == 0) return 1.0;
  1850. while ((i < frame_count) && (s < twopass->stats_in_end)) {
  1851. score_total += calculate_norm_frame_score(cpi, twopass, oxcf, s, av_score);
  1852. ++s;
  1853. ++i;
  1854. }
  1855. return score_total;
  1856. }
  1857. static void find_arf_order(VP9_COMP *cpi, GF_GROUP *gf_group,
  1858. int *index_counter, int depth, int start, int end) {
  1859. TWO_PASS *twopass = &cpi->twopass;
  1860. const FIRSTPASS_STATS *const start_pos = twopass->stats_in;
  1861. FIRSTPASS_STATS fpf_frame;
  1862. const int mid = (start + end + 1) >> 1;
  1863. const int min_frame_interval = 2;
  1864. int idx;
  1865. // Process regular P frames
  1866. if ((end - start < min_frame_interval) ||
  1867. (depth > gf_group->allowed_max_layer_depth)) {
  1868. for (idx = start; idx <= end; ++idx) {
  1869. gf_group->update_type[*index_counter] = LF_UPDATE;
  1870. gf_group->arf_src_offset[*index_counter] = 0;
  1871. gf_group->frame_gop_index[*index_counter] = idx;
  1872. gf_group->rf_level[*index_counter] = INTER_NORMAL;
  1873. gf_group->layer_depth[*index_counter] = depth;
  1874. gf_group->gfu_boost[*index_counter] = NORMAL_BOOST;
  1875. ++(*index_counter);
  1876. }
  1877. gf_group->max_layer_depth = VPXMAX(gf_group->max_layer_depth, depth);
  1878. return;
  1879. }
  1880. assert(abs(mid - start) >= 1 && abs(mid - end) >= 1);
  1881. // Process ARF frame
  1882. gf_group->layer_depth[*index_counter] = depth;
  1883. gf_group->update_type[*index_counter] = ARF_UPDATE;
  1884. gf_group->arf_src_offset[*index_counter] = mid - start;
  1885. gf_group->frame_gop_index[*index_counter] = mid;
  1886. gf_group->rf_level[*index_counter] = GF_ARF_LOW;
  1887. for (idx = 0; idx <= mid; ++idx)
  1888. if (EOF == input_stats(twopass, &fpf_frame)) break;
  1889. gf_group->gfu_boost[*index_counter] =
  1890. VPXMAX(MIN_ARF_GF_BOOST,
  1891. calc_arf_boost(cpi, end - mid + 1, mid - start) >> depth);
  1892. reset_fpf_position(twopass, start_pos);
  1893. ++(*index_counter);
  1894. find_arf_order(cpi, gf_group, index_counter, depth + 1, start, mid - 1);
  1895. gf_group->update_type[*index_counter] = USE_BUF_FRAME;
  1896. gf_group->arf_src_offset[*index_counter] = 0;
  1897. gf_group->frame_gop_index[*index_counter] = mid;
  1898. gf_group->rf_level[*index_counter] = INTER_NORMAL;
  1899. gf_group->layer_depth[*index_counter] = depth;
  1900. ++(*index_counter);
  1901. find_arf_order(cpi, gf_group, index_counter, depth + 1, mid + 1, end);
  1902. }
  1903. static INLINE void set_gf_overlay_frame_type(GF_GROUP *gf_group,
  1904. int frame_index,
  1905. int source_alt_ref_active) {
  1906. if (source_alt_ref_active) {
  1907. gf_group->update_type[frame_index] = OVERLAY_UPDATE;
  1908. gf_group->rf_level[frame_index] = INTER_NORMAL;
  1909. gf_group->layer_depth[frame_index] = MAX_ARF_LAYERS - 1;
  1910. gf_group->gfu_boost[frame_index] = NORMAL_BOOST;
  1911. } else {
  1912. gf_group->update_type[frame_index] = GF_UPDATE;
  1913. gf_group->rf_level[frame_index] = GF_ARF_STD;
  1914. gf_group->layer_depth[frame_index] = 0;
  1915. }
  1916. }
  1917. static void define_gf_group_structure(VP9_COMP *cpi) {
  1918. RATE_CONTROL *const rc = &cpi->rc;
  1919. TWO_PASS *const twopass = &cpi->twopass;
  1920. GF_GROUP *const gf_group = &twopass->gf_group;
  1921. int frame_index = 0;
  1922. int key_frame = cpi->common.frame_type == KEY_FRAME;
  1923. int layer_depth = 1;
  1924. int gop_frames =
  1925. rc->baseline_gf_interval - (key_frame || rc->source_alt_ref_pending);
  1926. gf_group->frame_start = cpi->common.current_video_frame;
  1927. gf_group->frame_end = gf_group->frame_start + rc->baseline_gf_interval;
  1928. gf_group->max_layer_depth = 0;
  1929. gf_group->allowed_max_layer_depth = 0;
  1930. // For key frames the frame target rate is already set and it
  1931. // is also the golden frame.
  1932. // === [frame_index == 0] ===
  1933. if (!key_frame)
  1934. set_gf_overlay_frame_type(gf_group, frame_index, rc->source_alt_ref_active);
  1935. ++frame_index;
  1936. // === [frame_index == 1] ===
  1937. if (rc->source_alt_ref_pending) {
  1938. gf_group->update_type[frame_index] = ARF_UPDATE;
  1939. gf_group->rf_level[frame_index] = GF_ARF_STD;
  1940. gf_group->layer_depth[frame_index] = layer_depth;
  1941. gf_group->arf_src_offset[frame_index] =
  1942. (unsigned char)(rc->baseline_gf_interval - 1);
  1943. gf_group->frame_gop_index[frame_index] = rc->baseline_gf_interval;
  1944. gf_group->max_layer_depth = 1;
  1945. ++frame_index;
  1946. ++layer_depth;
  1947. gf_group->allowed_max_layer_depth = cpi->oxcf.enable_auto_arf;
  1948. }
  1949. find_arf_order(cpi, gf_group, &frame_index, layer_depth, 1, gop_frames);
  1950. set_gf_overlay_frame_type(gf_group, frame_index, rc->source_alt_ref_pending);
  1951. gf_group->arf_src_offset[frame_index] = 0;
  1952. gf_group->frame_gop_index[frame_index] = rc->baseline_gf_interval;
  1953. // Set the frame ops number.
  1954. gf_group->gf_group_size = frame_index;
  1955. }
  1956. static void allocate_gf_group_bits(VP9_COMP *cpi, int64_t gf_group_bits,
  1957. int gf_arf_bits) {
  1958. VP9EncoderConfig *const oxcf = &cpi->oxcf;
  1959. RATE_CONTROL *const rc = &cpi->rc;
  1960. TWO_PASS *const twopass = &cpi->twopass;
  1961. GF_GROUP *const gf_group = &twopass->gf_group;
  1962. FIRSTPASS_STATS frame_stats;
  1963. int i;
  1964. int frame_index = 0;
  1965. int target_frame_size;
  1966. int key_frame;
  1967. const int max_bits = frame_max_bits(&cpi->rc, oxcf);
  1968. int64_t total_group_bits = gf_group_bits;
  1969. int mid_frame_idx;
  1970. int normal_frames;
  1971. int normal_frame_bits;
  1972. int last_frame_reduction = 0;
  1973. double av_score = 1.0;
  1974. double tot_norm_frame_score = 1.0;
  1975. double this_frame_score = 1.0;
  1976. // Define the GF structure and specify
  1977. int gop_frames = gf_group->gf_group_size;
  1978. key_frame = cpi->common.frame_type == KEY_FRAME;
  1979. // For key frames the frame target rate is already set and it
  1980. // is also the golden frame.
  1981. // === [frame_index == 0] ===
  1982. if (!key_frame) {
  1983. gf_group->bit_allocation[frame_index] =
  1984. rc->source_alt_ref_active ? 0 : gf_arf_bits;
  1985. }
  1986. // Deduct the boost bits for arf (or gf if it is not a key frame)
  1987. // from the group total.
  1988. if (rc->source_alt_ref_pending || !key_frame) total_group_bits -= gf_arf_bits;
  1989. ++frame_index;
  1990. // === [frame_index == 1] ===
  1991. // Store the bits to spend on the ARF if there is one.
  1992. if (rc->source_alt_ref_pending) {
  1993. gf_group->bit_allocation[frame_index] = gf_arf_bits;
  1994. ++frame_index;
  1995. }
  1996. // Define middle frame
  1997. mid_frame_idx = frame_index + (rc->baseline_gf_interval >> 1) - 1;
  1998. normal_frames = (rc->baseline_gf_interval - 1);
  1999. if (normal_frames > 1)
  2000. normal_frame_bits = (int)(total_group_bits / normal_frames);
  2001. else
  2002. normal_frame_bits = (int)total_group_bits;
  2003. gf_group->gfu_boost[1] = rc->gfu_boost;
  2004. if (cpi->multi_layer_arf) {
  2005. int idx;
  2006. int arf_depth_bits[MAX_ARF_LAYERS] = { 0 };
  2007. int arf_depth_count[MAX_ARF_LAYERS] = { 0 };
  2008. int arf_depth_boost[MAX_ARF_LAYERS] = { 0 };
  2009. int total_arfs = 1; // Account for the base layer ARF.
  2010. for (idx = 0; idx < gop_frames; ++idx) {
  2011. if (gf_group->update_type[idx] == ARF_UPDATE) {
  2012. arf_depth_boost[gf_group->layer_depth[idx]] += gf_group->gfu_boost[idx];
  2013. ++arf_depth_count[gf_group->layer_depth[idx]];
  2014. }
  2015. }
  2016. for (idx = 2; idx < MAX_ARF_LAYERS; ++idx) {
  2017. if (arf_depth_boost[idx] == 0) break;
  2018. arf_depth_bits[idx] = calculate_boost_bits(
  2019. rc->baseline_gf_interval - total_arfs - arf_depth_count[idx],
  2020. arf_depth_boost[idx], total_group_bits);
  2021. total_group_bits -= arf_depth_bits[idx];
  2022. total_arfs += arf_depth_count[idx];
  2023. }
  2024. // offset the base layer arf
  2025. normal_frames -= (total_arfs - 1);
  2026. if (normal_frames > 1)
  2027. normal_frame_bits = (int)(total_group_bits / normal_frames);
  2028. else
  2029. normal_frame_bits = (int)total_group_bits;
  2030. target_frame_size = normal_frame_bits;
  2031. target_frame_size =
  2032. clamp(target_frame_size, 0, VPXMIN(max_bits, (int)total_group_bits));
  2033. // The first layer ARF has its bit allocation assigned.
  2034. for (idx = frame_index; idx < gop_frames; ++idx) {
  2035. switch (gf_group->update_type[idx]) {
  2036. case ARF_UPDATE:
  2037. gf_group->bit_allocation[idx] =
  2038. (int)(((int64_t)arf_depth_bits[gf_group->layer_depth[idx]] *
  2039. gf_group->gfu_boost[idx]) /
  2040. arf_depth_boost[gf_group->layer_depth[idx]]);
  2041. break;
  2042. case USE_BUF_FRAME: gf_group->bit_allocation[idx] = 0; break;
  2043. default: gf_group->bit_allocation[idx] = target_frame_size; break;
  2044. }
  2045. }
  2046. gf_group->bit_allocation[idx] = 0;
  2047. return;
  2048. }
  2049. if (oxcf->vbr_corpus_complexity) {
  2050. av_score = get_distribution_av_err(cpi, twopass);
  2051. tot_norm_frame_score = calculate_group_score(cpi, av_score, normal_frames);
  2052. }
  2053. // Allocate bits to the other frames in the group.
  2054. for (i = 0; i < normal_frames; ++i) {
  2055. if (EOF == input_stats(twopass, &frame_stats)) break;
  2056. if (oxcf->vbr_corpus_complexity) {
  2057. this_frame_score = calculate_norm_frame_score(cpi, twopass, oxcf,
  2058. &frame_stats, av_score);
  2059. normal_frame_bits = (int)((double)total_group_bits *
  2060. (this_frame_score / tot_norm_frame_score));
  2061. }
  2062. target_frame_size = normal_frame_bits;
  2063. if ((i == (normal_frames - 1)) && (i >= 1)) {
  2064. last_frame_reduction = normal_frame_bits / 16;
  2065. target_frame_size -= last_frame_reduction;
  2066. }
  2067. target_frame_size =
  2068. clamp(target_frame_size, 0, VPXMIN(max_bits, (int)total_group_bits));
  2069. gf_group->bit_allocation[frame_index] = target_frame_size;
  2070. ++frame_index;
  2071. }
  2072. // Add in some extra bits for the middle frame in the group.
  2073. gf_group->bit_allocation[mid_frame_idx] += last_frame_reduction;
  2074. // Note:
  2075. // We need to configure the frame at the end of the sequence + 1 that will be
  2076. // the start frame for the next group. Otherwise prior to the call to
  2077. // vp9_rc_get_second_pass_params() the data will be undefined.
  2078. }
  2079. // Adjusts the ARNF filter for a GF group.
  2080. static void adjust_group_arnr_filter(VP9_COMP *cpi, double section_noise,
  2081. double section_inter,
  2082. double section_motion) {
  2083. TWO_PASS *const twopass = &cpi->twopass;
  2084. double section_zeromv = section_inter - section_motion;
  2085. twopass->arnr_strength_adjustment = 0;
  2086. if (section_noise < 150) {
  2087. twopass->arnr_strength_adjustment -= 1;
  2088. if (section_noise < 75) twopass->arnr_strength_adjustment -= 1;
  2089. } else if (section_noise > 250)
  2090. twopass->arnr_strength_adjustment += 1;
  2091. if (section_zeromv > 0.50) twopass->arnr_strength_adjustment += 1;
  2092. }
  2093. // Analyse and define a gf/arf group.
  2094. #define ARF_ABS_ZOOM_THRESH 4.0
  2095. #define MAX_GF_BOOST 5400
  2096. static void define_gf_group(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
  2097. VP9_COMMON *const cm = &cpi->common;
  2098. RATE_CONTROL *const rc = &cpi->rc;
  2099. VP9EncoderConfig *const oxcf = &cpi->oxcf;
  2100. TWO_PASS *const twopass = &cpi->twopass;
  2101. FIRSTPASS_STATS next_frame;
  2102. const FIRSTPASS_STATS *const start_pos = twopass->stats_in;
  2103. int i;
  2104. double gf_group_err = 0.0;
  2105. double gf_group_raw_error = 0.0;
  2106. double gf_group_noise = 0.0;
  2107. double gf_group_skip_pct = 0.0;
  2108. double gf_group_inactive_zone_rows = 0.0;
  2109. double gf_group_inter = 0.0;
  2110. double gf_group_motion = 0.0;
  2111. double gf_first_frame_err = 0.0;
  2112. double mod_frame_err = 0.0;
  2113. double mv_ratio_accumulator = 0.0;
  2114. double zero_motion_accumulator = 1.0;
  2115. double loop_decay_rate = 1.00;
  2116. double last_loop_decay_rate = 1.00;
  2117. double this_frame_mv_in_out = 0.0;
  2118. double mv_in_out_accumulator = 0.0;
  2119. double abs_mv_in_out_accumulator = 0.0;
  2120. double mv_ratio_accumulator_thresh;
  2121. double abs_mv_in_out_thresh;
  2122. double sr_accumulator = 0.0;
  2123. const double av_err = get_distribution_av_err(cpi, twopass);
  2124. unsigned int allow_alt_ref = is_altref_enabled(cpi);
  2125. int flash_detected;
  2126. int active_max_gf_interval;
  2127. int active_min_gf_interval;
  2128. int64_t gf_group_bits;
  2129. int gf_arf_bits;
  2130. const int is_key_frame = frame_is_intra_only(cm);
  2131. const int arf_active_or_kf = is_key_frame || rc->source_alt_ref_active;
  2132. int is_alt_ref_flash = 0;
  2133. double gop_intra_factor = 1.0;
  2134. int gop_frames;
  2135. // Reset the GF group data structures unless this is a key
  2136. // frame in which case it will already have been done.
  2137. if (is_key_frame == 0) {
  2138. vp9_zero(twopass->gf_group);
  2139. }
  2140. vpx_clear_system_state();
  2141. vp9_zero(next_frame);
  2142. // Load stats for the current frame.
  2143. mod_frame_err =
  2144. calculate_norm_frame_score(cpi, twopass, oxcf, this_frame, av_err);
  2145. // Note the error of the frame at the start of the group. This will be
  2146. // the GF frame error if we code a normal gf.
  2147. gf_first_frame_err = mod_frame_err;
  2148. // If this is a key frame or the overlay from a previous arf then
  2149. // the error score / cost of this frame has already been accounted for.
  2150. if (arf_active_or_kf) {
  2151. gf_group_err -= gf_first_frame_err;
  2152. gf_group_raw_error -= this_frame->coded_error;
  2153. gf_group_noise -= this_frame->frame_noise_energy;
  2154. gf_group_skip_pct -= this_frame->intra_skip_pct;
  2155. gf_group_inactive_zone_rows -= this_frame->inactive_zone_rows;
  2156. gf_group_inter -= this_frame->pcnt_inter;
  2157. gf_group_motion -= this_frame->pcnt_motion;
  2158. }
  2159. // Motion breakout threshold for loop below depends on image size.
  2160. mv_ratio_accumulator_thresh =
  2161. (cpi->initial_height + cpi->initial_width) / 4.0;
  2162. abs_mv_in_out_thresh = ARF_ABS_ZOOM_THRESH;
  2163. // Set a maximum and minimum interval for the GF group.
  2164. // If the image appears almost completely static we can extend beyond this.
  2165. {
  2166. int int_max_q = (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality,
  2167. cpi->common.bit_depth));
  2168. int q_term = (cm->current_video_frame == 0)
  2169. ? int_max_q / 32
  2170. : (int)(vp9_convert_qindex_to_q(rc->last_boosted_qindex,
  2171. cpi->common.bit_depth) /
  2172. 6);
  2173. active_min_gf_interval =
  2174. rc->min_gf_interval + arf_active_or_kf + VPXMIN(2, int_max_q / 200);
  2175. active_min_gf_interval =
  2176. VPXMIN(active_min_gf_interval, rc->max_gf_interval + arf_active_or_kf);
  2177. // The value chosen depends on the active Q range. At low Q we have
  2178. // bits to spare and are better with a smaller interval and smaller boost.
  2179. // At high Q when there are few bits to spare we are better with a longer
  2180. // interval to spread the cost of the GF.
  2181. active_max_gf_interval = 11 + arf_active_or_kf + VPXMIN(5, q_term);
  2182. // Force max GF interval to be odd.
  2183. active_max_gf_interval = active_max_gf_interval | 0x01;
  2184. // We have: active_min_gf_interval <=
  2185. // rc->max_gf_interval + arf_active_or_kf.
  2186. if (active_max_gf_interval < active_min_gf_interval) {
  2187. active_max_gf_interval = active_min_gf_interval;
  2188. } else {
  2189. active_max_gf_interval = VPXMIN(active_max_gf_interval,
  2190. rc->max_gf_interval + arf_active_or_kf);
  2191. }
  2192. // Would the active max drop us out just before the near the next kf?
  2193. if ((active_max_gf_interval <= rc->frames_to_key) &&
  2194. (active_max_gf_interval >= (rc->frames_to_key - rc->min_gf_interval)))
  2195. active_max_gf_interval = rc->frames_to_key / 2;
  2196. }
  2197. active_max_gf_interval =
  2198. VPXMAX(active_max_gf_interval, active_min_gf_interval);
  2199. if (cpi->multi_layer_arf) {
  2200. int layers = 0;
  2201. int max_layers = VPXMIN(MAX_ARF_LAYERS, cpi->oxcf.enable_auto_arf);
  2202. // Adapt the intra_error factor to active_max_gf_interval limit.
  2203. for (i = active_max_gf_interval; i > 0; i >>= 1) ++layers;
  2204. layers = VPXMIN(max_layers, layers);
  2205. gop_intra_factor += (layers * 0.25);
  2206. }
  2207. i = 0;
  2208. while (i < rc->static_scene_max_gf_interval && i < rc->frames_to_key) {
  2209. ++i;
  2210. // Accumulate error score of frames in this gf group.
  2211. mod_frame_err =
  2212. calculate_norm_frame_score(cpi, twopass, oxcf, this_frame, av_err);
  2213. gf_group_err += mod_frame_err;
  2214. gf_group_raw_error += this_frame->coded_error;
  2215. gf_group_noise += this_frame->frame_noise_energy;
  2216. gf_group_skip_pct += this_frame->intra_skip_pct;
  2217. gf_group_inactive_zone_rows += this_frame->inactive_zone_rows;
  2218. gf_group_inter += this_frame->pcnt_inter;
  2219. gf_group_motion += this_frame->pcnt_motion;
  2220. if (EOF == input_stats(twopass, &next_frame)) break;
  2221. // Test for the case where there is a brief flash but the prediction
  2222. // quality back to an earlier frame is then restored.
  2223. flash_detected = detect_flash(twopass, 0);
  2224. // Update the motion related elements to the boost calculation.
  2225. accumulate_frame_motion_stats(
  2226. &next_frame, &this_frame_mv_in_out, &mv_in_out_accumulator,
  2227. &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
  2228. // Monitor for static sections.
  2229. if ((rc->frames_since_key + i - 1) > 1) {
  2230. zero_motion_accumulator = VPXMIN(
  2231. zero_motion_accumulator, get_zero_motion_factor(cpi, &next_frame));
  2232. }
  2233. // Accumulate the effect of prediction quality decay.
  2234. if (!flash_detected) {
  2235. last_loop_decay_rate = loop_decay_rate;
  2236. loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
  2237. // Break clause to detect very still sections after motion. For example,
  2238. // a static image after a fade or other transition.
  2239. if (detect_transition_to_still(cpi, i, 5, loop_decay_rate,
  2240. last_loop_decay_rate)) {
  2241. allow_alt_ref = 0;
  2242. break;
  2243. }
  2244. // Update the accumulator for second ref error difference.
  2245. // This is intended to give an indication of how much the coded error is
  2246. // increasing over time.
  2247. if (i == 1) {
  2248. sr_accumulator += next_frame.coded_error;
  2249. } else {
  2250. sr_accumulator += (next_frame.sr_coded_error - next_frame.coded_error);
  2251. }
  2252. }
  2253. // Break out conditions.
  2254. // Break at maximum of active_max_gf_interval unless almost totally static.
  2255. //
  2256. // Note that the addition of a test of rc->source_alt_ref_active is
  2257. // deliberate. The effect of this is that after a normal altref group even
  2258. // if the material is static there will be one normal length GF group
  2259. // before allowing longer GF groups. The reason for this is that in cases
  2260. // such as slide shows where slides are separated by a complex transition
  2261. // such as a fade, the arf group spanning the transition may not be coded
  2262. // at a very high quality and hence this frame (with its overlay) is a
  2263. // poor golden frame to use for an extended group.
  2264. if (((i >= active_max_gf_interval) &&
  2265. ((zero_motion_accumulator < 0.995) || (rc->source_alt_ref_active))) ||
  2266. (
  2267. // Don't break out with a very short interval.
  2268. (i >= active_min_gf_interval) &&
  2269. // If possible dont break very close to a kf
  2270. ((rc->frames_to_key - i) >= rc->min_gf_interval) && (i & 0x01) &&
  2271. (!flash_detected) &&
  2272. ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) ||
  2273. (abs_mv_in_out_accumulator > abs_mv_in_out_thresh) ||
  2274. (sr_accumulator > gop_intra_factor * next_frame.intra_error)))) {
  2275. break;
  2276. }
  2277. *this_frame = next_frame;
  2278. }
  2279. // Was the group length constrained by the requirement for a new KF?
  2280. rc->constrained_gf_group = (i >= rc->frames_to_key) ? 1 : 0;
  2281. // Should we use the alternate reference frame.
  2282. if ((zero_motion_accumulator < 0.995) && allow_alt_ref &&
  2283. (twopass->kf_zeromotion_pct < STATIC_KF_GROUP_THRESH) &&
  2284. (i < cpi->oxcf.lag_in_frames) && (i >= rc->min_gf_interval)) {
  2285. const int forward_frames = (rc->frames_to_key - i >= i - 1)
  2286. ? i - 1
  2287. : VPXMAX(0, rc->frames_to_key - i);
  2288. // Calculate the boost for alt ref.
  2289. rc->gfu_boost = calc_arf_boost(cpi, forward_frames, (i - 1));
  2290. rc->source_alt_ref_pending = 1;
  2291. } else {
  2292. reset_fpf_position(twopass, start_pos);
  2293. rc->gfu_boost = VPXMIN(MAX_GF_BOOST, calc_arf_boost(cpi, (i - 1), 0));
  2294. rc->source_alt_ref_pending = 0;
  2295. }
  2296. #define LAST_ALR_ACTIVE_BEST_QUALITY_ADJUSTMENT_FACTOR 0.2
  2297. rc->arf_active_best_quality_adjustment_factor = 1.0;
  2298. if (rc->source_alt_ref_pending && !is_lossless_requested(&cpi->oxcf) &&
  2299. rc->frames_to_key <= rc->arf_active_best_quality_adjustment_window) {
  2300. rc->arf_active_best_quality_adjustment_factor =
  2301. LAST_ALR_ACTIVE_BEST_QUALITY_ADJUSTMENT_FACTOR +
  2302. (1.0 - LAST_ALR_ACTIVE_BEST_QUALITY_ADJUSTMENT_FACTOR) *
  2303. (rc->frames_to_key - i) /
  2304. VPXMAX(1, (rc->arf_active_best_quality_adjustment_window - i));
  2305. }
  2306. #ifdef AGGRESSIVE_VBR
  2307. // Limit maximum boost based on interval length.
  2308. rc->gfu_boost = VPXMIN((int)rc->gfu_boost, i * 140);
  2309. #else
  2310. rc->gfu_boost = VPXMIN((int)rc->gfu_boost, i * 200);
  2311. #endif
  2312. // Cap the ARF boost when perceptual quality AQ mode is enabled. This is
  2313. // designed to improve the perceptual quality of high value content and to
  2314. // make consistent quality across consecutive frames. It will hurt objective
  2315. // quality.
  2316. if (oxcf->aq_mode == PERCEPTUAL_AQ)
  2317. rc->gfu_boost = VPXMIN(rc->gfu_boost, MIN_ARF_GF_BOOST);
  2318. rc->baseline_gf_interval = i - rc->source_alt_ref_pending;
  2319. // Reset the file position.
  2320. reset_fpf_position(twopass, start_pos);
  2321. if (rc->source_alt_ref_pending)
  2322. is_alt_ref_flash = detect_flash(twopass, rc->baseline_gf_interval);
  2323. // Calculate the bits to be allocated to the gf/arf group as a whole
  2324. gf_group_bits = calculate_total_gf_group_bits(cpi, gf_group_err);
  2325. gop_frames =
  2326. rc->baseline_gf_interval + rc->source_alt_ref_pending - arf_active_or_kf;
  2327. // Store the average moise level measured for the group
  2328. // TODO(any): Experiment with removal of else condition (gop_frames = 0) so
  2329. // that consumption of group noise energy is based on previous gf group
  2330. if (gop_frames > 0)
  2331. twopass->gf_group.group_noise_energy = (int)(gf_group_noise / gop_frames);
  2332. else
  2333. twopass->gf_group.group_noise_energy = 0;
  2334. // Calculate an estimate of the maxq needed for the group.
  2335. // We are more aggressive about correcting for sections
  2336. // where there could be significant overshoot than for easier
  2337. // sections where we do not wish to risk creating an overshoot
  2338. // of the allocated bit budget.
  2339. if ((cpi->oxcf.rc_mode != VPX_Q) && (rc->baseline_gf_interval > 1)) {
  2340. const int vbr_group_bits_per_frame = (int)(gf_group_bits / gop_frames);
  2341. const double group_av_err = gf_group_raw_error / gop_frames;
  2342. const double group_av_noise = gf_group_noise / gop_frames;
  2343. const double group_av_skip_pct = gf_group_skip_pct / gop_frames;
  2344. const double group_av_inactive_zone = ((gf_group_inactive_zone_rows * 2) /
  2345. (gop_frames * (double)cm->mb_rows));
  2346. int tmp_q = get_twopass_worst_quality(
  2347. cpi, group_av_err, (group_av_skip_pct + group_av_inactive_zone),
  2348. group_av_noise, vbr_group_bits_per_frame);
  2349. twopass->active_worst_quality =
  2350. (tmp_q + (twopass->active_worst_quality * 3)) >> 2;
  2351. #if CONFIG_ALWAYS_ADJUST_BPM
  2352. // Reset rolling actual and target bits counters for ARF groups.
  2353. twopass->rolling_arf_group_target_bits = 0;
  2354. twopass->rolling_arf_group_actual_bits = 0;
  2355. #endif
  2356. }
  2357. // Context Adjustment of ARNR filter strength
  2358. if (rc->baseline_gf_interval > 1) {
  2359. adjust_group_arnr_filter(cpi, (gf_group_noise / gop_frames),
  2360. (gf_group_inter / gop_frames),
  2361. (gf_group_motion / gop_frames));
  2362. } else {
  2363. twopass->arnr_strength_adjustment = 0;
  2364. }
  2365. // Calculate the extra bits to be used for boosted frame(s)
  2366. gf_arf_bits = calculate_boost_bits((rc->baseline_gf_interval - 1),
  2367. rc->gfu_boost, gf_group_bits);
  2368. // Adjust KF group bits and error remaining.
  2369. twopass->kf_group_error_left -= gf_group_err;
  2370. // Decide GOP structure.
  2371. define_gf_group_structure(cpi);
  2372. // Allocate bits to each of the frames in the GF group.
  2373. allocate_gf_group_bits(cpi, gf_group_bits, gf_arf_bits);
  2374. // Reset the file position.
  2375. reset_fpf_position(twopass, start_pos);
  2376. // Calculate a section intra ratio used in setting max loop filter.
  2377. twopass->section_intra_rating = calculate_section_intra_ratio(
  2378. start_pos, twopass->stats_in_end, rc->baseline_gf_interval);
  2379. if (oxcf->resize_mode == RESIZE_DYNAMIC) {
  2380. // Default to starting GF groups at normal frame size.
  2381. cpi->rc.next_frame_size_selector = UNSCALED;
  2382. }
  2383. #if !CONFIG_ALWAYS_ADJUST_BPM
  2384. // Reset rolling actual and target bits counters for ARF groups.
  2385. twopass->rolling_arf_group_target_bits = 0;
  2386. twopass->rolling_arf_group_actual_bits = 0;
  2387. #endif
  2388. rc->preserve_arf_as_gld = rc->preserve_next_arf_as_gld;
  2389. rc->preserve_next_arf_as_gld = 0;
  2390. // If alt ref frame is flash do not set preserve_arf_as_gld
  2391. if (!is_lossless_requested(&cpi->oxcf) && !cpi->use_svc &&
  2392. cpi->oxcf.aq_mode == NO_AQ && cpi->multi_layer_arf && !is_alt_ref_flash)
  2393. rc->preserve_next_arf_as_gld = 1;
  2394. }
  2395. // Intra / Inter threshold very low
  2396. #define VERY_LOW_II 1.5
  2397. // Clean slide transitions we expect a sharp single frame spike in error.
  2398. #define ERROR_SPIKE 5.0
  2399. // Slide show transition detection.
  2400. // Tests for case where there is very low error either side of the current frame
  2401. // but much higher just for this frame. This can help detect key frames in
  2402. // slide shows even where the slides are pictures of different sizes.
  2403. // Also requires that intra and inter errors are very similar to help eliminate
  2404. // harmful false positives.
  2405. // It will not help if the transition is a fade or other multi-frame effect.
  2406. static int slide_transition(const FIRSTPASS_STATS *this_frame,
  2407. const FIRSTPASS_STATS *last_frame,
  2408. const FIRSTPASS_STATS *next_frame) {
  2409. return (this_frame->intra_error < (this_frame->coded_error * VERY_LOW_II)) &&
  2410. (this_frame->coded_error > (last_frame->coded_error * ERROR_SPIKE)) &&
  2411. (this_frame->coded_error > (next_frame->coded_error * ERROR_SPIKE));
  2412. }
  2413. // This test looks for anomalous changes in the nature of the intra signal
  2414. // related to the previous and next frame as an indicator for coding a key
  2415. // frame. This test serves to detect some additional scene cuts,
  2416. // especially in lowish motion and low contrast sections, that are missed
  2417. // by the other tests.
  2418. static int intra_step_transition(const FIRSTPASS_STATS *this_frame,
  2419. const FIRSTPASS_STATS *last_frame,
  2420. const FIRSTPASS_STATS *next_frame) {
  2421. double last_ii_ratio;
  2422. double this_ii_ratio;
  2423. double next_ii_ratio;
  2424. double last_pcnt_intra = 1.0 - last_frame->pcnt_inter;
  2425. double this_pcnt_intra = 1.0 - this_frame->pcnt_inter;
  2426. double next_pcnt_intra = 1.0 - next_frame->pcnt_inter;
  2427. double mod_this_intra = this_pcnt_intra + this_frame->pcnt_neutral;
  2428. // Calculate ii ratio for this frame last frame and next frame.
  2429. last_ii_ratio =
  2430. last_frame->intra_error / DOUBLE_DIVIDE_CHECK(last_frame->coded_error);
  2431. this_ii_ratio =
  2432. this_frame->intra_error / DOUBLE_DIVIDE_CHECK(this_frame->coded_error);
  2433. next_ii_ratio =
  2434. next_frame->intra_error / DOUBLE_DIVIDE_CHECK(next_frame->coded_error);
  2435. // Return true the intra/inter ratio for the current frame is
  2436. // low but better in the next and previous frame and the relative useage of
  2437. // intra in the current frame is markedly higher than the last and next frame.
  2438. if ((this_ii_ratio < 2.0) && (last_ii_ratio > 2.25) &&
  2439. (next_ii_ratio > 2.25) && (this_pcnt_intra > (3 * last_pcnt_intra)) &&
  2440. (this_pcnt_intra > (3 * next_pcnt_intra)) &&
  2441. ((this_pcnt_intra > 0.075) || (mod_this_intra > 0.85))) {
  2442. return 1;
  2443. // Very low inter intra ratio (i.e. not much gain from inter coding), most
  2444. // blocks neutral on coding method and better inter prediction either side
  2445. } else if ((this_ii_ratio < 1.25) && (mod_this_intra > 0.85) &&
  2446. (this_ii_ratio < last_ii_ratio * 0.9) &&
  2447. (this_ii_ratio < next_ii_ratio * 0.9)) {
  2448. return 1;
  2449. } else {
  2450. return 0;
  2451. }
  2452. }
  2453. // Minimum % intra coding observed in first pass (1.0 = 100%)
  2454. #define MIN_INTRA_LEVEL 0.25
  2455. // Threshold for use of the lagging second reference frame. Scene cuts do not
  2456. // usually have a high second ref useage.
  2457. #define SECOND_REF_USEAGE_THRESH 0.2
  2458. // Hard threshold where the first pass chooses intra for almost all blocks.
  2459. // In such a case even if the frame is not a scene cut coding a key frame
  2460. // may be a good option.
  2461. #define VERY_LOW_INTER_THRESH 0.05
  2462. // Maximum threshold for the relative ratio of intra error score vs best
  2463. // inter error score.
  2464. #define KF_II_ERR_THRESHOLD 2.5
  2465. #define KF_II_MAX 128.0
  2466. #define II_FACTOR 12.5
  2467. // Test for very low intra complexity which could cause false key frames
  2468. #define V_LOW_INTRA 0.5
  2469. static int test_candidate_kf(TWO_PASS *twopass,
  2470. const FIRSTPASS_STATS *last_frame,
  2471. const FIRSTPASS_STATS *this_frame,
  2472. const FIRSTPASS_STATS *next_frame) {
  2473. int is_viable_kf = 0;
  2474. double pcnt_intra = 1.0 - this_frame->pcnt_inter;
  2475. // Does the frame satisfy the primary criteria of a key frame?
  2476. // See above for an explanation of the test criteria.
  2477. // If so, then examine how well it predicts subsequent frames.
  2478. if (!detect_flash(twopass, -1) && !detect_flash(twopass, 0) &&
  2479. (this_frame->pcnt_second_ref < SECOND_REF_USEAGE_THRESH) &&
  2480. ((this_frame->pcnt_inter < VERY_LOW_INTER_THRESH) ||
  2481. (slide_transition(this_frame, last_frame, next_frame)) ||
  2482. (intra_step_transition(this_frame, last_frame, next_frame)) ||
  2483. (((this_frame->coded_error > (next_frame->coded_error * 1.2)) &&
  2484. (this_frame->coded_error > (last_frame->coded_error * 1.2))) &&
  2485. (pcnt_intra > MIN_INTRA_LEVEL) &&
  2486. ((pcnt_intra + this_frame->pcnt_neutral) > 0.5) &&
  2487. ((this_frame->intra_error /
  2488. DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) <
  2489. KF_II_ERR_THRESHOLD)))) {
  2490. int i;
  2491. const FIRSTPASS_STATS *start_pos = twopass->stats_in;
  2492. FIRSTPASS_STATS local_next_frame = *next_frame;
  2493. double boost_score = 0.0;
  2494. double old_boost_score = 0.0;
  2495. double decay_accumulator = 1.0;
  2496. // Examine how well the key frame predicts subsequent frames.
  2497. for (i = 0; i < 16; ++i) {
  2498. double next_iiratio = (II_FACTOR * local_next_frame.intra_error /
  2499. DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error));
  2500. if (next_iiratio > KF_II_MAX) next_iiratio = KF_II_MAX;
  2501. // Cumulative effect of decay in prediction quality.
  2502. if (local_next_frame.pcnt_inter > 0.85)
  2503. decay_accumulator *= local_next_frame.pcnt_inter;
  2504. else
  2505. decay_accumulator *= (0.85 + local_next_frame.pcnt_inter) / 2.0;
  2506. // Keep a running total.
  2507. boost_score += (decay_accumulator * next_iiratio);
  2508. // Test various breakout clauses.
  2509. if ((local_next_frame.pcnt_inter < 0.05) || (next_iiratio < 1.5) ||
  2510. (((local_next_frame.pcnt_inter - local_next_frame.pcnt_neutral) <
  2511. 0.20) &&
  2512. (next_iiratio < 3.0)) ||
  2513. ((boost_score - old_boost_score) < 3.0) ||
  2514. (local_next_frame.intra_error < V_LOW_INTRA)) {
  2515. break;
  2516. }
  2517. old_boost_score = boost_score;
  2518. // Get the next frame details
  2519. if (EOF == input_stats(twopass, &local_next_frame)) break;
  2520. }
  2521. // If there is tolerable prediction for at least the next 3 frames then
  2522. // break out else discard this potential key frame and move on
  2523. if (boost_score > 30.0 && (i > 3)) {
  2524. is_viable_kf = 1;
  2525. } else {
  2526. // Reset the file position
  2527. reset_fpf_position(twopass, start_pos);
  2528. is_viable_kf = 0;
  2529. }
  2530. }
  2531. return is_viable_kf;
  2532. }
  2533. #define FRAMES_TO_CHECK_DECAY 8
  2534. #define MIN_KF_TOT_BOOST 300
  2535. #define DEFAULT_SCAN_FRAMES_FOR_KF_BOOST 32
  2536. #define MAX_SCAN_FRAMES_FOR_KF_BOOST 48
  2537. #define MIN_SCAN_FRAMES_FOR_KF_BOOST 32
  2538. #define KF_ABS_ZOOM_THRESH 6.0
  2539. #ifdef AGGRESSIVE_VBR
  2540. #define KF_MAX_FRAME_BOOST 80.0
  2541. #define MAX_KF_TOT_BOOST 4800
  2542. #else
  2543. #define KF_MAX_FRAME_BOOST 96.0
  2544. #define MAX_KF_TOT_BOOST 5400
  2545. #endif
  2546. static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
  2547. int i, j;
  2548. RATE_CONTROL *const rc = &cpi->rc;
  2549. TWO_PASS *const twopass = &cpi->twopass;
  2550. GF_GROUP *const gf_group = &twopass->gf_group;
  2551. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  2552. const FIRSTPASS_STATS first_frame = *this_frame;
  2553. const FIRSTPASS_STATS *const start_position = twopass->stats_in;
  2554. FIRSTPASS_STATS next_frame;
  2555. FIRSTPASS_STATS last_frame;
  2556. int kf_bits = 0;
  2557. int64_t max_kf_bits;
  2558. double decay_accumulator = 1.0;
  2559. double zero_motion_accumulator = 1.0;
  2560. double zero_motion_sum = 0.0;
  2561. double zero_motion_avg;
  2562. double motion_compensable_sum = 0.0;
  2563. double motion_compensable_avg;
  2564. int num_frames = 0;
  2565. int kf_boost_scan_frames = DEFAULT_SCAN_FRAMES_FOR_KF_BOOST;
  2566. double boost_score = 0.0;
  2567. double kf_mod_err = 0.0;
  2568. double kf_raw_err = 0.0;
  2569. double kf_group_err = 0.0;
  2570. double recent_loop_decay[FRAMES_TO_CHECK_DECAY];
  2571. double sr_accumulator = 0.0;
  2572. double abs_mv_in_out_accumulator = 0.0;
  2573. const double av_err = get_distribution_av_err(cpi, twopass);
  2574. vp9_zero(next_frame);
  2575. cpi->common.frame_type = KEY_FRAME;
  2576. rc->frames_since_key = 0;
  2577. // Reset the GF group data structures.
  2578. vp9_zero(*gf_group);
  2579. // Is this a forced key frame by interval.
  2580. rc->this_key_frame_forced = rc->next_key_frame_forced;
  2581. // Clear the alt ref active flag and last group multi arf flags as they
  2582. // can never be set for a key frame.
  2583. rc->source_alt_ref_active = 0;
  2584. // KF is always a GF so clear frames till next gf counter.
  2585. rc->frames_till_gf_update_due = 0;
  2586. rc->frames_to_key = 1;
  2587. twopass->kf_group_bits = 0; // Total bits available to kf group
  2588. twopass->kf_group_error_left = 0.0; // Group modified error score.
  2589. kf_raw_err = this_frame->intra_error;
  2590. kf_mod_err =
  2591. calculate_norm_frame_score(cpi, twopass, oxcf, this_frame, av_err);
  2592. // Initialize the decay rates for the recent frames to check
  2593. for (j = 0; j < FRAMES_TO_CHECK_DECAY; ++j) recent_loop_decay[j] = 1.0;
  2594. // Find the next keyframe.
  2595. i = 0;
  2596. while (twopass->stats_in < twopass->stats_in_end &&
  2597. rc->frames_to_key < cpi->oxcf.key_freq) {
  2598. // Accumulate kf group error.
  2599. kf_group_err +=
  2600. calculate_norm_frame_score(cpi, twopass, oxcf, this_frame, av_err);
  2601. // Load the next frame's stats.
  2602. last_frame = *this_frame;
  2603. input_stats(twopass, this_frame);
  2604. // Provided that we are not at the end of the file...
  2605. if (cpi->oxcf.auto_key && twopass->stats_in < twopass->stats_in_end) {
  2606. double loop_decay_rate;
  2607. // Check for a scene cut.
  2608. if (test_candidate_kf(twopass, &last_frame, this_frame,
  2609. twopass->stats_in))
  2610. break;
  2611. // How fast is the prediction quality decaying?
  2612. loop_decay_rate = get_prediction_decay_rate(cpi, twopass->stats_in);
  2613. // We want to know something about the recent past... rather than
  2614. // as used elsewhere where we are concerned with decay in prediction
  2615. // quality since the last GF or KF.
  2616. recent_loop_decay[i % FRAMES_TO_CHECK_DECAY] = loop_decay_rate;
  2617. decay_accumulator = 1.0;
  2618. for (j = 0; j < FRAMES_TO_CHECK_DECAY; ++j)
  2619. decay_accumulator *= recent_loop_decay[j];
  2620. // Special check for transition or high motion followed by a
  2621. // static scene.
  2622. if (detect_transition_to_still(cpi, i, cpi->oxcf.key_freq - i,
  2623. loop_decay_rate, decay_accumulator))
  2624. break;
  2625. // Step on to the next frame.
  2626. ++rc->frames_to_key;
  2627. // If we don't have a real key frame within the next two
  2628. // key_freq intervals then break out of the loop.
  2629. if (rc->frames_to_key >= 2 * cpi->oxcf.key_freq) break;
  2630. } else {
  2631. ++rc->frames_to_key;
  2632. }
  2633. ++i;
  2634. }
  2635. // If there is a max kf interval set by the user we must obey it.
  2636. // We already breakout of the loop above at 2x max.
  2637. // This code centers the extra kf if the actual natural interval
  2638. // is between 1x and 2x.
  2639. if (cpi->oxcf.auto_key && rc->frames_to_key > cpi->oxcf.key_freq) {
  2640. FIRSTPASS_STATS tmp_frame = first_frame;
  2641. rc->frames_to_key /= 2;
  2642. // Reset to the start of the group.
  2643. reset_fpf_position(twopass, start_position);
  2644. kf_group_err = 0.0;
  2645. // Rescan to get the correct error data for the forced kf group.
  2646. for (i = 0; i < rc->frames_to_key; ++i) {
  2647. kf_group_err +=
  2648. calculate_norm_frame_score(cpi, twopass, oxcf, &tmp_frame, av_err);
  2649. input_stats(twopass, &tmp_frame);
  2650. }
  2651. rc->next_key_frame_forced = 1;
  2652. } else if (twopass->stats_in == twopass->stats_in_end ||
  2653. rc->frames_to_key >= cpi->oxcf.key_freq) {
  2654. rc->next_key_frame_forced = 1;
  2655. } else {
  2656. rc->next_key_frame_forced = 0;
  2657. }
  2658. // Special case for the last key frame of the file.
  2659. if (twopass->stats_in >= twopass->stats_in_end) {
  2660. // Accumulate kf group error.
  2661. kf_group_err +=
  2662. calculate_norm_frame_score(cpi, twopass, oxcf, this_frame, av_err);
  2663. }
  2664. // Calculate the number of bits that should be assigned to the kf group.
  2665. if (twopass->bits_left > 0 && twopass->normalized_score_left > 0.0) {
  2666. // Maximum number of bits for a single normal frame (not key frame).
  2667. const int max_bits = frame_max_bits(rc, &cpi->oxcf);
  2668. // Maximum number of bits allocated to the key frame group.
  2669. int64_t max_grp_bits;
  2670. // Default allocation based on bits left and relative
  2671. // complexity of the section.
  2672. twopass->kf_group_bits = (int64_t)(
  2673. twopass->bits_left * (kf_group_err / twopass->normalized_score_left));
  2674. // Clip based on maximum per frame rate defined by the user.
  2675. max_grp_bits = (int64_t)max_bits * (int64_t)rc->frames_to_key;
  2676. if (twopass->kf_group_bits > max_grp_bits)
  2677. twopass->kf_group_bits = max_grp_bits;
  2678. } else {
  2679. twopass->kf_group_bits = 0;
  2680. }
  2681. twopass->kf_group_bits = VPXMAX(0, twopass->kf_group_bits);
  2682. // Reset the first pass file position.
  2683. reset_fpf_position(twopass, start_position);
  2684. // Scan through the kf group collating various stats used to determine
  2685. // how many bits to spend on it.
  2686. boost_score = 0.0;
  2687. for (i = 0; i < VPXMIN(MAX_SCAN_FRAMES_FOR_KF_BOOST, (rc->frames_to_key - 1));
  2688. ++i) {
  2689. if (EOF == input_stats(twopass, &next_frame)) break;
  2690. zero_motion_sum += next_frame.pcnt_inter - next_frame.pcnt_motion;
  2691. motion_compensable_sum +=
  2692. 1 - (double)next_frame.coded_error / next_frame.intra_error;
  2693. num_frames++;
  2694. }
  2695. if (num_frames >= MIN_SCAN_FRAMES_FOR_KF_BOOST) {
  2696. zero_motion_avg = zero_motion_sum / num_frames;
  2697. motion_compensable_avg = motion_compensable_sum / num_frames;
  2698. kf_boost_scan_frames = (int)(VPXMAX(64 * zero_motion_avg - 16,
  2699. 160 * motion_compensable_avg - 112));
  2700. kf_boost_scan_frames =
  2701. VPXMAX(VPXMIN(kf_boost_scan_frames, MAX_SCAN_FRAMES_FOR_KF_BOOST),
  2702. MIN_SCAN_FRAMES_FOR_KF_BOOST);
  2703. }
  2704. reset_fpf_position(twopass, start_position);
  2705. for (i = 0; i < (rc->frames_to_key - 1); ++i) {
  2706. if (EOF == input_stats(twopass, &next_frame)) break;
  2707. // The zero motion test here insures that if we mark a kf group as static
  2708. // it is static throughout not just the first KF_BOOST_SCAN_MAX_FRAMES.
  2709. // It also allows for a larger boost on long static groups.
  2710. if ((i <= kf_boost_scan_frames) || (zero_motion_accumulator >= 0.99)) {
  2711. double frame_boost;
  2712. double zm_factor;
  2713. // Monitor for static sections.
  2714. // First frame in kf group the second ref indicator is invalid.
  2715. if (i > 0) {
  2716. zero_motion_accumulator = VPXMIN(
  2717. zero_motion_accumulator, get_zero_motion_factor(cpi, &next_frame));
  2718. } else {
  2719. zero_motion_accumulator =
  2720. next_frame.pcnt_inter - next_frame.pcnt_motion;
  2721. }
  2722. // Factor 0.75-1.25 based on how much of frame is static.
  2723. zm_factor = (0.75 + (zero_motion_accumulator / 2.0));
  2724. // The second (lagging) ref error is not valid immediately after
  2725. // a key frame because either the lag has not built up (in the case of
  2726. // the first key frame or it points to a refernce before the new key
  2727. // frame.
  2728. if (i < 2) sr_accumulator = 0.0;
  2729. frame_boost = calc_kf_frame_boost(cpi, &next_frame, &sr_accumulator, 0,
  2730. KF_MAX_FRAME_BOOST * zm_factor);
  2731. boost_score += frame_boost;
  2732. // Measure of zoom. Large zoom tends to indicate reduced boost.
  2733. abs_mv_in_out_accumulator +=
  2734. fabs(next_frame.mv_in_out_count * next_frame.pcnt_motion);
  2735. if ((frame_boost < 25.00) ||
  2736. (abs_mv_in_out_accumulator > KF_ABS_ZOOM_THRESH) ||
  2737. (sr_accumulator > (kf_raw_err * 1.50)))
  2738. break;
  2739. } else {
  2740. break;
  2741. }
  2742. }
  2743. reset_fpf_position(twopass, start_position);
  2744. // Store the zero motion percentage
  2745. twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0);
  2746. // Calculate a section intra ratio used in setting max loop filter.
  2747. twopass->key_frame_section_intra_rating = calculate_section_intra_ratio(
  2748. start_position, twopass->stats_in_end, rc->frames_to_key);
  2749. // Special case for static / slide show content but dont apply
  2750. // if the kf group is very short.
  2751. if ((zero_motion_accumulator > 0.99) && (rc->frames_to_key > 8)) {
  2752. rc->kf_boost = MAX_KF_TOT_BOOST;
  2753. } else {
  2754. // Apply various clamps for min and max boost
  2755. rc->kf_boost = VPXMAX((int)boost_score, (rc->frames_to_key * 3));
  2756. rc->kf_boost = VPXMAX(rc->kf_boost, MIN_KF_TOT_BOOST);
  2757. rc->kf_boost = VPXMIN(rc->kf_boost, MAX_KF_TOT_BOOST);
  2758. }
  2759. // Work out how many bits to allocate for the key frame itself.
  2760. kf_bits = calculate_boost_bits((rc->frames_to_key - 1), rc->kf_boost,
  2761. twopass->kf_group_bits);
  2762. // Based on the spatial complexity, increase the bits allocated to key frame.
  2763. kf_bits +=
  2764. (int)((twopass->kf_group_bits - kf_bits) * (kf_mod_err / kf_group_err));
  2765. max_kf_bits =
  2766. twopass->kf_group_bits - (rc->frames_to_key - 1) * FRAME_OVERHEAD_BITS;
  2767. max_kf_bits = lclamp(max_kf_bits, 0, INT_MAX);
  2768. kf_bits = VPXMIN(kf_bits, (int)max_kf_bits);
  2769. twopass->kf_group_bits -= kf_bits;
  2770. // Save the bits to spend on the key frame.
  2771. gf_group->bit_allocation[0] = kf_bits;
  2772. gf_group->update_type[0] = KF_UPDATE;
  2773. gf_group->rf_level[0] = KF_STD;
  2774. // Note the total error score of the kf group minus the key frame itself.
  2775. twopass->kf_group_error_left = (kf_group_err - kf_mod_err);
  2776. // Adjust the count of total modified error left.
  2777. // The count of bits left is adjusted elsewhere based on real coded frame
  2778. // sizes.
  2779. twopass->normalized_score_left -= kf_group_err;
  2780. if (oxcf->resize_mode == RESIZE_DYNAMIC) {
  2781. // Default to normal-sized frame on keyframes.
  2782. cpi->rc.next_frame_size_selector = UNSCALED;
  2783. }
  2784. #define ARF_ACTIVE_BEST_QUALITY_ADJUSTMENT_WINDOW_SIZE 64
  2785. // TODO(ravi.chaudhary@ittiam.com): Experiment without the below min
  2786. // condition. This might be helpful for small key frame intervals.
  2787. rc->arf_active_best_quality_adjustment_window =
  2788. VPXMIN(ARF_ACTIVE_BEST_QUALITY_ADJUSTMENT_WINDOW_SIZE, rc->frames_to_key);
  2789. }
  2790. static int is_skippable_frame(const VP9_COMP *cpi) {
  2791. // If the current frame does not have non-zero motion vector detected in the
  2792. // first pass, and so do its previous and forward frames, then this frame
  2793. // can be skipped for partition check, and the partition size is assigned
  2794. // according to the variance
  2795. const TWO_PASS *const twopass = &cpi->twopass;
  2796. return (!frame_is_intra_only(&cpi->common) &&
  2797. twopass->stats_in - 2 > twopass->stats_in_start &&
  2798. twopass->stats_in < twopass->stats_in_end &&
  2799. (twopass->stats_in - 1)->pcnt_inter -
  2800. (twopass->stats_in - 1)->pcnt_motion ==
  2801. 1 &&
  2802. (twopass->stats_in - 2)->pcnt_inter -
  2803. (twopass->stats_in - 2)->pcnt_motion ==
  2804. 1 &&
  2805. twopass->stats_in->pcnt_inter - twopass->stats_in->pcnt_motion == 1);
  2806. }
  2807. void vp9_rc_get_second_pass_params(VP9_COMP *cpi) {
  2808. VP9_COMMON *const cm = &cpi->common;
  2809. RATE_CONTROL *const rc = &cpi->rc;
  2810. TWO_PASS *const twopass = &cpi->twopass;
  2811. GF_GROUP *const gf_group = &twopass->gf_group;
  2812. FIRSTPASS_STATS this_frame;
  2813. if (!twopass->stats_in) return;
  2814. // If this is an arf frame then we dont want to read the stats file or
  2815. // advance the input pointer as we already have what we need.
  2816. if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
  2817. int target_rate;
  2818. vp9_zero(this_frame);
  2819. this_frame =
  2820. cpi->twopass.stats_in_start[cm->current_video_frame +
  2821. gf_group->arf_src_offset[gf_group->index]];
  2822. vp9_configure_buffer_updates(cpi, gf_group->index);
  2823. target_rate = gf_group->bit_allocation[gf_group->index];
  2824. target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate);
  2825. rc->base_frame_target = target_rate;
  2826. cm->frame_type = INTER_FRAME;
  2827. // Do the firstpass stats indicate that this frame is skippable for the
  2828. // partition search?
  2829. if (cpi->sf.allow_partition_search_skip && cpi->oxcf.pass == 2 &&
  2830. !cpi->use_svc) {
  2831. cpi->partition_search_skippable_frame = is_skippable_frame(cpi);
  2832. }
  2833. // The multiplication by 256 reverses a scaling factor of (>> 8)
  2834. // applied when combining MB error values for the frame.
  2835. twopass->mb_av_energy = log((this_frame.intra_error * 256.0) + 1.0);
  2836. twopass->mb_smooth_pct = this_frame.intra_smooth_pct;
  2837. return;
  2838. }
  2839. vpx_clear_system_state();
  2840. if (cpi->oxcf.rc_mode == VPX_Q) {
  2841. twopass->active_worst_quality = cpi->oxcf.cq_level;
  2842. } else if (cm->current_video_frame == 0) {
  2843. const int frames_left =
  2844. (int)(twopass->total_stats.count - cm->current_video_frame);
  2845. // Special case code for first frame.
  2846. const int section_target_bandwidth =
  2847. (int)(twopass->bits_left / frames_left);
  2848. const double section_length = twopass->total_left_stats.count;
  2849. const double section_error =
  2850. twopass->total_left_stats.coded_error / section_length;
  2851. const double section_intra_skip =
  2852. twopass->total_left_stats.intra_skip_pct / section_length;
  2853. const double section_inactive_zone =
  2854. (twopass->total_left_stats.inactive_zone_rows * 2) /
  2855. ((double)cm->mb_rows * section_length);
  2856. const double section_noise =
  2857. twopass->total_left_stats.frame_noise_energy / section_length;
  2858. int tmp_q;
  2859. tmp_q = get_twopass_worst_quality(
  2860. cpi, section_error, section_intra_skip + section_inactive_zone,
  2861. section_noise, section_target_bandwidth);
  2862. twopass->active_worst_quality = tmp_q;
  2863. twopass->baseline_active_worst_quality = tmp_q;
  2864. rc->ni_av_qi = tmp_q;
  2865. rc->last_q[INTER_FRAME] = tmp_q;
  2866. rc->avg_q = vp9_convert_qindex_to_q(tmp_q, cm->bit_depth);
  2867. rc->avg_frame_qindex[INTER_FRAME] = tmp_q;
  2868. rc->last_q[KEY_FRAME] = (tmp_q + cpi->oxcf.best_allowed_q) / 2;
  2869. rc->avg_frame_qindex[KEY_FRAME] = rc->last_q[KEY_FRAME];
  2870. }
  2871. vp9_zero(this_frame);
  2872. if (EOF == input_stats(twopass, &this_frame)) return;
  2873. // Set the frame content type flag.
  2874. if (this_frame.intra_skip_pct >= FC_ANIMATION_THRESH)
  2875. twopass->fr_content_type = FC_GRAPHICS_ANIMATION;
  2876. else
  2877. twopass->fr_content_type = FC_NORMAL;
  2878. // Keyframe and section processing.
  2879. if (rc->frames_to_key == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY)) {
  2880. FIRSTPASS_STATS this_frame_copy;
  2881. this_frame_copy = this_frame;
  2882. // Define next KF group and assign bits to it.
  2883. find_next_key_frame(cpi, &this_frame);
  2884. this_frame = this_frame_copy;
  2885. } else {
  2886. cm->frame_type = INTER_FRAME;
  2887. }
  2888. // Define a new GF/ARF group. (Should always enter here for key frames).
  2889. if (rc->frames_till_gf_update_due == 0) {
  2890. define_gf_group(cpi, &this_frame);
  2891. rc->frames_till_gf_update_due = rc->baseline_gf_interval;
  2892. #if ARF_STATS_OUTPUT
  2893. {
  2894. FILE *fpfile;
  2895. fpfile = fopen("arf.stt", "a");
  2896. ++arf_count;
  2897. fprintf(fpfile, "%10d %10ld %10d %10d %10ld %10ld\n",
  2898. cm->current_video_frame, rc->frames_till_gf_update_due,
  2899. rc->kf_boost, arf_count, rc->gfu_boost, cm->frame_type);
  2900. fclose(fpfile);
  2901. }
  2902. #endif
  2903. }
  2904. vp9_configure_buffer_updates(cpi, gf_group->index);
  2905. // Do the firstpass stats indicate that this frame is skippable for the
  2906. // partition search?
  2907. if (cpi->sf.allow_partition_search_skip && cpi->oxcf.pass == 2 &&
  2908. !cpi->use_svc) {
  2909. cpi->partition_search_skippable_frame = is_skippable_frame(cpi);
  2910. }
  2911. rc->base_frame_target = gf_group->bit_allocation[gf_group->index];
  2912. // The multiplication by 256 reverses a scaling factor of (>> 8)
  2913. // applied when combining MB error values for the frame.
  2914. twopass->mb_av_energy = log((this_frame.intra_error * 256.0) + 1.0);
  2915. twopass->mb_smooth_pct = this_frame.intra_smooth_pct;
  2916. // Update the total stats remaining structure.
  2917. subtract_stats(&twopass->total_left_stats, &this_frame);
  2918. }
  2919. #define MINQ_ADJ_LIMIT 48
  2920. #define MINQ_ADJ_LIMIT_CQ 20
  2921. #define HIGH_UNDERSHOOT_RATIO 2
  2922. void vp9_twopass_postencode_update(VP9_COMP *cpi) {
  2923. TWO_PASS *const twopass = &cpi->twopass;
  2924. RATE_CONTROL *const rc = &cpi->rc;
  2925. VP9_COMMON *const cm = &cpi->common;
  2926. const int bits_used = rc->base_frame_target;
  2927. // VBR correction is done through rc->vbr_bits_off_target. Based on the
  2928. // sign of this value, a limited % adjustment is made to the target rate
  2929. // of subsequent frames, to try and push it back towards 0. This method
  2930. // is designed to prevent extreme behaviour at the end of a clip
  2931. // or group of frames.
  2932. rc->vbr_bits_off_target += rc->base_frame_target - rc->projected_frame_size;
  2933. twopass->bits_left = VPXMAX(twopass->bits_left - bits_used, 0);
  2934. // Target vs actual bits for this arf group.
  2935. twopass->rolling_arf_group_target_bits += rc->this_frame_target;
  2936. twopass->rolling_arf_group_actual_bits += rc->projected_frame_size;
  2937. // Calculate the pct rc error.
  2938. if (rc->total_actual_bits) {
  2939. rc->rate_error_estimate =
  2940. (int)((rc->vbr_bits_off_target * 100) / rc->total_actual_bits);
  2941. rc->rate_error_estimate = clamp(rc->rate_error_estimate, -100, 100);
  2942. } else {
  2943. rc->rate_error_estimate = 0;
  2944. }
  2945. if (cpi->common.frame_type != KEY_FRAME) {
  2946. twopass->kf_group_bits -= bits_used;
  2947. twopass->last_kfgroup_zeromotion_pct = twopass->kf_zeromotion_pct;
  2948. }
  2949. twopass->kf_group_bits = VPXMAX(twopass->kf_group_bits, 0);
  2950. // Increment the gf group index ready for the next frame.
  2951. ++twopass->gf_group.index;
  2952. // If the rate control is drifting consider adjustment to min or maxq.
  2953. if ((cpi->oxcf.rc_mode != VPX_Q) && !cpi->rc.is_src_frame_alt_ref) {
  2954. const int maxq_adj_limit =
  2955. rc->worst_quality - twopass->active_worst_quality;
  2956. const int minq_adj_limit =
  2957. (cpi->oxcf.rc_mode == VPX_CQ ? MINQ_ADJ_LIMIT_CQ : MINQ_ADJ_LIMIT);
  2958. int aq_extend_min = 0;
  2959. int aq_extend_max = 0;
  2960. // Extend min or Max Q range to account for imbalance from the base
  2961. // value when using AQ.
  2962. if (cpi->oxcf.aq_mode != NO_AQ && cpi->oxcf.aq_mode != PSNR_AQ &&
  2963. cpi->oxcf.aq_mode != PERCEPTUAL_AQ) {
  2964. if (cm->seg.aq_av_offset < 0) {
  2965. // The balance of the AQ map tends towarda lowering the average Q.
  2966. aq_extend_min = 0;
  2967. aq_extend_max = VPXMIN(maxq_adj_limit, -cm->seg.aq_av_offset);
  2968. } else {
  2969. // The balance of the AQ map tends towards raising the average Q.
  2970. aq_extend_min = VPXMIN(minq_adj_limit, cm->seg.aq_av_offset);
  2971. aq_extend_max = 0;
  2972. }
  2973. }
  2974. // Undershoot.
  2975. if (rc->rate_error_estimate > cpi->oxcf.under_shoot_pct) {
  2976. --twopass->extend_maxq;
  2977. if (rc->rolling_target_bits >= rc->rolling_actual_bits)
  2978. ++twopass->extend_minq;
  2979. // Overshoot.
  2980. } else if (rc->rate_error_estimate < -cpi->oxcf.over_shoot_pct) {
  2981. --twopass->extend_minq;
  2982. if (rc->rolling_target_bits < rc->rolling_actual_bits)
  2983. ++twopass->extend_maxq;
  2984. } else {
  2985. // Adjustment for extreme local overshoot.
  2986. if (rc->projected_frame_size > (2 * rc->base_frame_target) &&
  2987. rc->projected_frame_size > (2 * rc->avg_frame_bandwidth))
  2988. ++twopass->extend_maxq;
  2989. // Unwind undershoot or overshoot adjustment.
  2990. if (rc->rolling_target_bits < rc->rolling_actual_bits)
  2991. --twopass->extend_minq;
  2992. else if (rc->rolling_target_bits > rc->rolling_actual_bits)
  2993. --twopass->extend_maxq;
  2994. }
  2995. twopass->extend_minq =
  2996. clamp(twopass->extend_minq, aq_extend_min, minq_adj_limit);
  2997. twopass->extend_maxq =
  2998. clamp(twopass->extend_maxq, aq_extend_max, maxq_adj_limit);
  2999. // If there is a big and undexpected undershoot then feed the extra
  3000. // bits back in quickly. One situation where this may happen is if a
  3001. // frame is unexpectedly almost perfectly predicted by the ARF or GF
  3002. // but not very well predcited by the previous frame.
  3003. if (!frame_is_kf_gf_arf(cpi) && !cpi->rc.is_src_frame_alt_ref) {
  3004. int fast_extra_thresh = rc->base_frame_target / HIGH_UNDERSHOOT_RATIO;
  3005. if (rc->projected_frame_size < fast_extra_thresh) {
  3006. rc->vbr_bits_off_target_fast +=
  3007. fast_extra_thresh - rc->projected_frame_size;
  3008. rc->vbr_bits_off_target_fast =
  3009. VPXMIN(rc->vbr_bits_off_target_fast, (4 * rc->avg_frame_bandwidth));
  3010. // Fast adaptation of minQ if necessary to use up the extra bits.
  3011. if (rc->avg_frame_bandwidth) {
  3012. twopass->extend_minq_fast =
  3013. (int)(rc->vbr_bits_off_target_fast * 8 / rc->avg_frame_bandwidth);
  3014. }
  3015. twopass->extend_minq_fast = VPXMIN(
  3016. twopass->extend_minq_fast, minq_adj_limit - twopass->extend_minq);
  3017. } else if (rc->vbr_bits_off_target_fast) {
  3018. twopass->extend_minq_fast = VPXMIN(
  3019. twopass->extend_minq_fast, minq_adj_limit - twopass->extend_minq);
  3020. } else {
  3021. twopass->extend_minq_fast = 0;
  3022. }
  3023. }
  3024. }
  3025. }