vp9_ratectrl.c 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174
  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <assert.h>
  11. #include <limits.h>
  12. #include <math.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "./vpx_dsp_rtcd.h"
  17. #include "vpx_dsp/vpx_dsp_common.h"
  18. #include "vpx_mem/vpx_mem.h"
  19. #include "vpx_ports/mem.h"
  20. #include "vpx_ports/system_state.h"
  21. #include "vp9/common/vp9_alloccommon.h"
  22. #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
  23. #include "vp9/common/vp9_common.h"
  24. #include "vp9/common/vp9_entropymode.h"
  25. #include "vp9/common/vp9_quant_common.h"
  26. #include "vp9/common/vp9_seg_common.h"
  27. #include "vp9/encoder/vp9_encodemv.h"
  28. #include "vp9/encoder/vp9_ratectrl.h"
  29. // Max rate per frame for 1080P and below encodes if no level requirement given.
  30. // For larger formats limit to MAX_MB_RATE bits per MB
  31. // 4Mbits is derived from the level requirement for level 4 (1080P 30) which
  32. // requires that HW can sustain a rate of 16Mbits over a 4 frame group.
  33. // If a lower level requirement is specified then this may over ride this value.
  34. #define MAX_MB_RATE 250
  35. #define MAXRATE_1080P 4000000
  36. #define DEFAULT_KF_BOOST 2000
  37. #define DEFAULT_GF_BOOST 2000
  38. #define LIMIT_QRANGE_FOR_ALTREF_AND_KEY 1
  39. #define MIN_BPB_FACTOR 0.005
  40. #define MAX_BPB_FACTOR 50
  41. #if CONFIG_VP9_HIGHBITDEPTH
  42. #define ASSIGN_MINQ_TABLE(bit_depth, name) \
  43. do { \
  44. switch (bit_depth) { \
  45. case VPX_BITS_8: name = name##_8; break; \
  46. case VPX_BITS_10: name = name##_10; break; \
  47. default: \
  48. assert(bit_depth == VPX_BITS_12); \
  49. name = name##_12; \
  50. break; \
  51. } \
  52. } while (0)
  53. #else
  54. #define ASSIGN_MINQ_TABLE(bit_depth, name) \
  55. do { \
  56. (void)bit_depth; \
  57. name = name##_8; \
  58. } while (0)
  59. #endif
  60. // Tables relating active max Q to active min Q
  61. static int kf_low_motion_minq_8[QINDEX_RANGE];
  62. static int kf_high_motion_minq_8[QINDEX_RANGE];
  63. static int arfgf_low_motion_minq_8[QINDEX_RANGE];
  64. static int arfgf_high_motion_minq_8[QINDEX_RANGE];
  65. static int inter_minq_8[QINDEX_RANGE];
  66. static int rtc_minq_8[QINDEX_RANGE];
  67. #if CONFIG_VP9_HIGHBITDEPTH
  68. static int kf_low_motion_minq_10[QINDEX_RANGE];
  69. static int kf_high_motion_minq_10[QINDEX_RANGE];
  70. static int arfgf_low_motion_minq_10[QINDEX_RANGE];
  71. static int arfgf_high_motion_minq_10[QINDEX_RANGE];
  72. static int inter_minq_10[QINDEX_RANGE];
  73. static int rtc_minq_10[QINDEX_RANGE];
  74. static int kf_low_motion_minq_12[QINDEX_RANGE];
  75. static int kf_high_motion_minq_12[QINDEX_RANGE];
  76. static int arfgf_low_motion_minq_12[QINDEX_RANGE];
  77. static int arfgf_high_motion_minq_12[QINDEX_RANGE];
  78. static int inter_minq_12[QINDEX_RANGE];
  79. static int rtc_minq_12[QINDEX_RANGE];
  80. #endif
  81. #ifdef AGGRESSIVE_VBR
  82. static int gf_high = 2400;
  83. static int gf_low = 400;
  84. static int kf_high = 4000;
  85. static int kf_low = 400;
  86. #else
  87. static int gf_high = 2000;
  88. static int gf_low = 400;
  89. static int kf_high = 4800;
  90. static int kf_low = 300;
  91. #endif
  92. // Functions to compute the active minq lookup table entries based on a
  93. // formulaic approach to facilitate easier adjustment of the Q tables.
  94. // The formulae were derived from computing a 3rd order polynomial best
  95. // fit to the original data (after plotting real maxq vs minq (not q index))
  96. static int get_minq_index(double maxq, double x3, double x2, double x1,
  97. vpx_bit_depth_t bit_depth) {
  98. int i;
  99. const double minqtarget = VPXMIN(((x3 * maxq + x2) * maxq + x1) * maxq, maxq);
  100. // Special case handling to deal with the step from q2.0
  101. // down to lossless mode represented by q 1.0.
  102. if (minqtarget <= 2.0) return 0;
  103. for (i = 0; i < QINDEX_RANGE; i++) {
  104. if (minqtarget <= vp9_convert_qindex_to_q(i, bit_depth)) return i;
  105. }
  106. return QINDEX_RANGE - 1;
  107. }
  108. static void init_minq_luts(int *kf_low_m, int *kf_high_m, int *arfgf_low,
  109. int *arfgf_high, int *inter, int *rtc,
  110. vpx_bit_depth_t bit_depth) {
  111. int i;
  112. for (i = 0; i < QINDEX_RANGE; i++) {
  113. const double maxq = vp9_convert_qindex_to_q(i, bit_depth);
  114. kf_low_m[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.150, bit_depth);
  115. kf_high_m[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.45, bit_depth);
  116. #ifdef AGGRESSIVE_VBR
  117. arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.275, bit_depth);
  118. inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.80, bit_depth);
  119. #else
  120. arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.30, bit_depth);
  121. inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth);
  122. #endif
  123. arfgf_high[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth);
  124. rtc[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth);
  125. }
  126. }
  127. void vp9_rc_init_minq_luts(void) {
  128. init_minq_luts(kf_low_motion_minq_8, kf_high_motion_minq_8,
  129. arfgf_low_motion_minq_8, arfgf_high_motion_minq_8,
  130. inter_minq_8, rtc_minq_8, VPX_BITS_8);
  131. #if CONFIG_VP9_HIGHBITDEPTH
  132. init_minq_luts(kf_low_motion_minq_10, kf_high_motion_minq_10,
  133. arfgf_low_motion_minq_10, arfgf_high_motion_minq_10,
  134. inter_minq_10, rtc_minq_10, VPX_BITS_10);
  135. init_minq_luts(kf_low_motion_minq_12, kf_high_motion_minq_12,
  136. arfgf_low_motion_minq_12, arfgf_high_motion_minq_12,
  137. inter_minq_12, rtc_minq_12, VPX_BITS_12);
  138. #endif
  139. }
  140. // These functions use formulaic calculations to make playing with the
  141. // quantizer tables easier. If necessary they can be replaced by lookup
  142. // tables if and when things settle down in the experimental bitstream
  143. double vp9_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth) {
  144. // Convert the index to a real Q value (scaled down to match old Q values)
  145. #if CONFIG_VP9_HIGHBITDEPTH
  146. switch (bit_depth) {
  147. case VPX_BITS_8: return vp9_ac_quant(qindex, 0, bit_depth) / 4.0;
  148. case VPX_BITS_10: return vp9_ac_quant(qindex, 0, bit_depth) / 16.0;
  149. default:
  150. assert(bit_depth == VPX_BITS_12);
  151. return vp9_ac_quant(qindex, 0, bit_depth) / 64.0;
  152. }
  153. #else
  154. return vp9_ac_quant(qindex, 0, bit_depth) / 4.0;
  155. #endif
  156. }
  157. int vp9_convert_q_to_qindex(double q_val, vpx_bit_depth_t bit_depth) {
  158. int i;
  159. for (i = 0; i < QINDEX_RANGE; ++i)
  160. if (vp9_convert_qindex_to_q(i, bit_depth) >= q_val) break;
  161. if (i == QINDEX_RANGE) i--;
  162. return i;
  163. }
  164. int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
  165. double correction_factor, vpx_bit_depth_t bit_depth) {
  166. const double q = vp9_convert_qindex_to_q(qindex, bit_depth);
  167. int enumerator = frame_type == KEY_FRAME ? 2700000 : 1800000;
  168. assert(correction_factor <= MAX_BPB_FACTOR &&
  169. correction_factor >= MIN_BPB_FACTOR);
  170. // q based adjustment to baseline enumerator
  171. enumerator += (int)(enumerator * q) >> 12;
  172. return (int)(enumerator * correction_factor / q);
  173. }
  174. int vp9_estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs,
  175. double correction_factor,
  176. vpx_bit_depth_t bit_depth) {
  177. const int bpm =
  178. (int)(vp9_rc_bits_per_mb(frame_type, q, correction_factor, bit_depth));
  179. return VPXMAX(FRAME_OVERHEAD_BITS,
  180. (int)(((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS));
  181. }
  182. int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) {
  183. const RATE_CONTROL *rc = &cpi->rc;
  184. const VP9EncoderConfig *oxcf = &cpi->oxcf;
  185. const int min_frame_target =
  186. VPXMAX(rc->min_frame_bandwidth, rc->avg_frame_bandwidth >> 5);
  187. if (target < min_frame_target) target = min_frame_target;
  188. if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) {
  189. // If there is an active ARF at this location use the minimum
  190. // bits on this frame even if it is a constructed arf.
  191. // The active maximum quantizer insures that an appropriate
  192. // number of bits will be spent if needed for constructed ARFs.
  193. target = min_frame_target;
  194. }
  195. // Clip the frame target to the maximum allowed value.
  196. if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth;
  197. if (oxcf->rc_max_inter_bitrate_pct) {
  198. const int max_rate =
  199. rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100;
  200. target = VPXMIN(target, max_rate);
  201. }
  202. return target;
  203. }
  204. int vp9_rc_clamp_iframe_target_size(const VP9_COMP *const cpi, int target) {
  205. const RATE_CONTROL *rc = &cpi->rc;
  206. const VP9EncoderConfig *oxcf = &cpi->oxcf;
  207. if (oxcf->rc_max_intra_bitrate_pct) {
  208. const int max_rate =
  209. rc->avg_frame_bandwidth * oxcf->rc_max_intra_bitrate_pct / 100;
  210. target = VPXMIN(target, max_rate);
  211. }
  212. if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth;
  213. return target;
  214. }
  215. // TODO(marpan/jianj): bits_off_target and buffer_level are used in the saame
  216. // way for CBR mode, for the buffering updates below. Look into removing one
  217. // of these (i.e., bits_off_target).
  218. // Update the buffer level before encoding with the per-frame-bandwidth,
  219. static void update_buffer_level_preencode(VP9_COMP *cpi) {
  220. RATE_CONTROL *const rc = &cpi->rc;
  221. rc->bits_off_target += rc->avg_frame_bandwidth;
  222. // Clip the buffer level to the maximum specified buffer size.
  223. rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size);
  224. rc->buffer_level = rc->bits_off_target;
  225. }
  226. // Update the buffer level before encoding with the per-frame-bandwidth
  227. // for SVC. The current and all upper temporal layers are updated, needed
  228. // for the layered rate control which involves cumulative buffer levels for
  229. // the temporal layers. Allow for using the timestamp(pts) delta for the
  230. // framerate when the set_ref_frame_config is used.
  231. static void update_buffer_level_svc_preencode(VP9_COMP *cpi) {
  232. SVC *const svc = &cpi->svc;
  233. int i;
  234. // Set this to 1 to use timestamp delta for "framerate" under
  235. // ref_frame_config usage.
  236. int use_timestamp = 1;
  237. const int64_t ts_delta =
  238. svc->time_stamp_superframe - svc->time_stamp_prev[svc->spatial_layer_id];
  239. for (i = svc->temporal_layer_id; i < svc->number_temporal_layers; ++i) {
  240. const int layer =
  241. LAYER_IDS_TO_IDX(svc->spatial_layer_id, i, svc->number_temporal_layers);
  242. LAYER_CONTEXT *const lc = &svc->layer_context[layer];
  243. RATE_CONTROL *const lrc = &lc->rc;
  244. if (use_timestamp && cpi->svc.use_set_ref_frame_config &&
  245. svc->number_temporal_layers == 1 && ts_delta > 0 &&
  246. svc->current_superframe > 0) {
  247. // TODO(marpan): This may need to be modified for temporal layers.
  248. const double framerate_pts = 10000000.0 / ts_delta;
  249. lrc->bits_off_target += (int)(lc->target_bandwidth / framerate_pts);
  250. } else {
  251. lrc->bits_off_target += (int)(lc->target_bandwidth / lc->framerate);
  252. }
  253. // Clip buffer level to maximum buffer size for the layer.
  254. lrc->bits_off_target =
  255. VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
  256. lrc->buffer_level = lrc->bits_off_target;
  257. if (i == svc->temporal_layer_id) {
  258. cpi->rc.bits_off_target = lrc->bits_off_target;
  259. cpi->rc.buffer_level = lrc->buffer_level;
  260. }
  261. }
  262. }
  263. // Update the buffer level for higher temporal layers, given the encoded current
  264. // temporal layer.
  265. static void update_layer_buffer_level_postencode(SVC *svc,
  266. int encoded_frame_size) {
  267. int i = 0;
  268. const int current_temporal_layer = svc->temporal_layer_id;
  269. for (i = current_temporal_layer + 1; i < svc->number_temporal_layers; ++i) {
  270. const int layer =
  271. LAYER_IDS_TO_IDX(svc->spatial_layer_id, i, svc->number_temporal_layers);
  272. LAYER_CONTEXT *lc = &svc->layer_context[layer];
  273. RATE_CONTROL *lrc = &lc->rc;
  274. lrc->bits_off_target -= encoded_frame_size;
  275. // Clip buffer level to maximum buffer size for the layer.
  276. lrc->bits_off_target =
  277. VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
  278. lrc->buffer_level = lrc->bits_off_target;
  279. }
  280. }
  281. // Update the buffer level after encoding with encoded frame size.
  282. static void update_buffer_level_postencode(VP9_COMP *cpi,
  283. int encoded_frame_size) {
  284. RATE_CONTROL *const rc = &cpi->rc;
  285. rc->bits_off_target -= encoded_frame_size;
  286. // Clip the buffer level to the maximum specified buffer size.
  287. rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size);
  288. // For screen-content mode, and if frame-dropper is off, don't let buffer
  289. // level go below threshold, given here as -rc->maximum_ buffer_size.
  290. if (cpi->oxcf.content == VP9E_CONTENT_SCREEN &&
  291. cpi->oxcf.drop_frames_water_mark == 0)
  292. rc->bits_off_target = VPXMAX(rc->bits_off_target, -rc->maximum_buffer_size);
  293. rc->buffer_level = rc->bits_off_target;
  294. if (is_one_pass_cbr_svc(cpi)) {
  295. update_layer_buffer_level_postencode(&cpi->svc, encoded_frame_size);
  296. }
  297. }
  298. int vp9_rc_get_default_min_gf_interval(int width, int height,
  299. double framerate) {
  300. // Assume we do not need any constraint lower than 4K 20 fps
  301. static const double factor_safe = 3840 * 2160 * 20.0;
  302. const double factor = width * height * framerate;
  303. const int default_interval =
  304. clamp((int)(framerate * 0.125), MIN_GF_INTERVAL, MAX_GF_INTERVAL);
  305. if (factor <= factor_safe)
  306. return default_interval;
  307. else
  308. return VPXMAX(default_interval,
  309. (int)(MIN_GF_INTERVAL * factor / factor_safe + 0.5));
  310. // Note this logic makes:
  311. // 4K24: 5
  312. // 4K30: 6
  313. // 4K60: 12
  314. }
  315. int vp9_rc_get_default_max_gf_interval(double framerate, int min_gf_interval) {
  316. int interval = VPXMIN(MAX_GF_INTERVAL, (int)(framerate * 0.75));
  317. interval += (interval & 0x01); // Round to even value
  318. return VPXMAX(interval, min_gf_interval);
  319. }
  320. void vp9_rc_init(const VP9EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) {
  321. int i;
  322. if (pass == 0 && oxcf->rc_mode == VPX_CBR) {
  323. rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
  324. rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
  325. } else {
  326. rc->avg_frame_qindex[KEY_FRAME] =
  327. (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
  328. rc->avg_frame_qindex[INTER_FRAME] =
  329. (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
  330. }
  331. rc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
  332. rc->last_q[INTER_FRAME] = oxcf->worst_allowed_q;
  333. rc->buffer_level = rc->starting_buffer_level;
  334. rc->bits_off_target = rc->starting_buffer_level;
  335. rc->rolling_target_bits = rc->avg_frame_bandwidth;
  336. rc->rolling_actual_bits = rc->avg_frame_bandwidth;
  337. rc->long_rolling_target_bits = rc->avg_frame_bandwidth;
  338. rc->long_rolling_actual_bits = rc->avg_frame_bandwidth;
  339. rc->total_actual_bits = 0;
  340. rc->total_target_bits = 0;
  341. rc->total_target_vs_actual = 0;
  342. rc->avg_frame_low_motion = 0;
  343. rc->count_last_scene_change = 0;
  344. rc->af_ratio_onepass_vbr = 10;
  345. rc->prev_avg_source_sad_lag = 0;
  346. rc->high_source_sad = 0;
  347. rc->reset_high_source_sad = 0;
  348. rc->high_source_sad_lagindex = -1;
  349. rc->high_num_blocks_with_motion = 0;
  350. rc->hybrid_intra_scene_change = 0;
  351. rc->re_encode_maxq_scene_change = 0;
  352. rc->alt_ref_gf_group = 0;
  353. rc->last_frame_is_src_altref = 0;
  354. rc->fac_active_worst_inter = 150;
  355. rc->fac_active_worst_gf = 100;
  356. rc->force_qpmin = 0;
  357. for (i = 0; i < MAX_LAG_BUFFERS; ++i) rc->avg_source_sad[i] = 0;
  358. rc->frames_since_key = 8; // Sensible default for first frame.
  359. rc->this_key_frame_forced = 0;
  360. rc->next_key_frame_forced = 0;
  361. rc->source_alt_ref_pending = 0;
  362. rc->source_alt_ref_active = 0;
  363. rc->frames_till_gf_update_due = 0;
  364. rc->ni_av_qi = oxcf->worst_allowed_q;
  365. rc->ni_tot_qi = 0;
  366. rc->ni_frames = 0;
  367. rc->tot_q = 0.0;
  368. rc->avg_q = vp9_convert_qindex_to_q(oxcf->worst_allowed_q, oxcf->bit_depth);
  369. for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
  370. rc->rate_correction_factors[i] = 1.0;
  371. rc->damped_adjustment[i] = 0;
  372. }
  373. rc->min_gf_interval = oxcf->min_gf_interval;
  374. rc->max_gf_interval = oxcf->max_gf_interval;
  375. if (rc->min_gf_interval == 0)
  376. rc->min_gf_interval = vp9_rc_get_default_min_gf_interval(
  377. oxcf->width, oxcf->height, oxcf->init_framerate);
  378. if (rc->max_gf_interval == 0)
  379. rc->max_gf_interval = vp9_rc_get_default_max_gf_interval(
  380. oxcf->init_framerate, rc->min_gf_interval);
  381. rc->baseline_gf_interval = (rc->min_gf_interval + rc->max_gf_interval) / 2;
  382. rc->force_max_q = 0;
  383. rc->last_post_encode_dropped_scene_change = 0;
  384. rc->use_post_encode_drop = 0;
  385. rc->ext_use_post_encode_drop = 0;
  386. rc->arf_active_best_quality_adjustment_factor = 1.0;
  387. rc->preserve_arf_as_gld = 0;
  388. rc->preserve_next_arf_as_gld = 0;
  389. rc->show_arf_as_gld = 0;
  390. }
  391. static int check_buffer_above_thresh(VP9_COMP *cpi, int drop_mark) {
  392. SVC *svc = &cpi->svc;
  393. if (!cpi->use_svc || cpi->svc.framedrop_mode != FULL_SUPERFRAME_DROP) {
  394. RATE_CONTROL *const rc = &cpi->rc;
  395. return (rc->buffer_level > drop_mark);
  396. } else {
  397. int i;
  398. // For SVC in the FULL_SUPERFRAME_DROP): the condition on
  399. // buffer (if its above threshold, so no drop) is checked on current and
  400. // upper spatial layers. If any spatial layer is not above threshold then
  401. // we return 0.
  402. for (i = svc->spatial_layer_id; i < svc->number_spatial_layers; ++i) {
  403. const int layer = LAYER_IDS_TO_IDX(i, svc->temporal_layer_id,
  404. svc->number_temporal_layers);
  405. LAYER_CONTEXT *lc = &svc->layer_context[layer];
  406. RATE_CONTROL *lrc = &lc->rc;
  407. // Exclude check for layer whose bitrate is 0.
  408. if (lc->target_bandwidth > 0) {
  409. const int drop_mark_layer = (int)(cpi->svc.framedrop_thresh[i] *
  410. lrc->optimal_buffer_level / 100);
  411. if (!(lrc->buffer_level > drop_mark_layer)) return 0;
  412. }
  413. }
  414. return 1;
  415. }
  416. }
  417. static int check_buffer_below_thresh(VP9_COMP *cpi, int drop_mark) {
  418. SVC *svc = &cpi->svc;
  419. if (!cpi->use_svc || cpi->svc.framedrop_mode == LAYER_DROP) {
  420. RATE_CONTROL *const rc = &cpi->rc;
  421. return (rc->buffer_level <= drop_mark);
  422. } else {
  423. int i;
  424. // For SVC in the constrained framedrop mode (svc->framedrop_mode =
  425. // CONSTRAINED_LAYER_DROP or FULL_SUPERFRAME_DROP): the condition on
  426. // buffer (if its below threshold, so drop frame) is checked on current
  427. // and upper spatial layers. For FULL_SUPERFRAME_DROP mode if any
  428. // spatial layer is <= threshold, then we return 1 (drop).
  429. for (i = svc->spatial_layer_id; i < svc->number_spatial_layers; ++i) {
  430. const int layer = LAYER_IDS_TO_IDX(i, svc->temporal_layer_id,
  431. svc->number_temporal_layers);
  432. LAYER_CONTEXT *lc = &svc->layer_context[layer];
  433. RATE_CONTROL *lrc = &lc->rc;
  434. // Exclude check for layer whose bitrate is 0.
  435. if (lc->target_bandwidth > 0) {
  436. const int drop_mark_layer = (int)(cpi->svc.framedrop_thresh[i] *
  437. lrc->optimal_buffer_level / 100);
  438. if (cpi->svc.framedrop_mode == FULL_SUPERFRAME_DROP) {
  439. if (lrc->buffer_level <= drop_mark_layer) return 1;
  440. } else {
  441. if (!(lrc->buffer_level <= drop_mark_layer)) return 0;
  442. }
  443. }
  444. }
  445. if (cpi->svc.framedrop_mode == FULL_SUPERFRAME_DROP)
  446. return 0;
  447. else
  448. return 1;
  449. }
  450. }
  451. static int drop_frame(VP9_COMP *cpi) {
  452. const VP9EncoderConfig *oxcf = &cpi->oxcf;
  453. RATE_CONTROL *const rc = &cpi->rc;
  454. SVC *svc = &cpi->svc;
  455. int drop_frames_water_mark = oxcf->drop_frames_water_mark;
  456. if (cpi->use_svc) {
  457. // If we have dropped max_consec_drop frames, then we don't
  458. // drop this spatial layer, and reset counter to 0.
  459. if (svc->drop_count[svc->spatial_layer_id] == svc->max_consec_drop) {
  460. svc->drop_count[svc->spatial_layer_id] = 0;
  461. return 0;
  462. } else {
  463. drop_frames_water_mark = svc->framedrop_thresh[svc->spatial_layer_id];
  464. }
  465. }
  466. if (!drop_frames_water_mark ||
  467. (svc->spatial_layer_id > 0 &&
  468. svc->framedrop_mode == FULL_SUPERFRAME_DROP)) {
  469. return 0;
  470. } else {
  471. if ((rc->buffer_level < 0 && svc->framedrop_mode != FULL_SUPERFRAME_DROP) ||
  472. (check_buffer_below_thresh(cpi, -1) &&
  473. svc->framedrop_mode == FULL_SUPERFRAME_DROP)) {
  474. // Always drop if buffer is below 0.
  475. return 1;
  476. } else {
  477. // If buffer is below drop_mark, for now just drop every other frame
  478. // (starting with the next frame) until it increases back over drop_mark.
  479. int drop_mark =
  480. (int)(drop_frames_water_mark * rc->optimal_buffer_level / 100);
  481. if (check_buffer_above_thresh(cpi, drop_mark) &&
  482. (rc->decimation_factor > 0)) {
  483. --rc->decimation_factor;
  484. } else if (check_buffer_below_thresh(cpi, drop_mark) &&
  485. rc->decimation_factor == 0) {
  486. rc->decimation_factor = 1;
  487. }
  488. if (rc->decimation_factor > 0) {
  489. if (rc->decimation_count > 0) {
  490. --rc->decimation_count;
  491. return 1;
  492. } else {
  493. rc->decimation_count = rc->decimation_factor;
  494. return 0;
  495. }
  496. } else {
  497. rc->decimation_count = 0;
  498. return 0;
  499. }
  500. }
  501. }
  502. }
  503. int post_encode_drop_cbr(VP9_COMP *cpi, size_t *size) {
  504. size_t frame_size = *size << 3;
  505. int64_t new_buffer_level =
  506. cpi->rc.buffer_level + cpi->rc.avg_frame_bandwidth - (int64_t)frame_size;
  507. // For now we drop if new buffer level (given the encoded frame size) goes
  508. // below 0.
  509. if (new_buffer_level < 0) {
  510. *size = 0;
  511. vp9_rc_postencode_update_drop_frame(cpi);
  512. // Update flag to use for next frame.
  513. if (cpi->rc.high_source_sad ||
  514. (cpi->use_svc && cpi->svc.high_source_sad_superframe))
  515. cpi->rc.last_post_encode_dropped_scene_change = 1;
  516. // Force max_q on next fame.
  517. cpi->rc.force_max_q = 1;
  518. cpi->rc.avg_frame_qindex[INTER_FRAME] = cpi->rc.worst_quality;
  519. cpi->last_frame_dropped = 1;
  520. cpi->ext_refresh_frame_flags_pending = 0;
  521. if (cpi->use_svc) {
  522. SVC *svc = &cpi->svc;
  523. int sl = 0;
  524. int tl = 0;
  525. svc->last_layer_dropped[svc->spatial_layer_id] = 1;
  526. svc->drop_spatial_layer[svc->spatial_layer_id] = 1;
  527. svc->drop_count[svc->spatial_layer_id]++;
  528. svc->skip_enhancement_layer = 1;
  529. // Postencode drop is only checked on base spatial layer,
  530. // for now if max-q is set on base we force it on all layers.
  531. for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
  532. for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
  533. const int layer =
  534. LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
  535. LAYER_CONTEXT *lc = &svc->layer_context[layer];
  536. RATE_CONTROL *lrc = &lc->rc;
  537. lrc->force_max_q = 1;
  538. lrc->avg_frame_qindex[INTER_FRAME] = cpi->rc.worst_quality;
  539. }
  540. }
  541. }
  542. return 1;
  543. }
  544. cpi->rc.force_max_q = 0;
  545. cpi->rc.last_post_encode_dropped_scene_change = 0;
  546. return 0;
  547. }
  548. int vp9_rc_drop_frame(VP9_COMP *cpi) {
  549. SVC *svc = &cpi->svc;
  550. int svc_prev_layer_dropped = 0;
  551. // In the constrained or full_superframe framedrop mode for svc
  552. // (framedrop_mode != LAYER_DROP), if the previous spatial layer was
  553. // dropped, drop the current spatial layer.
  554. if (cpi->use_svc && svc->spatial_layer_id > 0 &&
  555. svc->drop_spatial_layer[svc->spatial_layer_id - 1])
  556. svc_prev_layer_dropped = 1;
  557. if ((svc_prev_layer_dropped && svc->framedrop_mode != LAYER_DROP) ||
  558. drop_frame(cpi)) {
  559. vp9_rc_postencode_update_drop_frame(cpi);
  560. cpi->ext_refresh_frame_flags_pending = 0;
  561. cpi->last_frame_dropped = 1;
  562. if (cpi->use_svc) {
  563. svc->last_layer_dropped[svc->spatial_layer_id] = 1;
  564. svc->drop_spatial_layer[svc->spatial_layer_id] = 1;
  565. svc->drop_count[svc->spatial_layer_id]++;
  566. svc->skip_enhancement_layer = 1;
  567. if (svc->framedrop_mode == LAYER_DROP ||
  568. svc->drop_spatial_layer[0] == 0) {
  569. // For the case of constrained drop mode where the base is dropped
  570. // (drop_spatial_layer[0] == 1), which means full superframe dropped,
  571. // we don't increment the svc frame counters. In particular temporal
  572. // layer counter (which is incremented in vp9_inc_frame_in_layer())
  573. // won't be incremented, so on a dropped frame we try the same
  574. // temporal_layer_id on next incoming frame. This is to avoid an
  575. // issue with temporal alignement with full superframe dropping.
  576. vp9_inc_frame_in_layer(cpi);
  577. }
  578. if (svc->spatial_layer_id == svc->number_spatial_layers - 1) {
  579. int i;
  580. int all_layers_drop = 1;
  581. for (i = 0; i < svc->spatial_layer_id; i++) {
  582. if (svc->drop_spatial_layer[i] == 0) {
  583. all_layers_drop = 0;
  584. break;
  585. }
  586. }
  587. if (all_layers_drop == 1) svc->skip_enhancement_layer = 0;
  588. }
  589. }
  590. return 1;
  591. }
  592. return 0;
  593. }
  594. static int adjust_q_cbr(const VP9_COMP *cpi, int q) {
  595. // This makes sure q is between oscillating Qs to prevent resonance.
  596. if (!cpi->rc.reset_high_source_sad &&
  597. (!cpi->oxcf.gf_cbr_boost_pct ||
  598. !(cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame)) &&
  599. (cpi->rc.rc_1_frame * cpi->rc.rc_2_frame == -1) &&
  600. cpi->rc.q_1_frame != cpi->rc.q_2_frame) {
  601. int qclamp = clamp(q, VPXMIN(cpi->rc.q_1_frame, cpi->rc.q_2_frame),
  602. VPXMAX(cpi->rc.q_1_frame, cpi->rc.q_2_frame));
  603. // If the previous frame had overshoot and the current q needs to increase
  604. // above the clamped value, reduce the clamp for faster reaction to
  605. // overshoot.
  606. if (cpi->rc.rc_1_frame == -1 && q > qclamp)
  607. q = (q + qclamp) >> 1;
  608. else
  609. q = qclamp;
  610. }
  611. if (cpi->oxcf.content == VP9E_CONTENT_SCREEN)
  612. vp9_cyclic_refresh_limit_q(cpi, &q);
  613. return VPXMAX(VPXMIN(q, cpi->rc.worst_quality), cpi->rc.best_quality);
  614. }
  615. static double get_rate_correction_factor(const VP9_COMP *cpi) {
  616. const RATE_CONTROL *const rc = &cpi->rc;
  617. const VP9_COMMON *const cm = &cpi->common;
  618. double rcf;
  619. if (frame_is_intra_only(cm)) {
  620. rcf = rc->rate_correction_factors[KF_STD];
  621. } else if (cpi->oxcf.pass == 2) {
  622. RATE_FACTOR_LEVEL rf_lvl =
  623. cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
  624. rcf = rc->rate_correction_factors[rf_lvl];
  625. } else {
  626. if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
  627. !rc->is_src_frame_alt_ref && !cpi->use_svc &&
  628. (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 100))
  629. rcf = rc->rate_correction_factors[GF_ARF_STD];
  630. else
  631. rcf = rc->rate_correction_factors[INTER_NORMAL];
  632. }
  633. rcf *= rcf_mult[rc->frame_size_selector];
  634. return fclamp(rcf, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
  635. }
  636. static void set_rate_correction_factor(VP9_COMP *cpi, double factor) {
  637. RATE_CONTROL *const rc = &cpi->rc;
  638. const VP9_COMMON *const cm = &cpi->common;
  639. // Normalize RCF to account for the size-dependent scaling factor.
  640. factor /= rcf_mult[cpi->rc.frame_size_selector];
  641. factor = fclamp(factor, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
  642. if (frame_is_intra_only(cm)) {
  643. rc->rate_correction_factors[KF_STD] = factor;
  644. } else if (cpi->oxcf.pass == 2) {
  645. RATE_FACTOR_LEVEL rf_lvl =
  646. cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
  647. rc->rate_correction_factors[rf_lvl] = factor;
  648. } else {
  649. if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
  650. !rc->is_src_frame_alt_ref && !cpi->use_svc &&
  651. (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 100))
  652. rc->rate_correction_factors[GF_ARF_STD] = factor;
  653. else
  654. rc->rate_correction_factors[INTER_NORMAL] = factor;
  655. }
  656. }
  657. void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi) {
  658. const VP9_COMMON *const cm = &cpi->common;
  659. int correction_factor = 100;
  660. double rate_correction_factor = get_rate_correction_factor(cpi);
  661. double adjustment_limit;
  662. RATE_FACTOR_LEVEL rf_lvl =
  663. cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
  664. int projected_size_based_on_q = 0;
  665. // Do not update the rate factors for arf overlay frames.
  666. if (cpi->rc.is_src_frame_alt_ref) return;
  667. // Clear down mmx registers to allow floating point in what follows
  668. vpx_clear_system_state();
  669. // Work out how big we would have expected the frame to be at this Q given
  670. // the current correction factor.
  671. // Stay in double to avoid int overflow when values are large
  672. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->common.seg.enabled) {
  673. projected_size_based_on_q =
  674. vp9_cyclic_refresh_estimate_bits_at_q(cpi, rate_correction_factor);
  675. } else {
  676. FRAME_TYPE frame_type = cm->intra_only ? KEY_FRAME : cm->frame_type;
  677. projected_size_based_on_q =
  678. vp9_estimate_bits_at_q(frame_type, cm->base_qindex, cm->MBs,
  679. rate_correction_factor, cm->bit_depth);
  680. }
  681. // Work out a size correction factor.
  682. if (projected_size_based_on_q > FRAME_OVERHEAD_BITS)
  683. correction_factor = (int)((100 * (int64_t)cpi->rc.projected_frame_size) /
  684. projected_size_based_on_q);
  685. // Do not use damped adjustment for the first frame of each frame type
  686. if (!cpi->rc.damped_adjustment[rf_lvl]) {
  687. adjustment_limit = 1.0;
  688. cpi->rc.damped_adjustment[rf_lvl] = 1;
  689. } else {
  690. // More heavily damped adjustment used if we have been oscillating either
  691. // side of target.
  692. adjustment_limit =
  693. 0.25 + 0.5 * VPXMIN(1, fabs(log10(0.01 * correction_factor)));
  694. }
  695. cpi->rc.q_2_frame = cpi->rc.q_1_frame;
  696. cpi->rc.q_1_frame = cm->base_qindex;
  697. cpi->rc.rc_2_frame = cpi->rc.rc_1_frame;
  698. if (correction_factor > 110)
  699. cpi->rc.rc_1_frame = -1;
  700. else if (correction_factor < 90)
  701. cpi->rc.rc_1_frame = 1;
  702. else
  703. cpi->rc.rc_1_frame = 0;
  704. // Turn off oscilation detection in the case of massive overshoot.
  705. if (cpi->rc.rc_1_frame == -1 && cpi->rc.rc_2_frame == 1 &&
  706. correction_factor > 1000) {
  707. cpi->rc.rc_2_frame = 0;
  708. }
  709. if (correction_factor > 102) {
  710. // We are not already at the worst allowable quality
  711. correction_factor =
  712. (int)(100 + ((correction_factor - 100) * adjustment_limit));
  713. rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
  714. // Keep rate_correction_factor within limits
  715. if (rate_correction_factor > MAX_BPB_FACTOR)
  716. rate_correction_factor = MAX_BPB_FACTOR;
  717. } else if (correction_factor < 99) {
  718. // We are not already at the best allowable quality
  719. correction_factor =
  720. (int)(100 - ((100 - correction_factor) * adjustment_limit));
  721. rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
  722. // Keep rate_correction_factor within limits
  723. if (rate_correction_factor < MIN_BPB_FACTOR)
  724. rate_correction_factor = MIN_BPB_FACTOR;
  725. }
  726. set_rate_correction_factor(cpi, rate_correction_factor);
  727. }
  728. int vp9_rc_regulate_q(const VP9_COMP *cpi, int target_bits_per_frame,
  729. int active_best_quality, int active_worst_quality) {
  730. const VP9_COMMON *const cm = &cpi->common;
  731. CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
  732. int q = active_worst_quality;
  733. int last_error = INT_MAX;
  734. int i, target_bits_per_mb, bits_per_mb_at_this_q;
  735. const double correction_factor = get_rate_correction_factor(cpi);
  736. // Calculate required scaling factor based on target frame size and size of
  737. // frame produced using previous Q.
  738. target_bits_per_mb =
  739. (int)(((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / cm->MBs);
  740. i = active_best_quality;
  741. do {
  742. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
  743. cr->apply_cyclic_refresh &&
  744. (!cpi->oxcf.gf_cbr_boost_pct || !cpi->refresh_golden_frame)) {
  745. bits_per_mb_at_this_q =
  746. (int)vp9_cyclic_refresh_rc_bits_per_mb(cpi, i, correction_factor);
  747. } else {
  748. FRAME_TYPE frame_type = cm->intra_only ? KEY_FRAME : cm->frame_type;
  749. bits_per_mb_at_this_q = (int)vp9_rc_bits_per_mb(
  750. frame_type, i, correction_factor, cm->bit_depth);
  751. }
  752. if (bits_per_mb_at_this_q <= target_bits_per_mb) {
  753. if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error)
  754. q = i;
  755. else
  756. q = i - 1;
  757. break;
  758. } else {
  759. last_error = bits_per_mb_at_this_q - target_bits_per_mb;
  760. }
  761. } while (++i <= active_worst_quality);
  762. // Adjustment to q for CBR mode.
  763. if (cpi->oxcf.rc_mode == VPX_CBR) return adjust_q_cbr(cpi, q);
  764. return q;
  765. }
  766. static int get_active_quality(int q, int gfu_boost, int low, int high,
  767. int *low_motion_minq, int *high_motion_minq) {
  768. if (gfu_boost > high) {
  769. return low_motion_minq[q];
  770. } else if (gfu_boost < low) {
  771. return high_motion_minq[q];
  772. } else {
  773. const int gap = high - low;
  774. const int offset = high - gfu_boost;
  775. const int qdiff = high_motion_minq[q] - low_motion_minq[q];
  776. const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
  777. return low_motion_minq[q] + adjustment;
  778. }
  779. }
  780. static int get_kf_active_quality(const RATE_CONTROL *const rc, int q,
  781. vpx_bit_depth_t bit_depth) {
  782. int *kf_low_motion_minq;
  783. int *kf_high_motion_minq;
  784. ASSIGN_MINQ_TABLE(bit_depth, kf_low_motion_minq);
  785. ASSIGN_MINQ_TABLE(bit_depth, kf_high_motion_minq);
  786. return get_active_quality(q, rc->kf_boost, kf_low, kf_high,
  787. kf_low_motion_minq, kf_high_motion_minq);
  788. }
  789. static int get_gf_active_quality(const VP9_COMP *const cpi, int q,
  790. vpx_bit_depth_t bit_depth) {
  791. const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
  792. const RATE_CONTROL *const rc = &cpi->rc;
  793. int *arfgf_low_motion_minq;
  794. int *arfgf_high_motion_minq;
  795. const int gfu_boost = cpi->multi_layer_arf
  796. ? gf_group->gfu_boost[gf_group->index]
  797. : rc->gfu_boost;
  798. ASSIGN_MINQ_TABLE(bit_depth, arfgf_low_motion_minq);
  799. ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
  800. return get_active_quality(q, gfu_boost, gf_low, gf_high,
  801. arfgf_low_motion_minq, arfgf_high_motion_minq);
  802. }
  803. static int calc_active_worst_quality_one_pass_vbr(const VP9_COMP *cpi) {
  804. const RATE_CONTROL *const rc = &cpi->rc;
  805. const unsigned int curr_frame = cpi->common.current_video_frame;
  806. int active_worst_quality;
  807. if (cpi->common.frame_type == KEY_FRAME) {
  808. active_worst_quality =
  809. curr_frame == 0 ? rc->worst_quality : rc->last_q[KEY_FRAME] << 1;
  810. } else {
  811. if (!rc->is_src_frame_alt_ref &&
  812. (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
  813. active_worst_quality =
  814. curr_frame == 1
  815. ? rc->last_q[KEY_FRAME] * 5 >> 2
  816. : rc->last_q[INTER_FRAME] * rc->fac_active_worst_gf / 100;
  817. } else {
  818. active_worst_quality = curr_frame == 1
  819. ? rc->last_q[KEY_FRAME] << 1
  820. : rc->avg_frame_qindex[INTER_FRAME] *
  821. rc->fac_active_worst_inter / 100;
  822. }
  823. }
  824. return VPXMIN(active_worst_quality, rc->worst_quality);
  825. }
  826. // Adjust active_worst_quality level based on buffer level.
  827. static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) {
  828. // Adjust active_worst_quality: If buffer is above the optimal/target level,
  829. // bring active_worst_quality down depending on fullness of buffer.
  830. // If buffer is below the optimal level, let the active_worst_quality go from
  831. // ambient Q (at buffer = optimal level) to worst_quality level
  832. // (at buffer = critical level).
  833. const VP9_COMMON *const cm = &cpi->common;
  834. const RATE_CONTROL *rc = &cpi->rc;
  835. // Buffer level below which we push active_worst to worst_quality.
  836. int64_t critical_level = rc->optimal_buffer_level >> 3;
  837. int64_t buff_lvl_step = 0;
  838. int adjustment = 0;
  839. int active_worst_quality;
  840. int ambient_qp;
  841. unsigned int num_frames_weight_key = 5 * cpi->svc.number_temporal_layers;
  842. if (frame_is_intra_only(cm) || rc->reset_high_source_sad || rc->force_max_q)
  843. return rc->worst_quality;
  844. // For ambient_qp we use minimum of avg_frame_qindex[KEY_FRAME/INTER_FRAME]
  845. // for the first few frames following key frame. These are both initialized
  846. // to worst_quality and updated with (3/4, 1/4) average in postencode_update.
  847. // So for first few frames following key, the qp of that key frame is weighted
  848. // into the active_worst_quality setting.
  849. ambient_qp = (cm->current_video_frame < num_frames_weight_key)
  850. ? VPXMIN(rc->avg_frame_qindex[INTER_FRAME],
  851. rc->avg_frame_qindex[KEY_FRAME])
  852. : rc->avg_frame_qindex[INTER_FRAME];
  853. active_worst_quality = VPXMIN(rc->worst_quality, (ambient_qp * 5) >> 2);
  854. // For SVC if the current base spatial layer was key frame, use the QP from
  855. // that base layer for ambient_qp.
  856. if (cpi->use_svc && cpi->svc.spatial_layer_id > 0) {
  857. int layer = LAYER_IDS_TO_IDX(0, cpi->svc.temporal_layer_id,
  858. cpi->svc.number_temporal_layers);
  859. const LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
  860. if (lc->is_key_frame) {
  861. const RATE_CONTROL *lrc = &lc->rc;
  862. ambient_qp = VPXMIN(ambient_qp, lrc->last_q[KEY_FRAME]);
  863. active_worst_quality = VPXMIN(rc->worst_quality, (ambient_qp * 9) >> 3);
  864. }
  865. }
  866. if (rc->buffer_level > rc->optimal_buffer_level) {
  867. // Adjust down.
  868. // Maximum limit for down adjustment ~30%; make it lower for screen content.
  869. int max_adjustment_down = active_worst_quality / 3;
  870. if (cpi->oxcf.content == VP9E_CONTENT_SCREEN)
  871. max_adjustment_down = active_worst_quality >> 3;
  872. if (max_adjustment_down) {
  873. buff_lvl_step = ((rc->maximum_buffer_size - rc->optimal_buffer_level) /
  874. max_adjustment_down);
  875. if (buff_lvl_step)
  876. adjustment = (int)((rc->buffer_level - rc->optimal_buffer_level) /
  877. buff_lvl_step);
  878. active_worst_quality -= adjustment;
  879. }
  880. } else if (rc->buffer_level > critical_level) {
  881. // Adjust up from ambient Q.
  882. if (critical_level) {
  883. buff_lvl_step = (rc->optimal_buffer_level - critical_level);
  884. if (buff_lvl_step) {
  885. adjustment = (int)((rc->worst_quality - ambient_qp) *
  886. (rc->optimal_buffer_level - rc->buffer_level) /
  887. buff_lvl_step);
  888. }
  889. active_worst_quality = ambient_qp + adjustment;
  890. }
  891. } else {
  892. // Set to worst_quality if buffer is below critical level.
  893. active_worst_quality = rc->worst_quality;
  894. }
  895. return active_worst_quality;
  896. }
  897. static int rc_pick_q_and_bounds_one_pass_cbr(const VP9_COMP *cpi,
  898. int *bottom_index,
  899. int *top_index) {
  900. const VP9_COMMON *const cm = &cpi->common;
  901. const RATE_CONTROL *const rc = &cpi->rc;
  902. int active_best_quality;
  903. int active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
  904. int q;
  905. int *rtc_minq;
  906. ASSIGN_MINQ_TABLE(cm->bit_depth, rtc_minq);
  907. if (frame_is_intra_only(cm)) {
  908. active_best_quality = rc->best_quality;
  909. // Handle the special case for key frames forced when we have reached
  910. // the maximum key frame interval. Here force the Q to a range
  911. // based on the ambient Q to reduce the risk of popping.
  912. if (rc->this_key_frame_forced) {
  913. int qindex = rc->last_boosted_qindex;
  914. double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
  915. int delta_qindex = vp9_compute_qdelta(
  916. rc, last_boosted_q, (last_boosted_q * 0.75), cm->bit_depth);
  917. active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
  918. } else if (cm->current_video_frame > 0) {
  919. // not first frame of one pass and kf_boost is set
  920. double q_adj_factor = 1.0;
  921. double q_val;
  922. active_best_quality = get_kf_active_quality(
  923. rc, rc->avg_frame_qindex[KEY_FRAME], cm->bit_depth);
  924. // Allow somewhat lower kf minq with small image formats.
  925. if ((cm->width * cm->height) <= (352 * 288)) {
  926. q_adj_factor -= 0.25;
  927. }
  928. // Convert the adjustment factor to a qindex delta
  929. // on active_best_quality.
  930. q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
  931. active_best_quality +=
  932. vp9_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
  933. }
  934. } else if (!rc->is_src_frame_alt_ref && !cpi->use_svc &&
  935. cpi->oxcf.gf_cbr_boost_pct &&
  936. (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
  937. // Use the lower of active_worst_quality and recent
  938. // average Q as basis for GF/ARF best Q limit unless last frame was
  939. // a key frame.
  940. if (rc->frames_since_key > 1 &&
  941. rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
  942. q = rc->avg_frame_qindex[INTER_FRAME];
  943. } else {
  944. q = active_worst_quality;
  945. }
  946. active_best_quality = get_gf_active_quality(cpi, q, cm->bit_depth);
  947. } else {
  948. // Use the lower of active_worst_quality and recent/average Q.
  949. if (cm->current_video_frame > 1) {
  950. if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality)
  951. active_best_quality = rtc_minq[rc->avg_frame_qindex[INTER_FRAME]];
  952. else
  953. active_best_quality = rtc_minq[active_worst_quality];
  954. } else {
  955. if (rc->avg_frame_qindex[KEY_FRAME] < active_worst_quality)
  956. active_best_quality = rtc_minq[rc->avg_frame_qindex[KEY_FRAME]];
  957. else
  958. active_best_quality = rtc_minq[active_worst_quality];
  959. }
  960. }
  961. // Clip the active best and worst quality values to limits
  962. active_best_quality =
  963. clamp(active_best_quality, rc->best_quality, rc->worst_quality);
  964. active_worst_quality =
  965. clamp(active_worst_quality, active_best_quality, rc->worst_quality);
  966. *top_index = active_worst_quality;
  967. *bottom_index = active_best_quality;
  968. // Special case code to try and match quality with forced key frames
  969. if (frame_is_intra_only(cm) && rc->this_key_frame_forced) {
  970. q = rc->last_boosted_qindex;
  971. } else {
  972. q = vp9_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
  973. active_worst_quality);
  974. if (q > *top_index) {
  975. // Special case when we are targeting the max allowed rate
  976. if (rc->this_frame_target >= rc->max_frame_bandwidth)
  977. *top_index = q;
  978. else
  979. q = *top_index;
  980. }
  981. }
  982. assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
  983. assert(*bottom_index <= rc->worst_quality &&
  984. *bottom_index >= rc->best_quality);
  985. assert(q <= rc->worst_quality && q >= rc->best_quality);
  986. return q;
  987. }
  988. static int get_active_cq_level_one_pass(const RATE_CONTROL *rc,
  989. const VP9EncoderConfig *const oxcf) {
  990. static const double cq_adjust_threshold = 0.1;
  991. int active_cq_level = oxcf->cq_level;
  992. if (oxcf->rc_mode == VPX_CQ && rc->total_target_bits > 0) {
  993. const double x = (double)rc->total_actual_bits / rc->total_target_bits;
  994. if (x < cq_adjust_threshold) {
  995. active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold);
  996. }
  997. }
  998. return active_cq_level;
  999. }
  1000. #define SMOOTH_PCT_MIN 0.1
  1001. #define SMOOTH_PCT_DIV 0.05
  1002. static int get_active_cq_level_two_pass(const TWO_PASS *twopass,
  1003. const RATE_CONTROL *rc,
  1004. const VP9EncoderConfig *const oxcf) {
  1005. static const double cq_adjust_threshold = 0.1;
  1006. int active_cq_level = oxcf->cq_level;
  1007. if (oxcf->rc_mode == VPX_CQ) {
  1008. if (twopass->mb_smooth_pct > SMOOTH_PCT_MIN) {
  1009. active_cq_level -=
  1010. (int)((twopass->mb_smooth_pct - SMOOTH_PCT_MIN) / SMOOTH_PCT_DIV);
  1011. active_cq_level = VPXMAX(active_cq_level, 0);
  1012. }
  1013. if (rc->total_target_bits > 0) {
  1014. const double x = (double)rc->total_actual_bits / rc->total_target_bits;
  1015. if (x < cq_adjust_threshold) {
  1016. active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold);
  1017. }
  1018. }
  1019. }
  1020. return active_cq_level;
  1021. }
  1022. static int rc_pick_q_and_bounds_one_pass_vbr(const VP9_COMP *cpi,
  1023. int *bottom_index,
  1024. int *top_index) {
  1025. const VP9_COMMON *const cm = &cpi->common;
  1026. const RATE_CONTROL *const rc = &cpi->rc;
  1027. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  1028. const int cq_level = get_active_cq_level_one_pass(rc, oxcf);
  1029. int active_best_quality;
  1030. int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi);
  1031. int q;
  1032. int *inter_minq;
  1033. ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
  1034. if (frame_is_intra_only(cm)) {
  1035. if (oxcf->rc_mode == VPX_Q) {
  1036. int qindex = cq_level;
  1037. double q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
  1038. int delta_qindex = vp9_compute_qdelta(rc, q, q * 0.25, cm->bit_depth);
  1039. active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
  1040. } else if (rc->this_key_frame_forced) {
  1041. // Handle the special case for key frames forced when we have reached
  1042. // the maximum key frame interval. Here force the Q to a range
  1043. // based on the ambient Q to reduce the risk of popping.
  1044. int qindex = rc->last_boosted_qindex;
  1045. double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
  1046. int delta_qindex = vp9_compute_qdelta(
  1047. rc, last_boosted_q, last_boosted_q * 0.75, cm->bit_depth);
  1048. active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
  1049. } else {
  1050. // not first frame of one pass and kf_boost is set
  1051. double q_adj_factor = 1.0;
  1052. double q_val;
  1053. active_best_quality = get_kf_active_quality(
  1054. rc, rc->avg_frame_qindex[KEY_FRAME], cm->bit_depth);
  1055. // Allow somewhat lower kf minq with small image formats.
  1056. if ((cm->width * cm->height) <= (352 * 288)) {
  1057. q_adj_factor -= 0.25;
  1058. }
  1059. // Convert the adjustment factor to a qindex delta
  1060. // on active_best_quality.
  1061. q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
  1062. active_best_quality +=
  1063. vp9_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
  1064. }
  1065. } else if (!rc->is_src_frame_alt_ref &&
  1066. (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
  1067. // Use the lower of active_worst_quality and recent
  1068. // average Q as basis for GF/ARF best Q limit unless last frame was
  1069. // a key frame.
  1070. if (rc->frames_since_key > 1) {
  1071. if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
  1072. q = rc->avg_frame_qindex[INTER_FRAME];
  1073. } else {
  1074. q = active_worst_quality;
  1075. }
  1076. } else {
  1077. q = rc->avg_frame_qindex[KEY_FRAME];
  1078. }
  1079. // For constrained quality dont allow Q less than the cq level
  1080. if (oxcf->rc_mode == VPX_CQ) {
  1081. if (q < cq_level) q = cq_level;
  1082. active_best_quality = get_gf_active_quality(cpi, q, cm->bit_depth);
  1083. // Constrained quality use slightly lower active best.
  1084. active_best_quality = active_best_quality * 15 / 16;
  1085. } else if (oxcf->rc_mode == VPX_Q) {
  1086. int qindex = cq_level;
  1087. double q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
  1088. int delta_qindex;
  1089. if (cpi->refresh_alt_ref_frame)
  1090. delta_qindex = vp9_compute_qdelta(rc, q, q * 0.40, cm->bit_depth);
  1091. else
  1092. delta_qindex = vp9_compute_qdelta(rc, q, q * 0.50, cm->bit_depth);
  1093. active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
  1094. } else {
  1095. active_best_quality = get_gf_active_quality(cpi, q, cm->bit_depth);
  1096. }
  1097. } else {
  1098. if (oxcf->rc_mode == VPX_Q) {
  1099. int qindex = cq_level;
  1100. double q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
  1101. double delta_rate[FIXED_GF_INTERVAL] = { 0.50, 1.0, 0.85, 1.0,
  1102. 0.70, 1.0, 0.85, 1.0 };
  1103. int delta_qindex = vp9_compute_qdelta(
  1104. rc, q, q * delta_rate[cm->current_video_frame % FIXED_GF_INTERVAL],
  1105. cm->bit_depth);
  1106. active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
  1107. } else {
  1108. // Use the min of the average Q and active_worst_quality as basis for
  1109. // active_best.
  1110. if (cm->current_video_frame > 1) {
  1111. q = VPXMIN(rc->avg_frame_qindex[INTER_FRAME], active_worst_quality);
  1112. active_best_quality = inter_minq[q];
  1113. } else {
  1114. active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]];
  1115. }
  1116. // For the constrained quality mode we don't want
  1117. // q to fall below the cq level.
  1118. if ((oxcf->rc_mode == VPX_CQ) && (active_best_quality < cq_level)) {
  1119. active_best_quality = cq_level;
  1120. }
  1121. }
  1122. }
  1123. // Clip the active best and worst quality values to limits
  1124. active_best_quality =
  1125. clamp(active_best_quality, rc->best_quality, rc->worst_quality);
  1126. active_worst_quality =
  1127. clamp(active_worst_quality, active_best_quality, rc->worst_quality);
  1128. *top_index = active_worst_quality;
  1129. *bottom_index = active_best_quality;
  1130. #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
  1131. {
  1132. int qdelta = 0;
  1133. vpx_clear_system_state();
  1134. // Limit Q range for the adaptive loop.
  1135. if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced &&
  1136. !(cm->current_video_frame == 0)) {
  1137. qdelta = vp9_compute_qdelta_by_rate(
  1138. &cpi->rc, cm->frame_type, active_worst_quality, 2.0, cm->bit_depth);
  1139. } else if (!rc->is_src_frame_alt_ref &&
  1140. (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
  1141. qdelta = vp9_compute_qdelta_by_rate(
  1142. &cpi->rc, cm->frame_type, active_worst_quality, 1.75, cm->bit_depth);
  1143. }
  1144. if (rc->high_source_sad && cpi->sf.use_altref_onepass) qdelta = 0;
  1145. *top_index = active_worst_quality + qdelta;
  1146. *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
  1147. }
  1148. #endif
  1149. if (oxcf->rc_mode == VPX_Q) {
  1150. q = active_best_quality;
  1151. // Special case code to try and match quality with forced key frames
  1152. } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) {
  1153. q = rc->last_boosted_qindex;
  1154. } else {
  1155. q = vp9_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
  1156. active_worst_quality);
  1157. if (q > *top_index) {
  1158. // Special case when we are targeting the max allowed rate
  1159. if (rc->this_frame_target >= rc->max_frame_bandwidth)
  1160. *top_index = q;
  1161. else
  1162. q = *top_index;
  1163. }
  1164. }
  1165. assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
  1166. assert(*bottom_index <= rc->worst_quality &&
  1167. *bottom_index >= rc->best_quality);
  1168. assert(q <= rc->worst_quality && q >= rc->best_quality);
  1169. return q;
  1170. }
  1171. int vp9_frame_type_qdelta(const VP9_COMP *cpi, int rf_level, int q) {
  1172. static const double rate_factor_deltas[RATE_FACTOR_LEVELS] = {
  1173. 1.00, // INTER_NORMAL
  1174. 1.00, // INTER_HIGH
  1175. 1.50, // GF_ARF_LOW
  1176. 1.75, // GF_ARF_STD
  1177. 2.00, // KF_STD
  1178. };
  1179. const VP9_COMMON *const cm = &cpi->common;
  1180. int qdelta = vp9_compute_qdelta_by_rate(
  1181. &cpi->rc, cm->frame_type, q, rate_factor_deltas[rf_level], cm->bit_depth);
  1182. return qdelta;
  1183. }
  1184. #define STATIC_MOTION_THRESH 95
  1185. static void pick_kf_q_bound_two_pass(const VP9_COMP *cpi, int *bottom_index,
  1186. int *top_index) {
  1187. const VP9_COMMON *const cm = &cpi->common;
  1188. const RATE_CONTROL *const rc = &cpi->rc;
  1189. int active_best_quality;
  1190. int active_worst_quality = cpi->twopass.active_worst_quality;
  1191. if (rc->this_key_frame_forced) {
  1192. // Handle the special case for key frames forced when we have reached
  1193. // the maximum key frame interval. Here force the Q to a range
  1194. // based on the ambient Q to reduce the risk of popping.
  1195. double last_boosted_q;
  1196. int delta_qindex;
  1197. int qindex;
  1198. if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
  1199. qindex = VPXMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
  1200. active_best_quality = qindex;
  1201. last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
  1202. delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
  1203. last_boosted_q * 1.25, cm->bit_depth);
  1204. active_worst_quality =
  1205. VPXMIN(qindex + delta_qindex, active_worst_quality);
  1206. } else {
  1207. qindex = rc->last_boosted_qindex;
  1208. last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
  1209. delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
  1210. last_boosted_q * 0.75, cm->bit_depth);
  1211. active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
  1212. }
  1213. } else {
  1214. // Not forced keyframe.
  1215. double q_adj_factor = 1.0;
  1216. double q_val;
  1217. // Baseline value derived from cpi->active_worst_quality and kf boost.
  1218. active_best_quality =
  1219. get_kf_active_quality(rc, active_worst_quality, cm->bit_depth);
  1220. if (cpi->twopass.kf_zeromotion_pct >= STATIC_KF_GROUP_THRESH) {
  1221. active_best_quality /= 4;
  1222. }
  1223. // Dont allow the active min to be lossless (q0) unlesss the max q
  1224. // already indicates lossless.
  1225. active_best_quality =
  1226. VPXMIN(active_worst_quality, VPXMAX(1, active_best_quality));
  1227. // Allow somewhat lower kf minq with small image formats.
  1228. if ((cm->width * cm->height) <= (352 * 288)) {
  1229. q_adj_factor -= 0.25;
  1230. }
  1231. // Make a further adjustment based on the kf zero motion measure.
  1232. q_adj_factor += 0.05 - (0.001 * (double)cpi->twopass.kf_zeromotion_pct);
  1233. // Convert the adjustment factor to a qindex delta
  1234. // on active_best_quality.
  1235. q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
  1236. active_best_quality +=
  1237. vp9_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
  1238. }
  1239. *top_index = active_worst_quality;
  1240. *bottom_index = active_best_quality;
  1241. }
  1242. static int rc_constant_q(const VP9_COMP *cpi, int *bottom_index, int *top_index,
  1243. int gf_group_index) {
  1244. const VP9_COMMON *const cm = &cpi->common;
  1245. const RATE_CONTROL *const rc = &cpi->rc;
  1246. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  1247. const GF_GROUP *gf_group = &cpi->twopass.gf_group;
  1248. const int is_intra_frame = frame_is_intra_only(cm);
  1249. const int cq_level = get_active_cq_level_two_pass(&cpi->twopass, rc, oxcf);
  1250. int q = cq_level;
  1251. int active_best_quality = cq_level;
  1252. int active_worst_quality = cq_level;
  1253. // Key frame qp decision
  1254. if (is_intra_frame && rc->frames_to_key > 1)
  1255. pick_kf_q_bound_two_pass(cpi, &active_best_quality, &active_worst_quality);
  1256. // ARF / GF qp decision
  1257. if (!is_intra_frame && !rc->is_src_frame_alt_ref &&
  1258. cpi->refresh_alt_ref_frame) {
  1259. active_best_quality = get_gf_active_quality(cpi, q, cm->bit_depth);
  1260. // Modify best quality for second level arfs. For mode VPX_Q this
  1261. // becomes the baseline frame q.
  1262. if (gf_group->rf_level[gf_group_index] == GF_ARF_LOW) {
  1263. const int layer_depth = gf_group->layer_depth[gf_group_index];
  1264. // linearly fit the frame q depending on the layer depth index from
  1265. // the base layer ARF.
  1266. active_best_quality = ((layer_depth - 1) * cq_level +
  1267. active_best_quality + layer_depth / 2) /
  1268. layer_depth;
  1269. }
  1270. }
  1271. q = active_best_quality;
  1272. *top_index = active_worst_quality;
  1273. *bottom_index = active_best_quality;
  1274. return q;
  1275. }
  1276. static int rc_pick_q_and_bounds_two_pass(const VP9_COMP *cpi, int *bottom_index,
  1277. int *top_index, int gf_group_index) {
  1278. const VP9_COMMON *const cm = &cpi->common;
  1279. const RATE_CONTROL *const rc = &cpi->rc;
  1280. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  1281. const GF_GROUP *gf_group = &cpi->twopass.gf_group;
  1282. const int cq_level = get_active_cq_level_two_pass(&cpi->twopass, rc, oxcf);
  1283. int active_best_quality;
  1284. int active_worst_quality = cpi->twopass.active_worst_quality;
  1285. int q;
  1286. int *inter_minq;
  1287. int arf_active_best_quality_adjustment, arf_active_best_quality_max;
  1288. int *arfgf_high_motion_minq;
  1289. const int boost_frame =
  1290. !rc->is_src_frame_alt_ref &&
  1291. (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame);
  1292. ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
  1293. if (oxcf->rc_mode == VPX_Q)
  1294. return rc_constant_q(cpi, bottom_index, top_index, gf_group_index);
  1295. if (frame_is_intra_only(cm)) {
  1296. pick_kf_q_bound_two_pass(cpi, &active_best_quality, &active_worst_quality);
  1297. } else if (boost_frame) {
  1298. // Use the lower of active_worst_quality and recent
  1299. // average Q as basis for GF/ARF best Q limit unless last frame was
  1300. // a key frame.
  1301. if (rc->frames_since_key > 1 &&
  1302. rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
  1303. q = rc->avg_frame_qindex[INTER_FRAME];
  1304. } else {
  1305. q = active_worst_quality;
  1306. }
  1307. // For constrained quality dont allow Q less than the cq level
  1308. if (oxcf->rc_mode == VPX_CQ) {
  1309. if (q < cq_level) q = cq_level;
  1310. }
  1311. active_best_quality = get_gf_active_quality(cpi, q, cm->bit_depth);
  1312. ASSIGN_MINQ_TABLE(cm->bit_depth, arfgf_high_motion_minq);
  1313. arf_active_best_quality_max = arfgf_high_motion_minq[q];
  1314. arf_active_best_quality_adjustment =
  1315. arf_active_best_quality_max - active_best_quality;
  1316. active_best_quality = arf_active_best_quality_max -
  1317. (int)(arf_active_best_quality_adjustment *
  1318. rc->arf_active_best_quality_adjustment_factor);
  1319. // Modify best quality for second level arfs. For mode VPX_Q this
  1320. // becomes the baseline frame q.
  1321. if (gf_group->rf_level[gf_group_index] == GF_ARF_LOW) {
  1322. const int layer_depth = gf_group->layer_depth[gf_group_index];
  1323. // linearly fit the frame q depending on the layer depth index from
  1324. // the base layer ARF.
  1325. active_best_quality =
  1326. ((layer_depth - 1) * q + active_best_quality + layer_depth / 2) /
  1327. layer_depth;
  1328. }
  1329. } else {
  1330. active_best_quality = inter_minq[active_worst_quality];
  1331. // For the constrained quality mode we don't want
  1332. // q to fall below the cq level.
  1333. if ((oxcf->rc_mode == VPX_CQ) && (active_best_quality < cq_level)) {
  1334. active_best_quality = cq_level;
  1335. }
  1336. }
  1337. // Extension to max or min Q if undershoot or overshoot is outside
  1338. // the permitted range.
  1339. if (frame_is_intra_only(cm) || boost_frame) {
  1340. active_best_quality -=
  1341. (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast);
  1342. active_worst_quality += (cpi->twopass.extend_maxq / 2);
  1343. } else {
  1344. active_best_quality -=
  1345. (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast) / 2;
  1346. active_worst_quality += cpi->twopass.extend_maxq;
  1347. // For normal frames do not allow an active minq lower than the q used for
  1348. // the last boosted frame.
  1349. active_best_quality = VPXMAX(active_best_quality, rc->last_boosted_qindex);
  1350. }
  1351. #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
  1352. vpx_clear_system_state();
  1353. // Static forced key frames Q restrictions dealt with elsewhere.
  1354. if (!frame_is_intra_only(cm) || !rc->this_key_frame_forced ||
  1355. cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH) {
  1356. int qdelta = vp9_frame_type_qdelta(cpi, gf_group->rf_level[gf_group_index],
  1357. active_worst_quality);
  1358. active_worst_quality =
  1359. VPXMAX(active_worst_quality + qdelta, active_best_quality);
  1360. }
  1361. #endif
  1362. // Modify active_best_quality for downscaled normal frames.
  1363. if (rc->frame_size_selector != UNSCALED && !frame_is_kf_gf_arf(cpi)) {
  1364. int qdelta = vp9_compute_qdelta_by_rate(
  1365. rc, cm->frame_type, active_best_quality, 2.0, cm->bit_depth);
  1366. active_best_quality =
  1367. VPXMAX(active_best_quality + qdelta, rc->best_quality);
  1368. }
  1369. active_best_quality =
  1370. clamp(active_best_quality, rc->best_quality, rc->worst_quality);
  1371. active_worst_quality =
  1372. clamp(active_worst_quality, active_best_quality, rc->worst_quality);
  1373. if (frame_is_intra_only(cm) && rc->this_key_frame_forced) {
  1374. // If static since last kf use better of last boosted and last kf q.
  1375. if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
  1376. q = VPXMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
  1377. } else {
  1378. q = rc->last_boosted_qindex;
  1379. }
  1380. } else if (frame_is_intra_only(cm) && !rc->this_key_frame_forced) {
  1381. q = active_best_quality;
  1382. } else {
  1383. q = vp9_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
  1384. active_worst_quality);
  1385. if (q > active_worst_quality) {
  1386. // Special case when we are targeting the max allowed rate.
  1387. if (rc->this_frame_target >= rc->max_frame_bandwidth)
  1388. active_worst_quality = q;
  1389. else
  1390. q = active_worst_quality;
  1391. }
  1392. }
  1393. clamp(q, active_best_quality, active_worst_quality);
  1394. *top_index = active_worst_quality;
  1395. *bottom_index = active_best_quality;
  1396. assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
  1397. assert(*bottom_index <= rc->worst_quality &&
  1398. *bottom_index >= rc->best_quality);
  1399. assert(q <= rc->worst_quality && q >= rc->best_quality);
  1400. return q;
  1401. }
  1402. int vp9_rc_pick_q_and_bounds(const VP9_COMP *cpi, int *bottom_index,
  1403. int *top_index) {
  1404. int q;
  1405. const int gf_group_index = cpi->twopass.gf_group.index;
  1406. if (cpi->oxcf.pass == 0) {
  1407. if (cpi->oxcf.rc_mode == VPX_CBR)
  1408. q = rc_pick_q_and_bounds_one_pass_cbr(cpi, bottom_index, top_index);
  1409. else
  1410. q = rc_pick_q_and_bounds_one_pass_vbr(cpi, bottom_index, top_index);
  1411. } else {
  1412. q = rc_pick_q_and_bounds_two_pass(cpi, bottom_index, top_index,
  1413. gf_group_index);
  1414. }
  1415. if (cpi->sf.use_nonrd_pick_mode) {
  1416. if (cpi->sf.force_frame_boost == 1) q -= cpi->sf.max_delta_qindex;
  1417. if (q < *bottom_index)
  1418. *bottom_index = q;
  1419. else if (q > *top_index)
  1420. *top_index = q;
  1421. }
  1422. return q;
  1423. }
  1424. void vp9_configure_buffer_updates(VP9_COMP *cpi, int gf_group_index) {
  1425. VP9_COMMON *cm = &cpi->common;
  1426. TWO_PASS *const twopass = &cpi->twopass;
  1427. cpi->rc.is_src_frame_alt_ref = 0;
  1428. cm->show_existing_frame = 0;
  1429. cpi->rc.show_arf_as_gld = 0;
  1430. switch (twopass->gf_group.update_type[gf_group_index]) {
  1431. case KF_UPDATE:
  1432. cpi->refresh_last_frame = 1;
  1433. cpi->refresh_golden_frame = 1;
  1434. cpi->refresh_alt_ref_frame = 1;
  1435. break;
  1436. case LF_UPDATE:
  1437. cpi->refresh_last_frame = 1;
  1438. cpi->refresh_golden_frame = 0;
  1439. cpi->refresh_alt_ref_frame = 0;
  1440. break;
  1441. case GF_UPDATE:
  1442. cpi->refresh_last_frame = 1;
  1443. cpi->refresh_golden_frame = 1;
  1444. cpi->refresh_alt_ref_frame = 0;
  1445. break;
  1446. case OVERLAY_UPDATE:
  1447. cpi->refresh_last_frame = 0;
  1448. cpi->refresh_golden_frame = 1;
  1449. cpi->refresh_alt_ref_frame = 0;
  1450. cpi->rc.is_src_frame_alt_ref = 1;
  1451. if (cpi->rc.preserve_arf_as_gld) {
  1452. cpi->rc.show_arf_as_gld = 1;
  1453. cpi->refresh_golden_frame = 0;
  1454. cm->show_existing_frame = 1;
  1455. cm->refresh_frame_context = 0;
  1456. }
  1457. break;
  1458. case MID_OVERLAY_UPDATE:
  1459. cpi->refresh_last_frame = 1;
  1460. cpi->refresh_golden_frame = 0;
  1461. cpi->refresh_alt_ref_frame = 0;
  1462. cpi->rc.is_src_frame_alt_ref = 1;
  1463. break;
  1464. case USE_BUF_FRAME:
  1465. cpi->refresh_last_frame = 0;
  1466. cpi->refresh_golden_frame = 0;
  1467. cpi->refresh_alt_ref_frame = 0;
  1468. cpi->rc.is_src_frame_alt_ref = 1;
  1469. cm->show_existing_frame = 1;
  1470. cm->refresh_frame_context = 0;
  1471. break;
  1472. default:
  1473. assert(twopass->gf_group.update_type[gf_group_index] == ARF_UPDATE);
  1474. cpi->refresh_last_frame = 0;
  1475. cpi->refresh_golden_frame = 0;
  1476. cpi->refresh_alt_ref_frame = 1;
  1477. break;
  1478. }
  1479. }
  1480. void vp9_estimate_qp_gop(VP9_COMP *cpi) {
  1481. int gop_length = cpi->twopass.gf_group.gf_group_size;
  1482. int bottom_index, top_index;
  1483. int idx;
  1484. const int gf_index = cpi->twopass.gf_group.index;
  1485. const int is_src_frame_alt_ref = cpi->rc.is_src_frame_alt_ref;
  1486. const int refresh_frame_context = cpi->common.refresh_frame_context;
  1487. for (idx = 1; idx <= gop_length; ++idx) {
  1488. TplDepFrame *tpl_frame = &cpi->tpl_stats[idx];
  1489. int target_rate = cpi->twopass.gf_group.bit_allocation[idx];
  1490. cpi->twopass.gf_group.index = idx;
  1491. vp9_rc_set_frame_target(cpi, target_rate);
  1492. vp9_configure_buffer_updates(cpi, idx);
  1493. tpl_frame->base_qindex =
  1494. rc_pick_q_and_bounds_two_pass(cpi, &bottom_index, &top_index, idx);
  1495. tpl_frame->base_qindex = VPXMAX(tpl_frame->base_qindex, 1);
  1496. }
  1497. // Reset the actual index and frame update
  1498. cpi->twopass.gf_group.index = gf_index;
  1499. cpi->rc.is_src_frame_alt_ref = is_src_frame_alt_ref;
  1500. cpi->common.refresh_frame_context = refresh_frame_context;
  1501. vp9_configure_buffer_updates(cpi, gf_index);
  1502. }
  1503. void vp9_rc_compute_frame_size_bounds(const VP9_COMP *cpi, int frame_target,
  1504. int *frame_under_shoot_limit,
  1505. int *frame_over_shoot_limit) {
  1506. if (cpi->oxcf.rc_mode == VPX_Q) {
  1507. *frame_under_shoot_limit = 0;
  1508. *frame_over_shoot_limit = INT_MAX;
  1509. } else {
  1510. // For very small rate targets where the fractional adjustment
  1511. // may be tiny make sure there is at least a minimum range.
  1512. const int tol_low = (cpi->sf.recode_tolerance_low * frame_target) / 100;
  1513. const int tol_high = (cpi->sf.recode_tolerance_high * frame_target) / 100;
  1514. *frame_under_shoot_limit = VPXMAX(frame_target - tol_low - 100, 0);
  1515. *frame_over_shoot_limit =
  1516. VPXMIN(frame_target + tol_high + 100, cpi->rc.max_frame_bandwidth);
  1517. }
  1518. }
  1519. void vp9_rc_set_frame_target(VP9_COMP *cpi, int target) {
  1520. const VP9_COMMON *const cm = &cpi->common;
  1521. RATE_CONTROL *const rc = &cpi->rc;
  1522. rc->this_frame_target = target;
  1523. // Modify frame size target when down-scaling.
  1524. if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC &&
  1525. rc->frame_size_selector != UNSCALED)
  1526. rc->this_frame_target = (int)(rc->this_frame_target *
  1527. rate_thresh_mult[rc->frame_size_selector]);
  1528. // Target rate per SB64 (including partial SB64s.
  1529. rc->sb64_target_rate = (int)(((int64_t)rc->this_frame_target * 64 * 64) /
  1530. (cm->width * cm->height));
  1531. }
  1532. static void update_alt_ref_frame_stats(VP9_COMP *cpi) {
  1533. // this frame refreshes means next frames don't unless specified by user
  1534. RATE_CONTROL *const rc = &cpi->rc;
  1535. rc->frames_since_golden = 0;
  1536. // Mark the alt ref as done (setting to 0 means no further alt refs pending).
  1537. rc->source_alt_ref_pending = 0;
  1538. // Set the alternate reference frame active flag
  1539. rc->source_alt_ref_active = 1;
  1540. }
  1541. static void update_golden_frame_stats(VP9_COMP *cpi) {
  1542. RATE_CONTROL *const rc = &cpi->rc;
  1543. // Update the Golden frame usage counts.
  1544. if (cpi->refresh_golden_frame) {
  1545. // this frame refreshes means next frames don't unless specified by user
  1546. rc->frames_since_golden = 0;
  1547. // If we are not using alt ref in the up and coming group clear the arf
  1548. // active flag. In multi arf group case, if the index is not 0 then
  1549. // we are overlaying a mid group arf so should not reset the flag.
  1550. if (cpi->oxcf.pass == 2) {
  1551. if (!rc->source_alt_ref_pending && (cpi->twopass.gf_group.index == 0))
  1552. rc->source_alt_ref_active = 0;
  1553. } else if (!rc->source_alt_ref_pending) {
  1554. rc->source_alt_ref_active = 0;
  1555. }
  1556. // Decrement count down till next gf
  1557. if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--;
  1558. } else if (!cpi->refresh_alt_ref_frame) {
  1559. // Decrement count down till next gf
  1560. if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--;
  1561. rc->frames_since_golden++;
  1562. if (rc->show_arf_as_gld) {
  1563. rc->frames_since_golden = 0;
  1564. // If we are not using alt ref in the up and coming group clear the arf
  1565. // active flag. In multi arf group case, if the index is not 0 then
  1566. // we are overlaying a mid group arf so should not reset the flag.
  1567. if (!rc->source_alt_ref_pending && (cpi->twopass.gf_group.index == 0))
  1568. rc->source_alt_ref_active = 0;
  1569. }
  1570. }
  1571. }
  1572. static void update_altref_usage(VP9_COMP *const cpi) {
  1573. VP9_COMMON *const cm = &cpi->common;
  1574. int sum_ref_frame_usage = 0;
  1575. int arf_frame_usage = 0;
  1576. int mi_row, mi_col;
  1577. if (cpi->rc.alt_ref_gf_group && !cpi->rc.is_src_frame_alt_ref &&
  1578. !cpi->refresh_golden_frame && !cpi->refresh_alt_ref_frame)
  1579. for (mi_row = 0; mi_row < cm->mi_rows; mi_row += 8) {
  1580. for (mi_col = 0; mi_col < cm->mi_cols; mi_col += 8) {
  1581. int sboffset = ((cm->mi_cols + 7) >> 3) * (mi_row >> 3) + (mi_col >> 3);
  1582. sum_ref_frame_usage += cpi->count_arf_frame_usage[sboffset] +
  1583. cpi->count_lastgolden_frame_usage[sboffset];
  1584. arf_frame_usage += cpi->count_arf_frame_usage[sboffset];
  1585. }
  1586. }
  1587. if (sum_ref_frame_usage > 0) {
  1588. double altref_count = 100.0 * arf_frame_usage / sum_ref_frame_usage;
  1589. cpi->rc.perc_arf_usage =
  1590. 0.75 * cpi->rc.perc_arf_usage + 0.25 * altref_count;
  1591. }
  1592. }
  1593. static void compute_frame_low_motion(VP9_COMP *const cpi) {
  1594. VP9_COMMON *const cm = &cpi->common;
  1595. int mi_row, mi_col;
  1596. MODE_INFO **mi = cm->mi_grid_visible;
  1597. RATE_CONTROL *const rc = &cpi->rc;
  1598. const int rows = cm->mi_rows, cols = cm->mi_cols;
  1599. int cnt_zeromv = 0;
  1600. for (mi_row = 0; mi_row < rows; mi_row++) {
  1601. for (mi_col = 0; mi_col < cols; mi_col++) {
  1602. if (mi[0]->ref_frame[0] == LAST_FRAME &&
  1603. abs(mi[0]->mv[0].as_mv.row) < 16 && abs(mi[0]->mv[0].as_mv.col) < 16)
  1604. cnt_zeromv++;
  1605. mi++;
  1606. }
  1607. mi += 8;
  1608. }
  1609. cnt_zeromv = 100 * cnt_zeromv / (rows * cols);
  1610. rc->avg_frame_low_motion = (3 * rc->avg_frame_low_motion + cnt_zeromv) >> 2;
  1611. }
  1612. void vp9_rc_postencode_update(VP9_COMP *cpi, uint64_t bytes_used) {
  1613. const VP9_COMMON *const cm = &cpi->common;
  1614. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  1615. RATE_CONTROL *const rc = &cpi->rc;
  1616. SVC *const svc = &cpi->svc;
  1617. const int qindex = cm->base_qindex;
  1618. // Update rate control heuristics
  1619. rc->projected_frame_size = (int)(bytes_used << 3);
  1620. // Post encode loop adjustment of Q prediction.
  1621. vp9_rc_update_rate_correction_factors(cpi);
  1622. // Keep a record of last Q and ambient average Q.
  1623. if (frame_is_intra_only(cm)) {
  1624. rc->last_q[KEY_FRAME] = qindex;
  1625. rc->avg_frame_qindex[KEY_FRAME] =
  1626. ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2);
  1627. if (cpi->use_svc) {
  1628. int i = 0;
  1629. SVC *svc = &cpi->svc;
  1630. for (i = 0; i < svc->number_temporal_layers; ++i) {
  1631. const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i,
  1632. svc->number_temporal_layers);
  1633. LAYER_CONTEXT *lc = &svc->layer_context[layer];
  1634. RATE_CONTROL *lrc = &lc->rc;
  1635. lrc->last_q[KEY_FRAME] = rc->last_q[KEY_FRAME];
  1636. lrc->avg_frame_qindex[KEY_FRAME] = rc->avg_frame_qindex[KEY_FRAME];
  1637. }
  1638. }
  1639. } else {
  1640. if ((cpi->use_svc && oxcf->rc_mode == VPX_CBR) ||
  1641. (!rc->is_src_frame_alt_ref &&
  1642. !(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
  1643. rc->last_q[INTER_FRAME] = qindex;
  1644. rc->avg_frame_qindex[INTER_FRAME] =
  1645. ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2);
  1646. rc->ni_frames++;
  1647. rc->tot_q += vp9_convert_qindex_to_q(qindex, cm->bit_depth);
  1648. rc->avg_q = rc->tot_q / rc->ni_frames;
  1649. // Calculate the average Q for normal inter frames (not key or GFU
  1650. // frames).
  1651. rc->ni_tot_qi += qindex;
  1652. rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames;
  1653. }
  1654. }
  1655. if (cpi->use_svc) vp9_svc_adjust_avg_frame_qindex(cpi);
  1656. // Keep record of last boosted (KF/KF/ARF) Q value.
  1657. // If the current frame is coded at a lower Q then we also update it.
  1658. // If all mbs in this group are skipped only update if the Q value is
  1659. // better than that already stored.
  1660. // This is used to help set quality in forced key frames to reduce popping
  1661. if ((qindex < rc->last_boosted_qindex) || (cm->frame_type == KEY_FRAME) ||
  1662. (!rc->constrained_gf_group &&
  1663. (cpi->refresh_alt_ref_frame ||
  1664. (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) {
  1665. rc->last_boosted_qindex = qindex;
  1666. }
  1667. if (frame_is_intra_only(cm)) rc->last_kf_qindex = qindex;
  1668. update_buffer_level_postencode(cpi, rc->projected_frame_size);
  1669. // Rolling monitors of whether we are over or underspending used to help
  1670. // regulate min and Max Q in two pass.
  1671. if (!frame_is_intra_only(cm)) {
  1672. rc->rolling_target_bits = ROUND_POWER_OF_TWO(
  1673. rc->rolling_target_bits * 3 + rc->this_frame_target, 2);
  1674. rc->rolling_actual_bits = ROUND_POWER_OF_TWO(
  1675. rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2);
  1676. rc->long_rolling_target_bits = ROUND_POWER_OF_TWO(
  1677. rc->long_rolling_target_bits * 31 + rc->this_frame_target, 5);
  1678. rc->long_rolling_actual_bits = ROUND_POWER_OF_TWO(
  1679. rc->long_rolling_actual_bits * 31 + rc->projected_frame_size, 5);
  1680. }
  1681. // Actual bits spent
  1682. rc->total_actual_bits += rc->projected_frame_size;
  1683. rc->total_target_bits += cm->show_frame ? rc->avg_frame_bandwidth : 0;
  1684. rc->total_target_vs_actual = rc->total_actual_bits - rc->total_target_bits;
  1685. if (!cpi->use_svc) {
  1686. if (is_altref_enabled(cpi) && cpi->refresh_alt_ref_frame &&
  1687. (!frame_is_intra_only(cm)))
  1688. // Update the alternate reference frame stats as appropriate.
  1689. update_alt_ref_frame_stats(cpi);
  1690. else
  1691. // Update the Golden frame stats as appropriate.
  1692. update_golden_frame_stats(cpi);
  1693. }
  1694. // If second (long term) temporal reference is used for SVC,
  1695. // update the golden frame counter, only for base temporal layer.
  1696. if (cpi->use_svc && svc->use_gf_temporal_ref_current_layer &&
  1697. svc->temporal_layer_id == 0) {
  1698. int i = 0;
  1699. if (cpi->refresh_golden_frame)
  1700. rc->frames_since_golden = 0;
  1701. else
  1702. rc->frames_since_golden++;
  1703. // Decrement count down till next gf
  1704. if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--;
  1705. // Update the frames_since_golden for all upper temporal layers.
  1706. for (i = 1; i < svc->number_temporal_layers; ++i) {
  1707. const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i,
  1708. svc->number_temporal_layers);
  1709. LAYER_CONTEXT *const lc = &svc->layer_context[layer];
  1710. RATE_CONTROL *const lrc = &lc->rc;
  1711. lrc->frames_since_golden = rc->frames_since_golden;
  1712. }
  1713. }
  1714. if (frame_is_intra_only(cm)) rc->frames_since_key = 0;
  1715. if (cm->show_frame) {
  1716. rc->frames_since_key++;
  1717. rc->frames_to_key--;
  1718. }
  1719. // Trigger the resizing of the next frame if it is scaled.
  1720. if (oxcf->pass != 0) {
  1721. cpi->resize_pending =
  1722. rc->next_frame_size_selector != rc->frame_size_selector;
  1723. rc->frame_size_selector = rc->next_frame_size_selector;
  1724. }
  1725. if (oxcf->pass == 0) {
  1726. if (!frame_is_intra_only(cm) &&
  1727. (!cpi->use_svc ||
  1728. (cpi->use_svc &&
  1729. !svc->layer_context[svc->temporal_layer_id].is_key_frame &&
  1730. svc->spatial_layer_id == svc->number_spatial_layers - 1))) {
  1731. compute_frame_low_motion(cpi);
  1732. if (cpi->sf.use_altref_onepass) update_altref_usage(cpi);
  1733. }
  1734. // For SVC: set avg_frame_low_motion (only computed on top spatial layer)
  1735. // to all lower spatial layers.
  1736. if (cpi->use_svc &&
  1737. svc->spatial_layer_id == svc->number_spatial_layers - 1) {
  1738. int i;
  1739. for (i = 0; i < svc->number_spatial_layers - 1; ++i) {
  1740. const int layer = LAYER_IDS_TO_IDX(i, svc->temporal_layer_id,
  1741. svc->number_temporal_layers);
  1742. LAYER_CONTEXT *const lc = &svc->layer_context[layer];
  1743. RATE_CONTROL *const lrc = &lc->rc;
  1744. lrc->avg_frame_low_motion = rc->avg_frame_low_motion;
  1745. }
  1746. }
  1747. cpi->rc.last_frame_is_src_altref = cpi->rc.is_src_frame_alt_ref;
  1748. }
  1749. if (!frame_is_intra_only(cm)) rc->reset_high_source_sad = 0;
  1750. rc->last_avg_frame_bandwidth = rc->avg_frame_bandwidth;
  1751. if (cpi->use_svc && svc->spatial_layer_id < svc->number_spatial_layers - 1)
  1752. svc->lower_layer_qindex = cm->base_qindex;
  1753. }
  1754. void vp9_rc_postencode_update_drop_frame(VP9_COMP *cpi) {
  1755. cpi->common.current_video_frame++;
  1756. cpi->rc.frames_since_key++;
  1757. cpi->rc.frames_to_key--;
  1758. cpi->rc.rc_2_frame = 0;
  1759. cpi->rc.rc_1_frame = 0;
  1760. cpi->rc.last_avg_frame_bandwidth = cpi->rc.avg_frame_bandwidth;
  1761. // For SVC on dropped frame when framedrop_mode != LAYER_DROP:
  1762. // in this mode the whole superframe may be dropped if only a single layer
  1763. // has buffer underflow (below threshold). Since this can then lead to
  1764. // increasing buffer levels/overflow for certain layers even though whole
  1765. // superframe is dropped, we cap buffer level if its already stable.
  1766. if (cpi->use_svc && cpi->svc.framedrop_mode != LAYER_DROP &&
  1767. cpi->rc.buffer_level > cpi->rc.optimal_buffer_level) {
  1768. cpi->rc.buffer_level = cpi->rc.optimal_buffer_level;
  1769. cpi->rc.bits_off_target = cpi->rc.optimal_buffer_level;
  1770. }
  1771. }
  1772. static int calc_pframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) {
  1773. const RATE_CONTROL *const rc = &cpi->rc;
  1774. const int af_ratio = rc->af_ratio_onepass_vbr;
  1775. int target =
  1776. (!rc->is_src_frame_alt_ref &&
  1777. (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))
  1778. ? (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio) /
  1779. (rc->baseline_gf_interval + af_ratio - 1)
  1780. : (rc->avg_frame_bandwidth * rc->baseline_gf_interval) /
  1781. (rc->baseline_gf_interval + af_ratio - 1);
  1782. return vp9_rc_clamp_pframe_target_size(cpi, target);
  1783. }
  1784. static int calc_iframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) {
  1785. static const int kf_ratio = 25;
  1786. const RATE_CONTROL *rc = &cpi->rc;
  1787. const int target = rc->avg_frame_bandwidth * kf_ratio;
  1788. return vp9_rc_clamp_iframe_target_size(cpi, target);
  1789. }
  1790. static void adjust_gfint_frame_constraint(VP9_COMP *cpi, int frame_constraint) {
  1791. RATE_CONTROL *const rc = &cpi->rc;
  1792. rc->constrained_gf_group = 0;
  1793. // Reset gf interval to make more equal spacing for frame_constraint.
  1794. if ((frame_constraint <= 7 * rc->baseline_gf_interval >> 2) &&
  1795. (frame_constraint > rc->baseline_gf_interval)) {
  1796. rc->baseline_gf_interval = frame_constraint >> 1;
  1797. if (rc->baseline_gf_interval < 5)
  1798. rc->baseline_gf_interval = frame_constraint;
  1799. rc->constrained_gf_group = 1;
  1800. } else {
  1801. // Reset to keep gf_interval <= frame_constraint.
  1802. if (rc->baseline_gf_interval > frame_constraint) {
  1803. rc->baseline_gf_interval = frame_constraint;
  1804. rc->constrained_gf_group = 1;
  1805. }
  1806. }
  1807. }
  1808. void vp9_rc_get_one_pass_vbr_params(VP9_COMP *cpi) {
  1809. VP9_COMMON *const cm = &cpi->common;
  1810. RATE_CONTROL *const rc = &cpi->rc;
  1811. int target;
  1812. if (!cpi->refresh_alt_ref_frame &&
  1813. (cm->current_video_frame == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY) ||
  1814. rc->frames_to_key == 0)) {
  1815. cm->frame_type = KEY_FRAME;
  1816. rc->this_key_frame_forced =
  1817. cm->current_video_frame != 0 && rc->frames_to_key == 0;
  1818. rc->frames_to_key = cpi->oxcf.key_freq;
  1819. rc->kf_boost = DEFAULT_KF_BOOST;
  1820. rc->source_alt_ref_active = 0;
  1821. } else {
  1822. cm->frame_type = INTER_FRAME;
  1823. }
  1824. if (rc->frames_till_gf_update_due == 0) {
  1825. double rate_err = 1.0;
  1826. rc->gfu_boost = DEFAULT_GF_BOOST;
  1827. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->oxcf.pass == 0) {
  1828. vp9_cyclic_refresh_set_golden_update(cpi);
  1829. } else {
  1830. rc->baseline_gf_interval = VPXMIN(
  1831. 20, VPXMAX(10, (rc->min_gf_interval + rc->max_gf_interval) / 2));
  1832. }
  1833. rc->af_ratio_onepass_vbr = 10;
  1834. if (rc->rolling_target_bits > 0)
  1835. rate_err =
  1836. (double)rc->rolling_actual_bits / (double)rc->rolling_target_bits;
  1837. if (cm->current_video_frame > 30) {
  1838. if (rc->avg_frame_qindex[INTER_FRAME] > (7 * rc->worst_quality) >> 3 &&
  1839. rate_err > 3.5) {
  1840. rc->baseline_gf_interval =
  1841. VPXMIN(15, (3 * rc->baseline_gf_interval) >> 1);
  1842. } else if (rc->avg_frame_low_motion < 20) {
  1843. // Decrease gf interval for high motion case.
  1844. rc->baseline_gf_interval = VPXMAX(6, rc->baseline_gf_interval >> 1);
  1845. }
  1846. // Adjust boost and af_ratio based on avg_frame_low_motion, which varies
  1847. // between 0 and 100 (stationary, 100% zero/small motion).
  1848. rc->gfu_boost =
  1849. VPXMAX(500, DEFAULT_GF_BOOST * (rc->avg_frame_low_motion << 1) /
  1850. (rc->avg_frame_low_motion + 100));
  1851. rc->af_ratio_onepass_vbr = VPXMIN(15, VPXMAX(5, 3 * rc->gfu_boost / 400));
  1852. }
  1853. adjust_gfint_frame_constraint(cpi, rc->frames_to_key);
  1854. rc->frames_till_gf_update_due = rc->baseline_gf_interval;
  1855. cpi->refresh_golden_frame = 1;
  1856. rc->source_alt_ref_pending = 0;
  1857. rc->alt_ref_gf_group = 0;
  1858. if (cpi->sf.use_altref_onepass && cpi->oxcf.enable_auto_arf) {
  1859. rc->source_alt_ref_pending = 1;
  1860. rc->alt_ref_gf_group = 1;
  1861. }
  1862. }
  1863. if (cm->frame_type == KEY_FRAME)
  1864. target = calc_iframe_target_size_one_pass_vbr(cpi);
  1865. else
  1866. target = calc_pframe_target_size_one_pass_vbr(cpi);
  1867. vp9_rc_set_frame_target(cpi, target);
  1868. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->oxcf.pass == 0)
  1869. vp9_cyclic_refresh_update_parameters(cpi);
  1870. }
  1871. static int calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
  1872. const VP9EncoderConfig *oxcf = &cpi->oxcf;
  1873. const RATE_CONTROL *rc = &cpi->rc;
  1874. const SVC *const svc = &cpi->svc;
  1875. const int64_t diff = rc->optimal_buffer_level - rc->buffer_level;
  1876. const int64_t one_pct_bits = 1 + rc->optimal_buffer_level / 100;
  1877. int min_frame_target =
  1878. VPXMAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS);
  1879. int target;
  1880. if (oxcf->gf_cbr_boost_pct) {
  1881. const int af_ratio_pct = oxcf->gf_cbr_boost_pct + 100;
  1882. target = cpi->refresh_golden_frame
  1883. ? (rc->avg_frame_bandwidth * rc->baseline_gf_interval *
  1884. af_ratio_pct) /
  1885. (rc->baseline_gf_interval * 100 + af_ratio_pct - 100)
  1886. : (rc->avg_frame_bandwidth * rc->baseline_gf_interval * 100) /
  1887. (rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
  1888. } else {
  1889. target = rc->avg_frame_bandwidth;
  1890. }
  1891. if (is_one_pass_cbr_svc(cpi)) {
  1892. // Note that for layers, avg_frame_bandwidth is the cumulative
  1893. // per-frame-bandwidth. For the target size of this frame, use the
  1894. // layer average frame size (i.e., non-cumulative per-frame-bw).
  1895. int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
  1896. svc->number_temporal_layers);
  1897. const LAYER_CONTEXT *lc = &svc->layer_context[layer];
  1898. target = lc->avg_frame_size;
  1899. min_frame_target = VPXMAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS);
  1900. }
  1901. if (diff > 0) {
  1902. // Lower the target bandwidth for this frame.
  1903. const int pct_low = (int)VPXMIN(diff / one_pct_bits, oxcf->under_shoot_pct);
  1904. target -= (target * pct_low) / 200;
  1905. } else if (diff < 0) {
  1906. // Increase the target bandwidth for this frame.
  1907. const int pct_high =
  1908. (int)VPXMIN(-diff / one_pct_bits, oxcf->over_shoot_pct);
  1909. target += (target * pct_high) / 200;
  1910. }
  1911. if (oxcf->rc_max_inter_bitrate_pct) {
  1912. const int max_rate =
  1913. rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100;
  1914. target = VPXMIN(target, max_rate);
  1915. }
  1916. return VPXMAX(min_frame_target, target);
  1917. }
  1918. static int calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
  1919. const RATE_CONTROL *rc = &cpi->rc;
  1920. const VP9EncoderConfig *oxcf = &cpi->oxcf;
  1921. const SVC *const svc = &cpi->svc;
  1922. int target;
  1923. if (cpi->common.current_video_frame == 0) {
  1924. target = ((rc->starting_buffer_level / 2) > INT_MAX)
  1925. ? INT_MAX
  1926. : (int)(rc->starting_buffer_level / 2);
  1927. } else {
  1928. int kf_boost = 32;
  1929. double framerate = cpi->framerate;
  1930. if (svc->number_temporal_layers > 1 && oxcf->rc_mode == VPX_CBR) {
  1931. // Use the layer framerate for temporal layers CBR mode.
  1932. const int layer =
  1933. LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
  1934. svc->number_temporal_layers);
  1935. const LAYER_CONTEXT *lc = &svc->layer_context[layer];
  1936. framerate = lc->framerate;
  1937. }
  1938. kf_boost = VPXMAX(kf_boost, (int)(2 * framerate - 16));
  1939. if (rc->frames_since_key < framerate / 2) {
  1940. kf_boost = (int)(kf_boost * rc->frames_since_key / (framerate / 2));
  1941. }
  1942. target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4;
  1943. }
  1944. return vp9_rc_clamp_iframe_target_size(cpi, target);
  1945. }
  1946. static void set_intra_only_frame(VP9_COMP *cpi) {
  1947. VP9_COMMON *const cm = &cpi->common;
  1948. SVC *const svc = &cpi->svc;
  1949. // Don't allow intra_only frame for bypass/flexible SVC mode, or if number
  1950. // of spatial layers is 1 or if number of spatial or temporal layers > 3.
  1951. // Also if intra-only is inserted on very first frame, don't allow if
  1952. // if number of temporal layers > 1. This is because on intra-only frame
  1953. // only 3 reference buffers can be updated, but for temporal layers > 1
  1954. // we generally need to use buffer slots 4 and 5.
  1955. if ((cm->current_video_frame == 0 && svc->number_temporal_layers > 1) ||
  1956. svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS ||
  1957. svc->number_spatial_layers > 3 || svc->number_temporal_layers > 3 ||
  1958. svc->number_spatial_layers == 1)
  1959. return;
  1960. cm->show_frame = 0;
  1961. cm->intra_only = 1;
  1962. cm->frame_type = INTER_FRAME;
  1963. cpi->ext_refresh_frame_flags_pending = 1;
  1964. cpi->ext_refresh_last_frame = 1;
  1965. cpi->ext_refresh_golden_frame = 1;
  1966. cpi->ext_refresh_alt_ref_frame = 1;
  1967. if (cm->current_video_frame == 0) {
  1968. cpi->lst_fb_idx = 0;
  1969. cpi->gld_fb_idx = 1;
  1970. cpi->alt_fb_idx = 2;
  1971. } else {
  1972. int i;
  1973. int count = 0;
  1974. cpi->lst_fb_idx = -1;
  1975. cpi->gld_fb_idx = -1;
  1976. cpi->alt_fb_idx = -1;
  1977. // For intra-only frame we need to refresh all slots that were
  1978. // being used for the base layer (fb_idx_base[i] == 1).
  1979. // Start with assigning last first, then golden and then alt.
  1980. for (i = 0; i < REF_FRAMES; ++i) {
  1981. if (svc->fb_idx_base[i] == 1) count++;
  1982. if (count == 1 && cpi->lst_fb_idx == -1) cpi->lst_fb_idx = i;
  1983. if (count == 2 && cpi->gld_fb_idx == -1) cpi->gld_fb_idx = i;
  1984. if (count == 3 && cpi->alt_fb_idx == -1) cpi->alt_fb_idx = i;
  1985. }
  1986. // If golden or alt is not being used for base layer, then set them
  1987. // to the lst_fb_idx.
  1988. if (cpi->gld_fb_idx == -1) cpi->gld_fb_idx = cpi->lst_fb_idx;
  1989. if (cpi->alt_fb_idx == -1) cpi->alt_fb_idx = cpi->lst_fb_idx;
  1990. }
  1991. }
  1992. void vp9_rc_get_svc_params(VP9_COMP *cpi) {
  1993. VP9_COMMON *const cm = &cpi->common;
  1994. RATE_CONTROL *const rc = &cpi->rc;
  1995. SVC *const svc = &cpi->svc;
  1996. int target = rc->avg_frame_bandwidth;
  1997. int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
  1998. svc->number_temporal_layers);
  1999. if (svc->first_spatial_layer_to_encode)
  2000. svc->layer_context[svc->temporal_layer_id].is_key_frame = 0;
  2001. // Periodic key frames is based on the super-frame counter
  2002. // (svc.current_superframe), also only base spatial layer is key frame.
  2003. // Key frame is set for any of the following: very first frame, frame flags
  2004. // indicates key, superframe counter hits key frequencey, or (non-intra) sync
  2005. // flag is set for spatial layer 0.
  2006. if ((cm->current_video_frame == 0 && !svc->previous_frame_is_intra_only) ||
  2007. (cpi->frame_flags & FRAMEFLAGS_KEY) ||
  2008. (cpi->oxcf.auto_key &&
  2009. (svc->current_superframe % cpi->oxcf.key_freq == 0) &&
  2010. !svc->previous_frame_is_intra_only && svc->spatial_layer_id == 0) ||
  2011. (svc->spatial_layer_sync[0] == 1 && svc->spatial_layer_id == 0)) {
  2012. cm->frame_type = KEY_FRAME;
  2013. rc->source_alt_ref_active = 0;
  2014. if (is_one_pass_cbr_svc(cpi)) {
  2015. if (cm->current_video_frame > 0) vp9_svc_reset_temporal_layers(cpi, 1);
  2016. layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
  2017. svc->number_temporal_layers);
  2018. svc->layer_context[layer].is_key_frame = 1;
  2019. cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
  2020. // Assumption here is that LAST_FRAME is being updated for a keyframe.
  2021. // Thus no change in update flags.
  2022. target = calc_iframe_target_size_one_pass_cbr(cpi);
  2023. }
  2024. } else {
  2025. cm->frame_type = INTER_FRAME;
  2026. if (is_one_pass_cbr_svc(cpi)) {
  2027. LAYER_CONTEXT *lc = &svc->layer_context[layer];
  2028. // Add condition current_video_frame > 0 for the case where first frame
  2029. // is intra only followed by overlay/copy frame. In this case we don't
  2030. // want to reset is_key_frame to 0 on overlay/copy frame.
  2031. lc->is_key_frame =
  2032. (svc->spatial_layer_id == 0 && cm->current_video_frame > 0)
  2033. ? 0
  2034. : svc->layer_context[svc->temporal_layer_id].is_key_frame;
  2035. target = calc_pframe_target_size_one_pass_cbr(cpi);
  2036. }
  2037. }
  2038. if (svc->simulcast_mode) {
  2039. if (svc->spatial_layer_id > 0 &&
  2040. svc->layer_context[layer].is_key_frame == 1) {
  2041. cm->frame_type = KEY_FRAME;
  2042. cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
  2043. target = calc_iframe_target_size_one_pass_cbr(cpi);
  2044. }
  2045. // Set the buffer idx and refresh flags for key frames in simulcast mode.
  2046. // Note the buffer slot for long-term reference is set below (line 2255),
  2047. // and alt_ref is used for that on key frame. So use last and golden for
  2048. // the other two normal slots.
  2049. if (cm->frame_type == KEY_FRAME) {
  2050. if (svc->number_spatial_layers == 2) {
  2051. if (svc->spatial_layer_id == 0) {
  2052. cpi->lst_fb_idx = 0;
  2053. cpi->gld_fb_idx = 2;
  2054. cpi->alt_fb_idx = 6;
  2055. } else if (svc->spatial_layer_id == 1) {
  2056. cpi->lst_fb_idx = 1;
  2057. cpi->gld_fb_idx = 3;
  2058. cpi->alt_fb_idx = 6;
  2059. }
  2060. } else if (svc->number_spatial_layers == 3) {
  2061. if (svc->spatial_layer_id == 0) {
  2062. cpi->lst_fb_idx = 0;
  2063. cpi->gld_fb_idx = 3;
  2064. cpi->alt_fb_idx = 6;
  2065. } else if (svc->spatial_layer_id == 1) {
  2066. cpi->lst_fb_idx = 1;
  2067. cpi->gld_fb_idx = 4;
  2068. cpi->alt_fb_idx = 6;
  2069. } else if (svc->spatial_layer_id == 2) {
  2070. cpi->lst_fb_idx = 2;
  2071. cpi->gld_fb_idx = 5;
  2072. cpi->alt_fb_idx = 7;
  2073. }
  2074. }
  2075. cpi->ext_refresh_last_frame = 1;
  2076. cpi->ext_refresh_golden_frame = 1;
  2077. cpi->ext_refresh_alt_ref_frame = 1;
  2078. }
  2079. }
  2080. // Check if superframe contains a sync layer request.
  2081. vp9_svc_check_spatial_layer_sync(cpi);
  2082. // If long term termporal feature is enabled, set the period of the update.
  2083. // The update/refresh of this reference frame is always on base temporal
  2084. // layer frame.
  2085. if (svc->use_gf_temporal_ref_current_layer) {
  2086. // Only use gf long-term prediction on non-key superframes.
  2087. if (!svc->layer_context[svc->temporal_layer_id].is_key_frame) {
  2088. // Use golden for this reference, which will be used for prediction.
  2089. int index = svc->spatial_layer_id;
  2090. if (svc->number_spatial_layers == 3) index = svc->spatial_layer_id - 1;
  2091. assert(index >= 0);
  2092. cpi->gld_fb_idx = svc->buffer_gf_temporal_ref[index].idx;
  2093. // Enable prediction off LAST (last reference) and golden (which will
  2094. // generally be further behind/long-term reference).
  2095. cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
  2096. }
  2097. // Check for update/refresh of reference: only refresh on base temporal
  2098. // layer.
  2099. if (svc->temporal_layer_id == 0) {
  2100. if (svc->layer_context[svc->temporal_layer_id].is_key_frame) {
  2101. // On key frame we update the buffer index used for long term reference.
  2102. // Use the alt_ref since it is not used or updated on key frames.
  2103. int index = svc->spatial_layer_id;
  2104. if (svc->number_spatial_layers == 3) index = svc->spatial_layer_id - 1;
  2105. assert(index >= 0);
  2106. cpi->alt_fb_idx = svc->buffer_gf_temporal_ref[index].idx;
  2107. cpi->ext_refresh_alt_ref_frame = 1;
  2108. } else if (rc->frames_till_gf_update_due == 0) {
  2109. // Set perdiod of next update. Make it a multiple of 10, as the cyclic
  2110. // refresh is typically ~10%, and we'd like the update to happen after
  2111. // a few cylces of the refresh (so it better quality frame). Note the
  2112. // cyclic refresh for SVC only operates on base temporal layer frames.
  2113. // Choose 20 as perdiod for now (2 cycles).
  2114. rc->baseline_gf_interval = 20;
  2115. rc->frames_till_gf_update_due = rc->baseline_gf_interval;
  2116. cpi->ext_refresh_golden_frame = 1;
  2117. rc->gfu_boost = DEFAULT_GF_BOOST;
  2118. }
  2119. }
  2120. } else if (!svc->use_gf_temporal_ref) {
  2121. rc->frames_till_gf_update_due = INT_MAX;
  2122. rc->baseline_gf_interval = INT_MAX;
  2123. }
  2124. if (svc->set_intra_only_frame) {
  2125. set_intra_only_frame(cpi);
  2126. target = calc_iframe_target_size_one_pass_cbr(cpi);
  2127. }
  2128. // Any update/change of global cyclic refresh parameters (amount/delta-qp)
  2129. // should be done here, before the frame qp is selected.
  2130. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
  2131. vp9_cyclic_refresh_update_parameters(cpi);
  2132. vp9_rc_set_frame_target(cpi, target);
  2133. if (cm->show_frame) update_buffer_level_svc_preencode(cpi);
  2134. }
  2135. void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) {
  2136. VP9_COMMON *const cm = &cpi->common;
  2137. RATE_CONTROL *const rc = &cpi->rc;
  2138. int target;
  2139. if ((cm->current_video_frame == 0) || (cpi->frame_flags & FRAMEFLAGS_KEY) ||
  2140. (cpi->oxcf.auto_key && rc->frames_to_key == 0)) {
  2141. cm->frame_type = KEY_FRAME;
  2142. rc->frames_to_key = cpi->oxcf.key_freq;
  2143. rc->kf_boost = DEFAULT_KF_BOOST;
  2144. rc->source_alt_ref_active = 0;
  2145. } else {
  2146. cm->frame_type = INTER_FRAME;
  2147. }
  2148. if (rc->frames_till_gf_update_due == 0) {
  2149. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
  2150. vp9_cyclic_refresh_set_golden_update(cpi);
  2151. else
  2152. rc->baseline_gf_interval =
  2153. (rc->min_gf_interval + rc->max_gf_interval) / 2;
  2154. rc->frames_till_gf_update_due = rc->baseline_gf_interval;
  2155. // NOTE: frames_till_gf_update_due must be <= frames_to_key.
  2156. if (rc->frames_till_gf_update_due > rc->frames_to_key)
  2157. rc->frames_till_gf_update_due = rc->frames_to_key;
  2158. cpi->refresh_golden_frame = 1;
  2159. rc->gfu_boost = DEFAULT_GF_BOOST;
  2160. }
  2161. // Any update/change of global cyclic refresh parameters (amount/delta-qp)
  2162. // should be done here, before the frame qp is selected.
  2163. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
  2164. vp9_cyclic_refresh_update_parameters(cpi);
  2165. if (frame_is_intra_only(cm))
  2166. target = calc_iframe_target_size_one_pass_cbr(cpi);
  2167. else
  2168. target = calc_pframe_target_size_one_pass_cbr(cpi);
  2169. vp9_rc_set_frame_target(cpi, target);
  2170. if (cm->show_frame) update_buffer_level_preencode(cpi);
  2171. if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC)
  2172. cpi->resize_pending = vp9_resize_one_pass_cbr(cpi);
  2173. else
  2174. cpi->resize_pending = 0;
  2175. }
  2176. int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
  2177. vpx_bit_depth_t bit_depth) {
  2178. int start_index = rc->worst_quality;
  2179. int target_index = rc->worst_quality;
  2180. int i;
  2181. // Convert the average q value to an index.
  2182. for (i = rc->best_quality; i < rc->worst_quality; ++i) {
  2183. start_index = i;
  2184. if (vp9_convert_qindex_to_q(i, bit_depth) >= qstart) break;
  2185. }
  2186. // Convert the q target to an index
  2187. for (i = rc->best_quality; i < rc->worst_quality; ++i) {
  2188. target_index = i;
  2189. if (vp9_convert_qindex_to_q(i, bit_depth) >= qtarget) break;
  2190. }
  2191. return target_index - start_index;
  2192. }
  2193. int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
  2194. int qindex, double rate_target_ratio,
  2195. vpx_bit_depth_t bit_depth) {
  2196. int target_index = rc->worst_quality;
  2197. int i;
  2198. // Look up the current projected bits per block for the base index
  2199. const int base_bits_per_mb =
  2200. vp9_rc_bits_per_mb(frame_type, qindex, 1.0, bit_depth);
  2201. // Find the target bits per mb based on the base value and given ratio.
  2202. const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb);
  2203. // Convert the q target to an index
  2204. for (i = rc->best_quality; i < rc->worst_quality; ++i) {
  2205. if (vp9_rc_bits_per_mb(frame_type, i, 1.0, bit_depth) <=
  2206. target_bits_per_mb) {
  2207. target_index = i;
  2208. break;
  2209. }
  2210. }
  2211. return target_index - qindex;
  2212. }
  2213. void vp9_rc_set_gf_interval_range(const VP9_COMP *const cpi,
  2214. RATE_CONTROL *const rc) {
  2215. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  2216. // Special case code for 1 pass fixed Q mode tests
  2217. if ((oxcf->pass == 0) && (oxcf->rc_mode == VPX_Q)) {
  2218. rc->max_gf_interval = FIXED_GF_INTERVAL;
  2219. rc->min_gf_interval = FIXED_GF_INTERVAL;
  2220. rc->static_scene_max_gf_interval = FIXED_GF_INTERVAL;
  2221. } else {
  2222. // Set Maximum gf/arf interval
  2223. rc->max_gf_interval = oxcf->max_gf_interval;
  2224. rc->min_gf_interval = oxcf->min_gf_interval;
  2225. if (rc->min_gf_interval == 0)
  2226. rc->min_gf_interval = vp9_rc_get_default_min_gf_interval(
  2227. oxcf->width, oxcf->height, cpi->framerate);
  2228. if (rc->max_gf_interval == 0)
  2229. rc->max_gf_interval = vp9_rc_get_default_max_gf_interval(
  2230. cpi->framerate, rc->min_gf_interval);
  2231. // Extended max interval for genuinely static scenes like slide shows.
  2232. rc->static_scene_max_gf_interval = MAX_STATIC_GF_GROUP_LENGTH;
  2233. if (rc->max_gf_interval > rc->static_scene_max_gf_interval)
  2234. rc->max_gf_interval = rc->static_scene_max_gf_interval;
  2235. // Clamp min to max
  2236. rc->min_gf_interval = VPXMIN(rc->min_gf_interval, rc->max_gf_interval);
  2237. if (oxcf->target_level == LEVEL_AUTO) {
  2238. const uint32_t pic_size = cpi->common.width * cpi->common.height;
  2239. const uint32_t pic_breadth =
  2240. VPXMAX(cpi->common.width, cpi->common.height);
  2241. int i;
  2242. for (i = LEVEL_1; i < LEVEL_MAX; ++i) {
  2243. if (vp9_level_defs[i].max_luma_picture_size >= pic_size &&
  2244. vp9_level_defs[i].max_luma_picture_breadth >= pic_breadth) {
  2245. if (rc->min_gf_interval <=
  2246. (int)vp9_level_defs[i].min_altref_distance) {
  2247. rc->min_gf_interval =
  2248. (int)vp9_level_defs[i].min_altref_distance + 1;
  2249. rc->max_gf_interval =
  2250. VPXMAX(rc->max_gf_interval, rc->min_gf_interval);
  2251. }
  2252. break;
  2253. }
  2254. }
  2255. }
  2256. }
  2257. }
  2258. void vp9_rc_update_framerate(VP9_COMP *cpi) {
  2259. const VP9_COMMON *const cm = &cpi->common;
  2260. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  2261. RATE_CONTROL *const rc = &cpi->rc;
  2262. int vbr_max_bits;
  2263. rc->avg_frame_bandwidth = (int)(oxcf->target_bandwidth / cpi->framerate);
  2264. rc->min_frame_bandwidth =
  2265. (int)(rc->avg_frame_bandwidth * oxcf->two_pass_vbrmin_section / 100);
  2266. rc->min_frame_bandwidth =
  2267. VPXMAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS);
  2268. // A maximum bitrate for a frame is defined.
  2269. // However this limit is extended if a very high rate is given on the command
  2270. // line or the the rate cannnot be acheived because of a user specificed max q
  2271. // (e.g. when the user specifies lossless encode).
  2272. //
  2273. // If a level is specified that requires a lower maximum rate then the level
  2274. // value take precedence.
  2275. vbr_max_bits =
  2276. (int)(((int64_t)rc->avg_frame_bandwidth * oxcf->two_pass_vbrmax_section) /
  2277. 100);
  2278. rc->max_frame_bandwidth =
  2279. VPXMAX(VPXMAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), vbr_max_bits);
  2280. vp9_rc_set_gf_interval_range(cpi, rc);
  2281. }
  2282. #define VBR_PCT_ADJUSTMENT_LIMIT 50
  2283. // For VBR...adjustment to the frame target based on error from previous frames
  2284. static void vbr_rate_correction(VP9_COMP *cpi, int *this_frame_target) {
  2285. RATE_CONTROL *const rc = &cpi->rc;
  2286. int64_t vbr_bits_off_target = rc->vbr_bits_off_target;
  2287. int max_delta;
  2288. int frame_window = VPXMIN(16, ((int)cpi->twopass.total_stats.count -
  2289. cpi->common.current_video_frame));
  2290. // Calcluate the adjustment to rate for this frame.
  2291. if (frame_window > 0) {
  2292. max_delta = (vbr_bits_off_target > 0)
  2293. ? (int)(vbr_bits_off_target / frame_window)
  2294. : (int)(-vbr_bits_off_target / frame_window);
  2295. max_delta = VPXMIN(max_delta,
  2296. ((*this_frame_target * VBR_PCT_ADJUSTMENT_LIMIT) / 100));
  2297. // vbr_bits_off_target > 0 means we have extra bits to spend
  2298. if (vbr_bits_off_target > 0) {
  2299. *this_frame_target += (vbr_bits_off_target > max_delta)
  2300. ? max_delta
  2301. : (int)vbr_bits_off_target;
  2302. } else {
  2303. *this_frame_target -= (vbr_bits_off_target < -max_delta)
  2304. ? max_delta
  2305. : (int)-vbr_bits_off_target;
  2306. }
  2307. }
  2308. // Fast redistribution of bits arising from massive local undershoot.
  2309. // Dont do it for kf,arf,gf or overlay frames.
  2310. if (!frame_is_kf_gf_arf(cpi) && !rc->is_src_frame_alt_ref &&
  2311. rc->vbr_bits_off_target_fast) {
  2312. int one_frame_bits = VPXMAX(rc->avg_frame_bandwidth, *this_frame_target);
  2313. int fast_extra_bits;
  2314. fast_extra_bits = (int)VPXMIN(rc->vbr_bits_off_target_fast, one_frame_bits);
  2315. fast_extra_bits = (int)VPXMIN(
  2316. fast_extra_bits,
  2317. VPXMAX(one_frame_bits / 8, rc->vbr_bits_off_target_fast / 8));
  2318. *this_frame_target += (int)fast_extra_bits;
  2319. rc->vbr_bits_off_target_fast -= fast_extra_bits;
  2320. }
  2321. }
  2322. void vp9_set_target_rate(VP9_COMP *cpi) {
  2323. RATE_CONTROL *const rc = &cpi->rc;
  2324. int target_rate = rc->base_frame_target;
  2325. if (cpi->common.frame_type == KEY_FRAME)
  2326. target_rate = vp9_rc_clamp_iframe_target_size(cpi, target_rate);
  2327. else
  2328. target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate);
  2329. if (!cpi->oxcf.vbr_corpus_complexity) {
  2330. // Correction to rate target based on prior over or under shoot.
  2331. if (cpi->oxcf.rc_mode == VPX_VBR || cpi->oxcf.rc_mode == VPX_CQ)
  2332. vbr_rate_correction(cpi, &target_rate);
  2333. }
  2334. vp9_rc_set_frame_target(cpi, target_rate);
  2335. }
  2336. // Check if we should resize, based on average QP from past x frames.
  2337. // Only allow for resize at most one scale down for now, scaling factor is 2.
  2338. int vp9_resize_one_pass_cbr(VP9_COMP *cpi) {
  2339. const VP9_COMMON *const cm = &cpi->common;
  2340. RATE_CONTROL *const rc = &cpi->rc;
  2341. RESIZE_ACTION resize_action = NO_RESIZE;
  2342. int avg_qp_thr1 = 70;
  2343. int avg_qp_thr2 = 50;
  2344. int min_width = 180;
  2345. int min_height = 180;
  2346. int down_size_on = 1;
  2347. cpi->resize_scale_num = 1;
  2348. cpi->resize_scale_den = 1;
  2349. // Don't resize on key frame; reset the counters on key frame.
  2350. if (cm->frame_type == KEY_FRAME) {
  2351. cpi->resize_avg_qp = 0;
  2352. cpi->resize_count = 0;
  2353. return 0;
  2354. }
  2355. // Check current frame reslution to avoid generating frames smaller than
  2356. // the minimum resolution.
  2357. if (ONEHALFONLY_RESIZE) {
  2358. if ((cm->width >> 1) < min_width || (cm->height >> 1) < min_height)
  2359. down_size_on = 0;
  2360. } else {
  2361. if (cpi->resize_state == ORIG &&
  2362. (cm->width * 3 / 4 < min_width || cm->height * 3 / 4 < min_height))
  2363. return 0;
  2364. else if (cpi->resize_state == THREE_QUARTER &&
  2365. ((cpi->oxcf.width >> 1) < min_width ||
  2366. (cpi->oxcf.height >> 1) < min_height))
  2367. down_size_on = 0;
  2368. }
  2369. #if CONFIG_VP9_TEMPORAL_DENOISING
  2370. // If denoiser is on, apply a smaller qp threshold.
  2371. if (cpi->oxcf.noise_sensitivity > 0) {
  2372. avg_qp_thr1 = 60;
  2373. avg_qp_thr2 = 40;
  2374. }
  2375. #endif
  2376. // Resize based on average buffer underflow and QP over some window.
  2377. // Ignore samples close to key frame, since QP is usually high after key.
  2378. if (cpi->rc.frames_since_key > 2 * cpi->framerate) {
  2379. const int window = (int)(4 * cpi->framerate);
  2380. cpi->resize_avg_qp += cm->base_qindex;
  2381. if (cpi->rc.buffer_level < (int)(30 * rc->optimal_buffer_level / 100))
  2382. ++cpi->resize_buffer_underflow;
  2383. ++cpi->resize_count;
  2384. // Check for resize action every "window" frames.
  2385. if (cpi->resize_count >= window) {
  2386. int avg_qp = cpi->resize_avg_qp / cpi->resize_count;
  2387. // Resize down if buffer level has underflowed sufficient amount in past
  2388. // window, and we are at original or 3/4 of original resolution.
  2389. // Resize back up if average QP is low, and we are currently in a resized
  2390. // down state, i.e. 1/2 or 3/4 of original resolution.
  2391. // Currently, use a flag to turn 3/4 resizing feature on/off.
  2392. if (cpi->resize_buffer_underflow > (cpi->resize_count >> 2)) {
  2393. if (cpi->resize_state == THREE_QUARTER && down_size_on) {
  2394. resize_action = DOWN_ONEHALF;
  2395. cpi->resize_state = ONE_HALF;
  2396. } else if (cpi->resize_state == ORIG) {
  2397. resize_action = ONEHALFONLY_RESIZE ? DOWN_ONEHALF : DOWN_THREEFOUR;
  2398. cpi->resize_state = ONEHALFONLY_RESIZE ? ONE_HALF : THREE_QUARTER;
  2399. }
  2400. } else if (cpi->resize_state != ORIG &&
  2401. avg_qp < avg_qp_thr1 * cpi->rc.worst_quality / 100) {
  2402. if (cpi->resize_state == THREE_QUARTER ||
  2403. avg_qp < avg_qp_thr2 * cpi->rc.worst_quality / 100 ||
  2404. ONEHALFONLY_RESIZE) {
  2405. resize_action = UP_ORIG;
  2406. cpi->resize_state = ORIG;
  2407. } else if (cpi->resize_state == ONE_HALF) {
  2408. resize_action = UP_THREEFOUR;
  2409. cpi->resize_state = THREE_QUARTER;
  2410. }
  2411. }
  2412. // Reset for next window measurement.
  2413. cpi->resize_avg_qp = 0;
  2414. cpi->resize_count = 0;
  2415. cpi->resize_buffer_underflow = 0;
  2416. }
  2417. }
  2418. // If decision is to resize, reset some quantities, and check is we should
  2419. // reduce rate correction factor,
  2420. if (resize_action != NO_RESIZE) {
  2421. int target_bits_per_frame;
  2422. int active_worst_quality;
  2423. int qindex;
  2424. int tot_scale_change;
  2425. if (resize_action == DOWN_THREEFOUR || resize_action == UP_THREEFOUR) {
  2426. cpi->resize_scale_num = 3;
  2427. cpi->resize_scale_den = 4;
  2428. } else if (resize_action == DOWN_ONEHALF) {
  2429. cpi->resize_scale_num = 1;
  2430. cpi->resize_scale_den = 2;
  2431. } else { // UP_ORIG or anything else
  2432. cpi->resize_scale_num = 1;
  2433. cpi->resize_scale_den = 1;
  2434. }
  2435. tot_scale_change = (cpi->resize_scale_den * cpi->resize_scale_den) /
  2436. (cpi->resize_scale_num * cpi->resize_scale_num);
  2437. // Reset buffer level to optimal, update target size.
  2438. rc->buffer_level = rc->optimal_buffer_level;
  2439. rc->bits_off_target = rc->optimal_buffer_level;
  2440. rc->this_frame_target = calc_pframe_target_size_one_pass_cbr(cpi);
  2441. // Get the projected qindex, based on the scaled target frame size (scaled
  2442. // so target_bits_per_mb in vp9_rc_regulate_q will be correct target).
  2443. target_bits_per_frame = (resize_action >= 0)
  2444. ? rc->this_frame_target * tot_scale_change
  2445. : rc->this_frame_target / tot_scale_change;
  2446. active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
  2447. qindex = vp9_rc_regulate_q(cpi, target_bits_per_frame, rc->best_quality,
  2448. active_worst_quality);
  2449. // If resize is down, check if projected q index is close to worst_quality,
  2450. // and if so, reduce the rate correction factor (since likely can afford
  2451. // lower q for resized frame).
  2452. if (resize_action > 0 && qindex > 90 * cpi->rc.worst_quality / 100) {
  2453. rc->rate_correction_factors[INTER_NORMAL] *= 0.85;
  2454. }
  2455. // If resize is back up, check if projected q index is too much above the
  2456. // current base_qindex, and if so, reduce the rate correction factor
  2457. // (since prefer to keep q for resized frame at least close to previous q).
  2458. if (resize_action < 0 && qindex > 130 * cm->base_qindex / 100) {
  2459. rc->rate_correction_factors[INTER_NORMAL] *= 0.9;
  2460. }
  2461. }
  2462. return resize_action;
  2463. }
  2464. static void adjust_gf_boost_lag_one_pass_vbr(VP9_COMP *cpi,
  2465. uint64_t avg_sad_current) {
  2466. VP9_COMMON *const cm = &cpi->common;
  2467. RATE_CONTROL *const rc = &cpi->rc;
  2468. int target;
  2469. int found = 0;
  2470. int found2 = 0;
  2471. int frame;
  2472. int i;
  2473. uint64_t avg_source_sad_lag = avg_sad_current;
  2474. int high_source_sad_lagindex = -1;
  2475. int steady_sad_lagindex = -1;
  2476. uint32_t sad_thresh1 = 70000;
  2477. uint32_t sad_thresh2 = 120000;
  2478. int low_content = 0;
  2479. int high_content = 0;
  2480. double rate_err = 1.0;
  2481. // Get measure of complexity over the future frames, and get the first
  2482. // future frame with high_source_sad/scene-change.
  2483. int tot_frames = (int)vp9_lookahead_depth(cpi->lookahead) - 1;
  2484. for (frame = tot_frames; frame >= 1; --frame) {
  2485. const int lagframe_idx = tot_frames - frame + 1;
  2486. uint64_t reference_sad = rc->avg_source_sad[0];
  2487. for (i = 1; i < lagframe_idx; ++i) {
  2488. if (rc->avg_source_sad[i] > 0)
  2489. reference_sad = (3 * reference_sad + rc->avg_source_sad[i]) >> 2;
  2490. }
  2491. // Detect up-coming scene change.
  2492. if (!found &&
  2493. (rc->avg_source_sad[lagframe_idx] >
  2494. VPXMAX(sad_thresh1, (unsigned int)(reference_sad << 1)) ||
  2495. rc->avg_source_sad[lagframe_idx] >
  2496. VPXMAX(3 * sad_thresh1 >> 2,
  2497. (unsigned int)(reference_sad << 2)))) {
  2498. high_source_sad_lagindex = lagframe_idx;
  2499. found = 1;
  2500. }
  2501. // Detect change from motion to steady.
  2502. if (!found2 && lagframe_idx > 1 && lagframe_idx < tot_frames &&
  2503. rc->avg_source_sad[lagframe_idx - 1] > (sad_thresh1 >> 2)) {
  2504. found2 = 1;
  2505. for (i = lagframe_idx; i < tot_frames; ++i) {
  2506. if (!(rc->avg_source_sad[i] > 0 &&
  2507. rc->avg_source_sad[i] < (sad_thresh1 >> 2) &&
  2508. rc->avg_source_sad[i] <
  2509. (rc->avg_source_sad[lagframe_idx - 1] >> 1))) {
  2510. found2 = 0;
  2511. i = tot_frames;
  2512. }
  2513. }
  2514. if (found2) steady_sad_lagindex = lagframe_idx;
  2515. }
  2516. avg_source_sad_lag += rc->avg_source_sad[lagframe_idx];
  2517. }
  2518. if (tot_frames > 0) avg_source_sad_lag = avg_source_sad_lag / tot_frames;
  2519. // Constrain distance between detected scene cuts.
  2520. if (high_source_sad_lagindex != -1 &&
  2521. high_source_sad_lagindex != rc->high_source_sad_lagindex - 1 &&
  2522. abs(high_source_sad_lagindex - rc->high_source_sad_lagindex) < 4)
  2523. rc->high_source_sad_lagindex = -1;
  2524. else
  2525. rc->high_source_sad_lagindex = high_source_sad_lagindex;
  2526. // Adjust some factors for the next GF group, ignore initial key frame,
  2527. // and only for lag_in_frames not too small.
  2528. if (cpi->refresh_golden_frame == 1 && cm->current_video_frame > 30 &&
  2529. cpi->oxcf.lag_in_frames > 8) {
  2530. int frame_constraint;
  2531. if (rc->rolling_target_bits > 0)
  2532. rate_err =
  2533. (double)rc->rolling_actual_bits / (double)rc->rolling_target_bits;
  2534. high_content = high_source_sad_lagindex != -1 ||
  2535. avg_source_sad_lag > (rc->prev_avg_source_sad_lag << 1) ||
  2536. avg_source_sad_lag > sad_thresh2;
  2537. low_content = high_source_sad_lagindex == -1 &&
  2538. ((avg_source_sad_lag < (rc->prev_avg_source_sad_lag >> 1)) ||
  2539. (avg_source_sad_lag < sad_thresh1));
  2540. if (low_content) {
  2541. rc->gfu_boost = DEFAULT_GF_BOOST;
  2542. rc->baseline_gf_interval =
  2543. VPXMIN(15, (3 * rc->baseline_gf_interval) >> 1);
  2544. } else if (high_content) {
  2545. rc->gfu_boost = DEFAULT_GF_BOOST >> 1;
  2546. rc->baseline_gf_interval = (rate_err > 3.0)
  2547. ? VPXMAX(10, rc->baseline_gf_interval >> 1)
  2548. : VPXMAX(6, rc->baseline_gf_interval >> 1);
  2549. }
  2550. if (rc->baseline_gf_interval > cpi->oxcf.lag_in_frames - 1)
  2551. rc->baseline_gf_interval = cpi->oxcf.lag_in_frames - 1;
  2552. // Check for constraining gf_interval for up-coming scene/content changes,
  2553. // or for up-coming key frame, whichever is closer.
  2554. frame_constraint = rc->frames_to_key;
  2555. if (rc->high_source_sad_lagindex > 0 &&
  2556. frame_constraint > rc->high_source_sad_lagindex)
  2557. frame_constraint = rc->high_source_sad_lagindex;
  2558. if (steady_sad_lagindex > 3 && frame_constraint > steady_sad_lagindex)
  2559. frame_constraint = steady_sad_lagindex;
  2560. adjust_gfint_frame_constraint(cpi, frame_constraint);
  2561. rc->frames_till_gf_update_due = rc->baseline_gf_interval;
  2562. // Adjust factors for active_worst setting & af_ratio for next gf interval.
  2563. rc->fac_active_worst_inter = 150; // corresponds to 3/2 (= 150 /100).
  2564. rc->fac_active_worst_gf = 100;
  2565. if (rate_err < 2.0 && !high_content) {
  2566. rc->fac_active_worst_inter = 120;
  2567. rc->fac_active_worst_gf = 90;
  2568. } else if (rate_err > 8.0 && rc->avg_frame_qindex[INTER_FRAME] < 16) {
  2569. // Increase active_worst faster at low Q if rate fluctuation is high.
  2570. rc->fac_active_worst_inter = 200;
  2571. if (rc->avg_frame_qindex[INTER_FRAME] < 8)
  2572. rc->fac_active_worst_inter = 400;
  2573. }
  2574. if (low_content && rc->avg_frame_low_motion > 80) {
  2575. rc->af_ratio_onepass_vbr = 15;
  2576. } else if (high_content || rc->avg_frame_low_motion < 30) {
  2577. rc->af_ratio_onepass_vbr = 5;
  2578. rc->gfu_boost = DEFAULT_GF_BOOST >> 2;
  2579. }
  2580. if (cpi->sf.use_altref_onepass && cpi->oxcf.enable_auto_arf) {
  2581. // Flag to disable usage of ARF based on past usage, only allow this
  2582. // disabling if current frame/group does not start with key frame or
  2583. // scene cut. Note perc_arf_usage is only computed for speed >= 5.
  2584. int arf_usage_low =
  2585. (cm->frame_type != KEY_FRAME && !rc->high_source_sad &&
  2586. cpi->rc.perc_arf_usage < 15 && cpi->oxcf.speed >= 5);
  2587. // Don't use alt-ref for this group under certain conditions.
  2588. if (arf_usage_low ||
  2589. (rc->high_source_sad_lagindex > 0 &&
  2590. rc->high_source_sad_lagindex <= rc->frames_till_gf_update_due) ||
  2591. (avg_source_sad_lag > 3 * sad_thresh1 >> 3)) {
  2592. rc->source_alt_ref_pending = 0;
  2593. rc->alt_ref_gf_group = 0;
  2594. } else {
  2595. rc->source_alt_ref_pending = 1;
  2596. rc->alt_ref_gf_group = 1;
  2597. // If alt-ref is used for this gf group, limit the interval.
  2598. if (rc->baseline_gf_interval > 12) {
  2599. rc->baseline_gf_interval = 12;
  2600. rc->frames_till_gf_update_due = rc->baseline_gf_interval;
  2601. }
  2602. }
  2603. }
  2604. target = calc_pframe_target_size_one_pass_vbr(cpi);
  2605. vp9_rc_set_frame_target(cpi, target);
  2606. }
  2607. rc->prev_avg_source_sad_lag = avg_source_sad_lag;
  2608. }
  2609. // Compute average source sad (temporal sad: between current source and
  2610. // previous source) over a subset of superblocks. Use this is detect big changes
  2611. // in content and allow rate control to react.
  2612. // This function also handles special case of lag_in_frames, to measure content
  2613. // level in #future frames set by the lag_in_frames.
  2614. void vp9_scene_detection_onepass(VP9_COMP *cpi) {
  2615. VP9_COMMON *const cm = &cpi->common;
  2616. RATE_CONTROL *const rc = &cpi->rc;
  2617. YV12_BUFFER_CONFIG const *unscaled_src = cpi->un_scaled_source;
  2618. YV12_BUFFER_CONFIG const *unscaled_last_src = cpi->unscaled_last_source;
  2619. uint8_t *src_y;
  2620. int src_ystride;
  2621. int src_width;
  2622. int src_height;
  2623. uint8_t *last_src_y;
  2624. int last_src_ystride;
  2625. int last_src_width;
  2626. int last_src_height;
  2627. if (cpi->un_scaled_source == NULL || cpi->unscaled_last_source == NULL ||
  2628. (cpi->use_svc && cpi->svc.current_superframe == 0))
  2629. return;
  2630. src_y = unscaled_src->y_buffer;
  2631. src_ystride = unscaled_src->y_stride;
  2632. src_width = unscaled_src->y_width;
  2633. src_height = unscaled_src->y_height;
  2634. last_src_y = unscaled_last_src->y_buffer;
  2635. last_src_ystride = unscaled_last_src->y_stride;
  2636. last_src_width = unscaled_last_src->y_width;
  2637. last_src_height = unscaled_last_src->y_height;
  2638. #if CONFIG_VP9_HIGHBITDEPTH
  2639. if (cm->use_highbitdepth) return;
  2640. #endif
  2641. rc->high_source_sad = 0;
  2642. rc->high_num_blocks_with_motion = 0;
  2643. // For SVC: scene detection is only checked on first spatial layer of
  2644. // the superframe using the original/unscaled resolutions.
  2645. if (cpi->svc.spatial_layer_id == cpi->svc.first_spatial_layer_to_encode &&
  2646. src_width == last_src_width && src_height == last_src_height) {
  2647. YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS] = { NULL };
  2648. int num_mi_cols = cm->mi_cols;
  2649. int num_mi_rows = cm->mi_rows;
  2650. int start_frame = 0;
  2651. int frames_to_buffer = 1;
  2652. int frame = 0;
  2653. int scene_cut_force_key_frame = 0;
  2654. int num_zero_temp_sad = 0;
  2655. uint64_t avg_sad_current = 0;
  2656. uint32_t min_thresh = 10000;
  2657. float thresh = 8.0f;
  2658. uint32_t thresh_key = 140000;
  2659. if (cpi->oxcf.speed <= 5) thresh_key = 240000;
  2660. if (cpi->oxcf.content != VP9E_CONTENT_SCREEN) min_thresh = 65000;
  2661. if (cpi->oxcf.rc_mode == VPX_VBR) thresh = 2.1f;
  2662. if (cpi->use_svc && cpi->svc.number_spatial_layers > 1) {
  2663. const int aligned_width = ALIGN_POWER_OF_TWO(src_width, MI_SIZE_LOG2);
  2664. const int aligned_height = ALIGN_POWER_OF_TWO(src_height, MI_SIZE_LOG2);
  2665. num_mi_cols = aligned_width >> MI_SIZE_LOG2;
  2666. num_mi_rows = aligned_height >> MI_SIZE_LOG2;
  2667. }
  2668. if (cpi->oxcf.lag_in_frames > 0) {
  2669. frames_to_buffer = (cm->current_video_frame == 1)
  2670. ? (int)vp9_lookahead_depth(cpi->lookahead) - 1
  2671. : 2;
  2672. start_frame = (int)vp9_lookahead_depth(cpi->lookahead) - 1;
  2673. for (frame = 0; frame < frames_to_buffer; ++frame) {
  2674. const int lagframe_idx = start_frame - frame;
  2675. if (lagframe_idx >= 0) {
  2676. struct lookahead_entry *buf =
  2677. vp9_lookahead_peek(cpi->lookahead, lagframe_idx);
  2678. frames[frame] = &buf->img;
  2679. }
  2680. }
  2681. // The avg_sad for this current frame is the value of frame#1
  2682. // (first future frame) from previous frame.
  2683. avg_sad_current = rc->avg_source_sad[1];
  2684. if (avg_sad_current >
  2685. VPXMAX(min_thresh,
  2686. (unsigned int)(rc->avg_source_sad[0] * thresh)) &&
  2687. cm->current_video_frame > (unsigned int)cpi->oxcf.lag_in_frames)
  2688. rc->high_source_sad = 1;
  2689. else
  2690. rc->high_source_sad = 0;
  2691. if (rc->high_source_sad && avg_sad_current > thresh_key)
  2692. scene_cut_force_key_frame = 1;
  2693. // Update recursive average for current frame.
  2694. if (avg_sad_current > 0)
  2695. rc->avg_source_sad[0] =
  2696. (3 * rc->avg_source_sad[0] + avg_sad_current) >> 2;
  2697. // Shift back data, starting at frame#1.
  2698. for (frame = 1; frame < cpi->oxcf.lag_in_frames - 1; ++frame)
  2699. rc->avg_source_sad[frame] = rc->avg_source_sad[frame + 1];
  2700. }
  2701. for (frame = 0; frame < frames_to_buffer; ++frame) {
  2702. if (cpi->oxcf.lag_in_frames == 0 ||
  2703. (frames[frame] != NULL && frames[frame + 1] != NULL &&
  2704. frames[frame]->y_width == frames[frame + 1]->y_width &&
  2705. frames[frame]->y_height == frames[frame + 1]->y_height)) {
  2706. int sbi_row, sbi_col;
  2707. const int lagframe_idx =
  2708. (cpi->oxcf.lag_in_frames == 0) ? 0 : start_frame - frame + 1;
  2709. const BLOCK_SIZE bsize = BLOCK_64X64;
  2710. // Loop over sub-sample of frame, compute average sad over 64x64 blocks.
  2711. uint64_t avg_sad = 0;
  2712. uint64_t tmp_sad = 0;
  2713. int num_samples = 0;
  2714. int sb_cols = (num_mi_cols + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
  2715. int sb_rows = (num_mi_rows + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
  2716. if (cpi->oxcf.lag_in_frames > 0) {
  2717. src_y = frames[frame]->y_buffer;
  2718. src_ystride = frames[frame]->y_stride;
  2719. last_src_y = frames[frame + 1]->y_buffer;
  2720. last_src_ystride = frames[frame + 1]->y_stride;
  2721. }
  2722. num_zero_temp_sad = 0;
  2723. for (sbi_row = 0; sbi_row < sb_rows; ++sbi_row) {
  2724. for (sbi_col = 0; sbi_col < sb_cols; ++sbi_col) {
  2725. // Checker-board pattern, ignore boundary.
  2726. if (((sbi_row > 0 && sbi_col > 0) &&
  2727. (sbi_row < sb_rows - 1 && sbi_col < sb_cols - 1) &&
  2728. ((sbi_row % 2 == 0 && sbi_col % 2 == 0) ||
  2729. (sbi_row % 2 != 0 && sbi_col % 2 != 0)))) {
  2730. tmp_sad = cpi->fn_ptr[bsize].sdf(src_y, src_ystride, last_src_y,
  2731. last_src_ystride);
  2732. avg_sad += tmp_sad;
  2733. num_samples++;
  2734. if (tmp_sad == 0) num_zero_temp_sad++;
  2735. }
  2736. src_y += 64;
  2737. last_src_y += 64;
  2738. }
  2739. src_y += (src_ystride << 6) - (sb_cols << 6);
  2740. last_src_y += (last_src_ystride << 6) - (sb_cols << 6);
  2741. }
  2742. if (num_samples > 0) avg_sad = avg_sad / num_samples;
  2743. // Set high_source_sad flag if we detect very high increase in avg_sad
  2744. // between current and previous frame value(s). Use minimum threshold
  2745. // for cases where there is small change from content that is completely
  2746. // static.
  2747. if (lagframe_idx == 0) {
  2748. if (avg_sad >
  2749. VPXMAX(min_thresh,
  2750. (unsigned int)(rc->avg_source_sad[0] * thresh)) &&
  2751. rc->frames_since_key > 1 + cpi->svc.number_spatial_layers &&
  2752. num_zero_temp_sad < 3 * (num_samples >> 2))
  2753. rc->high_source_sad = 1;
  2754. else
  2755. rc->high_source_sad = 0;
  2756. if (rc->high_source_sad && avg_sad > thresh_key)
  2757. scene_cut_force_key_frame = 1;
  2758. if (avg_sad > 0 || cpi->oxcf.rc_mode == VPX_CBR)
  2759. rc->avg_source_sad[0] = (3 * rc->avg_source_sad[0] + avg_sad) >> 2;
  2760. } else {
  2761. rc->avg_source_sad[lagframe_idx] = avg_sad;
  2762. }
  2763. if (num_zero_temp_sad < (3 * num_samples >> 2))
  2764. rc->high_num_blocks_with_motion = 1;
  2765. }
  2766. }
  2767. // For CBR non-screen content mode, check if we should reset the rate
  2768. // control. Reset is done if high_source_sad is detected and the rate
  2769. // control is at very low QP with rate correction factor at min level.
  2770. if (cpi->oxcf.rc_mode == VPX_CBR &&
  2771. cpi->oxcf.content != VP9E_CONTENT_SCREEN && !cpi->use_svc) {
  2772. if (rc->high_source_sad && rc->last_q[INTER_FRAME] == rc->best_quality &&
  2773. rc->avg_frame_qindex[INTER_FRAME] < (rc->best_quality << 1) &&
  2774. rc->rate_correction_factors[INTER_NORMAL] == MIN_BPB_FACTOR) {
  2775. rc->rate_correction_factors[INTER_NORMAL] = 0.5;
  2776. rc->avg_frame_qindex[INTER_FRAME] = rc->worst_quality;
  2777. rc->buffer_level = rc->optimal_buffer_level;
  2778. rc->bits_off_target = rc->optimal_buffer_level;
  2779. rc->reset_high_source_sad = 1;
  2780. }
  2781. if (cm->frame_type != KEY_FRAME && rc->reset_high_source_sad)
  2782. rc->this_frame_target = rc->avg_frame_bandwidth;
  2783. }
  2784. // For SVC the new (updated) avg_source_sad[0] for the current superframe
  2785. // updates the setting for all layers.
  2786. if (cpi->use_svc) {
  2787. int sl, tl;
  2788. SVC *const svc = &cpi->svc;
  2789. for (sl = 0; sl < svc->number_spatial_layers; ++sl)
  2790. for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
  2791. int layer = LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
  2792. LAYER_CONTEXT *const lc = &svc->layer_context[layer];
  2793. RATE_CONTROL *const lrc = &lc->rc;
  2794. lrc->avg_source_sad[0] = rc->avg_source_sad[0];
  2795. }
  2796. }
  2797. // For VBR, under scene change/high content change, force golden refresh.
  2798. if (cpi->oxcf.rc_mode == VPX_VBR && cm->frame_type != KEY_FRAME &&
  2799. rc->high_source_sad && rc->frames_to_key > 3 &&
  2800. rc->count_last_scene_change > 4 &&
  2801. cpi->ext_refresh_frame_flags_pending == 0) {
  2802. int target;
  2803. cpi->refresh_golden_frame = 1;
  2804. if (scene_cut_force_key_frame) cm->frame_type = KEY_FRAME;
  2805. rc->source_alt_ref_pending = 0;
  2806. if (cpi->sf.use_altref_onepass && cpi->oxcf.enable_auto_arf)
  2807. rc->source_alt_ref_pending = 1;
  2808. rc->gfu_boost = DEFAULT_GF_BOOST >> 1;
  2809. rc->baseline_gf_interval =
  2810. VPXMIN(20, VPXMAX(10, rc->baseline_gf_interval));
  2811. adjust_gfint_frame_constraint(cpi, rc->frames_to_key);
  2812. rc->frames_till_gf_update_due = rc->baseline_gf_interval;
  2813. target = calc_pframe_target_size_one_pass_vbr(cpi);
  2814. vp9_rc_set_frame_target(cpi, target);
  2815. rc->count_last_scene_change = 0;
  2816. } else {
  2817. rc->count_last_scene_change++;
  2818. }
  2819. // If lag_in_frame is used, set the gf boost and interval.
  2820. if (cpi->oxcf.lag_in_frames > 0)
  2821. adjust_gf_boost_lag_one_pass_vbr(cpi, avg_sad_current);
  2822. }
  2823. }
  2824. // Test if encoded frame will significantly overshoot the target bitrate, and
  2825. // if so, set the QP, reset/adjust some rate control parameters, and return 1.
  2826. // frame_size = -1 means frame has not been encoded.
  2827. int vp9_encodedframe_overshoot(VP9_COMP *cpi, int frame_size, int *q) {
  2828. VP9_COMMON *const cm = &cpi->common;
  2829. RATE_CONTROL *const rc = &cpi->rc;
  2830. SPEED_FEATURES *const sf = &cpi->sf;
  2831. int thresh_qp = 7 * (rc->worst_quality >> 3);
  2832. int thresh_rate = rc->avg_frame_bandwidth << 3;
  2833. // Lower thresh_qp for video (more overshoot at lower Q) to be
  2834. // more conservative for video.
  2835. if (cpi->oxcf.content != VP9E_CONTENT_SCREEN)
  2836. thresh_qp = 3 * (rc->worst_quality >> 2);
  2837. // If this decision is not based on an encoded frame size but just on
  2838. // scene/slide change detection (i.e., re_encode_overshoot_cbr_rt ==
  2839. // FAST_DETECTION_MAXQ), for now skip the (frame_size > thresh_rate)
  2840. // condition in this case.
  2841. // TODO(marpan): Use a better size/rate condition for this case and
  2842. // adjust thresholds.
  2843. if ((sf->overshoot_detection_cbr_rt == FAST_DETECTION_MAXQ ||
  2844. frame_size > thresh_rate) &&
  2845. cm->base_qindex < thresh_qp) {
  2846. double rate_correction_factor =
  2847. cpi->rc.rate_correction_factors[INTER_NORMAL];
  2848. const int target_size = cpi->rc.avg_frame_bandwidth;
  2849. double new_correction_factor;
  2850. int target_bits_per_mb;
  2851. double q2;
  2852. int enumerator;
  2853. // Force a re-encode, and for now use max-QP.
  2854. *q = cpi->rc.worst_quality;
  2855. cpi->cyclic_refresh->counter_encode_maxq_scene_change = 0;
  2856. cpi->rc.re_encode_maxq_scene_change = 1;
  2857. // If the frame_size is much larger than the threshold (big content change)
  2858. // and the encoded frame used alot of Intra modes, then force hybrid_intra
  2859. // encoding for the re-encode on this scene change. hybrid_intra will
  2860. // use rd-based intra mode selection for small blocks.
  2861. if (sf->overshoot_detection_cbr_rt == RE_ENCODE_MAXQ &&
  2862. frame_size > (thresh_rate << 1) && cpi->svc.spatial_layer_id == 0) {
  2863. MODE_INFO **mi = cm->mi_grid_visible;
  2864. int sum_intra_usage = 0;
  2865. int mi_row, mi_col;
  2866. int tot = 0;
  2867. for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
  2868. for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
  2869. if (mi[0]->ref_frame[0] == INTRA_FRAME) sum_intra_usage++;
  2870. tot++;
  2871. mi++;
  2872. }
  2873. mi += 8;
  2874. }
  2875. sum_intra_usage = 100 * sum_intra_usage / (cm->mi_rows * cm->mi_cols);
  2876. if (sum_intra_usage > 60) cpi->rc.hybrid_intra_scene_change = 1;
  2877. }
  2878. // Adjust avg_frame_qindex, buffer_level, and rate correction factors, as
  2879. // these parameters will affect QP selection for subsequent frames. If they
  2880. // have settled down to a very different (low QP) state, then not adjusting
  2881. // them may cause next frame to select low QP and overshoot again.
  2882. cpi->rc.avg_frame_qindex[INTER_FRAME] = *q;
  2883. rc->buffer_level = rc->optimal_buffer_level;
  2884. rc->bits_off_target = rc->optimal_buffer_level;
  2885. // Reset rate under/over-shoot flags.
  2886. cpi->rc.rc_1_frame = 0;
  2887. cpi->rc.rc_2_frame = 0;
  2888. // Adjust rate correction factor.
  2889. target_bits_per_mb =
  2890. (int)(((uint64_t)target_size << BPER_MB_NORMBITS) / cm->MBs);
  2891. // Rate correction factor based on target_bits_per_mb and qp (==max_QP).
  2892. // This comes from the inverse computation of vp9_rc_bits_per_mb().
  2893. q2 = vp9_convert_qindex_to_q(*q, cm->bit_depth);
  2894. enumerator = 1800000; // Factor for inter frame.
  2895. enumerator += (int)(enumerator * q2) >> 12;
  2896. new_correction_factor = (double)target_bits_per_mb * q2 / enumerator;
  2897. if (new_correction_factor > rate_correction_factor) {
  2898. rate_correction_factor =
  2899. VPXMIN(2.0 * rate_correction_factor, new_correction_factor);
  2900. if (rate_correction_factor > MAX_BPB_FACTOR)
  2901. rate_correction_factor = MAX_BPB_FACTOR;
  2902. cpi->rc.rate_correction_factors[INTER_NORMAL] = rate_correction_factor;
  2903. }
  2904. // For temporal layers, reset the rate control parametes across all
  2905. // temporal layers. If the first_spatial_layer_to_encode > 0, then this
  2906. // superframe has skipped lower base layers. So in this case we should also
  2907. // reset and force max-q for spatial layers < first_spatial_layer_to_encode.
  2908. if (cpi->use_svc) {
  2909. int tl = 0;
  2910. int sl = 0;
  2911. SVC *svc = &cpi->svc;
  2912. for (sl = 0; sl < svc->first_spatial_layer_to_encode; ++sl) {
  2913. for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
  2914. const int layer =
  2915. LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
  2916. LAYER_CONTEXT *lc = &svc->layer_context[layer];
  2917. RATE_CONTROL *lrc = &lc->rc;
  2918. lrc->avg_frame_qindex[INTER_FRAME] = *q;
  2919. lrc->buffer_level = lrc->optimal_buffer_level;
  2920. lrc->bits_off_target = lrc->optimal_buffer_level;
  2921. lrc->rc_1_frame = 0;
  2922. lrc->rc_2_frame = 0;
  2923. lrc->rate_correction_factors[INTER_NORMAL] = rate_correction_factor;
  2924. lrc->force_max_q = 1;
  2925. }
  2926. }
  2927. }
  2928. return 1;
  2929. } else {
  2930. return 0;
  2931. }
  2932. }